• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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                              const Program *program);
47 
48     // Same as putProgram but computes the hash.
49     angle::Result updateProgram(const Context *context, const 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/2516
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 
63     // Empty the cache.
64     void clear();
65 
66     // Resize the cache. Discards current contents.
67     void resize(size_t maxCacheSizeBytes);
68 
69     // Returns the number of entries in the cache.
70     size_t entryCount() const;
71 
72     // Reduces the current cache size and returns the number of bytes freed.
73     size_t trim(size_t limit);
74 
75     // Returns the current cache size in bytes.
76     size_t size() const;
77 
78     // Returns the maximum cache size in bytes.
79     size_t maxSize() const;
80 
81   private:
82     egl::BlobCache &mBlobCache;
83 };
84 
85 }  // namespace gl
86 
87 #endif  // LIBANGLE_MEMORY_PROGRAM_CACHE_H_
88