43f443f086
Our "header" reading and writing code didn't agree, so we always failed to recognize cached program binaries. The asserts in the testing sink failed to notice, because we did get a 100% cache hit rate, but we immediately discarded the data we received. We now also check that we didn't insert anything into the cache, as a proxy for doing any shader compile work. That change, plus the tweak to set cached=false when the header fields are invalid (like we do if we encounter problems further in the blob) detected the problem. Adding the version tag to the start of the encoded blob fixes the test, and means that program binary caching is actually working again. This code still looks (and is) fragile. The next CL is going to rewrite things to use SkReadBuffer and SkWriteBuffer, make the parsing code less brittle, and give us a more robust way to detect failure anywhere in the stream. Bug: skia:9402 Change-Id: I0329f088e0afce3998494d91ef2206e5eb9cac42 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/294599 Reviewed-by: Brian Salomon <bsalomon@google.com> Commit-Queue: Brian Osman <brianosman@google.com>
94 lines
2.7 KiB
C++
94 lines
2.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.
|
|
*/
|
|
|
|
#ifndef MemoryCache_DEFINED
|
|
#define MemoryCache_DEFINED
|
|
|
|
#include "include/core/SkData.h"
|
|
#include "include/gpu/GrContextOptions.h"
|
|
#include "include/private/SkChecksum.h"
|
|
|
|
#include <unordered_map>
|
|
|
|
namespace sk_gpu_test {
|
|
|
|
/**
|
|
* This class can be used to maintain an in memory record of all programs cached by GrContext.
|
|
* It can be shared by multiple GrContexts so long as those GrContexts are created with the same
|
|
* options and will have the same GrCaps (e.g. same backend, same GL context creation parameters,
|
|
* ...).
|
|
*/
|
|
class MemoryCache : public GrContextOptions::PersistentCache {
|
|
public:
|
|
MemoryCache() = default;
|
|
MemoryCache(const MemoryCache&) = delete;
|
|
MemoryCache& operator=(const MemoryCache&) = delete;
|
|
void reset() {
|
|
this->resetCacheStats();
|
|
fMap.clear();
|
|
}
|
|
|
|
sk_sp<SkData> load(const SkData& key) override;
|
|
void store(const SkData& key, const SkData& data) override;
|
|
int numCacheMisses() const { return fCacheMissCnt; }
|
|
int numCacheStores() const { return fCacheStoreCnt; }
|
|
void resetCacheStats() {
|
|
fCacheMissCnt = 0;
|
|
fCacheStoreCnt = 0;
|
|
}
|
|
|
|
void writeShadersToDisk(const char* path, GrBackendApi backend);
|
|
|
|
template <typename Fn>
|
|
void foreach(Fn&& fn) {
|
|
for (auto it = fMap.begin(); it != fMap.end(); ++it) {
|
|
fn(it->first.fKey, it->second.fData, it->second.fHitCount);
|
|
}
|
|
}
|
|
|
|
private:
|
|
struct Key {
|
|
Key() = default;
|
|
Key(const SkData& key) : fKey(SkData::MakeWithCopy(key.data(), key.size())) {}
|
|
Key(const Key& that) = default;
|
|
Key& operator=(const Key&) = default;
|
|
bool operator==(const Key& that) const {
|
|
return that.fKey->size() == fKey->size() &&
|
|
!memcmp(fKey->data(), that.fKey->data(), that.fKey->size());
|
|
}
|
|
sk_sp<const SkData> fKey;
|
|
};
|
|
|
|
struct Value {
|
|
Value() = default;
|
|
Value(const SkData& data)
|
|
: fData(SkData::MakeWithCopy(data.data(), data.size()))
|
|
, fHitCount(1) {}
|
|
Value(const Value& that) = default;
|
|
Value& operator=(const Value&) = default;
|
|
|
|
sk_sp<SkData> fData;
|
|
int fHitCount;
|
|
};
|
|
|
|
struct Hash {
|
|
using argument_type = Key;
|
|
using result_type = uint32_t;
|
|
uint32_t operator()(const Key& key) const {
|
|
return key.fKey ? SkOpts::hash_fn(key.fKey->data(), key.fKey->size(), 0) : 0;
|
|
}
|
|
};
|
|
|
|
int fCacheMissCnt = 0;
|
|
int fCacheStoreCnt = 0;
|
|
std::unordered_map<Key, Value, Hash> fMap;
|
|
};
|
|
|
|
} // namespace sk_gpu_test
|
|
|
|
#endif
|