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