src/video/x11/SDL_x11opengl.c
author Sven Hesse <drmccoy@drmccoy.de>
Sun, 22 Oct 2017 20:24:58 +0200
changeset 11645 2088cd828335
parent 11441 f9cf3fb0b2f7
child 11811 5d94cb6b24d3
permissions -rw-r--r--
Don't X error in SDL_CreateWindow with unsupported GL attributes
slouken@1952
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
slouken@1952
     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@1952
     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@1952
    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@1952
    20
*/
icculus@8093
    21
#include "../../SDL_internal.h"
slouken@6044
    22
slouken@6044
    23
#if SDL_VIDEO_DRIVER_X11
slouken@6044
    24
slouken@1952
    25
#include "SDL_x11video.h"
icculus@5981
    26
#include "SDL_assert.h"
slouken@10806
    27
#include "SDL_hints.h"
slouken@1952
    28
slouken@1952
    29
/* GLX implementation of SDL OpenGL support */
slouken@1952
    30
slouken@1952
    31
#if SDL_VIDEO_OPENGL_GLX
slouken@1952
    32
#include "SDL_loadso.h"
slouken@6370
    33
#include "SDL_x11opengles.h"
slouken@1952
    34
slouken@1952
    35
#if defined(__IRIX__)
slouken@1952
    36
/* IRIX doesn't have a GL library versioning system */
slouken@7191
    37
#define DEFAULT_OPENGL  "libGL.so"
slouken@1952
    38
#elif defined(__MACOSX__)
slouken@7191
    39
#define DEFAULT_OPENGL  "/usr/X11R6/lib/libGL.1.dylib"
slouken@1952
    40
#elif defined(__QNXNTO__)
slouken@7191
    41
#define DEFAULT_OPENGL  "libGL.so.3"
slouken@1952
    42
#else
slouken@7191
    43
#define DEFAULT_OPENGL  "libGL.so.1"
slouken@1952
    44
#endif
slouken@1952
    45
slouken@3241
    46
#ifndef GLX_NONE_EXT
slouken@3241
    47
#define GLX_NONE_EXT                       0x8000
slouken@3241
    48
#endif
slouken@3241
    49
slouken@1952
    50
#ifndef GLX_ARB_multisample
slouken@1952
    51
#define GLX_ARB_multisample
slouken@1952
    52
#define GLX_SAMPLE_BUFFERS_ARB             100000
slouken@1952
    53
#define GLX_SAMPLES_ARB                    100001
slouken@1952
    54
#endif
slouken@1952
    55
slouken@1952
    56
#ifndef GLX_EXT_visual_rating
slouken@1952
    57
#define GLX_EXT_visual_rating
slouken@1952
    58
#define GLX_VISUAL_CAVEAT_EXT              0x20
slouken@1952
    59
#define GLX_NONE_EXT                       0x8000
slouken@1952
    60
#define GLX_SLOW_VISUAL_EXT                0x8001
slouken@1952
    61
#define GLX_NON_CONFORMANT_VISUAL_EXT      0x800D
slouken@1952
    62
#endif
slouken@1952
    63
mikesart@6570
    64
#ifndef GLX_EXT_visual_info
mikesart@6570
    65
#define GLX_EXT_visual_info
mikesart@6570
    66
#define GLX_X_VISUAL_TYPE_EXT              0x22
mikesart@6570
    67
#define GLX_DIRECT_COLOR_EXT               0x8003
mikesart@6570
    68
#endif
mikesart@6570
    69
slouken@3100
    70
#ifndef GLX_ARB_create_context
slouken@3100
    71
#define GLX_ARB_create_context
slouken@3100
    72
#define GLX_CONTEXT_MAJOR_VERSION_ARB      0x2091
slouken@3100
    73
#define GLX_CONTEXT_MINOR_VERSION_ARB      0x2092
slouken@3100
    74
#define GLX_CONTEXT_FLAGS_ARB              0x2094
slouken@3100
    75
#define GLX_CONTEXT_DEBUG_BIT_ARB          0x0001
slouken@3100
    76
#define GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x0002
slouken@5021
    77
slouken@5021
    78
/* Typedef for the GL 3.0 context creation function */
slouken@5021
    79
typedef GLXContext(*PFNGLXCREATECONTEXTATTRIBSARBPROC) (Display * dpy,
slouken@5021
    80
                                                        GLXFBConfig config,
slouken@5021
    81
                                                        GLXContext
slouken@5021
    82
                                                        share_context,
slouken@5021
    83
                                                        Bool direct,
slouken@5021
    84
                                                        const int
slouken@5021
    85
                                                        *attrib_list);
icculus@6360
    86
#endif
slouken@6296
    87
slouken@6296
    88
#ifndef GLX_ARB_create_context_profile
slouken@6296
    89
#define GLX_ARB_create_context_profile
slouken@6296
    90
#define GLX_CONTEXT_PROFILE_MASK_ARB       0x9126
slouken@6296
    91
#define GLX_CONTEXT_CORE_PROFILE_BIT_ARB   0x00000001
slouken@6296
    92
#define GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB 0x00000002
slouken@6296
    93
#endif
slouken@6296
    94
slouken@6296
    95
#ifndef GLX_ARB_create_context_robustness
slouken@6296
    96
#define GLX_ARB_create_context_robustness
slouken@6296
    97
#define GLX_CONTEXT_ROBUST_ACCESS_BIT_ARB  0x00000004
slouken@6296
    98
#define GLX_CONTEXT_RESET_NOTIFICATION_STRATEGY_ARB     0x8256
slouken@6296
    99
#define GLX_NO_RESET_NOTIFICATION_ARB                   0x8261
slouken@6296
   100
#define GLX_LOSE_CONTEXT_ON_RESET_ARB                   0x8252
slouken@6296
   101
#endif
slouken@6296
   102
slouken@6296
   103
#ifndef GLX_EXT_create_context_es2_profile
slouken@6296
   104
#define GLX_EXT_create_context_es2_profile
icculus@6375
   105
#ifndef GLX_CONTEXT_ES2_PROFILE_BIT_EXT
slouken@6296
   106
#define GLX_CONTEXT_ES2_PROFILE_BIT_EXT    0x00000002
slouken@6296
   107
#endif
icculus@6375
   108
#endif
slouken@6296
   109
icculus@7853
   110
#ifndef GLX_ARB_framebuffer_sRGB
icculus@7853
   111
#define GLX_ARB_framebuffer_sRGB
icculus@7853
   112
#ifndef GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB
icculus@7853
   113
#define GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB                0x20B2
icculus@7853
   114
#endif
icculus@7853
   115
#endif
icculus@7853
   116
icculus@11346
   117
#ifndef GLX_ARB_create_context_no_error
icculus@11346
   118
#define GLX_ARB_create_context_no_error
icculus@11346
   119
#ifndef GLX_CONTEXT_OPENGL_NO_ERROR_ARB
icculus@11346
   120
#define GLX_CONTEXT_OPENGL_NO_ERROR_ARB                 0x31B3
icculus@11346
   121
#endif
icculus@11346
   122
#endif
icculus@11346
   123
slouken@6296
   124
#ifndef GLX_EXT_swap_control
slouken@6296
   125
#define GLX_SWAP_INTERVAL_EXT              0x20F1
slouken@6296
   126
#define GLX_MAX_SWAP_INTERVAL_EXT          0x20F2
slouken@3100
   127
#endif
slouken@3100
   128
icculus@6382
   129
