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

Commit fe86bd1

Browse files
committed
Add SkPicture RasterCache
1 parent b8bb76f commit fe86bd1

10 files changed

+86
-99
lines changed

flow/layers/display_list_layer.cc

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,9 @@ void DisplayListLayer::Preroll(PrerollContext* context,
9393
const SkMatrix& matrix) {
9494
TRACE_EVENT0("flutter", "DisplayListLayer::Preroll");
9595
auto cacheable_entry = RasterCacheableEntry::MarkDisplayListCacheable(
96-
this->display_list(), *context, matrix, offset_, is_complex_,
97-
will_change_);
96+
this->display_list(), *context, matrix, offset_);
9897
context->raster_cached_entries.push_back(cacheable_entry);
99-
100-
auto& cache_entry = context->raster_cached_entries.back();
101-
cache_entry->need_caching = NeedCaching(context, matrix);
98+
cacheable_entry->need_caching = NeedCaching(context, matrix);
10299
}
103100

104101
bool DisplayListLayer::NeedCaching(PrerollContext* context,

flow/layers/picture_layer.cc

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,24 +110,28 @@ sk_sp<SkData> PictureLayer::SerializedPicture() const {
110110
void PictureLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) {
111111
TRACE_EVENT0("flutter", "PictureLayer::Preroll");
112112

113-
SkPicture* sk_picture = picture();
113+
auto cacheable_entry = RasterCacheableEntry::MarkSkPictureCacheable(
114+
this->picture(), *context, matrix, offset_);
115+
context->raster_cached_entries.push_back(cacheable_entry);
116+
cacheable_entry->need_caching = NeedCaching(context, matrix);
117+
}
114118

119+
bool PictureLayer::NeedCaching(PrerollContext* context, const SkMatrix& ctm) {
120+
SkPicture* sk_picture = picture();
115121
SkRect bounds = sk_picture->cullRect().makeOffset(offset_.x(), offset_.y());
122+
set_paint_bounds(bounds);
116123

117124
if (auto* cache = context->raster_cache) {
118125
TRACE_EVENT0("flutter", "PictureLayer::RasterCache (Preroll)");
119126
if (context->cull_rect.intersects(bounds)) {
120-
if (cache->Prepare(context, sk_picture, is_complex_, will_change_, matrix,
121-
offset_)) {
127+
if (cache->ShouldBeCached(context, sk_picture, is_complex_, will_change_,
128+
ctm)) {
122129
context->subtree_can_inherit_opacity = true;
123130
}
124-
} else {
125-
// Don't evict raster cache entry during partial repaint
126-
cache->Touch(sk_picture, matrix);
127131
}
132+
return true;
128133
}
129-
130-
set_paint_bounds(bounds);
134+
return false;
131135
}
132136

133137
void PictureLayer::Paint(PaintContext& context) const {

flow/layers/picture_layer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class PictureLayer : public Layer {
3232

3333
void Paint(PaintContext& context) const override;
3434

35+
bool NeedCaching(PrerollContext* context, const SkMatrix& ctm);
36+
3537
private:
3638
SkPoint offset_;
3739
// Even though pictures themselves are not GPU resources, they may reference

flow/raster_cache.cc

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,12 @@ std::unique_ptr<RasterCacheResult> RasterCache::RasterizeLayer(
258258
});
259259
}
260260

261-
bool RasterCache::Prepare(PrerollContext* context,
262-
SkPicture* picture,
263-
bool is_complex,
264-
bool will_change,
265-
const SkMatrix& untranslated_matrix,
266-
const SkPoint& offset) {
261+
bool RasterCache::ShouldBeCached(PrerollContext* context,
262+
SkPicture* picture,
263+
bool is_complex,
264+
bool will_change,
265+
const SkMatrix& untranslated_matrix,
266+
const SkPoint& offset) {
267267
if (!GenerateNewCacheInThisFrame()) {
268268
return false;
269269
}
@@ -272,7 +272,13 @@ bool RasterCache::Prepare(PrerollContext* context,
272272
// We only deal with pictures that are worthy of rasterization.
273273
return false;
274274
}
275+
return true;
276+
}
275277

278+
bool RasterCache::Prepare(PrerollContext* context,
279+
SkPicture* picture,
280+
const SkMatrix& untranslated_matrix,
281+
const SkPoint& offset) {
276282
SkMatrix transformation_matrix = untranslated_matrix;
277283
transformation_matrix.preTranslate(offset.x(), offset.y());
278284

@@ -327,20 +333,11 @@ bool RasterCache::ShouldBeCached(PrerollContext* context,
327333
return false;
328334
}
329335

330-
SkMatrix transformation_matrix = untranslated_matrix;
331-
transformation_matrix.preTranslate(offset.x(), offset.y());
332-
333-
if (!transformation_matrix.invert(nullptr)) {
334-
// The matrix was singular. No point in going further.
335-
return false;
336-
}
337336
return true;
338337
}
339338

340339
bool RasterCache::Prepare(PrerollContext* context,
341340
DisplayList* display_list,
342-
bool is_complex,
343-
bool will_change,
344341
const SkMatrix& untranslated_matrix,
345342
const SkPoint& offset) {
346343
SkMatrix transformation_matrix = untranslated_matrix;

flow/raster_cache.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,32 +176,40 @@ class RasterCache {
176176
return result;
177177
}
178178

179-
// Return true if the cache is generated.
179+
// Return true if the SkPicture can be cached.
180180
//
181181
// We may return false and not generate the cache if
182182
// 1. There are too many pictures to be cached in the current frame.
183183
// (See also kDefaultPictureAndDispLayListCacheLimitPerFrame.)
184184
// 2. The picture is not worth rasterizing
185185
// 3. The matrix is singular
186186
// 4. The picture is accessed too few times
187+
bool ShouldBeCached(PrerollContext* context,
188+
SkPicture* picture,
189+
bool is_complex,
190+
bool will_change,
191+
const SkMatrix& untranslated_matrix,
192+
const SkPoint& offset = SkPoint());
193+
194+
// Prepare to cache the SkPicture.
195+
// Return true if the cache is generated.
187196
bool Prepare(PrerollContext* context,
188197
SkPicture* picture,
189-
bool is_complex,
190-
bool will_change,
191198
const SkMatrix& untranslated_matrix,
192199
const SkPoint& offset = SkPoint());
193200

201+
// Return true if the DisplayList can be cached.
194202
bool ShouldBeCached(PrerollContext* context,
195203
DisplayList* display_list,
196204
bool is_complex,
197205
bool will_change,
198206
const SkMatrix& untranslated_matrix,
199207
const SkPoint& offset = SkPoint());
200208

209+
// Prepare to cache the DisplayList.
210+
// Return true if the cache is generated.
201211
bool Prepare(PrerollContext* context,
202212
DisplayList* display_list,
203-
bool is_complex,
204-
bool will_change,
205213
const SkMatrix& untranslated_matrix,
206214
const SkPoint& offset = SkPoint());
207215

0 commit comments

Comments
 (0)