Flutter Impeller
text_contents.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
6 
7 #include <cstring>
8 #include <optional>
9 #include <utility>
10 
12 #include "impeller/core/formats.h"
14 #include "impeller/entity/entity.h"
19 
20 namespace impeller {
22  return Point(size.width, size.height);
23 }
24 
27 
28 TextContents::TextContents() = default;
29 
30 TextContents::~TextContents() = default;
31 
32 void TextContents::SetTextFrame(const std::shared_ptr<TextFrame>& frame) {
33  frame_ = frame;
34 }
35 
37  color_ = color;
38 }
39 
41  return color_.WithAlpha(color_.alpha * inherited_opacity_);
42 }
43 
45  inherited_opacity_ = opacity;
46 }
47 
49  position_ = position;
50 }
51 
53  screen_transform_ = transform;
54 }
55 
57  force_text_color_ = value;
58 }
59 
60 std::optional<Rect> TextContents::GetCoverage(const Entity& entity) const {
61  const Matrix entity_offset_transform =
62  entity.GetTransform() * Matrix::MakeTranslation(position_);
63  return frame_->GetBounds().TransformBounds(entity_offset_transform);
64 }
65 
67  Color color,
68  const std::optional<StrokeParameters>& stroke) {
69  if (frame_->HasColor()) {
70  // Alpha is always applied when rendering, remove it here so
71  // we do not double-apply the alpha.
72  properties_.color = color.WithAlpha(1.0);
73  }
74  properties_.stroke = stroke;
75 }
76 
77 namespace {
78 Scalar AttractToOne(Scalar x) {
79  // Epsilon was decided by looking at the floating point inaccuracies in
80  // the ScaledK test.
81  const Scalar epsilon = 0.005f;
82  if (std::abs(x - 1.f) < epsilon) {
83  return 1.f;
84  }
85  if (std::abs(x + 1.f) < epsilon) {
86  return -1.f;
87  }
88  return x;
89 }
90 
91 } // namespace
92 
94  VS::PerVertexData* vtx_contents,
95  const Matrix& entity_transform,
96  const std::shared_ptr<TextFrame>& frame,
97  Point position,
98  const Matrix& screen_transform,
99  std::optional<GlyphProperties> glyph_properties,
100  const std::shared_ptr<GlyphAtlas>& atlas) {
101  // Common vertex information for all glyphs.
102  // All glyphs are given the same vertex information in the form of a
103  // unit-sized quad. The size of the glyph is specified in per instance data
104  // and the vertex shader uses this to size the glyph correctly. The
105  // interpolated vertex information is also used in the fragment shader to
106  // sample from the glyph atlas.
107 
108  constexpr std::array<Point, 4> unit_points = {Point{0, 0}, Point{1, 0},
109  Point{0, 1}, Point{1, 1}};
110 
111  Matrix entity_offset_transform =
112  entity_transform * Matrix::MakeTranslation(position);
113 
114  ISize atlas_size = atlas->GetTexture()->GetSize();
115  bool is_translation_scale = entity_offset_transform.IsTranslationScaleOnly();
116  Matrix basis_transform = entity_offset_transform.Basis();
117 
118  VS::PerVertexData vtx;
119  size_t i = 0u;
120 
121  const Matrix frame_transform =
122  screen_transform * Matrix::MakeTranslation(position);
123  Rational rounded_scale =
125  Scalar inverted_rounded_scale = static_cast<Scalar>(rounded_scale.Invert());
126  Matrix unscaled_basis =
127  basis_transform *
128  Matrix::MakeScale({inverted_rounded_scale, inverted_rounded_scale, 1});
129 
130  // In typical scales < 48x these values should be -1 or 1. We round to
131  // those to avoid inaccuracies.
132  unscaled_basis.m[0] = AttractToOne(unscaled_basis.m[0]);
133  unscaled_basis.m[5] = AttractToOne(unscaled_basis.m[5]);
134 
135  // Compute the device origin of the entire frame.
136  Point screen_offset = (entity_offset_transform * Point(0, 0));
137 
138  for (const TextRun& run : frame->GetRuns()) {
139  const Font& font = run.GetFont();
140  const ScaledFont scaled_font{.font = font, .scale = rounded_scale};
141  const FontGlyphAtlas* font_atlas = atlas->GetFontGlyphAtlas(scaled_font);
142 
143  if (!font_atlas) {
144  VALIDATION_LOG << "Could not find font in the atlas.";
145  // We will not find glyph bounds data for any characters in this run.
146  break;
147  }
148 
149  // Adjust glyph position based on the subpixel rounding used by the font.
150  //
151  // This value is really only used in the is_translation_scale case below,
152  // but that usage appears inside a pair of nested loops so we compute it
153  // once here for the common case for use many times below.
154  // For the other case, this is a fairly quick computation if we are
155  // only doing it just once.
156  Point subpixel_adjustment(0.5, 0.5);
157  switch (font.GetAxisAlignment()) {
159  break;
160  case AxisAlignment::kX:
161  subpixel_adjustment.x = 0.125;
162  break;
163  case AxisAlignment::kY:
164  subpixel_adjustment.y = 0.125;
165  break;
166  case AxisAlignment::kAll:
167  subpixel_adjustment.x = 0.125;
168  subpixel_adjustment.y = 0.125;
169  break;
170  }
171 
172  for (const TextRun::GlyphPosition& glyph_position :
173  run.GetGlyphPositions()) {
175  glyph_position, font.GetAxisAlignment(), frame_transform);
176  SubpixelGlyph subpixel_glyph(glyph_position.glyph, subpixel,
177  glyph_properties);
178  FrameBounds frame_bounds =
179  font_atlas->FindGlyphBounds(subpixel_glyph).value_or(FrameBounds{});
180 
181  // If frame_bounds.is_placeholder is true, either this set of attributes
182  // were not captured by the FirstPass dispatcher or this is the first
183  // frame the glyph has been rendered and so its atlas position was not
184  // known when the glyph was recorded. Perform a slow lookup into the
185  // glyph atlas hash table.
186  if (frame_bounds.is_placeholder) {
187  VALIDATION_LOG << "Frame bounds are not present in the atlas "
188  << font_atlas;
189  continue;
190  }
191 
192  // For each glyph, we compute two rectangles. One for the vertex
193  // positions and one for the texture coordinates (UVs). The atlas
194  // glyph bounds are used to compute UVs in cases where the
195  // destination and source sizes may differ due to clamping the sizes
196  // of large glyphs.
197  Point uv_origin = frame_bounds.atlas_bounds.GetLeftTop() / atlas_size;
198  Point uv_size =
199  SizeToPoint(frame_bounds.atlas_bounds.GetSize()) / atlas_size;
200 
201  for (const Point& point : unit_points) {
202  Point position;
203  if (is_translation_scale) {
204  Point unrounded_glyph_position =
205  // This is for RTL text.
206  unscaled_basis * frame_bounds.glyph_bounds.GetLeftTop() +
207  (basis_transform * glyph_position.position);
208 
209  Point screen_glyph_position =
210  (screen_offset + unrounded_glyph_position + subpixel_adjustment)
211  .Floor();
212  position =
213  (screen_glyph_position +
214  (unscaled_basis * point * frame_bounds.glyph_bounds.GetSize()))
215  .Round();
216  } else {
217  Rect scaled_bounds =
218  frame_bounds.glyph_bounds.Scale(inverted_rounded_scale);
219  position = entity_offset_transform *
220  (glyph_position.position + scaled_bounds.GetLeftTop() +
221  point * scaled_bounds.GetSize());
222  }
223  vtx.uv = uv_origin + (uv_size * point);
224  vtx.position = position;
225  vtx_contents[i++] = vtx;
226  }
227  }
228  }
229 }
230 
232  const Entity& entity,
233  RenderPass& pass) const {
234  Color color = GetColor();
235  if (color.IsTransparent()) {
236  return true;
237  }
238 
239  GlyphAtlas::Type type = frame_->GetAtlasType();
240  const std::shared_ptr<GlyphAtlas>& atlas =
241  renderer.GetLazyGlyphAtlas()->CreateOrGetGlyphAtlas(
242  *renderer.GetContext(), renderer.GetTransientsDataBuffer(), type);
243 
244  if (!atlas || !atlas->IsValid()) {
245  VALIDATION_LOG << "Cannot render glyphs without prepared atlas.";
246  return false;
247  }
248 
249  // Information shared by all glyph draw calls.
250  pass.SetCommandLabel("TextFrame");
251  auto opts = OptionsFromPassAndEntity(pass, entity);
252  opts.primitive_type = PrimitiveType::kTriangle;
253  pass.SetPipeline(renderer.GetGlyphAtlasPipeline(opts));
254 
255  // Common vertex uniforms for all glyphs.
256  VS::FrameInfo frame_info;
257  frame_info.mvp =
259  const Matrix& entity_transform = entity.GetTransform();
260  bool is_translation_scale = entity_transform.IsTranslationScaleOnly();
261 
262  VS::BindFrameInfo(
263  pass, renderer.GetTransientsDataBuffer().EmplaceUniform(frame_info));
264 
265  FS::FragInfo frag_info;
266  frag_info.use_text_color = force_text_color_ ? 1.0 : 0.0;
267  frag_info.text_color = ToVector(color.Premultiply());
268  frag_info.is_color_glyph = type == GlyphAtlas::Type::kColorBitmap;
269 
270  FS::BindFragInfo(
271  pass, renderer.GetTransientsDataBuffer().EmplaceUniform(frag_info));
272 
273  SamplerDescriptor sampler_desc;
274  if (is_translation_scale) {
275  // When the transform is translation+scale only, we normally use nearest-
276  // neighbor sampling for pixel-perfect text. However, if the X and Y
277  // scales differ significantly (non-uniform / anisotropic scaling, e.g.
278  // Transform.scale(scaleY: 2)), the glyph atlas entry is rasterized at
279  // max(|scaleX|,|scaleY|) uniformly and the compensating unscaled_basis
280  // squeezes one axis, causing a minification. Nearest-neighbor during
281  // minification discards texel columns/rows, producing jagged diagonals
282  // and varying stroke weights. Fall back to bilinear in that case.
283  // See https://github.com/flutter/flutter/issues/182143
284  constexpr Scalar kMinScaleForRatio = 0.001f;
285  constexpr Scalar kAnisotropicScaleThreshold = 1.15f;
286  const Scalar sx = entity_transform.GetBasisX().GetLength();
287  const Scalar sy = entity_transform.GetBasisY().GetLength();
288  const Scalar ratio = (sx > sy) ? sx / std::max(sy, kMinScaleForRatio)
289  : sy / std::max(sx, kMinScaleForRatio);
290  if (ratio > kAnisotropicScaleThreshold) {
291  // Non-uniform scale — use bilinear to avoid aliasing.
292  sampler_desc.min_filter = MinMagFilter::kLinear;
293  sampler_desc.mag_filter = MinMagFilter::kLinear;
294  } else {
295  sampler_desc.min_filter = MinMagFilter::kNearest;
296  sampler_desc.mag_filter = MinMagFilter::kNearest;
297  }
298  } else {
299  // Currently, we only propagate the scale of the transform to the atlas
300  // renderer, so if the transform has more than just a translation, we turn
301  // on linear sampling to prevent crunchiness caused by the pixel grid not
302  // being perfectly aligned.
303  // The downside is that this slightly over-blurs rotated/skewed text.
304  sampler_desc.min_filter = MinMagFilter::kLinear;
305  sampler_desc.mag_filter = MinMagFilter::kLinear;
306  }
307 
308  // No mipmaps for glyph atlas (glyphs are generated at exact scales).
309  sampler_desc.mip_filter = MipFilter::kBase;
310 
311  FS::BindGlyphAtlasSampler(
312  pass, // command
313  atlas->GetTexture(), // texture
314  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
315  sampler_desc) // sampler
316  );
317 
318  HostBuffer& data_host_buffer = renderer.GetTransientsDataBuffer();
319  HostBuffer& indexes_host_buffer = renderer.GetTransientsIndexesBuffer();
320  size_t glyph_count = 0;
321  for (const auto& run : frame_->GetRuns()) {
322  glyph_count += run.GetGlyphPositions().size();
323  }
324  size_t vertex_count = glyph_count * 4;
325  size_t index_count = glyph_count * 6;
326 
327  BufferView buffer_view = data_host_buffer.Emplace(
328  vertex_count * sizeof(VS::PerVertexData), alignof(VS::PerVertexData),
329  [&](uint8_t* data) {
330  VS::PerVertexData* vtx_contents =
331  reinterpret_cast<VS::PerVertexData*>(data);
332  ComputeVertexData(/*vtx_contents=*/vtx_contents,
333  /*entity_transform=*/entity.GetTransform(),
334  /*frame=*/frame_,
335  /*position=*/position_,
336  /*screen_transform=*/screen_transform_,
337  /*glyph_properties=*/GetGlyphProperties(),
338  /*atlas=*/atlas);
339  });
340  BufferView index_buffer_view = indexes_host_buffer.Emplace(
341  index_count * sizeof(uint16_t), alignof(uint16_t), [&](uint8_t* data) {
342  uint16_t* indices = reinterpret_cast<uint16_t*>(data);
343  size_t j = 0;
344  for (auto i = 0u; i < glyph_count; i++) {
345  size_t base = i * 4;
346  indices[j++] = base + 0;
347  indices[j++] = base + 1;
348  indices[j++] = base + 2;
349  indices[j++] = base + 1;
350  indices[j++] = base + 2;
351  indices[j++] = base + 3;
352  }
353  });
354 
355  pass.SetVertexBuffer(std::move(buffer_view));
356  pass.SetIndexBuffer(index_buffer_view, IndexType::k16bit);
357  pass.SetElementCount(index_count);
358 
359  return pass.Draw().ok();
360 }
361 
362 std::optional<GlyphProperties> TextContents::GetGlyphProperties() const {
363  return (properties_.stroke || frame_->HasColor())
364  ? std::optional<GlyphProperties>(properties_)
365  : std::nullopt;
366 }
367 
368 } // namespace impeller
const std::shared_ptr< LazyGlyphAtlas > & GetLazyGlyphAtlas() const
HostBuffer & GetTransientsIndexesBuffer() const
Retrieve the current host buffer for transient storage of indexes used for indexed draws.
HostBuffer & GetTransientsDataBuffer() const
Retrieve the current host buffer for transient storage of other non-index data.
PipelineRef GetGlyphAtlasPipeline(ContentContextOptions opts) const
std::shared_ptr< Context > GetContext() const
Matrix GetShaderTransform(const RenderPass &pass) const
Definition: entity.cc:50
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:46
float GetShaderClipDepth() const
Definition: entity.cc:90
An object that can look up glyph locations within the GlyphAtlas for a particular typeface.
Definition: glyph_atlas.h:244
std::optional< FrameBounds > FindGlyphBounds(const SubpixelGlyph &glyph) const
Find the location of a glyph in the atlas.
Definition: glyph_atlas.cc:140
Describes a typeface along with any modifications to its intrinsic properties.
Definition: font.h:36
AxisAlignment GetAxisAlignment() const
Definition: font.cc:41
Type
Describes how the glyphs are represented in the texture.
Definition: glyph_atlas.h:41
BufferView Emplace(const BufferType &buffer, size_t alignment=0)
Emplace non-uniform data (like contiguous vertices) onto the host buffer.
Definition: host_buffer.h:92
BufferView EmplaceUniform(const UniformType &uniform)
Emplace uniform data onto the host buffer. Ensure that backend specific uniform alignment requirement...
Definition: host_buffer.h:47
Rational Invert() const
Definition: rational.cc:46
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:30
virtual bool SetVertexBuffer(VertexBuffer buffer)
Specify the vertex and index buffer to use for this command.
Definition: render_pass.cc:127
virtual bool SetIndexBuffer(BufferView index_buffer, IndexType index_type)
Specify an index buffer to use for this command. To unset the index buffer, pass IndexType::kNone to ...
Definition: render_pass.cc:164
virtual void SetPipeline(PipelineRef pipeline)
The pipeline to use for this command.
Definition: render_pass.cc:86
virtual fml::Status Draw()
Record the currently pending command.
Definition: render_pass.cc:208
virtual void SetElementCount(size_t count)
Definition: render_pass.cc:119
virtual void SetCommandLabel(std::string_view label)
The debugging label to use for the command.
Definition: render_pass.cc:97
FragmentShader_ FragmentShader
Definition: pipeline.h:169
void SetInheritedOpacity(Scalar opacity) override
Inherit the provided opacity.
void SetForceTextColor(bool value)
Force the text color to apply to the rendered glyphs, even if those glyphs are bitmaps.
void SetTextProperties(Color color, const std::optional< StrokeParameters > &stroke)
Must be set after text frame.
static void ComputeVertexData(GlyphAtlasPipeline::VertexShader::PerVertexData *vtx_contents, const Matrix &entity_transform, const std::shared_ptr< TextFrame > &frame, Point position, const Matrix &screen_transform, std::optional< GlyphProperties > glyph_properties, const std::shared_ptr< GlyphAtlas > &atlas)
Computes the vertex data for the render operation from a collection of data drawn from the DrawTextFr...
void SetPosition(Point position)
void SetScreenTransform(const Matrix &transform)
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
std::optional< Rect > GetCoverage(const Entity &entity) const override
Get the area of the render pass that will be affected when this contents is rendered.
void SetTextFrame(const std::shared_ptr< TextFrame > &frame)
void SetColor(Color color)
Color GetColor() const
static Rational RoundScaledFontSize(Scalar scale)
Definition: text_frame.cc:55
static SubpixelPosition ComputeSubpixelPosition(const TextRun::GlyphPosition &glyph_position, AxisAlignment alignment, const Matrix &transform)
Definition: text_frame.cc:94
Represents a collection of positioned glyphs from a specific font.
Definition: text_run.h:20
int32_t value
int32_t x
float Scalar
Definition: scalar.h:19
TPoint< Scalar > Point
Definition: point.h:426
LinePipeline::FragmentShader FS
@ kBase
The texture is sampled as if it only had a single mipmap level.
Point SizeToPoint(Size size)
constexpr Vector4 ToVector(Color color)
Definition: shader_types.h:203
LinePipeline::VertexShader VS
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:34
@ kNearest
Select nearest to the sample point. Most widely supported.
Definition: comparable.h:93
Scalar alpha
Definition: color.h:143
constexpr bool IsTransparent() const
Definition: color.h:892
constexpr Color WithAlpha(Scalar new_alpha) const
Definition: color.h:278
constexpr Color Premultiply() const
Definition: color.h:212
Rect atlas_bounds
The bounds of the glyph within the glyph atlas texture.
Definition: glyph_atlas.h:24
Rect glyph_bounds
The glyph bounds within the local coordinate system.
Definition: glyph_atlas.h:26
std::optional< StrokeParameters > stroke
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
static constexpr Matrix MakeTranslation(const Vector3 &t)
Definition: matrix.h:95
constexpr Vector3 GetBasisY() const
Definition: matrix.h:390
constexpr bool IsTranslationScaleOnly() const
Returns true if the matrix has a scale-only basis and is non-projective. Note that an identity matrix...
Definition: matrix.h:501
Scalar m[16]
Definition: matrix.h:39
constexpr Matrix Basis() const
The Matrix without its w components (without translation).
Definition: matrix.h:239
static constexpr Matrix MakeScale(const Vector3 &s)
Definition: matrix.h:104
constexpr Vector3 GetBasisX() const
Definition: matrix.h:388
Scalar GetMaxBasisLengthXY() const
Return the maximum scale applied specifically to either the X axis or Y axis unit vectors (the bases)...
Definition: matrix.h:328
A font and a scale. Used as a key that represents a typeface within a glyph atlas.
A glyph and its subpixel position.
static constexpr TPoint Round(const TPoint< U > &other)
Definition: point.h:50
constexpr TSize< Type > GetSize() const
Returns the size of the rectangle which may be negative in either width or height and may have been c...
Definition: rect.h:327
constexpr TRect Scale(Type scale) const
Definition: rect.h:202
constexpr TPoint< T > GetLeftTop() const
Definition: rect.h:359
Type height
Definition: size.h:29
Type width
Definition: size.h:28
Scalar GetLength() const
Definition: vector.h:47
#define VALIDATION_LOG
Definition: validation.h:91