• 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     // 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     angle::Result putProgram(const egl::BlobCache::Key &programHash,
50                              const Context *context,
51                              const Program *program);
52 
53     // Same as putProgram but computes the hash.
54     angle::Result 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     ANGLE_NO_DISCARD bool putBinary(const egl::BlobCache::Key &programHash,
59                                     const uint8_t *binary,
60                                     size_t length);
61 
62     // Check the cache, and deserialize and load the program if found. Evict existing hash if load
63     // fails.
64     angle::Result getProgram(const Context *context,
65                              Program *program,
66                              egl::BlobCache::Key *hashOut);
67 
68     // Empty the cache.
69     void clear();
70 
71     // Resize the cache. Discards current contents.
72     void resize(size_t maxCacheSizeBytes);
73 
74     // Returns the number of entries in the cache.
75     size_t entryCount() const;
76 
77     // Reduces the current cache size and returns the number of bytes freed.
78     size_t trim(size_t limit);
79 
80     // Returns the current cache size in bytes.
81     size_t size() const;
82 
83     // Returns the maximum cache size in bytes.
84     size_t maxSize() const;
85 
86   private:
87     egl::BlobCache &mBlobCache;
88     unsigned int mIssuedWarnings;
89 };
90 
91 }  // namespace gl
92 
93 #endif  // LIBANGLE_MEMORY_PROGRAM_CACHE_H_
94