apply tizen coding style
[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(str != NULL);
220
221         if (!dir) {
222                 dir = tdm_helper_dump_make_directory(TDM_DUMP_DIR, NULL, NULL);
223                 TDM_RETURN_IF_FAIL(dir != NULL);
224         }
225
226         ret = tbm_surface_get_info(buffer, &info);
227         TDM_RETURN_IF_FAIL(ret == TBM_SURFACE_ERROR_NONE);
228
229         if (info.format == TBM_FORMAT_ARGB8888 || info.format == TBM_FORMAT_XRGB8888) {
230                 ext = file_exts[0];
231                 bw = info.planes[0].stride >> 2;
232         } else {
233                 ext = file_exts[1];
234                 bw = info.planes[0].stride;
235         }
236
237         snprintf(file, TDM_PATH_LEN, "%s/%c%c%c%c_%dx%d_%s.%s",
238                          dir, FOURCC_STR(info.format), bw, info.height, str, ext);
239
240         tdm_helper_dump_buffer(buffer, file);
241 }
242
243 EXTERN void
244 tdm_helper_dump_buffer(tbm_surface_h buffer, const char *file)
245 {
246         tbm_surface_info_s info;
247         int len, ret;
248         const char *ext;
249
250         TDM_RETURN_IF_FAIL(buffer != NULL);
251         TDM_RETURN_IF_FAIL(file != NULL);
252
253         ret = tbm_surface_map(buffer, TBM_OPTION_READ, &info);
254         TDM_RETURN_IF_FAIL(ret == TBM_SURFACE_ERROR_NONE);
255
256         len = strnlen(file, 1024);
257         if (info.format == TBM_FORMAT_ARGB8888 || info.format == TBM_FORMAT_XRGB8888)
258                 ext = file_exts[0];
259         else
260                 ext = file_exts[1];
261
262         if (strncmp(file + (len - 3), ext, 3)) {
263                 TDM_ERR("can't dump to '%s' file", file + (len - 3));
264                 tbm_surface_unmap(buffer);
265                 return;
266         }
267
268         switch (info.format) {
269         case TBM_FORMAT_ARGB8888:
270         case TBM_FORMAT_XRGB8888:
271                 _tdm_helper_dump_png(file, info.planes[0].ptr,
272                                                          info.planes[0].stride >> 2, info.height);
273                 break;
274         case TBM_FORMAT_YVU420:
275         case TBM_FORMAT_YUV420:
276                 _tdm_helper_dump_raw(file,
277                                                          info.planes[0].ptr,
278                                                          info.planes[0].stride * info.height,
279                                                          info.planes[1].ptr,
280                                                          info.planes[1].stride * (info.height >> 1),
281                                                          info.planes[2].ptr,
282                                                          info.planes[2].stride * (info.height >> 1));
283                 break;
284         case TBM_FORMAT_NV12:
285         case TBM_FORMAT_NV21:
286                 _tdm_helper_dump_raw(file,
287                                                          info.planes[0].ptr,
288                                                          info.planes[0].stride * info.height,
289                                                          info.planes[1].ptr,
290                                                          info.planes[1].stride * (info.height >> 1), NULL,
291                                                          0);
292                 break;
293         case TBM_FORMAT_YUYV:
294         case TBM_FORMAT_UYVY:
295                 _tdm_helper_dump_raw(file,
296                                                          info.planes[0].ptr,
297                                                          info.planes[0].stride * info.height, NULL, 0,
298                                                          NULL, 0);
299                 break;
300         default:
301                 TDM_ERR("can't dump %c%c%c%c buffer", FOURCC_STR(info.format));
302                 tbm_surface_unmap(buffer);
303                 return;
304         }
305
306         tbm_surface_unmap(buffer);
307
308         TDM_INFO("dump %s", file);
309 }
310
311 EXTERN void
312 tdm_helper_clear_buffer(tbm_surface_h buffer)
313 {
314         tbm_surface_info_s info;
315         int ret;
316
317         TDM_RETURN_IF_FAIL(buffer != NULL);
318
319         ret = tbm_surface_map(buffer, TBM_OPTION_READ, &info);
320         TDM_RETURN_IF_FAIL(ret == TBM_SURFACE_ERROR_NONE);
321
322         switch (info.format) {
323         case TBM_FORMAT_ARGB8888:
324         case TBM_FORMAT_XRGB8888:
325                 memset(info.planes[0].ptr, 0, info.planes[0].stride * info.height);
326                 break;
327         case TBM_FORMAT_YVU420:
328         case TBM_FORMAT_YUV420:
329                 memset((char*)info.planes[0].ptr, 0x10, info.planes[0].stride * info.height);
330                 memset((char*)info.planes[1].ptr, 0x80, info.planes[1].stride * (info.height >> 1));
331                 memset((char*)info.planes[2].ptr, 0x80, info.planes[2].stride * (info.height >> 1));
332                 break;
333         case TBM_FORMAT_NV12:
334         case TBM_FORMAT_NV21:
335                 memset((char*)info.planes[0].ptr, 0x10, info.planes[0].stride * info.height);
336                 memset((char*)info.planes[1].ptr, 0x80, info.planes[1].stride * (info.height >> 1));
337                 break;
338         case TBM_FORMAT_YUYV: {
339                 int *ibuf = (int*)info.planes[0].ptr;
340                 int i, size = info.planes[0].stride * info.height / 4;
341
342                 for (i = 0 ; i < size ; i++)
343                         ibuf[i] = 0x10801080;
344         }
345         break;
346         case TBM_FORMAT_UYVY: {
347                 int *ibuf = (int*)info.planes[0].ptr;
348                 int i, size = info.planes[0].stride * info.height / 4;
349
350                 for (i = 0 ; i < size ; i++)
351                         ibuf[i] = 0x80108010; /* YUYV -> 0xVYUY */
352         }
353         break;
354         default:
355                 TDM_ERR("can't clear %c%c%c%c buffer", FOURCC_STR(info.format));
356                 break;
357         }
358
359         tbm_surface_unmap(buffer);
360 }
361
362 EXTERN int
363 tdm_helper_get_fd(const char *env)
364 {
365         const char *value;
366         int fd, newfd, flags, ret;
367
368         value = (const char*)getenv(env);
369         if (!value)
370                 return -1;
371
372         ret = sscanf(value, "%d", &fd);
373         if (ret < 0) {
374                 TDM_ERR("sscanf failed: %m");
375                 return -1;
376         }
377
378         flags = fcntl(fd, F_GETFD);
379         if (flags == -1) {
380                 TDM_ERR("fcntl failed: %m");
381                 return -1;
382         }
383
384         newfd = dup(fd);
385         if (newfd < 0) {
386                 TDM_ERR("dup failed: %m");
387                 return -1;
388         }
389
390         TDM_INFO("%s: fd(%d) newfd(%d)", env, fd, newfd);
391
392         fcntl(newfd, F_SETFD, flags | FD_CLOEXEC);
393
394         return newfd;
395 }
396
397 EXTERN void
398 tdm_helper_set_fd(const char *env, int fd)
399 {
400         char buf[32];
401         int ret;
402
403         snprintf(buf, sizeof(buf), "%d", fd);
404
405         ret = setenv(env, (const char*)buf, 1);
406         if (ret) {
407                 TDM_ERR("setenv failed: %m");
408                 return;
409         }
410
411         if (fd >= 0)
412                 TDM_INFO("%s: fd(%d)", env, fd);
413 }
414
415 EXTERN void
416 tdm_helper_dump_start(char *dumppath, int *count)
417 {
418         if (dumppath == NULL || count == NULL) {
419                 TDM_DBG("tdm_helper_dump dumppath or count is null.");
420                 return;
421         }
422
423         tdm_dump_enable = 1;
424
425         TDM_DBG("tdm_helper_dump start.(path : %s)", dumppath);
426 }
427
428 EXTERN void
429 tdm_helper_dump_stop(void)
430 {
431         tdm_dump_enable = 0;
432
433         TDM_DBG("tdm_helper_dump stop.");
434 }
435
436 static pixman_format_code_t
437 _tdm_helper_pixman_format_get(tbm_format format)
438 {
439         switch (format) {
440         case TBM_FORMAT_ARGB8888:
441                 return PIXMAN_a8r8g8b8;
442         case TBM_FORMAT_XRGB8888:
443                 return PIXMAN_x8r8g8b8;
444         default:
445                 return 0;
446         }
447
448         return 0;
449 }
450
451 static tdm_error
452 _tdm_helper_buffer_convert(tbm_surface_h srcbuf, tbm_surface_h dstbuf,
453                                                    int dx, int dy, int dw, int dh, int count)
454 {
455         pixman_image_t *src_img = NULL, *dst_img = NULL;
456         pixman_format_code_t src_format, dst_format;
457         pixman_transform_t t;
458         struct pixman_f_transform ft;
459         pixman_op_t op;
460         tbm_surface_info_s src_info = {0, };
461         tbm_surface_info_s dst_info = {0, };
462         int stride, width;
463         double scale_x, scale_y;
464
465         TDM_RETURN_VAL_IF_FAIL(srcbuf != NULL, TDM_ERROR_INVALID_PARAMETER);
466         TDM_RETURN_VAL_IF_FAIL(dstbuf != NULL, TDM_ERROR_INVALID_PARAMETER);
467
468         if (tbm_surface_map(srcbuf, TBM_SURF_OPTION_READ, &src_info)
469                         != TBM_SURFACE_ERROR_NONE) {
470                 TDM_ERR("cannot mmap srcbuf\n");
471                 return TDM_ERROR_OPERATION_FAILED;
472         }
473
474         if (tbm_surface_map(dstbuf, TBM_SURF_OPTION_WRITE, &dst_info)
475                         != TBM_SURFACE_ERROR_NONE) {
476                 TDM_ERR("cannot mmap dstbuf\n");
477                 tbm_surface_unmap(srcbuf);
478                 return TDM_ERROR_OPERATION_FAILED;
479         }
480         TDM_GOTO_IF_FAIL(src_info.num_planes == 1, cant_convert);
481         TDM_GOTO_IF_FAIL(dst_info.num_planes == 1, cant_convert);
482
483         /* src */
484         src_format = _tdm_helper_pixman_format_get(src_info.format);
485         TDM_GOTO_IF_FAIL(src_format > 0, cant_convert);
486
487         width = src_info.planes[0].stride / 4;
488         stride = src_info.planes[0].stride;
489         src_img = pixman_image_create_bits(src_format, width, src_info.height,
490                                                                            (uint32_t*)src_info.planes[0].ptr, stride);
491         TDM_GOTO_IF_FAIL(src_img != NULL, cant_convert);
492
493         /* dst */
494         dst_format = _tdm_helper_pixman_format_get(dst_info.format);
495         TDM_GOTO_IF_FAIL(dst_format > 0, cant_convert);
496
497         width = dst_info.planes[0].stride / 4;
498         stride = dst_info.planes[0].stride;
499         dst_img = pixman_image_create_bits(dst_format, width, dst_info.height,
500                                                                            (uint32_t*)dst_info.planes[0].ptr, stride);
501         TDM_GOTO_IF_FAIL(dst_img != NULL, cant_convert);
502
503         pixman_f_transform_init_identity(&ft);
504
505         scale_x = (double)src_info.width / dw;
506         scale_y = (double)src_info.height / dh;
507
508         pixman_f_transform_scale(&ft, NULL, scale_x, scale_y);
509         pixman_f_transform_translate(&ft, NULL, 0, 0);
510         pixman_transform_from_pixman_f_transform(&t, &ft);
511         pixman_image_set_transform(src_img, &t);
512
513         if (count == 0)
514                 op = PIXMAN_OP_SRC;
515         else
516                 op = PIXMAN_OP_OVER;
517
518         pixman_image_composite(op, src_img, NULL, dst_img,
519                                                    0, 0, 0, 0, dx, dy, dw, dh);
520
521         if (src_img)
522                 pixman_image_unref(src_img);
523         if (dst_img)
524                 pixman_image_unref(dst_img);
525
526         tbm_surface_unmap(srcbuf);
527         tbm_surface_unmap(dstbuf);
528
529         return TDM_ERROR_NONE;
530
531 cant_convert:
532         if (src_img)
533                 pixman_image_unref(src_img);
534
535         tbm_surface_unmap(srcbuf);
536         tbm_surface_unmap(dstbuf);
537
538         return TDM_ERROR_OPERATION_FAILED;
539 }
540
541 EXTERN tdm_error
542 tdm_helper_capture_output(tdm_output *output, tbm_surface_h dst_buffer,
543                                                   int x, int y, int w, int h,
544                                                   tdm_helper_capture_handler func, void *data)
545 {
546         tbm_surface_h surface;
547         tdm_error err;
548         int i, count, first = 0;
549
550         TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
551         TDM_RETURN_VAL_IF_FAIL(dst_buffer != NULL, TDM_ERROR_INVALID_PARAMETER);
552         TDM_RETURN_VAL_IF_FAIL(x >= 0, TDM_ERROR_INVALID_PARAMETER);
553         TDM_RETURN_VAL_IF_FAIL(y >= 0, TDM_ERROR_INVALID_PARAMETER);
554         TDM_RETURN_VAL_IF_FAIL(w >= 0, TDM_ERROR_INVALID_PARAMETER);
555         TDM_RETURN_VAL_IF_FAIL(h >= 0, TDM_ERROR_INVALID_PARAMETER);
556         TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
557         TDM_RETURN_VAL_IF_FAIL(data != NULL, TDM_ERROR_INVALID_PARAMETER);
558
559         err = tdm_output_get_layer_count(output, &count);
560         if (err != TDM_ERROR_NONE) {
561                 TDM_ERR("tdm_output_get_layer_count fail(%d)\n", err);
562                 return TDM_ERROR_OPERATION_FAILED;
563         }
564         if (count <= 0) {
565                 TDM_ERR("tdm_output_get_layer_count err(%d, %d)\n", err, count);
566                 return TDM_ERROR_BAD_MODULE;
567         }
568
569         for (i = count - 1; i >= 0; i--) {
570                 tdm_layer *layer = tdm_output_get_layer(output, i, NULL);
571
572                 surface = tdm_layer_get_displaying_buffer(layer, &err);
573                 if (err != TDM_ERROR_NONE)
574                         continue;
575
576                 err = _tdm_helper_buffer_convert(surface, dst_buffer, x, y, w, h, first++);
577                 if (err != TDM_ERROR_NONE)
578                         TDM_DBG("convert fail %d-layer buffer\n", i);
579                 else
580                         TDM_DBG("convert success %d-layer buffer\n", i);
581         }
582
583         func(dst_buffer, data);
584
585         return TDM_ERROR_NONE;
586 }
587
588 EXTERN void
589 tdm_helper_get_display_information(tdm_display *dpy, char *reply, int *len)
590 {
591         tdm_private_display *private_display;
592         tdm_backend_module *module_data;
593         tdm_func_output *func_output;
594         tdm_func_layer *func_layer;
595         tdm_private_output *private_output = NULL;
596         tdm_private_layer *private_layer = NULL;
597         tdm_private_pp *private_pp = NULL;
598         tdm_private_capture *private_capture = NULL;
599         tdm_error ret;
600         int i;
601
602         TDM_DBG_RETURN_IF_FAIL(dpy != NULL);
603
604         private_display = dpy;
605         func_output = &private_display->func_output;
606         func_layer = &private_display->func_layer;
607         _pthread_mutex_lock(&private_display->lock);
608
609         /* module information */
610         module_data = private_display->module_data;
611         TDM_SNPRINTF(reply, len, "[TDM backend information]\n");
612         TDM_SNPRINTF(reply, len, "name: %s\n", module_data->name);
613         TDM_SNPRINTF(reply, len, "vendor: %s\n", module_data->vendor);
614         TDM_SNPRINTF(reply, len, "version: %d.%d\n\n",
615                                  (int)TDM_BACKEND_GET_ABI_MAJOR(module_data->abi_version),
616                                  (int)TDM_BACKEND_GET_ABI_MINOR(module_data->abi_version));
617
618         /* output information */
619         TDM_SNPRINTF(reply, len, "[Output information]\n");
620         TDM_SNPRINTF(reply, len, "--------------------------------------------------------------------------------------------\n");
621         TDM_SNPRINTF(reply, len, "idx   maker   model   name   type   status   dpms   subpixel   align_w   min   max   phy(mm)\n");
622         TDM_SNPRINTF(reply, len, "--------------------------------------------------------------------------------------------\n");
623         LIST_FOR_EACH_ENTRY(private_output, &private_display->output_list, link) {
624                 TDM_SNPRINTF(reply, len, "%d   %s   %s   %s   %s   %s   %s   %d   %d   %dx%d   %dx%d   %dx%d\n",
625                                          private_output->index, private_output->caps.maker,
626                                          private_output->caps.model, private_output->caps.name,
627                                          tdm_conn_str(private_output->caps.type),
628                                          tdm_status_str(private_output->caps.status),
629                                          tdm_dpms_str(private_output->current_dpms_value),
630                                          private_output->caps.subpixel,
631                                          private_output->caps.preferred_align,
632                                          private_output->caps.min_w, private_output->caps.min_h,
633                                          private_output->caps.max_w, private_output->caps.max_h,
634                                          private_output->caps.mmWidth, private_output->caps.mmHeight);
635
636                 TDM_SNPRINTF(reply, len, "\t%d modes:\n", private_output->caps.mode_count);
637
638                 if (private_output->caps.mode_count > 0) {
639                         const tdm_output_mode *current_mode = NULL;
640
641                         TDM_DBG_GOTO_IF_FAIL(func_output->output_get_mode, unlock);
642                         ret = func_output->output_get_mode(private_output->output_backend, &current_mode);
643                         TDM_DBG_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, unlock);
644
645                         TDM_SNPRINTF(reply, len, "\t\t name refresh (Hz) hdisp hss hse htot vdisp vss vse vtot\n");
646                         for (i = 0; i < private_output->caps.mode_count; i++) {
647                                 char *current = (current_mode == private_output->caps.modes + i) ? "*" : " ";
648                                 TDM_SNPRINTF(reply, len, "\t\t%s%s %d %d %d %d %d %d %d %d %d ",
649                                                          current,
650                                                          private_output->caps.modes[i].name,
651                                                          private_output->caps.modes[i].vrefresh,
652                                                          private_output->caps.modes[i].hdisplay,
653                                                          private_output->caps.modes[i].hsync_start,
654                                                          private_output->caps.modes[i].hsync_end,
655                                                          private_output->caps.modes[i].htotal,
656                                                          private_output->caps.modes[i].vdisplay,
657                                                          private_output->caps.modes[i].vsync_start,
658                                                          private_output->caps.modes[i].vsync_end,
659                                                          private_output->caps.modes[i].vtotal);
660                                 tdm_mode_flag_str(private_output->caps.modes[i].flags, &reply, len);
661                                 TDM_SNPRINTF(reply, len, " ");
662                                 tdm_mode_type_str(private_output->caps.modes[i].type, &reply, len);
663                                 TDM_SNPRINTF(reply, len, "\n");
664                         }
665                 }
666
667                 TDM_SNPRINTF(reply, len, "\t%d properties:\n", private_output->caps.prop_count);
668                 if (private_output->caps.prop_count > 0) {
669                         TDM_SNPRINTF(reply, len, "\t\tname\tidx\tvalue\n");
670                         for (i = 0; i < private_output->caps.prop_count; i++) {
671                                 tdm_value value;
672                                 TDM_DBG_GOTO_IF_FAIL(func_output->output_get_property, unlock);
673                                 ret = func_output->output_get_property(private_output->output_backend,
674                                                                                                            private_output->caps.props[i].id,
675                                                                                                            &value);
676                                 TDM_DBG_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, unlock);
677                                 TDM_SNPRINTF(reply, len, "\t\t%s\t%d\t%d\n",
678                                                          private_output->caps.props[i].name,
679                                                          private_output->caps.props[i].id,
680                                                          value.u32);
681                         }
682                 }
683         }
684         TDM_SNPRINTF(reply, len, "\n");
685
686         /* layer information */
687         TDM_SNPRINTF(reply, len, "[Layer information]\n");
688         TDM_SNPRINTF(reply, len, "-----------------------------------------------------------------------\n");
689         TDM_SNPRINTF(reply, len, "idx   output   zpos   buf   format   size   crop   geometry   transform\n");
690         TDM_SNPRINTF(reply, len, "-----------------------------------------------------------------------\n");
691         LIST_FOR_EACH_ENTRY(private_output, &private_display->output_list, link) {
692                 LIST_FOR_EACH_ENTRY(private_layer, &private_output->layer_list, link) {
693                         if (!private_layer->usable) {
694                                 tdm_info_layer info;
695                                 unsigned int format;
696                                 tdm_size size;
697                                 tbm_surface_info_s buf_info;
698
699                                 TDM_DBG_GOTO_IF_FAIL(func_layer->layer_get_info, unlock);
700                                 memset(&info, 0, sizeof info);
701                                 ret = func_layer->layer_get_info(private_layer->layer_backend, &info);
702                                 TDM_DBG_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, unlock);
703
704                                 format = tbm_surface_get_format(private_layer->showing_buffer);
705                                 tbm_surface_get_info(private_layer->showing_buffer, &buf_info);
706
707                                 if (IS_RGB(format))
708                                         size.h = buf_info.planes[0].stride >> 2;
709                                 else
710                                         size.h = buf_info.planes[0].stride;
711                                 size.v = tbm_surface_get_height(private_layer->showing_buffer);
712
713                                 if (info.src_config.format)
714                                         format = (info.src_config.format)?:format;
715
716                                 TDM_SNPRINTF(reply, len, "%d   %d   %d   %p   %c%c%c%c   %dx%d   %dx%d+%d+%d   %dx%d+%d+%d   %s\n",
717                                                          private_layer->index,
718                                                          private_output->index,
719                                                          private_layer->caps.zpos,
720                                                          private_layer->showing_buffer, FOURCC_STR(format), size.h, size.v,
721                                                          info.src_config.pos.w, info.src_config.pos.h, info.src_config.pos.x, info.src_config.pos.y,
722                                                          info.dst_pos.w, info.dst_pos.h, info.dst_pos.x, info.dst_pos.y,
723                                                          tdm_transform_str(info.transform));
724                         } else {
725                                 TDM_SNPRINTF(reply, len, "%d   %d   %d   -\n",
726                                                          private_layer->index,
727                                                          private_output->index,
728                                                          private_layer->caps.zpos);
729                         }
730
731                         TDM_SNPRINTF(reply, len, "\tcaps\t: ");
732                         tdm_layer_caps_str(private_layer->caps.capabilities, &reply, len);
733                         TDM_SNPRINTF(reply, len, "\n");
734
735                         TDM_SNPRINTF(reply, len, "\tformats\t: ");
736                         if (private_layer->caps.format_count > 0) {
737                                 const char *sep = "";
738                                 for (i = 0; i < private_layer->caps.format_count; i++) {
739                                         if (private_layer->caps.formats[i] == 0)
740                                                 continue;
741                                         TDM_SNPRINTF(reply, len, "%s%c%c%c%c", sep, FOURCC_STR(private_layer->caps.formats[i]));
742                                         sep = ",";
743                                 }
744                                 TDM_SNPRINTF(reply, len, "\n");
745                         }
746
747                         TDM_SNPRINTF(reply, len, "\t%d properties:\n", private_layer->caps.prop_count);
748                         if (private_layer->caps.prop_count > 0) {
749                                 TDM_SNPRINTF(reply, len, "\t\tname\tidx\tvalue\n");
750                                 for (i = 0; i < private_layer->caps.prop_count; i++) {
751                                         tdm_value value;
752                                         TDM_DBG_GOTO_IF_FAIL(func_layer->layer_get_property, unlock);
753                                         ret = func_layer->layer_get_property(private_layer->layer_backend,
754                                                                                                                  private_layer->caps.props[i].id,
755                                                                                                                  &value);
756                                         TDM_DBG_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, unlock);
757                                         TDM_SNPRINTF(reply, len, "\t\t%s\t%d\t%d\n",
758                                                                  private_layer->caps.props[i].name,
759                                                                  private_layer->caps.props[i].id,
760                                                                  value.u32);
761                                 }
762                         }
763                 }
764         }
765         TDM_SNPRINTF(reply, len, "\n");
766
767         if (private_display->capabilities & TDM_DISPLAY_CAPABILITY_PP) {
768                 const char *sep = "";
769                 TDM_SNPRINTF(reply, len, "[PP information]\n");
770                 TDM_SNPRINTF(reply, len, "caps\t: ");
771                 tdm_pp_caps_str(private_display->caps_pp.capabilities, &reply, len);
772                 TDM_SNPRINTF(reply, len, "\n");
773                 TDM_SNPRINTF(reply, len, "formats\t: ");
774                 for (i = 0; i < private_display->caps_pp.format_count; i++) {
775                         if (private_display->caps_pp.formats[i] == 0)
776                                 continue;
777                         TDM_SNPRINTF(reply, len, "%s%c%c%c%c", sep, FOURCC_STR(private_display->caps_pp.formats[i]));
778                         sep = ",";
779                 }
780                 TDM_SNPRINTF(reply, len, "\n");
781                 TDM_SNPRINTF(reply, len, "size\t: min(%dx%d) max(%dx%d) align_w(%d)\n",
782                                          private_display->caps_pp.min_w, private_display->caps_pp.min_h,
783                                          private_display->caps_pp.max_w, private_display->caps_pp.max_h,
784                                          private_display->caps_pp.preferred_align);
785                 if (!LIST_IS_EMPTY(&private_display->pp_list)) {
786                         TDM_SNPRINTF(reply, len, "-------------------------------------------------------------\n");
787                         TDM_SNPRINTF(reply, len, "src(format size crop)  |  dst(format size crop)  |  transform\n");
788                         TDM_SNPRINTF(reply, len, "-------------------------------------------------------------\n");
789                         LIST_FOR_EACH_ENTRY(private_pp, &private_display->pp_list, link) {
790                                 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",
791                                                          FOURCC_STR(private_pp->info.src_config.format),
792                                                          private_pp->info.src_config.size.h,
793                                                          private_pp->info.src_config.size.v,
794                                                          private_pp->info.src_config.pos.x, private_pp->info.src_config.pos.y,
795                                                          private_pp->info.src_config.pos.w, private_pp->info.src_config.pos.h,
796                                                          FOURCC_STR(private_pp->info.dst_config.format),
797                                                          private_pp->info.dst_config.size.h,
798                                                          private_pp->info.dst_config.size.v,
799                                                          private_pp->info.dst_config.pos.x, private_pp->info.dst_config.pos.y,
800                                                          private_pp->info.dst_config.pos.w, private_pp->info.dst_config.pos.h,
801                                                          tdm_transform_str(private_pp->info.transform));
802                         }
803                 }
804         } else {
805                 TDM_SNPRINTF(reply, len, "[No PP capability]\n");
806         }
807         TDM_SNPRINTF(reply, len, "\n");
808
809         if (private_display->capabilities & TDM_DISPLAY_CAPABILITY_CAPTURE) {
810                 const char *sep = "";
811                 TDM_SNPRINTF(reply, len, "[Capture information]\n");
812                 TDM_SNPRINTF(reply, len, "caps\t: ");
813                 tdm_capture_caps_str(private_display->caps_capture.capabilities, &reply, len);
814                 TDM_SNPRINTF(reply, len, "\n");
815                 TDM_SNPRINTF(reply, len, "formats\t: ");
816                 for (i = 0; i < private_display->caps_capture.format_count; i++) {
817                         if (private_display->caps_capture.formats[i] == 0)
818                                 continue;
819                         TDM_SNPRINTF(reply, len, "%s%c%c%c%c", sep, FOURCC_STR(private_display->caps_capture.formats[i]));
820                         sep = ",";
821                 }
822                 TDM_SNPRINTF(reply, len, "\n");
823                 TDM_SNPRINTF(reply, len, "size\t: min(%dx%d) max(%dx%d) align_w(%d)\n",
824                                          private_display->caps_capture.min_w, private_display->caps_capture.min_h,
825                                          private_display->caps_capture.max_w, private_display->caps_capture.max_h,
826                                          private_display->caps_capture.preferred_align);
827                 if (!LIST_IS_EMPTY(&private_display->capture_list)) {
828                         TDM_SNPRINTF(reply, len, "-----------------------------------\n");
829                         TDM_SNPRINTF(reply, len, "dst(format size crop)  |  transform\n");
830                         TDM_SNPRINTF(reply, len, "-----------------------------------\n");
831                         LIST_FOR_EACH_ENTRY(private_capture, &private_display->capture_list, link) {
832                                 TDM_SNPRINTF(reply, len, "%c%c%c%c %dx%d %dx%d+%d+%d | %s\n",
833                                                          FOURCC_STR(private_capture->info.dst_config.format),
834                                                          private_capture->info.dst_config.size.h,
835                                                          private_capture->info.dst_config.size.v,
836                                                          private_capture->info.dst_config.pos.x, private_capture->info.dst_config.pos.y,
837                                                          private_capture->info.dst_config.pos.w, private_capture->info.dst_config.pos.h,
838                                                          tdm_transform_str(private_capture->info.transform));
839                         }
840                 }
841         } else {
842                 TDM_SNPRINTF(reply, len, "[No Capture capability]\n");
843         }
844         TDM_SNPRINTF(reply, len, "\n");
845
846 unlock:
847         _pthread_mutex_unlock(&private_display->lock);
848 }