| Flutter Impeller |

Public Member Functions | Protected Types | Protected Member Functions | Static Protected Member Functions | List of all members
impeller::ColorSourceContents Class Reference

`#include <color_source_contents.h>`

Inheritance diagram for impeller::ColorSourceContents:

## Public Member Functions
| ColorSourceContents ()
| ~ColorSourceContents () override
void | SetGeometry (const Geometry *geometry)
| Set the geometry that this contents will use to render. More...
const Geometry * | GetGeometry () const
| Get the geometry that this contents will use to render. More...
void | SetEffectTransform (Matrix matrix)
| Set the effect transform for this color source. More...
const Matrix & | GetInverseEffectTransform () const
| Set the inverted effect transform for this color source. More...
void | SetOpacityFactor (Scalar opacity)
| Set the opacity factor for this color source. More...
Scalar | GetOpacityFactor () const
| Get the opacity factor for this color source. More...
virtual bool | IsSolidColor () const
std::optional< Rect > | GetCoverage (const Entity &entity) const override
| Get the area of the render pass that will be affected when this contents is rendered. More...
void | SetInheritedOpacity (Scalar opacity) override
| Inherit the provided opacity. More...
Public Member Functions inherited from impeller::Contents
| Contents ()
virtual | ~Contents ()
virtual bool | Render (const ContentContext &renderer, const Entity &entity, RenderPass &pass) const =0
void | SetCoverageHint (std::optional< Rect > coverage_hint)
| Hint that specifies the coverage area of this Contents that will actually be used during rendering. This is for optimization purposes only and can not be relied on as a clip. May optionally affect the result of `GetCoverage()`. More...
const std::optional< Rect > & | GetCoverageHint () const
virtual bool | IsOpaque (const Matrix &transform) const
| Whether this Contents only emits opaque source colors from the fragment stage. This value does not account for any entity properties (e.g. the blend mode), clips/visibility culling, or inherited opacity. More...
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 after this draw call. This should only be implemented for contents that may write to the clip buffer. More...
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, and blend mode. The result texture size is always the size of `GetCoverage(entity)`. More...
std::optional< Size > | GetColorSourceSize () const
| Return the color source's intrinsic size, if available. More...
void | SetColorSourceSize (Size size)
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 the "Source" color that will be used for the Entity's blend operation. More...
virtual const FilterContents * | AsFilter () const
| Cast to a filter. Returns `nullptr` if this Contents is not a filter. More...
virtual bool | ApplyColorFilter (const ColorFilterProc &color_filter_proc)
| If possible, applies a color filter to this contents inputs on the CPU. More...

## Protected Types
using | BindFragmentCallback = std::function< bool(RenderPass &pass)>
using | PipelineBuilderMethod = std::shared_ptr< Pipeline< PipelineDescriptor > >(impeller::ContentContext::*)(ContentContextOptions) const
using | PipelineBuilderCallback = std::function< std::shared_ptr< Pipeline< PipelineDescriptor > >(ContentContextOptions)>
using | CreateGeometryCallback = std::function< GeometryResult(const ContentContext &renderer, const Entity &entity, RenderPass &pass, const Geometry *geom)>

## Protected Member Functions
bool | AppliesAlphaForStrokeCoverage (const Matrix &transform) const
| Whether the entity should be treated as non-opaque due to stroke geometry requiring alpha for coverage. More...
template<typename VertexShaderT >
bool | DrawGeometry (const ContentContext &renderer, const Entity &entity, RenderPass &pass, const PipelineBuilderCallback &pipeline_callback, typename VertexShaderT::FrameInfo frame_info, const BindFragmentCallback &bind_fragment_callback, bool force_stencil=false, const CreateGeometryCallback &create_geom_callback=DefaultCreateGeometryCallback) const

## Static Protected Member Functions
static GeometryResult | DefaultCreateGeometryCallback (const ContentContext &renderer, const Entity &entity, RenderPass &pass, const Geometry *geom)

## Additional Inherited Members
Public Types inherited from impeller::Contents
using | ColorFilterProc = std::function< Color(Color)>
using | RenderProc = std::function< bool(const ContentContext &renderer, const Entity &entity, RenderPass &pass)>
using | CoverageProc = std::function< std::optional< Rect >(const Entity &entity)>
Static Public Member Functions inherited from impeller::Contents
static std::shared_ptr< Contents > | MakeAnonymous (RenderProc render_proc, CoverageProc coverage_proc)

## Detailed Description

