refactor EGL source code

no changes is functionality. split various objects into their own files.
make egl_display objec's lock internal.
This commit is contained in:
Mathias Agopian 2011-05-13 16:21:08 -07:00
parent 0ad71a97c6
commit 518ec112f4
15 changed files with 2421 additions and 2074 deletions

View File

@ -7,7 +7,11 @@ LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= \
EGL/egl_tls.cpp \
EGL/egl_display.cpp \
EGL/egl_object.cpp \
EGL/egl.cpp \
EGL/eglApi.cpp \
EGL/trace.cpp \
EGL/getProcAddress.cpp.arm \
EGL/hooks.cpp \

File diff suppressed because it is too large Load Diff

1464
opengl/libs/EGL/eglApi.cpp Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,269 @@
/*
** Copyright 2007, 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_display.h"
#include "egl_object.h"
#include "egl_tls.h"
#include "egl_impl.h"
#include "Loader.h"
// ----------------------------------------------------------------------------
namespace android {
// ----------------------------------------------------------------------------
extern void initEglTraceLevel();
extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
static int cmp_configs(const void* a, const void *b) {
const egl_config_t& c0 = *(egl_config_t const *)a;
const egl_config_t& c1 = *(egl_config_t const *)b;
return c0<c1 ? -1 : (c1<c0 ? 1 : 0);
}
// ----------------------------------------------------------------------------
egl_display_t egl_display_t::sDisplay[NUM_DISPLAYS];
egl_display_t::egl_display_t() :
magic('_dpy'), numTotalConfigs(0), configs(0), refs(0) {
}
egl_display_t::~egl_display_t() {
magic = 0;
}
egl_display_t* egl_display_t::get(EGLDisplay dpy) {
uintptr_t index = uintptr_t(dpy)-1U;
return (index >= NUM_DISPLAYS) ? NULL : &sDisplay[index];
}
void egl_display_t::addObject(egl_object_t* object) {
Mutex::Autolock _l(lock);
objects.add(object);
}
bool egl_display_t::getObject(egl_object_t* object) {
Mutex::Autolock _l(lock);
if (objects.indexOf(object) >= 0) {
object->incRef();
return true;
}
return false;
}
bool egl_display_t::removeObject(egl_object_t* object) {
Mutex::Autolock _l(lock);
if (object->decRef() == 1) {
objects.remove(object);
return true;
}
return false;
}
EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp) {
if (uintptr_t(disp) >= NUM_DISPLAYS)
return NULL;
return sDisplay[uintptr_t(disp)].getDisplay(disp);
}
EGLDisplay egl_display_t::getDisplay(EGLNativeDisplayType display) {
Mutex::Autolock _l(lock);
// get our driver loader
Loader& loader(Loader::getInstance());
for (int i = 0; i < IMPL_NUM_IMPLEMENTATIONS; i++) {
egl_connection_t* const cnx = &gEGLImpl[i];
if (cnx->dso && disp[i].dpy == EGL_NO_DISPLAY) {
EGLDisplay dpy = cnx->egl.eglGetDisplay(display);
disp[i].dpy = dpy;
if (dpy == EGL_NO_DISPLAY) {
loader.close(cnx->dso);
cnx->dso = NULL;
}
}
}
return EGLDisplay(uintptr_t(display) + 1U);
}
EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
Mutex::Autolock _l(lock);
if (refs > 0) {
if (major != NULL)
*major = VERSION_MAJOR;
if (minor != NULL)
*minor = VERSION_MINOR;
refs++;
return EGL_TRUE;
}
#if EGL_TRACE
// Called both at early_init time and at this time. (Early_init is pre-zygote, so
// the information from that call may be stale.)
initEglTraceLevel();
#endif
setGLHooksThreadSpecific(&gHooksNoContext);
// initialize each EGL and
// build our own extension string first, based on the extension we know
// and the extension supported by our client implementation
for (int i = 0; i < IMPL_NUM_IMPLEMENTATIONS; i++) {
egl_connection_t* const cnx = &gEGLImpl[i];
cnx->major = -1;
cnx->minor = -1;
if (!cnx->dso)
continue;
#if defined(ADRENO130)
#warning "Adreno-130 eglInitialize() workaround"
/*
* The ADRENO 130 driver returns a different EGLDisplay each time
* eglGetDisplay() is called, but also makes the EGLDisplay invalid
* after eglTerminate() has been called, so that eglInitialize()
* cannot be called again. Therefore, we need to make sure to call
* eglGetDisplay() before calling eglInitialize();
*/
if (i == IMPL_HARDWARE) {
disp[i].dpy =
cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
}
#endif
EGLDisplay idpy = disp[i].dpy;
if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
//LOGD("initialized %d dpy=%p, ver=%d.%d, cnx=%p",
// i, idpy, cnx->major, cnx->minor, cnx);
// display is now initialized
disp[i].state = egl_display_t::INITIALIZED;
// get the query-strings for this display for each implementation
disp[i].queryString.vendor = cnx->egl.eglQueryString(idpy,
EGL_VENDOR);
disp[i].queryString.version = cnx->egl.eglQueryString(idpy,
EGL_VERSION);
disp[i].queryString.extensions = cnx->egl.eglQueryString(idpy,
EGL_EXTENSIONS);
disp[i].queryString.clientApi = cnx->egl.eglQueryString(idpy,
EGL_CLIENT_APIS);
} else {
LOGW("%d: eglInitialize(%p) failed (%s)", i, idpy,
egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
}
}
EGLBoolean res = EGL_FALSE;
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) {
EGLint n;
if (cnx->egl.eglGetConfigs(disp[i].dpy, 0, 0, &n)) {
disp[i].config = (EGLConfig*) malloc(sizeof(EGLConfig) * n);
if (disp[i].config) {
if (cnx->egl.eglGetConfigs(disp[i].dpy, disp[i].config, n,
&disp[i].numConfigs)) {
numTotalConfigs += n;
res = EGL_TRUE;
}
}
}
}
}
if (res == EGL_TRUE) {
configs = new egl_config_t[numTotalConfigs];
for (int i = 0, k = 0; i < IMPL_NUM_IMPLEMENTATIONS; i++) {
egl_connection_t* const cnx = &gEGLImpl[i];
if (cnx->dso && cnx->major >= 0 && cnx->minor >= 0) {
for (int j = 0; j < disp[i].numConfigs; j++) {
configs[k].impl = i;
configs[k].config = disp[i].config[j];
configs[k].configId = k + 1; // CONFIG_ID start at 1
// store the implementation's CONFIG_ID
cnx->egl.eglGetConfigAttrib(disp[i].dpy, disp[i].config[j],
EGL_CONFIG_ID, &configs[k].implConfigId);
k++;
}
}
}
// sort our configurations so we can do binary-searches
qsort(configs, numTotalConfigs, sizeof(egl_config_t), cmp_configs);
refs++;
if (major != NULL)
*major = VERSION_MAJOR;
if (minor != NULL)
*minor = VERSION_MINOR;
return EGL_TRUE;
}
return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
}
EGLBoolean egl_display_t::terminate() {
Mutex::Autolock _l(lock);
if (refs == 0) {
return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
}
// this is specific to Android, display termination is ref-counted.
if (refs > 1) {
refs--;
return EGL_TRUE;
}
EGLBoolean res = EGL_FALSE;
for (int i = 0; i < IMPL_NUM_IMPLEMENTATIONS; i++) {
egl_connection_t* const cnx = &gEGLImpl[i];
if (cnx->dso && disp[i].state == egl_display_t::INITIALIZED) {
if (cnx->egl.eglTerminate(disp[i].dpy) == EGL_FALSE) {
LOGW("%d: eglTerminate(%p) failed (%s)", i, disp[i].dpy,
egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
}
// REVISIT: it's unclear what to do if eglTerminate() fails
free(disp[i].config);
disp[i].numConfigs = 0;
disp[i].config = 0;
disp[i].state = egl_display_t::TERMINATED;
res = EGL_TRUE;
}
}
// TODO: all egl_object_t should be marked for termination
refs--;
numTotalConfigs = 0;
delete[] configs;
return res;
}
// ----------------------------------------------------------------------------
}; // namespace android
// ----------------------------------------------------------------------------

