Flutter Impeller
impeller::TextContents Class Referencefinal

#include <text_contents.h>

Inheritance diagram for impeller::TextContents:
impeller::Contents

Public Member Functions

 TextContents ()
 
 ~TextContents ()
 
void SetTextFrame (const std::shared_ptr< TextFrame > &frame)
 
void SetColor (Color color)
 
Color GetColor () const
 
bool CanInheritOpacity (const Entity &entity) const override
 Whether or not this contents can accept the opacity peephole optimization. More...
 
void SetInheritedOpacity (Scalar opacity) override
 Inherit the provided opacity. More...
 
void SetOffset (Vector2 offset)
 
std::optional< RectGetTextFrameBounds () const
 
std::optional< RectGetCoverage (const Entity &entity) const override
 Get the screen space bounding rectangle that this contents affects. More...
 
void PopulateGlyphAtlas (const std::shared_ptr< LazyGlyphAtlas > &lazy_glyph_atlas, Scalar scale) override
 Add any text data to the specified lazy atlas. The scale parameter must be used again later when drawing the text. More...
 
bool Render (const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
 
- Public Member Functions inherited from impeller::Contents
 Contents ()
 
virtual ~Contents ()
 
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 StencilCoverage GetStencilCoverage (const Entity &entity, const std::optional< Rect > &current_stencil_coverage) const
 Given the current screen space bounding rectangle of the stencil, return the expected stencil coverage after this draw call. This should only be implemented for contents that may write to the stencil 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, const std::string &label="Snapshot") const
 Render this contents to a snapshot, respecting the entity's transform, path, stencil 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 > &stencil_coverage) const
 
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 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 23 of file text_contents.h.

Constructor & Destructor Documentation

◆ TextContents()

impeller::TextContents::TextContents ( )
default

◆ ~TextContents()

impeller::TextContents::~TextContents ( )
default

Member Function Documentation

◆ CanInheritOpacity()

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

Definition at line 51 of file text_contents.cc.

51  {
52  return !frame_->MaybeHasOverlapping();
53 }

◆ GetColor()

Color impeller::TextContents::GetColor ( ) const

Definition at line 47 of file text_contents.cc.

47  {
48  return color_.WithAlpha(color_.alpha * inherited_opacity_);
49 }

References impeller::Color::alpha, and impeller::Color::WithAlpha().

Referenced by Render().

◆ GetCoverage()

std::optional< Rect > impeller::TextContents::GetCoverage ( const Entity entity) const
overridevirtual

Get the screen space bounding rectangle that this contents affects.

Implements impeller::Contents.

Definition at line 63 of file text_contents.cc.

63  {
64  return frame_->GetBounds().TransformBounds(entity.GetTransformation());
65 }

References impeller::Entity::GetTransformation().

◆ GetTextFrameBounds()

std::optional<Rect> impeller::TextContents::GetTextFrameBounds ( ) const

◆ PopulateGlyphAtlas()

void impeller::TextContents::PopulateGlyphAtlas ( const std::shared_ptr< LazyGlyphAtlas > &  lazy_glyph_atlas,
Scalar  scale 
)
overridevirtual

Add any text data to the specified lazy atlas. The scale parameter must be used again later when drawing the text.

Reimplemented from impeller::Contents.

Definition at line 67 of file text_contents.cc.

69  {
70  lazy_glyph_atlas->AddTextFrame(*frame_, scale);
71  scale_ = scale;
72 }

◆ Render()

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

Implements impeller::Contents.

Definition at line 74 of file text_contents.cc.

