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