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

Commit 83c4c95

Browse files
committed
Merge FlutterMetaCompositor with FlutterCompositor.
1 parent a71c0c6 commit 83c4c95

File tree

8 files changed

+166
-237
lines changed

8 files changed

+166
-237
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2594,6 +2594,7 @@ FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterChann
25942594
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterChannelKeyResponderUnittests.mm
25952595
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterCompositor.h
25962596
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterCompositor.mm
2597+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterCompositorUnittests.mm
25972598
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject.mm
25982599
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject_Internal.h
25992600
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterEmbedderExternalTextureUnittests.mm
@@ -2618,10 +2619,7 @@ FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMacOS
26182619
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMenuPlugin.h
26192620
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMenuPlugin.mm
26202621
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMenuPluginTest.mm
2621-
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMenuPlugin_Internal.h
2622-
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMetalCompositor.h
2623-
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMetalCompositor.mm
2624-
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMetalCompositorUnittests.mm
2622+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMenuPlugin_Internal.hƒ
26252623
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMetalRenderer.h
26262624
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMetalRenderer.mm
26272625
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMetalRendererTest.mm

shell/platform/darwin/macos/BUILD.gn

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ source_set("flutter_framework_source") {
8282
"framework/Source/FlutterMenuPlugin.h",
8383
"framework/Source/FlutterMenuPlugin.mm",
8484
"framework/Source/FlutterMenuPlugin_Internal.h",
85-
"framework/Source/FlutterMetalCompositor.h",
86-
"framework/Source/FlutterMetalCompositor.mm",
8785
"framework/Source/FlutterMetalRenderer.h",
8886
"framework/Source/FlutterMetalRenderer.mm",
8987
"framework/Source/FlutterMouseCursorPlugin.h",
@@ -171,14 +169,14 @@ executable("flutter_desktop_darwin_unittests") {
171169
sources = [
172170
"framework/Source/AccessibilityBridgeMacTest.mm",
173171
"framework/Source/FlutterChannelKeyResponderUnittests.mm",
172+
"framework/Source/FlutterCompositorUnittests.mm",
174173
"framework/Source/FlutterEmbedderExternalTextureUnittests.mm",
175174
"framework/Source/FlutterEmbedderKeyResponderUnittests.mm",
176175
"framework/Source/FlutterEngineTest.mm",
177176
"framework/Source/FlutterEngineTestUtils.h",
178177
"framework/Source/FlutterEngineTestUtils.mm",
179178
"framework/Source/FlutterKeyboardManagerUnittests.mm",
180179
"framework/Source/FlutterMenuPluginTest.mm",
181-
"framework/Source/FlutterMetalCompositorUnittests.mm",
182180
"framework/Source/FlutterMetalRendererTest.mm",
183181
"framework/Source/FlutterMetalSurfaceManagerTest.mm",
184182
"framework/Source/FlutterPlatformNodeDelegateMacTest.mm",

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <list>
1010

1111
#include "flutter/fml/macros.h"
12+
#include "flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewController.h"
1213
#include "flutter/shell/platform/darwin/macos/framework/Source/FlutterView.h"
1314
#include "flutter/shell/platform/darwin/macos/framework/Source/FlutterViewProvider.h"
1415
#include "flutter/shell/platform/embedder/embedder.h"
@@ -25,9 +26,12 @@ class FlutterCompositor {
2526
// The view_provider is used to query FlutterViews from view IDs,
2627
// which are used for presenting and creating backing stores.
2728
// It must not be null, and is typically FlutterViewEngineProvider.
28-
explicit FlutterCompositor(id<FlutterViewProvider> view_provider);
29+
explicit FlutterCompositor(
30+
id<FlutterViewProvider> view_provider,
31+
FlutterPlatformViewController* platform_views_controller,
32+
id<MTLDevice> mtl_device);
2933

30-
virtual ~FlutterCompositor() = default;
34+
~FlutterCompositor() = default;
3135

3236
// Creates a BackingStore and saves updates the backing_store_out
3337
// data with the new BackingStore data.
@@ -40,17 +44,16 @@ class FlutterCompositor {
4044
// saved as user_data in the backing store must be collected
4145
// in the backing_store's destruction_callback field which will
4246
// be called when the embedder collects the backing store.
43-
virtual bool CreateBackingStore(const FlutterBackingStoreConfig* config,
44-
FlutterBackingStore* backing_store_out) = 0;
47+
bool CreateBackingStore(const FlutterBackingStoreConfig* config,
48+
FlutterBackingStore* backing_store_out);
4549

4650
// Releases the memory for any state used by the backing store.
47-
virtual bool CollectBackingStore(
48-
const FlutterBackingStore* backing_store) = 0;
51+
bool CollectBackingStore(const FlutterBackingStore* backing_store);
4952

5053
// Presents the FlutterLayers by updating FlutterView(s) using the
5154
// layer content.
5255
// Present sets frame_started_ to false.
53-
virtual bool Present(const FlutterLayer** layers, size_t layers_count) = 0;
56+
bool Present(const FlutterLayer** layers, size_t layers_count);
5457

5558
using PresentCallback = std::function<bool(bool has_flutter_content)>;
5659

@@ -105,6 +108,16 @@ class FlutterCompositor {
105108
// Current frame status.
106109
FrameStatus frame_status_ = kEnded;
107110

111+
// Presents the platform view layer represented by `layer`. `layer_index` is
112+
// used to position the layer in the z-axis. If the layer does not have a
113+
// superview, it will become subview of `default_base_view`.
114+
void PresentPlatformView(FlutterView* default_base_view,
115+
const FlutterLayer* layer,
116+
size_t layer_position);
117+
118+
const id<MTLDevice> mtl_device_;
119+
const FlutterPlatformViewController* platform_views_controller_;
120+
108121
FML_DISALLOW_COPY_AND_ASSIGN(FlutterCompositor);
109122
};
110123

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

Lines changed: 128 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,140 @@
44

55
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterCompositor.h"
66
#include "flutter/fml/logging.h"
7+
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterIOSurfaceHolder.h"
78

89
namespace flutter {
910

10-
FlutterCompositor::FlutterCompositor(id<FlutterViewProvider> view_provider)
11-
: view_provider_(view_provider) {
11+
FlutterCompositor::FlutterCompositor(id<FlutterViewProvider> view_provider,
12+
FlutterPlatformViewController* platform_views_controller,
13+
id<MTLDevice> mtl_device)
14+
: view_provider_(view_provider),
15+
mtl_device_(mtl_device),
16+
platform_views_controller_(platform_views_controller) {
1217
FML_CHECK(view_provider != nullptr) << "FlutterViewProvider* cannot be nullptr";
1318
}
1419

20+
bool FlutterCompositor::CreateBackingStore(const FlutterBackingStoreConfig* config,
21+
FlutterBackingStore* backing_store_out) {
22+
// TODO(dkwingsmt): This class only supports single-view for now. As more
23+
// classes are gradually converted to multi-view, it should get the view ID
24+
// from somewhere.
25+
FlutterView* view = GetView(kFlutterDefaultViewId);
26+
if (!view) {
27+
return false;
28+
}
29+
30+
CGSize size = CGSizeMake(config->size.width, config->size.height);
31+
32+
backing_store_out->metal.struct_size = sizeof(FlutterMetalBackingStore);
33+
backing_store_out->metal.texture.struct_size = sizeof(FlutterMetalTexture);
34+
35+
if (GetFrameStatus() != FrameStatus::kStarted) {
36+
StartFrame();
37+
// If the backing store is for the first layer, return the MTLTexture for the
38+
// FlutterView.
39+
FlutterMetalRenderBackingStore* backingStore =
40+
reinterpret_cast<FlutterMetalRenderBackingStore*>([view backingStoreForSize:size]);
41+
backing_store_out->metal.texture.texture =
42+
(__bridge FlutterMetalTextureHandle)backingStore.texture;
43+
} else {
44+
FlutterIOSurfaceHolder* io_surface_holder = [[FlutterIOSurfaceHolder alloc] init];
45+
[io_surface_holder recreateIOSurfaceWithSize:size];
46+
auto texture_descriptor =
47+
[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm
48+
width:size.width
49+
height:size.height
50+
mipmapped:NO];
51+
texture_descriptor.usage =
52+
MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget | MTLTextureUsageShaderWrite;
53+
54+
backing_store_out->metal.texture.texture = (__bridge_retained FlutterMetalTextureHandle)
55+
[mtl_device_ newTextureWithDescriptor:texture_descriptor
56+
iosurface:[io_surface_holder ioSurface]
57+
plane:0];
58+
59+
backing_store_out->metal.texture.user_data = (__bridge_retained void*)io_surface_holder;
60+
}
61+
62+
backing_store_out->type = kFlutterBackingStoreTypeMetal;
63+
backing_store_out->metal.texture.destruction_callback = [](void* user_data) {
64+
if (user_data != nullptr) {
65+
CFRelease(user_data);
66+
}
67+
};
68+
69+
return true;
70+
}
71+
72+
bool FlutterCompositor::Present(const FlutterLayer** layers, size_t layers_count) {
73+
// TODO(dkwingsmt): This class only supports single-view for now. As more
74+
// classes are gradually converted to multi-view, it should get the view ID
75+
// from somewhere.
76+
FlutterView* view = GetView(kFlutterDefaultViewId);
77+
if (!view) {
78+
return false;
79+
}
80+
81+
SetFrameStatus(FrameStatus::kPresenting);
82+
83+
bool has_flutter_content = false;
84+
for (size_t i = 0; i < layers_count; ++i) {
85+
const auto* layer = layers[i];
86+
FlutterBackingStore* backing_store = const_cast<FlutterBackingStore*>(layer->backing_store);
87+
88+
switch (layer->type) {
89+
case kFlutterLayerContentTypeBackingStore: {
90+
if (backing_store->metal.texture.user_data) {
91+
FlutterIOSurfaceHolder* io_surface_holder =
92+
(__bridge FlutterIOSurfaceHolder*)backing_store->metal.texture.user_data;
93+
IOSurfaceRef io_surface = [io_surface_holder ioSurface];
94+
InsertCALayerForIOSurface(view, io_surface);
95+
}
96+
has_flutter_content = true;
97+
break;
98+
}
99+
case kFlutterLayerContentTypePlatformView: {
100+
PresentPlatformView(view, layer, i);
101+
break;
102+
}
103+
};
104+
}
105+
106+
return EndFrame(has_flutter_content);
107+
}
108+
109+
void FlutterCompositor::PresentPlatformView(FlutterView* default_base_view,
110+
const FlutterLayer* layer,
111+
size_t layer_position) {
112+
// TODO (https://github.com/flutter/flutter/issues/96668)
113+
// once the issue is fixed, this check will pass.
114+
FML_DCHECK([[NSThread currentThread] isMainThread])
115+
<< "Must be on the main thread to present platform views";
116+
117+
int64_t platform_view_id = layer->platform_view->identifier;
118+
NSView* platform_view = [platform_views_controller_ platformViewWithID:platform_view_id];
119+
120+
FML_DCHECK(platform_view) << "Platform view not found for id: " << platform_view_id;
121+
122+
CGFloat scale = [[NSScreen mainScreen] backingScaleFactor];
123+
platform_view.frame = CGRectMake(layer->offset.x / scale, layer->offset.y / scale,
124+
layer->size.width / scale, layer->size.height / scale);
125+
if (platform_view.superview == nil) {
126+
[default_base_view addSubview:platform_view];
127+
}
128+
platform_view.layer.zPosition = layer_position;
129+
}
130+
131+
bool FlutterCompositor::CollectBackingStore(const FlutterBackingStore* backing_store) {
132+
// If we allocated this MTLTexture ourselves, user_data is not null, and we will need
133+
// to release it manually.
134+
if (backing_store->metal.texture.user_data != nullptr &&
135+
backing_store->metal.texture.texture != nullptr) {
136+
CFRelease(backing_store->metal.texture.texture);
137+
}
138+
return true;
139+
}
140+
15141
void FlutterCompositor::SetPresentCallback(
16142
const FlutterCompositor::PresentCallback& present_callback) {
17143
present_callback_ = present_callback;

shell/platform/darwin/macos/framework/Source/FlutterMetalCompositorUnittests.mm renamed to shell/platform/darwin/macos/framework/Source/FlutterCompositorUnittests.mm

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

8-
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterMetalCompositor.h"
8+
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterCompositor.h"
99
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterView.h"
1010
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterViewProvider.h"
1111
#import "flutter/testing/testing.h"
@@ -62,10 +62,10 @@ - (nullable FlutterView*)getView:(uint64_t)viewId {
6262
}
6363
} // namespace
6464

65-
TEST(FlutterMetalCompositorTest, TestPresent) {
66-
std::unique_ptr<flutter::FlutterMetalCompositor> macos_compositor =
67-
std::make_unique<FlutterMetalCompositor>(
68-
MockViewProvider(), /*platform_view_controller*/ nullptr, /*mtl_device*/ nullptr);
65+
TEST(FlutterCompositorTest, TestPresent) {
66+
std::unique_ptr<flutter::FlutterCompositor> macos_compositor =
67+
std::make_unique<FlutterCompositor>(MockViewProvider(), /*platform_view_controller*/ nullptr,
68+
/*mtl_device*/ nullptr);
6969

7070
bool flag = false;
7171
macos_compositor->SetPresentCallback([f = &flag](bool has_flutter_content) {
@@ -77,10 +77,10 @@ - (nullable FlutterView*)getView:(uint64_t)viewId {
7777
ASSERT_TRUE(flag);
7878
}
7979

80-
TEST(FlutterMetalCompositorTest, TestCreate) {
81-
std::unique_ptr<flutter::FlutterMetalCompositor> macos_compositor =
82-
std::make_unique<FlutterMetalCompositor>(
83-
MockViewProvider(), /*platform_view_controller*/ nullptr, /*mtl_device*/ nullptr);
80+
TEST(FlutterCompositorTest, TestCreate) {
81+
std::unique_ptr<flutter::FlutterCompositor> macos_compositor =
82+
std::make_unique<FlutterCompositor>(MockViewProvider(), /*platform_view_controller*/ nullptr,
83+
/*mtl_device*/ nullptr);
8484

8585
FlutterBackingStore backing_store;
8686
FlutterBackingStoreConfig config;
@@ -96,10 +96,10 @@ - (nullable FlutterView*)getView:(uint64_t)viewId {
9696
ASSERT_EQ(texture.height, 600ul);
9797
}
9898

99-
TEST(FlutterMetalCompositorTest, TestCompositing) {
100-
std::unique_ptr<flutter::FlutterMetalCompositor> macos_compositor =
101-
std::make_unique<FlutterMetalCompositor>(
102-
MockViewProvider(), /*platform_view_controller*/ nullptr, /*mtl_device*/ nullptr);
99+
TEST(FlutterCompositorTest, TestCompositing) {
100+
std::unique_ptr<flutter::FlutterCompositor> macos_compositor =
101+
std::make_unique<FlutterCompositor>(MockViewProvider(), /*platform_view_controller*/ nullptr,
102+
/*mtl_device*/ nullptr);
103103

104104
FlutterBackingStore backing_store;
105105
FlutterBackingStoreConfig config;

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject_Internal.h"
1313
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterMenuPlugin.h"
14-
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterMetalCompositor.h"
14+
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterCompositor.h"
1515
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterMetalRenderer.h"
1616
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterMouseCursorPlugin.h"
1717
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewController.h"
@@ -204,7 +204,6 @@ @implementation FlutterEngine {
204204

205205
// _macOSCompositor is created when the engine is created and
206206
// its destruction is handled by ARC when the engine is destroyed.
207-
// This is either a FlutterGLCompositor or a FlutterMetalCompositor instance.
208207
std::unique_ptr<flutter::FlutterCompositor> _macOSCompositor;
209208

210209
FlutterViewEngineProvider* _viewProvider;
@@ -429,7 +428,7 @@ - (FlutterCompositor*)createFlutterCompositor {
429428
__weak FlutterEngine* weakSelf = self;
430429

431430
FlutterMetalRenderer* metalRenderer = reinterpret_cast<FlutterMetalRenderer*>(_renderer);
432-
_macOSCompositor = std::make_unique<flutter::FlutterMetalCompositor>(
431+
_macOSCompositor = std::make_unique<flutter::FlutterCompositor>(
433432
_viewProvider, _platformViewController, metalRenderer.device);
434433
_macOSCompositor->SetPresentCallback([weakSelf](bool has_flutter_content) {
435434
if (has_flutter_content) {

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

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)