src/video/directfb/SDL_DirectFB_opengl.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 09 Dec 2016 01:47:43 -0800
changeset 10690 23a825f341e6
parent 9998 f67cf37e9cd4
child 10737 3406a0f8b041
permissions -rw-r--r--
Fixed bug 3513 - SDL_GL_SwapWindow does not return error status

Return an error code from SDL_GL_SwapWindow(), like the other SDL APIs.
slouken@2737
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@9998
     3
  Copyright (C) 1997-2016 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
*/
icculus@8093
    21
#include "../../SDL_internal.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@7191
    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@7191
    52
#define GL_LoadObject(X)    dlopen(X, (RTLD_NOW|RTLD_GLOBAL))
slouken@7191
    53
#define GL_LoadFunction     dlsym
slouken@7191
    54
#define GL_UnloadObject     dlclose
slouken@2737
    55
#else
slouken@7191
    56
#define GL_LoadObject   SDL_LoadObject
slouken@7191
    57
#define GL_LoadFunction SDL_LoadFunction
slouken@7191
    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@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) {
icculus@7037
   115
        return SDL_SetError("OpenGL context already created");
slouken@2737
   116
    }
slouken@2737
   117
slouken@2737
   118
slouken@2737
   119
    if (path == NULL) {
slouken@2737
   120
        path = SDL_getenv("SDL_VIDEO_GL_DRIVER");
slouken@2737
   121
        if (path == NULL) {
slouken@2737
   122
            path = "libGL.so";
slouken@2737
   123
        }
slouken@2737
   124
    }
slouken@2737
   125
slouken@2737
   126
    handle = GL_LoadObject(path);
slouken@2737
   127
    if (handle == NULL) {
slouken@2737
   128
        SDL_DFB_ERR("Library not found: %s\n", path);
slouken@2737
   129
        /* SDL_LoadObject() will call SDL_SetError() for us. */
slouken@2737
   130
        return -1;
slouken@2737
   131
    }
slouken@2737
   132
slouken@2737
   133
    SDL_DFB_DEBUG("Loaded library: %s\n", path);
slouken@2737
   134
slouken@2737
   135
    _this->gl_config.dll_handle = handle;
slouken@2737
   136
    _this->gl_config.driver_loaded = 1;
slouken@2737
   137
    if (path) {
slouken@2737
   138
        SDL_strlcpy(_this->gl_config.driver_path, path,
slouken@2737
   139
                    SDL_arraysize(_this->gl_config.driver_path));
slouken@2737
   140
    } else {
slouken@2737
   141
        *_this->gl_config.driver_path = '\0';
slouken@2737
   142
    }
slouken@2737
   143
slouken@4636
   144
    _this->gl_data->glFinish = DirectFB_GL_GetProcAddress(_this, "glFinish");
slouken@4636
   145
    _this->gl_data->glFlush = DirectFB_GL_GetProcAddress(_this, "glFlush");
slouken@2737
   146
slouken@2737
   147
    return 0;
slouken@2737
   148
}
slouken@2737
   149
slouken@2737
   150
static void
slouken@2737
   151
DirectFB_GL_UnloadLibrary(_THIS)
slouken@2737
   152
{
slouken@4636
   153
 #if 0
slouken@2737
   154
    int ret;
slouken@2737
   155
slouken@2737
   156
    if (_this->gl_config.driver_loaded) {
slouken@2737
   157
slouken@2737
   158
        ret = GL_UnloadObject(_this->gl_config.dll_handle);
slouken@2737
   159
        if (ret)
slouken@2737
   160
            SDL_DFB_ERR("Error #%d trying to unload library.\n", ret);
slouken@2737
   161
        _this->gl_config.dll_handle = NULL;
slouken@2737
   162
        _this->gl_config.driver_loaded = 0;
slouken@2737
   163
    }
slouken@4636
   164
#endif
slouken@4636
   165
    /* Free OpenGL memory */
slouken@4636
   166
    SDL_free(_this->gl_data);
slouken@4636
   167
    _this->gl_data = NULL;
slouken@2737
   168
}
slouken@2737
   169
slouken@2737
   170
void *
slouken@2737
   171
DirectFB_GL_GetProcAddress(_THIS, const char *proc)
slouken@2737
   172
{
slouken@2737
   173
    void *handle;
slouken@2737
   174
slouken@2737
   175
    handle = _this->gl_config.dll_handle;
slouken@2737
   176
    return GL_LoadFunction(handle, proc);
slouken@2737
   177
}
slouken@2737
   178
