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