Brian Salomon | 00a5eb8 | 2018-07-11 15:32:05 -0400 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkData.h" |
| 12 | #include "include/gpu/GrContextOptions.h" |
| 13 | #include "include/private/SkChecksum.h" |
Brian Salomon | 00a5eb8 | 2018-07-11 15:32:05 -0400 | [diff] [blame] | 14 | |
Hal Canary | 8a00144 | 2018-09-19 11:31:27 -0400 | [diff] [blame] | 15 | #include <unordered_map> |
| 16 | |
Brian Salomon | 00a5eb8 | 2018-07-11 15:32:05 -0400 | [diff] [blame] | 17 | namespace 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 | */ |
| 25 | class MemoryCache : public GrContextOptions::PersistentCache { |
| 26 | public: |
| 27 | MemoryCache() = default; |
| 28 | MemoryCache(const MemoryCache&) = delete; |
| 29 | MemoryCache& operator=(const MemoryCache&) = delete; |
Brian Osman | 0b8bb88 | 2019-04-12 11:47:19 -0400 | [diff] [blame] | 30 | void reset() { |
Brian Osman | 43f443f | 2020-06-05 11:11:36 -0400 | [diff] [blame] | 31 | this->resetCacheStats(); |
Brian Osman | 0b8bb88 | 2019-04-12 11:47:19 -0400 | [diff] [blame] | 32 | fMap.clear(); |
| 33 | } |
Brian Salomon | 00a5eb8 | 2018-07-11 15:32:05 -0400 | [diff] [blame] | 34 | |
| 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; } |
Brian Osman | 43f443f | 2020-06-05 11:11:36 -0400 | [diff] [blame] | 38 | int numCacheStores() const { return fCacheStoreCnt; } |
| 39 | void resetCacheStats() { |
| 40 | fCacheMissCnt = 0; |
| 41 | fCacheStoreCnt = 0; |
| 42 | } |
Brian Salomon | 00a5eb8 | 2018-07-11 15:32:05 -0400 | [diff] [blame] | 43 | |
Brian Osman | 5aa11fb | 2019-04-08 16:40:36 -0400 | [diff] [blame] | 44 | void writeShadersToDisk(const char* path, GrBackendApi backend); |
| 45 | |
Brian Osman | 0b8bb88 | 2019-04-12 11:47:19 -0400 | [diff] [blame] | 46 | template <typename Fn> |
| 47 | void foreach(Fn&& fn) { |
| 48 | for (auto it = fMap.begin(); it != fMap.end(); ++it) { |
| 49 | fn(it->first.fKey, it->second.fData, it->second.fHitCount); |
| 50 | } |
| 51 | } |
| 52 | |
Brian Salomon | 00a5eb8 | 2018-07-11 15:32:05 -0400 | [diff] [blame] | 53 | private: |
| 54 | struct Key { |
| 55 | Key() = default; |
| 56 | Key(const SkData& key) : fKey(SkData::MakeWithCopy(key.data(), key.size())) {} |
| 57 | Key(const Key& that) = default; |
| 58 | Key& operator=(const Key&) = default; |
| 59 | bool operator==(const Key& that) const { |
| 60 | return that.fKey->size() == fKey->size() && |
| 61 | !memcmp(fKey->data(), that.fKey->data(), that.fKey->size()); |
| 62 | } |
| 63 | sk_sp<const SkData> fKey; |
| 64 | }; |
| 65 | |
Brian Osman | 5aa11fb | 2019-04-08 16:40:36 -0400 | [diff] [blame] | 66 | struct Value { |
| 67 | Value() = default; |
| 68 | Value(const SkData& data) |
| 69 | : fData(SkData::MakeWithCopy(data.data(), data.size())) |
| 70 | , fHitCount(1) {} |
| 71 | Value(const Value& that) = default; |
| 72 | Value& operator=(const Value&) = default; |
| 73 | |
| 74 | sk_sp<SkData> fData; |
| 75 | int fHitCount; |
| 76 | }; |
| 77 | |
Brian Salomon | 00a5eb8 | 2018-07-11 15:32:05 -0400 | [diff] [blame] | 78 | struct Hash { |
| 79 | using argument_type = Key; |
| 80 | using result_type = uint32_t; |
| 81 | uint32_t operator()(const Key& key) const { |
| 82 | return key.fKey ? SkOpts::hash_fn(key.fKey->data(), key.fKey->size(), 0) : 0; |
| 83 | } |
| 84 | }; |
| 85 | |
| 86 | int fCacheMissCnt = 0; |
Brian Osman | 43f443f | 2020-06-05 11:11:36 -0400 | [diff] [blame] | 87 | int fCacheStoreCnt = 0; |
Brian Osman | 5aa11fb | 2019-04-08 16:40:36 -0400 | [diff] [blame] | 88 | std::unordered_map<Key, Value, Hash> fMap; |
Brian Salomon | 00a5eb8 | 2018-07-11 15:32:05 -0400 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | } // namespace sk_gpu_test |
| 92 | |
| 93 | #endif |