Hash :
9137adea
Author :
Date :
2018-08-27T14:22:37
Add support for EGL_ANDROID_blob_cache The functionality of MemoryProgramCache is divided up in two. BlobCache is now a generic binary cache, which interfaces with the callbacks from EGL_ANDROID_blob_cache. MemoryProgramCache handles program [de]serialization and interacts with BlobCache. Bug: angleproject:2516 Change-Id: Ie4328a2e56a26338e033d84f4e53a1103411937d Reviewed-on: https://chromium-review.googlesource.com/1194285 Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Geoff Lang <geofflang@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
//
// Copyright 2017 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// SizedMRUCache.h: A hashing map that stores blobs of sized, untyped data.
#ifndef LIBANGLE_SIZED_MRU_CACHE_H_
#define LIBANGLE_SIZED_MRU_CACHE_H_
#include <anglebase/containers/mru_cache.h>
#include "common/third_party/smhasher/src/PMurHash.h"
namespace angle
{
template <typename Key, typename Value>
class SizedMRUCache final : angle::NonCopyable
{
public:
SizedMRUCache(size_t maximumTotalSize)
: mMaximumTotalSize(maximumTotalSize),
mCurrentSize(0),
mStore(SizedMRUCacheStore::NO_AUTO_EVICT)
{
}
// Returns nullptr on failure.
const Value *put(const Key &key, Value &&value, size_t size)
{
if (size > mMaximumTotalSize)
{
return nullptr;
}
// Check for existing key.
eraseByKey(key);
auto retVal = mStore.Put(key, ValueAndSize(std::move(value), size));
mCurrentSize += size;
shrinkToSize(mMaximumTotalSize);
return &retVal->second.value;
}
bool get(const Key &key, const Value **valueOut)
{
const auto &iter = mStore.Get(key);
if (iter == mStore.end())
{
return false;
}
*valueOut = &iter->second.value;
return true;
}
bool getAt(size_t index, const Key **keyOut, const Value **valueOut)
{
if (index < mStore.size())
{
auto it = mStore.begin();
std::advance(it, index);
*keyOut = &it->first;
*valueOut = &it->second.value;
return true;
}
*valueOut = nullptr;
return false;
}
bool empty() const { return mStore.empty(); }
void clear()
{
mStore.Clear();
mCurrentSize = 0;
}
bool eraseByKey(const Key &key)
{
// Check for existing key.
auto existing = mStore.Peek(key);
if (existing != mStore.end())
{
mCurrentSize -= existing->second.size;
mStore.Erase(existing);
return true;
}
return false;
}
size_t entryCount() const { return mStore.size(); }
size_t size() const { return mCurrentSize; }
// Also discards the cache contents.
void resize(size_t maximumTotalSize)
{
clear();
mMaximumTotalSize = maximumTotalSize;
}
// Reduce current memory usage.
size_t shrinkToSize(size_t limit)
{
size_t initialSize = mCurrentSize;
while (mCurrentSize > limit)
{
ASSERT(!mStore.empty());
auto iter = mStore.rbegin();
mCurrentSize -= iter->second.size;
mStore.Erase(iter);
}
return (initialSize - mCurrentSize);
}
size_t maxSize() const { return mMaximumTotalSize; }
private:
struct ValueAndSize
{
ValueAndSize() : value(), size(0) {}
ValueAndSize(Value &&value, size_t size) : value(std::move(value)), size(size) {}
ValueAndSize(ValueAndSize &&other) : ValueAndSize() { *this = std::move(other); }
ValueAndSize &operator=(ValueAndSize &&other)
{
std::swap(value, other.value);
std::swap(size, other.size);
return *this;
}
Value value;
size_t size;
};
using SizedMRUCacheStore = base::HashingMRUCache<Key, ValueAndSize>;
size_t mMaximumTotalSize;
size_t mCurrentSize;
SizedMRUCacheStore mStore;
};
// Helper function used in a few places.
template <typename T>
void TrimCache(size_t maxStates, size_t gcLimit, const char *name, T *cache)
{
const size_t kGarbageCollectionLimit = maxStates / 2 + gcLimit;
if (cache->size() >= kGarbageCollectionLimit)
{
WARN() << "Overflowed the " << name << " cache limit of " << (maxStates / 2)
<< " elements, removing the least recently used to make room.";
cache->ShrinkToSize(maxStates / 2);
}
}
// Computes a hash of "key". Any data passed to this function must be multiples of
// 4 bytes, since the PMurHash32 method can only operate increments of 4-byte words.
static inline std::size_t ComputeGenericHash(const void *key, size_t keySize)
{
static constexpr unsigned int seed = 0xABCDEF98;
// We can't support "odd" alignments. ComputeGenericHash requires aligned types
ASSERT(keySize % 4 == 0);
return PMurHash32(seed, key, static_cast<int>(keySize));
}
template <typename T>
std::size_t ComputeGenericHash(const T &key)
{
static_assert(sizeof(key) % 4 == 0, "ComputeGenericHash requires aligned types");
return ComputeGenericHash(&key, sizeof(key));
}
} // namespace angle
#endif // LIBANGLE_SIZED_MRU_CACHE_H_