src/video/x11/SDL_x11opengles.c
author Sam Lantinga
Wed, 10 Jun 2009 13:54:13 +0000
changeset 3190 c68d2ca5970f
child 3218 81773a1eac83
permissions -rw-r--r--
Added missing files for OpenGL ES support
slouken@3190
     1
/*
slouken@3190
     2
    SDL - Simple DirectMedia Layer
slouken@3190
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@3190
     4
slouken@3190
     5
    This library is free software; you can redistribute it and/or
slouken@3190
     6
    modify it under the terms of the GNU Lesser General Public
slouken@3190
     7
    License as published by the Free Software Foundation; either
slouken@3190
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@3190
     9
slouken@3190
    10
    This library is distributed in the hope that it will be useful,
slouken@3190
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@3190
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@3190
    13
    Lesser General Public License for more details.
slouken@3190
    14
slouken@3190
    15
    You should have received a copy of the GNU Lesser General Public
slouken@3190
    16
    License along with _this library; if not, write to the Free Software
slouken@3190
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@3190
    18
slouken@3190
    19
    Sam Lantinga
slouken@3190
    20
    slouken@libsdl.org
slouken@3190
    21
slouken@3190
    22
    Open Pandora SDL driver
slouken@3190
    23
    Copyright (C) 2009 David Carré
slouken@3190
    24
    (cpasjuste@gmail.com)
slouken@3190
    25
*/
slouken@3190
    26
#include "SDL_config.h"
slouken@3190
    27
slouken@3190
    28
#if SDL_VIDEO_OPENGL_ES
slouken@3190
    29
slouken@3190
    30
#include "SDL_x11video.h"
slouken@3190
    31
#include "SDL_x11opengles.h"
slouken@3190
    32
slouken@3190
    33
#define DEFAULT_OPENGL	"/usr/lib/libGLES_CM.so"
slouken@3190
    34
slouken@3190
    35
