changeset 4711:ed040b480a9f

- Restructured threads and application structure. - Moved to SurfaceView instead of GLSurfaceView - Moved to C++ for the android library
author Paul Hunkin <paul@bieh.net>
date Tue, 29 Jun 2010 00:40:12 +1200
parents aeac51289991
children 8319aa8fa4dc
files android/testproject/jni/Android.mk android/testproject/jni/app-android.c android/testproject/jni/app-android.cpp android/testproject/jni/importgl.c android/testproject/jni/importgl.cpp android/testproject/src/org/libsdl/android/TestActivity.java src/video/android/SDL_androidgl.c
diffstat 7 files changed, 426 insertions(+), 385 deletions(-) [+]
line wrap: on
line diff
--- a/android/testproject/jni/Android.mk	Mon Jun 28 21:35:28 2010 +1200
+++ b/android/testproject/jni/Android.mk	Tue Jun 29 00:40:12 2010 +1200
@@ -11,8 +11,8 @@
                 -I$(SDL)/include
 
 LOCAL_SRC_FILES := \
-    importgl.c \
-    app-android.c \
+    importgl.cpp \
+    app-android.cpp \
     lesson05.c \
 
 LOCAL_LDLIBS := -lGLESv1_CM -ldl -llog -lSDL -lEGL -lgcc -L$(SDL) -L$(SDL)/build-scripts/android_libs/
