| Flutter macOS Embedder |

FlutterRenderer.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/macos/framework/Source/FlutterRenderer.h"
6
7 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine_Internal.h"
8 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterExternalTexture.h"
9 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterViewController_Internal.h"
10 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterViewEngineProvider.h"
11 #include "flutter/shell/platform/embedder/embedder.h"
12
13 #pragma mark - Static callbacks that require the engine.
14
15 static FlutterMetalTexture OnGetNextDrawable(void* user_data, const FlutterFrameInfo* frameInfo) {
16 NSCAssert(NO, @"The renderer config should not be used to get the next drawable.");
17 return FlutterMetalTexture{};
18 }
19
20 static bool OnPresentDrawable(void* user_data, const FlutterMetalTexture* texture) {
21 NSCAssert(NO, @"The renderer config should not be used to present drawable.");
22 return false;
23 }
24
25 static bool OnAcquireExternalTexture(void* user_data,
26 int64_t textureIdentifier,
27 size_t width,
28 size_t height,
29 FlutterMetalExternalTexture* metalTexture) {
30 FlutterEngine* engine = (__bridge FlutterEngine*)user_data;
31 return [engine.renderer populateTextureWithIdentifier:textureIdentifier
32 metalTexture:metalTexture];
33 }
34
35 #pragma mark - FlutterRenderer implementation
36
37 @implementation FlutterRenderer {
38 FlutterDarwinContextMetalSkia* _darwinMetalContext;
39 }
40
41 namespace {
42
43 // Attempts to find the integrated GPU backed metal device.
44 //
45 // See also: https://developer.apple.com/documentation/metal/multi-gpu-systems?language=objc
46 id<MTLDevice> SelectMetalDevice() {
47 NSArray<id<MTLDevice>>* devices = MTLCopyAllDevices();
48 for (id<MTLDevice> device in devices) {
49 if (device.hasUnifiedMemory) {
50 return device;
51 }
52 }
53 return MTLCreateSystemDefaultDevice();
54 }
55 } // namespace
56
57 - (instancetype)initWithFlutterEngine:(nonnull FlutterEngine*)flutterEngine {
58 self = [super initWithDelegate:self engine:flutterEngine];
59 if (self) {
60 _device = SelectMetalDevice();
61 if (!_device) {
62 NSLog(@"Could not acquire Metal device.");
63 return nil;
64 }
65
66 _commandQueue = [_device newCommandQueue];
67 if (!_commandQueue) {
68 NSLog(@"Could not create Metal command queue.");
69 return nil;
70 }
71
72 _darwinMetalContext = [[FlutterDarwinContextMetalSkia alloc] initWithMTLDevice:_device
73 commandQueue:_commandQueue];
74 }
75 return self;
76 }
77
78 - (FlutterRendererConfig)createRendererConfig {
79 FlutterRendererConfig config = {
80 .type = FlutterRendererType::kMetal,
81 .metal = {
82 .struct_size = sizeof(FlutterMetalRendererConfig),
83 .device = (__bridge FlutterMetalDeviceHandle)_device,
84 .present_command_queue = (__bridge FlutterMetalCommandQueueHandle)_commandQueue,
85 .get_next_drawable_callback =
86 reinterpret_cast<FlutterMetalTextureCallback>(OnGetNextDrawable),
87 .present_drawable_callback =
88 reinterpret_cast<FlutterMetalPresentCallback>(OnPresentDrawable),
89 .external_texture_frame_callback =
90 reinterpret_cast<FlutterMetalTextureFrameCallback>(OnAcquireExternalTexture),
91 }};
92 return config;
93 }
94
95 #pragma mark - Embedder callback implementations.
96
97 - (BOOL)populateTextureWithIdentifier:(int64_t)textureID
98 metalTexture:(FlutterMetalExternalTexture*)textureOut {
99 FlutterExternalTexture* texture = [self getTextureWithID:textureID];
100 return [texture populateTexture:textureOut];
101 }
102
103 #pragma mark - FlutterTextureRegistrar methods.
104
105 - (FlutterExternalTexture*)onRegisterTexture:(id<FlutterTexture>)texture {
106 return [[FlutterExternalTexture alloc] initWithFlutterTexture:texture
107 darwinMetalContext:_darwinMetalContext];
108 }
109
110 @end
FlutterEngine_Internal.h
FlutterExternalTexture.h
FlutterRenderer.h
OnAcquireExternalTexture
static bool OnAcquireExternalTexture(void *user_data, int64_t textureIdentifier, size_t width, size_t height, FlutterMetalExternalTexture *metalTexture)
Definition: FlutterRenderer.mm:25
OnGetNextDrawable
static FlutterMetalTexture OnGetNextDrawable(void *user_data, const FlutterFrameInfo *frameInfo)
Definition: FlutterRenderer.mm:15
OnPresentDrawable
static bool OnPresentDrawable(void *user_data, const FlutterMetalTexture *texture)
Definition: FlutterRenderer.mm:20
FlutterViewController_Internal.h
FlutterViewEngineProvider.h
FlutterEngine
Definition: FlutterEngine.h:32
FlutterExternalTexture
Definition: FlutterExternalTexture.h:19
-[FlutterExternalTexture populateTexture:]
BOOL populateTexture:(nonnull FlutterMetalExternalTexture *metalTexture)
FlutterRenderer
Definition: FlutterRenderer.h:19
FlutterTexture-p
Definition: FlutterTexture.h:21
user_data
void * user_data
Definition: texture_registrar_unittests.cc:27