src/video/cocoa/SDL_cocoaopengl.m
changeset 7594 6abcf951af68
parent 7412 50211a1fd557
child 7595 ede2237fcebf
equal deleted inserted replaced
7593:20298a0d8631 7594:6abcf951af68
    22 
    22 
    23 /* NSOpenGL implementation of SDL OpenGL support */
    23 /* NSOpenGL implementation of SDL OpenGL support */
    24 
    24 
    25 #if SDL_VIDEO_OPENGL_CGL
    25 #if SDL_VIDEO_OPENGL_CGL
    26 #include "SDL_cocoavideo.h"
    26 #include "SDL_cocoavideo.h"
       
    27 #include "SDL_cocoaopengl.h"
    27 
    28 
    28 #include <OpenGL/CGLTypes.h>
    29 #include <OpenGL/CGLTypes.h>
    29 #include <OpenGL/OpenGL.h>
    30 #include <OpenGL/OpenGL.h>
    30 #include <OpenGL/CGLRenderers.h>
    31 #include <OpenGL/CGLRenderers.h>
    31 
    32 
    43 #endif
    44 #endif
    44 #ifndef kCGLOGLPVersion_3_2_Core
    45 #ifndef kCGLOGLPVersion_3_2_Core
    45 #define kCGLOGLPVersion_3_2_Core 0x3200
    46 #define kCGLOGLPVersion_3_2_Core 0x3200
    46 #endif
    47 #endif
    47 
    48 
       
    49 @implementation SDLOpenGLContext : NSOpenGLContext
       
    50 
       
    51 - (id)initWithFormat:(NSOpenGLPixelFormat *)format
       
    52         shareContext:(NSOpenGLContext *)share
       
    53 {
       
    54     SDL_AtomicSet(&self->dirty, 0);
       
    55     return [super initWithFormat:format shareContext:share];
       
    56 }
       
    57 
       
    58 - (void)scheduleUpdate
       
    59 {
       
    60     SDL_AtomicAdd(&self->dirty, 1);
       
    61 }
       
    62 
       
    63 /* This should only be called on the thread on which a user is using the context. */
       
    64 - (void)updateIfNeeded
       
    65 {
       
    66     int value = SDL_AtomicSet(&self->dirty, 0);
       
    67     if (value > 0) {
       
    68         /* We call the real underlying update here, since -[SDLOpenGLContext update] just calls us. */
       
    69         [super update];
       
    70     }
       
    71 }
       
    72 
       
    73 /* This should only be called on the thread on which a user is using the context. */
       
    74 - (void)update
       
    75 {
       
    76     /* This ensures that regular 'update' calls clear the atomic dirty flag. */
       
    77     [self scheduleUpdate];
       
    78     [self updateIfNeeded];
       
    79 }
       
    80 
       
    81 @end
       
    82 
    48 
    83 
    49 int
    84 int
    50 Cocoa_GL_LoadLibrary(_THIS, const char *path)
    85 Cocoa_GL_LoadLibrary(_THIS, const char *path)
    51 {
    86 {
    52     /* Load the OpenGL library */
    87     /* Load the OpenGL library */
    87     NSAutoreleasePool *pool;
   122     NSAutoreleasePool *pool;
    88     SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
   123     SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
    89     SDL_DisplayData *displaydata = (SDL_DisplayData *)display->driverdata;
   124     SDL_DisplayData *displaydata = (SDL_DisplayData *)display->driverdata;
    90     NSOpenGLPixelFormatAttribute attr[32];
   125     NSOpenGLPixelFormatAttribute attr[32];
    91     NSOpenGLPixelFormat *fmt;
   126     NSOpenGLPixelFormat *fmt;
    92     NSOpenGLContext *context;
   127     SDLOpenGLContext *context;
    93     NSOpenGLContext *share_context = nil;
   128     NSOpenGLContext *share_context = nil;
    94     int i = 0;
   129     int i = 0;
    95 
   130 
    96     if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
   131     if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
    97         SDL_SetError ("OpenGL ES not supported on this platform");
   132         SDL_SetError ("OpenGL ES not supported on this platform");
   179 
   214 
   180     if (_this->gl_config.share_with_current_context) {
   215     if (_this->gl_config.share_with_current_context) {
   181         share_context = (NSOpenGLContext*)SDL_GL_GetCurrentContext();
   216         share_context = (NSOpenGLContext*)SDL_GL_GetCurrentContext();
   182     }
   217     }
   183 
   218 
   184     context = [[NSOpenGLContext alloc] initWithFormat:fmt shareContext:share_context];
   219     context = [[SDLOpenGLContext alloc] initWithFormat:fmt shareContext:share_context];
   185 
   220 
   186     [fmt release];
   221     [fmt release];
   187 
   222 
   188     if (context == nil) {
   223     if (context == nil) {
   189         SDL_SetError ("Failed creating OpenGL context");
   224         SDL_SetError ("Failed creating OpenGL context");
   208 
   243 
   209     pool = [[NSAutoreleasePool alloc] init];
   244     pool = [[NSAutoreleasePool alloc] init];
   210 
   245 
   211     if (context) {
   246     if (context) {
   212         SDL_WindowData *windowdata = (SDL_WindowData *)window->driverdata;
   247         SDL_WindowData *windowdata = (SDL_WindowData *)window->driverdata;
   213         NSOpenGLContext *nscontext = (NSOpenGLContext *)context;
   248         SDLOpenGLContext *nscontext = (SDLOpenGLContext *)context;
   214 
       
   215         windowdata->nscontext = nscontext;
   249         windowdata->nscontext = nscontext;
   216         if ([nscontext view] != [windowdata->nswindow contentView]) {
   250         if ([nscontext view] != [windowdata->nswindow contentView]) {
   217             [nscontext setView:[windowdata->nswindow contentView]];
   251             [nscontext setView:[windowdata->nswindow contentView]];
   218             [nscontext update];
   252             [nscontext scheduleUpdate];
   219         }
   253         }
       
   254 
       
   255         [nscontext updateIfNeeded];
   220         [nscontext makeCurrentContext];
   256         [nscontext makeCurrentContext];
   221     } else {
   257     } else {
   222         [NSOpenGLContext clearCurrentContext];
   258         [NSOpenGLContext clearCurrentContext];
   223     }
   259     }
   224 
   260 
   234     GLint value;
   270     GLint value;
   235     int status;
   271     int status;
   236 
   272 
   237     pool = [[NSAutoreleasePool alloc] init];
   273     pool = [[NSAutoreleasePool alloc] init];
   238 
   274 
   239     nscontext = [NSOpenGLContext currentContext];
   275     nscontext = (NSOpenGLContext*)SDL_GL_GetCurrentContext();
   240     if (nscontext != nil) {
   276     if (nscontext != nil) {
   241         value = interval;
   277         value = interval;
   242         [nscontext setValues:&value forParameter:NSOpenGLCPSwapInterval];
   278         [nscontext setValues:&value forParameter:NSOpenGLCPSwapInterval];
   243         status = 0;
   279         status = 0;
   244     } else {
   280     } else {
   257     GLint value;
   293     GLint value;
   258     int status = 0;
   294     int status = 0;
   259 
   295 
   260     pool = [[NSAutoreleasePool alloc] init];
   296     pool = [[NSAutoreleasePool alloc] init];
   261 
   297 
   262     nscontext = [NSOpenGLContext currentContext];
   298     nscontext = (NSOpenGLContext*)SDL_GL_GetCurrentContext();
   263     if (nscontext != nil) {
   299     if (nscontext != nil) {
   264         [nscontext getValues:&value forParameter:NSOpenGLCPSwapInterval];
   300         [nscontext getValues:&value forParameter:NSOpenGLCPSwapInterval];
   265         status = (int)value;
   301         status = (int)value;
   266     }
   302     }
   267 
   303 
   272 void
   308 void
   273 Cocoa_GL_SwapWindow(_THIS, SDL_Window * window)
   309 Cocoa_GL_SwapWindow(_THIS, SDL_Window * window)
   274 {
   310 {
   275     NSAutoreleasePool *pool;
   311     NSAutoreleasePool *pool;
   276     SDL_WindowData *windowdata = (SDL_WindowData *)window->driverdata;
   312     SDL_WindowData *windowdata = (SDL_WindowData *)window->driverdata;
   277     NSOpenGLContext *nscontext = windowdata->nscontext;
   313     SDLOpenGLContext *nscontext = windowdata->nscontext;
   278 
   314 
   279     pool = [[NSAutoreleasePool alloc] init];
   315     pool = [[NSAutoreleasePool alloc] init];
   280 
   316 
   281     [nscontext flushBuffer];
   317     [nscontext flushBuffer];
       
   318     [nscontext updateIfNeeded];
   282 
   319 
   283     [pool release];
   320     [pool release];
   284 }
   321 }
   285 
   322 
   286 void
   323 void