SDL 1.3 is now under the zlib license.
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
22 #include "SDL_DirectFB_video.h"
24 #if SDL_DIRECTFB_OPENGL
26 #include "SDL_DirectFB_opengl.h"
27 #include "SDL_DirectFB_window.h"
29 #include <directfbgl.h>
30 #include "SDL_loadso.h"
33 #if SDL_DIRECTFB_OPENGL
35 struct SDL_GLDriverData
37 int gl_active; /* to stop switching drivers while we have a valid context */
39 DirectFB_GLContext *firstgl; /* linked list */
42 void (*glFinish) (void);
43 void (*glFlush) (void);
46 #define OPENGL_REQUIRS_DLOPEN
47 #if defined(OPENGL_REQUIRS_DLOPEN) && defined(SDL_LOADSO_DLOPEN)
49 #define GL_LoadObject(X) dlopen(X, (RTLD_NOW|RTLD_GLOBAL))
50 #define GL_LoadFunction dlsym
51 #define GL_UnloadObject dlclose
53 #define GL_LoadObject SDL_LoadObject
54 #define GL_LoadFunction SDL_LoadFunction
55 #define GL_UnloadObject SDL_UnloadObject
58 static void DirectFB_GL_UnloadLibrary(_THIS);
61 DirectFB_GL_Initialize(_THIS)
68 (struct SDL_GLDriverData *) SDL_calloc(1,
71 if (!_this->gl_data) {
75 _this->gl_data->initialized = 0;
77 ++_this->gl_data->initialized;
78 _this->gl_data->firstgl = NULL;
80 if (DirectFB_GL_LoadLibrary(_this, NULL) < 0) {
84 /* Initialize extensions */
86 * X11_GL_InitExtensions(_this);
93 DirectFB_GL_Shutdown(_THIS)
95 if (!_this->gl_data || (--_this->gl_data->initialized > 0)) {
99 DirectFB_GL_UnloadLibrary(_this);
101 SDL_free(_this->gl_data);
102 _this->gl_data = NULL;
106 DirectFB_GL_LoadLibrary(_THIS, const char *path)
108 //SDL_DFB_DEVICEDATA(_this);
112 SDL_DFB_DEBUG("Loadlibrary : %s\n", path);
114 if (_this->gl_data->gl_active) {
115 SDL_SetError("OpenGL context already created");
121 path = SDL_getenv("SDL_VIDEO_GL_DRIVER");
127 handle = GL_LoadObject(path);
128 if (handle == NULL) {
129 SDL_DFB_ERR("Library not found: %s\n", path);
130 /* SDL_LoadObject() will call SDL_SetError() for us. */
134 SDL_DFB_DEBUG("Loaded library: %s\n", path);
136 _this->gl_config.dll_handle = handle;
137 _this->gl_config.driver_loaded = 1;
139 SDL_strlcpy(_this->gl_config.driver_path, path,
140 SDL_arraysize(_this->gl_config.driver_path));
142 *_this->gl_config.driver_path = '\0';
145 _this->gl_data->glFinish = DirectFB_GL_GetProcAddress(_this, "glFinish");
146 _this->gl_data->glFlush = DirectFB_GL_GetProcAddress(_this, "glFlush");
152 DirectFB_GL_UnloadLibrary(_THIS)
157 if (_this->gl_config.driver_loaded) {
159 ret = GL_UnloadObject(_this->gl_config.dll_handle);
161 SDL_DFB_ERR("Error #%d trying to unload library.\n", ret);
162 _this->gl_config.dll_handle = NULL;
163 _this->gl_config.driver_loaded = 0;
166 /* Free OpenGL memory */
167 SDL_free(_this->gl_data);
168 _this->gl_data = NULL;
172 DirectFB_GL_GetProcAddress(_THIS, const char *proc)
176 handle = _this->gl_config.dll_handle;
177 return GL_LoadFunction(handle, proc);
181 DirectFB_GL_CreateContext(_THIS, SDL_Window * window)
183 //SDL_DFB_DEVICEDATA(_this);
184 SDL_DFB_WINDOWDATA(window);
185 DirectFB_GLContext *context;
187 SDL_DFB_ALLOC_CLEAR(context, sizeof(DirectFB_GLContext));
189 SDL_DFB_CHECKERR(windata->surface->GetGL(windata->surface,
192 if (!context->context)
195 context->is_locked = 0;
196 context->sdl_window = window;
198 context->next = _this->gl_data->firstgl;
199 _this->gl_data->firstgl = context;
201 SDL_DFB_CHECK(context->context->Unlock(context->context));
203 if (DirectFB_GL_MakeCurrent(_this, window, context) < 0) {
204 DirectFB_GL_DeleteContext(_this, context);
215 DirectFB_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
217 //SDL_DFB_WINDOWDATA(window);
218 DirectFB_GLContext *ctx = (DirectFB_GLContext *) context;
219 DirectFB_GLContext *p;
221 for (p = _this->gl_data->firstgl; p; p = p->next)
224 SDL_DFB_CHECKERR(p->context->Unlock(p->context));
231 SDL_DFB_CHECKERR(ctx->context->Lock(ctx->context));
241 DirectFB_GL_SetSwapInterval(_THIS, int interval)
248 DirectFB_GL_GetSwapInterval(_THIS)
255 DirectFB_GL_SwapWindow(_THIS, SDL_Window * window)
257 //SDL_DFB_DEVICEDATA(_this);
258 SDL_DFB_WINDOWDATA(window);
260 DirectFB_GLContext *p;
264 region.x2 = window->w;
265 region.y2 = window->h;
268 if (devdata->glFinish)
270 else if (devdata->glFlush)
274 for (p = _this->gl_data->firstgl; p != NULL; p = p->next)
275 if (p->sdl_window == window && p->is_locked)
277 SDL_DFB_CHECKERR(p->context->Unlock(p->context));
281 SDL_DFB_CHECKERR(windata->window_surface->Flip(windata->window_surface,NULL, DSFLIP_PIPELINE |DSFLIP_BLIT | DSFLIP_ONSYNC ));
283 //if (windata->gl_context) {
284 //SDL_DFB_CHECKERR(windata->surface->Flip(windata->surface,NULL, DSFLIP_ONSYNC));
285 //SDL_DFB_CHECKERR(windata->gl_context->context->Lock(windata->gl_context->context));
294 DirectFB_GL_DeleteContext(_THIS, SDL_GLContext context)
296 DirectFB_GLContext *ctx = (DirectFB_GLContext *) context;
297 DirectFB_GLContext *p;
300 SDL_DFB_CHECK(ctx->context->Unlock(ctx->context));
301 SDL_DFB_RELEASE(ctx->context);
303 for (p = _this->gl_data->firstgl; p && p->next != ctx; p = p->next)
308 _this->gl_data->firstgl = ctx->next;
314 DirectFB_GL_FreeWindowContexts(_THIS, SDL_Window * window)
316 DirectFB_GLContext *p;
318 for (p = _this->gl_data->firstgl; p != NULL; p = p->next)
319 if (p->sdl_window == window)
322 SDL_DFB_CHECK(p->context->Unlock(p->context));
323 SDL_DFB_RELEASE(p->context);
328 DirectFB_GL_ReAllocWindowContexts(_THIS, SDL_Window * window)
330 DirectFB_GLContext *p;
332 for (p = _this->gl_data->firstgl; p != NULL; p = p->next)
333 if (p->sdl_window == window)
335 SDL_DFB_WINDOWDATA(window);
336 SDL_DFB_CHECK(windata->surface->GetGL(windata->surface,
339 SDL_DFB_CHECK(p->context->Lock(p->context));
344 DirectFB_GL_DestroyWindowContexts(_THIS, SDL_Window * window)
346 DirectFB_GLContext *p;
348 for (p = _this->gl_data->firstgl; p != NULL; p = p->next)
349 if (p->sdl_window == window)
350 DirectFB_GL_DeleteContext(_this, p);
355 /* vi: set ts=4 sw=4 expandtab: */