change sscanf to strtol for getting fd
[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 EXTERN double
60 tdm_helper_get_time(void)
61 {
62         struct timespec tp;
63
64         if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
65                 return (double)tp.tv_sec + ((double)tp.tv_nsec) / 1000000000.0;
66
67         return 0;
68 }
69
70 static void
71 _tdm_helper_dump_raw(const char *file, void *data1, int size1, void *data2,
72                                          int size2, void *data3, int size3)
73 {
74         unsigned int *blocks;
75         FILE *fp = fopen(file, "w+");
76         TDM_RETURN_IF_FAIL(fp != NULL);
77
78         blocks = (unsigned int *)data1;
79         fwrite(blocks, 1, size1, fp);
80
81         if (size2 > 0) {
82                 blocks = (unsigned int *)data2;
83                 fwrite(blocks, 1, size2, fp);
84         }
85
86         if (size3 > 0) {
87                 blocks = (unsigned int *)data3;
88                 fwrite(blocks, 1, size3, fp);
89         }
90
91         fclose(fp);
92 }
93
94 static void
95 _tdm_helper_dump_png(const char *file, const void *data, int width,
96                                          int height)
97 {
98         FILE *fp = fopen(file, "wb");
99         TDM_RETURN_IF_FAIL(fp != NULL);
100
101         png_structp pPngStruct =
102                 png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
103         if (!pPngStruct) {
104                 fclose(fp);
105                 return;
106         }
107
108         png_infop pPngInfo = png_create_info_struct(pPngStruct);
109         if (!pPngInfo) {
110                 png_destroy_write_struct(&pPngStruct, NULL);
111                 fclose(fp);
112                 return;
113         }
114
115         png_init_io(pPngStruct, fp);
116         png_set_IHDR(pPngStruct,
117                                  pPngInfo,
118                                  width,
119                                  height,
120                                  PNG_DEPTH,
121                                  PNG_COLOR_TYPE_RGBA,
122                                  PNG_INTERLACE_NONE,
123                                  PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
124
125         png_set_bgr(pPngStruct);
126         png_write_info(pPngStruct, pPngInfo);
127
128         const int pixel_size = 4;       // RGBA
129         png_bytep *row_pointers =
130                 png_malloc(pPngStruct, height * sizeof(png_byte *));
131         if (!row_pointers) {
132                 png_destroy_write_struct(&pPngStruct, &pPngInfo);
133                 fclose(fp);
134                 return;
135         }
136
137         unsigned int *blocks = (unsigned int *)data;
138         int y = 0;
139         int x = 0;
140
141         for (; y < height; ++y) {
142                 png_bytep row =
143                         png_malloc(pPngStruct, sizeof(png_byte) * width * pixel_size);
144                 if (!row) {
145                         for (x = 0; x < y; x++)
146                                 png_free(pPngStruct, row_pointers[x]);
147                         png_free(pPngStruct, row_pointers);
148                         png_destroy_write_struct(&pPngStruct, &pPngInfo);
149                         fclose(fp);
150                         return;
151                 }
152
153                 row_pointers[y] = (png_bytep)row;
154                 for (x = 0; x < width; ++x) {
155                         unsigned int curBlock = blocks[y * width + x];
156                         row[x * pixel_size] = (curBlock & 0xFF);
157                         row[1 + x * pixel_size] = (curBlock >> 8) & 0xFF;
158                         row[2 + x * pixel_size] = (curBlock >> 16) & 0xFF;
159                         row[3 + x * pixel_size] = (curBlock >> 24) & 0xFF;
160                 }
161         }
162
163         png_write_image(pPngStruct, row_pointers);
164         png_write_end(pPngStruct, pPngInfo);
165
166         for (y = 0; y < height; y++)
167                 png_free(pPngStruct, row_pointers[y]);
168         png_free(pPngStruct, row_pointers);
169
170         png_destroy_write_struct(&pPngStruct, &pPngInfo);
171
172         fclose(fp);
173 }
174
175 INTERN char *
176 tdm_helper_dump_make_directory(const char *path, char *reply, int *len)
177 {
178         char *fullpath = NULL;
179         time_t timer;
180         struct tm *t, *buf = NULL;
181
182         timer = time(NULL);
183
184         buf = calloc(1, sizeof(struct tm));
185         TDM_GOTO_IF_FAIL(buf != NULL, failed_make);
186
187         fullpath = calloc(1, TDM_PATH_LEN * sizeof(char));
188         TDM_GOTO_IF_FAIL(fullpath != NULL, failed_make);
189
190         t = localtime_r(&timer, buf);
191         TDM_GOTO_IF_FAIL(t != NULL, failed_make);
192
193         snprintf(fullpath, TDM_PATH_LEN, "%s/dump_%04d%02d%02d.%02d%02d%02d", path,
194                          t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
195
196         if ((mkdir(fullpath, 0755)) < 0) {
197                 TDM_ERR("mkdir '%s' fail\n", fullpath);
198                 TDM_SNPRINTF(reply, len, "mkdir '%s' fail\n", fullpath);
199                 goto failed_make;
200         }
201
202         free(buf);
203
204         return fullpath;
205 failed_make:
206         if (fullpath)
207                 free(fullpath);
208         if (buf)
209                 free(buf);
210         return NULL;
211 }
212
213 INTERN void
214 tdm_helper_dump_buffer_str(tbm_surface_h buffer, char *dir, char *str)
215 {
216         tbm_surface_info_s info;
217         const char *ext;
218         char file[TDM_PATH_LEN];
219         int ret, bw;
220
221         TDM_RETURN_IF_FAIL(buffer != NULL);
222         TDM_RETURN_IF_FAIL(dir != NULL);
223         TDM_RETURN_IF_FAIL(str != NULL);
224
225         ret = tbm_surface_get_info(buffer, &info);
226         TDM_RETURN_IF_FAIL(ret == TBM_SURFACE_ERROR_NONE);
227
228         if (info.format == TBM_FORMAT_ARGB8888 || info.format == TBM_FORMAT_XRGB8888) {
229                 ext = file_exts[0];
230                 bw = info.planes[0].stride >> 2;
231         } else {
232                 ext = file_exts[1];
233                 bw = info.planes[0].stride;
234         }
235
236         snprintf(file, TDM_PATH_LEN, "%s/%c%c%c%c_%dx%d_%s.%s",
237                          dir, FOURCC_STR(info.format), bw, info.height, str, ext);
238
239         tdm_helper_dump_buffer(buffer, file);
240 }
241
242 EXTERN void
243 tdm_helper_dump_buffer(tbm_surface_h buffer, const char *file)
244 {
245         tbm_surface_info_s info;
246         int len, ret;
247         const char *ext;
248         int bo_cnt;
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         bo_cnt = tbm_surface_internal_get_num_bos(buffer);
269         TDM_DBG("buffer: bo_cnt(%d) %dx%d %c%c%c%c, plane: (%p+%d, %d,%d) (%p+%d, %d,%d) (%p+%d, %d,%d)",
270                         bo_cnt, info.width, info.height, FOURCC_STR(info.format),
271                         info.planes[0].ptr, info.planes[0].offset, info.planes[0].stride, info.planes[0].size,
272                         info.planes[1].ptr, info.planes[1].offset, info.planes[1].stride, info.planes[1].size,
273                         info.planes[2].ptr, info.planes[2].offset, info.planes[2].stride, info.planes[2].size);
274
275         switch (info.format) {
276         case TBM_FORMAT_ARGB8888:
277         case TBM_FORMAT_XRGB8888:
278                 _tdm_helper_dump_png(file, info.planes[0].ptr,
279                                                          info.planes[0].stride >> 2, info.height);
280                 break;
281         case TBM_FORMAT_YVU420:
282         case TBM_FORMAT_YUV420:
283                 _tdm_helper_dump_raw(file,
284                                                          info.planes[0].ptr,
285                                                          info.planes[0].stride * info.height,
286                                                          info.planes[1].ptr,
287                                                          info.planes[1].stride * (info.height >> 1),
288                                                          info.planes[2].ptr,
289                                                          info.planes[2].stride * (info.height >> 1));
290                 break;
291         case TBM_FORMAT_NV12:
292         case TBM_FORMAT_NV21:
293                 if (bo_cnt == 1 &&
294                         (info.planes[0].ptr + info.planes[0].stride * info.height) != info.planes[1].ptr)
295                         TDM_WRN("%p != %p", info.planes[0].ptr + info.planes[0].stride * info.height, info.planes[1].ptr);
296
297                 _tdm_helper_dump_raw(file,
298                                                          info.planes[0].ptr,
299                                                          info.planes[0].stride * info.height,
300                                                          info.planes[1].ptr,
301                                                          info.planes[1].stride * (info.height >> 1), NULL,
302                                                          0);
303                 break;
304         case TBM_FORMAT_YUYV:
305         case TBM_FORMAT_UYVY:
306                 _tdm_helper_dump_raw(file,
307                                                          info.planes[0].ptr,
308                                                          info.planes[0].stride * info.height, NULL, 0,
309                                                          NULL, 0);
310                 break;
311         default:
312                 TDM_ERR("can't dump %c%c%c%c buffer", FOURCC_STR(info.format));
313                 tbm_surface_unmap(buffer);
314                 return;
315         }
316
317         tbm_surface_unmap(buffer);
318
319         TDM_INFO("dump %s", file);
320 }
321
322 void
323 tdm_helper_clear_buffer_pos(tbm_surface_h buffer, tdm_pos *pos)
324 {
325         tbm_surface_info_s info;
326         int ret;
327
328         TDM_RETURN_IF_FAIL(buffer != NULL);
329
330         ret = tbm_surface_map(buffer, TBM_OPTION_READ, &info);
331         TDM_RETURN_IF_FAIL(ret == TBM_SURFACE_ERROR_NONE);
332
333         switch (info.format) {
334         case TBM_FORMAT_ARGB8888:
335         case TBM_FORMAT_XRGB8888:
336                 if (!pos) {
337                         memset(info.planes[0].ptr, 0, info.planes[0].stride * info.height);
338                 } else {
339                         unsigned char *p;
340                         int x, y;
341                         for (y = pos->y; y <= (pos->y + pos->h); y++) {
342                                 p = info.planes[0].ptr + info.planes[0].stride * y;
343                                 for (x = pos->x; x <= (pos->x + pos->w); x++) {
344                                         int *ibuf = (int*)p;
345                                         ibuf[x] = 0x00000000;
346                                 }
347                         }
348                 }
349                 break;
350         case TBM_FORMAT_YVU420:
351         case TBM_FORMAT_YUV420:
352                 memset((char*)info.planes[0].ptr, 0x10, info.planes[0].stride * info.height);
353                 memset((char*)info.planes[1].ptr, 0x80, info.planes[1].stride * (info.height >> 1));
354                 memset((char*)info.planes[2].ptr, 0x80, info.planes[2].stride * (info.height >> 1));
355                 break;
356         case TBM_FORMAT_NV12:
357         case TBM_FORMAT_NV21:
358                 memset((char*)info.planes[0].ptr, 0x10, info.planes[0].stride * info.height);
359                 memset((char*)info.planes[1].ptr, 0x80, info.planes[1].stride * (info.height >> 1));
360                 break;
361         case TBM_FORMAT_YUYV: {
362                 int *ibuf = (int*)info.planes[0].ptr;
363                 int i, size = info.planes[0].stride * info.height / 4;
364
365                 for (i = 0 ; i < size ; i++)
366                         ibuf[i] = 0x10801080;
367         }
368         break;
369         case TBM_FORMAT_UYVY: {
370                 int *ibuf = (int*)info.planes[0].ptr;
371                 int i, size = info.planes[0].stride * info.height / 4;
372
373                 for (i = 0 ; i < size ; i++)
374                         ibuf[i] = 0x80108010; /* YUYV -> 0xVYUY */
375         }
376         break;
377         default:
378                 TDM_ERR("can't clear %c%c%c%c buffer", FOURCC_STR(info.format));
379                 break;
380         }
381
382         tbm_surface_unmap(buffer);
383 }
384
385 EXTERN void
386 tdm_helper_clear_buffer(tbm_surface_h buffer)
387 {
388         TDM_RETURN_IF_FAIL(buffer != NULL);
389
390         tdm_helper_clear_buffer_pos(buffer, NULL);
391 }
392
393 static pixman_format_code_t
394 _tdm_helper_pixman_format_get(tbm_format format)
395 {
396         switch (format) {
397         case TBM_FORMAT_ARGB8888:
398                 return PIXMAN_a8r8g8b8;
399         case TBM_FORMAT_XRGB8888:
400                 return PIXMAN_x8r8g8b8;
401         default:
402                 return 0;
403         }
404
405         return 0;
406 }
407
408 EXTERN tdm_error
409 tdm_helper_convert_buffer(tbm_surface_h srcbuf, tbm_surface_h dstbuf,
410                                                   tdm_pos *srcpos, tdm_pos *dstpos,
411                                                   tdm_transform transform, int over)
412 {
413         tbm_surface_info_s src_info, dst_info;
414         pixman_image_t *src_img = NULL, *dst_img = NULL;
415         pixman_format_code_t src_format, dst_format;
416         double scale_x, scale_y;
417         int rotate_step, bos;
418         pixman_transform_t t;
419         struct pixman_f_transform ft;
420         pixman_op_t op;
421         int src_stride, dst_stride;
422         int buf_width, err;
423         tdm_error ret = TDM_ERROR_OPERATION_FAILED;
424
425         TDM_RETURN_VAL_IF_FAIL(srcbuf != NULL, TDM_ERROR_INVALID_PARAMETER);
426         TDM_RETURN_VAL_IF_FAIL(dstbuf != NULL, TDM_ERROR_INVALID_PARAMETER);
427
428         bos = tbm_surface_internal_get_num_bos(srcbuf);
429         TDM_RETURN_VAL_IF_FAIL(bos == 1, TDM_ERROR_OPERATION_FAILED);
430
431         bos = tbm_surface_internal_get_num_bos(dstbuf);
432         TDM_RETURN_VAL_IF_FAIL(bos == 1, TDM_ERROR_OPERATION_FAILED);
433
434         err = tbm_surface_map(srcbuf, TBM_OPTION_READ, &src_info);
435         TDM_RETURN_VAL_IF_FAIL(err == TBM_SURFACE_ERROR_NONE, TDM_ERROR_OPERATION_FAILED);
436
437         err = tbm_surface_map(dstbuf, TBM_OPTION_WRITE, &dst_info);
438         TDM_GOTO_IF_FAIL(err == TBM_SURFACE_ERROR_NONE, unmap_srcbuf);
439
440         /* not handle buffers which have 2 more gem handles */
441         TDM_GOTO_IF_FAIL(src_info.planes[0].ptr != NULL, unmap_dstbuf);
442         TDM_GOTO_IF_FAIL(dst_info.planes[0].ptr != NULL, unmap_dstbuf);
443
444         src_format = _tdm_helper_pixman_format_get(src_info.format);
445         TDM_GOTO_IF_FAIL(src_format > 0, unmap_dstbuf);
446         dst_format = _tdm_helper_pixman_format_get(dst_info.format);
447         TDM_GOTO_IF_FAIL(dst_format > 0, unmap_dstbuf);
448
449         buf_width = src_info.planes[0].stride >> 2;
450         src_stride = src_info.planes[0].stride;
451         src_img = pixman_image_create_bits(src_format, buf_width, src_info.height,
452                                                                            (uint32_t*)src_info.planes[0].ptr, src_stride);
453         TDM_GOTO_IF_FAIL(src_img, unref_img);
454
455         buf_width = dst_info.planes[0].stride >> 2;
456         dst_stride = dst_info.planes[0].stride;
457         dst_img = pixman_image_create_bits(dst_format, buf_width, dst_info.height,
458                                                                            (uint32_t*)dst_info.planes[0].ptr, dst_stride);
459         TDM_GOTO_IF_FAIL(dst_img, unref_img);
460
461         pixman_f_transform_init_identity(&ft);
462
463         if (transform & TDM_TRANSFORM_FLIPPED) {
464                 pixman_f_transform_scale(&ft, NULL, -1, 1);
465                 pixman_f_transform_translate(&ft, NULL, dstpos->w, 0);
466         }
467
468         rotate_step = transform & 0x3;
469         if (rotate_step > 0) {
470                 int c, s, tx = 0, ty = 0;
471                 switch (rotate_step) {
472                 case 1:
473                         c = 0, s = -1, tx = -dstpos->w;
474                         break;
475                 case 2:
476                         c = -1, s = 0, tx = -dstpos->w, ty = -dstpos->h;
477                         break;
478                 case 3:
479                         c = 0, s = 1, ty = -dstpos->h;
480                         break;
481                 }
482                 pixman_f_transform_translate(&ft, NULL, tx, ty);
483                 pixman_f_transform_rotate(&ft, NULL, c, s);
484         }
485
486         if (rotate_step % 2 == 0) {
487                 scale_x = (double)srcpos->w / dstpos->w;
488                 scale_y = (double)srcpos->h / dstpos->h;
489         } else {
490                 scale_x = (double)srcpos->w / dstpos->h;
491                 scale_y = (double)srcpos->h / dstpos->w;
492         }
493
494         pixman_f_transform_scale(&ft, NULL, scale_x, scale_y);
495         pixman_f_transform_translate(&ft, NULL, srcpos->x, srcpos->y);
496         pixman_transform_from_pixman_f_transform(&t, &ft);
497         pixman_image_set_transform(src_img, &t);
498
499         op = (!over) ? PIXMAN_OP_SRC : PIXMAN_OP_OVER;
500
501         pixman_image_composite(op, src_img, NULL, dst_img, 0, 0, 0, 0,
502                                                    dstpos->x, dstpos->y, dstpos->w, dstpos->h);
503
504         ret = TDM_ERROR_NONE;
505
506 unref_img:
507         if (src_img)
508                 pixman_image_unref(src_img);
509         if (dst_img)
510                 pixman_image_unref(dst_img);
511 unmap_dstbuf:
512         tbm_surface_unmap(dstbuf);
513 unmap_srcbuf:
514         tbm_surface_unmap(srcbuf);
515
516         return ret;
517 }
518
519 EXTERN int
520 tdm_helper_get_fd(const char *env)
521 {
522         const char *value;
523         int fd, newfd, flags, ret;
524         char *end;
525         errno = 0;
526
527         value = (const char*)getenv(env);
528         if (!value)
529                 return -1;
530
531         const long sl = strtol(value, &end, 10);
532         if (end == value) {
533                 TDM_ERR("%s: not a decimal number\n", value);
534                 return -1;
535         } else if (*end != '\0') {
536                 TDM_ERR("%s: extra characters at end of input: %s\n", value, end);
537                 return -1;
538         } else if ((sl == LONG_MIN || sl == LONG_MAX) && errno == ERANGE) {
539                 TDM_ERR("%s out of range of type long\n", value);
540                 return -1;
541         } else if (sl > INT_MAX) {
542                 TDM_ERR("%ld greater than INT_MAX\n", sl);
543                 return -1;
544         } else if (sl < INT_MIN) {
545                 TDM_ERR("%ld less than INT_MIN\n", sl);
546                 return -1;
547         } else {
548                 fd = (int)sl;
549                 if (fd < 0) {
550                         TDM_ERR("%d out of fd range\n", fd);
551                         return -1;
552                 }
553         }
554
555         flags = fcntl(fd, F_GETFD);
556         if (flags == -1) {
557                 TDM_ERR("fcntl failed: %m");
558                 return -1;
559         }
560
561         newfd = dup(fd);
562         if (newfd < 0) {
563                 TDM_ERR("dup failed: %m");
564                 return -1;
565         }
566
567         TDM_INFO("%s: fd(%d) newfd(%d)", env, fd, newfd);
568
569         ret = fcntl(newfd, F_SETFD, flags | FD_CLOEXEC);
570         if (ret == -1) {
571                 TDM_ERR("fcntl failed: %m");
572                 close(newfd);
573                 return -1;
574         }
575
576         return newfd;
577 }
578
579 EXTERN void
580 tdm_helper_set_fd(const char *env, int fd)
581 {
582         char buf[32];
583         int ret;
584
585         snprintf(buf, sizeof(buf), "%d", fd);
586
587         ret = setenv(env, (const char*)buf, 1);
588         if (ret) {
589                 TDM_ERR("setenv failed: %m");
590                 return;
591         }
592
593         if (fd >= 0)
594                 TDM_INFO("%s: fd(%d)", env, fd);
595 }
596
597 EXTERN void
598 tdm_helper_dump_start(char *dumppath, int *count)
599 {
600         if (dumppath == NULL || count == NULL) {
601                 TDM_DBG("tdm_helper_dump dumppath or count is null.");
602                 return;
603         }
604
605         tdm_dump_enable = 1;
606
607         TDM_DBG("tdm_helper_dump start.(path : %s)", dumppath);
608 }
609
610 EXTERN void
611 tdm_helper_dump_stop(void)
612 {
613         tdm_dump_enable = 0;
614
615         TDM_DBG("tdm_helper_dump stop.");
616 }
617
618 static tdm_error
619 _tdm_helper_buffer_convert(tbm_surface_h srcbuf, tbm_surface_h dstbuf,
620                                                    int dx, int dy, int dw, int dh, int count)
621 {
622         pixman_image_t *src_img = NULL, *dst_img = NULL;
623         pixman_format_code_t src_format, dst_format;
624         pixman_transform_t t;
625         struct pixman_f_transform ft;
626         pixman_op_t op;
627         tbm_surface_info_s src_info = {0, };
628         tbm_surface_info_s dst_info = {0, };
629         int stride, width;
630         double scale_x, scale_y;
631
632         TDM_RETURN_VAL_IF_FAIL(srcbuf != NULL, TDM_ERROR_INVALID_PARAMETER);
633         TDM_RETURN_VAL_IF_FAIL(dstbuf != NULL, TDM_ERROR_INVALID_PARAMETER);
634
635         if (tbm_surface_map(srcbuf, TBM_SURF_OPTION_READ, &src_info) != TBM_SURFACE_ERROR_NONE) {
636                 TDM_ERR("cannot mmap srcbuf\n");
637                 return TDM_ERROR_OPERATION_FAILED;
638         }
639
640         if (tbm_surface_map(dstbuf, TBM_SURF_OPTION_WRITE, &dst_info) != TBM_SURFACE_ERROR_NONE) {
641                 TDM_ERR("cannot mmap dstbuf\n");
642                 tbm_surface_unmap(srcbuf);
643                 return TDM_ERROR_OPERATION_FAILED;
644         }
645         TDM_GOTO_IF_FAIL(src_info.num_planes == 1, cant_convert);
646         TDM_GOTO_IF_FAIL(dst_info.num_planes == 1, cant_convert);
647
648         /* src */
649         src_format = _tdm_helper_pixman_format_get(src_info.format);
650         TDM_GOTO_IF_FAIL(src_format > 0, cant_convert);
651
652         width = src_info.planes[0].stride / 4;
653         stride = src_info.planes[0].stride;
654         src_img = pixman_image_create_bits(src_format, width, src_info.height,
655                                                                            (uint32_t*)src_info.planes[0].ptr, stride);
656         TDM_GOTO_IF_FAIL(src_img != NULL, cant_convert);
657
658         /* dst */
659         dst_format = _tdm_helper_pixman_format_get(dst_info.format);
660         TDM_GOTO_IF_FAIL(dst_format > 0, cant_convert);
661
662         width = dst_info.planes[0].stride / 4;
663         stride = dst_info.planes[0].stride;
664         dst_img = pixman_image_create_bits(dst_format, width, dst_info.height,
665                                                                            (uint32_t*)dst_info.planes[0].ptr, stride);
666         TDM_GOTO_IF_FAIL(dst_img != NULL, cant_convert);
667
668         pixman_f_transform_init_identity(&ft);
669
670         scale_x = (double)src_info.width / dw;
671         scale_y = (double)src_info.height / dh;
672
673         pixman_f_transform_scale(&ft, NULL, scale_x, scale_y);
674         pixman_f_transform_translate(&ft, NULL, 0, 0);
675         pixman_transform_from_pixman_f_transform(&t, &ft);
676         pixman_image_set_transform(src_img, &t);
677
678         if (count == 0)
679                 op = PIXMAN_OP_SRC;
680         else
681                 op = PIXMAN_OP_OVER;
682
683         pixman_image_composite(op, src_img, NULL, dst_img,
684                                                    0, 0, 0, 0, dx, dy, dw, dh);
685
686         if (src_img)
687                 pixman_image_unref(src_img);
688         if (dst_img)
689                 pixman_image_unref(dst_img);
690
691         tbm_surface_unmap(srcbuf);
692         tbm_surface_unmap(dstbuf);
693
694         return TDM_ERROR_NONE;
695
696 cant_convert:
697         if (src_img)
698                 pixman_image_unref(src_img);
699
700         tbm_surface_unmap(srcbuf);
701         tbm_surface_unmap(dstbuf);
702
703         return TDM_ERROR_OPERATION_FAILED;
704 }
705
706 EXTERN tdm_error
707 tdm_helper_capture_output(tdm_output *output, tbm_surface_h dst_buffer,
708                                                   int x, int y, int w, int h,
709                                                   tdm_helper_capture_handler func, void *data)
710 {
711         tbm_surface_h surface;
712         tdm_error err;
713         int i, count, first = 0;
714
715         TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
716         TDM_RETURN_VAL_IF_FAIL(dst_buffer != NULL, TDM_ERROR_INVALID_PARAMETER);
717         TDM_RETURN_VAL_IF_FAIL(x >= 0, TDM_ERROR_INVALID_PARAMETER);
718         TDM_RETURN_VAL_IF_FAIL(y >= 0, TDM_ERROR_INVALID_PARAMETER);
719         TDM_RETURN_VAL_IF_FAIL(w >= 0, TDM_ERROR_INVALID_PARAMETER);
720         TDM_RETURN_VAL_IF_FAIL(h >= 0, TDM_ERROR_INVALID_PARAMETER);
721         TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
722         TDM_RETURN_VAL_IF_FAIL(data != NULL, TDM_ERROR_INVALID_PARAMETER);
723
724         err = tdm_output_get_layer_count(output, &count);
725         if (err != TDM_ERROR_NONE) {
726                 TDM_ERR("tdm_output_get_layer_count fail(%d)\n", err);
727                 return TDM_ERROR_OPERATION_FAILED;
728         }
729         if (count <= 0) {
730                 TDM_ERR("tdm_output_get_layer_count err(%d, %d)\n", err, count);
731                 return TDM_ERROR_BAD_MODULE;
732         }
733
734         for (i = count - 1; i >= 0; i--) {
735                 tdm_layer *layer = tdm_output_get_layer(output, i, NULL);
736
737                 surface = tdm_layer_get_displaying_buffer(layer, &err);
738                 if (err != TDM_ERROR_NONE)
739                         continue;
740
741                 err = _tdm_helper_buffer_convert(surface, dst_buffer, x, y, w, h, first++);
742                 if (err != TDM_ERROR_NONE)
743                         TDM_DBG("convert fail %d-layer buffer\n", i);
744                 else
745                         TDM_DBG("convert success %d-layer buffer\n", i);
746         }
747
748         func(dst_buffer, data);
749
750         return TDM_ERROR_NONE;
751 }
752
753 EXTERN void
754 tdm_helper_get_display_information(tdm_display *dpy, char *reply, int *len)
755 {
756         tdm_private_display *private_display;
757         tdm_backend_module *module_data;
758         tdm_func_output *func_output;
759         tdm_func_layer *func_layer;
760         tdm_private_output *private_output = NULL;
761         tdm_private_layer *private_layer = NULL;
762         tdm_private_pp *private_pp = NULL;
763         tdm_private_capture *private_capture = NULL;
764         tdm_error ret;
765         int i;
766
767         TDM_DBG_RETURN_IF_FAIL(dpy != NULL);
768
769         private_display = dpy;
770         func_output = &private_display->func_output;
771         func_layer = &private_display->func_layer;
772         _pthread_mutex_lock(&private_display->lock);
773
774         /* module information */
775         module_data = private_display->module_data;
776         TDM_SNPRINTF(reply, len, "[TDM backend information]\n");
777         TDM_SNPRINTF(reply, len, "name: %s\n", module_data->name);
778         TDM_SNPRINTF(reply, len, "vendor: %s\n", module_data->vendor);
779         TDM_SNPRINTF(reply, len, "version: %d.%d\n\n",
780                                  (int)TDM_BACKEND_GET_ABI_MAJOR(module_data->abi_version),
781                                  (int)TDM_BACKEND_GET_ABI_MINOR(module_data->abi_version));
782
783         /* output information */
784         TDM_SNPRINTF(reply, len, "[Output information]\n");
785         TDM_SNPRINTF(reply, len, "--------------------------------------------------------------------------------------------\n");
786         TDM_SNPRINTF(reply, len, "idx   maker   model   name   type   status   dpms   subpixel   align_w   min   max   phy(mm)\n");
787         TDM_SNPRINTF(reply, len, "--------------------------------------------------------------------------------------------\n");
788         LIST_FOR_EACH_ENTRY(private_output, &private_display->output_list, link) {
789                 TDM_SNPRINTF(reply, len, "%d   %s   %s   %s   %s   %s   %s   %u   %d   %dx%d   %dx%d   %ux%u\n",
790                                          private_output->index, private_output->caps.maker,
791                                          private_output->caps.model, private_output->caps.name,
792                                          tdm_conn_str(private_output->caps.type),
793                                          tdm_status_str(private_output->caps.status),
794                                          tdm_dpms_str(private_output->current_dpms_value),
795                                          private_output->caps.subpixel,
796                                          TDM_FRONT_VALUE(private_output->caps.preferred_align),
797                                          TDM_FRONT_VALUE(private_output->caps.min_w),
798                                          TDM_FRONT_VALUE(private_output->caps.min_h),
799                                          TDM_FRONT_VALUE(private_output->caps.max_w),
800                                          TDM_FRONT_VALUE(private_output->caps.max_h),
801                                          private_output->caps.mmWidth, private_output->caps.mmHeight);
802
803                 TDM_SNPRINTF(reply, len, "\t%u modes:\n", private_output->caps.mode_count);
804
805                 if (private_output->caps.mode_count > 0) {
806                         const tdm_output_mode *current_mode = NULL;
807
808                         TDM_DBG_GOTO_IF_FAIL(func_output->output_get_mode, unlock);
809                         ret = func_output->output_get_mode(private_output->output_backend, &current_mode);
810                         TDM_DBG_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, unlock);
811
812                         TDM_SNPRINTF(reply, len, "\t\t name refresh (Hz) clk hdisp hss hse htot vdisp vss vse vtot vscan\n");
813                         for (i = 0; i < private_output->caps.mode_count; i++) {
814                                 char *current = (current_mode == private_output->caps.modes + i) ? "*" : " ";
815                                 TDM_SNPRINTF(reply, len, "\t\t%s%s %u %u %u %u %u %u %u %u %u %u %u ",
816                                                          current,
817                                                          private_output->caps.modes[i].name,
818                                                          private_output->caps.modes[i].vrefresh,
819                                                          private_output->caps.modes[i].clock,
820                                                          private_output->caps.modes[i].hdisplay,
821                                                          private_output->caps.modes[i].hsync_start,
822                                                          private_output->caps.modes[i].hsync_end,
823                                                          private_output->caps.modes[i].htotal,
824                                                          private_output->caps.modes[i].vdisplay,
825                                                          private_output->caps.modes[i].vsync_start,
826                                                          private_output->caps.modes[i].vsync_end,
827                                                          private_output->caps.modes[i].vtotal,
828                                                          private_output->caps.modes[i].vscan);
829                                 tdm_mode_flag_str(private_output->caps.modes[i].flags, &reply, len);
830                                 TDM_SNPRINTF(reply, len, " ");
831                                 tdm_mode_type_str(private_output->caps.modes[i].type, &reply, len);
832                                 TDM_SNPRINTF(reply, len, "\n");
833                         }
834                 }
835
836                 TDM_SNPRINTF(reply, len, "\t%d properties:\n", private_output->caps.prop_count);
837                 if (private_output->caps.prop_count > 0) {
838                         TDM_SNPRINTF(reply, len, "\t\tname\tidx\tvalue\n");
839                         for (i = 0; i < private_output->caps.prop_count; i++) {
840                                 tdm_value value;
841                                 TDM_DBG_GOTO_IF_FAIL(func_output->output_get_property, unlock);
842                                 ret = func_output->output_get_property(private_output->output_backend,
843                                                                                                            private_output->caps.props[i].id,
844                                                                                                            &value);
845                                 TDM_DBG_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, unlock);
846                                 TDM_SNPRINTF(reply, len, "\t\t%s\t%u\t%u\n",
847                                                          private_output->caps.props[i].name,
848                                                          private_output->caps.props[i].id,
849                                                          value.u32);
850                         }
851                 }
852         }
853         TDM_SNPRINTF(reply, len, "\n");
854
855         /* layer information */
856         TDM_SNPRINTF(reply, len, "[Layer information]\n");
857         TDM_SNPRINTF(reply, len, "-----------------------------------------------------------------------\n");
858         TDM_SNPRINTF(reply, len, "idx   output   zpos   buf   format   size   crop   geometry   transform\n");
859         TDM_SNPRINTF(reply, len, "-----------------------------------------------------------------------\n");
860         LIST_FOR_EACH_ENTRY(private_output, &private_display->output_list, link) {
861                 LIST_FOR_EACH_ENTRY(private_layer, &private_output->layer_list, link) {
862                         if (!private_layer->usable) {
863                                 tdm_info_layer info;
864                                 unsigned int format;
865                                 tdm_size size;
866                                 tbm_surface_info_s buf_info;
867
868                                 TDM_DBG_GOTO_IF_FAIL(func_layer->layer_get_info, unlock);
869                                 memset(&info, 0, sizeof info);
870                                 ret = func_layer->layer_get_info(private_layer->layer_backend, &info);
871                                 TDM_DBG_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, unlock);
872
873                                 if (!private_layer->showing_buffer)
874                                         continue;
875
876                                 format = tbm_surface_get_format(private_layer->showing_buffer->buffer);
877                                 tbm_surface_get_info(private_layer->showing_buffer->buffer, &buf_info);
878
879                                 if (IS_RGB(format))
880                                         size.h = buf_info.planes[0].stride >> 2;
881                                 else
882                                         size.h = buf_info.planes[0].stride;
883                                 size.v = tbm_surface_get_height(private_layer->showing_buffer->buffer);
884
885                                 if (info.src_config.format)
886                                         format = (info.src_config.format) ? : format;
887
888                                 TDM_SNPRINTF(reply, len, "%d   %d   %d   %p   %c%c%c%c   %ux%u   %ux%u+%u+%u   %ux%u+%u+%u   %s\n",
889                                                          private_layer->index,
890                                                          private_output->index,
891                                                          private_layer->caps.zpos,
892                                                          private_layer->showing_buffer->buffer, FOURCC_STR(format), size.h, size.v,
893                                                          info.src_config.pos.w, info.src_config.pos.h, info.src_config.pos.x, info.src_config.pos.y,
894                                                          info.dst_pos.w, info.dst_pos.h, info.dst_pos.x, info.dst_pos.y,
895                                                          tdm_transform_str(info.transform));
896                         } else {
897                                 TDM_SNPRINTF(reply, len, "%d   %d   %d   -\n",
898                                                          private_layer->index,
899                                                          private_output->index,
900                                                          private_layer->caps.zpos);
901                         }
902
903                         TDM_SNPRINTF(reply, len, "\tcaps\t: ");
904                         tdm_layer_caps_str(private_layer->caps.capabilities, &reply, len);
905                         TDM_SNPRINTF(reply, len, "\n");
906
907                         TDM_SNPRINTF(reply, len, "\tformats\t: ");
908                         if (private_layer->caps.format_count > 0) {
909                                 const char *sep = "";
910                                 for (i = 0; i < private_layer->caps.format_count; i++) {
911                                         if (private_layer->caps.formats[i] == 0)
912                                                 continue;
913                                         TDM_SNPRINTF(reply, len, "%s%c%c%c%c", sep, FOURCC_STR(private_layer->caps.formats[i]));
914                                         sep = ",";
915                                 }
916                                 TDM_SNPRINTF(reply, len, "\n");
917                         }
918
919                         TDM_SNPRINTF(reply, len, "\t%u properties:\n", private_layer->caps.prop_count);
920                         if (private_layer->caps.prop_count > 0) {
921                                 TDM_SNPRINTF(reply, len, "\t\tname\tidx\tvalue\n");
922                                 for (i = 0; i < private_layer->caps.prop_count; i++) {
923                                         tdm_value value;
924                                         TDM_DBG_GOTO_IF_FAIL(func_layer->layer_get_property, unlock);
925                                         ret = func_layer->layer_get_property(private_layer->layer_backend,
926                                                                                                                  private_layer->caps.props[i].id,
927                                                                                                                  &value);
928                                         TDM_DBG_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, unlock);
929                                         TDM_SNPRINTF(reply, len, "\t\t%s\t%u\t%u\n",
930                                                                  private_layer->caps.props[i].name,
931                                                                  private_layer->caps.props[i].id,
932                                                                  value.u32);
933                                 }
934                         }
935                 }
936         }
937         TDM_SNPRINTF(reply, len, "\n");
938
939         if (private_display->capabilities & TDM_DISPLAY_CAPABILITY_PP) {
940                 const char *sep = "";
941                 TDM_SNPRINTF(reply, len, "[PP information]\n");
942                 TDM_SNPRINTF(reply, len, "caps\t: ");
943                 tdm_pp_caps_str(private_display->caps_pp.capabilities, &reply, len);
944                 TDM_SNPRINTF(reply, len, "\n");
945                 TDM_SNPRINTF(reply, len, "formats\t: ");
946                 for (i = 0; i < private_display->caps_pp.format_count; i++) {
947                         if (private_display->caps_pp.formats[i] == 0)
948                                 continue;
949                         TDM_SNPRINTF(reply, len, "%s%c%c%c%c", sep, FOURCC_STR(private_display->caps_pp.formats[i]));
950                         sep = ",";
951                 }
952                 TDM_SNPRINTF(reply, len, "\n");
953                 TDM_SNPRINTF(reply, len, "size\t: min(%dx%d) max(%dx%d) align_w(%d)\n",
954                                          TDM_FRONT_VALUE(private_display->caps_pp.min_w),
955                                          TDM_FRONT_VALUE(private_display->caps_pp.min_h),
956                                          TDM_FRONT_VALUE(private_display->caps_pp.max_w),
957                                          TDM_FRONT_VALUE(private_display->caps_pp.max_h),
958                                          TDM_FRONT_VALUE(private_display->caps_pp.preferred_align));
959                 if (!LIST_IS_EMPTY(&private_display->pp_list)) {
960                         TDM_SNPRINTF(reply, len, "-------------------------------------------------------------\n");
961                         TDM_SNPRINTF(reply, len, "src(format size crop)  |  dst(format size crop)  |  transform\n");
962                         TDM_SNPRINTF(reply, len, "-------------------------------------------------------------\n");
963                         LIST_FOR_EACH_ENTRY(private_pp, &private_display->pp_list, link) {
964                                 TDM_SNPRINTF(reply, len, "%c%c%c%c %ux%u %ux%u+%u+%u | %c%c%c%c %ux%u %ux%u+%u+%u | %s\n",
965                                                          FOURCC_STR(private_pp->info.src_config.format),
966                                                          private_pp->info.src_config.size.h,
967                                                          private_pp->info.src_config.size.v,
968                                                          private_pp->info.src_config.pos.x, private_pp->info.src_config.pos.y,
969                                                          private_pp->info.src_config.pos.w, private_pp->info.src_config.pos.h,
970                                                          FOURCC_STR(private_pp->info.dst_config.format),
971                                                          private_pp->info.dst_config.size.h,
972                                                          private_pp->info.dst_config.size.v,
973                                                          private_pp->info.dst_config.pos.x, private_pp->info.dst_config.pos.y,
974                                                          private_pp->info.dst_config.pos.w, private_pp->info.dst_config.pos.h,
975                                                          tdm_transform_str(private_pp->info.transform));
976                         }
977                 }
978         } else {
979                 TDM_SNPRINTF(reply, len, "[No PP capability]\n");
980         }
981         TDM_SNPRINTF(reply, len, "\n");
982
983         if (private_display->capabilities & TDM_DISPLAY_CAPABILITY_CAPTURE) {
984                 const char *sep = "";
985                 TDM_SNPRINTF(reply, len, "[Capture information]\n");
986                 TDM_SNPRINTF(reply, len, "caps\t: ");
987                 tdm_capture_caps_str(private_display->caps_capture.capabilities, &reply, len);
988                 TDM_SNPRINTF(reply, len, "\n");
989                 TDM_SNPRINTF(reply, len, "formats\t: ");
990                 for (i = 0; i < private_display->caps_capture.format_count; i++) {
991                         if (private_display->caps_capture.formats[i] == 0)
992                                 continue;
993                         TDM_SNPRINTF(reply, len, "%s%c%c%c%c", sep, FOURCC_STR(private_display->caps_capture.formats[i]));
994                         sep = ",";
995                 }
996                 TDM_SNPRINTF(reply, len, "\n");
997                 TDM_SNPRINTF(reply, len, "size\t: min(%dx%d) max(%dx%d) align_w(%d)\n",
998                                          TDM_FRONT_VALUE(private_display->caps_capture.min_w),
999                                          TDM_FRONT_VALUE(private_display->caps_capture.min_h),
1000                                          TDM_FRONT_VALUE(private_display->caps_capture.max_w),
1001                                          TDM_FRONT_VALUE(private_display->caps_capture.max_h),
1002                                          TDM_FRONT_VALUE(private_display->caps_capture.preferred_align));
1003                 if (!LIST_IS_EMPTY(&private_display->capture_list)) {
1004                         TDM_SNPRINTF(reply, len, "-----------------------------------\n");
1005                         TDM_SNPRINTF(reply, len, "dst(format size crop)  |  transform\n");
1006                         TDM_SNPRINTF(reply, len, "-----------------------------------\n");
1007                         LIST_FOR_EACH_ENTRY(private_capture, &private_display->capture_list, link) {
1008                                 TDM_SNPRINTF(reply, len, "%c%c%c%c %ux%u %ux%u+%u+%u | %s\n",
1009                                                          FOURCC_STR(private_capture->info.dst_config.format),
1010                                                          private_capture->info.dst_config.size.h,
1011                                                          private_capture->info.dst_config.size.v,
1012                                                          private_capture->info.dst_config.pos.x, private_capture->info.dst_config.pos.y,
1013                                                          private_capture->info.dst_config.pos.w, private_capture->info.dst_config.pos.h,
1014                                                          tdm_transform_str(private_capture->info.transform));
1015                         }
1016                 }
1017         } else {
1018                 TDM_SNPRINTF(reply, len, "[No Capture capability]\n");
1019         }
1020         TDM_SNPRINTF(reply, len, "\n");
1021
1022 unlock:
1023         _pthread_mutex_unlock(&private_display->lock);
1024 }
1025
1026 EXTERN int
1027 tdm_helper_commit_per_vblank_enabled(tdm_display *dpy)
1028 {
1029         tdm_private_display *private_display;
1030
1031         TDM_RETURN_VAL_IF_FAIL(dpy != NULL, 0);
1032
1033         private_display = dpy;
1034
1035         return private_display->commit_per_vblank;
1036 }
1037