hwc/overlay: Fix dma mode design and issues.

Add support for tracking the mode in which DMA is being used in overlay.
Remove unnecessary tracking variables, and their set,reset inconsistency.

With such varibles in hwc, it still leads to overlay giving out DMA pipes.
Make overlay as the single tracking place.

Change-Id: Ib949f3d0ef5918ef323c81cb1768b68a68c83da4
diff --git a/libhwcomposer/hwc.cpp b/libhwcomposer/hwc.cpp
index 8e7afa2..4f26191 100644
--- a/libhwcomposer/hwc.cpp
+++ b/libhwcomposer/hwc.cpp
@@ -38,6 +38,8 @@
 #include "profiler.h"
 
 using namespace qhwc;
+using namespace overlay;
+
 #define VSYNC_DEBUG 0
 #define BLANK_DEBUG 0
 
@@ -200,7 +202,7 @@
 
     ctx->mOverlay->configBegin();
     ctx->mRotMgr->configBegin();
-    ctx->mNeedsRotator = false;
+    Overlay::setDMAMode(Overlay::DMA_LINE_MODE);
 
     for (int32_t i = numDisplays; i >= 0; i--) {
         hwc_display_contents_1_t *list = displays[i];
diff --git a/libhwcomposer/hwc_mdpcomp.cpp b/libhwcomposer/hwc_mdpcomp.cpp
index 30178a0..2323030 100644
--- a/libhwcomposer/hwc_mdpcomp.cpp
+++ b/libhwcomposer/hwc_mdpcomp.cpp
@@ -23,7 +23,7 @@
 #include "mdp_version.h"
 #include <overlayRotator.h>
 
-using overlay::Rotator;
+using namespace overlay;
 using namespace overlay::utils;
 namespace ovutils = overlay::utils;
 
@@ -103,9 +103,10 @@
     }
 
     sMaxPipesPerMixer = MAX_PIPES_PER_MIXER;
-    if(property_get("debug.mdpcomp.maxpermixer", property, NULL) > 0) {
-        if(atoi(property) != 0)
-            sMaxPipesPerMixer = true;
+    if(property_get("debug.mdpcomp.maxpermixer", property, "-1") > 0) {
+        int val = atoi(property);
+        if(val >= 0)
+            sMaxPipesPerMixer = min(val, MAX_PIPES_PER_MIXER);
     }
 
     unsigned long idle_timeout = DEFAULT_IDLE_TIME;
@@ -289,7 +290,6 @@
     case MDPCOMP_OV_DMA:
         mdp_pipe = ov.nextPipe(ovutils::OV_MDP_PIPE_DMA, mDpy);
         if(mdp_pipe != ovutils::OV_INVALID) {
-            ctx->mDMAInUse = true;
             return mdp_pipe;
         }
     case MDPCOMP_OV_ANY:
@@ -350,7 +350,7 @@
         return false;
     }
 
-    if(mdpCount > (sMaxPipesPerMixer-fbNeeded)) {
+    if(mdpCount > (sMaxPipesPerMixer - fbNeeded)) {
         ALOGD_IF(isDebug(), "%s: Exceeds MAX_PIPES_PER_MIXER",__FUNCTION__);
         return false;
     }
@@ -400,11 +400,6 @@
         return false;
     }
 
-    if(ctx->mNeedsRotator && ctx->mDMAInUse) {
-        ALOGE("%s: No DMA for Rotator",__FUNCTION__);
-        return false;
-    }
-
     if(isSecuring(ctx, layer)) {
         ALOGD_IF(isDebug(), "%s: MDP securing is active", __FUNCTION__);
         return false;
@@ -468,6 +463,7 @@
     ALOGD_IF(isDebug(),"%s: cached count: %d",__FUNCTION__,
              mCurrentFrame.fbCount);
 }
