add dump function for debugging 06/57406/2
authorBoram Park <boram1288.park@samsung.com>
Wed, 20 Jan 2016 01:06:04 +0000 (10:06 +0900)
committerBoram Park <boram1288.park@samsung.com>
Wed, 20 Jan 2016 08:46:31 +0000 (17:46 +0900)
Change-Id: Ifb2abab9390a850e1224c38c3ae693a0ec1b1815

configure.ac
include/tdm_helper.h
packaging/libtdm.spec
src/tdm_helper.c

index 9cf040d..284ef1a 100644 (file)
@@ -23,7 +23,7 @@ LT_INIT([disable-static])
 # Enable quiet compiles on automake 1.11.
 m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])])
 
-PKG_CHECK_MODULES(TDM, libtbm pthread-stubs)
+PKG_CHECK_MODULES(TDM, libtbm pthread-stubs libpng)
 AC_SUBST(TDM_CFLAGS)
 AC_SUBST(TDM_LIBS)
 
index 245b7b0..d17c835 100644 (file)
@@ -37,6 +37,7 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 #define _TDM_HELPER_H_
 
 #include "tdm_types.h"
+#include <tbm_surface.h>
 
 #ifdef __cplusplus
 extern "C" {
@@ -100,6 +101,25 @@ extern "C" {
  */
 extern int tdm_helper_drm_fd;
 
+/**
+ * @brief Dump a buffer
+ * @details
+ * This function supports only if a buffer has below formats.
+ * - TBM_FORMAT_ARGB8888
+ * - TBM_FORMAT_XRGB8888
+ * - TBM_FORMAT_YVU420
+ * - TBM_FORMAT_YUV420
+ * - TBM_FORMAT_NV12
+ * - TBM_FORMAT_NV21
+ * - TBM_FORMAT_YUYV
+ * - TBM_FORMAT_UYVY
+ * The filename extension should be "png" for TBM_FORMAT_ARGB8888 and TBM_FORMAT_XRGB8888
+ * or "yuv" for YUV formats.
+ * @param[in] buffer A TDM buffer
+ * @param[in] file The path of file.
+ */
+void tdm_helper_dump_buffer(tbm_surface_h buffer, const char *file);
+
 #ifdef __cplusplus
 }
 #endif
index e505c35..bd6ba7f 100644 (file)
@@ -8,6 +8,7 @@ Source0:        %{name}-%{version}.tar.gz
 Source1001:        %{name}.manifest
 BuildRequires:  pkgconfig(pthread-stubs)
 BuildRequires:  pkgconfig(libtbm)
+BuildRequires:  pkgconfig(libpng)
 
 %description
 Common user library of Tizen Display Manager : libtdm front-end library
index 46fd6ab..b99abe7 100644 (file)
@@ -2,7 +2,179 @@
 #include "config.h"
 #endif
 
-#include "tdm_helper.h"
+#include <png.h>
+#include <string.h>
+#include <tbm_surface.h>
+#include <tbm_surface_internal.h>
+#include <string.h>
+
+#include "tdm.h"
+#include "tdm_private.h"
+
+#define PNG_DEPTH 8
 
 int tdm_helper_drm_fd = -1;
 
+static const char *dump_prefix[2] = {"png", "yuv"};
+
+static void
+_tdm_helper_dump_raw(const char *file, void *data1, int size1, void *data2,
+                     int size2, void *data3, int size3)
+{
+    unsigned int *blocks;
+    FILE *fp = fopen(file, "w+");
+    TDM_RETURN_IF_FAIL(fp != NULL);
+
+    blocks = (unsigned int*)data1;
+    fwrite(blocks, 1, size1, fp);
+
+    if (size2 > 0)
+    {
+        blocks = (unsigned int*)data2;
+        fwrite(blocks, 1, size2, fp);
+    }
+
+    if (size3 > 0)
+    {
+        blocks = (unsigned int*)data3;
+        fwrite(blocks, 1, size3, fp);
+    }
+
+    fclose(fp);
+}
+
+static void
+_tdm_helper_dump_png(const char *file, const void *data, int width,
+                     int height)
+{
+    FILE *fp = fopen(file, "wb");
+    TDM_RETURN_IF_FAIL(fp != NULL);
+
+    png_structp pPngStruct =
+        png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+    if (!pPngStruct)
+    {
+        fclose(fp);
+        return;
+    }
+
+    png_infop pPngInfo = png_create_info_struct(pPngStruct);
+    if (!pPngInfo)
+    {
+        png_destroy_write_struct(&pPngStruct, NULL);
+        fclose(fp);
+        return;
+    }
+
+    png_init_io(pPngStruct, fp);
+    png_set_IHDR(pPngStruct,
+                 pPngInfo,
+                 width,
+                 height,
+                 PNG_DEPTH,
+                 PNG_COLOR_TYPE_RGBA,
+                 PNG_INTERLACE_NONE,
+                 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
+
+    png_set_bgr(pPngStruct);
+    png_write_info(pPngStruct, pPngInfo);
+
+    const int pixel_size = 4;  // RGBA
+    png_bytep *row_pointers =
+        png_malloc(pPngStruct, height * sizeof(png_byte*));
+
+    unsigned int *blocks = (unsigned int*)data;
+    int y = 0;
+    int x = 0;
+
+    for (; y < height; ++y)
+    {
+        png_bytep row =
+            png_malloc(pPngStruct, sizeof(png_byte) * width * pixel_size);
+        row_pointers[y] = (png_bytep)row;
+        for (x = 0; x < width; ++x)
+        {
+            unsigned int curBlock = blocks[y * width + x];
+            row[x * pixel_size] = (curBlock & 0xFF);
+            row[1 + x * pixel_size] = (curBlock >> 8) & 0xFF;
+            row[2 + x * pixel_size] = (curBlock >> 16) & 0xFF;
+            row[3 + x * pixel_size] = (curBlock >> 24) & 0xFF;
+        }
+    }
+
+    png_write_image(pPngStruct, row_pointers);
+    png_write_end(pPngStruct, pPngInfo);
+
+    for (y = 0; y < height; y++)
+        png_free(pPngStruct, row_pointers[y]);
+    png_free(pPngStruct, row_pointers);
+
+    png_destroy_write_struct(&pPngStruct, &pPngInfo);
+
+    fclose(fp);
+}
+
+EXTERN void
+tdm_helper_dump_buffer(tbm_surface_h buffer, const char *file)
+{
+    tbm_surface_info_s info;
+    int len;
+    const char *prefix;
+
+    TDM_RETURN_IF_FAIL(buffer != NULL);
+    TDM_RETURN_IF_FAIL(file != NULL);
+
+    tbm_surface_get_info(buffer, &info);
+
+    len = strnlen(file, 1024);
+    if (info.format == TBM_FORMAT_ARGB8888 || info.format == TBM_FORMAT_XRGB8888)
+        prefix = dump_prefix[0];
+    else
+        prefix = dump_prefix[1];
+
+    if (strncmp(file + (len - 3), prefix, 3))
+    {
+        TDM_ERR("can't dump to '%s' file", file + (len - 3));
+        return;
+    }
+
+    switch(info.format)
+    {
+    case TBM_FORMAT_ARGB8888:
+    case TBM_FORMAT_XRGB8888:
+        _tdm_helper_dump_png(file, info.planes[0].ptr,
+                             info.planes[0].stride >> 2, info.height);
+        break;
+    case TBM_FORMAT_YVU420:
+    case TBM_FORMAT_YUV420:
+        _tdm_helper_dump_raw(file,
+                             info.planes[0].ptr + info.planes[0].offset,
+                             info.planes[0].stride * info.height,
+                             info.planes[1].ptr + info.planes[1].offset,
+                             info.planes[1].stride * (info.height >> 1),
+                             info.planes[2].ptr + info.planes[2].offset,
+                             info.planes[2].stride * (info.height >> 1));
+        break;
+    case TBM_FORMAT_NV12:
+    case TBM_FORMAT_NV21:
+        _tdm_helper_dump_raw(file,
+                             info.planes[0].ptr + info.planes[0].offset,
+                             info.planes[0].stride * info.height,
+                             info.planes[1].ptr + info.planes[1].offset,
+                             info.planes[1].stride * (info.height >> 1), NULL,
+                             0);
+        break;
+    case TBM_FORMAT_YUYV:
+    case TBM_FORMAT_UYVY:
+        _tdm_helper_dump_raw(file,
+                             info.planes[0].ptr + info.planes[0].offset,
+                             info.planes[0].stride * info.height, NULL, 0,
+                             NULL, 0);
+        break;
+    default:
+        TDM_ERR("can't dump %c%c%c%c buffer", FOURCC_STR (info.format));
+        return;
+    }
+
+    TDM_INFO("dump %s", file);
+}