slouken@2737
   179
SDL_GLContext
slouken@2737
   180
DirectFB_GL_CreateContext(_THIS, SDL_Window * window)
slouken@2737
   181
{
slouken@2737
   182
    SDL_DFB_WINDOWDATA(window);
slouken@2737
   183
    DirectFB_GLContext *context;
slouken@2737
   184
slouken@5199
   185
    SDL_DFB_ALLOC_CLEAR(context, sizeof(DirectFB_GLContext));
slouken@2737
   186
couriersud@3023
   187
    SDL_DFB_CHECKERR(windata->surface->GetGL(windata->surface,
couriersud@3023
   188
                                             &context->context));
slouken@2998
   189
slouken@2998
   190
    if (!context->context)
slouken@2998
   191
        return NULL;
slouken@2998
   192
slouken@4636
   193
    context->is_locked = 0;
slouken@4636
   194
    context->sdl_window = window;
slouken@7191
   195
slouken@2737
   196
    context->next = _this->gl_data->firstgl;
slouken@2737
   197
    _this->gl_data->firstgl = context;
slouken@2737
   198
slouken@4636
   199
    SDL_DFB_CHECK(context->context->Unlock(context->context));
slouken@4636
   200
slouken@2737
   201
    if (DirectFB_GL_MakeCurrent(_this, window, context) < 0) {
slouken@2737
   202
        DirectFB_GL_DeleteContext(_this, context);
slouken@2737
   203
        return NULL;
slouken@2737
   204
    }
slouken@2737
   205
slouken@2737
   206
    return context;
slouken@2737
   207
slouken@2737
   208
  error:
slouken@2737
   209
    return NULL;
slouken@2737
   210
}
slouken@2737
   211
slouken@2737
   212
int
slouken@2737
   213
DirectFB_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
slouken@2737
   214
{
slouken@2737
   215
    DirectFB_GLContext *ctx = (DirectFB_GLContext *) context;
slouken@2737
   216
    DirectFB_GLContext *p;
slouken@2737
   217
slouken@2737
   218
    for (p = _this->gl_data->firstgl; p; p = p->next)
slouken@4636
   219
    {
slouken@4636
   220
       if (p->is_locked) {
slouken@4636
   221
         SDL_DFB_CHECKERR(p->context->Unlock(p->context));
slouken@4636
   222
         p->is_locked = 0;
slouken@4636
   223
       }
slouken@7191
   224
slouken@2737
   225
    }
slouken@2737
   226
slouken@2737
   227
    if (ctx != NULL) {
slouken@2737
   228
        SDL_DFB_CHECKERR(ctx->context->Lock(ctx->context));
slouken@4636
   229
        ctx->is_locked = 1;
slouken@2737
   230
    }
slouken@2737
   231
slouken@2737
   232
    return 0;
slouken@2737
   233
  error:
slouken@2737
   234
    return -1;
slouken@2737
   235
}
slouken@2737
   236
slouken@2737
   237
int
slouken@2737
   238
DirectFB_GL_SetSwapInterval(_THIS, int interval)
slouken@2737
   239
{
icculus@7037
   240
    return SDL_Unsupported();
slouken@2737
   241
}
slouken@2737
   242
slouken@2737
   243
int
slouken@2737
   244
DirectFB_GL_GetSwapInterval(_THIS)
slouken@2737
   245
{
icculus@6382
   246
    return 0;
slouken@2737
   247
}
slouken@2737
   248
slouken@10690
   249
int
slouken@2737
   250
DirectFB_GL_SwapWindow(_THIS, SDL_Window * window)
slouken@2737
   251
{
slouken@2737
   252
    SDL_DFB_WINDOWDATA(window);
slouken@2737
   253
    DFBRegion region;
slouken@4636
   254
    DirectFB_GLContext *p;
slouken@2737
   255
slouken@2737
   256
    region.x1 = 0;
slouken@2737
   257
    region.y1 = 0;
slouken@2737
   258
    region.x2 = window->w;
slouken@2737
   259
    region.y2 = window->h;
slouken@2737
   260
slouken@4636
   261
#if 0
slouken@2737
   262
    if (devdata->glFinish)
slouken@2737
   263
        devdata->glFinish();
slouken@2737
   264
    else if (devdata->glFlush)
slouken@2737
   265
        devdata->glFlush();
slouken@4636
   266
#endif
slouken@2737
   267
slouken@7191
   268
    for (p = _this->gl_data->firstgl; p != NULL; p = p->next)
slouken@4636
   269
        if (p->sdl_window == window && p->is_locked)
slouken@4636
   270
        {
slouken@4636
   271
            SDL_DFB_CHECKERR(p->context->Unlock(p->context));
slouken@4636
   272
            p->is_locked = 0;
slouken@7191
   273
        }
slouken@2737
   274
slouken@4636
   275
    SDL_DFB_CHECKERR(windata->window_surface->Flip(windata->window_surface,NULL,  DSFLIP_PIPELINE |DSFLIP_BLIT | DSFLIP_ONSYNC ));
slouken@10690
   276
    return 0;
slouken@2737
   277
  error:
slouken@10690
   278
    return -1;
slouken@2737
   279
}
slouken@2737
   280
