Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Public Member Functions | Protected Types | Protected Member Functions | List of all members
impeller::ColorSourceContents Class Reference

#include <color_source_contents.h>

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

Public Member Functions

 ColorSourceContents ()
 
 ~ColorSourceContents () override
 
void SetGeometry (std::shared_ptr< Geometry > geometry)
 Set the geometry that this contents will use to render.
 
const std::shared_ptr< Geometry > & GetGeometry () const
 Get the geometry that this contents will use to render.
 
void SetEffectTransform (Matrix matrix)
 Set the effect transform for this color source.
 
const MatrixGetInverseEffectTransform () const
 Set the inverted effect transform for this color source.
 
void SetOpacityFactor (Scalar opacity)
 Set the opacity factor for this color source.
 
Scalar GetOpacityFactor () const
 Get the opacity factor for this color source.
 
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.
 
bool CanInheritOpacity (const Entity &entity) const override
 Whether or not this contents can accept the opacity peephole optimization.
 
void SetInheritedOpacity (Scalar opacity) override
 Inherit the provided opacity.
 
- Public Member Functions inherited from impeller::Contents
 Contents ()
 
virtual ~Contents ()
 
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 drawing the text.
 
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().
 
const std::optional< Rect > & GetCoverageHint () const
 
virtual bool IsOpaque () 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.
 
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.
 
virtual std::optional< SnapshotRenderToSnapshot (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).
 
virtual bool ShouldRender (const Entity &entity, const std::optional< Rect > clip_coverage) const
 
std::optional< SizeGetColorSourceSize () const
 Return the color source's intrinsic size, if available.
 
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.
 
virtual const FilterContentsAsFilter () const
 Cast to a filter. Returns nullptr if this Contents is not a filter.
 
virtual bool ApplyColorFilter (const ColorFilterProc &color_filter_proc)
 If possible, applies a color filter to this contents inputs on the CPU.
 

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)>
 

Protected Member Functions

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) const
 

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< ContentsMakeAnonymous (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 36 of file color_source_contents.h.

Member Typedef Documentation

◆ BindFragmentCallback

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

Definition at line 108 of file color_source_contents.h.

◆ PipelineBuilderCallback

Definition at line 111 of file color_source_contents.h.

◆ PipelineBuilderMethod

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

Definition at line 109 of file color_source_contents.h.

Constructor & Destructor Documentation

◆ ColorSourceContents()

impeller::ColorSourceContents::ColorSourceContents ( )
default

◆ ~ColorSourceContents()

impeller::ColorSourceContents::~ColorSourceContents ( )
overridedefault

Member Function Documentation

◆ CanInheritOpacity()

bool impeller::ColorSourceContents::CanInheritOpacity ( const Entity entity) const
overridevirtual

Whether or not this contents can accept the opacity peephole optimization.

By default all contents return false. Contents are responsible for determining whether or not their own geometries intersect in a way that makes accepting opacity impossible. It is always safe to return false, especially if computing overlap would be computationally expensive.

Reimplemented from impeller::Contents.

Reimplemented in impeller::RuntimeEffectContents.

Definition at line 49 of file color_source_contents.cc.

49 {
50 return true;
51}

◆ 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 
) const
inlineprotected

Stencil preparation draw.

Cover draw.

Definition at line 116 of file color_source_contents.h.

