| 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
5 #include "impeller/typographer/lazy_glyph_atlas.h"
6
7 #include "fml/logging.h"
8 #include "impeller/base/validation.h"
9 #include "impeller/typographer/glyph_atlas.h"
10 #include "impeller/typographer/typographer_context.h"
11
12 #include <memory>
13 #include <utility>
14
15 namespace impeller {
16
17 static const std::shared_ptr<GlyphAtlas> kNullGlyphAtlas = nullptr;
18
19 LazyGlyphAtlas::LazyGlyphAtlas(
20 std::shared_ptr<TypographerContext> typographer_context)
21 : typographer_context_(std::move(typographer_context)),
22 alpha_context_(typographer_context_
23 ? typographer_context_->CreateGlyphAtlasContext(
24 GlyphAtlas::Type::kAlphaBitmap)
25 : nullptr),
26 color_context_(typographer_context_
27 ? typographer_context_->CreateGlyphAtlasContext(
28 GlyphAtlas::Type::kColorBitmap)
29 : nullptr) {}
30
31 LazyGlyphAtlas::~LazyGlyphAtlas() = default;
32
33 void LazyGlyphAtlas::AddTextFrame(const TextFrame& frame,
34 Scalar scale,
35 Point offset,
36 const GlyphProperties& properties) {
37 FML_DCHECK(alpha_atlas_ == nullptr && color_atlas_ == nullptr);
38 if (frame.GetAtlasType() == GlyphAtlas::Type::kAlphaBitmap) {
39 frame.CollectUniqueFontGlyphPairs(alpha_glyph_map_, scale, offset,
40 properties);
41 } else {
42 frame.CollectUniqueFontGlyphPairs(color_glyph_map_, scale, offset,
43 properties);
44 }
45 }
46
47 void LazyGlyphAtlas::ResetTextFrames() {
48 alpha_glyph_map_.clear();
49 color_glyph_map_.clear();
50 alpha_atlas_.reset();
51 color_atlas_.reset();
52 }
53
54 const std::shared_ptr<GlyphAtlas>& LazyGlyphAtlas::CreateOrGetGlyphAtlas(
55 Context& context,
56 HostBuffer& host_buffer,
57 GlyphAtlas::Type type) const {
58 {
59 if (type == GlyphAtlas::Type::kAlphaBitmap && alpha_atlas_) {
60 return alpha_atlas_;
61 }
62 if (type == GlyphAtlas::Type::kColorBitmap && color_atlas_) {
63 return color_atlas_;
64 }
65 }
66
67 if (!typographer_context_) {
68 VALIDATION_LOG << "Unable to render text because a TypographerContext has "
69 "not been set.";
70 return kNullGlyphAtlas;
71 }
72 if (!typographer_context_->IsValid()) {
73 VALIDATION_LOG
74 << "Unable to render text because the TypographerContext is invalid.";
75 return kNullGlyphAtlas;
76 }
77
78 auto& glyph_map = type == GlyphAtlas::Type::kAlphaBitmap ? alpha_glyph_map_
79 : color_glyph_map_;
80 const std::shared_ptr<GlyphAtlasContext>& atlas_context =
81 type == GlyphAtlas::Type::kAlphaBitmap ? alpha_context_ : color_context_;
82 std::shared_ptr<GlyphAtlas> atlas = typographer_context_->CreateGlyphAtlas(
83 context, type, host_buffer, atlas_context, glyph_map);
84 if (!atlas || !atlas->IsValid()) {
85 VALIDATION_LOG << "Could not create valid atlas.";
86 return kNullGlyphAtlas;
87 }
88 if (type == GlyphAtlas::Type::kAlphaBitmap) {
89 alpha_atlas_ = std::move(atlas);
90 return alpha_atlas_;
91 }
92 if (type == GlyphAtlas::Type::kColorBitmap) {
93 color_atlas_ = std::move(atlas);
94 return color_atlas_;
95 }
96 FML_UNREACHABLE();
97 }
98
99 } // namespace impeller
impeller::GlyphAtlas::Type::kColorBitmap
@ kColorBitmap
impeller::LazyGlyphAtlas::~LazyGlyphAtlas
~LazyGlyphAtlas()
lazy_glyph_atlas.h
impeller::LazyGlyphAtlas::AddTextFrame
void AddTextFrame(const TextFrame &frame, Scalar scale, Point offset, const GlyphProperties &properties)
Definition: lazy_glyph_atlas.cc:33
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::LazyGlyphAtlas::ResetTextFrames
void ResetTextFrames()
Definition: lazy_glyph_atlas.cc:47
impeller::GlyphAtlas::Type::kAlphaBitmap
@ kAlphaBitmap
impeller::GlyphProperties
Definition: font_glyph_pair.h:20
impeller::TextFrame
Represents a collection of shaped text runs.
Definition: text_frame.h:19
impeller::HostBuffer
Definition: host_buffer.h:28
impeller::kNullGlyphAtlas
static const std::shared_ptr< GlyphAtlas > kNullGlyphAtlas
Definition: lazy_glyph_atlas.cc:17
typographer_context.h
validation.h
impeller::LazyGlyphAtlas::CreateOrGetGlyphAtlas
const std::shared_ptr< GlyphAtlas > & CreateOrGetGlyphAtlas(Context &context, HostBuffer &host_buffer, GlyphAtlas::Type type) const
Definition: lazy_glyph_atlas.cc:54
offset
SeparatedVector2 offset
Definition: stroke_path_geometry.cc:304
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:31
type
GLenum type
Definition: blit_command_gles.cc:127
impeller::TextFrame::CollectUniqueFontGlyphPairs
void CollectUniqueFontGlyphPairs(FontGlyphMap &glyph_map, Scalar scale, Point offset, const GlyphProperties &properties) const
Definition: text_frame.cc:87
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91
impeller::GlyphAtlas
A texture containing the bitmap representation of glyphs in different fonts along with the ability to...
Definition: glyph_atlas.h:27
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:30
impeller::TPoint< Scalar >
scale
const Scalar scale
Definition: stroke_path_geometry.cc:301
glyph_atlas.h
impeller
Definition: allocation.cc:12