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

37 {
38 return !frame_->MaybeHasOverlapping();
39 }

## ◆ GetColor()

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

Definition at line 33 of file text_contents.cc.

33 {
34 return color_.WithAlpha(color_.alpha * inherited_opacity_);
35 }

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

53 {
54 return frame_->GetBounds().TransformBounds(entity.GetTransform());
55 }

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

59 {
60 lazy_glyph_atlas->AddTextFrame(*frame_, scale);
61 scale_ = scale;
62 }

References scale.

## ◆ Render()

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

Implements impeller::Contents.

Definition at line 64 of file text_contents.cc.

66 {
67 auto color = GetColor();
68 if (color.IsTransparent()) {
69 return true;
70 }
71
72 auto type = frame_->GetAtlasType();
73 const std::shared_ptr<GlyphAtlas>& atlas =
74 renderer.GetLazyGlyphAtlas()->CreateOrGetGlyphAtlas(
75 *renderer.GetContext(), type);
76
77 if (!atlas || !atlas->IsValid()) {
78 VALIDATION_LOG << "Cannot render glyphs without prepared atlas.";
79 return false;
80 }
81
82 // Information shared by all glyph draw calls.
83 pass.SetCommandLabel("TextFrame");
84 auto opts = OptionsFromPassAndEntity(pass, entity);
85 opts.primitive_type = PrimitiveType::kTriangle;
86 if (type == GlyphAtlas::Type::kAlphaBitmap) {
87 pass.SetPipeline(renderer.GetGlyphAtlasPipeline(opts));
88 } else {
89 pass.SetPipeline(renderer.GetGlyphAtlasColorPipeline(opts));
90 }
91 pass.SetStencilReference(entity.GetClipDepth());
92
93 using VS = GlyphAtlasPipeline::VertexShader;
94 using FS = GlyphAtlasPipeline::FragmentShader;
95
96 // Common vertex uniforms for all glyphs.
97 VS::FrameInfo frame_info;
98 frame_info.mvp =
99 Entity::GetShaderTransform(entity.GetShaderClipDepth(), pass, Matrix());
100 frame_info.atlas_size =
101 Vector2{static_cast<Scalar>(atlas->GetTexture()->GetSize().width),
102 static_cast<Scalar>(atlas->GetTexture()->GetSize().height)};
103 frame_info.offset = offset_;
104 frame_info.is_translation_scale =
105 entity.GetTransform().IsTranslationScaleOnly();
106 frame_info.entity_transform = entity.GetTransform();
107 frame_info.text_color = ToVector(color.Premultiply());
108
109 VS::BindFrameInfo(pass,
110 renderer.GetTransientsBuffer().EmplaceUniform(frame_info));
111
112 if (type == GlyphAtlas::Type::kColorBitmap) {
113 using FSS = GlyphAtlasColorPipeline::FragmentShader;
114 FSS::FragInfo frag_info;
115 frag_info.use_text_color = force_text_color_ ? 1.0 : 0.0;
116 FSS::BindFragInfo(pass,
117 renderer.GetTransientsBuffer().EmplaceUniform(frag_info));
118 }
119
120 SamplerDescriptor sampler_desc;
121 if (frame_info.is_translation_scale) {
122 sampler_desc.min_filter = MinMagFilter::kNearest;
123 sampler_desc.mag_filter = MinMagFilter::kNearest;
124 } else {
125 // Currently, we only propagate the scale of the transform to the atlas
126 // renderer, so if the transform has more than just a translation, we turn
127 // on linear sampling to prevent crunchiness caused by the pixel grid not
128 // being perfectly aligned.
129 // The downside is that this slightly over-blurs rotated/skewed text.
130 sampler_desc.min_filter = MinMagFilter::kLinear;
131 sampler_desc.mag_filter = MinMagFilter::kLinear;
132 }
133 sampler_desc.mip_filter = MipFilter::kNearest;
134
135 FS::BindGlyphAtlasSampler(
136 pass, // command
137 atlas->GetTexture(), // texture
138 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
139 sampler_desc) // sampler
140 );
141
142 // Common vertex information for all glyphs.
143 // All glyphs are given the same vertex information in the form of a
144 // unit-sized quad. The size of the glyph is specified in per instance data
145 // and the vertex shader uses this to size the glyph correctly. The
146 // interpolated vertex information is also used in the fragment shader to
147 // sample from the glyph atlas.
148
149 constexpr std::array<Point, 6> unit_points = {Point{0, 0}, Point{1, 0},
150 Point{0, 1}, Point{1, 0},
151 Point{0, 1}, Point{1, 1}};
152
153 auto& host_buffer = renderer.GetTransientsBuffer();
154 size_t vertex_count = 0;
155 for (const auto& run : frame_->GetRuns()) {
156 vertex_count += run.GetGlyphPositions().size();
157 }
158 vertex_count *= 6;
159
160 auto buffer_view = host_buffer.Emplace(
161 vertex_count * sizeof(VS::PerVertexData), alignof(VS::PerVertexData),
162 [&](uint8_t* contents) {
163 VS::PerVertexData vtx;
164 VS::PerVertexData* vtx_contents =
165 reinterpret_cast<VS::PerVertexData*>(contents);
166 for (const TextRun& run : frame_->GetRuns()) {
167 const Font& font = run.GetFont();
168 Scalar rounded_scale = TextFrame::RoundScaledFontSize(
169 scale_, font.GetMetrics().point_size);
170 const FontGlyphAtlas* font_atlas =
171 atlas->GetFontGlyphAtlas(font, rounded_scale);
172 if (!font_atlas) {
173 VALIDATION_LOG << "Could not find font in the atlas.";
174 continue;
175 }
176
177 for (const TextRun::GlyphPosition& glyph_position :
178 run.GetGlyphPositions()) {
179 std::optional<Rect> maybe_atlas_glyph_bounds =
180 font_atlas->FindGlyphBounds(glyph_position.glyph);
181 if (!maybe_atlas_glyph_bounds.has_value()) {
182 VALIDATION_LOG << "Could not find glyph position in the atlas.";
183 continue;
184 }
185 const Rect& atlas_glyph_bounds = maybe_atlas_glyph_bounds.value();
186 vtx.atlas_glyph_bounds = Vector4(atlas_glyph_bounds.GetXYWH());
187 vtx.glyph_bounds = Vector4(glyph_position.glyph.bounds.GetXYWH());
188 vtx.glyph_position = glyph_position.position;
189
190 for (const Point& point : unit_points) {
191 vtx.unit_position = point;
192 std::memcpy(vtx_contents++, &vtx, sizeof(VS::PerVertexData));
193 }
194 }
195 }
196 });
197
198 pass.SetVertexBuffer({
199 .vertex_buffer = std::move(buffer_view),
200 .index_buffer = {},
201 .vertex_count = vertex_count,
202 .index_type = IndexType::kNone,
203 });
204
205 return pass.Draw().ok();
206 }

