summaryrefslogtreecommitdiff
path: root/tools/emulator/opengl/shared/OpenglCodecCommon
diff options
context:
space:
mode:
Diffstat (limited to 'tools/emulator/opengl/shared/OpenglCodecCommon')
-rw-r--r--tools/emulator/opengl/shared/OpenglCodecCommon/Android.mk38
-rw-r--r--tools/emulator/opengl/shared/OpenglCodecCommon/ErrorLog.h29
-rw-r--r--tools/emulator/opengl/shared/OpenglCodecCommon/FixedBuffer.h52
-rw-r--r--tools/emulator/opengl/shared/OpenglCodecCommon/GLClientState.cpp231
-rw-r--r--tools/emulator/opengl/shared/OpenglCodecCommon/GLClientState.h109
-rw-r--r--tools/emulator/opengl/shared/OpenglCodecCommon/GLDecoderContextData.h55
-rw-r--r--tools/emulator/opengl/shared/OpenglCodecCommon/IOStream.h90
-rw-r--r--tools/emulator/opengl/shared/OpenglCodecCommon/Makefile13
-rw-r--r--tools/emulator/opengl/shared/OpenglCodecCommon/TcpStream.cpp231
-rw-r--r--tools/emulator/opengl/shared/OpenglCodecCommon/TcpStream.h50
-rw-r--r--tools/emulator/opengl/shared/OpenglCodecCommon/TimeUtils.cpp55
-rw-r--r--tools/emulator/opengl/shared/OpenglCodecCommon/TimeUtils.h21
-rw-r--r--tools/emulator/opengl/shared/OpenglCodecCommon/codec_defs.h21
-rw-r--r--tools/emulator/opengl/shared/OpenglCodecCommon/glUtils.cpp147
-rw-r--r--tools/emulator/opengl/shared/OpenglCodecCommon/glUtils.h56
15 files changed, 1198 insertions, 0 deletions
diff --git a/tools/emulator/opengl/shared/OpenglCodecCommon/Android.mk b/tools/emulator/opengl/shared/OpenglCodecCommon/Android.mk
new file mode 100644
index 000000000..e56927035
--- /dev/null
+++ b/tools/emulator/opengl/shared/OpenglCodecCommon/Android.mk
@@ -0,0 +1,38 @@
+
+LOCAL_PATH := $(call my-dir)
+emulatorOpengl := $(LOCAL_PATH)/../..
+
+### OpenglCodecCommon ##############################################
+
+include $(CLEAR_VARS)
+
+OpenglCodecCommon := \
+ GLClientState.cpp \
+ glUtils.cpp \
+ TcpStream.cpp \
+ TimeUtils.cpp
+
+LOCAL_SRC_FILES := $(OpenglCodecCommon)
+
+LOCAL_C_INCLUDES += $(emulatorOpengl)/host/include/libOpenGLRender
+
+LOCAL_CFLAGS += -DLOG_TAG=\"eglCodecCommon\"
+LOCAL_MODULE_TAGS := debug
+LOCAL_MODULE := libOpenglCodecCommon
+
+include $(BUILD_STATIC_LIBRARY)
+
+### OpenglCodecCommon host ##############################################
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := $(OpenglCodecCommon)
+
+LOCAL_C_INCLUDES += $(emulatorOpengl)/host/include/libOpenGLRender
+
+LOCAL_MODULE_TAGS := debug
+LOCAL_MODULE := libOpenglCodecCommon
+LOCAL_PRELINK_MODULE := false
+
+# XXX - enable the next line for host debugging - JR
+# LOCAL_CFLAGS := -O0 -g
+include $(BUILD_HOST_STATIC_LIBRARY)
diff --git a/tools/emulator/opengl/shared/OpenglCodecCommon/ErrorLog.h b/tools/emulator/opengl/shared/OpenglCodecCommon/ErrorLog.h
new file mode 100644
index 000000000..8502bb0e5
--- /dev/null
+++ b/tools/emulator/opengl/shared/OpenglCodecCommon/ErrorLog.h
@@ -0,0 +1,29 @@
+/*
+* 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 _ERROR_LOG_H_
+#define _ERROR_LOG_H_
+
+#if (HAVE_ANDROID_OS == 1)
+# include <cutils/log.h>
+# define ERR(...) LOGE(__VA_ARGS__)
+# define DBG(...) LOGD(__VA_ARGS__)
+#else
+# include <stdio.h>
+# define ERR(...) fprintf(stderr, __VA_ARGS__)
+# define DBG(...) fprintf(stderr, __VA_ARGS__)
+#endif
+
+#endif
diff --git a/tools/emulator/opengl/shared/OpenglCodecCommon/FixedBuffer.h b/tools/emulator/opengl/shared/OpenglCodecCommon/FixedBuffer.h
new file mode 100644
index 000000000..78fa244fd
--- /dev/null
+++ b/tools/emulator/opengl/shared/OpenglCodecCommon/FixedBuffer.h
@@ -0,0 +1,52 @@
+/*
+* 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 _FIXED_BUFFER_H
+#define _FIXED_BUFFER_H
+
+class FixedBuffer {
+public:
+ FixedBuffer(size_t initialSize = 0) {
+ m_buffer = NULL;
+ m_bufferLen = 0;
+ alloc(m_bufferLen);
+ }
+
+ ~FixedBuffer() {
+ if (m_buffer != NULL) {
+ delete m_buffer;
+ m_bufferLen = 0;
+ }
+ }
+
+ void * alloc(size_t size) {
+ if (m_bufferLen >= size) return (void *)(m_buffer);
+
+ if (m_buffer != NULL) delete m_buffer;
+
+ m_bufferLen = size;
+ m_buffer = new unsigned char[m_bufferLen];
+ if (m_buffer == NULL) m_bufferLen = 0;
+
+ return m_buffer;
+ }
+ void *ptr() { return m_buffer; }
+ size_t len() { return m_bufferLen; }
+private:
+ unsigned char *m_buffer;
+ size_t m_bufferLen;
+};
+
+#endif
diff --git a/tools/emulator/opengl/shared/OpenglCodecCommon/GLClientState.cpp b/tools/emulator/opengl/shared/OpenglCodecCommon/GLClientState.cpp
new file mode 100644
index 000000000..7fc506821
--- /dev/null
+++ b/tools/emulator/opengl/shared/OpenglCodecCommon/GLClientState.cpp
@@ -0,0 +1,231 @@
+/*
+* 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.
+*/
+#include "GLClientState.h"
+#include "ErrorLog.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include "glUtils.h"
+#include <cutils/log.h>
+
+GLClientState::GLClientState(int nLocations)
+{
+ if (nLocations < LAST_LOCATION) {
+ nLocations = LAST_LOCATION;
+ }
+ m_nLocations = nLocations;
+ m_states = new VertexAttribState[m_nLocations];
+ for (int i = 0; i < m_nLocations; i++) {
+ m_states[i].enabled = 0;
+ m_states[i].enableDirty = false;
+ }
+ m_currentArrayVbo = 0;
+ m_currentIndexVbo = 0;
+ // init gl constans;
+ m_states[VERTEX_LOCATION].glConst = GL_VERTEX_ARRAY;
+ m_states[NORMAL_LOCATION].glConst = GL_NORMAL_ARRAY;
+ m_states[COLOR_LOCATION].glConst = GL_COLOR_ARRAY;
+ m_states[POINTSIZE_LOCATION].glConst = GL_POINT_SIZE_ARRAY_OES;
+ m_states[TEXCOORD0_LOCATION].glConst = GL_TEXTURE_COORD_ARRAY;
+ m_states[TEXCOORD1_LOCATION].glConst = GL_TEXTURE_COORD_ARRAY;
+ m_states[TEXCOORD2_LOCATION].glConst = GL_TEXTURE_COORD_ARRAY;
+ m_states[TEXCOORD3_LOCATION].glConst = GL_TEXTURE_COORD_ARRAY;
+ m_states[TEXCOORD4_LOCATION].glConst = GL_TEXTURE_COORD_ARRAY;
+ m_states[TEXCOORD5_LOCATION].glConst = GL_TEXTURE_COORD_ARRAY;
+ m_states[TEXCOORD6_LOCATION].glConst = GL_TEXTURE_COORD_ARRAY;
+ m_states[TEXCOORD7_LOCATION].glConst = GL_TEXTURE_COORD_ARRAY;
+ m_activeTexture = 0;
+
+ m_pixelStore.unpack_alignment = 4;
+ m_pixelStore.pack_alignment = 4;
+}
+
+GLClientState::~GLClientState()
+{
+ delete m_states;
+}
+
+void GLClientState::enable(int location, int state)
+{
+ if (!validLocation(location)) {
+ return;
+ }
+
+ m_states[location].enableDirty |= (state != m_states[location].enabled);
+ m_states[location].enabled = state;
+}
+
+void GLClientState::setState(int location, int size, GLenum type, GLsizei stride, void *data)
+{
+ if (!validLocation(location)) {
+ return;
+ }
+ m_states[location].size = size;
+ m_states[location].type = type;
+ m_states[location].stride = stride;
+ m_states[location].data = data;
+ m_states[location].bufferObject = m_currentArrayVbo;
+ m_states[location].elementSize = glSizeof(type) * size;
+}
+
+void GLClientState::setBufferObject(int location, GLuint id)
+{
+ if (!validLocation(location)) {
+ return;
+ }
+
+ m_states[location].bufferObject = id;
+}
+
+const GLClientState::VertexAttribState * GLClientState::getState(int location)
+{
+ if (!validLocation(location)) {
+ return NULL;
+ }
+ return & m_states[location];
+}
+
+const GLClientState::VertexAttribState * GLClientState::getStateAndEnableDirty(int location, bool *enableChanged)
+{
+ if (!validLocation(location)) {
+ return NULL;
+ }
+
+ if (enableChanged) {
+ *enableChanged = m_states[location].enableDirty;
+ }
+
+ m_states[location].enableDirty = false;
+ return & m_states[location];
+}
+
+int GLClientState::getLocation(GLenum loc)
+{
+ int retval;
+
+ switch(loc) {
+ case GL_VERTEX_ARRAY:
+ retval = int(VERTEX_LOCATION);
+ break;
+ case GL_NORMAL_ARRAY:
+ retval = int(NORMAL_LOCATION);
+ break;
+ case GL_COLOR_ARRAY:
+ retval = int(COLOR_LOCATION);
+ break;
+ case GL_POINT_SIZE_ARRAY_OES:
+ retval = int(POINTSIZE_LOCATION);
+ break;
+ case GL_TEXTURE_COORD_ARRAY:
+ retval = int (TEXCOORD0_LOCATION + m_activeTexture);
+ break;
+ default:
+ retval = loc;
+ }
+ return retval;
+}
+
+int GLClientState::setPixelStore(GLenum param, GLint value)
+{
+ int retval = 0;
+ switch(param) {
+ case GL_UNPACK_ALIGNMENT:
+ if (value == 1 || value == 2 || value == 4 || value == 8) {
+ m_pixelStore.unpack_alignment = value;
+ } else {
+ retval = GL_INVALID_VALUE;
+ }
+ break;
+ case GL_PACK_ALIGNMENT:
+ if (value == 1 || value == 2 || value == 4 || value == 8) {
+ m_pixelStore.pack_alignment = value;
+ } else {
+ retval = GL_INVALID_VALUE;
+ }
+ break;
+ default:
+ retval = GL_INVALID_ENUM;
+ }
+ return retval;
+}
+
+
+size_t GLClientState::pixelDataSize(GLsizei width, GLsizei height, GLenum format, GLenum type, int pack)
+{
+
+ int components = 0;
+ int componentsize = 0;
+ int pixelsize = 0;
+ switch(type) {
+ case GL_UNSIGNED_BYTE:
+ componentsize = 1;
+ break;
+ case GL_UNSIGNED_SHORT_5_6_5:
+ case GL_UNSIGNED_SHORT_4_4_4_4:
+ case GL_UNSIGNED_SHORT_5_5_5_1:
+ pixelsize = 2;
+ break;
+ default:
+ ERR("pixelDataSize: unknown pixel type - assuming pixel data 0\n");
+ componentsize = 0;
+ }
+
+ if (pixelsize == 0) {
+ switch(format) {
+#if 0
+ case GL_RED:
+ case GL_GREEN:
+ case GL_BLUE:
+#endif
+ case GL_ALPHA:
+ case GL_LUMINANCE:
+ components = 1;
+ break;
+ case GL_LUMINANCE_ALPHA:
+ components = 2;
+ break;
+ case GL_RGB:
+#if 0
+ case GL_BGR:
+#endif
+ components = 3;
+ break;
+ case GL_RGBA:
+#if 0
+ case GL_BGRA:
+#endif
+ components = 4;
+ break;
+ default:
+ ERR("pixelDataSize: unknown pixel format...\n");
+ components = 0;
+ }
+ pixelsize = components * componentsize;
+ }
+
+ int alignment = pack ? m_pixelStore.pack_alignment : m_pixelStore.unpack_alignment;
+
+ if (pixelsize == 0 ) {
+ ERR("unknown pixel size: width: %d height: %d format: %d type: %d pack: %d align: %d\n",
+ width, height, format, type, pack, alignment);
+ }
+ size_t linesize = pixelsize * width;
+ size_t aligned_linesize = int(linesize / alignment) * alignment;
+ if (aligned_linesize < linesize) {
+ aligned_linesize += alignment;
+ }
+ return aligned_linesize * height;
+}
+
diff --git a/tools/emulator/opengl/shared/OpenglCodecCommon/GLClientState.h b/tools/emulator/opengl/shared/OpenglCodecCommon/GLClientState.h
new file mode 100644
index 000000000..be625d555
--- /dev/null
+++ b/tools/emulator/opengl/shared/OpenglCodecCommon/GLClientState.h
@@ -0,0 +1,109 @@
+/*
+* 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 _GL_CLIENT_STATE_H_
+#define _GL_CLIENT_STATE_H_
+
+#define GL_API
+#ifndef ANDROID
+#define GL_APIENTRY
+#define GL_APIENTRYP
+#endif
+
+#include <GLES/gl.h>
+#include <GLES/glext.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+class GLClientState {
+public:
+ typedef enum {
+ VERTEX_LOCATION = 0,
+ NORMAL_LOCATION = 1,
+ COLOR_LOCATION = 2,
+ POINTSIZE_LOCATION = 3,
+ TEXCOORD0_LOCATION = 4,
+ TEXCOORD1_LOCATION = 5,
+ TEXCOORD2_LOCATION = 6,
+ TEXCOORD3_LOCATION = 7,
+ TEXCOORD4_LOCATION = 8,
+ TEXCOORD5_LOCATION = 9,
+ TEXCOORD6_LOCATION = 10,
+ TEXCOORD7_LOCATION = 11,
+ LAST_LOCATION = 12
+ } StateLocation;
+
+ typedef struct {
+ GLint enabled;
+ GLint size;
+ GLenum type;
+ GLsizei stride;
+ void *data;
+ GLuint bufferObject;
+ GLenum glConst;
+ unsigned int elementSize;
+ bool enableDirty; // true if any enable state has changed since last draw
+ } VertexAttribState;
+
+ typedef struct {
+ int unpack_alignment;
+ int pack_alignment;
+ } PixelStoreState;
+
+public:
+ GLClientState(int nLocations = 32);
+ ~GLClientState();
+ const PixelStoreState *pixelStoreState() { return &m_pixelStore; }
+ int setPixelStore(GLenum param, GLint value);
+ GLuint currentArrayVbo() { return m_currentArrayVbo; }
+ GLuint currentIndexVbo() { return m_currentIndexVbo; }
+ void enable(int location, int state);
+ void setState(int location, int size, GLenum type, GLsizei stride, void *data);
+ void setBufferObject(int location, GLuint id);
+ const VertexAttribState *getState(int location);
+ const VertexAttribState *getStateAndEnableDirty(int location, bool *enableChanged);
+ int getLocation(GLenum loc);
+ void setActiveTexture(int texUnit) {m_activeTexture = texUnit; };
+ int getActiveTexture() const { return m_activeTexture; }
+ int bindBuffer(GLenum target, GLuint id)
+ {
+ int err = 0;
+ switch(target) {
+ case GL_ARRAY_BUFFER:
+ m_currentArrayVbo = id;
+ break;
+ case GL_ELEMENT_ARRAY_BUFFER:
+ m_currentIndexVbo = id;
+ break;
+ default:
+ err = -1;
+ }
+ return err;
+ }
+ size_t pixelDataSize(GLsizei width, GLsizei height, GLenum format, GLenum type, int pack);
+
+private:
+ PixelStoreState m_pixelStore;
+ VertexAttribState *m_states;
+ int m_nLocations;
+ GLuint m_currentArrayVbo;
+ GLuint m_currentIndexVbo;
+ int m_activeTexture;
+
+
+ bool validLocation(int location) { return (location >= 0 && location < m_nLocations); }
+
+};
+#endif
diff --git a/tools/emulator/opengl/shared/OpenglCodecCommon/GLDecoderContextData.h b/tools/emulator/opengl/shared/OpenglCodecCommon/GLDecoderContextData.h
new file mode 100644
index 000000000..b0874475a
--- /dev/null
+++ b/tools/emulator/opengl/shared/OpenglCodecCommon/GLDecoderContextData.h
@@ -0,0 +1,55 @@
+/*
+* 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 _GL_DECODER_CONTEXT_DATA_H_
+#define _GL_DECODER_CONTEXT_DATA_H_
+
+#include <assert.h>
+#include <string.h>
+#include "FixedBuffer.h"
+
+class GLDecoderContextData {
+public:
+ typedef enum {
+ VERTEX_LOCATION = 0,
+ NORMAL_LOCATION = 1,
+ COLOR_LOCATION = 2,
+ POINTSIZE_LOCATION = 3,
+ TEXCOORD0_LOCATION = 4,
+ TEXCOORD1_LOCATION = 5,
+ TEXCOORD2_LOCATION = 6,
+ TEXCOORD3_LOCATION = 7,
+ TEXCOORD4_LOCATION = 8,
+ TEXCOORD5_LOCATION = 9,
+ TEXCOORD6_LOCATION = 10,
+ TEXCOORD7_LOCATION = 11,
+ LAST_LOCATION = 12
+ } PointerDataLocation;
+
+ void storePointerData(PointerDataLocation loc, void *data, size_t len) {
+ assert(loc < LAST_LOCATION);
+
+ m_pointerData[loc].alloc(len);
+ memcpy(m_pointerData[loc].ptr(), data, len);
+ }
+ void *pointerData(PointerDataLocation loc) {
+ assert(loc < LAST_LOCATION);
+ return m_pointerData[loc].ptr();
+ }
+private:
+ FixedBuffer m_pointerData[LAST_LOCATION];
+};
+
+#endif
diff --git a/tools/emulator/opengl/shared/OpenglCodecCommon/IOStream.h b/tools/emulator/opengl/shared/OpenglCodecCommon/IOStream.h
new file mode 100644
index 000000000..7cfccc340
--- /dev/null
+++ b/tools/emulator/opengl/shared/OpenglCodecCommon/IOStream.h
@@ -0,0 +1,90 @@
+/*
+* 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 __IO_STREAM_H__
+#define __IO_STREAM_H__
+
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "ErrorLog.h"
+
+class IOStream {
+public:
+
+ IOStream(size_t bufSize) {
+ m_buf = NULL;
+ m_bufsize = bufSize;
+ m_free = 0;
+ }
+
+ virtual void *allocBuffer(size_t minSize) = 0;
+ virtual int commitBuffer(size_t size) = 0;
+ virtual const unsigned char *readFully( void *buf, size_t len) = 0;
+
+ virtual ~IOStream() {
+
+ // NOTE: m_buf is 'owned' by the child class thus we expect it to be released by it
+ }
+
+ unsigned char *alloc(size_t len) {
+
+ if (m_buf && len > m_free) {
+ if (flush() < 0) {
+ ERR("Failed to flush in alloc\n");
+ return NULL; // we failed to flush so something is wrong
+ }
+ }
+
+ if (!m_buf || len > m_bufsize) {
+ int allocLen = m_bufsize < len ? len : m_bufsize;
+ m_buf = (unsigned char *)allocBuffer(allocLen);
+ if (!m_buf) {
+ ERR("Alloc (%u bytes) failed\n", allocLen);
+ return NULL;
+ }
+ m_bufsize = m_free = allocLen;
+ }
+
+ unsigned char *ptr;
+
+ ptr = m_buf + (m_bufsize - m_free);
+ m_free -= len;
+
+ return ptr;
+ }
+
+ int flush() {
+
+ if (!m_buf || m_free == m_bufsize) return 0;
+
+ int stat = commitBuffer(m_bufsize - m_free);
+ m_buf = NULL;
+ m_free = 0;
+ return stat;
+ }
+
+ const unsigned char *readback(void *buf, size_t len) {
+ flush();
+ return readFully(buf, len);
+ }
+
+private:
+ unsigned char *m_buf;
+ size_t m_bufsize;
+ size_t m_free;
+};
+
+#endif
diff --git a/tools/emulator/opengl/shared/OpenglCodecCommon/Makefile b/tools/emulator/opengl/shared/OpenglCodecCommon/Makefile
new file mode 100644
index 000000000..e8bf43162
--- /dev/null
+++ b/tools/emulator/opengl/shared/OpenglCodecCommon/Makefile
@@ -0,0 +1,13 @@
+
+ROOT=../..
+
+include $(ROOT)/make/commondefs
+
+CXXFILES = TcpStream.cpp GLClientState.cpp glUtils.cpp
+CXXINCS += -I$(ROOT)/libs/GLESv1 -I$(ROOT)/include
+
+LIBRARY_NAME = libcodecCommon.a
+
+include $(COMMONRULES)
+
+
diff --git a/tools/emulator/opengl/shared/OpenglCodecCommon/TcpStream.cpp b/tools/emulator/opengl/shared/OpenglCodecCommon/TcpStream.cpp
new file mode 100644
index 000000000..86202d0fb
--- /dev/null
+++ b/tools/emulator/opengl/shared/OpenglCodecCommon/TcpStream.cpp
@@ -0,0 +1,231 @@
+/*
+* 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.
+*/
+#include "TcpStream.h"
+
+#ifdef ANDROID
+#include <netinet/in.h>
+#endif
+
+#include <errno.h>
+#include <netdb.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+
+TcpStream::TcpStream(size_t bufSize) : IOStream(bufSize)
+{
+ m_sock = socket(AF_INET, SOCK_STREAM, 0);
+ m_bufsize = bufSize;
+ m_buf = NULL;
+}
+
+TcpStream::TcpStream(int sock, size_t bufSize) :
+ IOStream(bufSize),
+ m_sock(sock),
+ m_bufsize(bufSize),
+ m_buf(NULL)
+{
+
+}
+
+TcpStream::~TcpStream()
+{
+ if (m_sock >= 0) {
+ ::close(m_sock);
+ }
+ if (m_buf != NULL) {
+ free(m_buf);
+ }
+}
+
+
+int TcpStream::listen(unsigned short port, bool localhost_only, bool reuse_address)
+{
+ if (!valid()) return int(ERR_INVALID_SOCKET);
+
+ // NOTE: This is a potential security issue. However, since we accept connection
+ // from local host only, this should be reasonably OK.
+
+ if (reuse_address) {
+ int one = 1;
+ if (setsockopt(m_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0) {
+ perror("setsockopt resuseaddr");
+ }
+ }
+
+ struct sockaddr_in addr;
+ memset(&addr, 0, sizeof(addr));
+
+ addr.sin_family = AF_INET;
+ addr.sin_port = htons(port);
+ if (localhost_only) {
+ addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+ } else {
+ addr.sin_addr.s_addr = INADDR_ANY;
+ }
+
+ if (::bind(m_sock, (const sockaddr *) &addr, sizeof(addr)) < 0) {
+ perror("bind");
+ return -1;
+ }
+ if (::listen(m_sock, 5) < 0) {
+ perror("listen");
+ return -1;
+ }
+ return 0;
+}
+
+TcpStream * 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;
+ }
+
+ TcpStream *clientStream = NULL;
+
+ if (clientSock >= 0) {
+ clientStream = new TcpStream(clientSock, m_bufsize);
+ }
+ return clientStream;
+}
+
+
+int TcpStream::connect(const char *hostname, unsigned short port)
+{
+ struct addrinfo *ai;
+ char portstr[10];
+ snprintf(portstr, sizeof(portstr), "%d", port);
+
+ if (getaddrinfo(hostname, portstr, NULL, &ai) != 0) {
+ return -1;
+ }
+
+ struct addrinfo *i;
+ i = ai;
+ while (i != NULL) {
+ if (::connect(m_sock, i->ai_addr, i->ai_addrlen) >= 0) {
+ break;
+ } else {
+ if (errno != EINTR) {
+ i = i->ai_next;
+ }
+ }
+ }
+
+ freeaddrinfo(ai);
+ if (i == NULL) return -1;
+
+ return 0;
+}
+
+void *TcpStream::allocBuffer(size_t minSize)
+{
+ size_t allocSize = (m_bufsize < minSize ? minSize : m_bufsize);
+ if (!m_buf) {
+ m_buf = (unsigned char *)malloc(allocSize);
+ }
+ else if (m_bufsize < allocSize) {
+ unsigned char *p = (unsigned char *)realloc(m_buf, allocSize);
+ if (p != NULL) {
+ m_buf = p;
+ m_bufsize = allocSize;
+ } else {
+ ERR("realloc (%d) failed\n", allocSize);
+ free(m_buf);
+ m_buf = NULL;
+ m_bufsize = 0;
+ }
+ }
+
+ return m_buf;
+};
+
+int TcpStream::commitBuffer(size_t size)
+{
+ return writeFully(m_buf, size);
+}
+
+int TcpStream::writeFully(const void *buf, size_t len)
+{
+ if (!valid()) return -1;
+
+ size_t res = len;
+ int retval = 0;
+
+ while (res > 0) {
+ ssize_t stat = ::send(m_sock, (unsigned char *)(buf) + (len - res), res, 0);
+ if (stat < 0) {
+ if (errno != EINTR) {
+ retval = stat;
+ break;
+ }
+ } else {
+ res -= stat;
+ }
+ }
+ return retval;
+}
+
+const unsigned char *TcpStream::readFully(void *buf, size_t len)
+{
+ if (!valid()) return NULL;
+ if (!buf) return NULL; // do not allow NULL buf in that implementation
+ size_t res = len;
+ while (res > 0) {
+ ssize_t stat = ::recv(m_sock, (unsigned char *)(buf) + len - res, len, MSG_WAITALL);
+ if (stat == 0) {
+ // client shutdown;
+ return NULL;
+ } else if (stat < 0) {
+ if (errno == EINTR) {
+ continue;
+ } else {
+ return NULL;
+ }
+ } else {
+ res -= stat;
+ }
+ }
+ return (const unsigned char *)buf;
+}
+
+int TcpStream::recv(void *buf, size_t len)
+{
+ if (!valid()) return int(ERR_INVALID_SOCKET);
+ int res = 0;
+ while(true) {
+ res = ::recv(m_sock, buf, len, 0);
+ if (res < 0) {
+ if (errno == EINTR) {
+ continue;
+ }
+ }
+ break;
+ }
+ return res;
+}
diff --git a/tools/emulator/opengl/shared/OpenglCodecCommon/TcpStream.h b/tools/emulator/opengl/shared/OpenglCodecCommon/TcpStream.h
new file mode 100644
index 000000000..aa65b7ed7
--- /dev/null
+++ b/tools/emulator/opengl/shared/OpenglCodecCommon/TcpStream.h
@@ -0,0 +1,50 @@
+/*
+* 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 __TCP_STREAM_H
+#define __TCP_STREAM_H
+
+#include <stdlib.h>
+#include "IOStream.h"
+
+
+class TcpStream : public IOStream {
+public:
+ typedef enum { ERR_INVALID_SOCKET = -1000 } TcpStreamError;
+
+ explicit TcpStream(size_t bufsize = 10000);
+ ~TcpStream();
+ int listen(unsigned short port, bool localhost_only = true, bool reuse_address = true);
+ TcpStream *accept();
+ int connect(const char *hostname, unsigned short port);
+
+ virtual void *allocBuffer(size_t minSize);
+ virtual int commitBuffer(size_t size);
+ virtual const unsigned char *readFully( void *buf, size_t len);
+
+ bool valid() { return m_sock >= 0; }
+ int recv(void *buf, size_t len);
+
+private:
+ int writeFully(const void *buf, size_t len);
+
+private:
+ int m_sock;
+ size_t m_bufsize;
+ unsigned char *m_buf;
+ TcpStream(int sock, size_t bufSize);
+};
+
+#endif
diff --git a/tools/emulator/opengl/shared/OpenglCodecCommon/TimeUtils.cpp b/tools/emulator/opengl/shared/OpenglCodecCommon/TimeUtils.cpp
new file mode 100644
index 000000000..7f65cca5e
--- /dev/null
+++ b/tools/emulator/opengl/shared/OpenglCodecCommon/TimeUtils.cpp
@@ -0,0 +1,55 @@
+/*
+* 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.
+*/
+#include "TimeUtils.h"
+
+#ifdef _WIN32
+#include <Windows.h>
+#include <time.h>
+#include <stdio.h>
+#elif defined(__linux__)
+#include <stdlib.h>
+#include <sys/time.h>
+#include <time.h>
+#else
+#error "Unsupported platform"
+#endif
+
+long long GetCurrentTimeMS()
+{
+#ifdef _WIN32
+ static LARGE_INTEGER freq;
+ static bool bNotInit = true;
+ if ( bNotInit ) {
+ bNotInit = (QueryPerformanceFrequency( &freq ) == FALSE);
+ }
+ LARGE_INTEGER currVal;
+ QueryPerformanceCounter( &currVal );
+
+ return currVal.QuadPart / (freq.QuadPart / 1000);
+
+#elif defined(__linux__)
+
+ struct timespec now;
+ clock_gettime(CLOCK_MONOTONIC, &now);
+ long long iDiff = (now.tv_sec * 1000LL) + now.tv_nsec/1000000LL;
+ return iDiff;
+
+#else
+
+#error "Unsupported platform"
+
+#endif
+}
diff --git a/tools/emulator/opengl/shared/OpenglCodecCommon/TimeUtils.h b/tools/emulator/opengl/shared/OpenglCodecCommon/TimeUtils.h
new file mode 100644
index 000000000..184a13f81
--- /dev/null
+++ b/tools/emulator/opengl/shared/OpenglCodecCommon/TimeUtils.h
@@ -0,0 +1,21 @@
+/*
+* 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 _TIME_UTILS_H
+#define _TIME_UTILS_H
+
+long long GetCurrentTimeMS();
+
+#endif
diff --git a/tools/emulator/opengl/shared/OpenglCodecCommon/codec_defs.h b/tools/emulator/opengl/shared/OpenglCodecCommon/codec_defs.h
new file mode 100644
index 000000000..968ea3fdc
--- /dev/null
+++ b/tools/emulator/opengl/shared/OpenglCodecCommon/codec_defs.h
@@ -0,0 +1,21 @@
+/*
+* 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 _CODEC_DEFS_H
+#define _CODEC_DEFS_H
+
+#define CODEC_SERVER_PORT 22468
+
+#endif
diff --git a/tools/emulator/opengl/shared/OpenglCodecCommon/glUtils.cpp b/tools/emulator/opengl/shared/OpenglCodecCommon/glUtils.cpp
new file mode 100644
index 000000000..ad4888a9b
--- /dev/null
+++ b/tools/emulator/opengl/shared/OpenglCodecCommon/glUtils.cpp
@@ -0,0 +1,147 @@
+/*
+* 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.
+*/
+#include "glUtils.h"
+#include <string.h>
+#include "ErrorLog.h"
+
+size_t glSizeof(GLenum type)
+{
+ size_t retval = 0;
+ switch(type) {
+ case GL_BYTE:
+ case GL_UNSIGNED_BYTE:
+ retval = 1;
+ break;
+ case GL_SHORT:
+ case GL_UNSIGNED_SHORT:
+ retval = 2;
+ break;
+ case GL_FLOAT:
+ case GL_FIXED:
+ retval = 4;
+ break;
+#ifdef GL_DOUBLE
+ case GL_DOUBLE:
+ retval = 8;
+ break;
+#endif
+ }
+ return retval;
+
+}
+
+size_t glUtilsParamSize(GLenum param)
+{
+ size_t s = 0;
+
+ switch(param)
+ {
+ case GL_MAX_TEXTURE_SIZE:
+ case GL_TEXTURE_GEN_MODE_OES:
+ case GL_TEXTURE_ENV_MODE:
+ case GL_FOG_MODE:
+ case GL_FOG_DENSITY:
+ case GL_FOG_START:
+ case GL_FOG_END:
+ case GL_SPOT_EXPONENT:
+ case GL_CONSTANT_ATTENUATION:
+ case GL_LINEAR_ATTENUATION:
+ case GL_QUADRATIC_ATTENUATION:
+ case GL_SHININESS:
+ case GL_LIGHT_MODEL_TWO_SIDE:
+ case GL_POINT_SIZE:
+ case GL_POINT_SIZE_MIN:
+ case GL_POINT_SIZE_MAX:
+ case GL_POINT_FADE_THRESHOLD_SIZE:
+ case GL_CULL_FACE_MODE:
+ case GL_FRONT_FACE:
+ case GL_SHADE_MODEL:
+ case GL_DEPTH_WRITEMASK:
+ case GL_DEPTH_CLEAR_VALUE:
+ case GL_STENCIL_FAIL:
+ case GL_STENCIL_PASS_DEPTH_FAIL:
+ case GL_STENCIL_PASS_DEPTH_PASS:
+ case GL_STENCIL_REF:
+ case GL_STENCIL_WRITEMASK:
+ case GL_MATRIX_MODE:
+ case GL_MODELVIEW_STACK_DEPTH:
+ case GL_PROJECTION_STACK_DEPTH:
+ case GL_TEXTURE_STACK_DEPTH:
+ case GL_ALPHA_TEST_FUNC:
+ case GL_ALPHA_TEST_REF:
+ case GL_BLEND_DST:
+ case GL_BLEND_SRC:
+ case GL_LOGIC_OP_MODE:
+ case GL_SCISSOR_TEST:
+ case GL_MAX_TEXTURE_UNITS:
+ s = 1;
+ break;
+ case GL_ALIASED_LINE_WIDTH_RANGE:
+ case GL_ALIASED_POINT_SIZE_RANGE:
+ case GL_DEPTH_RANGE:
+ case GL_MAX_VIEWPORT_DIMS:
+ case GL_SMOOTH_POINT_SIZE_RANGE:
+ case GL_SMOOTH_LINE_WIDTH_RANGE:
+ s= 2;
+ break;
+ case GL_SPOT_DIRECTION:
+ case GL_POINT_DISTANCE_ATTENUATION:
+ case GL_CURRENT_NORMAL:
+ s = 3;
+ break;
+ case GL_CURRENT_TEXTURE_COORDS:
+ case GL_CURRENT_COLOR:
+ case GL_FOG_COLOR:
+ case GL_AMBIENT:
+ case GL_DIFFUSE:
+ case GL_SPECULAR:
+ case GL_EMISSION:
+ case GL_POSITION:
+ case GL_LIGHT_MODEL_AMBIENT:
+ case GL_TEXTURE_ENV_COLOR:
+ case GL_SCISSOR_BOX:
+ case GL_VIEWPORT:
+ case GL_TEXTURE_CROP_RECT_OES:
+ s = 4;
+ break;
+ case GL_MODELVIEW_MATRIX:
+ case GL_PROJECTION_MATRIX:
+ case GL_TEXTURE_MATRIX:
+ s = 16;
+ default:
+ ERR("glUtilsParamSize: unknow param 0x%08x\n", param);
+ s = 1; // assume 1
+ }
+ return s;
+}
+
+void glUtilsPackPointerData(unsigned char *dst, unsigned char *src,
+ int size, GLenum type, unsigned int stride,
+ unsigned int datalen)
+{
+ unsigned int vsize = size * glSizeof(type);
+ if (stride == 0) stride = vsize;
+
+ if (stride == vsize) {
+ memcpy(dst, src, datalen);
+ } else {
+ for (unsigned int i = 0; i < datalen; i += vsize) {
+ memcpy(dst, src, vsize);
+ dst += vsize;
+ src += stride;
+ }
+ }
+}
diff --git a/tools/emulator/opengl/shared/OpenglCodecCommon/glUtils.h b/tools/emulator/opengl/shared/OpenglCodecCommon/glUtils.h
new file mode 100644
index 000000000..96b29d660
--- /dev/null
+++ b/tools/emulator/opengl/shared/OpenglCodecCommon/glUtils.h
@@ -0,0 +1,56 @@
+/*
+* 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 __GL_UTILS_H__
+#define __GL_UTILS_H__
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#ifdef GL_API
+ #undef GL_API
+#endif
+#define GL_API
+
+#ifdef GL_APIENTRY
+ #undef GL_APIENTRY
+#endif
+
+#ifdef GL_APIENTRYP
+ #undef GL_APIENTRYP
+#endif
+#define GL_APIENTRYP
+
+#ifndef ANDROID
+#define GL_APIENTRY
+#endif
+
+#include <GLES/gl.h>
+#include <GLES/glext.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+ size_t glSizeof(GLenum type);
+ size_t glUtilsParamSize(GLenum param);
+ void glUtilsPackPointerData(unsigned char *dst, unsigned char *str,
+ int size, GLenum type, unsigned int stride,
+ unsigned int datalen);
+#ifdef __cplusplus
+};
+#endif
+
+#endif