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 {
21 namespace {
22 
23 // TODO(gaaclarke): Investigate if this is still needed for Windows.
24 // On Linux we use FreeType to rasterize glyphs. FreeType does not perform
25 // gamma correction itself during rasterization. Because we render in linear
26 // space, light text on a dark background would look too thin without
27 // correction. To compensate, we calculate a contrast/gamma correction
28 // factor based on the text color's luminance, which is used in the shader
29 // to adjust the glyph's coverage.
30 constexpr bool kPlatformGammaCorrectionDefault =
31 #if FML_OS_LINUX || FML_OS_WIN
32  true;
33 #else
34  false;
35 #endif
36 
37 Point SizeToPoint(Size size) {
38  return Point(size.width, size.height);
39 }
40 } // namespace
41 
44 
46 
47 TextContents::~TextContents() = default;
48 
49 void TextContents::SetTextFrame(const std::shared_ptr<TextFrame>& frame) {
50  frame_ = frame;
51 }
52 
54  color_ = color;
55 }
56 
58  return color_.WithAlpha(color_.alpha * inherited_opacity_);
59 }
60 
62  inherited_opacity_ = opacity;
63 }
64 
66  position_ = position;
67 }
68 
70  screen_transform_ = transform;
71 }
72 
74  force_text_color_ = value;
75 }
76 
77 std::optional<Rect> TextContents::GetCoverage(const Entity& entity) const {
78  return frame_->GetBounds().TransformBounds(entity.GetTransform());
79 }
80 
82  Color color,
83  const std::optional<StrokeParameters>& stroke) {
84  if (frame_->HasColor()) {
85  // Alpha is always applied when rendering, remove it here so
86  // we do not double-apply the alpha.
87  properties_.tone_or_color = color.WithAlpha(1.0);
88  } else {
89  properties_.tone_or_color = GlyphProperties::ComputeTone(color);
90  }
91  properties_.stroke = stroke;
92 }
93 
94 namespace {
95 Scalar AttractToOne(Scalar x) {
96  // Epsilon was decided by looking at the floating point inaccuracies in
97  // the ScaledK test.
98  const Scalar epsilon = 0.005f;
99  if (std::abs(x - 1.f) < epsilon) {
100  return 1.f;
101  }
102  if (std::abs(x + 1.f) < epsilon) {
103  return -1.f;
104  }
105  return x;
106 }
107 
108 } // namespace
109 
110 void TextContents::ComputeVertexData(VS::PerVertexData* vtx_contents,
111  const Matrix& entity_offset_transform,
112  const std::shared_ptr<TextFrame>& frame,
113  Point position,
114  const Matrix& screen_transform,
115  GlyphProperties glyph_properties,
116  const std::shared_ptr<GlyphAtlas>& atlas) {
117  // Common vertex information for all glyphs.
118  // All glyphs are given the same vertex information in the form of a
119  // unit-sized quad. The size of the glyph is specified in per instance data
120  // and the vertex shader uses this to size the glyph correctly. The
121  // interpolated vertex information is also used in the fragment shader to
122  // sample from the glyph atlas.
123 
124  constexpr std::array<Point, 4> unit_points = {Point{0, 0}, Point{1, 0},
125  Point{0, 1}, Point{1, 1}};
126 
127  ISize atlas_size = atlas->GetTexture()->GetSize();
128  bool is_translation_scale = entity_offset_transform.IsTranslationScaleOnly();
129  Matrix basis_transform = entity_offset_transform.Basis();
130 
131  VS::PerVertexData vtx;
132  size_t i = 0u;
133 
134  const Matrix frame_transform =
135  screen_transform * Matrix::MakeTranslation(position);
136  Rational rounded_scale =
138  Scalar inverted_rounded_scale = static_cast<Scalar>(rounded_scale.Invert());
139  Matrix unscaled_basis =
140  basis_transform *
141  Matrix::MakeScale({inverted_rounded_scale, inverted_rounded_scale, 1});
142 
143  // In typical scales < 48x these values should be -1 or 1. We round to
144  // those to avoid inaccuracies.
145  unscaled_basis.m[0] = AttractToOne(unscaled_basis.m[0]);
146  unscaled_basis.m[5] = AttractToOne(unscaled_basis.m[5]);
147 
148  // Compute the device origin of the entire frame.
149  Point screen_offset = (entity_offset_transform * Point(0, 0));
150 
151  for (const TextRun& run : frame->GetRuns()) {
152  const Font& font = run.GetFont();
153  const ScaledFont scaled_font{.font = font, .scale = rounded_scale};
154  const FontGlyphAtlas* font_atlas = atlas->GetFontGlyphAtlas(scaled_font);
155 
156  if (!font_atlas) {
157  VALIDATION_LOG << "Could not find font in the atlas.";
158  // We will not find glyph bounds data for any characters in this run.
159  break;
160  }
161 
162  // Adjust glyph position based on the subpixel rounding used by the font.
163  //
164  // This value is really only used in the is_translation_scale case below,
165  // but that usage appears inside a pair of nested loops so we compute it
166  // once here for the common case for use many times below.
167  // For the other case, this is a fairly quick computation if we are
168  // only doing it just once.
169  Point subpixel_adjustment(0.5, 0.5);
170  switch (font.GetAxisAlignment()) {
172  break;
173  case AxisAlignment::kX:
174  subpixel_adjustment.x = 0.125;
175  break;
176  case AxisAlignment::kY:
177  subpixel_adjustment.y = 0.125;
178  break;
179  case AxisAlignment::kAll:
180  subpixel_adjustment.x = 0.125;
181  subpixel_adjustment.y = 0.125;
182  break;
183  }
184 
185  for (const TextRun::GlyphPosition& glyph_position :
186  run.GetGlyphPositions()) {
188  glyph_position, font.GetAxisAlignment(), frame_transform);
189  SubpixelGlyph subpixel_glyph(glyph_position.glyph, subpixel,
190  glyph_properties);
191  FrameBounds frame_bounds =
192  font_atlas->FindGlyphBounds(subpixel_glyph).value_or(FrameBounds{});
193 
194  // If frame_bounds.is_placeholder is true, either this set of attributes
195  // were not captured by the FirstPass dispatcher or this is the first
196  // frame the glyph has been rendered and so its atlas position was not
197  // known when the glyph was recorded. Perform a slow lookup into the
198  // glyph atlas hash table.
199  if (frame_bounds.is_placeholder) {
200  VALIDATION_LOG << "Frame bounds are not present in the atlas "
201  << font_atlas;
202  continue;
203  }
204 
205  // For each glyph, we compute two rectangles. One for the vertex
206  // positions and one for the texture coordinates (UVs). The atlas
207  // glyph bounds are used to compute UVs in cases where the
208  // destination and source sizes may differ due to clamping the sizes
209  // of large glyphs.
210  Point uv_origin = frame_bounds.atlas_bounds.GetLeftTop() / atlas_size;
211  Point uv_size =
212  SizeToPoint(frame_bounds.atlas_bounds.GetSize()) / atlas_size;
213 
214  for (const Point& point : unit_points) {
215  Point position;
216  if (is_translation_scale) {
217  Point unrounded_glyph_position =
218  // This is for RTL text.
219  unscaled_basis * frame_bounds.glyph_bounds.GetLeftTop() +
220  (basis_transform * glyph_position.position);
221 
222  Point screen_glyph_position =
223  (screen_offset + unrounded_glyph_position + subpixel_adjustment)
224  .Floor();
225  position =
226  (screen_glyph_position +
227  (unscaled_basis * point * frame_bounds.glyph_bounds.GetSize()))
228  .Round();
229  } else {
230  Rect scaled_bounds =
231  frame_bounds.glyph_bounds.Scale(inverted_rounded_scale);
232  position = entity_offset_transform *
233  (glyph_position.position + scaled_bounds.GetLeftTop() +
234  point * scaled_bounds.GetSize());
235  }
236  vtx.uv = uv_origin + (uv_size * point);
237  vtx.position = position;
238  vtx_contents[i++] = vtx;
239  }
240  }
241  }
242 }
243 
245  const Entity& entity,
246  RenderPass& pass) const {
247  Color color = GetColor();
248  if (color.IsTransparent()) {
249  return true;
250  }
251 
252  GlyphAtlas::Type type = frame_->GetAtlasType();
253  const std::shared_ptr<GlyphAtlas>& atlas =
254  renderer.GetLazyGlyphAtlas()->CreateOrGetGlyphAtlas(
255  *renderer.GetContext(), renderer.GetTransientsDataBuffer(), type);
256 
257  if (!atlas || !atlas->IsValid()) {
258  VALIDATION_LOG << "Cannot render glyphs without prepared atlas.";
259  return false;
260  }
261 
262  // Information shared by all glyph draw calls.
263  pass.SetCommandLabel("TextFrame");
264  auto opts = OptionsFromPassAndEntity(pass, entity);
265  opts.primitive_type = PrimitiveType::kTriangle;
266  pass.SetPipeline(renderer.GetGlyphAtlasPipeline(opts));
267 
268  // Common vertex uniforms for all glyphs.
269  VS::FrameInfo frame_info;
270  frame_info.mvp =
272  const Matrix& entity_transform = entity.GetTransform();
273  bool is_translation_scale = entity_transform.IsTranslationScaleOnly();
274 
275  VS::BindFrameInfo(
276  pass, renderer.GetTransientsDataBuffer().EmplaceUniform(frame_info));
277 
278  FS::FragInfo frag_info;
279  frag_info.use_text_color = force_text_color_ ? 1.0 : 0.0;
280  frag_info.text_color = ToVector(color.Premultiply());
281  frag_info.is_color_glyph = type == GlyphAtlas::Type::kColorBitmap;
282  bool enable_gamma_correction = frame_->GetEnableGammaCorrection().value_or(
283  kPlatformGammaCorrectionDefault);
284  if (enable_gamma_correction) {
285  // Calculate relative luminance using Rec. 709 luma coefficients.
286  Scalar luma =
287  color.red * 0.2126f + color.green * 0.7152f + color.blue * 0.0722f;
288  // The contrast/gamma exponent applied in the shader ranges from 1.0 for
289  // black text to 2.2 (standard sRGB gamma) for white text. This interpolates
290  // the exponent based on the text color's luminance.
291  constexpr Scalar kMaxGammaCorrection = 1.2f;
292  frag_info.text_contrast = 1.0f + luma * kMaxGammaCorrection;
293  } else {
294  frag_info.text_contrast = 1.0f;
295  }
296 
297  FS::BindFragInfo(
298  pass, renderer.GetTransientsDataBuffer().EmplaceUniform(frag_info));
299 
300  SamplerDescriptor sampler_desc;
301  if (is_translation_scale) {
302  // When the transform is translation+scale only, we normally use nearest-
303  // neighbor sampling for pixel-perfect text. However, if the X and Y
304  // scales differ significantly (non-uniform / anisotropic scaling, e.g.
305  // Transform.scale(scaleY: 2)), the glyph atlas entry is rasterized at
306  // max(|scaleX|,|scaleY|) uniformly and the compensating unscaled_basis
307  // squeezes one axis, causing a minification. Nearest-neighbor during
308  // minification discards texel columns/rows, producing jagged diagonals
309  // and varying stroke weights. Fall back to bilinear in that case.
310  // See https://github.com/flutter/flutter/issues/182143
311  constexpr Scalar kMinScaleForRatio = 0.001f;
312  constexpr Scalar kAnisotropicScaleThreshold = 1.15f;
313  const Scalar sx = entity_transform.GetBasisX().GetLength();
314  const Scalar sy = entity_transform.GetBasisY().GetLength();
315  const Scalar ratio = (sx > sy) ? sx / std::max(sy, kMinScaleForRatio)
316  : sy / std::max(sx, kMinScaleForRatio);
317  if (ratio > kAnisotropicScaleThreshold) {
318  // Non-uniform scale — use bilinear to avoid aliasing.
319  sampler_desc.min_filter = MinMagFilter::kLinear;
320  sampler_desc.mag_filter = MinMagFilter::kLinear;
321  } else {
322  sampler_desc.min_filter = MinMagFilter::kNearest;
323  sampler_desc.mag_filter = MinMagFilter::kNearest;
324  }
325  } else {
326  // Currently, we only propagate the scale of the transform to the atlas
327  // renderer, so if the transform has more than just a translation, we turn
328  // on linear sampling to prevent crunchiness caused by the pixel grid not
329  // being perfectly aligned.
330  // The downside is that this slightly over-blurs rotated/skewed text.
331  sampler_desc.min_filter = MinMagFilter::kLinear;
332  sampler_desc.mag_filter = MinMagFilter::kLinear;
333  }
334 
335  // No mipmaps for glyph atlas (glyphs are generated at exact scales).
336  sampler_desc.mip_filter = MipFilter::kBase;
337 
338  FS::BindGlyphAtlasSampler(
339  pass, // command
340  atlas->GetTexture(), // texture
341  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
342  sampler_desc) // sampler
343  );
344 
345  HostBuffer& data_host_buffer = renderer.GetTransientsDataBuffer();
346  HostBuffer& indexes_host_buffer = renderer.GetTransientsIndexesBuffer();
347  size_t glyph_count = 0;
348  for (const auto& run : frame_->GetRuns()) {
349  glyph_count += run.GetGlyphPositions().size();
350  }
351  size_t vertex_count = glyph_count * 4;
352  size_t index_count = glyph_count * 6;
353 
354  BufferView buffer_view = data_host_buffer.Emplace(
355  vertex_count * sizeof(VS::PerVertexData), alignof(VS::PerVertexData),
356  [&](uint8_t* data) {
357  VS::PerVertexData* vtx_contents =
358  reinterpret_cast<VS::PerVertexData*>(data);
359  ComputeVertexData(/*vtx_contents=*/vtx_contents,
360  /*entity_transform=*/entity.GetTransform(),
361  /*frame=*/frame_,
362  /*position=*/position_,
363  /*screen_transform=*/screen_transform_,
364  /*glyph_properties=*/properties_,
365  /*atlas=*/atlas);
366  });
367  BufferView index_buffer_view = indexes_host_buffer.Emplace(
368  index_count * sizeof(uint16_t), alignof(uint16_t), [&](uint8_t* data) {
369  uint16_t* indices = reinterpret_cast<uint16_t*>(data);
370  size_t j = 0;
371  for (auto i = 0u; i < glyph_count; i++) {
372  size_t base = i * 4;
373  indices[j++] = base + 0;
374  indices[j++] = base + 1;
375  indices[j++] = base + 2;
376  indices[j++] = base + 1;
377  indices[j++] = base + 2;
378  indices[j++] = base + 3;
379  }
380  });
381 
382  pass.SetVertexBuffer(std::move(buffer_view));
383  pass.SetIndexBuffer(index_buffer_view, IndexType::k16bit);
384  pass.SetElementCount(index_count);
385 
386  return pass.Draw().ok();
387 }
388 
389 } // 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
static void ComputeVertexData(GlyphAtlasPipeline::VertexShader::PerVertexData *vtx_contents, const Matrix &entity_transform, const std::shared_ptr< TextFrame > &frame, Point position, const Matrix &screen_transform, 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 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.
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.
constexpr Vector4 ToVector(Color color)
Definition: shader_types.h:411
TSize< Scalar > Size
Definition: size.h:159
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.
Scalar blue
Definition: color.h:138
Scalar alpha
Definition: color.h:143
constexpr bool IsTransparent() const
Definition: color.h:897
constexpr Color WithAlpha(Scalar new_alpha) const
Definition: color.h:283
Scalar red
Definition: color.h:128
Scalar green
Definition: color.h:133
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
static Tone ComputeTone(const Color &c)
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:361
constexpr TRect Scale(Type scale) const
Definition: rect.h:226
constexpr TPoint< T > GetLeftTop() const
Definition: rect.h:393
Scalar GetLength() const
Definition: vector.h:47
#define VALIDATION_LOG
Definition: validation.h:91