+#ifdef USE_BACKBUFFER
+
+/* Create a bitmap context into which we render everything.
+ If the desired size has changed, re-created it.
+ */
+- (void) createBackbuffer
+{
+# ifdef USE_IPHONE
+ double s = [self hackedContentScaleFactor];
+ CGSize rotsize = ignore_rotation_p ? initial_bounds : rot_current_size;
+ int new_w = s * rotsize.width;
+ int new_h = s * rotsize.height;
+# else
+ int new_w = [self bounds].size.width;
+ int new_h = [self bounds].size.height;
+# endif
+
+ // 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();
+ }
+
+ if (backbuffer &&
+ backbuffer_size.width == new_w &&
+ backbuffer_size.height == new_h)
+ return;
+
+ CGSize osize = backbuffer_size;
+ CGContextRef ob = backbuffer;
+
+ backbuffer_size.width = new_w;
+ backbuffer_size.height = new_h;
+
+ backbuffer = CGBitmapContextCreate (NULL,
+ backbuffer_size.width,
+ backbuffer_size.height,
+ 8,
+ backbuffer_size.width * 4,
+ colorspace,
+ // kCGImageAlphaPremultipliedLast
+ (kCGImageAlphaNoneSkipFirst |
+ kCGBitmapByteOrder32Host)
+ );
+ NSAssert (backbuffer, @"unable to allocate back buffer");
+
+ // Clear it.
+ CGRect r;
+ r.origin.x = r.origin.y = 0;
+ r.size = backbuffer_size;
+ CGContextSetGrayFillColor (backbuffer, 0, 1);
+ CGContextFillRect (backbuffer, r);
+
+ if (ob) {
+ // Restore old bits, as much as possible, to the X11 upper left origin.
+ CGRect rect;
+ rect.origin.x = 0;
+ rect.origin.y = (backbuffer_size.height - osize.height);
+ rect.size = osize;
+ CGImageRef img = CGBitmapContextCreateImage (ob);
+ CGContextDrawImage (backbuffer, rect, img);
+ CGImageRelease (img);
+ CGContextRelease (ob);
+ }
+}
+
+#endif // USE_BACKBUFFER
+
+
+/* Inform X11 that the size of our window has changed.
+ */
+- (void) resize_x11
+{
+ if (!xwindow) return; // early
+
+# ifdef USE_BACKBUFFER
+ [self createBackbuffer];
+ jwxyz_window_resized (xdpy, xwindow,
+ 0, 0,
+ backbuffer_size.width, backbuffer_size.height,
+ backbuffer);
+# else // !USE_BACKBUFFER
+ NSRect r = [self frame]; // ignoring rotation is closer
+ r.size = [self bounds].size; // to what XGetGeometry expects.
+ jwxyz_window_resized (xdpy, xwindow,
+ r.origin.x, r.origin.y,
+ r.size.width, r.size.height,
+ 0);
+# endif // !USE_BACKBUFFER
+
+ // Next time render_x11 is called, run the saver's reshape_cb.
+ resized_p = YES;
+}
+
+