Flutter Engine
 
Loading...
Searching...
No Matches
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
14#include "third_party/abseil-cpp/absl/container/flat_hash_map.h"
15
16namespace 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;
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 &&
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_
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
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.
constexpr std::size_t HashCombine()
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.