src/video/directfb/SDL_DirectFB_opengl.c
author Sam Lantinga
Sun, 24 Jan 2010 21:10:53 +0000
changeset 3697 f7b03b6838cb
parent 3023 d72a0dd80e8b
child 4636 b196d2758026
permissions -rw-r--r--
Fixed bug #926

Updated copyright to LGPL version 2.1 and year 2010
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@2737
    33
};
slouken@2737
    34
slouken@2737
    35
#define OPENGL_REQUIRS_DLOPEN
slouken@2737
    36
#if defined(OPENGL_REQUIRS_DLOPEN) && defined(SDL_LOADSO_DLOPEN)
slouken@2737
    37
#include <dlfcn.h>
slouken@2737
    38
#define GL_LoadObject(X)	dlopen(X, (RTLD_NOW|RTLD_GLOBAL))
slouken@2737
    39
#define GL_LoadFunction		dlsym
slouken@2737
    40
#define GL_UnloadObject		dlclose
slouken@2737
    41
#else
slouken@2737
    42
#define GL_LoadObject	SDL_LoadObject
slouken@2737
    43
#define GL_LoadFunction	SDL_LoadFunction
slouken@2737
    44
#define GL_UnloadObject	SDL_UnloadObject
slouken@2737
    45
#endif
slouken@2737
    46
slouken@2737
    47
static void DirectFB_GL_UnloadLibrary(_THIS);
slouken@2737
    48
slouken@2737
    49
int
slouken@2737
    50
DirectFB_GL_Initialize(_THIS)
slouken@2737
    51
{
slouken@2737
    52
    if (_this->gl_data) {
slouken@2737
    53
        return 0;
slouken@2737
    54
    }
slouken@2737
    55
slouken@2737
    56
    _this->gl_data =
slouken@2737
    57
        (struct SDL_GLDriverData *) SDL_calloc(1,
slouken@2737
    58
                                               sizeof(struct
slouken@2737
    59
                                                      SDL_GLDriverData));
slouken@2737
    60
    if (!_this->gl_data) {
slouken@2737
    61
        SDL_OutOfMemory();
slouken@2737
    62
        return -1;
slouken@2737
    63
    }
slouken@2737
    64
    _this->gl_data->initialized = 0;
slouken@2737
    65
slouken@2737
    66
    ++_this->gl_data->initialized;
slouken@2737
    67
    _this->gl_data->firstgl = NULL;
slouken@2737
    68
slouken@2737
    69
    if (DirectFB_GL_LoadLibrary(_this, NULL) < 0) {
slouken@2737
    70
        return -1;
slouken@2737
    71
    }
slouken@2737
    72
slouken@2737
    73
    /* Initialize extensions */
slouken@2737
    74
    /* FIXME needed?
slouken@2737
    75
     * X11_GL_InitExtensions(_this);
slouken@2737
    76
     */
slouken@2737
    77
slouken@2737
    78
    return 0;
slouken@2737
    79
}
slouken@2737
    80
slouken@2737
    81
void
slouken@2737
    82
DirectFB_GL_Shutdown(_THIS)
slouken@2737
    83
{
slouken@2737
    84
    if (!_this->gl_data || (--_this->gl_data->initialized > 0)) {
slouken@2737
    85
        return;
slouken@2737
    86
    }
slouken@2737
    87
slouken@2737
    88
    DirectFB_GL_UnloadLibrary(_this);
slouken@2737
    89
slouken@2737
    90
    SDL_free(_this->gl_data);
slouken@2737
    91
    _this->gl_data = NULL;
slouken@2737
    92
}
slouken@2737
    93
slouken@2737
    94
int
slouken@2737
    95
