Flutter Engine
 
Loading...
Searching...
No Matches
external_view_embedder_2.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_ANDROID_EXTERNAL_VIEW_EMBEDDER_EXTERNAL_VIEW_EMBEDDER_2_H_
6#define FLUTTER_SHELL_PLATFORM_ANDROID_EXTERNAL_VIEW_EMBEDDER_EXTERNAL_VIEW_EMBEDDER_2_H_
7
8#include <atomic>
9#include <unordered_map>
10
17#include "third_party/abseil-cpp/absl/container/flat_hash_set.h"
18
19namespace flutter {
20
21//------------------------------------------------------------------------------
22/// Allows to embed Android views into a Flutter application.
23///
24/// This class calls Java methods via |PlatformViewAndroidJNI| to manage the
25/// lifecycle of the Android view corresponding to |flutter::PlatformViewLayer|.
26///
27/// It also orchestrates overlay surfaces. These are Android views
28/// that render above (by Z order) the Android view corresponding to
29/// |flutter::PlatformViewLayer|.
30///
31/// This implementation of the external view embedder is designed only to use
32/// HC++ mode. Mixing old HC modes is not supported, but either of the texture
33/// composition based platform views can be used with either mode.
35 public:
37 const AndroidContext& android_context,
38 std::shared_ptr<PlatformViewAndroidJNI> jni_facade,
39 std::shared_ptr<AndroidSurfaceFactory> surface_factory,
40 const TaskRunners& task_runners);
41
42 // |ExternalViewEmbedder|
44 int64_t view_id,
45 std::unique_ptr<flutter::EmbeddedViewParams> params) override;
46
47 // |ExternalViewEmbedder|
48 DlCanvas* CompositeEmbeddedView(int64_t view_id) override;
49
50 // |ExternalViewEmbedder|
52 int64_t flutter_view_id,
53 GrDirectContext* context,
54 const std::shared_ptr<impeller::AiksContext>& aiks_context,
55 std::unique_ptr<SurfaceFrame> frame) override;
56
57 // |ExternalViewEmbedder|
59 const fml::RefPtr<fml::RasterThreadMerger>& raster_thread_merger)
60 override;
61
62 // |ExternalViewEmbedder|
63 DlCanvas* GetRootCanvas() override;
64
65 // |ExternalViewEmbedder|
66 void BeginFrame(GrDirectContext* context,
68 raster_thread_merger) override;
69
70 // |ExternalViewEmbedder|
71 void PrepareFlutterView(DlISize frame_size,
72 double device_pixel_ratio) override;
73
74 // |ExternalViewEmbedder|
75 void CancelFrame() override;
76
77 // |ExternalViewEmbedder|
78 void EndFrame(bool should_resubmit_frame,
80 raster_thread_merger) override;
81
82 // |ExternalViewEmbedder|
83 bool SupportsDynamicThreadMerging() override;
84
85 // |ExternalViewEmbedder|
86 void Teardown() override;
87
88 // Gets the rect based on the device pixel ratio of a platform view displayed
89 // on the screen.
90 static DlRect GetViewRect(
91 int64_t view_id,
92 const std::unordered_map<int64_t, EmbeddedViewParams>& view_params);
93
94 private:
95 // The number of frames the rasterizer task runner will continue
96 // to run on the platform thread after no platform view is rendered.
97 //
98 // Note: this is an arbitrary number that attempts to account for cases
99 // where the platform view might be momentarily off the screen.
100 static const int kDefaultMergedLeaseDuration = 10;
101
102 // Provides metadata to the Android surfaces.
103 const AndroidContext& android_context_;
104
105 // Allows to call methods in Java.
106 const std::shared_ptr<PlatformViewAndroidJNI> jni_facade_;
107
108 // Allows to create surfaces.
109 const std::shared_ptr<AndroidSurfaceFactory> surface_factory_;
110
111 // Holds surfaces. Allows to recycle surfaces or allocate new ones.
112 const std::unique_ptr<SurfacePool> surface_pool_;
113
114 // The task runners.
115 const TaskRunners task_runners_;
116
117 // If the overlay layer is currently shown.
118 std::atomic_bool overlay_layer_is_shown_{false};
119
120 // The size of the root canvas.
121 DlISize frame_size_;
122
123 // The pixel ratio used to determinate the size of a platform view layer
124 // relative to the device layout system.
125 double device_pixel_ratio_;
126
127 // The order of composition. Each entry contains a unique id for the platform
128 // view.
129 std::vector<int64_t> composition_order_;
130
131 // The |EmbedderViewSlice| implementation keyed off the platform view id,
132 // which contains any subsequent operations until the next platform view or
133 // the end of the last leaf node in the layer tree.
134 std::unordered_map<int64_t, std::unique_ptr<EmbedderViewSlice>> slices_;
135
136 // The params for a platform view, which contains the size, position and
137 // mutation stack.
138 std::unordered_map<int64_t, EmbeddedViewParams> view_params_;
139
140 // The set of platform views that were visible in the last frame.
141 absl::flat_hash_set<int64_t> views_visible_last_frame_;
142
143 // Destroys the surfaces created from the surface factory.
144 // This method schedules a task on the platform thread, and waits for
145 // the task until it completes.
146 void DestroySurfaces();
147
148 // Resets the state.
149 void Reset();
150
151 // Whether the layer tree in the current frame has platform layers.
152 bool FrameHasPlatformLayers();
153
154 // Shows the overlay layer if it has content and the previous frame did not.
155 void ShowOverlayLayerIfNeeded();
156
157 // Hides the overlay layer if it does not have content and the previous
158 // frame did have content.
159 void HideOverlayLayerIfNeeded();
160};
161
162} // namespace flutter
163
164#endif // FLUTTER_SHELL_PLATFORM_ANDROID_EXTERNAL_VIEW_EMBEDDER_EXTERNAL_VIEW_EMBEDDER_2_H_
Holds state that is shared across Android surfaces.
DlCanvas * CompositeEmbeddedView(int64_t view_id) override
void SubmitFlutterView(int64_t flutter_view_id, GrDirectContext *context, const std::shared_ptr< impeller::AiksContext > &aiks_context, std::unique_ptr< SurfaceFrame > frame) override
PostPrerollResult PostPrerollAction(const fml::RefPtr< fml::RasterThreadMerger > &raster_thread_merger) override
void EndFrame(bool should_resubmit_frame, const fml::RefPtr< fml::RasterThreadMerger > &raster_thread_merger) override
static DlRect GetViewRect(int64_t view_id, const std::unordered_map< int64_t, EmbeddedViewParams > &view_params)
void PrerollCompositeEmbeddedView(int64_t view_id, std::unique_ptr< flutter::EmbeddedViewParams > params) override
void BeginFrame(GrDirectContext *context, const fml::RefPtr< fml::RasterThreadMerger > &raster_thread_merger) override
void PrepareFlutterView(DlISize frame_size, double device_pixel_ratio) override
Developer-facing API for rendering anything within the engine.
Definition dl_canvas.h:32
const EmbeddedViewParams * params
G_BEGIN_DECLS FlutterViewId view_id