| 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 "flutter/third_party/abseil-cpp/absl/container/flat_hash_map.h"
12 #include "impeller/entity/entity.h"
13 #include "impeller/geometry/scalar.h"
14 #include "impeller/geometry/sigma.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
45 TextShadowCacheKey(Scalar p_max_basis,
46 int64_t p_identifier,
47 bool p_is_single_glyph,
48 const Font& p_font,
49 Sigma p_sigma);
50
51 struct Hash {
52 std::size_t operator()(const TextShadowCacheKey& key) const {
53 return fml::HashCombine(key.max_basis, key.identifier,
54 key.is_single_glyph, key.font.GetHash(),
55 key.rounded_sigma.GetHash());
56 }
57 };
58
59 struct Equal {
60 constexpr bool operator()(const TextShadowCacheKey& lhs,
61 const TextShadowCacheKey& rhs) const {
62 return lhs.max_basis == rhs.max_basis &&
63 lhs.identifier == rhs.identifier &&
64 lhs.is_single_glyph == rhs.is_single_glyph &&
65 lhs.font.IsEqual(rhs.font) &&
66 lhs.rounded_sigma == rhs.rounded_sigma;
67 }
68 };
69 };
70
71 /// @brief Mark all glyph textures as unused this frame.
72 void MarkFrameStart();
73
74 /// @brief Remove all glyph textures that were not referenced at least once.
75 void MarkFrameEnd();
76
77 /// @brief Lookup the entity in the cache with the given filter/text contents,
78 /// returning the new entity to render.
79 ///
80 /// If the entity is not present, render and place in the cache.
81 std::optional<Entity> Lookup(const ContentContext& renderer,
82 const Entity& entity,
83 const std::shared_ptr<FilterContents>& contents,
84 const TextShadowCacheKey&);
85
86 // Visible for testing.
87 size_t GetCacheSizeForTesting() const { return entries_.size(); }
88
89 private:
90 TextShadowCache(const TextShadowCache&) = delete;
91
92 TextShadowCache& operator=(const TextShadowCache&) = delete;
93
94 struct TextShadowCacheData {
95 Entity entity;
96 bool used_this_frame = true;
97 Matrix key_matrix;
98 };
99
100 absl::flat_hash_map<TextShadowCacheKey,
101 TextShadowCacheData,
102 TextShadowCacheKey::Hash,
103 TextShadowCacheKey::Equal>
104 entries_;
105 };
106
107 } // namespace impeller
108
109 #endif // FLUTTER_IMPELLER_ENTITY_CONTENTS_TEXT_SHADOW_CACHE_H_
impeller::ContentContext
Definition: content_context.h:138
impeller::Entity
Definition: entity.h:20
impeller::Font
Describes a typeface along with any modifications to its intrinsic properties.
Definition: font.h:35
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:42
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:30
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:41
impeller::TextShadowCache::GetCacheSizeForTesting
size_t GetCacheSizeForTesting() const
Definition: text_shadow_cache.h:87
impeller::TextShadowCache::MarkFrameEnd
void MarkFrameEnd()
Remove all glyph textures that were not referenced at least once.
Definition: text_shadow_cache.cc:36
entity.h
impeller
Definition: allocation.cc:12
impeller::Scalar
float Scalar
Definition: scalar.h:18
scalar.h
sigma.h
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:59
impeller::TextShadowCache::TextShadowCacheKey::Equal::operator()
constexpr bool operator()(const TextShadowCacheKey &lhs, const TextShadowCacheKey &rhs) const
Definition: text_shadow_cache.h:60
impeller::TextShadowCache::TextShadowCacheKey::Hash
Definition: text_shadow_cache.h:51
impeller::TextShadowCache::TextShadowCacheKey::Hash::operator()
std::size_t operator()(const TextShadowCacheKey &key) const
Definition: text_shadow_cache.h:52
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::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::TextShadowCacheKey
TextShadowCacheKey(Scalar p_max_basis, int64_t p_identifier, bool p_is_single_glyph, const Font &p_font, Sigma p_sigma)
Definition: text_shadow_cache.cc:18
impeller::TextShadowCache::TextShadowCacheKey::font
Font font
Definition: text_shadow_cache.h:42