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 
6 #include <optional>
7 
8 #include "fml/logging.h"
11 #include "impeller/core/formats.h"
17 
18 namespace impeller {
19 
22  opts.sample_count = pass.GetSampleCount();
24 
25  bool has_depth_stencil_attachments =
27  FML_DCHECK(pass.HasDepthAttachment() == pass.HasStencilAttachment());
28 
29  opts.has_depth_stencil_attachments = has_depth_stencil_attachments;
33  }
34  return opts;
35 }
36 
38  const Entity& entity) {
40  opts.blend_mode = entity.GetBlendMode();
41  return opts;
42 }
43 
44 std::shared_ptr<Contents> Contents::MakeAnonymous(
45  Contents::RenderProc render_proc,
46  Contents::CoverageProc coverage_proc) {
47  return AnonymousContents::Make(std::move(render_proc),
48  std::move(coverage_proc));
49 }
50 
51 Contents::Contents() = default;
52 
53 Contents::~Contents() = default;
54 
55 bool Contents::IsOpaque() const {
56  return false;
57 }
58 
60  const Entity& entity,
61  const std::optional<Rect>& current_clip_coverage) const {
63  .coverage = current_clip_coverage};
64 }
65 
66 std::optional<Snapshot> Contents::RenderToSnapshot(
67  const ContentContext& renderer,
68  const Entity& entity,
69  std::optional<Rect> coverage_limit,
70  const std::optional<SamplerDescriptor>& sampler_descriptor,
71  bool msaa_enabled,
72  int32_t mip_count,
73  const std::string& label) const {
74  auto coverage = GetCoverage(entity);
75  if (!coverage.has_value()) {
76  return std::nullopt;
77  }
78 
79  // Pad Contents snapshots with 1 pixel borders to ensure correct sampling
80  // behavior. Not doing so results in a coverage leak for filters that support
81  // customizing the input sampling mode. Snapshots of contents should be
82  // theoretically treated as infinite size just like layers.
83  coverage = coverage->Expand(1);
84 
85  if (coverage_limit.has_value()) {
86  coverage = coverage->Intersection(*coverage_limit);
87  if (!coverage.has_value()) {
88  return std::nullopt;
89  }
90  }
91 
92  ISize subpass_size = ISize::Ceil(coverage->GetSize());
93  fml::StatusOr<RenderTarget> render_target = renderer.MakeSubpass(
94  label, subpass_size,
95  [&contents = *this, &entity, &coverage](const ContentContext& renderer,
96  RenderPass& pass) -> bool {
97  Entity sub_entity;
99  sub_entity.SetTransform(
100  Matrix::MakeTranslation(Vector3(-coverage->GetOrigin())) *
101  entity.GetTransform());
102  return contents.Render(renderer, sub_entity, pass);
103  },
104  msaa_enabled, /*depth_stencil_enabled=*/true,
105  std::min(mip_count, static_cast<int32_t>(subpass_size.MipCount())));
106 
107  if (!render_target.ok()) {
108  return std::nullopt;
109  }
110 
111  auto snapshot = Snapshot{
112  .texture = render_target.value().GetRenderTargetTexture(),
113  .transform = Matrix::MakeTranslation(coverage->GetOrigin()),
114  };
115  if (sampler_descriptor.has_value()) {
116  snapshot.sampler_descriptor = sampler_descriptor.value();
117  }
118 
119  return snapshot;
120 }
121 
122 bool Contents::CanInheritOpacity(const Entity& entity) const {
123  return false;
124 }
125 
127  VALIDATION_LOG << "Contents::SetInheritedOpacity should never be called when "
128  "Contents::CanAcceptOpacity returns false.";
129 }
130 
131 std::optional<Color> Contents::AsBackgroundColor(const Entity& entity,
132  ISize target_size) const {
133  return {};
134 }
135 
137  return nullptr;
138 }
139 
141  const Contents::ColorFilterProc& color_filter_proc) {
142  return false;
143 }
144 
145 bool Contents::ShouldRender(const Entity& entity,
146  const std::optional<Rect> clip_coverage) const {
147  if (!clip_coverage.has_value()) {
148  return false;
149  }
150  auto coverage = GetCoverage(entity);
151  if (!coverage.has_value()) {
152  return false;
153  }
154  if (coverage == Rect::MakeMaximum()) {
155  return true;
156  }
157  return clip_coverage->IntersectsWithRect(coverage.value());
158 }
159 
160 void Contents::SetCoverageHint(std::optional<Rect> coverage_hint) {
161  coverage_hint_ = coverage_hint;
162 }
163 
164 const std::optional<Rect>& Contents::GetCoverageHint() const {
165  return coverage_hint_;
166 }
167 
168 std::optional<Size> Contents::GetColorSourceSize() const {
169  return color_source_size_;
170 };
171 
173  color_source_size_ = size;
174 }
175 
176 } // namespace impeller
impeller::ContentContextOptions::StencilMode::kIgnore
@ kIgnore
Turn the stencil test off. Used when drawing without stencil-then-cover.
impeller::Contents::GetColorSourceSize
std::optional< Size > GetColorSourceSize() const
Return the color source's intrinsic size, if available.
Definition: contents.cc:168
impeller::OptionsFromPass
ContentContextOptions OptionsFromPass(const RenderPass &pass)
Definition: contents.cc:20
impeller::CompareFunction::kGreater
@ kGreater
Comparison test passes if new_value > current_value.
contents.h
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::Entity::SetBlendMode
void SetBlendMode(BlendMode blend_mode)
Definition: entity.cc:130
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:160
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:131
texture_contents.h
impeller::Entity::GetTransform
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:49
impeller::Contents::SetColorSourceSize
void SetColorSourceSize(Size size)
Definition: contents.cc:172
impeller::Contents::CanInheritOpacity
virtual bool CanInheritOpacity(const Entity &entity) const
Whether or not this contents can accept the opacity peephole optimization.
Definition: contents.cc:122
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, 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:66
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::has_depth_stencil_attachments
bool has_depth_stencil_attachments
Definition: content_context.h:339
impeller::ContentContextOptions::blend_mode
BlendMode blend_mode
Definition: content_context.h:333
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:59
impeller::ContentContext::kEnableStencilThenCover
static constexpr bool kEnableStencilThenCover
Definition: content_context.h:411
impeller::Entity
Definition: entity.h:21
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:37
impeller::Contents::GetCoverageHint
const std::optional< Rect > & GetCoverageHint() const
Definition: contents.cc:164
impeller::TSize< int64_t >
impeller::ContentContextOptions::color_attachment_pixel_format
PixelFormat color_attachment_pixel_format
Definition: content_context.h:338
render_pass.h
impeller::Contents::~Contents
virtual ~Contents()
impeller::ContentContext::MakeSubpass
fml::StatusOr< RenderTarget > MakeSubpass(const std::string &label, ISize texture_size, 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:478
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:23
impeller::Contents::ShouldRender
virtual bool ShouldRender(const Entity &entity, const std::optional< Rect > clip_coverage) const
Definition: contents.cc:145
impeller::ContentContextOptions::stencil_mode
StencilMode stencil_mode
Definition: content_context.h:335
strings.h
impeller::RenderPass::HasDepthAttachment
bool HasDepthAttachment() const
Whether the render target has a depth attachment.
Definition: render_pass.cc:31
anonymous_contents.h
impeller::Entity::GetBlendMode
BlendMode GetBlendMode() const
Definition: entity.cc:134
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:33
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:73
command_buffer.h
content_context.h
impeller::ContentContextOptions::depth_compare
CompareFunction depth_compare
Definition: content_context.h:334
impeller::Entity::SetTransform
void SetTransform(const Matrix &transform)
Set the global transform matrix for this Entity.
Definition: entity.cc:65
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:332
impeller::TRect< Scalar >::MakeMaximum
constexpr static TRect MakeMaximum()
Definition: rect.h:174
impeller::Contents::SetInheritedOpacity
virtual void SetInheritedOpacity(Scalar opacity)
Inherit the provided opacity.
Definition: contents.cc:126
impeller::RenderPass::HasStencilAttachment
bool HasStencilAttachment() const
Whether the render target has an stencil attachment.
Definition: render_pass.cc:35
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:44
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:140
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::TSize::MipCount
constexpr size_t MipCount() const
Definition: size.h:115
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:55
impeller::ContentContextOptions
Definition: content_context.h:288
impeller
Definition: aiks_blur_unittests.cc:20
impeller::ContentContext
Definition: content_context.h:392
impeller::Contents::AsFilter
virtual const FilterContents * AsFilter() const
Cast to a filter. Returns nullptr if this Contents is not a filter.
Definition: contents.cc:136
impeller::Contents::ClipCoverage::Type::kNoChange
@ kNoChange
impeller::Vector3
Definition: vector.h:20
impeller::BlendMode::kSourceOver
@ kSourceOver
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:22
impeller::RenderPass::GetRenderTargetPixelFormat
PixelFormat GetRenderTargetPixelFormat() const
The pixel format of the attached render target.
Definition: render_pass.cc:27