blob: f5506d60f811b103018d4f797ff9e40a69ec86cc [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 Lubick1175dc02022-02-28 12:41:27 -050027class SkData;
28
Stan Ilievd495f432017-10-09 15:49:32 -040029namespace android {
30
31class BlobCache;
32class FileBlobCache;
33
34namespace uirenderer {
35namespace skiapipeline {
36
37class ShaderCache : public GrContextOptions::PersistentCache {
38public:
39 /**
40 * "get" returns a pointer to the singleton ShaderCache object. This
41 * singleton object will never be destroyed.
42 */
Derek Sollenberger3fedf5a2020-02-21 13:07:28 -050043 static ShaderCache& get();
Stan Ilievd495f432017-10-09 15:49:32 -040044
45 /**
Yichi Chen9f959552018-03-29 21:21:54 +080046 * initShaderDiskCache" loads the serialized cache contents from disk,
47 * optionally checks that the on-disk cache matches a provided identity,
48 * and puts the ShaderCache into an initialized state, such that it is
49 * able to insert and retrieve entries from the cache. If identity is
50 * non-null and validation fails, the cache is initialized but contains
Kevin Lubick1175dc02022-02-28 12:41:27 -050051 * no data. If size is less than zero, the cache is initialized but
Yichi Chen9f959552018-03-29 21:21:54 +080052 * contains no data.
53 *
54 * This should be called when HWUI pipeline is initialized. When not in
55 * the initialized state the load and store methods will return without
56 * performing any cache operations.
Stan Ilievd495f432017-10-09 15:49:32 -040057 */
John Reck283bb462018-12-13 16:40:14 -080058 virtual void initShaderDiskCache(const void* identity, ssize_t size);
Yichi Chen9f959552018-03-29 21:21:54 +080059
60 virtual void initShaderDiskCache() { initShaderDiskCache(nullptr, 0); }
Stan Ilievd495f432017-10-09 15:49:32 -040061
62 /**
63 * "setFilename" sets the name of the file that should be used to store
64 * cache contents from one program invocation to another. This function does not perform any
65 * disk operation and it should be invoked before "initShaderCache".
66 */
67 virtual void setFilename(const char* filename);
68
69 /**
70 * "load" attempts to retrieve the value blob associated with a given key
71 * blob from cache. This will be called by Skia, when it needs to compile a new SKSL shader.
72 */
73 sk_sp<SkData> load(const SkData& key) override;
74
75 /**
76 * "store" attempts to insert a new key/value blob pair into the cache.
77 * This will be called by Skia after it compiled a new SKSL shader
78 */
Leon Scroggins III8cedb662022-05-02 10:38:38 -040079 void store(const SkData& key, const SkData& data, const SkString& description) override;
Stan Ilievd495f432017-10-09 15:49:32 -040080
Stan Iliev14211aa2019-01-14 12:29:30 -050081 /**
82 * "onVkFrameFlushed" tries to store Vulkan pipeline cache state.
83 * Pipeline cache is saved on disk only if the size of the data has changed or there was
84 * a new shader compiled.
85 */
Adlai Hollerd2345212020-10-07 14:16:40 -040086 void onVkFrameFlushed(GrDirectContext* context);
Stan Iliev14211aa2019-01-14 12:29:30 -050087
Stan Ilievd495f432017-10-09 15:49:32 -040088private:
89 // Creation and (the lack of) destruction is handled internally.
90 ShaderCache();
91
92 // Copying is disallowed.
93 ShaderCache(const ShaderCache&) = delete;
94 void operator=(const ShaderCache&) = delete;
95
96 /**
97 * "getBlobCacheLocked" returns the BlobCache object being used to store the
98 * key/value blob pairs. If the BlobCache object has not yet been created,
99 * this will do so, loading the serialized cache contents from disk if
100 * possible.
101 */
102 BlobCache* getBlobCacheLocked();
103
104 /**
Yichi Chen9f959552018-03-29 21:21:54 +0800105 * "validateCache" updates the cache to match the given identity. If the
106 * cache currently has the wrong identity, all entries in the cache are cleared.
107 */
108 bool validateCache(const void* identity, ssize_t size);
109
110 /**
111 * "saveToDiskLocked" attemps to save the current contents of the cache to
112 * disk. If the identity hash exists, we will insert the identity hash into
113 * the cache for next validation.
114 */
115 void saveToDiskLocked();
116
117 /**
Stan Ilievd495f432017-10-09 15:49:32 -0400118 * "mInitialized" indicates whether the ShaderCache is in the initialized
119 * state. It is initialized to false at construction time, and gets set to
120 * true when initialize is called.
121 * When in this state, the cache behaves as normal. When not,
122 * the load and store methods will return without performing any cache
123 * operations.
124 */
125 bool mInitialized = false;
126
127 /**
128 * "mBlobCache" is the cache in which the key/value blob pairs are stored. It
129 * is initially NULL, and will be initialized by getBlobCacheLocked the
130 * first time it's needed.
131 * The blob cache contains the Android build number. We treat version mismatches as an empty
132 * cache (logic implemented in BlobCache::unflatten).
133 */
134 std::unique_ptr<FileBlobCache> mBlobCache;
135
136 /**
137 * "mFilename" is the name of the file for storing cache contents in between
138 * program invocations. It is initialized to an empty string at
139 * construction time, and can be set with the setCacheFilename method. An
140 * empty string indicates that the cache should not be saved to or restored
141 * from disk.
142 */
143 std::string mFilename;
144
145 /**
Yichi Chen9f959552018-03-29 21:21:54 +0800146 * "mIDHash" is the current identity hash for the cache validation. It is
147 * initialized to an empty vector at construction time, and its content is
148 * generated in the call of the validateCache method. An empty vector
149 * indicates that cache validation is not performed, and the hash should
150 * not be stored on disk.
151 */
152 std::vector<uint8_t> mIDHash;
153
154 /**
Stan Ilievd495f432017-10-09 15:49:32 -0400155 * "mSavePending" indicates whether or not a deferred save operation is
156 * pending. Each time a key/value pair is inserted into the cache via
157 * load, a deferred save is initiated if one is not already pending.
158 * This will wait some amount of time and then trigger a save of the cache
Nolan Scobie193cd962023-02-08 20:03:31 -0500159 * contents to disk, unless mDeferredSaveDelayMs is 0 in which case saving
160 * is disabled.
Stan Ilievd495f432017-10-09 15:49:32 -0400161 */
162 bool mSavePending = false;
163
164 /**
165 * "mObservedBlobValueSize" is the maximum value size observed by the cache reading function.
166 */
John Reck283bb462018-12-13 16:40:14 -0800167 size_t mObservedBlobValueSize = 20 * 1024;
Stan Ilievd495f432017-10-09 15:49:32 -0400168
169 /**
Nolan Scobie193cd962023-02-08 20:03:31 -0500170 * The time in milliseconds to wait before saving newly inserted cache entries.
171 *
172 * WARNING: setting this to 0 will disable writing the cache to disk.
Stan Ilievd495f432017-10-09 15:49:32 -0400173 */
Nolan Scobie193cd962023-02-08 20:03:31 -0500174 unsigned int mDeferredSaveDelayMs = 4 * 1000;
Stan Ilievd495f432017-10-09 15:49:32 -0400175
176 /**
177 * "mMutex" is the mutex used to prevent concurrent access to the member
178 * variables. It must be locked whenever the member variables are accessed.
179 */
180 mutable std::mutex mMutex;
181
182 /**
Stan Iliev14211aa2019-01-14 12:29:30 -0500183 * If set to "true", the next call to onVkFrameFlushed, will invoke
184 * GrCanvas::storeVkPipelineCacheData. This does not guarantee that data will be stored on disk.
185 */
186 bool mTryToStorePipelineCache = true;
187
188 /**
189 * This flag is used by "ShaderCache::store" to distinguish between shader data and
190 * Vulkan pipeline data.
191 */
192 bool mInStoreVkPipelineInProgress = false;
193
194 /**
195 * "mNewPipelineCacheSize" has the size of the new Vulkan pipeline cache data. It is used
196 * to prevent unnecessary disk writes, if the pipeline cache size has not changed.
197 */
198 size_t mNewPipelineCacheSize = -1;
199 /**
200 * "mOldPipelineCacheSize" has the size of the Vulkan pipeline cache data stored on disk.
201 */
202 size_t mOldPipelineCacheSize = -1;
203
204 /**
205 * "mCacheDirty" is true when there is new shader cache data, which is not saved to disk.
206 */
207 bool mCacheDirty = false;
208
209 /**
Stan Ilievd495f432017-10-09 15:49:32 -0400210 * "sCache" is the singleton ShaderCache object.
211 */
212 static ShaderCache sCache;
213
Yichi Chen9f959552018-03-29 21:21:54 +0800214 /**
215 * "sIDKey" is the cache key of the identity hash
216 */
217 static constexpr uint8_t sIDKey = 0;
218
Leon Scroggins III8cedb662022-05-02 10:38:38 -0400219 /**
220 * Most of this class concerns persistent storage for shaders, but it's also
221 * interesting to keep track of how many shaders are stored in RAM. This
222 * class provides a convenient entry point for that.
223 */
224 int mNumShadersCachedInRam = 0;
225
John Reck283bb462018-12-13 16:40:14 -0800226 friend class ShaderCacheTestUtils; // used for unit testing
Stan Ilievd495f432017-10-09 15:49:32 -0400227};
228
229} /* namespace skiapipeline */
230} /* namespace uirenderer */
231} /* namespace android */