| Flutter Impeller |

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/contents.h"
6 #include <optional>
7
8 #include "fml/logging.h"
9 #include "impeller/base/strings.h"
10 #include "impeller/base/validation.h"
11 #include "impeller/core/formats.h"
12 #include "impeller/entity/contents/anonymous_contents.h"
13 #include "impeller/entity/contents/content_context.h"
14 #include "impeller/entity/contents/texture_contents.h"
15 #include "impeller/renderer/command_buffer.h"
16 #include "impeller/renderer/render_pass.h"
17
18 namespace impeller {
19
20 ContentContextOptions OptionsFromPass(const RenderPass& pass) {
21 ContentContextOptions opts;
22 opts.sample_count = pass.GetRenderTarget().GetSampleCount();
23 opts.color_attachment_pixel_format =
24 pass.GetRenderTarget().GetRenderTargetPixelFormat();
25 opts.has_stencil_attachment =
26 pass.GetRenderTarget().GetStencilAttachment().has_value();
27 return opts;
28 }
29
30 ContentContextOptions OptionsFromPassAndEntity(const RenderPass& pass,
31 const Entity& entity) {
32 ContentContextOptions opts = OptionsFromPass(pass);
33 opts.blend_mode = entity.GetBlendMode();
34 return opts;
35 }
36
37 std::shared_ptr<Contents> Contents::MakeAnonymous(
38 Contents::RenderProc render_proc,
39 Contents::CoverageProc coverage_proc) {
40 return AnonymousContents::Make(std::move(render_proc),
41 std::move(coverage_proc));
42 }
43
44 Contents::Contents() = default;
45
46 Contents::~Contents() = default;
47
48 bool Contents::IsOpaque() const {
49 return false;
50 }
51
52 Contents::StencilCoverage Contents::GetStencilCoverage(
53 const Entity& entity,
54 const std::optional<Rect>& current_stencil_coverage) const {
55 return {.type = StencilCoverage::Type::kNoChange,
56 .coverage = current_stencil_coverage};
57 }
58
59 std::optional<Snapshot> Contents::RenderToSnapshot(
60 const ContentContext& renderer,
61 const Entity& entity,
62 std::optional<Rect> coverage_limit,
63 const std::optional<SamplerDescriptor>& sampler_descriptor,
64 bool msaa_enabled,
65 const std::string& label) const {
66 auto coverage = GetCoverage(entity);
67 if (!coverage.has_value()) {
68 return std::nullopt;
69 }
70
71 // Pad Contents snapshots with 1 pixel borders to ensure correct sampling
72 // behavior. Not doing so results in a coverage leak for filters that support
73 // customizing the input sampling mode. Snapshots of contents should be
74 // theoretically treated as infinite size just like layers.
75 coverage = coverage->Expand(1);
76
77 if (coverage_limit.has_value()) {
78 coverage = coverage->Intersection(*coverage_limit);
79 if (!coverage.has_value()) {
80 return std::nullopt;
81 }
82 }
83
84 auto texture = renderer.MakeSubpass(
85 label, ISize::Ceil(coverage->size),
86 [&contents = *this, &entity, &coverage](const ContentContext& renderer,
87 RenderPass& pass) -> bool {
88 Entity sub_entity;
89 sub_entity.SetBlendMode(BlendMode::kSourceOver);
90 sub_entity.SetTransformation(
91 Matrix::MakeTranslation(Vector3(-coverage->origin)) *
92 entity.GetTransformation());
93 return contents.Render(renderer, sub_entity, pass);
94 },
95 msaa_enabled);
96
97 if (!texture) {
98 return std::nullopt;
99 }
100
101 auto snapshot = Snapshot{
102 .texture = texture,
103 .transform = Matrix::MakeTranslation(coverage->origin),
104 };
105 if (sampler_descriptor.has_value()) {
106 snapshot.sampler_descriptor = sampler_descriptor.value();
107 }
108
109 return snapshot;
110 }
111
112 bool Contents::CanInheritOpacity(const Entity& entity) const {
113 return false;
114 }
115
116 void Contents::SetInheritedOpacity(Scalar opacity) {
117 VALIDATION_LOG << "Contents::SetInheritedOpacity should never be called when "
118 "Contents::CanAcceptOpacity returns false.";
119 }
120
121 std::optional<Color> Contents::AsBackgroundColor(const Entity& entity,
122 ISize target_size) const {
123 return {};
124 }
125
126 const FilterContents* Contents::AsFilter() const {
127 return nullptr;
128 }
129
130 bool Contents::ApplyColorFilter(
131 const Contents::ColorFilterProc& color_filter_proc) {
132 return false;
133 }
134
135 bool Contents::ShouldRender(const Entity& entity,
136 const std::optional<Rect> stencil_coverage) const {
137 if (!stencil_coverage.has_value()) {
138 return false;
139 }
140 auto coverage = GetCoverage(entity);
141 if (!coverage.has_value()) {
142 return false;
143 }
144 if (coverage == Rect::MakeMaximum()) {
145 return true;
146 }
147 return stencil_coverage->IntersectsWithRect(coverage.value());
148 }
149
150 void Contents::SetCoverageHint(std::optional<Rect> coverage_hint) {
151 coverage_hint_ = coverage_hint;
152 }
153
154 const std::optional<Rect>& Contents::GetCoverageHint() const {
155 return coverage_hint_;
156 }
157
158 std::optional<Size> Contents::GetColorSourceSize() const {
159 return color_source_size_;
160 };
161
162 void Contents::SetColorSourceSize(Size size) {
163 color_source_size_ = size;
164 }
165
166 } // namespace impeller
impeller::Contents::GetColorSourceSize
std::optional< Size > GetColorSourceSize() const
Return the color source's intrinsic size, if available.
Definition: contents.cc:158
impeller::OptionsFromPass
ContentContextOptions OptionsFromPass(const RenderPass &pass)
Definition: contents.cc:20
impeller::RenderPass::GetRenderTarget
const RenderTarget & GetRenderTarget() const
Definition: render_pass.cc:26
contents.h
impeller::Scalar
float Scalar
Definition: scalar.h:15
impeller::Contents::SetCoverageHint
void SetCoverageHint(std::optional< Rect > coverage_hint)
Hint that specifies the coverage area of this Contents that will actually be used during rendering....
Definition: contents.cc:150
impeller::Contents::AsBackgroundColor
virtual std::optional< Color > AsBackgroundColor(const Entity &entity, ISize target_size) const
Returns a color if this Contents will flood the given target_size with a color. This output color is ...
Definition: contents.cc:121
texture_contents.h
impeller::Contents::SetColorSourceSize
void SetColorSourceSize(Size size)
Definition: contents.cc:162
impeller::Contents::CanInheritOpacity
virtual bool CanInheritOpacity(const Entity &entity) const
Whether or not this contents can accept the opacity peephole optimization.
Definition: contents.cc:112
impeller::Contents::Contents
Contents()
formats.h
impeller::Contents::GetCoverage
virtual std::optional< Rect > GetCoverage(const Entity &entity) const =0
Get the screen space bounding rectangle that this contents affects.
impeller::TSize< int64_t >::Ceil
constexpr TSize Ceil() const
Definition: size.h:91
impeller::RenderTarget::GetRenderTargetPixelFormat
PixelFormat GetRenderTargetPixelFormat() const
Definition: render_target.cc:164
impeller::ContentContextOptions::blend_mode
BlendMode blend_mode
Definition: content_context.h:304
impeller::Contents::StencilCoverage::Type::kNoChange
@ kNoChange
impeller::Contents::ShouldRender
virtual bool ShouldRender(const Entity &entity, const std::optional< Rect > stencil_coverage) const
Definition: contents.cc:135
impeller::Matrix::MakeTranslation
static constexpr Matrix MakeTranslation(const Vector3 &t)
Definition: matrix.h:94
validation.h
impeller::Entity
Definition: entity.h:21
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:30
impeller::Contents::GetCoverageHint
const std::optional< Rect > & GetCoverageHint() const
Definition: contents.cc:154
impeller::TSize< int64_t >
impeller::ContentContext::MakeSubpass
std::shared_ptr< Texture > MakeSubpass(const std::string &label, ISize texture_size, const SubpassCallback &subpass_callback, bool msaa_enabled=true) const
Creates a new texture of size texture_size and calls subpass_callback with a RenderPass for drawing t...
Definition: content_context.cc:373
impeller::ContentContextOptions::color_attachment_pixel_format
PixelFormat color_attachment_pixel_format
Definition: content_context.h:308
render_pass.h
impeller::Contents::StencilCoverage
Definition: contents.h:39
impeller::Contents::~Contents
virtual ~Contents()
impeller::Contents::ColorFilterProc
std::function< Color(Color)> ColorFilterProc
Definition: contents.h:37
impeller::Snapshot
Represents a texture and its intended draw transform/sampler configuration.
Definition: snapshot.h:24
impeller::ContentContextOptions::has_stencil_attachment
bool has_stencil_attachment
Definition: content_context.h:309
strings.h
anonymous_contents.h
impeller::Entity::GetBlendMode
BlendMode GetBlendMode() const
Definition: entity.cc:105
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:27
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:60
command_buffer.h
content_context.h
impeller::Contents::RenderProc
std::function< bool(const ContentContext &renderer, const Entity &entity, RenderPass &pass)> RenderProc
Definition: contents.h:48
impeller::ContentContextOptions::sample_count
SampleCount sample_count
Definition: content_context.h:303
impeller::TRect< Scalar >::MakeMaximum
constexpr static TRect MakeMaximum()
Definition: rect.h:75
impeller::Contents::SetInheritedOpacity
virtual void SetInheritedOpacity(Scalar opacity)
Inherit the provided opacity.
Definition: contents.cc:116
impeller::Contents::StencilCoverage::type
Type type
Definition: contents.h:42
impeller::Contents::MakeAnonymous
static std::shared_ptr< Contents > MakeAnonymous(RenderProc render_proc, CoverageProc coverage_proc)
Definition: contents.cc:37
impeller::Contents::ApplyColorFilter
virtual bool ApplyColorFilter(const ColorFilterProc &color_filter_proc)
If possible, applies a color filter to this contents inputs on the CPU.
Definition: contents.cc:130
impeller::Contents::CoverageProc
std::function< std::optional< Rect >(const Entity &entity)> CoverageProc
Definition: contents.h:49
impeller::Snapshot::texture
std::shared_ptr< Texture > texture
Definition: snapshot.h:25
impeller::RenderTarget::GetStencilAttachment
const std::optional< StencilAttachment > & GetStencilAttachment() const
Definition: render_target.cc:218
impeller::Contents::IsOpaque
virtual bool IsOpaque() const
Whether this Contents only emits opaque source colors from the fragment stage. This value does not ac...
Definition: contents.cc:48
impeller::ContentContextOptions
Definition: content_context.h:302
impeller::RenderTarget::GetSampleCount
SampleCount GetSampleCount() const
Definition: render_target.cc:126
impeller
Definition: aiks_context.cc:10
impeller::ContentContext
Definition: content_context.h:344
impeller::Contents::AsFilter
virtual const FilterContents * AsFilter() const
Cast to a filter. Returns nullptr if this Contents is not a filter.
Definition: contents.cc:126
impeller::Contents::GetStencilCoverage
virtual StencilCoverage GetStencilCoverage(const Entity &entity, const std::optional< Rect > &current_stencil_coverage) const
Given the current screen space bounding rectangle of the stencil, return the expected stencil coverag...
Definition: contents.cc:52
impeller::AnonymousContents::Make
static std::shared_ptr< Contents > Make(RenderProc render_proc, CoverageProc coverage_proc)
Definition: anonymous_contents.cc:11
impeller::FilterContents
Definition: filter_contents.h:19
impeller::Contents::RenderToSnapshot
virtual std::optional< Snapshot > RenderToSnapshot(const ContentContext &renderer, const Entity &entity, std::optional< Rect > coverage_limit=std::nullopt, const std::optional< SamplerDescriptor > &sampler_descriptor=std::nullopt, bool msaa_enabled=true, const std::string &label="Snapshot") const
Render this contents to a snapshot, respecting the entity's transform, path, stencil depth,...
Definition: contents.cc:59