context.c 25.6 KB
Newer Older
Camilla Berglund's avatar
Camilla Berglund committed
1
//========================================================================
Camilla Berglund's avatar
Camilla Berglund committed
2
// GLFW 3.3 - www.glfw.org
Camilla Berglund's avatar
Camilla Berglund committed
3
4
//------------------------------------------------------------------------
// Copyright (c) 2002-2006 Marcus Geelnard
Camilla Löwy's avatar
Camilla Löwy committed
5
// Copyright (c) 2006-2016 Camilla Löwy <elmindreda@glfw.org>
Camilla Berglund's avatar
Camilla Berglund committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
//    claim that you wrote the original software. If you use this software
//    in a product, an acknowledgment in the product documentation would
//    be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
//    be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
//    distribution.
//
//========================================================================
27
28
// Please use C89 style variable declarations in this file because VS 2010
//========================================================================
Camilla Berglund's avatar
Camilla Berglund committed
29
30
31

#include "internal.h"

32
#include <assert.h>
33
#include <stdio.h>
34
#include <string.h>
35
#include <limits.h>
36
#include <stdio.h>
37

Camilla Berglund's avatar
Camilla Berglund committed
38

39
40
41
42
//////////////////////////////////////////////////////////////////////////
//////                       GLFW internal API                      //////
//////////////////////////////////////////////////////////////////////////

