overlay: Add pipe life-cycle logging

Add pipe life-cycle logging including set, unset, commit.
Remove the older unused logging mechanism.
This is enabled via binder as:
$adb shell service call display.qservice 15 i32 4 i32 1
where 15 is DYNAMIC_DEBUG, 4 is for pipe lifecycle debugging and
1 is to enable

Change-Id: I6c02e24aa91135d4a6c67870b4c103546947da48
diff --git a/libhwcomposer/hwc_qclient.cpp b/libhwcomposer/hwc_qclient.cpp
index efe6e16..7e22fd0 100644
--- a/libhwcomposer/hwc_qclient.cpp
+++ b/libhwcomposer/hwc_qclient.cpp
@@ -33,12 +33,14 @@
 #include <mdp_version.h>
 #include <hwc_mdpcomp.h>
 #include <hwc_virtual.h>
+#include <overlay.h>
 
 #define QCLIENT_DEBUG 0
 
 using namespace android;
 using namespace qService;
 using namespace qhwc;
+using namespace overlay;
 
 namespace qClient {
 
@@ -236,6 +238,10 @@
             HWCVirtualBase::dynamicDebug(enable);
             if (debug_type != IQService::DEBUG_ALL)
                 break;
+        case IQService::DEBUG_PIPE_LIFECYCLE:
+            Overlay::debugPipeLifecycle(enable);
+            if (debug_type != IQService::DEBUG_ALL)
+                break;
     }
 }
 
diff --git a/liboverlay/mdpWrapper.h b/liboverlay/mdpWrapper.h
index 9c38cdb..36faf07 100644
--- a/liboverlay/mdpWrapper.h
+++ b/liboverlay/mdpWrapper.h
@@ -44,8 +44,11 @@
 #include <utils/Trace.h>
 #include <errno.h>
 #include "overlayUtils.h"
+#include "overlay.h"
 
 #define IOCTL_DEBUG 0
+#define LIKELY( exp )       (__builtin_expect( (exp) != 0, true  ))
+#define UNLIKELY( exp )     (__builtin_expect( (exp) != 0, false ))
 
 namespace overlay{
 
@@ -180,11 +183,33 @@
 
 inline bool validateAndSet(const int& fd, mdp_overlay_list& list) {
     ATRACE_CALL();
+    uint32_t id = 0;
+    if(UNLIKELY(Overlay::isDebugPipeLifecycle())) {
+        for(uint32_t i = 0; i < list.num_overlays; i++) {
+            if(list.overlay_list[i]->id != (uint32_t)MSMFB_NEW_REQUEST) {
+                id |= list.overlay_list[i]->id;
+            }
+        }
+
+        ALOGD("%s Total pipes needed: %d, Exisiting pipe mask 0x%04x",
+                __FUNCTION__, list.num_overlays, id);
+        id = 0;
+    }
+
     if (ioctl(fd, MSMFB_OVERLAY_PREPARE, &list) < 0) {
         ALOGD_IF(IOCTL_DEBUG, "Failed to call ioctl MSMFB_OVERLAY_PREPARE "
                 "err=%s", strerror(errno));
         return false;
     }
+
+    if(UNLIKELY(Overlay::isDebugPipeLifecycle())) {
+        for(uint32_t i = 0; i < list.num_overlays; i++) {
+            id |= list.overlay_list[i]->id;
+        }
+
+        ALOGD("%s Pipe mask after OVERLAY_PREPARE 0x%04x", __FUNCTION__, id);
+    }
+
     return true;
 }
 
@@ -200,6 +225,9 @@
 
 inline bool unsetOverlay(int fd, int ovId) {
     ATRACE_CALL();
+    ALOGD_IF(Overlay::isDebugPipeLifecycle(), "%s Unsetting pipe 0x%04x",
+            __FUNCTION__, ovId);
+
     if (ioctl(fd, MSMFB_OVERLAY_UNSET, &ovId) < 0) {
         ALOGE("Failed to call ioctl MSMFB_OVERLAY_UNSET err=%s",
                 strerror(errno));
@@ -230,6 +258,8 @@
 
 inline bool displayCommit(int fd, mdp_display_commit& info) {
     ATRACE_CALL();
+    ALOGD_IF(Overlay::isDebugPipeLifecycle(), "%s", __FUNCTION__);
+
     if(ioctl(fd, MSMFB_DISPLAY_COMMIT, &info) == -1) {
         ALOGE("Failed to call ioctl MSMFB_DISPLAY_COMMIT err=%s",
                 strerror(errno));
diff --git a/liboverlay/overlay.cpp b/liboverlay/overlay.cpp
index 26c8845..cad4c7d 100644
--- a/liboverlay/overlay.cpp
+++ b/liboverlay/overlay.cpp
@@ -33,8 +33,6 @@
 #include "mdp_version.h"
 #include "qdMetaData.h"
 
-#define PIPE_DEBUG 0
-
 namespace overlay {
 using namespace utils;
 using namespace qdutils;
@@ -46,7 +44,6 @@
         mPipeBook[i].init();
     }
 
-    mDumpStr[0] = '\0';
     initScalar();
     setDMAMultiplexingSupported();
 }
@@ -64,7 +61,6 @@
         PipeBook::resetUse(i);
         PipeBook::resetAllocation(i);
     }
-    mDumpStr[0] = '\0';
 }
 
 void Overlay::configDone() {
@@ -73,19 +69,9 @@
                     isSessionEnded((eDest)i)) {
             //Forces UNSET on pipes, flushes rotator memory and session, closes
             //fds
-            if(mPipeBook[i].valid()) {
-                char str[32];
-                snprintf(str, 32, "Unset=%s dpy=%d mix=%d; ",
-                        PipeBook::getDestStr((eDest)i),
-                        mPipeBook[i].mDisplay, mPipeBook[i].mMixer);
-#if PIPE_DEBUG
-                strlcat(mDumpStr, str, sizeof(mDumpStr));
-#endif
-            }
             mPipeBook[i].destroy();
         }
     }
-    dump();
     PipeBook::save();
 }
 
