| Flutter macOS Embedder |

FlutterCompositor.h
Go to the documentation of this file.
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 #ifndef FLUTTER_SHELL_PLATFORM_DARWIN_MACOS_FRAMEWORK_SOURCE_FLUTTERCOMPOSITOR_H_
6 #define FLUTTER_SHELL_PLATFORM_DARWIN_MACOS_FRAMEWORK_SOURCE_FLUTTERCOMPOSITOR_H_
7
8 #include <functional>
9 #include <list>
10 #include <unordered_map>
11 #include <variant>
12
13 #include "flutter/fml/macros.h"
14 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterMutatorView.h"
15 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewController.h"
16 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterTimeConverter.h"
17 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterViewProvider.h"
18 #include "flutter/shell/platform/embedder/embedder.h"
19
20 @class FlutterMutatorView;
21 @class FlutterCursorCoordinator;
22
23 namespace flutter {
24
25 struct BackingStoreLayer {
26 std::vector<FlutterRect> paint_region;
27 };
28
29 using LayerVariant = std::variant<PlatformViewLayer, BackingStoreLayer>;
30
31 // FlutterCompositor creates and manages the backing stores used for
32 // rendering Flutter content and presents Flutter content and Platform views.
33 // Platform views are not yet supported.
34 //
35 // TODO(cbracken): refactor for testability. https://github.com/flutter/flutter/issues/137648
36 class FlutterCompositor {
37 public:
38 // Create a FlutterCompositor with a view provider.
39 //
40 // The view_provider is used to query FlutterViews from view IDs,
41 // which are used for presenting and creating backing stores.
42 // It must not be null, and is typically FlutterViewEngineProvider.
43 FlutterCompositor(id<FlutterViewProvider> view_provider,
44 FlutterTimeConverter* time_converter,
45 FlutterPlatformViewController* platform_views_controller);
46
47 ~FlutterCompositor() = default;
48
49 // Allocate the resources for displaying a view.
50 //
51 // This method must be called when a view is added to FlutterEngine, and must be
52 // called on the main dispatch queue, or an assertion will be thrown.
53 void AddView(FlutterViewId view_id);
54
55 // Deallocate the resources for displaying a view.
56 //
57 // This method must be called when a view is removed from FlutterEngine, and
58 // must be called on the main dispatch queue, or an assertion will be thrown.
59 void RemoveView(FlutterViewId view_id);
60
61 // Creates a backing store and saves updates the backing_store_out data with
62 // the new FlutterBackingStore data.
63 //
64 // If the backing store is being requested for the first time for a given
65 // frame, this compositor does not create a new backing store but rather
66 // returns the backing store associated with the FlutterView's
67 // FlutterSurfaceManager.
68 //
69 // Any additional state allocated for the backing store and saved as
70 // user_data in the backing store must be collected in the backing_store's
71 // destruction_callback field which will be called when the embedder collects
72 // the backing store.
73 bool CreateBackingStore(const FlutterBackingStoreConfig* config,
74 FlutterBackingStore* backing_store_out);
75
76 // Presents the FlutterLayers by updating the FlutterView specified by
77 // `view_id` using the layer content.
78 bool Present(FlutterViewIdentifier view_id, const FlutterLayer** layers, size_t layers_count);
79
80 // The number of views that the FlutterCompositor is keeping track of.
81 //
82 // This method must only be used in unit tests.
83 size_t DebugNumViews();
84
85 private:
86 // A class that contains the information for a view to be presented.
87 class ViewPresenter {
88 public:
89 ViewPresenter();
90
91 void PresentPlatformViews(FlutterView* default_base_view,
92 const std::vector<LayerVariant>& layers,
93 const FlutterPlatformViewController* platform_views_controller);
94
95 private:
96 // Platform view to FlutterMutatorView that contains it.
97 NSMapTable<NSView*, FlutterMutatorView*>* mutator_views_;
98
99 // Coordinates mouse cursor changes between platform views and overlays.
100 FlutterCursorCoordinator* cursor_coordinator_;
101
102 // Presents the platform view layer represented by `layer`. `layer_index` is
103 // used to position the layer in the z-axis. If the layer does not have a
104 // superview, it will become subview of `default_base_view`.
105 FlutterMutatorView* PresentPlatformView(
106 FlutterView* default_base_view,
107 const PlatformViewLayer& layer,
108 size_t layer_position,
109 const FlutterPlatformViewController* platform_views_controller);
110
111 FML_DISALLOW_COPY_AND_ASSIGN(ViewPresenter);
112 };
113
114 // Where the compositor can query FlutterViews. Must not be null.
115 id<FlutterViewProvider> const view_provider_;
116
117 // Converts between engine time and core animation media time.
118 FlutterTimeConverter* const time_converter_;
119
120 // The controller used to manage creation and deletion of platform views.
121 const FlutterPlatformViewController* platform_view_controller_;
122
123 // The view presenters for views. Each key is a view ID.
124 std::unordered_map<FlutterViewId, ViewPresenter> presenters_;
125
126 FML_DISALLOW_COPY_AND_ASSIGN(FlutterCompositor);
127 };
128
129 } // namespace flutter
130
131 #endif // FLUTTER_SHELL_PLATFORM_DARWIN_MACOS_FRAMEWORK_SOURCE_FLUTTERCOMPOSITOR_H_
FlutterMutatorView.h
FlutterMutatorView
Definition: FlutterMutatorView.h:63
FlutterCursorCoordinator
Definition: FlutterMutatorView.h:45
flutter::FlutterCompositor::~FlutterCompositor
~FlutterCompositor()=default
flutter::BackingStoreLayer
Definition: FlutterCompositor.h:25
flutter::LayerVariant
std::variant< PlatformViewLayer, BackingStoreLayer > LayerVariant
Definition: FlutterCompositor.h:29
FlutterTimeConverter.h
flutter::FlutterCompositor::RemoveView
void RemoveView(FlutterViewId view_id)
Definition: FlutterCompositor.mm:49
FlutterPlatformViewController
Definition: FlutterPlatformViewController.h:17
FlutterPlatformViewController.h
FlutterViewProvider.h
flutter::FlutterCompositor
Definition: FlutterCompositor.h:36
flutter::FlutterCompositor::DebugNumViews
size_t DebugNumViews()
Definition: FlutterCompositor.mm:134
flutter::FlutterCompositor::FlutterCompositor
FlutterCompositor(id< FlutterViewProvider > view_provider, FlutterTimeConverter *time_converter, FlutterPlatformViewController *platform_views_controller)
Definition: FlutterCompositor.mm:35
flutter::FlutterCompositor::CreateBackingStore
bool CreateBackingStore(const FlutterBackingStoreConfig *config, FlutterBackingStore *backing_store_out)
Definition: FlutterCompositor.mm:54
flutter
Definition: AccessibilityBridgeMac.h:16
flutter::FlutterCompositor::Present
bool Present(FlutterViewIdentifier view_id, const FlutterLayer **layers, size_t layers_count)
Definition: FlutterCompositor.mm:71
flutter::BackingStoreLayer::paint_region
std::vector< FlutterRect > paint_region
Definition: FlutterCompositor.h:26
FlutterTimeConverter
Converts between the time representation used by Flutter Engine and CAMediaTime.
Definition: FlutterTimeConverter.h:13
FlutterView
Definition: FlutterView.h:35
flutter::FlutterCompositor::AddView
void AddView(FlutterViewId view_id)
Definition: FlutterCompositor.mm:44
flutter::PlatformViewLayer
Represents a platform view layer, including all mutations.
Definition: FlutterMutatorView.h:16
FlutterViewIdentifier
int64_t FlutterViewIdentifier
Definition: FlutterViewController.h:21