blob: bcf496164b77d72820145432dd8707e0146a2a90 [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
Mathias Agopian39c24a22013-04-04 23:17:56 -070019#include "../egl_impl.h"
20
Jamie Gennisaca51c02011-11-03 17:42:43 -070021#include "egl_display.h"
Jamie Gennisaca51c02011-11-03 17:42:43 -070022
Mathias Agopianb7f9a242017-03-08 22:29:31 -080023#include <private/EGL/cache.h>
24
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
29#include <log/log.h>
Jamie Gennis98c63832011-11-07 17:03:54 -080030
Jamie Gennis76601082011-11-06 14:14:33 -080031// Cache size limits.
Dan Willemsenbace39e2016-10-11 15:42:59 -070032static const size_t maxKeySize = 12 * 1024;
33static const size_t maxValueSize = 64 * 1024;
34static const size_t maxTotalSize = 2 * 1024 * 1024;
Jamie Gennis76601082011-11-06 14:14:33 -080035
Jamie Gennis99c3d702011-11-08 17:59:36 -080036// The time in seconds to wait before saving newly inserted cache entries.
37static const unsigned int deferredSaveDelay = 4;
38
Jamie Gennisaca51c02011-11-03 17:42:43 -070039// ----------------------------------------------------------------------------
40namespace android {
41// ----------------------------------------------------------------------------
42
43#define BC_EXT_STR "EGL_ANDROID_blob_cache"
44
Mathias Agopianb7f9a242017-03-08 22:29:31 -080045// called from android_view_ThreadedRenderer.cpp
46void egl_set_cache_filename(const char* filename) {
47 egl_cache_t::get()->setCacheFilename(filename);
48}
49
Jamie Gennisaca51c02011-11-03 17:42:43 -070050//
Jamie Gennis76601082011-11-06 14:14:33 -080051// Callback functions passed to EGL.
Jamie Gennisaca51c02011-11-03 17:42:43 -070052//
Jamie Gennisc42fcf02011-11-09 15:35:34 -080053static void setBlob(const void* key, EGLsizeiANDROID keySize,
54 const void* value, EGLsizeiANDROID valueSize) {
Jamie Gennis76601082011-11-06 14:14:33 -080055 egl_cache_t::get()->setBlob(key, keySize, value, valueSize);
Jamie Gennisaca51c02011-11-03 17:42:43 -070056}
57
Jamie Gennisc42fcf02011-11-09 15:35:34 -080058static EGLsizeiANDROID getBlob(const void* key, EGLsizeiANDROID keySize,
59 void* value, EGLsizeiANDROID valueSize) {
Jamie Gennis76601082011-11-06 14:14:33 -080060 return egl_cache_t::get()->getBlob(key, keySize, value, valueSize);
61}
62
63//
64// egl_cache_t definition
65//
66egl_cache_t::egl_cache_t() :
Mathias Agopianb7f9a242017-03-08 22:29:31 -080067 mInitialized(false) {
Jamie Gennis76601082011-11-06 14:14:33 -080068}
69
70egl_cache_t::~egl_cache_t() {
Jamie Gennisaca51c02011-11-03 17:42:43 -070071}
72
Jamie Gennis98c63832011-11-07 17:03:54 -080073egl_cache_t egl_cache_t::sCache;
74
Jamie Gennisaca51c02011-11-03 17:42:43 -070075egl_cache_t* egl_cache_t::get() {
Jamie Gennis98c63832011-11-07 17:03:54 -080076 return &sCache;
Jamie Gennisaca51c02011-11-03 17:42:43 -070077}
78
79void egl_cache_t::initialize(egl_display_t *display) {
Mathias Agopian65421432017-03-08 11:49:05 -080080 std::lock_guard<std::mutex> lock(mMutex);
Mathias Agopianada798b2012-02-13 17:09:30 -080081
82 egl_connection_t* const cnx = &gEGLImpl;
83 if (cnx->dso && cnx->major >= 0 && cnx->minor >= 0) {
84 const char* exts = display->disp.queryString.extensions;
85 size_t bcExtLen = strlen(BC_EXT_STR);
86 size_t extsLen = strlen(exts);
87 bool equal = !strcmp(BC_EXT_STR, exts);
88 bool atStart = !strncmp(BC_EXT_STR " ", exts, bcExtLen+1);
89 bool atEnd = (bcExtLen+1) < extsLen &&
90 !strcmp(" " BC_EXT_STR, exts + extsLen - (bcExtLen+1));
Mathias Agopian311b4792017-02-28 15:00:49 -080091 bool inMiddle = strstr(exts, " " BC_EXT_STR " ") != nullptr;
Mathias Agopianada798b2012-02-13 17:09:30 -080092 if (equal || atStart || atEnd || inMiddle) {
93 PFNEGLSETBLOBCACHEFUNCSANDROIDPROC eglSetBlobCacheFuncsANDROID;
94 eglSetBlobCacheFuncsANDROID =
95 reinterpret_cast<PFNEGLSETBLOBCACHEFUNCSANDROIDPROC>(
Jamie Gennisc42fcf02011-11-09 15:35:34 -080096 cnx->egl.eglGetProcAddress(
97 "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, "
100 "but unable to get eglSetBlobCacheFuncsANDROID");
101 return;
102 }
Jamie Gennisaca51c02011-11-03 17:42:43 -0700103
Mathias Agopianada798b2012-02-13 17:09:30 -0800104 eglSetBlobCacheFuncsANDROID(display->disp.dpy,
105 android::setBlob, android::getBlob);
106 EGLint err = cnx->egl.eglGetError();
107 if (err != EGL_SUCCESS) {
108 ALOGE("eglSetBlobCacheFuncsANDROID resulted in an error: "
109 "%#x", err);
Jamie Gennisaca51c02011-11-03 17:42:43 -0700110 }
111 }
112 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800113
Jamie Gennis76601082011-11-06 14:14:33 -0800114 mInitialized = true;
115}
116
117void egl_cache_t::terminate() {
Mathias Agopian65421432017-03-08 11:49:05 -0800118 std::lock_guard<std::mutex> lock(mMutex);
Stan Iliev9e7cd072017-10-09 15:56:10 -0400119 if (mBlobCache) {
120 mBlobCache->writeToFile();
121 }
Yi Kong48a6cd22018-07-18 10:07:09 -0700122 mBlobCache = nullptr;
Jamie Gennis76601082011-11-06 14:14:33 -0800123}
124
Jamie Gennisc42fcf02011-11-09 15:35:34 -0800125void egl_cache_t::setBlob(const void* key, EGLsizeiANDROID keySize,
126 const void* value, EGLsizeiANDROID valueSize) {
Mathias Agopian65421432017-03-08 11:49:05 -0800127 std::lock_guard<std::mutex> lock(mMutex);
Jamie Gennis76601082011-11-06 14:14:33 -0800128
129 if (keySize < 0 || valueSize < 0) {
Steve Block32397c12012-01-05 23:22:43 +0000130 ALOGW("EGL_ANDROID_blob_cache set: negative sizes are not allowed");
Jamie Gennis76601082011-11-06 14:14:33 -0800131 return;
132 }
133
134 if (mInitialized) {
Mathias Agopianb7f9a242017-03-08 22:29:31 -0800135 BlobCache* bc = getBlobCacheLocked();
Jamie Gennis76601082011-11-06 14:14:33 -0800136 bc->set(key, keySize, value, valueSize);
Jamie Gennis99c3d702011-11-08 17:59:36 -0800137
138 if (!mSavePending) {
Jamie Gennis99c3d702011-11-08 17:59:36 -0800139 mSavePending = true;
Mathias Agopian65421432017-03-08 11:49:05 -0800140 std::thread deferredSaveThread([this]() {
141 sleep(deferredSaveDelay);
142 std::lock_guard<std::mutex> lock(mMutex);
Stan Iliev9e7cd072017-10-09 15:56:10 -0400143 if (mInitialized && mBlobCache) {
144 mBlobCache->writeToFile();
Mathias Agopian65421432017-03-08 11:49:05 -0800145 }
146 mSavePending = false;
147 });
148 deferredSaveThread.detach();
Jamie Gennis99c3d702011-11-08 17:59:36 -0800149 }
Jamie Gennis76601082011-11-06 14:14:33 -0800150 }
151}
152
Jamie Gennisc42fcf02011-11-09 15:35:34 -0800153EGLsizeiANDROID egl_cache_t::getBlob(const void* key, EGLsizeiANDROID keySize,
154 void* value, EGLsizeiANDROID valueSize) {
Mathias Agopian65421432017-03-08 11:49:05 -0800155 std::lock_guard<std::mutex> lock(mMutex);
Jamie Gennis76601082011-11-06 14:14:33 -0800156
157 if (keySize < 0 || valueSize < 0) {
Steve Block32397c12012-01-05 23:22:43 +0000158 ALOGW("EGL_ANDROID_blob_cache set: negative sizes are not allowed");
Jamie Gennis76601082011-11-06 14:14:33 -0800159 return 0;
160 }
161
162 if (mInitialized) {
Mathias Agopianb7f9a242017-03-08 22:29:31 -0800163 BlobCache* bc = getBlobCacheLocked();
Jamie Gennis76601082011-11-06 14:14:33 -0800164 return bc->get(key, keySize, value, valueSize);
165 }
166 return 0;
167}
168
Jamie Gennis98c63832011-11-07 17:03:54 -0800169void egl_cache_t::setCacheFilename(const char* filename) {
Mathias Agopian65421432017-03-08 11:49:05 -0800170 std::lock_guard<std::mutex> lock(mMutex);
Jamie Gennis98c63832011-11-07 17:03:54 -0800171 mFilename = filename;
172}
173
Mathias Agopianb7f9a242017-03-08 22:29:31 -0800174BlobCache* egl_cache_t::getBlobCacheLocked() {
175 if (mBlobCache == nullptr) {
Stan Iliev9e7cd072017-10-09 15:56:10 -0400176 mBlobCache.reset(new FileBlobCache(maxKeySize, maxValueSize, maxTotalSize, mFilename));
Jamie Gennis76601082011-11-06 14:14:33 -0800177 }
Mathias Agopianb7f9a242017-03-08 22:29:31 -0800178 return mBlobCache.get();
Jamie Gennis76601082011-11-06 14:14:33 -0800179}
180
Jamie Gennisaca51c02011-11-03 17:42:43 -0700181// ----------------------------------------------------------------------------
182}; // namespace android
183// ----------------------------------------------------------------------------