#ifndef GLX_EXT_swap_control_tear
icculus@6382
   130
#define GLX_LATE_SWAPS_TEAR_EXT 0x20F3
icculus@6382
   131
#endif
icculus@6382
   132
mdiluzio@9412
   133
#ifndef GLX_ARB_context_flush_control
mdiluzio@9412
   134
#define GLX_ARB_context_flush_control
mdiluzio@9412
   135
#define GLX_CONTEXT_RELEASE_BEHAVIOR_ARB   0x2097
mdiluzio@9412
   136
#define GLX_CONTEXT_RELEASE_BEHAVIOR_NONE_ARB           0x0000
mdiluzio@9412
   137
#define GLX_CONTEXT_RELEASE_BEHAVIOR_FLUSH_ARB          0x2098
mdiluzio@9412
   138
#endif
mdiluzio@9412
   139
slouken@6183
   140
#define OPENGL_REQUIRES_DLOPEN
slouken@6183
   141
#if defined(OPENGL_REQUIRES_DLOPEN) && defined(SDL_LOADSO_DLOPEN)
slouken@1952
   142
#include <dlfcn.h>
slouken@7191
   143
#define GL_LoadObject(X)    dlopen(X, (RTLD_NOW|RTLD_GLOBAL))
slouken@7191
   144
#define GL_LoadFunction     dlsym
slouken@7191
   145
#define GL_UnloadObject     dlclose
slouken@1952
   146
#else
slouken@7191
   147
#define GL_LoadObject   SDL_LoadObject
slouken@7191
   148
#define GL_LoadFunction SDL_LoadFunction
slouken@7191
   149
#define GL_UnloadObject SDL_UnloadObject
slouken@1952
   150
#endif
slouken@1952
   151
slouken@3057
   152
static void X11_GL_InitExtensions(_THIS);
bob@2322
   153
slouken@1952
   154
int
slouken@1952
   155
X11_GL_LoadLibrary(_THIS, const char *path)
slouken@1952
   156
{
slouken@7512
   157
    Display *display;
slouken@1952
   158
    void *handle;
slouken@1952
   159
slouken@6370
   160
    if (_this->gl_data) {
icculus@7037
   161
        return SDL_SetError("OpenGL context already created");
slouken@6370
   162
    }
slouken@6370
   163
slouken@3057
   164
    /* Load the OpenGL library */
slouken@3057
   165
    if (path == NULL) {
slouken@3057
   166
        path = SDL_getenv("SDL_OPENGL_LIBRARY");
slouken@1952
   167
    }
slouken@3057
   168
    if (path == NULL) {
slouken@3057
   169
        path = DEFAULT_OPENGL;
slouken@3057
   170
    }
slouken@6183
   171
    _this->gl_config.dll_handle = GL_LoadObject(path);
slouken@3057
   172
    if (!_this->gl_config.dll_handle) {
slouken@6183
   173
#if defined(OPENGL_REQUIRES_DLOPEN) && defined(SDL_LOADSO_DLOPEN)
slouken@6183
   174
        SDL_SetError("Failed loading %s: %s", path, dlerror());
slouken@6183
   175
#endif
slouken@3057
   176
        return -1;
slouken@3057
   177
    }
slouken@3057
   178
    SDL_strlcpy(_this->gl_config.driver_path, path,
slouken@3057
   179
                SDL_arraysize(_this->gl_config.driver_path));
slouken@2244
   180
slouken@3057
   181
    /* Allocate OpenGL memory */
slouken@3057
   182
    _this->gl_data =
slouken@3057
   183
        (struct SDL_GLDriverData *) SDL_calloc(1,
slouken@3057
   184
                                               sizeof(struct
slouken@3057
   185
                                                      SDL_GLDriverData));
slouken@3057
   186
    if (!_this->gl_data) {
icculus@7037
   187
        return SDL_OutOfMemory();
slouken@3057
   188
    }
slouken@3057
   189
slouken@3057
   190
    /* Load function pointers */
slouken@3057
   191
    handle = _this->gl_config.dll_handle;
slouken@7512
   192
    _this->gl_data->glXQueryExtension =
slouken@7512
   193
        (Bool (*)(Display *, int *, int *))
slouken@7512
   194
            GL_LoadFunction(handle, "glXQueryExtension");
slouken@1952
   195
    _this->gl_data->glXGetProcAddress =
slouken@4550
   196
        (void *(*)(const GLubyte *))
slouken@4550
   197
            GL_LoadFunction(handle, "glXGetProcAddressARB");
slouken@1952
   198
    _this->gl_data->glXChooseVisual =
slouken@4550
   199
        (XVisualInfo * (*)(Display *, int, int *))
slouken@4550
   200
            X11_GL_GetProcAddress(_this, "glXChooseVisual");
slouken@1952
   201
    _this->gl_data->glXCreateContext =
slouken@1952
   202
        (GLXContext(*)(Display *, XVisualInfo *, GLXContext, int))
slouken@4550
   203
            X11_GL_GetProcAddress(_this, "glXCreateContext");
slouken@1952
   204
    _this->gl_data->glXDestroyContext =
slouken@4550
   205
        (void (*)(Display *, GLXContext))
slouken@4550
   206
            X11_GL_GetProcAddress(_this, "glXDestroyContext");
slouken@1952
   207
    _this->gl_data->glXMakeCurrent =
slouken@4550
   208
        (int (*)(Display *, GLXDrawable, GLXContext))
slouken@4550
   209
            X11_GL_GetProcAddress(_this, "glXMakeCurrent");
slouken@1952
   210
    _this->gl_data->glXSwapBuffers =
slouken@4550
   211
        (void (*)(Display *, GLXDrawable))
slouken@4550
   212
            X11_GL_GetProcAddress(_this, "glXSwapBuffers");
icculus@5572
   213
    _this->gl_data->glXQueryDrawable =
icculus@5572
   214
        (void (*)(Display*,GLXDrawable,int,unsigned int*))
icculus@5572
   215
            X11_GL_GetProcAddress(_this, "glXQueryDrawable");
slouken@1952
   216
slouken@7512
   217
    if (!_this->gl_data->glXQueryExtension ||
slouken@7512
   218
        !_this->gl_data->glXChooseVisual ||
slouken@1952
   219
        !_this->gl_data->glXCreateContext ||
slouken@1952
   220
        !_this->gl_data->glXDestroyContext ||
slouken@4550
   221
        !_this->gl_data->glXMakeCurrent ||
slouken@4550
   222
        !_this->gl_data->glXSwapBuffers) {
icculus@7037
   223
        return SDL_SetError("Could not retrieve OpenGL functions");
slouken@1952
   224
    }
slouken@1952
   225
slouken@7512
   226
    display = ((SDL_VideoData *) _this->driverdata)->display;
slouken@7512
   227
    if (!_this->gl_data->glXQueryExtension(display, &_this->gl_data->errorBase, &_this->gl_data->eventBase)) {
slouken@7512
   228
        return SDL_SetError("GLX is not supported");
slouken@7512
   229
    }
slouken@7512
   230
slouken@3057
   231
    /* Initialize extensions */
slouken@10806
   232
    /* See lengthy comment about the inc/dec in 
slouken@10806
   233
       ../windows/SDL_windowsopengl.c. */
slouken@10806
   234
    ++_this->gl_config.driver_loaded;
slouken@3057
   235
    X11_GL_InitExtensions(_this);
slouken@10806
   236
    --_this->gl_config.driver_loaded;
gabomdq@7659
   237
    
gabomdq@7723
   238
    /* If we need a GL ES context and there's no  
gabomdq@7723
   239
     * GLX_EXT_create_context_es2_profile extension, switch over to X11_GLES functions  
gabomdq@7723
   240
     */
gabomdq@7723
   241
    if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES && 
slouken@10806
   242
        X11_GL_UseEGL(_this) ) {
gabomdq@7659
   243
#if SDL_VIDEO_OPENGL_EGL
gabomdq@7723
   244
        X11_GL_UnloadLibrary(_this);
gabomdq@7723
   245
        /* Better avoid conflicts! */
gabomdq@7723
   246
        if (_this->gl_config.dll_handle != NULL ) {
gabomdq@7723
   247
            GL_UnloadObject(_this->gl_config.dll_handle);
gabomdq@7723
   248
            _this->gl_config.dll_handle = NULL;
gabomdq@7723
   249
        }
gabomdq@7723
   250
        _this->GL_LoadLibrary = X11_GLES_LoadLibrary;
gabomdq@7723
   251
        _this->GL_GetProcAddress = X11_GLES_GetProcAddress;
gabomdq@7723
   252
        _this->GL_UnloadLibrary = X11_GLES_UnloadLibrary;
gabomdq@7723
   253
        _this->GL_CreateContext = X11_GLES_CreateContext;
gabomdq@7723
   254
        _this->GL_MakeCurrent = X11_GLES_MakeCurrent;
gabomdq@7723
   255
        _this->GL_SetSwapInterval = X11_GLES_SetSwapInterval;
gabomdq@7723
   256
        _this->GL_GetSwapInterval = X11_GLES_GetSwapInterval;
gabomdq@7723
   257
        _this->GL_SwapWindow = X11_GLES_SwapWindow;
gabomdq@7723
   258
        _this->GL_DeleteContext = X11_GLES_DeleteContext;
gabomdq@7723
   259
        return X11_GLES_LoadLibrary(_this, NULL);
gabomdq@7659
   260
#else
gabomdq@7723
   261
        return SDL_SetError("SDL not configured with EGL support");
gabomdq@7659
   262
#endif
gabomdq@7659
   263
    }
