Skip to content

Commit

Permalink
Throttle picture raster cache (flutter#7759)
Browse files Browse the repository at this point in the history
This decreases worst_frame_rasterizer_time_millis from 30ms to 10ms when
we enabled picture raster cache in tiles_scroll (i.e., lower the
threshold from 10 to 5).
  • Loading branch information
liyuqian authored Feb 9, 2019
1 parent a6753b0 commit 68396ae
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
12 changes: 10 additions & 2 deletions flow/raster_cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ void RasterCacheResult::draw(SkCanvas& canvas, const SkPaint* paint) const {
canvas.drawImage(image_, bounds.fLeft, bounds.fTop, paint);
}

RasterCache::RasterCache(size_t threshold)
: threshold_(threshold), checkerboard_images_(false), weak_factory_(this) {}
RasterCache::RasterCache(size_t threshold, size_t picture_cache_limit_per_frame)
: threshold_(threshold),
picture_cache_limit_per_frame_(picture_cache_limit_per_frame),
checkerboard_images_(false),
weak_factory_(this) {}

RasterCache::~RasterCache() = default;

Expand Down Expand Up @@ -182,6 +185,9 @@ bool RasterCache::Prepare(GrContext* context,
SkColorSpace* dst_color_space,
bool is_complex,
bool will_change) {
if (picture_cached_this_frame_ >= picture_cache_limit_per_frame_) {
return false;
}
if (!IsPictureWorthRasterizing(picture, will_change, is_complex)) {
// We only deal with pictures that are worthy of rasterization.
return false;
Expand Down Expand Up @@ -211,6 +217,7 @@ bool RasterCache::Prepare(GrContext* context,
entry.image = RasterizePicture(picture, context, transformation_matrix,
dst_color_space, checkerboard_images_);
}
picture_cached_this_frame_++;
return true;
}

Expand All @@ -232,6 +239,7 @@ void RasterCache::SweepAfterFrame() {
using LayerCache = LayerRasterCacheKey::Map<Entry>;
SweepOneCacheAfterFrame<PictureCache, PictureCache::iterator>(picture_cache_);
SweepOneCacheAfterFrame<LayerCache, LayerCache::iterator>(layer_cache_);
picture_cached_this_frame_ = 0;
}

void RasterCache::Clear() {
Expand Down
14 changes: 13 additions & 1 deletion flow/raster_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,15 @@ struct PrerollContext;

class RasterCache {
public:
explicit RasterCache(size_t threshold = 3);
// The default max number of picture raster caches to be generated per frame.
// Generating too many caches in one frame may cause jank on that frame. This
// limit allows us to throttle the cache and distribute the work across
// multiple frames.
static constexpr int kDefaultPictureCacheLimitPerFrame = 3;

explicit RasterCache(
size_t threshold = 3,
size_t picture_cache_limit_per_frame = kDefaultPictureCacheLimitPerFrame);

~RasterCache();

Expand All @@ -67,6 +75,8 @@ class RasterCache {
// 1. The picture is not worth rasterizing
// 2. The matrix is singular
// 3. The picture is accessed too few times
// 4. There are too many pictures to be cached in the current frame.
// (See also kDefaultPictureCacheLimitPerFrame.)
bool Prepare(GrContext* context,
SkPicture* picture,
const SkMatrix& transformation_matrix,
Expand Down Expand Up @@ -110,6 +120,8 @@ class RasterCache {
}

const size_t threshold_;
const size_t picture_cache_limit_per_frame_;
size_t picture_cached_this_frame_ = 0;
PictureRasterCacheKey::Map<Entry> picture_cache_;
LayerRasterCacheKey::Map<Entry> layer_cache_;
bool checkerboard_images_;
Expand Down

0 comments on commit 68396ae

Please sign in to comment.