blob: 22f59a67bccbb9e40451805c9e86e0bbc5037439 [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#include "ShaderCache.h"
Ryan Prichardbc3bf5b2024-04-22 22:03:20 -070018
Kevin Lubick1175dc02022-02-28 12:41:27 -050019#include <SkData.h>
rnleece9762b2021-05-21 15:40:53 -070020#include <gui/TraceUtils.h>
Nolan Scobie572d8012024-08-30 13:43:34 -040021#include <include/gpu/ganesh/GrDirectContext.h>
Stan Ilievd495f432017-10-09 15:49:32 -040022#include <log/log.h>
Yichi Chen9f959552018-03-29 21:21:54 +080023#include <openssl/sha.h>
Ryan Prichardbc3bf5b2024-04-22 22:03:20 -070024
John Reck283bb462018-12-13 16:40:14 -080025#include <algorithm>
26#include <array>
Ryan Prichardbc3bf5b2024-04-22 22:03:20 -070027#include <mutex>
John Reck283bb462018-12-13 16:40:14 -080028#include <thread>
Ryan Prichardbc3bf5b2024-04-22 22:03:20 -070029
Stan Ilievd495f432017-10-09 15:49:32 -040030#include "FileBlobCache.h"
Lingfeng Yang3a9f2232018-01-24 10:40:18 -080031#include "Properties.h"
Stan Ilievd495f432017-10-09 15:49:32 -040032
33namespace android {
34namespace uirenderer {
35namespace skiapipeline {
36
37// Cache size limits.
38static const size_t maxKeySize = 1024;
Leon Scroggins III05f5eca2021-06-07 16:09:37 -040039static const size_t maxValueSize = 2 * 1024 * 1024;
Nolan Scobief50917c2023-02-09 13:43:52 -050040static const size_t maxTotalSize = 4 * 1024 * 1024;
41static_assert(maxKeySize + maxValueSize < maxTotalSize);
Stan Ilievd495f432017-10-09 15:49:32 -040042
43ShaderCache::ShaderCache() {
44 // There is an "incomplete FileBlobCache type" compilation error, if ctor is moved to header.
45}
46
47ShaderCache ShaderCache::sCache;
48
49ShaderCache& ShaderCache::get() {
50 return sCache;
51}
52
Yichi Chen9f959552018-03-29 21:21:54 +080053bool ShaderCache::validateCache(const void* identity, ssize_t size) {
John Reck283bb462018-12-13 16:40:14 -080054 if (nullptr == identity && size == 0) return true;
Yichi Chen9f959552018-03-29 21:21:54 +080055
56 if (nullptr == identity || size < 0) {
57 if (CC_UNLIKELY(Properties::debugLevel & kDebugCaches)) {
58 ALOGW("ShaderCache::validateCache invalid cache identity");
59 }
60 mBlobCache->clear();
61 return false;
62 }
63
64 SHA256_CTX ctx;
65 SHA256_Init(&ctx);
66
67 SHA256_Update(&ctx, identity, size);
68 mIDHash.resize(SHA256_DIGEST_LENGTH);
69 SHA256_Final(mIDHash.data(), &ctx);
70
71 std::array<uint8_t, SHA256_DIGEST_LENGTH> hash;
72 auto key = sIDKey;
73 auto loaded = mBlobCache->get(&key, sizeof(key), hash.data(), hash.size());
74
John Reck283bb462018-12-13 16:40:14 -080075 if (loaded && std::equal(hash.begin(), hash.end(), mIDHash.begin())) return true;
Yichi Chen9f959552018-03-29 21:21:54 +080076
77 if (CC_UNLIKELY(Properties::debugLevel & kDebugCaches)) {
78 ALOGW("ShaderCache::validateCache cache validation fails");
79 }
80 mBlobCache->clear();
81 return false;
82}
83
84void ShaderCache::initShaderDiskCache(const void* identity, ssize_t size) {
Stan Ilievd495f432017-10-09 15:49:32 -040085 ATRACE_NAME("initShaderDiskCache");
Matt Buckleye278da12023-06-20 22:51:05 +000086 std::lock_guard lock(mMutex);
Lingfeng Yang3a9f2232018-01-24 10:40:18 -080087
88 // Emulators can switch between different renders either as part of config
89 // or snapshot migration. Also, program binaries may not work well on some
90 // desktop / laptop GPUs. Thus, disable the shader disk cache for emulator builds.
91 if (!Properties::runningInEmulator && mFilename.length() > 0) {
Stan Ilievd495f432017-10-09 15:49:32 -040092 mBlobCache.reset(new FileBlobCache(maxKeySize, maxValueSize, maxTotalSize, mFilename));
Yichi Chen9f959552018-03-29 21:21:54 +080093 validateCache(identity, size);
Stan Ilievd495f432017-10-09 15:49:32 -040094 mInitialized = true;
Matt Buckleye9adfbd2023-07-06 23:15:30 +000095 if (identity != nullptr && size > 0 && mIDHash.size()) {
96 set(&sIDKey, sizeof(sIDKey), mIDHash.data(), mIDHash.size());
97 }
Stan Ilievd495f432017-10-09 15:49:32 -040098 }
99}
100
101void ShaderCache::setFilename(const char* filename) {
Matt Buckleye278da12023-06-20 22:51:05 +0000102 std::lock_guard lock(mMutex);
Stan Ilievd495f432017-10-09 15:49:32 -0400103 mFilename = filename;
104}
105
Stan Ilievd495f432017-10-09 15:49:32 -0400106sk_sp<SkData> ShaderCache::load(const SkData& key) {
107 ATRACE_NAME("ShaderCache::load");
108 size_t keySize = key.size();
Matt Buckleye278da12023-06-20 22:51:05 +0000109 std::lock_guard lock(mMutex);
Stan Ilievd495f432017-10-09 15:49:32 -0400110 if (!mInitialized) {
Stan Ilievd495f432017-10-09 15:49:32 -0400111 return nullptr;
112 }
113
114 // mObservedBlobValueSize is reasonably big to avoid memory reallocation
115 // Allocate a buffer with malloc. SkData takes ownership of that allocation and will call free.
116 void* valueBuffer = malloc(mObservedBlobValueSize);
117 if (!valueBuffer) {
118 return nullptr;
119 }
Matt Buckleye9adfbd2023-07-06 23:15:30 +0000120 size_t valueSize = mBlobCache->get(key.data(), keySize, valueBuffer, mObservedBlobValueSize);
Stan Ilievd495f432017-10-09 15:49:32 -0400121 int maxTries = 3;
122 while (valueSize > mObservedBlobValueSize && maxTries > 0) {
123 mObservedBlobValueSize = std::min(valueSize, maxValueSize);
John Reck283bb462018-12-13 16:40:14 -0800124 void* newValueBuffer = realloc(valueBuffer, mObservedBlobValueSize);
Stan Iliev003a9f62018-03-29 13:33:53 -0400125 if (!newValueBuffer) {
126 free(valueBuffer);
Stan Ilievd495f432017-10-09 15:49:32 -0400127 return nullptr;
128 }
Stan Iliev003a9f62018-03-29 13:33:53 -0400129 valueBuffer = newValueBuffer;
Matt Buckleye9adfbd2023-07-06 23:15:30 +0000130 valueSize = mBlobCache->get(key.data(), keySize, valueBuffer, mObservedBlobValueSize);
Stan Ilievd495f432017-10-09 15:49:32 -0400131 maxTries--;
132 }
133 if (!valueSize) {
134 free(valueBuffer);
135 return nullptr;
136 }
137 if (valueSize > mObservedBlobValueSize) {
John Reck283bb462018-12-13 16:40:14 -0800138 ALOGE("ShaderCache::load value size is too big %d", (int)valueSize);
Stan Ilievd495f432017-10-09 15:49:32 -0400139 free(valueBuffer);
140 return nullptr;
141 }
Leon Scroggins III8cedb662022-05-02 10:38:38 -0400142 mNumShadersCachedInRam++;
143 ATRACE_FORMAT("HWUI RAM cache: %d shaders", mNumShadersCachedInRam);
Stan Ilievd495f432017-10-09 15:49:32 -0400144 return SkData::MakeFromMalloc(valueBuffer, valueSize);
145}
146
Matt Buckleye9adfbd2023-07-06 23:15:30 +0000147void ShaderCache::set(const void* key, size_t keySize, const void* value, size_t valueSize) {
148 switch (mBlobCache->set(key, keySize, value, valueSize)) {
Leon Scroggins III77644a22022-05-03 15:50:51 -0400149 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);
Matt Buckleye9adfbd2023-07-06 23:15:30 +0000155 if (mIDHash.size()) {
156 set(&sIDKey, sizeof(sIDKey), mIDHash.data(), mIDHash.size());
157 }
Leon Scroggins III77644a22022-05-03 15:50:51 -0400158 return;
159 }
160 case BlobCache::InsertResult::kNotEnoughSpace: {
161 ATRACE_FORMAT("ShaderCache: could not fit {key: %lu value %lu}!", keySize, valueSize);
162 return;
163 }
164 case BlobCache::InsertResult::kInvalidValueSize:
165 case BlobCache::InsertResult::kInvalidKeySize: {
166 ATRACE_FORMAT("ShaderCache: invalid size {key: %lu value %lu}!", keySize, valueSize);
167 return;
168 }
169 case BlobCache::InsertResult::kKeyTooBig:
170 case BlobCache::InsertResult::kValueTooBig:
171 case BlobCache::InsertResult::kCombinedTooBig: {
172 ATRACE_FORMAT("ShaderCache: entry too big: {key: %lu value %lu}!", keySize, valueSize);
173 return;
174 }
175 }
176}
Leon Scroggins III77644a22022-05-03 15:50:51 -0400177
Yichi Chen9f959552018-03-29 21:21:54 +0800178void ShaderCache::saveToDiskLocked() {
179 ATRACE_NAME("ShaderCache::saveToDiskLocked");
Nolan Scobie193cd962023-02-08 20:03:31 -0500180 if (mInitialized && mBlobCache) {
Matt Buckleye278da12023-06-20 22:51:05 +0000181 // The most straightforward way to make ownership shared
182 mMutex.unlock();
183 mMutex.lock_shared();
Yichi Chen9f959552018-03-29 21:21:54 +0800184 mBlobCache->writeToFile();
Matt Buckleye278da12023-06-20 22:51:05 +0000185 mMutex.unlock_shared();
186 mMutex.lock();
Yichi Chen9f959552018-03-29 21:21:54 +0800187 }
Yichi Chen9f959552018-03-29 21:21:54 +0800188}
189
Leon Scroggins III8cedb662022-05-02 10:38:38 -0400190void ShaderCache::store(const SkData& key, const SkData& data, const SkString& /*description*/) {
Stan Ilievd495f432017-10-09 15:49:32 -0400191 ATRACE_NAME("ShaderCache::store");
Matt Buckleye278da12023-06-20 22:51:05 +0000192 std::lock_guard lock(mMutex);
Leon Scroggins III8cedb662022-05-02 10:38:38 -0400193 mNumShadersCachedInRam++;
194 ATRACE_FORMAT("HWUI RAM cache: %d shaders", mNumShadersCachedInRam);
Stan Ilievd495f432017-10-09 15:49:32 -0400195
196 if (!mInitialized) {
Stan Ilievd495f432017-10-09 15:49:32 -0400197 return;
198 }
199
200 size_t valueSize = data.size();
201 size_t keySize = key.size();
202 if (keySize == 0 || valueSize == 0 || valueSize >= maxValueSize) {
203 ALOGW("ShaderCache::store: sizes %d %d not allowed", (int)keySize, (int)valueSize);
204 return;
205 }
206
207 const void* value = data.data();
208
Stan Iliev14211aa2019-01-14 12:29:30 -0500209 if (mInStoreVkPipelineInProgress) {
210 if (mOldPipelineCacheSize == -1) {
211 // Record the initial pipeline cache size stored in the file.
Matt Buckleye9adfbd2023-07-06 23:15:30 +0000212 mOldPipelineCacheSize = mBlobCache->get(key.data(), keySize, nullptr, 0);
Stan Iliev14211aa2019-01-14 12:29:30 -0500213 }
214 if (mNewPipelineCacheSize != -1 && mNewPipelineCacheSize == valueSize) {
215 // There has not been change in pipeline cache size. Stop trying to save.
216 mTryToStorePipelineCache = false;
217 return;
218 }
219 mNewPipelineCacheSize = valueSize;
220 } else {
221 mCacheDirty = true;
222 // If there are new shaders compiled, we probably have new pipeline state too.
223 // Store pipeline cache on the next flush.
224 mNewPipelineCacheSize = -1;
225 mTryToStorePipelineCache = true;
226 }
Matt Buckleye9adfbd2023-07-06 23:15:30 +0000227 set(key.data(), keySize, value, valueSize);
Stan Ilievd495f432017-10-09 15:49:32 -0400228
Nolan Scobie193cd962023-02-08 20:03:31 -0500229 if (!mSavePending && mDeferredSaveDelayMs > 0) {
Stan Ilievd495f432017-10-09 15:49:32 -0400230 mSavePending = true;
231 std::thread deferredSaveThread([this]() {
Nolan Scobie193cd962023-02-08 20:03:31 -0500232 usleep(mDeferredSaveDelayMs * 1000); // milliseconds to microseconds
Matt Buckleye278da12023-06-20 22:51:05 +0000233 std::lock_guard lock(mMutex);
Stan Iliev14211aa2019-01-14 12:29:30 -0500234 // Store file on disk if there a new shader or Vulkan pipeline cache size changed.
235 if (mCacheDirty || mNewPipelineCacheSize != mOldPipelineCacheSize) {
236 saveToDiskLocked();
237 mOldPipelineCacheSize = mNewPipelineCacheSize;
238 mTryToStorePipelineCache = false;
239 mCacheDirty = false;
240 }
Nolan Scobie193cd962023-02-08 20:03:31 -0500241 mSavePending = false;
Stan Ilievd495f432017-10-09 15:49:32 -0400242 });
243 deferredSaveThread.detach();
244 }
245}
246
Adlai Hollerd2345212020-10-07 14:16:40 -0400247void ShaderCache::onVkFrameFlushed(GrDirectContext* context) {
Stan Iliev14211aa2019-01-14 12:29:30 -0500248 {
Matt Buckleye278da12023-06-20 22:51:05 +0000249 mMutex.lock_shared();
Stan Iliev14211aa2019-01-14 12:29:30 -0500250 if (!mInitialized || !mTryToStorePipelineCache) {
Matt Buckleye278da12023-06-20 22:51:05 +0000251 mMutex.unlock_shared();
Stan Iliev14211aa2019-01-14 12:29:30 -0500252 return;
253 }
Matt Buckleye278da12023-06-20 22:51:05 +0000254 mMutex.unlock_shared();
Stan Iliev14211aa2019-01-14 12:29:30 -0500255 }
256 mInStoreVkPipelineInProgress = true;
257 context->storeVkPipelineCacheData();
258 mInStoreVkPipelineInProgress = false;
259}
260
Stan Ilievd495f432017-10-09 15:49:32 -0400261} /* namespace skiapipeline */
262} /* namespace uirenderer */
263} /* namespace android */