be3d0bf9ccf37f7dfaa8225226b16cda39a8cdf8
[platform/core/uifw/libtdm.git] / src / tdm.c
1 /**************************************************************************
2  *
3  * libtdm
4  *
5  * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
6  *
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>
13  *
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:
21  *
22  * The above copyright notice and this permission notice (including the
23  * next paragraph) shall be included in all copies or substantial portions
24  * of the Software.
25  *
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.
33  *
34 **************************************************************************/
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include "tdm_private.h"
41
42 pthread_mutex_t tdm_mutex_check_lock = PTHREAD_MUTEX_INITIALIZER;
43 int tdm_mutex_locked;
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;
48
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;
52
53 /* LCOV_EXCL_START */
54 static tdm_private_layer *
55 _tdm_display_find_private_layer(tdm_private_output *private_output,
56                                                                 tdm_layer *layer_backend)
57 {
58         tdm_private_layer *private_layer = NULL;
59
60         LIST_FOR_EACH_ENTRY(private_layer, &private_output->layer_list, link) {
61                 if (private_layer->layer_backend == layer_backend)
62                         return private_layer;
63         }
64
65         return NULL;
66 }
67
68 INTERN tdm_private_output *
69 tdm_display_find_private_output(tdm_private_display *private_display, tdm_output *output_backend)
70 {
71         tdm_private_module *private_module = NULL;
72         tdm_private_output *private_output = NULL;
73
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;
78                 }
79         }
80
81         return NULL;
82 }
83
84 INTERN void *
85 tdm_display_find_output_stamp(tdm_private_display *private_display, double stamp)
86 {
87         tdm_private_module *private_module = NULL;
88         tdm_private_output *private_output = NULL;
89
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;
94                 }
95         }
96
97         return NULL;
98 }
99
100 static void
101 _tdm_display_destroy_caps_pp(tdm_caps_pp *caps_pp)
102 {
103         if (caps_pp->formats)
104                 free(caps_pp->formats);
105
106         memset(caps_pp, 0, sizeof(tdm_caps_pp));
107 }
108
109 static void
110 _tdm_display_destroy_caps_capture(tdm_caps_capture *caps_capture)
111 {
112         if (caps_capture->formats)
113                 free(caps_capture->formats);
114
115         memset(caps_capture, 0, sizeof(tdm_caps_capture));
116 }
117
118 static void
119 _tdm_display_destroy_caps_layer(tdm_caps_layer *caps_layer)
120 {
121         if (caps_layer->formats)
122                 free(caps_layer->formats);
123
124         if (caps_layer->props)
125                 free(caps_layer->props);
126
127         memset(caps_layer, 0, sizeof(tdm_caps_layer));
128 }
129
130 static void
131 _tdm_display_destroy_caps_output(tdm_caps_output *caps_output)
132 {
133         if (caps_output->modes)
134                 free(caps_output->modes);
135
136         if (caps_output->props)
137                 free(caps_output->props);
138
139         memset(caps_output, 0, sizeof(tdm_caps_output));
140 }
141
142 static void
143 _tdm_display_destroy_private_layer(tdm_private_layer *private_layer)
144 {
145         tdm_private_capture *c = NULL, *cc = NULL;
146
147         tdm_layer_unset_buffer_internal(private_layer);
148
149         LIST_FOR_EACH_ENTRY_SAFE(c, cc, &private_layer->capture_list, link)
150         tdm_capture_destroy_internal(c);
151
152         _tdm_display_destroy_caps_layer(&private_layer->caps);
153
154         /* when destroying layer, someone could check if layer is valid. So delete
155          * the layer's link at last.
156          */
157         LIST_DEL(&private_layer->link);
158
159         free(private_layer);
160 }
161
162 static void
163 _tdm_display_destroy_private_output(tdm_private_output *private_output)
164 {
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;
173
174         free(private_output->layers_ptr);
175
176         if (private_output->vblank_timeout_timer)
177                 tdm_event_loop_source_remove(private_output->vblank_timeout_timer);
178
179         LIST_FOR_EACH_ENTRY_SAFE(v, vv, &private_output->vblank_handler_list, link) {
180                 LIST_DEL(&v->link);
181                 free(v);
182         }
183
184         LIST_FOR_EACH_ENTRY_SAFE(om, omm, &private_output->output_commit_handler_list, link) {
185                 LIST_DEL(&om->link);
186                 free(om);
187         }
188
189         LIST_FOR_EACH_ENTRY_SAFE(lm, lmm, &private_output->layer_commit_handler_list, link) {
190                 LIST_DEL(&lm->link);
191                 free(lm);
192         }
193
194         LIST_FOR_EACH_ENTRY_SAFE(lm, lmm, &private_output->pending_commit_handler_list, link) {
195                 LIST_DEL(&lm->link);
196                 free(lm);
197         }
198
199         LIST_FOR_EACH_ENTRY_SAFE(h, hh, &private_output->change_handler_list, link) {
200                 LIST_DEL(&h->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);
203                 free(h);
204         }
205
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);
211         }
212
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);
218         }
219
220         LIST_FOR_EACH_ENTRY_SAFE(c, cc, &private_output->capture_list, link)
221         tdm_capture_destroy_internal(c);
222
223         LIST_FOR_EACH_ENTRY_SAFE(hw, hww, &private_output->hwc_window_list, link)
224         tdm_hwc_window_destroy_internal(hw);
225
226         LIST_FOR_EACH_ENTRY_SAFE(l, ll, &private_output->layer_list, link)
227         _tdm_display_destroy_private_layer(l);
228
229         _tdm_display_destroy_caps_output(&private_output->caps);
230
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);
235
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.
238          */
239         LIST_DEL(&private_output->link);
240
241         private_output->stamp = 0;
242         free(private_output);
243 }
244
245 static void
246 _tdm_display_destroy_private_display(tdm_private_display *private_display)
247 {
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;
251
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);
255                 }
256
257                 LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_module->output_list, link) {
258                         _tdm_display_destroy_private_output(o);
259                 }
260
261                 _tdm_display_destroy_caps_pp(&private_module->caps_pp);
262                 _tdm_display_destroy_caps_capture(&private_module->caps_capture);
263
264                 private_module->capabilities = 0;
265                 private_module->caps_display.max_layer_count = 0;
266
267                 if (private_module->outputs) {
268                         free(private_module->outputs);
269                         private_module->outputs = NULL;
270                 }
271         }
272 }
273
274 static tdm_error
275 _tdm_display_update_caps_pp(tdm_private_module *private_module, tdm_caps_pp *caps)
276 {
277         tdm_func_display *func_display;
278         tdm_error ret;
279
280         func_display = &private_module->func_display;
281
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;
285         }
286
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;
291         }
292
293         return TDM_ERROR_NONE;
294 }
295
296 static tdm_error
297 _tdm_display_update_caps_capture(tdm_private_module *private_module, tdm_caps_capture *caps)
298 {
299         tdm_func_display *func_display;
300         tdm_error ret;
301
302         func_display = &private_module->func_display;
303
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;
307         }
308
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;
313         }
314
315         return TDM_ERROR_NONE;
316 }
317
318 static tdm_error
319 _tdm_display_update_caps_layer(tdm_private_module *private_module,
320                                                            tdm_layer *layer_backend, tdm_caps_layer *caps)
321 {
322         tdm_func_layer *func_layer = &private_module->func_layer;
323         tdm_error ret;
324
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;
328         }
329
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;
334         }
335
336         return TDM_ERROR_NONE;
337 }
338
339 static tdm_error
340 _tdm_display_update_caps_output(tdm_private_module *private_module, int pipe,
341                                                                 tdm_output *output_backend, tdm_caps_output *caps)
342 {
343         tdm_func_output *func_output = &private_module->func_output;
344         char temp[TDM_NAME_LEN];
345         tdm_error ret;
346         double stamp;
347
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;
351         }
352
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);
357
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;
361         }
362
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);
366
367         return TDM_ERROR_NONE;
368 }
369
370 static tdm_error
371 _tdm_display_update_layer(tdm_private_output *private_output,
372                                                   tdm_layer *layer_backend, int index)
373 {
374         tdm_private_layer *private_layer;
375         tdm_error ret;
376
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);
381
382                 LIST_ADDTAIL(&private_layer->link, &private_output->layer_list);
383                 LIST_INITHEAD(&private_layer->capture_list);
384
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;
391         }
392
393         _tdm_display_destroy_caps_layer(&private_layer->caps);
394
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);
397
398         return TDM_ERROR_NONE;
399 }
400
401 INTERN tdm_error
402 tdm_display_update_output(tdm_private_module *private_module,
403                                                   tdm_output *output_backend, int pipe, unsigned int need_new_caps)
404 {
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;
409         tdm_error ret;
410
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);
415
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++;
419
420                 LIST_ADDTAIL(&private_output->link, &private_module->output_list);
421
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;
428
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);
437
438                 private_output->need_validate.event_fd = -1;
439
440                 if (func_output->output_set_status_handler) {
441                         func_output->output_set_status_handler(private_output->output_backend,
442                                                                                                    tdm_output_cb_status,
443                                                                                                    private_output);
444                         private_output->regist_change_cb = 1;
445                 }
446         }
447
448         /* need_new_caps will be true only in case of "disconnected -> connected" and "connected -> disconnected"
449          * because we have to get new modes.
450          */
451         if (need_new_caps) {
452                 _tdm_display_destroy_caps_output(&private_output->caps);
453
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);
456
457                 if (private_output->caps.status == TDM_OUTPUT_CONN_STATUS_DISCONNECTED)
458                         private_output->current_mode = NULL;
459         }
460
461         layers = func_output->output_get_layers(output_backend, &layer_count, &ret);
462         if (ret != TDM_ERROR_NONE) {
463                 free(layers);
464                 return ret;
465         }
466
467         for (i = 0; i < layer_count; i++) {
468                 ret = _tdm_display_update_layer(private_output, layers[i], i);
469                 if (ret != TDM_ERROR_NONE) {
470                         free(layers);
471                         return ret;
472                 }
473         }
474
475         free(layers);
476
477         return TDM_ERROR_NONE;
478 }
479
480 static void
481 _tdm_display_set_main_first(tdm_output **outputs, int index)
482 {
483         tdm_output *output_tmp = NULL;
484
485         if (index == 0)
486                 return;
487
488         output_tmp = outputs[0];
489         outputs[0] = outputs[index];
490         outputs[index] = output_tmp;
491 }
492
493 static tdm_output **
494 _tdm_display_get_ordered_outputs(tdm_private_module *private_module, int *count)
495 {
496         tdm_func_display *func_display = &private_module->func_display;
497         tdm_output **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;
504         tdm_error ret;
505
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;
510         }
511
512         outputs = func_display->display_get_outputs(private_module->bdata, &output_count, &ret);
513         if (ret != TDM_ERROR_NONE)
514                 goto failed_get_outputs;
515
516         *count = output_count;
517
518         if (output_count == 0)
519                 goto failed_get_outputs;
520         else if (output_count == 1) {
521                 private_module->outputs = outputs;
522                 return outputs;
523         }
524
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));
530
531                 ret = func_output->output_get_capability(outputs[i], &caps);
532                 if (ret != TDM_ERROR_NONE) {
533                         TDM_ERR("output_get_capability() failed");
534                         goto failed_get_outputs;
535                 }
536
537                 if (caps.status == TDM_OUTPUT_CONN_STATUS_CONNECTED) {
538                         output_connected_count++;
539
540                         switch (caps.type) {
541                         case TDM_OUTPUT_TYPE_DSI:
542                                 output_dsi = outputs[i];
543                                 index_dsi = i;
544                                 break;
545                         case TDM_OUTPUT_TYPE_LVDS:
546                                 output_lvds = outputs[i];
547                                 index_lvds = i;
548                                 break;
549                         case TDM_OUTPUT_TYPE_HDMIA:
550                                 output_hdmia = outputs[i];
551                                 index_hdmia = i;
552                                 break;
553                         case TDM_OUTPUT_TYPE_HDMIB:
554                                 output_hdmib = outputs[i];
555                                 index_hdmib = i;
556                                 break;
557                         default:
558                                 break;
559                         }
560                 }
561
562                 _tdm_display_destroy_caps_output(&caps);
563         }
564
565         /* ordering : main output is first */
566         /* If there is no connected output, lvds or dsi cannot be main display. (cannot connect after booting)
567           * But hdmi is possible, so set hdmi to main display.
568           * If connected only one output, it is main output.
569           * If connected outputs over 2, has priority like below.
570           * (dsi > lvds > hdmi > else)
571           */
572         if (output_connected_count == 0) {
573                 /* hdmi > dsi > lvds > else */
574                 if (output_hdmia != NULL)
575                         _tdm_display_set_main_first(outputs, index_hdmia);
576                 else if (output_hdmib != NULL)
577                         _tdm_display_set_main_first(outputs, index_hdmib);
578                 else if (output_dsi != NULL)
579                         _tdm_display_set_main_first(outputs, index_dsi);
580                 else if (output_lvds != NULL)
581                         _tdm_display_set_main_first(outputs, index_lvds);
582         } else { /* (output_connected_count > 1) */
583                 /* dsi > lvds > hdmi > else */
584                 if (output_dsi != NULL)
585                         _tdm_display_set_main_first(outputs, index_dsi);
586                 else if (output_lvds != NULL)
587                         _tdm_display_set_main_first(outputs, index_lvds);
588                 else if (output_hdmia != NULL)
589                         _tdm_display_set_main_first(outputs, index_hdmia);
590                 else if (output_hdmib != NULL)
591                         _tdm_display_set_main_first(outputs, index_hdmib);
592         }
593
594         private_module->outputs = outputs;
595
596         return outputs;
597
598 failed_get_outputs:
599         free(outputs);
600         *count = 0;
601         return NULL;
602 }
603
604 static tdm_error
605 _tdm_display_setup(tdm_private_display *private_display)
606 {
607         tdm_private_module *private_module = NULL;
608         tdm_error ret = TDM_ERROR_NONE;
609         int index = 0;
610
611         if (private_display->pp_module) {
612                 ret = _tdm_display_update_caps_pp(private_display->pp_module,
613                                                                                   &private_display->pp_module->caps_pp);
614                 if (ret != TDM_ERROR_NONE)
615                         goto failed_update;
616         }
617
618         if (private_display->capture_module) {
619                 ret = _tdm_display_update_caps_capture(private_display->capture_module,
620                                                                                            &private_display->capture_module->caps_capture);
621                 if (ret != TDM_ERROR_NONE)
622                         goto failed_update;
623         }
624
625         LIST_FOR_EACH_ENTRY(private_module, &private_display->module_list, link) {
626                 tdm_output **outputs;
627                 int output_count = 0, i;
628
629                 outputs = _tdm_display_get_ordered_outputs(private_module, &output_count);
630                 if (!outputs)
631                         goto failed_update;
632
633                 for (i = 0; i < output_count; i++) {
634                         ret = tdm_display_update_output(private_module, outputs[i], index++, 1);
635                         if (ret != TDM_ERROR_NONE)
636                                 goto failed_update;
637                 }
638         }
639
640         return TDM_ERROR_NONE;
641
642 failed_update:
643         _tdm_display_destroy_private_display(private_display);
644         return ret;
645 }
646 /* LCOV_EXCL_STOP */
647
648 EXTERN tdm_error
649 tdm_display_update(tdm_display *dpy)
650 {
651         TDM_RETURN_VAL_IF_FAIL(dpy != NULL, TDM_ERROR_INVALID_PARAMETER);
652
653         TDM_DEPRECATED(NULL);
654
655         return TDM_ERROR_NONE;
656 }
657
658 int tdm_debug_module;
659 int tdm_debug_dump;
660 int tdm_ttrace_module;
661 int tdm_ttrace_output;
662
663 static tdm_private_display *g_private_display;
664 static pthread_mutex_t gLock = PTHREAD_MUTEX_INITIALIZER;
665
666 /* LCOV_EXCL_START */
667 static tdm_error
668 _tdm_display_check_module(tdm_backend_module *module)
669 {
670         int major, minor;
671
672         TDM_INFO("TDM ABI version : %d.%d",
673                          TDM_MAJOR_VERSION, TDM_MINOR_VERSION);
674
675         if (!module->name) {
676                 TDM_ERR("TDM backend doesn't have name");
677                 return TDM_ERROR_BAD_MODULE;
678         }
679
680         if (!module->vendor) {
681                 TDM_ERR("TDM backend doesn't have vendor");
682                 return TDM_ERROR_BAD_MODULE;
683         }
684
685         major = TDM_BACKEND_GET_ABI_MAJOR(module->abi_version);
686         minor = TDM_BACKEND_GET_ABI_MINOR(module->abi_version);
687
688         TDM_INFO("TDM module name: %s", module->name);
689         TDM_INFO("'%s' vendor: %s", module->name, module->vendor);
690         TDM_INFO("'%s' version: %d.%d", module->name, major, minor);
691
692         if (major != TDM_MAJOR_VERSION) {
693                 TDM_ERR("'%s' major version mismatch, %d != %d",
694                                 module->name, major, TDM_MAJOR_VERSION);
695                 return TDM_ERROR_BAD_MODULE;
696         }
697
698         if (minor > TDM_MINOR_VERSION) {
699                 TDM_ERR("'%s' minor version(%d) is newer than %d",
700                                 module->name, minor, TDM_MINOR_VERSION);
701                 return TDM_ERROR_BAD_MODULE;
702         }
703
704         if (!module->init) {
705                 TDM_ERR("'%s' doesn't have init function", module->name);
706                 return TDM_ERROR_BAD_MODULE;
707         }
708
709         if (!module->deinit) {
710                 TDM_ERR("'%s' doesn't have deinit function", module->name);
711                 return TDM_ERROR_BAD_MODULE;
712         }
713
714         return TDM_ERROR_NONE;
715 }
716
717 static tdm_error
718 _tdm_display_check_backend_functions(tdm_private_module *private_module)
719 {
720         tdm_func_display *func_display = &private_module->func_display;
721         tdm_func_output *func_output = &private_module->func_output;
722         tdm_func_layer *func_layer = &private_module->func_layer;
723         tdm_error ret;
724
725         /* below functions should be implemented in backend side */
726
727         TDM_RETURN_VAL_IF_FAIL(func_display != NULL, TDM_ERROR_BAD_MODULE);
728         TDM_RETURN_VAL_IF_FAIL(func_display->display_get_capability, TDM_ERROR_BAD_MODULE);
729         TDM_RETURN_VAL_IF_FAIL(func_display->display_get_outputs, TDM_ERROR_BAD_MODULE);
730         TDM_RETURN_VAL_IF_FAIL(func_output->output_get_capability, TDM_ERROR_BAD_MODULE);
731         TDM_RETURN_VAL_IF_FAIL(func_output->output_get_layers, TDM_ERROR_BAD_MODULE);
732         TDM_RETURN_VAL_IF_FAIL(func_output->output_wait_vblank, TDM_ERROR_BAD_MODULE);
733         TDM_RETURN_VAL_IF_FAIL(func_output->output_set_vblank_handler, TDM_ERROR_BAD_MODULE);
734         TDM_RETURN_VAL_IF_FAIL(func_output->output_commit, TDM_ERROR_BAD_MODULE);
735         TDM_RETURN_VAL_IF_FAIL(func_output->output_set_commit_handler, TDM_ERROR_BAD_MODULE);
736         TDM_RETURN_VAL_IF_FAIL(func_output->output_set_mode, TDM_ERROR_BAD_MODULE);
737         TDM_RETURN_VAL_IF_FAIL(func_layer->layer_get_capability, TDM_ERROR_BAD_MODULE);
738         TDM_RETURN_VAL_IF_FAIL(func_layer->layer_set_info, TDM_ERROR_BAD_MODULE);
739         TDM_RETURN_VAL_IF_FAIL(func_layer->layer_get_info, TDM_ERROR_BAD_MODULE);
740         TDM_RETURN_VAL_IF_FAIL(func_layer->layer_set_buffer, TDM_ERROR_BAD_MODULE);
741         TDM_RETURN_VAL_IF_FAIL(func_layer->layer_unset_buffer, TDM_ERROR_BAD_MODULE);
742
743         ret = func_display->display_get_capability(private_module->bdata, &private_module->caps_display);
744         if (ret != TDM_ERROR_NONE) {
745                 TDM_ERR("display_get_capability() failed");
746                 return TDM_ERROR_BAD_MODULE;
747         }
748
749         if (private_module->capabilities & TDM_DISPLAY_CAPABILITY_PP) {
750                 tdm_func_pp *func_pp = &private_module->func_pp;
751                 TDM_RETURN_VAL_IF_FAIL(func_display->display_get_pp_capability, TDM_ERROR_BAD_MODULE);
752                 TDM_RETURN_VAL_IF_FAIL(func_display->display_create_pp, TDM_ERROR_BAD_MODULE);
753                 TDM_RETURN_VAL_IF_FAIL(func_pp->pp_destroy, TDM_ERROR_BAD_MODULE);
754                 TDM_RETURN_VAL_IF_FAIL(func_pp->pp_commit, TDM_ERROR_BAD_MODULE);
755                 TDM_RETURN_VAL_IF_FAIL(func_pp->pp_set_done_handler, TDM_ERROR_BAD_MODULE);
756         }
757
758         if (private_module->capabilities & TDM_DISPLAY_CAPABILITY_CAPTURE) {
759                 tdm_func_capture *func_capture = &private_module->func_capture;
760                 TDM_RETURN_VAL_IF_FAIL(func_display->display_get_capture_capability, TDM_ERROR_BAD_MODULE);
761                 if (private_module->caps_capture.capabilities & TDM_CAPTURE_CAPABILITY_OUTPUT)
762                         TDM_RETURN_VAL_IF_FAIL(func_output->output_create_capture, TDM_ERROR_BAD_MODULE);
763                 if (private_module->caps_capture.capabilities & TDM_CAPTURE_CAPABILITY_LAYER)
764                         TDM_RETURN_VAL_IF_FAIL(func_layer->layer_create_capture, TDM_ERROR_BAD_MODULE);
765                 TDM_RETURN_VAL_IF_FAIL(func_capture->capture_destroy, TDM_ERROR_BAD_MODULE);
766                 TDM_RETURN_VAL_IF_FAIL(func_capture->capture_commit, TDM_ERROR_BAD_MODULE);
767                 TDM_RETURN_VAL_IF_FAIL(func_capture->capture_set_done_handler, TDM_ERROR_BAD_MODULE);
768         }
769
770         return TDM_ERROR_NONE;
771 }
772
773 static tdm_error
774 _tdm_display_load_module_with_file(tdm_private_display *private_display,
775                                                                    const char *file)
776 {
777         char path[TDM_PATH_LEN] = {0,};
778         void *module = NULL;
779         tdm_backend_module *module_data;
780         tdm_backend_data *bdata = NULL;
781         tdm_private_module *private_module = NULL;
782         tdm_error ret;
783         double stamp;
784         int size;
785
786         size = snprintf(path, sizeof(path), TDM_MODULE_PATH "/%s", file);
787         if (size >= (int)sizeof(path)) {
788                 TDM_WRN("too long: %s/%s", TDM_MODULE_PATH, file);
789                 return TDM_ERROR_BAD_MODULE;
790         };
791
792         private_module = calloc(1, sizeof *private_module);
793         if (!private_module) {
794                 TDM_ERR("alloc failed: %m");
795                 ret = TDM_ERROR_OUT_OF_MEMORY;
796                 goto failed_load;
797         }
798
799         private_module->private_display = private_display;
800
801         stamp = tdm_helper_get_time();
802
803         TDM_TRACE_BEGIN("TDM_Load_Backend");
804         module = dlopen(path, RTLD_LAZY);
805         if (!module) {
806                 TDM_ERR("%s", dlerror());
807                 TDM_TRACE_END();
808                 ret = TDM_ERROR_BAD_MODULE;
809                 goto failed_load;
810         }
811
812         private_module->module = module;
813
814         module_data = dlsym(module, "tdm_backend_module_data");
815         if (!module_data) {
816                 TDM_ERR("'%s' doesn't have data object", file);
817                 ret = TDM_ERROR_BAD_MODULE;
818                 TDM_TRACE_END();
819                 goto failed_load;
820         }
821         TDM_TRACE_END();
822
823         private_module->module_data = module_data;
824
825         TDM_DBG("dlopen, dlsym time: %.3f ms", (tdm_helper_get_time() - stamp) * 1000.0);
826
827         /* check if version, init() and deinit() are valid or not */
828         ret = _tdm_display_check_module(module_data);
829         if (ret != TDM_ERROR_NONE) {
830                 TDM_ERR("backend(%s) load failed: check module error", module_data->name);
831                 goto failed_load;
832         }
833
834         private_display->current_module = private_module;
835
836         /* We don't care if backend_data is NULL or not. It's up to backend. */
837         TDM_TRACE_BEGIN("TDM_Init_Backend");
838         stamp = tdm_helper_get_time();
839         bdata = module_data->init((tdm_display *)private_display, &ret);
840         TDM_DBG("backend init() time: %.3f ms", (tdm_helper_get_time() - stamp) * 1000.0);
841         TDM_TRACE_END();
842
843         private_display->current_module = NULL;
844
845         private_module->bdata = bdata;
846
847         if (ret != TDM_ERROR_NONE) {
848                 TDM_ERR("backend(%s) load failed: init error", module_data->name);
849                 goto failed_load;
850         }
851
852         ret = _tdm_display_check_backend_functions(private_module);
853         if (ret != TDM_ERROR_NONE) {
854                 TDM_ERR("backend(%s) load failed: check functions error", module_data->name);
855                 goto failed_load;
856         }
857
858         LIST_INITHEAD(&private_module->output_list);
859         LIST_INITHEAD(&private_module->pp_list);
860         LIST_INITHEAD(&private_module->capture_list);
861
862         LIST_ADDTAIL(&private_module->link, &private_display->module_list);
863
864         TDM_INFO("Success to load '%s' module", module_data->name);
865
866         return TDM_ERROR_NONE;
867 failed_load:
868         if (bdata)
869                 module_data->deinit(bdata);
870         if (module)
871                 dlclose(module);
872         if (private_module)
873                 free(private_module);
874         return ret;
875 }
876
877 static tdm_error
878 _tdm_display_load_modules(tdm_private_display *private_display)
879 {
880         const char *module_names;
881         tdm_error ret = TDM_ERROR_NONE;
882         char temp[TDM_PATH_LEN];
883         char *arg;
884         char *end;
885
886         LIST_INITHEAD(&private_display->module_list);
887
888         module_names = tdm_config_get_string(TDM_CONFIG_KEY_GENERAL_BACKENDS, TDM_DEFAULT_MODULE);
889
890         snprintf(temp, TDM_PATH_LEN, "%s", module_names);
891
892         arg = strtok_r(temp, TDM_CONFIG_DELIM, &end);
893         while (arg) {
894                 TDM_INFO("loading a %s backend", arg);
895                 ret = _tdm_display_load_module_with_file(private_display, arg);
896                 if (ret == TDM_ERROR_NONE)
897                         TDM_INFO("%s backend loading success", arg);
898                 else
899                         TDM_INFO("%s backend loading failed", arg);
900                 arg = strtok_r(NULL, TDM_CONFIG_DELIM, &end);
901         }
902
903         /* load bufmgr priv from dummy lib */
904         if (LIST_IS_EMPTY(&private_display->module_list)) {
905                 TDM_WRN("No backend. loading a %s backend", TDM_DUMMY_MODULE);
906                 ret = _tdm_display_load_module_with_file(private_display, TDM_DUMMY_MODULE);
907                 if (ret == TDM_ERROR_NONE)
908                         TDM_INFO("%s backend loading success", TDM_DUMMY_MODULE);
909                 else
910                         TDM_INFO("%s backend loading failed", TDM_DUMMY_MODULE);
911         }
912
913         return ret;
914 }
915
916 static void
917 _tdm_display_unload_modules(tdm_private_display *private_display)
918 {
919         tdm_private_module *private_module = NULL, *bb = NULL;
920
921         LIST_FOR_EACH_ENTRY_SAFE(private_module, bb, &private_display->module_list, link) {
922                 LIST_DEL(&private_module->link);
923
924                 if (private_module->module_data)
925                         private_module->module_data->deinit(private_module->bdata);
926                 if (private_module->module)
927                         dlclose(private_module->module);
928
929                 free(private_module);
930         }
931 }
932 /* LCOV_EXCL_STOP */
933
934 EXTERN tdm_display *
935 tdm_display_init(tdm_error *error)
936 {
937         tdm_private_display *private_display = NULL;
938         const char *str;
939         tdm_error ret;
940         double stamp1, stamp2, start;
941         int mode;
942
943         pthread_mutex_lock(&gLock);
944
945         if (g_private_display) {
946                 g_private_display->init_count++;
947                 pthread_mutex_unlock(&gLock);
948                 if (error)
949                         *error = TDM_ERROR_NONE;
950                 return g_private_display;
951         }
952
953         start = stamp1 = tdm_helper_get_time();
954
955         stamp2 = tdm_helper_get_time();
956         TDM_INFO("config init time: %.3f ms", (stamp2 - stamp1) * 1000.0);
957         stamp1 = stamp2;
958
959         private_display = calloc(1, sizeof(tdm_private_display));
960         if (!private_display) {
961                 /* LCOV_EXCL_START */
962                 ret = TDM_ERROR_OUT_OF_MEMORY;
963                 TDM_ERR("'private_display != NULL' failed");
964                 goto failed_alloc;
965                 /* LCOV_EXCL_STOP */
966         }
967
968         private_display->stamp = tdm_helper_get_time();
969
970         str = tdm_config_get_string(TDM_CONFIG_KEY_DEBUG_MODULE, NULL);
971         if (str)
972                 tdm_display_enable_debug_module(str);
973
974         str = tdm_config_get_string(TDM_CONFIG_KEY_DEBUG_DUMP, NULL);
975         if (str)
976                 tdm_display_enable_dump(private_display, str, NULL, NULL);
977
978         if (pthread_mutex_init(&private_display->lock, NULL)) {
979                 /* LCOV_EXCL_START */
980                 ret = TDM_ERROR_OPERATION_FAILED;
981                 TDM_ERR("mutex init failed: %m");
982                 goto failed_mutex_init;
983                 /* LCOV_EXCL_STOP */
984         }
985
986         _pthread_mutex_lock(&private_display->lock);
987
988         stamp2 = tdm_helper_get_time();
989         TDM_DBG("prepare init time: %.3f ms", (stamp2 - stamp1) * 1000.0);
990         stamp1 = stamp2;
991
992         ret = tdm_event_loop_init(private_display);
993         if (ret != TDM_ERROR_NONE)
994                 goto failed_event;
995
996         stamp2 = tdm_helper_get_time();
997         TDM_INFO("event loop init time: %.3f ms", (stamp2 - stamp1) * 1000.0);
998         stamp1 = stamp2;
999
1000         ret = tdm_vblank_init(private_display);
1001         if (ret != TDM_ERROR_NONE)
1002                 goto failed_vblank;
1003
1004         ret = tdm_output_init(private_display);
1005         if (ret != TDM_ERROR_NONE)
1006                 goto failed_load;
1007
1008         ret = tdm_pp_init(private_display);
1009         if (ret != TDM_ERROR_NONE)
1010                 goto failed_load;
1011
1012         ret = tdm_capture_init(private_display);
1013         if (ret != TDM_ERROR_NONE)
1014                 goto failed_load;
1015
1016         ret = _tdm_display_load_modules(private_display);
1017         if (ret != TDM_ERROR_NONE)
1018                 goto failed_load;
1019
1020         stamp2 = tdm_helper_get_time();
1021         TDM_INFO("loading backend time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1022         stamp1 = stamp2;
1023
1024 #ifdef INIT_BUFMGR
1025         int tdm_drm_fd = tdm_helper_get_fd("TDM_DRM_MASTER_FD");
1026         if (tdm_drm_fd >= 0) {
1027                 private_display->bufmgr = tbm_bufmgr_init(tdm_drm_fd);
1028                 close(tdm_drm_fd);
1029                 if (!private_display->bufmgr) {
1030                         TDM_ERR("tbm_bufmgr_init failed");
1031                         goto failed_update;
1032                 } else {
1033                         TDM_INFO("tbm_bufmgr_init successed");
1034                 }
1035
1036                 stamp2 = tdm_helper_get_time();
1037                 TDM_DBG("initializing bufmgr time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1038                 stamp1 = stamp2;
1039         }
1040 #endif
1041
1042         TDM_TRACE_BEGIN("TDM_Update_Display");
1043         ret = _tdm_display_setup(private_display);
1044         TDM_TRACE_END();
1045         if (ret != TDM_ERROR_NONE)
1046                 goto failed_update;
1047
1048         stamp2 = tdm_helper_get_time();
1049         TDM_DBG("updating display time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1050         stamp1 = stamp2;
1051
1052         tdm_event_loop_create_backend_source(private_display);
1053
1054         private_display->init_count = 1;
1055         g_private_display = private_display;
1056
1057         tdm_private_module *b = NULL;
1058         tdm_private_output *o = NULL;
1059         LIST_FOR_EACH_ENTRY(b, &private_display->module_list, link) {
1060                 LIST_FOR_EACH_ENTRY(o, &b->output_list, link) {
1061                         if (o->caps.capabilities & TDM_OUTPUT_CAPABILITY_HWC)
1062                                 tdm_output_need_validate_event_init(o);
1063                 }
1064         }
1065
1066         /* the COMMIT_PER_VBLANK functionality is ability of an output to support
1067          * several operational modes (commit_per_vblank modes) related to tdm_commit;
1068          * this functionality can be turned off which means a default mode */
1069         mode = tdm_config_get_int(TDM_CONFIG_KEY_GENERAL_COMMIT_PER_VBLANK, 0);
1070         if (mode > 0) {
1071                 tdm_private_module *b = NULL;
1072                 tdm_private_output *o = NULL;
1073
1074                 /* outputs which support hwc capability can work only
1075                  * if commit_per_vblank mode is '0' (default mode) */
1076                 LIST_FOR_EACH_ENTRY(b, &private_display->module_list, link) {
1077                         LIST_FOR_EACH_ENTRY(o, &b->output_list, link) {
1078                                 if (!(o->caps.capabilities & TDM_OUTPUT_CAPABILITY_HWC))
1079                                         tdm_output_choose_commit_per_vblank_mode(o, mode);
1080                         }
1081                 }
1082         }
1083
1084         if (error)
1085                 *error = TDM_ERROR_NONE;
1086
1087         _pthread_mutex_unlock(&private_display->lock);
1088         pthread_mutex_unlock(&gLock);
1089
1090         TDM_INFO("init time: %.3f ms", (tdm_helper_get_time() - start) * 1000.0);
1091
1092         return (tdm_display *)private_display;
1093
1094 /* LCOV_EXCL_START */
1095 failed_update:
1096         _tdm_display_unload_modules(private_display);
1097 failed_load:
1098         tdm_vblank_deinit(private_display);
1099 failed_vblank:
1100         tdm_event_loop_stop(private_display);
1101         tdm_event_loop_deinit(private_display);
1102 failed_event:
1103         _pthread_mutex_unlock(&private_display->lock);
1104         pthread_mutex_destroy(&private_display->lock);
1105 failed_mutex_init:
1106         free(private_display);
1107 failed_alloc:
1108         if (error)
1109                 *error = ret;
1110         pthread_mutex_unlock(&gLock);
1111         return NULL;
1112 /* LCOV_EXCL_STOP */
1113 }
1114
1115 EXTERN void
1116 tdm_display_deinit(tdm_display *dpy)
1117 {
1118         tdm_private_display *private_display = dpy;
1119
1120         if (!private_display)
1121                 return;
1122
1123         pthread_mutex_lock(&gLock);
1124
1125         private_display->init_count--;
1126         if (private_display->init_count > 0) {
1127                 pthread_mutex_unlock(&gLock);
1128                 return;
1129         }
1130
1131         /* dont move the position of lock/unlock. all resource should be protected
1132          * during destroying. after tdm_event_loop_deinit, we don't worry about thread
1133          * things because it's finalized.
1134          */
1135         _pthread_mutex_lock(&private_display->lock);
1136         tdm_event_loop_stop(private_display);
1137         _pthread_mutex_unlock(&private_display->lock);
1138
1139         tdm_event_loop_deinit(private_display);
1140
1141         /* when private_output is destroyed, all vblank resources of client and server
1142          * are destroyed. Then we can call tdm_vblank_deinit. After destroying display,
1143          * we can unload backend modulues.
1144          */
1145         _tdm_display_destroy_private_display(private_display);
1146         _tdm_display_unload_modules(private_display);
1147
1148         tdm_vblank_deinit(private_display);
1149
1150 #ifdef INIT_BUFMGR
1151         if (private_display->bufmgr)
1152                 tbm_bufmgr_deinit(private_display->bufmgr);
1153 #endif
1154
1155         tbm_drm_helper_unset_tbm_master_fd();
1156
1157         pthread_mutex_destroy(&private_display->lock);
1158         free(private_display);
1159         g_private_display = NULL;
1160
1161         if (tdm_debug_dump_dir) {
1162                 free(tdm_debug_dump_dir);
1163                 tdm_debug_dump_dir = NULL;
1164         }
1165
1166         tdm_config_deinit();
1167
1168         pthread_mutex_unlock(&gLock);
1169
1170         TDM_INFO("done");
1171 }
1172
1173 INTERN tdm_private_display *
1174 tdm_display_get(void)
1175 {
1176         return g_private_display;
1177 }
1178