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)
 
void SetForceTextColor (bool value)
 Force the text color to apply to the rendered glyphs, even if those glyphs are bitmaps. More...
 
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 area of the render pass that will be affected when this contents is rendered. 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 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, 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 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 24 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 49 of file text_contents.cc.

49  {
50  return !frame_->MaybeHasOverlapping();
51 }

◆ GetColor()

Color impeller::TextContents::GetColor ( ) const

Definition at line 45 of file text_contents.cc.

45  {
46  return color_.WithAlpha(color_.alpha * inherited_opacity_);
47 }

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 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 65 of file text_contents.cc.

65  {
66  return frame_->GetBounds().TransformBounds(entity.GetTransform());
67 }

References impeller::Entity::GetTransform().

◆ 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 69 of file text_contents.cc.

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

◆ Render()

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

Implements impeller::Contents.

Definition at line 76 of file text_contents.cc.

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

References impeller::RenderPass::AddCommand(), impeller::Command::BindVertices(), DEBUG_COMMAND_INFO, impeller::HostBuffer::Emplace(), impeller::HostBuffer::EmplaceUniform(), impeller::FontGlyphAtlas::FindGlyphBounds(), impeller::Entity::GetClipDepth(), GetColor(), impeller::ContentContext::GetContext(), impeller::ContentContext::GetGlyphAtlasColorPipeline(), impeller::ContentContext::GetGlyphAtlasPipeline(), impeller::ContentContext::GetLazyGlyphAtlas(), impeller::Font::GetMetrics(), impeller::RenderPass::GetOrthographicTransform(), impeller::Entity::GetTransform(), impeller::RenderPass::GetTransientsBuffer(), impeller::TRect< T >::GetXYWH(), impeller::Matrix::IsTranslationScaleOnly(), impeller::GlyphAtlas::kAlphaBitmap, impeller::GlyphAtlas::kColorBitmap, impeller::kLinear, impeller::kNearest, impeller::kNone, impeller::kTriangle, impeller::SamplerDescriptor::mag_filter, impeller::SamplerDescriptor::min_filter, impeller::SamplerDescriptor::mip_filter, impeller::OptionsFromPassAndEntity(), impeller::Command::pipeline, impeller::Font::Metrics::point_size, impeller::TextFrame::RoundScaledFontSize(), impeller::Command::stencil_reference, impeller::ToVector(), and VALIDATION_LOG.

◆ SetColor()

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

Definition at line 41 of file text_contents.cc.

41  {
42  color_ = color;
43 }

◆ SetForceTextColor()

void impeller::TextContents::SetForceTextColor ( bool  value)

Force the text color to apply to the rendered glyphs, even if those glyphs are bitmaps.

This is used to ensure that mask blurs work correctly on emoji.

Definition at line 61 of file text_contents.cc.

61  {
62  force_text_color_ = value;
63 }

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

53  {
54  inherited_opacity_ = opacity;
55 }

◆ SetOffset()

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

Definition at line 57 of file text_contents.cc.

57  {
58  offset_ = offset;
59 }

◆ SetTextFrame()

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

Definition at line 25 of file text_contents.cc.

25  {
26  frame_ = frame;
27 }

The documentation for this class was generated from the following files:
impeller::GlyphAtlas::Type::kColorBitmap
@ kColorBitmap
DEBUG_COMMAND_INFO
#define DEBUG_COMMAND_INFO(obj, arg)
Definition: command.h:28
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::GlyphAtlas::Type::kAlphaBitmap
@ kAlphaBitmap
impeller::Vector2
Point Vector2
Definition: point.h:312
impeller::RenderPipelineT::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline.h:93
impeller::Color::alpha
Scalar alpha
Definition: color.h:143
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::PrimitiveType::kTriangle
@ kTriangle
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:28
impeller::MipFilter::kNearest
@ kNearest
Sample from the nearest mip level.
impeller::Point
TPoint< Scalar > Point
Definition: point.h:308
impeller::MinMagFilter::kLinear
@ kLinear
impeller::Color::WithAlpha
constexpr Color WithAlpha(Scalar new_alpha) const
Definition: color.h:270
impeller::IndexType::kNone
@ kNone
Does not use the index buffer.
impeller::Rect
TRect< Scalar > Rect
Definition: rect.h:488
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:67
impeller::ToVector
constexpr Vector4 ToVector(Color color)
Definition: shader_types.h:185
impeller::TextContents::GetColor
Color GetColor() const
Definition: text_contents.cc:45
impeller::RenderPipelineT::VertexShader
VertexShader_ VertexShader
Definition: pipeline.h:92