43
44
45
46
47
48
// Checks whether the desired context attributes are valid
//
// This function checks things like whether the specified client API version
// exists and whether all relevant options have supported and non-conflicting
// values
//
49
GLFWbool _glfwIsValidContextConfig(const _GLFWctxconfig* ctxconfig)
Camilla Berglund's avatar
Camilla Berglund committed
50
{
51
52
53
54
55
56
57
58
59
60
    if (ctxconfig->share)
    {
        if (ctxconfig->client == GLFW_NO_API ||
            ctxconfig->share->context.client == GLFW_NO_API)
        {
            _glfwInputError(GLFW_NO_WINDOW_CONTEXT, NULL);
            return GLFW_FALSE;
        }
    }

61
    if (ctxconfig->source != GLFW_NATIVE_CONTEXT_API &&
Camilla Löwy's avatar
Camilla Löwy committed
62
63
        ctxconfig->source != GLFW_EGL_CONTEXT_API &&
        ctxconfig->source != GLFW_OSMESA_CONTEXT_API)
64
65
    {
        _glfwInputError(GLFW_INVALID_ENUM,
66
                        "Invalid context creation API 0x%08X",
67
68
69
70
71
72
73
                        ctxconfig->source);
        return GLFW_FALSE;
    }

    if (ctxconfig->client != GLFW_NO_API &&
        ctxconfig->client != GLFW_OPENGL_API &&
        ctxconfig->client != GLFW_OPENGL_ES_API)
74
    {
Camilla Berglund's avatar
Camilla Berglund committed
75
        _glfwInputError(GLFW_INVALID_ENUM,
76
                        "Invalid client API 0x%08X",
77
                        ctxconfig->client);
78
        return GLFW_FALSE;
79
    }
Camilla Berglund's avatar
Camilla Berglund committed
80

81
    if (ctxconfig->client == GLFW_OPENGL_API)
82
    {
83
84
85
86
        if ((ctxconfig->major < 1 || ctxconfig->minor < 0) ||
            (ctxconfig->major == 1 && ctxconfig->minor > 5) ||
            (ctxconfig->major == 2 && ctxconfig->minor > 1) ||
            (ctxconfig->major == 3 && ctxconfig->minor > 3))
87
88
89
90
91
        {
            // OpenGL 1.0 is the smallest valid version
            // OpenGL 1.x series ended with version 1.5
            // OpenGL 2.x series ended with version 2.1
            // OpenGL 3.x series ended with version 3.3
Camilla Berglund's avatar
Camilla Berglund committed
92
            // For now, let everything else through
93

94
            _glfwInputError(GLFW_INVALID_VALUE,
Camilla Berglund's avatar
Camilla Berglund committed
95
                            "Invalid OpenGL version %i.%i",
96
                            ctxconfig->major, ctxconfig->minor);
97
            return GLFW_FALSE;
98
99
        }

100
        if (ctxconfig->profile)
101
        {
102
103
            if (ctxconfig->profile != GLFW_OPENGL_CORE_PROFILE &&
                ctxconfig->profile != GLFW_OPENGL_COMPAT_PROFILE)
104
            {
105
                _glfwInputError(GLFW_INVALID_ENUM,
106
                                "Invalid OpenGL profile 0x%08X",
Camilla Berglund's avatar
Camilla Berglund committed
107
                                ctxconfig->profile);
108
                return GLFW_FALSE;
109
            }
Camilla Berglund's avatar
Camilla Berglund committed
110

111
            if (ctxconfig->major <= 2 ||
112
                (ctxconfig->major == 3 && ctxconfig->minor < 2))
113
114
115
116
            {
                // Desktop OpenGL context profiles are only defined for version 3.2
                // and above

117
                _glfwInputError(GLFW_INVALID_VALUE,
Camilla Berglund's avatar
Camilla Berglund committed
118
                                "Context profiles are only defined for OpenGL version 3.2 and above");
119
                return GLFW_FALSE;
120
121
122
            }
        }

123
        if (ctxconfig->forward && ctxconfig->major <= 2)
124
125
        {
            // Forward-compatible contexts are only defined for OpenGL version 3.0 and above
126
            _glfwInputError(GLFW_INVALID_VALUE,
Camilla Berglund's avatar
Camilla Berglund committed
127
                            "Forward-compatibility is only defined for OpenGL version 3.0 and above");
128
            return GLFW_FALSE;
129
130
        }
    }
131
    else if (ctxconfig->client == GLFW_OPENGL_ES_API)
132
    {
133
134
135
        if (ctxconfig->major < 1 || ctxconfig->minor < 0 ||
            (ctxconfig->major == 1 && ctxconfig->minor > 1) ||
            (ctxconfig->major == 2 && ctxconfig->minor > 0))
136
        {
137
138
            // OpenGL ES 1.0 is the smallest valid version
            // OpenGL ES 1.x series ended with version 1.1
139
            // OpenGL ES 2.x series ended with version 2.0
Camilla Berglund's avatar
Camilla Berglund committed
140
            // For now, let everything else through
141

142
            _glfwInputError(GLFW_INVALID_VALUE,
Camilla Berglund's avatar
Camilla Berglund committed
143
                            "Invalid OpenGL ES version %i.%i",
144
                            ctxconfig->major, ctxconfig->minor);
145
            return GLFW_FALSE;
146
        }
147
    }
Camilla Berglund's avatar
Camilla Berglund committed
148

149
    if (ctxconfig->robustness)
150
    {
151
152
        if (ctxconfig->robustness != GLFW_NO_RESET_NOTIFICATION &&
            ctxconfig->robustness != GLFW_LOSE_CONTEXT_ON_RESET)
153
        {
Camilla Berglund's avatar
Camilla Berglund committed
154
            _glfwInputError(GLFW_INVALID_ENUM,
155
                            "Invalid context robustness mode 0x%08X",
Camilla Berglund's avatar
Camilla Berglund committed
156
                            ctxconfig->robustness);
157
            return GLFW_FALSE;
158
159
160
        }
    }

161
162
163
164
165
    if (ctxconfig->release)
    {
        if (ctxconfig->release != GLFW_RELEASE_BEHAVIOR_NONE &&
            ctxconfig->release != GLFW_RELEASE_BEHAVIOR_FLUSH)
        {
Camilla Berglund's avatar
Camilla Berglund committed
166
            _glfwInputError(GLFW_INVALID_ENUM,
167
                            "Invalid context release behavior 0x%08X",
Camilla Berglund's avatar
Camilla Berglund committed
168
                            ctxconfig->release);
169
            return GLFW_FALSE;
170
171
172
        }
    }

173
    return GLFW_TRUE;
174
}
175

