src/video/cocoa/SDL_cocoaopengl.m
changeset 7594 6abcf951af68
parent 7412 50211a1fd557
child 7595 ede2237fcebf
     1.1 --- a/src/video/cocoa/SDL_cocoaopengl.m	Wed Aug 07 16:29:15 2013 -0700
     1.2 +++ b/src/video/cocoa/SDL_cocoaopengl.m	Wed Aug 07 16:29:21 2013 -0700
     1.3 @@ -24,6 +24,7 @@
     1.4  
     1.5  #if SDL_VIDEO_OPENGL_CGL
     1.6  #include "SDL_cocoavideo.h"
     1.7 +#include "SDL_cocoaopengl.h"
     1.8  
     1.9  #include <OpenGL/CGLTypes.h>
    1.10  #include <OpenGL/OpenGL.h>
    1.11 @@ -45,6 +46,40 @@
    1.12  #define kCGLOGLPVersion_3_2_Core 0x3200
    1.13  #endif
    1.14  
    1.15 +@implementation SDLOpenGLContext : NSOpenGLContext
    1.16 +
    1.17 +- (id)initWithFormat:(NSOpenGLPixelFormat *)format
    1.18 +        shareContext:(NSOpenGLContext *)share
    1.19 +{
    1.20 +    SDL_AtomicSet(&self->dirty, 0);
    1.21 +    return [super initWithFormat:format shareContext:share];
    1.22 +}
    1.23 +
    1.24 +- (void)scheduleUpdate
    1.25 +{
    1.26 +    SDL_AtomicAdd(&self->dirty, 1);
    1.27 +}
    1.28 +
    1.29 +/* This should only be called on the thread on which a user is using the context. */
    1.30 +- (void)updateIfNeeded
    1.31 +{
    1.32 +    int value = SDL_AtomicSet(&self->dirty, 0);
    1.33 +    if (value > 0) {
    1.34 +        /* We call the real underlying update here, since -[SDLOpenGLContext update] just calls us. */
    1.35 +        [super update];
    1.36 +    }
    1.37 +}
    1.38 +
    1.39 +/* This should only be called on the thread on which a user is using the context. */
    1.40 +- (void)update
    1.41 +{
    1.42 +    /* This ensures that regular 'update' calls clear the atomic dirty flag. */
    1.43 +    [self scheduleUpdate];
    1.44 +    [self updateIfNeeded];
    1.45 +}
    1.46 +
    1.47 +@end
    1.48 +
    1.49  
    1.50  int
    1.51  Cocoa_GL_LoadLibrary(_THIS, const char *path)
    1.52 @@ -89,7 +124,7 @@
    1.53      SDL_DisplayData *displaydata = (SDL_DisplayData *)display->driverdata;
    1.54      NSOpenGLPixelFormatAttribute attr[32];
    1.55      NSOpenGLPixelFormat *fmt;
    1.56 -    NSOpenGLContext *context;
    1.57 +    SDLOpenGLContext *context;
    1.58      NSOpenGLContext *share_context = nil;
    1.59      int i = 0;
    1.60  
    1.61 @@ -181,7 +216,7 @@
    1.62          share_context = (NSOpenGLContext*)SDL_GL_GetCurrentContext();
    1.63      }
    1.64  
    1.65 -    context = [[NSOpenGLContext alloc] initWithFormat:fmt shareContext:share_context];
    1.66 +    context = [[SDLOpenGLContext alloc] initWithFormat:fmt shareContext:share_context];
    1.67  
    1.68      [fmt release];
    1.69  
    1.70 @@ -210,13 +245,14 @@
    1.71  
    1.72      if (context) {
    1.73          SDL_WindowData *windowdata = (SDL_WindowData *)window->driverdata;
    1.74 -        NSOpenGLContext *nscontext = (NSOpenGLContext *)context;
    1.75 -
    1.76 +        SDLOpenGLContext *nscontext = (SDLOpenGLContext *)context;
    1.77          windowdata->nscontext = nscontext;
    1.78          if ([nscontext view] != [windowdata->nswindow contentView]) {
    1.79              [nscontext setView:[windowdata->nswindow contentView]];
    1.80 -            [nscontext update];
    1.81 +            [nscontext scheduleUpdate];
    1.82          }
    1.83 +
    1.84 +        [nscontext updateIfNeeded];
    1.85          [nscontext makeCurrentContext];
    1.86      } else {
    1.87          [NSOpenGLContext clearCurrentContext];
    1.88 @@ -236,7 +272,7 @@
    1.89  
    1.90      pool = [[NSAutoreleasePool alloc] init];
    1.91  
    1.92 -    nscontext = [NSOpenGLContext currentContext];
    1.93 +    nscontext = (NSOpenGLContext*)SDL_GL_GetCurrentContext();
    1.94      if (nscontext != nil) {
    1.95          value = interval;
    1.96          [nscontext setValues:&value forParameter:NSOpenGLCPSwapInterval];
    1.97 @@ -259,7 +295,7 @@
    1.98  
    1.99      pool = [[NSAutoreleasePool alloc] init];
   1.100  
   1.101 -    nscontext = [NSOpenGLContext currentContext];
   1.102 +    nscontext = (NSOpenGLContext*)SDL_GL_GetCurrentContext();
   1.103      if (nscontext != nil) {
   1.104          [nscontext getValues:&value forParameter:NSOpenGLCPSwapInterval];
   1.105          status = (int)value;
   1.106 @@ -274,11 +310,12 @@
   1.107  {
   1.108      NSAutoreleasePool *pool;
   1.109      SDL_WindowData *windowdata = (SDL_WindowData *)window->driverdata;
   1.110 -    NSOpenGLContext *nscontext = windowdata->nscontext;
   1.111 +    SDLOpenGLContext *nscontext = windowdata->nscontext;
   1.112  
   1.113      pool = [[NSAutoreleasePool alloc] init];
   1.114  
   1.115      [nscontext flushBuffer];
   1.116 +    [nscontext updateIfNeeded];
   1.117  
   1.118      [pool release];
   1.119  }