DirectFB_GL_LoadLibrary(_THIS, const char *path)
slouken@2737
    96
{
slouken@2737
    97
    SDL_DFB_DEVICEDATA(_this);
slouken@2737
    98
slouken@2737
    99
    void *handle = NULL;
slouken@2737
   100
slouken@2737
   101
    SDL_DFB_DEBUG("Loadlibrary : %s\n", path);
slouken@2737
   102
slouken@2737
   103
    if (_this->gl_data->gl_active) {
slouken@2737
   104
        SDL_SetError("OpenGL context already created");
slouken@2737
   105
        return -1;
slouken@2737
   106
    }
slouken@2737
   107
slouken@2737
   108
slouken@2737
   109
    if (path == NULL) {
slouken@2737
   110
        path = SDL_getenv("SDL_VIDEO_GL_DRIVER");
slouken@2737
   111
        if (path == NULL) {
slouken@2737
   112
            path = "libGL.so";
slouken@2737
   113
        }
slouken@2737
   114
    }
slouken@2737
   115
slouken@2737
   116
    handle = GL_LoadObject(path);
slouken@2737
   117
    if (handle == NULL) {
slouken@2737
   118
        SDL_DFB_ERR("Library not found: %s\n", path);
slouken@2737
   119
        /* SDL_LoadObject() will call SDL_SetError() for us. */
slouken@2737
   120
        return -1;
slouken@2737
   121
    }
slouken@2737
   122
slouken@2737
   123
    SDL_DFB_DEBUG("Loaded library: %s\n", path);
slouken@2737
   124
slouken@2737
   125
    /* Unload the old driver and reset the pointers */
slouken@2737
   126
    DirectFB_GL_UnloadLibrary(_this);
slouken@2737
   127
slouken@2737
   128
    _this->gl_config.dll_handle = handle;
slouken@2737
   129
    _this->gl_config.driver_loaded = 1;
slouken@2737
   130
    if (path) {
slouken@2737
   131
        SDL_strlcpy(_this->gl_config.driver_path, path,
slouken@2737
   132
                    SDL_arraysize(_this->gl_config.driver_path));
slouken@2737
   133
    } else {
slouken@2737
   134
        *_this->gl_config.driver_path = '\0';
slouken@2737
   135
    }
slouken@2737
   136
slouken@2737
   137
    devdata->glFinish = DirectFB_GL_GetProcAddress(_this, "glFinish");
slouken@2737
   138
    devdata->glFlush = DirectFB_GL_GetProcAddress(_this, "glFlush");
slouken@2737
   139
slouken@2737
   140
    return 0;
slouken@2737
   141
}
slouken@2737
   142
slouken@2737
   143
static void
slouken@2737
   144
DirectFB_GL_UnloadLibrary(_THIS)
slouken@2737
   145
{
slouken@2737
   146
    int ret;
slouken@2737
   147
slouken@2737
   148
    if (_this->gl_config.driver_loaded) {
slouken@2737
   149
slouken@2737
   150
        ret = GL_UnloadObject(_this->gl_config.dll_handle);
slouken@2737
   151
        if (ret)
slouken@2737
   152
            SDL_DFB_ERR("Error #%d trying to unload library.\n", ret);
slouken@2737
   153
        _this->gl_config.dll_handle = NULL;
slouken@2737
   154
        _this->gl_config.driver_loaded = 0;
slouken@2737
   155
    }
slouken@2737
   156
}
slouken@2737
   157
slouken@2737
   158
void *
slouken@2737
   159
DirectFB_GL_GetProcAddress(_THIS, const char *proc)
slouken@2737
   160
{
slouken@2737
   161
    void *handle;
slouken@2737
   162
slouken@2737
   163
    handle = _this->gl_config.dll_handle;
slouken@2737
   164
    return GL_LoadFunction(handle, proc);
slouken@2737
   165
}
slouken@2737
   166
slouken@2737
   167
SDL_GLContext
slouken@2737
   168
DirectFB_GL_CreateContext(_THIS, SDL_Window * window)
slouken@2737
   169
{
slouken@2737
   170
    SDL_DFB_WINDOWDATA(window);
slouken@2737
   171
    DirectFB_GLContext *context;
slouken@2737
   172
    int ret;
slouken@2737
   173
slouken@2737
   174
    SDL_DFB_CALLOC(context, 1, sizeof(*context));
slouken@2737
   175
couriersud@3023
   176
    SDL_DFB_CHECKERR(windata->surface->GetGL(windata->surface,
couriersud@3023
   177
                                             &context->context));
slouken@2998
   178
slouken@2998
   179
    if (!context->context)
slouken@2998
   180
        return NULL;
slouken@2998
   181
slouken@2737
   182
    SDL_DFB_CHECKERR(context->context->Unlock(context->context));
slouken@2737
   183
slouken@2737
   184
    context->next = _this->gl_data->firstgl;
slouken@2737
   185
    _this->gl_data->firstgl = context;
slouken@2737
   186
slouken@2737
   187
    if (DirectFB_GL_MakeCurrent(_this, window, context) < 0) {
slouken@2737
   188
        DirectFB_GL_DeleteContext(_this, context);
slouken@2737
   189
        return NULL;
slouken@2737
   190
    }
slouken@2737
   191
slouken@2737
   192
    return context;
slouken@2737
   193
slouken@2737
   194
  error:
slouken@2737
   195
    return NULL;
slouken@2737
   196
}
slouken@2737
   197
slouken@2737
   198
int
slouken@2737
   199
