| Flutter Impeller |

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, bool stroke, Scalar stroke_width, Cap stroke_cap, Join stroke_join, Scalar stroke_miter)
| Must be set after text frame. More...
Color | GetColor () const
bool | CanInheritOpacity (const Entity &entity) const override
| Whether or not this contents can accept the opacity peephole optimization. More...
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 | PopulateGlyphAtlas (const std::shared_ptr< LazyGlyphAtlas > &lazy_glyph_atlas, Scalar scale) override
| Add any text data to the specified lazy atlas. The scale parameter must be used again later when drawing the text. 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
| 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 ClipCoverage | GetClipCoverage (const Entity &entity, const std::optional< Rect > &current_clip_coverage) const
| Given the current pass space bounding rectangle of the clip buffer, return the expected clip coverage after this draw call. This should only be implemented for contents that may write to the clip buffer. 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, const std::string &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...
virtual bool | ShouldRender (const Entity &entity, const std::optional< Rect > clip_coverage) const
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 const FilterContents * | AsFilter () const
| Cast to a filter. Returns `nullptr` if this Contents is not a filter. More...
virtual bool | ApplyColorFilter (const ColorFilterProc &color_filter_proc)
| If possible, applies a color filter to this contents inputs on the CPU. More...

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

## Detailed Description

Definition at line 20 of file text_contents.h.

## Constructor & Destructor Documentation

## ◆ TextContents()

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

## ◆ ~TextContents()

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

## Member Function Documentation

## ◆ CanInheritOpacity()

bool impeller::TextContents::CanInheritOpacity | ( | const Entity & | entity | ) | const
| override virtual

Whether or not this contents can accept the opacity peephole optimization.

By default all contents return false. Contents are responsible for determining whether or not their own geometries intersect in a way that makes accepting opacity impossible. It is always safe to return false, especially if computing overlap would be computationally expensive.

Reimplemented from impeller::Contents.

Definition at line 40 of file text_contents.cc.

40 {
41 // Computing whether or not opacity can be inherited requires determining if
42 // any glyphs can overlap exactly. While this was previously implemented
43 // via TextFrame::MaybeHasOverlapping, this code relied on scaling up text
44 // bounds for a size specified at 1.0 DPR, which was not accurate at
45 // higher or lower DPRs. Rather than re-implement the checks to compute exact
46 // glyph bounds, for now this optimization has been disabled for Text.
47 return false;
48 }

## ◆ GetColor()

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

Definition at line 36 of file text_contents.cc.

36 {
37 return color_.WithAlpha(color_.alpha * inherited_opacity_);
38 }

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 62 of file text_contents.cc.

62 {
63 return frame_->GetBounds().TransformBounds(entity.GetTransform());
64 }

References impeller::Entity::GetTransform().

## ◆ GetTextFrameBounds()

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

## ◆ PopulateGlyphAtlas()

void impeller::TextContents::PopulateGlyphAtlas | ( | const std::shared_ptr< LazyGlyphAtlas > & | lazy_glyph_atlas,
|  | Scalar | scale
| ) |  |
| override virtual

Add any text data to the specified lazy atlas. The scale parameter must be used again later when drawing the text.

Reimplemented from impeller::Contents.

Definition at line 66 of file text_contents.cc.

68 {
69 lazy_glyph_atlas->AddTextFrame(*frame_, scale, offset_, properties_);
70 scale_ = scale;
71 }

References scale.

## ◆ Render()

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

Implements impeller::Contents.

Definition at line 93 of file text_contents.cc.

