make sure the directory path is not null for dump
[platform/core/uifw/libtdm.git] / src / tdm_helper.c
1 /**************************************************************************
2  *
3  * libtdm
4  *
5  * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
6  *
7  * Contact: Eunchul Kim <chulspro.kim@samsung.com>,
8  *          JinYoung Jeon <jy0.jeon@samsung.com>,
9  *          Taeheon Kim <th908.kim@samsung.com>,
10  *          YoungJun Cho <yj44.cho@samsung.com>,
11  *          SooChan Lim <sc1.lim@samsung.com>,
12  *          Boram Park <sc1.lim@samsung.com>
13  *
14  * Permission is hereby granted, free of charge, to any person obtaining a
15  * copy of this software and associated documentation files (the
16  * "Software"), to deal in the Software without restriction, including
17  * without limitation the rights to use, copy, modify, merge, publish,
18  * distribute, sub license, and/or sell copies of the Software, and to
19  * permit persons to whom the Software is furnished to do so, subject to
20  * the following conditions:
21  *
22  * The above copyright notice and this permission notice (including the
23  * next paragraph) shall be included in all copies or substantial portions
24  * of the Software.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
27  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
29  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
30  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
31  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
32  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33  *
34 **************************************************************************/
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include <png.h>
41 #include <string.h>
42 #include <tbm_surface.h>
43 #include <tbm_surface_internal.h>
44 #include <string.h>
45 #include <time.h>
46 #include <pixman.h>
47
48 #include "tdm.h"
49 #include "tdm_private.h"
50 #include "tdm_helper.h"
51
52 #define PNG_DEPTH 8
53
54 static const char *file_exts[2] = {"png", "yuv"};
55
56 int tdm_dump_enable;
57 char *tdm_debug_dump_dir;
58
59 INTERN unsigned long
60 tdm_helper_get_time_in_millis(void)
61 {
62         struct timespec tp;
63
64         if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
65                 return (tp.tv_sec * 1000) + (tp.tv_nsec / 1000000L);
66
67         return 0;
68 }
69
70 INTERN unsigned long
71 tdm_helper_get_time_in_micros(void)
72 {
73         struct timespec tp;
74
75         if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
76                 return (tp.tv_sec * 1000000) + (tp.tv_nsec / 1000L);
77
78         return 0;
79 }
80
81 static void
82 _tdm_helper_dump_raw(const char *file, void *data1, int size1, void *data2,
83                                          int size2, void *data3, int size3)
84 {
85         unsigned int *blocks;
86         FILE *fp = fopen(file, "w+");
87         TDM_RETURN_IF_FAIL(fp != NULL);
88
89         blocks = (unsigned int *)data1;
90         fwrite(blocks, 1, size1, fp);
91
92         if (size2 > 0) {
93                 blocks = (unsigned int *)data2;
94                 fwrite(blocks, 1, size2, fp);
95         }
96
97         if (size3 > 0) {
98                 blocks = (unsigned int *)data3;
99                 fwrite(blocks, 1, size3, fp);
100         }
101
102         fclose(fp);
103 }
104
105 static void
106 _tdm_helper_dump_png(const char *file, const void *data, int width,
107                                          int height)
108 {
109         FILE *fp = fopen(file, "wb");
110         TDM_RETURN_IF_FAIL(fp != NULL);
111
112         png_structp pPngStruct =
113                 png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
114         if (!pPngStruct) {
115                 fclose(fp);
116                 return;
117         }
118
119         png_infop pPngInfo = png_create_info_struct(pPngStruct);
120         if (!pPngInfo) {
121                 png_destroy_write_struct(&pPngStruct, NULL);
122                 fclose(fp);
123                 return;
124         }
125
126         png_init_io(pPngStruct, fp);
127         png_set_IHDR(pPngStruct,
128                                  pPngInfo,
129                                  width,
130                                  height,
131                                  PNG_DEPTH,
132                                  PNG_COLOR_TYPE_RGBA,
133                                  PNG_INTERLACE_NONE,
134                                  PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
135
136         png_set_bgr(pPngStruct);
137         png_write_info(pPngStruct, pPngInfo);
138
139         const int pixel_size = 4;       // RGBA
140         png_bytep *row_pointers =
141                 png_malloc(pPngStruct, height * sizeof(png_byte *));
142
143         unsigned int *blocks = (unsigned int *)data;
144         int y = 0;
145         int x = 0;
146
147         for (; y < height; ++y) {
148                 png_bytep row =
149                         png_malloc(pPngStruct, sizeof(png_byte) * width * pixel_size);
150                 row_pointers[y] = (png_bytep)row;
151                 for (x = 0; x < width; ++x) {
152                         unsigned int curBlock = blocks[y * width + x];
153                         row[x * pixel_size] = (curBlock & 0xFF);
154                         row[1 + x * pixel_size] = (curBlock >> 8) & 0xFF;
155                         row[2 + x * pixel_size] = (curBlock >> 16) & 0xFF;
156                         row[3 + x * pixel_size] = (curBlock >> 24) & 0xFF;
157                 }
158         }
159
160         png_write_image(pPngStruct, row_pointers);
161         png_write_end(pPngStruct, pPngInfo);
162
163         for (y = 0; y < height; y++)
164                 png_free(pPngStruct, row_pointers[y]);
165         png_free(pPngStruct, row_pointers);
166
167         png_destroy_write_struct(&pPngStruct, &pPngInfo);
168
169         fclose(fp);
170 }
171
172 INTERN char *
173 tdm_helper_dump_make_directory(const char *path, char *reply, int *len)
174 {
175         char *fullpath = NULL;
176         time_t timer;
177         struct tm *t, *buf = NULL;
178
179         timer = time(NULL);
180
181         buf = calloc(1, sizeof(struct tm));
182         TDM_GOTO_IF_FAIL(buf != NULL, failed_make);
183
184         fullpath = calloc(1, TDM_PATH_LEN * sizeof(char));
185         TDM_GOTO_IF_FAIL(fullpath != NULL, failed_make);
186
187         t = localtime_r(&timer, buf);
188         TDM_GOTO_IF_FAIL(t != NULL, failed_make);
189
190         snprintf(fullpath, TDM_PATH_LEN, "%s/dump_%04d%02d%02d.%02d%02d%02d", path,
191                          t->tm_year+1900, t->tm_mon+1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
192
193         if ((mkdir(fullpath, 0755)) < 0) {
194                 TDM_ERR("mkdir '%s' fail\n", fullpath);
195                 TDM_SNPRINTF(reply, len, "mkdir '%s' fail\n", fullpath);
196                 goto failed_make;
197         }
198
199         free(buf);
200
201         return fullpath;
202 failed_make:
203         if (fullpath)
204                 free(fullpath);
205         if (buf)
206                 free(buf);
207         return NULL;
208 }
209
210 INTERN void
211 tdm_helper_dump_buffer_str(tbm_surface_h buffer, char *dir, char *str)
212 {
213         tbm_surface_info_s info;
214         const char *ext;
215         char file[TDM_PATH_LEN];
216         int ret, bw;
217
218         TDM_RETURN_IF_FAIL(buffer != NULL);
219         TDM_RETURN_IF_FAIL(dir != NULL);
220         TDM_RETURN_IF_FAIL(str != NULL);
221
222         ret = tbm_surface_get_info(buffer, &info);
223         TDM_RETURN_IF_FAIL(ret == TBM_SURFACE_ERROR_NONE);
224
225         if (info.format == TBM_FORMAT_ARGB8888 || info.format == TBM_FORMAT_XRGB8888) {
226                 ext = file_exts[0];
227                 bw = info.planes[0].stride >> 2;
228         } else {
229                 ext = file_exts[1];
230                 bw = info.planes[0].stride;
231         }
232
233         snprintf(file, TDM_PATH_LEN, "%s/%c%c%c%c_%dx%d_%s.%s",
234                          dir, FOURCC_STR(info.format), bw, info.height, str, ext);
235
236         tdm_helper_dump_buffer(buffer, file);
237 }
238
239 EXTERN void
240 tdm_helper_dump_buffer(tbm_surface_h buffer, const char *file)
241 {
242         tbm_surface_info_s info;
243         int len, ret;
244         const char *ext;
245
246         TDM_RETURN_IF_FAIL(buffer != NULL);
247         TDM_RETURN_IF_FAIL(file != NULL);
248
249         ret = tbm_surface_map(buffer, TBM_OPTION_READ, &info);
250         TDM_RETURN_IF_FAIL(ret == TBM_SURFACE_ERROR_NONE);
251
252         len = strnlen(file, 1024);
253         if (info.format == TBM_FORMAT_ARGB8888 || info.format == TBM_FORMAT_XRGB8888)
254                 ext = file_exts[0];
255         else
256                 ext = file_exts[1];
257
258         if (strncmp(file + (len - 3), ext, 3)) {
259                 TDM_ERR("can't dump to '%s' file", file + (len - 3));
260                 tbm_surface_unmap(buffer);
261                 return;
262         }
263
264         switch (info.format) {
265         case TBM_FORMAT_ARGB8888:
266         case TBM_FORMAT_XRGB8888:
267                 _tdm_helper_dump_png(file, info.planes[0].ptr,
268                                                          info.planes[0].stride >> 2, info.height);
269                 break;
270         case TBM_FORMAT_YVU420:
271         case TBM_FORMAT_YUV420:
272                 _tdm_helper_dump_raw(file,
273                                                          info.planes[0].ptr,
274                                                          info.planes[0].stride * info.height,
275                                                          info.planes[1].ptr,
276                                                          info.planes[1].stride * (info.height >> 1),
277                                                          info.planes[2].ptr,
278                                                          info.planes[2].stride * (info.height >> 1));
279                 break;
280         case TBM_FORMAT_NV12:
281         case TBM_FORMAT_NV21:
282                 _tdm_helper_dump_raw(file,
283                                                          info.planes[0].ptr,
284                                                          info.planes[0].stride * info.height,
285                                                          info.planes[1].ptr,
286                                                          info.planes[1].stride * (info.height >> 1), NULL,
287                                                          0);
288                 break;
289         case TBM_FORMAT_YUYV:
290         case TBM_FORMAT_UYVY:
291                 _tdm_helper_dump_raw(file,
292                                                          info.planes[0].ptr,
293                                                          info.planes[0].stride * info.height, NULL, 0,
294                                                          NULL, 0);
295                 break;
296         default:
297                 TDM_ERR("can't dump %c%c%c%c buffer", FOURCC_STR(info.format));
298                 tbm_surface_unmap(buffer);
299                 return;
300         }
301
302         tbm_surface_unmap(buffer);
303
304         TDM_INFO("dump %s", file);
305 }
306
307 EXTERN void
308 tdm_helper_clear_buffer(tbm_surface_h buffer)
309 {
310         tbm_surface_info_s info;
311         int ret;
312
313         TDM_RETURN_IF_FAIL(buffer != NULL);
314
315         ret = tbm_surface_map(buffer, TBM_OPTION_READ, &info);
316         TDM_RETURN_IF_FAIL(ret == TBM_SURFACE_ERROR_NONE);
317
318         switch (info.format) {
319         case TBM_FORMAT_ARGB8888:
320         case TBM_FORMAT_XRGB8888:
321                 memset(info.planes[0].ptr, 0, info.planes[0].stride * info.height);
322                 break;
323         case TBM_FORMAT_YVU420:
324         case TBM_FORMAT_YUV420:
325                 memset((char*)info.planes[0].ptr, 0x10, info.planes[0].stride * info.height);
326                 memset((char*)info.planes[1].ptr, 0x80, info.planes[1].stride * (info.height >> 1));
327                 memset((char*)info.planes[2].ptr, 0x80, info.planes[2].stride * (info.height >> 1));
328                 break;
329         case TBM_FORMAT_NV12:
330         case TBM_FORMAT_NV21:
331                 memset((char*)info.planes[0].ptr, 0x10, info.planes[0].stride * info.height);
332                 memset((char*)info.planes[1].ptr, 0x80, info.planes[1].stride * (info.height >> 1));
333                 break;
334         case TBM_FORMAT_YUYV: {
335                 int *ibuf = (int*)info.planes[0].ptr;
336                 int i, size = info.planes[0].stride * info.height / 4;
337
338                 for (i = 0 ; i < size ; i++)
339                         ibuf[i] = 0x10801080;
340         }
341         break;
342         case TBM_FORMAT_UYVY: {
343                 int *ibuf = (int*)info.planes[0].ptr;
344                 int i, size = info.planes[0].stride * info.height / 4;
345
346                 for (i = 0 ; i < size ; i++)
347                         ibuf[i] = 0x80108010; /* YUYV -> 0xVYUY */
348         }
349         break;
350         default:
351                 TDM_ERR("can't clear %c%c%c%c buffer", FOURCC_STR(info.format));
352                 break;
353         }
354
355         tbm_surface_unmap(buffer);
356 }
357
358 EXTERN int
359 tdm_helper_get_fd(const char *env)
360 {
361         const char *value;
362         int fd, newfd, flags, ret;
363
364         value = (const char*)getenv(env);
365         if (!value)
366                 return -1;
367
368         ret = sscanf(value, "%d", &fd);
369         if (ret < 0) {
370                 TDM_ERR("sscanf failed: %m");
371                 return -1;
372         }
373
374         flags = fcntl(fd, F_GETFD);
375         if (flags == -1) {
376                 TDM_ERR("fcntl failed: %m");
377                 return -1;
378         }
379
380         newfd = dup(fd);
381         if (newfd < 0) {
382                 TDM_ERR("dup failed: %m");
383                 return -1;
384         }
385
386         TDM_INFO("%s: fd(%d) newfd(%d)", env, fd, newfd);
387
388         fcntl(newfd, F_SETFD, flags | FD_CLOEXEC);
389
390         return newfd;
391 }
392
393 EXTERN void
394 tdm_helper_set_fd(const char *env, int fd)
395 {
396         char buf[32];
397         int ret;
398
399         snprintf(buf, sizeof(buf), "%d", fd);
400
401         ret = setenv(env, (const char*)buf, 1);
402         if (ret) {
403                 TDM_ERR("setenv failed: %m");
404                 return;
405         }
406
407         if (fd >= 0)
408                 TDM_INFO("%s: fd(%d)", env, fd);
409 }
410
411 EXTERN void
412 tdm_helper_dump_start(char *dumppath, int *count)
413 {
414         if (dumppath == NULL || count == NULL) {
415                 TDM_DBG("tdm_helper_dump dumppath or count is null.");
416                 return;
417         }
418
419         tdm_dump_enable = 1;
420
421         TDM_DBG("tdm_helper_dump start.(path : %s)", dumppath);
422 }
423
424 EXTERN void
425 tdm_helper_dump_stop(void)
426 {
427         tdm_dump_enable = 0;
428
429         TDM_DBG("tdm_helper_dump stop.");
430 }
431
432 static pixman_format_code_t
433 _tdm_helper_pixman_format_get(tbm_format format)
434 {
435         switch (format) {
436         case TBM_FORMAT_ARGB8888:
437                 return PIXMAN_a8r8g8b8;
438         case TBM_FORMAT_XRGB8888:
439                 return PIXMAN_x8r8g8b8;
440         default:
441                 return 0;
442         }
443
444         return 0;
445 }
446
447 static tdm_error
448 _tdm_helper_buffer_convert(tbm_surface_h srcbuf, tbm_surface_h dstbuf,
449                                                    int dx, int dy, int dw, int dh, int count)
450 {
451         pixman_image_t *src_img = NULL, *dst_img = NULL;
452         pixman_format_code_t src_format, dst_format;
453         pixman_transform_t t;
454         struct pixman_f_transform ft;
455         pixman_op_t op;
456         tbm_surface_info_s src_info = {0, };
457         tbm_surface_info_s dst_info = {0, };
458         int stride, width;
459         double scale_x, scale_y;
460
461         TDM_RETURN_VAL_IF_FAIL(srcbuf != NULL, TDM_ERROR_INVALID_PARAMETER);
462         TDM_RETURN_VAL_IF_FAIL(dstbuf != NULL, TDM_ERROR_INVALID_PARAMETER);
463
464         if (tbm_surface_map(srcbuf, TBM_SURF_OPTION_READ, &src_info)
465                         != TBM_SURFACE_ERROR_NONE) {
466                 TDM_ERR("cannot mmap srcbuf\n");
467                 return TDM_ERROR_OPERATION_FAILED;
468         }
469
470         if (tbm_surface_map(dstbuf, TBM_SURF_OPTION_WRITE, &dst_info)
471                         != TBM_SURFACE_ERROR_NONE) {
472                 TDM_ERR("cannot mmap dstbuf\n");
473                 tbm_surface_unmap(srcbuf);
474                 return TDM_ERROR_OPERATION_FAILED;
475         }
476         TDM_GOTO_IF_FAIL(src_info.num_planes == 1, cant_convert);
477         TDM_GOTO_IF_FAIL(dst_info.num_planes == 1, cant_convert);
478
479         /* src */
480         src_format = _tdm_helper_pixman_format_get(src_info.format);
481         TDM_GOTO_IF_FAIL(src_format > 0, cant_convert);
482
483         width = src_info.planes[0].stride / 4;
484         stride = src_info.planes[0].stride;
485         src_img = pixman_image_create_bits(src_format, width, src_info.height,
486                                                                            (uint32_t*)src_info.planes[0].ptr, stride);
487         TDM_GOTO_IF_FAIL(src_img != NULL, cant_convert);
488
489         /* dst */
490         dst_format = _tdm_helper_pixman_format_get(dst_info.format);
491         TDM_GOTO_IF_FAIL(dst_format > 0, cant_convert);
492
493         width = dst_info.planes[0].stride / 4;
494         stride = dst_info.planes[0].stride;
495         dst_img = pixman_image_create_bits(dst_format, width, dst_info.height,
496                                                                            (uint32_t*)dst_info.planes[0].ptr, stride);
497         TDM_GOTO_IF_FAIL(dst_img != NULL, cant_convert);
498
499         pixman_f_transform_init_identity(&ft);
500
501         scale_x = (double)src_info.width / dw;
502         scale_y = (double)src_info.height / dh;
503
504         pixman_f_transform_scale(&ft, NULL, scale_x, scale_y);
505         pixman_f_transform_translate(&ft, NULL, 0, 0);
506         pixman_transform_from_pixman_f_transform(&t, &ft);
507         pixman_image_set_transform(src_img, &t);
508
509         if (count == 0)
510                 op = PIXMAN_OP_SRC;
511         else
512                 op = PIXMAN_OP_OVER;
513
514         pixman_image_composite(op, src_img, NULL, dst_img,
515                                                    0, 0, 0, 0, dx, dy, dw, dh);
516
517         if (src_img)
518                 pixman_image_unref(src_img);
519         if (dst_img)
520                 pixman_image_unref(dst_img);
521
522         tbm_surface_unmap(srcbuf);
523         tbm_surface_unmap(dstbuf);
524
525         return TDM_ERROR_NONE;
526
527 cant_convert:
528         if (src_img)
529                 pixman_image_unref(src_img);
530
531         tbm_surface_unmap(srcbuf);
532         tbm_surface_unmap(dstbuf);
533
534         return TDM_ERROR_OPERATION_FAILED;
535 }
536
537 EXTERN tdm_error
538 tdm_helper_capture_output(tdm_output *output, tbm_surface_h dst_buffer,
539                                                   int x, int y, int w, int h,
540                                                   tdm_helper_capture_handler func, void *data)
541 {
542         tbm_surface_h surface;
543         tdm_error err;
544         int i, count, first = 0;
545
546         TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
547         TDM_RETURN_VAL_IF_FAIL(dst_buffer != NULL, TDM_ERROR_INVALID_PARAMETER);
548         TDM_RETURN_VAL_IF_FAIL(x >= 0, TDM_ERROR_INVALID_PARAMETER);
549         TDM_RETURN_VAL_IF_FAIL(y >= 0, TDM_ERROR_INVALID_PARAMETER);
550         TDM_RETURN_VAL_IF_FAIL(w >= 0, TDM_ERROR_INVALID_PARAMETER);
551         TDM_RETURN_VAL_IF_FAIL(h >= 0, TDM_ERROR_INVALID_PARAMETER);
552         TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
553         TDM_RETURN_VAL_IF_FAIL(data != NULL, TDM_ERROR_INVALID_PARAMETER);
554
555         err = tdm_output_get_layer_count(output, &count);
556         if (err != TDM_ERROR_NONE) {
557                 TDM_ERR("tdm_output_get_layer_count fail(%d)\n", err);
558                 return TDM_ERROR_OPERATION_FAILED;
559         }
560         if (count <= 0) {
561                 TDM_ERR("tdm_output_get_layer_count err(%d, %d)\n", err, count);
562                 return TDM_ERROR_BAD_MODULE;
563         }
564
565         for (i = count - 1; i >= 0; i--) {
566                 tdm_layer *layer = tdm_output_get_layer(output, i, NULL);
567
568                 surface = tdm_layer_get_displaying_buffer(layer, &err);
569                 if (err != TDM_ERROR_NONE)
570                         continue;
571
572                 err = _tdm_helper_buffer_convert(surface, dst_buffer, x, y, w, h, first++);
573                 if (err != TDM_ERROR_NONE)
574                         TDM_DBG("convert fail %d-layer buffer\n", i);
575                 else
576                         TDM_DBG("convert success %d-layer buffer\n", i);
577         }
578
579         func(dst_buffer, data);
580
581         return TDM_ERROR_NONE;
582 }
583
584 EXTERN void
585 tdm_helper_get_display_information(tdm_display *dpy, char *reply, int *len)
586 {
587         tdm_private_display *private_display;
588         tdm_backend_module *module_data;
589         tdm_func_output *func_output;
590         tdm_func_layer *func_layer;
591         tdm_private_output *private_output = NULL;
592         tdm_private_layer *private_layer = NULL;
593         tdm_private_pp *private_pp = NULL;
594         tdm_private_capture *private_capture = NULL;
595         tdm_error ret;
596         int i;
597
598         TDM_DBG_RETURN_IF_FAIL(dpy != NULL);
599
600         private_display = dpy;
601         func_output = &private_display->func_output;
602         func_layer = &private_display->func_layer;
603         _pthread_mutex_lock(&private_display->lock);
604
605         /* module information */
606         module_data = private_display->module_data;
607         TDM_SNPRINTF(reply, len, "[TDM backend information]\n");
608         TDM_SNPRINTF(reply, len, "name: %s\n", module_data->name);
609         TDM_SNPRINTF(reply, len, "vendor: %s\n", module_data->vendor);
610         TDM_SNPRINTF(reply, len, "version: %d.%d\n\n",
611                                  (int)TDM_BACKEND_GET_ABI_MAJOR(module_data->abi_version),
612                                  (int)TDM_BACKEND_GET_ABI_MINOR(module_data->abi_version));
613
614         /* output information */
615         TDM_SNPRINTF(reply, len, "[Output information]\n");
616         TDM_SNPRINTF(reply, len, "--------------------------------------------------------------------------------------------\n");
617         TDM_SNPRINTF(reply, len, "idx   maker   model   name   type   status   dpms   subpixel   align_w   min   max   phy(mm)\n");
618         TDM_SNPRINTF(reply, len, "--------------------------------------------------------------------------------------------\n");
619         LIST_FOR_EACH_ENTRY(private_output, &private_display->output_list, link) {
620                 TDM_SNPRINTF(reply, len, "%d   %s   %s   %s   %s   %s   %s   %d   %d   %dx%d   %dx%d   %dx%d\n",
621                                          private_output->index, private_output->caps.maker,
622                                          private_output->caps.model, private_output->caps.name,
623                                          tdm_conn_str(private_output->caps.type),
624                                          tdm_status_str(private_output->caps.status),
625                                          tdm_dpms_str(private_output->current_dpms_value),
626                                          private_output->caps.subpixel,
627                                          private_output->caps.preferred_align,
628                                          private_output->caps.min_w, private_output->caps.min_h,
629                                          private_output->caps.max_w, private_output->caps.max_h,
630                                          private_output->caps.mmWidth, private_output->caps.mmHeight);
631
632                 TDM_SNPRINTF(reply, len, "\t%d modes:\n", private_output->caps.mode_count);
633
634                 if (private_output->caps.mode_count > 0) {
635                         const tdm_output_mode *current_mode = NULL;
636
637                         TDM_DBG_GOTO_IF_FAIL(func_output->output_get_mode, unlock);
638                         ret = func_output->output_get_mode(private_output->output_backend, &current_mode);
639                         TDM_DBG_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, unlock);
640
641                         TDM_SNPRINTF(reply, len, "\t\t name refresh (Hz) clk hdisp hss hse htot vdisp vss vse vtot vscan\n");
642                         for (i = 0; i < private_output->caps.mode_count; i++) {
643                                 char *current = (current_mode == private_output->caps.modes + i) ? "*" : " ";
644                                 TDM_SNPRINTF(reply, len, "\t\t%s%s %d %d %d %d %d %d %d %d %d %d %d ",
645                                                          current,
646                                                          private_output->caps.modes[i].name,
647                                                          private_output->caps.modes[i].vrefresh,
648                                                          private_output->caps.modes[i].clock,
649                                                          private_output->caps.modes[i].hdisplay,
650                                                          private_output->caps.modes[i].hsync_start,
651                                                          private_output->caps.modes[i].hsync_end,
652                                                          private_output->caps.modes[i].htotal,
653                                                          private_output->caps.modes[i].vdisplay,
654                                                          private_output->caps.modes[i].vsync_start,
655                                                          private_output->caps.modes[i].vsync_end,
656                                                          private_output->caps.modes[i].vtotal,
657                                                          private_output->caps.modes[i].vscan);
658                                 tdm_mode_flag_str(private_output->caps.modes[i].flags, &reply, len);
659                                 TDM_SNPRINTF(reply, len, " ");
660                                 tdm_mode_type_str(private_output->caps.modes[i].type, &reply, len);
661                                 TDM_SNPRINTF(reply, len, "\n");
662                         }
663                 }
664
665                 TDM_SNPRINTF(reply, len, "\t%d properties:\n", private_output->caps.prop_count);
666                 if (private_output->caps.prop_count > 0) {
667                         TDM_SNPRINTF(reply, len, "\t\tname\tidx\tvalue\n");
668                         for (i = 0; i < private_output->caps.prop_count; i++) {
669                                 tdm_value value;
670                                 TDM_DBG_GOTO_IF_FAIL(func_output->output_get_property, unlock);
671                                 ret = func_output->output_get_property(private_output->output_backend,
672                                                                                                            private_output->caps.props[i].id,
673                                                                                                            &value);
674                                 TDM_DBG_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, unlock);
675                                 TDM_SNPRINTF(reply, len, "\t\t%s\t%d\t%d\n",
676                                                          private_output->caps.props[i].name,
677                                                          private_output->caps.props[i].id,
678                                                          value.u32);
679                         }
680                 }
681         }
682         TDM_SNPRINTF(reply, len, "\n");
683
684         /* layer information */
685         TDM_SNPRINTF(reply, len, "[Layer information]\n");
686         TDM_SNPRINTF(reply, len, "-----------------------------------------------------------------------\n");
687         TDM_SNPRINTF(reply, len, "idx   output   zpos   buf   format   size   crop   geometry   transform\n");
688         TDM_SNPRINTF(reply, len, "-----------------------------------------------------------------------\n");
689         LIST_FOR_EACH_ENTRY(private_output, &private_display->output_list, link) {
690                 LIST_FOR_EACH_ENTRY(private_layer, &private_output->layer_list, link) {
691                         if (!private_layer->usable) {
692                                 tdm_info_layer info;
693                                 unsigned int format;
694                                 tdm_size size;
695                                 tbm_surface_info_s buf_info;
696
697                                 TDM_DBG_GOTO_IF_FAIL(func_layer->layer_get_info, unlock);
698                                 memset(&info, 0, sizeof info);
699                                 ret = func_layer->layer_get_info(private_layer->layer_backend, &info);
700                                 TDM_DBG_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, unlock);
701
702                                 format = tbm_surface_get_format(private_layer->showing_buffer);
703                                 tbm_surface_get_info(private_layer->showing_buffer, &buf_info);
704
705                                 if (IS_RGB(format))
706                                         size.h = buf_info.planes[0].stride >> 2;
707                                 else
708                                         size.h = buf_info.planes[0].stride;
709                                 size.v = tbm_surface_get_height(private_layer->showing_buffer);
710
711                                 if (info.src_config.format)
712                                         format = (info.src_config.format)?:format;
713
714                                 TDM_SNPRINTF(reply, len, "%d   %d   %d   %p   %c%c%c%c   %dx%d   %dx%d+%d+%d   %dx%d+%d+%d   %s\n",
715                                                          private_layer->index,
716                                                          private_output->index,
717                                                          private_layer->caps.zpos,
718                                                          private_layer->showing_buffer, FOURCC_STR(format), size.h, size.v,
719                                                          info.src_config.pos.w, info.src_config.pos.h, info.src_config.pos.x, info.src_config.pos.y,
720                                                          info.dst_pos.w, info.dst_pos.h, info.dst_pos.x, info.dst_pos.y,
721                                                          tdm_transform_str(info.transform));
722                         } else {
723                                 TDM_SNPRINTF(reply, len, "%d   %d   %d   -\n",
724                                                          private_layer->index,
725                                                          private_output->index,
726                                                          private_layer->caps.zpos);
727                         }
728
729                         TDM_SNPRINTF(reply, len, "\tcaps\t: ");
730                         tdm_layer_caps_str(private_layer->caps.capabilities, &reply, len);
731                         TDM_SNPRINTF(reply, len, "\n");
732
733                         TDM_SNPRINTF(reply, len, "\tformats\t: ");
734                         if (private_layer->caps.format_count > 0) {
735                                 const char *sep = "";
736                                 for (i = 0; i < private_layer->caps.format_count; i++) {
737                                         if (private_layer->caps.formats[i] == 0)
738                                                 continue;
739                                         TDM_SNPRINTF(reply, len, "%s%c%c%c%c", sep, FOURCC_STR(private_layer->caps.formats[i]));
740                                         sep = ",";
741                                 }
742                                 TDM_SNPRINTF(reply, len, "\n");
743                         }
744
745                         TDM_SNPRINTF(reply, len, "\t%d properties:\n", private_layer->caps.prop_count);
746                         if (private_layer->caps.prop_count > 0) {
747                                 TDM_SNPRINTF(reply, len, "\t\tname\tidx\tvalue\n");
748                                 for (i = 0; i < private_layer->caps.prop_count; i++) {
749                                         tdm_value value;
750                                         TDM_DBG_GOTO_IF_FAIL(func_layer->layer_get_property, unlock);
751                                         ret = func_layer->layer_get_property(private_layer->layer_backend,
752                                                                                                                  private_layer->caps.props[i].id,
753                                                                                                                  &value);
754                                         TDM_DBG_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, unlock);
755                                         TDM_SNPRINTF(reply, len, "\t\t%s\t%d\t%d\n",
756                                                                  private_layer->caps.props[i].name,
757                                                                  private_layer->caps.props[i].id,
758                                                                  value.u32);
759                                 }
760                         }
761                 }
762         }
763         TDM_SNPRINTF(reply, len, "\n");
764
765         if (private_display->capabilities & TDM_DISPLAY_CAPABILITY_PP) {
766                 const char *sep = "";
767                 TDM_SNPRINTF(reply, len, "[PP information]\n");
768                 TDM_SNPRINTF(reply, len, "caps\t: ");
769                 tdm_pp_caps_str(private_display->caps_pp.capabilities, &reply, len);
770                 TDM_SNPRINTF(reply, len, "\n");
771                 TDM_SNPRINTF(reply, len, "formats\t: ");
772                 for (i = 0; i < private_display->caps_pp.format_count; i++) {
773                         if (private_display->caps_pp.formats[i] == 0)
774                                 continue;
775                         TDM_SNPRINTF(reply, len, "%s%c%c%c%c", sep, FOURCC_STR(private_display->caps_pp.formats[i]));
776                         sep = ",";
777                 }
778                 TDM_SNPRINTF(reply, len, "\n");
779                 TDM_SNPRINTF(reply, len, "size\t: min(%dx%d) max(%dx%d) align_w(%d)\n",
780                                          private_display->caps_pp.min_w, private_display->caps_pp.min_h,
781                                          private_display->caps_pp.max_w, private_display->caps_pp.max_h,
782                                          private_display->caps_pp.preferred_align);
783                 if (!LIST_IS_EMPTY(&private_display->pp_list)) {
784                         TDM_SNPRINTF(reply, len, "-------------------------------------------------------------\n");
785                         TDM_SNPRINTF(reply, len, "src(format size crop)  |  dst(format size crop)  |  transform\n");
786                         TDM_SNPRINTF(reply, len, "-------------------------------------------------------------\n");
787                         LIST_FOR_EACH_ENTRY(private_pp, &private_display->pp_list, link) {
788                                 TDM_SNPRINTF(reply, len, "%c%c%c%c %dx%d %dx%d+%d+%d | %c%c%c%c %dx%d %dx%d+%d+%d | %s\n",
789                                                          FOURCC_STR(private_pp->info.src_config.format),
790                                                          private_pp->info.src_config.size.h,
791                                                          private_pp->info.src_config.size.v,
792                                                          private_pp->info.src_config.pos.x, private_pp->info.src_config.pos.y,
793                                                          private_pp->info.src_config.pos.w, private_pp->info.src_config.pos.h,
794                                                          FOURCC_STR(private_pp->info.dst_config.format),
795                                                          private_pp->info.dst_config.size.h,
796                                                          private_pp->info.dst_config.size.v,
797                                                          private_pp->info.dst_config.pos.x, private_pp->info.dst_config.pos.y,
798                                                          private_pp->info.dst_config.pos.w, private_pp->info.dst_config.pos.h,
799                                                          tdm_transform_str(private_pp->info.transform));
800                         }
801                 }
802         } else {
803                 TDM_SNPRINTF(reply, len, "[No PP capability]\n");
804         }
805         TDM_SNPRINTF(reply, len, "\n");
806
807         if (private_display->capabilities & TDM_DISPLAY_CAPABILITY_CAPTURE) {
808                 const char *sep = "";
809                 TDM_SNPRINTF(reply, len, "[Capture information]\n");
810                 TDM_SNPRINTF(reply, len, "caps\t: ");
811                 tdm_capture_caps_str(private_display->caps_capture.capabilities, &reply, len);
812                 TDM_SNPRINTF(reply, len, "\n");
813                 TDM_SNPRINTF(reply, len, "formats\t: ");
814                 for (i = 0; i < private_display->caps_capture.format_count; i++) {
815                         if (private_display->caps_capture.formats[i] == 0)
816                                 continue;
817                         TDM_SNPRINTF(reply, len, "%s%c%c%c%c", sep, FOURCC_STR(private_display->caps_capture.formats[i]));
818                         sep = ",";
819                 }
820                 TDM_SNPRINTF(reply, len, "\n");
821                 TDM_SNPRINTF(reply, len, "size\t: min(%dx%d) max(%dx%d) align_w(%d)\n",
822                                          private_display->caps_capture.min_w, private_display->caps_capture.min_h,
823                                          private_display->caps_capture.max_w, private_display->caps_capture.max_h,
824                                          private_display->caps_capture.preferred_align);
825                 if (!LIST_IS_EMPTY(&private_display->capture_list)) {
826                         TDM_SNPRINTF(reply, len, "-----------------------------------\n");
827                         TDM_SNPRINTF(reply, len, "dst(format size crop)  |  transform\n");
828                         TDM_SNPRINTF(reply, len, "-----------------------------------\n");
829                         LIST_FOR_EACH_ENTRY(private_capture, &private_display->capture_list, link) {
830                                 TDM_SNPRINTF(reply, len, "%c%c%c%c %dx%d %dx%d+%d+%d | %s\n",
831                                                          FOURCC_STR(private_capture->info.dst_config.format),
832                                                          private_capture->info.dst_config.size.h,
833                                                          private_capture->info.dst_config.size.v,
834                                                          private_capture->info.dst_config.pos.x, private_capture->info.dst_config.pos.y,
835                                                          private_capture->info.dst_config.pos.w, private_capture->info.dst_config.pos.h,
836                                                          tdm_transform_str(private_capture->info.transform));
837                         }
838                 }
839         } else {
840                 TDM_SNPRINTF(reply, len, "[No Capture capability]\n");
841         }
842         TDM_SNPRINTF(reply, len, "\n");
843
844 unlock:
845         _pthread_mutex_unlock(&private_display->lock);
846 }