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