Flutter Engine
The Flutter Engine
embedder_layers.cc
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#include "flutter/shell/platform/embedder/embedder_layers.h"
6
7#include <algorithm>
8
9namespace flutter {
10
12 double device_pixel_ratio,
13 SkMatrix root_surface_transformation,
14 uint64_t presentation_time)
15 : frame_size_(frame_size),
16 device_pixel_ratio_(device_pixel_ratio),
17 root_surface_transformation_(root_surface_transformation),
18 presentation_time_(presentation_time) {}
19
21
24 const std::vector<SkIRect>& paint_region_vec) {
25 FlutterLayer layer = {};
26
27 layer.struct_size = sizeof(FlutterLayer);
29 layer.backing_store = store;
30
31 const auto layer_bounds =
32 SkRect::MakeWH(frame_size_.width(), frame_size_.height());
33
34 const auto transformed_layer_bounds =
35 root_surface_transformation_.mapRect(layer_bounds);
36
37 layer.offset.x = transformed_layer_bounds.x();
38 layer.offset.y = transformed_layer_bounds.y();
39 layer.size.width = transformed_layer_bounds.width();
40 layer.size.height = transformed_layer_bounds.height();
41
42 auto paint_region_rects = std::make_unique<std::vector<FlutterRect>>();
43 paint_region_rects->reserve(paint_region_vec.size());
44
45 for (const auto& rect : paint_region_vec) {
46 auto transformed_rect =
47 root_surface_transformation_.mapRect(SkRect::Make(rect));
48 paint_region_rects->push_back(FlutterRect{
49 transformed_rect.x(),
50 transformed_rect.y(),
51 transformed_rect.right(),
52 transformed_rect.bottom(),
53 });
54 }
55
56 auto paint_region = std::make_unique<FlutterRegion>();
57 paint_region->struct_size = sizeof(FlutterRegion);
58 paint_region->rects = paint_region_rects->data();
59 paint_region->rects_count = paint_region_rects->size();
60 rects_referenced_.push_back(std::move(paint_region_rects));
61
62 auto present_info = std::make_unique<FlutterBackingStorePresentInfo>();
63 present_info->struct_size = sizeof(FlutterBackingStorePresentInfo);
64 present_info->paint_region = paint_region.get();
65 regions_referenced_.push_back(std::move(paint_region));
66 layer.backing_store_present_info = present_info.get();
67 layer.presentation_time = presentation_time_;
68
69 present_info_referenced_.push_back(std::move(present_info));
70 presented_layers_.push_back(layer);
71}
72
73static std::unique_ptr<FlutterPlatformViewMutation> ConvertMutation(
74 double opacity) {
75 FlutterPlatformViewMutation mutation = {};
77 mutation.opacity = opacity;
78 return std::make_unique<FlutterPlatformViewMutation>(mutation);
79}
80
81static std::unique_ptr<FlutterPlatformViewMutation> ConvertMutation(
82 const SkRect& rect) {
83 FlutterPlatformViewMutation mutation = {};
85 mutation.clip_rect.left = rect.left();
86 mutation.clip_rect.top = rect.top();
87 mutation.clip_rect.right = rect.right();
88 mutation.clip_rect.bottom = rect.bottom();
89 return std::make_unique<FlutterPlatformViewMutation>(mutation);
90}
91
92static FlutterSize VectorToSize(const SkVector& vector) {
93 FlutterSize size = {};
94 size.width = vector.x();
95 size.height = vector.y();
96 return size;
97}
98
99static std::unique_ptr<FlutterPlatformViewMutation> ConvertMutation(
100 const SkRRect& rrect) {
101 FlutterPlatformViewMutation mutation = {};
103 const auto& rect = rrect.rect();
104 mutation.clip_rounded_rect.rect.left = rect.left();
105 mutation.clip_rounded_rect.rect.top = rect.top();
106 mutation.clip_rounded_rect.rect.right = rect.right();
107 mutation.clip_rounded_rect.rect.bottom = rect.bottom();
109 VectorToSize(rrect.radii(SkRRect::Corner::kUpperLeft_Corner));
111 VectorToSize(rrect.radii(SkRRect::Corner::kUpperRight_Corner));
113 VectorToSize(rrect.radii(SkRRect::Corner::kLowerRight_Corner));
115 VectorToSize(rrect.radii(SkRRect::Corner::kLowerLeft_Corner));
116 return std::make_unique<FlutterPlatformViewMutation>(mutation);
117}
118
119static std::unique_ptr<FlutterPlatformViewMutation> ConvertMutation(
120 const SkMatrix& matrix) {
121 FlutterPlatformViewMutation mutation = {};
132 return std::make_unique<FlutterPlatformViewMutation>(mutation);
133}
134
137 const EmbeddedViewParams& params) {
138 {
139 FlutterPlatformView view = {};
140 view.struct_size = sizeof(FlutterPlatformView);
141 view.identifier = identifier;
142
143 const auto& mutators = params.mutatorsStack();
144
145 std::vector<const FlutterPlatformViewMutation*> mutations_array;
146
147 for (auto i = mutators.Bottom(); i != mutators.Top(); ++i) {
148 const auto& mutator = *i;
149 switch (mutator->GetType()) {
151 mutations_array.push_back(
152 mutations_referenced_
153 .emplace_back(ConvertMutation(mutator->GetRect()))
154 .get());
155 } break;
157 mutations_array.push_back(
158 mutations_referenced_
159 .emplace_back(ConvertMutation(mutator->GetRRect()))
160 .get());
161 } break;
162 case MutatorType::kClipPath: {
163 // Unsupported mutation.
164 } break;
165 case MutatorType::kTransform: {
166 const auto& matrix = mutator->GetMatrix();
167 if (!matrix.isIdentity()) {
168 mutations_array.push_back(
169 mutations_referenced_.emplace_back(ConvertMutation(matrix))
170 .get());
171 }
172 } break;
173 case MutatorType::kOpacity: {
174 const double opacity =
175 std::clamp(mutator->GetAlphaFloat(), 0.0f, 1.0f);
176 if (opacity < 1.0) {
177 mutations_array.push_back(
178 mutations_referenced_.emplace_back(ConvertMutation(opacity))
179 .get());
180 }
181 } break;
183 break;
184 }
185 }
186
187 if (!mutations_array.empty()) {
188 // If there are going to be any mutations, they must first take into
189 // account the root surface transformation.
190 if (!root_surface_transformation_.isIdentity()) {
191 mutations_array.push_back(
192 mutations_referenced_
193 .emplace_back(ConvertMutation(root_surface_transformation_))
194 .get());
195 }
196
197 auto mutations =
198 std::make_unique<std::vector<const FlutterPlatformViewMutation*>>(
199 mutations_array.rbegin(), mutations_array.rend());
200 mutations_arrays_referenced_.emplace_back(std::move(mutations));
201
202 view.mutations_count = mutations_array.size();
203 view.mutations = mutations_arrays_referenced_.back().get()->data();
204 }
205
206 platform_views_referenced_.emplace_back(
207 std::make_unique<FlutterPlatformView>(view));
208 }
209
210 FlutterLayer layer = {};
211
212 layer.struct_size = sizeof(FlutterLayer);
214 layer.platform_view = platform_views_referenced_.back().get();
215
216 const auto layer_bounds =
217 SkRect::MakeXYWH(params.finalBoundingRect().x(), //
218 params.finalBoundingRect().y(), //
219 params.sizePoints().width() * device_pixel_ratio_, //
220 params.sizePoints().height() * device_pixel_ratio_ //
221 );
222
223 const auto transformed_layer_bounds =
224 root_surface_transformation_.mapRect(layer_bounds);
225
226 layer.offset.x = transformed_layer_bounds.x();
227 layer.offset.y = transformed_layer_bounds.y();
228 layer.size.width = transformed_layer_bounds.width();
229 layer.size.height = transformed_layer_bounds.height();
230
231 layer.presentation_time = presentation_time_;
232
233 presented_layers_.push_back(layer);
234}
235
237 FlutterViewId view_id,
238 const PresentCallback& callback) const {
239 std::vector<const FlutterLayer*> presented_layers_pointers;
240 presented_layers_pointers.reserve(presented_layers_.size());
241 for (const auto& layer : presented_layers_) {
242 presented_layers_pointers.push_back(&layer);
243 }
244 callback(view_id, presented_layers_pointers);
245}
246
247} // namespace flutter
static unsigned clamp(SkFixed fx, int max)
SI void store(P *ptr, const T &val)
static constexpr int kMScaleX
horizontal scale factor
Definition: SkMatrix.h:353
static constexpr int kMTransY
vertical translation
Definition: SkMatrix.h:358
static constexpr int kMPersp1
input y perspective factor
Definition: SkMatrix.h:360
static constexpr int kMPersp0
input x perspective factor
Definition: SkMatrix.h:359
static constexpr int kMPersp2
perspective bias
Definition: SkMatrix.h:361
static constexpr int kMTransX
horizontal translation
Definition: SkMatrix.h:355
static constexpr int kMSkewY
vertical skew factor
Definition: SkMatrix.h:356
static constexpr int kMScaleY
vertical scale factor
Definition: SkMatrix.h:357
static constexpr int kMSkewX
horizontal skew factor
Definition: SkMatrix.h:354
bool mapRect(SkRect *dst, const SkRect &src, SkApplyPerspectiveClip pc=SkApplyPerspectiveClip::kYes) const
Definition: SkMatrix.cpp:1141
bool isIdentity() const
Definition: SkMatrix.h:223
const SkRect & rect() const
Definition: SkRRect.h:264
SkVector radii(Corner corner) const
Definition: SkRRect.h:271
void PushPlatformViewLayer(FlutterPlatformViewIdentifier identifier, const EmbeddedViewParams &params)
void PushBackingStoreLayer(const FlutterBackingStore *store, const std::vector< SkIRect > &drawn_region)
EmbedderLayers(SkISize frame_size, double device_pixel_ratio, SkMatrix root_surface_transformation, uint64_t presentation_time)
void InvokePresentCallback(FlutterViewId view_id, const PresentCallback &callback) const
std::function< bool(FlutterViewId view_id, const std::vector< const FlutterLayer * > &layers)> PresentCallback
static SkString identifier(const FontFamilyDesc &family, const FontDesc &font)
int64_t FlutterPlatformViewIdentifier
Definition: embedder.h:1188
@ kFlutterLayerContentTypePlatformView
Indicates that the contents of this layer are determined by the embedder.
Definition: embedder.h:1795
@ kFlutterLayerContentTypeBackingStore
Definition: embedder.h:1793
@ kFlutterPlatformViewMutationTypeClipRoundedRect
Definition: embedder.h:1697
@ kFlutterPlatformViewMutationTypeClipRect
Definition: embedder.h:1694
@ kFlutterPlatformViewMutationTypeTransformation
Definition: embedder.h:1700
@ kFlutterPlatformViewMutationTypeOpacity
Definition: embedder.h:1691
const EmbeddedViewParams * params
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
unsigned useCenter Optional< SkMatrix > matrix
Definition: SkRecords.h:258
SkRRect rrect
Definition: SkRecords.h:232
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
@ kBackdropFilter
int64_t FlutterViewId
Definition: flutter_view.h:13
static FlutterSize VectorToSize(const SkVector &vector)
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
static std::unique_ptr< FlutterPlatformViewMutation > ConvertMutation(double opacity)
const myers::Point & get(const myers::Segment &)
FlutterPoint offset
Definition: embedder.h:1835
FlutterLayerContentType type
Definition: embedder.h:1824
const FlutterBackingStore * backing_store
Definition: embedder.h:1828
FlutterBackingStorePresentInfo * backing_store_present_info
Definition: embedder.h:1841
uint64_t presentation_time
Definition: embedder.h:1845
const FlutterPlatformView * platform_view
Definition: embedder.h:1831
size_t struct_size
This size of this struct. Must be sizeof(FlutterLayer).
Definition: embedder.h:1821
FlutterSize size
The size of the layer (in physical pixels).
Definition: embedder.h:1837
FlutterTransformation transformation
Definition: embedder.h:1710
FlutterPlatformViewMutationType type
The type of the mutation described by the subsequent union.
Definition: embedder.h:1705
FlutterRoundedRect clip_rounded_rect
Definition: embedder.h:1709
size_t struct_size
The size of this struct. Must be sizeof(FlutterPlatformView).
Definition: embedder.h:1716
const FlutterPlatformViewMutation ** mutations
Definition: embedder.h:1736
FlutterPlatformViewIdentifier identifier
Definition: embedder.h:1720
size_t mutations_count
Definition: embedder.h:1723
double y
Definition: embedder.h:447
double x
Definition: embedder.h:446
A structure to represent a rectangle.
Definition: embedder.h:437
double bottom
Definition: embedder.h:441
double top
Definition: embedder.h:439
double left
Definition: embedder.h:438
double right
Definition: embedder.h:440
A region represented by a collection of non-overlapping rectangles.
Definition: embedder.h:1799
FlutterRect rect
Definition: embedder.h:452
FlutterSize upper_left_corner_radius
Definition: embedder.h:453
FlutterSize lower_left_corner_radius
Definition: embedder.h:456
FlutterSize upper_right_corner_radius
Definition: embedder.h:454
FlutterSize lower_right_corner_radius
Definition: embedder.h:455
A structure to represent the width and height.
Definition: embedder.h:423
double height
Definition: embedder.h:425
double width
Definition: embedder.h:424
double transY
vertical translation
Definition: embedder.h:289
double pers2
perspective scale factor
Definition: embedder.h:295
double skewX
horizontal skew factor
Definition: embedder.h:281
double pers0
input x-axis perspective factor
Definition: embedder.h:291
double scaleX
horizontal scale factor
Definition: embedder.h:279
double skewY
vertical skew factor
Definition: embedder.h:285
double scaleY
vertical scale factor
Definition: embedder.h:287
double pers1
input y-axis perspective factor
Definition: embedder.h:293
double transX
horizontal translation
Definition: embedder.h:283
Definition: SkSize.h:16
constexpr int32_t width() const
Definition: SkSize.h:36
constexpr int32_t height() const
Definition: SkSize.h:37
constexpr float y() const
Definition: SkPoint_impl.h:187
constexpr float x() const
Definition: SkPoint_impl.h:181
static SkRect Make(const SkISize &size)
Definition: SkRect.h:669
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609