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