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 
9 namespace flutter {
10 
11 EmbedderLayers::EmbedderLayers(SkISize frame_size,
12  double device_pixel_ratio,
13  SkMatrix root_surface_transformation)
14  : frame_size_(frame_size),
15  device_pixel_ratio_(device_pixel_ratio),
16  root_surface_transformation_(root_surface_transformation) {}
17 
19 
21  FlutterLayer layer = {};
22 
23  layer.struct_size = sizeof(FlutterLayer);
25  layer.backing_store = store;
26 
27  const auto layer_bounds =
28  SkRect::MakeWH(frame_size_.width(), frame_size_.height());
29 
30  const auto transformed_layer_bounds =
31  root_surface_transformation_.mapRect(layer_bounds);
32 
33  layer.offset.x = transformed_layer_bounds.x();
34  layer.offset.y = transformed_layer_bounds.y();
35  layer.size.width = transformed_layer_bounds.width();
36  layer.size.height = transformed_layer_bounds.height();
37 
38  presented_layers_.push_back(layer);
39 }
40 
41 static std::unique_ptr<FlutterPlatformViewMutation> ConvertMutation(
42  double opacity) {
43  FlutterPlatformViewMutation mutation = {};
45  mutation.opacity = opacity;
46  return std::make_unique<FlutterPlatformViewMutation>(mutation);
47 }
48 
49 static std::unique_ptr<FlutterPlatformViewMutation> ConvertMutation(
50  const SkRect& rect) {
51  FlutterPlatformViewMutation mutation = {};
53  mutation.clip_rect.left = rect.left();
54  mutation.clip_rect.top = rect.top();
55  mutation.clip_rect.right = rect.right();
56  mutation.clip_rect.bottom = rect.bottom();
57  return std::make_unique<FlutterPlatformViewMutation>(mutation);
58 }
59 
60 static FlutterSize VectorToSize(const SkVector& vector) {
61  FlutterSize size = {};
62  size.width = vector.x();
63  size.height = vector.y();
64  return size;
65 }
66 
67 static std::unique_ptr<FlutterPlatformViewMutation> ConvertMutation(
68  const SkRRect& rrect) {
69  FlutterPlatformViewMutation mutation = {};
71  const auto& rect = rrect.rect();
72  mutation.clip_rounded_rect.rect.left = rect.left();
73  mutation.clip_rounded_rect.rect.top = rect.top();
74  mutation.clip_rounded_rect.rect.right = rect.right();
75  mutation.clip_rounded_rect.rect.bottom = rect.bottom();
77  VectorToSize(rrect.radii(SkRRect::Corner::kUpperLeft_Corner));
79  VectorToSize(rrect.radii(SkRRect::Corner::kUpperRight_Corner));
81  VectorToSize(rrect.radii(SkRRect::Corner::kLowerRight_Corner));
83  VectorToSize(rrect.radii(SkRRect::Corner::kLowerLeft_Corner));
84  return std::make_unique<FlutterPlatformViewMutation>(mutation);
85 }
86 
87 static std::unique_ptr<FlutterPlatformViewMutation> ConvertMutation(
88  const SkMatrix& matrix) {
89  FlutterPlatformViewMutation mutation = {};
91  mutation.transformation.scaleX = matrix[SkMatrix::kMScaleX];
92  mutation.transformation.skewX = matrix[SkMatrix::kMSkewX];
93  mutation.transformation.transX = matrix[SkMatrix::kMTransX];
94  mutation.transformation.skewY = matrix[SkMatrix::kMSkewY];
95  mutation.transformation.scaleY = matrix[SkMatrix::kMScaleY];
96  mutation.transformation.transY = matrix[SkMatrix::kMTransY];
97  mutation.transformation.pers0 = matrix[SkMatrix::kMPersp0];
98  mutation.transformation.pers1 = matrix[SkMatrix::kMPersp1];
99  mutation.transformation.pers2 = matrix[SkMatrix::kMPersp2];
100  return std::make_unique<FlutterPlatformViewMutation>(mutation);
101 }
102 
105  const EmbeddedViewParams& params) {
106  {
107  FlutterPlatformView view = {};
108  view.struct_size = sizeof(FlutterPlatformView);
109  view.identifier = identifier;
110 
111  const auto& mutators = params.mutatorsStack();
112 
113  std::vector<const FlutterPlatformViewMutation*> mutations_array;
114 
115  for (auto i = mutators.Bottom(); i != mutators.Top(); ++i) {
116  const auto& mutator = *i;
117  switch (mutator->GetType()) {
118  case MutatorType::clip_rect: {
119  mutations_array.push_back(
120  mutations_referenced_
121  .emplace_back(ConvertMutation(mutator->GetRect()))
122  .get());
123  } break;
125  mutations_array.push_back(
126  mutations_referenced_
127  .emplace_back(ConvertMutation(mutator->GetRRect()))
128  .get());
129  } break;
130  case MutatorType::clip_path: {
131  // Unsupported mutation.
132  } break;
133  case MutatorType::transform: {
134  const auto& matrix = mutator->GetMatrix();
135  if (!matrix.isIdentity()) {
136  mutations_array.push_back(
137  mutations_referenced_.emplace_back(ConvertMutation(matrix))
138  .get());
139  }
140  } break;
141  case MutatorType::opacity: {
142  const double opacity =
143  std::clamp(mutator->GetAlphaFloat(), 0.0f, 1.0f);
144  if (opacity < 1.0) {
145  mutations_array.push_back(
146  mutations_referenced_.emplace_back(ConvertMutation(opacity))
147  .get());
148  }
149  } break;
150  }
151  }
152 
153  if (!mutations_array.empty()) {
154  // If there are going to be any mutations, they must first take into
155  // account the root surface transformation.
156  if (!root_surface_transformation_.isIdentity()) {
157  mutations_array.push_back(
158  mutations_referenced_
159  .emplace_back(ConvertMutation(root_surface_transformation_))
160  .get());
161  }
162 
163  auto mutations =
164  std::make_unique<std::vector<const FlutterPlatformViewMutation*>>(
165  mutations_array.rbegin(), mutations_array.rend());
166  mutations_arrays_referenced_.emplace_back(std::move(mutations));
167 
168  view.mutations_count = mutations_array.size();
169  view.mutations = mutations_arrays_referenced_.back().get()->data();
170  }
171 
172  platform_views_referenced_.emplace_back(
173  std::make_unique<FlutterPlatformView>(view));
174  }
175 
176  FlutterLayer layer = {};
177 
178  layer.struct_size = sizeof(FlutterLayer);
180  layer.platform_view = platform_views_referenced_.back().get();
181 
182  const auto layer_bounds =
183  SkRect::MakeXYWH(params.finalBoundingRect().x(), //
184  params.finalBoundingRect().y(), //
185  params.sizePoints().width() * device_pixel_ratio_, //
186  params.sizePoints().height() * device_pixel_ratio_ //
187  );
188 
189  const auto transformed_layer_bounds =
190  root_surface_transformation_.mapRect(layer_bounds);
191 
192  layer.offset.x = transformed_layer_bounds.x();
193  layer.offset.y = transformed_layer_bounds.y();
194  layer.size.width = transformed_layer_bounds.width();
195  layer.size.height = transformed_layer_bounds.height();
196 
197  presented_layers_.push_back(layer);
198 }
199 
201  const PresentCallback& callback) const {
202  std::vector<const FlutterLayer*> presented_layers_pointers;
203  presented_layers_pointers.reserve(presented_layers_.size());
204  for (const auto& layer : presented_layers_) {
205  presented_layers_pointers.push_back(&layer);
206  }
207  callback(std::move(presented_layers_pointers));
208 }
209 
210 } // namespace flutter
double skewY
vertical skew factor
Definition: embedder.h:230
static FlutterSize VectorToSize(const SkVector &vector)
double skewX
horizontal skew factor
Definition: embedder.h:226
double pers1
input y-axis perspective factor
Definition: embedder.h:238
FlutterSize upper_left_corner_radius
Definition: embedder.h:340
FlutterSize upper_right_corner_radius
Definition: embedder.h:341
double pers2
perspective scale factor
Definition: embedder.h:240
EmbedderLayers(SkISize frame_size, double device_pixel_ratio, SkMatrix root_surface_transformation)
double y
Definition: embedder.h:334
const FlutterBackingStore * backing_store
Definition: embedder.h:893
FlutterPlatformViewIdentifier identifier
Definition: embedder.h:822
FlutterSize size
The size of the layer (in physical pixels).
Definition: embedder.h:902
FlutterTransformation transformation
Definition: embedder.h:812
double pers0
input x-axis perspective factor
Definition: embedder.h:236
double right
Definition: embedder.h:327
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
void PushPlatformViewLayer(FlutterPlatformViewIdentifier identifier, const EmbeddedViewParams &params)
FlutterPoint offset
Definition: embedder.h:900
FlutterSize lower_right_corner_radius
Definition: embedder.h:342
double top
Definition: embedder.h:326
double scaleY
vertical scale factor
Definition: embedder.h:232
double left
Definition: embedder.h:325
Indicates that the contents of this layer are determined by the embedder.
Definition: embedder.h:881
double transX
horizontal translation
Definition: embedder.h:228
double x
Definition: embedder.h:333
const SkSize & sizePoints() const
const FlutterPlatformView * platform_view
Definition: embedder.h:896
FlutterLayerContentType type
Definition: embedder.h:889
void InvokePresentCallback(const PresentCallback &callback) const
size_t mutations_count
Definition: embedder.h:825
double width
Definition: embedder.h:311
A structure to represent the width and height.
Definition: embedder.h:310
double bottom
Definition: embedder.h:328
const FlutterPlatformViewMutation ** mutations
Definition: embedder.h:838
FlutterRoundedRect clip_rounded_rect
Definition: embedder.h:811
double transY
vertical translation
Definition: embedder.h:234
const SkRect & finalBoundingRect() const
double height
Definition: embedder.h:312
const MutatorsStack & mutatorsStack() const
FlutterPlatformViewMutationType type
The type of the mutation described by the subsequent union.
Definition: embedder.h:807
size_t struct_size
The size of this struct. Must be sizeof(FlutterPlatformView).
Definition: embedder.h:818
static std::unique_ptr< FlutterPlatformViewMutation > ConvertMutation(double opacity)
FlutterSize lower_left_corner_radius
Definition: embedder.h:343
double scaleX
horizontal scale factor
Definition: embedder.h:224
void PushBackingStoreLayer(const FlutterBackingStore *store)
size_t struct_size
This size of this struct. Must be sizeof(FlutterLayer).
Definition: embedder.h:886
int64_t FlutterPlatformViewIdentifier
Definition: embedder.h:590
std::function< bool(const std::vector< const FlutterLayer * > &layers)> PresentCallback
FlutterRect rect
Definition: embedder.h:339