1 // 2 // Copyright 2017 The ANGLE Project Authors. All rights reserved. 3 // Use of this source code is governed by a BSD-style license that can be 4 // found in the LICENSE file. 5 // 6 // MemoryProgramCache: Stores compiled and linked programs in memory so they don't 7 // always have to be re-compiled. Can be used in conjunction with the platform 8 // layer to warm up the cache from disk. 9 10 #ifndef LIBANGLE_MEMORY_PROGRAM_CACHE_H_ 11 #define LIBANGLE_MEMORY_PROGRAM_CACHE_H_ 12 13 #include <array> 14 15 #include "common/MemoryBuffer.h" 16 #include "libANGLE/BlobCache.h" 17 #include "libANGLE/Error.h" 18 19 namespace gl 20 { 21 class Context; 22 class Program; 23 class ProgramState; 24 25 class MemoryProgramCache final : angle::NonCopyable 26 { 27 public: 28 explicit MemoryProgramCache(egl::BlobCache &blobCache); 29 ~MemoryProgramCache(); 30 31 static void ComputeHash(const Context *context, 32 const Program *program, 33 egl::BlobCache::Key *hashOut); 34 35 // Check if the cache contains a binary matching the specified program. 36 bool get(const Context *context, 37 const egl::BlobCache::Key &programHash, 38 egl::BlobCache::Value *programOut); 39 40 // For querying the contents of the cache. 41 bool getAt(size_t index, 42 const egl::BlobCache::Key **hashOut, 43 egl::BlobCache::Value *programOut); 44 45 // Evict a program from the binary cache. 46 void remove(const egl::BlobCache::Key &programHash); 47 48 // Helper method that serializes a program. 49 void putProgram(const egl::BlobCache::Key &programHash, 50 const Context *context, 51 const Program *program); 52 53 // Same as putProgram but computes the hash. 54 void updateProgram(const Context *context, const Program *program); 55 56 // Store a binary directly. TODO(syoussefi): deprecated. Will be removed once Chrome supports 57 // EGL_ANDROID_blob_cache. http://anglebug.com/2516 58 void putBinary(const egl::BlobCache::Key &programHash, const uint8_t *binary, size_t length); 59 60 // Check the cache, and deserialize and load the program if found. Evict existing hash if load 61 // fails. 62 angle::Result getProgram(const Context *context, 63 Program *program, 64 egl::BlobCache::Key *hashOut); 65 66 // Empty the cache. 67 void clear(); 68 69 // Resize the cache. Discards current contents. 70 void resize(size_t maxCacheSizeBytes); 71 72 // Returns the number of entries in the cache. 73 size_t entryCount() const; 74 75 // Reduces the current cache size and returns the number of bytes freed. 76 size_t trim(size_t limit); 77 78 // Returns the current cache size in bytes. 79 size_t size() const; 80 81 // Returns the maximum cache size in bytes. 82 size_t maxSize() const; 83 84 private: 85 egl::BlobCache &mBlobCache; 86 unsigned int mIssuedWarnings; 87 }; 88 89 } // namespace gl 90 91 #endif // LIBANGLE_MEMORY_PROGRAM_CACHE_H_ 92