Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkiaMetalContext.mm
Go to the documentation of this file.
1// Copyright 2020 Google LLC.
2// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3
5
11
12#import <Metal/Metal.h>
13#import <MetalKit/MetalKit.h>
14#import <UIKit/UIKit.h>
15
16// A UIView that uses a Metal-backed SkSurface to draw.
17@interface SkiaMtkView : MTKView
18 @property (strong) SkiaViewController* controller;
19
20 // Override of the MTKView interface. Uses Skia+Metal to draw.
21 - (void)drawRect:(CGRect)rect;
22
23 // Required initializer.
24 - (instancetype)initWithFrame:(CGRect)frameRect
25 device:(id<MTLDevice>)device
26 queue:(id<MTLCommandQueue>)queue
27 grDevice:(GrDirectContext*)dContext;
28@end
29
30@implementation SkiaMtkView {
31 id<MTLCommandQueue> fQueue;
33}
34
35- (instancetype)initWithFrame:(CGRect)frameRect
36 device:(id<MTLDevice>)mtlDevice
37 queue:(id<MTLCommandQueue>)queue
38 grDevice:(GrDirectContext*)dContext {
39 self = [super initWithFrame:frameRect device:mtlDevice];
40 fQueue = queue;
41 fDContext = dContext;
43 return self;
44}
45
46- (void)drawRect:(CGRect)rect {
47 [super drawRect:rect];
48 // TODO(halcanary): Use the rect and the InvalidationController to speed up rendering.
49 SkiaViewController* viewController = [self controller];
50 if (!viewController || ![[self currentDrawable] texture] || !fDContext) {
51 return;
52 }
53 CGSize size = [self drawableSize];
55 if (!surface) {
56 NSLog(@"error: no sksurface");
57 return;
58 }
59 [viewController draw:rect toCanvas:surface->getCanvas() atSize:size];
61 surface = nullptr;
62
63 id<MTLCommandBuffer> commandBuffer = [fQueue commandBuffer];
64 [commandBuffer presentDrawable:[self currentDrawable]];
65 [commandBuffer commit];
66
67 bool paused = [viewController isPaused];
68 [self setEnableSetNeedsDisplay:paused];
69 [self setPaused:paused];
70}
71@end
72
74 @property (strong) id<MTLDevice> metalDevice;
75 @property (strong) id<MTLCommandQueue> metalQueue;
76 - (instancetype) init;
77 - (UIView*) makeViewWithController:(SkiaViewController*)vc withFrame:(CGRect)frame;
78 - (SkiaViewController*) getViewController:(UIView*)view;
79@end
80
81@implementation SkiaMetalContext {
83}
84
85- (instancetype) init {
86 self = [super init];
87 [self setMetalDevice:MTLCreateSystemDefaultDevice()];
88 if(![self metalDevice]) {
89 NSLog(@"Metal is not supported on this device");
90 return nil;
91 }
92 [self setMetalQueue:[[self metalDevice] newCommandQueue]];
93 GrMtlBackendContext backendContext = {};
94 backendContext.fDevice.reset((__bridge void*)[self metalDevice]);
95 backendContext.fQueue.reset((__bridge void*)[self metalQueue]);
97
98 if (!fDContext) {
99 NSLog(@"GrDirectContexts::MakeMetal failed");
100 return nil;
101 }
102 return self;
103}
104
105- (UIView*) makeViewWithController:(SkiaViewController*)vc withFrame:(CGRect)frame {
106 SkiaMtkView* skiaView = [[SkiaMtkView alloc] initWithFrame:frame
107 device:[self metalDevice]
108 queue:[self metalQueue]
109 grDevice:fDContext.get()];
110 [skiaView setPreferredFramesPerSecond:30];
111 [skiaView setController:vc];
112 return skiaView;
113}
114
115- (SkiaViewController*) getViewController:(UIView*)view {
116 return [view isKindOfClass:[SkiaMtkView class]] ? [(SkiaMtkView*)view controller] : nil;
117}
118@end
119
void SkMtkViewConfigForSkia(MTKView *)
sk_sp< SkSurface > SkMtkViewToSurface(MTKView *, GrRecordingContext *)
GrDirectContext * fDContext
SkiaContext * MakeSkiaMetalContext()
void flushAndSubmit(GrSyncCpu sync=GrSyncCpu::kNo)
VkDevice device
Definition main.cc:53
VkQueue queue
Definition main.cc:55
VkSurfaceKHR surface
Definition main.cc:49
id< MTLCommandQueue > metalQueue
id< MTLDevice > metalDevice
SkiaViewController * controller
void draw:toCanvas:atSize:(CGRect rect,[toCanvas] SkCanvas *canvas,[atSize] CGSize size)
FlutterViewController * viewController
instancetype initWithFrame
FlTexture * texture
SK_API sk_sp< GrDirectContext > MakeMetal(const GrMtlBackendContext &, const GrContextOptions &)
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition switches.h:259
sk_cfp< GrMTLHandle > fDevice
sk_cfp< GrMTLHandle > fQueue