blob: 2fa56f613144bd7a0df1884151706ff2fba599de [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"
18#include <algorithm>
19#include <log/log.h>
20#include <thread>
21#include "FileBlobCache.h"
Lingfeng Yang3a9f2232018-01-24 10:40:18 -080022#include "Properties.h"
Stan Ilievd495f432017-10-09 15:49:32 -040023#include "utils/TraceUtils.h"
24
25namespace android {
26namespace uirenderer {
27namespace skiapipeline {
28
29// Cache size limits.
30static const size_t maxKeySize = 1024;
31static const size_t maxValueSize = 64 * 1024;
32static const size_t maxTotalSize = 512 * 1024;
33
34ShaderCache::ShaderCache() {
35 // There is an "incomplete FileBlobCache type" compilation error, if ctor is moved to header.
36}
37
38ShaderCache ShaderCache::sCache;
39
40ShaderCache& ShaderCache::get() {
41 return sCache;
42}
43
44void ShaderCache::initShaderDiskCache() {
45 ATRACE_NAME("initShaderDiskCache");
46 std::lock_guard<std::mutex> lock(mMutex);
Lingfeng Yang3a9f2232018-01-24 10:40:18 -080047
48 // Emulators can switch between different renders either as part of config
49 // or snapshot migration. Also, program binaries may not work well on some
50 // desktop / laptop GPUs. Thus, disable the shader disk cache for emulator builds.
51 if (!Properties::runningInEmulator && mFilename.length() > 0) {
Stan Ilievd495f432017-10-09 15:49:32 -040052 mBlobCache.reset(new FileBlobCache(maxKeySize, maxValueSize, maxTotalSize, mFilename));
53 mInitialized = true;
54 }
55}
56
57void ShaderCache::setFilename(const char* filename) {
58 std::lock_guard<std::mutex> lock(mMutex);
59 mFilename = filename;
60}
61
62BlobCache* ShaderCache::getBlobCacheLocked() {
63 LOG_ALWAYS_FATAL_IF(!mInitialized, "ShaderCache has not been initialized");
64 return mBlobCache.get();
65}
66
67sk_sp<SkData> ShaderCache::load(const SkData& key) {
68 ATRACE_NAME("ShaderCache::load");
69 size_t keySize = key.size();
70 std::lock_guard<std::mutex> lock(mMutex);
71 if (!mInitialized) {
Stan Ilievd495f432017-10-09 15:49:32 -040072 return nullptr;
73 }
74
75 // mObservedBlobValueSize is reasonably big to avoid memory reallocation
76 // Allocate a buffer with malloc. SkData takes ownership of that allocation and will call free.
77 void* valueBuffer = malloc(mObservedBlobValueSize);
78 if (!valueBuffer) {
79 return nullptr;
80 }
81 BlobCache* bc = getBlobCacheLocked();
82 size_t valueSize = bc->get(key.data(), keySize, valueBuffer, mObservedBlobValueSize);
83 int maxTries = 3;
84 while (valueSize > mObservedBlobValueSize && maxTries > 0) {
85 mObservedBlobValueSize = std::min(valueSize, maxValueSize);
86 valueBuffer = realloc(valueBuffer, mObservedBlobValueSize);
87 if (!valueBuffer) {
88 return nullptr;
89 }
90 valueSize = bc->get(key.data(), keySize, valueBuffer, mObservedBlobValueSize);
91 maxTries--;
92 }
93 if (!valueSize) {
94 free(valueBuffer);
95 return nullptr;
96 }
97 if (valueSize > mObservedBlobValueSize) {
98 ALOGE("ShaderCache::load value size is too big %d", (int) valueSize);
99 free(valueBuffer);
100 return nullptr;
101 }
102 return SkData::MakeFromMalloc(valueBuffer, valueSize);
103}
104
105void ShaderCache::store(const SkData& key, const SkData& data) {
106 ATRACE_NAME("ShaderCache::store");
107 std::lock_guard<std::mutex> lock(mMutex);
108
109 if (!mInitialized) {
Stan Ilievd495f432017-10-09 15:49:32 -0400110 return;
111 }
112
113 size_t valueSize = data.size();
114 size_t keySize = key.size();
115 if (keySize == 0 || valueSize == 0 || valueSize >= maxValueSize) {
116 ALOGW("ShaderCache::store: sizes %d %d not allowed", (int)keySize, (int)valueSize);
117 return;
118 }
119
120 const void* value = data.data();
121
122 BlobCache* bc = getBlobCacheLocked();
123 bc->set(key.data(), keySize, value, valueSize);
124
125 if (!mSavePending && mDeferredSaveDelay > 0) {
126 mSavePending = true;
127 std::thread deferredSaveThread([this]() {
128 sleep(mDeferredSaveDelay);
129 std::lock_guard<std::mutex> lock(mMutex);
130 ATRACE_NAME("ShaderCache::saveToDisk");
131 if (mInitialized && mBlobCache) {
132 mBlobCache->writeToFile();
133 }
134 mSavePending = false;
135 });
136 deferredSaveThread.detach();
137 }
138}
139
140} /* namespace skiapipeline */
141} /* namespace uirenderer */
142} /* namespace android */