Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
paint_pass_delegate.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
6
13
14namespace impeller {
15
16/// PaintPassDelegate
17/// ----------------------------------------------
18
20
21// |EntityPassDelgate|
23
24// |EntityPassDelgate|
28
29// |EntityPassDelgate|
31 return false;
32}
33
34// |EntityPassDelgate|
36 std::shared_ptr<Texture> target,
37 const Matrix& effect_transform) {
38 auto contents = TextureContents::MakeRect(Rect::MakeSize(target->GetSize()));
39 contents->SetTexture(target);
40 contents->SetLabel("Subpass");
41 contents->SetSourceRect(Rect::MakeSize(target->GetSize()));
42 contents->SetOpacity(paint_.color.alpha);
43 contents->SetDeferApplyingOpacity(true);
44
45 return paint_.WithFiltersForSubpassTarget(std::move(contents),
46 effect_transform);
47}
48
49// |EntityPassDelgate|
50std::shared_ptr<FilterContents> PaintPassDelegate::WithImageFilter(
51 const FilterInput::Variant& input,
52 const Matrix& effect_transform) const {
53 return paint_.WithImageFilter(input, effect_transform,
55}
56
57/// OpacityPeepholePassDelegate
58/// ----------------------------------------------
59
62
63// |EntityPassDelgate|
65
66// |EntityPassDelgate|
70
71// |EntityPassDelgate|
73 EntityPass* entity_pass) {
74 // Passes with enforced bounds that clip the contents can not be safely
75 // collapsed.
76 if (entity_pass->GetBoundsLimitMightClipContent()) {
77 return false;
78 }
79
80 // OpacityPeepholePassDelegate will only get used if the pass's blend mode is
81 // SourceOver, so no need to check here.
82 if (paint_.color.alpha <= 0.0 || paint_.color.alpha >= 1.0 ||
83 paint_.image_filter || paint_.color_filter) {
84 return false;
85 }
86
87 // Note: determing whether any coverage intersects has quadradic complexity in
88 // the number of rectangles, and depending on whether or not we cache at
89 // different levels of the entity tree may end up cubic. In the interest of
90 // proving whether or not this optimization is valuable, we only consider very
91 // simple peephole optimizations here - where there is a single drawing
92 // command wrapped in save layer. This would indicate something like an
93 // Opacity or FadeTransition wrapping a very simple widget, like in the
94 // CupertinoPicker.
95 if (entity_pass->GetElementCount() > 3) {
96 // Single paint command with a save layer would be:
97 // 1. clip
98 // 2. draw command
99 // 3. restore.
100 return false;
101 }
102 bool all_can_accept = true;
103 std::vector<Rect> all_coverages;
104 auto had_subpass = entity_pass->IterateUntilSubpass(
105 [&all_coverages, &all_can_accept](Entity& entity) {
106 const auto& contents = entity.GetContents();
107 if (!entity.CanInheritOpacity()) {
108 all_can_accept = false;
109 return false;
110 }
111 auto maybe_coverage = contents->GetCoverage(entity);
112 if (maybe_coverage.has_value()) {
113 auto coverage = maybe_coverage.value();
114 for (const auto& cv : all_coverages) {
115 if (cv.IntersectsWithRect(coverage)) {
116 all_can_accept = false;
117 return false;
118 }
119 }
120 all_coverages.push_back(coverage);
121 }
122 return true;
123 });
124 if (had_subpass || !all_can_accept) {
125 return false;
126 }
127 auto alpha = paint_.color.alpha;
128 entity_pass->IterateUntilSubpass([&alpha](Entity& entity) {
129 entity.SetInheritedOpacity(alpha);
130 return true;
131 });
132 return true;
133}
134
135// |EntityPassDelgate|
136std::shared_ptr<Contents>
138 std::shared_ptr<Texture> target,
139 const Matrix& effect_transform) {
140 auto contents = TextureContents::MakeRect(Rect::MakeSize(target->GetSize()));
141 contents->SetLabel("Subpass");
142 contents->SetTexture(target);
143 contents->SetSourceRect(Rect::MakeSize(target->GetSize()));
144 contents->SetOpacity(paint_.color.alpha);
145 contents->SetDeferApplyingOpacity(true);
146
147 return paint_.WithFiltersForSubpassTarget(std::move(contents),
148 effect_transform);
149}
150
151// |EntityPassDelgate|
152std::shared_ptr<FilterContents> OpacityPeepholePassDelegate::WithImageFilter(
153 const FilterInput::Variant& input,
154 const Matrix& effect_transform) const {
155 return paint_.WithImageFilter(input, effect_transform,
157}
158
159} // namespace impeller
bool IterateUntilSubpass(const std::function< bool(Entity &)> &iterator)
Iterate entities in this pass up until the first subpass is found. This is useful for limiting look-a...
size_t GetElementCount() const
Return the number of elements on this pass.
bool GetBoundsLimitMightClipContent() const
Indicates if the bounds limit set using |SetBoundsLimit()| might clip the contents of the pass.
bool CanInheritOpacity() const
Definition entity.cc:123
bool SetInheritedOpacity(Scalar alpha)
Definition entity.cc:134
const std::shared_ptr< Contents > & GetContents() const
Definition entity.cc:94
std::variant< std::shared_ptr< FilterContents >, std::shared_ptr< Contents >, std::shared_ptr< Texture >, Rect > Variant
std::shared_ptr< Contents > CreateContentsForSubpassTarget(std::shared_ptr< Texture > target, const Matrix &effect_transform) override
bool CanCollapseIntoParentPass(EntityPass *entity_pass) override
Whether or not this entity pass can be collapsed into the parent. If true, this method may modify the...
std::shared_ptr< FilterContents > WithImageFilter(const FilterInput::Variant &input, const Matrix &effect_transform) const override
bool CanCollapseIntoParentPass(EntityPass *entity_pass) override
Whether or not this entity pass can be collapsed into the parent. If true, this method may modify the...
std::shared_ptr< FilterContents > WithImageFilter(const FilterInput::Variant &input, const Matrix &effect_transform) const override
std::shared_ptr< Contents > CreateContentsForSubpassTarget(std::shared_ptr< Texture > target, const Matrix &effect_transform) override
static std::shared_ptr< TextureContents > MakeRect(Rect destination)
A common case factory that marks the texture contents as having a destination rectangle....
const Paint & paint
uint32_t * target
Definition ref_ptr.h:256
Scalar alpha
Definition color.h:143
A 4x4 matrix using column-major storage.
Definition matrix.h:37
std::shared_ptr< ImageFilter > image_filter
Definition paint.h:67
Color color
Definition paint.h:55
BlendMode blend_mode
Definition paint.h:64
std::shared_ptr< Contents > WithFiltersForSubpassTarget(std::shared_ptr< Contents > input, const Matrix &effect_transform=Matrix()) const
Wrap this paint's configured filters to the given contents of subpass target.
Definition paint.cc:67
std::shared_ptr< FilterContents > WithImageFilter(const FilterInput::Variant &input, const Matrix &effect_transform, Entity::RenderingMode rendering_mode) const
Definition paint.cc:88
std::shared_ptr< ColorFilter > color_filter
Definition paint.h:68
static constexpr TRect MakeSize(const TSize< U > &size)
Definition rect.h:146