76  {
77  auto color = GetColor();
78  if (color.IsTransparent()) {
79  return true;
80  }
81 
82  auto type = frame_->GetAtlasType();
83  auto atlas =
84  ResolveAtlas(*renderer.GetContext(), type, renderer.GetLazyGlyphAtlas());
85 
86  if (!atlas || !atlas->IsValid()) {
87  VALIDATION_LOG << "Cannot render glyphs without prepared atlas.";
88  return false;
89  }
90 
91  // Information shared by all glyph draw calls.
92  Command cmd;
93  DEBUG_COMMAND_INFO(cmd, "TextFrame");
94  auto opts = OptionsFromPassAndEntity(pass, entity);
95  opts.primitive_type = PrimitiveType::kTriangle;
96  if (type == GlyphAtlas::Type::kAlphaBitmap) {
97  cmd.pipeline = renderer.GetGlyphAtlasPipeline(opts);
98  } else {
99  cmd.pipeline = renderer.GetGlyphAtlasColorPipeline(opts);
100  }
101  cmd.stencil_reference = entity.GetStencilDepth();
102 
105 
106  // Common vertex uniforms for all glyphs.
107  VS::FrameInfo frame_info;
108  frame_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize());
109  frame_info.atlas_size =
110  Vector2{static_cast<Scalar>(atlas->GetTexture()->GetSize().width),
111  static_cast<Scalar>(atlas->GetTexture()->GetSize().height)};
112  frame_info.offset = offset_;
113  frame_info.is_translation_scale =
114  entity.GetTransformation().IsTranslationScaleOnly();
115  frame_info.entity_transform = entity.GetTransformation();
116  frame_info.text_color = ToVector(color.Premultiply());
117 
118  VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info));
119 
120  SamplerDescriptor sampler_desc;
121  if (frame_info.is_translation_scale) {
122  sampler_desc.min_filter = MinMagFilter::kNearest;
123  sampler_desc.mag_filter = MinMagFilter::kNearest;
124  } else {
125  // Currently, we only propagate the scale of the transform to the atlas
126  // renderer, so if the transform has more than just a translation, we turn
127  // on linear sampling to prevent crunchiness caused by the pixel grid not
128  // being perfectly aligned.
129  // The downside is that this slightly over-blurs rotated/skewed text.
130  sampler_desc.min_filter = MinMagFilter::kLinear;
131  sampler_desc.mag_filter = MinMagFilter::kLinear;
132  }
133  sampler_desc.mip_filter = MipFilter::kNearest;
134 
135  FS::BindGlyphAtlasSampler(
136  cmd, // command
137  atlas->GetTexture(), // texture
138  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
139  sampler_desc) // sampler
140  );
141 
142  // Common vertex information for all glyphs.
143  // All glyphs are given the same vertex information in the form of a
144  // unit-sized quad. The size of the glyph is specified in per instance data
145  // and the vertex shader uses this to size the glyph correctly. The
146  // interpolated vertex information is also used in the fragment shader to
147  // sample from the glyph atlas.
148 
149  constexpr std::array<Point, 6> unit_points = {Point{0, 0}, Point{1, 0},
150  Point{0, 1}, Point{1, 0},
151  Point{0, 1}, Point{1, 1}};
152 
153  auto& host_buffer = pass.GetTransientsBuffer();
154  size_t vertex_count = 0;
155  for (const auto& run : frame_->GetRuns()) {
156  vertex_count += run.GetGlyphPositions().size();
157  }
158  vertex_count *= 6;
159 
160  auto buffer_view = host_buffer.Emplace(
161  vertex_count * sizeof(VS::PerVertexData), alignof(VS::PerVertexData),
162  [&](uint8_t* contents) {
163  VS::PerVertexData vtx;
164  VS::PerVertexData* vtx_contents =
165  reinterpret_cast<VS::PerVertexData*>(contents);
166  for (const TextRun& run : frame_->GetRuns()) {
167  const Font& font = run.GetFont();
168  Scalar rounded_scale = TextFrame::RoundScaledFontSize(
169  scale_, font.GetMetrics().point_size);
170  const FontGlyphAtlas* font_atlas =
171  atlas->GetFontGlyphAtlas(font, rounded_scale);
172  if (!font_atlas) {
173  VALIDATION_LOG << "Could not find font in the atlas.";
174  continue;
175  }
176 
177  for (const TextRun::GlyphPosition& glyph_position :
178  run.GetGlyphPositions()) {
179  std::optional<Rect> maybe_atlas_glyph_bounds =
180  font_atlas->FindGlyphBounds(glyph_position.glyph);
181  if (!maybe_atlas_glyph_bounds.has_value()) {
182  VALIDATION_LOG << "Could not find glyph position in the atlas.";
183  continue;
184  }
185  const Rect& atlas_glyph_bounds = maybe_atlas_glyph_bounds.value();
186  vtx.atlas_glyph_bounds = Vector4(
187  atlas_glyph_bounds.origin.x, atlas_glyph_bounds.origin.y,
188  atlas_glyph_bounds.size.width, atlas_glyph_bounds.size.height);
189  vtx.glyph_bounds = Vector4(glyph_position.glyph.bounds.origin.x,
190  glyph_position.glyph.bounds.origin.y,
191  glyph_position.glyph.bounds.size.width,
192  glyph_position.glyph.bounds.size.height);
193  vtx.glyph_position = glyph_position.position;
194 
195  for (const Point& point : unit_points) {
196  vtx.unit_position = point;
197  std::memcpy(vtx_contents++, &vtx, sizeof(VS::PerVertexData));
198  }
199  }
200  }
201  });
202 
203  cmd.BindVertices({
204  .vertex_buffer = buffer_view,
205  .index_buffer = {},
206  .vertex_count = vertex_count,
207  .index_type = IndexType::kNone,
208  });
209 
210  return pass.AddCommand(std::move(cmd));
211 }

