| Flutter Impeller |

text_shadow_cache.cc
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 #include "impeller/entity/contents/text_shadow_cache.h"
6
7 #include "fml/closure.h"
8 #include "impeller/entity/contents/content_context.h"
9 #include "impeller/entity/contents/contents.h"
10 #include "impeller/entity/contents/filters/filter_contents.h"
11 #include "impeller/geometry/sigma.h"
12
13 namespace impeller {
14
15 // Rounds sigma values for gaussian blur to nearest decimal.
16 static constexpr int32_t kMaxSigmaDenominator = 10;
17
18 TextShadowCache::TextShadowCacheKey::TextShadowCacheKey(Scalar p_max_basis,
19 int64_t p_identifier,
20 bool p_is_single_glyph,
21 const Font& p_font,
22 Sigma p_sigma,
23 Color p_color)
24 : max_basis(p_max_basis),
25 identifier(p_identifier),
26 is_single_glyph(p_is_single_glyph),
27 font(p_font),
28 rounded_sigma(Rational(std::round(p_sigma.sigma * kMaxSigmaDenominator),
29 kMaxSigmaDenominator)),
30 color(p_color) {}
31
32 void TextShadowCache::MarkFrameStart() {
33 for (auto& entry : entries_) {
34 entry.second.used_this_frame = false;
35 }
36 }
37
38 void TextShadowCache::MarkFrameEnd() {
39 absl::erase_if(entries_,
40 [](const auto& pair) { return !pair.second.used_this_frame; });
41 }
42
43 std::optional<Entity> TextShadowCache::Lookup(
44 const ContentContext& renderer,
45 const Entity& entity,
46 const std::shared_ptr<FilterContents>& contents,
47 const TextShadowCacheKey& text_key) {
48 auto it = entries_.find(text_key);
49
50 if (it != entries_.end()) {
51 it->second.used_this_frame = true;
52 Entity cache_entity = it->second.entity.Clone();
53 cache_entity.SetClipDepth(entity.GetClipDepth());
54 cache_entity.SetTransform(entity.GetTransform() * it->second.key_matrix);
55 return cache_entity;
56 }
57
58 std::optional<Rect> filter_coverage = contents->GetCoverage(entity);
59 if (!filter_coverage.has_value()) {
60 return std::nullopt;
61 }
62
63 // Execute the filter to produce a snapshot that can be resued on subsequent
64 // frames. To prevent this texture from being re-used by the render target
65 // cache, we temporarily disable any RT caching.
66 renderer.GetRenderTargetCache()->DisableCache();
67 fml::ScopedCleanupClosure closure(
68 [&] { renderer.GetRenderTargetCache()->EnableCache(); });
69 std::optional<Entity> maybe_entity =
70 contents->GetEntity(renderer, entity, contents->GetCoverageHint());
71 if (!maybe_entity.has_value()) {
72 return std::nullopt;
73 }
74
75 // The original entity has a transform matrix A. The snapshot entity has a
76 // transform matrix B. We need a function that converts A to B, so that if we
77 // render an entity with a slightly different transform matrix A', it appears
78 // in the correct position.
79 // A * K = B
80 // A-1 * A * K = A-1 * B
81 // K = A-1 * B
82 //
83 // The transform matrix K can be computed by inverse A times B. Multiplying
84 // any subsequent entity transforms by this matrix will correctly position
85 // them.
86 Matrix key_matrix =
87 entity.GetTransform().Invert() * maybe_entity->GetTransform();
88 entries_[text_key] =
89 TextShadowCacheData{.entity = maybe_entity.value().Clone(),
90 .used_this_frame = true,
91 .key_matrix = key_matrix};
92
93 maybe_entity->SetClipDepth(entity.GetClipDepth());
94 return maybe_entity;
95 }
96
97 } // namespace impeller
impeller::ContentContext
Definition: content_context.h:122
impeller::ContentContext::GetRenderTargetCache
const std::shared_ptr< RenderTargetAllocator > & GetRenderTargetCache() const
Definition: content_context.h:258
impeller::Entity
Definition: entity.h:26
impeller::Entity::SetTransform
void SetTransform(const Matrix &transform)
Set the global transform matrix for this Entity.
Definition: entity.cc:62
impeller::Entity::GetCoverage
std::optional< Rect > GetCoverage() const
Definition: entity.cc:66
impeller::Entity::SetClipDepth
void SetClipDepth(uint32_t clip_depth)
Definition: entity.cc:82
impeller::Entity::Clone
Entity Clone() const
Definition: entity.cc:159
impeller::Entity::GetTransform
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:46
impeller::Entity::GetClipDepth
uint32_t GetClipDepth() const
Definition: entity.cc:86
impeller::Font
Describes a typeface along with any modifications to its intrinsic properties.
Definition: font.h:36
impeller::Rational
Definition: rational.h:13
impeller::TextShadowCache::MarkFrameStart
void MarkFrameStart()
Mark all glyph textures as unused this frame.
Definition: text_shadow_cache.cc:32
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::MarkFrameEnd
void MarkFrameEnd()
Remove all glyph textures that were not referenced at least once.
Definition: text_shadow_cache.cc:38
content_context.h
contents.h
filter_contents.h
impeller
Definition: allocation.cc:12
impeller::Scalar
float Scalar
Definition: scalar.h:19
impeller::kMaxSigmaDenominator
static constexpr int32_t kMaxSigmaDenominator
Definition: text_shadow_cache.cc:16
std
Definition: comparable.h:95
sigma.h
impeller::Color
Definition: color.h:124
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::Matrix::Invert
Matrix Invert() const
Definition: matrix.cc:99
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
A key to look up cached glyph textures.
Definition: text_shadow_cache.h:38
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
text_shadow_cache.h