Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
Mathias Agopian | 311b479 | 2017-02-28 15:00:49 -0800 | [diff] [blame] | 17 | #include "egl_cache.h" |
| 18 | |
Cody Northrop | 2c9085b | 2022-12-12 11:35:54 -0700 | [diff] [blame] | 19 | #include <android-base/properties.h> |
| 20 | #include <inttypes.h> |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 21 | #include <log/log.h> |
Mathias Agopian | b7f9a24 | 2017-03-08 22:29:31 -0800 | [diff] [blame] | 22 | #include <private/EGL/cache.h> |
Jiyong Park | a243e5d | 2017-06-21 12:26:51 +0900 | [diff] [blame] | 23 | #include <unistd.h> |
Mathias Agopian | 311b479 | 2017-02-28 15:00:49 -0800 | [diff] [blame] | 24 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 25 | #include <thread> |
| 26 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 27 | #include "../egl_impl.h" |
Cody Northrop | 4373541 | 2023-02-08 16:19:05 +0000 | [diff] [blame] | 28 | #include "egl_cache_multifile.h" |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 29 | #include "egl_display.h" |
Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 30 | |
Cody Northrop | 2c9085b | 2022-12-12 11:35:54 -0700 | [diff] [blame] | 31 | // Monolithic cache size limits. |
Cody Northrop | 4373541 | 2023-02-08 16:19:05 +0000 | [diff] [blame] | 32 | static const size_t maxKeySize = 12 * 1024; |
| 33 | static const size_t maxValueSize = 64 * 1024; |
| 34 | static const size_t maxTotalSize = 32 * 1024 * 1024; |
Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 35 | |
Cody Northrop | 2c9085b | 2022-12-12 11:35:54 -0700 | [diff] [blame] | 36 | // The time in seconds to wait before saving newly inserted monolithic cache entries. |
Cody Northrop | 4373541 | 2023-02-08 16:19:05 +0000 | [diff] [blame] | 37 | static const unsigned int deferredSaveDelay = 4; |
Jamie Gennis | 99c3d70 | 2011-11-08 17:59:36 -0800 | [diff] [blame] | 38 | |
Cody Northrop | 4373541 | 2023-02-08 16:19:05 +0000 | [diff] [blame] | 39 | // Multifile cache size limit |
| 40 | constexpr size_t kMultifileCacheByteLimit = 64 * 1024 * 1024; |
| 41 | |
| 42 | // Delay before cleaning up multifile cache entries |
| 43 | static const unsigned int deferredMultifileCleanupDelaySeconds = 1; |
Cody Northrop | 2c9085b | 2022-12-12 11:35:54 -0700 | [diff] [blame] | 44 | |
Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 45 | namespace android { |
Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 46 | |
| 47 | #define BC_EXT_STR "EGL_ANDROID_blob_cache" |
| 48 | |
Mathias Agopian | b7f9a24 | 2017-03-08 22:29:31 -0800 | [diff] [blame] | 49 | // called from android_view_ThreadedRenderer.cpp |
| 50 | void egl_set_cache_filename(const char* filename) { |
| 51 | egl_cache_t::get()->setCacheFilename(filename); |
| 52 | } |
| 53 | |
Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 54 | // |
Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 55 | // Callback functions passed to EGL. |
Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 56 | // |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 57 | static void setBlob(const void* key, EGLsizeiANDROID keySize, const void* value, |
| 58 | EGLsizeiANDROID valueSize) { |
Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 59 | egl_cache_t::get()->setBlob(key, keySize, value, valueSize); |
Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 62 | static EGLsizeiANDROID getBlob(const void* key, EGLsizeiANDROID keySize, void* value, |
| 63 | EGLsizeiANDROID valueSize) { |
Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 64 | return egl_cache_t::get()->getBlob(key, keySize, value, valueSize); |
| 65 | } |
| 66 | |
| 67 | // |
| 68 | // egl_cache_t definition |
| 69 | // |
Cody Northrop | 2c9085b | 2022-12-12 11:35:54 -0700 | [diff] [blame] | 70 | egl_cache_t::egl_cache_t() |
Cody Northrop | 4373541 | 2023-02-08 16:19:05 +0000 | [diff] [blame] | 71 | : mInitialized(false), |
| 72 | mMultifileMode(false), |
| 73 | mCacheByteLimit(maxTotalSize), |
| 74 | mMultifileCleanupPending(false) {} |
Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 75 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 76 | egl_cache_t::~egl_cache_t() {} |
Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 77 | |
Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 78 | egl_cache_t egl_cache_t::sCache; |
| 79 | |
Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 80 | egl_cache_t* egl_cache_t::get() { |
Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 81 | return &sCache; |
Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 84 | void egl_cache_t::initialize(egl_display_t* display) { |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 85 | std::lock_guard<std::mutex> lock(mMutex); |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 86 | |
| 87 | egl_connection_t* const cnx = &gEGLImpl; |
Cody Northrop | 4373541 | 2023-02-08 16:19:05 +0000 | [diff] [blame] | 88 | if (cnx->dso && cnx->major >= 0 && cnx->minor >= 0) { |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 89 | const char* exts = display->disp.queryString.extensions; |
| 90 | size_t bcExtLen = strlen(BC_EXT_STR); |
| 91 | size_t extsLen = strlen(exts); |
| 92 | bool equal = !strcmp(BC_EXT_STR, exts); |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 93 | bool atStart = !strncmp(BC_EXT_STR " ", exts, bcExtLen + 1); |
| 94 | bool atEnd = (bcExtLen + 1) < extsLen && |
| 95 | !strcmp(" " BC_EXT_STR, exts + extsLen - (bcExtLen + 1)); |
Mathias Agopian | 311b479 | 2017-02-28 15:00:49 -0800 | [diff] [blame] | 96 | bool inMiddle = strstr(exts, " " BC_EXT_STR " ") != nullptr; |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 97 | if (equal || atStart || atEnd || inMiddle) { |
| 98 | PFNEGLSETBLOBCACHEFUNCSANDROIDPROC eglSetBlobCacheFuncsANDROID; |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 99 | eglSetBlobCacheFuncsANDROID = reinterpret_cast<PFNEGLSETBLOBCACHEFUNCSANDROIDPROC>( |
| 100 | cnx->egl.eglGetProcAddress("eglSetBlobCacheFuncsANDROID")); |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 101 | if (eglSetBlobCacheFuncsANDROID == nullptr) { |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 102 | ALOGE("EGL_ANDROID_blob_cache advertised, " |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 103 | "but unable to get eglSetBlobCacheFuncsANDROID"); |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 104 | return; |
| 105 | } |
Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 106 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 107 | eglSetBlobCacheFuncsANDROID(display->disp.dpy, android::setBlob, android::getBlob); |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 108 | EGLint err = cnx->egl.eglGetError(); |
| 109 | if (err != EGL_SUCCESS) { |
| 110 | ALOGE("eglSetBlobCacheFuncsANDROID resulted in an error: " |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 111 | "%#x", |
| 112 | err); |
Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | } |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 116 | |
Cody Northrop | 4373541 | 2023-02-08 16:19:05 +0000 | [diff] [blame] | 117 | // Allow forcing monolithic cache for debug purposes |
| 118 | if (base::GetProperty("debug.egl.blobcache.multifilemode", "") == "false") { |
| 119 | ALOGD("Forcing monolithic cache due to debug.egl.blobcache.multifilemode == \"false\""); |
Cody Northrop | 2c9085b | 2022-12-12 11:35:54 -0700 | [diff] [blame] | 120 | mMultifileMode = false; |
| 121 | } |
| 122 | |
| 123 | if (mMultifileMode) { |
Cody Northrop | 4373541 | 2023-02-08 16:19:05 +0000 | [diff] [blame] | 124 | mCacheByteLimit = kMultifileCacheByteLimit; |
Cody Northrop | 2c9085b | 2022-12-12 11:35:54 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 127 | mInitialized = true; |
| 128 | } |
| 129 | |
| 130 | void egl_cache_t::terminate() { |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 131 | std::lock_guard<std::mutex> lock(mMutex); |
Stan Iliev | 9e7cd07 | 2017-10-09 15:56:10 -0400 | [diff] [blame] | 132 | if (mBlobCache) { |
| 133 | mBlobCache->writeToFile(); |
| 134 | } |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 135 | mBlobCache = nullptr; |
Cody Northrop | 4373541 | 2023-02-08 16:19:05 +0000 | [diff] [blame] | 136 | if (mMultifileMode) { |
| 137 | checkMultifileCacheSize(mCacheByteLimit); |
Cody Northrop | 2c9085b | 2022-12-12 11:35:54 -0700 | [diff] [blame] | 138 | } |
Cody Northrop | 4373541 | 2023-02-08 16:19:05 +0000 | [diff] [blame] | 139 | mMultifileMode = false; |
Cody Northrop | 2c9085b | 2022-12-12 11:35:54 -0700 | [diff] [blame] | 140 | mInitialized = false; |
Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 143 | void egl_cache_t::setBlob(const void* key, EGLsizeiANDROID keySize, const void* value, |
| 144 | EGLsizeiANDROID valueSize) { |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 145 | std::lock_guard<std::mutex> lock(mMutex); |
Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 146 | |
| 147 | if (keySize < 0 || valueSize < 0) { |
Steve Block | 32397c1 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 148 | ALOGW("EGL_ANDROID_blob_cache set: negative sizes are not allowed"); |
Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 149 | return; |
| 150 | } |
| 151 | |
| 152 | if (mInitialized) { |
Cody Northrop | 2c9085b | 2022-12-12 11:35:54 -0700 | [diff] [blame] | 153 | if (mMultifileMode) { |
Cody Northrop | 4373541 | 2023-02-08 16:19:05 +0000 | [diff] [blame] | 154 | setBlobMultifile(key, keySize, value, valueSize, mFilename); |
| 155 | |
| 156 | if (!mMultifileCleanupPending) { |
| 157 | mMultifileCleanupPending = true; |
| 158 | // Kick off a thread to cull cache files below limit |
| 159 | std::thread deferredMultifileCleanupThread([this]() { |
| 160 | sleep(deferredMultifileCleanupDelaySeconds); |
| 161 | std::lock_guard<std::mutex> lock(mMutex); |
| 162 | // Check the size of cache and remove entries to stay under limit |
| 163 | checkMultifileCacheSize(mCacheByteLimit); |
| 164 | mMultifileCleanupPending = false; |
| 165 | }); |
| 166 | deferredMultifileCleanupThread.detach(); |
| 167 | } |
Cody Northrop | 2c9085b | 2022-12-12 11:35:54 -0700 | [diff] [blame] | 168 | } else { |
| 169 | BlobCache* bc = getBlobCacheLocked(); |
| 170 | bc->set(key, keySize, value, valueSize); |
| 171 | |
| 172 | if (!mSavePending) { |
| 173 | mSavePending = true; |
| 174 | std::thread deferredSaveThread([this]() { |
Cody Northrop | 4373541 | 2023-02-08 16:19:05 +0000 | [diff] [blame] | 175 | sleep(deferredSaveDelay); |
Cody Northrop | 2c9085b | 2022-12-12 11:35:54 -0700 | [diff] [blame] | 176 | std::lock_guard<std::mutex> lock(mMutex); |
| 177 | if (mInitialized && mBlobCache) { |
| 178 | mBlobCache->writeToFile(); |
| 179 | } |
| 180 | mSavePending = false; |
| 181 | }); |
| 182 | deferredSaveThread.detach(); |
| 183 | } |
Jamie Gennis | 99c3d70 | 2011-11-08 17:59:36 -0800 | [diff] [blame] | 184 | } |
Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame] | 188 | EGLsizeiANDROID egl_cache_t::getBlob(const void* key, EGLsizeiANDROID keySize, void* value, |
| 189 | EGLsizeiANDROID valueSize) { |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 190 | std::lock_guard<std::mutex> lock(mMutex); |
Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 191 | |
| 192 | if (keySize < 0 || valueSize < 0) { |
Cody Northrop | 2c9085b | 2022-12-12 11:35:54 -0700 | [diff] [blame] | 193 | ALOGW("EGL_ANDROID_blob_cache get: negative sizes are not allowed"); |
Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | if (mInitialized) { |
Cody Northrop | 2c9085b | 2022-12-12 11:35:54 -0700 | [diff] [blame] | 198 | if (mMultifileMode) { |
Cody Northrop | 4373541 | 2023-02-08 16:19:05 +0000 | [diff] [blame] | 199 | return getBlobMultifile(key, keySize, value, valueSize, mFilename); |
Cody Northrop | 2c9085b | 2022-12-12 11:35:54 -0700 | [diff] [blame] | 200 | } else { |
| 201 | BlobCache* bc = getBlobCacheLocked(); |
| 202 | return bc->get(key, keySize, value, valueSize); |
| 203 | } |
Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 204 | } |
| 205 | return 0; |
| 206 | } |
| 207 | |
Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 208 | void egl_cache_t::setCacheFilename(const char* filename) { |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 209 | std::lock_guard<std::mutex> lock(mMutex); |
Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 210 | mFilename = filename; |
| 211 | } |
| 212 | |
Cody Northrop | 2c9085b | 2022-12-12 11:35:54 -0700 | [diff] [blame] | 213 | void egl_cache_t::setCacheLimit(int64_t cacheByteLimit) { |
| 214 | std::lock_guard<std::mutex> lock(mMutex); |
| 215 | |
| 216 | if (!mMultifileMode) { |
| 217 | // If we're not in multifile mode, ensure the cache limit is only being lowered, |
| 218 | // not increasing above the hard coded platform limit |
Cody Northrop | 4373541 | 2023-02-08 16:19:05 +0000 | [diff] [blame] | 219 | if (cacheByteLimit > maxTotalSize) { |
Cody Northrop | 2c9085b | 2022-12-12 11:35:54 -0700 | [diff] [blame] | 220 | return; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | mCacheByteLimit = cacheByteLimit; |
| 225 | } |
| 226 | |
| 227 | size_t egl_cache_t::getCacheSize() { |
| 228 | std::lock_guard<std::mutex> lock(mMutex); |
Cody Northrop | 4373541 | 2023-02-08 16:19:05 +0000 | [diff] [blame] | 229 | if (mMultifileMode) { |
| 230 | return getMultifileCacheSize(); |
Cody Northrop | 2c9085b | 2022-12-12 11:35:54 -0700 | [diff] [blame] | 231 | } |
| 232 | if (mBlobCache) { |
| 233 | return mBlobCache->getSize(); |
| 234 | } |
| 235 | return 0; |
| 236 | } |
| 237 | |
Mathias Agopian | b7f9a24 | 2017-03-08 22:29:31 -0800 | [diff] [blame] | 238 | BlobCache* egl_cache_t::getBlobCacheLocked() { |
| 239 | if (mBlobCache == nullptr) { |
Cody Northrop | 4373541 | 2023-02-08 16:19:05 +0000 | [diff] [blame] | 240 | mBlobCache.reset(new FileBlobCache(maxKeySize, maxValueSize, mCacheByteLimit, mFilename)); |
Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 241 | } |
Mathias Agopian | b7f9a24 | 2017-03-08 22:29:31 -0800 | [diff] [blame] | 242 | return mBlobCache.get(); |
Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 243 | } |
| 244 | |
Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 245 | }; // namespace android |