blob: 0492d70652df6576b3b6be808f62d459bc516366 [file] [log] [blame]
Stan Ilievd495f432017-10-09 15:49:32 -04001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
John Reck283bb462018-12-13 16:40:14 -080019#include <GrContextOptions.h>
Kevin Lubick1175dc02022-02-28 12:41:27 -050020#include <SkRefCnt.h>
Stan Ilievd495f432017-10-09 15:49:32 -040021#include <cutils/compiler.h>
22#include <memory>
23#include <mutex>
24#include <string>
25#include <vector>
Stan Ilievd495f432017-10-09 15:49:32 -040026
Kevin Lubickdc73cfc2023-03-14 23:27:19 +000027class GrDirectContext;
Kevin Lubick1175dc02022-02-28 12:41:27 -050028class SkData;
29
Stan Ilievd495f432017-10-09 15:49:32 -040030namespace android {
31
32class BlobCache;
33class FileBlobCache;
34
35namespace uirenderer {
36namespace skiapipeline {
37
38class ShaderCache : public GrContextOptions::PersistentCache {
39public:
40 /**
41 * "get" returns a pointer to the singleton ShaderCache object. This
42 * singleton object will never be destroyed.
43 */
Derek Sollenberger3fedf5a2020-02-21 13:07:28 -050044 static ShaderCache& get();
Stan Ilievd495f432017-10-09 15:49:32 -040045
46 /**
Yichi Chen9f959552018-03-29 21:21:54 +080047 * initShaderDiskCache" loads the serialized cache contents from disk,
48 * optionally checks that the on-disk cache matches a provided identity,
49 * and puts the ShaderCache into an initialized state, such that it is
50 * able to insert and retrieve entries from the cache. If identity is
51 * non-null and validation fails, the cache is initialized but contains
Kevin Lubick1175dc02022-02-28 12:41:27 -050052 * no data. If size is less than zero, the cache is initialized but
Yichi Chen9f959552018-03-29 21:21:54 +080053 * contains no data.
54 *
55 * This should be called when HWUI pipeline is initialized. When not in
56 * the initialized state the load and store methods will return without
57 * performing any cache operations.
Stan Ilievd495f432017-10-09 15:49:32 -040058 */
John Reck283bb462018-12-13 16:40:14 -080059 virtual void initShaderDiskCache(const void* identity, ssize_t size);
Yichi Chen9f959552018-03-29 21:21:54 +080060
61 virtual void initShaderDiskCache() { initShaderDiskCache(nullptr, 0); }
Stan Ilievd495f432017-10-09 15:49:32 -040062
63 /**
64 * "setFilename" sets the name of the file that should be used to store
65 * cache contents from one program invocation to another. This function does not perform any
66 * disk operation and it should be invoked before "initShaderCache".
67 */
68 virtual void setFilename(const char* filename);
69
70 /**
71 * "load" attempts to retrieve the value blob associated with a given key
72 * blob from cache. This will be called by Skia, when it needs to compile a new SKSL shader.
73 */
74 sk_sp<SkData> load(const SkData& key) override;
75
76 /**
77 * "store" attempts to insert a new key/value blob pair into the cache.
78 * This will be called by Skia after it compiled a new SKSL shader
79 */
Leon Scroggins III8cedb662022-05-02 10:38:38 -040080 void store(const SkData& key, const SkData& data, const SkString& description) override;
Stan Ilievd495f432017-10-09 15:49:32 -040081
Stan Iliev14211aa2019-01-14 12:29:30 -050082 /**
83 * "onVkFrameFlushed" tries to store Vulkan pipeline cache state.
84 * Pipeline cache is saved on disk only if the size of the data has changed or there was
85 * a new shader compiled.
86 */
Adlai Hollerd2345212020-10-07 14:16:40 -040087 void onVkFrameFlushed(GrDirectContext* context);
Stan Iliev14211aa2019-01-14 12:29:30 -050088
Stan Ilievd495f432017-10-09 15:49:32 -040089private:
90 // Creation and (the lack of) destruction is handled internally.
91 ShaderCache();
92
93 // Copying is disallowed.
94 ShaderCache(const ShaderCache&) = delete;
95 void operator=(const ShaderCache&) = delete;
96
97 /**
98 * "getBlobCacheLocked" returns the BlobCache object being used to store the
99 * key/value blob pairs. If the BlobCache object has not yet been created,
100 * this will do so, loading the serialized cache contents from disk if
101 * possible.
102 */
103 BlobCache* getBlobCacheLocked();
104
105 /**
Yichi Chen9f959552018-03-29 21:21:54 +0800106 * "validateCache" updates the cache to match the given identity. If the
107 * cache currently has the wrong identity, all entries in the cache are cleared.
108 */
109 bool validateCache(const void* identity, ssize_t size);
110
111 /**
112 * "saveToDiskLocked" attemps to save the current contents of the cache to
113 * disk. If the identity hash exists, we will insert the identity hash into
114 * the cache for next validation.
115 */
116 void saveToDiskLocked();
117
118 /**
Stan Ilievd495f432017-10-09 15:49:32 -0400119 * "mInitialized" indicates whether the ShaderCache is in the initialized
120 * state. It is initialized to false at construction time, and gets set to
121 * true when initialize is called.
122 * When in this state, the cache behaves as normal. When not,
123 * the load and store methods will return without performing any cache
124 * operations.
125 */
126 bool mInitialized = false;
127
128 /**
129 * "mBlobCache" is the cache in which the key/value blob pairs are stored. It
130 * is initially NULL, and will be initialized by getBlobCacheLocked the
131 * first time it's needed.
132 * The blob cache contains the Android build number. We treat version mismatches as an empty
133 * cache (logic implemented in BlobCache::unflatten).
134 */
135 std::unique_ptr<FileBlobCache> mBlobCache;
136
137 /**
138 * "mFilename" is the name of the file for storing cache contents in between
139 * program invocations. It is initialized to an empty string at
140 * construction time, and can be set with the setCacheFilename method. An
141 * empty string indicates that the cache should not be saved to or restored
142 * from disk.
143 */
144 std::string mFilename;
145
146 /**
Yichi Chen9f959552018-03-29 21:21:54 +0800147 * "mIDHash" is the current identity hash for the cache validation. It is
148 * initialized to an empty vector at construction time, and its content is
149 * generated in the call of the validateCache method. An empty vector
150 * indicates that cache validation is not performed, and the hash should
151 * not be stored on disk.
152 */
153 std::vector<uint8_t> mIDHash;
154
155 /**
Stan Ilievd495f432017-10-09 15:49:32 -0400156 * "mSavePending" indicates whether or not a deferred save operation is
157 * pending. Each time a key/value pair is inserted into the cache via
158 * load, a deferred save is initiated if one is not already pending.
159 * This will wait some amount of time and then trigger a save of the cache
Nolan Scobie193cd962023-02-08 20:03:31 -0500160 * contents to disk, unless mDeferredSaveDelayMs is 0 in which case saving
161 * is disabled.
Stan Ilievd495f432017-10-09 15:49:32 -0400162 */
163 bool mSavePending = false;
164
165 /**
166 * "mObservedBlobValueSize" is the maximum value size observed by the cache reading function.
167 */
John Reck283bb462018-12-13 16:40:14 -0800168 size_t mObservedBlobValueSize = 20 * 1024;
Stan Ilievd495f432017-10-09 15:49:32 -0400169
170 /**
Nolan Scobie193cd962023-02-08 20:03:31 -0500171 * The time in milliseconds to wait before saving newly inserted cache entries.
172 *
173 * WARNING: setting this to 0 will disable writing the cache to disk.
Stan Ilievd495f432017-10-09 15:49:32 -0400174 */
Nolan Scobie193cd962023-02-08 20:03:31 -0500175 unsigned int mDeferredSaveDelayMs = 4 * 1000;
Stan Ilievd495f432017-10-09 15:49:32 -0400176
177 /**
178 * "mMutex" is the mutex used to prevent concurrent access to the member
179 * variables. It must be locked whenever the member variables are accessed.
180 */
181 mutable std::mutex mMutex;
182
183 /**
Stan Iliev14211aa2019-01-14 12:29:30 -0500184 * If set to "true", the next call to onVkFrameFlushed, will invoke
185 * GrCanvas::storeVkPipelineCacheData. This does not guarantee that data will be stored on disk.
186 */
187 bool mTryToStorePipelineCache = true;
188
189 /**
190 * This flag is used by "ShaderCache::store" to distinguish between shader data and
191 * Vulkan pipeline data.
192 */
193 bool mInStoreVkPipelineInProgress = false;
194
195 /**
196 * "mNewPipelineCacheSize" has the size of the new Vulkan pipeline cache data. It is used
197 * to prevent unnecessary disk writes, if the pipeline cache size has not changed.
198 */
199 size_t mNewPipelineCacheSize = -1;
200 /**
201 * "mOldPipelineCacheSize" has the size of the Vulkan pipeline cache data stored on disk.
202 */
203 size_t mOldPipelineCacheSize = -1;
204
205 /**
206 * "mCacheDirty" is true when there is new shader cache data, which is not saved to disk.
207 */
208 bool mCacheDirty = false;
209
210 /**
Stan Ilievd495f432017-10-09 15:49:32 -0400211 * "sCache" is the singleton ShaderCache object.
212 */
213 static ShaderCache sCache;
214
Yichi Chen9f959552018-03-29 21:21:54 +0800215 /**
216 * "sIDKey" is the cache key of the identity hash
217 */
218 static constexpr uint8_t sIDKey = 0;
219
Leon Scroggins III8cedb662022-05-02 10:38:38 -0400220 /**
221 * Most of this class concerns persistent storage for shaders, but it's also
222 * interesting to keep track of how many shaders are stored in RAM. This
223 * class provides a convenient entry point for that.
224 */
225 int mNumShadersCachedInRam = 0;
226
John Reck283bb462018-12-13 16:40:14 -0800227 friend class ShaderCacheTestUtils; // used for unit testing
Stan Ilievd495f432017-10-09 15:49:32 -0400228};
229
230} /* namespace skiapipeline */
231} /* namespace uirenderer */
232} /* namespace android */