| 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
11 #include "impeller/core/sampler_descriptor.h"
12 #include "impeller/geometry/color.h"
13 #include "impeller/geometry/rect.h"
14 #include "impeller/renderer/snapshot.h"
15 #include "impeller/typographer/lazy_glyph_atlas.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
40 Type type = Type::kNoChange;
41 std::optional<Rect> coverage = std::nullopt;
42 };
43
44 using RenderProc = std::function<bool(const ContentContext& renderer,
45 const Entity& entity,
46 RenderPass& pass)>;
47 using CoverageProc = std::function<std::optional<Rect>(const Entity& entity)>;
48
49 static std::shared_ptr<Contents> MakeAnonymous(RenderProc render_proc,
50 CoverageProc coverage_proc);
51
52 Contents();
53
54 virtual ~Contents();
55
56 /// @brief Add any text data to the specified lazy atlas. The scale parameter
57 /// must be used again later when drawing the text.
58 virtual void PopulateGlyphAtlas(
59 const std::shared_ptr<LazyGlyphAtlas>& lazy_glyph_atlas,
60 Scalar scale) {}
61
62 virtual bool Render(const ContentContext& renderer,
63 const Entity& entity,
64 RenderPass& pass) const = 0;
65
66 //----------------------------------------------------------------------------
67 /// @brief Get the area of the render pass that will be affected when this
68 /// contents is rendered.
69 ///
70 /// During rendering, coverage coordinates count pixels from the top
71 /// left corner of the framebuffer.
72 ///
73 /// @return The coverage rectangle. An `std::nullopt` result means that
74 /// rendering this contents has no effect on the output color.
75 ///
76 virtual std::optional<Rect> GetCoverage(const Entity& entity) const = 0;
77
78 //----------------------------------------------------------------------------
79 /// @brief Hint that specifies the coverage area of this Contents that will
80 /// actually be used during rendering. This is for optimization
81 /// purposes only and can not be relied on as a clip. May optionally
82 /// affect the result of `GetCoverage()`.
83 ///
84 void SetCoverageHint(std::optional<Rect> coverage_hint);
85
86 const std::optional<Rect>& GetCoverageHint() const;
87
88 //----------------------------------------------------------------------------
89 /// @brief Whether this Contents only emits opaque source colors from the
90 /// fragment stage. This value does not account for any entity
91 /// properties (e.g. the blend mode), clips/visibility culling, or
92 /// inherited opacity.
93 ///
94 virtual bool IsOpaque() const;
95
96 //----------------------------------------------------------------------------
97 /// @brief Given the current pass space bounding rectangle of the clip
98 /// buffer, return the expected clip coverage after this draw call.
99 /// This should only be implemented for contents that may write to the
100 /// clip buffer.
101 ///
102 /// During rendering, coverage coordinates count pixels from the top
103 /// left corner of the framebuffer.
104 ///
105 virtual ClipCoverage GetClipCoverage(
106 const Entity& entity,
107 const std::optional<Rect>& current_clip_coverage) const;
108
109 //----------------------------------------------------------------------------
110 /// @brief Render this contents to a snapshot, respecting the entity's
111 /// transform, path, clip depth, and blend mode.
112 /// The result texture size is always the size of
113 /// `GetCoverage(entity)`.
114 ///
115 virtual std::optional<Snapshot> RenderToSnapshot(
116 const ContentContext& renderer,
117 const Entity& entity,
118 std::optional<Rect> coverage_limit = std::nullopt,
119 const std::optional<SamplerDescriptor>& sampler_descriptor = std::nullopt,
120 bool msaa_enabled = true,
121 int32_t mip_count = 1,
122 const std::string& label = "Snapshot") const;
123
124 virtual bool ShouldRender(const Entity& entity,
125 const std::optional<Rect> clip_coverage) const;
126
127 //----------------------------------------------------------------------------
128 /// @brief Return the color source's intrinsic size, if available.
129 ///
130 /// For example, a gradient has a size based on its end and beginning
131 /// points, ignoring any tiling. Solid colors and runtime effects have
132 /// no size.
133 ///
134 std::optional<Size> GetColorSourceSize() const;
135
136 void SetColorSourceSize(Size size);
137
138 //----------------------------------------------------------------------------
139 /// @brief Whether or not this contents can accept the opacity peephole
140 /// optimization.
141 ///
142 /// By default all contents return false. Contents are responsible
143 /// for determining whether or not their own geometries intersect in
144 /// a way that makes accepting opacity impossible. It is always safe
145 /// to return false, especially if computing overlap would be
146 /// computationally expensive.
147 ///
148 virtual bool CanInheritOpacity(const Entity& entity) const;
149
150 //----------------------------------------------------------------------------
151 /// @brief Inherit the provided opacity.
152 ///
153 /// Use of this method is invalid if CanAcceptOpacity returns false.
154 ///
155 virtual void SetInheritedOpacity(Scalar opacity);
156
157 //----------------------------------------------------------------------------
158 /// @brief Returns a color if this Contents will flood the given `target_size`
159 /// with a color. This output color is the "Source" color that will be
160 /// used for the Entity's blend operation.
161 ///
162 /// This is useful for absorbing full screen solid color draws into
163 /// subpass clear colors.
164 ///
165 virtual std::optional<Color> AsBackgroundColor(const Entity& entity,
166 ISize target_size) const;
167
168 //----------------------------------------------------------------------------
169 /// @brief Cast to a filter. Returns `nullptr` if this Contents is not a
170 /// filter.
171 ///
172 virtual const FilterContents* AsFilter() const;
173
174 //----------------------------------------------------------------------------
175 /// @brief If possible, applies a color filter to this contents inputs on
176 /// the CPU.
177 ///
178 /// This method will either fully apply the color filter or
179 /// perform no action. Partial/incorrect application of the color
180 /// filter will never occur.
181 ///
182 /// @param[in] color_filter_proc A function that filters a given
183 /// unpremultiplied color to produce a new
184 /// unpremultiplied color.
185 ///
186 /// @return True if the color filter was able to be fully applied to all
187 /// all relevant inputs. Otherwise, this operation is a no-op and
188 /// false is returned.
189 ///
190 [[nodiscard]] virtual bool ApplyColorFilter(
191 const ColorFilterProc& color_filter_proc);
192
193 private:
194 std::optional<Rect> coverage_hint_;
195 std::optional<Size> color_source_size_;
196
197 Contents(const Contents&) = delete;
198
199 Contents& operator=(const Contents&) = delete;
200 };
201
202 } // namespace impeller
203
204 #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:177
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:169
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:140
impeller::Contents::ClipCoverage::Type
Type
Definition: contents.h:38
impeller::Color
Definition: color.h:124
impeller::Contents::SetColorSourceSize
void SetColorSourceSize(Size size)
Definition: contents.cc:181
impeller::Contents::CanInheritOpacity
virtual bool CanInheritOpacity(const Entity &entity) const
Whether or not this contents can accept the opacity peephole optimization.
Definition: contents.cc:131
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:173
impeller::TSize< Scalar >
impeller::Contents::ClipCoverage::coverage
std::optional< Rect > coverage
Definition: contents.h:41
impeller::Contents::~Contents
virtual ~Contents()
impeller::Contents::ColorFilterProc
std::function< Color(Color)> ColorFilterProc
Definition: contents.h:35
impeller::Contents::ClipCoverage::type
Type type
Definition: contents.h:40
impeller::Contents::ShouldRender
virtual bool ShouldRender(const Entity &entity, const std::optional< Rect > clip_coverage) const
Definition: contents.cc:154
impeller::Contents::PopulateGlyphAtlas
virtual void PopulateGlyphAtlas(const std::shared_ptr< LazyGlyphAtlas > &lazy_glyph_atlas, Scalar scale)
Add any text data to the specified lazy atlas. The scale parameter must be used again later when draw...
Definition: contents.h:58
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:46
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:135
impeller::Contents::ClipCoverage
Definition: contents.h:37
scale
const Scalar scale
Definition: stroke_path_geometry.cc:308
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:149
impeller::Contents::CoverageProc
std::function< std::optional< Rect >(const Entity &entity)> CoverageProc
Definition: contents.h:47
impeller::Contents
Definition: contents.h:31
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:52
impeller
Definition: aiks_blend_unittests.cc:18
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:145
impeller::Contents::ClipCoverage::Type::kNoChange
@ kNoChange
impeller::FilterContents
Definition: filter_contents.h:22