Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit c83533f

Browse files
authored
[Impeller] Add placeholder filter input. (#44290)
This is a revival of my patch to add the necessary mechanisms for: flutter/flutter#131632 ... since it turns out we need it sooner than later for flutter/flutter#131182.
1 parent 7da3556 commit c83533f

File tree

11 files changed

+192
-2
lines changed

11 files changed

+192
-2
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,6 +1201,8 @@ ORIGIN: ../../../flutter/impeller/entity/contents/filters/inputs/filter_contents
12011201
ORIGIN: ../../../flutter/impeller/entity/contents/filters/inputs/filter_contents_filter_input.h + ../../../flutter/LICENSE
12021202
ORIGIN: ../../../flutter/impeller/entity/contents/filters/inputs/filter_input.cc + ../../../flutter/LICENSE
12031203
ORIGIN: ../../../flutter/impeller/entity/contents/filters/inputs/filter_input.h + ../../../flutter/LICENSE
1204+
ORIGIN: ../../../flutter/impeller/entity/contents/filters/inputs/placeholder_filter_input.cc + ../../../flutter/LICENSE
1205+
ORIGIN: ../../../flutter/impeller/entity/contents/filters/inputs/placeholder_filter_input.h + ../../../flutter/LICENSE
12041206
ORIGIN: ../../../flutter/impeller/entity/contents/filters/inputs/texture_filter_input.cc + ../../../flutter/LICENSE
12051207
ORIGIN: ../../../flutter/impeller/entity/contents/filters/inputs/texture_filter_input.h + ../../../flutter/LICENSE
12061208
ORIGIN: ../../../flutter/impeller/entity/contents/filters/linear_to_srgb_filter_contents.cc + ../../../flutter/LICENSE
@@ -3903,6 +3905,8 @@ FILE: ../../../flutter/impeller/entity/contents/filters/inputs/filter_contents_f
39033905
FILE: ../../../flutter/impeller/entity/contents/filters/inputs/filter_contents_filter_input.h
39043906
FILE: ../../../flutter/impeller/entity/contents/filters/inputs/filter_input.cc
39053907
FILE: ../../../flutter/impeller/entity/contents/filters/inputs/filter_input.h
3908+
FILE: ../../../flutter/impeller/entity/contents/filters/inputs/placeholder_filter_input.cc
3909+
FILE: ../../../flutter/impeller/entity/contents/filters/inputs/placeholder_filter_input.h
39063910
FILE: ../../../flutter/impeller/entity/contents/filters/inputs/texture_filter_input.cc
39073911
FILE: ../../../flutter/impeller/entity/contents/filters/inputs/texture_filter_input.h
39083912
FILE: ../../../flutter/impeller/entity/contents/filters/linear_to_srgb_filter_contents.cc

impeller/entity/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ impeller_component("entity") {
175175
"contents/filters/inputs/filter_contents_filter_input.h",
176176
"contents/filters/inputs/filter_input.cc",
177177
"contents/filters/inputs/filter_input.h",
178+
"contents/filters/inputs/placeholder_filter_input.cc",
179+
"contents/filters/inputs/placeholder_filter_input.h",
178180
"contents/filters/inputs/texture_filter_input.cc",
179181
"contents/filters/inputs/texture_filter_input.h",
180182
"contents/filters/linear_to_srgb_filter_contents.cc",

impeller/entity/contents/filters/filter_contents.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,4 +277,23 @@ Matrix FilterContents::GetTransform(const Matrix& parent_transform) const {
277277
return parent_transform * GetLocalTransform(parent_transform);
278278
}
279279

280+
bool FilterContents::IsLeaf() const {
281+
for (auto& input : inputs_) {
282+
if (!input->IsLeaf()) {
283+
return false;
284+
}
285+
}
286+
return true;
287+
}
288+
289+
void FilterContents::SetLeafInputs(const FilterInput::Vector& inputs) {
290+
if (IsLeaf()) {
291+
inputs_ = inputs;
292+
return;
293+
}
294+
for (auto& input : inputs_) {
295+
input->SetLeafInputs(inputs);
296+
}
297+
}
298+
280299
} // namespace impeller

impeller/entity/contents/filters/filter_contents.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,15 @@ class FilterContents : public Contents {
133133

134134
Matrix GetTransform(const Matrix& parent_transform) const;
135135

136+
/// @brief Returns `true` if this filter does not have any `FilterInput`
137+
/// children.
138+
bool IsLeaf() const;
139+
140+
/// @brief Replaces the leaf of all leaf `FilterContents` with a new set
141+
/// of `inputs`.
142+
/// @see `FilterContents::IsLeaf`
143+
void SetLeafInputs(const FilterInput::Vector& inputs);
144+
136145
private:
137146
virtual std::optional<Rect> GetFilterCoverage(
138147
const FilterInput::Vector& inputs,

impeller/entity/contents/filters/inputs/filter_contents_filter_input.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,13 @@ void FilterContentsFilterInput::PopulateGlyphAtlas(
5858
filter_->PopulateGlyphAtlas(lazy_glyph_atlas, scale);
5959
}
6060

61+
bool FilterContentsFilterInput::IsLeaf() const {
62+
return false;
63+
}
64+
65+
void FilterContentsFilterInput::SetLeafInputs(
66+
const FilterInput::Vector& inputs) {
67+
filter_->SetLeafInputs(inputs);
68+
}
69+
6170
} // namespace impeller

impeller/entity/contents/filters/inputs/filter_contents_filter_input.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,14 @@ class FilterContentsFilterInput final : public FilterInput {
3636
const std::shared_ptr<LazyGlyphAtlas>& lazy_glyph_atlas,
3737
Scalar scale) override;
3838

39+
// |FilterInput|
40+
bool IsLeaf() const override;
41+
42+
// |FilterInput|
43+
void SetLeafInputs(const FilterInput::Vector& inputs) override;
44+
3945
private:
40-
FilterContentsFilterInput(std::shared_ptr<FilterContents> filter);
46+
explicit FilterContentsFilterInput(std::shared_ptr<FilterContents> filter);
4147

4248
std::shared_ptr<FilterContents> filter_;
4349
mutable std::optional<Snapshot> snapshot_;

impeller/entity/contents/filters/inputs/filter_input.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "impeller/entity/contents/filters/filter_contents.h"
1212
#include "impeller/entity/contents/filters/inputs/contents_filter_input.h"
1313
#include "impeller/entity/contents/filters/inputs/filter_contents_filter_input.h"
14+
#include "impeller/entity/contents/filters/inputs/placeholder_filter_input.h"
1415
#include "impeller/entity/contents/filters/inputs/texture_filter_input.h"
1516

1617
namespace impeller {
@@ -32,6 +33,11 @@ FilterInput::Ref FilterInput::Make(Variant input, bool msaa_enabled) {
3233
return Make(*texture, Matrix());
3334
}
3435

36+
if (auto rect = std::get_if<Rect>(&input)) {
37+
return std::shared_ptr<PlaceholderFilterInput>(
38+
new PlaceholderFilterInput(*rect));
39+
}
40+
3541
FML_UNREACHABLE();
3642
}
3743

@@ -70,4 +76,10 @@ void FilterInput::PopulateGlyphAtlas(
7076

7177
FilterInput::~FilterInput() = default;
7278

79+
bool FilterInput::IsLeaf() const {
80+
return true;
81+
}
82+
83+
void FilterInput::SetLeafInputs(const FilterInput::Vector& inputs) {}
84+
7385
} // namespace impeller

impeller/entity/contents/filters/inputs/filter_input.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ class FilterInput {
3232
using Vector = std::vector<FilterInput::Ref>;
3333
using Variant = std::variant<std::shared_ptr<FilterContents>,
3434
std::shared_ptr<Contents>,
35-
std::shared_ptr<Texture>>;
35+
std::shared_ptr<Texture>,
36+
Rect>;
3637

3738
virtual ~FilterInput();
3839

@@ -67,6 +68,15 @@ class FilterInput {
6768
virtual void PopulateGlyphAtlas(
6869
const std::shared_ptr<LazyGlyphAtlas>& lazy_glyph_atlas,
6970
Scalar scale);
71+
72+
/// @brief Returns `true` unless this input is a `FilterInput`, which may
73+
/// take other inputs.
74+
virtual bool IsLeaf() const;
75+
76+
/// @brief Replaces the inputs of all leaf `FilterContents` with a new set
77+
/// of `inputs`.
78+
/// @see `FilterInput::IsLeaf`
79+
virtual void SetLeafInputs(const FilterInput::Vector& inputs);
7080
};
7181

7282
} // namespace impeller

impeller/entity/contents/filters/inputs/filter_input_unittests.cc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
#include <memory>
66
#include "flutter/testing/testing.h"
77
#include "gtest/gtest.h"
8+
#include "impeller/entity/contents/filters/color_filter_contents.h"
89
#include "impeller/entity/contents/filters/inputs/filter_input.h"
910
#include "impeller/entity/entity.h"
11+
#include "impeller/geometry/color.h"
1012
#include "impeller/geometry/geometry_asserts.h"
1113

1214
namespace impeller {
@@ -25,5 +27,41 @@ TEST(FilterInputTest, CanSetLocalTransformForTexture) {
2527
Matrix::MakeTranslation({1.0, 2.0, 0.0}));
2628
}
2729

30+
TEST(FilterInputTest, IsLeaf) {
31+
std::shared_ptr<FilterContents> leaf =
32+
ColorFilterContents::MakeBlend(BlendMode::kSource, {});
33+
ASSERT_TRUE(leaf->IsLeaf());
34+
35+
auto base = ColorFilterContents::MakeMatrixFilter(
36+
FilterInput::Make(leaf), Matrix(), {}, Matrix(), false);
37+
38+
ASSERT_TRUE(leaf->IsLeaf());
39+
ASSERT_FALSE(base->IsLeaf());
40+
}
41+
42+
TEST(FilterInputTest, SetCoverageInputs) {
43+
std::shared_ptr<FilterContents> leaf =
44+
ColorFilterContents::MakeBlend(BlendMode::kSource, {});
45+
ASSERT_TRUE(leaf->IsLeaf());
46+
47+
auto base = ColorFilterContents::MakeMatrixFilter(
48+
FilterInput::Make(leaf), Matrix(), {}, Matrix(), false);
49+
50+
{
51+
auto result = base->GetCoverage({});
52+
ASSERT_FALSE(result.has_value());
53+
}
54+
55+
auto coverage_rect = Rect::MakeLTRB(100, 100, 200, 200);
56+
base->SetLeafInputs(FilterInput::Make({coverage_rect}));
57+
58+
{
59+
auto result = base->GetCoverage({});
60+
ASSERT_TRUE(result.has_value());
61+
// NOLINTNEXTLINE(bugprone-unchecked-optional-access)
62+
ASSERT_RECT_NEAR(result.value(), coverage_rect);
63+
}
64+
}
65+
2866
} // namespace testing
2967
} // namespace impeller
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 "impeller/entity/contents/filters/inputs/placeholder_filter_input.h"
6+
7+
#include <optional>
8+
#include <utility>
9+
10+
#include "impeller/base/strings.h"
11+
12+
namespace impeller {
13+
14+
PlaceholderFilterInput::PlaceholderFilterInput(Rect coverage_rect)
15+
: coverage_rect_(coverage_rect) {}
16+
17+
PlaceholderFilterInput::~PlaceholderFilterInput() = default;
18+
19+
FilterInput::Variant PlaceholderFilterInput::GetInput() const {
20+
return coverage_rect_;
21+
}
22+
23+
std::optional<Snapshot> PlaceholderFilterInput::GetSnapshot(
24+
const std::string& label,
25+
const ContentContext& renderer,
26+
const Entity& entity,
27+
std::optional<Rect> coverage_limit) const {
28+
return std::nullopt;
29+
}
30+
31+
std::optional<Rect> PlaceholderFilterInput::GetCoverage(
32+
const Entity& entity) const {
33+
return coverage_rect_;
34+
}
35+
36+
void PlaceholderFilterInput::PopulateGlyphAtlas(
37+
const std::shared_ptr<LazyGlyphAtlas>& lazy_glyph_atlas,
38+
Scalar scale) {}
39+
40+
} // namespace impeller

0 commit comments

Comments
 (0)