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  bool stroke,
63  Cap stroke_cap,
64  Join stroke_join,
65  Scalar stroke_miter) {
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 }
79 
80 namespace {
81 Scalar AttractToOne(Scalar x) {
82  // Epsilon was decided by looking at the floating point inaccuracies in
83  // the ScaledK test.
84  const Scalar epsilon = 0.005f;
85  if (std::abs(x - 1.f) < epsilon) {
86  return 1.f;
87  }
88  if (std::abs(x + 1.f) < epsilon) {
89  return -1.f;
90  }
91  return x;
92 }
93 
94 } // namespace
95 
97  VS::PerVertexData* vtx_contents,
98  const std::shared_ptr<TextFrame>& frame,
99  Scalar scale,
100  const Matrix& entity_transform,
101  Vector2 offset,
102  std::optional<GlyphProperties> glyph_properties,
103  const std::shared_ptr<GlyphAtlas>& atlas) {
104  // Common vertex information for all glyphs.
105  // All glyphs are given the same vertex information in the form of a
106  // unit-sized quad. The size of the glyph is specified in per instance data
107  // and the vertex shader uses this to size the glyph correctly. The
108  // interpolated vertex information is also used in the fragment shader to
109  // sample from the glyph atlas.
110 
111  constexpr std::array<Point, 6> unit_points = {Point{0, 0}, Point{1, 0},
112  Point{0, 1}, Point{1, 0},
113  Point{0, 1}, Point{1, 1}};
114 
115  ISize atlas_size = atlas->GetTexture()->GetSize();
116  bool is_translation_scale = entity_transform.IsTranslationScaleOnly();
117  Matrix basis_transform = entity_transform.Basis();
118 
119  VS::PerVertexData vtx;
120  size_t i = 0u;
121  size_t bounds_offset = 0u;
122  for (const TextRun& run : frame->GetRuns()) {
123  const Font& font = run.GetFont();
124  Scalar rounded_scale = frame->GetScale();
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  Scalar inverted_rounded_scale = 1.f / rounded_scale;
185  Rect scaled_bounds = glyph_bounds.Scale(inverted_rounded_scale);
186  // For each glyph, we compute two rectangles. One for the vertex
187  // positions and one for the texture coordinates (UVs). The atlas
188  // glyph bounds are used to compute UVs in cases where the
189  // destination and source sizes may differ due to clamping the sizes
190  // of large glyphs.
191  Point uv_origin = (atlas_glyph_bounds.GetLeftTop()) / atlas_size;
192  Point uv_size = SizeToPoint(atlas_glyph_bounds.GetSize()) / atlas_size;
193 
194  Matrix unscaled_basis =
195  basis_transform * Matrix::MakeScale({inverted_rounded_scale,
196  inverted_rounded_scale, 1});
197 
198  // In typical scales < 48x these values should be -1 or 1. We round to
199  // those to avoid inaccuracies.
200  unscaled_basis.m[0] = AttractToOne(unscaled_basis.m[0]);
201  unscaled_basis.m[5] = AttractToOne(unscaled_basis.m[5]);
202 
203  Point unrounded_glyph_position =
204  // This is for RTL text.
205  unscaled_basis * glyph_bounds.GetLeftTop() +
206  (basis_transform * glyph_position.position);
207 
208  Point screen_glyph_position =
209  (screen_offset + unrounded_glyph_position + subpixel_adjustment)
210  .Floor();
211  for (const Point& point : unit_points) {
212  Point position;
213  if (is_translation_scale) {
214  position = (screen_glyph_position +
215  (unscaled_basis * point * glyph_bounds.GetSize()))
216  .Round();
217  } else {
218  position = entity_transform *
219  (glyph_position.position + scaled_bounds.GetLeftTop() +
220  point * scaled_bounds.GetSize());
221  }
222  vtx.uv = uv_origin + (uv_size * point);
223  vtx.position = position;
224  vtx_contents[i++] = vtx;
225  }
226  }
227  }
228 }
229 
231  const Entity& entity,
232  RenderPass& pass) const {
233  auto color = GetColor();
234  if (color.IsTransparent()) {
235  return true;
236  }
237 
238  auto type = frame_->GetAtlasType();
239  const std::shared_ptr<GlyphAtlas>& atlas =
240  renderer.GetLazyGlyphAtlas()->CreateOrGetGlyphAtlas(
241  *renderer.GetContext(), renderer.GetTransientsBuffer(), type);
242 
243  if (!atlas || !atlas->IsValid()) {
244  VALIDATION_LOG << "Cannot render glyphs without prepared atlas.";
245  return false;
246  }
247  if (!frame_->IsFrameComplete()) {
248  VALIDATION_LOG << "Failed to find font glyph bounds.";
249  return false;
250  }
251 
252  // Information shared by all glyph draw calls.
253  pass.SetCommandLabel("TextFrame");
254  auto opts = OptionsFromPassAndEntity(pass, entity);
255  opts.primitive_type = PrimitiveType::kTriangle;
256  pass.SetPipeline(renderer.GetGlyphAtlasPipeline(opts));
257 
258  // Common vertex uniforms for all glyphs.
259  VS::FrameInfo frame_info;
260  frame_info.mvp =
262  bool is_translation_scale = entity.GetTransform().IsTranslationScaleOnly();
263  Matrix entity_transform = entity.GetTransform();
264 
265  VS::BindFrameInfo(pass,
266  renderer.GetTransientsBuffer().EmplaceUniform(frame_info));
267 
268  FS::FragInfo frag_info;
269  frag_info.use_text_color = force_text_color_ ? 1.0 : 0.0;
270  frag_info.text_color = ToVector(color.Premultiply());
271  frag_info.is_color_glyph = type == GlyphAtlas::Type::kColorBitmap;
272 
273  FS::BindFragInfo(pass,
274  renderer.GetTransientsBuffer().EmplaceUniform(frag_info));
275 
276  SamplerDescriptor sampler_desc;
277  if (is_translation_scale) {
278  sampler_desc.min_filter = MinMagFilter::kNearest;
279  sampler_desc.mag_filter = MinMagFilter::kNearest;
280  } else {
281  // Currently, we only propagate the scale of the transform to the atlas
282  // renderer, so if the transform has more than just a translation, we turn
283  // on linear sampling to prevent crunchiness caused by the pixel grid not
284  // being perfectly aligned.
285  // The downside is that this slightly over-blurs rotated/skewed text.
286  sampler_desc.min_filter = MinMagFilter::kLinear;
287  sampler_desc.mag_filter = MinMagFilter::kLinear;
288  }
289 
290  // No mipmaps for glyph atlas (glyphs are generated at exact scales).
291  sampler_desc.mip_filter = MipFilter::kBase;
292 
293  FS::BindGlyphAtlasSampler(
294  pass, // command
295  atlas->GetTexture(), // texture
296  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
297  sampler_desc) // sampler
298  );
299 
300  auto& host_buffer = renderer.GetTransientsBuffer();
301  size_t vertex_count = 0;
302  for (const auto& run : frame_->GetRuns()) {
303  vertex_count += run.GetGlyphPositions().size();
304  }
305  vertex_count *= 6;
306 
307  BufferView buffer_view = host_buffer.Emplace(
308  vertex_count * sizeof(VS::PerVertexData), alignof(VS::PerVertexData),
309  [&](uint8_t* contents) {
310  VS::PerVertexData* vtx_contents =
311  reinterpret_cast<VS::PerVertexData*>(contents);
312  ComputeVertexData(vtx_contents, frame_, scale_,
313  /*entity_transform=*/entity_transform, offset_,
314  GetGlyphProperties(), atlas);
315  });
316 
317  pass.SetVertexBuffer(std::move(buffer_view));
319  pass.SetElementCount(vertex_count);
320 
321  return pass.Draw().ok();
322 }
323 
324 std::optional<GlyphProperties> TextContents::GetGlyphProperties() const {
325  return (properties_.stroke || frame_->HasColor())
326  ? std::optional<GlyphProperties>(properties_)
327  : std::nullopt;
328 }
329 
330 } // 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
Get the vertex shader transform used for drawing this Entity.
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
BufferView EmplaceUniform(const UniformType &uniform)
Emplace uniform data onto the host buffer. Ensure that backend specific uniform alignment requirement...
Definition: host_buffer.h:48
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:114
void SetOffset(Vector2 offset)
void SetTextProperties(Color color, bool stroke, Scalar stroke_width, Cap stroke_cap, Join stroke_join, Scalar stroke_miter)
Must be set after text frame.
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.
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 Point ComputeSubpixelPosition(const TextRun::GlyphPosition &glyph_position, AxisAlignment alignment, const Matrix &transform)
Definition: text_frame.cc:69
Represents a collection of positioned glyphs from a specific font.
Definition: text_run.h:20
int32_t value
int32_t x
@ kNone
Does not use the index buffer.
Join
Definition: path.h:25
float Scalar
Definition: scalar.h:18
GlyphAtlasPipeline::VertexShader VS
TPoint< Scalar > Point
Definition: point.h:327
Cap
Definition: path.h:19
@ 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
GlyphAtlasPipeline::FragmentShader FS
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
SolidFillVertexShader::PerVertexData vtx
const Scalar stroke_width
const Scalar scale
SeparatedVector2 offset
Scalar alpha
Definition: color.h:142
constexpr Color WithAlpha(Scalar new_alpha) const
Definition: color.h:277
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
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:427
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
#define VALIDATION_LOG
Definition: validation.h:91