Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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
12
13@protocol FlutterMetalDrawable <MTLDrawable>
14- (void)flutterPrepareForPresent:(nonnull id<MTLCommandBuffer>)commandBuffer;
15@end
16
17namespace flutter {
18
19static IOSContextMetalSkia* CastToMetalContext(const std::shared_ptr<IOSContext>& context)
20 __attribute__((cf_audited_transfer)) {
21 return (IOSContextMetalSkia*)context.get();
22}
23
25 std::shared_ptr<IOSContext> context)
26 : IOSSurface(std::move(context)),
28 layer_(layer) {
29 is_valid_ = layer_;
30 auto metal_context = CastToMetalContext(GetContext());
31 auto darwin_context = metal_context->GetDarwinContext().get();
32 command_queue_ = darwin_context.commandQueue;
33 device_ = darwin_context.device;
34}
35
36// |IOSSurface|
38
39// |IOSSurface|
41 return is_valid_;
42}
43
44// |IOSSurface|
46 // Nothing to do.
47}
48
49// |IOSSurface|
50std::unique_ptr<Surface> IOSSurfaceMetalSkia::CreateGPUSurface(GrDirectContext* context) {
51 FML_DCHECK(context);
52 return std::make_unique<GPUSurfaceMetalSkia>(this, // delegate
53 sk_ref_sp(context), // context
54 GetContext()->GetMsaaSampleCount() // sample count
55 );
56}
57
58// |GPUSurfaceMetalDelegate|
60 CAMetalLayer* layer = layer_.get();
61 layer.device = device_;
62
63 layer.pixelFormat = MTLPixelFormatBGRA8Unorm;
64 // Flutter needs to read from the color attachment in cases where there are effects such as
65 // backdrop filters. Flutter plugins that create platform views may also read from the layer.
66 layer.framebufferOnly = NO;
67
68 const auto drawable_size = CGSizeMake(frame_info.width(), frame_info.height());
69 if (!CGSizeEqualToSize(drawable_size, layer.drawableSize)) {
70 layer.drawableSize = drawable_size;
71 }
72
73 // When there are platform views in the scene, the drawable needs to be presented in the same
74 // transaction as the one created for platform views. When the drawable are being presented from
75 // the raster thread, there is no such transaction.
76 layer.presentsWithTransaction = [[NSThread currentThread] isMainThread];
77
78 return (__bridge GPUCAMetalLayerHandle)layer;
79}
80
81// |GPUSurfaceMetalDelegate|
83 if (drawable == nullptr) {
84 FML_DLOG(ERROR) << "Could not acquire next Metal drawable from the SkSurface.";
85 return false;
86 }
87
88 id<MTLCommandBuffer> command_buffer = [command_queue_ commandBuffer];
89
90 id<CAMetalDrawable> metal_drawable = (__bridge id<CAMetalDrawable>)drawable;
91 if ([metal_drawable conformsToProtocol:@protocol(FlutterMetalDrawable)]) {
92 [(id<FlutterMetalDrawable>)metal_drawable flutterPrepareForPresent:command_buffer];
93 }
94
95 [command_buffer commit];
96 [command_buffer waitUntilScheduled];
97
98 [metal_drawable present];
99 return true;
100}
101
102// |GPUSurfaceMetalDelegate|
104 FML_CHECK(false) << "render to texture not supported on ios";
105 return {.texture_id = -1, .texture = nullptr};
106}
107
108// |GPUSurfaceMetalDelegate|
110 FML_CHECK(false) << "render to texture not supported on ios";
111 return false;
112}
113
114// |GPUSurfaceMetalDelegate|
116 return false;
117}
118
119} // namespace flutter
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"...
std::unique_ptr< Surface > CreateGPUSurface(GrDirectContext *gr_context) override
bool AllowsDrawingWhenGpuDisabled() const override
Whether to allow drawing to the surface when the GPU is disabled.
IOSSurfaceMetalSkia(const fml::scoped_nsobject< CAMetalLayer > &layer, std::shared_ptr< IOSContext > context)
GPUCAMetalLayerHandle GetCAMetalLayer(const SkISize &frame_info) const override __attribute__((cf_audited_transfer))
Returns the handle to the CAMetalLayer to render to. This is only called when the specified render ta...
bool PresentTexture(GPUMTLTextureInfo texture) const override __attribute__((cf_audited_transfer))
Presents the texture with texture_id to the "screen". texture_id corresponds to a texture that has be...
bool PresentDrawable(GrMTLHandle drawable) const override __attribute__((cf_audited_transfer))
Presents the drawable to the "screen". The drawable is obtained from the CAMetalLayer that given by G...
GPUMTLTextureInfo GetMTLTexture(const SkISize &frame_info) const override __attribute__((cf_audited_transfer))
Returns the handle to the MTLTexture to render to. This is only called when the specified render targ...
std::shared_ptr< IOSContext > GetContext() const
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
constexpr int32_t width() const
Definition SkSize.h:36
constexpr int32_t height() const
Definition SkSize.h:37
#define ERROR(message)