src/video/cocoa/SDL_cocoaopengl.m
author Sam Lantinga <slouken@libsdl.org>
Fri, 27 Dec 2013 10:18:11 -0800
changeset 8083 3782a12331d6
parent 7897 622d75108e60
child 8093 b43765095a6f
permissions -rw-r--r--
Bump SDL to build with 10.7 SDK.

This also bumps the minimum requirement for building SDL to 10.7, and
removes some checking we no longer need.

CR: saml
slouken@1936
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
slouken@1936
     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@1936
     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@1936
    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@1936
    20
*/
slouken@1936
    21
#include "SDL_config.h"
slouken@1936
    22
slouken@1936
    23
/* NSOpenGL implementation of SDL OpenGL support */
slouken@1936
    24
slouken@1952
    25
#if SDL_VIDEO_OPENGL_CGL
slouken@6044
    26
#include "SDL_cocoavideo.h"
jorgen@7594
    27
#include "SDL_cocoaopengl.h"
slouken@6044
    28
slouken@1936
    29
#include <OpenGL/CGLTypes.h>
slouken@2738
    30
#include <OpenGL/OpenGL.h>
slouken@3570
    31
#include <OpenGL/CGLRenderers.h>
slouken@1936
    32
slouken@1936
    33
#include "SDL_loadso.h"
slouken@1936
    34
#include "SDL_opengl.h"
slouken@1936
    35
icculus@6567
    36
#define DEFAULT_OPENGL  "/System/Library/Frameworks/OpenGL.framework/Libraries/libGL.dylib"
slouken@1936
    37
slouken@6952
    38
#ifndef kCGLPFAOpenGLProfile
icculus@6567
    39
#define kCGLPFAOpenGLProfile 99
slouken@6952
    40
#endif
slouken@6952
    41
#ifndef kCGLOGLPVersion_Legacy
icculus@6567
    42
#define kCGLOGLPVersion_Legacy 0x1000
slouken@6952
    43
#endif
icculus@7894
    44
#ifndef kCGLOGLPVersion_GL3_Core
icculus@7894
    45
#define kCGLOGLPVersion_GL3_Core 0x3200
icculus@7894
    46
#endif
icculus@7894
    47
#ifndef kCGLOGLPVersion_GL4_Core
icculus@7894
    48
#define kCGLOGLPVersion_GL4_Core 0x4100
icculus@6567
    49
#endif
icculus@6567
    50
jorgen@7594
    51
@implementation SDLOpenGLContext : NSOpenGLContext
jorgen@7594
    52
jorgen@7594
    53
- (id)initWithFormat:(NSOpenGLPixelFormat *)format
jorgen@7594
    54
        shareContext:(NSOpenGLContext *)share
jorgen@7594
    55
{
jorgen@7595
    56
    self = [super initWithFormat:format shareContext:share];
jorgen@7595
    57
    if (self) {
jorgen@7595
    58
        SDL_AtomicSet(&self->dirty, 0);
jorgen@7595
    59
        self->window = NULL;
jorgen@7595
    60
    }
jorgen@7595
    61
    return self;
jorgen@7594
    62
}
jorgen@7594
    63
jorgen@7594
    64
- (void)scheduleUpdate
jorgen@7594
    65
{
jorgen@7594
    66
    SDL_AtomicAdd(&self->dirty, 1);
jorgen@7594
    67
}
jorgen@7594
    68
jorgen@7594
    69
/* This should only be called on the thread on which a user is using the context. */
jorgen@7594
    70
- (void)updateIfNeeded
jorgen@7594
    71
{
jorgen@7594
    72
    int value = SDL_AtomicSet(&self->dirty, 0);
jorgen@7594
    73
    if (value > 0) {
jorgen@7594
    74
        /* We call the real underlying update here, since -[SDLOpenGLContext update] just calls us. */
jorgen@7594
    75
        [super update];
jorgen@7594
    76
    }
jorgen@7594
    77
}
jorgen@7594
    78
jorgen@7594
    79
/* This should only be called on the thread on which a user is using the context. */
jorgen@7594
    80
- (void)update
jorgen@7594
    81
{
jorgen@7594
    82
    /* This ensures that regular 'update' calls clear the atomic dirty flag. */
jorgen@7594
    83
    [self scheduleUpdate];
jorgen@7594
    84
    [self updateIfNeeded];
jorgen@7594
    85
}
jorgen@7594
    86
jorgen@7595
    87
/* Updates the drawable for the contexts and manages related state. */
jorgen@7595
    88
- (void)setWindow:(SDL_Window *)newWindow
jorgen@7595
    89
{
jorgen@7595
    90
    if (self->window) {
jorgen@7595
    91
        SDL_WindowData *oldwindowdata = (SDL_WindowData *)self->window->driverdata;
jorgen@7595
    92
jorgen@7595
    93
        /* Make sure to remove us from the old window's context list, or we'll get scheduled updates from it too. */
jorgen@7595
    94
        NSMutableArray *contexts = oldwindowdata->nscontexts;
jorgen@7595
    95
        @synchronized (contexts) {
jorgen@7595
    96
            [contexts removeObject:self];
jorgen@7595
    97
        }
jorgen@7595
    98
    }
jorgen@7595
    99
jorgen@7595
   100
    self->window = newWindow;
jorgen@7595
   101
jorgen@7595
   102
    if (newWindow) {
jorgen@7595
   103
        SDL_WindowData *windowdata = (SDL_WindowData *)newWindow->driverdata;
jorgen@7595
   104
jorgen@7595
   105
        /* Now sign up for scheduled updates for the new window. */
jorgen@7595
   106
        NSMutableArray *contexts = windowdata->nscontexts;
jorgen@7595
   107
        @synchronized (contexts) {
jorgen@7595
   108
            [contexts addObject:self];
jorgen@7595
   109
        }
jorgen@7595
   110
jorgen@7595
   111
        if ([self view] != [windowdata->nswindow contentView]) {
jorgen@7595
   112
            [self setView:[windowdata->nswindow contentView]];
jorgen@7595
   113
            [self scheduleUpdate];
jorgen@7595
   114
        }
jorgen@7595
   115
    } else {
jorgen@7595
   116
        [self clearDrawable];
jorgen@7595
   117
        [self scheduleUpdate];
jorgen@7595
   118
    }
jorgen@7595
   119
}
jorgen@7595
   120
jorgen@7594
   121
@end
jorgen@7594
   122
slouken@1936
   123
slouken@1936
   124
int
slouken@1936
   125
Cocoa_GL_LoadLibrary(_THIS, const char *path)
slouken@1936
   126
{
slouken@3057
   127
    /* Load the OpenGL library */
slouken@1936
   128
    if (path == NULL) {
slouken@1952
   129
        path = SDL_getenv("SDL_OPENGL_LIBRARY");
slouken@1952
   130
    }
slouken@1952
   131
    if (path == NULL) {
slouken@1952
   132
        path = DEFAULT_OPENGL;
slouken@1936
   133
    }
slouken@1936
   134
    _this->gl_config.dll_handle = SDL_LoadObject(path);
slouken@1936
   135
    if (!_this->gl_config.dll_handle) {
slouken@1936
   136
        return -1;
slouken@1936
   137
    }
slouken@1936
   138
    SDL_strlcpy(_this->gl_config.driver_path, path,
slouken@1936
   139
                SDL_arraysize(_this->gl_config.driver_path));
slouken@1936
   140
    return 0;
slouken@1936
   141
}
slouken@1936
   142
slouken@1936
   143
void *
slouken@1936
   144
Cocoa_GL_GetProcAddress(_THIS, const char *proc)
slouken@1936
   145
{
slouken@1936
   146
    return SDL_LoadFunction(_this->gl_config.dll_handle, proc);
slouken@1936
   147
}
slouken@1936
   148
slouken@3057
   149
void
slouken@1936
   150
Cocoa_GL_UnloadLibrary(_THIS)
slouken@1936
   151
{
slouken@3057
   152
    SDL_UnloadObject(_this->gl_config.dll_handle);
slouken@3057
   153
    _this->gl_config.dll_handle = NULL;
slouken@1936
   154
}
slouken@1936
   155
slouken@1936
   156
SDL_GLContext
slouken@1936
   157
Cocoa_GL_CreateContext(_THIS, SDL_Window * window)
slouken@1936
   158
{
icculus@6567
   159
    const int wantver = (_this->gl_config.major_version << 8) |
icculus@6567
   160
                        (_this->gl_config.minor_version);
icculus@6567
   161
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@6848
   162
    NSAutoreleasePool *pool;
slouken@5246
   163
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@1936
   164
    SDL_DisplayData *displaydata = (SDL_DisplayData *)display->driverdata;
slouken@1936
   165
    NSOpenGLPixelFormatAttribute attr[32];
slouken@1936
   166
    NSOpenGLPixelFormat *fmt;
jorgen@7594
   167
    SDLOpenGLContext *context;
flibitijibibo@7152
   168
    NSOpenGLContext *share_context = nil;
slouken@1936
   169
    int i = 0;
slouken@1936
   170
icculus@6567
   171
    if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
icculus@7894
   172
        SDL_SetError ("OpenGL ES is not supported on this platform");
icculus@6567
   173
        return NULL;
icculus@6567
   174
    }
icculus@6567
   175
icculus@6567
   176
    /* Sadly, we'll have to update this as life progresses, since we need to
icculus@6567
   177
       set an enum for context profiles, not a context version number */
icculus@7894
   178
    if (wantver > 0x0401) {
icculus@7894
   179
        SDL_SetError ("OpenGL > 4.1 is not supported on this platform");
icculus@6567
   180
        return NULL;
icculus@6567
   181
    }
icculus@6567
   182
slouken@6848
   183
    pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   184
slouken@6848
   185
    /* specify a profile if we're on Lion (10.7) or later. */
slouken@6848
   186
    if (data->osversion >= 0x1070) {
slouken@6848
   187
        NSOpenGLPixelFormatAttribute profile = kCGLOGLPVersion_Legacy;
slouken@6848
   188
        if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_CORE) {
slouken@6848
   189
            if (wantver == 0x0302) {
icculus@7894
   190
                profile = kCGLOGLPVersion_GL3_Core;
icculus@7894
   191
            } else if ((wantver == 0x0401) && (data->osversion >= 0x1090)) {
icculus@7894
   192
                profile = kCGLOGLPVersion_GL4_Core;
icculus@7894
   193
            } else {
icculus@7894
   194
                SDL_SetError("Requested GL version is not supported on this platform");
icculus@7894
   195
                [pool release];
icculus@7894
   196
                return NULL;
alexey@6832
   197
            }
alexey@6832
   198
        }
slouken@6848
   199
        attr[i++] = kCGLPFAOpenGLProfile;
slouken@6848
   200
        attr[i++] = profile;
slouken@6848
   201
    }
slouken@1936
   202
slouken@6848
   203
    attr[i++] = NSOpenGLPFAColorSize;
slouken@6848
   204
    attr[i++] = SDL_BYTESPERPIXEL(display->current_mode.format)*8;
slouken@6848
   205
slouken@6848
   206
    attr[i++] = NSOpenGLPFADepthSize;
slouken@6848
   207
    attr[i++] = _this->gl_config.depth_size;
slouken@6848
   208
slouken@6848
   209
    if (_this->gl_config.double_buffer) {
slouken@6848
   210
        attr[i++] = NSOpenGLPFADoubleBuffer;
slouken@6848
   211
    }
slouken@6848
   212
slouken@6848
   213
    if (_this->gl_config.stereo) {
slouken@6848
   214
        attr[i++] = NSOpenGLPFAStereo;
slouken@6848
   215
    }
slouken@6848
   216
slouken@6848
   217
    if (_this->gl_config.stencil_size) {
slouken@6848
   218
        attr[i++] = NSOpenGLPFAStencilSize;
slouken@6848
   219
        attr[i++] = _this->gl_config.stencil_size;
slouken@6848
   220
    }
slouken@6848
   221
slouken@6848
   222
    if ((_this->gl_config.accum_red_size +
slouken@6848
   223
         _this->gl_config.accum_green_size +
slouken@6848
   224
         _this->gl_config.accum_blue_size +
slouken@6848
   225
         _this->gl_config.accum_alpha_size) > 0) {
slouken@6848
   226
        attr[i++] = NSOpenGLPFAAccumSize;
slouken@6848
   227
        attr[i++] = _this->gl_config.accum_red_size + _this->gl_config.accum_green_size + _this->gl_config.accum_blue_size + _this->gl_config.accum_alpha_size;
slouken@6848
   228
    }
slouken@6848
   229
slouken@6848
   230
    if (_this->gl_config.multisamplebuffers) {
slouken@6848
   231
        attr[i++] = NSOpenGLPFASampleBuffers;
slouken@6848
   232
        attr[i++] = _this->gl_config.multisamplebuffers;
slouken@6848
   233
    }
slouken@6848
   234
slouken@6848
   235
    if (_this->gl_config.multisamplesamples) {
slouken@6848
   236
        attr[i++] = NSOpenGLPFASamples;
slouken@6848
   237
        attr[i++] = _this->gl_config.multisamplesamples;
slouken@6848
   238
        attr[i++] = NSOpenGLPFANoRecovery;
slouken@6848
   239
    }
slouken@6848
   240
slouken@6848
   241
    if (_this->gl_config.accelerated >= 0) {
slouken@6848
   242
        if (_this->gl_config.accelerated) {
slouken@6848
   243
            attr[i++] = NSOpenGLPFAAccelerated;
slouken@6848
   244
        } else {
slouken@6848
   245
            attr[i++] = NSOpenGLPFARendererID;
slouken@6848
   246
            attr[i++] = kCGLRendererGenericFloatID;
alexey@6832
   247
        }
slouken@6848
   248
    }
slouken@6848
   249
slouken@6848
   250
    attr[i++] = NSOpenGLPFAScreenMask;
slouken@6848
   251
    attr[i++] = CGDisplayIDToOpenGLDisplayMask(displaydata->display);
slouken@6848
   252
    attr[i] = 0;
slouken@6848
   253
slouken@6848
   254
    fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attr];
slouken@6848
   255
    if (fmt == nil) {
slouken@6848
   256
        SDL_SetError ("Failed creating OpenGL pixel format");
slouken@6848
   257
        [pool release];
slouken@6848
   258
        return NULL;
slouken@6848
   259
    }
slouken@6848
   260
flibitijibibo@7152
   261
    if (_this->gl_config.share_with_current_context) {
slouken@7412
   262
        share_context = (NSOpenGLContext*)SDL_GL_GetCurrentContext();
flibitijibibo@7152
   263
    }
flibitijibibo@7152
   264
jorgen@7594
   265
    context = [[SDLOpenGLContext alloc] initWithFormat:fmt shareContext:share_context];
slouken@6848
   266
slouken@6848
   267
    [fmt release];
slouken@6848
   268
slouken@6848
   269
    if (context == nil) {
slouken@6848
   270
        SDL_SetError ("Failed creating OpenGL context");
slouken@6848
   271
        [pool release];
slouken@6848
   272
        return NULL;
slouken@6848
   273
    }
slouken@6848
   274
slouken@6848
   275
    [pool release];
slouken@1936
   276
slouken@2178
   277
    if ( Cocoa_GL_MakeCurrent(_this, window, context) < 0 ) {
slouken@2178
   278
        Cocoa_GL_DeleteContext(_this, context);
slouken@2178
   279
        return NULL;
slouken@2178
   280
    }
slouken@2178
   281
slouken@2178
   282
    return context;
slouken@1936
   283
}
slouken@1936
   284
slouken@1936
   285
int
slouken@1936
   286
Cocoa_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
slouken@1936
   287
{
slouken@6848
   288
    NSAutoreleasePool *pool;
slouken@1936
   289
slouken@6848
   290
    pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   291
slouken@6848
   292
    if (context) {
jorgen@7594
   293
        SDLOpenGLContext *nscontext = (SDLOpenGLContext *)context;
jorgen@7595
   294
        [nscontext setWindow:window];
jorgen@7594
   295
        [nscontext updateIfNeeded];
slouken@6848
   296
        [nscontext makeCurrentContext];
slouken@6848
   297
    } else {
slouken@6848
   298
        [NSOpenGLContext clearCurrentContext];
slouken@1936
   299
    }
slouken@1936
   300
slouken@6848
   301
    [pool release];
slouken@1936
   302
    return 0;
slouken@1936
   303
}
slouken@1936
   304
urkle@7746
   305
void
urkle@7746
   306
Cocoa_GL_GetDrawableSize(_THIS, SDL_Window * window, int * w, int * h)
urkle@7746
   307
{
urkle@7746
   308
    SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
urkle@7746
   309
    NSView *contentView = [windata->nswindow contentView];
urkle@7746
   310
    NSRect viewport = [contentView bounds];
urkle@7746
   311
urkle@7746
   312
    /* This gives us the correct viewport for a Retina-enabled view, only
urkle@7746
   313
     * supported on 10.7+. */
urkle@7746
   314
    if ([contentView respondsToSelector:@selector(convertRectToBacking:)]) {
urkle@7746
   315
        viewport = [contentView convertRectToBacking:viewport];
urkle@7746
   316
    }
urkle@7746
   317
urkle@7746
   318
    if (w) {
urkle@7746
   319
        *w = viewport.size.width;
urkle@7746
   320
    }
urkle@7746
   321
urkle@7746
   322
    if (h) {
urkle@7746
   323
        *h = viewport.size.height;
urkle@7746
   324
    }
urkle@7746
   325
}
urkle@7746
   326
slouken@1936
   327
int
slouken@1936
   328
Cocoa_GL_SetSwapInterval(_THIS, int interval)
slouken@1936
   329
{
slouken@6848
   330
    NSAutoreleasePool *pool;
slouken@1936
   331
    NSOpenGLContext *nscontext;
slouken@2703
   332
    GLint value;
slouken@1936
   333
    int status;
slouken@1936
   334
icculus@7743
   335
    if (interval < 0) {  /* no extension for this on Mac OS X at the moment. */
icculus@7743
   336
        return SDL_SetError("Late swap tearing currently unsupported");
icculus@7743
   337
    }
icculus@7743
   338
slouken@6848
   339
    pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   340
jorgen@7594
   341
    nscontext = (NSOpenGLContext*)SDL_GL_GetCurrentContext();
slouken@6848
   342
    if (nscontext != nil) {
slouken@6848
   343
        value = interval;
slouken@6848
   344
        [nscontext setValues:&value forParameter:NSOpenGLCPSwapInterval];
slouken@6848
   345
        status = 0;
slouken@6848
   346
    } else {
icculus@7037
   347
        status = SDL_SetError("No current OpenGL context");
slouken@1936
   348
    }
slouken@1936
   349
slouken@6848
   350
    [pool release];
slouken@1936
   351
    return status;
slouken@1936
   352
}
slouken@1936
   353
slouken@1936
   354
int
slouken@1936
   355
Cocoa_GL_GetSwapInterval(_THIS)
slouken@1936
   356
{
slouken@6848
   357
    NSAutoreleasePool *pool;
slouken@1936
   358
    NSOpenGLContext *nscontext;
slouken@2703
   359
    GLint value;
icculus@6382
   360
    int status = 0;
slouken@1936
   361
slouken@6848
   362
    pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   363
jorgen@7594
   364
    nscontext = (NSOpenGLContext*)SDL_GL_GetCurrentContext();
slouken@6848
   365
    if (nscontext != nil) {
slouken@6848
   366
        [nscontext getValues:&value forParameter:NSOpenGLCPSwapInterval];
slouken@6848
   367
        status = (int)value;
slouken@1936
   368
    }
slouken@1936
   369
slouken@6848
   370
    [pool release];
slouken@1936
   371
    return status;
slouken@1936
   372
}
slouken@1936
   373
slouken@1936
   374
void
slouken@1936
   375
Cocoa_GL_SwapWindow(_THIS, SDL_Window * window)
slouken@1936
   376
{
slouken@6848
   377
    NSAutoreleasePool *pool;
slouken@1936
   378
slouken@6848
   379
    pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   380
slouken@7738
   381
    SDLOpenGLContext* nscontext = (SDLOpenGLContext*)SDL_GL_GetCurrentContext();
slouken@7408
   382
    [nscontext flushBuffer];
jorgen@7594
   383
    [nscontext updateIfNeeded];
slouken@6848
   384
slouken@6848
   385
    [pool release];
slouken@1936
   386
}
slouken@1936
   387
slouken@1936
   388
void
slouken@1936
   389
Cocoa_GL_DeleteContext(_THIS, SDL_GLContext context)
slouken@1936
   390
{
slouken@6848
   391
    NSAutoreleasePool *pool;
jorgen@7595
   392
    SDLOpenGLContext *nscontext = (SDLOpenGLContext *)context;
slouken@1936
   393
slouken@6848
   394
    pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   395
jorgen@7595
   396
    [nscontext setWindow:NULL];
slouken@6848
   397
    [nscontext release];
slouken@6848
   398
slouken@6848
   399
    [pool release];
slouken@1936
   400
}
slouken@1936
   401
slouken@1952
   402
#endif /* SDL_VIDEO_OPENGL_CGL */
slouken@1936
   403
slouken@1936
   404
/* vi: set ts=4 sw=4 expandtab: */