blob: 6225c26c9b79d3994d54dfa02139841b220e4f99 [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 Northrop2c9085b2022-12-12 11:35:54 -070028#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.
Dan Willemsenbace39e2016-10-11 15:42:59 -070032static const size_t maxKeySize = 12 * 1024;
33static const size_t maxValueSize = 64 * 1024;
Trevor David Black94764682022-09-26 20:31:28 +000034static 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.
Jamie Gennis99c3d702011-11-08 17:59:36 -080037static const unsigned int deferredSaveDelay = 4;
38
Cody Northrop2c9085b2022-12-12 11:35:54 -070039// 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;
44
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()
71 : mInitialized(false), mMultifileMode(true), mCacheByteLimit(maxTotalSize) {}
Jamie Gennis76601082011-11-06 14:14:33 -080072
Yiwei Zhang8af03062020-08-12 21:28:15 -070073egl_cache_t::~egl_cache_t() {}
Jamie Gennisaca51c02011-11-03 17:42:43 -070074
Jamie Gennis98c63832011-11-07 17:03:54 -080075egl_cache_t egl_cache_t::sCache;
76
Jamie Gennisaca51c02011-11-03 17:42:43 -070077egl_cache_t* egl_cache_t::get() {
Jamie Gennis98c63832011-11-07 17:03:54 -080078 return &sCache;
Jamie Gennisaca51c02011-11-03 17:42:43 -070079}
80
Yiwei Zhang8af03062020-08-12 21:28:15 -070081void egl_cache_t::initialize(egl_display_t* display) {
Mathias Agopian65421432017-03-08 11:49:05 -080082 std::lock_guard<std::mutex> lock(mMutex);
Mathias Agopianada798b2012-02-13 17:09:30 -080083
84 egl_connection_t* const cnx = &gEGLImpl;
85 if (cnx->dso && cnx->major >= 0 && cnx->minor >= 0) {
86 const char* exts = display->disp.queryString.extensions;
87 size_t bcExtLen = strlen(BC_EXT_STR);
88 size_t extsLen = strlen(exts);
89 bool equal = !strcmp(BC_EXT_STR, exts);
Yiwei Zhang8af03062020-08-12 21:28:15 -070090 bool atStart = !strncmp(BC_EXT_STR " ", exts, bcExtLen + 1);
91 bool atEnd = (bcExtLen + 1) < extsLen &&
92 !strcmp(" " BC_EXT_STR, exts + extsLen - (bcExtLen + 1));
Mathias Agopian311b4792017-02-28 15:00:49 -080093 bool inMiddle = strstr(exts, " " BC_EXT_STR " ") != nullptr;
Mathias Agopianada798b2012-02-13 17:09:30 -080094 if (equal || atStart || atEnd || inMiddle) {
95 PFNEGLSETBLOBCACHEFUNCSANDROIDPROC eglSetBlobCacheFuncsANDROID;
Yiwei Zhang8af03062020-08-12 21:28:15 -070096 eglSetBlobCacheFuncsANDROID = reinterpret_cast<PFNEGLSETBLOBCACHEFUNCSANDROIDPROC>(
97 cnx->egl.eglGetProcAddress("eglSetBlobCacheFuncsANDROID"));
Yi Kong48a6cd22018-07-18 10:07:09 -070098 if (eglSetBlobCacheFuncsANDROID == nullptr) {
Mathias Agopianada798b2012-02-13 17:09:30 -080099 ALOGE("EGL_ANDROID_blob_cache advertised, "
Yiwei Zhang8af03062020-08-12 21:28:15 -0700100 "but unable to get eglSetBlobCacheFuncsANDROID");
Mathias Agopianada798b2012-02-13 17:09:30 -0800101 return;
102 }
Jamie Gennisaca51c02011-11-03 17:42:43 -0700103
Yiwei Zhang8af03062020-08-12 21:28:15 -0700104 eglSetBlobCacheFuncsANDROID(display->disp.dpy, android::setBlob, android::getBlob);
Mathias Agopianada798b2012-02-13 17:09:30 -0800105 EGLint err = cnx->egl.eglGetError();
106 if (err != EGL_SUCCESS) {
107 ALOGE("eglSetBlobCacheFuncsANDROID resulted in an error: "
Yiwei Zhang8af03062020-08-12 21:28:15 -0700108 "%#x",
109 err);
Jamie Gennisaca51c02011-11-03 17:42:43 -0700110 }
111 }
112 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800113
Cody Northrop2c9085b2022-12-12 11:35:54 -0700114 mMultifileMode = true;
115
116 // Allow forcing monolithic cache for debug purposes
117 if (base::GetProperty("debug.egl.blobcache.multifilemode", "") == "false") {
118 ALOGD("Forcing monolithic cache due to debug.egl.blobcache.multifilemode == \"false\"");
119 mMultifileMode = false;
120 }
121
122 if (mMultifileMode) {
123 mCacheByteLimit = kMultifileCacheByteLimit;
124 }
125
Jamie Gennis76601082011-11-06 14:14:33 -0800126 mInitialized = true;
127}
128
129void egl_cache_t::terminate() {
Mathias Agopian65421432017-03-08 11:49:05 -0800130 std::lock_guard<std::mutex> lock(mMutex);
Stan Iliev9e7cd072017-10-09 15:56:10 -0400131 if (mBlobCache) {
132 mBlobCache->writeToFile();
133 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700134 mBlobCache = nullptr;
Cody Northrop2c9085b2022-12-12 11:35:54 -0700135 if (mMultifileMode) {
136 checkMultifileCacheSize(mCacheByteLimit);
137 }
138 mMultifileMode = false;
139 mInitialized = false;
Jamie Gennis76601082011-11-06 14:14:33 -0800140}
141
Yiwei Zhang8af03062020-08-12 21:28:15 -0700142void egl_cache_t::setBlob(const void* key, EGLsizeiANDROID keySize, const void* value,
143 EGLsizeiANDROID valueSize) {
Mathias Agopian65421432017-03-08 11:49:05 -0800144 std::lock_guard<std::mutex> lock(mMutex);
Jamie Gennis76601082011-11-06 14:14:33 -0800145
146 if (keySize < 0 || valueSize < 0) {
Steve Block32397c12012-01-05 23:22:43 +0000147 ALOGW("EGL_ANDROID_blob_cache set: negative sizes are not allowed");
Jamie Gennis76601082011-11-06 14:14:33 -0800148 return;
149 }
150
151 if (mInitialized) {
Cody Northrop2c9085b2022-12-12 11:35:54 -0700152 if (mMultifileMode) {
153 setBlobMultifile(key, keySize, value, valueSize, mFilename);
Jamie Gennis99c3d702011-11-08 17:59:36 -0800154
Cody Northrop2c9085b2022-12-12 11:35:54 -0700155 if (!mMultifileCleanupPending) {
156 mMultifileCleanupPending = true;
157 // Kick off a thread to cull cache files below limit
158 std::thread deferredMultifileCleanupThread([this]() {
159 sleep(deferredMultifileCleanupDelaySeconds);
160 std::lock_guard<std::mutex> lock(mMutex);
161 // Check the size of cache and remove entries to stay under limit
162 checkMultifileCacheSize(mCacheByteLimit);
163 mMultifileCleanupPending = false;
164 });
165 deferredMultifileCleanupThread.detach();
166 }
167 } else {
168 BlobCache* bc = getBlobCacheLocked();
169 bc->set(key, keySize, value, valueSize);
170
171 if (!mSavePending) {
172 mSavePending = true;
173 std::thread deferredSaveThread([this]() {
174 sleep(deferredSaveDelay);
175 std::lock_guard<std::mutex> lock(mMutex);
176 if (mInitialized && mBlobCache) {
177 mBlobCache->writeToFile();
178 }
179 mSavePending = false;
180 });
181 deferredSaveThread.detach();
182 }
Jamie Gennis99c3d702011-11-08 17:59:36 -0800183 }
Jamie Gennis76601082011-11-06 14:14:33 -0800184 }
185}
186
Yiwei Zhang8af03062020-08-12 21:28:15 -0700187EGLsizeiANDROID egl_cache_t::getBlob(const void* key, EGLsizeiANDROID keySize, void* value,
188 EGLsizeiANDROID valueSize) {
Mathias Agopian65421432017-03-08 11:49:05 -0800189 std::lock_guard<std::mutex> lock(mMutex);
Jamie Gennis76601082011-11-06 14:14:33 -0800190
191 if (keySize < 0 || valueSize < 0) {
Cody Northrop2c9085b2022-12-12 11:35:54 -0700192 ALOGW("EGL_ANDROID_blob_cache get: negative sizes are not allowed");
Jamie Gennis76601082011-11-06 14:14:33 -0800193 return 0;
194 }
195
196 if (mInitialized) {
Cody Northrop2c9085b2022-12-12 11:35:54 -0700197 if (mMultifileMode) {
198 return getBlobMultifile(key, keySize, value, valueSize, mFilename);
199 } else {
200 BlobCache* bc = getBlobCacheLocked();
201 return bc->get(key, keySize, value, valueSize);
202 }
Jamie Gennis76601082011-11-06 14:14:33 -0800203 }
204 return 0;
205}
206
Jamie Gennis98c63832011-11-07 17:03:54 -0800207void egl_cache_t::setCacheFilename(const char* filename) {
Mathias Agopian65421432017-03-08 11:49:05 -0800208 std::lock_guard<std::mutex> lock(mMutex);
Jamie Gennis98c63832011-11-07 17:03:54 -0800209 mFilename = filename;
210}
211
Cody Northrop2c9085b2022-12-12 11:35:54 -0700212void egl_cache_t::setCacheLimit(int64_t cacheByteLimit) {
213 std::lock_guard<std::mutex> lock(mMutex);
214
215 if (!mMultifileMode) {
216 // If we're not in multifile mode, ensure the cache limit is only being lowered,
217 // not increasing above the hard coded platform limit
218 if (cacheByteLimit > maxTotalSize) {
219 return;
220 }
221 }
222
223 mCacheByteLimit = cacheByteLimit;
224}
225
226size_t egl_cache_t::getCacheSize() {
227 std::lock_guard<std::mutex> lock(mMutex);
228 if (mMultifileMode) {
229 return getMultifileCacheSize();
230 }
231 if (mBlobCache) {
232 return mBlobCache->getSize();
233 }
234 return 0;
235}
236
Mathias Agopianb7f9a242017-03-08 22:29:31 -0800237BlobCache* egl_cache_t::getBlobCacheLocked() {
238 if (mBlobCache == nullptr) {
Cody Northrop2c9085b2022-12-12 11:35:54 -0700239 mBlobCache.reset(new FileBlobCache(maxKeySize, maxValueSize, mCacheByteLimit, mFilename));
Jamie Gennis76601082011-11-06 14:14:33 -0800240 }
Mathias Agopianb7f9a242017-03-08 22:29:31 -0800241 return mBlobCache.get();
Jamie Gennis76601082011-11-06 14:14:33 -0800242}
243
Jamie Gennisaca51c02011-11-03 17:42:43 -0700244}; // namespace android