slouken@3057
   264
slouken@1952
   265
    return 0;
slouken@1952
   266
}
slouken@1952
   267
slouken@1952
   268
void *
slouken@1952
   269
X11_GL_GetProcAddress(_THIS, const char *proc)
slouken@1952
   270
{
slouken@1952
   271
    if (_this->gl_data->glXGetProcAddress) {
slouken@1952
   272
        return _this->gl_data->glXGetProcAddress((const GLubyte *) proc);
slouken@1952
   273
    }
slouken@4550
   274
    return GL_LoadFunction(_this->gl_config.dll_handle, proc);
slouken@1952
   275
}
slouken@1952
   276
slouken@3057
   277
void
slouken@1952
   278
X11_GL_UnloadLibrary(_THIS)
slouken@1952
   279
{
slouken@3057
   280
    /* Don't actually unload the library, since it may have registered
slouken@3057
   281
     * X11 shutdown hooks, per the notes at:
slouken@3057
   282
     * http://dri.sourceforge.net/doc/DRIuserguide.html
slouken@3057
   283
     */
slouken@3057
   284
#if 0
slouken@3057
   285
    GL_UnloadObject(_this->gl_config.dll_handle);
slouken@3057
   286
    _this->gl_config.dll_handle = NULL;
slouken@3057
   287
#endif
slouken@3057
   288
slouken@3057
   289
    /* Free OpenGL memory */
slouken@7719
   290
    SDL_free(_this->gl_data);
slouken@7719
   291
    _this->gl_data = NULL;
slouken@1952
   292
}
slouken@1952
   293
slouken@1952
   294
static SDL_bool
slouken@1952
   295
HasExtension(const char *extension, const char *extensions)
slouken@1952
   296
{
slouken@1952
   297
    const char *start;
slouken@1952
   298
    const char *where, *terminator;
slouken@1952
   299
icculus@9354
   300
    if (!extensions)
icculus@9354
   301
        return SDL_FALSE;
icculus@9354
   302
slouken@1952
   303
    /* Extension names should not have spaces. */
slouken@1952
   304
    where = SDL_strchr(extension, ' ');
slouken@1952
   305
    if (where || *extension == '\0')
slouken@1952
   306
        return SDL_FALSE;
slouken@1952
   307
slouken@1952
   308
    /* It takes a bit of care to be fool-proof about parsing the
slouken@1952
   309
     * OpenGL extensions string. Don't be fooled by sub-strings,
slouken@1952
   310
     * etc. */
slouken@1952
   311
slouken@1952
   312
    start = extensions;
slouken@1952
   313
slouken@1952
   314
    for (;;) {
slouken@1952
   315
        where = SDL_strstr(start, extension);
slouken@1952
   316
        if (!where)
slouken@1952
   317
            break;
slouken@1952
   318
slouken@1952
   319
        terminator = where + SDL_strlen(extension);
slouken@1952
   320
        if (where == start || *(where - 1) == ' ')
slouken@1952
   321
            if (*terminator == ' ' || *terminator == '\0')
slouken@1952
   322
                return SDL_TRUE;
slouken@1952
   323
slouken@1952
   324
        start = terminator;
slouken@1952
   325
    }
slouken@1952
   326
    return SDL_FALSE;
slouken@1952
   327
}
slouken@1952
   328
slouken@1952
   329
static void
slouken@1952
   330
