2 Simple DirectMedia Layer
3 Copyright (C) 1997-2016 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_internal.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) {
75 return SDL_OutOfMemory();
77 _this->gl_data->initialized = 0;
79 ++_this->gl_data->initialized;
80 _this->gl_data->firstgl = NULL;
82 if (DirectFB_GL_LoadLibrary(_this, NULL) < 0) {
86 /* Initialize extensions */
88 * X11_GL_InitExtensions(_this);
95 DirectFB_GL_Shutdown(_THIS)
97 if (!_this->gl_data || (--_this->gl_data->initialized > 0)) {
101 DirectFB_GL_UnloadLibrary(_this);
103 SDL_free(_this->gl_data);
104 _this->gl_data = NULL;
108 DirectFB_GL_LoadLibrary(_THIS, const char *path)
112 SDL_DFB_DEBUG("Loadlibrary : %s\n", path);
114 if (_this->gl_data->gl_active) {
115 return SDL_SetError("OpenGL context already created");
120 path = SDL_getenv("SDL_VIDEO_GL_DRIVER");
126 handle = GL_LoadObject(path);
127 if (handle == NULL) {
128 SDL_DFB_ERR("Library not found: %s\n", path);
129 /* SDL_LoadObject() will call SDL_SetError() for us. */
133 SDL_DFB_DEBUG("Loaded library: %s\n", path);
135 _this->gl_config.dll_handle = handle;
136 _this->gl_config.driver_loaded = 1;
138 SDL_strlcpy(_this->gl_config.driver_path, path,
139 SDL_arraysize(_this->gl_config.driver_path));
141 *_this->gl_config.driver_path = '\0';
144 _this->gl_data->glFinish = DirectFB_GL_GetProcAddress(_this, "glFinish");
145 _this->gl_data->glFlush = DirectFB_GL_GetProcAddress(_this, "glFlush");
151 DirectFB_GL_UnloadLibrary(_THIS)
156 if (_this->gl_config.driver_loaded) {
158 ret = GL_UnloadObject(_this->gl_config.dll_handle);
160 SDL_DFB_ERR("Error #%d trying to unload library.\n", ret);
161 _this->gl_config.dll_handle = NULL;
162 _this->gl_config.driver_loaded = 0;
165 /* Free OpenGL memory */
166 SDL_free(_this->gl_data);
167 _this->gl_data = NULL;
171 DirectFB_GL_GetProcAddress(_THIS, const char *proc)
175 handle = _this->gl_config.dll_handle;
176 return GL_LoadFunction(handle, proc);
180 DirectFB_GL_CreateContext(_THIS, SDL_Window * window)
182 SDL_DFB_WINDOWDATA(window);
183 DirectFB_GLContext *context;
185 SDL_DFB_ALLOC_CLEAR(context, sizeof(DirectFB_GLContext));
187 SDL_DFB_CHECKERR(windata->surface->GetGL(windata->surface,
190 if (!context->context)
193 context->is_locked = 0;
194 context->sdl_window = window;
196 context->next = _this->gl_data->firstgl;
197 _this->gl_data->firstgl = context;
199 SDL_DFB_CHECK(context->context->Unlock(context->context));
201 if (DirectFB_GL_MakeCurrent(_this, window, context) < 0) {
202 DirectFB_GL_DeleteContext(_this, context);
213 DirectFB_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
215 DirectFB_GLContext *ctx = (DirectFB_GLContext *) context;
216 DirectFB_GLContext *p;
218 for (p = _this->gl_data->firstgl; p; p = p->next)
221 SDL_DFB_CHECKERR(p->context->Unlock(p->context));
228 SDL_DFB_CHECKERR(ctx->context->Lock(ctx->context));
238 DirectFB_GL_SetSwapInterval(_THIS, int interval)
240 return SDL_Unsupported();
244 DirectFB_GL_GetSwapInterval(_THIS)
250 DirectFB_GL_SwapWindow(_THIS, SDL_Window * window)
252 SDL_DFB_WINDOWDATA(window);
254 DirectFB_GLContext *p;
258 region.x2 = window->w;
259 region.y2 = window->h;
262 if (devdata->glFinish)
264 else if (devdata->glFlush)
268 for (p = _this->gl_data->firstgl; p != NULL; p = p->next)
269 if (p->sdl_window == window && p->is_locked)
271 SDL_DFB_CHECKERR(p->context->Unlock(p->context));
275 SDL_DFB_CHECKERR(windata->window_surface->Flip(windata->window_surface,NULL, DSFLIP_PIPELINE |DSFLIP_BLIT | DSFLIP_ONSYNC ));
282 DirectFB_GL_DeleteContext(_THIS, SDL_GLContext context)
284 DirectFB_GLContext *ctx = (DirectFB_GLContext *) context;
285 DirectFB_GLContext *p;
288 SDL_DFB_CHECK(ctx->context->Unlock(ctx->context));
289 SDL_DFB_RELEASE(ctx->context);
291 for (p = _this->gl_data->firstgl; p && p->next != ctx; p = p->next)
296 _this->gl_data->firstgl = ctx->next;
302 DirectFB_GL_FreeWindowContexts(_THIS, SDL_Window * window)
304 DirectFB_GLContext *p;
306 for (p = _this->gl_data->firstgl; p != NULL; p = p->next)
307 if (p->sdl_window == window)
310 SDL_DFB_CHECK(p->context->Unlock(p->context));
311 SDL_DFB_RELEASE(p->context);
316 DirectFB_GL_ReAllocWindowContexts(_THIS, SDL_Window * window)
318 DirectFB_GLContext *p;
320 for (p = _this->gl_data->firstgl; p != NULL; p = p->next)
321 if (p->sdl_window == window)
323 SDL_DFB_WINDOWDATA(window);
324 SDL_DFB_CHECK(windata->surface->GetGL(windata->surface,
327 SDL_DFB_CHECK(p->context->Lock(p->context));
332 DirectFB_GL_DestroyWindowContexts(_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)
338 DirectFB_GL_DeleteContext(_this, p);
343 #endif /* SDL_VIDEO_DRIVER_DIRECTFB */
345 /* vi: set ts=4 sw=4 expandtab: */