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;
54 static tdm_private_layer *
55 _tdm_display_find_private_layer(tdm_private_output *private_output,
56 tdm_layer *layer_backend)
58 tdm_private_layer *private_layer = NULL;
60 LIST_FOR_EACH_ENTRY(private_layer, &private_output->layer_list, link) {
61 if (private_layer->layer_backend == layer_backend)
68 INTERN tdm_private_output *
69 tdm_display_find_private_output(tdm_private_display *private_display, tdm_output *output_backend)
71 tdm_private_module *private_module = NULL;
72 tdm_private_output *private_output = NULL;
74 LIST_FOR_EACH_ENTRY(private_module, &private_display->module_list, link) {
75 LIST_FOR_EACH_ENTRY(private_output, &private_module->output_list, link) {
76 if (private_output->output_backend == output_backend)
77 return private_output;
85 tdm_display_find_output_stamp(tdm_private_display *private_display, double stamp)
87 tdm_private_module *private_module = NULL;
88 tdm_private_output *private_output = NULL;
90 LIST_FOR_EACH_ENTRY(private_module, &private_display->module_list, link) {
91 LIST_FOR_EACH_ENTRY(private_output, &private_module->output_list, link) {
92 if (private_output->stamp == stamp)
93 return private_output;
101 _tdm_display_destroy_caps_pp(tdm_caps_pp *caps_pp)
103 if (caps_pp->formats)
104 free(caps_pp->formats);
106 memset(caps_pp, 0, sizeof(tdm_caps_pp));
110 _tdm_display_destroy_caps_capture(tdm_caps_capture *caps_capture)
112 if (caps_capture->formats)
113 free(caps_capture->formats);
115 memset(caps_capture, 0, sizeof(tdm_caps_capture));
119 _tdm_display_destroy_caps_layer(tdm_caps_layer *caps_layer)
121 if (caps_layer->formats)
122 free(caps_layer->formats);
124 if (caps_layer->props)
125 free(caps_layer->props);
127 memset(caps_layer, 0, sizeof(tdm_caps_layer));
131 _tdm_display_destroy_caps_output(tdm_caps_output *caps_output)
133 if (caps_output->modes)
134 free(caps_output->modes);
136 if (caps_output->props)
137 free(caps_output->props);
139 memset(caps_output, 0, sizeof(tdm_caps_output));
143 _tdm_display_destroy_private_layer(tdm_private_layer *private_layer)
145 tdm_private_capture *c = NULL, *cc = NULL;
147 tdm_layer_unset_buffer_internal(private_layer);
149 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &private_layer->capture_list, link)
150 tdm_capture_destroy_internal(c);
152 _tdm_display_destroy_caps_layer(&private_layer->caps);
154 /* when destroying layer, someone could check if layer is valid. So delete
155 * the layer's link at last.
157 LIST_DEL(&private_layer->link);
163 _tdm_display_destroy_private_output(tdm_private_output *private_output)
165 tdm_private_display *private_display = private_output->private_display;
166 tdm_private_layer *l = NULL, *ll = NULL;
167 tdm_private_hwc_window *hw = NULL, *hww = NULL;
168 tdm_private_capture *c = NULL, *cc = NULL;
169 tdm_private_output_vblank_handler *v = NULL, *vv = NULL;
170 tdm_private_output_commit_handler *om = NULL, *omm = NULL;
171 tdm_private_layer_commit_handler *lm = NULL, *lmm = NULL;
172 tdm_private_output_change_handler *h = NULL, *hh = NULL;
174 free(private_output->layers_ptr);
176 if (private_output->vblank_timeout_timer)
177 tdm_event_loop_source_remove(private_output->vblank_timeout_timer);
179 LIST_FOR_EACH_ENTRY_SAFE(v, vv, &private_output->vblank_handler_list, link) {
184 LIST_FOR_EACH_ENTRY_SAFE(om, omm, &private_output->output_commit_handler_list, link) {
189 LIST_FOR_EACH_ENTRY_SAFE(lm, lmm, &private_output->layer_commit_handler_list, link) {
194 LIST_FOR_EACH_ENTRY_SAFE(lm, lmm, &private_output->pending_commit_handler_list, link) {
199 LIST_FOR_EACH_ENTRY_SAFE(h, hh, &private_output->change_handler_list, link) {
201 tdm_thread_cb_remove(h->private_output, TDM_THREAD_CB_OUTPUT_DPMS, NULL, tdm_output_thread_cb_change, h);
202 tdm_thread_cb_remove(h->private_output, TDM_THREAD_CB_OUTPUT_STATUS, NULL, tdm_output_thread_cb_change, h);
206 if (private_output->vblank) {
207 /* tdm_vblank APIs is for server. it should be called in unlock status*/
208 _pthread_mutex_unlock(&private_display->lock);
209 tdm_vblank_destroy(private_output->vblank);
210 _pthread_mutex_lock(&private_display->lock);
213 if (private_output->ttrace_vblank) {
214 /* tdm_vblank APIs is for server. it should be called in unlock status*/
215 _pthread_mutex_unlock(&private_display->lock);
216 tdm_vblank_destroy(private_output->ttrace_vblank);
217 _pthread_mutex_lock(&private_display->lock);
220 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &private_output->capture_list, link)
221 tdm_capture_destroy_internal(c);
223 LIST_FOR_EACH_ENTRY_SAFE(hw, hww, &private_output->hwc_window_list, link)
224 tdm_hwc_window_destroy_internal(hw);
226 LIST_FOR_EACH_ENTRY_SAFE(l, ll, &private_output->layer_list, link)
227 _tdm_display_destroy_private_layer(l);
229 _tdm_display_destroy_caps_output(&private_output->caps);
231 tdm_thread_cb_remove(private_output, TDM_THREAD_CB_NEED_VALIDATE, NULL,
232 tdm_output_need_validate_handler_thread, NULL);
233 tdm_event_loop_source_remove(private_output->need_validate.event_source);
234 close(private_output->need_validate.event_fd);
236 /* when destroying output, vblank objects are also destroyed. vblank checks
237 * if output object is valid. So delete the output's link at last.
239 LIST_DEL(&private_output->link);
241 private_output->stamp = 0;
242 free(private_output);
246 _tdm_display_destroy_private_display(tdm_private_display *private_display)
248 tdm_private_module *private_module = NULL, *bb = NULL;
249 tdm_private_output *o = NULL, *oo = NULL;
250 tdm_private_pp *p = NULL, *pp = NULL;
252 LIST_FOR_EACH_ENTRY_SAFE(private_module, bb, &private_display->module_list, link) {
253 LIST_FOR_EACH_ENTRY_SAFE(p, pp, &private_module->pp_list, link) {
254 tdm_pp_destroy_internal(p);
257 LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_module->output_list, link) {
258 _tdm_display_destroy_private_output(o);
261 _tdm_display_destroy_caps_pp(&private_module->caps_pp);
262 _tdm_display_destroy_caps_capture(&private_module->caps_capture);
264 private_module->capabilities = 0;
265 private_module->caps_display.max_layer_count = 0;
267 if (private_module->outputs) {
268 free(private_module->outputs);
269 private_module->outputs = NULL;
275 _tdm_display_update_caps_pp(tdm_private_module *private_module, tdm_caps_pp *caps)
277 tdm_func_display *func_display;
280 func_display = &private_module->func_display;
282 if (!func_display->display_get_pp_capability) {
283 TDM_ERR("backend(%s) no display_get_pp_capability()", private_module->module_data->name);
284 return TDM_ERROR_BAD_MODULE;
287 ret = func_display->display_get_pp_capability(private_module->bdata, caps);
288 if (ret != TDM_ERROR_NONE) {
289 TDM_ERR("backend(%s) display_get_pp_capability() failed", private_module->module_data->name);
290 return TDM_ERROR_BAD_MODULE;
293 return TDM_ERROR_NONE;
297 _tdm_display_update_caps_capture(tdm_private_module *private_module, tdm_caps_capture *caps)
299 tdm_func_display *func_display;
302 func_display = &private_module->func_display;
304 if (!func_display->display_get_capture_capability) {
305 TDM_ERR("backend(%s) no display_get_capture_capability()", private_module->module_data->name);
306 return TDM_ERROR_BAD_MODULE;
309 ret = func_display->display_get_capture_capability(private_module->bdata, caps);
310 if (ret != TDM_ERROR_NONE) {
311 TDM_ERR("backend(%s) display_get_capture_capability() failed", private_module->module_data->name);
312 return TDM_ERROR_BAD_MODULE;
315 return TDM_ERROR_NONE;
319 _tdm_display_update_caps_layer(tdm_private_module *private_module,
320 tdm_layer *layer_backend, tdm_caps_layer *caps)
322 tdm_func_layer *func_layer = &private_module->func_layer;
325 if (!func_layer->layer_get_capability) {
326 TDM_ERR("backend(%s) no layer_get_capability()", private_module->module_data->name);
327 return TDM_ERROR_BAD_MODULE;
330 ret = func_layer->layer_get_capability(layer_backend, caps);
331 if (ret != TDM_ERROR_NONE) {
332 TDM_ERR("backend(%s) layer_get_capability() failed", private_module->module_data->name);
333 return TDM_ERROR_BAD_MODULE;
336 return TDM_ERROR_NONE;
340 _tdm_display_update_caps_output(tdm_private_module *private_module, int pipe,
341 tdm_output *output_backend, tdm_caps_output *caps)
343 tdm_func_output *func_output = &private_module->func_output;
344 char temp[TDM_NAME_LEN];
348 if (!func_output->output_get_capability) {
349 TDM_ERR("backend(%s) no output_get_capability()", private_module->module_data->name);
350 return TDM_ERROR_BAD_MODULE;
353 stamp = tdm_helper_get_time();
354 ret = func_output->output_get_capability(output_backend, caps);
355 TDM_DBG("backend(%s) backend output_get_capability() time: %.3f ms",
356 private_module->module_data->name, (tdm_helper_get_time() - stamp) * 1000.0);
358 if (ret != TDM_ERROR_NONE) {
359 TDM_ERR("backend(%s) output_get_capability() failed", private_module->module_data->name);
360 return TDM_ERROR_BAD_MODULE;
363 /* FIXME: Use model for tdm client to distinguish amoung outputs */
364 snprintf(temp, TDM_NAME_LEN, "%s-%d", caps->model, pipe);
365 snprintf(caps->model, TDM_NAME_LEN, "%s", temp);
367 return TDM_ERROR_NONE;
371 _tdm_display_update_layer(tdm_private_output *private_output,
372 tdm_layer *layer_backend, int index)
374 tdm_private_layer *private_layer;
377 private_layer = _tdm_display_find_private_layer(private_output, layer_backend);
378 if (!private_layer) {
379 private_layer = calloc(1, sizeof(tdm_private_layer));
380 TDM_RETURN_VAL_IF_FAIL(private_layer != NULL, TDM_ERROR_OUT_OF_MEMORY);
382 LIST_ADDTAIL(&private_layer->link, &private_output->layer_list);
383 LIST_INITHEAD(&private_layer->capture_list);
385 private_layer->index = index;
386 private_layer->private_module = private_output->private_module;
387 private_layer->private_display = private_output->private_display;
388 private_layer->private_output = private_output;
389 private_layer->layer_backend = layer_backend;
390 private_layer->usable = 1;
393 _tdm_display_destroy_caps_layer(&private_layer->caps);
395 ret = _tdm_display_update_caps_layer(private_output->private_module, layer_backend, &private_layer->caps);
396 TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, ret);
398 return TDM_ERROR_NONE;
402 tdm_display_update_output(tdm_private_module *private_module,
403 tdm_output *output_backend, int pipe, unsigned int need_new_caps)
405 tdm_func_output *func_output = &private_module->func_output;
406 tdm_private_output *private_output = NULL;
407 tdm_layer **layers = NULL;
408 int layer_count = 0, i;
411 private_output = tdm_display_find_private_output(private_module->private_display, output_backend);
412 if (!private_output) {
413 private_output = calloc(1, sizeof(tdm_private_output));
414 TDM_RETURN_VAL_IF_FAIL(private_output != NULL, TDM_ERROR_OUT_OF_MEMORY);
416 private_output->stamp = tdm_helper_get_time();
417 while (tdm_display_find_output_stamp(private_module->private_display, private_output->stamp))
418 private_output->stamp++;
420 LIST_ADDTAIL(&private_output->link, &private_module->output_list);
422 private_output->private_module = private_module;
423 private_output->private_display = private_module->private_display;
424 private_output->current_dpms_value = TDM_OUTPUT_DPMS_OFF;
425 private_output->output_backend = output_backend;
426 private_output->pipe = pipe;
427 private_output->index = pipe;
429 LIST_INITHEAD(&private_output->layer_list);
430 LIST_INITHEAD(&private_output->hwc_window_list);
431 LIST_INITHEAD(&private_output->capture_list);
432 LIST_INITHEAD(&private_output->vblank_handler_list);
433 LIST_INITHEAD(&private_output->output_commit_handler_list);
434 LIST_INITHEAD(&private_output->layer_commit_handler_list);
435 LIST_INITHEAD(&private_output->pending_commit_handler_list);
436 LIST_INITHEAD(&private_output->change_handler_list);
438 private_output->need_validate.event_fd = -1;
440 if (func_output->output_set_status_handler) {
441 func_output->output_set_status_handler(private_output->output_backend,
442 tdm_output_cb_status,
444 private_output->regist_change_cb = 1;
448 /* need_new_caps will be true only in case of "disconnected -> connected" and "connected -> disconnected"
449 * because we have to get new modes.
452 _tdm_display_destroy_caps_output(&private_output->caps);
454 ret = _tdm_display_update_caps_output(private_module, pipe, output_backend, &private_output->caps);
455 TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, ret);
458 layers = func_output->output_get_layers(output_backend, &layer_count, &ret);
459 if (ret != TDM_ERROR_NONE) {
464 for (i = 0; i < layer_count; i++) {
465 ret = _tdm_display_update_layer(private_output, layers[i], i);
466 if (ret != TDM_ERROR_NONE) {
474 return TDM_ERROR_NONE;
478 _tdm_display_set_main_first(tdm_output **outputs, int index)
480 tdm_output *output_tmp = NULL;
485 output_tmp = outputs[0];
486 outputs[0] = outputs[index];
487 outputs[index] = output_tmp;
493 _tdm_display_get_ordered_outputs(tdm_private_module *private_module, int *count)
495 tdm_func_display *func_display = &private_module->func_display;
496 tdm_output **outputs = NULL;
497 tdm_output **new_outputs = NULL;
498 tdm_output *output_dsi = NULL;
499 tdm_output *output_lvds = NULL;
500 tdm_output *output_hdmia = NULL;
501 tdm_output *output_hdmib = NULL;
502 int i, output_count = 0, output_connected_count = 0;
503 int index_dsi = 0, index_lvds = 0, index_hdmia = 0, index_hdmib = 0;
506 /* don't change list order if not init time */
507 if (private_module->outputs) {
508 TDM_ERR("can't change output order");
509 return private_module->outputs;
512 outputs = func_display->display_get_outputs(private_module->bdata, &output_count, &ret);
513 if (ret != TDM_ERROR_NONE)
514 goto failed_get_outputs;
516 *count = output_count;
518 if (output_count == 0)
519 goto failed_get_outputs;
520 else if (output_count == 1) {
521 private_module->outputs = outputs;
525 /* count connected outputs */
526 for (i = 0; i < output_count; i++) {
527 tdm_func_output *func_output = &private_module->func_output;
528 tdm_caps_output caps;
529 memset(&caps, 0, sizeof(tdm_caps_output));
531 if (!func_output->output_get_capability) {
532 TDM_ERR("no output_get_capability()");
533 goto failed_get_outputs;
536 ret = func_output->output_get_capability(outputs[i], &caps);
537 if (ret != TDM_ERROR_NONE) {
538 TDM_ERR("output_get_capability() failed");
539 goto failed_get_outputs;
542 if (caps.status == TDM_OUTPUT_CONN_STATUS_CONNECTED) {
543 output_connected_count++;
546 case TDM_OUTPUT_TYPE_DSI:
547 output_dsi = outputs[i];
550 case TDM_OUTPUT_TYPE_LVDS:
551 output_lvds = outputs[i];
554 case TDM_OUTPUT_TYPE_HDMIA:
555 output_hdmia = outputs[i];
558 case TDM_OUTPUT_TYPE_HDMIB:
559 output_hdmib = outputs[i];
567 _tdm_display_destroy_caps_output(&caps);
570 /* ordering : main output is first */
571 /* If there is no connected output, lvds or dsi cannot be main display. (cannot connect after booting)
572 * But hdmi is possible, so set hdmi to main display.
573 * If connected only one output, it is main output.
574 * If connected outputs over 2, has priority like below.
575 * (dsi > lvds > hdmi > else)
577 if (output_connected_count == 0) {
578 /* hdmi > dsi > lvds > else */
579 if (output_hdmia != NULL)
580 new_outputs = _tdm_display_set_main_first(outputs, index_hdmia);
581 else if (output_hdmib != NULL)
582 new_outputs = _tdm_display_set_main_first(outputs, index_hdmib);
583 else if (output_dsi != NULL)
584 new_outputs = _tdm_display_set_main_first(outputs, index_dsi);
585 else if (output_lvds != NULL)
586 new_outputs = _tdm_display_set_main_first(outputs, index_lvds);
588 new_outputs = outputs;
589 } else { /* (output_connected_count > 1) */
590 /* dsi > lvds > hdmi > else */
591 if (output_dsi != NULL)
592 new_outputs = _tdm_display_set_main_first(outputs, index_dsi);
593 else if (output_lvds != NULL)
594 new_outputs = _tdm_display_set_main_first(outputs, index_lvds);
595 else if (output_hdmia != NULL)
596 new_outputs = _tdm_display_set_main_first(outputs, index_hdmia);
597 else if (output_hdmib != NULL)
598 new_outputs = _tdm_display_set_main_first(outputs, index_hdmib);
600 new_outputs = outputs;
603 private_module->outputs = new_outputs;
614 _tdm_display_setup(tdm_private_display *private_display)
616 tdm_private_module *private_module = NULL;
617 tdm_error ret = TDM_ERROR_NONE;
620 if (private_display->pp_module) {
621 ret = _tdm_display_update_caps_pp(private_display->pp_module,
622 &private_display->pp_module->caps_pp);
623 if (ret != TDM_ERROR_NONE)
627 if (private_display->capture_module) {
628 ret = _tdm_display_update_caps_capture(private_display->capture_module,
629 &private_display->capture_module->caps_capture);
630 if (ret != TDM_ERROR_NONE)
634 LIST_FOR_EACH_ENTRY(private_module, &private_display->module_list, link) {
635 tdm_output **outputs;
636 int output_count = 0, i;
638 outputs = _tdm_display_get_ordered_outputs(private_module, &output_count);
642 for (i = 0; i < output_count; i++) {
643 ret = tdm_display_update_output(private_module, outputs[i], index++, 1);
644 if (ret != TDM_ERROR_NONE)
649 return TDM_ERROR_NONE;
652 _tdm_display_destroy_private_display(private_display);
658 tdm_display_update(tdm_display *dpy)
660 TDM_RETURN_VAL_IF_FAIL(dpy != NULL, TDM_ERROR_INVALID_PARAMETER);
662 TDM_DEPRECATED(NULL);
664 return TDM_ERROR_NONE;
667 int tdm_debug_module;
669 int tdm_ttrace_module;
670 int tdm_ttrace_output;
672 static tdm_private_display *g_private_display;
673 static pthread_mutex_t gLock = PTHREAD_MUTEX_INITIALIZER;
675 /* LCOV_EXCL_START */
677 _tdm_display_check_module(tdm_backend_module *module)
681 TDM_INFO("TDM ABI version : %d.%d",
682 TDM_MAJOR_VERSION, TDM_MINOR_VERSION);
685 TDM_ERR("TDM backend doesn't have name");
686 return TDM_ERROR_BAD_MODULE;
689 if (!module->vendor) {
690 TDM_ERR("TDM backend doesn't have vendor");
691 return TDM_ERROR_BAD_MODULE;
694 major = TDM_BACKEND_GET_ABI_MAJOR(module->abi_version);
695 minor = TDM_BACKEND_GET_ABI_MINOR(module->abi_version);
697 TDM_INFO("TDM module name: %s", module->name);
698 TDM_INFO("'%s' vendor: %s", module->name, module->vendor);
699 TDM_INFO("'%s' version: %d.%d", module->name, major, minor);
701 if (major != TDM_MAJOR_VERSION) {
702 TDM_ERR("'%s' major version mismatch, %d != %d",
703 module->name, major, TDM_MAJOR_VERSION);
704 return TDM_ERROR_BAD_MODULE;
707 if (minor > TDM_MINOR_VERSION) {
708 TDM_ERR("'%s' minor version(%d) is newer than %d",
709 module->name, minor, TDM_MINOR_VERSION);
710 return TDM_ERROR_BAD_MODULE;
714 TDM_ERR("'%s' doesn't have init function", module->name);
715 return TDM_ERROR_BAD_MODULE;
718 if (!module->deinit) {
719 TDM_ERR("'%s' doesn't have deinit function", module->name);
720 return TDM_ERROR_BAD_MODULE;
723 return TDM_ERROR_NONE;
727 _tdm_display_check_backend_functions(tdm_private_module *private_module)
729 tdm_func_display *func_display = &private_module->func_display;
730 tdm_func_output *func_output = &private_module->func_output;
731 tdm_func_layer *func_layer = &private_module->func_layer;
734 /* below functions should be implemented in backend side */
736 TDM_RETURN_VAL_IF_FAIL(func_display != NULL, TDM_ERROR_BAD_MODULE);
737 TDM_RETURN_VAL_IF_FAIL(func_display->display_get_capability, TDM_ERROR_BAD_MODULE);
738 TDM_RETURN_VAL_IF_FAIL(func_display->display_get_outputs, TDM_ERROR_BAD_MODULE);
739 TDM_RETURN_VAL_IF_FAIL(func_output->output_get_capability, TDM_ERROR_BAD_MODULE);
740 TDM_RETURN_VAL_IF_FAIL(func_output->output_get_layers, TDM_ERROR_BAD_MODULE);
741 TDM_RETURN_VAL_IF_FAIL(func_output->output_wait_vblank, TDM_ERROR_BAD_MODULE);
742 TDM_RETURN_VAL_IF_FAIL(func_output->output_set_vblank_handler, TDM_ERROR_BAD_MODULE);
743 TDM_RETURN_VAL_IF_FAIL(func_output->output_commit, TDM_ERROR_BAD_MODULE);
744 TDM_RETURN_VAL_IF_FAIL(func_output->output_set_commit_handler, TDM_ERROR_BAD_MODULE);
745 TDM_RETURN_VAL_IF_FAIL(func_output->output_set_mode, TDM_ERROR_BAD_MODULE);
746 TDM_RETURN_VAL_IF_FAIL(func_layer->layer_get_capability, TDM_ERROR_BAD_MODULE);
747 TDM_RETURN_VAL_IF_FAIL(func_layer->layer_set_info, TDM_ERROR_BAD_MODULE);
748 TDM_RETURN_VAL_IF_FAIL(func_layer->layer_get_info, TDM_ERROR_BAD_MODULE);
749 TDM_RETURN_VAL_IF_FAIL(func_layer->layer_set_buffer, TDM_ERROR_BAD_MODULE);
750 TDM_RETURN_VAL_IF_FAIL(func_layer->layer_unset_buffer, TDM_ERROR_BAD_MODULE);
752 ret = func_display->display_get_capability(private_module->bdata, &private_module->caps_display);
753 if (ret != TDM_ERROR_NONE) {
754 TDM_ERR("display_get_capability() failed");
755 return TDM_ERROR_BAD_MODULE;
758 if (private_module->capabilities & TDM_DISPLAY_CAPABILITY_PP) {
759 tdm_func_pp *func_pp = &private_module->func_pp;
760 TDM_RETURN_VAL_IF_FAIL(func_display->display_get_pp_capability, TDM_ERROR_BAD_MODULE);
761 TDM_RETURN_VAL_IF_FAIL(func_display->display_create_pp, TDM_ERROR_BAD_MODULE);
762 TDM_RETURN_VAL_IF_FAIL(func_pp->pp_destroy, TDM_ERROR_BAD_MODULE);
763 TDM_RETURN_VAL_IF_FAIL(func_pp->pp_commit, TDM_ERROR_BAD_MODULE);
764 TDM_RETURN_VAL_IF_FAIL(func_pp->pp_set_done_handler, TDM_ERROR_BAD_MODULE);
767 if (private_module->capabilities & TDM_DISPLAY_CAPABILITY_CAPTURE) {
768 tdm_func_capture *func_capture = &private_module->func_capture;
769 TDM_RETURN_VAL_IF_FAIL(func_display->display_get_capture_capability, TDM_ERROR_BAD_MODULE);
770 if (private_module->caps_capture.capabilities & TDM_CAPTURE_CAPABILITY_OUTPUT)
771 TDM_RETURN_VAL_IF_FAIL(func_output->output_create_capture, TDM_ERROR_BAD_MODULE);
772 if (private_module->caps_capture.capabilities & TDM_CAPTURE_CAPABILITY_LAYER)
773 TDM_RETURN_VAL_IF_FAIL(func_layer->layer_create_capture, TDM_ERROR_BAD_MODULE);
774 TDM_RETURN_VAL_IF_FAIL(func_capture->capture_destroy, TDM_ERROR_BAD_MODULE);
775 TDM_RETURN_VAL_IF_FAIL(func_capture->capture_commit, TDM_ERROR_BAD_MODULE);
776 TDM_RETURN_VAL_IF_FAIL(func_capture->capture_set_done_handler, TDM_ERROR_BAD_MODULE);
779 return TDM_ERROR_NONE;
783 _tdm_display_load_module_with_file(tdm_private_display *private_display,
786 char path[TDM_PATH_LEN] = {0,};
788 tdm_backend_module *module_data;
789 tdm_backend_data *bdata = NULL;
790 tdm_private_module *private_module = NULL;
795 size = snprintf(path, sizeof(path), TDM_MODULE_PATH "/%s", file);
796 if (size >= (int)sizeof(path)) {
797 TDM_WRN("too long: %s/%s", TDM_MODULE_PATH, file);
798 return TDM_ERROR_BAD_MODULE;
801 private_module = calloc(1, sizeof *private_module);
802 if (!private_module) {
803 TDM_ERR("alloc failed: %m");
804 ret = TDM_ERROR_OUT_OF_MEMORY;
808 private_module->private_display = private_display;
810 stamp = tdm_helper_get_time();
812 TDM_TRACE_BEGIN("TDM_Load_Backend");
813 module = dlopen(path, RTLD_LAZY);
815 TDM_ERR("%s", dlerror());
817 ret = TDM_ERROR_BAD_MODULE;
821 private_module->module = module;
823 module_data = dlsym(module, "tdm_backend_module_data");
825 TDM_ERR("'%s' doesn't have data object", file);
826 ret = TDM_ERROR_BAD_MODULE;
832 private_module->module_data = module_data;
834 TDM_DBG("dlopen, dlsym time: %.3f ms", (tdm_helper_get_time() - stamp) * 1000.0);
836 /* check if version, init() and deinit() are valid or not */
837 ret = _tdm_display_check_module(module_data);
838 if (ret != TDM_ERROR_NONE) {
839 TDM_ERR("backend(%s) load failed: check module error", module_data->name);
843 private_display->current_module = private_module;
845 /* We don't care if backend_data is NULL or not. It's up to backend. */
846 TDM_TRACE_BEGIN("TDM_Init_Backend");
847 stamp = tdm_helper_get_time();
848 bdata = module_data->init((tdm_display *)private_display, &ret);
849 TDM_DBG("backend init() time: %.3f ms", (tdm_helper_get_time() - stamp) * 1000.0);
852 private_display->current_module = NULL;
854 private_module->bdata = bdata;
856 if (ret != TDM_ERROR_NONE) {
857 TDM_ERR("backend(%s) load failed: init error", module_data->name);
861 ret = _tdm_display_check_backend_functions(private_module);
862 if (ret != TDM_ERROR_NONE) {
863 TDM_ERR("backend(%s) load failed: check functions error", module_data->name);
867 LIST_INITHEAD(&private_module->output_list);
868 LIST_INITHEAD(&private_module->pp_list);
869 LIST_INITHEAD(&private_module->capture_list);
871 LIST_ADDTAIL(&private_module->link, &private_display->module_list);
873 TDM_INFO("Success to load '%s' module", module_data->name);
875 return TDM_ERROR_NONE;
878 module_data->deinit(bdata);
882 free(private_module);
887 _tdm_display_load_modules(tdm_private_display *private_display)
889 const char *module_names;
891 char temp[TDM_PATH_LEN];
895 LIST_INITHEAD(&private_display->module_list);
897 module_names = tdm_config_get_string(TDM_CONFIG_KEY_GENERAL_BACKENDS, TDM_DEFAULT_MODULE);
899 snprintf(temp, TDM_PATH_LEN, "%s", module_names);
901 arg = strtok_r(temp, TDM_CONFIG_DELIM, &end);
903 TDM_INFO("loading a %s backend", arg);
904 ret = _tdm_display_load_module_with_file(private_display, arg);
905 if (ret == TDM_ERROR_NONE)
906 TDM_INFO("%s backend loading success", arg);
908 TDM_INFO("%s backend loading failed", arg);
909 arg = strtok_r(NULL, TDM_CONFIG_DELIM, &end);
912 /* load bufmgr priv from dummy lib */
913 if (LIST_IS_EMPTY(&private_display->module_list)) {
914 TDM_WRN("No backend. loading a %s backend", TDM_DUMMY_MODULE);
915 ret = _tdm_display_load_module_with_file(private_display, TDM_DUMMY_MODULE);
916 if (ret == TDM_ERROR_NONE)
917 TDM_INFO("%s backend loading success", TDM_DUMMY_MODULE);
919 TDM_INFO("%s backend loading failed", TDM_DUMMY_MODULE);
926 _tdm_display_unload_modules(tdm_private_display *private_display)
928 tdm_private_module *private_module = NULL, *bb = NULL;
930 LIST_FOR_EACH_ENTRY_SAFE(private_module, bb, &private_display->module_list, link) {
931 LIST_DEL(&private_module->link);
933 if (private_module->module_data)
934 private_module->module_data->deinit(private_module->bdata);
935 if (private_module->module)
936 dlclose(private_module->module);
938 free(private_module);
944 tdm_display_init(tdm_error *error)
946 tdm_private_display *private_display = NULL;
949 double stamp1, stamp2, start;
952 pthread_mutex_lock(&gLock);
954 if (g_private_display) {
955 g_private_display->init_count++;
956 pthread_mutex_unlock(&gLock);
958 *error = TDM_ERROR_NONE;
959 return g_private_display;
962 start = stamp1 = tdm_helper_get_time();
964 stamp2 = tdm_helper_get_time();
965 TDM_INFO("config init time: %.3f ms", (stamp2 - stamp1) * 1000.0);
968 private_display = calloc(1, sizeof(tdm_private_display));
969 if (!private_display) {
970 /* LCOV_EXCL_START */
971 ret = TDM_ERROR_OUT_OF_MEMORY;
972 TDM_ERR("'private_display != NULL' failed");
977 private_display->stamp = tdm_helper_get_time();
979 str = tdm_config_get_string(TDM_CONFIG_KEY_DEBUG_MODULE, NULL);
981 tdm_display_enable_debug_module(str);
983 str = tdm_config_get_string(TDM_CONFIG_KEY_DEBUG_DUMP, NULL);
985 tdm_display_enable_dump(private_display, str, NULL, NULL);
987 if (pthread_mutex_init(&private_display->lock, NULL)) {
988 /* LCOV_EXCL_START */
989 ret = TDM_ERROR_OPERATION_FAILED;
990 TDM_ERR("mutex init failed: %m");
991 goto failed_mutex_init;
995 _pthread_mutex_lock(&private_display->lock);
997 stamp2 = tdm_helper_get_time();
998 TDM_DBG("prepare init time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1001 ret = tdm_event_loop_init(private_display);
1002 if (ret != TDM_ERROR_NONE)
1005 stamp2 = tdm_helper_get_time();
1006 TDM_INFO("event loop init time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1009 ret = tdm_vblank_init(private_display);
1010 if (ret != TDM_ERROR_NONE)
1013 ret = tdm_output_init(private_display);
1014 if (ret != TDM_ERROR_NONE)
1017 ret = tdm_pp_init(private_display);
1018 if (ret != TDM_ERROR_NONE)
1021 ret = tdm_capture_init(private_display);
1022 if (ret != TDM_ERROR_NONE)
1025 ret = _tdm_display_load_modules(private_display);
1026 if (ret != TDM_ERROR_NONE)
1029 stamp2 = tdm_helper_get_time();
1030 TDM_INFO("loading backend time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1034 int tdm_drm_fd = tdm_helper_get_fd("TDM_DRM_MASTER_FD");
1035 if (tdm_drm_fd >= 0) {
1036 private_display->bufmgr = tbm_bufmgr_init(tdm_drm_fd);
1038 if (!private_display->bufmgr) {
1039 TDM_ERR("tbm_bufmgr_init failed");
1042 TDM_INFO("tbm_bufmgr_init successed");
1045 stamp2 = tdm_helper_get_time();
1046 TDM_DBG("initializing bufmgr time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1051 TDM_TRACE_BEGIN("TDM_Update_Display");
1052 ret = _tdm_display_setup(private_display);
1054 if (ret != TDM_ERROR_NONE)
1057 stamp2 = tdm_helper_get_time();
1058 TDM_DBG("updating display time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1061 tdm_event_loop_create_backend_source(private_display);
1063 private_display->init_count = 1;
1064 g_private_display = private_display;
1066 tdm_private_module *b = NULL;
1067 tdm_private_output *o = NULL;
1068 LIST_FOR_EACH_ENTRY(b, &private_display->module_list, link) {
1069 LIST_FOR_EACH_ENTRY(o, &b->output_list, link) {
1070 if (o->caps.capabilities & TDM_OUTPUT_CAPABILITY_HWC)
1071 tdm_output_need_validate_event_init(o);
1075 /* the COMMIT_PER_VBLANK functionality is ability of an output to support
1076 * several operational modes (commit_per_vblank modes) related to tdm_commit;
1077 * this functionality can be turned off which means a default mode */
1078 mode = tdm_config_get_int(TDM_CONFIG_KEY_GENERAL_COMMIT_PER_VBLANK, 0);
1080 tdm_private_module *b = NULL;
1081 tdm_private_output *o = NULL;
1083 /* outputs which support hwc capability can work only
1084 * if commit_per_vblank mode is '0' (default mode) */
1085 LIST_FOR_EACH_ENTRY(b, &private_display->module_list, link) {
1086 LIST_FOR_EACH_ENTRY(o, &b->output_list, link) {
1087 if (!(o->caps.capabilities & TDM_OUTPUT_CAPABILITY_HWC))
1088 tdm_output_choose_commit_per_vblank_mode(o, mode);
1094 *error = TDM_ERROR_NONE;
1096 _pthread_mutex_unlock(&private_display->lock);
1097 pthread_mutex_unlock(&gLock);
1099 TDM_INFO("init time: %.3f ms", (tdm_helper_get_time() - start) * 1000.0);
1101 return (tdm_display *)private_display;
1103 /* LCOV_EXCL_START */
1105 _tdm_display_unload_modules(private_display);
1107 tdm_vblank_deinit(private_display);
1109 tdm_event_loop_stop(private_display);
1110 tdm_event_loop_deinit(private_display);
1112 _pthread_mutex_unlock(&private_display->lock);
1113 pthread_mutex_destroy(&private_display->lock);
1115 free(private_display);
1119 pthread_mutex_unlock(&gLock);
1121 /* LCOV_EXCL_STOP */
1125 tdm_display_deinit(tdm_display *dpy)
1127 tdm_private_display *private_display = dpy;
1129 if (!private_display)
1132 pthread_mutex_lock(&gLock);
1134 private_display->init_count--;
1135 if (private_display->init_count > 0) {
1136 pthread_mutex_unlock(&gLock);
1140 /* dont move the position of lock/unlock. all resource should be protected
1141 * during destroying. after tdm_event_loop_deinit, we don't worry about thread
1142 * things because it's finalized.
1144 _pthread_mutex_lock(&private_display->lock);
1145 tdm_event_loop_stop(private_display);
1146 _pthread_mutex_unlock(&private_display->lock);
1148 tdm_event_loop_deinit(private_display);
1150 /* when private_output is destroyed, all vblank resources of client and server
1151 * are destroyed. Then we can call tdm_vblank_deinit. After destroying display,
1152 * we can unload backend modulues.
1154 _tdm_display_destroy_private_display(private_display);
1155 _tdm_display_unload_modules(private_display);
1157 tdm_vblank_deinit(private_display);
1160 if (private_display->bufmgr)
1161 tbm_bufmgr_deinit(private_display->bufmgr);
1164 tbm_drm_helper_unset_tbm_master_fd();
1166 pthread_mutex_destroy(&private_display->lock);
1167 free(private_display);
1168 g_private_display = NULL;
1170 if (tdm_debug_dump_dir) {
1171 free(tdm_debug_dump_dir);
1172 tdm_debug_dump_dir = NULL;
1175 tdm_config_deinit();
1177 pthread_mutex_unlock(&gLock);
1182 INTERN tdm_private_display *
1183 tdm_display_get(void)
1185 return g_private_display;