Flutter Engine
The Flutter Engine
FlutterDarwinContextMetalImpeller.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/FlutterDarwinContextMetalImpeller.h"
6
7#include "flutter/common/graphics/persistent_cache.h"
8#include "flutter/fml/logging.h"
9#include "flutter/impeller/renderer/backend/metal/context_mtl.h"
10#include "flutter/shell/common/context_options.h"
11#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h"
12#include "impeller/entity/mtl/entity_shaders.h"
13#include "impeller/entity/mtl/framebuffer_blend_shaders.h"
14#include "impeller/entity/mtl/modern_shaders.h"
15
16#if IMPELLER_ENABLE_3D
17#include "impeller/scene/shaders/mtl/scene_shaders.h" // nogncheck
18#endif // IMPELLER_ENABLE_3D
19
21
22static std::shared_ptr<impeller::ContextMTL> CreateImpellerContext(
23 const std::shared_ptr<const fml::SyncSwitch>& is_gpu_disabled_sync_switch) {
24 std::vector<std::shared_ptr<fml::Mapping>> shader_mappings = {
25 std::make_shared<fml::NonOwnedMapping>(impeller_entity_shaders_data,
26 impeller_entity_shaders_length),
27#if IMPELLER_ENABLE_3D
28 std::make_shared<fml::NonOwnedMapping>(impeller_scene_shaders_data,
29 impeller_scene_shaders_length),
30#endif // IMPELLER_ENABLE_3D
31 std::make_shared<fml::NonOwnedMapping>(impeller_modern_shaders_data,
32 impeller_modern_shaders_length),
33 std::make_shared<fml::NonOwnedMapping>(impeller_framebuffer_blend_shaders_data,
34 impeller_framebuffer_blend_shaders_length),
35 };
36 auto context = impeller::ContextMTL::Create(shader_mappings, is_gpu_disabled_sync_switch,
37 "Impeller Library");
38 if (!context) {
39 FML_LOG(ERROR) << "Could not create Metal Impeller Context.";
40 return nullptr;
41 }
42
43 return context;
44}
45
47
48- (instancetype)init:(const std::shared_ptr<const fml::SyncSwitch>&)is_gpu_disabled_sync_switch {
49 self = [super init];
50 if (self != nil) {
51 _context = CreateImpellerContext(is_gpu_disabled_sync_switch);
52 id<MTLDevice> device = _context->GetMTLDevice();
53 if (!device) {
54 FML_DLOG(ERROR) << "Could not acquire Metal device.";
55 return nil;
56 }
57
58 CVMetalTextureCacheRef textureCache;
59 CVReturn cvReturn = CVMetalTextureCacheCreate(kCFAllocatorDefault, // allocator
60 nil, // cache attributes (nil default)
61 device, // metal device
62 nil, // texture attributes (nil default)
63 &textureCache // [out] cache
64 );
65
66 if (cvReturn != kCVReturnSuccess) {
67 FML_DLOG(ERROR) << "Could not create Metal texture cache.";
68 return nil;
69 }
70 _textureCache.Reset(textureCache);
71 }
72 return self;
73}
74
76 createExternalTextureWithIdentifier:(int64_t)textureID
77 texture:(NSObject<FlutterTexture>*)texture {
78 return [[FlutterDarwinExternalTextureMetal alloc] initWithTextureCache:_textureCache
79 textureID:textureID
80 texture:texture
81 enableImpeller:YES];
82}
83
84@end
static FLUTTER_ASSERT_ARC std::shared_ptr< impeller::ContextMTL > CreateImpellerContext(const std::shared_ptr< const fml::SyncSwitch > &is_gpu_disabled_sync_switch)
static std::shared_ptr< ContextMTL > Create(const std::vector< std::string > &shader_library_paths, std::shared_ptr< const fml::SyncSwitch > is_gpu_disabled_sync_switch)
Definition: context_mtl.mm:221
VkDevice device
Definition: main.cc:53
#define FML_DLOG(severity)
Definition: logging.h:102
#define FML_LOG(severity)
Definition: logging.h:82
fml::CFRef< CVMetalTextureCacheRef > textureCache
static bool init()
Definition: ascii_trie.cc:9
Definition: ref_ptr.h:256
#define ERROR(message)
Definition: elf_loader.cc:260