diff options
Diffstat (limited to 'emulator/opengl/shared/OpenglCodecCommon')
11 files changed, 193 insertions, 434 deletions
diff --git a/emulator/opengl/shared/OpenglCodecCommon/Android.mk b/emulator/opengl/shared/OpenglCodecCommon/Android.mk index 5deb1f7..83090d8 100644 --- a/emulator/opengl/shared/OpenglCodecCommon/Android.mk +++ b/emulator/opengl/shared/OpenglCodecCommon/Android.mk @@ -24,19 +24,21 @@ endif $(call emugl-begin-host-static-library,libOpenglCodecCommon) LOCAL_SRC_FILES := $(host_commonSources) - -$(call emugl-export,STATIC_LIBRARIES,libcutils) -$(call emugl-export,C_INCLUDES,$(LOCAL_PATH)) +$(call emugl-import, libemugl_common) +$(call emugl-export,C_INCLUDES,$(EMUGL_PATH)/host/include/libOpenglRender $(LOCAL_PATH)) +$(call emugl-export,LDLIBS,-lstdc++) $(call emugl-end-module) ### OpenglCodecCommon host, 64-bit ######################################### -$(call emugl-begin-host-static-library,lib64OpenglCodecCommon) +ifdef EMUGL_BUILD_64BITS + $(call emugl-begin-host64-static-library,lib64OpenglCodecCommon) -LOCAL_SRC_FILES := $(host_commonSources) - -$(call emugl-export,STATIC_LIBRARIES,lib64cutils) -$(call emugl-export,C_INCLUDES,$(LOCAL_PATH)) -$(call emugl-export,CFLAGS,-m64) -$(call emugl-end-module) + LOCAL_SRC_FILES := $(host_commonSources) + $(call emugl-import, lib64emugl_common) + $(call emugl-export,C_INCLUDES,$(EMUGL_PATH)/host/include/libOpenglRender $(LOCAL_PATH)) + $(call emugl-export,CFLAGS,-m64 -fPIC) + $(call emugl-export,LDLIBS,-lstdc++) + $(call emugl-end-module) +endif diff --git a/emulator/opengl/shared/OpenglCodecCommon/ErrorLog.h b/emulator/opengl/shared/OpenglCodecCommon/ErrorLog.h index 6f41fd7..4cad61f 100644 --- a/emulator/opengl/shared/OpenglCodecCommon/ErrorLog.h +++ b/emulator/opengl/shared/OpenglCodecCommon/ErrorLog.h @@ -16,22 +16,12 @@ #ifndef _ERROR_LOG_H_ #define _ERROR_LOG_H_ -#if (HAVE_ANDROID_OS == 1) -# include <cutils/log.h> -# define ERR(...) ALOGE(__VA_ARGS__) -# ifdef EMUGL_DEBUG -# define DBG(...) ALOGD(__VA_ARGS__) -# else -# define DBG(...) ((void)0) -# endif +#include <stdio.h> +#define ERR(...) fprintf(stderr, __VA_ARGS__) +#ifdef EMUGL_DEBUG +# define DBG(...) fprintf(stderr, __VA_ARGS__) #else -# include <stdio.h> -# define ERR(...) fprintf(stderr, __VA_ARGS__) -# ifdef EMUGL_DEBUG -# define DBG(...) fprintf(stderr, __VA_ARGS__) -# else -# define DBG(...) ((void)0) -# endif +# define DBG(...) ((void)0) #endif -#endif +#endif // _ERROR_LOG_H_ diff --git a/emulator/opengl/shared/OpenglCodecCommon/GLClientState.cpp b/emulator/opengl/shared/OpenglCodecCommon/GLClientState.cpp index 9795490..b02131c 100644 --- a/emulator/opengl/shared/OpenglCodecCommon/GLClientState.cpp +++ b/emulator/opengl/shared/OpenglCodecCommon/GLClientState.cpp @@ -19,7 +19,6 @@ #include <stdlib.h> #include <string.h> #include "glUtils.h" -#include <cutils/log.h> #ifndef MAX #define MAX(a, b) ((a) < (b) ? (b) : (a)) diff --git a/emulator/opengl/shared/OpenglCodecCommon/GLDecoderContextData.h b/emulator/opengl/shared/OpenglCodecCommon/GLDecoderContextData.h index 23785ae..d5b5189 100644 --- a/emulator/opengl/shared/OpenglCodecCommon/GLDecoderContextData.h +++ b/emulator/opengl/shared/OpenglCodecCommon/GLDecoderContextData.h @@ -53,12 +53,12 @@ public: void storePointerData(unsigned int loc, void *data, size_t len) { - assert(loc < m_nLocations); + assert(loc < (unsigned)m_nLocations); m_pointerData[loc].alloc(len); memcpy(m_pointerData[loc].ptr(), data, len); } void *pointerData(unsigned int loc) { - assert(loc < m_nLocations); + assert(loc < (unsigned)m_nLocations); return m_pointerData[loc].ptr(); } private: diff --git a/emulator/opengl/shared/OpenglCodecCommon/GLSharedGroup.cpp b/emulator/opengl/shared/OpenglCodecCommon/GLSharedGroup.cpp index c7da37a..59f7b97 100644 --- a/emulator/opengl/shared/OpenglCodecCommon/GLSharedGroup.cpp +++ b/emulator/opengl/shared/OpenglCodecCommon/GLSharedGroup.cpp @@ -16,23 +16,22 @@ #include "GLSharedGroup.h" -/**** KeyedVector utilities ****/ - -template <typename T> -static void clearObjectMap(android::DefaultKeyedVector<GLuint, T>& v) { - for (size_t i = 0; i < v.size(); i++) - delete v.valueAt(i); - v.clear(); -} +#include <string.h> /**** BufferData ****/ BufferData::BufferData() : m_size(0) {}; + BufferData::BufferData(GLsizeiptr size, void * data) : m_size(size) { - void * buffer = NULL; - if (size>0) buffer = m_fixedBuffer.alloc(size); - if (data) memcpy(buffer, data, size); + void* buffer = NULL; + + if (size > 0) { + buffer = m_fixedBuffer.alloc(size); + if (data) { + memcpy(buffer, data, size); + } + } } /**** ProgramData ****/ @@ -204,9 +203,7 @@ bool ProgramData::attachShader(GLuint shader) return false; } } - // AKA m_shaders.push_back(), but that has an ambiguous call to insertAt() - // due to the default parameters. This is the desired insertAt() overload. - m_shaders.insertAt(shader, m_shaders.size(), 1); + m_shaders.append(shader); return true; } @@ -215,7 +212,7 @@ bool ProgramData::detachShader(GLuint shader) size_t n = m_shaders.size(); for (size_t i = 0; i < n; i++) { if (m_shaders[i] == shader) { - m_shaders.removeAt(i); + m_shaders.remove(i); return true; } } @@ -225,49 +222,32 @@ bool ProgramData::detachShader(GLuint shader) /***** GLSharedGroup ****/ GLSharedGroup::GLSharedGroup() : - m_buffers(android::DefaultKeyedVector<GLuint, BufferData*>(NULL)), - m_programs(android::DefaultKeyedVector<GLuint, ProgramData*>(NULL)), - m_shaders(android::DefaultKeyedVector<GLuint, ShaderData*>(NULL)) -{ -} + m_buffers(), m_programs(), m_shaders() {} -GLSharedGroup::~GLSharedGroup() -{ - m_buffers.clear(); - m_programs.clear(); - clearObjectMap(m_buffers); - clearObjectMap(m_programs); - clearObjectMap(m_shaders); -} +GLSharedGroup::~GLSharedGroup() {} BufferData * GLSharedGroup::getBufferData(GLuint bufferId) { - android::AutoMutex _lock(m_lock); - return m_buffers.valueFor(bufferId); + emugl::Mutex::AutoLock _lock(m_lock); + return m_buffers.get(bufferId); } void GLSharedGroup::addBufferData(GLuint bufferId, GLsizeiptr size, void * data) { - android::AutoMutex _lock(m_lock); - m_buffers.add(bufferId, new BufferData(size, data)); + emugl::Mutex::AutoLock _lock(m_lock); + m_buffers.set(bufferId, new BufferData(size, data)); } void GLSharedGroup::updateBufferData(GLuint bufferId, GLsizeiptr size, void * data) { - android::AutoMutex _lock(m_lock); - ssize_t idx = m_buffers.indexOfKey(bufferId); - if (idx >= 0) { - delete m_buffers.valueAt(idx); - m_buffers.editValueAt(idx) = new BufferData(size, data); - } else { - m_buffers.add(bufferId, new BufferData(size, data)); - } + emugl::Mutex::AutoLock _lock(m_lock); + m_buffers.set(bufferId, new BufferData(size, data)); } GLenum GLSharedGroup::subUpdateBufferData(GLuint bufferId, GLintptr offset, GLsizeiptr size, void * data) { - android::AutoMutex _lock(m_lock); - BufferData * buf = m_buffers.valueFor(bufferId); + emugl::Mutex::AutoLock _lock(m_lock); + BufferData * buf = m_buffers.get(bufferId); if ((!buf) || (buf->m_size < offset+size) || (offset < 0) || (size<0)) return GL_INVALID_VALUE; //it's safe to update now @@ -277,215 +257,197 @@ GLenum GLSharedGroup::subUpdateBufferData(GLuint bufferId, GLintptr offset, GLsi void GLSharedGroup::deleteBufferData(GLuint bufferId) { - android::AutoMutex _lock(m_lock); - ssize_t idx = m_buffers.indexOfKey(bufferId); - if (idx >= 0) { - delete m_buffers.valueAt(idx); - m_buffers.removeItemsAt(idx); - } + emugl::Mutex::AutoLock _lock(m_lock); + (void) m_buffers.remove(bufferId); } void GLSharedGroup::addProgramData(GLuint program) { - android::AutoMutex _lock(m_lock); - ProgramData *pData = m_programs.valueFor(program); - if (pData) - { - m_programs.removeItem(program); - delete pData; - } - - m_programs.add(program,new ProgramData()); + emugl::Mutex::AutoLock _lock(m_lock); + m_programs.set(program, new ProgramData()); } void GLSharedGroup::initProgramData(GLuint program, GLuint numIndexes) { - android::AutoMutex _lock(m_lock); - ProgramData *pData = m_programs.valueFor(program); - if (pData) - { + emugl::Mutex::AutoLock _lock(m_lock); + ProgramData *pData = m_programs.get(program); + if (pData) { pData->initProgramData(numIndexes); } } bool GLSharedGroup::isProgramInitialized(GLuint program) { - android::AutoMutex _lock(m_lock); - ProgramData* pData = m_programs.valueFor(program); - if (pData) - { - return pData->isInitialized(); - } - return false; + emugl::Mutex::AutoLock _lock(m_lock); + ProgramData* pData = m_programs.get(program); + return pData && pData->isInitialized(); } void GLSharedGroup::deleteProgramData(GLuint program) { - android::AutoMutex _lock(m_lock); - ProgramData *pData = m_programs.valueFor(program); - if (pData) - delete pData; - m_programs.removeItem(program); + emugl::Mutex::AutoLock _lock(m_lock); + m_programs.remove(program); } void GLSharedGroup::attachShader(GLuint program, GLuint shader) { - android::AutoMutex _lock(m_lock); - ProgramData* programData = m_programs.valueFor(program); - ssize_t idx = m_shaders.indexOfKey(shader); - if (programData && idx >= 0) { - if (programData->attachShader(shader)) { - refShaderDataLocked(idx); - } + emugl::Mutex::AutoLock _lock(m_lock); + ProgramData* programData = m_programs.get(program); + if (programData && programData->attachShader(shader)) { + refShaderDataLocked(shader); } } void GLSharedGroup::detachShader(GLuint program, GLuint shader) { - android::AutoMutex _lock(m_lock); - ProgramData* programData = m_programs.valueFor(program); - ssize_t idx = m_shaders.indexOfKey(shader); - if (programData && idx >= 0) { - if (programData->detachShader(shader)) { - unrefShaderDataLocked(idx); - } + emugl::Mutex::AutoLock _lock(m_lock); + ProgramData* programData = m_programs.get(program); + if (programData && programData->detachShader(shader)) { + unrefShaderDataLocked(shader); } } -void GLSharedGroup::setProgramIndexInfo(GLuint program, GLuint index, GLint base, GLint size, GLenum type, const char* name) +void GLSharedGroup::setProgramIndexInfo(GLuint program, + GLuint index, + GLint base, + GLint size, + GLenum type, + const char* name) { - android::AutoMutex _lock(m_lock); - ProgramData* pData = m_programs.valueFor(program); - if (pData) - { - pData->setIndexInfo(index,base,size,type); - - if (type == GL_SAMPLER_2D) { - size_t n = pData->getNumShaders(); - for (size_t i = 0; i < n; i++) { - GLuint shaderId = pData->getShader(i); - ShaderData* shader = m_shaders.valueFor(shaderId); - if (!shader) continue; - ShaderData::StringList::iterator nameIter = shader->samplerExternalNames.begin(); - ShaderData::StringList::iterator nameEnd = shader->samplerExternalNames.end(); - while (nameIter != nameEnd) { - if (*nameIter == name) { - pData->setIndexFlags(index, ProgramData::INDEX_FLAG_SAMPLER_EXTERNAL); - break; - } - ++nameIter; + emugl::Mutex::AutoLock _lock(m_lock); + ProgramData* pData = m_programs.get(program); + if (!pData) { + return; + } + pData->setIndexInfo(index,base,size,type); + + if (type == GL_SAMPLER_2D) { + size_t n = pData->getNumShaders(); + for (size_t i = 0; i < n; i++) { + GLuint shaderId = pData->getShader(i); + ShaderData* shader = m_shaders.get(shaderId); + if (!shader) continue; +#if 0 // TODO(digit): Understand why samplerExternalNames is always empty? + ShaderData::StringList::iterator nameIter = + shader->samplerExternalNames.begin(); + ShaderData::StringList::iterator nameEnd = + shader->samplerExternalNames.end(); + while (nameIter != nameEnd) { + if (*nameIter == name) { + pData->setIndexFlags( + index, + ProgramData::INDEX_FLAG_SAMPLER_EXTERNAL); + break; } + ++nameIter; } +#endif } } } + GLenum GLSharedGroup::getProgramUniformType(GLuint program, GLint location) { - android::AutoMutex _lock(m_lock); - ProgramData* pData = m_programs.valueFor(program); - GLenum type=0; - if (pData) - { - type = pData->getTypeForLocation(location); - } - return type; + emugl::Mutex::AutoLock _lock(m_lock); + ProgramData* pData = m_programs.get(program); + return pData ? pData->getTypeForLocation(location) : 0; } bool GLSharedGroup::isProgram(GLuint program) { - android::AutoMutex _lock(m_lock); - ProgramData* pData = m_programs.valueFor(program); - return (pData!=NULL); + emugl::Mutex::AutoLock _lock(m_lock); + ProgramData* pData = m_programs.get(program); + return (pData != NULL); } void GLSharedGroup::setupLocationShiftWAR(GLuint program) { - android::AutoMutex _lock(m_lock); - ProgramData* pData = m_programs.valueFor(program); + emugl::Mutex::AutoLock _lock(m_lock); + ProgramData* pData = m_programs.get(program); if (pData) pData->setupLocationShiftWAR(); } -GLint GLSharedGroup::locationWARHostToApp(GLuint program, GLint hostLoc, GLint arrIndex) +GLint GLSharedGroup::locationWARHostToApp(GLuint program, + GLint hostLoc, + GLint arrIndex) { - android::AutoMutex _lock(m_lock); - ProgramData* pData = m_programs.valueFor(program); - if (pData) return pData->locationWARHostToApp(hostLoc, arrIndex); - else return hostLoc; + emugl::Mutex::AutoLock _lock(m_lock); + ProgramData* pData = m_programs.get(program); + return pData ? pData->locationWARHostToApp(hostLoc, arrIndex) : hostLoc; } GLint GLSharedGroup::locationWARAppToHost(GLuint program, GLint appLoc) { - android::AutoMutex _lock(m_lock); - ProgramData* pData = m_programs.valueFor(program); - if (pData) return pData->locationWARAppToHost(appLoc); - else return appLoc; + emugl::Mutex::AutoLock _lock(m_lock); + ProgramData* pData = m_programs.get(program); + return pData ? pData->locationWARAppToHost(appLoc) : appLoc; } bool GLSharedGroup::needUniformLocationWAR(GLuint program) { - android::AutoMutex _lock(m_lock); - ProgramData* pData = m_programs.valueFor(program); - if (pData) return pData->needUniformLocationWAR(); - return false; + emugl::Mutex::AutoLock _lock(m_lock); + ProgramData* pData = m_programs.get(program); + return pData ? pData->needUniformLocationWAR() : false; } -GLint GLSharedGroup::getNextSamplerUniform(GLuint program, GLint index, GLint* val, GLenum* target) const +GLint GLSharedGroup::getNextSamplerUniform(GLuint program, + GLint index, + GLint* val, + GLenum* target) const { - android::AutoMutex _lock(m_lock); - ProgramData* pData = m_programs.valueFor(program); + emugl::Mutex::AutoLock _lock(m_lock); + ProgramData* pData = m_programs.get(program); return pData ? pData->getNextSamplerUniform(index, val, target) : -1; } -bool GLSharedGroup::setSamplerUniform(GLuint program, GLint appLoc, GLint val, GLenum* target) +bool GLSharedGroup::setSamplerUniform(GLuint program, + GLint appLoc, + GLint val, + GLenum* target) { - android::AutoMutex _lock(m_lock); - ProgramData* pData = m_programs.valueFor(program); + emugl::Mutex::AutoLock _lock(m_lock); + ProgramData* pData = m_programs.get(program); return pData ? pData->setSamplerUniform(appLoc, val, target) : false; } bool GLSharedGroup::addShaderData(GLuint shader) { - android::AutoMutex _lock(m_lock); + emugl::Mutex::AutoLock _lock(m_lock); ShaderData* data = new ShaderData; - if (data) { - if (m_shaders.add(shader, data) < 0) { - delete data; - data = NULL; - } - data->refcount = 1; - } - return data != NULL; + data->refcount = 1; + m_shaders.set(shader, data); + return true; } ShaderData* GLSharedGroup::getShaderData(GLuint shader) { - android::AutoMutex _lock(m_lock); - return m_shaders.valueFor(shader); + emugl::Mutex::AutoLock _lock(m_lock); + ShaderData* data = m_shaders.get(shader); + if (data) { + data->refcount++; + } + return data; } void GLSharedGroup::unrefShaderData(GLuint shader) { - android::AutoMutex _lock(m_lock); - ssize_t idx = m_shaders.indexOfKey(shader); - if (idx >= 0) { - unrefShaderDataLocked(idx); - } + emugl::Mutex::AutoLock _lock(m_lock); + unrefShaderDataLocked(shader); } -void GLSharedGroup::refShaderDataLocked(ssize_t shaderIdx) +void GLSharedGroup::refShaderDataLocked(GLuint shader) { - assert(shaderIdx >= 0 && shaderIdx <= m_shaders.size()); - ShaderData* data = m_shaders.valueAt(shaderIdx); - data->refcount++; + ShaderData* data = m_shaders.get(shader); + if (data) { + data->refcount++; + } } -void GLSharedGroup::unrefShaderDataLocked(ssize_t shaderIdx) +void GLSharedGroup::unrefShaderDataLocked(GLuint shader) { - assert(shaderIdx >= 0 && shaderIdx <= m_shaders.size()); - ShaderData* data = m_shaders.valueAt(shaderIdx); - if (--data->refcount == 0) { - delete data; - m_shaders.removeItemsAt(shaderIdx); + ShaderData* data = m_shaders.get(shader); + if (data && --data->refcount == 0) { + m_shaders.remove(shader); } } diff --git a/emulator/opengl/shared/OpenglCodecCommon/GLSharedGroup.h b/emulator/opengl/shared/OpenglCodecCommon/GLSharedGroup.h index 61b8f00..f111f99 100644 --- a/emulator/opengl/shared/OpenglCodecCommon/GLSharedGroup.h +++ b/emulator/opengl/shared/OpenglCodecCommon/GLSharedGroup.h @@ -16,6 +16,11 @@ #ifndef _GL_SHARED_GROUP_H_ #define _GL_SHARED_GROUP_H_ +#include "emugl/common/id_to_object_map.h" +#include "emugl/common/mutex.h" +#include "emugl/common/pod_vector.h" +#include "emugl/common/smart_ptr.h" + #define GL_API #ifndef ANDROID #define GL_APIENTRY @@ -30,18 +35,13 @@ #include <stdio.h> #include <stdlib.h> #include "ErrorLog.h" -#include <utils/KeyedVector.h> -#include <utils/List.h> -#include <utils/String8.h> -#include <utils/threads.h> #include "FixedBuffer.h" -#include "SmartPtr.h" struct BufferData { BufferData(); BufferData(GLsizeiptr size, void * data); GLsizeiptr m_size; - FixedBuffer m_fixedBuffer; + FixedBuffer m_fixedBuffer; }; class ProgramData { @@ -61,7 +61,7 @@ private: bool m_initialized; bool m_locShiftWAR; - android::Vector<GLuint> m_shaders; + emugl::PodVector<GLuint> m_shaders; public: enum { @@ -92,20 +92,22 @@ public: }; struct ShaderData { +#if 0 // TODO(digit): Undertand why this is never used? typedef android::List<android::String8> StringList; StringList samplerExternalNames; +#endif int refcount; }; class GLSharedGroup { private: - android::DefaultKeyedVector<GLuint, BufferData*> m_buffers; - android::DefaultKeyedVector<GLuint, ProgramData*> m_programs; - android::DefaultKeyedVector<GLuint, ShaderData*> m_shaders; - mutable android::Mutex m_lock; + emugl::IdToObjectMap<BufferData> m_buffers; + emugl::IdToObjectMap<ProgramData> m_programs; + emugl::IdToObjectMap<ShaderData> m_shaders; + mutable emugl::Mutex m_lock; - void refShaderDataLocked(ssize_t shaderIdx); - void unrefShaderDataLocked(ssize_t shaderIdx); + void refShaderDataLocked(GLuint shader); + void unrefShaderDataLocked(GLuint shader); public: GLSharedGroup(); @@ -138,6 +140,6 @@ public: void unrefShaderData(GLuint shader); }; -typedef SmartPtr<GLSharedGroup> GLSharedGroupPtr; +typedef emugl::SmartPtr<GLSharedGroup> GLSharedGroupPtr; #endif //_GL_SHARED_GROUP_H_ diff --git a/emulator/opengl/shared/OpenglCodecCommon/SmartPtr.h b/emulator/opengl/shared/OpenglCodecCommon/SmartPtr.h deleted file mode 100644 index 4bdfbe4..0000000 --- a/emulator/opengl/shared/OpenglCodecCommon/SmartPtr.h +++ /dev/null @@ -1,167 +0,0 @@ -/* -* Copyright (C) 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 __SMART_PTR_H -#define __SMART_PTR_H - -#include <cutils/threads.h> -#include <cutils/atomic.h> - -template <class T, bool threadSafe = false> -class SmartPtr -{ -public: - explicit SmartPtr(T* ptr = (T*)NULL) { - if (threadSafe) { - m_lock = new mutex_t; - mutex_init(m_lock); - } - else m_lock = NULL; - - m_ptr = ptr; - if (ptr) - m_pRefCount = new int32_t(1); - else - m_pRefCount = NULL; - } - - SmartPtr<T,threadSafe>(const SmartPtr<T,false>& rhs) { - if (threadSafe) { - m_lock = new mutex_t; - mutex_init(m_lock); - } - else m_lock = NULL; - - m_pRefCount = rhs.m_pRefCount; - m_ptr = rhs.m_ptr; - use(); - } - - SmartPtr<T,threadSafe>(SmartPtr<T,true>& rhs) { - if (threadSafe) { - m_lock = new mutex_t; - mutex_init(m_lock); - } - else m_lock = NULL; - - if (rhs.m_lock) mutex_lock(rhs.m_lock); - m_pRefCount = rhs.m_pRefCount; - m_ptr = rhs.m_ptr; - use(); - if (rhs.m_lock) mutex_unlock(rhs.m_lock); - } - - ~SmartPtr() { - if (m_lock) mutex_lock(m_lock); - release(); - if (m_lock) - { - mutex_unlock(m_lock); - mutex_destroy(m_lock); - delete m_lock; - } - } - - T* Ptr() const { - return m_ptr; - } - - const T* constPtr() const - { - return m_ptr; - } - - T* operator->() const { - return m_ptr; - } - - T& operator*() const { - return *m_ptr; - } - - operator void*() const { - return (void *)m_ptr; - } - - // This gives STL lists something to compare. - bool operator <(const SmartPtr<T>& t1) const { - return m_ptr < t1.m_ptr; - } - - SmartPtr<T,threadSafe>& operator=(const SmartPtr<T,false>& rhs) - { - if (m_ptr == rhs.m_ptr) - return *this; - - if (m_lock) mutex_lock(m_lock); - release(); - m_pRefCount = rhs.m_pRefCount; - m_ptr = rhs.m_ptr; - use(); - if (m_lock) mutex_unlock(m_lock); - - return *this; - } - - SmartPtr<T,threadSafe>& operator=(SmartPtr<T,true>& rhs) - { - if (m_ptr == rhs.m_ptr) - return *this; - - if (m_lock) mutex_lock(m_lock); - release(); - if (rhs.m_lock) mutex_lock(rhs.m_lock); - m_pRefCount = rhs.m_pRefCount; - m_ptr = rhs.m_ptr; - use(); - if (rhs.m_lock) mutex_unlock(rhs.m_lock); - if (m_lock) mutex_unlock(m_lock); - - return *this; - } - -private: - int32_t *m_pRefCount; - mutex_t *m_lock; - T* m_ptr; - - // Increment the reference count on this pointer by 1. - int use() { - if (!m_pRefCount) return 0; - return android_atomic_inc(m_pRefCount) + 1; - } - - // Decrement the reference count on the pointer by 1. - // If the reference count goes to (or below) 0, the pointer is deleted. - int release() { - if (!m_pRefCount) return 0; - - int iVal = android_atomic_dec(m_pRefCount); - if (iVal > 1) - return iVal - 1; - - delete m_pRefCount; - m_pRefCount = NULL; - - if (m_ptr) { - delete m_ptr; - m_ptr = NULL; - } - return 0; - } - -}; - -#endif // of __SMART_PTR_H diff --git a/emulator/opengl/shared/OpenglCodecCommon/SocketStream.cpp b/emulator/opengl/shared/OpenglCodecCommon/SocketStream.cpp index f7a2314..726d359 100644 --- a/emulator/opengl/shared/OpenglCodecCommon/SocketStream.cpp +++ b/emulator/opengl/shared/OpenglCodecCommon/SocketStream.cpp @@ -14,7 +14,6 @@ * limitations under the License. */ #include "SocketStream.h" -#include <cutils/sockets.h> #include <errno.h> #include <stdio.h> #include <stdlib.h> @@ -53,6 +52,7 @@ SocketStream::~SocketStream() #else ::close(m_sock); #endif + m_sock = -1; } if (m_buf != NULL) { free(m_buf); @@ -112,7 +112,6 @@ int SocketStream::writeFully(const void* buffer, size_t size) const unsigned char *SocketStream::readFully(void *buf, size_t len) { - const unsigned char* ret = NULL; if (!valid()) return NULL; if (!buf) { return NULL; // do not allow NULL buf in that implementation @@ -140,7 +139,7 @@ const unsigned char *SocketStream::read( void *buf, size_t *inout_len) int n; do { - n = recv(buf, *inout_len); + n = this->recv(buf, *inout_len); } while( n < 0 && errno == EINTR ); if (n > 0) { diff --git a/emulator/opengl/shared/OpenglCodecCommon/TcpStream.cpp b/emulator/opengl/shared/OpenglCodecCommon/TcpStream.cpp index 8a6e56e..ba355ab 100644 --- a/emulator/opengl/shared/OpenglCodecCommon/TcpStream.cpp +++ b/emulator/opengl/shared/OpenglCodecCommon/TcpStream.cpp @@ -14,7 +14,8 @@ * limitations under the License. */ #include "TcpStream.h" -#include <cutils/sockets.h> +#include "emugl/common/sockets.h" + #include <errno.h> #include <stdio.h> #include <stdlib.h> @@ -30,77 +31,48 @@ #define LISTEN_BACKLOG 4 -TcpStream::TcpStream(size_t bufSize) : - SocketStream(bufSize) -{ -} +TcpStream::TcpStream(size_t bufSize) : SocketStream(bufSize) {} TcpStream::TcpStream(int sock, size_t bufSize) : - SocketStream(sock, bufSize) -{ + SocketStream(sock, bufSize) { // disable Nagle algorithm to improve bandwidth of small // packets which are quite common in our implementation. -#ifdef _WIN32 - DWORD flag; -#else - int flag; -#endif - flag = 1; - setsockopt( sock, IPPROTO_TCP, TCP_NODELAY, (const char*)&flag, sizeof(flag) ); + emugl::socketTcpDisableNagle(sock); } -int TcpStream::listen(char addrstr[MAX_ADDRSTR_LEN]) -{ - m_sock = socket_loopback_server(0, SOCK_STREAM); +int TcpStream::listen(char addrstr[MAX_ADDRSTR_LEN]) { + m_sock = emugl::socketTcpLoopbackServer(0, SOCK_STREAM); if (!valid()) return int(ERR_INVALID_SOCKET); - /* get the actual port number assigned by the system */ - struct sockaddr_in addr; - socklen_t addrLen = sizeof(addr); - memset(&addr, 0, sizeof(addr)); - if (getsockname(m_sock, (struct sockaddr*)&addr, &addrLen) < 0) { - close(m_sock); + int port = emugl::socketGetPort(m_sock); + if (port < 0) { + ::close(m_sock); return int(ERR_INVALID_SOCKET); } - snprintf(addrstr, MAX_ADDRSTR_LEN - 1, "%hu", ntohs(addr.sin_port)); + + snprintf(addrstr, MAX_ADDRSTR_LEN - 1, "%hu", port); addrstr[MAX_ADDRSTR_LEN-1] = '\0'; return 0; } -SocketStream * TcpStream::accept() -{ - int clientSock = -1; - - while (true) { - struct sockaddr_in addr; - socklen_t len = sizeof(addr); - clientSock = ::accept(m_sock, (sockaddr *)&addr, &len); - - if (clientSock < 0 && errno == EINTR) { - continue; - } - break; - } +SocketStream * TcpStream::accept() { + int clientSock = emugl::socketAccept(m_sock); + if (clientSock < 0) + return NULL; - TcpStream *clientStream = NULL; - - if (clientSock >= 0) { - clientStream = new TcpStream(clientSock, m_bufsize); - } - return clientStream; + return new TcpStream(clientSock, m_bufsize); } -int TcpStream::connect(const char* addr) -{ +int TcpStream::connect(const char* addr) { int port = atoi(addr); - return connect("127.0.0.1",port); + m_sock = emugl::socketTcpLoopbackClient(port, SOCK_STREAM); + return valid() ? 0 : -1; } int TcpStream::connect(const char* hostname, unsigned short port) { - m_sock = socket_network_client(hostname, port, SOCK_STREAM); - if (!valid()) return -1; - return 0; + m_sock = emugl::socketTcpClient(hostname, port, SOCK_STREAM); + return valid() ? 0 : -1; } diff --git a/emulator/opengl/shared/OpenglCodecCommon/UnixStream.cpp b/emulator/opengl/shared/OpenglCodecCommon/UnixStream.cpp index b2eef6d..7b2f67d 100644 --- a/emulator/opengl/shared/OpenglCodecCommon/UnixStream.cpp +++ b/emulator/opengl/shared/OpenglCodecCommon/UnixStream.cpp @@ -14,7 +14,9 @@ * limitations under the License. */ #include "UnixStream.h" -#include <cutils/sockets.h> + +#include "emugl/common/sockets.h" + #include <errno.h> #include <stdio.h> #include <stdlib.h> @@ -92,7 +94,7 @@ int UnixStream::listen(char addrstr[MAX_ADDRSTR_LEN]) return -1; } - m_sock = socket_local_server(addrstr, ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM); + m_sock = emugl::socketLocalServer(addrstr, SOCK_STREAM); if (!valid()) return int(ERR_INVALID_SOCKET); return 0; @@ -123,7 +125,7 @@ SocketStream * UnixStream::accept() int UnixStream::connect(const char* addr) { - m_sock = socket_local_client(addr, ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM); + m_sock = emugl::socketLocalClient(addr, SOCK_STREAM); if (!valid()) return -1; return 0; diff --git a/emulator/opengl/shared/OpenglCodecCommon/Win32PipeStream.cpp b/emulator/opengl/shared/OpenglCodecCommon/Win32PipeStream.cpp index 76907a0..dcfb0c0 100644 --- a/emulator/opengl/shared/OpenglCodecCommon/Win32PipeStream.cpp +++ b/emulator/opengl/shared/OpenglCodecCommon/Win32PipeStream.cpp @@ -193,8 +193,6 @@ int Win32PipeStream::commitBuffer(size_t size) const unsigned char *Win32PipeStream::readFully(void *buf, size_t len) { - const unsigned char* ret = NULL; - if (m_pipe == INVALID_HANDLE_VALUE) return NULL; |