From 5e1cfd81a59d78eb3528fc0ca5fbe568dc7e47ae Mon Sep 17 00:00:00 2001 From: Tong Mu Date: Wed, 27 Mar 2024 17:30:01 -0700 Subject: [PATCH 1/3] Impl --- .../framework/Source/FlutterCompositor.h | 42 ++++++++++++------- .../framework/Source/FlutterCompositor.mm | 42 ++++++++++++------- 2 files changed, 54 insertions(+), 30 deletions(-) diff --git a/shell/platform/darwin/macos/framework/Source/FlutterCompositor.h b/shell/platform/darwin/macos/framework/Source/FlutterCompositor.h index aad527a3785df..3e5ff9a9304e1 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterCompositor.h +++ b/shell/platform/darwin/macos/framework/Source/FlutterCompositor.h @@ -7,6 +7,7 @@ #include #include +#include #include "flutter/fml/macros.h" #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewController.h" @@ -34,9 +35,9 @@ class FlutterCompositor { // The view_provider is used to query FlutterViews from view IDs, // which are used for presenting and creating backing stores. // It must not be null, and is typically FlutterViewEngineProvider. - explicit FlutterCompositor(id view_provider, - FlutterTimeConverter* time_converter, - FlutterPlatformViewController* platform_views_controller); + FlutterCompositor(id view_provider, + FlutterTimeConverter* time_converter, + FlutterPlatformViewController* platform_views_controller); ~FlutterCompositor() = default; @@ -56,19 +57,31 @@ class FlutterCompositor { FlutterBackingStore* backing_store_out); // Presents the FlutterLayers by updating the FlutterView specified by - // `view_id` using the layer content. Sets frame_started_ to false. + // `view_id` using the layer content. bool Present(FlutterViewId view_id, const FlutterLayer** layers, size_t layers_count); private: - void PresentPlatformViews(FlutterView* default_base_view, - const std::vector& platform_views_layers); - - // Presents the platform view layer represented by `layer`. `layer_index` is - // used to position the layer in the z-axis. If the layer does not have a - // superview, it will become subview of `default_base_view`. - FlutterMutatorView* PresentPlatformView(FlutterView* default_base_view, - const PlatformViewLayer& layer, - size_t index); + // A class that contains the information for a view to be presented. + class ViewPresenter { + public: + ViewPresenter(); + + void PresentPlatformViews(FlutterView* default_base_view, + const std::vector& platform_views, + const FlutterPlatformViewController* platform_views_controller); + + private: + // Platform view to FlutterMutatorView that contains it. + NSMapTable* mutator_views_; + + // Presents the platform view layer represented by `layer`. `layer_index` is + // used to position the layer in the z-axis. If the layer does not have a + // superview, it will become subview of `default_base_view`. + FlutterMutatorView* PresentPlatformView(FlutterView* default_base_view, + const PlatformViewLayer& layer, + size_t layer_position, + const FlutterPlatformViewController* platform_views_controller); + }; // Where the compositor can query FlutterViews. Must not be null. id const view_provider_; @@ -79,8 +92,7 @@ class FlutterCompositor { // The controller used to manage creation and deletion of platform views. const FlutterPlatformViewController* platform_view_controller_; - // Platform view to FlutterMutatorView that contains it. - NSMapTable* mutator_views_; + std::unordered_map presenters_; FML_DISALLOW_COPY_AND_ASSIGN(FlutterCompositor); }; diff --git a/shell/platform/darwin/macos/framework/Source/FlutterCompositor.mm b/shell/platform/darwin/macos/framework/Source/FlutterCompositor.mm index 890a4c0cfca3b..0dabc19e37d9b 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterCompositor.mm +++ b/shell/platform/darwin/macos/framework/Source/FlutterCompositor.mm @@ -27,8 +27,7 @@ FlutterPlatformViewController* platform_view_controller) : view_provider_(view_provider), time_converter_(time_converter), - platform_view_controller_(platform_view_controller), - mutator_views_([NSMapTable strongToStrongObjectsMapTable]) { + platform_view_controller_(platform_view_controller) { FML_CHECK(view_provider != nullptr) << "view_provider cannot be nullptr"; } @@ -55,6 +54,10 @@ bool FlutterCompositor::Present(FlutterViewId view_id, const FlutterLayer** layers, size_t layers_count) { + // TODO(dkwingsmt): The macOS embedder only supports rendering to the implicit + // view for now. As it supports adding more views, this assertion should be + // lifted. https://github.com/flutter/flutter/issues/142845 + FML_DCHECK(view_id == kFlutterImplicitViewId); FlutterView* view = [view_provider_ viewForId:view_id]; if (!view) { return false; @@ -95,18 +98,25 @@ auto platform_views_layers = std::make_shared>( CopyPlatformViewLayers(layers, layers_count)); - [view.surfaceManager presentSurfaces:surfaces - atTime:presentation_time - notify:^{ - PresentPlatformViews(view, *platform_views_layers); - }]; + [view.surfaceManager + presentSurfaces:surfaces + atTime:presentation_time + notify:^{ + // Gets a presenter or create a new one for the view. + ViewPresenter& presenter = presenters_[view_id]; + presenter.PresentPlatformViews(view, *platform_views_layers, platform_view_controller_); + }]; return true; } -void FlutterCompositor::PresentPlatformViews( +FlutterCompositor::ViewPresenter::ViewPresenter() : + mutator_views_([NSMapTable strongToStrongObjectsMapTable]) {} + +void FlutterCompositor::ViewPresenter::PresentPlatformViews( FlutterView* default_base_view, - const std::vector& platform_views) { + const std::vector& platform_views, + const FlutterPlatformViewController* platform_view_controller) { FML_DCHECK([[NSThread currentThread] isMainThread]) << "Must be on the main thread to present platform views"; @@ -115,7 +125,7 @@ for (const auto& platform_view : platform_views) { [present_mutators addObject:PresentPlatformView(default_base_view, platform_view.first, - platform_view.second)]; + platform_view.second, platform_view_controller)]; } NSMutableArray* obsolete_mutators = @@ -127,17 +137,19 @@ [mutator removeFromSuperview]; } - [platform_view_controller_ disposePlatformViews]; + [platform_view_controller disposePlatformViews]; } -FlutterMutatorView* FlutterCompositor::PresentPlatformView(FlutterView* default_base_view, - const PlatformViewLayer& layer, - size_t index) { +FlutterMutatorView* FlutterCompositor::ViewPresenter::PresentPlatformView( + FlutterView* default_base_view, + const PlatformViewLayer& layer, + size_t index, + const FlutterPlatformViewController* platform_view_controller) { FML_DCHECK([[NSThread currentThread] isMainThread]) << "Must be on the main thread to present platform views"; int64_t platform_view_id = layer.identifier(); - NSView* platform_view = [platform_view_controller_ platformViewWithID:platform_view_id]; + NSView* platform_view = [platform_view_controller platformViewWithID:platform_view_id]; FML_DCHECK(platform_view) << "Platform view not found for id: " << platform_view_id; From b282f713261d6cd50a12625f452d3c6264edf029 Mon Sep 17 00:00:00 2001 From: Tong Mu Date: Wed, 27 Mar 2024 17:30:23 -0700 Subject: [PATCH 2/3] Format --- .../framework/Source/FlutterCompositor.h | 9 ++++--- .../framework/Source/FlutterCompositor.mm | 25 ++++++++++--------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/shell/platform/darwin/macos/framework/Source/FlutterCompositor.h b/shell/platform/darwin/macos/framework/Source/FlutterCompositor.h index 3e5ff9a9304e1..ebf91071d7c24 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterCompositor.h +++ b/shell/platform/darwin/macos/framework/Source/FlutterCompositor.h @@ -77,10 +77,11 @@ class FlutterCompositor { // Presents the platform view layer represented by `layer`. `layer_index` is // used to position the layer in the z-axis. If the layer does not have a // superview, it will become subview of `default_base_view`. - FlutterMutatorView* PresentPlatformView(FlutterView* default_base_view, - const PlatformViewLayer& layer, - size_t layer_position, - const FlutterPlatformViewController* platform_views_controller); + FlutterMutatorView* PresentPlatformView( + FlutterView* default_base_view, + const PlatformViewLayer& layer, + size_t layer_position, + const FlutterPlatformViewController* platform_views_controller); }; // Where the compositor can query FlutterViews. Must not be null. diff --git a/shell/platform/darwin/macos/framework/Source/FlutterCompositor.mm b/shell/platform/darwin/macos/framework/Source/FlutterCompositor.mm index 0dabc19e37d9b..e2ea4f5b279df 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterCompositor.mm +++ b/shell/platform/darwin/macos/framework/Source/FlutterCompositor.mm @@ -98,20 +98,20 @@ auto platform_views_layers = std::make_shared>( CopyPlatformViewLayers(layers, layers_count)); - [view.surfaceManager - presentSurfaces:surfaces - atTime:presentation_time - notify:^{ - // Gets a presenter or create a new one for the view. - ViewPresenter& presenter = presenters_[view_id]; - presenter.PresentPlatformViews(view, *platform_views_layers, platform_view_controller_); - }]; + [view.surfaceManager presentSurfaces:surfaces + atTime:presentation_time + notify:^{ + // Gets a presenter or create a new one for the view. + ViewPresenter& presenter = presenters_[view_id]; + presenter.PresentPlatformViews(view, *platform_views_layers, + platform_view_controller_); + }]; return true; } -FlutterCompositor::ViewPresenter::ViewPresenter() : - mutator_views_([NSMapTable strongToStrongObjectsMapTable]) {} +FlutterCompositor::ViewPresenter::ViewPresenter() + : mutator_views_([NSMapTable strongToStrongObjectsMapTable]) {} void FlutterCompositor::ViewPresenter::PresentPlatformViews( FlutterView* default_base_view, @@ -124,8 +124,9 @@ NSMutableArray* present_mutators = [NSMutableArray array]; for (const auto& platform_view : platform_views) { - [present_mutators addObject:PresentPlatformView(default_base_view, platform_view.first, - platform_view.second, platform_view_controller)]; + [present_mutators + addObject:PresentPlatformView(default_base_view, platform_view.first, platform_view.second, + platform_view_controller)]; } NSMutableArray* obsolete_mutators = From b557b298ae0048bd87bd59a24c3812ddc1091382 Mon Sep 17 00:00:00 2001 From: Tong Mu Date: Thu, 28 Mar 2024 15:57:35 -0700 Subject: [PATCH 3/3] Comments --- .../darwin/macos/framework/Source/FlutterCompositor.h | 2 ++ .../darwin/macos/framework/Source/FlutterCompositor.mm | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/shell/platform/darwin/macos/framework/Source/FlutterCompositor.h b/shell/platform/darwin/macos/framework/Source/FlutterCompositor.h index ebf91071d7c24..8d2e5c6b33ed8 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterCompositor.h +++ b/shell/platform/darwin/macos/framework/Source/FlutterCompositor.h @@ -82,6 +82,8 @@ class FlutterCompositor { const PlatformViewLayer& layer, size_t layer_position, const FlutterPlatformViewController* platform_views_controller); + + FML_DISALLOW_COPY_AND_ASSIGN(ViewPresenter); }; // Where the compositor can query FlutterViews. Must not be null. diff --git a/shell/platform/darwin/macos/framework/Source/FlutterCompositor.mm b/shell/platform/darwin/macos/framework/Source/FlutterCompositor.mm index e2ea4f5b279df..e7749ca0e539b 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterCompositor.mm +++ b/shell/platform/darwin/macos/framework/Source/FlutterCompositor.mm @@ -124,9 +124,9 @@ NSMutableArray* present_mutators = [NSMutableArray array]; for (const auto& platform_view : platform_views) { - [present_mutators - addObject:PresentPlatformView(default_base_view, platform_view.first, platform_view.second, - platform_view_controller)]; + FlutterMutatorView* container = PresentPlatformView( + default_base_view, platform_view.first, platform_view.second, platform_view_controller); + [present_mutators addObject:container]; } NSMutableArray* obsolete_mutators =