2018-07-11 19:32:05 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkData.h"
|
|
|
|
#include "include/gpu/GrContextOptions.h"
|
|
|
|
#include "include/private/SkChecksum.h"
|
2018-07-11 19:32:05 +00:00
|
|
|
|
2018-09-19 15:31:27 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
|
2018-07-11 19:32:05 +00:00
|
|
|
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;
|
2019-04-12 15:47:19 +00:00
|
|
|
void reset() {
|
2020-06-05 15:11:36 +00:00
|
|
|
this->resetCacheStats();
|
2019-04-12 15:47:19 +00:00
|
|
|
fMap.clear();
|
|
|
|
}
|
2018-07-11 19:32:05 +00:00
|
|
|
|
|
|
|
sk_sp<SkData> load(const SkData& key) override;
|
|
|
|
void store(const SkData& key, const SkData& data) override;
|
|
|
|
int numCacheMisses() const { return fCacheMissCnt; }
|
2020-06-05 15:11:36 +00:00
|
|
|
int numCacheStores() const { return fCacheStoreCnt; }
|
|
|
|
void resetCacheStats() {
|
|
|
|
fCacheMissCnt = 0;
|
|
|
|
fCacheStoreCnt = 0;
|
|
|
|
}
|
2018-07-11 19:32:05 +00:00
|
|
|
|
2019-04-08 20:40:36 +00:00
|
|
|
void writeShadersToDisk(const char* path, GrBackendApi backend);
|
|
|
|
|
2019-04-12 15:47:19 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-11 19:32:05 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2019-04-08 20:40:36 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2018-07-11 19:32:05 +00:00
|
|
|
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;
|
2020-06-05 15:11:36 +00:00
|
|
|
int fCacheStoreCnt = 0;
|
2019-04-08 20:40:36 +00:00
|
|
|
std::unordered_map<Key, Value, Hash> fMap;
|
2018-07-11 19:32:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace sk_gpu_test
|
|
|
|
|
|
|
|
#endif
|