References impeller::RenderPass::Draw(), impeller::HostBuffer::Emplace(), impeller::HostBuffer::EmplaceUniform(), impeller::FontGlyphAtlas::FindGlyphBounds(), impeller::Entity::GetClipDepth(), GetColor(), impeller::ContentContext::GetContext(), impeller::ContentContext::GetGlyphAtlasColorPipeline(), impeller::ContentContext::GetGlyphAtlasPipeline(), impeller::ContentContext::GetLazyGlyphAtlas(), impeller::Font::GetMetrics(), impeller::Entity::GetShaderClipDepth(), impeller::Entity::GetShaderTransform(), impeller::Entity::GetTransform(), impeller::ContentContext::GetTransientsBuffer(), impeller::TRect< T >::GetXYWH(), impeller::Matrix::IsTranslationScaleOnly(), impeller::GlyphAtlas::kAlphaBitmap, impeller::GlyphAtlas::kColorBitmap, impeller::kLinear, impeller::kNearest, impeller::kNone, impeller::kTriangle, impeller::SamplerDescriptor::mag_filter, impeller::SamplerDescriptor::min_filter, impeller::SamplerDescriptor::mip_filter, impeller::OptionsFromPassAndEntity(), impeller::Font::Metrics::point_size, impeller::TextFrame::RoundScaledFontSize(), impeller::RenderPass::SetCommandLabel(), impeller::RenderPass::SetPipeline(), impeller::RenderPass::SetStencilReference(), impeller::RenderPass::SetVertexBuffer(), impeller::ToVector(), VALIDATION_LOG, and vtx.

## ◆ SetColor()

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

Definition at line 29 of file text_contents.cc.

29 {
30 color_ = color;
31 }

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

49 {
50 force_text_color_ = value;
51 }

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

41 {
42 inherited_opacity_ = opacity;
43 }

## ◆ SetOffset()

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

Definition at line 45 of file text_contents.cc.

45 {
46 offset_ = offset;
47 }

References offset.

## ◆ SetTextFrame()

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

Definition at line 25 of file text_contents.cc.

25 {
26 frame_ = frame;
27 }

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

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

impeller::GlyphAtlas::Type::kColorBitmap
@ kColorBitmap
impeller::Entity::GetShaderTransform
Matrix GetShaderTransform(const RenderPass &pass) const
Get the vertex shader transform used for drawing this Entity.
Definition: entity.cc:53
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::GlyphAtlas::Type::kAlphaBitmap
@ kAlphaBitmap
vtx
SolidFillVertexShader::PerVertexData vtx
Definition: stroke_path_geometry.cc:301
impeller::Vector2
Point Vector2
Definition: point.h:320
impeller::RenderPipelineT::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline.h:94
impeller::Color::alpha
Scalar alpha
Definition: color.h:143
impeller::TextFrame::RoundScaledFontSize
static Scalar RoundScaledFontSize(Scalar scale, Scalar point_size)
Definition: text_frame.cc:66
impeller::MinMagFilter::kNearest
@ kNearest
Select nearest to the sample point. Most widely supported.
impeller::VS
SolidFillVertexShader VS
Definition: stroke_path_geometry.cc:15
impeller::PrimitiveType::kTriangle
@ kTriangle
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:37
impeller::MipFilter::kNearest
@ kNearest
Sample from the nearest mip level.
impeller::Point
TPoint< Scalar > Point
Definition: point.h:316
impeller::MinMagFilter::kLinear
@ kLinear
impeller::Color::WithAlpha
constexpr Color WithAlpha(Scalar new_alpha) const
Definition: color.h:270
impeller::IndexType::kNone
@ kNone
Does not use the index buffer.
impeller::Rect
TRect< Scalar > Rect
Definition: rect.h:661
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:73
impeller::ToVector
constexpr Vector4 ToVector(Color color)
Definition: shader_types.h:185
scale
const Scalar scale
Definition: stroke_path_geometry.cc:297
impeller::TextContents::GetColor
Color GetColor() const
Definition: text_contents.cc:33
offset
Point offset
Definition: stroke_path_geometry.cc:300
impeller::RenderPipelineT::VertexShader
VertexShader_ VertexShader
Definition: pipeline.h:93