DEADSOFTWARE

Fix typo in nanoGL_GetProcAddress, return 1 in nanoGL_Init() if wrapper already initi...
[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>
25 #include <dlfcn.h>
27 //#include <cutils/log.h>
29 #include "nanogl.h"
30 #include "glesinterface.h"
31 #include "gl.h"
34 #define DEBUG_NANO 0
36 #ifdef __ANDROID__
37 #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 LOGD(...) if (DEBUG_NANO) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
42 #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG,__VA_ARGS__)
43 #define LOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG,__VA_ARGS__)
44 #else
46 #define LOGI(...) printf("I: "__VA_ARGS__)
47 #define LOGD(...) if(DEBUG_NANO) printf("D: "__VA_ARGS__)
48 #define LOGE(...) printf("E: "__VA_ARGS__)
49 #define LOGW(...) printf("W: "__VA_ARGS__)
51 #endif
54 #define GL_ENTRY(_r, _api, ...) #_api,
56 static char const * const gl_names[] = {
57 #include "gl_entries.in"
58 NULL
59 };
61 //const char * driver;
63 static void* glesLib = NULL;
65 GlESInterface* glEsImpl = NULL;
67 int initialized = 0;
69 extern void InitGLStructs();
71 static void gl_unimplemented() {
72 LOGE ("Called unimplemented OpenGL ES API\n");
73 }
75 void *nanoGL_GetProcAddress(const char *procname)
76 {
77 #if defined(__MULTITEXTURE_SUPPORT__)
78 if (!strcmp(procname, "glMultiTexCoord2fARB"))
79 {
80 return (void*)&glMultiTexCoord2fARB;
81 }
82 else if (!strcmp(procname, "glActiveTextureARB"))
83 {
84 return (void*)&glActiveTexture;
85 }
86 else if (!strcmp(procname, "glClientActiveTextureARB"))
87 {
88 return (void*)&glClientActiveTexture;
89 }
90 #endif
91 return dlsym(glesLib, procname);
92 }
94 static int CreateGlEsInterface( const char * name, void * lib, void * lib1, void * default_func )
95 {
96 // alloc space
97 if ( !glEsImpl )
98 glEsImpl = (GlESInterface *) malloc(sizeof(GlESInterface));
100 if (!glEsImpl) {
101 return 0;
104 // load GL API calls
105 char const * const * api;
106 api = gl_names;
108 // nanoGL interface pointer
109 void ** ptr = (void **)(glEsImpl);
111 while (*api)
113 void * f;
115 f = dlsym(lib, *api); // try libGLESxx_CM.so
117 if (f == NULL) {
118 LOGW( "<%s> not found in %s. Trying libEGL.so.", *api, name); //driver);
120 // try lib1
121 if ( lib1 ) {
122 f = dlsym(lib1, *api); // libEGL.so
124 if ( f == NULL ) {
125 LOGE ( "<%s> not found in libEGL.so", *api);
126 f = default_func; //(void*)gl_unimplemented;
128 else {
129 LOGD ("<%s> @ 0x%p\n", *api, f);
132 else
133 f = default_func;
135 else {
136 LOGD ("<%s> @ 0x%p\n", *api, f);
139 *ptr++ = f;
140 api++;
143 return 1;
146 // Load using the dynamic loader
147 static int loadDriver(const char * name) {
148 glesLib = dlopen(name, RTLD_NOW | RTLD_LOCAL);
149 int rc = (glesLib) ? 1 : 0;
150 return rc;
153 /**
154 * Init
155 */
156 int nanoGL_Init()
158 if( initialized ) return 1;
160 const char * lib1 = "libGLESv1_CM.so"; // Has both gl* & egl* funcs SDK < 1.5
161 const char * lib2 = "libGLESv2.so"; // Only gl* funcs SDK >= 1.5
162 const char * lib3 = "libEGL.so"; // Only egl* funcs SDK >= 1.5
163 const char * driver;
165 // load lib
166 LOGI("nanoGL: Init loading driver %s\n", lib1);
167 //LOG (ANDROID_LOG_DEBUG, LOG_TAG, "nanoGL: Init loading driver %s\n", lib1);
169 if ( ! loadDriver(lib1) )
171 LOGE("Failed to load driver %s. Trying %s\n", lib1, lib2);
173 if ( ! loadDriver(lib2) ) {
174 LOGE ("Failed to load %s.\n", lib2);
175 return 0;
177 else
178 driver = lib2;
180 else
181 driver = lib1;
183 void * eglLib;
185 //if ( strcmp(driver, lib2) == 0 ) {
186 LOGD ("**** Will Load EGL subs from %s ****", lib3);
188 eglLib = dlopen(lib3, RTLD_NOW | RTLD_LOCAL);
190 if ( ! eglLib ) {
191 LOGE ( "Failed to load %s", lib3);
193 //}
195 // Load API gl* for 1.5+ else egl* gl*
196 //if (CreateGlEsInterface(driver, glesLib, eglLib, NULL) == -1)
197 if ( !CreateGlEsInterface(driver, glesLib, eglLib, (void *) gl_unimplemented) == -1)
199 // release lib
200 LOGE ( "CreateGlEsInterface failed.");
202 dlclose(glesLib);
203 return 0;
206 // Init nanoGL
207 InitGLStructs();
208 initialized = 1;
209 return 1;
212 void nanoGL_Destroy()
214 LOGD ("nanoGL_Destroy");
216 if (glEsImpl) {
217 free( glEsImpl);
218 glEsImpl = NULL;
221 // release lib
222 dlclose(glesLib);