Flutter Impeller
impeller::TextShadowCache Class Reference

A cache for blurred text that re-uses these across frames. More...

#include <text_shadow_cache.h>

Classes

struct  TextShadowCacheKey
 A key to look up cached glyph textures. More...
 

Public Member Functions

 TextShadowCache ()=default
 
 ~TextShadowCache ()=default
 
void MarkFrameStart ()
 Mark all glyph textures as unused this frame. More...
 
void MarkFrameEnd ()
 Remove all glyph textures that were not referenced at least once. More...
 
std::optional< EntityLookup (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 render. More...
 
size_t GetCacheSizeForTesting () const
 

Detailed Description

A cache for blurred text that re-uses these across frames.

Text shadows are generally stable, but expensive to compute as we use a full gaussian blur. This class caches these shadows by text blob identifier and holds them for at least one frame.

Additionally, there is an optimization for a single glyph (generally an Icon) that uses the content itself as a key.

If there was a cheaper method of text frame identity, or a per-glyph caching system this could be more efficient. As it exists, this mostly ameliorate severe performance degradation for glyph shadows but does not provide substantially better performance than Skia.

Definition at line 31 of file text_shadow_cache.h.

Constructor & Destructor Documentation

◆ TextShadowCache()

impeller::TextShadowCache::TextShadowCache ( )
default

◆ ~TextShadowCache()

impeller::TextShadowCache::~TextShadowCache ( )
default

Member Function Documentation

◆ GetCacheSizeForTesting()

size_t impeller::TextShadowCache::GetCacheSizeForTesting ( ) const
inline

Definition at line 87 of file text_shadow_cache.h.

87 { return entries_.size(); }

Referenced by impeller::testing::TEST_P().

◆ Lookup()

std::optional< Entity > impeller::TextShadowCache::Lookup ( const ContentContext renderer,
const Entity entity,
const std::shared_ptr< FilterContents > &  contents,
const TextShadowCacheKey text_key 
)

Lookup the entity in the cache with the given filter/text contents, returning the new entity to render.

If the entity is not present, render and place in the cache.

Definition at line 41 of file text_shadow_cache.cc.

45  {
46  auto it = entries_.find(text_key);
47 
48  if (it != entries_.end()) {
49  it->second.used_this_frame = true;
50  Entity cache_entity = it->second.entity.Clone();
51  cache_entity.SetClipDepth(entity.GetClipDepth());
52  cache_entity.SetTransform(entity.GetTransform() * it->second.key_matrix);
53  return cache_entity;
54  }
55 
56  std::optional<Rect> filter_coverage = contents->GetCoverage(entity);
57  if (!filter_coverage.has_value()) {
58  return std::nullopt;
59  }
60 
61  // Execute the filter to produce a snapshot that can be resued on subsequent
62  // frames. To prevent this texture from being re-used by the render target
63  // cache, we temporarily disable any RT caching.
64  renderer.GetRenderTargetCache()->DisableCache();
65  fml::ScopedCleanupClosure closure(
66  [&] { renderer.GetRenderTargetCache()->EnableCache(); });
67  std::optional<Entity> maybe_entity =
68  contents->GetEntity(renderer, entity, contents->GetCoverageHint());
69  if (!maybe_entity.has_value()) {
70  return std::nullopt;
71  }
72 
73  // The original entity has a transform matrix A. The snapshot entity has a
74  // transform matrix B. We need a function that converts A to B, so that if we
75  // render an entity with a slightly different transform matrix A', it appears
76  // in the correct position.
77  // A * K = B
78  // A-1 * A * K = A-1 * B
79  // K = A-1 * B
80  //
81  // The transform matrix K can be computed by inverse A times B. Multiplying
82  // any subsequent entity transforms by this matrix will correctly position
83  // them.
84  Matrix key_matrix =
85  entity.GetTransform().Invert() * maybe_entity->GetTransform();
86  entries_[text_key] =
87  TextShadowCacheData{.entity = maybe_entity.value().Clone(),
88  .used_this_frame = true,
89  .key_matrix = key_matrix};
90 
91  maybe_entity->SetClipDepth(entity.GetClipDepth());
92  return maybe_entity;
93 }

References impeller::Entity::Clone(), impeller::Entity::GetClipDepth(), impeller::Entity::GetCoverage(), impeller::ContentContext::GetRenderTargetCache(), impeller::Entity::GetTransform(), impeller::Matrix::Invert(), impeller::Entity::SetClipDepth(), and impeller::Entity::SetTransform().

◆ MarkFrameEnd()

void impeller::TextShadowCache::MarkFrameEnd ( )

Remove all glyph textures that were not referenced at least once.

Definition at line 36 of file text_shadow_cache.cc.

36  {
37  absl::erase_if(entries_,
38  [](const auto& pair) { return !pair.second.used_this_frame; });
39 }

◆ MarkFrameStart()

void impeller::TextShadowCache::MarkFrameStart ( )

Mark all glyph textures as unused this frame.

Definition at line 30 of file text_shadow_cache.cc.

30  {
31  for (auto& entry : entries_) {
32  entry.second.used_this_frame = false;
33  }
34 }

Referenced by impeller::DisplayListToTexture(), and impeller::RenderToTarget().


The documentation for this class was generated from the following files: