src/video/directfb/SDL_DirectFB_opengl.c
author Sam Lantinga
Mon, 14 Mar 2011 23:13:33 -0700
changeset 5488 c20436fd5627
parent 5262 b530ef003506
child 5535 96594ac5fd1a
permissions -rw-r--r--
DirectFB driver update

Couriersud to Sam

the attached patch brings the DirectFB driver back in line with recent
SDL 1.3 developments.
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@5488
    29
slouken@5199
    30
#include "SDL_DirectFB_opengl.h"
slouken@5199
    31
#include "SDL_DirectFB_window.h"
slouken@5199
    32
slouken@5199
    33
#include <directfbgl.h>
slouken@5199
    34
#include "SDL_loadso.h"
slouken@5199
    35
#endif
slouken@5199
    36
slouken@5199
    37
#if SDL_DIRECTFB_OPENGL
slouken@2737
    38
slouken@2737
    39
struct SDL_GLDriverData
slouken@2737
    40
{
slouken@2737
    41
    int gl_active;              /* to stop switching drivers while we have a valid context */
slouken@2737
    42
    int initialized;
slouken@2737
    43
    DirectFB_GLContext *firstgl;        /* linked list */
slouken@4636
    44
    
slouken@4636
    45
    /* OpenGL */
slouken@4636
    46
    void (*glFinish) (void);
slouken@4636
    47
    void (*glFlush) (void);
slouken@2737
    48
};
slouken@2737
    49
slouken@2737
    50
#define OPENGL_REQUIRS_DLOPEN
slouken@2737
    51
#if defined(OPENGL_REQUIRS_DLOPEN) && defined(SDL_LOADSO_DLOPEN)
slouken@2737
    52
#include <dlfcn.h>
slouken@2737
    53
#define GL_LoadObject(X)	dlopen(X, (RTLD_NOW|RTLD_GLOBAL))
slouken@2737
    54
#define GL_LoadFunction		dlsym
slouken@2737
    55
#define GL_UnloadObject		dlclose
slouken@2737
    56
#else
slouken@2737
    57
#define GL_LoadObject	SDL_LoadObject
slouken@2737
    58
#define GL_LoadFunction	SDL_LoadFunction
slouken@2737
    59
#define GL_UnloadObject	SDL_UnloadObject
slouken@2737
    60
#endif
slouken@2737
    61
slouken@2737
    62
static void DirectFB_GL_UnloadLibrary(_THIS);
slouken@2737
    63
slouken@2737
    64
int
slouken@2737
    65
DirectFB_GL_Initialize(_THIS)
slouken@2737
    66
{
slouken@2737
    67
    if (_this->gl_data) {
slouken@2737
    68
        return 0;
slouken@2737
    69
    }
slouken@2737
    70
slouken@2737
    71
    _this->gl_data =
slouken@2737
    72
        (struct SDL_GLDriverData *) SDL_calloc(1,
slouken@2737
    73
                                               sizeof(struct
slouken@2737
    74
                                                      SDL_GLDriverData));
slouken@2737
    75
    if (!_this->gl_data) {
slouken@2737
    76
        SDL_OutOfMemory();
slouken@2737
    77
        return -1;
slouken@2737
    78
    }
slouken@2737
    79
    _this->gl_data->initialized = 0;
slouken@2737
    80
slouken@2737
    81
    ++_this->gl_data->initialized;
slouken@2737
    82
    _this->gl_data->firstgl = NULL;
slouken@2737
    83
slouken@2737
    84
    if (DirectFB_GL_LoadLibrary(_this, NULL) < 0) {
slouken@2737
    85
        return -1;
slouken@2737
    86
    }
slouken@2737
    87
slouken@2737
    88
    /* Initialize extensions */
slouken@2737
    89
    /* FIXME needed?
slouken@2737
    90
     * X11_GL_InitExtensions(_this);
slouken@2737
    91
     */
slouken@2737
    92
slouken@2737
    93
    return 0;
slouken@2737
    94
}
slouken@2737
    95
slouken@2737
    96
void
slouken@2737
    97
DirectFB_GL_Shutdown(_THIS)
slouken@2737
    98
{
slouken@2737
    99
    if (!_this->gl_data || (--_this->gl_data->initialized > 0)) {
slouken@2737
   100
        return;
slouken@2737
   101
    }
slouken@2737
   102
slouken@2737
   103
    DirectFB_GL_UnloadLibrary(_this);
slouken@2737
   104
slouken@2737
   105
    SDL_free(_this->gl_data);
slouken@2737
   106
    _this->gl_data = NULL;
slouken@2737
   107
}
slouken@2737
   108
