Flutter Impeller
impeller::AtlasContents Class Referencefinal

#include <atlas_contents.h>

Inheritance diagram for impeller::AtlasContents:
impeller::Contents

Public Member Functions

 AtlasContents ()
 
 ~AtlasContents () override
 
void SetTexture (std::shared_ptr< Texture > texture)
 
std::shared_ptr< TextureGetTexture () const
 
void SetTransforms (std::vector< Matrix > transforms)
 
void SetBlendMode (BlendMode blend_mode)
 
void SetTextureCoordinates (std::vector< Rect > texture_coords)
 
void SetColors (std::vector< Color > colors)
 
void SetCullRect (std::optional< Rect > cull_rect)
 
void SetSamplerDescriptor (SamplerDescriptor desc)
 
void SetAlpha (Scalar alpha)
 
const SamplerDescriptorGetSamplerDescriptor () const
 
const std::vector< Matrix > & GetTransforms () const
 
const std::vector< Rect > & GetTextureCoordinates () const
 
const std::vector< Color > & GetColors () 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...
 
bool Render (const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
 
- 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. More...
 
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
 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< 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). More...
 
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. More...
 
void SetColorSourceSize (Size size)
 
virtual bool CanInheritOpacity (const Entity &entity) const
 Whether or not this contents can accept the opacity peephole optimization. More...
 
virtual void SetInheritedOpacity (Scalar opacity)
 Inherit the provided opacity. More...
 
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 const FilterContentsAsFilter () 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...
 

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

Definition at line 18 of file atlas_contents.h.

Constructor & Destructor Documentation

◆ AtlasContents()

impeller::AtlasContents::AtlasContents ( )
explicitdefault

◆ ~AtlasContents()

impeller::AtlasContents::~AtlasContents ( )
overridedefault

Member Function Documentation

◆ GetColors()

const std::vector< Color > & impeller::AtlasContents::GetColors ( ) const

Definition at line 96 of file atlas_contents.cc.

96  {
97  return colors_;
98 }

◆ GetCoverage()

std::optional< Rect > impeller::AtlasContents::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.

Definition at line 58 of file atlas_contents.cc.

58  {
59  if (cull_rect_.has_value()) {
60  return cull_rect_.value().TransformBounds(entity.GetTransform());
61  }
62  return ComputeBoundingBox().TransformBounds(entity.GetTransform());
63 }

References impeller::Entity::GetTransform(), and impeller::TRect< T >::TransformBounds().

◆ GetSamplerDescriptor()

const SamplerDescriptor & impeller::AtlasContents::GetSamplerDescriptor ( ) const

Definition at line 84 of file atlas_contents.cc.

84  {
85  return sampler_descriptor_;
86 }

◆ GetTexture()

std::shared_ptr< Texture > impeller::AtlasContents::GetTexture ( ) const

Definition at line 28 of file atlas_contents.cc.

28  {
29  return texture_;
30 }

◆ GetTextureCoordinates()

const std::vector< Rect > & impeller::AtlasContents::GetTextureCoordinates ( ) const

Definition at line 92 of file atlas_contents.cc.

92  {
93  return texture_coords_;
94 }

◆ GetTransforms()

const std::vector< Matrix > & impeller::AtlasContents::GetTransforms ( ) const

Definition at line 88 of file atlas_contents.cc.

88  {
89  return transforms_;
90 }

◆ Render()

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

Implements impeller::Contents.

Definition at line 100 of file atlas_contents.cc.

