Skip to content

Commit

Permalink
Defer decisions about RasterCache actions until after Preroll is comp…
Browse files Browse the repository at this point in the history
…lete (flutter#31892)
  • Loading branch information
JsouLiang authored Jun 30, 2022
1 parent 870bc60 commit 557655b
Show file tree
Hide file tree
Showing 49 changed files with 2,379 additions and 1,038 deletions.
9 changes: 9 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ FILE: ../../../flutter/flow/layer_snapshot_store.h
FILE: ../../../flutter/flow/layers/backdrop_filter_layer.cc
FILE: ../../../flutter/flow/layers/backdrop_filter_layer.h
FILE: ../../../flutter/flow/layers/backdrop_filter_layer_unittests.cc
FILE: ../../../flutter/flow/layers/cacheable_layer.cc
FILE: ../../../flutter/flow/layers/cacheable_layer.h
FILE: ../../../flutter/flow/layers/checkerboard_layertree_unittests.cc
FILE: ../../../flutter/flow/layers/clip_path_layer.cc
FILE: ../../../flutter/flow/layers/clip_path_layer.h
Expand All @@ -155,11 +157,15 @@ FILE: ../../../flutter/flow/layers/container_layer_unittests.cc
FILE: ../../../flutter/flow/layers/display_list_layer.cc
FILE: ../../../flutter/flow/layers/display_list_layer.h
FILE: ../../../flutter/flow/layers/display_list_layer_unittests.cc
FILE: ../../../flutter/flow/layers/display_list_raster_cache_item.cc
FILE: ../../../flutter/flow/layers/display_list_raster_cache_item.h
FILE: ../../../flutter/flow/layers/image_filter_layer.cc
FILE: ../../../flutter/flow/layers/image_filter_layer.h
FILE: ../../../flutter/flow/layers/image_filter_layer_unittests.cc
FILE: ../../../flutter/flow/layers/layer.cc
FILE: ../../../flutter/flow/layers/layer.h
FILE: ../../../flutter/flow/layers/layer_raster_cache_item.cc
FILE: ../../../flutter/flow/layers/layer_raster_cache_item.h
FILE: ../../../flutter/flow/layers/layer_tree.cc
FILE: ../../../flutter/flow/layers/layer_tree.h
FILE: ../../../flutter/flow/layers/layer_tree_unittests.cc
Expand Down Expand Up @@ -194,9 +200,12 @@ FILE: ../../../flutter/flow/paint_utils.cc
FILE: ../../../flutter/flow/paint_utils.h
FILE: ../../../flutter/flow/raster_cache.cc
FILE: ../../../flutter/flow/raster_cache.h
FILE: ../../../flutter/flow/raster_cache_item.h
FILE: ../../../flutter/flow/raster_cache_key.cc
FILE: ../../../flutter/flow/raster_cache_key.h
FILE: ../../../flutter/flow/raster_cache_unittests.cc
FILE: ../../../flutter/flow/raster_cache_util.cc
FILE: ../../../flutter/flow/raster_cache_util.h
FILE: ../../../flutter/flow/rtree.cc
FILE: ../../../flutter/flow/rtree.h
FILE: ../../../flutter/flow/rtree_unittests.cc
Expand Down
9 changes: 9 additions & 0 deletions flow/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ source_set("flow") {
"layer_snapshot_store.h",
"layers/backdrop_filter_layer.cc",
"layers/backdrop_filter_layer.h",
"layers/cacheable_layer.cc",
"layers/cacheable_layer.h",
"layers/clip_path_layer.cc",
"layers/clip_path_layer.h",
"layers/clip_rect_layer.cc",
Expand All @@ -35,10 +37,14 @@ source_set("flow") {
"layers/container_layer.h",
"layers/display_list_layer.cc",
"layers/display_list_layer.h",
"layers/display_list_raster_cache_item.cc",
"layers/display_list_raster_cache_item.h",
"layers/image_filter_layer.cc",
"layers/image_filter_layer.h",
"layers/layer.cc",
"layers/layer.h",
"layers/layer_raster_cache_item.cc",
"layers/layer_raster_cache_item.h",
"layers/layer_tree.cc",
"layers/layer_tree.h",
"layers/offscreen_surface.cc",
Expand All @@ -63,8 +69,11 @@ source_set("flow") {
"paint_utils.h",
"raster_cache.cc",
"raster_cache.h",
"raster_cache_item.h",
"raster_cache_key.cc",
"raster_cache_key.h",
"raster_cache_util.cc",
"raster_cache_util.h",
"rtree.cc",
"rtree.h",
"skia_gpu_object.h",
Expand Down
38 changes: 38 additions & 0 deletions flow/layers/cacheable_layer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "flutter/flow/layers/cacheable_layer.h"
#include "flutter/flow/raster_cache.h"
#include "flutter/flow/raster_cache_item.h"

namespace flutter {

AutoCache::AutoCache(RasterCacheItem* raster_cache_item,
PrerollContext* context,
const SkMatrix& matrix)
: raster_cache_item_(raster_cache_item),
context_(context),
matrix_(matrix) {
if (IsCacheEnabled()) {
raster_cache_item->PrerollSetup(context, matrix);
}
}

bool AutoCache::IsCacheEnabled() {
return raster_cache_item_ && context_ && context_->raster_cache;
}

AutoCache::~AutoCache() {
if (IsCacheEnabled()) {
raster_cache_item_->PrerollFinalize(context_, matrix_);
}
}

CacheableContainerLayer::CacheableContainerLayer(int layer_cached_threshold,
bool can_cache_children) {
layer_raster_cache_item_ = LayerRasterCacheItem::Make(
this, layer_cached_threshold, can_cache_children);
}

} // namespace flutter
50 changes: 50 additions & 0 deletions flow/layers/cacheable_layer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef FLUTTER_FLOW_LAYERS_CACHEABLE_LAYER_H_
#define FLUTTER_FLOW_LAYERS_CACHEABLE_LAYER_H_

#include <memory>

#include "flutter/flow/layers/container_layer.h"
#include "flutter/flow/layers/display_list_raster_cache_item.h"
#include "flutter/flow/layers/layer_raster_cache_item.h"

namespace flutter {

class AutoCache {
public:
AutoCache(RasterCacheItem* raster_cache_item,
PrerollContext* context,
const SkMatrix& matrix);

void ShouldNotBeCached() { raster_cache_item_ = nullptr; }

~AutoCache();

private:
inline bool IsCacheEnabled();
RasterCacheItem* raster_cache_item_ = nullptr;
PrerollContext* context_ = nullptr;
const SkMatrix& matrix_;
};

class CacheableContainerLayer : public ContainerLayer {
public:
explicit CacheableContainerLayer(
int layer_cached_threshold =
RasterCacheUtil::kMinimumRendersBeforeCachingFilterLayer,
bool can_cache_children = false);

const LayerRasterCacheItem* raster_cache_item() const {
return layer_raster_cache_item_.get();
}

protected:
std::unique_ptr<LayerRasterCacheItem> layer_raster_cache_item_;
};

} // namespace flutter

#endif // FLUTTER_FLOW_LAYERS_CACHEABLE_LAYER_H_
25 changes: 17 additions & 8 deletions flow/layers/clip_path_layer_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

#include "flutter/flow/layers/clip_path_layer.h"

#include "flutter/flow/layers/layer_tree.h"
#include "flutter/flow/layers/opacity_layer.h"
#include "flutter/flow/raster_cache_item.h"
#include "flutter/flow/testing/layer_test.h"
#include "flutter/flow/testing/mock_layer.h"
#include "flutter/fml/macros.h"
#include "flutter/testing/mock_canvas.h"
#include "gtest/gtest.h"
#include "include/core/SkPaint.h"

namespace flutter {
namespace testing {
Expand Down Expand Up @@ -501,24 +505,29 @@ TEST_F(ClipPathLayerTest, LayerCached) {

use_mock_raster_cache();

const auto* clip_cache_item = layer->raster_cache_item();

EXPECT_EQ(raster_cache()->GetLayerCachedEntriesCount(), (size_t)0);
EXPECT_FALSE(raster_cache()->Draw(layer.get(), cache_canvas,
RasterCacheLayerStrategy::kLayer));

layer->Preroll(preroll_context(), initial_transform);
LayerTree::TryToRasterCache(cacheable_items(), &paint_context());

EXPECT_EQ(raster_cache()->GetLayerCachedEntriesCount(), (size_t)0);
EXPECT_FALSE(raster_cache()->Draw(layer.get(), cache_canvas,
RasterCacheLayerStrategy::kLayer));
EXPECT_EQ(clip_cache_item->cache_state(), RasterCacheItem::CacheState::kNone);

layer->Preroll(preroll_context(), initial_transform);
LayerTree::TryToRasterCache(cacheable_items(), &paint_context());
EXPECT_EQ(raster_cache()->GetLayerCachedEntriesCount(), (size_t)0);
EXPECT_FALSE(raster_cache()->Draw(layer.get(), cache_canvas,
RasterCacheLayerStrategy::kLayer));
EXPECT_EQ(clip_cache_item->cache_state(), RasterCacheItem::CacheState::kNone);

layer->Preroll(preroll_context(), initial_transform);
LayerTree::TryToRasterCache(cacheable_items(), &paint_context());
EXPECT_EQ(raster_cache()->GetLayerCachedEntriesCount(), (size_t)1);
EXPECT_TRUE(raster_cache()->Draw(layer.get(), cache_canvas,
RasterCacheLayerStrategy::kLayer));
EXPECT_EQ(clip_cache_item->cache_state(),
RasterCacheItem::CacheState::kCurrent);
SkPaint paint;
EXPECT_TRUE(raster_cache()->Draw(clip_cache_item->GetId().value(),
cache_canvas, &paint));
}

} // namespace testing
Expand Down
22 changes: 13 additions & 9 deletions flow/layers/clip_rect_layer_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "flutter/flow/layers/clip_rect_layer.h"

#include "flutter/flow/layers/layer_tree.h"
#include "flutter/flow/layers/opacity_layer.h"
#include "flutter/flow/testing/layer_test.h"
#include "flutter/flow/testing/mock_layer.h"
Expand Down Expand Up @@ -491,24 +492,27 @@ TEST_F(ClipRectLayerTest, LayerCached) {

use_mock_raster_cache();

EXPECT_EQ(raster_cache()->GetLayerCachedEntriesCount(), (size_t)0);
EXPECT_FALSE(raster_cache()->Draw(layer.get(), cache_canvas,
RasterCacheLayerStrategy::kLayer));
const auto* clip_cache_item = layer->raster_cache_item();

layer->Preroll(preroll_context(), initial_transform);
LayerTree::TryToRasterCache(cacheable_items(), &paint_context());

EXPECT_EQ(raster_cache()->GetLayerCachedEntriesCount(), (size_t)0);
EXPECT_FALSE(raster_cache()->Draw(layer.get(), cache_canvas,
RasterCacheLayerStrategy::kLayer));
EXPECT_EQ(clip_cache_item->cache_state(), RasterCacheItem::CacheState::kNone);

layer->Preroll(preroll_context(), initial_transform);
LayerTree::TryToRasterCache(cacheable_items(), &paint_context());
EXPECT_EQ(raster_cache()->GetLayerCachedEntriesCount(), (size_t)0);
EXPECT_FALSE(raster_cache()->Draw(layer.get(), cache_canvas,
RasterCacheLayerStrategy::kLayer));
EXPECT_EQ(clip_cache_item->cache_state(), RasterCacheItem::CacheState::kNone);

layer->Preroll(preroll_context(), initial_transform);
LayerTree::TryToRasterCache(cacheable_items(), &paint_context());
EXPECT_EQ(raster_cache()->GetLayerCachedEntriesCount(), (size_t)1);
EXPECT_TRUE(raster_cache()->Draw(layer.get(), cache_canvas,
RasterCacheLayerStrategy::kLayer));
EXPECT_EQ(clip_cache_item->cache_state(),
RasterCacheItem::CacheState::kCurrent);
SkPaint paint;
EXPECT_TRUE(raster_cache()->Draw(clip_cache_item->GetId().value(),
cache_canvas, &paint));
}

} // namespace testing
Expand Down
57 changes: 48 additions & 9 deletions flow/layers/clip_rrect_layer_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "flutter/flow/layers/clip_rrect_layer.h"

#include "flutter/flow/layers/layer_tree.h"
#include "flutter/flow/layers/opacity_layer.h"
#include "flutter/flow/testing/layer_test.h"
#include "flutter/flow/testing/mock_layer.h"
Expand Down Expand Up @@ -486,6 +487,7 @@ TEST_F(ClipRRectLayerTest, OpacityInheritanceSaveLayerPainting) {

TEST_F(ClipRRectLayerTest, LayerCached) {
auto path1 = SkPath().addRect({10, 10, 30, 30});
SkPaint paint = SkPaint();
auto mock1 = MockLayer::MakeOpacityCompatible(path1);
SkRect clip_rect = SkRect::MakeWH(500, 500);
SkRRect clip_r_rect = SkRRect::MakeRectXY(clip_rect, 20, 20);
Expand All @@ -500,24 +502,61 @@ TEST_F(ClipRRectLayerTest, LayerCached) {

use_mock_raster_cache();

EXPECT_EQ(raster_cache()->GetLayerCachedEntriesCount(), (size_t)0);
EXPECT_FALSE(raster_cache()->Draw(layer.get(), cache_canvas,
RasterCacheLayerStrategy::kLayer));
const auto* clip_cache_item = layer->raster_cache_item();

layer->Preroll(preroll_context(), initial_transform);
LayerTree::TryToRasterCache(cacheable_items(), &paint_context());

EXPECT_EQ(raster_cache()->GetLayerCachedEntriesCount(), (size_t)0);
EXPECT_FALSE(raster_cache()->Draw(layer.get(), cache_canvas,
RasterCacheLayerStrategy::kLayer));
EXPECT_EQ(clip_cache_item->cache_state(), RasterCacheItem::CacheState::kNone);

layer->Preroll(preroll_context(), initial_transform);
LayerTree::TryToRasterCache(cacheable_items(), &paint_context());
EXPECT_EQ(raster_cache()->GetLayerCachedEntriesCount(), (size_t)0);
EXPECT_FALSE(raster_cache()->Draw(layer.get(), cache_canvas,
RasterCacheLayerStrategy::kLayer));
EXPECT_EQ(clip_cache_item->cache_state(), RasterCacheItem::CacheState::kNone);

layer->Preroll(preroll_context(), initial_transform);
LayerTree::TryToRasterCache(cacheable_items(), &paint_context());
EXPECT_EQ(raster_cache()->GetLayerCachedEntriesCount(), (size_t)1);
EXPECT_TRUE(raster_cache()->Draw(layer.get(), cache_canvas,
RasterCacheLayerStrategy::kLayer));
EXPECT_EQ(clip_cache_item->cache_state(),
RasterCacheItem::CacheState::kCurrent);
EXPECT_TRUE(raster_cache()->Draw(clip_cache_item->GetId().value(),
cache_canvas, &paint));
}

TEST_F(ClipRRectLayerTest, NoSaveLayerShouldNotCache) {
auto path1 = SkPath().addRect({10, 10, 30, 30});

auto mock1 = MockLayer::MakeOpacityCompatible(path1);
SkRect clip_rect = SkRect::MakeWH(500, 500);
SkRRect clip_r_rect = SkRRect::MakeRectXY(clip_rect, 20, 20);
auto layer = std::make_shared<ClipRRectLayer>(clip_r_rect, Clip::antiAlias);
layer->Add(mock1);

auto initial_transform = SkMatrix::Translate(50.0, 25.5);
SkMatrix cache_ctm = initial_transform;
SkCanvas cache_canvas;
cache_canvas.setMatrix(cache_ctm);

use_mock_raster_cache();

const auto* clip_cache_item = layer->raster_cache_item();

layer->Preroll(preroll_context(), initial_transform);
LayerTree::TryToRasterCache(cacheable_items(), &paint_context());

EXPECT_EQ(raster_cache()->GetLayerCachedEntriesCount(), (size_t)0);
EXPECT_EQ(clip_cache_item->cache_state(), RasterCacheItem::CacheState::kNone);

layer->Preroll(preroll_context(), initial_transform);
LayerTree::TryToRasterCache(cacheable_items(), &paint_context());
EXPECT_EQ(raster_cache()->GetLayerCachedEntriesCount(), (size_t)0);
EXPECT_EQ(clip_cache_item->cache_state(), RasterCacheItem::CacheState::kNone);

layer->Preroll(preroll_context(), initial_transform);
LayerTree::TryToRasterCache(cacheable_items(), &paint_context());
EXPECT_EQ(raster_cache()->GetLayerCachedEntriesCount(), (size_t)0);
EXPECT_EQ(clip_cache_item->cache_state(), RasterCacheItem::CacheState::kNone);
}

} // namespace testing
Expand Down
Loading

0 comments on commit 557655b

Please sign in to comment.