| 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.GetSampleCount();
23 opts.color_attachment_pixel_format = pass.GetRenderTargetPixelFormat();
24 opts.has_stencil_attachment = pass.HasStencilAttachment();
25 return opts;
26 }
27
28 ContentContextOptions OptionsFromPassAndEntity(const RenderPass& pass,
29 const Entity& entity) {
30 ContentContextOptions opts = OptionsFromPass(pass);
31 opts.blend_mode = entity.GetBlendMode();
32 return opts;
33 }
34
35 std::shared_ptr<Contents> Contents::MakeAnonymous(
36 Contents::RenderProc render_proc,
37 Contents::CoverageProc coverage_proc) {
38 return AnonymousContents::Make(std::move(render_proc),
39 std::move(coverage_proc));
40 }
41
42 Contents::Contents() = default;
43
44 Contents::~Contents() = default;
45
46 bool Contents::IsOpaque() const {
47 return false;
48 }
49
50 Contents::ClipCoverage Contents::GetClipCoverage(
51 const Entity& entity,
52 const std::optional<Rect>& current_clip_coverage) const {
53 return {.type = ClipCoverage::Type::kNoChange,
54 .coverage = current_clip_coverage};
55 }
56
57 std::optional<Snapshot> Contents::RenderToSnapshot(
58 const ContentContext& renderer,
59 const Entity& entity,
60 std::optional<Rect> coverage_limit,
61 const std::optional<SamplerDescriptor>& sampler_descriptor,
62 bool msaa_enabled,
63 const std::string& label) const {
64 auto coverage = GetCoverage(entity);
65 if (!coverage.has_value()) {
66 return std::nullopt;
67 }
68
69 // Pad Contents snapshots with 1 pixel borders to ensure correct sampling
70 // behavior. Not doing so results in a coverage leak for filters that support
71 // customizing the input sampling mode. Snapshots of contents should be
72 // theoretically treated as infinite size just like layers.
73 coverage = coverage->Expand(1);
74
75 if (coverage_limit.has_value()) {
76 coverage = coverage->Intersection(*coverage_limit);
77 if (!coverage.has_value()) {
78 return std::nullopt;
79 }
80 }
81
82 fml::StatusOr<RenderTarget> render_target = renderer.MakeSubpass(
83 label, ISize::Ceil(coverage->GetSize()),
84 [&contents = *this, &entity, &coverage](const ContentContext& renderer,
85 RenderPass& pass) -> bool {
86 Entity sub_entity;
87 sub_entity.SetBlendMode(BlendMode::kSourceOver);
88 sub_entity.SetTransform(
89 Matrix::MakeTranslation(Vector3(-coverage->GetOrigin())) *
90 entity.GetTransform());
91 return contents.Render(renderer, sub_entity, pass);
92 },
93 msaa_enabled);
94
95 if (!render_target.ok()) {
96 return std::nullopt;
97 }
98
99 auto snapshot = Snapshot{
100 .texture = render_target.value().GetRenderTargetTexture(),
101 .transform = Matrix::MakeTranslation(coverage->GetOrigin()),
102 };
103 if (sampler_descriptor.has_value()) {
104 snapshot.sampler_descriptor = sampler_descriptor.value();
105 }
106
107 return snapshot;
108 }
109
110 bool Contents::CanInheritOpacity(const Entity& entity) const {
111 return false;
112 }
113
114 void Contents::SetInheritedOpacity(Scalar opacity) {
115 VALIDATION_LOG << "Contents::SetInheritedOpacity should never be called when "
116 "Contents::CanAcceptOpacity returns false.";
117 }
118
119 std::optional<Color> Contents::AsBackgroundColor(const Entity& entity,
120 ISize target_size) const {
121 return {};
122 }
123
124 const FilterContents* Contents::AsFilter() const {
125 return nullptr;
126 }
127
128 bool Contents::ApplyColorFilter(
129 const Contents::ColorFilterProc& color_filter_proc) {
130 return false;
131 }
132
133 bool Contents::ShouldRender(const Entity& entity,
134 const std::optional<Rect> clip_coverage) const {
135 if (!clip_coverage.has_value()) {
136 return false;
137 }
138 auto coverage = GetCoverage(entity);
139 if (!coverage.has_value()) {
140 return false;
141 }
142 if (coverage == Rect::MakeMaximum()) {
143 return true;
144 }
145 return clip_coverage->IntersectsWithRect(coverage.value());
146 }
147
148 void Contents::SetCoverageHint(std::optional<Rect> coverage_hint) {
149 coverage_hint_ = coverage_hint;
150 }
151
152 const std::optional<Rect>& Contents::GetCoverageHint() const {
153 return coverage_hint_;
154 }
155
156 std::optional<Size> Contents::GetColorSourceSize() const {
157 return color_source_size_;
158 };
159
160 void Contents::SetColorSourceSize(Size size) {
161 color_source_size_ = size;
162 }
163
164 } // namespace impeller
impeller::Contents::GetColorSourceSize
std::optional< Size > GetColorSourceSize() const
Return the color source's intrinsic size, if available.
Definition: contents.cc:156
impeller::OptionsFromPass
ContentContextOptions OptionsFromPass(const RenderPass &pass)
Definition: contents.cc:20
contents.h
impeller::Scalar
float Scalar
Definition: scalar.h:18
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:148
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:119
texture_contents.h
impeller::Contents::SetColorSourceSize
void SetColorSourceSize(Size size)
Definition: contents.cc:160
impeller::Contents::CanInheritOpacity
virtual bool CanInheritOpacity(const Entity &entity) const
Whether or not this contents can accept the opacity peephole optimization.
Definition: contents.cc:110
impeller::ContentContext::MakeSubpass
fml::StatusOr< RenderTarget > 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:410
impeller::Contents::Contents
Contents()
formats.h
impeller::Contents::GetCoverage
virtual std::optional< Rect > GetCoverage(const Entity &entity) const =0
Get the area of the render pass that will be affected when this contents is rendered.
impeller::TSize< int64_t >::Ceil
constexpr TSize Ceil() const
Definition: size.h:96
impeller::ContentContextOptions::blend_mode
BlendMode blend_mode
Definition: content_context.h:278
impeller::Matrix::MakeTranslation
static constexpr Matrix MakeTranslation(const Vector3 &t)
Definition: matrix.h:95
validation.h
impeller::Contents::GetClipCoverage
virtual ClipCoverage GetClipCoverage(const Entity &entity, const std::optional< Rect > &current_clip_coverage) const
Given the current pass space bounding rectangle of the clip buffer, return the expected clip coverage...
Definition: contents.cc:50
impeller::Entity
Definition: entity.h:21
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:28
impeller::Contents::GetCoverageHint
const std::optional< Rect > & GetCoverageHint() const
Definition: contents.cc:152
impeller::TSize< int64_t >
impeller::ContentContextOptions::color_attachment_pixel_format
PixelFormat color_attachment_pixel_format
Definition: content_context.h:282
render_pass.h
impeller::Contents::~Contents
virtual ~Contents()
impeller::Contents::ColorFilterProc
std::function< Color(Color)> ColorFilterProc
Definition: contents.h:38
impeller::Contents::ClipCoverage::type
Type type
Definition: contents.h:43
impeller::Snapshot
Represents a texture and its intended draw transform/sampler configuration.
Definition: snapshot.h:25
impeller::RenderPass::GetSampleCount
SampleCount GetSampleCount() const
The sample count of the attached render target.
Definition: render_pass.cc:31
impeller::Contents::ShouldRender
virtual bool ShouldRender(const Entity &entity, const std::optional< Rect > clip_coverage) const
Definition: contents.cc:133
impeller::ContentContextOptions::has_stencil_attachment
bool has_stencil_attachment
Definition: content_context.h:283
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:29
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:67
command_buffer.h
content_context.h
impeller::Contents::RenderProc
std::function< bool(const ContentContext &renderer, const Entity &entity, RenderPass &pass)> RenderProc
Definition: contents.h:49
impeller::ContentContextOptions::sample_count
SampleCount sample_count
Definition: content_context.h:277
impeller::TRect< Scalar >::MakeMaximum
constexpr static TRect MakeMaximum()
Definition: rect.h:72
impeller::Contents::SetInheritedOpacity
virtual void SetInheritedOpacity(Scalar opacity)
Inherit the provided opacity.
Definition: contents.cc:114
impeller::RenderPass::HasStencilAttachment
bool HasStencilAttachment() const
Whether the render target has an stencil attachment.
Definition: render_pass.cc:39
impeller::Contents::ClipCoverage
Definition: contents.h:40
impeller::Contents::MakeAnonymous
static std::shared_ptr< Contents > MakeAnonymous(RenderProc render_proc, CoverageProc coverage_proc)
Definition: contents.cc:35
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:128
impeller::Contents::CoverageProc
std::function< std::optional< Rect >(const Entity &entity)> CoverageProc
Definition: contents.h:50
impeller::Snapshot::texture
std::shared_ptr< Texture > texture
Definition: snapshot.h:26
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:46
impeller::ContentContextOptions
Definition: content_context.h:276
impeller
Definition: aiks_context.cc:10
impeller::ContentContext
Definition: content_context.h:332
impeller::Contents::AsFilter
virtual const FilterContents * AsFilter() const
Cast to a filter. Returns nullptr if this Contents is not a filter.
Definition: contents.cc:124
impeller::Contents::ClipCoverage::Type::kNoChange
@ kNoChange
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:21
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, clip depth,...
Definition: contents.cc:57
impeller::RenderPass::GetRenderTargetPixelFormat
PixelFormat GetRenderTargetPixelFormat() const
The pixel format of the attached render target.
Definition: render_pass.cc:35