102  {
103  if (texture_ == nullptr || blend_mode_ == BlendMode::kClear ||
104  alpha_ <= 0.0) {
105  return true;
106  }
107 
108  BlendMode blend_mode = blend_mode_;
109  if (colors_.empty()) {
110  blend_mode = BlendMode::kSource;
111  }
112 
113  constexpr size_t indices[6] = {0, 1, 2, 1, 2, 3};
114 
116 
117  VertexBufferBuilder<VS::PerVertexData> vtx_builder;
118  vtx_builder.Reserve(texture_coords_.size() * 6);
119  const auto texture_size = texture_->GetSize();
120  auto& host_buffer = renderer.GetTransientsBuffer();
121  bool has_colors = !colors_.empty();
122  for (size_t i = 0; i < texture_coords_.size(); i++) {
123  auto sample_rect = texture_coords_[i];
124  auto matrix = transforms_[i];
125  auto points = sample_rect.GetPoints();
126  auto transformed_points =
127  Rect::MakeSize(sample_rect.GetSize()).GetTransformedPoints(matrix);
128  Color color =
129  has_colors ? colors_[i].Premultiply() : Color::BlackTransparent();
130  for (size_t j = 0; j < 6; j++) {
131  VS::PerVertexData data;
132  data.vertices = transformed_points[indices[j]];
133  data.texture_coords = points[indices[j]] / texture_size;
134  data.color = color;
135  vtx_builder.AppendVertex(data);
136  }
137  }
138 
139  auto dst_sampler_descriptor = sampler_descriptor_;
140  if (renderer.GetDeviceCapabilities().SupportsDecalSamplerAddressMode()) {
141  dst_sampler_descriptor.width_address_mode = SamplerAddressMode::kDecal;
142  dst_sampler_descriptor.height_address_mode = SamplerAddressMode::kDecal;
143  }
144  const std::unique_ptr<const Sampler>& dst_sampler =
145  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
146  dst_sampler_descriptor);
147 
148  if (blend_mode <= BlendMode::kModulate) {
150 
151 #ifdef IMPELLER_DEBUG
152  pass.SetCommandLabel(
153  SPrintF("DrawAtlas Blend (%s)", BlendModeToString(blend_mode)));
154 #endif // IMPELLER_DEBUG
155  pass.SetVertexBuffer(vtx_builder.CreateVertexBuffer(host_buffer));
156  pass.SetPipeline(
157  renderer.GetPorterDuffBlendPipeline(OptionsFromPass(pass)));
158 
159  FS::FragInfo frag_info;
160  VS::FrameInfo frame_info;
161 
162  FS::BindTextureSamplerDst(pass, texture_, dst_sampler);
163  frame_info.texture_sampler_y_coord_scale = texture_->GetYCoordScale();
164 
165  frag_info.output_alpha = alpha_;
166  frag_info.input_alpha = 1.0;
167 
168  auto inverted_blend_mode =
169  InvertPorterDuffBlend(blend_mode).value_or(BlendMode::kSource);
170  auto blend_coefficients =
171  kPorterDuffCoefficients[static_cast<int>(inverted_blend_mode)];
172  frag_info.src_coeff = blend_coefficients[0];
173  frag_info.src_coeff_dst_alpha = blend_coefficients[1];
174  frag_info.dst_coeff = blend_coefficients[2];
175  frag_info.dst_coeff_src_alpha = blend_coefficients[3];
176  frag_info.dst_coeff_src_color = blend_coefficients[4];
177  // These values are ignored on platforms that natively support decal.
178  frag_info.tmx = static_cast<int>(Entity::TileMode::kDecal);
179  frag_info.tmy = static_cast<int>(Entity::TileMode::kDecal);
180 
181  FS::BindFragInfo(pass, host_buffer.EmplaceUniform(frag_info));
182 
183  frame_info.mvp = entity.GetShaderTransform(pass);
184 
185  auto uniform_view = host_buffer.EmplaceUniform(frame_info);
186  VS::BindFrameInfo(pass, uniform_view);
187 
188  return pass.Draw().ok();
189  }
190 
193 
194 #ifdef IMPELLER_DEBUG
195  pass.SetCommandLabel(
196  SPrintF("DrawAtlas Advanced Blend (%s)", BlendModeToString(blend_mode)));
197 #endif // IMPELLER_DEBUG
198  pass.SetVertexBuffer(vtx_builder.CreateVertexBuffer(host_buffer));
199 
200  pass.SetPipeline(renderer.GetDrawVerticesUberShader(OptionsFromPass(pass)));
201  FS::BindTextureSampler(pass, texture_, dst_sampler);
202 
203  VUS::FrameInfo frame_info;
204  FS::FragInfo frag_info;
205 
206  frame_info.texture_sampler_y_coord_scale = texture_->GetYCoordScale();
207  frame_info.mvp = entity.GetShaderTransform(pass);
208 
209  frag_info.alpha = alpha_;
210  frag_info.blend_mode = static_cast<int>(blend_mode);
211 
212  // These values are ignored on platforms that natively support decal.
213  frag_info.tmx = static_cast<int>(Entity::TileMode::kDecal);
214  frag_info.tmy = static_cast<int>(Entity::TileMode::kDecal);
215 
216  FS::BindFragInfo(pass, host_buffer.EmplaceUniform(frag_info));
217  VS::BindFrameInfo(pass, host_buffer.EmplaceUniform(frame_info));
218 
219  return pass.Draw().ok();
220 }

References impeller::VertexBufferBuilder< VertexType_, IndexType_ >::AppendVertex(), impeller::Color::BlackTransparent(), impeller::BlendModeToString(), color, impeller::VertexBufferBuilder< VertexType_, IndexType_ >::CreateVertexBuffer(), data, impeller::RenderPass::Draw(), impeller::ContentContext::GetContext(), impeller::ContentContext::GetDeviceCapabilities(), impeller::ContentContext::GetDrawVerticesUberShader(), impeller::ContentContext::GetPorterDuffBlendPipeline(), impeller::Entity::GetShaderTransform(), impeller::TRect< T >::GetTransformedPoints(), impeller::ContentContext::GetTransientsBuffer(), impeller::InvertPorterDuffBlend(), impeller::kClear, impeller::Entity::kDecal, impeller::kDecal, impeller::kModulate, impeller::kPorterDuffCoefficients, impeller::kSource, impeller::TRect< Scalar >::MakeSize(), impeller::OptionsFromPass(), impeller::VertexBufferBuilder< VertexType_, IndexType_ >::Reserve(), impeller::RenderPass::SetCommandLabel(), impeller::RenderPass::SetPipeline(), impeller::RenderPass::SetVertexBuffer(), impeller::SPrintF(), impeller::Capabilities::SupportsDecalSamplerAddressMode(), and impeller::SamplerDescriptor::width_address_mode.

