Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Classes | Public Member Functions | List of all members
impeller::RuntimeEffectContents Class Referencefinal

#include <runtime_effect_contents.h>

Inheritance diagram for impeller::RuntimeEffectContents:
impeller::ColorSourceContents impeller::Contents

Classes

struct  TextureInput
 

Public Member Functions

void SetRuntimeStage (std::shared_ptr< RuntimeStage > runtime_stage)
 
void SetUniformData (std::shared_ptr< std::vector< uint8_t > > uniform_data)
 
void SetTextureInputs (std::vector< TextureInput > texture_inputs)
 
bool CanInheritOpacity (const Entity &entity) const override
 Whether or not this contents can accept the opacity peephole optimization.
 
bool Render (const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
 
bool BootstrapShader (const ContentContext &renderer) const
 Load the runtime effect and ensure a default PSO is initialized.
 
- Public Member Functions inherited from impeller::ColorSourceContents
 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.
 
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.
 
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.
 

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)
 
- Protected Types inherited from impeller::ColorSourceContents
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 inherited from impeller::ColorSourceContents
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
 

Detailed Description

Definition at line 17 of file runtime_effect_contents.h.

Member Function Documentation

◆ BootstrapShader()

bool impeller::RuntimeEffectContents::BootstrapShader ( const ContentContext renderer) const

Load the runtime effect and ensure a default PSO is initialized.

Definition at line 70 of file runtime_effect_contents.cc.

71 {
72 if (!RegisterShader(renderer)) {
73 return false;
74 }
75 ContentContextOptions options;
76 options.color_attachment_pixel_format =
77 renderer.GetContext()->GetCapabilities()->GetDefaultColorFormat();
78 return !!CreatePipeline(renderer, options);
79}
const char * options

◆ CanInheritOpacity()

bool impeller::RuntimeEffectContents::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::ColorSourceContents.

Definition at line 42 of file runtime_effect_contents.cc.

42 {
43 return false;
44}

◆ Render()

bool impeller::RuntimeEffectContents::Render ( const ContentContext renderer,
const Entity entity,
RenderPass pass 
) const
overridevirtual

Get or register shader. Flutter will do this when the runtime effect is first loaded, but this check is added to supporting testing of the Aiks API and non-flutter usage of Impeller.

Fragment stage uniforms.

Now that the descriptor set layouts are known, get the pipeline.

Implements impeller::Contents.

Definition at line 181 of file runtime_effect_contents.cc.

