blob: 1e8a34863dca86294e7c19feda7ad18a32056006 [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
Mathias Agopian311b4792017-02-28 15:00:49 -080017#include "egl_cache.h"
18
Cody Northrop2c9085b2022-12-12 11:35:54 -070019#include <android-base/properties.h>
20#include <inttypes.h>
Yiwei Zhang8af03062020-08-12 21:28:15 -070021#include <log/log.h>
Mathias Agopianb7f9a242017-03-08 22:29:31 -080022#include <private/EGL/cache.h>
Jiyong Parka243e5d2017-06-21 12:26:51 +090023#include <unistd.h>
Mathias Agopian311b4792017-02-28 15:00:49 -080024
Mathias Agopian65421432017-03-08 11:49:05 -080025#include <thread>
26
Yiwei Zhang8af03062020-08-12 21:28:15 -070027#include "../egl_impl.h"
Cody Northrop43735412023-02-08 16:19:05 +000028#include "egl_cache_multifile.h"
Yiwei Zhang8af03062020-08-12 21:28:15 -070029#include "egl_display.h"
Jamie Gennis98c63832011-11-07 17:03:54 -080030
Cody Northrop2c9085b2022-12-12 11:35:54 -070031// Monolithic cache size limits.
Cody Northrop43735412023-02-08 16:19:05 +000032static const size_t maxKeySize = 12 * 1024;
33static const size_t maxValueSize = 64 * 1024;
34static const size_t maxTotalSize = 32 * 1024 * 1024;
Jamie Gennis76601082011-11-06 14:14:33 -080035
Cody Northrop2c9085b2022-12-12 11:35:54 -070036// The time in seconds to wait before saving newly inserted monolithic cache entries.
Cody Northrop43735412023-02-08 16:19:05 +000037static const unsigned int deferredSaveDelay = 4;
Jamie Gennis99c3d702011-11-08 17:59:36 -080038
Cody Northrop43735412023-02-08 16:19:05 +000039// Multifile cache size limit
40constexpr size_t kMultifileCacheByteLimit = 64 * 1024 * 1024;
41
42// Delay before cleaning up multifile cache entries
43static const unsigned int deferredMultifileCleanupDelaySeconds = 1;
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 Northrop43735412023-02-08 16:19:05 +000071 : mInitialized(false),
72 mMultifileMode(false),
73 mCacheByteLimit(maxTotalSize),
74 mMultifileCleanupPending(false) {}
Jamie Gennis76601082011-11-06 14:14:33 -080075
Yiwei Zhang8af03062020-08-12 21:28:15 -070076egl_cache_t::~egl_cache_t() {}
Jamie Gennisaca51c02011-11-03 17:42:43 -070077
Jamie Gennis98c63832011-11-07 17:03:54 -080078egl_cache_t egl_cache_t::sCache;
79
Jamie Gennisaca51c02011-11-03 17:42:43 -070080egl_cache_t* egl_cache_t::get() {
Jamie Gennis98c63832011-11-07 17:03:54 -080081 return &sCache;
Jamie Gennisaca51c02011-11-03 17:42:43 -070082}
83
Yiwei Zhang8af03062020-08-12 21:28:15 -070084void egl_cache_t::initialize(egl_display_t* display) {
Mathias Agopian65421432017-03-08 11:49:05 -080085 std::lock_guard<std::mutex> lock(mMutex);
Mathias Agopianada798b2012-02-13 17:09:30 -080086
87 egl_connection_t* const cnx = &gEGLImpl;
Cody Northrop43735412023-02-08 16:19:05 +000088 if (cnx->dso && cnx->major >= 0 && cnx->minor >= 0) {
Mathias Agopianada798b2012-02-13 17:09:30 -080089 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 Zhang8af03062020-08-12 21:28:15 -070093 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 Agopian311b4792017-02-28 15:00:49 -080096 bool inMiddle = strstr(exts, " " BC_EXT_STR " ") != nullptr;
Mathias Agopianada798b2012-02-13 17:09:30 -080097 if (equal || atStart || atEnd || inMiddle) {
98 PFNEGLSETBLOBCACHEFUNCSANDROIDPROC eglSetBlobCacheFuncsANDROID;
Yiwei Zhang8af03062020-08-12 21:28:15 -070099 eglSetBlobCacheFuncsANDROID = reinterpret_cast<PFNEGLSETBLOBCACHEFUNCSANDROIDPROC>(
100 cnx->egl.eglGetProcAddress("eglSetBlobCacheFuncsANDROID"));
Yi Kong48a6cd22018-07-18 10:07:09 -0700101 if (eglSetBlobCacheFuncsANDROID == nullptr) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800102 ALOGE("EGL_ANDROID_blob_cache advertised, "
Yiwei Zhang8af03062020-08-12 21:28:15 -0700103 "but unable to get eglSetBlobCacheFuncsANDROID");
Mathias Agopianada798b2012-02-13 17:09:30 -0800104 return;
105 }
Jamie Gennisaca51c02011-11-03 17:42:43 -0700106
Yiwei Zhang8af03062020-08-12 21:28:15 -0700107 eglSetBlobCacheFuncsANDROID(display->disp.dpy, android::setBlob, android::getBlob);
Mathias Agopianada798b2012-02-13 17:09:30 -0800108 EGLint err = cnx->egl.eglGetError();
109 if (err != EGL_SUCCESS) {
110 ALOGE("eglSetBlobCacheFuncsANDROID resulted in an error: "
Yiwei Zhang8af03062020-08-12 21:28:15 -0700111 "%#x",
112 err);
Jamie Gennisaca51c02011-11-03 17:42:43 -0700113 }
114 }
115 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800116
Cody Northrop43735412023-02-08 16:19:05 +0000117 // 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 Northrop2c9085b2022-12-12 11:35:54 -0700120 mMultifileMode = false;
121 }
122
123 if (mMultifileMode) {
Cody Northrop43735412023-02-08 16:19:05 +0000124 mCacheByteLimit = kMultifileCacheByteLimit;
Cody Northrop2c9085b2022-12-12 11:35:54 -0700125 }
126
Jamie Gennis76601082011-11-06 14:14:33 -0800127 mInitialized = true;
128}
129
130void egl_cache_t::terminate() {
Mathias Agopian65421432017-03-08 11:49:05 -0800131 std::lock_guard<std::mutex> lock(mMutex);
Stan Iliev9e7cd072017-10-09 15:56:10 -0400132 if (mBlobCache) {
133 mBlobCache->writeToFile();
134 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700135 mBlobCache = nullptr;
Cody Northrop43735412023-02-08 16:19:05 +0000136 if (mMultifileMode) {
137 checkMultifileCacheSize(mCacheByteLimit);
Cody Northrop2c9085b2022-12-12 11:35:54 -0700138 }
Cody Northrop43735412023-02-08 16:19:05 +0000139 mMultifileMode = false;
Cody Northrop2c9085b2022-12-12 11:35:54 -0700140 mInitialized = false;
Jamie Gennis76601082011-11-06 14:14:33 -0800141}
142
Yiwei Zhang8af03062020-08-12 21:28:15 -0700143void egl_cache_t::setBlob(const void* key, EGLsizeiANDROID keySize, const void* value,
144 EGLsizeiANDROID valueSize) {
Mathias Agopian65421432017-03-08 11:49:05 -0800145 std::lock_guard<std::mutex> lock(mMutex);
Jamie Gennis76601082011-11-06 14:14:33 -0800146
147 if (keySize < 0 || valueSize < 0) {
Steve Block32397c12012-01-05 23:22:43 +0000148 ALOGW("EGL_ANDROID_blob_cache set: negative sizes are not allowed");
Jamie Gennis76601082011-11-06 14:14:33 -0800149 return;
150 }
151
152 if (mInitialized) {
Cody Northrop2c9085b2022-12-12 11:35:54 -0700153 if (mMultifileMode) {
Cody Northrop43735412023-02-08 16:19:05 +0000154 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 Northrop2c9085b2022-12-12 11:35:54 -0700168 } 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 Northrop43735412023-02-08 16:19:05 +0000175 sleep(deferredSaveDelay);
Cody Northrop2c9085b2022-12-12 11:35:54 -0700176 std::lock_guard<std::mutex> lock(mMutex);
177 if (mInitialized && mBlobCache) {
178 mBlobCache->writeToFile();
179 }
180 mSavePending = false;
181 });
182 deferredSaveThread.detach();
183 }
Jamie Gennis99c3d702011-11-08 17:59:36 -0800184 }
Jamie Gennis76601082011-11-06 14:14:33 -0800185 }
186}
187
Yiwei Zhang8af03062020-08-12 21:28:15 -0700188EGLsizeiANDROID egl_cache_t::getBlob(const void* key, EGLsizeiANDROID keySize, void* value,
189 EGLsizeiANDROID valueSize) {
Mathias Agopian65421432017-03-08 11:49:05 -0800190 std::lock_guard<std::mutex> lock(mMutex);
Jamie Gennis76601082011-11-06 14:14:33 -0800191
192 if (keySize < 0 || valueSize < 0) {
Cody Northrop2c9085b2022-12-12 11:35:54 -0700193 ALOGW("EGL_ANDROID_blob_cache get: negative sizes are not allowed");
Jamie Gennis76601082011-11-06 14:14:33 -0800194 return 0;
195 }
196
197 if (mInitialized) {
Cody Northrop2c9085b2022-12-12 11:35:54 -0700198 if (mMultifileMode) {
Cody Northrop43735412023-02-08 16:19:05 +0000199 return getBlobMultifile(key, keySize, value, valueSize, mFilename);
Cody Northrop2c9085b2022-12-12 11:35:54 -0700200 } else {
201 BlobCache* bc = getBlobCacheLocked();
202 return bc->get(key, keySize, value, valueSize);
203 }
Jamie Gennis76601082011-11-06 14:14:33 -0800204 }
205 return 0;
206}
207
Jamie Gennis98c63832011-11-07 17:03:54 -0800208void egl_cache_t::setCacheFilename(const char* filename) {
Mathias Agopian65421432017-03-08 11:49:05 -0800209 std::lock_guard<std::mutex> lock(mMutex);
Jamie Gennis98c63832011-11-07 17:03:54 -0800210 mFilename = filename;
211}
212
Cody Northrop2c9085b2022-12-12 11:35:54 -0700213void 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 Northrop43735412023-02-08 16:19:05 +0000219 if (cacheByteLimit > maxTotalSize) {
Cody Northrop2c9085b2022-12-12 11:35:54 -0700220 return;
221 }
222 }
223
224 mCacheByteLimit = cacheByteLimit;
225}
226
227size_t egl_cache_t::getCacheSize() {
228 std::lock_guard<std::mutex> lock(mMutex);
Cody Northrop43735412023-02-08 16:19:05 +0000229 if (mMultifileMode) {
230 return getMultifileCacheSize();
Cody Northrop2c9085b2022-12-12 11:35:54 -0700231 }
232 if (mBlobCache) {
233 return mBlobCache->getSize();
234 }
235 return 0;
236}
237
Mathias Agopianb7f9a242017-03-08 22:29:31 -0800238BlobCache* egl_cache_t::getBlobCacheLocked() {
239 if (mBlobCache == nullptr) {
Cody Northrop43735412023-02-08 16:19:05 +0000240 mBlobCache.reset(new FileBlobCache(maxKeySize, maxValueSize, mCacheByteLimit, mFilename));
Jamie Gennis76601082011-11-06 14:14:33 -0800241 }
Mathias Agopianb7f9a242017-03-08 22:29:31 -0800242 return mBlobCache.get();
Jamie Gennis76601082011-11-06 14:14:33 -0800243}
244
Jamie Gennisaca51c02011-11-03 17:42:43 -0700245}; // namespace android