hqd: Adjust resolution based on aspect ratio

When the MDP downscale path is enabled we assign the primary
resolution as the external display's dimensions. However, the
aspect ratios of the external and primary displays can be different.
As a result, directly assigning primary resolution could lead
to an incorrect final image.

We get around this by calculating a new resolution by
keeping aspect ratio intact.

Change-Id: I217718a5474862154fe60837fc6518408d2c32c7
diff --git a/libexternal/external.cpp b/libexternal/external.cpp
index 1268b0f..3376d88 100644
--- a/libexternal/external.cpp
+++ b/libexternal/external.cpp
@@ -35,6 +35,7 @@
 #include "overlayUtils.h"
 #include "overlay.h"
 #include "mdp_version.h"
+#include "qd_utils.h"
 
 using namespace android;
 
@@ -594,14 +595,27 @@
             // Restrict this upto 1080p resolution max
             if(((priW * priH) > (width * height)) &&
                ((priW * priH) <= SUPPORTED_DOWNSCALE_EXT_AREA)) {
-                mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].xres = priW;
-                mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].yres = priH;
+                // tmpW and tmpH will hold the primary dimensions before we
+                // update the aspect ratio if necessary.
+                int tmpW = priW;
+                int tmpH = priH;
                 // HDMI is always in landscape, so always assign the higher
                 // dimension to hdmi's xres
                 if(priH > priW) {
-                    mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].xres = priH;
-                    mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].yres = priW;
+                    tmpW = priH;
+                    tmpH = priW;
                 }
+                // The aspect ratios of the external and primary displays
+                // can be different. As a result, directly assigning primary
+                // resolution could lead to an incorrect final image.
+                // We get around this by calculating a new resolution by
+                // keeping aspect ratio intact.
+                hwc_rect r = {0, 0, 0, 0};
+                getAspectRatioPosition(tmpW, tmpH, width, height, r);
+                mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].xres =
+                                                              r.right - r.left;
+                mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].yres =
+                                                              r.bottom - r.top;
                 // Set External Display MDP Downscale mode indicator
                 mHwcContext->dpyAttr[HWC_DISPLAY_EXTERNAL].mDownScaleMode =true;
             }