blob: 22911a5413b27f3d49ee0964fbfc434d85b03d68 [file] [log] [blame]
Brian Salomon00a5eb82018-07-11 15:32:05 -04001/*
2 * Copyright 2018 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef MemoryCache_DEFINED
9#define MemoryCache_DEFINED
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkData.h"
12#include "include/gpu/GrContextOptions.h"
13#include "include/private/SkChecksum.h"
Brian Salomon00a5eb82018-07-11 15:32:05 -040014
Hal Canary8a001442018-09-19 11:31:27 -040015#include <unordered_map>
16
Brian Salomon00a5eb82018-07-11 15:32:05 -040017namespace sk_gpu_test {
18
19/**
20 * This class can be used to maintain an in memory record of all programs cached by GrContext.
21 * It can be shared by multiple GrContexts so long as those GrContexts are created with the same
22 * options and will have the same GrCaps (e.g. same backend, same GL context creation parameters,
23 * ...).
24 */
25class MemoryCache : public GrContextOptions::PersistentCache {
26public:
27 MemoryCache() = default;
28 MemoryCache(const MemoryCache&) = delete;
29 MemoryCache& operator=(const MemoryCache&) = delete;
Brian Osman0b8bb882019-04-12 11:47:19 -040030 void reset() {
31 fCacheMissCnt = 0;
32 fMap.clear();
33 }
Brian Salomon00a5eb82018-07-11 15:32:05 -040034
35 sk_sp<SkData> load(const SkData& key) override;
36 void store(const SkData& key, const SkData& data) override;
37 int numCacheMisses() const { return fCacheMissCnt; }
38 void resetNumCacheMisses() { fCacheMissCnt = 0; }
39
Brian Osman5aa11fb2019-04-08 16:40:36 -040040 void writeShadersToDisk(const char* path, GrBackendApi backend);
41
Brian Osman0b8bb882019-04-12 11:47:19 -040042 template <typename Fn>
43 void foreach(Fn&& fn) {
44 for (auto it = fMap.begin(); it != fMap.end(); ++it) {
45 fn(it->first.fKey, it->second.fData, it->second.fHitCount);
46 }
47 }
48
Brian Salomon00a5eb82018-07-11 15:32:05 -040049private:
50 struct Key {
51 Key() = default;
52 Key(const SkData& key) : fKey(SkData::MakeWithCopy(key.data(), key.size())) {}
53 Key(const Key& that) = default;
54 Key& operator=(const Key&) = default;
55 bool operator==(const Key& that) const {
56 return that.fKey->size() == fKey->size() &&
57 !memcmp(fKey->data(), that.fKey->data(), that.fKey->size());
58 }
59 sk_sp<const SkData> fKey;
60 };
61
Brian Osman5aa11fb2019-04-08 16:40:36 -040062 struct Value {
63 Value() = default;
64 Value(const SkData& data)
65 : fData(SkData::MakeWithCopy(data.data(), data.size()))
66 , fHitCount(1) {}
67 Value(const Value& that) = default;
68 Value& operator=(const Value&) = default;
69
70 sk_sp<SkData> fData;
71 int fHitCount;
72 };
73
Brian Salomon00a5eb82018-07-11 15:32:05 -040074 struct Hash {
75 using argument_type = Key;
76 using result_type = uint32_t;
77 uint32_t operator()(const Key& key) const {
78 return key.fKey ? SkOpts::hash_fn(key.fKey->data(), key.fKey->size(), 0) : 0;
79 }
80 };
81
82 int fCacheMissCnt = 0;
Brian Osman5aa11fb2019-04-08 16:40:36 -040083 std::unordered_map<Key, Value, Hash> fMap;
Brian Salomon00a5eb82018-07-11 15:32:05 -040084};
85
86} // namespace sk_gpu_test
87
88#endif