Flutter Engine
The Flutter Engine
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
14
15@protocol FlutterMetalDrawable <MTLDrawable>
16- (void)flutterPrepareForPresent:(nonnull id<MTLCommandBuffer>)commandBuffer;
17@end
18
19namespace flutter {
20
21static IOSContextMetalSkia* CastToMetalContext(const std::shared_ptr<IOSContext>& context)
22 __attribute__((cf_audited_transfer)) {
23 return (IOSContextMetalSkia*)context.get();
24}
25
27 std::shared_ptr<IOSContext> context)
28 : IOSSurface(std::move(context)),
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|
40
41// |IOSSurface|
42bool IOSSurfaceMetalSkia::IsValid() const {
43 return is_valid_;
44}
45
46// |IOSSurface|
47void IOSSurfaceMetalSkia::UpdateStorageSizeIfNecessary() {
48 // Nothing to do.
49}
50
51// |IOSSurface|
52std::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|
60GPUCAMetalLayerHandle 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|
83bool 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|
104GPUMTLTextureInfo 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|
110bool IOSSurfaceMetalSkia::PresentTexture(GPUMTLTextureInfo texture) const {
111 FML_CHECK(false) << "render to texture not supported on ios";
112 return false;
113}
114
115// |GPUSurfaceMetalDelegate|
116bool IOSSurfaceMetalSkia::AllowsDrawingWhenGpuDisabled() const {
117 return false;
118}
119
120} // namespace flutter
121
122#endif // !SLIMPELLER
const void * GrMTLHandle
Definition: GrMtlTypes.h:20
sk_sp< T > sk_ref_sp(T *obj)
Definition: SkRefCnt.h:381
Interface implemented by all platform surfaces that can present a metal backing store to the "screen"...
IOSSurfaceMetalSkia(const fml::scoped_nsobject< CAMetalLayer > &layer, std::shared_ptr< IOSContext > context)
std::shared_ptr< IOSContext > GetContext() const
Definition: ios_surface.mm:56
T get() const __attribute((ns_returns_not_retained))
#define FML_DLOG(severity)
Definition: logging.h:102
#define FML_CHECK(condition)
Definition: logging.h:85
#define FML_DCHECK(condition)
Definition: logging.h:103
FlTexture * texture
struct flutter::ImageMetaData __attribute__((packed))
static IOSContextMetalSkia * CastToMetalContext(const std::shared_ptr< IOSContext > &context) __attribute__((cf_audited_transfer))
Definition: ref_ptr.h:256
Definition: SkSize.h:16
constexpr int32_t width() const
Definition: SkSize.h:36
constexpr int32_t height() const
Definition: SkSize.h:37
#define ERROR(message)
Definition: elf_loader.cc:260