Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
external_view_embedder.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_FUCHSIA_FLUTTER_EXTERNAL_VIEW_EMBEDDER_H_
6#define FLUTTER_SHELL_PLATFORM_FUCHSIA_FLUTTER_EXTERNAL_VIEW_EMBEDDER_H_
7
8#include <fuchsia/ui/composition/cpp/fidl.h>
9#include <fuchsia/ui/views/cpp/fidl.h>
10#include <lib/fit/function.h>
11
12#include <cstdint> // For uint32_t & uint64_t
13#include <memory>
14#include <string>
15#include <unordered_map>
16#include <vector>
17
18#include "flutter/flow/embedded_views.h"
19#include "flutter/fml/logging.h"
20#include "flutter/fml/macros.h"
21#include "flutter/shell/platform/fuchsia/flutter/canvas_spy.h"
22#include "flutter/shell/platform/fuchsia/flutter/rtree.h"
28
29#include "flatland_connection.h"
30#include "surface_producer.h"
31
32namespace flutter_runner {
33
34using ViewCallback = std::function<void()>;
35using ViewCreatedCallback = std::function<void(
36 fuchsia::ui::composition::ContentId,
37 fuchsia::ui::composition::ChildViewWatcherHandle child_view_watcher)>;
38using ViewIdCallback = std::function<void(fuchsia::ui::composition::ContentId)>;
39
40// This class orchestrates interaction with the Scenic's compositor on
41// Fuchsia. It ensures that flutter content and platform view content are both
42// rendered correctly in a unified scene.
44 public:
45 constexpr static uint32_t kDefaultViewportSize = 32;
46
48 fuchsia::ui::views::ViewCreationToken view_creation_token,
49 fuchsia::ui::views::ViewIdentityOnCreation view_identity,
50 fuchsia::ui::composition::ViewBoundProtocols endpoints,
51 fidl::InterfaceRequest<fuchsia::ui::composition::ParentViewportWatcher>
52 parent_viewport_watcher_request,
53 std::shared_ptr<FlatlandConnection> flatland,
54 std::shared_ptr<SurfaceProducer> surface_producer,
55 bool intercept_all_input = false);
57
58 // |ExternalViewEmbedder|
60
61 // |ExternalViewEmbedder|
63 int64_t view_id,
64 std::unique_ptr<flutter::EmbeddedViewParams> params) override;
65
66 // |ExternalViewEmbedder|
67 flutter::DlCanvas* CompositeEmbeddedView(int64_t view_id) override;
68
69 // |ExternalViewEmbedder|
71 const fml::RefPtr<fml::RasterThreadMerger>& raster_thread_merger)
72 override;
73
74 // |ExternalViewEmbedder|
75 void BeginFrame(GrDirectContext* context,
77 raster_thread_merger) override;
78
79 // |ExternalViewEmbedder|
80 void PrepareFlutterView(SkISize frame_size,
81 double device_pixel_ratio) override;
82
83 // |ExternalViewEmbedder|
84 void EndFrame(bool should_resubmit_frame,
86 raster_thread_merger) override;
87
88 // |ExternalViewEmbedder|
90 int64_t flutter_view_id,
91 GrDirectContext* context,
92 const std::shared_ptr<impeller::AiksContext>& aiks_context,
93 std::unique_ptr<flutter::SurfaceFrame> frame) override;
94
95 // |ExternalViewEmbedder|
96 void CancelFrame() override { Reset(); }
97
98 // |ExternalViewEmbedder|
99 bool SupportsDynamicThreadMerging() override { return false; }
100
101 // View manipulation.
102 // |SetViewProperties| doesn't manipulate the view directly -- it sets pending
103 // properties for the next |UpdateView| call.
104 void CreateView(int64_t view_id,
105 ViewCallback on_view_created,
106 ViewCreatedCallback on_view_bound);
107 void DestroyView(int64_t view_id, ViewIdCallback on_view_unbound);
108 void SetViewProperties(int64_t view_id,
109 const SkRect& occlusion_hint,
110 bool hit_testable,
111 bool focusable);
112
113 // Holds the clip transform that may be applied on a View.
115 fuchsia::ui::composition::TransformId transform_id;
116 std::vector<fuchsia::ui::composition::TransformId> children;
117 };
118
119 private:
120 void Reset(); // Reset state for a new frame.
121
122 // This struct represents a transformed clip rect.
123 struct TransformedClip {
124 SkMatrix transform = SkMatrix::I();
125 SkRect rect = SkRect::MakeEmpty();
126
127 bool operator==(const TransformedClip& other) const {
128 return transform == other.transform && rect == other.rect;
129 }
130 };
131
132 // This struct represents all the mutators that can be applied to a
133 // PlatformView, unpacked from the `MutatorStack`.
134 struct ViewMutators {
135 std::vector<TransformedClip> clips;
136 SkMatrix total_transform = SkMatrix::I();
137 SkMatrix transform = SkMatrix::I();
138 SkScalar opacity = 1.f;
139
140 bool operator==(const ViewMutators& other) const {
141 return clips == other.clips && total_transform == other.total_transform &&
142 transform == other.transform && opacity == other.opacity;
143 }
144 };
145
146 ViewMutators ParseMutatorStack(const flutter::MutatorsStack& mutators_stack);
147
148 struct EmbedderLayer {
149 EmbedderLayer(const SkISize& frame_size,
150 std::optional<flutter::EmbeddedViewParams> view_params,
151 flutter::RTreeFactory rtree_factory)
152 : rtree(rtree_factory.getInstance()),
153 embedded_view_params(std::move(view_params)),
154 recorder(std::make_unique<SkPictureRecorder>()),
155 canvas_spy(std::make_unique<flutter::CanvasSpy>(
156 recorder->beginRecording(SkRect::Make(frame_size),
157 &rtree_factory))),
158 surface_size(frame_size),
159 picture(nullptr) {}
160
161 // Records paint operations applied to this layer's `SkCanvas`.
162 // These records are used to determine which portions of this layer
163 // contain content. The embedder propagates this information to scenic, so
164 // that scenic can accurately decide which portions of this layer may
165 // interact with input.
167
168 std::optional<flutter::EmbeddedViewParams> embedded_view_params;
169 std::unique_ptr<SkPictureRecorder> recorder;
170 // TODO(cyanglaz: use DlOpSpy instead.
171 // https://github.com/flutter/flutter/issues/123805
172 std::unique_ptr<flutter::CanvasSpy> canvas_spy;
173 SkISize surface_size;
175 };
176 using EmbedderLayerId = std::optional<uint32_t>;
177 constexpr static EmbedderLayerId kRootLayerId = EmbedderLayerId{};
178
179 struct View {
180 std::vector<ClipTransform> clip_transforms;
181 fuchsia::ui::composition::TransformId transform_id;
182 fuchsia::ui::composition::ContentId viewport_id;
183 ViewMutators mutators;
185 SkRect pending_occlusion_hint = SkRect::MakeEmpty();
186 SkRect occlusion_hint = SkRect::MakeEmpty();
187 fit::callback<void(const SkSize&, const SkRect&)>
188 pending_create_viewport_callback;
189 };
190
191 struct Layer {
192 // Transform on which Images are set.
193 fuchsia::ui::composition::TransformId transform_id;
194 };
195
196 std::shared_ptr<FlatlandConnection> flatland_;
197 std::shared_ptr<SurfaceProducer> surface_producer_;
198
199 fuchsia::ui::composition::ParentViewportWatcherPtr parent_viewport_watcher_;
200
201 fuchsia::ui::composition::TransformId root_transform_id_;
202
203 std::unordered_map<int64_t, View> views_;
204 std::vector<Layer> layers_;
205
206 std::unordered_map<EmbedderLayerId, EmbedderLayer> frame_layers_;
207 std::vector<EmbedderLayerId> frame_composition_order_;
208 std::vector<fuchsia::ui::composition::TransformId> child_transforms_;
209 SkISize frame_size_ = SkISize::Make(0, 0);
210 float frame_dpr_ = 1.f;
211
212 // TransformId for the input interceptor node when input shield is turned on,
213 // std::nullptr otherwise.
214 std::optional<fuchsia::ui::composition::TransformId>
215 input_interceptor_transform_;
216
218};
219
220} // namespace flutter_runner
221
222#endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_FLUTTER_EXTERNAL_VIEW_EMBEDDER_H_
static std::unique_ptr< SkEncoder > Make(SkWStream *dst, const SkPixmap *src, const SkYUVAPixmaps *srcYUVA, const SkColorSpace *srcYUVAColorSpace, const SkJpegEncoder::Options &options)
static const SkMatrix & I()
Developer-facing API for rendering anything within the engine.
Definition dl_canvas.h:37
void EndFrame(bool should_resubmit_frame, const fml::RefPtr< fml::RasterThreadMerger > &raster_thread_merger) override
flutter::PostPrerollResult PostPrerollAction(const fml::RefPtr< fml::RasterThreadMerger > &raster_thread_merger) override
void PrepareFlutterView(SkISize frame_size, double device_pixel_ratio) override
static constexpr uint32_t kDefaultViewportSize
void DestroyView(int64_t view_id, ViewIdCallback on_view_unbound)
void CreateView(int64_t view_id, ViewCallback on_view_created, ViewCreatedCallback on_view_bound)
flutter::DlCanvas * CompositeEmbeddedView(int64_t view_id) override
flutter::DlCanvas * GetRootCanvas() override
void SubmitFlutterView(int64_t flutter_view_id, GrDirectContext *context, const std::shared_ptr< impeller::AiksContext > &aiks_context, std::unique_ptr< flutter::SurfaceFrame > frame) override
void BeginFrame(GrDirectContext *context, const fml::RefPtr< fml::RasterThreadMerger > &raster_thread_merger) override
void SetViewProperties(int64_t view_id, const SkRect &occlusion_hint, bool hit_testable, bool focusable)
void PrerollCompositeEmbeddedView(int64_t view_id, std::unique_ptr< flutter::EmbeddedViewParams > params) override
const EmbeddedViewParams * params
double frame
Definition examples.cpp:31
float SkScalar
Definition extension.cpp:12
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition macros.h:27
sk_sp< const SkPicture > picture
Definition SkRecords.h:299
std::function< void()> ViewCallback
std::function< void(fuchsia::ui::composition::ContentId)> ViewIdCallback
std::function< void(fuchsia::ui::composition::ContentId, fuchsia::ui::composition::ChildViewWatcherHandle child_view_watcher)> ViewCreatedCallback
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition switches.h:259
Definition ref_ptr.h:256
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
static constexpr SkRect MakeEmpty()
Definition SkRect.h:595
static constexpr SkSize MakeEmpty()
Definition SkSize.h:62
std::vector< fuchsia::ui::composition::TransformId > children