| Flutter iOS Embedder |

ios_surface_software.mm
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #import "flutter/shell/platform/darwin/ios/ios_surface_software.h"
6
7 #include <QuartzCore/CALayer.h>
8
9 #include <memory>
10
11 #include "flutter/fml/logging.h"
12 #include "flutter/fml/platform/darwin/cf_utils.h"
13 #include "flutter/fml/trace_event.h"
14
15 #include "third_party/skia/include/core/SkSurface.h"
16 #include "third_party/skia/include/utils/mac/SkCGUtils.h"
17
18 FLUTTER_ASSERT_ARC
19
20 namespace flutter {
21
22 IOSSurfaceSoftware::IOSSurfaceSoftware(const fml::scoped_nsobject<CALayer>& layer,
23 std::shared_ptr<IOSContext> context)
24 : IOSSurface(std::move(context)), layer_(layer) {}
25
26 IOSSurfaceSoftware::~IOSSurfaceSoftware() = default;
27
28 bool IOSSurfaceSoftware::IsValid() const {
29 return layer_;
30 }
31
32 void IOSSurfaceSoftware::UpdateStorageSizeIfNecessary() {
33 // Nothing to do here. We don't need an external entity to tell us when our
34 // backing store needs to be updated. Instead, we let the frame tell us its
35 // size so we can update to match. This method was added to work around
36 // Android oddities.
37 }
38
39 std::unique_ptr<Surface> IOSSurfaceSoftware::CreateGPUSurface(GrDirectContext* gr_context) {
40 if (!IsValid()) {
41 return nullptr;
42 }
43
44 auto surface = std::make_unique<GPUSurfaceSoftware>(this, true /* render to surface */);
45
46 if (!surface->IsValid()) {
47 return nullptr;
48 }
49
50 return surface;
51 }
52
53 sk_sp<SkSurface> IOSSurfaceSoftware::AcquireBackingStore(const SkISize& size) {
54 TRACE_EVENT0("flutter", "IOSSurfaceSoftware::AcquireBackingStore");
55 if (!IsValid()) {
56 return nullptr;
57 }
58
59 if (sk_surface_ != nullptr &&
60 SkISize::Make(sk_surface_->width(), sk_surface_->height()) == size) {
61 // The old and new surface sizes are the same. Nothing to do here.
62 return sk_surface_;
63 }
64
65 SkImageInfo info = SkImageInfo::MakeN32(size.fWidth, size.fHeight, kPremul_SkAlphaType,
66 SkColorSpace::MakeSRGB());
67 sk_surface_ = SkSurfaces::Raster(info, nullptr);
68 return sk_surface_;
69 }
70
71 bool IOSSurfaceSoftware::PresentBackingStore(sk_sp<SkSurface> backing_store) {
72 TRACE_EVENT0("flutter", "IOSSurfaceSoftware::PresentBackingStore");
73 if (!IsValid() || backing_store == nullptr) {
74 return false;
75 }
76
77 SkPixmap pixmap;
78 if (!backing_store->peekPixels(&pixmap)) {
79 return false;
80 }
81
82 // Some basic sanity checking.
83 uint64_t expected_pixmap_data_size = pixmap.width() * pixmap.height() * 4;
84
85 const size_t pixmap_size = pixmap.computeByteSize();
86
87 if (expected_pixmap_data_size != pixmap_size) {
88 return false;
89 }
90
91 fml::CFRef<CGColorSpaceRef> colorspace(CGColorSpaceCreateDeviceRGB());
92
93 // Setup the data provider that gives CG a view into the pixmap.
94 fml::CFRef<CGDataProviderRef> pixmap_data_provider(CGDataProviderCreateWithData(
95 nullptr, // info
96 pixmap.addr32(), // data
97 pixmap_size, // size
98 nullptr // release callback
99 ));
100
101 if (!pixmap_data_provider) {
102 return false;
103 }
104
105 // Create the CGImageRef representation on the pixmap.
106 fml::CFRef<CGImageRef> pixmap_image(CGImageCreate(pixmap.width(), // width
107 pixmap.height(), // height
108 8, // bits per component
109 32, // bits per pixel
110 pixmap.rowBytes(), // bytes per row
111 colorspace, // colorspace
112 kCGImageAlphaPremultipliedLast, // bitmap info
113 pixmap_data_provider, // data provider
114 nullptr, // decode array
115 false, // should interpolate
116 kCGRenderingIntentDefault // rendering intent
117 ));
118
119 if (!pixmap_image) {
120 return false;
121 }
122
123 layer_.get().contents = (__bridge id)(static_cast<CGImageRef>(pixmap_image));
124
125 return true;
126 }
127
128 } // namespace flutter
flutter::IOSSurfaceSoftware::IsValid
bool IsValid() const override
Definition: ios_surface_software.mm:28
flutter::IOSSurfaceSoftware::PresentBackingStore
bool PresentBackingStore(sk_sp< SkSurface > backing_store) override
Definition: ios_surface_software.mm:71
ios_surface_software.h
flutter::IOSSurfaceSoftware::IOSSurfaceSoftware
IOSSurfaceSoftware(const fml::scoped_nsobject< CALayer > &layer, std::shared_ptr< IOSContext > context)
Definition: ios_surface_software.mm:22
flutter::IOSSurface
Definition: ios_surface.h:25
flutter::IOSSurfaceSoftware::AcquireBackingStore
sk_sp< SkSurface > AcquireBackingStore(const SkISize &size) override
Definition: ios_surface_software.mm:53
flutter
Definition: accessibility_bridge.h:28
flutter::IOSSurfaceSoftware::UpdateStorageSizeIfNecessary
void UpdateStorageSizeIfNecessary() override
Definition: ios_surface_software.mm:32
flutter::IOSSurfaceSoftware::CreateGPUSurface
std::unique_ptr< Surface > CreateGPUSurface(GrDirectContext *gr_context=nullptr) override
Definition: ios_surface_software.mm:39
FLUTTER_ASSERT_ARC
Definition: FlutterChannelKeyResponder.mm:13
flutter::IOSSurfaceSoftware::~IOSSurfaceSoftware
~IOSSurfaceSoftware() override