Fix issue about checkerboardRasterCacheImages not working (flutter/engine#34424)

This commit is contained in:
ColdPaleLight
2022-07-15 07:08:04 +08:00
committed by GitHub
parent b0a6764623
commit 45d9265b01
8 changed files with 51 additions and 20 deletions

View File

@@ -159,7 +159,6 @@ bool DisplayListRasterCacheItem::TryToPrepareRasterCache(
.matrix = transformation_matrix_,
.logical_rect = bounds,
.flow_type = flow_type,
.checkerboard = context.checkerboard_offscreen_layers,
// clang-format on
};
return context.raster_cache->UpdateCacheEntry(

View File

@@ -148,12 +148,11 @@ bool LayerRasterCacheItem::TryToPrepareRasterCache(const PaintContext& context,
if (const SkRect* paint_bounds = GetPaintBoundsFromLayer()) {
RasterCache::Context r_context = {
// clang-format off
.gr_context = context.gr_context,
.dst_color_space = context.dst_color_space,
.matrix = matrix_,
.logical_rect = *paint_bounds,
.flow_type = flow_type,
.checkerboard = context.checkerboard_offscreen_layers,
.gr_context = context.gr_context,
.dst_color_space = context.dst_color_space,
.matrix = matrix_,
.logical_rect = *paint_bounds,
.flow_type = flow_type,
// clang-format on
};
return context.raster_cache->UpdateCacheEntry(

View File

@@ -24,14 +24,14 @@ sk_sp<SkShader> CreateCheckerboardShader(SkColor c1, SkColor c2, int size) {
SkSamplingOptions());
}
} // anonymous namespace
void DrawCheckerboard(SkCanvas* canvas, SkColor c1, SkColor c2, int size) {
SkPaint paint;
paint.setShader(CreateCheckerboardShader(c1, c2, size));
canvas->drawPaint(paint);
}
} // anonymous namespace
void DrawCheckerboard(SkCanvas* canvas, const SkRect& rect) {
// Draw a checkerboard
canvas->save();

View File

@@ -11,8 +11,6 @@
namespace flutter {
void DrawCheckerboard(SkCanvas* canvas, SkColor c1, SkColor c2, int size);
void DrawCheckerboard(SkCanvas* canvas, const SkRect& rect);
} // namespace flutter

View File

@@ -51,7 +51,9 @@ RasterCache::RasterCache(size_t access_threshold,
/// @note Procedure doesn't copy all closures.
std::unique_ptr<RasterCacheResult> RasterCache::Rasterize(
const RasterCache::Context& context,
const std::function<void(SkCanvas*)>& draw_function) {
const std::function<void(SkCanvas*)>& draw_function,
const std::function<void(SkCanvas*, const SkRect& rect)>& draw_checkerboard)
const {
TRACE_EVENT0("flutter", "RasterCachePopulate");
SkRect dest_rect =
@@ -78,8 +80,8 @@ std::unique_ptr<RasterCacheResult> RasterCache::Rasterize(
canvas->concat(context.matrix);
draw_function(canvas);
if (context.checkerboard) {
DrawCheckerboard(canvas, context.logical_rect);
if (checkerboard_images_) {
draw_checkerboard(canvas, context.logical_rect);
}
return std::make_unique<RasterCacheResult>(
@@ -93,7 +95,8 @@ bool RasterCache::UpdateCacheEntry(
RasterCacheKey key = RasterCacheKey(id, raster_cache_context.matrix);
Entry& entry = cache_[key];
if (!entry.image) {
entry.image = Rasterize(raster_cache_context, render_function);
entry.image =
Rasterize(raster_cache_context, render_function, DrawCheckerboard);
if (entry.image != nullptr) {
switch (id.type()) {
case RasterCacheKeyType::kDisplayList: {

View File

@@ -100,12 +100,13 @@ class RasterCache {
const SkMatrix& matrix;
const SkRect& logical_rect;
const char* flow_type;
bool checkerboard;
};
static std::unique_ptr<RasterCacheResult> Rasterize(
std::unique_ptr<RasterCacheResult> Rasterize(
const RasterCache::Context& context,
const std::function<void(SkCanvas*)>& draw_function);
const std::function<void(SkCanvas*)>& draw_function,
const std::function<void(SkCanvas*, const SkRect& rect)>&
draw_checkerboard) const;
explicit RasterCache(
size_t access_threshold = 3,

View File

@@ -120,6 +120,39 @@ TEST(RasterCache, ThresholdIsRespectedForDisplayList) {
ASSERT_TRUE(display_list_item.Draw(paint_context, &dummy_canvas, &paint));
}
TEST(RasterCache, SetCheckboardCacheImages) {
size_t threshold = 1;
flutter::RasterCache cache(threshold);
SkMatrix matrix = SkMatrix::I();
auto display_list = GetSampleDisplayList();
PaintContextHolder paint_context_holder = GetSamplePaintContextHolder(&cache);
auto& paint_context = paint_context_holder.paint_context;
auto dummy_draw_function = [](SkCanvas* canvas) {};
bool did_draw_checkerboard = false;
auto draw_checkerboard = [&](SkCanvas* canvas, const SkRect&) {
did_draw_checkerboard = true;
};
RasterCache::Context r_context = {
// clang-format off
.gr_context = paint_context.gr_context,
.dst_color_space = paint_context.dst_color_space,
.matrix = matrix,
.logical_rect = display_list->bounds(),
.flow_type = "RasterCacheFlow::DisplayList",
// clang-format on
};
cache.SetCheckboardCacheImages(false);
cache.Rasterize(r_context, dummy_draw_function, draw_checkerboard);
ASSERT_FALSE(did_draw_checkerboard);
cache.SetCheckboardCacheImages(true);
cache.Rasterize(r_context, dummy_draw_function, draw_checkerboard);
ASSERT_TRUE(did_draw_checkerboard);
}
TEST(RasterCache, AccessThresholdOfZeroDisablesCachingForSkPicture) {
size_t threshold = 0;
flutter::RasterCache cache(threshold);

View File

@@ -33,7 +33,6 @@ void MockRasterCache::AddMockLayer(int width, int height) {
.dst_color_space = preroll_context_.dst_color_space,
.matrix = ctm,
.logical_rect = layer.paint_bounds(),
.checkerboard = preroll_context_.checkerboard_offscreen_layers,
// clang-format on
};
UpdateCacheEntry(
@@ -71,7 +70,6 @@ void MockRasterCache::AddMockPicture(int width, int height) {
.dst_color_space = preroll_context_.dst_color_space,
.matrix = ctm,
.logical_rect = display_list->bounds(),
.checkerboard = preroll_context_.checkerboard_offscreen_layers,
// clang-format on
};
UpdateCacheEntry(RasterCacheKeyID(display_list->unique_id(),