slouken@2737
   109
int
slouken@2737
   110
DirectFB_GL_LoadLibrary(_THIS, const char *path)
slouken@2737
   111
{
slouken@4636
   112
    //SDL_DFB_DEVICEDATA(_this);
slouken@2737
   113
slouken@2737
   114
    void *handle = NULL;
slouken@2737
   115
slouken@2737
   116
    SDL_DFB_DEBUG("Loadlibrary : %s\n", path);
slouken@2737
   117
slouken@2737
   118
    if (_this->gl_data->gl_active) {
slouken@2737
   119
        SDL_SetError("OpenGL context already created");
slouken@2737
   120
        return -1;
slouken@2737
   121
    }
slouken@2737
   122
slouken@2737
   123
slouken@2737
   124
    if (path == NULL) {
slouken@2737
   125
        path = SDL_getenv("SDL_VIDEO_GL_DRIVER");
slouken@2737
   126
        if (path == NULL) {
slouken@2737
   127
            path = "libGL.so";
slouken@2737
   128
        }
slouken@2737
   129
    }
slouken@2737
   130
slouken@2737
   131
    handle = GL_LoadObject(path);
slouken@2737
   132
    if (handle == NULL) {
slouken@2737
   133
        SDL_DFB_ERR("Library not found: %s\n", path);
slouken@2737
   134
        /* SDL_LoadObject() will call SDL_SetError() for us. */
slouken@2737
   135
        return -1;
slouken@2737
   136
    }
slouken@2737
   137
slouken@2737
   138
    SDL_DFB_DEBUG("Loaded library: %s\n", path);
slouken@2737
   139
slouken@2737
   140
    _this->gl_config.dll_handle = handle;
slouken@2737
   141
    _this->gl_config.driver_loaded = 1;
slouken@2737
   142
    if (path) {
slouken@2737
   143
        SDL_strlcpy(_this->gl_config.driver_path, path,
slouken@2737
   144
                    SDL_arraysize(_this->gl_config.driver_path));
slouken@2737
   145
    } else {
slouken@2737
   146
        *_this->gl_config.driver_path = '\0';
slouken@2737
   147
    }
slouken@2737
   148
slouken@4636
   149
    _this->gl_data->glFinish = DirectFB_GL_GetProcAddress(_this, "glFinish");
slouken@4636
   150
    _this->gl_data->glFlush = DirectFB_GL_GetProcAddress(_this, "glFlush");
slouken@2737
   151
slouken@2737
   152
    return 0;
slouken@2737
   153
}
slouken@2737
   154
slouken@2737
   155
static void
slouken@2737
   156
DirectFB_GL_UnloadLibrary(_THIS)
slouken@2737
   157
{
slouken@4636
   158
 #if 0
slouken@2737
   159
    int ret;
slouken@2737
   160
slouken@2737
   161
    if (_this->gl_config.driver_loaded) {
slouken@2737
   162
slouken@2737
   163
        ret = GL_UnloadObject(_this->gl_config.dll_handle);
slouken@2737
   164
        if (ret)
slouken@2737
   165
            SDL_DFB_ERR("Error #%d trying to unload library.\n", ret);
slouken@2737
   166
        _this->gl_config.dll_handle = NULL;
slouken@2737
   167
        _this->gl_config.driver_loaded = 0;
slouken@2737
   168
    }
slouken@4636
   169
#endif
slouken@4636
   170
    /* Free OpenGL memory */
slouken@4636
   171
    SDL_free(_this->gl_data);
slouken@4636
   172
    _this->gl_data = NULL;
slouken@2737
   173
}
slouken@2737
   174
slouken@2737
   175
void *
slouken@2737
   176
DirectFB_GL_GetProcAddress(_THIS, const char *proc)
slouken@2737
   177
{
slouken@2737
   178
    void *handle;
slouken@2737
   179
slouken@2737
   180
    handle = _this->gl_config.dll_handle;
slouken@2737
   181
    return GL_LoadFunction(handle, proc);
slouken@2737
   182
}
slouken@2737
   183
slouken@2737
   184
SDL_GLContext
slouken@2737
   185
