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

Commit 24b2533

Browse files
committed
Create FlutterGLCompositor.
Refactor rendering one layer to use the FlutterGLCompositor. In this commit, FlutterGLCompositor only supports rendering one view and does not support rendering platform views.
1 parent aa7b403 commit 24b2533

11 files changed

+329
-24
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,8 @@ FILE: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FlutterPlug
10471047
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FlutterViewController.h
10481048
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Info.plist
10491049
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterAppDelegate.mm
1050+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterBackingStoreData.h
1051+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterBackingStoreData.mm
10501052
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject.mm
10511053
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject_Internal.h
10521054
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm

shell/platform/darwin/macos/BUILD.gn

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ source_set("flutter_framework_source") {
4646

4747
sources = [
4848
"framework/Source/FlutterAppDelegate.mm",
49+
"framework/Source/FlutterBackingStoreData.h",
50+
"framework/Source/FlutterBackingStoreData.mm",
4951
"framework/Source/FlutterDartProject.mm",
5052
"framework/Source/FlutterDartProject_Internal.h",
5153
"framework/Source/FlutterEngine.mm",
@@ -54,6 +56,8 @@ source_set("flutter_framework_source") {
5456
"framework/Source/FlutterExternalTextureGL.mm",
5557
"framework/Source/FlutterFrameBufferProvider.h",
5658
"framework/Source/FlutterFrameBufferProvider.mm",
59+
"framework/Source/FlutterGLCompositor.h",
60+
"framework/Source/FlutterGLCompositor.mm",
5761
"framework/Source/FlutterIOSurfaceHolder.h",
5862
"framework/Source/FlutterIOSurfaceHolder.mm",
5963
"framework/Source/FlutterMouseCursorPlugin.h",
@@ -77,9 +81,12 @@ source_set("flutter_framework_source") {
7781
sources += _flutter_framework_headers
7882

7983
deps = [
84+
"//flutter/flow:flow",
85+
"//flutter/fml",
8086
"//flutter/shell/platform/common/cpp:common_cpp_switches",
8187
"//flutter/shell/platform/darwin/common:framework_shared",
8288
"//flutter/shell/platform/embedder:embedder_as_internal_library",
89+
"//third_party/skia",
8390
]
8491

8592
public_configs = [ "//flutter:config" ]
@@ -119,7 +126,10 @@ executable("flutter_desktop_darwin_unittests") {
119126

120127
sources = [
121128
"framework/Source/FlutterEngineTest.mm",
129+
"framework/Source/FlutterGLCompositorUnittests.mm",
122130
"framework/Source/FlutterViewControllerTest.mm",
131+
"framework/Source/FlutterViewControllerTestUtils.h",
132+
"framework/Source/FlutterViewControllerTestUtils.mm",
123133
]
124134

125135
cflags_objcc = [ "-fobjc-arc" ]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
/**
8+
* FlutterBackingStoreData holds data to be stored in the
9+
* BackingStore's user_data.
10+
*/
11+
@interface FlutterBackingStoreData : NSObject
12+
13+
- (nullable instancetype)initWithIsRootView:(bool)is_root_view;
14+
15+
- (bool)isRootView;
16+
17+
@end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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/FlutterBackingStoreData.h"
6+
7+
#include <OpenGL/gl.h>
8+
9+
@interface FlutterBackingStoreData () {
10+
bool _isRootView;
11+
}
12+
@end
13+
14+
@implementation FlutterBackingStoreData
15+
16+
- (nullable instancetype)initWithIsRootView:(bool)isRootView {
17+
if (self = [super init]) {
18+
_isRootView = isRootView;
19+
}
20+
return self;
21+
}
22+
23+
- (bool)isRootView {
24+
return _isRootView;
25+
}
26+
27+
@end

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

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +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/FlutterViewController_Internal.h"
13+
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterGLCompositor.h"
1414
#import "flutter/shell/platform/embedder/embedder.h"
1515

1616
/**
@@ -197,6 +197,10 @@ @implementation FlutterEngine {
197197

198198
// Pointer to the Dart AOT snapshot and instruction data.
199199
_FlutterEngineAOTData* _aotData;
200+
201+
// _macOSGLCompositor is created when the engine is created and
202+
// it's destruction is handled by ARC when the engine is destroyed.
203+
std::unique_ptr<flutter::FlutterGLCompositor> _macOSGLCompositor;
200204
}
201205

202206
- (instancetype)initWithName:(NSString*)labelPrefix project:(FlutterDartProject*)project {
@@ -306,6 +310,11 @@ - (BOOL)runWithEntrypoint:(NSString*)entrypoint {
306310
flutterArguments.aot_data = _aotData;
307311
}
308312

313+
// Embedder.cc makes a deep copy of the compositor so we do not
314+
// need to store compositor on the heap.
315+
FlutterCompositor compositor = [self createFlutterCompositor];
316+
flutterArguments.compositor = &compositor;
317+
309318
FlutterEngineResult result = _embedderAPI.Initialize(
310319
FLUTTER_ENGINE_VERSION, &rendererConfig, &flutterArguments, (__bridge void*)(self), &_engine);
311320
if (result != kSuccess) {
@@ -360,6 +369,45 @@ - (void)setViewController:(FlutterViewController*)controller {
360369
}
361370
}
362371

372+
- (FlutterCompositor)createFlutterCompositor {
373+
[_mainOpenGLContext makeCurrentContext];
374+
375+
_macOSGLCompositor = std::make_unique<flutter::FlutterGLCompositor>(_viewController);
376+
377+
FlutterCompositor compositor = {};
378+
compositor.struct_size = sizeof(FlutterCompositor);
379+
compositor.user_data = _macOSGLCompositor.get();
380+
381+
compositor.create_backing_store_callback = [](const FlutterBackingStoreConfig* config, //
382+
FlutterBackingStore* backing_store_out, //
383+
void* user_data //
384+
) {
385+
return reinterpret_cast<flutter::FlutterGLCompositor*>(user_data)->CreateBackingStore(
386+
config, backing_store_out);
387+
};
388+
389+
compositor.collect_backing_store_callback = [](const FlutterBackingStore* backing_store, //
390+
void* user_data //
391+
) {
392+
return reinterpret_cast<flutter::FlutterGLCompositor*>(user_data)->CollectBackingStore(
393+
backing_store);
394+
};
395+
396+
compositor.present_layers_callback = [](const FlutterLayer** layers, //
397+
size_t layers_count, //
398+
void* user_data //
399+
) {
400+
return reinterpret_cast<flutter::FlutterGLCompositor*>(user_data)->Present(layers,
401+
layers_count);
402+
};
403+
404+
__weak FlutterEngine* weak_self = self;
405+
_macOSGLCompositor->SetPresentCallback(
406+
[weak_self]() { return [weak_self engineCallbackOnPresent]; });
407+
408+
return compositor;
409+
}
410+
363411
- (id<FlutterBinaryMessenger>)binaryMessenger {
364412
// TODO(stuartmorgan): Switch to FlutterBinaryMessengerRelay to avoid plugins
365413
// keeping the engine alive.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
#include <vector>
6+
7+
#include "flutter/fml/macros.h"
8+
#include "flutter/shell/platform/darwin/macos/framework/Source/FlutterSurfaceManager.h"
9+
#include "flutter/shell/platform/darwin/macos/framework/Source/FlutterViewController_Internal.h"
10+
#include "flutter/shell/platform/embedder/embedder.h"
11+
#include "third_party/skia/include/gpu/GrDirectContext.h"
12+
13+
namespace flutter {
14+
15+
// FlutterGLCompositor 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+
// FlutterGLCompositor is created and destroyed by FlutterEngine.
19+
class FlutterGLCompositor {
20+
public:
21+
FlutterGLCompositor(FlutterViewController* view_controller);
22+
23+
// Creates a BackingStore and saves updates the backing_store_out
24+
// data with the new BackingStore data.
25+
// If the view requesting the backing store is the root view,
26+
// we do not create a new backing store but rather return the
27+
// backing store associated with the root view's FlutterSurfaceManager.
28+
//
29+
// Any additional state allocated for the backing store and
30+
// saved as user_data in the backing store must be collected
31+
// in the backing_store's desctruction_callback field which will
32+
// be called when the embedder collects the backing store.
33+
bool CreateBackingStore(const FlutterBackingStoreConfig* config,
34+
FlutterBackingStore* backing_store_out);
35+
36+
// Releases the memory for any state used by the backing store.
37+
bool CollectBackingStore(const FlutterBackingStore* backing_store);
38+
39+
// Presents the FlutterLayers by updating FlutterView(s) using the
40+
// layer content.
41+
bool Present(const FlutterLayer** layers, size_t layers_count);
42+
43+
using PresentCallback = std::function<bool()>;
44+
45+
// PresentCallback is called at the end of the Present function.
46+
void SetPresentCallback(const PresentCallback& present_callback);
47+
48+
private:
49+
FlutterViewController* view_controller_;
50+
PresentCallback present_callback_;
51+
NSOpenGLContext* open_gl_context_;
52+
53+
FML_DISALLOW_COPY_AND_ASSIGN(FlutterGLCompositor);
54+
};
55+
56+
} // namespace flutter
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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/FlutterGLCompositor.h"
6+
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterBackingStoreData.h"
7+
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterFrameBufferProvider.h"
8+
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterIOSurfaceHolder.h"
9+
10+
#import <OpenGL/gl.h>
11+
#import "flutter/fml/logging.h"
12+
#import "flutter/fml/platform/darwin/cf_utils.h"
13+
#import "third_party/skia/include/core/SkCanvas.h"
14+
#import "third_party/skia/include/core/SkSurface.h"
15+
#import "third_party/skia/include/gpu/gl/GrGLAssembleInterface.h"
16+
#import "third_party/skia/include/utils/mac/SkCGUtils.h"
17+
18+
#include <unistd.h>
19+
20+
namespace flutter {
21+
22+
FlutterGLCompositor::FlutterGLCompositor(FlutterViewController* view_controller)
23+
: view_controller_(view_controller),
24+
open_gl_context_(view_controller.flutterView.openGLContext) {}
25+
26+
bool FlutterGLCompositor::CreateBackingStore(const FlutterBackingStoreConfig* config,
27+
FlutterBackingStore* backing_store_out) {
28+
CGSize size = CGSizeMake(config->size.width, config->size.height);
29+
FlutterBackingStoreData* data =
30+
[[FlutterBackingStoreData alloc] initWithIsRootView:config->is_root_view];
31+
32+
backing_store_out->type = kFlutterBackingStoreTypeOpenGL;
33+
backing_store_out->is_cacheable = true;
34+
backing_store_out->open_gl.type = kFlutterOpenGLTargetTypeFramebuffer;
35+
backing_store_out->open_gl.framebuffer.target = GL_RGBA8;
36+
37+
if (config->is_root_view) {
38+
// The root view uses FlutterSurfaceManager and is not cacheable since
39+
// the fbo id changes on every present.
40+
backing_store_out->is_cacheable = false;
41+
auto fbo = [view_controller_.flutterView frameBufferIDForSize:size];
42+
backing_store_out->open_gl.framebuffer.name = fbo;
43+
} else {
44+
FML_CHECK(false) << "Compositor only supports creating a backing store for the root view";
45+
}
46+
47+
backing_store_out->open_gl.framebuffer.user_data = (__bridge_retained void*)data;
48+
backing_store_out->open_gl.framebuffer.destruction_callback = [](void* user_data) {
49+
if (user_data != nullptr) {
50+
CFRelease(user_data);
51+
}
52+
};
53+
54+
return true;
55+
}
56+
57+
bool FlutterGLCompositor::CollectBackingStore(const FlutterBackingStore* backing_store) {
58+
return true;
59+
}
60+
61+
bool FlutterGLCompositor::Present(const FlutterLayer** layers, size_t layers_count) {
62+
for (size_t i = 0; i < layers_count; ++i) {
63+
const auto* layer = layers[i];
64+
FlutterBackingStore* backing_store = const_cast<FlutterBackingStore*>(layer->backing_store);
65+
switch (layer->type) {
66+
case kFlutterLayerContentTypeBackingStore: {
67+
FlutterBackingStoreData* data =
68+
(__bridge FlutterBackingStoreData*)(backing_store->open_gl.framebuffer.user_data);
69+
if (![data isRootView]) {
70+
FML_CHECK(false) << "Compositor only supports presenting the root view.";
71+
}
72+
break;
73+
}
74+
case kFlutterLayerContentTypePlatformView:
75+
// Add functionality in follow up PR.
76+
FML_CHECK(false) << "Presenting PlatformViews not yet supported";
77+
break;
78+
};
79+
}
80+
return present_callback_();
81+
}
82+
83+
void FlutterGLCompositor::SetPresentCallback(
84+
const FlutterGLCompositor::PresentCallback& present_callback) {
85+
present_callback_ = present_callback;
86+
}
87+
88+
} // namespace flutter
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 <Foundation/Foundation.h>
6+
7+
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterGLCompositor.h"
8+
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterViewControllerTestUtils.h"
9+
#import "flutter/testing/testing.h"
10+
11+
namespace flutter::testing {
12+
13+
TEST(FlutterGLCompositorTest, TestPresent) {
14+
id mockViewController = CreateMockViewController(nil);
15+
16+
std::unique_ptr<flutter::FlutterGLCompositor> macos_compositor =
17+
std::make_unique<FlutterGLCompositor>(mockViewController);
18+
19+
bool flag = false;
20+
macos_compositor->SetPresentCallback([f = &flag]() {
21+
*f = true;
22+
return true;
23+
});
24+
25+
ASSERT_TRUE(macos_compositor->Present(nil, 0));
26+
ASSERT_TRUE(flag);
27+
}
28+
29+
} // flutter::testing

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

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,14 @@
99
#import "flutter/shell/platform/darwin/macos/framework/Headers/FlutterEngine.h"
1010
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject_Internal.h"
1111
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine_Internal.h"
12+
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterViewControllerTestUtils.h"
1213
#include "flutter/testing/testing.h"
1314

1415
namespace flutter::testing {
1516

16-
// Returns a mock FlutterViewController that is able to work in environments
17-
// without a real pasteboard.
18-
id mockViewController(NSString* pasteboardString) {
19-
NSString* fixtures = @(testing::GetFixturesPath());
20-
FlutterDartProject* project = [[FlutterDartProject alloc]
21-
initWithAssetsPath:fixtures
22-
ICUDataPath:[fixtures stringByAppendingString:@"/icudtl.dat"]];
23-
FlutterViewController* viewController = [[FlutterViewController alloc] initWithProject:project];
24-
25-
// Mock pasteboard so that this test will work in environments without a
26-
// real pasteboard.
27-
id pasteboardMock = OCMClassMock([NSPasteboard class]);
28-
OCMExpect([pasteboardMock stringForType:[OCMArg any]]).andDo(^(NSInvocation* invocation) {
29-
NSString* returnValue = pasteboardString.length > 0 ? pasteboardString : nil;
30-
[invocation setReturnValue:&returnValue];
31-
});
32-
id viewControllerMock = OCMPartialMock(viewController);
33-
OCMStub([viewControllerMock pasteboard]).andReturn(pasteboardMock);
34-
return viewControllerMock;
35-
}
36-
3717
TEST(FlutterViewController, HasStringsWhenPasteboardEmpty) {
3818
// Mock FlutterViewController so that it behaves like the pasteboard is empty.
39-
id viewControllerMock = mockViewController(nil);
19+
id viewControllerMock = CreateMockViewController(nil);
4020

4121
// Call hasStrings and expect it to be false.
4222
__block bool calledAfterClear = false;
@@ -56,7 +36,7 @@ id mockViewController(NSString* pasteboardString) {
5636
TEST(FlutterViewController, HasStringsWhenPasteboardFull) {
5737
// Mock FlutterViewController so that it behaves like the pasteboard has a
5838
// valid string.
59-
id viewControllerMock = mockViewController(@"some string");
39+
id viewControllerMock = CreateMockViewController(@"some string");
6040

6141
// Call hasStrings and expect it to be true.
6242
__block bool called = false;

0 commit comments

Comments
 (0)