Revert "Try rasterizing images and layers only once , even when their rasterization fails. Further enforce the same access threshold on layers as on Pictures. Previously layers would always be cached. The latter is a semantic change. (#16545)" (flutter/engine#16889)

This caused regression in several benchmarks, including:
animated_placeholder_perf. Regression tracked in
https://github.com/flutter/flutter/issues/51776.

This reverts commit ac4e9832c9.
This commit is contained in:
Chris Bracken
2020-03-02 11:44:45 -08:00
committed by GitHub
parent 83543c22ec
commit 88d9a5f43c
3 changed files with 34 additions and 64 deletions

View File

@@ -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,

View File

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

View File

@@ -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<SkImage> image;
sk_sp<SkColorSpace> 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;
auto picture = GetSamplePicture();
sk_sp<SkImage> image;
sk_sp<SkColorSpace> srgb = SkColorSpace::MakeSRGB();
ASSERT_FALSE(cache.Prepare(nullptr, &layer, matrix));
ASSERT_FALSE(cache.Prepare(nullptr, &layer, matrix));
ASSERT_FALSE(cache.Prepare(nullptr, &layer, matrix));
ASSERT_FALSE(
cache.Prepare(NULL, picture.get(), matrix, srgb.get(), true, false));
// 1st access.
ASSERT_FALSE(cache.Get(&layer, matrix).is_valid());
ASSERT_FALSE(cache.Prepare(nullptr, &layer, matrix));
// 2st access.
ASSERT_FALSE(cache.Get(&layer, matrix).is_valid());
// 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<SkImage> image;
sk_sp<SkColorSpace> 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<SkImage> image;
sk_sp<SkColorSpace> 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<SkColorSpace> 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