DEADSOFTWARE

Update NanoGL Tizen port
[nanogl.git] / nanogl.cpp
1 /*
2 Copyright (C) 2007-2009 Olli Hinkka
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
21 #define LOG_TAG "nanoGL"
23 #include <stdio.h>
24 #include <stdlib.h>
26 //#include <cutils/log.h>
28 #include "nanogl.h"
29 #include "glesinterface.h"
30 #include "gl.h"
32 #define DEBUG_NANO 0
34 #ifdef __ANDROID__
36 #include <android/log.h>
38 #define LOG __android_log_print
40 #define LOGI( ... ) __android_log_print( ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__ )
41 #define LOGE( ... ) __android_log_print( ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__ )
42 #define LOGW( ... ) __android_log_print( ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__ )
43 #define LOGD( ... ) if ( DEBUG_NANO ) __android_log_print( ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__ )
45 #elif defined( TIZEN )
46 #include <dlog.h>
48 #define LOGI( ... ) dlog_print( DLOG_INFO, LOG_TAG, __VA_ARGS__ )
49 #define LOGE( ... ) dlog_print( DLOG_ERROR, LOG_TAG, __VA_ARGS__ )
50 #define LOGW( ... ) dlog_print( DLOG_WARN, LOG_TAG, __VA_ARGS__ )
51 #define LOGD( ... ) if( DEBUG_NANO ) dlog_print( DLOG_DEBUG, LOG_TAG, __VA_ARGS__ )
54 #elif !defined(_MSC_VER)
56 #define LOGI( ... ) \
57 printf( "I: "__VA_ARGS__ ); \
58 printf( "\n" )
59 #define LOGD( ... ) \
60 if ( DEBUG_NANO ) \
61 { \
62 printf( "D: "__VA_ARGS__ ); \
63 printf( "\n" ); \
64 }
65 #define LOGE( ... ) \
66 printf( "E: "__VA_ARGS__ ); \
67 printf( "\n" )
68 #define LOGW( ... ) \
69 printf( "W: "__VA_ARGS__ ); \
70 printf( "\n" )
71 #else
73 #define LOGI printf
74 #define LOGD printf
75 #define LOGE printf
76 #define LOGW printf
78 #endif
80 #ifdef _WIN32
81 #include <windows.h>
82 #define dlopen( x, y ) LoadLibraryA( x )
83 #define dlsym( x, y ) ( void * ) GetProcAddress( (HINSTANCE)x, y )
84 #define dlclose( x ) FreeLibrary( (HINSTANCE)x )
85 #define GLLIB1 "opengl32.dll"
86 #define GLLIB2 "opengl32.dll"
87 #define GLLIB3 "opengl32.dll"
88 #else
89 #include <dlfcn.h>
90 #define GLLIB1 "libGLESv1_CM.so"
91 #define GLLIB2 "libGLESv2.so"
92 #define GLLIB3 "libEGL.so"
93 #endif
95 //#define GL_ENTRY(_r, _api, ...) #_api,
97 static char const *const gl_names[] =
98 {
99 #include "funcnames.h"
100 NULL
101 };
103 //const char * driver;
104 static void *glesLib = NULL;
105 GlESInterface *glEsImpl = NULL;
107 extern void InitGLStructs( );
109 void APIENTRY gl_unimplemented( GLenum none )
111 #ifndef USE_CORE_PROFILE
112 LOGE( "Called unimplemented OpenGL ES API\n" );
113 #endif
116 #ifdef XASH_SDL
117 #include "SDL.h"
118 #endif
120 void *nanoGL_GetProcAddress( const char *name )
122 void *addr = NULL;
123 #ifdef XASH_SDL
124 addr = SDL_GL_GetProcAddress( name );
125 if ( !addr )
126 #endif
127 addr = dlsym( glesLib, name );
128 return addr;
131 static int CreateGlEsInterface( const char *name, void *lib, void *lib1, void *default_func )
133 // alloc space
134 if ( !glEsImpl )
135 glEsImpl = (GlESInterface *)malloc( sizeof( GlESInterface ) );
137 if ( !glEsImpl )
139 return 0;
142 // load GL API calls
143 char const *const *api;
144 api = gl_names;
146 // nanoGL interface pointer
147 void **ptr = (void **)( glEsImpl );
149 while ( *api )
151 void *f;
153 f = dlsym( lib, *api ); // try libGLESxx_CM.so
155 #ifdef USE_CORE_PROFILE
156 // Hack: try ARB and EXT suffix
157 if ( f == NULL )
159 char namearb[256];
160 snprintf( namearb, 256, "%sARB", *api );
161 f = dlsym( lib, namearb );
163 if ( f == NULL )
165 char namearb[256];
166 snprintf( namearb, 256, "%sEXT", *api );
167 f = dlsym( lib, namearb );
169 #endif
170 if ( f == NULL )
172 LOGW( "<%s> not found in %s. Trying libEGL.so.", *api, name ); //driver);
174 // try lib1
175 if ( lib1 )
177 f = dlsym( lib1, *api ); // libEGL.so
179 if ( f == NULL )
181 LOGE( "<%s> not found in libEGL.so", *api );
182 if ( glEsImpl->eglGetProcAddress && ( (void *)glEsImpl->eglGetProcAddress != (void *)gl_unimplemented ) )
183 f = (void *)glEsImpl->eglGetProcAddress( *api );
184 if ( f == NULL )
185 f = (void *)default_func; //(void*)gl_unimplemented;
187 else
189 LOGD( "<%s> @ 0x%p\n", *api, f );
192 else
194 LOGE( "libEGL.so not loaded!" );
195 if ( glEsImpl->eglGetProcAddress && ( (void *)glEsImpl->eglGetProcAddress != (void *)gl_unimplemented ) )
196 f = (void *)glEsImpl->eglGetProcAddress( *api );
197 if ( !f )
198 f = (void *)default_func;
201 else
203 LOGD( "<%s> @ 0x%p\n", *api, f );
206 *ptr++ = f;
207 api++;
210 return 1;
213 // Load using the dynamic loader
214 static int loadDriver( const char *name )
216 glesLib = dlopen( name, RTLD_NOW | RTLD_LOCAL );
217 int rc = ( glesLib ) ? 1 : 0;
218 return rc;
221 /**
222 * Init
223 */
224 int nanoGL_Init( )
226 const char *lib1 = GLLIB1; // Has both gl* & egl* funcs SDK < 1.5
227 const char *lib2 = GLLIB2; // Only gl* funcs SDK >= 1.5
228 const char *lib3 = GLLIB3; // Only egl* funcs SDK >= 1.5
229 const char *driver;
231 // load lib
232 LOGI( "nanoGL: Init loading driver %s\n", lib1 );
233 //LOG (ANDROID_LOG_DEBUG, LOG_TAG, "nanoGL: Init loading driver %s\n", lib1);
235 if ( !loadDriver( lib1 ) )
237 LOGE( "Failed to load driver %s. Trying %s\n", lib1, lib2 );
239 if ( !loadDriver( lib2 ) )
241 LOGE( "Failed to load %s.\n", lib2 );
242 return 0;
244 else
245 driver = lib2;
247 else
248 driver = lib1;
250 void *eglLib;
252 //if ( strcmp(driver, lib2) == 0 ) {
253 LOGD( "**** Will Load EGL subs from %s ****", lib3 );
255 eglLib = dlopen( lib3, RTLD_NOW | RTLD_LOCAL );
257 if ( !eglLib )
259 LOGE( "Failed to load %s", lib3 );
261 //}
263 // Load API gl* for 1.5+ else egl* gl*
264 if ( !CreateGlEsInterface( driver, glesLib, eglLib, (void *)gl_unimplemented ) == -1 )
266 // release lib
267 LOGE( "CreateGlEsInterface failed." );
269 dlclose( glesLib );
270 return 0;
273 #ifdef __ANDROID__
274 // somewhy it does not initialize correctly
275 *( (void **)&glEsImpl->glGenFramebuffers ) = (void *)glEsImpl->eglGetProcAddress( "glGenFramebuffersOES" );
276 *( (void **)&glEsImpl->glGenRenderbuffers ) = (void *)glEsImpl->eglGetProcAddress( "glGenRenderbuffersOES" );
277 *( (void **)&glEsImpl->glRenderbufferStorage ) = (void *)glEsImpl->eglGetProcAddress( "glRenderbufferStorageOES" );
278 *( (void **)&glEsImpl->glBindFramebuffer ) = (void *)glEsImpl->eglGetProcAddress( "glBindFramebufferOES" );
279 *( (void **)&glEsImpl->glBindRenderbuffer ) = (void *)glEsImpl->eglGetProcAddress( "glBindRenderbufferOES" );
280 *( (void **)&glEsImpl->glFramebufferTexture2D ) = (void *)glEsImpl->eglGetProcAddress( "glFramebufferTexture2DOES" );
281 *( (void **)&glEsImpl->glDeleteRenderbuffers ) = (void *)glEsImpl->eglGetProcAddress( "glDeleteRenderbuffersOES" );
282 *( (void **)&glEsImpl->glDeleteFramebuffers ) = (void *)glEsImpl->eglGetProcAddress( "glDeleteFramebuffersOES" );
283 *( (void **)&glEsImpl->glFramebufferRenderbuffer ) = (void *)glEsImpl->eglGetProcAddress( "glFramebufferRenderbufferOES" );
284 #endif
286 // Init nanoGL
287 InitGLStructs( );
288 return 1;
291 void nanoGL_Destroy( )
293 LOGD( "nanoGL_Destroy" );
295 if ( glEsImpl )
297 free( glEsImpl );
298 glEsImpl = NULL;
301 // release lib
302 dlclose( glesLib );