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

Commit 1a3a6d2

Browse files
committed
Addressing PR comments
1 parent 0177f9e commit 1a3a6d2

11 files changed

+97
-113
lines changed

shell/platform/darwin/macos/BUILD.gn

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ source_set("flutter_framework_source") {
7777
deps = [
7878
"//flutter/flow:flow",
7979
"//flutter/fml",
80-
"//flutter/fml:fml",
8180
"//flutter/shell/platform/common/cpp:common_cpp_switches",
8281
"//flutter/shell/platform/darwin/common:framework_shared",
8382
"//flutter/shell/platform/embedder:embedder_as_internal_library",
@@ -123,8 +122,8 @@ executable("flutter_desktop_darwin_unittests") {
123122
"framework/Source/FlutterEngineTest.mm",
124123
"framework/Source/FlutterMacOSGLCompositorUnittests.mm",
125124
"framework/Source/FlutterViewControllerTest.mm",
126-
"framework/Source/FlutterViewControllerTestsUtils.h",
127-
"framework/Source/FlutterViewControllerTestsUtils.mm",
125+
"framework/Source/FlutterViewControllerTestUtils.h",
126+
"framework/Source/FlutterViewControllerTestUtils.mm",
128127
]
129128

130129
cflags_objcc = [ "-fobjc-arc" ]

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,11 @@ @implementation FlutterEngine {
201201
// FlutterMacOSGLCompositor is created by the engine.
202202
// This is only created when the engine has a FlutterViewController
203203
// and used to support platform views.
204+
// Creation / Destruction.
204205
std::unique_ptr<flutter::FlutterMacOSGLCompositor> _macOSCompositor;
206+
207+
// FlutterCompositor is copied and used in embedder.cc.
208+
FlutterCompositor _compositor;
205209
}
206210

207211
- (instancetype)initWithName:(NSString*)labelPrefix project:(FlutterDartProject*)project {
@@ -311,14 +315,8 @@ - (BOOL)runWithEntrypoint:(NSString*)entrypoint {
311315
flutterArguments.aot_data = _aotData;
312316
}
313317

314-
// Only create a Compositor if we have a ViewController.
315-
if (_viewController) {
316-
// Engine does not need to manage the life cycle of compositor
317-
// since compositor is captured and copied in embedder.cc.
318-
FlutterCompositor compositor = {};
319-
[self setupCompositor:&compositor];
320-
flutterArguments.compositor = &compositor;
321-
}
318+
[self setupCompositor];
319+
flutterArguments.compositor = &_compositor;
322320

323321
FlutterEngineResult result = _embedderAPI.Initialize(
324322
FLUTTER_ENGINE_VERSION, &rendererConfig, &flutterArguments, (__bridge void*)(self), &_engine);
@@ -374,39 +372,41 @@ - (void)setViewController:(FlutterViewController*)controller {
374372
}
375373
}
376374

377-
- (void)setupCompositor:(FlutterCompositor*)compositor {
375+
- (void)setupCompositor {
378376
[_mainOpenGLContext makeCurrentContext];
379377

380-
_macOSCompositor =
381-
std::make_unique<flutter::FlutterMacOSGLCompositor>(_viewController, _resourceContext);
378+
_macOSCompositor = std::make_unique<flutter::FlutterMacOSGLCompositor>(_viewController);
382379

383-
compositor->struct_size = sizeof(FlutterCompositor);
384-
compositor->user_data = _macOSCompositor.get();
380+
_compositor = {};
381+
_compositor.struct_size = sizeof(FlutterCompositor);
382+
_compositor.user_data = _macOSCompositor.get();
385383

386-
compositor->create_backing_store_callback = [](const FlutterBackingStoreConfig* config, //
384+
_compositor.create_backing_store_callback = [](const FlutterBackingStoreConfig* config, //
387385
FlutterBackingStore* backing_store_out, //
388386
void* user_data //
389387
) {
390388
return reinterpret_cast<flutter::FlutterMacOSGLCompositor*>(user_data)->CreateBackingStore(
391389
config, backing_store_out);
392390
};
393391

394-
compositor->collect_backing_store_callback = [](const FlutterBackingStore* backing_store, //
392+
_compositor.collect_backing_store_callback = [](const FlutterBackingStore* backing_store, //
395393
void* user_data //
396394
) {
397395
return reinterpret_cast<flutter::FlutterMacOSGLCompositor*>(user_data)->CollectBackingStore(
398396
backing_store);
399397
};
400398

401-
compositor->present_layers_callback = [](const FlutterLayer** layers, //
399+
_compositor.present_layers_callback = [](const FlutterLayer** layers, //
402400
size_t layers_count, //
403401
void* user_data //
404402
) {
405403
return reinterpret_cast<flutter::FlutterMacOSGLCompositor*>(user_data)->Present(layers,
406404
layers_count);
407405
};
408406

409-
_macOSCompositor->SetPresentCallback([self]() { return [self engineCallbackOnPresent]; });
407+
__weak FlutterEngine* weak_self = self;
408+
_macOSCompositor->SetPresentCallback(
409+
[weak_self]() { return [weak_self engineCallbackOnPresent]; });
410410
}
411411

412412
- (id<FlutterBinaryMessenger>)binaryMessenger {

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

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,20 @@
1212

1313
namespace flutter {
1414

15-
/**
16-
* FlutterMacOSGLCompositor creates and manages backing stores used for
17-
* rendering Flutter content and presents Flutter content and Platform views.
18-
*/
15+
// FlutterMacOSGLCompositor creates and manages the backing stores used for
16+
// rendering Flutter content and presents Flutter content and Platform views.
17+
// Platform views are not yet supported.
18+
// FlutterMacOSGLCompositor is created and destroyed by FlutterEngine.
1919
class FlutterMacOSGLCompositor {
2020
public:
21-
FlutterMacOSGLCompositor(FlutterViewController* view_controller,
22-
NSOpenGLContext* open_gl_context);
21+
FlutterMacOSGLCompositor(FlutterViewController* view_controller);
2322

24-
virtual ~FlutterMacOSGLCompositor();
25-
26-
// Creates a backing store according to FlutterBackingStoreConfig
27-
// by modifying backing_store_out.
23+
// Creates a FlutterSurfaceManager and uses the FlutterSurfaceManager's
24+
// underlying FBO and texture in the backing store.
25+
// Any additional state allocated for the backing store and
26+
// saved as user_data in the backing store must be collected
27+
// in the backing_store's desctruction_callback field which will
28+
// be called when the embedder collects the backing store.
2829
bool CreateBackingStore(const FlutterBackingStoreConfig* config,
2930
FlutterBackingStore* backing_store_out);
3031

@@ -40,17 +41,11 @@ class FlutterMacOSGLCompositor {
4041
// PresentCallback is called at the end of the Present function.
4142
void SetPresentCallback(const PresentCallback& present_callback);
4243

43-
protected:
44+
private:
4445
FlutterViewController* view_controller_;
4546
PresentCallback present_callback_;
4647
NSOpenGLContext* open_gl_context_;
4748

48-
// Creates a FlutterSurfaceManager and uses the FlutterSurfaceManager's
49-
// underlying FBO and texture in the backing store.
50-
bool CreateBackingStoreUsingSurfaceManager(
51-
const FlutterBackingStoreConfig* config,
52-
FlutterBackingStore* backing_store_out);
53-
5449
FML_DISALLOW_COPY_AND_ASSIGN(FlutterMacOSGLCompositor);
5550
};
5651

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

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,39 @@
1616

1717
namespace flutter {
1818

19-
FlutterMacOSGLCompositor::FlutterMacOSGLCompositor(FlutterViewController* view_controller,
20-
NSOpenGLContext* open_gl_context)
21-
: view_controller_(view_controller), open_gl_context_(open_gl_context) {}
22-
23-
FlutterMacOSGLCompositor::~FlutterMacOSGLCompositor() = default;
19+
FlutterMacOSGLCompositor::FlutterMacOSGLCompositor(FlutterViewController* view_controller)
20+
: view_controller_(view_controller),
21+
open_gl_context_(view_controller.flutterView.openGLContext) {}
2422

2523
bool FlutterMacOSGLCompositor::CreateBackingStore(const FlutterBackingStoreConfig* config,
2624
FlutterBackingStore* backing_store_out) {
27-
return CreateBackingStoreUsingSurfaceManager(config, backing_store_out);
25+
FlutterSurfaceManager* surfaceManager =
26+
[[FlutterSurfaceManager alloc] initWithLayer:view_controller_.flutterView.layer
27+
openGLContext:open_gl_context_
28+
numFramebuffers:1];
29+
30+
GLuint fbo = [surfaceManager getFramebuffer];
31+
GLuint texture = [surfaceManager getTexture];
32+
33+
CGSize size = CGSizeMake(config->size.width, config->size.height);
34+
size_t kFlutterSurfaceManagerFrontBuffer = 0;
35+
[surfaceManager backTextureWithIOSurface:kFlutterSurfaceManagerFrontBuffer
36+
size:size
37+
backingTexture:texture
38+
fbo:fbo];
39+
40+
backing_store_out->type = kFlutterBackingStoreTypeOpenGL;
41+
backing_store_out->open_gl.type = kFlutterOpenGLTargetTypeFramebuffer;
42+
backing_store_out->open_gl.framebuffer.target = GL_RGBA8;
43+
backing_store_out->open_gl.framebuffer.name = fbo;
44+
backing_store_out->open_gl.framebuffer.user_data = (__bridge_retained void*)surfaceManager;
45+
backing_store_out->open_gl.framebuffer.destruction_callback = [](void* user_data) {
46+
if (user_data != nullptr) {
47+
CFRelease(user_data);
48+
}
49+
};
50+
51+
return true;
2852
}
2953

3054
bool FlutterMacOSGLCompositor::CollectBackingStore(const FlutterBackingStore* backing_store) {
@@ -40,11 +64,12 @@
4064
switch (layer->type) {
4165
case kFlutterLayerContentTypeBackingStore: {
4266
FlutterSurfaceManager* surfaceManager =
43-
(__bridge FlutterSurfaceManager*)backing_store->user_data;
67+
(__bridge FlutterSurfaceManager*)backing_store->open_gl.framebuffer.user_data;
4468

4569
CGSize size = CGSizeMake(layer->size.width, layer->size.height);
4670
[view_controller_.flutterView frameBufferIDForSize:size];
47-
[surfaceManager setLayerContentWithIOSurface:[surfaceManager getIOSurface]];
71+
size_t kFlutterSurfaceManagerFrontBuffer = 0;
72+
[surfaceManager setLayerContentWithIOSurface:kFlutterSurfaceManagerFrontBuffer];
4873
break;
4974
}
5075
case kFlutterLayerContentTypePlatformView:
@@ -56,36 +81,6 @@
5681
return present_callback_();
5782
}
5883

59-
bool FlutterMacOSGLCompositor::CreateBackingStoreUsingSurfaceManager(
60-
const FlutterBackingStoreConfig* config,
61-
FlutterBackingStore* backing_store_out) {
62-
FlutterSurfaceManager* surfaceManager =
63-
[[FlutterSurfaceManager alloc] initWithLayer:view_controller_.flutterView.layer
64-
openGLContext:open_gl_context_];
65-
66-
GLuint fbo = [surfaceManager getFramebuffer];
67-
GLuint texture = [surfaceManager getTexture];
68-
IOSurfaceRef* io_surface_ref = [surfaceManager getIOSurface];
69-
70-
CGSize size = CGSizeMake(config->size.width, config->size.height);
71-
72-
[surfaceManager backTextureWithIOSurface:io_surface_ref size:size backingTexture:texture fbo:fbo];
73-
74-
backing_store_out->type = kFlutterBackingStoreTypeOpenGL;
75-
backing_store_out->user_data = (__bridge_retained void*)surfaceManager;
76-
backing_store_out->open_gl.type = kFlutterOpenGLTargetTypeFramebuffer;
77-
backing_store_out->open_gl.framebuffer.target = GL_RGBA8;
78-
backing_store_out->open_gl.framebuffer.name = fbo;
79-
backing_store_out->open_gl.framebuffer.user_data = backing_store_out->user_data;
80-
backing_store_out->open_gl.framebuffer.destruction_callback = [](void* user_data) {
81-
if (user_data != nullptr) {
82-
CFRelease(user_data);
83-
}
84-
};
85-
86-
return true;
87-
}
88-
8984
void FlutterMacOSGLCompositor::SetPresentCallback(
9085
const FlutterMacOSGLCompositor::PresentCallback& present_callback) {
9186
present_callback_ = present_callback;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#import <Foundation/Foundation.h>
66

77
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterMacOSGLCompositor.h"
8-
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterViewControllerTestsUtils.h"
8+
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterViewControllerTestUtils.h"
99
#import "flutter/testing/testing.h"
1010

1111
namespace flutter::testing {
@@ -14,7 +14,7 @@
1414
id mockViewController = CreateMockViewController(nil);
1515

1616
std::unique_ptr<flutter::FlutterMacOSGLCompositor> macos_compositor =
17-
std::make_unique<FlutterMacOSGLCompositor>(mockViewController, nil);
17+
std::make_unique<FlutterMacOSGLCompositor>(mockViewController);
1818

1919
bool flag = false;
2020
macos_compositor->SetPresentCallback([f = &flag]() {

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

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
// Manages the IOSurfaces for FlutterView
44
@interface FlutterSurfaceManager : NSObject
55

6-
- (instancetype)initWithLayer:(CALayer*)layer openGLContext:(NSOpenGLContext*)opengLContext;
6+
- (instancetype)initWithLayer:(CALayer*)containingLayer
7+
openGLContext:(NSOpenGLContext*)openGLContext
8+
numFramebuffers:(int)numFramebuffers;
79

810
- (void)ensureSurfaceSize:(CGSize)size;
911
- (void)swapBuffers;
@@ -16,14 +18,15 @@
1618
- (void)setLayerContent;
1719

1820
/**
19-
* Sets the CALayer content to the content of the provided ioSurface.
21+
* Sets the CALayer content to the content of ioSurface at ioSurfaceNum.
2022
*/
21-
- (void)setLayerContentWithIOSurface:(IOSurfaceRef*)ioSurface;
23+
// TODO(richardjcai): Fix this and remove setLayerContent.
24+
- (void)setLayerContentWithIOSurface:(int)ioSurfaceNum;
2225

2326
/**
2427
* Binds the IOSurface to the provided texture/framebuffer.
2528
*/
26-
- (void)backTextureWithIOSurface:(IOSurfaceRef*)ioSurface
29+
- (void)backTextureWithIOSurface:(int)ioSurfaceNum
2730
size:(CGSize)size
2831
backingTexture:(GLuint)texture
2932
fbo:(GLuint)fbo;
@@ -47,14 +50,4 @@
4750
*/
4851
- (uint32_t)getTexture;
4952

50-
/**
51-
* Returns the kFront IOSurfaceRef.
52-
* The IOSurface is backed by the FBO provided by getFramebuffer
53-
* and texture provided by getTexture. The IOSurface is used
54-
* in FlutterMacOSCompositor's Present call.
55-
* The IOSurface is collected when the backing store that uses the
56-
* IOSurface is collected.
57-
*/
58-
- (IOSurfaceRef*)getIOSurface;
59-
6053
@end

0 commit comments

Comments
 (0)