X11_GL_InitExtensions(_THIS)
slouken@1952
   331
{
slouken@1952
   332
    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
icculus@9355
   333
    const int screen = DefaultScreen(display);
icculus@11439
   334
    XVisualInfo *vinfo = NULL;
icculus@11439
   335
    Window w = 0;
icculus@11441
   336
    GLXContext prev_ctx = 0;
icculus@11441
   337
    GLXDrawable prev_drawable = 0;
icculus@11439
   338
    GLXContext context = 0;
slouken@1952
   339
    const char *(*glXQueryExtensionsStringFunc) (Display *, int);
slouken@1952
   340
    const char *extensions;
slouken@1952
   341
icculus@11438
   342
    vinfo = X11_GL_GetVisual(_this, display, screen);
icculus@11439
   343
    if (vinfo) {
icculus@11439
   344
        GLXContext (*glXGetCurrentContextFunc) (void) =
icculus@11439
   345
            (GLXContext(*)(void))
icculus@11441
   346
                X11_GL_GetProcAddress(_this, "glXGetCurrentContext");
icculus@11439
   347
icculus@11441
   348
        GLXDrawable (*glXGetCurrentDrawableFunc) (void) =
icculus@11441
   349
            (GLXDrawable(*)(void))
icculus@11441
   350
                X11_GL_GetProcAddress(_this, "glXGetCurrentDrawable");
icculus@11441
   351
icculus@11441
   352
        if (glXGetCurrentContextFunc && glXGetCurrentDrawableFunc) {
icculus@11439
   353
            XSetWindowAttributes xattr;
icculus@11441
   354
            prev_ctx = glXGetCurrentContextFunc();
icculus@11441
   355
            prev_drawable = glXGetCurrentDrawableFunc();
icculus@11439
   356
icculus@11439
   357
            xattr.background_pixel = 0;
icculus@11439
   358
            xattr.border_pixel = 0;
icculus@11439
   359
            xattr.colormap =
icculus@11439
   360
                X11_XCreateColormap(display, RootWindow(display, screen),
icculus@11439
   361
                                    vinfo->visual, AllocNone);
icculus@11439
   362
            w = X11_XCreateWindow(display, RootWindow(display, screen), 0, 0,
icculus@11439
   363
                        32, 32, 0, vinfo->depth, InputOutput, vinfo->visual,
icculus@11439
   364
                        (CWBackPixel | CWBorderPixel | CWColormap), &xattr);
icculus@11439
   365
icculus@11439
   366
            context = _this->gl_data->glXCreateContext(display, vinfo,
icculus@11439
   367
                                                        NULL, True);
icculus@11439
   368
            if (context) {
icculus@11439
   369
                _this->gl_data->glXMakeCurrent(display, w, context);
icculus@11439
   370
            }
icculus@11439
   371
        }
icculus@11439
   372
icculus@11439
   373
        X11_XFree(vinfo);
icculus@11438
   374
    }
icculus@11438
   375
slouken@1952
   376
    glXQueryExtensionsStringFunc =
slouken@1952
   377
        (const char *(*)(Display *, int)) X11_GL_GetProcAddress(_this,
slouken@1952
   378
                                                                "glXQueryExtensionsString");
slouken@1952
   379
    if (glXQueryExtensionsStringFunc) {
slouken@1952
   380
        extensions = glXQueryExtensionsStringFunc(display, screen);
slouken@1952
   381
    } else {
slouken@1952
   382
        extensions = NULL;
slouken@1952
   383
    }
slouken@1952
   384
icculus@6382
   385
    /* Check for GLX_EXT_swap_control(_tear) */
icculus@6382
   386
    _this->gl_data->HAS_GLX_EXT_swap_control_tear = SDL_FALSE;
icculus@5572
   387
    if (HasExtension("GLX_EXT_swap_control", extensions)) {
icculus@5572
   388
        _this->gl_data->glXSwapIntervalEXT =
slouken@6846
   389
            (void (*)(Display*,GLXDrawable,int))
icculus@5572
   390
                X11_GL_GetProcAddress(_this, "glXSwapIntervalEXT");
icculus@6382
   391
        if (HasExtension("GLX_EXT_swap_control_tear", extensions)) {
icculus@6382
   392
            _this->gl_data->HAS_GLX_EXT_swap_control_tear = SDL_TRUE;
icculus@6382
   393
        }
slouken@1952
   394
    }
slouken@1952
   395
slouken@1952
   396
    /* Check for GLX_MESA_swap_control */
slouken@1952
   397
    if (HasExtension("GLX_MESA_swap_control", extensions)) {
slouken@1952
   398
        _this->gl_data->glXSwapIntervalMESA =
icculus@5572
   399
            (int(*)(int)) X11_GL_GetProcAddress(_this, "glXSwapIntervalMESA");
slouken@1952
   400
        _this->gl_data->glXGetSwapIntervalMESA =
icculus@5572
   401
            (int(*)(void)) X11_GL_GetProcAddress(_this,
slouken@1952
   402
                                                   "glXGetSwapIntervalMESA");
slouken@1952
   403
    }
slouken@1952
   404
icculus@5572
   405
    /* Check for GLX_SGI_swap_control */
icculus@5572
   406
    if (HasExtension("GLX_SGI_swap_control", extensions)) {
icculus@5572
   407
        _this->gl_data->glXSwapIntervalSGI =
icculus@5572
   408
            (int (*)(int)) X11_GL_GetProcAddress(_this, "glXSwapIntervalSGI");
icculus@5572
   409
    }
icculus@5572
   410
icculus@9330
   411
    /* Check for GLX_ARB_create_context */
icculus@9330
   412
    if (HasExtension("GLX_ARB_create_context", extensions)) {
icculus@9330
   413
        _this->gl_data->glXCreateContextAttribsARB =
icculus@9330
   414
            (GLXContext (*)(Display*,GLXFBConfig,GLXContext,Bool,const int *))
icculus@9330
   415
                X11_GL_GetProcAddress(_this, "glXCreateContextAttribsARB");
icculus@9330
   416
        _this->gl_data->glXChooseFBConfig =
icculus@9330
   417
            (GLXFBConfig *(*)(Display *, int, const int *, int *))
icculus@9330
   418
                X11_GL_GetProcAddress(_this, "glXChooseFBConfig");
icculus@9330
   419
    }
icculus@9330
   420
slouken@1952
   421
    /* Check for GLX_EXT_visual_rating */
slouken@1952
   422
    if (HasExtension("GLX_EXT_visual_rating", extensions)) {
slouken@1952
   423
        _this->gl_data->HAS_GLX_EXT_visual_rating = SDL_TRUE;
slouken@1952
   424
    }
slouken@1952
   425
mikesart@6570
   426
    /* Check for GLX_EXT_visual_info */
mikesart@6570
   427
    if (HasExtension("GLX_EXT_visual_info", extensions)) {
mikesart@6570
   428
        _this->gl_data->HAS_GLX_EXT_visual_info = SDL_TRUE;
mikesart@6570
   429
    }
gabomdq@7659
   430
    
gabomdq@7659
   431
    /* Check for GLX_EXT_create_context_es2_profile */
gabomdq@7659
   432
    if (HasExtension("GLX_EXT_create_context_es2_profile", extensions)) {
icculus@11438
   433
        /* this wants to call glGetString(), so it needs a context. */
icculus@11438
   434
        /* !!! FIXME: it would be nice not to make a context here though! */
icculus@11438
   435
        if (context) {
icculus@11438
   436
            SDL_GL_DeduceMaxSupportedESProfile(
icculus@11438
   437
                &_this->gl_data->es_profile_max_supported_version.major,
icculus@11438
   438
                &_this->gl_data->es_profile_max_supported_version.minor
icculus@11438
   439
            );
icculus@11438
   440
        }
gabomdq@7659
   441
    }
mdiluzio@9412
   442
mdiluzio@9412
   443
    /* Check for GLX_ARB_context_flush_control */
mdiluzio@9412
   444
    if (HasExtension("GLX_ARB_context_flush_control", extensions)) {
mdiluzio@9412
   445
        _this->gl_data->HAS_GLX_ARB_context_flush_control = SDL_TRUE;
mdiluzio@9412
   446
    }
icculus@11332
   447
icculus@11332
   448
    /* Check for GLX_ARB_create_context_robustness */
icculus@11332
   449
    if (HasExtension("GLX_ARB_create_context_robustness", extensions)) {
icculus@11332
   450
        _this->gl_data->HAS_GLX_ARB_create_context_robustness = SDL_TRUE;
icculus@11332
   451
    }
flibitijibibo@11347
   452
flibitijibibo@11347
   453
    /* Check for GLX_ARB_create_context_no_error */
flibitijibibo@11347
   454
    if (HasExtension("GLX_ARB_create_context_no_error", extensions)) {
flibitijibibo@11347
   455
        _this->gl_data->HAS_GLX_ARB_create_context_no_error = SDL_TRUE;
flibitijibibo@11347
   456
    }
icculus@11438
   457
icculus@11438
   458
    if (context) {
icculus@11438
   459
        _this->gl_data->glXMakeCurrent(display, None, NULL);
icculus@11438
   460
        _this->gl_data->glXDestroyContext(display, context);
icculus@11441
   461
        if (prev_ctx && prev_drawable) {
icculus@11441
   462
            _this->gl_data->glXMakeCurrent(display, prev_drawable, prev_ctx);
icculus@11440
   463
        }
icculus@11438
   464
    }
icculus@11438
   465
drmccoy@11645
   466
    if (w) {
drmccoy@11645
   467
        X11_XDestroyWindow(display, w);
drmccoy@11645
   468
    }
icculus@11438
   469
    X11_PumpEvents(_this);
slouken@1952
   470
}
slouken@1952
   471
