src/video/directfb/SDL_DirectFB_opengl.c
author Sam Lantinga
Wed, 18 Jan 2017 12:19:57 -0800
changeset 10821 b0b8395f5cf9
parent 10741 a32fd6b7412b
child 10998 cedca11e9b61
permissions -rw-r--r--
Removed unused variable
slouken@2737
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 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
    if (path) {
slouken@2737
   137
        SDL_strlcpy(_this->gl_config.driver_path, path,
slouken@2737
   138
                    SDL_arraysize(_this->gl_config.driver_path));
slouken@2737
   139
    } else {
slouken@2737
   140
        *_this->gl_config.driver_path = '\0';
slouken@2737
   141
    }
slouken@2737
   142
slouken@4636
   143
    _this->gl_data->glFinish = DirectFB_GL_GetProcAddress(_this, "glFinish");
slouken@4636
   144
    _this->gl_data->glFlush = DirectFB_GL_GetProcAddress(_this, "glFlush");
slouken@2737
   145
slouken@2737
   146
    return 0;
slouken@2737
   147
}
slouken@2737
   148
slouken@2737
   149
static void
slouken@2737
   150
DirectFB_GL_UnloadLibrary(_THIS)
slouken@2737
   151
{
slouken@4636
   152
 #if 0
slouken@10741
   153
    int ret = GL_UnloadObject(_this->gl_config.dll_handle);
slouken@10741
   154
    if (ret)
slouken@10741
   155
        SDL_DFB_ERR("Error #%d trying to unload library.\n", ret);
slouken@10741
   156
    _this->gl_config.dll_handle = NULL;
slouken@4636
   157
#endif
slouken@4636
   158
    /* Free OpenGL memory */
slouken@4636
   159
    SDL_free(_this->gl_data);
slouken@4636
   160
    _this->gl_data = NULL;
slouken@2737
   161
}
slouken@2737
   162
slouken@2737
   163
void *
slouken@2737
   164
DirectFB_GL_GetProcAddress(_THIS, const char *proc)
slouken@2737
   165
{
slouken@2737
   166
    void *handle;
slouken@2737
   167
slouken@2737
   168
    handle = _this->gl_config.dll_handle;
slouken@2737
   169
    return GL_LoadFunction(handle, proc);
slouken@2737
   170
}
slouken@2737
   171
slouken@2737
   172
SDL_GLContext
slouken@2737
   173
DirectFB_GL_CreateContext(_THIS, SDL_Window * window)
slouken@2737
   174
{
slouken@2737
   175
    SDL_DFB_WINDOWDATA(window);
slouken@2737
   176
    DirectFB_GLContext *context;
slouken@2737
   177
slouken@5199
   178
    SDL_DFB_ALLOC_CLEAR(context, sizeof(DirectFB_GLContext));
slouken@2737
   179
couriersud@3023
   180
    SDL_DFB_CHECKERR(windata->surface->GetGL(windata->surface,
couriersud@3023
   181
                                             &context->context));
slouken@2998
   182
slouken@2998
   183
    if (!context->context)
slouken@2998
   184
        return NULL;
slouken@2998
   185
slouken@4636
   186
    context->is_locked = 0;
slouken@4636
   187
    context->sdl_window = window;
slouken@7191
   188
slouken@2737
   189
    context->next = _this->gl_data->firstgl;
slouken@2737
   190
    _this->gl_data->firstgl = context;
slouken@2737
   191
slouken@4636
   192
    SDL_DFB_CHECK(context->context->Unlock(context->context));
slouken@4636
   193
slouken@2737
   194
    if (DirectFB_GL_MakeCurrent(_this, window, context) < 0) {
slouken@2737
   195
        DirectFB_GL_DeleteContext(_this, context);
slouken@2737
   196
        return NULL;
slouken@2737
   197
    }
slouken@2737
   198
slouken@2737
   199
    return context;
slouken@2737
   200
slouken@2737
   201
  error:
slouken@2737
   202
    return NULL;
slouken@2737
   203
}
slouken@2737
   204
slouken@2737
   205
int
slouken@2737
   206
DirectFB_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
slouken@2737
   207
{
slouken@2737
   208
    DirectFB_GLContext *ctx = (DirectFB_GLContext *) context;
slouken@2737
   209
    DirectFB_GLContext *p;
slouken@2737
   210
slouken@2737
   211
    for (p = _this->gl_data->firstgl; p; p = p->next)
slouken@4636
   212
    {
slouken@4636
   213
       if (p->is_locked) {
slouken@4636
   214
         SDL_DFB_CHECKERR(p->context->Unlock(p->context));
slouken@4636
   215
         p->is_locked = 0;
slouken@4636
   216
       }
slouken@7191
   217
slouken@2737
   218
    }
slouken@2737
   219
slouken@2737
   220
    if (ctx != NULL) {
slouken@2737
   221
        SDL_DFB_CHECKERR(ctx->context->Lock(ctx->context));
slouken@4636
   222
        ctx->is_locked = 1;
slouken@2737
   223
    }
slouken@2737
   224
slouken@2737
   225
    return 0;
slouken@2737
   226
  error:
slouken@2737
   227
    return -1;
slouken@2737
   228
}
slouken@2737
   229
slouken@2737
   230
int
slouken@2737
   231
DirectFB_GL_SetSwapInterval(_THIS, int interval)
slouken@2737
   232
{
icculus@7037
   233
    return SDL_Unsupported();
slouken@2737
   234
}
slouken@2737
   235
slouken@2737
   236
int
slouken@2737
   237
