blob: 3baff7ea8f90fd7f82105d1161e3ea4c5ad3a860 [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"
Adlai Hollerd2345212020-10-07 14:16:40 -040018#include <GrDirectContext.h>
Stan Ilievd495f432017-10-09 15:49:32 -040019#include <log/log.h>
Yichi Chen9f959552018-03-29 21:21:54 +080020#include <openssl/sha.h>
John Reck283bb462018-12-13 16:40:14 -080021#include <algorithm>
22#include <array>
23#include <thread>
Stan Ilievd495f432017-10-09 15:49:32 -040024#include "FileBlobCache.h"
Lingfeng Yang3a9f2232018-01-24 10:40:18 -080025#include "Properties.h"
Stan Ilievd495f432017-10-09 15:49:32 -040026#include "utils/TraceUtils.h"
27
28namespace android {
29namespace uirenderer {
30namespace skiapipeline {
31
32// Cache size limits.
33static const size_t maxKeySize = 1024;
Stan Ilievda8a5102019-01-24 14:57:01 -050034static const size_t maxValueSize = 512 * 1024;
35static const size_t maxTotalSize = 1024 * 1024;
Stan Ilievd495f432017-10-09 15:49:32 -040036
37ShaderCache::ShaderCache() {
38 // There is an "incomplete FileBlobCache type" compilation error, if ctor is moved to header.
39}
40
41ShaderCache ShaderCache::sCache;
42
43ShaderCache& ShaderCache::get() {
44 return sCache;
45}
46
Yichi Chen9f959552018-03-29 21:21:54 +080047bool ShaderCache::validateCache(const void* identity, ssize_t size) {
John Reck283bb462018-12-13 16:40:14 -080048 if (nullptr == identity && size == 0) return true;
Yichi Chen9f959552018-03-29 21:21:54 +080049
50 if (nullptr == identity || size < 0) {
51 if (CC_UNLIKELY(Properties::debugLevel & kDebugCaches)) {
52 ALOGW("ShaderCache::validateCache invalid cache identity");
53 }
54 mBlobCache->clear();
55 return false;
56 }
57
58 SHA256_CTX ctx;
59 SHA256_Init(&ctx);
60
61 SHA256_Update(&ctx, identity, size);
62 mIDHash.resize(SHA256_DIGEST_LENGTH);
63 SHA256_Final(mIDHash.data(), &ctx);
64
65 std::array<uint8_t, SHA256_DIGEST_LENGTH> hash;
66 auto key = sIDKey;
67 auto loaded = mBlobCache->get(&key, sizeof(key), hash.data(), hash.size());
68
John Reck283bb462018-12-13 16:40:14 -080069 if (loaded && std::equal(hash.begin(), hash.end(), mIDHash.begin())) return true;
Yichi Chen9f959552018-03-29 21:21:54 +080070
71 if (CC_UNLIKELY(Properties::debugLevel & kDebugCaches)) {
72 ALOGW("ShaderCache::validateCache cache validation fails");
73 }
74 mBlobCache->clear();
75 return false;
76}
77
78void ShaderCache::initShaderDiskCache(const void* identity, ssize_t size) {
Stan Ilievd495f432017-10-09 15:49:32 -040079 ATRACE_NAME("initShaderDiskCache");
80 std::lock_guard<std::mutex> lock(mMutex);
Lingfeng Yang3a9f2232018-01-24 10:40:18 -080081
82 // Emulators can switch between different renders either as part of config
83 // or snapshot migration. Also, program binaries may not work well on some
84 // desktop / laptop GPUs. Thus, disable the shader disk cache for emulator builds.
85 if (!Properties::runningInEmulator && mFilename.length() > 0) {
Stan Ilievd495f432017-10-09 15:49:32 -040086 mBlobCache.reset(new FileBlobCache(maxKeySize, maxValueSize, maxTotalSize, mFilename));
Yichi Chen9f959552018-03-29 21:21:54 +080087 validateCache(identity, size);
Stan Ilievd495f432017-10-09 15:49:32 -040088 mInitialized = true;
89 }
90}
91
92void ShaderCache::setFilename(const char* filename) {
93 std::lock_guard<std::mutex> lock(mMutex);
94 mFilename = filename;
95}
96
97BlobCache* ShaderCache::getBlobCacheLocked() {
98 LOG_ALWAYS_FATAL_IF(!mInitialized, "ShaderCache has not been initialized");
99 return mBlobCache.get();
100}
101
102sk_sp<SkData> ShaderCache::load(const SkData& key) {
103 ATRACE_NAME("ShaderCache::load");
104 size_t keySize = key.size();
105 std::lock_guard<std::mutex> lock(mMutex);
106 if (!mInitialized) {
Stan Ilievd495f432017-10-09 15:49:32 -0400107 return nullptr;
108 }
109
110 // mObservedBlobValueSize is reasonably big to avoid memory reallocation
111 // Allocate a buffer with malloc. SkData takes ownership of that allocation and will call free.
112 void* valueBuffer = malloc(mObservedBlobValueSize);
113 if (!valueBuffer) {
114 return nullptr;
115 }
116 BlobCache* bc = getBlobCacheLocked();
117 size_t valueSize = bc->get(key.data(), keySize, valueBuffer, mObservedBlobValueSize);
118 int maxTries = 3;
119 while (valueSize > mObservedBlobValueSize && maxTries > 0) {
120 mObservedBlobValueSize = std::min(valueSize, maxValueSize);
John Reck283bb462018-12-13 16:40:14 -0800121 void* newValueBuffer = realloc(valueBuffer, mObservedBlobValueSize);
Stan Iliev003a9f62018-03-29 13:33:53 -0400122 if (!newValueBuffer) {
123 free(valueBuffer);
Stan Ilievd495f432017-10-09 15:49:32 -0400124 return nullptr;
125 }
Stan Iliev003a9f62018-03-29 13:33:53 -0400126 valueBuffer = newValueBuffer;
Stan Ilievd495f432017-10-09 15:49:32 -0400127 valueSize = bc->get(key.data(), keySize, valueBuffer, mObservedBlobValueSize);
128 maxTries--;
129 }
130 if (!valueSize) {
131 free(valueBuffer);
132 return nullptr;
133 }
134 if (valueSize > mObservedBlobValueSize) {
John Reck283bb462018-12-13 16:40:14 -0800135 ALOGE("ShaderCache::load value size is too big %d", (int)valueSize);
Stan Ilievd495f432017-10-09 15:49:32 -0400136 free(valueBuffer);
137 return nullptr;
138 }
139 return SkData::MakeFromMalloc(valueBuffer, valueSize);
140}
141
Yichi Chen9f959552018-03-29 21:21:54 +0800142void ShaderCache::saveToDiskLocked() {
143 ATRACE_NAME("ShaderCache::saveToDiskLocked");
144 if (mInitialized && mBlobCache && mSavePending) {
145 if (mIDHash.size()) {
146 auto key = sIDKey;
147 mBlobCache->set(&key, sizeof(key), mIDHash.data(), mIDHash.size());
148 }
149 mBlobCache->writeToFile();
150 }
151 mSavePending = false;
152}
153
Stan Ilievd495f432017-10-09 15:49:32 -0400154void ShaderCache::store(const SkData& key, const SkData& data) {
155 ATRACE_NAME("ShaderCache::store");
156 std::lock_guard<std::mutex> lock(mMutex);
157
158 if (!mInitialized) {
Stan Ilievd495f432017-10-09 15:49:32 -0400159 return;
160 }
161
162 size_t valueSize = data.size();
163 size_t keySize = key.size();
164 if (keySize == 0 || valueSize == 0 || valueSize >= maxValueSize) {
165 ALOGW("ShaderCache::store: sizes %d %d not allowed", (int)keySize, (int)valueSize);
166 return;
167 }
168
169 const void* value = data.data();
170
171 BlobCache* bc = getBlobCacheLocked();
Stan Iliev14211aa2019-01-14 12:29:30 -0500172 if (mInStoreVkPipelineInProgress) {
173 if (mOldPipelineCacheSize == -1) {
174 // Record the initial pipeline cache size stored in the file.
175 mOldPipelineCacheSize = bc->get(key.data(), keySize, nullptr, 0);
176 }
177 if (mNewPipelineCacheSize != -1 && mNewPipelineCacheSize == valueSize) {
178 // There has not been change in pipeline cache size. Stop trying to save.
179 mTryToStorePipelineCache = false;
180 return;
181 }
182 mNewPipelineCacheSize = valueSize;
183 } else {
184 mCacheDirty = true;
185 // If there are new shaders compiled, we probably have new pipeline state too.
186 // Store pipeline cache on the next flush.
187 mNewPipelineCacheSize = -1;
188 mTryToStorePipelineCache = true;
189 }
Stan Ilievd495f432017-10-09 15:49:32 -0400190 bc->set(key.data(), keySize, value, valueSize);
191
192 if (!mSavePending && mDeferredSaveDelay > 0) {
193 mSavePending = true;
194 std::thread deferredSaveThread([this]() {
195 sleep(mDeferredSaveDelay);
196 std::lock_guard<std::mutex> lock(mMutex);
Stan Iliev14211aa2019-01-14 12:29:30 -0500197 // Store file on disk if there a new shader or Vulkan pipeline cache size changed.
198 if (mCacheDirty || mNewPipelineCacheSize != mOldPipelineCacheSize) {
199 saveToDiskLocked();
200 mOldPipelineCacheSize = mNewPipelineCacheSize;
201 mTryToStorePipelineCache = false;
202 mCacheDirty = false;
203 }
Stan Ilievd495f432017-10-09 15:49:32 -0400204 });
205 deferredSaveThread.detach();
206 }
207}
208
Adlai Hollerd2345212020-10-07 14:16:40 -0400209void ShaderCache::onVkFrameFlushed(GrDirectContext* context) {
Stan Iliev14211aa2019-01-14 12:29:30 -0500210 {
211 std::lock_guard<std::mutex> lock(mMutex);
212
213 if (!mInitialized || !mTryToStorePipelineCache) {
214 return;
215 }
216 }
217 mInStoreVkPipelineInProgress = true;
218 context->storeVkPipelineCacheData();
219 mInStoreVkPipelineInProgress = false;
220}
221
Stan Ilievd495f432017-10-09 15:49:32 -0400222} /* namespace skiapipeline */
223} /* namespace uirenderer */
224} /* namespace android */