Color sources are geometry-ignostic `Contents` capable of shading any area defined by an `impeller::Geometry`. Conceptually, `impeller::ColorSourceContents` implement a particular color shading behavior.

This separation of concerns between geometry and color source output allows Impeller to handle most possible draw combinations in a consistent way. For example: There are color sources for handling solid colors, gradients, textures, custom runtime effects, and even 3D scenes.

There are some special rendering exceptions that deviate from this pattern and cross geometry and color source concerns, such as text atlas and image atlas rendering. Special `Contents` exist for rendering these behaviors which don't implement `ColorSourceContents`.

See also
  `impeller::Geometry`

Definition at line 37 of file color_source_contents.h.

## Member Typedef Documentation

## ◆ BindFragmentCallback

using impeller::ColorSourceContents::BindFragmentCallback = std::function<bool(RenderPass& pass)>
| protected

Definition at line 103 of file color_source_contents.h.

## ◆ CreateGeometryCallback

using impeller::ColorSourceContents::CreateGeometryCallback = std::function<GeometryResult(const ContentContext& renderer, const Entity& entity, RenderPass& pass, const Geometry* geom)>
| protected

Definition at line 113 of file color_source_contents.h.

## ◆ PipelineBuilderCallback

using impeller::ColorSourceContents::PipelineBuilderCallback = std::function<std::shared_ptr<Pipeline<PipelineDescriptor> >( ContentContextOptions)>
| protected

Definition at line 108 of file color_source_contents.h.

## ◆ PipelineBuilderMethod

using impeller::ColorSourceContents::PipelineBuilderMethod = std::shared_ptr<Pipeline<PipelineDescriptor> > ( impeller::ContentContext::*)(ContentContextOptions) const
| protected

Definition at line 105 of file color_source_contents.h.

## Constructor & Destructor Documentation

## ◆ ColorSourceContents()

impeller::ColorSourceContents::ColorSourceContents | ( |  | ) |
| default

## ◆ ~ColorSourceContents()

impeller::ColorSourceContents::~ColorSourceContents | ( |  | ) |
| overridedefault

## Member Function Documentation

## ◆ AppliesAlphaForStrokeCoverage()

bool impeller::ColorSourceContents::AppliesAlphaForStrokeCoverage | ( | const Matrix & | transform | ) | const
| protected

Whether the entity should be treated as non-opaque due to stroke geometry requiring alpha for coverage.

Definition at line 53 of file color_source_contents.cc.

54 {
55 return GetGeometry() && GetGeometry()->ComputeAlphaCoverage(transform) < 1.0;
56 }

References impeller::Geometry::ComputeAlphaCoverage(), GetGeometry(), and transform.

Referenced by impeller::LinearGradientContents::IsOpaque(), impeller::RadialGradientContents::IsOpaque(), impeller::SweepGradientContents::IsOpaque(), impeller::SolidColorContents::IsOpaque(), and impeller::TiledTextureContents::IsOpaque().

## ◆ DefaultCreateGeometryCallback()

static GeometryResult impeller::ColorSourceContents::DefaultCreateGeometryCallback | ( | const ContentContext & | renderer,
|  | const Entity & | entity,
|  | RenderPass & | pass,
|  | const Geometry * | geom
| ) |  |
| inlinestaticprotected

Definition at line 115 of file color_source_contents.h.

119 {
120 return geom->GetPositionBuffer(renderer, entity, pass);
121 }

References impeller::Geometry::GetPositionBuffer().

## ◆ DrawGeometry()

template<typename VertexShaderT >

bool impeller::ColorSourceContents::DrawGeometry | ( | const ContentContext & | renderer,
|  | const Entity & | entity,
|  | RenderPass & | pass,
|  | const PipelineBuilderCallback & | pipeline_callback,
|  | typename VertexShaderT::FrameInfo | frame_info,
|  | const BindFragmentCallback & | bind_fragment_callback,
|  | bool | force_stencil = `false`,
|  | const CreateGeometryCallback & | create_geom_callback = `DefaultCreateGeometryCallback`
| ) |  | const
| inlineprotected

Stencil preparation draw.

Cover draw.

Definition at line 128 of file color_source_contents.h.

