Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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.
 
Color GetColor () const
 
bool CanInheritOpacity (const Entity &entity) const override
 Whether or not this contents can accept the opacity peephole optimization.
 
void SetInheritedOpacity (Scalar opacity) override
 Inherit the provided opacity.
 
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.
 
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.
 
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().
 
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.
 
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.
 
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).
 
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.
 
void SetColorSourceSize (Size size)
 
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.
 
virtual const FilterContentsAsFilter () const
 Cast to a filter. Returns nullptr if this Contents is not a filter.
 
virtual bool ApplyColorFilter (const ColorFilterProc &color_filter_proc)
 If possible, applies a color filter to this contents inputs on the CPU.
 

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

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

◆ GetColor()

Color impeller::TextContents::GetColor ( ) const

Definition at line 34 of file text_contents.cc.

34 {
35 return color_.WithAlpha(color_.alpha * inherited_opacity_);
36}
Scalar alpha
Definition color.h:143
constexpr Color WithAlpha(Scalar new_alpha) const
Definition color.h:270

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

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

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

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

◆ Render()

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

Implements impeller::Contents.

Definition at line 65 of file text_contents.cc.

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

◆ SetColor()

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

Definition at line 30 of file text_contents.cc.

30 {
31 color_ = color;
32}

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

50 {
51 force_text_color_ = value;
52}
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 42 of file text_contents.cc.

42 {
43 inherited_opacity_ = opacity;
44}

◆ SetOffset()

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

Definition at line 46 of file text_contents.cc.

46 {
47 offset_ = offset;
48}

◆ SetScale()

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

Definition at line 56 of file text_contents.h.

56{ scale_ = scale; }

◆ SetTextFrame()

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

Definition at line 26 of file text_contents.cc.

26 {
27 frame_ = frame;
28}
double frame
Definition examples.cpp:31

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