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)
: cache_map_(CacheMap::NO_AUTO_EVICT), max_budget_(max_budget_bytes) {}
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();
}
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);
DCHECK(cache_map_.Peek(key) == cache_map_.end());
......@@ -40,7 +42,7 @@ void ClientPaintCache::Put(PaintDataType type, PaintCacheId id, size_t size) {
void ClientPaintCache::Purge(PurgedData* purged_data) {
while (bytes_used_ > max_budget_) {
auto it = cache_map_.rbegin();
PaintDataType type = it->first.first;
PaintCacheDataType type = it->first.first;
PaintCacheId id = it->first.second;
(*purged_data)[static_cast<uint32_t>(type)].push_back(id);
......@@ -76,14 +78,14 @@ SkPath* ServicePaintCache::GetPath(PaintCacheId id) {
return it == cached_paths_.end() ? nullptr : &it->second;
}
void ServicePaintCache::Purge(PaintDataType type,
void ServicePaintCache::Purge(PaintCacheDataType type,
size_t n,
const volatile PaintCacheId* ids) {
switch (type) {
case PaintDataType::kTextBlob:
case PaintCacheDataType::kTextBlob:
EraseFromMap(&cached_blobs_, n, ids);
return;
case PaintDataType::kPath:
case PaintCacheDataType::kPath:
EraseFromMap(&cached_paths_, n, ids);
return;
}
......
......@@ -15,23 +15,41 @@
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 PaintCacheIds = std::vector<PaintCacheId>;
enum class PaintDataType : uint32_t { kTextBlob, kPath, kLast = kPath };
constexpr size_t PaintDataTypeCount =
static_cast<uint32_t>(PaintDataType::kLast) + 1u;
enum class PaintCacheDataType : uint32_t { kTextBlob, kPath, kLast = kPath };
constexpr size_t PaintCacheDataTypeCount =
static_cast<uint32_t>(PaintCacheDataType::kLast) + 1u;
class CC_PAINT_EXPORT ClientPaintCache {
public:
explicit ClientPaintCache(size_t max_budget_bytes);
~ClientPaintCache();
bool Get(PaintDataType type, PaintCacheId id);
void Put(PaintDataType type, PaintCacheId id, size_t size);
bool Get(PaintCacheDataType type, PaintCacheId id);
void Put(PaintCacheDataType type, PaintCacheId id, size_t size);
// Populates |purged_data| with the list of ids which should be purged from
// the ServicePaintCache.
using PurgedData = PaintCacheIds[PaintDataTypeCount];
using PurgedData = PaintCacheIds[PaintCacheDataTypeCount];
void Purge(PurgedData* purged_data);
// Notifies that all entries should be purged from the ServicePaintCache.
......@@ -40,7 +58,7 @@ class CC_PAINT_EXPORT ClientPaintCache {
private:
using CacheMap =
base::MRUCache<std::pair<PaintDataType, PaintCacheId>, size_t>;
base::MRUCache<std::pair<PaintCacheDataType, PaintCacheId>, size_t>;
CacheMap cache_map_;
const size_t max_budget_;
size_t bytes_used_ = 0u;
......@@ -63,7 +81,9 @@ class CC_PAINT_EXPORT ServicePaintCache {
void PutPath(PaintCacheId, SkPath path);
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();
bool empty() const { return cached_blobs_.empty() && cached_paths_.empty(); }
......
......@@ -33,7 +33,9 @@ SkPath CreatePath() {
class PaintCacheTest : public ::testing::TestWithParam<uint32_t> {
public:
PaintDataType GetType() { return static_cast<PaintDataType>(GetParam()); }
PaintCacheDataType GetType() {
return static_cast<PaintCacheDataType>(GetParam());
}
};
TEST_P(PaintCacheTest, ClientBasic) {
......@@ -65,7 +67,7 @@ TEST_P(PaintCacheTest, ClientPurgeAll) {
TEST_P(PaintCacheTest, ServiceBasic) {
ServicePaintCache service_cache;
switch (GetType()) {
case PaintDataType::kTextBlob: {
case PaintCacheDataType::kTextBlob: {
auto blob = CreateBlob();
auto id = blob->uniqueID();
EXPECT_EQ(nullptr, service_cache.GetTextBlob(id));
......@@ -76,7 +78,7 @@ TEST_P(PaintCacheTest, ServiceBasic) {
service_cache.PutTextBlob(id, blob);
} break;
case PaintDataType::kPath: {
case PaintCacheDataType::kPath: {
auto path = CreatePath();
auto id = path.getGenerationID();
EXPECT_EQ(nullptr, service_cache.GetPath(id));
......@@ -98,7 +100,7 @@ INSTANTIATE_TEST_CASE_P(
P,
PaintCacheTest,
::testing::Range(static_cast<uint32_t>(0),
static_cast<uint32_t>(PaintDataType::kLast)));
static_cast<uint32_t>(PaintCacheDataType::kLast)));
} // namespace
} // namespace cc
......@@ -170,7 +170,7 @@ void PaintOpWriter::Write(const SkPath& path) {
if (!valid_)
return;
if (options_.paint_cache->Get(PaintDataType::kPath, id))
if (options_.paint_cache->Get(PaintCacheDataType::kPath, id))
return;
uint64_t bytes_required = path.writeToMemory(nullptr);
if (bytes_required > remaining_bytes_) {
......@@ -180,7 +180,7 @@ void PaintOpWriter::Write(const SkPath& path) {
size_t bytes_written = path.writeToMemory(memory_);
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;
memory_ += bytes_written;
remaining_bytes_ -= bytes_written;
......@@ -311,7 +311,7 @@ void PaintOpWriter::Write(const sk_sp<SkTextBlob>& blob) {
if (!valid_)
return;
if (options_.paint_cache->Get(PaintDataType::kTextBlob, blob_id))
if (options_.paint_cache->Get(PaintCacheDataType::kTextBlob, blob_id))
return;
auto encodeTypeface = [](SkTypeface* tf, void* ctx) -> sk_sp<SkData> {
......@@ -329,7 +329,8 @@ void PaintOpWriter::Write(const sk_sp<SkTextBlob>& blob) {
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;
memory_ += bytes_written;
remaining_bytes_ -= bytes_written;
......
......@@ -1388,18 +1388,18 @@ void RasterImplementation::FlushPaintCachePurgedEntries() {
return;
paint_cache_->Purge(&temp_paint_cache_purged_data_);
for (uint32_t i = static_cast<uint32_t>(cc::PaintDataType::kTextBlob);
i < cc::PaintDataTypeCount; ++i) {
for (uint32_t i = static_cast<uint32_t>(cc::PaintCacheDataType::kTextBlob);
i < cc::PaintCacheDataTypeCount; ++i) {
auto& ids = temp_paint_cache_purged_data_[i];
if (ids.empty())
continue;
switch (static_cast<cc::PaintDataType>(i)) {
case cc::PaintDataType::kTextBlob:
switch (static_cast<cc::PaintCacheDataType>(i)) {
case cc::PaintCacheDataType::kTextBlob:
helper_->DeletePaintCacheTextBlobsINTERNALImmediate(ids.size(),
ids.data());
break;
case cc::PaintDataType::kPath:
case cc::PaintCacheDataType::kPath:
helper_->DeletePaintCachePathsINTERNALImmediate(ids.size(), ids.data());
break;
}
......
......@@ -2966,7 +2966,7 @@ void RasterDecoderImpl::DeletePaintCacheTextBlobsINTERNALHelper(
return;
}
paint_cache_->Purge(cc::PaintDataType::kTextBlob, n, paint_cache_ids);
paint_cache_->Purge(cc::PaintCacheDataType::kTextBlob, n, paint_cache_ids);
}
void RasterDecoderImpl::DeletePaintCachePathsINTERNALHelper(
......@@ -2979,7 +2979,7 @@ void RasterDecoderImpl::DeletePaintCachePathsINTERNALHelper(
return;
}
paint_cache_->Purge(cc::PaintDataType::kPath, n, paint_cache_ids);
paint_cache_->Purge(cc::PaintCacheDataType::kPath, n, paint_cache_ids);
}
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