7 #include <tbm_surface.h>
8 #include <tbm_surface_internal.h>
13 #include "tdm_private.h"
17 static const char *dump_prefix[2] = {"png", "yuv"};
20 tdm_helper_get_time_in_millis(void)
24 if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
25 return (tp.tv_sec * 1000) + (tp.tv_nsec / 1000000L);
31 _tdm_helper_dump_raw(const char *file, void *data1, int size1, void *data2,
32 int size2, void *data3, int size3)
35 FILE *fp = fopen(file, "w+");
36 TDM_RETURN_IF_FAIL(fp != NULL);
38 blocks = (unsigned int *)data1;
39 fwrite(blocks, 1, size1, fp);
42 blocks = (unsigned int *)data2;
43 fwrite(blocks, 1, size2, fp);
47 blocks = (unsigned int *)data3;
48 fwrite(blocks, 1, size3, fp);
55 _tdm_helper_dump_png(const char *file, const void *data, int width,
58 FILE *fp = fopen(file, "wb");
59 TDM_RETURN_IF_FAIL(fp != NULL);
61 png_structp pPngStruct =
62 png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
68 png_infop pPngInfo = png_create_info_struct(pPngStruct);
70 png_destroy_write_struct(&pPngStruct, NULL);
75 png_init_io(pPngStruct, fp);
76 png_set_IHDR(pPngStruct,
83 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
85 png_set_bgr(pPngStruct);
86 png_write_info(pPngStruct, pPngInfo);
88 const int pixel_size = 4; // RGBA
89 png_bytep *row_pointers =
90 png_malloc(pPngStruct, height * sizeof(png_byte *));
92 unsigned int *blocks = (unsigned int *)data;
96 for (; y < height; ++y) {
98 png_malloc(pPngStruct, sizeof(png_byte) * width * pixel_size);
99 row_pointers[y] = (png_bytep)row;
100 for (x = 0; x < width; ++x) {
101 unsigned int curBlock = blocks[y * width + x];
102 row[x * pixel_size] = (curBlock & 0xFF);
103 row[1 + x * pixel_size] = (curBlock >> 8) & 0xFF;
104 row[2 + x * pixel_size] = (curBlock >> 16) & 0xFF;
105 row[3 + x * pixel_size] = (curBlock >> 24) & 0xFF;
109 png_write_image(pPngStruct, row_pointers);
110 png_write_end(pPngStruct, pPngInfo);
112 for (y = 0; y < height; y++)
113 png_free(pPngStruct, row_pointers[y]);
114 png_free(pPngStruct, row_pointers);
116 png_destroy_write_struct(&pPngStruct, &pPngInfo);
122 tdm_helper_dump_buffer(tbm_surface_h buffer, const char *file)
124 tbm_surface_info_s info;
128 TDM_RETURN_IF_FAIL(buffer != NULL);
129 TDM_RETURN_IF_FAIL(file != NULL);
131 ret = tbm_surface_map(buffer, TBM_DEVICE_CPU, &info);
132 TDM_RETURN_IF_FAIL(ret == TBM_SURFACE_ERROR_NONE);
134 len = strnlen(file, 1024);
135 if (info.format == TBM_FORMAT_ARGB8888 || info.format == TBM_FORMAT_XRGB8888)
136 prefix = dump_prefix[0];
138 prefix = dump_prefix[1];
140 if (strncmp(file + (len - 3), prefix, 3)) {
141 TDM_ERR("can't dump to '%s' file", file + (len - 3));
142 tbm_surface_unmap(buffer);
146 switch (info.format) {
147 case TBM_FORMAT_ARGB8888:
148 case TBM_FORMAT_XRGB8888:
149 _tdm_helper_dump_png(file, info.planes[0].ptr,
150 info.planes[0].stride >> 2, info.height);
152 case TBM_FORMAT_YVU420:
153 case TBM_FORMAT_YUV420:
154 _tdm_helper_dump_raw(file,
156 info.planes[0].stride * info.height,
158 info.planes[1].stride * (info.height >> 1),
160 info.planes[2].stride * (info.height >> 1));
162 case TBM_FORMAT_NV12:
163 case TBM_FORMAT_NV21:
164 _tdm_helper_dump_raw(file,
166 info.planes[0].stride * info.height,
168 info.planes[1].stride * (info.height >> 1), NULL,
171 case TBM_FORMAT_YUYV:
172 case TBM_FORMAT_UYVY:
173 _tdm_helper_dump_raw(file,
175 info.planes[0].stride * info.height, NULL, 0,
179 TDM_ERR("can't dump %c%c%c%c buffer", FOURCC_STR (info.format));
180 tbm_surface_unmap(buffer);
184 tbm_surface_unmap(buffer);
186 TDM_INFO("dump %s", file);
190 tdm_helper_get_fd(const char *env)
193 int fd, newfd, flags, ret;
195 value = (const char*)getenv(env);
199 ret = sscanf(value, "%d", &fd);
201 TDM_ERR("sscanf failed: %m");
205 flags = fcntl(fd, F_GETFD);
207 TDM_ERR("fcntl failed: %m");
213 TDM_ERR("dup failed: %m");
217 TDM_INFO("%s: fd(%d) newfd(%d)", env, fd, newfd);
219 fcntl(newfd, F_SETFD, flags | FD_CLOEXEC);
225 tdm_helper_set_fd(const char *env, int fd)
230 snprintf(buf, sizeof(buf), "%d", fd);
232 ret = setenv(env, (const char*)buf, 1);
234 TDM_ERR("setenv failed: %m");
239 TDM_INFO("%s: fd(%d)", env, fd);