|
6 | 6 |
|
7 | 7 | #include <QuartzCore/CAMetalLayer.h> |
8 | 8 |
|
| 9 | +#include "flutter/fml/trace_event.h" |
9 | 10 | #include "third_party/skia/include/core/SkSurface.h" |
10 | 11 | #include "third_party/skia/include/gpu/GrBackendSurface.h" |
11 | 12 | #include "third_party/skia/include/ports/SkCFObject.h" |
|
15 | 16 | namespace flutter { |
16 | 17 |
|
17 | 18 | GPUSurfaceMetal::GPUSurfaceMetal(GPUSurfaceDelegate* delegate, |
18 | | - fml::scoped_nsobject<CAMetalLayer> layer) |
19 | | - : delegate_(delegate), layer_(std::move(layer)) { |
20 | | - if (!layer_) { |
21 | | - FML_LOG(ERROR) << "Could not create metal surface because of invalid layer."; |
22 | | - return; |
23 | | - } |
24 | | - |
25 | | - layer.get().pixelFormat = MTLPixelFormatBGRA8Unorm; |
| 19 | + fml::scoped_nsobject<CAMetalLayer> layer, |
| 20 | + sk_sp<GrContext> context, |
| 21 | + fml::scoped_nsprotocol<id<MTLCommandQueue>> command_queue) |
| 22 | + : delegate_(delegate), |
| 23 | + layer_(std::move(layer)), |
| 24 | + context_(std::move(context)), |
| 25 | + command_queue_(std::move(command_queue)) { |
| 26 | + layer_.get().pixelFormat = MTLPixelFormatBGRA8Unorm; |
26 | 27 | // Flutter needs to read from the color attachment in cases where there are effects such as |
27 | 28 | // backdrop filters. |
28 | | - layer.get().framebufferOnly = NO; |
29 | | - |
30 | | - auto metal_device = fml::scoped_nsprotocol<id<MTLDevice>>([layer_.get().device retain]); |
31 | | - auto metal_queue = fml::scoped_nsprotocol<id<MTLCommandQueue>>([metal_device newCommandQueue]); |
32 | | - |
33 | | - if (!metal_device || !metal_queue) { |
34 | | - FML_LOG(ERROR) << "Could not create metal device or queue."; |
35 | | - return; |
36 | | - } |
37 | | - |
38 | | - command_queue_ = metal_queue; |
39 | | - |
40 | | - // The context creation routine accepts arguments using transfer semantics. |
41 | | - auto context = GrContext::MakeMetal(metal_device.release(), metal_queue.release()); |
42 | | - if (!context) { |
43 | | - FML_LOG(ERROR) << "Could not create Skia metal context."; |
44 | | - return; |
45 | | - } |
46 | | - |
47 | | - context_ = context; |
48 | | -} |
49 | | - |
50 | | -GPUSurfaceMetal::GPUSurfaceMetal(GPUSurfaceDelegate* delegate, |
51 | | - sk_sp<GrContext> gr_context, |
52 | | - fml::scoped_nsobject<CAMetalLayer> layer) |
53 | | - : delegate_(delegate), layer_(std::move(layer)), context_(gr_context) { |
54 | | - if (!layer_) { |
55 | | - FML_LOG(ERROR) << "Could not create metal surface because of invalid layer."; |
56 | | - return; |
57 | | - } |
58 | | - if (!context_) { |
59 | | - FML_LOG(ERROR) << "Could not create metal surface because of invalid Skia metal context."; |
60 | | - return; |
61 | | - } |
62 | | - |
63 | | - layer.get().pixelFormat = MTLPixelFormatBGRA8Unorm; |
64 | | - |
65 | | - auto metal_device = fml::scoped_nsprotocol<id<MTLDevice>>([layer_.get().device retain]); |
66 | | - auto metal_queue = fml::scoped_nsprotocol<id<MTLCommandQueue>>([metal_device newCommandQueue]); |
67 | | - |
68 | | - if (!metal_device || !metal_queue) { |
69 | | - FML_LOG(ERROR) << "Could not create metal device or queue."; |
70 | | - return; |
71 | | - } |
72 | | - |
73 | | - command_queue_ = metal_queue; |
| 29 | + layer_.get().framebufferOnly = NO; |
74 | 30 | } |
75 | 31 |
|
76 | 32 | GPUSurfaceMetal::~GPUSurfaceMetal() { |
|
95 | 51 | } |
96 | 52 |
|
97 | 53 | const auto bounds = layer_.get().bounds.size; |
98 | | - if (bounds.width <= 0.0 || bounds.height <= 0.0) { |
| 54 | + const auto scale = layer_.get().contentsScale; |
| 55 | + if (bounds.width <= 0.0 || bounds.height <= 0.0 || scale <= 0.0) { |
99 | 56 | FML_LOG(ERROR) << "Metal layer bounds were invalid."; |
100 | 57 | return nullptr; |
101 | 58 | } |
102 | 59 |
|
| 60 | + const auto scaled_bounds = CGSizeMake(bounds.width * scale, bounds.height * scale); |
| 61 | + |
103 | 62 | ReleaseUnusedDrawableIfNecessary(); |
104 | 63 |
|
| 64 | + if (!CGSizeEqualToSize(scaled_bounds, layer_.get().drawableSize)) { |
| 65 | + layer_.get().drawableSize = scaled_bounds; |
| 66 | + } |
| 67 | + |
105 | 68 | auto surface = SkSurface::MakeFromCAMetalLayer(context_.get(), // context |
106 | 69 | layer_.get(), // layer |
107 | 70 | kTopLeft_GrSurfaceOrigin, // origin |
|
118 | 81 | } |
119 | 82 |
|
120 | 83 | auto submit_callback = [this](const SurfaceFrame& surface_frame, SkCanvas* canvas) -> bool { |
| 84 | + TRACE_EVENT0("flutter", "GPUSurfaceMetal::Submit"); |
121 | 85 | canvas->flush(); |
122 | 86 |
|
123 | 87 | if (next_drawable_ == nullptr) { |
|
159 | 123 | SkMatrix GPUSurfaceMetal::GetRootTransformation() const { |
160 | 124 | // This backend does not currently support root surface transformations. Just |
161 | 125 | // return identity. |
162 | | - SkMatrix matrix; |
163 | | - matrix.reset(); |
164 | | - return matrix; |
| 126 | + return {}; |
165 | 127 | } |
166 | 128 |
|
167 | 129 | // |Surface| |
|
0 commit comments