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"};
19 static int *tdm_helper_dump_count;
20 static char *tdm_helper_dump_path;
23 tdm_helper_unlock_in_cb(tdm_private_display *private_display)
25 int ret = pthread_mutex_trylock(&private_display->lock);
29 _pthread_mutex_unlock(&private_display->lock);
30 else if (ret == EBUSY) {
31 _pthread_mutex_unlock(&private_display->lock);
39 tdm_helper_lock_in_cb(tdm_private_display *private_display, int need_lock)
44 _pthread_mutex_lock(&private_display->lock);
48 tdm_helper_get_time_in_millis(void)
52 if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
53 return (tp.tv_sec * 1000) + (tp.tv_nsec / 1000000L);
59 _tdm_helper_dump_raw(const char *file, void *data1, int size1, void *data2,
60 int size2, void *data3, int size3)
63 FILE *fp = fopen(file, "w+");
64 TDM_RETURN_IF_FAIL(fp != NULL);
66 blocks = (unsigned int *)data1;
67 fwrite(blocks, 1, size1, fp);
70 blocks = (unsigned int *)data2;
71 fwrite(blocks, 1, size2, fp);
75 blocks = (unsigned int *)data3;
76 fwrite(blocks, 1, size3, fp);
83 _tdm_helper_dump_png(const char *file, const void *data, int width,
86 FILE *fp = fopen(file, "wb");
87 TDM_RETURN_IF_FAIL(fp != NULL);
89 png_structp pPngStruct =
90 png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
96 png_infop pPngInfo = png_create_info_struct(pPngStruct);
98 png_destroy_write_struct(&pPngStruct, NULL);
103 png_init_io(pPngStruct, fp);
104 png_set_IHDR(pPngStruct,
111 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
113 png_set_bgr(pPngStruct);
114 png_write_info(pPngStruct, pPngInfo);
116 const int pixel_size = 4; // RGBA
117 png_bytep *row_pointers =
118 png_malloc(pPngStruct, height * sizeof(png_byte *));
120 unsigned int *blocks = (unsigned int *)data;
124 for (; y < height; ++y) {
126 png_malloc(pPngStruct, sizeof(png_byte) * width * pixel_size);
127 row_pointers[y] = (png_bytep)row;
128 for (x = 0; x < width; ++x) {
129 unsigned int curBlock = blocks[y * width + x];
130 row[x * pixel_size] = (curBlock & 0xFF);
131 row[1 + x * pixel_size] = (curBlock >> 8) & 0xFF;
132 row[2 + x * pixel_size] = (curBlock >> 16) & 0xFF;
133 row[3 + x * pixel_size] = (curBlock >> 24) & 0xFF;
137 png_write_image(pPngStruct, row_pointers);
138 png_write_end(pPngStruct, pPngInfo);
140 for (y = 0; y < height; y++)
141 png_free(pPngStruct, row_pointers[y]);
142 png_free(pPngStruct, row_pointers);
144 png_destroy_write_struct(&pPngStruct, &pPngInfo);
150 tdm_helper_dump_buffer(tbm_surface_h buffer, const char *file)
152 tbm_surface_info_s info;
156 TDM_RETURN_IF_FAIL(buffer != NULL);
157 TDM_RETURN_IF_FAIL(file != NULL);
159 ret = tbm_surface_map(buffer, TBM_DEVICE_CPU, &info);
160 TDM_RETURN_IF_FAIL(ret == TBM_SURFACE_ERROR_NONE);
162 len = strnlen(file, 1024);
163 if (info.format == TBM_FORMAT_ARGB8888 || info.format == TBM_FORMAT_XRGB8888)
164 prefix = dump_prefix[0];
166 prefix = dump_prefix[1];
168 if (strncmp(file + (len - 3), prefix, 3)) {
169 TDM_ERR("can't dump to '%s' file", file + (len - 3));
170 tbm_surface_unmap(buffer);
174 switch (info.format) {
175 case TBM_FORMAT_ARGB8888:
176 case TBM_FORMAT_XRGB8888:
177 _tdm_helper_dump_png(file, info.planes[0].ptr,
178 info.planes[0].stride >> 2, info.height);
180 case TBM_FORMAT_YVU420:
181 case TBM_FORMAT_YUV420:
182 _tdm_helper_dump_raw(file,
184 info.planes[0].stride * info.height,
186 info.planes[1].stride * (info.height >> 1),
188 info.planes[2].stride * (info.height >> 1));
190 case TBM_FORMAT_NV12:
191 case TBM_FORMAT_NV21:
192 _tdm_helper_dump_raw(file,
194 info.planes[0].stride * info.height,
196 info.planes[1].stride * (info.height >> 1), NULL,
199 case TBM_FORMAT_YUYV:
200 case TBM_FORMAT_UYVY:
201 _tdm_helper_dump_raw(file,
203 info.planes[0].stride * info.height, NULL, 0,
207 TDM_ERR("can't dump %c%c%c%c buffer", FOURCC_STR (info.format));
208 tbm_surface_unmap(buffer);
212 tbm_surface_unmap(buffer);
214 TDM_INFO("dump %s", file);
218 tdm_helper_get_fd(const char *env)
221 int fd, newfd, flags, ret;
223 value = (const char*)getenv(env);
227 ret = sscanf(value, "%d", &fd);
229 TDM_ERR("sscanf failed: %m");
233 flags = fcntl(fd, F_GETFD);
235 TDM_ERR("fcntl failed: %m");
241 TDM_ERR("dup failed: %m");
245 TDM_INFO("%s: fd(%d) newfd(%d)", env, fd, newfd);
247 fcntl(newfd, F_SETFD, flags | FD_CLOEXEC);
253 tdm_helper_set_fd(const char *env, int fd)
258 snprintf(buf, sizeof(buf), "%d", fd);
260 ret = setenv(env, (const char*)buf, 1);
262 TDM_ERR("setenv failed: %m");
267 TDM_INFO("%s: fd(%d)", env, fd);
271 tdm_helper_dump_start(char *dumppath, int *count)
273 if (tdm_helper_dump_count != NULL) {
274 TDM_DBG("tdm_helper_dump is already started.");
278 if (dumppath == NULL) {
279 TDM_DBG("tdm_helper_dump dumppath is null.");
283 tdm_helper_dump_count = count;
284 tdm_helper_dump_path = dumppath;
286 TDM_DBG("tdm_helper_dump start.(path : %s)", tdm_helper_dump_path);
290 tdm_helper_dump_stop(void)
292 tdm_helper_dump_path = NULL;
293 tdm_helper_dump_count = NULL;
295 TDM_DBG("tdm_helper_dump stop.");
299 tdm_helper_get_dump_count(void)
301 if ((tdm_helper_dump_count != NULL) && (tdm_helper_dump_path != NULL)) {
302 if (*tdm_helper_dump_count == 1000)
303 *tdm_helper_dump_count = 1;
304 return (*tdm_helper_dump_count)++;
310 tdm_helper_get_dump_path(void)
312 return tdm_helper_dump_path;