Flutter Impeller
lazy_glyph_atlas.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 "fml/logging.h"
12 
13 #include <memory>
14 #include <utility>
15 
16 namespace impeller {
17 
18 static const std::shared_ptr<GlyphAtlas> kNullGlyphAtlas = nullptr;
19 
21  std::shared_ptr<TypographerContext> typographer_context)
22  : typographer_context_(std::move(typographer_context)),
23  alpha_data_(typographer_context_
24  ? typographer_context_->CreateGlyphAtlasContext(
25  GlyphAtlas::Type::kAlphaBitmap)
26  : nullptr),
27  color_data_(typographer_context_
28  ? typographer_context_->CreateGlyphAtlasContext(
29  GlyphAtlas::Type::kColorBitmap)
30  : nullptr) {}
31 
33 
35  const std::shared_ptr<TextFrame>& frame,
36  Point position,
37  const Matrix& transform,
38  const std::optional<GlyphProperties>& properties) {
39  FML_DCHECK(alpha_data_.atlas == nullptr && color_data_.atlas == nullptr);
40  AtlasData& data = GetData(frame->GetAtlasType());
41  data.renderable_frames.emplace_back(
42  frame, transform * Matrix::MakeTranslation(position), properties);
43 }
44 
46  alpha_data_.reset();
47  color_data_.reset();
48 }
49 
50 const std::shared_ptr<GlyphAtlas>& LazyGlyphAtlas::CreateOrGetGlyphAtlas(
51  Context& context,
52  HostBuffer& data_host_buffer,
53  GlyphAtlas::Type type) {
54  AtlasData& data = GetData(type);
55  if (data.atlas) {
56  return data.atlas;
57  }
58 
59  if (!typographer_context_) {
60  VALIDATION_LOG << "Unable to render text because a TypographerContext has "
61  "not been set.";
62  return kNullGlyphAtlas;
63  }
64  if (!typographer_context_->IsValid()) {
66  << "Unable to render text because the TypographerContext is invalid.";
67  return kNullGlyphAtlas;
68  }
69 
70  data.atlas = typographer_context_->CreateGlyphAtlas(
71  context, type, data_host_buffer, data.context, data.renderable_frames);
72  if (!data.atlas || !data.atlas->IsValid()) {
73  VALIDATION_LOG << "Could not create valid atlas.";
74  return kNullGlyphAtlas;
75  }
76  return data.atlas;
77 }
78 
79 LazyGlyphAtlas::AtlasData::AtlasData(std::shared_ptr<GlyphAtlasContext> context)
80  : context(std::move(context)) {}
81 
82 LazyGlyphAtlas::AtlasData::~AtlasData() = default;
83 
84 void LazyGlyphAtlas::AtlasData::reset() {
85  renderable_frames.clear();
86  atlas.reset();
87 }
88 
89 LazyGlyphAtlas::AtlasData& LazyGlyphAtlas::GetData(GlyphAtlas::Type type) {
90  switch (type) {
92  return alpha_data_;
94  return color_data_;
95  }
96  FML_UNREACHABLE();
97 }
98 
99 } // namespace impeller
To do anything rendering related with Impeller, you need a context.
Definition: context.h:65
A texture containing the bitmap representation of glyphs in different fonts along with the ability to...
Definition: glyph_atlas.h:37
Type
Describes how the glyphs are represented in the texture.
Definition: glyph_atlas.h:41
const std::shared_ptr< GlyphAtlas > & CreateOrGetGlyphAtlas(Context &context, HostBuffer &host_buffer, GlyphAtlas::Type type)
LazyGlyphAtlas(std::shared_ptr< TypographerContext > typographer_context)
void AddTextFrame(const std::shared_ptr< TextFrame > &frame, Point position, const Matrix &transform, const std::optional< GlyphProperties > &properties)
static const std::shared_ptr< GlyphAtlas > kNullGlyphAtlas
Definition: comparable.h:93
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
static constexpr Matrix MakeTranslation(const Vector3 &t)
Definition: matrix.h:95
#define VALIDATION_LOG
Definition: validation.h:91