src/video/directfb/SDL_DirectFB_opengl.c
author Sam Lantinga
Fri, 11 Feb 2011 22:37:15 -0800
changeset 5262 b530ef003506
parent 5199 164f20ba08eb
child 5488 c20436fd5627
permissions -rw-r--r--
Happy 2011! :)
slouken@2737
     1
/*
slouken@2737
     2
    SDL - Simple DirectMedia Layer
slouken@5262
     3
    Copyright (C) 1997-2011 Sam Lantinga
slouken@2737
     4
slouken@2737
     5
    This library is free software; you can redistribute it and/or
slouken@2737
     6
    modify it under the terms of the GNU Lesser General Public
slouken@2737
     7
    License as published by the Free Software Foundation; either
slouken@2737
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@2737
     9
slouken@2737
    10
    This library is distributed in the hope that it will be useful,
slouken@2737
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@2737
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@2737
    13
    Lesser General Public License for more details.
slouken@2737
    14
slouken@2737
    15
    You should have received a copy of the GNU Lesser General Public
slouken@5199
    16
    License along with this library; if not, write to the Free Software
slouken@2737
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@2737
    18
slouken@2737
    19
    Sam Lantinga
slouken@2737
    20
    slouken@libsdl.org
slouken@5199
    21
slouken@5199
    22
    SDL1.3 DirectFB driver by couriersud@arcor.de
slouken@5199
    23
	
slouken@2737
    24
*/
slouken@2737
    25
slouken@2737
    26
#include "SDL_DirectFB_video.h"
slouken@2737
    27
slouken@2737
    28
#if SDL_DIRECTFB_OPENGL
slouken@5199
    29
#include "SDL_DirectFB_opengl.h"
slouken@5199
    30
#include "SDL_DirectFB_window.h"
slouken@5199
    31
slouken@5199
    32
#include <directfbgl.h>
slouken@5199
    33
#include "SDL_loadso.h"
slouken@5199
    34
#endif
slouken@5199
    35
slouken@5199
    36
#if SDL_DIRECTFB_OPENGL
slouken@2737
    37
slouken@2737
    38
struct SDL_GLDriverData
slouken@2737
    39
{
slouken@2737
    40
    int gl_active;              /* to stop switching drivers while we have a valid context */
slouken@2737
    41
    int initialized;
slouken@2737
    42
    DirectFB_GLContext *firstgl;        /* linked list */
slouken@4636
    43
    
slouken@4636
    44
    /* OpenGL */
slouken@4636
    45
    void (*glFinish) (void);
slouken@4636
    46
    void (*glFlush) (void);
slouken@2737
    47
};
slouken@2737
    48
slouken@2737
    49
#define OPENGL_REQUIRS_DLOPEN
slouken@2737
    50
#if defined(OPENGL_REQUIRS_DLOPEN) && defined(SDL_LOADSO_DLOPEN)
slouken@2737
    51
#include <dlfcn.h>
slouken@2737
    52
#define GL_LoadObject(X)	dlopen(X, (RTLD_NOW|RTLD_GLOBAL))
slouken@2737
    53
#define GL_LoadFunction		dlsym
slouken@2737
    54
#define GL_UnloadObject		dlclose
slouken@2737
    55
#else
slouken@2737
    56
#define GL_LoadObject	SDL_LoadObject
slouken@2737
    57
#define GL_LoadFunction	SDL_LoadFunction
slouken@2737
    58
#define GL_UnloadObject	SDL_UnloadObject
slouken@2737
    59
#endif
slouken@2737
    60
slouken@2737
    61
static void DirectFB_GL_UnloadLibrary(_THIS);
slouken@2737
    62
slouken@2737
    63
int
slouken@2737
    64
