2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2011 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 SDL1.3 DirectFB driver by couriersud@arcor.de
26 #include "SDL_DirectFB_video.h"
28 #if SDL_DIRECTFB_OPENGL
30 #include "SDL_DirectFB_opengl.h"
31 #include "SDL_DirectFB_window.h"
33 #include <directfbgl.h>
34 #include "SDL_loadso.h"
37 #if SDL_DIRECTFB_OPENGL
39 struct SDL_GLDriverData
41 int gl_active; /* to stop switching drivers while we have a valid context */
43 DirectFB_GLContext *firstgl; /* linked list */
46 void (*glFinish) (void);
47 void (*glFlush) (void);
50 #define OPENGL_REQUIRS_DLOPEN
51 #if defined(OPENGL_REQUIRS_DLOPEN) && defined(SDL_LOADSO_DLOPEN)
53 #define GL_LoadObject(X) dlopen(X, (RTLD_NOW|RTLD_GLOBAL))
54 #define GL_LoadFunction dlsym
55 #define GL_UnloadObject dlclose
57 #define GL_LoadObject SDL_LoadObject
58 #define GL_LoadFunction SDL_LoadFunction
59 #define GL_UnloadObject SDL_UnloadObject
62 static void DirectFB_GL_UnloadLibrary(_THIS);
65 DirectFB_GL_Initialize(_THIS)
72 (struct SDL_GLDriverData *) SDL_calloc(1,
75 if (!_this->gl_data) {
79 _this->gl_data->initialized = 0;
81 ++_this->gl_data->initialized;
82 _this->gl_data->firstgl = NULL;
84 if (DirectFB_GL_LoadLibrary(_this, NULL) < 0) {
88 /* Initialize extensions */
90 * X11_GL_InitExtensions(_this);
97 DirectFB_GL_Shutdown(_THIS)
99 if (!_this->gl_data || (--_this->gl_data->initialized > 0)) {
103 DirectFB_GL_UnloadLibrary(_this);
105 SDL_free(_this->gl_data);
106 _this->gl_data = NULL;
110 DirectFB_GL_LoadLibrary(_THIS, const char *path)
112 //SDL_DFB_DEVICEDATA(_this);
116 SDL_DFB_DEBUG("Loadlibrary : %s\n", path);
118 if (_this->gl_data->gl_active) {
119 SDL_SetError("OpenGL context already created");
125 path = SDL_getenv("SDL_VIDEO_GL_DRIVER");
131 handle = GL_LoadObject(path);
132 if (handle == NULL) {
133 SDL_DFB_ERR("Library not found: %s\n", path);
134 /* SDL_LoadObject() will call SDL_SetError() for us. */
138 SDL_DFB_DEBUG("Loaded library: %s\n", path);
140 _this->gl_config.dll_handle = handle;
141 _this->gl_config.driver_loaded = 1;
143 SDL_strlcpy(_this->gl_config.driver_path, path,
144 SDL_arraysize(_this->gl_config.driver_path));
146 *_this->gl_config.driver_path = '\0';
149 _this->gl_data->glFinish = DirectFB_GL_GetProcAddress(_this, "glFinish");
150 _this->gl_data->glFlush = DirectFB_GL_GetProcAddress(_this, "glFlush");
156 DirectFB_GL_UnloadLibrary(_THIS)
161 if (_this->gl_config.driver_loaded) {
163 ret = GL_UnloadObject(_this->gl_config.dll_handle);
165 SDL_DFB_ERR("Error #%d trying to unload library.\n", ret);
166 _this->gl_config.dll_handle = NULL;
167 _this->gl_config.driver_loaded = 0;
170 /* Free OpenGL memory */
171 SDL_free(_this->gl_data);
172 _this->gl_data = NULL;
176 DirectFB_GL_GetProcAddress(_THIS, const char *proc)
180 handle = _this->gl_config.dll_handle;
181 return GL_LoadFunction(handle, proc);
185 DirectFB_GL_CreateContext(_THIS, SDL_Window * window)
187 //SDL_DFB_DEVICEDATA(_this);
188 SDL_DFB_WINDOWDATA(window);
189 DirectFB_GLContext *context;
191 SDL_DFB_ALLOC_CLEAR(context, sizeof(DirectFB_GLContext));
193 SDL_DFB_CHECKERR(windata->surface->GetGL(windata->surface,
196 if (!context->context)
199 context->is_locked = 0;
200 context->sdl_window = window;
202 context->next = _this->gl_data->firstgl;
203 _this->gl_data->firstgl = context;
205 SDL_DFB_CHECK(context->context->Unlock(context->context));
207 if (DirectFB_GL_MakeCurrent(_this, window, context) < 0) {
208 DirectFB_GL_DeleteContext(_this, context);
219 DirectFB_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
221 //SDL_DFB_WINDOWDATA(window);
222 DirectFB_GLContext *ctx = (DirectFB_GLContext *) context;
223 DirectFB_GLContext *p;
225 for (p = _this->gl_data->firstgl; p; p = p->next)
228 SDL_DFB_CHECKERR(p->context->Unlock(p->context));
235 SDL_DFB_CHECKERR(ctx->context->Lock(ctx->context));
245 DirectFB_GL_SetSwapInterval(_THIS, int interval)
252 DirectFB_GL_GetSwapInterval(_THIS)
259 DirectFB_GL_SwapWindow(_THIS, SDL_Window * window)
261 //SDL_DFB_DEVICEDATA(_this);
262 SDL_DFB_WINDOWDATA(window);
264 DirectFB_GLContext *p;
268 region.x2 = window->w;
269 region.y2 = window->h;
272 if (devdata->glFinish)
274 else if (devdata->glFlush)
278 for (p = _this->gl_data->firstgl; p != NULL; p = p->next)
279 if (p->sdl_window == window && p->is_locked)
281 SDL_DFB_CHECKERR(p->context->Unlock(p->context));
285 SDL_DFB_CHECKERR(windata->window_surface->Flip(windata->window_surface,NULL, DSFLIP_PIPELINE |DSFLIP_BLIT | DSFLIP_ONSYNC ));
287 //if (windata->gl_context) {
288 //SDL_DFB_CHECKERR(windata->surface->Flip(windata->surface,NULL, DSFLIP_ONSYNC));
289 //SDL_DFB_CHECKERR(windata->gl_context->context->Lock(windata->gl_context->context));
298 DirectFB_GL_DeleteContext(_THIS, SDL_GLContext context)
300 DirectFB_GLContext *ctx = (DirectFB_GLContext *) context;
301 DirectFB_GLContext *p;
304 SDL_DFB_CHECK(ctx->context->Unlock(ctx->context));
305 SDL_DFB_RELEASE(ctx->context);
307 for (p = _this->gl_data->firstgl; p && p->next != ctx; p = p->next)
312 _this->gl_data->firstgl = ctx->next;
318 DirectFB_GL_FreeWindowContexts(_THIS, SDL_Window * window)
320 DirectFB_GLContext *p;
322 for (p = _this->gl_data->firstgl; p != NULL; p = p->next)
323 if (p->sdl_window == window)
326 SDL_DFB_CHECK(p->context->Unlock(p->context));
327 SDL_DFB_RELEASE(p->context);
332 DirectFB_GL_ReAllocWindowContexts(_THIS, SDL_Window * window)
334 DirectFB_GLContext *p;
336 for (p = _this->gl_data->firstgl; p != NULL; p = p->next)
337 if (p->sdl_window == window)
339 SDL_DFB_WINDOWDATA(window);
340 SDL_DFB_CHECK(windata->surface->GetGL(windata->surface,
343 SDL_DFB_CHECK(p->context->Lock(p->context));
348 DirectFB_GL_DestroyWindowContexts(_THIS, SDL_Window * window)
350 DirectFB_GLContext *p;
352 for (p = _this->gl_data->firstgl; p != NULL; p = p->next)
353 if (p->sdl_window == window)
354 DirectFB_GL_DeleteContext(_this, p);
359 /* vi: set ts=4 sw=4 expandtab: */