hwc: Reserve pipes for video layers first

There could be scenarios where the bottom layers are RGB, which could
take up all ViG pipes as well, leaving no pipes for videos. Using
mixed mode may not help if those RGB layers are updating, since GPU
will be continuously invoked.

This patch reserves pipes for videos instead of serial order.

Change-Id: Ia5e25294ecc18a54dc36923a930cb23914d9475b
diff --git a/libhwcomposer/hwc_mdpcomp.cpp b/libhwcomposer/hwc_mdpcomp.cpp
index 5572cc4..eba6f3f 100644
--- a/libhwcomposer/hwc_mdpcomp.cpp
+++ b/libhwcomposer/hwc_mdpcomp.cpp
@@ -56,6 +56,8 @@
 int (*MDPComp::sPerfLockRelease)(int value) = NULL;
 int MDPComp::sPerfHintWindow = -1;
 
+enum AllocOrder { FORMAT_YUV, FORMAT_RGB, FORMAT_MAX };
+
 MDPComp* MDPComp::getObject(hwc_context_t *ctx, const int& dpy) {
     if(qdutils::MDPVersion::getInstance().isSrcSplit()) {
         sSrcSplitEnabled = true;
@@ -2248,39 +2250,48 @@
 
 bool MDPCompNonSplit::allocLayerPipes(hwc_context_t *ctx,
         hwc_display_contents_1_t* list) {
-    for(int index = 0; index < mCurrentFrame.layerCount; index++) {
+    for(uint32_t formatType = FORMAT_YUV; formatType < FORMAT_MAX;
+            formatType++) {
+        for(int index = 0; index < mCurrentFrame.layerCount; index++) {
+            if(mCurrentFrame.isFBComposed[index]) continue;
 
-        if(mCurrentFrame.isFBComposed[index]) continue;
-
-        hwc_layer_1_t* layer = &list->hwLayers[index];
-        private_handle_t *hnd = (private_handle_t *)layer->handle;
-        if(isYUVSplitNeeded(hnd) && sEnableYUVsplit){
-            if(allocSplitVGPipes(ctx, index)){
+            hwc_layer_1_t* layer = &list->hwLayers[index];
+            private_handle_t *hnd = (private_handle_t *)layer->handle;
+            if(formatType == FORMAT_YUV && !isYuvBuffer(hnd))
                 continue;
+            if(formatType == FORMAT_RGB && isYuvBuffer(hnd))
+                continue;
+
+            if(isYUVSplitNeeded(hnd) && sEnableYUVsplit){
+                if(allocSplitVGPipes(ctx, index)){
+                    continue;
+                }
             }
-        }
 
-        int mdpIndex = mCurrentFrame.layerToMDP[index];
-        PipeLayerPair& info = mCurrentFrame.mdpToLayer[mdpIndex];
-        info.pipeInfo = new MdpPipeInfoNonSplit;
-        info.rot = NULL;
-        MdpPipeInfoNonSplit& pipe_info = *(MdpPipeInfoNonSplit*)info.pipeInfo;
+            int mdpIndex = mCurrentFrame.layerToMDP[index];
+            PipeLayerPair& info = mCurrentFrame.mdpToLayer[mdpIndex];
+            info.pipeInfo = new MdpPipeInfoNonSplit;
+            info.rot = NULL;
+            MdpPipeInfoNonSplit& pipe_info =
+                    *(MdpPipeInfoNonSplit*)info.pipeInfo;
 
-        Overlay::PipeSpecs pipeSpecs;
-        pipeSpecs.formatClass = isYuvBuffer(hnd) ?
-                Overlay::FORMAT_YUV : Overlay::FORMAT_RGB;
-        pipeSpecs.needsScaling = qhwc::needsScaling(layer) or
-                (qdutils::MDPVersion::getInstance().is8x26() and
-                ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres > 1024);
-        pipeSpecs.dpy = mDpy;
-        pipeSpecs.fb = false;
-        pipeSpecs.numActiveDisplays = ctx->numActiveDisplays;
+            Overlay::PipeSpecs pipeSpecs;
+            pipeSpecs.formatClass = isYuvBuffer(hnd) ?
+                    Overlay::FORMAT_YUV : Overlay::FORMAT_RGB;
+            pipeSpecs.needsScaling = qhwc::needsScaling(layer) or
+                    (qdutils::MDPVersion::getInstance().is8x26() and
+                     ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres > 1024);
+            pipeSpecs.dpy = mDpy;
+            pipeSpecs.fb = false;
+            pipeSpecs.numActiveDisplays = ctx->numActiveDisplays;
 
-        pipe_info.index = ctx->mOverlay->getPipe(pipeSpecs);
+            pipe_info.index = ctx->mOverlay->getPipe(pipeSpecs);
 
-        if(pipe_info.index == ovutils::OV_INVALID) {
-            ALOGD_IF(isDebug(), "%s: Unable to get pipe", __FUNCTION__);
-            return false;
+            if(pipe_info.index == ovutils::OV_INVALID) {
+                ALOGD_IF(isDebug(), "%s: Unable to get pipe for layer %d of "\
+                        "format type %d", __FUNCTION__, index, formatType);
+                return false;
+            }
         }
     }
     return true;
@@ -2482,36 +2493,43 @@
 
 bool MDPCompSplit::allocLayerPipes(hwc_context_t *ctx,
         hwc_display_contents_1_t* list) {
-    for(int index = 0 ; index < mCurrentFrame.layerCount; index++) {
+    for(uint32_t formatType = FORMAT_YUV; formatType < FORMAT_MAX;
+            formatType++) {
+        for(int index = 0 ; index < mCurrentFrame.layerCount; index++) {
+            if(mCurrentFrame.isFBComposed[index]) continue;
 
-        if(mCurrentFrame.isFBComposed[index]) continue;
-
-        hwc_layer_1_t* layer = &list->hwLayers[index];
-        private_handle_t *hnd = (private_handle_t *)layer->handle;
-        hwc_rect_t dst = layer->displayFrame;
-        const int lSplit = getLeftSplit(ctx, mDpy);
-        if(isYUVSplitNeeded(hnd) && sEnableYUVsplit){
-            if((dst.left > lSplit)||(dst.right < lSplit)){
-                if(allocSplitVGPipes(ctx, index)){
-                    continue;
-                }
-            }
-        }
-        //XXX: Check for forced 2D composition
-        if(needs3DComposition(ctx, mDpy) && get3DFormat(hnd) != HAL_NO_3D)
-            if(allocSplitVGPipes(ctx,index))
+            hwc_layer_1_t* layer = &list->hwLayers[index];
+            private_handle_t *hnd = (private_handle_t *)layer->handle;
+            if(formatType == FORMAT_YUV && !isYuvBuffer(hnd))
+                continue;
+            if(formatType == FORMAT_RGB && isYuvBuffer(hnd))
                 continue;
 
-        int mdpIndex = mCurrentFrame.layerToMDP[index];
-        PipeLayerPair& info = mCurrentFrame.mdpToLayer[mdpIndex];
-        info.pipeInfo = new MdpPipeInfoSplit;
-        info.rot = NULL;
-        MdpPipeInfoSplit& pipe_info = *(MdpPipeInfoSplit*)info.pipeInfo;
+            hwc_rect_t dst = layer->displayFrame;
+            const int lSplit = getLeftSplit(ctx, mDpy);
+            if(isYUVSplitNeeded(hnd) && sEnableYUVsplit){
+                if((dst.left > lSplit)||(dst.right < lSplit)){
+                    if(allocSplitVGPipes(ctx, index)){
+                        continue;
+                    }
+                }
+            }
+            //XXX: Check for forced 2D composition
+            if(needs3DComposition(ctx, mDpy) && get3DFormat(hnd) != HAL_NO_3D)
+                if(allocSplitVGPipes(ctx,index))
+                    continue;
 
-        if(!acquireMDPPipes(ctx, layer, pipe_info)) {
-            ALOGD_IF(isDebug(), "%s: Unable to get pipe for type",
-                    __FUNCTION__);
-            return false;
+            int mdpIndex = mCurrentFrame.layerToMDP[index];
+            PipeLayerPair& info = mCurrentFrame.mdpToLayer[mdpIndex];
+            info.pipeInfo = new MdpPipeInfoSplit;
+            info.rot = NULL;
+            MdpPipeInfoSplit& pipe_info = *(MdpPipeInfoSplit*)info.pipeInfo;
+
+            if(!acquireMDPPipes(ctx, layer, pipe_info)) {
+                ALOGD_IF(isDebug(), "%s: Unable to get pipe for layer %d of "\
+                        "format type %d", __FUNCTION__, index, formatType);
+                return false;
+            }
         }
     }
     return true;