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 
6 
7 #include "fml/closure.h"
12 
13 namespace impeller {
14 
15 // Rounds sigma values for gaussian blur to nearest decimal.
16 static constexpr int32_t kMaxSigmaDenominator = 10;
17 
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),
30  color(p_color) {}
31 
33  for (auto& entry : entries_) {
34  entry.second.used_this_frame = false;
35  }
36 }
37 
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
const std::shared_ptr< RenderTargetAllocator > & GetRenderTargetCache() const
void SetTransform(const Matrix &transform)
Set the global transform matrix for this Entity.
Definition: entity.cc:62
std::optional< Rect > GetCoverage() const
Definition: entity.cc:66
void SetClipDepth(uint32_t clip_depth)
Definition: entity.cc:82
Entity Clone() const
Definition: entity.cc:159
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:46
uint32_t GetClipDepth() const
Definition: entity.cc:86
Describes a typeface along with any modifications to its intrinsic properties.
Definition: font.h:36
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...
void MarkFrameEnd()
Remove all glyph textures that were not referenced at least once.
float Scalar
Definition: scalar.h:19
static constexpr int32_t kMaxSigmaDenominator
Definition: comparable.h:95
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
Matrix Invert() const
Definition: matrix.cc:99
In filters that use Gaussian distributions, "sigma" is a size of one standard deviation in terms of t...
Definition: sigma.h:32
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, Color p_color)