63fe459b6a05583b9ff9a31775308feee3eed0ba
[platform/core/uifw/libtdm.git] / src / tdm_output.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 <sc1.lim@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 #define COUNT_MAX   10
43
44 #define OUTPUT_FUNC_ENTRY() \
45         tdm_private_display *private_display; \
46         tdm_private_output *private_output; \
47         tdm_error ret = TDM_ERROR_NONE; /* default TDM_ERROR_NONE */\
48         TDM_RETURN_VAL_IF_FAIL(tdm_output_is_valid(output), TDM_ERROR_INVALID_PARAMETER); \
49         private_output = (tdm_private_output*)output; \
50         private_display = private_output->private_display
51
52 #define OUTPUT_FUNC_ENTRY_ERROR() \
53         tdm_private_display *private_display; \
54         tdm_private_output *private_output; \
55         tdm_error ret = TDM_ERROR_NONE; /* default TDM_ERROR_NONE */\
56         TDM_RETURN_VAL_IF_FAIL_WITH_ERROR(tdm_output_is_valid(output), TDM_ERROR_INVALID_PARAMETER, NULL); \
57         private_output = (tdm_private_output*)output; \
58         private_display = private_output->private_display
59
60 static void
61 _tdm_output_vblank_timeout_update(tdm_private_output *private_output, int ms_delay);
62
63 static tdm_error
64 _tdm_output_vblank_timeout_cb(void *user_data)
65 {
66         tdm_private_output *private_output = user_data;
67         tdm_private_vblank_handler *v = NULL;
68
69         TDM_RETURN_VAL_IF_FAIL(private_output != NULL, TDM_ERROR_OPERATION_FAILED);
70
71         TDM_ERR("TDM output(%d) vblank TIMEOUT!!", private_output->pipe);
72
73         LIST_FOR_EACH_ENTRY(v, &private_output->vblank_handler_list, link) {
74                 TDM_ERR("vblank_handler(%p) interval(%d) sync(%d) sent_to_frontend(%u) owner_tid(%d)",
75                                 v, v->interval, v->sync, v->sent_to_frontend, v->owner_tid);
76         }
77
78         return TDM_ERROR_NONE;
79 }
80
81 INTERN void
82 tdm_output_vblank_print_wait_information(tdm_private_output *private_output, void *user_data)
83 {
84         tdm_private_vblank_handler *v = NULL;
85
86         TDM_RETURN_IF_FAIL(private_output != NULL);
87         TDM_RETURN_IF_FAIL(user_data != NULL);
88
89         TDM_ERR("TDM output(%d) vblank user_data(%p) info!!", private_output->pipe, user_data);
90
91         LIST_FOR_EACH_ENTRY(v, &private_output->vblank_handler_list, link) {
92                 if (v->user_data != user_data)
93                         continue;
94                 TDM_ERR("vblank_handler(%p) interval(%d) sync(%d) sent_to_frontend(%u) owner_tid(%d)",
95                                 v, v->interval, v->sync, v->sent_to_frontend, v->owner_tid);
96         }
97 }
98
99 static void
100 _tdm_output_vblank_timeout_update(tdm_private_output *private_output, int ms_delay)
101 {
102         tdm_error ret;
103
104         if (!private_output->vblank_timeout_timer) {
105                 private_output->vblank_timeout_timer =
106                         tdm_event_loop_add_timer_handler(private_output->private_display,
107                                                                                          _tdm_output_vblank_timeout_cb,
108                                                                                          private_output,
109                                                                                          &ret);
110                 if (!private_output->vblank_timeout_timer) {
111                         TDM_ERR("output(%d) couldn't add timer", private_output->pipe);
112                         return;
113                 }
114                 TDM_INFO("output(%d) create vblank timeout timer", private_output->pipe);
115         }
116
117         ret = tdm_event_loop_source_timer_update(private_output->vblank_timeout_timer, ms_delay);
118         if (ret != TDM_ERROR_NONE) {
119                 TDM_ERR("output(%d) couldn't update timer", private_output->pipe);
120                 return;
121         }
122 }
123
124 static tdm_private_hwc_window *
125 _tdm_output_find_private_hwc_window(tdm_private_output *private_output,
126                                                                 tdm_hwc_window *hwc_window_backend)
127 {
128         tdm_private_hwc_window *private_hwc_window = NULL;
129
130         LIST_FOR_EACH_ENTRY(private_hwc_window, &private_output->hwc_window_list, link) {
131                 if (private_hwc_window->hwc_window_backend == hwc_window_backend)
132                         return private_hwc_window;
133         }
134
135         return NULL;
136 }
137
138 EXTERN tdm_error
139 tdm_output_get_model_info(tdm_output *output, const char **maker,
140                                                   const char **model, const char **name)
141 {
142         OUTPUT_FUNC_ENTRY();
143
144         _pthread_mutex_lock(&private_display->lock);
145
146         if (maker)
147                 *maker = private_output->caps.maker;
148         if (model)
149                 *model = private_output->caps.model;
150         if (name)
151                 *name = private_output->caps.name;
152
153         _pthread_mutex_unlock(&private_display->lock);
154
155         return ret;
156 }
157
158 EXTERN tdm_error
159 tdm_output_get_capabilities(tdm_output *output, tdm_output_capability *capabilities)
160 {
161         OUTPUT_FUNC_ENTRY();
162
163         TDM_RETURN_VAL_IF_FAIL(capabilities != NULL, TDM_ERROR_INVALID_PARAMETER);
164
165         _pthread_mutex_lock(&private_display->lock);
166
167         *capabilities = private_output->caps.capabilities;
168
169         _pthread_mutex_unlock(&private_display->lock);
170
171         return ret;
172 }
173
174 EXTERN tdm_error
175 tdm_output_get_conn_status(tdm_output *output, tdm_output_conn_status *status)
176 {
177         OUTPUT_FUNC_ENTRY();
178
179         TDM_RETURN_VAL_IF_FAIL(status != NULL, TDM_ERROR_INVALID_PARAMETER);
180
181         _pthread_mutex_lock(&private_display->lock);
182
183         *status = private_output->caps.status;
184
185         _pthread_mutex_unlock(&private_display->lock);
186
187         return ret;
188 }
189
190 /* LCOV_EXCL_START */
191 static void
192 _tdm_output_update(tdm_output *output_backend, void *user_data)
193 {
194         tdm_private_display *private_display;
195         tdm_private_output *private_output = user_data;
196         tdm_error ret;
197
198         TDM_RETURN_IF_FAIL(private_output);
199
200         private_display = private_output->private_display;
201
202         ret = tdm_display_update_output(private_display, output_backend, private_output->pipe);
203         TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
204 }
205
206 INTERN void
207 tdm_output_cb_status(tdm_output *output_backend, tdm_output_conn_status status,
208                                          void *user_data)
209 {
210         tdm_private_display *private_display;
211         tdm_private_output *private_output = user_data;
212         tdm_value value;
213
214         TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
215         TDM_RETURN_IF_FAIL(private_output);
216
217         private_display = private_output->private_display;
218
219         if (!tdm_thread_in_display_thread(syscall(SYS_gettid))) {
220                 tdm_thread_cb_output_status output_status;
221                 tdm_error ret;
222
223                 TDM_INFO("output(%d) %s", private_output->pipe, tdm_status_str(status));
224
225                 _tdm_output_update(output_backend, user_data);
226                 private_output->caps.status = status;
227
228                 output_status.base.type = TDM_THREAD_CB_OUTPUT_STATUS;
229                 output_status.base.length = sizeof output_status;
230                 output_status.output_stamp = private_output->stamp;
231                 output_status.status = status;
232                 output_status.user_data = user_data;
233
234                 value.u32 = status;
235                 tdm_output_call_change_handler_internal(private_output,
236                                                                                                 &private_output->change_handler_list_sub,
237                                                                                                 TDM_OUTPUT_CHANGE_CONNECTION,
238                                                                                                 value, 0);
239
240                 ret = tdm_thread_send_cb(private_display->private_loop, &output_status.base);
241                 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
242
243                 return;
244         }
245
246         TDM_INFO("output(%d) %s", private_output->pipe, tdm_status_str(status));
247
248         if (!tdm_thread_is_running())
249                 _tdm_output_update(output_backend, user_data);
250         private_output->caps.status = status;
251
252         value.u32 = status;
253         tdm_output_call_change_handler_internal(private_output,
254                                                                                         &private_output->change_handler_list_main,
255                                                                                         TDM_OUTPUT_CHANGE_CONNECTION,
256                                                                                         value, 0);
257 }
258 /* LCOV_EXCL_STOP */
259
260 EXTERN tdm_error
261 tdm_output_add_change_handler(tdm_output *output,
262                                                           tdm_output_change_handler func,
263                                                           void *user_data)
264 {
265         tdm_private_change_handler *change_handler;
266         OUTPUT_FUNC_ENTRY();
267
268         TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
269
270         _pthread_mutex_lock(&private_display->lock);
271
272         change_handler = calloc(1, sizeof(tdm_private_change_handler));
273         if (!change_handler) {
274                 /* LCOV_EXCL_START */
275                 TDM_ERR("failed: alloc memory");
276                 _pthread_mutex_unlock(&private_display->lock);
277                 return TDM_ERROR_OUT_OF_MEMORY;
278                 /* LCOV_EXCL_STOP */
279         }
280
281         change_handler->private_output = private_output;
282         change_handler->func = func;
283         change_handler->user_data = user_data;
284         change_handler->owner_tid = syscall(SYS_gettid);
285
286         if (!tdm_thread_in_display_thread(change_handler->owner_tid))
287                 LIST_ADDTAIL(&change_handler->link, &private_output->change_handler_list_sub);
288         else
289                 LIST_ADDTAIL(&change_handler->link, &private_output->change_handler_list_main);
290
291         _pthread_mutex_unlock(&private_display->lock);
292
293         return ret;
294 }
295
296 EXTERN void
297 tdm_output_remove_change_handler(tdm_output *output,
298                                                                  tdm_output_change_handler func,
299                                                                  void *user_data)
300 {
301         tdm_private_display *private_display;
302         tdm_private_output *private_output;
303         tdm_private_change_handler *h = NULL, *hh = NULL;
304
305         TDM_RETURN_IF_FAIL(tdm_output_is_valid(output));
306         TDM_RETURN_IF_FAIL(func != NULL);
307
308         private_output = (tdm_private_output*)output;
309         private_display = private_output->private_display;
310
311         _pthread_mutex_lock(&private_display->lock);
312
313         LIST_FOR_EACH_ENTRY_SAFE(h, hh, &private_output->change_handler_list_main, link) {
314                 if (h->func != func || h->user_data != user_data)
315                         continue;
316
317                 LIST_DEL(&h->link);
318                 free(h);
319
320                 _pthread_mutex_unlock(&private_display->lock);
321
322                 return;
323         }
324
325         LIST_FOR_EACH_ENTRY_SAFE(h, hh, &private_output->change_handler_list_sub, link) {
326                 if (h->func != func || h->user_data != user_data)
327                         continue;
328
329                 LIST_DEL(&h->link);
330                 free(h);
331
332                 _pthread_mutex_unlock(&private_display->lock);
333
334                 return;
335         }
336
337         _pthread_mutex_unlock(&private_display->lock);
338 }
339
340 EXTERN tdm_error
341 tdm_output_get_output_type(tdm_output *output, tdm_output_type *type)
342 {
343         OUTPUT_FUNC_ENTRY();
344
345         TDM_RETURN_VAL_IF_FAIL(type != NULL, TDM_ERROR_INVALID_PARAMETER);
346
347         _pthread_mutex_lock(&private_display->lock);
348
349         *type = private_output->caps.type;
350
351         _pthread_mutex_unlock(&private_display->lock);
352
353         return ret;
354 }
355
356 EXTERN tdm_error
357 tdm_output_get_layer_count(tdm_output *output, int *count)
358 {
359         tdm_private_layer *private_layer = NULL;
360
361         OUTPUT_FUNC_ENTRY();
362
363         TDM_RETURN_VAL_IF_FAIL(count != NULL, TDM_ERROR_INVALID_PARAMETER);
364
365         _pthread_mutex_lock(&private_display->lock);
366
367         if (private_output->caps.capabilities & TDM_OUTPUT_CAPABILITY_HWC) {
368                 TDM_ERR("output(%p) support HWC. Use HWC functions", private_output);
369                 *count = 0;
370                 _pthread_mutex_unlock(&private_display->lock);
371                 return TDM_ERROR_BAD_REQUEST;
372         }
373
374         *count = 0;
375         LIST_FOR_EACH_ENTRY(private_layer, &private_output->layer_list, link)
376         (*count)++;
377         if (*count == 0) {
378                 _pthread_mutex_unlock(&private_display->lock);
379                 return TDM_ERROR_NONE;
380         }
381
382         _pthread_mutex_unlock(&private_display->lock);
383
384         return ret;
385 }
386
387
388 EXTERN tdm_layer *
389 tdm_output_get_layer(tdm_output *output, int index, tdm_error *error)
390 {
391         tdm_private_layer *private_layer = NULL;
392
393         OUTPUT_FUNC_ENTRY_ERROR();
394
395         _pthread_mutex_lock(&private_display->lock);
396
397         if (error)
398                 *error = TDM_ERROR_NONE;
399
400         if (private_output->caps.capabilities & TDM_OUTPUT_CAPABILITY_HWC) {
401                 _pthread_mutex_unlock(&private_display->lock);
402                 TDM_ERR("output(%p) support HWC. Use HWC functions", private_output);
403                 if (error)
404                         *error = TDM_ERROR_BAD_REQUEST;
405                 return NULL;
406         }
407
408         LIST_FOR_EACH_ENTRY(private_layer, &private_output->layer_list, link) {
409                 if (private_layer->index == index) {
410                         _pthread_mutex_unlock(&private_display->lock);
411                         return private_layer;
412                 }
413         }
414
415         _pthread_mutex_unlock(&private_display->lock);
416
417         return NULL;
418 }
419
420 EXTERN tdm_error
421 tdm_output_get_available_properties(tdm_output *output, const tdm_prop **props,
422                                                                         int *count)
423 {
424         OUTPUT_FUNC_ENTRY();
425
426         TDM_RETURN_VAL_IF_FAIL(props != NULL, TDM_ERROR_INVALID_PARAMETER);
427         TDM_RETURN_VAL_IF_FAIL(count != NULL, TDM_ERROR_INVALID_PARAMETER);
428
429         _pthread_mutex_lock(&private_display->lock);
430
431         *props = (const tdm_prop *)private_output->caps.props;
432         *count = private_output->caps.prop_count;
433
434         _pthread_mutex_unlock(&private_display->lock);
435
436         return ret;
437 }
438
439 EXTERN tdm_error
440 tdm_output_get_available_modes(tdm_output *output,
441                                                            const tdm_output_mode **modes, int *count)
442 {
443         OUTPUT_FUNC_ENTRY();
444
445         TDM_RETURN_VAL_IF_FAIL(modes != NULL, TDM_ERROR_INVALID_PARAMETER);
446         TDM_RETURN_VAL_IF_FAIL(count != NULL, TDM_ERROR_INVALID_PARAMETER);
447
448         _pthread_mutex_lock(&private_display->lock);
449
450         *modes = (const tdm_output_mode *)private_output->caps.modes;
451         *count = private_output->caps.mode_count;
452
453         _pthread_mutex_unlock(&private_display->lock);
454
455         return ret;
456 }
457
458 EXTERN tdm_error
459 tdm_output_get_available_size(tdm_output *output, int *min_w, int *min_h,
460                                                           int *max_w, int *max_h, int *preferred_align)
461 {
462         OUTPUT_FUNC_ENTRY();
463
464         _pthread_mutex_lock(&private_display->lock);
465
466         if (min_w)
467                 *min_w = TDM_FRONT_VALUE(private_output->caps.min_w);
468         if (min_h)
469                 *min_h = TDM_FRONT_VALUE(private_output->caps.min_h);
470         if (max_w)
471                 *max_w = TDM_FRONT_VALUE(private_output->caps.max_w);
472         if (max_h)
473                 *max_h = TDM_FRONT_VALUE(private_output->caps.max_h);
474         if (preferred_align)
475                 *preferred_align = TDM_FRONT_VALUE(private_output->caps.preferred_align);
476
477         _pthread_mutex_unlock(&private_display->lock);
478
479         return ret;
480 }
481
482 EXTERN tdm_error
483 tdm_output_get_cursor_available_size(tdm_output *output, int *min_w, int *min_h,
484                                                                          int *max_w, int *max_h, int *preferred_align)
485 {
486         OUTPUT_FUNC_ENTRY();
487
488         _pthread_mutex_lock(&private_display->lock);
489
490         if (!tdm_display_check_module_abi(private_display, 1, 5)) {
491
492                 if (min_w)
493                         *min_w = -1;
494                 if (min_h)
495                         *min_h = -1;
496                 if (max_w)
497                         *max_w = -1;
498                 if (max_h)
499                         *max_h = -1;
500                 if (preferred_align)
501                         *preferred_align = -1;
502
503                 _pthread_mutex_unlock(&private_display->lock);
504
505                 return TDM_ERROR_BAD_MODULE;
506         }
507
508         if (min_w)
509                 *min_w = TDM_FRONT_VALUE(private_output->caps.cursor_min_w);
510         if (min_h)
511                 *min_h = TDM_FRONT_VALUE(private_output->caps.cursor_min_h);
512         if (max_w)
513                 *max_w = TDM_FRONT_VALUE(private_output->caps.cursor_max_w);
514         if (max_h)
515                 *max_h = TDM_FRONT_VALUE(private_output->caps.cursor_max_h);
516         if (preferred_align)
517                 *preferred_align = TDM_FRONT_VALUE(private_output->caps.cursor_preferred_align);
518
519         _pthread_mutex_unlock(&private_display->lock);
520
521         return ret;
522 }
523
524 EXTERN tdm_error
525 tdm_output_get_physical_size(tdm_output *output, unsigned int *mmWidth,
526                                                          unsigned int *mmHeight)
527 {
528         OUTPUT_FUNC_ENTRY();
529
530         _pthread_mutex_lock(&private_display->lock);
531
532         if (mmWidth)
533                 *mmWidth = private_output->caps.mmWidth;
534         if (mmHeight)
535                 *mmHeight = private_output->caps.mmHeight;
536
537         _pthread_mutex_unlock(&private_display->lock);
538
539         return ret;
540 }
541
542 EXTERN tdm_error
543 tdm_output_get_subpixel(tdm_output *output, unsigned int *subpixel)
544 {
545         OUTPUT_FUNC_ENTRY();
546         TDM_RETURN_VAL_IF_FAIL(subpixel != NULL, TDM_ERROR_INVALID_PARAMETER);
547
548         _pthread_mutex_lock(&private_display->lock);
549
550         *subpixel = private_output->caps.subpixel;
551
552         _pthread_mutex_unlock(&private_display->lock);
553
554         return ret;
555 }
556
557 EXTERN tdm_error
558 tdm_output_get_pipe(tdm_output *output, unsigned int *pipe)
559 {
560         OUTPUT_FUNC_ENTRY();
561         TDM_RETURN_VAL_IF_FAIL(pipe != NULL, TDM_ERROR_INVALID_PARAMETER);
562
563         _pthread_mutex_lock(&private_display->lock);
564
565         *pipe = private_output->pipe;
566
567         _pthread_mutex_unlock(&private_display->lock);
568
569         return ret;
570 }
571
572 EXTERN tdm_error
573 tdm_output_get_primary_index(tdm_output *output, int *index)
574 {
575         tdm_private_layer *private_layer = NULL;
576
577         OUTPUT_FUNC_ENTRY();
578         TDM_RETURN_VAL_IF_FAIL(index != NULL, TDM_ERROR_INVALID_PARAMETER);
579
580         _pthread_mutex_lock(&private_display->lock);
581
582         LIST_FOR_EACH_ENTRY(private_layer, &private_output->layer_list, link) {
583                 if (private_layer->caps.capabilities & TDM_LAYER_CAPABILITY_PRIMARY) {
584                         *index = private_layer->index;
585                         break;
586                 }
587         }
588
589         _pthread_mutex_unlock(&private_display->lock);
590
591         return ret;
592 }
593
594 EXTERN tdm_error
595 tdm_output_set_property(tdm_output *output, unsigned int id, tdm_value value)
596 {
597         tdm_func_output *func_output;
598         OUTPUT_FUNC_ENTRY();
599
600         _pthread_mutex_lock(&private_display->lock);
601
602         func_output = &private_display->func_output;
603
604         if (!func_output->output_set_property) {
605                 /* LCOV_EXCL_START */
606                 _pthread_mutex_unlock(&private_display->lock);
607                 TDM_ERR("not implemented!!");
608                 return TDM_ERROR_NOT_IMPLEMENTED;
609                 /* LCOV_EXCL_STOP */
610         }
611
612         ret = func_output->output_set_property(private_output->output_backend, id,
613                                                                                    value);
614
615         _pthread_mutex_unlock(&private_display->lock);
616
617         return ret;
618 }
619
620 EXTERN tdm_error
621 tdm_output_get_property(tdm_output *output, unsigned int id, tdm_value *value)
622 {
623         tdm_func_output *func_output;
624         OUTPUT_FUNC_ENTRY();
625
626         TDM_RETURN_VAL_IF_FAIL(value != NULL, TDM_ERROR_INVALID_PARAMETER);
627
628         _pthread_mutex_lock(&private_display->lock);
629
630         func_output = &private_display->func_output;
631
632         if (!func_output->output_get_property) {
633                 /* LCOV_EXCL_START */
634                 _pthread_mutex_unlock(&private_display->lock);
635                 TDM_ERR("not implemented!!");
636                 return TDM_ERROR_NOT_IMPLEMENTED;
637                 /* LCOV_EXCL_STOP */
638         }
639
640         ret = func_output->output_get_property(private_output->output_backend, id,
641                                                                                    value);
642
643         _pthread_mutex_unlock(&private_display->lock);
644
645         return ret;
646 }
647
648 INTERN void
649 tdm_output_cb_vblank(tdm_output *output_backend, unsigned int sequence,
650                                          unsigned int tv_sec, unsigned int tv_usec, void *user_data)
651 {
652         tdm_private_vblank_handler *vblank_handler = user_data;
653         tdm_private_vblank_handler *v = NULL, *vv = NULL;
654         tdm_private_output *private_output;
655         tdm_private_display *private_display;
656         struct list_head clone_list;
657         int interval, sync;
658         pid_t tid = syscall(SYS_gettid);
659
660         TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
661         TDM_RETURN_IF_FAIL(vblank_handler);
662
663         private_output = vblank_handler->private_output;
664         private_display = private_output->private_display;
665
666         if (vblank_handler->owner_tid != tid) {
667                 tdm_thread_cb_output_vblank output_vblank;
668                 tdm_error ret;
669
670                 vblank_handler->sent_to_frontend = 1;
671
672                 output_vblank.base.type = TDM_THREAD_CB_OUTPUT_VBLANK;
673                 output_vblank.base.length = sizeof output_vblank;
674                 output_vblank.output_stamp = vblank_handler->private_output->stamp;
675                 output_vblank.sequence = sequence;
676                 output_vblank.tv_sec = tv_sec;
677                 output_vblank.tv_usec = tv_usec;
678                 output_vblank.user_data = user_data;
679
680                 ret = tdm_thread_send_cb(private_display->private_loop, &output_vblank.base);
681                 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
682
683                 return;
684         }
685
686         vblank_handler->sent_to_frontend = 0;
687
688         _tdm_output_vblank_timeout_update(private_output, 0);
689
690         if (vblank_handler->owner_tid != tid)
691                 TDM_NEVER_GET_HERE();
692
693         interval = vblank_handler->interval;
694         sync = vblank_handler->sync;
695
696         LIST_INITHEAD(&clone_list);
697
698         LIST_FOR_EACH_ENTRY_SAFE(v, vv, &private_output->vblank_handler_list, link) {
699                 if (v->interval != interval || v->sync != sync || v->owner_tid != tid)
700                         continue;
701
702                 LIST_DEL(&v->link);
703                 LIST_ADDTAIL(&v->link, &clone_list);
704         }
705
706         if (tdm_debug_module & TDM_DEBUG_COMMIT)
707                 TDM_INFO("----------------------------------------- output(%d) got vblank", private_output->pipe);
708
709         _pthread_mutex_unlock(&private_display->lock);
710         LIST_FOR_EACH_ENTRY_SAFE(v, vv, &clone_list, link) {
711                 if (tdm_debug_module & TDM_DEBUG_COMMIT)
712                         TDM_INFO("handler(%p)", v);
713
714                 LIST_DEL(&v->link);
715
716                 if (v->func)
717                         v->func(v->private_output, sequence, tv_sec, tv_usec, v->user_data);
718
719                 free(v);
720         }
721         _pthread_mutex_lock(&private_display->lock);
722
723         if (tdm_debug_module & TDM_DEBUG_COMMIT)
724                 TDM_INFO("-----------------------------------------...");
725 }
726
727 INTERN void
728 tdm_output_cb_commit(tdm_output *output_backend, unsigned int sequence,
729                                          unsigned int tv_sec, unsigned int tv_usec, void *user_data)
730 {
731         tdm_private_output_commit_handler *output_commit_handler = user_data;
732         tdm_private_display *private_display;
733         tdm_private_output *private_output;
734         tdm_private_layer *private_layer = NULL;
735
736         TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
737
738         if (!output_commit_handler)
739                 return;
740
741         private_output = output_commit_handler->private_output;
742         private_display = private_output->private_display;
743
744         if (output_commit_handler->owner_tid != syscall(SYS_gettid)) {
745                 tdm_thread_cb_output_commit output_commit;
746                 tdm_error ret;
747
748                 output_commit.base.type = TDM_THREAD_CB_OUTPUT_COMMIT;
749                 output_commit.base.length = sizeof output_commit;
750                 output_commit.output_stamp = private_output->stamp;
751                 output_commit.sequence = sequence;
752                 output_commit.tv_sec = tv_sec;
753                 output_commit.tv_usec = tv_usec;
754                 output_commit.user_data = user_data;
755
756                 ret = tdm_thread_send_cb(private_display->private_loop, &output_commit.base);
757                 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
758
759                 return;
760         }
761
762         LIST_DEL(&output_commit_handler->link);
763
764         if (tdm_debug_module & TDM_DEBUG_COMMIT) {
765                 TDM_INFO("----------------------------------------- output(%d) committed", private_output->pipe);
766                 TDM_INFO("handler(%p)", output_commit_handler);
767         }
768
769         if (private_output->commit_type == TDM_COMMIT_TYPE_OUTPUT) {
770                 /* In case of layer commit, the below will be handled in the layer commit callback */
771                 LIST_FOR_EACH_ENTRY(private_layer, &private_output->layer_list, link) {
772                         if (private_layer->committed_buffer)
773                                 tdm_layer_committed(private_layer, &private_layer->committed_buffer);
774                 }
775         }
776
777         if (output_commit_handler->func) {
778                 _pthread_mutex_unlock(&private_display->lock);
779                 output_commit_handler->func(private_output, sequence,
780                                                                         tv_sec, tv_usec, output_commit_handler->user_data);
781                 _pthread_mutex_lock(&private_display->lock);
782         }
783
784         free(output_commit_handler);
785
786         if (tdm_debug_module & TDM_DEBUG_COMMIT)
787                 TDM_INFO("-----------------------------------------...");
788 }
789
790 /* add_front: To distinguish between the user vblank handlers and the layer
791  *            commit vblank handlers. The layer commit handlers will be called
792  *            before calling the user vblank handlers.
793  */
794 static tdm_error
795 _tdm_output_wait_vblank(tdm_output *output, int interval, int sync,
796                                                 tdm_output_vblank_handler func, void *user_data,
797                                                 unsigned int add_front)
798 {
799         tdm_func_output *func_output;
800         tdm_private_vblank_handler *vblank_handler = NULL, *v = NULL;
801         unsigned int skip_request = 0;
802         pid_t tid = syscall(SYS_gettid);
803
804         OUTPUT_FUNC_ENTRY();
805
806         func_output = &private_display->func_output;
807
808         /* interval SHOULD be at least 1 */
809         if (interval <= 0)
810                 interval = 1;
811
812         if (!func_output->output_wait_vblank) {
813                 /* LCOV_EXCL_START */
814                 TDM_ERR("not implemented!!");
815                 return TDM_ERROR_NOT_IMPLEMENTED;
816                 /* LCOV_EXCL_STOP */
817         }
818
819         if (!private_output->regist_vblank_cb) {
820                 private_output->regist_vblank_cb = 1;
821                 ret = func_output->output_set_vblank_handler(private_output->output_backend,
822                                 tdm_output_cb_vblank);
823         }
824
825         vblank_handler = calloc(1, sizeof(tdm_private_vblank_handler));
826         if (!vblank_handler) {
827                 /* LCOV_EXCL_START */
828                 TDM_ERR("failed: alloc memory");
829                 return TDM_ERROR_OUT_OF_MEMORY;
830                 /* LCOV_EXCL_STOP */
831         }
832
833         if (tdm_debug_module & TDM_DEBUG_COMMIT)
834                 TDM_INFO("output(%d) wait_vblank: handler(%p)", private_output->pipe, vblank_handler);
835
836         LIST_FOR_EACH_ENTRY(v, &private_output->vblank_handler_list, link) {
837                 if (v->interval == interval && v->sync == sync && v->owner_tid == tid) {
838                         skip_request = 1;
839                         break;
840                 }
841         }
842
843         if (add_front)
844                 LIST_ADD(&vblank_handler->link, &private_output->vblank_handler_list);
845         else
846                 LIST_ADDTAIL(&vblank_handler->link, &private_output->vblank_handler_list);
847
848         vblank_handler->private_output = private_output;
849         vblank_handler->interval = interval;
850         vblank_handler->sync = sync;
851         vblank_handler->func = func;
852         vblank_handler->user_data = user_data;
853         vblank_handler->owner_tid = tid;
854
855         /* If there is the previous request, we can skip to call output_wait_vblank() */
856         if (!skip_request) {
857                 ret = func_output->output_wait_vblank(private_output->output_backend, interval,
858                                                                                           sync, vblank_handler);
859                 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
860
861                 _tdm_output_vblank_timeout_update(private_output, 1000);
862
863                 if (tdm_debug_module & TDM_DEBUG_COMMIT)
864                         TDM_INFO("output(%d) backend wait_vblank", private_output->pipe);
865         }
866
867         return ret;
868
869 wait_failed:
870         /* LCOV_EXCL_START */
871         if (vblank_handler) {
872                 LIST_DEL(&vblank_handler->link);
873                 free(vblank_handler);
874         }
875         return ret;
876         /* LCOV_EXCL_STOP */
877 }
878
879 EXTERN tdm_error
880 tdm_output_wait_vblank(tdm_output *output, int interval, int sync,
881                                            tdm_output_vblank_handler func, void *user_data)
882 {
883         OUTPUT_FUNC_ENTRY();
884
885         _pthread_mutex_lock(&private_display->lock);
886
887         if (TDM_OUTPUT_DPMS_VSYNC_IS_OFF(private_output->current_dpms_value)) {
888                 TDM_WRN("output(%d) dpms: %s", private_output->pipe,
889                                 tdm_dpms_str(private_output->current_dpms_value));
890                 _pthread_mutex_unlock(&private_display->lock);
891                 return TDM_ERROR_DPMS_OFF;
892         }
893
894         ret = _tdm_output_wait_vblank(output, interval, sync, func, user_data, 0);
895
896         _pthread_mutex_unlock(&private_display->lock);
897
898         return ret;
899 }
900
901 /* LCOV_EXCL_START */
902 EXTERN tdm_error
903 tdm_output_wait_vblank_add_front(tdm_output *output, int interval, int sync,
904                                                                  tdm_output_vblank_handler func, void *user_data)
905 {
906         OUTPUT_FUNC_ENTRY();
907
908         _pthread_mutex_lock(&private_display->lock);
909
910         if (TDM_OUTPUT_DPMS_VSYNC_IS_OFF(private_output->current_dpms_value)) {
911                 TDM_WRN("output(%d) dpms: %s", private_output->pipe,
912                                 tdm_dpms_str(private_output->current_dpms_value));
913                 _pthread_mutex_unlock(&private_display->lock);
914                 return TDM_ERROR_DPMS_OFF;
915         }
916
917         ret = _tdm_output_wait_vblank(output, interval, sync, func, user_data, 1);
918
919         _pthread_mutex_unlock(&private_display->lock);
920
921         return ret;
922 }
923 /* LCOV_EXCL_STOP */
924
925 INTERN void
926 tdm_output_remove_vblank_handler_internal(tdm_output *output, tdm_output_vblank_handler func, void *user_data)
927 {
928         tdm_private_output *private_output = (tdm_private_output*)output;
929         tdm_private_vblank_handler *v = NULL;
930
931         TDM_RETURN_IF_FAIL(private_output != NULL);
932         TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
933
934         LIST_FOR_EACH_ENTRY(v, &private_output->vblank_handler_list, link) {
935                 if (v->func == func && v->user_data == user_data) {
936                         /* only set func & user_data to NULL. It will be freed when an event occurs */
937                         v->func = NULL;
938                         v->user_data = NULL;
939                         break;
940                 }
941         }
942 }
943
944 INTERN void
945 tdm_output_remove_commit_handler_internal(tdm_output *output, tdm_output_commit_handler func, void *user_data)
946 {
947         tdm_private_output *private_output = (tdm_private_output*)output;
948         tdm_private_output_commit_handler *c = NULL;
949
950         TDM_RETURN_IF_FAIL(private_output != NULL);
951         TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
952
953         LIST_FOR_EACH_ENTRY(c, &private_output->output_commit_handler_list, link) {
954                 if (c->func == func && c->user_data == user_data) {
955                         /* only set func & user_data to NULL. It will be freed when an event occurs */
956                         c->func = NULL;
957                         c->user_data = NULL;
958                         break;
959                 }
960         }
961 }
962
963 EXTERN tdm_error
964 tdm_output_remove_vblank_handler(tdm_output *output, tdm_output_vblank_handler func, void *user_data)
965 {
966         OUTPUT_FUNC_ENTRY();
967
968         _pthread_mutex_lock(&private_display->lock);
969
970         tdm_output_remove_vblank_handler_internal(output, func, user_data);
971
972         _pthread_mutex_unlock(&private_display->lock);
973
974         return ret;
975 }
976
977 EXTERN tdm_error
978 tdm_output_remove_commit_handler(tdm_output *output, tdm_output_commit_handler func, void *user_data)
979 {
980         OUTPUT_FUNC_ENTRY();
981
982         _pthread_mutex_lock(&private_display->lock);
983
984         tdm_output_remove_commit_handler_internal(output, func, user_data);
985
986         _pthread_mutex_unlock(&private_display->lock);
987
988         return ret;
989 }
990
991 INTERN tdm_error
992 tdm_output_commit_internal(tdm_output *output, int sync, tdm_output_commit_handler func, void *user_data)
993 {
994         tdm_func_output *func_output;
995         tdm_private_output_commit_handler *output_commit_handler = NULL;
996         tdm_private_layer *private_layer = NULL;
997         tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_ON;
998
999         OUTPUT_FUNC_ENTRY();
1000
1001         func_output = &private_display->func_output;
1002
1003         if (!func_output->output_commit) {
1004                 /* LCOV_EXCL_START */
1005                 TDM_ERR("not implemented!!");
1006                 return TDM_ERROR_NOT_IMPLEMENTED;
1007                 /* LCOV_EXCL_STOP */
1008         }
1009
1010         ret = tdm_output_get_dpms_internal(output, &dpms_value);
1011         TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, ret);
1012
1013         if (!TDM_OUTPUT_DPMS_VSYNC_IS_OFF(dpms_value)) {
1014                 if (func) {
1015                         if (!private_output->regist_commit_cb) {
1016                                 private_output->regist_commit_cb = 1;
1017                                 ret = func_output->output_set_commit_handler(private_output->output_backend,
1018                                                         tdm_output_cb_commit);
1019                                 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, commit_failed);
1020                         }
1021
1022                         output_commit_handler = calloc(1, sizeof(tdm_private_output_commit_handler));
1023                         if (!output_commit_handler) {
1024                                 /* LCOV_EXCL_START */
1025                                 TDM_ERR("failed: alloc memory");
1026                                 return TDM_ERROR_OUT_OF_MEMORY;
1027                                 /* LCOV_EXCL_STOP */
1028                         }
1029
1030                         LIST_ADDTAIL(&output_commit_handler->link, &private_output->output_commit_handler_list);
1031                         output_commit_handler->private_output = private_output;
1032                         output_commit_handler->func = func;
1033                         output_commit_handler->user_data = user_data;
1034                         output_commit_handler->owner_tid = syscall(SYS_gettid);
1035                 }
1036
1037                 ret = func_output->output_commit(private_output->output_backend, sync,
1038                                 output_commit_handler);
1039                 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, commit_failed);
1040
1041                 if (tdm_debug_module & TDM_DEBUG_COMMIT)
1042                         TDM_INFO("output(%d) backend commit: handle(%p) func(%p) user_data(%p)",
1043                                         private_output->pipe, output_commit_handler, func, user_data);
1044         }
1045
1046         /* Even if DPMS is off, committed_buffer should be changed because it will be referred
1047          * for tdm_layer_committed() function.
1048          */
1049         LIST_FOR_EACH_ENTRY(private_layer, &private_output->layer_list, link) {
1050                 if (!private_layer->waiting_buffer)
1051                         continue;
1052
1053                 private_layer->committed_buffer = private_layer->waiting_buffer;
1054                 private_layer->waiting_buffer = NULL;
1055                 if (tdm_debug_module & TDM_DEBUG_BUFFER)
1056                         TDM_INFO("layer(%p) waiting_buffer(%p) committed_buffer(%p)",
1057                                          private_layer, private_layer->waiting_buffer,
1058                                          private_layer->committed_buffer->buffer);
1059         }
1060
1061         if (TDM_OUTPUT_DPMS_VSYNC_IS_OFF(dpms_value)) {
1062                 TDM_WRN("dpms %s. Directly call commit handler instead of commit.", tdm_dpms_str(dpms_value));
1063                 if (func)
1064                         func(output, 0, 0, 0, user_data);
1065         }
1066
1067         return ret;
1068
1069 commit_failed:
1070         /* LCOV_EXCL_START */
1071         if (output_commit_handler) {
1072                 LIST_DEL(&output_commit_handler->link);
1073                 free(output_commit_handler);
1074         }
1075         return ret;
1076         /* LCOV_EXCL_STOP */
1077 }
1078
1079 EXTERN tdm_error
1080 tdm_output_commit(tdm_output *output, int sync, tdm_output_commit_handler func,
1081                                   void *user_data)
1082 {
1083         tdm_private_layer *private_layer = NULL;
1084
1085         OUTPUT_FUNC_ENTRY();
1086
1087         _pthread_mutex_lock(&private_display->lock);
1088
1089         if (private_output->commit_type == TDM_COMMIT_TYPE_NONE)
1090                 private_output->commit_type = TDM_COMMIT_TYPE_OUTPUT;
1091         else if (private_output->commit_type == TDM_COMMIT_TYPE_LAYER) {
1092                 TDM_ERR("Can't supported. Use tdm_layer_commit");
1093                 _pthread_mutex_unlock(&private_display->lock);
1094                 return TDM_ERROR_BAD_REQUEST;
1095         }
1096
1097         if (private_output->commit_per_vblank) {
1098                 TDM_ERR("Use tdm_layer_commit");
1099                 _pthread_mutex_unlock(&private_display->lock);
1100                 return TDM_ERROR_BAD_REQUEST;
1101         }
1102
1103         if (TDM_OUTPUT_DPMS_VSYNC_IS_OFF(private_output->current_dpms_value)) {
1104                 TDM_ERR("output(%d) dpms: %s", private_output->pipe,
1105                                 tdm_dpms_str(private_output->current_dpms_value));
1106                 _pthread_mutex_unlock(&private_display->lock);
1107                 return TDM_ERROR_DPMS_OFF;
1108         }
1109
1110         if (tdm_debug_module & TDM_DEBUG_COMMIT)
1111                 TDM_INFO("output(%d) commit", private_output->pipe);
1112
1113         /* apply the pending data of all layers */
1114         LIST_FOR_EACH_ENTRY(private_layer, &private_output->layer_list, link) {
1115                 tdm_layer_commit_pending_data(private_layer);
1116         }
1117
1118         ret = tdm_output_commit_internal(output, sync, func, user_data);
1119
1120         _pthread_mutex_unlock(&private_display->lock);
1121
1122         return ret;
1123 }
1124
1125 EXTERN tdm_error
1126 tdm_output_set_mode(tdm_output *output, const tdm_output_mode *mode)
1127 {
1128         tdm_func_output *func_output;
1129         OUTPUT_FUNC_ENTRY();
1130
1131         TDM_RETURN_VAL_IF_FAIL(mode != NULL, TDM_ERROR_INVALID_PARAMETER);
1132
1133         _pthread_mutex_lock(&private_display->lock);
1134
1135         func_output = &private_display->func_output;
1136
1137         if (!func_output->output_set_mode) {
1138                 /* LCOV_EXCL_START */
1139                 _pthread_mutex_unlock(&private_display->lock);
1140                 TDM_ERR("not implemented!!");
1141                 return TDM_ERROR_NOT_IMPLEMENTED;
1142                 /* LCOV_EXCL_STOP */
1143         }
1144
1145         ret = func_output->output_set_mode(private_output->output_backend, mode);
1146         if (ret == TDM_ERROR_NONE) {
1147                 private_output->current_mode = mode;
1148                 TDM_INFO("mode: %dx%d %dhz", mode->hdisplay, mode->vdisplay, mode->vrefresh);
1149         }
1150
1151         _pthread_mutex_unlock(&private_display->lock);
1152
1153         return ret;
1154 }
1155
1156 EXTERN tdm_error
1157 tdm_output_get_mode(tdm_output *output, const tdm_output_mode **mode)
1158 {
1159         OUTPUT_FUNC_ENTRY();
1160
1161         TDM_RETURN_VAL_IF_FAIL(mode != NULL, TDM_ERROR_INVALID_PARAMETER);
1162
1163         _pthread_mutex_lock(&private_display->lock);
1164
1165         *mode = private_output->current_mode;
1166
1167         _pthread_mutex_unlock(&private_display->lock);
1168
1169         return ret;
1170 }
1171
1172 static tdm_error
1173 _tdm_output_dpms_changed_timeout(void *user_data)
1174 {
1175         tdm_private_output *private_output = user_data;
1176         tdm_value value;
1177
1178         value.u32 = private_output->current_dpms_value;
1179         tdm_output_call_change_handler_internal(private_output,
1180                                                                                         &private_output->change_handler_list_sub,
1181                                                                                         TDM_OUTPUT_CHANGE_DPMS,
1182                                                                                         value, 0);
1183
1184         return TDM_ERROR_NONE;
1185 }
1186
1187 static tdm_error
1188 tdm_output_call_dpms_change_handler(tdm_output *output)
1189 {
1190         tdm_private_output *private_output = (tdm_private_output*)output;
1191         tdm_value value;
1192
1193         /** Use timer to call the output change callback of the sub-thread.
1194          * The output change callback of tdm_server and tdm_vblank was called
1195          * in the main thread. And it made the multi thread issue. If we use
1196          * the timer, we can call the sub-thread's output change callback in
1197          * sub-thread.
1198          */
1199         if (!private_output->dpms_changed_timer) {
1200                 private_output->dpms_changed_timer =
1201                         tdm_event_loop_add_timer_handler(private_output->private_display,
1202                                                                                          _tdm_output_dpms_changed_timeout, private_output, NULL);
1203                 if (!private_output->dpms_changed_timer) {
1204                         /* LCOV_EXCL_START */
1205                         TDM_ERR("can't create dpms timer!!");
1206                         return TDM_ERROR_OUT_OF_MEMORY;
1207                         /* LCOV_EXCL_STOP */
1208                 }
1209         }
1210
1211         value.u32 = private_output->current_dpms_value;
1212         tdm_output_call_change_handler_internal(private_output,
1213                                                                                         &private_output->change_handler_list_main,
1214                                                                                         TDM_OUTPUT_CHANGE_DPMS,
1215                                                                                         value, 0);
1216
1217         if (!LIST_IS_EMPTY(&private_output->change_handler_list_sub)) {
1218                 tdm_error ret = tdm_event_loop_source_timer_update(private_output->dpms_changed_timer, 1);
1219                 if (ret != TDM_ERROR_NONE)
1220                         TDM_NEVER_GET_HERE();
1221         }
1222
1223         return TDM_ERROR_NONE;
1224 }
1225
1226 /* LCOV_EXCL_START */
1227 INTERN void
1228 tdm_output_cb_dpms(tdm_output *output_backend, tdm_output_dpms dpms, void *user_data)
1229 {
1230         tdm_private_display *private_display;
1231         tdm_private_output *private_output = user_data;
1232         tdm_value value;
1233
1234         TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
1235         TDM_RETURN_IF_FAIL(private_output);
1236
1237         private_display = private_output->private_display;
1238
1239         if (!tdm_thread_in_display_thread(syscall(SYS_gettid))) {
1240                 tdm_thread_cb_output_dpms output_dpms;
1241                 tdm_error ret;
1242
1243                 _tdm_output_update(output_backend, user_data);
1244
1245                 output_dpms.base.type = TDM_THREAD_CB_OUTPUT_DPMS;
1246                 output_dpms.base.length = sizeof output_dpms;
1247                 output_dpms.output_stamp = private_output->stamp;
1248                 output_dpms.dpms = dpms;
1249                 output_dpms.user_data = user_data;
1250
1251                 value.u32 = dpms;
1252                 tdm_output_call_change_handler_internal(private_output,
1253                                                                                                 &private_output->change_handler_list_sub,
1254                                                                                                 TDM_OUTPUT_CHANGE_DPMS,
1255                                                                                                 value, 0);
1256
1257                 ret = tdm_thread_send_cb(private_display->private_loop, &output_dpms.base);
1258                 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
1259
1260                 return;
1261         }
1262
1263         private_output->current_dpms_value = dpms;
1264         private_output->waiting_dpms_change = 0;
1265
1266         TDM_INFO("output(%d) dpms async '%s' done", private_output->pipe, tdm_dpms_str(dpms));
1267
1268         value.u32 = dpms;
1269         tdm_output_call_change_handler_internal(private_output,
1270                                                                                         &private_output->change_handler_list_main,
1271                                                                                         TDM_OUTPUT_CHANGE_DPMS,
1272                                                                                         value, 0);
1273 }
1274 /* LCOV_EXCL_STOP */
1275
1276 EXTERN tdm_error
1277 tdm_output_set_dpms(tdm_output *output, tdm_output_dpms dpms_value)
1278 {
1279         tdm_func_output *func_output;
1280         OUTPUT_FUNC_ENTRY();
1281
1282         if (dpms_value > TDM_OUTPUT_DPMS_OFF) {
1283                 if (dpms_value & TDM_OUTPUT_DPMS_DEFAULT_MASK) {
1284                         TDM_ERR("Don't use the low-4bit for an extended DPMS mode: dpms_value(%x)", dpms_value);
1285                         return TDM_ERROR_BAD_REQUEST;
1286                 }
1287
1288                 if (!(private_output->caps.capabilities & TDM_OUTPUT_CAPABILITY_EXTENDED_DPMS)) {
1289                         TDM_ERR("output(%d) doesn't support the extended DPMS control: '%s'",
1290                                         private_output->pipe, tdm_dpms_str(dpms_value));
1291                         return TDM_ERROR_BAD_REQUEST;
1292                 }
1293         }
1294
1295         _pthread_mutex_lock(&private_display->lock);
1296
1297         if (private_output->waiting_dpms_change) {
1298                 TDM_ERR("DPMS is not changed yet. Can't be changed twice");
1299                 _pthread_mutex_unlock(&private_display->lock);
1300                 return TDM_ERROR_BAD_REQUEST;
1301         }
1302
1303         func_output = &private_display->func_output;
1304
1305         TDM_INFO("output(%d) dpms '%s'", private_output->pipe, tdm_dpms_str(dpms_value));
1306
1307         if (func_output->output_set_dpms)
1308                 ret = func_output->output_set_dpms(private_output->output_backend, dpms_value);
1309         else {
1310                 /* LCOV_EXCL_START */
1311                 ret = TDM_ERROR_NONE;
1312                 TDM_WRN("not implemented!!");
1313                 goto done;
1314                 /* LCOV_EXCL_STOP */
1315         }
1316
1317 done:
1318         if (ret == TDM_ERROR_NONE) {
1319                 if (private_output->current_dpms_value != dpms_value) {
1320                         private_output->current_dpms_value = dpms_value;
1321                         TDM_INFO("output(%d) dpms '%s' done", private_output->pipe, tdm_dpms_str(dpms_value));
1322                         tdm_output_call_dpms_change_handler(output);
1323                 }
1324         } else {
1325                 tdm_output_dpms temp = TDM_OUTPUT_DPMS_OFF;
1326
1327                 /* update current_dpms_value forcely */
1328                 tdm_output_get_dpms_internal(output, &temp);
1329
1330                 TDM_ERR("output(%d) set_dpms failed: dpms '%s'", private_output->pipe, tdm_dpms_str(temp));
1331         }
1332
1333         _pthread_mutex_unlock(&private_display->lock);
1334
1335         return ret;
1336 }
1337
1338 /* LCOV_EXCL_START */
1339 EXTERN tdm_error
1340 tdm_output_set_dpms_async(tdm_output *output, tdm_output_dpms dpms_value)
1341 {
1342         tdm_func_output *func_output;
1343         OUTPUT_FUNC_ENTRY();
1344
1345         if (!(private_output->caps.capabilities & TDM_OUTPUT_CAPABILITY_ASYNC_DPMS)) {
1346                 TDM_ERR("output doesn't support the asynchronous DPMS control!");
1347                 return TDM_ERROR_BAD_REQUEST;
1348         }
1349
1350         if (dpms_value > TDM_OUTPUT_DPMS_OFF) {
1351                 if (dpms_value & TDM_OUTPUT_DPMS_DEFAULT_MASK) {
1352                         TDM_ERR("Don't use the low-4bit for an extended DPMS mode: dpms_value(%x)", dpms_value);
1353                         return TDM_ERROR_BAD_REQUEST;
1354                 }
1355
1356                 if (!(private_output->caps.capabilities & TDM_OUTPUT_CAPABILITY_EXTENDED_DPMS)) {
1357                         TDM_ERR("output(%d) doesn't support the extended DPMS control: '%s'",
1358                                         private_output->pipe, tdm_dpms_str(dpms_value));
1359                         return TDM_ERROR_BAD_REQUEST;
1360                 }
1361         }
1362
1363         _pthread_mutex_lock(&private_display->lock);
1364
1365         if (private_output->waiting_dpms_change) {
1366                 TDM_ERR("DPMS is not changed yet. Can't be changed twice");
1367                 _pthread_mutex_unlock(&private_display->lock);
1368                 return TDM_ERROR_BAD_REQUEST;
1369         }
1370
1371         func_output = &private_display->func_output;
1372         if (!func_output->output_set_dpms_handler) {
1373                 TDM_ERR("not implemented: output_set_dpms_handler");
1374                 _pthread_mutex_unlock(&private_display->lock);
1375                 return TDM_ERROR_NOT_IMPLEMENTED;
1376         }
1377
1378         if (!func_output->output_set_dpms_async) {
1379                 TDM_ERR("not implemented: output_set_dpms_async");
1380                 _pthread_mutex_unlock(&private_display->lock);
1381                 return TDM_ERROR_NOT_IMPLEMENTED;
1382         }
1383
1384         if (!private_output->regist_dpms_cb) {
1385                 private_output->regist_dpms_cb = 1;
1386                 ret = func_output->output_set_dpms_handler(private_output->output_backend,
1387                                 tdm_output_cb_dpms, private_output);
1388                 if (ret != TDM_ERROR_NONE) {
1389                         _pthread_mutex_unlock(&private_display->lock);
1390                         TDM_ERR("Can't set the dpms handler!!");
1391                         return ret;
1392                 }
1393         }
1394
1395         TDM_INFO("output(%d) dpms async '%s'", private_output->pipe, tdm_dpms_str(dpms_value));
1396
1397         ret = func_output->output_set_dpms_async(private_output->output_backend, dpms_value);
1398
1399         if (ret == TDM_ERROR_NONE) {
1400                 private_output->waiting_dpms_change = 1;
1401                 TDM_INFO("output(%d) dpms async '%s' waiting", private_output->pipe, tdm_dpms_str(dpms_value));
1402         } else {
1403                 tdm_output_dpms temp = TDM_OUTPUT_DPMS_OFF;
1404
1405                 /* update current_dpms_value forcely */
1406                 tdm_output_get_dpms_internal(output, &temp);
1407
1408                 TDM_ERR("output(%d) set_dpms_async failed: dpms '%s'", private_output->pipe, tdm_dpms_str(temp));
1409         }
1410
1411         _pthread_mutex_unlock(&private_display->lock);
1412
1413         return ret;
1414 }
1415 /* LCOV_EXCL_STOP */
1416
1417 INTERN tdm_error
1418 tdm_output_get_dpms_internal(tdm_output *output, tdm_output_dpms *dpms_value)
1419 {
1420         tdm_func_output *func_output;
1421         OUTPUT_FUNC_ENTRY();
1422
1423         TDM_RETURN_VAL_IF_FAIL(private_output != NULL, TDM_ERROR_INVALID_PARAMETER);
1424
1425         /* TODO: this is ugly. But before calling backend's output_get_dpms(), we have
1426          * to check if all backends's DPMS operation has no problem. In future, we'd
1427          * better use new env instead of using commit_per_vblank variable to distinguish
1428          * whether we use the stored value or backend's output_get_dpms.
1429          */
1430         if (!private_output->commit_per_vblank) {
1431                 *dpms_value = private_output->current_dpms_value;
1432                 return TDM_ERROR_NONE;
1433         }
1434
1435         func_output = &private_display->func_output;
1436
1437         if (!func_output->output_get_dpms) {
1438                 /* LCOV_EXCL_START */
1439                 *dpms_value = private_output->current_dpms_value;
1440                 TDM_WRN("not implemented!!");
1441                 return TDM_ERROR_NONE;
1442                 /* LCOV_EXCL_STOP */
1443         }
1444
1445         ret = func_output->output_get_dpms(private_output->output_backend, dpms_value);
1446         if (ret != TDM_ERROR_NONE) {
1447                 /* LCOV_EXCL_START */
1448                 TDM_ERR("output_get_dpms failed");
1449                 *dpms_value = TDM_OUTPUT_DPMS_OFF;
1450                 /* LCOV_EXCL_STOP */
1451         }
1452
1453         /* checking with backend's value */
1454         if (*dpms_value != private_output->current_dpms_value) {
1455                 TDM_ERR("output(%d) dpms changed suddenly: %s -> %s",
1456                                 private_output->pipe, private_output->current_dpms_value,
1457                                 tdm_dpms_str(*dpms_value));
1458                 private_output->current_dpms_value = *dpms_value;
1459                 tdm_output_call_dpms_change_handler(output);
1460         }
1461
1462         return ret;
1463 }
1464
1465 EXTERN tdm_error
1466 tdm_output_get_dpms(tdm_output *output, tdm_output_dpms *dpms_value)
1467 {
1468         OUTPUT_FUNC_ENTRY();
1469
1470         TDM_RETURN_VAL_IF_FAIL(dpms_value != NULL, TDM_ERROR_INVALID_PARAMETER);
1471
1472         _pthread_mutex_lock(&private_display->lock);
1473
1474         ret = tdm_output_get_dpms_internal(output, dpms_value);
1475
1476         _pthread_mutex_unlock(&private_display->lock);
1477
1478         return ret;
1479 }
1480
1481 EXTERN tdm_capture *
1482 tdm_output_create_capture(tdm_output *output, tdm_error *error)
1483 {
1484         tdm_capture *capture = NULL;
1485
1486         OUTPUT_FUNC_ENTRY_ERROR();
1487
1488         _pthread_mutex_lock(&private_display->lock);
1489
1490         capture = (tdm_capture *)tdm_capture_create_output_internal(private_output, error);
1491
1492         _pthread_mutex_unlock(&private_display->lock);
1493
1494         return capture;
1495 }
1496
1497 EXTERN tdm_hwc_window *
1498 tdm_output_hwc_create_window(tdm_output *output, tdm_error *error)
1499 {
1500         tdm_hwc_window *hwc_window = NULL;
1501
1502         OUTPUT_FUNC_ENTRY_ERROR();
1503
1504         _pthread_mutex_lock(&private_display->lock);
1505
1506         if (private_output->caps.capabilities & TDM_OUTPUT_CAPABILITY_HWC)
1507                 hwc_window = (tdm_hwc_window *)tdm_hwc_window_create_internal(private_output, 0, error);
1508         else {
1509                 /* LCOV_EXCL_START */
1510                 TDM_ERR("output(%p) not support HWC", private_output);
1511                 if (error)
1512                         *error = TDM_ERROR_BAD_REQUEST;
1513                 /* LCOV_EXCL_STOP */
1514         }
1515
1516         _pthread_mutex_unlock(&private_display->lock);
1517
1518         return hwc_window;
1519 }
1520
1521 EXTERN tdm_hwc_window *
1522 tdm_output_hwc_create_video_window(tdm_output *output, tdm_error *error)
1523 {
1524         tdm_hwc_window *hwc_window = NULL;
1525
1526         OUTPUT_FUNC_ENTRY_ERROR();
1527
1528         _pthread_mutex_lock(&private_display->lock);
1529
1530         if (private_output->caps.capabilities & TDM_OUTPUT_CAPABILITY_HWC)
1531                 hwc_window = (tdm_hwc_window *)tdm_hwc_window_create_internal(private_output, 1, error);
1532         else {
1533                 /* LCOV_EXCL_START */
1534                 TDM_ERR("output(%p) not support HWC", private_output);
1535                 if (error)
1536                         *error = TDM_ERROR_BAD_REQUEST;
1537                 /* LCOV_EXCL_STOP */
1538         }
1539
1540         _pthread_mutex_unlock(&private_display->lock);
1541
1542         return hwc_window;
1543 }
1544
1545 EXTERN tdm_error
1546 tdm_output_hwc_destroy_window(tdm_output *output, tdm_hwc_window *hwc_window)
1547 {
1548         OUTPUT_FUNC_ENTRY();
1549
1550         TDM_RETURN_VAL_IF_FAIL(hwc_window != NULL, TDM_ERROR_INVALID_PARAMETER);
1551
1552         _pthread_mutex_lock(&private_display->lock);
1553
1554         ret = tdm_hwc_window_destroy_internal(hwc_window);
1555
1556         _pthread_mutex_unlock(&private_display->lock);
1557
1558         return ret;
1559 }
1560
1561 EXTERN tdm_error
1562 tdm_output_hwc_validate(tdm_output *output, tdm_hwc_window **composited_wnds,
1563                                                                         uint32_t num_wnds, uint32_t *num_types)
1564 {
1565         tdm_func_output *func_output = NULL;
1566         tdm_private_hwc_window **composited_wnds_frontend = NULL;
1567         tdm_hwc_window **composited_wnds_backend = NULL;
1568         int i;
1569
1570         OUTPUT_FUNC_ENTRY();
1571
1572         TDM_RETURN_VAL_IF_FAIL(num_types != NULL, TDM_ERROR_INVALID_PARAMETER);
1573
1574         _pthread_mutex_lock(&private_display->lock);
1575
1576         if (!(private_output->caps.capabilities & TDM_OUTPUT_CAPABILITY_HWC)) {
1577                 TDM_ERR("output(%p) not support HWC", private_output);
1578                 _pthread_mutex_unlock(&private_display->lock);
1579                 return TDM_ERROR_BAD_REQUEST;
1580         }
1581
1582         func_output = &private_display->func_output;
1583
1584         if (!func_output->output_hwc_validate) {
1585                 /* LCOV_EXCL_START */
1586                 _pthread_mutex_unlock(&private_display->lock);
1587                 TDM_ERR("not implemented!!");
1588                 return TDM_ERROR_NOT_IMPLEMENTED;
1589                 /* LCOV_EXCL_STOP */
1590         }
1591
1592         if (num_wnds == 0) {
1593                 ret = func_output->output_hwc_validate(private_output->output_backend, NULL, 0, num_types);
1594
1595                 _pthread_mutex_unlock(&private_display->lock);
1596
1597                 return ret;
1598         }
1599
1600         composited_wnds_backend = calloc(num_wnds, sizeof(tdm_hwc_window *));
1601         if (!composited_wnds_backend) {
1602                 /* LCOV_EXCL_START */
1603                 _pthread_mutex_unlock(&private_display->lock);
1604                 return TDM_ERROR_OUT_OF_MEMORY;
1605                 /* LCOV_EXCL_STOP */
1606         }
1607
1608         composited_wnds_frontend = (tdm_private_hwc_window **)composited_wnds;
1609
1610         for (i = 0; i < num_wnds; i++)
1611                 composited_wnds_backend[i] = composited_wnds_frontend[i]->hwc_window_backend;
1612
1613         ret = func_output->output_hwc_validate(private_output->output_backend, composited_wnds_backend, num_wnds, num_types);
1614
1615         free(composited_wnds_backend);
1616
1617         _pthread_mutex_unlock(&private_display->lock);
1618
1619         return ret;
1620 }
1621
1622 EXTERN tdm_error
1623 tdm_output_hwc_set_need_validate_handler(tdm_output *output,
1624                 tdm_output_need_validate_handler hndl)
1625 {
1626         OUTPUT_FUNC_ENTRY();
1627
1628         TDM_RETURN_VAL_IF_FAIL(hndl != NULL, TDM_ERROR_INVALID_PARAMETER);
1629
1630         _pthread_mutex_lock(&private_display->lock);
1631
1632         if (!(private_output->caps.capabilities & TDM_OUTPUT_CAPABILITY_HWC)) {
1633                 TDM_ERR("output(%p) not support HWC", private_output);
1634                 _pthread_mutex_unlock(&private_display->lock);
1635                 return TDM_ERROR_BAD_REQUEST;
1636         }
1637
1638         /* there's no reason to allow this */
1639         if (private_output->need_validate.hndl) {
1640
1641                 _pthread_mutex_unlock(&private_display->lock);
1642                 return TDM_ERROR_OPERATION_FAILED;
1643         }
1644
1645         private_output->need_validate.hndl = hndl;
1646
1647         _pthread_mutex_unlock(&private_display->lock);
1648
1649         return ret;
1650 }
1651
1652 EXTERN tdm_error
1653 tdm_output_hwc_get_changed_composition_types(tdm_output *output,
1654                                                                                  uint32_t *num_elements,
1655                                                                                  tdm_hwc_window **hwc_window,
1656                                                                                  tdm_hwc_window_composition *composition_types)
1657 {
1658         tdm_func_output *func_output = NULL;
1659         tdm_private_hwc_window * private_hwc_window = NULL;
1660         int i = 0;
1661
1662         OUTPUT_FUNC_ENTRY();
1663
1664         TDM_RETURN_VAL_IF_FAIL(num_elements != NULL, TDM_ERROR_INVALID_PARAMETER);
1665
1666         _pthread_mutex_lock(&private_display->lock);
1667
1668         if (!(private_output->caps.capabilities & TDM_OUTPUT_CAPABILITY_HWC)) {
1669                 TDM_ERR("output(%p) not support HWC", private_output);
1670                 _pthread_mutex_unlock(&private_display->lock);
1671                 return TDM_ERROR_BAD_REQUEST;
1672         }
1673
1674         func_output = &private_display->func_output;
1675
1676         if (!func_output->output_hwc_get_changed_composition_types) {
1677                 /* LCOV_EXCL_START */
1678                 _pthread_mutex_unlock(&private_display->lock);
1679                 TDM_ERR("not implemented!!");
1680                 return TDM_ERROR_NOT_IMPLEMENTED;
1681                 /* LCOV_EXCL_STOP */
1682         }
1683
1684         ret = func_output->output_hwc_get_changed_composition_types(private_output->output_backend,
1685                                                         num_elements, hwc_window, composition_types);
1686         if (ret != TDM_ERROR_NONE) {
1687                 /* LCOV_EXCL_START */
1688                 _pthread_mutex_unlock(&private_display->lock);
1689                 return ret;
1690                 /* LCOV_EXCL_STOP */
1691         }
1692
1693         if (hwc_window == NULL || composition_types == NULL) {
1694                 _pthread_mutex_unlock(&private_display->lock);
1695                 return TDM_ERROR_NONE;
1696         }
1697
1698         for (i = 0; i < *num_elements; i++) {
1699
1700                 private_hwc_window = _tdm_output_find_private_hwc_window(private_output, hwc_window[i]);
1701
1702                 if (private_hwc_window == NULL) {
1703                         /* LCOV_EXCL_START */
1704                         TDM_ERR("failed! This should never happen!");
1705                         func_output->output_hwc_destroy_window(private_output->output_backend, hwc_window[i]);
1706                         *num_elements = 0;
1707                         _pthread_mutex_unlock(&private_display->lock);
1708                         return TDM_ERROR_OPERATION_FAILED;
1709                         /* LCOV_EXCL_STOP */
1710                 }
1711
1712                 hwc_window[i] = (tdm_hwc_window*)private_hwc_window;
1713         }
1714
1715         _pthread_mutex_unlock(&private_display->lock);
1716
1717         return ret;
1718 }
1719
1720 tdm_error
1721 tdm_output_hwc_accept_changes(tdm_output *output)
1722 {
1723         tdm_func_output *func_output = NULL;
1724
1725         OUTPUT_FUNC_ENTRY();
1726
1727         _pthread_mutex_lock(&private_display->lock);
1728
1729         if (!(private_output->caps.capabilities & TDM_OUTPUT_CAPABILITY_HWC)) {
1730                 TDM_ERR("output(%p) not support HWC", private_output);
1731                 _pthread_mutex_unlock(&private_display->lock);
1732                 return TDM_ERROR_BAD_REQUEST;
1733         }
1734
1735         func_output = &private_display->func_output;
1736
1737         if (!func_output->output_hwc_validate) {
1738                 /* LCOV_EXCL_START */
1739                 _pthread_mutex_unlock(&private_display->lock);
1740                 TDM_ERR("not implemented!!");
1741                 return TDM_ERROR_NOT_IMPLEMENTED;
1742                 /* LCOV_EXCL_STOP */
1743         }
1744
1745         ret = func_output->output_hwc_accept_changes(private_output->output_backend);
1746
1747         _pthread_mutex_unlock(&private_display->lock);
1748
1749         return ret;
1750 }
1751
1752 tbm_surface_queue_h
1753 tdm_output_hwc_get_target_buffer_queue(tdm_output *output, tdm_error *error)
1754 {
1755         tdm_func_output *func_output = NULL;
1756         tbm_surface_queue_h queue = NULL;
1757
1758         OUTPUT_FUNC_ENTRY_ERROR();
1759
1760         _pthread_mutex_lock(&private_display->lock);
1761
1762         if (!(private_output->caps.capabilities & TDM_OUTPUT_CAPABILITY_HWC)) {
1763                 TDM_ERR("output(%p) not support HWC", private_output);
1764                 if (error)
1765                         *error = TDM_ERROR_BAD_REQUEST;
1766                 _pthread_mutex_unlock(&private_display->lock);
1767                 return NULL;
1768         }
1769
1770         func_output = &private_display->func_output;
1771
1772         if (!func_output->output_hwc_get_target_buffer_queue) {
1773                 /* LCOV_EXCL_START */
1774                 _pthread_mutex_unlock(&private_display->lock);
1775                 TDM_ERR("not implemented!!");
1776                 return NULL;
1777                 /* LCOV_EXCL_STOP */
1778         }
1779
1780         queue = func_output->output_hwc_get_target_buffer_queue(private_output->output_backend, error);
1781
1782         _pthread_mutex_unlock(&private_display->lock);
1783
1784         return queue;
1785 }
1786
1787 EXTERN tdm_error
1788 tdm_output_hwc_set_client_target_buffer(tdm_output *output, tbm_surface_h target_buffer, tdm_hwc_region damage)
1789 {
1790         tdm_func_output *func_output = NULL;
1791
1792         OUTPUT_FUNC_ENTRY();
1793
1794         _pthread_mutex_lock(&private_display->lock);
1795
1796         if (!(private_output->caps.capabilities & TDM_OUTPUT_CAPABILITY_HWC)) {
1797                 TDM_ERR("output(%p) not support HWC", private_output);
1798                 _pthread_mutex_unlock(&private_display->lock);
1799                 return TDM_ERROR_BAD_REQUEST;
1800         }
1801
1802         if (tdm_debug_dump & TDM_DUMP_FLAG_WINDOW) {
1803                 /* LCOV_EXCL_START */
1804                 char str[TDM_PATH_LEN];
1805                 static int i;
1806                 snprintf(str, TDM_PATH_LEN, "target_window_%d_%03d",
1807                                  private_output->index, i++);
1808                 tdm_helper_dump_buffer_str(target_buffer, tdm_debug_dump_dir, str);
1809                 /* LCOV_EXCL_STOP */
1810         }
1811
1812         func_output = &private_display->func_output;
1813
1814         if (!func_output->output_hwc_set_client_target_buffer) {
1815                 /* LCOV_EXCL_START */
1816                 _pthread_mutex_unlock(&private_display->lock);
1817                 TDM_ERR("not implemented!!");
1818                 return TDM_ERROR_NOT_IMPLEMENTED;
1819                 /* LCOV_EXCL_STOP */
1820         }
1821
1822         ret = func_output->output_hwc_set_client_target_buffer(private_output->output_backend, target_buffer, damage);
1823
1824         _pthread_mutex_unlock(&private_display->lock);
1825
1826         return ret;
1827 }
1828
1829 tdm_error
1830 tdm_output_hwc_get_video_supported_formats(tdm_output *output, const tbm_format **formats,
1831                                                                 int *count)
1832 {
1833         tdm_func_output *func_output;
1834         OUTPUT_FUNC_ENTRY();
1835
1836         TDM_RETURN_VAL_IF_FAIL(formats != NULL, TDM_ERROR_INVALID_PARAMETER);
1837         TDM_RETURN_VAL_IF_FAIL(count != NULL, TDM_ERROR_INVALID_PARAMETER);
1838
1839         _pthread_mutex_lock(&private_display->lock);
1840
1841         func_output = &private_display->func_output;
1842
1843         if (!func_output->output_hwc_get_video_supported_formats) {
1844                 /* LCOV_EXCL_START */
1845                 _pthread_mutex_unlock(&private_display->lock);
1846                 TDM_ERR("not implemented!!");
1847                 return TDM_ERROR_NOT_IMPLEMENTED;
1848                 /* LCOV_EXCL_STOP */
1849         }
1850
1851         ret = func_output->output_hwc_get_video_supported_formats(
1852                                                                 private_output->output_backend, formats, count);
1853
1854         _pthread_mutex_unlock(&private_display->lock);
1855
1856         return ret;
1857 }
1858
1859 INTERN void
1860 tdm_output_call_change_handler_internal(tdm_private_output *private_output,
1861                                                                                 struct list_head *change_handler_list,
1862                                                                                 tdm_output_change_type type,
1863                                                                                 tdm_value value,
1864                                                                                 int no_check_thread_id)
1865 {
1866         tdm_private_display *private_display;
1867         tdm_private_change_handler *change_handler = NULL;
1868
1869         TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
1870         TDM_RETURN_IF_FAIL(private_output);
1871
1872         private_display = private_output->private_display;
1873         if (!tdm_thread_in_display_thread(syscall(SYS_gettid))) {
1874                 if (type & TDM_OUTPUT_CHANGE_CONNECTION)
1875                         TDM_INFO("output(%d) changed: %s (%d)",
1876                                          private_output->pipe, tdm_status_str(value.u32), value.u32);
1877                 if (type & TDM_OUTPUT_CHANGE_DPMS)
1878                         TDM_INFO("output(%d) changed: dpms %s (%d)",
1879                                          private_output->pipe, tdm_dpms_str(value.u32), value.u32);
1880         }
1881
1882         if (LIST_IS_EMPTY(change_handler_list))
1883                 return;
1884
1885         LIST_FOR_EACH_ENTRY(change_handler, change_handler_list, link) {
1886                 if (!no_check_thread_id && change_handler->owner_tid != syscall(SYS_gettid))
1887                         TDM_NEVER_GET_HERE();
1888
1889                 _pthread_mutex_unlock(&private_display->lock);
1890                 change_handler->func(private_output, type,
1891                                                          value, change_handler->user_data);
1892                 _pthread_mutex_lock(&private_display->lock);
1893         }
1894 }
1895
1896 static int
1897 _is_hwc_output_still_existed(tdm_private_output *private_output)
1898 {
1899         tdm_private_display *dpy;
1900         tdm_private_output *o = NULL;
1901
1902         dpy = tdm_display_init(NULL);
1903         TDM_RETURN_VAL_IF_FAIL(dpy != NULL, TDM_ERROR_OPERATION_FAILED);
1904
1905         LIST_FOR_EACH_ENTRY(o, &dpy->output_list, link) {
1906                 if (!(o->caps.capabilities & TDM_OUTPUT_CAPABILITY_HWC))
1907                         continue;
1908
1909                 if (o == private_output)
1910                         goto exist;
1911         }
1912
1913         tdm_display_deinit(dpy);
1914         return 0;
1915
1916 exist:
1917         tdm_display_deinit(dpy);
1918         return 1;
1919 }
1920
1921 /* gets called on behalf of the ecore-main-loop thread */
1922 INTERN tdm_error
1923 tdm_output_cb_need_validate(tdm_private_output *private_output)
1924 {
1925         TDM_RETURN_VAL_IF_FAIL(private_output != NULL, TDM_ERROR_INVALID_PARAMETER);
1926
1927         /* as we get 'private_output' within an event, an output this 'private_output'
1928          * points to can be destroyed already */
1929         if (!_is_hwc_output_still_existed(private_output))
1930                 return TDM_ERROR_NONE;
1931
1932         TDM_INFO("tdm-backend asks for revalidation for the output:%p.", private_output);
1933
1934         if (private_output->need_validate.hndl)
1935                 private_output->need_validate.hndl((tdm_output*)private_output);
1936
1937         return TDM_ERROR_NONE;
1938 }
1939
1940 /* gets called on behalf of the tdm-thread */
1941 static tdm_error
1942 _need_validate_handler(int fd, tdm_event_loop_mask mask, void *user_data)
1943 {
1944         tdm_thread_cb_need_validate ev;
1945         tdm_private_output *private_output;
1946         tdm_error ret;
1947         uint64_t value;
1948
1949         private_output = (tdm_private_output *)user_data;
1950
1951         if (read(private_output->need_validate.event_fd, &value, sizeof(value)) < 0) {
1952                 TDM_ERR("error while trying to read from a need_validate.event_fd fd.");
1953                 return TDM_ERROR_OPERATION_FAILED;
1954         }
1955
1956         ev.base.type = TDM_THREAD_CB_NEED_VALIDATE;
1957         ev.base.length = sizeof ev;
1958         ev.o = private_output;
1959
1960         ret = tdm_thread_send_cb(private_output->private_display->private_loop, &ev.base);
1961         TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
1962
1963         TDM_INFO("tdm-thread: get a 'need to revalidate' event for the ouptut:%p.", private_output);
1964
1965         /* who cares about this? */
1966         return TDM_ERROR_NONE;
1967 }
1968
1969 INTERN tdm_error
1970 tdm_output_need_validate_event_init(tdm_output *output)
1971 {
1972         int fd;
1973
1974         OUTPUT_FUNC_ENTRY();
1975
1976         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
1977
1978         if (!(private_output->caps.capabilities & TDM_OUTPUT_CAPABILITY_HWC)) {
1979                 TDM_ERR("output(%p) not support HWC", private_output);
1980                 return TDM_ERROR_BAD_REQUEST;
1981         }
1982
1983         /* build in eventfd fds into event_loop listened & handled by the tdm-thread */
1984         fd = eventfd(0, 0);
1985         TDM_WARNING_IF_FAIL(fd >= 0);
1986
1987         private_output->need_validate.event_source = tdm_event_loop_add_fd_handler(private_display,
1988                         fd,     TDM_EVENT_LOOP_READABLE, _need_validate_handler, private_output, &ret);
1989         TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
1990
1991         private_output->need_validate.event_fd = fd;
1992
1993         TDM_INFO("register an output:%p for the revalidation, event_fd:%d.", private_output, fd);
1994
1995         return ret;
1996 }
1997
1998 INTERN tdm_error
1999 tdm_output_choose_commit_per_vblank_mode(tdm_private_output *private_output, int mode)
2000 {
2001         if (!private_output)
2002                 return TDM_ERROR_INVALID_PARAMETER;
2003
2004         if (mode < 0 || mode > 2)
2005                 return TDM_ERROR_INVALID_PARAMETER;
2006
2007         private_output->commit_per_vblank = mode;
2008
2009         if (private_output->commit_per_vblank == 0)
2010                 TDM_INFO("commit per vblank: disable");
2011         else if (private_output->commit_per_vblank == 1)
2012                 TDM_INFO("commit per vblank: enable (1 layer)");
2013         else if (private_output->commit_per_vblank == 2)
2014                 TDM_INFO("commit per vblank: enable (previous commit)");
2015
2016         return TDM_ERROR_NONE;
2017 }