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"
11 
12 #include <memory>
13 #include <utility>
14 
15 namespace impeller {
16 
17 static const std::shared_ptr<GlyphAtlas> kNullGlyphAtlas = nullptr;
18 
20  std::shared_ptr<TypographerContext> typographer_context)
21  : typographer_context_(std::move(typographer_context)),
22  alpha_context_(typographer_context_
23  ? typographer_context_->CreateGlyphAtlasContext()
24  : nullptr),
25  color_context_(typographer_context_
26  ? typographer_context_->CreateGlyphAtlasContext()
27  : nullptr) {}
28 
30 
32  FML_DCHECK(alpha_atlas_ == nullptr && color_atlas_ == nullptr);
34  frame.CollectUniqueFontGlyphPairs(alpha_glyph_map_, scale);
35  } else {
36  frame.CollectUniqueFontGlyphPairs(color_glyph_map_, scale);
37  }
38 }
39 
41  alpha_glyph_map_.clear();
42  color_glyph_map_.clear();
43  alpha_atlas_.reset();
44  color_atlas_.reset();
45 }
46 
47 const std::shared_ptr<GlyphAtlas>& LazyGlyphAtlas::CreateOrGetGlyphAtlas(
48  Context& context,
49  GlyphAtlas::Type type) const {
50  {
51  if (type == GlyphAtlas::Type::kAlphaBitmap && alpha_atlas_) {
52  return alpha_atlas_;
53  }
54  if (type == GlyphAtlas::Type::kColorBitmap && color_atlas_) {
55  return color_atlas_;
56  }
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  auto& glyph_map = type == GlyphAtlas::Type::kAlphaBitmap ? alpha_glyph_map_
71  : color_glyph_map_;
72  const std::shared_ptr<GlyphAtlasContext>& atlas_context =
73  type == GlyphAtlas::Type::kAlphaBitmap ? alpha_context_ : color_context_;
74  std::shared_ptr<GlyphAtlas> atlas = typographer_context_->CreateGlyphAtlas(
75  context, type, atlas_context, glyph_map);
76  if (!atlas || !atlas->IsValid()) {
77  VALIDATION_LOG << "Could not create valid atlas.";
78  return kNullGlyphAtlas;
79  }
80  if (type == GlyphAtlas::Type::kAlphaBitmap) {
81  alpha_atlas_ = std::move(atlas);
82  return alpha_atlas_;
83  }
84  if (type == GlyphAtlas::Type::kColorBitmap) {
85  color_atlas_ = std::move(atlas);
86  return color_atlas_;
87  }
88  FML_UNREACHABLE();
89 }
90 
91 } // namespace impeller
impeller::GlyphAtlas::Type::kColorBitmap
@ kColorBitmap
impeller::LazyGlyphAtlas::~LazyGlyphAtlas
~LazyGlyphAtlas()
lazy_glyph_atlas.h
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::LazyGlyphAtlas::ResetTextFrames
void ResetTextFrames()
Definition: lazy_glyph_atlas.cc:40
impeller::GlyphAtlas::Type::kAlphaBitmap
@ kAlphaBitmap
impeller::TextFrame
Represents a collection of shaped text runs.
Definition: text_frame.h:20
impeller::kNullGlyphAtlas
static const std::shared_ptr< GlyphAtlas > kNullGlyphAtlas
Definition: lazy_glyph_atlas.cc:17
impeller::LazyGlyphAtlas::AddTextFrame
void AddTextFrame(const TextFrame &frame, Scalar scale)
Definition: lazy_glyph_atlas.cc:31
impeller::TextFrame::CollectUniqueFontGlyphPairs
void CollectUniqueFontGlyphPairs(FontGlyphMap &glyph_map, Scalar scale) const
Definition: text_frame.cc:70
typographer_context.h
validation.h
impeller::LazyGlyphAtlas::CreateOrGetGlyphAtlas
const std::shared_ptr< GlyphAtlas > & CreateOrGetGlyphAtlas(Context &context, GlyphAtlas::Type type) const
Definition: lazy_glyph_atlas.cc:47
impeller::LazyGlyphAtlas::LazyGlyphAtlas
LazyGlyphAtlas(std::shared_ptr< TypographerContext > typographer_context)
Definition: lazy_glyph_atlas.cc:19
impeller::GlyphAtlas::Type
Type
Describes how the glyphs are represented in the texture.
Definition: glyph_atlas.h:32
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:73
impeller::Context
To do anything rendering related with Impeller, you need a context.
Definition: context.h:46
std
Definition: comparable.h:95
impeller::TextFrame::GetAtlasType
GlyphAtlas::Type GetAtlasType() const
The type of atlas this run should be emplaced in.
Definition: text_frame.cc:28
scale
const Scalar scale
Definition: stroke_path_geometry.cc:297
glyph_atlas.h
impeller
Definition: aiks_blur_unittests.cc:20