95 {
96 auto color = GetColor();
97 if (color.IsTransparent()) {
98 return true;
99 }
100
101 auto type = frame_->GetAtlasType();
102 const std::shared_ptr<GlyphAtlas>& atlas =
103 renderer.GetLazyGlyphAtlas()->CreateOrGetGlyphAtlas(
104 *renderer.GetContext(), renderer.GetTransientsBuffer(), type);
105
106 if (!atlas || !atlas->IsValid()) {
107 VALIDATION_LOG << "Cannot render glyphs without prepared atlas.";
108 return false;
109 }
110
111 // Information shared by all glyph draw calls.
112 pass.SetCommandLabel("TextFrame");
113 auto opts = OptionsFromPassAndEntity(pass, entity);
114 opts.primitive_type = PrimitiveType::kTriangle;
115 pass.SetPipeline(renderer.GetGlyphAtlasPipeline(opts));
116
117 using VS = GlyphAtlasPipeline::VertexShader;
118 using FS = GlyphAtlasPipeline::FragmentShader;
119
120 // Common vertex uniforms for all glyphs.
121 VS::FrameInfo frame_info;
122 frame_info.mvp =
123 Entity::GetShaderTransform(entity.GetShaderClipDepth(), pass, Matrix());
124 ISize atlas_size = atlas->GetTexture()->GetSize();
125 bool is_translation_scale = entity.GetTransform().IsTranslationScaleOnly();
126 Matrix entity_transform = entity.GetTransform();
127 Matrix basis_transform = entity_transform.Basis();
128
129 VS::BindFrameInfo(pass,
130 renderer.GetTransientsBuffer().EmplaceUniform(frame_info));
131
132 FS::FragInfo frag_info;
133 frag_info.use_text_color = force_text_color_ ? 1.0 : 0.0;
134 frag_info.text_color = ToVector(color.Premultiply());
135 frag_info.is_color_glyph = type == GlyphAtlas::Type::kColorBitmap;
136
137 FS::BindFragInfo(pass,
138 renderer.GetTransientsBuffer().EmplaceUniform(frag_info));
139
140 SamplerDescriptor sampler_desc;
141 if (is_translation_scale) {
142 sampler_desc.min_filter = MinMagFilter::kNearest;
143 sampler_desc.mag_filter = MinMagFilter::kNearest;
144 } else {
145 // Currently, we only propagate the scale of the transform to the atlas
146 // renderer, so if the transform has more than just a translation, we turn
147 // on linear sampling to prevent crunchiness caused by the pixel grid not
148 // being perfectly aligned.
149 // The downside is that this slightly over-blurs rotated/skewed text.
150 sampler_desc.min_filter = MinMagFilter::kLinear;
151 sampler_desc.mag_filter = MinMagFilter::kLinear;
152 }
153
154 // No mipmaps for glyph atlas (glyphs are generated at exact scales).
155 sampler_desc.mip_filter = MipFilter::kBase;
156
157 FS::BindGlyphAtlasSampler(
158 pass, // command
159 atlas->GetTexture(), // texture
160 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
161 sampler_desc) // sampler
162 );
163
164 // Common vertex information for all glyphs.
165 // All glyphs are given the same vertex information in the form of a
166 // unit-sized quad. The size of the glyph is specified in per instance data
167 // and the vertex shader uses this to size the glyph correctly. The
168 // interpolated vertex information is also used in the fragment shader to
169 // sample from the glyph atlas.
170
171 constexpr std::array<Point, 6> unit_points = {Point{0, 0}, Point{1, 0},
172 Point{0, 1}, Point{1, 0},
173 Point{0, 1}, Point{1, 1}};
174
175 auto& host_buffer = renderer.GetTransientsBuffer();
176 size_t vertex_count = 0;
177 for (const auto& run : frame_->GetRuns()) {
178 vertex_count += run.GetGlyphPositions().size();
179 }
180 vertex_count *= 6;
181
182 BufferView buffer_view = host_buffer.Emplace(
183 vertex_count * sizeof(VS::PerVertexData), alignof(VS::PerVertexData),
184 [&](uint8_t* contents) {
185 VS::PerVertexData vtx;
186 VS::PerVertexData* vtx_contents =
187 reinterpret_cast<VS::PerVertexData*>(contents);
188 size_t i = 0u;
189 for (const TextRun& run : frame_->GetRuns()) {
190 const Font& font = run.GetFont();
191 Scalar rounded_scale = TextFrame::RoundScaledFontSize(
192 scale_, font.GetMetrics().point_size);
193 const FontGlyphAtlas* font_atlas =
194 atlas->GetFontGlyphAtlas(font, rounded_scale);
195 if (!font_atlas) {
196 VALIDATION_LOG << "Could not find font in the atlas.";
197 continue;
198 }
199
200 // Adjust glyph position based on the subpixel rounding
201 // used by the font.
202 Point subpixel_adjustment(0.5, 0.5);
203 switch (font.GetAxisAlignment()) {
204 case AxisAlignment::kNone:
205 break;
206 case AxisAlignment::kX:
207 subpixel_adjustment.x = 0.125;
208 break;
209 case AxisAlignment::kY:
210 subpixel_adjustment.y = 0.125;
211 break;
212 case AxisAlignment::kAll:
213 subpixel_adjustment.x = 0.125;
214 subpixel_adjustment.y = 0.125;
215 break;
216 }
217
218 Point screen_offset = (entity_transform * Point(0, 0));
219 for (const TextRun::GlyphPosition& glyph_position :
220 run.GetGlyphPositions()) {
221 // Note: uses unrounded scale for more accurate subpixel position.
222 Point subpixel = TextFrame::ComputeSubpixelPosition(
223 glyph_position, font.GetAxisAlignment(), offset_, scale_);
224 std::optional<std::pair<Rect, Rect>> maybe_atlas_glyph_bounds =
225 font_atlas->FindGlyphBounds(
226 SubpixelGlyph{glyph_position.glyph, subpixel, properties_});
227 if (!maybe_atlas_glyph_bounds.has_value()) {
228 VALIDATION_LOG << "Could not find glyph position in the atlas.";
229 continue;
230 }
231 const Rect& atlas_glyph_bounds =
232 maybe_atlas_glyph_bounds.value().first;
233 Rect glyph_bounds = maybe_atlas_glyph_bounds.value().second;
234 Rect scaled_bounds = glyph_bounds.Scale(1.0 / rounded_scale);
235 // For each glyph, we compute two rectangles. One for the vertex
236 // positions and one for the texture coordinates (UVs). The atlas
237 // glyph bounds are used to compute UVs in cases where the
238 // destination and source sizes may differ due to clamping the sizes
239 // of large glyphs.
240 Point uv_origin =
241 (atlas_glyph_bounds.GetLeftTop() - Point(0.5, 0.5)) /
242 atlas_size;
243 Point uv_size =
244 (atlas_glyph_bounds.GetSize() + Point(1, 1)) / atlas_size;
245
246 Point unrounded_glyph_position =
247 basis_transform *
248 (glyph_position.position + scaled_bounds.GetLeftTop());
249
250 Point screen_glyph_position =
251 (screen_offset + unrounded_glyph_position + subpixel_adjustment)
252 .Floor();
253
254 for (const Point& point : unit_points) {
255 Point position;
256 if (is_translation_scale) {
257 position = (screen_glyph_position +
258 (basis_transform * point * scaled_bounds.GetSize()))
259 .Round();
260 } else {
261 position = entity_transform * (glyph_position.position +
262 scaled_bounds.GetLeftTop() +
263 point * scaled_bounds.GetSize());
264 }
265 vtx.uv = uv_origin + (uv_size * point);
266 vtx.position = position;
267 vtx_contents[i++] = vtx;
268 }
269 }
270 }
271 });
272
273 pass.SetVertexBuffer({
274 .vertex_buffer = std::move(buffer_view),
275 .index_buffer = {},
276 .vertex_count = vertex_count,
277 .index_type = IndexType::kNone,
278 });
279
280 return pass.Draw().ok();
281 }

