Modified version of Chris Bagwell patch for Xorg Bug #39949.
It fixes issue with rotation - apparently RRCrtcGetScanoutSize has
already done the swap between width/height, so no further swapping is needed.
Only when computing panning region they need to be swapped.

Applied cleanly to xorg-server 1.12.4, tested to work on 1.14.3.

diff -ur xorg-server-1.12.4/randr/rrcrtc.c xorg-test/randr/rrcrtc.c
--- xorg-server-1.12.4/randr/rrcrtc.c	2012-08-02 07:59:23.000000000 +0700
+++ xorg-test/randr/rrcrtc.c	2013-09-30 13:48:34.719602771 +0700
@@ -282,22 +282,43 @@
 static void
 crtc_bounds(RRCrtcPtr crtc, int *left, int *right, int *top, int *bottom)
 {
+    int width, height;
+    BoxRec panned_area;
+    ScreenPtr pScreen = crtc->pScreen;
+    rrScrPrivPtr pScrPriv = rrGetScrPriv(pScreen);
+
     *left = crtc->x;
     *top = crtc->y;
 
-    switch (crtc->rotation) {
-    case RR_Rotate_0:
-    case RR_Rotate_180:
-    default:
-        *right = crtc->x + crtc->mode->mode.width;
-        *bottom = crtc->y + crtc->mode->mode.height;
-        return;
-    case RR_Rotate_90:
-    case RR_Rotate_270:
-        *right = crtc->x + crtc->mode->mode.height;
-        *bottom = crtc->y + crtc->mode->mode.width;
-        return;
+    if (pScrPriv->rrGetPanning &&
+        pScrPriv->rrGetPanning(pScreen, crtc, &panned_area, NULL, NULL) &&
+        (panned_area.x2 > panned_area.x1) && (panned_area.y2 > panned_area.y1))
+    {
+		//swap width & height depending on rotation
+		switch (crtc->rotation) {
+		case RR_Rotate_0:
+		case RR_Rotate_180:
+		default:
+			width = panned_area.x2 - panned_area.x1;
+			height = panned_area.y2 - panned_area.y1;
+			break;
+
+		case RR_Rotate_90:
+		case RR_Rotate_270:
+			height = panned_area.x2 - panned_area.x1;
+			width = panned_area.y2 - panned_area.y1;
+			break;
+		}
     }
+    else {
+		// there is no need to swap on rotation - transform would have already
+		// done that for us.
+		RRCrtcGetScanoutSize(crtc, &width, &height);
+    }
+
+	// apply the bounding
+	*right = crtc->x + width;
+    *bottom = crtc->y + height;
 }
 
 /* overlapping counts as adjacent */
