| Flutter Impeller |

Public Member Functions | Static Public Member Functions | List of all members
impeller::TextContents Class Referencefinal

`#include <text_contents.h>`

Inheritance diagram for impeller::TextContents:

## Public Member Functions
| TextContents ()
| ~TextContents ()
void | SetTextFrame (const std::shared_ptr< TextFrame > &frame)
void | SetColor (Color color)
void | SetForceTextColor (bool value)
| Force the text color to apply to the rendered glyphs, even if those glyphs are bitmaps. More...
void | SetTextProperties (Color color, const std::optional< StrokeParameters > &stroke)
| Must be set after text frame. More...
Color | GetColor () const
void | SetInheritedOpacity (Scalar opacity) override
| Inherit the provided opacity. More...
void | SetOffset (Vector2 offset)
std::optional< Rect > | GetTextFrameBounds () const
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. More...
void | SetScale (Scalar scale)
bool | Render (const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Public Member Functions inherited from impeller::Contents
| Contents ()
virtual | ~Contents ()
void | SetCoverageHint (std::optional< Rect > coverage_hint)
| Hint that specifies the coverage area of this Contents that will actually be used during rendering. This is for optimization purposes only and can not be relied on as a clip. May optionally affect the result of `GetCoverage()`. More...
const std::optional< Rect > & | GetCoverageHint () const
virtual bool | IsOpaque (const Matrix &transform) const
| Whether this Contents only emits opaque source colors from the fragment stage. This value does not account for any entity properties (e.g. the blend mode), clips/visibility culling, or inherited opacity. More...
virtual std::optional< Snapshot > | RenderToSnapshot (const ContentContext &renderer, const Entity &entity, std::optional< Rect > coverage_limit=std::nullopt, const std::optional< SamplerDescriptor > &sampler_descriptor=std::nullopt, bool msaa_enabled=true, int32_t mip_count=1, std::string_view label="Snapshot") const
| Render this contents to a snapshot, respecting the entity's transform, path, clip depth, and blend mode. The result texture size is always the size of `GetCoverage(entity)`. More...
std::optional< Size > | GetColorSourceSize () const
| Return the color source's intrinsic size, if available. More...
void | SetColorSourceSize (Size size)
virtual std::optional< Color > | AsBackgroundColor (const Entity &entity, ISize target_size) const
| Returns a color if this Contents will flood the given `target_size` with a color. This output color is the "Source" color that will be used for the Entity's blend operation. More...
virtual bool | ApplyColorFilter (const ColorFilterProc &color_filter_proc)
| If possible, applies a color filter to this contents inputs on the CPU. More...

## Static Public Member Functions
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)
Static Public Member Functions inherited from impeller::Contents
static std::shared_ptr< Contents > | MakeAnonymous (RenderProc render_proc, CoverageProc coverage_proc)

## Additional Inherited Members
Public Types inherited from impeller::Contents
using | ColorFilterProc = std::function< Color(Color)>
using | RenderProc = std::function< bool(const ContentContext &renderer, const Entity &entity, RenderPass &pass)>
using | CoverageProc = std::function< std::optional< Rect >(const Entity &entity)>

## Detailed Description

Definition at line 23 of file text_contents.h.

## Constructor & Destructor Documentation

## ◆ TextContents()

impeller::TextContents::TextContents | ( |  | ) |
| default

## ◆ ~TextContents()

impeller::TextContents::~TextContents | ( |  | ) |
| default

## Member Function Documentation

## ◆ ComputeVertexData()

void impeller::TextContents::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
| ) |  |
| static

Definition at line 87 of file text_contents.cc.

94 {
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()) {
132 case AxisAlignment::kNone:
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 }
168 SubpixelPosition subpixel = TextFrame::ComputeSubpixelPosition(
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 }
impeller::TextFrame::ComputeSubpixelPosition
static SubpixelPosition ComputeSubpixelPosition(const TextRun::GlyphPosition &glyph_position, AxisAlignment alignment, const Matrix &transform)
Definition: text_frame.cc:94
transform
Matrix transform
Definition: gaussian_blur_filter_contents.cc:214
impeller::Scalar
float Scalar
Definition: scalar.h:19
impeller::Rect
TRect< Scalar > Rect
Definition: rect.h:788
impeller::Point
TPoint< Scalar > Point
Definition: point.h:327
impeller::AxisAlignment::kX
@ kX
impeller::AxisAlignment::kAll
@ kAll
impeller::AxisAlignment::kNone
@ kNone
impeller::AxisAlignment::kY
@ kY
impeller::SizeToPoint
Point SizeToPoint(Size size)
Definition: text_contents.cc:21
impeller::SubpixelPosition
SubpixelPosition
Definition: font_glyph_pair.h:56
impeller::ISize
ISize64 ISize
Definition: size.h:162
impeller::Matrix::m
Scalar m[16]
Definition: matrix.h:39
impeller::Matrix::MakeScale
static constexpr Matrix MakeScale(const Vector3 &s)
Definition: matrix.h:104
impeller::TPoint::Round
static constexpr TPoint Round(const TPoint< U > &other)
Definition: point.h:49
impeller::TRect::Scale
constexpr TRect Scale(Type scale) const
Definition: rect.h:202
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91

References impeller::FrameBounds::atlas_bounds, impeller::Matrix::Basis(), impeller::TextFrame::ComputeSubpixelPosition(), impeller::FontGlyphAtlas::FindGlyphBounds(), impeller::Font::GetAxisAlignment(), impeller::TRect< T >::GetLeftTop(), impeller::TRect< T >::GetSize(), impeller::SubpixelGlyph::glyph, impeller::FrameBounds::glyph_bounds, impeller::Rational::Invert(), impeller::FrameBounds::is_placeholder, impeller::Matrix::IsTranslationScaleOnly(), impeller::kAll, impeller::kNone, impeller::kX, impeller::kY, impeller::Matrix::m, impeller::Matrix::MakeScale(), impeller::TPoint< T >::Round(), impeller::TRect< T >::Scale(), impeller::SizeToPoint(), transform, VALIDATION_LOG, impeller::TPoint< T >::x, and impeller::TPoint< T >::y.

Referenced by Render(), and impeller::testing::TEST_P().

## ◆ GetColor()

Color impeller::TextContents::GetColor | ( |  | ) | const

Definition at line 40 of file text_contents.cc.

40 {
41 return color_.WithAlpha(color_.alpha * inherited_opacity_);
42 }
impeller::Color::alpha
Scalar alpha
Definition: color.h:143
impeller::Color::WithAlpha
constexpr Color WithAlpha(Scalar new_alpha) const
Definition: color.h:278

References impeller::Color::alpha, and impeller::Color::WithAlpha().

Referenced by Render().

## ◆ GetCoverage()

std::optional< Rect > impeller::TextContents::GetCoverage | ( | const Entity & | entity | ) | const
| override virtual

Get the area of the render pass that will be affected when this contents is rendered.

During rendering, coverage coordinates count pixels from the top left corner of the framebuffer.

Returns
  The coverage rectangle. An `std::nullopt` result means that rendering this contents has no effect on the output color.

Implements impeller::Contents.

Definition at line 56 of file text_contents.cc.

56 {
57 return frame_->GetBounds().TransformBounds(entity.GetTransform());
58 }

References impeller::Entity::GetTransform().

## ◆ GetTextFrameBounds()

std::optional<Rect> impeller::TextContents::GetTextFrameBounds | ( |  | ) | const

## ◆ Render()

bool impeller::TextContents::Render | ( | const ContentContext & | renderer,
|  | const Entity & | entity,
|  | RenderPass & | pass
| ) |  | const
| override virtual

Implements impeller::Contents.

Definition at line 220 of file text_contents.cc.

