Flutter Engine
The Flutter Engine
FlutterDarwinContextMetalSkia.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/graphics/FlutterDarwinContextMetalSkia.h"
8
9#include "flutter/common/graphics/persistent_cache.h"
10#include "flutter/fml/logging.h"
11#include "flutter/shell/common/context_options.h"
12#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h"
16
18
20
21- (instancetype)initWithDefaultMTLDevice {
22 id<MTLDevice> device = MTLCreateSystemDefaultDevice();
23 return [self initWithMTLDevice:device commandQueue:[device newCommandQueue]];
24}
25
26- (instancetype)initWithMTLDevice:(id<MTLDevice>)device
27 commandQueue:(id<MTLCommandQueue>)commandQueue {
28 self = [super init];
29 if (self != nil) {
30 _device = device;
31
32 if (!_device) {
33 FML_DLOG(ERROR) << "Could not acquire Metal device.";
34 return nil;
35 }
36
37 _commandQueue = commandQueue;
38
39 if (!_commandQueue) {
40 FML_DLOG(ERROR) << "Could not create Metal command queue.";
41 return nil;
42 }
43
44 [_commandQueue setLabel:@"Flutter Main Queue"];
45
46 CVReturn cvReturn = CVMetalTextureCacheCreate(kCFAllocatorDefault, // allocator
47 nil, // cache attributes (nil default)
48 _device, // metal device
49 nil, // texture attributes (nil default)
50 &_textureCache // [out] cache
51 );
52 if (cvReturn != kCVReturnSuccess) {
53 FML_DLOG(ERROR) << "Could not create Metal texture cache.";
54 return nil;
55 }
56
57 // The devices are in the same "sharegroup" because they share the same device and command
58 // queues for now. When the resource context gets its own transfer queue, this will have to be
59 // refactored.
60 _mainContext = [self createGrContext];
61 _resourceContext = [self createGrContext];
62
63 if (!_mainContext || !_resourceContext) {
64 FML_DLOG(ERROR) << "Could not create Skia Metal contexts.";
65 return nil;
66 }
67
68 // Only log this message on iOS where the default is Impeller. On macOS
69 // desktop, Skia is still the default and this log is unecessary.
70#if defined(FML_OS_IOS) || defined(FML_OS_IOS_SIM)
71 FML_LOG(IMPORTANT) << "Using the Skia rendering backend (Metal).";
72#endif // defined(FML_OS_IOS) || defined(FML_OS_IOS_SIM)
73
74 _resourceContext->setResourceCacheLimit(0u);
75 }
76 return self;
77}
78
80 const auto contextOptions =
82 id<MTLDevice> device = _device;
83 id<MTLCommandQueue> commandQueue = _commandQueue;
85}
86
87+ (sk_sp<GrDirectContext>)createGrContext:(id<MTLDevice>)device
88 commandQueue:(id<MTLCommandQueue>)commandQueue {
89 const auto contextOptions =
91 GrMtlBackendContext backendContext = {};
92 // Skia expect arguments to `MakeMetal` transfer ownership of the reference in for release later
93 // when the GrDirectContext is collected.
94 backendContext.fDevice.reset((__bridge_retained void*)device);
95 backendContext.fQueue.reset((__bridge_retained void*)commandQueue);
96 return GrDirectContexts::MakeMetal(backendContext, contextOptions);
97}
98
99- (void)dealloc {
100 if (_textureCache) {
101 CFRelease(_textureCache);
102 }
103}
104
106 createExternalTextureWithIdentifier:(int64_t)textureID
107 texture:(NSObject<FlutterTexture>*)texture {
108 return [[FlutterDarwinExternalTextureMetal alloc] initWithTextureCache:_textureCache
109 textureID:textureID
110 texture:texture
111 enableImpeller:NO];
112}
113
114@end
115
116#endif // !SLIMPELLER
#define FML_DLOG(severity)
Definition: logging.h:102
#define FML_LOG(severity)
Definition: logging.h:82
instancetype initWithMTLDevice:commandQueue:(id< MTLDevice > device,[commandQueue] id< MTLCommandQueue > commandQueue)
sk_sp< GrDirectContext > createGrContext:commandQueue:(id< MTLDevice > device,[commandQueue] id< MTLCommandQueue > commandQueue)
SK_API sk_sp< GrDirectContext > MakeMetal(const GrMtlBackendContext &, const GrContextOptions &)
GrContextOptions MakeDefaultContextOptions(ContextType type, std::optional< GrBackendApi > api)
Initializes GrContextOptions with values suitable for Flutter. The options can be further tweaked bef...
@ kRender
The context is used to render to a texture or renderbuffer.
sk_cfp< GrMTLHandle > fDevice
sk_cfp< GrMTLHandle > fQueue
#define ERROR(message)
Definition: elf_loader.cc:260