◆ SetAlpha()

void impeller::AtlasContents::SetAlpha ( Scalar  alpha)

Definition at line 46 of file atlas_contents.cc.

46  {
47  alpha_ = alpha;
48 }

◆ SetBlendMode()

void impeller::AtlasContents::SetBlendMode ( BlendMode  blend_mode)

Definition at line 50 of file atlas_contents.cc.

50  {
51  blend_mode_ = blend_mode;
52 }

◆ SetColors()

void impeller::AtlasContents::SetColors ( std::vector< Color colors)

Definition at line 42 of file atlas_contents.cc.

42  {
43  colors_ = std::move(colors);
44 }

◆ SetCullRect()

void impeller::AtlasContents::SetCullRect ( std::optional< Rect cull_rect)

Definition at line 54 of file atlas_contents.cc.

54  {
55  cull_rect_ = cull_rect;
56 }

◆ SetSamplerDescriptor()

void impeller::AtlasContents::SetSamplerDescriptor ( SamplerDescriptor  desc)

Definition at line 80 of file atlas_contents.cc.

80  {
81  sampler_descriptor_ = std::move(desc);
82 }

◆ SetTexture()

void impeller::AtlasContents::SetTexture ( std::shared_ptr< Texture texture)

Definition at line 24 of file atlas_contents.cc.

24  {
25  texture_ = std::move(texture);
26 }

◆ SetTextureCoordinates()

void impeller::AtlasContents::SetTextureCoordinates ( std::vector< Rect texture_coords)

Definition at line 37 of file atlas_contents.cc.

37  {
38  texture_coords_ = std::move(texture_coords);
39  bounding_box_cache_.reset();
40 }

◆ SetTransforms()

void impeller::AtlasContents::SetTransforms ( std::vector< Matrix transforms)

Definition at line 32 of file atlas_contents.cc.

32  {
33  transforms_ = std::move(transforms);
34  bounding_box_cache_.reset();
35 }

The documentation for this class was generated from the following files:
impeller::RenderPipelineHandle::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline.h:107
impeller::OptionsFromPass
ContentContextOptions OptionsFromPass(const RenderPass &pass)
Definition: contents.cc:19
impeller::BlendModeToString
const char * BlendModeToString(BlendMode blend_mode)
Definition: color.cc:47
impeller::BlendMode
BlendMode
Definition: color.h:59
data
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63
impeller::Entity::TileMode::kDecal
@ kDecal
impeller::BlendMode::kSource
@ kSource
impeller::TRect::TransformBounds
constexpr TRect TransformBounds(const Matrix &transform) const
Creates a new bounding box that contains this transformed rectangle.
Definition: rect.h:463
impeller::InvertPorterDuffBlend
std::optional< BlendMode > InvertPorterDuffBlend(BlendMode blend_mode)
Definition: blend_filter_contents.cc:31
impeller::BlendMode::kModulate
@ kModulate
impeller::VS
SolidFillVertexShader VS
Definition: stroke_path_geometry.cc:16
impeller::SPrintF
std::string SPrintF(const char *format,...)
Definition: strings.cc:12
impeller::TRect::GetTransformedPoints
constexpr std::array< TPoint< T >, 4 > GetTransformedPoints(const Matrix &transform) const
Definition: rect.h:417
impeller::BlendMode::kClear
@ kClear
impeller::RenderPipelineHandle::VertexShader
VertexShader_ VertexShader
Definition: pipeline.h:106
impeller::SamplerDescriptor::width_address_mode
SamplerAddressMode width_address_mode
Definition: sampler_descriptor.h:20
impeller::kPorterDuffCoefficients
constexpr std::array< std::array< Scalar, 5 >, 15 > kPorterDuffCoefficients
Definition: blend_filter_contents.h:15
impeller::TRect< Scalar >::MakeSize
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:146
impeller::Color::BlackTransparent
static constexpr Color BlackTransparent()
Definition: color.h:272
color
DlColor color
Definition: dl_golden_blur_unittests.cc:23
impeller::SamplerAddressMode::kDecal
@ kDecal
decal sampling mode is only supported on devices that pass the Capabilities.SupportsDecalSamplerAddre...