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...
 
void SetTextProperties (Color color, bool stroke, Scalar stroke_width, Cap stroke_cap, Join stroke_join, Scalar stroke_miter)
 Must be set after text frame. More...
 
Color GetColor () const
 
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 SetScale (Scalar scale)
 
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 Matrix &transform) 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 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, std::string_view 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...
 
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 bool ApplyColorFilter (const ColorFilterProc &color_filter_proc)
 If possible, applies a color filter to this contents inputs on the CPU. More...
 

Static Public Member Functions

static void ComputeVertexData (GlyphAtlasPipeline::VertexShader::PerVertexData *vtx_contents, const std::shared_ptr< TextFrame > &frame, Scalar scale, const Matrix &entity_transform, Vector2 offset, std::optional< GlyphProperties > glyph_properties, const std::shared_ptr< GlyphAtlas > &atlas)
 
- Static Public Member Functions inherited from impeller::Contents
static std::shared_ptr< ContentsMakeAnonymous (RenderProc render_proc, CoverageProc coverage_proc)
 

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

Detailed Description

Definition at line 21 of file text_contents.h.

Constructor & Destructor Documentation

◆ TextContents()

impeller::TextContents::TextContents ( )
default

◆ ~TextContents()

impeller::TextContents::~TextContents ( )
default

Member Function Documentation

◆ ComputeVertexData()

void impeller::TextContents::ComputeVertexData ( GlyphAtlasPipeline::VertexShader::PerVertexData *  vtx_contents,
const std::shared_ptr< TextFrame > &  frame,
Scalar  scale,
const Matrix entity_transform,
Vector2  offset,
std::optional< GlyphProperties glyph_properties,
const std::shared_ptr< GlyphAtlas > &  atlas 
)
static

Definition at line 80 of file text_contents.cc.

