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