Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
flutter::RasterCache Class Reference

#include <raster_cache.h>

Inheritance diagram for flutter::RasterCache:
flutter::testing::MockRasterCache

Classes

struct  CacheInfo
 
struct  Context
 

Public Member Functions

std::unique_ptr< RasterCacheResultRasterize (const RasterCache::Context &context, sk_sp< const DlRTree > rtree, const std::function< void(DlCanvas *)> &draw_function, const std::function< void(DlCanvas *, const DlRect &rect)> &draw_checkerboard) const
 
 RasterCache (size_t access_threshold=3, size_t picture_and_display_list_cache_limit_per_frame=RasterCacheUtil::kDefaultPictureAndDisplayListCacheLimitPerFrame)
 
virtual ~RasterCache ()=default
 
bool Draw (const RasterCacheKeyID &id, DlCanvas &canvas, const DlPaint *paint, bool preserve_rtree=false) const
 
bool HasEntry (const RasterCacheKeyID &id, const SkMatrix &) const
 
void BeginFrame ()
 
void EvictUnusedCacheEntries ()
 
void EndFrame ()
 
void Clear ()
 
const RasterCacheMetricspicture_metrics () const
 
const RasterCacheMetricslayer_metrics () const
 
size_t GetCachedEntriesCount () const
 
size_t GetLayerCachedEntriesCount () const
 
size_t GetPictureCachedEntriesCount () const
 
size_t EstimatePictureCacheByteSize () const
 Estimate how much memory is used by picture raster cache entries in bytes.
 
size_t EstimateLayerCacheByteSize () const
 Estimate how much memory is used by layer raster cache entries in bytes.
 
size_t access_threshold () const
 Return the number of frames that a picture must be prepared before it will be cached. If the number is 0, then no picture will ever be cached.
 
bool GenerateNewCacheInThisFrame () const
 
CacheInfo MarkSeen (const RasterCacheKeyID &id, const SkMatrix &matrix, bool visible) const
 The entry whose RasterCacheKey is generated by RasterCacheKeyID and matrix is marked as encountered by the current frame. The entry will be created if it does not exist. Optionally the entry will be marked as visible in the current frame if the caller determines that it intersects the cull rect. The access_count of the entry will be increased if it is visible, or if it was ever visible.
 
int GetAccessCount (const RasterCacheKeyID &id, const SkMatrix &matrix) const
 
bool UpdateCacheEntry (const RasterCacheKeyID &id, const Context &raster_cache_context, const std::function< void(DlCanvas *)> &render_function, sk_sp< const DlRTree > rtree=nullptr) const
 

Friends

class RasterCacheItem
 
class LayerRasterCacheItem
 

Detailed Description

RasterCache is used to cache rasterized layers or display lists to improve performance.

Life cycle of RasterCache methods:

Definition at line 119 of file raster_cache.h.

Constructor & Destructor Documentation

◆ RasterCache()

flutter::RasterCache::RasterCache ( size_t  access_threshold = 3,
size_t  picture_and_display_list_cache_limit_per_frame = RasterCacheUtil::kDefaultPictureAndDisplayListCacheLimitPerFrame 
)
explicit

Definition at line 74 of file raster_cache.cc.

76 : access_threshold_(access_threshold),
77 display_list_cache_limit_per_frame_(display_list_cache_limit_per_frame) {}
size_t access_threshold() const
Return the number of frames that a picture must be prepared before it will be cached....

◆ ~RasterCache()

virtual flutter::RasterCache::~RasterCache ( )
virtualdefault

Member Function Documentation

◆ access_threshold()

size_t flutter::RasterCache::access_threshold ( ) const
inline

Return the number of frames that a picture must be prepared before it will be cached. If the number is 0, then no picture will ever be cached.

If the number is one, then it must be prepared and drawn on 1 frame and it will then be cached on the next frame if it is prepared.

Definition at line 217 of file raster_cache.h.

217{ return access_threshold_; }

Referenced by flutter::testing::MockRasterCache::AddMockPicture().

◆ BeginFrame()

void flutter::RasterCache::BeginFrame ( )

◆ Clear()

void flutter::RasterCache::Clear ( )

Definition at line 240 of file raster_cache.cc.

240 {
241 cache_.clear();
242 picture_metrics_ = {};
243 layer_metrics_ = {};
244}

◆ Draw()

bool flutter::RasterCache::Draw ( const RasterCacheKeyID id,
DlCanvas canvas,
const DlPaint paint,
bool  preserve_rtree = false 
) const

