2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 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 #include "SDL_config.h"
24 #include "SDL_DirectFB_video.h"
26 #if SDL_DIRECTFB_OPENGL
28 struct SDL_GLDriverData
30 int gl_active; /* to stop switching drivers while we have a valid context */
32 DirectFB_GLContext *firstgl; /* linked list */
35 #define OPENGL_REQUIRS_DLOPEN
36 #if defined(OPENGL_REQUIRS_DLOPEN) && defined(SDL_LOADSO_DLOPEN)
38 #define GL_LoadObject(X) dlopen(X, (RTLD_NOW|RTLD_GLOBAL))
39 #define GL_LoadFunction dlsym
40 #define GL_UnloadObject dlclose
42 #define GL_LoadObject SDL_LoadObject
43 #define GL_LoadFunction SDL_LoadFunction
44 #define GL_UnloadObject SDL_UnloadObject
47 static void DirectFB_GL_UnloadLibrary(_THIS);
50 DirectFB_GL_Initialize(_THIS)
57 (struct SDL_GLDriverData *) SDL_calloc(1,
60 if (!_this->gl_data) {
64 _this->gl_data->initialized = 0;
66 ++_this->gl_data->initialized;
67 _this->gl_data->firstgl = NULL;
69 if (DirectFB_GL_LoadLibrary(_this, NULL) < 0) {
73 /* Initialize extensions */
75 * X11_GL_InitExtensions(_this);
82 DirectFB_GL_Shutdown(_THIS)
84 if (!_this->gl_data || (--_this->gl_data->initialized > 0)) {
88 DirectFB_GL_UnloadLibrary(_this);
90 SDL_free(_this->gl_data);
91 _this->gl_data = NULL;
95 DirectFB_GL_LoadLibrary(_THIS, const char *path)
97 SDL_DFB_DEVICEDATA(_this);
101 SDL_DFB_DEBUG("Loadlibrary : %s\n", path);
103 if (_this->gl_data->gl_active) {
104 SDL_SetError("OpenGL context already created");
110 path = SDL_getenv("SDL_VIDEO_GL_DRIVER");
116 handle = GL_LoadObject(path);
117 if (handle == NULL) {
118 SDL_DFB_ERR("Library not found: %s\n", path);
119 /* SDL_LoadObject() will call SDL_SetError() for us. */
123 SDL_DFB_DEBUG("Loaded library: %s\n", path);
125 /* Unload the old driver and reset the pointers */
126 DirectFB_GL_UnloadLibrary(_this);
128 _this->gl_config.dll_handle = handle;
129 _this->gl_config.driver_loaded = 1;
131 SDL_strlcpy(_this->gl_config.driver_path, path,
132 SDL_arraysize(_this->gl_config.driver_path));
134 *_this->gl_config.driver_path = '\0';
137 devdata->glFinish = DirectFB_GL_GetProcAddress(_this, "glFinish");
138 devdata->glFlush = DirectFB_GL_GetProcAddress(_this, "glFlush");
144 DirectFB_GL_UnloadLibrary(_THIS)
148 if (_this->gl_config.driver_loaded) {
150 ret = GL_UnloadObject(_this->gl_config.dll_handle);
152 SDL_DFB_ERR("Error #%d trying to unload library.\n", ret);
153 _this->gl_config.dll_handle = NULL;
154 _this->gl_config.driver_loaded = 0;
159 DirectFB_GL_GetProcAddress(_THIS, const char *proc)
163 handle = _this->gl_config.dll_handle;
164 return GL_LoadFunction(handle, proc);
168 DirectFB_GL_CreateContext(_THIS, SDL_Window * window)
170 SDL_DFB_WINDOWDATA(window);
171 DirectFB_GLContext *context;
174 SDL_DFB_CALLOC(context, 1, sizeof(*context));
176 SDL_DFB_CHECKERR(windata->surface->
177 GetGL(windata->surface, &context->context));
179 if (!context->context)
182 SDL_DFB_CHECKERR(context->context->Unlock(context->context));
184 context->next = _this->gl_data->firstgl;
185 _this->gl_data->firstgl = context;
187 if (DirectFB_GL_MakeCurrent(_this, window, context) < 0) {
188 DirectFB_GL_DeleteContext(_this, context);
199 DirectFB_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
201 SDL_DFB_WINDOWDATA(window);
202 DirectFB_GLContext *ctx = (DirectFB_GLContext *) context;
203 DirectFB_GLContext *p;
207 for (p = _this->gl_data->firstgl; p; p = p->next)
208 p->context->Unlock(p->context);
213 windata->gl_context = NULL;
214 /* Everything is unlocked, check for a resize */
215 SDL_DFB_CHECKERR(windata->surface->
216 GetSize(windata->surface, &cw, &ch));
217 if (cw != window->w || ch != window->h)
218 SDL_DFB_CHECKERR(windata->window->
219 ResizeSurface(windata->window, window->w,
224 SDL_DFB_CHECKERR(ctx->context->Lock(ctx->context));
228 windata->gl_context = ctx;
236 DirectFB_GL_SetSwapInterval(_THIS, int interval)
243 DirectFB_GL_GetSwapInterval(_THIS)
250 DirectFB_GL_SwapWindow(_THIS, SDL_Window * window)
252 SDL_DFB_DEVICEDATA(_this);
253 SDL_DFB_WINDOWDATA(window);
259 region.x2 = window->w;
260 region.y2 = window->h;
262 if (devdata->glFinish)
264 else if (devdata->glFlush)
267 if (1 || windata->gl_context) {
268 /* SDL_DFB_CHECKERR(windata->gl_context->context->Unlock(windata->gl_context->context)); */
269 SDL_DFB_CHECKERR(windata->surface->
270 Flip(windata->surface, ®ion, DSFLIP_ONSYNC));
271 /* SDL_DFB_CHECKERR(windata->gl_context->context->Lock(windata->gl_context->context)); */
281 DirectFB_GL_DeleteContext(_THIS, SDL_GLContext context)
283 DirectFB_GLContext *ctx = (DirectFB_GLContext *) context;
284 DirectFB_GLContext *p;
286 ctx->context->Unlock(ctx->context);
287 ctx->context->Release(ctx->context);
289 p = _this->gl_data->firstgl;
290 while (p && p->next != ctx)
295 _this->gl_data->firstgl = ctx->next;
303 /* vi: set ts=4 sw=4 expandtab: */