DirectFB_GL_CreateContext(_THIS, SDL_Window * window)
slouken@2737
   186
{
slouken@4636
   187
    //SDL_DFB_DEVICEDATA(_this);
slouken@2737
   188
    SDL_DFB_WINDOWDATA(window);
slouken@2737
   189
    DirectFB_GLContext *context;
slouken@2737
   190
slouken@5199
   191
    SDL_DFB_ALLOC_CLEAR(context, sizeof(DirectFB_GLContext));
slouken@2737
   192
couriersud@3023
   193
    SDL_DFB_CHECKERR(windata->surface->GetGL(windata->surface,
couriersud@3023
   194
                                             &context->context));
slouken@2998
   195
slouken@2998
   196
    if (!context->context)
slouken@2998
   197
        return NULL;
slouken@2998
   198
slouken@4636
   199
    context->is_locked = 0;
slouken@4636
   200
    context->sdl_window = window;
slouken@4636
   201
    
slouken@2737
   202
    context->next = _this->gl_data->firstgl;
slouken@2737
   203
    _this->gl_data->firstgl = context;
slouken@2737
   204
slouken@4636
   205
    SDL_DFB_CHECK(context->context->Unlock(context->context));
slouken@4636
   206
slouken@2737
   207
    if (DirectFB_GL_MakeCurrent(_this, window, context) < 0) {
slouken@2737
   208
        DirectFB_GL_DeleteContext(_this, context);
slouken@2737
   209
        return NULL;
slouken@2737
   210
    }
slouken@2737
   211
slouken@2737
   212
    return context;
slouken@2737
   213
slouken@2737
   214
  error:
slouken@2737
   215
    return NULL;
slouken@2737
   216
}
slouken@2737
   217
slouken@2737
   218
int
slouken@2737
   219
DirectFB_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
slouken@2737
   220
{
slouken@4636
   221
    //SDL_DFB_WINDOWDATA(window);
slouken@2737
   222
    DirectFB_GLContext *ctx = (DirectFB_GLContext *) context;
slouken@2737
   223
    DirectFB_GLContext *p;
slouken@2737
   224
slouken@2737
   225
    for (p = _this->gl_data->firstgl; p; p = p->next)
slouken@4636
   226
    {
slouken@4636
   227
       if (p->is_locked) {
slouken@4636
   228
         SDL_DFB_CHECKERR(p->context->Unlock(p->context));
slouken@4636
   229
         p->is_locked = 0;
slouken@4636
   230
       }
slouken@4636
   231
        
slouken@2737
   232
    }
slouken@2737
   233
slouken@2737
   234
    if (ctx != NULL) {
slouken@2737
   235
        SDL_DFB_CHECKERR(ctx->context->Lock(ctx->context));
slouken@4636
   236
        ctx->is_locked = 1;
slouken@2737
   237
    }
slouken@2737
   238
slouken@2737
   239
    return 0;
slouken@2737
   240
  error:
slouken@2737
   241
    return -1;
slouken@2737
   242
}
slouken@2737
   243
slouken@2737
   244
int
slouken@2737
   245
DirectFB_GL_SetSwapInterval(_THIS, int interval)
slouken@2737
   246
{
slouken@2737
   247
    SDL_Unsupported();
slouken@2737
   248
    return -1;
slouken@2737
   249
}
slouken@2737
   250
slouken@2737
   251
int
slouken@2737
   252
DirectFB_GL_GetSwapInterval(_THIS)
slouken@2737
   253
{
slouken@2737
   254
    SDL_Unsupported();
slouken@2737
   255
    return -1;
slouken@2737
   256
}
slouken@2737
   257
slouken@2737
   258
void
slouken@2737
   259
DirectFB_GL_SwapWindow(_THIS, SDL_Window * window)
slouken@2737
   260
{
slouken@4636
   261
    //SDL_DFB_DEVICEDATA(_this);
slouken@2737
   262
    SDL_DFB_WINDOWDATA(window);
slouken@2737
   263
    DFBRegion region;
slouken@4636
   264
    DirectFB_GLContext *p;
slouken@2737
   265
slouken@2737
   266
    region.x1 = 0;
slouken@2737
   267
    region.y1 = 0;
slouken@2737
   268
    region.x2 = window->w;
slouken@2737
   269
    region.y2 = window->h;
slouken@2737
   270
slouken@4636
   271
#if 0
slouken@2737
   272
    if (devdata->glFinish)
slouken@2737
   273
        devdata->glFinish();
slouken@2737
   274
    else if (devdata->glFlush)
slouken@2737
   275
        devdata->glFlush();
slouken@4636
   276
#endif
slouken@2737
   277
slouken@4636
   278
  	for (p = _this->gl_data->firstgl; p != NULL; p = p->next)
slouken@4636
   279
        if (p->sdl_window == window && p->is_locked)
slouken@4636
   280
        {
slouken@4636
   281
            SDL_DFB_CHECKERR(p->context->Unlock(p->context));
slouken@4636
   282
            p->is_locked = 0;
slouken@4636
   283
        }            
slouken@2737
   284
slouken@4636
   285
    SDL_DFB_CHECKERR(windata->window_surface->Flip(windata->window_surface,NULL,  DSFLIP_PIPELINE |DSFLIP_BLIT | DSFLIP_ONSYNC ));
slouken@4636
   286
slouken@4636
   287
    //if (windata->gl_context) {
slouken@4636
   288
        //SDL_DFB_CHECKERR(windata->surface->Flip(windata->surface,NULL, DSFLIP_ONSYNC)); 
slouken@4636
   289
        //SDL_DFB_CHECKERR(windata->gl_context->context->Lock(windata->gl_context->context));
slouken@4636
   290
    //}
slouken@2737
   291
slouken@2737
   292
    return;
slouken@2737
   293
  error:
slouken@2737
   294
    return;
slouken@2737
   295
}
slouken@2737
   296
slouken@2737
   297
void
slouken@2737
   298
DirectFB_GL_DeleteContext(_THIS, SDL_GLContext context)
slouken@2737
   299
{
slouken@2737
   300
    DirectFB_GLContext *ctx = (DirectFB_GLContext *) context;
slouken@2737
   301
    DirectFB_GLContext *p;
slouken@2737
   302
slouken@4636
   303
    if (ctx->is_locked)
slouken@4636
   304
        SDL_DFB_CHECK(ctx->context->Unlock(ctx->context));
slouken@4636
   305
    SDL_DFB_RELEASE(ctx->context);
slouken@2737
   306
slouken@4636
   307
    for (p = _this->gl_data->firstgl; p && p->next != ctx; p = p->next)
slouken@4636
   308
        ;
slouken@2737
   309
    if (p)
slouken@2737
   310
        p->next = ctx->next;
slouken@2737
   311
    else
slouken@2737
   312
        _this->gl_data->firstgl = ctx->next;
slouken@2737
   313
slouken@2737
   314
    SDL_DFB_FREE(ctx);
slouken@4636
   315
}
slouken@2737
   316
slouken@4636
   317
void
slouken@4636
   318
DirectFB_GL_FreeWindowContexts(_THIS, SDL_Window * window)
slouken@4636
   319
{
slouken@4636
   320
    DirectFB_GLContext *p;
slouken@4636
   321
    
slouken@4636
   322
	for (p = _this->gl_data->firstgl; p != NULL; p = p->next)
slouken@4636
   323
	    if (p->sdl_window == window)
slouken@4636
   324
	    {
slouken@4636
   325
	    	if (p->is_locked)
slouken@4636
   326
	        	SDL_DFB_CHECK(p->context->Unlock(p->context));
slouken@4636
   327
	        SDL_DFB_RELEASE(p->context);
slouken@4636
   328
	    }
slouken@4636
   329
}
slouken@4636
   330
slouken@4636
   331
void
slouken@4636
   332
DirectFB_GL_ReAllocWindowContexts(_THIS, SDL_Window * window)
slouken@4636
   333
{
slouken@4636
   334
    DirectFB_GLContext *p;
slouken@4636
   335
slouken@4636
   336
	for (p = _this->gl_data->firstgl; p != NULL; p = p->next)
slouken@4636
   337
	    if (p->sdl_window == window)
slouken@4636
   338
	    {
slouken@4636
   339
            SDL_DFB_WINDOWDATA(window);
slouken@4636
   340
			SDL_DFB_CHECK(windata->surface->GetGL(windata->surface,
slouken@4636
   341
	                                         &p->context));
slouken@4636
   342
    		if (p->is_locked)
slouken@4636
   343
            	SDL_DFB_CHECK(p->context->Lock(p->context));
slouken@4636
   344
	        }
slouken@4636
   345
}
slouken@4636
   346
slouken@4636
   347
void
slouken@4636
   348
DirectFB_GL_DestroyWindowContexts(_THIS, SDL_Window * window)
slouken@4636
   349
{
slouken@4636
   350
    DirectFB_GLContext *p;
slouken@4636
   351
slouken@4636
   352
	for (p = _this->gl_data->firstgl; p != NULL; p = p->next)
slouken@4636
   353
		if (p->sdl_window == window)
slouken@4636
   354
			DirectFB_GL_DeleteContext(_this, p);
slouken@2737
   355
}
slouken@2737
   356
slouken@2737
   357
#endif
slouken@2737
   358
slouken@2737
   359
/* vi: set ts=4 sw=4 expandtab: */