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

Commit ac08def

Browse files
committed
Create FlutterPlatformViewController
1 parent 182b975 commit ac08def

10 files changed

+260
-187
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,8 @@ FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterIOSur
10721072
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterIOSurfaceHolder.mm
10731073
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMouseCursorPlugin.h
10741074
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMouseCursorPlugin.mm
1075+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewController.h
1076+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewController.mm
10751077
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViews.h
10761078
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.h
10771079
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.mm

shell/platform/darwin/macos/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ source_set("flutter_framework_source") {
6262
"framework/Source/FlutterIOSurfaceHolder.mm",
6363
"framework/Source/FlutterMouseCursorPlugin.h",
6464
"framework/Source/FlutterMouseCursorPlugin.mm",
65+
"framework/Source/FlutterPlatformViewController.mm",
66+
"framework/Source/FlutterPlatformViewController_Internal.h",
6567
"framework/Source/FlutterPlatformViews.h",
6668
"framework/Source/FlutterResizeSynchronizer.h",
6769
"framework/Source/FlutterResizeSynchronizer.mm",

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

Lines changed: 80 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject_Internal.h"
1212
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.h"
1313
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterGLCompositor.h"
14+
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewController_Internal.h"
1415
#import "flutter/shell/platform/embedder/embedder.h"
1516

1617
/**
@@ -204,6 +205,13 @@ @implementation FlutterEngine {
204205

205206
// FlutterCompositor is copied and used in embedder.cc.
206207
FlutterCompositor _compositor;
208+
209+
// A method channel for platform view functionality.
210+
FlutterMethodChannel* _platformViewsChannel;
211+
212+
// Used to support creation and deletion of platform views and
213+
// registering platform view factories.
214+
FlutterPlatformViewController* _platformViewController;
207215
}
208216

209217
- (instancetype)initWithName:(NSString*)labelPrefix project:(FlutterDartProject*)project {
@@ -313,6 +321,9 @@ - (BOOL)runWithEntrypoint:(NSString*)entrypoint {
313321
flutterArguments.aot_data = _aotData;
314322
}
315323

324+
[self setupPlatformViewChannel];
325+
[self createPlatformViewController];
326+
316327
flutterArguments.compositor = [self createFlutterCompositor];
317328

318329
FlutterEngineResult result = _embedderAPI.Initialize(
@@ -369,54 +380,6 @@ - (void)setViewController:(FlutterViewController*)controller {
369380
}
370381
}
371382

372-
- (FlutterCompositor*)createFlutterCompositor {
373-
// TODO(richardjcai): Add support for creating a FlutterGLCompositor
374-
// with a nil _viewController for headless engines.
375-
// https://github.com/flutter/flutter/issues/71606
376-
if (_viewController == nullptr) {
377-
return nullptr;
378-
}
379-
380-
[_mainOpenGLContext makeCurrentContext];
381-
382-
_macOSGLCompositor = std::make_unique<flutter::FlutterGLCompositor>(_viewController);
383-
384-
_compositor = {};
385-
_compositor.struct_size = sizeof(FlutterCompositor);
386-
_compositor.user_data = _macOSGLCompositor.get();
387-
388-
_compositor.create_backing_store_callback = [](const FlutterBackingStoreConfig* config, //
389-
FlutterBackingStore* backing_store_out, //
390-
void* user_data //
391-
) {
392-
return reinterpret_cast<flutter::FlutterGLCompositor*>(user_data)->CreateBackingStore(
393-
config, backing_store_out);
394-
};
395-
396-
_compositor.collect_backing_store_callback = [](const FlutterBackingStore* backing_store, //
397-
void* user_data //
398-
) {
399-
return reinterpret_cast<flutter::FlutterGLCompositor*>(user_data)->CollectBackingStore(
400-
backing_store);
401-
};
402-
403-
_compositor.present_layers_callback = [](const FlutterLayer** layers, //
404-
size_t layers_count, //
405-
void* user_data //
406-
) {
407-
return reinterpret_cast<flutter::FlutterGLCompositor*>(user_data)->Present(layers,
408-
layers_count);
409-
};
410-
411-
__weak FlutterEngine* weak_self = self;
412-
_macOSGLCompositor->SetPresentCallback(
413-
[weak_self]() { return [weak_self engineCallbackOnPresent]; });
414-
415-
_compositor.avoid_backing_store_cache = true;
416-
417-
return &_compositor;
418-
}
419-
420383
- (id<FlutterBinaryMessenger>)binaryMessenger {
421384
// TODO(stuartmorgan): Switch to FlutterBinaryMessengerRelay to avoid plugins
422385
// keeping the engine alive.
@@ -493,6 +456,10 @@ - (void)sendPointerEvent:(const FlutterPointerEvent&)event {
493456
_embedderAPI.SendPointerEvent(_engine, &event, 1);
494457
}
495458

459+
- (FlutterPlatformViewController*)platformViewController {
460+
return _platformViewController;
461+
}
462+
496463
#pragma mark - Private methods
497464

498465
- (void)sendUserLocales {
@@ -600,6 +567,71 @@ - (void)shutDownEngine {
600567
_engine = nullptr;
601568
}
602569

570+
- (FlutterCompositor*)createFlutterCompositor {
571+
// TODO(richardjcai): Add support for creating a FlutterGLCompositor
572+
// with a nil _viewController for headless engines.
573+
// https://github.com/flutter/flutter/issues/71606
574+
if (_viewController == nullptr) {
575+
return nullptr;
576+
}
577+
578+
[_mainOpenGLContext makeCurrentContext];
579+
580+
_macOSGLCompositor =
581+
std::make_unique<flutter::FlutterGLCompositor>(_viewController, _platformViewController);
582+
583+
_compositor = {};
584+
_compositor.struct_size = sizeof(FlutterCompositor);
585+
_compositor.user_data = _macOSGLCompositor.get();
586+
587+
_compositor.create_backing_store_callback = [](const FlutterBackingStoreConfig* config, //
588+
FlutterBackingStore* backing_store_out, //
589+
void* user_data //
590+
) {
591+
return reinterpret_cast<flutter::FlutterGLCompositor*>(user_data)->CreateBackingStore(
592+
config, backing_store_out);
593+
};
594+
595+
_compositor.collect_backing_store_callback = [](const FlutterBackingStore* backing_store, //
596+
void* user_data //
597+
) {
598+
return reinterpret_cast<flutter::FlutterGLCompositor*>(user_data)->CollectBackingStore(
599+
backing_store);
600+
};
601+
602+
_compositor.present_layers_callback = [](const FlutterLayer** layers, //
603+
size_t layers_count, //
604+
void* user_data //
605+
) {
606+
return reinterpret_cast<flutter::FlutterGLCompositor*>(user_data)->Present(layers,
607+
layers_count);
608+
};
609+
610+
__weak FlutterEngine* weak_self = self;
611+
_macOSGLCompositor->SetPresentCallback(
612+
[weak_self]() { return [weak_self engineCallbackOnPresent]; });
613+
614+
_compositor.avoid_backing_store_cache = true;
615+
616+
return &_compositor;
617+
}
618+
619+
- (void)createPlatformViewController {
620+
_platformViewController = [[FlutterPlatformViewController alloc] init];
621+
}
622+
623+
- (void)setupPlatformViewChannel {
624+
_platformViewsChannel =
625+
[FlutterMethodChannel methodChannelWithName:@"flutter/platform_views"
626+
binaryMessenger:self.binaryMessenger
627+
codec:[FlutterStandardMethodCodec sharedInstance]];
628+
629+
__weak FlutterEngine* weak_self = self;
630+
[_platformViewsChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
631+
[[weak_self platformViewController] handleMethodCall:call result:result];
632+
}];
633+
}
634+
603635
#pragma mark - FlutterBinaryMessenger
604636

605637
- (void)sendOnChannel:(nonnull NSString*)channel message:(nullable NSData*)message {

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

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

77
#include "flutter/fml/macros.h"
88
#include "flutter/shell/platform/darwin/macos/framework/Source/FlutterBackingStoreData.h"
9+
#include "flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewController_Internal.h"
910
#include "flutter/shell/platform/darwin/macos/framework/Source/FlutterSurfaceManager.h"
1011
#include "flutter/shell/platform/darwin/macos/framework/Source/FlutterViewController_Internal.h"
1112
#include "flutter/shell/platform/embedder/embedder.h"
@@ -19,7 +20,8 @@ namespace flutter {
1920
// FlutterGLCompositor is created and destroyed by FlutterEngine.
2021
class FlutterGLCompositor {
2122
public:
22-
FlutterGLCompositor(FlutterViewController* view_controller);
23+
FlutterGLCompositor(FlutterViewController* view_controller,
24+
FlutterPlatformViewController* platform_view_controller);
2325

2426
// Creates a BackingStore and saves updates the backing_store_out
2527
// data with the new BackingStore data.
@@ -50,6 +52,7 @@ class FlutterGLCompositor {
5052

5153
private:
5254
const FlutterViewController* view_controller_;
55+
const FlutterPlatformViewController* platform_view_controller_;
5356
const NSOpenGLContext* open_gl_context_;
5457
PresentCallback present_callback_;
5558

@@ -75,9 +78,6 @@ class FlutterGLCompositor {
7578
// Set frame_started_ to true and reset all layer state.
7679
void StartFrame();
7780

78-
// Remove platform views that are specified for deletion.
79-
void DisposePlatformViews();
80-
8181
// Creates a CALayer and adds it to ca_layer_map_ and increments
8282
// ca_layer_count_; Returns the key value (size_t) for the layer in
8383
// ca_layer_map_.

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

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@
1818

1919
namespace flutter {
2020

21-
FlutterGLCompositor::FlutterGLCompositor(FlutterViewController* view_controller)
21+
FlutterGLCompositor::FlutterGLCompositor(FlutterViewController* view_controller,
22+
FlutterPlatformViewController* platform_view_controller)
2223
: open_gl_context_(view_controller.flutterView.openGLContext) {
2324
FML_CHECK(view_controller != nullptr) << "FlutterViewController* cannot be nullptr";
25+
FML_CHECK(platform_view_controller != nullptr)
26+
<< "FlutterPlatformViewController* cannot be nullptr";
27+
2428
view_controller_ = view_controller;
29+
platform_view_controller_ = platform_view_controller;
2530
}
2631

2732
bool FlutterGLCompositor::CreateBackingStore(const FlutterBackingStoreConfig* config,
@@ -71,7 +76,7 @@
7176
}
7277

7378
bool FlutterGLCompositor::Present(const FlutterLayer** layers, size_t layers_count) {
74-
DisposePlatformViews();
79+
[platform_view_controller_ disposePlatformViews];
7580
for (size_t i = 0; i < layers_count; ++i) {
7681
const auto* layer = layers[i];
7782
FlutterBackingStore* backing_store = const_cast<FlutterBackingStore*>(layer->backing_store);
@@ -85,9 +90,15 @@
8590
break;
8691
}
8792
case kFlutterLayerContentTypePlatformView:
88-
FML_CHECK([[NSThread currentThread] isMainThread])
93+
FML_DCHECK([[NSThread currentThread] isMainThread])
8994
<< "Must be on the main thread to handle presenting platform views";
90-
NSView* platform_view = view_controller_.platformViews[layer->platform_view->identifier];
95+
96+
FML_DCHECK(platform_view_controller_.platformViews.count(layer->platform_view->identifier))
97+
<< "Platform view not found for id: " << layer->platform_view->identifier;
98+
99+
NSView* platform_view =
100+
platform_view_controller_.platformViews[layer->platform_view->identifier];
101+
91102
CGFloat scale = [[NSScreen mainScreen] backingScaleFactor];
92103
platform_view.frame = CGRectMake(layer->offset.x / scale, layer->offset.y / scale,
93104
layer->size.width / scale, layer->size.height / scale);
@@ -108,15 +119,15 @@
108119
void FlutterGLCompositor::PresentBackingStoreContent(
109120
FlutterBackingStoreData* flutter_backing_store_data,
110121
size_t layer_position) {
111-
FML_CHECK([[NSThread currentThread] isMainThread])
122+
FML_DCHECK([[NSThread currentThread] isMainThread])
112123
<< "Must be on the main thread to update CALayer contents";
113124

114125
FlutterIOSurfaceHolder* io_surface_holder = [flutter_backing_store_data ioSurfaceHolder];
115126
size_t layer_id = [flutter_backing_store_data layerId];
116127

117128
CALayer* content_layer = ca_layer_map_[layer_id];
118129

119-
FML_CHECK(content_layer) << "Unable to find a content layer with layer id " << layer_id;
130+
FML_DCHECK(content_layer) << "Unable to find a content layer with layer id " << layer_id;
120131

121132
content_layer.frame = content_layer.superlayer.bounds;
122133
content_layer.zPosition = layer_position;
@@ -158,20 +169,4 @@
158169
return ca_layer_count_++;
159170
}
160171

161-
void FlutterGLCompositor::DisposePlatformViews() {
162-
auto views_to_dispose = view_controller_.platformViewsToDispose;
163-
if (views_to_dispose.empty()) {
164-
return;
165-
}
166-
167-
for (int64_t viewId : views_to_dispose) {
168-
FML_CHECK([[NSThread currentThread] isMainThread])
169-
<< "Must be on the main thread to handle disposing platform views";
170-
NSView* view = view_controller_.platformViews[viewId];
171-
[view removeFromSuperview];
172-
view_controller_.platformViews.erase(viewId);
173-
}
174-
views_to_dispose.clear();
175-
}
176-
177172
} // namespace flutter

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@
55
#import <Foundation/Foundation.h>
66

77
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterGLCompositor.h"
8+
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewController_Internal.h"
89
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterViewControllerTestUtils.h"
910
#import "flutter/testing/testing.h"
1011

1112
namespace flutter::testing {
1213

1314
TEST(FlutterGLCompositorTest, TestPresent) {
1415
id mockViewController = CreateMockViewController(nil);
16+
FlutterPlatformViewController* platformViewController =
17+
[[FlutterPlatformViewController alloc] init];
1518

1619
std::unique_ptr<flutter::FlutterGLCompositor> macos_compositor =
17-
std::make_unique<FlutterGLCompositor>(mockViewController);
20+
std::make_unique<FlutterGLCompositor>(mockViewController, platformViewController);
1821

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

0 commit comments

Comments
 (0)