136 {
137 auto options = OptionsFromPassAndEntity(pass, entity);
138
139 GeometryResult::Mode geometry_mode = GetGeometry()->GetResultMode();
140 bool do_cover_draw = false;
141 Rect cover_area = {};
142
143 bool is_stencil_then_cover =
144 geometry_mode == GeometryResult::Mode::kNonZero ||
145 geometry_mode == GeometryResult::Mode::kEvenOdd;
146 if (!is_stencil_then_cover && force_stencil) {
147 geometry_mode = GeometryResult::Mode::kNonZero;
148 is_stencil_then_cover = true;
149 }
150
151 if (is_stencil_then_cover) {
152 pass.SetStencilReference(0);
153
154 /// Stencil preparation draw.
155
156 GeometryResult stencil_geometry_result =
157 GetGeometry()->GetPositionBuffer(renderer, entity, pass);
158 if (stencil_geometry_result.vertex_buffer.vertex_count == 0u) {
159 return true;
160 }
161 pass.SetVertexBuffer(std::move(stencil_geometry_result.vertex_buffer));
162 options.primitive_type = stencil_geometry_result.type;
163
164 options.blend_mode = BlendMode::kDestination;
165 switch (stencil_geometry_result.mode) {
166 case GeometryResult::Mode::kNonZero:
167 pass.SetCommandLabel("Stencil preparation (NonZero)");
168 options.stencil_mode =
169 ContentContextOptions::StencilMode::kStencilNonZeroFill;
170 break;
171 case GeometryResult::Mode::kEvenOdd:
172 pass.SetCommandLabel("Stencil preparation (EvenOdd)");
173 options.stencil_mode =
174 ContentContextOptions::StencilMode::kStencilEvenOddFill;
175 break;
176 default:
177 if (force_stencil) {
178 pass.SetCommandLabel("Stencil preparation (NonZero)");
179 options.stencil_mode =
180 ContentContextOptions::StencilMode::kStencilNonZeroFill;
181 break;
182 }
183 FML_UNREACHABLE();
184 }
185 pass.SetPipeline(renderer.GetClipPipeline(options));
186 ClipPipeline::VertexShader::FrameInfo clip_frame_info;
187 clip_frame_info.depth = entity.GetShaderClipDepth();
188 clip_frame_info.mvp = stencil_geometry_result.transform;
189 ClipPipeline::VertexShader::BindFrameInfo(
190 pass, renderer.GetTransientsBuffer().EmplaceUniform(clip_frame_info));
191
192 if (!pass.Draw().ok()) {
193 return false;
194 }
195
196 /// Cover draw.
197
198 options.blend_mode = entity.GetBlendMode();
199 options.stencil_mode = ContentContextOptions::StencilMode::kCoverCompare;
200 std::optional<Rect> maybe_cover_area = GetGeometry()->GetCoverage({});
201 if (!maybe_cover_area.has_value()) {
202 return true;
203 }
204 do_cover_draw = true;
205 cover_area = maybe_cover_area.value();
206 }
207
208 GeometryResult geometry_result;
209 if (do_cover_draw) {
210 RectGeometry geom(cover_area);
211 geometry_result = create_geom_callback(renderer, entity, pass, &geom);
212 } else {
213 geometry_result =
214 create_geom_callback(renderer, entity, pass, GetGeometry());
215 }
216
217 if (geometry_result.vertex_buffer.vertex_count == 0u) {
218 return true;
219 }
220 pass.SetVertexBuffer(std::move(geometry_result.vertex_buffer));
221 options.primitive_type = geometry_result.type;
222
223 // Enable depth writing for all opaque entities in order to allow
224 // reordering. Opaque entities are coerced to source blending by
225 // `EntityPass::AddEntity`.
226 options.depth_write_enabled = options.blend_mode == BlendMode::kSource;
227
228 // Take the pre-populated vertex shader uniform struct and set managed
229 // values.
230 frame_info.mvp = geometry_result.transform;
231
232 // If overdraw prevention is enabled (like when drawing stroke paths), we
233 // increment the stencil buffer as we draw, preventing overlapping fragments
234 // from drawing. Afterwards, we need to append another draw call to clean up
235 // the stencil buffer (happens below in this method).
236 if (geometry_result.mode == GeometryResult::Mode::kPreventOverdraw) {
237 options.stencil_mode =
238 ContentContextOptions::StencilMode::kOverdrawPreventionIncrement;
239 }
240 pass.SetStencilReference(0);
241
242 VertexShaderT::BindFrameInfo(
243 pass, renderer.GetTransientsBuffer().EmplaceUniform(frame_info));
244
245 // The reason we need to have a callback mechanism here is that this routine
246 // may insert draw calls before the main draw call below. For example, for
247 // sufficiently complex paths we may opt to use stencil-then-cover to avoid
248 // tessellation.
249 if (!bind_fragment_callback(pass)) {
250 return false;
251 }
252
253 pass.SetPipeline(pipeline_callback(options));
254
255 if (!pass.Draw().ok()) {
256 return false;
257 }
258
259 // If we performed overdraw prevention, a subsection of the clip heightmap
260 // was incremented by 1 in order to self-clip. So simply append a clip
261 // restore to clean it up.
262 if (geometry_result.mode == GeometryResult::Mode::kPreventOverdraw) {
263 auto restore = ClipRestoreContents();
264 restore.SetRestoreCoverage(GetCoverage(entity));
265 Entity restore_entity = entity.Clone();
266 return restore.Render(renderer, restore_entity, pass);
267 }
268 return true;
269 }