slouken@2737
   281
void
slouken@2737
   282
DirectFB_GL_DeleteContext(_THIS, SDL_GLContext context)
slouken@2737
   283
{
slouken@2737
   284
    DirectFB_GLContext *ctx = (DirectFB_GLContext *) context;
slouken@2737
   285
    DirectFB_GLContext *p;
slouken@2737
   286
slouken@4636
   287
    if (ctx->is_locked)
slouken@4636
   288
        SDL_DFB_CHECK(ctx->context->Unlock(ctx->context));
slouken@4636
   289
    SDL_DFB_RELEASE(ctx->context);
slouken@2737
   290
slouken@4636
   291
    for (p = _this->gl_data->firstgl; p && p->next != ctx; p = p->next)
slouken@4636
   292
        ;
slouken@2737
   293
    if (p)
slouken@2737
   294
        p->next = ctx->next;
slouken@2737
   295
    else
slouken@2737
   296
        _this->gl_data->firstgl = ctx->next;
slouken@2737
   297
slouken@2737
   298
    SDL_DFB_FREE(ctx);
slouken@4636
   299
}
slouken@2737
   300
slouken@4636
   301
void
slouken@4636
   302
DirectFB_GL_FreeWindowContexts(_THIS, SDL_Window * window)
slouken@4636
   303
{
slouken@4636
   304
    DirectFB_GLContext *p;
slouken@7191
   305
slouken@7191
   306
    for (p = _this->gl_data->firstgl; p != NULL; p = p->next)
slouken@7191
   307
        if (p->sdl_window == window)
slouken@7191
   308
        {
slouken@7191
   309
            if (p->is_locked)
slouken@7191
   310
                SDL_DFB_CHECK(p->context->Unlock(p->context));
slouken@7191
   311
            SDL_DFB_RELEASE(p->context);
slouken@7191
   312
        }
slouken@4636
   313
}
slouken@4636
   314
slouken@4636
   315
void
slouken@4636
   316
DirectFB_GL_ReAllocWindowContexts(_THIS, SDL_Window * window)
slouken@4636
   317
{
slouken@4636
   318
    DirectFB_GLContext *p;
slouken@4636
   319
slouken@7191
   320
    for (p = _this->gl_data->firstgl; p != NULL; p = p->next)
slouken@7191
   321
        if (p->sdl_window == window)
slouken@7191
   322
        {
slouken@4636
   323
            SDL_DFB_WINDOWDATA(window);
slouken@7191
   324
            SDL_DFB_CHECK(windata->surface->GetGL(windata->surface,
slouken@7191
   325
                                             &p->context));
slouken@7191
   326
            if (p->is_locked)
slouken@7191
   327
                SDL_DFB_CHECK(p->context->Lock(p->context));
slouken@7191
   328
            }
slouken@4636
   329
}
slouken@4636
   330
slouken@4636
   331
void
slouken@4636
   332
DirectFB_GL_DestroyWindowContexts(_THIS, SDL_Window * window)
slouken@4636
   333
{
slouken@4636
   334
    DirectFB_GLContext *p;
slouken@4636
   335
slouken@7191
   336
    for (p = _this->gl_data->firstgl; p != NULL; p = p->next)
slouken@7191
   337
        if (p->sdl_window == window)
slouken@7191
   338
            DirectFB_GL_DeleteContext(_this, p);
slouken@2737
   339
}
slouken@2737
   340
slouken@2737
   341
#endif
slouken@2737
   342
slouken@6044
   343
#endif /* SDL_VIDEO_DRIVER_DIRECTFB */
slouken@6044
   344
slouken@2737
   345
/* vi: set ts=4 sw=4 expandtab: */