176
177
// Chooses the framebuffer config that best matches the desired one
//
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
const _GLFWfbconfig* _glfwChooseFBConfig(const _GLFWfbconfig* desired,
                                         const _GLFWfbconfig* alternatives,
                                         unsigned int count)
{
    unsigned int i;
    unsigned int missing, leastMissing = UINT_MAX;
    unsigned int colorDiff, leastColorDiff = UINT_MAX;
    unsigned int extraDiff, leastExtraDiff = UINT_MAX;
    const _GLFWfbconfig* current;
    const _GLFWfbconfig* closest = NULL;

    for (i = 0;  i < count;  i++)
    {
        current = alternatives + i;

        if (desired->stereo > 0 && current->stereo == 0)
        {
            // Stereo is a hard constraint
            continue;
        }

Camilla Berglund's avatar
Camilla Berglund committed
199
200
201
202
203
204
        if (desired->doublebuffer != current->doublebuffer)
        {
            // Double buffering is a hard constraint
            continue;
        }

205
206
207
208
209
210
211
212
213
214
215
216
217
        // Count number of missing buffers
        {
            missing = 0;

            if (desired->alphaBits > 0 && current->alphaBits == 0)
                missing++;

            if (desired->depthBits > 0 && current->depthBits == 0)
                missing++;

            if (desired->stencilBits > 0 && current->stencilBits == 0)
                missing++;

Camilla Berglund's avatar
Camilla Berglund committed
218
219
220
            if (desired->auxBuffers > 0 &&
                current->auxBuffers < desired->auxBuffers)
            {
221
                missing += desired->auxBuffers - current->auxBuffers;
Camilla Berglund's avatar
Camilla Berglund committed
222
            }
223
224
225
226
227
228
229
230

            if (desired->samples > 0 && current->samples == 0)
            {
                // Technically, several multisampling buffers could be
                // involved, but that's a lower level implementation detail and
                // not important to us here, so we count them as one
                missing++;
            }
231
232
233

            if (desired->transparent != current->transparent)
                missing++;
234
235
236
237
238
239
240
241
242
        }

        // These polynomials make many small channel size differences matter
        // less than one large channel size difference

        // Calculate color channel size difference value
        {
            colorDiff = 0;

Camilla Berglund's avatar
Camilla Berglund committed
243
            if (desired->redBits != GLFW_DONT_CARE)
244
245
246
247
248
            {
                colorDiff += (desired->redBits - current->redBits) *
                             (desired->redBits - current->redBits);
            }

Camilla Berglund's avatar
Camilla Berglund committed
249
            if (desired->greenBits != GLFW_DONT_CARE)
250
251
252
253
254
            {
                colorDiff += (desired->greenBits - current->greenBits) *
                             (desired->greenBits - current->greenBits);
            }

Camilla Berglund's avatar
Camilla Berglund committed
255
            if (desired->blueBits != GLFW_DONT_CARE)
256
257
258
259
260
261
262
263
264
265
            {
                colorDiff += (desired->blueBits - current->blueBits) *
                             (desired->blueBits - current->blueBits);
            }
        }

        // Calculate non-color channel size difference value
        {
            extraDiff = 0;

Camilla Berglund's avatar
Camilla Berglund committed
266
            if (desired->alphaBits != GLFW_DONT_CARE)
267
268
269
270
271
            {
                extraDiff += (desired->alphaBits - current->alphaBits) *
                             (desired->alphaBits - current->alphaBits);
            }

Camilla Berglund's avatar
Camilla Berglund committed
272
            if (desired->depthBits != GLFW_DONT_CARE)
273
274
275
276
277
            {
                extraDiff += (desired->depthBits - current->depthBits) *
                             (desired->depthBits - current->depthBits);
            }

Camilla Berglund's avatar
Camilla Berglund committed
278
            if (desired->stencilBits != GLFW_DONT_CARE)
279
280
281
282
283
            {
                extraDiff += (desired->stencilBits - current->stencilBits) *
                             (desired->stencilBits - current->stencilBits);
            }

Camilla Berglund's avatar
Camilla Berglund committed
284
            if (desired->accumRedBits != GLFW_DONT_CARE)
285
286
287
288
289
            {
                extraDiff += (desired->accumRedBits - current->accumRedBits) *
                             (desired->accumRedBits - current->accumRedBits);
            }

Camilla Berglund's avatar
Camilla Berglund committed
290
            if (desired->accumGreenBits != GLFW_DONT_CARE)
291
292
293
294
295
            {
                extraDiff += (desired->accumGreenBits - current->accumGreenBits) *
                             (desired->accumGreenBits - current->accumGreenBits);
            }

Camilla Berglund's avatar
Camilla Berglund committed
296
            if (desired->accumBlueBits != GLFW_DONT_CARE)
297
298
299
300
301
            {
                extraDiff += (desired->accumBlueBits - current->accumBlueBits) *
                             (desired->accumBlueBits - current->accumBlueBits);
            }

Camilla Berglund's avatar
Camilla Berglund committed
302
            if (desired->accumAlphaBits != GLFW_DONT_CARE)
303
304
305
306
307
            {
                extraDiff += (desired->accumAlphaBits - current->accumAlphaBits) *
                             (desired->accumAlphaBits - current->accumAlphaBits);
            }

Camilla Berglund's avatar
Camilla Berglund committed
308
            if (desired->samples != GLFW_DONT_CARE)
309
310
311
312
313
            {
                extraDiff += (desired->samples - current->samples) *
                             (desired->samples - current->samples);
            }

Camilla Berglund's avatar
Camilla Berglund committed
314
315
            if (desired->sRGB && !current->sRGB)
                extraDiff++;
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
        }

        // Figure out if the current one is better than the best one found so far
        // Least number of missing buffers is the most important heuristic,
        // then color buffer size match and lastly size match for other buffers

        if (missing < leastMissing)
            closest = current;
        else if (missing == leastMissing)
        {
            if ((colorDiff < leastColorDiff) ||
                (colorDiff == leastColorDiff && extraDiff < leastExtraDiff))
            {
                closest = current;
            }
        }

        if (current == closest)
        {
            leastMissing = missing;
            leastColorDiff = colorDiff;
            leastExtraDiff = extraDiff;
        }
    }

    return closest;
}

