Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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
6#include <optional>
7
8#include "fml/logging.h"
17
18namespace impeller {
19
22 opts.sample_count = pass.GetSampleCount();
24
25 bool has_depth_stencil_attachments =
28
29 opts.has_depth_stencil_attachments = has_depth_stencil_attachments;
32 return opts;
33}
34
36 const Entity& entity) {
38 opts.blend_mode = entity.GetBlendMode();
39 return opts;
40}
41
42std::shared_ptr<Contents> Contents::MakeAnonymous(
43 Contents::RenderProc render_proc,
44 Contents::CoverageProc coverage_proc) {
45 return AnonymousContents::Make(std::move(render_proc),
46 std::move(coverage_proc));
47}
48
49Contents::Contents() = default;
50
51Contents::~Contents() = default;
52
53bool Contents::IsOpaque() const {
54 return false;
55}
56
58 const Entity& entity,
59 const std::optional<Rect>& current_clip_coverage) const {
61 .coverage = current_clip_coverage};
62}
63
64std::optional<Snapshot> Contents::RenderToSnapshot(
65 const ContentContext& renderer,
66 const Entity& entity,
67 std::optional<Rect> coverage_limit,
68 const std::optional<SamplerDescriptor>& sampler_descriptor,
69 bool msaa_enabled,
70 int32_t mip_count,
71 const std::string& label) const {
72 auto coverage = GetCoverage(entity);
73 if (!coverage.has_value()) {
74 return std::nullopt;
75 }
76
77 std::shared_ptr<CommandBuffer> command_buffer =
78 renderer.GetContext()->CreateCommandBuffer();
79 if (!command_buffer) {
80 return std::nullopt;
81 }
82
83 // Pad Contents snapshots with 1 pixel borders to ensure correct sampling
84 // behavior. Not doing so results in a coverage leak for filters that support
85 // customizing the input sampling mode. Snapshots of contents should be
86 // theoretically treated as infinite size just like layers.
87 coverage = coverage->Expand(1);
88
89 if (coverage_limit.has_value()) {
90 coverage = coverage->Intersection(*coverage_limit);
91 if (!coverage.has_value()) {
92 return std::nullopt;
93 }
94 }
95
96 ISize subpass_size = ISize::Ceil(coverage->GetSize());
97 fml::StatusOr<RenderTarget> render_target = renderer.MakeSubpass(
98 label, subpass_size, command_buffer,
99 [&contents = *this, &entity, &coverage](const ContentContext& renderer,
100 RenderPass& pass) -> bool {
101 Entity sub_entity;
103 sub_entity.SetTransform(
104 Matrix::MakeTranslation(Vector3(-coverage->GetOrigin())) *
105 entity.GetTransform());
106 return contents.Render(renderer, sub_entity, pass);
107 },
108 msaa_enabled, /*depth_stencil_enabled=*/true,
109 std::min(mip_count, static_cast<int32_t>(subpass_size.MipCount())));
110
111 if (!render_target.ok()) {
112 return std::nullopt;
113 }
114 if (!renderer.GetContext()
115 ->GetCommandQueue()
116 ->Submit(/*buffers=*/{std::move(command_buffer)})
117 .ok()) {
118 return std::nullopt;
119 }
120
121 auto snapshot = Snapshot{
122 .texture = render_target.value().GetRenderTargetTexture(),
123 .transform = Matrix::MakeTranslation(coverage->GetOrigin()),
124 };
125 if (sampler_descriptor.has_value()) {
126 snapshot.sampler_descriptor = sampler_descriptor.value();
127 }
128
129 return snapshot;
130}
131
132bool Contents::CanInheritOpacity(const Entity& entity) const {
133 return false;
134}
135
137 VALIDATION_LOG << "Contents::SetInheritedOpacity should never be called when "
138 "Contents::CanAcceptOpacity returns false.";
139}
140
141std::optional<Color> Contents::AsBackgroundColor(const Entity& entity,
142 ISize target_size) const {
143 return {};
144}
145
147 return nullptr;
148}
149
151 const Contents::ColorFilterProc& color_filter_proc) {
152 return false;
153}
154
156 const std::optional<Rect> clip_coverage) const {
157 if (!clip_coverage.has_value()) {
158 return false;
159 }
160 auto coverage = GetCoverage(entity);
161 if (!coverage.has_value()) {
162 return false;
163 }
164 if (coverage == Rect::MakeMaximum()) {
165 return true;
166 }
167 return clip_coverage->IntersectsWithRect(coverage.value());
168}
169
170void Contents::SetCoverageHint(std::optional<Rect> coverage_hint) {
171 coverage_hint_ = coverage_hint;
172}
173
174const std::optional<Rect>& Contents::GetCoverageHint() const {
175 return coverage_hint_;
176}
177
178std::optional<Size> Contents::GetColorSourceSize() const {
179 return color_source_size_;
180};
181
183 color_source_size_ = size;
184}
185
186} // namespace impeller
static bool ok(int result)
const T & value() const
Definition status_or.h:77
bool ok() const
Definition status_or.h:75
static std::shared_ptr< Contents > Make(RenderProc render_proc, CoverageProc coverage_proc)
virtual bool CanInheritOpacity(const Entity &entity) const
Whether or not this contents can accept the opacity peephole optimization.
Definition contents.cc:132
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:141
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.
std::optional< Size > GetColorSourceSize() const
Return the color source's intrinsic size, if available.
Definition contents.cc:178
virtual bool ApplyColorFilter(const ColorFilterProc &color_filter_proc)
If possible, applies a color filter to this contents inputs on the CPU.
Definition contents.cc:150
const std::optional< Rect > & GetCoverageHint() const
Definition contents.cc:174
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, int32_t mip_count=1, const std::string &label="Snapshot") const
Render this contents to a snapshot, respecting the entity's transform, path, clip depth,...
Definition contents.cc:64
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:57
void SetColorSourceSize(Size size)
Definition contents.cc:182
virtual bool IsOpaque() const
Whether this Contents only emits opaque source colors from the fragment stage. This value does not ac...
Definition contents.cc:53
virtual bool ShouldRender(const Entity &entity, const std::optional< Rect > clip_coverage) const
Definition contents.cc:155
std::function< std::optional< Rect >(const Entity &entity)> CoverageProc
Definition contents.h:50
virtual void SetInheritedOpacity(Scalar opacity)
Inherit the provided opacity.
Definition contents.cc:136
static std::shared_ptr< Contents > MakeAnonymous(RenderProc render_proc, CoverageProc coverage_proc)
Definition contents.cc:42
std::function< Color(Color)> ColorFilterProc
Definition contents.h:38
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:170
virtual const FilterContents * AsFilter() const
Cast to a filter. Returns nullptr if this Contents is not a filter.
Definition contents.cc:146
std::function< bool(const ContentContext &renderer, const Entity &entity, RenderPass &pass)> RenderProc
Definition contents.h:49
void SetTransform(const Matrix &transform)
Set the global transform matrix for this Entity.
Definition entity.cc:62
BlendMode GetBlendMode() const
Definition entity.cc:119
void SetBlendMode(BlendMode blend_mode)
Definition entity.cc:115
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition entity.cc:46
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition render_pass.h:33
SampleCount GetSampleCount() const
The sample count of the attached render target.
PixelFormat GetRenderTargetPixelFormat() const
The pixel format of the attached render target.
bool HasStencilAttachment() const
Whether the render target has an stencil attachment.
bool HasDepthAttachment() const
Whether the render target has a depth attachment.
#define FML_DCHECK(condition)
Definition logging.h:103
float Scalar
Definition scalar.h:18
@ kGreater
Comparison test passes if new_value > current_value.
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition contents.cc:35
ContentContextOptions OptionsFromPass(const RenderPass &pass)
Definition contents.cc:20
static constexpr Matrix MakeTranslation(const Vector3 &t)
Definition matrix.h:95
Represents a texture and its intended draw transform/sampler configuration.
Definition snapshot.h:24
std::shared_ptr< Texture > texture
Definition snapshot.h:25
static constexpr TRect MakeMaximum()
Definition rect.h:174
constexpr TSize Ceil() const
Definition size.h:96
constexpr size_t MipCount() const
Definition size.h:115
#define VALIDATION_LOG
Definition validation.h:73