Definition at line 177 of file raster_cache.cc.

180 {
181 auto it = cache_.find(RasterCacheKey(id, ToSkMatrix(canvas.GetMatrix())));
182 if (it == cache_.end()) {
183 return false;
184 }
185
186 Entry& entry = it->second;
187
188 if (entry.image) {
189 entry.image->draw(canvas, paint, preserve_rtree);
190 return true;
191 }
192
193 return false;
194}
SkMatrix ToSkMatrix(const DlMatrix &matrix)

References flutter::DlCanvas::GetMatrix(), and flutter::ToSkMatrix().

Referenced by flutter::testing::TEST(), and flutter::testing::TEST().

◆ EndFrame()

void flutter::RasterCache::EndFrame ( )

◆ EstimateLayerCacheByteSize()

size_t flutter::RasterCache::EstimateLayerCacheByteSize ( ) const

Estimate how much memory is used by layer raster cache entries in bytes.

Only SkImage's memory usage is counted as other objects are often much smaller compared to SkImage. SkImageInfo::computeMinByteSize is used to estimate the SkImage memory usage.

Definition at line 283 of file raster_cache.cc.

283 {
284 size_t layer_cache_bytes = 0;
285 for (const auto& item : cache_) {
286 if (item.first.kind() == RasterCacheKeyKind::kLayerMetrics &&
287 item.second.image) {
288 layer_cache_bytes += item.second.image->image_bytes();
289 }
290 }
291 return layer_cache_bytes;
292}

References flutter::kLayerMetrics.

Referenced by flutter::TEST(), and flutter::TEST().

◆ EstimatePictureCacheByteSize()

size_t flutter::RasterCache::EstimatePictureCacheByteSize ( ) const

Estimate how much memory is used by picture raster cache entries in bytes.

Only SkImage's memory usage is counted as other objects are often much smaller compared to SkImage. SkImageInfo::computeMinByteSize is used to estimate the SkImage memory usage.

Definition at line 294 of file raster_cache.cc.

294 {
295 size_t picture_cache_bytes = 0;
296 for (const auto& item : cache_) {
297 if (item.first.kind() == RasterCacheKeyKind::kDisplayListMetrics &&
298 item.second.image) {
299 picture_cache_bytes += item.second.image->image_bytes();
300 }
301 }
302 return picture_cache_bytes;
303}

References flutter::kDisplayListMetrics.

Referenced by flutter::TEST(), flutter::TEST(), and flutter::testing::TEST().

◆ EvictUnusedCacheEntries()

void flutter::RasterCache::EvictUnusedCacheEntries ( )

Definition at line 215 of file raster_cache.cc.

215 {
216 std::vector<RasterCacheKey::Map<Entry>::iterator> dead;
217
218 for (auto it = cache_.begin(); it != cache_.end(); ++it) {
219 Entry& entry = it->second;
220 if (!entry.encountered_this_frame) {
221 dead.push_back(it);
222 }
223 }
224
225 for (auto it : dead) {
226 if (it->second.image) {
227 RasterCacheMetrics& metrics = GetMetricsForKind(it->first.kind());
228 metrics.eviction_count++;
229 metrics.eviction_bytes += it->second.image->image_bytes();
230 }
231 cache_.erase(it);
232 }
233}

References flutter::RasterCacheMetrics::eviction_bytes, and flutter::RasterCacheMetrics::eviction_count.

Referenced by flutter::LayerTree::Paint(), flutter::TEST(), flutter::TEST(), flutter::testing::TEST(), and flutter::testing::TEST().

◆ GenerateNewCacheInThisFrame()

bool flutter::RasterCache::GenerateNewCacheInThisFrame ( ) const
inline

Definition at line 219 of file raster_cache.h.

219 {
220 // Disabling caching when access_threshold is zero is historic behavior.
221 return access_threshold_ != 0 && display_list_cached_this_frame_ <
222 display_list_cache_limit_per_frame_;
223 }

◆ GetAccessCount()

int flutter::RasterCache::GetAccessCount ( const RasterCacheKeyID id,
const SkMatrix &  matrix 
) const

Returns the access count (i.e. accesses_since_visible) for the given entry in the cache, or -1 if no such entry exists.

Definition at line 158 of file raster_cache.cc.