References impeller::Matrix::Basis(), buffer_view, color, impeller::TextFrame::ComputeSubpixelPosition(), impeller::RenderPass::Draw(), impeller::HostBuffer::EmplaceUniform(), impeller::FontGlyphAtlas::FindGlyphBounds(), impeller::Font::GetAxisAlignment(), GetColor(), impeller::ContentContext::GetContext(), impeller::ContentContext::GetGlyphAtlasPipeline(), impeller::ContentContext::GetLazyGlyphAtlas(), impeller::TRect< T >::GetLeftTop(), impeller::Font::GetMetrics(), impeller::Entity::GetShaderClipDepth(), impeller::Entity::GetShaderTransform(), impeller::TRect< T >::GetSize(), impeller::Entity::GetTransform(), impeller::ContentContext::GetTransientsBuffer(), impeller::SubpixelGlyph::glyph, impeller::Matrix::IsTranslationScaleOnly(), impeller::kAll, impeller::kBase, impeller::GlyphAtlas::kColorBitmap, impeller::kLinear, impeller::kNearest, impeller::kNone, impeller::kTriangle, impeller::kX, impeller::kY, impeller::SamplerDescriptor::mag_filter, impeller::SamplerDescriptor::min_filter, impeller::SamplerDescriptor::mip_filter, impeller::OptionsFromPassAndEntity(), impeller::Font::Metrics::point_size, impeller::TPoint< T >::Round(), impeller::TextFrame::RoundScaledFontSize(), impeller::TRect< T >::Scale(), impeller::RenderPass::SetCommandLabel(), impeller::RenderPass::SetPipeline(), impeller::RenderPass::SetVertexBuffer(), impeller::ToVector(), type, VALIDATION_LOG, vtx, impeller::TPoint< T >::x, and impeller::TPoint< T >::y.

## ◆ SetColor()

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

Definition at line 32 of file text_contents.cc.

32 {
33 color_ = color;
34 }

References color.

## ◆ 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 58 of file text_contents.cc.

58 {
59 force_text_color_ = value;
60 }

## ◆ 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 50 of file text_contents.cc.

50 {
51 inherited_opacity_ = opacity;
52 }

## ◆ SetOffset()

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

Definition at line 54 of file text_contents.cc.

54 {
55 offset_ = offset;
56 }

References offset.

## ◆ SetScale()

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