slouken@6296
   472
/* glXChooseVisual and glXChooseFBConfig have some small differences in
slouken@7191
   473
 * the attribute encoding, it can be chosen with the for_FBConfig parameter.
icculus@10781
   474
 * Some targets fail if you use GLX_X_VISUAL_TYPE_EXT/GLX_DIRECT_COLOR_EXT,
icculus@10781
   475
 *  so it gets specified last if used and is pointed to by *_pvistypeattr.
icculus@10781
   476
 *  In case of failure, if that pointer is not NULL, set that pointer to None
icculus@10781
   477
 *  and try again.
slouken@6296
   478
 */
icculus@9458
   479
static int
icculus@10781
   480
X11_GL_GetAttributes(_THIS, Display * display, int screen, int * attribs, int size, Bool for_FBConfig, int **_pvistypeattr)
slouken@1952
   481
{
slouken@5395
   482
    int i = 0;
slouken@7191
   483
    const int MAX_ATTRIBUTES = 64;
icculus@10781
   484
    int *pvistypeattr = NULL;
slouken@1952
   485
slouken@7191
   486
    /* assert buffer is large enough to hold all SDL attributes. */
mikesart@6570
   487
    SDL_assert(size >= MAX_ATTRIBUTES);
slouken@3139
   488
slouken@1952
   489
    /* Setup our GLX attributes according to the gl_config. */
slouken@6296
   490
    if( for_FBConfig ) {
slouken@6296
   491
        attribs[i++] = GLX_RENDER_TYPE;
icculus@7462
   492
        attribs[i++] = GLX_RGBA_BIT;
slouken@6296
   493
    } else {
slouken@6296
   494
        attribs[i++] = GLX_RGBA;
slouken@6296
   495
    }
slouken@1952
   496
    attribs[i++] = GLX_RED_SIZE;
slouken@1952
   497
    attribs[i++] = _this->gl_config.red_size;
slouken@1952
   498
    attribs[i++] = GLX_GREEN_SIZE;
slouken@1952
   499
    attribs[i++] = _this->gl_config.green_size;
slouken@1952
   500
    attribs[i++] = GLX_BLUE_SIZE;
slouken@1952
   501
    attribs[i++] = _this->gl_config.blue_size;
slouken@1952
   502
slouken@1952
   503
    if (_this->gl_config.alpha_size) {
slouken@1952
   504
        attribs[i++] = GLX_ALPHA_SIZE;
slouken@1952
   505
        attribs[i++] = _this->gl_config.alpha_size;
slouken@1952
   506
    }
slouken@1952
   507
slouken@1952
   508
    if (_this->gl_config.double_buffer) {
slouken@1952
   509
        attribs[i++] = GLX_DOUBLEBUFFER;
icculus@7462
   510
        if( for_FBConfig ) {
icculus@7462
   511
            attribs[i++] = True;
icculus@7462
   512
        }
slouken@1952
   513
    }
slouken@1952
   514
slouken@1952
   515
    attribs[i++] = GLX_DEPTH_SIZE;
slouken@1952
   516
    attribs[i++] = _this->gl_config.depth_size;
slouken@1952
   517
slouken@1952
   518
    if (_this->gl_config.stencil_size) {
slouken@1952
   519
        attribs[i++] = GLX_STENCIL_SIZE;
slouken@1952
   520
        attribs[i++] = _this->gl_config.stencil_size;
slouken@1952
   521
    }
slouken@1952
   522
slouken@1952
   523
    if (_this->gl_config.accum_red_size) {
slouken@1952
   524
        attribs[i++] = GLX_ACCUM_RED_SIZE;
slouken@1952
   525
        attribs[i++] = _this->gl_config.accum_red_size;
slouken@1952
   526
    }
slouken@1952
   527
slouken@1952
   528
    if (_this->gl_config.accum_green_size) {
slouken@1952
   529
        attribs[i++] = GLX_ACCUM_GREEN_SIZE;
slouken@1952
   530
        attribs[i++] = _this->gl_config.accum_green_size;
slouken@1952
   531
    }
slouken@1952
   532
slouken@1952
   533
    if (_this->gl_config.accum_blue_size) {
slouken@1952
   534
        attribs[i++] = GLX_ACCUM_BLUE_SIZE;
slouken@1952
   535
        attribs[i++] = _this->gl_config.accum_blue_size;
slouken@1952
   536
    }
slouken@1952
   537
slouken@1952
   538
    if (_this->gl_config.accum_alpha_size) {
slouken@1952
   539
        attribs[i++] = GLX_ACCUM_ALPHA_SIZE;
slouken@1952
   540
        attribs[i++] = _this->gl_config.accum_alpha_size;
slouken@1952
   541
    }
slouken@1952
   542
slouken@1952
   543
    if (_this->gl_config.stereo) {
slouken@1952
   544
        attribs[i++] = GLX_STEREO;
icculus@7462
   545
        if( for_FBConfig ) {
icculus@7462
   546
            attribs[i++] = True;
icculus@7462
   547
        }
slouken@1952
   548
    }
slouken@1952
   549
slouken@1952
   550
    if (_this->gl_config.multisamplebuffers) {
slouken@1952
   551
        attribs[i++] = GLX_SAMPLE_BUFFERS_ARB;
slouken@1952
   552
        attribs[i++] = _this->gl_config.multisamplebuffers;
slouken@1952
   553
    }
slouken@1952
   554
slouken@1952
   555
    if (_this->gl_config.multisamplesamples) {
slouken@1952
   556
        attribs[i++] = GLX_SAMPLES_ARB;
slouken@1952
   557
        attribs[i++] = _this->gl_config.multisamplesamples;
slouken@1952
   558
    }
slouken@1952
   559
icculus@7853
   560
    if (_this->gl_config.framebuffer_srgb_capable) {
icculus@7853
   561
        attribs[i++] = GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB;
icculus@9459
   562
        attribs[i++] = True;  /* always needed, for_FBConfig or not! */
icculus@7853
   563
    }
icculus@7853
   564
slouken@3571
   565
    if (_this->gl_config.accelerated >= 0 &&
slouken@3571
   566
        _this->gl_data->HAS_GLX_EXT_visual_rating) {
slouken@1952
   567
        attribs[i++] = GLX_VISUAL_CAVEAT_EXT;
slouken@3570
   568
        attribs[i++] = _this->gl_config.accelerated ? GLX_NONE_EXT :
slouken@3570
   569
                                                      GLX_SLOW_VISUAL_EXT;
slouken@1952
   570
    }
slouken@3570
   571
icculus@6817
   572
    /* If we're supposed to use DirectColor visuals, and we've got the
icculus@6817
   573
       EXT_visual_info extension, then add GLX_X_VISUAL_TYPE_EXT. */
mikesart@6570
   574
    if (X11_UseDirectColorVisuals() &&
mikesart@6570
   575
        _this->gl_data->HAS_GLX_EXT_visual_info) {
icculus@10781
   576
        pvistypeattr = &attribs[i];
mikesart@6570
   577
        attribs[i++] = GLX_X_VISUAL_TYPE_EXT;
mikesart@6570
   578
        attribs[i++] = GLX_DIRECT_COLOR_EXT;
mikesart@6570
   579
    }
mikesart@6570
   580
slouken@1952
   581
    attribs[i++] = None;
mikesart@6570
   582
mikesart@6570
   583
    SDL_assert(i <= MAX_ATTRIBUTES);
slouken@7191
   584
icculus@10781
   585
    if (_pvistypeattr) {
icculus@10781
   586
        *_pvistypeattr = pvistypeattr;
icculus@10781
   587
    }
icculus@10781
   588
slouken@5395
   589
    return i;
slouken@5395
   590
}
slouken@5395
   591
slouken@5395
   592
XVisualInfo *
slouken@5395
   593
X11_GL_GetVisual(_THIS, Display * display, int screen)
slouken@5395
   594
{
slouken@5395
   595
    /* 64 seems nice. */
slouken@6296
   596
    int attribs[64];
slouken@8780
   597
    XVisualInfo *vinfo;
icculus@10781
   598
    int *pvistypeattr = NULL;
slouken@1952
   599
slouken@6181
   600
    if (!_this->gl_data) {
slouken@6181
   601
        /* The OpenGL library wasn't loaded, SDL_GetError() should have info */
slouken@6181
   602
        return NULL;
slouken@6181
   603
    }
slouken@6181
   604
icculus@10781
   605
    X11_GL_GetAttributes(_this, display, screen, attribs, 64, SDL_FALSE, &pvistypeattr);
slouken@1952
   606
    vinfo = _this->gl_data->glXChooseVisual(display, screen, attribs);
icculus@10781
   607
icculus@10781
   608
    if (!vinfo && (pvistypeattr != NULL)) {
icculus@10781
   609
        *pvistypeattr = None;
icculus@10781
   610
        vinfo = _this->gl_data->glXChooseVisual(display, screen, attribs);
icculus@10781
   611
    }
icculus@10781
   612
slouken@1952
   613
    if (!vinfo) {
slouken@1952
   614
        SDL_SetError("Couldn't find matching GLX visual");
slouken@1952
   615
    }
slouken@1952
   616
    return vinfo;
slouken@1952
   617
}
slouken@1952
   618
slouken@6988
   619
static int (*handler) (Display *, XErrorEvent *) = NULL;
icculus@9812
   620
static const char *errorHandlerOperation = NULL;
slouken@7512
   621
static int errorBase = 0;
jorgen@8804
   622
static int errorCode = 0;
slouken@6988
   623
static int
icculus@9812
   624
X11_GL_ErrorHandler(Display * d, XErrorEvent * e)
slouken@6988
   625
{
jorgen@8804
   626
    char *x11_error = NULL;
jorgen@8804
   627
    char x11_error_locale[256];
jorgen@8804
   628
jorgen@8804
   629
    errorCode = e->error_code;
jorgen@8804
   630
    if (X11_XGetErrorText(d, errorCode, x11_error_locale, sizeof(x11_error_locale)) == Success)
jorgen@8804
   631
    {
slouken@8967
   632
        x11_error = SDL_iconv_string("UTF-8", "", x11_error_locale, SDL_strlen(x11_error_locale)+1);
slouken@6988
   633
    }
jorgen@8804
   634
jorgen@8804
   635
    if (x11_error)
jorgen@8804
   636
    {
icculus@9812
   637
        SDL_SetError("Could not %s: %s", errorHandlerOperation, x11_error);
jorgen@8804
   638
        SDL_free(x11_error);
jorgen@8804
   639
    }
jorgen@8804
   640
    else
jorgen@8804
   641
    {
philipp@10945
   642
        SDL_SetError("Could not %s: %i (Base %i)", errorHandlerOperation, errorCode, errorBase);
jorgen@8804
   643
    }
jorgen@8804
   644
jorgen@8804
   645
    return (0);
slouken@6988
   646
}
slouken@6988
   647
slouken@10806
   648
SDL_bool
slouken@10806
   649
X11_GL_UseEGL(_THIS)
slouken@10806
   650
{
slouken@10806
   651
    SDL_assert(_this->gl_data != NULL);
slouken@10806
   652
    SDL_assert(_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES);
slouken@10806
   653
slouken@10806
   654
    return (SDL_GetHintBoolean(SDL_HINT_OPENGL_ES_DRIVER, SDL_FALSE)
slouken@10806
   655
            || _this->gl_config.major_version == 1 /* No GLX extension for OpenGL ES 1.x profiles. */
slouken@10806
   656
            || _this->gl_config.major_version > _this->gl_data->es_profile_max_supported_version.major
slouken@10806
   657
            || (_this->gl_config.major_version == _this->gl_data->es_profile_max_supported_version.major
slouken@10806
   658
                && _this->gl_config.minor_version > _this->gl_data->es_profile_max_supported_version.minor));
slouken@10806
   659
}
slouken@10806
   660
slouken@1952
   661
SDL_GLContext
slouken@1952
   662
