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_hwc(tdm_private_display *private_display, tdm_hwc *hwc_backend)
133 tdm_private_module *private_module = NULL;
134 tdm_private_output *private_output = NULL;
135 tdm_private_hwc *private_hwc = NULL;
137 LIST_FOR_EACH_ENTRY(private_module, &private_display->module_list, link) {
138 LIST_FOR_EACH_ENTRY(private_output, &private_module->output_list, link) {
139 if (!private_output->private_hwc) continue;
140 private_hwc = private_output->private_hwc;
141 if (private_hwc->hwc_backend == hwc_backend)
150 tdm_display_find_hwc_stamp(tdm_private_display *private_display, double stamp)
152 tdm_private_module *private_module = NULL;
153 tdm_private_output *private_output = NULL;
154 tdm_private_hwc *private_hwc = NULL;
156 LIST_FOR_EACH_ENTRY(private_module, &private_display->module_list, link) {
157 LIST_FOR_EACH_ENTRY(private_output, &private_module->output_list, link) {
158 if (!private_output->private_hwc) continue;
159 private_hwc = private_output->private_hwc;
160 if (private_hwc->stamp == stamp)
169 _tdm_display_destroy_caps_pp(tdm_caps_pp *caps_pp)
171 if (caps_pp->formats)
172 free(caps_pp->formats);
174 memset(caps_pp, 0, sizeof(tdm_caps_pp));
178 _tdm_display_destroy_caps_capture(tdm_caps_capture *caps_capture)
180 if (caps_capture->formats)
181 free(caps_capture->formats);
183 memset(caps_capture, 0, sizeof(tdm_caps_capture));
187 _tdm_display_destroy_caps_layer(tdm_caps_layer *caps_layer)
189 if (caps_layer->formats)
190 free(caps_layer->formats);
192 if (caps_layer->props)
193 free(caps_layer->props);
195 memset(caps_layer, 0, sizeof(tdm_caps_layer));
199 _tdm_display_destroy_caps_output(tdm_caps_output *caps_output)
201 if (caps_output->modes)
202 free(caps_output->modes);
204 if (caps_output->props)
205 free(caps_output->props);
207 memset(caps_output, 0, sizeof(tdm_caps_output));
211 _tdm_display_destroy_private_layer(tdm_private_layer *private_layer)
213 tdm_private_capture *c = NULL, *cc = NULL;
215 tdm_layer_unset_buffer_internal(private_layer);
217 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &private_layer->capture_list, link)
218 tdm_capture_destroy_internal(c);
220 _tdm_display_destroy_caps_layer(&private_layer->caps);
222 /* when destroying layer, someone could check if layer is valid. So delete
223 * the layer's link at last.
225 LIST_DEL(&private_layer->link);
231 tdm_display_destroy_private_output(tdm_private_output *private_output)
233 tdm_private_display *private_display = private_output->private_display;
234 tdm_private_layer *l = NULL, *ll = NULL;
235 tdm_private_hwc *private_hwc = NULL;
236 tdm_private_hwc_window *hw = NULL, *hww = NULL;
237 tdm_private_capture *c = NULL, *cc = NULL;
238 tdm_private_output_vblank_handler *v = NULL, *vv = NULL;
239 tdm_private_output_commit_handler *om = NULL, *omm = NULL;
240 tdm_private_hwc_commit_handler *hm = NULL, *hmm = NULL;
241 tdm_private_layer_commit_handler *lm = NULL, *lmm = NULL;
242 tdm_private_output_change_handler *h = NULL, *hh = NULL;
243 tdm_private_output_destroy_handler *dh = NULL, *dhh = NULL;
246 ret = tdm_output_call_thread_cb_destroy(private_output);
247 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
249 free(private_output->layers_ptr);
251 if (private_output->vblank_timeout_timer)
252 tdm_event_loop_source_remove(private_output->vblank_timeout_timer);
254 LIST_FOR_EACH_ENTRY_SAFE(v, vv, &private_output->vblank_handler_list, link) {
259 LIST_FOR_EACH_ENTRY_SAFE(om, omm, &private_output->output_commit_handler_list, link) {
264 if (private_output->private_hwc) {
265 private_hwc = private_output->private_hwc;
266 LIST_FOR_EACH_ENTRY_SAFE(hm, hmm, &private_hwc->hwc_commit_handler_list, link) {
271 LIST_FOR_EACH_ENTRY_SAFE(lm, lmm, &private_output->layer_commit_handler_list, link) {
277 LIST_FOR_EACH_ENTRY_SAFE(lm, lmm, &private_output->pending_commit_handler_list, link) {
282 LIST_FOR_EACH_ENTRY_SAFE(h, hh, &private_output->change_handler_list, link) {
284 tdm_thread_cb_remove(h->private_output, TDM_THREAD_CB_OUTPUT_DPMS, NULL, tdm_output_thread_cb_change, h);
285 tdm_thread_cb_remove(h->private_output, TDM_THREAD_CB_OUTPUT_STATUS, NULL, tdm_output_thread_cb_change, h);
289 LIST_FOR_EACH_ENTRY_SAFE(dh, dhh, &private_output->destroy_handler_list, link) {
291 tdm_thread_cb_remove(dh->private_output, TDM_THREAD_CB_OUTPUT_DESTROY, NULL, tdm_output_thread_cb_destroy, dh);
295 if (private_output->vblank) {
296 /* tdm_vblank APIs is for server. it should be called in unlock status*/
297 _pthread_mutex_unlock(&private_display->lock);
298 tdm_vblank_destroy(private_output->vblank);
299 _pthread_mutex_lock(&private_display->lock);
302 if (private_output->ttrace_vblank) {
303 /* tdm_vblank APIs is for server. it should be called in unlock status*/
304 _pthread_mutex_unlock(&private_display->lock);
305 tdm_vblank_destroy(private_output->ttrace_vblank);
306 _pthread_mutex_lock(&private_display->lock);
309 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &private_output->capture_list, link)
310 tdm_capture_destroy_internal(c);
312 if (private_output->private_hwc) {
313 private_hwc = private_output->private_hwc;
314 LIST_FOR_EACH_ENTRY_SAFE(hw, hww, &private_hwc->hwc_window_list, link)
315 tdm_hwc_window_destroy_internal(hw);
318 LIST_FOR_EACH_ENTRY_SAFE(l, ll, &private_output->layer_list, link)
319 _tdm_display_destroy_private_layer(l);
322 _tdm_display_destroy_caps_output(&private_output->caps);
324 /* when destroying output, vblank objects are also destroyed. vblank checks
325 * if output object is valid. So delete the output's link at last.
327 LIST_DEL(&private_output->link);
329 private_output->stamp = 0;
330 free(private_output);
334 _tdm_display_destroy_private_display(tdm_private_display *private_display)
336 tdm_private_module *private_module = NULL, *bb = NULL;
337 tdm_private_output *o = NULL, *oo = NULL;
338 tdm_private_pp *p = NULL, *pp = NULL;
339 tdm_private_output_create_handler *ch = NULL, *chh = NULL;
341 LIST_FOR_EACH_ENTRY_SAFE(private_module, bb, &private_display->module_list, link) {
342 LIST_FOR_EACH_ENTRY_SAFE(p, pp, &private_module->pp_list, link) {
343 tdm_pp_destroy_internal(p);
346 LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_module->output_list, link) {
347 tdm_display_destroy_private_output(o);
350 _tdm_display_destroy_caps_pp(&private_module->caps_pp);
351 _tdm_display_destroy_caps_capture(&private_module->caps_capture);
353 private_module->capabilities = 0;
354 private_module->caps_display.max_layer_count = 0;
356 if (private_module->outputs) {
357 free(private_module->outputs);
358 private_module->outputs = NULL;
362 LIST_FOR_EACH_ENTRY_SAFE(ch, chh, &private_display->output_create_handler_list, link) {
364 tdm_thread_cb_remove(ch->private_display, TDM_THREAD_CB_DISPLAY_OUTPUT_CREATE, NULL,
365 tdm_display_thread_cb_output_create, ch);
371 _tdm_display_update_caps_pp(tdm_private_module *private_module, tdm_caps_pp *caps)
373 tdm_func_display *func_display;
376 func_display = &private_module->func_display;
378 if (!func_display->display_get_pp_capability) {
379 TDM_ERR("backend(%s) no display_get_pp_capability()", private_module->module_data->name);
380 return TDM_ERROR_BAD_MODULE;
383 ret = func_display->display_get_pp_capability(private_module->bdata, caps);
384 if (ret != TDM_ERROR_NONE) {
385 TDM_ERR("backend(%s) display_get_pp_capability() failed", private_module->module_data->name);
386 return TDM_ERROR_BAD_MODULE;
389 return TDM_ERROR_NONE;
393 _tdm_display_update_caps_capture(tdm_private_module *private_module, tdm_caps_capture *caps)
395 tdm_func_display *func_display;
398 func_display = &private_module->func_display;
400 if (!func_display->display_get_capture_capability) {
401 TDM_ERR("backend(%s) no display_get_capture_capability()", private_module->module_data->name);
402 return TDM_ERROR_BAD_MODULE;
405 ret = func_display->display_get_capture_capability(private_module->bdata, caps);
406 if (ret != TDM_ERROR_NONE) {
407 TDM_ERR("backend(%s) display_get_capture_capability() failed", private_module->module_data->name);
408 return TDM_ERROR_BAD_MODULE;
411 return TDM_ERROR_NONE;
415 _tdm_display_update_caps_layer(tdm_private_module *private_module,
416 tdm_layer *layer_backend, tdm_caps_layer *caps)
418 tdm_func_layer *func_layer = &private_module->func_layer;
421 if (!func_layer->layer_get_capability) {
422 TDM_ERR("backend(%s) no layer_get_capability()", private_module->module_data->name);
423 return TDM_ERROR_BAD_MODULE;
426 ret = func_layer->layer_get_capability(layer_backend, caps);
427 if (ret != TDM_ERROR_NONE) {
428 TDM_ERR("backend(%s) layer_get_capability() failed", private_module->module_data->name);
429 return TDM_ERROR_BAD_MODULE;
432 return TDM_ERROR_NONE;
436 _tdm_display_update_caps_output(tdm_private_module *private_module, int pipe,
437 tdm_output *output_backend, tdm_caps_output *caps)
439 tdm_func_output *func_output = &private_module->func_output;
440 char temp[TDM_NAME_LEN];
444 stamp = tdm_helper_get_time();
445 ret = func_output->output_get_capability(output_backend, caps);
446 TDM_DBG("backend(%s) backend output_get_capability() time: %.3f ms",
447 private_module->module_data->name, (tdm_helper_get_time() - stamp) * 1000.0);
449 if (ret != TDM_ERROR_NONE) {
450 TDM_ERR("backend(%s) output_get_capability() failed", private_module->module_data->name);
451 return TDM_ERROR_BAD_MODULE;
454 /* FIXME: Use model for tdm client to distinguish amoung outputs */
455 snprintf(temp, TDM_NAME_LEN, "%s-%d", caps->model, pipe);
456 snprintf(caps->model, TDM_NAME_LEN, "%s", temp);
458 return TDM_ERROR_NONE;
462 _tdm_display_update_layer(tdm_private_output *private_output,
463 tdm_layer *layer_backend, int index)
465 tdm_private_layer *private_layer;
468 private_layer = _tdm_display_find_private_layer(private_output, layer_backend);
469 if (!private_layer) {
470 private_layer = calloc(1, sizeof(tdm_private_layer));
471 TDM_RETURN_VAL_IF_FAIL(private_layer != NULL, TDM_ERROR_OUT_OF_MEMORY);
473 LIST_ADDTAIL(&private_layer->link, &private_output->layer_list);
474 LIST_INITHEAD(&private_layer->capture_list);
476 private_layer->index = index;
477 private_layer->private_module = private_output->private_module;
478 private_layer->private_display = private_output->private_display;
479 private_layer->private_output = private_output;
480 private_layer->layer_backend = layer_backend;
481 private_layer->usable = 1;
484 _tdm_display_destroy_caps_layer(&private_layer->caps);
486 ret = _tdm_display_update_caps_layer(private_output->private_module, layer_backend, &private_layer->caps);
487 TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, ret);
489 return TDM_ERROR_NONE;
493 tdm_display_update_output(tdm_private_module *private_module, tdm_output *output_backend)
495 tdm_func_output *func_output = &private_module->func_output;
496 tdm_private_display *private_display = NULL;
497 tdm_private_output *private_output = NULL;
498 tdm_layer **layers = NULL;
499 tdm_private_hwc *private_hwc = NULL;
501 int layer_count = 0, i;
503 unsigned int output_created = 0;
505 private_display = private_module->private_display;
507 private_output = tdm_display_find_private_output(private_display, output_backend);
508 if (!private_output) {
509 unsigned int pipe = tdm_display_find_empty_output_pipe(private_display);
511 private_output = calloc(1, sizeof(tdm_private_output));
512 TDM_RETURN_VAL_IF_FAIL(private_output != NULL, TDM_ERROR_OUT_OF_MEMORY);
514 private_output->stamp = tdm_helper_get_time();
515 while (tdm_display_find_output_stamp(private_display, private_output->stamp))
516 private_output->stamp++;
518 LIST_ADDTAIL(&private_output->link, &private_module->output_list);
520 private_output->private_module = private_module;
521 private_output->private_display = private_display;
522 private_output->current_dpms_value = TDM_OUTPUT_DPMS_OFF;
523 private_output->output_backend = output_backend;
524 private_output->pipe = pipe;
525 private_output->index = pipe;
527 LIST_INITHEAD(&private_output->capture_list);
528 LIST_INITHEAD(&private_output->vblank_handler_list);
529 LIST_INITHEAD(&private_output->output_commit_handler_list);
530 LIST_INITHEAD(&private_output->pending_commit_handler_list);
531 LIST_INITHEAD(&private_output->destroy_handler_list);
532 LIST_INITHEAD(&private_output->change_handler_list);
534 if (func_output->output_set_status_handler) {
535 func_output->output_set_status_handler(private_output->output_backend,
536 tdm_output_cb_status,
538 private_output->regist_change_cb = 1;
543 /* NOTE that output modes will be allocated newly after here */
544 _tdm_display_destroy_caps_output(&private_output->caps);
545 ret = _tdm_display_update_caps_output(private_module, private_output->pipe, output_backend, &private_output->caps);
546 TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, ret);
548 if (private_output->caps.status == TDM_OUTPUT_CONN_STATUS_DISCONNECTED)
549 private_output->current_mode = NULL;
551 /* get the tdm_hwc object */
552 if (private_output->caps.capabilities&TDM_OUTPUT_CAPABILITY_HWC) {
553 hwc = func_output->output_get_hwc(output_backend, &ret);
554 TDM_RETURN_VAL_IF_FAIL(hwc != NULL, ret);
556 private_hwc = calloc(1, sizeof(tdm_private_hwc));
557 TDM_RETURN_VAL_IF_FAIL(private_hwc != NULL, TDM_ERROR_OUT_OF_MEMORY);
559 private_hwc->private_module = private_module;
560 private_hwc->private_output = private_output;
561 private_hwc->hwc_backend = hwc;
562 private_hwc->index = pipe;
563 private_hwc->stamp = tdm_helper_get_time();
564 while (tdm_display_find_hwc_stamp(private_module->private_display, private_hwc->stamp))
565 private_hwc->stamp++;
567 LIST_INITHEAD(&private_hwc->hwc_window_list);
568 LIST_INITHEAD(&private_hwc->hwc_commit_handler_list);
570 private_output->private_hwc = private_hwc;
572 LIST_INITHEAD(&private_output->layer_list);
573 LIST_INITHEAD(&private_output->layer_commit_handler_list);
576 _tdm_display_destroy_caps_output(&private_output->caps);
577 ret = _tdm_display_update_caps_output(private_module, private_output->pipe, output_backend, &private_output->caps);
578 TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, ret);
580 if (private_output->caps.status == TDM_OUTPUT_CONN_STATUS_DISCONNECTED)
581 private_output->current_mode = NULL;
584 /* do not use the layer object when the tdm_output has the hwc capability */
585 if ((private_output->caps.capabilities&TDM_OUTPUT_CAPABILITY_HWC) == 0) {
586 layers = func_output->output_get_layers(output_backend, &layer_count, &ret);
587 if (ret != TDM_ERROR_NONE) {
592 for (i = 0; i < layer_count; i++) {
593 ret = _tdm_display_update_layer(private_output, layers[i], i);
594 if (ret != TDM_ERROR_NONE) {
603 if (output_created) {
604 ret = tdm_display_call_thread_cb_output_create(private_display, private_output);
605 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
608 return TDM_ERROR_NONE;
612 _tdm_display_set_main_first(tdm_output **outputs, int index)
614 tdm_output *output_tmp = NULL;
619 output_tmp = outputs[0];
620 outputs[0] = outputs[index];
621 outputs[index] = output_tmp;
625 _tdm_display_get_ordered_outputs(tdm_private_module *private_module, int *count)
627 tdm_func_display *func_display = &private_module->func_display;
628 tdm_output **outputs = NULL;
629 tdm_output *output_dsi = NULL;
630 tdm_output *output_lvds = NULL;
631 tdm_output *output_hdmia = NULL;
632 tdm_output *output_hdmib = NULL;
633 int i, output_count = 0, output_connected_count = 0;
634 int index_dsi = 0, index_lvds = 0, index_hdmia = 0, index_hdmib = 0;
637 /* don't change list order if not init time */
638 if (private_module->outputs) {
639 TDM_ERR("can't change output order");
640 return private_module->outputs;
643 outputs = func_display->display_get_outputs(private_module->bdata, &output_count, &ret);
644 if (ret != TDM_ERROR_NONE)
647 *count = output_count;
649 if (output_count == 0)
651 else if (output_count == 1) {
652 private_module->outputs = outputs;
656 /* count connected outputs */
657 for (i = 0; i < output_count; i++) {
658 tdm_func_output *func_output = &private_module->func_output;
659 tdm_caps_output caps;
660 memset(&caps, 0, sizeof(tdm_caps_output));
662 ret = func_output->output_get_capability(outputs[i], &caps);
663 if (ret != TDM_ERROR_NONE) {
664 TDM_ERR("output_get_capability() failed");
668 if (caps.status == TDM_OUTPUT_CONN_STATUS_CONNECTED) {
669 output_connected_count++;
672 case TDM_OUTPUT_TYPE_DSI:
673 output_dsi = outputs[i];
676 case TDM_OUTPUT_TYPE_LVDS:
677 output_lvds = outputs[i];
680 case TDM_OUTPUT_TYPE_HDMIA:
681 output_hdmia = outputs[i];
684 case TDM_OUTPUT_TYPE_HDMIB:
685 output_hdmib = outputs[i];
693 _tdm_display_destroy_caps_output(&caps);
696 /* ordering : main output is first */
697 /* If there is no connected output, lvds or dsi cannot be main display. (cannot connect after booting)
698 * But hdmi is possible, so set hdmi to main display.
699 * If connected only one output, it is main output.
700 * If connected outputs over 2, has priority like below.
701 * (dsi > lvds > hdmi > else)
703 if (output_connected_count == 0) {
704 /* hdmi > dsi > lvds > else */
705 if (output_hdmia != NULL)
706 _tdm_display_set_main_first(outputs, index_hdmia);
707 else if (output_hdmib != NULL)
708 _tdm_display_set_main_first(outputs, index_hdmib);
709 else if (output_dsi != NULL)
710 _tdm_display_set_main_first(outputs, index_dsi);
711 else if (output_lvds != NULL)
712 _tdm_display_set_main_first(outputs, index_lvds);
713 } else { /* (output_connected_count > 1) */
714 /* dsi > lvds > hdmi > else */
715 if (output_dsi != NULL)
716 _tdm_display_set_main_first(outputs, index_dsi);
717 else if (output_lvds != NULL)
718 _tdm_display_set_main_first(outputs, index_lvds);
719 else if (output_hdmia != NULL)
720 _tdm_display_set_main_first(outputs, index_hdmia);
721 else if (output_hdmib != NULL)
722 _tdm_display_set_main_first(outputs, index_hdmib);
725 private_module->outputs = outputs;
736 _tdm_display_setup(tdm_private_display *private_display)
738 tdm_private_module *private_module = NULL;
739 tdm_error ret = TDM_ERROR_NONE;
740 int output_count = 0;
742 if (private_display->pp_module) {
743 ret = _tdm_display_update_caps_pp(private_display->pp_module,
744 &private_display->pp_module->caps_pp);
745 if (ret != TDM_ERROR_NONE)
749 if (private_display->capture_module) {
750 ret = _tdm_display_update_caps_capture(private_display->capture_module,
751 &private_display->capture_module->caps_capture);
752 if (ret != TDM_ERROR_NONE)
756 LIST_FOR_EACH_ENTRY(private_module, &private_display->module_list, link) {
757 tdm_output **outputs;
760 outputs = _tdm_display_get_ordered_outputs(private_module, &count);
763 TDM_GOTO_IF_FAIL(outputs != NULL, failed_update);
765 for (i = 0; i < count; i++) {
766 ret = tdm_display_update_output(private_module, outputs[i]);
767 if (ret != TDM_ERROR_NONE)
773 /* At least, the output count should be greater than 0 to ensure tdm_vblank works.
774 * So we will create a dummy output when backends don't have any output.
775 * Without destroying a tdm_output object, this dummy output will be replaced with
776 * a virtual output which is created in runtime.
778 if (output_count == 0) {
779 tdm_output **outputs;
782 TDM_INFO("loading a %s backend", TDM_DUMMY_MODULE);
783 ret = _tdm_display_load_module_with_file(private_display, TDM_DUMMY_MODULE);
784 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed_update);
785 TDM_GOTO_IF_FAIL(private_display->dummy_module != NULL, failed_update);
787 private_module = private_display->dummy_module;
789 outputs = _tdm_display_get_ordered_outputs(private_module, &count);
790 TDM_GOTO_IF_FAIL(count > 0, failed_update);
791 TDM_GOTO_IF_FAIL(outputs != NULL, failed_update);
793 for (i = 0; i < count; i++) {
794 ret = tdm_display_update_output(private_module, outputs[i]);
795 if (ret != TDM_ERROR_NONE)
800 TDM_INFO("loading a %s backend", TDM_VIRTUAL_MODULE);
801 ret = _tdm_display_load_module_with_file(private_display, TDM_VIRTUAL_MODULE);
802 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed_update);
803 TDM_GOTO_IF_FAIL(private_display->virtual_module != NULL, failed_update);
805 return TDM_ERROR_NONE;
808 _tdm_display_destroy_private_display(private_display);
814 tdm_display_update(tdm_display *dpy)
816 TDM_RETURN_VAL_IF_FAIL(dpy != NULL, TDM_ERROR_INVALID_PARAMETER);
818 TDM_DEPRECATED(NULL);
820 return TDM_ERROR_NONE;
823 int tdm_debug_module;
825 int tdm_ttrace_module;
826 int tdm_ttrace_output;
828 static tdm_private_display *g_private_display;
829 static pthread_mutex_t gLock = PTHREAD_MUTEX_INITIALIZER;
831 /* LCOV_EXCL_START */
833 _tdm_display_check_module(tdm_backend_module *module)
835 int tdm_backend_major, tdm_backend_minor;
836 int backend_module_major, backend_module_minor;
838 tdm_backend_major = TDM_BACKEND_GET_ABI_MAJOR(TDM_BACKEND_ABI_LATEST_VERSION);
839 tdm_backend_minor = TDM_BACKEND_GET_ABI_MINOR(TDM_BACKEND_ABI_LATEST_VERSION);
841 TDM_INFO("TDM Backend ABI version : %d.%d",
842 tdm_backend_major, tdm_backend_minor);
845 TDM_ERR("TDM backend doesn't have name");
846 return TDM_ERROR_BAD_MODULE;
849 if (!module->vendor) {
850 TDM_ERR("TDM backend doesn't have vendor");
851 return TDM_ERROR_BAD_MODULE;
854 backend_module_major = TDM_BACKEND_GET_ABI_MAJOR(module->abi_version);
855 backend_module_minor = TDM_BACKEND_GET_ABI_MINOR(module->abi_version);
857 TDM_INFO("TDM module name: %s", module->name);
858 TDM_INFO("'%s' vendor: %s", module->name, module->vendor);
859 TDM_INFO("'%s' backend ABI version: %d.%d", module->name, backend_module_major, backend_module_minor);
861 if (backend_module_major > tdm_backend_major) {
862 TDM_ERR("'%s' major version(%d) is newer than %d",
863 module->name, backend_module_major, tdm_backend_major);
864 return TDM_ERROR_BAD_MODULE;
867 if (tdm_backend_major == backend_module_major &&
868 backend_module_minor > tdm_backend_minor) {
869 TDM_ERR("'%s' minor version(%d) is newer than %d",
870 module->name, backend_module_minor, tdm_backend_minor);
871 return TDM_ERROR_BAD_MODULE;
875 TDM_ERR("'%s' doesn't have init function", module->name);
876 return TDM_ERROR_BAD_MODULE;
879 if (!module->deinit) {
880 TDM_ERR("'%s' doesn't have deinit function", module->name);
881 return TDM_ERROR_BAD_MODULE;
884 return TDM_ERROR_NONE;
888 _tdm_display_check_backend_functions(tdm_private_module *private_module)
890 tdm_func_display *func_display = &private_module->func_display;
891 tdm_func_output *func_output = &private_module->func_output;
892 tdm_func_layer *func_layer = &private_module->func_layer;
895 /* below functions should be implemented in backend side */
897 TDM_RETURN_VAL_IF_FAIL(func_display != NULL, TDM_ERROR_BAD_MODULE);
898 TDM_RETURN_VAL_IF_FAIL(func_display->display_get_capability, TDM_ERROR_BAD_MODULE);
899 TDM_RETURN_VAL_IF_FAIL(func_display->display_get_outputs, TDM_ERROR_BAD_MODULE);
900 TDM_RETURN_VAL_IF_FAIL(func_output->output_get_capability, TDM_ERROR_BAD_MODULE);
901 TDM_RETURN_VAL_IF_FAIL(func_output->output_get_layers, TDM_ERROR_BAD_MODULE);
902 TDM_RETURN_VAL_IF_FAIL(func_output->output_wait_vblank, TDM_ERROR_BAD_MODULE);
903 TDM_RETURN_VAL_IF_FAIL(func_output->output_set_vblank_handler, TDM_ERROR_BAD_MODULE);
904 TDM_RETURN_VAL_IF_FAIL(func_output->output_commit, TDM_ERROR_BAD_MODULE);
905 TDM_RETURN_VAL_IF_FAIL(func_output->output_set_commit_handler, TDM_ERROR_BAD_MODULE);
906 TDM_RETURN_VAL_IF_FAIL(func_output->output_set_mode, TDM_ERROR_BAD_MODULE);
907 TDM_RETURN_VAL_IF_FAIL(func_layer->layer_get_capability, TDM_ERROR_BAD_MODULE);
908 TDM_RETURN_VAL_IF_FAIL(func_layer->layer_set_info, TDM_ERROR_BAD_MODULE);
909 TDM_RETURN_VAL_IF_FAIL(func_layer->layer_get_info, TDM_ERROR_BAD_MODULE);
910 TDM_RETURN_VAL_IF_FAIL(func_layer->layer_set_buffer, TDM_ERROR_BAD_MODULE);
911 TDM_RETURN_VAL_IF_FAIL(func_layer->layer_unset_buffer, TDM_ERROR_BAD_MODULE);
913 ret = func_display->display_get_capability(private_module->bdata, &private_module->caps_display);
914 if (ret != TDM_ERROR_NONE) {
915 TDM_ERR("display_get_capability() failed");
916 return TDM_ERROR_BAD_MODULE;
919 if (private_module->capabilities & TDM_DISPLAY_CAPABILITY_PP) {
920 tdm_func_pp *func_pp = &private_module->func_pp;
921 TDM_RETURN_VAL_IF_FAIL(func_display->display_get_pp_capability, TDM_ERROR_BAD_MODULE);
922 TDM_RETURN_VAL_IF_FAIL(func_display->display_create_pp, TDM_ERROR_BAD_MODULE);
923 TDM_RETURN_VAL_IF_FAIL(func_pp->pp_destroy, TDM_ERROR_BAD_MODULE);
924 TDM_RETURN_VAL_IF_FAIL(func_pp->pp_commit, TDM_ERROR_BAD_MODULE);
925 TDM_RETURN_VAL_IF_FAIL(func_pp->pp_set_done_handler, TDM_ERROR_BAD_MODULE);
928 if (private_module->capabilities & TDM_DISPLAY_CAPABILITY_CAPTURE) {
929 tdm_func_capture *func_capture = &private_module->func_capture;
930 TDM_RETURN_VAL_IF_FAIL(func_display->display_get_capture_capability, TDM_ERROR_BAD_MODULE);
931 if (private_module->caps_capture.capabilities & TDM_CAPTURE_CAPABILITY_OUTPUT)
932 TDM_RETURN_VAL_IF_FAIL(func_output->output_create_capture, TDM_ERROR_BAD_MODULE);
933 if (private_module->caps_capture.capabilities & TDM_CAPTURE_CAPABILITY_LAYER)
934 TDM_RETURN_VAL_IF_FAIL(func_layer->layer_create_capture, TDM_ERROR_BAD_MODULE);
935 TDM_RETURN_VAL_IF_FAIL(func_capture->capture_destroy, TDM_ERROR_BAD_MODULE);
936 TDM_RETURN_VAL_IF_FAIL(func_capture->capture_commit, TDM_ERROR_BAD_MODULE);
937 TDM_RETURN_VAL_IF_FAIL(func_capture->capture_set_done_handler, TDM_ERROR_BAD_MODULE);
940 return TDM_ERROR_NONE;
944 _tdm_display_load_module_with_file(tdm_private_display *private_display,
947 char path[TDM_PATH_LEN] = {0,};
949 tdm_backend_module *module_data;
950 tdm_backend_data *bdata = NULL;
951 tdm_private_module *private_module = NULL;
956 size = snprintf(path, sizeof(path), TDM_MODULE_PATH "/%s", file);
957 if (size >= (int)sizeof(path)) {
958 TDM_WRN("too long: %s/%s", TDM_MODULE_PATH, file);
959 return TDM_ERROR_BAD_MODULE;
962 private_module = calloc(1, sizeof *private_module);
963 if (!private_module) {
964 TDM_ERR("alloc failed: %m");
965 ret = TDM_ERROR_OUT_OF_MEMORY;
969 private_module->private_display = private_display;
971 stamp = tdm_helper_get_time();
973 TDM_TRACE_BEGIN("TDM_Load_Backend");
974 module = dlopen(path, RTLD_LAZY);
976 TDM_ERR("%s", dlerror());
978 ret = TDM_ERROR_BAD_MODULE;
982 private_module->module = module;
984 module_data = dlsym(module, "tdm_backend_module_data");
986 TDM_ERR("'%s' doesn't have data object", file);
987 ret = TDM_ERROR_BAD_MODULE;
993 private_module->module_data = module_data;
995 TDM_DBG("dlopen, dlsym time: %.3f ms", (tdm_helper_get_time() - stamp) * 1000.0);
997 /* check if version, init() and deinit() are valid or not */
998 ret = _tdm_display_check_module(module_data);
999 if (ret != TDM_ERROR_NONE) {
1000 TDM_ERR("backend(%s) load failed: check module error", module_data->name);
1004 private_display->current_module = private_module;
1006 /* We don't care if backend_data is NULL or not. It's up to backend. */
1007 TDM_TRACE_BEGIN("TDM_Init_Backend");
1008 stamp = tdm_helper_get_time();
1009 bdata = module_data->init((tdm_display *)private_display, &ret);
1010 TDM_DBG("backend init() time: %.3f ms", (tdm_helper_get_time() - stamp) * 1000.0);
1013 private_display->current_module = NULL;
1015 if (!strncmp(file, TDM_DUMMY_MODULE, TDM_NAME_LEN))
1016 private_display->dummy_module = private_module;
1018 if (!strncmp(file, TDM_VIRTUAL_MODULE, TDM_NAME_LEN))
1019 private_display->virtual_module = private_module;
1021 private_module->bdata = bdata;
1023 if (ret != TDM_ERROR_NONE) {
1024 TDM_ERR("backend(%s) load failed: init error", module_data->name);
1028 ret = _tdm_display_check_backend_functions(private_module);
1029 if (ret != TDM_ERROR_NONE) {
1030 TDM_ERR("backend(%s) load failed: check functions error", module_data->name);
1034 LIST_INITHEAD(&private_module->output_list);
1035 LIST_INITHEAD(&private_module->pp_list);
1036 LIST_INITHEAD(&private_module->capture_list);
1038 LIST_ADDTAIL(&private_module->link, &private_display->module_list);
1040 TDM_INFO("Success to load '%s' module", module_data->name);
1042 return TDM_ERROR_NONE;
1045 module_data->deinit(bdata);
1049 free(private_module);
1054 _tdm_display_load_modules(tdm_private_display *private_display)
1056 const char *module_names;
1057 tdm_error ret = TDM_ERROR_NONE;
1058 char temp[TDM_PATH_LEN];
1062 LIST_INITHEAD(&private_display->module_list);
1064 module_names = tdm_config_get_string(TDM_CONFIG_KEY_GENERAL_BACKENDS, TDM_DEFAULT_MODULE);
1066 snprintf(temp, TDM_PATH_LEN, "%s", module_names);
1068 arg = strtok_r(temp, TDM_CONFIG_DELIM, &end);
1070 TDM_INFO("loading a %s backend", arg);
1071 ret = _tdm_display_load_module_with_file(private_display, arg);
1072 if (ret == TDM_ERROR_NONE)
1073 TDM_INFO("%s backend loading success", arg);
1075 TDM_INFO("%s backend loading failed", arg);
1076 arg = strtok_r(NULL, TDM_CONFIG_DELIM, &end);
1083 _tdm_display_unload_modules(tdm_private_display *private_display)
1085 tdm_private_module *private_module = NULL, *bb = NULL;
1087 LIST_FOR_EACH_ENTRY_SAFE(private_module, bb, &private_display->module_list, link) {
1088 LIST_DEL(&private_module->link);
1090 if (private_module->module_data)
1091 private_module->module_data->deinit(private_module->bdata);
1092 if (private_module->module)
1093 dlclose(private_module->module);
1095 free(private_module);
1098 /* LCOV_EXCL_STOP */
1101 tdm_display_find_stamp(tdm_private_display *private_display, double stamp)
1103 return (void*)g_private_display;
1106 EXTERN tdm_display *
1107 tdm_display_init(tdm_error *error)
1109 tdm_private_display *private_display = NULL;
1112 double stamp1, stamp2, start;
1115 pthread_mutex_lock(&gLock);
1117 if (g_private_display) {
1118 g_private_display->init_count++;
1119 pthread_mutex_unlock(&gLock);
1121 *error = TDM_ERROR_NONE;
1122 return g_private_display;
1125 start = stamp1 = tdm_helper_get_time();
1127 stamp2 = tdm_helper_get_time();
1128 TDM_INFO("config init time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1131 private_display = calloc(1, sizeof(tdm_private_display));
1132 if (!private_display) {
1133 /* LCOV_EXCL_START */
1134 ret = TDM_ERROR_OUT_OF_MEMORY;
1135 TDM_ERR("'private_display != NULL' failed");
1137 /* LCOV_EXCL_STOP */
1140 private_display->stamp = tdm_helper_get_time();
1142 LIST_INITHEAD(&private_display->output_create_handler_list);
1144 str = tdm_config_get_string(TDM_CONFIG_KEY_DEBUG_MODULE, NULL);
1146 tdm_display_enable_debug_module(str);
1148 str = tdm_config_get_string(TDM_CONFIG_KEY_DEBUG_DUMP, NULL);
1150 tdm_display_enable_dump(private_display, str, NULL, NULL);
1152 if (pthread_mutex_init(&private_display->lock, NULL)) {
1153 /* LCOV_EXCL_START */
1154 ret = TDM_ERROR_OPERATION_FAILED;
1155 TDM_ERR("mutex init failed: %m");
1156 goto failed_mutex_init;
1157 /* LCOV_EXCL_STOP */
1160 _pthread_mutex_lock(&private_display->lock);
1162 stamp2 = tdm_helper_get_time();
1163 TDM_DBG("prepare init time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1166 ret = tdm_event_loop_init(private_display);
1167 if (ret != TDM_ERROR_NONE)
1170 stamp2 = tdm_helper_get_time();
1171 TDM_INFO("event loop init time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1174 ret = tdm_vblank_init(private_display);
1175 if (ret != TDM_ERROR_NONE)
1178 ret = tdm_output_init(private_display);
1179 if (ret != TDM_ERROR_NONE)
1182 ret = tdm_hwc_init(private_display);
1183 if (ret != TDM_ERROR_NONE)
1186 ret = tdm_pp_init(private_display);
1187 if (ret != TDM_ERROR_NONE)
1190 ret = tdm_capture_init(private_display);
1191 if (ret != TDM_ERROR_NONE)
1194 ret = _tdm_display_load_modules(private_display);
1195 if (ret != TDM_ERROR_NONE)
1198 stamp2 = tdm_helper_get_time();
1199 TDM_INFO("loading backend time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1203 int tdm_drm_fd = tdm_helper_get_fd("TDM_DRM_MASTER_FD");
1204 if (tdm_drm_fd >= 0) {
1205 private_display->bufmgr = tbm_bufmgr_init(tdm_drm_fd);
1207 if (!private_display->bufmgr) {
1208 TDM_ERR("tbm_bufmgr_init failed");
1211 TDM_INFO("tbm_bufmgr_init successed");
1214 stamp2 = tdm_helper_get_time();
1215 TDM_DBG("initializing bufmgr time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1220 TDM_TRACE_BEGIN("TDM_Update_Display");
1221 ret = _tdm_display_setup(private_display);
1223 if (ret != TDM_ERROR_NONE)
1226 stamp2 = tdm_helper_get_time();
1227 TDM_DBG("updating display time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1230 tdm_event_loop_create_backend_source(private_display);
1232 private_display->init_count = 1;
1233 g_private_display = private_display;
1235 /* the COMMIT_PER_VBLANK functionality is ability of an output to support
1236 * several operational modes (commit_per_vblank modes) related to tdm_commit;
1237 * this functionality can be turned off which means a default mode */
1238 mode = tdm_config_get_int(TDM_CONFIG_KEY_GENERAL_COMMIT_PER_VBLANK, 0);
1240 tdm_private_module *b = NULL;
1241 tdm_private_output *o = NULL;
1243 /* outputs which support hwc capability can work only
1244 * if commit_per_vblank mode is '0' (default mode) */
1245 LIST_FOR_EACH_ENTRY(b, &private_display->module_list, link) {
1246 LIST_FOR_EACH_ENTRY(o, &b->output_list, link) {
1247 if (!(o->caps.capabilities & TDM_OUTPUT_CAPABILITY_HWC))
1248 tdm_output_choose_commit_per_vblank_mode(o, mode);
1254 *error = TDM_ERROR_NONE;
1256 tdm_thread_cb_set_find_func(TDM_THREAD_CB_DISPLAY_OUTPUT_CREATE, tdm_display_find_stamp);
1258 _pthread_mutex_unlock(&private_display->lock);
1259 pthread_mutex_unlock(&gLock);
1261 TDM_INFO("init time: %.3f ms", (tdm_helper_get_time() - start) * 1000.0);
1263 return (tdm_display *)private_display;
1265 /* LCOV_EXCL_START */
1267 _tdm_display_unload_modules(private_display);
1269 tdm_vblank_deinit(private_display);
1271 tdm_event_loop_stop(private_display);
1272 tdm_event_loop_deinit(private_display);
1274 _pthread_mutex_unlock(&private_display->lock);
1275 pthread_mutex_destroy(&private_display->lock);
1277 free(private_display);
1281 pthread_mutex_unlock(&gLock);
1283 /* LCOV_EXCL_STOP */
1287 tdm_display_deinit(tdm_display *dpy)
1289 tdm_private_display *private_display = dpy;
1291 if (!private_display)
1294 pthread_mutex_lock(&gLock);
1296 private_display->init_count--;
1297 if (private_display->init_count > 0) {
1298 pthread_mutex_unlock(&gLock);
1302 /* dont move the position of lock/unlock. all resource should be protected
1303 * during destroying. after tdm_event_loop_deinit, we don't worry about thread
1304 * things because it's finalized.
1306 _pthread_mutex_lock(&private_display->lock);
1307 tdm_event_loop_stop(private_display);
1308 _pthread_mutex_unlock(&private_display->lock);
1310 tdm_event_loop_deinit(private_display);
1312 /* when private_output is destroyed, all vblank resources of client and server
1313 * are destroyed. Then we can call tdm_vblank_deinit. After destroying display,
1314 * we can unload backend modulues.
1316 _tdm_display_destroy_private_display(private_display);
1317 _tdm_display_unload_modules(private_display);
1319 tdm_vblank_deinit(private_display);
1322 if (private_display->bufmgr)
1323 tbm_bufmgr_deinit(private_display->bufmgr);
1326 tbm_drm_helper_unset_tbm_master_fd();
1328 pthread_mutex_destroy(&private_display->lock);
1329 free(private_display);
1330 g_private_display = NULL;
1332 if (tdm_debug_dump_dir) {
1333 free(tdm_debug_dump_dir);
1334 tdm_debug_dump_dir = NULL;
1337 tdm_config_deinit();
1339 pthread_mutex_unlock(&gLock);
1344 INTERN tdm_private_display *
1345 tdm_display_get(void)
1347 return g_private_display;