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_context_(typographer_context_
24  ? typographer_context_->CreateGlyphAtlasContext(
25  GlyphAtlas::Type::kAlphaBitmap)
26  : nullptr),
27  color_context_(typographer_context_
28  ? typographer_context_->CreateGlyphAtlasContext(
29  GlyphAtlas::Type::kColorBitmap)
30  : nullptr) {}
31 
33 
34 void LazyGlyphAtlas::AddTextFrame(const std::shared_ptr<TextFrame>& frame,
35  Rational scale,
36  Point offset,
37  const Matrix& transform,
38  std::optional<GlyphProperties> properties) {
39  frame->SetPerFrameData(scale, offset, transform, properties);
40  FML_DCHECK(alpha_atlas_ == nullptr && color_atlas_ == nullptr);
41  if (frame->GetAtlasType() == GlyphAtlas::Type::kAlphaBitmap) {
42  alpha_text_frames_.push_back(frame);
43  } else {
44  color_text_frames_.push_back(frame);
45  }
46 }
47 
49  alpha_text_frames_.clear();
50  color_text_frames_.clear();
51  alpha_atlas_.reset();
52  color_atlas_.reset();
53 }
54 
55 const std::shared_ptr<GlyphAtlas>& LazyGlyphAtlas::CreateOrGetGlyphAtlas(
56  Context& context,
57  HostBuffer& host_buffer,
58  GlyphAtlas::Type type) const {
59  {
60  if (type == GlyphAtlas::Type::kAlphaBitmap && alpha_atlas_) {
61  return alpha_atlas_;
62  }
63  if (type == GlyphAtlas::Type::kColorBitmap && color_atlas_) {
64  return color_atlas_;
65  }
66  }
67 
68  if (!typographer_context_) {
69  VALIDATION_LOG << "Unable to render text because a TypographerContext has "
70  "not been set.";
71  return kNullGlyphAtlas;
72  }
73  if (!typographer_context_->IsValid()) {
75  << "Unable to render text because the TypographerContext is invalid.";
76  return kNullGlyphAtlas;
77  }
78 
79  auto& glyph_map = type == GlyphAtlas::Type::kAlphaBitmap ? alpha_text_frames_
80  : color_text_frames_;
81  const std::shared_ptr<GlyphAtlasContext>& atlas_context =
82  type == GlyphAtlas::Type::kAlphaBitmap ? alpha_context_ : color_context_;
83  std::shared_ptr<GlyphAtlas> atlas = typographer_context_->CreateGlyphAtlas(
84  context, type, host_buffer, atlas_context, glyph_map);
85  if (!atlas || !atlas->IsValid()) {
86  VALIDATION_LOG << "Could not create valid atlas.";
87  return kNullGlyphAtlas;
88  }
90  alpha_atlas_ = std::move(atlas);
91  return alpha_atlas_;
92  }
94  color_atlas_ = std::move(atlas);
95  return color_atlas_;
96  }
97  FML_UNREACHABLE();
98 }
99 
100 } // namespace impeller
GLenum type
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:70
Type
Describes how the glyphs are represented in the texture.
Definition: glyph_atlas.h:74
const std::shared_ptr< GlyphAtlas > & CreateOrGetGlyphAtlas(Context &context, HostBuffer &host_buffer, GlyphAtlas::Type type) const
LazyGlyphAtlas(std::shared_ptr< TypographerContext > typographer_context)
void AddTextFrame(const std::shared_ptr< TextFrame > &frame, Rational scale, Point offset, const Matrix &transform, std::optional< GlyphProperties > properties)
static const std::shared_ptr< GlyphAtlas > kNullGlyphAtlas
Definition: comparable.h:95
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
#define VALIDATION_LOG
Definition: validation.h:91