Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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#import "flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetalSkia.h"
6
7#include "flutter/common/graphics/persistent_cache.h"
8#include "flutter/fml/logging.h"
9#include "flutter/shell/common/context_options.h"
10#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h"
14
16
18
19- (instancetype)initWithDefaultMTLDevice {
20 id<MTLDevice> device = MTLCreateSystemDefaultDevice();
21 return [self initWithMTLDevice:device commandQueue:[device newCommandQueue]];
22}
23
24- (instancetype)initWithMTLDevice:(id<MTLDevice>)device
25 commandQueue:(id<MTLCommandQueue>)commandQueue {
26 self = [super init];
27 if (self != nil) {
28 _device = device;
29
30 if (!_device) {
31 FML_DLOG(ERROR) << "Could not acquire Metal device.";
32 return nil;
33 }
34
35 _commandQueue = commandQueue;
36
37 if (!_commandQueue) {
38 FML_DLOG(ERROR) << "Could not create Metal command queue.";
39 return nil;
40 }
41
42 [_commandQueue setLabel:@"Flutter Main Queue"];
43
44 CVReturn cvReturn = CVMetalTextureCacheCreate(kCFAllocatorDefault, // allocator
45 nil, // cache attributes (nil default)
46 _device, // metal device
47 nil, // texture attributes (nil default)
48 &_textureCache // [out] cache
49 );
50 if (cvReturn != kCVReturnSuccess) {
51 FML_DLOG(ERROR) << "Could not create Metal texture cache.";
52 return nil;
53 }
54
55 // The devices are in the same "sharegroup" because they share the same device and command
56 // queues for now. When the resource context gets its own transfer queue, this will have to be
57 // refactored.
58 _mainContext = [self createGrContext];
59 _resourceContext = [self createGrContext];
60
61 if (!_mainContext || !_resourceContext) {
62 FML_DLOG(ERROR) << "Could not create Skia Metal contexts.";
63 return nil;
64 }
65
66 FML_LOG(IMPORTANT) << "Using the Skia rendering backend (Metal).";
67
68 _resourceContext->setResourceCacheLimit(0u);
69 }
70 return self;
71}
72
74 const auto contextOptions =
75 flutter::MakeDefaultContextOptions(flutter::ContextType::kRender, GrBackendApi::kMetal);
76 id<MTLDevice> device = _device;
77 id<MTLCommandQueue> commandQueue = _commandQueue;
79}
80
81+ (sk_sp<GrDirectContext>)createGrContext:(id<MTLDevice>)device
82 commandQueue:(id<MTLCommandQueue>)commandQueue {
83 const auto contextOptions =
84 flutter::MakeDefaultContextOptions(flutter::ContextType::kRender, GrBackendApi::kMetal);
85 GrMtlBackendContext backendContext = {};
86 // Skia expect arguments to `MakeMetal` transfer ownership of the reference in for release later
87 // when the GrDirectContext is collected.
88 backendContext.fDevice.reset((__bridge_retained void*)device);
89 backendContext.fQueue.reset((__bridge_retained void*)commandQueue);
90 return GrDirectContexts::MakeMetal(backendContext, contextOptions);
91}
92
93- (void)dealloc {
94 if (_textureCache) {
95 CFRelease(_textureCache);
96 }
97}
98
100 createExternalTextureWithIdentifier:(int64_t)textureID
101 texture:(NSObject<FlutterTexture>*)texture {
102 return [[FlutterDarwinExternalTextureMetal alloc] initWithTextureCache:_textureCache
103 textureID:textureID
104 texture:texture
105 enableImpeller:NO];
106}
107
108@end
#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...
sk_cfp< GrMTLHandle > fDevice
sk_cfp< GrMTLHandle > fQueue
#define ERROR(message)