Flutter Engine
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 #include "third_party/skia/include/utils/mac/SkCGUtils.h"
15 
16 namespace flutter {
17 
19  std::shared_ptr<IOSContext> context)
20  : IOSSurface(std::move(context)), layer_(std::move(layer)) {}
21 
23 
25  return layer_;
26 }
27 
29  // Nothing to do here. We don't need an external entity to tell us when our
30  // backing store needs to be updated. Instead, we let the frame tell us its
31  // size so we can update to match. This method was added to work around
32  // Android oddities.
33 }
34 
35 std::unique_ptr<Surface> IOSSurfaceSoftware::CreateGPUSurface(GrDirectContext* gr_context) {
36  if (!IsValid()) {
37  return nullptr;
38  }
39 
40  auto surface = std::make_unique<GPUSurfaceSoftware>(this, true /* render to surface */);
41 
42  if (!surface->IsValid()) {
43  return nullptr;
44  }
45 
46  return surface;
47 }
48 
49 sk_sp<SkSurface> IOSSurfaceSoftware::AcquireBackingStore(const SkISize& size) {
50  TRACE_EVENT0("flutter", "IOSSurfaceSoftware::AcquireBackingStore");
51  if (!IsValid()) {
52  return nullptr;
53  }
54 
55  if (sk_surface_ != nullptr &&
56  SkISize::Make(sk_surface_->width(), sk_surface_->height()) == size) {
57  // The old and new surface sizes are the same. Nothing to do here.
58  return sk_surface_;
59  }
60 
61  SkImageInfo info = SkImageInfo::MakeN32(size.fWidth, size.fHeight, kPremul_SkAlphaType,
62  SkColorSpace::MakeSRGB());
63  sk_surface_ = SkSurface::MakeRaster(info, nullptr);
64  return sk_surface_;
65 }
66 
67 bool IOSSurfaceSoftware::PresentBackingStore(sk_sp<SkSurface> backing_store) {
68  TRACE_EVENT0("flutter", "IOSSurfaceSoftware::PresentBackingStore");
69  if (!IsValid() || backing_store == nullptr) {
70  return false;
71  }
72 
73  SkPixmap pixmap;
74  if (!backing_store->peekPixels(&pixmap)) {
75  return false;
76  }
77 
78  // Some basic sanity checking.
79  uint64_t expected_pixmap_data_size = pixmap.width() * pixmap.height() * 4;
80 
81  const size_t pixmap_size = pixmap.computeByteSize();
82 
83  if (expected_pixmap_data_size != pixmap_size) {
84  return false;
85  }
86 
87  fml::CFRef<CGColorSpaceRef> colorspace(CGColorSpaceCreateDeviceRGB());
88 
89  // Setup the data provider that gives CG a view into the pixmap.
90  fml::CFRef<CGDataProviderRef> pixmap_data_provider(CGDataProviderCreateWithData(
91  nullptr, // info
92  pixmap.addr32(), // data
93  pixmap_size, // size
94  nullptr // release callback
95  ));
96 
97  if (!pixmap_data_provider) {
98  return false;
99  }
100 
101  // Create the CGImageRef representation on the pixmap.
102  fml::CFRef<CGImageRef> pixmap_image(CGImageCreate(pixmap.width(), // width
103  pixmap.height(), // height
104  8, // bits per component
105  32, // bits per pixel
106  pixmap.rowBytes(), // bytes per row
107  colorspace, // colorspace
108  kCGImageAlphaPremultipliedLast, // bitmap info
109  pixmap_data_provider, // data provider
110  nullptr, // decode array
111  false, // should interpolate
112  kCGRenderingIntentDefault // rendering intent
113  ));
114 
115  if (!pixmap_image) {
116  return false;
117  }
118 
119  layer_.get().contents = reinterpret_cast<id>(static_cast<CGImageRef>(pixmap_image));
120 
121  return true;
122 }
123 
124 } // namespace flutter
#define TRACE_EVENT0(category_group, name)
Definition: trace_event.h:90
sk_sp< SkSurface > AcquireBackingStore(const SkISize &size) override
Called when the GPU surface needs a new buffer to render a new frame into.
void UpdateStorageSizeIfNecessary() override
IOSSurfaceSoftware(fml::scoped_nsobject< CALayer > layer, std::shared_ptr< IOSContext > context)
Definition: ref_ptr.h:252
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
std::unique_ptr< Surface > CreateGPUSurface(GrDirectContext *gr_context=nullptr) override
bool PresentBackingStore(sk_sp< SkSurface > backing_store) override
Called by the platform when a frame has been rendered into the backing store and the platform must di...