Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 3d0731a

Browse files
committed
Add CacheableLayer, Prepare RasterCache use local PrerollContext
1 parent 5c59e73 commit 3d0731a

13 files changed

+172
-63
lines changed

flow/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ source_set("flow") {
2020
"instrumentation.h",
2121
"layers/backdrop_filter_layer.cc",
2222
"layers/backdrop_filter_layer.h",
23+
"layers/cacheable_layer.cc",
24+
"layers/cacheable_layer.h",
2325
"layers/clip_path_layer.cc",
2426
"layers/clip_path_layer.h",
2527
"layers/clip_rect_layer.cc",

flow/layers/cacheable_layer.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
namespace flutter {} // namespace flutter

flow/layers/cacheable_layer.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef FLUTTER_FLOW_LAYERS_CACHEABL_LAYER_H_
6+
#define FLUTTER_FLOW_LAYERS_CACHEABL_LAYER_H_
7+
8+
#include <memory>
9+
#include "flutter/flow/embedded_views.h"
10+
#include "flutter/flow/layers/layer.h"
11+
#include "flutter/flow/raster_cache.h"
12+
#include "include/core/SkColor.h"
13+
#include "include/core/SkMatrix.h"
14+
15+
namespace flutter {
16+
17+
class CacheableLayer : public Layer {
18+
public:
19+
enum class CacheType { kNone, kCurrent, kChildren };
20+
21+
bool IsCacheable() { return true; }
22+
23+
virtual void TryToPrepareRasterCache(PrerollContext* context,
24+
const SkMatrix& matrix) {}
25+
26+
virtual CacheType NeedCaching(PrerollContext* context,
27+
const SkMatrix& ctm) = 0;
28+
29+
virtual ~CacheableLayer() = default;
30+
};
31+
32+
} // namespace flutter
33+
34+
#endif // FLUTTER_FLOW_LAYERS_CACHEABL_LAYER_H_

flow/layers/container_layer.cc

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -189,27 +189,21 @@ void ContainerLayer::PaintChildren(PaintContext& context) const {
189189
}
190190

191191
void ContainerLayer::TryToPrepareRasterCache(PrerollContext* context,
192-
const SkMatrix& ctm) {
193-
if (!context->has_platform_view && !context->has_texture_layer &&
194-
context->raster_cache &&
195-
SkRect::Intersects(context->cull_rect, this->paint_bounds())) {
196-
context->raster_cache->Prepare(context, this, ctm);
197-
} else if (context->raster_cache) {
198-
// Don't evict raster cache entry during partial repaint
199-
context->raster_cache->Touch(this, ctm);
200-
}
192+
const SkMatrix& matrix) {
193+
TryToPrepareRasterCache(context, this, matrix);
201194
}
202195

196+
// TODO(JsouLiang): will remove this method
203197
void ContainerLayer::TryToPrepareRasterCache(PrerollContext* context,
204198
Layer* layer,
205-
const SkMatrix& matrix) {
199+
const SkMatrix& ctm) {
206200
if (!context->has_platform_view && !context->has_texture_layer &&
207201
context->raster_cache &&
208202
SkRect::Intersects(context->cull_rect, layer->paint_bounds())) {
209-
context->raster_cache->Prepare(context, layer, matrix);
203+
context->raster_cache->Prepare(context, layer, ctm);
210204
} else if (context->raster_cache) {
211205
// Don't evict raster cache entry during partial repaint
212-
context->raster_cache->Touch(layer, matrix);
206+
context->raster_cache->Touch(layer, ctm);
213207
}
214208
}
215209

@@ -258,10 +252,14 @@ ContainerLayer* MergedContainerLayer::GetChildContainer() const {
258252
return static_cast<ContainerLayer*>(layers()[0].get());
259253
}
260254

261-
Layer* MergedContainerLayer::GetCacheableChild() const {
255+
CacheableLayer* MergedContainerLayer::GetCacheableChild() const {
262256
ContainerLayer* child_container = GetChildContainer();
263257
if (child_container->layers().size() == 1) {
264-
return child_container->layers()[0].get();
258+
auto* child = child_container->layers()[0].get();
259+
if (child->IsCacheable()) {
260+
return static_cast<CacheableLayer*>(child);
261+
}
262+
return nullptr;
265263
}
266264

267265
return child_container;

flow/layers/container_layer.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77

88
#include <vector>
99

10-
#include "flutter/flow/layers/layer.h"
11-
10+
#include "flutter/flow/layers/cacheable_layer.h"
1211
namespace flutter {
1312

14-
class ContainerLayer : public Layer {
13+
class ContainerLayer : public CacheableLayer {
1514
public:
1615
ContainerLayer();
1716

@@ -20,9 +19,6 @@ class ContainerLayer : public Layer {
2019

2120
virtual void Add(std::shared_ptr<Layer> layer);
2221

23-
void TryToPrepareRasterCache(PrerollContext* context,
24-
const SkMatrix& ctm) override;
25-
2622
void Preroll(PrerollContext* context, const SkMatrix& matrix) override;
2723
void Paint(PaintContext& context) const override;
2824

@@ -31,6 +27,14 @@ class ContainerLayer : public Layer {
3127
virtual void DiffChildren(DiffContext* context,
3228
const ContainerLayer* old_layer);
3329

30+
CacheableLayer::CacheType NeedCaching(PrerollContext* context,
31+
const SkMatrix& ctm) override {
32+
return CacheableLayer::CacheType::kNone;
33+
}
34+
35+
void TryToPrepareRasterCache(PrerollContext* context,
36+
const SkMatrix& matrix) override;
37+
3438
protected:
3539
void PrerollChildren(PrerollContext* context,
3640
const SkMatrix& child_matrix,
@@ -129,7 +133,7 @@ class MergedContainerLayer : public ContainerLayer {
129133
* @see GetChildContainer()
130134
* @return the best candidate Layer for caching the children
131135
*/
132-
Layer* GetCacheableChild() const;
136+
CacheableLayer* GetCacheableChild() const;
133137

134138
private:
135139
FML_DISALLOW_COPY_AND_ASSIGN(MergedContainerLayer);

flow/layers/display_list_layer.cc

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include "flutter/flow/layers/display_list_layer.h"
66

77
#include "flutter/display_list/display_list_builder.h"
8-
#include "flutter/flow/layers/layer.h"
98

109
namespace flutter {
1110

@@ -92,28 +91,31 @@ bool DisplayListLayer::Compare(DiffContext::Statistics& statistics,
9291
void DisplayListLayer::Preroll(PrerollContext* context,
9392
const SkMatrix& matrix) {
9493
TRACE_EVENT0("flutter", "DisplayListLayer::Preroll");
95-
context->raster_cached_entries.emplace_back(RasterCacheEntry(this));
94+
context->raster_cached_entries.emplace_back(
95+
RasterCacheEntry(this, *context, matrix));
9696

9797
auto& cache_entry = context->raster_cached_entries.back();
9898
// display layer is a leaf node
9999
cache_entry.num_child_entries = 0;
100-
cache_entry.need_caching = IsNeedCached(context, matrix);
100+
auto cached_type = NeedCaching(context, matrix);
101+
cache_entry.need_caching = cached_type == CacheableLayer::CacheType::kCurrent;
101102
}
102103

103-
bool DisplayListLayer::IsNeedCached(PrerollContext* context,
104-
const SkMatrix& ctm) {
104+
CacheableLayer::CacheType DisplayListLayer::NeedCaching(PrerollContext* context,
105+
const SkMatrix& ctm) {
105106
DisplayList* disp_list = display_list();
106107

107108
SkRect bounds = disp_list->bounds().makeOffset(offset_.x(), offset_.y());
108109

109110
if (auto* cache = context->raster_cache) {
110111
TRACE_EVENT0("flutter", "DisplayListLayer::RasterCache (Preroll)");
111-
if (context->cull_rect.intersects(bounds)) {
112-
return cache->ShouldBeCached(context, disp_list, is_complex_,
113-
will_change_, ctm);
112+
if (context->cull_rect.intersects(bounds) &&
113+
cache->ShouldBeCached(context, disp_list, is_complex_, will_change_,
114+
ctm)) {
115+
return CacheableLayer::CacheType::kCurrent;
114116
}
115117
}
116-
return false;
118+
return CacheableLayer::CacheType::kNone;
117119
}
118120

119121
void DisplayListLayer::TryToPrepareRasterCache(PrerollContext* context,

flow/layers/display_list_layer.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
#define FLUTTER_FLOW_LAYERS_DISPLAY_LIST_LAYER_H_
77

88
#include "flutter/display_list/display_list.h"
9-
#include "flutter/flow/layers/layer.h"
9+
#include "flutter/flow/layers/cacheable_layer.h"
1010
#include "flutter/flow/raster_cache.h"
1111
#include "flutter/flow/skia_gpu_object.h"
1212
#include "include/core/SkMatrix.h"
1313

1414
namespace flutter {
1515

16-
class DisplayListLayer : public Layer {
16+
class DisplayListLayer : public CacheableLayer {
1717
public:
1818
static constexpr size_t kMaxBytesToCompare = 10000;
1919

@@ -34,7 +34,8 @@ class DisplayListLayer : public Layer {
3434
return this;
3535
}
3636

37-
bool IsNeedCached(PrerollContext* context, const SkMatrix& ctm) override;
37+
CacheableLayer::CacheType NeedCaching(PrerollContext* context,
38+
const SkMatrix& ctm) override;
3839

3940
void TryToPrepareRasterCache(PrerollContext* context,
4041
const SkMatrix& ctm) override;

flow/layers/image_filter_layer.cc

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
#include "flutter/flow/layers/image_filter_layer.h"
6+
#include "flutter/flow/layers/layer.h"
67

78
namespace flutter {
89

@@ -43,7 +44,8 @@ void ImageFilterLayer::Preroll(PrerollContext* context,
4344
Layer::AutoPrerollSaveLayerState save =
4445
Layer::AutoPrerollSaveLayerState::Create(context);
4546

46-
context->raster_cached_entries.emplace_back(RasterCacheEntry(this));
47+
context->raster_cached_entries.emplace_back(
48+
RasterCacheEntry(this, *context, matrix));
4749
auto current_index = context->raster_cached_entries.size();
4850

4951
auto& cache_entry = context->raster_cached_entries.back();
@@ -53,7 +55,18 @@ void ImageFilterLayer::Preroll(PrerollContext* context,
5355

5456
cache_entry.num_child_entries =
5557
context->raster_cached_entries.size() - current_index;
56-
cache_entry.need_caching = IsNeedCached(context, matrix);
58+
auto cache_type = NeedCaching(context, matrix);
59+
60+
cache_entry.need_caching = false;
61+
if (cache_type == CacheableLayer::CacheType::kCurrent) {
62+
cache_entry.layer = this;
63+
} else if (cache_type == CacheableLayer::CacheType::kChildren) {
64+
// Replace Cacheable child
65+
auto* cacheable_child = GetCacheableChild();
66+
if (cacheable_child) {
67+
cache_entry.UpdateRasterCacheEntry(cacheable_child, *context, matrix);
68+
}
69+
}
5770

5871
if (!filter_) {
5972
set_paint_bounds(child_bounds);
@@ -68,10 +81,10 @@ void ImageFilterLayer::Preroll(PrerollContext* context,
6881
set_paint_bounds(child_bounds);
6982
}
7083

71-
bool ImageFilterLayer::IsNeedCached(PrerollContext* context,
72-
const SkMatrix& ctm) {
84+
CacheableLayer::CacheType ImageFilterLayer::NeedCaching(PrerollContext* context,
85+
const SkMatrix& ctm) {
7386
if (render_count_ >= kMinimumRendersBeforeCachingFilterLayer) {
74-
return true;
87+
return CacheableLayer::CacheType::kCurrent;
7588
}
7689
transformed_filter_ = nullptr;
7790
// This ImageFilterLayer is not yet considered stable so we
@@ -93,9 +106,9 @@ bool ImageFilterLayer::IsNeedCached(PrerollContext* context,
93106
// stable between frames and also avoiding a rendering surface
94107
// switch during the Paint phase even if they are not stable.
95108
// This benefit is seen most during animations.
96-
return true;
109+
return CacheableLayer::CacheType::kChildren;
97110
}
98-
return false;
111+
return CacheableLayer::CacheType::kNone;
99112
}
100113

101114
void ImageFilterLayer::Paint(PaintContext& context) const {

flow/layers/image_filter_layer.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef FLUTTER_FLOW_LAYERS_IMAGE_FILTER_LAYER_H_
66
#define FLUTTER_FLOW_LAYERS_IMAGE_FILTER_LAYER_H_
77

8+
#include "flutter/flow/layers/cacheable_layer.h"
89
#include "flutter/flow/layers/container_layer.h"
910
#include "third_party/skia/include/core/SkImageFilter.h"
1011

@@ -20,7 +21,8 @@ class ImageFilterLayer : public MergedContainerLayer {
2021

2122
void Paint(PaintContext& context) const override;
2223

23-
bool IsNeedCached(PrerollContext* context, const SkMatrix& ctm) override;
24+
CacheableLayer::CacheType NeedCaching(PrerollContext* context,
25+
const SkMatrix& ctm) override;
2426

2527
private:
2628
// The ImageFilterLayer might cache the filtered output of this layer

flow/layers/layer.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "flutter/flow/layers/layer.h"
66
#include <algorithm>
77

8+
#include "flutter/flow/diff_context.h"
89
#include "flutter/flow/paint_utils.h"
910
#include "flutter/flow/raster_cache.h"
1011
#include "include/core/SkMatrix.h"
@@ -32,9 +33,6 @@ uint64_t Layer::NextUniqueID() {
3233

3334
void Layer::Preroll(PrerollContext* context, const SkMatrix& matrix) {}
3435

35-
void Layer::TryToPrepareRasterCache(PrerollContext* context,
36-
const SkMatrix& ctm) {}
37-
3836
Layer::AutoPrerollSaveLayerState::AutoPrerollSaveLayerState(
3937
PrerollContext* preroll_context,
4038
bool save_layer_is_active,

0 commit comments

Comments
 (0)