skia2/tools/gpu/MemoryCache.cpp
Brian Salomon 00a5eb8c12 Add gltestpersistentcache config that tests GrContextOption's cache.
Uses a new GPU sink that runs each test twice, once to populate the
cache and then again with a new GrContext but a warmed cache. It
verifies that the two generated images are the same.

Change-Id: Iaba195a69751f14ea946afe7174228a813b83a63
Reviewed-on: https://skia-review.googlesource.com/140567
Commit-Queue: Brian Salomon <bsalomon@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
2018-07-11 20:06:33 +00:00

57 lines
1.7 KiB
C++

/*
* Copyright 2018 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "MemoryCache.h"
#include "SkBase64.h"
// Change this to 1 to log cache hits/misses/stores using SkDebugf.
#define LOG_MEMORY_CACHE 0
static SkString data_to_str(const SkData& data) {
size_t encodeLength = SkBase64::Encode(data.data(), data.size(), nullptr);
SkString str;
str.resize(encodeLength);
SkBase64::Encode(data.data(), data.size(), str.writable_str());
static constexpr size_t kMaxLength = 60;
static constexpr char kTail[] = "...";
static const size_t kTailLen = strlen(kTail);
bool overlength = encodeLength > kMaxLength;
if (overlength) {
str = SkString(str.c_str(), kMaxLength - kTailLen);
str.append(kTail);
}
return str;
}
namespace sk_gpu_test {
sk_sp<SkData> MemoryCache::load(const SkData& key) {
auto result = fMap.find(key);
if (result == fMap.end()) {
if (LOG_MEMORY_CACHE) {
SkDebugf("Load Key: %s\n\tNot Found.\n\n", data_to_str(key).c_str());
}
++fCacheMissCnt;
return nullptr;
}
if (LOG_MEMORY_CACHE) {
SkDebugf("Load Key: %s\n\tFound Data: %s\n\n", data_to_str(key).c_str(),
data_to_str(*result->second).c_str());
}
return result->second;
}
void MemoryCache::store(const SkData& key, const SkData& data) {
if (LOG_MEMORY_CACHE) {
SkDebugf("Store Key: %s\n\tData: %s\n\n", data_to_str(key).c_str(),
data_to_str(data).c_str());
}
fMap[Key(key)] = SkData::MakeWithCopy(data.data(), data.size());
}
} // namespace sk_gpu_test