Definition at line 65 of file text_contents.h.

65 { scale_ = scale; }

References scale.

## ◆ SetTextFrame()

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

Definition at line 28 of file text_contents.cc.

28 {
29 frame_ = frame;
30 }

## ◆ SetTextProperties()

void impeller::TextContents::SetTextProperties | ( | Color | color,
|  | bool | stroke,
|  | Scalar | stroke_width,
|  | Cap | stroke_cap,
|  | Join | stroke_join,
|  | Scalar | stroke_miter
| ) |  |

Must be set after text frame.

Definition at line 73 of file text_contents.cc.

78 {
79 if (frame_->HasColor()) {
80 // Alpha is always applied when rendering, remove it here so
81 // we do not double-apply the alpha.
82 properties_.color = color.WithAlpha(1.0);
83 }
84 if (stroke) {
85 properties_.stroke = true;
86 properties_.stroke_width = stroke_width;
87 properties_.stroke_cap = stroke_cap;
88 properties_.stroke_join = stroke_join;
89 properties_.stroke_miter = stroke_miter;
90 }
91 }

References impeller::GlyphProperties::color, color, stroke, impeller::GlyphProperties::stroke, impeller::GlyphProperties::stroke_cap, impeller::GlyphProperties::stroke_join, impeller::GlyphProperties::stroke_miter, impeller::GlyphProperties::stroke_width, and stroke_width.

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

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

impeller::ISize
ISize64 ISize
Definition: size.h:140
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:50
impeller::AxisAlignment::kAll
@ kAll
impeller::Scalar
float Scalar
Definition: scalar.h:18
vtx
SolidFillVertexShader::PerVertexData vtx
Definition: stroke_path_geometry.cc:312
impeller::TextFrame::ComputeSubpixelPosition
static Point ComputeSubpixelPosition(const TextRun::GlyphPosition &glyph_position, AxisAlignment alignment, Point offset, Scalar scale)
Definition: text_frame.cc:68
impeller::TPoint::Round
static constexpr TPoint Round(const TPoint< U > &other)
Definition: point.h:49
impeller::Color::alpha
Scalar alpha
Definition: color.h:143
buffer_view
BufferView buffer_view
Definition: blit_command_gles.cc:127
impeller::GlyphProperties::stroke_miter
Scalar stroke_miter
Definition: font_glyph_pair.h:24
impeller::TextFrame::RoundScaledFontSize
static Scalar RoundScaledFontSize(Scalar scale, Scalar point_size)
Definition: text_frame.cc:40
stroke_width
const Scalar stroke_width
Definition: stroke_path_geometry.cc:304
offset
SeparatedVector2 offset
Definition: stroke_path_geometry.cc:311
impeller::GlyphProperties::color
Color color
Definition: font_glyph_pair.h:20
impeller::MinMagFilter::kNearest
@ kNearest
Select nearest to the sample point. Most widely supported.
impeller::VS
SolidFillVertexShader VS
Definition: stroke_path_geometry.cc:16
impeller::PrimitiveType::kTriangle
@ kTriangle
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:34
impeller::Point
TPoint< Scalar > Point
Definition: point.h:322
impeller::MinMagFilter::kLinear
@ kLinear
impeller::Color::WithAlpha
constexpr Color WithAlpha(Scalar new_alpha) const
Definition: color.h:280
impeller::RenderPipelineHandle::VertexShader
VertexShader_ VertexShader
Definition: pipeline.h:106
type
GLenum type
Definition: blit_command_gles.cc:126
impeller::IndexType::kNone
@ kNone
Does not use the index buffer.
impeller::TRect::Scale
constexpr TRect Scale(Type scale) const
Definition: rect.h:192
impeller::GlyphProperties::stroke_join
Join stroke_join
Definition: font_glyph_pair.h:23
impeller::GlyphProperties::stroke
bool stroke
Definition: font_glyph_pair.h:25
impeller::Rect
TRect< Scalar > Rect
Definition: rect.h:769
impeller::AxisAlignment::kX
@ kX
impeller::GlyphProperties::stroke_width
Scalar stroke_width
Definition: font_glyph_pair.h:21
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:73
impeller::GlyphProperties::stroke_cap
Cap stroke_cap
Definition: font_glyph_pair.h:22
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:185
scale
const Scalar scale
Definition: stroke_path_geometry.cc:308
impeller::TextContents::GetColor
Color GetColor() const
Definition: text_contents.cc:36
color
DlColor color
Definition: dl_golden_blur_unittests.cc:23
impeller::AxisAlignment::kNone
@ kNone
stroke
bool stroke
Definition: dl_golden_blur_unittests.cc:21
impeller::AxisAlignment::kY
@ kY