test: remove sig handler to avoid deadlock
[platform/core/uifw/libtdm.git] / tools / tdm_test_server.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 #include <stdio.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <signal.h>
40 #include <poll.h>
41 #include <errno.h>
42 #include <time.h>
43 #include <stdint.h>
44
45 #include <tbm_surface.h>
46 #include <tbm_surface_internal.h>
47
48 #include <tdm.h>
49 #include <tdm_log.h>
50 #include <tdm_list.h>
51 #include <tdm_helper.h>
52 #include <tdm_backend.h>
53
54 #include "tdm_macro.h"
55 #include "tdm_private.h"
56 #include "buffers.h"
57
58 ////////////////////////////////////////////////////////////////////////////////
59 struct typestrings {
60         int type;
61         const char *string;
62         const char *desc;
63 };
64
65 struct optstrings {
66         int  type;
67         const char *opt;
68         const char *desc;
69         const char *arg;
70         const char *ex;
71 };
72
73 enum {
74         OPT_QRY,
75         OPT_TST,
76         OPT_GEN,
77 };
78
79 static struct typestrings typestrs[] = {
80         {OPT_QRY, "Query",          NULL},
81         {OPT_TST, "Test",           NULL},
82         {OPT_GEN, "General",        NULL},
83 };
84
85 static struct optstrings optstrs[] = {
86         {
87                 OPT_QRY, "q", "show tdm output, layer information",
88                 NULL, NULL
89         },
90         {
91                 OPT_TST, "a", "set all layer objects for all connected outputs",
92                 NULL, NULL
93         },
94         {
95                 OPT_TST, "o", "set a mode for a output object",
96                 "<output_idx>@<mode>[&<refresh>]", "0@1920x1080"
97         },
98         {
99                 OPT_TST, "l", "set a layer object",
100                 "<layer_idx>[:<w>x<h>[+<x>+<y>][,<h>x<v>][@<format>]]~<w>x<h>[+<x>+<y>][*<transform>]", NULL
101         },
102         {
103                 OPT_TST, "p", "set a PP object.\n\t\t'-l' is used to show the result on screen.",
104                 "<w>x<h>[+<x>+<y>][,<h>x<v>][@<format>]~<w>x<h>[+<x>+<y>][,<h>x<v>][@<format>][*<transform>][&<fps>]", NULL
105         },
106         {
107                 OPT_TST, "c", "catpure a output object or a layer object.\n\t\t'-l' is used to show the result on screen.",
108                 "<output_idx>[,<layer_idx>]~<w>x<h>[+<x>+<y>][,<h>x<v>][@<format>][*<transform>][^stream]", NULL
109         },
110         {
111                 OPT_GEN, "w", "set the property of a object",
112                 "<prop_name>:<value>", NULL
113         },
114         {
115                 OPT_GEN, "b", "set the fill(smtpe,tiles,plain) and framebuffer type(scanout,noncachable,wc)",
116                 "<fill>[:<buf_flag>[,<buf_flag2>[,...]]]", NULL
117         },
118         {
119                 OPT_GEN, "v", "update layers every vblank",
120                 NULL, NULL
121         },
122 };
123
124 struct usagestring {
125         const char *string;
126         const char *desc;
127 };
128
129 static struct usagestring usages[] = {
130         {
131                 "-q",
132                 NULL
133         },
134         {
135                 "-a -b plain",
136                 "test all outputs, layers with plain buffers"
137         },
138         {
139                 "-o 0@1920x1080",
140                 "Set the \"1920x1080\" mode to the output 0. And show a buffer via a primary layer of the output 0"
141         },
142         {
143                 "-o 0@1920x1080 -l 1~640x480+50+100",
144                 "Create the 640x480 buffer and show it in the (50,100) pos of screen via the layer 1"
145         },
146         {
147                 "-p 320x240@NV12~480x360+80+40,640x480@AR24 -l 1~640x480+50+100",
148                 "Convert the 320x240@NV12 buffer to the 640x480@AR24 buffer(480x360+80+40) and show the result via the layer 1"
149         },
150 };
151
152 static void
153 usage(char *app_name)
154 {
155         int type_size = sizeof(typestrs) / sizeof(struct typestrings);
156         int opt_size = sizeof(optstrs) / sizeof(struct optstrings);
157         int usages_size = sizeof(usages) / sizeof(struct usagestring);
158         int t;
159
160         printf("usage: %s \n\n", app_name);
161
162         for (t = 0; t < type_size; t++) {
163                 int o, f = 1;
164
165                 for (o = 0; o < opt_size; o++)
166                         if (optstrs[o].type == typestrs[t].type) {
167                                 if (f == 1)
168                                         printf(" %s options: %s\n\n", typestrs[t].string, (typestrs[t].desc) ? : "");
169                                 printf("\t-%s\t%s\n", optstrs[o].opt, optstrs[o].desc);
170                                 if (optstrs[o].arg)
171                                         printf("\t\t  %s\n", optstrs[o].arg);
172                                 if (optstrs[o].ex)
173                                         printf("\t\t  ex) %s\n", optstrs[o].ex);
174                                 f = 0;
175                         }
176                 printf("\n");
177         }
178
179         printf(" For example)\n\n");
180
181         for (t = 0; t < usages_size; t++) {
182                 printf("    $ %s %s\n", app_name, usages[t].string);
183                 printf("\t%s\n", usages[t].desc);
184         }
185         printf("\n");
186         exit(0);
187 }
188
189 ////////////////////////////////////////////////////////////////////////////////
190
191 static const char *tdm_buf_flag_names[] = {
192         "scanout",
193         "noncachable",
194         "wc",
195 };
196 TDM_BIT_NAME_FB(buf_flag)
197
198 #define DEFAULT_FORMAT  TBM_FORMAT_ARGB8888
199
200 #define print_size(s) \
201         printf("%dx%d", (s)->h, (s)->v)
202 #define print_pos(p) \
203         printf("%dx%d+%d+%d", (p)->w, (p)->h, (p)->x, (p)->y)
204 #define print_format(f) \
205         if (f) printf("%c%c%c%c", FOURCC_STR(f)); \
206         else printf("NONE")
207 #define print_config(c) \
208         do { \
209                 print_size(&(c)->size); \
210                 printf(" "); \
211                 print_pos(&(c)->pos); \
212                 printf(" "); \
213                 print_format((c)->format); \
214         } while (0)
215 #define print_prop(w) \
216         printf("%s(%d)", (w)->name, ((w)->value).u32)
217
218 typedef struct _tdm_test_server tdm_test_server;
219 typedef struct _tdm_test_server_layer tdm_test_server_layer;
220 typedef struct _tdm_test_server_capture tdm_test_server_capture;
221
222 typedef struct _tdm_test_server_prop {
223         /* args */
224         char name[TDM_NAME_LEN];
225         tdm_value value;
226
227         /* variables for test */
228         struct list_head link;
229 } tdm_test_server_prop;
230
231 typedef struct _tdm_test_server_buffer {
232         /* variables for test */
233         tbm_surface_h b;
234         int in_use;
235         tdm_test_server_layer *l;
236
237         tdm_test_server_capture *c;
238         tdm_buffer_release_handler done;
239 } tdm_test_server_buffer;
240
241 typedef struct _tdm_test_server_output {
242         /* args */
243         int idx;
244         char mode[TDM_NAME_LEN];
245         int refresh;
246
247         /* variables for test */
248         struct list_head link;
249         struct list_head prop_list;
250         struct list_head layer_list;
251         tdm_test_server *data;
252         tdm_output *output;
253
254         int fill_primary_layer;
255 } tdm_test_server_output;
256
257 typedef struct _tdm_test_server_pp {
258         /* args */
259         tdm_info_pp info;
260         int fps;
261
262         /* variables for test */
263         struct list_head link;
264         tdm_test_server *data;
265         tdm_test_server_layer *l;
266         tdm_pp *pp;
267         tbm_surface_h bufs[6];
268         int buf_idx;
269
270         tdm_event_loop_source *timer_source;
271 } tdm_test_server_pp;
272
273 struct _tdm_test_server_capture {
274         /* args */
275         int output_idx;
276         int layer_idx;
277         tdm_info_capture info;
278
279         /* variables for test */
280         struct list_head link;
281         tdm_test_server *data;
282         tdm_test_server_layer *l;
283         tdm_capture *capture;
284 };
285
286 struct _tdm_test_server_layer {
287         /* args */
288         int idx;
289         tdm_info_layer info;
290
291         /* variables for test */
292         struct list_head link;
293         struct list_head prop_list;
294         tdm_test_server *data;
295         tdm_test_server_output *o;
296         tdm_layer *layer;
297         int is_primary;
298         tdm_test_server_pp *owner_p;
299         tdm_test_server_capture *owner_c;
300         tbm_surface_h bufs[3];
301         int buf_idx;
302 };
303
304 struct _tdm_test_server {
305         /* args */
306         int do_query;
307         int do_all;
308         int do_vblank;
309         int bflags;
310         int b_fill;
311
312         /* variables for test */
313         struct list_head output_list;
314         struct list_head pp_list;
315         struct list_head capture_list;
316         tdm_display *display;
317 };
318
319 static void run_test(tdm_test_server *data);
320 static void output_setup(tdm_test_server_output *o);
321 static void layer_show_buffer(tdm_test_server_layer *l, tbm_surface_h b);
322 static void capture_attach(tdm_test_server_capture *c, tbm_surface_h b);
323
324 static char*
325 parse_size(tdm_size *size, char *arg)
326 {
327         char *end;
328         size->h = strtol(arg, &end, 10);
329         TDM_EXIT_IF_FAIL(*end == 'x');
330         arg = end + 1;
331         size->v = strtol(arg, &end, 10);
332         return end;
333 }
334
335 static char*
336 parse_pos(tdm_pos *pos, char *arg)
337 {
338         char *end;
339         pos->w = strtol(arg, &end, 10);
340         TDM_EXIT_IF_FAIL(*end == 'x');
341         arg = end + 1;
342         pos->h = strtol(arg, &end, 10);
343         if (*end == '+') {
344                 arg = end + 1;
345                 pos->x = strtol(arg, &end, 10);
346                 TDM_EXIT_IF_FAIL(*end == '+');
347                 arg = end + 1;
348                 pos->y = strtol(arg, &end, 10);
349         }
350         return end;
351 }
352
353 static char*
354 parse_config(tdm_info_config *config, char *arg)
355 {
356         char *end;
357         end = parse_pos(&config->pos, arg);
358         if (*end == ',') {
359                 arg = end + 1;
360                 end = parse_size(&config->size, arg);
361         }
362         if (*end == '@') {
363                 char temp[32];
364                 arg = end + 1;
365                 end = strtostr(temp, 32, arg, TDM_DELIM);
366                 config->format = FOURCC_ID(temp);
367         }
368         return end;
369 }
370
371 static void
372 parse_arg_o(tdm_test_server_output *o, char *arg)
373 {
374         char *end;
375         TDM_EXIT_IF_FAIL(arg != NULL);
376         o->idx = strtol(arg, &end, 10);
377         TDM_EXIT_IF_FAIL(*end == '@');
378         arg = end + 1;
379         end = strtostr(o->mode, TDM_NAME_LEN, arg, TDM_DELIM);
380         if (*end == ',') {
381                 arg = end + 1;
382                 o->refresh = strtol(arg, &end, 10);
383         }
384 }
385
386 static void
387 parse_arg_p(tdm_test_server_pp *p, char *arg)
388 {
389         tdm_info_pp *pp_info = &p->info;
390         char *end;
391         TDM_EXIT_IF_FAIL(arg != NULL);
392         end = parse_config(&pp_info->src_config, arg);
393         TDM_EXIT_IF_FAIL(*end == '~');
394         arg = end + 1;
395         end = parse_config(&pp_info->dst_config, arg);
396         if (*end == '*') {
397                 arg = end + 1;
398                 pp_info->transform = strtol(arg, &end, 10);
399         }
400         if (*end == '&') {
401                 arg = end + 1;
402                 p->fps = strtol(arg, &end, 10);
403         }
404 }
405
406 static void
407 parse_arg_c(tdm_test_server_capture *c, char *arg)
408 {
409         tdm_info_capture *capture_info = &c->info;
410         char *end;
411         TDM_EXIT_IF_FAIL(arg != NULL);
412         c->output_idx = strtol(arg, &end, 10);
413         if (*end == ',') {
414                 arg = end + 1;
415                 c->layer_idx = strtol(arg, &end, 10);
416         }
417         TDM_EXIT_IF_FAIL(*end == '~');
418         arg = end + 1;
419         end = parse_config(&capture_info->dst_config, arg);
420         if (*end == '*') {
421                 arg = end + 1;
422                 capture_info->transform = strtol(arg, &end, 10);
423         }
424         if (*end == '^') {
425                 arg = end + 1;
426                 if (strtol(arg, &end, 10) > 0)
427                         capture_info->type = TDM_CAPTURE_TYPE_STREAM;
428                 else
429                         capture_info->type = TDM_CAPTURE_TYPE_ONESHOT;
430         }
431 }
432
433 static void
434 parse_arg_l(tdm_test_server_layer *l, char *arg)
435 {
436         tdm_info_layer *layer_info = &l->info;
437         char *end;
438         TDM_EXIT_IF_FAIL(arg != NULL);
439         l->idx = strtol(arg, &end, 10);
440         if (*end == ':') {
441                 arg = end + 1;
442                 end = parse_config(&layer_info->src_config, arg);
443         }
444         TDM_EXIT_IF_FAIL(*end == '~');
445         arg = end + 1;
446         end = parse_pos(&layer_info->dst_pos, arg);
447         if (*end == '*') {
448                 arg = end + 1;
449                 layer_info->transform = strtol(arg, &end, 10);
450         }
451 }
452
453 static void
454 parse_arg_w(tdm_test_server_prop *w, char *arg)
455 {
456         char *end;
457         TDM_EXIT_IF_FAIL(arg != NULL);
458         end = strtostr(w->name, TDM_PATH_LEN, arg, TDM_DELIM);
459         TDM_EXIT_IF_FAIL(*end == ':');
460         arg = end + 1;
461         w->value.u32 = strtol(arg, &end, 10);
462 }
463
464 static void
465 parse_arg_b(tdm_test_server *data, char *arg)
466 {
467         char *end = arg;
468         char temp[TDM_NAME_LEN] = {0,};
469         TDM_EXIT_IF_FAIL(arg != NULL);
470
471         end = strtostr(temp, 32, arg, TDM_DELIM);
472         if (!strncmp(temp, "smpte", 5))
473                 data->b_fill = PATTERN_SMPTE;
474         else if (!strncmp(temp, "tiles", 5))
475                 data->b_fill = PATTERN_TILES;
476         else if (!strncmp(temp, "plain", 5))
477                 data->b_fill = PATTERN_PLAIN;
478         else {
479                 printf("'%s': unknown flag\n", temp);
480                 exit(0);
481         }
482
483         if (*arg == ':') {
484                 data->bflags = 0;
485                 arg = end + 1;
486                 snprintf(temp, TDM_NAME_LEN, "%s", arg);
487                 arg = strtok_r(temp, ",", &end);
488                 while (arg) {
489                         if (!strncmp(arg, "default", 7))
490                                 printf("Ignore '%s' flag\n", arg);
491                         else if (!strncmp(arg, "scanout", 7))
492                                 data->bflags |= TBM_BO_SCANOUT;
493                         else if (!strncmp(arg, "noncachable", 11))
494                                 data->bflags |= TBM_BO_NONCACHABLE;
495                         else if (!strncmp(arg, "wc", 2))
496                                 data->bflags |= TBM_BO_WC;
497                         else {
498                                 printf("'%s': unknown flag\n", arg);
499                                 exit(0);
500                         }
501                         arg = strtok_r(NULL, ",", &end);
502                 }
503         }
504 }
505
506 static void
507 parse_args(tdm_test_server *data, int argc, char *argv[])
508 {
509         tdm_test_server_output *o = NULL;
510         tdm_test_server_layer *l = NULL;
511         tdm_test_server_pp *p = NULL;
512         tdm_test_server_capture *c = NULL;
513         tdm_test_server_prop *w = NULL;
514         void *last_option = NULL;
515         void *last_object = NULL;
516         int i;
517
518         if (argc < 2) {
519                 usage(argv[0]);
520                 exit(0);
521         }
522
523         for (i = 1; i < argc; i++) {
524                 if (!strncmp(argv[i] + 1, "q", 1)) {
525                         data->do_query = 1;
526                         return;
527                 } else if (!strncmp(argv[i] + 1, "a", 1)) {
528                         data->do_all = 1;
529                 } else if (!strncmp(argv[i] + 1, "o", 1)) {
530                         TDM_GOTO_IF_FAIL(data->do_all == 0, all);
531                         o = calloc(1, sizeof * o);
532                         TDM_EXIT_IF_FAIL(o != NULL);
533                         o->data = data;
534                         LIST_INITHEAD(&o->layer_list);
535                         LIST_INITHEAD(&o->prop_list);
536                         LIST_ADDTAIL(&o->link, &data->output_list);
537                         parse_arg_o(o, argv[++i]);
538                         last_object = o;
539                 } else if (!strncmp(argv[i] + 1, "p", 1)) {
540                         TDM_GOTO_IF_FAIL(data->do_all == 0, all);
541                         p = calloc(1, sizeof * p);
542                         TDM_EXIT_IF_FAIL(p != NULL);
543                         p->data = data;
544                         p->fps = 30;   /* default 30 fps */
545                         LIST_ADDTAIL(&p->link, &data->pp_list);
546                         parse_arg_p(p, argv[++i]);
547                         last_option = p;
548                 } else if (!strncmp(argv[i] + 1, "c", 1)) {
549                         TDM_GOTO_IF_FAIL(data->do_all == 0, all);
550                         c = calloc(1, sizeof * c);
551                         TDM_EXIT_IF_FAIL(c != NULL);
552                         c->data = data;
553                         c->output_idx = -1;
554                         c->layer_idx = -1;
555                         LIST_ADDTAIL(&c->link, &data->capture_list);
556                         parse_arg_c(c, argv[++i]);
557                         last_option = c;
558                 } else if (!strncmp(argv[i] + 1, "l", 1)) {
559                         TDM_GOTO_IF_FAIL(data->do_all == 0, all);
560                         if (!o) {
561                                 o = calloc(1, sizeof * o);
562                                 TDM_EXIT_IF_FAIL(o != NULL);
563                                 o->data = data;
564                                 LIST_INITHEAD(&o->layer_list);
565                                 LIST_INITHEAD(&o->prop_list);
566                                 LIST_ADDTAIL(&o->link, &data->output_list);
567                         }
568                         l = calloc(1, sizeof * l);
569                         TDM_EXIT_IF_FAIL(l != NULL);
570                         LIST_INITHEAD(&l->prop_list);
571                         LIST_ADDTAIL(&l->link, &o->layer_list);
572                         l->data = data;
573                         l->o = o;
574                         parse_arg_l(l, argv[++i]);
575                         if (p && last_option == p) {
576                                 p->l = l;
577                                 l->owner_p = p;
578                         } else if (c && last_option == c) {
579                                 c->l = l;
580                                 l->owner_c = c;
581                         }
582                         last_object = l;
583                 } else if (!strncmp(argv[i] + 1, "w", 1)) {
584                         TDM_GOTO_IF_FAIL(data->do_all == 0, all);
585                         if (!last_object)
586                                 goto no_object;
587                         w = calloc(1, sizeof * w);
588                         TDM_EXIT_IF_FAIL(w != NULL);
589                         if (o && last_object == o)
590                                 LIST_ADDTAIL(&w->link, &o->prop_list);
591                         else if (l && last_object == l)
592                                 LIST_ADDTAIL(&w->link, &l->prop_list);
593                         parse_arg_w(w, argv[++i]);
594                 } else if (!strncmp(argv[i] + 1, "b", 1)) {
595                         parse_arg_b(data, argv[++i]);
596                 } else if (!strncmp(argv[i] + 1, "v", 1)) {
597                         data->do_vblank = 1;
598                 } else {
599                         usage(argv[0]);
600                         exit(0);
601                 }
602         }
603
604         LIST_FOR_EACH_ENTRY(p, &data->pp_list, link) {
605                 if (!p->l)
606                         goto no_layer;
607         }
608         LIST_FOR_EACH_ENTRY(c, &data->capture_list, link) {
609                 if (!c->l)
610                         goto no_layer;
611         }
612
613         return;
614 no_layer:
615         printf("Use '-l' to set a layer for '-p' or '-c'.\n");
616         exit(0);
617 no_object:
618         printf("Use '-o' or '-l' to set a object first.\n");
619         exit(0);
620 all:
621         printf("Can't use '-%s' with '-a'.\n", argv[i] + 1);
622         exit(0);
623 }
624
625 static void
626 interpret_args(tdm_test_server *data)
627 {
628         tdm_test_server_output *o = NULL;
629         tdm_test_server_layer *l = NULL;
630         tdm_error ret;
631
632         /* create the objects of outputs */
633         if (data->do_all) {
634                 int i, output_count;
635
636                 ret = tdm_display_get_output_count(data->display, &output_count);
637                 TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
638
639                 for (i = 0; i < output_count; i++) {
640                         tdm_output *output = tdm_display_get_output(data->display, i, NULL);
641                         tdm_output_conn_status status;
642                         tdm_output_get_conn_status(output, &status);
643                         if (status == TDM_OUTPUT_CONN_STATUS_DISCONNECTED)
644                                 continue;
645                         o = calloc(1, sizeof * o);
646                         TDM_EXIT_IF_FAIL(o != NULL);
647                         o->data = data;
648                         LIST_INITHEAD(&o->layer_list);
649                         LIST_INITHEAD(&o->prop_list);
650                         LIST_ADDTAIL(&o->link, &data->output_list);
651                         o->idx = i;
652                         o->fill_primary_layer = 1;
653                 }
654         }
655
656         /* check if the primary layer object exists */
657         LIST_FOR_EACH_ENTRY(o, &data->output_list, link) {
658                 tdm_output *output;
659                 const tdm_output_mode *mode;
660                 int j, layer_count, primary_index = 0;
661                 tdm_test_server_layer *primary_l = NULL;
662
663                 output_setup(o);
664
665                 output = tdm_display_get_output(data->display, o->idx, &ret);
666                 TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
667                 ret = tdm_output_get_mode(output, &mode);
668                 TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
669                 ret = tdm_output_get_layer_count(output, &layer_count);
670                 TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
671                 ret = tdm_output_get_primary_index(output, &primary_index);
672                 TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
673
674                 if (LIST_IS_EMPTY(&o->layer_list))
675                         o->fill_primary_layer = 1;
676                 else {
677                         LIST_FOR_EACH_ENTRY(l, &o->layer_list, link) {
678                                 if (l->idx == primary_index) {
679                                         l->is_primary = 1;
680                                         o->fill_primary_layer = 1;
681                                         primary_l = l;
682                                         break;
683                                 }
684                         }
685                 }
686
687                 if (!primary_l || data->do_all) {
688                         for (j = 0; j < layer_count; j++) {
689                                 if (j != primary_index && !data->do_all)
690                                         continue;
691                                 l = calloc(1, sizeof * l);
692                                 TDM_EXIT_IF_FAIL(l != NULL);
693                                 LIST_INITHEAD(&l->prop_list);
694                                 LIST_ADDTAIL(&l->link, &o->layer_list);
695                                 l->data = data;
696                                 l->o = o;
697                                 l->idx = j;
698                                 if (j == primary_index) {
699                                         l->is_primary = 1;
700                                         l->info.dst_pos.w = mode->hdisplay;
701                                         l->info.dst_pos.h = mode->vdisplay;
702                                         primary_l = l;
703                                 } else {
704                                         l->info.dst_pos.w = TDM_ALIGN(mode->hdisplay / 3, 2);
705                                         l->info.dst_pos.h = TDM_ALIGN(mode->vdisplay / 3, 2);
706                                         l->info.dst_pos.x = TDM_ALIGN(((mode->hdisplay / 3) / layer_count) * j, 2);
707                                         l->info.dst_pos.y = TDM_ALIGN(((mode->vdisplay / 3) / layer_count) * j, 2);
708                                 }
709                         }
710                 }
711
712                 TDM_EXIT_IF_FAIL(primary_l != NULL);
713                 LIST_DEL(&primary_l->link);
714                 LIST_ADD(&primary_l->link, &o->layer_list);
715         }
716
717         /* fill the empty information of layers */
718         LIST_FOR_EACH_ENTRY(o, &data->output_list, link) {
719                 tdm_output *output;
720                 int minw, minh, maxw, maxh;
721
722                 output = tdm_display_get_output(data->display, o->idx, &ret);
723                 TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
724                 ret = tdm_output_get_available_size(output, &minw, &minh, &maxw, &maxh, NULL);
725                 TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
726
727                 /* l->info.src_config.size will be decided when a buffer shows really */
728                 LIST_FOR_EACH_ENTRY(l, &o->layer_list, link) {
729                         if (minw > 0 && minh > 0) {
730                                 TDM_EXIT_IF_FAIL(l->info.dst_pos.w >= minw);
731                                 TDM_EXIT_IF_FAIL(l->info.dst_pos.h >= minh);
732                         }
733                         if (maxw > 0 && maxh > 0) {
734                                 TDM_EXIT_IF_FAIL(l->info.dst_pos.w <= maxw);
735                                 TDM_EXIT_IF_FAIL(l->info.dst_pos.h <= maxh);
736                         }
737
738                         if (l->owner_p) {
739                                 l->info.src_config.format = l->owner_p->info.dst_config.format;
740                                 if (l->info.src_config.pos.w == 0) {
741                                         TDM_EXIT_IF_FAIL(l->owner_p->info.dst_config.size.h > 0);
742                                         l->info.src_config.pos.w = l->owner_p->info.dst_config.size.h;
743                                         l->info.src_config.pos.h = l->owner_p->info.dst_config.size.v;
744                                 }
745                         } else if (l->owner_c) {
746                                 l->info.src_config.format = l->owner_c->info.dst_config.format;
747                                 if (l->info.src_config.pos.w == 0) {
748                                         TDM_EXIT_IF_FAIL(l->owner_c->info.dst_config.size.h > 0);
749                                         l->info.src_config.pos.w = l->owner_c->info.dst_config.size.h;
750                                         l->info.src_config.pos.h = l->owner_c->info.dst_config.size.v;
751                                 }
752                         } else {
753                                 if (l->info.src_config.pos.w == 0) {
754                                         TDM_EXIT_IF_FAIL(l->info.dst_pos.w > 0);
755                                         l->info.src_config.pos.w = l->info.dst_pos.w;
756                                         l->info.src_config.pos.h = l->info.dst_pos.h;
757                                 }
758                         }
759                 }
760         }
761 }
762
763 static unsigned int tts_screen_prerotation_hint;
764 static tdm_test_server tts_data;
765 static int tts_buffer_key;
766 #define TTS_BUFFER_KEY ((unsigned long)&tts_buffer_key)
767
768 static tbm_surface_h
769 buffer_allocate(int width, int height, int format, int flags)
770 {
771         tdm_test_server_buffer *tb = calloc(1, sizeof *tb);
772         TDM_EXIT_IF_FAIL(tb != NULL);
773         if (tts_screen_prerotation_hint % 180)
774                 TDM_SWAP(width, height);
775         tb->b = tbm_surface_internal_create_with_flags(width, height, format, flags);
776         TDM_EXIT_IF_FAIL(tb->b != NULL);
777         tdm_helper_clear_buffer(tb->b);
778         tbm_surface_internal_add_user_data(tb->b, TTS_BUFFER_KEY, free);
779         tbm_surface_internal_set_user_data(tb->b, TTS_BUFFER_KEY, tb);
780         return tb->b;
781 }
782
783 static tdm_test_server_buffer*
784 get_tts_buffer(tbm_surface_h b)
785 {
786         tdm_test_server_buffer *tb = NULL;
787         tbm_surface_internal_get_user_data(b, TTS_BUFFER_KEY, (void **)&tb);
788         return tb;
789 }
790
791 static void
792 destroy(tdm_test_server *data)
793 {
794         tdm_test_server_output *o = NULL, *oo = NULL;
795         tdm_test_server_layer *l = NULL, *ll = NULL;
796         tdm_test_server_pp *p = NULL, *pp = NULL;
797         tdm_test_server_capture *c = NULL, *cc = NULL;
798         tdm_test_server_prop *w = NULL, *ww = NULL;
799         int i;
800
801         LIST_FOR_EACH_ENTRY_SAFE(c, cc, &data->capture_list, link) {
802                 LIST_DEL(&c->link);
803                 tdm_capture_destroy(c->capture);
804                 free(c);
805         }
806
807         LIST_FOR_EACH_ENTRY_SAFE(p, pp, &data->pp_list, link) {
808                 tdm_display_lock(data->display);
809                 tdm_event_loop_source_remove(p->timer_source);
810                 tdm_display_unlock(data->display);
811
812                 tdm_pp_destroy(p->pp);
813         }
814
815         LIST_FOR_EACH_ENTRY_SAFE(o, oo, &data->output_list, link) {
816                 LIST_FOR_EACH_ENTRY_SAFE(l, ll, &o->layer_list, link) {
817                         tdm_layer_unset_buffer(l->layer);
818                 }
819                 tdm_layer_commit(l->layer, NULL, NULL);
820         }
821
822         LIST_FOR_EACH_ENTRY_SAFE(p, pp, &data->pp_list, link) {
823                 LIST_DEL(&p->link);
824                 for (i = 0; i < TDM_ARRAY_SIZE(p->bufs); i++)
825                         tbm_surface_destroy(p->bufs[i]);
826                 free(p);
827         }
828
829         LIST_FOR_EACH_ENTRY_SAFE(o, oo, &data->output_list, link) {
830                 LIST_DEL(&o->link);
831                 LIST_FOR_EACH_ENTRY_SAFE(l, ll, &o->layer_list, link) {
832                         LIST_DEL(&l->link);
833                         LIST_FOR_EACH_ENTRY_SAFE(w, ww, &l->prop_list, link) {
834                                 LIST_DEL(&w->link);
835                                 free(w);
836                         }
837                         for (i = 0; i < TDM_ARRAY_SIZE(l->bufs); i++)
838                                 tbm_surface_destroy(l->bufs[i]);
839                         free(l);
840                 }
841                 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &o->prop_list, link) {
842                         LIST_DEL(&w->link);
843                         free(w);
844                 }
845
846                 free(o);
847         }
848
849         if (data->display)
850                 tdm_display_deinit(data->display);
851
852         exit(0);
853 }
854
855 int
856 main(int argc, char *argv[])
857 {
858         tdm_test_server *data = &tts_data;
859         char temp[TDM_SERVER_REPLY_MSG_LEN];
860         int len = sizeof temp;
861         tdm_error ret;
862         const char *value;
863
864         value = (const char*)getenv("SCREEN_PREROTATION_HINT");
865         if (value) {
866                 char *end;
867                 tts_screen_prerotation_hint = strtol(value, &end, 10);
868                 printf("SCREEN_PREROTATION_HINT = %d", tts_screen_prerotation_hint);
869         }
870
871         memset(data, 0, sizeof * data);
872         LIST_INITHEAD(&data->output_list);
873         LIST_INITHEAD(&data->pp_list);
874         LIST_INITHEAD(&data->capture_list);
875
876         /* init value */
877         data->bflags = TBM_BO_SCANOUT;
878         data->b_fill = PATTERN_SMPTE;
879
880         data->display = tdm_display_init(&ret);
881         TDM_EXIT_IF_FAIL(data->display != NULL);
882
883         parse_args(data, argc, argv);
884         interpret_args(data);
885
886         if (data->do_query) {
887                 tdm_helper_get_display_information(data->display, temp, &len);
888                 printf("%s", temp);
889                 goto done;
890         }
891
892         run_test(data);
893
894 done:
895         tdm_display_deinit(data->display);
896
897         return 0;
898 }
899
900 static void
901 output_setup(tdm_test_server_output *o)
902 {
903         tdm_test_server *data = o->data;
904         const tdm_output_mode *modes, *found = NULL, *best = NULL, *prefer = NULL;
905         tdm_test_server_prop *w = NULL;
906         const tdm_prop *props;
907         int i, count;
908         tdm_error ret;
909
910         o->output = tdm_display_get_output(data->display, o->idx, &ret);
911         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
912
913         ret = tdm_output_get_available_modes(o->output, &modes, &count);
914         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
915
916         for (i = 0; i < count; i++) {
917                 if (!strncmp(o->mode, modes[i].name, TDM_NAME_LEN) && o->refresh == modes[i].vrefresh) {
918                         found = &modes[i];
919                         printf("found mode: %dx%d %d\n", found->hdisplay, found->vdisplay, found->vrefresh);
920                         break;
921                 }
922                 if (!best)
923                         best = &modes[i];
924                 if (modes[i].flags & TDM_OUTPUT_MODE_TYPE_PREFERRED)
925                         prefer = &modes[i];
926         }
927         if (!found && prefer) {
928                 found = prefer;
929                 printf("found prefer mode: %dx%d %d\n", found->hdisplay, found->vdisplay, found->vrefresh);
930         }
931         if (!found && best) {
932                 found = best;
933                 printf("found best mode: %dx%d %d\n", found->hdisplay, found->vdisplay, found->vrefresh);
934         }
935
936         if (!found) {
937                 printf("couldn't find any mode\n");
938                 exit(0);
939         }
940
941         ret = tdm_output_set_mode(o->output, found);
942         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
943
944         printf("output %d: %s %d\n", o->idx, found->name, found->vrefresh);
945
946         ret = tdm_output_get_available_properties(o->output, &props, &count);
947         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
948
949         LIST_FOR_EACH_ENTRY(w, &o->prop_list, link) {
950                 for (i = 0; i < count; i++) {
951                         if (strncmp(w->name, props[i].name, TDM_NAME_LEN))
952                                 continue;
953                         ret = tdm_output_set_property(o->output, props[i].id, w->value);
954                         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
955                         printf("\tprop '%s': %d\n", props[i].name, w->value.u32);
956                         break;
957                 }
958         }
959
960         /* DPMS on forcely at the first time. */
961         ret = tdm_output_set_dpms(o->output, TDM_OUTPUT_DPMS_ON);
962         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
963 }
964
965 static tbm_surface_h
966 layer_get_buffer(tdm_test_server_layer *l)
967 {
968         int i, size = TDM_ARRAY_SIZE(l->bufs);
969         if (!l->bufs[0]) {
970                 for (i = 0; i < size; i++) {
971                         int width = (l->info.src_config.size.h) ? : l->info.src_config.pos.w;
972                         int height = (l->info.src_config.size.v) ? : l->info.src_config.pos.h;
973                         unsigned int format = (l->info.src_config.format) ? : DEFAULT_FORMAT;
974                         int flags = l->o->data->bflags;
975                         tbm_surface_h b = buffer_allocate(width, height, format, flags);
976                         TDM_EXIT_IF_FAIL(b != NULL);
977                         l->bufs[i] = b;
978                 }
979         }
980         for (i = 0; i < size; i++) {
981                 tdm_test_server_buffer *tb = get_tts_buffer(l->bufs[i]);
982                 TDM_EXIT_IF_FAIL(tb != NULL);
983                 if (!tb->in_use)
984                         return l->bufs[i];
985         }
986         printf("no available layer buffer.\n");
987         exit(0);
988 }
989
990 static void
991 layer_cb_commit(tdm_layer *layer, unsigned int sequence,
992                                 unsigned int tv_sec, unsigned int tv_usec,
993                                 void *user_data)
994 {
995         tdm_test_server_layer *l = user_data;
996         tbm_surface_h b = layer_get_buffer(l);
997         TDM_EXIT_IF_FAIL(b != NULL);
998
999         if (!l->is_primary || l->o->fill_primary_layer)
1000                 tdm_test_buffer_fill(b, l->data->b_fill);
1001
1002         if (!l->is_primary || l->o->fill_primary_layer)
1003                 layer_show_buffer(l, b);
1004 }
1005
1006 static void
1007 layer_cb_buffer_release(tbm_surface_h b, void *user_data)
1008 {
1009         tdm_test_server_buffer *tb = get_tts_buffer(b);
1010         TDM_EXIT_IF_FAIL(tb != NULL);
1011
1012         tb->in_use = 0;
1013         tdm_buffer_remove_release_handler(b, layer_cb_buffer_release, NULL);
1014         if (tb->done)
1015                 tb->done(b, user_data);
1016 }
1017
1018 static void
1019 layer_show_buffer(tdm_test_server_layer *l, tbm_surface_h b)
1020 {
1021         tdm_test_server *data = l->o->data;
1022         tdm_test_server_buffer *tb;
1023         tdm_error ret;
1024
1025         ret = tdm_layer_set_buffer(l->layer, b);
1026         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1027
1028         tb = get_tts_buffer(b);
1029         TDM_EXIT_IF_FAIL(tb != NULL);
1030
1031         tb->in_use = 1;
1032         tdm_buffer_add_release_handler(b, layer_cb_buffer_release, NULL);
1033
1034         if (data->do_vblank)
1035                 ret = tdm_layer_commit(l->layer, layer_cb_commit, l);
1036         else
1037                 ret = tdm_layer_commit(l->layer, NULL, NULL);
1038         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1039
1040         printf("show:\tl(%p) b(%p)\n", l, b);
1041 }
1042
1043 static void
1044 layer_setup(tdm_test_server_layer *l, tbm_surface_h b)
1045 {
1046         tdm_test_server_prop *w = NULL;
1047         const tdm_prop *props;
1048         int i, count;
1049         tdm_error ret;
1050         tbm_surface_info_s info;
1051
1052         l->layer = tdm_output_get_layer(l->o->output, l->idx, &ret);
1053         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1054
1055         /* The size and format information should be same with buffer's */
1056         tbm_surface_get_info(b, &info);
1057         if (tts_screen_prerotation_hint % 180) {
1058                 if (IS_RGB(info.format)) {
1059                         l->info.src_config.size.h = info.height;
1060                         l->info.src_config.size.v = info.planes[0].stride >> 2;
1061                 } else {
1062                         l->info.src_config.size.h = info.height;
1063                         l->info.src_config.size.v = info.planes[0].stride;
1064                 }
1065         } else {
1066                 if (IS_RGB(info.format)) {
1067                         l->info.src_config.size.h = info.planes[0].stride >> 2;
1068                         l->info.src_config.size.v = info.height;
1069                 } else {
1070                         l->info.src_config.size.h = info.planes[0].stride;
1071                         l->info.src_config.size.v = info.height;
1072                 }
1073         }
1074         l->info.src_config.format = info.format;
1075
1076         ret = tdm_layer_set_info(l->layer, &l->info);
1077         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1078
1079         printf("layer %d: output(%d) ", l->idx, l->o->idx);
1080         print_config(&l->info.src_config);
1081         printf(" ! ");
1082         print_pos(&l->info.dst_pos);
1083         printf(" transform(%s)\n", tdm_transform_str(l->info.transform));
1084
1085         ret = tdm_layer_get_available_properties(l->layer, &props, &count);
1086         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1087
1088         LIST_FOR_EACH_ENTRY(w, &l->prop_list, link) {
1089                 for (i = 0; i < count; i++) {
1090                         if (strncmp(w->name, props[i].name, TDM_NAME_LEN))
1091                                 continue;
1092                         ret = tdm_layer_set_property(l->layer, props[i].id, w->value);
1093                         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1094                         printf("\tprop '%s': %d\n", props[i].name, w->value.u32);
1095                         break;
1096                 }
1097         }
1098 }
1099
1100 static tbm_surface_h
1101 pp_get_buffer(tdm_test_server_pp *p)
1102 {
1103         int i, size = TDM_ARRAY_SIZE(p->bufs);
1104         if (!p->bufs[0]) {
1105                 for (i = 0; i < size; i++) {
1106                         int width = (p->info.src_config.size.h) ? : p->info.src_config.pos.w;
1107                         int height = (p->info.src_config.size.v) ? : p->info.src_config.pos.h;
1108                         unsigned int format = (p->info.src_config.format) ? : DEFAULT_FORMAT;
1109                         tbm_surface_h b = buffer_allocate(width, height, format, 0);
1110                         TDM_EXIT_IF_FAIL(b != NULL);
1111                         p->bufs[i] = b;
1112                 }
1113         }
1114         for (i = 0; i < size; i++) {
1115                 tdm_test_server_buffer *tb = get_tts_buffer(p->bufs[i]);
1116
1117                 if (tb == NULL)
1118                         continue;
1119
1120                 if (!tb->in_use) {
1121                         tdm_test_buffer_fill(p->bufs[i], p->data->b_fill);
1122                         return p->bufs[i];
1123                 }
1124         }
1125         printf("no available pp buffer.\n");
1126         exit(0);
1127 }
1128
1129 static void
1130 pp_cb_done(tdm_pp *pp, tbm_surface_h sb, tbm_surface_h db, void *user_data)
1131 {
1132         tdm_test_server_buffer *stb, *dtb;
1133
1134         stb = get_tts_buffer(sb);
1135         TDM_EXIT_IF_FAIL(stb != NULL);
1136
1137         dtb = get_tts_buffer(db);
1138         TDM_EXIT_IF_FAIL(dtb != NULL);
1139
1140         stb->in_use = dtb->in_use = 0;
1141
1142         layer_show_buffer(dtb->l, db);
1143 }
1144
1145 static void
1146 pp_convert_buffer(tdm_test_server_pp *p, tbm_surface_h sb, tbm_surface_h db)
1147 {
1148         tdm_test_server_buffer *stb, *dtb;
1149         tdm_error ret;
1150
1151         stb = get_tts_buffer(sb);
1152         TDM_EXIT_IF_FAIL(stb != NULL);
1153
1154         dtb = get_tts_buffer(db);
1155         TDM_EXIT_IF_FAIL(dtb != NULL);
1156
1157         stb->in_use = dtb->in_use = 1;
1158         dtb->l = p->l;
1159
1160         ret = tdm_pp_attach(p->pp, sb, db);
1161         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1162
1163         ret = tdm_pp_commit(p->pp);
1164         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1165
1166         printf("convt:\tp(%p) sb(%p) db(%p)\n", p, sb, db);
1167 }
1168
1169 /* tdm_event_loop_xxx() function is not for the display server. It's for TDM
1170  * backend module. I use them only for simulating the video play. When we call
1171  * tdm_event_loop_xxx() outside of TDM backend module, we have to lock/unlock
1172  * the TDM display. And when the callback function of tdm_event_loop_xxx() is
1173  * called, the display has been already locked. So in this test application,
1174  * we have to unlock/lock the display for testing.
1175  */
1176 static tdm_error
1177 pp_cb_timeout(void *user_data)
1178 {
1179         tdm_test_server_pp *p = user_data;
1180         tdm_test_server *data = p->l->o->data;
1181         tbm_surface_h sb, db;
1182         tdm_error ret;
1183
1184         tdm_display_unlock(data->display);
1185
1186         sb = pp_get_buffer(p);
1187         TDM_EXIT_IF_FAIL(sb != NULL);
1188         db = layer_get_buffer(p->l);
1189         TDM_EXIT_IF_FAIL(db != NULL);
1190
1191         pp_convert_buffer(p, sb, db);
1192
1193         tdm_display_lock(data->display);
1194         ret = tdm_event_loop_source_timer_update(p->timer_source, 1000 / p->fps);
1195         tdm_display_unlock(data->display);
1196         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1197
1198         tdm_display_lock(data->display);
1199
1200         return TDM_ERROR_NONE;
1201 }
1202
1203 static void
1204 pp_setup(tdm_test_server_pp *p, tbm_surface_h sb, tbm_surface_h db)
1205 {
1206         tdm_test_server *data;
1207         tbm_surface_info_s info;
1208         tdm_error ret;
1209
1210         if (!p || !p->l || !p->l->o || !p->l->o->data) {
1211                 TDM_ERR("invalid parameter failed");
1212                 exit(0);
1213         }
1214         data = p->l->o->data;
1215
1216         p->pp = tdm_display_create_pp(data->display, &ret);
1217         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1218
1219         /* The size and format information should be same with buffer's */
1220         tbm_surface_get_info(sb, &info);
1221         if (IS_RGB(info.format)) {
1222                 p->info.src_config.size.h = info.planes[0].stride >> 2;
1223                 p->info.src_config.size.v = info.height;
1224         } else {
1225                 p->info.src_config.size.h = info.planes[0].stride;
1226                 p->info.src_config.size.v = info.height;
1227         }
1228         p->info.src_config.format = info.format;
1229
1230         /* The size and format information should be same with buffer's */
1231         tbm_surface_get_info(db, &info);
1232         if (IS_RGB(info.format)) {
1233                 p->info.dst_config.size.h = info.planes[0].stride >> 2;
1234                 p->info.dst_config.size.v = info.height;
1235         } else {
1236                 p->info.dst_config.size.h = info.planes[0].stride;
1237                 p->info.dst_config.size.v = info.height;
1238         }
1239         p->info.dst_config.format = info.format;
1240
1241         ret = tdm_pp_set_info(p->pp, &p->info);
1242         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1243
1244         ret = tdm_pp_set_done_handler(p->pp, pp_cb_done, NULL);
1245         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1246
1247         printf("pp: ");
1248         print_config(&p->info.src_config);
1249         printf(" ! ");
1250         print_config(&p->info.dst_config);
1251         printf(" fps(%d) transform(%s)\n", p->fps, tdm_transform_str(p->info.transform));
1252         printf("\toutput_idx(%d) layer_idx(%d)\n", p->l->o->idx, p->l->idx);
1253
1254         layer_setup(p->l, db);
1255
1256         /* tdm_event_loop_xxx() function is not for the display server. It's for TDM
1257          * backend module. I use them only for simulating the video play. When we call
1258          * tdm_event_loop_xxx() outside of TDM backend module, we have to lock/unlock
1259          * the TDM display. And when the callback function of tdm_event_loop_xxx() is
1260          * called, the display has been already locked. So in this test application,
1261          * we have to unlock/lock the display for testing.
1262          */
1263         tdm_display_lock(data->display);
1264         p->timer_source = tdm_event_loop_add_timer_handler(data->display, pp_cb_timeout, p, &ret);
1265         tdm_display_unlock(data->display);
1266         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1267
1268         tdm_display_lock(data->display);
1269         ret = tdm_event_loop_source_timer_update(p->timer_source, 1000 / p->fps);
1270         tdm_display_unlock(data->display);
1271         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1272 }
1273
1274 static void
1275 capture_cb_buffer_done(tbm_surface_h b, void *user_data)
1276 {
1277         tdm_test_server_buffer *tb;
1278
1279         tb = get_tts_buffer(b);
1280         TDM_EXIT_IF_FAIL(tb != NULL);
1281
1282         capture_attach(tb->c, b);
1283 }
1284
1285 static void
1286 capture_cb_done(tdm_capture *capture, tbm_surface_h b, void *user_data)
1287 {
1288         tdm_test_server_buffer *tb;
1289
1290         tb = get_tts_buffer(b);
1291         TDM_EXIT_IF_FAIL(tb != NULL);
1292
1293         tb->in_use = 0;
1294
1295         tb->done = capture_cb_buffer_done;
1296         layer_show_buffer(tb->l, b);
1297 }
1298
1299 static void
1300 capture_attach(tdm_test_server_capture *c, tbm_surface_h b)
1301 {
1302         tdm_error ret;
1303         tdm_test_server_buffer *tb;
1304
1305         tb = get_tts_buffer(b);
1306         TDM_EXIT_IF_FAIL(tb != NULL);
1307
1308         tb->in_use = 1;
1309         tb->l = c->l;
1310         tb->c = c;
1311
1312         ret = tdm_capture_attach(c->capture, b);
1313         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1314
1315         printf("capture:\tc(%p) b(%p)\n", c, b);
1316
1317         ret = tdm_capture_commit(c->capture);
1318         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1319 }
1320
1321 static void
1322 capture_setup(tdm_test_server_capture *c, tbm_surface_h b)
1323 {
1324         tdm_test_server *data;
1325         tdm_output *output;
1326         tdm_layer *layer;
1327         tbm_surface_info_s info;
1328         tdm_error ret;
1329
1330         if (!c || !c->l || !c->l->o || !c->l->o->data) {
1331                 TDM_ERR("invalid parameter failed");
1332                 exit(0);
1333         }
1334         data = c->l->o->data;
1335
1336         if (c->output_idx != -1 && c->layer_idx == -1) {
1337                 output = tdm_display_get_output(data->display, c->output_idx, &ret);
1338                 TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1339                 c->capture = tdm_output_create_capture(output, &ret);
1340                 TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1341         } else if (c->output_idx != -1 && c->layer_idx != -1) {
1342                 output = tdm_display_get_output(data->display, c->output_idx, &ret);
1343                 TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1344                 layer = tdm_output_get_layer(output, c->layer_idx, &ret);
1345                 TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1346                 c->capture = tdm_layer_create_capture(layer, &ret);
1347                 TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1348         }
1349
1350         TDM_EXIT_IF_FAIL(c->capture != NULL);
1351
1352         /* The size and format information should be same with buffer's */
1353         tbm_surface_get_info(b, &info);
1354         if (IS_RGB(info.format)) {
1355                 c->info.dst_config.size.h = info.planes[0].stride >> 2;
1356                 c->info.dst_config.size.v = info.height;
1357         } else {
1358                 c->info.dst_config.size.h = info.planes[0].stride;
1359                 c->info.dst_config.size.v = info.height;
1360         }
1361         c->info.dst_config.format = info.format;
1362
1363         ret = tdm_capture_set_info(c->capture, &c->info);
1364         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1365
1366         ret = tdm_capture_set_done_handler(c->capture, capture_cb_done, NULL);
1367         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1368
1369         printf("capture: o(%d) l(%d) ", c->output_idx, c->layer_idx);
1370         print_config(&c->info.dst_config);
1371         printf(" transform(%s)\n", tdm_transform_str(c->info.transform));
1372         printf("\toutput_idx(%d) layer_idx(%d)\n", c->l->o->idx, c->l->idx);
1373
1374         layer_setup(c->l, b);
1375 }
1376
1377 static void
1378 run_test(tdm_test_server *data)
1379 {
1380         tdm_test_server_output *o = NULL;
1381         tdm_test_server_layer *l = NULL;
1382         tdm_test_server_pp *p = NULL;
1383         tdm_test_server_capture *c = NULL;
1384         tdm_display_capability caps;
1385         tdm_error ret;
1386
1387         ret = tdm_display_get_capabilities(data->display, &caps);
1388         TDM_EXIT_IF_FAIL(ret == TDM_ERROR_NONE);
1389
1390         LIST_FOR_EACH_ENTRY(o, &data->output_list, link) {
1391                 LIST_FOR_EACH_ENTRY(l, &o->layer_list, link) {
1392                         if (!l->owner_p && !l->owner_c) {
1393                                 tbm_surface_h b;
1394                                 b = layer_get_buffer(l);
1395                                 if (!l->is_primary || l->o->fill_primary_layer)
1396                                         tdm_test_buffer_fill(b, data->b_fill);
1397                                 layer_setup(l, b);
1398                                 layer_show_buffer(l, b);
1399                         }
1400                 }
1401         }
1402
1403         LIST_FOR_EACH_ENTRY(p, &data->pp_list, link) {
1404                 tbm_surface_h sb, db;
1405                 TDM_GOTO_IF_FAIL(caps & TDM_DISPLAY_CAPABILITY_PP, no_pp);
1406                 sb = pp_get_buffer(p);
1407                 TDM_EXIT_IF_FAIL(sb != NULL);
1408                 db = layer_get_buffer(p->l);
1409                 TDM_EXIT_IF_FAIL(db != NULL);
1410                 pp_setup(p, sb, db);
1411                 pp_convert_buffer(p, sb, db);
1412         }
1413
1414         LIST_FOR_EACH_ENTRY(c, &data->capture_list, link) {
1415                 TDM_GOTO_IF_FAIL(caps & TDM_DISPLAY_CAPABILITY_CAPTURE, no_capture);
1416                 tbm_surface_h b;
1417                 b = layer_get_buffer(c->l);
1418                 capture_setup(c, b);
1419                 capture_attach(c, b);
1420                 b = layer_get_buffer(c->l);
1421                 capture_attach(c, b);
1422                 b = layer_get_buffer(c->l);
1423                 capture_attach(c, b);
1424         }
1425
1426         printf("enter test loop\n");
1427
1428         while (1)
1429                 tdm_display_handle_events(data->display);
1430
1431         destroy(data);
1432
1433         return;
1434 no_pp:
1435         printf("no PP capability\n");
1436         exit(0);
1437 no_capture:
1438         printf("no Capture capability\n");
1439         exit(0);
1440 }