hwc: Refactor windowboxing feature

1. Remove AIV video mode composition stratergy and reuse video only
   composition stratergy to achieve the same functionality.
2. Drop all non AIV layers from the external list at the start of MDP
   composition prepare and fall back to video only composition.

Change-Id: I34760e2df57cfbb923a6be0182e632c9ddd6aa07
diff --git a/libhwcomposer/hwc_mdpcomp.cpp b/libhwcomposer/hwc_mdpcomp.cpp
index 5bd3392..4a74e96 100644
--- a/libhwcomposer/hwc_mdpcomp.cpp
+++ b/libhwcomposer/hwc_mdpcomp.cpp
@@ -710,11 +710,8 @@
     const int numAppLayers = ctx->listStats[mDpy].numAppLayers;
     int priDispW = ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres;
 
-    // This is a special mode which needs to be handled separately.
-    // Only AIV tagged layers will be displayed on external in this mode.
-    // This is applicable only for external display , Return false, so that
-    // this will be handled separately
-    if(ctx->mAIVVideoMode[mDpy]) {
+    // Fall back to video only composition, if AIV video mode is enabled
+    if(ctx->listStats[mDpy].mAIVVideoMode) {
         ALOGD_IF(isDebug(), "%s: AIV Video Mode enabled dpy %d",
             __FUNCTION__, mDpy);
         return false;
@@ -1261,55 +1258,8 @@
     return true;
 }
 
-bool MDPComp::tryAIVVideoMode(hwc_context_t *ctx,
-        hwc_display_contents_1_t* list) {
-    if(sSimulationFlags & MDPCOMP_AVOID_AIV_VIDEO_MODE)
-        return false;
-    if(!ctx->mAIVVideoMode[mDpy]) {
-        return false;
-    }
-    int numAppLayers = ctx->listStats[mDpy].numAppLayers;
-
-    mCurrentFrame.reset(numAppLayers);
-    updateAIVLayers(ctx, list);
-    int mdpCount = mCurrentFrame.mdpCount;
-
-    if(mdpCount == 0) {
-        reset(ctx);
-        return false;
-    }
-
-    if(mCurrentFrame.fbCount) {
-        mCurrentFrame.fbZ = mCurrentFrame.mdpCount;
-    }
-
-    if(sEnableYUVsplit){
-        adjustForSourceSplit(ctx, list);
-    }
-
-    if(!postHeuristicsHandling(ctx, list)) {
-        ALOGD_IF(isDebug(), "post heuristic handling failed");
-        reset(ctx);
-        return false;
-    }
-
-    ALOGD_IF(sSimulationFlags,"%s: AIV_VIDEO_MODE_COMP SUCCEEDED",
-             __FUNCTION__);
-    return true;
-}
-
 bool MDPComp::tryVideoOnly(hwc_context_t *ctx,
         hwc_display_contents_1_t* list) {
-    // This is a special mode which needs to be handled separately.
-    // Only AIV tagged layers will be displayed on external in this mode.
-    // This is applicable only for external display , Return false, so that
-    // this will be handled separately
-    if(ctx->mAIVVideoMode[mDpy]) {
-        ALOGD_IF(isDebug(), "%s: AIV Video Mode enabled dpy %d",
-            __FUNCTION__, mDpy);
-        return false;
-    }
-
     const bool secureOnly = true;
     return videoOnlyComp(ctx, list, not secureOnly) or
             videoOnlyComp(ctx, list, secureOnly);
@@ -1359,11 +1309,8 @@
 /* if tryFullFrame fails, try to push all video and secure RGB layers to MDP */
 bool MDPComp::tryMDPOnlyLayers(hwc_context_t *ctx,
         hwc_display_contents_1_t* list) {
-    // This is a special mode which needs to be handled separately.
-    // Only AIV tagged layers will be displayed on external in this mode.
-    // This is applicable only for external display , Return false, so that
-    // this will be handled separately
-    if(ctx->mAIVVideoMode[mDpy]) {
+    // Fall back to video only composition, if AIV video mode is enabled
+    if(ctx->listStats[mDpy].mAIVVideoMode) {
         ALOGD_IF(isDebug(), "%s: AIV Video Mode enabled dpy %d",
             __FUNCTION__, mDpy);
         return false;
@@ -1665,17 +1612,12 @@
             __FUNCTION__, frame.mdpCount, frame.fbCount, frame.dropCount);
 }
 
-// Mark AIV layers for composition and drop other non-AIV layers.
-void MDPComp::updateAIVLayers(hwc_context_t* ctx,
+// drop other non-AIV layers from external display list.
+void MDPComp::dropNonAIVLayers(hwc_context_t* ctx,
                               hwc_display_contents_1_t* list) {
     for (size_t i = 0; i < (size_t)ctx->listStats[mDpy].numAppLayers; i++) {
         hwc_layer_1_t * layer = &list->hwLayers[i];
-        if(isAIVVideoLayer(layer)) {
-            if(isYUVDoable(ctx, layer)) {
-                mCurrentFrame.isFBComposed[i] = false;
-                mCurrentFrame.fbCount--;
-            }
-        } else if(!isAIVCCLayer(layer)) {
+         if(!(isAIVVideoLayer(layer) || isAIVCCLayer(layer))) {
             mCurrentFrame.dropCount++;
             mCurrentFrame.drop[i] = true;
         }
@@ -1695,6 +1637,10 @@
         int nYuvIndex = ctx->listStats[mDpy].yuvIndices[index];
         hwc_layer_1_t* layer = &list->hwLayers[nYuvIndex];
 
+        if(mCurrentFrame.drop[nYuvIndex]) {
+            continue;
+        }
+
         if(!isYUVDoable(ctx, layer)) {
             if(!frame.isFBComposed[nYuvIndex]) {
                 frame.isFBComposed[nYuvIndex] = true;
@@ -1963,11 +1909,16 @@
     //Hard conditions, if not met, cannot do MDP comp
     if(isFrameDoable(ctx)) {
         generateROI(ctx, list);
+        // if AIV Video mode is enabled, drop all non AIV layers from the
+        // external display list.
+        if(ctx->listStats[mDpy].mAIVVideoMode) {
+            dropNonAIVLayers(ctx, list);
+        }
 
         // if tryFullFrame fails, try to push all video and secure RGB layers
         // to MDP for composition.
         mModeOn = tryFullFrame(ctx, list) || tryMDPOnlyLayers(ctx, list) ||
-                  tryVideoOnly(ctx, list) || tryAIVVideoMode(ctx, list);
+                  tryVideoOnly(ctx, list);
         if(mModeOn) {
             setMDPCompLayerFlags(ctx, list);
         } else {
@@ -2655,8 +2606,8 @@
             whf.format = getMdpFormat(HAL_PIXEL_FORMAT_BGRX_8888);
     }
     // update source crop and destination position of AIV video layer.
-    if(ctx->mAIVVideoMode[mDpy] &&isYuvBuffer(hnd)) {
-        updateExtDisplayCoordinates(ctx, crop, dst, mDpy);
+    if(ctx->listStats[mDpy].mAIVVideoMode && isYuvBuffer(hnd)) {
+        updateCoordinates(ctx, crop, dst, mDpy);
     }
     /* Calculate the external display position based on MDP downscale,
        ActionSafe, and extorientation features. */
diff --git a/libhwcomposer/hwc_mdpcomp.h b/libhwcomposer/hwc_mdpcomp.h
index 5b47984..4634fbc 100644
--- a/libhwcomposer/hwc_mdpcomp.h
+++ b/libhwcomposer/hwc_mdpcomp.h
@@ -75,7 +75,6 @@
         MDPCOMP_AVOID_LOAD_MDP = 0x004,
         MDPCOMP_AVOID_VIDEO_ONLY = 0x008,
         MDPCOMP_AVOID_MDP_ONLY_LAYERS = 0x010,
-        MDPCOMP_AVOID_AIV_VIDEO_MODE = 0x020,
     };
 
     /* mdp pipe data */
@@ -188,8 +187,6 @@
     bool loadBasedComp(hwc_context_t *ctx, hwc_display_contents_1_t* list);
     /* Checks if its worth doing load based partial comp */
     bool isLoadBasedCompDoable(hwc_context_t *ctx);
-    /* checks for conditions where AIV layers cannot be bypassed */
-    bool tryAIVVideoMode(hwc_context_t *ctx, hwc_display_contents_1_t* list);
     /* checks for conditions where only video can be bypassed */
     bool tryVideoOnly(hwc_context_t *ctx, hwc_display_contents_1_t* list);
     bool videoOnlyComp(hwc_context_t *ctx, hwc_display_contents_1_t* list,
@@ -225,8 +222,8 @@
     bool intersectingUpdatingLayers(const hwc_display_contents_1_t* list,
             int fromIndex, int toIndex, int targetLayerIndex);
 
-    /* Mark AIV layers for composition and drop other non-AIV layers.*/
-    void updateAIVLayers(hwc_context_t* ctx, hwc_display_contents_1_t* list);
+    /* drop other non-AIV layers from external display list.*/
+    void dropNonAIVLayers(hwc_context_t* ctx, hwc_display_contents_1_t* list);
 
         /* updates cache map with YUV info */
     void updateYUV(hwc_context_t* ctx, hwc_display_contents_1_t* list,
diff --git a/libhwcomposer/hwc_utils.cpp b/libhwcomposer/hwc_utils.cpp
index dcc7687..7e77d95 100644
--- a/libhwcomposer/hwc_utils.cpp
+++ b/libhwcomposer/hwc_utils.cpp
@@ -964,7 +964,7 @@
     uint32_t refreshRate = 0;
     qdutils::MDPVersion& mdpHw = qdutils::MDPVersion::getInstance();
 
-    ctx->mAIVVideoMode[dpy] = false;
+    ctx->listStats[dpy].mAIVVideoMode = false;
     resetROI(ctx, dpy);
 
     trimList(ctx, list, dpy);
@@ -974,8 +974,10 @@
         private_handle_t *hnd = (private_handle_t *)layer->handle;
 
 #ifdef QCOM_BSP
+        // Window boxing feature is applicable obly for external display, So
+        // enable mAIVVideoMode only for external display
         if(ctx->mWindowboxFeature && dpy && isAIVVideoLayer(layer)) {
-            ctx->mAIVVideoMode[dpy] = true;
+            ctx->listStats[dpy].mAIVVideoMode = true;
         }
         if (layer->flags & HWC_SCREENSHOT_ANIMATOR_LAYER) {
             ctx->listStats[dpy].isDisplayAnimating = true;
@@ -1880,7 +1882,7 @@
              crop.left, crop.top, crop.right, crop.bottom);
 }
 
-void updateExtDisplayCoordinates(hwc_context_t *ctx, hwc_rect_t& crop,
+void updateCoordinates(hwc_context_t *ctx, hwc_rect_t& crop,
                            hwc_rect_t& dst, int dpy) {
     updateCropAIVVideoMode(ctx, crop, dpy);
     updateDestAIVVideoMode(ctx, crop, dst, dpy);
@@ -1919,8 +1921,8 @@
             whf.format = getMdpFormat(HAL_PIXEL_FORMAT_BGRX_8888);
     }
     // update source crop and destination position of AIV video layer.
-    if(ctx->mAIVVideoMode[dpy] && isYuvBuffer(hnd)) {
-        updateExtDisplayCoordinates(ctx, crop, dst, dpy);
+    if(ctx->listStats[dpy].mAIVVideoMode && isYuvBuffer(hnd)) {
+        updateCoordinates(ctx, crop, dst, dpy);
     }
     calcExtDisplayPosition(ctx, hnd, dpy, crop, dst, transform, orient);
     int downscale = getRotDownscale(ctx, layer);
@@ -2017,8 +2019,8 @@
     }
 
     // update source crop and destination position of AIV video layer.
-    if(ctx->mAIVVideoMode[dpy] && isYuvBuffer(hnd)) {
-        updateExtDisplayCoordinates(ctx, crop, dst, dpy);
+    if(ctx->listStats[dpy].mAIVVideoMode && isYuvBuffer(hnd)) {
+        updateCoordinates(ctx, crop, dst, dpy);
     }
 
     /* Calculate the external display position based on MDP downscale,
@@ -2163,8 +2165,8 @@
             getMdpFormat(hnd->format), (uint32_t)hnd->size);
 
     // update source crop and destination position of AIV video layer.
-    if(ctx->mAIVVideoMode[dpy] && isYuvBuffer(hnd)) {
-        updateExtDisplayCoordinates(ctx, crop, dst, dpy);
+    if(ctx->listStats[dpy].mAIVVideoMode && isYuvBuffer(hnd)) {
+        updateCoordinates(ctx, crop, dst, dpy);
     }
 
     /* Calculate the external display position based on MDP downscale,
diff --git a/libhwcomposer/hwc_utils.h b/libhwcomposer/hwc_utils.h
index c443bf8..e84387d 100644
--- a/libhwcomposer/hwc_utils.h
+++ b/libhwcomposer/hwc_utils.h
@@ -141,6 +141,8 @@
     int secureRGBIndices[MAX_NUM_APP_LAYERS];
     //dyn refresh rate-Client requested refreshrate
     uint32_t refreshRateRequest;
+    // Flag related to windowboxing feature
+    bool mAIVVideoMode;
 };
 
 //PTOR Comp info
@@ -381,7 +383,7 @@
 bool isZoomModeEnabled(hwc_rect_t crop);
 void updateCropAIVVideoMode(hwc_context_t *ctx, hwc_rect_t& crop, int dpy);
 void updateDestAIVVideoMode(hwc_context_t *ctx, hwc_rect_t& dst, int dpy);
-void updateExtDisplayCoordinates(hwc_context_t *ctx, hwc_rect_t& crop,
+void updateCoordinates(hwc_context_t *ctx, hwc_rect_t& crop,
                            hwc_rect_t& dst, int dpy);
 
 //Routine to configure low resolution panels (<= 2048 width)
@@ -618,8 +620,7 @@
     bool mThermalBurstMode;
     //Layers out of ROI
     bool copybitDrop[MAX_NUM_APP_LAYERS];
-    // Flags related to windowboxing feature
-    bool mAIVVideoMode[HWC_NUM_DISPLAY_TYPES];
+    // Flag related to windowboxing feature
     bool mWindowboxFeature;
 };