gralloc1: Add Dump() in buffer manager

CRs-Fixed: 2016226
Change-Id: Ia09de175b6b0c837ae9d66deb6f08615a7cf6baa
diff --git a/libgralloc1/gr_buf_mgr.cpp b/libgralloc1/gr_buf_mgr.cpp
index 51166d8..eefb17a 100644
--- a/libgralloc1/gr_buf_mgr.cpp
+++ b/libgralloc1/gr_buf_mgr.cpp
@@ -17,8 +17,10 @@
  * limitations under the License.
  */
 
+#include <iomanip>
 #include <utility>
 #include <vector>
+#include <sstream>
 
 #include "qd_utils.h"
 #include "gr_priv_handle.h"
@@ -803,4 +805,26 @@
   layout->planes[2].v_increment = static_cast<int32_t>(ycbcr.cstride);
   return GRALLOC1_ERROR_NONE;
 }
+
+gralloc1_error_t BufferManager::Dump(std::ostringstream *os) {
+  for (auto it : handles_map_) {
+    auto buf = it.second;
+    auto hnd = buf->handle;
+    *os << "handle id: " << std::setw(4) << hnd->id;
+    *os << " fd: "       << std::setw(3) << hnd->fd;
+    *os << " fd_meta: "  << std::setw(3) << hnd->fd_metadata;
+    *os << " wxh: "      << std::setw(4) << hnd->width <<" x " << std::setw(4) <<  hnd->height;
+    *os << " uwxuh: "    << std::setw(4) << hnd->unaligned_width << " x ";
+    *os << std::setw(4)  <<  hnd->unaligned_height;
+    *os << " size: "     << std::setw(9) << hnd->size;
+    *os << std::hex << std::setfill('0');
+    *os << " priv_flags: " << "0x" << std::setw(8) << hnd->flags;
+    *os << " prod_usage: " << "0x" << std::setw(8) << hnd->producer_usage;
+    *os << " cons_usage: " << "0x" << std::setw(8) << hnd->consumer_usage;
+    // TODO(user): get format string from qdutils
+    *os << " format: "     << "0x" << std::setw(8) << hnd->format;
+    *os << std::dec  << std::setfill(' ') << std::endl;
+  }
+  return GRALLOC1_ERROR_NONE;
+}
 }  //  namespace gralloc1
diff --git a/libgralloc1/gr_buf_mgr.h b/libgralloc1/gr_buf_mgr.h
index 08ff2ef..da048d2 100644
--- a/libgralloc1/gr_buf_mgr.h
+++ b/libgralloc1/gr_buf_mgr.h
@@ -48,6 +48,7 @@
   gralloc1_error_t Perform(int operation, va_list args);
   gralloc1_error_t GetFlexLayout(const private_handle_t *hnd, struct android_flex_layout *layout);
   gralloc1_error_t GetNumFlexPlanes(const private_handle_t *hnd, uint32_t *out_num_planes);
+  gralloc1_error_t Dump(std::ostringstream *os);
 
   template <typename... Args>
   gralloc1_error_t CallBufferDescriptorFunction(gralloc1_buffer_descriptor_t descriptor_id,
diff --git a/libgralloc1/gr_device_impl.cpp b/libgralloc1/gr_device_impl.cpp
index 2bb66d1..fc0b35d 100644
--- a/libgralloc1/gr_device_impl.cpp
+++ b/libgralloc1/gr_device_impl.cpp
@@ -29,6 +29,7 @@
 
 #include <cutils/log.h>
 #include <sync/sync.h>
+#include <algorithm>
 #include <sstream>
 #include <string>
 
@@ -172,17 +173,19 @@
     ALOGE("Gralloc Error : device=%p", (void *)device);
     return GRALLOC1_ERROR_BAD_DESCRIPTOR;
   }
+  const size_t max_dump_size = 8192;
   if (out_buffer == nullptr) {
-    *out_size = 1024;
+    *out_size = max_dump_size;
   } else {
     std::ostringstream os;
-    // TODO(user): implement in buffer manager
     os << "-------------------------------" << std::endl;
     os << "QTI gralloc dump:" << std::endl;
     os << "-------------------------------" << std::endl;
-    auto copy_size = os.str().size() < *out_size ? os.str().size() : *out_size;
-    std::copy_n(out_buffer, copy_size, os.str().begin());
-    *out_size = static_cast<uint32_t>(copy_size);
+    GrallocImpl const *dev = GRALLOC_IMPL(device);
+    dev->buf_mgr_->Dump(&os);
+    os << "-------------------------------" << std::endl;
+    auto copied = os.str().copy(out_buffer, std::min(os.str().size(), max_dump_size), 0);
+    *out_size = UINT(copied);
   }
 
   return GRALLOC1_ERROR_NONE;