DirectFB_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
slouken@2737
   200
{
slouken@2737
   201
    SDL_DFB_WINDOWDATA(window);
slouken@2737
   202
    DirectFB_GLContext *ctx = (DirectFB_GLContext *) context;
slouken@2737
   203
    DirectFB_GLContext *p;
slouken@2737
   204
slouken@2737
   205
    int ret;
slouken@2737
   206
slouken@2737
   207
    for (p = _this->gl_data->firstgl; p; p = p->next)
slouken@2737
   208
        p->context->Unlock(p->context);
slouken@2737
   209
slouken@2737
   210
    if (windata) {
slouken@2737
   211
        windata->gl_context = NULL;
slouken@2737
   212
        /* Everything is unlocked, check for a resize */
couriersud@3023
   213
        DirectFB_AdjustWindowSurface(window);
slouken@2737
   214
    }
slouken@2737
   215
slouken@2737
   216
    if (ctx != NULL) {
slouken@2737
   217
        SDL_DFB_CHECKERR(ctx->context->Lock(ctx->context));
slouken@2737
   218
    }
slouken@2737
   219
slouken@2737
   220
    if (windata)
slouken@2737
   221
        windata->gl_context = ctx;
slouken@2737
   222
slouken@2737
   223
    return 0;
slouken@2737
   224
  error:
slouken@2737
   225
    return -1;
slouken@2737
   226
}
slouken@2737
   227
slouken@2737
   228
int
slouken@2737
   229
DirectFB_GL_SetSwapInterval(_THIS, int interval)
slouken@2737
   230
{
slouken@2737
   231
    SDL_Unsupported();
slouken@2737
   232
    return -1;
slouken@2737
   233
}
slouken@2737
   234
slouken@2737
   235
int
slouken@2737
   236
DirectFB_GL_GetSwapInterval(_THIS)
slouken@2737
   237
{
slouken@2737
   238
    SDL_Unsupported();
slouken@2737
   239
    return -1;
slouken@2737
   240
}
slouken@2737
   241
slouken@2737
   242
void
slouken@2737
   243
DirectFB_GL_SwapWindow(_THIS, SDL_Window * window)
slouken@2737
   244
{
slouken@2737
   245
    SDL_DFB_DEVICEDATA(_this);
slouken@2737
   246
    SDL_DFB_WINDOWDATA(window);
slouken@2737
   247
    int ret;
slouken@2737
   248
    DFBRegion region;
slouken@2737
   249
slouken@2737
   250
    region.x1 = 0;
slouken@2737
   251
    region.y1 = 0;
slouken@2737
   252
    region.x2 = window->w;
slouken@2737
   253
    region.y2 = window->h;
slouken@2737
   254
slouken@2737
   255
    if (devdata->glFinish)
slouken@2737
   256
        devdata->glFinish();
slouken@2737
   257
    else if (devdata->glFlush)
slouken@2737
   258
        devdata->glFlush();
slouken@2737
   259
slouken@2737
   260
    if (1 || windata->gl_context) {
slouken@2737
   261
        /* SDL_DFB_CHECKERR(windata->gl_context->context->Unlock(windata->gl_context->context)); */
couriersud@3023
   262
        SDL_DFB_CHECKERR(windata->surface->Flip(windata->surface, &region,
couriersud@3023
   263
                                                DSFLIP_ONSYNC));
slouken@2737
   264
        /* SDL_DFB_CHECKERR(windata->gl_context->context->Lock(windata->gl_context->context)); */
slouken@2737
   265
slouken@2737
   266
    }
slouken@2737
   267
slouken@2737
   268
    return;
slouken@2737
   269
  error:
slouken@2737
   270
    return;
slouken@2737
   271
}
slouken@2737
   272
slouken@2737
   273
void
slouken@2737
   274
DirectFB_GL_DeleteContext(_THIS, SDL_GLContext context)
slouken@2737
   275
{
slouken@2737
   276
    DirectFB_GLContext *ctx = (DirectFB_GLContext *) context;
slouken@2737
   277
    DirectFB_GLContext *p;
slouken@2737
   278
slouken@2737
   279
    ctx->context->Unlock(ctx->context);
slouken@2737
   280
    ctx->context->Release(ctx->context);
slouken@2737
   281
slouken@2737
   282
    p = _this->gl_data->firstgl;
slouken@2737
   283
    while (p && p->next != ctx)
slouken@2737
   284
        p = p->next;
slouken@2737
   285
    if (p)
slouken@2737
   286
        p->next = ctx->next;
slouken@2737
   287
    else
slouken@2737
   288
        _this->gl_data->firstgl = ctx->next;
slouken@2737
   289
slouken@2737
   290
    SDL_DFB_FREE(ctx);
slouken@2737
   291
slouken@2737
   292
}
slouken@2737
   293
slouken@2737
   294
#endif
slouken@2737
   295
slouken@2737
   296
/* vi: set ts=4 sw=4 expandtab: */