skia2/tools/gpu/MemoryCache.h
Brian Osman f0de96f7b8 Reland "Redesign program key construction"
This is a reland of bbbf1a7f50

Original change's description:
> Redesign program key construction
>
> This does two things:
> 1) Moves responsibility for bit-packing portions of the key into the key
>    itself. A new GrKeyBuilder type manages adding bits, with asserts to
>    ensure a value always fits in the requested number. In theory this
>    will let us generate smaller keys overall, at the expense of slightly
>    more complex code during construction.
> 2) Adds a string label parameter for key methods that fold in data. For
>    new methods, the label is required. To ease migration, the old add32
>    does not require a label (yet). This will let us generate detailed,
>    human readable keys, either based on SK_DEBUG, or a runtime option
>    (if we're comfortable paying the cost).
>
> Bug: skia:11372
> Change-Id: Ib0f941551e0dbadabbd2a7de912b00e9e766b166
> Reviewed-on: https://skia-review.googlesource.com/c/skia/+/377876
> Commit-Queue: Brian Osman <brianosman@google.com>
> Reviewed-by: Brian Salomon <bsalomon@google.com>

Bug: skia:11372
Cq-Include-Trybots: luci.skia.skia.primary:Test-Win10-MSVC-Golo-GPU-QuadroP400-x86_64-Debug-All-Vulkan
Change-Id: I179ed581bc9ba772191e727274ac0ac6979ebdf3
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/378778
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
2021-03-03 17:44:00 +00:00

96 lines
2.8 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, const SkString& description) 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.fDescription, 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, const SkString& description)
: fData(SkData::MakeWithCopy(data.data(), data.size()))
, fDescription(description)
, fHitCount(1) {}
Value(const Value& that) = default;
Value& operator=(const Value&) = default;
sk_sp<SkData> fData;
SkString fDescription;
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