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