Commit cb653fbe authored by Khushal's avatar Khushal Committed by Commit Bot

cc: Follow up on PaintCache addition.

Add class comments and some renames for PaintCache. Following up from:
https://chromium-review.googlesource.com/c/chromium/src/+/1321190/4

R=enne@chromium.org

Bug: 894200
Change-Id: Ie8a0629071f8156e26945be7e9c61e04742158e5
Reviewed-on: https://chromium-review.googlesource.com/c/1336040Reviewed-by: default avatarAntoine Labour <piman@chromium.org>
Reviewed-by: default avatarenne <enne@chromium.org>
Commit-Queue: Khushal <khushalsagar@chromium.org>
Cr-Commit-Position: refs/heads/master@{#608298}
parent b9b009d2
...@@ -25,11 +25,13 @@ ClientPaintCache::ClientPaintCache(size_t max_budget_bytes) ...@@ -25,11 +25,13 @@ ClientPaintCache::ClientPaintCache(size_t max_budget_bytes)
: cache_map_(CacheMap::NO_AUTO_EVICT), max_budget_(max_budget_bytes) {} : cache_map_(CacheMap::NO_AUTO_EVICT), max_budget_(max_budget_bytes) {}
ClientPaintCache::~ClientPaintCache() = default; ClientPaintCache::~ClientPaintCache() = default;
bool ClientPaintCache::Get(PaintDataType type, PaintCacheId id) { bool ClientPaintCache::Get(PaintCacheDataType type, PaintCacheId id) {
return cache_map_.Get(std::make_pair(type, id)) != cache_map_.end(); return cache_map_.Get(std::make_pair(type, id)) != cache_map_.end();
} }
void ClientPaintCache::Put(PaintDataType type, PaintCacheId id, size_t size) { void ClientPaintCache::Put(PaintCacheDataType type,
PaintCacheId id,
size_t size) {
auto key = std::make_pair(type, id); auto key = std::make_pair(type, id);
DCHECK(cache_map_.Peek(key) == cache_map_.end()); DCHECK(cache_map_.Peek(key) == cache_map_.end());
...@@ -40,7 +42,7 @@ void ClientPaintCache::Put(PaintDataType type, PaintCacheId id, size_t size) { ...@@ -40,7 +42,7 @@ void ClientPaintCache::Put(PaintDataType type, PaintCacheId id, size_t size) {
void ClientPaintCache::Purge(PurgedData* purged_data) { void ClientPaintCache::Purge(PurgedData* purged_data) {
while (bytes_used_ > max_budget_) { while (bytes_used_ > max_budget_) {
auto it = cache_map_.rbegin(); auto it = cache_map_.rbegin();
PaintDataType type = it->first.first; PaintCacheDataType type = it->first.first;
PaintCacheId id = it->first.second; PaintCacheId id = it->first.second;
(*purged_data)[static_cast<uint32_t>(type)].push_back(id); (*purged_data)[static_cast<uint32_t>(type)].push_back(id);
...@@ -76,14 +78,14 @@ SkPath* ServicePaintCache::GetPath(PaintCacheId id) { ...@@ -76,14 +78,14 @@ SkPath* ServicePaintCache::GetPath(PaintCacheId id) {
return it == cached_paths_.end() ? nullptr : &it->second; return it == cached_paths_.end() ? nullptr : &it->second;
} }
void ServicePaintCache::Purge(PaintDataType type, void ServicePaintCache::Purge(PaintCacheDataType type,
size_t n, size_t n,
const volatile PaintCacheId* ids) { const volatile PaintCacheId* ids) {
switch (type) { switch (type) {
case PaintDataType::kTextBlob: case PaintCacheDataType::kTextBlob:
EraseFromMap(&cached_blobs_, n, ids); EraseFromMap(&cached_blobs_, n, ids);
return; return;
case PaintDataType::kPath: case PaintCacheDataType::kPath:
EraseFromMap(&cached_paths_, n, ids); EraseFromMap(&cached_paths_, n, ids);
return; return;
} }
......
...@@ -15,23 +15,41 @@ ...@@ -15,23 +15,41 @@
namespace cc { namespace cc {
// PaintCache is used to cache high frequency small paint data types, like
// SkTextBlob and SkPath in the GPU service. The ClientPaintCache budgets and
// controls the cache state in the ServicePaintCache, regularly purging old
// entries returned in ClientPaintCache::Purge from the service side cache. In
// addition to this, the complete cache is cleared during the raster context
// idle cleanup. This effectively means that the cache budget is used as working
// memory that is only kept while we are actively rasterizing.
//
// The entries are serialized by the caller during paint op serialization, and
// the cache assumes the deserialization and purging to be done in order for
// accurately tracking the service side state in ServicePaintCache.
//
// Note that while TransferCache should be used for large data types that would
// benefit from a shared cache budgeted across all clients, using a client
// controlled PaintCache with a tighter budget is better for these data types
// since it avoids the need for cross-process ref-counting required by the
// TransferCache.
using PaintCacheId = uint32_t; using PaintCacheId = uint32_t;
using PaintCacheIds = std::vector<PaintCacheId>; using PaintCacheIds = std::vector<PaintCacheId>;
enum class PaintDataType : uint32_t { kTextBlob, kPath, kLast = kPath }; enum class PaintCacheDataType : uint32_t { kTextBlob, kPath, kLast = kPath };
constexpr size_t PaintDataTypeCount = constexpr size_t PaintCacheDataTypeCount =
static_cast<uint32_t>(PaintDataType::kLast) + 1u; static_cast<uint32_t>(PaintCacheDataType::kLast) + 1u;
class CC_PAINT_EXPORT ClientPaintCache { class CC_PAINT_EXPORT ClientPaintCache {
public: public:
explicit ClientPaintCache(size_t max_budget_bytes); explicit ClientPaintCache(size_t max_budget_bytes);
~ClientPaintCache(); ~ClientPaintCache();
bool Get(PaintDataType type, PaintCacheId id); bool Get(PaintCacheDataType type, PaintCacheId id);
void Put(PaintDataType type, PaintCacheId id, size_t size); void Put(PaintCacheDataType type, PaintCacheId id, size_t size);
// Populates |purged_data| with the list of ids which should be purged from // Populates |purged_data| with the list of ids which should be purged from
// the ServicePaintCache. // the ServicePaintCache.
using PurgedData = PaintCacheIds[PaintDataTypeCount]; using PurgedData = PaintCacheIds[PaintCacheDataTypeCount];
void Purge(PurgedData* purged_data); void Purge(PurgedData* purged_data);
// Notifies that all entries should be purged from the ServicePaintCache. // Notifies that all entries should be purged from the ServicePaintCache.
...@@ -40,7 +58,7 @@ class CC_PAINT_EXPORT ClientPaintCache { ...@@ -40,7 +58,7 @@ class CC_PAINT_EXPORT ClientPaintCache {
private: private:
using CacheMap = using CacheMap =
base::MRUCache<std::pair<PaintDataType, PaintCacheId>, size_t>; base::MRUCache<std::pair<PaintCacheDataType, PaintCacheId>, size_t>;
CacheMap cache_map_; CacheMap cache_map_;
const size_t max_budget_; const size_t max_budget_;
size_t bytes_used_ = 0u; size_t bytes_used_ = 0u;
...@@ -63,7 +81,9 @@ class CC_PAINT_EXPORT ServicePaintCache { ...@@ -63,7 +81,9 @@ class CC_PAINT_EXPORT ServicePaintCache {
void PutPath(PaintCacheId, SkPath path); void PutPath(PaintCacheId, SkPath path);
SkPath* GetPath(PaintCacheId id); SkPath* GetPath(PaintCacheId id);
void Purge(PaintDataType type, size_t n, const volatile PaintCacheId* ids); void Purge(PaintCacheDataType type,
size_t n,
const volatile PaintCacheId* ids);
void PurgeAll(); void PurgeAll();
bool empty() const { return cached_blobs_.empty() && cached_paths_.empty(); } bool empty() const { return cached_blobs_.empty() && cached_paths_.empty(); }
......
...@@ -33,7 +33,9 @@ SkPath CreatePath() { ...@@ -33,7 +33,9 @@ SkPath CreatePath() {
class PaintCacheTest : public ::testing::TestWithParam<uint32_t> { class PaintCacheTest : public ::testing::TestWithParam<uint32_t> {
public: public:
PaintDataType GetType() { return static_cast<PaintDataType>(GetParam()); } PaintCacheDataType GetType() {
return static_cast<PaintCacheDataType>(GetParam());
}
}; };
TEST_P(PaintCacheTest, ClientBasic) { TEST_P(PaintCacheTest, ClientBasic) {
...@@ -65,7 +67,7 @@ TEST_P(PaintCacheTest, ClientPurgeAll) { ...@@ -65,7 +67,7 @@ TEST_P(PaintCacheTest, ClientPurgeAll) {
TEST_P(PaintCacheTest, ServiceBasic) { TEST_P(PaintCacheTest, ServiceBasic) {
ServicePaintCache service_cache; ServicePaintCache service_cache;
switch (GetType()) { switch (GetType()) {
case PaintDataType::kTextBlob: { case PaintCacheDataType::kTextBlob: {
auto blob = CreateBlob(); auto blob = CreateBlob();
auto id = blob->uniqueID(); auto id = blob->uniqueID();
EXPECT_EQ(nullptr, service_cache.GetTextBlob(id)); EXPECT_EQ(nullptr, service_cache.GetTextBlob(id));
...@@ -76,7 +78,7 @@ TEST_P(PaintCacheTest, ServiceBasic) { ...@@ -76,7 +78,7 @@ TEST_P(PaintCacheTest, ServiceBasic) {
service_cache.PutTextBlob(id, blob); service_cache.PutTextBlob(id, blob);
} break; } break;
case PaintDataType::kPath: { case PaintCacheDataType::kPath: {
auto path = CreatePath(); auto path = CreatePath();
auto id = path.getGenerationID(); auto id = path.getGenerationID();
EXPECT_EQ(nullptr, service_cache.GetPath(id)); EXPECT_EQ(nullptr, service_cache.GetPath(id));
...@@ -98,7 +100,7 @@ INSTANTIATE_TEST_CASE_P( ...@@ -98,7 +100,7 @@ INSTANTIATE_TEST_CASE_P(
P, P,
PaintCacheTest, PaintCacheTest,
::testing::Range(static_cast<uint32_t>(0), ::testing::Range(static_cast<uint32_t>(0),
static_cast<uint32_t>(PaintDataType::kLast))); static_cast<uint32_t>(PaintCacheDataType::kLast)));
} // namespace } // namespace
} // namespace cc } // namespace cc
...@@ -170,7 +170,7 @@ void PaintOpWriter::Write(const SkPath& path) { ...@@ -170,7 +170,7 @@ void PaintOpWriter::Write(const SkPath& path) {
if (!valid_) if (!valid_)
return; return;
if (options_.paint_cache->Get(PaintDataType::kPath, id)) if (options_.paint_cache->Get(PaintCacheDataType::kPath, id))
return; return;
uint64_t bytes_required = path.writeToMemory(nullptr); uint64_t bytes_required = path.writeToMemory(nullptr);
if (bytes_required > remaining_bytes_) { if (bytes_required > remaining_bytes_) {
...@@ -180,7 +180,7 @@ void PaintOpWriter::Write(const SkPath& path) { ...@@ -180,7 +180,7 @@ void PaintOpWriter::Write(const SkPath& path) {
size_t bytes_written = path.writeToMemory(memory_); size_t bytes_written = path.writeToMemory(memory_);
DCHECK_EQ(bytes_written, bytes_required); DCHECK_EQ(bytes_written, bytes_required);
options_.paint_cache->Put(PaintDataType::kPath, id, bytes_written); options_.paint_cache->Put(PaintCacheDataType::kPath, id, bytes_written);
*bytes_to_skip = bytes_written; *bytes_to_skip = bytes_written;
memory_ += bytes_written; memory_ += bytes_written;
remaining_bytes_ -= bytes_written; remaining_bytes_ -= bytes_written;
...@@ -311,7 +311,7 @@ void PaintOpWriter::Write(const sk_sp<SkTextBlob>& blob) { ...@@ -311,7 +311,7 @@ void PaintOpWriter::Write(const sk_sp<SkTextBlob>& blob) {
if (!valid_) if (!valid_)
return; return;
if (options_.paint_cache->Get(PaintDataType::kTextBlob, blob_id)) if (options_.paint_cache->Get(PaintCacheDataType::kTextBlob, blob_id))
return; return;
auto encodeTypeface = [](SkTypeface* tf, void* ctx) -> sk_sp<SkData> { auto encodeTypeface = [](SkTypeface* tf, void* ctx) -> sk_sp<SkData> {
...@@ -329,7 +329,8 @@ void PaintOpWriter::Write(const sk_sp<SkTextBlob>& blob) { ...@@ -329,7 +329,8 @@ void PaintOpWriter::Write(const sk_sp<SkTextBlob>& blob) {
return; return;
} }
options_.paint_cache->Put(PaintDataType::kTextBlob, blob_id, bytes_written); options_.paint_cache->Put(PaintCacheDataType::kTextBlob, blob_id,
bytes_written);
*size_memory = bytes_written; *size_memory = bytes_written;
memory_ += bytes_written; memory_ += bytes_written;
remaining_bytes_ -= bytes_written; remaining_bytes_ -= bytes_written;
......
...@@ -1388,18 +1388,18 @@ void RasterImplementation::FlushPaintCachePurgedEntries() { ...@@ -1388,18 +1388,18 @@ void RasterImplementation::FlushPaintCachePurgedEntries() {
return; return;
paint_cache_->Purge(&temp_paint_cache_purged_data_); paint_cache_->Purge(&temp_paint_cache_purged_data_);
for (uint32_t i = static_cast<uint32_t>(cc::PaintDataType::kTextBlob); for (uint32_t i = static_cast<uint32_t>(cc::PaintCacheDataType::kTextBlob);
i < cc::PaintDataTypeCount; ++i) { i < cc::PaintCacheDataTypeCount; ++i) {
auto& ids = temp_paint_cache_purged_data_[i]; auto& ids = temp_paint_cache_purged_data_[i];
if (ids.empty()) if (ids.empty())
continue; continue;
switch (static_cast<cc::PaintDataType>(i)) { switch (static_cast<cc::PaintCacheDataType>(i)) {
case cc::PaintDataType::kTextBlob: case cc::PaintCacheDataType::kTextBlob:
helper_->DeletePaintCacheTextBlobsINTERNALImmediate(ids.size(), helper_->DeletePaintCacheTextBlobsINTERNALImmediate(ids.size(),
ids.data()); ids.data());
break; break;
case cc::PaintDataType::kPath: case cc::PaintCacheDataType::kPath:
helper_->DeletePaintCachePathsINTERNALImmediate(ids.size(), ids.data()); helper_->DeletePaintCachePathsINTERNALImmediate(ids.size(), ids.data());
break; break;
} }
......
...@@ -2966,7 +2966,7 @@ void RasterDecoderImpl::DeletePaintCacheTextBlobsINTERNALHelper( ...@@ -2966,7 +2966,7 @@ void RasterDecoderImpl::DeletePaintCacheTextBlobsINTERNALHelper(
return; return;
} }
paint_cache_->Purge(cc::PaintDataType::kTextBlob, n, paint_cache_ids); paint_cache_->Purge(cc::PaintCacheDataType::kTextBlob, n, paint_cache_ids);
} }
void RasterDecoderImpl::DeletePaintCachePathsINTERNALHelper( void RasterDecoderImpl::DeletePaintCachePathsINTERNALHelper(
...@@ -2979,7 +2979,7 @@ void RasterDecoderImpl::DeletePaintCachePathsINTERNALHelper( ...@@ -2979,7 +2979,7 @@ void RasterDecoderImpl::DeletePaintCachePathsINTERNALHelper(
return; return;
} }
paint_cache_->Purge(cc::PaintDataType::kPath, n, paint_cache_ids); paint_cache_->Purge(cc::PaintCacheDataType::kPath, n, paint_cache_ids);
} }
void RasterDecoderImpl::DoClearPaintCacheINTERNAL() { void RasterDecoderImpl::DoClearPaintCacheINTERNAL() {
......
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