src/video/directfb/SDL_DirectFB_opengl.c
author Sam Lantinga
Mon, 16 Aug 2010 09:04:55 -0700
changeset 4636 b196d2758026
parent 3697 f7b03b6838cb
child 5199 164f20ba08eb
permissions -rw-r--r--
Couriersud to Sam

Hi Sam,

20100815_1.diff contains updates for the directfb driver:

- more documentation, mainly on software OpenGL in README.directfb
- Revised error handling leading to leaner code
- Improved/fixed OpenGL handling of multiple contexts.
- Made the built-in simple window manager handle OpenGL windows.
- Rewrote pixelformat mapping - this was quite ugly before.

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