References impeller::Entity::Clone(), impeller::RenderPass::Draw(), impeller::HostBuffer::EmplaceUniform(), impeller::Entity::GetBlendMode(), impeller::ContentContext::GetClipPipeline(), GetCoverage(), impeller::Geometry::GetCoverage(), GetGeometry(), impeller::Geometry::GetPositionBuffer(), impeller::Geometry::GetResultMode(), impeller::Entity::GetShaderClipDepth(), impeller::ContentContext::GetTransientsBuffer(), impeller::ContentContextOptions::kCoverCompare, impeller::kDestination, impeller::GeometryResult::kEvenOdd, impeller::GeometryResult::kNonZero, impeller::ContentContextOptions::kOverdrawPreventionIncrement, impeller::GeometryResult::kPreventOverdraw, impeller::kSource, impeller::ContentContextOptions::kStencilEvenOddFill, impeller::ContentContextOptions::kStencilNonZeroFill, impeller::GeometryResult::mode, impeller::OptionsFromPassAndEntity(), impeller::Entity::Render(), impeller::RenderPass::SetCommandLabel(), impeller::RenderPass::SetPipeline(), impeller::RenderPass::SetStencilReference(), impeller::RenderPass::SetVertexBuffer(), impeller::GeometryResult::transform, impeller::GeometryResult::type, impeller::GeometryResult::vertex_buffer, and impeller::VertexBuffer::vertex_count.

## ◆ GetCoverage()

std::optional< Rect > impeller::ColorSourceContents::GetCoverage | ( | const Entity & | entity | ) | const
| overridevirtual

Get the area of the render pass that will be affected when this contents is rendered.

During rendering, coverage coordinates count pixels from the top left corner of the framebuffer.

Returns
  The coverage rectangle. An `std::nullopt` result means that rendering this contents has no effect on the output color.

Implements impeller::Contents.

Reimplemented in impeller::SolidColorContents.

Definition at line 44 of file color_source_contents.cc.

45 {
46 return geometry_->GetCoverage(entity.GetTransform());
47 };

References impeller::Geometry::GetCoverage(), and impeller::Entity::GetTransform().

Referenced by DrawGeometry(), and impeller::TiledTextureContents::RenderToSnapshot().

## ◆ GetGeometry()

const Geometry * impeller::ColorSourceContents::GetGeometry | ( |  | ) | const

Get the geometry that this contents will use to render.

Definition at line 20 of file color_source_contents.cc.

20 {
21 return geometry_;
22 }

Referenced by AppliesAlphaForStrokeCoverage(), impeller::SolidColorContents::AsBackgroundColor(), DrawGeometry(), impeller::SolidColorContents::GetCoverage(), impeller::TiledTextureContents::Render(), impeller::SolidColorContents::Render(), and impeller::TiledTextureContents::RenderToSnapshot().

## ◆ GetInverseEffectTransform()

const Matrix & impeller::ColorSourceContents::GetInverseEffectTransform | ( |  | ) | const

Set the inverted effect transform for this color source.

         When the effect transform is set via `SetEffectTransform`, the
         value is inverted upon storage. The reason for this is that most
         color sources internally use the inverted transform.

Returns
  The inverse of the transform set by `SetEffectTransform`.

See also
  `SetEffectTransform`

Definition at line 36 of file color_source_contents.cc.

36 {
37 return inverse_matrix_;
38 }

Referenced by impeller::TiledTextureContents::Render(), and impeller::TiledTextureContents::RenderToSnapshot().

## ◆ GetOpacityFactor()

Scalar impeller::ColorSourceContents::GetOpacityFactor | ( |  | ) | const

Get the opacity factor for this color source.

        This value is is factored into the output of the color source in
        addition to opacity information that may be supplied any other
        inputs.

Note
  If set, the output of this method factors factors in the inherited opacity of this `Contents`.

Definition at line 28 of file color_source_contents.cc.

28 {
29 return opacity_ * inherited_opacity_;
30 }

