From 206ea399b5461adda14d7ec6b6177c7b9a03923a Mon Sep 17 00:00:00 2001 From: Andrew Hsieh Date: Thu, 22 Mar 2012 01:27:44 +0800 Subject: [PATCH] Refactor with ToTargetCompatibleHandle() Refactor statements doing host-to-target handle conversion, eg. uintptr_t hndlptr = (uintptr_t)hostHandle; unsigned int hndl = (unsigned int)hndlptr; assert(sizeof(hndl) == sizeof(hndlptr) || hndl == hndlptr); into a call to ToTargetCompatibleHandle() Change-Id: I0bcfb37f1b50679d29e7f21fe230ad433fbbef7c --- .../host/libs/Translator/EGL/EglDisplay.cpp | 43 ++++++------------- .../libs/Translator/GLES_CM/GLEScmImp.cpp | 9 +--- .../libs/Translator/GLES_V2/GLESv2Imp.cpp | 9 +--- .../libs/Translator/GLcommon/GLEScontext.cpp | 5 +-- .../Translator/include/GLcommon/GLutils.h | 16 +++++++ 5 files changed, 33 insertions(+), 49 deletions(-) diff --git a/tools/emulator/opengl/host/libs/Translator/EGL/EglDisplay.cpp b/tools/emulator/opengl/host/libs/Translator/EGL/EglDisplay.cpp index c2b515e47..99f9d8b1e 100644 --- a/tools/emulator/opengl/host/libs/Translator/EGL/EglDisplay.cpp +++ b/tools/emulator/opengl/host/libs/Translator/EGL/EglDisplay.cpp @@ -17,7 +17,6 @@ #include "EglOsApi.h" #include #include -#include EglDisplay::EglDisplay(EGLNativeInternalDisplayType dpy,bool isDefault) : m_dpy(dpy), @@ -142,11 +141,8 @@ EglConfig* EglDisplay::getConfig(EGLConfig conf) { SurfacePtr EglDisplay::getSurface(EGLSurface surface) { android::Mutex::Autolock mutex(m_lock); - /* surface is "key" in map. - In 64-bit the upper 32-bit should be all zero. Assert for that. */ - uintptr_t hndlptr = (uintptr_t)surface; - unsigned int hndl = (unsigned int)hndlptr; - assert(sizeof(hndl) == sizeof(hndlptr) || hndl == hndlptr); + /* surface is "key" in map. */ + unsigned int hndl = ToTargetCompatibleHandle((uintptr_t)surface); SurfacesHndlMap::iterator it = m_surfaces.find(hndl); return it != m_surfaces.end() ? (*it).second : @@ -155,11 +151,8 @@ SurfacePtr EglDisplay::getSurface(EGLSurface surface) { ContextPtr EglDisplay::getContext(EGLContext ctx) { android::Mutex::Autolock mutex(m_lock); - /* ctx is "key" in map. - In 64-bit the upper 32-bit should be all zero. Assert for that. */ - uintptr_t hndlptr = (uintptr_t)ctx; - unsigned int hndl = (unsigned int)hndlptr; - assert(sizeof(hndl) == sizeof(hndlptr) || hndl == hndlptr); + /* ctx is "key" in map. */ + unsigned int hndl = ToTargetCompatibleHandle((uintptr_t)ctx); ContextsHndlMap::iterator it = m_contexts.find(hndl); return it != m_contexts.end() ? (*it).second : @@ -168,11 +161,8 @@ ContextPtr EglDisplay::getContext(EGLContext ctx) { bool EglDisplay::removeSurface(EGLSurface s) { android::Mutex::Autolock mutex(m_lock); - /* s is "key" in map. - In 64-bit the upper 32-bit should be all zero. Assert for that. */ - uintptr_t hndlptr = (uintptr_t)s; - unsigned int hndl = (unsigned int)hndlptr; - assert(sizeof(hndl) == sizeof(hndlptr) || hndl == hndlptr); + /* s is "key" in map. */ + unsigned int hndl = ToTargetCompatibleHandle((uintptr_t)s); SurfacesHndlMap::iterator it = m_surfaces.find(hndl); if(it != m_surfaces.end()) { m_surfaces.erase(it); @@ -200,11 +190,8 @@ bool EglDisplay::removeSurface(SurfacePtr s) { bool EglDisplay::removeContext(EGLContext ctx) { android::Mutex::Autolock mutex(m_lock); - /* ctx is "key" in map. - In 64-bit the upper 32-bit should be all zero. Assert for that. */ - uintptr_t hndlptr = (uintptr_t)ctx; - unsigned int hndl = (unsigned int)hndlptr; - assert(sizeof(hndl) == sizeof(hndlptr) || hndl == hndlptr); + /* ctx is "key" in map. */ + unsigned int hndl = ToTargetCompatibleHandle((uintptr_t)ctx); ContextsHndlMap::iterator it = m_contexts.find(hndl); if(it != m_contexts.end()) { m_contexts.erase(it); @@ -307,22 +294,16 @@ EGLImageKHR EglDisplay::addImageKHR(ImagePtr img) { ImagePtr EglDisplay::getImage(EGLImageKHR img) { android::Mutex::Autolock mutex(m_lock); - /* img is "key" in map. - In 64-bit the upper 32-bit should be all zero. Assert for that. */ - uintptr_t hndlptr = (uintptr_t)img; - unsigned int hndl = (unsigned int)hndlptr; - assert(sizeof(hndl) == sizeof(hndlptr) || hndl == hndlptr); + /* img is "key" in map. */ + unsigned int hndl = ToTargetCompatibleHandle((uintptr_t)img); ImagesHndlMap::iterator i( m_eglImages.find(hndl) ); return (i != m_eglImages.end()) ? (*i).second :ImagePtr(NULL); } bool EglDisplay:: destroyImageKHR(EGLImageKHR img) { android::Mutex::Autolock mutex(m_lock); - /* img is "key" in map. - In 64-bit the upper 32-bit should be all zero. Assert for that. */ - uintptr_t hndlptr = (uintptr_t)img; - unsigned int hndl = (unsigned int)hndlptr; - assert(sizeof(hndl) == sizeof(hndlptr) || hndl == hndlptr); + /* img is "key" in map. */ + unsigned int hndl = ToTargetCompatibleHandle((uintptr_t)img); ImagesHndlMap::iterator i( m_eglImages.find(hndl) ); if (i != m_eglImages.end()) { diff --git a/tools/emulator/opengl/host/libs/Translator/GLES_CM/GLEScmImp.cpp b/tools/emulator/opengl/host/libs/Translator/GLES_CM/GLEScmImp.cpp index ae903776b..b78a0220b 100644 --- a/tools/emulator/opengl/host/libs/Translator/GLES_CM/GLEScmImp.cpp +++ b/tools/emulator/opengl/host/libs/Translator/GLES_CM/GLEScmImp.cpp @@ -35,7 +35,6 @@ #include #include #include -#include extern "C" { @@ -1656,9 +1655,7 @@ GL_API void GL_APIENTRY glEGLImageTargetTexture2DOES(GLenum target, GLeglImageOE { GET_CTX(); SET_ERROR_IF(!GLEScmValidate::textureTargetLimited(target),GL_INVALID_ENUM); - uintptr_t imagehndlptr = (uintptr_t)image; - unsigned int imagehndl = (unsigned int)imagehndlptr; - assert(sizeof(imagehndl) == sizeof(imagehndlptr) || imagehndl == imagehndlptr); + unsigned int imagehndl = ToTargetCompatibleHandle((uintptr_t)image); EglImage *img = s_eglIface->eglAttachEGLImage(imagehndl); if (img) { // Create the texture object in the underlying EGL implementation, @@ -1694,9 +1691,7 @@ GL_API void GL_APIENTRY glEGLImageTargetRenderbufferStorageOES(GLenum target, GL { GET_CTX(); SET_ERROR_IF(target != GL_RENDERBUFFER_OES,GL_INVALID_ENUM); - uintptr_t imagehndlptr = (uintptr_t)image; - unsigned int imagehndl = (unsigned int)imagehndlptr; - assert(sizeof(imagehndl) == sizeof(imagehndlptr) || imagehndl == imagehndlptr); + unsigned int imagehndl = ToTargetCompatibleHandle((uintptr_t)image); EglImage *img = s_eglIface->eglAttachEGLImage(imagehndl); SET_ERROR_IF(!img,GL_INVALID_VALUE); SET_ERROR_IF(!ctx->shareGroup().Ptr(),GL_INVALID_OPERATION); diff --git a/tools/emulator/opengl/host/libs/Translator/GLES_V2/GLESv2Imp.cpp b/tools/emulator/opengl/host/libs/Translator/GLES_V2/GLESv2Imp.cpp index 2e746cf13..412584d5d 100644 --- a/tools/emulator/opengl/host/libs/Translator/GLES_V2/GLESv2Imp.cpp +++ b/tools/emulator/opengl/host/libs/Translator/GLES_V2/GLESv2Imp.cpp @@ -32,7 +32,6 @@ #include "ProgramData.h" #include #include -#include extern "C" { @@ -2009,9 +2008,7 @@ GL_APICALL void GL_APIENTRY glEGLImageTargetTexture2DOES(GLenum target, GLeglIma { GET_CTX(); SET_ERROR_IF(!GLESv2Validate::textureTargetLimited(target),GL_INVALID_ENUM); - uintptr_t imagehndlptr = (uintptr_t)image; - unsigned int imagehndl = (unsigned int)imagehndlptr; - assert(sizeof(imagehndl) == sizeof(imagehndlptr) || imagehndl == imagehndlptr); + unsigned int imagehndl = ToTargetCompatibleHandle((uintptr_t)image); EglImage *img = s_eglIface->eglAttachEGLImage(imagehndl); if (img) { // Create the texture object in the underlying EGL implementation, @@ -2047,9 +2044,7 @@ GL_APICALL void GL_APIENTRY glEGLImageTargetRenderbufferStorageOES(GLenum target { GET_CTX(); SET_ERROR_IF(target != GL_RENDERBUFFER_OES,GL_INVALID_ENUM); - uintptr_t imagehndlptr = (uintptr_t)image; - unsigned int imagehndl = (unsigned int)imagehndlptr; - assert(sizeof(imagehndl) == sizeof(imagehndlptr) || imagehndl == imagehndlptr); + unsigned int imagehndl = ToTargetCompatibleHandle((uintptr_t)image); EglImage *img = s_eglIface->eglAttachEGLImage(imagehndl); SET_ERROR_IF(!img,GL_INVALID_VALUE); SET_ERROR_IF(!ctx->shareGroup().Ptr(),GL_INVALID_OPERATION); diff --git a/tools/emulator/opengl/host/libs/Translator/GLcommon/GLEScontext.cpp b/tools/emulator/opengl/host/libs/Translator/GLcommon/GLEScontext.cpp index d77a3d51c..cff639eff 100644 --- a/tools/emulator/opengl/host/libs/Translator/GLcommon/GLEScontext.cpp +++ b/tools/emulator/opengl/host/libs/Translator/GLcommon/GLEScontext.cpp @@ -23,7 +23,6 @@ #include #include #include -#include //decleration static void convertFixedDirectLoop(const char* dataIn,unsigned int strideIn,void* dataOut,unsigned int nBytes,unsigned int strideOut,int attribSize); @@ -190,9 +189,7 @@ GLEScontext::~GLEScontext() { const GLvoid* GLEScontext::setPointer(GLenum arrType,GLint size,GLenum type,GLsizei stride,const GLvoid* data,bool normalize) { GLuint bufferName = m_arrayBuffer; if(bufferName) { - uintptr_t offsetptr = (uintptr_t)data; - unsigned int offset = offsetptr; - assert(sizeof(offset) == sizeof(offsetptr) || offset == offsetptr); + unsigned int offset = ToTargetCompatibleHandle((uintptr_t)data); GLESbuffer* vbo = static_cast(m_shareGroup->getObjectData(VERTEXBUFFER,bufferName).Ptr()); m_map[arrType]->setBuffer(size,type,stride,vbo,bufferName,offset,normalize); return static_cast(vbo->getData()) + offset; diff --git a/tools/emulator/opengl/host/libs/Translator/include/GLcommon/GLutils.h b/tools/emulator/opengl/host/libs/Translator/include/GLcommon/GLutils.h index 7e2a03835..2aed64629 100644 --- a/tools/emulator/opengl/host/libs/Translator/include/GLcommon/GLutils.h +++ b/tools/emulator/opengl/host/libs/Translator/include/GLcommon/GLutils.h @@ -16,6 +16,9 @@ #ifndef GL_UTILS_H #define GL_UTILS_H +#include +#include + typedef enum{ GLES_1_1 = 1, GLES_2_0 = 2, @@ -32,4 +35,17 @@ void swap(T& x,T& y) { bool isPowerOf2(int num); +inline +unsigned int ToTargetCompatibleHandle(uintptr_t hostHandle) +{ + // The host and target handles can have different sizes (e.g. 32-bit + // target handle for ARM, and 64-bit host handle on x86_64). + // This function checks that the input host handle value can be + // converted into a target handle one without losing any bits. + // + unsigned int targetHandle = (unsigned int)hostHandle; + assert(sizeof(targetHandle) == sizeof(hostHandle) || targetHandle == hostHandle); + return targetHandle; +} + #endif