summaryrefslogtreecommitdiffstats
path: root/opengl/libs
diff options
context:
space:
mode:
Diffstat (limited to 'opengl/libs')
-rw-r--r--opengl/libs/Android.mk2
-rw-r--r--opengl/libs/EGL/egl.cpp14
-rw-r--r--opengl/libs/EGL/eglApi.cpp99
-rw-r--r--opengl/libs/EGL/egl_cache.cpp344
-rw-r--r--opengl/libs/EGL/egl_cache.h130
-rw-r--r--opengl/libs/EGL/egl_display.cpp78
-rw-r--r--opengl/libs/EGL/egl_display.h23
-rw-r--r--opengl/libs/EGL/egl_object.cpp4
-rw-r--r--opengl/libs/EGL/egl_object.h7
9 files changed, 617 insertions, 84 deletions
diff --git a/opengl/libs/Android.mk b/opengl/libs/Android.mk
index 3e66a13..5855b63 100644
--- a/opengl/libs/Android.mk
+++ b/opengl/libs/Android.mk
@@ -8,6 +8,7 @@ include $(CLEAR_VARS)
LOCAL_SRC_FILES:= \
EGL/egl_tls.cpp \
+ EGL/egl_cache.cpp \
EGL/egl_display.cpp \
EGL/egl_object.cpp \
EGL/egl.cpp \
@@ -157,4 +158,3 @@ LOCAL_MODULE:= libETC1
include $(BUILD_SHARED_LIBRARY)
include $(call all-makefiles-under,$(LOCAL_PATH))
-
diff --git a/opengl/libs/EGL/egl.cpp b/opengl/libs/EGL/egl.cpp
index 1e43195..6ad06af 100644
--- a/opengl/libs/EGL/egl.cpp
+++ b/opengl/libs/EGL/egl.cpp
@@ -212,16 +212,20 @@ egl_connection_t* validate_display_config(EGLDisplay dpy, EGLConfig config,
EGLImageKHR egl_get_image_for_current_context(EGLImageKHR image)
{
- ImageRef _i(image);
- if (!_i.get())
- return EGL_NO_IMAGE_KHR;
-
EGLContext context = egl_tls_t::getContext();
if (context == EGL_NO_CONTEXT || image == EGL_NO_IMAGE_KHR)
return EGL_NO_IMAGE_KHR;
egl_context_t const * const c = get_context(context);
- if (c == NULL) // this should never happen
+ if (c == NULL) // this should never happen, by construction
+ return EGL_NO_IMAGE_KHR;
+
+ egl_display_t* display = egl_display_t::get(c->dpy);
+ if (display == NULL) // this should never happen, by construction
+ return EGL_NO_IMAGE_KHR;
+
+ ImageRef _i(display, image);
+ if (!_i.get())
return EGL_NO_IMAGE_KHR;
// here we don't validate the context because if it's been marked for
diff --git a/opengl/libs/EGL/eglApi.cpp b/opengl/libs/EGL/eglApi.cpp
index 1f9ce68..2237eb6 100644
--- a/opengl/libs/EGL/eglApi.cpp
+++ b/opengl/libs/EGL/eglApi.cpp
@@ -49,22 +49,6 @@ using namespace android;
// ----------------------------------------------------------------------------
-static char const * const sVendorString = "Android";
-static char const * const sVersionString = "1.4 Android META-EGL";
-static char const * const sClientApiString = "OpenGL ES";
-static char const * const sExtensionString =
- "EGL_KHR_image "
- "EGL_KHR_image_base "
- "EGL_KHR_image_pixmap "
- "EGL_KHR_gl_texture_2D_image "
- "EGL_KHR_gl_texture_cubemap_image "
- "EGL_KHR_gl_renderbuffer_image "
- "EGL_KHR_fence_sync "
- "EGL_ANDROID_image_native_buffer "
- "EGL_ANDROID_swap_rectangle "
- "EGL_NV_system_time "
- ;
-
struct extention_map_t {
const char* name;
__eglMustCastToProperFunctionPointerType address;
@@ -79,8 +63,6 @@ static const extention_map_t sExtentionMap[] = {
(__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
{ "eglDestroyImageKHR",
(__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
- { "eglSetSwapRectangleANDROID",
- (__eglMustCastToProperFunctionPointerType)&eglSetSwapRectangleANDROID },
{ "eglGetSystemTimeFrequencyNV",
(__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
{ "eglGetSystemTimeNV",
@@ -451,7 +433,7 @@ EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
egl_display_t const * const dp = validate_display(dpy);
if (!dp) return EGL_FALSE;
- SurfaceRef _s(surface);
+ SurfaceRef _s(dp, surface);
if (!_s.get())
return setError(EGL_BAD_SURFACE, EGL_FALSE);
@@ -472,7 +454,7 @@ EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
egl_display_t const * const dp = validate_display(dpy);
if (!dp) return EGL_FALSE;
- SurfaceRef _s(surface);
+ SurfaceRef _s(dp, surface);
if (!_s.get())
return setError(EGL_BAD_SURFACE, EGL_FALSE);
@@ -541,7 +523,7 @@ EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
if (!dp)
return EGL_FALSE;
- ContextRef _c(ctx);
+ ContextRef _c(dp, ctx);
if (!_c.get())
return setError(EGL_BAD_CONTEXT, EGL_FALSE);
@@ -592,9 +574,9 @@ EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
}
// get a reference to the object passed in
- ContextRef _c(ctx);
- SurfaceRef _d(draw);
- SurfaceRef _r(read);
+ ContextRef _c(dp, ctx);
+ SurfaceRef _d(dp, draw);
+ SurfaceRef _r(dp, read);
// validate the context (if not EGL_NO_CONTEXT)
if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
@@ -696,7 +678,7 @@ EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
egl_display_t const * const dp = validate_display(dpy);
if (!dp) return EGL_FALSE;
- ContextRef _c(ctx);
+ ContextRef _c(dp, ctx);
if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
egl_context_t * const c = get_context(ctx);
@@ -858,10 +840,17 @@ __eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
return NULL;
}
+ // The EGL_ANDROID_blob_cache extension should not be exposed to
+ // applications. It is used internally by the Android EGL layer.
+ if (!strcmp(procname, "eglSetBlobCacheFuncsANDROID")) {
+ return NULL;
+ }
+
__eglMustCastToProperFunctionPointerType addr;
addr = findProcAddress(procname, sExtentionMap, NELEM(sExtentionMap));
if (addr) return addr;
+
// this protects accesses to sGLExtentionMap and sGLExtentionSlot
pthread_mutex_lock(&sExtensionMapMutex);
@@ -937,7 +926,7 @@ EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
egl_display_t const * const dp = validate_display(dpy);
if (!dp) return EGL_FALSE;
- SurfaceRef _s(draw);
+ SurfaceRef _s(dp, draw);
if (!_s.get())
return setError(EGL_BAD_SURFACE, EGL_FALSE);
@@ -953,7 +942,7 @@ EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
egl_display_t const * const dp = validate_display(dpy);
if (!dp) return EGL_FALSE;
- SurfaceRef _s(surface);
+ SurfaceRef _s(dp, surface);
if (!_s.get())
return setError(EGL_BAD_SURFACE, EGL_FALSE);
@@ -971,13 +960,13 @@ const char* eglQueryString(EGLDisplay dpy, EGLint name)
switch (name) {
case EGL_VENDOR:
- return sVendorString;
+ return dp->getVendorString();
case EGL_VERSION:
- return sVersionString;
+ return dp->getVersionString();
case EGL_EXTENSIONS:
- return sExtensionString;
+ return dp->getExtensionString();
case EGL_CLIENT_APIS:
- return sClientApiString;
+ return dp->getClientApiString();
}
return setError(EGL_BAD_PARAMETER, (const char *)0);
}
@@ -995,7 +984,7 @@ EGLBoolean eglSurfaceAttrib(
egl_display_t const * const dp = validate_display(dpy);
if (!dp) return EGL_FALSE;
- SurfaceRef _s(surface);
+ SurfaceRef _s(dp, surface);
if (!_s.get())
return setError(EGL_BAD_SURFACE, EGL_FALSE);
@@ -1015,7 +1004,7 @@ EGLBoolean eglBindTexImage(
egl_display_t const * const dp = validate_display(dpy);
if (!dp) return EGL_FALSE;
- SurfaceRef _s(surface);
+ SurfaceRef _s(dp, surface);
if (!_s.get())
return setError(EGL_BAD_SURFACE, EGL_FALSE);
@@ -1035,7 +1024,7 @@ EGLBoolean eglReleaseTexImage(
egl_display_t const * const dp = validate_display(dpy);
if (!dp) return EGL_FALSE;
- SurfaceRef _s(surface);
+ SurfaceRef _s(dp, surface);
if (!_s.get())
return setError(EGL_BAD_SURFACE, EGL_FALSE);
@@ -1194,7 +1183,7 @@ EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
egl_display_t const * const dp = validate_display(dpy);
if (!dp) return EGL_FALSE;
- SurfaceRef _s(surface);
+ SurfaceRef _s(dp, surface);
if (!_s.get())
return setError(EGL_BAD_SURFACE, EGL_FALSE);
@@ -1213,7 +1202,7 @@ EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
egl_display_t const * const dp = validate_display(dpy);
if (!dp) return EGL_FALSE;
- SurfaceRef _s(surface);
+ SurfaceRef _s(dp, surface);
if (!_s.get())
return setError(EGL_BAD_SURFACE, EGL_FALSE);
@@ -1234,7 +1223,7 @@ EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
if (!dp) return EGL_NO_IMAGE_KHR;
if (ctx != EGL_NO_CONTEXT) {
- ContextRef _c(ctx);
+ ContextRef _c(dp, ctx);
if (!_c.get())
return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
egl_context_t * const c = get_context(ctx);
@@ -1303,7 +1292,7 @@ EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
egl_display_t const * const dp = validate_display(dpy);
if (!dp) return EGL_FALSE;
- ImageRef _i(img);
+ ImageRef _i(dp, img);
if (!_i.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
egl_image_t* image = get_image(img);
@@ -1342,7 +1331,7 @@ EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_l
if (!dp) return EGL_NO_SYNC_KHR;
EGLContext ctx = eglGetCurrentContext();
- ContextRef _c(ctx);
+ ContextRef _c(dp, ctx);
if (!_c.get())
return setError(EGL_BAD_CONTEXT, EGL_NO_SYNC_KHR);
@@ -1365,12 +1354,12 @@ EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
egl_display_t const * const dp = validate_display(dpy);
if (!dp) return EGL_FALSE;
- SyncRef _s(sync);
+ SyncRef _s(dp, sync);
if (!_s.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
egl_sync_t* syncObject = get_sync(sync);
EGLContext ctx = syncObject->context;
- ContextRef _c(ctx);
+ ContextRef _c(dp, ctx);
if (!_c.get())
return setError(EGL_BAD_CONTEXT, EGL_FALSE);
@@ -1392,12 +1381,12 @@ EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags, EGLTi
egl_display_t const * const dp = validate_display(dpy);
if (!dp) return EGL_FALSE;
- SyncRef _s(sync);
+ SyncRef _s(dp, sync);
if (!_s.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE);
egl_sync_t* syncObject = get_sync(sync);
EGLContext ctx = syncObject->context;
- ContextRef _c(ctx);
+ ContextRef _c(dp, ctx);
if (!_c.get())
return setError(EGL_BAD_CONTEXT, EGL_FALSE);
@@ -1417,13 +1406,13 @@ EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint attribute
egl_display_t const * const dp = validate_display(dpy);
if (!dp) return EGL_FALSE;
- SyncRef _s(sync);
+ SyncRef _s(dp, sync);
if (!_s.get())
return setError(EGL_BAD_PARAMETER, EGL_FALSE);
egl_sync_t* syncObject = get_sync(sync);
EGLContext ctx = syncObject->context;
- ContextRef _c(ctx);
+ ContextRef _c(dp, ctx);
if (!_c.get())
return setError(EGL_BAD_CONTEXT, EGL_FALSE);
@@ -1440,25 +1429,7 @@ EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint attribute
// ANDROID extensions
// ----------------------------------------------------------------------------
-EGLBoolean eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw,
- EGLint left, EGLint top, EGLint width, EGLint height)
-{
- clearError();
-
- egl_display_t const * const dp = validate_display(dpy);
- if (!dp) return EGL_FALSE;
-
- SurfaceRef _s(draw);
- if (!_s.get())
- return setError(EGL_BAD_SURFACE, EGL_FALSE);
-
- egl_surface_t const * const s = get_surface(draw);
- if (s->cnx->egl.eglSetSwapRectangleANDROID) {
- return s->cnx->egl.eglSetSwapRectangleANDROID(
- dp->disp[s->impl].dpy, s->surface, left, top, width, height);
- }
- return setError(EGL_BAD_DISPLAY, NULL);
-}
+/* ANDROID extensions entry-point go here */
// ----------------------------------------------------------------------------
// NVIDIA extensions
diff --git a/opengl/libs/EGL/egl_cache.cpp b/opengl/libs/EGL/egl_cache.cpp
new file mode 100644
index 0000000..13a4929
--- /dev/null
+++ b/opengl/libs/EGL/egl_cache.cpp
@@ -0,0 +1,344 @@
+/*
+ ** 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"
+
+#include <fcntl.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+// Cache size limits.
+static const size_t maxKeySize = 1024;
+static const size_t maxValueSize = 4096;
+static const size_t maxTotalSize = 64 * 1024;
+
+// Cache file header
+static const char* cacheFileMagic = "EGL$";
+static const size_t cacheFileHeaderSize = 8;
+
+// The time in seconds to wait before saving newly inserted cache entries.
+static const unsigned int deferredSaveDelay = 4;
+
+// ----------------------------------------------------------------------------
+namespace android {
+// ----------------------------------------------------------------------------
+
+#define BC_EXT_STR "EGL_ANDROID_blob_cache"
+
+//
+// Callback functions passed to EGL.
+//
+static void setBlob(const void* key, EGLsizeiANDROID keySize,
+ const void* value, EGLsizeiANDROID valueSize) {
+ egl_cache_t::get()->setBlob(key, keySize, value, valueSize);
+}
+
+static EGLsizeiANDROID getBlob(const void* key, EGLsizeiANDROID keySize,
+ void* value, EGLsizeiANDROID valueSize) {
+ return egl_cache_t::get()->getBlob(key, keySize, value, valueSize);
+}
+
+//
+// egl_cache_t definition
+//
+egl_cache_t::egl_cache_t() :
+ mInitialized(false),
+ mBlobCache(NULL) {
+}
+
+egl_cache_t::~egl_cache_t() {
+}
+
+egl_cache_t egl_cache_t::sCache;
+
+egl_cache_t* egl_cache_t::get() {
+ return &sCache;
+}
+
+void egl_cache_t::initialize(egl_display_t *display) {
+ Mutex::Autolock lock(mMutex);
+ for (int i = 0; i < IMPL_NUM_IMPLEMENTATIONS; i++) {
+ egl_connection_t* const cnx = &gEGLImpl[i];
+ if (cnx->dso && cnx->major >= 0 && cnx->minor >= 0) {
+ const char* exts = display->disp[i].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(" " BC_EXT_STR " ", exts);
+ if (equal || atStart || atEnd || inMiddle) {
+ PFNEGLSETBLOBCACHEFUNCSANDROIDPROC eglSetBlobCacheFuncsANDROID;
+ eglSetBlobCacheFuncsANDROID =
+ reinterpret_cast<PFNEGLSETBLOBCACHEFUNCSANDROIDPROC>(
+ cnx->egl.eglGetProcAddress(
+ "eglSetBlobCacheFuncsANDROID"));
+ if (eglSetBlobCacheFuncsANDROID == NULL) {
+ LOGE("EGL_ANDROID_blob_cache advertised by display %d, "
+ "but unable to get eglSetBlobCacheFuncsANDROID", i);
+ continue;
+ }
+
+ eglSetBlobCacheFuncsANDROID(display->disp[i].dpy,
+ android::setBlob, android::getBlob);
+ EGLint err = cnx->egl.eglGetError();
+ if (err != EGL_SUCCESS) {
+ LOGE("eglSetBlobCacheFuncsANDROID resulted in an error: "
+ "%#x", err);
+ }
+ }
+ }
+ }
+ mInitialized = true;
+}
+
+void egl_cache_t::terminate() {
+ Mutex::Autolock lock(mMutex);
+ if (mBlobCache != NULL) {
+ saveBlobCacheLocked();
+ mBlobCache = NULL;
+ }
+ mInitialized = false;
+}
+
+void egl_cache_t::setBlob(const void* key, EGLsizeiANDROID keySize,
+ const void* value, EGLsizeiANDROID valueSize) {
+ Mutex::Autolock lock(mMutex);
+
+ if (keySize < 0 || valueSize < 0) {
+ LOGW("EGL_ANDROID_blob_cache set: negative sizes are not allowed");
+ return;
+ }
+
+ if (mInitialized) {
+ sp<BlobCache> bc = getBlobCacheLocked();
+ bc->set(key, keySize, value, valueSize);
+
+ 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();
+ }
+ }
+}
+
+EGLsizeiANDROID egl_cache_t::getBlob(const void* key, EGLsizeiANDROID keySize,
+ void* value, EGLsizeiANDROID valueSize) {
+ Mutex::Autolock lock(mMutex);
+
+ if (keySize < 0 || valueSize < 0) {
+ LOGW("EGL_ANDROID_blob_cache set: negative sizes are not allowed");
+ return 0;
+ }
+
+ if (mInitialized) {
+ sp<BlobCache> bc = getBlobCacheLocked();
+ return bc->get(key, keySize, value, valueSize);
+ }
+ return 0;
+}
+
+void egl_cache_t::setCacheFilename(const char* filename) {
+ Mutex::Autolock lock(mMutex);
+ mFilename = filename;
+}
+
+sp<BlobCache> egl_cache_t::getBlobCacheLocked() {
+ if (mBlobCache == NULL) {
+ mBlobCache = new BlobCache(maxKeySize, maxValueSize, maxTotalSize);
+ loadBlobCacheLocked();
+ }
+ return mBlobCache;
+}
+
+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;
+}
+
+void egl_cache_t::saveBlobCacheLocked() {
+ 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.
+ LOGE("error unlinking cache file %s: %s (%d)", fname,
+ 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) {
+ LOGE("error creating cache file %s: %s (%d)", fname,
+ strerror(errno), errno);
+ return;
+ }
+ }
+
+ size_t fileSize = headerSize + cacheSize;
+ if (ftruncate(fd, fileSize) == -1) {
+ LOGE("error setting cache file size: %s (%d)", strerror(errno),
+ errno);
+ close(fd);
+ unlink(fname);
+ return;
+ }
+
+ uint8_t* buf = reinterpret_cast<uint8_t*>(mmap(NULL, fileSize,
+ PROT_WRITE, MAP_SHARED, fd, 0));
+ if (buf == MAP_FAILED) {
+ LOGE("error mmaping cache file: %s (%d)", strerror(errno),
+ errno);
+ close(fd);
+ unlink(fname);
+ return;
+ }
+
+ status_t err = mBlobCache->flatten(buf + headerSize, cacheSize, NULL,
+ 0);
+ if (err != OK) {
+ LOGE("error writing cache contents: %s (%d)", strerror(-err),
+ -err);
+ munmap(buf, fileSize);
+ 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);
+
+ munmap(buf, fileSize);
+ fchmod(fd, S_IRUSR);
+ close(fd);
+ }
+}
+
+void egl_cache_t::loadBlobCacheLocked() {
+ if (mFilename.length() > 0) {
+ size_t headerSize = cacheFileHeaderSize;
+
+ int fd = open(mFilename.string(), O_RDONLY, 0);
+ if (fd == -1) {
+ if (errno != ENOENT) {
+ LOGE("error opening cache file %s: %s (%d)", mFilename.string(),
+ strerror(errno), errno);
+ }
+ return;
+ }
+
+ struct stat statBuf;
+ if (fstat(fd, &statBuf) == -1) {
+ LOGE("error stat'ing cache file: %s (%d)", strerror(errno), errno);
+ close(fd);
+ return;
+ }
+
+ // Sanity check the size before trying to mmap it.
+ size_t fileSize = statBuf.st_size;
+ if (fileSize > maxTotalSize * 2) {
+ LOGE("cache file is too large: %#llx", statBuf.st_size);
+ close(fd);
+ return;
+ }
+
+ uint8_t* buf = reinterpret_cast<uint8_t*>(mmap(NULL, fileSize,
+ PROT_READ, MAP_PRIVATE, fd, 0));
+ if (buf == MAP_FAILED) {
+ LOGE("error mmaping cache file: %s (%d)", strerror(errno),
+ errno);
+ close(fd);
+ return;
+ }
+
+ // Check the file magic and CRC
+ size_t cacheSize = fileSize - headerSize;
+ if (memcmp(buf, cacheFileMagic, 4) != 0) {
+ LOGE("cache file has bad mojo");
+ close(fd);
+ return;
+ }
+ uint32_t* crc = reinterpret_cast<uint32_t*>(buf + 4);
+ if (crc32c(buf + headerSize, cacheSize) != *crc) {
+ LOGE("cache file failed CRC check");
+ close(fd);
+ return;
+ }
+
+ status_t err = mBlobCache->unflatten(buf + headerSize, cacheSize, NULL,
+ 0);
+ if (err != OK) {
+ LOGE("error reading cache contents: %s (%d)", strerror(-err),
+ -err);
+ munmap(buf, fileSize);
+ close(fd);
+ return;
+ }
+
+ munmap(buf, fileSize);
+ close(fd);
+ }
+}
+
+// ----------------------------------------------------------------------------
+}; // namespace android
+// ----------------------------------------------------------------------------
diff --git a/opengl/libs/EGL/egl_cache.h b/opengl/libs/EGL/egl_cache.h
new file mode 100644
index 0000000..8760009
--- /dev/null
+++ b/opengl/libs/EGL/egl_cache.h
@@ -0,0 +1,130 @@
+/*
+ ** 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.
+ */
+
+#ifndef ANDROID_EGL_CACHE_H
+#define ANDROID_EGL_CACHE_H
+
+#include <EGL/egl.h>
+#include <EGL/eglext.h>
+
+#include <utils/BlobCache.h>
+#include <utils/String8.h>
+#include <utils/StrongPointer.h>
+
+// ----------------------------------------------------------------------------
+namespace android {
+// ----------------------------------------------------------------------------
+
+class egl_display_t;
+
+class EGLAPI egl_cache_t {
+public:
+
+ // get returns a pointer to the singleton egl_cache_t object. This
+ // singleton object will never be destroyed.
+ static egl_cache_t* get();
+
+ // initialize puts the egl_cache_t into an initialized state, such that it
+ // is able to insert and retrieve entries from the cache. This should be
+ // called when EGL is initialized. When not in the initialized state the
+ // getBlob and setBlob methods will return without performing any cache
+ // operations.
+ void initialize(egl_display_t* display);
+
+ // terminate puts the egl_cache_t back into the uninitialized state. When
+ // in this state the getBlob and setBlob methods will return without
+ // performing any cache operations.
+ void terminate();
+
+ // setBlob attempts to insert a new key/value blob pair into the cache.
+ // This will be called by the hardware vendor's EGL implementation via the
+ // EGL_ANDROID_blob_cache extension.
+ void setBlob(const void* key, EGLsizeiANDROID keySize, const void* value,
+ EGLsizeiANDROID valueSize);
+
+ // getBlob attempts to retrieve the value blob associated with a given key
+ // blob from cache. This will be called by the hardware vendor's EGL
+ // implementation via the EGL_ANDROID_blob_cache extension.
+ EGLsizeiANDROID getBlob(const void* key, EGLsizeiANDROID keySize,
+ void* value, EGLsizeiANDROID valueSize);
+
+ // setCacheFilename sets the name of the file that should be used to store
+ // cache contents from one program invocation to another.
+ void setCacheFilename(const char* filename);
+
+private:
+ // Creation and (the lack of) destruction is handled internally.
+ egl_cache_t();
+ ~egl_cache_t();
+
+ // Copying is disallowed.
+ egl_cache_t(const egl_cache_t&); // not implemented
+ void operator=(const egl_cache_t&); // not implemented
+
+ // getBlobCacheLocked returns the BlobCache object being used to store the
+ // key/value blob pairs. If the BlobCache object has not yet been created,
+ // this will do so, loading the serialized cache contents from disk if
+ // possible.
+ sp<BlobCache> getBlobCacheLocked();
+
+ // saveBlobCache attempts to save the current contents of mBlobCache to
+ // disk.
+ void saveBlobCacheLocked();
+
+ // loadBlobCache attempts to load the saved cache contents from disk into
+ // mBlobCache.
+ void loadBlobCacheLocked();
+
+ // mInitialized indicates whether the egl_cache_t is in the initialized
+ // state. It is initialized to false at construction time, and gets set to
+ // true when initialize is called. It is set back to false when terminate
+ // is called. When in this state, the cache behaves as normal. When not,
+ // the getBlob and setBlob methods will return without performing any cache
+ // operations.
+ bool mInitialized;
+
+ // mBlobCache is the cache in which the key/value blob pairs are stored. It
+ // is initially NULL, and will be initialized by getBlobCacheLocked the
+ // first time it's needed.
+ sp<BlobCache> mBlobCache;
+
+ // mFilename is the name of the file for storing cache contents in between
+ // program invocations. It is initialized to an empty string at
+ // construction time, and can be set with the setCacheFilename method. An
+ // empty string indicates that the cache should not be saved to or restored
+ // from disk.
+ String8 mFilename;
+
+ // mSavePending indicates whether or not a deferred save operation is
+ // pending. Each time a key/value pair is inserted into the cache via
+ // setBlob, a deferred save is initiated if one is not already pending.
+ // This will wait some amount of time and then trigger a save of the cache
+ // contents to disk.
+ bool mSavePending;
+
+ // mMutex is the mutex used to prevent concurrent access to the member
+ // variables. It must be locked whenever the member variables are accessed.
+ mutable Mutex mMutex;
+
+ // sCache is the singleton egl_cache_t object.
+ static egl_cache_t sCache;
+};
+
+// ----------------------------------------------------------------------------
+}; // namespace android
+// ----------------------------------------------------------------------------
+
+#endif // ANDROID_EGL_CACHE_H
diff --git a/opengl/libs/EGL/egl_display.cpp b/opengl/libs/EGL/egl_display.cpp
index 83aafa6..31119f9 100644
--- a/opengl/libs/EGL/egl_display.cpp
+++ b/opengl/libs/EGL/egl_display.cpp
@@ -14,6 +14,9 @@
** limitations under the License.
*/
+#include <string.h>
+
+#include "egl_cache.h"
#include "egl_display.h"
#include "egl_object.h"
#include "egl_tls.h"
@@ -24,6 +27,36 @@
namespace android {
// ----------------------------------------------------------------------------
+static char const * const sVendorString = "Android";
+static char const * const sVersionString = "1.4 Android META-EGL";
+static char const * const sClientApiString = "OpenGL ES";
+
+// this is the list of EGL extensions that are exposed to applications
+// some of them are mandatory because used by the ANDROID system.
+//
+// mandatory extensions are required per the CDD and not explicitly
+// checked during EGL initialization. the system *assumes* these extensions
+// are present. the system may not function properly if some mandatory
+// extensions are missing.
+//
+// NOTE: sExtensionString MUST be have a single space as the last character.
+//
+static char const * const sExtensionString =
+ "EGL_KHR_image " // mandatory
+ "EGL_KHR_image_base " // mandatory
+ "EGL_KHR_image_pixmap "
+ "EGL_KHR_gl_texture_2D_image "
+ "EGL_KHR_gl_texture_cubemap_image "
+ "EGL_KHR_gl_renderbuffer_image "
+ "EGL_KHR_fence_sync "
+ "EGL_NV_system_time "
+ "EGL_ANDROID_image_native_buffer " // mandatory
+ ;
+
+// extensions not exposed to applications but used by the ANDROID system
+// "EGL_ANDROID_recordable " // mandatory
+// "EGL_ANDROID_blob_cache " // strongly recommended
+
extern void initEglTraceLevel();
extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
@@ -43,6 +76,7 @@ egl_display_t::egl_display_t() :
egl_display_t::~egl_display_t() {
magic = 0;
+ egl_cache_t::get()->terminate();
}
egl_display_t* egl_display_t::get(EGLDisplay dpy) {
@@ -60,11 +94,13 @@ void egl_display_t::removeObject(egl_object_t* object) {
objects.remove(object);
}
-bool egl_display_t::getObject(egl_object_t* object) {
+bool egl_display_t::getObject(egl_object_t* object) const {
Mutex::Autolock _l(lock);
if (objects.indexOf(object) >= 0) {
- object->incRef();
- return true;
+ if (object->getDisplay() == this) {
+ object->incRef();
+ return true;
+ }
}
return false;
}
@@ -170,6 +206,42 @@ EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
}
}
+ // the query strings are per-display
+ mVendorString.setTo(sVendorString);
+ mVersionString.setTo(sVersionString);
+ mClientApiString.setTo(sClientApiString);
+
+ // we only add extensions that exist in at least one implementation
+ char const* start = sExtensionString;
+ char const* end;
+ do {
+ // find the space separating this extension for the next one
+ end = strchr(start, ' ');
+ if (end) {
+ // length of the extension string
+ const size_t len = end - start;
+ if (len) {
+ // NOTE: we could avoid the copy if we had strnstr.
+ const String8 ext(start, len);
+ // now go through all implementations and look for this extension
+ for (int i = 0; i < IMPL_NUM_IMPLEMENTATIONS; i++) {
+ if (disp[i].queryString.extensions) {
+ // if we find it, add this extension string to our list
+ // (and don't forget the space)
+ const char* match = strstr(disp[i].queryString.extensions, ext.string());
+ if (match && (match[len] == ' ' || match[len] == 0)) {
+ mExtensionString.append(start, len+1);
+ }
+ }
+ }
+ }
+ // process the next extension string, and skip the space.
+ start = end + 1;
+ }
+ } while (end);
+
+ egl_cache_t::get()->initialize(this);
+
EGLBoolean res = EGL_FALSE;
for (int i = 0; i < IMPL_NUM_IMPLEMENTATIONS; i++) {
egl_connection_t* const cnx = &gEGLImpl[i];
diff --git a/opengl/libs/EGL/egl_display.h b/opengl/libs/EGL/egl_display.h
index 113595f..042ae07 100644
--- a/opengl/libs/EGL/egl_display.h
+++ b/opengl/libs/EGL/egl_display.h
@@ -29,6 +29,7 @@
#include <utils/SortedVector.h>
#include <utils/threads.h>
+#include <utils/String8.h>
#include "egldefs.h"
#include "hooks.h"
@@ -59,7 +60,7 @@ struct egl_config_t {
// ----------------------------------------------------------------------------
-class egl_display_t {
+class EGLAPI egl_display_t { // marked as EGLAPI for testing purposes
static egl_display_t sDisplay[NUM_DISPLAYS];
EGLDisplay getDisplay(EGLNativeDisplayType display);
@@ -81,7 +82,7 @@ public:
// remove object from this display's list
void removeObject(egl_object_t* object);
// add reference to this object. returns true if this is a valid object.
- bool getObject(egl_object_t* object);
+ bool getObject(egl_object_t* object) const;
static egl_display_t* get(EGLDisplay dpy);
@@ -91,6 +92,13 @@ public:
inline bool isValid() const { return magic == '_dpy'; }
inline bool isAlive() const { return isValid(); }
+ char const * getVendorString() const { return mVendorString.string(); }
+ char const * getVersionString() const { return mVersionString.string(); }
+ char const * getClientApiString() const { return mClientApiString.string(); }
+ char const * getExtensionString() const { return mExtensionString.string(); }
+
+ inline uint32_t getRefsCount() const { return refs; }
+
struct strings_t {
char const * vendor;
char const * version;
@@ -117,9 +125,13 @@ public:
egl_config_t* configs;
private:
- uint32_t refs;
- Mutex lock;
- SortedVector<egl_object_t*> objects;
+ uint32_t refs;
+ mutable Mutex lock;
+ SortedVector<egl_object_t*> objects;
+ String8 mVendorString;
+ String8 mVersionString;
+ String8 mClientApiString;
+ String8 mExtensionString;
};
// ----------------------------------------------------------------------------
@@ -141,4 +153,3 @@ EGLBoolean validate_display_surface(EGLDisplay dpy, EGLSurface surface);
// ----------------------------------------------------------------------------
#endif // ANDROID_EGL_DISPLAY_H
-
diff --git a/opengl/libs/EGL/egl_object.cpp b/opengl/libs/EGL/egl_object.cpp
index dbf9a01..20cdc7e 100644
--- a/opengl/libs/EGL/egl_object.cpp
+++ b/opengl/libs/EGL/egl_object.cpp
@@ -55,10 +55,10 @@ void egl_object_t::destroy() {
}
}
-bool egl_object_t::get() {
+bool egl_object_t::get(egl_display_t const* display, egl_object_t* object) {
// used by LocalRef, this does an incRef() atomically with
// checking that the object is valid.
- return display->getObject(this);
+ return display->getObject(object);
}
// ----------------------------------------------------------------------------
diff --git a/opengl/libs/EGL/egl_object.h b/opengl/libs/EGL/egl_object.h
index 46f7139..df1b261 100644
--- a/opengl/libs/EGL/egl_object.h
+++ b/opengl/libs/EGL/egl_object.h
@@ -52,10 +52,11 @@ public:
inline int32_t incRef() { return android_atomic_inc(&count); }
inline int32_t decRef() { return android_atomic_dec(&count); }
+ inline egl_display_t* getDisplay() const { return display; }
private:
void terminate();
- bool get();
+ static bool get(egl_display_t const* display, egl_object_t* object);
public:
template <typename N, typename T>
@@ -66,9 +67,9 @@ public:
public:
~LocalRef();
explicit LocalRef(egl_object_t* rhs);
- explicit LocalRef(T o) : ref(0) {
+ explicit LocalRef(egl_display_t const* display, T o) : ref(0) {
egl_object_t* native = reinterpret_cast<N*>(o);
- if (o && native->get()) {
+ if (o && egl_object_t::get(display, native)) {
ref = native;
}
}