Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 36b8aa2

Browse files
committed
[macOS] Isolate openGL rendering to FlutterOpenGLRenderer
This is in preparation for having an equivalent FlutterMetalRenderer
1 parent e502305 commit 36b8aa2

File tree

9 files changed

+275
-164
lines changed

9 files changed

+275
-164
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,8 @@ FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterExter
10541054
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.mm
10551055
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMouseCursorPlugin.h
10561056
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMouseCursorPlugin.mm
1057+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterOpenGLRenderer.h
1058+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterOpenGLRenderer.mm
10571059
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.h
10581060
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.mm
10591061
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterSurfaceManager.h

shell/platform/darwin/common/framework/Headers/FlutterTexture.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ FLUTTER_EXPORT
3939
/**
4040
* Registers a `FlutterTexture` for usage in Flutter and returns an id that can be used to reference
4141
* that texture when calling into Flutter with channels. Textures must be registered on the
42-
* platform thread.
42+
* platform thread. On success returns the pointer to the registered texture, else returns 0.
4343
*/
4444
- (int64_t)registerTexture:(NSObject<FlutterTexture>*)texture;
4545
/**

shell/platform/darwin/macos/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ source_set("flutter_framework_source") {
5454
"framework/Source/FlutterExternalTextureGL.mm",
5555
"framework/Source/FlutterMouseCursorPlugin.h",
5656
"framework/Source/FlutterMouseCursorPlugin.mm",
57+
"framework/Source/FlutterOpenGLRenderer.h",
58+
"framework/Source/FlutterOpenGLRenderer.mm",
5759
"framework/Source/FlutterResizeSynchronizer.h",
5860
"framework/Source/FlutterResizeSynchronizer.mm",
5961
"framework/Source/FlutterSurfaceManager.h",

shell/platform/darwin/macos/framework/Headers/FlutterEngine.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* Coordinates a single instance of execution of a Flutter engine.
2222
*/
2323
FLUTTER_EXPORT
24-
@interface FlutterEngine : NSObject <FlutterTextureRegistry, FlutterPluginRegistry>
24+
@interface FlutterEngine : NSObject <FlutterPluginRegistry>
2525

2626
/**
2727
* Initializes an engine with the given viewController.

shell/platform/darwin/macos/framework/Source/FlutterEngine.mm

Lines changed: 5 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject_Internal.h"
1212
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.h"
13+
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterOpenGLRenderer.h"
1314
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterViewController_Internal.h"
1415
#import "flutter/shell/platform/embedder/embedder.h"
1516

@@ -37,42 +38,11 @@ @interface FlutterEngine () <FlutterBinaryMessenger>
3738
*/
3839
- (void)sendUserLocales;
3940

40-
/**
41-
* Called by the engine to make the context the engine should draw into current.
42-
*/
43-
- (bool)engineCallbackOnMakeCurrent;
44-
45-
/**
46-
* Called by the engine to clear the context the engine should draw into.
47-
*/
48-
- (bool)engineCallbackOnClearCurrent;
49-
50-
/**
51-
* Called by the engine when the context's buffers should be swapped.
52-
*/
53-
- (bool)engineCallbackOnPresent;
54-
55-
/**
56-
* Called by the engine when framebuffer object ID is requested.
57-
*/
58-
- (uint32_t)engineCallbackOnFBO:(const FlutterFrameInfo*)info;
59-
60-
/**
61-
* Makes the resource context the current context.
62-
*/
63-
- (bool)engineCallbackOnMakeResourceCurrent;
64-
6541
/**
6642
* Handles a platform message from the engine.
6743
*/
6844
- (void)engineCallbackOnPlatformMessage:(const FlutterPlatformMessage*)message;
6945