121 {
122 auto options = OptionsFromPassAndEntity(pass, entity);
123
124 GeometryResult::Mode geometry_mode = GetGeometry()->GetResultMode();
125 Geometry& geometry = *GetGeometry();
126
127 const bool is_stencil_then_cover =
128 geometry_mode == GeometryResult::Mode::kNonZero ||
129 geometry_mode == GeometryResult::Mode::kEvenOdd;
130 if (is_stencil_then_cover) {
131 pass.SetStencilReference(0);
132
133 /// Stencil preparation draw.
134
135 GeometryResult stencil_geometry_result =
136 GetGeometry()->GetPositionBuffer(renderer, entity, pass);
137 if (stencil_geometry_result.vertex_buffer.vertex_count == 0u) {
138 return true;
139 }
140 pass.SetVertexBuffer(std::move(stencil_geometry_result.vertex_buffer));
141 options.primitive_type = stencil_geometry_result.type;
142
143 options.blend_mode = BlendMode::kDestination;
144 switch (stencil_geometry_result.mode) {
146 pass.SetCommandLabel("Stencil preparation (NonZero)");
147 options.stencil_mode =
149 break;
151 pass.SetCommandLabel("Stencil preparation (EvenOdd)");
152 options.stencil_mode =
154 break;
155 default:
157 }
158 pass.SetPipeline(renderer.GetClipPipeline(options));
159 ClipPipeline::VertexShader::FrameInfo clip_frame_info;
160 clip_frame_info.depth = entity.GetShaderClipDepth();
161 clip_frame_info.mvp = stencil_geometry_result.transform;
162 ClipPipeline::VertexShader::BindFrameInfo(
163 pass, renderer.GetTransientsBuffer().EmplaceUniform(clip_frame_info));
164
165 if (!pass.Draw().ok()) {
166 return false;
167 }
168
169 /// Cover draw.
170
171 options.blend_mode = entity.GetBlendMode();
173 std::optional<Rect> maybe_cover_area = GetGeometry()->GetCoverage({});
174 if (!maybe_cover_area.has_value()) {
175 return true;
176 }
177 geometry = RectGeometry(maybe_cover_area.value());
178 }
179
180 GeometryResult geometry_result =
181 geometry.GetPositionBuffer(renderer, entity, pass);
182 if (geometry_result.vertex_buffer.vertex_count == 0u) {
183 return true;
184 }
185 pass.SetVertexBuffer(std::move(geometry_result.vertex_buffer));
186 options.primitive_type = geometry_result.type;
187
188 // Take the pre-populated vertex shader uniform struct and set managed
189 // values.
190 frame_info.mvp = geometry_result.transform;
191
192 // If overdraw prevention is enabled (like when drawing stroke paths), we
193 // increment the stencil buffer as we draw, preventing overlapping fragments
194 // from drawing. Afterwards, we need to append another draw call to clean up
195 // the stencil buffer (happens below in this method).
196 if (geometry_result.mode == GeometryResult::Mode::kPreventOverdraw) {
197 options.stencil_mode =
199 }
200 pass.SetStencilReference(0);
201
202 VertexShaderT::BindFrameInfo(
203 pass, renderer.GetTransientsBuffer().EmplaceUniform(frame_info));
204
205 // The reason we need to have a callback mechanism here is that this routine
206 // may insert draw calls before the main draw call below. For example, for
207 // sufficiently complex paths we may opt to use stencil-then-cover to avoid
208 // tessellation.
209 if (!bind_fragment_callback(pass)) {
210 return false;
211 }
212
213 pass.SetPipeline(pipeline_callback(options));
214
215 if (!pass.Draw().ok()) {
216 return false;
217 }
218
219 // If we performed overdraw prevention, a subsection of the clip heightmap
220 // was incremented by 1 in order to self-clip. So simply append a clip
221 // restore to clean it up.
222 if (geometry_result.mode == GeometryResult::Mode::kPreventOverdraw) {
223 auto restore = ClipRestoreContents();
224 restore.SetRestoreCoverage(GetCoverage(entity));
225 Entity restore_entity = entity.Clone();
226 return restore.Render(renderer, restore_entity, pass);
227 }
228 return true;
229 }
const char * options
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.
const std::shared_ptr< Geometry > & GetGeometry() const
Get the geometry that this contents will use to render.
#define FML_UNREACHABLE()
Definition logging.h:109
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition contents.cc:35

◆ 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::FramebufferBlendContents, and impeller::SolidColorContents.

Definition at line 44 of file color_source_contents.cc.

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

◆ GetGeometry()

const std::shared_ptr< 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}

◆ 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}

◆ 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.
See also
Contents::CanInheritOpacity

Definition at line 28 of file color_source_contents.cc.

28 {
29 return opacity_ * inherited_opacity_;
30}

◆ 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}
unsigned useCenter Optional< SkMatrix > matrix
Definition SkRecords.h:258

◆ SetGeometry()

void impeller::ColorSourceContents::SetGeometry ( std::shared_ptr< 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_ = std::move(geometry);
18}

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

53 {
54 inherited_opacity_ = opacity;
55}

◆ 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: