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