DirectFB_GL_Initialize(_THIS)
slouken@2737
    65
{
slouken@2737
    66
    if (_this->gl_data) {
slouken@2737
    67
        return 0;
slouken@2737
    68
    }
slouken@2737
    69
slouken@2737
    70
    _this->gl_data =
slouken@2737
    71
        (struct SDL_GLDriverData *) SDL_calloc(1,
slouken@2737
    72
                                               sizeof(struct
slouken@2737
    73
                                                      SDL_GLDriverData));
slouken@2737
    74
    if (!_this->gl_data) {
slouken@2737
    75
        SDL_OutOfMemory();
slouken@2737
    76
        return -1;
slouken@2737
    77
    }
slouken@2737
    78
    _this->gl_data->initialized = 0;
slouken@2737
    79
slouken@2737
    80
    ++_this->gl_data->initialized;
slouken@2737
    81
    _this->gl_data->firstgl = NULL;
slouken@2737
    82
slouken@2737
    83
    if (DirectFB_GL_LoadLibrary(_this, NULL) < 0) {
slouken@2737
    84
        return -1;
slouken@2737
    85
    }
slouken@2737
    86
slouken@2737
    87
    /* Initialize extensions */
slouken@2737
    88
    /* FIXME needed?
slouken@2737
    89
     * X11_GL_InitExtensions(_this);
slouken@2737
    90
     */
slouken@2737
    91
slouken@2737
    92
    return 0;
slouken@2737
    93
}
slouken@2737
    94
slouken@2737
    95
void
slouken@2737
    96
DirectFB_GL_Shutdown(_THIS)
slouken@2737
    97
{
slouken@2737
    98
    if (!_this->gl_data || (--_this->gl_data->initialized > 0)) {
slouken@2737
    99
        return;
slouken@2737
   100
    }
slouken@2737
   101
slouken@2737
   102
    DirectFB_GL_UnloadLibrary(_this);
slouken@2737
   103
slouken@2737
   104
    SDL_free(_this->gl_data);
slouken@2737
   105
    _this->gl_data = NULL;
slouken@2737
   106
}
slouken@2737
   107
slouken@2737
   108
int
slouken@2737
   109
DirectFB_GL_LoadLibrary(_THIS, const char *path)
slouken@2737
   110
{
slouken@4636
   111
    //SDL_DFB_DEVICEDATA(_this);
slouken@2737
   112
slouken@2737
   113
    void *handle = NULL;
slouken@2737
   114
slouken@2737
   115
    SDL_DFB_DEBUG("Loadlibrary : %s\n", path);
slouken@2737
   116
slouken@2737
   117
    if (_this->gl_data->gl_active) {
slouken@2737
   118
        SDL_SetError("OpenGL context already created");
slouken@2737
   119
        return -1;
slouken@2737
   120
    }
slouken@2737
   121
slouken@2737
   122
slouken@2737
   123
    if (path == NULL) {
slouken@2737
   124
        path = SDL_getenv("SDL_VIDEO_GL_DRIVER");
slouken@2737
   125
        if (path == NULL) {
slouken@2737
   126
            path = "libGL.so";
slouken@2737
   127
        }
slouken@2737
   128
    }
slouken@2737
   129
slouken@2737
   130
    handle = GL_LoadObject(path);
slouken@2737
   131
    if (handle == NULL) {
slouken@2737
   132
        SDL_DFB_ERR("Library not found: %s\n", path);
slouken@2737
   133
        /* SDL_LoadObject() will call SDL_SetError() for us. */
slouken@2737
   134
        return -1;
slouken@2737
   135
    }
slouken@2737
   136
slouken@2737
   137
    SDL_DFB_DEBUG("Loaded library: %s\n", path);
slouken@2737
   138
slouken@2737
   139
    _this->gl_config.dll_handle = handle;
slouken@2737
   140
    _this->gl_config.driver_loaded = 1;
slouken@2737
   141
    if (path) {
slouken@2737
   142
        SDL_strlcpy(_this->gl_config.driver_path, path,
slouken@2737
   143
                    SDL_arraysize(_this->gl_config.driver_path));
slouken@2737
   144
    } else {
slouken@2737
   145
        *_this->gl_config.driver_path = '\0';
slouken@2737
   146
    }
slouken@2737
   147
slouken@4636
   148
    _this->gl_data->glFinish = DirectFB_GL_GetProcAddress(_this, "glFinish");
slouken@4636
   149
    _this->gl_data->glFlush = DirectFB_GL_GetProcAddress(_this, "glFlush");
slouken@2737
   150
slouken@2737
   151
    return 0;
slouken@2737
   152
}
slouken@2737
   153
slouken@2737
   154
static void
slouken@2737
   155
DirectFB_GL_UnloadLibrary(_THIS)
slouken@2737
   156
{
slouken@4636
   157
 #if 0
slouken@2737
   158
    int ret;
slouken@2737
   159
slouken@2737
   160
    if (_this->gl_config.driver_loaded) {
slouken@2737
   161
slouken@2737
   162
        ret = GL_UnloadObject(_this->gl_config.dll_handle);
slouken@2737
   163
        if (ret)
slouken@2737
   164
            SDL_DFB_ERR("Error #%d trying to unload library.\n", ret);
slouken@2737
   165
        _this->gl_config.dll_handle = NULL;
slouken@2737
   166
        _this->gl_config.driver_loaded = 0;
slouken@2737
   167
    }
slouken@4636
   168
#endif
slouken@4636
   169
    /* Free OpenGL memory */
slouken@4636
   170
    SDL_free(_this->gl_data);
slouken@4636
   171
    _this->gl_data = NULL;
slouken@2737
   172
}
slouken@2737
   173
slouken@2737
   174
void *
slouken@2737
   175
DirectFB_GL_GetProcAddress(_THIS, const char *proc)
slouken@2737
   176
{
slouken@2737
   177
    void *handle;
slouken@2737
   178
slouken@2737
   179
    handle = _this->gl_config.dll_handle;
slouken@2737
   180
    return GL_LoadFunction(handle, proc);
slouken@2737
   181
}
slouken@2737
   182
slouken@2737
   183
SDL_GLContext
slouken@2737
   184
DirectFB_GL_CreateContext(_THIS, SDL_Window * window)
slouken@2737
   185
{
slouken@4636
   186
    //SDL_DFB_DEVICEDATA(_this);
slouken@2737
   187
    SDL_DFB_WINDOWDATA(window);
slouken@2737
   188
    DirectFB_GLContext *context;
slouken@2737
   189
slouken@5199
   190
    SDL_DFB_ALLOC_CLEAR(context, sizeof(DirectFB_GLContext));
slouken@2737
   191
couriersud@3023
   192
    SDL_DFB_CHECKERR(windata->surface->GetGL(windata->surface,
couriersud@3023
   193
                                             &context->context));
slouken@2998
   194
slouken@2998
   195
    if (!context->context)
slouken@2998
   196
        return NULL;
slouken@2998
   197
slouken@4636
   198
    context->is_locked = 0;
slouken@4636
   199
    context->sdl_window = window;
slouken@4636
   200
    
slouken@2737
   201
    context->next = _this->gl_data->firstgl;
slouken@2737
   202
    _this->gl_data->firstgl = context;
slouken@2737
   203
slouken@4636
   204
    SDL_DFB_CHECK(context->context->Unlock(context->context));
slouken@4636
   205
slouken@2737
   206
    if (DirectFB_GL_MakeCurrent(_this, window, context) < 0) {
slouken@2737
   207
        DirectFB_GL_DeleteContext(_this, context);
slouken@2737
   208
        return NULL;
slouken@2737
   209
    }
slouken@2737
   210
slouken@2737
   211
    return context;
slouken@2737
   212
slouken@2737
   213
  error:
slouken@2737
   214
    return NULL;
slouken@2737
   215
}
slouken@2737
   216
slouken@2737
   217
int
slouken@2737
   218
DirectFB_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
slouken@2737
   219
{
slouken@4636
   220
    //SDL_DFB_WINDOWDATA(window);
slouken@2737
   221
    DirectFB_GLContext *ctx = (DirectFB_GLContext *) context;
slouken@2737
   222
    DirectFB_GLContext *p;
slouken@2737
   223
slouken@2737
   224
    for (p = _this->gl_data->firstgl; p; p = p->next)
slouken@4636
   225
    {
slouken@4636
   226
       if (p->is_locked) {
slouken@4636
   227
         SDL_DFB_CHECKERR(p->context->Unlock(p->context));
slouken@4636
   228
         p->is_locked = 0;
slouken@4636
   229
       }
slouken@4636
   230
        
slouken@2737
   231
    }
slouken@2737
   232
slouken@2737
   233
    if (ctx != NULL) {
slouken@2737
   234
        SDL_DFB_CHECKERR(ctx->context->Lock(ctx->context));
slouken@4636
   235
        ctx->is_locked = 1;
slouken@2737
   236
    }
slouken@2737
   237
slouken@2737
   238
    return 0;
slouken@2737
   239
  error:
slouken@2737
   240
    return -1;
slouken@2737
   241
}
slouken@2737
   242
slouken@2737
   243
int
slouken@2737
   244
DirectFB_GL_SetSwapInterval(_THIS, int interval)
slouken@2737
   245
{
slouken@2737
   246
    SDL_Unsupported();
slouken@2737
   247
    return -1;
slouken@2737
   248
}
slouken@2737
   249
slouken@2737
   250
int
slouken@2737
   251
DirectFB_GL_GetSwapInterval(_THIS)
slouken@2737
   252
{
slouken@2737
   253
    SDL_Unsupported();
slouken@2737
   254
    return -1;
slouken@2737
   255
}
slouken@2737
   256
slouken@2737
   257
void
slouken@2737
   258
DirectFB_GL_SwapWindow(_THIS, SDL_Window * window)
slouken@2737
   259
{
slouken@4636
   260
    //SDL_DFB_DEVICEDATA(_this);
slouken@2737
   261
    SDL_DFB_WINDOWDATA(window);
slouken@2737
   262
    DFBRegion region;
slouken@4636
   263
    DirectFB_GLContext *p;
slouken@2737
   264
slouken@2737
   265
    region.x1 = 0;
slouken@2737
   266
    region.y1 = 0;
slouken@2737
   267
    region.x2 = window->w;
slouken@2737
   268
    region.y2 = window->h;
slouken@2737
   269
slouken@4636
   270
#if 0
slouken@2737
   271
    if (devdata->glFinish)
slouken@2737
   272
        devdata->glFinish();
slouken@2737
   273
    else if (devdata->glFlush)
slouken@2737
   274
        devdata->glFlush();
slouken@4636
   275
#endif
slouken@2737
   276
slouken@4636
   277
  	for (p = _this->gl_data->firstgl; p != NULL; p = p->next)
slouken@4636
   278
        if (p->sdl_window == window && p->is_locked)
slouken@4636
   279
        {
slouken@4636
   280
            SDL_DFB_CHECKERR(p->context->Unlock(p->context));
slouken@4636
   281
            p->is_locked = 0;
slouken@4636
   282
        }            
slouken@2737
   283
slouken@4636
   284
    SDL_DFB_CHECKERR(windata->window_surface->Flip(windata->window_surface,NULL,  DSFLIP_PIPELINE |DSFLIP_BLIT | DSFLIP_ONSYNC ));
slouken@4636
   285
slouken@4636
   286
    //if (windata->gl_context) {
slouken@4636
   287
        //SDL_DFB_CHECKERR(windata->surface->Flip(windata->surface,NULL, DSFLIP_ONSYNC)); 
slouken@4636
   288
        //SDL_DFB_CHECKERR(windata->gl_context->context->Lock(windata->gl_context->context));
slouken@4636
   289
    //}
slouken@2737
   290
slouken@2737
   291
    return;
slouken@2737
   292
  error:
slouken@2737
   293
    return;
slouken@2737
   294
}
slouken@2737
   295
slouken@2737
   296
void
slouken@2737
   297
DirectFB_GL_DeleteContext(_THIS, SDL_GLContext context)
slouken@2737
   298
{
slouken@2737
   299
    DirectFB_GLContext *ctx = (DirectFB_GLContext *) context;
slouken@2737
   300
    DirectFB_GLContext *p;
slouken@2737
   301
slouken@4636
   302
    if (ctx->is_locked)
slouken@4636
   303
        SDL_DFB_CHECK(ctx->context->Unlock(ctx->context));
slouken@4636
   304
    SDL_DFB_RELEASE(ctx->context);
slouken@2737
   305
slouken@4636
   306
    for (p = _this->gl_data->firstgl; p && p->next != ctx; p = p->next)
slouken@4636
   307
        ;
slouken@2737
   308
    if (p)
slouken@2737
   309
        p->next = ctx->next;
slouken@2737
   310
    else
slouken@2737
   311
        _this->gl_data->firstgl = ctx->next;
slouken@2737
   312
slouken@2737
   313
    SDL_DFB_FREE(ctx);
slouken@4636
   314
}
slouken@2737
   315
slouken@4636
   316
void
slouken@4636
   317
DirectFB_GL_FreeWindowContexts(_THIS, SDL_Window * window)
slouken@4636
   318
{
slouken@4636
   319
    DirectFB_GLContext *p;
slouken@4636
   320
    
slouken@4636
   321
	for (p = _this->gl_data->firstgl; p != NULL; p = p->next)
slouken@4636
   322
	    if (p->sdl_window == window)
slouken@4636
   323
	    {
slouken@4636
   324
	    	if (p->is_locked)
slouken@4636
   325
	        	SDL_DFB_CHECK(p->context->Unlock(p->context));
slouken@4636
   326
	        SDL_DFB_RELEASE(p->context);
slouken@4636
   327
	    }
slouken@4636
   328
}
slouken@4636
   329
slouken@4636
   330
void
slouken@4636
   331
DirectFB_GL_ReAllocWindowContexts(_THIS, SDL_Window * window)
slouken@4636
   332
{
slouken@4636
   333
    DirectFB_GLContext *p;
slouken@4636
   334
slouken@4636
   335
	for (p = _this->gl_data->firstgl; p != NULL; p = p->next)
slouken@4636
   336
	    if (p->sdl_window == window)
slouken@4636
   337
	    {
slouken@4636
   338
            SDL_DFB_WINDOWDATA(window);
slouken@4636
   339
			SDL_DFB_CHECK(windata->surface->GetGL(windata->surface,
slouken@4636
   340
	                                         &p->context));
slouken@4636
   341
    		if (p->is_locked)
slouken@4636
   342
            	SDL_DFB_CHECK(p->context->Lock(p->context));
slouken@4636
   343
	        }
slouken@4636
   344
}
slouken@4636
   345
slouken@4636
   346
void
slouken@4636
   347
DirectFB_GL_DestroyWindowContexts(_THIS, SDL_Window * window)
slouken@4636
   348
{
slouken@4636
   349
    DirectFB_GLContext *p;
slouken@4636
   350
slouken@4636
   351
	for (p = _this->gl_data->firstgl; p != NULL; p = p->next)
slouken@4636
   352
		if (p->sdl_window == window)
slouken@4636
   353
			DirectFB_GL_DeleteContext(_this, p);
slouken@2737
   354
}
slouken@2737
   355
slouken@2737
   356
#endif
slouken@2737
   357
slouken@2737
   358
/* vi: set ts=4 sw=4 expandtab: */