Evicting unused cache entries before RasterCache actions (flutter/engine#34627)

This commit is contained in:
ColdPaleLight
2022-07-23 10:56:03 +08:00
committed by GitHub
parent 8cd062be3c
commit 369a324ff8
9 changed files with 252 additions and 199 deletions

View File

@@ -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<FrameTimingsRecorder>();
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);

View File

@@ -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();
}
}

View File

@@ -150,6 +150,7 @@ void LayerTree::Paint(CompositorContext::ScopedFrame& frame,
};
if (cache) {
cache->EvictUnusedCacheEntries();
TryToRasterCache(raster_cache_items_, &context, ignore_raster_cache);
}

View File

@@ -171,58 +171,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<Entry>& cache,
RasterCacheMetrics& picture_metrics,
RasterCacheMetrics& layer_metrics) {
std::vector<RasterCacheKey::Map<Entry>::iterator> dead;
for (auto it = cache.begin(); it != cache.end(); ++it) {
void RasterCache::UpdateMetrics() {
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;
}
FML_DCHECK(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<RasterCacheKey::Map<Entry>::iterator> dead;
for (auto it = cache_.begin(); it != cache_.end(); ++it) {
Entry& entry = it->second;
if (!entry.encountered_this_frame) {
dead.push_back(it);
}
}
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();
}
@@ -303,4 +292,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

View File

@@ -78,20 +78,40 @@ 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 + 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.
* The size of all of the cached images during this frame.
*/
size_t total_bytes() const { return in_use_bytes + eviction_bytes; }
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
* - 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 - 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:
* Computes used counts and memory then reports cache metrics.
*/
class RasterCache {
public:
struct Context {
@@ -126,9 +146,11 @@ class RasterCache {
bool HasEntry(const RasterCacheKeyID& id, const SkMatrix&) const;
void PrepareNewFrame();
void BeginFrame();
void CleanupAfterFrame();
void EvictUnusedCacheEntries();
void EndFrame();
void Clear();
@@ -222,9 +244,9 @@ class RasterCache {
std::unique_ptr<RasterCacheResult> image;
};
void SweepOneCacheAfterFrame(RasterCacheKey::Map<Entry>& 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_;

View File

@@ -44,36 +44,36 @@ 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);
// 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));
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(
ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache(
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(
ASSERT_TRUE(RasterCacheItemPrerollAndTryToRasterCache(
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);
@@ -96,29 +96,29 @@ 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);
// 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));
cache.CleanupAfterFrame();
cache.PrepareNewFrame();
cache.EndFrame();
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));
cache.CleanupAfterFrame();
cache.PrepareNewFrame();
cache.EndFrame();
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));
}
@@ -174,10 +174,10 @@ 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(
ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache(
display_list_item, preroll_context, paint_context, matrix));
ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint));
}
@@ -199,11 +199,11 @@ 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);
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));
}
@@ -226,17 +226,17 @@ 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);
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));
}
@@ -258,32 +258,32 @@ 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);
// 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, 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;
@@ -294,73 +294,78 @@ 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));
DisplayListRasterCacheItem display_list_item_1(display_list_1.get(),
SkPoint(), true, false);
DisplayListRasterCacheItem display_list_item_2(display_list_2.get(),
SkPoint(), true, false);
cache.CleanupAfterFrame();
cache.PrepareNewFrame();
// 2.
ASSERT_TRUE(DisplayListRasterCacheItemTryToRasterCache(
display_item, preroll_context, paint_context, matrix));
ASSERT_TRUE(display_item.Draw(paint_context, &dummy_canvas, &paint));
cache.CleanupAfterFrame();
cache.PrepareNewFrame();
cache.CleanupAfterFrame(); // Extra frame without a Get image access.
cache.PrepareNewFrame();
ASSERT_FALSE(cache.Draw(display_item.GetId().value(), dummy_canvas, &paint));
ASSERT_FALSE(display_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.PrepareNewFrame();
DisplayListRasterCacheItem display_list_item(display_list.get(), SkPoint(),
true, false);
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();
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.PrepareNewFrame();
cache.CleanupAfterFrame(); // Extra frame without a Get image access.
cache.PrepareNewFrame();
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(
cache.Draw(display_list_item.GetId().value(), dummy_canvas, &paint));
ASSERT_FALSE(display_list_item.Draw(paint_context, &dummy_canvas, &paint));
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_EQ(cache.EstimatePictureCacheByteSize(), 0u);
ASSERT_EQ(cache.picture_metrics().total_count(), 0u);
ASSERT_EQ(cache.picture_metrics().total_bytes(), 0u);
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_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();
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_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) {
@@ -395,18 +400,18 @@ 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);
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.CleanupAfterFrame();
cache.PrepareNewFrame();
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));
@@ -434,19 +439,19 @@ 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);
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.CleanupAfterFrame();
cache.PrepareNewFrame();
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));
}
@@ -477,19 +482,19 @@ 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);
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.CleanupAfterFrame();
cache.PrepareNewFrame();
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));
@@ -503,16 +508,16 @@ TEST(RasterCache, NaiveComplexityScoringDisplayList) {
DisplayListRasterCacheItem display_list_item_2 =
DisplayListRasterCacheItem(display_list.get(), SkPoint(), false, false);
cache.PrepareNewFrame();
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.CleanupAfterFrame();
cache.PrepareNewFrame();
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));
}
@@ -543,11 +548,11 @@ 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]);
ASSERT_FALSE(DisplayListRasterCacheItemTryToRasterCache(
ASSERT_FALSE(RasterCacheItemPrerollAndTryToRasterCache(
display_list_item, preroll_context, paint_context, matrices[j]));
}
@@ -557,7 +562,7 @@ TEST(RasterCache, DisplayListWithSingularMatrixIsNotCached) {
display_list_item.Draw(paint_context, &dummy_canvas, &paint));
}
cache.CleanupAfterFrame();
cache.EndFrame();
}
}

View File

@@ -24,7 +24,9 @@ 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);
layer.raster_cache_item()->TryToPrepareRasterCache(paint_context_);
RasterCache::Context r_context = {
@@ -60,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,
@@ -81,7 +81,6 @@ void MockRasterCache::AddMockPicture(int width, int height) {
});
}
static std::vector<RasterCacheItem*> raster_cache_items_;
PrerollContextHolder GetSamplePrerollContextHolder(RasterCache* raster_cache) {
FixedRefreshRateStopwatch raster_time;
FixedRefreshRateStopwatch ui_time;
@@ -141,13 +140,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);

View File

@@ -46,6 +46,8 @@ class MockRasterCacheResult : public RasterCacheResult {
SkRect device_rect_;
};
static std::vector<RasterCacheItem*> 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
};
@@ -124,12 +127,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

View File

@@ -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<FrameDamage> 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();