Referenced by impeller::SolidColorContents::GetColor(), impeller::RadialGradientContents::IsOpaque(), impeller::LinearGradientContents::IsOpaque(), impeller::SweepGradientContents::IsOpaque(), impeller::TiledTextureContents::IsOpaque(), impeller::TiledTextureContents::Render(), and impeller::TiledTextureContents::RenderToSnapshot().

## ◆ IsSolidColor()

bool impeller::ColorSourceContents::IsSolidColor | ( |  | ) | const
| virtual

Reimplemented in impeller::SolidColorContents.

Definition at line 40 of file color_source_contents.cc.

40 {
41 return false;
42 }

## ◆ SetEffectTransform()

void impeller::ColorSourceContents::SetEffectTransform | ( | Matrix | matrix | ) |

Set the effect transform for this color source.

        The effect transform is a transform matrix that is applied to
        the shaded color output and does not impact geometry in any way.

        For example: With repeat tiling, any gradient or
        `TiledTextureContents` could be used with an effect transform to
        inexpensively draw an infinite scrolling background pattern.

Definition at line 32 of file color_source_contents.cc.

32 {
33 inverse_matrix_ = matrix.Invert();
34 }

References impeller::Matrix::Invert().

## ◆ SetGeometry()

void impeller::ColorSourceContents::SetGeometry | ( | const Geometry * | geometry | ) |

Set the geometry that this contents will use to render.

Definition at line 16 of file color_source_contents.cc.

16 {
17 geometry_ = geometry;
18 }

Referenced by impeller::testing::TEST_P().

## ◆ SetInheritedOpacity()

void impeller::ColorSourceContents::SetInheritedOpacity | ( | Scalar | opacity | ) |
| overridevirtual

Inherit the provided opacity.

       Use of this method is invalid if CanAcceptOpacity returns false.

Reimplemented from impeller::Contents.

Definition at line 49 of file color_source_contents.cc.

49 {
50 inherited_opacity_ = opacity;
51 }

## ◆ SetOpacityFactor()

void impeller::ColorSourceContents::SetOpacityFactor | ( | Scalar | opacity | ) |

Set the opacity factor for this color source.

Definition at line 24 of file color_source_contents.cc.

24 {
25 opacity_ = alpha;
26 }

The documentation for this class was generated from the following files:

- impeller/entity/contents/color_source_contents.h
- impeller/entity/contents/color_source_contents.cc

impeller::Geometry::ComputeAlphaCoverage
virtual Scalar ComputeAlphaCoverage(const Matrix &transform) const
Definition: geometry.h:124
impeller::ContentContextOptions::StencilMode::kOverdrawPreventionIncrement
@ kOverdrawPreventionIncrement
impeller::Geometry::GetCoverage
virtual std::optional< Rect > GetCoverage(const Matrix &transform) const =0
impeller::BlendMode::kSource
@ kSource
impeller::BlendMode::kDestination
@ kDestination
impeller::Geometry::GetResultMode
virtual GeometryResult::Mode GetResultMode() const
Definition: geometry.cc:56
impeller::ContentContextOptions::StencilMode::kStencilEvenOddFill
@ kStencilEvenOddFill
impeller::GeometryResult::Mode::kEvenOdd
@ kEvenOdd
impeller::GeometryResult::Mode
Mode
Definition: geometry.h:27
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:34
impeller::GeometryResult::type
PrimitiveType type
Definition: geometry.h:42
impeller::Geometry::GetPositionBuffer
virtual GeometryResult GetPositionBuffer(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const =0
transform
Matrix transform
Definition: gaussian_blur_filter_contents.cc:213
impeller::ContentContextOptions::StencilMode::kStencilNonZeroFill
@ kStencilNonZeroFill
impeller::Rect
TRect< Scalar > Rect
Definition: rect.h:776
impeller::Matrix::Invert
Matrix Invert() const
Definition: matrix.cc:97
impeller::ColorSourceContents::GetGeometry
const Geometry * GetGeometry() const
Get the geometry that this contents will use to render.
Definition: color_source_contents.cc:20
impeller::ColorSourceContents::GetCoverage
std::optional< Rect > GetCoverage(const Entity &entity) const override
Get the area of the render pass that will be affected when this contents is rendered.
Definition: color_source_contents.cc:44
impeller::GeometryResult::Mode::kPreventOverdraw
@ kPreventOverdraw
impeller::ContentContextOptions::StencilMode::kCoverCompare
@ kCoverCompare
impeller::GeometryResult::Mode::kNonZero
@ kNonZero