159 {
160 RasterCacheKey key = RasterCacheKey(id, matrix);
161 auto entry = cache_.find(key);
162 if (entry != cache_.cend()) {
163 return entry->second.accesses_since_visible;
164 }
165 return -1;
166}

References key.

◆ GetCachedEntriesCount()

size_t flutter::RasterCache::GetCachedEntriesCount ( ) const

Definition at line 246 of file raster_cache.cc.

246 {
247 return cache_.size();
248}

Referenced by flutter::testing::TEST_F(), and flutter::testing::TEST_F().

◆ GetLayerCachedEntriesCount()

size_t flutter::RasterCache::GetLayerCachedEntriesCount ( ) const

Return the number of map entries in the layer cache regardless of whether the entries have been populated with an image.

Definition at line 250 of file raster_cache.cc.

250 {
251 size_t layer_cached_entries_count = 0;
252 for (const auto& item : cache_) {
253 if (item.first.kind() == RasterCacheKeyKind::kLayerMetrics) {
254 layer_cached_entries_count++;
255 }
256 }
257 return layer_cached_entries_count;
258}

References flutter::kLayerMetrics.

◆ GetPictureCachedEntriesCount()

size_t flutter::RasterCache::GetPictureCachedEntriesCount ( ) const

Return the number of map entries in the picture (DisplayList) cache regardless of whether the entries have been populated with an image.

Definition at line 260 of file raster_cache.cc.

260 {
261 size_t display_list_cached_entries_count = 0;
262 for (const auto& item : cache_) {
263 if (item.first.kind() == RasterCacheKeyKind::kDisplayListMetrics) {
264 display_list_cached_entries_count++;
265 }
266 }
267 return display_list_cached_entries_count;
268}

References flutter::kDisplayListMetrics.

◆ HasEntry()

bool flutter::RasterCache::HasEntry ( const RasterCacheKeyID id,
const SkMatrix &  matrix 
) const

Definition at line 168 of file raster_cache.cc.

169 {
170 RasterCacheKey key = RasterCacheKey(id, matrix);
171 if (cache_.find(key) != cache_.cend()) {
172 return true;
173 }
174 return false;
175}

References key.

◆ layer_metrics()

const RasterCacheMetrics & flutter::RasterCache::layer_metrics ( ) const
inline

Definition at line 173 of file raster_cache.h.

173{ return layer_metrics_; }

Referenced by flutter::FrameTimingsRecorder::RecordRasterEnd().

◆ MarkSeen()

RasterCache::CacheInfo flutter::RasterCache::MarkSeen ( const RasterCacheKeyID id,
const SkMatrix &  matrix,
bool  visible 
) const

The entry whose RasterCacheKey is generated by RasterCacheKeyID and matrix is marked as encountered by the current frame. The entry will be created if it does not exist. Optionally the entry will be marked as visible in the current frame if the caller determines that it intersects the cull rect. The access_count of the entry will be increased if it is visible, or if it was ever visible.

Returns
the number of times the entry has been hit since it was created. For a new entry that will be 1 if it is visible, or zero if non-visible.

Definition at line 145 of file raster_cache.cc.

147 {
148 RasterCacheKey key = RasterCacheKey(id, matrix);
149 Entry& entry = cache_[key];
150 entry.encountered_this_frame = true;
151 entry.visible_this_frame = visible;
152 if (visible || entry.accesses_since_visible > 0) {
153 entry.accesses_since_visible++;
154 }
155 return {entry.accesses_since_visible, entry.image != nullptr};
156}

References key.

◆ picture_metrics()

const RasterCacheMetrics & flutter::RasterCache::picture_metrics ( ) const
inline

Definition at line 172 of file raster_cache.h.

172{ return picture_metrics_; }

Referenced by flutter::FrameTimingsRecorder::RecordRasterEnd(), flutter::testing::TEST(), and flutter::testing::TEST().

◆ Rasterize()

std::unique_ptr< RasterCacheResult > flutter::RasterCache::Rasterize ( const RasterCache::Context context,
sk_sp< const DlRTree rtree,
const std::function< void(DlCanvas *)> &  draw_function,
const std::function< void(DlCanvas *, const DlRect &rect)> &  draw_checkerboard 
) const
Note
Procedure doesn't copy all closures.

Definition at line 80 of file raster_cache.cc.

