@@ -178,8 +178,8 @@ std::unique_ptr<RasterCacheResult> RasterCache::RasterizeDisplayList(
178178void RasterCache::Prepare (PrerollContext* context,
179179 Layer* layer,
180180 const SkMatrix& ctm) {
181- LayerRasterCacheKey cache_key (layer->unique_id (), ctm);
182- Entry& entry = layer_cache_ [cache_key];
181+ RasterCacheKey cache_key (layer->unique_id (), RasterCacheKeyType:: kLayer , ctm);
182+ Entry& entry = cache_ [cache_key];
183183 entry.access_count ++;
184184 entry.used_this_frame = true ;
185185 if (!entry.image ) {
@@ -242,10 +242,11 @@ bool RasterCache::Prepare(PrerollContext* context,
242242 return false ;
243243 }
244244
245- PictureRasterCacheKey cache_key (picture->uniqueID (), transformation_matrix);
245+ RasterCacheKey cache_key (picture->uniqueID (), RasterCacheKeyType::kPicture ,
246+ transformation_matrix);
246247
247248 // Creates an entry, if not present prior.
248- Entry& entry = picture_cache_ [cache_key];
249+ Entry& entry = cache_ [cache_key];
249250 if (entry.access_count < access_threshold_) {
250251 // Frame threshold has not yet been reached.
251252 return false ;
@@ -295,11 +296,12 @@ bool RasterCache::Prepare(PrerollContext* context,
295296 return false ;
296297 }
297298
298- DisplayListRasterCacheKey cache_key (display_list->unique_id (),
299- transformation_matrix);
299+ RasterCacheKey cache_key (display_list->unique_id (),
300+ RasterCacheKeyType::kDisplayList ,
301+ transformation_matrix);
300302
301303 // Creates an entry, if not present prior.
302- Entry& entry = display_list_cache_ [cache_key];
304+ Entry& entry = cache_ [cache_key];
303305 if (entry.access_count < access_threshold_) {
304306 // Frame threshold has not yet been reached.
305307 return false ;
@@ -321,30 +323,28 @@ bool RasterCache::Prepare(PrerollContext* context,
321323}
322324
323325void RasterCache::Touch (Layer* layer, const SkMatrix& ctm) {
324- LayerRasterCacheKey cache_key (layer->unique_id (), ctm);
325- auto it = layer_cache_.find (cache_key);
326- if (it != layer_cache_.end ()) {
327- it->second .used_this_frame = true ;
328- it->second .access_count ++;
329- }
326+ RasterCacheKey cache_key (layer->unique_id (), RasterCacheKeyType::kLayer , ctm);
327+ Touch (cache_key);
330328}
331329
332330void RasterCache::Touch (SkPicture* picture,
333331 const SkMatrix& transformation_matrix) {
334- PictureRasterCacheKey cache_key (picture->uniqueID (), transformation_matrix);
335- auto it = picture_cache_.find (cache_key);
336- if (it != picture_cache_.end ()) {
337- it->second .used_this_frame = true ;
338- it->second .access_count ++;
339- }
332+ RasterCacheKey cache_key (picture->uniqueID (), RasterCacheKeyType::kPicture ,
333+ transformation_matrix);
334+ Touch (cache_key);
340335}
341336
342337void RasterCache::Touch (DisplayList* display_list,
343338 const SkMatrix& transformation_matrix) {
344- DisplayListRasterCacheKey cache_key (display_list->unique_id (),
345- transformation_matrix);
346- auto it = display_list_cache_.find (cache_key);
347- if (it != display_list_cache_.end ()) {
339+ RasterCacheKey cache_key (display_list->unique_id (),
340+ RasterCacheKeyType::kDisplayList ,
341+ transformation_matrix);
342+ Touch (cache_key);
343+ }
344+
345+ void RasterCache::Touch (const RasterCacheKey& cache_key) {
346+ auto it = cache_.find (cache_key);
347+ if (it != cache_.end ()) {
348348 it->second .used_this_frame = true ;
349349 it->second .access_count ++;
350350 }
@@ -353,52 +353,33 @@ void RasterCache::Touch(DisplayList* display_list,
353353bool RasterCache::Draw (const SkPicture& picture,
354354 SkCanvas& canvas,
355355 const SkPaint* paint) const {
356- PictureRasterCacheKey cache_key (picture.uniqueID (), canvas.getTotalMatrix ());
357- auto it = picture_cache_.find (cache_key);
358- if (it == picture_cache_.end ()) {
359- return false ;
360- }
361-
362- Entry& entry = it->second ;
363- entry.access_count ++;
364- entry.used_this_frame = true ;
365-
366- if (entry.image ) {
367- entry.image ->draw (canvas, paint);
368- return true ;
369- }
370-
371- return false ;
356+ RasterCacheKey cache_key (picture.uniqueID (), RasterCacheKeyType::kPicture ,
357+ canvas.getTotalMatrix ());
358+ return Draw (cache_key, canvas, paint);
372359}
373360
374361bool RasterCache::Draw (const DisplayList& display_list,
375362 SkCanvas& canvas,
376363 const SkPaint* paint) const {
377- DisplayListRasterCacheKey cache_key (display_list.unique_id (),
378- canvas.getTotalMatrix ());
379- auto it = display_list_cache_.find (cache_key);
380- if (it == display_list_cache_.end ()) {
381- return false ;
382- }
383-
384- Entry& entry = it->second ;
385- entry.access_count ++;
386- entry.used_this_frame = true ;
387-
388- if (entry.image ) {
389- entry.image ->draw (canvas, paint);
390- return true ;
391- }
392-
393- return false ;
364+ RasterCacheKey cache_key (display_list.unique_id (),
365+ RasterCacheKeyType::kDisplayList ,
366+ canvas.getTotalMatrix ());
367+ return Draw (cache_key, canvas, paint);
394368}
395369
396370bool RasterCache::Draw (const Layer* layer,
397371 SkCanvas& canvas,
398372 const SkPaint* paint) const {
399- LayerRasterCacheKey cache_key (layer->unique_id (), canvas.getTotalMatrix ());
400- auto it = layer_cache_.find (cache_key);
401- if (it == layer_cache_.end ()) {
373+ RasterCacheKey cache_key (layer->unique_id (), RasterCacheKeyType::kLayer ,
374+ canvas.getTotalMatrix ());
375+ return Draw (cache_key, canvas, paint);
376+ }
377+
378+ bool RasterCache::Draw (const RasterCacheKey& cache_key,
379+ SkCanvas& canvas,
380+ const SkPaint* paint) const {
381+ auto it = cache_.find (cache_key);
382+ if (it == cache_.end ()) {
402383 return false ;
403384 }
404385
@@ -419,37 +400,88 @@ void RasterCache::PrepareNewFrame() {
419400 display_list_cached_this_frame_ = 0 ;
420401}
421402
403+ void RasterCache::SweepOneCacheAfterFrame (RasterCacheKey::Map<Entry>& cache,
404+ RasterCacheMetrics& picture_metrics,
405+ RasterCacheMetrics& layer_metrics) {
406+ std::vector<RasterCacheKey::Map<Entry>::iterator> dead;
407+
408+ for (auto it = cache.begin (); it != cache.end (); ++it) {
409+ Entry& entry = it->second ;
410+
411+ if (!entry.used_this_frame ) {
412+ dead.push_back (it);
413+ } else if (entry.image ) {
414+ RasterCacheKeyKind kind = it->first .kind ();
415+ switch (kind) {
416+ case RasterCacheKeyKind::kPictureMetrics :
417+ picture_metrics.in_use_count ++;
418+ picture_metrics.in_use_bytes += entry.image ->image_bytes ();
419+ break ;
420+ case RasterCacheKeyKind::kLayerMetrics :
421+ layer_metrics.in_use_count ++;
422+ layer_metrics.in_use_bytes += entry.image ->image_bytes ();
423+ break ;
424+ }
425+ }
426+ entry.used_this_frame = false ;
427+ }
428+
429+ for (auto it : dead) {
430+ if (it->second .image ) {
431+ RasterCacheKeyKind kind = it->first .kind ();
432+ switch (kind) {
433+ case RasterCacheKeyKind::kPictureMetrics :
434+ picture_metrics.eviction_count ++;
435+ picture_metrics.eviction_bytes += it->second .image ->image_bytes ();
436+ break ;
437+ case RasterCacheKeyKind::kLayerMetrics :
438+ layer_metrics.eviction_count ++;
439+ layer_metrics.eviction_bytes += it->second .image ->image_bytes ();
440+ break ;
441+ }
442+ }
443+ cache.erase (it);
444+ }
445+ }
446+
422447void RasterCache::CleanupAfterFrame () {
423448 picture_metrics_ = {};
424449 layer_metrics_ = {};
425450 {
426451 TRACE_EVENT0 (" flutter" , " RasterCache::SweepCaches" );
427- SweepOneCacheAfterFrame (picture_cache_, picture_metrics_);
428- SweepOneCacheAfterFrame (display_list_cache_, picture_metrics_);
429- SweepOneCacheAfterFrame (layer_cache_, layer_metrics_);
452+ SweepOneCacheAfterFrame (cache_, picture_metrics_, layer_metrics_);
430453 }
431454 TraceStatsToTimeline ();
432455}
433456
434457void RasterCache::Clear () {
435- picture_cache_.clear ();
436- display_list_cache_.clear ();
437- layer_cache_.clear ();
458+ cache_.clear ();
438459 picture_metrics_ = {};
439460 layer_metrics_ = {};
440461}
441462
442463size_t RasterCache::GetCachedEntriesCount () const {
443- return layer_cache_.size () + picture_cache_.size () +
444- display_list_cache_.size ();
464+ return cache_.size ();
445465}
446466
447467size_t RasterCache::GetLayerCachedEntriesCount () const {
448- return layer_cache_.size ();
468+ size_t layer_cached_entries_count = 0 ;
469+ for (const auto & item : cache_) {
470+ if (item.first .kind () == RasterCacheKeyKind::kLayerMetrics ) {
471+ layer_cached_entries_count++;
472+ }
473+ }
474+ return layer_cached_entries_count;
449475}
450476
451477size_t RasterCache::GetPictureCachedEntriesCount () const {
452- return picture_cache_.size () + display_list_cache_.size ();
478+ size_t picture_cached_entries_count = 0 ;
479+ for (const auto & item : cache_) {
480+ if (item.first .kind () == RasterCacheKeyKind::kPictureMetrics ) {
481+ picture_cached_entries_count++;
482+ }
483+ }
484+ return picture_cached_entries_count;
453485}
454486
455487void RasterCache::SetCheckboardCacheImages (bool checkerboard) {
@@ -479,8 +511,9 @@ void RasterCache::TraceStatsToTimeline() const {
479511
480512size_t RasterCache::EstimateLayerCacheByteSize () const {
481513 size_t layer_cache_bytes = 0 ;
482- for (const auto & item : layer_cache_) {
483- if (item.second .image ) {
514+ for (const auto & item : cache_) {
515+ if (item.first .kind () == RasterCacheKeyKind::kLayerMetrics &&
516+ item.second .image ) {
484517 layer_cache_bytes += item.second .image ->image_bytes ();
485518 }
486519 }
@@ -489,13 +522,9 @@ size_t RasterCache::EstimateLayerCacheByteSize() const {
489522
490523size_t RasterCache::EstimatePictureCacheByteSize () const {
491524 size_t picture_cache_bytes = 0 ;
492- for (const auto & item : picture_cache_) {
493- if (item.second .image ) {
494- picture_cache_bytes += item.second .image ->image_bytes ();
495- }
496- }
497- for (const auto & item : display_list_cache_) {
498- if (item.second .image ) {
525+ for (const auto & item : cache_) {
526+ if (item.first .kind () == RasterCacheKeyKind::kPictureMetrics &&
527+ item.second .image ) {
499528 picture_cache_bytes += item.second .image ->image_bytes ();
500529 }
501530 }
0 commit comments