Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 6e08d0c

Browse files
author
Jonah Williams
authored
[Impeller] remove shared_ptr copies during text contents rendering. (#49837)
We copied the shared ptr to make it easy to return nullptr, but instead we could use a static const assigned to nullptr
1 parent 924c172 commit 6e08d0c

11 files changed

+42
-49
lines changed

impeller/entity/contents/content_context.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -731,11 +731,11 @@ class ContentContext {
731731
const RenderTarget& subpass_target,
732732
const SubpassCallback& subpass_callback) const;
733733

734-
std::shared_ptr<LazyGlyphAtlas> GetLazyGlyphAtlas() const {
734+
const std::shared_ptr<LazyGlyphAtlas>& GetLazyGlyphAtlas() const {
735735
return lazy_glyph_atlas_;
736736
}
737737

738-
std::shared_ptr<RenderTargetAllocator> GetRenderTargetCache() const {
738+
const std::shared_ptr<RenderTargetAllocator>& GetRenderTargetCache() const {
739739
return render_target_cache_;
740740
}
741741

impeller/entity/contents/text_contents.cc

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,6 @@ void TextContents::SetTextFrame(const std::shared_ptr<TextFrame>& frame) {
2626
frame_ = frame;
2727
}
2828

29-
std::shared_ptr<GlyphAtlas> TextContents::ResolveAtlas(
30-
Context& context,
31-
GlyphAtlas::Type type,
32-
const std::shared_ptr<LazyGlyphAtlas>& lazy_atlas) const {
33-
FML_DCHECK(lazy_atlas);
34-
if (lazy_atlas) {
35-
return lazy_atlas->CreateOrGetGlyphAtlas(context, type);
36-
}
37-
38-
return nullptr;
39-
}
40-
4129
void TextContents::SetColor(Color color) {
4230
color_ = color;
4331
}
@@ -82,8 +70,9 @@ bool TextContents::Render(const ContentContext& renderer,
8270
}
8371

8472
auto type = frame_->GetAtlasType();
85-
auto atlas =
86-
ResolveAtlas(*renderer.GetContext(), type, renderer.GetLazyGlyphAtlas());
73+
const std::shared_ptr<GlyphAtlas>& atlas =
74+
renderer.GetLazyGlyphAtlas()->CreateOrGetGlyphAtlas(
75+
*renderer.GetContext(), type);
8776

8877
if (!atlas || !atlas->IsValid()) {
8978
VALIDATION_LOG << "Cannot render glyphs without prepared atlas.";

impeller/entity/contents/text_contents.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,8 @@
55
#ifndef FLUTTER_IMPELLER_ENTITY_CONTENTS_TEXT_CONTENTS_H_
66
#define FLUTTER_IMPELLER_ENTITY_CONTENTS_TEXT_CONTENTS_H_
77

8-
#include <functional>
98
#include <memory>
10-
#include <variant>
11-
#include <vector>
129

13-
#include "flutter/fml/macros.h"
1410
#include "impeller/entity/contents/contents.h"
1511
#include "impeller/geometry/color.h"
1612
#include "impeller/typographer/glyph_atlas.h"
@@ -70,11 +66,6 @@ class TextContents final : public Contents {
7066
Vector2 offset_;
7167
bool force_text_color_ = false;
7268

73-
std::shared_ptr<GlyphAtlas> ResolveAtlas(
74-
Context& context,
75-
GlyphAtlas::Type type,
76-
const std::shared_ptr<LazyGlyphAtlas>& lazy_atlas) const;
77-
7869
TextContents(const TextContents&) = delete;
7970

8071
TextContents& operator=(const TextContents&) = delete;

impeller/typographer/backends/skia/typographer_context_skia.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ static std::shared_ptr<Texture> UploadGlyphTextureAtlas(
313313
std::shared_ptr<GlyphAtlas> TypographerContextSkia::CreateGlyphAtlas(
314314
Context& context,
315315
GlyphAtlas::Type type,
316-
std::shared_ptr<GlyphAtlasContext> atlas_context,
316+
const std::shared_ptr<GlyphAtlasContext>& atlas_context,
317317
const FontGlyphMap& font_glyph_map) const {
318318
TRACE_EVENT0("impeller", __FUNCTION__);
319319
if (!IsValid()) {

impeller/typographer/backends/skia/typographer_context_skia.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#ifndef FLUTTER_IMPELLER_TYPOGRAPHER_BACKENDS_SKIA_TYPOGRAPHER_CONTEXT_SKIA_H_
66
#define FLUTTER_IMPELLER_TYPOGRAPHER_BACKENDS_SKIA_TYPOGRAPHER_CONTEXT_SKIA_H_
77

8-
#include "flutter/fml/macros.h"
98
#include "impeller/typographer/typographer_context.h"
109

1110
namespace impeller {
@@ -25,7 +24,7 @@ class TypographerContextSkia : public TypographerContext {
2524
std::shared_ptr<GlyphAtlas> CreateGlyphAtlas(
2625
Context& context,
2726
GlyphAtlas::Type type,
28-
std::shared_ptr<GlyphAtlasContext> atlas_context,
27+
const std::shared_ptr<GlyphAtlasContext>& atlas_context,
2928
const FontGlyphMap& font_glyph_map) const override;
3029

3130
private:

impeller/typographer/backends/stb/typographer_context_stb.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ static std::shared_ptr<Texture> UploadGlyphTextureAtlas(
375375
std::shared_ptr<GlyphAtlas> TypographerContextSTB::CreateGlyphAtlas(
376376
Context& context,
377377
GlyphAtlas::Type type,
378-
std::shared_ptr<GlyphAtlasContext> atlas_context,
378+
const std::shared_ptr<GlyphAtlasContext>& atlas_context,
379379
const FontGlyphMap& font_glyph_map) const {
380380
TRACE_EVENT0("impeller", __FUNCTION__);
381381
if (!IsValid()) {

impeller/typographer/backends/stb/typographer_context_stb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class TypographerContextSTB : public TypographerContext {
2727
std::shared_ptr<GlyphAtlas> CreateGlyphAtlas(
2828
Context& context,
2929
GlyphAtlas::Type type,
30-
std::shared_ptr<GlyphAtlasContext> atlas_context,
30+
const std::shared_ptr<GlyphAtlasContext>& atlas_context,
3131
const FontGlyphMap& font_glyph_map) const override;
3232

3333
private:

impeller/typographer/lazy_glyph_atlas.cc

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44

55
#include "impeller/typographer/lazy_glyph_atlas.h"
66

7+
#include "fml/logging.h"
78
#include "impeller/base/validation.h"
9+
#include "impeller/typographer/glyph_atlas.h"
810
#include "impeller/typographer/typographer_context.h"
911

1012
#include <utility>
1113

1214
namespace impeller {
1315

16+
static const std::shared_ptr<GlyphAtlas> kNullGlyphAtlas = nullptr;
17+
1418
LazyGlyphAtlas::LazyGlyphAtlas(
1519
std::shared_ptr<TypographerContext> typographer_context)
1620
: typographer_context_(std::move(typographer_context)),
@@ -24,7 +28,7 @@ LazyGlyphAtlas::LazyGlyphAtlas(
2428
LazyGlyphAtlas::~LazyGlyphAtlas() = default;
2529

2630
void LazyGlyphAtlas::AddTextFrame(const TextFrame& frame, Scalar scale) {
27-
FML_DCHECK(atlas_map_.empty());
31+
FML_DCHECK(alpha_atlas_ == nullptr && color_atlas_ == nullptr);
2832
if (frame.GetAtlasType() == GlyphAtlas::Type::kAlphaBitmap) {
2933
frame.CollectUniqueFontGlyphPairs(alpha_glyph_map_, scale);
3034
} else {
@@ -35,42 +39,52 @@ void LazyGlyphAtlas::AddTextFrame(const TextFrame& frame, Scalar scale) {
3539
void LazyGlyphAtlas::ResetTextFrames() {
3640
alpha_glyph_map_.clear();
3741
color_glyph_map_.clear();
38-
atlas_map_.clear();
42+
alpha_atlas_.reset();
43+
color_atlas_.reset();
3944
}
4045

41-
std::shared_ptr<GlyphAtlas> LazyGlyphAtlas::CreateOrGetGlyphAtlas(
46+
const std::shared_ptr<GlyphAtlas>& LazyGlyphAtlas::CreateOrGetGlyphAtlas(
4247
Context& context,
4348
GlyphAtlas::Type type) const {
4449
{
45-
auto atlas_it = atlas_map_.find(type);
46-
if (atlas_it != atlas_map_.end()) {
47-
return atlas_it->second;
50+
if (type == GlyphAtlas::Type::kAlphaBitmap && alpha_atlas_) {
51+
return alpha_atlas_;
52+
}
53+
if (type == GlyphAtlas::Type::kColorBitmap && color_atlas_) {
54+
return color_atlas_;
4855
}
4956
}
5057

5158
if (!typographer_context_) {
5259
VALIDATION_LOG << "Unable to render text because a TypographerContext has "
5360
"not been set.";
54-
return nullptr;
61+
return kNullGlyphAtlas;
5562
}
5663
if (!typographer_context_->IsValid()) {
5764
VALIDATION_LOG
5865
<< "Unable to render text because the TypographerContext is invalid.";
59-
return nullptr;
66+
return kNullGlyphAtlas;
6067
}
6168

6269
auto& glyph_map = type == GlyphAtlas::Type::kAlphaBitmap ? alpha_glyph_map_
6370
: color_glyph_map_;
64-
auto atlas_context =
71+
const std::shared_ptr<GlyphAtlasContext>& atlas_context =
6572
type == GlyphAtlas::Type::kAlphaBitmap ? alpha_context_ : color_context_;
66-
auto atlas = typographer_context_->CreateGlyphAtlas(context, type,
67-
atlas_context, glyph_map);
73+
std::shared_ptr<GlyphAtlas> atlas = typographer_context_->CreateGlyphAtlas(
74+
context, type, atlas_context, glyph_map);
6875
if (!atlas || !atlas->IsValid()) {
6976
VALIDATION_LOG << "Could not create valid atlas.";
70-
return nullptr;
77+
return kNullGlyphAtlas;
78+
}
79+
if (type == GlyphAtlas::Type::kAlphaBitmap) {
80+
alpha_atlas_ = std::move(atlas);
81+
return alpha_atlas_;
82+
}
83+
if (type == GlyphAtlas::Type::kColorBitmap) {
84+
color_atlas_ = std::move(atlas);
85+
return color_atlas_;
7186
}
72-
atlas_map_[type] = atlas;
73-
return atlas;
87+
FML_UNREACHABLE();
7488
}
7589

7690
} // namespace impeller

impeller/typographer/lazy_glyph_atlas.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class LazyGlyphAtlas {
2626

2727
void ResetTextFrames();
2828

29-
std::shared_ptr<GlyphAtlas> CreateOrGetGlyphAtlas(
29+
const std::shared_ptr<GlyphAtlas>& CreateOrGetGlyphAtlas(
3030
Context& context,
3131
GlyphAtlas::Type type) const;
3232

@@ -37,8 +37,8 @@ class LazyGlyphAtlas {
3737
FontGlyphMap color_glyph_map_;
3838
std::shared_ptr<GlyphAtlasContext> alpha_context_;
3939
std::shared_ptr<GlyphAtlasContext> color_context_;
40-
mutable std::unordered_map<GlyphAtlas::Type, std::shared_ptr<GlyphAtlas>>
41-
atlas_map_;
40+
mutable std::shared_ptr<GlyphAtlas> alpha_atlas_;
41+
mutable std::shared_ptr<GlyphAtlas> color_atlas_;
4242

4343
LazyGlyphAtlas(const LazyGlyphAtlas&) = delete;
4444

impeller/typographer/typographer_context.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class TypographerContext {
3535
virtual std::shared_ptr<GlyphAtlas> CreateGlyphAtlas(
3636
Context& context,
3737
GlyphAtlas::Type type,
38-
std::shared_ptr<GlyphAtlasContext> atlas_context,
38+
const std::shared_ptr<GlyphAtlasContext>& atlas_context,
3939
const FontGlyphMap& font_glyph_map) const = 0;
4040

4141
protected:

0 commit comments

Comments
 (0)