overlay: Add support for pipe dumps.

Add support for pipe state dump useful in dumpsys.
Reduce debug log spam when mdp state changes.

Change-Id: Ie27ad00698085e2ffd6166ad2f7503fe16a571f8
diff --git a/libhwcomposer/hwc.cpp b/libhwcomposer/hwc.cpp
index 6a0e0ee..187ec4a 100644
--- a/libhwcomposer/hwc.cpp
+++ b/libhwcomposer/hwc.cpp
@@ -487,13 +487,15 @@
 void hwc_dump(struct hwc_composer_device_1* dev, char *buff, int buff_len)
 {
     hwc_context_t* ctx = (hwc_context_t*)(dev);
-    android::String8 buf("");
-    dumpsys_log(buf, "Qualcomm HWC state:\n");
-    dumpsys_log(buf, "  MDPVersion=%d\n", ctx->mMDP.version);
-    dumpsys_log(buf, "  DisplayPanel=%c\n", ctx->mMDP.panel);
-    ctx->mMDPComp->dump(buf);
-    //XXX: Call Other dump functions
-    strlcpy(buff, buf.string(), buff_len);
+    android::String8 aBuf("");
+    dumpsys_log(aBuf, "Qualcomm HWC state:\n");
+    dumpsys_log(aBuf, "  MDPVersion=%d\n", ctx->mMDP.version);
+    dumpsys_log(aBuf, "  DisplayPanel=%c\n", ctx->mMDP.panel);
+    ctx->mMDPComp->dump(aBuf);
+    char ovDump[2048] = {'\0'};
+    ctx->mOverlay->getDump(ovDump, 2048);
+    dumpsys_log(aBuf, ovDump);
+    strlcpy(buff, aBuf.string(), buff_len);
 }
 
 static int hwc_device_close(struct hw_device_t *dev)
diff --git a/liboverlay/overlay.cpp b/liboverlay/overlay.cpp
index 04deee5..441b822 100644
--- a/liboverlay/overlay.cpp
+++ b/liboverlay/overlay.cpp
@@ -202,10 +202,28 @@
 
 void Overlay::dump() const {
     if(strlen(mDumpStr)) { //dump only on state change
-        ALOGD("%s\n", mDumpStr);
+        ALOGD_IF(PIPE_DEBUG, "%s\n", mDumpStr);
     }
 }
 
+void Overlay::getDump(char *buf, size_t len) {
+    int totalPipes = 0;
+    const char *str = "\nOverlay State\n==========================\n";
+    strncat(buf, str, strlen(str));
+    for(int i = 0; i < PipeBook::NUM_PIPES; i++) {
+        if(mPipeBook[i].valid()) {
+            mPipeBook[i].mPipe->getDump(buf, len);
+            char str[64] = {'\0'};
+            snprintf(str, 64, "Attached to dpy=%d\n\n", mPipeBook[i].mDisplay);
+            strncat(buf, str, strlen(str));
+            totalPipes++;
+        }
+    }
+    char str_pipes[64] = {'\0'};
+    snprintf(str_pipes, 64, "Pipes used=%d\n\n", totalPipes);
+    strncat(buf, str_pipes, strlen(str_pipes));
+}
+
 void Overlay::PipeBook::init() {
     mPipe = NULL;
     mDisplay = DPY_UNUSED;
diff --git a/liboverlay/overlay.h b/liboverlay/overlay.h
index a470f45..0f15baa 100644
--- a/liboverlay/overlay.h
+++ b/liboverlay/overlay.h
@@ -77,6 +77,10 @@
     void setExtFbNum(int fbNum);
     /* Returns framebuffer index of the current external display */
     int getExtFbNum();
+    /* Returns pipe dump. Expects a NULL terminated buffer of big enough size
+     * to populate.
+     */
+    void getDump(char *buf, size_t len);
 
 private:
     /* Ctor setup */
diff --git a/liboverlay/overlayCtrlData.h b/liboverlay/overlayCtrlData.h
index 0c49bdd..c9a4949 100644
--- a/liboverlay/overlayCtrlData.h
+++ b/liboverlay/overlayCtrlData.h
@@ -81,9 +81,6 @@
     /* retrieve cached crop data */
     utils::Dim getCrop() const;
 
-    /* dump the state of the object */
-    void dump() const;
-
     /* Perform transformation calculations */
     void doTransform();
 
@@ -96,6 +93,12 @@
     /* Update the src format */
     void updateSrcformat(const uint32_t& inputsrcFormat);
 
+    /* dump the state of the object */
+    void dump() const;
+
+    /* Return the dump in the specified buffer */
+    void getDump(char *buf, size_t len);
+
 private:
     /* Retrieve screen info from underlying mdp */
     bool getScreenInfo(utils::ScreenInfo& info);
@@ -134,6 +137,9 @@
     /* sump the state of the obj */
     void dump() const;
 
+    /* Return the dump in the specified buffer */
+    void getDump(char *buf, size_t len);
+
 private:
     // mdp data struct
     MdpData mMdp;
@@ -213,6 +219,10 @@
     return mMdp.getDownscalefactor();
 }
 
