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