- if (get_boolean_resource (mi->dpy, "doubleBuffer", "DoubleBuffer"))
- attrs[i++] = NSOpenGLPFADoubleBuffer;
-
- Bool ms_p = get_boolean_resource (mi->dpy, "multiSample", "MultiSample");
-
- /* Sometimes, turning on multisampling kills performance. At one point,
- I thought the answer was, "only run multisampling on one screen, and
- leave it turned off on other screens". That's what this code does,
- but it turns out, that solution is insufficient. I can't really tell
- what causes poor performance with multisampling, but it's not
- predictable. Without changing the code, some times a given saver will
- perform fine with multisampling on, and other times it will perform
- very badly. Without multisampling, they always perform fine.
- */
-// if (ms_p && [[view window] screen] != [[NSScreen screens] objectAtIndex:0])
-// ms_p = 0;
-
- if (ms_p) {
- attrs[i++] = NSOpenGLPFASampleBuffers; attrs[i++] = 1;
- attrs[i++] = NSOpenGLPFASamples; attrs[i++] = 6;
- // Don't really understand what this means:
- // attrs[i++] = NSOpenGLPFANoRecovery;
- }
-
- attrs[i] = 0;
-
- NSOpenGLPixelFormat *pixfmt =
- [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
-
- if (ms_p && !pixfmt) { // Retry without multisampling.
- i -= 2;
- attrs[i] = 0;
- pixfmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
- }
-
- NSAssert (pixfmt, @"unable to create NSOpenGLPixelFormat");
-
- // #### Analyze says: "Potential leak of an object stored into pixfmt"
- ctx = [[NSOpenGLContext alloc]
- initWithFormat:pixfmt
- shareContext:nil];
-// [pixfmt release]; // #### ???
- }
-
- // Sync refreshes to the vertical blanking interval
- GLint r = 1;
- [ctx setValues:&r forParameter:NSOpenGLCPSwapInterval];
-// check_gl_error ("NSOpenGLCPSwapInterval"); // SEGV sometimes. Too early?
-
- // #### Analyze says: "Potential leak of an object stored into "ctx"
- // But makeCurrentContext retains it (right?)
-
- [ctx makeCurrentContext];
- check_gl_error ("makeCurrentContext");
-
- [view setOglContext:ctx];
-
- // Clear frame buffer ASAP, else there are bits left over from other apps.
- glClearColor (0, 0, 0, 1);
- glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-// glFinish ();
-// glXSwapBuffers (mi->dpy, mi->window);
-
-
- // Enable multi-threading, if possible. This runs most OpenGL commands
- // and GPU management on a second CPU.
- {
-# ifndef kCGLCEMPEngine
-# define kCGLCEMPEngine 313 // Added in MacOS 10.4.8 + XCode 2.4.
-# endif
- CGLContextObj cctx = CGLGetCurrentContext();
- CGLError err = CGLEnable (cctx, kCGLCEMPEngine);
- if (err != kCGLNoError) {
- NSLog (@"enabling multi-threaded OpenGL failed: %d", err);
- }
- }
-
- check_gl_error ("init_GL");
-
-# else // USE_IPHONE
-
- EAGLContext *ogl_ctx = ctx;
- if (!ogl_ctx) {
-
- Bool dbuf_p =
- get_boolean_resource (mi->dpy, "doubleBuffer", "DoubleBuffer");
-
- /* There seems to be no way to actually turn off double-buffering in
- EAGLContext (e.g., no way to draw to the front buffer directly)
- but if we turn on "retained backing" for non-buffering apps like
- "pipes", at least the back buffer isn't auto-cleared on them.
- */
- CAEAGLLayer *eagl_layer = (CAEAGLLayer *) view.layer;
- eagl_layer.opaque = TRUE;
- eagl_layer.drawableProperties =
- [NSDictionary dictionaryWithObjectsAndKeys:
- kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat,
- [NSNumber numberWithBool:!dbuf_p], kEAGLDrawablePropertyRetainedBacking,
- nil];
-
- // Without this, the GL frame buffer is half the screen resolution!
- eagl_layer.contentsScale = [UIScreen mainScreen].scale;
-
- ogl_ctx = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
- }
-
- if (!ogl_ctx)
- return 0;
- [view setOglContext:ogl_ctx];
- // #### Analyze says "Potential leak of an object stored into ogl_ctx"
-
- check_gl_error ("OES_init");