+inline void Ctrl::getDump(char *buf, size_t len) {
+    mMdp.getDump(buf, len);
+}
+
 inline Data::Data() {
     mMdp.reset();
 }
@@ -249,6 +259,9 @@
     ALOGE("== Dump Data MDP end ==");
 }
 
+inline void Data::getDump(char *buf, size_t len) {
+    mMdp.getDump(buf, len);
+}
 
 } // overlay
 
diff --git a/liboverlay/overlayMdp.cpp b/liboverlay/overlayMdp.cpp
index 8bca85f..8b40ad7 100644
--- a/liboverlay/overlayMdp.cpp
+++ b/liboverlay/overlayMdp.cpp
@@ -278,6 +278,10 @@
     ALOGE("== Dump MdpCtrl end ==");
 }
 
+void MdpCtrl::getDump(char *buf, size_t len) {
+    ovutils::getDump(buf, len, "Ctrl(mdp_overlay)", mOVInfo);
+}
+
 void MdpData::dump() const {
     ALOGE("== Dump MdpData start ==");
     mFd.dump();
@@ -285,6 +289,10 @@
     ALOGE("== Dump MdpData end ==");
 }
 
+void MdpData::getDump(char *buf, size_t len) {
+    ovutils::getDump(buf, len, "Data(msmfb_overlay_data)", mOvData);
+}
+
 void MdpCtrl3D::dump() const {
     ALOGE("== Dump MdpCtrl start ==");
     mFd.dump();
diff --git a/liboverlay/overlayMdp.h b/liboverlay/overlayMdp.h
index c1684fc..55e2787 100644
--- a/liboverlay/overlayMdp.h
+++ b/liboverlay/overlayMdp.h
@@ -141,9 +141,6 @@
     /* using user_data, sets/unsets roationvalue in mdp flags */
     void setRotationFlags();
 
-    /* dump state of the object */
-    void dump() const;
-
     /* Perform transformation calculations */
     void doTransform();
 
@@ -156,6 +153,12 @@
     /* Update the src format */
     void updateSrcformat(const uint32_t& inputsrcFormat);
 
+    /* dump state of the object */
+    void dump() const;
+
+    /* Return the dump in the specified buffer */
+    void getDump(char *buf, size_t len);
+
 private:
 
     /* helper functions for overlayTransform */
@@ -237,6 +240,10 @@
 
     /* dump state of the object */
     void dump() const;
+
+    /* Return the dump in the specified buffer */
+    void getDump(char *buf, size_t len);
+
 private:
 
     /* actual overlay mdp data */
diff --git a/liboverlay/overlayMdpRot.cpp b/liboverlay/overlayMdpRot.cpp
index a802b4b..dc8ef09 100755
--- a/liboverlay/overlayMdpRot.cpp
+++ b/liboverlay/overlayMdpRot.cpp
@@ -272,4 +272,9 @@
     ALOGE("== Dump MdpRot end ==");
 }
 
+void MdpRot::getDump(char *buf, size_t len) const {
+    ovutils::getDump(buf, len, "MdpRotCtrl(msm_rotator_img_info)", mRotImgInfo);
+    ovutils::getDump(buf, len, "MdpRotData(msm_rotator_data_info)", mRotDataInfo);
+}
+
 } // namespace overlay
