| Flutter Impeller |

text_shadow_cache.h
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 #ifndef FLUTTER_IMPELLER_ENTITY_CONTENTS_TEXT_SHADOW_CACHE_H_
6 #define FLUTTER_IMPELLER_ENTITY_CONTENTS_TEXT_SHADOW_CACHE_H_
7
8 #include <cstdint>
9 #include <memory>
10
11 #include "impeller/entity/entity.h"
12 #include "impeller/geometry/scalar.h"
13 #include "impeller/geometry/sigma.h"
14 #include "third_party/abseil-cpp/absl/container/flat_hash_map.h"
15
16 namespace impeller {
17
18 /// @brief A cache for blurred text that re-uses these across frames.
19 ///
20 /// Text shadows are generally stable, but expensive to compute as we use a
21 /// full gaussian blur. This class caches these shadows by text blob identifier
22 /// and holds them for at least one frame.
23 ///
24 /// Additionally, there is an optimization for a single glyph (generally an
25 /// Icon) that uses the content itself as a key.
26 ///
27 /// If there was a cheaper method of text frame identity, or a per-glyph caching
28 /// system this could be more efficient. As it exists, this mostly ameliorate
29 /// severe performance degradation for glyph shadows but does not provide
30 /// substantially better performance than Skia.
31 class TextShadowCache {
32 public:
33 TextShadowCache() = default;
34
35 ~TextShadowCache() = default;
36
37 /// @brief A key to look up cached glyph textures.
38 struct TextShadowCacheKey {
39 Scalar max_basis;
40 int64_t identifier;
41 bool is_single_glyph;
42 Font font;
43 Rational rounded_sigma;
44 Color color;
45
46 TextShadowCacheKey(Scalar p_max_basis,
47 int64_t p_identifier,
48 bool p_is_single_glyph,
49 const Font& p_font,
50 Sigma p_sigma,
51 Color p_color);
52
53 struct Hash {
54 std::size_t operator()(const TextShadowCacheKey& key) const {
55 return fml::HashCombine(key.max_basis, key.identifier,
56 key.is_single_glyph, key.font.GetHash(),
57 key.rounded_sigma.GetHash(),
58 key.color.ToARGB());
59 }
60 };
61
62 struct Equal {
63 constexpr bool operator()(const TextShadowCacheKey& lhs,
64 const TextShadowCacheKey& rhs) const {
65 return lhs.max_basis == rhs.max_basis &&
66 lhs.identifier == rhs.identifier &&
67 lhs.is_single_glyph == rhs.is_single_glyph &&
68 lhs.font.IsEqual(rhs.font) &&
69 lhs.rounded_sigma == rhs.rounded_sigma && lhs.color == rhs.color;
70 }
71 };
72 };
73
74 /// @brief Mark all glyph textures as unused this frame.
75 void MarkFrameStart();
76
77 /// @brief Remove all glyph textures that were not referenced at least once.
78 void MarkFrameEnd();
79
80 /// @brief Lookup the entity in the cache with the given filter/text contents,
81 /// returning the new entity to render.
82 ///
83 /// If the entity is not present, render and place in the cache.
84 std::optional<Entity> Lookup(const ContentContext& renderer,
85 const Entity& entity,
86 const std::shared_ptr<FilterContents>& contents,
87 const TextShadowCacheKey&);
88
89 // Visible for testing.
90 size_t GetCacheSizeForTesting() const { return entries_.size(); }
91
92 private:
93 TextShadowCache(const TextShadowCache&) = delete;
94
95 TextShadowCache& operator=(const TextShadowCache&) = delete;
96
97 struct TextShadowCacheData {
98 Entity entity;
99 bool used_this_frame = true;
100 Matrix key_matrix;
101 };
102
103 absl::flat_hash_map<TextShadowCacheKey,
104 TextShadowCacheData,
105 TextShadowCacheKey::Hash,
106 TextShadowCacheKey::Equal>
107 entries_;
108 };
109
110 } // namespace impeller
111
112 #endif // FLUTTER_IMPELLER_ENTITY_CONTENTS_TEXT_SHADOW_CACHE_H_
impeller::ContentContext
Definition: content_context.h:122
impeller::Entity
Definition: entity.h:26
impeller::Font
Describes a typeface along with any modifications to its intrinsic properties.
Definition: font.h:36
impeller::Font::IsEqual
bool IsEqual(const Font &other) const override
Definition: font.cc:36
impeller::Font::GetHash
std::size_t GetHash() const override
Definition: font.cc:31
impeller::Rational
Definition: rational.h:13
impeller::Rational::GetHash
uint64_t GetHash() const
Definition: rational.cc:38
impeller::TextShadowCache
A cache for blurred text that re-uses these across frames.
Definition: text_shadow_cache.h:31
impeller::TextShadowCache::MarkFrameStart
void MarkFrameStart()
Mark all glyph textures as unused this frame.
Definition: text_shadow_cache.cc:32
impeller::TextShadowCache::~TextShadowCache
~TextShadowCache()=default
impeller::TextShadowCache::TextShadowCache
TextShadowCache()=default
impeller::TextShadowCache::Lookup
std::optional< Entity > Lookup(const ContentContext &renderer, const Entity &entity, const std::shared_ptr< FilterContents > &contents, const TextShadowCacheKey &)
Lookup the entity in the cache with the given filter/text contents, returning the new entity to rende...
Definition: text_shadow_cache.cc:43
impeller::TextShadowCache::GetCacheSizeForTesting
size_t GetCacheSizeForTesting() const
Definition: text_shadow_cache.h:90
impeller::TextShadowCache::MarkFrameEnd
void MarkFrameEnd()
Remove all glyph textures that were not referenced at least once.
Definition: text_shadow_cache.cc:38
entity.h
impeller
Definition: allocation.cc:12
impeller::Scalar
float Scalar
Definition: scalar.h:19
scalar.h
sigma.h
impeller::Color
Definition: color.h:124
impeller::Color::ToARGB
uint32_t ToARGB() const
Convert to ARGB 32 bit color.
Definition: color.h:259
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::Sigma
In filters that use Gaussian distributions, "sigma" is a size of one standard deviation in terms of t...
Definition: sigma.h:32
impeller::TextShadowCache::TextShadowCacheKey::Equal
Definition: text_shadow_cache.h:62
impeller::TextShadowCache::TextShadowCacheKey::Equal::operator()
constexpr bool operator()(const TextShadowCacheKey &lhs, const TextShadowCacheKey &rhs) const
Definition: text_shadow_cache.h:63
impeller::TextShadowCache::TextShadowCacheKey::Hash
Definition: text_shadow_cache.h:53
impeller::TextShadowCache::TextShadowCacheKey::Hash::operator()
std::size_t operator()(const TextShadowCacheKey &key) const
Definition: text_shadow_cache.h:54
impeller::TextShadowCache::TextShadowCacheKey
A key to look up cached glyph textures.
Definition: text_shadow_cache.h:38
impeller::TextShadowCache::TextShadowCacheKey::identifier
int64_t identifier
Definition: text_shadow_cache.h:40
impeller::TextShadowCache::TextShadowCacheKey::TextShadowCacheKey
TextShadowCacheKey(Scalar p_max_basis, int64_t p_identifier, bool p_is_single_glyph, const Font &p_font, Sigma p_sigma, Color p_color)
Definition: text_shadow_cache.cc:18
impeller::TextShadowCache::TextShadowCacheKey::max_basis
Scalar max_basis
Definition: text_shadow_cache.h:39
impeller::TextShadowCache::TextShadowCacheKey::is_single_glyph
bool is_single_glyph
Definition: text_shadow_cache.h:41
impeller::TextShadowCache::TextShadowCacheKey::rounded_sigma
Rational rounded_sigma
Definition: text_shadow_cache.h:43
impeller::TextShadowCache::TextShadowCacheKey::color
Color color
Definition: text_shadow_cache.h:44
impeller::TextShadowCache::TextShadowCacheKey::font
Font font
Definition: text_shadow_cache.h:42