macro: move backend's name definition
[platform/core/uifw/libtdm.git] / src / tdm.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 "tdm_private.h"
41
42 pthread_mutex_t tdm_mutex_check_lock = PTHREAD_MUTEX_INITIALIZER;
43 int tdm_mutex_locked;
44 const char *tdm_mutex_lock_func;
45 int tdm_mutex_lock_line;
46 const char *tdm_mutex_unlock_func;
47 int tdm_mutex_unlock_line;
48
49 /* LCOV_EXCL_START */
50 static tdm_private_layer *
51 _tdm_display_find_private_layer(tdm_private_output *private_output,
52                                                                 tdm_layer *layer_backend)
53 {
54         tdm_private_layer *private_layer = NULL;
55
56         LIST_FOR_EACH_ENTRY(private_layer, &private_output->layer_list, link) {
57                 if (private_layer->layer_backend == layer_backend)
58                         return private_layer;
59         }
60
61         return NULL;
62 }
63
64 INTERN tdm_private_output *
65 tdm_display_find_private_output(tdm_private_display *private_display, tdm_output *output_backend)
66 {
67         tdm_private_backend *private_backend = NULL;
68         tdm_private_output *private_output = NULL;
69
70         LIST_FOR_EACH_ENTRY(private_backend, &private_display->backend_list, link) {
71                 LIST_FOR_EACH_ENTRY(private_output, &private_backend->output_list, link) {
72                         if (private_output->output_backend == output_backend)
73                                 return private_output;
74                 }
75         }
76
77         return NULL;
78 }
79
80 INTERN void *
81 tdm_display_find_output_stamp(tdm_private_display *private_display, double stamp)
82 {
83         tdm_private_backend *private_backend = NULL;
84         tdm_private_output *private_output = NULL;
85
86         LIST_FOR_EACH_ENTRY(private_backend, &private_display->backend_list, link) {
87                 LIST_FOR_EACH_ENTRY(private_output, &private_backend->output_list, link) {
88                         if (private_output->stamp == stamp)
89                                 return private_output;
90                 }
91         }
92
93         return NULL;
94 }
95
96 static void
97 _tdm_display_destroy_caps_pp(tdm_caps_pp *caps_pp)
98 {
99         if (caps_pp->formats)
100                 free(caps_pp->formats);
101
102         memset(caps_pp, 0, sizeof(tdm_caps_pp));
103 }
104
105 static void
106 _tdm_display_destroy_caps_capture(tdm_caps_capture *caps_capture)
107 {
108         if (caps_capture->formats)
109                 free(caps_capture->formats);
110
111         memset(caps_capture, 0, sizeof(tdm_caps_capture));
112 }
113
114 static void
115 _tdm_display_destroy_caps_layer(tdm_caps_layer *caps_layer)
116 {
117         if (caps_layer->formats)
118                 free(caps_layer->formats);
119
120         if (caps_layer->props)
121                 free(caps_layer->props);
122
123         memset(caps_layer, 0, sizeof(tdm_caps_layer));
124 }
125
126 static void
127 _tdm_display_destroy_caps_output(tdm_caps_output *caps_output)
128 {
129         if (caps_output->modes)
130                 free(caps_output->modes);
131
132         if (caps_output->props)
133                 free(caps_output->props);
134
135         memset(caps_output, 0, sizeof(tdm_caps_output));
136 }
137
138 static void
139 _tdm_display_destroy_private_layer(tdm_private_layer *private_layer)
140 {
141         tdm_private_capture *c = NULL, *cc = NULL;
142
143         tdm_layer_unset_buffer_internal(private_layer);
144
145         LIST_FOR_EACH_ENTRY_SAFE(c, cc, &private_layer->capture_list, link)
146         tdm_capture_destroy_internal(c);
147
148         _tdm_display_destroy_caps_layer(&private_layer->caps);
149
150         /* when destroying layer, someone could check if layer is valid. So delete
151          * the layer's link at last.
152          */
153         LIST_DEL(&private_layer->link);
154
155         free(private_layer);
156 }
157
158 static void
159 _tdm_display_destroy_private_output(tdm_private_output *private_output)
160 {
161         tdm_private_display *private_display = private_output->private_display;
162         tdm_private_layer *l = NULL, *ll = NULL;
163         tdm_private_hwc_window *hw = NULL, *hww = NULL;
164         tdm_private_capture *c = NULL, *cc = NULL;
165         tdm_private_output_vblank_handler *v = NULL, *vv = NULL;
166         tdm_private_output_commit_handler *om = NULL, *omm = NULL;
167         tdm_private_layer_commit_handler *lm = NULL, *lmm = NULL;
168         tdm_private_output_change_handler *h = NULL, *hh = NULL;
169
170         free(private_output->layers_ptr);
171
172         if (private_output->vblank_timeout_timer)
173                 tdm_event_loop_source_remove(private_output->vblank_timeout_timer);
174
175         LIST_FOR_EACH_ENTRY_SAFE(v, vv, &private_output->vblank_handler_list, link) {
176                 LIST_DEL(&v->link);
177                 free(v);
178         }
179
180         LIST_FOR_EACH_ENTRY_SAFE(om, omm, &private_output->output_commit_handler_list, link) {
181                 LIST_DEL(&om->link);
182                 free(om);
183         }
184
185         LIST_FOR_EACH_ENTRY_SAFE(lm, lmm, &private_output->layer_commit_handler_list, link) {
186                 LIST_DEL(&lm->link);
187                 free(lm);
188         }
189
190         LIST_FOR_EACH_ENTRY_SAFE(lm, lmm, &private_output->pending_commit_handler_list, link) {
191                 LIST_DEL(&lm->link);
192                 free(lm);
193         }
194
195         LIST_FOR_EACH_ENTRY_SAFE(h, hh, &private_output->change_handler_list, link) {
196                 LIST_DEL(&h->link);
197                 tdm_thread_cb_remove(h->private_output, TDM_THREAD_CB_OUTPUT_CHANGE, NULL, tdm_output_thread_cb_change, h);
198                 free(h);
199         }
200
201         if (private_output->vblank) {
202                 /* tdm_vblank APIs is for server. it should be called in unlock status*/
203                 _pthread_mutex_unlock(&private_display->lock);
204                 tdm_vblank_destroy(private_output->vblank);
205                 _pthread_mutex_lock(&private_display->lock);
206         }
207
208         if (private_output->ttrace_vblank) {
209                 /* tdm_vblank APIs is for server. it should be called in unlock status*/
210                 _pthread_mutex_unlock(&private_display->lock);
211                 tdm_vblank_destroy(private_output->ttrace_vblank);
212                 _pthread_mutex_lock(&private_display->lock);
213         }
214
215         LIST_FOR_EACH_ENTRY_SAFE(c, cc, &private_output->capture_list, link)
216         tdm_capture_destroy_internal(c);
217
218         LIST_FOR_EACH_ENTRY_SAFE(hw, hww, &private_output->hwc_window_list, link)
219         tdm_hwc_window_destroy_internal(hw);
220
221         LIST_FOR_EACH_ENTRY_SAFE(l, ll, &private_output->layer_list, link)
222         _tdm_display_destroy_private_layer(l);
223
224         _tdm_display_destroy_caps_output(&private_output->caps);
225
226         tdm_thread_cb_remove(private_output, TDM_THREAD_CB_NEED_VALIDATE, NULL,
227                                                  tdm_output_need_validate_handler_thread, NULL);
228         tdm_event_loop_source_remove(private_output->need_validate.event_source);
229         close(private_output->need_validate.event_fd);
230
231         /* when destroying output, vblank objects are also destroyed. vblank checks
232          * if output object is valid. So delete the output's link at last.
233          */
234         LIST_DEL(&private_output->link);
235
236         private_output->stamp = 0;
237         free(private_output);
238 }
239
240 static void
241 _tdm_display_destroy_private_display(tdm_private_display *private_display)
242 {
243         tdm_private_backend *private_backend = NULL, *bb = NULL;
244         tdm_private_output *o = NULL, *oo = NULL;
245         tdm_private_pp *p = NULL, *pp = NULL;
246
247         LIST_FOR_EACH_ENTRY_SAFE(private_backend, bb, &private_display->backend_list, link) {
248                 LIST_FOR_EACH_ENTRY_SAFE(p, pp, &private_backend->pp_list, link) {
249                         tdm_pp_destroy_internal(p);
250                 }
251
252                 LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_backend->output_list, link) {
253                         _tdm_display_destroy_private_output(o);
254                 }
255
256                 _tdm_display_destroy_caps_pp(&private_backend->caps_pp);
257                 _tdm_display_destroy_caps_capture(&private_backend->caps_capture);
258
259                 private_backend->capabilities = 0;
260                 private_backend->caps_display.max_layer_count = 0;
261
262                 if (private_backend->outputs) {
263                         free(private_backend->outputs);
264                         private_backend->outputs = NULL;
265                 }
266         }
267 }
268
269 static tdm_error
270 _tdm_display_update_caps_pp(tdm_private_backend *private_backend, tdm_caps_pp *caps)
271 {
272         tdm_func_display *func_display;
273         tdm_error ret;
274
275         func_display = &private_backend->func_display;
276
277         if (!func_display->display_get_pp_capability) {
278                 TDM_ERR("backend(%s) no display_get_pp_capability()", private_backend->module_data->name);
279                 return TDM_ERROR_BAD_MODULE;
280         }
281
282         ret = func_display->display_get_pp_capability(private_backend->bdata, caps);
283         if (ret != TDM_ERROR_NONE) {
284                 TDM_ERR("backend(%s) display_get_pp_capability() failed", private_backend->module_data->name);
285                 return TDM_ERROR_BAD_MODULE;
286         }
287
288         return TDM_ERROR_NONE;
289 }
290
291 static tdm_error
292 _tdm_display_update_caps_capture(tdm_private_backend *private_backend, tdm_caps_capture *caps)
293 {
294         tdm_func_display *func_display;
295         tdm_error ret;
296
297         func_display = &private_backend->func_display;
298
299         if (!func_display->display_get_capture_capability) {
300                 TDM_ERR("backend(%s) no display_get_capture_capability()", private_backend->module_data->name);
301                 return TDM_ERROR_BAD_MODULE;
302         }
303
304         ret = func_display->display_get_capture_capability(private_backend->bdata, caps);
305         if (ret != TDM_ERROR_NONE) {
306                 TDM_ERR("backend(%s) display_get_capture_capability() failed", private_backend->module_data->name);
307                 return TDM_ERROR_BAD_MODULE;
308         }
309
310         return TDM_ERROR_NONE;
311 }
312
313 static tdm_error
314 _tdm_display_update_caps_layer(tdm_private_backend *private_backend,
315                                                            tdm_layer *layer_backend, tdm_caps_layer *caps)
316 {
317         tdm_func_layer *func_layer = &private_backend->func_layer;
318         tdm_error ret;
319
320         if (!func_layer->layer_get_capability) {
321                 TDM_ERR("backend(%s) no layer_get_capability()", private_backend->module_data->name);
322                 return TDM_ERROR_BAD_MODULE;
323         }
324
325         ret = func_layer->layer_get_capability(layer_backend, caps);
326         if (ret != TDM_ERROR_NONE) {
327                 TDM_ERR("backend(%s) layer_get_capability() failed", private_backend->module_data->name);
328                 return TDM_ERROR_BAD_MODULE;
329         }
330
331         return TDM_ERROR_NONE;
332 }
333
334 static tdm_error
335 _tdm_display_update_caps_output(tdm_private_backend *private_backend, int pipe,
336                                                                 tdm_output *output_backend, tdm_caps_output *caps)
337 {
338         tdm_func_output *func_output = &private_backend->func_output;
339         char temp[TDM_NAME_LEN];
340         tdm_error ret;
341         double stamp;
342
343         if (!func_output->output_get_capability) {
344                 TDM_ERR("backend(%s) no output_get_capability()", private_backend->module_data->name);
345                 return TDM_ERROR_BAD_MODULE;
346         }
347
348         stamp = tdm_helper_get_time();
349         ret = func_output->output_get_capability(output_backend, caps);
350         TDM_DBG("backend(%s) backend output_get_capability() time: %.3f ms",
351                         private_backend->module_data->name, (tdm_helper_get_time() - stamp) * 1000.0);
352
353         if (ret != TDM_ERROR_NONE) {
354                 TDM_ERR("backend(%s) output_get_capability() failed", private_backend->module_data->name);
355                 return TDM_ERROR_BAD_MODULE;
356         }
357
358         /* FIXME: Use model for tdm client to distinguish amoung outputs */
359         snprintf(temp, TDM_NAME_LEN, "%s-%d", caps->model, pipe);
360         snprintf(caps->model, TDM_NAME_LEN, "%s", temp);
361
362         return TDM_ERROR_NONE;
363 }
364
365 static tdm_error
366 _tdm_display_update_layer(tdm_private_output *private_output,
367                                                   tdm_layer *layer_backend, int index)
368 {
369         tdm_private_layer *private_layer;
370         tdm_error ret;
371
372         private_layer = _tdm_display_find_private_layer(private_output, layer_backend);
373         if (!private_layer) {
374                 private_layer = calloc(1, sizeof(tdm_private_layer));
375                 TDM_RETURN_VAL_IF_FAIL(private_layer != NULL, TDM_ERROR_OUT_OF_MEMORY);
376
377                 LIST_ADDTAIL(&private_layer->link, &private_output->layer_list);
378                 private_layer->index = index;
379                 private_layer->private_backend = private_output->private_backend;
380                 private_layer->private_display = private_output->private_display;
381                 private_layer->private_output = private_output;
382                 private_layer->layer_backend = layer_backend;
383
384                 LIST_INITHEAD(&private_layer->capture_list);
385
386                 private_layer->usable = 1;
387         } else
388                 _tdm_display_destroy_caps_layer(&private_layer->caps);
389
390         ret = _tdm_display_update_caps_layer(private_layer->private_backend, layer_backend,
391                                                                                  &private_layer->caps);
392         if (ret != TDM_ERROR_NONE)
393                 goto failed_update;
394
395         return TDM_ERROR_NONE;
396 failed_update:
397         _tdm_display_destroy_private_layer(private_layer);
398         return ret;
399 }
400
401 INTERN tdm_error
402 tdm_display_update_output(tdm_private_backend *private_backend,
403                                                   tdm_output *output_backend, int pipe)
404 {
405         tdm_func_output *func_output = &private_backend->func_output;
406         tdm_private_output *private_output = NULL;
407         tdm_layer **layers = NULL;
408         int layer_count = 0, i;
409         tdm_error ret;
410
411         private_output = tdm_display_find_private_output(private_backend->private_display, output_backend);
412         if (!private_output) {
413                 private_output = calloc(1, sizeof(tdm_private_output));
414                 TDM_RETURN_VAL_IF_FAIL(private_output != NULL, TDM_ERROR_OUT_OF_MEMORY);
415
416                 private_output->stamp = tdm_helper_get_time();
417                 while (tdm_display_find_output_stamp(private_backend->private_display, private_output->stamp))
418                         private_output->stamp++;
419
420                 LIST_ADDTAIL(&private_output->link, &private_backend->output_list);
421
422                 private_output->private_backend = private_backend;
423                 private_output->private_display = private_backend->private_display;
424                 private_output->current_dpms_value = TDM_OUTPUT_DPMS_OFF;
425                 private_output->output_backend = output_backend;
426                 private_output->pipe = pipe;
427                 private_output->index = pipe;
428
429                 LIST_INITHEAD(&private_output->layer_list);
430                 LIST_INITHEAD(&private_output->hwc_window_list);
431                 LIST_INITHEAD(&private_output->capture_list);
432                 LIST_INITHEAD(&private_output->vblank_handler_list);
433                 LIST_INITHEAD(&private_output->output_commit_handler_list);
434                 LIST_INITHEAD(&private_output->layer_commit_handler_list);
435                 LIST_INITHEAD(&private_output->pending_commit_handler_list);
436                 LIST_INITHEAD(&private_output->change_handler_list);
437
438                 if (func_output->output_set_status_handler) {
439                         func_output->output_set_status_handler(private_output->output_backend,
440                                                                                                    tdm_output_cb_status,
441                                                                                                    private_output);
442                         private_output->regist_change_cb = 1;
443                 }
444
445                 ret = _tdm_display_update_caps_output(private_backend, pipe, output_backend,
446                                                                                           &private_output->caps);
447                 if (ret != TDM_ERROR_NONE)
448                         return ret;
449         } else {
450                 tdm_caps_output new_caps;
451
452                 ret = _tdm_display_update_caps_output(private_backend, pipe, output_backend,
453                                                                                           &new_caps);
454                 if (ret != TDM_ERROR_NONE)
455                         return ret;
456
457                 /* FIXME: This is very ugly. need to fix after the TDM ABI is changed. */
458                 if (private_output->caps.status != new_caps.status) {
459                         _tdm_display_destroy_caps_output(&private_output->caps);
460                         private_output->caps = new_caps;
461                         private_output->current_mode = NULL;
462                 } else {
463                         tdm_output_mode *old_modes = private_output->caps.modes;
464                         unsigned int old_mode_count = private_output->caps.mode_count;
465                         if (new_caps.modes)
466                                 free(new_caps.modes);
467                         new_caps.modes = old_modes;
468                         new_caps.mode_count = old_mode_count;
469                         if (private_output->caps.props)
470                                 free(private_output->caps.props);
471                         private_output->caps = new_caps;
472                 }
473         }
474
475         layers = func_output->output_get_layers(output_backend, &layer_count, &ret);
476         if (ret != TDM_ERROR_NONE)
477                 goto failed_update;
478
479         for (i = 0; i < layer_count; i++) {
480                 ret = _tdm_display_update_layer(private_output, layers[i], i);
481                 if (ret != TDM_ERROR_NONE)
482                         goto failed_update;
483         }
484
485         free(layers);
486
487         return TDM_ERROR_NONE;
488 failed_update:
489         _tdm_display_destroy_private_output(private_output);
490         free(layers);
491         return ret;
492 }
493
494 static tdm_output **
495 _tdm_display_set_main_first(tdm_output **outputs, int index)
496 {
497         tdm_output *output_tmp = NULL;
498
499         if (index == 0)
500                 return outputs;
501
502         output_tmp = outputs[0];
503         outputs[0] = outputs[index];
504         outputs[index] = output_tmp;
505
506         return outputs;
507 }
508
509 static tdm_output **
510 _tdm_display_get_ordered_outputs(tdm_private_backend *private_backend, int *count)
511 {
512         tdm_func_display *func_display = &private_backend->func_display;
513         tdm_output **outputs = NULL;
514         tdm_output **new_outputs = NULL;
515         tdm_output *output_dsi = NULL;
516         tdm_output *output_lvds = NULL;
517         tdm_output *output_hdmia = NULL;
518         tdm_output *output_hdmib = NULL;
519         int i, output_count = 0, output_connected_count = 0;
520         int index_dsi = 0, index_lvds = 0, index_hdmia = 0, index_hdmib = 0;
521         tdm_error ret;
522
523         /* don't change list order if not init time */
524         if (private_backend->outputs) {
525                 TDM_ERR("can't change output order");
526                 return private_backend->outputs;
527         }
528
529         outputs = func_display->display_get_outputs(private_backend->bdata, &output_count, &ret);
530         if (ret != TDM_ERROR_NONE)
531                 goto failed_get_outputs;
532
533         *count = output_count;
534
535         if (output_count == 0)
536                 goto failed_get_outputs;
537         else if (output_count == 1) {
538                 private_backend->outputs = outputs;
539                 return outputs;
540         }
541
542         /* count connected outputs */
543         for (i = 0; i < output_count; i++) {
544                 tdm_func_output *func_output = &private_backend->func_output;
545                 tdm_caps_output caps;
546                 memset(&caps, 0, sizeof(tdm_caps_output));
547
548                 if (!func_output->output_get_capability) {
549                         TDM_ERR("no output_get_capability()");
550                         goto failed_get_outputs;
551                 }
552
553                 ret = func_output->output_get_capability(outputs[i], &caps);
554                 if (ret != TDM_ERROR_NONE) {
555                         TDM_ERR("output_get_capability() failed");
556                         goto failed_get_outputs;
557                 }
558
559                 if (caps.status == TDM_OUTPUT_CONN_STATUS_CONNECTED) {
560                         output_connected_count++;
561
562                         switch (caps.type) {
563                         case TDM_OUTPUT_TYPE_DSI:
564                                 output_dsi = outputs[i];
565                                 index_dsi = i;
566                                 break;
567                         case TDM_OUTPUT_TYPE_LVDS:
568                                 output_lvds = outputs[i];
569                                 index_lvds = i;
570                                 break;
571                         case TDM_OUTPUT_TYPE_HDMIA:
572                                 output_hdmia = outputs[i];
573                                 index_hdmia = i;
574                                 break;
575                         case TDM_OUTPUT_TYPE_HDMIB:
576                                 output_hdmib = outputs[i];
577                                 index_hdmib = i;
578                                 break;
579                         default:
580                                 break;
581                         }
582                 }
583
584                 _tdm_display_destroy_caps_output(&caps);
585         }
586
587         /* ordering : main output is first */
588         /* If there is no connected output, lvds or dsi cannot be main display. (cannot connect after booting)
589           * But hdmi is possible, so set hdmi to main display.
590           * If connected only one output, it is main output.
591           * If connected outputs over 2, has priority like below.
592           * (dsi > lvds > hdmi > else)
593           */
594         if (output_connected_count == 0) {
595                 /* hdmi > dsi > lvds > else */
596                 if (output_hdmia != NULL)
597                         new_outputs = _tdm_display_set_main_first(outputs, index_hdmia);
598                 else if (output_hdmib != NULL)
599                         new_outputs = _tdm_display_set_main_first(outputs, index_hdmib);
600                 else if (output_dsi != NULL)
601                         new_outputs = _tdm_display_set_main_first(outputs, index_dsi);
602                 else if (output_lvds != NULL)
603                         new_outputs = _tdm_display_set_main_first(outputs, index_lvds);
604                 else
605                         new_outputs = outputs;
606         } else { /* (output_connected_count > 1) */
607                 /* dsi > lvds > hdmi > else */
608                 if (output_dsi != NULL)
609                         new_outputs = _tdm_display_set_main_first(outputs, index_dsi);
610                 else if (output_lvds != NULL)
611                         new_outputs = _tdm_display_set_main_first(outputs, index_lvds);
612                 else if (output_hdmia != NULL)
613                         new_outputs = _tdm_display_set_main_first(outputs, index_hdmia);
614                 else if (output_hdmib != NULL)
615                         new_outputs = _tdm_display_set_main_first(outputs, index_hdmib);
616                 else
617                         new_outputs = outputs;
618         }
619
620         private_backend->outputs = new_outputs;
621
622         return new_outputs;
623
624 failed_get_outputs:
625         free(outputs);
626         *count = 0;
627         return NULL;
628 }
629
630 static tdm_error
631 _tdm_display_update(tdm_private_display *private_display)
632 {
633         tdm_private_backend *private_backend = NULL;
634         tdm_error ret = TDM_ERROR_NONE;
635         int index = 0;
636
637         if (private_display->pp_backend) {
638                 ret = _tdm_display_update_caps_pp(private_display->pp_backend,
639                                                                                   &private_display->pp_backend->caps_pp);
640                 if (ret != TDM_ERROR_NONE)
641                         goto failed_update;
642         }
643
644         if (private_display->capture_backend) {
645                 ret = _tdm_display_update_caps_capture(private_display->capture_backend,
646                                                                                            &private_display->capture_backend->caps_capture);
647                 if (ret != TDM_ERROR_NONE)
648                         goto failed_update;
649         }
650
651         LIST_FOR_EACH_ENTRY(private_backend, &private_display->backend_list, link) {
652                 tdm_output **outputs;
653                 int output_count = 0, i;
654
655                 outputs = _tdm_display_get_ordered_outputs(private_backend, &output_count);
656                 if (!outputs)
657                         goto failed_update;
658
659                 for (i = 0; i < output_count; i++) {
660                         ret = tdm_display_update_output(private_backend, outputs[i], index++);
661                         if (ret != TDM_ERROR_NONE)
662                                 goto failed_update;
663                 }
664         }
665
666         return TDM_ERROR_NONE;
667
668 failed_update:
669         _tdm_display_destroy_private_display(private_display);
670         return ret;
671 }
672 /* LCOV_EXCL_STOP */
673
674 EXTERN tdm_error
675 tdm_display_update(tdm_display *dpy)
676 {
677         TDM_RETURN_VAL_IF_FAIL(dpy != NULL, TDM_ERROR_INVALID_PARAMETER);
678         TDM_RETURN_VAL_IF_FAIL(tdm_display_is_valid(dpy), TDM_ERROR_INVALID_PARAMETER);
679
680         TDM_DEPRECATED(NULL);
681
682         return TDM_ERROR_NONE;
683 }
684
685 int tdm_debug_module;
686 int tdm_debug_dump;
687 int tdm_ttrace_module;
688 int tdm_ttrace_output;
689
690 static tdm_private_display *g_private_display;
691 static pthread_mutex_t gLock = PTHREAD_MUTEX_INITIALIZER;
692
693 /* LCOV_EXCL_START */
694 static tdm_error
695 _tdm_display_check_module(tdm_backend_module *module)
696 {
697         int major, minor;
698
699         TDM_INFO("TDM ABI version : %d.%d",
700                          TDM_MAJOR_VERSION, TDM_MINOR_VERSION);
701
702         if (!module->name) {
703                 TDM_ERR("TDM backend doesn't have name");
704                 return TDM_ERROR_BAD_MODULE;
705         }
706
707         if (!module->vendor) {
708                 TDM_ERR("TDM backend doesn't have vendor");
709                 return TDM_ERROR_BAD_MODULE;
710         }
711
712         major = TDM_BACKEND_GET_ABI_MAJOR(module->abi_version);
713         minor = TDM_BACKEND_GET_ABI_MINOR(module->abi_version);
714
715         TDM_INFO("TDM module name: %s", module->name);
716         TDM_INFO("'%s' vendor: %s", module->name, module->vendor);
717         TDM_INFO("'%s' version: %d.%d", module->name, major, minor);
718
719         if (major != TDM_MAJOR_VERSION) {
720                 TDM_ERR("'%s' major version mismatch, %d != %d",
721                                 module->name, major, TDM_MAJOR_VERSION);
722                 return TDM_ERROR_BAD_MODULE;
723         }
724
725         if (minor > TDM_MINOR_VERSION) {
726                 TDM_ERR("'%s' minor version(%d) is newer than %d",
727                                 module->name, minor, TDM_MINOR_VERSION);
728                 return TDM_ERROR_BAD_MODULE;
729         }
730
731         if (!module->init) {
732                 TDM_ERR("'%s' doesn't have init function", module->name);
733                 return TDM_ERROR_BAD_MODULE;
734         }
735
736         if (!module->deinit) {
737                 TDM_ERR("'%s' doesn't have deinit function", module->name);
738                 return TDM_ERROR_BAD_MODULE;
739         }
740
741         return TDM_ERROR_NONE;
742 }
743
744 static tdm_error
745 _tdm_display_check_backend_functions(tdm_private_backend *private_backend)
746 {
747         tdm_func_display *func_display = &private_backend->func_display;
748         tdm_func_output *func_output = &private_backend->func_output;
749         tdm_func_layer *func_layer = &private_backend->func_layer;
750         tdm_error ret;
751
752         /* below functions should be implemented in backend side */
753
754         TDM_RETURN_VAL_IF_FAIL(func_display != NULL, TDM_ERROR_BAD_MODULE);
755         TDM_RETURN_VAL_IF_FAIL(func_display->display_get_outputs, TDM_ERROR_BAD_MODULE);
756         TDM_RETURN_VAL_IF_FAIL(func_output->output_get_capability, TDM_ERROR_BAD_MODULE);
757         TDM_RETURN_VAL_IF_FAIL(func_output->output_get_layers, TDM_ERROR_BAD_MODULE);
758         TDM_RETURN_VAL_IF_FAIL(func_layer->layer_get_capability, TDM_ERROR_BAD_MODULE);
759
760         ret = func_display->display_get_capability(private_backend->bdata, &private_backend->caps_display);
761         if (ret != TDM_ERROR_NONE) {
762                 TDM_ERR("display_get_capability() failed");
763                 return TDM_ERROR_BAD_MODULE;
764         }
765
766         if (private_backend->capabilities & TDM_DISPLAY_CAPABILITY_PP) {
767                 tdm_func_pp *func_pp = &private_backend->func_pp;
768                 TDM_RETURN_VAL_IF_FAIL(func_display->display_get_pp_capability, TDM_ERROR_BAD_MODULE);
769                 TDM_RETURN_VAL_IF_FAIL(func_display->display_create_pp, TDM_ERROR_BAD_MODULE);
770                 TDM_RETURN_VAL_IF_FAIL(func_pp->pp_destroy, TDM_ERROR_BAD_MODULE);
771                 TDM_RETURN_VAL_IF_FAIL(func_pp->pp_commit, TDM_ERROR_BAD_MODULE);
772                 TDM_RETURN_VAL_IF_FAIL(func_pp->pp_set_done_handler, TDM_ERROR_BAD_MODULE);
773         }
774
775         if (private_backend->capabilities & TDM_DISPLAY_CAPABILITY_CAPTURE) {
776                 tdm_func_capture *func_capture = &private_backend->func_capture;
777                 TDM_RETURN_VAL_IF_FAIL(func_display->display_get_capture_capability, TDM_ERROR_BAD_MODULE);
778                 if (private_backend->caps_capture.capabilities & TDM_CAPTURE_CAPABILITY_OUTPUT)
779                         TDM_RETURN_VAL_IF_FAIL(func_output->output_create_capture, TDM_ERROR_BAD_MODULE);
780                 if (private_backend->caps_capture.capabilities & TDM_CAPTURE_CAPABILITY_LAYER)
781                         TDM_RETURN_VAL_IF_FAIL(func_layer->layer_create_capture, TDM_ERROR_BAD_MODULE);
782                 TDM_RETURN_VAL_IF_FAIL(func_capture->capture_destroy, TDM_ERROR_BAD_MODULE);
783                 TDM_RETURN_VAL_IF_FAIL(func_capture->capture_commit, TDM_ERROR_BAD_MODULE);
784                 TDM_RETURN_VAL_IF_FAIL(func_capture->capture_set_done_handler, TDM_ERROR_BAD_MODULE);
785         }
786
787         return TDM_ERROR_NONE;
788 }
789
790 static tdm_error
791 _tdm_display_load_module_with_file(tdm_private_display *private_display,
792                                                                    const char *file)
793 {
794         char path[TDM_PATH_LEN] = {0,};
795         void *module = NULL;
796         tdm_backend_module *module_data;
797         tdm_backend_data *bdata = NULL;
798         tdm_private_backend *private_backend = NULL;
799         tdm_error ret;
800         double stamp;
801         int size;
802
803         size = snprintf(path, sizeof(path), TDM_MODULE_PATH "/%s", file);
804         if (size >= (int)sizeof(path)) {
805                 TDM_WRN("too long: %s/%s", TDM_MODULE_PATH, file);
806                 return TDM_ERROR_BAD_MODULE;
807         };
808
809         private_backend = calloc(1, sizeof *private_backend);
810         if (!private_backend) {
811                 TDM_ERR("alloc failed: %m");
812                 ret = TDM_ERROR_OUT_OF_MEMORY;
813                 goto failed_load;
814         }
815
816         private_backend->private_display = private_display;
817
818         stamp = tdm_helper_get_time();
819
820         TDM_TRACE_BEGIN("TDM_Load_Backend");
821         module = dlopen(path, RTLD_LAZY);
822         if (!module) {
823                 TDM_ERR("failed to load module: %s(%s)", dlerror(), file);
824                 TDM_TRACE_END();
825                 ret = TDM_ERROR_BAD_MODULE;
826                 goto failed_load;
827         }
828
829         private_backend->module = module;
830
831         module_data = dlsym(module, "tdm_backend_module_data");
832         if (!module_data) {
833                 TDM_ERR("'%s' doesn't have data object", file);
834                 ret = TDM_ERROR_BAD_MODULE;
835                 TDM_TRACE_END();
836                 goto failed_load;
837         }
838         TDM_TRACE_END();
839
840         private_backend->module_data = module_data;
841
842         TDM_DBG("dlopen, dlsym time: %.3f ms", (tdm_helper_get_time() - stamp) * 1000.0);
843
844         /* check if version, init() and deinit() are valid or not */
845         ret = _tdm_display_check_module(module_data);
846         if (ret != TDM_ERROR_NONE) {
847                 TDM_ERR("backend(%s) load failed: check module error", module_data->name);
848                 goto failed_load;
849         }
850
851         private_display->current_backend = private_backend;
852
853         /* We don't care if backend_data is NULL or not. It's up to backend. */
854         TDM_TRACE_BEGIN("TDM_Init_Backend");
855         stamp = tdm_helper_get_time();
856         bdata = module_data->init((tdm_display *)private_display, &ret);
857         TDM_DBG("backend init() time: %.3f ms", (tdm_helper_get_time() - stamp) * 1000.0);
858         TDM_TRACE_END();
859
860         private_display->current_backend = NULL;
861
862         private_backend->bdata = bdata;
863
864         if (ret != TDM_ERROR_NONE) {
865                 TDM_ERR("backend(%s) load failed: init error", module_data->name);
866                 goto failed_load;
867         }
868
869         ret = _tdm_display_check_backend_functions(private_backend);
870         if (ret != TDM_ERROR_NONE) {
871                 TDM_ERR("backend(%s) load failed: check functions error", module_data->name);
872                 goto failed_load;
873         }
874
875         LIST_INITHEAD(&private_backend->output_list);
876         LIST_INITHEAD(&private_backend->pp_list);
877         LIST_INITHEAD(&private_backend->capture_list);
878
879         LIST_ADDTAIL(&private_backend->link, &private_display->backend_list);
880
881         TDM_INFO("Success to load '%s' module", module_data->name);
882
883         return TDM_ERROR_NONE;
884 failed_load:
885         if (bdata)
886                 module_data->deinit(bdata);
887         dlclose(module);
888         if (private_backend)
889                 free(private_backend);
890         return ret;
891 }
892
893 static tdm_error
894 _tdm_display_load_modules(tdm_private_display *private_display)
895 {
896         const char *module_names;
897         tdm_error ret = 0;
898         char temp[TDM_PATH_LEN];
899         char *arg;
900         char *end;
901
902         LIST_INITHEAD(&private_display->backend_list);
903
904         module_names = tdm_config_get_string(TDM_CONFIG_KEY_GENERAL_BACKENDS, TDM_DEFAULT_MODULE);
905
906         snprintf(temp, TDM_PATH_LEN, "%s", module_names);
907
908         arg = strtok_r(temp, TDM_CONFIG_DELIM, &end);
909         while (arg) {
910                 TDM_INFO("loading a %s backend", arg);
911                 ret = _tdm_display_load_module_with_file(private_display, arg);
912                 if (ret == TDM_ERROR_NONE)
913                         TDM_INFO("%s backend loading success", arg);
914                 else
915                         TDM_INFO("%s backend loading failed", arg);
916                 arg = strtok_r(NULL, TDM_CONFIG_DELIM, &end);
917         }
918
919         /* load bufmgr priv from dummy lib */
920         if (LIST_IS_EMPTY(&private_display->backend_list)) {
921                 TDM_WRN("No backend. loading a %s backend", TDM_DUMMY_MODULE);
922                 ret = _tdm_display_load_module_with_file(private_display, TDM_DUMMY_MODULE);
923                 if (ret == TDM_ERROR_NONE)
924                         TDM_INFO("%s backend loading success", TDM_DUMMY_MODULE);
925                 else
926                         TDM_INFO("%s backend loading failed", TDM_DUMMY_MODULE);
927         }
928
929         return ret;
930 }
931
932 static void
933 _tdm_display_unload_modules(tdm_private_display *private_display)
934 {
935         tdm_private_backend *private_backend = NULL, *bb = NULL;
936
937         LIST_FOR_EACH_ENTRY_SAFE(private_backend, bb, &private_display->backend_list, link) {
938                 LIST_DEL(&private_backend->link);
939
940                 if (private_backend->module_data)
941                         private_backend->module_data->deinit(private_backend->bdata);
942                 if (private_backend->module)
943                         dlclose(private_backend->module);
944
945                 free(private_backend);
946         }
947 }
948 /* LCOV_EXCL_STOP */
949
950 EXTERN tdm_display *
951 tdm_display_init(tdm_error *error)
952 {
953         tdm_private_display *private_display = NULL;
954         const char *str;
955         tdm_error ret;
956         double stamp1, stamp2, start;
957         int mode;
958
959         pthread_mutex_lock(&gLock);
960
961         if (g_private_display) {
962                 g_private_display->init_count++;
963                 pthread_mutex_unlock(&gLock);
964                 if (error)
965                         *error = TDM_ERROR_NONE;
966                 return g_private_display;
967         }
968
969         start = stamp1 = tdm_helper_get_time();
970
971         stamp2 = tdm_helper_get_time();
972         TDM_INFO("config init time: %.3f ms", (stamp2 - stamp1) * 1000.0);
973         stamp1 = stamp2;
974
975         private_display = calloc(1, sizeof(tdm_private_display));
976         if (!private_display) {
977                 /* LCOV_EXCL_START */
978                 ret = TDM_ERROR_OUT_OF_MEMORY;
979                 TDM_ERR("'private_display != NULL' failed");
980                 goto failed_alloc;
981                 /* LCOV_EXCL_STOP */
982         }
983
984         private_display->stamp = tdm_helper_get_time();
985
986         str = tdm_config_get_string(TDM_CONFIG_KEY_DEBUG_MODULE, NULL);
987         if (str)
988                 tdm_display_enable_debug_module(str);
989
990         str = tdm_config_get_string(TDM_CONFIG_KEY_DEBUG_DUMP, NULL);
991         if (str)
992                 tdm_display_enable_dump(private_display, str, NULL, NULL);
993
994         if (pthread_mutex_init(&private_display->lock, NULL)) {
995                 /* LCOV_EXCL_START */
996                 ret = TDM_ERROR_OPERATION_FAILED;
997                 TDM_ERR("mutex init failed: %m");
998                 goto failed_mutex_init;
999                 /* LCOV_EXCL_STOP */
1000         }
1001
1002         _pthread_mutex_lock(&private_display->lock);
1003
1004         stamp2 = tdm_helper_get_time();
1005         TDM_DBG("prepare init time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1006         stamp1 = stamp2;
1007
1008         ret = tdm_event_loop_init(private_display);
1009         if (ret != TDM_ERROR_NONE)
1010                 goto failed_event;
1011
1012         stamp2 = tdm_helper_get_time();
1013         TDM_INFO("event loop init time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1014         stamp1 = stamp2;
1015
1016         ret = tdm_vblank_init(private_display);
1017         if (ret != TDM_ERROR_NONE)
1018                 goto failed_vblank;
1019
1020         ret = tdm_output_init(private_display);
1021         if (ret != TDM_ERROR_NONE)
1022                 goto failed_load;
1023
1024         ret = tdm_pp_init(private_display);
1025         if (ret != TDM_ERROR_NONE)
1026                 goto failed_load;
1027
1028         ret = tdm_capture_init(private_display);
1029         if (ret != TDM_ERROR_NONE)
1030                 goto failed_load;
1031
1032         ret = _tdm_display_load_modules(private_display);
1033         if (ret != TDM_ERROR_NONE)
1034                 goto failed_load;
1035
1036         stamp2 = tdm_helper_get_time();
1037         TDM_INFO("loading backend time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1038         stamp1 = stamp2;
1039
1040 #ifdef INIT_BUFMGR
1041         int tdm_drm_fd = tdm_helper_get_fd("TDM_DRM_MASTER_FD");
1042         if (tdm_drm_fd >= 0) {
1043                 private_display->bufmgr = tbm_bufmgr_init(tdm_drm_fd);
1044                 close(tdm_drm_fd);
1045                 if (!private_display->bufmgr) {
1046                         TDM_ERR("tbm_bufmgr_init failed");
1047                         goto failed_update;
1048                 } else {
1049                         TDM_INFO("tbm_bufmgr_init successed");
1050                 }
1051
1052                 stamp2 = tdm_helper_get_time();
1053                 TDM_DBG("initializing bufmgr time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1054                 stamp1 = stamp2;
1055         }
1056 #endif
1057
1058         TDM_TRACE_BEGIN("TDM_Update_Display");
1059         ret = _tdm_display_update(private_display);
1060         TDM_TRACE_END();
1061         if (ret != TDM_ERROR_NONE)
1062                 goto failed_update;
1063
1064         stamp2 = tdm_helper_get_time();
1065         TDM_DBG("updating display time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1066         stamp1 = stamp2;
1067
1068         tdm_event_loop_create_backend_source(private_display);
1069
1070         private_display->init_count = 1;
1071         g_private_display = private_display;
1072
1073         tdm_private_backend *b = NULL;
1074         tdm_private_output *o = NULL;
1075         LIST_FOR_EACH_ENTRY(b, &private_display->backend_list, link) {
1076                 LIST_FOR_EACH_ENTRY(o, &b->output_list, link) {
1077                         if (o->caps.capabilities & TDM_OUTPUT_CAPABILITY_HWC)
1078                                 tdm_output_need_validate_event_init(o);
1079                 }
1080         }
1081
1082         /* the COMMIT_PER_VBLANK functionality is ability of an output to support
1083          * several operational modes (commit_per_vblank modes) related to tdm_commit;
1084          * this functionality can be turned off which means a default mode */
1085         mode = tdm_config_get_int(TDM_CONFIG_KEY_GENERAL_COMMIT_PER_VBLANK, 0);
1086         if (mode > 0) {
1087                 tdm_private_backend *b = NULL;
1088                 tdm_private_output *o = NULL;
1089
1090                 /* outputs which support hwc capability can work only
1091                  * if commit_per_vblank mode is '0' (default mode) */
1092                 LIST_FOR_EACH_ENTRY(b, &private_display->backend_list, link) {
1093                         LIST_FOR_EACH_ENTRY(o, &b->output_list, link) {
1094                                 if (!(o->caps.capabilities & TDM_OUTPUT_CAPABILITY_HWC))
1095                                         tdm_output_choose_commit_per_vblank_mode(o, mode);
1096                         }
1097                 }
1098         }
1099
1100         if (error)
1101                 *error = TDM_ERROR_NONE;
1102
1103         _pthread_mutex_unlock(&private_display->lock);
1104         pthread_mutex_unlock(&gLock);
1105
1106         TDM_INFO("init time: %.3f ms", (tdm_helper_get_time() - start) * 1000.0);
1107
1108         return (tdm_display *)private_display;
1109
1110 /* LCOV_EXCL_START */
1111 failed_update:
1112         _tdm_display_unload_modules(private_display);
1113 failed_load:
1114         tdm_event_loop_stop(private_display);
1115         tdm_event_loop_deinit(private_display);
1116 failed_event:
1117         tdm_vblank_deinit(private_display);
1118 failed_vblank:
1119         _pthread_mutex_unlock(&private_display->lock);
1120         pthread_mutex_destroy(&private_display->lock);
1121 failed_mutex_init:
1122         free(private_display);
1123 failed_alloc:
1124         if (error)
1125                 *error = ret;
1126         pthread_mutex_unlock(&gLock);
1127         return NULL;
1128 /* LCOV_EXCL_STOP */
1129 }
1130
1131 EXTERN void
1132 tdm_display_deinit(tdm_display *dpy)
1133 {
1134         tdm_private_display *private_display = dpy;
1135
1136         if (!private_display)
1137                 return;
1138
1139         TDM_RETURN_IF_FAIL(tdm_display_is_valid(dpy));
1140
1141         pthread_mutex_lock(&gLock);
1142
1143         private_display->init_count--;
1144         if (private_display->init_count > 0) {
1145                 pthread_mutex_unlock(&gLock);
1146                 return;
1147         }
1148
1149         /* dont move the position of lock/unlock. all resource should be protected
1150          * during destroying. after tdm_event_loop_deinit, we don't worry about thread
1151          * things because it's finalized.
1152          */
1153         _pthread_mutex_lock(&private_display->lock);
1154         tdm_event_loop_stop(private_display);
1155         tdm_event_loop_deinit(private_display);
1156         _pthread_mutex_unlock(&private_display->lock);
1157
1158         /* when private_output is destroyed, all vblank resources of client and server
1159          * are destroyed. Then we can call tdm_vblank_deinit. After destroying display,
1160          * we can unload backend modulues.
1161          */
1162         _tdm_display_destroy_private_display(private_display);
1163         _tdm_display_unload_modules(private_display);
1164
1165         tdm_vblank_deinit(private_display);
1166
1167 #ifdef INIT_BUFMGR
1168         if (private_display->bufmgr)
1169                 tbm_bufmgr_deinit(private_display->bufmgr);
1170 #endif
1171
1172         tbm_drm_helper_unset_tbm_master_fd();
1173
1174         pthread_mutex_destroy(&private_display->lock);
1175         free(private_display);
1176         g_private_display = NULL;
1177
1178         if (tdm_debug_dump_dir) {
1179                 free(tdm_debug_dump_dir);
1180                 tdm_debug_dump_dir = NULL;
1181         }
1182
1183         tdm_config_deinit();
1184
1185         pthread_mutex_unlock(&gLock);
1186
1187         TDM_INFO("done");
1188 }
1189
1190 INTERN tdm_private_display *
1191 tdm_display_get(void)
1192 {
1193         return g_private_display;
1194 }
1195
1196 INTERN int
1197 tdm_display_is_valid(tdm_display *dpy)
1198 {
1199         if (dpy != NULL && dpy == g_private_display)
1200                 return 1;
1201         return 0;
1202 }
1203
1204 INTERN int
1205 tdm_output_is_valid(tdm_output *output)
1206 {
1207         tdm_private_backend *b = NULL;
1208         tdm_private_output *o = NULL;
1209
1210         if (!output || !g_private_display)
1211                 return 0;
1212
1213         LIST_FOR_EACH_ENTRY(b, &g_private_display->backend_list, link) {
1214                 LIST_FOR_EACH_ENTRY(o, &b->output_list, link)
1215                         if (o == output)
1216                                 return 1;
1217         }
1218
1219         return 0;
1220 }
1221
1222 /* LCOV_EXCL_START */
1223 INTERN int
1224 tdm_backend_check_module_abi(tdm_private_backend *private_backend, int abimaj, int abimin)
1225 {
1226         tdm_backend_module *module = private_backend->module_data;
1227
1228         if (TDM_BACKEND_GET_ABI_MAJOR(module->abi_version) < abimaj)
1229                 return 0;
1230
1231         if (TDM_BACKEND_GET_ABI_MINOR(module->abi_version) < abimin)
1232                 return 0;
1233
1234         return 1;
1235 }
1236
1237 INTERN tdm_error
1238 tdm_display_enable_debug_module(const char*modules)
1239 {
1240         char temp[TDM_PATH_LEN];
1241         char *arg;
1242         char *end;
1243
1244         snprintf(temp, TDM_PATH_LEN, "%s", modules);
1245
1246         tdm_debug_module = 0;
1247
1248         arg = strtok_r(temp, TDM_CONFIG_DELIM, &end);
1249         while (arg) {
1250                 if (!strncmp(arg, "none", 4)) {
1251                         tdm_debug_module = 0;
1252                         return TDM_ERROR_NONE;
1253                 }
1254                 if (!strncmp(arg, "all", 3)) {
1255                         tdm_debug_module = 0xFFFFFFFF;
1256                         return TDM_ERROR_NONE;
1257                 }
1258                 if (!strncmp(arg, "buffer", 6))
1259                         tdm_debug_module |= TDM_DEBUG_BUFFER;
1260                 else if (!strncmp(arg, "event", 5))
1261                         tdm_debug_module |= TDM_DEBUG_EVENT;
1262                 else if (!strncmp(arg, "thread", 6))
1263                         tdm_debug_module |= TDM_DEBUG_THREAD;
1264                 else if (!strncmp(arg, "mutex", 5))
1265                         tdm_debug_module |= TDM_DEBUG_MUTEX;
1266                 else if (!strncmp(arg, "vblank", 6))
1267                         tdm_debug_module |= TDM_DEBUG_VBLANK;
1268                 else if (!strncmp(arg, "commit", 6))
1269                         tdm_debug_module |= TDM_DEBUG_COMMIT;
1270
1271                 arg = strtok_r(NULL, TDM_CONFIG_DELIM, &end);
1272         }
1273
1274         TDM_INFO("module debugging... '%s'", modules);
1275
1276         return TDM_ERROR_NONE;
1277 }
1278
1279 INTERN tdm_error
1280 tdm_display_enable_dump(tdm_private_display *private_display, const char *dump_str, char *reply, int *len)
1281 {
1282         char temp[TDM_PATH_LEN] = {0,}, temp2[TDM_PATH_LEN] = {0,};
1283         char *path = NULL, *path2;
1284         char *arg;
1285         char *end;
1286
1287         snprintf(temp2, TDM_PATH_LEN, "%s", dump_str);
1288         path2 = strtostr(temp, TDM_PATH_LEN, temp2, "@");
1289         if (!path2 || path2[0] == '\0')
1290                 path2 = TDM_DUMP_DIR;
1291         else
1292                 path2++;
1293
1294         tdm_debug_dump = 0;
1295
1296         snprintf(temp, sizeof(temp), "%s", dump_str);
1297         arg = strtok_r(temp, ",", &end);
1298         TDM_GOTO_IF_FAIL(arg != NULL, done);
1299
1300         if (!strncmp(arg, "none", 4)) {
1301                 tdm_debug_dump = 0;
1302                 TDM_SNPRINTF(reply, len, "path: %s\n", (tdm_debug_dump_dir) ? : "unknown");
1303                 if (tdm_debug_dump_dir) {
1304                         free(tdm_debug_dump_dir);
1305                         tdm_debug_dump_dir = NULL;
1306                 }
1307                 goto done;
1308         }
1309
1310         path = tdm_helper_dump_make_directory(path2, reply, len);
1311         TDM_GOTO_IF_FAIL(path != NULL, done);
1312
1313         if (!strncmp(arg, "current", 7)) {
1314                 tdm_private_backend *b = NULL;
1315                 tdm_private_output *o = NULL;
1316
1317                 if (!private_display) {
1318                         TDM_WRN("no private_display");
1319                         goto done;
1320                 }
1321
1322                 LIST_FOR_EACH_ENTRY(b, &private_display->backend_list, link) {
1323                         LIST_FOR_EACH_ENTRY(o, &b->output_list, link) {
1324                                 tdm_private_layer *l = NULL;
1325                                 LIST_FOR_EACH_ENTRY(l, &o->layer_list, link) {
1326                                         char str[TDM_PATH_LEN];
1327                                         if (l->usable || l->caps.capabilities & TDM_LAYER_CAPABILITY_VIDEO)
1328                                                 continue;
1329                                         if (!l->showing_buffer)
1330                                                 continue;
1331                                         snprintf(str, TDM_PATH_LEN, "layer_%d_%d", o->index, l->index);
1332                                         tdm_helper_dump_buffer_str(l->showing_buffer->buffer, path, str);
1333                                 }
1334                         }
1335                 }
1336
1337                 TDM_SNPRINTF(reply, len, "path: %s\n", path);
1338                 goto done;
1339         }
1340
1341         TDM_SNPRINTF(reply, len, "dump: %s\n", arg);
1342
1343         while (arg) {
1344                 if (!strncmp(arg, "all", 3)) {
1345                         tdm_debug_dump = 0xFFFFFFFF;
1346                         goto done;
1347                 } else if (!strncmp(arg, "layer", 5)) {
1348                         tdm_debug_dump |= TDM_DUMP_FLAG_LAYER;
1349                 } else if (!strncmp(arg, "pp", 2)) {
1350                         tdm_debug_dump |= TDM_DUMP_FLAG_PP;
1351                 } else if (!strncmp(arg, "capture", 7)) {
1352                         tdm_debug_dump |= TDM_DUMP_FLAG_CAPTURE;
1353                 } else if (!strncmp(arg, "window", 6)) {
1354                         tdm_debug_dump |= TDM_DUMP_FLAG_WINDOW;
1355                 } else
1356                         goto done;
1357
1358                 arg = strtok_r(NULL, ",", &end);
1359         }
1360
1361         if (tdm_debug_dump_dir)
1362                 free(tdm_debug_dump_dir);
1363
1364         tdm_debug_dump_dir = strndup(path, TDM_PATH_LEN);
1365
1366         TDM_INFO("dump... '%s'", dump_str);
1367
1368 done:
1369         if (path)
1370                 free(path);
1371
1372         return TDM_ERROR_NONE;
1373 }
1374
1375 static void
1376 _tdm_display_ttrace_vblank_cb(tdm_vblank *vblank, tdm_error error, unsigned int sequence,
1377                                                           unsigned int tv_sec, unsigned int tv_usec, void *user_data)
1378 {
1379         tdm_error ret = TDM_ERROR_NONE;
1380
1381         TDM_TRACE_MARK("VSYNC");
1382
1383         ret = tdm_vblank_wait(vblank, 0, 0, 1, _tdm_display_ttrace_vblank_cb, NULL);
1384         TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
1385 }
1386
1387 INTERN tdm_error
1388 tdm_display_enable_ttrace_vblank(tdm_display *dpy, tdm_output *output, int enable)
1389 {
1390         tdm_private_display *private_display = dpy;
1391         tdm_private_backend *private_backend = NULL;
1392         tdm_private_output *private_output = NULL;
1393         const tdm_output_mode *mode = NULL;
1394         tdm_vblank *vblank = NULL;
1395         tdm_error ret = TDM_ERROR_NONE;
1396
1397         if (!enable) {
1398                 LIST_FOR_EACH_ENTRY(private_backend, &private_display->backend_list, link) {
1399                         LIST_FOR_EACH_ENTRY(private_output, &private_backend->output_list, link) {
1400                                 if (private_output->ttrace_vblank)
1401                                         tdm_vblank_destroy(private_output->ttrace_vblank);
1402                                 private_output->ttrace_vblank = NULL;
1403                         }
1404                 }
1405                 return TDM_ERROR_NONE;
1406         }
1407
1408         private_output = output;
1409         TDM_RETURN_VAL_IF_FAIL(private_output != NULL, TDM_ERROR_INVALID_PARAMETER);
1410
1411         if (private_output->ttrace_vblank)
1412                 return TDM_ERROR_NONE;
1413
1414         vblank = tdm_vblank_create(private_display, output, &ret);
1415         TDM_RETURN_VAL_IF_FAIL(vblank != NULL, ret);
1416
1417         ret = tdm_output_get_mode(output, &mode);
1418         TDM_GOTO_IF_FAIL(mode != NULL, enable_fail);
1419
1420         ret = tdm_vblank_set_fps(vblank, mode->vrefresh);
1421         TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, enable_fail);
1422
1423         ret = tdm_vblank_set_enable_fake(vblank, 1);
1424         TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, enable_fail);
1425
1426         ret = tdm_vblank_wait(vblank, 0, 0, 1, _tdm_display_ttrace_vblank_cb, NULL);
1427         TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, enable_fail);
1428
1429         private_output->ttrace_vblank = vblank;
1430
1431         return TDM_ERROR_NONE;
1432
1433 enable_fail:
1434         if (vblank)
1435                 tdm_vblank_destroy(vblank);
1436
1437         return ret;
1438 }
1439
1440 INTERN tdm_error
1441 tdm_display_enable_ttrace(tdm_private_display *private_display, const char *ttrace, int output_id, char *reply, int *len)
1442 {
1443         char temp[TDM_PATH_LEN];
1444         char *arg;
1445         char *end;
1446         tdm_output *output;
1447         tdm_error ret;
1448         tdm_output_type type;
1449
1450         snprintf(temp, TDM_PATH_LEN, "%s", ttrace);
1451
1452         tdm_ttrace_output = output_id;
1453         tdm_ttrace_module = 0;
1454
1455         output = tdm_display_get_output(private_display, output_id, &ret);
1456         if (!output) {
1457                 TDM_SNPRINTF(reply, len, "can't find the output_id(%d)\n", output_id);
1458                 return ret;
1459         }
1460
1461         ret = tdm_output_get_output_type(output, &type);
1462         if (ret != TDM_ERROR_NONE) {
1463                 TDM_SNPRINTF(reply, len, "can't find the type of output_id(%d)\n", output_id);
1464                 return ret;
1465         }
1466
1467         arg = strtok_r(temp, TDM_CONFIG_DELIM, &end);
1468         while (arg) {
1469                 if (!strncmp(arg, "none", 4))
1470                         tdm_ttrace_module = 0;
1471                 else if (!strncmp(arg, "all", 3))
1472                         tdm_ttrace_module = 0xFFFFFFFF;
1473                 else if (!strncmp(arg, "vsync", 5))
1474                         tdm_ttrace_module |= TDM_TTRACE_VSYNC;
1475                 else if (!strncmp(arg, "client_vblank", 13))
1476                         tdm_ttrace_module |= TDM_TTRACE_CLIENT_VBLANK;
1477                 else if (!strncmp(arg, "server_vblank", 13))
1478                         tdm_ttrace_module |= TDM_TTRACE_SERVER_VBLANK;
1479                 else if (!strncmp(arg, "vblank", 6))
1480                         tdm_ttrace_module |= TDM_TTRACE_VBLANK;
1481                 else if (!strncmp(arg, "layer", 5))
1482                         tdm_ttrace_module |= TDM_TTRACE_LAYER;
1483                 else if (!strncmp(arg, "pp", 2))
1484                         tdm_ttrace_module |= TDM_TTRACE_PP;
1485                 else if (!strncmp(arg, "capture", 7))
1486                         tdm_ttrace_module |= TDM_TTRACE_CAPTURE;
1487                 else  {
1488                         tdm_ttrace_module = 0;
1489                         tdm_display_enable_ttrace_vblank(private_display, NULL, 0);
1490                         tdm_server_enable_ttrace_client_vblank(private_display, NULL, 0);
1491                         TDM_SNPRINTF(reply, len, "unknown option: '%s'\n", arg);
1492                         return TDM_ERROR_NONE;
1493                 }
1494
1495                 arg = strtok_r(NULL, TDM_CONFIG_DELIM, &end);
1496         }
1497
1498         TDM_SNPRINTF(reply, len, "ttrace debugging... '%s' %x\n", ttrace, tdm_ttrace_module);
1499
1500         if (tdm_ttrace_module & TDM_TTRACE_VSYNC)
1501                 tdm_display_enable_ttrace_vblank(private_display, output, 1);
1502         else
1503                 tdm_display_enable_ttrace_vblank(private_display, NULL, 0);
1504
1505         if (tdm_ttrace_module & TDM_TTRACE_CLIENT_VBLANK)
1506                 tdm_server_enable_ttrace_client_vblank(private_display, output, 1);
1507         else
1508                 tdm_server_enable_ttrace_client_vblank(private_display, NULL, 0);
1509
1510         return TDM_ERROR_NONE;
1511 }
1512
1513 INTERN tdm_error
1514 tdm_display_enable_fps(tdm_private_display *private_display, int enable)
1515 {
1516         private_display->print_fps = enable;
1517
1518         TDM_INFO("print fps: %s", (enable) ? "enable" : "disable");
1519
1520         return TDM_ERROR_NONE;
1521 }
1522 /* LCOV_EXCL_STOP */