diff --git a/liboverlay/overlayMdssRot.cpp b/liboverlay/overlayMdssRot.cpp
index d9cdc03..0da653c 100644
--- a/liboverlay/overlayMdssRot.cpp
+++ b/liboverlay/overlayMdssRot.cpp
@@ -284,4 +284,10 @@
     if (mRotInfo.flags & utils::OV_MDP_SECURE_OVERLAY_SESSION)
         mBufSize = utils::align(mBufSize, SIZE_1M);
 }
+
+void MdssRot::getDump(char *buf, size_t len) const {
+    ovutils::getDump(buf, len, "MdssRotCtrl(mdp_overlay)", mRotInfo);
+    ovutils::getDump(buf, len, "MdssRotData(msmfb_overlay_data)", mRotData);
+}
+
 } // namespace overlay
diff --git a/liboverlay/overlayRotator.h b/liboverlay/overlayRotator.h
index f1a60e1..ee1610c 100644
--- a/liboverlay/overlayRotator.h
+++ b/liboverlay/overlayRotator.h
@@ -62,6 +62,7 @@
     virtual uint32_t getSessId() const = 0;
     virtual bool queueBuffer(int fd, uint32_t offset) = 0;
     virtual void dump() const = 0;
+    virtual void getDump(char *buf, size_t len) const = 0;
     static Rotator *getRotator();
 
 protected:
@@ -133,6 +134,7 @@
     virtual uint32_t getSessId() const;
     virtual bool queueBuffer(int fd, uint32_t offset);
     virtual void dump() const;
+    virtual void getDump(char *buf, size_t len) const;
 
 private:
     explicit MdpRot();
@@ -195,6 +197,7 @@
     virtual uint32_t getSessId() const;
     virtual bool queueBuffer(int fd, uint32_t offset);
     virtual void dump() const;
+    virtual void getDump(char *buf, size_t len) const;
 
 private:
     explicit MdssRot();
diff --git a/liboverlay/overlayUtils.cpp b/liboverlay/overlayUtils.cpp
index 9bffff0..3880976 100644
--- a/liboverlay/overlayUtils.cpp
+++ b/liboverlay/overlayUtils.cpp
@@ -335,6 +335,83 @@
     return (mdpVersion >= qdutils::MDSS_V5);
 }
 
+void getDump(char *buf, size_t len, const char *prefix,
+        const mdp_overlay& ov) {
+    char str[256] = {'\0'};
+    snprintf(str, 256,
+            "%s id=%d z=%d fg=%d alpha=%d mask=%d flags=0x%x\n",
+            prefix, ov.id, ov.z_order, ov.is_fg, ov.alpha,
+            ov.transp_mask, ov.flags);
+    strncat(buf, str, strlen(str));
+    getDump(buf, len, "\tsrc(msmfb_img)", ov.src);
+    getDump(buf, len, "\tsrc_rect(mdp_rect)", ov.src_rect);
+    getDump(buf, len, "\tdst_rect(mdp_rect)", ov.dst_rect);
+}
+
+void getDump(char *buf, size_t len, const char *prefix,
+        const msmfb_img& ov) {
+    char str_src[256] = {'\0'};
+    snprintf(str_src, 256,
+            "%s w=%d h=%d format=%d %s\n",
+            prefix, ov.width, ov.height, ov.format,
+            overlay::utils::getFormatString(ov.format));
+    strncat(buf, str_src, strlen(str_src));
+}
+
+void getDump(char *buf, size_t len, const char *prefix,
+        const mdp_rect& ov) {
+    char str_rect[256] = {'\0'};
+    snprintf(str_rect, 256,
+            "%s x=%d y=%d w=%d h=%d\n",
+            prefix, ov.x, ov.y, ov.w, ov.h);
+    strncat(buf, str_rect, strlen(str_rect));
+}
+
+void getDump(char *buf, size_t len, const char *prefix,
+        const msmfb_overlay_data& ov) {
+    char str[256] = {'\0'};
+    snprintf(str, 256,
+            "%s id=%d\n",
+            prefix, ov.id);
+    strncat(buf, str, strlen(str));
+    getDump(buf, len, "\tdata(msmfb_data)", ov.data);
+}
+
+void getDump(char *buf, size_t len, const char *prefix,
+        const msmfb_data& ov) {
+    char str_data[256] = {'\0'};
+    snprintf(str_data, 256,
+            "%s offset=%d memid=%d id=%d flags=0x%x priv=%d\n",
+            prefix, ov.offset, ov.memory_id, ov.id, ov.flags,
+            ov.priv);
+    strncat(buf, str_data, strlen(str_data));
+}
+
+void getDump(char *buf, size_t len, const char *prefix,
+        const msm_rotator_img_info& rot) {
+    char str[256] = {'\0'};
+    snprintf(str, 256, "%s sessid=%u rot=%d, enable=%d downscale=%d\n",
+            prefix, rot.session_id, rot.rotations, rot.enable,
+            rot.downscale_ratio);
+    strncat(buf, str, strlen(str));
+    getDump(buf, len, "\tsrc", rot.src);
+    getDump(buf, len, "\tdst", rot.dst);
+    getDump(buf, len, "\tsrc_rect", rot.src_rect);
+}
+
+void getDump(char *buf, size_t len, const char *prefix,
+        const msm_rotator_data_info& rot) {
+    char str[256] = {'\0'};
+    snprintf(str, 256,
+            "%s sessid=%u verkey=%d\n",
+            prefix, rot.session_id, rot.version_key);
+    strncat(buf, str, strlen(str));
+    getDump(buf, len, "\tsrc", rot.src);
+    getDump(buf, len, "\tdst", rot.dst);
+    getDump(buf, len, "\tsrc_chroma", rot.src_chroma);
+    getDump(buf, len, "\tdst_chroma", rot.dst_chroma);
+}
+
 } // utils
 
 } // overlay