344
345
// Retrieves the attributes of the current context
//
346
347
GLFWbool _glfwRefreshContextAttribs(_GLFWwindow* window,
                                    const _GLFWctxconfig* ctxconfig)
348
{
Camilla Berglund's avatar
Cleanup    
Camilla Berglund committed
349
    int i;
350
    _GLFWwindow* previous;
Camilla Berglund's avatar
Cleanup    
Camilla Berglund committed
351
352
353
354
355
356
357
358
359
360
361
    const char* version;
    const char* prefixes[] =
    {
        "OpenGL ES-CM ",
        "OpenGL ES-CL ",
        "OpenGL ES ",
        NULL
    };

    window->context.source = ctxconfig->source;
    window->context.client = GLFW_OPENGL_API;
362

Camilla Löwy's avatar
Cleanup    
Camilla Löwy committed
363
    previous = _glfwPlatformGetTls(&_glfw.contextSlot);
364
365
    glfwMakeContextCurrent((GLFWwindow*) window);

366
    window->context.GetIntegerv = (PFNGLGETINTEGERVPROC)
367
        window->context.getProcAddress("glGetIntegerv");
368
    window->context.GetString = (PFNGLGETSTRINGPROC)
369
        window->context.getProcAddress("glGetString");
370
371
372
    if (!window->context.GetIntegerv || !window->context.GetString)
    {
        _glfwInputError(GLFW_PLATFORM_ERROR, "Entry point retrieval is broken");
373
        glfwMakeContextCurrent((GLFWwindow*) previous);
374
375
        return GLFW_FALSE;
    }
376

Camilla Berglund's avatar
Cleanup    
Camilla Berglund committed
377
378
    version = (const char*) window->context.GetString(GL_VERSION);
    if (!version)
379
    {
380
381
382
383
384
385
386
387
388
389
390
        if (ctxconfig->client == GLFW_OPENGL_API)
        {
            _glfwInputError(GLFW_PLATFORM_ERROR,
                            "OpenGL version string retrieval is broken");
        }
        else
        {
            _glfwInputError(GLFW_PLATFORM_ERROR,
                            "OpenGL ES version string retrieval is broken");
        }

391
        glfwMakeContextCurrent((GLFWwindow*) previous);
392
        return GLFW_FALSE;
Camilla Berglund's avatar
Camilla Berglund committed
393
394
    }

Camilla Berglund's avatar
Cleanup    
Camilla Berglund committed
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
    for (i = 0;  prefixes[i];  i++)
    {
        const size_t length = strlen(prefixes[i]);

        if (strncmp(version, prefixes[i], length) == 0)
        {
            version += length;
            window->context.client = GLFW_OPENGL_ES_API;
            break;
        }
    }

    if (!sscanf(version, "%d.%d.%d",
                &window->context.major,
                &window->context.minor,
                &window->context.revision))
    {
412
413
414
415
416
417
418
419
420
421
422
        if (window->context.client == GLFW_OPENGL_API)
        {
            _glfwInputError(GLFW_PLATFORM_ERROR,
                            "No version found in OpenGL version string");
        }
        else
        {
            _glfwInputError(GLFW_PLATFORM_ERROR,
                            "No version found in OpenGL ES version string");
        }

423
        glfwMakeContextCurrent((GLFWwindow*) previous);
Camilla Berglund's avatar
Cleanup    
Camilla Berglund committed
424
425
        return GLFW_FALSE;
    }
426

Camilla Berglund's avatar
Cleanup    
Camilla Berglund committed
427
428
429
430
431
432
433
434
435
436
437
    if (window->context.major < ctxconfig->major ||
        (window->context.major == ctxconfig->major &&
         window->context.minor < ctxconfig->minor))
    {
        // The desired OpenGL version is greater than the actual version
        // This only happens if the machine lacks {GLX|WGL}_ARB_create_context
        // /and/ the user has requested an OpenGL version greater than 1.0

        // For API consistency, we emulate the behavior of the
        // {GLX|WGL}_ARB_create_context extension and fail here

438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
        if (window->context.client == GLFW_OPENGL_API)
        {
            _glfwInputError(GLFW_VERSION_UNAVAILABLE,
                            "Requested OpenGL version %i.%i, got version %i.%i",
                            ctxconfig->major, ctxconfig->minor,
                            window->context.major, window->context.minor);
        }
        else
        {
            _glfwInputError(GLFW_VERSION_UNAVAILABLE,
                            "Requested OpenGL ES version %i.%i, got version %i.%i",
                            ctxconfig->major, ctxconfig->minor,
                            window->context.major, window->context.minor);
        }

453
        glfwMakeContextCurrent((GLFWwindow*) previous);
Camilla Berglund's avatar
Cleanup    
Camilla Berglund committed
454
455
456
        return GLFW_FALSE;
    }

457
    if (window->context.major >= 3)
Camilla Berglund's avatar
Camilla Berglund committed
458
459
460
461
462
    {
        // OpenGL 3.0+ uses a different function for extension string retrieval
        // We cache it here instead of in glfwExtensionSupported mostly to alert
        // users as early as possible that their build may be broken

463
        window->context.GetStringi = (PFNGLGETSTRINGIPROC)
464
            window->context.getProcAddress("glGetStringi");
465
        if (!window->context.GetStringi)
Camilla Berglund's avatar
Camilla Berglund committed
466
        {
467
468
            _glfwInputError(GLFW_PLATFORM_ERROR,
                            "Entry point retrieval is broken");
469
            glfwMakeContextCurrent((GLFWwindow*) previous);
470
            return GLFW_FALSE;
Camilla Berglund's avatar
Camilla Berglund committed
471
        }
472
    }
473

474
    if (window->context.client == GLFW_OPENGL_API)
475
    {
476
        // Read back context flags (OpenGL 3.0 and above)
477
        if (window->context.major >= 3)
Camilla Berglund's avatar
Camilla Berglund committed
478
479
        {
            GLint flags;
480
            window->context.GetIntegerv(GL_CONTEXT_FLAGS, &flags);
Camilla Berglund's avatar
Camilla Berglund committed
481
482

            if (flags & GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT)
483
                window->context.forward = GLFW_TRUE;
484

485
            if (flags & GL_CONTEXT_FLAG_DEBUG_BIT)
486
                window->context.debug = GLFW_TRUE;
487
            else if (glfwExtensionSupported("GL_ARB_debug_output") &&
488
                     ctxconfig->debug)
489
490
            {
                // HACK: This is a workaround for older drivers (pre KHR_debug)
491
492
                //       not setting the debug bit in the context flags for
                //       debug contexts
493
                window->context.debug = GLFW_TRUE;
494
            }
495
496
497

            if (flags & GL_CONTEXT_FLAG_NO_ERROR_BIT_KHR)
                window->context.noerror = GLFW_TRUE;
Camilla Berglund's avatar
Camilla Berglund committed
498
        }
499

500
        // Read back OpenGL context profile (OpenGL 3.2 and above)
501
        if (window->context.major >= 4 ||
502
            (window->context.major == 3 && window->context.minor >= 2))
Camilla Berglund's avatar
Camilla Berglund committed
503
504
        {
            GLint mask;
505
            window->context.GetIntegerv(GL_CONTEXT_PROFILE_MASK, &mask);
Camilla Berglund's avatar
Camilla Berglund committed
506
507

            if (mask & GL_CONTEXT_COMPATIBILITY_PROFILE_BIT)
508
                window->context.profile = GLFW_OPENGL_COMPAT_PROFILE;
Camilla Berglund's avatar
Camilla Berglund committed
509
            else if (mask & GL_CONTEXT_CORE_PROFILE_BIT)
510
                window->context.profile = GLFW_OPENGL_CORE_PROFILE;
511
512
513
514
515
516
            else if (glfwExtensionSupported("GL_ARB_compatibility"))
            {
                // HACK: This is a workaround for the compatibility profile bit
                //       not being set in the context flags if an OpenGL 3.2+
                //       context was created without having requested a specific
                //       version
517
                window->context.profile = GLFW_OPENGL_COMPAT_PROFILE;
518
            }
Camilla Berglund's avatar
Camilla Berglund committed
519
        }
520
521
522
523
524

        // Read back robustness strategy
        if (glfwExtensionSupported("GL_ARB_robustness"))
        {
            // NOTE: We avoid using the context flags for detection, as they are
Camilla Berglund's avatar
Camilla Berglund committed
525
            //       only present from 3.0 while the extension applies from 1.1
526
527

            GLint strategy;
528
529
            window->context.GetIntegerv(GL_RESET_NOTIFICATION_STRATEGY_ARB,
                                        &strategy);
530
531

            if (strategy == GL_LOSE_CONTEXT_ON_RESET_ARB)
532
                window->context.robustness = GLFW_LOSE_CONTEXT_ON_RESET;
533
            else if (strategy == GL_NO_RESET_NOTIFICATION_ARB)
534
                window->context.robustness = GLFW_NO_RESET_NOTIFICATION;
535
536
537
538
539
540
541
542
        }
    }
    else
    {
        // Read back robustness strategy
        if (glfwExtensionSupported("GL_EXT_robustness"))
        {
            // NOTE: The values of these constants match those of the OpenGL ARB
Camilla Berglund's avatar
Camilla Berglund committed
543
            //       one, so we can reuse them here
544
545

            GLint strategy;
546
547
            window->context.GetIntegerv(GL_RESET_NOTIFICATION_STRATEGY_ARB,
                                        &strategy);
548
549

            if (strategy == GL_LOSE_CONTEXT_ON_RESET_ARB)
550
                window->context.robustness = GLFW_LOSE_CONTEXT_ON_RESET;
551
            else if (strategy == GL_NO_RESET_NOTIFICATION_ARB)
552
                window->context.robustness = GLFW_NO_RESET_NOTIFICATION;
553
        }
554
    }
555
556
557
558

    if (glfwExtensionSupported("GL_KHR_context_flush_control"))
    {
        GLint behavior;
559
        window->context.GetIntegerv(GL_CONTEXT_RELEASE_BEHAVIOR, &behavior);
560
561
562
563
564
565

        if (behavior == GL_NONE)
            window->context.release = GLFW_RELEASE_BEHAVIOR_NONE;
        else if (behavior == GL_CONTEXT_RELEASE_BEHAVIOR_FLUSH)
            window->context.release = GLFW_RELEASE_BEHAVIOR_FLUSH;
    }
566

567
568
569
    // Clearing the front buffer to black to avoid garbage pixels left over from
    // previous uses of our bit of VRAM
    {
570
571
        PFNGLCLEARPROC glClear = (PFNGLCLEARPROC)
            window->context.getProcAddress("glClear");
572
        glClear(GL_COLOR_BUFFER_BIT);
573
        window->context.swapBuffers(window);
574
575
    }

576
    glfwMakeContextCurrent((GLFWwindow*) previous);
577
    return GLFW_TRUE;
578
579
}

580
581
// Searches an extension string for the specified extension
//
Camilla Berglund's avatar
Camilla Berglund committed
582
GLFWbool _glfwStringInExtensionString(const char* string, const char* extensions)
Camilla Berglund's avatar
Camilla Berglund committed
583
{
584
    const char* start = extensions;
Camilla Berglund's avatar
Camilla Berglund committed
585

586
    for (;;)
Camilla Berglund's avatar
Camilla Berglund committed
587
    {
588
589
        const char* where;
        const char* terminator;
Camilla Berglund's avatar
Camilla Berglund committed
590

591
        where = strstr(start, string);
592
        if (!where)
593
            return GLFW_FALSE;
594

595
596
        terminator = where + strlen(string);
        if (where == start || *(where - 1) == ' ')
Camilla Berglund's avatar
Camilla Berglund committed
597
        {
598
            if (*terminator == ' ' || *terminator == '\0')
Camilla Berglund's avatar
Camilla Berglund committed
599
600
                break;
        }
601

Camilla Berglund's avatar
Camilla Berglund committed
602
603
604
        start = terminator;
    }

605
    return GLFW_TRUE;
Camilla Berglund's avatar
Camilla Berglund committed
606
607
608
}


609
610
611
//////////////////////////////////////////////////////////////////////////
//////                        GLFW public API                       //////
//////////////////////////////////////////////////////////////////////////
Camilla Berglund's avatar
Camilla Berglund committed
612

613
GLFWAPI void glfwMakeContextCurrent(GLFWwindow* handle)
614
615
{
    _GLFWwindow* window = (_GLFWwindow*) handle;
616
    _GLFWwindow* previous = _glfwPlatformGetTls(&_glfw.contextSlot);
617

618
    _GLFW_REQUIRE_INIT();
619

620
    if (window && window->context.client == GLFW_NO_API)
621
    {
622
623
        _glfwInputError(GLFW_NO_WINDOW_CONTEXT,
                        "Cannot make current with a window that has no OpenGL or OpenGL ES context");
624
625
626
        return;
    }

627
628
629
    if (previous)
    {
        if (!window || window->context.source != previous->context.source)
Camilla Berglund's avatar
Cleanup    
Camilla Berglund committed
630
            previous->context.makeCurrent(NULL);
631
632
633
    }

    if (window)
Camilla Berglund's avatar
Cleanup    
Camilla Berglund committed
634
        window->context.makeCurrent(window);
635
636
}

637
GLFWAPI GLFWwindow* glfwGetCurrentContext(void)
638
{
639
    _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
640
    return _glfwPlatformGetTls(&_glfw.contextSlot);
641
642
}

643
GLFWAPI void glfwSwapBuffers(GLFWwindow* handle)
644
{
645
    _GLFWwindow* window = (_GLFWwindow*) handle;
Camilla Berglund's avatar
Camilla Berglund committed
646
    assert(window != NULL);
647

648
    _GLFW_REQUIRE_INIT();
649

650
    if (window->context.client == GLFW_NO_API)
651
    {
652
653
        _glfwInputError(GLFW_NO_WINDOW_CONTEXT,
                        "Cannot swap buffers of a window that has no OpenGL or OpenGL ES context");
654
655
656
        return;
    }

657
    window->context.swapBuffers(window);
658
659
660
661
}

GLFWAPI void glfwSwapInterval(int interval)
{
662
663
    _GLFWwindow* window;

664
    _GLFW_REQUIRE_INIT();
665

666
    window = _glfwPlatformGetTls(&_glfw.contextSlot);
667
    if (!window)
668
    {
669
670
        _glfwInputError(GLFW_NO_CURRENT_CONTEXT,
                        "Cannot set swap interval without a current OpenGL or OpenGL ES context");
671
672
673
        return;
    }

674
    window->context.swapInterval(interval);
675
676
}

677
GLFWAPI int glfwExtensionSupported(const char* extension)
Camilla Berglund's avatar
Camilla Berglund committed
678
{
Camilla Berglund's avatar
Camilla Berglund committed
679
    _GLFWwindow* window;
Camilla Berglund's avatar
Camilla Berglund committed
680
    assert(extension != NULL);
681

682
    _GLFW_REQUIRE_INIT_OR_RETURN(GLFW_FALSE);
Camilla Berglund's avatar
Camilla Berglund committed
683

684
    window = _glfwPlatformGetTls(&_glfw.contextSlot);
Camilla Berglund's avatar
Camilla Berglund committed
685
    if (!window)
686
    {
687
688
        _glfwInputError(GLFW_NO_CURRENT_CONTEXT,
                        "Cannot query extension without a current OpenGL or OpenGL ES context");
689
        return GLFW_FALSE;
690
691
    }

692
    if (*extension == '\0')
693
    {
694
        _glfwInputError(GLFW_INVALID_VALUE, "Extension name cannot be an empty string");
695
        return GLFW_FALSE;
696
    }
Camilla Berglund's avatar
Camilla Berglund committed
697

698
    if (window->context.major >= 3)
Camilla Berglund's avatar
Camilla Berglund committed
699
    {
700
701
702
        int i;
        GLint count;

Camilla Berglund's avatar
Camilla Berglund committed
703
704
        // Check if extension is in the modern OpenGL extensions string list

705
        window->context.GetIntegerv(GL_NUM_EXTENSIONS, &count);
Camilla Berglund's avatar
Camilla Berglund committed
706

707
        for (i = 0;  i < count;  i++)
Camilla Berglund's avatar
Camilla Berglund committed
708
        {
709
710
            const char* en = (const char*)
                window->context.GetStringi(GL_EXTENSIONS, i);
711
            if (!en)
712
            {
713
                _glfwInputError(GLFW_PLATFORM_ERROR,
Camilla Berglund's avatar
Camilla Berglund committed
714
                                "Extension string retrieval is broken");
715
                return GLFW_FALSE;
716
            }
717
718

            if (strcmp(en, extension) == 0)
719
                return GLFW_TRUE;
Camilla Berglund's avatar
Camilla Berglund committed
720
721
        }
    }
722
723
724
725
    else
    {
        // Check if extension is in the old style OpenGL extensions string

726
727
        const char* extensions = (const char*)
            window->context.GetString(GL_EXTENSIONS);
728
729
730
        if (!extensions)
        {
            _glfwInputError(GLFW_PLATFORM_ERROR,
Camilla Berglund's avatar
Camilla Berglund committed
731
                            "Extension string retrieval is broken");
732
            return GLFW_FALSE;
733
734
735
        }

        if (_glfwStringInExtensionString(extension, extensions))
736
            return GLFW_TRUE;
737
    }
Camilla Berglund's avatar
Camilla Berglund committed
738

739
    // Check if extension is in the platform-specific string
740
    return window->context.extensionSupported(extension);
Camilla Berglund's avatar
Camilla Berglund committed
741
742
}

743
GLFWAPI GLFWglproc glfwGetProcAddress(const char* procname)
Camilla Berglund's avatar
Camilla Berglund committed
744
{
745
    _GLFWwindow* window;
Camilla Berglund's avatar
Camilla Berglund committed
746
    assert(procname != NULL);
747

748
    _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
749

750
    window = _glfwPlatformGetTls(&_glfw.contextSlot);
751
    if (!window)
752
    {
753
754
        _glfwInputError(GLFW_NO_CURRENT_CONTEXT,
                        "Cannot query entry point without a current OpenGL or OpenGL ES context");
Camilla Berglund's avatar
Camilla Berglund committed
755
        return NULL;
756
    }
Camilla Berglund's avatar
Camilla Berglund committed
757

758
    return window->context.getProcAddress(procname);
Camilla Berglund's avatar
Camilla Berglund committed
759
760
}

761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
GLFWAPI GLFWusercontext* glfwCreateUserContext(GLFWwindow* handle)
{
    _GLFWusercontext* context;
    _GLFWwindow* window = (_GLFWwindow*)handle;

    context = _glfwPlatformCreateUserContext(window);

    return (GLFWusercontext*)context;
}

GLFWAPI void glfwDestroyUserContext(GLFWusercontext* handle)
{
    _GLFWusercontext* context = (_GLFWusercontext*)handle;

    _glfwPlatformDestroyUserContext(context);
}

GLFWAPI void glfwMakeUserContextCurrent(GLFWusercontext* handle)
{
    _GLFWusercontext* context = (_GLFWusercontext*)handle;

    _glfwPlatformMakeUserContextCurrent(context);
}