2 Simple DirectMedia Layer
3 Copyright (C) 1997-2012 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.
21 #include "SDL_config.h"
23 #if SDL_VIDEO_DRIVER_DIRECTFB
25 #include "SDL_DirectFB_video.h"
27 #if SDL_DIRECTFB_OPENGL
29 #include "SDL_DirectFB_opengl.h"
30 #include "SDL_DirectFB_window.h"
32 #include <directfbgl.h>
33 #include "SDL_loadso.h"
36 #if SDL_DIRECTFB_OPENGL
38 struct SDL_GLDriverData
40 int gl_active; /* to stop switching drivers while we have a valid context */
42 DirectFB_GLContext *firstgl; /* linked list */
45 void (*glFinish) (void);
46 void (*glFlush) (void);
49 #define OPENGL_REQUIRS_DLOPEN
50 #if defined(OPENGL_REQUIRS_DLOPEN) && defined(SDL_LOADSO_DLOPEN)
52 #define GL_LoadObject(X) dlopen(X, (RTLD_NOW|RTLD_GLOBAL))
53 #define GL_LoadFunction dlsym
54 #define GL_UnloadObject dlclose
56 #define GL_LoadObject SDL_LoadObject
57 #define GL_LoadFunction SDL_LoadFunction
58 #define GL_UnloadObject SDL_UnloadObject
61 static void DirectFB_GL_UnloadLibrary(_THIS);
64 DirectFB_GL_Initialize(_THIS)
71 (struct SDL_GLDriverData *) SDL_calloc(1,
74 if (!_this->gl_data) {
78 _this->gl_data->initialized = 0;
80 ++_this->gl_data->initialized;
81 _this->gl_data->firstgl = NULL;
83 if (DirectFB_GL_LoadLibrary(_this, NULL) < 0) {
87 /* Initialize extensions */
89 * X11_GL_InitExtensions(_this);
96 DirectFB_GL_Shutdown(_THIS)
98 if (!_this->gl_data || (--_this->gl_data->initialized > 0)) {
102 DirectFB_GL_UnloadLibrary(_this);
104 SDL_free(_this->gl_data);
105 _this->gl_data = NULL;
109 DirectFB_GL_LoadLibrary(_THIS, const char *path)
111 //SDL_DFB_DEVICEDATA(_this);
115 SDL_DFB_DEBUG("Loadlibrary : %s\n", path);
117 if (_this->gl_data->gl_active) {
118 SDL_SetError("OpenGL context already created");
124 path = SDL_getenv("SDL_VIDEO_GL_DRIVER");
130 handle = GL_LoadObject(path);
131 if (handle == NULL) {
132 SDL_DFB_ERR("Library not found: %s\n", path);
133 /* SDL_LoadObject() will call SDL_SetError() for us. */
137 SDL_DFB_DEBUG("Loaded library: %s\n", path);
139 _this->gl_config.dll_handle = handle;
140 _this->gl_config.driver_loaded = 1;
142 SDL_strlcpy(_this->gl_config.driver_path, path,
143 SDL_arraysize(_this->gl_config.driver_path));
145 *_this->gl_config.driver_path = '\0';
148 _this->gl_data->glFinish = DirectFB_GL_GetProcAddress(_this, "glFinish");
149 _this->gl_data->glFlush = DirectFB_GL_GetProcAddress(_this, "glFlush");
155 DirectFB_GL_UnloadLibrary(_THIS)
160 if (_this->gl_config.driver_loaded) {
162 ret = GL_UnloadObject(_this->gl_config.dll_handle);
164 SDL_DFB_ERR("Error #%d trying to unload library.\n", ret);
165 _this->gl_config.dll_handle = NULL;
166 _this->gl_config.driver_loaded = 0;
169 /* Free OpenGL memory */
170 SDL_free(_this->gl_data);
171 _this->gl_data = NULL;
175 DirectFB_GL_GetProcAddress(_THIS, const char *proc)
179 handle = _this->gl_config.dll_handle;
180 return GL_LoadFunction(handle, proc);
184 DirectFB_GL_CreateContext(_THIS, SDL_Window * window)
186 //SDL_DFB_DEVICEDATA(_this);
187 SDL_DFB_WINDOWDATA(window);
188 DirectFB_GLContext *context;
190 SDL_DFB_ALLOC_CLEAR(context, sizeof(DirectFB_GLContext));
192 SDL_DFB_CHECKERR(windata->surface->GetGL(windata->surface,
195 if (!context->context)
198 context->is_locked = 0;
199 context->sdl_window = window;
201 context->next = _this->gl_data->firstgl;
202 _this->gl_data->firstgl = context;
204 SDL_DFB_CHECK(context->context->Unlock(context->context));
206 if (DirectFB_GL_MakeCurrent(_this, window, context) < 0) {
207 DirectFB_GL_DeleteContext(_this, context);
218 DirectFB_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
220 //SDL_DFB_WINDOWDATA(window);
221 DirectFB_GLContext *ctx = (DirectFB_GLContext *) context;
222 DirectFB_GLContext *p;
224 for (p = _this->gl_data->firstgl; p; p = p->next)
227 SDL_DFB_CHECKERR(p->context->Unlock(p->context));
234 SDL_DFB_CHECKERR(ctx->context->Lock(ctx->context));
244 DirectFB_GL_SetSwapInterval(_THIS, int interval)
251 DirectFB_GL_GetSwapInterval(_THIS)
258 DirectFB_GL_SwapWindow(_THIS, SDL_Window * window)
260 //SDL_DFB_DEVICEDATA(_this);
261 SDL_DFB_WINDOWDATA(window);
263 DirectFB_GLContext *p;
267 region.x2 = window->w;
268 region.y2 = window->h;
271 if (devdata->glFinish)
273 else if (devdata->glFlush)
277 for (p = _this->gl_data->firstgl; p != NULL; p = p->next)
278 if (p->sdl_window == window && p->is_locked)
280 SDL_DFB_CHECKERR(p->context->Unlock(p->context));
284 SDL_DFB_CHECKERR(windata->window_surface->Flip(windata->window_surface,NULL, DSFLIP_PIPELINE |DSFLIP_BLIT | DSFLIP_ONSYNC ));
286 //if (windata->gl_context) {
287 //SDL_DFB_CHECKERR(windata->surface->Flip(windata->surface,NULL, DSFLIP_ONSYNC));
288 //SDL_DFB_CHECKERR(windata->gl_context->context->Lock(windata->gl_context->context));
297 DirectFB_GL_DeleteContext(_THIS, SDL_GLContext context)
299 DirectFB_GLContext *ctx = (DirectFB_GLContext *) context;
300 DirectFB_GLContext *p;
303 SDL_DFB_CHECK(ctx->context->Unlock(ctx->context));
304 SDL_DFB_RELEASE(ctx->context);
306 for (p = _this->gl_data->firstgl; p && p->next != ctx; p = p->next)
311 _this->gl_data->firstgl = ctx->next;
317 DirectFB_GL_FreeWindowContexts(_THIS, SDL_Window * window)
319 DirectFB_GLContext *p;
321 for (p = _this->gl_data->firstgl; p != NULL; p = p->next)
322 if (p->sdl_window == window)
325 SDL_DFB_CHECK(p->context->Unlock(p->context));
326 SDL_DFB_RELEASE(p->context);
331 DirectFB_GL_ReAllocWindowContexts(_THIS, SDL_Window * window)
333 DirectFB_GLContext *p;
335 for (p = _this->gl_data->firstgl; p != NULL; p = p->next)
336 if (p->sdl_window == window)
338 SDL_DFB_WINDOWDATA(window);
339 SDL_DFB_CHECK(windata->surface->GetGL(windata->surface,
342 SDL_DFB_CHECK(p->context->Lock(p->context));
347 DirectFB_GL_DestroyWindowContexts(_THIS, SDL_Window * window)
349 DirectFB_GLContext *p;
351 for (p = _this->gl_data->firstgl; p != NULL; p = p->next)
352 if (p->sdl_window == window)
353 DirectFB_GL_DeleteContext(_this, p);
358 #endif /* SDL_VIDEO_DRIVER_DIRECTFB */
360 /* vi: set ts=4 sw=4 expandtab: */