| 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
5 #include "impeller/entity/contents/text_contents.h"
6
7 #include <cstring>
8 #include <optional>
9 #include <utility>
10
11 #include "impeller/core/buffer_view.h"
12 #include "impeller/core/formats.h"
13 #include "impeller/core/sampler_descriptor.h"
14 #include "impeller/entity/entity.h"
15 #include "impeller/geometry/color.h"
16 #include "impeller/geometry/point.h"
17 #include "impeller/renderer/render_pass.h"
18 #include "impeller/typographer/glyph_atlas.h"
19
20 namespace impeller {
21 Point SizeToPoint(Size size) {
22 return Point(size.width, size.height);
23 }
24
25 using VS = GlyphAtlasPipeline::VertexShader;
26 using FS = GlyphAtlasPipeline::FragmentShader;
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
36 void TextContents::SetColor(Color color) {
37 color_ = color;
38 }
39
40 Color TextContents::GetColor() const {
41 return color_.WithAlpha(color_.alpha * inherited_opacity_);
42 }
43
44 void TextContents::SetInheritedOpacity(Scalar opacity) {
45 inherited_opacity_ = opacity;
46 }
47
48 void TextContents::SetOffset(Vector2 offset) {
49 offset_ = offset;
50 }
51
52 void TextContents::SetForceTextColor(bool value) {
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
60 void TextContents::SetTextProperties(Color color,
61 bool stroke,
62 Scalar stroke_width,
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 void TextContents::ComputeVertexData(
81 VS::PerVertexData* vtx_contents,
82 const std::shared_ptr<TextFrame>& frame,
83 Scalar scale,
84 const Matrix& entity_transform,
85 Vector2 offset,
86 std::optional<GlyphProperties> glyph_properties,
87 const std::shared_ptr<GlyphAtlas>& atlas) {
88 // Common vertex information for all glyphs.
89 // All glyphs are given the same vertex information in the form of a
90 // unit-sized quad. The size of the glyph is specified in per instance data
91 // and the vertex shader uses this to size the glyph correctly. The
92 // interpolated vertex information is also used in the fragment shader to
93 // sample from the glyph atlas.
94
95 constexpr std::array<Point, 6> unit_points = {Point{0, 0}, Point{1, 0},
96 Point{0, 1}, Point{1, 0},
97 Point{0, 1}, Point{1, 1}};
98
99 ISize atlas_size = atlas->GetTexture()->GetSize();
100 bool is_translation_scale = entity_transform.IsTranslationScaleOnly();
101 Matrix basis_transform = entity_transform.Basis();
102
103 VS::PerVertexData vtx;
104 size_t i = 0u;
105 size_t bounds_offset = 0u;
106 for (const TextRun& run : frame->GetRuns()) {
107 const Font& font = run.GetFont();
108 Scalar rounded_scale = TextFrame::RoundScaledFontSize(scale);
109 FontGlyphAtlas* font_atlas = nullptr;
110
111 // Adjust glyph position based on the subpixel rounding
112 // used by the font.
113 Point subpixel_adjustment(0.5, 0.5);
114 switch (font.GetAxisAlignment()) {
115 case AxisAlignment::kNone:
116 break;
117 case AxisAlignment::kX:
118 subpixel_adjustment.x = 0.125;
119 break;
120 case AxisAlignment::kY:
121 subpixel_adjustment.y = 0.125;
122 break;
123 case AxisAlignment::kAll:
124 subpixel_adjustment.x = 0.125;
125 subpixel_adjustment.y = 0.125;
126 break;
127 }
128
129 Point screen_offset = (entity_transform * Point(0, 0));
130 for (const TextRun::GlyphPosition& glyph_position :
131 run.GetGlyphPositions()) {
132 const FrameBounds& frame_bounds = frame->GetFrameBounds(bounds_offset);
133 bounds_offset++;
134 auto atlas_glyph_bounds = frame_bounds.atlas_bounds;
135 auto glyph_bounds = frame_bounds.glyph_bounds;
136
137 // If frame_bounds.is_placeholder is true, this is the first frame
138 // the glyph has been rendered and so its atlas position was not
139 // known when the glyph was recorded. Perform a slow lookup into the
140 // glyph atlas hash table.
141 if (frame_bounds.is_placeholder) {
142 if (!font_atlas) {
143 font_atlas =
144 atlas->GetOrCreateFontGlyphAtlas(ScaledFont{font, rounded_scale});
145 }
146
147 if (!font_atlas) {
148 VALIDATION_LOG << "Could not find font in the atlas.";
149 continue;
150 }
151 Point subpixel = TextFrame::ComputeSubpixelPosition(
152 glyph_position, font.GetAxisAlignment(), entity_transform);
153
154 std::optional<FrameBounds> maybe_atlas_glyph_bounds =
155 font_atlas->FindGlyphBounds(SubpixelGlyph{
156 glyph_position.glyph, //
157 subpixel, //
158 glyph_properties //
159 });
160 if (!maybe_atlas_glyph_bounds.has_value()) {
161 VALIDATION_LOG << "Could not find glyph position in the atlas.";
162 continue;
163 }
164 atlas_glyph_bounds = maybe_atlas_glyph_bounds.value().atlas_bounds;
165 }
166
167 Rect scaled_bounds = glyph_bounds.Scale(1.0 / rounded_scale);
168 // For each glyph, we compute two rectangles. One for the vertex
169 // positions and one for the texture coordinates (UVs). The atlas
170 // glyph bounds are used to compute UVs in cases where the
171 // destination and source sizes may differ due to clamping the sizes
172 // of large glyphs.
173 Point uv_origin = (atlas_glyph_bounds.GetLeftTop()) / atlas_size;
174 Point uv_size = SizeToPoint(atlas_glyph_bounds.GetSize()) / atlas_size;
175
176 Point unrounded_glyph_position =
177 // This is for RTL text.
178 (basis_transform.m[0] < 0 ? Matrix::MakeScale({-1, 1, 1})
179 : Matrix()) *
180 glyph_bounds.GetLeftTop() +
181 (basis_transform * glyph_position.position);
182
183 Point screen_glyph_position =
184 (screen_offset + unrounded_glyph_position + subpixel_adjustment)
185 .Floor();
186 for (const Point& point : unit_points) {
187 Point position;
188 if (is_translation_scale) {
189 position = (screen_glyph_position +
190 (basis_transform * point * scaled_bounds.GetSize()))
191 .Round();
192 } else {
193 position = entity_transform *
194 (glyph_position.position + scaled_bounds.GetLeftTop() +
195 point * scaled_bounds.GetSize());
196 }
197 vtx.uv = uv_origin + (uv_size * point);
198 vtx.position = position;
199 vtx_contents[i++] = vtx;
200 }
201 }
202 }
203 }
204
205 bool TextContents::Render(const ContentContext& renderer,
206 const Entity& entity,
207 RenderPass& pass) const {
208 auto color = GetColor();
209 if (color.IsTransparent()) {
210 return true;
211 }
212
213 auto type = frame_->GetAtlasType();
214 const std::shared_ptr<GlyphAtlas>& atlas =
215 renderer.GetLazyGlyphAtlas()->CreateOrGetGlyphAtlas(
216 *renderer.GetContext(), renderer.GetTransientsBuffer(), type);
217
218 if (!atlas || !atlas->IsValid()) {
219 VALIDATION_LOG << "Cannot render glyphs without prepared atlas.";
220 return false;
221 }
222 if (!frame_->IsFrameComplete()) {
223 VALIDATION_LOG << "Failed to find font glyph bounds.";
224 return false;
225 }
226
227 // Information shared by all glyph draw calls.
228 pass.SetCommandLabel("TextFrame");
229 auto opts = OptionsFromPassAndEntity(pass, entity);
230 opts.primitive_type = PrimitiveType::kTriangle;
231 pass.SetPipeline(renderer.GetGlyphAtlasPipeline(opts));
232
233 // Common vertex uniforms for all glyphs.
234 VS::FrameInfo frame_info;
235 frame_info.mvp =
236 Entity::GetShaderTransform(entity.GetShaderClipDepth(), pass, Matrix());
237 bool is_translation_scale = entity.GetTransform().IsTranslationScaleOnly();
238 Matrix entity_transform = entity.GetTransform();
239
240 VS::BindFrameInfo(pass,
241 renderer.GetTransientsBuffer().EmplaceUniform(frame_info));
242
243 FS::FragInfo frag_info;
244 frag_info.use_text_color = force_text_color_ ? 1.0 : 0.0;
245 frag_info.text_color = ToVector(color.Premultiply());
246 frag_info.is_color_glyph = type == GlyphAtlas::Type::kColorBitmap;
247
248 FS::BindFragInfo(pass,
249 renderer.GetTransientsBuffer().EmplaceUniform(frag_info));
250
251 SamplerDescriptor sampler_desc;
252 if (is_translation_scale) {
253 sampler_desc.min_filter = MinMagFilter::kNearest;
254 sampler_desc.mag_filter = MinMagFilter::kNearest;
255 } else {
256 // Currently, we only propagate the scale of the transform to the atlas
257 // renderer, so if the transform has more than just a translation, we turn
258 // on linear sampling to prevent crunchiness caused by the pixel grid not
259 // being perfectly aligned.
260 // The downside is that this slightly over-blurs rotated/skewed text.
261 sampler_desc.min_filter = MinMagFilter::kLinear;
262 sampler_desc.mag_filter = MinMagFilter::kLinear;
263 }
264
265 // No mipmaps for glyph atlas (glyphs are generated at exact scales).
266 sampler_desc.mip_filter = MipFilter::kBase;
267
268 FS::BindGlyphAtlasSampler(
269 pass, // command
270 atlas->GetTexture(), // texture
271 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
272 sampler_desc) // sampler
273 );
274
275 auto& host_buffer = renderer.GetTransientsBuffer();
276 size_t vertex_count = 0;
277 for (const auto& run : frame_->GetRuns()) {
278 vertex_count += run.GetGlyphPositions().size();
279 }
280 vertex_count *= 6;
281
282 BufferView buffer_view = host_buffer.Emplace(
283 vertex_count * sizeof(VS::PerVertexData), alignof(VS::PerVertexData),
284 [&](uint8_t* contents) {
285 VS::PerVertexData* vtx_contents =
286 reinterpret_cast<VS::PerVertexData*>(contents);
287 ComputeVertexData(vtx_contents, frame_, scale_,
288 /*entity_transform=*/entity_transform, offset_,
289 GetGlyphProperties(), atlas);
290 });
291
292 pass.SetVertexBuffer(std::move(buffer_view));
293 pass.SetIndexBuffer({}, IndexType::kNone);
294 pass.SetElementCount(vertex_count);
295
296 return pass.Draw().ok();
297 }
298
299 std::optional<GlyphProperties> TextContents::GetGlyphProperties() const {
300 return (properties_.stroke || frame_->HasColor())
301 ? std::optional<GlyphProperties>(properties_)
302 : std::nullopt;
303 }
304
305 } // namespace impeller
text_contents.h
impeller::GlyphAtlas::Type::kColorBitmap
@ kColorBitmap
impeller::RenderPipelineHandle::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline.h:114
impeller::Matrix::m
Scalar m[16]
Definition: matrix.h:39
impeller::MipFilter::kBase
@ kBase
The texture is sampled as if it only had a single mipmap level.
impeller::Entity::GetShaderTransform
Matrix GetShaderTransform(const RenderPass &pass) const
Get the vertex shader transform used for drawing this Entity.
Definition: entity.cc:48
impeller::AxisAlignment::kAll
@ kAll
point.h
impeller::Entity::GetShaderClipDepth
float GetShaderClipDepth() const
Definition: entity.cc:88
impeller::TPoint::y
Type y
Definition: point.h:31
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::Font
Describes a typeface along with any modifications to its intrinsic properties.
Definition: font.h:35
impeller::ContentContext::GetLazyGlyphAtlas
const std::shared_ptr< LazyGlyphAtlas > & GetLazyGlyphAtlas() const
Definition: content_context.h:834
impeller::Entity::GetTransform
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:44
entity.h
impeller::Color
Definition: color.h:123
impeller::FontGlyphAtlas::FindGlyphBounds
std::optional< FrameBounds > FindGlyphBounds(const SubpixelGlyph &glyph) const
Find the location of a glyph in the atlas.
Definition: glyph_atlas.cc:131
vtx
SolidFillVertexShader::PerVertexData vtx
Definition: stroke_path_geometry.cc:320
impeller::FontGlyphAtlas
An object that can look up glyph locations within the GlyphAtlas for a particular typeface.
Definition: glyph_atlas.h:269
value
int32_t value
Definition: dl_golden_unittests.cc:444
impeller::TextContents::SetForceTextColor
void SetForceTextColor(bool value)
Force the text color to apply to the rendered glyphs, even if those glyphs are bitmaps.
Definition: text_contents.cc:52
impeller::VS
GlyphAtlasPipeline::VertexShader VS
Definition: text_contents.cc:25
impeller::TextContents::SetTextProperties
void SetTextProperties(Color color, bool stroke, Scalar stroke_width, Cap stroke_cap, Join stroke_join, Scalar stroke_miter)
Must be set after text frame.
Definition: text_contents.cc:60
impeller::SubpixelGlyph
A glyph and its subpixel position.
Definition: font_glyph_pair.h:60
impeller::TextContents::GetCoverage
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.
Definition: text_contents.cc:56
impeller::TRect::GetLeftTop
constexpr TPoint< T > GetLeftTop() const
Definition: rect.h:363
impeller::TPoint::Round
static constexpr TPoint Round(const TPoint< U > &other)
Definition: point.h:49
formats.h
impeller::Color::alpha
Scalar alpha
Definition: color.h:142
impeller::RenderPass::SetVertexBuffer
virtual bool SetVertexBuffer(VertexBuffer buffer)
Specify the vertex and index buffer to use for this command.
Definition: render_pass.cc:127
buffer_view
BufferView buffer_view
Definition: blit_command_gles.cc:152
impeller::GlyphProperties::stroke_miter
Scalar stroke_miter
Definition: font_glyph_pair.h:21
impeller::TextRun
Represents a collection of positioned glyphs from a specific font.
Definition: text_run.h:20
impeller::RenderPass::SetCommandLabel
virtual void SetCommandLabel(std::string_view label)
The debugging label to use for the command.
Definition: render_pass.cc:97
impeller::TextContents::SetColor
void SetColor(Color color)
Definition: text_contents.cc:36
stroke_width
const Scalar stroke_width
Definition: stroke_path_geometry.cc:312
offset
SeparatedVector2 offset
Definition: stroke_path_geometry.cc:319
impeller::MinMagFilter::kNearest
@ kNearest
Select nearest to the sample point. Most widely supported.
impeller::GlyphProperties::color
Color color
Definition: font_glyph_pair.h:17
impeller::FrameBounds::is_placeholder
bool is_placeholder
Definition: glyph_atlas.h:62
impeller::RenderPass::Draw
virtual fml::Status Draw()
Record the currently pending command.
Definition: render_pass.cc:208
impeller::SamplerDescriptor::mag_filter
MinMagFilter mag_filter
Definition: sampler_descriptor.h:16
impeller::SamplerDescriptor
Definition: sampler_descriptor.h:14
impeller::PrimitiveType::kTriangle
@ kTriangle
impeller::FrameBounds::atlas_bounds
Rect atlas_bounds
The bounds of the glyph within the glyph atlas.
Definition: glyph_atlas.h:57
impeller::Entity
Definition: entity.h:20
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:34
impeller::TextFrame::RoundScaledFontSize
static Scalar RoundScaledFontSize(Scalar scale)
Definition: text_frame.cc:54
impeller::Matrix::Basis
constexpr Matrix Basis() const
The Matrix without its w components (without translation).
Definition: matrix.h:239
impeller::TSize< Scalar >
impeller::SamplerDescriptor::min_filter
MinMagFilter min_filter
Definition: sampler_descriptor.h:15
impeller::Point
TPoint< Scalar > Point
Definition: point.h:327
render_pass.h
impeller::TextContents::ComputeVertexData
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)
Definition: text_contents.cc:80
impeller::ContentContext::GetGlyphAtlasPipeline
PipelineRef GetGlyphAtlasPipeline(ContentContextOptions opts) const
Definition: content_context.h:518
impeller::ContentContext::GetContext
std::shared_ptr< Context > GetContext() const
Definition: content_context.cc:613
impeller::Color::WithAlpha
constexpr Color WithAlpha(Scalar new_alpha) const
Definition: color.h:277
impeller::Matrix::IsTranslationScaleOnly
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
impeller::RenderPipelineHandle::VertexShader
VertexShader_ VertexShader
Definition: pipeline.h:113
impeller::RenderPass::SetIndexBuffer
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
type
GLenum type
Definition: blit_command_gles.cc:151
impeller::IndexType::kNone
@ kNone
Does not use the index buffer.
impeller::TRect::Scale
constexpr TRect Scale(Type scale) const
Definition: rect.h:206
impeller::SubpixelGlyph::glyph
Glyph glyph
Definition: font_glyph_pair.h:61
impeller::GlyphProperties::stroke_join
Join stroke_join
Definition: font_glyph_pair.h:20
impeller::GlyphProperties::stroke
bool stroke
Definition: font_glyph_pair.h:22
impeller::AxisAlignment::kX
@ kX
impeller::SizeToPoint
Point SizeToPoint(Size size)
Definition: text_contents.cc:21
impeller::TextContents::~TextContents
~TextContents()
impeller::TSize::width
Type width
Definition: size.h:28
impeller::TextContents::SetOffset
void SetOffset(Vector2 offset)
Definition: text_contents.cc:48
impeller::SamplerDescriptor::mip_filter
MipFilter mip_filter
Definition: sampler_descriptor.h:17
impeller::TPoint::x
Type x
Definition: point.h:30
impeller::GlyphProperties::stroke_width
Scalar stroke_width
Definition: font_glyph_pair.h:18
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:30
impeller::MinMagFilter::kLinear
@ kLinear
sampler_descriptor.h
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91
impeller::BufferView
Definition: buffer_view.h:19
impeller::TRect::GetSize
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
impeller::GlyphProperties::stroke_cap
Cap stroke_cap
Definition: font_glyph_pair.h:19
impeller::Join
Join
Definition: path.h:25
impeller::TextContents::TextContents
TextContents()
std
Definition: comparable.h:95
impeller::TextContents::SetInheritedOpacity
void SetInheritedOpacity(Scalar opacity) override
Inherit the provided opacity.
Definition: text_contents.cc:44
impeller::ToVector
constexpr Vector4 ToVector(Color color)
Definition: shader_types.h:190
buffer_view.h
impeller::TPoint
Definition: point.h:27
impeller::TextRun::GlyphPosition
Definition: text_run.h:22
impeller::RenderPass::SetElementCount
virtual void SetElementCount(size_t count)
Definition: render_pass.cc:119
impeller::HostBuffer::EmplaceUniform
BufferView EmplaceUniform(const UniformType &uniform)
Emplace uniform data onto the host buffer. Ensure that backend specific uniform alignment requirement...
Definition: host_buffer.h:48
impeller::TextContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: text_contents.cc:205
scale
const Scalar scale
Definition: stroke_path_geometry.cc:316
impeller::FrameBounds
Definition: glyph_atlas.h:55
impeller::TextContents::GetColor
Color GetColor() const
Definition: text_contents.cc:40
color.h
impeller::RenderPass::SetPipeline
virtual void SetPipeline(PipelineRef pipeline)
The pipeline to use for this command.
Definition: render_pass.cc:86
impeller::TSize::height
Type height
Definition: size.h:29
impeller::TextContents::SetTextFrame
void SetTextFrame(const std::shared_ptr< TextFrame > &frame)
Definition: text_contents.cc:32
glyph_atlas.h
impeller::AxisAlignment::kNone
@ kNone
impeller::TextFrame::ComputeSubpixelPosition
static Point ComputeSubpixelPosition(const TextRun::GlyphPosition &glyph_position, AxisAlignment alignment, const Matrix &transform)
Definition: text_frame.cc:82
impeller
Definition: allocation.cc:12
impeller::FS
GlyphAtlasPipeline::FragmentShader FS
Definition: text_contents.cc:26
impeller::Matrix::MakeScale
static constexpr Matrix MakeScale(const Vector3 &s)
Definition: matrix.h:104
impeller::ContentContext
Definition: content_context.h:371
impeller::TRect< Scalar >
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::ScaledFont
A font and a scale. Used as a key that represents a typeface within a glyph atlas.
Definition: font_glyph_pair.h:40
impeller::ContentContext::GetTransientsBuffer
HostBuffer & GetTransientsBuffer() const
Retrieve the currnent host buffer for transient storage.
Definition: content_context.h:866
impeller::Cap
Cap
Definition: path.h:19
impeller::Font::GetAxisAlignment
AxisAlignment GetAxisAlignment() const
Definition: font.cc:41
impeller::AxisAlignment::kY
@ kY
impeller::FrameBounds::glyph_bounds
Rect glyph_bounds
The local glyph bounds.
Definition: glyph_atlas.h:59