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 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame^] | 19 | #include <log/log.h> |
Mathias Agopian | b7f9a24 | 2017-03-08 22:29:31 -0800 | [diff] [blame] | 20 | #include <private/EGL/cache.h> |
Jiyong Park | a243e5d | 2017-06-21 12:26:51 +0900 | [diff] [blame] | 21 | #include <unistd.h> |
Mathias Agopian | 311b479 | 2017-02-28 15:00:49 -0800 | [diff] [blame] | 22 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 23 | #include <thread> |
| 24 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame^] | 25 | #include "../egl_impl.h" |
| 26 | #include "egl_display.h" |
Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 27 | |
Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 28 | // Cache size limits. |
Dan Willemsen | bace39e | 2016-10-11 15:42:59 -0700 | [diff] [blame] | 29 | static const size_t maxKeySize = 12 * 1024; |
| 30 | static const size_t maxValueSize = 64 * 1024; |
| 31 | static const size_t maxTotalSize = 2 * 1024 * 1024; |
Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 32 | |
Jamie Gennis | 99c3d70 | 2011-11-08 17:59:36 -0800 | [diff] [blame] | 33 | // The time in seconds to wait before saving newly inserted cache entries. |
| 34 | static const unsigned int deferredSaveDelay = 4; |
| 35 | |
Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 36 | namespace android { |
Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 37 | |
| 38 | #define BC_EXT_STR "EGL_ANDROID_blob_cache" |
| 39 | |
Mathias Agopian | b7f9a24 | 2017-03-08 22:29:31 -0800 | [diff] [blame] | 40 | // called from android_view_ThreadedRenderer.cpp |
| 41 | void egl_set_cache_filename(const char* filename) { |
| 42 | egl_cache_t::get()->setCacheFilename(filename); |
| 43 | } |
| 44 | |
Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 45 | // |
Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 46 | // Callback functions passed to EGL. |
Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 47 | // |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame^] | 48 | static void setBlob(const void* key, EGLsizeiANDROID keySize, const void* value, |
| 49 | EGLsizeiANDROID valueSize) { |
Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 50 | egl_cache_t::get()->setBlob(key, keySize, value, valueSize); |
Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame^] | 53 | static EGLsizeiANDROID getBlob(const void* key, EGLsizeiANDROID keySize, void* value, |
| 54 | EGLsizeiANDROID valueSize) { |
Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 55 | return egl_cache_t::get()->getBlob(key, keySize, value, valueSize); |
| 56 | } |
| 57 | |
| 58 | // |
| 59 | // egl_cache_t definition |
| 60 | // |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame^] | 61 | egl_cache_t::egl_cache_t() : mInitialized(false) {} |
Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 62 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame^] | 63 | egl_cache_t::~egl_cache_t() {} |
Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 64 | |
Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 65 | egl_cache_t egl_cache_t::sCache; |
| 66 | |
Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 67 | egl_cache_t* egl_cache_t::get() { |
Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 68 | return &sCache; |
Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame^] | 71 | void egl_cache_t::initialize(egl_display_t* display) { |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 72 | std::lock_guard<std::mutex> lock(mMutex); |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 73 | |
| 74 | egl_connection_t* const cnx = &gEGLImpl; |
| 75 | if (cnx->dso && cnx->major >= 0 && cnx->minor >= 0) { |
| 76 | const char* exts = display->disp.queryString.extensions; |
| 77 | size_t bcExtLen = strlen(BC_EXT_STR); |
| 78 | size_t extsLen = strlen(exts); |
| 79 | bool equal = !strcmp(BC_EXT_STR, exts); |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame^] | 80 | bool atStart = !strncmp(BC_EXT_STR " ", exts, bcExtLen + 1); |
| 81 | bool atEnd = (bcExtLen + 1) < extsLen && |
| 82 | !strcmp(" " BC_EXT_STR, exts + extsLen - (bcExtLen + 1)); |
Mathias Agopian | 311b479 | 2017-02-28 15:00:49 -0800 | [diff] [blame] | 83 | bool inMiddle = strstr(exts, " " BC_EXT_STR " ") != nullptr; |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 84 | if (equal || atStart || atEnd || inMiddle) { |
| 85 | PFNEGLSETBLOBCACHEFUNCSANDROIDPROC eglSetBlobCacheFuncsANDROID; |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame^] | 86 | eglSetBlobCacheFuncsANDROID = reinterpret_cast<PFNEGLSETBLOBCACHEFUNCSANDROIDPROC>( |
| 87 | cnx->egl.eglGetProcAddress("eglSetBlobCacheFuncsANDROID")); |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 88 | if (eglSetBlobCacheFuncsANDROID == nullptr) { |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 89 | ALOGE("EGL_ANDROID_blob_cache advertised, " |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame^] | 90 | "but unable to get eglSetBlobCacheFuncsANDROID"); |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 91 | return; |
| 92 | } |
Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 93 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame^] | 94 | eglSetBlobCacheFuncsANDROID(display->disp.dpy, android::setBlob, android::getBlob); |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 95 | EGLint err = cnx->egl.eglGetError(); |
| 96 | if (err != EGL_SUCCESS) { |
| 97 | ALOGE("eglSetBlobCacheFuncsANDROID resulted in an error: " |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame^] | 98 | "%#x", |
| 99 | err); |
Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | } |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 103 | |
Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 104 | mInitialized = true; |
| 105 | } |
| 106 | |
| 107 | void egl_cache_t::terminate() { |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 108 | std::lock_guard<std::mutex> lock(mMutex); |
Stan Iliev | 9e7cd07 | 2017-10-09 15:56:10 -0400 | [diff] [blame] | 109 | if (mBlobCache) { |
| 110 | mBlobCache->writeToFile(); |
| 111 | } |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 112 | mBlobCache = nullptr; |
Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame^] | 115 | void egl_cache_t::setBlob(const void* key, EGLsizeiANDROID keySize, const void* value, |
| 116 | EGLsizeiANDROID valueSize) { |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 117 | std::lock_guard<std::mutex> lock(mMutex); |
Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 118 | |
| 119 | if (keySize < 0 || valueSize < 0) { |
Steve Block | 32397c1 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 120 | ALOGW("EGL_ANDROID_blob_cache set: negative sizes are not allowed"); |
Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 121 | return; |
| 122 | } |
| 123 | |
| 124 | if (mInitialized) { |
Mathias Agopian | b7f9a24 | 2017-03-08 22:29:31 -0800 | [diff] [blame] | 125 | BlobCache* bc = getBlobCacheLocked(); |
Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 126 | bc->set(key, keySize, value, valueSize); |
Jamie Gennis | 99c3d70 | 2011-11-08 17:59:36 -0800 | [diff] [blame] | 127 | |
| 128 | if (!mSavePending) { |
Jamie Gennis | 99c3d70 | 2011-11-08 17:59:36 -0800 | [diff] [blame] | 129 | mSavePending = true; |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 130 | std::thread deferredSaveThread([this]() { |
| 131 | sleep(deferredSaveDelay); |
| 132 | std::lock_guard<std::mutex> lock(mMutex); |
Stan Iliev | 9e7cd07 | 2017-10-09 15:56:10 -0400 | [diff] [blame] | 133 | if (mInitialized && mBlobCache) { |
| 134 | mBlobCache->writeToFile(); |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 135 | } |
| 136 | mSavePending = false; |
| 137 | }); |
| 138 | deferredSaveThread.detach(); |
Jamie Gennis | 99c3d70 | 2011-11-08 17:59:36 -0800 | [diff] [blame] | 139 | } |
Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame^] | 143 | EGLsizeiANDROID egl_cache_t::getBlob(const void* key, EGLsizeiANDROID keySize, 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 0; |
| 150 | } |
| 151 | |
| 152 | if (mInitialized) { |
Mathias Agopian | b7f9a24 | 2017-03-08 22:29:31 -0800 | [diff] [blame] | 153 | BlobCache* bc = getBlobCacheLocked(); |
Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 154 | return bc->get(key, keySize, value, valueSize); |
| 155 | } |
| 156 | return 0; |
| 157 | } |
| 158 | |
Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 159 | void egl_cache_t::setCacheFilename(const char* filename) { |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 160 | std::lock_guard<std::mutex> lock(mMutex); |
Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 161 | mFilename = filename; |
| 162 | } |
| 163 | |
Mathias Agopian | b7f9a24 | 2017-03-08 22:29:31 -0800 | [diff] [blame] | 164 | BlobCache* egl_cache_t::getBlobCacheLocked() { |
| 165 | if (mBlobCache == nullptr) { |
Stan Iliev | 9e7cd07 | 2017-10-09 15:56:10 -0400 | [diff] [blame] | 166 | mBlobCache.reset(new FileBlobCache(maxKeySize, maxValueSize, maxTotalSize, mFilename)); |
Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 167 | } |
Mathias Agopian | b7f9a24 | 2017-03-08 22:29:31 -0800 | [diff] [blame] | 168 | return mBlobCache.get(); |
Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 171 | }; // namespace android |