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 | #include "ShaderCache.h" |
Adlai Holler | d234521 | 2020-10-07 14:16:40 -0400 | [diff] [blame] | 18 | #include <GrDirectContext.h> |
Kevin Lubick | 1175dc0 | 2022-02-28 12:41:27 -0500 | [diff] [blame] | 19 | #include <SkData.h> |
rnlee | ce9762b | 2021-05-21 15:40:53 -0700 | [diff] [blame] | 20 | #include <gui/TraceUtils.h> |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 21 | #include <log/log.h> |
Yichi Chen | 9f95955 | 2018-03-29 21:21:54 +0800 | [diff] [blame] | 22 | #include <openssl/sha.h> |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 23 | #include <algorithm> |
| 24 | #include <array> |
| 25 | #include <thread> |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 26 | #include "FileBlobCache.h" |
Lingfeng Yang | 3a9f223 | 2018-01-24 10:40:18 -0800 | [diff] [blame] | 27 | #include "Properties.h" |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | namespace uirenderer { |
| 31 | namespace skiapipeline { |
| 32 | |
| 33 | // Cache size limits. |
| 34 | static const size_t maxKeySize = 1024; |
Leon Scroggins III | 05f5eca | 2021-06-07 16:09:37 -0400 | [diff] [blame] | 35 | static const size_t maxValueSize = 2 * 1024 * 1024; |
Stan Iliev | da8a510 | 2019-01-24 14:57:01 -0500 | [diff] [blame] | 36 | static const size_t maxTotalSize = 1024 * 1024; |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 37 | |
| 38 | ShaderCache::ShaderCache() { |
| 39 | // There is an "incomplete FileBlobCache type" compilation error, if ctor is moved to header. |
| 40 | } |
| 41 | |
| 42 | ShaderCache ShaderCache::sCache; |
| 43 | |
| 44 | ShaderCache& ShaderCache::get() { |
| 45 | return sCache; |
| 46 | } |
| 47 | |
Yichi Chen | 9f95955 | 2018-03-29 21:21:54 +0800 | [diff] [blame] | 48 | bool ShaderCache::validateCache(const void* identity, ssize_t size) { |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 49 | if (nullptr == identity && size == 0) return true; |
Yichi Chen | 9f95955 | 2018-03-29 21:21:54 +0800 | [diff] [blame] | 50 | |
| 51 | if (nullptr == identity || size < 0) { |
| 52 | if (CC_UNLIKELY(Properties::debugLevel & kDebugCaches)) { |
| 53 | ALOGW("ShaderCache::validateCache invalid cache identity"); |
| 54 | } |
| 55 | mBlobCache->clear(); |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | SHA256_CTX ctx; |
| 60 | SHA256_Init(&ctx); |
| 61 | |
| 62 | SHA256_Update(&ctx, identity, size); |
| 63 | mIDHash.resize(SHA256_DIGEST_LENGTH); |
| 64 | SHA256_Final(mIDHash.data(), &ctx); |
| 65 | |
| 66 | std::array<uint8_t, SHA256_DIGEST_LENGTH> hash; |
| 67 | auto key = sIDKey; |
| 68 | auto loaded = mBlobCache->get(&key, sizeof(key), hash.data(), hash.size()); |
| 69 | |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 70 | if (loaded && std::equal(hash.begin(), hash.end(), mIDHash.begin())) return true; |
Yichi Chen | 9f95955 | 2018-03-29 21:21:54 +0800 | [diff] [blame] | 71 | |
| 72 | if (CC_UNLIKELY(Properties::debugLevel & kDebugCaches)) { |
| 73 | ALOGW("ShaderCache::validateCache cache validation fails"); |
| 74 | } |
| 75 | mBlobCache->clear(); |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | void ShaderCache::initShaderDiskCache(const void* identity, ssize_t size) { |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 80 | ATRACE_NAME("initShaderDiskCache"); |
| 81 | std::lock_guard<std::mutex> lock(mMutex); |
Lingfeng Yang | 3a9f223 | 2018-01-24 10:40:18 -0800 | [diff] [blame] | 82 | |
| 83 | // Emulators can switch between different renders either as part of config |
| 84 | // or snapshot migration. Also, program binaries may not work well on some |
| 85 | // desktop / laptop GPUs. Thus, disable the shader disk cache for emulator builds. |
| 86 | if (!Properties::runningInEmulator && mFilename.length() > 0) { |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 87 | mBlobCache.reset(new FileBlobCache(maxKeySize, maxValueSize, maxTotalSize, mFilename)); |
Yichi Chen | 9f95955 | 2018-03-29 21:21:54 +0800 | [diff] [blame] | 88 | validateCache(identity, size); |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 89 | mInitialized = true; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | void ShaderCache::setFilename(const char* filename) { |
| 94 | std::lock_guard<std::mutex> lock(mMutex); |
| 95 | mFilename = filename; |
| 96 | } |
| 97 | |
| 98 | BlobCache* ShaderCache::getBlobCacheLocked() { |
| 99 | LOG_ALWAYS_FATAL_IF(!mInitialized, "ShaderCache has not been initialized"); |
| 100 | return mBlobCache.get(); |
| 101 | } |
| 102 | |
| 103 | sk_sp<SkData> ShaderCache::load(const SkData& key) { |
| 104 | ATRACE_NAME("ShaderCache::load"); |
| 105 | size_t keySize = key.size(); |
| 106 | std::lock_guard<std::mutex> lock(mMutex); |
| 107 | if (!mInitialized) { |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 108 | return nullptr; |
| 109 | } |
| 110 | |
| 111 | // mObservedBlobValueSize is reasonably big to avoid memory reallocation |
| 112 | // Allocate a buffer with malloc. SkData takes ownership of that allocation and will call free. |
| 113 | void* valueBuffer = malloc(mObservedBlobValueSize); |
| 114 | if (!valueBuffer) { |
| 115 | return nullptr; |
| 116 | } |
| 117 | BlobCache* bc = getBlobCacheLocked(); |
| 118 | size_t valueSize = bc->get(key.data(), keySize, valueBuffer, mObservedBlobValueSize); |
| 119 | int maxTries = 3; |
| 120 | while (valueSize > mObservedBlobValueSize && maxTries > 0) { |
| 121 | mObservedBlobValueSize = std::min(valueSize, maxValueSize); |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 122 | void* newValueBuffer = realloc(valueBuffer, mObservedBlobValueSize); |
Stan Iliev | 003a9f6 | 2018-03-29 13:33:53 -0400 | [diff] [blame] | 123 | if (!newValueBuffer) { |
| 124 | free(valueBuffer); |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 125 | return nullptr; |
| 126 | } |
Stan Iliev | 003a9f6 | 2018-03-29 13:33:53 -0400 | [diff] [blame] | 127 | valueBuffer = newValueBuffer; |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 128 | valueSize = bc->get(key.data(), keySize, valueBuffer, mObservedBlobValueSize); |
| 129 | maxTries--; |
| 130 | } |
| 131 | if (!valueSize) { |
| 132 | free(valueBuffer); |
| 133 | return nullptr; |
| 134 | } |
| 135 | if (valueSize > mObservedBlobValueSize) { |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 136 | ALOGE("ShaderCache::load value size is too big %d", (int)valueSize); |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 137 | free(valueBuffer); |
| 138 | return nullptr; |
| 139 | } |
Leon Scroggins III | 8cedb66 | 2022-05-02 10:38:38 -0400 | [diff] [blame] | 140 | mNumShadersCachedInRam++; |
| 141 | ATRACE_FORMAT("HWUI RAM cache: %d shaders", mNumShadersCachedInRam); |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 142 | return SkData::MakeFromMalloc(valueBuffer, valueSize); |
| 143 | } |
| 144 | |
Leon Scroggins III | 77644a2 | 2022-05-03 15:50:51 -0400 | [diff] [blame] | 145 | namespace { |
| 146 | // Helper for BlobCache::set to trace the result. |
| 147 | void set(BlobCache* cache, const void* key, size_t keySize, const void* value, size_t valueSize) { |
| 148 | switch (cache->set(key, keySize, value, valueSize)) { |
| 149 | case BlobCache::InsertResult::kInserted: |
| 150 | // This is what we expect/hope. It means the cache is large enough. |
| 151 | return; |
| 152 | case BlobCache::InsertResult::kDidClean: { |
| 153 | ATRACE_FORMAT("ShaderCache: evicted an entry to fit {key: %lu value %lu}!", keySize, |
| 154 | valueSize); |
| 155 | return; |
| 156 | } |
| 157 | case BlobCache::InsertResult::kNotEnoughSpace: { |
| 158 | ATRACE_FORMAT("ShaderCache: could not fit {key: %lu value %lu}!", keySize, valueSize); |
| 159 | return; |
| 160 | } |
| 161 | case BlobCache::InsertResult::kInvalidValueSize: |
| 162 | case BlobCache::InsertResult::kInvalidKeySize: { |
| 163 | ATRACE_FORMAT("ShaderCache: invalid size {key: %lu value %lu}!", keySize, valueSize); |
| 164 | return; |
| 165 | } |
| 166 | case BlobCache::InsertResult::kKeyTooBig: |
| 167 | case BlobCache::InsertResult::kValueTooBig: |
| 168 | case BlobCache::InsertResult::kCombinedTooBig: { |
| 169 | ATRACE_FORMAT("ShaderCache: entry too big: {key: %lu value %lu}!", keySize, valueSize); |
| 170 | return; |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | } // namespace |
| 175 | |
Yichi Chen | 9f95955 | 2018-03-29 21:21:54 +0800 | [diff] [blame] | 176 | void ShaderCache::saveToDiskLocked() { |
| 177 | ATRACE_NAME("ShaderCache::saveToDiskLocked"); |
Nolan Scobie | 193cd96 | 2023-02-08 20:03:31 -0500 | [diff] [blame^] | 178 | if (mInitialized && mBlobCache) { |
Yichi Chen | 9f95955 | 2018-03-29 21:21:54 +0800 | [diff] [blame] | 179 | if (mIDHash.size()) { |
| 180 | auto key = sIDKey; |
Leon Scroggins III | 77644a2 | 2022-05-03 15:50:51 -0400 | [diff] [blame] | 181 | set(mBlobCache.get(), &key, sizeof(key), mIDHash.data(), mIDHash.size()); |
Yichi Chen | 9f95955 | 2018-03-29 21:21:54 +0800 | [diff] [blame] | 182 | } |
| 183 | mBlobCache->writeToFile(); |
| 184 | } |
Yichi Chen | 9f95955 | 2018-03-29 21:21:54 +0800 | [diff] [blame] | 185 | } |
| 186 | |
Leon Scroggins III | 8cedb66 | 2022-05-02 10:38:38 -0400 | [diff] [blame] | 187 | void ShaderCache::store(const SkData& key, const SkData& data, const SkString& /*description*/) { |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 188 | ATRACE_NAME("ShaderCache::store"); |
| 189 | std::lock_guard<std::mutex> lock(mMutex); |
Leon Scroggins III | 8cedb66 | 2022-05-02 10:38:38 -0400 | [diff] [blame] | 190 | mNumShadersCachedInRam++; |
| 191 | ATRACE_FORMAT("HWUI RAM cache: %d shaders", mNumShadersCachedInRam); |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 192 | |
| 193 | if (!mInitialized) { |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 194 | return; |
| 195 | } |
| 196 | |
| 197 | size_t valueSize = data.size(); |
| 198 | size_t keySize = key.size(); |
| 199 | if (keySize == 0 || valueSize == 0 || valueSize >= maxValueSize) { |
| 200 | ALOGW("ShaderCache::store: sizes %d %d not allowed", (int)keySize, (int)valueSize); |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | const void* value = data.data(); |
| 205 | |
| 206 | BlobCache* bc = getBlobCacheLocked(); |
Stan Iliev | 14211aa | 2019-01-14 12:29:30 -0500 | [diff] [blame] | 207 | if (mInStoreVkPipelineInProgress) { |
| 208 | if (mOldPipelineCacheSize == -1) { |
| 209 | // Record the initial pipeline cache size stored in the file. |
| 210 | mOldPipelineCacheSize = bc->get(key.data(), keySize, nullptr, 0); |
| 211 | } |
| 212 | if (mNewPipelineCacheSize != -1 && mNewPipelineCacheSize == valueSize) { |
| 213 | // There has not been change in pipeline cache size. Stop trying to save. |
| 214 | mTryToStorePipelineCache = false; |
| 215 | return; |
| 216 | } |
| 217 | mNewPipelineCacheSize = valueSize; |
| 218 | } else { |
| 219 | mCacheDirty = true; |
| 220 | // If there are new shaders compiled, we probably have new pipeline state too. |
| 221 | // Store pipeline cache on the next flush. |
| 222 | mNewPipelineCacheSize = -1; |
| 223 | mTryToStorePipelineCache = true; |
| 224 | } |
Leon Scroggins III | 77644a2 | 2022-05-03 15:50:51 -0400 | [diff] [blame] | 225 | set(bc, key.data(), keySize, value, valueSize); |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 226 | |
Nolan Scobie | 193cd96 | 2023-02-08 20:03:31 -0500 | [diff] [blame^] | 227 | if (!mSavePending && mDeferredSaveDelayMs > 0) { |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 228 | mSavePending = true; |
| 229 | std::thread deferredSaveThread([this]() { |
Nolan Scobie | 193cd96 | 2023-02-08 20:03:31 -0500 | [diff] [blame^] | 230 | usleep(mDeferredSaveDelayMs * 1000); // milliseconds to microseconds |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 231 | std::lock_guard<std::mutex> lock(mMutex); |
Stan Iliev | 14211aa | 2019-01-14 12:29:30 -0500 | [diff] [blame] | 232 | // Store file on disk if there a new shader or Vulkan pipeline cache size changed. |
| 233 | if (mCacheDirty || mNewPipelineCacheSize != mOldPipelineCacheSize) { |
| 234 | saveToDiskLocked(); |
| 235 | mOldPipelineCacheSize = mNewPipelineCacheSize; |
| 236 | mTryToStorePipelineCache = false; |
| 237 | mCacheDirty = false; |
| 238 | } |
Nolan Scobie | 193cd96 | 2023-02-08 20:03:31 -0500 | [diff] [blame^] | 239 | mSavePending = false; |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 240 | }); |
| 241 | deferredSaveThread.detach(); |
| 242 | } |
| 243 | } |
| 244 | |
Adlai Holler | d234521 | 2020-10-07 14:16:40 -0400 | [diff] [blame] | 245 | void ShaderCache::onVkFrameFlushed(GrDirectContext* context) { |
Stan Iliev | 14211aa | 2019-01-14 12:29:30 -0500 | [diff] [blame] | 246 | { |
| 247 | std::lock_guard<std::mutex> lock(mMutex); |
| 248 | |
| 249 | if (!mInitialized || !mTryToStorePipelineCache) { |
| 250 | return; |
| 251 | } |
| 252 | } |
| 253 | mInStoreVkPipelineInProgress = true; |
| 254 | context->storeVkPipelineCacheData(); |
| 255 | mInStoreVkPipelineInProgress = false; |
| 256 | } |
| 257 | |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 258 | } /* namespace skiapipeline */ |
| 259 | } /* namespace uirenderer */ |
| 260 | } /* namespace android */ |