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  offset_ = offset;
50 }
51 
53  force_text_color_ = value;
54 }
55 
56 std::optional<Rect> TextContents::GetCoverage(const Entity& entity) const {
57  return frame_->GetBounds().TransformBounds(entity.GetTransform());
58 }
59 
61  Color color,
62  const std::optional<StrokeParameters>& stroke) {
63  if (frame_->HasColor()) {
64  // Alpha is always applied when rendering, remove it here so
65  // we do not double-apply the alpha.
66  properties_.color = color.WithAlpha(1.0);
67  }
68  properties_.stroke = stroke;
69 }
70 
71 namespace {
72 Scalar AttractToOne(Scalar x) {
73  // Epsilon was decided by looking at the floating point inaccuracies in
74  // the ScaledK test.
75  const Scalar epsilon = 0.005f;
76  if (std::abs(x - 1.f) < epsilon) {
77  return 1.f;
78  }
79  if (std::abs(x + 1.f) < epsilon) {
80  return -1.f;
81  }
82  return x;
83 }
84 
85 } // namespace
86 
88  VS::PerVertexData* vtx_contents,
89  const std::shared_ptr<TextFrame>& frame,
90  Scalar scale,
91  const Matrix& entity_transform,
92  Vector2 offset,
93  std::optional<GlyphProperties> glyph_properties,
94  const std::shared_ptr<GlyphAtlas>& atlas) {
95  // Common vertex information for all glyphs.
96  // All glyphs are given the same vertex information in the form of a
97  // unit-sized quad. The size of the glyph is specified in per instance data
98  // and the vertex shader uses this to size the glyph correctly. The
99  // interpolated vertex information is also used in the fragment shader to
100  // sample from the glyph atlas.
101 
102  constexpr std::array<Point, 4> unit_points = {Point{0, 0}, Point{1, 0},
103  Point{0, 1}, Point{1, 1}};
104 
105  ISize atlas_size = atlas->GetTexture()->GetSize();
106  bool is_translation_scale = entity_transform.IsTranslationScaleOnly();
107  Matrix basis_transform = entity_transform.Basis();
108 
109  VS::PerVertexData vtx;
110  size_t i = 0u;
111  size_t bounds_offset = 0u;
112  Rational rounded_scale = frame->GetScale();
113  Scalar inverted_rounded_scale = static_cast<Scalar>(rounded_scale.Invert());
114  Matrix unscaled_basis =
115  basis_transform *
116  Matrix::MakeScale({inverted_rounded_scale, inverted_rounded_scale, 1});
117 
118  // In typical scales < 48x these values should be -1 or 1. We round to
119  // those to avoid inaccuracies.
120  unscaled_basis.m[0] = AttractToOne(unscaled_basis.m[0]);
121  unscaled_basis.m[5] = AttractToOne(unscaled_basis.m[5]);
122 
123  for (const TextRun& run : frame->GetRuns()) {
124  const Font& font = run.GetFont();
125  const Matrix transform = frame->GetOffsetTransform();
126  FontGlyphAtlas* font_atlas = nullptr;
127 
128  // Adjust glyph position based on the subpixel rounding
129  // used by the font.
130  Point subpixel_adjustment(0.5, 0.5);
131  switch (font.GetAxisAlignment()) {
133  break;
134  case AxisAlignment::kX:
135  subpixel_adjustment.x = 0.125;
136  break;
137  case AxisAlignment::kY:
138  subpixel_adjustment.y = 0.125;
139  break;
140  case AxisAlignment::kAll:
141  subpixel_adjustment.x = 0.125;
142  subpixel_adjustment.y = 0.125;
143  break;
144  }
145 
146  Point screen_offset = (entity_transform * Point(0, 0));
147  for (const TextRun::GlyphPosition& glyph_position :
148  run.GetGlyphPositions()) {
149  const FrameBounds& frame_bounds = frame->GetFrameBounds(bounds_offset);
150  bounds_offset++;
151  auto atlas_glyph_bounds = frame_bounds.atlas_bounds;
152  auto glyph_bounds = frame_bounds.glyph_bounds;
153 
154  // If frame_bounds.is_placeholder is true, this is the first frame
155  // the glyph has been rendered and so its atlas position was not
156  // known when the glyph was recorded. Perform a slow lookup into the
157  // glyph atlas hash table.
158  if (frame_bounds.is_placeholder) {
159  if (!font_atlas) {
160  font_atlas =
161  atlas->GetOrCreateFontGlyphAtlas(ScaledFont{font, rounded_scale});
162  }
163 
164  if (!font_atlas) {
165  VALIDATION_LOG << "Could not find font in the atlas.";
166  continue;
167  }
169  glyph_position, font.GetAxisAlignment(), transform);
170 
171  std::optional<FrameBounds> maybe_atlas_glyph_bounds =
172  font_atlas->FindGlyphBounds(SubpixelGlyph{
173  glyph_position.glyph, //
174  subpixel, //
175  glyph_properties //
176  });
177  if (!maybe_atlas_glyph_bounds.has_value()) {
178  VALIDATION_LOG << "Could not find glyph position in the atlas.";
179  continue;
180  }
181  atlas_glyph_bounds = maybe_atlas_glyph_bounds.value().atlas_bounds;
182  }
183 
184  Rect scaled_bounds = glyph_bounds.Scale(inverted_rounded_scale);
185  // For each glyph, we compute two rectangles. One for the vertex
186  // positions and one for the texture coordinates (UVs). The atlas
187  // glyph bounds are used to compute UVs in cases where the
188  // destination and source sizes may differ due to clamping the sizes
189  // of large glyphs.
190  Point uv_origin = atlas_glyph_bounds.GetLeftTop() / atlas_size;
191  Point uv_size = SizeToPoint(atlas_glyph_bounds.GetSize()) / atlas_size;
192 
193  Point unrounded_glyph_position =
194  // This is for RTL text.
195  unscaled_basis * glyph_bounds.GetLeftTop() +
196  (basis_transform * glyph_position.position);
197 
198  Point screen_glyph_position =
199  (screen_offset + unrounded_glyph_position + subpixel_adjustment)
200  .Floor();
201  for (const Point& point : unit_points) {
202  Point position;
203  if (is_translation_scale) {
204  position = (screen_glyph_position +
205  (unscaled_basis * point * glyph_bounds.GetSize()))
206  .Round();
207  } else {
208  position = entity_transform *
209  (glyph_position.position + scaled_bounds.GetLeftTop() +
210  point * scaled_bounds.GetSize());
211  }
212  vtx.uv = uv_origin + (uv_size * point);
213  vtx.position = position;
214  vtx_contents[i++] = vtx;
215  }
216  }
217  }
218 }
219 
221  const Entity& entity,
222  RenderPass& pass) const {
223  Color color = GetColor();
224  if (color.IsTransparent()) {
225  return true;
226  }
227 
228  GlyphAtlas::Type type = frame_->GetAtlasType();
229  const std::shared_ptr<GlyphAtlas>& atlas =
230  renderer.GetLazyGlyphAtlas()->CreateOrGetGlyphAtlas(
231  *renderer.GetContext(), renderer.GetTransientsBuffer(), type);
232 
233  if (!atlas || !atlas->IsValid()) {
234  VALIDATION_LOG << "Cannot render glyphs without prepared atlas.";
235  return false;
236  }
237  if (!frame_->IsFrameComplete()) {
238  VALIDATION_LOG << "Failed to find font glyph bounds.";
239  return false;
240  }
241 
242  // Information shared by all glyph draw calls.
243  pass.SetCommandLabel("TextFrame");
244  auto opts = OptionsFromPassAndEntity(pass, entity);
245  opts.primitive_type = PrimitiveType::kTriangle;
246  pass.SetPipeline(renderer.GetGlyphAtlasPipeline(opts));
247 
248  // Common vertex uniforms for all glyphs.
249  VS::FrameInfo frame_info;
250  frame_info.mvp =
252  bool is_translation_scale = entity.GetTransform().IsTranslationScaleOnly();
253  Matrix entity_transform = entity.GetTransform();
254 
255  VS::BindFrameInfo(pass,
256  renderer.GetTransientsBuffer().EmplaceUniform(frame_info));
257 
258  FS::FragInfo frag_info;
259  frag_info.use_text_color = force_text_color_ ? 1.0 : 0.0;
260  frag_info.text_color = ToVector(color.Premultiply());
261  frag_info.is_color_glyph = type == GlyphAtlas::Type::kColorBitmap;
262 
263  FS::BindFragInfo(pass,
264  renderer.GetTransientsBuffer().EmplaceUniform(frag_info));
265 
266  SamplerDescriptor sampler_desc;
267  if (is_translation_scale) {
268  sampler_desc.min_filter = MinMagFilter::kNearest;
269  sampler_desc.mag_filter = MinMagFilter::kNearest;
270  } else {
271  // Currently, we only propagate the scale of the transform to the atlas
272  // renderer, so if the transform has more than just a translation, we turn
273  // on linear sampling to prevent crunchiness caused by the pixel grid not
274  // being perfectly aligned.
275  // The downside is that this slightly over-blurs rotated/skewed text.
276  sampler_desc.min_filter = MinMagFilter::kLinear;
277  sampler_desc.mag_filter = MinMagFilter::kLinear;
278  }
279 
280  // No mipmaps for glyph atlas (glyphs are generated at exact scales).
281  sampler_desc.mip_filter = MipFilter::kBase;
282 
283  FS::BindGlyphAtlasSampler(
284  pass, // command
285  atlas->GetTexture(), // texture
286  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
287  sampler_desc) // sampler
288  );
289 
290  HostBuffer& host_buffer = renderer.GetTransientsBuffer();
291  size_t glyph_count = 0;
292  for (const auto& run : frame_->GetRuns()) {
293  glyph_count += run.GetGlyphPositions().size();
294  }
295  size_t vertex_count = glyph_count * 4;
296  size_t index_count = glyph_count * 6;
297 
298  BufferView buffer_view = host_buffer.Emplace(
299  vertex_count * sizeof(VS::PerVertexData), alignof(VS::PerVertexData),
300  [&](uint8_t* data) {
301  VS::PerVertexData* vtx_contents =
302  reinterpret_cast<VS::PerVertexData*>(data);
303  ComputeVertexData(/*vtx_contents=*/vtx_contents,
304  /*frame=*/frame_,
305  /*scale=*/scale_,
306  /*entity_transform=*/entity_transform,
307  /*offset=*/offset_,
308  /*glyph_properties=*/GetGlyphProperties(),
309  /*atlas=*/atlas);
310  });
311  BufferView index_buffer_view = host_buffer.Emplace(
312  index_count * sizeof(uint16_t), alignof(uint16_t), [&](uint8_t* data) {
313  uint16_t* indices = reinterpret_cast<uint16_t*>(data);
314  size_t j = 0;
315  for (auto i = 0u; i < glyph_count; i++) {
316  size_t base = i * 4;
317  indices[j++] = base + 0;
318  indices[j++] = base + 1;
319  indices[j++] = base + 2;
320  indices[j++] = base + 1;
321  indices[j++] = base + 2;
322  indices[j++] = base + 3;
323  }
324  });
325 
326  pass.SetVertexBuffer(std::move(buffer_view));
327  pass.SetIndexBuffer(index_buffer_view, IndexType::k16bit);
328  pass.SetElementCount(index_count);
329 
330  return pass.Draw().ok();
331 }
332 
333 std::optional<GlyphProperties> TextContents::GetGlyphProperties() const {
334  return (properties_.stroke || frame_->HasColor())
335  ? std::optional<GlyphProperties>(properties_)
336  : std::nullopt;
337 }
338 
339 } // namespace impeller
GLenum type
BufferView buffer_view
HostBuffer & GetTransientsBuffer() const
Retrieve the currnent host buffer for transient storage.
const std::shared_ptr< LazyGlyphAtlas > & GetLazyGlyphAtlas() const
PipelineRef GetGlyphAtlasPipeline(ContentContextOptions opts) const
std::shared_ptr< Context > GetContext() const
Matrix GetShaderTransform(const RenderPass &pass) const
Definition: entity.cc:48
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:44
float GetShaderClipDepth() const
Definition: entity.cc:88
An object that can look up glyph locations within the GlyphAtlas for a particular typeface.
Definition: glyph_atlas.h:269
std::optional< FrameBounds > FindGlyphBounds(const SubpixelGlyph &glyph) const
Find the location of a glyph in the atlas.
Definition: glyph_atlas.cc:131
Describes a typeface along with any modifications to its intrinsic properties.
Definition: font.h:35
AxisAlignment GetAxisAlignment() const
Definition: font.cc:41
Type
Describes how the glyphs are represented in the texture.
Definition: glyph_atlas.h:74
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:50
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:164
void SetOffset(Vector2 offset)
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.
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)
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
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:327
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:190
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:95
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.
Definition: glyph_atlas.h:57
Rect glyph_bounds
The local glyph bounds.
Definition: glyph_atlas.h:59
std::optional< StrokeParameters > stroke
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
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:440
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
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:49
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:331
constexpr TRect Scale(Type scale) const
Definition: rect.h:206
constexpr TPoint< T > GetLeftTop() const
Definition: rect.h:363
Type height
Definition: size.h:29
Type width
Definition: size.h:28
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:68
#define VALIDATION_LOG
Definition: validation.h:91