9f41287e646fb9ff97be0c58c74b673bb4004496
[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
458         layers = func_output->output_get_layers(output_backend, &layer_count, &ret);
459         if (ret != TDM_ERROR_NONE) {
460                 free(layers);
461                 return ret;
462         }
463
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) {
467                         free(layers);
468                         return ret;
469                 }
470         }
471
472         free(layers);
473
474         return TDM_ERROR_NONE;
475 }
476
477 static tdm_output **
478 _tdm_display_set_main_first(tdm_output **outputs, int index)
479 {
480         tdm_output *output_tmp = NULL;
481
482         if (index == 0)
483                 return outputs;
484
485         output_tmp = outputs[0];
486         outputs[0] = outputs[index];
487         outputs[index] = output_tmp;
488
489         return outputs;
490 }
491
492 static tdm_output **
493 _tdm_display_get_ordered_outputs(tdm_private_module *private_module, int *count)
494 {
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;
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                 if (!func_output->output_get_capability) {
532                         TDM_ERR("no output_get_capability()");
533                         goto failed_get_outputs;
534                 }
535
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;
540                 }
541
542                 if (caps.status == TDM_OUTPUT_CONN_STATUS_CONNECTED) {
543                         output_connected_count++;
544
545                         switch (caps.type) {
546                         case TDM_OUTPUT_TYPE_DSI:
547                                 output_dsi = outputs[i];
548                                 index_dsi = i;
549                                 break;
550                         case TDM_OUTPUT_TYPE_LVDS:
551                                 output_lvds = outputs[i];
552                                 index_lvds = i;
553                                 break;
554                         case TDM_OUTPUT_TYPE_HDMIA:
555                                 output_hdmia = outputs[i];
556                                 index_hdmia = i;
557                                 break;
558                         case TDM_OUTPUT_TYPE_HDMIB:
559                                 output_hdmib = outputs[i];
560                                 index_hdmib = i;
561                                 break;
562                         default:
563                                 break;
564                         }
565                 }
566
567                 _tdm_display_destroy_caps_output(&caps);
568         }
569
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)
576           */
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);
587                 else
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);
599                 else
600                         new_outputs = outputs;
601         }
602
603         private_module->outputs = new_outputs;
604
605         return new_outputs;
606
607 failed_get_outputs:
608         free(outputs);
609         *count = 0;
610         return NULL;
611 }
612
613 static tdm_error
614 _tdm_display_setup(tdm_private_display *private_display)
615 {
616         tdm_private_module *private_module = NULL;
617         tdm_error ret = TDM_ERROR_NONE;
618         int index = 0;
619
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)
624                         goto failed_update;
625         }
626
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)
631                         goto failed_update;
632         }
633
634         LIST_FOR_EACH_ENTRY(private_module, &private_display->module_list, link) {
635                 tdm_output **outputs;
636                 int output_count = 0, i;
637
638                 outputs = _tdm_display_get_ordered_outputs(private_module, &output_count);
639                 if (!outputs)
640                         goto failed_update;
641
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)
645                                 goto failed_update;
646                 }
647         }
648
649         return TDM_ERROR_NONE;
650
651 failed_update:
652         _tdm_display_destroy_private_display(private_display);
653         return ret;
654 }
655 /* LCOV_EXCL_STOP */
656
657 EXTERN tdm_error
658 tdm_display_update(tdm_display *dpy)
659 {
660         TDM_RETURN_VAL_IF_FAIL(dpy != NULL, TDM_ERROR_INVALID_PARAMETER);
661
662         TDM_DEPRECATED(NULL);
663
664         return TDM_ERROR_NONE;
665 }
666
667 int tdm_debug_module;
668 int tdm_debug_dump;
669 int tdm_ttrace_module;
670 int tdm_ttrace_output;
671
672 static tdm_private_display *g_private_display;
673 static pthread_mutex_t gLock = PTHREAD_MUTEX_INITIALIZER;
674
675 /* LCOV_EXCL_START */
676 static tdm_error
677 _tdm_display_check_module(tdm_backend_module *module)
678 {
679         int major, minor;
680
681         TDM_INFO("TDM ABI version : %d.%d",
682                          TDM_MAJOR_VERSION, TDM_MINOR_VERSION);
683
684         if (!module->name) {
685                 TDM_ERR("TDM backend doesn't have name");
686                 return TDM_ERROR_BAD_MODULE;
687         }
688
689         if (!module->vendor) {
690                 TDM_ERR("TDM backend doesn't have vendor");
691                 return TDM_ERROR_BAD_MODULE;
692         }
693
694         major = TDM_BACKEND_GET_ABI_MAJOR(module->abi_version);
695         minor = TDM_BACKEND_GET_ABI_MINOR(module->abi_version);
696
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);
700
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;
705         }
706
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;
711         }
712
713         if (!module->init) {
714                 TDM_ERR("'%s' doesn't have init function", module->name);
715                 return TDM_ERROR_BAD_MODULE;
716         }
717
718         if (!module->deinit) {
719                 TDM_ERR("'%s' doesn't have deinit function", module->name);
720                 return TDM_ERROR_BAD_MODULE;
721         }
722
723         return TDM_ERROR_NONE;
724 }
725
726 static tdm_error
727 _tdm_display_check_backend_functions(tdm_private_module *private_module)
728 {
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;
732         tdm_error ret;
733
734         /* below functions should be implemented in backend side */
735
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);
751
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;
756         }
757
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);
765         }
766
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);
777         }
778
779         return TDM_ERROR_NONE;
780 }
781
782 static tdm_error
783 _tdm_display_load_module_with_file(tdm_private_display *private_display,
784                                                                    const char *file)
785 {
786         char path[TDM_PATH_LEN] = {0,};
787         void *module = NULL;
788         tdm_backend_module *module_data;
789         tdm_backend_data *bdata = NULL;
790         tdm_private_module *private_module = NULL;
791         tdm_error ret;
792         double stamp;
793         int size;
794
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;
799         };
800
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;
805                 goto failed_load;
806         }
807
808         private_module->private_display = private_display;
809
810         stamp = tdm_helper_get_time();
811
812         TDM_TRACE_BEGIN("TDM_Load_Backend");
813         module = dlopen(path, RTLD_LAZY);
814         if (!module) {
815                 TDM_ERR("%s", dlerror());
816                 TDM_TRACE_END();
817                 ret = TDM_ERROR_BAD_MODULE;
818                 goto failed_load;
819         }
820
821         private_module->module = module;
822
823         module_data = dlsym(module, "tdm_backend_module_data");
824         if (!module_data) {
825                 TDM_ERR("'%s' doesn't have data object", file);
826                 ret = TDM_ERROR_BAD_MODULE;
827                 TDM_TRACE_END();
828                 goto failed_load;
829         }
830         TDM_TRACE_END();
831
832         private_module->module_data = module_data;
833
834         TDM_DBG("dlopen, dlsym time: %.3f ms", (tdm_helper_get_time() - stamp) * 1000.0);
835
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);
840                 goto failed_load;
841         }
842
843         private_display->current_module = private_module;
844
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);
850         TDM_TRACE_END();
851
852         private_display->current_module = NULL;
853
854         private_module->bdata = bdata;
855
856         if (ret != TDM_ERROR_NONE) {
857                 TDM_ERR("backend(%s) load failed: init error", module_data->name);
858                 goto failed_load;
859         }
860
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);
864                 goto failed_load;
865         }
866
867         LIST_INITHEAD(&private_module->output_list);
868         LIST_INITHEAD(&private_module->pp_list);
869         LIST_INITHEAD(&private_module->capture_list);
870
871         LIST_ADDTAIL(&private_module->link, &private_display->module_list);
872
873         TDM_INFO("Success to load '%s' module", module_data->name);
874
875         return TDM_ERROR_NONE;
876 failed_load:
877         if (bdata)
878                 module_data->deinit(bdata);
879         if (module)
880                 dlclose(module);
881         if (private_module)
882                 free(private_module);
883         return ret;
884 }
885
886 static tdm_error
887 _tdm_display_load_modules(tdm_private_display *private_display)
888 {
889         const char *module_names;
890         tdm_error ret = 0;
891         char temp[TDM_PATH_LEN];
892         char *arg;
893         char *end;
894
895         LIST_INITHEAD(&private_display->module_list);
896
897         module_names = tdm_config_get_string(TDM_CONFIG_KEY_GENERAL_BACKENDS, TDM_DEFAULT_MODULE);
898
899         snprintf(temp, TDM_PATH_LEN, "%s", module_names);
900
901         arg = strtok_r(temp, TDM_CONFIG_DELIM, &end);
902         while (arg) {
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);
907                 else
908                         TDM_INFO("%s backend loading failed", arg);
909                 arg = strtok_r(NULL, TDM_CONFIG_DELIM, &end);
910         }
911
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);
918                 else
919                         TDM_INFO("%s backend loading failed", TDM_DUMMY_MODULE);
920         }
921
922         return ret;
923 }
924
925 static void
926 _tdm_display_unload_modules(tdm_private_display *private_display)
927 {
928         tdm_private_module *private_module = NULL, *bb = NULL;
929
930         LIST_FOR_EACH_ENTRY_SAFE(private_module, bb, &private_display->module_list, link) {
931                 LIST_DEL(&private_module->link);
932
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);
937
938                 free(private_module);
939         }
940 }
941 /* LCOV_EXCL_STOP */
942
943 EXTERN tdm_display *
944 tdm_display_init(tdm_error *error)
945 {
946         tdm_private_display *private_display = NULL;
947         const char *str;
948         tdm_error ret;
949         double stamp1, stamp2, start;
950         int mode;
951
952         pthread_mutex_lock(&gLock);
953
954         if (g_private_display) {
955                 g_private_display->init_count++;
956                 pthread_mutex_unlock(&gLock);
957                 if (error)
958                         *error = TDM_ERROR_NONE;
959                 return g_private_display;
960         }
961
962         start = stamp1 = tdm_helper_get_time();
963
964         stamp2 = tdm_helper_get_time();
965         TDM_INFO("config init time: %.3f ms", (stamp2 - stamp1) * 1000.0);
966         stamp1 = stamp2;
967
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");
973                 goto failed_alloc;
974                 /* LCOV_EXCL_STOP */
975         }
976
977         private_display->stamp = tdm_helper_get_time();
978
979         str = tdm_config_get_string(TDM_CONFIG_KEY_DEBUG_MODULE, NULL);
980         if (str)
981                 tdm_display_enable_debug_module(str);
982
983         str = tdm_config_get_string(TDM_CONFIG_KEY_DEBUG_DUMP, NULL);
984         if (str)
985                 tdm_display_enable_dump(private_display, str, NULL, NULL);
986
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;
992                 /* LCOV_EXCL_STOP */
993         }
994
995         _pthread_mutex_lock(&private_display->lock);
996
997         stamp2 = tdm_helper_get_time();
998         TDM_DBG("prepare init time: %.3f ms", (stamp2 - stamp1) * 1000.0);
999         stamp1 = stamp2;
1000
1001         ret = tdm_event_loop_init(private_display);
1002         if (ret != TDM_ERROR_NONE)
1003                 goto failed_event;
1004
1005         stamp2 = tdm_helper_get_time();
1006         TDM_INFO("event loop init time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1007         stamp1 = stamp2;
1008
1009         ret = tdm_vblank_init(private_display);
1010         if (ret != TDM_ERROR_NONE)
1011                 goto failed_vblank;
1012
1013         ret = tdm_output_init(private_display);
1014         if (ret != TDM_ERROR_NONE)
1015                 goto failed_load;
1016
1017         ret = tdm_pp_init(private_display);
1018         if (ret != TDM_ERROR_NONE)
1019                 goto failed_load;
1020
1021         ret = tdm_capture_init(private_display);
1022         if (ret != TDM_ERROR_NONE)
1023                 goto failed_load;
1024
1025         ret = _tdm_display_load_modules(private_display);
1026         if (ret != TDM_ERROR_NONE)
1027                 goto failed_load;
1028
1029         stamp2 = tdm_helper_get_time();
1030         TDM_INFO("loading backend time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1031         stamp1 = stamp2;
1032
1033 #ifdef INIT_BUFMGR
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);
1037                 close(tdm_drm_fd);
1038                 if (!private_display->bufmgr) {
1039                         TDM_ERR("tbm_bufmgr_init failed");
1040                         goto failed_update;
1041                 } else {
1042                         TDM_INFO("tbm_bufmgr_init successed");
1043                 }
1044
1045                 stamp2 = tdm_helper_get_time();
1046                 TDM_DBG("initializing bufmgr time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1047                 stamp1 = stamp2;
1048         }
1049 #endif
1050
1051         TDM_TRACE_BEGIN("TDM_Update_Display");
1052         ret = _tdm_display_setup(private_display);
1053         TDM_TRACE_END();
1054         if (ret != TDM_ERROR_NONE)
1055                 goto failed_update;
1056
1057         stamp2 = tdm_helper_get_time();
1058         TDM_DBG("updating display time: %.3f ms", (stamp2 - stamp1) * 1000.0);
1059         stamp1 = stamp2;
1060
1061         tdm_event_loop_create_backend_source(private_display);
1062
1063         private_display->init_count = 1;
1064         g_private_display = private_display;
1065
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);
1072                 }
1073         }
1074
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);
1079         if (mode > 0) {
1080                 tdm_private_module *b = NULL;
1081                 tdm_private_output *o = NULL;
1082
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);
1089                         }
1090                 }
1091         }
1092
1093         if (error)
1094                 *error = TDM_ERROR_NONE;
1095
1096         _pthread_mutex_unlock(&private_display->lock);
1097         pthread_mutex_unlock(&gLock);
1098
1099         TDM_INFO("init time: %.3f ms", (tdm_helper_get_time() - start) * 1000.0);
1100
1101         return (tdm_display *)private_display;
1102
1103 /* LCOV_EXCL_START */
1104 failed_update:
1105         _tdm_display_unload_modules(private_display);
1106 failed_load:
1107         tdm_vblank_deinit(private_display);
1108 failed_vblank:
1109         tdm_event_loop_stop(private_display);
1110         tdm_event_loop_deinit(private_display);
1111 failed_event:
1112         _pthread_mutex_unlock(&private_display->lock);
1113         pthread_mutex_destroy(&private_display->lock);
1114 failed_mutex_init:
1115         free(private_display);
1116 failed_alloc:
1117         if (error)
1118                 *error = ret;
1119         pthread_mutex_unlock(&gLock);
1120         return NULL;
1121 /* LCOV_EXCL_STOP */
1122 }
1123
1124 EXTERN void
1125 tdm_display_deinit(tdm_display *dpy)
1126 {
1127         tdm_private_display *private_display = dpy;
1128
1129         if (!private_display)
1130                 return;
1131
1132         pthread_mutex_lock(&gLock);
1133
1134         private_display->init_count--;
1135         if (private_display->init_count > 0) {
1136                 pthread_mutex_unlock(&gLock);
1137                 return;
1138         }
1139
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.
1143          */
1144         _pthread_mutex_lock(&private_display->lock);
1145         tdm_event_loop_stop(private_display);
1146         _pthread_mutex_unlock(&private_display->lock);
1147
1148         tdm_event_loop_deinit(private_display);
1149
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.
1153          */
1154         _tdm_display_destroy_private_display(private_display);
1155         _tdm_display_unload_modules(private_display);
1156
1157         tdm_vblank_deinit(private_display);
1158
1159 #ifdef INIT_BUFMGR
1160         if (private_display->bufmgr)
1161                 tbm_bufmgr_deinit(private_display->bufmgr);
1162 #endif
1163
1164         tbm_drm_helper_unset_tbm_master_fd();
1165
1166         pthread_mutex_destroy(&private_display->lock);
1167         free(private_display);
1168         g_private_display = NULL;
1169
1170         if (tdm_debug_dump_dir) {
1171                 free(tdm_debug_dump_dir);
1172                 tdm_debug_dump_dir = NULL;
1173         }
1174
1175         tdm_config_deinit();
1176
1177         pthread_mutex_unlock(&gLock);
1178
1179         TDM_INFO("done");
1180 }
1181
1182 INTERN tdm_private_display *
1183 tdm_display_get(void)
1184 {
1185         return g_private_display;
1186 }
1187