| 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/text_frame.h"
11 #include "impeller/typographer/typographer_context.h"
12
13 #include <memory>
14 #include <utility>
15
16 namespace impeller {
17
18 static const std::shared_ptr<GlyphAtlas> kNullGlyphAtlas = nullptr;
19
20 LazyGlyphAtlas::LazyGlyphAtlas(
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
32 LazyGlyphAtlas::~LazyGlyphAtlas() = default;
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
48 void LazyGlyphAtlas::ResetTextFrames() {
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()) {
74 VALIDATION_LOG
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 }
89 if (type == GlyphAtlas::Type::kAlphaBitmap) {
90 alpha_atlas_ = std::move(atlas);
91 return alpha_atlas_;
92 }
93 if (type == GlyphAtlas::Type::kColorBitmap) {
94 color_atlas_ = std::move(atlas);
95 return color_atlas_;
96 }
97 FML_UNREACHABLE();
98 }
99
100 } // namespace impeller
type
GLenum type
Definition: blit_command_gles.cc:151
impeller::Context
To do anything rendering related with Impeller, you need a context.
Definition: context.h:65
impeller::GlyphAtlas
A texture containing the bitmap representation of glyphs in different fonts along with the ability to...
Definition: glyph_atlas.h:70
impeller::GlyphAtlas::Type
Type
Describes how the glyphs are represented in the texture.
Definition: glyph_atlas.h:74
impeller::GlyphAtlas::Type::kAlphaBitmap
@ kAlphaBitmap
impeller::GlyphAtlas::Type::kColorBitmap
@ kColorBitmap
impeller::HostBuffer
Definition: host_buffer.h:26
impeller::LazyGlyphAtlas::CreateOrGetGlyphAtlas
const std::shared_ptr< GlyphAtlas > & CreateOrGetGlyphAtlas(Context &context, HostBuffer &host_buffer, GlyphAtlas::Type type) const
Definition: lazy_glyph_atlas.cc:55
impeller::LazyGlyphAtlas::~LazyGlyphAtlas
~LazyGlyphAtlas()
impeller::LazyGlyphAtlas::ResetTextFrames
void ResetTextFrames()
Definition: lazy_glyph_atlas.cc:48
impeller::LazyGlyphAtlas::LazyGlyphAtlas
LazyGlyphAtlas(std::shared_ptr< TypographerContext > typographer_context)
Definition: lazy_glyph_atlas.cc:20
impeller::LazyGlyphAtlas::AddTextFrame
void AddTextFrame(const std::shared_ptr< TextFrame > &frame, Rational scale, Point offset, const Matrix &transform, std::optional< GlyphProperties > properties)
Definition: lazy_glyph_atlas.cc:34
impeller::Rational
Definition: rational.h:13
transform
Matrix transform
Definition: gaussian_blur_filter_contents.cc:214
glyph_atlas.h
lazy_glyph_atlas.h
impeller
Definition: allocation.cc:12
impeller::kNullGlyphAtlas
static const std::shared_ptr< GlyphAtlas > kNullGlyphAtlas
Definition: lazy_glyph_atlas.cc:18
std
Definition: comparable.h:95
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::TPoint< Scalar >
text_frame.h
typographer_context.h
validation.h
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91