24- (instancetype)initWithDefaultMTLDevice {
25 id<MTLDevice>
device = MTLCreateSystemDefaultDevice();
26 return [
self initWithMTLDevice:device commandQueue:[device newCommandQueue]];
29- (instancetype)initWithMTLDevice:(
id<MTLDevice>)device
30 commandQueue:(
id<MTLCommandQueue>)commandQueue {
36 FML_DLOG(ERROR) <<
"Could not acquire Metal device.";
40 _commandQueue = commandQueue;
43 FML_DLOG(ERROR) <<
"Could not create Metal command queue.";
47 [_commandQueue setLabel:@"Flutter Main Queue"];
50 CVMetalTextureCacheRef cache =
nullptr;
51 CVReturn cvReturn = CVMetalTextureCacheCreate(kCFAllocatorDefault,
57 _textureCache.Reset(cache);
58 if (cvReturn != kCVReturnSuccess) {
59 FML_DLOG(ERROR) <<
"Could not create Metal texture cache.";
67 _mainContext = [
self createGrContext];
68 _resourceContext = [
self createGrContext];
70 if (!_mainContext || !_resourceContext) {
71 FML_DLOG(ERROR) <<
"Could not create Skia Metal contexts.";
77#if defined(FML_OS_IOS) || defined(FML_OS_IOS_SIM)
78 FML_LOG(IMPORTANT) <<
"Using the Skia rendering backend (Metal).";
81 _resourceContext->setResourceCacheLimit(0u);
86- (sk_sp<GrDirectContext>)createGrContext {
87 id<MTLDevice>
device = _device;
88 id<MTLCommandQueue> commandQueue = _commandQueue;
92+ (sk_sp<GrDirectContext>)createGrContext:(
id<MTLDevice>)device
93 commandQueue:(
id<MTLCommandQueue>)commandQueue {
94 const auto contextOptions =
96 GrMtlBackendContext backendContext = {};
99 backendContext.fDevice.retain((__bridge
void*)
device);
100 backendContext.fQueue.retain((__bridge
void*)commandQueue);
101 return GrDirectContexts::MakeMetal(backendContext, contextOptions);
105 createExternalTextureWithIdentifier:(int64_t)textureID