From 9ef007ab506691cd8fa964c3671d4885b5c7117d Mon Sep 17 00:00:00 2001 From: ColdPaleLight Date: Wed, 13 Jul 2022 14:23:30 +0800 Subject: [PATCH 1/6] Evicting unused cache entries before RasterCache actions --- flow/frame_timings_recorder_unittests.cc | 11 +-- flow/layers/container_layer_unittests.cc | 29 ++++---- flow/layers/layer_tree.cc | 1 + flow/raster_cache.cc | 68 +++++++++---------- flow/raster_cache.h | 12 ++-- flow/raster_cache_unittests.cc | 86 ++++++++++++------------ shell/common/rasterizer.cc | 4 +- 7 files changed, 109 insertions(+), 102 deletions(-) diff --git a/flow/frame_timings_recorder_unittests.cc b/flow/frame_timings_recorder_unittests.cc index fe2f0f8bf9495..ab1ef1ebc7eda 100644 --- a/flow/frame_timings_recorder_unittests.cc +++ b/flow/frame_timings_recorder_unittests.cc @@ -91,7 +91,7 @@ TEST(FrameTimingsRecorderTest, RecordRasterTimesWithCache) { using namespace std::chrono_literals; MockRasterCache cache(1, 10); - cache.PrepareNewFrame(); + cache.BeginFrame(); const auto raster_start = fml::TimePoint::Now(); recorder->RecordRasterStart(raster_start); @@ -102,8 +102,9 @@ TEST(FrameTimingsRecorderTest, RecordRasterTimesWithCache) { cache.AddMockPicture(100, 100); size_t picture_bytes = cache.EstimatePictureCacheByteSize(); EXPECT_GT(picture_bytes, 0u); + cache.EvictUnusedCacheEntries(); - cache.CleanupAfterFrame(); + cache.EndFrame(); const auto before_raster_end_wall_time = fml::TimePoint::CurrentWallTime(); std::this_thread::sleep_for(1ms); @@ -252,7 +253,7 @@ TEST(FrameTimingsRecorderTest, ClonedHasSameRasterEnd) { TEST(FrameTimingsRecorderTest, ClonedHasSameRasterEndWithCache) { auto recorder = std::make_unique(); MockRasterCache cache(1, 10); - cache.PrepareNewFrame(); + cache.BeginFrame(); const auto now = fml::TimePoint::Now(); recorder->RecordVsync(now, now + fml::TimeDelta::FromMilliseconds(16)); @@ -266,8 +267,8 @@ TEST(FrameTimingsRecorderTest, ClonedHasSameRasterEndWithCache) { cache.AddMockPicture(100, 100); size_t picture_bytes = cache.EstimatePictureCacheByteSize(); EXPECT_GT(picture_bytes, 0u); - - cache.CleanupAfterFrame(); + cache.EvictUnusedCacheEntries(); + cache.EndFrame(); recorder->RecordRasterEnd(&cache); auto cloned = recorder->CloneUntil(FrameTimingsRecorder::State::kRasterEnd); diff --git a/flow/layers/container_layer_unittests.cc b/flow/layers/container_layer_unittests.cc index 89f6477a4ec4b..577ed32ffb493 100644 --- a/flow/layers/container_layer_unittests.cc +++ b/flow/layers/container_layer_unittests.cc @@ -268,8 +268,9 @@ TEST_F(ContainerLayerTest, RasterCacheTest) { { // frame1 use_mock_raster_cache(); - preroll_context()->raster_cache->PrepareNewFrame(); + preroll_context()->raster_cache->BeginFrame(); layer->Preroll(preroll_context(), SkMatrix::I()); + preroll_context()->raster_cache->EvictUnusedCacheEntries(); // Cache the cacheable entries LayerTree::TryToRasterCache(*(preroll_context()->raster_cached_entries), &paint_context()); @@ -299,7 +300,7 @@ TEST_F(ContainerLayerTest, RasterCacheTest) { // render count < 2 don't cache it EXPECT_EQ(cacheable_container_layer2->raster_cache_item()->cache_state(), RasterCacheItem::CacheState::kNone); - preroll_context()->raster_cache->CleanupAfterFrame(); + preroll_context()->raster_cache->EndFrame(); } { @@ -307,8 +308,9 @@ TEST_F(ContainerLayerTest, RasterCacheTest) { // new frame the layer tree will create new PrerollContext, so in here we // clear the cached_entries preroll_context()->raster_cached_entries->clear(); - preroll_context()->raster_cache->PrepareNewFrame(); + preroll_context()->raster_cache->BeginFrame(); layer->Preroll(preroll_context(), SkMatrix::I()); + preroll_context()->raster_cache->EvictUnusedCacheEntries(); // Cache the cacheable entries LayerTree::TryToRasterCache(*(preroll_context()->raster_cached_entries), @@ -342,17 +344,17 @@ TEST_F(ContainerLayerTest, RasterCacheTest) { EXPECT_TRUE(raster_cache()->Draw( cacheable_layer21->raster_cache_item()->GetId().value(), cache_canvas, &paint)); - preroll_context()->raster_cache->CleanupAfterFrame(); + preroll_context()->raster_cache->EndFrame(); } { // frame3 // new frame the layer tree will create new PrerollContext, so in here we // clear the cached_entries - preroll_context()->raster_cache->PrepareNewFrame(); + preroll_context()->raster_cache->BeginFrame(); preroll_context()->raster_cached_entries->clear(); layer->Preroll(preroll_context(), SkMatrix::I()); - + preroll_context()->raster_cache->EvictUnusedCacheEntries(); // Cache the cacheable entries LayerTree::TryToRasterCache(*(preroll_context()->raster_cached_entries), &paint_context()); @@ -385,33 +387,34 @@ TEST_F(ContainerLayerTest, RasterCacheTest) { EXPECT_TRUE(raster_cache()->HasEntry( cacheable_layer21->raster_cache_item()->GetId().value(), SkMatrix::I())); - preroll_context()->raster_cache->CleanupAfterFrame(); + preroll_context()->raster_cache->EndFrame(); } { - preroll_context()->raster_cache->PrepareNewFrame(); + preroll_context()->raster_cache->BeginFrame(); // frame4 preroll_context()->raster_cached_entries->clear(); layer->Preroll(preroll_context(), SkMatrix::I()); + preroll_context()->raster_cache->EvictUnusedCacheEntries(); LayerTree::TryToRasterCache(*(preroll_context()->raster_cached_entries), &paint_context()); - preroll_context()->raster_cache->CleanupAfterFrame(); + preroll_context()->raster_cache->EndFrame(); // frame5 - preroll_context()->raster_cache->PrepareNewFrame(); + preroll_context()->raster_cache->BeginFrame(); preroll_context()->raster_cached_entries->clear(); layer->Preroll(preroll_context(), SkMatrix::I()); LayerTree::TryToRasterCache(*(preroll_context()->raster_cached_entries), &paint_context()); - preroll_context()->raster_cache->CleanupAfterFrame(); + preroll_context()->raster_cache->EndFrame(); // frame6 - preroll_context()->raster_cache->PrepareNewFrame(); + preroll_context()->raster_cache->BeginFrame(); preroll_context()->raster_cached_entries->clear(); layer->Preroll(preroll_context(), SkMatrix::I()); LayerTree::TryToRasterCache(*(preroll_context()->raster_cached_entries), &paint_context()); - preroll_context()->raster_cache->CleanupAfterFrame(); + preroll_context()->raster_cache->EndFrame(); } } diff --git a/flow/layers/layer_tree.cc b/flow/layers/layer_tree.cc index 79cf6cc13ecd3..f481a05c65bcb 100644 --- a/flow/layers/layer_tree.cc +++ b/flow/layers/layer_tree.cc @@ -150,6 +150,7 @@ void LayerTree::Paint(CompositorContext::ScopedFrame& frame, }; if (cache) { + cache->EvictUnusedCacheEntries(); TryToRasterCache(raster_cache_items_, &context, ignore_raster_cache); } diff --git a/flow/raster_cache.cc b/flow/raster_cache.cc index 2284cd238067d..6c4473618cfc0 100644 --- a/flow/raster_cache.cc +++ b/flow/raster_cache.cc @@ -159,58 +159,47 @@ bool RasterCache::Draw(const RasterCacheKeyID& id, return false; } -void RasterCache::PrepareNewFrame() { +void RasterCache::BeginFrame() { display_list_cached_this_frame_ = 0; + picture_metrics_ = {}; + layer_metrics_ = {}; } -void RasterCache::SweepOneCacheAfterFrame(RasterCacheKey::Map& cache, - RasterCacheMetrics& picture_metrics, - RasterCacheMetrics& layer_metrics) { +void RasterCache::UpdateMetrics() { + for (auto it = cache_.begin(); it != cache_.end(); ++it) { + Entry& entry = it->second; + FML_CHECK(entry.encountered_this_frame); + if (entry.image) { + RasterCacheMetrics& metrics = GetMetricsForKind(it->first.kind()); + metrics.in_use_count++; + metrics.in_use_bytes += entry.image->image_bytes(); + } + entry.encountered_this_frame = false; + } +} + +void RasterCache::EvictUnusedCacheEntries() { std::vector::iterator> dead; - for (auto it = cache.begin(); it != cache.end(); ++it) { + for (auto it = cache_.begin(); it != cache_.end(); ++it) { Entry& entry = it->second; - if (!entry.encountered_this_frame) { dead.push_back(it); - } else if (entry.image) { - RasterCacheKeyKind kind = it->first.kind(); - switch (kind) { - case RasterCacheKeyKind::kDisplayListMetrics: - picture_metrics.in_use_count++; - picture_metrics.in_use_bytes += entry.image->image_bytes(); - break; - case RasterCacheKeyKind::kLayerMetrics: - layer_metrics.in_use_count++; - layer_metrics.in_use_bytes += entry.image->image_bytes(); - break; - } } - entry.encountered_this_frame = false; } for (auto it : dead) { if (it->second.image) { - RasterCacheKeyKind kind = it->first.kind(); - switch (kind) { - case RasterCacheKeyKind::kDisplayListMetrics: - picture_metrics.eviction_count++; - picture_metrics.eviction_bytes += it->second.image->image_bytes(); - break; - case RasterCacheKeyKind::kLayerMetrics: - layer_metrics.eviction_count++; - layer_metrics.eviction_bytes += it->second.image->image_bytes(); - break; - } + RasterCacheMetrics& metrics = GetMetricsForKind(it->first.kind()); + metrics.eviction_count++; + metrics.eviction_bytes += it->second.image->image_bytes(); } - cache.erase(it); + cache_.erase(it); } } -void RasterCache::CleanupAfterFrame() { - picture_metrics_ = {}; - layer_metrics_ = {}; - SweepOneCacheAfterFrame(cache_, picture_metrics_, layer_metrics_); +void RasterCache::EndFrame() { + UpdateMetrics(); TraceStatsToTimeline(); } @@ -291,4 +280,13 @@ size_t RasterCache::EstimatePictureCacheByteSize() const { return picture_cache_bytes; } +RasterCacheMetrics& RasterCache::GetMetricsForKind(RasterCacheKeyKind kind) { + switch (kind) { + case RasterCacheKeyKind::kDisplayListMetrics: + return picture_metrics_; + case RasterCacheKeyKind::kLayerMetrics: + return layer_metrics_; + } +} + } // namespace flutter diff --git a/flow/raster_cache.h b/flow/raster_cache.h index 01f08e12cf90c..4e1628eb73847 100644 --- a/flow/raster_cache.h +++ b/flow/raster_cache.h @@ -125,9 +125,11 @@ class RasterCache { bool HasEntry(const RasterCacheKeyID& id, const SkMatrix&) const; - void PrepareNewFrame(); + void BeginFrame(); - void CleanupAfterFrame(); + void EvictUnusedCacheEntries(); + + void EndFrame(); void Clear(); @@ -221,9 +223,9 @@ class RasterCache { std::unique_ptr image; }; - void SweepOneCacheAfterFrame(RasterCacheKey::Map& cache, - RasterCacheMetrics& picture_metrics, - RasterCacheMetrics& layer_metrics); + void UpdateMetrics(); + + RasterCacheMetrics& GetMetricsForKind(RasterCacheKeyKind kind); const size_t access_threshold_; const size_t display_list_cache_limit_per_frame_; diff --git a/flow/raster_cache_unittests.cc b/flow/raster_cache_unittests.cc index 067e045fa91b6..d0cdd07bcb4be 100644 --- a/flow/raster_cache_unittests.cc +++ b/flow/raster_cache_unittests.cc @@ -41,7 +41,7 @@ TEST(RasterCache, MetricsOmitUnpopulatedEntries) { auto& preroll_context = preroll_context_holder.preroll_context; auto& paint_context = paint_context_holder.paint_context; - cache.PrepareNewFrame(); + cache.BeginFrame(); DisplayListRasterCacheItem display_list_item(display_list.get(), SkPoint(), true, false); @@ -50,27 +50,27 @@ TEST(RasterCache, MetricsOmitUnpopulatedEntries) { display_list_item, preroll_context, paint_context, matrix)); ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint)); - cache.CleanupAfterFrame(); + cache.EndFrame(); ASSERT_EQ(cache.picture_metrics().total_count(), 0u); ASSERT_EQ(cache.picture_metrics().total_bytes(), 0u); - cache.PrepareNewFrame(); + cache.BeginFrame(); // 2nd access. ASSERT_FALSE(DisplayListRasterCacheItemTryToRasterCache( display_list_item, preroll_context, paint_context, matrix)); ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint)); - cache.CleanupAfterFrame(); + cache.EndFrame(); ASSERT_EQ(cache.picture_metrics().total_count(), 0u); ASSERT_EQ(cache.picture_metrics().total_bytes(), 0u); - cache.PrepareNewFrame(); + cache.BeginFrame(); // Now Prepare should cache it. ASSERT_TRUE(DisplayListRasterCacheItemTryToRasterCache( display_list_item, preroll_context, paint_context, matrix)); ASSERT_TRUE(display_list_item.Draw(paint_context, &dummy_canvas, &paint)); - cache.CleanupAfterFrame(); + cache.EndFrame(); ASSERT_EQ(cache.picture_metrics().total_count(), 1u); // 150w * 100h * 4bpp ASSERT_EQ(cache.picture_metrics().total_bytes(), 25600u); @@ -93,7 +93,7 @@ TEST(RasterCache, ThresholdIsRespectedForDisplayList) { auto& preroll_context = preroll_context_holder.preroll_context; auto& paint_context = paint_context_holder.paint_context; - cache.PrepareNewFrame(); + cache.BeginFrame(); DisplayListRasterCacheItem display_list_item(display_list.get(), SkPoint(), true, false); @@ -103,16 +103,16 @@ TEST(RasterCache, ThresholdIsRespectedForDisplayList) { display_list_item, preroll_context, paint_context, matrix)); ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint)); - cache.CleanupAfterFrame(); - cache.PrepareNewFrame(); + cache.EndFrame(); + cache.BeginFrame(); // 2nd access. ASSERT_FALSE(DisplayListRasterCacheItemTryToRasterCache( display_list_item, preroll_context, paint_context, matrix)); ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint)); - cache.CleanupAfterFrame(); - cache.PrepareNewFrame(); + cache.EndFrame(); + cache.BeginFrame(); // Now Prepare should cache it. ASSERT_TRUE(DisplayListRasterCacheItemTryToRasterCache( @@ -138,7 +138,7 @@ TEST(RasterCache, AccessThresholdOfZeroDisablesCachingForSkPicture) { auto& preroll_context = preroll_context_holder.preroll_context; auto& paint_context = paint_context_holder.paint_context; - cache.PrepareNewFrame(); + cache.BeginFrame(); DisplayListRasterCacheItem display_list_item(display_list.get(), SkPoint(), true, false); ASSERT_FALSE(DisplayListRasterCacheItemTryToRasterCache( @@ -163,7 +163,7 @@ TEST(RasterCache, AccessThresholdOfZeroDisablesCachingForDisplayList) { auto& preroll_context = preroll_context_holder.preroll_context; auto& paint_context = paint_context_holder.paint_context; - cache.PrepareNewFrame(); + cache.BeginFrame(); DisplayListRasterCacheItem display_list_item(display_list.get(), SkPoint(), true, false); @@ -190,7 +190,7 @@ TEST(RasterCache, PictureCacheLimitPerFrameIsRespectedWhenZeroForSkPicture) { auto& preroll_context = preroll_context_holder.preroll_context; auto& paint_context = paint_context_holder.paint_context; - cache.PrepareNewFrame(); + cache.BeginFrame(); DisplayListRasterCacheItem display_list_item(display_list.get(), SkPoint(), true, false); @@ -222,7 +222,7 @@ TEST(RasterCache, PictureCacheLimitPerFrameIsRespectedWhenZeroForDisplayList) { auto& preroll_context = preroll_context_holder.preroll_context; auto& paint_context = paint_context_holder.paint_context; - cache.PrepareNewFrame(); + cache.BeginFrame(); DisplayListRasterCacheItem display_list_item(display_list.get(), SkPoint(), true, false); @@ -265,19 +265,20 @@ TEST(RasterCache, SweepsRemoveUnusedSkPictures) { display_item, preroll_context, paint_context, matrix)); ASSERT_FALSE(display_item.Draw(paint_context, &dummy_canvas, &paint)); - cache.CleanupAfterFrame(); - cache.PrepareNewFrame(); + cache.EndFrame(); + cache.BeginFrame(); // 2. ASSERT_TRUE(DisplayListRasterCacheItemTryToRasterCache( display_item, preroll_context, paint_context, matrix)); ASSERT_TRUE(display_item.Draw(paint_context, &dummy_canvas, &paint)); - cache.CleanupAfterFrame(); + cache.EndFrame(); - cache.PrepareNewFrame(); - cache.CleanupAfterFrame(); // Extra frame without a Get image access. + cache.BeginFrame(); + cache.EvictUnusedCacheEntries(); + cache.EndFrame(); // Extra frame without a Get image access. - cache.PrepareNewFrame(); + cache.BeginFrame(); ASSERT_FALSE(cache.Draw(display_item.GetId().value(), dummy_canvas, &paint)); ASSERT_FALSE(display_item.Draw(paint_context, &dummy_canvas, &paint)); @@ -300,7 +301,7 @@ TEST(RasterCache, SweepsRemoveUnusedDisplayLists) { auto& preroll_context = preroll_context_holder.preroll_context; auto& paint_context = paint_context_holder.paint_context; - cache.PrepareNewFrame(); + cache.BeginFrame(); DisplayListRasterCacheItem display_list_item(display_list.get(), SkPoint(), true, false); @@ -309,19 +310,20 @@ TEST(RasterCache, SweepsRemoveUnusedDisplayLists) { display_list_item, preroll_context, paint_context, matrix)); ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint)); - cache.CleanupAfterFrame(); - cache.PrepareNewFrame(); + cache.EndFrame(); + cache.BeginFrame(); ASSERT_TRUE(DisplayListRasterCacheItemTryToRasterCache( display_list_item, preroll_context, paint_context, matrix)); ASSERT_TRUE(display_list_item.Draw(paint_context, &dummy_canvas, &paint)); - cache.CleanupAfterFrame(); + cache.EndFrame(); - cache.PrepareNewFrame(); - cache.CleanupAfterFrame(); // Extra frame without a Get image access. + cache.BeginFrame(); + cache.EvictUnusedCacheEntries(); + cache.EndFrame(); // Extra frame without a Get image access. - cache.PrepareNewFrame(); + cache.BeginFrame(); ASSERT_FALSE( cache.Draw(display_list_item.GetId().value(), dummy_canvas, &paint)); ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint)); @@ -359,7 +361,7 @@ TEST(RasterCache, DeviceRectRoundOutForDisplayList) { auto& preroll_context = preroll_context_holder.preroll_context; auto& paint_context = paint_context_holder.paint_context; - cache.PrepareNewFrame(); + cache.BeginFrame(); DisplayListRasterCacheItem display_list_item(display_list.get(), SkPoint(), true, false); @@ -367,8 +369,8 @@ TEST(RasterCache, DeviceRectRoundOutForDisplayList) { display_list_item, preroll_context, paint_context, ctm)); ASSERT_FALSE(display_list_item.Draw(paint_context, &canvas, &paint)); - cache.CleanupAfterFrame(); - cache.PrepareNewFrame(); + cache.EndFrame(); + cache.BeginFrame(); ASSERT_TRUE(DisplayListRasterCacheItemTryToRasterCache( display_list_item, preroll_context, paint_context, ctm)); @@ -398,7 +400,7 @@ TEST(RasterCache, NestedOpCountMetricUsedForDisplayList) { auto& preroll_context = preroll_context_holder.preroll_context; auto& paint_context = paint_context_holder.paint_context; - cache.PrepareNewFrame(); + cache.BeginFrame(); DisplayListRasterCacheItem display_list_item(display_list.get(), SkPoint(), false, false); @@ -407,8 +409,8 @@ TEST(RasterCache, NestedOpCountMetricUsedForDisplayList) { display_list_item, preroll_context, paint_context, matrix)); ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint)); - cache.CleanupAfterFrame(); - cache.PrepareNewFrame(); + cache.EndFrame(); + cache.BeginFrame(); ASSERT_TRUE(DisplayListRasterCacheItemTryToRasterCache( display_list_item, preroll_context, paint_context, matrix)); @@ -441,7 +443,7 @@ TEST(RasterCache, NaiveComplexityScoringDisplayList) { auto& preroll_context = preroll_context_holder.preroll_context; auto& paint_context = paint_context_holder.paint_context; - cache.PrepareNewFrame(); + cache.BeginFrame(); DisplayListRasterCacheItem display_list_item(display_list.get(), SkPoint(), false, false); @@ -450,8 +452,8 @@ TEST(RasterCache, NaiveComplexityScoringDisplayList) { display_list_item, preroll_context, paint_context, matrix)); ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint)); - cache.CleanupAfterFrame(); - cache.PrepareNewFrame(); + cache.EndFrame(); + cache.BeginFrame(); ASSERT_FALSE(DisplayListRasterCacheItemTryToRasterCache( display_list_item, preroll_context, paint_context, matrix)); @@ -467,14 +469,14 @@ TEST(RasterCache, NaiveComplexityScoringDisplayList) { DisplayListRasterCacheItem display_list_item_2 = DisplayListRasterCacheItem(display_list.get(), SkPoint(), false, false); - cache.PrepareNewFrame(); + cache.BeginFrame(); ASSERT_FALSE(DisplayListRasterCacheItemTryToRasterCache( display_list_item_2, preroll_context, paint_context, matrix)); ASSERT_FALSE(display_list_item_2.Draw(paint_context, &dummy_canvas, &paint)); - cache.CleanupAfterFrame(); - cache.PrepareNewFrame(); + cache.EndFrame(); + cache.BeginFrame(); ASSERT_TRUE(DisplayListRasterCacheItemTryToRasterCache( display_list_item_2, preroll_context, paint_context, matrix)); @@ -507,7 +509,7 @@ TEST(RasterCache, DisplayListWithSingularMatrixIsNotCached) { true, false); for (int i = 0; i < 10; i++) { - cache.PrepareNewFrame(); + cache.BeginFrame(); for (int j = 0; j < matrixCount; j++) { display_list_item.set_matrix(matrices[j]); @@ -521,7 +523,7 @@ TEST(RasterCache, DisplayListWithSingularMatrixIsNotCached) { display_list_item.Draw(paint_context, &dummy_canvas, &paint)); } - cache.CleanupAfterFrame(); + cache.EndFrame(); } } diff --git a/shell/common/rasterizer.cc b/shell/common/rasterizer.cc index 8587d253d1f4d..d121b90527fab 100644 --- a/shell/common/rasterizer.cc +++ b/shell/common/rasterizer.cc @@ -637,7 +637,7 @@ RasterStatus Rasterizer::DrawToSurfaceUnsafe( frame->GetDisplayListBuilder().get() // display list builder ); if (compositor_frame) { - compositor_context_->raster_cache().PrepareNewFrame(); + compositor_context_->raster_cache().BeginFrame(); frame_timings_recorder.RecordRasterStart(fml::TimePoint::Now()); std::unique_ptr damage; @@ -704,7 +704,7 @@ RasterStatus Rasterizer::DrawToSurfaceUnsafe( frame->Submit(); } - compositor_context_->raster_cache().CleanupAfterFrame(); + compositor_context_->raster_cache().EndFrame(); frame_timings_recorder.RecordRasterEnd( &compositor_context_->raster_cache()); FireNextFrameCallbackIfPresent(); From ac4a16bcfe9e44c6a5f55252abac2ebac2ac62ac Mon Sep 17 00:00:00 2001 From: ColdPaleLight Date: Thu, 14 Jul 2022 12:27:48 +0800 Subject: [PATCH 2/6] Tweak code and add unittests --- flow/raster_cache.h | 4 +- flow/raster_cache_unittests.cc | 154 ++++++++++++++++++++---------- flow/testing/mock_raster_cache.cc | 17 +++- flow/testing/mock_raster_cache.h | 10 +- 4 files changed, 128 insertions(+), 57 deletions(-) diff --git a/flow/raster_cache.h b/flow/raster_cache.h index 4e1628eb73847..76d7fefe2cd9a 100644 --- a/flow/raster_cache.h +++ b/flow/raster_cache.h @@ -82,14 +82,14 @@ struct RasterCacheMetrics { * they were used in the frame or held memory during the frame and then * were evicted after it ended. */ - size_t total_count() const { return in_use_count + eviction_count; } + size_t total_count() const { return in_use_count; } /** * The size of all of the cached images during this frame whether * they were used in the frame or held memory during the frame and then * were evicted after it ended. */ - size_t total_bytes() const { return in_use_bytes + eviction_bytes; } + size_t total_bytes() const { return in_use_bytes; } }; class RasterCache { diff --git a/flow/raster_cache_unittests.cc b/flow/raster_cache_unittests.cc index d0cdd07bcb4be..a1b330dc59f2d 100644 --- a/flow/raster_cache_unittests.cc +++ b/flow/raster_cache_unittests.cc @@ -46,7 +46,7 @@ TEST(RasterCache, MetricsOmitUnpopulatedEntries) { true, false); // 1st access. - ASSERT_FALSE(DisplayListRasterCacheItemTryToRasterCache( + ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache( display_list_item, preroll_context, paint_context, matrix)); ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint)); @@ -56,7 +56,7 @@ TEST(RasterCache, MetricsOmitUnpopulatedEntries) { cache.BeginFrame(); // 2nd access. - ASSERT_FALSE(DisplayListRasterCacheItemTryToRasterCache( + ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache( display_list_item, preroll_context, paint_context, matrix)); ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint)); @@ -66,7 +66,7 @@ TEST(RasterCache, MetricsOmitUnpopulatedEntries) { cache.BeginFrame(); // Now Prepare should cache it. - ASSERT_TRUE(DisplayListRasterCacheItemTryToRasterCache( + ASSERT_TRUE(RasterCacheItemPrerollAndTryToRasterCache( display_list_item, preroll_context, paint_context, matrix)); ASSERT_TRUE(display_list_item.Draw(paint_context, &dummy_canvas, &paint)); @@ -99,7 +99,7 @@ TEST(RasterCache, ThresholdIsRespectedForDisplayList) { true, false); // 1st access. - ASSERT_FALSE(DisplayListRasterCacheItemTryToRasterCache( + ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache( display_list_item, preroll_context, paint_context, matrix)); ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint)); @@ -107,7 +107,7 @@ TEST(RasterCache, ThresholdIsRespectedForDisplayList) { cache.BeginFrame(); // 2nd access. - ASSERT_FALSE(DisplayListRasterCacheItemTryToRasterCache( + ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache( display_list_item, preroll_context, paint_context, matrix)); ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint)); @@ -115,7 +115,7 @@ TEST(RasterCache, ThresholdIsRespectedForDisplayList) { cache.BeginFrame(); // Now Prepare should cache it. - ASSERT_TRUE(DisplayListRasterCacheItemTryToRasterCache( + ASSERT_TRUE(RasterCacheItemPrerollAndTryToRasterCache( display_list_item, preroll_context, paint_context, matrix)); ASSERT_TRUE(display_list_item.Draw(paint_context, &dummy_canvas, &paint)); } @@ -141,7 +141,7 @@ TEST(RasterCache, AccessThresholdOfZeroDisablesCachingForSkPicture) { cache.BeginFrame(); DisplayListRasterCacheItem display_list_item(display_list.get(), SkPoint(), true, false); - ASSERT_FALSE(DisplayListRasterCacheItemTryToRasterCache( + ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache( display_list_item, preroll_context, paint_context, matrix)); ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint)); } @@ -167,7 +167,7 @@ TEST(RasterCache, AccessThresholdOfZeroDisablesCachingForDisplayList) { DisplayListRasterCacheItem display_list_item(display_list.get(), SkPoint(), true, false); - ASSERT_FALSE(DisplayListRasterCacheItemTryToRasterCache( + ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache( display_list_item, preroll_context, paint_context, matrix)); ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint)); } @@ -194,13 +194,13 @@ TEST(RasterCache, PictureCacheLimitPerFrameIsRespectedWhenZeroForSkPicture) { DisplayListRasterCacheItem display_list_item(display_list.get(), SkPoint(), true, false); - ASSERT_FALSE(DisplayListRasterCacheItemTryToRasterCache( + ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache( display_list_item, preroll_context, paint_context, matrix)); ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint)); - ASSERT_FALSE(DisplayListRasterCacheItemTryToRasterCache( + ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache( display_list_item, preroll_context, paint_context, matrix)); ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint)); - ASSERT_FALSE(DisplayListRasterCacheItemTryToRasterCache( + ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache( display_list_item, preroll_context, paint_context, matrix)); ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint)); } @@ -227,27 +227,26 @@ TEST(RasterCache, PictureCacheLimitPerFrameIsRespectedWhenZeroForDisplayList) { DisplayListRasterCacheItem display_list_item(display_list.get(), SkPoint(), true, false); // 1st access. - ASSERT_FALSE(DisplayListRasterCacheItemTryToRasterCache( + ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache( display_list_item, preroll_context, paint_context, matrix)); ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint)); // 2nd access. - ASSERT_FALSE(DisplayListRasterCacheItemTryToRasterCache( + ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache( display_list_item, preroll_context, paint_context, matrix)); ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint)); // the picture_cache_limit_per_frame = 0, so don't cache it - ASSERT_FALSE(DisplayListRasterCacheItemTryToRasterCache( + ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache( display_list_item, preroll_context, paint_context, matrix)); ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint)); } -TEST(RasterCache, SweepsRemoveUnusedSkPictures) { +TEST(RasterCache, SweepsRemoveUnusedDisplayLists) { size_t threshold = 1; flutter::RasterCache cache(threshold); SkMatrix matrix = SkMatrix::I(); auto display_list = GetSampleDisplayList(); - ; SkCanvas dummy_canvas; SkPaint paint; @@ -258,19 +257,21 @@ TEST(RasterCache, SweepsRemoveUnusedSkPictures) { auto& preroll_context = preroll_context_holder.preroll_context; auto& paint_context = paint_context_holder.paint_context; - DisplayListRasterCacheItem display_item(display_list.get(), SkPoint(), true, - false); - // 1. - ASSERT_FALSE(DisplayListRasterCacheItemTryToRasterCache( - display_item, preroll_context, paint_context, matrix)); - ASSERT_FALSE(display_item.Draw(paint_context, &dummy_canvas, &paint)); + cache.BeginFrame(); + + DisplayListRasterCacheItem display_list_item(display_list.get(), SkPoint(), + true, false); + + ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache( + display_list_item, preroll_context, paint_context, matrix)); + ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint)); cache.EndFrame(); cache.BeginFrame(); - // 2. - ASSERT_TRUE(DisplayListRasterCacheItemTryToRasterCache( - display_item, preroll_context, paint_context, matrix)); - ASSERT_TRUE(display_item.Draw(paint_context, &dummy_canvas, &paint)); + + ASSERT_TRUE(RasterCacheItemPrerollAndTryToRasterCache( + display_list_item, preroll_context, paint_context, matrix)); + ASSERT_TRUE(display_list_item.Draw(paint_context, &dummy_canvas, &paint)); cache.EndFrame(); @@ -279,18 +280,19 @@ TEST(RasterCache, SweepsRemoveUnusedSkPictures) { cache.EndFrame(); // Extra frame without a Get image access. cache.BeginFrame(); - - ASSERT_FALSE(cache.Draw(display_item.GetId().value(), dummy_canvas, &paint)); - ASSERT_FALSE(display_item.Draw(paint_context, &dummy_canvas, &paint)); + ASSERT_FALSE( + cache.Draw(display_list_item.GetId().value(), dummy_canvas, &paint)); + ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint)); } -TEST(RasterCache, SweepsRemoveUnusedDisplayLists) { +TEST(RasterCache, EvitUnusedCacheEntries) { size_t threshold = 1; flutter::RasterCache cache(threshold); SkMatrix matrix = SkMatrix::I(); - auto display_list = GetSampleDisplayList(); + auto display_list_1 = GetSampleDisplayList(); + auto display_list_2 = GetSampleDisplayList(); SkCanvas dummy_canvas; SkPaint paint; @@ -301,32 +303,78 @@ TEST(RasterCache, SweepsRemoveUnusedDisplayLists) { auto& preroll_context = preroll_context_holder.preroll_context; auto& paint_context = paint_context_holder.paint_context; - cache.BeginFrame(); + DisplayListRasterCacheItem display_list_item_1(display_list_1.get(), + SkPoint(), true, false); + DisplayListRasterCacheItem display_list_item_2(display_list_2.get(), + SkPoint(), true, false); - DisplayListRasterCacheItem display_list_item(display_list.get(), SkPoint(), - true, false); + cache.BeginFrame(); + RasterCacheItemPreroll(display_list_item_1, preroll_context, matrix); + RasterCacheItemPreroll(display_list_item_2, preroll_context, matrix); + cache.EvictUnusedCacheEntries(); + ASSERT_EQ(cache.EstimatePictureCacheByteSize(), 0u); + ASSERT_FALSE( + RasterCacheItemTryToRasterCache(display_list_item_1, paint_context)); + ASSERT_FALSE( + RasterCacheItemTryToRasterCache(display_list_item_2, paint_context)); + ASSERT_EQ(cache.EstimatePictureCacheByteSize(), 0u); + ASSERT_FALSE(display_list_item_1.Draw(paint_context, &dummy_canvas, &paint)); + ASSERT_FALSE(display_list_item_2.Draw(paint_context, &dummy_canvas, &paint)); + cache.EndFrame(); - ASSERT_FALSE(DisplayListRasterCacheItemTryToRasterCache( - display_list_item, preroll_context, paint_context, matrix)); - ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint)); + ASSERT_EQ(cache.EstimatePictureCacheByteSize(), 0u); + ASSERT_EQ(cache.picture_metrics().total_count(), 0u); + ASSERT_EQ(cache.picture_metrics().total_bytes(), 0u); - cache.EndFrame(); cache.BeginFrame(); + RasterCacheItemPreroll(display_list_item_1, preroll_context, matrix); + RasterCacheItemPreroll(display_list_item_2, preroll_context, matrix); + cache.EvictUnusedCacheEntries(); + ASSERT_EQ(cache.EstimatePictureCacheByteSize(), 0u); + ASSERT_TRUE( + RasterCacheItemTryToRasterCache(display_list_item_1, paint_context)); + ASSERT_TRUE( + RasterCacheItemTryToRasterCache(display_list_item_2, paint_context)); + ASSERT_EQ(cache.EstimatePictureCacheByteSize(), 51200u); + ASSERT_TRUE(display_list_item_1.Draw(paint_context, &dummy_canvas, &paint)); + ASSERT_TRUE(display_list_item_2.Draw(paint_context, &dummy_canvas, &paint)); + cache.EndFrame(); - ASSERT_TRUE(DisplayListRasterCacheItemTryToRasterCache( - display_list_item, preroll_context, paint_context, matrix)); - ASSERT_TRUE(display_list_item.Draw(paint_context, &dummy_canvas, &paint)); + ASSERT_EQ(cache.EstimatePictureCacheByteSize(), 51200u); + ASSERT_EQ(cache.picture_metrics().total_count(), 2u); + ASSERT_EQ(cache.picture_metrics().total_bytes(), 51200u); + cache.BeginFrame(); + RasterCacheItemPreroll(display_list_item_1, preroll_context, matrix); + cache.EvictUnusedCacheEntries(); + ASSERT_EQ(cache.EstimatePictureCacheByteSize(), 25600u); + ASSERT_TRUE( + RasterCacheItemTryToRasterCache(display_list_item_1, paint_context)); + ASSERT_EQ(cache.EstimatePictureCacheByteSize(), 25600u); + ASSERT_TRUE(display_list_item_1.Draw(paint_context, &dummy_canvas, &paint)); cache.EndFrame(); + ASSERT_EQ(cache.EstimatePictureCacheByteSize(), 25600u); + ASSERT_EQ(cache.picture_metrics().total_count(), 1u); + ASSERT_EQ(cache.picture_metrics().total_bytes(), 25600u); + cache.BeginFrame(); cache.EvictUnusedCacheEntries(); - cache.EndFrame(); // Extra frame without a Get image access. + ASSERT_EQ(cache.EstimatePictureCacheByteSize(), 0u); + cache.EndFrame(); + + ASSERT_EQ(cache.EstimatePictureCacheByteSize(), 0u); + ASSERT_EQ(cache.picture_metrics().total_count(), 0u); + ASSERT_EQ(cache.picture_metrics().total_bytes(), 0u); cache.BeginFrame(); ASSERT_FALSE( - cache.Draw(display_list_item.GetId().value(), dummy_canvas, &paint)); - ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint)); + cache.Draw(display_list_item_1.GetId().value(), dummy_canvas, &paint)); + ASSERT_FALSE(display_list_item_1.Draw(paint_context, &dummy_canvas, &paint)); + ASSERT_FALSE( + cache.Draw(display_list_item_2.GetId().value(), dummy_canvas, &paint)); + ASSERT_FALSE(display_list_item_2.Draw(paint_context, &dummy_canvas, &paint)); + cache.EndFrame(); } TEST(RasterCache, ComputeDeviceRectBasedOnFractionalTranslation) { @@ -365,14 +413,14 @@ TEST(RasterCache, DeviceRectRoundOutForDisplayList) { DisplayListRasterCacheItem display_list_item(display_list.get(), SkPoint(), true, false); - ASSERT_FALSE(DisplayListRasterCacheItemTryToRasterCache( + ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache( display_list_item, preroll_context, paint_context, ctm)); ASSERT_FALSE(display_list_item.Draw(paint_context, &canvas, &paint)); cache.EndFrame(); cache.BeginFrame(); - ASSERT_TRUE(DisplayListRasterCacheItemTryToRasterCache( + ASSERT_TRUE(RasterCacheItemPrerollAndTryToRasterCache( display_list_item, preroll_context, paint_context, ctm)); ASSERT_TRUE(display_list_item.Draw(paint_context, &canvas, &paint)); @@ -405,14 +453,14 @@ TEST(RasterCache, NestedOpCountMetricUsedForDisplayList) { DisplayListRasterCacheItem display_list_item(display_list.get(), SkPoint(), false, false); - ASSERT_FALSE(DisplayListRasterCacheItemTryToRasterCache( + ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache( display_list_item, preroll_context, paint_context, matrix)); ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint)); cache.EndFrame(); cache.BeginFrame(); - ASSERT_TRUE(DisplayListRasterCacheItemTryToRasterCache( + ASSERT_TRUE(RasterCacheItemPrerollAndTryToRasterCache( display_list_item, preroll_context, paint_context, matrix)); ASSERT_TRUE(display_list_item.Draw(paint_context, &dummy_canvas, &paint)); } @@ -448,14 +496,14 @@ TEST(RasterCache, NaiveComplexityScoringDisplayList) { DisplayListRasterCacheItem display_list_item(display_list.get(), SkPoint(), false, false); - ASSERT_FALSE(DisplayListRasterCacheItemTryToRasterCache( + ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache( display_list_item, preroll_context, paint_context, matrix)); ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint)); cache.EndFrame(); cache.BeginFrame(); - ASSERT_FALSE(DisplayListRasterCacheItemTryToRasterCache( + ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache( display_list_item, preroll_context, paint_context, matrix)); ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint)); @@ -471,14 +519,14 @@ TEST(RasterCache, NaiveComplexityScoringDisplayList) { DisplayListRasterCacheItem(display_list.get(), SkPoint(), false, false); cache.BeginFrame(); - ASSERT_FALSE(DisplayListRasterCacheItemTryToRasterCache( + ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache( display_list_item_2, preroll_context, paint_context, matrix)); ASSERT_FALSE(display_list_item_2.Draw(paint_context, &dummy_canvas, &paint)); cache.EndFrame(); cache.BeginFrame(); - ASSERT_TRUE(DisplayListRasterCacheItemTryToRasterCache( + ASSERT_TRUE(RasterCacheItemPrerollAndTryToRasterCache( display_list_item_2, preroll_context, paint_context, matrix)); ASSERT_TRUE(display_list_item_2.Draw(paint_context, &dummy_canvas, &paint)); } @@ -513,7 +561,7 @@ TEST(RasterCache, DisplayListWithSingularMatrixIsNotCached) { for (int j = 0; j < matrixCount; j++) { display_list_item.set_matrix(matrices[j]); - ASSERT_FALSE(DisplayListRasterCacheItemTryToRasterCache( + ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache( display_list_item, preroll_context, paint_context, matrices[j])); } diff --git a/flow/testing/mock_raster_cache.cc b/flow/testing/mock_raster_cache.cc index 8f4aec37685b1..d58f31cc42520 100644 --- a/flow/testing/mock_raster_cache.cc +++ b/flow/testing/mock_raster_cache.cc @@ -26,6 +26,8 @@ void MockRasterCache::AddMockLayer(int width, int height) { path.addRect(100, 100, 100 + width, 100 + height); MockCacheableLayer layer = MockCacheableLayer(path); layer.Preroll(&preroll_context_, ctm); + MarkSeen(RasterCacheKeyID(layer.unique_id(), RasterCacheKeyType::kLayer), ctm, + true); layer.raster_cache_item()->TryToPrepareRasterCache(paint_context_); RasterCache::Context r_context = { // clang-format off @@ -143,13 +145,26 @@ PaintContextHolder GetSamplePaintContextHolder(RasterCache* raster_cache) { return holder; } -bool DisplayListRasterCacheItemTryToRasterCache( +bool RasterCacheItemPrerollAndTryToRasterCache( DisplayListRasterCacheItem& display_list_item, PrerollContext& context, PaintContext& paint_context, const SkMatrix& matrix) { + RasterCacheItemPreroll(display_list_item, context, matrix); + context.raster_cache->EvictUnusedCacheEntries(); + return RasterCacheItemTryToRasterCache(display_list_item, paint_context); +} + +void RasterCacheItemPreroll(DisplayListRasterCacheItem& display_list_item, + PrerollContext& context, + const SkMatrix& matrix) { display_list_item.PrerollSetup(&context, matrix); display_list_item.PrerollFinalize(&context, matrix); +} + +bool RasterCacheItemTryToRasterCache( + DisplayListRasterCacheItem& display_list_item, + PaintContext& paint_context) { if (display_list_item.cache_state() == RasterCacheItem::CacheState::kCurrent) { return display_list_item.TryToPrepareRasterCache(paint_context); diff --git a/flow/testing/mock_raster_cache.h b/flow/testing/mock_raster_cache.h index 0a240985f850d..c35cb3d678fc3 100644 --- a/flow/testing/mock_raster_cache.h +++ b/flow/testing/mock_raster_cache.h @@ -124,12 +124,20 @@ PrerollContextHolder GetSamplePrerollContextHolder( PaintContextHolder GetSamplePaintContextHolder( RasterCache* raster_cache = nullptr); -bool DisplayListRasterCacheItemTryToRasterCache( +bool RasterCacheItemPrerollAndTryToRasterCache( DisplayListRasterCacheItem& display_list_item, PrerollContext& context, PaintContext& paint_context, const SkMatrix& matrix); +void RasterCacheItemPreroll(DisplayListRasterCacheItem& display_list_item, + PrerollContext& context, + const SkMatrix& matrix); + +bool RasterCacheItemTryToRasterCache( + DisplayListRasterCacheItem& display_list_item, + PaintContext& paint_context); + } // namespace testing } // namespace flutter From 70755f27ea2882530324a6e5c189a8817cb34506 Mon Sep 17 00:00:00 2001 From: ColdPaleLight Date: Thu, 14 Jul 2022 12:32:28 +0800 Subject: [PATCH 3/6] remove obsolete unittests --- flow/raster_cache_unittests.cc | 45 ---------------------------------- 1 file changed, 45 deletions(-) diff --git a/flow/raster_cache_unittests.cc b/flow/raster_cache_unittests.cc index a1b330dc59f2d..cc33ada0319b6 100644 --- a/flow/raster_cache_unittests.cc +++ b/flow/raster_cache_unittests.cc @@ -240,51 +240,6 @@ TEST(RasterCache, PictureCacheLimitPerFrameIsRespectedWhenZeroForDisplayList) { ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint)); } -TEST(RasterCache, SweepsRemoveUnusedDisplayLists) { - size_t threshold = 1; - flutter::RasterCache cache(threshold); - - SkMatrix matrix = SkMatrix::I(); - - auto display_list = GetSampleDisplayList(); - - SkCanvas dummy_canvas; - SkPaint paint; - - PrerollContextHolder preroll_context_holder = - GetSamplePrerollContextHolder(&cache); - PaintContextHolder paint_context_holder = GetSamplePaintContextHolder(&cache); - auto& preroll_context = preroll_context_holder.preroll_context; - auto& paint_context = paint_context_holder.paint_context; - - cache.BeginFrame(); - - DisplayListRasterCacheItem display_list_item(display_list.get(), SkPoint(), - true, false); - - ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache( - display_list_item, preroll_context, paint_context, matrix)); - ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint)); - - cache.EndFrame(); - cache.BeginFrame(); - - ASSERT_TRUE(RasterCacheItemPrerollAndTryToRasterCache( - display_list_item, preroll_context, paint_context, matrix)); - ASSERT_TRUE(display_list_item.Draw(paint_context, &dummy_canvas, &paint)); - - cache.EndFrame(); - - cache.BeginFrame(); - cache.EvictUnusedCacheEntries(); - cache.EndFrame(); // Extra frame without a Get image access. - - cache.BeginFrame(); - ASSERT_FALSE( - cache.Draw(display_list_item.GetId().value(), dummy_canvas, &paint)); - ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint)); -} - TEST(RasterCache, EvitUnusedCacheEntries) { size_t threshold = 1; flutter::RasterCache cache(threshold); From c31f717464cc0206e85674ff903064a241e563bd Mon Sep 17 00:00:00 2001 From: ColdPaleLight Date: Thu, 14 Jul 2022 12:38:23 +0800 Subject: [PATCH 4/6] update document comments --- flow/raster_cache.h | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/flow/raster_cache.h b/flow/raster_cache.h index 76d7fefe2cd9a..51c74e941f06b 100644 --- a/flow/raster_cache.h +++ b/flow/raster_cache.h @@ -78,16 +78,12 @@ struct RasterCacheMetrics { size_t in_use_bytes = 0; /** - * The total cache entries that had images during this frame whether - * they were used in the frame or held memory during the frame and then - * were evicted after it ended. + * The total cache entries that had images during this frame. */ size_t total_count() const { return in_use_count; } /** - * The size of all of the cached images during this frame whether - * they were used in the frame or held memory during the frame and then - * were evicted after it ended. + * The size of all of the cached images during this frame. */ size_t total_bytes() const { return in_use_bytes; } }; From a3f18780529ae49abae20cb3a51994faf6068701 Mon Sep 17 00:00:00 2001 From: ColdPaleLight Date: Tue, 19 Jul 2022 16:31:05 +0800 Subject: [PATCH 5/6] Add comments about life cycle --- flow/raster_cache.cc | 2 +- flow/raster_cache.h | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/flow/raster_cache.cc b/flow/raster_cache.cc index ee8cc4954acec..8b4a09dbda6e1 100644 --- a/flow/raster_cache.cc +++ b/flow/raster_cache.cc @@ -171,7 +171,7 @@ void RasterCache::BeginFrame() { void RasterCache::UpdateMetrics() { for (auto it = cache_.begin(); it != cache_.end(); ++it) { Entry& entry = it->second; - FML_CHECK(entry.encountered_this_frame); + FML_DCHECK(entry.encountered_this_frame); if (entry.image) { RasterCacheMetrics& metrics = GetMetricsForKind(it->first.kind()); metrics.in_use_count++; diff --git a/flow/raster_cache.h b/flow/raster_cache.h index 093b801b22edf..98061948609b5 100644 --- a/flow/raster_cache.h +++ b/flow/raster_cache.h @@ -88,6 +88,29 @@ struct RasterCacheMetrics { size_t total_bytes() const { return in_use_bytes; } }; +/** + * RasterCache is used to cache rasterized layers or display lists to improve + * performance. + * + * Life cycle of RasterCache methods: + * - Preroll stage + * - RasterCacheItem::PrerollSetup + * At the start of each layer's preroll, add cache items to + * `PrerollContext::raster_cached_entries`. + * - RasterCacheItem::PrerollFinalize + * At the end of each layer's preroll, may mark cache entris as + * encountered by the current frame. + * - Paint stage + * - RasterCache::EvictUnusedCacheEntries + * Evit cached images that are no longer used. + * - LayerTree::TryToPrepareRasterCache + * Create cache image for each cache entry if it does not exist. + * - LayerTree::Paint + * If layers or display lists are cached as cached images, the method + * `RasterCache::Draw` will be used to draw those cache images. + * - RasterCache::EndFrame: + * Computes used counts and memory then reports cache metrics. + */ class RasterCache { public: struct Context { From 5d605fba5d631cafb7691139d3de76021c4ba5ff Mon Sep 17 00:00:00 2001 From: ColdPaleLight Date: Wed, 20 Jul 2022 15:29:05 +0800 Subject: [PATCH 6/6] Clean code --- flow/raster_cache.h | 15 ++++++++------- flow/testing/mock_raster_cache.cc | 11 ++++------- flow/testing/mock_raster_cache.h | 5 ++++- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/flow/raster_cache.h b/flow/raster_cache.h index 98061948609b5..8eb33d532999e 100644 --- a/flow/raster_cache.h +++ b/flow/raster_cache.h @@ -94,18 +94,19 @@ struct RasterCacheMetrics { * * Life cycle of RasterCache methods: * - Preroll stage - * - RasterCacheItem::PrerollSetup - * At the start of each layer's preroll, add cache items to - * `PrerollContext::raster_cached_entries`. - * - RasterCacheItem::PrerollFinalize - * At the end of each layer's preroll, may mark cache entris as - * encountered by the current frame. + * - LayerTree::Preroll - for each Layer in the tree: + * - RasterCacheItem::PrerollSetup + * At the start of each layer's preroll, add cache items to + * `PrerollContext::raster_cached_entries`. + * - RasterCacheItem::PrerollFinalize + * At the end of each layer's preroll, may mark cache entris as + * encountered by the current frame. * - Paint stage * - RasterCache::EvictUnusedCacheEntries * Evit cached images that are no longer used. * - LayerTree::TryToPrepareRasterCache * Create cache image for each cache entry if it does not exist. - * - LayerTree::Paint + * - LayerTree::Paint - for each layer in the tree: * If layers or display lists are cached as cached images, the method * `RasterCache::Draw` will be used to draw those cache images. * - RasterCache::EndFrame: diff --git a/flow/testing/mock_raster_cache.cc b/flow/testing/mock_raster_cache.cc index a9e951860767f..3ce1be25cfcd8 100644 --- a/flow/testing/mock_raster_cache.cc +++ b/flow/testing/mock_raster_cache.cc @@ -24,10 +24,10 @@ void MockRasterCache::AddMockLayer(int width, int height) { SkMatrix ctm = SkMatrix::I(); SkPath path; path.addRect(100, 100, 100 + width, 100 + height); - MockCacheableLayer layer = MockCacheableLayer(path); + int layer_cached_threshold = 1; + MockCacheableLayer layer = + MockCacheableLayer(path, SkPaint(), layer_cached_threshold); layer.Preroll(&preroll_context_, ctm); - MarkSeen(RasterCacheKeyID(layer.unique_id(), RasterCacheKeyType::kLayer), ctm, - true); layer.raster_cache_item()->TryToPrepareRasterCache(paint_context_); RasterCache::Context r_context = { // clang-format off @@ -62,10 +62,8 @@ void MockRasterCache::AddMockPicture(int width, int height) { DisplayListRasterCacheItem display_list_item(display_list.get(), SkPoint(), true, false); for (int i = 0; i < access_threshold(); i++) { - MarkSeen(display_list_item.GetId().value(), ctm, true); - Draw(display_list_item.GetId().value(), mock_canvas_, nullptr); + AutoCache(&display_list_item, &preroll_context_, ctm); } - MarkSeen(display_list_item.GetId().value(), ctm, true); RasterCache::Context r_context = { // clang-format off .gr_context = preroll_context_.gr_context, @@ -83,7 +81,6 @@ void MockRasterCache::AddMockPicture(int width, int height) { }); } -static std::vector raster_cache_items_; PrerollContextHolder GetSamplePrerollContextHolder(RasterCache* raster_cache) { FixedRefreshRateStopwatch raster_time; FixedRefreshRateStopwatch ui_time; diff --git a/flow/testing/mock_raster_cache.h b/flow/testing/mock_raster_cache.h index c35cb3d678fc3..d6b385b57d389 100644 --- a/flow/testing/mock_raster_cache.h +++ b/flow/testing/mock_raster_cache.h @@ -46,6 +46,8 @@ class MockRasterCacheResult : public RasterCacheResult { SkRect device_rect_; }; +static std::vector raster_cache_items_; + /** * @brief A RasterCache implementation that simulates the act of rendering a * Layer or SkPicture without the overhead of rasterization or pixel storage. @@ -73,7 +75,7 @@ class MockRasterCache : public RasterCache { TextureRegistry texture_registry_; PrerollContext preroll_context_ = { // clang-format off - .raster_cache = nullptr, + .raster_cache = this, .gr_context = nullptr, .view_embedder = nullptr, .mutators_stack = mutators_stack_, @@ -87,6 +89,7 @@ class MockRasterCache : public RasterCache { .frame_device_pixel_ratio = 1.0f, .has_platform_view = false, .has_texture_layer = false, + .raster_cached_entries = &raster_cache_items_ // clang-format on };