Flutter Impeller
impeller::ColorSourceContents Class Referenceabstract

#include <color_source_contents.h>

Inheritance diagram for impeller::ColorSourceContents:
impeller::Contents impeller::CircleContents impeller::ConicalGradientContents impeller::FramebufferBlendContents impeller::LinearGradientContents impeller::RadialGradientContents impeller::RuntimeEffectContents impeller::SolidColorContents impeller::SweepGradientContents impeller::TiledTextureContents impeller::UberSDFContents

Public Types

using BindFragmentCallback = std::function< bool(RenderPass &pass)>
 
using PipelineBuilderCallback = std::function< PipelineRef(ContentContextOptions)>
 
using CreateGeometryCallback = std::function< GeometryResult(const ContentContext &renderer, const Entity &entity, RenderPass &pass, const Geometry *geom)>
 
- 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)>
 

Public Member Functions

 ColorSourceContents ()
 
 ~ColorSourceContents () override
 
virtual const GeometryGetGeometry () const =0
 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 MatrixGetInverseEffectTransform () 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< RectGetCoverage (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 std::optional< SnapshotRenderToSnapshot (const ContentContext &renderer, const Entity &entity, const SnapshotOptions &options) 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< SizeGetColorSourceSize () const
 Return the color source's intrinsic size, if available. More...
 
void SetColorSourceSize (Size size)
 
virtual std::optional< ColorAsBackgroundColor (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 bool ApplyColorFilter (const ColorFilterProc &color_filter_proc)
 If possible, applies a color filter to this contents inputs on the CPU. More...
 

Static Public Member Functions

template<typename VertexShaderT >
static bool DrawGeometry (const Contents *contents, const Geometry *geometry, 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)
 
- Static Public Member Functions inherited from impeller::Contents
static std::shared_ptr< ContentsMakeAnonymous (RenderProc render_proc, CoverageProc coverage_proc)
 

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)
 

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 38 of file color_source_contents.h.

Member Typedef Documentation

◆ BindFragmentCallback

Definition at line 40 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)>

Definition at line 43 of file color_source_contents.h.

◆ PipelineBuilderCallback

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 46 of file color_source_contents.cc.

47  {
49 }
virtual const Geometry * GetGeometry() const =0
Get the geometry that this contents will use to render.
virtual Scalar ComputeAlphaCoverage(const Matrix &transform) const
Definition: geometry.h:125

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

Referenced by impeller::LinearGradientContents::IsOpaque(), impeller::RadialGradientContents::IsOpaque(), impeller::SolidColorContents::IsOpaque(), impeller::SweepGradientContents::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 240 of file color_source_contents.h.

244  {
245  return geom->GetPositionBuffer(renderer, entity, pass);
246  }

References impeller::Geometry::GetPositionBuffer().

◆ DrawGeometry() [1/2]

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

Definition at line 253 of file color_source_contents.h.

261  {
262  //
263  return DrawGeometry<VertexShaderT>(this, //
264  GetGeometry(), //
265  renderer, //
266  entity, //
267  pass, //
268  pipeline_callback, //
269  frame_info, //
270  bind_fragment_callback, //
271  force_stencil, //
272  create_geom_callback);
273  }

References GetGeometry().

◆ DrawGeometry() [2/2]

template<typename VertexShaderT >
static bool impeller::ColorSourceContents::DrawGeometry ( const Contents contents,
const Geometry geometry,
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 
)
inlinestatic

Stencil preparation draw.

Cover draw.

Definition at line 108 of file color_source_contents.h.

