src/video/directfb/SDL_DirectFB_opengl.c
author Ryan C. Gordon
Sun, 31 Mar 2013 12:48:50 -0400
changeset 7037 3fedf1f25b94
parent 6885 700f1b25f77f
child 7191 75360622e65f
permissions -rw-r--r--
Make SDL_SetError and friends unconditionally return -1.

This lets us change things like this...

if (Failed) {
SDL_SetError("We failed");
return -1;
}

...into this...

if (Failed) {
return SDL_SetError("We failed");
}


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