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