2011-11-04 00:42:43 +00:00
|
|
|
/*
|
|
|
|
** Copyright 2011, The Android Open Source Project
|
|
|
|
**
|
|
|
|
** Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
** you may not use this file except in compliance with the License.
|
|
|
|
** You may obtain a copy of the License at
|
|
|
|
**
|
|
|
|
** http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
**
|
|
|
|
** Unless required by applicable law or agreed to in writing, software
|
|
|
|
** distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
** See the License for the specific language governing permissions and
|
|
|
|
** limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "egl_cache.h"
|
|
|
|
#include "egl_display.h"
|
|
|
|
#include "egl_impl.h"
|
|
|
|
#include "egldefs.h"
|
|
|
|
|
2011-11-08 01:03:54 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2011-11-20 00:25:24 +00:00
|
|
|
#ifndef MAX_EGL_CACHE_ENTRY_SIZE
|
|
|
|
#define MAX_EGL_CACHE_ENTRY_SIZE (16 * 1024);
|
|
|
|
#endif
|
|
|
|
|
2012-09-12 18:55:16 +00:00
|
|
|
#ifndef MAX_EGL_CACHE_KEY_SIZE
|
|
|
|
#define MAX_EGL_CACHE_KEY_SIZE (1024);
|
|
|
|
#endif
|
|
|
|
|
2011-11-20 00:25:24 +00:00
|
|
|
#ifndef MAX_EGL_CACHE_SIZE
|
|
|
|
#define MAX_EGL_CACHE_SIZE (64 * 1024);
|
|
|
|
#endif
|
|
|
|
|
2011-11-06 22:14:33 +00:00
|
|
|
// Cache size limits.
|
2012-09-12 18:55:16 +00:00
|
|
|
static const size_t maxKeySize = MAX_EGL_CACHE_KEY_SIZE;
|
2011-11-20 00:25:24 +00:00
|
|
|
static const size_t maxValueSize = MAX_EGL_CACHE_ENTRY_SIZE;
|
|
|
|
static const size_t maxTotalSize = MAX_EGL_CACHE_SIZE;
|
2011-11-06 22:14:33 +00:00
|
|
|
|
2011-11-08 01:03:54 +00:00
|
|
|
// Cache file header
|
|
|
|
static const char* cacheFileMagic = "EGL$";
|
|
|
|
static const size_t cacheFileHeaderSize = 8;
|
|
|
|
|
2011-11-09 01:59:36 +00:00
|
|
|
// The time in seconds to wait before saving newly inserted cache entries.
|
|
|
|
static const unsigned int deferredSaveDelay = 4;
|
|
|
|
|
2011-11-04 00:42:43 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
namespace android {
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#define BC_EXT_STR "EGL_ANDROID_blob_cache"
|
|
|
|
|
|
|
|
//
|
2011-11-06 22:14:33 +00:00
|
|
|
// Callback functions passed to EGL.
|
2011-11-04 00:42:43 +00:00
|
|
|
//
|
2011-11-09 23:35:34 +00:00
|
|
|
static void setBlob(const void* key, EGLsizeiANDROID keySize,
|
|
|
|
const void* value, EGLsizeiANDROID valueSize) {
|
2011-11-06 22:14:33 +00:00
|
|
|
egl_cache_t::get()->setBlob(key, keySize, value, valueSize);
|
|
|
|
}
|
2011-11-04 00:42:43 +00:00
|
|
|
|
2011-11-09 23:35:34 +00:00
|
|
|
static EGLsizeiANDROID getBlob(const void* key, EGLsizeiANDROID keySize,
|
|
|
|
void* value, EGLsizeiANDROID valueSize) {
|
2011-11-06 22:14:33 +00:00
|
|
|
return egl_cache_t::get()->getBlob(key, keySize, value, valueSize);
|
|
|
|
}
|
2011-11-04 00:42:43 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// egl_cache_t definition
|
|
|
|
//
|
2011-11-06 22:14:33 +00:00
|
|
|
egl_cache_t::egl_cache_t() :
|
|
|
|
mInitialized(false),
|
|
|
|
mBlobCache(NULL) {
|
2011-11-04 00:42:43 +00:00
|
|
|
}
|
|
|
|
|
2011-11-06 22:14:33 +00:00
|
|
|
egl_cache_t::~egl_cache_t() {
|
2011-11-04 00:42:43 +00:00
|
|
|
}
|
|
|
|
|
2011-11-08 01:03:54 +00:00
|
|
|
egl_cache_t egl_cache_t::sCache;
|
|
|
|
|
2011-11-04 00:42:43 +00:00
|
|
|
egl_cache_t* egl_cache_t::get() {
|
2011-11-08 01:03:54 +00:00
|
|
|
return &sCache;
|
2011-11-04 00:42:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void egl_cache_t::initialize(egl_display_t *display) {
|
2011-11-06 22:14:33 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
2012-02-14 01:09:30 +00:00
|
|
|
|
|
|
|
egl_connection_t* const cnx = &gEGLImpl;
|
|
|
|
if (cnx->dso && cnx->major >= 0 && cnx->minor >= 0) {
|
|
|
|
const char* exts = display->disp.queryString.extensions;
|
|
|
|
size_t bcExtLen = strlen(BC_EXT_STR);
|
|
|
|
size_t extsLen = strlen(exts);
|
|
|
|
bool equal = !strcmp(BC_EXT_STR, exts);
|
|
|
|
bool atStart = !strncmp(BC_EXT_STR " ", exts, bcExtLen+1);
|
|
|
|
bool atEnd = (bcExtLen+1) < extsLen &&
|
|
|
|
!strcmp(" " BC_EXT_STR, exts + extsLen - (bcExtLen+1));
|
|
|
|
bool inMiddle = strstr(exts, " " BC_EXT_STR " ");
|
|
|
|
if (equal || atStart || atEnd || inMiddle) {
|
|
|
|
PFNEGLSETBLOBCACHEFUNCSANDROIDPROC eglSetBlobCacheFuncsANDROID;
|
|
|
|
eglSetBlobCacheFuncsANDROID =
|
|
|
|
reinterpret_cast<PFNEGLSETBLOBCACHEFUNCSANDROIDPROC>(
|
2011-11-09 23:35:34 +00:00
|
|
|
cnx->egl.eglGetProcAddress(
|
|
|
|
"eglSetBlobCacheFuncsANDROID"));
|
2012-02-14 01:09:30 +00:00
|
|
|
if (eglSetBlobCacheFuncsANDROID == NULL) {
|
|
|
|
ALOGE("EGL_ANDROID_blob_cache advertised, "
|
|
|
|
"but unable to get eglSetBlobCacheFuncsANDROID");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
eglSetBlobCacheFuncsANDROID(display->disp.dpy,
|
|
|
|
android::setBlob, android::getBlob);
|
|
|
|
EGLint err = cnx->egl.eglGetError();
|
|
|
|
if (err != EGL_SUCCESS) {
|
|
|
|
ALOGE("eglSetBlobCacheFuncsANDROID resulted in an error: "
|
|
|
|
"%#x", err);
|
2011-11-04 00:42:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-02-14 01:09:30 +00:00
|
|
|
|
2011-11-06 22:14:33 +00:00
|
|
|
mInitialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void egl_cache_t::terminate() {
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
if (mBlobCache != NULL) {
|
|
|
|
saveBlobCacheLocked();
|
|
|
|
mBlobCache = NULL;
|
|
|
|
}
|
|
|
|
mInitialized = false;
|
|
|
|
}
|
|
|
|
|
2011-11-09 23:35:34 +00:00
|
|
|
void egl_cache_t::setBlob(const void* key, EGLsizeiANDROID keySize,
|
|
|
|
const void* value, EGLsizeiANDROID valueSize) {
|
2011-11-06 22:14:33 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
|
|
|
|
if (keySize < 0 || valueSize < 0) {
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW("EGL_ANDROID_blob_cache set: negative sizes are not allowed");
|
2011-11-06 22:14:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mInitialized) {
|
|
|
|
sp<BlobCache> bc = getBlobCacheLocked();
|
|
|
|
bc->set(key, keySize, value, valueSize);
|
2011-11-09 01:59:36 +00:00
|
|
|
|
|
|
|
if (!mSavePending) {
|
|
|
|
class DeferredSaveThread : public Thread {
|
|
|
|
public:
|
|
|
|
DeferredSaveThread() : Thread(false) {}
|
|
|
|
|
|
|
|
virtual bool threadLoop() {
|
|
|
|
sleep(deferredSaveDelay);
|
|
|
|
egl_cache_t* c = egl_cache_t::get();
|
|
|
|
Mutex::Autolock lock(c->mMutex);
|
|
|
|
if (c->mInitialized) {
|
|
|
|
c->saveBlobCacheLocked();
|
|
|
|
}
|
|
|
|
c->mSavePending = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// The thread will hold a strong ref to itself until it has finished
|
|
|
|
// running, so there's no need to keep a ref around.
|
|
|
|
sp<Thread> deferredSaveThread(new DeferredSaveThread());
|
|
|
|
mSavePending = true;
|
|
|
|
deferredSaveThread->run();
|
|
|
|
}
|
2011-11-06 22:14:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-09 23:35:34 +00:00
|
|
|
EGLsizeiANDROID egl_cache_t::getBlob(const void* key, EGLsizeiANDROID keySize,
|
|
|
|
void* value, EGLsizeiANDROID valueSize) {
|
2011-11-06 22:14:33 +00:00
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
|
|
|
|
if (keySize < 0 || valueSize < 0) {
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW("EGL_ANDROID_blob_cache set: negative sizes are not allowed");
|
2011-11-06 22:14:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mInitialized) {
|
|
|
|
sp<BlobCache> bc = getBlobCacheLocked();
|
|
|
|
return bc->get(key, keySize, value, valueSize);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-08 01:03:54 +00:00
|
|
|
void egl_cache_t::setCacheFilename(const char* filename) {
|
|
|
|
Mutex::Autolock lock(mMutex);
|
|
|
|
mFilename = filename;
|
|
|
|
}
|
|
|
|
|
2011-11-06 22:14:33 +00:00
|
|
|
sp<BlobCache> egl_cache_t::getBlobCacheLocked() {
|
|
|
|
if (mBlobCache == NULL) {
|
|
|
|
mBlobCache = new BlobCache(maxKeySize, maxValueSize, maxTotalSize);
|
|
|
|
loadBlobCacheLocked();
|
|
|
|
}
|
|
|
|
return mBlobCache;
|
|
|
|
}
|
|
|
|
|
2011-11-08 01:03:54 +00:00
|
|
|
static uint32_t crc32c(const uint8_t* buf, size_t len) {
|
|
|
|
const uint32_t polyBits = 0x82F63B78;
|
|
|
|
uint32_t r = 0;
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
|
|
r ^= buf[i];
|
|
|
|
for (int j = 0; j < 8; j++) {
|
|
|
|
if (r & 1) {
|
|
|
|
r = (r >> 1) ^ polyBits;
|
|
|
|
} else {
|
|
|
|
r >>= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2011-11-06 22:14:33 +00:00
|
|
|
void egl_cache_t::saveBlobCacheLocked() {
|
2011-11-08 01:03:54 +00:00
|
|
|
if (mFilename.length() > 0) {
|
|
|
|
size_t cacheSize = mBlobCache->getFlattenedSize();
|
|
|
|
size_t headerSize = cacheFileHeaderSize;
|
|
|
|
const char* fname = mFilename.string();
|
|
|
|
|
|
|
|
// Try to create the file with no permissions so we can write it
|
|
|
|
// without anyone trying to read it.
|
|
|
|
int fd = open(fname, O_CREAT | O_EXCL | O_RDWR, 0);
|
|
|
|
if (fd == -1) {
|
|
|
|
if (errno == EEXIST) {
|
|
|
|
// The file exists, delete it and try again.
|
|
|
|
if (unlink(fname) == -1) {
|
|
|
|
// No point in retrying if the unlink failed.
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("error unlinking cache file %s: %s (%d)", fname,
|
2011-11-08 01:03:54 +00:00
|
|
|
strerror(errno), errno);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Retry now that we've unlinked the file.
|
|
|
|
fd = open(fname, O_CREAT | O_EXCL | O_RDWR, 0);
|
|
|
|
}
|
|
|
|
if (fd == -1) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("error creating cache file %s: %s (%d)", fname,
|
2011-11-08 01:03:54 +00:00
|
|
|
strerror(errno), errno);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t fileSize = headerSize + cacheSize;
|
|
|
|
|
2012-06-27 23:14:42 +00:00
|
|
|
uint8_t* buf = new uint8_t [fileSize];
|
|
|
|
if (!buf) {
|
|
|
|
ALOGE("error allocating buffer for cache contents: %s (%d)",
|
|
|
|
strerror(errno), errno);
|
2011-11-08 01:03:54 +00:00
|
|
|
close(fd);
|
|
|
|
unlink(fname);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t err = mBlobCache->flatten(buf + headerSize, cacheSize, NULL,
|
|
|
|
0);
|
|
|
|
if (err != OK) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("error writing cache contents: %s (%d)", strerror(-err),
|
2011-11-08 01:03:54 +00:00
|
|
|
-err);
|
2012-06-27 23:14:42 +00:00
|
|
|
delete [] buf;
|
2011-11-08 01:03:54 +00:00
|
|
|
close(fd);
|
|
|
|
unlink(fname);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the file magic and CRC
|
|
|
|
memcpy(buf, cacheFileMagic, 4);
|
|
|
|
uint32_t* crc = reinterpret_cast<uint32_t*>(buf + 4);
|
|
|
|
*crc = crc32c(buf + headerSize, cacheSize);
|
|
|
|
|
2012-06-27 23:14:42 +00:00
|
|
|
if (write(fd, buf, fileSize) == -1) {
|
|
|
|
ALOGE("error writing cache file: %s (%d)", strerror(errno),
|
|
|
|
errno);
|
|
|
|
delete [] buf;
|
|
|
|
close(fd);
|
|
|
|
unlink(fname);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete [] buf;
|
2011-11-08 01:03:54 +00:00
|
|
|
fchmod(fd, S_IRUSR);
|
|
|
|
close(fd);
|
|
|
|
}
|
2011-11-06 22:14:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void egl_cache_t::loadBlobCacheLocked() {
|
2011-11-08 01:03:54 +00:00
|
|
|
if (mFilename.length() > 0) {
|
|
|
|
size_t headerSize = cacheFileHeaderSize;
|
|
|
|
|
|
|
|
int fd = open(mFilename.string(), O_RDONLY, 0);
|
|
|
|
if (fd == -1) {
|
|
|
|
if (errno != ENOENT) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("error opening cache file %s: %s (%d)", mFilename.string(),
|
2011-11-08 01:03:54 +00:00
|
|
|
strerror(errno), errno);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct stat statBuf;
|
|
|
|
if (fstat(fd, &statBuf) == -1) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("error stat'ing cache file: %s (%d)", strerror(errno), errno);
|
2011-11-08 01:03:54 +00:00
|
|
|
close(fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sanity check the size before trying to mmap it.
|
|
|
|
size_t fileSize = statBuf.st_size;
|
|
|
|
if (fileSize > maxTotalSize * 2) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("cache file is too large: %#llx", statBuf.st_size);
|
2011-11-08 01:03:54 +00:00
|
|
|
close(fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* buf = reinterpret_cast<uint8_t*>(mmap(NULL, fileSize,
|
|
|
|
PROT_READ, MAP_PRIVATE, fd, 0));
|
|
|
|
if (buf == MAP_FAILED) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("error mmaping cache file: %s (%d)", strerror(errno),
|
2011-11-08 01:03:54 +00:00
|
|
|
errno);
|
|
|
|
close(fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the file magic and CRC
|
|
|
|
size_t cacheSize = fileSize - headerSize;
|
|
|
|
if (memcmp(buf, cacheFileMagic, 4) != 0) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("cache file has bad mojo");
|
2011-11-08 01:03:54 +00:00
|
|
|
close(fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
uint32_t* crc = reinterpret_cast<uint32_t*>(buf + 4);
|
|
|
|
if (crc32c(buf + headerSize, cacheSize) != *crc) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("cache file failed CRC check");
|
2011-11-08 01:03:54 +00:00
|
|
|
close(fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-11-09 23:35:34 +00:00
|
|
|
status_t err = mBlobCache->unflatten(buf + headerSize, cacheSize, NULL,
|
|
|
|
0);
|
2011-11-08 01:03:54 +00:00
|
|
|
if (err != OK) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("error reading cache contents: %s (%d)", strerror(-err),
|
2011-11-08 01:03:54 +00:00
|
|
|
-err);
|
|
|
|
munmap(buf, fileSize);
|
|
|
|
close(fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
munmap(buf, fileSize);
|
|
|
|
close(fd);
|
|
|
|
}
|
2011-11-04 00:42:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
}; // namespace android
|
|
|
|
// ----------------------------------------------------------------------------
|