Flutter Engine
embedder_assertions.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_EMBEDDER_TESTS_EMBEDDER_ASSERTIONS_H_
6 #define FLUTTER_SHELL_PLATFORM_EMBEDDER_TESTS_EMBEDDER_ASSERTIONS_H_
7 
8 #include <sstream>
9 
10 #include "flutter/fml/logging.h"
11 #include "flutter/shell/platform/embedder/embedder.h"
12 #include "flutter/shell/platform/embedder/embedder_engine.h"
13 #include "flutter/testing/assertions.h"
14 #include "gtest/gtest.h"
15 #include "third_party/skia/include/core/SkPoint.h"
16 #include "third_party/skia/include/core/SkSize.h"
17 
18 //------------------------------------------------------------------------------
19 // Equality
20 //------------------------------------------------------------------------------
21 inline bool operator==(const FlutterPoint& a, const FlutterPoint& b) {
22  return flutter::testing::NumberNear(a.x, b.x) &&
24 }
25 
26 inline bool operator==(const FlutterRect& a, const FlutterRect& b) {
27  return flutter::testing::NumberNear(a.left, b.left) &&
31 }
32 
33 inline bool operator==(const FlutterSize& a, const FlutterSize& b) {
36 }
37 
38 inline bool operator==(const FlutterRoundedRect& a,
39  const FlutterRoundedRect& b) {
40  return a.rect == b.rect &&
45 }
46 
47 inline bool operator==(const FlutterTransformation& a,
48  const FlutterTransformation& b) {
49  return a.scaleX == b.scaleX && a.skewX == b.skewX && a.transX == b.transX &&
50  a.skewY == b.skewY && a.scaleY == b.scaleY && a.transY == b.transY &&
51  a.pers0 == b.pers0 && a.pers1 == b.pers1 && a.pers2 == b.pers2;
52 }
53 
54 inline bool operator==(const FlutterOpenGLTexture& a,
55  const FlutterOpenGLTexture& b) {
56  return a.target == b.target && a.name == b.name && a.format == b.format &&
57  a.user_data == b.user_data &&
59 }
60 
61 inline bool operator==(const FlutterOpenGLFramebuffer& a,
62  const FlutterOpenGLFramebuffer& b) {
63  return a.target == b.target && a.name == b.name &&
64  a.user_data == b.user_data &&
66 }
67 
69  const FlutterOpenGLBackingStore& b) {
70  if (!(a.type == b.type)) {
71  return false;
72  }
73 
74  switch (a.type) {
76  return a.texture == b.texture;
78  return a.framebuffer == b.framebuffer;
79  }
80 
81  return false;
82 }
83 
85  const FlutterSoftwareBackingStore& b) {
86  return a.allocation == b.allocation && a.row_bytes == b.row_bytes &&
87  a.height == b.height && a.user_data == b.user_data &&
89 }
90 
91 inline bool operator==(const FlutterBackingStore& a,
92  const FlutterBackingStore& b) {
93  if (!(a.struct_size == b.struct_size && a.user_data == b.user_data &&
94  a.type == b.type && a.did_update == b.did_update)) {
95  return false;
96  }
97 
98  switch (a.type) {
100  return a.open_gl == b.open_gl;
102  return a.software == b.software;
103  }
104 
105  return false;
106 }
107 
109  const FlutterPlatformViewMutation& b) {
110  if (a.type != b.type) {
111  return false;
112  }
113 
114  switch (a.type) {
118  return a.clip_rect == b.clip_rect;
120  return a.clip_rounded_rect == b.clip_rounded_rect;
122  return a.transformation == b.transformation;
123  }
124 
125  return false;
126 }
127 
128 inline bool operator==(const FlutterPlatformView& a,
129  const FlutterPlatformView& b) {
130  if (!(a.struct_size == b.struct_size && a.identifier == b.identifier &&
132  return false;
133  }
134 
135  for (size_t i = 0; i < a.mutations_count; ++i) {
136  if (!(*a.mutations[i] == *b.mutations[i])) {
137  return false;
138  }
139  }
140 
141  return true;
142 }
143 
144 inline bool operator==(const FlutterLayer& a, const FlutterLayer& b) {
145  if (!(a.struct_size == b.struct_size && a.type == b.type &&
146  a.offset == b.offset && a.size == b.size)) {
147  return false;
148  }
149 
150  switch (a.type) {
152  return *a.backing_store == *b.backing_store;
154  return *a.platform_view == *b.platform_view;
155  }
156 
157  return false;
158 }
159 
160 //------------------------------------------------------------------------------
161 // Printing
162 //------------------------------------------------------------------------------
163 
164 inline std::ostream& operator<<(std::ostream& out, const FlutterPoint& point) {
165  return out << "(" << point.x << ", " << point.y << ")";
166 }
167 
168 inline std::ostream& operator<<(std::ostream& out, const FlutterRect& r) {
169  return out << "LTRB (" << r.left << ", " << r.top << ", " << r.right << ", "
170  << r.bottom << ")";
171 }
172 
173 inline std::ostream& operator<<(std::ostream& out, const FlutterSize& size) {
174  return out << "(" << size.width << ", " << size.height << ")";
175 }
176 
177 inline std::ostream& operator<<(std::ostream& out,
178  const FlutterRoundedRect& r) {
179  out << "Rect: " << r.rect << ", ";
180  out << "Upper Left Corner Radius: " << r.upper_left_corner_radius << ", ";
181  out << "Upper Right Corner Radius: " << r.upper_right_corner_radius << ", ";
182  out << "Lower Right Corner Radius: " << r.lower_right_corner_radius << ", ";
183  out << "Lower Left Corner Radius: " << r.lower_left_corner_radius;
184  return out;
185 }
186 
187 inline std::ostream& operator<<(std::ostream& out,
188  const FlutterTransformation& t) {
189  out << "Scale X: " << t.scaleX << ", ";
190  out << "Skew X: " << t.skewX << ", ";
191  out << "Trans X: " << t.transX << ", ";
192  out << "Skew Y: " << t.skewY << ", ";
193  out << "Scale Y: " << t.scaleY << ", ";
194  out << "Trans Y: " << t.transY << ", ";
195  out << "Pers 0: " << t.pers0 << ", ";
196  out << "Pers 1: " << t.pers1 << ", ";
197  out << "Pers 2: " << t.pers2;
198  return out;
199 }
200 
203  switch (type) {
205  return "kFlutterLayerContentTypeBackingStore";
207  return "kFlutterLayerContentTypePlatformView";
208  }
209  return "Unknown";
210 }
211 
214  switch (type) {
216  return "kFlutterBackingStoreTypeOpenGL";
218  return "kFlutterBackingStoreTypeSoftware";
219  }
220  return "Unknown";
221 }
222 
223 inline std::ostream& operator<<(std::ostream& out,
224  const FlutterOpenGLTexture& item) {
225  return out << "(FlutterOpenGLTexture) Target: 0x" << std::hex << item.target
226  << std::dec << " Name: " << item.name << " Format: " << item.format
227  << " User Data: " << item.user_data << " Destruction Callback: "
228  << reinterpret_cast<void*>(item.destruction_callback);
229 }
230 
231 inline std::ostream& operator<<(std::ostream& out,
232  const FlutterOpenGLFramebuffer& item) {
233  return out << "(FlutterOpenGLFramebuffer) Target: 0x" << std::hex
234  << item.target << std::dec << " Name: " << item.name
235  << " User Data: " << item.user_data << " Destruction Callback: "
236  << reinterpret_cast<void*>(item.destruction_callback);
237 }
238 
241  switch (type) {
243  return "kFlutterPlatformViewMutationTypeOpacity";
245  return "kFlutterPlatformViewMutationTypeClipRect";
247  return "kFlutterPlatformViewMutationTypeClipRoundedRect";
249  return "kFlutterPlatformViewMutationTypeTransformation";
250  }
251  return "Unknown";
252 }
253 
254 inline std::ostream& operator<<(std::ostream& out,
255  const FlutterPlatformViewMutation& m) {
256  out << "(FlutterPlatformViewMutation) Type: "
258  switch (m.type) {
260  out << "Opacity: " << m.opacity;
262  out << "Clip Rect: " << m.clip_rect;
264  out << "Clip Rounded Rect: " << m.clip_rounded_rect;
266  out << "Transformation: " << m.transformation;
267  }
268  return out;
269 }
270 
271 inline std::ostream& operator<<(std::ostream& out,
272  const FlutterPlatformView& platform_view) {
273  out << "["
274  << "(FlutterPlatformView) Struct Size: " << platform_view.struct_size
275  << " Identifier: " << platform_view.identifier
276  << " Mutations Count: " << platform_view.mutations_count;
277 
278  if (platform_view.mutations_count > 0) {
279  out << std::endl;
280  for (size_t i = 0; i < platform_view.mutations_count; i++) {
281  out << "Mutation " << i << ": " << *platform_view.mutations[i]
282  << std::endl;
283  }
284  }
285 
286  out << "]";
287 
288  return out;
289 }
290 
293  switch (type) {
295  return "kFlutterOpenGLTargetTypeTexture";
297  return "kFlutterOpenGLTargetTypeFramebuffer";
298  }
299  return "Unknown";
300 }
301 
302 inline std::ostream& operator<<(std::ostream& out,
303  const FlutterOpenGLBackingStore& item) {
304  out << "(FlutterOpenGLBackingStore) Type: "
305  << FlutterOpenGLTargetTypeToString(item.type) << " ";
306  switch (item.type) {
308  out << item.texture;
309  break;
311  out << item.framebuffer;
312  break;
313  }
314  return out;
315 }
316 
317 inline std::ostream& operator<<(std::ostream& out,
318  const FlutterSoftwareBackingStore& item) {
319  return out << "(FlutterSoftwareBackingStore) Allocation: " << item.allocation
320  << " Row Bytes: " << item.row_bytes << " Height: " << item.height
321  << " User Data: " << item.user_data << " Destruction Callback: "
322  << reinterpret_cast<void*>(item.destruction_callback);
323 }
324 
325 inline std::ostream& operator<<(std::ostream& out,
326  const FlutterBackingStore& backing_store) {
327  out << "(FlutterBackingStore) Struct size: " << backing_store.struct_size
328  << " User Data: " << backing_store.user_data
329  << " Type: " << FlutterBackingStoreTypeToString(backing_store.type)
330  << " ";
331 
332  switch (backing_store.type) {
334  out << backing_store.open_gl;
335  break;
336 
338  out << backing_store.software;
339  break;
340  }
341 
342  return out;
343 }
344 
345 inline std::ostream& operator<<(std::ostream& out, const FlutterLayer& layer) {
346  out << "(Flutter Layer) Struct size: " << layer.struct_size
347  << " Type: " << FlutterLayerContentTypeToString(layer.type);
348 
349  switch (layer.type) {
351  out << *layer.backing_store;
352  break;
354  out << *layer.platform_view;
355  break;
356  }
357 
358  return out << " Offset: " << layer.offset << " Size: " << layer.size;
359 }
360 
361 //------------------------------------------------------------------------------
362 // Factories and Casts
363 //------------------------------------------------------------------------------
364 
365 inline FlutterPoint FlutterPointMake(double x, double y) {
366  FlutterPoint point = {};
367  point.x = x;
368  point.y = y;
369  return point;
370 }
371 
372 inline FlutterSize FlutterSizeMake(double width, double height) {
373  FlutterSize size = {};
374  size.width = width;
375  size.height = height;
376  return size;
377 }
378 
379 inline FlutterSize FlutterSizeMake(const SkVector& vector) {
380  FlutterSize size = {};
381  size.width = vector.x();
382  size.height = vector.y();
383  return size;
384 }
385 
386 inline FlutterTransformation FlutterTransformationMake(const SkMatrix& matrix) {
387  FlutterTransformation transformation = {};
388  transformation.scaleX = matrix[SkMatrix::kMScaleX];
389  transformation.skewX = matrix[SkMatrix::kMSkewX];
390  transformation.transX = matrix[SkMatrix::kMTransX];
391  transformation.skewY = matrix[SkMatrix::kMSkewY];
392  transformation.scaleY = matrix[SkMatrix::kMScaleY];
393  transformation.transY = matrix[SkMatrix::kMTransY];
394  transformation.pers0 = matrix[SkMatrix::kMPersp0];
395  transformation.pers1 = matrix[SkMatrix::kMPersp1];
396  transformation.pers2 = matrix[SkMatrix::kMPersp2];
397  return transformation;
398 }
399 
400 inline SkMatrix SkMatrixMake(const FlutterTransformation& xformation) {
401  return SkMatrix::MakeAll(xformation.scaleX, //
402  xformation.skewX, //
403  xformation.transX, //
404  xformation.skewY, //
405  xformation.scaleY, //
406  xformation.transY, //
407  xformation.pers0, //
408  xformation.pers1, //
409  xformation.pers2 //
410  );
411 }
412 
414  return reinterpret_cast<flutter::EmbedderEngine*>(engine);
415 }
416 
417 inline FlutterRect FlutterRectMake(const SkRect& rect) {
418  FlutterRect r = {};
419  r.left = rect.left();
420  r.top = rect.top();
421  r.right = rect.right();
422  r.bottom = rect.bottom();
423  return r;
424 }
425 
426 inline FlutterRect FlutterRectMakeLTRB(double l, double t, double r, double b) {
427  FlutterRect rect = {};
428  rect.left = l;
429  rect.top = t;
430  rect.right = r;
431  rect.bottom = b;
432  return rect;
433 }
434 
435 inline SkRect SkRectMake(const FlutterRect& rect) {
436  return SkRect::MakeLTRB(rect.left, rect.top, rect.right, rect.bottom);
437 }
438 
439 inline FlutterRoundedRect FlutterRoundedRectMake(const SkRRect& rect) {
440  FlutterRoundedRect r = {};
441  r.rect = FlutterRectMake(rect.rect());
443  FlutterSizeMake(rect.radii(SkRRect::Corner::kUpperLeft_Corner));
445  FlutterSizeMake(rect.radii(SkRRect::Corner::kUpperRight_Corner));
447  FlutterSizeMake(rect.radii(SkRRect::Corner::kLowerRight_Corner));
449  FlutterSizeMake(rect.radii(SkRRect::Corner::kLowerLeft_Corner));
450  return r;
451 }
452 
453 #endif // FLUTTER_SHELL_PLATFORM_EMBEDDER_TESTS_EMBEDDER_ASSERTIONS_H_
double skewY
vertical skew factor
Definition: embedder.h:230
FlutterPlatformViewMutationType
Definition: embedder.h:790
FlutterRect FlutterRectMake(const SkRect &rect)
Specified an software allocation for Flutter to render into using the CPU.
Definition: embedder.h:846
bool NumberNear(double a, double b)
Definition: assertions.h:13
double skewX
horizontal skew factor
Definition: embedder.h:226
double pers1
input y-axis perspective factor
Definition: embedder.h:238
A structure to represent a 2D point.
Definition: embedder.h:332
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
double y
Definition: embedder.h:334
FlutterOpenGLFramebuffer framebuffer
Definition: embedder.h:769
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
FlutterSize FlutterSizeMake(double width, double height)
std::string FlutterBackingStoreTypeToString(FlutterBackingStoreType type)
double right
Definition: embedder.h:327
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
FlutterLayerContentType
Definition: embedder.h:876
std::ostream & operator<<(std::ostream &out, const FlutterPoint &point)
FlutterPoint offset
Definition: embedder.h:900
FlutterSize lower_right_corner_radius
Definition: embedder.h:342
double top
Definition: embedder.h:326
A structure to represent a rectangle.
Definition: embedder.h:324
SkMatrix SkMatrixMake(const FlutterTransformation &xformation)
double scaleY
vertical scale factor
Definition: embedder.h:232
double left
Definition: embedder.h:325
VoidCallback destruction_callback
Definition: embedder.h:787
FlutterOpenGLTargetType type
Definition: embedder.h:763
VoidCallback destruction_callback
Definition: embedder.h:291
Indicates that the contents of this layer are determined by the embedder.
Definition: embedder.h:881
FlutterSoftwareBackingStore software
The description of the software backing store.
Definition: embedder.h:865
double transX
horizontal translation
Definition: embedder.h:228
double x
Definition: embedder.h:333
VoidCallback destruction_callback
Definition: embedder.h:266
size_t row_bytes
The number of bytes in a single row of the allocation.
Definition: embedder.h:778
FlutterPoint FlutterPointMake(double x, double y)
SkRect SkRectMake(const FlutterRect &rect)
FlutterOpenGLBackingStore open_gl
The description of the OpenGL backing store.
Definition: embedder.h:863
const FlutterPlatformView * platform_view
Definition: embedder.h:896
FlutterLayerContentType type
Definition: embedder.h:889
FlutterRect FlutterRectMakeLTRB(double l, double t, double r, double b)
int32_t height
size_t mutations_count
Definition: embedder.h:825
int32_t width
double width
Definition: embedder.h:311
size_t struct_size
The size of this struct. Must be sizeof(FlutterBackingStore).
Definition: embedder.h:851
flutter::EmbedderEngine * ToEmbedderEngine(const FlutterEngine &engine)
A structure to represent the width and height.
Definition: embedder.h:310
double bottom
Definition: embedder.h:328
std::string FlutterOpenGLTargetTypeToString(FlutterOpenGLTargetType type)
size_t height
The number of rows in the allocation.
Definition: embedder.h:780
std::string FlutterPlatformViewMutationTypeToString(FlutterPlatformViewMutationType type)
FlutterBackingStoreType type
Specifies the type of backing store.
Definition: embedder.h:857
const FlutterPlatformViewMutation ** mutations
Definition: embedder.h:838
FlutterRoundedRect clip_rounded_rect
Definition: embedder.h:811
double transY
vertical translation
Definition: embedder.h:234
double height
Definition: embedder.h:312
FlutterBackingStoreType
Definition: embedder.h:841
uint32_t name
The name of the texture.
Definition: embedder.h:259
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
bool operator==(const FlutterPoint &a, const FlutterPoint &b)
void * user_data
User data to be returned on the invocation of the destruction callback.
Definition: embedder.h:287
A structure to represent a rounded rectangle.
Definition: embedder.h:338
void * user_data
User data to be returned on the invocation of the destruction callback.
Definition: embedder.h:263
FlutterSize lower_left_corner_radius
Definition: embedder.h:343
double scaleX
horizontal scale factor
Definition: embedder.h:224
FlutterRoundedRect FlutterRoundedRectMake(const SkRRect &rect)
uint32_t format
The texture format (example GL_RGBA8).
Definition: embedder.h:261
size_t struct_size
This size of this struct. Must be sizeof(FlutterLayer).
Definition: embedder.h:886
FlutterTransformation FlutterTransformationMake(const SkMatrix &matrix)
uint32_t name
The name of the framebuffer.
Definition: embedder.h:284
FlutterOpenGLTexture texture
A texture for Flutter to render into.
Definition: embedder.h:766
std::string FlutterLayerContentTypeToString(FlutterLayerContentType type)
FlutterRect rect
Definition: embedder.h:339
FlutterOpenGLTargetType
Definition: embedder.h:245