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

Commit d954488

Browse files
committed
Create FlutterPlatformViewController
1 parent 182b975 commit d954488

10 files changed

+260
-184
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: 79 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,11 @@ @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+
FlutterPlatformViewController* _platformViewController;
207213
}
208214

209215
- (instancetype)initWithName:(NSString*)labelPrefix project:(FlutterDartProject*)project {
@@ -313,6 +319,9 @@ - (BOOL)runWithEntrypoint:(NSString*)entrypoint {
313319
flutterArguments.aot_data = _aotData;
314320
}
315321

322+
[self setupPlatformViewChannel];
323+
[self createPlatformViewController];
324+
316325
flutterArguments.compositor = [self createFlutterCompositor];
317326

318327
FlutterEngineResult result = _embedderAPI.Initialize(
@@ -369,54 +378,6 @@ - (void)setViewController:(FlutterViewController*)controller {
369378
}
370379
}
371380

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-
420381
- (id<FlutterBinaryMessenger>)binaryMessenger {
421382
// TODO(stuartmorgan): Switch to FlutterBinaryMessengerRelay to avoid plugins
422383
// keeping the engine alive.
@@ -493,6 +454,10 @@ - (void)sendPointerEvent:(const FlutterPointerEvent&)event {
493454
_embedderAPI.SendPointerEvent(_engine, &event, 1);
494455
}
495456

457+
- (FlutterPlatformViewController*)platformViewController {
458+
return _platformViewController;
459+
}
460+
496461
#pragma mark - Private methods
497462

498463
- (void)sendUserLocales {
@@ -600,6 +565,72 @@ - (void)shutDownEngine {
600565
_engine = nullptr;
601566
}
602567

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

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

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

Lines changed: 6 additions & 2 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,7 +78,8 @@ 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.
81+
// Remove platform views whos ids are stored in platform_view_controller_'s
82+
// platformViewsToDispose set.
7983
void DisposePlatformViews();
8084

8185
// Creates a CALayer and adds it to ca_layer_map_ and increments

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)