Flutter Engine
The Flutter Engine
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"
16
17namespace impeller {
18
21 opts.sample_count = pass.GetSampleCount();
23
24 bool has_depth_stencil_attachments =
27
28 opts.has_depth_stencil_attachments = has_depth_stencil_attachments;
31 return opts;
32}
33
35 const Entity& entity) {
37 opts.blend_mode = entity.GetBlendMode();
38 return opts;
39}
40
41std::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
48Contents::Contents() = default;
49
50Contents::~Contents() = default;
51
52bool Contents::IsOpaque() const {
53 return false;
54}
55
57 const Entity& entity,
58 const std::optional<Rect>& current_clip_coverage) const {
60 .coverage = current_clip_coverage};
61}
62
63std::optional<Snapshot> Contents::RenderToSnapshot(
65 const Entity& entity,
66 std::optional<Rect> coverage_limit,
67 const std::optional<SamplerDescriptor>& sampler_descriptor,
68 bool msaa_enabled,
69 int32_t mip_count,
70 const std::string& label) const {
71 auto coverage = GetCoverage(entity);
72 if (!coverage.has_value()) {
73 return std::nullopt;
74 }
75
76 std::shared_ptr<CommandBuffer> command_buffer =
77 renderer.GetContext()->CreateCommandBuffer();
78 if (!command_buffer) {
79 return std::nullopt;
80 }
81
82 // Pad Contents snapshots with 1 pixel borders to ensure correct sampling
83 // behavior. Not doing so results in a coverage leak for filters that support
84 // customizing the input sampling mode. Snapshots of contents should be
85 // theoretically treated as infinite size just like layers.
86 coverage = coverage->Expand(1);
87
88 if (coverage_limit.has_value()) {
89 coverage = coverage->Intersection(*coverage_limit);
90 if (!coverage.has_value()) {
91 return std::nullopt;
92 }
93 }
94
95 ISize subpass_size = ISize::Ceil(coverage->GetSize());
96 fml::StatusOr<RenderTarget> render_target = renderer.MakeSubpass(
97 label, subpass_size, command_buffer,
98 [&contents = *this, &entity, &coverage](const ContentContext& renderer,
99 RenderPass& pass) -> bool {
100 Entity sub_entity;
102 sub_entity.SetTransform(
104 entity.GetTransform());
105 return contents.Render(renderer, sub_entity, pass);
106 },
107 msaa_enabled, /*depth_stencil_enabled=*/true,
108 std::min(mip_count, static_cast<int32_t>(subpass_size.MipCount())));
109
110 if (!render_target.ok()) {
111 return std::nullopt;
112 }
113 if (!renderer.GetContext()
114 ->GetCommandQueue()
115 ->Submit(/*buffers=*/{std::move(command_buffer)})
116 .ok()) {
117 return std::nullopt;
118 }
119
120 auto snapshot = Snapshot{
121 .texture = render_target.value().GetRenderTargetTexture(),
122 .transform = Matrix::MakeTranslation(coverage->GetOrigin()),
123 };
124 if (sampler_descriptor.has_value()) {
125 snapshot.sampler_descriptor = sampler_descriptor.value();
126 }
127
128 return snapshot;
129}
130
131bool Contents::CanInheritOpacity(const Entity& entity) const {
132 return false;
133}
134
136 VALIDATION_LOG << "Contents::SetInheritedOpacity should never be called when "
137 "Contents::CanAcceptOpacity returns false.";
138}
139
140std::optional<Color> Contents::AsBackgroundColor(const Entity& entity,
141 ISize target_size) const {
142 return {};
143}
144
146 return nullptr;
147}
148
150 const Contents::ColorFilterProc& color_filter_proc) {
151 return false;
152}
153
155 const std::optional<Rect> clip_coverage) const {
156 if (!clip_coverage.has_value()) {
157 return false;
158 }
159 auto coverage = GetCoverage(entity);
160 if (!coverage.has_value()) {
161 return false;
162 }
163 if (coverage == Rect::MakeMaximum()) {
164 return true;
165 }
166 return clip_coverage->IntersectsWithRect(coverage.value());
167}
168
169void Contents::SetCoverageHint(std::optional<Rect> coverage_hint) {
170 coverage_hint_ = coverage_hint;
171}
172
173const std::optional<Rect>& Contents::GetCoverageHint() const {
174 return coverage_hint_;
175}
176
177std::optional<Size> Contents::GetColorSourceSize() const {
178 return color_source_size_;
179};
180
182 color_source_size_ = size;
183}
184
185} // 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:131
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:140
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:177
virtual bool ApplyColorFilter(const ColorFilterProc &color_filter_proc)
If possible, applies a color filter to this contents inputs on the CPU.
Definition: contents.cc:149
const std::optional< Rect > & GetCoverageHint() const
Definition: contents.cc:173
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:63
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:56
void SetColorSourceSize(Size size)
Definition: contents.cc:181
virtual bool IsOpaque() const
Whether this Contents only emits opaque source colors from the fragment stage. This value does not ac...
Definition: contents.cc:52
virtual bool ShouldRender(const Entity &entity, const std::optional< Rect > clip_coverage) const
Definition: contents.cc:154
std::function< std::optional< Rect >(const Entity &entity)> CoverageProc
Definition: contents.h:47
virtual void SetInheritedOpacity(Scalar opacity)
Inherit the provided opacity.
Definition: contents.cc:135
static std::shared_ptr< Contents > MakeAnonymous(RenderProc render_proc, CoverageProc coverage_proc)
Definition: contents.cc:41
std::function< Color(Color)> ColorFilterProc
Definition: contents.h:35
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:169
virtual const FilterContents * AsFilter() const
Cast to a filter. Returns nullptr if this Contents is not a filter.
Definition: contents.cc:145
std::function< bool(const ContentContext &renderer, const Entity &entity, RenderPass &pass)> RenderProc
Definition: contents.h:46
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.
Definition: render_pass.cc:23
PixelFormat GetRenderTargetPixelFormat() const
The pixel format of the attached render target.
Definition: render_pass.cc:27
bool HasStencilAttachment() const
Whether the render target has an stencil attachment.
Definition: render_pass.cc:35
bool HasDepthAttachment() const
Whether the render target has a depth attachment.
Definition: render_pass.cc:31
#define FML_DCHECK(condition)
Definition: logging.h:103
static float min(float r, float g, float b)
Definition: hsl.cpp:48
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
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:34
ContentContextOptions OptionsFromPass(const RenderPass &pass)
Definition: contents.cc:19
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