blob: 2f91c778b8a000a024d8f03a50fb44c9489a13e1 [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>
Matt Buckleye278da12023-06-20 22:51:05 +000022#include <ftl/shared_mutex.h>
23#include <utils/Mutex.h>
24
Stan Ilievd495f432017-10-09 15:49:32 -040025#include <memory>
Stan Ilievd495f432017-10-09 15:49:32 -040026#include <string>
27#include <vector>
Stan Ilievd495f432017-10-09 15:49:32 -040028
Kevin Lubick1175dc02022-02-28 12:41:27 -050029class SkData;
30
Stan Ilievd495f432017-10-09 15:49:32 -040031namespace android {
32
33class BlobCache;
34class FileBlobCache;
35
36namespace uirenderer {
37namespace skiapipeline {
38
39class ShaderCache : public GrContextOptions::PersistentCache {
40public:
41 /**
42 * "get" returns a pointer to the singleton ShaderCache object. This
43 * singleton object will never be destroyed.
44 */
Derek Sollenberger3fedf5a2020-02-21 13:07:28 -050045 static ShaderCache& get();
Stan Ilievd495f432017-10-09 15:49:32 -040046
47 /**
Yichi Chen9f959552018-03-29 21:21:54 +080048 * initShaderDiskCache" loads the serialized cache contents from disk,
49 * optionally checks that the on-disk cache matches a provided identity,
50 * and puts the ShaderCache into an initialized state, such that it is
51 * able to insert and retrieve entries from the cache. If identity is
52 * non-null and validation fails, the cache is initialized but contains
Kevin Lubick1175dc02022-02-28 12:41:27 -050053 * no data. If size is less than zero, the cache is initialized but
Yichi Chen9f959552018-03-29 21:21:54 +080054 * contains no data.
55 *
56 * This should be called when HWUI pipeline is initialized. When not in
57 * the initialized state the load and store methods will return without
58 * performing any cache operations.
Stan Ilievd495f432017-10-09 15:49:32 -040059 */
John Reck283bb462018-12-13 16:40:14 -080060 virtual void initShaderDiskCache(const void* identity, ssize_t size);
Yichi Chen9f959552018-03-29 21:21:54 +080061
62 virtual void initShaderDiskCache() { initShaderDiskCache(nullptr, 0); }
Stan Ilievd495f432017-10-09 15:49:32 -040063
64 /**
65 * "setFilename" sets the name of the file that should be used to store
66 * cache contents from one program invocation to another. This function does not perform any
67 * disk operation and it should be invoked before "initShaderCache".
68 */
69 virtual void setFilename(const char* filename);
70
71 /**
72 * "load" attempts to retrieve the value blob associated with a given key
73 * blob from cache. This will be called by Skia, when it needs to compile a new SKSL shader.
74 */
75 sk_sp<SkData> load(const SkData& key) override;
76
77 /**
78 * "store" attempts to insert a new key/value blob pair into the cache.
79 * This will be called by Skia after it compiled a new SKSL shader
80 */
Leon Scroggins III8cedb662022-05-02 10:38:38 -040081 void store(const SkData& key, const SkData& data, const SkString& description) override;
Stan Ilievd495f432017-10-09 15:49:32 -040082
Stan Iliev14211aa2019-01-14 12:29:30 -050083 /**
84 * "onVkFrameFlushed" tries to store Vulkan pipeline cache state.
85 * Pipeline cache is saved on disk only if the size of the data has changed or there was
86 * a new shader compiled.
87 */
Adlai Hollerd2345212020-10-07 14:16:40 -040088 void onVkFrameFlushed(GrDirectContext* context);
Stan Iliev14211aa2019-01-14 12:29:30 -050089
Stan Ilievd495f432017-10-09 15:49:32 -040090private:
91 // Creation and (the lack of) destruction is handled internally.
92 ShaderCache();
93
94 // Copying is disallowed.
95 ShaderCache(const ShaderCache&) = delete;
96 void operator=(const ShaderCache&) = delete;
97
98 /**
99 * "getBlobCacheLocked" returns the BlobCache object being used to store the
100 * key/value blob pairs. If the BlobCache object has not yet been created,
101 * this will do so, loading the serialized cache contents from disk if
102 * possible.
103 */
Matt Buckleye278da12023-06-20 22:51:05 +0000104 BlobCache* getBlobCacheLocked() REQUIRES(mMutex);
Stan Ilievd495f432017-10-09 15:49:32 -0400105
106 /**
Yichi Chen9f959552018-03-29 21:21:54 +0800107 * "validateCache" updates the cache to match the given identity. If the
108 * cache currently has the wrong identity, all entries in the cache are cleared.
109 */
Matt Buckleye278da12023-06-20 22:51:05 +0000110 bool validateCache(const void* identity, ssize_t size) REQUIRES(mMutex);
Yichi Chen9f959552018-03-29 21:21:54 +0800111
112 /**
Matt Buckleye278da12023-06-20 22:51:05 +0000113 * "saveToDiskLocked" attempts to save the current contents of the cache to
Yichi Chen9f959552018-03-29 21:21:54 +0800114 * disk. If the identity hash exists, we will insert the identity hash into
115 * the cache for next validation.
116 */
Matt Buckleye278da12023-06-20 22:51:05 +0000117 void saveToDiskLocked() REQUIRES(mMutex);
Yichi Chen9f959552018-03-29 21:21:54 +0800118
119 /**
Stan Ilievd495f432017-10-09 15:49:32 -0400120 * "mInitialized" indicates whether the ShaderCache is in the initialized
121 * state. It is initialized to false at construction time, and gets set to
122 * true when initialize is called.
123 * When in this state, the cache behaves as normal. When not,
124 * the load and store methods will return without performing any cache
125 * operations.
126 */
Matt Buckleye278da12023-06-20 22:51:05 +0000127 bool mInitialized GUARDED_BY(mMutex) = false;
Stan Ilievd495f432017-10-09 15:49:32 -0400128
129 /**
130 * "mBlobCache" is the cache in which the key/value blob pairs are stored. It
131 * is initially NULL, and will be initialized by getBlobCacheLocked the
132 * first time it's needed.
133 * The blob cache contains the Android build number. We treat version mismatches as an empty
134 * cache (logic implemented in BlobCache::unflatten).
135 */
Matt Buckleye278da12023-06-20 22:51:05 +0000136 std::unique_ptr<FileBlobCache> mBlobCache GUARDED_BY(mMutex);
Stan Ilievd495f432017-10-09 15:49:32 -0400137
138 /**
139 * "mFilename" is the name of the file for storing cache contents in between
140 * program invocations. It is initialized to an empty string at
141 * construction time, and can be set with the setCacheFilename method. An
142 * empty string indicates that the cache should not be saved to or restored
143 * from disk.
144 */
Matt Buckleye278da12023-06-20 22:51:05 +0000145 std::string mFilename GUARDED_BY(mMutex);
Stan Ilievd495f432017-10-09 15:49:32 -0400146
147 /**
Yichi Chen9f959552018-03-29 21:21:54 +0800148 * "mIDHash" is the current identity hash for the cache validation. It is
149 * initialized to an empty vector at construction time, and its content is
150 * generated in the call of the validateCache method. An empty vector
151 * indicates that cache validation is not performed, and the hash should
152 * not be stored on disk.
153 */
Matt Buckleye278da12023-06-20 22:51:05 +0000154 std::vector<uint8_t> mIDHash GUARDED_BY(mMutex);
Yichi Chen9f959552018-03-29 21:21:54 +0800155
156 /**
Stan Ilievd495f432017-10-09 15:49:32 -0400157 * "mSavePending" indicates whether or not a deferred save operation is
158 * pending. Each time a key/value pair is inserted into the cache via
159 * load, a deferred save is initiated if one is not already pending.
160 * This will wait some amount of time and then trigger a save of the cache
Nolan Scobie193cd962023-02-08 20:03:31 -0500161 * contents to disk, unless mDeferredSaveDelayMs is 0 in which case saving
162 * is disabled.
Stan Ilievd495f432017-10-09 15:49:32 -0400163 */
Matt Buckleye278da12023-06-20 22:51:05 +0000164 bool mSavePending GUARDED_BY(mMutex) = false;
Stan Ilievd495f432017-10-09 15:49:32 -0400165
166 /**
167 * "mObservedBlobValueSize" is the maximum value size observed by the cache reading function.
168 */
John Reck283bb462018-12-13 16:40:14 -0800169 size_t mObservedBlobValueSize = 20 * 1024;
Stan Ilievd495f432017-10-09 15:49:32 -0400170
171 /**
Nolan Scobie193cd962023-02-08 20:03:31 -0500172 * The time in milliseconds to wait before saving newly inserted cache entries.
173 *
174 * WARNING: setting this to 0 will disable writing the cache to disk.
Stan Ilievd495f432017-10-09 15:49:32 -0400175 */
Nolan Scobie193cd962023-02-08 20:03:31 -0500176 unsigned int mDeferredSaveDelayMs = 4 * 1000;
Stan Ilievd495f432017-10-09 15:49:32 -0400177
178 /**
Matt Buckleye278da12023-06-20 22:51:05 +0000179 * "mMutex" is the shared mutex used to prevent concurrent access to the member
Stan Ilievd495f432017-10-09 15:49:32 -0400180 * variables. It must be locked whenever the member variables are accessed.
181 */
Matt Buckleye278da12023-06-20 22:51:05 +0000182 mutable ftl::SharedMutex mMutex;
Stan Ilievd495f432017-10-09 15:49:32 -0400183
184 /**
Stan Iliev14211aa2019-01-14 12:29:30 -0500185 * If set to "true", the next call to onVkFrameFlushed, will invoke
186 * GrCanvas::storeVkPipelineCacheData. This does not guarantee that data will be stored on disk.
187 */
Matt Buckleye278da12023-06-20 22:51:05 +0000188 bool mTryToStorePipelineCache GUARDED_BY(mMutex) = true;
Stan Iliev14211aa2019-01-14 12:29:30 -0500189
190 /**
191 * This flag is used by "ShaderCache::store" to distinguish between shader data and
192 * Vulkan pipeline data.
193 */
194 bool mInStoreVkPipelineInProgress = false;
195
196 /**
197 * "mNewPipelineCacheSize" has the size of the new Vulkan pipeline cache data. It is used
198 * to prevent unnecessary disk writes, if the pipeline cache size has not changed.
199 */
Matt Buckleye278da12023-06-20 22:51:05 +0000200 size_t mNewPipelineCacheSize GUARDED_BY(mMutex) = -1;
Stan Iliev14211aa2019-01-14 12:29:30 -0500201 /**
202 * "mOldPipelineCacheSize" has the size of the Vulkan pipeline cache data stored on disk.
203 */
Matt Buckleye278da12023-06-20 22:51:05 +0000204 size_t mOldPipelineCacheSize GUARDED_BY(mMutex) = -1;
Stan Iliev14211aa2019-01-14 12:29:30 -0500205
206 /**
207 * "mCacheDirty" is true when there is new shader cache data, which is not saved to disk.
208 */
Matt Buckleye278da12023-06-20 22:51:05 +0000209 bool mCacheDirty GUARDED_BY(mMutex) = false;
Stan Iliev14211aa2019-01-14 12:29:30 -0500210
211 /**
Stan Ilievd495f432017-10-09 15:49:32 -0400212 * "sCache" is the singleton ShaderCache object.
213 */
214 static ShaderCache sCache;
215
Yichi Chen9f959552018-03-29 21:21:54 +0800216 /**
217 * "sIDKey" is the cache key of the identity hash
218 */
219 static constexpr uint8_t sIDKey = 0;
220
Leon Scroggins III8cedb662022-05-02 10:38:38 -0400221 /**
222 * Most of this class concerns persistent storage for shaders, but it's also
223 * interesting to keep track of how many shaders are stored in RAM. This
224 * class provides a convenient entry point for that.
225 */
Matt Buckleye278da12023-06-20 22:51:05 +0000226 int mNumShadersCachedInRam GUARDED_BY(mMutex) = 0;
Leon Scroggins III8cedb662022-05-02 10:38:38 -0400227
John Reck283bb462018-12-13 16:40:14 -0800228 friend class ShaderCacheTestUtils; // used for unit testing
Stan Ilievd495f432017-10-09 15:49:32 -0400229};
230
231} /* namespace skiapipeline */
232} /* namespace uirenderer */
233} /* namespace android */