src/video/directfb/SDL_DirectFB_opengl.c
author Sam Lantinga
Sun, 04 Jan 2009 23:43:33 +0000
changeset 2998 d364ee9b9c15
parent 2859 99210400e8b9
child 3011 8f4ed5ec2b06
permissions -rw-r--r--
Date: Sun, 04 Jan 2009 20:53:30 +0100
From: Couriersud
Subject: SDL1.3 DirectFB patches

The attached contains the following directfb changes:

- Dynamic loading of libdirectfb.so. This may to turned off as well in configure
- Use linux-input by default. May be switched off by environment variable.
- Added some code which will use directfb's x11 backend when DISPLAY is set.
slouken@2737
     1
/*
slouken@2737
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     3
    Copyright (C) 1997-2009 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
slouken@2737
   176
    SDL_DFB_CHECKERR(windata->surface->
slouken@2737
   177
                     GetGL(windata->surface, &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
        int cw, ch;
slouken@2737
   212
slouken@2737
   213
        windata->gl_context = NULL;
slouken@2737
   214
        /* Everything is unlocked, check for a resize */
slouken@2737
   215
        SDL_DFB_CHECKERR(windata->surface->
slouken@2737
   216
                         GetSize(windata->surface, &cw, &ch));
slouken@2737
   217
        if (cw != window->w || ch != window->h)
slouken@2737
   218
            SDL_DFB_CHECKERR(windata->window->
slouken@2737
   219
                             ResizeSurface(windata->window, window->w,
slouken@2737
   220
                                           window->h));
slouken@2737
   221
    }
slouken@2737
   222
slouken@2737
   223
    if (ctx != NULL) {
slouken@2737
   224
        SDL_DFB_CHECKERR(ctx->context->Lock(ctx->context));
slouken@2737
   225
    }
slouken@2737
   226
slouken@2737
   227
    if (windata)
slouken@2737
   228
        windata->gl_context = ctx;
slouken@2737
   229
slouken@2737
   230
    return 0;
slouken@2737
   231
  error:
slouken@2737
   232
    return -1;
slouken@2737
   233
}
slouken@2737
   234
slouken@2737
   235
int
slouken@2737
   236
DirectFB_GL_SetSwapInterval(_THIS, int interval)
slouken@2737
   237
{
slouken@2737
   238
    SDL_Unsupported();
slouken@2737
   239
    return -1;
slouken@2737
   240
}
slouken@2737
   241
slouken@2737
   242
int
slouken@2737
   243
DirectFB_GL_GetSwapInterval(_THIS)
slouken@2737
   244
{
slouken@2737
   245
    SDL_Unsupported();
slouken@2737
   246
    return -1;
slouken@2737
   247
}
slouken@2737
   248
slouken@2737
   249
void
slouken@2737
   250
DirectFB_GL_SwapWindow(_THIS, SDL_Window * window)
slouken@2737
   251
{
slouken@2737
   252
    SDL_DFB_DEVICEDATA(_this);
slouken@2737
   253
    SDL_DFB_WINDOWDATA(window);
slouken@2737
   254
    int ret;
slouken@2737
   255
    DFBRegion region;
slouken@2737
   256
slouken@2737
   257
    region.x1 = 0;
slouken@2737
   258
    region.y1 = 0;
slouken@2737
   259
    region.x2 = window->w;
slouken@2737
   260
    region.y2 = window->h;
slouken@2737
   261
slouken@2737
   262
    if (devdata->glFinish)
slouken@2737
   263
        devdata->glFinish();
slouken@2737
   264
    else if (devdata->glFlush)
slouken@2737
   265
        devdata->glFlush();
slouken@2737
   266
slouken@2737
   267
    if (1 || windata->gl_context) {
slouken@2737
   268
        /* SDL_DFB_CHECKERR(windata->gl_context->context->Unlock(windata->gl_context->context)); */
slouken@2737
   269
        SDL_DFB_CHECKERR(windata->surface->
slouken@2737
   270
                         Flip(windata->surface, &region, DSFLIP_ONSYNC));
slouken@2737
   271
        /* SDL_DFB_CHECKERR(windata->gl_context->context->Lock(windata->gl_context->context)); */
slouken@2737
   272
slouken@2737
   273
    }
slouken@2737
   274
slouken@2737
   275
    return;
slouken@2737
   276
  error:
slouken@2737
   277
    return;
slouken@2737
   278
}
slouken@2737
   279
slouken@2737
   280
void
slouken@2737
   281
DirectFB_GL_DeleteContext(_THIS, SDL_GLContext context)
slouken@2737
   282
{
slouken@2737
   283
    DirectFB_GLContext *ctx = (DirectFB_GLContext *) context;
slouken@2737
   284
    DirectFB_GLContext *p;
slouken@2737
   285
slouken@2737
   286
    ctx->context->Unlock(ctx->context);
slouken@2737
   287
    ctx->context->Release(ctx->context);
slouken@2737
   288
slouken@2737
   289
    p = _this->gl_data->firstgl;
slouken@2737
   290
    while (p && p->next != ctx)
slouken@2737
   291
        p = p->next;
slouken@2737
   292
    if (p)
slouken@2737
   293
        p->next = ctx->next;
slouken@2737
   294
    else
slouken@2737
   295
        _this->gl_data->firstgl = ctx->next;
slouken@2737
   296
slouken@2737
   297
    SDL_DFB_FREE(ctx);
slouken@2737
   298
slouken@2737
   299
}
slouken@2737
   300
slouken@2737
   301
#endif
slouken@2737
   302
slouken@2737
   303
/* vi: set ts=4 sw=4 expandtab: */