diff --git a/flow/raster_cache.cc b/flow/raster_cache.cc index 46253c97c509c..f179905ed8e53 100644 --- a/flow/raster_cache.cc +++ b/flow/raster_cache.cc @@ -119,15 +119,24 @@ static RasterCacheResult Rasterize( return {surface->makeImageSnapshot(), logical_rect}; } -bool RasterCache::Prepare(PrerollContext* context, +RasterCacheResult RasterizePicture(SkPicture* picture, + GrContext* context, + const SkMatrix& ctm, + SkColorSpace* dst_color_space, + bool checkerboard) { + return Rasterize(context, ctm, dst_color_space, checkerboard, + picture->cullRect(), + [=](SkCanvas* canvas) { canvas->drawPicture(picture); }); +} + +void RasterCache::Prepare(PrerollContext* context, Layer* layer, const SkMatrix& ctm) { LayerRasterCacheKey cache_key(layer->unique_id(), ctm); - Entry& entry = layer_cache_[cache_key]; - - if (!entry.did_rasterize && !entry.image.is_valid() && - entry.access_count >= access_threshold_) { + entry.access_count++; + entry.used_this_frame = true; + if (!entry.image.is_valid()) { entry.image = Rasterize( context->gr_context, ctm, context->dst_color_space, checkerboard_images_, layer->paint_bounds(), @@ -152,12 +161,7 @@ bool RasterCache::Prepare(PrerollContext* context, layer->Paint(paintContext); } }); - - entry.did_rasterize = true; - - return true; } - return false; } bool RasterCache::Prepare(GrContext* context, @@ -196,19 +200,12 @@ bool RasterCache::Prepare(GrContext* context, return false; } - // Don't try to rasterize pictures that were already attempted to be - // rasterized even if the image is invalid. - if (!entry.did_rasterize && !entry.image.is_valid()) { - entry.image = - Rasterize(context, transformation_matrix, dst_color_space, - checkerboard_images_, picture->cullRect(), - [=](SkCanvas* canvas) { canvas->drawPicture(picture); }); - - entry.did_rasterize = true; + if (!entry.image.is_valid()) { + entry.image = RasterizePicture(picture, context, transformation_matrix, + dst_color_space, checkerboard_images_); picture_cached_this_frame_++; - return true; } - return false; + return true; } RasterCacheResult RasterCache::Get(const SkPicture& picture, diff --git a/flow/raster_cache.h b/flow/raster_cache.h index 93e6200cbf41d..b51382a5c8d2d 100644 --- a/flow/raster_cache.h +++ b/flow/raster_cache.h @@ -84,7 +84,7 @@ class RasterCache { bool is_complex, bool will_change); - bool Prepare(PrerollContext* context, Layer* layer, const SkMatrix& ctm); + void Prepare(PrerollContext* context, Layer* layer, const SkMatrix& ctm); RasterCacheResult Get(const SkPicture& picture, const SkMatrix& ctm) const; @@ -101,7 +101,6 @@ class RasterCache { private: struct Entry { bool used_this_frame = false; - bool did_rasterize = false; size_t access_count = 0; RasterCacheResult image; }; diff --git a/flow/raster_cache_unittests.cc b/flow/raster_cache_unittests.cc index 16abea65e5ca8..03e3b3879c469 100644 --- a/flow/raster_cache_unittests.cc +++ b/flow/raster_cache_unittests.cc @@ -4,7 +4,6 @@ #include "flutter/flow/raster_cache.h" -#include "flutter/flow/layers/container_layer.h" #include "gtest/gtest.h" #include "third_party/skia/include/core/SkPicture.h" #include "third_party/skia/include/core/SkPictureRecorder.h" @@ -30,7 +29,7 @@ TEST(RasterCache, SimpleInitialization) { ASSERT_TRUE(true); } -TEST(RasterCache, ThresholdIsRespectedForPictures) { +TEST(RasterCache, ThresholdIsRespected) { size_t threshold = 2; flutter::RasterCache cache(threshold); @@ -38,6 +37,8 @@ TEST(RasterCache, ThresholdIsRespectedForPictures) { auto picture = GetSamplePicture(); + sk_sp image; + sk_sp srgb = SkColorSpace::MakeSRGB(); ASSERT_FALSE( cache.Prepare(NULL, picture.get(), matrix, srgb.get(), true, false)); @@ -60,28 +61,21 @@ TEST(RasterCache, ThresholdIsRespectedForPictures) { ASSERT_TRUE(cache.Get(*picture, matrix).is_valid()); } -TEST(RasterCache, ThresholdIsRespectedForLayers) { - size_t threshold = 2; +TEST(RasterCache, AccessThresholdOfZeroDisablesCaching) { + size_t threshold = 0; flutter::RasterCache cache(threshold); SkMatrix matrix = SkMatrix::I(); - ContainerLayer layer; - - sk_sp srgb = SkColorSpace::MakeSRGB(); - ASSERT_FALSE(cache.Prepare(nullptr, &layer, matrix)); - ASSERT_FALSE(cache.Prepare(nullptr, &layer, matrix)); - ASSERT_FALSE(cache.Prepare(nullptr, &layer, matrix)); - - // 1st access. - ASSERT_FALSE(cache.Get(&layer, matrix).is_valid()); + auto picture = GetSamplePicture(); - ASSERT_FALSE(cache.Prepare(nullptr, &layer, matrix)); + sk_sp image; - // 2st access. - ASSERT_FALSE(cache.Get(&layer, matrix).is_valid()); + sk_sp srgb = SkColorSpace::MakeSRGB(); + ASSERT_FALSE( + cache.Prepare(NULL, picture.get(), matrix, srgb.get(), true, false)); - // Calling Prepare now would crash due to the nullptr. + ASSERT_FALSE(cache.Get(*picture, matrix).is_valid()); } TEST(RasterCache, PictureCacheLimitPerFrameIsRespectedWhenZero) { @@ -92,6 +86,8 @@ TEST(RasterCache, PictureCacheLimitPerFrameIsRespectedWhenZero) { auto picture = GetSamplePicture(); + sk_sp image; + sk_sp srgb = SkColorSpace::MakeSRGB(); ASSERT_FALSE( cache.Prepare(NULL, picture.get(), matrix, srgb.get(), true, false)); @@ -107,6 +103,8 @@ TEST(RasterCache, SweepsRemoveUnusedFrames) { auto picture = GetSamplePicture(); + sk_sp image; + sk_sp srgb = SkColorSpace::MakeSRGB(); ASSERT_FALSE(cache.Prepare(NULL, picture.get(), matrix, srgb.get(), true, false)); // 1 @@ -124,29 +122,5 @@ TEST(RasterCache, SweepsRemoveUnusedFrames) { ASSERT_FALSE(cache.Get(*picture, matrix).is_valid()); } -TEST(RasterCache, TryRasterizngOnlyOnce) { - size_t threshold = 1; - flutter::RasterCache cache(threshold); - - SkMatrix matrix = SkMatrix::I(); - // Test picture too large to successfully rasterize. - auto picture = SkPicture::MakePlaceholder(SkRect::MakeWH(2e12, 2e12)); - - sk_sp srgb = SkColorSpace::MakeSRGB(); - ASSERT_FALSE(cache.Prepare(nullptr, picture.get(), matrix, srgb.get(), true, - false)); // 1 - ASSERT_FALSE(cache.Get(*picture, matrix).is_valid()); - - // Rasterization ran, though Get() below returns an invalid image. - ASSERT_TRUE(cache.Prepare(nullptr, picture.get(), matrix, srgb.get(), true, - false)); // 2 - ASSERT_FALSE(cache.Get(*picture, matrix).is_valid()); - - // This time we should not try again to rasterize. - ASSERT_FALSE(cache.Prepare(nullptr, picture.get(), matrix, srgb.get(), true, - false)); // 2 - ASSERT_FALSE(cache.Get(*picture, matrix).is_valid()); -} - } // namespace testing } // namespace flutter