tdm_server: Remove duplicate code
[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 INTERN void *
131 tdm_display_find_private_voutput(tdm_private_display *private_display, double stamp)
132 {
133         tdm_private_module *private_module = NULL;
134         tdm_private_voutput *private_voutput = NULL;
135         tdm_private_output *private_output = NULL;
136
137         private_module = private_display->virtual_module;
138         if (!private_module) return NULL;
139
140         LIST_FOR_EACH_ENTRY(private_voutput, &private_module->voutput_list, link) {
141                 if (!private_voutput->private_output) continue;
142                 private_output = private_voutput->private_output;
143                 if (private_output->stamp == stamp)
144                         return private_voutput;
145         }
146
147         return NULL;
148 }
149
150 tdm_private_hwc *
151 tdm_display_find_private_hwc(tdm_private_display *private_display, tdm_hwc *hwc_backend)
152 {
153         tdm_private_module *private_module = NULL;
154         tdm_private_output *private_output = NULL;
155         tdm_private_hwc *private_hwc = NULL;
156
157         LIST_FOR_EACH_ENTRY(private_module, &private_display->module_list, link) {
158                 LIST_FOR_EACH_ENTRY(private_output, &private_module->output_list, link) {
159                         if (!private_output->private_hwc) continue;
160                         private_hwc = private_output->private_hwc;
161                         if (private_hwc->hwc_backend == hwc_backend)
162                                 return private_hwc;
163                 }
164         }
165
166         return NULL;
167 }
168
169 INTERN void *
170 tdm_display_find_hwc_stamp(tdm_private_display *private_display, double stamp)
171 {
172         tdm_private_module *private_module = NULL;
173         tdm_private_output *private_output = NULL;
174         tdm_private_hwc *private_hwc = NULL;
175
176         LIST_FOR_EACH_ENTRY(private_module, &private_display->module_list, link) {
177                 LIST_FOR_EACH_ENTRY(private_output, &private_module->output_list, link) {
178                         if (!private_output->private_hwc) continue;
179                         private_hwc = private_output->private_hwc;
180                         if (private_hwc->stamp == stamp)
181                                 return private_hwc;
182                 }
183         }
184
185         return NULL;
186 }
187
188 static void
189 _tdm_display_destroy_caps_pp(tdm_caps_pp *caps_pp)
190 {
191         if (caps_pp->formats)
192                 free(caps_pp->formats);
193
194         memset(caps_pp, 0, sizeof(tdm_caps_pp));
195 }
196
197 static void
198 _tdm_display_destroy_caps_capture(tdm_caps_capture *caps_capture)
199 {
200         if (caps_capture->formats)
201                 free(caps_capture->formats);
202
203         memset(caps_capture, 0, sizeof(tdm_caps_capture));
204 }
205
206 static void
207 _tdm_display_destroy_caps_layer(tdm_caps_layer *caps_layer)
208 {
209         if (caps_layer->formats)
210                 free(caps_layer->formats);
211
212         if (caps_layer->props)
213                 free(caps_layer->props);
214
215         memset(caps_layer, 0, sizeof(tdm_caps_layer));
216 }
217
218 static void
219 _tdm_display_destroy_caps_output(tdm_caps_output *caps_output)
220 {
221         if (caps_output->modes)
222                 free(caps_output->modes);
223
224         if (caps_output->props)
225                 free(caps_output->props);
226
227         memset(caps_output, 0, sizeof(tdm_caps_output));
228 }
229
230 static void
231 _tdm_display_destroy_private_layer(tdm_private_layer *private_layer)
232 {
233         tdm_private_capture *c = NULL, *cc = NULL;
234
235         tdm_layer_unset_buffer_internal(private_layer);
236
237         LIST_FOR_EACH_ENTRY_SAFE(c, cc, &private_layer->capture_list, link)
238         tdm_capture_destroy_internal(c);
239
240         _tdm_display_destroy_caps_layer(&private_layer->caps);
241
242         /* when destroying layer, someone could check if layer is valid. So delete
243          * the layer's link at last.
244          */
245         LIST_DEL(&private_layer->link);
246
247         free(private_layer);
248 }
249
250 INTERN void
251 tdm_display_destroy_private_output(tdm_private_output *private_output)
252 {
253         tdm_private_display *private_display = private_output->private_display;
254         tdm_private_layer *l = NULL, *ll = NULL;
255         tdm_private_hwc *private_hwc = NULL;
256         tdm_private_hwc_window *hw = NULL, *hww = NULL;
257         tdm_private_capture *c = NULL, *cc = NULL;
258         tdm_private_output_vblank_handler *v = NULL, *vv = NULL;
259         tdm_private_output_commit_handler *om = NULL, *omm = NULL;
260         tdm_private_hwc_commit_handler *hm = NULL, *hmm = NULL;
261         tdm_private_layer_commit_handler *lm = NULL, *lmm = NULL;
262         tdm_private_output_change_handler *h = NULL, *hh = NULL;
263         tdm_private_output_destroy_handler *dh = NULL, *dhh = NULL;
264         tdm_error ret;
265         int mutex_locked = 0;
266
267         ret = tdm_output_call_thread_cb_destroy(private_output);
268         TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
269
270         free(private_output->layers_ptr);
271
272         if (private_output->vblank_timeout_timer)
273                 tdm_event_loop_source_remove(private_output->vblank_timeout_timer);
274
275         LIST_FOR_EACH_ENTRY_SAFE(v, vv, &private_output->vblank_handler_list, link) {
276                 LIST_DEL(&v->link);
277                 free(v);
278         }
279
280         LIST_FOR_EACH_ENTRY_SAFE(om, omm, &private_output->output_commit_handler_list, link) {
281                 LIST_DEL(&om->link);
282                 free(om);
283         }
284
285         if (private_output->private_hwc) {
286                 private_hwc = private_output->private_hwc;
287                 LIST_FOR_EACH_ENTRY_SAFE(hm, hmm, &private_hwc->hwc_commit_handler_list, link) {
288                         LIST_DEL(&hm->link);
289                         free(hm);
290                 }
291         } else {
292                 LIST_FOR_EACH_ENTRY_SAFE(lm, lmm, &private_output->layer_commit_handler_list, link) {
293                         LIST_DEL(&lm->link);
294                         free(lm);
295                 }
296         }
297
298         LIST_FOR_EACH_ENTRY_SAFE(lm, lmm, &private_output->pending_commit_handler_list, link) {
299                 LIST_DEL(&lm->link);
300                 free(lm);
301         }
302
303         LIST_FOR_EACH_ENTRY_SAFE(h, hh, &private_output->change_handler_list, link) {
304                 LIST_DEL(&h->link);
305                 tdm_thread_cb_remove(h->private_output, TDM_THREAD_CB_OUTPUT_DPMS, NULL, tdm_output_thread_cb_change, h);
306                 tdm_thread_cb_remove(h->private_output, TDM_THREAD_CB_OUTPUT_STATUS, NULL, tdm_output_thread_cb_change, h);
307                 free(h);
308         }
309
310         LIST_FOR_EACH_ENTRY_SAFE(dh, dhh, &private_output->destroy_handler_list, link) {
311                 LIST_DEL(&dh->link);
312                 tdm_thread_cb_remove(dh->private_output, TDM_THREAD_CB_OUTPUT_DESTROY, NULL, tdm_output_thread_cb_destroy, dh);
313                 free(dh);
314         }
315
316         if (tdm_thread_is_running() && TDM_MUTEX_IS_LOCKED()) mutex_locked = 1;
317         if (private_output->vblank) {
318                 /* tdm_vblank APIs is for server. it should be called in unlock status*/
319                 if (mutex_locked) _pthread_mutex_unlock(&private_display->lock);
320                 tdm_vblank_destroy(private_output->vblank);
321                 if (mutex_locked) _pthread_mutex_lock(&private_display->lock);
322         }
323
324         if (private_output->ttrace_vblank) {
325                 /* tdm_vblank APIs is for server. it should be called in unlock status*/
326                 if (mutex_locked) _pthread_mutex_unlock(&private_display->lock);
327                 tdm_vblank_destroy(private_output->ttrace_vblank);
328                 if (mutex_locked) _pthread_mutex_lock(&private_display->lock);
329         }
330
331         LIST_FOR_EACH_ENTRY_SAFE(c, cc, &private_output->capture_list, link)
332                 tdm_capture_destroy_internal(c);
333
334         if (private_output->private_hwc) {
335                 private_hwc = private_output->private_hwc;
336                 LIST_FOR_EACH_ENTRY_SAFE(hw, hww, &private_hwc->hwc_window_list, link)
337                         tdm_hwc_window_destroy_internal(hw);
338                 if (private_hwc->display_target_buffer)
339                         tbm_surface_internal_unref(private_hwc->display_target_buffer);
340                 free(private_hwc);
341         } else {
342                 LIST_FOR_EACH_ENTRY_SAFE(l, ll, &private_output->layer_list, link)
343                         _tdm_display_destroy_private_layer(l);
344         }
345
346         _tdm_display_destroy_caps_output(&private_output->caps);
347
348         /* when destroying output, vblank objects are also destroyed. vblank checks
349          * if output object is valid. So delete the output's link at last.
350          */
351         LIST_DEL(&private_output->link);
352
353         private_output->stamp = 0;
354         free(private_output);
355 }
356
357 static void
358 _tdm_display_destroy_private_display(tdm_private_display *private_display)
359 {
360         tdm_private_module *private_module = NULL, *bb = NULL;
361         tdm_private_output *o = NULL, *oo = NULL;
362         tdm_private_pp *p = NULL, *pp = NULL;
363         tdm_private_output_create_handler *ch = NULL, *chh = NULL;
364
365         LIST_FOR_EACH_ENTRY_SAFE(private_module, bb, &private_display->module_list, link) {
366                 LIST_FOR_EACH_ENTRY_SAFE(p, pp, &private_module->pp_list, link) {
367                         tdm_pp_destroy_internal(p);
368                 }
369
370                 LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_module->output_list, link) {
371                         tdm_display_destroy_private_output(o);
372                 }
373
374                 _tdm_display_destroy_caps_pp(&private_module->caps_pp);
375                 _tdm_display_destroy_caps_capture(&private_module->caps_capture);
376
377                 private_module->capabilities = 0;
378                 private_module->caps_display.max_layer_count = 0;
379
380                 if (private_module->outputs) {
381                         free(private_module->outputs);
382                         private_module->outputs = NULL;
383                 }
384         }
385
386         LIST_FOR_EACH_ENTRY_SAFE(ch, chh, &private_display->output_create_handler_list, link) {
387                 LIST_DEL(&ch->link);
388                 tdm_thread_cb_remove(ch->private_display, TDM_THREAD_CB_DISPLAY_OUTPUT_CREATE, NULL,
389                                                          tdm_display_thread_cb_output_create, ch);
390                 free(ch);
391         }
392 }
393
394 static tdm_error
395 _tdm_display_update_caps_pp(tdm_private_module *private_module, tdm_caps_pp *caps)
396 {
397         tdm_func_display *func_display;
398         tdm_error ret;
399
400         func_display = &private_module->func_display;
401
402         if (!func_display->display_get_pp_capability) {
403                 TDM_ERR("backend(%s) no display_get_pp_capability()", private_module->module_data->name);
404                 return TDM_ERROR_BAD_MODULE;
405         }
406
407         memset(caps, 0, sizeof(tdm_caps_pp));
408
409         ret = func_display->display_get_pp_capability(private_module->bdata, caps);
410         if (ret != TDM_ERROR_NONE) {
411                 TDM_ERR("backend(%s) display_get_pp_capability() failed", private_module->module_data->name);
412                 return TDM_ERROR_BAD_MODULE;
413         }
414
415         return TDM_ERROR_NONE;
416 }
417
418 static tdm_error
419 _tdm_display_update_caps_capture(tdm_private_module *private_module, tdm_caps_capture *caps)
420 {
421         tdm_func_display *func_display;
422         tdm_error ret;
423
424         func_display = &private_module->func_display;
425
426         if (!func_display->display_get_capture_capability) {
427                 TDM_ERR("backend(%s) no display_get_capture_capability()", private_module->module_data->name);
428                 return TDM_ERROR_BAD_MODULE;
429         }
430
431         ret = func_display->display_get_capture_capability(private_module->bdata, caps);
432         if (ret != TDM_ERROR_NONE) {
433                 TDM_ERR("backend(%s) display_get_capture_capability() failed", private_module->module_data->name);
434                 return TDM_ERROR_BAD_MODULE;
435         }
436
437         return TDM_ERROR_NONE;
438 }
439
440 static tdm_error
441 _tdm_display_update_caps_layer(tdm_private_module *private_module,
442                                                            tdm_layer *layer_backend, tdm_caps_layer *caps)
443 {
444         tdm_func_layer *func_layer = &private_module->func_layer;
445         tdm_error ret;
446
447         if (!func_layer->layer_get_capability) {
448                 TDM_ERR("backend(%s) no layer_get_capability()", private_module->module_data->name);
449                 return TDM_ERROR_BAD_MODULE;
450         }
451
452         ret = func_layer->layer_get_capability(layer_backend, caps);
453         if (ret != TDM_ERROR_NONE) {
454                 TDM_ERR("backend(%s) layer_get_capability() failed", private_module->module_data->name);
455                 return TDM_ERROR_BAD_MODULE;
456         }
457
458         return TDM_ERROR_NONE;
459 }
460
461 static tdm_error
462 _tdm_display_update_caps_output(tdm_private_module *private_module, int pipe,
463                                                                 tdm_output *output_backend, tdm_caps_output *caps)
464 {
465         tdm_func_output *func_output = &private_module->func_output;
466         char temp[TDM_NAME_LEN];
467         tdm_error ret;
468         hal_tdm_error hret;
469         double stamp;
470         int n;
471
472         stamp = tdm_helper_get_time();
473         if (private_module->use_hal_tdm) {
474                 hret = hal_tdm_output_get_capability(output_backend, (hal_tdm_caps_output *)caps);
475                 TDM_DBG("backend(HAL-TDM) backend output_get_capability() time: %.3f ms",
476                         (tdm_helper_get_time() - stamp) * 1000.0);
477                 if (hret != HAL_TDM_ERROR_NONE) {
478                         TDM_ERR("backend(HAL-TDM) output_get_capability() failed");
479                         return TDM_ERROR_BAD_MODULE;
480                 }
481         } else {
482                 ret = func_output->output_get_capability(output_backend, caps);
483                 TDM_DBG("backend(%s) backend output_get_capability() time: %.3f ms",
484                                 private_module->module_data->name, (tdm_helper_get_time() - stamp) * 1000.0);
485                 if (ret != TDM_ERROR_NONE) {
486                         TDM_ERR("backend(%s) output_get_capability() failed", private_module->module_data->name);
487                         return TDM_ERROR_BAD_MODULE;
488                 }
489         }
490
491         /* FIXME: Use model for tdm client to distinguish amoung outputs */
492         n = snprintf(temp, sizeof(temp), "%s-%d", caps->model, pipe);
493         if ((size_t)n >= sizeof(temp)) {
494                  temp[sizeof(temp) - 1] = '\0';
495         }
496
497         snprintf(caps->model, TDM_NAME_LEN, "%s", temp);
498
499         return TDM_ERROR_NONE;
500 }
501
502 static tdm_error
503 _tdm_display_update_layer(tdm_private_output *private_output,
504                                                   tdm_layer *layer_backend, int index)
505 {
506         tdm_private_layer *private_layer;
507         tdm_error ret;
508
509         private_layer = _tdm_display_find_private_layer(private_output, layer_backend);
510         if (!private_layer) {
511                 private_layer = calloc(1, sizeof(tdm_private_layer));
512                 TDM_RETURN_VAL_IF_FAIL(private_layer != NULL, TDM_ERROR_OUT_OF_MEMORY);
513
514                 LIST_ADDTAIL(&private_layer->link, &private_output->layer_list);
515                 LIST_INITHEAD(&private_layer->capture_list);
516
517                 private_layer->index = index;
518                 private_layer->private_module = private_output->private_module;
519                 private_layer->private_display = private_output->private_display;
520                 private_layer->private_output = private_output;
521                 private_layer->layer_backend = layer_backend;
522                 private_layer->usable = 1;
523         }
524
525         _tdm_display_destroy_caps_layer(&private_layer->caps);
526
527         ret = _tdm_display_update_caps_layer(private_output->private_module, layer_backend, &private_layer->caps);
528         TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, ret);
529
530         return TDM_ERROR_NONE;
531 }
532
533 INTERN tdm_error
534 tdm_display_update_output(tdm_private_module *private_module, tdm_output *output_backend)
535 {
536         tdm_func_output *func_output = &private_module->func_output;
537         tdm_private_display *private_display = NULL;
538         tdm_private_output *private_output = NULL;
539         tdm_layer **layers = NULL;
540         tdm_private_hwc *private_hwc = NULL;
541         tdm_hwc *hwc;
542         int layer_count = 0, i;
543         tdm_error ret;
544         unsigned int output_created = 0;
545
546         private_display = private_module->private_display;
547
548         private_output = tdm_display_find_private_output(private_display, output_backend);
549         if (!private_output) {
550                 unsigned int pipe = tdm_display_find_empty_output_pipe(private_display);
551
552                 private_output = calloc(1, sizeof(tdm_private_output));
553                 TDM_RETURN_VAL_IF_FAIL(private_output != NULL, TDM_ERROR_OUT_OF_MEMORY);
554
555                 private_output->stamp = tdm_helper_get_time();
556                 while (tdm_display_find_output_stamp(private_display, private_output->stamp))
557                         private_output->stamp++;
558
559                 LIST_ADDTAIL(&private_output->link, &private_module->output_list);
560
561                 private_output->private_module = private_module;
562                 private_output->private_display = private_display;
563                 private_output->current_dpms_value = TDM_OUTPUT_DPMS_OFF;
564                 private_output->output_backend = output_backend;
565                 private_output->pipe = pipe;
566                 private_output->index = pipe;
567
568                 LIST_INITHEAD(&private_output->capture_list);
569                 LIST_INITHEAD(&private_output->vblank_handler_list);
570                 LIST_INITHEAD(&private_output->output_commit_handler_list);
571                 LIST_INITHEAD(&private_output->pending_commit_handler_list);
572                 LIST_INITHEAD(&private_output->destroy_handler_list);
573                 LIST_INITHEAD(&private_output->change_handler_list);
574                 LIST_INITHEAD(&private_output->mode_change_request_handler_list);
575
576                 if (private_module->use_hal_tdm) {
577                         hal_tdm_error hret;
578                         hret = hal_tdm_output_set_status_handler(private_output->output_backend, tdm_output_cb_status_hal_tdm, private_output);
579                         if (hret == HAL_TDM_ERROR_NONE)
580                                 private_output->regist_change_cb = 1;
581                 } else {
582                         if (func_output->output_set_status_handler) {
583                                 func_output->output_set_status_handler(private_output->output_backend,
584                                                                                                            tdm_output_cb_status,
585                                                                                                            private_output);
586                                 private_output->regist_change_cb = 1;
587                         }
588                 }
589
590                 output_created = 1;
591
592                 /* NOTE that output modes will be allocated newly after here */
593                 _tdm_display_destroy_caps_output(&private_output->caps);
594                 ret = _tdm_display_update_caps_output(private_module, private_output->pipe, output_backend, &private_output->caps);
595                 TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, ret);
596
597                 if (private_output->caps.status == TDM_OUTPUT_CONN_STATUS_DISCONNECTED)
598                         private_output->current_mode = NULL;
599
600                 /* get the tdm_hwc object */
601                 if (private_output->caps.capabilities & TDM_OUTPUT_CAPABILITY_HWC) {
602                         if (private_module->use_hal_tdm)
603                                 hwc = (tdm_hwc *)hal_tdm_output_get_hwc(output_backend, (hal_tdm_error *)&ret);
604                         else
605                                 hwc = func_output->output_get_hwc(output_backend, &ret);
606                         TDM_RETURN_VAL_IF_FAIL(hwc != NULL, ret);
607
608                         private_hwc = calloc(1, sizeof(tdm_private_hwc));
609                         TDM_RETURN_VAL_IF_FAIL(private_hwc != NULL, TDM_ERROR_OUT_OF_MEMORY);
610
611                         private_hwc->private_module = private_module;
612                         private_hwc->private_output = private_output;
613                         private_hwc->hwc_backend = hwc;
614                         private_hwc->index = pipe;
615                         private_hwc->stamp = tdm_helper_get_time();
616                         while (tdm_display_find_hwc_stamp(private_module->private_display, private_hwc->stamp))
617                                 private_hwc->stamp++;
618
619                         LIST_INITHEAD(&private_hwc->hwc_window_list);
620                         LIST_INITHEAD(&private_hwc->hwc_commit_handler_list);
621
622                         private_output->private_hwc = private_hwc;
623                 } else {
624                         LIST_INITHEAD(&private_output->layer_list);
625                         LIST_INITHEAD(&private_output->layer_commit_handler_list);
626                 }
627         } else {
628                 _tdm_display_destroy_caps_output(&private_output->caps);
629                 ret = _tdm_display_update_caps_output(private_module, private_output->pipe, output_backend, &private_output->caps);
630                 TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, ret);
631
632                 if (private_output->caps.status == TDM_OUTPUT_CONN_STATUS_DISCONNECTED)
633                         private_output->current_mode = NULL;
634         }
635
636         /* do not use the layer object when the tdm_output has the hwc capability */
637         if ((private_output->caps.capabilities & TDM_OUTPUT_CAPABILITY_HWC) == 0) {
638                 if (private_module->use_hal_tdm) {
639                         TDM_ERR("hal-tdm not support layer");
640                         return TDM_ERROR_NOT_IMPLEMENTED;
641                 }
642                 layers = func_output->output_get_layers(output_backend, &layer_count, &ret);
643                 if (ret != TDM_ERROR_NONE) {
644                         free(layers);
645                         return ret;
646                 }
647
648                 for (i = 0; i < layer_count; i++) {
649                         ret = _tdm_display_update_layer(private_output, layers[i], i);
650                         if (ret != TDM_ERROR_NONE) {
651                                 free(layers);
652                                 return ret;
653                         }
654                 }
655
656                 free(layers);
657         }
658
659         if (output_created) {
660                 ret = tdm_display_call_thread_cb_output_create(private_display, private_output);
661                 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
662         }
663
664         return TDM_ERROR_NONE;
665 }
666
667 static void
668 _tdm_display_set_main_first(tdm_output **outputs, int index)
669 {
670         tdm_output *output_tmp = NULL;
671
672         if (index == 0)
673                 return;
674
675         output_tmp = outputs[0];
676         outputs[0] = outputs[index];
677         outputs[index] = output_tmp;
678 }
679
680 static tdm_output **
681 _tdm_display_get_ordered_outputs(tdm_private_module *private_module, int *count)
682 {
683         tdm_func_display *func_display = &private_module->func_display;
684         tdm_output **outputs = NULL;
685         tdm_output *output_dsi = NULL;
686         tdm_output *output_lvds = NULL;
687         tdm_output *output_hdmia = NULL;
688         tdm_output *output_hdmib = NULL;
689         int i, output_count = 0, output_connected_count = 0;
690         int index_dsi = 0, index_lvds = 0, index_hdmia = 0, index_hdmib = 0;
691         tdm_error ret;
692
693         /* don't change list order if not init time */
694         if (private_module->outputs) {
695                 TDM_ERR("can't change output order");
696                 return private_module->outputs;
697         }
698
699         if (private_module->use_hal_tdm)
700                 outputs = (tdm_output **)hal_tdm_display_get_outputs(private_module->htdm_dpy, &output_count, (hal_tdm_error *)&ret);
701         else
702                 outputs = func_display->display_get_outputs(private_module->bdata, &output_count, &ret);
703         if (ret != TDM_ERROR_NONE)
704                 goto no_output;
705
706         *count = output_count;
707
708         if (output_count == 0)
709                 goto no_output;
710         else if (output_count == 1) {
711                 private_module->outputs = outputs;
712                 return outputs;
713         }
714
715         /* count connected outputs */
716         for (i = 0; i < output_count; i++) {
717                 tdm_func_output *func_output = &private_module->func_output;
718                 tdm_caps_output caps;
719                 memset(&caps, 0, sizeof(tdm_caps_output));
720
721                 if (private_module->use_hal_tdm)
722                         ret = (tdm_error)hal_tdm_output_get_capability(outputs[i], (hal_tdm_caps_output *)&caps);
723                 else
724                         ret = func_output->output_get_capability(outputs[i], &caps);
725                 if (ret != TDM_ERROR_NONE) {
726                         TDM_ERR("output_get_capability() failed");
727                         goto no_output;
728                 }
729
730                 if (caps.status == TDM_OUTPUT_CONN_STATUS_CONNECTED) {
731                         output_connected_count++;
732
733                         switch (caps.type) {
734                         case TDM_OUTPUT_TYPE_DSI:
735                                 if (output_dsi != NULL) continue;
736                                 output_dsi = outputs[i];
737                                 index_dsi = i;
738                                 break;
739                         case TDM_OUTPUT_TYPE_LVDS:
740                                 if (output_lvds != NULL) continue;
741                                 output_lvds = outputs[i];
742                                 index_lvds = i;
743                                 break;
744                         case TDM_OUTPUT_TYPE_HDMIA:
745                                 if (output_hdmia != NULL) continue;
746                                 output_hdmia = outputs[i];
747                                 index_hdmia = i;
748                                 break;
749                         case TDM_OUTPUT_TYPE_HDMIB:
750                                 if (output_hdmib != NULL) continue;
751                                 output_hdmib = outputs[i];
752                                 index_hdmib = i;
753                                 break;
754                         default:
755                                 break;
756                         }
757                 }
758
759                 _tdm_display_destroy_caps_output(&caps);
760         }
761
762         /* ordering : main output is first */
763         /* If there is no connected output, lvds or dsi cannot be main display. (cannot connect after booting)
764           * But hdmi is possible, so set hdmi to main display.
765           * If connected only one output, it is main output.
766           * If connected outputs over 2, has priority like below.
767           * (dsi > lvds > hdmi > else)
768           */
769         if (output_connected_count == 0) {
770                 /* hdmi > dsi > lvds > else */
771                 if (output_hdmia != NULL)
772                         _tdm_display_set_main_first(outputs, index_hdmia);
773                 else if (output_hdmib != NULL)
774                         _tdm_display_set_main_first(outputs, index_hdmib);
775                 else if (output_dsi != NULL)
776                         _tdm_display_set_main_first(outputs, index_dsi);
777                 else if (output_lvds != NULL)
778                         _tdm_display_set_main_first(outputs, index_lvds);
779         } else { /* (output_connected_count > 1) */
780                 /* dsi > lvds > hdmi > else */
781                 if (output_dsi != NULL)
782                         _tdm_display_set_main_first(outputs, index_dsi);
783                 else if (output_lvds != NULL)
784                         _tdm_display_set_main_first(outputs, index_lvds);
785                 else if (output_hdmia != NULL)
786                         _tdm_display_set_main_first(outputs, index_hdmia);
787                 else if (output_hdmib != NULL)
788                         _tdm_display_set_main_first(outputs, index_hdmib);
789         }
790
791         private_module->outputs = outputs;
792
793         return outputs;
794
795 no_output:
796         free(outputs);
797         *count = 0;
798         return NULL;
799 }
800
801 static tdm_error
802 _tdm_display_setup(tdm_private_display *private_display)
803 {
804         tdm_private_module *private_module = NULL;
805         tdm_error ret = TDM_ERROR_NONE;
806         int output_count = 0;
807         int virtual = 0;
808         int virtual_hwc_target_buffer_bo_default = 0;
809
810         if (private_display->pp_module && !private_display->pp_module->use_hal_tdm) {
811                 ret = _tdm_display_update_caps_pp(private_display->pp_module,
812                                                                                   &private_display->pp_module->caps_pp);
813                 if (ret != TDM_ERROR_NONE)
814                         goto failed_update;
815         }
816
817         if (private_display->capture_module && !private_display->capture_module->use_hal_tdm) {
818                 ret = _tdm_display_update_caps_capture(private_display->capture_module,
819                                                                                            &private_display->capture_module->caps_capture);
820                 if (ret != TDM_ERROR_NONE)
821                         goto failed_update;
822         }
823
824         LIST_FOR_EACH_ENTRY(private_module, &private_display->module_list, link) {
825                 tdm_output **outputs;
826                 int i, count = 0;
827
828                 outputs = _tdm_display_get_ordered_outputs(private_module, &count);
829
830                 if (count > 0)
831                         TDM_GOTO_IF_FAIL(outputs != NULL, failed_update);
832
833                 for (i = 0; i < count; i++) {
834                         ret = tdm_display_update_output(private_module, outputs[i]);
835                         if (ret != TDM_ERROR_NONE)
836                                 goto failed_update;
837                         output_count++;
838                 }
839         }
840
841         /* At least, the output count should be greater than 0 to ensure tdm_vblank works.
842          * So we will create a dummy output when backends don't have any output.
843          * Without destroying a tdm_output object, this dummy output will be replaced with
844          * a virtual output which is created in runtime.
845          */
846         if (output_count == 0) {
847                 tdm_output **outputs;
848                 int i, count = 0;
849
850                 TDM_INIT_INFO("loading a %s backend", TDM_DUMMY_MODULE);
851                 ret = _tdm_display_load_module_with_file(private_display, TDM_DUMMY_MODULE);
852                 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed_update);
853                 TDM_GOTO_IF_FAIL(private_display->dummy_module != NULL, failed_update);
854
855                 private_module = private_display->dummy_module;
856
857                 outputs = _tdm_display_get_ordered_outputs(private_module, &count);
858                 TDM_GOTO_IF_FAIL(count > 0, failed_update);
859                 TDM_GOTO_IF_FAIL(outputs != NULL, failed_update);
860
861                 for (i = 0; i < count; i++) {
862                         ret = tdm_display_update_output(private_module, outputs[i]);
863                         if (ret != TDM_ERROR_NONE)
864                                 goto failed_update;
865                 }
866         }
867
868         virtual = tdm_config_get_int(TDM_CONFIG_KEY_GENERAL_VIRTUAL_OUTPUT, 0);
869         if (virtual) {
870                 TDM_INIT_INFO("loading a %s backend", TDM_VIRTUAL_MODULE);
871                 ret = _tdm_display_load_module_with_file(private_display, TDM_VIRTUAL_MODULE);
872                 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed_update);
873                 TDM_GOTO_IF_FAIL(private_display->virtual_module != NULL, failed_update);
874
875                 virtual_hwc_target_buffer_bo_default = tdm_config_get_int(TDM_CONFIG_KEY_GENERAL_VIRTUAL_OUTPUT_HWC_TARGET_BO_DEFAULT, 0);
876                 if (virtual_hwc_target_buffer_bo_default)
877                         private_display->virtual_module->use_default_type_bo = 1;
878         }
879
880         return TDM_ERROR_NONE;
881
882 failed_update:
883         _tdm_display_destroy_private_display(private_display);
884         return ret;
885 }
886 /* LCOV_EXCL_STOP */
887
888 EXTERN tdm_error
889 tdm_display_update(tdm_display *dpy)
890 {
891         TDM_RETURN_VAL_IF_FAIL(dpy != NULL, TDM_ERROR_INVALID_PARAMETER);
892
893         TDM_DEPRECATED(NULL);
894
895         return TDM_ERROR_NONE;
896 }
897
898 int tdm_debug_module;
899 int tdm_debug_dump;
900 int tdm_ttrace_module;
901 int tdm_ttrace_output;
902
903 static tdm_private_display *g_private_display;
904 static pthread_mutex_t gLock = PTHREAD_MUTEX_INITIALIZER;
905
906 /* LCOV_EXCL_START */
907 static tdm_error
908 _tdm_display_load_hal_backend(tdm_private_display *private_display)
909 {
910         hal_tdm_backend *tdm_backend = NULL;
911         hal_tdm_display *hal_tdm_dpy = NULL;
912         tdm_private_module *private_module = NULL;
913         tdm_backend_module *module_data = NULL;
914         hal_tdm_event_source **event_sources = NULL;
915         hal_tdm_caps_display caps;
916         hal_tdm_caps_pp pp_caps;
917         hal_tdm_caps_capture capture_caps;
918         hal_tdm_error hret;
919         hal_tdm_fd master_drm_fd = -1;
920         tdm_error error;
921         int num = 0;
922
923         tdm_backend = hal_tdm_get_backend(&hret);
924         if (tdm_backend == NULL || hret != HAL_TDM_ERROR_NONE) {
925                 TDM_ERR("failed hal_tdm_get_backend");
926                 return TDM_ERROR_NO_MODULE;
927         }
928
929         private_module = calloc(1, sizeof *private_module);
930         if (private_module == NULL) {
931                 TDM_ERR("failed: alloc");
932                 hal_tdm_put_backend(tdm_backend);
933                 return TDM_ERROR_OUT_OF_MEMORY;
934         }
935
936         module_data = calloc(1, sizeof *module_data);
937         if (module_data == NULL) {
938                 TDM_ERR("failed: alloc");
939                 goto fail;
940         }
941         module_data->name = hal_tdm_backend_get_name(tdm_backend);
942         module_data->vendor = hal_tdm_backend_get_vendor(tdm_backend);
943         module_data->abi_version = hal_tdm_backend_get_abi_version(tdm_backend);
944
945         hal_tdm_dpy = hal_tdm_backend_get_display(tdm_backend, &hret);
946         if (hal_tdm_dpy == NULL || hret != HAL_TDM_ERROR_NONE) {
947                 TDM_ERR("failed hal_tdm_backend_get_display");
948                 goto fail;
949         }
950
951         if (hal_tdm_backend_has_drm_device(tdm_backend, &hret)) {
952                 master_drm_fd = hal_tdm_backend_get_master_drm_fd(tdm_backend, &hret);
953                 if (master_drm_fd < 0) {
954                         TDM_INFO("tdm_backend has no master drm_fd.");
955
956                         master_drm_fd = tbm_drm_helper_get_master_fd();
957                         if (master_drm_fd < 0) {
958                                 TDM_ERR("failed to get master_drm_fd");
959                                 goto fail;
960                         }
961                         TDM_INFO("libtbm gets a master drm_fd from libtbm via tbm_drm_helper.");
962
963                         TDM_INFO("libtdm sends a master drm_fd to tdm_backend.");
964                         hret = hal_tdm_backend_set_master_drm_fd(tdm_backend, master_drm_fd);
965                         if (hret != HAL_TDM_ERROR_NONE) {
966                                 TDM_ERR("failed hal_tdm_backend_get_display");
967                                 goto fail;
968                         }
969                 } else {
970                         TDM_INFO("tdm_backend has a master drm_fd.");
971
972                         tbm_drm_helper_set_tbm_master_fd(master_drm_fd);
973                 }
974         }
975
976         if (hal_tdm_display_get_capability(hal_tdm_dpy, &caps) != HAL_TDM_ERROR_NONE) {
977                 TDM_ERR("failed hal_tdm_display_get_capability");
978                 goto fail;
979         }
980
981         private_module->use_hal_tdm = 1;
982         private_module->htdm_backend = tdm_backend;
983         private_module->module_data = module_data;
984         private_module->htdm_dpy = hal_tdm_dpy;
985         private_module->private_display = private_display;
986         private_display->current_module = private_module;
987
988         if (hal_tdm_display_get_pp_capability(hal_tdm_dpy, &pp_caps) == HAL_TDM_ERROR_NONE) {
989                 private_display->pp_module = private_module;
990                 private_module->caps_pp.capabilities = (tdm_pp_capability)pp_caps.capabilities;
991                 private_module->caps_pp.format_count = pp_caps.format_count;
992                 private_module->caps_pp.formats = pp_caps.formats;
993                 private_module->caps_pp.min_w = pp_caps.min_w;
994                 private_module->caps_pp.min_h = pp_caps.min_h;
995                 private_module->caps_pp.max_w = pp_caps.max_w;
996                 private_module->caps_pp.max_h = pp_caps.max_h;
997                 private_module->caps_pp.preferred_align = pp_caps.preferred_align;
998                 private_module->caps_pp.preferred_align_vertical = pp_caps.preferred_align_vertical;
999                 private_module->caps_pp.max_attach_count = pp_caps.max_attach_count;
1000
1001                 private_module->capabilities |= TDM_DISPLAY_CAPABILITY_PP;
1002         }
1003         if (hal_tdm_display_get_capture_capability(hal_tdm_dpy, &capture_caps) == HAL_TDM_ERROR_NONE) {
1004                 private_display->capture_module = private_module;
1005                 private_module->caps_capture.capabilities = (tdm_capture_capability)capture_caps.capabilities;
1006                 private_module->caps_capture.format_count = capture_caps.format_count;
1007                 private_module->caps_capture.formats = capture_caps.formats;
1008                 private_module->caps_capture.min_w = capture_caps.min_w;
1009                 private_module->caps_capture.min_h = capture_caps.min_h;
1010                 private_module->caps_capture.max_w = capture_caps.max_w;
1011                 private_module->caps_capture.max_h = capture_caps.max_h;
1012                 private_module->caps_capture.preferred_align = capture_caps.preferred_align;
1013                 private_module->caps_capture.max_attach_count = capture_caps.max_attach_count;
1014
1015                 private_module->capabilities |= TDM_DISPLAY_CAPABILITY_CAPTURE;
1016         }
1017
1018         LIST_INITHEAD(&private_module->output_list);
1019         LIST_INITHEAD(&private_module->voutput_list);
1020         LIST_INITHEAD(&private_module->pp_list);
1021         LIST_INITHEAD(&private_module->capture_list);
1022
1023         LIST_ADDTAIL(&private_module->link, &private_display->module_list);
1024
1025         event_sources = hal_tdm_backend_get_event_sources(tdm_backend, &num, &hret);
1026         if (num != 0 && hret == HAL_TDM_ERROR_NONE) {
1027                 private_module->event_source_num = num;
1028                 for (int i = 0; i < num; i++) {
1029                         hal_tdm_event_source *event_source = event_sources[i];
1030                         private_module->htdm_event_source[i] = tdm_event_loop_add_fd_handler(private_display, event_source->event_fd,
1031                                                                           TDM_EVENT_LOOP_READABLE, (tdm_event_loop_fd_handler)event_source->func, event_source->user_data, &error);
1032                 }
1033         }
1034
1035         TDM_INIT_INFO("use HAL-TDM_API");
1036
1037         return TDM_ERROR_NONE;
1038
1039 fail:
1040         if (master_drm_fd >= 0)
1041                 close(master_drm_fd);
1042         hal_tdm_put_backend(tdm_backend);
1043         if (module_data) {
1044                 if (module_data->name)
1045                         free(module_data->name);
1046                 if (module_data->vendor)
1047                         free(module_data->vendor);
1048                 free(module_data);
1049         }
1050         free(private_module);
1051
1052         return TDM_ERROR_NO_MODULE;
1053 }
1054
1055 static tdm_error
1056 _tdm_display_check_module(tdm_backend_module *module)
1057 {
1058         int tdm_backend_major, tdm_backend_minor;
1059         int backend_module_major, backend_module_minor;
1060
1061         tdm_backend_major = TDM_BACKEND_GET_ABI_MAJOR(TDM_BACKEND_ABI_LATEST_VERSION);
1062         tdm_backend_minor = TDM_BACKEND_GET_ABI_MINOR(TDM_BACKEND_ABI_LATEST_VERSION);
1063
1064         TDM_INFO("TDM Backend ABI version : %d.%d",
1065                          tdm_backend_major, tdm_backend_minor);
1066
1067         if (!module->name) {
1068                 TDM_ERR("TDM backend doesn't have name");
1069                 return TDM_ERROR_BAD_MODULE;
1070         }
1071
1072         if (!module->vendor) {
1073                 TDM_ERR("TDM backend doesn't have vendor");
1074                 return TDM_ERROR_BAD_MODULE;
1075         }
1076
1077         backend_module_major = TDM_BACKEND_GET_ABI_MAJOR(module->abi_version);
1078         backend_module_minor = TDM_BACKEND_GET_ABI_MINOR(module->abi_version);
1079
1080         TDM_INFO("TDM module name: %s", module->name);
1081         TDM_INFO("'%s' vendor: %s", module->name, module->vendor);
1082         TDM_INFO("'%s' backend ABI version: %d.%d", module->name, backend_module_major, backend_module_minor);
1083
1084         if (backend_module_major > tdm_backend_major) {
1085                 TDM_ERR("'%s' major version(%d) is newer than %d",
1086                                 module->name, backend_module_major, tdm_backend_major);
1087                 return TDM_ERROR_BAD_MODULE;
1088         }
1089
1090         if (tdm_backend_major == backend_module_major &&
1091                 backend_module_minor > tdm_backend_minor) {
1092                 TDM_ERR("'%s' minor version(%d) is newer than %d",
1093                                 module->name, backend_module_minor, tdm_backend_minor);
1094                 return TDM_ERROR_BAD_MODULE;
1095         }
1096
1097         if (!module->init) {
1098                 TDM_ERR("'%s' doesn't have init function", module->name);
1099                 return TDM_ERROR_BAD_MODULE;
1100         }
1101
1102         if (!module->deinit) {
1103                 TDM_ERR("'%s' doesn't have deinit function", module->name);
1104                 return TDM_ERROR_BAD_MODULE;
1105         }
1106
1107         return TDM_ERROR_NONE;
1108 }
1109
1110 static tdm_error
1111 _tdm_display_check_backend_functions(tdm_private_module *private_module)
1112 {
1113         tdm_func_display *func_display = &private_module->func_display;
1114         tdm_func_output *func_output = &private_module->func_output;
1115         tdm_func_layer *func_layer = &private_module->func_layer;
1116         tdm_error ret;
1117
1118         /* below functions should be implemented in backend side */
1119
1120         TDM_RETURN_VAL_IF_FAIL(func_display != NULL, TDM_ERROR_BAD_MODULE);
1121         TDM_RETURN_VAL_IF_FAIL(func_display->display_get_capability, TDM_ERROR_BAD_MODULE);
1122         TDM_RETURN_VAL_IF_FAIL(func_display->display_get_outputs, TDM_ERROR_BAD_MODULE);
1123         TDM_RETURN_VAL_IF_FAIL(func_output->output_get_capability, TDM_ERROR_BAD_MODULE);
1124         TDM_RETURN_VAL_IF_FAIL(func_output->output_get_layers, TDM_ERROR_BAD_MODULE);
1125         TDM_RETURN_VAL_IF_FAIL(func_output->output_wait_vblank, TDM_ERROR_BAD_MODULE);
1126         TDM_RETURN_VAL_IF_FAIL(func_output->output_set_vblank_handler, TDM_ERROR_BAD_MODULE);
1127         TDM_RETURN_VAL_IF_FAIL(func_output->output_commit, TDM_ERROR_BAD_MODULE);
1128         TDM_RETURN_VAL_IF_FAIL(func_output->output_set_commit_handler, TDM_ERROR_BAD_MODULE);
1129         TDM_RETURN_VAL_IF_FAIL(func_output->output_set_mode, TDM_ERROR_BAD_MODULE);
1130         TDM_RETURN_VAL_IF_FAIL(func_layer->layer_get_capability, TDM_ERROR_BAD_MODULE);
1131         TDM_RETURN_VAL_IF_FAIL(func_layer->layer_set_info, TDM_ERROR_BAD_MODULE);
1132         TDM_RETURN_VAL_IF_FAIL(func_layer->layer_get_info, TDM_ERROR_BAD_MODULE);
1133         TDM_RETURN_VAL_IF_FAIL(func_layer->layer_set_buffer, TDM_ERROR_BAD_MODULE);
1134         TDM_RETURN_VAL_IF_FAIL(func_layer->layer_unset_buffer, TDM_ERROR_BAD_MODULE);
1135
1136         ret = func_display->display_get_capability(private_module->bdata, &private_module->caps_display);
1137         if (ret != TDM_ERROR_NONE) {
1138                 TDM_ERR("display_get_capability() failed");
1139                 return TDM_ERROR_BAD_MODULE;
1140         }
1141
1142         if (private_module->capabilities & TDM_DISPLAY_CAPABILITY_PP) {
1143                 tdm_func_pp *func_pp = &private_module->func_pp;
1144                 TDM_RETURN_VAL_IF_FAIL(func_display->display_get_pp_capability, TDM_ERROR_BAD_MODULE);
1145                 TDM_RETURN_VAL_IF_FAIL(func_display->display_create_pp, TDM_ERROR_BAD_MODULE);
1146                 TDM_RETURN_VAL_IF_FAIL(func_pp->pp_destroy, TDM_ERROR_BAD_MODULE);
1147                 TDM_RETURN_VAL_IF_FAIL(func_pp->pp_commit, TDM_ERROR_BAD_MODULE);
1148                 TDM_RETURN_VAL_IF_FAIL(func_pp->pp_set_done_handler, TDM_ERROR_BAD_MODULE);
1149         }
1150
1151         if (private_module->capabilities & TDM_DISPLAY_CAPABILITY_CAPTURE) {
1152                 tdm_func_capture *func_capture = &private_module->func_capture;
1153                 TDM_RETURN_VAL_IF_FAIL(func_display->display_get_capture_capability, TDM_ERROR_BAD_MODULE);
1154                 if (private_module->caps_capture.capabilities & TDM_CAPTURE_CAPABILITY_OUTPUT)
1155                         TDM_RETURN_VAL_IF_FAIL(func_output->output_create_capture, TDM_ERROR_BAD_MODULE);
1156                 if (private_module->caps_capture.capabilities & TDM_CAPTURE_CAPABILITY_LAYER)
1157                         TDM_RETURN_VAL_IF_FAIL(func_layer->layer_create_capture, TDM_ERROR_BAD_MODULE);
1158                 TDM_RETURN_VAL_IF_FAIL(func_capture->capture_destroy, TDM_ERROR_BAD_MODULE);
1159                 TDM_RETURN_VAL_IF_FAIL(func_capture->capture_commit, TDM_ERROR_BAD_MODULE);
1160                 TDM_RETURN_VAL_IF_FAIL(func_capture->capture_set_done_handler, TDM_ERROR_BAD_MODULE);
1161         }
1162
1163         return TDM_ERROR_NONE;
1164 }
1165
1166 static tdm_error
1167 _tdm_display_load_module_with_file(tdm_private_display *private_display,
1168                                                                    const char *file)
1169 {
1170         char path[TDM_PATH_LEN] = {0,};
1171         void *module = NULL;
1172         tdm_backend_module *module_data;
1173         tdm_backend_data *bdata = NULL;
1174         tdm_private_module *private_module = NULL;
1175         tdm_error ret;
1176         double stamp;
1177         int size;
1178
1179         size = snprintf(path, sizeof(path), TDM_MODULE_PATH "/%s", file);
1180         if (size >= (int)sizeof(path)) {
1181                 TDM_WRN("too long: %s/%s", TDM_MODULE_PATH, file);
1182                 return TDM_ERROR_BAD_MODULE;
1183         };
1184
1185         private_module = calloc(1, sizeof *private_module);
1186         if (!private_module) {
1187                 TDM_ERR("alloc failed: %m");
1188                 ret = TDM_ERROR_OUT_OF_MEMORY;
1189                 goto failed_load;
1190         }
1191
1192         private_module->private_display = private_display;
1193
1194         stamp = tdm_helper_get_time();
1195
1196         TDM_TRACE_BEGIN("TDM_Load_Backend");
1197         module = dlopen(path, RTLD_LAZY);
1198         if (!module) {
1199                 TDM_ERR("%s", dlerror());
1200                 TDM_TRACE_END();
1201                 ret = TDM_ERROR_NO_MODULE;
1202                 goto failed_load;
1203         }
1204
1205         private_module->module = module;
1206
1207         module_data = dlsym(module, "tdm_backend_module_data");
1208         if (!module_data) {
1209                 TDM_ERR("'%s' doesn't have data object", file);
1210                 ret = TDM_ERROR_BAD_MODULE;
1211                 TDM_TRACE_END();
1212                 goto failed_load;
1213         }
1214         TDM_TRACE_END();
1215
1216         private_module->module_data = module_data;
1217
1218         TDM_DBG("dlopen, dlsym time: %.3f ms", (tdm_helper_get_time() - stamp) * 1000.0);
1219
1220         /* check if version, init() and deinit() are valid or not */
1221         ret = _tdm_display_check_module(module_data);
1222         if (ret != TDM_ERROR_NONE) {
1223                 TDM_ERR("backend(%s) load failed: check module error", module_data->name);
1224                 goto failed_load;
1225         }
1226
1227         private_display->current_module = private_module;
1228
1229         /* We don't care if backend_data is NULL or not. It's up to backend. */
1230         TDM_TRACE_BEGIN("TDM_Init_Backend");
1231         stamp = tdm_helper_get_time();
1232         TDM_INIT_INFO("init backend");
1233         bdata = module_data->init((tdm_display *)private_display, &ret);
1234         TDM_INIT_INFO("init backend done");
1235         TDM_DBG("backend init() time: %.3f ms", (tdm_helper_get_time() - stamp) * 1000.0);
1236         TDM_TRACE_END();
1237
1238         private_display->current_module = NULL;
1239
1240         if (!strncmp(file, TDM_DUMMY_MODULE, TDM_NAME_LEN))
1241                 private_display->dummy_module = private_module;
1242
1243         if (!strncmp(file, TDM_VIRTUAL_MODULE, TDM_NAME_LEN))
1244                 private_display->virtual_module = private_module;
1245
1246         private_module->bdata = bdata;
1247
1248         if (ret != TDM_ERROR_NONE) {
1249                 TDM_ERR("backend(%s) load failed: init error", module_data->name);
1250                 goto failed_load;
1251         }
1252
1253         ret = _tdm_display_check_backend_functions(private_module);
1254         if (ret != TDM_ERROR_NONE) {
1255                 TDM_ERR("backend(%s) load failed: check functions error", module_data->name);
1256                 goto failed_load;
1257         }
1258
1259         LIST_INITHEAD(&private_module->output_list);
1260         LIST_INITHEAD(&private_module->voutput_list);
1261         LIST_INITHEAD(&private_module->pp_list);
1262         LIST_INITHEAD(&private_module->capture_list);
1263
1264         LIST_ADDTAIL(&private_module->link, &private_display->module_list);
1265
1266         TDM_INIT_INFO("Success to load '%s' module", module_data->name);
1267
1268         return TDM_ERROR_NONE;
1269 failed_load:
1270         if (bdata)
1271                 module_data->deinit(bdata);
1272         if (module)
1273                 dlclose(module);
1274         if (private_module)
1275                 free(private_module);
1276         return ret;
1277 }
1278
1279 static tdm_error
1280 _tdm_display_load_modules(tdm_private_display *private_display)
1281 {
1282         const char *module_names;
1283         tdm_error ret = TDM_ERROR_NONE;
1284         char temp[TDM_PATH_LEN];
1285         char *arg;
1286         char *end;
1287
1288         LIST_INITHEAD(&private_display->module_list);
1289
1290         if (_tdm_display_load_hal_backend(private_display) == TDM_ERROR_NONE) {
1291                 TDM_INIT_INFO("use hal tdm");
1292                 return ret;
1293         }
1294
1295         module_names = tdm_config_get_string(TDM_CONFIG_KEY_GENERAL_BACKENDS, TDM_DEFAULT_MODULE);
1296
1297         snprintf(temp, TDM_PATH_LEN, "%s", module_names);
1298
1299         arg = strtok_r(temp, TDM_CONFIG_DELIM, &end);
1300         while (arg) {
1301                 TDM_INIT_INFO("loading a %s backend", arg);
1302                 ret = _tdm_display_load_module_with_file(private_display, arg);
1303                 if (ret == TDM_ERROR_NONE)
1304                         TDM_INIT_INFO("%s backend loading success", arg);
1305                 else
1306                         TDM_INIT_INFO("%s backend loading failed", arg);
1307                 arg = strtok_r(NULL, TDM_CONFIG_DELIM, &end);
1308         }
1309
1310         return ret;
1311 }
1312
1313 static void
1314 _tdm_display_unload_modules(tdm_private_display *private_display)
1315 {
1316         tdm_private_module *private_module = NULL, *bb = NULL;
1317         tdm_backend_module *module_data = NULL;
1318
1319         LIST_FOR_EACH_ENTRY_SAFE(private_module, bb, &private_display->module_list, link) {
1320                 LIST_DEL(&private_module->link);
1321                 if (private_module->use_hal_tdm) {
1322                         if (private_module->event_source_num != 0) {
1323                                 for (int i = 0; i < private_module->event_source_num; i++) {
1324                                         tdm_event_loop_source *htdm_event_source = private_module->htdm_event_source[i];
1325                                         tdm_event_loop_source_remove(htdm_event_source);
1326                                 }
1327                         }
1328
1329                         module_data = private_module->module_data;
1330                         if (module_data){
1331                                 free(module_data->name);
1332                                 free(module_data->vendor);
1333                                 free(module_data);
1334                         }
1335                         hal_tdm_put_backend(private_module->htdm_backend);
1336                         free(private_module);
1337                 } else {
1338                         if (private_module->module_data)
1339                                 private_module->module_data->deinit(private_module->bdata);
1340                         if (private_module->module)
1341                                 dlclose(private_module->module);
1342
1343                         free(private_module);
1344                 }
1345         }
1346 }
1347 /* LCOV_EXCL_STOP */
1348
1349 INTERN void *
1350 tdm_display_find_stamp(tdm_private_display *private_display, double stamp)
1351 {
1352         return (void*)g_private_display;
1353 }
1354
1355 EXTERN tdm_display *
1356 tdm_display_init(tdm_error *error)
1357 {
1358         tdm_private_display *private_display = NULL;
1359         const char *str;
1360         tdm_error ret;
1361         double stamp1, stamp2, start;
1362         int mode;
1363
1364         pthread_mutex_lock(&gLock);
1365
1366         if (g_private_display) {
1367                 g_private_display->init_count++;
1368                 pthread_mutex_unlock(&gLock);
1369                 if (error)
1370                         *error = TDM_ERROR_NONE;
1371                 return g_private_display;
1372         }
1373
1374         start = stamp1 = tdm_helper_get_time();
1375
1376         private_display = calloc(1, sizeof(tdm_private_display));
1377         if (!private_display) {
1378                 /* LCOV_EXCL_START */
1379                 ret = TDM_ERROR_OUT_OF_MEMORY;
1380                 TDM_ERR("'private_display != NULL' failed");
1381                 goto failed_alloc;
1382                 /* LCOV_EXCL_STOP */
1383         }
1384
1385         private_display->stamp = tdm_helper_get_time();
1386
1387         LIST_INITHEAD(&private_display->output_create_handler_list);
1388
1389         str = tdm_config_get_string(TDM_CONFIG_KEY_DEBUG_MODULE, NULL);
1390         if (str)
1391                 tdm_display_enable_debug_module(str);
1392
1393         str = tdm_config_get_string(TDM_CONFIG_KEY_DEBUG_DUMP, NULL);
1394         if (str)
1395                 tdm_display_enable_dump(private_display, str, NULL, NULL);
1396
1397         if (pthread_mutex_init(&private_display->lock, NULL)) {
1398                 /* LCOV_EXCL_START */
1399                 ret = TDM_ERROR_OPERATION_FAILED;
1400                 TDM_ERR("mutex init failed: %m");
1401                 goto failed_mutex_init;
1402                 /* LCOV_EXCL_STOP */
1403         }
1404
1405         _pthread_mutex_lock(&private_display->lock);
1406
1407         stamp2 = tdm_helper_get_time();
1408         TDM_DBG("prepare init time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1409         stamp1 = stamp2;
1410
1411         ret = tdm_event_loop_init(private_display);
1412         if (ret != TDM_ERROR_NONE)
1413                 goto failed_event;
1414
1415         stamp2 = tdm_helper_get_time();
1416         TDM_INIT_INFO("event loop init time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1417         stamp1 = stamp2;
1418
1419         ret = tdm_vblank_init(private_display);
1420         if (ret != TDM_ERROR_NONE)
1421                 goto failed_vblank;
1422
1423         ret = tdm_output_init(private_display);
1424         if (ret != TDM_ERROR_NONE)
1425                 goto failed_load;
1426
1427         ret = tdm_hwc_init(private_display);
1428         if (ret != TDM_ERROR_NONE)
1429                 goto failed_load;
1430
1431         ret = tdm_pp_init(private_display);
1432         if (ret != TDM_ERROR_NONE)
1433                 goto failed_load;
1434
1435         ret = tdm_capture_init(private_display);
1436         if (ret != TDM_ERROR_NONE)
1437                 goto failed_load;
1438
1439         ret = _tdm_display_load_modules(private_display);
1440         if (!(ret == TDM_ERROR_NONE || ret == TDM_ERROR_NO_MODULE))
1441                 goto failed_load;
1442
1443         stamp2 = tdm_helper_get_time();
1444         TDM_INIT_INFO("loading backend time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1445         stamp1 = stamp2;
1446
1447 #ifdef INIT_BUFMGR
1448 #if 0
1449         int tdm_drm_fd = tdm_helper_get_fd("TDM_DRM_MASTER_FD");
1450         if (tdm_drm_fd >= 0) {
1451                 private_display->bufmgr = tbm_bufmgr_init(tdm_drm_fd);
1452                 close(tdm_drm_fd);
1453                 if (!private_display->bufmgr) {
1454                         TDM_ERR("tbm_bufmgr_init failed");
1455                         goto failed_update;
1456                 } else {
1457                         TDM_INFO("tbm_bufmgr_init successed");
1458                 }
1459
1460                 stamp2 = tdm_helper_get_time();
1461                 TDM_DBG("initializing bufmgr time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1462                 stamp1 = stamp2;
1463         }
1464 #else
1465         private_display->bufmgr = tbm_bufmgr_init(-1);
1466         if (!private_display->bufmgr) {
1467                 TDM_ERR("tbm_bufmgr_init failed");
1468                 goto failed_update;
1469         } else {
1470                 TDM_INFO("tbm_bufmgr_init successed");
1471         }
1472
1473         stamp2 = tdm_helper_get_time();
1474         TDM_DBG("initializing bufmgr time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1475         stamp1 = stamp2;
1476 #endif
1477 #endif
1478
1479         TDM_TRACE_BEGIN("TDM_Update_Display");
1480         ret = _tdm_display_setup(private_display);
1481         TDM_INIT_INFO("updating display");
1482         TDM_TRACE_END();
1483         if (ret != TDM_ERROR_NONE)
1484                 goto failed_update;
1485
1486         stamp2 = tdm_helper_get_time();
1487         TDM_DBG("updating display time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1488         stamp1 = stamp2;
1489
1490         tdm_event_loop_create_backend_source(private_display);
1491
1492         private_display->init_count = 1;
1493         g_private_display = private_display;
1494
1495         /* the COMMIT_PER_VBLANK functionality is ability of an output to support
1496          * several operational modes (commit_per_vblank modes) related to tdm_commit;
1497          * this functionality can be turned off which means a default mode */
1498         mode = tdm_config_get_int(TDM_CONFIG_KEY_GENERAL_COMMIT_PER_VBLANK, 0);
1499         if (mode > 0) {
1500                 tdm_private_module *b = NULL;
1501                 tdm_private_output *o = NULL;
1502
1503                 /* outputs which support hwc capability can work only
1504                  * if commit_per_vblank mode is '0' (default mode) */
1505                 LIST_FOR_EACH_ENTRY(b, &private_display->module_list, link) {
1506                         LIST_FOR_EACH_ENTRY(o, &b->output_list, link) {
1507                                 if (!(o->caps.capabilities & TDM_OUTPUT_CAPABILITY_HWC))
1508                                         tdm_output_choose_commit_per_vblank_mode(o, mode);
1509                         }
1510                 }
1511         }
1512
1513         if (error)
1514                 *error = TDM_ERROR_NONE;
1515
1516         tdm_thread_cb_set_find_func(TDM_THREAD_CB_DISPLAY_OUTPUT_CREATE, tdm_display_find_stamp);
1517
1518         _pthread_mutex_unlock(&private_display->lock);
1519         pthread_mutex_unlock(&gLock);
1520
1521         TDM_INIT_INFO("init time: %.3f ms", (tdm_helper_get_time() - start) * 1000.0);
1522
1523         return (tdm_display *)private_display;
1524
1525 /* LCOV_EXCL_START */
1526 failed_update:
1527 #ifdef INIT_BUFMGR
1528         if (private_display->bufmgr)
1529                 tbm_bufmgr_deinit(private_display->bufmgr);
1530 #endif
1531         _tdm_display_unload_modules(private_display);
1532 failed_load:
1533         tdm_vblank_deinit(private_display);
1534 failed_vblank:
1535         tdm_event_loop_stop(private_display);
1536         tdm_event_loop_deinit(private_display);
1537 failed_event:
1538         _pthread_mutex_unlock(&private_display->lock);
1539         pthread_mutex_destroy(&private_display->lock);
1540 failed_mutex_init:
1541         free(private_display);
1542 failed_alloc:
1543         if (error)
1544                 *error = ret;
1545         pthread_mutex_unlock(&gLock);
1546         return NULL;
1547 /* LCOV_EXCL_STOP */
1548 }
1549
1550 EXTERN void
1551 tdm_display_deinit(tdm_display *dpy)
1552 {
1553         tdm_private_display *private_display = dpy;
1554
1555         if (!private_display)
1556                 return;
1557
1558         pthread_mutex_lock(&gLock);
1559
1560         private_display->init_count--;
1561         if (private_display->init_count > 0) {
1562                 pthread_mutex_unlock(&gLock);
1563                 return;
1564         }
1565
1566         /* dont move the position of lock/unlock. all resource should be protected
1567          * during destroying. after tdm_event_loop_deinit, we don't worry about thread
1568          * things because it's finalized.
1569          */
1570         _pthread_mutex_lock(&private_display->lock);
1571         tdm_event_loop_stop(private_display);
1572         _pthread_mutex_unlock(&private_display->lock);
1573
1574         tdm_event_loop_deinit(private_display);
1575
1576         /* when private_output is destroyed, all vblank resources of client and server
1577          * are destroyed. Then we can call tdm_vblank_deinit. After destroying display,
1578          * we can unload backend modulues.
1579          */
1580         _tdm_display_destroy_private_display(private_display);
1581         _tdm_display_unload_modules(private_display);
1582
1583         tdm_vblank_deinit(private_display);
1584
1585 #ifdef INIT_BUFMGR
1586         if (private_display->bufmgr)
1587                 tbm_bufmgr_deinit(private_display->bufmgr);
1588 #endif
1589
1590         tbm_drm_helper_unset_tbm_master_fd();
1591
1592         pthread_mutex_destroy(&private_display->lock);
1593         free(private_display);
1594         g_private_display = NULL;
1595
1596         if (tdm_debug_dump_dir) {
1597                 free(tdm_debug_dump_dir);
1598                 tdm_debug_dump_dir = NULL;
1599         }
1600
1601         tdm_config_deinit();
1602
1603         pthread_mutex_unlock(&gLock);
1604
1605         TDM_INFO("done");
1606 }
1607
1608 INTERN tdm_private_display *
1609 tdm_display_get(void)
1610 {
1611         return g_private_display;
1612 }
1613