+
+ // Colorspaces and CGContexts only happen with non-GL hacks.
+ if (colorspace)
+ CGColorSpaceRelease (colorspace);
+# ifdef BACKBUFFER_CGCONTEXT
+ if (window_ctx)
+ CGContextRelease (window_ctx);
+# endif
+
+ NSWindow *window = [self window];
+
+ if (window && xdpy) {
+ [self lockFocus];
+
+# if defined(BACKBUFFER_CGCONTEXT)
+ // TODO: This was borrowed from jwxyz_window_resized, and should
+ // probably be refactored.
+
+ // Figure out which screen the window is currently on.
+ CGDirectDisplayID cgdpy = 0;
+
+ {
+// int wx, wy;
+// TODO: XTranslateCoordinates is returning (0,1200) on my system.
+// Is this right?
+// In any case, those weren't valid coordinates for CGGetDisplaysWithPoint.
+// XTranslateCoordinates (xdpy, xwindow, NULL, 0, 0, &wx, &wy, NULL);
+// p.x = wx;
+// p.y = wy;
+
+ NSPoint p0 = {0, 0};
+ p0 = [window convertBaseToScreen:p0];
+ CGPoint p = {p0.x, p0.y};
+ CGDisplayCount n;
+ CGGetDisplaysWithPoint (p, 1, &cgdpy, &n);
+ NSAssert (cgdpy, @"unable to find CGDisplay");
+ }
+
+ {
+ // Figure out this screen's colorspace, and use that for every CGImage.
+ //
+ CMProfileRef profile = 0;
+
+ // CMGetProfileByAVID is deprecated as of OS X 10.6, but there's no
+ // documented replacement as of OS X 10.9.
+ // http://lists.apple.com/archives/colorsync-dev/2012/Nov/msg00001.html
+ CMGetProfileByAVID ((CMDisplayIDType) cgdpy, &profile);
+ NSAssert (profile, @"unable to find colorspace profile");
+ colorspace = CGColorSpaceCreateWithPlatformColorSpace (profile);
+ NSAssert (colorspace, @"unable to find colorspace");
+ }
+# elif defined(BACKBUFFER_CALAYER)
+ // Was apparently faster until 10.9.
+ colorspace = CGColorSpaceCreateDeviceRGB ();
+# endif // BACKBUFFER_CALAYER
+
+# ifdef BACKBUFFER_CGCONTEXT
+ window_ctx = [[window graphicsContext] graphicsPort];
+ CGContextRetain (window_ctx);
+# endif // BACKBUFFER_CGCONTEXT
+
+ [self unlockFocus];
+ } else {
+# ifdef BACKBUFFER_CGCONTEXT
+ window_ctx = NULL;
+# endif // BACKBUFFER_CGCONTEXT
+ colorspace = CGColorSpaceCreateDeviceRGB();
+ }