blob: efa67dbc1c6956956a3250522f0b5c5242ca4862 [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
Yiwei Zhang8af03062020-08-12 21:28:15 -070019#include <log/log.h>
Mathias Agopianb7f9a242017-03-08 22:29:31 -080020#include <private/EGL/cache.h>
Jiyong Parka243e5d2017-06-21 12:26:51 +090021#include <unistd.h>
Mathias Agopian311b4792017-02-28 15:00:49 -080022
Mathias Agopian65421432017-03-08 11:49:05 -080023#include <thread>
24
Yiwei Zhang8af03062020-08-12 21:28:15 -070025#include "../egl_impl.h"
26#include "egl_display.h"
Jamie Gennis98c63832011-11-07 17:03:54 -080027
Jamie Gennis76601082011-11-06 14:14:33 -080028// Cache size limits.
Dan Willemsenbace39e2016-10-11 15:42:59 -070029static const size_t maxKeySize = 12 * 1024;
30static const size_t maxValueSize = 64 * 1024;
31static const size_t maxTotalSize = 2 * 1024 * 1024;
Jamie Gennis76601082011-11-06 14:14:33 -080032
Jamie Gennis99c3d702011-11-08 17:59:36 -080033// The time in seconds to wait before saving newly inserted cache entries.
34static const unsigned int deferredSaveDelay = 4;
35
Jamie Gennisaca51c02011-11-03 17:42:43 -070036namespace android {
Jamie Gennisaca51c02011-11-03 17:42:43 -070037
38#define BC_EXT_STR "EGL_ANDROID_blob_cache"
39
Mathias Agopianb7f9a242017-03-08 22:29:31 -080040// called from android_view_ThreadedRenderer.cpp
41void egl_set_cache_filename(const char* filename) {
42 egl_cache_t::get()->setCacheFilename(filename);
43}
44
Jamie Gennisaca51c02011-11-03 17:42:43 -070045//
Jamie Gennis76601082011-11-06 14:14:33 -080046// Callback functions passed to EGL.
Jamie Gennisaca51c02011-11-03 17:42:43 -070047//
Yiwei Zhang8af03062020-08-12 21:28:15 -070048static void setBlob(const void* key, EGLsizeiANDROID keySize, const void* value,
49 EGLsizeiANDROID valueSize) {
Jamie Gennis76601082011-11-06 14:14:33 -080050 egl_cache_t::get()->setBlob(key, keySize, value, valueSize);
Jamie Gennisaca51c02011-11-03 17:42:43 -070051}
52
Yiwei Zhang8af03062020-08-12 21:28:15 -070053static EGLsizeiANDROID getBlob(const void* key, EGLsizeiANDROID keySize, void* value,
54 EGLsizeiANDROID valueSize) {
Jamie Gennis76601082011-11-06 14:14:33 -080055 return egl_cache_t::get()->getBlob(key, keySize, value, valueSize);
56}
57
58//
59// egl_cache_t definition
60//
Yiwei Zhang8af03062020-08-12 21:28:15 -070061egl_cache_t::egl_cache_t() : mInitialized(false) {}
Jamie Gennis76601082011-11-06 14:14:33 -080062
Yiwei Zhang8af03062020-08-12 21:28:15 -070063egl_cache_t::~egl_cache_t() {}
Jamie Gennisaca51c02011-11-03 17:42:43 -070064
Jamie Gennis98c63832011-11-07 17:03:54 -080065egl_cache_t egl_cache_t::sCache;
66
Jamie Gennisaca51c02011-11-03 17:42:43 -070067egl_cache_t* egl_cache_t::get() {
Jamie Gennis98c63832011-11-07 17:03:54 -080068 return &sCache;
Jamie Gennisaca51c02011-11-03 17:42:43 -070069}
70
Yiwei Zhang8af03062020-08-12 21:28:15 -070071void egl_cache_t::initialize(egl_display_t* display) {
Mathias Agopian65421432017-03-08 11:49:05 -080072 std::lock_guard<std::mutex> lock(mMutex);
Mathias Agopianada798b2012-02-13 17:09:30 -080073
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 Zhang8af03062020-08-12 21:28:15 -070080 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 Agopian311b4792017-02-28 15:00:49 -080083 bool inMiddle = strstr(exts, " " BC_EXT_STR " ") != nullptr;
Mathias Agopianada798b2012-02-13 17:09:30 -080084 if (equal || atStart || atEnd || inMiddle) {
85 PFNEGLSETBLOBCACHEFUNCSANDROIDPROC eglSetBlobCacheFuncsANDROID;
Yiwei Zhang8af03062020-08-12 21:28:15 -070086 eglSetBlobCacheFuncsANDROID = reinterpret_cast<PFNEGLSETBLOBCACHEFUNCSANDROIDPROC>(
87 cnx->egl.eglGetProcAddress("eglSetBlobCacheFuncsANDROID"));
Yi Kong48a6cd22018-07-18 10:07:09 -070088 if (eglSetBlobCacheFuncsANDROID == nullptr) {
Mathias Agopianada798b2012-02-13 17:09:30 -080089 ALOGE("EGL_ANDROID_blob_cache advertised, "
Yiwei Zhang8af03062020-08-12 21:28:15 -070090 "but unable to get eglSetBlobCacheFuncsANDROID");
Mathias Agopianada798b2012-02-13 17:09:30 -080091 return;
92 }
Jamie Gennisaca51c02011-11-03 17:42:43 -070093
Yiwei Zhang8af03062020-08-12 21:28:15 -070094 eglSetBlobCacheFuncsANDROID(display->disp.dpy, android::setBlob, android::getBlob);
Mathias Agopianada798b2012-02-13 17:09:30 -080095 EGLint err = cnx->egl.eglGetError();
96 if (err != EGL_SUCCESS) {
97 ALOGE("eglSetBlobCacheFuncsANDROID resulted in an error: "
Yiwei Zhang8af03062020-08-12 21:28:15 -070098 "%#x",
99 err);
Jamie Gennisaca51c02011-11-03 17:42:43 -0700100 }
101 }
102 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800103
Jamie Gennis76601082011-11-06 14:14:33 -0800104 mInitialized = true;
105}
106
107void egl_cache_t::terminate() {
Mathias Agopian65421432017-03-08 11:49:05 -0800108 std::lock_guard<std::mutex> lock(mMutex);
Stan Iliev9e7cd072017-10-09 15:56:10 -0400109 if (mBlobCache) {
110 mBlobCache->writeToFile();
111 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700112 mBlobCache = nullptr;
Jamie Gennis76601082011-11-06 14:14:33 -0800113}
114
Yiwei Zhang8af03062020-08-12 21:28:15 -0700115void egl_cache_t::setBlob(const void* key, EGLsizeiANDROID keySize, const void* value,
116 EGLsizeiANDROID valueSize) {
Mathias Agopian65421432017-03-08 11:49:05 -0800117 std::lock_guard<std::mutex> lock(mMutex);
Jamie Gennis76601082011-11-06 14:14:33 -0800118
119 if (keySize < 0 || valueSize < 0) {
Steve Block32397c12012-01-05 23:22:43 +0000120 ALOGW("EGL_ANDROID_blob_cache set: negative sizes are not allowed");
Jamie Gennis76601082011-11-06 14:14:33 -0800121 return;
122 }
123
124 if (mInitialized) {
Mathias Agopianb7f9a242017-03-08 22:29:31 -0800125 BlobCache* bc = getBlobCacheLocked();
Jamie Gennis76601082011-11-06 14:14:33 -0800126 bc->set(key, keySize, value, valueSize);
Jamie Gennis99c3d702011-11-08 17:59:36 -0800127
128 if (!mSavePending) {
Jamie Gennis99c3d702011-11-08 17:59:36 -0800129 mSavePending = true;
Mathias Agopian65421432017-03-08 11:49:05 -0800130 std::thread deferredSaveThread([this]() {
131 sleep(deferredSaveDelay);
132 std::lock_guard<std::mutex> lock(mMutex);
Stan Iliev9e7cd072017-10-09 15:56:10 -0400133 if (mInitialized && mBlobCache) {
134 mBlobCache->writeToFile();
Mathias Agopian65421432017-03-08 11:49:05 -0800135 }
136 mSavePending = false;
137 });
138 deferredSaveThread.detach();
Jamie Gennis99c3d702011-11-08 17:59:36 -0800139 }
Jamie Gennis76601082011-11-06 14:14:33 -0800140 }
141}
142
Yiwei Zhang8af03062020-08-12 21:28:15 -0700143EGLsizeiANDROID egl_cache_t::getBlob(const void* key, EGLsizeiANDROID keySize, 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 0;
150 }
151
152 if (mInitialized) {
Mathias Agopianb7f9a242017-03-08 22:29:31 -0800153 BlobCache* bc = getBlobCacheLocked();
Jamie Gennis76601082011-11-06 14:14:33 -0800154 return bc->get(key, keySize, value, valueSize);
155 }
156 return 0;
157}
158
Jamie Gennis98c63832011-11-07 17:03:54 -0800159void egl_cache_t::setCacheFilename(const char* filename) {
Mathias Agopian65421432017-03-08 11:49:05 -0800160 std::lock_guard<std::mutex> lock(mMutex);
Jamie Gennis98c63832011-11-07 17:03:54 -0800161 mFilename = filename;
162}
163
Mathias Agopianb7f9a242017-03-08 22:29:31 -0800164BlobCache* egl_cache_t::getBlobCacheLocked() {
165 if (mBlobCache == nullptr) {
Stan Iliev9e7cd072017-10-09 15:56:10 -0400166 mBlobCache.reset(new FileBlobCache(maxKeySize, maxValueSize, maxTotalSize, mFilename));
Jamie Gennis76601082011-11-06 14:14:33 -0800167 }
Mathias Agopianb7f9a242017-03-08 22:29:31 -0800168 return mBlobCache.get();
Jamie Gennis76601082011-11-06 14:14:33 -0800169}
170
Jamie Gennisaca51c02011-11-03 17:42:43 -0700171}; // namespace android