X11_GL_CreateContext(_THIS, SDL_Window * window)
slouken@1952
   663
{
slouken@1952
   664
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1952
   665
    Display *display = data->videodata->display;
slouken@1952
   666
    int screen =
slouken@5246
   667
        ((SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata)->screen;
slouken@1952
   668
    XWindowAttributes xattr;
slouken@1952
   669
    XVisualInfo v, *vinfo;
slouken@1952
   670
    int n;
slouken@6393
   671
    GLXContext context = NULL, share_context;
slouken@6393
   672
slouken@6393
   673
    if (_this->gl_config.share_with_current_context) {
slouken@7413
   674
        share_context = (GLXContext)SDL_GL_GetCurrentContext();
slouken@6393
   675
    } else {
slouken@6393
   676
        share_context = NULL;
slouken@6393
   677
    }
slouken@1952
   678
slouken@1955
   679
    /* We do this to create a clean separation between X and GLX errors. */
icculus@7827
   680
    X11_XSync(display, False);
icculus@9812
   681
    errorHandlerOperation = "create GL context";
slouken@7512
   682
    errorBase = _this->gl_data->errorBase;
jorgen@8804
   683
    errorCode = Success;
icculus@9812
   684
    handler = X11_XSetErrorHandler(X11_GL_ErrorHandler);
icculus@7827
   685
    X11_XGetWindowAttributes(display, data->xwindow, &xattr);
slouken@1952
   686
    v.screen = screen;
icculus@7827
   687
    v.visualid = X11_XVisualIDFromVisual(xattr.visual);
icculus@7827
   688
    vinfo = X11_XGetVisualInfo(display, VisualScreenMask | VisualIDMask, &v, &n);
slouken@1952
   689
    if (vinfo) {
slouken@6393
   690
        if (_this->gl_config.major_version < 3 &&
slouken@6393
   691
            _this->gl_config.profile_mask == 0 &&
slouken@6393
   692
            _this->gl_config.flags == 0) {
slouken@6393
   693
            /* Create legacy context */
slouken@3100
   694
            context =
slouken@6393
   695
                _this->gl_data->glXCreateContext(display, vinfo, share_context, True);
slouken@3100
   696
        } else {
flibitijibibo@11347
   697
            /* max 14 attributes plus terminator */
flibitijibibo@11347
   698
            int attribs[15] = {
icculus@9330
   699
                GLX_CONTEXT_MAJOR_VERSION_ARB,
icculus@9330
   700
                _this->gl_config.major_version,
icculus@9330
   701
                GLX_CONTEXT_MINOR_VERSION_ARB,
icculus@9330
   702
                _this->gl_config.minor_version,
icculus@9330
   703
                0
icculus@9330
   704
            };
icculus@9330
   705
            int iattr = 4;
slouken@6296
   706
icculus@9330
   707
            /* SDL profile bits match GLX profile bits */
icculus@9330
   708
            if( _this->gl_config.profile_mask != 0 ) {
icculus@9330
   709
                attribs[iattr++] = GLX_CONTEXT_PROFILE_MASK_ARB;
icculus@9330
   710
                attribs[iattr++] = _this->gl_config.profile_mask;
icculus@9330
   711
            }
slouken@6296
   712
icculus@9330
   713
            /* SDL flags match GLX flags */
icculus@9330
   714
            if( _this->gl_config.flags != 0 ) {
icculus@9330
   715
                attribs[iattr++] = GLX_CONTEXT_FLAGS_ARB;
icculus@9330
   716
                attribs[iattr++] = _this->gl_config.flags;
icculus@9330
   717
            }
slouken@6296
   718
mdiluzio@9412
   719
            /* only set if glx extension is available */
mdiluzio@9412
   720
            if( _this->gl_data->HAS_GLX_ARB_context_flush_control ) {
mdiluzio@9412
   721
                attribs[iattr++] = GLX_CONTEXT_RELEASE_BEHAVIOR_ARB;
mdiluzio@9412
   722
                attribs[iattr++] = 
mdiluzio@9412
   723
                    _this->gl_config.release_behavior ? 
mdiluzio@9412
   724
                    GLX_CONTEXT_RELEASE_BEHAVIOR_FLUSH_ARB : 
mdiluzio@9412
   725
                    GLX_CONTEXT_RELEASE_BEHAVIOR_NONE_ARB;
mdiluzio@9412
   726
            }
mdiluzio@9412
   727
icculus@11332
   728
            /* only set if glx extension is available */
icculus@11332
   729
            if( _this->gl_data->HAS_GLX_ARB_create_context_robustness ) {
icculus@11332
   730
                attribs[iattr++] = GLX_CONTEXT_RESET_NOTIFICATION_STRATEGY_ARB;
icculus@11332
   731
                attribs[iattr++] =
icculus@11332
   732
                    _this->gl_config.reset_notification ?
icculus@11332
   733
                    GLX_LOSE_CONTEXT_ON_RESET_ARB :
icculus@11332
   734
                    GLX_NO_RESET_NOTIFICATION_ARB;
icculus@11332
   735
            }
icculus@11332
   736
flibitijibibo@11347
   737
            /* only set if glx extension is available */
flibitijibibo@11347
   738
            if( _this->gl_data->HAS_GLX_ARB_create_context_no_error ) {
flibitijibibo@11347
   739
                attribs[iattr++] = GLX_CONTEXT_OPENGL_NO_ERROR_ARB;
flibitijibibo@11347
   740
                attribs[iattr++] = _this->gl_config.no_error;
flibitijibibo@11347
   741
            }
flibitijibibo@11347
   742
icculus@9330
   743
            attribs[iattr++] = 0;
slouken@3139
   744
icculus@9330
   745
            /* Get a pointer to the context creation function for GL 3.0 */
icculus@9330
   746
            if (!_this->gl_data->glXCreateContextAttribsARB) {
icculus@9330
   747
                SDL_SetError("OpenGL 3.0 and later are not supported by this system");
icculus@9330
   748
            } else {
icculus@9330
   749
                int glxAttribs[64];
icculus@9330
   750
icculus@9330
   751
                /* Create a GL 3.x context */
icculus@9330
   752
                GLXFBConfig *framebuffer_config = NULL;
icculus@9330
   753
                int fbcount = 0;
icculus@10781
   754
                int *pvistypeattr = NULL;
icculus@9330
   755
icculus@10781
   756
                X11_GL_GetAttributes(_this,display,screen,glxAttribs,64,SDL_TRUE,&pvistypeattr);
icculus@9330
   757
icculus@10781
   758
                if (_this->gl_data->glXChooseFBConfig) {
icculus@10781
   759
                    framebuffer_config = _this->gl_data->glXChooseFBConfig(display,
icculus@9330
   760
                                          DefaultScreen(display), glxAttribs,
icculus@10781
   761
                                          &fbcount);
icculus@10781
   762
icculus@10781
   763
                    if (!framebuffer_config && (pvistypeattr != NULL)) {
icculus@10781
   764
                        *pvistypeattr = None;
icculus@10781
   765
                        framebuffer_config = _this->gl_data->glXChooseFBConfig(display,
icculus@10781
   766
                                          DefaultScreen(display), glxAttribs,
icculus@10781
   767
                                          &fbcount);
icculus@10781
   768
                    }
icculus@10781
   769
            
icculus@10781
   770
                    if (framebuffer_config) {
icculus@10781
   771
                        context = _this->gl_data->glXCreateContextAttribsARB(display,
icculus@10781
   772
                                                        framebuffer_config[0],
icculus@10781
   773
                                                        share_context, True, attribs);
icculus@10781
   774
                        X11_XFree(framebuffer_config);
icculus@10781
   775
                    }
slouken@3100
   776
                }
slouken@3100
   777
            }
slouken@3100
   778
        }
icculus@7827
   779
        X11_XFree(vinfo);
slouken@1952
   780
    }
icculus@7827
   781
    X11_XSync(display, False);
icculus@7827
   782
    X11_XSetErrorHandler(handler);
slouken@7191
   783
slouken@1952
   784
    if (!context) {
jorgen@8804
   785
        if (errorCode == Success) {
jorgen@8804
   786
            SDL_SetError("Could not create GL context");
jorgen@8804
   787
        }
slouken@2178
   788
        return NULL;
slouken@1952
   789
    }
slouken@2178
   790
slouken@2178
   791
    if (X11_GL_MakeCurrent(_this, window, context) < 0) {
slouken@2178
   792
        X11_GL_DeleteContext(_this, context);
slouken@2178
   793
        return NULL;
slouken@2178
   794
    }
slouken@2178
   795
slouken@2178
   796
    return context;
slouken@1952
   797
}
slouken@1952
   798
slouken@1952
   799
int
slouken@1952
   800
X11_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
slouken@1952
   801
{
slouken@1952
   802
    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
slouken@1952
   803
    Window drawable =
urkle@6794
   804
        (context ? ((SDL_WindowData *) window->driverdata)->xwindow : None);
slouken@1952
   805
    GLXContext glx_context = (GLXContext) context;
icculus@9812
   806
    int rc;
slouken@1952
   807
slouken@6522
   808
    if (!_this->gl_data) {
icculus@7037
   809
        return SDL_SetError("OpenGL not initialized");
slouken@6522
   810
    }
slouken@6522
   811
icculus@9812
   812
    /* We do this to create a clean separation between X and GLX errors. */
icculus@9812
   813
    X11_XSync(display, False);
icculus@9812
   814
    errorHandlerOperation = "make GL context current";
icculus@9812
   815
    errorBase = _this->gl_data->errorBase;
icculus@9812
   816
    errorCode = Success;
icculus@9812
   817
    handler = X11_XSetErrorHandler(X11_GL_ErrorHandler);
icculus@9812
   818
    rc = _this->gl_data->glXMakeCurrent(display, drawable, glx_context);
icculus@9812
   819
    X11_XSetErrorHandler(handler);
icculus@9812
   820
icculus@9812
   821
    if (errorCode != Success) {   /* uhoh, an X error was thrown! */
icculus@9812
   822
        return -1;  /* the error handler called SDL_SetError() already. */
philipp@10173
   823
    } else if (!rc) {  /* glXMakeCurrent() failed without throwing an X error */
icculus@7037
   824
        return SDL_SetError("Unable to make GL context current");
slouken@1952
   825
    }
slouken@1952
   826
icculus@7037
   827
    return 0;
slouken@1952
   828
}
slouken@1952
   829
slouken@7191
   830
/*
philipp@10173
   831
   0 is a valid argument to glXSwapInterval(MESA|EXT) and setting it to 0
icculus@5572
   832
   will undo the effect of a previous call with a value that is greater
icculus@5572
   833
   than zero (or at least that is what the docs say). OTOH, 0 is an invalid
philipp@10173
   834
   argument to glXSwapIntervalSGI and it returns an error if you call it
icculus@5572
   835
   with 0 as an argument.
bob@2327
   836
*/
bob@2327
   837
philipp@10170
   838
static int swapinterval = 0;
slouken@1952
   839
int
slouken@1952
   840
X11_GL_SetSwapInterval(_THIS, int interval)
slouken@1952
   841
{
icculus@6382
   842
    int status = -1;
slouken@1952
   843
icculus@6382
   844
    if ((interval < 0) && (!_this->gl_data->HAS_GLX_EXT_swap_control_tear)) {
icculus@6382
   845
        SDL_SetError("Negative swap interval unsupported in this GL");
icculus@6382
   846
    } else if (_this->gl_data->glXSwapIntervalEXT) {
icculus@5572
   847
        Display *display = ((SDL_VideoData *) _this->driverdata)->display;
icculus@5572
   848
        const SDL_WindowData *windowdata = (SDL_WindowData *)
slouken@7412
   849
            SDL_GL_GetCurrentWindow()->driverdata;
slouken@6846
   850
icculus@5572
   851
        Window drawable = windowdata->xwindow;
slouken@6846
   852
slouken@6846
   853
        /*
slouken@6846
   854
         * This is a workaround for a bug in NVIDIA drivers. Bug has been reported
slouken@6846
   855
         * and will be fixed in a future release (probably 319.xx).
slouken@6846
   856
         *
slouken@6846
   857
         * There's a bug where glXSetSwapIntervalEXT ignores updates because
slouken@6846
   858
         * it has the wrong value cached. To work around it, we just run a no-op
slouken@6846
   859
         * update to the current value.
slouken@6846
   860
         */
slouken@6851
   861
        int currentInterval = X11_GL_GetSwapInterval(_this);
slouken@6846
   862
        _this->gl_data->glXSwapIntervalEXT(display, drawable, currentInterval);
slouken@6846
   863
        _this->gl_data->glXSwapIntervalEXT(display, drawable, interval);
slouken@6846
   864
slouken@6846
   865
        status = 0;
slouken@6846
   866
        swapinterval = interval;
icculus@5572
   867
    } else if (_this->gl_data->glXSwapIntervalMESA) {
slouken@1952
   868
        status = _this->gl_data->glXSwapIntervalMESA(interval);
slouken@1952
   869
        if (status != 0) {
philipp@10173
   870
            SDL_SetError("glXSwapIntervalMESA failed");
bob@2327
   871
        } else {
bob@2327
   872
            swapinterval = interval;
slouken@1952
   873
        }
slouken@1952
   874
    } else if (_this->gl_data->glXSwapIntervalSGI) {
slouken@1952
   875
        status = _this->gl_data->glXSwapIntervalSGI(interval);
slouken@1952
   876
        if (status != 0) {
philipp@10173
   877
            SDL_SetError("glXSwapIntervalSGI failed");
bob@2327
   878
        } else {
bob@2327
   879
            swapinterval = interval;
slouken@1952
   880
        }
slouken@1952
   881
    } else {
slouken@1952
   882
        SDL_Unsupported();
slouken@1952
   883
    }
slouken@1952
   884
    return status;
slouken@1952
   885
}
slouken@1952
   886
slouken@1952
   887
int
slouken@1952
   888
X11_GL_GetSwapInterval(_THIS)
slouken@1952
   889
{
icculus@5572
   890
    if (_this->gl_data->glXSwapIntervalEXT) {
icculus@5572
   891
        Display *display = ((SDL_VideoData *) _this->driverdata)->display;
icculus@5572
   892
        const SDL_WindowData *windowdata = (SDL_WindowData *)
slouken@7412
   893
            SDL_GL_GetCurrentWindow()->driverdata;
icculus@5572
   894
        Window drawable = windowdata->xwindow;
icculus@6382
   895
        unsigned int allow_late_swap_tearing = 0;
icculus@6382
   896
        unsigned int interval = 0;
icculus@6382
   897
icculus@6382
   898
        if (_this->gl_data->HAS_GLX_EXT_swap_control_tear) {
icculus@6382
   899
            _this->gl_data->glXQueryDrawable(display, drawable,
icculus@6382
   900
                                            GLX_LATE_SWAPS_TEAR_EXT,
icculus@6382
   901
                                            &allow_late_swap_tearing);
icculus@6382
   902
        }
icculus@6382
   903
icculus@5572
   904
        _this->gl_data->glXQueryDrawable(display, drawable,
icculus@6382
   905
                                         GLX_SWAP_INTERVAL_EXT, &interval);
icculus@6382
   906
icculus@6382
   907
        if ((allow_late_swap_tearing) && (interval > 0)) {
icculus@6382
   908
            return -((int) interval);
icculus@6382
   909
        }
icculus@6382
   910
icculus@6382
   911
        return (int) interval;
icculus@5572
   912
    } else if (_this->gl_data->glXGetSwapIntervalMESA) {
slouken@1952
   913
        return _this->gl_data->glXGetSwapIntervalMESA();
slouken@1952
   914
    } else {
bob@2327
   915
        return swapinterval;
slouken@1952
   916
    }
slouken@1952
   917
}
slouken@1952
   918
slouken@10690
   919
int
slouken@1952
   920
X11_GL_SwapWindow(_THIS, SDL_Window * window)
slouken@1952
   921
{
slouken@1952
   922
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1952
   923
    Display *display = data->videodata->display;
slouken@1952
   924
slouken@3685
   925
    _this->gl_data->glXSwapBuffers(display, data->xwindow);
slouken@10690
   926
    return 0;
slouken@1952
   927
}
slouken@1952
   928
slouken@1952
   929
void
slouken@1952
   930
X11_GL_DeleteContext(_THIS, SDL_GLContext context)
slouken@1952
   931
{
slouken@1952
   932
    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
slouken@1952
   933
    GLXContext glx_context = (GLXContext) context;
slouken@1952
   934
slouken@6522
   935
    if (!_this->gl_data) {
slouken@6522
   936
        return;
slouken@6522
   937
    }
slouken@1952
   938
    _this->gl_data->glXDestroyContext(display, glx_context);
icculus@7827
   939
    X11_XSync(display, False);
slouken@1952
   940
}
slouken@1952
   941
slouken@1952
   942
#endif /* SDL_VIDEO_OPENGL_GLX */
slouken@1952
   943
slouken@6044
   944
#endif /* SDL_VIDEO_DRIVER_X11 */
slouken@6044
   945
slouken@1952
   946
/* vi: set ts=4 sw=4 expandtab: */