87  {
88  // Common vertex information for all glyphs.
89  // All glyphs are given the same vertex information in the form of a
90  // unit-sized quad. The size of the glyph is specified in per instance data
91  // and the vertex shader uses this to size the glyph correctly. The
92  // interpolated vertex information is also used in the fragment shader to
93  // sample from the glyph atlas.
94 
95  constexpr std::array<Point, 6> unit_points = {Point{0, 0}, Point{1, 0},
96  Point{0, 1}, Point{1, 0},
97  Point{0, 1}, Point{1, 1}};
98 
99  ISize atlas_size = atlas->GetTexture()->GetSize();
100  bool is_translation_scale = entity_transform.IsTranslationScaleOnly();
101  Matrix basis_transform = entity_transform.Basis();
102 
103  VS::PerVertexData vtx;
104  size_t i = 0u;
105  size_t bounds_offset = 0u;
106  for (const TextRun& run : frame->GetRuns()) {
107  const Font& font = run.GetFont();
108  Scalar rounded_scale = frame->GetScale();
109  const Matrix transform = frame->GetOffsetTransform();
110  FontGlyphAtlas* font_atlas = nullptr;
111 
112  // Adjust glyph position based on the subpixel rounding
113  // used by the font.
114  Point subpixel_adjustment(0.5, 0.5);
115  switch (font.GetAxisAlignment()) {
117  break;
118  case AxisAlignment::kX:
119  subpixel_adjustment.x = 0.125;
120  break;
121  case AxisAlignment::kY:
122  subpixel_adjustment.y = 0.125;
123  break;
124  case AxisAlignment::kAll:
125  subpixel_adjustment.x = 0.125;
126  subpixel_adjustment.y = 0.125;
127  break;
128  }
129 
130  Point screen_offset = (entity_transform * Point(0, 0));
131  for (const TextRun::GlyphPosition& glyph_position :
132  run.GetGlyphPositions()) {
133  const FrameBounds& frame_bounds = frame->GetFrameBounds(bounds_offset);
134  bounds_offset++;
135  auto atlas_glyph_bounds = frame_bounds.atlas_bounds;
136  auto glyph_bounds = frame_bounds.glyph_bounds;
137 
138  // If frame_bounds.is_placeholder is true, this is the first frame
139  // the glyph has been rendered and so its atlas position was not
140  // known when the glyph was recorded. Perform a slow lookup into the
141  // glyph atlas hash table.
142  if (frame_bounds.is_placeholder) {
143  if (!font_atlas) {
144  font_atlas =
145  atlas->GetOrCreateFontGlyphAtlas(ScaledFont{font, rounded_scale});
146  }
147 
148  if (!font_atlas) {
149  VALIDATION_LOG << "Could not find font in the atlas.";
150  continue;
151  }
153  glyph_position, font.GetAxisAlignment(), transform);
154 
155  std::optional<FrameBounds> maybe_atlas_glyph_bounds =
156  font_atlas->FindGlyphBounds(SubpixelGlyph{
157  glyph_position.glyph, //
158  subpixel, //
159  glyph_properties //
160  });
161  if (!maybe_atlas_glyph_bounds.has_value()) {
162  VALIDATION_LOG << "Could not find glyph position in the atlas.";
163  continue;
164  }
165  atlas_glyph_bounds = maybe_atlas_glyph_bounds.value().atlas_bounds;
166  }
167 
168  Rect scaled_bounds = glyph_bounds.Scale(1.0 / rounded_scale);
169  // For each glyph, we compute two rectangles. One for the vertex
170  // positions and one for the texture coordinates (UVs). The atlas
171  // glyph bounds are used to compute UVs in cases where the
172  // destination and source sizes may differ due to clamping the sizes
173  // of large glyphs.
174  Point uv_origin = (atlas_glyph_bounds.GetLeftTop()) / atlas_size;
175  Point uv_size = SizeToPoint(atlas_glyph_bounds.GetSize()) / atlas_size;
176 
177  Matrix unscaled_basis =
178  Matrix::MakeScale({basis_transform.m[0] > 0 ? 1.f : -1.f,
179  basis_transform.m[5] > 0 ? 1.f : -1.f, 1.f});
180  Point unrounded_glyph_position =
181  // This is for RTL text.
182  unscaled_basis * glyph_bounds.GetLeftTop() +
183  (basis_transform * glyph_position.position);
184 
185  Point screen_glyph_position =
186  (screen_offset + unrounded_glyph_position + subpixel_adjustment)
187  .Floor();
188  for (const Point& point : unit_points) {
189  Point position;
190  if (is_translation_scale) {
191  position = (screen_glyph_position +
192  (unscaled_basis * point * glyph_bounds.GetSize()))
193  .Round();
194  } else {
195  position = entity_transform *
196  (glyph_position.position + scaled_bounds.GetLeftTop() +
197  point * scaled_bounds.GetSize());
198  }
199  vtx.uv = uv_origin + (uv_size * point);
200  vtx.position = position;
201  vtx_contents[i++] = vtx;
202  }
203  }
204  }
205 }
static Point ComputeSubpixelPosition(const TextRun::GlyphPosition &glyph_position, AxisAlignment alignment, const Matrix &transform)
Definition: text_frame.cc:69
float Scalar
Definition: scalar.h:18
TRect< Scalar > Rect
Definition: rect.h:792
TPoint< Scalar > Point
Definition: point.h:327
Point SizeToPoint(Size size)
ISize64 ISize
Definition: size.h:174
SolidFillVertexShader::PerVertexData vtx
static constexpr Matrix MakeScale(const Vector3 &s)
Definition: matrix.h:104
static constexpr TPoint Round(const TPoint< U > &other)
Definition: point.h:49
constexpr TRect Scale(Type scale) const
Definition: rect.h:206
#define VALIDATION_LOG
Definition: validation.h:91

References impeller::FrameBounds::atlas_bounds, impeller::Matrix::Basis(), impeller::TextFrame::ComputeSubpixelPosition(), impeller::FontGlyphAtlas::FindGlyphBounds(), impeller::Font::GetAxisAlignment(), impeller::TRect< T >::GetLeftTop(), impeller::TRect< T >::GetSize(), impeller::SubpixelGlyph::glyph, impeller::FrameBounds::glyph_bounds, impeller::FrameBounds::is_placeholder, impeller::Matrix::IsTranslationScaleOnly(), impeller::kAll, impeller::kNone, impeller::kX, impeller::kY, impeller::Matrix::m, impeller::Matrix::MakeScale(), impeller::TPoint< T >::Round(), impeller::TRect< T >::Scale(), impeller::SizeToPoint(), transform, VALIDATION_LOG, vtx, impeller::TPoint< T >::x, and impeller::TPoint< T >::y.

