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"
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.
32  public:
33  TextShadowCache() = default;
34 
35  ~TextShadowCache() = default;
36 
37  /// @brief A key to look up cached glyph textures.
40  int64_t identifier;
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_
Describes a typeface along with any modifications to its intrinsic properties.
Definition: font.h:35
bool IsEqual(const Font &other) const override
Definition: font.cc:36
std::size_t GetHash() const override
Definition: font.cc:31
uint64_t GetHash() const
Definition: rational.cc:42
A cache for blurred text that re-uses these across frames.
void MarkFrameStart()
Mark all glyph textures as unused this frame.
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...
size_t GetCacheSizeForTesting() const
void MarkFrameEnd()
Remove all glyph textures that were not referenced at least once.
float Scalar
Definition: scalar.h:19
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
In filters that use Gaussian distributions, "sigma" is a size of one standard deviation in terms of t...
Definition: sigma.h:32
constexpr bool operator()(const TextShadowCacheKey &lhs, const TextShadowCacheKey &rhs) const
std::size_t operator()(const TextShadowCacheKey &key) const
A key to look up cached glyph textures.
TextShadowCacheKey(Scalar p_max_basis, int64_t p_identifier, bool p_is_single_glyph, const Font &p_font, Sigma p_sigma)