| Flutter Impeller |

matrix_filter_contents.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 "impeller/entity/contents/filters/matrix_filter_contents.h"
6
7 namespace impeller {
8
9 MatrixFilterContents::MatrixFilterContents() = default;
10
11 MatrixFilterContents::~MatrixFilterContents() = default;
12
13 void MatrixFilterContents::SetMatrix(Matrix matrix) {
14 matrix_ = matrix;
15 }
16
17 void MatrixFilterContents::SetRenderingMode(
18 Entity::RenderingMode rendering_mode) {
19 rendering_mode_ = rendering_mode;
20 FilterContents::SetRenderingMode(rendering_mode);
21 }
22
23 bool MatrixFilterContents::IsTranslationOnly() const {
24 return matrix_.Basis().IsIdentity() && FilterContents::IsTranslationOnly();
25 }
26
27 void MatrixFilterContents::SetSamplerDescriptor(SamplerDescriptor desc) {
28 sampler_descriptor_ = std::move(desc);
29 }
30
31 std::optional<Entity> MatrixFilterContents::RenderFilter(
32 const FilterInput::Vector& inputs,
33 const ContentContext& renderer,
34 const Entity& entity,
35 const Matrix& effect_transform,
36 const Rect& coverage,
37 const std::optional<Rect>& coverage_hint) const {
38 auto snapshot = inputs[0]->GetSnapshot("Matrix", renderer, entity);
39 if (!snapshot.has_value()) {
40 return std::nullopt;
41 }
42
43 // The filter's matrix needs to be applied within the space defined by the
44 // scene's current transform matrix (CTM). For example: If the CTM is
45 // scaled up, then translations applied by the matrix should be magnified
46 // accordingly.
47 //
48 // To accomplish this, we sandwich the filter's matrix within the CTM in both
49 // cases. But notice that for the subpass backdrop filter case, we use the
50 // "effect transform" instead of the Entity's transform!
51 //
52 // That's because in the subpass backdrop filter case, the Entity's transform
53 // isn't actually the captured CTM of the scene like it usually is; instead,
54 // it's just a screen space translation that offsets the backdrop texture (as
55 // mentioned above). And so we sneak the subpass's captured CTM in through the
56 // effect transform.
57
58 auto transform = rendering_mode_ == Entity::RenderingMode::kSubpass
59 ? effect_transform
60 : entity.GetTransform();
61 snapshot->transform = transform * //
62 matrix_ * //
63 transform.Invert() * //
64 snapshot->transform;
65
66 snapshot->sampler_descriptor = sampler_descriptor_;
67 return Entity::FromSnapshot(snapshot, entity.GetBlendMode(),
68 entity.GetClipDepth());
69 }
70
71 std::optional<Rect> MatrixFilterContents::GetFilterSourceCoverage(
72 const Matrix& effect_transform,
73 const Rect& output_limit) const {
74 auto transform = effect_transform * //
75 matrix_ * //
76 effect_transform.Invert(); //
77 if (transform.GetDeterminant() == 0.0) {
78 return std::nullopt;
79 }
80 auto inverse = transform.Invert();
81 return output_limit.TransformBounds(inverse);
82 }
83
84 std::optional<Rect> MatrixFilterContents::GetFilterCoverage(
85 const FilterInput::Vector& inputs,
86 const Entity& entity,
87 const Matrix& effect_transform) const {
88 if (inputs.empty()) {
89 return std::nullopt;
90 }
91
92 auto coverage = inputs[0]->GetCoverage(entity);
93 if (!coverage.has_value()) {
94 return std::nullopt;
95 }
96 auto& m = rendering_mode_ == Entity::RenderingMode::kSubpass
97 ? effect_transform
98 : inputs[0]->GetTransform(entity);
99 auto transform = m * //
100 matrix_ * //
101 m.Invert(); //
102 return coverage->TransformBounds(transform);
103 }
104
105 } // namespace impeller
impeller::MatrixFilterContents::SetRenderingMode
void SetRenderingMode(Entity::RenderingMode rendering_mode) override
Marks this filter chain as applying in a subpass scenario.
Definition: matrix_filter_contents.cc:17
impeller::Entity::GetTransform
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:49
impeller::MatrixFilterContents::SetMatrix
void SetMatrix(Matrix matrix)
Definition: matrix_filter_contents.cc:13
impeller::TRect::TransformBounds
constexpr TRect TransformBounds(const Matrix &transform) const
Creates a new bounding box that contains this transformed rectangle.
Definition: rect.h:264
impeller::SamplerDescriptor
Definition: sampler_descriptor.h:15
impeller::Entity
Definition: entity.h:21
impeller::Matrix::Basis
constexpr Matrix Basis() const
The Matrix without its w components (without translation).
Definition: matrix.h:224
impeller::MatrixFilterContents::~MatrixFilterContents
~MatrixFilterContents() override
impeller::FilterContents::IsTranslationOnly
virtual bool IsTranslationOnly() const
Returns true if this filter graph doesn't perform any basis transforms to the filtered content....
Definition: filter_contents.cc:320
matrix_filter_contents.h
impeller::Rect
TRect< Scalar > Rect
Definition: rect.h:488
impeller::Entity::RenderingMode::kSubpass
@ kSubpass
impeller::Entity::GetBlendMode
BlendMode GetBlendMode() const
Definition: entity.cc:105
impeller::Matrix::Invert
Matrix Invert() const
Definition: matrix.cc:97
impeller::Matrix::IsIdentity
constexpr bool IsIdentity() const
Definition: matrix.h:347
impeller::Entity::GetClipDepth
uint32_t GetClipDepth() const
Definition: entity.cc:93
impeller::MatrixFilterContents::MatrixFilterContents
MatrixFilterContents()
impeller::FilterContents::SetRenderingMode
virtual void SetRenderingMode(Entity::RenderingMode rendering_mode)
Marks this filter chain as applying in a subpass scenario.
Definition: filter_contents.cc:348
impeller::Entity::RenderingMode
RenderingMode
Definition: entity.h:26
impeller::MatrixFilterContents::GetFilterCoverage
std::optional< Rect > GetFilterCoverage(const FilterInput::Vector &inputs, const Entity &entity, const Matrix &effect_transform) const override
Internal utility method for |GetLocalCoverage| that computes the output coverage of this filter acros...
Definition: matrix_filter_contents.cc:84
impeller::FilterInput::Vector
std::vector< FilterInput::Ref > Vector
Definition: filter_input.h:33
impeller::Entity::FromSnapshot
static std::optional< Entity > FromSnapshot(const std::optional< Snapshot > &snapshot, BlendMode blend_mode=BlendMode::kSourceOver, uint32_t clip_depth=0)
Create an entity that can be used to render a given snapshot.
Definition: entity.cc:21
impeller::MatrixFilterContents::IsTranslationOnly
bool IsTranslationOnly() const override
Returns true if this filter graph doesn't perform any basis transforms to the filtered content....
Definition: matrix_filter_contents.cc:23
impeller
Definition: aiks_context.cc:10
impeller::MatrixFilterContents::SetSamplerDescriptor
void SetSamplerDescriptor(SamplerDescriptor desc)
Definition: matrix_filter_contents.cc:27
impeller::ContentContext
Definition: content_context.h:332
impeller::TRect
Definition: rect.h:22
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37