forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reland gpu: Add disk caching for skia generated shaders in OOP-R.
This reverts commit f9395d7. Use the GrContextOptions::PersistentCache API provided by skia to persist shaders generated internally by skia for OOP raster to disk. This requires using a special client id to namespace these shaders, similar to the one used by the InProcessCommandBuffer for viz. While the shaders for different sources are stored seperately on disk, they are finally merged into a single memory cache in the GPU process. In order to maintain a seperate cache for skia generated shaders, this also plumbs the client id for a loaded shader to the GPU process. TBR=tsepez@chromium.org Bug: 854416,840559, 865138 Cq-Include-Trybots: luci.chromium.try:android_optional_gpu_tests_rel;luci.chromium.try:linux_optional_gpu_tests_rel;luci.chromium.try:mac_optional_gpu_tests_rel;luci.chromium.try:win_optional_gpu_tests_rel Change-Id: I65544ccaff96c3154a822dbc2500468fbcac8a0b Reviewed-on: https://chromium-review.googlesource.com/1142829 Commit-Queue: Khushal <khushalsagar@chromium.org> Reviewed-by: Antoine Labour <piman@chromium.org> Cr-Commit-Position: refs/heads/master@{#576742}
- Loading branch information
1 parent
8a9a50c
commit c2667e3
Showing
30 changed files
with
557 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
// Copyright (c) 2018 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "gpu/command_buffer/service/gr_shader_cache.h" | ||
|
||
#include "base/trace_event/trace_event.h" | ||
|
||
namespace gpu { | ||
namespace raster { | ||
namespace { | ||
|
||
std::string MakeString(const SkData* data) { | ||
return std::string(static_cast<const char*>(data->data()), data->size()); | ||
} | ||
|
||
sk_sp<SkData> MakeData(const std::string& str) { | ||
return SkData::MakeWithCopy(str.c_str(), str.length()); | ||
} | ||
|
||
} // namespace | ||
|
||
GrShaderCache::GrShaderCache(size_t max_cache_size_bytes, Client* client) | ||
: cache_size_limit_(max_cache_size_bytes), | ||
store_(Store::NO_AUTO_EVICT), | ||
client_(client) {} | ||
|
||
GrShaderCache::~GrShaderCache() = default; | ||
|
||
sk_sp<SkData> GrShaderCache::load(const SkData& key) { | ||
TRACE_EVENT0("gpu", "GrShaderCache::load"); | ||
DCHECK_NE(current_client_id_, kInvalidClientId); | ||
|
||
CacheKey cache_key(SkData::MakeWithoutCopy(key.data(), key.size())); | ||
auto it = store_.Get(cache_key); | ||
if (it == store_.end()) | ||
return nullptr; | ||
|
||
WriteToDisk(it->first, &it->second); | ||
return it->second.data; | ||
} | ||
|
||
void GrShaderCache::store(const SkData& key, const SkData& data) { | ||
TRACE_EVENT0("gpu", "GrShaderCache::store"); | ||
DCHECK_NE(current_client_id_, kInvalidClientId); | ||
|
||
if (data.size() > cache_size_limit_) | ||
return; | ||
EnforceLimits(data.size()); | ||
|
||
CacheKey cache_key(SkData::MakeWithCopy(key.data(), key.size())); | ||
CacheData cache_data(SkData::MakeWithCopy(data.data(), data.size())); | ||
auto it = store_.Put(std::move(cache_key), std::move(cache_data)); | ||
curr_size_bytes_ += it->second.data->size(); | ||
|
||
WriteToDisk(it->first, &it->second); | ||
} | ||
|
||
void GrShaderCache::PopulateCache(const std::string& key, | ||
const std::string& data) { | ||
if (data.length() > cache_size_limit_) | ||
return; | ||
|
||
EnforceLimits(data.size()); | ||
|
||
CacheKey cache_key(MakeData(key)); | ||
CacheData cache_data(MakeData(data)); | ||
auto it = store_.Put(std::move(cache_key), std::move(cache_data)); | ||
curr_size_bytes_ += it->second.data->size(); | ||
|
||
// This was loaded off the disk cache, no need to push this back for disk | ||
// write. | ||
it->second.pending_disk_write = false; | ||
} | ||
|
||
void GrShaderCache::CacheClientIdOnDisk(int32_t client_id) { | ||
client_ids_to_cache_on_disk_.insert(client_id); | ||
} | ||
|
||
void GrShaderCache::PurgeMemory( | ||
base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) { | ||
size_t original_limit = cache_size_limit_; | ||
|
||
switch (memory_pressure_level) { | ||
case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE: | ||
// This function is only called with moderate or critical pressure. | ||
NOTREACHED(); | ||
return; | ||
case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE: | ||
cache_size_limit_ = cache_size_limit_ / 4; | ||
break; | ||
case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL: | ||
cache_size_limit_ = 0; | ||
break; | ||
} | ||
|
||
EnforceLimits(0u); | ||
cache_size_limit_ = original_limit; | ||
} | ||
|
||
void GrShaderCache::WriteToDisk(const CacheKey& key, CacheData* data) { | ||
DCHECK_NE(current_client_id_, kInvalidClientId); | ||
|
||
if (!data->pending_disk_write) | ||
return; | ||
|
||
// Only cache the shader on disk if this client id is permitted. | ||
if (client_ids_to_cache_on_disk_.count(current_client_id_) == 0) | ||
return; | ||
|
||
data->pending_disk_write = false; | ||
client_->StoreShader(MakeString(key.data.get()), | ||
MakeString(data->data.get())); | ||
} | ||
|
||
void GrShaderCache::EnforceLimits(size_t size_needed) { | ||
DCHECK_LE(size_needed, cache_size_limit_); | ||
|
||
while (size_needed + curr_size_bytes_ > cache_size_limit_) { | ||
auto it = store_.rbegin(); | ||
DCHECK_GE(curr_size_bytes_, it->second.data->size()); | ||
curr_size_bytes_ -= it->second.data->size(); | ||
store_.Erase(it); | ||
} | ||
} | ||
|
||
GrShaderCache::ScopedCacheUse::ScopedCacheUse(GrShaderCache* cache, | ||
int32_t client_id) | ||
: cache_(cache) { | ||
cache_->current_client_id_ = client_id; | ||
} | ||
|
||
GrShaderCache::ScopedCacheUse::~ScopedCacheUse() { | ||
cache_->current_client_id_ = kInvalidClientId; | ||
} | ||
|
||
GrShaderCache::CacheKey::CacheKey(sk_sp<SkData> data) : data(std::move(data)) { | ||
hash = base::Hash(this->data->data(), this->data->size()); | ||
} | ||
GrShaderCache::CacheKey::CacheKey(const CacheKey& other) = default; | ||
GrShaderCache::CacheKey::CacheKey(CacheKey&& other) = default; | ||
GrShaderCache::CacheKey& GrShaderCache::CacheKey::operator=( | ||
const CacheKey& other) = default; | ||
GrShaderCache::CacheKey& GrShaderCache::CacheKey::operator=(CacheKey&& other) = | ||
default; | ||
GrShaderCache::CacheKey::~CacheKey() = default; | ||
|
||
bool GrShaderCache::CacheKey::operator==(const CacheKey& other) const { | ||
return data->equals(other.data.get()); | ||
} | ||
|
||
GrShaderCache::CacheData::CacheData(sk_sp<SkData> data) | ||
: data(std::move(data)) {} | ||
GrShaderCache::CacheData::CacheData(CacheData&& other) = default; | ||
GrShaderCache::CacheData& GrShaderCache::CacheData::operator=( | ||
CacheData&& other) = default; | ||
GrShaderCache::CacheData::~CacheData() = default; | ||
|
||
bool GrShaderCache::CacheData::operator==(const CacheData& other) const { | ||
return data->equals(other.data.get()); | ||
} | ||
|
||
} // namespace raster | ||
} // namespace gpu |
Oops, something went wrong.