DEADSOFTWARE

Initial version
[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>
28 #include <android/log.h>
30 #include "nanogl.h"
31 #include "glesinterface.h"
32 #include "gl.h"
34 #define LOG __android_log_print
35 #define DEBUG_NANO 0
37 #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
38 #define LOGD(...) if (DEBUG_NANO) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
39 #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG,__VA_ARGS__)
40 #define LOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG,__VA_ARGS__)
42 #define GL_ENTRY(_r, _api, ...) #_api,
44 static char const * const gl_names[] = {
45 #include "gl_entries.in"
46 NULL
47 };
49 //const char * driver;
51 static void* glesLib = NULL;
53 GlESInterface* glEsImpl = NULL;
55 extern void InitGLStructs();
57 static void gl_unimplemented() {
58 LOGE ("Called unimplemented OpenGL ES API\n");
59 }
62 static int CreateGlEsInterface( const char * name, void * lib, void * lib1, void * default_func )
63 {
64 // alloc space
65 if ( !glEsImpl )
66 glEsImpl = (GlESInterface *) malloc(sizeof(GlESInterface));
68 if (!glEsImpl) {
69 return 0;
70 }
72 // load GL API calls
73 char const * const * api;
74 api = gl_names;
76 // nanoGL interface pointer
77 void ** ptr = (void **)(glEsImpl);
79 while (*api)
80 {
81 void * f;
83 f = dlsym(lib, *api); // ltry ibGLESxx_CM.so
85 if (f == NULL) {
86 LOGW( "<%s> not found in %s. Trying libEGL.so.", *api, name); //driver);
88 // try lib1
89 if ( lib1 ) {
90 f = dlsym(lib1, *api); // libEGL.so
92 if ( f == NULL ) {
93 LOGE ( "<%s> not found in libEGL.so", *api);
94 f = default_func; //(void*)gl_unimplemented;
95 }
96 else {
97 LOGD ("<%s> @ 0x%p\n", *api, f);
98 }
99 }
100 else
101 f = default_func;
103 else {
104 LOGD ("<%s> @ 0x%p\n", *api, f);
107 *ptr++ = f;
108 api++;
111 return 1;
114 // Load using the dynamic loader
115 static int loadDriver(const char * name) {
116 glesLib = dlopen(name, RTLD_NOW | RTLD_LOCAL);
117 int rc = (glesLib) ? 1 : 0;
118 return rc;
121 /**
122 * Init
123 */
124 int nanoGL_Init()
126 const char * lib1 = "libGLESv1_CM.so"; // Has both gl* & egl* funcs SDK < 1.5
127 const char * lib2 = "libGLESv2.so"; // Only gl* funcs SDK >= 1.5
128 const char * lib3 = "libEGL.so"; // Only egl* funcs SDK >= 1.5
129 const char * driver;
131 // load lib
132 LOGI("nanoGL: Init loading driver %s\n", lib1);
133 //LOG (ANDROID_LOG_DEBUG, LOG_TAG, "nanoGL: Init loading driver %s\n", lib1);
135 if ( ! loadDriver(lib1) )
137 LOGE("Failed to load driver %s. Trying %s\n", lib1, lib2);
139 if ( ! loadDriver(lib2) ) {
140 LOGE ("Failed to load %s.\n", lib2);
141 return 0;
143 else
144 driver = lib2;
146 else
147 driver = lib1;
149 void * eglLib;
151 //if ( strcmp(driver, lib2) == 0 ) {
152 LOGD ("**** Will Load EGL subs from %s ****", lib3);
154 eglLib = dlopen(lib3, RTLD_NOW | RTLD_LOCAL);
156 if ( ! eglLib ) {
157 LOGE ( "Failed to load %s", lib3);
159 //}
161 // Load API gl* for 1.5+ else egl* gl*
162 //if (CreateGlEsInterface(driver, glesLib, eglLib, NULL) == -1)
163 if ( !CreateGlEsInterface(driver, glesLib, eglLib, (void *) gl_unimplemented) == -1)
165 // release lib
166 LOGE ( "CreateGlEsInterface failed.");
168 dlclose(glesLib);
169 return 0;
172 // Init nanoGL
173 InitGLStructs();
174 return 1;
177 void nanoGL_Destroy()
179 LOGD ("nanoGL_Destroy");
181 if (glEsImpl) {
182 free( glEsImpl);
183 glEsImpl = NULL;
186 // release lib
187 dlclose(glesLib);