blob: 98ff1d12cca70f7e0074f4446cdea95755ebef1b [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 Northropb5267032023-10-24 10:11:21 -060044constexpr uint32_t kMaxMultifileTotalEntries = 4 * 1024;
Cody Northrop2c9085b2022-12-12 11:35:54 -070045
Jamie Gennisaca51c02011-11-03 17:42:43 -070046namespace android {
Jamie Gennisaca51c02011-11-03 17:42:43 -070047
48#define BC_EXT_STR "EGL_ANDROID_blob_cache"
49
Mathias Agopianb7f9a242017-03-08 22:29:31 -080050// called from android_view_ThreadedRenderer.cpp
51void egl_set_cache_filename(const char* filename) {
52 egl_cache_t::get()->setCacheFilename(filename);
53}
54
Jamie Gennisaca51c02011-11-03 17:42:43 -070055//
Jamie Gennis76601082011-11-06 14:14:33 -080056// Callback functions passed to EGL.
Jamie Gennisaca51c02011-11-03 17:42:43 -070057//
Yiwei Zhang8af03062020-08-12 21:28:15 -070058static void setBlob(const void* key, EGLsizeiANDROID keySize, const void* value,
59 EGLsizeiANDROID valueSize) {
Jamie Gennis76601082011-11-06 14:14:33 -080060 egl_cache_t::get()->setBlob(key, keySize, value, valueSize);
Jamie Gennisaca51c02011-11-03 17:42:43 -070061}
62
Yiwei Zhang8af03062020-08-12 21:28:15 -070063static EGLsizeiANDROID getBlob(const void* key, EGLsizeiANDROID keySize, void* value,
64 EGLsizeiANDROID valueSize) {
Jamie Gennis76601082011-11-06 14:14:33 -080065 return egl_cache_t::get()->getBlob(key, keySize, value, valueSize);
66}
67
68//
69// egl_cache_t definition
70//
Cody Northrop2c9085b2022-12-12 11:35:54 -070071egl_cache_t::egl_cache_t()
Cody Northrop6cca6c22023-02-08 20:23:13 -070072 : mInitialized(false), mMultifileMode(false), mCacheByteLimit(kMaxMonolithicTotalSize) {}
Jamie Gennis76601082011-11-06 14:14:33 -080073
Yiwei Zhang8af03062020-08-12 21:28:15 -070074egl_cache_t::~egl_cache_t() {}
Jamie Gennisaca51c02011-11-03 17:42:43 -070075
Jamie Gennis98c63832011-11-07 17:03:54 -080076egl_cache_t egl_cache_t::sCache;
77
Jamie Gennisaca51c02011-11-03 17:42:43 -070078egl_cache_t* egl_cache_t::get() {
Jamie Gennis98c63832011-11-07 17:03:54 -080079 return &sCache;
Jamie Gennisaca51c02011-11-03 17:42:43 -070080}
81
Yiwei Zhang8af03062020-08-12 21:28:15 -070082void egl_cache_t::initialize(egl_display_t* display) {
Mathias Agopian65421432017-03-08 11:49:05 -080083 std::lock_guard<std::mutex> lock(mMutex);
Mathias Agopianada798b2012-02-13 17:09:30 -080084
85 egl_connection_t* const cnx = &gEGLImpl;
Cody Northrop6cca6c22023-02-08 20:23:13 -070086 if (display && cnx->dso && cnx->major >= 0 && cnx->minor >= 0) {
Mathias Agopianada798b2012-02-13 17:09:30 -080087 const char* exts = display->disp.queryString.extensions;
88 size_t bcExtLen = strlen(BC_EXT_STR);
89 size_t extsLen = strlen(exts);
90 bool equal = !strcmp(BC_EXT_STR, exts);
Yiwei Zhang8af03062020-08-12 21:28:15 -070091 bool atStart = !strncmp(BC_EXT_STR " ", exts, bcExtLen + 1);
92 bool atEnd = (bcExtLen + 1) < extsLen &&
93 !strcmp(" " BC_EXT_STR, exts + extsLen - (bcExtLen + 1));
Mathias Agopian311b4792017-02-28 15:00:49 -080094 bool inMiddle = strstr(exts, " " BC_EXT_STR " ") != nullptr;
Mathias Agopianada798b2012-02-13 17:09:30 -080095 if (equal || atStart || atEnd || inMiddle) {
96 PFNEGLSETBLOBCACHEFUNCSANDROIDPROC eglSetBlobCacheFuncsANDROID;
Yiwei Zhang8af03062020-08-12 21:28:15 -070097 eglSetBlobCacheFuncsANDROID = reinterpret_cast<PFNEGLSETBLOBCACHEFUNCSANDROIDPROC>(
98 cnx->egl.eglGetProcAddress("eglSetBlobCacheFuncsANDROID"));
Yi Kong48a6cd22018-07-18 10:07:09 -070099 if (eglSetBlobCacheFuncsANDROID == nullptr) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800100 ALOGE("EGL_ANDROID_blob_cache advertised, "
Yiwei Zhang8af03062020-08-12 21:28:15 -0700101 "but unable to get eglSetBlobCacheFuncsANDROID");
Mathias Agopianada798b2012-02-13 17:09:30 -0800102 return;
103 }
Jamie Gennisaca51c02011-11-03 17:42:43 -0700104
Yiwei Zhang8af03062020-08-12 21:28:15 -0700105 eglSetBlobCacheFuncsANDROID(display->disp.dpy, android::setBlob, android::getBlob);
Mathias Agopianada798b2012-02-13 17:09:30 -0800106 EGLint err = cnx->egl.eglGetError();
107 if (err != EGL_SUCCESS) {
108 ALOGE("eglSetBlobCacheFuncsANDROID resulted in an error: "
Yiwei Zhang8af03062020-08-12 21:28:15 -0700109 "%#x",
110 err);
Jamie Gennisaca51c02011-11-03 17:42:43 -0700111 }
112 }
113 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800114
Jamie Gennis76601082011-11-06 14:14:33 -0800115 mInitialized = true;
116}
117
118void egl_cache_t::terminate() {
Mathias Agopian65421432017-03-08 11:49:05 -0800119 std::lock_guard<std::mutex> lock(mMutex);
Stan Iliev9e7cd072017-10-09 15:56:10 -0400120 if (mBlobCache) {
121 mBlobCache->writeToFile();
122 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700123 mBlobCache = nullptr;
Cody Northrop6cca6c22023-02-08 20:23:13 -0700124 if (mMultifileBlobCache) {
125 mMultifileBlobCache->finish();
Cody Northrop2c9085b2022-12-12 11:35:54 -0700126 }
Cody Northrop6cca6c22023-02-08 20:23:13 -0700127 mMultifileBlobCache = nullptr;
Cody Northrop2c9085b2022-12-12 11:35:54 -0700128 mInitialized = false;
Jamie Gennis76601082011-11-06 14:14:33 -0800129}
130
Yiwei Zhang8af03062020-08-12 21:28:15 -0700131void egl_cache_t::setBlob(const void* key, EGLsizeiANDROID keySize, const void* value,
132 EGLsizeiANDROID valueSize) {
Mathias Agopian65421432017-03-08 11:49:05 -0800133 std::lock_guard<std::mutex> lock(mMutex);
Jamie Gennis76601082011-11-06 14:14:33 -0800134
135 if (keySize < 0 || valueSize < 0) {
Steve Block32397c12012-01-05 23:22:43 +0000136 ALOGW("EGL_ANDROID_blob_cache set: negative sizes are not allowed");
Jamie Gennis76601082011-11-06 14:14:33 -0800137 return;
138 }
139
Cody Northrop84227272023-02-27 15:59:27 -0700140 updateMode();
141
Jamie Gennis76601082011-11-06 14:14:33 -0800142 if (mInitialized) {
Cody Northrop2c9085b2022-12-12 11:35:54 -0700143 if (mMultifileMode) {
Cody Northrop6cca6c22023-02-08 20:23:13 -0700144 MultifileBlobCache* mbc = getMultifileBlobCacheLocked();
145 mbc->set(key, keySize, value, valueSize);
Cody Northrop2c9085b2022-12-12 11:35:54 -0700146 } else {
147 BlobCache* bc = getBlobCacheLocked();
148 bc->set(key, keySize, value, valueSize);
149
150 if (!mSavePending) {
151 mSavePending = true;
152 std::thread deferredSaveThread([this]() {
Cody Northrop6cca6c22023-02-08 20:23:13 -0700153 sleep(kDeferredMonolithicSaveDelay);
Cody Northrop2c9085b2022-12-12 11:35:54 -0700154 std::lock_guard<std::mutex> lock(mMutex);
155 if (mInitialized && mBlobCache) {
156 mBlobCache->writeToFile();
157 }
158 mSavePending = false;
159 });
160 deferredSaveThread.detach();
161 }
Jamie Gennis99c3d702011-11-08 17:59:36 -0800162 }
Jamie Gennis76601082011-11-06 14:14:33 -0800163 }
164}
165
Yiwei Zhang8af03062020-08-12 21:28:15 -0700166EGLsizeiANDROID egl_cache_t::getBlob(const void* key, EGLsizeiANDROID keySize, void* value,
167 EGLsizeiANDROID valueSize) {
Mathias Agopian65421432017-03-08 11:49:05 -0800168 std::lock_guard<std::mutex> lock(mMutex);
Jamie Gennis76601082011-11-06 14:14:33 -0800169
170 if (keySize < 0 || valueSize < 0) {
Cody Northrop2c9085b2022-12-12 11:35:54 -0700171 ALOGW("EGL_ANDROID_blob_cache get: negative sizes are not allowed");
Jamie Gennis76601082011-11-06 14:14:33 -0800172 return 0;
173 }
174
Cody Northrop84227272023-02-27 15:59:27 -0700175 updateMode();
176
Jamie Gennis76601082011-11-06 14:14:33 -0800177 if (mInitialized) {
Cody Northrop2c9085b2022-12-12 11:35:54 -0700178 if (mMultifileMode) {
Cody Northrop6cca6c22023-02-08 20:23:13 -0700179 MultifileBlobCache* mbc = getMultifileBlobCacheLocked();
180 return mbc->get(key, keySize, value, valueSize);
Cody Northrop2c9085b2022-12-12 11:35:54 -0700181 } else {
182 BlobCache* bc = getBlobCacheLocked();
183 return bc->get(key, keySize, value, valueSize);
184 }
Jamie Gennis76601082011-11-06 14:14:33 -0800185 }
Cody Northrop6cca6c22023-02-08 20:23:13 -0700186
Jamie Gennis76601082011-11-06 14:14:33 -0800187 return 0;
188}
189
Cody Northrop6cca6c22023-02-08 20:23:13 -0700190void egl_cache_t::setCacheMode(EGLCacheMode cacheMode) {
191 mMultifileMode = (cacheMode == EGLCacheMode::Multifile);
192}
193
Jamie Gennis98c63832011-11-07 17:03:54 -0800194void egl_cache_t::setCacheFilename(const char* filename) {
Mathias Agopian65421432017-03-08 11:49:05 -0800195 std::lock_guard<std::mutex> lock(mMutex);
Jamie Gennis98c63832011-11-07 17:03:54 -0800196 mFilename = filename;
197}
198
Cody Northrop2c9085b2022-12-12 11:35:54 -0700199void egl_cache_t::setCacheLimit(int64_t cacheByteLimit) {
200 std::lock_guard<std::mutex> lock(mMutex);
201
202 if (!mMultifileMode) {
203 // If we're not in multifile mode, ensure the cache limit is only being lowered,
204 // not increasing above the hard coded platform limit
Cody Northrop6cca6c22023-02-08 20:23:13 -0700205 if (cacheByteLimit > kMaxMonolithicTotalSize) {
Cody Northrop2c9085b2022-12-12 11:35:54 -0700206 return;
207 }
208 }
209
210 mCacheByteLimit = cacheByteLimit;
211}
212
213size_t egl_cache_t::getCacheSize() {
214 std::lock_guard<std::mutex> lock(mMutex);
Cody Northrop6cca6c22023-02-08 20:23:13 -0700215 if (mMultifileBlobCache) {
216 return mMultifileBlobCache->getTotalSize();
Cody Northrop2c9085b2022-12-12 11:35:54 -0700217 }
218 if (mBlobCache) {
219 return mBlobCache->getSize();
220 }
221 return 0;
222}
223
Cody Northrop84227272023-02-27 15:59:27 -0700224void egl_cache_t::updateMode() {
225 // We don't set the mode in the constructor because these checks have
226 // a non-trivial cost, and not all processes that instantiate egl_cache_t
227 // will use it.
228
229 // If we've already set the mode, skip these checks
230 static bool checked = false;
231 if (checked) {
232 return;
233 }
234 checked = true;
235
236 // Check the device config to decide whether multifile should be used
237 if (base::GetBoolProperty("ro.egl.blobcache.multifile", false)) {
238 mMultifileMode = true;
239 ALOGV("Using multifile EGL blobcache");
240 }
241
242 // Allow forcing the mode for debug purposes
243 std::string mode = base::GetProperty("debug.egl.blobcache.multifile", "");
244 if (mode == "true") {
245 ALOGV("Forcing multifile cache due to debug.egl.blobcache.multifile == %s", mode.c_str());
246 mMultifileMode = true;
247 } else if (mode == "false") {
248 ALOGV("Forcing monolithic cache due to debug.egl.blobcache.multifile == %s", mode.c_str());
249 mMultifileMode = false;
250 }
251
252 if (mMultifileMode) {
253 mCacheByteLimit = static_cast<size_t>(
254 base::GetUintProperty<uint32_t>("ro.egl.blobcache.multifile_limit",
Cody Northrop5dbcfa72023-03-24 15:34:09 -0600255 kMaxMultifileTotalSize));
Cody Northrop84227272023-02-27 15:59:27 -0700256
257 // Check for a debug value
258 int debugCacheSize = base::GetIntProperty("debug.egl.blobcache.multifile_limit", -1);
259 if (debugCacheSize >= 0) {
260 ALOGV("Overriding cache limit %zu with %i from debug.egl.blobcache.multifile_limit",
261 mCacheByteLimit, debugCacheSize);
262 mCacheByteLimit = debugCacheSize;
263 }
264
265 ALOGV("Using multifile EGL blobcache limit of %zu bytes", mCacheByteLimit);
266 }
267}
268
Mathias Agopianb7f9a242017-03-08 22:29:31 -0800269BlobCache* egl_cache_t::getBlobCacheLocked() {
270 if (mBlobCache == nullptr) {
Cody Northrop6cca6c22023-02-08 20:23:13 -0700271 mBlobCache.reset(new FileBlobCache(kMaxMonolithicKeySize, kMaxMonolithicValueSize,
272 mCacheByteLimit, mFilename));
Jamie Gennis76601082011-11-06 14:14:33 -0800273 }
Mathias Agopianb7f9a242017-03-08 22:29:31 -0800274 return mBlobCache.get();
Jamie Gennis76601082011-11-06 14:14:33 -0800275}
276
Cody Northrop6cca6c22023-02-08 20:23:13 -0700277MultifileBlobCache* egl_cache_t::getMultifileBlobCacheLocked() {
278 if (mMultifileBlobCache == nullptr) {
Cody Northrop5dbcfa72023-03-24 15:34:09 -0600279 mMultifileBlobCache.reset(new MultifileBlobCache(kMaxMultifileKeySize,
280 kMaxMultifileValueSize, mCacheByteLimit,
Cody Northropb5267032023-10-24 10:11:21 -0600281 kMaxMultifileTotalEntries, mFilename));
Cody Northrop6cca6c22023-02-08 20:23:13 -0700282 }
283 return mMultifileBlobCache.get();
284}
285
Jamie Gennisaca51c02011-11-03 17:42:43 -0700286}; // namespace android