Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 1 | /* |
| 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 Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 19 | #include <GrContextOptions.h> |
Kevin Lubick | 1175dc0 | 2022-02-28 12:41:27 -0500 | [diff] [blame] | 20 | #include <SkRefCnt.h> |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 21 | #include <cutils/compiler.h> |
Matt Buckley | e278da1 | 2023-06-20 22:51:05 +0000 | [diff] [blame] | 22 | #include <ftl/shared_mutex.h> |
| 23 | #include <utils/Mutex.h> |
| 24 | |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 25 | #include <memory> |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 26 | #include <string> |
| 27 | #include <vector> |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 28 | |
Kevin Lubick | 1175dc0 | 2022-02-28 12:41:27 -0500 | [diff] [blame] | 29 | class SkData; |
| 30 | |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 31 | namespace android { |
| 32 | |
| 33 | class BlobCache; |
| 34 | class FileBlobCache; |
| 35 | |
| 36 | namespace uirenderer { |
| 37 | namespace skiapipeline { |
| 38 | |
| 39 | class ShaderCache : public GrContextOptions::PersistentCache { |
| 40 | public: |
| 41 | /** |
| 42 | * "get" returns a pointer to the singleton ShaderCache object. This |
| 43 | * singleton object will never be destroyed. |
| 44 | */ |
Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 45 | static ShaderCache& get(); |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 46 | |
| 47 | /** |
Yichi Chen | 9f95955 | 2018-03-29 21:21:54 +0800 | [diff] [blame] | 48 | * 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 Lubick | 1175dc0 | 2022-02-28 12:41:27 -0500 | [diff] [blame] | 53 | * no data. If size is less than zero, the cache is initialized but |
Yichi Chen | 9f95955 | 2018-03-29 21:21:54 +0800 | [diff] [blame] | 54 | * 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 Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 59 | */ |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 60 | virtual void initShaderDiskCache(const void* identity, ssize_t size); |
Yichi Chen | 9f95955 | 2018-03-29 21:21:54 +0800 | [diff] [blame] | 61 | |
| 62 | virtual void initShaderDiskCache() { initShaderDiskCache(nullptr, 0); } |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 63 | |
| 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 III | 8cedb66 | 2022-05-02 10:38:38 -0400 | [diff] [blame] | 81 | void store(const SkData& key, const SkData& data, const SkString& description) override; |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 82 | |
Stan Iliev | 14211aa | 2019-01-14 12:29:30 -0500 | [diff] [blame] | 83 | /** |
| 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 Holler | d234521 | 2020-10-07 14:16:40 -0400 | [diff] [blame] | 88 | void onVkFrameFlushed(GrDirectContext* context); |
Stan Iliev | 14211aa | 2019-01-14 12:29:30 -0500 | [diff] [blame] | 89 | |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 90 | private: |
| 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 | /** |
Yichi Chen | 9f95955 | 2018-03-29 21:21:54 +0800 | [diff] [blame] | 99 | * "validateCache" updates the cache to match the given identity. If the |
| 100 | * cache currently has the wrong identity, all entries in the cache are cleared. |
| 101 | */ |
Matt Buckley | e278da1 | 2023-06-20 22:51:05 +0000 | [diff] [blame] | 102 | bool validateCache(const void* identity, ssize_t size) REQUIRES(mMutex); |
Yichi Chen | 9f95955 | 2018-03-29 21:21:54 +0800 | [diff] [blame] | 103 | |
| 104 | /** |
Matt Buckley | e9adfbd | 2023-07-06 23:15:30 +0000 | [diff] [blame^] | 105 | * Helper for BlobCache::set to trace the result and ensure the identity hash |
| 106 | * does not get evicted. |
| 107 | */ |
| 108 | void set(const void* key, size_t keySize, const void* value, size_t valueSize) REQUIRES(mMutex); |
| 109 | |
| 110 | /** |
Matt Buckley | e278da1 | 2023-06-20 22:51:05 +0000 | [diff] [blame] | 111 | * "saveToDiskLocked" attempts to save the current contents of the cache to |
Yichi Chen | 9f95955 | 2018-03-29 21:21:54 +0800 | [diff] [blame] | 112 | * disk. If the identity hash exists, we will insert the identity hash into |
| 113 | * the cache for next validation. |
| 114 | */ |
Matt Buckley | e278da1 | 2023-06-20 22:51:05 +0000 | [diff] [blame] | 115 | void saveToDiskLocked() REQUIRES(mMutex); |
Yichi Chen | 9f95955 | 2018-03-29 21:21:54 +0800 | [diff] [blame] | 116 | |
| 117 | /** |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 118 | * "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 | */ |
Matt Buckley | e278da1 | 2023-06-20 22:51:05 +0000 | [diff] [blame] | 125 | bool mInitialized GUARDED_BY(mMutex) = false; |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 126 | |
| 127 | /** |
Matt Buckley | e9adfbd | 2023-07-06 23:15:30 +0000 | [diff] [blame^] | 128 | * "mBlobCache" is the cache in which the key/value blob pairs are stored. |
| 129 | * The blob cache contains the Android build number. We treat version mismatches |
| 130 | * as an empty cache (logic implemented in BlobCache::unflatten). |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 131 | */ |
Matt Buckley | e278da1 | 2023-06-20 22:51:05 +0000 | [diff] [blame] | 132 | std::unique_ptr<FileBlobCache> mBlobCache GUARDED_BY(mMutex); |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 133 | |
| 134 | /** |
| 135 | * "mFilename" is the name of the file for storing cache contents in between |
| 136 | * program invocations. It is initialized to an empty string at |
| 137 | * construction time, and can be set with the setCacheFilename method. An |
| 138 | * empty string indicates that the cache should not be saved to or restored |
| 139 | * from disk. |
| 140 | */ |
Matt Buckley | e278da1 | 2023-06-20 22:51:05 +0000 | [diff] [blame] | 141 | std::string mFilename GUARDED_BY(mMutex); |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 142 | |
| 143 | /** |
Yichi Chen | 9f95955 | 2018-03-29 21:21:54 +0800 | [diff] [blame] | 144 | * "mIDHash" is the current identity hash for the cache validation. It is |
| 145 | * initialized to an empty vector at construction time, and its content is |
| 146 | * generated in the call of the validateCache method. An empty vector |
| 147 | * indicates that cache validation is not performed, and the hash should |
| 148 | * not be stored on disk. |
| 149 | */ |
Matt Buckley | e278da1 | 2023-06-20 22:51:05 +0000 | [diff] [blame] | 150 | std::vector<uint8_t> mIDHash GUARDED_BY(mMutex); |
Yichi Chen | 9f95955 | 2018-03-29 21:21:54 +0800 | [diff] [blame] | 151 | |
| 152 | /** |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 153 | * "mSavePending" indicates whether or not a deferred save operation is |
| 154 | * pending. Each time a key/value pair is inserted into the cache via |
| 155 | * load, a deferred save is initiated if one is not already pending. |
| 156 | * This will wait some amount of time and then trigger a save of the cache |
Nolan Scobie | 193cd96 | 2023-02-08 20:03:31 -0500 | [diff] [blame] | 157 | * contents to disk, unless mDeferredSaveDelayMs is 0 in which case saving |
| 158 | * is disabled. |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 159 | */ |
Matt Buckley | e278da1 | 2023-06-20 22:51:05 +0000 | [diff] [blame] | 160 | bool mSavePending GUARDED_BY(mMutex) = false; |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 161 | |
| 162 | /** |
| 163 | * "mObservedBlobValueSize" is the maximum value size observed by the cache reading function. |
| 164 | */ |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 165 | size_t mObservedBlobValueSize = 20 * 1024; |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 166 | |
| 167 | /** |
Nolan Scobie | 193cd96 | 2023-02-08 20:03:31 -0500 | [diff] [blame] | 168 | * The time in milliseconds to wait before saving newly inserted cache entries. |
| 169 | * |
| 170 | * WARNING: setting this to 0 will disable writing the cache to disk. |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 171 | */ |
Nolan Scobie | 193cd96 | 2023-02-08 20:03:31 -0500 | [diff] [blame] | 172 | unsigned int mDeferredSaveDelayMs = 4 * 1000; |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 173 | |
| 174 | /** |
Matt Buckley | e278da1 | 2023-06-20 22:51:05 +0000 | [diff] [blame] | 175 | * "mMutex" is the shared mutex used to prevent concurrent access to the member |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 176 | * variables. It must be locked whenever the member variables are accessed. |
| 177 | */ |
Matt Buckley | e278da1 | 2023-06-20 22:51:05 +0000 | [diff] [blame] | 178 | mutable ftl::SharedMutex mMutex; |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 179 | |
| 180 | /** |
Stan Iliev | 14211aa | 2019-01-14 12:29:30 -0500 | [diff] [blame] | 181 | * If set to "true", the next call to onVkFrameFlushed, will invoke |
| 182 | * GrCanvas::storeVkPipelineCacheData. This does not guarantee that data will be stored on disk. |
| 183 | */ |
Matt Buckley | e278da1 | 2023-06-20 22:51:05 +0000 | [diff] [blame] | 184 | bool mTryToStorePipelineCache GUARDED_BY(mMutex) = true; |
Stan Iliev | 14211aa | 2019-01-14 12:29:30 -0500 | [diff] [blame] | 185 | |
| 186 | /** |
| 187 | * This flag is used by "ShaderCache::store" to distinguish between shader data and |
| 188 | * Vulkan pipeline data. |
| 189 | */ |
| 190 | bool mInStoreVkPipelineInProgress = false; |
| 191 | |
| 192 | /** |
| 193 | * "mNewPipelineCacheSize" has the size of the new Vulkan pipeline cache data. It is used |
| 194 | * to prevent unnecessary disk writes, if the pipeline cache size has not changed. |
| 195 | */ |
Matt Buckley | e278da1 | 2023-06-20 22:51:05 +0000 | [diff] [blame] | 196 | size_t mNewPipelineCacheSize GUARDED_BY(mMutex) = -1; |
Stan Iliev | 14211aa | 2019-01-14 12:29:30 -0500 | [diff] [blame] | 197 | /** |
| 198 | * "mOldPipelineCacheSize" has the size of the Vulkan pipeline cache data stored on disk. |
| 199 | */ |
Matt Buckley | e278da1 | 2023-06-20 22:51:05 +0000 | [diff] [blame] | 200 | size_t mOldPipelineCacheSize GUARDED_BY(mMutex) = -1; |
Stan Iliev | 14211aa | 2019-01-14 12:29:30 -0500 | [diff] [blame] | 201 | |
| 202 | /** |
| 203 | * "mCacheDirty" is true when there is new shader cache data, which is not saved to disk. |
| 204 | */ |
Matt Buckley | e278da1 | 2023-06-20 22:51:05 +0000 | [diff] [blame] | 205 | bool mCacheDirty GUARDED_BY(mMutex) = false; |
Stan Iliev | 14211aa | 2019-01-14 12:29:30 -0500 | [diff] [blame] | 206 | |
| 207 | /** |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 208 | * "sCache" is the singleton ShaderCache object. |
| 209 | */ |
| 210 | static ShaderCache sCache; |
| 211 | |
Yichi Chen | 9f95955 | 2018-03-29 21:21:54 +0800 | [diff] [blame] | 212 | /** |
| 213 | * "sIDKey" is the cache key of the identity hash |
| 214 | */ |
| 215 | static constexpr uint8_t sIDKey = 0; |
| 216 | |
Leon Scroggins III | 8cedb66 | 2022-05-02 10:38:38 -0400 | [diff] [blame] | 217 | /** |
| 218 | * Most of this class concerns persistent storage for shaders, but it's also |
| 219 | * interesting to keep track of how many shaders are stored in RAM. This |
| 220 | * class provides a convenient entry point for that. |
| 221 | */ |
Matt Buckley | e278da1 | 2023-06-20 22:51:05 +0000 | [diff] [blame] | 222 | int mNumShadersCachedInRam GUARDED_BY(mMutex) = 0; |
Leon Scroggins III | 8cedb66 | 2022-05-02 10:38:38 -0400 | [diff] [blame] | 223 | |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 224 | friend class ShaderCacheTestUtils; // used for unit testing |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 225 | }; |
| 226 | |
| 227 | } /* namespace skiapipeline */ |
| 228 | } /* namespace uirenderer */ |
| 229 | } /* namespace android */ |