+
 void MDPComp::updateLayerCache(hwc_context_t* ctx,
                                hwc_display_contents_1_t* list) {
 
@@ -501,7 +497,7 @@
     int numAvailable = ov.availablePipes(mDpy);
 
     //Reserve DMA for rotator
-    if(ctx->mNeedsRotator)
+    if(Overlay::getDMAMode() == Overlay::DMA_BLOCK_MODE)
         numAvailable -= numDMAPipes;
 
     //Reserve pipe(s)for FB
@@ -544,7 +540,6 @@
 
 int MDPComp::programMDP(hwc_context_t *ctx, hwc_display_contents_1_t* list) {
     int fbZOrder = -1;
-    ctx->mDMAInUse = false;
 
     if(!allocLayerPipes(ctx, list)) {
         ALOGD_IF(isDebug(), "%s: Unable to allocate MDP pipes", __FUNCTION__);
@@ -718,8 +713,9 @@
 
         ePipeType type = MDPCOMP_OV_ANY;
 
-        if(!qhwc::needsScaling(layer) && !ctx->mNeedsRotator
-           && ctx->mMDP.version >= qdutils::MDSS_V5) {
+        if(!qhwc::needsScaling(layer)
+            && Overlay::getDMAMode() != Overlay::DMA_BLOCK_MODE
+            && ctx->mMDP.version >= qdutils::MDSS_V5) {
             type = MDPCOMP_OV_DMA;
         }
 
@@ -889,8 +885,9 @@
 
         ePipeType type = MDPCOMP_OV_ANY;
 
-        if(!qhwc::needsScaling(layer) && !ctx->mNeedsRotator
-           && ctx->mMDP.version >= qdutils::MDSS_V5)
+        if(!qhwc::needsScaling(layer)
+            && Overlay::getDMAMode() != Overlay::DMA_BLOCK_MODE
+            && ctx->mMDP.version >= qdutils::MDSS_V5)
             type = MDPCOMP_OV_DMA;
 
         if(!acquireMDPPipes(ctx, layer, pipe_info, type)) {
diff --git a/libhwcomposer/hwc_utils.cpp b/libhwcomposer/hwc_utils.cpp
index db927e5..a578bb0 100644
--- a/libhwcomposer/hwc_utils.cpp
+++ b/libhwcomposer/hwc_utils.cpp
@@ -332,8 +332,9 @@
             ctx->listStats[dpy].yuvIndices[yuvCount] = i;
             yuvCount++;
 
-            if(layer->transform & HWC_TRANSFORM_ROT_90)
-                ctx->mNeedsRotator = true;
+            if(layer->transform & HWC_TRANSFORM_ROT_90) {
+                Overlay::setDMAMode(Overlay::DMA_BLOCK_MODE);
+            }
         }
         if(layer->blending == HWC_BLENDING_PREMULT)
             ctx->listStats[dpy].preMultipliedAlpha = true;
diff --git a/libhwcomposer/hwc_utils.h b/libhwcomposer/hwc_utils.h
index 8c51b38..001e3e8 100644
--- a/libhwcomposer/hwc_utils.h
+++ b/libhwcomposer/hwc_utils.h
@@ -274,10 +274,6 @@
     mutable Locker mExtSetLock;
     //Vsync
     struct vsync_state vstate;
-    //DMA used for rotator
-    bool mDMAInUse;
-    //MDP rotater needed
-    bool mNeedsRotator;
 };
 
 namespace qhwc {
diff --git a/liboverlay/overlay.cpp b/liboverlay/overlay.cpp
index 1faeadf..43b6589 100644
--- a/liboverlay/overlay.cpp
+++ b/liboverlay/overlay.cpp
@@ -92,6 +92,11 @@
             if((mPipeBook[i].mDisplay == PipeBook::DPY_UNUSED ||
                     mPipeBook[i].mDisplay == dpy) &&
                     PipeBook::isNotAllocated(i)) {
+                //In block mode we don't allow line operations
+                if(sDMAMode == DMA_BLOCK_MODE &&
+                    PipeBook::getPipeType((eDest)i) == OV_MDP_PIPE_DMA)
+                    continue;
+
                 dest = (eDest)i;
                 PipeBook::setAllocation(i);
                 break;
@@ -311,6 +316,7 @@
 
 Overlay* Overlay::sInstance = 0;
 int Overlay::sExtFbIndex = 1;
+int Overlay::sDMAMode = DMA_LINE_MODE;
 int Overlay::PipeBook::NUM_PIPES = 0;
 int Overlay::PipeBook::sPipeUsageBitmap = 0;
 int Overlay::PipeBook::sLastUsageBitmap = 0;
diff --git a/liboverlay/overlay.h b/liboverlay/overlay.h
index fdeebc2..0cedac9 100644
--- a/liboverlay/overlay.h
+++ b/liboverlay/overlay.h
@@ -40,6 +40,8 @@
 
 class Overlay : utils::NoCopy {
 public:
+    enum { DMA_BLOCK_MODE, DMA_LINE_MODE };
+
     /* dtor close */
     ~Overlay();
 
@@ -84,6 +86,8 @@
      * to populate.
      */
     void getDump(char *buf, size_t len);
+    static void setDMAMode(const int& mode);
+    static int getDMAMode();
 
 private:
     /* Ctor setup */
@@ -148,6 +152,7 @@
     /* Singleton Instance*/
     static Overlay *sInstance;
     static int sExtFbIndex;
+    static int sDMAMode;
 };
 
 inline void Overlay::validate(int index) {
@@ -176,6 +181,15 @@
     return sExtFbIndex;
 }
 
+inline void Overlay::setDMAMode(const int& mode) {
+    if(mode == DMA_LINE_MODE || mode == DMA_BLOCK_MODE)
+        sDMAMode = mode;
+}
+
+inline int Overlay::getDMAMode() {
+    return sDMAMode;
+}
+
 inline bool Overlay::PipeBook::valid() {
     return (mPipe != NULL);
 }