#define LOAD_FUNC(NAME) \
slouken@3190
    36
	*((void**)&_this->gles_data->NAME) = dlsym(handle, #NAME); \
slouken@3190
    37
	if (!_this->gles_data->NAME) \
slouken@3190
    38
	{ \
slouken@3190
    39
		SDL_SetError("Could not retrieve EGL function " #NAME); \
slouken@3190
    40
		return -1; \
slouken@3190
    41
	}
slouken@3190
    42
slouken@3190
    43
/* GLES implementation of SDL OpenGL support */
slouken@3190
    44
slouken@3190
    45
void *
slouken@3190
    46
X11_GLES_GetProcAddress(_THIS, const char *proc)
slouken@3190
    47
{
slouken@3190
    48
    static char procname[1024];
slouken@3190
    49
    void *handle;
slouken@3190
    50
    void *retval;
slouken@3190
    51
slouken@3190
    52
    handle = _this->gl_config.dll_handle;
slouken@3190
    53
    if (_this->gles_data->eglGetProcAddress) {
slouken@3190
    54
        retval = _this->gles_data->eglGetProcAddress(proc);
slouken@3190
    55
        if (retval) {
slouken@3190
    56
            return retval;
slouken@3190
    57
        }
slouken@3190
    58
    }
slouken@3190
    59
#if defined(__OpenBSD__) && !defined(__ELF__)
slouken@3190
    60
#undef dlsym(x,y);
slouken@3190
    61
#endif
slouken@3190
    62
    retval = dlsym(handle, proc);
slouken@3190
    63
    if (!retval && strlen(proc) <= 1022) {
slouken@3190
    64
        procname[0] = '_';
slouken@3190
    65
        strcpy(procname + 1, proc);
slouken@3190
    66
        retval = dlsym(handle, procname);
slouken@3190
    67
    }
slouken@3190
    68
    return retval;
slouken@3190
    69
}
slouken@3190
    70
slouken@3190
    71
void
slouken@3190
    72
X11_GLES_UnloadLibrary(_THIS)
slouken@3190
    73
{
slouken@3190
    74
    if (_this->gl_config.driver_loaded) {
slouken@3190
    75
        _this->gles_data->eglTerminate(_this->gles_data->egl_display);
slouken@3190
    76
slouken@3190
    77
        dlclose(_this->gl_config.dll_handle);
slouken@3190
    78
slouken@3190
    79
        _this->gles_data->eglGetProcAddress = NULL;
slouken@3190
    80
        _this->gles_data->eglChooseConfig = NULL;
slouken@3190
    81
        _this->gles_data->eglCreateContext = NULL;
slouken@3190
    82
        _this->gles_data->eglCreateWindowSurface = NULL;
slouken@3190
    83
        _this->gles_data->eglDestroyContext = NULL;
slouken@3190
    84
        _this->gles_data->eglDestroySurface = NULL;
slouken@3190
    85
        _this->gles_data->eglMakeCurrent = NULL;
slouken@3190
    86
        _this->gles_data->eglSwapBuffers = NULL;
slouken@3190
    87
        _this->gles_data->eglGetDisplay = NULL;
slouken@3190
    88
        _this->gles_data->eglTerminate = NULL;
slouken@3190
    89
slouken@3190
    90
        _this->gl_config.dll_handle = NULL;
slouken@3190
    91
        _this->gl_config.driver_loaded = 0;
slouken@3190
    92
    }
slouken@3190
    93
}
slouken@3190
    94
slouken@3190
    95
int
slouken@3190
    96
X11_GLES_LoadLibrary(_THIS, const char *path)
slouken@3190
    97
{
slouken@3190
    98
    void *handle;
slouken@3190
    99
    int dlopen_flags;
slouken@3190
   100
slouken@3190
   101
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@3190
   102
slouken@3190
   103
    if (_this->gles_data->egl_active) {
slouken@3190
   104
        SDL_SetError("OpenGL ES context already created");
slouken@3190
   105
        return -1;
slouken@3190
   106
    }
slouken@3190
   107
#ifdef RTLD_GLOBAL
slouken@3190
   108
    dlopen_flags = RTLD_LAZY | RTLD_GLOBAL;
slouken@3190
   109
#else
slouken@3190
   110
    dlopen_flags = RTLD_LAZY;
slouken@3190
   111
#endif
slouken@3190
   112
    handle = dlopen(path, dlopen_flags);
slouken@3190
   113
    /* Catch the case where the application isn't linked with EGL */
slouken@3190
   114
    if ((dlsym(handle, "eglChooseConfig") == NULL) && (path == NULL)) {
slouken@3190
   115
slouken@3190
   116
        dlclose(handle);
slouken@3190
   117
        path = getenv("SDL_VIDEO_GL_DRIVER");
slouken@3190
   118
        if (path == NULL) {
slouken@3190
   119
            path = DEFAULT_OPENGL;
slouken@3190
   120
        }
slouken@3190
   121
        handle = dlopen(path, dlopen_flags);
slouken@3190
   122
    }
slouken@3190
   123
slouken@3190
   124
    if (handle == NULL) {
slouken@3190
   125
        SDL_SetError("Could not load OpenGL ES/EGL library");
slouken@3190
   126
        return -1;
slouken@3190
   127
    }
slouken@3190
   128
slouken@3190
   129
    /* Unload the old driver and reset the pointers */
slouken@3190
   130
    X11_GLES_UnloadLibrary(_this);
slouken@3190
   131
slouken@3190
   132
    /* Load new function pointers */
slouken@3190
   133
    LOAD_FUNC(eglGetDisplay);
slouken@3190
   134
    LOAD_FUNC(eglInitialize);
slouken@3190
   135
    LOAD_FUNC(eglTerminate);
slouken@3190
   136
    LOAD_FUNC(eglGetProcAddress);
slouken@3190
   137
    LOAD_FUNC(eglChooseConfig);
slouken@3190
   138
    LOAD_FUNC(eglGetConfigAttrib);
slouken@3190
   139
    LOAD_FUNC(eglCreateContext);
slouken@3190
   140
    LOAD_FUNC(eglDestroyContext);
slouken@3190
   141
    LOAD_FUNC(eglCreateWindowSurface);
slouken@3190
   142
    LOAD_FUNC(eglDestroySurface);
slouken@3190
   143
    LOAD_FUNC(eglMakeCurrent);
slouken@3190
   144
    LOAD_FUNC(eglSwapBuffers);
slouken@3190
   145
slouken@3190
   146
    _this->gles_data->egl_display =
slouken@3190
   147
        _this->gles_data->eglGetDisplay((NativeDisplayType) data->display);
slouken@3190
   148
slouken@3190
   149
    if (!_this->gles_data->egl_display) {
slouken@3190
   150
        SDL_SetError("Could not get EGL display");
slouken@3190
   151
        return -1;
slouken@3190
   152
    }
slouken@3190
   153
slouken@3190
   154
    if (_this->gles_data->
slouken@3190
   155
        eglInitialize(_this->gles_data->egl_display, NULL,
slouken@3190
   156
                      NULL) != EGL_TRUE) {
slouken@3190
   157
        SDL_SetError("Could not initialize EGL");
slouken@3190
   158
        return -1;
slouken@3190
   159
    }
slouken@3190
   160
slouken@3190
   161
    _this->gl_config.dll_handle = handle;
slouken@3190
   162
    _this->gl_config.driver_loaded = 1;
slouken@3190
   163
slouken@3190
   164
    if (path) {
slouken@3190
   165
        strncpy(_this->gl_config.driver_path, path,
slouken@3190
   166
                sizeof(_this->gl_config.driver_path) - 1);
slouken@3190
   167
    } else {
slouken@3190
   168
        strcpy(_this->gl_config.driver_path, "");
slouken@3190
   169
    }
slouken@3190
   170
    return 0;
slouken@3190
   171
}
slouken@3190
   172
slouken@3190
   173
XVisualInfo *
slouken@3190
   174
X11_GLES_GetVisual(_THIS, Display * display, int screen)
slouken@3190
   175
{
slouken@3190
   176
    /* 64 seems nice. */
slouken@3190
   177
    EGLint attribs[64];
slouken@3190
   178
    EGLint found_configs = 0;
slouken@3190
   179
    VisualID visual_id;
slouken@3190
   180
    int i;
slouken@3190
   181
slouken@3190
   182
    /* load the gl driver from a default path */
slouken@3190
   183
    if (!_this->gl_config.driver_loaded) {
slouken@3190
   184
        /* no driver has been loaded, use default (ourselves) */
slouken@3190
   185
        if (X11_GLES_LoadLibrary(_this, NULL) < 0) {
slouken@3190
   186
            return NULL;
slouken@3190
   187
        }
slouken@3190
   188
    }
slouken@3190
   189
slouken@3190
   190
    i = 0;
slouken@3190
   191
    attribs[i++] = EGL_RED_SIZE;
slouken@3190
   192
    attribs[i++] = _this->gl_config.red_size;
slouken@3190
   193
    attribs[i++] = EGL_GREEN_SIZE;
slouken@3190
   194
    attribs[i++] = _this->gl_config.green_size;
slouken@3190
   195
    attribs[i++] = EGL_BLUE_SIZE;
slouken@3190
   196
    attribs[i++] = _this->gl_config.blue_size;
slouken@3190
   197
slouken@3190
   198
    if (_this->gl_config.alpha_size) {
slouken@3190
   199
        attribs[i++] = EGL_ALPHA_SIZE;
slouken@3190
   200
        attribs[i++] = _this->gl_config.alpha_size;
slouken@3190
   201
    }
slouken@3190
   202
slouken@3190
   203
    if (_this->gl_config.buffer_size) {
slouken@3190
   204
        attribs[i++] = EGL_BUFFER_SIZE;
slouken@3190
   205
        attribs[i++] = _this->gl_config.buffer_size;
slouken@3190
   206
    }
slouken@3190
   207
slouken@3190
   208
    attribs[i++] = EGL_DEPTH_SIZE;
slouken@3190
   209
    attribs[i++] = _this->gl_config.depth_size;
slouken@3190
   210
slouken@3190
   211
    if (_this->gl_config.stencil_size) {
slouken@3190
   212
        attribs[i++] = EGL_STENCIL_SIZE;
slouken@3190
   213
        attribs[i++] = _this->gl_config.stencil_size;
slouken@3190
   214
    }
slouken@3190
   215
slouken@3190
   216
    if (_this->gl_config.multisamplebuffers) {
slouken@3190
   217
        attribs[i++] = EGL_SAMPLE_BUFFERS;
slouken@3190
   218
        attribs[i++] = _this->gl_config.multisamplebuffers;
slouken@3190
   219
    }
slouken@3190
   220
slouken@3190
   221
    if (_this->gl_config.multisamplesamples) {
slouken@3190
   222
        attribs[i++] = EGL_SAMPLES;
slouken@3190
   223
        attribs[i++] = _this->gl_config.multisamplesamples;
slouken@3190
   224
    }
slouken@3190
   225
slouken@3190
   226
    attribs[i++] = EGL_NONE;
slouken@3190
   227
slouken@3190
   228
    if (_this->gles_data->eglChooseConfig(_this->gles_data->egl_display,
slouken@3190
   229
                                          attribs,
slouken@3190
   230
                                          &_this->gles_data->egl_config, 1,
slouken@3190
   231
                                          &found_configs) == EGL_FALSE ||
slouken@3190
   232
        found_configs == 0) {
slouken@3190
   233
        SDL_SetError("Couldn't find matching EGL config");
slouken@3190
   234
        return NULL;
slouken@3190
   235
    }
slouken@3190
   236
slouken@3190
   237
    if (_this->gles_data->eglGetConfigAttrib(_this->gles_data->egl_display,
slouken@3190
   238
                                             _this->gles_data->egl_config,
slouken@3190
   239
                                             EGL_NATIVE_VISUAL_ID,
slouken@3190
   240
                                             (EGLint *) & visual_id) ==
slouken@3190
   241
        EGL_FALSE || !visual_id) {
slouken@3190
   242
        /* Use the default visual when all else fails */
slouken@3190
   243
        XVisualInfo vi_in;
slouken@3190
   244
        int out_count;
slouken@3190
   245
        vi_in.screen = screen;
slouken@3190
   246
slouken@3190
   247
        _this->gles_data->egl_visualinfo = XGetVisualInfo(display,
slouken@3190
   248
                                                          VisualScreenMask,
slouken@3190
   249
                                                          &vi_in, &out_count);
slouken@3190
   250
    } else {
slouken@3190
   251
        XVisualInfo vi_in;
slouken@3190
   252
        int out_count;
slouken@3190
   253
slouken@3190
   254
        vi_in.screen = screen;
slouken@3190
   255
        vi_in.visualid = visual_id;
slouken@3190
   256
        _this->gles_data->egl_visualinfo = XGetVisualInfo(display,
slouken@3190
   257
                                                          VisualScreenMask |
slouken@3190
   258
                                                          VisualIDMask,
slouken@3190
   259
                                                          &vi_in, &out_count);
slouken@3190
   260
    }
slouken@3190
   261
slouken@3190
   262
    return _this->gles_data->egl_visualinfo;
slouken@3190
   263
}
slouken@3190
   264
slouken@3190
   265
SDL_GLContext
slouken@3190
   266
X11_GLES_CreateContext(_THIS, SDL_Window * window)
slouken@3190
   267
{
slouken@3190
   268
    int retval;
slouken@3190
   269
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@3190
   270
    Display *display = data->videodata->display;
slouken@3190
   271
slouken@3190
   272
    XSync(display, False);
slouken@3190
   273
slouken@3190
   274
slouken@3190
   275
    _this->gles_data->egl_context =
slouken@3190
   276
        _this->gles_data->eglCreateContext(_this->gles_data->egl_display,
slouken@3190
   277
                                           _this->gles_data->egl_config,
slouken@3190
   278
                                           EGL_NO_CONTEXT, NULL);
slouken@3190
   279
    XSync(display, False);
slouken@3190
   280
slouken@3190
   281
    if (_this->gles_data->egl_context == EGL_NO_CONTEXT) {
slouken@3190
   282
        SDL_SetError("Could not create EGL context");
slouken@3190
   283
        return NULL;
slouken@3190
   284
    }
slouken@3190
   285
slouken@3190
   286
    _this->gles_data->egl_active = 1;
slouken@3190
   287
slouken@3190
   288
    if (_this->gles_data->egl_active)
slouken@3190
   289
        retval = 1;
slouken@3190
   290
    else
slouken@3190
   291
        retval = 0;
slouken@3190
   292
slouken@3190
   293
    return (retval);
slouken@3190
   294
}
slouken@3190
   295
slouken@3190
   296
int
slouken@3190
   297
X11_GLES_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
slouken@3190
   298
{
slouken@3190
   299
    int retval;
slouken@3190
   300
slouken@3190
   301
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@3190
   302
    Display *display = data->videodata->display;
slouken@3190
   303
slouken@3190
   304
    retval = 1;
slouken@3190
   305
    if (!_this->gles_data->eglMakeCurrent(_this->gles_data->egl_display,
slouken@3190
   306
                                          _this->gles_data->egl_surface,
slouken@3190
   307
                                          _this->gles_data->egl_surface,
slouken@3190
   308
                                          _this->gles_data->egl_context)) {
slouken@3190
   309
        SDL_SetError("Unable to make EGL context current");
slouken@3190
   310
        retval = -1;
slouken@3190
   311
    }
slouken@3190
   312
    XSync(display, False);
slouken@3190
   313
slouken@3190
   314
    return (retval);
slouken@3190
   315
}
slouken@3190
   316
slouken@3190
   317
static int swapinterval = -1;
slouken@3190
   318
int
slouken@3190
   319
X11_GLES_SetSwapInterval(_THIS, int interval)
slouken@3190
   320
{
slouken@3190
   321
    return 0;
slouken@3190
   322
}
slouken@3190
   323
slouken@3190
   324
int
slouken@3190
   325
X11_GLES_GetSwapInterval(_THIS)
slouken@3190
   326
{
slouken@3190
   327
    return 0;
slouken@3190
   328
}
slouken@3190
   329
slouken@3190
   330
void
slouken@3190
   331
X11_GLES_SwapWindow(_THIS, SDL_Window * window)
slouken@3190
   332
{
slouken@3190
   333
    _this->gles_data->eglSwapBuffers(_this->gles_data->egl_display,
slouken@3190
   334
                                     _this->gles_data->egl_surface);
slouken@3190
   335
}
slouken@3190
   336
slouken@3190
   337
void
slouken@3190
   338
X11_GLES_DeleteContext(_THIS, SDL_GLContext context)
slouken@3190
   339
{
slouken@3190
   340
    /* Clean up GLES and EGL */
slouken@3190
   341
    if (_this->gles_data->egl_context != EGL_NO_CONTEXT ||
slouken@3190
   342
        _this->gles_data->egl_surface != EGL_NO_SURFACE) {
slouken@3190
   343
        _this->gles_data->eglMakeCurrent(_this->gles_data->egl_display,
slouken@3190
   344
                                         EGL_NO_SURFACE, EGL_NO_SURFACE,
slouken@3190
   345
                                         EGL_NO_CONTEXT);
slouken@3190
   346
slouken@3190
   347
        if (_this->gles_data->egl_context != EGL_NO_CONTEXT) {
slouken@3190
   348
            _this->gles_data->eglDestroyContext(_this->gles_data->egl_display,
slouken@3190
   349
                                                _this->gles_data->
slouken@3190
   350
                                                egl_context);
slouken@3190
   351
            _this->gles_data->egl_context = EGL_NO_CONTEXT;
slouken@3190
   352
        }
slouken@3190
   353
slouken@3190
   354
        if (_this->gles_data->egl_surface != EGL_NO_SURFACE) {
slouken@3190
   355
            _this->gles_data->eglDestroySurface(_this->gles_data->egl_display,
slouken@3190
   356
                                                _this->gles_data->
slouken@3190
   357
                                                egl_surface);
slouken@3190
   358
            _this->gles_data->egl_surface = EGL_NO_SURFACE;
slouken@3190
   359
        }
slouken@3190
   360
    }
slouken@3190
   361
    _this->gles_data->egl_active = 0;
slouken@3190
   362
}
slouken@3190
   363
slouken@3190
   364
#endif /* SDL_VIDEO_OPENGL_ES */
slouken@3190
   365
slouken@3190
   366
/* vi: set ts=4 sw=4 expandtab: */