Flutter Impeller
contents.h
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 #ifndef FLUTTER_IMPELLER_ENTITY_CONTENTS_CONTENTS_H_
6 #define FLUTTER_IMPELLER_ENTITY_CONTENTS_CONTENTS_H_
7 
8 #include <functional>
9 #include <memory>
10 
13 #include "impeller/geometry/rect.h"
16 
17 namespace impeller {
18 
19 class ContentContext;
20 struct ContentContextOptions;
21 class Entity;
22 class Surface;
23 class RenderPass;
24 class FilterContents;
25 
26 ContentContextOptions OptionsFromPass(const RenderPass& pass);
27 
28 ContentContextOptions OptionsFromPassAndEntity(const RenderPass& pass,
29  const Entity& entity);
30 
31 class Contents {
32  public:
33  /// A procedure that filters a given unpremultiplied color to produce a new
34  /// unpremultiplied color.
35  using ColorFilterProc = std::function<Color(Color)>;
36 
37  struct ClipCoverage {
38  enum class Type { kNoChange, kAppend, kRestore };
39 
41  // TODO(jonahwilliams): this should probably use the Entity::ClipOperation
42  // enum, but that has transitive import errors.
44 
45  /// @brief This coverage is the outer coverage of the clip.
46  ///
47  /// For example, if the clip is a circular clip, this is the rectangle that
48  /// contains the circle and not the rectangle that is contained within the
49  /// circle. This means that we cannot use the coverage alone to determine if
50  /// a clip can be culled, and instead also use the somewhat hacky
51  /// "is_difference_or_non_square" field.
52  std::optional<Rect> coverage = std::nullopt;
53  };
54 
55  using RenderProc = std::function<bool(const ContentContext& renderer,
56  const Entity& entity,
57  RenderPass& pass)>;
58  using CoverageProc = std::function<std::optional<Rect>(const Entity& entity)>;
59 
60  static std::shared_ptr<Contents> MakeAnonymous(RenderProc render_proc,
61  CoverageProc coverage_proc);
62 
63  Contents();
64 
65  virtual ~Contents();
66 
67  virtual bool Render(const ContentContext& renderer,
68  const Entity& entity,
69  RenderPass& pass) const = 0;
70 
71  //----------------------------------------------------------------------------
72  /// @brief Get the area of the render pass that will be affected when this
73  /// contents is rendered.
74  ///
75  /// During rendering, coverage coordinates count pixels from the top
76  /// left corner of the framebuffer.
77  ///
78  /// @return The coverage rectangle. An `std::nullopt` result means that
79  /// rendering this contents has no effect on the output color.
80  ///
81  virtual std::optional<Rect> GetCoverage(const Entity& entity) const = 0;
82 
83  //----------------------------------------------------------------------------
84  /// @brief Hint that specifies the coverage area of this Contents that will
85  /// actually be used during rendering. This is for optimization
86  /// purposes only and can not be relied on as a clip. May optionally
87  /// affect the result of `GetCoverage()`.
88  ///
89  void SetCoverageHint(std::optional<Rect> coverage_hint);
90 
91  const std::optional<Rect>& GetCoverageHint() const;
92 
93  //----------------------------------------------------------------------------
94  /// @brief Whether this Contents only emits opaque source colors from the
95  /// fragment stage. This value does not account for any entity
96  /// properties (e.g. the blend mode), clips/visibility culling, or
97  /// inherited opacity.
98  ///
99  /// @param transform The current transform matrix of the entity that will
100  /// render this contents.
101  virtual bool IsOpaque(const Matrix& transform) const;
102 
103  //----------------------------------------------------------------------------
104  /// @brief Given the current pass space bounding rectangle of the clip
105  /// buffer, return the expected clip coverage after this draw call.
106  /// This should only be implemented for contents that may write to the
107  /// clip buffer.
108  ///
109  /// During rendering, coverage coordinates count pixels from the top
110  /// left corner of the framebuffer.
111  ///
113  const Entity& entity,
114  const std::optional<Rect>& current_clip_coverage) const;
115 
116  //----------------------------------------------------------------------------
117  /// @brief Render this contents to a snapshot, respecting the entity's
118  /// transform, path, clip depth, and blend mode.
119  /// The result texture size is always the size of
120  /// `GetCoverage(entity)`.
121  ///
122  virtual std::optional<Snapshot> RenderToSnapshot(
123  const ContentContext& renderer,
124  const Entity& entity,
125  std::optional<Rect> coverage_limit = std::nullopt,
126  const std::optional<SamplerDescriptor>& sampler_descriptor = std::nullopt,
127  bool msaa_enabled = true,
128  int32_t mip_count = 1,
129  const std::string& label = "Snapshot") const;
130 
131  //----------------------------------------------------------------------------
132  /// @brief Return the color source's intrinsic size, if available.
133  ///
134  /// For example, a gradient has a size based on its end and beginning
135  /// points, ignoring any tiling. Solid colors and runtime effects have
136  /// no size.
137  ///
138  std::optional<Size> GetColorSourceSize() const;
139 
140  void SetColorSourceSize(Size size);
141 
142  //----------------------------------------------------------------------------
143  /// @brief Inherit the provided opacity.
144  ///
145  /// Use of this method is invalid if CanAcceptOpacity returns false.
146  ///
147  virtual void SetInheritedOpacity(Scalar opacity);
148 
149  //----------------------------------------------------------------------------
150  /// @brief Returns a color if this Contents will flood the given `target_size`
151  /// with a color. This output color is the "Source" color that will be
152  /// used for the Entity's blend operation.
153  ///
154  /// This is useful for absorbing full screen solid color draws into
155  /// subpass clear colors.
156  ///
157  virtual std::optional<Color> AsBackgroundColor(const Entity& entity,
158  ISize target_size) const;
159 
160  //----------------------------------------------------------------------------
161  /// @brief Cast to a filter. Returns `nullptr` if this Contents is not a
162  /// filter.
163  ///
164  virtual const FilterContents* AsFilter() const;
165 
166  //----------------------------------------------------------------------------
167  /// @brief If possible, applies a color filter to this contents inputs on
168  /// the CPU.
169  ///
170  /// This method will either fully apply the color filter or
171  /// perform no action. Partial/incorrect application of the color
172  /// filter will never occur.
173  ///
174  /// @param[in] color_filter_proc A function that filters a given
175  /// unpremultiplied color to produce a new
176  /// unpremultiplied color.
177  ///
178  /// @return True if the color filter was able to be fully applied to all
179  /// all relevant inputs. Otherwise, this operation is a no-op and
180  /// false is returned.
181  ///
182  [[nodiscard]] virtual bool ApplyColorFilter(
183  const ColorFilterProc& color_filter_proc);
184 
185  private:
186  std::optional<Rect> coverage_hint_;
187  std::optional<Size> color_source_size_;
188 
189  Contents(const Contents&) = delete;
190 
191  Contents& operator=(const Contents&) = delete;
192 };
193 
194 } // namespace impeller
195 
196 #endif // FLUTTER_IMPELLER_ENTITY_CONTENTS_CONTENTS_H_
impeller::Contents::GetColorSourceSize
std::optional< Size > GetColorSourceSize() const
Return the color source's intrinsic size, if available.
Definition: contents.cc:158
impeller::OptionsFromPass
ContentContextOptions OptionsFromPass(const RenderPass &pass)
Definition: contents.cc:19
lazy_glyph_atlas.h
impeller::Scalar
float Scalar
Definition: scalar.h:18
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:150
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:136
impeller::Contents::ClipCoverage::Type
Type
Definition: contents.h:38
impeller::Color
Definition: color.h:123
impeller::Contents::SetColorSourceSize
void SetColorSourceSize(Size size)
Definition: contents.cc:162
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:63
impeller::Contents::Contents
Contents()
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::ClipCoverage::Type::kRestore
@ kRestore
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:56
impeller::Entity
Definition: entity.h:20
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:34
impeller::Contents::GetCoverageHint
const std::optional< Rect > & GetCoverageHint() const
Definition: contents.cc:154
impeller::TSize< Scalar >
impeller::Contents::ClipCoverage::coverage
std::optional< Rect > coverage
This coverage is the outer coverage of the clip.
Definition: contents.h:52
impeller::Contents::~Contents
virtual ~Contents()
transform
Matrix transform
Definition: gaussian_blur_filter_contents.cc:213
impeller::Contents::ColorFilterProc
std::function< Color(Color)> ColorFilterProc
Definition: contents.h:35
impeller::Contents::ClipCoverage::type
Type type
Definition: contents.h:40
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:33
sampler_descriptor.h
impeller::Contents::RenderProc
std::function< bool(const ContentContext &renderer, const Entity &entity, RenderPass &pass)> RenderProc
Definition: contents.h:57
impeller::Contents::Render
virtual bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const =0
snapshot.h
rect.h
impeller::Contents::SetInheritedOpacity
virtual void SetInheritedOpacity(Scalar opacity)
Inherit the provided opacity.
Definition: contents.cc:131
impeller::Contents::ClipCoverage
Definition: contents.h:37
impeller::Contents::ClipCoverage::Type::kAppend
@ kAppend
color.h
impeller::Contents::MakeAnonymous
static std::shared_ptr< Contents > MakeAnonymous(RenderProc render_proc, CoverageProc coverage_proc)
Definition: contents.cc:41
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:145
impeller::Contents::CoverageProc
std::function< std::optional< Rect >(const Entity &entity)> CoverageProc
Definition: contents.h:58
impeller::Contents
Definition: contents.h:31
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
Definition: allocation.cc:12
impeller::ContentContext
Definition: content_context.h:366
impeller::Contents::AsFilter
virtual const FilterContents * AsFilter() const
Cast to a filter. Returns nullptr if this Contents is not a filter.
Definition: contents.cc:141
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::Contents::ClipCoverage::Type::kNoChange
@ kNoChange
impeller::FilterContents
Definition: filter_contents.h:22
impeller::Contents::ClipCoverage::is_difference_or_non_square
bool is_difference_or_non_square
Definition: contents.h:43