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

Commit 7f7fc90

Browse files
committed
raster cache process analysis
1 parent 5d1fddc commit 7f7fc90

File tree

6 files changed

+48
-30
lines changed

6 files changed

+48
-30
lines changed

flow/layers/display_list_raster_cache_item.cc

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ void DisplayListRasterCacheItem::PrerollSetup(PrerollContext* context,
8181
return;
8282
}
8383

84+
matrix_ = matrix;
8485
transformation_matrix_ = matrix;
8586
transformation_matrix_.preTranslate(offset_.x(), offset_.y());
8687

@@ -117,11 +118,18 @@ void DisplayListRasterCacheItem::PrerollFinalize(PrerollContext* context,
117118
context->subtree_can_inherit_opacity = true;
118119
cache_state_ = CacheState::kCurrent;
119120
} else {
121+
// if the bounds with the cull rect is not intersect, we only touch the
122+
// cache entry. And should not cache the display list
123+
cache_state_ = CacheState::kNone;
120124
raster_cache->Touch(key_id_, matrix);
121125
}
122126
return;
123127
}
124128

129+
bool DisplayListRasterCacheItem::Touch(const PaintContext* context) const {
130+
return context->raster_cache->Touch(key_id_, transformation_matrix_);
131+
}
132+
125133
bool DisplayListRasterCacheItem::Draw(const PaintContext& context,
126134
const SkPaint* paint) const {
127135
return Draw(context, context.leaf_nodes_canvas, paint);
@@ -133,27 +141,20 @@ bool DisplayListRasterCacheItem::Draw(const PaintContext& context,
133141
if (!context.raster_cache || !canvas) {
134142
return false;
135143
}
136-
if (cache_state_ == CacheState::kCurrent) {
137-
return context.raster_cache->Draw(key_id_, *canvas, paint);
138-
}
139-
// This display_list doesn't cache itself, this only increase the entry
140-
// access_count;
141-
context.raster_cache->Touch(key_id_, canvas->getTotalMatrix());
142-
return false;
144+
return context.raster_cache->Draw(key_id_, *canvas, paint);
143145
}
144146

145147
static const auto* flow_type = "RasterCacheFlow::DisplayList";
146148

147149
bool DisplayListRasterCacheItem::TryToPrepareRasterCache(
148-
const PaintContext& context,
149-
bool parent_cached) const {
150+
const PaintContext& context) const {
150151
// If we don't have raster_cache we should not cache the current display_list.
151152
// If the current node's ancestor has been cached we also should not cache the
152153
// current node. In the current frame, the raster_cache will collect all
153154
// display_list or picture_list to calculate the memory they used, we
154155
// shouldn't cache the current node if the memory is more significant than the
155156
// limit.
156-
if (cache_state_ == kNone || !context.raster_cache || parent_cached ||
157+
if (cache_state_ == kNone || !context.raster_cache ||
157158
!context.raster_cache->GenerateNewCacheInThisFrame()) {
158159
return false;
159160
}

flow/layers/display_list_raster_cache_item.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,15 @@ class DisplayListRasterCacheItem : public RasterCacheItem {
3838
void PrerollFinalize(PrerollContext* context,
3939
const SkMatrix& matrix) override;
4040

41+
bool Touch(const PaintContext* context) const override;
42+
4143
bool Draw(const PaintContext& context, const SkPaint* paint) const override;
4244

4345
bool Draw(const PaintContext& context,
4446
SkCanvas* canvas,
4547
const SkPaint* paint) const override;
4648

47-
bool TryToPrepareRasterCache(const PaintContext& context,
48-
bool parent_cached = false) const override;
49+
bool TryToPrepareRasterCache(const PaintContext& context) const override;
4950

5051
void ModifyMatrix(SkPoint offset) const {
5152
matrix_ = matrix_.preTranslate(offset.x(), offset.y());

flow/layers/layer_raster_cache_item.cc

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,11 @@ void LayerRasterCacheItem::PrerollFinalize(PrerollContext* context,
4242
if (!context->raster_cache || !context->raster_cached_entries) {
4343
return;
4444
}
45-
// We've marked the cache entry that we would like to cache so it stays
46-
// alive, but if the following conditions apply then we need to set our
47-
// state back to kDoNotCache so that we don't populate the entry later.
48-
if (context->has_platform_view || context->has_texture_layer ||
49-
!SkRect::Intersects(context->cull_rect, layer_->paint_bounds())) {
50-
return;
51-
}
5245
child_items_ = context->raster_cached_entries->size() - child_items_;
46+
// Determine cache current layer or cache children.
5347
if (num_cache_attempts_ >= layer_cached_threshold_) {
5448
// the layer can be cached
5549
cache_state_ = CacheState::kCurrent;
56-
context->raster_cache->MarkSeen(key_id_, matrix_);
5750
} else {
5851
num_cache_attempts_++;
5952
// access current layer
@@ -67,9 +60,25 @@ void LayerRasterCacheItem::PrerollFinalize(PrerollContext* context,
6760
RasterCacheKeyType::kLayerChildren);
6861
}
6962
cache_state_ = CacheState::kChildren;
70-
context->raster_cache->MarkSeen(layer_children_id_.value(), matrix_);
7163
}
7264
}
65+
// We've marked the cache entry that we would like to cache so it stays
66+
// alive, but if the following conditions apply then we need to set our
67+
// state back to kDoNotCache so that we don't populate the entry later.
68+
if (context->has_platform_view || context->has_texture_layer ||
69+
!SkRect::Intersects(context->cull_rect, layer_->paint_bounds())) {
70+
if (cache_state_ != CacheState::kNone) {
71+
// Only touch the entry, do not to cache the layer or children.
72+
context->raster_cache->Touch(GetId().value(), matrix);
73+
cache_state_ = CacheState::kNone;
74+
}
75+
return;
76+
}
77+
// The layer can be cache, so we should create a entry.
78+
if (cache_state_ != CacheState::kNone) {
79+
// Create Cache entry
80+
context->raster_cache->MarkSeen(GetId().value(), matrix);
81+
}
7382
}
7483

7584
std::optional<RasterCacheKeyID> LayerRasterCacheItem::GetId() const {
@@ -138,9 +147,9 @@ bool Rasterize(RasterCacheItem::CacheState cache_state,
138147

139148
static const auto* flow_type = "RasterCacheFlow::Layer";
140149

141-
bool LayerRasterCacheItem::TryToPrepareRasterCache(const PaintContext& context,
142-
bool parent_cached) const {
143-
if (!context.raster_cache || parent_cached) {
150+
bool LayerRasterCacheItem::TryToPrepareRasterCache(
151+
const PaintContext& context) const {
152+
if (!context.raster_cache) {
144153
return false;
145154
}
146155
if (cache_state_ != kNone) {
@@ -171,6 +180,10 @@ bool LayerRasterCacheItem::Draw(const PaintContext& context,
171180
return Draw(context, context.leaf_nodes_canvas, paint);
172181
}
173182

183+
bool LayerRasterCacheItem::Touch(const PaintContext* context) const {
184+
return context->raster_cache->Touch(GetId().value(), matrix_);
185+
}
186+
174187
bool LayerRasterCacheItem::Draw(const PaintContext& context,
175188
SkCanvas* canvas,
176189
const SkPaint* paint) const {

flow/layers/layer_raster_cache_item.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,13 @@ class LayerRasterCacheItem : public RasterCacheItem {
3838

3939
bool Draw(const PaintContext& context, const SkPaint* paint) const override;
4040

41+
bool Touch(const PaintContext* context) const override;
42+
4143
bool Draw(const PaintContext& context,
4244
SkCanvas* canvas,
4345
const SkPaint* paint) const override;
4446

45-
bool TryToPrepareRasterCache(const PaintContext& context,
46-
bool parent_cached = false) const override;
47+
bool TryToPrepareRasterCache(const PaintContext& context) const override;
4748

4849
void MarkCacheChildren() { can_cache_children_ = true; }
4950

flow/layers/layer_tree.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,13 @@ void LayerTree::TryToRasterCache(
8484
// try to cache current layer
8585
// If parent failed to cache, just proceed to the next entry
8686
// cache current entry, this entry's parent must not cache
87-
if (item->TryToPrepareRasterCache(*paint_context, false)) {
87+
if (item->TryToPrepareRasterCache(*paint_context)) {
8888
// if parent cached, then foreach child layer to touch them.
8989
for (unsigned j = 0; j < item->child_items(); j++) {
9090
auto* child_item = raster_cached_items[i + j + 1];
9191
if (child_item->need_caching()) {
92-
child_item->TryToPrepareRasterCache(*paint_context, true);
92+
// If parent has cached, we only touch this cache entry.
93+
child_item->Touch(paint_context);
9394
}
9495
}
9596
i += item->child_items() + 1;

flow/raster_cache_item.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class RasterCacheItem {
4545
virtual void PrerollFinalize(PrerollContext* context,
4646
const SkMatrix& matrix) = 0;
4747

48+
virtual bool Touch(const PaintContext* context) const = 0;
49+
4850
virtual bool Draw(const PaintContext& context,
4951
const SkPaint* paint) const = 0;
5052

@@ -54,8 +56,7 @@ class RasterCacheItem {
5456

5557
virtual std::optional<RasterCacheKeyID> GetId() const { return key_id_; }
5658

57-
virtual bool TryToPrepareRasterCache(const PaintContext& context,
58-
bool parent_cached = false) const = 0;
59+
virtual bool TryToPrepareRasterCache(const PaintContext& context) const = 0;
5960

6061
unsigned child_items() const { return child_items_; }
6162

0 commit comments

Comments
 (0)