GLESv2 sample working
[sdlhildon] / sdlgles / src / SDL_gles.c
1 /* This file is part of SDL_gles - SDL addon for OpenGL|ES
2  * Copyright (C) 2010 Javier S. Pedro
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 3 of the License, or (at your option) any later version.
8  *
9  * This library 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.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA or see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <dlfcn.h>
24 #include <assert.h>
25
26 #include <EGL/egl.h>
27
28 #include <SDL.h>
29 #include <SDL_syswm.h>
30
31 #include "SDL_gles.h"
32
33 typedef struct SDL_GLES_ContextPriv
34 {
35         SDL_GLES_Context p;
36
37         EGLConfig egl_config;
38         EGLContext egl_context;
39 } SDL_GLES_ContextPriv;
40
41 static const char * default_libgl[] = {
42         [SDL_GLES_VERSION_1_1] = "/usr/lib/libGLES_CM.so",
43         [SDL_GLES_VERSION_2_0] = "/usr/lib/libGLESv2.so"
44 };
45
46 static SDL_GLES_Version gl_version = SDL_GLES_VERSION_NONE;
47 static void* gl_handle = NULL;
48 static EGLint egl_major, egl_minor;
49
50 static Display *display = NULL;
51 static EGLDisplay *egl_display = EGL_NO_DISPLAY;
52 static EGLSurface egl_surface = EGL_NO_SURFACE;
53 static EGLint attrib_list[] = {
54         EGL_BUFFER_SIZE,                        0,
55         EGL_RED_SIZE,                           0,
56         EGL_GREEN_SIZE,                         0,
57         EGL_BLUE_SIZE,                          0,
58         EGL_LUMINANCE_SIZE,                     0,
59         EGL_ALPHA_SIZE,                         0,
60         EGL_CONFIG_CAVEAT,                      EGL_DONT_CARE,
61         EGL_CONFIG_ID,                          EGL_DONT_CARE,
62         EGL_DEPTH_SIZE,                         0,
63         EGL_LEVEL,                                      0,
64         EGL_NATIVE_RENDERABLE,          EGL_DONT_CARE,
65         EGL_NATIVE_VISUAL_TYPE,         EGL_DONT_CARE,
66         EGL_RENDERABLE_TYPE,            EGL_OPENGL_BIT,
67         EGL_SAMPLE_BUFFERS,                     0,
68         EGL_SAMPLES,                            0,
69         EGL_STENCIL_SIZE,                       0,
70         EGL_SURFACE_TYPE,                       EGL_WINDOW_BIT,
71         EGL_TRANSPARENT_TYPE,           EGL_NONE,
72         EGL_TRANSPARENT_RED_VALUE,      EGL_DONT_CARE,
73         EGL_TRANSPARENT_GREEN_VALUE,EGL_DONT_CARE,
74         EGL_TRANSPARENT_BLUE_VALUE,     EGL_DONT_CARE,
75         EGL_NONE
76 };
77 static EGLint context_attrib_list[] = {
78         EGL_CONTEXT_CLIENT_VERSION,     1,
79         EGL_NONE
80 };
81 static const int attrib_list_size = (sizeof(attrib_list) / sizeof(EGLint)) / 2;
82 static const int context_attrib_list_size = (sizeof(context_attrib_list) / sizeof(EGLint)) / 2;
83 static SDL_GLES_ContextPriv *cur_context = NULL;
84
85 static const char * get_error_string(int error) {
86         switch (error) {
87                 case EGL_SUCCESS:
88                         return "EGL_SUCCESS";
89                 case EGL_NOT_INITIALIZED:
90                         return "EGL_NOT_INITIALIZED";
91                 case EGL_BAD_ACCESS:
92                         return "EGL_BAD_ACCESS";
93                 case EGL_BAD_ALLOC:
94                         return "EGL_BAD_ALLOC";
95                 case EGL_BAD_ATTRIBUTE:
96                         return "EGL_BAD_ATTRIBUTE";
97                 case EGL_BAD_CONFIG:
98                         return "EGL_BAD_CONFIG";
99                 case EGL_BAD_CONTEXT:
100                         return "EGL_BAD_CONTEXT";
101                 case EGL_BAD_CURRENT_SURFACE:
102                         return "EGL_BAD_CURRENT_SURFACE";
103                 case EGL_BAD_DISPLAY:
104                         return "EGL_BAD_DISPLAY";
105                 case EGL_BAD_MATCH:
106                         return "EGL_BAD_MATCH";
107                 case EGL_BAD_NATIVE_PIXMAP:
108                         return "EGL_BAD_NATIVE_PIXMAP";
109                 case EGL_BAD_NATIVE_WINDOW:
110                         return "EGL_BAD_NATIVE_WINDOW";
111                 case EGL_BAD_PARAMETER:
112                         return "EGL_BAD_PARAMETER";
113                 case EGL_BAD_SURFACE:
114                         return "EGL_BAD_SURFACE";
115                 case EGL_CONTEXT_LOST:
116                         return "EGL_CONTEXT_LOST";
117                 default:
118                         return "EGL_UNKNOWN_ERROR";
119     }
120 }
121
122 static int set_egl_attrib(EGLenum attrib, EGLint value)
123 {
124         const EGLint a = attrib;
125         int i;
126         for (i = 0; i < attrib_list_size; i++) {
127                 if (attrib_list[i * 2] == a) {
128                         attrib_list[(i*2)+1] = value;
129                         return 0;
130                 }
131         }
132
133         return -1;
134 }
135
136 static EGLint get_egl_attrib(EGLenum attrib)
137 {
138         const EGLint a = attrib;
139         int i;
140         for (i = 0; i < attrib_list_size; i++) {
141                 if (attrib_list[i * 2] == a) {
142                         return attrib_list[(i*2)+1];
143                 }
144         }
145
146         return -1;
147 }
148
149 static int set_egl_context_attrib(EGLenum attrib, EGLint value)
150 {
151         const EGLint a = attrib;
152         int i;
153         for (i = 0; i < context_attrib_list_size; i++) {
154                 if (context_attrib_list[i * 2] == a) {
155                         context_attrib_list[(i*2)+1] = value;
156                         return 0;
157                 }
158         }
159
160         return -1;
161 }
162
163 int SDL_GLES_LoadLibrary(const char *path)
164 {
165         if (!path) {
166                 path = getenv("SDL_VIDEO_GL_DRIVER");
167                 if (!path) {
168                         switch (gl_version) {
169                                 case SDL_GLES_VERSION_1_1:
170                                 case SDL_GLES_VERSION_2_0:
171                                         path = default_libgl[gl_version];
172                                 break;
173                                 default:
174                                         SDL_SetError("No GL version specific and SDL_VIDEO_GL_DRIVER set");
175                                         return -1;
176                         }
177                 }
178         }
179
180         /* Dynamically load the desired GL library */
181         gl_handle = dlopen(path, RTLD_LAZY|RTLD_GLOBAL);
182         if (!gl_handle) {
183                 SDL_SetError("Failed to open GL library: %s (%s)", path, dlerror());
184                 return -2;
185         }
186
187         return 0;
188 }
189
190 void* SDL_GLES_GetProcAddress(const char *proc)
191 {
192         if (!gl_handle) return NULL;
193         return dlsym(gl_handle, proc);
194 }
195
196 int SDL_GLES_Init(SDL_GLES_Version version)
197 {
198         SDL_SysWMinfo info;
199         EGLBoolean res;
200
201         SDL_VERSION(&info.version);
202         if (SDL_GetWMInfo(&info) != 1) {
203                 SDL_SetError("SDL_gles is incompatible with this SDL version");
204                 return -1;
205         }
206
207         display = info.info.x11.gfxdisplay;
208
209         egl_display = eglGetDisplay((EGLNativeDisplayType)display);
210         if (egl_display == EGL_NO_DISPLAY) {
211                 SDL_SetError("EGL found no available displays");
212                 return -2;
213         }
214
215         res = eglInitialize(egl_display, &egl_major, &egl_minor);
216         if (!res) {
217                 SDL_SetError("EGL failed to initialize: %s",
218                         get_error_string(eglGetError()));
219                 return -2;
220         }
221
222         gl_version = version;
223         switch (gl_version) {
224                 case SDL_GLES_VERSION_1_1:
225                         /* defaults are OK */
226                         break;
227                 case SDL_GLES_VERSION_2_0:
228                         /* by default, set egl renderable type attribute to GL ES 2 */
229                         res = set_egl_attrib(EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT) == 0;
230                         assert(res);
231                         /* and request GL ES 2.0 contexts */
232                         res = set_egl_context_attrib(EGL_CONTEXT_CLIENT_VERSION, 2) == 0;
233                         assert(res);
234                         break;
235                 default:
236                         break;
237         }
238
239         return 0;
240 }
241
242 void SDL_GLES_Quit()
243 {
244         if (gl_handle) {
245                 dlclose(gl_handle);
246                 gl_handle = NULL;
247         }
248         if (egl_display != EGL_NO_DISPLAY) {
249                 eglTerminate(egl_display);
250                 egl_display = EGL_NO_DISPLAY;
251         }
252 }
253
254 int SDL_GLES_SetVideoMode()
255 {
256         SDL_SysWMinfo info;
257         EGLBoolean res;
258
259         SDL_VERSION(&info.version);
260         if (SDL_GetWMInfo(&info) != 1) {
261                 SDL_SetError("SDL_gles is incompatible with this SDL version");
262                 return -1;
263         }
264
265         /* Destroy previous surface, if any. */
266         if (egl_surface != EGL_NO_SURFACE) {
267                 /* Ensure the surface is not the current one,
268                  * thus freeing memory earlier. */
269                 eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
270                          EGL_NO_CONTEXT);
271                 eglDestroySurface(egl_display, egl_surface);
272                 egl_surface = EGL_NO_SURFACE;
273         }
274
275         /* No current context? Quietly defer surface creation.
276          * Surface will be created on the call to MakeCurrent. */
277         if (!cur_context) {
278                 return 0;
279         }
280
281         /* Create the new window surface. */
282         egl_surface = eglCreateWindowSurface(egl_display, cur_context->egl_config,
283                 (EGLNativeWindowType)info.info.x11.window, NULL);
284         if (egl_surface == EGL_NO_SURFACE) {
285                 SDL_SetError("EGL failed to create a window surface: %s",
286                         get_error_string(eglGetError()));
287                 return -2;
288         }
289
290         /* New surface created. Make it active. */
291         assert(cur_context && cur_context->egl_context != EGL_NO_CONTEXT);
292         res = eglMakeCurrent(egl_display, egl_surface, egl_surface,
293                 cur_context->egl_context);
294
295         if (!res) {
296                 SDL_SetError("EGL failed to change current surface: %s",
297                         get_error_string(eglGetError()));
298                 cur_context = NULL;
299                 return -2;
300         }
301
302         return 0;
303 }
304
305 SDL_GLES_Context* SDL_GLES_CreateContext(void)
306 {
307         SDL_GLES_ContextPriv *context = malloc(sizeof(SDL_GLES_ContextPriv));
308         if (!context) {
309                 SDL_Error(SDL_ENOMEM);
310                 return NULL;
311         }
312
313         EGLBoolean res;
314         EGLConfig configs[1];
315         EGLint num_config;
316
317         res = eglChooseConfig(egl_display, attrib_list, configs, 1, &num_config);
318         if (!res || num_config < 1) {
319                 SDL_SetError("EGL failed to find any valid config with required attributes: %s",
320                         get_error_string(eglGetError()));
321                 free(context);
322                 return NULL;
323         }
324
325         context->egl_config = configs[0];
326         context->egl_context = eglCreateContext(egl_display, configs[0],
327                 EGL_NO_CONTEXT, context_attrib_list);
328         if (context->egl_context == EGL_NO_CONTEXT) {
329                 SDL_SetError("EGL failed to create context: %s",
330                         get_error_string(eglGetError()));
331                 free(context);
332                 return NULL;
333         }
334
335         return (SDL_GLES_Context*) context;
336 }
337
338 void SDL_GLES_DeleteContext(SDL_GLES_Context* c)
339 {
340         SDL_GLES_ContextPriv *context = (SDL_GLES_ContextPriv*)c;
341         if (!context) return;
342
343         if (cur_context == context) {
344                 /* Deleting the active context */
345                 SDL_GLES_MakeCurrent(NULL);
346         }
347
348         eglDestroyContext(egl_display, context->egl_context);
349         free(context);
350 }
351
352 int SDL_GLES_MakeCurrent(SDL_GLES_Context* c)
353 {
354         SDL_GLES_ContextPriv *context = (SDL_GLES_ContextPriv*)c;
355         int res;
356
357         cur_context = context;
358
359         /* SDL_GLES_SetVideoMode() will appropiately clear the current context
360          * (and surface), then create a new surface matching the selected context
361          * config and make both the surface and the context the active ones. */
362         res = SDL_GLES_SetVideoMode();
363         if (res != 0) return res; /* Surface (re-)creation failed. */
364
365         /* TODO Update attrib_list. Make SDL_GLES_GetAttribute work. */
366
367         if (cur_context) {
368                 /* If selecting a new context, bind the required API. */
369                 switch (gl_version) {
370                         case SDL_GLES_VERSION_1_1:
371                         case SDL_GLES_VERSION_2_0:
372                                 res = eglBindAPI(EGL_OPENGL_ES_API);
373                                 if (!res) {
374                                         SDL_SetError("EGL failed to bind the required API: %s",
375                                                 get_error_string(eglGetError()));
376                                         return -2;
377                                 }
378                                 break;
379                         default:
380                                 break;
381                 }
382         }
383
384         return 0;
385 }
386
387 void SDL_GLES_SwapBuffers()
388 {
389         eglSwapBuffers(egl_display, egl_surface);
390 }
391