blob: 1607267bf10bb3ad1bd583b166437801a323ecf1 [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 Agopian39c24a22013-04-04 23:17:56 -070017#include "../egl_impl.h"
18
Jamie Gennisaca51c02011-11-03 17:42:43 -070019#include "egl_cache.h"
20#include "egl_display.h"
Jamie Gennisaca51c02011-11-03 17:42:43 -070021#include "egldefs.h"
22
Jamie Gennis98c63832011-11-07 17:03:54 -080023#include <fcntl.h>
24#include <sys/mman.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27#include <unistd.h>
28
Jamie Gennis89c1d612011-11-19 16:25:24 -080029#ifndef MAX_EGL_CACHE_ENTRY_SIZE
30#define MAX_EGL_CACHE_ENTRY_SIZE (16 * 1024);
31#endif
32
Jamie Gennisf478e6d2012-09-12 11:55:16 -070033#ifndef MAX_EGL_CACHE_KEY_SIZE
34#define MAX_EGL_CACHE_KEY_SIZE (1024);
35#endif
36
Jamie Gennis89c1d612011-11-19 16:25:24 -080037#ifndef MAX_EGL_CACHE_SIZE
38#define MAX_EGL_CACHE_SIZE (64 * 1024);
39#endif
40
Jamie Gennis76601082011-11-06 14:14:33 -080041// Cache size limits.
Jamie Gennisf478e6d2012-09-12 11:55:16 -070042static const size_t maxKeySize = MAX_EGL_CACHE_KEY_SIZE;
Jamie Gennis89c1d612011-11-19 16:25:24 -080043static const size_t maxValueSize = MAX_EGL_CACHE_ENTRY_SIZE;
44static const size_t maxTotalSize = MAX_EGL_CACHE_SIZE;
Jamie Gennis76601082011-11-06 14:14:33 -080045
Jamie Gennis98c63832011-11-07 17:03:54 -080046// Cache file header
47static const char* cacheFileMagic = "EGL$";
48static const size_t cacheFileHeaderSize = 8;
49
Jamie Gennis99c3d702011-11-08 17:59:36 -080050// The time in seconds to wait before saving newly inserted cache entries.
51static const unsigned int deferredSaveDelay = 4;
52
Jamie Gennisaca51c02011-11-03 17:42:43 -070053// ----------------------------------------------------------------------------
54namespace android {
55// ----------------------------------------------------------------------------
56
57#define BC_EXT_STR "EGL_ANDROID_blob_cache"
58
59//
Jamie Gennis76601082011-11-06 14:14:33 -080060// Callback functions passed to EGL.
Jamie Gennisaca51c02011-11-03 17:42:43 -070061//
Jamie Gennisc42fcf02011-11-09 15:35:34 -080062static void setBlob(const void* key, EGLsizeiANDROID keySize,
63 const void* value, EGLsizeiANDROID valueSize) {
Jamie Gennis76601082011-11-06 14:14:33 -080064 egl_cache_t::get()->setBlob(key, keySize, value, valueSize);
Jamie Gennisaca51c02011-11-03 17:42:43 -070065}
66
Jamie Gennisc42fcf02011-11-09 15:35:34 -080067static EGLsizeiANDROID getBlob(const void* key, EGLsizeiANDROID keySize,
68 void* value, EGLsizeiANDROID valueSize) {
Jamie Gennis76601082011-11-06 14:14:33 -080069 return egl_cache_t::get()->getBlob(key, keySize, value, valueSize);
70}
71
72//
73// egl_cache_t definition
74//
75egl_cache_t::egl_cache_t() :
76 mInitialized(false),
77 mBlobCache(NULL) {
78}
79
80egl_cache_t::~egl_cache_t() {
Jamie Gennisaca51c02011-11-03 17:42:43 -070081}
82
Jamie Gennis98c63832011-11-07 17:03:54 -080083egl_cache_t egl_cache_t::sCache;
84
Jamie Gennisaca51c02011-11-03 17:42:43 -070085egl_cache_t* egl_cache_t::get() {
Jamie Gennis98c63832011-11-07 17:03:54 -080086 return &sCache;
Jamie Gennisaca51c02011-11-03 17:42:43 -070087}
88
89void egl_cache_t::initialize(egl_display_t *display) {
Jamie Gennis76601082011-11-06 14:14:33 -080090 Mutex::Autolock lock(mMutex);
Mathias Agopianada798b2012-02-13 17:09:30 -080091
92 egl_connection_t* const cnx = &gEGLImpl;
93 if (cnx->dso && cnx->major >= 0 && cnx->minor >= 0) {
94 const char* exts = display->disp.queryString.extensions;
95 size_t bcExtLen = strlen(BC_EXT_STR);
96 size_t extsLen = strlen(exts);
97 bool equal = !strcmp(BC_EXT_STR, exts);
98 bool atStart = !strncmp(BC_EXT_STR " ", exts, bcExtLen+1);
99 bool atEnd = (bcExtLen+1) < extsLen &&
100 !strcmp(" " BC_EXT_STR, exts + extsLen - (bcExtLen+1));
101 bool inMiddle = strstr(exts, " " BC_EXT_STR " ");
102 if (equal || atStart || atEnd || inMiddle) {
103 PFNEGLSETBLOBCACHEFUNCSANDROIDPROC eglSetBlobCacheFuncsANDROID;
104 eglSetBlobCacheFuncsANDROID =
105 reinterpret_cast<PFNEGLSETBLOBCACHEFUNCSANDROIDPROC>(
Jamie Gennisc42fcf02011-11-09 15:35:34 -0800106 cnx->egl.eglGetProcAddress(
107 "eglSetBlobCacheFuncsANDROID"));
Mathias Agopianada798b2012-02-13 17:09:30 -0800108 if (eglSetBlobCacheFuncsANDROID == NULL) {
109 ALOGE("EGL_ANDROID_blob_cache advertised, "
110 "but unable to get eglSetBlobCacheFuncsANDROID");
111 return;
112 }
Jamie Gennisaca51c02011-11-03 17:42:43 -0700113
Mathias Agopianada798b2012-02-13 17:09:30 -0800114 eglSetBlobCacheFuncsANDROID(display->disp.dpy,
115 android::setBlob, android::getBlob);
116 EGLint err = cnx->egl.eglGetError();
117 if (err != EGL_SUCCESS) {
118 ALOGE("eglSetBlobCacheFuncsANDROID resulted in an error: "
119 "%#x", err);
Jamie Gennisaca51c02011-11-03 17:42:43 -0700120 }
121 }
122 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800123
Jamie Gennis76601082011-11-06 14:14:33 -0800124 mInitialized = true;
125}
126
127void egl_cache_t::terminate() {
128 Mutex::Autolock lock(mMutex);
Jamie Gennis5539e212013-07-30 15:10:02 -0700129 saveBlobCacheLocked();
130 mBlobCache = NULL;
Jamie Gennis76601082011-11-06 14:14:33 -0800131}
132
Jamie Gennisc42fcf02011-11-09 15:35:34 -0800133void egl_cache_t::setBlob(const void* key, EGLsizeiANDROID keySize,
134 const void* value, EGLsizeiANDROID valueSize) {
Jamie Gennis76601082011-11-06 14:14:33 -0800135 Mutex::Autolock lock(mMutex);
136
137 if (keySize < 0 || valueSize < 0) {
Steve Block32397c12012-01-05 23:22:43 +0000138 ALOGW("EGL_ANDROID_blob_cache set: negative sizes are not allowed");
Jamie Gennis76601082011-11-06 14:14:33 -0800139 return;
140 }
141
142 if (mInitialized) {
143 sp<BlobCache> bc = getBlobCacheLocked();
144 bc->set(key, keySize, value, valueSize);
Jamie Gennis99c3d702011-11-08 17:59:36 -0800145
146 if (!mSavePending) {
147 class DeferredSaveThread : public Thread {
148 public:
149 DeferredSaveThread() : Thread(false) {}
150
151 virtual bool threadLoop() {
152 sleep(deferredSaveDelay);
153 egl_cache_t* c = egl_cache_t::get();
154 Mutex::Autolock lock(c->mMutex);
155 if (c->mInitialized) {
156 c->saveBlobCacheLocked();
157 }
158 c->mSavePending = false;
159 return false;
160 }
161 };
162
163 // The thread will hold a strong ref to itself until it has finished
164 // running, so there's no need to keep a ref around.
165 sp<Thread> deferredSaveThread(new DeferredSaveThread());
166 mSavePending = true;
167 deferredSaveThread->run();
168 }
Jamie Gennis76601082011-11-06 14:14:33 -0800169 }
170}
171
Jamie Gennisc42fcf02011-11-09 15:35:34 -0800172EGLsizeiANDROID egl_cache_t::getBlob(const void* key, EGLsizeiANDROID keySize,
173 void* value, EGLsizeiANDROID valueSize) {
Jamie Gennis76601082011-11-06 14:14:33 -0800174 Mutex::Autolock lock(mMutex);
175
176 if (keySize < 0 || valueSize < 0) {
Steve Block32397c12012-01-05 23:22:43 +0000177 ALOGW("EGL_ANDROID_blob_cache set: negative sizes are not allowed");
Jamie Gennis76601082011-11-06 14:14:33 -0800178 return 0;
179 }
180
181 if (mInitialized) {
182 sp<BlobCache> bc = getBlobCacheLocked();
183 return bc->get(key, keySize, value, valueSize);
184 }
185 return 0;
186}
187
Jamie Gennis98c63832011-11-07 17:03:54 -0800188void egl_cache_t::setCacheFilename(const char* filename) {
189 Mutex::Autolock lock(mMutex);
190 mFilename = filename;
191}
192
Jamie Gennis76601082011-11-06 14:14:33 -0800193sp<BlobCache> egl_cache_t::getBlobCacheLocked() {
194 if (mBlobCache == NULL) {
195 mBlobCache = new BlobCache(maxKeySize, maxValueSize, maxTotalSize);
196 loadBlobCacheLocked();
197 }
198 return mBlobCache;
199}
200
Jamie Gennis98c63832011-11-07 17:03:54 -0800201static uint32_t crc32c(const uint8_t* buf, size_t len) {
202 const uint32_t polyBits = 0x82F63B78;
203 uint32_t r = 0;
204 for (size_t i = 0; i < len; i++) {
205 r ^= buf[i];
206 for (int j = 0; j < 8; j++) {
207 if (r & 1) {
208 r = (r >> 1) ^ polyBits;
209 } else {
210 r >>= 1;
211 }
212 }
213 }
214 return r;
215}
216
Jamie Gennis76601082011-11-06 14:14:33 -0800217void egl_cache_t::saveBlobCacheLocked() {
Jamie Gennis5539e212013-07-30 15:10:02 -0700218 if (mFilename.length() > 0 && mBlobCache != NULL) {
Jamie Gennis98c63832011-11-07 17:03:54 -0800219 size_t cacheSize = mBlobCache->getFlattenedSize();
220 size_t headerSize = cacheFileHeaderSize;
221 const char* fname = mFilename.string();
222
223 // Try to create the file with no permissions so we can write it
224 // without anyone trying to read it.
225 int fd = open(fname, O_CREAT | O_EXCL | O_RDWR, 0);
226 if (fd == -1) {
227 if (errno == EEXIST) {
228 // The file exists, delete it and try again.
229 if (unlink(fname) == -1) {
230 // No point in retrying if the unlink failed.
Steve Blocke6f43dd2012-01-06 19:20:56 +0000231 ALOGE("error unlinking cache file %s: %s (%d)", fname,
Jamie Gennis98c63832011-11-07 17:03:54 -0800232 strerror(errno), errno);
233 return;
234 }
235 // Retry now that we've unlinked the file.
236 fd = open(fname, O_CREAT | O_EXCL | O_RDWR, 0);
237 }
238 if (fd == -1) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000239 ALOGE("error creating cache file %s: %s (%d)", fname,
Jamie Gennis98c63832011-11-07 17:03:54 -0800240 strerror(errno), errno);
241 return;
242 }
243 }
244
245 size_t fileSize = headerSize + cacheSize;
Jamie Gennis98c63832011-11-07 17:03:54 -0800246
vijay guptaa30cc7d2012-06-27 16:14:42 -0700247 uint8_t* buf = new uint8_t [fileSize];
248 if (!buf) {
249 ALOGE("error allocating buffer for cache contents: %s (%d)",
250 strerror(errno), errno);
Jamie Gennis98c63832011-11-07 17:03:54 -0800251 close(fd);
252 unlink(fname);
253 return;
254 }
255
256 status_t err = mBlobCache->flatten(buf + headerSize, cacheSize, NULL,
257 0);
258 if (err != OK) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000259 ALOGE("error writing cache contents: %s (%d)", strerror(-err),
Jamie Gennis98c63832011-11-07 17:03:54 -0800260 -err);
vijay guptaa30cc7d2012-06-27 16:14:42 -0700261 delete [] buf;
Jamie Gennis98c63832011-11-07 17:03:54 -0800262 close(fd);
263 unlink(fname);
264 return;
265 }
266
267 // Write the file magic and CRC
268 memcpy(buf, cacheFileMagic, 4);
269 uint32_t* crc = reinterpret_cast<uint32_t*>(buf + 4);
270 *crc = crc32c(buf + headerSize, cacheSize);
271
vijay guptaa30cc7d2012-06-27 16:14:42 -0700272 if (write(fd, buf, fileSize) == -1) {
273 ALOGE("error writing cache file: %s (%d)", strerror(errno),
274 errno);
275 delete [] buf;
276 close(fd);
277 unlink(fname);
278 return;
279 }
280
281 delete [] buf;
Jamie Gennis98c63832011-11-07 17:03:54 -0800282 fchmod(fd, S_IRUSR);
283 close(fd);
284 }
Jamie Gennis76601082011-11-06 14:14:33 -0800285}
286
287void egl_cache_t::loadBlobCacheLocked() {
Jamie Gennis98c63832011-11-07 17:03:54 -0800288 if (mFilename.length() > 0) {
289 size_t headerSize = cacheFileHeaderSize;
290
291 int fd = open(mFilename.string(), O_RDONLY, 0);
292 if (fd == -1) {
293 if (errno != ENOENT) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000294 ALOGE("error opening cache file %s: %s (%d)", mFilename.string(),
Jamie Gennis98c63832011-11-07 17:03:54 -0800295 strerror(errno), errno);
296 }
297 return;
298 }
299
300 struct stat statBuf;
301 if (fstat(fd, &statBuf) == -1) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000302 ALOGE("error stat'ing cache file: %s (%d)", strerror(errno), errno);
Jamie Gennis98c63832011-11-07 17:03:54 -0800303 close(fd);
304 return;
305 }
306
307 // Sanity check the size before trying to mmap it.
308 size_t fileSize = statBuf.st_size;
309 if (fileSize > maxTotalSize * 2) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000310 ALOGE("cache file is too large: %#llx", statBuf.st_size);
Jamie Gennis98c63832011-11-07 17:03:54 -0800311 close(fd);
312 return;
313 }
314
315 uint8_t* buf = reinterpret_cast<uint8_t*>(mmap(NULL, fileSize,
316 PROT_READ, MAP_PRIVATE, fd, 0));
317 if (buf == MAP_FAILED) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000318 ALOGE("error mmaping cache file: %s (%d)", strerror(errno),
Jamie Gennis98c63832011-11-07 17:03:54 -0800319 errno);
320 close(fd);
321 return;
322 }
323
324 // Check the file magic and CRC
325 size_t cacheSize = fileSize - headerSize;
326 if (memcmp(buf, cacheFileMagic, 4) != 0) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000327 ALOGE("cache file has bad mojo");
Jamie Gennis98c63832011-11-07 17:03:54 -0800328 close(fd);
329 return;
330 }
331 uint32_t* crc = reinterpret_cast<uint32_t*>(buf + 4);
332 if (crc32c(buf + headerSize, cacheSize) != *crc) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000333 ALOGE("cache file failed CRC check");
Jamie Gennis98c63832011-11-07 17:03:54 -0800334 close(fd);
335 return;
336 }
337
Jamie Gennisc42fcf02011-11-09 15:35:34 -0800338 status_t err = mBlobCache->unflatten(buf + headerSize, cacheSize, NULL,
339 0);
Jamie Gennis98c63832011-11-07 17:03:54 -0800340 if (err != OK) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000341 ALOGE("error reading cache contents: %s (%d)", strerror(-err),
Jamie Gennis98c63832011-11-07 17:03:54 -0800342 -err);
343 munmap(buf, fileSize);
344 close(fd);
345 return;
346 }
347
348 munmap(buf, fileSize);
349 close(fd);
350 }
Jamie Gennisaca51c02011-11-03 17:42:43 -0700351}
352
353// ----------------------------------------------------------------------------
354}; // namespace android
355// ----------------------------------------------------------------------------