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.

Change-Id: Id4458fcadc7775a2d78b1849de6782857c6ac17f
diff --git a/libhwcomposer/hwc_mdpcomp.cpp b/libhwcomposer/hwc_mdpcomp.cpp
index ff48d79..d7366fb 100644
--- a/libhwcomposer/hwc_mdpcomp.cpp
+++ b/libhwcomposer/hwc_mdpcomp.cpp
@@ -366,10 +366,6 @@
         ctx->isPaddingRound = false;
         ALOGD_IF(isDebug(), "%s: padding round",__FUNCTION__);
         ret = false;
-    } else if(numAppLayers > MAX_NUM_APP_LAYERS) {
-        ALOGD_IF(isDebug(), "%s: Number of App layers exceeded the limit ",
-                 __FUNCTION__);
-        ret = false;
     }
     return ret;
 }
@@ -722,8 +718,16 @@
 
 int MDPComp::prepare(hwc_context_t *ctx, hwc_display_contents_1_t* list) {
 
-    //reset old data
     const int numLayers = ctx->listStats[mDpy].numAppLayers;
+
+    //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;
+    }
+    //reset old data
     mCurrentFrame.reset(numLayers);
 
     //Hard conditions, if not met, cannot do MDP comp