--- a/android/testproject/jni/app-android.c	Mon Jun 28 21:35:28 2010 +1200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,183 +0,0 @@
-/*******************************************************************************
-                               Headers
-*******************************************************************************/
-#include <jni.h>
-#include <sys/time.h>
-#include <time.h>
-#include <android/log.h>
-#include <stdint.h>
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <math.h>
-
-#include <pthread.h>
-
-#include "importgl.h"
-#include "egl.h"
-
-/*******************************************************************************
-                               Globals
-*******************************************************************************/
-int gAppAlive = 1;
-
-static int sWindowWidth  = 320;
-static int sWindowHeight = 480;
-static int sDemoStopped  = 0;
-
-static long _getTime(void){
-	struct timeval  now;
-	gettimeofday(&now, NULL);
-	return (long)(now.tv_sec*1000 + now.tv_usec/1000);
-}
-
-
-/*******************************************************************************
-                               Things used by libsdl
-*******************************************************************************/
-pthread_mutex_t mSDLRenderMutex;
-pthread_cond_t mSDLRenderCondition;
-
-EGLContext mContext;
-EGLDisplay mDisplay;
-EGLSurface mRead;
-EGLSurface mDraw;
-	
-/*******************************************************************************
-                      SDL thread
-*******************************************************************************/
-pthread_t mSDLThread = 0;
-
-void* sdlThreadProc(void* args){
-	__android_log_print(ANDROID_LOG_INFO, "SDL", "Thread Entry");
-
-	if(!eglMakeCurrent(mDisplay, mDraw, mRead, mContext)){
-		__android_log_print(ANDROID_LOG_INFO, "SDL", "Couldn't make current: 0x%x", eglGetError());
-		return NULL;
-	}
-
-	
-	return (void *)SDL_main();
-}
-   
-/*******************************************************************************
-                      Initialize the graphics state
-*******************************************************************************/
-void Java_org_libsdl_android_TestRenderer_nativeInit( JNIEnv*  env )
-{
-	importGLInit();
-
-	gAppAlive    = 1;
-	sDemoStopped = 0;
-
-	__android_log_print(ANDROID_LOG_INFO, "SDL", "Entry point");
-
-	pthread_mutex_init(&mSDLRenderMutex, NULL);
-	pthread_cond_init (&mSDLRenderCondition, NULL);
-
-	//Get some egl stuff we need
-	mContext = eglGetCurrentContext();
-	mDisplay = eglGetCurrentDisplay();
-	mRead = eglGetCurrentSurface(EGL_READ);
-	mDraw = eglGetCurrentSurface(EGL_DRAW);
-
-	//We need to abandon our context so SDL can have it
-	if(!eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT)){
-		__android_log_print(ANDROID_LOG_INFO, "SDL", "Couldn't abandon context: 0x%x", eglGetError());
-		return NULL;
-	}
-
-	//Spin up the SDL thread
-	int r = pthread_create(&mSDLThread, NULL, sdlThreadProc, NULL);
-
-	if(r != 0){
-		__android_log_print(ANDROID_LOG_INFO, "SDL", "Couldn't spawn thread: %d", r);
-	}else{		
-		__android_log_print(ANDROID_LOG_INFO, "SDL", "Started SDL thread");
-	}
-
-}
-
-/*******************************************************************************
-                                 Resize
-*******************************************************************************/
-void Java_org_libsdl_android_TestRenderer_nativeResize( JNIEnv*  env, 
-														jobject  thiz, 
-														jint w,
-														jint h )
-{
-	sWindowWidth  = w;
-	sWindowHeight = h;
-	__android_log_print(ANDROID_LOG_INFO, "SDL", "resize w=%d h=%d", w, h);
-
-}
-
-/*******************************************************************************
-                         Finalize (ie: shutdown)
-*******************************************************************************/
-void Java_org_libsdl_android_TestRenderer_nativeDone( JNIEnv*  env )
-{
-
-	//shut down the app
-
-	importGLDeinit();
-
-	__android_log_print(ANDROID_LOG_INFO, "SDL", "Finalize");
-}
-
-/*******************************************************************************
-                   Pause (ie: stop as soon as possible)
-*******************************************************************************/
-void Java_org_libsdl_android_TestGLSurfaceView_nativePause( JNIEnv*  env )
-{
-	sDemoStopped = !sDemoStopped;
-	if (sDemoStopped) {
-		//we paused
-		__android_log_print(ANDROID_LOG_INFO, "SDL", "Pause");
-	} else {
-		//we resumed
-		__android_log_print(ANDROID_LOG_INFO, "SDL", "Resume");
-	}
-}
-
-/*******************************************************************************
-                     Render the next frame
-*******************************************************************************/
-
-volatile int frames = 0;
-volatile int startSDL = 0;
-
-//eglSwapBuffers(mDisplay, mDraw);
-	
-void Java_org_libsdl_android_TestRenderer_nativeRender( JNIEnv*  env )
-{    
-	__android_log_print(ANDROID_LOG_INFO, "SDL", "JNI: BeginRender");
-
-    //Let the SDL thread do an entire run
-    int lastFrames = frames;
-    startSDL = 1;
-
-    //wait for it to finish
-    while(lastFrames == frames){
-        ;   
-    }
-
-	__android_log_print(ANDROID_LOG_INFO, "SDL", "JNI: EndRender");
-}
-
-void sdl_render(){
-
-    //When we get here, we've accumulated a full frame
-
-    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: BeginRender");
-    
-    frames++;
-
-    while(startSDL == 0){
-        ;
-    }
-    startSDL = 0;
-
-    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: EndRender");
-}
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/android/testproject/jni/app-android.cpp	Tue Jun 29 00:40:12 2010 +1200
@@ -0,0 +1,101 @@
+/*******************************************************************************
+                               Headers
+*******************************************************************************/
+#include <jni.h>
+#include <sys/time.h>
+#include <time.h>
+#include <android/log.h>
+#include <stdint.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+
+#include <pthread.h>
+
+#include "importgl.h"
+#include "egl.h"
+
+/*******************************************************************************
+                               Globals
+*******************************************************************************/
+static long _getTime(void){
+	struct timeval  now;
+	gettimeofday(&now, NULL);
+	return (long)(now.tv_sec*1000 + now.tv_usec/1000);
+}
+
+JNIEnv* mEnv = NULL;
+JavaVM* mVM = NULL;
+
+//Main activity
+jclass mActivityInstance;
+
+//method signatures
+jmethodID midCreateGLContext;
+jmethodID midFlipBuffers;
+
+extern "C" int SDL_main();
+
+/*******************************************************************************
+                 Functions called by JNI
+*******************************************************************************/	
+
+extern "C" void Java_org_libsdl_android_TestActivity_nativeInit( JNIEnv*  env, jobject obj )
+{    
+	__android_log_print(ANDROID_LOG_INFO, "SDL", "JNI: NativeInit");
+
+	mEnv = env;
+
+    SDL_main();
+}
+
+extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
+{
+    JNIEnv* env = NULL;
+    jint result = -1;
+
+    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
+        return result;
+    }
+
+    mEnv = env;
+
+    __android_log_print(ANDROID_LOG_INFO, "SDL", "JNI: OnLoad");
+
+    jclass cls = mEnv->FindClass ("org/libsdl/android/TestActivity"); 
+    mActivityInstance = cls;
+    midCreateGLContext = mEnv->GetStaticMethodID(cls,"createGLContext","()V");
+    midFlipBuffers = mEnv->GetStaticMethodID(cls,"flipBuffers","()V");
+
+    if(!midCreateGLContext || !midFlipBuffers){
+        __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Bad mids\n");
+    }else{
+        __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Good mids\n");
+    }
+    
+    return JNI_VERSION_1_4;
+}
+
+
+
+/*******************************************************************************
+                 Functions called by SDL
+*******************************************************************************/
+extern "C" void sdl_create_context(){
+	__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_create_context()\n");
+
+    mEnv->CallStaticVoidMethod(mActivityInstance, midCreateGLContext ); 
+    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_create_context() return\n");
+
+   // exit(1);
+}
+
+extern "C" void sdl_render(){
+
+    //When we get here, we've accumulated a full frame
+   //__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_render()");
+    
+    mEnv->CallStaticVoidMethod(mActivityInstance, midFlipBuffers ); 
+}
+
--- a/android/testproject/jni/importgl.c	Mon Jun 28 21:35:28 2010 +1200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,168 +0,0 @@
-/* San Angeles Observation OpenGL ES version example
- * Copyright 2004-2005 Jetro Lauha
- * All rights reserved.
- * Web: http://iki.fi/jetro/
- *
- * This source is free software; you can redistribute it and/or
- * modify it under the terms of EITHER:
- *   (1) The GNU Lesser General Public License as published by the Free
- *       Software Foundation; either version 2.1 of the License, or (at
- *       your option) any later version. The text of the GNU Lesser
- *       General Public License is included with this source in the
- *       file LICENSE-LGPL.txt.
- *   (2) The BSD-style license that is included with this source in
- *       the file LICENSE-BSD.txt.
- *
- * This source is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
- * LICENSE-LGPL.txt and LICENSE-BSD.txt for more details.
- *
- * $Id: importgl.c,v 1.4 2005/02/08 18:42:55 tonic Exp $
- * $Revision: 1.4 $
- */
-
-#undef WIN32
-#undef LINUX
-#ifdef _MSC_VER
-// Desktop or mobile Win32 environment:
-#define WIN32
-#else
-// Linux environment:
-#define LINUX
-#endif
-
-#ifndef DISABLE_IMPORTGL
-
-#if defined(WIN32)
-#define WIN32_LEAN_AND_MEAN
-#include <windows.h>
-#include <tchar.h>
-static HMODULE sGLESDLL = NULL;
-#endif // WIN32
-
-#ifdef LINUX
-#include <stdlib.h>
-#include <dlfcn.h>
-static void *sGLESSO = NULL;
-#endif // LINUX
-
-#endif /* DISABLE_IMPORTGL */
-
-#define IMPORTGL_NO_FNPTR_DEFS
-#define IMPORTGL_API
-#define IMPORTGL_FNPTRINIT = NULL
-#include "importgl.h"
-
-
-/* Imports function pointers to selected function calls in OpenGL ES Common
- * or Common Lite profile DLL or shared object. The function pointers are
- * stored as global symbols with equivalent function name but prefixed with
- * "funcPtr_". Standard gl/egl calls are redirected to the function pointers
- * with preprocessor macros (see importgl.h).
- */
-int importGLInit()
-{
-    int result = 1;
-
-#ifndef DISABLE_IMPORTGL
-
-#undef IMPORT_FUNC
-
-#ifdef WIN32
-    sGLESDLL = LoadLibrary(_T("libGLES_CM.dll"));
-    if (sGLESDLL == NULL)
-        sGLESDLL = LoadLibrary(_T("libGLES_CL.dll"));
-    if (sGLESDLL == NULL)
-        return 0;   // Cannot find OpenGL ES Common or Common Lite DLL.
-
-    /* The following fetches address to each egl & gl function call
-     * and stores it to the related function pointer. Casting through
-     * void * results in warnings with VC warning level 4, which
-     * could be fixed by casting to the true type for each fetch.
-     */
-#define IMPORT_FUNC(funcName) do { \
-        void *procAddress = (void *)GetProcAddress(sGLESDLL, _T(#funcName)); \
-        if (procAddress == NULL) result = 0; \
-        *((void **)&FNPTR(funcName)) = procAddress; } while (0)
-#endif // WIN32
-
-#ifdef LINUX
-#ifdef ANDROID_NDK
-    sGLESSO = dlopen("libGLESv1_CM.so", RTLD_NOW);
-#else /* !ANDROID_NDK */
-    sGLESSO = dlopen("libGLES_CM.so", RTLD_NOW);
-    if (sGLESSO == NULL)
-        sGLESSO = dlopen("libGLES_CL.so", RTLD_NOW);
-#endif /* !ANDROID_NDK */
-    if (sGLESSO == NULL)
-        return 0;   // Cannot find OpenGL ES Common or Common Lite SO.
-
-#define IMPORT_FUNC(funcName) do { \
-        void *procAddress = (void *)dlsym(sGLESSO, #funcName); \
-        if (procAddress == NULL) result = 0; \
-        *((void **)&FNPTR(funcName)) = procAddress; } while (0)
-#endif // LINUX
-
-#ifndef ANDROID_NDK
-    IMPORT_FUNC(eglChooseConfig);
-    IMPORT_FUNC(eglCreateContext);
-    IMPORT_FUNC(eglCreateWindowSurface);
-    IMPORT_FUNC(eglDestroyContext);
-    IMPORT_FUNC(eglDestroySurface);
-    IMPORT_FUNC(eglGetConfigAttrib);
-    IMPORT_FUNC(eglGetConfigs);
-    IMPORT_FUNC(eglGetDisplay);
-    IMPORT_FUNC(eglGetError);
-    IMPORT_FUNC(eglInitialize);
-    IMPORT_FUNC(eglMakeCurrent);
-    IMPORT_FUNC(eglSwapBuffers);
-    IMPORT_FUNC(eglTerminate);
-#endif /* !ANDROID_NDK */
-
-    IMPORT_FUNC(glBlendFunc);
-    IMPORT_FUNC(glClear);
-    IMPORT_FUNC(glClearColorx);
-    IMPORT_FUNC(glColor4x);
-    IMPORT_FUNC(glColorPointer);
-    IMPORT_FUNC(glDisable);
-    IMPORT_FUNC(glDisableClientState);
-    IMPORT_FUNC(glDrawArrays);
-    IMPORT_FUNC(glEnable);
-    IMPORT_FUNC(glEnableClientState);
-    IMPORT_FUNC(glFrustumx);
-    IMPORT_FUNC(glGetError);
-    IMPORT_FUNC(glLightxv);
-    IMPORT_FUNC(glLoadIdentity);
-    IMPORT_FUNC(glMaterialx);
-    IMPORT_FUNC(glMaterialxv);
-    IMPORT_FUNC(glMatrixMode);
-    IMPORT_FUNC(glMultMatrixx);
-    IMPORT_FUNC(glNormalPointer);
-    IMPORT_FUNC(glPopMatrix);
-    IMPORT_FUNC(glPushMatrix);
-    IMPORT_FUNC(glRotatex);
-    IMPORT_FUNC(glScalex);
-    IMPORT_FUNC(glShadeModel);
-    IMPORT_FUNC(glTranslatex);
-    IMPORT_FUNC(glVertexPointer);
-    IMPORT_FUNC(glViewport);
-
-#endif /* DISABLE_IMPORTGL */
-
-    return result;
-}
-
-
-void importGLDeinit()
-{
-#ifndef DISABLE_IMPORTGL
-#ifdef WIN32
-    FreeLibrary(sGLESDLL);
-#endif
-
-#ifdef LINUX
-    dlclose(sGLESSO);
-#endif
-#endif /* DISABLE_IMPORTGL */
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/android/testproject/jni/importgl.cpp	Tue Jun 29 00:40:12 2010 +1200
@@ -0,0 +1,168 @@
+/* San Angeles Observation OpenGL ES version example
+ * Copyright 2004-2005 Jetro Lauha
+ * All rights reserved.
+ * Web: http://iki.fi/jetro/
+ *
+ * This source is free software; you can redistribute it and/or
+ * modify it under the terms of EITHER:
+ *   (1) The GNU Lesser General Public License as published by the Free
+ *       Software Foundation; either version 2.1 of the License, or (at
+ *       your option) any later version. The text of the GNU Lesser
+ *       General Public License is included with this source in the
+ *       file LICENSE-LGPL.txt.
+ *   (2) The BSD-style license that is included with this source in
+ *       the file LICENSE-BSD.txt.
+ *
+ * This source is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
+ * LICENSE-LGPL.txt and LICENSE-BSD.txt for more details.
+ *
+ * $Id: importgl.c,v 1.4 2005/02/08 18:42:55 tonic Exp $
+ * $Revision: 1.4 $
+ */
+
+#undef WIN32
+#undef LINUX
+#ifdef _MSC_VER
+// Desktop or mobile Win32 environment:
+#define WIN32
+#else
+// Linux environment:
+#define LINUX
+#endif
+
+#ifndef DISABLE_IMPORTGL
+
+#if defined(WIN32)
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+#include <tchar.h>
+static HMODULE sGLESDLL = NULL;
+#endif // WIN32
+
+#ifdef LINUX
+#include <stdlib.h>
+#include <dlfcn.h>
+static void *sGLESSO = NULL;
+#endif // LINUX
+
+#endif /* DISABLE_IMPORTGL */
+
+#define IMPORTGL_NO_FNPTR_DEFS
+#define IMPORTGL_API
+#define IMPORTGL_FNPTRINIT = NULL
+#include "importgl.h"
+
+
+/* Imports function pointers to selected function calls in OpenGL ES Common
+ * or Common Lite profile DLL or shared object. The function pointers are
+ * stored as global symbols with equivalent function name but prefixed with
+ * "funcPtr_". Standard gl/egl calls are redirected to the function pointers
+ * with preprocessor macros (see importgl.h).
+ */
+int importGLInit()
+{
+    int result = 1;
+
+#ifndef DISABLE_IMPORTGL
+
+#undef IMPORT_FUNC
+
+#ifdef WIN32
+    sGLESDLL = LoadLibrary(_T("libGLES_CM.dll"));
+    if (sGLESDLL == NULL)
+        sGLESDLL = LoadLibrary(_T("libGLES_CL.dll"));
+    if (sGLESDLL == NULL)
+        return 0;   // Cannot find OpenGL ES Common or Common Lite DLL.
+
+    /* The following fetches address to each egl & gl function call
+     * and stores it to the related function pointer. Casting through
+     * void * results in warnings with VC warning level 4, which
+     * could be fixed by casting to the true type for each fetch.
+     */
+#define IMPORT_FUNC(funcName) do { \
+        void *procAddress = (void *)GetProcAddress(sGLESDLL, _T(#funcName)); \
+        if (procAddress == NULL) result = 0; \
+        *((void **)&FNPTR(funcName)) = procAddress; } while (0)
+#endif // WIN32
+
+#ifdef LINUX
+#ifdef ANDROID_NDK
+    sGLESSO = dlopen("libGLESv1_CM.so", RTLD_NOW);
+#else /* !ANDROID_NDK */
+    sGLESSO = dlopen("libGLES_CM.so", RTLD_NOW);
+    if (sGLESSO == NULL)
+        sGLESSO = dlopen("libGLES_CL.so", RTLD_NOW);
+#endif /* !ANDROID_NDK */
+    if (sGLESSO == NULL)
+        return 0;   // Cannot find OpenGL ES Common or Common Lite SO.
+
+#define IMPORT_FUNC(funcName) do { \
+        void *procAddress = (void *)dlsym(sGLESSO, #funcName); \
+        if (procAddress == NULL) result = 0; \
+        *((void **)&FNPTR(funcName)) = procAddress; } while (0)
+#endif // LINUX
+
+#ifndef ANDROID_NDK
+    IMPORT_FUNC(eglChooseConfig);
+    IMPORT_FUNC(eglCreateContext);
+    IMPORT_FUNC(eglCreateWindowSurface);
+    IMPORT_FUNC(eglDestroyContext);
+    IMPORT_FUNC(eglDestroySurface);
+    IMPORT_FUNC(eglGetConfigAttrib);
+    IMPORT_FUNC(eglGetConfigs);
+    IMPORT_FUNC(eglGetDisplay);
+    IMPORT_FUNC(eglGetError);
+    IMPORT_FUNC(eglInitialize);
+    IMPORT_FUNC(eglMakeCurrent);
+    IMPORT_FUNC(eglSwapBuffers);
+    IMPORT_FUNC(eglTerminate);
+#endif /* !ANDROID_NDK */
+
+    IMPORT_FUNC(glBlendFunc);
+    IMPORT_FUNC(glClear);
+    IMPORT_FUNC(glClearColorx);
+    IMPORT_FUNC(glColor4x);
+    IMPORT_FUNC(glColorPointer);
+    IMPORT_FUNC(glDisable);
+    IMPORT_FUNC(glDisableClientState);
+    IMPORT_FUNC(glDrawArrays);
+    IMPORT_FUNC(glEnable);
+    IMPORT_FUNC(glEnableClientState);
+    IMPORT_FUNC(glFrustumx);
+    IMPORT_FUNC(glGetError);
+    IMPORT_FUNC(glLightxv);
+    IMPORT_FUNC(glLoadIdentity);
+    IMPORT_FUNC(glMaterialx);
+    IMPORT_FUNC(glMaterialxv);
+    IMPORT_FUNC(glMatrixMode);
+    IMPORT_FUNC(glMultMatrixx);
+    IMPORT_FUNC(glNormalPointer);
+    IMPORT_FUNC(glPopMatrix);
+    IMPORT_FUNC(glPushMatrix);
+    IMPORT_FUNC(glRotatex);
+    IMPORT_FUNC(glScalex);
+    IMPORT_FUNC(glShadeModel);
+    IMPORT_FUNC(glTranslatex);
+    IMPORT_FUNC(glVertexPointer);
+    IMPORT_FUNC(glViewport);
+
+#endif /* DISABLE_IMPORTGL */
+
+    return result;
+}
+
+
+void importGLDeinit()
+{
+#ifndef DISABLE_IMPORTGL
+#ifdef WIN32
+    FreeLibrary(sGLESDLL);
+#endif
+
+#ifdef LINUX
+    dlclose(sGLESSO);
+#endif
+#endif /* DISABLE_IMPORTGL */
+}
--- a/android/testproject/src/org/libsdl/android/TestActivity.java	Mon Jun 28 21:35:28 2010 +1200
+++ b/android/testproject/src/org/libsdl/android/TestActivity.java	Tue Jun 29 00:40:12 2010 +1200
@@ -2,66 +2,197 @@
 
 import javax.microedition.khronos.egl.EGLConfig;
 import javax.microedition.khronos.opengles.GL10;
+import javax.microedition.khronos.egl.*;
 
 import android.app.Activity;
 import android.content.Context;
-import android.opengl.GLSurfaceView;
+import android.view.SurfaceHolder;
+import android.view.SurfaceView;
 import android.os.Bundle;
 import android.view.MotionEvent;
+import android.util.Log;
+import android.graphics.*;
+
+import java.lang.*;
+
+
+//http://www.mail-archive.com/android-beginners@googlegroups.com/msg01830.html
+
+/*
+In TestActivity::onResume() call SDL_Init
+SDL_GL_CreateContext call SDLSurface::createSDLGLContext()
+SDL_GL_FlipBuffers calls SDLSurface::flip()
+
+*/
+
+
 
 public class TestActivity extends Activity {
-    @Override
+
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
-        mGLView = new TestGLSurfaceView(this);
-        setContentView(mGLView);
+        mSurface = new SDLSurface(getApplication());
+        setContentView(mSurface);
+        SurfaceHolder holder = mSurface.getHolder();
+        holder.setType(SurfaceHolder.SURFACE_TYPE_GPU); 
     }
 
-    @Override
     protected void onPause() {
         super.onPause();
-        mGLView.onPause();
     }
 
-    @Override
     protected void onResume() {
         super.onResume();
-        mGLView.onResume();
+
+        //All set up. Start up SDL
+       
+       
     }
 
-    private GLSurfaceView mGLView;
+    private static SDLSurface mSurface;
 
     static {
         System.loadLibrary("sanangeles");
     }
+
+    //C functions we call
+    public static native void nativeInit();
+
+
+    //Java functions called from C
+    private static void createGLContext(){
+        mSurface.initEGL();
+    }
+
+    public static void flipBuffers(){
+        mSurface.flipBuffers();
+    }
+}
+
+class SDLThread implements Runnable{
+    public void run(){
+        TestActivity.nativeInit();
+    }
 }
 
-class TestGLSurfaceView extends GLSurfaceView {
-    public TestGLSurfaceView(Context context) {
-        super(context);
-        mRenderer = new TestRenderer();
-        setRenderer(mRenderer);
+class SDLSurface extends SurfaceView implements SurfaceHolder.Callback{
+
+    private EGLContext  mEGLContext;
+    private EGLSurface  mEGLSurface;
+    private EGLDisplay  mEGLDisplay;
+
+    public void surfaceCreated(SurfaceHolder holder) {
+        Log.v("SDL","Surface created"); 
 
-        //setRenderMode(RENDERMODE_WHEN_DIRTY);
+        Thread runner = new Thread(new SDLThread(), "SDLThread"); // (1) Create a new thread.
+		runner.start(); // (2) Start the thread     
+        
+    }
+
+    public void surfaceDestroyed(SurfaceHolder holder) {
+        Log.v("SDL","Surface destroyed");
+    }
+
+    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
+       
     }
 
-    public boolean onTouchEvent(final MotionEvent event) {
-        if (event.getAction() == MotionEvent.ACTION_DOWN) {
-            nativePause();
+
+    boolean initEGL(){
+        Log.v("SDL","Starting up");
+
+        try{
+
+            // Get an EGL instance
+            EGL10 egl = (EGL10)EGLContext.getEGL();
+
+            // Get to the default display.
+            EGLDisplay dpy = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
+
+            // We can now initialize EGL for that display
+            int[] version = new int[2];
+            egl.eglInitialize(dpy, version);
+
+            // Specify a configuration for our opengl session
+            // and grab the first configuration that matches is
+            int[] configSpec = {
+                    //EGL10.EGL_DEPTH_SIZE,   16,
+                    EGL10.EGL_NONE
+            };
+            EGLConfig[] configs = new EGLConfig[1];
+            int[] num_config = new int[1];
+            egl.eglChooseConfig(dpy, configSpec, configs, 1, num_config);
+            EGLConfig config = configs[0];
+
+            // Create an OpenGL ES context. This must be done only once
+            EGLContext ctx = egl.eglCreateContext(dpy, config, EGL10.EGL_NO_CONTEXT, null);
+
+            // Create an EGL surface we can render into.
+            EGLSurface surface = egl.eglCreateWindowSurface(dpy, config, this, null);
+
+            // Before we can issue GL commands, we need to make sure
+            // the context is current and bound to a surface.
+            egl.eglMakeCurrent(dpy, surface, surface, ctx);
+
+            mEGLContext = ctx;
+            mEGLDisplay = dpy;
+            mEGLSurface = surface;
+        }catch(Exception e){
+            Log.v("SDL", e + "");
         }
+
+        Log.v("SDL","Done making!");
+
         return true;
     }
 
-    TestRenderer mRenderer;
+    public SDLSurface(Context context) {
+        super(context);
+
+        getHolder().addCallback(this);
+      
+    }
+
+     public void onDraw(Canvas canvas) {
+
+        
+     }
+
+
+    public void flipBuffers(){
+        //Log.v("test","Draw!");
 
-    private static native void nativePause();
+        try{
+        
+            EGL10 egl = (EGL10)EGLContext.getEGL();
+            GL10 gl = (GL10)mEGLContext.getGL();
+
+            egl.eglWaitNative(EGL10.EGL_NATIVE_RENDERABLE, null);
+
+            //drawing here
+
+            egl.eglWaitGL();
+
+            egl.eglSwapBuffers(mEGLDisplay, mEGLSurface);
+
+            
+        }catch(Exception e){
+            Log.v("SDL", e + "");
+        }
+
+    }
+
 }
 
+
+/*
 class TestRenderer implements GLSurfaceView.Renderer {
     public void onSurfaceCreated(GL10 gl, EGLConfig config) {
         nativeInit();
     }
 
+    
+
     public void onSurfaceChanged(GL10 gl, int w, int h) {
         //gl.glViewport(0, 0, w, h);
         nativeResize(w, h);
@@ -75,4 +206,6 @@
     private static native void nativeResize(int w, int h);
     private static native void nativeRender();
     private static native void nativeDone();
+
 }
+*/
--- a/src/video/android/SDL_androidgl.c	Mon Jun 28 21:35:28 2010 +1200
+++ b/src/video/android/SDL_androidgl.c	Tue Jun 29 00:40:12 2010 +1200
@@ -41,8 +41,7 @@
 /*
 These things are in the JNI android support
 */
-extern pthread_mutex_t mSDLRenderMutex;
-extern pthread_cond_t mSDLRenderCondition;
+extern void sdl_create_context();
 extern void sdl_render();
 
 /* GL functions */
@@ -68,7 +67,7 @@
 */
 
 SDL_GLContext Android_GL_CreateContext(_THIS, SDL_Window * window){
-	__android_log_print(ANDROID_LOG_INFO, "SDL", "[STUB] GL_CreateContext\n");
+	sdl_create_context();
 	return 1;
 }
 
@@ -89,16 +88,7 @@
 }
 
 void Android_GL_SwapWindow(_THIS, SDL_Window * window){
-
-/*
-	pthread_mutex_lock(&mSDLRenderMutex);
-	pthread_cond_wait(&mSDLRenderCondition, &mSDLRenderMutex);
-	pthread_mutex_unlock(&mSDLRenderMutex);
-*/	
-	
-	//__android_log_print(ANDROID_LOG_INFO, "SDL", "[STUB] GL_SwapWindow\n");
 	sdl_render();
-
 }
 
 void Android_GL_DeleteContext(_THIS, SDL_GLContext context){