| 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/validation.h"
10 #include "impeller/core/formats.h"
11 #include "impeller/entity/contents/anonymous_contents.h"
12 #include "impeller/entity/contents/content_context.h"
13 #include "impeller/entity/entity.h"
14 #include "impeller/renderer/command_buffer.h"
15 #include "impeller/renderer/render_pass.h"
16
17 namespace impeller {
18
19 ContentContextOptions OptionsFromPass(const RenderPass& pass) {
20 ContentContextOptions opts;
21 opts.sample_count = pass.GetSampleCount();
22 opts.color_attachment_pixel_format = pass.GetRenderTargetPixelFormat();
23
24 bool has_depth_stencil_attachments =
25 pass.HasDepthAttachment() && pass.HasStencilAttachment();
26 FML_DCHECK(pass.HasDepthAttachment() == pass.HasStencilAttachment());
27
28 opts.has_depth_stencil_attachments = has_depth_stencil_attachments;
29 opts.depth_compare = CompareFunction::kGreaterEqual;
30 opts.stencil_mode = ContentContextOptions::StencilMode::kIgnore;
31 return opts;
32 }
33
34 ContentContextOptions OptionsFromPassAndEntity(const RenderPass& pass,
35 const Entity& entity) {
36 ContentContextOptions opts = OptionsFromPass(pass);
37 opts.blend_mode = entity.GetBlendMode();
38 return opts;
39 }
40
41 std::shared_ptr<Contents> Contents::MakeAnonymous(
42 Contents::RenderProc render_proc,
43 Contents::CoverageProc coverage_proc) {
44 return AnonymousContents::Make(std::move(render_proc),
45 std::move(coverage_proc));
46 }
47
48 Contents::Contents() = default;
49
50 Contents::~Contents() = default;
51
52 bool Contents::IsOpaque(const Matrix& transform) const {
53 return false;
54 }
55
56 std::optional<Snapshot> Contents::RenderToSnapshot(
57 const ContentContext& renderer,
58 const Entity& entity,
59 const Contents::SnapshotOptions& options) const {
60 auto coverage = GetCoverage(entity);
61 if (!coverage.has_value()) {
62 return std::nullopt;
63 }
64
65 std::shared_ptr<CommandBuffer> command_buffer =
66 renderer.GetContext()->CreateCommandBuffer();
67 if (!command_buffer) {
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(options.coverage_expansion);
76
77 if (options.coverage_limit.has_value()) {
78 coverage = coverage->Intersection(*options.coverage_limit);
79 if (!coverage.has_value()) {
80 return std::nullopt;
81 }
82 }
83
84 ISize subpass_size = ISize::Ceil(coverage->GetSize());
85 fml::StatusOr<RenderTarget> render_target = renderer.MakeSubpass(
86 options.label, subpass_size, command_buffer,
87 [&contents = *this, &entity, &coverage](const ContentContext& renderer,
88 RenderPass& pass) -> bool {
89 Entity sub_entity;
90 sub_entity.SetBlendMode(BlendMode::kSrcOver);
91 sub_entity.SetTransform(
92 Matrix::MakeTranslation(Vector3(-coverage->GetOrigin())) *
93 entity.GetTransform());
94 return contents.Render(renderer, sub_entity, pass);
95 },
96 options.msaa_enabled, /*depth_stencil_enabled=*/true,
97 std::min(options.mip_count,
98 static_cast<int32_t>(subpass_size.MipCount())));
99
100 if (!render_target.ok()) {
101 return std::nullopt;
102 }
103 if (!renderer.GetContext()->EnqueueCommandBuffer(std::move(command_buffer))) {
104 return std::nullopt;
105 }
106
107 auto snapshot = Snapshot{
108 .texture = render_target.value().GetRenderTargetTexture(),
109 .transform = Matrix::MakeTranslation(coverage->GetOrigin()),
110 };
111 if (options.sampler_descriptor.has_value()) {
112 snapshot.sampler_descriptor = options.sampler_descriptor.value();
113 }
114
115 return snapshot;
116 }
117
118 void Contents::SetInheritedOpacity(Scalar opacity) {
119 VALIDATION_LOG << "Contents::SetInheritedOpacity should never be called when "
120 "Contents::CanAcceptOpacity returns false.";
121 }
122
123 std::optional<Color> Contents::AsBackgroundColor(const Entity& entity,
124 ISize target_size) const {
125 return {};
126 }
127
128 bool Contents::ApplyColorFilter(
129 const Contents::ColorFilterProc& color_filter_proc) {
130 return false;
131 }
132
133 void Contents::SetCoverageHint(std::optional<Rect> coverage_hint) {
134 coverage_hint_ = coverage_hint;
135 }
136
137 const std::optional<Rect>& Contents::GetCoverageHint() const {
138 return coverage_hint_;
139 }
140
141 std::optional<Size> Contents::GetColorSourceSize() const {
142 return color_source_size_;
143 };
144
145 void Contents::SetColorSourceSize(Size size) {
146 color_source_size_ = size;
147 }
148
149 } // namespace impeller
anonymous_contents.h
impeller::AnonymousContents::Make
static std::shared_ptr< Contents > Make(RenderProc render_proc, CoverageProc coverage_proc)
Definition: anonymous_contents.cc:11
impeller::ContentContext
Definition: content_context.h:138
impeller::ContentContext::MakeSubpass
fml::StatusOr< RenderTarget > MakeSubpass(std::string_view label, ISize texture_size, const std::shared_ptr< CommandBuffer > &command_buffer, const SubpassCallback &subpass_callback, bool msaa_enabled=true, bool depth_stencil_enabled=false, int32_t mip_count=1) const
Creates a new texture of size texture_size and calls subpass_callback with a RenderPass for drawing t...
Definition: content_context.cc:881
impeller::ContentContext::GetContext
std::shared_ptr< Context > GetContext() const
Definition: content_context.cc:953
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:123
impeller::Contents::~Contents
virtual ~Contents()
impeller::Contents::GetColorSourceSize
std::optional< Size > GetColorSourceSize() const
Return the color source's intrinsic size, if available.
Definition: contents.cc:141
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::GetCoverageHint
const std::optional< Rect > & GetCoverageHint() const
Definition: contents.cc:137
impeller::Contents::IsOpaque
virtual bool IsOpaque(const Matrix &transform) const
Whether this Contents only emits opaque source colors from the fragment stage. This value does not ac...
Definition: contents.cc:52
impeller::Contents::RenderToSnapshot
virtual std::optional< Snapshot > RenderToSnapshot(const ContentContext &renderer, const Entity &entity, const SnapshotOptions &options) const
Render this contents to a snapshot, respecting the entity's transform, path, clip depth,...
Definition: contents.cc:56
impeller::Contents::SetColorSourceSize
void SetColorSourceSize(Size size)
Definition: contents.cc:145
impeller::Contents::Contents
Contents()
impeller::Contents::CoverageProc
std::function< std::optional< Rect >(const Entity &entity)> CoverageProc
Definition: contents.h:40
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::Contents::SetInheritedOpacity
virtual void SetInheritedOpacity(Scalar opacity)
Inherit the provided opacity.
Definition: contents.cc:118
impeller::Contents::MakeAnonymous
static std::shared_ptr< Contents > MakeAnonymous(RenderProc render_proc, CoverageProc coverage_proc)
Definition: contents.cc:41
impeller::Contents::ColorFilterProc
std::function< Color(Color)> ColorFilterProc
Definition: contents.h:35
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:133
impeller::Contents::RenderProc
std::function< bool(const ContentContext &renderer, const Entity &entity, RenderPass &pass)> RenderProc
Definition: contents.h:39
impeller::Entity
Definition: entity.h:26
impeller::Entity::GetBlendMode
BlendMode GetBlendMode() const
Definition: entity.cc:101
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:30
impeller::RenderPass::GetSampleCount
SampleCount GetSampleCount() const
The sample count of the attached render target.
Definition: render_pass.cc:27
impeller::RenderPass::GetRenderTargetPixelFormat
PixelFormat GetRenderTargetPixelFormat() const
The pixel format of the attached render target.
Definition: render_pass.cc:31
impeller::RenderPass::HasStencilAttachment
bool HasStencilAttachment() const
Whether the render target has an stencil attachment.
Definition: render_pass.cc:39
impeller::RenderPass::HasDepthAttachment
bool HasDepthAttachment() const
Whether the render target has a depth attachment.
Definition: render_pass.cc:35
command_buffer.h
content_context.h
contents.h
formats.h
entity.h
subpass_size
ISize subpass_size
The output size of the down-sampling pass.
Definition: gaussian_blur_filter_contents.cc:202
transform
Matrix transform
Definition: gaussian_blur_filter_contents.cc:214
impeller
Definition: allocation.cc:12
impeller::Scalar
float Scalar
Definition: scalar.h:19
impeller::CompareFunction::kGreaterEqual
@ kGreaterEqual
Comparison test passes if new_value >= current_value.
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:34
impeller::OptionsFromPass
ContentContextOptions OptionsFromPass(const RenderPass &pass)
Definition: contents.cc:19
render_pass.h
impeller::ContentContextOptions
Definition: content_context.h:40
impeller::ContentContextOptions::has_depth_stencil_attachments
bool has_depth_stencil_attachments
Definition: content_context.h:99
impeller::ContentContextOptions::stencil_mode
StencilMode stencil_mode
Definition: content_context.h:96
impeller::ContentContextOptions::blend_mode
BlendMode blend_mode
Definition: content_context.h:94
impeller::ContentContextOptions::color_attachment_pixel_format
PixelFormat color_attachment_pixel_format
Definition: content_context.h:98
impeller::ContentContextOptions::StencilMode::kIgnore
@ kIgnore
impeller::ContentContextOptions::sample_count
SampleCount sample_count
Definition: content_context.h:93
impeller::ContentContextOptions::depth_compare
CompareFunction depth_compare
Definition: content_context.h:95
impeller::Contents::SnapshotOptions
Definition: contents.h:85
impeller::Contents::SnapshotOptions::label
std::string_view label
Definition: contents.h:90
impeller::Contents::SnapshotOptions::mip_count
int32_t mip_count
Definition: contents.h:89
impeller::Contents::SnapshotOptions::sampler_descriptor
const std::optional< SamplerDescriptor > & sampler_descriptor
Definition: contents.h:87
impeller::Contents::SnapshotOptions::coverage_expansion
int32_t coverage_expansion
Definition: contents.h:91
impeller::Contents::SnapshotOptions::coverage_limit
std::optional< Rect > coverage_limit
Definition: contents.h:86
impeller::Contents::SnapshotOptions::msaa_enabled
bool msaa_enabled
Definition: contents.h:88
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::Matrix::MakeTranslation
static constexpr Matrix MakeTranslation(const Vector3 &t)
Definition: matrix.h:95
impeller::Snapshot
Represents a texture and its intended draw transform/sampler configuration.
Definition: snapshot.h:24
impeller::Snapshot::texture
std::shared_ptr< Texture > texture
Definition: snapshot.h:25
impeller::TSize
Definition: size.h:25
impeller::TSize::Ceil
constexpr TSize Ceil() const
Definition: size.h:114
validation.h
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91