Referenced by Render(), and impeller::testing::TEST_P().

◆ GetColor()

Color impeller::TextContents::GetColor ( ) const

Definition at line 40 of file text_contents.cc.

40  {
41  return color_.WithAlpha(color_.alpha * inherited_opacity_);
42 }
Scalar alpha
Definition: color.h:142
constexpr Color WithAlpha(Scalar new_alpha) const
Definition: color.h:277

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

56  {
57  return frame_->GetBounds().TransformBounds(entity.GetTransform());
58 }

References impeller::Entity::GetTransform().

◆ GetTextFrameBounds()

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

◆ Render()

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

Implements impeller::Contents.

Definition at line 207 of file text_contents.cc.

209  {
210  auto color = GetColor();
211  if (color.IsTransparent()) {
212  return true;
213  }
214 
215  auto type = frame_->GetAtlasType();
216  const std::shared_ptr<GlyphAtlas>& atlas =
217  renderer.GetLazyGlyphAtlas()->CreateOrGetGlyphAtlas(
218  *renderer.GetContext(), renderer.GetTransientsBuffer(), type);
219 
220  if (!atlas || !atlas->IsValid()) {
221  VALIDATION_LOG << "Cannot render glyphs without prepared atlas.";
222  return false;
223  }
224  if (!frame_->IsFrameComplete()) {
225  VALIDATION_LOG << "Failed to find font glyph bounds.";
226  return false;
227  }
228 
229  // Information shared by all glyph draw calls.
230  pass.SetCommandLabel("TextFrame");
231  auto opts = OptionsFromPassAndEntity(pass, entity);
232  opts.primitive_type = PrimitiveType::kTriangle;
233  pass.SetPipeline(renderer.GetGlyphAtlasPipeline(opts));
234 
235  // Common vertex uniforms for all glyphs.
236  VS::FrameInfo frame_info;
237  frame_info.mvp =
238  Entity::GetShaderTransform(entity.GetShaderClipDepth(), pass, Matrix());
239  bool is_translation_scale = entity.GetTransform().IsTranslationScaleOnly();
240  Matrix entity_transform = entity.GetTransform();
241 
242  VS::BindFrameInfo(pass,
243  renderer.GetTransientsBuffer().EmplaceUniform(frame_info));
244 
245  FS::FragInfo frag_info;
246  frag_info.use_text_color = force_text_color_ ? 1.0 : 0.0;
247  frag_info.text_color = ToVector(color.Premultiply());
248  frag_info.is_color_glyph = type == GlyphAtlas::Type::kColorBitmap;
249 
250  FS::BindFragInfo(pass,
251  renderer.GetTransientsBuffer().EmplaceUniform(frag_info));
252 
253  SamplerDescriptor sampler_desc;
254  if (is_translation_scale) {
255  sampler_desc.min_filter = MinMagFilter::kNearest;
256  sampler_desc.mag_filter = MinMagFilter::kNearest;
257  } else {
258  // Currently, we only propagate the scale of the transform to the atlas
259  // renderer, so if the transform has more than just a translation, we turn
260  // on linear sampling to prevent crunchiness caused by the pixel grid not
261  // being perfectly aligned.
262  // The downside is that this slightly over-blurs rotated/skewed text.
263  sampler_desc.min_filter = MinMagFilter::kLinear;
264  sampler_desc.mag_filter = MinMagFilter::kLinear;
265  }
266 
267  // No mipmaps for glyph atlas (glyphs are generated at exact scales).
268  sampler_desc.mip_filter = MipFilter::kBase;
269 
270  FS::BindGlyphAtlasSampler(
271  pass, // command
272  atlas->GetTexture(), // texture
273  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
274  sampler_desc) // sampler
275  );
276 
277  auto& host_buffer = renderer.GetTransientsBuffer();
278  size_t vertex_count = 0;
279  for (const auto& run : frame_->GetRuns()) {
280  vertex_count += run.GetGlyphPositions().size();
281  }
282  vertex_count *= 6;
283 
284  BufferView buffer_view = host_buffer.Emplace(
285  vertex_count * sizeof(VS::PerVertexData), alignof(VS::PerVertexData),
286  [&](uint8_t* contents) {
287  VS::PerVertexData* vtx_contents =
288  reinterpret_cast<VS::PerVertexData*>(contents);
289  ComputeVertexData(vtx_contents, frame_, scale_,
290  /*entity_transform=*/entity_transform, offset_,
291  GetGlyphProperties(), atlas);
292  });
293 
294  pass.SetVertexBuffer(std::move(buffer_view));
295  pass.SetIndexBuffer({}, IndexType::kNone);
296  pass.SetElementCount(vertex_count);
297 
298  return pass.Draw().ok();
299 }
GLenum type
BufferView buffer_view
Matrix GetShaderTransform(const RenderPass &pass) const
Get the vertex shader transform used for drawing this Entity.
Definition: entity.cc:48
static void ComputeVertexData(GlyphAtlasPipeline::VertexShader::PerVertexData *vtx_contents, const std::shared_ptr< TextFrame > &frame, Scalar scale, const Matrix &entity_transform, Vector2 offset, std::optional< GlyphProperties > glyph_properties, const std::shared_ptr< GlyphAtlas > &atlas)
Color GetColor() const
@ kNone
Does not use the index buffer.
@ kBase
The texture is sampled as if it only had a single mipmap level.
constexpr Vector4 ToVector(Color color)
Definition: shader_types.h:190
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:34
@ kNearest
Select nearest to the sample point. Most widely supported.

