Flutter Impeller
morphology_filter_contents_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 "flutter/testing/testing.h"
10 
11 namespace impeller {
12 namespace testing {
13 
15  public:
16  std::shared_ptr<Texture> MakeTexture(ISize size) {
17  std::shared_ptr<CommandBuffer> command_buffer =
18  GetContentContext().GetContext()->CreateCommandBuffer();
19  if (!command_buffer) {
20  return nullptr;
21  }
22 
23  auto render_target = GetContentContext().MakeSubpass(
24  "Clear Subpass", size, command_buffer,
25  [](const ContentContext&, RenderPass&) { return true; });
26 
27  if (!GetContentContext()
28  .GetContext()
29  ->GetCommandQueue()
30  ->Submit(/*buffers=*/{command_buffer})
31  .ok()) {
32  return nullptr;
33  }
34 
35  if (render_target.ok()) {
36  return render_target.value().GetRenderTargetTexture();
37  }
38  return nullptr;
39  }
40 };
41 
43 
44 TEST_P(MorphologyFilterContentsTest, RenderCoverageMatchesGetCoverage) {
45  std::shared_ptr<Texture> texture = MakeTexture(ISize(100, 100));
46  ASSERT_NE(texture, nullptr);
48  FilterInput::Make(texture), Radius{2.0}, Vector2(1, 0),
50 
51  Entity entity;
52  ContentContext& renderer = GetContentContext();
53  std::optional<Entity> result =
54  contents->GetEntity(renderer, entity, /*coverage_hint=*/{});
55 
56  ASSERT_TRUE(result.has_value());
57  if (result.has_value()) {
58  std::optional<Rect> result_coverage = result.value().GetCoverage();
59  std::optional<Rect> contents_coverage = contents->GetCoverage(entity);
60  Rect expected = Rect::MakeLTRB(-2, 0, 102, 100);
61  ASSERT_TRUE(result_coverage.has_value());
62  ASSERT_TRUE(contents_coverage.has_value());
63  if (result_coverage.has_value() && contents_coverage.has_value()) {
64  EXPECT_TRUE(RectNear(result_coverage.value(), expected));
65  EXPECT_TRUE(RectNear(contents_coverage.value(), expected));
66  }
67  }
68 }
69 
71  RenderDilateWithFractionalCoverageIsSymmetric) {
72  // Non-integer scale and radius produce a fractional pixel expansion
73  // 1.5 * 2.625 = 3.9375, exercising the render target ceiling logic.
74  Scalar radius = 1.5;
75  Scalar scale = 2.625;
76 
77  std::shared_ptr<Texture> texture = MakeTexture(ISize(100, 100));
78  ASSERT_NE(texture, nullptr);
80  FilterInput::Make(texture), Radius{radius}, Vector2(1, 0),
82  contents->SetEffectTransform(Matrix::MakeScale(Vector2(scale, scale)));
83 
84  Entity entity;
85  ContentContext& renderer = GetContentContext();
86  std::optional<Entity> result =
87  contents->GetEntity(renderer, entity, /*coverage_hint=*/{});
88 
89  ASSERT_TRUE(result.has_value());
90  if (result.has_value()) {
91  std::optional<Rect> result_coverage = result->GetCoverage();
92  ASSERT_TRUE(result_coverage.has_value());
93  if (result_coverage.has_value()) {
94  Scalar expected_expansion = radius * scale;
95  EXPECT_TRUE(result_coverage->Contains(Rect::MakeLTRB(
96  -expected_expansion, 0, 100 + expected_expansion, 100)));
97  Scalar left_expansion = 0 - result_coverage->GetLeft();
98  Scalar right_expansion = result_coverage->GetRight() - 100;
99  EXPECT_NEAR(left_expansion, right_expansion, 0.5);
100  }
101  }
102 }
103 
105  RenderDilateYWithFractionalCoverageIsSymmetric) {
106  // Non-integer scale and radius produce a fractional pixel expansion
107  // 1.5 * 2.625 = 3.9375, exercising the render target ceiling logic.
108  Scalar radius = 1.5;
109  Scalar scale = 2.625;
110 
111  std::shared_ptr<Texture> texture = MakeTexture(ISize(100, 100));
112  ASSERT_NE(texture, nullptr);
114  FilterInput::Make(texture), Radius{radius}, Vector2(0, 1),
116  contents->SetEffectTransform(Matrix::MakeScale(Vector2(scale, scale)));
117 
118  Entity entity;
119  ContentContext& renderer = GetContentContext();
120  std::optional<Entity> result =
121  contents->GetEntity(renderer, entity, /*coverage_hint=*/{});
122 
123  ASSERT_TRUE(result.has_value());
124  if (result.has_value()) {
125  std::optional<Rect> result_coverage = result->GetCoverage();
126  ASSERT_TRUE(result_coverage.has_value());
127  if (result_coverage.has_value()) {
128  Scalar expected_expansion = radius * scale;
129  EXPECT_TRUE(result_coverage->Contains(Rect::MakeLTRB(
130  0, -expected_expansion, 100, 100 + expected_expansion)));
131  Scalar top_expansion = 0 - result_coverage->GetTop();
132  Scalar bottom_expansion = result_coverage->GetBottom() - 100;
133  EXPECT_NEAR(top_expansion, bottom_expansion, 0.5);
134  }
135  }
136 }
137 
138 } // namespace testing
139 } // namespace impeller
fml::StatusOr< RenderTarget > MakeSubpass(std::string_view label, ISize texture_size, const std::shared_ptr< CommandBuffer > &command_buffer, const SubpassCallback &subpass_callback, bool msaa_enabled=true, bool depth_stencil_enabled=false, int32_t mip_count=1) const
Creates a new texture of size texture_size and calls subpass_callback with a RenderPass for drawing t...
std::shared_ptr< Context > GetContext() const
static std::shared_ptr< FilterContents > MakeDirectionalMorphology(FilterInput::Ref input, Radius radius, Vector2 direction, MorphType morph_type)
static FilterInput::Ref Make(Variant input, bool msaa_enabled=true)
Definition: filter_input.cc:19
ContentContext & GetContentContext() const
Definition: playground.cc:146
std::shared_ptr< Context > GetContext() const
Definition: playground.cc:126
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:30
inline ::testing::AssertionResult RectNear(impeller::Rect a, impeller::Rect b)
TEST_P(AiksTest, DrawAtlasNoColor)
INSTANTIATE_PLAYGROUND_SUITE(AiksTest)
Point Vector2
Definition: point.h:430
float Scalar
Definition: scalar.h:19
ISize64 ISize
Definition: size.h:162
std::shared_ptr< CommandBuffer > command_buffer
static constexpr Matrix MakeScale(const Vector3 &s)
Definition: matrix.h:104
For convolution filters, the "radius" is the size of the convolution kernel to use on the local space...
Definition: sigma.h:48
constexpr static TRect MakeLTRB(Type left, Type top, Type right, Type bottom)
Definition: rect.h:129