Commit 40ba8a25 authored by Xida Chen's avatar Xida Chen Committed by Commit Bot

[OT-PW] Make entries in PaintWorkletImageCache ref counted

Currently, the PaintWorkletImageCache keeps a hashmap from
PaintWorkletInput to a PaintRecord. The problem is that the PaintRecord
could be used by multiple tiles for rasterization, while the main
thread might be trying to purge the cache.

This CL makes the PaintRecord ref counted. Whenever a tile is using it
for rasterization, the counter will increment. When the rasterization
is done, the counter will decrement.

Bug: 907897
Change-Id: Idaf1a1f191c084b7531c3a17e5163288e8ba1d4b
Reviewed-on: https://chromium-review.googlesource.com/c/1439761Reviewed-by: default avatarvmpstr <vmpstr@chromium.org>
Commit-Queue: Xida Chen <xidachen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#626872}
parent 0c9fc2f3
......@@ -50,12 +50,13 @@ scoped_refptr<TileTask> PaintWorkletImageCache::GetTaskForPaintWorkletImage(
// then there is no need to call the Paint() function.
void PaintWorkletImageCache::PaintImageInTask(const PaintImage& paint_image) {
sk_sp<PaintRecord> record = painter_->Paint();
records_[paint_image.paint_worklet_input()] = record;
records_[paint_image.paint_worklet_input()] =
std::make_pair(std::move(record), 0);
}
PaintRecord* PaintWorkletImageCache::GetPaintRecordForTest(
PaintWorkletInput* input) {
return records_[input].get();
return records_[input].first.get();
}
} // namespace cc
......@@ -5,6 +5,8 @@
#ifndef CC_TILES_PAINT_WORKLET_IMAGE_CACHE_H_
#define CC_TILES_PAINT_WORKLET_IMAGE_CACHE_H_
#include <utility>
#include "base/containers/flat_map.h"
#include "cc/cc_export.h"
#include "cc/paint/draw_image.h"
......@@ -32,15 +34,19 @@ class CC_EXPORT PaintWorkletImageCache {
void PaintImageInTask(const PaintImage& paint_image);
PaintRecord* GetPaintRecordForTest(PaintWorkletInput* input);
const base::flat_map<PaintWorkletInput*, sk_sp<PaintRecord>>&
const base::flat_map<PaintWorkletInput*,
std::pair<sk_sp<PaintRecord>, size_t>>&
GetRecordsForTest() {
return records_;
}
private:
// The PaintRecord is produced by PaintWorkletLayerPainter::Paint(), and used
// for raster.
base::flat_map<PaintWorkletInput*, sk_sp<PaintRecord>> records_;
// This is a map of paint worklet inputs to a pair of paint record and a
// reference count. The paint record is the representation of the worklet
// output based on the input, and the reference count is the number of times
// that it is used for tile rasterization.
base::flat_map<PaintWorkletInput*, std::pair<sk_sp<PaintRecord>, size_t>>
records_;
// The PaintWorkletImageCache is owned by ImageController, which has the same
// life time as the LayerTreeHostImpl, that guarantees that the painter will
// live as long as the LayerTreeHostImpl.
......
......@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <utility>
#include "cc/tiles/paint_worklet_image_cache.h"
#include "cc/paint/draw_image.h"
......@@ -87,15 +89,17 @@ TEST(PaintWorkletImageCacheTest, MultipleRecordsInCache) {
TestTileTaskRunner::ProcessTask(task1.get());
TestTileTaskRunner::ProcessTask(task2.get());
base::flat_map<PaintWorkletInput*, sk_sp<PaintRecord>> records =
cache.GetRecordsForTest();
base::flat_map<PaintWorkletInput*, std::pair<sk_sp<PaintRecord>, size_t>>
records = cache.GetRecordsForTest();
EXPECT_EQ(records.size(), 2u);
PaintRecord* record1 = records[paint_image1.paint_worklet_input()].get();
PaintRecord* record1 =
records[paint_image1.paint_worklet_input()].first.get();
EXPECT_TRUE(record1);
TestPaintRecord(record1);
PaintRecord* record2 = records[paint_image2.paint_worklet_input()].get();
PaintRecord* record2 =
records[paint_image2.paint_worklet_input()].first.get();
EXPECT_TRUE(record2);
TestPaintRecord(record2);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment