src/video/cocoa/SDL_cocoaopengl.m
author Sam Lantinga <slouken@libsdl.org>
Mon, 26 May 2008 12:56:10 +0000
changeset 2703 18a472b6d745
parent 2178 114a541cfae2
child 2738 79c1bd651f04
permissions -rw-r--r--
Fixed compiler warning
slouken@1936
     1
/*
slouken@1936
     2
    SDL - Simple DirectMedia Layer
slouken@1936
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@1936
     4
slouken@1936
     5
    This library is free software; you can redistribute it and/or
slouken@1936
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1936
     7
    License as published by the Free Software Foundation; either
slouken@1936
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1936
     9
slouken@1936
    10
    This library is distributed in the hope that it will be useful,
slouken@1936
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1936
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1936
    13
    Lesser General Public License for more details.
slouken@1936
    14
slouken@1936
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1936
    16
    License along with this library; if not, write to the Free Software
slouken@1936
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1936
    18
slouken@1936
    19
    Sam Lantinga
slouken@1936
    20
    slouken@libsdl.org
slouken@1936
    21
*/
slouken@1936
    22
#include "SDL_config.h"
slouken@1936
    23
slouken@1936
    24
#include "SDL_cocoavideo.h"
slouken@1936
    25
slouken@1936
    26
/* NSOpenGL implementation of SDL OpenGL support */
slouken@1936
    27
slouken@1952
    28
#if SDL_VIDEO_OPENGL_CGL
slouken@1936
    29
#include <OpenGL/CGLTypes.h>
slouken@1936
    30
slouken@1936
    31
#include "SDL_loadso.h"
slouken@1936
    32
#include "SDL_opengl.h"
slouken@1936
    33
slouken@1936
    34
slouken@1952
    35
#define DEFAULT_OPENGL  "/System/Library/Frameworks/OpenGL.framework/Libraries/libGL.dylib"
slouken@1936
    36
slouken@1936
    37
/* This is implemented in Mac OS X 10.3 and above */
slouken@1936
    38
#if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_3
slouken@1936
    39
@implementation NSOpenGLContext(CGLContextAccess)
slouken@1936
    40
- (CGLContextObj)CGLContextObj;
slouken@1936
    41
{
slouken@1936
    42
    return _contextAuxiliary;
slouken@1936
    43
}
slouken@1936
    44
@end
slouken@1936
    45
#endif /* < 10.3 */
slouken@1936
    46
slouken@1936
    47
int
slouken@1936
    48
Cocoa_GL_LoadLibrary(_THIS, const char *path)
slouken@1936
    49
{
slouken@1936
    50
    if (_this->gl_config.driver_loaded) {
slouken@1936
    51
        if (path) {
slouken@1936
    52
            SDL_SetError("OpenGL library already loaded");
slouken@1936
    53
            return -1;
slouken@1936
    54
        } else {
slouken@1936
    55
            ++_this->gl_config.driver_loaded;
slouken@1936
    56
            return 0;
slouken@1936
    57
        }
slouken@1936
    58
    }
slouken@1936
    59
    if (path == NULL) {
slouken@1952
    60
        path = SDL_getenv("SDL_OPENGL_LIBRARY");
slouken@1952
    61
    }
slouken@1952
    62
    if (path == NULL) {
slouken@1952
    63
        path = DEFAULT_OPENGL;
slouken@1936
    64
    }
slouken@1936
    65
    _this->gl_config.dll_handle = SDL_LoadObject(path);
slouken@1936
    66
    if (!_this->gl_config.dll_handle) {
slouken@1936
    67
        return -1;
slouken@1936
    68
    }
slouken@1936
    69
    SDL_strlcpy(_this->gl_config.driver_path, path,
slouken@1936
    70
                SDL_arraysize(_this->gl_config.driver_path));
slouken@1936
    71
    _this->gl_config.driver_loaded = 1;
slouken@1936
    72
    return 0;
slouken@1936
    73
}
slouken@1936
    74
slouken@1936
    75
void *
slouken@1936
    76
Cocoa_GL_GetProcAddress(_THIS, const char *proc)
slouken@1936
    77
{
slouken@1936
    78
    return SDL_LoadFunction(_this->gl_config.dll_handle, proc);
slouken@1936
    79
}
slouken@1936
    80
slouken@1936
    81
static void
slouken@1936
    82
Cocoa_GL_UnloadLibrary(_THIS)
slouken@1936
    83
{
slouken@1936
    84
    if (_this->gl_config.driver_loaded > 0) {
slouken@1936
    85
        if (--_this->gl_config.driver_loaded > 0) {
slouken@1936
    86
            return;
slouken@1936
    87
        }
slouken@1936
    88
        SDL_UnloadObject(_this->gl_config.dll_handle);
slouken@1936
    89
        _this->gl_config.dll_handle = NULL;
slouken@1936
    90
    }
slouken@1936
    91
}
slouken@1936
    92
slouken@1936
    93
static int
slouken@1936
    94
Cocoa_GL_Initialize(_THIS)
slouken@1936
    95
{
slouken@1936
    96
    if (_this->gl_data) {
slouken@1936
    97
        ++_this->gl_data->initialized;
slouken@1936
    98
        return 0;
slouken@1936
    99
    }
slouken@1936
   100
slouken@1936
   101
    _this->gl_data =
slouken@1936
   102
        (struct SDL_GLDriverData *) SDL_calloc(1,
slouken@1936
   103
                                               sizeof(struct
slouken@1936
   104
                                                      SDL_GLDriverData));
slouken@1936
   105
    if (!_this->gl_data) {
slouken@1936
   106
        SDL_OutOfMemory();
slouken@1936
   107
        return -1;
slouken@1936
   108
    }
slouken@1936
   109
    _this->gl_data->initialized = 1;
slouken@1936
   110
slouken@1936
   111
    if (Cocoa_GL_LoadLibrary(_this, NULL) < 0) {
slouken@1936
   112
        return -1;
slouken@1936
   113
    }
slouken@1936
   114
    return 0;
slouken@1936
   115
}
slouken@1936
   116
slouken@1952
   117
static void
slouken@1952
   118
Cocoa_GL_Shutdown(_THIS)
slouken@1952
   119
{
slouken@1952
   120
    if (!_this->gl_data || (--_this->gl_data->initialized > 0)) {
slouken@1952
   121
        return;
slouken@1952
   122
    }
slouken@1952
   123
slouken@1952
   124
    Cocoa_GL_UnloadLibrary(_this);
slouken@1952
   125
slouken@1952
   126
    SDL_free(_this->gl_data);
slouken@1952
   127
    _this->gl_data = NULL;
slouken@1952
   128
}
slouken@1952
   129
slouken@1936
   130
int
slouken@1936
   131
Cocoa_GL_SetupWindow(_THIS, SDL_Window * window)
slouken@1936
   132
{
slouken@1936
   133
    if (Cocoa_GL_Initialize(_this) < 0) {
slouken@1936
   134
        return -1;
slouken@1936
   135
    }
slouken@1936
   136
    return 0;
slouken@1936
   137
}
slouken@1936
   138
slouken@1936
   139
void
slouken@1936
   140
Cocoa_GL_CleanupWindow(_THIS, SDL_Window * window)
slouken@1936
   141
{
slouken@1936
   142
    Cocoa_GL_Shutdown(_this);
slouken@1936
   143
}
slouken@1936
   144
slouken@1936
   145
SDL_GLContext
slouken@1936
   146
Cocoa_GL_CreateContext(_THIS, SDL_Window * window)
slouken@1936
   147
{
slouken@1936
   148
    NSAutoreleasePool *pool;
slouken@1936
   149
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1936
   150
    SDL_DisplayData *displaydata = (SDL_DisplayData *)display->driverdata;
slouken@1936
   151
    NSOpenGLPixelFormatAttribute attr[32];
slouken@1936
   152
    NSOpenGLPixelFormat *fmt;
slouken@2178
   153
    NSOpenGLContext *context;
slouken@1936
   154
    int i = 0;
slouken@1936
   155
slouken@1936
   156
    pool = [[NSAutoreleasePool alloc] init];
slouken@1936
   157
slouken@1936
   158
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1936
   159
        attr[i++] = NSOpenGLPFAFullScreen;
slouken@1936
   160
    }
slouken@1936
   161
slouken@1936
   162
    attr[i++] = NSOpenGLPFAColorSize;
slouken@1936
   163
    attr[i++] = SDL_BYTESPERPIXEL(display->current_mode.format)*8;
slouken@1936
   164
slouken@1936
   165
    attr[i++] = NSOpenGLPFADepthSize;
slouken@1936
   166
    attr[i++] = _this->gl_config.depth_size;
slouken@1936
   167
slouken@1936
   168
    if (_this->gl_config.double_buffer) {
slouken@1936
   169
        attr[i++] = NSOpenGLPFADoubleBuffer;
slouken@1936
   170
    }
slouken@1936
   171
slouken@1936
   172
    if (_this->gl_config.stereo) {
slouken@1936
   173
        attr[i++] = NSOpenGLPFAStereo;
slouken@1936
   174
    }
slouken@1936
   175
slouken@1936
   176
    if (_this->gl_config.stencil_size) {
slouken@1936
   177
        attr[i++] = NSOpenGLPFAStencilSize;
slouken@1936
   178
        attr[i++] = _this->gl_config.stencil_size;
slouken@1936
   179
    }
slouken@1936
   180
slouken@1936
   181
    if ((_this->gl_config.accum_red_size +
slouken@1936
   182
         _this->gl_config.accum_green_size +
slouken@1936
   183
         _this->gl_config.accum_blue_size +
slouken@1936
   184
         _this->gl_config.accum_alpha_size) > 0) {
slouken@1936
   185
        attr[i++] = NSOpenGLPFAAccumSize;
slouken@1936
   186
        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@1936
   187
    }
slouken@1936
   188
slouken@1936
   189
    if (_this->gl_config.multisamplebuffers) {
slouken@1936
   190
        attr[i++] = NSOpenGLPFASampleBuffers;
slouken@1936
   191
        attr[i++] = _this->gl_config.multisamplebuffers;
slouken@1936
   192
    }
slouken@1936
   193
slouken@1936
   194
    if (_this->gl_config.multisamplesamples) {
slouken@1936
   195
        attr[i++] = NSOpenGLPFASamples;
slouken@1936
   196
        attr[i++] = _this->gl_config.multisamplesamples;
slouken@1936
   197
        attr[i++] = NSOpenGLPFANoRecovery;
slouken@1936
   198
    }
slouken@1936
   199
slouken@1936
   200
    if (_this->gl_config.accelerated > 0) {
slouken@1936
   201
        attr[i++] = NSOpenGLPFAAccelerated;
slouken@1936
   202
    }
slouken@1936
   203
slouken@1936
   204
    attr[i++] = NSOpenGLPFAScreenMask;
slouken@1936
   205
    attr[i++] = CGDisplayIDToOpenGLDisplayMask(displaydata->display);
slouken@1936
   206
    attr[i] = 0;
slouken@1936
   207
slouken@1936
   208
    fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attr];
slouken@1936
   209
    if (fmt == nil) {
slouken@1936
   210
        SDL_SetError ("Failed creating OpenGL pixel format");
slouken@1936
   211
        [pool release];
slouken@1936
   212
        return NULL;
slouken@1936
   213
    }
slouken@1936
   214
slouken@2178
   215
    context = [[NSOpenGLContext alloc] initWithFormat:fmt shareContext:nil];
slouken@1936
   216
slouken@1936
   217
    [fmt release];
slouken@1936
   218
slouken@2178
   219
    if (context == nil) {
slouken@1936
   220
        SDL_SetError ("Failed creating OpenGL context");
slouken@1936
   221
        [pool release];
slouken@1936
   222
        return NULL;
slouken@1936
   223
    }
slouken@1936
   224
slouken@1936
   225
    /*
slouken@1936
   226
     * Wisdom from Apple engineer in reference to UT2003's OpenGL performance:
slouken@1936
   227
     *  "You are blowing a couple of the internal OpenGL function caches. This
slouken@1936
   228
     *  appears to be happening in the VAO case.  You can tell OpenGL to up
slouken@1936
   229
     *  the cache size by issuing the following calls right after you create
slouken@1936
   230
     *  the OpenGL context.  The default cache size is 16."    --ryan.
slouken@1936
   231
     */
slouken@1936
   232
slouken@1936
   233
    #ifndef GLI_ARRAY_FUNC_CACHE_MAX
slouken@1936
   234
    #define GLI_ARRAY_FUNC_CACHE_MAX 284
slouken@1936
   235
    #endif
slouken@1936
   236
slouken@1936
   237
    #ifndef GLI_SUBMIT_FUNC_CACHE_MAX
slouken@1936
   238
    #define GLI_SUBMIT_FUNC_CACHE_MAX 280
slouken@1936
   239
    #endif
slouken@1936
   240
slouken@1936
   241
    {
slouken@1936
   242
        long cache_max = 64;
slouken@2178
   243
        CGLContextObj ctx = [context CGLContextObj];
slouken@1936
   244
        CGLSetParameter (ctx, GLI_SUBMIT_FUNC_CACHE_MAX, &cache_max);
slouken@1936
   245
        CGLSetParameter (ctx, GLI_ARRAY_FUNC_CACHE_MAX, &cache_max);
slouken@1936
   246
    }
slouken@1936
   247
slouken@1936
   248
    /* End Wisdom from Apple Engineer section. --ryan. */
slouken@1936
   249
slouken@1936
   250
    [pool release];
slouken@2178
   251
slouken@2178
   252
    if ( Cocoa_GL_MakeCurrent(_this, window, context) < 0 ) {
slouken@2178
   253
        Cocoa_GL_DeleteContext(_this, context);
slouken@2178
   254
        return NULL;
slouken@2178
   255
    }
slouken@2178
   256
slouken@2178
   257
    return context;
slouken@1936
   258
}
slouken@1936
   259
slouken@1936
   260
int
slouken@1936
   261
Cocoa_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
slouken@1936
   262
{
slouken@1936
   263
    NSAutoreleasePool *pool;
slouken@1936
   264
slouken@1936
   265
    pool = [[NSAutoreleasePool alloc] init];
slouken@1936
   266
slouken@1936
   267
    if (context) {
slouken@1936
   268
        SDL_WindowData *windowdata = (SDL_WindowData *)window->driverdata;
slouken@1936
   269
        NSOpenGLContext *nscontext = (NSOpenGLContext *)context;
slouken@1936
   270
slouken@1973
   271
        if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1973
   272
            [nscontext setFullScreen];
slouken@1973
   273
        } else {
slouken@1973
   274
            [nscontext setView:[windowdata->window contentView]];
slouken@1973
   275
            [nscontext update];
slouken@1973
   276
        }
slouken@1936
   277
        [nscontext makeCurrentContext];
slouken@1936
   278
    } else {
slouken@1936
   279
        [NSOpenGLContext clearCurrentContext];
slouken@1936
   280
    }
slouken@1936
   281
slouken@1936
   282
    [pool release];
slouken@1936
   283
    return 0;
slouken@1936
   284
}
slouken@1936
   285
slouken@1936
   286
int
slouken@1936
   287
Cocoa_GL_SetSwapInterval(_THIS, int interval)
slouken@1936
   288
{
slouken@1936
   289
    NSAutoreleasePool *pool;
slouken@1936
   290
    NSOpenGLContext *nscontext;
slouken@2703
   291
    GLint value;
slouken@1936
   292
    int status;
slouken@1936
   293
slouken@1936
   294
    pool = [[NSAutoreleasePool alloc] init];
slouken@1936
   295
slouken@1936
   296
    nscontext = [NSOpenGLContext currentContext];
slouken@1936
   297
    if (nscontext != nil) {
slouken@1936
   298
        value = interval;
slouken@1936
   299
        [nscontext setValues:&value forParameter:NSOpenGLCPSwapInterval];
slouken@1936
   300
        status = 0;
slouken@1936
   301
    } else {
slouken@1936
   302
        SDL_SetError("No current OpenGL context");
slouken@1936
   303
        status = -1;
slouken@1936
   304
    }
slouken@1936
   305
slouken@1936
   306
    [pool release];
slouken@1936
   307
    return status;
slouken@1936
   308
}
slouken@1936
   309
slouken@1936
   310
int
slouken@1936
   311
Cocoa_GL_GetSwapInterval(_THIS)
slouken@1936
   312
{
slouken@1936
   313
    NSAutoreleasePool *pool;
slouken@1936
   314
    NSOpenGLContext *nscontext;
slouken@2703
   315
    GLint value;
slouken@1936
   316
    int status;
slouken@1936
   317
slouken@1936
   318
    pool = [[NSAutoreleasePool alloc] init];
slouken@1936
   319
slouken@1936
   320
    nscontext = [NSOpenGLContext currentContext];
slouken@1936
   321
    if (nscontext != nil) {
slouken@1936
   322
        [nscontext getValues:&value forParameter:NSOpenGLCPSwapInterval];
slouken@1936
   323
        status = (int)value;
slouken@1936
   324
    } else {
slouken@1936
   325
        SDL_SetError("No current OpenGL context");
slouken@1936
   326
        status = -1;
slouken@1936
   327
    }
slouken@1936
   328
slouken@1936
   329
    [pool release];
slouken@1936
   330
    return status;
slouken@1936
   331
}
slouken@1936
   332
slouken@1936
   333
void
slouken@1936
   334
Cocoa_GL_SwapWindow(_THIS, SDL_Window * window)
slouken@1936
   335
{
slouken@1936
   336
    NSAutoreleasePool *pool;
slouken@1936
   337
    NSOpenGLContext *nscontext;
slouken@1936
   338
slouken@1936
   339
    pool = [[NSAutoreleasePool alloc] init];
slouken@1936
   340
slouken@1936
   341
    /* FIXME: Do we need to get the context for the window? */
slouken@1936
   342
    nscontext = [NSOpenGLContext currentContext];
slouken@1936
   343
    if (nscontext != nil) {
slouken@1936
   344
        [nscontext flushBuffer];
slouken@1936
   345
    }
slouken@1936
   346
slouken@1936
   347
    [pool release];
slouken@1936
   348
}
slouken@1936
   349
slouken@1936
   350
void
slouken@1936
   351
Cocoa_GL_DeleteContext(_THIS, SDL_GLContext context)
slouken@1936
   352
{
slouken@1936
   353
    NSAutoreleasePool *pool;
slouken@1936
   354
    NSOpenGLContext *nscontext = (NSOpenGLContext *)context;
slouken@1936
   355
slouken@1936
   356
    pool = [[NSAutoreleasePool alloc] init];
slouken@1936
   357
slouken@1936
   358
    [nscontext clearDrawable];
slouken@1936
   359
    [nscontext release];
slouken@1936
   360
slouken@1936
   361
    [pool release];
slouken@1936
   362
}
slouken@1936
   363
slouken@1952
   364
#endif /* SDL_VIDEO_OPENGL_CGL */
slouken@1936
   365
slouken@1936
   366
/* vi: set ts=4 sw=4 expandtab: */