src/video/cocoa/SDL_cocoaopengl.m
changeset 6848 478ecc8a58b3
parent 6832 156e608ec4ef
child 6885 700f1b25f77f
     1.1 --- a/src/video/cocoa/SDL_cocoaopengl.m	Mon Feb 11 17:25:58 2013 -0800
     1.2 +++ b/src/video/cocoa/SDL_cocoaopengl.m	Mon Feb 11 17:39:52 2013 -0800
     1.3 @@ -80,6 +80,7 @@
     1.4      const int wantver = (_this->gl_config.major_version << 8) |
     1.5                          (_this->gl_config.minor_version);
     1.6      SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
     1.7 +    NSAutoreleasePool *pool;
     1.8      SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
     1.9      SDL_DisplayData *displaydata = (SDL_DisplayData *)display->driverdata;
    1.10      NSOpenGLPixelFormatAttribute attr[32];
    1.11 @@ -99,115 +100,120 @@
    1.12          return NULL;
    1.13      }
    1.14  
    1.15 -    @autoreleasepool {
    1.16 -        /* specify a profile if we're on Lion (10.7) or later. */
    1.17 -        if (data->osversion >= 0x1070) {
    1.18 -            NSOpenGLPixelFormatAttribute profile = kCGLOGLPVersion_Legacy;
    1.19 -            if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_CORE) {
    1.20 -                if (wantver == 0x0302) {
    1.21 -                    profile = kCGLOGLPVersion_3_2_Core;
    1.22 -                }
    1.23 +    pool = [[NSAutoreleasePool alloc] init];
    1.24 +
    1.25 +    /* specify a profile if we're on Lion (10.7) or later. */
    1.26 +    if (data->osversion >= 0x1070) {
    1.27 +        NSOpenGLPixelFormatAttribute profile = kCGLOGLPVersion_Legacy;
    1.28 +        if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_CORE) {
    1.29 +            if (wantver == 0x0302) {
    1.30 +                profile = kCGLOGLPVersion_3_2_Core;
    1.31              }
    1.32 -            attr[i++] = kCGLPFAOpenGLProfile;
    1.33 -            attr[i++] = profile;
    1.34          }
    1.35 +        attr[i++] = kCGLPFAOpenGLProfile;
    1.36 +        attr[i++] = profile;
    1.37 +    }
    1.38  
    1.39 -    #ifndef FULLSCREEN_TOGGLEABLE
    1.40 -        if (window->flags & SDL_WINDOW_FULLSCREEN) {
    1.41 -            attr[i++] = NSOpenGLPFAFullScreen;
    1.42 +#ifndef FULLSCREEN_TOGGLEABLE
    1.43 +    if (window->flags & SDL_WINDOW_FULLSCREEN) {
    1.44 +        attr[i++] = NSOpenGLPFAFullScreen;
    1.45 +    }
    1.46 +#endif
    1.47 +
    1.48 +    attr[i++] = NSOpenGLPFAColorSize;
    1.49 +    attr[i++] = SDL_BYTESPERPIXEL(display->current_mode.format)*8;
    1.50 +
    1.51 +    attr[i++] = NSOpenGLPFADepthSize;
    1.52 +    attr[i++] = _this->gl_config.depth_size;
    1.53 +
    1.54 +    if (_this->gl_config.double_buffer) {
    1.55 +        attr[i++] = NSOpenGLPFADoubleBuffer;
    1.56 +    }
    1.57 +
    1.58 +    if (_this->gl_config.stereo) {
    1.59 +        attr[i++] = NSOpenGLPFAStereo;
    1.60 +    }
    1.61 +
    1.62 +    if (_this->gl_config.stencil_size) {
    1.63 +        attr[i++] = NSOpenGLPFAStencilSize;
    1.64 +        attr[i++] = _this->gl_config.stencil_size;
    1.65 +    }
    1.66 +
    1.67 +    if ((_this->gl_config.accum_red_size +
    1.68 +         _this->gl_config.accum_green_size +
    1.69 +         _this->gl_config.accum_blue_size +
    1.70 +         _this->gl_config.accum_alpha_size) > 0) {
    1.71 +        attr[i++] = NSOpenGLPFAAccumSize;
    1.72 +        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;
    1.73 +    }
    1.74 +
    1.75 +    if (_this->gl_config.multisamplebuffers) {
    1.76 +        attr[i++] = NSOpenGLPFASampleBuffers;
    1.77 +        attr[i++] = _this->gl_config.multisamplebuffers;
    1.78 +    }
    1.79 +
    1.80 +    if (_this->gl_config.multisamplesamples) {
    1.81 +        attr[i++] = NSOpenGLPFASamples;
    1.82 +        attr[i++] = _this->gl_config.multisamplesamples;
    1.83 +        attr[i++] = NSOpenGLPFANoRecovery;
    1.84 +    }
    1.85 +
    1.86 +    if (_this->gl_config.accelerated >= 0) {
    1.87 +        if (_this->gl_config.accelerated) {
    1.88 +            attr[i++] = NSOpenGLPFAAccelerated;
    1.89 +        } else {
    1.90 +            attr[i++] = NSOpenGLPFARendererID;
    1.91 +            attr[i++] = kCGLRendererGenericFloatID;
    1.92          }
    1.93 +    }
    1.94 +
    1.95 +    attr[i++] = NSOpenGLPFAScreenMask;
    1.96 +    attr[i++] = CGDisplayIDToOpenGLDisplayMask(displaydata->display);
    1.97 +    attr[i] = 0;
    1.98 +
    1.99 +    fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attr];
   1.100 +    if (fmt == nil) {
   1.101 +        SDL_SetError ("Failed creating OpenGL pixel format");
   1.102 +        [pool release];
   1.103 +        return NULL;
   1.104 +    }
   1.105 +
   1.106 +    context = [[NSOpenGLContext alloc] initWithFormat:fmt shareContext:nil];
   1.107 +
   1.108 +    [fmt release];
   1.109 +
   1.110 +    if (context == nil) {
   1.111 +        SDL_SetError ("Failed creating OpenGL context");
   1.112 +        [pool release];
   1.113 +        return NULL;
   1.114 +    }
   1.115 +
   1.116 +    /*
   1.117 +     * Wisdom from Apple engineer in reference to UT2003's OpenGL performance:
   1.118 +     *  "You are blowing a couple of the internal OpenGL function caches. This
   1.119 +     *  appears to be happening in the VAO case.  You can tell OpenGL to up
   1.120 +     *  the cache size by issuing the following calls right after you create
   1.121 +     *  the OpenGL context.  The default cache size is 16."    --ryan.
   1.122 +     */
   1.123 +
   1.124 +    #ifndef GLI_ARRAY_FUNC_CACHE_MAX
   1.125 +    #define GLI_ARRAY_FUNC_CACHE_MAX 284
   1.126      #endif
   1.127  
   1.128 -        attr[i++] = NSOpenGLPFAColorSize;
   1.129 -        attr[i++] = SDL_BYTESPERPIXEL(display->current_mode.format)*8;
   1.130 +    #ifndef GLI_SUBMIT_FUNC_CACHE_MAX
   1.131 +    #define GLI_SUBMIT_FUNC_CACHE_MAX 280
   1.132 +    #endif
   1.133  
   1.134 -        attr[i++] = NSOpenGLPFADepthSize;
   1.135 -        attr[i++] = _this->gl_config.depth_size;
   1.136 +    {
   1.137 +        GLint cache_max = 64;
   1.138 +        CGLContextObj ctx = [context CGLContextObj];
   1.139 +        CGLSetParameter (ctx, GLI_SUBMIT_FUNC_CACHE_MAX, &cache_max);
   1.140 +        CGLSetParameter (ctx, GLI_ARRAY_FUNC_CACHE_MAX, &cache_max);
   1.141 +    }
   1.142  
   1.143 -        if (_this->gl_config.double_buffer) {
   1.144 -            attr[i++] = NSOpenGLPFADoubleBuffer;
   1.145 -        }
   1.146 +    /* End Wisdom from Apple Engineer section. --ryan. */
   1.147  
   1.148 -        if (_this->gl_config.stereo) {
   1.149 -            attr[i++] = NSOpenGLPFAStereo;
   1.150 -        }
   1.151 -
   1.152 -        if (_this->gl_config.stencil_size) {
   1.153 -            attr[i++] = NSOpenGLPFAStencilSize;
   1.154 -            attr[i++] = _this->gl_config.stencil_size;
   1.155 -        }
   1.156 -
   1.157 -        if ((_this->gl_config.accum_red_size +
   1.158 -             _this->gl_config.accum_green_size +
   1.159 -             _this->gl_config.accum_blue_size +
   1.160 -             _this->gl_config.accum_alpha_size) > 0) {
   1.161 -            attr[i++] = NSOpenGLPFAAccumSize;
   1.162 -            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;
   1.163 -        }
   1.164 -
   1.165 -        if (_this->gl_config.multisamplebuffers) {
   1.166 -            attr[i++] = NSOpenGLPFASampleBuffers;
   1.167 -            attr[i++] = _this->gl_config.multisamplebuffers;
   1.168 -        }
   1.169 -
   1.170 -        if (_this->gl_config.multisamplesamples) {
   1.171 -            attr[i++] = NSOpenGLPFASamples;
   1.172 -            attr[i++] = _this->gl_config.multisamplesamples;
   1.173 -            attr[i++] = NSOpenGLPFANoRecovery;
   1.174 -        }
   1.175 -
   1.176 -        if (_this->gl_config.accelerated >= 0) {
   1.177 -            if (_this->gl_config.accelerated) {
   1.178 -                attr[i++] = NSOpenGLPFAAccelerated;
   1.179 -            } else {
   1.180 -                attr[i++] = NSOpenGLPFARendererID;
   1.181 -                attr[i++] = kCGLRendererGenericFloatID;
   1.182 -            }
   1.183 -        }
   1.184 -
   1.185 -        attr[i++] = NSOpenGLPFAScreenMask;
   1.186 -        attr[i++] = CGDisplayIDToOpenGLDisplayMask(displaydata->display);
   1.187 -        attr[i] = 0;
   1.188 -
   1.189 -        fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attr];
   1.190 -        if (fmt == nil) {
   1.191 -            SDL_SetError ("Failed creating OpenGL pixel format");
   1.192 -            return NULL;
   1.193 -        }
   1.194 -
   1.195 -        context = [[NSOpenGLContext alloc] initWithFormat:fmt shareContext:nil];
   1.196 -
   1.197 -        [fmt release];
   1.198 -
   1.199 -        if (context == nil) {
   1.200 -            SDL_SetError ("Failed creating OpenGL context");
   1.201 -            return NULL;
   1.202 -        }
   1.203 -
   1.204 -        /*
   1.205 -         * Wisdom from Apple engineer in reference to UT2003's OpenGL performance:
   1.206 -         *  "You are blowing a couple of the internal OpenGL function caches. This
   1.207 -         *  appears to be happening in the VAO case.  You can tell OpenGL to up
   1.208 -         *  the cache size by issuing the following calls right after you create
   1.209 -         *  the OpenGL context.  The default cache size is 16."    --ryan.
   1.210 -         */
   1.211 -
   1.212 -        #ifndef GLI_ARRAY_FUNC_CACHE_MAX
   1.213 -        #define GLI_ARRAY_FUNC_CACHE_MAX 284
   1.214 -        #endif
   1.215 -
   1.216 -        #ifndef GLI_SUBMIT_FUNC_CACHE_MAX
   1.217 -        #define GLI_SUBMIT_FUNC_CACHE_MAX 280
   1.218 -        #endif
   1.219 -
   1.220 -        {
   1.221 -            GLint cache_max = 64;
   1.222 -            CGLContextObj ctx = [context CGLContextObj];
   1.223 -            CGLSetParameter (ctx, GLI_SUBMIT_FUNC_CACHE_MAX, &cache_max);
   1.224 -            CGLSetParameter (ctx, GLI_ARRAY_FUNC_CACHE_MAX, &cache_max);
   1.225 -        }
   1.226 -    }
   1.227 -    /* End Wisdom from Apple Engineer section. --ryan. */
   1.228 +    [pool release];
   1.229  
   1.230      if ( Cocoa_GL_MakeCurrent(_this, window, context) < 0 ) {
   1.231          Cocoa_GL_DeleteContext(_this, context);
   1.232 @@ -220,94 +226,107 @@
   1.233  int
   1.234  Cocoa_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
   1.235  {
   1.236 -    @autoreleasepool {
   1.237 -        if (context) {
   1.238 -            SDL_WindowData *windowdata = (SDL_WindowData *)window->driverdata;
   1.239 -            NSOpenGLContext *nscontext = (NSOpenGLContext *)context;
   1.240 +    NSAutoreleasePool *pool;
   1.241  
   1.242 -            if (window->flags & SDL_WINDOW_SHOWN) {
   1.243 -    #ifndef FULLSCREEN_TOGGLEABLE
   1.244 -                if (window->flags & SDL_WINDOW_FULLSCREEN) {
   1.245 -                    [nscontext setFullScreen];
   1.246 -                } else
   1.247 -    #endif
   1.248 -                {
   1.249 -                    [nscontext setView:[windowdata->nswindow contentView]];
   1.250 -                    [nscontext update];
   1.251 -                }
   1.252 +    pool = [[NSAutoreleasePool alloc] init];
   1.253 +
   1.254 +    if (context) {
   1.255 +        SDL_WindowData *windowdata = (SDL_WindowData *)window->driverdata;
   1.256 +        NSOpenGLContext *nscontext = (NSOpenGLContext *)context;
   1.257 +
   1.258 +        if (window->flags & SDL_WINDOW_SHOWN) {
   1.259 +#ifndef FULLSCREEN_TOGGLEABLE
   1.260 +            if (window->flags & SDL_WINDOW_FULLSCREEN) {
   1.261 +                [nscontext setFullScreen];
   1.262 +            } else
   1.263 +#endif
   1.264 +            {
   1.265 +                [nscontext setView:[windowdata->nswindow contentView]];
   1.266 +                [nscontext update];
   1.267              }
   1.268 -            [nscontext makeCurrentContext];
   1.269 -        } else {
   1.270 -            [NSOpenGLContext clearCurrentContext];
   1.271          }
   1.272 +        [nscontext makeCurrentContext];
   1.273 +    } else {
   1.274 +        [NSOpenGLContext clearCurrentContext];
   1.275      }
   1.276  
   1.277 +    [pool release];
   1.278      return 0;
   1.279  }
   1.280  
   1.281  int
   1.282  Cocoa_GL_SetSwapInterval(_THIS, int interval)
   1.283  {
   1.284 +    NSAutoreleasePool *pool;
   1.285      NSOpenGLContext *nscontext;
   1.286      GLint value;
   1.287      int status;
   1.288  
   1.289 -    @autoreleasepool {
   1.290 -        nscontext = [NSOpenGLContext currentContext];
   1.291 -        if (nscontext != nil) {
   1.292 -            value = interval;
   1.293 -            [nscontext setValues:&value forParameter:NSOpenGLCPSwapInterval];
   1.294 -            status = 0;
   1.295 -        } else {
   1.296 -            SDL_SetError("No current OpenGL context");
   1.297 -            status = -1;
   1.298 -        }
   1.299 +    pool = [[NSAutoreleasePool alloc] init];
   1.300 +
   1.301 +    nscontext = [NSOpenGLContext currentContext];
   1.302 +    if (nscontext != nil) {
   1.303 +        value = interval;
   1.304 +        [nscontext setValues:&value forParameter:NSOpenGLCPSwapInterval];
   1.305 +        status = 0;
   1.306 +    } else {
   1.307 +        SDL_SetError("No current OpenGL context");
   1.308 +        status = -1;
   1.309      }
   1.310  
   1.311 +    [pool release];
   1.312      return status;
   1.313  }
   1.314  
   1.315  int
   1.316  Cocoa_GL_GetSwapInterval(_THIS)
   1.317  {
   1.318 +    NSAutoreleasePool *pool;
   1.319      NSOpenGLContext *nscontext;
   1.320      GLint value;
   1.321      int status = 0;
   1.322  
   1.323 -    @autoreleasepool {
   1.324 -        nscontext = [NSOpenGLContext currentContext];
   1.325 -        if (nscontext != nil) {
   1.326 -            [nscontext getValues:&value forParameter:NSOpenGLCPSwapInterval];
   1.327 -            status = (int)value;
   1.328 -        }
   1.329 +    pool = [[NSAutoreleasePool alloc] init];
   1.330 +
   1.331 +    nscontext = [NSOpenGLContext currentContext];
   1.332 +    if (nscontext != nil) {
   1.333 +        [nscontext getValues:&value forParameter:NSOpenGLCPSwapInterval];
   1.334 +        status = (int)value;
   1.335      }
   1.336  
   1.337 +    [pool release];
   1.338      return status;
   1.339  }
   1.340  
   1.341  void
   1.342  Cocoa_GL_SwapWindow(_THIS, SDL_Window * window)
   1.343  {
   1.344 +    NSAutoreleasePool *pool;
   1.345      NSOpenGLContext *nscontext;
   1.346  
   1.347 -    @autoreleasepool {
   1.348 -        /* FIXME: Do we need to get the context for the window? */
   1.349 -        nscontext = [NSOpenGLContext currentContext];
   1.350 -        if (nscontext != nil) {
   1.351 -            [nscontext flushBuffer];
   1.352 -        }
   1.353 +    pool = [[NSAutoreleasePool alloc] init];
   1.354 +
   1.355 +    /* FIXME: Do we need to get the context for the window? */
   1.356 +    nscontext = [NSOpenGLContext currentContext];
   1.357 +    if (nscontext != nil) {
   1.358 +        [nscontext flushBuffer];
   1.359      }
   1.360 +
   1.361 +    [pool release];
   1.362  }
   1.363  
   1.364  void
   1.365  Cocoa_GL_DeleteContext(_THIS, SDL_GLContext context)
   1.366  {
   1.367 +    NSAutoreleasePool *pool;
   1.368      NSOpenGLContext *nscontext = (NSOpenGLContext *)context;
   1.369  
   1.370 -    @autoreleasepool {
   1.371 -        [nscontext clearDrawable];
   1.372 -        [nscontext release];
   1.373 -    }
   1.374 +    pool = [[NSAutoreleasePool alloc] init];
   1.375 +
   1.376 +    [nscontext clearDrawable];
   1.377 +    [nscontext release];
   1.378 +
   1.379 +    [pool release];
   1.380  }
   1.381  
   1.382  #endif /* SDL_VIDEO_OPENGL_CGL */