118  {
119  auto options = OptionsFromPassAndEntity(pass, entity);
120 
121  GeometryResult::Mode geometry_mode = geometry->GetResultMode();
122  bool do_cover_draw = false;
123  Rect cover_area = {};
124 
125  bool is_stencil_then_cover =
126  geometry_mode == GeometryResult::Mode::kNonZero ||
127  geometry_mode == GeometryResult::Mode::kEvenOdd;
128  if (!is_stencil_then_cover && force_stencil) {
129  geometry_mode = GeometryResult::Mode::kNonZero;
130  is_stencil_then_cover = true;
131  }
132 
133  if (is_stencil_then_cover) {
134  pass.SetStencilReference(0);
135 
136  /// Stencil preparation draw.
137 
138  GeometryResult stencil_geometry_result =
139  geometry->GetPositionBuffer(renderer, entity, pass);
140  if (stencil_geometry_result.vertex_buffer.vertex_count == 0u) {
141  return true;
142  }
143  pass.SetVertexBuffer(std::move(stencil_geometry_result.vertex_buffer));
144  options.primitive_type = stencil_geometry_result.type;
145 
146  options.blend_mode = BlendMode::kDst;
147  switch (stencil_geometry_result.mode) {
149  pass.SetCommandLabel("Stencil preparation (NonZero)");
150  options.stencil_mode =
152  break;
154  pass.SetCommandLabel("Stencil preparation (EvenOdd)");
155  options.stencil_mode =
157  break;
158  default:
159  if (force_stencil) {
160  pass.SetCommandLabel("Stencil preparation (NonZero)");
161  options.stencil_mode =
163  break;
164  }
165  FML_UNREACHABLE();
166  }
167  pass.SetPipeline(renderer.GetClipPipeline(options));
168  ClipPipeline::VertexShader::FrameInfo clip_frame_info;
169  clip_frame_info.depth = entity.GetShaderClipDepth();
170  clip_frame_info.mvp = stencil_geometry_result.transform;
171  ClipPipeline::VertexShader::BindFrameInfo(
172  pass,
173  renderer.GetTransientsDataBuffer().EmplaceUniform(clip_frame_info));
174 
175  if (!pass.Draw().ok()) {
176  return false;
177  }
178 
179  /// Cover draw.
180 
181  options.blend_mode = entity.GetBlendMode();
183  std::optional<Rect> maybe_cover_area = geometry->GetCoverage({});
184  if (!maybe_cover_area.has_value()) {
185  return true;
186  }
187  do_cover_draw = true;
188  cover_area = maybe_cover_area.value();
189  }
190 
191  GeometryResult geometry_result;
192  if (do_cover_draw) {
193  FillRectGeometry geom(cover_area);
194  geometry_result = create_geom_callback(renderer, entity, pass, &geom);
195  } else {
196  geometry_result = create_geom_callback(renderer, entity, pass, geometry);
197  }
198 
199  if (geometry_result.vertex_buffer.vertex_count == 0u) {
200  return true;
201  }
202  pass.SetVertexBuffer(std::move(geometry_result.vertex_buffer));
203  options.primitive_type = geometry_result.type;
204 
205  // Enable depth writing for all opaque entities in order to allow
206  // reordering. Opaque entities are coerced to source blending by
207  // `EntityPass::AddEntity`.
208  options.depth_write_enabled = options.blend_mode == BlendMode::kSrc;
209 
210  // Take the pre-populated vertex shader uniform struct and set managed
211  // values.
212  frame_info.mvp = geometry_result.transform;
213 
214  // If overdraw prevention is requested (like when drawing stroke paths), set
215  // up the depth buffer and depth comparison function to prevent the same
216  // pixel from being painted multiple times.
217  if (geometry_result.mode == GeometryResult::Mode::kPreventOverdraw) {
218  options.depth_write_enabled = true;
219  options.depth_compare = CompareFunction::kGreater;
220  }
221  pass.SetStencilReference(0);
222 
223  VertexShaderT::BindFrameInfo(
224  pass, renderer.GetTransientsDataBuffer().EmplaceUniform(frame_info));
225 
226  // The reason we need to have a callback mechanism here is that this routine
227  // may insert draw calls before the main draw call below. For example, for
228  // sufficiently complex paths we may opt to use stencil-then-cover to avoid
229  // tessellation.
230  if (!bind_fragment_callback(pass)) {
231  return false;
232  }
233 
234  pass.SetPipeline(pipeline_callback(options));
235 
236  return pass.Draw().ok();
237  }
TRect< Scalar > Rect
Definition: rect.h:788
@ kGreater
Comparison test passes if new_value > current_value.
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:34

References impeller::RenderPass::Draw(), impeller::HostBuffer::EmplaceUniform(), impeller::Entity::GetBlendMode(), impeller::ContentContext::GetClipPipeline(), impeller::Geometry::GetCoverage(), impeller::Geometry::GetPositionBuffer(), impeller::Geometry::GetResultMode(), impeller::Entity::GetShaderClipDepth(), impeller::ContentContext::GetTransientsDataBuffer(), impeller::ContentContextOptions::kCoverCompare, impeller::kDst, impeller::GeometryResult::kEvenOdd, impeller::kGreater, impeller::GeometryResult::kNonZero, impeller::GeometryResult::kPreventOverdraw, impeller::kSrc, impeller::ContentContextOptions::kStencilEvenOddFill, impeller::ContentContextOptions::kStencilNonZeroFill, impeller::GeometryResult::mode, impeller::OptionsFromPassAndEntity(), 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::UberSDFContents, and impeller::SolidColorContents.

Definition at line 36 of file color_source_contents.cc.

37  {
38  const Geometry* geometry = GetGeometry();
39  return geometry ? geometry->GetCoverage(entity.GetTransform()) : std::nullopt;
40 }

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

Referenced by impeller::TiledTextureContents::RenderToSnapshot().

◆ GetGeometry()

◆ 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 28 of file color_source_contents.cc.

28  {
29  return inverse_matrix_;
30 }

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 20 of file color_source_contents.cc.

20  {
21  return opacity_ * inherited_opacity_;
22 }

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

◆ IsSolidColor()

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

Reimplemented in impeller::SolidColorContents.

Definition at line 32 of file color_source_contents.cc.

32  {
33  return false;
34 }

◆ 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 24 of file color_source_contents.cc.

24  {
25  inverse_matrix_ = matrix.Invert();
26 }
Matrix Invert() const
Definition: matrix.cc:99

References impeller::Matrix::Invert().

◆ 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 42 of file color_source_contents.cc.

42  {
43  inherited_opacity_ = opacity;
44 }

◆ SetOpacityFactor()

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

Set the opacity factor for this color source.

Definition at line 16 of file color_source_contents.cc.

16  {
17  opacity_ = alpha;
18 }

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