183 {
184 const std::shared_ptr<Context>& context = renderer.GetContext();
185 const std::shared_ptr<ShaderLibrary>& library = context->GetShaderLibrary();
186
187 //--------------------------------------------------------------------------
188 /// Get or register shader. Flutter will do this when the runtime effect
189 /// is first loaded, but this check is added to supporting testing of the
190 /// Aiks API and non-flutter usage of Impeller.
191 ///
192 if (!RegisterShader(renderer)) {
193 return false;
194 }
195
196 //--------------------------------------------------------------------------
197 /// Fragment stage uniforms.
198 ///
199 BindFragmentCallback bind_callback = [this, &renderer,
200 &context](RenderPass& pass) {
201 size_t minimum_sampler_index = 100000000;
202 size_t buffer_index = 0;
203 size_t buffer_offset = 0;
204
205 for (const auto& uniform : runtime_stage_->GetUniforms()) {
206 std::shared_ptr<ShaderMetadata> metadata = MakeShaderMetadata(uniform);
207
208 switch (uniform.type) {
209 case kSampledImage: {
210 // Sampler uniforms are ordered in the IPLR according to their
211 // declaration and the uniform location reflects the correct offset to
212 // be mapped to - except that it may include all proceeding float
213 // uniforms. For example, a float sampler that comes after 4 float
214 // uniforms may have a location of 4. To convert to the actual offset
215 // we need to find the largest location assigned to a float uniform
216 // and then subtract this from all uniform locations. This is more or
217 // less the same operation we previously performed in the shader
218 // compiler.
219 minimum_sampler_index =
220 std::min(minimum_sampler_index, uniform.location);
221 break;
222 }
223 case kFloat: {
224 FML_DCHECK(renderer.GetContext()->GetBackendType() !=
226 << "Uniform " << uniform.name
227 << " had unexpected type kFloat for Vulkan backend.";
228
229 size_t alignment =
230 std::max(uniform.bit_width / 8, DefaultUniformAlignment());
231 BufferView buffer_view = renderer.GetTransientsBuffer().Emplace(
232 uniform_data_->data() + buffer_offset, uniform.GetSize(),
233 alignment);
234
235 ShaderUniformSlot uniform_slot;
236 uniform_slot.name = uniform.name.c_str();
237 uniform_slot.ext_res_0 = uniform.location;
238 pass.BindResource(ShaderStage::kFragment,
239 DescriptorType::kUniformBuffer, uniform_slot,
240 metadata, std::move(buffer_view));
241 buffer_index++;
242 buffer_offset += uniform.GetSize();
243 break;
244 }
245 case kStruct: {
246 FML_DCHECK(renderer.GetContext()->GetBackendType() ==
248 ShaderUniformSlot uniform_slot;
249 uniform_slot.name = uniform.name.c_str();
250 uniform_slot.binding = uniform.location;
251
252 // TODO(jonahwilliams): rewrite this to emplace directly into
253 // HostBuffer.
254 std::vector<float> uniform_buffer;
255 uniform_buffer.reserve(uniform.struct_layout.size());
256 size_t uniform_byte_index = 0u;
257 for (const auto& byte_type : uniform.struct_layout) {
258 if (byte_type == 0) {
259 uniform_buffer.push_back(0.f);
260 } else if (byte_type == 1) {
261 uniform_buffer.push_back(reinterpret_cast<float*>(
262 uniform_data_->data())[uniform_byte_index++]);
263 } else {
265 }
266 }
267 size_t alignment = std::max(sizeof(float) * uniform_buffer.size(),
269
270 BufferView buffer_view = renderer.GetTransientsBuffer().Emplace(
271 reinterpret_cast<const void*>(uniform_buffer.data()),
272 sizeof(float) * uniform_buffer.size(), alignment);
273 pass.BindResource(ShaderStage::kFragment,
274 DescriptorType::kUniformBuffer, uniform_slot,
275 ShaderMetadata{}, std::move(buffer_view));
276 }
277 }
278 }
279
280 size_t sampler_index = 0;
281 for (const auto& uniform : runtime_stage_->GetUniforms()) {
282 std::shared_ptr<ShaderMetadata> metadata = MakeShaderMetadata(uniform);
283
284 switch (uniform.type) {
285 case kSampledImage: {
286 FML_DCHECK(sampler_index < texture_inputs_.size());
287 auto& input = texture_inputs_[sampler_index];
288
289 const std::unique_ptr<const Sampler>& sampler =
290 context->GetSamplerLibrary()->GetSampler(
291 input.sampler_descriptor);
292
293 SampledImageSlot image_slot;
294 image_slot.name = uniform.name.c_str();
295 image_slot.binding = uniform.binding;
296 image_slot.texture_index = uniform.location - minimum_sampler_index;
297 pass.BindResource(ShaderStage::kFragment,
299 *metadata, input.texture, sampler);
300
301 sampler_index++;
302 break;
303 }
304 default:
305 continue;
306 }
307 }
308 return true;
309 };
310
311 /// Now that the descriptor set layouts are known, get the pipeline.
312 using VS = RuntimeEffectVertexShader;
313
314 PipelineBuilderCallback pipeline_callback =
315 [&](ContentContextOptions options) {
316 // Pipeline creation callback for the cache handler to call.
317 return renderer.GetCachedRuntimeEffectPipeline(
318 runtime_stage_->GetEntrypoint(), options,
319 [&]() { return CreatePipeline(renderer, options); });
320 };
321
322 return ColorSourceContents::DrawGeometry<VS>(renderer, entity, pass,
323 pipeline_callback,
324 VS::FrameInfo{}, bind_callback);
325}
std::function< std::shared_ptr< Pipeline< PipelineDescriptor > >(ContentContextOptions)> PipelineBuilderCallback
std::function< bool(RenderPass &pass)> BindFragmentCallback
#define FML_UNREACHABLE()
Definition logging.h:109
#define FML_DCHECK(condition)
Definition logging.h:103
static std::shared_ptr< ShaderMetadata > MakeShaderMetadata(const RuntimeUniformDescription &uniform)
SolidFillVertexShader VS
constexpr size_t DefaultUniformAlignment()
Definition platform.h:15

◆ SetRuntimeStage()

void impeller::RuntimeEffectContents::SetRuntimeStage ( std::shared_ptr< RuntimeStage runtime_stage)

Definition at line 27 of file runtime_effect_contents.cc.

28 {
29 runtime_stage_ = std::move(runtime_stage);
30}

◆ SetTextureInputs()

void impeller::RuntimeEffectContents::SetTextureInputs ( std::vector< TextureInput texture_inputs)

Definition at line 37 of file runtime_effect_contents.cc.

38 {
39 texture_inputs_ = std::move(texture_inputs);
40}

◆ SetUniformData()

void impeller::RuntimeEffectContents::SetUniformData ( std::shared_ptr< std::vector< uint8_t > >  uniform_data)

Definition at line 32 of file runtime_effect_contents.cc.

33 {
34 uniform_data_ = std::move(uniform_data);
35}

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