References buffer_view, ComputeVertexData(), impeller::RenderPass::Draw(), impeller::HostBuffer::EmplaceUniform(), GetColor(), impeller::ContentContext::GetContext(), impeller::ContentContext::GetGlyphAtlasPipeline(), impeller::ContentContext::GetLazyGlyphAtlas(), impeller::Entity::GetShaderClipDepth(), impeller::Entity::GetShaderTransform(), impeller::Entity::GetTransform(), impeller::ContentContext::GetTransientsBuffer(), impeller::Matrix::IsTranslationScaleOnly(), impeller::kBase, 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::RenderPass::SetCommandLabel(), impeller::RenderPass::SetElementCount(), impeller::RenderPass::SetIndexBuffer(), impeller::RenderPass::SetPipeline(), impeller::RenderPass::SetVertexBuffer(), impeller::ToVector(), type, and VALIDATION_LOG.

Referenced by impeller::testing::TEST_P().

◆ SetColor()

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

Definition at line 36 of file text_contents.cc.

36  {
37  color_ = color;
38 }

Referenced by impeller::testing::TEST_P().

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

52  {
53  force_text_color_ = value;
54 }
int32_t value

References value.

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

44  {
45  inherited_opacity_ = opacity;
46 }

◆ SetOffset()

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

Definition at line 48 of file text_contents.cc.

48  {
49  offset_ = offset;
50 }
SeparatedVector2 offset

References offset.

Referenced by impeller::testing::TEST_P().

◆ SetScale()

void impeller::TextContents::SetScale ( Scalar  scale)
inline

Definition at line 58 of file text_contents.h.

58 { scale_ = scale; }
const Scalar scale

References scale.

Referenced by impeller::testing::TEST_P().

◆ SetTextFrame()

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

Definition at line 32 of file text_contents.cc.

32  {
33  frame_ = frame;
34 }

Referenced by impeller::testing::TEST_P().

◆ SetTextProperties()

void impeller::TextContents::SetTextProperties ( Color  color,
bool  stroke,
Scalar  stroke_width,
Cap  stroke_cap,
Join  stroke_join,
Scalar  stroke_miter 
)

Must be set after text frame.

Definition at line 60 of file text_contents.cc.

65  {
66  if (frame_->HasColor()) {
67  // Alpha is always applied when rendering, remove it here so
68  // we do not double-apply the alpha.
69  properties_.color = color.WithAlpha(1.0);
70  }
71  if (stroke) {
72  properties_.stroke = true;
73  properties_.stroke_width = stroke_width;
74  properties_.stroke_cap = stroke_cap;
75  properties_.stroke_join = stroke_join;
76  properties_.stroke_miter = stroke_miter;
77  }
78 }
const Scalar stroke_width

References impeller::GlyphProperties::color, impeller::GlyphProperties::stroke, impeller::GlyphProperties::stroke_cap, impeller::GlyphProperties::stroke_join, impeller::GlyphProperties::stroke_miter, stroke_width, impeller::GlyphProperties::stroke_width, and impeller::Color::WithAlpha().


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