Flutter Impeller
text_contents.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
6 
7 #include <cstring>
8 #include <optional>
9 #include <utility>
10 
11 #include "impeller/core/formats.h"
14 #include "impeller/entity/entity.h"
18 
19 namespace impeller {
20 
21 TextContents::TextContents() = default;
22 
23 TextContents::~TextContents() = default;
24 
25 void TextContents::SetTextFrame(const std::shared_ptr<TextFrame>& frame) {
26  frame_ = frame;
27 }
28 
29 std::shared_ptr<GlyphAtlas> TextContents::ResolveAtlas(
30  Context& context,
31  GlyphAtlas::Type type,
32  const std::shared_ptr<LazyGlyphAtlas>& lazy_atlas) const {
33  FML_DCHECK(lazy_atlas);
34  if (lazy_atlas) {
35  return lazy_atlas->CreateOrGetGlyphAtlas(context, type);
36  }
37 
38  return nullptr;
39 }
40 
42  color_ = color;
43 }
44 
46  return color_.WithAlpha(color_.alpha * inherited_opacity_);
47 }
48 
49 bool TextContents::CanInheritOpacity(const Entity& entity) const {
50  return !frame_->MaybeHasOverlapping();
51 }
52 
54  inherited_opacity_ = opacity;
55 }
56 
58  offset_ = offset;
59 }
60 
62  force_text_color_ = value;
63 }
64 
65 std::optional<Rect> TextContents::GetCoverage(const Entity& entity) const {
66  return frame_->GetBounds().TransformBounds(entity.GetTransform());
67 }
68 
70  const std::shared_ptr<LazyGlyphAtlas>& lazy_glyph_atlas,
71  Scalar scale) {
72  lazy_glyph_atlas->AddTextFrame(*frame_, scale);
73  scale_ = scale;
74 }
75 
76 bool TextContents::Render(const ContentContext& renderer,
77  const Entity& entity,
78  RenderPass& pass) const {
79  auto color = GetColor();
80  if (color.IsTransparent()) {
81  return true;
82  }
83 
84  auto type = frame_->GetAtlasType();
85  auto atlas =
86  ResolveAtlas(*renderer.GetContext(), type, renderer.GetLazyGlyphAtlas());
87 
88  if (!atlas || !atlas->IsValid()) {
89  VALIDATION_LOG << "Cannot render glyphs without prepared atlas.";
90  return false;
91  }
92 
93  // Information shared by all glyph draw calls.
94  Command cmd;
95  DEBUG_COMMAND_INFO(cmd, "TextFrame");
96  auto opts = OptionsFromPassAndEntity(pass, entity);
97  opts.primitive_type = PrimitiveType::kTriangle;
98  if (type == GlyphAtlas::Type::kAlphaBitmap) {
99  cmd.pipeline = renderer.GetGlyphAtlasPipeline(opts);
100  } else {
101  cmd.pipeline = renderer.GetGlyphAtlasColorPipeline(opts);
102  }
103  cmd.stencil_reference = entity.GetClipDepth();
104 
107 
108  // Common vertex uniforms for all glyphs.
109  VS::FrameInfo frame_info;
110  frame_info.mvp = pass.GetOrthographicTransform();
111  frame_info.atlas_size =
112  Vector2{static_cast<Scalar>(atlas->GetTexture()->GetSize().width),
113  static_cast<Scalar>(atlas->GetTexture()->GetSize().height)};
114  frame_info.offset = offset_;
115  frame_info.is_translation_scale =
117  frame_info.entity_transform = entity.GetTransform();
118  frame_info.text_color = ToVector(color.Premultiply());
119 
120  VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info));
121 
122  if (type == GlyphAtlas::Type::kColorBitmap) {
124  FSS::FragInfo frag_info;
125  frag_info.use_text_color = force_text_color_ ? 1.0 : 0.0;
126  FSS::BindFragInfo(cmd,
127  pass.GetTransientsBuffer().EmplaceUniform(frag_info));
128  }
129 
130  SamplerDescriptor sampler_desc;
131  if (frame_info.is_translation_scale) {
132  sampler_desc.min_filter = MinMagFilter::kNearest;
133  sampler_desc.mag_filter = MinMagFilter::kNearest;
134  } else {
135  // Currently, we only propagate the scale of the transform to the atlas
136  // renderer, so if the transform has more than just a translation, we turn
137  // on linear sampling to prevent crunchiness caused by the pixel grid not
138  // being perfectly aligned.
139  // The downside is that this slightly over-blurs rotated/skewed text.
140  sampler_desc.min_filter = MinMagFilter::kLinear;
141  sampler_desc.mag_filter = MinMagFilter::kLinear;
142  }
143  sampler_desc.mip_filter = MipFilter::kNearest;
144 
145  FS::BindGlyphAtlasSampler(
146  cmd, // command
147  atlas->GetTexture(), // texture
148  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
149  sampler_desc) // sampler
150  );
151 
152  // Common vertex information for all glyphs.
153  // All glyphs are given the same vertex information in the form of a
154  // unit-sized quad. The size of the glyph is specified in per instance data
155  // and the vertex shader uses this to size the glyph correctly. The
156  // interpolated vertex information is also used in the fragment shader to
157  // sample from the glyph atlas.
158 
159  constexpr std::array<Point, 6> unit_points = {Point{0, 0}, Point{1, 0},
160  Point{0, 1}, Point{1, 0},
161  Point{0, 1}, Point{1, 1}};
162 
163  auto& host_buffer = pass.GetTransientsBuffer();
164  size_t vertex_count = 0;
165  for (const auto& run : frame_->GetRuns()) {
166  vertex_count += run.GetGlyphPositions().size();
167  }
168  vertex_count *= 6;
169 
170  auto buffer_view = host_buffer.Emplace(
171  vertex_count * sizeof(VS::PerVertexData), alignof(VS::PerVertexData),
172  [&](uint8_t* contents) {
173  VS::PerVertexData vtx;
174  VS::PerVertexData* vtx_contents =
175  reinterpret_cast<VS::PerVertexData*>(contents);
176  for (const TextRun& run : frame_->GetRuns()) {
177  const Font& font = run.GetFont();
178  Scalar rounded_scale = TextFrame::RoundScaledFontSize(
179  scale_, font.GetMetrics().point_size);
180  const FontGlyphAtlas* font_atlas =
181  atlas->GetFontGlyphAtlas(font, rounded_scale);
182  if (!font_atlas) {
183  VALIDATION_LOG << "Could not find font in the atlas.";
184  continue;
185  }
186 
187  for (const TextRun::GlyphPosition& glyph_position :
188  run.GetGlyphPositions()) {
189  std::optional<Rect> maybe_atlas_glyph_bounds =
190  font_atlas->FindGlyphBounds(glyph_position.glyph);
191  if (!maybe_atlas_glyph_bounds.has_value()) {
192  VALIDATION_LOG << "Could not find glyph position in the atlas.";
193  continue;
194  }
195  const Rect& atlas_glyph_bounds = maybe_atlas_glyph_bounds.value();
196  vtx.atlas_glyph_bounds = Vector4(atlas_glyph_bounds.GetXYWH());
197  vtx.glyph_bounds = Vector4(glyph_position.glyph.bounds.GetXYWH());
198  vtx.glyph_position = glyph_position.position;
199 
200  for (const Point& point : unit_points) {
201  vtx.unit_position = point;
202  std::memcpy(vtx_contents++, &vtx, sizeof(VS::PerVertexData));
203  }
204  }
205  }
206  });
207 
208  cmd.BindVertices({
209  .vertex_buffer = buffer_view,
210  .index_buffer = {},
211  .vertex_count = vertex_count,
212  .index_type = IndexType::kNone,
213  });
214 
215  return pass.AddCommand(std::move(cmd));
216 }
217 
218 } // namespace impeller
text_contents.h
impeller::GlyphAtlas::Type::kColorBitmap
@ kColorBitmap
impeller::Command
An object used to specify work to the GPU along with references to resources the GPU will used when d...
Definition: command.h:92
DEBUG_COMMAND_INFO
#define DEBUG_COMMAND_INFO(obj, arg)
Definition: command.h:28
impeller::TextContents::CanInheritOpacity
bool CanInheritOpacity(const Entity &entity) const override
Whether or not this contents can accept the opacity peephole optimization.
Definition: text_contents.cc:49
lazy_glyph_atlas.h
impeller::ContentContext::GetGlyphAtlasColorPipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetGlyphAtlasColorPipeline(ContentContextOptions opts) const
Definition: content_context.h:501
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::Font
Describes a typeface along with any modifications to its intrinsic properties.
Definition: font.h:22
impeller::GlyphAtlas::Type::kAlphaBitmap
@ kAlphaBitmap
impeller::Entity::GetTransform
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:49
impeller::ContentContext::GetLazyGlyphAtlas
std::shared_ptr< LazyGlyphAtlas > GetLazyGlyphAtlas() const
Definition: content_context.h:724
entity.h
impeller::Color
Definition: color.h:124
impeller::FontGlyphAtlas
An object that can look up glyph locations within the GlyphAtlas for a particular typeface.
Definition: glyph_atlas.h:184
impeller::Vector4
Definition: vector.h:232
impeller::TextContents::SetForceTextColor
void SetForceTextColor(bool value)
Force the text color to apply to the rendered glyphs, even if those glyphs are bitmaps.
Definition: text_contents.cc:61
impeller::TextContents::GetCoverage
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.
Definition: text_contents.cc:65
formats.h
impeller::HostBuffer::Emplace
BufferView Emplace(const BufferType &buffer)
Emplace non-uniform data (like contiguous vertices) onto the host buffer.
Definition: host_buffer.h:85
impeller::RenderPipelineT::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline.h:93
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::TextRun
Represents a collection of positioned glyphs from a specific font.
Definition: text_run.h:20
impeller::RenderPass::GetOrthographicTransform
const Matrix & GetOrthographicTransform() const
Definition: render_pass.cc:51
impeller::TextContents::SetColor
void SetColor(Color color)
Definition: text_contents.cc:41
impeller::MinMagFilter::kNearest
@ kNearest
Select nearest to the sample point. Most widely supported.
impeller::SamplerDescriptor::mag_filter
MinMagFilter mag_filter
Definition: sampler_descriptor.h:17
impeller::SamplerDescriptor
Definition: sampler_descriptor.h:15
impeller::PrimitiveType::kTriangle
@ kTriangle
impeller::Entity
Definition: entity.h:21
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:28
impeller::MipFilter::kNearest
@ kNearest
Sample from the nearest mip level.
impeller::SamplerDescriptor::min_filter
MinMagFilter min_filter
Definition: sampler_descriptor.h:16
render_pass.h
impeller::TextContents::PopulateGlyphAtlas
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 draw...
Definition: text_contents.cc:69
impeller::ContentContext::GetContext
std::shared_ptr< Context > GetContext() const
Definition: content_context.cc:479
impeller::MinMagFilter::kLinear
@ kLinear
impeller::Color::WithAlpha
constexpr Color WithAlpha(Scalar new_alpha) const
Definition: color.h:270
impeller::Matrix::IsTranslationScaleOnly
constexpr bool IsTranslationScaleOnly() const
Returns true if the matrix has a scale-only basis and is non-projective. Note that an identity matrix...
Definition: matrix.h:360
impeller::GlyphAtlas::Type
Type
Describes how the glyphs are represented in the texture.
Definition: glyph_atlas.h:33
impeller::Command::BindVertices
bool BindVertices(VertexBuffer buffer)
Specify the vertex and index buffer to use for this command.
Definition: command.cc:15
impeller::IndexType::kNone
@ kNone
Does not use the index buffer.
impeller::Font::Metrics::point_size
Scalar point_size
Definition: font.h:35
impeller::TextContents::~TextContents
~TextContents()
impeller::Font::GetMetrics
const Metrics & GetMetrics() const
Definition: font.cc:37
impeller::TextContents::SetOffset
void SetOffset(Vector2 offset)
Definition: text_contents.cc:57
impeller::SamplerDescriptor::mip_filter
MipFilter mip_filter
Definition: sampler_descriptor.h:18
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:29
impeller::Entity::GetClipDepth
uint32_t GetClipDepth() const
Definition: entity.cc:93
sampler_descriptor.h
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:67
impeller::Command::stencil_reference
uint32_t stencil_reference
Definition: command.h:122
content_context.h
impeller::TextContents::TextContents
TextContents()
impeller::Context
To do anything rendering related with Impeller, you need a context.
Definition: context.h:47
impeller::TextContents::SetInheritedOpacity
void SetInheritedOpacity(Scalar opacity) override
Inherit the provided opacity.
Definition: text_contents.cc:53
impeller::ToVector
constexpr Vector4 ToVector(Color color)
Definition: shader_types.h:185
impeller::TPoint< Scalar >
impeller::TextRun::GlyphPosition
Definition: text_run.h:22
impeller::HostBuffer::EmplaceUniform
BufferView EmplaceUniform(const UniformType &uniform)
Emplace uniform data onto the host buffer. Ensure that backend specific uniform alignment requirement...
Definition: host_buffer.h:41
impeller::TextContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: text_contents.cc:76
impeller::TextContents::GetColor
Color GetColor() const
Definition: text_contents.cc:45
impeller::FontGlyphAtlas::FindGlyphBounds
std::optional< Rect > FindGlyphBounds(const Glyph &glyph) const
Find the location of a glyph in the atlas.
Definition: glyph_atlas.cc:111
impeller::TextContents::SetTextFrame
void SetTextFrame(const std::shared_ptr< TextFrame > &frame)
Definition: text_contents.cc:25
glyph_atlas.h
impeller::Command::pipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > pipeline
Definition: command.h:96
impeller::ContentContext::GetGlyphAtlasPipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetGlyphAtlasPipeline(ContentContextOptions opts) const
Definition: content_context.h:496
impeller::RenderPipelineT::VertexShader
VertexShader_ VertexShader
Definition: pipeline.h:92
impeller::RenderPass::AddCommand
bool AddCommand(Command &&command)
Record a command for subsequent encoding to the underlying command buffer. No work is encoded into th...
Definition: render_pass.cc:67
impeller
Definition: aiks_context.cc:10
impeller::TRect::GetXYWH
constexpr std::array< T, 4 > GetXYWH() const
Get the x, y coordinates of the origin and the width and height of the rectangle in an array.
Definition: rect.h:235
impeller::ContentContext
Definition: content_context.h:332
impeller::TRect< Scalar >
impeller::RenderPass::GetTransientsBuffer
HostBuffer & GetTransientsBuffer()
Definition: render_pass.cc:55