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"
10 #include "impeller/core/formats.h"
13 #include "impeller/entity/entity.h"
16 
17 namespace impeller {
18 
21  opts.sample_count = pass.GetSampleCount();
23 
24  bool has_depth_stencil_attachments =
26  FML_DCHECK(pass.HasDepthAttachment() == pass.HasStencilAttachment());
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 
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  std::optional<Rect> coverage_limit,
60  const std::optional<SamplerDescriptor>& sampler_descriptor,
61  bool msaa_enabled,
62  int32_t mip_count,
63  std::string_view label) const {
64  auto coverage = GetCoverage(entity);
65  if (!coverage.has_value()) {
66  return std::nullopt;
67  }
68 
69  std::shared_ptr<CommandBuffer> command_buffer =
70  renderer.GetContext()->CreateCommandBuffer();
71  if (!command_buffer) {
72  return std::nullopt;
73  }
74 
75  // Pad Contents snapshots with 1 pixel borders to ensure correct sampling
76  // behavior. Not doing so results in a coverage leak for filters that support
77  // customizing the input sampling mode. Snapshots of contents should be
78  // theoretically treated as infinite size just like layers.
79  coverage = coverage->Expand(1);
80 
81  if (coverage_limit.has_value()) {
82  coverage = coverage->Intersection(*coverage_limit);
83  if (!coverage.has_value()) {
84  return std::nullopt;
85  }
86  }
87 
88  ISize subpass_size = ISize::Ceil(coverage->GetSize());
89  fml::StatusOr<RenderTarget> render_target = renderer.MakeSubpass(
90  label, subpass_size, command_buffer,
91  [&contents = *this, &entity, &coverage](const ContentContext& renderer,
92  RenderPass& pass) -> bool {
93  Entity sub_entity;
95  sub_entity.SetTransform(
96  Matrix::MakeTranslation(Vector3(-coverage->GetOrigin())) *
97  entity.GetTransform());
98  return contents.Render(renderer, sub_entity, pass);
99  },
100  msaa_enabled, /*depth_stencil_enabled=*/true,
101  std::min(mip_count, static_cast<int32_t>(subpass_size.MipCount())));
102 
103  if (!render_target.ok()) {
104  return std::nullopt;
105  }
106  if (!renderer.GetContext()->EnqueueCommandBuffer(std::move(command_buffer))) {
107  return std::nullopt;
108  }
109 
110  auto snapshot = Snapshot{
111  .texture = render_target.value().GetRenderTargetTexture(),
112  .transform = Matrix::MakeTranslation(coverage->GetOrigin()),
113  };
114  if (sampler_descriptor.has_value()) {
115  snapshot.sampler_descriptor = sampler_descriptor.value();
116  }
117 
118  return snapshot;
119 }
120 
122  VALIDATION_LOG << "Contents::SetInheritedOpacity should never be called when "
123  "Contents::CanAcceptOpacity returns false.";
124 }
125 
126 std::optional<Color> Contents::AsBackgroundColor(const Entity& entity,
127  ISize target_size) const {
128  return {};
129 }
130 
132  const Contents::ColorFilterProc& color_filter_proc) {
133  return false;
134 }
135 
136 void Contents::SetCoverageHint(std::optional<Rect> coverage_hint) {
137  coverage_hint_ = coverage_hint;
138 }
139 
140 const std::optional<Rect>& Contents::GetCoverageHint() const {
141  return coverage_hint_;
142 }
143 
144 std::optional<Size> Contents::GetColorSourceSize() const {
145  return color_source_size_;
146 };
147 
149  color_source_size_ = size;
150 }
151 
152 } // namespace impeller
static std::shared_ptr< Contents > Make(RenderProc render_proc, CoverageProc coverage_proc)
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...
std::shared_ptr< Context > GetContext() const
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:126
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, std::string_view label="Snapshot") const
Render this contents to a snapshot, respecting the entity's transform, path, clip depth,...
Definition: contents.cc:56
std::optional< Size > GetColorSourceSize() const
Return the color source's intrinsic size, if available.
Definition: contents.cc:144
virtual bool ApplyColorFilter(const ColorFilterProc &color_filter_proc)
If possible, applies a color filter to this contents inputs on the CPU.
Definition: contents.cc:131
const std::optional< Rect > & GetCoverageHint() const
Definition: contents.cc:140
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
void SetColorSourceSize(Size size)
Definition: contents.cc:148
std::function< std::optional< Rect >(const Entity &entity)> CoverageProc
Definition: contents.h:40
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.
virtual void SetInheritedOpacity(Scalar opacity)
Inherit the provided opacity.
Definition: contents.cc:121
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:136
std::function< bool(const ContentContext &renderer, const Entity &entity, RenderPass &pass)> RenderProc
Definition: contents.h:39
void SetTransform(const Matrix &transform)
Set the global transform matrix for this Entity.
Definition: entity.cc:60
BlendMode GetBlendMode() const
Definition: entity.cc:101
void SetBlendMode(BlendMode blend_mode)
Definition: entity.cc:97
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:44
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:30
SampleCount GetSampleCount() const
The sample count of the attached render target.
Definition: render_pass.cc:27
PixelFormat GetRenderTargetPixelFormat() const
The pixel format of the attached render target.
Definition: render_pass.cc:31
bool HasStencilAttachment() const
Whether the render target has an stencil attachment.
Definition: render_pass.cc:39
bool HasDepthAttachment() const
Whether the render target has a depth attachment.
Definition: render_pass.cc:35
ISize subpass_size
The output size of the down-sampling pass.
float Scalar
Definition: scalar.h:19
@ kGreaterEqual
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
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
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
constexpr TSize Ceil() const
Definition: size.h:114
#define VALIDATION_LOG
Definition: validation.h:91