@@ -138,16 +124,7 @@
         if(not mPipeBook[index].valid()) {
             mPipeBook[index].mPipe = new GenericPipe(dpy);
             mPipeBook[index].mSession = PipeBook::NONE;
-            char str[32];
-            snprintf(str, 32, "Set=%s dpy=%d mix=%d; ",
-                     PipeBook::getDestStr(dest), dpy, mixer);
-#if PIPE_DEBUG
-            strlcat(mDumpStr, str, sizeof(mDumpStr));
-#endif
         }
-    } else {
-        ALOGD_IF(PIPE_DEBUG, "Pipe unavailable type=%d display=%d mixer=%d",
-                (int)type, dpy, mixer);
     }
 
     return dest;
@@ -507,14 +484,6 @@
     return true;
 }
 
-void Overlay::dump() const {
-#if PIPE_DEBUG
-    if(strlen(mDumpStr)) { //dump only on state change
-        ALOGD("%s\n", mDumpStr);
-    }
-#endif
-}
-
 void Overlay::getDump(char *buf, size_t len) {
     int totalPipes = 0;
     const char *str = "\nOverlay State\n\n";
@@ -596,6 +565,7 @@
 int Overlay::sDpyFbMap[DPY_MAX] = {0, -1, -1};
 int Overlay::sDMAMode = DMA_LINE_MODE;
 bool Overlay::sDMAMultiplexingSupported = false;
+bool Overlay::sDebugPipeLifecycle = false;
 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 4f5d56d..e8369ca 100644
--- a/liboverlay/overlay.h
+++ b/liboverlay/overlay.h
@@ -152,6 +152,10 @@
      * Single interface panels will only update left ROI. */
     static bool displayCommit(const int& fd, const utils::Dim& lRoi,
                               const utils::Dim& rRoi);
+    /* Logs pipe lifecycle events like set, unset, commit when enabled */
+    static void debugPipeLifecycle(const bool& enable);
+    /* Returns true if pipe life cycle logging is enabled */
+    static bool isDebugPipeLifecycle();
 
 private:
     /* Ctor setup */
@@ -159,7 +163,6 @@
     /*Validate index range, abort if invalid */
     void validate(int index);
     static void setDMAMultiplexingSupported();
-    void dump() const;
     /* Returns an available pipe based on the type of pipe requested. When ANY
      * is requested, the first available VG or RGB is returned. If no pipe is
      * available for the display "dpy" then INV is returned. Note: If a pipe is
@@ -239,9 +242,6 @@
 
     PipeBook mPipeBook[utils::OV_INVALID]; //Used as max
 
-    /* Dump string */
-    char mDumpStr[1024];
-
     /* Singleton Instance*/
     static Overlay *sInstance;
     static int sDpyFbMap[DPY_MAX];
@@ -249,6 +249,7 @@
     static bool sDMAMultiplexingSupported;
     static void *sLibScaleHandle;
     static int (*sFnProgramScale)(struct mdp_overlay_list *);
+    static bool sDebugPipeLifecycle;
 
     friend class MdpCtrl;
 };
@@ -342,6 +343,14 @@
     return sFnProgramScale;
 }
 
+inline void Overlay::debugPipeLifecycle(const bool& enable) {
+    sDebugPipeLifecycle = enable;
+}
+
+inline bool Overlay::isDebugPipeLifecycle() {
+    return sDebugPipeLifecycle;
+}
+
 inline bool Overlay::PipeBook::valid() {
     return (mPipe != NULL);
 }
diff --git a/libqservice/IQService.h b/libqservice/IQService.h
index 9be04b2..30c064e 100644
--- a/libqservice/IQService.h
+++ b/libqservice/IQService.h
@@ -65,7 +65,8 @@
         DEBUG_ALL,
         DEBUG_MDPCOMP,
         DEBUG_VSYNC,
-        DEBUG_VD
+        DEBUG_VD,
+        DEBUG_PIPE_LIFECYCLE,
     };
 
     // Register a client that can be notified