Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
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
19
20namespace impeller {
22 return Point(size.width, size.height);
23}
24
25using VS = GlyphAtlasPipeline::VertexShader;
26using FS = GlyphAtlasPipeline::FragmentShader;
27
29
31
32void 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
60std::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_.tone_or_color = color.WithAlpha(1.0);
73 } else {
75 }
76 properties_.stroke = stroke;
77}
78
79namespace {
80Scalar AttractToOne(Scalar x) {
81 // Epsilon was decided by looking at the floating point inaccuracies in
82 // the ScaledK test.
83 const Scalar epsilon = 0.005f;
84 if (std::abs(x - 1.f) < epsilon) {
85 return 1.f;
86 }
87 if (std::abs(x + 1.f) < epsilon) {
88 return -1.f;
89 }
90 return x;
91}
92
93} // namespace
94
95void TextContents::ComputeVertexData(VS::PerVertexData* vtx_contents,
96 const Matrix& entity_transform,
97 const std::shared_ptr<TextFrame>& frame,
98 Point position,
99 const Matrix& screen_transform,
100 GlyphProperties glyph_properties,
101 const std::shared_ptr<GlyphAtlas>& atlas) {
102 // Common vertex information for all glyphs.
103 // All glyphs are given the same vertex information in the form of a
104 // unit-sized quad. The size of the glyph is specified in per instance data
105 // and the vertex shader uses this to size the glyph correctly. The
106 // interpolated vertex information is also used in the fragment shader to
107 // sample from the glyph atlas.
108
109 constexpr std::array<Point, 4> unit_points = {Point{0, 0}, Point{1, 0},
110 Point{0, 1}, Point{1, 1}};
111
112 Matrix entity_offset_transform =
113 entity_transform * Matrix::MakeTranslation(position);
114
115 ISize atlas_size = atlas->GetTexture()->GetSize();
116 bool is_translation_scale = entity_offset_transform.IsTranslationScaleOnly();
117 Matrix basis_transform = entity_offset_transform.Basis();
118
119 VS::PerVertexData vtx;
120 size_t i = 0u;
121
122 const Matrix frame_transform =
123 screen_transform * Matrix::MakeTranslation(position);
124 Rational rounded_scale =
126 Scalar inverted_rounded_scale = static_cast<Scalar>(rounded_scale.Invert());
127 Matrix unscaled_basis =
128 basis_transform *
129 Matrix::MakeScale({inverted_rounded_scale, inverted_rounded_scale, 1});
130
131 // In typical scales < 48x these values should be -1 or 1. We round to
132 // those to avoid inaccuracies.
133 unscaled_basis.m[0] = AttractToOne(unscaled_basis.m[0]);
134 unscaled_basis.m[5] = AttractToOne(unscaled_basis.m[5]);
135
136 // Compute the device origin of the entire frame.
137 Point screen_offset = (entity_offset_transform * Point(0, 0));
138
139 for (const TextRun& run : frame->GetRuns()) {
140 const Font& font = run.GetFont();
141 const ScaledFont scaled_font{.font = font, .scale = rounded_scale};
142 const FontGlyphAtlas* font_atlas = atlas->GetFontGlyphAtlas(scaled_font);
143
144 if (!font_atlas) {
145 VALIDATION_LOG << "Could not find font in the atlas.";
146 // We will not find glyph bounds data for any characters in this run.
147 break;
148 }
149
150 // Adjust glyph position based on the subpixel rounding used by the font.
151 //
152 // This value is really only used in the is_translation_scale case below,
153 // but that usage appears inside a pair of nested loops so we compute it
154 // once here for the common case for use many times below.
155 // For the other case, this is a fairly quick computation if we are
156 // only doing it just once.
157 Point subpixel_adjustment(0.5, 0.5);
158 switch (font.GetAxisAlignment()) {
160 break;
162 subpixel_adjustment.x = 0.125;
163 break;
165 subpixel_adjustment.y = 0.125;
166 break;
168 subpixel_adjustment.x = 0.125;
169 subpixel_adjustment.y = 0.125;
170 break;
171 }
172
173 for (const TextRun::GlyphPosition& glyph_position :
174 run.GetGlyphPositions()) {
176 glyph_position, font.GetAxisAlignment(), frame_transform);
177 SubpixelGlyph subpixel_glyph(glyph_position.glyph, subpixel,
178 glyph_properties);
179 FrameBounds frame_bounds =
180 font_atlas->FindGlyphBounds(subpixel_glyph).value_or(FrameBounds{});
181
182 // If frame_bounds.is_placeholder is true, either this set of attributes
183 // were not captured by the FirstPass dispatcher or this is the first
184 // frame the glyph has been rendered and so its atlas position was not
185 // known when the glyph was recorded. Perform a slow lookup into the
186 // glyph atlas hash table.
187 if (frame_bounds.is_placeholder) {
188 VALIDATION_LOG << "Frame bounds are not present in the atlas "
189 << font_atlas;
190 continue;
191 }
192
193 // For each glyph, we compute two rectangles. One for the vertex
194 // positions and one for the texture coordinates (UVs). The atlas
195 // glyph bounds are used to compute UVs in cases where the
196 // destination and source sizes may differ due to clamping the sizes
197 // of large glyphs.
198 Point uv_origin = frame_bounds.atlas_bounds.GetLeftTop() / atlas_size;
199 Point uv_size =
200 SizeToPoint(frame_bounds.atlas_bounds.GetSize()) / atlas_size;
201
202 for (const Point& point : unit_points) {
203 Point position;
204 if (is_translation_scale) {
205 Point unrounded_glyph_position =
206 // This is for RTL text.
207 unscaled_basis * frame_bounds.glyph_bounds.GetLeftTop() +
208 (basis_transform * glyph_position.position);
209
210 Point screen_glyph_position =
211 (screen_offset + unrounded_glyph_position + subpixel_adjustment)
212 .Floor();
213 position =
214 (screen_glyph_position +
215 (unscaled_basis * point * frame_bounds.glyph_bounds.GetSize()))
216 .Round();
217 } else {
218 Rect scaled_bounds =
219 frame_bounds.glyph_bounds.Scale(inverted_rounded_scale);
220 position = entity_offset_transform *
221 (glyph_position.position + scaled_bounds.GetLeftTop() +
222 point * scaled_bounds.GetSize());
223 }
224 vtx.uv = uv_origin + (uv_size * point);
225 vtx.position = position;
226 vtx_contents[i++] = vtx;
227 }
228 }
229 }
230}
231
233 const Entity& entity,
234 RenderPass& pass) const {
235 Color color = GetColor();
236 if (color.IsTransparent()) {
237 return true;
238 }
239
240 GlyphAtlas::Type type = frame_->GetAtlasType();
241 const std::shared_ptr<GlyphAtlas>& atlas =
242 renderer.GetLazyGlyphAtlas()->CreateOrGetGlyphAtlas(
243 *renderer.GetContext(), renderer.GetTransientsDataBuffer(), type);
244
245 if (!atlas || !atlas->IsValid()) {
246 VALIDATION_LOG << "Cannot render glyphs without prepared atlas.";
247 return false;
248 }
249
250 // Information shared by all glyph draw calls.
251 pass.SetCommandLabel("TextFrame");
252 auto opts = OptionsFromPassAndEntity(pass, entity);
253 opts.primitive_type = PrimitiveType::kTriangle;
254 pass.SetPipeline(renderer.GetGlyphAtlasPipeline(opts));
255
256 // Common vertex uniforms for all glyphs.
257 VS::FrameInfo frame_info;
258 frame_info.mvp =
260 const Matrix& entity_transform = entity.GetTransform();
261 bool is_translation_scale = entity_transform.IsTranslationScaleOnly();
262
263 VS::BindFrameInfo(
264 pass, renderer.GetTransientsDataBuffer().EmplaceUniform(frame_info));
265
266 FS::FragInfo frag_info;
267 frag_info.use_text_color = force_text_color_ ? 1.0 : 0.0;
268 frag_info.text_color = ToVector(color.Premultiply());
269 frag_info.is_color_glyph = type == GlyphAtlas::Type::kColorBitmap;
270
271 FS::BindFragInfo(
272 pass, renderer.GetTransientsDataBuffer().EmplaceUniform(frag_info));
273
274 SamplerDescriptor sampler_desc;
275 if (is_translation_scale) {
276 // When the transform is translation+scale only, we normally use nearest-
277 // neighbor sampling for pixel-perfect text. However, if the X and Y
278 // scales differ significantly (non-uniform / anisotropic scaling, e.g.
279 // Transform.scale(scaleY: 2)), the glyph atlas entry is rasterized at
280 // max(|scaleX|,|scaleY|) uniformly and the compensating unscaled_basis
281 // squeezes one axis, causing a minification. Nearest-neighbor during
282 // minification discards texel columns/rows, producing jagged diagonals
283 // and varying stroke weights. Fall back to bilinear in that case.
284 // See https://github.com/flutter/flutter/issues/182143
285 constexpr Scalar kMinScaleForRatio = 0.001f;
286 constexpr Scalar kAnisotropicScaleThreshold = 1.15f;
287 const Scalar sx = entity_transform.GetBasisX().GetLength();
288 const Scalar sy = entity_transform.GetBasisY().GetLength();
289 const Scalar ratio = (sx > sy) ? sx / std::max(sy, kMinScaleForRatio)
290 : sy / std::max(sx, kMinScaleForRatio);
291 if (ratio > kAnisotropicScaleThreshold) {
292 // Non-uniform scale — use bilinear to avoid aliasing.
293 sampler_desc.min_filter = MinMagFilter::kLinear;
294 sampler_desc.mag_filter = MinMagFilter::kLinear;
295 } else {
296 sampler_desc.min_filter = MinMagFilter::kNearest;
297 sampler_desc.mag_filter = MinMagFilter::kNearest;
298 }
299 } else {
300 // Currently, we only propagate the scale of the transform to the atlas
301 // renderer, so if the transform has more than just a translation, we turn
302 // on linear sampling to prevent crunchiness caused by the pixel grid not
303 // being perfectly aligned.
304 // The downside is that this slightly over-blurs rotated/skewed text.
305 sampler_desc.min_filter = MinMagFilter::kLinear;
306 sampler_desc.mag_filter = MinMagFilter::kLinear;
307 }
308
309 // No mipmaps for glyph atlas (glyphs are generated at exact scales).
310 sampler_desc.mip_filter = MipFilter::kBase;
311
312 FS::BindGlyphAtlasSampler(
313 pass, // command
314 atlas->GetTexture(), // texture
315 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
316 sampler_desc) // sampler
317 );
318
319 HostBuffer& data_host_buffer = renderer.GetTransientsDataBuffer();
320 HostBuffer& indexes_host_buffer = renderer.GetTransientsIndexesBuffer();
321 size_t glyph_count = 0;
322 for (const auto& run : frame_->GetRuns()) {
323 glyph_count += run.GetGlyphPositions().size();
324 }
325 size_t vertex_count = glyph_count * 4;
326 size_t index_count = glyph_count * 6;
327
328 BufferView buffer_view = data_host_buffer.Emplace(
329 vertex_count * sizeof(VS::PerVertexData), alignof(VS::PerVertexData),
330 [&](uint8_t* data) {
331 VS::PerVertexData* vtx_contents =
332 reinterpret_cast<VS::PerVertexData*>(data);
333 ComputeVertexData(/*vtx_contents=*/vtx_contents,
334 /*entity_transform=*/entity.GetTransform(),
335 /*frame=*/frame_,
336 /*position=*/position_,
337 /*screen_transform=*/screen_transform_,
338 /*glyph_properties=*/properties_,
339 /*atlas=*/atlas);
340 });
341 BufferView index_buffer_view = indexes_host_buffer.Emplace(
342 index_count * sizeof(uint16_t), alignof(uint16_t), [&](uint8_t* data) {
343 uint16_t* indices = reinterpret_cast<uint16_t*>(data);
344 size_t j = 0;
345 for (auto i = 0u; i < glyph_count; i++) {
346 size_t base = i * 4;
347 indices[j++] = base + 0;
348 indices[j++] = base + 1;
349 indices[j++] = base + 2;
350 indices[j++] = base + 1;
351 indices[j++] = base + 2;
352 indices[j++] = base + 3;
353 }
354 });
355
356 pass.SetVertexBuffer(std::move(buffer_view));
357 pass.SetIndexBuffer(index_buffer_view, IndexType::k16bit);
358 pass.SetElementCount(index_count);
359
360 return pass.Draw().ok();
361}
362
363} // namespace impeller
bool ok() const
Definition status.h:71
HostBuffer & GetTransientsDataBuffer() const
Retrieve the current host buffer for transient storage of other non-index data.
const std::shared_ptr< LazyGlyphAtlas > & GetLazyGlyphAtlas() const
PipelineRef GetGlyphAtlasPipeline(ContentContextOptions opts) const
HostBuffer & GetTransientsIndexesBuffer() const
Retrieve the current host buffer for transient storage of indexes used for indexed draws.
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.
std::optional< FrameBounds > FindGlyphBounds(const SubpixelGlyph &glyph) const
Find the location of a glyph in the atlas.
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.
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 ...
virtual void SetPipeline(PipelineRef pipeline)
The pipeline to use for this command.
virtual fml::Status Draw()
Record the currently pending command.
virtual void SetElementCount(size_t count)
virtual void SetCommandLabel(std::string_view label)
The debugging label to use for the command.
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)
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)
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.
impeller::ShaderType type
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
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 TPoint< T > GetLeftTop() const
Definition rect.h:393
constexpr TRect Scale(Type scale) const
Definition rect.h:226
Scalar GetLength() const
Definition vector.h:47
#define VALIDATION_LOG
Definition validation.h:91