blob: 1b68344cb1d2f62af367bcd7ebd8fdaeec862662 [file] [log] [blame]
Jamie Gennisaca51c02011-11-03 17:42:43 -07001/*
2 ** Copyright 2011, 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
Cody Northrop6cca6c22023-02-08 20:23:13 -070017// #define LOG_NDEBUG 0
18
Mathias Agopian311b4792017-02-28 15:00:49 -080019#include "egl_cache.h"
20
Cody Northrop2c9085b2022-12-12 11:35:54 -070021#include <android-base/properties.h>
22#include <inttypes.h>
Yiwei Zhang8af03062020-08-12 21:28:15 -070023#include <log/log.h>
Mathias Agopianb7f9a242017-03-08 22:29:31 -080024#include <private/EGL/cache.h>
Jiyong Parka243e5d2017-06-21 12:26:51 +090025#include <unistd.h>
Mathias Agopian311b4792017-02-28 15:00:49 -080026
Mathias Agopian65421432017-03-08 11:49:05 -080027#include <thread>
28
Yiwei Zhang8af03062020-08-12 21:28:15 -070029#include "../egl_impl.h"
30#include "egl_display.h"
Jamie Gennis98c63832011-11-07 17:03:54 -080031
Cody Northrop2c9085b2022-12-12 11:35:54 -070032// Monolithic cache size limits.
Cody Northrop6cca6c22023-02-08 20:23:13 -070033static const size_t kMaxMonolithicKeySize = 12 * 1024;
34static const size_t kMaxMonolithicValueSize = 64 * 1024;
35static const size_t kMaxMonolithicTotalSize = 2 * 1024 * 1024;
Jamie Gennis76601082011-11-06 14:14:33 -080036
Cody Northrop2c9085b2022-12-12 11:35:54 -070037// The time in seconds to wait before saving newly inserted monolithic cache entries.
Cody Northrop6cca6c22023-02-08 20:23:13 -070038static const unsigned int kDeferredMonolithicSaveDelay = 4;
Jamie Gennis99c3d702011-11-08 17:59:36 -080039
Cody Northrop6cca6c22023-02-08 20:23:13 -070040// Multifile cache size limits
Cody Northrop5dbcfa72023-03-24 15:34:09 -060041constexpr uint32_t kMaxMultifileKeySize = 1 * 1024 * 1024;
42constexpr uint32_t kMaxMultifileValueSize = 8 * 1024 * 1024;
43constexpr uint32_t kMaxMultifileTotalSize = 32 * 1024 * 1024;
Cody Northrop2c9085b2022-12-12 11:35:54 -070044
Jamie Gennisaca51c02011-11-03 17:42:43 -070045namespace android {
Jamie Gennisaca51c02011-11-03 17:42:43 -070046
47#define BC_EXT_STR "EGL_ANDROID_blob_cache"
48
Mathias Agopianb7f9a242017-03-08 22:29:31 -080049// called from android_view_ThreadedRenderer.cpp
50void egl_set_cache_filename(const char* filename) {
51 egl_cache_t::get()->setCacheFilename(filename);
52}
53
Jamie Gennisaca51c02011-11-03 17:42:43 -070054//
Jamie Gennis76601082011-11-06 14:14:33 -080055// Callback functions passed to EGL.
Jamie Gennisaca51c02011-11-03 17:42:43 -070056//
Yiwei Zhang8af03062020-08-12 21:28:15 -070057static void setBlob(const void* key, EGLsizeiANDROID keySize, const void* value,
58 EGLsizeiANDROID valueSize) {
Jamie Gennis76601082011-11-06 14:14:33 -080059 egl_cache_t::get()->setBlob(key, keySize, value, valueSize);
Jamie Gennisaca51c02011-11-03 17:42:43 -070060}
61
Yiwei Zhang8af03062020-08-12 21:28:15 -070062static EGLsizeiANDROID getBlob(const void* key, EGLsizeiANDROID keySize, void* value,
63 EGLsizeiANDROID valueSize) {
Jamie Gennis76601082011-11-06 14:14:33 -080064 return egl_cache_t::get()->getBlob(key, keySize, value, valueSize);
65}
66
67//
68// egl_cache_t definition
69//
Cody Northrop2c9085b2022-12-12 11:35:54 -070070egl_cache_t::egl_cache_t()
Cody Northrop6cca6c22023-02-08 20:23:13 -070071 : mInitialized(false), mMultifileMode(false), mCacheByteLimit(kMaxMonolithicTotalSize) {}
Jamie Gennis76601082011-11-06 14:14:33 -080072
Yiwei Zhang8af03062020-08-12 21:28:15 -070073egl_cache_t::~egl_cache_t() {}
Jamie Gennisaca51c02011-11-03 17:42:43 -070074
Jamie Gennis98c63832011-11-07 17:03:54 -080075egl_cache_t egl_cache_t::sCache;
76
Jamie Gennisaca51c02011-11-03 17:42:43 -070077egl_cache_t* egl_cache_t::get() {
Jamie Gennis98c63832011-11-07 17:03:54 -080078 return &sCache;
Jamie Gennisaca51c02011-11-03 17:42:43 -070079}
80
Yiwei Zhang8af03062020-08-12 21:28:15 -070081void egl_cache_t::initialize(egl_display_t* display) {
Mathias Agopian65421432017-03-08 11:49:05 -080082 std::lock_guard<std::mutex> lock(mMutex);
Mathias Agopianada798b2012-02-13 17:09:30 -080083
84 egl_connection_t* const cnx = &gEGLImpl;
Cody Northrop6cca6c22023-02-08 20:23:13 -070085 if (display && cnx->dso && cnx->major >= 0 && cnx->minor >= 0) {
Mathias Agopianada798b2012-02-13 17:09:30 -080086 const char* exts = display->disp.queryString.extensions;
87 size_t bcExtLen = strlen(BC_EXT_STR);
88 size_t extsLen = strlen(exts);
89 bool equal = !strcmp(BC_EXT_STR, exts);
Yiwei Zhang8af03062020-08-12 21:28:15 -070090 bool atStart = !strncmp(BC_EXT_STR " ", exts, bcExtLen + 1);
91 bool atEnd = (bcExtLen + 1) < extsLen &&
92 !strcmp(" " BC_EXT_STR, exts + extsLen - (bcExtLen + 1));
Mathias Agopian311b4792017-02-28 15:00:49 -080093 bool inMiddle = strstr(exts, " " BC_EXT_STR " ") != nullptr;
Mathias Agopianada798b2012-02-13 17:09:30 -080094 if (equal || atStart || atEnd || inMiddle) {
95 PFNEGLSETBLOBCACHEFUNCSANDROIDPROC eglSetBlobCacheFuncsANDROID;
Yiwei Zhang8af03062020-08-12 21:28:15 -070096 eglSetBlobCacheFuncsANDROID = reinterpret_cast<PFNEGLSETBLOBCACHEFUNCSANDROIDPROC>(
97 cnx->egl.eglGetProcAddress("eglSetBlobCacheFuncsANDROID"));
Yi Kong48a6cd22018-07-18 10:07:09 -070098 if (eglSetBlobCacheFuncsANDROID == nullptr) {
Mathias Agopianada798b2012-02-13 17:09:30 -080099 ALOGE("EGL_ANDROID_blob_cache advertised, "
Yiwei Zhang8af03062020-08-12 21:28:15 -0700100 "but unable to get eglSetBlobCacheFuncsANDROID");
Mathias Agopianada798b2012-02-13 17:09:30 -0800101 return;
102 }
Jamie Gennisaca51c02011-11-03 17:42:43 -0700103
Yiwei Zhang8af03062020-08-12 21:28:15 -0700104 eglSetBlobCacheFuncsANDROID(display->disp.dpy, android::setBlob, android::getBlob);
Mathias Agopianada798b2012-02-13 17:09:30 -0800105 EGLint err = cnx->egl.eglGetError();
106 if (err != EGL_SUCCESS) {
107 ALOGE("eglSetBlobCacheFuncsANDROID resulted in an error: "
Yiwei Zhang8af03062020-08-12 21:28:15 -0700108 "%#x",
109 err);
Jamie Gennisaca51c02011-11-03 17:42:43 -0700110 }
111 }
112 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800113
Jamie Gennis76601082011-11-06 14:14:33 -0800114 mInitialized = true;
115}
116
117void egl_cache_t::terminate() {
Mathias Agopian65421432017-03-08 11:49:05 -0800118 std::lock_guard<std::mutex> lock(mMutex);
Stan Iliev9e7cd072017-10-09 15:56:10 -0400119 if (mBlobCache) {
120 mBlobCache->writeToFile();
121 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700122 mBlobCache = nullptr;
Cody Northrop6cca6c22023-02-08 20:23:13 -0700123 if (mMultifileBlobCache) {
124 mMultifileBlobCache->finish();
Cody Northrop2c9085b2022-12-12 11:35:54 -0700125 }
Cody Northrop6cca6c22023-02-08 20:23:13 -0700126 mMultifileBlobCache = nullptr;
Cody Northrop2c9085b2022-12-12 11:35:54 -0700127 mInitialized = false;
Jamie Gennis76601082011-11-06 14:14:33 -0800128}
129
Yiwei Zhang8af03062020-08-12 21:28:15 -0700130void egl_cache_t::setBlob(const void* key, EGLsizeiANDROID keySize, const void* value,
131 EGLsizeiANDROID valueSize) {
Mathias Agopian65421432017-03-08 11:49:05 -0800132 std::lock_guard<std::mutex> lock(mMutex);
Jamie Gennis76601082011-11-06 14:14:33 -0800133
134 if (keySize < 0 || valueSize < 0) {
Steve Block32397c12012-01-05 23:22:43 +0000135 ALOGW("EGL_ANDROID_blob_cache set: negative sizes are not allowed");
Jamie Gennis76601082011-11-06 14:14:33 -0800136 return;
137 }
138
Cody Northrop84227272023-02-27 15:59:27 -0700139 updateMode();
140
Jamie Gennis76601082011-11-06 14:14:33 -0800141 if (mInitialized) {
Cody Northrop2c9085b2022-12-12 11:35:54 -0700142 if (mMultifileMode) {
Cody Northrop6cca6c22023-02-08 20:23:13 -0700143 MultifileBlobCache* mbc = getMultifileBlobCacheLocked();
144 mbc->set(key, keySize, value, valueSize);
Cody Northrop2c9085b2022-12-12 11:35:54 -0700145 } else {
146 BlobCache* bc = getBlobCacheLocked();
147 bc->set(key, keySize, value, valueSize);
148
149 if (!mSavePending) {
150 mSavePending = true;
151 std::thread deferredSaveThread([this]() {
Cody Northrop6cca6c22023-02-08 20:23:13 -0700152 sleep(kDeferredMonolithicSaveDelay);
Cody Northrop2c9085b2022-12-12 11:35:54 -0700153 std::lock_guard<std::mutex> lock(mMutex);
154 if (mInitialized && mBlobCache) {
155 mBlobCache->writeToFile();
156 }
157 mSavePending = false;
158 });
159 deferredSaveThread.detach();
160 }
Jamie Gennis99c3d702011-11-08 17:59:36 -0800161 }
Jamie Gennis76601082011-11-06 14:14:33 -0800162 }
163}
164
Yiwei Zhang8af03062020-08-12 21:28:15 -0700165EGLsizeiANDROID egl_cache_t::getBlob(const void* key, EGLsizeiANDROID keySize, void* value,
166 EGLsizeiANDROID valueSize) {
Mathias Agopian65421432017-03-08 11:49:05 -0800167 std::lock_guard<std::mutex> lock(mMutex);
Jamie Gennis76601082011-11-06 14:14:33 -0800168
169 if (keySize < 0 || valueSize < 0) {
Cody Northrop2c9085b2022-12-12 11:35:54 -0700170 ALOGW("EGL_ANDROID_blob_cache get: negative sizes are not allowed");
Jamie Gennis76601082011-11-06 14:14:33 -0800171 return 0;
172 }
173
Cody Northrop84227272023-02-27 15:59:27 -0700174 updateMode();
175
Jamie Gennis76601082011-11-06 14:14:33 -0800176 if (mInitialized) {
Cody Northrop2c9085b2022-12-12 11:35:54 -0700177 if (mMultifileMode) {
Cody Northrop6cca6c22023-02-08 20:23:13 -0700178 MultifileBlobCache* mbc = getMultifileBlobCacheLocked();
179 return mbc->get(key, keySize, value, valueSize);
Cody Northrop2c9085b2022-12-12 11:35:54 -0700180 } else {
181 BlobCache* bc = getBlobCacheLocked();
182 return bc->get(key, keySize, value, valueSize);
183 }
Jamie Gennis76601082011-11-06 14:14:33 -0800184 }
Cody Northrop6cca6c22023-02-08 20:23:13 -0700185
Jamie Gennis76601082011-11-06 14:14:33 -0800186 return 0;
187}
188
Cody Northrop6cca6c22023-02-08 20:23:13 -0700189void egl_cache_t::setCacheMode(EGLCacheMode cacheMode) {
190 mMultifileMode = (cacheMode == EGLCacheMode::Multifile);
191}
192
Jamie Gennis98c63832011-11-07 17:03:54 -0800193void egl_cache_t::setCacheFilename(const char* filename) {
Mathias Agopian65421432017-03-08 11:49:05 -0800194 std::lock_guard<std::mutex> lock(mMutex);
Jamie Gennis98c63832011-11-07 17:03:54 -0800195 mFilename = filename;
196}
197
Cody Northrop2c9085b2022-12-12 11:35:54 -0700198void egl_cache_t::setCacheLimit(int64_t cacheByteLimit) {
199 std::lock_guard<std::mutex> lock(mMutex);
200
201 if (!mMultifileMode) {
202 // If we're not in multifile mode, ensure the cache limit is only being lowered,
203 // not increasing above the hard coded platform limit
Cody Northrop6cca6c22023-02-08 20:23:13 -0700204 if (cacheByteLimit > kMaxMonolithicTotalSize) {
Cody Northrop2c9085b2022-12-12 11:35:54 -0700205 return;
206 }
207 }
208
209 mCacheByteLimit = cacheByteLimit;
210}
211
212size_t egl_cache_t::getCacheSize() {
213 std::lock_guard<std::mutex> lock(mMutex);
Cody Northrop6cca6c22023-02-08 20:23:13 -0700214 if (mMultifileBlobCache) {
215 return mMultifileBlobCache->getTotalSize();
Cody Northrop2c9085b2022-12-12 11:35:54 -0700216 }
217 if (mBlobCache) {
218 return mBlobCache->getSize();
219 }
220 return 0;
221}
222
Cody Northrop84227272023-02-27 15:59:27 -0700223void egl_cache_t::updateMode() {
224 // We don't set the mode in the constructor because these checks have
225 // a non-trivial cost, and not all processes that instantiate egl_cache_t
226 // will use it.
227
228 // If we've already set the mode, skip these checks
229 static bool checked = false;
230 if (checked) {
231 return;
232 }
233 checked = true;
234
235 // Check the device config to decide whether multifile should be used
236 if (base::GetBoolProperty("ro.egl.blobcache.multifile", false)) {
237 mMultifileMode = true;
238 ALOGV("Using multifile EGL blobcache");
239 }
240
241 // Allow forcing the mode for debug purposes
242 std::string mode = base::GetProperty("debug.egl.blobcache.multifile", "");
243 if (mode == "true") {
244 ALOGV("Forcing multifile cache due to debug.egl.blobcache.multifile == %s", mode.c_str());
245 mMultifileMode = true;
246 } else if (mode == "false") {
247 ALOGV("Forcing monolithic cache due to debug.egl.blobcache.multifile == %s", mode.c_str());
248 mMultifileMode = false;
249 }
250
251 if (mMultifileMode) {
252 mCacheByteLimit = static_cast<size_t>(
253 base::GetUintProperty<uint32_t>("ro.egl.blobcache.multifile_limit",
Cody Northrop5dbcfa72023-03-24 15:34:09 -0600254 kMaxMultifileTotalSize));
Cody Northrop84227272023-02-27 15:59:27 -0700255
256 // Check for a debug value
257 int debugCacheSize = base::GetIntProperty("debug.egl.blobcache.multifile_limit", -1);
258 if (debugCacheSize >= 0) {
259 ALOGV("Overriding cache limit %zu with %i from debug.egl.blobcache.multifile_limit",
260 mCacheByteLimit, debugCacheSize);
261 mCacheByteLimit = debugCacheSize;
262 }
263
264 ALOGV("Using multifile EGL blobcache limit of %zu bytes", mCacheByteLimit);
265 }
266}
267
Mathias Agopianb7f9a242017-03-08 22:29:31 -0800268BlobCache* egl_cache_t::getBlobCacheLocked() {
269 if (mBlobCache == nullptr) {
Cody Northrop6cca6c22023-02-08 20:23:13 -0700270 mBlobCache.reset(new FileBlobCache(kMaxMonolithicKeySize, kMaxMonolithicValueSize,
271 mCacheByteLimit, mFilename));
Jamie Gennis76601082011-11-06 14:14:33 -0800272 }
Mathias Agopianb7f9a242017-03-08 22:29:31 -0800273 return mBlobCache.get();
Jamie Gennis76601082011-11-06 14:14:33 -0800274}
275
Cody Northrop6cca6c22023-02-08 20:23:13 -0700276MultifileBlobCache* egl_cache_t::getMultifileBlobCacheLocked() {
277 if (mMultifileBlobCache == nullptr) {
Cody Northrop5dbcfa72023-03-24 15:34:09 -0600278 mMultifileBlobCache.reset(new MultifileBlobCache(kMaxMultifileKeySize,
279 kMaxMultifileValueSize, mCacheByteLimit,
280 mFilename));
Cody Northrop6cca6c22023-02-08 20:23:13 -0700281 }
282 return mMultifileBlobCache.get();
283}
284
Jamie Gennisaca51c02011-11-03 17:42:43 -0700285}; // namespace android