View File

@ -0,0 +1,143 @@
/*
** Copyright 2007, 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_DISPLAY_H
#define ANDROID_EGL_DISPLAY_H
#include <ctype.h>
#include <stdint.h>
#include <stdlib.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <GLES/gl.h>
#include <GLES/glext.h>
#include <utils/SortedVector.h>
#include <utils/threads.h>
#include "hooks.h"
// ----------------------------------------------------------------------------
namespace android {
// ----------------------------------------------------------------------------
class egl_object_t;
class egl_connection_t;
// ----------------------------------------------------------------------------
struct egl_config_t {
egl_config_t() {}
egl_config_t(int impl, EGLConfig config)
: impl(impl), config(config), configId(0), implConfigId(0) { }
int impl; // the implementation this config is for
EGLConfig config; // the implementation's EGLConfig
EGLint configId; // our CONFIG_ID
EGLint implConfigId; // the implementation's CONFIG_ID
inline bool operator < (const egl_config_t& rhs) const {
if (impl < rhs.impl) return true;
if (impl > rhs.impl) return false;
return config < rhs.config;
}
};
// ----------------------------------------------------------------------------
class egl_display_t {
static egl_display_t sDisplay[NUM_DISPLAYS];
EGLDisplay getDisplay(EGLNativeDisplayType display);
public:
enum {
NOT_INITIALIZED = 0,
INITIALIZED = 1,
TERMINATED = 2
};
egl_display_t();
~egl_display_t();
EGLBoolean initialize(EGLint *major, EGLint *minor);
EGLBoolean terminate();
// add object to this display's list
void addObject(egl_object_t* object);
// remove object from this display's list if it has no reference.
// returns true if object was removed.
bool removeObject(egl_object_t* object);
// add reference to this object. returns true if this is a valid object.
bool getObject(egl_object_t* object);
static egl_display_t* get(EGLDisplay dpy);
static EGLDisplay getFromNativeDisplay(EGLNativeDisplayType disp);
inline bool isReady() const { return (refs > 0); }
inline bool isValid() const { return magic == '_dpy'; }
inline bool isAlive() const { return isValid(); }
struct strings_t {
char const * vendor;
char const * version;
char const * clientApi;
char const * extensions;
};
struct DisplayImpl {
DisplayImpl() : dpy(EGL_NO_DISPLAY), config(0),
state(NOT_INITIALIZED), numConfigs(0) { }
EGLDisplay dpy;
EGLConfig* config;
EGLint state;
EGLint numConfigs;
strings_t queryString;
};
private:
uint32_t magic;
public:
DisplayImpl disp[IMPL_NUM_IMPLEMENTATIONS];
EGLint numTotalConfigs;
egl_config_t* configs;
private:
uint32_t refs;
Mutex lock;
SortedVector<egl_object_t*> objects;
};
// ----------------------------------------------------------------------------
inline egl_display_t* get_display(EGLDisplay dpy) {
return egl_display_t::get(dpy);
}
// ----------------------------------------------------------------------------
egl_display_t* validate_display(EGLDisplay dpy);
egl_connection_t* validate_display_config(EGLDisplay dpy,
EGLConfig config, egl_display_t const*& dp);
EGLBoolean validate_display_context(EGLDisplay dpy, EGLContext ctx);
EGLBoolean validate_display_surface(EGLDisplay dpy, EGLSurface surface);
// ----------------------------------------------------------------------------
}; // namespace android
// ----------------------------------------------------------------------------
#endif // ANDROID_EGL_DISPLAY_H

View File

@ -0,0 +1,49 @@
/*
** Copyright 2007, 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 <ctype.h>
#include <stdint.h>
#include <stdlib.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <GLES/gl.h>
#include <GLES/glext.h>
#include <utils/threads.h>
#include "egl_object.h"
// ----------------------------------------------------------------------------
namespace android {
// ----------------------------------------------------------------------------
egl_object_t::egl_object_t(egl_display_t* disp) :
display(disp), terminated(0), count(1) {
display->addObject(this);
}
bool egl_object_t::get() {
return display->getObject(this);
}
bool egl_object_t::put() {
return display->removeObject(this);
}
// ----------------------------------------------------------------------------
}; // namespace android
// ----------------------------------------------------------------------------

View File

@ -0,0 +1,202 @@
/*
** Copyright 2007, 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_OBJECT_H
#define ANDROID_EGL_OBJECT_H
#include <ctype.h>
#include <stdint.h>
#include <stdlib.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <GLES/gl.h>
#include <GLES/glext.h>
#include <utils/threads.h>
#include <system/window.h>
#include "egl_display.h"
// ----------------------------------------------------------------------------
namespace android {
// ----------------------------------------------------------------------------
struct egl_display_t;
class egl_object_t {
egl_display_t *display;
volatile int32_t terminated;
mutable volatile int32_t count;
public:
egl_object_t(egl_display_t* display);
inline bool isAlive() const { return !terminated; }
inline int32_t incRef() { return android_atomic_inc(&count); }
inline int32_t decRef() { return android_atomic_dec(&count); }
private:
bool get();
bool put();
public:
template <typename N, typename T>
struct LocalRef {
N* ref;
LocalRef(T o) : ref(0) {
N* native = reinterpret_cast<N*>(o);
if (o && native->get()) {
ref = native;
}
}
~LocalRef() {
if (ref && ref->put()) {
delete ref;
}
}
inline N* get() {
return ref;
}
void acquire() const {
if (ref) {
android_atomic_inc(&ref->count);
}
}
void release() const {
if (ref) {
int32_t c = android_atomic_dec(&ref->count);
// ref->count cannot be 1 prior atomic_dec because we have
// a reference, and if we have one, it means there was
// already one before us.
LOGE_IF(c==1, "refcount is now 0 in release()");
}
}
void terminate() {
if (ref) {
ref->terminated = 1;
release();
}
}
};
};
// ----------------------------------------------------------------------------
struct egl_surface_t: public egl_object_t {
typedef egl_object_t::LocalRef<egl_surface_t, EGLSurface> Ref;
egl_surface_t(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win,
EGLSurface surface, int impl, egl_connection_t const* cnx) :
egl_object_t(get_display(dpy)), dpy(dpy), surface(surface),
config(config), win(win), impl(impl), cnx(cnx) {
}
~egl_surface_t() {
}
EGLDisplay dpy;
EGLSurface surface;
EGLConfig config;
sp<ANativeWindow> win;
int impl;
egl_connection_t const* cnx;
};
struct egl_context_t: public egl_object_t {
typedef egl_object_t::LocalRef<egl_context_t, EGLContext> Ref;
egl_context_t(EGLDisplay dpy, EGLContext context, EGLConfig config,
int impl, egl_connection_t const* cnx, int version) :
egl_object_t(get_display(dpy)), dpy(dpy), context(context),
config(config), read(0), draw(0), impl(impl), cnx(cnx),
version(version) {
}
~egl_context_t() {
}
EGLDisplay dpy;
EGLContext context;
EGLConfig config;
EGLSurface read;
EGLSurface draw;
int impl;
egl_connection_t const* cnx;
int version;
};
struct egl_image_t: public egl_object_t {
typedef egl_object_t::LocalRef<egl_image_t, EGLImageKHR> Ref;
egl_image_t(EGLDisplay dpy, EGLContext context) :
egl_object_t(get_display(dpy)), dpy(dpy), context(context) {
memset(images, 0, sizeof(images));
}
EGLDisplay dpy;
EGLContext context;
EGLImageKHR images[IMPL_NUM_IMPLEMENTATIONS];
};
struct egl_sync_t: public egl_object_t {
typedef egl_object_t::LocalRef<egl_sync_t, EGLSyncKHR> Ref;
egl_sync_t(EGLDisplay dpy, EGLContext context, EGLSyncKHR sync) :
egl_object_t(get_display(dpy)), dpy(dpy), context(context), sync(sync) {
}
EGLDisplay dpy;
EGLContext context;
EGLSyncKHR sync;
};
// ----------------------------------------------------------------------------
typedef egl_surface_t::Ref SurfaceRef;
typedef egl_context_t::Ref ContextRef;
typedef egl_image_t::Ref ImageRef;
typedef egl_sync_t::Ref SyncRef;
// ----------------------------------------------------------------------------
template<typename NATIVE, typename EGL>
static inline NATIVE* egl_to_native_cast(EGL arg) {
return reinterpret_cast<NATIVE*>(arg);
}
static inline
egl_surface_t* get_surface(EGLSurface surface) {
return egl_to_native_cast<egl_surface_t>(surface);
}
static inline
egl_context_t* get_context(EGLContext context) {
return egl_to_native_cast<egl_context_t>(context);
}
static inline
egl_image_t* get_image(EGLImageKHR image) {
return egl_to_native_cast<egl_image_t>(image);
}
static inline
egl_sync_t* get_sync(EGLSyncKHR sync) {
return egl_to_native_cast<egl_sync_t>(sync);
}
// ----------------------------------------------------------------------------
}; // namespace android
// ----------------------------------------------------------------------------
#endif // ANDROID_EGL_OBJECT_H

133
opengl/libs/EGL/egl_tls.cpp Normal file
View File

@ -0,0 +1,133 @@
/*
** 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 <pthread.h>
#include <cutils/log.h>
#include <EGL/egl.h>
#include "egl_tls.h"
namespace android {
pthread_key_t egl_tls_t::sKey = -1;
pthread_mutex_t egl_tls_t::sLockKey = PTHREAD_MUTEX_INITIALIZER;
egl_tls_t::egl_tls_t()
: error(EGL_SUCCESS), ctx(0), logCallWithNoContext(EGL_TRUE), dbg(0) {
}
const char *egl_tls_t::egl_strerror(EGLint err) {
switch (err) {
case EGL_SUCCESS: return "EGL_SUCCESS";
case EGL_NOT_INITIALIZED: return "EGL_NOT_INITIALIZED";
case EGL_BAD_ACCESS: return "EGL_BAD_ACCESS";
case EGL_BAD_ALLOC: return "EGL_BAD_ALLOC";
case EGL_BAD_ATTRIBUTE: return "EGL_BAD_ATTRIBUTE";
case EGL_BAD_CONFIG: return "EGL_BAD_CONFIG";
case EGL_BAD_CONTEXT: return "EGL_BAD_CONTEXT";
case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE";
case EGL_BAD_DISPLAY: return "EGL_BAD_DISPLAY";
case EGL_BAD_MATCH: return "EGL_BAD_MATCH";
case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP";
case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW";
case EGL_BAD_PARAMETER: return "EGL_BAD_PARAMETER";
case EGL_BAD_SURFACE: return "EGL_BAD_SURFACE";
case EGL_CONTEXT_LOST: return "EGL_CONTEXT_LOST";
default: return "UNKNOWN";
}
}
void egl_tls_t::validateTLSKey()
{
if (sKey == -1) {
pthread_mutex_lock(&sLockKey);
if (sKey == -1)
pthread_key_create(&sKey, NULL);
pthread_mutex_unlock(&sLockKey);
}
}
void egl_tls_t::setErrorEtcImpl(const char* caller, int line, EGLint error) {
validateTLSKey();
egl_tls_t* tls = getTLS();
if (tls->error != error) {
LOGE("%s:%d error %x (%s)", caller, line, error, egl_strerror(error));
tls->error = error;
}
}
bool egl_tls_t::logNoContextCall() {
egl_tls_t* tls = getTLS();
if (tls->logCallWithNoContext == true) {
tls->logCallWithNoContext = false;
return true;
}
return false;
}
egl_tls_t* egl_tls_t::getTLS() {
egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
if (tls == 0) {
tls = new egl_tls_t;
pthread_setspecific(sKey, tls);
}
return tls;
}
void egl_tls_t::clearTLS() {
if (sKey != -1) {
egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
if (tls) {
delete tls;
pthread_setspecific(sKey, 0);
}
}
}
void egl_tls_t::clearError() {
// This must clear the error from all the underlying EGL implementations as
// well as the EGL wrapper layer.
eglGetError();
}
EGLint egl_tls_t::getError() {
if (sKey == -1)
return EGL_SUCCESS;
egl_tls_t* tls = (egl_tls_t*)pthread_getspecific(sKey);
if (!tls) return EGL_SUCCESS;
EGLint error = tls->error;
tls->error = EGL_SUCCESS;
return error;
}
void egl_tls_t::setContext(EGLContext ctx) {
validateTLSKey();
getTLS()->ctx = ctx;
}
EGLContext egl_tls_t::getContext() {
if (sKey == -1)
return EGL_NO_CONTEXT;
egl_tls_t* tls = (egl_tls_t *)pthread_getspecific(sKey);
if (!tls) return EGL_NO_CONTEXT;
return tls->ctx;
}
} // namespace android

63
opengl/libs/EGL/egl_tls.h Normal file
View File

@ -0,0 +1,63 @@
/*
** 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_TLS_H
#define ANDROID_EGL_TLS_H
#include <pthread.h>
#include <EGL/egl.h>
namespace android {
class DbgContext;
class egl_tls_t {
static pthread_key_t sKey;
static pthread_mutex_t sLockKey;
EGLint error;
EGLContext ctx;
EGLBoolean logCallWithNoContext;
DbgContext* dbg;
egl_tls_t();
static void validateTLSKey();
static void setErrorEtcImpl(const char* caller, int line, EGLint error);
public:
static egl_tls_t* getTLS();
static void clearTLS();
static void clearError();
static EGLint getError();
static void setContext(EGLContext ctx);
static EGLContext getContext();
static bool logNoContextCall();
static const char *egl_strerror(EGLint err);
template<typename T>
static T setErrorEtc(const char* caller,
int line, EGLint error, T returnValue) {
setErrorEtcImpl(caller, line, error);
return returnValue;
}
};
#define setError(_e, _r) egl_tls_t::setErrorEtc(__FUNCTION__, __LINE__, _e, _r)
}; // namespace android
#endif // ANDROID_EGL_TLS_H

View File

@ -15,22 +15,18 @@
*/
#include "header.h"
#include "egl_tls.h"
extern "C"
{
extern "C" {
#include "liblzf/lzf.h"
}
namespace android
{
namespace android {
pthread_key_t dbgEGLThreadLocalStorageKey = -1;
static pthread_key_t dbgEGLThreadLocalStorageKey = -1;
static pthread_mutex_t gThreadLocalStorageKeyMutex = PTHREAD_MUTEX_INITIALIZER;
DbgContext * getDbgContextThreadSpecific()
{
tls_t* tls = (tls_t*)pthread_getspecific(dbgEGLThreadLocalStorageKey);
return tls->dbg;
DbgContext * getDbgContextThreadSpecific() {
return (DbgContext*)pthread_getspecific(dbgEGLThreadLocalStorageKey);
}
DbgContext::DbgContext(const unsigned version, const gl_hooks_t * const hooks,
@ -60,10 +56,13 @@ DbgContext::~DbgContext()
free(lzf_ref[1]);
}
DbgContext * CreateDbgContext(const pthread_key_t EGLThreadLocalStorageKey,
const unsigned version, const gl_hooks_t * const hooks)
DbgContext* CreateDbgContext(const unsigned version, const gl_hooks_t * const hooks)
{
dbgEGLThreadLocalStorageKey = EGLThreadLocalStorageKey;
pthread_mutex_lock(&gThreadLocalStorageKeyMutex);
if (dbgEGLThreadLocalStorageKey == -1)
pthread_key_create(&dbgEGLThreadLocalStorageKey, NULL);
pthread_mutex_unlock(&gThreadLocalStorageKeyMutex);
assert(version < 2);
assert(GL_NO_ERROR == hooks->gl.glGetError());
GLint MAX_VERTEX_ATTRIBS = 0;
@ -71,7 +70,7 @@ DbgContext * CreateDbgContext(const pthread_key_t EGLThreadLocalStorageKey,
GLint readFormat, readType;
hooks->gl.glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT, &readFormat);
hooks->gl.glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE, &readType);
DbgContext * const dbg = new DbgContext(version, hooks, MAX_VERTEX_ATTRIBS, readFormat, readType);
DbgContext* dbg = new DbgContext(version, hooks, MAX_VERTEX_ATTRIBS, readFormat, readType);
glesv2debugger::Message msg, cmd;
msg.set_context_id(reinterpret_cast<int>(dbg));
@ -88,12 +87,13 @@ DbgContext * CreateDbgContext(const pthread_key_t EGLThreadLocalStorageKey,
msg.set_arg0(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS);
msg.set_arg1(MAX_COMBINED_TEXTURE_IMAGE_UNITS);
Send(msg, cmd);
*(DbgContext **)pthread_getspecific(dbgEGLThreadLocalStorageKey) = dbg;
return dbg;
}
void DestroyDbgContext(DbgContext * const dbg)
{
delete dbg;
void dbgReleaseThread() {
delete getDbgContextThreadSpecific();
}
unsigned GetBytesPerPixel(const GLenum format, const GLenum type)

View File

@ -16,14 +16,12 @@
#include "header.h"
#include "gtest/gtest.h"
#include "egl_tls.h"
#include "hooks.h"
namespace android
{
extern FILE * file;
extern unsigned int MAX_FILE_SIZE;
extern pthread_key_t dbgEGLThreadLocalStorageKey;
};
// tmpfile fails, so need to manually make a writable file first
@ -114,7 +112,7 @@ TEST_F(ServerFileTest, CreateDbgContext)
};
hooks.gl.glGetError = HookMock::GetError;
hooks.gl.glGetIntegerv = HookMock::GetIntegerv;
DbgContext * const dbg = CreateDbgContext(-1, 1, &hooks);
DbgContext * const dbg = CreateDbgContext(1, &hooks);
ASSERT_TRUE(dbg != NULL);
EXPECT_TRUE(dbg->vertexAttribs != NULL);
@ -132,7 +130,7 @@ TEST_F(ServerFileTest, CreateDbgContext)
EXPECT_EQ(expectedConstant, read.arg1());
}
CheckNoAvailable();
DestroyDbgContext(dbg);
dbgReleaseThread();
}
void * glNoop()
@ -143,7 +141,7 @@ void * glNoop()
class ServerFileContextTest : public ServerFileTest
{
protected:
tls_t tls;
DbgContext* dbg;
gl_hooks_t hooks;
ServerFileContextTest() { }
@ -153,12 +151,8 @@ protected:
virtual void SetUp() {
ServerFileTest::SetUp();
if (dbgEGLThreadLocalStorageKey == -1)
pthread_key_create(&dbgEGLThreadLocalStorageKey, NULL);
ASSERT_NE(-1, dbgEGLThreadLocalStorageKey);
tls.dbg = new DbgContext(1, &hooks, 32, GL_RGBA, GL_UNSIGNED_BYTE);
ASSERT_NE((void *)NULL, tls.dbg);
pthread_setspecific(dbgEGLThreadLocalStorageKey, &tls);
dbg = new DbgContext(1, &hooks, 32, GL_RGBA, GL_UNSIGNED_BYTE);
ASSERT_NE((void *)NULL, dbg);
for (unsigned int i = 0; i < sizeof(hooks) / sizeof(void *); i++)
((void **)&hooks)[i] = reinterpret_cast<void *>(glNoop);
}
@ -183,7 +177,7 @@ TEST_F(ServerFileContextTest, MessageLoop)
return ret;
}
} caller;
const int contextId = reinterpret_cast<int>(tls.dbg);
const int contextId = reinterpret_cast<int>(dbg);
glesv2debugger::Message msg, read;
EXPECT_EQ(ret, MessageLoop(caller, msg, msg.glFinish));
@ -214,25 +208,25 @@ TEST_F(ServerFileContextTest, MessageLoop)
TEST_F(ServerFileContextTest, DisableEnableVertexAttribArray)
{
Debug_glEnableVertexAttribArray(tls.dbg->MAX_VERTEX_ATTRIBS + 2); // should just ignore invalid index
Debug_glEnableVertexAttribArray(dbg->MAX_VERTEX_ATTRIBS + 2); // should just ignore invalid index
glesv2debugger::Message read;
rewind(file);
Read(read);
EXPECT_EQ(read.glEnableVertexAttribArray, read.function());
EXPECT_EQ(tls.dbg->MAX_VERTEX_ATTRIBS + 2, read.arg0());
EXPECT_EQ(dbg->MAX_VERTEX_ATTRIBS + 2, read.arg0());
Read(read);
rewind(file);
Debug_glDisableVertexAttribArray(tls.dbg->MAX_VERTEX_ATTRIBS + 4); // should just ignore invalid index
Debug_glDisableVertexAttribArray(dbg->MAX_VERTEX_ATTRIBS + 4); // should just ignore invalid index
rewind(file);
Read(read);
Read(read);
for (unsigned int i = 0; i < tls.dbg->MAX_VERTEX_ATTRIBS; i += 5) {
for (unsigned int i = 0; i < dbg->MAX_VERTEX_ATTRIBS; i += 5) {
rewind(file);
Debug_glEnableVertexAttribArray(i);
EXPECT_TRUE(tls.dbg->vertexAttribs[i].enabled);
EXPECT_TRUE(dbg->vertexAttribs[i].enabled);
rewind(file);
Read(read);
EXPECT_EQ(read.glEnableVertexAttribArray, read.function());
@ -241,7 +235,7 @@ TEST_F(ServerFileContextTest, DisableEnableVertexAttribArray)
rewind(file);
Debug_glDisableVertexAttribArray(i);
EXPECT_FALSE(tls.dbg->vertexAttribs[i].enabled);
EXPECT_FALSE(dbg->vertexAttribs[i].enabled);
rewind(file);
Read(read);
EXPECT_EQ(read.glDisableVertexAttribArray, read.function());

View File

@ -19,13 +19,11 @@
#include "header.h"
#include "gtest/gtest.h"
#include "egl_tls.h"
#include "hooks.h"
namespace android
{
extern int serverSock, clientSock;
extern pthread_key_t dbgEGLThreadLocalStorageKey;
};
void * glNoop();
@ -33,7 +31,7 @@ void * glNoop();
class SocketContextTest : public ::testing::Test
{
protected:
tls_t tls;
DbgContext* dbg;
gl_hooks_t hooks;
int sock;
char * buffer;
@ -46,12 +44,8 @@ protected:
}
virtual void SetUp() {
if (dbgEGLThreadLocalStorageKey == -1)
pthread_key_create(&dbgEGLThreadLocalStorageKey, NULL);
ASSERT_NE(-1, dbgEGLThreadLocalStorageKey);
tls.dbg = new DbgContext(1, &hooks, 32, GL_RGBA, GL_UNSIGNED_BYTE);
ASSERT_TRUE(tls.dbg != NULL);
pthread_setspecific(dbgEGLThreadLocalStorageKey, &tls);
dbg = new DbgContext(1, &hooks, 32, GL_RGBA, GL_UNSIGNED_BYTE);
ASSERT_TRUE(dbg != NULL);
for (unsigned int i = 0; i < sizeof(hooks) / sizeof(void *); i++)
((void **)&hooks)[i] = (void *)glNoop;
@ -73,7 +67,7 @@ protected:
}
void Write(glesv2debugger::Message & msg) const {
msg.set_context_id((int)tls.dbg);
msg.set_context_id((int)dbg);
msg.set_type(msg.Response);
ASSERT_TRUE(msg.has_context_id());
ASSERT_TRUE(msg.has_function());
@ -129,7 +123,7 @@ TEST_F(SocketContextTest, MessageLoopSkip)
}
} caller;
glesv2debugger::Message msg, read, cmd;
tls.dbg->expectResponse.Bit(msg.glFinish, true);
dbg->expectResponse.Bit(msg.glFinish, true);
cmd.set_function(cmd.SKIP);
cmd.set_expect_response(false);
@ -158,7 +152,7 @@ TEST_F(SocketContextTest, MessageLoopContinue)
}
} caller;
glesv2debugger::Message msg, read, cmd;
tls.dbg->expectResponse.Bit(msg.glCreateShader, true);
dbg->expectResponse.Bit(msg.glCreateShader, true);
cmd.set_function(cmd.CONTINUE);
cmd.set_expect_response(false); // MessageLoop should automatically skip after continue
@ -204,7 +198,7 @@ TEST_F(SocketContextTest, MessageLoopGenerateCall)
glesv2debugger::Message msg, read, cmd;
hooks.gl.glCreateShader = caller.CreateShader;
hooks.gl.glCreateProgram = caller.CreateProgram;
tls.dbg->expectResponse.Bit(msg.glCreateProgram, true);
dbg->expectResponse.Bit(msg.glCreateProgram, true);
cmd.set_function(cmd.glCreateShader);
cmd.set_arg0(GL_FRAGMENT_SHADER);
@ -272,7 +266,7 @@ TEST_F(SocketContextTest, MessageLoopSetProp)
glesv2debugger::Message msg, read, cmd;
hooks.gl.glCreateShader = caller.CreateShader;
hooks.gl.glCreateProgram = caller.CreateProgram;
tls.dbg->expectResponse.Bit(msg.glCreateProgram, false);
dbg->expectResponse.Bit(msg.glCreateProgram, false);
cmd.set_function(cmd.SETPROP);
cmd.set_prop(cmd.ExpectResponse);
@ -305,8 +299,8 @@ TEST_F(SocketContextTest, MessageLoopSetProp)
EXPECT_EQ((int *)ret, MessageLoop(caller, msg, msg.glCreateProgram));
EXPECT_TRUE(tls.dbg->expectResponse.Bit(msg.glCreateProgram));
EXPECT_EQ(819, tls.dbg->captureDraw);
EXPECT_TRUE(dbg->expectResponse.Bit(msg.glCreateProgram));
EXPECT_EQ(819, dbg->captureDraw);
Read(read);
EXPECT_EQ(read.glCreateProgram, read.function());
@ -362,7 +356,7 @@ TEST_F(SocketContextTest, TexImage2D)
} caller;
glesv2debugger::Message msg, read, cmd;
hooks.gl.glTexImage2D = caller.TexImage2D;
tls.dbg->expectResponse.Bit(msg.glTexImage2D, false);
dbg->expectResponse.Bit(msg.glTexImage2D, false);
Debug_glTexImage2D(_target, _level, _internalformat, _width, _height, _border,
_format, _type, _pixels);
@ -382,7 +376,7 @@ TEST_F(SocketContextTest, TexImage2D)
EXPECT_TRUE(read.has_data());
uint32_t dataLen = 0;
const unsigned char * data = tls.dbg->Decompress(read.data().data(),
const unsigned char * data = dbg->Decompress(read.data().data(),
read.data().length(), &dataLen);
EXPECT_EQ(sizeof(_pixels), dataLen);
if (sizeof(_pixels) == dataLen)
@ -435,7 +429,7 @@ TEST_F(SocketContextTest, CopyTexImage2D)
glesv2debugger::Message msg, read, cmd;
hooks.gl.glCopyTexImage2D = caller.CopyTexImage2D;
hooks.gl.glReadPixels = caller.ReadPixels;
tls.dbg->expectResponse.Bit(msg.glCopyTexImage2D, false);
dbg->expectResponse.Bit(msg.glCopyTexImage2D, false);
Debug_glCopyTexImage2D(_target, _level, _internalformat, _x, _y, _width, _height,
_border);
@ -459,7 +453,7 @@ TEST_F(SocketContextTest, CopyTexImage2D)
EXPECT_EQ(GL_RGBA, read.pixel_format());
EXPECT_EQ(GL_UNSIGNED_BYTE, read.pixel_type());
uint32_t dataLen = 0;
unsigned char * const data = tls.dbg->Decompress(read.data().data(),
unsigned char * const data = dbg->Decompress(read.data().data(),
read.data().length(), &dataLen);
ASSERT_EQ(sizeof(_pixels), dataLen);
for (unsigned i = 0; i < sizeof(_pixels) / sizeof(*_pixels); i++)

View File

@ -25,6 +25,9 @@
#include "hooks.h"
#define VERSION_MAJOR 1
#define VERSION_MINOR 4
// ----------------------------------------------------------------------------
namespace android {
// ----------------------------------------------------------------------------
@ -41,6 +44,8 @@ struct egl_connection_t
EGLAPI EGLImageKHR egl_get_image_for_current_context(EGLImageKHR image);
extern egl_connection_t gEGLImpl[IMPL_NUM_IMPLEMENTATIONS];
// ----------------------------------------------------------------------------
}; // namespace android
// ----------------------------------------------------------------------------

View File

@ -1,40 +0,0 @@
/*
** 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_TLS_H
#define ANDROID_EGL_TLS_H
#include <EGL/egl.h>
#include "glesv2dbg.h"
namespace android
{
struct tls_t {
tls_t() : error(EGL_SUCCESS), ctx(0), logCallWithNoContext(EGL_TRUE), dbg(0) { }
~tls_t() {
if (dbg)
DestroyDbgContext(dbg);
}
EGLint error;
EGLContext ctx;
EGLBoolean logCallWithNoContext;
DbgContext* dbg;
};
}
#endif

View File

@ -23,10 +23,9 @@ namespace android
{
struct DbgContext;
DbgContext * CreateDbgContext(const pthread_key_t EGLThreadLocalStorageKey,
const unsigned version, const gl_hooks_t * const hooks);
DbgContext* CreateDbgContext(const unsigned version, const gl_hooks_t * const hooks);
void DestroyDbgContext(DbgContext * const dbg);
void dbgReleaseThread();
// create and bind socket if haven't already, if failed to create socket or
// forceUseFile, then open /data/local/tmp/dump.gles2dbg, exit when size reached