DirectFB_GL_GetSwapInterval(_THIS)
slouken@2737
   238
{
icculus@6382
   239
    return 0;
slouken@2737
   240
}
slouken@2737
   241
slouken@10690
   242
int
slouken@2737
   243
DirectFB_GL_SwapWindow(_THIS, SDL_Window * window)
slouken@2737
   244
{
slouken@2737
   245
    SDL_DFB_WINDOWDATA(window);
slouken@2737
   246
    DFBRegion region;
slouken@4636
   247
    DirectFB_GLContext *p;
slouken@2737
   248
slouken@2737
   249
    region.x1 = 0;
slouken@2737
   250
    region.y1 = 0;
slouken@2737
   251
    region.x2 = window->w;
slouken@2737
   252
    region.y2 = window->h;
slouken@2737
   253
slouken@4636
   254
#if 0
slouken@2737
   255
    if (devdata->glFinish)
slouken@2737
   256
        devdata->glFinish();
slouken@2737
   257
    else if (devdata->glFlush)
slouken@2737
   258
        devdata->glFlush();
slouken@4636
   259
#endif
slouken@2737
   260
slouken@7191
   261
    for (p = _this->gl_data->firstgl; p != NULL; p = p->next)
slouken@4636
   262
        if (p->sdl_window == window && p->is_locked)
slouken@4636
   263
        {
slouken@4636
   264
            SDL_DFB_CHECKERR(p->context->Unlock(p->context));
slouken@4636
   265
            p->is_locked = 0;
slouken@7191
   266
        }
slouken@2737
   267
slouken@4636
   268
    SDL_DFB_CHECKERR(windata->window_surface->Flip(windata->window_surface,NULL,  DSFLIP_PIPELINE |DSFLIP_BLIT | DSFLIP_ONSYNC ));
slouken@10690
   269
    return 0;
slouken@2737
   270
  error:
slouken@10690
   271
    return -1;
slouken@2737
   272
}
slouken@2737
   273
slouken@2737
   274
void
slouken@2737
   275
DirectFB_GL_DeleteContext(_THIS, SDL_GLContext context)
slouken@2737
   276
{
slouken@2737
   277
    DirectFB_GLContext *ctx = (DirectFB_GLContext *) context;
slouken@2737
   278
    DirectFB_GLContext *p;
slouken@2737
   279
slouken@4636
   280
    if (ctx->is_locked)
slouken@4636
   281
        SDL_DFB_CHECK(ctx->context->Unlock(ctx->context));
slouken@4636
   282
    SDL_DFB_RELEASE(ctx->context);
slouken@2737
   283
slouken@4636
   284
    for (p = _this->gl_data->firstgl; p && p->next != ctx; p = p->next)
slouken@4636
   285
        ;
slouken@2737
   286
    if (p)
slouken@2737
   287
        p->next = ctx->next;
slouken@2737
   288
    else
slouken@2737
   289
        _this->gl_data->firstgl = ctx->next;
slouken@2737
   290
slouken@2737
   291
    SDL_DFB_FREE(ctx);
slouken@4636
   292
}
slouken@2737
   293
slouken@4636
   294
void
slouken@4636
   295
DirectFB_GL_FreeWindowContexts(_THIS, SDL_Window * window)
slouken@4636
   296
{
slouken@4636
   297
    DirectFB_GLContext *p;
slouken@7191
   298
slouken@7191
   299
    for (p = _this->gl_data->firstgl; p != NULL; p = p->next)
slouken@7191
   300
        if (p->sdl_window == window)
slouken@7191
   301
        {
slouken@7191
   302
            if (p->is_locked)
slouken@7191
   303
                SDL_DFB_CHECK(p->context->Unlock(p->context));
slouken@7191
   304
            SDL_DFB_RELEASE(p->context);
slouken@7191
   305
        }
slouken@4636
   306
}
slouken@4636
   307
slouken@4636
   308
void
slouken@4636
   309
DirectFB_GL_ReAllocWindowContexts(_THIS, SDL_Window * window)
slouken@4636
   310
{
slouken@4636
   311
    DirectFB_GLContext *p;
slouken@4636
   312
slouken@7191
   313
    for (p = _this->gl_data->firstgl; p != NULL; p = p->next)
slouken@7191
   314
        if (p->sdl_window == window)
slouken@7191
   315
        {
slouken@4636
   316
            SDL_DFB_WINDOWDATA(window);
slouken@7191
   317
            SDL_DFB_CHECK(windata->surface->GetGL(windata->surface,
slouken@7191
   318
                                             &p->context));
slouken@7191
   319
            if (p->is_locked)
slouken@7191
   320
                SDL_DFB_CHECK(p->context->Lock(p->context));
slouken@7191
   321
            }
slouken@4636
   322
}
slouken@4636
   323
slouken@4636
   324
void
slouken@4636
   325
DirectFB_GL_DestroyWindowContexts(_THIS, SDL_Window * window)
slouken@4636
   326
{
slouken@4636
   327
    DirectFB_GLContext *p;
slouken@4636
   328
slouken@7191
   329
    for (p = _this->gl_data->firstgl; p != NULL; p = p->next)
slouken@7191
   330
        if (p->sdl_window == window)
slouken@7191
   331
            DirectFB_GL_DeleteContext(_this, p);
slouken@2737
   332
}
slouken@2737
   333
slouken@2737
   334
#endif
slouken@2737
   335
slouken@6044
   336
#endif /* SDL_VIDEO_DRIVER_DIRECTFB */
slouken@6044
   337
slouken@2737
   338
/* vi: set ts=4 sw=4 expandtab: */