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 <sc1.lim@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 **************************************************************************/
41 #include "tdm_backend.h"
42 #include "tdm_private.h"
43 #include "tdm_helper.h"
45 pthread_mutex_t tdm_mutex_check_lock = PTHREAD_MUTEX_INITIALIZER;
47 const char *tdm_mutex_lock_func;
48 int tdm_mutex_lock_line;
49 const char *tdm_mutex_unlock_func;
50 int tdm_mutex_unlock_line;
52 static tdm_private_layer *
53 _tdm_display_find_private_layer(tdm_private_output *private_output,
54 tdm_layer *layer_backend)
56 tdm_private_layer *private_layer = NULL;
58 LIST_FOR_EACH_ENTRY(private_layer, &private_output->layer_list, link) {
59 if (private_layer->layer_backend == layer_backend)
66 static tdm_private_output *
67 _tdm_display_find_private_output(tdm_private_display *private_display,
68 tdm_output *output_backend)
70 tdm_private_output *private_output = NULL;
72 LIST_FOR_EACH_ENTRY(private_output, &private_display->output_list, link) {
73 if (private_output->output_backend == output_backend)
74 return private_output;
80 INTERN tdm_private_output *
81 tdm_display_find_output_stamp(tdm_private_display *private_display, double stamp)
83 tdm_private_output *private_output = NULL;
85 LIST_FOR_EACH_ENTRY(private_output, &private_display->output_list, link) {
86 if (private_output->stamp == stamp)
87 return private_output;
94 _tdm_display_destroy_caps_pp(tdm_caps_pp *caps_pp)
97 free(caps_pp->formats);
99 memset(caps_pp, 0, sizeof(tdm_caps_pp));
103 _tdm_display_destroy_caps_capture(tdm_caps_capture *caps_capture)
105 if (caps_capture->formats)
106 free(caps_capture->formats);
108 memset(caps_capture, 0, sizeof(tdm_caps_capture));
112 _tdm_display_destroy_caps_layer(tdm_caps_layer *caps_layer)
114 if (caps_layer->formats)
115 free(caps_layer->formats);
117 if (caps_layer->props)
118 free(caps_layer->props);
120 memset(caps_layer, 0, sizeof(tdm_caps_layer));
124 _tdm_display_destroy_caps_output(tdm_caps_output *caps_output)
126 if (caps_output->modes)
127 free(caps_output->modes);
129 if (caps_output->props)
130 free(caps_output->props);
132 memset(caps_output, 0, sizeof(tdm_caps_output));
136 _tdm_display_destroy_private_layer(tdm_private_layer *private_layer)
138 tdm_private_capture *c = NULL, *cc = NULL;
140 LIST_DEL(&private_layer->link);
142 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &private_layer->capture_list, link)
143 tdm_capture_destroy_internal(c);
145 _tdm_display_destroy_caps_layer(&private_layer->caps);
151 _tdm_display_destroy_private_output(tdm_private_output *private_output)
153 tdm_private_display *private_display = private_output->private_display;
154 tdm_private_layer *l = NULL, *ll = NULL;
155 tdm_private_capture *c = NULL, *cc = NULL;
156 tdm_private_vblank_handler *v = NULL, *vv = NULL;
157 tdm_private_output_commit_handler *om = NULL, *omm = NULL;
158 tdm_private_layer_commit_handler *lm = NULL, *lmm = NULL;
159 tdm_private_change_handler *h = NULL, *hh = NULL;
161 LIST_DEL(&private_output->link);
163 free(private_output->layers_ptr);
165 LIST_FOR_EACH_ENTRY_SAFE(v, vv, &private_output->vblank_handler_list, link) {
170 LIST_FOR_EACH_ENTRY_SAFE(om, omm, &private_output->output_commit_handler_list, link) {
175 LIST_FOR_EACH_ENTRY_SAFE(lm, lmm, &private_output->layer_commit_handler_list, link) {
180 LIST_FOR_EACH_ENTRY_SAFE(lm, lmm, &private_output->pending_commit_handler_list, link) {
185 LIST_FOR_EACH_ENTRY_SAFE(h, hh, &private_output->change_handler_list_main, link) {
190 LIST_FOR_EACH_ENTRY_SAFE(h, hh, &private_output->change_handler_list_sub, link) {
195 if (private_output->vblank) {
196 /* tdm_vblank APIs is for server. it should be called in unlock status*/
197 _pthread_mutex_unlock(&private_display->lock);
198 tdm_vblank_destroy(private_output->vblank);
199 _pthread_mutex_lock(&private_display->lock);
202 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &private_output->capture_list, link)
203 tdm_capture_destroy_internal(c);
205 LIST_FOR_EACH_ENTRY_SAFE(l, ll, &private_output->layer_list, link)
206 _tdm_display_destroy_private_layer(l);
208 _tdm_display_destroy_caps_output(&private_output->caps);
210 if (private_output->dpms_changed_timer)
211 tdm_event_loop_source_remove(private_output->dpms_changed_timer);
213 private_output->stamp = 0;
214 free(private_output);
218 _tdm_display_destroy_private_display(tdm_private_display *private_display)
220 tdm_private_output *o = NULL, *oo = NULL;
221 tdm_private_pp *p = NULL, *pp = NULL;
223 free(private_display->outputs_ptr);
224 if (private_display->outputs) {
225 free(private_display->outputs);
226 private_display->outputs = NULL;
229 LIST_FOR_EACH_ENTRY_SAFE(p, pp, &private_display->pp_list, link)
230 tdm_pp_destroy_internal(p);
232 LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_display->output_list, link)
233 _tdm_display_destroy_private_output(o);
235 _tdm_display_destroy_caps_pp(&private_display->caps_pp);
236 _tdm_display_destroy_caps_capture(&private_display->caps_capture);
238 private_display->capabilities = 0;
239 private_display->caps_display.max_layer_count = 0;
243 _tdm_display_update_caps_pp(tdm_private_display *private_display,
246 tdm_func_display *func_display = &private_display->func_display;
249 if (!(private_display->capabilities & TDM_DISPLAY_CAPABILITY_PP))
250 return TDM_ERROR_NONE;
252 if (!func_display->display_get_pp_capability) {
253 TDM_ERR("no display_get_pp_capability()");
254 return TDM_ERROR_BAD_MODULE;
257 ret = func_display->display_get_pp_capability(private_display->bdata, caps);
258 if (ret != TDM_ERROR_NONE) {
259 TDM_ERR("display_get_pp_capability() failed");
260 return TDM_ERROR_BAD_MODULE;
263 return TDM_ERROR_NONE;
267 _tdm_display_update_caps_capture(tdm_private_display *private_display,
268 tdm_caps_capture *caps)
270 tdm_func_display *func_display = &private_display->func_display;
273 if (!(private_display->capabilities & TDM_DISPLAY_CAPABILITY_CAPTURE))
274 return TDM_ERROR_NONE;
276 if (!func_display->display_get_capture_capability) {
277 TDM_ERR("no display_get_capture_capability()");
278 return TDM_ERROR_BAD_MODULE;
281 ret = func_display->display_get_capture_capability(private_display->bdata, caps);
282 if (ret != TDM_ERROR_NONE) {
283 TDM_ERR("display_get_capture_capability() failed");
284 return TDM_ERROR_BAD_MODULE;
287 return TDM_ERROR_NONE;
291 _tdm_display_update_caps_layer(tdm_private_display *private_display,
292 tdm_layer *layer_backend, tdm_caps_layer *caps)
294 tdm_func_layer *func_layer = &private_display->func_layer;
297 if (!func_layer->layer_get_capability) {
298 TDM_ERR("no layer_get_capability()");
299 return TDM_ERROR_BAD_MODULE;
302 ret = func_layer->layer_get_capability(layer_backend, caps);
303 if (ret != TDM_ERROR_NONE) {
304 TDM_ERR("layer_get_capability() failed");
305 return TDM_ERROR_BAD_MODULE;
308 return TDM_ERROR_NONE;
312 _tdm_display_update_caps_output(tdm_private_display *private_display, int pipe,
313 tdm_output *output_backend, tdm_caps_output *caps)
315 tdm_func_output *func_output = &private_display->func_output;
316 char temp[TDM_NAME_LEN];
320 if (!func_output->output_get_capability) {
321 TDM_ERR("no output_get_capability()");
322 return TDM_ERROR_BAD_MODULE;
325 stamp = tdm_helper_get_time();
326 ret = func_output->output_get_capability(output_backend, caps);
327 TDM_DBG("backend output_get_capability() time: %.3f ms", (tdm_helper_get_time() - stamp) * 1000.0);
329 if (ret != TDM_ERROR_NONE) {
330 TDM_ERR("output_get_capability() failed");
331 return TDM_ERROR_BAD_MODULE;
334 /* FIXME: Use model for tdm client to distinguish amoung outputs */
335 snprintf(temp, TDM_NAME_LEN, "%s-%d", caps->model, pipe);
336 snprintf(caps->model, TDM_NAME_LEN, "%s", temp);
338 return TDM_ERROR_NONE;
342 _tdm_display_update_layer(tdm_private_display *private_display,
343 tdm_private_output *private_output,
344 tdm_layer *layer_backend, int index)
346 tdm_private_layer *private_layer;
349 private_layer = _tdm_display_find_private_layer(private_output, layer_backend);
350 if (!private_layer) {
351 private_layer = calloc(1, sizeof(tdm_private_layer));
352 TDM_RETURN_VAL_IF_FAIL(private_layer != NULL, TDM_ERROR_OUT_OF_MEMORY);
354 LIST_ADDTAIL(&private_layer->link, &private_output->layer_list);
355 private_layer->index = index;
356 private_layer->private_display = private_display;
357 private_layer->private_output = private_output;
358 private_layer->layer_backend = layer_backend;
360 LIST_INITHEAD(&private_layer->capture_list);
362 private_layer->usable = 1;
364 _tdm_display_destroy_caps_layer(&private_layer->caps);
366 ret = _tdm_display_update_caps_layer(private_display, layer_backend,
367 &private_layer->caps);
368 if (ret != TDM_ERROR_NONE)
371 return TDM_ERROR_NONE;
373 _tdm_display_destroy_private_layer(private_layer);
378 tdm_display_update_output(tdm_private_display *private_display,
379 tdm_output *output_backend, int pipe)
381 tdm_func_output *func_output = &private_display->func_output;
382 tdm_private_output *private_output = NULL;
383 tdm_layer **layers = NULL;
384 int layer_count = 0, i;
387 private_output = _tdm_display_find_private_output(private_display, output_backend);
388 if (!private_output) {
389 private_output = calloc(1, sizeof(tdm_private_output));
390 TDM_RETURN_VAL_IF_FAIL(private_output != NULL, TDM_ERROR_OUT_OF_MEMORY);
392 private_output->stamp = tdm_helper_get_time();
393 while (tdm_display_find_output_stamp(private_display, private_output->stamp))
394 private_output->stamp++;
396 LIST_ADDTAIL(&private_output->link, &private_display->output_list);
398 private_output->private_display = private_display;
399 private_output->current_dpms_value = TDM_OUTPUT_DPMS_OFF;
400 private_output->output_backend = output_backend;
401 private_output->pipe = pipe;
402 private_output->index = pipe;
404 LIST_INITHEAD(&private_output->layer_list);
405 LIST_INITHEAD(&private_output->capture_list);
406 LIST_INITHEAD(&private_output->vblank_handler_list);
407 LIST_INITHEAD(&private_output->output_commit_handler_list);
408 LIST_INITHEAD(&private_output->layer_commit_handler_list);
409 LIST_INITHEAD(&private_output->pending_commit_handler_list);
410 LIST_INITHEAD(&private_output->change_handler_list_main);
411 LIST_INITHEAD(&private_output->change_handler_list_sub);
413 if (func_output->output_set_status_handler) {
414 func_output->output_set_status_handler(private_output->output_backend,
415 tdm_output_cb_status,
417 private_output->regist_change_cb = 1;
420 ret = _tdm_display_update_caps_output(private_display, pipe, output_backend,
421 &private_output->caps);
422 if (ret != TDM_ERROR_NONE)
425 tdm_caps_output new_caps;
427 ret = _tdm_display_update_caps_output(private_display, pipe, output_backend,
429 if (ret != TDM_ERROR_NONE)
432 /* FIXME: This is very ugly. need to fix after the TDM ABI is changed. */
433 if (private_output->caps.status != new_caps.status) {
434 _tdm_display_destroy_caps_output(&private_output->caps);
435 private_output->caps = new_caps;
436 private_output->current_mode = NULL;
438 tdm_output_mode *old_modes = private_output->caps.modes;
439 unsigned int old_mode_count = private_output->caps.mode_count;
441 free(new_caps.modes);
442 new_caps.modes = old_modes;
443 new_caps.mode_count = old_mode_count;
444 if (private_output->caps.props)
445 free(private_output->caps.props);
446 private_output->caps = new_caps;
450 layers = func_output->output_get_layers(output_backend, &layer_count, &ret);
451 if (ret != TDM_ERROR_NONE)
454 for (i = 0; i < layer_count; i++) {
455 ret = _tdm_display_update_layer(private_display, private_output, layers[i], i);
456 if (ret != TDM_ERROR_NONE)
462 return TDM_ERROR_NONE;
464 _tdm_display_destroy_private_output(private_output);
470 _tdm_display_set_main_first(tdm_output **outputs, int index)
472 tdm_output *output_tmp = NULL;
477 output_tmp = outputs[0];
478 outputs[0] = outputs[index];
479 outputs[index] = output_tmp;
485 _tdm_display_get_ordered_outputs(tdm_private_display *private_display, int *count)
487 tdm_func_display *func_display = &private_display->func_display;
488 tdm_output **outputs = NULL;
489 tdm_output **new_outputs = NULL;
490 tdm_output *output_dsi = NULL;
491 tdm_output *output_lvds = NULL;
492 tdm_output *output_hdmia = NULL;
493 tdm_output *output_hdmib = NULL;
494 int i, output_count = 0, output_connected_count = 0;
495 int index_dsi = 0, index_lvds = 0, index_hdmia = 0, index_hdmib = 0;
498 /* don't change list order if not init time */
499 if (private_display->outputs)
500 return private_display->outputs;
502 outputs = func_display->display_get_outputs(private_display->bdata, &output_count, &ret);
503 if (ret != TDM_ERROR_NONE)
504 goto failed_get_outputs;
506 *count = output_count;
508 if (output_count == 0)
509 goto failed_get_outputs;
510 else if (output_count == 1) {
511 private_display->outputs = outputs;
515 /* count connected outputs */
516 for (i = 0; i < output_count; i++) {
517 tdm_func_output *func_output = &private_display->func_output;
518 tdm_caps_output caps;
519 memset(&caps, 0, sizeof(tdm_caps_output));
521 if (!func_output->output_get_capability) {
522 TDM_ERR("no output_get_capability()");
523 goto failed_get_outputs;
526 ret = func_output->output_get_capability(outputs[i], &caps);
527 if (ret != TDM_ERROR_NONE) {
528 TDM_ERR("output_get_capability() failed");
529 goto failed_get_outputs;
532 if (caps.status == TDM_OUTPUT_CONN_STATUS_CONNECTED) {
533 output_connected_count++;
536 case TDM_OUTPUT_TYPE_DSI:
537 output_dsi = outputs[i];
540 case TDM_OUTPUT_TYPE_LVDS:
541 output_lvds = outputs[i];
544 case TDM_OUTPUT_TYPE_HDMIA:
545 output_hdmia = outputs[i];
548 case TDM_OUTPUT_TYPE_HDMIB:
549 output_hdmib = outputs[i];
557 _tdm_display_destroy_caps_output(&caps);
560 /* ordering : main output is first */
561 /* If there is no connected output, lvds or dsi cannot be main display. (cannot connect after booting)
562 * But hdmi is possible, so set hdmi to main display.
563 * If connected only one output, it is main output.
564 * If connected outputs over 2, has priority like below.
565 * (dsi > lvds > hdmi > else)
567 if (output_connected_count == 0) {
568 /* hdmi > dsi > lvds > else */
569 if (output_hdmia != NULL)
570 new_outputs = _tdm_display_set_main_first(outputs, index_hdmia);
571 else if (output_hdmib != NULL)
572 new_outputs = _tdm_display_set_main_first(outputs, index_hdmib);
573 else if (output_dsi != NULL)
574 new_outputs = _tdm_display_set_main_first(outputs, index_dsi);
575 else if (output_lvds != NULL)
576 new_outputs = _tdm_display_set_main_first(outputs, index_lvds);
578 new_outputs = outputs;
579 } else { /* (output_connected_count > 1) */
580 /* dsi > lvds > hdmi > else */
581 if (output_dsi != NULL)
582 new_outputs = _tdm_display_set_main_first(outputs, index_dsi);
583 else if (output_lvds != NULL)
584 new_outputs = _tdm_display_set_main_first(outputs, index_lvds);
585 else if (output_hdmia != NULL)
586 new_outputs = _tdm_display_set_main_first(outputs, index_hdmia);
587 else if (output_hdmib != NULL)
588 new_outputs = _tdm_display_set_main_first(outputs, index_hdmib);
590 new_outputs = outputs;
593 private_display->outputs = new_outputs;
604 _tdm_display_update_internal(tdm_private_display *private_display,
607 tdm_output **outputs = NULL;
608 int output_count = 0, i;
609 tdm_error ret = TDM_ERROR_NONE;
611 LIST_INITHEAD(&private_display->output_list);
612 LIST_INITHEAD(&private_display->pp_list);
613 LIST_INITHEAD(&private_display->capture_list);
616 ret = _tdm_display_update_caps_pp(private_display, &private_display->caps_pp);
617 if (ret != TDM_ERROR_NONE)
620 ret = _tdm_display_update_caps_capture(private_display,
621 &private_display->caps_capture);
622 if (ret != TDM_ERROR_NONE)
626 outputs = _tdm_display_get_ordered_outputs(private_display, &output_count);
630 for (i = 0; i < output_count; i++) {
631 ret = tdm_display_update_output(private_display, outputs[i], i);
632 if (ret != TDM_ERROR_NONE)
636 return TDM_ERROR_NONE;
639 _tdm_display_destroy_private_display(private_display);
644 tdm_display_update(tdm_display *dpy)
646 tdm_private_display *private_display;
649 TDM_RETURN_VAL_IF_FAIL(dpy != NULL, TDM_ERROR_INVALID_PARAMETER);
651 private_display = dpy;
652 _pthread_mutex_lock(&private_display->lock);
654 ret = _tdm_display_update_internal(private_display, 1);
656 _pthread_mutex_unlock(&private_display->lock);
661 #define SUFFIX_MODULE ".so"
662 #define TDM_DEFAULT_MODULE "libtdm-default"SUFFIX_MODULE
663 #define TDM_DUMMY_MODULE "libtdm-dummy"SUFFIX_MODULE
665 int tdm_debug_module;
668 static tdm_private_display *g_private_display;
669 static pthread_mutex_t gLock = PTHREAD_MUTEX_INITIALIZER;
672 _tdm_display_check_module(tdm_backend_module *module)
676 TDM_INFO("TDM ABI version : %d.%d",
677 TDM_MAJOR_VERSION, TDM_MINOR_VERSION);
680 TDM_ERR("TDM backend doesn't have name");
681 return TDM_ERROR_BAD_MODULE;
684 if (!module->vendor) {
685 TDM_ERR("TDM backend doesn't have vendor");
686 return TDM_ERROR_BAD_MODULE;
689 major = TDM_BACKEND_GET_ABI_MAJOR(module->abi_version);
690 minor = TDM_BACKEND_GET_ABI_MINOR(module->abi_version);
692 TDM_INFO("TDM module name: %s", module->name);
693 TDM_INFO("'%s' vendor: %s", module->name, module->vendor);
694 TDM_INFO("'%s' version: %d.%d", module->name, major, minor);
696 if (major != TDM_MAJOR_VERSION) {
697 TDM_ERR("'%s' major version mismatch, %d != %d",
698 module->name, major, TDM_MAJOR_VERSION);
699 return TDM_ERROR_BAD_MODULE;
702 if (minor > TDM_MINOR_VERSION) {
703 TDM_ERR("'%s' minor version(%d) is newer than %d",
704 module->name, minor, TDM_MINOR_VERSION);
705 return TDM_ERROR_BAD_MODULE;
709 TDM_ERR("'%s' doesn't have init function", module->name);
710 return TDM_ERROR_BAD_MODULE;
713 if (!module->deinit) {
714 TDM_ERR("'%s' doesn't have deinit function", module->name);
715 return TDM_ERROR_BAD_MODULE;
718 return TDM_ERROR_NONE;
722 _tdm_display_check_backend_functions(tdm_private_display *private_display)
724 tdm_func_display *func_display = &private_display->func_display;
725 tdm_func_output *func_output = &private_display->func_output;
726 tdm_func_layer *func_layer = &private_display->func_layer;
729 /* below functions should be implemented in backend side */
731 TDM_RETURN_VAL_IF_FAIL(func_display != NULL, TDM_ERROR_BAD_MODULE);
732 TDM_RETURN_VAL_IF_FAIL(func_display->display_get_outputs, TDM_ERROR_BAD_MODULE);
733 TDM_RETURN_VAL_IF_FAIL(func_output->output_get_capability, TDM_ERROR_BAD_MODULE);
734 TDM_RETURN_VAL_IF_FAIL(func_output->output_get_layers, TDM_ERROR_BAD_MODULE);
735 TDM_RETURN_VAL_IF_FAIL(func_layer->layer_get_capability, TDM_ERROR_BAD_MODULE);
737 ret = func_display->display_get_capability(private_display->bdata, &private_display->caps_display);
738 if (ret != TDM_ERROR_NONE) {
739 TDM_ERR("display_get_capability() failed");
740 return TDM_ERROR_BAD_MODULE;
743 if (private_display->capabilities & TDM_DISPLAY_CAPABILITY_PP) {
744 tdm_func_pp *func_pp = &private_display->func_pp;
745 TDM_RETURN_VAL_IF_FAIL(func_display->display_get_pp_capability, TDM_ERROR_BAD_MODULE);
746 TDM_RETURN_VAL_IF_FAIL(func_display->display_create_pp, TDM_ERROR_BAD_MODULE);
747 TDM_RETURN_VAL_IF_FAIL(func_pp->pp_destroy, TDM_ERROR_BAD_MODULE);
748 TDM_RETURN_VAL_IF_FAIL(func_pp->pp_commit, TDM_ERROR_BAD_MODULE);
749 TDM_RETURN_VAL_IF_FAIL(func_pp->pp_set_done_handler, TDM_ERROR_BAD_MODULE);
752 if (private_display->capabilities & TDM_DISPLAY_CAPABILITY_CAPTURE) {
753 tdm_func_capture *func_capture = &private_display->func_capture;
754 TDM_RETURN_VAL_IF_FAIL(func_display->display_get_capture_capability, TDM_ERROR_BAD_MODULE);
755 if (private_display->caps_capture.capabilities & TDM_CAPTURE_CAPABILITY_OUTPUT)
756 TDM_RETURN_VAL_IF_FAIL(func_output->output_create_capture, TDM_ERROR_BAD_MODULE);
757 if (private_display->caps_capture.capabilities & TDM_CAPTURE_CAPABILITY_LAYER)
758 TDM_RETURN_VAL_IF_FAIL(func_layer->layer_create_capture, TDM_ERROR_BAD_MODULE);
759 TDM_RETURN_VAL_IF_FAIL(func_capture->capture_destroy, TDM_ERROR_BAD_MODULE);
760 TDM_RETURN_VAL_IF_FAIL(func_capture->capture_commit, TDM_ERROR_BAD_MODULE);
761 TDM_RETURN_VAL_IF_FAIL(func_capture->capture_set_done_handler, TDM_ERROR_BAD_MODULE);
764 return TDM_ERROR_NONE;
768 _tdm_display_load_module_with_file(tdm_private_display *private_display,
771 char path[PATH_MAX] = {0,};
772 tdm_backend_module *module_data;
778 size = snprintf(path, sizeof(path), TDM_MODULE_PATH "/%s", file);
779 if (size >= (int)sizeof(path)) {
780 TDM_WRN("too long: %s/%s", TDM_MODULE_PATH, file);
781 return TDM_ERROR_BAD_MODULE;
784 stamp = tdm_helper_get_time();
785 TDM_TRACE_BEGIN(Load_Backend);
787 module = dlopen(path, RTLD_LAZY);
789 TDM_ERR("failed to load module: %s(%s)", dlerror(), file);
791 return TDM_ERROR_BAD_MODULE;
794 module_data = dlsym(module, "tdm_backend_module_data");
796 TDM_ERR("'%s' doesn't have data object", file);
797 ret = TDM_ERROR_BAD_MODULE;
802 TDM_DBG("dlopen, dlsym time: %.3f ms", (tdm_helper_get_time() - stamp) * 1000.0);
804 private_display->module_data = module_data;
805 private_display->module = module;
807 /* check if version, init() and deinit() are valid or not */
808 ret = _tdm_display_check_module(module_data);
809 if (ret != TDM_ERROR_NONE)
814 /* We don't care if backend_data is NULL or not. It's up to backend. */
815 TDM_TRACE_BEGIN(Init_Backend);
816 stamp = tdm_helper_get_time();
817 private_display->bdata = module_data->init((tdm_display *)private_display, &ret);
818 TDM_DBG("backend init() time: %.3f ms", (tdm_helper_get_time() - stamp) * 1000.0);
821 if (ret != TDM_ERROR_NONE) {
822 TDM_ERR("failed to init '%s' module", module_data->name);
826 ret = _tdm_display_check_backend_functions(private_display);
827 if (ret != TDM_ERROR_NONE) {
828 module_data->deinit(private_display->bdata);
829 private_display->bdata = NULL;
833 TDM_INFO("Success to load '%s' module", module_data->name);
835 return TDM_ERROR_NONE;
838 private_display->module_data = NULL;
839 private_display->module = NULL;
844 _tdm_display_load_module(tdm_private_display *private_display)
846 const char *module_name;
847 char module[TDM_NAME_LEN];
848 struct dirent **namelist;
852 module_name = getenv("TDM_MODULE");
854 module_name = TDM_DEFAULT_MODULE;
856 strncpy(module, module_name, TDM_NAME_LEN - 1);
857 module[TDM_NAME_LEN - 1] = '\0';
859 /* load bufmgr priv from default lib */
860 ret = _tdm_display_load_module_with_file(private_display, (const char*)module);
861 if (ret == TDM_ERROR_NONE)
862 return TDM_ERROR_NONE;
864 /* load bufmgr priv from dummy lib */
865 ret = _tdm_display_load_module_with_file(private_display, TDM_DUMMY_MODULE);
866 if (ret == TDM_ERROR_NONE)
867 return TDM_ERROR_NONE;
869 /* load bufmgr priv from configured path */
870 n = scandir(TDM_MODULE_PATH, &namelist, 0, alphasort);
872 TDM_ERR("no module in '%s'\n", TDM_MODULE_PATH);
873 return TDM_ERROR_BAD_MODULE;
876 ret = TDM_ERROR_BAD_MODULE;
878 if (ret < 0 && strstr(namelist[n]->d_name, SUFFIX_MODULE))
879 ret = _tdm_display_load_module_with_file(private_display, namelist[n]->d_name);
889 _tdm_display_unload_module(tdm_private_display *private_display)
891 if (private_display->module_data)
892 private_display->module_data->deinit(private_display->bdata);
893 if (private_display->module)
894 dlclose(private_display->module);
896 private_display->bdata = NULL;
897 private_display->module_data = NULL;
898 private_display->module = NULL;
902 tdm_display_init(tdm_error *error)
904 tdm_private_display *private_display = NULL;
907 double stamp1, stamp2, start;
909 pthread_mutex_lock(&gLock);
911 if (g_private_display) {
912 g_private_display->init_count++;
913 pthread_mutex_unlock(&gLock);
915 *error = TDM_ERROR_NONE;
916 return g_private_display;
919 start = stamp1 = tdm_helper_get_time();
921 private_display = calloc(1, sizeof(tdm_private_display));
922 if (!private_display) {
923 ret = TDM_ERROR_OUT_OF_MEMORY;
924 TDM_ERR("'private_display != NULL' failed");
928 str = getenv("TDM_DEBUG_MODULE");
930 tdm_display_enable_debug_module(str);
932 str = getenv("TDM_DEBUG_DUMP");
934 tdm_display_enable_dump(private_display, str, NULL, NULL);
936 str = getenv("TDM_DEBUG_PATH");
938 tdm_display_enable_path(str);
940 str = getenv("TDM_COMMIT_PER_VBLANK");
943 int enable = strtol(str, &end, 10);
945 tdm_display_enable_commit_per_vblank(private_display, enable);
948 if (pthread_mutex_init(&private_display->lock, NULL)) {
949 ret = TDM_ERROR_OPERATION_FAILED;
950 TDM_ERR("mutex init failed: %m");
951 goto failed_mutex_init;
954 _pthread_mutex_lock(&private_display->lock);
956 stamp2 = tdm_helper_get_time();
957 TDM_DBG("prepare init time: %.3f ms", (stamp2 - stamp1) * 1000.0);
960 ret = tdm_event_loop_init(private_display);
961 if (ret != TDM_ERROR_NONE)
964 stamp2 = tdm_helper_get_time();
965 TDM_DBG("creating event loop time: %.3f ms", (stamp2 - stamp1) * 1000.0);
968 ret = _tdm_display_load_module(private_display);
969 if (ret != TDM_ERROR_NONE)
972 stamp2 = tdm_helper_get_time();
973 TDM_DBG("loading backend time: %.3f ms", (stamp2 - stamp1) * 1000.0);
977 int tdm_drm_fd = tdm_helper_get_fd("TDM_DRM_MASTER_FD");
978 if (tdm_drm_fd >= 0) {
979 private_display->bufmgr = tbm_bufmgr_init(tdm_drm_fd);
981 if (!private_display->bufmgr) {
982 TDM_ERR("tbm_bufmgr_init failed");
985 TDM_INFO("tbm_bufmgr_init successed");
988 stamp2 = tdm_helper_get_time();
989 TDM_DBG("initializing bufmgr time: %.3f ms", (stamp2 - stamp1) * 1000.0);
994 TDM_TRACE_BEGIN(Update_Display);
995 ret = _tdm_display_update_internal(private_display, 0);
997 if (ret != TDM_ERROR_NONE)
1000 stamp2 = tdm_helper_get_time();
1001 TDM_DBG("updating display time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1004 tdm_event_loop_create_backend_source(private_display);
1006 private_display->init_count = 1;
1007 private_display->commit_type = TDM_COMMIT_TYPE_NONE;
1009 g_private_display = private_display;
1012 *error = TDM_ERROR_NONE;
1014 _pthread_mutex_unlock(&private_display->lock);
1015 pthread_mutex_unlock(&gLock);
1017 TDM_INFO("init time: %.3f ms", (tdm_helper_get_time() - start) * 1000.0);
1019 return (tdm_display *)private_display;
1022 _tdm_display_unload_module(private_display);
1024 tdm_event_loop_stop(private_display);
1025 tdm_event_loop_deinit(private_display);
1027 _pthread_mutex_unlock(&private_display->lock);
1028 pthread_mutex_destroy(&private_display->lock);
1030 free(private_display);
1034 pthread_mutex_unlock(&gLock);
1039 tdm_display_deinit(tdm_display *dpy)
1041 tdm_private_display *private_display = dpy;
1043 if (!private_display)
1046 pthread_mutex_lock(&gLock);
1048 private_display->init_count--;
1049 if (private_display->init_count > 0) {
1050 pthread_mutex_unlock(&gLock);
1054 /* dont move the position of lock/unlock. all resource should be protected
1055 * during destroying. after tdm_event_loop_deinit, we don't worry about thread
1056 * things because it's finalized.
1058 _pthread_mutex_lock(&private_display->lock);
1059 tdm_event_loop_stop(private_display);
1060 _tdm_display_destroy_private_display(private_display);
1061 _tdm_display_unload_module(private_display);
1062 _pthread_mutex_unlock(&private_display->lock);
1064 tdm_event_loop_deinit(private_display);
1067 if (private_display->bufmgr)
1068 tbm_bufmgr_deinit(private_display->bufmgr);
1071 tdm_helper_set_fd("TDM_DRM_MASTER_FD", -1);
1073 pthread_mutex_destroy(&private_display->lock);
1074 free(private_display);
1075 g_private_display = NULL;
1077 if (tdm_debug_dump_dir) {
1078 free(tdm_debug_dump_dir);
1079 tdm_debug_dump_dir = NULL;
1082 pthread_mutex_unlock(&gLock);
1088 tdm_display_check_module_abi(tdm_private_display *private_display, int abimaj, int abimin)
1090 tdm_backend_module *module = private_display->module_data;
1092 if (TDM_BACKEND_GET_ABI_MAJOR(module->abi_version) < abimaj)
1095 if (TDM_BACKEND_GET_ABI_MINOR(module->abi_version) < abimin)
1102 tdm_display_enable_debug_module(const char*modules)
1104 char temp[TDM_PATH_LEN];
1108 snprintf(temp, TDM_PATH_LEN, "%s", modules);
1110 tdm_debug_module = 0;
1112 arg = strtok_r(temp, TDM_DELIM, &end);
1114 if (!strncmp(arg, "none", 4)) {
1115 tdm_debug_module = 0;
1116 return TDM_ERROR_NONE;
1118 if (!strncmp(arg, "all", 3)) {
1119 tdm_debug_module = 0xFFFFFFFF;
1120 return TDM_ERROR_NONE;
1122 if (!strncmp(arg, "buffer", 6))
1123 tdm_debug_module |= TDM_DEBUG_BUFFER;
1124 else if (!strncmp(arg, "thread", 6))
1125 tdm_debug_module |= TDM_DEBUG_THREAD;
1126 else if (!strncmp(arg, "mutex", 5))
1127 tdm_debug_module |= TDM_DEBUG_MUTEX;
1128 else if (!strncmp(arg, "vblank", 6))
1129 tdm_debug_module |= TDM_DEBUG_VBLANK;
1130 else if (!strncmp(arg, "commit", 6))
1131 tdm_debug_module |= TDM_DEBUG_COMMIT;
1133 return TDM_ERROR_BAD_REQUEST;
1135 arg = strtok_r(NULL, TDM_DELIM, &end);
1138 TDM_INFO("module debugging... '%s'", modules);
1140 return TDM_ERROR_NONE;
1144 tdm_display_enable_dump(tdm_private_display *private_display, const char *dump_str, char *reply, int *len)
1146 char temp[TDM_PATH_LEN] = {0,}, temp2[TDM_PATH_LEN] = {0,};
1147 char *path = NULL, *path2;
1151 snprintf(temp2, TDM_PATH_LEN, "%s", dump_str);
1152 path2 = strtostr(temp, TDM_PATH_LEN, temp2, "@");
1153 if (!path2 || path2[0] == '\0')
1154 path2 = TDM_DUMP_DIR;
1160 snprintf(temp, sizeof(temp), "%s", dump_str);
1161 arg = strtok_r(temp, ",", &end);
1162 TDM_GOTO_IF_FAIL(arg != NULL, done);
1164 if (!strncmp(arg, "none", 4)) {
1166 TDM_SNPRINTF(reply, len, "path: %s\n", (tdm_debug_dump_dir) ? : "unknown");
1167 if (tdm_debug_dump_dir) {
1168 free(tdm_debug_dump_dir);
1169 tdm_debug_dump_dir = NULL;
1174 path = tdm_helper_dump_make_directory(path2, reply, len);
1175 TDM_GOTO_IF_FAIL(path != NULL, done);
1177 if (!strncmp(arg, "current", 7)) {
1178 tdm_private_output *o = NULL;
1179 if (!private_display) {
1180 TDM_WRN("no private_display");
1184 LIST_FOR_EACH_ENTRY(o, &private_display->output_list, link) {
1185 tdm_private_layer *l = NULL;
1186 LIST_FOR_EACH_ENTRY(l, &o->layer_list, link) {
1187 char str[TDM_PATH_LEN];
1188 if (l->usable || l->caps.capabilities & TDM_LAYER_CAPABILITY_VIDEO)
1190 if (!l->showing_buffer)
1192 snprintf(str, TDM_PATH_LEN, "layer_%d_%d", o->index, l->index);
1193 tdm_helper_dump_buffer_str(l->showing_buffer->buffer, path, str);
1197 TDM_SNPRINTF(reply, len, "path: %s\n", path);
1201 TDM_SNPRINTF(reply, len, "dump: %s\n", arg);
1204 if (!strncmp(arg, "all", 3)) {
1205 tdm_debug_dump = 0xFFFFFFFF;
1207 } else if (!strncmp(arg, "layer", 5)) {
1208 tdm_debug_dump |= TDM_DUMP_FLAG_LAYER;
1209 } else if (!strncmp(arg, "pp", 2)) {
1210 tdm_debug_dump |= TDM_DUMP_FLAG_PP;
1211 } else if (!strncmp(arg, "capture", 7)) {
1212 tdm_debug_dump |= TDM_DUMP_FLAG_CAPTURE;
1216 arg = strtok_r(NULL, ",", &end);
1219 if (tdm_debug_dump_dir)
1220 free(tdm_debug_dump_dir);
1222 tdm_debug_dump_dir = strndup(path, TDM_PATH_LEN);
1224 TDM_INFO("dump... '%s'", dump_str);
1230 return TDM_ERROR_NONE;
1234 tdm_display_enable_path(const char *path)
1236 static int old_stdout = -1;
1237 char fd_name[TDM_PATH_LEN];
1241 if (old_stdout == -1)
1242 old_stdout = dup(STDOUT_FILENO);
1244 tdm_log_enable_dlog(0);
1246 snprintf(fd_name, TDM_PATH_LEN, "%s", path);
1248 log_fl = fopen(fd_name, "a");
1250 TDM_ERR("failed: open file(%s)\n", fd_name);
1251 return TDM_ERROR_OPERATION_FAILED;
1255 close(STDOUT_FILENO);
1257 setvbuf(log_fl, NULL, _IOLBF, 512);
1258 log_fd = fileno(log_fl);
1260 dup2(log_fd, STDOUT_FILENO);
1263 return TDM_ERROR_NONE;
1268 _tdm_display_ttrace_vblank_cb(tdm_vblank *vblank, tdm_error error, unsigned int sequence,
1269 unsigned int tv_sec, unsigned int tv_usec, void *user_data)
1271 tdm_error ret = TDM_ERROR_NONE;
1273 TDM_TRACE_MARK(VBlank);
1275 ret = tdm_vblank_wait(vblank, 0, 0, 1, _tdm_display_ttrace_vblank_cb, NULL);
1276 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
1280 tdm_display_enable_ttrace_vblank(tdm_display *dpy, tdm_output *output, int enable)
1282 static tdm_vblank *vblank = NULL;
1283 tdm_error ret = TDM_ERROR_NONE;
1287 tdm_vblank_destroy(vblank);
1289 return TDM_ERROR_NONE;
1291 const tdm_output_mode *mode = NULL;
1294 return TDM_ERROR_NONE;
1296 vblank = tdm_vblank_create(dpy, output, &ret);
1297 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, ret);
1299 ret = tdm_output_get_mode(output, &mode);
1300 TDM_GOTO_IF_FAIL(mode != NULL, enable_fail);
1302 ret = tdm_vblank_set_fps(vblank, mode->vrefresh);
1303 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, enable_fail);
1305 ret = tdm_vblank_set_enable_fake(vblank, 1);
1306 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, enable_fail);
1308 ret = tdm_vblank_wait(vblank, 0, 0, 1, _tdm_display_ttrace_vblank_cb, NULL);
1309 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, enable_fail);
1312 return TDM_ERROR_NONE;
1316 tdm_vblank_destroy(vblank);
1322 tdm_display_enable_commit_per_vblank(tdm_private_display *private_display, int enable)
1324 private_display->commit_per_vblank = enable;
1326 if (private_display->commit_per_vblank == 0)
1327 TDM_INFO("commit per vblank: disable");
1328 else if (private_display->commit_per_vblank == 1)
1329 TDM_INFO("commit per vblank: enable (1 layer)");
1330 else if (private_display->commit_per_vblank == 2)
1331 TDM_INFO("commit per vblank: enable (previous commit)");
1333 private_display->commit_per_vblank = 1;
1334 TDM_INFO("commit per vblank: enable (changed to 1 layer)");
1337 return TDM_ERROR_NONE;
1341 tdm_display_enable_fps(tdm_private_display *private_display, int enable)
1343 private_display->print_fps = enable;
1345 TDM_INFO("print fps: %s", (enable) ? "enable" : "disable");
1347 return TDM_ERROR_NONE;