70-
/**
71-
* Forwards texture copy request to the corresponding texture via |textureID|.
72-
*/
73-
- (BOOL)populateTextureWithIdentifier:(int64_t)textureID
74-
openGLTexture:(FlutterOpenGLTexture*)openGLTexture;
75-
7646
/**
7747
* Requests that the task be posted back the to the Flutter engine at the target time. The target
7848
* time is in the clock used by the Flutter engine.
@@ -119,7 +89,7 @@ - (instancetype)initWithPlugin:(NSString*)pluginKey flutterEngine:(FlutterEngine
11989
}
12090

12191
- (id<FlutterTextureRegistry>)textures {
122-
return _flutterEngine;
92+
return _flutterEngine.openGLRenderer;
12393
}
12494

12595
- (NSView*)view {
@@ -138,38 +108,10 @@ - (void)addMethodCallDelegate:(nonnull id<FlutterPlugin>)delegate
138108
// Callbacks provided to the engine. See the called methods for documentation.
139109
#pragma mark - Static methods provided to engine configuration
140110

141-
static bool OnMakeCurrent(FlutterEngine* engine) {
142-
return [engine engineCallbackOnMakeCurrent];
143-
}
144-
145-
static bool OnClearCurrent(FlutterEngine* engine) {
146-
return [engine engineCallbackOnClearCurrent];
147-
}
148-
149-
static bool OnPresent(FlutterEngine* engine) {
150-
return [engine engineCallbackOnPresent];
151-
}
152-
153-
static uint32_t OnFBO(FlutterEngine* engine, const FlutterFrameInfo* info) {
154-
return [engine engineCallbackOnFBO:info];
155-
}
156-
157-
static bool OnMakeResourceCurrent(FlutterEngine* engine) {
158-
return [engine engineCallbackOnMakeResourceCurrent];
159-
}
160-
161111
static void OnPlatformMessage(const FlutterPlatformMessage* message, FlutterEngine* engine) {
162112
[engine engineCallbackOnPlatformMessage:message];
163113
}
164114

165-
static bool OnAcquireExternalTexture(FlutterEngine* engine,
166-
int64_t texture_identifier,
167-
size_t width,
168-
size_t height,
169-
FlutterOpenGLTexture* open_gl_texture) {
170-
return [engine populateTextureWithIdentifier:texture_identifier openGLTexture:open_gl_texture];
171-
}
172-
173115
#pragma mark -
174116

175117
@implementation FlutterEngine {
@@ -179,22 +121,12 @@ @implementation FlutterEngine {
179121
// The project being run by this engine.
180122
FlutterDartProject* _project;
181123

182-
// The context provided to the Flutter engine for resource loading.
183-
NSOpenGLContext* _resourceContext;
184-
185-
// The context that is owned by the currently displayed FlutterView. This is stashed in the engine
186-
// so that the view doesn't need to be accessed from a background thread.
187-
NSOpenGLContext* _mainOpenGLContext;
188-
189124
// A mapping of channel names to the registered handlers for those channels.
190125
NSMutableDictionary<NSString*, FlutterBinaryMessageHandler>* _messageHandlers;
191126

192127
// Whether the engine can continue running after the view controller is removed.
193128
BOOL _allowHeadlessExecution;
194129

195-
// A mapping of textureID to internal FlutterExternalTextureGL adapter.
196-
NSMutableDictionary<NSNumber*, FlutterExternalTextureGL*>* _textures;
197-
198130
// Pointer to the Dart AOT snapshot and instruction data.
199131
_FlutterEngineAOTData* _aotData;
200132
}
@@ -211,10 +143,10 @@ - (instancetype)initWithName:(NSString*)labelPrefix
211143

212144
_project = project ?: [[FlutterDartProject alloc] init];
213145
_messageHandlers = [[NSMutableDictionary alloc] init];
214-
_textures = [[NSMutableDictionary alloc] init];
215146
_allowHeadlessExecution = allowHeadlessExecution;
216147
_embedderAPI.struct_size = sizeof(FlutterEngineProcTable);
217148
FlutterEngineGetProcAddresses(&_embedderAPI);
149+
_openGLRenderer = [[FlutterOpenGLRenderer alloc] initWithFlutterEngine:_engine];
218150

219151
NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter];
220152
[notificationCenter addObserver:self
@@ -242,17 +174,8 @@ - (BOOL)runWithEntrypoint:(NSString*)entrypoint {
242174
return NO;
243175
}
244176

245-
const FlutterRendererConfig rendererConfig = {
246-
.type = kOpenGL,
247-
.open_gl.struct_size = sizeof(FlutterOpenGLRendererConfig),
248-
.open_gl.make_current = (BoolCallback)OnMakeCurrent,
249-
.open_gl.clear_current = (BoolCallback)OnClearCurrent,
250-
.open_gl.present = (BoolCallback)OnPresent,
251-
.open_gl.fbo_with_frame_info_callback = (UIntFrameInfoCallback)OnFBO,
252-
.open_gl.fbo_reset_after_present = true,
253-
.open_gl.make_resource_current = (BoolCallback)OnMakeResourceCurrent,
254-
.open_gl.gl_external_texture_frame_callback = (TextureFrameCallback)OnAcquireExternalTexture,
255-
};
177+
[_openGLRenderer attachToFlutterView:_viewController.flutterView];
178+
const FlutterRendererConfig rendererConfig = [_openGLRenderer createRendererConfig];
256179

257180
// TODO(stuartmorgan): Move internal channel registration from FlutterViewController to here.
258181

@@ -351,15 +274,6 @@ - (void)loadAOTData:(NSString*)assetsDir {
351274
}
352275
}
353276

354-
- (void)setViewController:(FlutterViewController*)controller {
355-
_viewController = controller;
356-
_mainOpenGLContext = controller.flutterView.openGLContext;
357-
if (!controller && !_allowHeadlessExecution) {
358-
[self shutDownEngine];
359-
_resourceContext = nil;
360-
}
361-
}
362-
363277
- (id<FlutterBinaryMessenger>)binaryMessenger {
364278
// TODO(stuartmorgan): Switch to FlutterBinaryMessengerRelay to avoid plugins
365279
// keeping the engine alive.
@@ -372,17 +286,6 @@ - (BOOL)running {
372286
return _engine != nullptr;
373287
}
374288

375-
- (NSOpenGLContext*)resourceContext {
376-
if (!_resourceContext) {
377-
NSOpenGLPixelFormatAttribute attributes[] = {
378-
NSOpenGLPFAColorSize, 24, NSOpenGLPFAAlphaSize, 8, 0,
379-
};
380-
NSOpenGLPixelFormat* pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
381-
_resourceContext = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil];
382-
}
383-
return _resourceContext;
384-
}
385-
386289
- (void)updateDisplayConfig {
387290
if (!_engine) {
388291
return;
@@ -461,37 +364,6 @@ - (void)sendUserLocales {
461364
_embedderAPI.UpdateLocales(_engine, flutterLocaleList.data(), flutterLocaleList.size());
462365
}
463366

464-
- (bool)engineCallbackOnMakeCurrent {
465-
if (!_mainOpenGLContext) {
466-
return false;
467-
}
468-
[_mainOpenGLContext makeCurrentContext];
469-
return true;
470-
}
471-
472-
- (uint32_t)engineCallbackOnFBO:(const FlutterFrameInfo*)info {
473-
CGSize size = CGSizeMake(info->size.width, info->size.height);
474-
return [_viewController.flutterView frameBufferIDForSize:size];
475-
}
476-
477-
- (bool)engineCallbackOnClearCurrent {
478-
[NSOpenGLContext clearCurrentContext];
479-
return true;
480-
}
481-
482-
- (bool)engineCallbackOnPresent {
483-
if (!_mainOpenGLContext) {
484-
return false;
485-
}
486-
[self.viewController.flutterView present];
487-
return true;
488-
}
489-
490-
- (bool)engineCallbackOnMakeResourceCurrent {
491-
[self.resourceContext makeCurrentContext];
492-
return true;
493-
}
494-
495367
- (void)engineCallbackOnPlatformMessage:(const FlutterPlatformMessage*)message {
496368
NSData* messageData = [NSData dataWithBytesNoCopy:(void*)message->message
497369
length:message->message_size
@@ -618,31 +490,6 @@ - (void)cleanupConnection:(FlutterBinaryMessengerConnection)connection {
618490
return [[FlutterEngineRegistrar alloc] initWithPlugin:pluginName flutterEngine:self];
619491
}
620492

621-
#pragma mark - FlutterTextureRegistrar
622-
623-
- (BOOL)populateTextureWithIdentifier:(int64_t)textureID
624-
openGLTexture:(FlutterOpenGLTexture*)openGLTexture {
625-
return [_textures[@(textureID)] populateTexture:openGLTexture];
626-
}
627-
628-
- (int64_t)registerTexture:(id<FlutterTexture>)texture {
629-
FlutterExternalTextureGL* FlutterTexture =
630-
[[FlutterExternalTextureGL alloc] initWithFlutterTexture:texture];
631-
int64_t textureID = [FlutterTexture textureID];
632-
_embedderAPI.RegisterExternalTexture(_engine, textureID);
633-
_textures[@(textureID)] = FlutterTexture;
634-
return textureID;
635-
}
636-
637-
- (void)textureFrameAvailable:(int64_t)textureID {
638-
_embedderAPI.MarkExternalTextureFrameAvailable(_engine, textureID);
639-
}
640-
641-
- (void)unregisterTexture:(int64_t)textureID {
642-
_embedderAPI.UnregisterExternalTexture(_engine, textureID);
643-
[_textures removeObjectForKey:@(textureID)];
644-
}
645-
646493
#pragma mark - Task runner integration
647494

648495
- (void)postMainThreadTask:(FlutterTask)task targetTimeInNanoseconds:(uint64_t)targetTime {

shell/platform/darwin/macos/framework/Source/FlutterEngine_Internal.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#import <Cocoa/Cocoa.h>
88

9+
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterOpenGLRenderer.h"
910
#include "flutter/shell/platform/embedder/embedder.h"
1011

1112
@interface FlutterEngine ()
@@ -16,10 +17,10 @@
1617
@property(nonatomic, readonly) BOOL running;
1718

1819
/**
19-
* The resource context used by the engine for texture uploads. FlutterViews associated with this
20-
* engine should be created to share with this context.
20+
* Provides the renderer config needed to initialize the engine and also handles external texture
21+
* management.
2122
*/
22-
@property(nonatomic, readonly, nullable) NSOpenGLContext* resourceContext;
23+
@property(nonatomic, readonly, nonnull) FlutterOpenGLRenderer* openGLRenderer;
2324