85 {
86 auto matrix = RasterCacheUtil::GetIntegralTransCTM(context.matrix);
87 SkRect dest_rect =
88 RasterCacheUtil::GetRoundedOutDeviceBounds(context.logical_rect, matrix);
89
90 const SkImageInfo image_info = SkImageInfo::MakeN32Premul(
91 dest_rect.width(), dest_rect.height(), context.dst_color_space);
92
93 sk_sp<SkSurface> surface =
94 context.gr_context
95 ? SkSurfaces::RenderTarget(context.gr_context, skgpu::Budgeted::kYes,
96 image_info)
97 : SkSurfaces::Raster(image_info);
98
99 if (!surface) {
100 return nullptr;
101 }
102
103 DlSkCanvasAdapter canvas(surface->getCanvas());
104 canvas.Clear(DlColor::kTransparent());
105
106 canvas.Translate(-dest_rect.left(), -dest_rect.top());
107 canvas.Transform(ToDlMatrix(matrix));
108 draw_function(&canvas);
109
110 if (checkerboard_images_) {
111 draw_checkerboard(&canvas, ToDlRect(context.logical_rect));
112 }
113
114 auto image = DlImageSkia::Make(surface->makeImageSnapshot());
115 return std::make_unique<RasterCacheResult>(
116 image, context.logical_rect, context.flow_type, std::move(rtree));
117}
static sk_sp< DlImage > Make(const SkImage *image)
FlutterVulkanImage * image
VkSurfaceKHR surface
Definition main.cc:65
DlMatrix ToDlMatrix(const SkMatrix &matrix)
const DlRect & ToDlRect(const SkRect &rect)
static constexpr DlColor kTransparent()
Definition dl_color.h:68
static SkMatrix GetIntegralTransCTM(const SkMatrix &ctm)
Snap the translation components of the matrix to integers.
static SkRect GetRoundedOutDeviceBounds(const SkRect &rect, const SkMatrix &ctm)

References flutter::DlCanvas::Clear(), flutter::RasterCache::Context::dst_color_space, flutter::RasterCache::Context::flow_type, flutter::RasterCacheUtil::GetIntegralTransCTM(), flutter::RasterCacheUtil::GetRoundedOutDeviceBounds(), flutter::RasterCache::Context::gr_context, image, flutter::DlColor::kTransparent(), flutter::RasterCache::Context::logical_rect, flutter::DlImageSkia::Make(), flutter::RasterCache::Context::matrix, surface, flutter::ToDlMatrix(), flutter::ToDlRect(), flutter::DlSkCanvasAdapter::Transform(), and flutter::DlSkCanvasAdapter::Translate().

Referenced by UpdateCacheEntry().

◆ UpdateCacheEntry()

bool flutter::RasterCache::UpdateCacheEntry ( const RasterCacheKeyID id,
const Context raster_cache_context,
const std::function< void(DlCanvas *)> &  render_function,
sk_sp< const DlRTree rtree = nullptr 
) const

Definition at line 119 of file raster_cache.cc.

123 {
124 RasterCacheKey key = RasterCacheKey(id, raster_cache_context.matrix);
125 Entry& entry = cache_[key];
126 if (!entry.image) {
127 void (*func)(DlCanvas*, const DlRect& rect) = DrawCheckerboard;
128 entry.image = Rasterize(raster_cache_context, std::move(rtree),
129 render_function, func);
130 if (entry.image != nullptr) {
131 switch (id.type()) {
133 display_list_cached_this_frame_++;
134 break;
135 }
136 default:
137 break;
138 }
139 return true;
140 }
141 }
142 return entry.image != nullptr;
143}
std::unique_ptr< RasterCacheResult > Rasterize(const RasterCache::Context &context, sk_sp< const DlRTree > rtree, const std::function< void(DlCanvas *)> &draw_function, const std::function< void(DlCanvas *, const DlRect &rect)> &draw_checkerboard) const
impeller::Rect DlRect
void DrawCheckerboard(DlCanvas *canvas, const DlRect &rect)
flutter::DlCanvas DlCanvas
impeller::ShaderType type

References flutter::DrawCheckerboard(), flutter::kDisplayList, key, flutter::RasterCache::Context::matrix, Rasterize(), and type.

Referenced by flutter::testing::MockRasterCache::AddMockLayer(), and flutter::testing::MockRasterCache::AddMockPicture().

Friends And Related Symbol Documentation

◆ LayerRasterCacheItem

friend class LayerRasterCacheItem
friend

Definition at line 273 of file raster_cache.h.

◆ RasterCacheItem

friend class RasterCacheItem
friend

Definition at line 272 of file raster_cache.h.


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