blob: c7a30141803d8e88d2915421c92fbe35e4e96d77 [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"
22#include "utils/TraceUtils.h"
23
24namespace android {
25namespace uirenderer {
26namespace skiapipeline {
27
28// Cache size limits.
29static const size_t maxKeySize = 1024;
30static const size_t maxValueSize = 64 * 1024;
31static const size_t maxTotalSize = 512 * 1024;
32
33ShaderCache::ShaderCache() {
34 // There is an "incomplete FileBlobCache type" compilation error, if ctor is moved to header.
35}
36
37ShaderCache ShaderCache::sCache;
38
39ShaderCache& ShaderCache::get() {
40 return sCache;
41}
42
43void ShaderCache::initShaderDiskCache() {
44 ATRACE_NAME("initShaderDiskCache");
45 std::lock_guard<std::mutex> lock(mMutex);
46 if (mFilename.length() > 0) {
47 mBlobCache.reset(new FileBlobCache(maxKeySize, maxValueSize, maxTotalSize, mFilename));
48 mInitialized = true;
49 }
50}
51
52void ShaderCache::setFilename(const char* filename) {
53 std::lock_guard<std::mutex> lock(mMutex);
54 mFilename = filename;
55}
56
57BlobCache* ShaderCache::getBlobCacheLocked() {
58 LOG_ALWAYS_FATAL_IF(!mInitialized, "ShaderCache has not been initialized");
59 return mBlobCache.get();
60}
61
62sk_sp<SkData> ShaderCache::load(const SkData& key) {
63 ATRACE_NAME("ShaderCache::load");
64 size_t keySize = key.size();
65 std::lock_guard<std::mutex> lock(mMutex);
66 if (!mInitialized) {
Stan Ilievd495f432017-10-09 15:49:32 -040067 return nullptr;
68 }
69
70 // mObservedBlobValueSize is reasonably big to avoid memory reallocation
71 // Allocate a buffer with malloc. SkData takes ownership of that allocation and will call free.
72 void* valueBuffer = malloc(mObservedBlobValueSize);
73 if (!valueBuffer) {
74 return nullptr;
75 }
76 BlobCache* bc = getBlobCacheLocked();
77 size_t valueSize = bc->get(key.data(), keySize, valueBuffer, mObservedBlobValueSize);
78 int maxTries = 3;
79 while (valueSize > mObservedBlobValueSize && maxTries > 0) {
80 mObservedBlobValueSize = std::min(valueSize, maxValueSize);
81 valueBuffer = realloc(valueBuffer, mObservedBlobValueSize);
82 if (!valueBuffer) {
83 return nullptr;
84 }
85 valueSize = bc->get(key.data(), keySize, valueBuffer, mObservedBlobValueSize);
86 maxTries--;
87 }
88 if (!valueSize) {
89 free(valueBuffer);
90 return nullptr;
91 }
92 if (valueSize > mObservedBlobValueSize) {
93 ALOGE("ShaderCache::load value size is too big %d", (int) valueSize);
94 free(valueBuffer);
95 return nullptr;
96 }
97 return SkData::MakeFromMalloc(valueBuffer, valueSize);
98}
99
100void ShaderCache::store(const SkData& key, const SkData& data) {
101 ATRACE_NAME("ShaderCache::store");
102 std::lock_guard<std::mutex> lock(mMutex);
103
104 if (!mInitialized) {
Stan Ilievd495f432017-10-09 15:49:32 -0400105 return;
106 }
107
108 size_t valueSize = data.size();
109 size_t keySize = key.size();
110 if (keySize == 0 || valueSize == 0 || valueSize >= maxValueSize) {
111 ALOGW("ShaderCache::store: sizes %d %d not allowed", (int)keySize, (int)valueSize);
112 return;
113 }
114
115 const void* value = data.data();
116
117 BlobCache* bc = getBlobCacheLocked();
118 bc->set(key.data(), keySize, value, valueSize);
119
120 if (!mSavePending && mDeferredSaveDelay > 0) {
121 mSavePending = true;
122 std::thread deferredSaveThread([this]() {
123 sleep(mDeferredSaveDelay);
124 std::lock_guard<std::mutex> lock(mMutex);
125 ATRACE_NAME("ShaderCache::saveToDisk");
126 if (mInitialized && mBlobCache) {
127 mBlobCache->writeToFile();
128 }
129 mSavePending = false;
130 });
131 deferredSaveThread.detach();
132 }
133}
134
135} /* namespace skiapipeline */
136} /* namespace uirenderer */
137} /* namespace android */