Flutter Engine
The Flutter Engine
Public Member Functions | List of all members
impeller::TextContents Class Referencefinal

#include <text_contents.h>

Inheritance diagram for impeller::TextContents:
impeller::Contents

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< RectGetTextFrameBounds () const
 
std::optional< RectGetCoverage (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 ()
 
virtual void PopulateGlyphAtlas (const std::shared_ptr< LazyGlyphAtlas > &lazy_glyph_atlas, Scalar scale)
 Add any text data to the specified lazy atlas. The scale parameter must be used again later when drawing the text. More...
 
virtual bool Render (const ContentContext &renderer, const Entity &entity, RenderPass &pass) const =0
 
virtual std::optional< RectGetCoverage (const Entity &entity) const =0
 Get the area of the render pass that will be affected when this contents is rendered. More...
 
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< SnapshotRenderToSnapshot (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< SizeGetColorSourceSize () const
 Return the color source's intrinsic size, if available. More...
 
void SetColorSourceSize (Size size)
 
virtual bool CanInheritOpacity (const Entity &entity) const
 Whether or not this contents can accept the opacity peephole optimization. More...
 
virtual void SetInheritedOpacity (Scalar opacity)
 Inherit the provided opacity. More...
 
virtual std::optional< ColorAsBackgroundColor (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 FilterContentsAsFilter () 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< ContentsMakeAnonymous (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
overridevirtual

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}
Scalar alpha
Definition: color.h:143
constexpr Color WithAlpha(Scalar new_alpha) const
Definition: color.h:280

◆ GetCoverage()

std::optional< Rect > impeller::TextContents::GetCoverage ( const Entity entity) const
overridevirtual

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}

◆ GetTextFrameBounds()

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

◆ PopulateGlyphAtlas()

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

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}
const Scalar scale

◆ Render()

bool impeller::TextContents::Render ( const ContentContext renderer,
const Entity entity,
RenderPass pass 
) const
overridevirtual

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
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();
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()) {
205 break;
207 subpixel_adjustment.x = 0.125;
208 break;
210 subpixel_adjustment.y = 0.125;
211 break;
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.
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 // For each glyph, we compute two rectangles. One for the vertex
235 // positions and one for the texture coordinates (UVs). The atlas
236 // glyph bounds are used to compute UVs in cases where the
237 // destination and source sizes may differ due to clamping the sizes
238 // of large glyphs.
239 Point uv_origin =
240 (atlas_glyph_bounds.GetLeftTop() - Point(0.5, 0.5)) /
241 atlas_size;
242 Point uv_size =
243 (atlas_glyph_bounds.GetSize() + Point(1, 1)) / atlas_size;
244
245 Point unrounded_glyph_position =
246 (basis_transform * glyph_position.position) +
247 glyph_bounds.GetLeftTop();
248 Point screen_glyph_position =
249 (screen_offset + unrounded_glyph_position + subpixel_adjustment)
250 .Floor();
251
252 Size scaled_size = glyph_bounds.GetSize();
253 for (const Point& point : unit_points) {
254 Point position;
255 if (is_translation_scale) {
256 position = screen_glyph_position + (point * scaled_size);
257 } else {
258 Rect scaled_bounds = glyph_bounds.Scale(1.0 / rounded_scale);
259 position = entity_transform * (glyph_position.position +
260 scaled_bounds.GetLeftTop() +
261 point * scaled_bounds.GetSize());
262 }
263 vtx.uv = uv_origin + (uv_size * point);
264 vtx.position = position;
265 vtx_contents[i++] = vtx;
266 }
267 }
268 }
269 });
270
271 pass.SetVertexBuffer({
272 .vertex_buffer = std::move(buffer_view),
273 .index_buffer = {},
274 .vertex_count = vertex_count,
275 .index_type = IndexType::kNone,
276 });
277
278 return pass.Draw().ok();
279}
GLenum type
BufferView buffer_view
Matrix GetShaderTransform(const RenderPass &pass) const
Get the vertex shader transform used for drawing this Entity.
Definition: entity.cc:50
FragmentShader_ FragmentShader
Definition: pipeline.h:107
Color GetColor() const
static Scalar RoundScaledFontSize(Scalar scale, Scalar point_size)
Definition: text_frame.cc:40
static Point ComputeSubpixelPosition(const TextRun::GlyphPosition &glyph_position, AxisAlignment alignment, Point offset, Scalar scale)
Definition: text_frame.cc:68
DlColor color
sk_sp< const SkImage > atlas
Definition: SkRecords.h:331
font
Font Metadata and Metrics.
@ kNone
Does not use the index buffer.
float Scalar
Definition: scalar.h:18
@ kBase
The texture is sampled as if it only had a single mipmap level.
TRect< Scalar > Rect
Definition: rect.h:769
SolidFillVertexShader VS
TPoint< Scalar > Point
Definition: point.h:322
constexpr Vector4 ToVector(Color color)
Definition: shader_types.h:185
TSize< Scalar > Size
Definition: size.h:137
@ kNearest
Select nearest to the sample point. Most widely supported.
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:34
ISize64 ISize
Definition: size.h:140
Definition: run.py:1
SK_API sk_sp< PrecompileColorFilter > Matrix()
SolidFillVertexShader::PerVertexData vtx
constexpr TRect Scale(Type scale) const
Definition: rect.h:192
#define VALIDATION_LOG
Definition: validation.h:73

◆ SetColor()

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

Definition at line 32 of file text_contents.cc.

32 {
33 color_ = color;
34}

◆ 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}
uint8_t value

◆ SetInheritedOpacity()

void impeller::TextContents::SetInheritedOpacity ( Scalar  opacity)
overridevirtual

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}
SeparatedVector2 offset

◆ SetScale()

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

Definition at line 65 of file text_contents.h.

65{ scale_ = 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}
double frame
Definition: examples.cpp:31

◆ 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}
const Scalar stroke_width

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