Flutter Impeller
entity_unittests.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 <algorithm>
6 #include <cstring>
7 #include <memory>
8 #include <optional>
9 #include <utility>
10 #include <vector>
11 
12 #include "flutter/display_list/geometry/dl_path_builder.h"
13 #include "flutter/display_list/testing/dl_test_snippets.h"
14 #include "fml/logging.h"
15 #include "gtest/gtest.h"
17 #include "impeller/core/formats.h"
19 #include "impeller/core/raw_ptr.h"
37 #include "impeller/entity/entity.h"
55 #include "impeller/renderer/testing/mocks.h"
57 #include "third_party/imgui/imgui.h"
58 
59 // TODO(zanderso): https://github.com/flutter/flutter/issues/127701
60 // NOLINTBEGIN(bugprone-unchecked-optional-access)
61 
62 namespace impeller {
63 namespace testing {
64 
65 using EntityTest = EntityPlayground;
67 
69  return Rect::MakeSize(size).Shift(center - size / 2);
70 }
71 
72 TEST_P(EntityTest, CanCreateEntity) {
73  Entity entity;
74  ASSERT_TRUE(entity.GetTransform().IsIdentity());
75 }
76 
77 TEST_P(EntityTest, FilterCoverageRespectsCropRect) {
78  auto image = CreateTextureForFixture("boston.jpg");
80  FilterInput::Make({image}));
81 
82  // Without the crop rect (default behavior).
83  {
84  auto actual = filter->GetCoverage({});
85  auto expected = Rect::MakeSize(image->GetSize());
86 
87  ASSERT_TRUE(actual.has_value());
88  ASSERT_RECT_NEAR(actual.value(), expected);
89  }
90 
91  // With the crop rect.
92  {
93  auto expected = Rect::MakeLTRB(50, 50, 100, 100);
94  filter->SetCoverageHint(expected);
95  auto actual = filter->GetCoverage({});
96 
97  ASSERT_TRUE(actual.has_value());
98  ASSERT_RECT_NEAR(actual.value(), expected);
99  }
100 }
101 
102 TEST_P(EntityTest, GeometryBoundsAreTransformed) {
103  auto geometry = Geometry::MakeRect(Rect::MakeXYWH(100, 100, 100, 100));
104  auto transform = Matrix::MakeScale({2.0, 2.0, 2.0});
105 
106  ASSERT_RECT_NEAR(geometry->GetCoverage(transform).value(),
107  Rect::MakeXYWH(200, 200, 200, 200));
108 }
109 
110 TEST_P(EntityTest, ThreeStrokesInOnePath) {
111  flutter::DlPath path = flutter::DlPathBuilder{}
112  .MoveTo({100, 100})
113  .LineTo({100, 200})
114  .MoveTo({100, 300})
115  .LineTo({100, 400})
116  .MoveTo({100, 500})
117  .LineTo({100, 600})
118  .TakePath();
119 
120  Entity entity;
121  entity.SetTransform(Matrix::MakeScale(GetContentScale()));
122  auto contents = std::make_unique<SolidColorContents>();
123 
124  std::unique_ptr<Geometry> geom =
125  Geometry::MakeStrokePath(path, {.width = 5.0f});
126  contents->SetGeometry(geom.get());
127  contents->SetColor(Color::Red());
128  entity.SetContents(std::move(contents));
129  ASSERT_TRUE(OpenPlaygroundHere(std::move(entity)));
130 }
131 
132 TEST_P(EntityTest, StrokeWithTextureContents) {
133  auto bridge = CreateTextureForFixture("bay_bridge.jpg");
134  flutter::DlPath path = flutter::DlPathBuilder{}
135  .MoveTo({100, 100})
136  .LineTo({100, 200})
137  .MoveTo({100, 300})
138  .LineTo({100, 400})
139  .MoveTo({100, 500})
140  .LineTo({100, 600})
141  .TakePath();
142 
143  Entity entity;
144  entity.SetTransform(Matrix::MakeScale(GetContentScale()));
145  auto contents = std::make_unique<TiledTextureContents>();
146  std::unique_ptr<Geometry> geom =
147  Geometry::MakeStrokePath(path, {.width = 100.0f});
148  contents->SetGeometry(geom.get());
149  contents->SetTexture(bridge);
150  contents->SetTileModes(Entity::TileMode::kClamp, Entity::TileMode::kClamp);
151  entity.SetContents(std::move(contents));
152  ASSERT_TRUE(OpenPlaygroundHere(std::move(entity)));
153 }
154 
155 TEST_P(EntityTest, TriangleInsideASquare) {
156  auto callback = [&](ContentContext& context, RenderPass& pass) {
157  Point offset(100, 100);
158 
159  static PlaygroundPoint point_a(Point(10, 10) + offset, 20, Color::White());
160  Point a = DrawPlaygroundPoint(point_a);
161  static PlaygroundPoint point_b(Point(210, 10) + offset, 20, Color::White());
162  Point b = DrawPlaygroundPoint(point_b);
163  static PlaygroundPoint point_c(Point(210, 210) + offset, 20,
164  Color::White());
165  Point c = DrawPlaygroundPoint(point_c);
166  static PlaygroundPoint point_d(Point(10, 210) + offset, 20, Color::White());
167  Point d = DrawPlaygroundPoint(point_d);
168  static PlaygroundPoint point_e(Point(50, 50) + offset, 20, Color::White());
169  Point e = DrawPlaygroundPoint(point_e);
170  static PlaygroundPoint point_f(Point(100, 50) + offset, 20, Color::White());
171  Point f = DrawPlaygroundPoint(point_f);
172  static PlaygroundPoint point_g(Point(50, 150) + offset, 20, Color::White());
173  Point g = DrawPlaygroundPoint(point_g);
174  flutter::DlPath path = flutter::DlPathBuilder{}
175  .MoveTo(a)
176  .LineTo(b)
177  .LineTo(c)
178  .LineTo(d)
179  .Close()
180  .MoveTo(e)
181  .LineTo(f)
182  .LineTo(g)
183  .Close()
184  .TakePath();
185 
186  Entity entity;
187  entity.SetTransform(Matrix::MakeScale(GetContentScale()));
188  auto contents = std::make_unique<SolidColorContents>();
189  std::unique_ptr<Geometry> geom =
190  Geometry::MakeStrokePath(path, {.width = 20.0});
191  contents->SetGeometry(geom.get());
192  contents->SetColor(Color::Red());
193  entity.SetContents(std::move(contents));
194 
195  return entity.Render(context, pass);
196  };
197  ASSERT_TRUE(OpenPlaygroundHere(callback));
198 }
199 
200 TEST_P(EntityTest, StrokeCapAndJoinTest) {
201  const Point padding(300, 250);
202  const Point margin(140, 180);
203 
204  auto callback = [&](ContentContext& context, RenderPass& pass) {
205  // Slightly above sqrt(2) by default, so that right angles are just below
206  // the limit and acute angles are over the limit (causing them to get
207  // beveled).
208  static Scalar miter_limit = 1.41421357;
209  static Scalar width = 30;
210 
211  ImGui::Begin("Controls", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
212  {
213  ImGui::SliderFloat("Miter limit", &miter_limit, 0, 30);
214  ImGui::SliderFloat("Stroke width", &width, 0, 100);
215  if (ImGui::Button("Reset")) {
216  miter_limit = 1.41421357;
217  width = 30;
218  }
219  }
220  ImGui::End();
221 
222  auto world_matrix = Matrix::MakeScale(GetContentScale());
223  auto render_path = [width = width, &context, &pass, &world_matrix](
224  const flutter::DlPath& path, Cap cap, Join join) {
225  auto contents = std::make_unique<SolidColorContents>();
226  std::unique_ptr<Geometry> geom =
228  .width = width,
229  .cap = cap,
230  .join = join,
231  .miter_limit = miter_limit,
232  });
233  contents->SetGeometry(geom.get());
234  contents->SetColor(Color::Red());
235 
236  Entity entity;
237  entity.SetTransform(world_matrix);
238  entity.SetContents(std::move(contents));
239 
240  auto coverage = entity.GetCoverage();
241  if (coverage.has_value()) {
242  auto bounds_contents = std::make_unique<SolidColorContents>();
243 
244  std::unique_ptr<Geometry> geom = Geometry::MakeFillPath(
245  flutter::DlPath::MakeRect(entity.GetCoverage().value()));
246 
247  bounds_contents->SetGeometry(geom.get());
248  bounds_contents->SetColor(Color::Green().WithAlpha(0.5));
249  Entity bounds_entity;
250  bounds_entity.SetContents(std::move(bounds_contents));
251  bounds_entity.Render(context, pass);
252  }
253 
254  entity.Render(context, pass);
255  };
256 
257  const Point a_def(0, 0), b_def(0, 100), c_def(150, 0), d_def(150, -100),
258  e_def(75, 75);
259  const Scalar r = 30;
260  // Cap::kButt demo.
261  {
262  Point off = Point(0, 0) * padding + margin;
263  static PlaygroundPoint point_a(off + a_def, r, Color::Black());
264  static PlaygroundPoint point_b(off + b_def, r, Color::White());
265  auto [a, b] = DrawPlaygroundLine(point_a, point_b);
266  static PlaygroundPoint point_c(off + c_def, r, Color::Black());
267  static PlaygroundPoint point_d(off + d_def, r, Color::White());
268  auto [c, d] = DrawPlaygroundLine(point_c, point_d);
269  render_path(flutter::DlPathBuilder{} //
270  .MoveTo(a)
271  .CubicCurveTo(b, d, c)
272  .TakePath(),
274  }
275 
276  // Cap::kSquare demo.
277  {
278  Point off = Point(1, 0) * padding + margin;
279  static PlaygroundPoint point_a(off + a_def, r, Color::Black());
280  static PlaygroundPoint point_b(off + b_def, r, Color::White());
281  auto [a, b] = DrawPlaygroundLine(point_a, point_b);
282  static PlaygroundPoint point_c(off + c_def, r, Color::Black());
283  static PlaygroundPoint point_d(off + d_def, r, Color::White());
284  auto [c, d] = DrawPlaygroundLine(point_c, point_d);
285  render_path(flutter::DlPathBuilder{} //
286  .MoveTo(a)
287  .CubicCurveTo(b, d, c)
288  .TakePath(),
290  }
291 
292  // Cap::kRound demo.
293  {
294  Point off = Point(2, 0) * padding + margin;
295  static PlaygroundPoint point_a(off + a_def, r, Color::Black());
296  static PlaygroundPoint point_b(off + b_def, r, Color::White());
297  auto [a, b] = DrawPlaygroundLine(point_a, point_b);
298  static PlaygroundPoint point_c(off + c_def, r, Color::Black());
299  static PlaygroundPoint point_d(off + d_def, r, Color::White());
300  auto [c, d] = DrawPlaygroundLine(point_c, point_d);
301  render_path(flutter::DlPathBuilder{} //
302  .MoveTo(a)
303  .CubicCurveTo(b, d, c)
304  .TakePath(),
306  }
307 
308  // Join::kBevel demo.
309  {
310  Point off = Point(0, 1) * padding + margin;
311  static PlaygroundPoint point_a =
312  PlaygroundPoint(off + a_def, r, Color::White());
313  static PlaygroundPoint point_b =
314  PlaygroundPoint(off + e_def, r, Color::White());
315  static PlaygroundPoint point_c =
316  PlaygroundPoint(off + c_def, r, Color::White());
317  Point a = DrawPlaygroundPoint(point_a);
318  Point b = DrawPlaygroundPoint(point_b);
319  Point c = DrawPlaygroundPoint(point_c);
320  render_path(flutter::DlPathBuilder{} //
321  .MoveTo(a)
322  .LineTo(b)
323  .LineTo(c)
324  .Close()
325  .TakePath(),
327  }
328 
329  // Join::kMiter demo.
330  {
331  Point off = Point(1, 1) * padding + margin;
332  static PlaygroundPoint point_a(off + a_def, r, Color::White());
333  static PlaygroundPoint point_b(off + e_def, r, Color::White());
334  static PlaygroundPoint point_c(off + c_def, r, Color::White());
335  Point a = DrawPlaygroundPoint(point_a);
336  Point b = DrawPlaygroundPoint(point_b);
337  Point c = DrawPlaygroundPoint(point_c);
338  render_path(flutter::DlPathBuilder{} //
339  .MoveTo(a)
340  .LineTo(b)
341  .LineTo(c)
342  .Close()
343  .TakePath(),
345  }
346 
347  // Join::kRound demo.
348  {
349  Point off = Point(2, 1) * padding + margin;
350  static PlaygroundPoint point_a(off + a_def, r, Color::White());
351  static PlaygroundPoint point_b(off + e_def, r, Color::White());
352  static PlaygroundPoint point_c(off + c_def, r, Color::White());
353  Point a = DrawPlaygroundPoint(point_a);
354  Point b = DrawPlaygroundPoint(point_b);
355  Point c = DrawPlaygroundPoint(point_c);
356  render_path(flutter::DlPathBuilder{} //
357  .MoveTo(a)
358  .LineTo(b)
359  .LineTo(c)
360  .Close()
361  .TakePath(),
363  }
364 
365  return true;
366  };
367  ASSERT_TRUE(OpenPlaygroundHere(callback));
368 }
369 
370 TEST_P(EntityTest, CubicCurveTest) {
371  // Compare with https://fiddle.skia.org/c/b3625f26122c9de7afe7794fcf25ead3
372  flutter::DlPath path =
373  flutter::DlPathBuilder{}
374  .MoveTo({237.164, 125.003})
375  .CubicCurveTo({236.709, 125.184}, {236.262, 125.358},
376  {235.81, 125.538})
377  .CubicCurveTo({235.413, 125.68}, {234.994, 125.832},
378  {234.592, 125.977})
379  .CubicCurveTo({234.592, 125.977}, {234.591, 125.977},
380  {234.59, 125.977})
381  .CubicCurveTo({222.206, 130.435}, {207.708, 135.753},
382  {192.381, 141.429})
383  .CubicCurveTo({162.77, 151.336}, {122.17, 156.894}, {84.1123, 160})
384  .Close()
385  .TakePath();
386  Entity entity;
387  entity.SetTransform(Matrix::MakeScale(GetContentScale()));
388 
389  std::unique_ptr<Geometry> geom = Geometry::MakeFillPath(path);
390 
391  auto contents = std::make_shared<SolidColorContents>();
392  contents->SetColor(Color::Red());
393  contents->SetGeometry(geom.get());
394 
395  entity.SetContents(contents);
396  ASSERT_TRUE(OpenPlaygroundHere(std::move(entity)));
397 }
398 
399 TEST_P(EntityTest, CanDrawCorrectlyWithRotatedTransform) {
400  auto callback = [&](ContentContext& context, RenderPass& pass) -> bool {
401  const char* input_axis[] = {"X", "Y", "Z"};
402  static int rotation_axis_index = 0;
403  static float rotation = 0;
404  ImGui::Begin("Controls", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
405  ImGui::SliderFloat("Rotation", &rotation, -kPi, kPi);
406  ImGui::Combo("Rotation Axis", &rotation_axis_index, input_axis,
407  sizeof(input_axis) / sizeof(char*));
408  Matrix rotation_matrix;
409  switch (rotation_axis_index) {
410  case 0:
411  rotation_matrix = Matrix::MakeRotationX(Radians(rotation));
412  break;
413  case 1:
414  rotation_matrix = Matrix::MakeRotationY(Radians(rotation));
415  break;
416  case 2:
417  rotation_matrix = Matrix::MakeRotationZ(Radians(rotation));
418  break;
419  default:
420  rotation_matrix = Matrix{};
421  break;
422  }
423 
424  if (ImGui::Button("Reset")) {
425  rotation = 0;
426  }
427  ImGui::End();
428  Matrix current_transform =
429  Matrix::MakeScale(GetContentScale())
431  Vector3(Point(pass.GetRenderTargetSize().width / 2.0,
432  pass.GetRenderTargetSize().height / 2.0)));
433  Matrix result_transform = current_transform * rotation_matrix;
434  flutter::DlPath path =
435  flutter::DlPath::MakeRect(Rect::MakeXYWH(-300, -400, 600, 800));
436 
437  Entity entity;
438  entity.SetTransform(result_transform);
439 
440  std::unique_ptr<Geometry> geom = Geometry::MakeFillPath(path);
441 
442  auto contents = std::make_shared<SolidColorContents>();
443  contents->SetColor(Color::Red());
444  contents->SetGeometry(geom.get());
445 
446  entity.SetContents(contents);
447  return entity.Render(context, pass);
448  };
449  ASSERT_TRUE(OpenPlaygroundHere(callback));
450 }
451 
452 TEST_P(EntityTest, CubicCurveAndOverlapTest) {
453  // Compare with https://fiddle.skia.org/c/7a05a3e186c65a8dfb732f68020aae06
454  flutter::DlPath path =
455  flutter::DlPathBuilder{}
456  .MoveTo({359.934, 96.6335})
457  .CubicCurveTo({358.189, 96.7055}, {356.436, 96.7908},
458  {354.673, 96.8895})
459  .CubicCurveTo({354.571, 96.8953}, {354.469, 96.9016},
460  {354.367, 96.9075})
461  .CubicCurveTo({352.672, 97.0038}, {350.969, 97.113},
462  {349.259, 97.2355})
463  .CubicCurveTo({349.048, 97.2506}, {348.836, 97.2678},
464  {348.625, 97.2834})
465  .CubicCurveTo({347.019, 97.4014}, {345.407, 97.5299},
466  {343.789, 97.6722})
467  .CubicCurveTo({343.428, 97.704}, {343.065, 97.7402},
468  {342.703, 97.7734})
469  .CubicCurveTo({341.221, 97.9086}, {339.736, 98.0505},
470  {338.246, 98.207})
471  .CubicCurveTo({337.702, 98.2642}, {337.156, 98.3292},
472  {336.612, 98.3894})
473  .CubicCurveTo({335.284, 98.5356}, {333.956, 98.6837},
474  {332.623, 98.8476})
475  .CubicCurveTo({332.495, 98.8635}, {332.366, 98.8818},
476  {332.237, 98.8982})
477  .LineTo({332.237, 102.601})
478  .LineTo({321.778, 102.601})
479  .LineTo({321.778, 100.382})
480  .CubicCurveTo({321.572, 100.413}, {321.367, 100.442},
481  {321.161, 100.476})
482  .CubicCurveTo({319.22, 100.79}, {317.277, 101.123},
483  {315.332, 101.479})
484  .CubicCurveTo({315.322, 101.481}, {315.311, 101.482},
485  {315.301, 101.484})
486  .LineTo({310.017, 105.94})
487  .LineTo({309.779, 105.427})
488  .LineTo({314.403, 101.651})
489  .CubicCurveTo({314.391, 101.653}, {314.379, 101.656},
490  {314.368, 101.658})
491  .CubicCurveTo({312.528, 102.001}, {310.687, 102.366},
492  {308.846, 102.748})
493  .CubicCurveTo({307.85, 102.955}, {306.855, 103.182}, {305.859, 103.4})
494  .CubicCurveTo({305.048, 103.579}, {304.236, 103.75},
495  {303.425, 103.936})
496  .LineTo({299.105, 107.578})
497  .LineTo({298.867, 107.065})
498  .LineTo({302.394, 104.185})
499  .LineTo({302.412, 104.171})
500  .CubicCurveTo({301.388, 104.409}, {300.366, 104.67},
501  {299.344, 104.921})
502  .CubicCurveTo({298.618, 105.1}, {297.89, 105.269}, {297.165, 105.455})
503  .CubicCurveTo({295.262, 105.94}, {293.36, 106.445},
504  {291.462, 106.979})
505  .CubicCurveTo({291.132, 107.072}, {290.802, 107.163},
506  {290.471, 107.257})
507  .CubicCurveTo({289.463, 107.544}, {288.455, 107.839},
508  {287.449, 108.139})
509  .CubicCurveTo({286.476, 108.431}, {285.506, 108.73},
510  {284.536, 109.035})
511  .CubicCurveTo({283.674, 109.304}, {282.812, 109.579},
512  {281.952, 109.859})
513  .CubicCurveTo({281.177, 110.112}, {280.406, 110.377},
514  {279.633, 110.638})
515  .CubicCurveTo({278.458, 111.037}, {277.256, 111.449},
516  {276.803, 111.607})
517  .CubicCurveTo({276.76, 111.622}, {276.716, 111.637},
518  {276.672, 111.653})
519  .CubicCurveTo({275.017, 112.239}, {273.365, 112.836},
520  {271.721, 113.463})
521  .LineTo({271.717, 113.449})
522  .CubicCurveTo({271.496, 113.496}, {271.238, 113.559},
523  {270.963, 113.628})
524  .CubicCurveTo({270.893, 113.645}, {270.822, 113.663},
525  {270.748, 113.682})
526  .CubicCurveTo({270.468, 113.755}, {270.169, 113.834},
527  {269.839, 113.926})
528  .CubicCurveTo({269.789, 113.94}, {269.732, 113.957},
529  {269.681, 113.972})
530  .CubicCurveTo({269.391, 114.053}, {269.081, 114.143},
531  {268.756, 114.239})
532  .CubicCurveTo({268.628, 114.276}, {268.5, 114.314},
533  {268.367, 114.354})
534  .CubicCurveTo({268.172, 114.412}, {267.959, 114.478},
535  {267.752, 114.54})
536  .CubicCurveTo({263.349, 115.964}, {258.058, 117.695},
537  {253.564, 119.252})
538  .CubicCurveTo({253.556, 119.255}, {253.547, 119.258},
539  {253.538, 119.261})
540  .CubicCurveTo({251.844, 119.849}, {250.056, 120.474},
541  {248.189, 121.131})
542  .CubicCurveTo({248, 121.197}, {247.812, 121.264}, {247.621, 121.331})
543  .CubicCurveTo({247.079, 121.522}, {246.531, 121.715},
544  {245.975, 121.912})
545  .CubicCurveTo({245.554, 122.06}, {245.126, 122.212},
546  {244.698, 122.364})
547  .CubicCurveTo({244.071, 122.586}, {243.437, 122.811},
548  {242.794, 123.04})
549  .CubicCurveTo({242.189, 123.255}, {241.58, 123.472},
550  {240.961, 123.693})
551  .CubicCurveTo({240.659, 123.801}, {240.357, 123.909},
552  {240.052, 124.018})
553  .CubicCurveTo({239.12, 124.351}, {238.18, 124.687}, {237.22, 125.032})
554  .LineTo({237.164, 125.003})
555  .CubicCurveTo({236.709, 125.184}, {236.262, 125.358},
556  {235.81, 125.538})
557  .CubicCurveTo({235.413, 125.68}, {234.994, 125.832},
558  {234.592, 125.977})
559  .CubicCurveTo({234.592, 125.977}, {234.591, 125.977},
560  {234.59, 125.977})
561  .CubicCurveTo({222.206, 130.435}, {207.708, 135.753},
562  {192.381, 141.429})
563  .CubicCurveTo({162.77, 151.336}, {122.17, 156.894}, {84.1123, 160})
564  .LineTo({360, 160})
565  .LineTo({360, 119.256})
566  .LineTo({360, 106.332})
567  .LineTo({360, 96.6307})
568  .CubicCurveTo({359.978, 96.6317}, {359.956, 96.6326},
569  {359.934, 96.6335})
570  .Close()
571  .MoveTo({337.336, 124.143})
572  .CubicCurveTo({337.274, 122.359}, {338.903, 121.511},
573  {338.903, 121.511})
574  .CubicCurveTo({338.903, 121.511}, {338.96, 123.303},
575  {337.336, 124.143})
576  .Close()
577  .MoveTo({340.082, 121.849})
578  .CubicCurveTo({340.074, 121.917}, {340.062, 121.992},
579  {340.046, 122.075})
580  .CubicCurveTo({340.039, 122.109}, {340.031, 122.142},
581  {340.023, 122.177})
582  .CubicCurveTo({340.005, 122.26}, {339.98, 122.346},
583  {339.952, 122.437})
584  .CubicCurveTo({339.941, 122.473}, {339.931, 122.507},
585  {339.918, 122.544})
586  .CubicCurveTo({339.873, 122.672}, {339.819, 122.804},
587  {339.75, 122.938})
588  .CubicCurveTo({339.747, 122.944}, {339.743, 122.949},
589  {339.74, 122.955})
590  .CubicCurveTo({339.674, 123.08}, {339.593, 123.205},
591  {339.501, 123.328})
592  .CubicCurveTo({339.473, 123.366}, {339.441, 123.401},
593  {339.41, 123.438})
594  .CubicCurveTo({339.332, 123.534}, {339.243, 123.625},
595  {339.145, 123.714})
596  .CubicCurveTo({339.105, 123.75}, {339.068, 123.786},
597  {339.025, 123.821})
598  .CubicCurveTo({338.881, 123.937}, {338.724, 124.048},
599  {338.539, 124.143})
600  .CubicCurveTo({338.532, 123.959}, {338.554, 123.79},
601  {338.58, 123.626})
602  .CubicCurveTo({338.58, 123.625}, {338.58, 123.625}, {338.58, 123.625})
603  .CubicCurveTo({338.607, 123.455}, {338.65, 123.299},
604  {338.704, 123.151})
605  .CubicCurveTo({338.708, 123.14}, {338.71, 123.127},
606  {338.714, 123.117})
607  .CubicCurveTo({338.769, 122.971}, {338.833, 122.838},
608  {338.905, 122.712})
609  .CubicCurveTo({338.911, 122.702}, {338.916, 122.69200000000001},
610  {338.922, 122.682})
611  .CubicCurveTo({338.996, 122.557}, {339.072, 122.444},
612  {339.155, 122.34})
613  .CubicCurveTo({339.161, 122.333}, {339.166, 122.326},
614  {339.172, 122.319})
615  .CubicCurveTo({339.256, 122.215}, {339.339, 122.12},
616  {339.425, 122.037})
617  .CubicCurveTo({339.428, 122.033}, {339.431, 122.03},
618  {339.435, 122.027})
619  .CubicCurveTo({339.785, 121.687}, {340.106, 121.511},
620  {340.106, 121.511})
621  .CubicCurveTo({340.106, 121.511}, {340.107, 121.645},
622  {340.082, 121.849})
623  .Close()
624  .MoveTo({340.678, 113.245})
625  .CubicCurveTo({340.594, 113.488}, {340.356, 113.655},
626  {340.135, 113.775})
627  .CubicCurveTo({339.817, 113.948}, {339.465, 114.059},
628  {339.115, 114.151})
629  .CubicCurveTo({338.251, 114.379}, {337.34, 114.516},
630  {336.448, 114.516})
631  .CubicCurveTo({335.761, 114.516}, {335.072, 114.527},
632  {334.384, 114.513})
633  .CubicCurveTo({334.125, 114.508}, {333.862, 114.462},
634  {333.605, 114.424})
635  .CubicCurveTo({332.865, 114.318}, {332.096, 114.184},
636  {331.41, 113.883})
637  .CubicCurveTo({330.979, 113.695}, {330.442, 113.34},
638  {330.672, 112.813})
639  .CubicCurveTo({331.135, 111.755}, {333.219, 112.946},
640  {334.526, 113.833})
641  .CubicCurveTo({334.54, 113.816}, {334.554, 113.8}, {334.569, 113.784})
642  .CubicCurveTo({333.38, 112.708}, {331.749, 110.985},
643  {332.76, 110.402})
644  .CubicCurveTo({333.769, 109.82}, {334.713, 111.93},
645  {335.228, 113.395})
646  .CubicCurveTo({334.915, 111.889}, {334.59, 109.636},
647  {335.661, 109.592})
648  .CubicCurveTo({336.733, 109.636}, {336.408, 111.889},
649  {336.07, 113.389})
650  .CubicCurveTo({336.609, 111.93}, {337.553, 109.82},
651  {338.563, 110.402})
652  .CubicCurveTo({339.574, 110.984}, {337.942, 112.708},
653  {336.753, 113.784})
654  .CubicCurveTo({336.768, 113.8}, {336.782, 113.816},
655  {336.796, 113.833})
656  .CubicCurveTo({338.104, 112.946}, {340.187, 111.755},
657  {340.65, 112.813})
658  .CubicCurveTo({340.71, 112.95}, {340.728, 113.102},
659  {340.678, 113.245})
660  .Close()
661  .MoveTo({346.357, 106.771})
662  .CubicCurveTo({346.295, 104.987}, {347.924, 104.139},
663  {347.924, 104.139})
664  .CubicCurveTo({347.924, 104.139}, {347.982, 105.931},
665  {346.357, 106.771})
666  .Close()
667  .MoveTo({347.56, 106.771})
668  .CubicCurveTo({347.498, 104.987}, {349.127, 104.139},
669  {349.127, 104.139})
670  .CubicCurveTo({349.127, 104.139}, {349.185, 105.931},
671  {347.56, 106.771})
672  .Close()
673  .TakePath();
674  Entity entity;
675  entity.SetTransform(Matrix::MakeScale(GetContentScale()));
676 
677  std::unique_ptr<Geometry> geom = Geometry::MakeFillPath(path);
678 
679  auto contents = std::make_shared<SolidColorContents>();
680  contents->SetColor(Color::Red());
681  contents->SetGeometry(geom.get());
682 
683  entity.SetContents(contents);
684  ASSERT_TRUE(OpenPlaygroundHere(std::move(entity)));
685 }
686 
687 TEST_P(EntityTest, SolidColorContentsStrokeSetStrokeCapsAndJoins) {
688  {
689  auto geometry = Geometry::MakeStrokePath(flutter::DlPath{});
690  auto path_geometry = static_cast<StrokePathGeometry*>(geometry.get());
691  // Defaults.
692  ASSERT_EQ(path_geometry->GetStrokeCap(), Cap::kButt);
693  ASSERT_EQ(path_geometry->GetStrokeJoin(), Join::kMiter);
694  }
695 
696  {
697  auto geometry = Geometry::MakeStrokePath(flutter::DlPath{}, //
698  {
699  .width = 1.0f,
700  .cap = Cap::kSquare,
701  .miter_limit = 4.0f,
702  });
703  auto path_geometry = static_cast<StrokePathGeometry*>(geometry.get());
704  ASSERT_EQ(path_geometry->GetStrokeCap(), Cap::kSquare);
705  }
706 
707  {
708  auto geometry = Geometry::MakeStrokePath(flutter::DlPath{}, //
709  {
710  .width = 1.0f,
711  .cap = Cap::kRound,
712  .miter_limit = 4.0f,
713  });
714  auto path_geometry = static_cast<StrokePathGeometry*>(geometry.get());
715  ASSERT_EQ(path_geometry->GetStrokeCap(), Cap::kRound);
716  }
717 }
718 
719 TEST_P(EntityTest, SolidColorContentsStrokeSetMiterLimit) {
720  {
721  auto geometry = Geometry::MakeStrokePath(flutter::DlPath{});
722  auto path_geometry = static_cast<StrokePathGeometry*>(geometry.get());
723  ASSERT_FLOAT_EQ(path_geometry->GetMiterLimit(), 4);
724  }
725 
726  {
727  auto geometry = Geometry::MakeStrokePath(flutter::DlPath{}, //
728  {
729  .width = 1.0f,
730  .miter_limit = 8.0f,
731  });
732  auto path_geometry = static_cast<StrokePathGeometry*>(geometry.get());
733  ASSERT_FLOAT_EQ(path_geometry->GetMiterLimit(), 8);
734  }
735 
736  {
737  auto geometry = Geometry::MakeStrokePath(flutter::DlPath{}, //
738  {
739  .width = 1.0f,
740  .miter_limit = -1.0f,
741  });
742  auto path_geometry = static_cast<StrokePathGeometry*>(geometry.get());
743  ASSERT_FLOAT_EQ(path_geometry->GetMiterLimit(), 4);
744  }
745 }
746 
747 TEST_P(EntityTest, BlendingModeOptions) {
748  std::vector<const char*> blend_mode_names;
749  std::vector<BlendMode> blend_mode_values;
750  {
751  // Force an exhausiveness check with a switch. When adding blend modes,
752  // update this switch with a new name/value to make it selectable in the
753  // test GUI.
754 
755  const BlendMode b{};
756  static_assert(b == BlendMode::kClear); // Ensure the first item in
757  // the switch is the first
758  // item in the enum.
760  switch (b) {
761  case BlendMode::kClear:
762  blend_mode_names.push_back("Clear");
763  blend_mode_values.push_back(BlendMode::kClear);
764  case BlendMode::kSrc:
765  blend_mode_names.push_back("Source");
766  blend_mode_values.push_back(BlendMode::kSrc);
767  case BlendMode::kDst:
768  blend_mode_names.push_back("Destination");
769  blend_mode_values.push_back(BlendMode::kDst);
770  case BlendMode::kSrcOver:
771  blend_mode_names.push_back("SourceOver");
772  blend_mode_values.push_back(BlendMode::kSrcOver);
773  case BlendMode::kDstOver:
774  blend_mode_names.push_back("DestinationOver");
775  blend_mode_values.push_back(BlendMode::kDstOver);
776  case BlendMode::kSrcIn:
777  blend_mode_names.push_back("SourceIn");
778  blend_mode_values.push_back(BlendMode::kSrcIn);
779  case BlendMode::kDstIn:
780  blend_mode_names.push_back("DestinationIn");
781  blend_mode_values.push_back(BlendMode::kDstIn);
782  case BlendMode::kSrcOut:
783  blend_mode_names.push_back("SourceOut");
784  blend_mode_values.push_back(BlendMode::kSrcOut);
785  case BlendMode::kDstOut:
786  blend_mode_names.push_back("DestinationOut");
787  blend_mode_values.push_back(BlendMode::kDstOut);
788  case BlendMode::kSrcATop:
789  blend_mode_names.push_back("SourceATop");
790  blend_mode_values.push_back(BlendMode::kSrcATop);
791  case BlendMode::kDstATop:
792  blend_mode_names.push_back("DestinationATop");
793  blend_mode_values.push_back(BlendMode::kDstATop);
794  case BlendMode::kXor:
795  blend_mode_names.push_back("Xor");
796  blend_mode_values.push_back(BlendMode::kXor);
797  case BlendMode::kPlus:
798  blend_mode_names.push_back("Plus");
799  blend_mode_values.push_back(BlendMode::kPlus);
801  blend_mode_names.push_back("Modulate");
802  blend_mode_values.push_back(BlendMode::kModulate);
803  };
804  }
805 
806  auto callback = [&](ContentContext& context, RenderPass& pass) {
807  auto world_matrix = Matrix::MakeScale(GetContentScale());
808  auto draw_rect = [&context, &pass, &world_matrix](
809  Rect rect, Color color, BlendMode blend_mode) -> bool {
812 
814  {
815  auto r = rect.GetLTRB();
816  vtx_builder.AddVertices({
817  {Point(r[0], r[1])},
818  {Point(r[2], r[1])},
819  {Point(r[2], r[3])},
820  {Point(r[0], r[1])},
821  {Point(r[2], r[3])},
822  {Point(r[0], r[3])},
823  });
824  }
825 
826  pass.SetCommandLabel("Blended Rectangle");
827  auto options = OptionsFromPass(pass);
828  options.blend_mode = blend_mode;
829  options.primitive_type = PrimitiveType::kTriangle;
830  pass.SetPipeline(context.GetSolidFillPipeline(options));
831  pass.SetVertexBuffer(
832  vtx_builder.CreateVertexBuffer(context.GetTransientsBuffer()));
833 
834  VS::FrameInfo frame_info;
835  frame_info.mvp = pass.GetOrthographicTransform() * world_matrix;
836  VS::BindFrameInfo(
837  pass, context.GetTransientsBuffer().EmplaceUniform(frame_info));
838  FS::FragInfo frag_info;
839  frag_info.color = color.Premultiply();
840  FS::BindFragInfo(
841  pass, context.GetTransientsBuffer().EmplaceUniform(frame_info));
842  return pass.Draw().ok();
843  };
844 
845  ImGui::Begin("Controls", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
846  static Color color1(1, 0, 0, 0.5), color2(0, 1, 0, 0.5);
847  ImGui::ColorEdit4("Color 1", reinterpret_cast<float*>(&color1));
848  ImGui::ColorEdit4("Color 2", reinterpret_cast<float*>(&color2));
849  static int current_blend_index = 3;
850  ImGui::ListBox("Blending mode", &current_blend_index,
851  blend_mode_names.data(), blend_mode_names.size());
852  ImGui::End();
853 
854  BlendMode selected_mode = blend_mode_values[current_blend_index];
855 
856  Point a, b, c, d;
857  static PlaygroundPoint point_a(Point(400, 100), 20, Color::White());
858  static PlaygroundPoint point_b(Point(200, 300), 20, Color::White());
859  std::tie(a, b) = DrawPlaygroundLine(point_a, point_b);
860  static PlaygroundPoint point_c(Point(470, 190), 20, Color::White());
861  static PlaygroundPoint point_d(Point(270, 390), 20, Color::White());
862  std::tie(c, d) = DrawPlaygroundLine(point_c, point_d);
863 
864  bool result = true;
865  result = result &&
866  draw_rect(Rect::MakeXYWH(0, 0, pass.GetRenderTargetSize().width,
867  pass.GetRenderTargetSize().height),
869  result = result && draw_rect(Rect::MakeLTRB(a.x, a.y, b.x, b.y), color1,
871  result = result && draw_rect(Rect::MakeLTRB(c.x, c.y, d.x, d.y), color2,
872  selected_mode);
873  return result;
874  };
875  ASSERT_TRUE(OpenPlaygroundHere(callback));
876 }
877 
878 TEST_P(EntityTest, BezierCircleScaled) {
879  auto callback = [&](ContentContext& context, RenderPass& pass) -> bool {
880  static float scale = 20;
881 
882  ImGui::Begin("Controls", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
883  ImGui::SliderFloat("Scale", &scale, 1, 100);
884  ImGui::End();
885 
886  Entity entity;
887  entity.SetTransform(Matrix::MakeScale(GetContentScale()));
888  auto path = flutter::DlPathBuilder{}
889  .MoveTo({97.325, 34.818})
890  .CubicCurveTo({98.50862885295136, 34.81812293973836},
891  {99.46822048142015, 33.85863261475589},
892  {99.46822048142015, 32.67499810206613})
893  .CubicCurveTo({99.46822048142015, 31.491363589376355},
894  {98.50862885295136, 30.53187326439389},
895  {97.32499434685802, 30.531998226542708})
896  .CubicCurveTo({96.14153655073771, 30.532123170035373},
897  {95.18222070648729, 31.491540299350355},
898  {95.18222070648729, 32.67499810206613})
899  .CubicCurveTo({95.18222070648729, 33.85845590478189},
900  {96.14153655073771, 34.81787303409686},
901  {97.32499434685802, 34.81799797758954})
902  .Close()
903  .TakePath();
904  entity.SetTransform(
905  Matrix::MakeScale({scale, scale, 1.0}).Translate({-90, -20, 0}));
906 
907  std::unique_ptr<Geometry> geom = Geometry::MakeFillPath(path);
908 
909  auto contents = std::make_shared<SolidColorContents>();
910  contents->SetColor(Color::Red());
911  contents->SetGeometry(geom.get());
912 
913  entity.SetContents(contents);
914  return entity.Render(context, pass);
915  };
916  ASSERT_TRUE(OpenPlaygroundHere(callback));
917 }
918 
919 TEST_P(EntityTest, Filters) {
920  auto bridge = CreateTextureForFixture("bay_bridge.jpg");
921  auto boston = CreateTextureForFixture("boston.jpg");
922  auto kalimba = CreateTextureForFixture("kalimba.jpg");
923  ASSERT_TRUE(bridge && boston && kalimba);
924 
925  auto callback = [&](ContentContext& context, RenderPass& pass) -> bool {
926  auto fi_bridge = FilterInput::Make(bridge);
927  auto fi_boston = FilterInput::Make(boston);
928  auto fi_kalimba = FilterInput::Make(kalimba);
929 
930  std::shared_ptr<FilterContents> blend0 = ColorFilterContents::MakeBlend(
931  BlendMode::kModulate, {fi_kalimba, fi_boston});
932 
933  auto blend1 = ColorFilterContents::MakeBlend(
935  {FilterInput::Make(blend0), fi_bridge, fi_bridge, fi_bridge});
936 
937  Entity entity;
938  entity.SetTransform(Matrix::MakeScale(GetContentScale()) *
939  Matrix::MakeTranslation({500, 300}) *
940  Matrix::MakeScale(Vector2{0.5, 0.5}));
941  entity.SetContents(blend1);
942  return entity.Render(context, pass);
943  };
944  ASSERT_TRUE(OpenPlaygroundHere(callback));
945 }
946 
947 TEST_P(EntityTest, GaussianBlurFilter) {
948  auto boston =
949  CreateTextureForFixture("boston.jpg", /*enable_mipmapping=*/true);
950  ASSERT_TRUE(boston);
951 
952  auto callback = [&](ContentContext& context, RenderPass& pass) -> bool {
953  const char* input_type_names[] = {"Texture", "Solid Color"};
954  const char* blur_type_names[] = {"Image blur", "Mask blur"};
955  const char* pass_variation_names[] = {"New"};
956  const char* blur_style_names[] = {"Normal", "Solid", "Outer", "Inner"};
957  const char* tile_mode_names[] = {"Clamp", "Repeat", "Mirror", "Decal"};
958  const FilterContents::BlurStyle blur_styles[] = {
961  const Entity::TileMode tile_modes[] = {
964 
965  // UI state.
966  static int selected_input_type = 0;
967  static Color input_color = Color::Black();
968  static int selected_blur_type = 0;
969  static int selected_pass_variation = 0;
970  static bool combined_sigma = false;
971  static float blur_amount_coarse[2] = {0, 0};
972  static float blur_amount_fine[2] = {10, 10};
973  static int selected_blur_style = 0;
974  static int selected_tile_mode = 3;
975  static Color cover_color(1, 0, 0, 0.2);
976  static Color bounds_color(0, 1, 0, 0.1);
977  static float offset[2] = {500, 400};
978  static float rotation = 0;
979  static float scale[2] = {0.65, 0.65};
980  static float skew[2] = {0, 0};
981  static float path_rect[4] = {0, 0,
982  static_cast<float>(boston->GetSize().width),
983  static_cast<float>(boston->GetSize().height)};
984 
985  ImGui::Begin("Controls", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
986  {
987  ImGui::Combo("Input type", &selected_input_type, input_type_names,
988  sizeof(input_type_names) / sizeof(char*));
989  if (selected_input_type == 0) {
990  ImGui::SliderFloat("Input opacity", &input_color.alpha, 0, 1);
991  } else {
992  ImGui::ColorEdit4("Input color",
993  reinterpret_cast<float*>(&input_color));
994  }
995  ImGui::Combo("Blur type", &selected_blur_type, blur_type_names,
996  sizeof(blur_type_names) / sizeof(char*));
997  if (selected_blur_type == 0) {
998  ImGui::Combo("Pass variation", &selected_pass_variation,
999  pass_variation_names,
1000  sizeof(pass_variation_names) / sizeof(char*));
1001  }
1002  ImGui::Checkbox("Combined sigma", &combined_sigma);
1003  if (combined_sigma) {
1004  ImGui::SliderFloat("Sigma (coarse)", blur_amount_coarse, 0, 1000);
1005  ImGui::SliderFloat("Sigma (fine)", blur_amount_fine, 0, 10);
1006  blur_amount_coarse[1] = blur_amount_coarse[0];
1007  blur_amount_fine[1] = blur_amount_fine[0];
1008  } else {
1009  ImGui::SliderFloat2("Sigma (coarse)", blur_amount_coarse, 0, 1000);
1010  ImGui::SliderFloat2("Sigma (fine)", blur_amount_fine, 0, 10);
1011  }
1012  ImGui::Combo("Blur style", &selected_blur_style, blur_style_names,
1013  sizeof(blur_style_names) / sizeof(char*));
1014  ImGui::Combo("Tile mode", &selected_tile_mode, tile_mode_names,
1015  sizeof(tile_mode_names) / sizeof(char*));
1016  ImGui::ColorEdit4("Cover color", reinterpret_cast<float*>(&cover_color));
1017  ImGui::ColorEdit4("Bounds color ",
1018  reinterpret_cast<float*>(&bounds_color));
1019  ImGui::SliderFloat2("Translation", offset, 0,
1020  pass.GetRenderTargetSize().width);
1021  ImGui::SliderFloat("Rotation", &rotation, 0, kPi * 2);
1022  ImGui::SliderFloat2("Scale", scale, 0, 3);
1023  ImGui::SliderFloat2("Skew", skew, -3, 3);
1024  ImGui::SliderFloat4("Path XYWH", path_rect, -1000, 1000);
1025  }
1026  ImGui::End();
1027 
1028  auto blur_sigma_x = Sigma{blur_amount_coarse[0] + blur_amount_fine[0]};
1029  auto blur_sigma_y = Sigma{blur_amount_coarse[1] + blur_amount_fine[1]};
1030 
1031  std::shared_ptr<Contents> input;
1032  Size input_size;
1033 
1034  auto input_rect =
1035  Rect::MakeXYWH(path_rect[0], path_rect[1], path_rect[2], path_rect[3]);
1036 
1037  std::unique_ptr<Geometry> solid_color_input;
1038  if (selected_input_type == 0) {
1039  auto texture = std::make_shared<TextureContents>();
1040  texture->SetSourceRect(Rect::MakeSize(boston->GetSize()));
1041  texture->SetDestinationRect(input_rect);
1042  texture->SetTexture(boston);
1043  texture->SetOpacity(input_color.alpha);
1044 
1045  input = texture;
1046  input_size = input_rect.GetSize();
1047  } else {
1048  auto fill = std::make_shared<SolidColorContents>();
1049  fill->SetColor(input_color);
1050  solid_color_input =
1051  Geometry::MakeFillPath(flutter::DlPath::MakeRect(input_rect));
1052 
1053  fill->SetGeometry(solid_color_input.get());
1054 
1055  input = fill;
1056  input_size = input_rect.GetSize();
1057  }
1058 
1059  std::shared_ptr<FilterContents> blur;
1060  switch (selected_pass_variation) {
1061  case 0:
1062  blur = std::make_shared<GaussianBlurFilterContents>(
1063  blur_sigma_x.sigma, blur_sigma_y.sigma,
1064  tile_modes[selected_tile_mode], blur_styles[selected_blur_style],
1065  /*geometry=*/nullptr);
1066  blur->SetInputs({FilterInput::Make(input)});
1067  break;
1068  case 1:
1070  FilterInput::Make(input), blur_sigma_x, blur_sigma_y,
1071  tile_modes[selected_tile_mode], blur_styles[selected_blur_style]);
1072  break;
1073  };
1074  FML_CHECK(blur);
1075 
1076  auto mask_blur = FilterContents::MakeBorderMaskBlur(
1077  FilterInput::Make(input), blur_sigma_x, blur_sigma_y,
1078  blur_styles[selected_blur_style]);
1079 
1080  auto ctm = Matrix::MakeScale(GetContentScale()) *
1081  Matrix::MakeTranslation(Vector3(offset[0], offset[1])) *
1082  Matrix::MakeRotationZ(Radians(rotation)) *
1083  Matrix::MakeScale(Vector2(scale[0], scale[1])) *
1084  Matrix::MakeSkew(skew[0], skew[1]) *
1085  Matrix::MakeTranslation(-Point(input_size) / 2);
1086 
1087  auto target_contents = selected_blur_type == 0 ? blur : mask_blur;
1088 
1089  Entity entity;
1090  entity.SetContents(target_contents);
1091  entity.SetTransform(ctm);
1092 
1093  entity.Render(context, pass);
1094 
1095  // Renders a red "cover" rectangle that shows the original position of the
1096  // unfiltered input.
1097  Entity cover_entity;
1098  std::unique_ptr<Geometry> geom =
1099  Geometry::MakeFillPath(flutter::DlPath::MakeRect(input_rect));
1100  auto contents = std::make_shared<SolidColorContents>();
1101  contents->SetColor(cover_color);
1102  contents->SetGeometry(geom.get());
1103  cover_entity.SetContents(std::move(contents));
1104  cover_entity.SetTransform(ctm);
1105  cover_entity.Render(context, pass);
1106 
1107  // Renders a green bounding rect of the target filter.
1108  Entity bounds_entity;
1109  std::optional<Rect> target_contents_coverage =
1110  target_contents->GetCoverage(entity);
1111  if (target_contents_coverage.has_value()) {
1112  std::unique_ptr<Geometry> geom =
1113  Geometry::MakeFillPath(flutter::DlPath::MakeRect(
1114  target_contents->GetCoverage(entity).value()));
1115  auto contents = std::make_shared<SolidColorContents>();
1116  contents->SetColor(bounds_color);
1117  contents->SetGeometry(geom.get());
1118 
1119  bounds_entity.SetContents(contents);
1120  bounds_entity.SetTransform(Matrix());
1121  bounds_entity.Render(context, pass);
1122  }
1123 
1124  return true;
1125  };
1126  ASSERT_TRUE(OpenPlaygroundHere(callback));
1127 }
1128 
1129 TEST_P(EntityTest, MorphologyFilter) {
1130  auto boston = CreateTextureForFixture("boston.jpg");
1131  ASSERT_TRUE(boston);
1132 
1133  auto callback = [&](ContentContext& context, RenderPass& pass) -> bool {
1134  const char* morphology_type_names[] = {"Dilate", "Erode"};
1135  const FilterContents::MorphType morphology_types[] = {
1137  static Color input_color = Color::Black();
1138  // UI state.
1139  static int selected_morphology_type = 0;
1140  static float radius[2] = {20, 20};
1141  static Color cover_color(1, 0, 0, 0.2);
1142  static Color bounds_color(0, 1, 0, 0.1);
1143  static float offset[2] = {500, 400};
1144  static float rotation = 0;
1145  static float scale[2] = {0.65, 0.65};
1146  static float skew[2] = {0, 0};
1147  static float path_rect[4] = {0, 0,
1148  static_cast<float>(boston->GetSize().width),
1149  static_cast<float>(boston->GetSize().height)};
1150  static float effect_transform_scale = 1;
1151 
1152  ImGui::Begin("Controls", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
1153  {
1154  ImGui::Combo("Morphology type", &selected_morphology_type,
1155  morphology_type_names,
1156  sizeof(morphology_type_names) / sizeof(char*));
1157  ImGui::SliderFloat2("Radius", radius, 0, 200);
1158  ImGui::SliderFloat("Input opacity", &input_color.alpha, 0, 1);
1159  ImGui::ColorEdit4("Cover color", reinterpret_cast<float*>(&cover_color));
1160  ImGui::ColorEdit4("Bounds color ",
1161  reinterpret_cast<float*>(&bounds_color));
1162  ImGui::SliderFloat2("Translation", offset, 0,
1163  pass.GetRenderTargetSize().width);
1164  ImGui::SliderFloat("Rotation", &rotation, 0, kPi * 2);
1165  ImGui::SliderFloat2("Scale", scale, 0, 3);
1166  ImGui::SliderFloat2("Skew", skew, -3, 3);
1167  ImGui::SliderFloat4("Path XYWH", path_rect, -1000, 1000);
1168  ImGui::SliderFloat("Effect transform scale", &effect_transform_scale, 0,
1169  3);
1170  }
1171  ImGui::End();
1172 
1173  std::shared_ptr<Contents> input;
1174  Size input_size;
1175 
1176  auto input_rect =
1177  Rect::MakeXYWH(path_rect[0], path_rect[1], path_rect[2], path_rect[3]);
1178  auto texture = std::make_shared<TextureContents>();
1179  texture->SetSourceRect(Rect::MakeSize(boston->GetSize()));
1180  texture->SetDestinationRect(input_rect);
1181  texture->SetTexture(boston);
1182  texture->SetOpacity(input_color.alpha);
1183 
1184  input = texture;
1185  input_size = input_rect.GetSize();
1186 
1187  auto contents = FilterContents::MakeMorphology(
1188  FilterInput::Make(input), Radius{radius[0]}, Radius{radius[1]},
1189  morphology_types[selected_morphology_type]);
1190  contents->SetEffectTransform(Matrix::MakeScale(
1191  Vector2{effect_transform_scale, effect_transform_scale}));
1192 
1193  auto ctm = Matrix::MakeScale(GetContentScale()) *
1194  Matrix::MakeTranslation(Vector3(offset[0], offset[1])) *
1195  Matrix::MakeRotationZ(Radians(rotation)) *
1196  Matrix::MakeScale(Vector2(scale[0], scale[1])) *
1197  Matrix::MakeSkew(skew[0], skew[1]) *
1198  Matrix::MakeTranslation(-Point(input_size) / 2);
1199 
1200  Entity entity;
1201  entity.SetContents(contents);
1202  entity.SetTransform(ctm);
1203 
1204  entity.Render(context, pass);
1205 
1206  // Renders a red "cover" rectangle that shows the original position of the
1207  // unfiltered input.
1208  Entity cover_entity;
1209  std::unique_ptr<Geometry> geom =
1210  Geometry::MakeFillPath(flutter::DlPath::MakeRect(input_rect));
1211  auto cover_contents = std::make_shared<SolidColorContents>();
1212  cover_contents->SetColor(cover_color);
1213  cover_contents->SetGeometry(geom.get());
1214  cover_entity.SetContents(cover_contents);
1215  cover_entity.SetTransform(ctm);
1216  cover_entity.Render(context, pass);
1217 
1218  // Renders a green bounding rect of the target filter.
1219  Entity bounds_entity;
1220  std::unique_ptr<Geometry> bounds_geom = Geometry::MakeFillPath(
1221  flutter::DlPath::MakeRect(contents->GetCoverage(entity).value()));
1222  auto bounds_contents = std::make_shared<SolidColorContents>();
1223  bounds_contents->SetColor(bounds_color);
1224  bounds_contents->SetGeometry(bounds_geom.get());
1225  bounds_entity.SetContents(std::move(bounds_contents));
1226  bounds_entity.SetTransform(Matrix());
1227 
1228  bounds_entity.Render(context, pass);
1229 
1230  return true;
1231  };
1232  ASSERT_TRUE(OpenPlaygroundHere(callback));
1233 }
1234 
1235 TEST_P(EntityTest, SetBlendMode) {
1236  Entity entity;
1237  ASSERT_EQ(entity.GetBlendMode(), BlendMode::kSrcOver);
1239  ASSERT_EQ(entity.GetBlendMode(), BlendMode::kClear);
1240 }
1241 
1242 TEST_P(EntityTest, ContentsGetBoundsForEmptyPathReturnsNullopt) {
1243  Entity entity;
1244  entity.SetContents(std::make_shared<SolidColorContents>());
1245  ASSERT_FALSE(entity.GetCoverage().has_value());
1246 }
1247 
1248 TEST_P(EntityTest, SolidStrokeCoverageIsCorrect) {
1249  {
1250  auto geometry = Geometry::MakeStrokePath(
1251  flutter::DlPath::MakeLine({0, 0}, {10, 10}), //
1252  {
1253  .width = 4.0f,
1254  .cap = Cap::kButt,
1255  .join = Join::kBevel,
1256  .miter_limit = 4.0f,
1257  });
1258 
1259  Entity entity;
1260  auto contents = std::make_unique<SolidColorContents>();
1261  contents->SetGeometry(geometry.get());
1262  contents->SetColor(Color::Black());
1263  entity.SetContents(std::move(contents));
1264  auto actual = entity.GetCoverage();
1265  auto expected = Rect::MakeLTRB(-2, -2, 12, 12);
1266 
1267  ASSERT_TRUE(actual.has_value());
1268  ASSERT_RECT_NEAR(actual.value(), expected);
1269  }
1270 
1271  // Cover the Cap::kSquare case.
1272  {
1273  auto geometry = Geometry::MakeStrokePath(
1274  flutter::DlPath::MakeLine({0, 0}, {10, 10}), //
1275  {
1276  .width = 4.0,
1277  .cap = Cap::kSquare,
1278  .join = Join::kBevel,
1279  .miter_limit = 4.0,
1280  });
1281 
1282  Entity entity;
1283  auto contents = std::make_unique<SolidColorContents>();
1284  contents->SetGeometry(geometry.get());
1285  contents->SetColor(Color::Black());
1286  entity.SetContents(std::move(contents));
1287  auto actual = entity.GetCoverage();
1288  auto expected =
1289  Rect::MakeLTRB(-sqrt(8), -sqrt(8), 10 + sqrt(8), 10 + sqrt(8));
1290 
1291  ASSERT_TRUE(actual.has_value());
1292  ASSERT_RECT_NEAR(actual.value(), expected);
1293  }
1294 
1295  // Cover the Join::kMiter case.
1296  {
1297  auto geometry = Geometry::MakeStrokePath(
1298  flutter::DlPath::MakeLine({0, 0}, {10, 10}), //
1299  {
1300  .width = 4.0f,
1301  .cap = Cap::kSquare,
1302  .join = Join::kMiter,
1303  .miter_limit = 2.0f,
1304  });
1305 
1306  Entity entity;
1307  auto contents = std::make_unique<SolidColorContents>();
1308  contents->SetGeometry(geometry.get());
1309  contents->SetColor(Color::Black());
1310  entity.SetContents(std::move(contents));
1311  auto actual = entity.GetCoverage();
1312  auto expected = Rect::MakeLTRB(-4, -4, 14, 14);
1313 
1314  ASSERT_TRUE(actual.has_value());
1315  ASSERT_RECT_NEAR(actual.value(), expected);
1316  }
1317 }
1318 
1319 TEST_P(EntityTest, BorderMaskBlurCoverageIsCorrect) {
1320  auto fill = std::make_shared<SolidColorContents>();
1321  auto geom = Geometry::MakeFillPath(
1322  flutter::DlPath::MakeRect(Rect::MakeXYWH(0, 0, 300, 400)));
1323  fill->SetGeometry(geom.get());
1324  fill->SetColor(Color::CornflowerBlue());
1325  auto border_mask_blur = FilterContents::MakeBorderMaskBlur(
1326  FilterInput::Make(fill), Radius{3}, Radius{4});
1327 
1328  {
1329  Entity e;
1330  e.SetTransform(Matrix());
1331  auto actual = border_mask_blur->GetCoverage(e);
1332  auto expected = Rect::MakeXYWH(-3, -4, 306, 408);
1333  ASSERT_TRUE(actual.has_value());
1334  ASSERT_RECT_NEAR(actual.value(), expected);
1335  }
1336 
1337  {
1338  Entity e;
1340  auto actual = border_mask_blur->GetCoverage(e);
1341  auto expected = Rect::MakeXYWH(-287.792, -4.94975, 504.874, 504.874);
1342  ASSERT_TRUE(actual.has_value());
1343  ASSERT_RECT_NEAR(actual.value(), expected);
1344  }
1345 }
1346 
1347 TEST_P(EntityTest, SolidFillCoverageIsCorrect) {
1348  // No transform
1349  {
1350  auto fill = std::make_shared<SolidColorContents>();
1351  fill->SetColor(Color::CornflowerBlue());
1352  auto expected = Rect::MakeLTRB(100, 110, 200, 220);
1353  auto geom = Geometry::MakeFillPath(flutter::DlPath::MakeRect(expected));
1354  fill->SetGeometry(geom.get());
1355 
1356  auto coverage = fill->GetCoverage({});
1357  ASSERT_TRUE(coverage.has_value());
1358  ASSERT_RECT_NEAR(coverage.value(), expected);
1359  }
1360 
1361  // Entity transform
1362  {
1363  auto fill = std::make_shared<SolidColorContents>();
1364  auto geom = Geometry::MakeFillPath(
1365  flutter::DlPath::MakeRect(Rect::MakeLTRB(100, 110, 200, 220)));
1366  fill->SetColor(Color::CornflowerBlue());
1367  fill->SetGeometry(geom.get());
1368 
1369  Entity entity;
1371  entity.SetContents(std::move(fill));
1372 
1373  auto coverage = entity.GetCoverage();
1374  auto expected = Rect::MakeLTRB(104, 115, 204, 225);
1375  ASSERT_TRUE(coverage.has_value());
1376  ASSERT_RECT_NEAR(coverage.value(), expected);
1377  }
1378 
1379  // No coverage for fully transparent colors
1380  {
1381  auto fill = std::make_shared<SolidColorContents>();
1382  auto geom = Geometry::MakeFillPath(
1383  flutter::DlPath::MakeRect(Rect::MakeLTRB(100, 110, 200, 220)));
1384  fill->SetColor(Color::WhiteTransparent());
1385  fill->SetGeometry(geom.get());
1386 
1387  auto coverage = fill->GetCoverage({});
1388  ASSERT_FALSE(coverage.has_value());
1389  }
1390 }
1391 
1392 TEST_P(EntityTest, RRectShadowTest) {
1393  auto callback = [&](ContentContext& context, RenderPass& pass) {
1394  static Color color = Color::Red();
1395  static float corner_radius = 100;
1396  static float blur_radius = 100;
1397  static bool show_coverage = false;
1398  static Color coverage_color = Color::Green().WithAlpha(0.2);
1399  static PlaygroundPoint top_left_point(Point(200, 200), 30, Color::White());
1400  static PlaygroundPoint bottom_right_point(Point(600, 400), 30,
1401  Color::White());
1402 
1403  ImGui::Begin("Controls", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
1404  ImGui::SliderFloat("Corner radius", &corner_radius, 0, 300);
1405  ImGui::SliderFloat("Blur radius", &blur_radius, 0, 300);
1406  ImGui::ColorEdit4("Color", reinterpret_cast<Scalar*>(&color));
1407  ImGui::Checkbox("Show coverage", &show_coverage);
1408  if (show_coverage) {
1409  ImGui::ColorEdit4("Coverage color",
1410  reinterpret_cast<Scalar*>(&coverage_color));
1411  }
1412  ImGui::End();
1413 
1414  auto [top_left, bottom_right] =
1415  DrawPlaygroundLine(top_left_point, bottom_right_point);
1416  auto rect =
1417  Rect::MakeLTRB(top_left.x, top_left.y, bottom_right.x, bottom_right.y);
1418 
1419  auto contents = std::make_unique<SolidRRectBlurContents>();
1420  contents->SetShape(rect, corner_radius);
1421  contents->SetColor(color);
1422  contents->SetSigma(Radius(blur_radius));
1423 
1424  Entity entity;
1425  entity.SetTransform(Matrix::MakeScale(GetContentScale()));
1426  entity.SetContents(std::move(contents));
1427  entity.Render(context, pass);
1428 
1429  auto coverage = entity.GetCoverage();
1430  if (show_coverage && coverage.has_value()) {
1431  auto bounds_contents = std::make_unique<SolidColorContents>();
1432  auto geom = Geometry::MakeFillPath(
1433  flutter::DlPath::MakeRect(entity.GetCoverage().value()));
1434  bounds_contents->SetGeometry(geom.get());
1435  bounds_contents->SetColor(coverage_color.Premultiply());
1436  Entity bounds_entity;
1437  bounds_entity.SetContents(std::move(bounds_contents));
1438  bounds_entity.Render(context, pass);
1439  }
1440 
1441  return true;
1442  };
1443  ASSERT_TRUE(OpenPlaygroundHere(callback));
1444 }
1445 
1446 TEST_P(EntityTest, ColorMatrixFilterCoverageIsCorrect) {
1447  // Set up a simple color background.
1448  auto fill = std::make_shared<SolidColorContents>();
1449  auto geom = Geometry::MakeFillPath(
1450  flutter::DlPath::MakeRect(Rect::MakeXYWH(0, 0, 300, 400)));
1451  fill->SetGeometry(geom.get());
1452  fill->SetColor(Color::Coral());
1453 
1454  // Set the color matrix filter.
1455  ColorMatrix matrix = {
1456  1, 1, 1, 1, 1, //
1457  1, 1, 1, 1, 1, //
1458  1, 1, 1, 1, 1, //
1459  1, 1, 1, 1, 1, //
1460  };
1461 
1462  auto filter =
1464 
1465  Entity e;
1466  e.SetTransform(Matrix());
1467 
1468  // Confirm that the actual filter coverage matches the expected coverage.
1469  auto actual = filter->GetCoverage(e);
1470  auto expected = Rect::MakeXYWH(0, 0, 300, 400);
1471 
1472  ASSERT_TRUE(actual.has_value());
1473  ASSERT_RECT_NEAR(actual.value(), expected);
1474 }
1475 
1476 TEST_P(EntityTest, ColorMatrixFilterEditable) {
1477  auto bay_bridge = CreateTextureForFixture("bay_bridge.jpg");
1478  ASSERT_TRUE(bay_bridge);
1479 
1480  auto callback = [&](ContentContext& context, RenderPass& pass) -> bool {
1481  // UI state.
1482  static ColorMatrix color_matrix = {
1483  1, 0, 0, 0, 0, //
1484  0, 3, 0, 0, 0, //
1485  0, 0, 1, 0, 0, //
1486  0, 0, 0, 1, 0, //
1487  };
1488  static float offset[2] = {500, 400};
1489  static float rotation = 0;
1490  static float scale[2] = {0.65, 0.65};
1491  static float skew[2] = {0, 0};
1492 
1493  // Define the ImGui
1494  ImGui::Begin("Color Matrix", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
1495  {
1496  std::string label = "##1";
1497  for (int i = 0; i < 20; i += 5) {
1498  ImGui::InputScalarN(label.c_str(), ImGuiDataType_Float,
1499  &(color_matrix.array[i]), 5, nullptr, nullptr,
1500  "%.2f", 0);
1501  label[2]++;
1502  }
1503 
1504  ImGui::SliderFloat2("Translation", &offset[0], 0,
1505  pass.GetRenderTargetSize().width);
1506  ImGui::SliderFloat("Rotation", &rotation, 0, kPi * 2);
1507  ImGui::SliderFloat2("Scale", &scale[0], 0, 3);
1508  ImGui::SliderFloat2("Skew", &skew[0], -3, 3);
1509  }
1510  ImGui::End();
1511 
1512  // Set the color matrix filter.
1514  FilterInput::Make(bay_bridge), color_matrix);
1515 
1516  // Define the entity with the color matrix filter.
1517  Entity entity;
1518  entity.SetTransform(
1519  Matrix::MakeScale(GetContentScale()) *
1520  Matrix::MakeTranslation(Vector3(offset[0], offset[1])) *
1521  Matrix::MakeRotationZ(Radians(rotation)) *
1522  Matrix::MakeScale(Vector2(scale[0], scale[1])) *
1523  Matrix::MakeSkew(skew[0], skew[1]) *
1524  Matrix::MakeTranslation(-Point(bay_bridge->GetSize()) / 2));
1525  entity.SetContents(filter);
1526  entity.Render(context, pass);
1527 
1528  return true;
1529  };
1530 
1531  ASSERT_TRUE(OpenPlaygroundHere(callback));
1532 }
1533 
1534 TEST_P(EntityTest, LinearToSrgbFilterCoverageIsCorrect) {
1535  // Set up a simple color background.
1536  auto geom = Geometry::MakeFillPath(
1537  flutter::DlPath::MakeRect(Rect::MakeXYWH(0, 0, 300, 400)));
1538  auto fill = std::make_shared<SolidColorContents>();
1539  fill->SetGeometry(geom.get());
1540  fill->SetColor(Color::MintCream());
1541 
1542  auto filter =
1544 
1545  Entity e;
1546  e.SetTransform(Matrix());
1547 
1548  // Confirm that the actual filter coverage matches the expected coverage.
1549  auto actual = filter->GetCoverage(e);
1550  auto expected = Rect::MakeXYWH(0, 0, 300, 400);
1551 
1552  ASSERT_TRUE(actual.has_value());
1553  ASSERT_RECT_NEAR(actual.value(), expected);
1554 }
1555 
1556 TEST_P(EntityTest, LinearToSrgbFilter) {
1557  auto image = CreateTextureForFixture("kalimba.jpg");
1558  ASSERT_TRUE(image);
1559 
1560  auto callback = [&](ContentContext& context, RenderPass& pass) -> bool {
1561  auto filtered =
1563 
1564  // Define the entity that will serve as the control image as a Gaussian blur
1565  // filter with no filter at all.
1566  Entity entity_left;
1567  entity_left.SetTransform(Matrix::MakeScale(GetContentScale()) *
1568  Matrix::MakeTranslation({100, 300}) *
1569  Matrix::MakeScale(Vector2{0.5, 0.5}));
1570  auto unfiltered = FilterContents::MakeGaussianBlur(FilterInput::Make(image),
1571  Sigma{0}, Sigma{0});
1572  entity_left.SetContents(unfiltered);
1573 
1574  // Define the entity that will be filtered from linear to sRGB.
1575  Entity entity_right;
1576  entity_right.SetTransform(Matrix::MakeScale(GetContentScale()) *
1577  Matrix::MakeTranslation({500, 300}) *
1578  Matrix::MakeScale(Vector2{0.5, 0.5}));
1579  entity_right.SetContents(filtered);
1580  return entity_left.Render(context, pass) &&
1581  entity_right.Render(context, pass);
1582  };
1583 
1584  ASSERT_TRUE(OpenPlaygroundHere(callback));
1585 }
1586 
1587 TEST_P(EntityTest, SrgbToLinearFilterCoverageIsCorrect) {
1588  // Set up a simple color background.
1589  auto fill = std::make_shared<SolidColorContents>();
1590  auto geom = Geometry::MakeFillPath(
1591  flutter::DlPath::MakeRect(Rect::MakeXYWH(0, 0, 300, 400)));
1592  fill->SetGeometry(geom.get());
1593  fill->SetColor(Color::DeepPink());
1594 
1595  auto filter =
1597 
1598  Entity e;
1599  e.SetTransform(Matrix());
1600 
1601  // Confirm that the actual filter coverage matches the expected coverage.
1602  auto actual = filter->GetCoverage(e);
1603  auto expected = Rect::MakeXYWH(0, 0, 300, 400);
1604 
1605  ASSERT_TRUE(actual.has_value());
1606  ASSERT_RECT_NEAR(actual.value(), expected);
1607 }
1608 
1609 TEST_P(EntityTest, SrgbToLinearFilter) {
1610  auto image = CreateTextureForFixture("embarcadero.jpg");
1611  ASSERT_TRUE(image);
1612 
1613  auto callback = [&](ContentContext& context, RenderPass& pass) -> bool {
1614  auto filtered =
1616 
1617  // Define the entity that will serve as the control image as a Gaussian blur
1618  // filter with no filter at all.
1619  Entity entity_left;
1620  entity_left.SetTransform(Matrix::MakeScale(GetContentScale()) *
1621  Matrix::MakeTranslation({100, 300}) *
1622  Matrix::MakeScale(Vector2{0.5, 0.5}));
1623  auto unfiltered = FilterContents::MakeGaussianBlur(FilterInput::Make(image),
1624  Sigma{0}, Sigma{0});
1625  entity_left.SetContents(unfiltered);
1626 
1627  // Define the entity that will be filtered from sRGB to linear.
1628  Entity entity_right;
1629  entity_right.SetTransform(Matrix::MakeScale(GetContentScale()) *
1630  Matrix::MakeTranslation({500, 300}) *
1631  Matrix::MakeScale(Vector2{0.5, 0.5}));
1632  entity_right.SetContents(filtered);
1633  return entity_left.Render(context, pass) &&
1634  entity_right.Render(context, pass);
1635  };
1636 
1637  ASSERT_TRUE(OpenPlaygroundHere(callback));
1638 }
1639 
1640 static Vector3 RGBToYUV(Vector3 rgb, YUVColorSpace yuv_color_space) {
1641  Vector3 yuv;
1642  switch (yuv_color_space) {
1644  yuv.x = rgb.x * 0.299 + rgb.y * 0.587 + rgb.z * 0.114;
1645  yuv.y = rgb.x * -0.169 + rgb.y * -0.331 + rgb.z * 0.5 + 0.5;
1646  yuv.z = rgb.x * 0.5 + rgb.y * -0.419 + rgb.z * -0.081 + 0.5;
1647  break;
1649  yuv.x = rgb.x * 0.257 + rgb.y * 0.516 + rgb.z * 0.100 + 0.063;
1650  yuv.y = rgb.x * -0.145 + rgb.y * -0.291 + rgb.z * 0.439 + 0.5;
1651  yuv.z = rgb.x * 0.429 + rgb.y * -0.368 + rgb.z * -0.071 + 0.5;
1652  break;
1653  }
1654  return yuv;
1655 }
1656 
1657 static std::vector<std::shared_ptr<Texture>> CreateTestYUVTextures(
1658  Context* context,
1659  YUVColorSpace yuv_color_space) {
1660  Vector3 red = {244.0 / 255.0, 67.0 / 255.0, 54.0 / 255.0};
1661  Vector3 green = {76.0 / 255.0, 175.0 / 255.0, 80.0 / 255.0};
1662  Vector3 blue = {33.0 / 255.0, 150.0 / 255.0, 243.0 / 255.0};
1663  Vector3 white = {1.0, 1.0, 1.0};
1664  Vector3 red_yuv = RGBToYUV(red, yuv_color_space);
1665  Vector3 green_yuv = RGBToYUV(green, yuv_color_space);
1666  Vector3 blue_yuv = RGBToYUV(blue, yuv_color_space);
1667  Vector3 white_yuv = RGBToYUV(white, yuv_color_space);
1668  std::vector<Vector3> yuvs{red_yuv, green_yuv, blue_yuv, white_yuv};
1669  std::vector<uint8_t> y_data;
1670  std::vector<uint8_t> uv_data;
1671  for (int i = 0; i < 4; i++) {
1672  auto yuv = yuvs[i];
1673  uint8_t y = std::round(yuv.x * 255.0);
1674  uint8_t u = std::round(yuv.y * 255.0);
1675  uint8_t v = std::round(yuv.z * 255.0);
1676  for (int j = 0; j < 16; j++) {
1677  y_data.push_back(y);
1678  }
1679  for (int j = 0; j < 8; j++) {
1680  uv_data.push_back(j % 2 == 0 ? u : v);
1681  }
1682  }
1683  auto cmd_buffer = context->CreateCommandBuffer();
1684  auto blit_pass = cmd_buffer->CreateBlitPass();
1685 
1686  impeller::TextureDescriptor y_texture_descriptor;
1687  y_texture_descriptor.storage_mode = impeller::StorageMode::kHostVisible;
1688  y_texture_descriptor.format = PixelFormat::kR8UNormInt;
1689  y_texture_descriptor.size = {8, 8};
1690  auto y_texture =
1691  context->GetResourceAllocator()->CreateTexture(y_texture_descriptor);
1692  auto y_mapping = std::make_shared<fml::DataMapping>(y_data);
1693  auto y_mapping_buffer =
1694  context->GetResourceAllocator()->CreateBufferWithCopy(*y_mapping);
1695 
1696  blit_pass->AddCopy(DeviceBuffer::AsBufferView(y_mapping_buffer), y_texture);
1697 
1698  impeller::TextureDescriptor uv_texture_descriptor;
1699  uv_texture_descriptor.storage_mode = impeller::StorageMode::kHostVisible;
1700  uv_texture_descriptor.format = PixelFormat::kR8G8UNormInt;
1701  uv_texture_descriptor.size = {4, 4};
1702  auto uv_texture =
1703  context->GetResourceAllocator()->CreateTexture(uv_texture_descriptor);
1704  auto uv_mapping = std::make_shared<fml::DataMapping>(uv_data);
1705  auto uv_mapping_buffer =
1706  context->GetResourceAllocator()->CreateBufferWithCopy(*uv_mapping);
1707 
1708  blit_pass->AddCopy(DeviceBuffer::AsBufferView(uv_mapping_buffer), uv_texture);
1709 
1710  if (!blit_pass->EncodeCommands() ||
1711  !context->GetCommandQueue()->Submit({cmd_buffer}).ok()) {
1712  FML_DLOG(ERROR) << "Could not copy contents into Y/UV texture.";
1713  }
1714 
1715  return {y_texture, uv_texture};
1716 }
1717 
1718 TEST_P(EntityTest, YUVToRGBFilter) {
1719  if (GetParam() == PlaygroundBackend::kOpenGLES) {
1720  // TODO(114588) : Support YUV to RGB filter on OpenGLES backend.
1721  GTEST_SKIP()
1722  << "YUV to RGB filter is not supported on OpenGLES backend yet.";
1723  }
1724 
1725  auto callback = [&](ContentContext& context, RenderPass& pass) -> bool {
1726  YUVColorSpace yuv_color_space_array[2]{YUVColorSpace::kBT601FullRange,
1728  for (int i = 0; i < 2; i++) {
1729  auto yuv_color_space = yuv_color_space_array[i];
1730  auto textures =
1731  CreateTestYUVTextures(GetContext().get(), yuv_color_space);
1732  auto filter_contents = FilterContents::MakeYUVToRGBFilter(
1733  textures[0], textures[1], yuv_color_space);
1734  Entity filter_entity;
1735  filter_entity.SetContents(filter_contents);
1736  auto snapshot = filter_contents->RenderToSnapshot(context, filter_entity);
1737 
1738  Entity entity;
1739  auto contents = TextureContents::MakeRect(Rect::MakeLTRB(0, 0, 256, 256));
1740  contents->SetTexture(snapshot->texture);
1741  contents->SetSourceRect(Rect::MakeSize(snapshot->texture->GetSize()));
1742  entity.SetContents(contents);
1743  entity.SetTransform(
1744  Matrix::MakeTranslation({static_cast<Scalar>(100 + 400 * i), 300}));
1745  entity.Render(context, pass);
1746  }
1747  return true;
1748  };
1749  ASSERT_TRUE(OpenPlaygroundHere(callback));
1750 }
1751 
1752 TEST_P(EntityTest, RuntimeEffect) {
1753  auto runtime_stages =
1754  OpenAssetAsRuntimeStage("runtime_stage_example.frag.iplr");
1755  auto runtime_stage =
1756  runtime_stages[PlaygroundBackendToRuntimeStageBackend(GetBackend())];
1757  ASSERT_TRUE(runtime_stage);
1758  ASSERT_TRUE(runtime_stage->IsDirty());
1759 
1760  bool expect_dirty = true;
1761 
1762  PipelineRef first_pipeline;
1763  std::unique_ptr<Geometry> geom = Geometry::MakeCover();
1764 
1765  auto callback = [&](ContentContext& context, RenderPass& pass) -> bool {
1766  EXPECT_EQ(runtime_stage->IsDirty(), expect_dirty);
1767 
1768  auto contents = std::make_shared<RuntimeEffectContents>();
1769  contents->SetGeometry(geom.get());
1770  contents->SetRuntimeStage(runtime_stage);
1771 
1772  struct FragUniforms {
1773  Vector2 iResolution;
1774  Scalar iTime;
1775  } frag_uniforms = {
1776  .iResolution = Vector2(GetWindowSize().width, GetWindowSize().height),
1777  .iTime = static_cast<Scalar>(GetSecondsElapsed()),
1778  };
1779  auto uniform_data = std::make_shared<std::vector<uint8_t>>();
1780  uniform_data->resize(sizeof(FragUniforms));
1781  memcpy(uniform_data->data(), &frag_uniforms, sizeof(FragUniforms));
1782  contents->SetUniformData(uniform_data);
1783 
1784  Entity entity;
1785  entity.SetContents(contents);
1786  bool result = contents->Render(context, entity, pass);
1787 
1788  if (expect_dirty) {
1789  first_pipeline = pass.GetCommands().back().pipeline;
1790  } else {
1791  EXPECT_EQ(pass.GetCommands().back().pipeline, first_pipeline);
1792  }
1793  expect_dirty = false;
1794  return result;
1795  };
1796 
1797  // Simulate some renders and hot reloading of the shader.
1798  auto content_context = GetContentContext();
1799  {
1800  RenderTarget target =
1801  content_context->GetRenderTargetCache()->CreateOffscreen(
1802  *content_context->GetContext(), {1, 1}, 1u);
1803 
1804  testing::MockRenderPass mock_pass(GetContext(), target);
1805  callback(*content_context, mock_pass);
1806  callback(*content_context, mock_pass);
1807 
1808  // Dirty the runtime stage.
1809  runtime_stages = OpenAssetAsRuntimeStage("runtime_stage_example.frag.iplr");
1810  runtime_stage =
1811  runtime_stages[PlaygroundBackendToRuntimeStageBackend(GetBackend())];
1812 
1813  ASSERT_TRUE(runtime_stage->IsDirty());
1814  expect_dirty = true;
1815 
1816  callback(*content_context, mock_pass);
1817  }
1818 }
1819 
1820 TEST_P(EntityTest, RuntimeEffectCanSuccessfullyRender) {
1821  auto runtime_stages =
1822  OpenAssetAsRuntimeStage("runtime_stage_example.frag.iplr");
1823  auto runtime_stage =
1824  runtime_stages[PlaygroundBackendToRuntimeStageBackend(GetBackend())];
1825  ASSERT_TRUE(runtime_stage);
1826  ASSERT_TRUE(runtime_stage->IsDirty());
1827 
1828  auto contents = std::make_shared<RuntimeEffectContents>();
1829  auto geom = Geometry::MakeCover();
1830  contents->SetGeometry(geom.get());
1831  contents->SetRuntimeStage(runtime_stage);
1832 
1833  struct FragUniforms {
1834  Vector2 iResolution;
1835  Scalar iTime;
1836  } frag_uniforms = {
1837  .iResolution = Vector2(GetWindowSize().width, GetWindowSize().height),
1838  .iTime = static_cast<Scalar>(GetSecondsElapsed()),
1839  };
1840  auto uniform_data = std::make_shared<std::vector<uint8_t>>();
1841  uniform_data->resize(sizeof(FragUniforms));
1842  memcpy(uniform_data->data(), &frag_uniforms, sizeof(FragUniforms));
1843  contents->SetUniformData(uniform_data);
1844 
1845  Entity entity;
1846  entity.SetContents(contents);
1847 
1848  // Create a render target with a depth-stencil, similar to how EntityPass
1849  // does.
1850  RenderTarget target =
1851  GetContentContext()->GetRenderTargetCache()->CreateOffscreenMSAA(
1852  *GetContext(), {GetWindowSize().width, GetWindowSize().height}, 1,
1853  "RuntimeEffect Texture");
1854  testing::MockRenderPass pass(GetContext(), target);
1855 
1856  ASSERT_TRUE(contents->Render(*GetContentContext(), entity, pass));
1857  ASSERT_EQ(pass.GetCommands().size(), 1u);
1858  const auto& command = pass.GetCommands()[0];
1859  ASSERT_TRUE(command.pipeline->GetDescriptor()
1860  .GetDepthStencilAttachmentDescriptor()
1861  .has_value());
1862  ASSERT_TRUE(command.pipeline->GetDescriptor()
1863  .GetFrontStencilAttachmentDescriptor()
1864  .has_value());
1865 }
1866 
1867 TEST_P(EntityTest, RuntimeEffectCanPrecache) {
1868  auto runtime_stages =
1869  OpenAssetAsRuntimeStage("runtime_stage_example.frag.iplr");
1870  auto runtime_stage =
1871  runtime_stages[PlaygroundBackendToRuntimeStageBackend(GetBackend())];
1872  ASSERT_TRUE(runtime_stage);
1873  ASSERT_TRUE(runtime_stage->IsDirty());
1874 
1875  auto contents = std::make_shared<RuntimeEffectContents>();
1876  contents->SetRuntimeStage(runtime_stage);
1877 
1878  EXPECT_TRUE(contents->BootstrapShader(*GetContentContext()));
1879 }
1880 
1881 TEST_P(EntityTest, RuntimeEffectSetsRightSizeWhenUniformIsStruct) {
1882  if (GetBackend() != PlaygroundBackend::kVulkan) {
1883  GTEST_SKIP() << "Test only applies to Vulkan";
1884  }
1885 
1886  auto runtime_stages =
1887  OpenAssetAsRuntimeStage("runtime_stage_example.frag.iplr");
1888  auto runtime_stage =
1889  runtime_stages[PlaygroundBackendToRuntimeStageBackend(GetBackend())];
1890  ASSERT_TRUE(runtime_stage);
1891  ASSERT_TRUE(runtime_stage->IsDirty());
1892 
1893  auto contents = std::make_shared<RuntimeEffectContents>();
1894  auto geom = Geometry::MakeCover();
1895  contents->SetGeometry(geom.get());
1896  contents->SetRuntimeStage(runtime_stage);
1897 
1898  struct FragUniforms {
1899  Vector2 iResolution;
1900  Scalar iTime;
1901  } frag_uniforms = {
1902  .iResolution = Vector2(GetWindowSize().width, GetWindowSize().height),
1903  .iTime = static_cast<Scalar>(GetSecondsElapsed()),
1904  };
1905  auto uniform_data = std::make_shared<std::vector<uint8_t>>();
1906  uniform_data->resize(sizeof(FragUniforms));
1907  memcpy(uniform_data->data(), &frag_uniforms, sizeof(FragUniforms));
1908 
1910  uniform_data, GetContentContext()->GetTransientsBuffer(),
1911  runtime_stage->GetUniforms()[0],
1912  GetContentContext()->GetTransientsBuffer().GetMinimumUniformAlignment());
1913 
1914  // 16 bytes:
1915  // 8 bytes for iResolution
1916  // 4 bytes for iTime
1917  // 4 bytes padding
1918  EXPECT_EQ(buffer_view.GetRange().length, 16u);
1919 }
1920 
1921 TEST_P(EntityTest, ColorFilterWithForegroundColorAdvancedBlend) {
1922  auto image = CreateTextureForFixture("boston.jpg");
1923  auto filter = ColorFilterContents::MakeBlend(
1925 
1926  auto callback = [&](ContentContext& context, RenderPass& pass) -> bool {
1927  Entity entity;
1928  entity.SetTransform(Matrix::MakeScale(GetContentScale()) *
1929  Matrix::MakeTranslation({500, 300}) *
1930  Matrix::MakeScale(Vector2{0.5, 0.5}));
1931  entity.SetContents(filter);
1932  return entity.Render(context, pass);
1933  };
1934  ASSERT_TRUE(OpenPlaygroundHere(callback));
1935 }
1936 
1937 TEST_P(EntityTest, ColorFilterWithForegroundColorClearBlend) {
1938  auto image = CreateTextureForFixture("boston.jpg");
1939  auto filter = ColorFilterContents::MakeBlend(
1941 
1942  auto callback = [&](ContentContext& context, RenderPass& pass) -> bool {
1943  Entity entity;
1944  entity.SetTransform(Matrix::MakeScale(GetContentScale()) *
1945  Matrix::MakeTranslation({500, 300}) *
1946  Matrix::MakeScale(Vector2{0.5, 0.5}));
1947  entity.SetContents(filter);
1948  return entity.Render(context, pass);
1949  };
1950  ASSERT_TRUE(OpenPlaygroundHere(callback));
1951 }
1952 
1953 TEST_P(EntityTest, ColorFilterWithForegroundColorSrcBlend) {
1954  auto image = CreateTextureForFixture("boston.jpg");
1955  auto filter = ColorFilterContents::MakeBlend(
1957 
1958  auto callback = [&](ContentContext& context, RenderPass& pass) -> bool {
1959  Entity entity;
1960  entity.SetTransform(Matrix::MakeScale(GetContentScale()) *
1961  Matrix::MakeTranslation({500, 300}) *
1962  Matrix::MakeScale(Vector2{0.5, 0.5}));
1963  entity.SetContents(filter);
1964  return entity.Render(context, pass);
1965  };
1966  ASSERT_TRUE(OpenPlaygroundHere(callback));
1967 }
1968 
1969 TEST_P(EntityTest, ColorFilterWithForegroundColorDstBlend) {
1970  auto image = CreateTextureForFixture("boston.jpg");
1971  auto filter = ColorFilterContents::MakeBlend(
1973 
1974  auto callback = [&](ContentContext& context, RenderPass& pass) -> bool {
1975  Entity entity;
1976  entity.SetTransform(Matrix::MakeScale(GetContentScale()) *
1977  Matrix::MakeTranslation({500, 300}) *
1978  Matrix::MakeScale(Vector2{0.5, 0.5}));
1979  entity.SetContents(filter);
1980  return entity.Render(context, pass);
1981  };
1982  ASSERT_TRUE(OpenPlaygroundHere(callback));
1983 }
1984 
1985 TEST_P(EntityTest, ColorFilterWithForegroundColorSrcInBlend) {
1986  auto image = CreateTextureForFixture("boston.jpg");
1987  auto filter = ColorFilterContents::MakeBlend(
1989 
1990  auto callback = [&](ContentContext& context, RenderPass& pass) -> bool {
1991  Entity entity;
1992  entity.SetTransform(Matrix::MakeScale(GetContentScale()) *
1993  Matrix::MakeTranslation({500, 300}) *
1994  Matrix::MakeScale(Vector2{0.5, 0.5}));
1995  entity.SetContents(filter);
1996  return entity.Render(context, pass);
1997  };
1998  ASSERT_TRUE(OpenPlaygroundHere(callback));
1999 }
2000 
2001 TEST_P(EntityTest, CoverageForStrokePathWithNegativeValuesInTransform) {
2002  auto arrow_head = flutter::DlPathBuilder{}
2003  .MoveTo({50, 120})
2004  .LineTo({120, 190})
2005  .LineTo({190, 120})
2006  .TakePath();
2007  auto geometry = Geometry::MakeStrokePath(arrow_head, //
2008  {
2009  .width = 15.0f,
2010  .cap = Cap::kRound,
2011  .join = Join::kRound,
2012  .miter_limit = 4.0f,
2013  });
2014 
2015  auto transform = Matrix::MakeTranslation({300, 300}) *
2017  // Note that e[0][0] used to be tested here, but it was -epsilon solely
2018  // due to floating point inaccuracy in the transcendental trig functions.
2019  // e[1][0] is the intended negative value that we care about (-1.0) as it
2020  // comes from the rotation of pi/2.
2021  EXPECT_LT(transform.e[1][0], 0.0f);
2022  auto coverage = geometry->GetCoverage(transform);
2023  ASSERT_RECT_NEAR(coverage.value(), Rect::MakeXYWH(102.5, 342.5, 85, 155));
2024 }
2025 
2026 TEST_P(EntityTest, SolidColorContentsIsOpaque) {
2027  Matrix matrix;
2028  SolidColorContents contents;
2029  auto geom = Geometry::MakeRect(Rect::MakeLTRB(0, 0, 10, 10));
2030  contents.SetGeometry(geom.get());
2031 
2032  contents.SetColor(Color::CornflowerBlue());
2033  EXPECT_TRUE(contents.IsOpaque(matrix));
2034  contents.SetColor(Color::CornflowerBlue().WithAlpha(0.5));
2035  EXPECT_FALSE(contents.IsOpaque(matrix));
2036 
2037  // Create stroked path that required alpha coverage.
2038  geom = Geometry::MakeStrokePath(flutter::DlPath::MakeLine({0, 0}, {100, 100}),
2039  {.width = 0.05});
2040  contents.SetGeometry(geom.get());
2041  contents.SetColor(Color::CornflowerBlue());
2042 
2043  EXPECT_FALSE(contents.IsOpaque(matrix));
2044 }
2045 
2046 TEST_P(EntityTest, ConicalGradientContentsIsOpaque) {
2047  Matrix matrix;
2048  ConicalGradientContents contents;
2049  auto geom = Geometry::MakeRect(Rect::MakeLTRB(0, 0, 10, 10));
2050  contents.SetGeometry(geom.get());
2051 
2052  contents.SetColors({Color::CornflowerBlue()});
2053  EXPECT_FALSE(contents.IsOpaque(matrix));
2054  contents.SetColors({Color::CornflowerBlue().WithAlpha(0.5)});
2055  EXPECT_FALSE(contents.IsOpaque(matrix));
2056 
2057  // Create stroked path that required alpha coverage.
2058  geom = Geometry::MakeStrokePath(
2059  flutter::DlPathBuilder{}.MoveTo({0, 0}).LineTo({100, 100}).TakePath(),
2060  {.width = 0.05f});
2061  contents.SetGeometry(geom.get());
2062  contents.SetColors({Color::CornflowerBlue()});
2063 
2064  EXPECT_FALSE(contents.IsOpaque(matrix));
2065 }
2066 
2067 TEST_P(EntityTest, LinearGradientContentsIsOpaque) {
2068  Matrix matrix;
2069  LinearGradientContents contents;
2070  auto geom = Geometry::MakeRect(Rect::MakeLTRB(0, 0, 10, 10));
2071  contents.SetGeometry(geom.get());
2072 
2073  contents.SetColors({Color::CornflowerBlue()});
2074  EXPECT_TRUE(contents.IsOpaque(matrix));
2075  contents.SetColors({Color::CornflowerBlue().WithAlpha(0.5)});
2076  EXPECT_FALSE(contents.IsOpaque(matrix));
2077  contents.SetColors({Color::CornflowerBlue()});
2079  EXPECT_FALSE(contents.IsOpaque(matrix));
2080 
2081  // Create stroked path that required alpha coverage.
2082  geom = Geometry::MakeStrokePath(
2083  flutter::DlPathBuilder{}.MoveTo({0, 0}).LineTo({100, 100}).TakePath(),
2084  {.width = 0.05f});
2085  contents.SetGeometry(geom.get());
2086  contents.SetColors({Color::CornflowerBlue()});
2087 
2088  EXPECT_FALSE(contents.IsOpaque(matrix));
2089 }
2090 
2091 TEST_P(EntityTest, RadialGradientContentsIsOpaque) {
2092  Matrix matrix;
2093  RadialGradientContents contents;
2094  auto geom = Geometry::MakeRect(Rect::MakeLTRB(0, 0, 10, 10));
2095  contents.SetGeometry(geom.get());
2096 
2097  contents.SetColors({Color::CornflowerBlue()});
2098  EXPECT_TRUE(contents.IsOpaque(matrix));
2099  contents.SetColors({Color::CornflowerBlue().WithAlpha(0.5)});
2100  EXPECT_FALSE(contents.IsOpaque(matrix));
2101  contents.SetColors({Color::CornflowerBlue()});
2103  EXPECT_FALSE(contents.IsOpaque(matrix));
2104 
2105  // Create stroked path that required alpha coverage.
2106  geom = Geometry::MakeStrokePath(
2107  flutter::DlPathBuilder{}.MoveTo({0, 0}).LineTo({100, 100}).TakePath(),
2108  {.width = 0.05});
2109  contents.SetGeometry(geom.get());
2110  contents.SetColors({Color::CornflowerBlue()});
2111 
2112  EXPECT_FALSE(contents.IsOpaque(matrix));
2113 }
2114 
2115 TEST_P(EntityTest, SweepGradientContentsIsOpaque) {
2116  Matrix matrix;
2117  RadialGradientContents contents;
2118  auto geom = Geometry::MakeRect(Rect::MakeLTRB(0, 0, 10, 10));
2119  contents.SetGeometry(geom.get());
2120 
2121  contents.SetColors({Color::CornflowerBlue()});
2122  EXPECT_TRUE(contents.IsOpaque(matrix));
2123  contents.SetColors({Color::CornflowerBlue().WithAlpha(0.5)});
2124  EXPECT_FALSE(contents.IsOpaque(matrix));
2125  contents.SetColors({Color::CornflowerBlue()});
2127  EXPECT_FALSE(contents.IsOpaque(matrix));
2128 
2129  // Create stroked path that required alpha coverage.
2130  geom = Geometry::MakeStrokePath(
2131  flutter::DlPathBuilder{}.MoveTo({0, 0}).LineTo({100, 100}).TakePath(),
2132  {.width = 0.05f});
2133  contents.SetGeometry(geom.get());
2134  contents.SetColors({Color::CornflowerBlue()});
2135 
2136  EXPECT_FALSE(contents.IsOpaque(matrix));
2137 }
2138 
2139 TEST_P(EntityTest, TiledTextureContentsIsOpaque) {
2140  Matrix matrix;
2141  auto bay_bridge = CreateTextureForFixture("bay_bridge.jpg");
2142  TiledTextureContents contents;
2143  contents.SetTexture(bay_bridge);
2144  // This is a placeholder test. Images currently never decompress as opaque
2145  // (whether in Flutter or the playground), and so this should currently always
2146  // return false in practice.
2147  EXPECT_FALSE(contents.IsOpaque(matrix));
2148 }
2149 
2150 TEST_P(EntityTest, PointFieldGeometryCoverage) {
2151  std::vector<Point> points = {{10, 20}, {100, 200}};
2152  PointFieldGeometry geometry(points.data(), 2, 5.0, false);
2153  ASSERT_EQ(geometry.GetCoverage(Matrix()), Rect::MakeLTRB(5, 15, 105, 205));
2154  ASSERT_EQ(geometry.GetCoverage(Matrix::MakeTranslation({30, 0, 0})),
2155  Rect::MakeLTRB(35, 15, 135, 205));
2156 }
2157 
2158 TEST_P(EntityTest, ColorFilterContentsWithLargeGeometry) {
2159  Entity entity;
2160  entity.SetTransform(Matrix::MakeScale(GetContentScale()));
2161  auto src_contents = std::make_shared<SolidColorContents>();
2162  auto src_geom = Geometry::MakeRect(Rect::MakeLTRB(-300, -500, 30000, 50000));
2163  src_contents->SetGeometry(src_geom.get());
2164  src_contents->SetColor(Color::Red());
2165 
2166  auto dst_contents = std::make_shared<SolidColorContents>();
2167  auto dst_geom = Geometry::MakeRect(Rect::MakeLTRB(300, 500, 20000, 30000));
2168  dst_contents->SetGeometry(dst_geom.get());
2169  dst_contents->SetColor(Color::Blue());
2170 
2171  auto contents = ColorFilterContents::MakeBlend(
2172  BlendMode::kSrcOver, {FilterInput::Make(dst_contents, false),
2173  FilterInput::Make(src_contents, false)});
2174  entity.SetContents(std::move(contents));
2175  ASSERT_TRUE(OpenPlaygroundHere(std::move(entity)));
2176 }
2177 
2178 TEST_P(EntityTest, TextContentsCeilsGlyphScaleToDecimal) {
2179  ASSERT_EQ(TextFrame::RoundScaledFontSize(0.4321111f), Rational(43, 100));
2180  ASSERT_EQ(TextFrame::RoundScaledFontSize(0.5321111f), Rational(53, 100));
2181  ASSERT_EQ(TextFrame::RoundScaledFontSize(2.1f), Rational(21, 10));
2182  ASSERT_EQ(TextFrame::RoundScaledFontSize(0.0f), Rational(0, 1));
2183  ASSERT_EQ(TextFrame::RoundScaledFontSize(100000000.0f), Rational(48, 1));
2184 }
2185 
2186 TEST_P(EntityTest, SpecializationConstantsAreAppliedToVariants) {
2187  auto content_context = GetContentContext();
2188 
2189  auto default_gyph = content_context->GetGlyphAtlasPipeline({
2190  .color_attachment_pixel_format = PixelFormat::kR8G8B8A8UNormInt,
2191  .has_depth_stencil_attachments = false,
2192  });
2193  auto alt_gyph = content_context->GetGlyphAtlasPipeline(
2194  {.color_attachment_pixel_format = PixelFormat::kR8G8B8A8UNormInt,
2195  .has_depth_stencil_attachments = true});
2196 
2197  EXPECT_NE(default_gyph, alt_gyph);
2198  EXPECT_EQ(default_gyph->GetDescriptor().GetSpecializationConstants(),
2199  alt_gyph->GetDescriptor().GetSpecializationConstants());
2200 
2201  auto use_a8 = GetContext()->GetCapabilities()->GetDefaultGlyphAtlasFormat() ==
2203 
2204  std::vector<Scalar> expected_constants = {static_cast<Scalar>(use_a8)};
2205  EXPECT_EQ(default_gyph->GetDescriptor().GetSpecializationConstants(),
2206  expected_constants);
2207 }
2208 
2209 TEST_P(EntityTest, DecalSpecializationAppliedToMorphologyFilter) {
2210  auto content_context = GetContentContext();
2211  auto default_color_burn = content_context->GetMorphologyFilterPipeline({
2212  .color_attachment_pixel_format = PixelFormat::kR8G8B8A8UNormInt,
2213  });
2214 
2215  auto decal_supported = static_cast<Scalar>(
2216  GetContext()->GetCapabilities()->SupportsDecalSamplerAddressMode());
2217  std::vector<Scalar> expected_constants = {decal_supported};
2218  ASSERT_EQ(default_color_burn->GetDescriptor().GetSpecializationConstants(),
2219  expected_constants);
2220 }
2221 
2222 // This doesn't really tell you if the hashes will have frequent
2223 // collisions, but since this type is only used to hash a bounded
2224 // set of options, we can just compare benchmarks.
2225 TEST_P(EntityTest, ContentContextOptionsHasReasonableHashFunctions) {
2226  ContentContextOptions opts;
2227  auto hash_a = opts.ToKey();
2228 
2230  auto hash_b = opts.ToKey();
2231 
2232  opts.has_depth_stencil_attachments = false;
2233  auto hash_c = opts.ToKey();
2234 
2236  auto hash_d = opts.ToKey();
2237 
2238  EXPECT_NE(hash_a, hash_b);
2239  EXPECT_NE(hash_b, hash_c);
2240  EXPECT_NE(hash_c, hash_d);
2241 }
2242 
2243 #ifdef FML_OS_LINUX
2244 TEST_P(EntityTest, FramebufferFetchVulkanBindingOffsetIsTheSame) {
2245  // Using framebuffer fetch on Vulkan requires that we maintain a subpass input
2246  // binding that we don't have a good route for configuring with the
2247  // current metadata approach. This test verifies that the binding value
2248  // doesn't change
2249  // from the expected constant.
2250  // See also:
2251  // * impeller/renderer/backend/vulkan/binding_helpers_vk.cc
2252  // * impeller/entity/shaders/blending/framebuffer_blend.frag
2253  // This test only works on Linux because macOS hosts incorrectly
2254  // populate the
2255  // Vulkan descriptor sets based on the MSL compiler settings.
2256 
2257  bool expected_layout = false;
2259  FragmentShader::kDescriptorSetLayouts) {
2260  if (layout.binding == 64 &&
2261  layout.descriptor_type == DescriptorType::kInputAttachment) {
2262  expected_layout = true;
2263  }
2264  }
2265  EXPECT_TRUE(expected_layout);
2266 }
2267 #endif
2268 
2269 TEST_P(EntityTest, FillPathGeometryGetPositionBufferReturnsExpectedMode) {
2270  RenderTarget target;
2271  testing::MockRenderPass mock_pass(GetContext(), target);
2272 
2273  auto get_result = [this, &mock_pass](const flutter::DlPath& path) {
2274  auto geometry = Geometry::MakeFillPath(
2275  path, /* inner rect */ Rect::MakeLTRB(0, 0, 100, 100));
2276  return geometry->GetPositionBuffer(*GetContentContext(), {}, mock_pass);
2277  };
2278 
2279  // Convex path
2280  {
2281  GeometryResult result =
2282  get_result(flutter::DlPath::MakeRect(Rect::MakeLTRB(0, 0, 100, 100)));
2283  EXPECT_EQ(result.mode, GeometryResult::Mode::kNormal);
2284  }
2285 
2286  // Concave path
2287  {
2288  flutter::DlPath path = flutter::DlPathBuilder{}
2289  .MoveTo({0, 0})
2290  .LineTo({100, 0})
2291  .LineTo({100, 100})
2292  .LineTo({51, 50})
2293  .Close()
2294  .TakePath();
2295  GeometryResult result = get_result(path);
2296  EXPECT_EQ(result.mode, GeometryResult::Mode::kNonZero);
2297  }
2298 }
2299 
2300 TEST_P(EntityTest, FailOnValidationError) {
2301  if (GetParam() != PlaygroundBackend::kVulkan) {
2302  GTEST_SKIP() << "Validation is only fatal on Vulkan backend.";
2303  }
2304  EXPECT_DEATH(
2305  // The easiest way to trigger a validation error is to try to compile
2306  // a shader with an unsupported pixel format.
2307  GetContentContext()->GetBlendColorBurnPipeline({
2308  .color_attachment_pixel_format = PixelFormat::kUnknown,
2309  .has_depth_stencil_attachments = false,
2310  }),
2311  "");
2312 }
2313 
2314 TEST_P(EntityTest, CanComputeGeometryForEmptyPathsWithoutCrashing) {
2315  flutter::DlPath path = flutter::DlPath::MakeRect(Rect::MakeLTRB(0, 0, 0, 0));
2316 
2317  EXPECT_TRUE(path.GetBounds().IsEmpty());
2318 
2319  auto geom = Geometry::MakeFillPath(path);
2320 
2321  Entity entity;
2322  RenderTarget target =
2323  GetContentContext()->GetRenderTargetCache()->CreateOffscreen(
2324  *GetContext(), {1, 1}, 1u);
2325  testing::MockRenderPass render_pass(GetContext(), target);
2326  auto position_result =
2327  geom->GetPositionBuffer(*GetContentContext(), entity, render_pass);
2328 
2329  EXPECT_EQ(position_result.vertex_buffer.vertex_count, 0u);
2330 
2331  EXPECT_EQ(geom->GetResultMode(), GeometryResult::Mode::kNormal);
2332 }
2333 
2334 TEST_P(EntityTest, CanRenderEmptyPathsWithoutCrashing) {
2335  flutter::DlPath path = flutter::DlPath::MakeRect(Rect::MakeLTRB(0, 0, 0, 0));
2336 
2337  EXPECT_TRUE(path.GetBounds().IsEmpty());
2338 
2339  auto contents = std::make_shared<SolidColorContents>();
2340  std::unique_ptr<Geometry> geom = Geometry::MakeFillPath(path);
2341  contents->SetGeometry(geom.get());
2342  contents->SetColor(Color::Red());
2343 
2344  Entity entity;
2345  entity.SetTransform(Matrix::MakeScale(GetContentScale()));
2346  entity.SetContents(contents);
2347 
2348  ASSERT_TRUE(OpenPlaygroundHere(std::move(entity)));
2349 }
2350 
2351 TEST_P(EntityTest, DrawSuperEllipse) {
2352  auto callback = [&](ContentContext& context, RenderPass& pass) -> bool {
2353  // UI state.
2354  static float alpha = 10;
2355  static float beta = 10;
2356  static float radius = 40;
2357  static int degree = 4;
2358  static Color color = Color::Red();
2359 
2360  ImGui::Begin("Controls", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
2361  ImGui::SliderFloat("Alpha", &alpha, 0, 100);
2362  ImGui::SliderFloat("Beta", &beta, 0, 100);
2363  ImGui::SliderInt("Degreee", &degree, 1, 20);
2364  ImGui::SliderFloat("Radius", &radius, 0, 400);
2365  ImGui::ColorEdit4("Color", reinterpret_cast<float*>(&color));
2366  ImGui::End();
2367 
2368  auto contents = std::make_shared<SolidColorContents>();
2369  std::unique_ptr<SuperellipseGeometry> geom =
2370  std::make_unique<SuperellipseGeometry>(Point{400, 400}, radius, degree,
2371  alpha, beta);
2372  contents->SetColor(color);
2373  contents->SetGeometry(geom.get());
2374 
2375  Entity entity;
2376  entity.SetContents(contents);
2377 
2378  return entity.Render(context, pass);
2379  };
2380 
2381  ASSERT_TRUE(OpenPlaygroundHere(callback));
2382 }
2383 
2384 TEST_P(EntityTest, DrawRoundSuperEllipse) {
2385  auto callback = [&](ContentContext& context, RenderPass& pass) -> bool {
2386  // UI state.
2387  static int style_index = 0;
2388  static float center[2] = {830, 830};
2389  static float size[2] = {600, 600};
2390  static bool horizontal_symmetry = true;
2391  static bool vertical_symmetry = true;
2392  static bool corner_symmetry = true;
2393 
2394  const char* style_options[] = {"Fill", "Stroke"};
2395 
2396  // Initially radius_tl[0] will be mirrored to all 8 values since all 3
2397  // symmetries are enabled.
2398  static std::array<float, 2> radius_tl = {200};
2399  static std::array<float, 2> radius_tr;
2400  static std::array<float, 2> radius_bl;
2401  static std::array<float, 2> radius_br;
2402 
2403  auto AddRadiusControl = [](std::array<float, 2>& radii, const char* tb_name,
2404  const char* lr_name) {
2405  std::string name = "Radius";
2406  if (!horizontal_symmetry || !vertical_symmetry) {
2407  name += ":";
2408  }
2409  if (!vertical_symmetry) {
2410  name = name + " " + tb_name;
2411  }
2412  if (!horizontal_symmetry) {
2413  name = name + " " + lr_name;
2414  }
2415  if (corner_symmetry) {
2416  ImGui::SliderFloat(name.c_str(), radii.data(), 0, 1000);
2417  } else {
2418  ImGui::SliderFloat2(name.c_str(), radii.data(), 0, 1000);
2419  }
2420  };
2421 
2422  if (corner_symmetry) {
2423  radius_tl[1] = radius_tl[0];
2424  radius_tr[1] = radius_tr[0];
2425  radius_bl[1] = radius_bl[0];
2426  radius_br[1] = radius_br[0];
2427  }
2428 
2429  ImGui::Begin("Controls", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
2430  {
2431  ImGui::Combo("Style", &style_index, style_options,
2432  sizeof(style_options) / sizeof(char*));
2433  ImGui::SliderFloat2("Center", center, 0, 1000);
2434  ImGui::SliderFloat2("Size", size, 0, 1000);
2435  ImGui::Checkbox("Symmetry: Horizontal", &horizontal_symmetry);
2436  ImGui::Checkbox("Symmetry: Vertical", &vertical_symmetry);
2437  ImGui::Checkbox("Symmetry: Corners", &corner_symmetry);
2438  AddRadiusControl(radius_tl, "Top", "Left");
2439  if (!horizontal_symmetry) {
2440  AddRadiusControl(radius_tr, "Top", "Right");
2441  } else {
2442  radius_tr = radius_tl;
2443  }
2444  if (!vertical_symmetry) {
2445  AddRadiusControl(radius_bl, "Bottom", "Left");
2446  } else {
2447  radius_bl = radius_tl;
2448  }
2449  if (!horizontal_symmetry && !vertical_symmetry) {
2450  AddRadiusControl(radius_br, "Bottom", "Right");
2451  } else {
2452  if (horizontal_symmetry) {
2453  radius_br = radius_bl;
2454  } else {
2455  radius_br = radius_tr;
2456  }
2457  }
2458  }
2459 
2460  ImGui::End();
2461 
2462  RoundingRadii radii{
2463  .top_left = {radius_tl[0], radius_tl[1]},
2464  .top_right = {radius_tr[0], radius_tr[1]},
2465  .bottom_left = {radius_bl[0], radius_bl[1]},
2466  .bottom_right = {radius_br[0], radius_br[1]},
2467  };
2468 
2470  RectMakeCenterSize({center[0], center[1]}, {size[0], size[1]}), radii);
2471 
2472  flutter::DlPath path;
2473  std::unique_ptr<Geometry> geom;
2474  if (style_index == 0) {
2475  geom = std::make_unique<RoundSuperellipseGeometry>(
2476  RectMakeCenterSize({center[0], center[1]}, {size[0], size[1]}),
2477  radii);
2478  } else {
2479  path = flutter::DlPath::MakeRoundSuperellipse(rse);
2480  geom = Geometry::MakeStrokePath(path, {.width = 2.0f});
2481  }
2482 
2483  auto contents = std::make_shared<SolidColorContents>();
2484  contents->SetColor(Color::Red());
2485  contents->SetGeometry(geom.get());
2486 
2487  Entity entity;
2488  entity.SetContents(contents);
2489 
2490  return entity.Render(context, pass);
2491  };
2492 
2493  ASSERT_TRUE(OpenPlaygroundHere(callback));
2494 }
2495 
2496 TEST_P(EntityTest, SolidColorApplyColorFilter) {
2497  auto contents = SolidColorContents();
2498  contents.SetColor(Color::CornflowerBlue().WithAlpha(0.75));
2499  auto result = contents.ApplyColorFilter([](const Color& color) {
2500  return color.Blend(Color::LimeGreen().WithAlpha(0.75), BlendMode::kScreen);
2501  });
2502  ASSERT_TRUE(result);
2503  ASSERT_COLOR_NEAR(contents.GetColor(),
2504  Color(0.424452, 0.828743, 0.79105, 0.9375));
2505 }
2506 
2507 #define APPLY_COLOR_FILTER_GRADIENT_TEST(name) \
2508  TEST_P(EntityTest, name##GradientApplyColorFilter) { \
2509  auto contents = name##GradientContents(); \
2510  contents.SetColors({Color::CornflowerBlue().WithAlpha(0.75)}); \
2511  auto result = contents.ApplyColorFilter([](const Color& color) { \
2512  return color.Blend(Color::LimeGreen().WithAlpha(0.75), \
2513  BlendMode::kScreen); \
2514  }); \
2515  ASSERT_TRUE(result); \
2516  \
2517  std::vector<Color> expected = {Color(0.433247, 0.879523, 0.825324, 0.75)}; \
2518  ASSERT_COLORS_NEAR(contents.GetColors(), expected); \
2519  }
2520 
2525 
2526 TEST_P(EntityTest, GiantStrokePathAllocation) {
2527  flutter::DlPathBuilder builder;
2528  for (int i = 0; i < 10000; i++) {
2529  builder.LineTo(Point(i, i));
2530  }
2531  flutter::DlPath path = builder.TakePath();
2532  auto geom = Geometry::MakeStrokePath(path, {.width = 10.0f});
2533 
2534  ContentContext content_context(GetContext(), /*typographer_context=*/nullptr);
2535  Entity entity;
2536 
2537  auto cmd_buffer = content_context.GetContext()->CreateCommandBuffer();
2538 
2539  RenderTargetAllocator allocator(
2540  content_context.GetContext()->GetResourceAllocator());
2541 
2542  auto render_target = allocator.CreateOffscreen(
2543  *content_context.GetContext(), /*size=*/{10, 10}, /*mip_count=*/1);
2544  auto pass = cmd_buffer->CreateRenderPass(render_target);
2545 
2546  GeometryResult result =
2547  geom->GetPositionBuffer(content_context, entity, *pass);
2548 
2549  // Validate the buffer data overflowed the small buffer
2550  EXPECT_GT(result.vertex_buffer.vertex_count, kPointArenaSize);
2551 
2552  // Validate that there are no uninitialized points near the gap.
2553  Point* written_data = reinterpret_cast<Point*>(
2556 
2557  std::vector<Point> expected = {
2558  Point(2043.46, 2050.54), //
2559  Point(2050.54, 2043.46), //
2560  Point(2044.46, 2051.54), //
2561  Point(2051.54, 2044.46), //
2562  Point(2045.46, 2052.54) //
2563  };
2564 
2565  Point point = written_data[kPointArenaSize - 2];
2566  EXPECT_NEAR(point.x, expected[0].x, 0.1);
2567  EXPECT_NEAR(point.y, expected[0].y, 0.1);
2568 
2569  point = written_data[kPointArenaSize - 1];
2570  EXPECT_NEAR(point.x, expected[1].x, 0.1);
2571  EXPECT_NEAR(point.y, expected[1].y, 0.1);
2572 
2573  point = written_data[kPointArenaSize];
2574  EXPECT_NEAR(point.x, expected[2].x, 0.1);
2575  EXPECT_NEAR(point.y, expected[2].y, 0.1);
2576 
2577  point = written_data[kPointArenaSize + 1];
2578  EXPECT_NEAR(point.x, expected[3].x, 0.1);
2579  EXPECT_NEAR(point.y, expected[3].y, 0.1);
2580 
2581  point = written_data[kPointArenaSize + 2];
2582  EXPECT_NEAR(point.x, expected[4].x, 0.1);
2583  EXPECT_NEAR(point.y, expected[4].y, 0.1);
2584 }
2585 
2587  public:
2589  : DeviceBuffer(desc), storage_(desc.size) {}
2590 
2591  bool SetLabel(std::string_view label) override { return true; }
2592  bool SetLabel(std::string_view label, Range range) override { return true; }
2593  bool OnCopyHostBuffer(const uint8_t* source,
2594  Range source_range,
2595  size_t offset) {
2596  return true;
2597  }
2598 
2599  uint8_t* OnGetContents() const override {
2600  return const_cast<uint8_t*>(storage_.data());
2601  }
2602 
2603  void Flush(std::optional<Range> range) const override {
2604  flush_called_ = true;
2605  }
2606 
2607  bool flush_called() const { return flush_called_; }
2608 
2609  private:
2610  std::vector<uint8_t> storage_;
2611  mutable bool flush_called_ = false;
2612 };
2613 
2615  public:
2617  return ISize(1024, 1024);
2618  };
2619 
2620  std::shared_ptr<DeviceBuffer> OnCreateBuffer(
2621  const DeviceBufferDescriptor& desc) override {
2622  return std::make_shared<FlushTestDeviceBuffer>(desc);
2623  };
2624 
2625  std::shared_ptr<Texture> OnCreateTexture(
2626  const TextureDescriptor& desc) override {
2627  return nullptr;
2628  }
2629 };
2630 
2632  public:
2634  const std::shared_ptr<Context>& context,
2635  const std::shared_ptr<TypographerContext>& typographer_context,
2636  const std::shared_ptr<Allocator>& allocator)
2637  : ContentContext(context, typographer_context) {
2639  allocator, context->GetIdleWaiter(),
2640  context->GetCapabilities()->GetMinimumUniformAlignment()));
2641  }
2642 };
2643 
2644 TEST_P(EntityTest, RoundSuperellipseGetPositionBufferFlushes) {
2645  RenderTarget target;
2646  testing::MockRenderPass mock_pass(GetContext(), target);
2647 
2648  auto content_context = std::make_shared<FlushTestContentContext>(
2649  GetContext(), GetTypographerContext(),
2650  std::make_shared<FlushTestAllocator>());
2651  auto geometry =
2652  Geometry::MakeRoundSuperellipse(Rect::MakeLTRB(0, 0, 100, 100), 5);
2653  auto result = geometry->GetPositionBuffer(*content_context, {}, mock_pass);
2654 
2655  auto device_buffer = reinterpret_cast<const FlushTestDeviceBuffer*>(
2656  result.vertex_buffer.vertex_buffer.GetBuffer());
2657  EXPECT_TRUE(device_buffer->flush_called());
2658 }
2659 
2660 } // namespace testing
2661 } // namespace impeller
2662 
2663 // NOLINTEND(bugprone-unchecked-optional-access)
BufferView buffer_view
An object that allocates device memory.
Definition: allocator.h:24
static std::shared_ptr< ColorFilterContents > MakeColorMatrix(FilterInput::Ref input, const ColorMatrix &color_matrix)
static std::shared_ptr< ColorFilterContents > MakeSrgbToLinearFilter(FilterInput::Ref input)
static std::shared_ptr< ColorFilterContents > MakeLinearToSrgbFilter(FilterInput::Ref input)
static std::shared_ptr< ColorFilterContents > MakeBlend(BlendMode blend_mode, FilterInput::Vector inputs, std::optional< Color > foreground_color=std::nullopt)
the [inputs] are expected to be in the order of dst, src.
void SetGeometry(const Geometry *geometry)
Set the geometry that this contents will use to render.
void SetColors(std::vector< Color > colors)
HostBuffer & GetTransientsBuffer() const
Retrieve the currnent host buffer for transient storage.
void SetTransientsBuffer(std::shared_ptr< HostBuffer > host_buffer)
PipelineRef GetSolidFillPipeline(ContentContextOptions opts) const
virtual bool IsOpaque(const Matrix &transform) const
Whether this Contents only emits opaque source colors from the fragment stage. This value does not ac...
Definition: contents.cc:52
To do anything rendering related with Impeller, you need a context.
Definition: context.h:65
virtual std::shared_ptr< CommandBuffer > CreateCommandBuffer() const =0
Create a new command buffer. Command buffers can be used to encode graphics, blit,...
virtual std::shared_ptr< CommandQueue > GetCommandQueue() const =0
Return the graphics queue for submitting command buffers.
virtual std::shared_ptr< Allocator > GetResourceAllocator() const =0
Returns the allocator used to create textures and buffers on the device.
static BufferView AsBufferView(std::shared_ptr< DeviceBuffer > buffer)
Create a buffer view of this entire buffer.
virtual uint8_t * OnGetContents() const =0
void SetTransform(const Matrix &transform)
Set the global transform matrix for this Entity.
Definition: entity.cc:60
std::optional< Rect > GetCoverage() const
Definition: entity.cc:64
BlendMode GetBlendMode() const
Definition: entity.cc:101
void SetContents(std::shared_ptr< Contents > contents)
Definition: entity.cc:72
void SetBlendMode(BlendMode blend_mode)
Definition: entity.cc:97
bool Render(const ContentContext &renderer, RenderPass &parent_pass) const
Definition: entity.cc:144
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:44
static constexpr BlendMode kLastPipelineBlendMode
Definition: entity.h:28
@ kNormal
Blurred inside and outside.
@ kOuter
Nothing inside, blurred outside.
@ kInner
Blurred inside, nothing outside.
@ kSolid
Solid inside, blurred outside.
static std::shared_ptr< FilterContents > MakeMorphology(FilterInput::Ref input, Radius radius_x, Radius radius_y, MorphType morph_type)
static std::shared_ptr< FilterContents > MakeBorderMaskBlur(FilterInput::Ref input, Sigma sigma_x, Sigma sigma_y, BlurStyle blur_style=BlurStyle::kNormal)
static std::shared_ptr< FilterContents > MakeGaussianBlur(const FilterInput::Ref &input, Sigma sigma_x, Sigma sigma_y, Entity::TileMode tile_mode=Entity::TileMode::kDecal, BlurStyle mask_blur_style=BlurStyle::kNormal, const Geometry *mask_geometry=nullptr)
static std::shared_ptr< FilterContents > MakeYUVToRGBFilter(std::shared_ptr< Texture > y_texture, std::shared_ptr< Texture > uv_texture, YUVColorSpace yuv_color_space)
static FilterInput::Ref Make(Variant input, bool msaa_enabled=true)
Definition: filter_input.cc:19
static std::unique_ptr< Geometry > MakeFillPath(const flutter::DlPath &path, std::optional< Rect > inner_rect=std::nullopt)
Definition: geometry.cc:62
static std::unique_ptr< Geometry > MakeRect(const Rect &rect)
Definition: geometry.cc:83
static std::unique_ptr< Geometry > MakeStrokePath(const flutter::DlPath &path, const StrokeParameters &stroke={})
Definition: geometry.cc:68
static std::unique_ptr< Geometry > MakeRoundSuperellipse(const Rect &rect, Scalar corner_radius)
Definition: geometry.cc:130
static std::unique_ptr< Geometry > MakeCover()
Definition: geometry.cc:79
static std::shared_ptr< HostBuffer > Create(const std::shared_ptr< Allocator > &allocator, const std::shared_ptr< const IdleWaiter > &idle_waiter, size_t minimum_uniform_alignment)
Definition: host_buffer.cc:21
BufferView EmplaceUniform(const UniformType &uniform)
Emplace uniform data onto the host buffer. Ensure that backend specific uniform alignment requirement...
Definition: host_buffer.h:47
void SetTileMode(Entity::TileMode tile_mode)
void SetColors(std::vector< Color > colors)
bool IsOpaque(const Matrix &transform) const override
Whether this Contents only emits opaque source colors from the fragment stage. This value does not ac...
A geometry class specialized for Canvas::DrawPoints.
std::optional< Rect > GetCoverage(const Matrix &transform) const override
bool IsOpaque(const Matrix &transform) const override
Whether this Contents only emits opaque source colors from the fragment stage. This value does not ac...
void SetTileMode(Entity::TileMode tile_mode)
void SetColors(std::vector< Color > colors)
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:30
FragmentShader_ FragmentShader
Definition: pipeline.h:164
a wrapper around the impeller [Allocator] instance that can be used to provide caching of allocated r...
virtual RenderTarget CreateOffscreen(const Context &context, ISize size, int mip_count, std::string_view label="Offscreen", RenderTarget::AttachmentConfig color_attachment_config=RenderTarget::kDefaultColorAttachmentConfig, std::optional< RenderTarget::AttachmentConfig > stencil_attachment_config=RenderTarget::kDefaultStencilAttachmentConfig, const std::shared_ptr< Texture > &existing_color_texture=nullptr, const std::shared_ptr< Texture > &existing_depth_stencil_texture=nullptr)
static BufferView EmplaceVulkanUniform(const std::shared_ptr< const std::vector< uint8_t >> &input_data, HostBuffer &host_buffer, const RuntimeUniformDescription &uniform, size_t minimum_uniform_alignment)
bool IsOpaque(const Matrix &transform) const override
Whether this Contents only emits opaque source colors from the fragment stage. This value does not ac...
A Geometry that produces fillable vertices representing the stroked outline of a |DlPath| object usin...
static Rational RoundScaledFontSize(Scalar scale)
Definition: text_frame.cc:55
static std::shared_ptr< TextureContents > MakeRect(Rect destination)
bool IsOpaque(const Matrix &transform) const override
Whether this Contents only emits opaque source colors from the fragment stage. This value does not ac...
void SetTexture(std::shared_ptr< Texture > texture)
VertexBuffer CreateVertexBuffer(HostBuffer &host_buffer) const
VertexBufferBuilder & AddVertices(std::initializer_list< VertexType_ > vertices)
std::shared_ptr< Texture > OnCreateTexture(const TextureDescriptor &desc) override
std::shared_ptr< DeviceBuffer > OnCreateBuffer(const DeviceBufferDescriptor &desc) override
ISize GetMaxTextureSizeSupported() const override
FlushTestContentContext(const std::shared_ptr< Context > &context, const std::shared_ptr< TypographerContext > &typographer_context, const std::shared_ptr< Allocator > &allocator)
bool OnCopyHostBuffer(const uint8_t *source, Range source_range, size_t offset)
void Flush(std::optional< Range > range) const override
bool SetLabel(std::string_view label, Range range) override
bool SetLabel(std::string_view label) override
FlushTestDeviceBuffer(const DeviceBufferDescriptor &desc)
Vector2 blur_radius
Blur radius in source pixels based on scaled_sigma.
Vector2 padding
The halo padding in source space.
#define ASSERT_RECT_NEAR(a, b)
#define ASSERT_COLOR_NEAR(a, b)
Rect RectMakeCenterSize(Point center, Size size)
TEST_P(AiksTest, DrawAtlasNoColor)
APPLY_COLOR_FILTER_GRADIENT_TEST(Linear)
INSTANTIATE_PLAYGROUND_SUITE(AiksTest)
static Vector3 RGBToYUV(Vector3 rgb, YUVColorSpace yuv_color_space)
static std::vector< std::shared_ptr< Texture > > CreateTestYUVTextures(Context *context, YUVColorSpace yuv_color_space)
YUVColorSpace
Definition: color.h:54
Join
An enum that describes ways to join two segments of a path.
@ kPoint
Draws a point at each input vertex.
Point Vector2
Definition: point.h:331
constexpr float kPi
Definition: constants.h:26
float Scalar
Definition: scalar.h:19
Point DrawPlaygroundPoint(PlaygroundPoint &point)
Definition: widgets.cc:9
std::tuple< Point, Point > DrawPlaygroundLine(PlaygroundPoint &point_a, PlaygroundPoint &point_b)
Definition: widgets.cc:50
constexpr RuntimeStageBackend PlaygroundBackendToRuntimeStageBackend(PlaygroundBackend backend)
Definition: playground.h:33
TPoint< Scalar > Point
Definition: point.h:327
Cap
An enum that describes ways to decorate the end of a path contour.
LinePipeline::FragmentShader FS
constexpr float kPiOver2
Definition: constants.h:32
flutter::DlPath DlPath
Definition: dl_dispatcher.h:29
BlendMode
Definition: color.h:58
LinePipeline::VertexShader VS
void MoveTo(PathBuilder *builder, Scalar x, Scalar y)
Definition: tessellator.cc:20
static constexpr size_t kPointArenaSize
The size of the point arena buffer stored on the tessellator.
Definition: tessellator.h:25
void LineTo(PathBuilder *builder, Scalar x, Scalar y)
Definition: tessellator.cc:24
ContentContextOptions OptionsFromPass(const RenderPass &pass)
Definition: contents.cc:19
ISize64 ISize
Definition: size.h:162
void Close(PathBuilder *builder)
Definition: tessellator.cc:38
Range GetRange() const
Definition: buffer_view.h:27
const DeviceBuffer * GetBuffer() const
Definition: buffer_view.cc:17
static constexpr Color LimeGreen()
Definition: color.h:602
static constexpr Color MintCream()
Definition: color.h:658
Scalar alpha
Definition: color.h:143
static constexpr Color DeepPink()
Definition: color.h:434
static constexpr Color Black()
Definition: color.h:266
static constexpr Color CornflowerBlue()
Definition: color.h:342
static constexpr Color White()
Definition: color.h:264
constexpr Color WithAlpha(Scalar new_alpha) const
Definition: color.h:278
static constexpr Color WhiteTransparent()
Definition: color.h:268
static constexpr Color Coral()
Definition: color.h:338
static constexpr Color Red()
Definition: color.h:272
constexpr Color Premultiply() const
Definition: color.h:212
Color Blend(Color source, BlendMode blend_mode) const
Blends an unpremultiplied destination color into a given unpremultiplied source color to form a new u...
Definition: color.cc:157
static constexpr Color Blue()
Definition: color.h:276
static constexpr Color Green()
Definition: color.h:274
Scalar array[20]
Definition: color.h:118
constexpr uint64_t ToKey() const
@ kNormal
The geometry has no overlapping triangles.
VertexBuffer vertex_buffer
Definition: geometry.h:38
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
static constexpr Matrix MakeTranslation(const Vector3 &t)
Definition: matrix.h:95
constexpr bool IsIdentity() const
Definition: matrix.h:414
static Matrix MakeRotationY(Radians r)
Definition: matrix.h:208
static constexpr Matrix MakeSkew(Scalar sx, Scalar sy)
Definition: matrix.h:127
static Matrix MakeRotationZ(Radians r)
Definition: matrix.h:223
static constexpr Matrix MakeScale(const Vector3 &s)
Definition: matrix.h:104
static Matrix MakeRotationX(Radians r)
Definition: matrix.h:193
For convolution filters, the "radius" is the size of the convolution kernel to use on the local space...
Definition: sigma.h:48
size_t offset
Definition: range.h:14
static RoundSuperellipse MakeRectRadii(const Rect &rect, const RoundingRadii &radii)
In filters that use Gaussian distributions, "sigma" is a size of one standard deviation in terms of t...
Definition: sigma.h:32
constexpr static TRect MakeXYWH(Type x, Type y, Type width, Type height)
Definition: rect.h:136
constexpr TRect< T > Shift(T dx, T dy) const
Returns a new rectangle translated by the given offset.
Definition: rect.h:606
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:150
constexpr static TRect MakeLTRB(Type left, Type top, Type right, Type bottom)
Definition: rect.h:129
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
std::vector< Point > points