| 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/contents/content_context.h"
15 #include "impeller/entity/entity.h"
16 #include "impeller/geometry/color.h"
17 #include "impeller/geometry/point.h"
18 #include "impeller/renderer/render_pass.h"
19 #include "impeller/typographer/glyph_atlas.h"
20 #include "impeller/typographer/lazy_glyph_atlas.h"
21
22 namespace impeller {
23
24 TextContents::TextContents() = default;
25
26 TextContents::~TextContents() = default;
27
28 void TextContents::SetTextFrame(const std::shared_ptr<TextFrame>& frame) {
29 frame_ = frame;
30 }
31
32 void TextContents::SetColor(Color color) {
33 color_ = color;
34 }
35
36 Color TextContents::GetColor() const {
37 return color_.WithAlpha(color_.alpha * inherited_opacity_);
38 }
39
40 void TextContents::SetInheritedOpacity(Scalar opacity) {
41 inherited_opacity_ = opacity;
42 }
43
44 void TextContents::SetOffset(Vector2 offset) {
45 offset_ = offset;
46 }
47
48 void TextContents::SetForceTextColor(bool value) {
49 force_text_color_ = value;
50 }
51
52 std::optional<Rect> TextContents::GetCoverage(const Entity& entity) const {
53 return frame_->GetBounds().TransformBounds(entity.GetTransform());
54 }
55
56 void TextContents::SetTextProperties(Color color,
57 bool stroke,
58 Scalar stroke_width,
59 Cap stroke_cap,
60 Join stroke_join,
61 Scalar stroke_miter) {
62 if (frame_->HasColor()) {
63 // Alpha is always applied when rendering, remove it here so
64 // we do not double-apply the alpha.
65 properties_.color = color.WithAlpha(1.0);
66 }
67 if (stroke) {
68 properties_.stroke = true;
69 properties_.stroke_width = stroke_width;
70 properties_.stroke_cap = stroke_cap;
71 properties_.stroke_join = stroke_join;
72 properties_.stroke_miter = stroke_miter;
73 }
74 }
75
76 bool TextContents::Render(const ContentContext& renderer,
77 const Entity& entity,
78 RenderPass& pass) const {
79 auto color = GetColor();
80 if (color.IsTransparent()) {
81 return true;
82 }
83
84 auto type = frame_->GetAtlasType();
85 const std::shared_ptr<GlyphAtlas>& atlas =
86 renderer.GetLazyGlyphAtlas()->CreateOrGetGlyphAtlas(
87 *renderer.GetContext(), renderer.GetTransientsBuffer(), type);
88
89 if (!atlas || !atlas->IsValid()) {
90 VALIDATION_LOG << "Cannot render glyphs without prepared atlas.";
91 return false;
92 }
93
94 // Information shared by all glyph draw calls.
95 pass.SetCommandLabel("TextFrame");
96 auto opts = OptionsFromPassAndEntity(pass, entity);
97 opts.primitive_type = PrimitiveType::kTriangle;
98 pass.SetPipeline(renderer.GetGlyphAtlasPipeline(opts));
99
100 using VS = GlyphAtlasPipeline::VertexShader;
101 using FS = GlyphAtlasPipeline::FragmentShader;
102
103 // Common vertex uniforms for all glyphs.
104 VS::FrameInfo frame_info;
105 frame_info.mvp =
106 Entity::GetShaderTransform(entity.GetShaderClipDepth(), pass, Matrix());
107 ISize atlas_size = atlas->GetTexture()->GetSize();
108 bool is_translation_scale = entity.GetTransform().IsTranslationScaleOnly();
109 Matrix entity_transform = entity.GetTransform();
110 Matrix basis_transform = entity_transform.Basis();
111
112 VS::BindFrameInfo(pass,
113 renderer.GetTransientsBuffer().EmplaceUniform(frame_info));
114
115 FS::FragInfo frag_info;
116 frag_info.use_text_color = force_text_color_ ? 1.0 : 0.0;
117 frag_info.text_color = ToVector(color.Premultiply());
118 frag_info.is_color_glyph = type == GlyphAtlas::Type::kColorBitmap;
119
120 FS::BindFragInfo(pass,
121 renderer.GetTransientsBuffer().EmplaceUniform(frag_info));
122
123 SamplerDescriptor sampler_desc;
124 if (is_translation_scale) {
125 sampler_desc.min_filter = MinMagFilter::kNearest;
126 sampler_desc.mag_filter = MinMagFilter::kNearest;
127 } else {
128 // Currently, we only propagate the scale of the transform to the atlas
129 // renderer, so if the transform has more than just a translation, we turn
130 // on linear sampling to prevent crunchiness caused by the pixel grid not
131 // being perfectly aligned.
132 // The downside is that this slightly over-blurs rotated/skewed text.
133 sampler_desc.min_filter = MinMagFilter::kLinear;
134 sampler_desc.mag_filter = MinMagFilter::kLinear;
135 }
136
137 // No mipmaps for glyph atlas (glyphs are generated at exact scales).
138 sampler_desc.mip_filter = MipFilter::kBase;
139
140 FS::BindGlyphAtlasSampler(
141 pass, // command
142 atlas->GetTexture(), // texture
143 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
144 sampler_desc) // sampler
145 );
146
147 // Common vertex information for all glyphs.
148 // All glyphs are given the same vertex information in the form of a
149 // unit-sized quad. The size of the glyph is specified in per instance data
150 // and the vertex shader uses this to size the glyph correctly. The
151 // interpolated vertex information is also used in the fragment shader to
152 // sample from the glyph atlas.
153
154 constexpr std::array<Point, 6> unit_points = {Point{0, 0}, Point{1, 0},
155 Point{0, 1}, Point{1, 0},
156 Point{0, 1}, Point{1, 1}};
157
158 auto& host_buffer = renderer.GetTransientsBuffer();
159 size_t vertex_count = 0;
160 for (const auto& run : frame_->GetRuns()) {
161 vertex_count += run.GetGlyphPositions().size();
162 }
163 vertex_count *= 6;
164
165 BufferView buffer_view = host_buffer.Emplace(
166 vertex_count * sizeof(VS::PerVertexData), alignof(VS::PerVertexData),
167 [&](uint8_t* contents) {
168 VS::PerVertexData vtx;
169 VS::PerVertexData* vtx_contents =
170 reinterpret_cast<VS::PerVertexData*>(contents);
171 size_t i = 0u;
172 for (const TextRun& run : frame_->GetRuns()) {
173 const Font& font = run.GetFont();
174 Scalar rounded_scale = TextFrame::RoundScaledFontSize(
175 scale_, font.GetMetrics().point_size);
176 const FontGlyphAtlas* font_atlas =
177 atlas->GetFontGlyphAtlas(font, rounded_scale);
178 if (!font_atlas) {
179 VALIDATION_LOG << "Could not find font in the atlas.";
180 continue;
181 }
182
183 // Adjust glyph position based on the subpixel rounding
184 // used by the font.
185 Point subpixel_adjustment(0.5, 0.5);
186 switch (font.GetAxisAlignment()) {
187 case AxisAlignment::kNone:
188 break;
189 case AxisAlignment::kX:
190 subpixel_adjustment.x = 0.125;
191 break;
192 case AxisAlignment::kY:
193 subpixel_adjustment.y = 0.125;
194 break;
195 case AxisAlignment::kAll:
196 subpixel_adjustment.x = 0.125;
197 subpixel_adjustment.y = 0.125;
198 break;
199 }
200
201 Point screen_offset = (entity_transform * Point(0, 0));
202 for (const TextRun::GlyphPosition& glyph_position :
203 run.GetGlyphPositions()) {
204 // Note: uses unrounded scale for more accurate subpixel position.
205 Point subpixel = TextFrame::ComputeSubpixelPosition(
206 glyph_position, font.GetAxisAlignment(), offset_, scale_);
207 std::optional<std::pair<Rect, Rect>> maybe_atlas_glyph_bounds =
208 font_atlas->FindGlyphBounds(SubpixelGlyph{
209 glyph_position.glyph, subpixel,
210 (properties_.stroke || frame_->HasColor())
211 ? std::optional<GlyphProperties>(properties_)
212 : std::nullopt});
213 if (!maybe_atlas_glyph_bounds.has_value()) {
214 VALIDATION_LOG << "Could not find glyph position in the atlas.";
215 continue;
216 }
217 const Rect& atlas_glyph_bounds =
218 maybe_atlas_glyph_bounds.value().first;
219 Rect glyph_bounds = maybe_atlas_glyph_bounds.value().second;
220 Rect scaled_bounds = glyph_bounds.Scale(1.0 / rounded_scale);
221 // For each glyph, we compute two rectangles. One for the vertex
222 // positions and one for the texture coordinates (UVs). The atlas
223 // glyph bounds are used to compute UVs in cases where the
224 // destination and source sizes may differ due to clamping the sizes
225 // of large glyphs.
226 Point uv_origin =
227 (atlas_glyph_bounds.GetLeftTop() - Point(0.5, 0.5)) /
228 atlas_size;
229 Point uv_size =
230 (atlas_glyph_bounds.GetSize() + Point(1, 1)) / atlas_size;
231
232 Point unrounded_glyph_position =
233 basis_transform *
234 (glyph_position.position + scaled_bounds.GetLeftTop());
235
236 Point screen_glyph_position =
237 (screen_offset + unrounded_glyph_position + subpixel_adjustment)
238 .Floor();
239
240 for (const Point& point : unit_points) {
241 Point position;
242 if (is_translation_scale) {
243 position = (screen_glyph_position +
244 (basis_transform * point * scaled_bounds.GetSize()))
245 .Round();
246 } else {
247 position = entity_transform * (glyph_position.position +
248 scaled_bounds.GetLeftTop() +
249 point * scaled_bounds.GetSize());
250 }
251 vtx.uv = uv_origin + (uv_size * point);
252 vtx.position = position;
253 vtx_contents[i++] = vtx;
254 }
255 }
256 }
257 });
258
259 pass.SetVertexBuffer({
260 .vertex_buffer = std::move(buffer_view),
261 .index_buffer = {},
262 .vertex_count = vertex_count,
263 .index_type = IndexType::kNone,
264 });
265
266 return pass.Draw().ok();
267 }
268
269 } // namespace impeller
text_contents.h
impeller::GlyphAtlas::Type::kColorBitmap
@ kColorBitmap
impeller::RenderPipelineHandle::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline.h:107
impeller::Entity::GetShaderTransform
Matrix GetShaderTransform(const RenderPass &pass) const
Get the vertex shader transform used for drawing this Entity.
Definition: entity.cc:51
lazy_glyph_atlas.h
impeller::AxisAlignment::kAll
@ kAll
point.h
impeller::Entity::GetShaderClipDepth
float GetShaderClipDepth() const
Definition: entity.cc:99
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:721
impeller::Entity::GetTransform
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:47
entity.h
impeller::Color
Definition: color.h:123
vtx
SolidFillVertexShader::PerVertexData vtx
Definition: stroke_path_geometry.cc:305
impeller::FontGlyphAtlas
An object that can look up glyph locations within the GlyphAtlas for a particular typeface.
Definition: glyph_atlas.h:204
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:48
impeller::TextFrame::ComputeSubpixelPosition
static Point ComputeSubpixelPosition(const TextRun::GlyphPosition &glyph_position, AxisAlignment alignment, Point offset, Scalar scale)
Definition: text_frame.cc:68
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:56
impeller::SubpixelGlyph
A glyph and its subpixel position.
Definition: font_glyph_pair.h:54
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:52
impeller::TRect::GetLeftTop
constexpr TPoint< T > GetLeftTop() const
Definition: rect.h:353
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:123
buffer_view
BufferView buffer_view
Definition: blit_command_gles.cc:128
impeller::GlyphProperties::stroke_miter
Scalar stroke_miter
Definition: font_glyph_pair.h:25
impeller::TextFrame::RoundScaledFontSize
static Scalar RoundScaledFontSize(Scalar scale, Scalar point_size)
Definition: text_frame.cc:40
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:32
stroke_width
const Scalar stroke_width
Definition: stroke_path_geometry.cc:297
offset
SeparatedVector2 offset
Definition: stroke_path_geometry.cc:304
impeller::GlyphProperties::color
Color color
Definition: font_glyph_pair.h:21
impeller::RenderPass::Draw
virtual fml::Status Draw()
Record the currently pending command.
Definition: render_pass.cc:127
impeller::MinMagFilter::kNearest
@ kNearest
Select nearest to the sample point. Most widely supported.
impeller::SamplerDescriptor::mag_filter
MinMagFilter mag_filter
Definition: sampler_descriptor.h:17
impeller::VS
SolidFillVertexShader VS
Definition: stroke_path_geometry.cc:16
impeller::SamplerDescriptor
Definition: sampler_descriptor.h:15
impeller::PrimitiveType::kTriangle
@ kTriangle
impeller::Entity
Definition: entity.h:20
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:34
impeller::Matrix::Basis
constexpr Matrix Basis() const
The Matrix without its w components (without translation).
Definition: matrix.h:229
impeller::TSize
Definition: size.h:19
impeller::SamplerDescriptor::min_filter
MinMagFilter min_filter
Definition: sampler_descriptor.h:16
impeller::Point
TPoint< Scalar > Point
Definition: point.h:327
render_pass.h
impeller::ContentContext::GetContext
std::shared_ptr< Context > GetContext() const
Definition: content_context.cc:550
impeller::MinMagFilter::kLinear
@ kLinear
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:397
impeller::RenderPipelineHandle::VertexShader
VertexShader_ VertexShader
Definition: pipeline.h:106
type
GLenum type
Definition: blit_command_gles.cc:127
impeller::IndexType::kNone
@ kNone
Does not use the index buffer.
impeller::TRect::Scale
constexpr TRect Scale(Type scale) const
Definition: rect.h:196
impeller::SubpixelGlyph::glyph
Glyph glyph
Definition: font_glyph_pair.h:55
impeller::Font::Metrics::point_size
Scalar point_size
Definition: font.h:48
impeller::GlyphProperties::stroke_join
Join stroke_join
Definition: font_glyph_pair.h:24
impeller::GlyphProperties::stroke
bool stroke
Definition: font_glyph_pair.h:26
impeller::AxisAlignment::kX
@ kX
impeller::TextContents::~TextContents
~TextContents()
impeller::Font::GetMetrics
const Metrics & GetMetrics() const
Definition: font.cc:45
impeller::TextContents::SetOffset
void SetOffset(Vector2 offset)
Definition: text_contents.cc:44
impeller::SamplerDescriptor::mip_filter
MipFilter mip_filter
Definition: sampler_descriptor.h:18
impeller::TPoint::x
Type x
Definition: point.h:30
impeller::GlyphProperties::stroke_width
Scalar stroke_width
Definition: font_glyph_pair.h:22
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:33
sampler_descriptor.h
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91
content_context.h
impeller::BufferView
Definition: buffer_view.h:15
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:321
impeller::FontGlyphAtlas::FindGlyphBounds
std::optional< std::pair< Rect, Rect > > FindGlyphBounds(const SubpixelGlyph &glyph) const
Find the location of a glyph in the atlas.
Definition: glyph_atlas.cc:119
impeller::GlyphProperties::stroke_cap
Cap stroke_cap
Definition: font_glyph_pair.h:23
impeller::Join
Join
Definition: path.h:24
impeller::MipFilter::kBase
@ kBase
The texture is sampled as if it only had a single mipmap level.
impeller::TextContents::TextContents
TextContents()
impeller::TextContents::SetInheritedOpacity
void SetInheritedOpacity(Scalar opacity) override
Inherit the provided opacity.
Definition: text_contents.cc:40
impeller::ToVector
constexpr Vector4 ToVector(Color color)
Definition: shader_types.h:188
buffer_view.h
impeller::TPoint< Scalar >
impeller::TextRun::GlyphPosition
Definition: text_run.h:22
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:50
impeller::TextContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: text_contents.cc:76
impeller::TextContents::GetColor
Color GetColor() const
Definition: text_contents.cc:36
color.h
color
DlColor color
Definition: dl_golden_blur_unittests.cc:24
impeller::RenderPass::SetPipeline
virtual void SetPipeline(const std::shared_ptr< Pipeline< PipelineDescriptor >> &pipeline)
The pipeline to use for this command.
Definition: render_pass.cc:92
impeller::TextContents::SetTextFrame
void SetTextFrame(const std::shared_ptr< TextFrame > &frame)
Definition: text_contents.cc:28
glyph_atlas.h
impeller::AxisAlignment::kNone
@ kNone
impeller::ContentContext::GetGlyphAtlasPipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetGlyphAtlasPipeline(ContentContextOptions opts) const
Definition: content_context.h:497
impeller
Definition: allocation.cc:12
stroke
bool stroke
Definition: dl_golden_blur_unittests.cc:22
impeller::ContentContext
Definition: content_context.h:366
impeller::TRect< Scalar >
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::ContentContext::GetTransientsBuffer
HostBuffer & GetTransientsBuffer() const
Retrieve the currnent host buffer for transient storage.
Definition: content_context.h:753
impeller::Cap
Cap
Definition: path.h:18
impeller::Font::GetAxisAlignment
AxisAlignment GetAxisAlignment() const
Definition: font.cc:41
impeller::AxisAlignment::kY
@ kY