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