1 /**************************************************************************
5 * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
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>
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:
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
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.
34 **************************************************************************/
40 #include "tdm_private.h"
42 pthread_mutex_t tdm_mutex_check_lock = PTHREAD_MUTEX_INITIALIZER;
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;
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;
53 static tdm_error _tdm_display_load_module_with_file(tdm_private_display *private_display, const char *file);
56 static tdm_private_layer *
57 _tdm_display_find_private_layer(tdm_private_output *private_output,
58 tdm_layer *layer_backend)
60 tdm_private_layer *private_layer = NULL;
62 LIST_FOR_EACH_ENTRY(private_layer, &private_output->layer_list, link) {
63 if (private_layer->layer_backend == layer_backend)
70 INTERN tdm_private_output *
71 tdm_display_find_private_output(tdm_private_display *private_display, tdm_output *output_backend)
73 tdm_private_module *private_module = NULL;
74 tdm_private_output *private_output = NULL;
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;
87 tdm_display_find_empty_output_pipe(tdm_private_display *private_display)
89 tdm_private_module *private_module = NULL;
90 tdm_private_output *private_output = NULL;
91 unsigned int pipe = 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) {
115 tdm_display_find_output_stamp(tdm_private_display *private_display, double stamp)
117 tdm_private_module *private_module = NULL;
118 tdm_private_output *private_output = NULL;
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;
131 tdm_display_find_private_voutput(tdm_private_display *private_display, double stamp)
133 tdm_private_module *private_module = NULL;
134 tdm_private_voutput *private_voutput = NULL;
135 tdm_private_output *private_output = NULL;
137 private_module = private_display->virtual_module;
138 if (!private_module) return NULL;
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;
151 tdm_display_find_private_hwc(tdm_private_display *private_display, tdm_hwc *hwc_backend)
153 tdm_private_module *private_module = NULL;
154 tdm_private_output *private_output = NULL;
155 tdm_private_hwc *private_hwc = NULL;
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)
170 tdm_display_find_hwc_stamp(tdm_private_display *private_display, double stamp)
172 tdm_private_module *private_module = NULL;
173 tdm_private_output *private_output = NULL;
174 tdm_private_hwc *private_hwc = NULL;
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)
189 _tdm_display_destroy_caps_pp(tdm_caps_pp *caps_pp)
191 if (caps_pp->formats)
192 free(caps_pp->formats);
194 memset(caps_pp, 0, sizeof(tdm_caps_pp));
198 _tdm_display_destroy_caps_capture(tdm_caps_capture *caps_capture)
200 if (caps_capture->formats)
201 free(caps_capture->formats);
203 memset(caps_capture, 0, sizeof(tdm_caps_capture));
207 _tdm_display_destroy_caps_layer(tdm_caps_layer *caps_layer)
209 if (caps_layer->formats)
210 free(caps_layer->formats);
212 if (caps_layer->props)
213 free(caps_layer->props);
215 memset(caps_layer, 0, sizeof(tdm_caps_layer));
219 _tdm_display_destroy_caps_output(tdm_caps_output *caps_output)
221 if (caps_output->modes)
222 free(caps_output->modes);
224 if (caps_output->props)
225 free(caps_output->props);
227 memset(caps_output, 0, sizeof(tdm_caps_output));
231 _tdm_display_destroy_private_layer(tdm_private_layer *private_layer)
233 tdm_private_capture *c = NULL, *cc = NULL;
235 tdm_layer_unset_buffer_internal(private_layer);
237 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &private_layer->capture_list, link)
238 tdm_capture_destroy_internal(c);
240 _tdm_display_destroy_caps_layer(&private_layer->caps);
242 /* when destroying layer, someone could check if layer is valid. So delete
243 * the layer's link at last.
245 LIST_DEL(&private_layer->link);
251 tdm_display_destroy_private_output(tdm_private_output *private_output)
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;
265 int mutex_locked = 0;
267 ret = tdm_output_call_thread_cb_destroy(private_output);
268 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
270 free(private_output->layers_ptr);
272 if (private_output->vblank_timeout_timer)
273 tdm_event_loop_source_remove(private_output->vblank_timeout_timer);
275 LIST_FOR_EACH_ENTRY_SAFE(v, vv, &private_output->vblank_handler_list, link) {
280 LIST_FOR_EACH_ENTRY_SAFE(om, omm, &private_output->output_commit_handler_list, link) {
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) {
292 LIST_FOR_EACH_ENTRY_SAFE(lm, lmm, &private_output->layer_commit_handler_list, link) {
298 LIST_FOR_EACH_ENTRY_SAFE(lm, lmm, &private_output->pending_commit_handler_list, link) {
303 LIST_FOR_EACH_ENTRY_SAFE(h, hh, &private_output->change_handler_list, 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);
310 LIST_FOR_EACH_ENTRY_SAFE(dh, dhh, &private_output->destroy_handler_list, link) {
312 tdm_thread_cb_remove(dh->private_output, TDM_THREAD_CB_OUTPUT_DESTROY, NULL, tdm_output_thread_cb_destroy, dh);
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);
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);
331 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &private_output->capture_list, link)
332 tdm_capture_destroy_internal(c);
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);
342 LIST_FOR_EACH_ENTRY_SAFE(l, ll, &private_output->layer_list, link)
343 _tdm_display_destroy_private_layer(l);
346 _tdm_display_destroy_caps_output(&private_output->caps);
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.
351 LIST_DEL(&private_output->link);
353 private_output->stamp = 0;
354 free(private_output);
358 _tdm_display_destroy_private_display(tdm_private_display *private_display)
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;
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);
370 LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_module->output_list, link) {
371 tdm_display_destroy_private_output(o);
374 _tdm_display_destroy_caps_pp(&private_module->caps_pp);
375 _tdm_display_destroy_caps_capture(&private_module->caps_capture);
377 private_module->capabilities = 0;
378 private_module->caps_display.max_layer_count = 0;
380 if (private_module->outputs) {
381 free(private_module->outputs);
382 private_module->outputs = NULL;
386 LIST_FOR_EACH_ENTRY_SAFE(ch, chh, &private_display->output_create_handler_list, link) {
388 tdm_thread_cb_remove(ch->private_display, TDM_THREAD_CB_DISPLAY_OUTPUT_CREATE, NULL,
389 tdm_display_thread_cb_output_create, ch);
395 _tdm_display_update_caps_pp(tdm_private_module *private_module, tdm_caps_pp *caps)
397 tdm_func_display *func_display;
400 func_display = &private_module->func_display;
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;
407 memset(caps, 0, sizeof(tdm_caps_pp));
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;
415 return TDM_ERROR_NONE;
419 _tdm_display_update_caps_capture(tdm_private_module *private_module, tdm_caps_capture *caps)
421 tdm_func_display *func_display;
424 func_display = &private_module->func_display;
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;
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;
437 return TDM_ERROR_NONE;
441 _tdm_display_update_caps_layer(tdm_private_module *private_module,
442 tdm_layer *layer_backend, tdm_caps_layer *caps)
444 tdm_func_layer *func_layer = &private_module->func_layer;
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;
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;
458 return TDM_ERROR_NONE;
462 _tdm_display_update_caps_output(tdm_private_module *private_module, int pipe,
463 tdm_output *output_backend, tdm_caps_output *caps)
465 tdm_func_output *func_output = &private_module->func_output;
466 char temp[TDM_NAME_LEN];
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;
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;
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';
497 snprintf(caps->model, TDM_NAME_LEN, "%s", temp);
499 return TDM_ERROR_NONE;
503 _tdm_display_update_layer(tdm_private_output *private_output,
504 tdm_layer *layer_backend, int index)
506 tdm_private_layer *private_layer;
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);
514 LIST_ADDTAIL(&private_layer->link, &private_output->layer_list);
515 LIST_INITHEAD(&private_layer->capture_list);
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;
525 _tdm_display_destroy_caps_layer(&private_layer->caps);
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);
530 return TDM_ERROR_NONE;
534 tdm_display_update_output(tdm_private_module *private_module, tdm_output *output_backend)
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;
542 int layer_count = 0, i;
544 unsigned int output_created = 0;
546 private_display = private_module->private_display;
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);
552 private_output = calloc(1, sizeof(tdm_private_output));
553 TDM_RETURN_VAL_IF_FAIL(private_output != NULL, TDM_ERROR_OUT_OF_MEMORY);
555 private_output->stamp = tdm_helper_get_time();
556 while (tdm_display_find_output_stamp(private_display, private_output->stamp))
557 private_output->stamp++;
559 LIST_ADDTAIL(&private_output->link, &private_module->output_list);
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;
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);
576 if (private_module->use_hal_tdm) {
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;
582 if (func_output->output_set_status_handler) {
583 func_output->output_set_status_handler(private_output->output_backend,
584 tdm_output_cb_status,
586 private_output->regist_change_cb = 1;
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);
597 if (private_output->caps.status == TDM_OUTPUT_CONN_STATUS_DISCONNECTED)
598 private_output->current_mode = NULL;
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);
605 hwc = func_output->output_get_hwc(output_backend, &ret);
606 TDM_RETURN_VAL_IF_FAIL(hwc != NULL, ret);
608 private_hwc = calloc(1, sizeof(tdm_private_hwc));
609 TDM_RETURN_VAL_IF_FAIL(private_hwc != NULL, TDM_ERROR_OUT_OF_MEMORY);
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++;
619 LIST_INITHEAD(&private_hwc->hwc_window_list);
620 LIST_INITHEAD(&private_hwc->hwc_commit_handler_list);
622 private_output->private_hwc = private_hwc;
624 LIST_INITHEAD(&private_output->layer_list);
625 LIST_INITHEAD(&private_output->layer_commit_handler_list);
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);
632 if (private_output->caps.status == TDM_OUTPUT_CONN_STATUS_DISCONNECTED)
633 private_output->current_mode = NULL;
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;
642 layers = func_output->output_get_layers(output_backend, &layer_count, &ret);
643 if (ret != TDM_ERROR_NONE) {
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) {
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);
664 return TDM_ERROR_NONE;
668 _tdm_display_set_main_first(tdm_output **outputs, int index)
670 tdm_output *output_tmp = NULL;
675 output_tmp = outputs[0];
676 outputs[0] = outputs[index];
677 outputs[index] = output_tmp;
681 _tdm_display_get_ordered_outputs(tdm_private_module *private_module, int *count)
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;
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;
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);
702 outputs = func_display->display_get_outputs(private_module->bdata, &output_count, &ret);
703 if (ret != TDM_ERROR_NONE)
706 *count = output_count;
708 if (output_count == 0)
710 else if (output_count == 1) {
711 private_module->outputs = outputs;
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));
721 if (private_module->use_hal_tdm)
722 ret = (tdm_error)hal_tdm_output_get_capability(outputs[i], (hal_tdm_caps_output *)&caps);
724 ret = func_output->output_get_capability(outputs[i], &caps);
725 if (ret != TDM_ERROR_NONE) {
726 TDM_ERR("output_get_capability() failed");
730 if (caps.status == TDM_OUTPUT_CONN_STATUS_CONNECTED) {
731 output_connected_count++;
734 case TDM_OUTPUT_TYPE_DSI:
735 if (output_dsi != NULL) continue;
736 output_dsi = outputs[i];
739 case TDM_OUTPUT_TYPE_LVDS:
740 if (output_lvds != NULL) continue;
741 output_lvds = outputs[i];
744 case TDM_OUTPUT_TYPE_HDMIA:
745 if (output_hdmia != NULL) continue;
746 output_hdmia = outputs[i];
749 case TDM_OUTPUT_TYPE_HDMIB:
750 if (output_hdmib != NULL) continue;
751 output_hdmib = outputs[i];
759 _tdm_display_destroy_caps_output(&caps);
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)
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);
791 private_module->outputs = outputs;
802 _tdm_display_setup(tdm_private_display *private_display)
804 tdm_private_module *private_module = NULL;
805 tdm_error ret = TDM_ERROR_NONE;
806 int output_count = 0;
808 int virtual_hwc_target_buffer_bo_default = 0;
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)
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)
824 LIST_FOR_EACH_ENTRY(private_module, &private_display->module_list, link) {
825 tdm_output **outputs;
828 outputs = _tdm_display_get_ordered_outputs(private_module, &count);
831 TDM_GOTO_IF_FAIL(outputs != NULL, failed_update);
833 for (i = 0; i < count; i++) {
834 ret = tdm_display_update_output(private_module, outputs[i]);
835 if (ret != TDM_ERROR_NONE)
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.
846 if (output_count == 0) {
847 tdm_output **outputs;
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);
855 private_module = private_display->dummy_module;
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);
861 for (i = 0; i < count; i++) {
862 ret = tdm_display_update_output(private_module, outputs[i]);
863 if (ret != TDM_ERROR_NONE)
868 virtual = tdm_config_get_int(TDM_CONFIG_KEY_GENERAL_VIRTUAL_OUTPUT, 0);
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);
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;
880 return TDM_ERROR_NONE;
883 _tdm_display_destroy_private_display(private_display);
889 tdm_display_update(tdm_display *dpy)
891 TDM_RETURN_VAL_IF_FAIL(dpy != NULL, TDM_ERROR_INVALID_PARAMETER);
893 TDM_DEPRECATED(NULL);
895 return TDM_ERROR_NONE;
898 int tdm_debug_module;
900 int tdm_ttrace_module;
901 int tdm_ttrace_output;
903 static tdm_private_display *g_private_display;
904 static pthread_mutex_t gLock = PTHREAD_MUTEX_INITIALIZER;
906 /* LCOV_EXCL_START */
908 _tdm_display_load_hal_backend(tdm_private_display *private_display)
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;
919 hal_tdm_fd master_drm_fd = -1;
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;
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;
936 module_data = calloc(1, sizeof *module_data);
937 if (module_data == NULL) {
938 TDM_ERR("failed: alloc");
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);
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");
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.");
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");
961 TDM_INFO("libtbm gets a master drm_fd from libtbm via tbm_drm_helper.");
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");
970 TDM_INFO("tdm_backend has a master drm_fd.");
972 tbm_drm_helper_set_tbm_master_fd(master_drm_fd);
976 if (hal_tdm_display_get_capability(hal_tdm_dpy, &caps) != HAL_TDM_ERROR_NONE) {
977 TDM_ERR("failed hal_tdm_display_get_capability");
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;
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;
1001 private_module->capabilities |= TDM_DISPLAY_CAPABILITY_PP;
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;
1015 private_module->capabilities |= TDM_DISPLAY_CAPABILITY_CAPTURE;
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);
1023 LIST_ADDTAIL(&private_module->link, &private_display->module_list);
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);
1035 TDM_INIT_INFO("use HAL-TDM_API");
1037 return TDM_ERROR_NONE;
1040 if (master_drm_fd >= 0)
1041 close(master_drm_fd);
1042 hal_tdm_put_backend(tdm_backend);
1044 if (module_data->name)
1045 free(module_data->name);
1046 if (module_data->vendor)
1047 free(module_data->vendor);
1050 free(private_module);
1052 return TDM_ERROR_NO_MODULE;
1056 _tdm_display_check_module(tdm_backend_module *module)
1058 int tdm_backend_major, tdm_backend_minor;
1059 int backend_module_major, backend_module_minor;
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);
1064 TDM_INFO("TDM Backend ABI version : %d.%d",
1065 tdm_backend_major, tdm_backend_minor);
1067 if (!module->name) {
1068 TDM_ERR("TDM backend doesn't have name");
1069 return TDM_ERROR_BAD_MODULE;
1072 if (!module->vendor) {
1073 TDM_ERR("TDM backend doesn't have vendor");
1074 return TDM_ERROR_BAD_MODULE;
1077 backend_module_major = TDM_BACKEND_GET_ABI_MAJOR(module->abi_version);
1078 backend_module_minor = TDM_BACKEND_GET_ABI_MINOR(module->abi_version);
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);
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;
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;
1097 if (!module->init) {
1098 TDM_ERR("'%s' doesn't have init function", module->name);
1099 return TDM_ERROR_BAD_MODULE;
1102 if (!module->deinit) {
1103 TDM_ERR("'%s' doesn't have deinit function", module->name);
1104 return TDM_ERROR_BAD_MODULE;
1107 return TDM_ERROR_NONE;
1111 _tdm_display_check_backend_functions(tdm_private_module *private_module)
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;
1118 /* below functions should be implemented in backend side */
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);
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;
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);
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);
1163 return TDM_ERROR_NONE;
1167 _tdm_display_load_module_with_file(tdm_private_display *private_display,
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;
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;
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;
1192 private_module->private_display = private_display;
1194 stamp = tdm_helper_get_time();
1196 TDM_TRACE_BEGIN("TDM_Load_Backend");
1197 module = dlopen(path, RTLD_LAZY);
1199 TDM_ERR("%s", dlerror());
1201 ret = TDM_ERROR_NO_MODULE;
1205 private_module->module = module;
1207 module_data = dlsym(module, "tdm_backend_module_data");
1209 TDM_ERR("'%s' doesn't have data object", file);
1210 ret = TDM_ERROR_BAD_MODULE;
1216 private_module->module_data = module_data;
1218 TDM_DBG("dlopen, dlsym time: %.3f ms", (tdm_helper_get_time() - stamp) * 1000.0);
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);
1227 private_display->current_module = private_module;
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);
1238 private_display->current_module = NULL;
1240 if (!strncmp(file, TDM_DUMMY_MODULE, TDM_NAME_LEN))
1241 private_display->dummy_module = private_module;
1243 if (!strncmp(file, TDM_VIRTUAL_MODULE, TDM_NAME_LEN))
1244 private_display->virtual_module = private_module;
1246 private_module->bdata = bdata;
1248 if (ret != TDM_ERROR_NONE) {
1249 TDM_ERR("backend(%s) load failed: init error", module_data->name);
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);
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);
1264 LIST_ADDTAIL(&private_module->link, &private_display->module_list);
1266 TDM_INIT_INFO("Success to load '%s' module", module_data->name);
1268 return TDM_ERROR_NONE;
1271 module_data->deinit(bdata);
1275 free(private_module);
1280 _tdm_display_load_modules(tdm_private_display *private_display)
1282 const char *module_names;
1283 tdm_error ret = TDM_ERROR_NONE;
1284 char temp[TDM_PATH_LEN];
1288 LIST_INITHEAD(&private_display->module_list);
1290 if (_tdm_display_load_hal_backend(private_display) == TDM_ERROR_NONE) {
1291 TDM_INIT_INFO("use hal tdm");
1295 module_names = tdm_config_get_string(TDM_CONFIG_KEY_GENERAL_BACKENDS, TDM_DEFAULT_MODULE);
1297 snprintf(temp, TDM_PATH_LEN, "%s", module_names);
1299 arg = strtok_r(temp, TDM_CONFIG_DELIM, &end);
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);
1306 TDM_INIT_INFO("%s backend loading failed", arg);
1307 arg = strtok_r(NULL, TDM_CONFIG_DELIM, &end);
1314 _tdm_display_unload_modules(tdm_private_display *private_display)
1316 tdm_private_module *private_module = NULL, *bb = NULL;
1317 tdm_backend_module *module_data = NULL;
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);
1329 module_data = private_module->module_data;
1331 free(module_data->name);
1332 free(module_data->vendor);
1335 hal_tdm_put_backend(private_module->htdm_backend);
1336 free(private_module);
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);
1343 free(private_module);
1347 /* LCOV_EXCL_STOP */
1350 tdm_display_find_stamp(tdm_private_display *private_display, double stamp)
1352 return (void*)g_private_display;
1355 EXTERN tdm_display *
1356 tdm_display_init(tdm_error *error)
1358 tdm_private_display *private_display = NULL;
1361 double stamp1, stamp2, start;
1364 pthread_mutex_lock(&gLock);
1366 if (g_private_display) {
1367 g_private_display->init_count++;
1368 pthread_mutex_unlock(&gLock);
1370 *error = TDM_ERROR_NONE;
1371 return g_private_display;
1374 start = stamp1 = tdm_helper_get_time();
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");
1382 /* LCOV_EXCL_STOP */
1385 private_display->stamp = tdm_helper_get_time();
1387 LIST_INITHEAD(&private_display->output_create_handler_list);
1389 str = tdm_config_get_string(TDM_CONFIG_KEY_DEBUG_MODULE, NULL);
1391 tdm_display_enable_debug_module(str);
1393 str = tdm_config_get_string(TDM_CONFIG_KEY_DEBUG_DUMP, NULL);
1395 tdm_display_enable_dump(private_display, str, NULL, NULL);
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 */
1405 _pthread_mutex_lock(&private_display->lock);
1407 stamp2 = tdm_helper_get_time();
1408 TDM_DBG("prepare init time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1411 ret = tdm_event_loop_init(private_display);
1412 if (ret != TDM_ERROR_NONE)
1415 stamp2 = tdm_helper_get_time();
1416 TDM_INIT_INFO("event loop init time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1419 ret = tdm_vblank_init(private_display);
1420 if (ret != TDM_ERROR_NONE)
1423 ret = tdm_output_init(private_display);
1424 if (ret != TDM_ERROR_NONE)
1427 ret = tdm_hwc_init(private_display);
1428 if (ret != TDM_ERROR_NONE)
1431 ret = tdm_pp_init(private_display);
1432 if (ret != TDM_ERROR_NONE)
1435 ret = tdm_capture_init(private_display);
1436 if (ret != TDM_ERROR_NONE)
1439 ret = _tdm_display_load_modules(private_display);
1440 if (!(ret == TDM_ERROR_NONE || ret == TDM_ERROR_NO_MODULE))
1443 stamp2 = tdm_helper_get_time();
1444 TDM_INIT_INFO("loading backend time: %.3f ms", (stamp2 - stamp1) * 1000.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);
1453 if (!private_display->bufmgr) {
1454 TDM_ERR("tbm_bufmgr_init failed");
1457 TDM_INFO("tbm_bufmgr_init successed");
1460 stamp2 = tdm_helper_get_time();
1461 TDM_DBG("initializing bufmgr time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1465 private_display->bufmgr = tbm_bufmgr_init(-1);
1466 if (!private_display->bufmgr) {
1467 TDM_ERR("tbm_bufmgr_init failed");
1470 TDM_INFO("tbm_bufmgr_init successed");
1473 stamp2 = tdm_helper_get_time();
1474 TDM_DBG("initializing bufmgr time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1479 TDM_TRACE_BEGIN("TDM_Update_Display");
1480 ret = _tdm_display_setup(private_display);
1481 TDM_INIT_INFO("updating display");
1483 if (ret != TDM_ERROR_NONE)
1486 stamp2 = tdm_helper_get_time();
1487 TDM_DBG("updating display time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1490 tdm_event_loop_create_backend_source(private_display);
1492 private_display->init_count = 1;
1493 g_private_display = private_display;
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);
1500 tdm_private_module *b = NULL;
1501 tdm_private_output *o = NULL;
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);
1514 *error = TDM_ERROR_NONE;
1516 tdm_thread_cb_set_find_func(TDM_THREAD_CB_DISPLAY_OUTPUT_CREATE, tdm_display_find_stamp);
1518 _pthread_mutex_unlock(&private_display->lock);
1519 pthread_mutex_unlock(&gLock);
1521 TDM_INIT_INFO("init time: %.3f ms", (tdm_helper_get_time() - start) * 1000.0);
1523 return (tdm_display *)private_display;
1525 /* LCOV_EXCL_START */
1528 if (private_display->bufmgr)
1529 tbm_bufmgr_deinit(private_display->bufmgr);
1531 _tdm_display_unload_modules(private_display);
1533 tdm_vblank_deinit(private_display);
1535 tdm_event_loop_stop(private_display);
1536 tdm_event_loop_deinit(private_display);
1538 _pthread_mutex_unlock(&private_display->lock);
1539 pthread_mutex_destroy(&private_display->lock);
1541 free(private_display);
1545 pthread_mutex_unlock(&gLock);
1547 /* LCOV_EXCL_STOP */
1551 tdm_display_deinit(tdm_display *dpy)
1553 tdm_private_display *private_display = dpy;
1555 if (!private_display)
1558 pthread_mutex_lock(&gLock);
1560 private_display->init_count--;
1561 if (private_display->init_count > 0) {
1562 pthread_mutex_unlock(&gLock);
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.
1570 _pthread_mutex_lock(&private_display->lock);
1571 tdm_event_loop_stop(private_display);
1572 _pthread_mutex_unlock(&private_display->lock);
1574 tdm_event_loop_deinit(private_display);
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.
1580 _tdm_display_destroy_private_display(private_display);
1581 _tdm_display_unload_modules(private_display);
1583 tdm_vblank_deinit(private_display);
1586 if (private_display->bufmgr)
1587 tbm_bufmgr_deinit(private_display->bufmgr);
1590 tbm_drm_helper_unset_tbm_master_fd();
1592 pthread_mutex_destroy(&private_display->lock);
1593 free(private_display);
1594 g_private_display = NULL;
1596 if (tdm_debug_dump_dir) {
1597 free(tdm_debug_dump_dir);
1598 tdm_debug_dump_dir = NULL;
1601 tdm_config_deinit();
1603 pthread_mutex_unlock(&gLock);
1608 INTERN tdm_private_display *
1609 tdm_display_get(void)
1611 return g_private_display;