src/video/cocoa/SDL_cocoaopengl.m
changeset 12840 1b6f67e84802
parent 12706 d7782848eb50
child 12863 bddd4ba329cc
     1.1 --- a/src/video/cocoa/SDL_cocoaopengl.m	Tue Jun 11 19:58:10 2019 -0700
     1.2 +++ b/src/video/cocoa/SDL_cocoaopengl.m	Tue Jun 11 16:19:01 2019 -0400
     1.3 @@ -36,23 +36,6 @@
     1.4  
     1.5  #define DEFAULT_OPENGL  "/System/Library/Frameworks/OpenGL.framework/Libraries/libGL.dylib"
     1.6  
     1.7 -static CVReturn
     1.8 -DisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp* now, const CVTimeStamp* outputTime, CVOptionFlags flagsIn, CVOptionFlags* flagsOut, void* displayLinkContext)
     1.9 -{
    1.10 -    SDLOpenGLContext *nscontext = (SDLOpenGLContext *) displayLinkContext;
    1.11 -
    1.12 -    /*printf("DISPLAY LINK! %u\n", (unsigned int) SDL_GetTicks()); */
    1.13 -    const int setting = SDL_AtomicGet(&nscontext->swapIntervalSetting);
    1.14 -    if (setting != 0) { /* nothing to do if vsync is disabled, don't even lock */
    1.15 -        SDL_LockMutex(nscontext->swapIntervalMutex);
    1.16 -        SDL_AtomicAdd(&nscontext->swapIntervalsPassed, 1);
    1.17 -        SDL_CondSignal(nscontext->swapIntervalCond);
    1.18 -        SDL_UnlockMutex(nscontext->swapIntervalMutex);
    1.19 -    }
    1.20 -
    1.21 -    return kCVReturnSuccess;
    1.22 -}
    1.23 -
    1.24  @implementation SDLOpenGLContext : NSOpenGLContext
    1.25  
    1.26  - (id)initWithFormat:(NSOpenGLPixelFormat *)format
    1.27 @@ -62,20 +45,6 @@
    1.28      if (self) {
    1.29          SDL_AtomicSet(&self->dirty, 0);
    1.30          self->window = NULL;
    1.31 -        SDL_AtomicSet(&self->swapIntervalSetting, 0);
    1.32 -        SDL_AtomicSet(&self->swapIntervalsPassed, 0);
    1.33 -        self->swapIntervalCond = SDL_CreateCond();
    1.34 -        self->swapIntervalMutex = SDL_CreateMutex();
    1.35 -        if (!self->swapIntervalCond || !self->swapIntervalMutex) {
    1.36 -            [self release];
    1.37 -            return nil;
    1.38 -        }
    1.39 -
    1.40 -        /* !!! FIXME: check return values. */
    1.41 -        CVDisplayLinkCreateWithActiveCGDisplays(&self->displayLink);
    1.42 -        CVDisplayLinkSetOutputCallback(self->displayLink, &DisplayLinkCallback, self);
    1.43 -        CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext(self->displayLink, [self CGLContextObj], [format CGLPixelFormatObj]);
    1.44 -        CVDisplayLinkStart(displayLink);
    1.45      }
    1.46      return self;
    1.47  }
    1.48 @@ -145,19 +114,6 @@
    1.49      }
    1.50  }
    1.51  
    1.52 -- (void)dealloc
    1.53 -{
    1.54 -    if (self->displayLink) {
    1.55 -        CVDisplayLinkRelease(self->displayLink);
    1.56 -    }
    1.57 -    if (self->swapIntervalCond) {
    1.58 -        SDL_DestroyCond(self->swapIntervalCond);
    1.59 -    }
    1.60 -    if (self->swapIntervalMutex) {
    1.61 -        SDL_DestroyMutex(self->swapIntervalMutex);
    1.62 -    }
    1.63 -    [super dealloc];
    1.64 -}
    1.65  @end
    1.66  
    1.67  
    1.68 @@ -208,7 +164,6 @@
    1.69      const char *glversion;
    1.70      int glversion_major;
    1.71      int glversion_minor;
    1.72 -    int interval;
    1.73  
    1.74      if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
    1.75  #if SDL_VIDEO_OPENGL_EGL
    1.76 @@ -320,10 +275,6 @@
    1.77          return NULL;
    1.78      }
    1.79  
    1.80 -    /* vsync is handled separately by synchronizing with a display link. */
    1.81 -    interval = 0;
    1.82 -    [context setValues:&interval forParameter:NSOpenGLCPSwapInterval];
    1.83 -
    1.84      if ( Cocoa_GL_MakeCurrent(_this, window, context) < 0 ) {
    1.85          Cocoa_GL_DeleteContext(_this, context);
    1.86          SDL_SetError("Failed making OpenGL context current");
    1.87 @@ -417,17 +368,21 @@
    1.88  Cocoa_GL_SetSwapInterval(_THIS, int interval)
    1.89  { @autoreleasepool
    1.90  {
    1.91 -    SDLOpenGLContext *nscontext = (SDLOpenGLContext *) SDL_GL_GetCurrentContext();
    1.92 +    NSOpenGLContext *nscontext;
    1.93 +    GLint value;
    1.94      int status;
    1.95  
    1.96 -    if (nscontext == nil) {
    1.97 +    if (interval < 0) {  /* no extension for this on Mac OS X at the moment. */
    1.98 +        return SDL_SetError("Late swap tearing currently unsupported");
    1.99 +    }
   1.100 +
   1.101 +    nscontext = (NSOpenGLContext*)SDL_GL_GetCurrentContext();
   1.102 +    if (nscontext != nil) {
   1.103 +        value = interval;
   1.104 +        [nscontext setValues:&value forParameter:NSOpenGLCPSwapInterval];
   1.105 +        status = 0;
   1.106 +    } else {
   1.107          status = SDL_SetError("No current OpenGL context");
   1.108 -    } else {
   1.109 -        SDL_LockMutex(nscontext->swapIntervalMutex);
   1.110 -        SDL_AtomicSet(&nscontext->swapIntervalsPassed, 0);
   1.111 -        SDL_AtomicSet(&nscontext->swapIntervalSetting, interval);
   1.112 -        SDL_UnlockMutex(nscontext->swapIntervalMutex);
   1.113 -        status = 0;
   1.114      }
   1.115  
   1.116      return status;
   1.117 @@ -437,8 +392,17 @@
   1.118  Cocoa_GL_GetSwapInterval(_THIS)
   1.119  { @autoreleasepool
   1.120  {
   1.121 -    SDLOpenGLContext *nscontext = (SDLOpenGLContext *) SDL_GL_GetCurrentContext();
   1.122 -    return nscontext ? SDL_AtomicGet(&nscontext->swapIntervalSetting) : 0;
   1.123 +    NSOpenGLContext *nscontext;
   1.124 +    GLint value;
   1.125 +    int status = 0;
   1.126 +
   1.127 +    nscontext = (NSOpenGLContext*)SDL_GL_GetCurrentContext();
   1.128 +    if (nscontext != nil) {
   1.129 +        [nscontext getValues:&value forParameter:NSOpenGLCPSwapInterval];
   1.130 +        status = (int)value;
   1.131 +    }
   1.132 +
   1.133 +    return status;
   1.134  }}
   1.135  
   1.136  int
   1.137 @@ -447,25 +411,6 @@
   1.138  {
   1.139      SDLOpenGLContext* nscontext = (SDLOpenGLContext*)SDL_GL_GetCurrentContext();
   1.140      SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
   1.141 -    const int setting = SDL_AtomicGet(&nscontext->swapIntervalSetting);
   1.142 -
   1.143 -    if (setting == 0) {
   1.144 -        /* nothing to do if vsync is disabled, don't even lock */
   1.145 -    } else if (setting < 0) {  /* late swap tearing */
   1.146 -        SDL_LockMutex(nscontext->swapIntervalMutex);
   1.147 -        while (SDL_AtomicGet(&nscontext->swapIntervalsPassed) == 0) {
   1.148 -            SDL_CondWait(nscontext->swapIntervalCond, nscontext->swapIntervalMutex);
   1.149 -        }
   1.150 -        SDL_AtomicSet(&nscontext->swapIntervalsPassed, 0);
   1.151 -        SDL_UnlockMutex(nscontext->swapIntervalMutex);
   1.152 -    } else {
   1.153 -        SDL_LockMutex(nscontext->swapIntervalMutex);
   1.154 -        do {  /* always wait here so we know we just hit a swap interval. */
   1.155 -            SDL_CondWait(nscontext->swapIntervalCond, nscontext->swapIntervalMutex);
   1.156 -        } while ((SDL_AtomicGet(&nscontext->swapIntervalsPassed) % setting) != 0);
   1.157 -        SDL_AtomicSet(&nscontext->swapIntervalsPassed, 0);
   1.158 -        SDL_UnlockMutex(nscontext->swapIntervalMutex);
   1.159 -    }
   1.160  
   1.161      /* on 10.14 ("Mojave") and later, this deadlocks if two contexts in two
   1.162         threads try to swap at the same time, so put a mutex around it. */