Flutter iOS Embedder
ios_surface_metal_skia.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 #if !SLIMPELLER
6 
8 
9 #include "flutter/shell/gpu/gpu_surface_metal_delegate.h"
10 #include "flutter/shell/gpu/gpu_surface_metal_skia.h"
12 
14 
15 @protocol FlutterMetalDrawable <MTLDrawable>
16 - (void)flutterPrepareForPresent:(nonnull id<MTLCommandBuffer>)commandBuffer;
17 @end
18 
19 namespace flutter {
20 
21 IOSSurfaceMetalSkia::IOSSurfaceMetalSkia(CAMetalLayer* layer, std::shared_ptr<IOSContext> context)
22  : IOSSurface(std::move(context)),
23  GPUSurfaceMetalDelegate(MTLRenderTargetType::kCAMetalLayer),
24  layer_(layer) {
25  is_valid_ = layer_;
26  IOSContextMetalSkia* metal_context = static_cast<IOSContextMetalSkia*>(GetContext().get());
27  FlutterDarwinContextMetalSkia* darwin_context = metal_context->GetDarwinContext();
28  command_queue_ = darwin_context.commandQueue;
29  device_ = darwin_context.device;
30 }
31 
32 // |IOSSurface|
34 
35 // |IOSSurface|
36 bool IOSSurfaceMetalSkia::IsValid() const {
37  return is_valid_;
38 }
39 
40 // |IOSSurface|
41 void IOSSurfaceMetalSkia::UpdateStorageSizeIfNecessary() {
42  // Nothing to do.
43 }
44 
45 // |IOSSurface|
46 std::unique_ptr<Surface> IOSSurfaceMetalSkia::CreateGPUSurface(GrDirectContext* context) {
47  FML_DCHECK(context);
48  return std::make_unique<GPUSurfaceMetalSkia>(this, // delegate
49  sk_ref_sp(context) // context
50  );
51 }
52 
53 // |GPUSurfaceMetalDelegate|
54 GPUCAMetalLayerHandle IOSSurfaceMetalSkia::GetCAMetalLayer(const SkISize& frame_info) const {
55  layer_.device = device_;
56 
57  layer_.pixelFormat = MTLPixelFormatBGRA8Unorm;
58  // Flutter needs to read from the color attachment in cases where there are effects such as
59  // backdrop filters. Flutter plugins that create platform views may also read from the layer.
60  layer_.framebufferOnly = NO;
61 
62  const auto drawable_size = CGSizeMake(frame_info.width(), frame_info.height());
63  if (!CGSizeEqualToSize(drawable_size, layer_.drawableSize)) {
64  layer_.drawableSize = drawable_size;
65  }
66 
67  // When there are platform views in the scene, the drawable needs to be presented in the same
68  // transaction as the one created for platform views. When the drawable are being presented from
69  // the raster thread, there is no such transaction.
70  layer_.presentsWithTransaction = [[NSThread currentThread] isMainThread];
71 
72  return (__bridge GPUCAMetalLayerHandle)layer_;
73 }
74 
75 // |GPUSurfaceMetalDelegate|
76 bool IOSSurfaceMetalSkia::PreparePresent(GrMTLHandle drawable) const {
77  id<MTLCommandBuffer> command_buffer = [command_queue_ commandBuffer];
78  id<CAMetalDrawable> metal_drawable = (__bridge id<CAMetalDrawable>)drawable;
79  if ([metal_drawable conformsToProtocol:@protocol(FlutterMetalDrawable)]) {
80  [(id<FlutterMetalDrawable>)metal_drawable flutterPrepareForPresent:command_buffer];
81  }
82  [command_buffer commit];
83  [command_buffer waitUntilScheduled];
84  return true;
85 }
86 
87 // |GPUSurfaceMetalDelegate|
88 bool IOSSurfaceMetalSkia::PresentDrawable(GrMTLHandle drawable) const {
89  if (drawable == nullptr) {
90  FML_DLOG(ERROR) << "Could not acquire next Metal drawable from the SkSurface.";
91  return false;
92  }
93 
94  id<CAMetalDrawable> metal_drawable = (__bridge id<CAMetalDrawable>)drawable;
95  [metal_drawable present];
96  return true;
97 }
98 
99 // |GPUSurfaceMetalDelegate|
100 GPUMTLTextureInfo IOSSurfaceMetalSkia::GetMTLTexture(const SkISize& frame_info) const {
101  FML_CHECK(false) << "render to texture not supported on ios";
102  return {.texture_id = -1, .texture = nullptr};
103 }
104 
105 // |GPUSurfaceMetalDelegate|
106 bool IOSSurfaceMetalSkia::PresentTexture(GPUMTLTextureInfo texture) const {
107  FML_CHECK(false) << "render to texture not supported on ios";
108  return false;
109 }
110 
111 // |GPUSurfaceMetalDelegate|
112 bool IOSSurfaceMetalSkia::AllowsDrawingWhenGpuDisabled() const {
113  return false;
114 }
115 
116 } // namespace flutter
117 
118 #endif // !SLIMPELLER
flutter::IOSContextMetalSkia::GetDarwinContext
FlutterDarwinContextMetalSkia * GetDarwinContext() const
Definition: ios_context_metal_skia.mm:25
ios_surface_metal_skia.h
flutter::IOSSurfaceMetalSkia::~IOSSurfaceMetalSkia
~IOSSurfaceMetalSkia()
flutter::IOSSurface
Definition: ios_surface.h:20
FlutterMetalDrawable-p
Definition: FlutterMetalLayer.h:37
ios_context_metal_skia.h
flutter::IOSContextMetalSkia
Definition: ios_context_metal_skia.h:20
flutter
Definition: accessibility_bridge.h:27
flutter::IOSSurface::GetContext
std::shared_ptr< IOSContext > GetContext() const
Definition: ios_surface.mm:62
flutter::IOSSurfaceMetalSkia::IOSSurfaceMetalSkia
IOSSurfaceMetalSkia(CAMetalLayer *layer, std::shared_ptr< IOSContext > context)
Definition: ios_surface_metal_skia.mm:21
FLUTTER_ASSERT_ARC
Definition: FlutterChannelKeyResponder.mm:13