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;
266 ret = tdm_output_call_thread_cb_destroy(private_output);
267 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
269 free(private_output->layers_ptr);
271 if (private_output->vblank_timeout_timer)
272 tdm_event_loop_source_remove(private_output->vblank_timeout_timer);
274 LIST_FOR_EACH_ENTRY_SAFE(v, vv, &private_output->vblank_handler_list, link) {
279 LIST_FOR_EACH_ENTRY_SAFE(om, omm, &private_output->output_commit_handler_list, link) {
284 if (private_output->private_hwc) {
285 private_hwc = private_output->private_hwc;
286 LIST_FOR_EACH_ENTRY_SAFE(hm, hmm, &private_hwc->hwc_commit_handler_list, link) {
291 LIST_FOR_EACH_ENTRY_SAFE(lm, lmm, &private_output->layer_commit_handler_list, link) {
297 LIST_FOR_EACH_ENTRY_SAFE(lm, lmm, &private_output->pending_commit_handler_list, link) {
302 LIST_FOR_EACH_ENTRY_SAFE(h, hh, &private_output->change_handler_list, link) {
304 tdm_thread_cb_remove(h->private_output, TDM_THREAD_CB_OUTPUT_DPMS, NULL, tdm_output_thread_cb_change, h);
305 tdm_thread_cb_remove(h->private_output, TDM_THREAD_CB_OUTPUT_STATUS, NULL, tdm_output_thread_cb_change, h);
309 LIST_FOR_EACH_ENTRY_SAFE(dh, dhh, &private_output->destroy_handler_list, link) {
311 tdm_thread_cb_remove(dh->private_output, TDM_THREAD_CB_OUTPUT_DESTROY, NULL, tdm_output_thread_cb_destroy, dh);
315 if (private_output->vblank) {
316 /* tdm_vblank APIs is for server. it should be called in unlock status*/
317 _pthread_mutex_unlock(&private_display->lock);
318 tdm_vblank_destroy(private_output->vblank);
319 _pthread_mutex_lock(&private_display->lock);
322 if (private_output->ttrace_vblank) {
323 /* tdm_vblank APIs is for server. it should be called in unlock status*/
324 _pthread_mutex_unlock(&private_display->lock);
325 tdm_vblank_destroy(private_output->ttrace_vblank);
326 _pthread_mutex_lock(&private_display->lock);
329 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &private_output->capture_list, link)
330 tdm_capture_destroy_internal(c);
332 if (private_output->private_hwc) {
333 private_hwc = private_output->private_hwc;
334 LIST_FOR_EACH_ENTRY_SAFE(hw, hww, &private_hwc->hwc_window_list, link)
335 tdm_hwc_window_destroy_internal(hw);
338 LIST_FOR_EACH_ENTRY_SAFE(l, ll, &private_output->layer_list, link)
339 _tdm_display_destroy_private_layer(l);
342 _tdm_display_destroy_caps_output(&private_output->caps);
344 /* when destroying output, vblank objects are also destroyed. vblank checks
345 * if output object is valid. So delete the output's link at last.
347 LIST_DEL(&private_output->link);
349 private_output->stamp = 0;
350 free(private_output);
354 _tdm_display_destroy_private_display(tdm_private_display *private_display)
356 tdm_private_module *private_module = NULL, *bb = NULL;
357 tdm_private_output *o = NULL, *oo = NULL;
358 tdm_private_pp *p = NULL, *pp = NULL;
359 tdm_private_output_create_handler *ch = NULL, *chh = NULL;
361 LIST_FOR_EACH_ENTRY_SAFE(private_module, bb, &private_display->module_list, link) {
362 LIST_FOR_EACH_ENTRY_SAFE(p, pp, &private_module->pp_list, link) {
363 tdm_pp_destroy_internal(p);
366 LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_module->output_list, link) {
367 tdm_display_destroy_private_output(o);
370 _tdm_display_destroy_caps_pp(&private_module->caps_pp);
371 _tdm_display_destroy_caps_capture(&private_module->caps_capture);
373 private_module->capabilities = 0;
374 private_module->caps_display.max_layer_count = 0;
376 if (private_module->outputs) {
377 free(private_module->outputs);
378 private_module->outputs = NULL;
382 LIST_FOR_EACH_ENTRY_SAFE(ch, chh, &private_display->output_create_handler_list, link) {
384 tdm_thread_cb_remove(ch->private_display, TDM_THREAD_CB_DISPLAY_OUTPUT_CREATE, NULL,
385 tdm_display_thread_cb_output_create, ch);
391 _tdm_display_update_caps_pp(tdm_private_module *private_module, tdm_caps_pp *caps)
393 tdm_func_display *func_display;
396 func_display = &private_module->func_display;
398 if (!func_display->display_get_pp_capability) {
399 TDM_ERR("backend(%s) no display_get_pp_capability()", private_module->module_data->name);
400 return TDM_ERROR_BAD_MODULE;
403 ret = func_display->display_get_pp_capability(private_module->bdata, caps);
404 if (ret != TDM_ERROR_NONE) {
405 TDM_ERR("backend(%s) display_get_pp_capability() failed", private_module->module_data->name);
406 return TDM_ERROR_BAD_MODULE;
409 return TDM_ERROR_NONE;
413 _tdm_display_update_caps_capture(tdm_private_module *private_module, tdm_caps_capture *caps)
415 tdm_func_display *func_display;
418 func_display = &private_module->func_display;
420 if (!func_display->display_get_capture_capability) {
421 TDM_ERR("backend(%s) no display_get_capture_capability()", private_module->module_data->name);
422 return TDM_ERROR_BAD_MODULE;
425 ret = func_display->display_get_capture_capability(private_module->bdata, caps);
426 if (ret != TDM_ERROR_NONE) {
427 TDM_ERR("backend(%s) display_get_capture_capability() failed", private_module->module_data->name);
428 return TDM_ERROR_BAD_MODULE;
431 return TDM_ERROR_NONE;
435 _tdm_display_update_caps_layer(tdm_private_module *private_module,
436 tdm_layer *layer_backend, tdm_caps_layer *caps)
438 tdm_func_layer *func_layer = &private_module->func_layer;
441 if (!func_layer->layer_get_capability) {
442 TDM_ERR("backend(%s) no layer_get_capability()", private_module->module_data->name);
443 return TDM_ERROR_BAD_MODULE;
446 ret = func_layer->layer_get_capability(layer_backend, caps);
447 if (ret != TDM_ERROR_NONE) {
448 TDM_ERR("backend(%s) layer_get_capability() failed", private_module->module_data->name);
449 return TDM_ERROR_BAD_MODULE;
452 return TDM_ERROR_NONE;
456 _tdm_display_update_caps_output(tdm_private_module *private_module, int pipe,
457 tdm_output *output_backend, tdm_caps_output *caps)
459 tdm_func_output *func_output = &private_module->func_output;
460 char temp[TDM_NAME_LEN];
464 stamp = tdm_helper_get_time();
465 ret = func_output->output_get_capability(output_backend, caps);
466 TDM_DBG("backend(%s) backend output_get_capability() time: %.3f ms",
467 private_module->module_data->name, (tdm_helper_get_time() - stamp) * 1000.0);
469 if (ret != TDM_ERROR_NONE) {
470 TDM_ERR("backend(%s) output_get_capability() failed", private_module->module_data->name);
471 return TDM_ERROR_BAD_MODULE;
474 /* FIXME: Use model for tdm client to distinguish amoung outputs */
475 snprintf(temp, TDM_NAME_LEN, "%s-%d", caps->model, pipe);
476 snprintf(caps->model, TDM_NAME_LEN, "%s", temp);
478 return TDM_ERROR_NONE;
482 _tdm_display_update_layer(tdm_private_output *private_output,
483 tdm_layer *layer_backend, int index)
485 tdm_private_layer *private_layer;
488 private_layer = _tdm_display_find_private_layer(private_output, layer_backend);
489 if (!private_layer) {
490 private_layer = calloc(1, sizeof(tdm_private_layer));
491 TDM_RETURN_VAL_IF_FAIL(private_layer != NULL, TDM_ERROR_OUT_OF_MEMORY);
493 LIST_ADDTAIL(&private_layer->link, &private_output->layer_list);
494 LIST_INITHEAD(&private_layer->capture_list);
496 private_layer->index = index;
497 private_layer->private_module = private_output->private_module;
498 private_layer->private_display = private_output->private_display;
499 private_layer->private_output = private_output;
500 private_layer->layer_backend = layer_backend;
501 private_layer->usable = 1;
504 _tdm_display_destroy_caps_layer(&private_layer->caps);
506 ret = _tdm_display_update_caps_layer(private_output->private_module, layer_backend, &private_layer->caps);
507 TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, ret);
509 return TDM_ERROR_NONE;
513 tdm_display_update_output(tdm_private_module *private_module, tdm_output *output_backend)
515 tdm_func_output *func_output = &private_module->func_output;
516 tdm_private_display *private_display = NULL;
517 tdm_private_output *private_output = NULL;
518 tdm_layer **layers = NULL;
519 tdm_private_hwc *private_hwc = NULL;
521 int layer_count = 0, i;
523 unsigned int output_created = 0;
525 private_display = private_module->private_display;
527 private_output = tdm_display_find_private_output(private_display, output_backend);
528 if (!private_output) {
529 unsigned int pipe = tdm_display_find_empty_output_pipe(private_display);
531 private_output = calloc(1, sizeof(tdm_private_output));
532 TDM_RETURN_VAL_IF_FAIL(private_output != NULL, TDM_ERROR_OUT_OF_MEMORY);
534 private_output->stamp = tdm_helper_get_time();
535 while (tdm_display_find_output_stamp(private_display, private_output->stamp))
536 private_output->stamp++;
538 LIST_ADDTAIL(&private_output->link, &private_module->output_list);
540 private_output->private_module = private_module;
541 private_output->private_display = private_display;
542 private_output->current_dpms_value = TDM_OUTPUT_DPMS_OFF;
543 private_output->output_backend = output_backend;
544 private_output->pipe = pipe;
545 private_output->index = pipe;
547 LIST_INITHEAD(&private_output->capture_list);
548 LIST_INITHEAD(&private_output->vblank_handler_list);
549 LIST_INITHEAD(&private_output->output_commit_handler_list);
550 LIST_INITHEAD(&private_output->pending_commit_handler_list);
551 LIST_INITHEAD(&private_output->destroy_handler_list);
552 LIST_INITHEAD(&private_output->change_handler_list);
553 LIST_INITHEAD(&private_output->mode_change_request_handler_list);
555 if (func_output->output_set_status_handler) {
556 func_output->output_set_status_handler(private_output->output_backend,
557 tdm_output_cb_status,
559 private_output->regist_change_cb = 1;
564 /* NOTE that output modes will be allocated newly after here */
565 _tdm_display_destroy_caps_output(&private_output->caps);
566 ret = _tdm_display_update_caps_output(private_module, private_output->pipe, output_backend, &private_output->caps);
567 TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, ret);
569 if (private_output->caps.status == TDM_OUTPUT_CONN_STATUS_DISCONNECTED)
570 private_output->current_mode = NULL;
572 /* get the tdm_hwc object */
573 if (private_output->caps.capabilities&TDM_OUTPUT_CAPABILITY_HWC) {
574 hwc = func_output->output_get_hwc(output_backend, &ret);
575 TDM_RETURN_VAL_IF_FAIL(hwc != NULL, ret);
577 private_hwc = calloc(1, sizeof(tdm_private_hwc));
578 TDM_RETURN_VAL_IF_FAIL(private_hwc != NULL, TDM_ERROR_OUT_OF_MEMORY);
580 private_hwc->private_module = private_module;
581 private_hwc->private_output = private_output;
582 private_hwc->hwc_backend = hwc;
583 private_hwc->index = pipe;
584 private_hwc->stamp = tdm_helper_get_time();
585 while (tdm_display_find_hwc_stamp(private_module->private_display, private_hwc->stamp))
586 private_hwc->stamp++;
588 LIST_INITHEAD(&private_hwc->hwc_window_list);
589 LIST_INITHEAD(&private_hwc->hwc_commit_handler_list);
591 private_output->private_hwc = private_hwc;
593 LIST_INITHEAD(&private_output->layer_list);
594 LIST_INITHEAD(&private_output->layer_commit_handler_list);
597 _tdm_display_destroy_caps_output(&private_output->caps);
598 ret = _tdm_display_update_caps_output(private_module, private_output->pipe, output_backend, &private_output->caps);
599 TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, ret);
601 if (private_output->caps.status == TDM_OUTPUT_CONN_STATUS_DISCONNECTED)
602 private_output->current_mode = NULL;
605 /* do not use the layer object when the tdm_output has the hwc capability */
606 if ((private_output->caps.capabilities&TDM_OUTPUT_CAPABILITY_HWC) == 0) {
607 layers = func_output->output_get_layers(output_backend, &layer_count, &ret);
608 if (ret != TDM_ERROR_NONE) {
613 for (i = 0; i < layer_count; i++) {
614 ret = _tdm_display_update_layer(private_output, layers[i], i);
615 if (ret != TDM_ERROR_NONE) {
624 if (output_created) {
625 ret = tdm_display_call_thread_cb_output_create(private_display, private_output);
626 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
629 return TDM_ERROR_NONE;
633 _tdm_display_set_main_first(tdm_output **outputs, int index)
635 tdm_output *output_tmp = NULL;
640 output_tmp = outputs[0];
641 outputs[0] = outputs[index];
642 outputs[index] = output_tmp;
646 _tdm_display_get_ordered_outputs(tdm_private_module *private_module, int *count)
648 tdm_func_display *func_display = &private_module->func_display;
649 tdm_output **outputs = NULL;
650 tdm_output *output_dsi = NULL;
651 tdm_output *output_lvds = NULL;
652 tdm_output *output_hdmia = NULL;
653 tdm_output *output_hdmib = NULL;
654 int i, output_count = 0, output_connected_count = 0;
655 int index_dsi = 0, index_lvds = 0, index_hdmia = 0, index_hdmib = 0;
658 /* don't change list order if not init time */
659 if (private_module->outputs) {
660 TDM_ERR("can't change output order");
661 return private_module->outputs;
664 outputs = func_display->display_get_outputs(private_module->bdata, &output_count, &ret);
665 if (ret != TDM_ERROR_NONE)
668 *count = output_count;
670 if (output_count == 0)
672 else if (output_count == 1) {
673 private_module->outputs = outputs;
677 /* count connected outputs */
678 for (i = 0; i < output_count; i++) {
679 tdm_func_output *func_output = &private_module->func_output;
680 tdm_caps_output caps;
681 memset(&caps, 0, sizeof(tdm_caps_output));
683 ret = func_output->output_get_capability(outputs[i], &caps);
684 if (ret != TDM_ERROR_NONE) {
685 TDM_ERR("output_get_capability() failed");
689 if (caps.status == TDM_OUTPUT_CONN_STATUS_CONNECTED) {
690 output_connected_count++;
693 case TDM_OUTPUT_TYPE_DSI:
694 output_dsi = outputs[i];
697 case TDM_OUTPUT_TYPE_LVDS:
698 output_lvds = outputs[i];
701 case TDM_OUTPUT_TYPE_HDMIA:
702 output_hdmia = outputs[i];
705 case TDM_OUTPUT_TYPE_HDMIB:
706 output_hdmib = outputs[i];
714 _tdm_display_destroy_caps_output(&caps);
717 /* ordering : main output is first */
718 /* If there is no connected output, lvds or dsi cannot be main display. (cannot connect after booting)
719 * But hdmi is possible, so set hdmi to main display.
720 * If connected only one output, it is main output.
721 * If connected outputs over 2, has priority like below.
722 * (dsi > lvds > hdmi > else)
724 if (output_connected_count == 0) {
725 /* hdmi > dsi > lvds > else */
726 if (output_hdmia != NULL)
727 _tdm_display_set_main_first(outputs, index_hdmia);
728 else if (output_hdmib != NULL)
729 _tdm_display_set_main_first(outputs, index_hdmib);
730 else if (output_dsi != NULL)
731 _tdm_display_set_main_first(outputs, index_dsi);
732 else if (output_lvds != NULL)
733 _tdm_display_set_main_first(outputs, index_lvds);
734 } else { /* (output_connected_count > 1) */
735 /* dsi > lvds > hdmi > else */
736 if (output_dsi != NULL)
737 _tdm_display_set_main_first(outputs, index_dsi);
738 else if (output_lvds != NULL)
739 _tdm_display_set_main_first(outputs, index_lvds);
740 else if (output_hdmia != NULL)
741 _tdm_display_set_main_first(outputs, index_hdmia);
742 else if (output_hdmib != NULL)
743 _tdm_display_set_main_first(outputs, index_hdmib);
746 private_module->outputs = outputs;
757 _tdm_display_setup(tdm_private_display *private_display)
759 tdm_private_module *private_module = NULL;
760 tdm_error ret = TDM_ERROR_NONE;
761 int output_count = 0;
764 if (private_display->pp_module) {
765 ret = _tdm_display_update_caps_pp(private_display->pp_module,
766 &private_display->pp_module->caps_pp);
767 if (ret != TDM_ERROR_NONE)
771 if (private_display->capture_module) {
772 ret = _tdm_display_update_caps_capture(private_display->capture_module,
773 &private_display->capture_module->caps_capture);
774 if (ret != TDM_ERROR_NONE)
778 LIST_FOR_EACH_ENTRY(private_module, &private_display->module_list, link) {
779 tdm_output **outputs;
782 outputs = _tdm_display_get_ordered_outputs(private_module, &count);
785 TDM_GOTO_IF_FAIL(outputs != NULL, failed_update);
787 for (i = 0; i < count; i++) {
788 ret = tdm_display_update_output(private_module, outputs[i]);
789 if (ret != TDM_ERROR_NONE)
795 /* At least, the output count should be greater than 0 to ensure tdm_vblank works.
796 * So we will create a dummy output when backends don't have any output.
797 * Without destroying a tdm_output object, this dummy output will be replaced with
798 * a virtual output which is created in runtime.
800 if (output_count == 0) {
801 tdm_output **outputs;
804 TDM_INFO("loading a %s backend", TDM_DUMMY_MODULE);
805 ret = _tdm_display_load_module_with_file(private_display, TDM_DUMMY_MODULE);
806 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed_update);
807 TDM_GOTO_IF_FAIL(private_display->dummy_module != NULL, failed_update);
809 private_module = private_display->dummy_module;
811 outputs = _tdm_display_get_ordered_outputs(private_module, &count);
812 TDM_GOTO_IF_FAIL(count > 0, failed_update);
813 TDM_GOTO_IF_FAIL(outputs != NULL, failed_update);
815 for (i = 0; i < count; i++) {
816 ret = tdm_display_update_output(private_module, outputs[i]);
817 if (ret != TDM_ERROR_NONE)
822 virtual = tdm_config_get_int(TDM_CONFIG_KEY_GENERAL_VIRTUAL_OUTPUT, 0);
824 TDM_INFO("loading a %s backend", TDM_VIRTUAL_MODULE);
825 ret = _tdm_display_load_module_with_file(private_display, TDM_VIRTUAL_MODULE);
826 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed_update);
827 TDM_GOTO_IF_FAIL(private_display->virtual_module != NULL, failed_update);
830 return TDM_ERROR_NONE;
833 _tdm_display_destroy_private_display(private_display);
839 tdm_display_update(tdm_display *dpy)
841 TDM_RETURN_VAL_IF_FAIL(dpy != NULL, TDM_ERROR_INVALID_PARAMETER);
843 TDM_DEPRECATED(NULL);
845 return TDM_ERROR_NONE;
848 int tdm_debug_module;
850 int tdm_ttrace_module;
851 int tdm_ttrace_output;
853 static tdm_private_display *g_private_display;
854 static pthread_mutex_t gLock = PTHREAD_MUTEX_INITIALIZER;
856 /* LCOV_EXCL_START */
858 _tdm_display_check_module(tdm_backend_module *module)
860 int tdm_backend_major, tdm_backend_minor;
861 int backend_module_major, backend_module_minor;
863 tdm_backend_major = TDM_BACKEND_GET_ABI_MAJOR(TDM_BACKEND_ABI_LATEST_VERSION);
864 tdm_backend_minor = TDM_BACKEND_GET_ABI_MINOR(TDM_BACKEND_ABI_LATEST_VERSION);
866 TDM_INFO("TDM Backend ABI version : %d.%d",
867 tdm_backend_major, tdm_backend_minor);
870 TDM_ERR("TDM backend doesn't have name");
871 return TDM_ERROR_BAD_MODULE;
874 if (!module->vendor) {
875 TDM_ERR("TDM backend doesn't have vendor");
876 return TDM_ERROR_BAD_MODULE;
879 backend_module_major = TDM_BACKEND_GET_ABI_MAJOR(module->abi_version);
880 backend_module_minor = TDM_BACKEND_GET_ABI_MINOR(module->abi_version);
882 TDM_INFO("TDM module name: %s", module->name);
883 TDM_INFO("'%s' vendor: %s", module->name, module->vendor);
884 TDM_INFO("'%s' backend ABI version: %d.%d", module->name, backend_module_major, backend_module_minor);
886 if (backend_module_major > tdm_backend_major) {
887 TDM_ERR("'%s' major version(%d) is newer than %d",
888 module->name, backend_module_major, tdm_backend_major);
889 return TDM_ERROR_BAD_MODULE;
892 if (tdm_backend_major == backend_module_major &&
893 backend_module_minor > tdm_backend_minor) {
894 TDM_ERR("'%s' minor version(%d) is newer than %d",
895 module->name, backend_module_minor, tdm_backend_minor);
896 return TDM_ERROR_BAD_MODULE;
900 TDM_ERR("'%s' doesn't have init function", module->name);
901 return TDM_ERROR_BAD_MODULE;
904 if (!module->deinit) {
905 TDM_ERR("'%s' doesn't have deinit function", module->name);
906 return TDM_ERROR_BAD_MODULE;
909 return TDM_ERROR_NONE;
913 _tdm_display_check_backend_functions(tdm_private_module *private_module)
915 tdm_func_display *func_display = &private_module->func_display;
916 tdm_func_output *func_output = &private_module->func_output;
917 tdm_func_layer *func_layer = &private_module->func_layer;
920 /* below functions should be implemented in backend side */
922 TDM_RETURN_VAL_IF_FAIL(func_display != NULL, TDM_ERROR_BAD_MODULE);
923 TDM_RETURN_VAL_IF_FAIL(func_display->display_get_capability, TDM_ERROR_BAD_MODULE);
924 TDM_RETURN_VAL_IF_FAIL(func_display->display_get_outputs, TDM_ERROR_BAD_MODULE);
925 TDM_RETURN_VAL_IF_FAIL(func_output->output_get_capability, TDM_ERROR_BAD_MODULE);
926 TDM_RETURN_VAL_IF_FAIL(func_output->output_get_layers, TDM_ERROR_BAD_MODULE);
927 TDM_RETURN_VAL_IF_FAIL(func_output->output_wait_vblank, TDM_ERROR_BAD_MODULE);
928 TDM_RETURN_VAL_IF_FAIL(func_output->output_set_vblank_handler, TDM_ERROR_BAD_MODULE);
929 TDM_RETURN_VAL_IF_FAIL(func_output->output_commit, TDM_ERROR_BAD_MODULE);
930 TDM_RETURN_VAL_IF_FAIL(func_output->output_set_commit_handler, TDM_ERROR_BAD_MODULE);
931 TDM_RETURN_VAL_IF_FAIL(func_output->output_set_mode, TDM_ERROR_BAD_MODULE);
932 TDM_RETURN_VAL_IF_FAIL(func_layer->layer_get_capability, TDM_ERROR_BAD_MODULE);
933 TDM_RETURN_VAL_IF_FAIL(func_layer->layer_set_info, TDM_ERROR_BAD_MODULE);
934 TDM_RETURN_VAL_IF_FAIL(func_layer->layer_get_info, TDM_ERROR_BAD_MODULE);
935 TDM_RETURN_VAL_IF_FAIL(func_layer->layer_set_buffer, TDM_ERROR_BAD_MODULE);
936 TDM_RETURN_VAL_IF_FAIL(func_layer->layer_unset_buffer, TDM_ERROR_BAD_MODULE);
938 ret = func_display->display_get_capability(private_module->bdata, &private_module->caps_display);
939 if (ret != TDM_ERROR_NONE) {
940 TDM_ERR("display_get_capability() failed");
941 return TDM_ERROR_BAD_MODULE;
944 if (private_module->capabilities & TDM_DISPLAY_CAPABILITY_PP) {
945 tdm_func_pp *func_pp = &private_module->func_pp;
946 TDM_RETURN_VAL_IF_FAIL(func_display->display_get_pp_capability, TDM_ERROR_BAD_MODULE);
947 TDM_RETURN_VAL_IF_FAIL(func_display->display_create_pp, TDM_ERROR_BAD_MODULE);
948 TDM_RETURN_VAL_IF_FAIL(func_pp->pp_destroy, TDM_ERROR_BAD_MODULE);
949 TDM_RETURN_VAL_IF_FAIL(func_pp->pp_commit, TDM_ERROR_BAD_MODULE);
950 TDM_RETURN_VAL_IF_FAIL(func_pp->pp_set_done_handler, TDM_ERROR_BAD_MODULE);
953 if (private_module->capabilities & TDM_DISPLAY_CAPABILITY_CAPTURE) {
954 tdm_func_capture *func_capture = &private_module->func_capture;
955 TDM_RETURN_VAL_IF_FAIL(func_display->display_get_capture_capability, TDM_ERROR_BAD_MODULE);
956 if (private_module->caps_capture.capabilities & TDM_CAPTURE_CAPABILITY_OUTPUT)
957 TDM_RETURN_VAL_IF_FAIL(func_output->output_create_capture, TDM_ERROR_BAD_MODULE);
958 if (private_module->caps_capture.capabilities & TDM_CAPTURE_CAPABILITY_LAYER)
959 TDM_RETURN_VAL_IF_FAIL(func_layer->layer_create_capture, TDM_ERROR_BAD_MODULE);
960 TDM_RETURN_VAL_IF_FAIL(func_capture->capture_destroy, TDM_ERROR_BAD_MODULE);
961 TDM_RETURN_VAL_IF_FAIL(func_capture->capture_commit, TDM_ERROR_BAD_MODULE);
962 TDM_RETURN_VAL_IF_FAIL(func_capture->capture_set_done_handler, TDM_ERROR_BAD_MODULE);
965 return TDM_ERROR_NONE;
969 _tdm_display_load_module_with_file(tdm_private_display *private_display,
972 char path[TDM_PATH_LEN] = {0,};
974 tdm_backend_module *module_data;
975 tdm_backend_data *bdata = NULL;
976 tdm_private_module *private_module = NULL;
981 size = snprintf(path, sizeof(path), TDM_MODULE_PATH "/%s", file);
982 if (size >= (int)sizeof(path)) {
983 TDM_WRN("too long: %s/%s", TDM_MODULE_PATH, file);
984 return TDM_ERROR_BAD_MODULE;
987 private_module = calloc(1, sizeof *private_module);
988 if (!private_module) {
989 TDM_ERR("alloc failed: %m");
990 ret = TDM_ERROR_OUT_OF_MEMORY;
994 private_module->private_display = private_display;
996 stamp = tdm_helper_get_time();
998 TDM_TRACE_BEGIN("TDM_Load_Backend");
999 module = dlopen(path, RTLD_LAZY);
1001 TDM_ERR("%s", dlerror());
1003 ret = TDM_ERROR_NO_MODULE;
1007 private_module->module = module;
1009 module_data = dlsym(module, "tdm_backend_module_data");
1011 TDM_ERR("'%s' doesn't have data object", file);
1012 ret = TDM_ERROR_BAD_MODULE;
1018 private_module->module_data = module_data;
1020 TDM_DBG("dlopen, dlsym time: %.3f ms", (tdm_helper_get_time() - stamp) * 1000.0);
1022 /* check if version, init() and deinit() are valid or not */
1023 ret = _tdm_display_check_module(module_data);
1024 if (ret != TDM_ERROR_NONE) {
1025 TDM_ERR("backend(%s) load failed: check module error", module_data->name);
1029 private_display->current_module = private_module;
1031 /* We don't care if backend_data is NULL or not. It's up to backend. */
1032 TDM_TRACE_BEGIN("TDM_Init_Backend");
1033 stamp = tdm_helper_get_time();
1034 bdata = module_data->init((tdm_display *)private_display, &ret);
1035 TDM_DBG("backend init() time: %.3f ms", (tdm_helper_get_time() - stamp) * 1000.0);
1038 private_display->current_module = NULL;
1040 if (!strncmp(file, TDM_DUMMY_MODULE, TDM_NAME_LEN))
1041 private_display->dummy_module = private_module;
1043 if (!strncmp(file, TDM_VIRTUAL_MODULE, TDM_NAME_LEN))
1044 private_display->virtual_module = private_module;
1046 private_module->bdata = bdata;
1048 if (ret != TDM_ERROR_NONE) {
1049 TDM_ERR("backend(%s) load failed: init error", module_data->name);
1053 ret = _tdm_display_check_backend_functions(private_module);
1054 if (ret != TDM_ERROR_NONE) {
1055 TDM_ERR("backend(%s) load failed: check functions error", module_data->name);
1059 LIST_INITHEAD(&private_module->output_list);
1060 LIST_INITHEAD(&private_module->voutput_list);
1061 LIST_INITHEAD(&private_module->pp_list);
1062 LIST_INITHEAD(&private_module->capture_list);
1064 LIST_ADDTAIL(&private_module->link, &private_display->module_list);
1066 TDM_INFO("Success to load '%s' module", module_data->name);
1068 return TDM_ERROR_NONE;
1071 module_data->deinit(bdata);
1075 free(private_module);
1080 _tdm_display_load_modules(tdm_private_display *private_display)
1082 const char *module_names;
1083 tdm_error ret = TDM_ERROR_NONE;
1084 char temp[TDM_PATH_LEN];
1088 LIST_INITHEAD(&private_display->module_list);
1090 module_names = tdm_config_get_string(TDM_CONFIG_KEY_GENERAL_BACKENDS, TDM_DEFAULT_MODULE);
1092 snprintf(temp, TDM_PATH_LEN, "%s", module_names);
1094 arg = strtok_r(temp, TDM_CONFIG_DELIM, &end);
1096 TDM_INFO("loading a %s backend", arg);
1097 ret = _tdm_display_load_module_with_file(private_display, arg);
1098 if (ret == TDM_ERROR_NONE)
1099 TDM_INFO("%s backend loading success", arg);
1101 TDM_INFO("%s backend loading failed", arg);
1102 arg = strtok_r(NULL, TDM_CONFIG_DELIM, &end);
1109 _tdm_display_unload_modules(tdm_private_display *private_display)
1111 tdm_private_module *private_module = NULL, *bb = NULL;
1113 LIST_FOR_EACH_ENTRY_SAFE(private_module, bb, &private_display->module_list, link) {
1114 LIST_DEL(&private_module->link);
1116 if (private_module->module_data)
1117 private_module->module_data->deinit(private_module->bdata);
1118 if (private_module->module)
1119 dlclose(private_module->module);
1121 free(private_module);
1124 /* LCOV_EXCL_STOP */
1127 tdm_display_find_stamp(tdm_private_display *private_display, double stamp)
1129 return (void*)g_private_display;
1132 EXTERN tdm_display *
1133 tdm_display_init(tdm_error *error)
1135 tdm_private_display *private_display = NULL;
1138 double stamp1, stamp2, start;
1141 pthread_mutex_lock(&gLock);
1143 if (g_private_display) {
1144 g_private_display->init_count++;
1145 pthread_mutex_unlock(&gLock);
1147 *error = TDM_ERROR_NONE;
1148 return g_private_display;
1151 start = stamp1 = tdm_helper_get_time();
1153 private_display = calloc(1, sizeof(tdm_private_display));
1154 if (!private_display) {
1155 /* LCOV_EXCL_START */
1156 ret = TDM_ERROR_OUT_OF_MEMORY;
1157 TDM_ERR("'private_display != NULL' failed");
1159 /* LCOV_EXCL_STOP */
1162 private_display->stamp = tdm_helper_get_time();
1164 LIST_INITHEAD(&private_display->output_create_handler_list);
1166 str = tdm_config_get_string(TDM_CONFIG_KEY_DEBUG_MODULE, NULL);
1168 tdm_display_enable_debug_module(str);
1170 str = tdm_config_get_string(TDM_CONFIG_KEY_DEBUG_DUMP, NULL);
1172 tdm_display_enable_dump(private_display, str, NULL, NULL);
1174 if (pthread_mutex_init(&private_display->lock, NULL)) {
1175 /* LCOV_EXCL_START */
1176 ret = TDM_ERROR_OPERATION_FAILED;
1177 TDM_ERR("mutex init failed: %m");
1178 goto failed_mutex_init;
1179 /* LCOV_EXCL_STOP */
1182 _pthread_mutex_lock(&private_display->lock);
1184 stamp2 = tdm_helper_get_time();
1185 TDM_DBG("prepare init time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1188 ret = tdm_event_loop_init(private_display);
1189 if (ret != TDM_ERROR_NONE)
1192 stamp2 = tdm_helper_get_time();
1193 TDM_INFO("event loop init time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1196 ret = tdm_vblank_init(private_display);
1197 if (ret != TDM_ERROR_NONE)
1200 ret = tdm_output_init(private_display);
1201 if (ret != TDM_ERROR_NONE)
1204 ret = tdm_hwc_init(private_display);
1205 if (ret != TDM_ERROR_NONE)
1208 ret = tdm_pp_init(private_display);
1209 if (ret != TDM_ERROR_NONE)
1212 ret = tdm_capture_init(private_display);
1213 if (ret != TDM_ERROR_NONE)
1216 ret = _tdm_display_load_modules(private_display);
1217 if (!(ret == TDM_ERROR_NONE || ret == TDM_ERROR_NO_MODULE))
1220 stamp2 = tdm_helper_get_time();
1221 TDM_INFO("loading backend time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1225 int tdm_drm_fd = tdm_helper_get_fd("TDM_DRM_MASTER_FD");
1226 if (tdm_drm_fd >= 0) {
1227 private_display->bufmgr = tbm_bufmgr_init(tdm_drm_fd);
1229 if (!private_display->bufmgr) {
1230 TDM_ERR("tbm_bufmgr_init failed");
1233 TDM_INFO("tbm_bufmgr_init successed");
1236 stamp2 = tdm_helper_get_time();
1237 TDM_DBG("initializing bufmgr time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1242 TDM_TRACE_BEGIN("TDM_Update_Display");
1243 ret = _tdm_display_setup(private_display);
1245 if (ret != TDM_ERROR_NONE)
1248 stamp2 = tdm_helper_get_time();
1249 TDM_DBG("updating display time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1252 tdm_event_loop_create_backend_source(private_display);
1254 private_display->init_count = 1;
1255 g_private_display = private_display;
1257 /* the COMMIT_PER_VBLANK functionality is ability of an output to support
1258 * several operational modes (commit_per_vblank modes) related to tdm_commit;
1259 * this functionality can be turned off which means a default mode */
1260 mode = tdm_config_get_int(TDM_CONFIG_KEY_GENERAL_COMMIT_PER_VBLANK, 0);
1262 tdm_private_module *b = NULL;
1263 tdm_private_output *o = NULL;
1265 /* outputs which support hwc capability can work only
1266 * if commit_per_vblank mode is '0' (default mode) */
1267 LIST_FOR_EACH_ENTRY(b, &private_display->module_list, link) {
1268 LIST_FOR_EACH_ENTRY(o, &b->output_list, link) {
1269 if (!(o->caps.capabilities & TDM_OUTPUT_CAPABILITY_HWC))
1270 tdm_output_choose_commit_per_vblank_mode(o, mode);
1276 *error = TDM_ERROR_NONE;
1278 tdm_thread_cb_set_find_func(TDM_THREAD_CB_DISPLAY_OUTPUT_CREATE, tdm_display_find_stamp);
1280 _pthread_mutex_unlock(&private_display->lock);
1281 pthread_mutex_unlock(&gLock);
1283 TDM_INFO("init time: %.3f ms", (tdm_helper_get_time() - start) * 1000.0);
1285 return (tdm_display *)private_display;
1287 /* LCOV_EXCL_START */
1289 _tdm_display_unload_modules(private_display);
1291 tdm_vblank_deinit(private_display);
1293 tdm_event_loop_stop(private_display);
1294 tdm_event_loop_deinit(private_display);
1296 _pthread_mutex_unlock(&private_display->lock);
1297 pthread_mutex_destroy(&private_display->lock);
1299 free(private_display);
1303 pthread_mutex_unlock(&gLock);
1305 /* LCOV_EXCL_STOP */
1309 tdm_display_deinit(tdm_display *dpy)
1311 tdm_private_display *private_display = dpy;
1313 if (!private_display)
1316 pthread_mutex_lock(&gLock);
1318 private_display->init_count--;
1319 if (private_display->init_count > 0) {
1320 pthread_mutex_unlock(&gLock);
1324 /* dont move the position of lock/unlock. all resource should be protected
1325 * during destroying. after tdm_event_loop_deinit, we don't worry about thread
1326 * things because it's finalized.
1328 _pthread_mutex_lock(&private_display->lock);
1329 tdm_event_loop_stop(private_display);
1330 _pthread_mutex_unlock(&private_display->lock);
1332 tdm_event_loop_deinit(private_display);
1334 /* when private_output is destroyed, all vblank resources of client and server
1335 * are destroyed. Then we can call tdm_vblank_deinit. After destroying display,
1336 * we can unload backend modulues.
1338 _tdm_display_destroy_private_display(private_display);
1339 _tdm_display_unload_modules(private_display);
1341 tdm_vblank_deinit(private_display);
1344 if (private_display->bufmgr)
1345 tbm_bufmgr_deinit(private_display->bufmgr);
1348 tbm_drm_helper_unset_tbm_master_fd();
1350 pthread_mutex_destroy(&private_display->lock);
1351 free(private_display);
1352 g_private_display = NULL;
1354 if (tdm_debug_dump_dir) {
1355 free(tdm_debug_dump_dir);
1356 tdm_debug_dump_dir = NULL;
1359 tdm_config_deinit();
1361 pthread_mutex_unlock(&gLock);
1366 INTERN tdm_private_display *
1367 tdm_display_get(void)
1369 return g_private_display;