hwc: Move max app layer check outside isFrameDoable routine.

Move the condition for checking maximum number of app layers
ourside the isFrameDoable function to avoid heap corruption while
updating hnd array in cacheAll function.

Bug: 24163261

Acked-by: Ramkumar Radhakrishnan <ramkumar@codeaurora.org>

Change-Id: Id4458fcadc7775a2d78b1849de6782857c6ac17f
diff --git a/msm8960/libhwcomposer/hwc_mdpcomp.cpp b/msm8960/libhwcomposer/hwc_mdpcomp.cpp
index 24b941f..744fc09 100644
--- a/msm8960/libhwcomposer/hwc_mdpcomp.cpp
+++ b/msm8960/libhwcomposer/hwc_mdpcomp.cpp
@@ -358,10 +358,6 @@
     } else if(ctx->mVideoTransFlag) {
         ALOGD_IF(isDebug(), "%s: MDP Comp. video transition padding round",
                 __FUNCTION__);
-    } else if(numAppLayers > MAX_NUM_APP_LAYERS) {
-        ALOGD_IF(isDebug(), "%s: Number of App layers exceeded the limit ",
-                 __FUNCTION__);
-        ret = false;
     }
     return ret;
 }
@@ -733,10 +729,19 @@
 
 int MDPComp::prepare(hwc_context_t *ctx, hwc_display_contents_1_t* list) {
 
-    //reset old data
     const int numLayers = ctx->listStats[mDpy].numAppLayers;
+
+    //reset old data
     mCurrentFrame.reset(numLayers);
 
+    //number of app layers exceeds MAX_NUM_APP_LAYERS fall back to GPU
+    //do not cache the information for next draw cycle.
+    if(numLayers > MAX_NUM_APP_LAYERS) {
+        ALOGD_IF(isDebug(), "%s: Number of App layers exceeded the limit ",
+                 __FUNCTION__);
+        return 0;
+    }
+
     //Hard conditions, if not met, cannot do MDP comp
     if(!isFrameDoable(ctx)) {
         ALOGD_IF( isDebug(),"%s: MDP Comp not possible for this frame",