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 
68 inline bool operator==(const FlutterMetalTexture& a,
69  const FlutterMetalTexture& b) {
70  return a.texture_id == b.texture_id && a.texture == b.texture;
71 }
72 
73 inline bool operator==(const FlutterMetalBackingStore& a,
74  const FlutterMetalBackingStore& b) {
75  return a.texture == b.texture;
76 }
77 
79  const FlutterOpenGLBackingStore& b) {
80  if (!(a.type == b.type)) {
81  return false;
82  }
83 
84  switch (a.type) {
86  return a.texture == b.texture;
88  return a.framebuffer == b.framebuffer;
89  }
90 
91  return false;
92 }
93 
95  const FlutterSoftwareBackingStore& b) {
96  return a.allocation == b.allocation && a.row_bytes == b.row_bytes &&
97  a.height == b.height && a.user_data == b.user_data &&
99 }
100 
101 inline bool operator==(const FlutterBackingStore& a,
102  const FlutterBackingStore& b) {
103  if (!(a.struct_size == b.struct_size && a.user_data == b.user_data &&
104  a.type == b.type && a.did_update == b.did_update)) {
105  return false;
106  }
107 
108  switch (a.type) {
110  return a.open_gl == b.open_gl;
112  return a.software == b.software;
114  return a.metal == b.metal;
115  }
116 
117  return false;
118 }
119 
121  const FlutterPlatformViewMutation& b) {
122  if (a.type != b.type) {
123  return false;
124  }
125 
126  switch (a.type) {
130  return a.clip_rect == b.clip_rect;
132  return a.clip_rounded_rect == b.clip_rounded_rect;
134  return a.transformation == b.transformation;
135  }
136 
137  return false;
138 }
139 
140 inline bool operator==(const FlutterPlatformView& a,
141  const FlutterPlatformView& b) {
142  if (!(a.struct_size == b.struct_size && a.identifier == b.identifier &&
144  return false;
145  }
146 
147  for (size_t i = 0; i < a.mutations_count; ++i) {
148  if (!(*a.mutations[i] == *b.mutations[i])) {
149  return false;
150  }
151  }
152 
153  return true;
154 }
155 
156 inline bool operator==(const FlutterLayer& a, const FlutterLayer& b) {
157  if (!(a.struct_size == b.struct_size && a.type == b.type &&
158  a.offset == b.offset && a.size == b.size)) {
159  return false;
160  }
161 
162  switch (a.type) {
164  return *a.backing_store == *b.backing_store;
166  return *a.platform_view == *b.platform_view;
167  }
168 
169  return false;
170 }
171 
172 //------------------------------------------------------------------------------
173 // Printing
174 //------------------------------------------------------------------------------
175 
176 inline std::ostream& operator<<(std::ostream& out, const FlutterPoint& point) {
177  return out << "(" << point.x << ", " << point.y << ")";
178 }
179 
180 inline std::ostream& operator<<(std::ostream& out, const FlutterRect& r) {
181  return out << "LTRB (" << r.left << ", " << r.top << ", " << r.right << ", "
182  << r.bottom << ")";
183 }
184 
185 inline std::ostream& operator<<(std::ostream& out, const FlutterSize& size) {
186  return out << "(" << size.width << ", " << size.height << ")";
187 }
188 
189 inline std::ostream& operator<<(std::ostream& out,
190  const FlutterRoundedRect& r) {
191  out << "Rect: " << r.rect << ", ";
192  out << "Upper Left Corner Radius: " << r.upper_left_corner_radius << ", ";
193  out << "Upper Right Corner Radius: " << r.upper_right_corner_radius << ", ";
194  out << "Lower Right Corner Radius: " << r.lower_right_corner_radius << ", ";
195  out << "Lower Left Corner Radius: " << r.lower_left_corner_radius;
196  return out;
197 }
198 
199 inline std::ostream& operator<<(std::ostream& out,
200  const FlutterTransformation& t) {
201  out << "Scale X: " << t.scaleX << ", ";
202  out << "Skew X: " << t.skewX << ", ";
203  out << "Trans X: " << t.transX << ", ";
204  out << "Skew Y: " << t.skewY << ", ";
205  out << "Scale Y: " << t.scaleY << ", ";
206  out << "Trans Y: " << t.transY << ", ";
207  out << "Pers 0: " << t.pers0 << ", ";
208  out << "Pers 1: " << t.pers1 << ", ";
209  out << "Pers 2: " << t.pers2;
210  return out;
211 }
212 
215  switch (type) {
217  return "kFlutterLayerContentTypeBackingStore";
219  return "kFlutterLayerContentTypePlatformView";
220  }
221  return "Unknown";
222 }
223 
226  switch (type) {
228  return "kFlutterBackingStoreTypeOpenGL";
230  return "kFlutterBackingStoreTypeSoftware";
232  return "kFlutterBackingStoreTypeMetal";
233  }
234  return "Unknown";
235 }
236 
237 inline std::ostream& operator<<(std::ostream& out,
238  const FlutterOpenGLTexture& item) {
239  return out << "(FlutterOpenGLTexture) Target: 0x" << std::hex << item.target
240  << std::dec << " Name: " << item.name << " Format: " << item.format
241  << " User Data: " << item.user_data << " Destruction Callback: "
242  << reinterpret_cast<void*>(item.destruction_callback);
243 }
244 
245 inline std::ostream& operator<<(std::ostream& out,
246  const FlutterOpenGLFramebuffer& item) {
247  return out << "(FlutterOpenGLFramebuffer) Target: 0x" << std::hex
248  << item.target << std::dec << " Name: " << item.name
249  << " User Data: " << item.user_data << " Destruction Callback: "
250  << reinterpret_cast<void*>(item.destruction_callback);
251 }
252 
253 inline std::ostream& operator<<(std::ostream& out,
254  const FlutterMetalTexture& item) {
255  return out << "(FlutterMetalTexture) Texture ID: " << std::hex
256  << item.texture_id << std::dec << " Handle: 0x" << std::hex
257  << item.texture;
258 }
261  switch (type) {
263  return "kFlutterPlatformViewMutationTypeOpacity";
265  return "kFlutterPlatformViewMutationTypeClipRect";
267  return "kFlutterPlatformViewMutationTypeClipRoundedRect";
269  return "kFlutterPlatformViewMutationTypeTransformation";
270  }
271  return "Unknown";
272 }
273 
274 inline std::ostream& operator<<(std::ostream& out,
275  const FlutterPlatformViewMutation& m) {
276  out << "(FlutterPlatformViewMutation) Type: "
278  switch (m.type) {
280  out << "Opacity: " << m.opacity;
282  out << "Clip Rect: " << m.clip_rect;
284  out << "Clip Rounded Rect: " << m.clip_rounded_rect;
286  out << "Transformation: " << m.transformation;
287  }
288  return out;
289 }
290 
291 inline std::ostream& operator<<(std::ostream& out,
293  out << "["
294  << "(FlutterPlatformView) Struct Size: " << platform_view.struct_size
295  << " Identifier: " << platform_view.identifier
296  << " Mutations Count: " << platform_view.mutations_count;
297 
298  if (platform_view.mutations_count > 0) {
299  out << std::endl;
300  for (size_t i = 0; i < platform_view.mutations_count; i++) {
301  out << "Mutation " << i << ": " << *platform_view.mutations[i]
302  << std::endl;
303  }
304  }
305 
306  out << "]";
307 
308  return out;
309 }
310 
313  switch (type) {
315  return "kFlutterOpenGLTargetTypeTexture";
317  return "kFlutterOpenGLTargetTypeFramebuffer";
318  }
319  return "Unknown";
320 }
321 
322 inline std::ostream& operator<<(std::ostream& out,
323  const FlutterOpenGLBackingStore& item) {
324  out << "(FlutterOpenGLBackingStore) Type: "
325  << FlutterOpenGLTargetTypeToString(item.type) << " ";
326  switch (item.type) {
328  out << item.texture;
329  break;
331  out << item.framebuffer;
332  break;
333  }
334  return out;
335 }
336 
337 inline std::ostream& operator<<(std::ostream& out,
338  const FlutterSoftwareBackingStore& item) {
339  return out << "(FlutterSoftwareBackingStore) Allocation: " << item.allocation
340  << " Row Bytes: " << item.row_bytes << " Height: " << item.height
341  << " User Data: " << item.user_data << " Destruction Callback: "
342  << reinterpret_cast<void*>(item.destruction_callback);
343 }
344 
345 inline std::ostream& operator<<(std::ostream& out,
346  const FlutterMetalBackingStore& item) {
347  return out << "(FlutterMetalBackingStore) Texture: " << item.texture;
348 }
349 
350 inline std::ostream& operator<<(std::ostream& out,
351  const FlutterBackingStore& backing_store) {
352  out << "(FlutterBackingStore) Struct size: " << backing_store.struct_size
353  << " User Data: " << backing_store.user_data
354  << " Type: " << FlutterBackingStoreTypeToString(backing_store.type)
355  << " ";
356 
357  switch (backing_store.type) {
359  out << backing_store.open_gl;
360  break;
361 
363  out << backing_store.software;
364  break;
365 
367  out << backing_store.metal;
368  break;
369  }
370 
371  return out;
372 }
373 
374 inline std::ostream& operator<<(std::ostream& out, const FlutterLayer& layer) {
375  out << "(Flutter Layer) Struct size: " << layer.struct_size
376  << " Type: " << FlutterLayerContentTypeToString(layer.type);
377 
378  switch (layer.type) {
380  out << *layer.backing_store;
381  break;
383  out << *layer.platform_view;
384  break;
385  }
386 
387  return out << " Offset: " << layer.offset << " Size: " << layer.size;
388 }
389 
390 //------------------------------------------------------------------------------
391 // Factories and Casts
392 //------------------------------------------------------------------------------
393 
394 inline FlutterPoint FlutterPointMake(double x, double y) {
395  FlutterPoint point = {};
396  point.x = x;
397  point.y = y;
398  return point;
399 }
400 
401 inline FlutterSize FlutterSizeMake(double width, double height) {
402  FlutterSize size = {};
403  size.width = width;
404  size.height = height;
405  return size;
406 }
407 
408 inline FlutterSize FlutterSizeMake(const SkVector& vector) {
409  FlutterSize size = {};
410  size.width = vector.x();
411  size.height = vector.y();
412  return size;
413 }
414 
415 inline FlutterTransformation FlutterTransformationMake(const SkMatrix& matrix) {
416  FlutterTransformation transformation = {};
417  transformation.scaleX = matrix[SkMatrix::kMScaleX];
418  transformation.skewX = matrix[SkMatrix::kMSkewX];
419  transformation.transX = matrix[SkMatrix::kMTransX];
420  transformation.skewY = matrix[SkMatrix::kMSkewY];
421  transformation.scaleY = matrix[SkMatrix::kMScaleY];
422  transformation.transY = matrix[SkMatrix::kMTransY];
423  transformation.pers0 = matrix[SkMatrix::kMPersp0];
424  transformation.pers1 = matrix[SkMatrix::kMPersp1];
425  transformation.pers2 = matrix[SkMatrix::kMPersp2];
426  return transformation;
427 }
428 
429 inline SkMatrix SkMatrixMake(const FlutterTransformation& xformation) {
430  return SkMatrix::MakeAll(xformation.scaleX, //
431  xformation.skewX, //
432  xformation.transX, //
433  xformation.skewY, //
434  xformation.scaleY, //
435  xformation.transY, //
436  xformation.pers0, //
437  xformation.pers1, //
438  xformation.pers2 //
439  );
440 }
441 
443  return reinterpret_cast<flutter::EmbedderEngine*>(engine);
444 }
445 
446 inline FlutterRect FlutterRectMake(const SkRect& rect) {
447  FlutterRect r = {};
448  r.left = rect.left();
449  r.top = rect.top();
450  r.right = rect.right();
451  r.bottom = rect.bottom();
452  return r;
453 }
454 
455 inline FlutterRect FlutterRectMakeLTRB(double l, double t, double r, double b) {
456  FlutterRect rect = {};
457  rect.left = l;
458  rect.top = t;
459  rect.right = r;
460  rect.bottom = b;
461  return rect;
462 }
463 
464 inline SkRect SkRectMake(const FlutterRect& rect) {
465  return SkRect::MakeLTRB(rect.left, rect.top, rect.right, rect.bottom);
466 }
467 
468 inline FlutterRoundedRect FlutterRoundedRectMake(const SkRRect& rect) {
469  FlutterRoundedRect r = {};
470  r.rect = FlutterRectMake(rect.rect());
472  FlutterSizeMake(rect.radii(SkRRect::Corner::kUpperLeft_Corner));
474  FlutterSizeMake(rect.radii(SkRRect::Corner::kUpperRight_Corner));
476  FlutterSizeMake(rect.radii(SkRRect::Corner::kLowerRight_Corner));
478  FlutterSizeMake(rect.radii(SkRRect::Corner::kLowerLeft_Corner));
479  return r;
480 }
481 
482 #endif // FLUTTER_SHELL_PLATFORM_EMBEDDER_TESTS_EMBEDDER_ASSERTIONS_H_
double skewY
vertical skew factor
Definition: embedder.h:238
FlutterPlatformViewMutationType
Definition: embedder.h:992
KeyCallType type
FlutterRect FlutterRectMake(const SkRect &rect)
Specified an software allocation for Flutter to render into using the CPU.
Definition: embedder.h:1048
bool NumberNear(double a, double b)
Definition: assertions.h:13
double skewX
horizontal skew factor
Definition: embedder.h:234
double pers1
input y-axis perspective factor
Definition: embedder.h:246
A structure to represent a 2D point.
Definition: embedder.h:341
FlutterSize upper_left_corner_radius
Definition: embedder.h:349
int64_t texture_id
Definition: embedder.h:499
FlutterSize upper_right_corner_radius
Definition: embedder.h:350
double pers2
perspective scale factor
Definition: embedder.h:248
double y
Definition: embedder.h:343
std::unique_ptr< flutter::PlatformViewIOS > platform_view
FlutterOpenGLFramebuffer framebuffer
Definition: embedder.h:960
const FlutterBackingStore * backing_store
Definition: embedder.h:1099
FlutterPlatformViewIdentifier identifier
Definition: embedder.h:1024
FlutterSize size
The size of the layer (in physical pixels).
Definition: embedder.h:1108
FlutterTransformation transformation
Definition: embedder.h:1014
double pers0
input x-axis perspective factor
Definition: embedder.h:244
FlutterSize FlutterSizeMake(double width, double height)
std::string FlutterBackingStoreTypeToString(FlutterBackingStoreType type)
double right
Definition: embedder.h:336
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
Specifies a Metal backing store. This is backed by a Metal texture.
Definition: embedder.h:1050
FlutterLayerContentType
Definition: embedder.h:1082
std::ostream & operator<<(std::ostream &out, const FlutterPoint &point)
FlutterPoint offset
Definition: embedder.h:1106
FlutterSize lower_right_corner_radius
Definition: embedder.h:351
double top
Definition: embedder.h:335
A structure to represent a rectangle.
Definition: embedder.h:333
SkMatrix SkMatrixMake(const FlutterTransformation &xformation)
double scaleY
vertical scale factor
Definition: embedder.h:240
double left
Definition: embedder.h:334
VoidCallback destruction_callback
Definition: embedder.h:978
FlutterOpenGLTargetType type
Definition: embedder.h:954
FlutterMetalTexture texture
Definition: embedder.h:988
VoidCallback destruction_callback
Definition: embedder.h:299
Indicates that the contents of this layer are determined by the embedder.
Definition: embedder.h:1087
FlutterSoftwareBackingStore software
The description of the software backing store.
Definition: embedder.h:1069
double transX
horizontal translation
Definition: embedder.h:236
double x
Definition: embedder.h:342
VoidCallback destruction_callback
Definition: embedder.h:274
size_t row_bytes
The number of bytes in a single row of the allocation.
Definition: embedder.h:969
FlutterPoint FlutterPointMake(double x, double y)
SkRect SkRectMake(const FlutterRect &rect)
FlutterOpenGLBackingStore open_gl
The description of the OpenGL backing store.
Definition: embedder.h:1067
const FlutterPlatformView * platform_view
Definition: embedder.h:1102
FlutterLayerContentType type
Definition: embedder.h:1095
FlutterRect FlutterRectMakeLTRB(double l, double t, double r, double b)
size_t mutations_count
Definition: embedder.h:1027
double width
Definition: embedder.h:320
size_t struct_size
The size of this struct. Must be sizeof(FlutterBackingStore).
Definition: embedder.h:1055
flutter::EmbedderEngine * ToEmbedderEngine(const FlutterEngine &engine)
A structure to represent the width and height.
Definition: embedder.h:319
int32_t width
double bottom
Definition: embedder.h:337
std::string FlutterOpenGLTargetTypeToString(FlutterOpenGLTargetType type)
size_t height
The number of rows in the allocation.
Definition: embedder.h:971
int32_t height
std::string FlutterPlatformViewMutationTypeToString(FlutterPlatformViewMutationType type)
FlutterBackingStoreType type
Specifies the type of backing store.
Definition: embedder.h:1061
const FlutterPlatformViewMutation ** mutations
Definition: embedder.h:1040
FlutterRoundedRect clip_rounded_rect
Definition: embedder.h:1013
double transY
vertical translation
Definition: embedder.h:242
FlutterMetalTextureHandle texture
Definition: embedder.h:502
double height
Definition: embedder.h:321
FlutterMetalBackingStore metal
Definition: embedder.h:1071
FlutterBackingStoreType
Definition: embedder.h:1043
uint32_t name
The name of the texture.
Definition: embedder.h:267
FlutterPlatformViewMutationType type
The type of the mutation described by the subsequent union.
Definition: embedder.h:1009
size_t struct_size
The size of this struct. Must be sizeof(FlutterPlatformView).
Definition: embedder.h:1020
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:295
A structure to represent a rounded rectangle.
Definition: embedder.h:347
void * user_data
User data to be returned on the invocation of the destruction callback.
Definition: embedder.h:271
FlutterSize lower_left_corner_radius
Definition: embedder.h:352
double scaleX
horizontal scale factor
Definition: embedder.h:232
FlutterRoundedRect FlutterRoundedRectMake(const SkRRect &rect)
uint32_t format
The texture format (example GL_RGBA8).
Definition: embedder.h:269
size_t struct_size
This size of this struct. Must be sizeof(FlutterLayer).
Definition: embedder.h:1092
FlutterTransformation FlutterTransformationMake(const SkMatrix &matrix)
uint32_t name
The name of the framebuffer.
Definition: embedder.h:292
FlutterOpenGLTexture texture
A texture for Flutter to render into.
Definition: embedder.h:957
std::string FlutterLayerContentTypeToString(FlutterLayerContentType type)
FlutterRect rect
Definition: embedder.h:348
FlutterOpenGLTargetType
Definition: embedder.h:253