blob: 40dfc9d4309be87f0fdb79669d14f969a0365a9d [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
Aditya Kumar9feca012024-03-11 22:23:26 +000019#include <FileBlobCache.h>
John Reck283bb462018-12-13 16:40:14 -080020#include <GrContextOptions.h>
Kevin Lubick1175dc02022-02-28 12:41:27 -050021#include <SkRefCnt.h>
Stan Ilievd495f432017-10-09 15:49:32 -040022#include <cutils/compiler.h>
Matt Buckleye278da12023-06-20 22:51:05 +000023#include <ftl/shared_mutex.h>
24#include <utils/Mutex.h>
25
Stan Ilievd495f432017-10-09 15:49:32 -040026#include <memory>
Stan Ilievd495f432017-10-09 15:49:32 -040027#include <string>
28#include <vector>
Stan Ilievd495f432017-10-09 15:49:32 -040029
Kevin Lubickdc73cfc2023-03-14 23:27:19 +000030class GrDirectContext;
Kevin Lubick1175dc02022-02-28 12:41:27 -050031class SkData;
32
Stan Ilievd495f432017-10-09 15:49:32 -040033namespace android {
34
35class BlobCache;
Stan Ilievd495f432017-10-09 15:49:32 -040036
37namespace uirenderer {
38namespace skiapipeline {
39
40class ShaderCache : public GrContextOptions::PersistentCache {
41public:
42 /**
43 * "get" returns a pointer to the singleton ShaderCache object. This
44 * singleton object will never be destroyed.
45 */
Derek Sollenberger3fedf5a2020-02-21 13:07:28 -050046 static ShaderCache& get();
Stan Ilievd495f432017-10-09 15:49:32 -040047
48 /**
Yichi Chen9f959552018-03-29 21:21:54 +080049 * initShaderDiskCache" loads the serialized cache contents from disk,
50 * optionally checks that the on-disk cache matches a provided identity,
51 * and puts the ShaderCache into an initialized state, such that it is
52 * able to insert and retrieve entries from the cache. If identity is
53 * non-null and validation fails, the cache is initialized but contains
Kevin Lubick1175dc02022-02-28 12:41:27 -050054 * no data. If size is less than zero, the cache is initialized but
Yichi Chen9f959552018-03-29 21:21:54 +080055 * contains no data.
56 *
57 * This should be called when HWUI pipeline is initialized. When not in
58 * the initialized state the load and store methods will return without
59 * performing any cache operations.
Stan Ilievd495f432017-10-09 15:49:32 -040060 */
John Reck283bb462018-12-13 16:40:14 -080061 virtual void initShaderDiskCache(const void* identity, ssize_t size);
Yichi Chen9f959552018-03-29 21:21:54 +080062
63 virtual void initShaderDiskCache() { initShaderDiskCache(nullptr, 0); }
Stan Ilievd495f432017-10-09 15:49:32 -040064
65 /**
66 * "setFilename" sets the name of the file that should be used to store
67 * cache contents from one program invocation to another. This function does not perform any
68 * disk operation and it should be invoked before "initShaderCache".
69 */
70 virtual void setFilename(const char* filename);
71
72 /**
73 * "load" attempts to retrieve the value blob associated with a given key
74 * blob from cache. This will be called by Skia, when it needs to compile a new SKSL shader.
75 */
76 sk_sp<SkData> load(const SkData& key) override;
77
78 /**
79 * "store" attempts to insert a new key/value blob pair into the cache.
80 * This will be called by Skia after it compiled a new SKSL shader
81 */
Leon Scroggins III8cedb662022-05-02 10:38:38 -040082 void store(const SkData& key, const SkData& data, const SkString& description) override;
Stan Ilievd495f432017-10-09 15:49:32 -040083
Stan Iliev14211aa2019-01-14 12:29:30 -050084 /**
85 * "onVkFrameFlushed" tries to store Vulkan pipeline cache state.
86 * Pipeline cache is saved on disk only if the size of the data has changed or there was
87 * a new shader compiled.
88 */
Adlai Hollerd2345212020-10-07 14:16:40 -040089 void onVkFrameFlushed(GrDirectContext* context);
Stan Iliev14211aa2019-01-14 12:29:30 -050090
Stan Ilievd495f432017-10-09 15:49:32 -040091private:
92 // Creation and (the lack of) destruction is handled internally.
93 ShaderCache();
94
95 // Copying is disallowed.
96 ShaderCache(const ShaderCache&) = delete;
97 void operator=(const ShaderCache&) = delete;
98
99 /**
Yichi Chen9f959552018-03-29 21:21:54 +0800100 * "validateCache" updates the cache to match the given identity. If the
101 * cache currently has the wrong identity, all entries in the cache are cleared.
102 */
Matt Buckleye278da12023-06-20 22:51:05 +0000103 bool validateCache(const void* identity, ssize_t size) REQUIRES(mMutex);
Yichi Chen9f959552018-03-29 21:21:54 +0800104
105 /**
Matt Buckleye9adfbd2023-07-06 23:15:30 +0000106 * Helper for BlobCache::set to trace the result and ensure the identity hash
107 * does not get evicted.
108 */
109 void set(const void* key, size_t keySize, const void* value, size_t valueSize) REQUIRES(mMutex);
110
111 /**
Matt Buckleye278da12023-06-20 22:51:05 +0000112 * "saveToDiskLocked" attempts to save the current contents of the cache to
Yichi Chen9f959552018-03-29 21:21:54 +0800113 * disk. If the identity hash exists, we will insert the identity hash into
114 * the cache for next validation.
115 */
Matt Buckleye278da12023-06-20 22:51:05 +0000116 void saveToDiskLocked() REQUIRES(mMutex);
Yichi Chen9f959552018-03-29 21:21:54 +0800117
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 */
Matt Buckleye278da12023-06-20 22:51:05 +0000126 bool mInitialized GUARDED_BY(mMutex) = false;
Stan Ilievd495f432017-10-09 15:49:32 -0400127
128 /**
Matt Buckleye9adfbd2023-07-06 23:15:30 +0000129 * "mBlobCache" is the cache in which the key/value blob pairs are stored.
130 * The blob cache contains the Android build number. We treat version mismatches
131 * as an empty cache (logic implemented in BlobCache::unflatten).
Stan Ilievd495f432017-10-09 15:49:32 -0400132 */
Matt Buckleye278da12023-06-20 22:51:05 +0000133 std::unique_ptr<FileBlobCache> mBlobCache GUARDED_BY(mMutex);
Stan Ilievd495f432017-10-09 15:49:32 -0400134
135 /**
136 * "mFilename" is the name of the file for storing cache contents in between
137 * program invocations. It is initialized to an empty string at
138 * construction time, and can be set with the setCacheFilename method. An
139 * empty string indicates that the cache should not be saved to or restored
140 * from disk.
141 */
Matt Buckleye278da12023-06-20 22:51:05 +0000142 std::string mFilename GUARDED_BY(mMutex);
Stan Ilievd495f432017-10-09 15:49:32 -0400143
144 /**
Yichi Chen9f959552018-03-29 21:21:54 +0800145 * "mIDHash" is the current identity hash for the cache validation. It is
146 * initialized to an empty vector at construction time, and its content is
147 * generated in the call of the validateCache method. An empty vector
148 * indicates that cache validation is not performed, and the hash should
149 * not be stored on disk.
150 */
Matt Buckleye278da12023-06-20 22:51:05 +0000151 std::vector<uint8_t> mIDHash GUARDED_BY(mMutex);
Yichi Chen9f959552018-03-29 21:21:54 +0800152
153 /**
Stan Ilievd495f432017-10-09 15:49:32 -0400154 * "mSavePending" indicates whether or not a deferred save operation is
155 * pending. Each time a key/value pair is inserted into the cache via
156 * load, a deferred save is initiated if one is not already pending.
157 * This will wait some amount of time and then trigger a save of the cache
Nolan Scobie193cd962023-02-08 20:03:31 -0500158 * contents to disk, unless mDeferredSaveDelayMs is 0 in which case saving
159 * is disabled.
Stan Ilievd495f432017-10-09 15:49:32 -0400160 */
Matt Buckleye278da12023-06-20 22:51:05 +0000161 bool mSavePending GUARDED_BY(mMutex) = false;
Stan Ilievd495f432017-10-09 15:49:32 -0400162
163 /**
164 * "mObservedBlobValueSize" is the maximum value size observed by the cache reading function.
165 */
John Reck283bb462018-12-13 16:40:14 -0800166 size_t mObservedBlobValueSize = 20 * 1024;
Stan Ilievd495f432017-10-09 15:49:32 -0400167
168 /**
Nolan Scobie193cd962023-02-08 20:03:31 -0500169 * The time in milliseconds to wait before saving newly inserted cache entries.
170 *
171 * WARNING: setting this to 0 will disable writing the cache to disk.
Stan Ilievd495f432017-10-09 15:49:32 -0400172 */
Nolan Scobie193cd962023-02-08 20:03:31 -0500173 unsigned int mDeferredSaveDelayMs = 4 * 1000;
Stan Ilievd495f432017-10-09 15:49:32 -0400174
175 /**
Matt Buckleye278da12023-06-20 22:51:05 +0000176 * "mMutex" is the shared mutex used to prevent concurrent access to the member
Stan Ilievd495f432017-10-09 15:49:32 -0400177 * variables. It must be locked whenever the member variables are accessed.
178 */
Matt Buckleye278da12023-06-20 22:51:05 +0000179 mutable ftl::SharedMutex mMutex;
Stan Ilievd495f432017-10-09 15:49:32 -0400180
181 /**
Stan Iliev14211aa2019-01-14 12:29:30 -0500182 * If set to "true", the next call to onVkFrameFlushed, will invoke
183 * GrCanvas::storeVkPipelineCacheData. This does not guarantee that data will be stored on disk.
184 */
Matt Buckleye278da12023-06-20 22:51:05 +0000185 bool mTryToStorePipelineCache GUARDED_BY(mMutex) = true;
Stan Iliev14211aa2019-01-14 12:29:30 -0500186
187 /**
188 * This flag is used by "ShaderCache::store" to distinguish between shader data and
189 * Vulkan pipeline data.
190 */
191 bool mInStoreVkPipelineInProgress = false;
192
193 /**
194 * "mNewPipelineCacheSize" has the size of the new Vulkan pipeline cache data. It is used
195 * to prevent unnecessary disk writes, if the pipeline cache size has not changed.
196 */
Matt Buckleye278da12023-06-20 22:51:05 +0000197 size_t mNewPipelineCacheSize GUARDED_BY(mMutex) = -1;
Stan Iliev14211aa2019-01-14 12:29:30 -0500198 /**
199 * "mOldPipelineCacheSize" has the size of the Vulkan pipeline cache data stored on disk.
200 */
Matt Buckleye278da12023-06-20 22:51:05 +0000201 size_t mOldPipelineCacheSize GUARDED_BY(mMutex) = -1;
Stan Iliev14211aa2019-01-14 12:29:30 -0500202
203 /**
204 * "mCacheDirty" is true when there is new shader cache data, which is not saved to disk.
205 */
Matt Buckleye278da12023-06-20 22:51:05 +0000206 bool mCacheDirty GUARDED_BY(mMutex) = false;
Stan Iliev14211aa2019-01-14 12:29:30 -0500207
208 /**
Stan Ilievd495f432017-10-09 15:49:32 -0400209 * "sCache" is the singleton ShaderCache object.
210 */
211 static ShaderCache sCache;
212
Yichi Chen9f959552018-03-29 21:21:54 +0800213 /**
214 * "sIDKey" is the cache key of the identity hash
215 */
216 static constexpr uint8_t sIDKey = 0;
217
Leon Scroggins III8cedb662022-05-02 10:38:38 -0400218 /**
219 * Most of this class concerns persistent storage for shaders, but it's also
220 * interesting to keep track of how many shaders are stored in RAM. This
221 * class provides a convenient entry point for that.
222 */
Matt Buckleye278da12023-06-20 22:51:05 +0000223 int mNumShadersCachedInRam GUARDED_BY(mMutex) = 0;
Leon Scroggins III8cedb662022-05-02 10:38:38 -0400224
John Reck283bb462018-12-13 16:40:14 -0800225 friend class ShaderCacheTestUtils; // used for unit testing
Stan Ilievd495f432017-10-09 15:49:32 -0400226};
227
228} /* namespace skiapipeline */
229} /* namespace uirenderer */
230} /* namespace android */