222 {
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.GetTransientsDataBuffer(), 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 =
251 Entity::GetShaderTransform(entity.GetShaderClipDepth(), pass, Matrix());
252 bool is_translation_scale = entity.GetTransform().IsTranslationScaleOnly();
253 Matrix entity_transform = entity.GetTransform();
254
255 VS::BindFrameInfo(
256 pass, renderer.GetTransientsDataBuffer().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(
264 pass, renderer.GetTransientsDataBuffer().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& data_host_buffer = renderer.GetTransientsDataBuffer();
291 HostBuffer& indexes_host_buffer = renderer.GetTransientsIndexesBuffer();
292 size_t glyph_count = 0;
293 for (const auto& run : frame_->GetRuns()) {
294 glyph_count += run.GetGlyphPositions().size();
295 }
296 size_t vertex_count = glyph_count * 4;
297 size_t index_count = glyph_count * 6;
298
299 BufferView buffer_view = data_host_buffer.Emplace(
300 vertex_count * sizeof(VS::PerVertexData), alignof(VS::PerVertexData),
301 [&](uint8_t* data) {
302 VS::PerVertexData* vtx_contents =
303 reinterpret_cast<VS::PerVertexData*>(data);
304 ComputeVertexData(/*vtx_contents=*/vtx_contents,
305 /*frame=*/frame_,
306 /*scale=*/scale_,
307 /*entity_transform=*/entity_transform,
308 /*offset=*/offset_,
309 /*glyph_properties=*/GetGlyphProperties(),
310 /*atlas=*/atlas);
311 });
312 BufferView index_buffer_view = indexes_host_buffer.Emplace(
313 index_count * sizeof(uint16_t), alignof(uint16_t), [&](uint8_t* data) {
314 uint16_t* indices = reinterpret_cast<uint16_t*>(data);
315 size_t j = 0;
316 for (auto i = 0u; i < glyph_count; i++) {
317 size_t base = i * 4;
318 indices[j++] = base + 0;
319 indices[j++] = base + 1;
320 indices[j++] = base + 2;
321 indices[j++] = base + 1;
322 indices[j++] = base + 2;
323 indices[j++] = base + 3;
324 }
325 });
326
327 pass.SetVertexBuffer(std::move(buffer_view));
328 pass.SetIndexBuffer(index_buffer_view, IndexType::k16bit);
329 pass.SetElementCount(index_count);
330
331 return pass.Draw().ok();
332 }
type
GLenum type
Definition: blit_command_gles.cc:153
buffer_view
BufferView buffer_view
Definition: blit_command_gles.cc:154
impeller::Entity::GetShaderTransform
Matrix GetShaderTransform(const RenderPass &pass) const
Definition: entity.cc:48
impeller::GlyphAtlas::Type
Type
Describes how the glyphs are represented in the texture.
Definition: glyph_atlas.h:41
impeller::GlyphAtlas::Type::kColorBitmap
@ kColorBitmap
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:87
impeller::TextContents::GetColor
Color GetColor() const
Definition: text_contents.cc:40
impeller::PrimitiveType::kTriangle
@ kTriangle
impeller::IndexType::k16bit
@ k16bit
impeller::MipFilter::kBase
@ kBase
The texture is sampled as if it only had a single mipmap level.
impeller::ToVector
constexpr Vector4 ToVector(Color color)
Definition: shader_types.h:190
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:34
impeller::MinMagFilter::kLinear
@ kLinear
impeller::MinMagFilter::kNearest
@ kNearest
Select nearest to the sample point. Most widely supported.
data
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:68

References buffer_view, ComputeVertexData(), data, impeller::RenderPass::Draw(), impeller::HostBuffer::Emplace(), impeller::HostBuffer::EmplaceUniform(), GetColor(), impeller::ContentContext::GetContext(), impeller::ContentContext::GetGlyphAtlasPipeline(), impeller::ContentContext::GetLazyGlyphAtlas(), impeller::Entity::GetShaderClipDepth(), impeller::Entity::GetShaderTransform(), impeller::Entity::GetTransform(), impeller::ContentContext::GetTransientsDataBuffer(), impeller::ContentContext::GetTransientsIndexesBuffer(), impeller::Matrix::IsTranslationScaleOnly(), impeller::Color::IsTransparent(), impeller::k16bit, impeller::kBase, impeller::GlyphAtlas::kColorBitmap, impeller::kLinear, impeller::kNearest, impeller::kTriangle, impeller::SamplerDescriptor::mag_filter, impeller::SamplerDescriptor::min_filter, impeller::SamplerDescriptor::mip_filter, impeller::OptionsFromPassAndEntity(), impeller::Color::Premultiply(), impeller::RenderPass::SetCommandLabel(), impeller::RenderPass::SetElementCount(), impeller::RenderPass::SetIndexBuffer(), impeller::RenderPass::SetPipeline(), impeller::RenderPass::SetVertexBuffer(), impeller::ToVector(), type, and VALIDATION_LOG.

Referenced by impeller::testing::TEST_P().

## ◆ SetColor()

void impeller::TextContents::SetColor | ( | Color | color | ) |

Definition at line 36 of file text_contents.cc.

36 {
37 color_ = color;
38 }

Referenced by impeller::testing::TEST_P().

## ◆ SetForceTextColor()

void impeller::TextContents::SetForceTextColor | ( | bool | value | ) |

Force the text color to apply to the rendered glyphs, even if those glyphs are bitmaps.

This is used to ensure that mask blurs work correctly on emoji.

Definition at line 52 of file text_contents.cc.

52 {
53 force_text_color_ = value;
54 }
value
int32_t value
Definition: dl_golden_unittests.cc:459

References value.

## ◆ SetInheritedOpacity()

void impeller::TextContents::SetInheritedOpacity | ( | Scalar | opacity | ) |
| override virtual

Inherit the provided opacity.

       Use of this method is invalid if CanAcceptOpacity returns false.

Reimplemented from impeller::Contents.

Definition at line 44 of file text_contents.cc.

44 {
45 inherited_opacity_ = opacity;
46 }

## ◆ SetOffset()

void impeller::TextContents::SetOffset | ( | Vector2 | offset | ) |

Definition at line 48 of file text_contents.cc.

48 {
49 offset_ = offset;
50 }

Referenced by impeller::testing::TEST_P().

## ◆ SetScale()

void impeller::TextContents::SetScale | ( | Scalar | scale | ) |
| inline

Definition at line 56 of file text_contents.h.

56 { scale_ = scale; }

Referenced by impeller::testing::TEST_P().

## ◆ SetTextFrame()

void impeller::TextContents::SetTextFrame | ( | const std::shared_ptr< TextFrame > & | frame | ) |

Definition at line 32 of file text_contents.cc.

32 {
33 frame_ = frame;
34 }

Referenced by impeller::testing::TEST_P().

## ◆ SetTextProperties()

void impeller::TextContents::SetTextProperties | ( | Color | color,
|  | const std::optional< StrokeParameters > & | stroke
| ) |  |

Must be set after text frame.

Definition at line 60 of file text_contents.cc.

62 {
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 }
impeller::GlyphProperties::color
Color color
Definition: font_glyph_pair.h:20
impeller::GlyphProperties::stroke
std::optional< StrokeParameters > stroke
Definition: font_glyph_pair.h:21

References impeller::GlyphProperties::color, impeller::GlyphProperties::stroke, and impeller::Color::WithAlpha().

The documentation for this class was generated from the following files:

- impeller/entity/contents/text_contents.h
- impeller/entity/contents/text_contents.cc