- Restructured threads and application structure.
authorPaul Hunkin <paul@bieh.net>
Tue, 29 Jun 2010 00:40:12 +1200
changeset 4711ed040b480a9f
parent 4710 aeac51289991
child 4712 8319aa8fa4dc
- Restructured threads and application structure.
- Moved to SurfaceView instead of GLSurfaceView
- Moved to C++ for the android library
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
     1.1 --- a/android/testproject/jni/Android.mk	Mon Jun 28 21:35:28 2010 +1200
     1.2 +++ b/android/testproject/jni/Android.mk	Tue Jun 29 00:40:12 2010 +1200
     1.3 @@ -11,8 +11,8 @@
     1.4                  -I$(SDL)/include
     1.5  
     1.6  LOCAL_SRC_FILES := \
     1.7 -    importgl.c \
     1.8 -    app-android.c \
     1.9 +    importgl.cpp \
    1.10 +    app-android.cpp \
    1.11      lesson05.c \
    1.12  
    1.13  LOCAL_LDLIBS := -lGLESv1_CM -ldl -llog -lSDL -lEGL -lgcc -L$(SDL) -L$(SDL)/build-scripts/android_libs/
     2.1 --- a/android/testproject/jni/app-android.c	Mon Jun 28 21:35:28 2010 +1200
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,183 +0,0 @@
     2.4 -/*******************************************************************************
     2.5 -                               Headers
     2.6 -*******************************************************************************/
     2.7 -#include <jni.h>
     2.8 -#include <sys/time.h>
     2.9 -#include <time.h>
    2.10 -#include <android/log.h>
    2.11 -#include <stdint.h>
    2.12 -
    2.13 -#include <stdio.h>
    2.14 -#include <stdlib.h>
    2.15 -#include <math.h>
    2.16 -
    2.17 -#include <pthread.h>
    2.18 -
    2.19 -#include "importgl.h"
    2.20 -#include "egl.h"
    2.21 -
    2.22 -/*******************************************************************************
    2.23 -                               Globals
    2.24 -*******************************************************************************/
    2.25 -int gAppAlive = 1;
    2.26 -
    2.27 -static int sWindowWidth  = 320;
    2.28 -static int sWindowHeight = 480;
    2.29 -static int sDemoStopped  = 0;
    2.30 -
    2.31 -static long _getTime(void){
    2.32 -	struct timeval  now;
    2.33 -	gettimeofday(&now, NULL);
    2.34 -	return (long)(now.tv_sec*1000 + now.tv_usec/1000);
    2.35 -}
    2.36 -
    2.37 -
    2.38 -/*******************************************************************************
    2.39 -                               Things used by libsdl
    2.40 -*******************************************************************************/
    2.41 -pthread_mutex_t mSDLRenderMutex;
    2.42 -pthread_cond_t mSDLRenderCondition;
    2.43 -
    2.44 -EGLContext mContext;
    2.45 -EGLDisplay mDisplay;
    2.46 -EGLSurface mRead;
    2.47 -EGLSurface mDraw;
    2.48 -	
    2.49 -/*******************************************************************************
    2.50 -                      SDL thread
    2.51 -*******************************************************************************/
    2.52 -pthread_t mSDLThread = 0;
    2.53 -
    2.54 -void* sdlThreadProc(void* args){
    2.55 -	__android_log_print(ANDROID_LOG_INFO, "SDL", "Thread Entry");
    2.56 -
    2.57 -	if(!eglMakeCurrent(mDisplay, mDraw, mRead, mContext)){
    2.58 -		__android_log_print(ANDROID_LOG_INFO, "SDL", "Couldn't make current: 0x%x", eglGetError());
    2.59 -		return NULL;
    2.60 -	}
    2.61 -
    2.62 -	
    2.63 -	return (void *)SDL_main();
    2.64 -}
    2.65 -   
    2.66 -/*******************************************************************************
    2.67 -                      Initialize the graphics state
    2.68 -*******************************************************************************/
    2.69 -void Java_org_libsdl_android_TestRenderer_nativeInit( JNIEnv*  env )
    2.70 -{
    2.71 -	importGLInit();
    2.72 -
    2.73 -	gAppAlive    = 1;
    2.74 -	sDemoStopped = 0;
    2.75 -
    2.76 -	__android_log_print(ANDROID_LOG_INFO, "SDL", "Entry point");
    2.77 -
    2.78 -	pthread_mutex_init(&mSDLRenderMutex, NULL);
    2.79 -	pthread_cond_init (&mSDLRenderCondition, NULL);
    2.80 -
    2.81 -	//Get some egl stuff we need
    2.82 -	mContext = eglGetCurrentContext();
    2.83 -	mDisplay = eglGetCurrentDisplay();
    2.84 -	mRead = eglGetCurrentSurface(EGL_READ);
    2.85 -	mDraw = eglGetCurrentSurface(EGL_DRAW);
    2.86 -
    2.87 -	//We need to abandon our context so SDL can have it
    2.88 -	if(!eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT)){
    2.89 -		__android_log_print(ANDROID_LOG_INFO, "SDL", "Couldn't abandon context: 0x%x", eglGetError());
    2.90 -		return NULL;
    2.91 -	}
    2.92 -
    2.93 -	//Spin up the SDL thread
    2.94 -	int r = pthread_create(&mSDLThread, NULL, sdlThreadProc, NULL);
    2.95 -
    2.96 -	if(r != 0){
    2.97 -		__android_log_print(ANDROID_LOG_INFO, "SDL", "Couldn't spawn thread: %d", r);
    2.98 -	}else{		
    2.99 -		__android_log_print(ANDROID_LOG_INFO, "SDL", "Started SDL thread");
   2.100 -	}
   2.101 -
   2.102 -}
   2.103 -
   2.104 -/*******************************************************************************
   2.105 -                                 Resize
   2.106 -*******************************************************************************/
   2.107 -void Java_org_libsdl_android_TestRenderer_nativeResize( JNIEnv*  env, 
   2.108 -														jobject  thiz, 
   2.109 -														jint w,
   2.110 -														jint h )
   2.111 -{
   2.112 -	sWindowWidth  = w;
   2.113 -	sWindowHeight = h;
   2.114 -	__android_log_print(ANDROID_LOG_INFO, "SDL", "resize w=%d h=%d", w, h);
   2.115 -
   2.116 -}
   2.117 -
   2.118 -/*******************************************************************************
   2.119 -                         Finalize (ie: shutdown)
   2.120 -*******************************************************************************/
   2.121 -void Java_org_libsdl_android_TestRenderer_nativeDone( JNIEnv*  env )
   2.122 -{
   2.123 -
   2.124 -	//shut down the app
   2.125 -
   2.126 -	importGLDeinit();
   2.127 -
   2.128 -	__android_log_print(ANDROID_LOG_INFO, "SDL", "Finalize");
   2.129 -}
   2.130 -
   2.131 -/*******************************************************************************
   2.132 -                   Pause (ie: stop as soon as possible)
   2.133 -*******************************************************************************/
   2.134 -void Java_org_libsdl_android_TestGLSurfaceView_nativePause( JNIEnv*  env )
   2.135 -{
   2.136 -	sDemoStopped = !sDemoStopped;
   2.137 -	if (sDemoStopped) {
   2.138 -		//we paused
   2.139 -		__android_log_print(ANDROID_LOG_INFO, "SDL", "Pause");
   2.140 -	} else {
   2.141 -		//we resumed
   2.142 -		__android_log_print(ANDROID_LOG_INFO, "SDL", "Resume");
   2.143 -	}
   2.144 -}
   2.145 -
   2.146 -/*******************************************************************************
   2.147 -                     Render the next frame
   2.148 -*******************************************************************************/
   2.149 -
   2.150 -volatile int frames = 0;
   2.151 -volatile int startSDL = 0;
   2.152 -
   2.153 -//eglSwapBuffers(mDisplay, mDraw);
   2.154 -	
   2.155 -void Java_org_libsdl_android_TestRenderer_nativeRender( JNIEnv*  env )
   2.156 -{    
   2.157 -	__android_log_print(ANDROID_LOG_INFO, "SDL", "JNI: BeginRender");
   2.158 -
   2.159 -    //Let the SDL thread do an entire run
   2.160 -    int lastFrames = frames;
   2.161 -    startSDL = 1;
   2.162 -
   2.163 -    //wait for it to finish
   2.164 -    while(lastFrames == frames){
   2.165 -        ;   
   2.166 -    }
   2.167 -
   2.168 -	__android_log_print(ANDROID_LOG_INFO, "SDL", "JNI: EndRender");
   2.169 -}
   2.170 -
   2.171 -void sdl_render(){
   2.172 -
   2.173 -    //When we get here, we've accumulated a full frame
   2.174 -
   2.175 -    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: BeginRender");
   2.176 -    
   2.177 -    frames++;
   2.178 -
   2.179 -    while(startSDL == 0){
   2.180 -        ;
   2.181 -    }
   2.182 -    startSDL = 0;
   2.183 -
   2.184 -    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: EndRender");
   2.185 -}
   2.186 -
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/android/testproject/jni/app-android.cpp	Tue Jun 29 00:40:12 2010 +1200
     3.3 @@ -0,0 +1,101 @@
     3.4 +/*******************************************************************************
     3.5 +                               Headers
     3.6 +*******************************************************************************/
     3.7 +#include <jni.h>
     3.8 +#include <sys/time.h>
     3.9 +#include <time.h>
    3.10 +#include <android/log.h>
    3.11 +#include <stdint.h>
    3.12 +
    3.13 +#include <stdio.h>
    3.14 +#include <stdlib.h>
    3.15 +#include <math.h>
    3.16 +
    3.17 +#include <pthread.h>
    3.18 +
    3.19 +#include "importgl.h"
    3.20 +#include "egl.h"
    3.21 +
    3.22 +/*******************************************************************************
    3.23 +                               Globals
    3.24 +*******************************************************************************/
    3.25 +static long _getTime(void){
    3.26 +	struct timeval  now;
    3.27 +	gettimeofday(&now, NULL);
    3.28 +	return (long)(now.tv_sec*1000 + now.tv_usec/1000);
    3.29 +}
    3.30 +
    3.31 +JNIEnv* mEnv = NULL;
    3.32 +JavaVM* mVM = NULL;
    3.33 +
    3.34 +//Main activity
    3.35 +jclass mActivityInstance;
    3.36 +
    3.37 +//method signatures
    3.38 +jmethodID midCreateGLContext;
    3.39 +jmethodID midFlipBuffers;
    3.40 +
    3.41 +extern "C" int SDL_main();
    3.42 +
    3.43 +/*******************************************************************************
    3.44 +                 Functions called by JNI
    3.45 +*******************************************************************************/	
    3.46 +
    3.47 +extern "C" void Java_org_libsdl_android_TestActivity_nativeInit( JNIEnv*  env, jobject obj )
    3.48 +{    
    3.49 +	__android_log_print(ANDROID_LOG_INFO, "SDL", "JNI: NativeInit");
    3.50 +
    3.51 +	mEnv = env;
    3.52 +
    3.53 +    SDL_main();
    3.54 +}
    3.55 +
    3.56 +extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
    3.57 +{
    3.58 +    JNIEnv* env = NULL;
    3.59 +    jint result = -1;
    3.60 +
    3.61 +    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
    3.62 +        return result;
    3.63 +    }
    3.64 +
    3.65 +    mEnv = env;
    3.66 +
    3.67 +    __android_log_print(ANDROID_LOG_INFO, "SDL", "JNI: OnLoad");
    3.68 +
    3.69 +    jclass cls = mEnv->FindClass ("org/libsdl/android/TestActivity"); 
    3.70 +    mActivityInstance = cls;
    3.71 +    midCreateGLContext = mEnv->GetStaticMethodID(cls,"createGLContext","()V");
    3.72 +    midFlipBuffers = mEnv->GetStaticMethodID(cls,"flipBuffers","()V");
    3.73 +
    3.74 +    if(!midCreateGLContext || !midFlipBuffers){
    3.75 +        __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Bad mids\n");
    3.76 +    }else{
    3.77 +        __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Good mids\n");
    3.78 +    }
    3.79 +    
    3.80 +    return JNI_VERSION_1_4;
    3.81 +}
    3.82 +
    3.83 +
    3.84 +
    3.85 +/*******************************************************************************
    3.86 +                 Functions called by SDL
    3.87 +*******************************************************************************/
    3.88 +extern "C" void sdl_create_context(){
    3.89 +	__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_create_context()\n");
    3.90 +
    3.91 +    mEnv->CallStaticVoidMethod(mActivityInstance, midCreateGLContext ); 
    3.92 +    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_create_context() return\n");
    3.93 +
    3.94 +   // exit(1);
    3.95 +}
    3.96 +
    3.97 +extern "C" void sdl_render(){
    3.98 +
    3.99 +    //When we get here, we've accumulated a full frame
   3.100 +   //__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_render()");
   3.101 +    
   3.102 +    mEnv->CallStaticVoidMethod(mActivityInstance, midFlipBuffers ); 
   3.103 +}
   3.104 +
     4.1 --- a/android/testproject/jni/importgl.c	Mon Jun 28 21:35:28 2010 +1200
     4.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.3 @@ -1,168 +0,0 @@
     4.4 -/* San Angeles Observation OpenGL ES version example
     4.5 - * Copyright 2004-2005 Jetro Lauha
     4.6 - * All rights reserved.
     4.7 - * Web: http://iki.fi/jetro/
     4.8 - *
     4.9 - * This source is free software; you can redistribute it and/or
    4.10 - * modify it under the terms of EITHER:
    4.11 - *   (1) The GNU Lesser General Public License as published by the Free
    4.12 - *       Software Foundation; either version 2.1 of the License, or (at
    4.13 - *       your option) any later version. The text of the GNU Lesser
    4.14 - *       General Public License is included with this source in the
    4.15 - *       file LICENSE-LGPL.txt.
    4.16 - *   (2) The BSD-style license that is included with this source in
    4.17 - *       the file LICENSE-BSD.txt.
    4.18 - *
    4.19 - * This source is distributed in the hope that it will be useful,
    4.20 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.21 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
    4.22 - * LICENSE-LGPL.txt and LICENSE-BSD.txt for more details.
    4.23 - *
    4.24 - * $Id: importgl.c,v 1.4 2005/02/08 18:42:55 tonic Exp $
    4.25 - * $Revision: 1.4 $
    4.26 - */
    4.27 -
    4.28 -#undef WIN32
    4.29 -#undef LINUX
    4.30 -#ifdef _MSC_VER
    4.31 -// Desktop or mobile Win32 environment:
    4.32 -#define WIN32
    4.33 -#else
    4.34 -// Linux environment:
    4.35 -#define LINUX
    4.36 -#endif
    4.37 -
    4.38 -#ifndef DISABLE_IMPORTGL
    4.39 -
    4.40 -#if defined(WIN32)
    4.41 -#define WIN32_LEAN_AND_MEAN
    4.42 -#include <windows.h>
    4.43 -#include <tchar.h>
    4.44 -static HMODULE sGLESDLL = NULL;
    4.45 -#endif // WIN32
    4.46 -
    4.47 -#ifdef LINUX
    4.48 -#include <stdlib.h>
    4.49 -#include <dlfcn.h>
    4.50 -static void *sGLESSO = NULL;
    4.51 -#endif // LINUX
    4.52 -
    4.53 -#endif /* DISABLE_IMPORTGL */
    4.54 -
    4.55 -#define IMPORTGL_NO_FNPTR_DEFS
    4.56 -#define IMPORTGL_API
    4.57 -#define IMPORTGL_FNPTRINIT = NULL
    4.58 -#include "importgl.h"
    4.59 -
    4.60 -
    4.61 -/* Imports function pointers to selected function calls in OpenGL ES Common
    4.62 - * or Common Lite profile DLL or shared object. The function pointers are
    4.63 - * stored as global symbols with equivalent function name but prefixed with
    4.64 - * "funcPtr_". Standard gl/egl calls are redirected to the function pointers
    4.65 - * with preprocessor macros (see importgl.h).
    4.66 - */
    4.67 -int importGLInit()
    4.68 -{
    4.69 -    int result = 1;
    4.70 -
    4.71 -#ifndef DISABLE_IMPORTGL
    4.72 -
    4.73 -#undef IMPORT_FUNC
    4.74 -
    4.75 -#ifdef WIN32
    4.76 -    sGLESDLL = LoadLibrary(_T("libGLES_CM.dll"));
    4.77 -    if (sGLESDLL == NULL)
    4.78 -        sGLESDLL = LoadLibrary(_T("libGLES_CL.dll"));
    4.79 -    if (sGLESDLL == NULL)
    4.80 -        return 0;   // Cannot find OpenGL ES Common or Common Lite DLL.
    4.81 -
    4.82 -    /* The following fetches address to each egl & gl function call
    4.83 -     * and stores it to the related function pointer. Casting through
    4.84 -     * void * results in warnings with VC warning level 4, which
    4.85 -     * could be fixed by casting to the true type for each fetch.
    4.86 -     */
    4.87 -#define IMPORT_FUNC(funcName) do { \
    4.88 -        void *procAddress = (void *)GetProcAddress(sGLESDLL, _T(#funcName)); \
    4.89 -        if (procAddress == NULL) result = 0; \
    4.90 -        *((void **)&FNPTR(funcName)) = procAddress; } while (0)
    4.91 -#endif // WIN32
    4.92 -
    4.93 -#ifdef LINUX
    4.94 -#ifdef ANDROID_NDK
    4.95 -    sGLESSO = dlopen("libGLESv1_CM.so", RTLD_NOW);
    4.96 -#else /* !ANDROID_NDK */
    4.97 -    sGLESSO = dlopen("libGLES_CM.so", RTLD_NOW);
    4.98 -    if (sGLESSO == NULL)
    4.99 -        sGLESSO = dlopen("libGLES_CL.so", RTLD_NOW);
   4.100 -#endif /* !ANDROID_NDK */
   4.101 -    if (sGLESSO == NULL)
   4.102 -        return 0;   // Cannot find OpenGL ES Common or Common Lite SO.
   4.103 -
   4.104 -#define IMPORT_FUNC(funcName) do { \
   4.105 -        void *procAddress = (void *)dlsym(sGLESSO, #funcName); \
   4.106 -        if (procAddress == NULL) result = 0; \
   4.107 -        *((void **)&FNPTR(funcName)) = procAddress; } while (0)
   4.108 -#endif // LINUX
   4.109 -
   4.110 -#ifndef ANDROID_NDK
   4.111 -    IMPORT_FUNC(eglChooseConfig);
   4.112 -    IMPORT_FUNC(eglCreateContext);
   4.113 -    IMPORT_FUNC(eglCreateWindowSurface);
   4.114 -    IMPORT_FUNC(eglDestroyContext);
   4.115 -    IMPORT_FUNC(eglDestroySurface);
   4.116 -    IMPORT_FUNC(eglGetConfigAttrib);
   4.117 -    IMPORT_FUNC(eglGetConfigs);
   4.118 -    IMPORT_FUNC(eglGetDisplay);
   4.119 -    IMPORT_FUNC(eglGetError);
   4.120 -    IMPORT_FUNC(eglInitialize);
   4.121 -    IMPORT_FUNC(eglMakeCurrent);
   4.122 -    IMPORT_FUNC(eglSwapBuffers);
   4.123 -    IMPORT_FUNC(eglTerminate);
   4.124 -#endif /* !ANDROID_NDK */
   4.125 -
   4.126 -    IMPORT_FUNC(glBlendFunc);
   4.127 -    IMPORT_FUNC(glClear);
   4.128 -    IMPORT_FUNC(glClearColorx);
   4.129 -    IMPORT_FUNC(glColor4x);
   4.130 -    IMPORT_FUNC(glColorPointer);
   4.131 -    IMPORT_FUNC(glDisable);
   4.132 -    IMPORT_FUNC(glDisableClientState);
   4.133 -    IMPORT_FUNC(glDrawArrays);
   4.134 -    IMPORT_FUNC(glEnable);
   4.135 -    IMPORT_FUNC(glEnableClientState);
   4.136 -    IMPORT_FUNC(glFrustumx);
   4.137 -    IMPORT_FUNC(glGetError);
   4.138 -    IMPORT_FUNC(glLightxv);
   4.139 -    IMPORT_FUNC(glLoadIdentity);
   4.140 -    IMPORT_FUNC(glMaterialx);
   4.141 -    IMPORT_FUNC(glMaterialxv);
   4.142 -    IMPORT_FUNC(glMatrixMode);
   4.143 -    IMPORT_FUNC(glMultMatrixx);
   4.144 -    IMPORT_FUNC(glNormalPointer);
   4.145 -    IMPORT_FUNC(glPopMatrix);
   4.146 -    IMPORT_FUNC(glPushMatrix);
   4.147 -    IMPORT_FUNC(glRotatex);
   4.148 -    IMPORT_FUNC(glScalex);
   4.149 -    IMPORT_FUNC(glShadeModel);
   4.150 -    IMPORT_FUNC(glTranslatex);
   4.151 -    IMPORT_FUNC(glVertexPointer);
   4.152 -    IMPORT_FUNC(glViewport);
   4.153 -
   4.154 -#endif /* DISABLE_IMPORTGL */
   4.155 -
   4.156 -    return result;
   4.157 -}
   4.158 -
   4.159 -
   4.160 -void importGLDeinit()
   4.161 -{
   4.162 -#ifndef DISABLE_IMPORTGL
   4.163 -#ifdef WIN32
   4.164 -    FreeLibrary(sGLESDLL);
   4.165 -#endif
   4.166 -
   4.167 -#ifdef LINUX
   4.168 -    dlclose(sGLESSO);
   4.169 -#endif
   4.170 -#endif /* DISABLE_IMPORTGL */
   4.171 -}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/android/testproject/jni/importgl.cpp	Tue Jun 29 00:40:12 2010 +1200
     5.3 @@ -0,0 +1,168 @@
     5.4 +/* San Angeles Observation OpenGL ES version example
     5.5 + * Copyright 2004-2005 Jetro Lauha
     5.6 + * All rights reserved.
     5.7 + * Web: http://iki.fi/jetro/
     5.8 + *
     5.9 + * This source is free software; you can redistribute it and/or
    5.10 + * modify it under the terms of EITHER:
    5.11 + *   (1) The GNU Lesser General Public License as published by the Free
    5.12 + *       Software Foundation; either version 2.1 of the License, or (at
    5.13 + *       your option) any later version. The text of the GNU Lesser
    5.14 + *       General Public License is included with this source in the
    5.15 + *       file LICENSE-LGPL.txt.
    5.16 + *   (2) The BSD-style license that is included with this source in
    5.17 + *       the file LICENSE-BSD.txt.
    5.18 + *
    5.19 + * This source is distributed in the hope that it will be useful,
    5.20 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    5.21 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
    5.22 + * LICENSE-LGPL.txt and LICENSE-BSD.txt for more details.
    5.23 + *
    5.24 + * $Id: importgl.c,v 1.4 2005/02/08 18:42:55 tonic Exp $
    5.25 + * $Revision: 1.4 $
    5.26 + */
    5.27 +
    5.28 +#undef WIN32
    5.29 +#undef LINUX
    5.30 +#ifdef _MSC_VER
    5.31 +// Desktop or mobile Win32 environment:
    5.32 +#define WIN32
    5.33 +#else
    5.34 +// Linux environment:
    5.35 +#define LINUX
    5.36 +#endif
    5.37 +
    5.38 +#ifndef DISABLE_IMPORTGL
    5.39 +
    5.40 +#if defined(WIN32)
    5.41 +#define WIN32_LEAN_AND_MEAN
    5.42 +#include <windows.h>
    5.43 +#include <tchar.h>
    5.44 +static HMODULE sGLESDLL = NULL;
    5.45 +#endif // WIN32
    5.46 +
    5.47 +#ifdef LINUX
    5.48 +#include <stdlib.h>
    5.49 +#include <dlfcn.h>
    5.50 +static void *sGLESSO = NULL;
    5.51 +#endif // LINUX
    5.52 +
    5.53 +#endif /* DISABLE_IMPORTGL */
    5.54 +
    5.55 +#define IMPORTGL_NO_FNPTR_DEFS
    5.56 +#define IMPORTGL_API
    5.57 +#define IMPORTGL_FNPTRINIT = NULL
    5.58 +#include "importgl.h"
    5.59 +
    5.60 +
    5.61 +/* Imports function pointers to selected function calls in OpenGL ES Common
    5.62 + * or Common Lite profile DLL or shared object. The function pointers are
    5.63 + * stored as global symbols with equivalent function name but prefixed with
    5.64 + * "funcPtr_". Standard gl/egl calls are redirected to the function pointers
    5.65 + * with preprocessor macros (see importgl.h).
    5.66 + */
    5.67 +int importGLInit()
    5.68 +{
    5.69 +    int result = 1;
    5.70 +
    5.71 +#ifndef DISABLE_IMPORTGL
    5.72 +
    5.73 +#undef IMPORT_FUNC
    5.74 +
    5.75 +#ifdef WIN32
    5.76 +    sGLESDLL = LoadLibrary(_T("libGLES_CM.dll"));
    5.77 +    if (sGLESDLL == NULL)
    5.78 +        sGLESDLL = LoadLibrary(_T("libGLES_CL.dll"));
    5.79 +    if (sGLESDLL == NULL)
    5.80 +        return 0;   // Cannot find OpenGL ES Common or Common Lite DLL.
    5.81 +
    5.82 +    /* The following fetches address to each egl & gl function call
    5.83 +     * and stores it to the related function pointer. Casting through
    5.84 +     * void * results in warnings with VC warning level 4, which
    5.85 +     * could be fixed by casting to the true type for each fetch.
    5.86 +     */
    5.87 +#define IMPORT_FUNC(funcName) do { \
    5.88 +        void *procAddress = (void *)GetProcAddress(sGLESDLL, _T(#funcName)); \
    5.89 +        if (procAddress == NULL) result = 0; \
    5.90 +        *((void **)&FNPTR(funcName)) = procAddress; } while (0)
    5.91 +#endif // WIN32
    5.92 +
    5.93 +#ifdef LINUX
    5.94 +#ifdef ANDROID_NDK
    5.95 +    sGLESSO = dlopen("libGLESv1_CM.so", RTLD_NOW);
    5.96 +#else /* !ANDROID_NDK */
    5.97 +    sGLESSO = dlopen("libGLES_CM.so", RTLD_NOW);
    5.98 +    if (sGLESSO == NULL)
    5.99 +        sGLESSO = dlopen("libGLES_CL.so", RTLD_NOW);
   5.100 +#endif /* !ANDROID_NDK */
   5.101 +    if (sGLESSO == NULL)
   5.102 +        return 0;   // Cannot find OpenGL ES Common or Common Lite SO.
   5.103 +
   5.104 +#define IMPORT_FUNC(funcName) do { \
   5.105 +        void *procAddress = (void *)dlsym(sGLESSO, #funcName); \
   5.106 +        if (procAddress == NULL) result = 0; \
   5.107 +        *((void **)&FNPTR(funcName)) = procAddress; } while (0)
   5.108 +#endif // LINUX
   5.109 +
   5.110 +#ifndef ANDROID_NDK
   5.111 +    IMPORT_FUNC(eglChooseConfig);
   5.112 +    IMPORT_FUNC(eglCreateContext);
   5.113 +    IMPORT_FUNC(eglCreateWindowSurface);
   5.114 +    IMPORT_FUNC(eglDestroyContext);
   5.115 +    IMPORT_FUNC(eglDestroySurface);
   5.116 +    IMPORT_FUNC(eglGetConfigAttrib);
   5.117 +    IMPORT_FUNC(eglGetConfigs);
   5.118 +    IMPORT_FUNC(eglGetDisplay);
   5.119 +    IMPORT_FUNC(eglGetError);
   5.120 +    IMPORT_FUNC(eglInitialize);
   5.121 +    IMPORT_FUNC(eglMakeCurrent);
   5.122 +    IMPORT_FUNC(eglSwapBuffers);
   5.123 +    IMPORT_FUNC(eglTerminate);
   5.124 +#endif /* !ANDROID_NDK */
   5.125 +
   5.126 +    IMPORT_FUNC(glBlendFunc);
   5.127 +    IMPORT_FUNC(glClear);
   5.128 +    IMPORT_FUNC(glClearColorx);
   5.129 +    IMPORT_FUNC(glColor4x);
   5.130 +    IMPORT_FUNC(glColorPointer);
   5.131 +    IMPORT_FUNC(glDisable);
   5.132 +    IMPORT_FUNC(glDisableClientState);
   5.133 +    IMPORT_FUNC(glDrawArrays);
   5.134 +    IMPORT_FUNC(glEnable);
   5.135 +    IMPORT_FUNC(glEnableClientState);
   5.136 +    IMPORT_FUNC(glFrustumx);
   5.137 +    IMPORT_FUNC(glGetError);
   5.138 +    IMPORT_FUNC(glLightxv);
   5.139 +    IMPORT_FUNC(glLoadIdentity);
   5.140 +    IMPORT_FUNC(glMaterialx);
   5.141 +    IMPORT_FUNC(glMaterialxv);
   5.142 +    IMPORT_FUNC(glMatrixMode);
   5.143 +    IMPORT_FUNC(glMultMatrixx);
   5.144 +    IMPORT_FUNC(glNormalPointer);
   5.145 +    IMPORT_FUNC(glPopMatrix);
   5.146 +    IMPORT_FUNC(glPushMatrix);
   5.147 +    IMPORT_FUNC(glRotatex);
   5.148 +    IMPORT_FUNC(glScalex);
   5.149 +    IMPORT_FUNC(glShadeModel);
   5.150 +    IMPORT_FUNC(glTranslatex);
   5.151 +    IMPORT_FUNC(glVertexPointer);
   5.152 +    IMPORT_FUNC(glViewport);
   5.153 +
   5.154 +#endif /* DISABLE_IMPORTGL */
   5.155 +
   5.156 +    return result;
   5.157 +}
   5.158 +
   5.159 +
   5.160 +void importGLDeinit()
   5.161 +{
   5.162 +#ifndef DISABLE_IMPORTGL
   5.163 +#ifdef WIN32
   5.164 +    FreeLibrary(sGLESDLL);
   5.165 +#endif
   5.166 +
   5.167 +#ifdef LINUX
   5.168 +    dlclose(sGLESSO);
   5.169 +#endif
   5.170 +#endif /* DISABLE_IMPORTGL */
   5.171 +}
     6.1 --- a/android/testproject/src/org/libsdl/android/TestActivity.java	Mon Jun 28 21:35:28 2010 +1200
     6.2 +++ b/android/testproject/src/org/libsdl/android/TestActivity.java	Tue Jun 29 00:40:12 2010 +1200
     6.3 @@ -2,66 +2,197 @@
     6.4  
     6.5  import javax.microedition.khronos.egl.EGLConfig;
     6.6  import javax.microedition.khronos.opengles.GL10;
     6.7 +import javax.microedition.khronos.egl.*;
     6.8  
     6.9  import android.app.Activity;
    6.10  import android.content.Context;
    6.11 -import android.opengl.GLSurfaceView;
    6.12 +import android.view.SurfaceHolder;
    6.13 +import android.view.SurfaceView;
    6.14  import android.os.Bundle;
    6.15  import android.view.MotionEvent;
    6.16 +import android.util.Log;
    6.17 +import android.graphics.*;
    6.18 +
    6.19 +import java.lang.*;
    6.20 +
    6.21 +
    6.22 +//http://www.mail-archive.com/android-beginners@googlegroups.com/msg01830.html
    6.23 +
    6.24 +/*
    6.25 +In TestActivity::onResume() call SDL_Init
    6.26 +SDL_GL_CreateContext call SDLSurface::createSDLGLContext()
    6.27 +SDL_GL_FlipBuffers calls SDLSurface::flip()
    6.28 +
    6.29 +*/
    6.30 +
    6.31 +
    6.32  
    6.33  public class TestActivity extends Activity {
    6.34 -    @Override
    6.35 +
    6.36      protected void onCreate(Bundle savedInstanceState) {
    6.37          super.onCreate(savedInstanceState);
    6.38 -        mGLView = new TestGLSurfaceView(this);
    6.39 -        setContentView(mGLView);
    6.40 +        mSurface = new SDLSurface(getApplication());
    6.41 +        setContentView(mSurface);
    6.42 +        SurfaceHolder holder = mSurface.getHolder();
    6.43 +        holder.setType(SurfaceHolder.SURFACE_TYPE_GPU); 
    6.44      }
    6.45  
    6.46 -    @Override
    6.47      protected void onPause() {
    6.48          super.onPause();
    6.49 -        mGLView.onPause();
    6.50      }
    6.51  
    6.52 -    @Override
    6.53      protected void onResume() {
    6.54          super.onResume();
    6.55 -        mGLView.onResume();
    6.56 +
    6.57 +        //All set up. Start up SDL
    6.58 +       
    6.59 +       
    6.60      }
    6.61  
    6.62 -    private GLSurfaceView mGLView;
    6.63 +    private static SDLSurface mSurface;
    6.64  
    6.65      static {
    6.66          System.loadLibrary("sanangeles");
    6.67      }
    6.68 +
    6.69 +    //C functions we call
    6.70 +    public static native void nativeInit();
    6.71 +
    6.72 +
    6.73 +    //Java functions called from C
    6.74 +    private static void createGLContext(){
    6.75 +        mSurface.initEGL();
    6.76 +    }
    6.77 +
    6.78 +    public static void flipBuffers(){
    6.79 +        mSurface.flipBuffers();
    6.80 +    }
    6.81  }
    6.82  
    6.83 -class TestGLSurfaceView extends GLSurfaceView {
    6.84 -    public TestGLSurfaceView(Context context) {
    6.85 -        super(context);
    6.86 -        mRenderer = new TestRenderer();
    6.87 -        setRenderer(mRenderer);
    6.88 +class SDLThread implements Runnable{
    6.89 +    public void run(){
    6.90 +        TestActivity.nativeInit();
    6.91 +    }
    6.92 +}
    6.93  
    6.94 -        //setRenderMode(RENDERMODE_WHEN_DIRTY);
    6.95 +class SDLSurface extends SurfaceView implements SurfaceHolder.Callback{
    6.96 +
    6.97 +    private EGLContext  mEGLContext;
    6.98 +    private EGLSurface  mEGLSurface;
    6.99 +    private EGLDisplay  mEGLDisplay;
   6.100 +
   6.101 +    public void surfaceCreated(SurfaceHolder holder) {
   6.102 +        Log.v("SDL","Surface created"); 
   6.103 +
   6.104 +        Thread runner = new Thread(new SDLThread(), "SDLThread"); // (1) Create a new thread.
   6.105 +		runner.start(); // (2) Start the thread     
   6.106 +        
   6.107      }
   6.108  
   6.109 -    public boolean onTouchEvent(final MotionEvent event) {
   6.110 -        if (event.getAction() == MotionEvent.ACTION_DOWN) {
   6.111 -            nativePause();
   6.112 +    public void surfaceDestroyed(SurfaceHolder holder) {
   6.113 +        Log.v("SDL","Surface destroyed");
   6.114 +    }
   6.115 +
   6.116 +    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
   6.117 +       
   6.118 +    }
   6.119 +
   6.120 +
   6.121 +    boolean initEGL(){
   6.122 +        Log.v("SDL","Starting up");
   6.123 +
   6.124 +        try{
   6.125 +
   6.126 +            // Get an EGL instance
   6.127 +            EGL10 egl = (EGL10)EGLContext.getEGL();
   6.128 +
   6.129 +            // Get to the default display.
   6.130 +            EGLDisplay dpy = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
   6.131 +
   6.132 +            // We can now initialize EGL for that display
   6.133 +            int[] version = new int[2];
   6.134 +            egl.eglInitialize(dpy, version);
   6.135 +
   6.136 +            // Specify a configuration for our opengl session
   6.137 +            // and grab the first configuration that matches is
   6.138 +            int[] configSpec = {
   6.139 +                    //EGL10.EGL_DEPTH_SIZE,   16,
   6.140 +                    EGL10.EGL_NONE
   6.141 +            };
   6.142 +            EGLConfig[] configs = new EGLConfig[1];
   6.143 +            int[] num_config = new int[1];
   6.144 +            egl.eglChooseConfig(dpy, configSpec, configs, 1, num_config);
   6.145 +            EGLConfig config = configs[0];
   6.146 +
   6.147 +            // Create an OpenGL ES context. This must be done only once
   6.148 +            EGLContext ctx = egl.eglCreateContext(dpy, config, EGL10.EGL_NO_CONTEXT, null);
   6.149 +
   6.150 +            // Create an EGL surface we can render into.
   6.151 +            EGLSurface surface = egl.eglCreateWindowSurface(dpy, config, this, null);
   6.152 +
   6.153 +            // Before we can issue GL commands, we need to make sure
   6.154 +            // the context is current and bound to a surface.
   6.155 +            egl.eglMakeCurrent(dpy, surface, surface, ctx);
   6.156 +
   6.157 +            mEGLContext = ctx;
   6.158 +            mEGLDisplay = dpy;
   6.159 +            mEGLSurface = surface;
   6.160 +        }catch(Exception e){
   6.161 +            Log.v("SDL", e + "");
   6.162          }
   6.163 +
   6.164 +        Log.v("SDL","Done making!");
   6.165 +
   6.166          return true;
   6.167      }
   6.168  
   6.169 -    TestRenderer mRenderer;
   6.170 +    public SDLSurface(Context context) {
   6.171 +        super(context);
   6.172  
   6.173 -    private static native void nativePause();
   6.174 +        getHolder().addCallback(this);
   6.175 +      
   6.176 +    }
   6.177 +
   6.178 +     public void onDraw(Canvas canvas) {
   6.179 +
   6.180 +        
   6.181 +     }
   6.182 +
   6.183 +
   6.184 +    public void flipBuffers(){
   6.185 +        //Log.v("test","Draw!");
   6.186 +
   6.187 +        try{
   6.188 +        
   6.189 +            EGL10 egl = (EGL10)EGLContext.getEGL();
   6.190 +            GL10 gl = (GL10)mEGLContext.getGL();
   6.191 +
   6.192 +            egl.eglWaitNative(EGL10.EGL_NATIVE_RENDERABLE, null);
   6.193 +
   6.194 +            //drawing here
   6.195 +
   6.196 +            egl.eglWaitGL();
   6.197 +
   6.198 +            egl.eglSwapBuffers(mEGLDisplay, mEGLSurface);
   6.199 +
   6.200 +            
   6.201 +        }catch(Exception e){
   6.202 +            Log.v("SDL", e + "");
   6.203 +        }
   6.204 +
   6.205 +    }
   6.206 +
   6.207  }
   6.208  
   6.209 +
   6.210 +/*
   6.211  class TestRenderer implements GLSurfaceView.Renderer {
   6.212      public void onSurfaceCreated(GL10 gl, EGLConfig config) {
   6.213          nativeInit();
   6.214      }
   6.215  
   6.216 +    
   6.217 +
   6.218      public void onSurfaceChanged(GL10 gl, int w, int h) {
   6.219          //gl.glViewport(0, 0, w, h);
   6.220          nativeResize(w, h);
   6.221 @@ -75,4 +206,6 @@
   6.222      private static native void nativeResize(int w, int h);
   6.223      private static native void nativeRender();
   6.224      private static native void nativeDone();
   6.225 +
   6.226  }
   6.227 +*/
     7.1 --- a/src/video/android/SDL_androidgl.c	Mon Jun 28 21:35:28 2010 +1200
     7.2 +++ b/src/video/android/SDL_androidgl.c	Tue Jun 29 00:40:12 2010 +1200
     7.3 @@ -41,8 +41,7 @@
     7.4  /*
     7.5  These things are in the JNI android support
     7.6  */
     7.7 -extern pthread_mutex_t mSDLRenderMutex;
     7.8 -extern pthread_cond_t mSDLRenderCondition;
     7.9 +extern void sdl_create_context();
    7.10  extern void sdl_render();
    7.11  
    7.12  /* GL functions */
    7.13 @@ -68,7 +67,7 @@
    7.14  */
    7.15  
    7.16  SDL_GLContext Android_GL_CreateContext(_THIS, SDL_Window * window){
    7.17 -	__android_log_print(ANDROID_LOG_INFO, "SDL", "[STUB] GL_CreateContext\n");
    7.18 +	sdl_create_context();
    7.19  	return 1;
    7.20  }
    7.21  
    7.22 @@ -89,16 +88,7 @@
    7.23  }
    7.24  
    7.25  void Android_GL_SwapWindow(_THIS, SDL_Window * window){
    7.26 -
    7.27 -/*
    7.28 -	pthread_mutex_lock(&mSDLRenderMutex);
    7.29 -	pthread_cond_wait(&mSDLRenderCondition, &mSDLRenderMutex);
    7.30 -	pthread_mutex_unlock(&mSDLRenderMutex);
    7.31 -*/	
    7.32 -	
    7.33 -	//__android_log_print(ANDROID_LOG_INFO, "SDL", "[STUB] GL_SwapWindow\n");
    7.34  	sdl_render();
    7.35 -
    7.36  }
    7.37  
    7.38  void Android_GL_DeleteContext(_THIS, SDL_GLContext context){