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 
6 
11 #include "flutter/shell/platform/embedder/embedder.h"
12 
13 #pragma mark - Static callbacks that require the engine.
14 
15 static FlutterMetalTexture OnGetNextDrawable(FlutterEngine* engine,
16  const FlutterFrameInfo* frameInfo) {
17  NSCAssert(NO, @"The renderer config should not be used to get the next drawable.");
18  return FlutterMetalTexture{};
19 }
20 
21 static bool OnPresentDrawable(FlutterEngine* engine, const FlutterMetalTexture* texture) {
22  NSCAssert(NO, @"The renderer config should not be used to present drawable.");
23  return false;
24 }
25 
27  int64_t textureIdentifier,
28  size_t width,
29  size_t height,
30  FlutterMetalExternalTexture* metalTexture) {
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 - (instancetype)initWithFlutterEngine:(nonnull FlutterEngine*)flutterEngine {
42  self = [super initWithDelegate:self engine:flutterEngine];
43  if (self) {
44  _device = MTLCreateSystemDefaultDevice();
45  if (!_device) {
46  NSLog(@"Could not acquire Metal device.");
47  return nil;
48  }
49 
50  _commandQueue = [_device newCommandQueue];
51  if (!_commandQueue) {
52  NSLog(@"Could not create Metal command queue.");
53  return nil;
54  }
55 
56  _darwinMetalContext = [[FlutterDarwinContextMetalSkia alloc] initWithMTLDevice:_device
57  commandQueue:_commandQueue];
58  }
59  return self;
60 }
61 
62 - (FlutterRendererConfig)createRendererConfig {
63  FlutterRendererConfig config = {
64  .type = FlutterRendererType::kMetal,
65  .metal.struct_size = sizeof(FlutterMetalRendererConfig),
66  .metal.device = (__bridge FlutterMetalDeviceHandle)_device,
67  .metal.present_command_queue = (__bridge FlutterMetalCommandQueueHandle)_commandQueue,
68  .metal.get_next_drawable_callback =
69  reinterpret_cast<FlutterMetalTextureCallback>(OnGetNextDrawable),
70  .metal.present_drawable_callback =
71  reinterpret_cast<FlutterMetalPresentCallback>(OnPresentDrawable),
72  .metal.external_texture_frame_callback =
73  reinterpret_cast<FlutterMetalTextureFrameCallback>(OnAcquireExternalTexture),
74  };
75  return config;
76 }
77 
78 #pragma mark - Embedder callback implementations.
79 
80 - (BOOL)populateTextureWithIdentifier:(int64_t)textureID
81  metalTexture:(FlutterMetalExternalTexture*)textureOut {
82  FlutterExternalTexture* texture = [self getTextureWithID:textureID];
83  return [texture populateTexture:textureOut];
84 }
85 
86 #pragma mark - FlutterTextureRegistrar methods.
87 
88 - (FlutterExternalTexture*)onRegisterTexture:(id<FlutterTexture>)texture {
89  return [[FlutterExternalTexture alloc] initWithFlutterTexture:texture
90  darwinMetalContext:_darwinMetalContext];
91 }
92 
93 @end
FlutterEngine
Definition: FlutterEngine.h:30
OnPresentDrawable
static bool OnPresentDrawable(FlutterEngine *engine, const FlutterMetalTexture *texture)
Definition: FlutterRenderer.mm:21
OnAcquireExternalTexture
static bool OnAcquireExternalTexture(FlutterEngine *engine, int64_t textureIdentifier, size_t width, size_t height, FlutterMetalExternalTexture *metalTexture)
Definition: FlutterRenderer.mm:26
FlutterEngine_Internal.h
FlutterViewEngineProvider.h
FlutterRenderer.h
FlutterExternalTexture.h
FlutterExternalTexture
Definition: FlutterExternalTexture.h:15
FlutterRenderer
Definition: FlutterRenderer.h:15
OnGetNextDrawable
static FlutterMetalTexture OnGetNextDrawable(FlutterEngine *engine, const FlutterFrameInfo *frameInfo)
Definition: FlutterRenderer.mm:15
FlutterViewController_Internal.h
FlutterTexture-p
Definition: FlutterTexture.h:21
-[FlutterExternalTexture populateTexture:]
BOOL populateTexture:(nonnull FlutterMetalExternalTexture *metalTexture)