| 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 transformation 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 sandwitch 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.GetTransformation();
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.GetStencilDepth());
69 }
70
71 std::optional<Rect> MatrixFilterContents::GetFilterCoverage(
72 const FilterInput::Vector& inputs,
73 const Entity& entity,
74 const Matrix& effect_transform) const {
75 if (inputs.empty()) {
76 return std::nullopt;
77 }
78
79 auto coverage = inputs[0]->GetCoverage(entity);
80 if (!coverage.has_value()) {
81 return std::nullopt;
82 }
83 auto& m = rendering_mode_ == Entity::RenderingMode::kSubpass
84 ? effect_transform
85 : inputs[0]->GetTransform(entity);
86 auto transform = m * //
87 matrix_ * //
88 m.Invert(); //
89 return coverage->TransformBounds(transform);
90 }
91
92 } // 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::GetStencilDepth
uint32_t GetStencilDepth() const
Definition: entity.cc:93
impeller::Entity::FromSnapshot
static std::optional< Entity > FromSnapshot(const std::optional< Snapshot > &snapshot, BlendMode blend_mode=BlendMode::kSourceOver, uint32_t stencil_depth=0)
Create an entity that can be used to render a given snapshot.
Definition: entity.cc:21
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:204
impeller::Entity::GetTransformation
const Matrix & GetTransformation() const
Definition: entity.cc:49
impeller::SamplerDescriptor
Definition: sampler_descriptor.h:18
impeller::Entity
Definition: entity.h:21
impeller::Matrix::Basis
constexpr Matrix Basis() const
Definition: matrix.h:222
impeller::MatrixFilterContents::~MatrixFilterContents
~MatrixFilterContents() override
impeller::FilterContents::IsTranslationOnly
virtual bool IsTranslationOnly() const
Returns true if this filter graph doesn't perform any basis transformations to the filtered content....
Definition: filter_contents.cc:273
matrix_filter_contents.h
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:345
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:301
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
Definition: matrix_filter_contents.cc:71
impeller::FilterInput::Vector
std::vector< FilterInput::Ref > Vector
Definition: filter_input.h:32
impeller::MatrixFilterContents::IsTranslationOnly
bool IsTranslationOnly() const override
Returns true if this filter graph doesn't perform any basis transformations 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:344
impeller::TRect
Definition: rect.h:20
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:36