hwc: Use viewframe to compute display position and crop.

1. Use viewframe of external display to compute Aspect ratio position
   for external.
2. Use viewframe to find the crop rectangle of the frame buffer to
   configure rotator for external orientation usecase.

Change-Id: I564c70f202176c8b1f707cbafc08cbbb2abde383
diff --git a/libhwcomposer/hwc_fbupdate.cpp b/libhwcomposer/hwc_fbupdate.cpp
index bb9adbf..0ca5ad9 100644
--- a/libhwcomposer/hwc_fbupdate.cpp
+++ b/libhwcomposer/hwc_fbupdate.cpp
@@ -78,13 +78,17 @@
     if(mDpy && (extOrient & HWC_TRANSFORM_ROT_90)) {
         mRot = ctx->mRotMgr->getNext();
         if(mRot == NULL) return false;
+        // Composed FB content will have black bars, if the viewFrame of the
+        // external is different from {0, 0, fbWidth, fbHeight}, so intersect
+        // viewFrame with sourceCrop to avoid those black bars
+        sourceCrop = getIntersection(sourceCrop, ctx->mViewFrame[mDpy]);
         //Configure rotator for pre-rotation
         if(configRotator(mRot, info, sourceCrop, mdpFlags, orient, 0) < 0) {
             ALOGE("%s: configRotator Failed!", __FUNCTION__);
             mRot = NULL;
             return false;
         }
-       ctx->mLayerRotMap[mDpy]->add(layer, mRot);
+        ctx->mLayerRotMap[mDpy]->add(layer, mRot);
         info.format = (mRot)->getDstFormat();
         updateSource(orient, info, sourceCrop);
         rotFlags |= ovutils::ROT_PREROTATED;
diff --git a/libhwcomposer/hwc_utils.cpp b/libhwcomposer/hwc_utils.cpp
index e632843..db19a93 100644
--- a/libhwcomposer/hwc_utils.cpp
+++ b/libhwcomposer/hwc_utils.cpp
@@ -390,6 +390,7 @@
 // based on the position and aspect ratio with orientation
 void getAspectRatioPosition(hwc_context_t* ctx, int dpy, int extOrientation,
                             hwc_rect_t& inRect, hwc_rect_t& outRect) {
+    hwc_rect_t viewFrame = ctx->mViewFrame[dpy];
     // Physical display resolution
     float fbWidth  = ctx->dpyAttr[dpy].xres;
     float fbHeight = ctx->dpyAttr[dpy].yres;
@@ -427,14 +428,20 @@
         yPos = rect.top;
         width = rect.right - rect.left;
         height = rect.bottom - rect.top;
+        // swap viewframe coordinates for 90 degree rotation.
+        swap(viewFrame.left, viewFrame.top);
+        swap(viewFrame.right, viewFrame.bottom);
     }
+    // if viewframe left and top coordinates are non zero value then exclude it
+    // during the computation of xRatio and yRatio
+    xRatio = (inPos.x - viewFrame.left)/actualWidth;
+    yRatio = (inPos.y - viewFrame.top)/actualHeight;
+    // Use viewframe width and height to compute wRatio and hRatio.
+    wRatio = inPos.w/(viewFrame.right - viewFrame.left);
+    hRatio = inPos.h/(viewFrame.bottom - viewFrame.top);
+
 
     //Calculate the position...
-    xRatio = inPos.x/actualWidth;
-    yRatio = inPos.y/actualHeight;
-    wRatio = inPos.w/actualWidth;
-    hRatio = inPos.h/actualHeight;
-
     outPos.x = (xRatio * width) + xPos;
     outPos.y = (yRatio * height) + yPos;
     outPos.w = wRatio * width;