blob: 8140292b34f9d9d04ddd0a4b53d6212cb64861ed [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
Cody Northrop6cca6c22023-02-08 20:23:13 -070017// #define LOG_NDEBUG 0
18
Mathias Agopian311b4792017-02-28 15:00:49 -080019#include "egl_cache.h"
20
Cody Northrop2c9085b2022-12-12 11:35:54 -070021#include <android-base/properties.h>
22#include <inttypes.h>
Yiwei Zhang8af03062020-08-12 21:28:15 -070023#include <log/log.h>
Mathias Agopianb7f9a242017-03-08 22:29:31 -080024#include <private/EGL/cache.h>
Jiyong Parka243e5d2017-06-21 12:26:51 +090025#include <unistd.h>
Mathias Agopian311b4792017-02-28 15:00:49 -080026
Mathias Agopian65421432017-03-08 11:49:05 -080027#include <thread>
28
Yiwei Zhang8af03062020-08-12 21:28:15 -070029#include "../egl_impl.h"
30#include "egl_display.h"
Jamie Gennis98c63832011-11-07 17:03:54 -080031
Cody Northrop2c9085b2022-12-12 11:35:54 -070032// Monolithic cache size limits.
Cody Northrop6cca6c22023-02-08 20:23:13 -070033static const size_t kMaxMonolithicKeySize = 12 * 1024;
34static const size_t kMaxMonolithicValueSize = 64 * 1024;
35static const size_t kMaxMonolithicTotalSize = 2 * 1024 * 1024;
Jamie Gennis76601082011-11-06 14:14:33 -080036
Cody Northrop2c9085b2022-12-12 11:35:54 -070037// The time in seconds to wait before saving newly inserted monolithic cache entries.
Cody Northrop6cca6c22023-02-08 20:23:13 -070038static const unsigned int kDeferredMonolithicSaveDelay = 4;
Jamie Gennis99c3d702011-11-08 17:59:36 -080039
Cody Northrop6cca6c22023-02-08 20:23:13 -070040// Multifile cache size limits
41constexpr uint32_t kMultifileHotCacheLimit = 8 * 1024 * 1024;
42constexpr uint32_t kMultifileCacheByteLimit = 32 * 1024 * 1024;
Cody Northrop2c9085b2022-12-12 11:35:54 -070043
Jamie Gennisaca51c02011-11-03 17:42:43 -070044namespace android {
Jamie Gennisaca51c02011-11-03 17:42:43 -070045
46#define BC_EXT_STR "EGL_ANDROID_blob_cache"
47
Mathias Agopianb7f9a242017-03-08 22:29:31 -080048// called from android_view_ThreadedRenderer.cpp
49void egl_set_cache_filename(const char* filename) {
50 egl_cache_t::get()->setCacheFilename(filename);
51}
52
Jamie Gennisaca51c02011-11-03 17:42:43 -070053//
Jamie Gennis76601082011-11-06 14:14:33 -080054// Callback functions passed to EGL.
Jamie Gennisaca51c02011-11-03 17:42:43 -070055//
Yiwei Zhang8af03062020-08-12 21:28:15 -070056static void setBlob(const void* key, EGLsizeiANDROID keySize, const void* value,
57 EGLsizeiANDROID valueSize) {
Jamie Gennis76601082011-11-06 14:14:33 -080058 egl_cache_t::get()->setBlob(key, keySize, value, valueSize);
Jamie Gennisaca51c02011-11-03 17:42:43 -070059}
60
Yiwei Zhang8af03062020-08-12 21:28:15 -070061static EGLsizeiANDROID getBlob(const void* key, EGLsizeiANDROID keySize, void* value,
62 EGLsizeiANDROID valueSize) {
Jamie Gennis76601082011-11-06 14:14:33 -080063 return egl_cache_t::get()->getBlob(key, keySize, value, valueSize);
64}
65
66//
67// egl_cache_t definition
68//
Cody Northrop2c9085b2022-12-12 11:35:54 -070069egl_cache_t::egl_cache_t()
Cody Northrop6cca6c22023-02-08 20:23:13 -070070 : mInitialized(false), mMultifileMode(false), mCacheByteLimit(kMaxMonolithicTotalSize) {}
Jamie Gennis76601082011-11-06 14:14:33 -080071
Yiwei Zhang8af03062020-08-12 21:28:15 -070072egl_cache_t::~egl_cache_t() {}
Jamie Gennisaca51c02011-11-03 17:42:43 -070073
Jamie Gennis98c63832011-11-07 17:03:54 -080074egl_cache_t egl_cache_t::sCache;
75
Jamie Gennisaca51c02011-11-03 17:42:43 -070076egl_cache_t* egl_cache_t::get() {
Jamie Gennis98c63832011-11-07 17:03:54 -080077 return &sCache;
Jamie Gennisaca51c02011-11-03 17:42:43 -070078}
79
Yiwei Zhang8af03062020-08-12 21:28:15 -070080void egl_cache_t::initialize(egl_display_t* display) {
Mathias Agopian65421432017-03-08 11:49:05 -080081 std::lock_guard<std::mutex> lock(mMutex);
Mathias Agopianada798b2012-02-13 17:09:30 -080082
83 egl_connection_t* const cnx = &gEGLImpl;
Cody Northrop6cca6c22023-02-08 20:23:13 -070084 if (display && cnx->dso && cnx->major >= 0 && cnx->minor >= 0) {
Mathias Agopianada798b2012-02-13 17:09:30 -080085 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 Zhang8af03062020-08-12 21:28:15 -070089 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 Agopian311b4792017-02-28 15:00:49 -080092 bool inMiddle = strstr(exts, " " BC_EXT_STR " ") != nullptr;
Mathias Agopianada798b2012-02-13 17:09:30 -080093 if (equal || atStart || atEnd || inMiddle) {
94 PFNEGLSETBLOBCACHEFUNCSANDROIDPROC eglSetBlobCacheFuncsANDROID;
Yiwei Zhang8af03062020-08-12 21:28:15 -070095 eglSetBlobCacheFuncsANDROID = reinterpret_cast<PFNEGLSETBLOBCACHEFUNCSANDROIDPROC>(
96 cnx->egl.eglGetProcAddress("eglSetBlobCacheFuncsANDROID"));
Yi Kong48a6cd22018-07-18 10:07:09 -070097 if (eglSetBlobCacheFuncsANDROID == nullptr) {
Mathias Agopianada798b2012-02-13 17:09:30 -080098 ALOGE("EGL_ANDROID_blob_cache advertised, "
Yiwei Zhang8af03062020-08-12 21:28:15 -070099 "but unable to get eglSetBlobCacheFuncsANDROID");
Mathias Agopianada798b2012-02-13 17:09:30 -0800100 return;
101 }
Jamie Gennisaca51c02011-11-03 17:42:43 -0700102
Yiwei Zhang8af03062020-08-12 21:28:15 -0700103 eglSetBlobCacheFuncsANDROID(display->disp.dpy, android::setBlob, android::getBlob);
Mathias Agopianada798b2012-02-13 17:09:30 -0800104 EGLint err = cnx->egl.eglGetError();
105 if (err != EGL_SUCCESS) {
106 ALOGE("eglSetBlobCacheFuncsANDROID resulted in an error: "
Yiwei Zhang8af03062020-08-12 21:28:15 -0700107 "%#x",
108 err);
Jamie Gennisaca51c02011-11-03 17:42:43 -0700109 }
110 }
111 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800112
Jamie Gennis76601082011-11-06 14:14:33 -0800113 mInitialized = true;
114}
115
116void egl_cache_t::terminate() {
Mathias Agopian65421432017-03-08 11:49:05 -0800117 std::lock_guard<std::mutex> lock(mMutex);
Stan Iliev9e7cd072017-10-09 15:56:10 -0400118 if (mBlobCache) {
119 mBlobCache->writeToFile();
120 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700121 mBlobCache = nullptr;
Cody Northrop6cca6c22023-02-08 20:23:13 -0700122 if (mMultifileBlobCache) {
123 mMultifileBlobCache->finish();
Cody Northrop2c9085b2022-12-12 11:35:54 -0700124 }
Cody Northrop6cca6c22023-02-08 20:23:13 -0700125 mMultifileBlobCache = nullptr;
Cody Northrop2c9085b2022-12-12 11:35:54 -0700126 mInitialized = false;
Jamie Gennis76601082011-11-06 14:14:33 -0800127}
128
Yiwei Zhang8af03062020-08-12 21:28:15 -0700129void egl_cache_t::setBlob(const void* key, EGLsizeiANDROID keySize, const void* value,
130 EGLsizeiANDROID valueSize) {
Mathias Agopian65421432017-03-08 11:49:05 -0800131 std::lock_guard<std::mutex> lock(mMutex);
Jamie Gennis76601082011-11-06 14:14:33 -0800132
133 if (keySize < 0 || valueSize < 0) {
Steve Block32397c12012-01-05 23:22:43 +0000134 ALOGW("EGL_ANDROID_blob_cache set: negative sizes are not allowed");
Jamie Gennis76601082011-11-06 14:14:33 -0800135 return;
136 }
137
138 if (mInitialized) {
Cody Northrop2c9085b2022-12-12 11:35:54 -0700139 if (mMultifileMode) {
Cody Northrop6cca6c22023-02-08 20:23:13 -0700140 MultifileBlobCache* mbc = getMultifileBlobCacheLocked();
141 mbc->set(key, keySize, value, valueSize);
Cody Northrop2c9085b2022-12-12 11:35:54 -0700142 } else {
143 BlobCache* bc = getBlobCacheLocked();
144 bc->set(key, keySize, value, valueSize);
145
146 if (!mSavePending) {
147 mSavePending = true;
148 std::thread deferredSaveThread([this]() {
Cody Northrop6cca6c22023-02-08 20:23:13 -0700149 sleep(kDeferredMonolithicSaveDelay);
Cody Northrop2c9085b2022-12-12 11:35:54 -0700150 std::lock_guard<std::mutex> lock(mMutex);
151 if (mInitialized && mBlobCache) {
152 mBlobCache->writeToFile();
153 }
154 mSavePending = false;
155 });
156 deferredSaveThread.detach();
157 }
Jamie Gennis99c3d702011-11-08 17:59:36 -0800158 }
Jamie Gennis76601082011-11-06 14:14:33 -0800159 }
160}
161
Yiwei Zhang8af03062020-08-12 21:28:15 -0700162EGLsizeiANDROID egl_cache_t::getBlob(const void* key, EGLsizeiANDROID keySize, void* value,
163 EGLsizeiANDROID valueSize) {
Mathias Agopian65421432017-03-08 11:49:05 -0800164 std::lock_guard<std::mutex> lock(mMutex);
Jamie Gennis76601082011-11-06 14:14:33 -0800165
166 if (keySize < 0 || valueSize < 0) {
Cody Northrop2c9085b2022-12-12 11:35:54 -0700167 ALOGW("EGL_ANDROID_blob_cache get: negative sizes are not allowed");
Jamie Gennis76601082011-11-06 14:14:33 -0800168 return 0;
169 }
170
171 if (mInitialized) {
Cody Northrop2c9085b2022-12-12 11:35:54 -0700172 if (mMultifileMode) {
Cody Northrop6cca6c22023-02-08 20:23:13 -0700173 MultifileBlobCache* mbc = getMultifileBlobCacheLocked();
174 return mbc->get(key, keySize, value, valueSize);
Cody Northrop2c9085b2022-12-12 11:35:54 -0700175 } else {
176 BlobCache* bc = getBlobCacheLocked();
177 return bc->get(key, keySize, value, valueSize);
178 }
Jamie Gennis76601082011-11-06 14:14:33 -0800179 }
Cody Northrop6cca6c22023-02-08 20:23:13 -0700180
Jamie Gennis76601082011-11-06 14:14:33 -0800181 return 0;
182}
183
Cody Northrop6cca6c22023-02-08 20:23:13 -0700184void egl_cache_t::setCacheMode(EGLCacheMode cacheMode) {
185 mMultifileMode = (cacheMode == EGLCacheMode::Multifile);
186}
187
Jamie Gennis98c63832011-11-07 17:03:54 -0800188void egl_cache_t::setCacheFilename(const char* filename) {
Mathias Agopian65421432017-03-08 11:49:05 -0800189 std::lock_guard<std::mutex> lock(mMutex);
Jamie Gennis98c63832011-11-07 17:03:54 -0800190 mFilename = filename;
191}
192
Cody Northrop2c9085b2022-12-12 11:35:54 -0700193void egl_cache_t::setCacheLimit(int64_t cacheByteLimit) {
194 std::lock_guard<std::mutex> lock(mMutex);
195
196 if (!mMultifileMode) {
197 // If we're not in multifile mode, ensure the cache limit is only being lowered,
198 // not increasing above the hard coded platform limit
Cody Northrop6cca6c22023-02-08 20:23:13 -0700199 if (cacheByteLimit > kMaxMonolithicTotalSize) {
Cody Northrop2c9085b2022-12-12 11:35:54 -0700200 return;
201 }
202 }
203
204 mCacheByteLimit = cacheByteLimit;
205}
206
207size_t egl_cache_t::getCacheSize() {
208 std::lock_guard<std::mutex> lock(mMutex);
Cody Northrop6cca6c22023-02-08 20:23:13 -0700209 if (mMultifileBlobCache) {
210 return mMultifileBlobCache->getTotalSize();
Cody Northrop2c9085b2022-12-12 11:35:54 -0700211 }
212 if (mBlobCache) {
213 return mBlobCache->getSize();
214 }
215 return 0;
216}
217
Mathias Agopianb7f9a242017-03-08 22:29:31 -0800218BlobCache* egl_cache_t::getBlobCacheLocked() {
219 if (mBlobCache == nullptr) {
Cody Northrop6cca6c22023-02-08 20:23:13 -0700220 mBlobCache.reset(new FileBlobCache(kMaxMonolithicKeySize, kMaxMonolithicValueSize,
221 mCacheByteLimit, mFilename));
Jamie Gennis76601082011-11-06 14:14:33 -0800222 }
Mathias Agopianb7f9a242017-03-08 22:29:31 -0800223 return mBlobCache.get();
Jamie Gennis76601082011-11-06 14:14:33 -0800224}
225
Cody Northrop6cca6c22023-02-08 20:23:13 -0700226MultifileBlobCache* egl_cache_t::getMultifileBlobCacheLocked() {
227 if (mMultifileBlobCache == nullptr) {
228 mMultifileBlobCache.reset(
229 new MultifileBlobCache(mCacheByteLimit, kMultifileHotCacheLimit, mFilename));
230 }
231 return mMultifileBlobCache.get();
232}
233
Jamie Gennisaca51c02011-11-03 17:42:43 -0700234}; // namespace android