References impeller::RenderPass::AddCommand(), impeller::Command::BindVertices(), DEBUG_COMMAND_INFO, impeller::HostBuffer::Emplace(), impeller::HostBuffer::EmplaceUniform(), impeller::FontGlyphAtlas::FindGlyphBounds(), GetColor(), impeller::ContentContext::GetContext(), impeller::ContentContext::GetGlyphAtlasColorPipeline(), impeller::ContentContext::GetGlyphAtlasPipeline(), impeller::ContentContext::GetLazyGlyphAtlas(), impeller::Font::GetMetrics(), impeller::RenderPass::GetRenderTargetSize(), impeller::Entity::GetStencilDepth(), impeller::Entity::GetTransformation(), impeller::RenderPass::GetTransientsBuffer(), impeller::TSize< T >::height, impeller::Matrix::IsTranslationScaleOnly(), impeller::GlyphAtlas::kAlphaBitmap, impeller::kLinear, impeller::kNearest, impeller::kNone, impeller::kTriangle, impeller::SamplerDescriptor::mag_filter, impeller::Matrix::MakeOrthographic(), impeller::SamplerDescriptor::min_filter, impeller::SamplerDescriptor::mip_filter, impeller::OptionsFromPassAndEntity(), impeller::TRect< T >::origin, impeller::Command::pipeline, impeller::Font::Metrics::point_size, impeller::TextFrame::RoundScaledFontSize(), impeller::TRect< T >::size, impeller::Command::stencil_reference, impeller::ToVector(), VALIDATION_LOG, impeller::TSize< T >::width, impeller::TPoint< T >::x, and impeller::TPoint< T >::y.

◆ SetColor()

void impeller::TextContents::SetColor ( Color  color)

Definition at line 43 of file text_contents.cc.

43  {
44  color_ = color;
45 }

◆ SetInheritedOpacity()

void impeller::TextContents::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 55 of file text_contents.cc.

55  {
56  inherited_opacity_ = opacity;
57 }

◆ SetOffset()

void impeller::TextContents::SetOffset ( Vector2  offset)

Definition at line 59 of file text_contents.cc.

59  {
60  offset_ = offset;
61 }

◆ SetTextFrame()

void impeller::TextContents::SetTextFrame ( const std::shared_ptr< TextFrame > &  frame)

Definition at line 27 of file text_contents.cc.

27  {
28  frame_ = frame;
29 }

The documentation for this class was generated from the following files:
DEBUG_COMMAND_INFO
#define DEBUG_COMMAND_INFO(obj, arg)
Definition: command.h:31
impeller::Scalar
float Scalar
Definition: scalar.h:15
impeller::GlyphAtlas::Type::kAlphaBitmap
@ kAlphaBitmap
impeller::Vector2
Point Vector2
Definition: point.h:310
impeller::RenderPipelineT::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline.h:91
impeller::Color::alpha
Scalar alpha
Definition: color.h:141
impeller::TextFrame::RoundScaledFontSize
static Scalar RoundScaledFontSize(Scalar scale, Scalar point_size)
Definition: text_frame.cc:66
impeller::MinMagFilter::kNearest
@ kNearest
Select nearest to the sample point. Most widely supported.
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:30
impeller::MipFilter::kNearest
@ kNearest
Sample from the nearest mip level.
impeller::Point
TPoint< Scalar > Point
Definition: point.h:306
impeller::MinMagFilter::kLinear
@ kLinear
impeller::Color::WithAlpha
constexpr Color WithAlpha(Scalar new_alpha) const
Definition: color.h:268
impeller::IndexType::kNone
@ kNone
Does not use the index buffer.
impeller::Rect
TRect< Scalar > Rect
Definition: rect.h:306
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:60
impeller::ToVector
constexpr Vector4 ToVector(Color color)
Definition: shader_types.h:172
impeller::Matrix::MakeOrthographic
static constexpr Matrix MakeOrthographic(TSize< T > size)
Definition: matrix.h:448
impeller::TextContents::GetColor
Color GetColor() const
Definition: text_contents.cc:47
impeller::PrimitiveType::kTriangle
@ kTriangle
impeller::RenderPipelineT::VertexShader
VertexShader_ VertexShader
Definition: pipeline.h:90