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