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              size_t *programSizeOut);
40 
41     // For querying the contents of the cache.
42     bool getAt(size_t index,
43                const egl::BlobCache::Key **hashOut,
44                egl::BlobCache::Value *programOut);
45 
46     // Evict a program from the binary cache.
47     void remove(const egl::BlobCache::Key &programHash);
48 
49     // Helper method that serializes a program.
50     angle::Result putProgram(const egl::BlobCache::Key &programHash,
51                              const Context *context,
52                              const Program *program);
53 
54     // Same as putProgram but computes the hash.
55     angle::Result updateProgram(const Context *context, const Program *program);
56 
57     // Store a binary directly.  TODO(syoussefi): deprecated.  Will be removed once Chrome supports
58     // EGL_ANDROID_blob_cache. http://anglebug.com/2516
59     ANGLE_NO_DISCARD bool putBinary(const egl::BlobCache::Key &programHash,
60                                     const uint8_t *binary,
61                                     size_t length);
62 
63     // Check the cache, and deserialize and load the program if found. Evict existing hash if load
64     // fails.
65     angle::Result getProgram(const Context *context,
66                              Program *program,
67                              egl::BlobCache::Key *hashOut);
68 
69     // Empty the cache.
70     void clear();
71 
72     // Resize the cache. Discards current contents.
73     void resize(size_t maxCacheSizeBytes);
74 
75     // Returns the number of entries in the cache.
76     size_t entryCount() const;
77 
78     // Reduces the current cache size and returns the number of bytes freed.
79     size_t trim(size_t limit);
80 
81     // Returns the current cache size in bytes.
82     size_t size() const;
83 
84     // Returns the maximum cache size in bytes.
85     size_t maxSize() const;
86 
87   private:
88     egl::BlobCache &mBlobCache;
89     unsigned int mIssuedWarnings;
90 };
91 
92 }  // namespace gl
93 
94 #endif  // LIBANGLE_MEMORY_PROGRAM_CACHE_H_
95