diff --git a/liboverlay/overlayUtils.h b/liboverlay/overlayUtils.h
index 2f83374..946748e 100644
--- a/liboverlay/overlayUtils.h
+++ b/liboverlay/overlayUtils.h
@@ -36,6 +36,7 @@
 #include <hardware/hardware.h>
 #include <hardware/gralloc.h> // buffer_handle_t
 #include <linux/msm_mdp.h> // flags
+#include <linux/msm_rotator.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -774,6 +775,17 @@
     return OV_MDP_PIPE_ANY;
 }
 
+void getDump(char *buf, size_t len, const char *prefix, const mdp_overlay& ov);
+void getDump(char *buf, size_t len, const char *prefix, const msmfb_img& ov);
+void getDump(char *buf, size_t len, const char *prefix, const mdp_rect& ov);
+void getDump(char *buf, size_t len, const char *prefix,
+        const msmfb_overlay_data& ov);
+void getDump(char *buf, size_t len, const char *prefix, const msmfb_data& ov);
+void getDump(char *buf, size_t len, const char *prefix,
+        const msm_rotator_img_info& ov);
+void getDump(char *buf, size_t len, const char *prefix,
+        const msm_rotator_data_info& ov);
+
 } // namespace utils ends
 
 //--------------------Class Res stuff (namespace overlay only) -----------
diff --git a/liboverlay/pipes/overlayGenPipe.cpp b/liboverlay/pipes/overlayGenPipe.cpp
index 0b864df..486cfda 100644
--- a/liboverlay/pipes/overlayGenPipe.cpp
+++ b/liboverlay/pipes/overlayGenPipe.cpp
@@ -241,6 +241,13 @@
     ALOGE("== Dump Generic pipe end ==");
 }
 
+void GenericPipe::getDump(char *buf, size_t len) {
+    mCtrlData.ctrl.getDump(buf, len);
+    mCtrlData.data.getDump(buf, len);
+    if(mRotUsed && mRot)
+        mRot->getDump(buf, len);
+}
+
 bool GenericPipe::isClosed() const  {
     return (pipeState == CLOSED);
 }
diff --git a/liboverlay/pipes/overlayGenPipe.h b/liboverlay/pipes/overlayGenPipe.h
index 604529a..1d1be25 100644
--- a/liboverlay/pipes/overlayGenPipe.h
+++ b/liboverlay/pipes/overlayGenPipe.h
@@ -84,6 +84,10 @@
 
     /* dump the state of the object */
     void dump() const;
+
+    /* Return the dump in the specified buffer */
+    void getDump(char *buf, size_t len);
+
 private:
     /* set Closed pipe */
     bool setClosed();