From a00ae5e90a77b99fc57b64087f89a3e831eaefaa Mon Sep 17 00:00:00 2001 From: ColdPaleLight Date: Thu, 10 Mar 2022 10:44:57 +0800 Subject: [PATCH] Tweak the code and add some comments --- flow/raster_cache.cc | 2 +- flow/raster_cache.h | 4 +++- flow/raster_cache_unittests.cc | 12 ++++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/flow/raster_cache.cc b/flow/raster_cache.cc index 625f79b81d252..101a83180e847 100644 --- a/flow/raster_cache.cc +++ b/flow/raster_cache.cc @@ -416,8 +416,8 @@ void RasterCache::SweepOneCacheAfterFrame(RasterCacheKey::Map& cache, for (auto it = cache.begin(); it != cache.end(); ++it) { Entry& entry = it->second; if (!entry.used_this_frame) { - entry.unused_count++; if (entry.unused_count < entry.unused_threshold()) { + entry.unused_count++; if (entry.image) { RasterCacheKeyKind kind = it->first.kind(); switch (kind) { diff --git a/flow/raster_cache.h b/flow/raster_cache.h index 5b4c6ab98dc70..f1331d638ebfe 100644 --- a/flow/raster_cache.h +++ b/flow/raster_cache.h @@ -308,7 +308,9 @@ class RasterCache { size_t access_count = 0; size_t unused_count = 0; std::unique_ptr image; - size_t unused_threshold() const { return is_high_priority ? 3 : 1; } + // Return the number of frames the entry survives if it is not used. If the + // number is 0, then it will be evicted when not in use. + size_t unused_threshold() const { return is_high_priority ? 3 : 0; } }; void Touch(const RasterCacheKey& cache_key); diff --git a/flow/raster_cache_unittests.cc b/flow/raster_cache_unittests.cc index f7b5fe99bbf9a..c078026e56f35 100644 --- a/flow/raster_cache_unittests.cc +++ b/flow/raster_cache_unittests.cc @@ -371,6 +371,12 @@ TEST(RasterCache, KeepUnusedSkPicturesIfIsHighPriority) { PrepareAndCleanupEmptyFrame(cache, 3); cache.PrepareNewFrame(); + ASSERT_TRUE(cache.Draw(*picture, dummy_canvas)); + cache.CleanupAfterFrame(); + + // The entry will be evicted when it is not used 4 times in a row. + PrepareAndCleanupEmptyFrame(cache, 4); + cache.PrepareNewFrame(); ASSERT_FALSE(cache.Draw(*picture, dummy_canvas)); cache.CleanupAfterFrame(); } @@ -408,6 +414,12 @@ TEST(RasterCache, KeepUnusedDisplayListsIfIsHighPriority) { PrepareAndCleanupEmptyFrame(cache, 3); cache.PrepareNewFrame(); + ASSERT_TRUE(cache.Draw(*display_list, dummy_canvas)); + cache.CleanupAfterFrame(); + + // The entry will be evicted when it is not used 4 times in a row. + PrepareAndCleanupEmptyFrame(cache, 4); + cache.PrepareNewFrame(); ASSERT_FALSE(cache.Draw(*display_list, dummy_canvas)); cache.CleanupAfterFrame(); }