2425
/**
2526
* Function pointers for interacting with the embedder.h API.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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 <Cocoa/Cocoa.h>
6+
7+
#import "flutter/shell/platform/darwin/macos/framework/Headers/FlutterEngine.h"
8+
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterView.h"
9+
#import "flutter/shell/platform/embedder/embedder.h"
10+
11+
NS_ASSUME_NONNULL_BEGIN
12+
13+
/**
14+
* Provides the renderer config needed to initialize the embedder engine and also handles external
15+
* texture management. This is initialized during FlutterEngine creation and then attached to the
16+
* FlutterView once the FlutterViewController is initializer.
17+
*/
18+
@interface FlutterOpenGLRenderer : NSObject <FlutterTextureRegistry>
19+
20+
/**
21+
* The resource context used by the engine for texture uploads. FlutterViews associated with this
22+
* engine should be created to share with this context.
23+
*/
24+
@property(nonatomic, readonly, nullable) NSOpenGLContext* resourceContext;
25+
26+
/**
27+
* Intializes the renderer with the given FlutterEngine.
28+
*/
29+
- (instancetype)initWithFlutterEngine:(FLUTTER_API_SYMBOL(FlutterEngine))engine;
30+
31+
/**
32+
* Attaches to the FlutterView and sets up the renderers main OpenGL context.
33+
*/
34+
- (void)attachToFlutterView:(FlutterView*)view;
35+
36+
/**
37+
* Called by the engine to make the context the engine should draw into current.
38+
*/
39+
- (bool)makeCurrent;
40+
41+
/**
42+
* Called by the engine to clear the context the engine should draw into.
43+
*/
44+
- (bool)clearCurrent;
45+
46+
/**
47+
* Called by the engine when the context's buffers should be swapped.
48+
*/
49+
- (bool)present;
50+
51+
/**
52+
* Called by the engine when framebuffer object ID is requested.
53+
*/
54+
- (uint32_t)getFBO:(const FlutterFrameInfo*)info;
55+
56+
/**
57+
* Makes the resource context the current context.
58+
*/
59+
- (bool)makeResourceCurrent;
60+
61+
/**
62+
* Called by the engine to unset the resource context.
63+
*/
64+
- (void)clearResourceContext;
65+
66+
/**
67+
* Populates the texture registry with the provided openGLTexture.
68+
*/
69+
- (BOOL)populateTextureWithIdentifier:(int64_t)textureID
70+
openGLTexture:(FlutterOpenGLTexture*)openGLTexture;
71+
72+
/**
73+
* Creates a FlutterRendererConfig that renders using OpenGL context(s) held
74+
* by this class.
75+
*/
76+
- (FlutterRendererConfig)createRendererConfig;
77+
78+
@end
79+
80+
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)