6516b6ce4519c02c243780a46cd61a694f6e8866
[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 <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 #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(output != NULL, 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(output != NULL, 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_output_vblank_handler *v = NULL;
68
69         TDM_RETURN_VAL_IF_FAIL(private_output != NULL, TDM_ERROR_OPERATION_FAILED);
70
71         private_output->vblank_timeout_timer_expired++;
72
73         TDM_ERR("TDM output(%d) vblank TIMEOUT!! (%d time%s)",
74                         private_output->pipe,
75                         private_output->vblank_timeout_timer_expired,
76                         (private_output->vblank_timeout_timer_expired > 1) ? "s" : "");
77
78         LIST_FOR_EACH_ENTRY(v, &private_output->vblank_handler_list, link) {
79                 TDM_ERR("vblank_handler(%p) interval(%d) sync(%d) sent_to_frontend(%u) owner_tid(%d)",
80                                 v, v->interval, v->sync, v->sent_to_frontend, v->owner_tid);
81         }
82
83         return TDM_ERROR_NONE;
84 }
85
86 INTERN void
87 tdm_output_vblank_print_wait_information(tdm_private_output *private_output, void *user_data)
88 {
89         tdm_private_output_vblank_handler *v = NULL;
90
91         TDM_RETURN_IF_FAIL(private_output != NULL);
92         TDM_RETURN_IF_FAIL(user_data != NULL);
93
94         TDM_ERR("TDM output(%d) vblank user_data(%p) info!!", private_output->pipe, user_data);
95
96         LIST_FOR_EACH_ENTRY(v, &private_output->vblank_handler_list, link) {
97                 if (v->user_data != user_data)
98                         continue;
99                 TDM_ERR("vblank_handler(%p) interval(%d) sync(%d) sent_to_frontend(%u) owner_tid(%d)",
100                                 v, v->interval, v->sync, v->sent_to_frontend, v->owner_tid);
101         }
102 }
103
104 static void
105 _tdm_output_vblank_timeout_update(tdm_private_output *private_output, int ms_delay)
106 {
107         tdm_error ret;
108
109         if (!private_output->vblank_timeout_timer) {
110                 private_output->vblank_timeout_timer =
111                         tdm_event_loop_add_timer_handler(private_output->private_display,
112                                                                                          _tdm_output_vblank_timeout_cb,
113                                                                                          private_output,
114                                                                                          &ret);
115                 if (!private_output->vblank_timeout_timer) {
116                         TDM_ERR("output(%d) couldn't add timer", private_output->pipe);
117                         return;
118                 }
119                 TDM_INFO("output(%d) create vblank timeout timer", private_output->pipe);
120                 private_output->vblank_timeout_timer_expired = 0;
121         }
122
123         ret = tdm_event_loop_source_timer_update(private_output->vblank_timeout_timer, ms_delay);
124         if (ret != TDM_ERROR_NONE) {
125                 TDM_ERR("output(%d) couldn't update timer", private_output->pipe);
126                 return;
127         }
128 }
129
130 INTERN tdm_error
131 tdm_output_init(tdm_private_display *private_display)
132 {
133         tdm_thread_cb_set_find_func(TDM_THREAD_CB_OUTPUT_DESTROY, tdm_display_find_output_stamp);
134         tdm_thread_cb_set_find_func(TDM_THREAD_CB_OUTPUT_COMMIT, tdm_display_find_output_stamp);
135         tdm_thread_cb_set_find_func(TDM_THREAD_CB_OUTPUT_VBLANK, tdm_display_find_output_stamp);
136         tdm_thread_cb_set_find_func(TDM_THREAD_CB_OUTPUT_STATUS, tdm_display_find_output_stamp);
137         tdm_thread_cb_set_find_func(TDM_THREAD_CB_OUTPUT_DPMS, tdm_display_find_output_stamp);
138         tdm_thread_cb_set_find_func(TDM_THREAD_CB_VOUTPUT_COMMIT, tdm_display_find_private_voutput);
139
140         return TDM_ERROR_NONE;
141 }
142
143 INTERN tdm_error
144 tdm_output_call_thread_cb_destroy(tdm_private_output *private_output)
145 {
146         tdm_thread_cb_output_destroy output_destroy;
147         tdm_error ret;
148
149         memset(&output_destroy, 0, sizeof output_destroy);
150         output_destroy.base.type = TDM_THREAD_CB_OUTPUT_DESTROY;
151         output_destroy.base.length = sizeof output_destroy;
152         output_destroy.base.object_stamp = private_output->stamp;
153         output_destroy.base.data = NULL;
154         output_destroy.base.sync = 1;
155
156         ret = tdm_thread_cb_call(private_output, &output_destroy.base, 1);
157         TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
158
159         return TDM_ERROR_NONE;
160 }
161
162 INTERN void
163 tdm_output_thread_cb_destroy(tdm_private_display *private_display, void *object, tdm_thread_cb_base *cb_base, void *user_data)
164 {
165         tdm_private_output *private_output = object;
166         tdm_private_output_destroy_handler *destroy_handler = user_data;
167
168         TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
169
170         assert(destroy_handler->owner_tid == syscall(SYS_gettid));
171
172         _pthread_mutex_unlock(&private_display->lock);
173         destroy_handler->func(private_output, destroy_handler->user_data);
174         _pthread_mutex_lock(&private_display->lock);
175 }
176
177 EXTERN tdm_error
178 tdm_output_add_destroy_handler(tdm_output *output,
179                                                            tdm_output_destroy_handler func,
180                                                            void *user_data)
181 {
182         tdm_private_output_destroy_handler *destroy_handler = NULL;
183         OUTPUT_FUNC_ENTRY();
184
185         TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
186
187         _pthread_mutex_lock(&private_display->lock);
188
189         LIST_FOR_EACH_ENTRY(destroy_handler, &private_output->destroy_handler_list, link) {
190                 if (destroy_handler->func == func && destroy_handler->user_data == user_data) {
191                         TDM_ERR("can't add twice");
192                         _pthread_mutex_unlock(&private_display->lock);
193                         return TDM_ERROR_BAD_REQUEST;
194                 }
195         }
196
197         destroy_handler = calloc(1, sizeof(tdm_private_output_destroy_handler));
198         if (!destroy_handler) {
199                 TDM_ERR("failed: alloc memory");
200                 _pthread_mutex_unlock(&private_display->lock);
201                 return TDM_ERROR_OUT_OF_MEMORY;
202         }
203
204         ret = tdm_thread_cb_add(private_output, TDM_THREAD_CB_OUTPUT_DESTROY, NULL, tdm_output_thread_cb_destroy, destroy_handler);
205         if (ret != TDM_ERROR_NONE) {
206                 TDM_ERR("tdm_thread_cb_add failed");
207                 free(destroy_handler);
208                 _pthread_mutex_unlock(&private_display->lock);
209                 return TDM_ERROR_OPERATION_FAILED;
210         }
211
212         destroy_handler->private_output = private_output;
213         destroy_handler->func = func;
214         destroy_handler->user_data = user_data;
215         destroy_handler->owner_tid = syscall(SYS_gettid);
216
217         LIST_ADDTAIL(&destroy_handler->link, &private_output->destroy_handler_list);
218
219         _pthread_mutex_unlock(&private_display->lock);
220
221         return ret;
222 }
223
224 EXTERN void
225 tdm_output_remove_destroy_handler(tdm_output *output,
226                                                                   tdm_output_destroy_handler func,
227                                                                   void *user_data)
228 {
229         tdm_private_display *private_display;
230         tdm_private_output *private_output;
231         tdm_private_output_destroy_handler *destroy_handler = NULL, *hh = NULL;
232
233         TDM_RETURN_IF_FAIL(output != NULL);
234         TDM_RETURN_IF_FAIL(func != NULL);
235
236         private_output = (tdm_private_output*)output;
237         private_display = private_output->private_display;
238
239         _pthread_mutex_lock(&private_display->lock);
240
241         LIST_FOR_EACH_ENTRY_SAFE(destroy_handler, hh, &private_output->destroy_handler_list, link) {
242                 if (destroy_handler->func != func || destroy_handler->user_data != user_data)
243                         continue;
244
245                 tdm_thread_cb_remove(private_output, TDM_THREAD_CB_OUTPUT_DESTROY, NULL, tdm_output_thread_cb_destroy, destroy_handler);
246
247                 LIST_DEL(&destroy_handler->link);
248                 free(destroy_handler);
249
250                 _pthread_mutex_unlock(&private_display->lock);
251
252                 return;
253         }
254
255         _pthread_mutex_unlock(&private_display->lock);
256 }
257
258 EXTERN tdm_module *
259 tdm_output_get_backend_module(tdm_output *output, tdm_error *error)
260 {
261         tdm_private_module *private_module;
262
263         OUTPUT_FUNC_ENTRY_ERROR();
264
265         _pthread_mutex_lock(&private_display->lock);
266
267         private_module = private_output->private_module;
268
269         if (error)
270                 *error = TDM_ERROR_NONE;
271
272         _pthread_mutex_unlock(&private_display->lock);
273
274         return private_module;
275 }
276
277 EXTERN tdm_error
278 tdm_output_get_model_info(tdm_output *output, const char **maker,
279                                                   const char **model, const char **name)
280 {
281         OUTPUT_FUNC_ENTRY();
282
283         _pthread_mutex_lock(&private_display->lock);
284
285         if (maker)
286                 *maker = private_output->caps.maker;
287         if (model)
288                 *model = private_output->caps.model;
289         if (name)
290                 *name = private_output->caps.name;
291
292         _pthread_mutex_unlock(&private_display->lock);
293
294         return ret;
295 }
296
297 EXTERN tdm_error
298 tdm_output_get_capabilities(tdm_output *output, tdm_output_capability *capabilities)
299 {
300         OUTPUT_FUNC_ENTRY();
301
302         TDM_RETURN_VAL_IF_FAIL(capabilities != NULL, TDM_ERROR_INVALID_PARAMETER);
303
304         _pthread_mutex_lock(&private_display->lock);
305
306         *capabilities = private_output->caps.capabilities;
307
308         _pthread_mutex_unlock(&private_display->lock);
309
310         return ret;
311 }
312
313 EXTERN tdm_error
314 tdm_output_get_conn_status(tdm_output *output, tdm_output_conn_status *status)
315 {
316         OUTPUT_FUNC_ENTRY();
317
318         TDM_RETURN_VAL_IF_FAIL(status != NULL, TDM_ERROR_INVALID_PARAMETER);
319
320         _pthread_mutex_lock(&private_display->lock);
321
322         *status = private_output->caps.status;
323
324         _pthread_mutex_unlock(&private_display->lock);
325
326         return ret;
327 }
328
329 INTERN void
330 tdm_output_thread_cb_change(tdm_private_display *private_display, void *object, tdm_thread_cb_base *cb_base, void *user_data)
331 {
332         tdm_private_output *private_output = object;
333         tdm_private_output_change_handler *change_handler = user_data;
334         tdm_output_change_type type = TDM_OUTPUT_CHANGE_CONNECTION;
335         tdm_value value = {.u32 = 0 };
336
337         TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
338
339         assert(change_handler->owner_tid == syscall(SYS_gettid));
340
341         if (cb_base->type == TDM_THREAD_CB_OUTPUT_STATUS) {
342                 tdm_thread_cb_output_status *output_status = (tdm_thread_cb_output_status *)cb_base;
343                 type = TDM_OUTPUT_CHANGE_CONNECTION;
344                 value.u32 = output_status->status;
345         } else if (cb_base->type == TDM_THREAD_CB_OUTPUT_DPMS) {
346                 tdm_thread_cb_output_dpms *output_dpms = (tdm_thread_cb_output_dpms *)cb_base;
347                 type = TDM_OUTPUT_CHANGE_DPMS;
348                 value.u32 = output_dpms->dpms;
349         } else {
350                 TDM_NEVER_GET_HERE();
351                 return;
352         }
353
354         _pthread_mutex_unlock(&private_display->lock);
355         change_handler->func(private_output, type, value, change_handler->user_data);
356         _pthread_mutex_lock(&private_display->lock);
357
358 }
359
360 static tdm_error
361 _tdm_output_call_thread_cb_status(tdm_private_output *private_output, tdm_output_conn_status status)
362 {
363         tdm_thread_cb_output_status output_status;
364         pid_t caller_tid = syscall(SYS_gettid);
365         tdm_error ret;
366
367         memset(&output_status, 0, sizeof output_status);
368         output_status.base.type = TDM_THREAD_CB_OUTPUT_STATUS;
369         output_status.base.length = sizeof output_status;
370         output_status.base.object_stamp = private_output->stamp;
371         output_status.base.data = NULL;
372         if (tdm_thread_in_display_thread(caller_tid))
373                 output_status.base.sync = 0;
374         else
375                 output_status.base.sync = 1;
376         output_status.status = status;
377
378         ret = tdm_thread_cb_call(private_output, &output_status.base, 1);
379         TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
380
381         return TDM_ERROR_NONE;
382 }
383
384 static tdm_error
385 _tdm_output_call_thread_cb_dpms(tdm_private_output *private_output, tdm_output_dpms dpms)
386 {
387         tdm_thread_cb_output_dpms output_dpms;
388         tdm_error ret;
389
390         memset(&output_dpms, 0, sizeof output_dpms);
391         output_dpms.base.type = TDM_THREAD_CB_OUTPUT_DPMS;
392         output_dpms.base.length = sizeof output_dpms;
393         output_dpms.base.object_stamp = private_output->stamp;
394         output_dpms.base.data = NULL;
395         output_dpms.base.sync = 0;
396         output_dpms.dpms = dpms;
397
398         ret = tdm_thread_cb_call(private_output, &output_dpms.base, 1);
399         TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
400
401         return TDM_ERROR_NONE;
402 }
403
404 INTERN void
405 tdm_output_cb_status(tdm_output *output_backend, tdm_output_conn_status status, void *user_data)
406 {
407         tdm_private_output *private_output = user_data;
408         tdm_error ret;
409
410         TDM_RETURN_IF_FAIL(private_output);
411
412         TDM_INFO("output(%d) %s", private_output->pipe, tdm_status_str(status));
413
414         if ((private_output->caps.status == TDM_OUTPUT_CONN_STATUS_DISCONNECTED && status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) ||
415                 (private_output->caps.status != TDM_OUTPUT_CONN_STATUS_DISCONNECTED && status == TDM_OUTPUT_CONN_STATUS_DISCONNECTED)) {
416                 ret = tdm_display_update_output(private_output->private_module, output_backend);
417                 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
418         } else {
419                 private_output->caps.status = status;
420         }
421
422         ret = _tdm_output_call_thread_cb_status(private_output, status);
423         TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
424 }
425
426 INTERN void
427 tdm_output_cb_dpms(tdm_output *output_backend, tdm_output_dpms dpms, void *user_data)
428 {
429         tdm_private_output *private_output = user_data;
430         tdm_error ret;
431
432         TDM_INFO("output(%d) %s", private_output->pipe, tdm_status_str(dpms));
433
434         private_output->current_dpms_value = dpms;
435         private_output->waiting_dpms_change = 0;
436         TDM_INFO("output(%d) dpms async '%s' done", private_output->pipe, tdm_dpms_str(dpms));
437
438         ret = _tdm_output_call_thread_cb_dpms(private_output, dpms);
439         TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
440 }
441
442 EXTERN tdm_error
443 tdm_output_add_change_handler(tdm_output *output,
444                                                           tdm_output_change_handler func,
445                                                           void *user_data)
446 {
447         tdm_private_output_change_handler *change_handler = NULL;
448         OUTPUT_FUNC_ENTRY();
449
450         TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
451
452         _pthread_mutex_lock(&private_display->lock);
453
454         LIST_FOR_EACH_ENTRY(change_handler, &private_output->change_handler_list, link) {
455                 if (change_handler->func == func && change_handler->user_data == user_data) {
456                         TDM_ERR("can't add twice");
457                         _pthread_mutex_unlock(&private_display->lock);
458                         return TDM_ERROR_BAD_REQUEST;
459                 }
460         }
461
462         change_handler = calloc(1, sizeof(tdm_private_output_change_handler));
463         if (!change_handler) {
464                 /* LCOV_EXCL_START */
465                 TDM_ERR("failed: alloc memory");
466                 _pthread_mutex_unlock(&private_display->lock);
467                 return TDM_ERROR_OUT_OF_MEMORY;
468                 /* LCOV_EXCL_STOP */
469         }
470
471         ret = tdm_thread_cb_add(private_output, TDM_THREAD_CB_OUTPUT_DPMS, NULL, tdm_output_thread_cb_change, change_handler);
472         if (ret != TDM_ERROR_NONE) {
473                 /* LCOV_EXCL_START */
474                 TDM_ERR("tdm_thread_cb_add failed");
475                 free(change_handler);
476                 _pthread_mutex_unlock(&private_display->lock);
477                 return TDM_ERROR_OPERATION_FAILED;
478                 /* LCOV_EXCL_STOP */
479         }
480
481         ret = tdm_thread_cb_add(private_output, TDM_THREAD_CB_OUTPUT_STATUS, NULL, tdm_output_thread_cb_change, change_handler);
482         if (ret != TDM_ERROR_NONE) {
483                 /* LCOV_EXCL_START */
484                 TDM_ERR("tdm_thread_cb_add failed");
485                 tdm_thread_cb_remove(private_output, TDM_THREAD_CB_OUTPUT_DPMS, NULL, tdm_output_thread_cb_change, change_handler);
486                 free(change_handler);
487                 _pthread_mutex_unlock(&private_display->lock);
488                 return TDM_ERROR_OPERATION_FAILED;
489                 /* LCOV_EXCL_STOP */
490         }
491
492         change_handler->private_output = private_output;
493         change_handler->func = func;
494         change_handler->user_data = user_data;
495         change_handler->owner_tid = syscall(SYS_gettid);
496
497         LIST_ADDTAIL(&change_handler->link, &private_output->change_handler_list);
498
499         _pthread_mutex_unlock(&private_display->lock);
500
501         return ret;
502 }
503
504 EXTERN tdm_error
505 tdm_output_add_mode_change_request_handler(tdm_output *output,
506                                                                                    tdm_output_mode_change_request_handler func,
507                                                                                    void *user_data)
508 {
509         tdm_private_display *private_display;
510         tdm_private_output *private_output;
511         tdm_private_output_mode_change_handler *mode_change_handler = NULL;
512
513         TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
514         TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
515
516         private_output = (tdm_private_output*)output;
517         private_display = private_output->private_display;
518
519         _pthread_mutex_lock(&private_display->lock);
520
521         LIST_FOR_EACH_ENTRY(mode_change_handler, &private_output->mode_change_request_handler_list, link) {
522                 if (mode_change_handler->func == func && mode_change_handler->user_data == user_data) {
523                         TDM_ERR("can't add twice");
524                         _pthread_mutex_unlock(&private_display->lock);
525                         return TDM_ERROR_BAD_REQUEST;
526                 }
527         }
528
529         mode_change_handler = calloc(1, sizeof(tdm_private_output_mode_change_handler));
530         if (!mode_change_handler) {
531                 /* LCOV_EXCL_START */
532                 TDM_ERR("failed: alloc memory");
533                 _pthread_mutex_unlock(&private_display->lock);
534                 return TDM_ERROR_OUT_OF_MEMORY;
535                 /* LCOV_EXCL_STOP */
536         }
537
538         mode_change_handler->private_output = private_output;
539         mode_change_handler->func = func;
540         mode_change_handler->user_data = user_data;
541
542         LIST_ADDTAIL(&mode_change_handler->link, &private_output->mode_change_request_handler_list);
543
544         _pthread_mutex_unlock(&private_display->lock);
545
546         return TDM_ERROR_NONE;
547 }
548
549 EXTERN tdm_error
550 tdm_output_remove_mode_change_request_handler(tdm_output *output,
551                                                                                           tdm_output_mode_change_request_handler func,
552                                                                                           void *user_data)
553 {
554         tdm_private_display *private_display;
555         tdm_private_output *private_output;
556         tdm_private_output_mode_change_handler *mode_change_handler = NULL, *hh = NULL;
557
558         TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
559         TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
560
561         private_output = (tdm_private_output*)output;
562         private_display = private_output->private_display;
563
564         _pthread_mutex_lock(&private_display->lock);
565
566         LIST_FOR_EACH_ENTRY_SAFE(mode_change_handler, hh, &private_output->mode_change_request_handler_list, link) {
567                 if (mode_change_handler->func != func || mode_change_handler->user_data != user_data)
568                         continue;
569
570                 LIST_DEL(&mode_change_handler->link);
571                 free(mode_change_handler);
572
573                 _pthread_mutex_unlock(&private_display->lock);
574
575                 return TDM_ERROR_NONE;
576         }
577
578         _pthread_mutex_unlock(&private_display->lock);
579
580         return TDM_ERROR_INVALID_PARAMETER;
581 }
582
583 EXTERN void
584 tdm_output_remove_change_handler(tdm_output *output,
585                                                                  tdm_output_change_handler func,
586                                                                  void *user_data)
587 {
588         tdm_private_display *private_display;
589         tdm_private_output *private_output;
590         tdm_private_output_change_handler *change_handler = NULL, *hh = NULL;
591
592         TDM_RETURN_IF_FAIL(output != NULL);
593         TDM_RETURN_IF_FAIL(func != NULL);
594
595         private_output = (tdm_private_output*)output;
596         private_display = private_output->private_display;
597
598         _pthread_mutex_lock(&private_display->lock);
599
600         LIST_FOR_EACH_ENTRY_SAFE(change_handler, hh, &private_output->change_handler_list, link) {
601                 if (change_handler->func != func || change_handler->user_data != user_data)
602                         continue;
603
604                 tdm_thread_cb_remove(private_output, TDM_THREAD_CB_OUTPUT_DPMS, NULL, tdm_output_thread_cb_change, change_handler);
605                 tdm_thread_cb_remove(private_output, TDM_THREAD_CB_OUTPUT_STATUS, NULL, tdm_output_thread_cb_change, change_handler);
606
607                 LIST_DEL(&change_handler->link);
608                 free(change_handler);
609
610                 _pthread_mutex_unlock(&private_display->lock);
611
612                 return;
613         }
614
615         _pthread_mutex_unlock(&private_display->lock);
616 }
617
618 EXTERN tdm_error
619 tdm_output_get_output_type(tdm_output *output, tdm_output_type *type)
620 {
621         OUTPUT_FUNC_ENTRY();
622
623         TDM_RETURN_VAL_IF_FAIL(type != NULL, TDM_ERROR_INVALID_PARAMETER);
624
625         _pthread_mutex_lock(&private_display->lock);
626
627         *type = private_output->caps.type;
628
629         _pthread_mutex_unlock(&private_display->lock);
630
631         return ret;
632 }
633
634 EXTERN tdm_error
635 tdm_output_get_layer_count(tdm_output *output, int *count)
636 {
637         tdm_private_layer *private_layer = NULL;
638
639         OUTPUT_FUNC_ENTRY();
640
641         TDM_RETURN_VAL_IF_FAIL(count != NULL, TDM_ERROR_INVALID_PARAMETER);
642
643         _pthread_mutex_lock(&private_display->lock);
644
645         if (private_output->caps.capabilities & TDM_OUTPUT_CAPABILITY_HWC) {
646                 TDM_ERR("output(%p) support HWC. Use HWC functions", private_output);
647                 *count = 0;
648                 _pthread_mutex_unlock(&private_display->lock);
649                 return TDM_ERROR_BAD_REQUEST;
650         }
651
652         *count = 0;
653         LIST_FOR_EACH_ENTRY(private_layer, &private_output->layer_list, link)
654                 (*count)++;
655         if (*count == 0) {
656                 _pthread_mutex_unlock(&private_display->lock);
657                 return TDM_ERROR_NONE;
658         }
659
660         _pthread_mutex_unlock(&private_display->lock);
661
662         return ret;
663 }
664
665
666 EXTERN tdm_layer *
667 tdm_output_get_layer(tdm_output *output, int index, tdm_error *error)
668 {
669         tdm_private_layer *private_layer = NULL;
670
671         OUTPUT_FUNC_ENTRY_ERROR();
672
673         _pthread_mutex_lock(&private_display->lock);
674
675         if (error)
676                 *error = TDM_ERROR_NONE;
677
678         if (private_output->caps.capabilities & TDM_OUTPUT_CAPABILITY_HWC) {
679                 _pthread_mutex_unlock(&private_display->lock);
680                 TDM_ERR("output(%p) support HWC. Use HWC functions", private_output);
681                 if (error)
682                         *error = TDM_ERROR_BAD_REQUEST;
683                 return NULL;
684         }
685
686         LIST_FOR_EACH_ENTRY(private_layer, &private_output->layer_list, link) {
687                 if (private_layer->index == index) {
688                         _pthread_mutex_unlock(&private_display->lock);
689                         return private_layer;
690                 }
691         }
692
693         _pthread_mutex_unlock(&private_display->lock);
694
695         return NULL;
696 }
697
698 EXTERN tdm_error
699 tdm_output_get_available_properties(tdm_output *output, const tdm_prop **props,
700                                                                         int *count)
701 {
702         OUTPUT_FUNC_ENTRY();
703
704         TDM_RETURN_VAL_IF_FAIL(props != NULL, TDM_ERROR_INVALID_PARAMETER);
705         TDM_RETURN_VAL_IF_FAIL(count != NULL, TDM_ERROR_INVALID_PARAMETER);
706
707         _pthread_mutex_lock(&private_display->lock);
708
709         *props = (const tdm_prop *)private_output->caps.props;
710         *count = private_output->caps.prop_count;
711
712         _pthread_mutex_unlock(&private_display->lock);
713
714         return ret;
715 }
716
717 EXTERN tdm_error
718 tdm_output_get_available_modes(tdm_output *output,
719                                                            const tdm_output_mode **modes, int *count)
720 {
721         OUTPUT_FUNC_ENTRY();
722
723         TDM_RETURN_VAL_IF_FAIL(modes != NULL, TDM_ERROR_INVALID_PARAMETER);
724         TDM_RETURN_VAL_IF_FAIL(count != NULL, TDM_ERROR_INVALID_PARAMETER);
725
726         _pthread_mutex_lock(&private_display->lock);
727
728         *modes = (const tdm_output_mode *)private_output->caps.modes;
729         *count = private_output->caps.mode_count;
730
731         _pthread_mutex_unlock(&private_display->lock);
732
733         return ret;
734 }
735
736 EXTERN tdm_error
737 tdm_output_get_available_size(tdm_output *output, int *min_w, int *min_h,
738                                                           int *max_w, int *max_h, int *preferred_align)
739 {
740         OUTPUT_FUNC_ENTRY();
741
742         _pthread_mutex_lock(&private_display->lock);
743
744         if (min_w)
745                 *min_w = TDM_FRONT_VALUE(private_output->caps.min_w);
746         if (min_h)
747                 *min_h = TDM_FRONT_VALUE(private_output->caps.min_h);
748         if (max_w)
749                 *max_w = TDM_FRONT_VALUE(private_output->caps.max_w);
750         if (max_h)
751                 *max_h = TDM_FRONT_VALUE(private_output->caps.max_h);
752         if (preferred_align)
753                 *preferred_align = TDM_FRONT_VALUE(private_output->caps.preferred_align);
754
755         _pthread_mutex_unlock(&private_display->lock);
756
757         return ret;
758 }
759
760 EXTERN tdm_error
761 tdm_output_get_cursor_available_size(tdm_output *output, int *min_w, int *min_h,
762                                                                          int *max_w, int *max_h, int *preferred_align)
763 {
764         OUTPUT_FUNC_ENTRY();
765
766         _pthread_mutex_lock(&private_display->lock);
767
768         if (!tdm_module_check_abi(private_output->private_module, 1, 5)) {
769                 _pthread_mutex_unlock(&private_display->lock);
770                 return TDM_ERROR_BAD_REQUEST;
771         }
772
773         if (min_w)
774                 *min_w = TDM_FRONT_VALUE(private_output->caps.cursor_min_w);
775         if (min_h)
776                 *min_h = TDM_FRONT_VALUE(private_output->caps.cursor_min_h);
777         if (max_w)
778                 *max_w = TDM_FRONT_VALUE(private_output->caps.cursor_max_w);
779         if (max_h)
780                 *max_h = TDM_FRONT_VALUE(private_output->caps.cursor_max_h);
781         if (preferred_align)
782                 *preferred_align = TDM_FRONT_VALUE(private_output->caps.cursor_preferred_align);
783
784         _pthread_mutex_unlock(&private_display->lock);
785
786         return ret;
787 }
788
789 EXTERN tdm_error
790 tdm_output_get_physical_size(tdm_output *output, unsigned int *mmWidth,
791                                                          unsigned int *mmHeight)
792 {
793         OUTPUT_FUNC_ENTRY();
794
795         _pthread_mutex_lock(&private_display->lock);
796
797         if (mmWidth)
798                 *mmWidth = private_output->caps.mmWidth;
799         if (mmHeight)
800                 *mmHeight = private_output->caps.mmHeight;
801
802         _pthread_mutex_unlock(&private_display->lock);
803
804         return ret;
805 }
806
807 EXTERN tdm_error
808 tdm_output_get_subpixel(tdm_output *output, unsigned int *subpixel)
809 {
810         OUTPUT_FUNC_ENTRY();
811         TDM_RETURN_VAL_IF_FAIL(subpixel != NULL, TDM_ERROR_INVALID_PARAMETER);
812
813         _pthread_mutex_lock(&private_display->lock);
814
815         *subpixel = private_output->caps.subpixel;
816
817         _pthread_mutex_unlock(&private_display->lock);
818
819         return ret;
820 }
821
822 EXTERN tdm_error
823 tdm_output_get_pipe(tdm_output *output, unsigned int *pipe)
824 {
825         OUTPUT_FUNC_ENTRY();
826         TDM_RETURN_VAL_IF_FAIL(pipe != NULL, TDM_ERROR_INVALID_PARAMETER);
827
828         _pthread_mutex_lock(&private_display->lock);
829
830         *pipe = private_output->pipe;
831
832         _pthread_mutex_unlock(&private_display->lock);
833
834         return ret;
835 }
836
837 EXTERN tdm_error
838 tdm_output_get_primary_index(tdm_output *output, int *index)
839 {
840         tdm_private_layer *private_layer = NULL;
841
842         OUTPUT_FUNC_ENTRY();
843         TDM_RETURN_VAL_IF_FAIL(index != NULL, TDM_ERROR_INVALID_PARAMETER);
844
845         _pthread_mutex_lock(&private_display->lock);
846
847         LIST_FOR_EACH_ENTRY(private_layer, &private_output->layer_list, link) {
848                 if (private_layer->caps.capabilities & TDM_LAYER_CAPABILITY_PRIMARY) {
849                         *index = private_layer->index;
850                         break;
851                 }
852         }
853
854         _pthread_mutex_unlock(&private_display->lock);
855
856         return ret;
857 }
858
859 EXTERN tdm_error
860 tdm_output_set_property(tdm_output *output, unsigned int id, tdm_value value)
861 {
862         tdm_private_module *private_module;
863         tdm_func_output *func_output;
864         OUTPUT_FUNC_ENTRY();
865
866         _pthread_mutex_lock(&private_display->lock);
867
868         private_module = private_output->private_module;
869         func_output = &private_module->func_output;
870
871         if (!func_output->output_set_property) {
872                 /* LCOV_EXCL_START */
873                 _pthread_mutex_unlock(&private_display->lock);
874                 TDM_WRN("not implemented!!");
875                 return TDM_ERROR_NOT_IMPLEMENTED;
876                 /* LCOV_EXCL_STOP */
877         }
878
879         ret = func_output->output_set_property(private_output->output_backend, id,
880                                                                                    value);
881
882         _pthread_mutex_unlock(&private_display->lock);
883
884         return ret;
885 }
886
887 EXTERN tdm_error
888 tdm_output_get_property(tdm_output *output, unsigned int id, tdm_value *value)
889 {
890         tdm_private_module *private_module;
891         tdm_func_output *func_output;
892         OUTPUT_FUNC_ENTRY();
893
894         TDM_RETURN_VAL_IF_FAIL(value != NULL, TDM_ERROR_INVALID_PARAMETER);
895
896         _pthread_mutex_lock(&private_display->lock);
897
898         private_module = private_output->private_module;
899         func_output = &private_module->func_output;
900
901         if (!func_output->output_get_property) {
902                 /* LCOV_EXCL_START */
903                 _pthread_mutex_unlock(&private_display->lock);
904                 TDM_WRN("not implemented!!");
905                 return TDM_ERROR_NOT_IMPLEMENTED;
906                 /* LCOV_EXCL_STOP */
907         }
908
909         ret = func_output->output_get_property(private_output->output_backend, id,
910                                                                                    value);
911
912         _pthread_mutex_unlock(&private_display->lock);
913
914         return ret;
915 }
916
917 static void
918 _tdm_output_thread_cb_vblank(tdm_private_display *private_display, void *object, tdm_thread_cb_base *cb_base, void *user_data)
919 {
920         tdm_thread_cb_output_vblank *output_vblank = (tdm_thread_cb_output_vblank *)cb_base;
921         tdm_private_output_vblank_handler *vblank_handler = output_vblank->base.data;
922         tdm_private_output_vblank_handler *v = NULL, *vv = NULL;
923         tdm_private_output *private_output = object;
924         struct list_head clone_list;
925         int interval, sync;
926         pid_t tid = syscall(SYS_gettid);
927
928         TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
929
930         assert(vblank_handler->owner_tid == tid);
931
932         vblank_handler->sent_to_frontend = 0;
933
934         _tdm_output_vblank_timeout_update(private_output, 0);
935
936         tdm_thread_cb_remove(private_output, TDM_THREAD_CB_OUTPUT_VBLANK, vblank_handler, _tdm_output_thread_cb_vblank, NULL);
937
938         interval = vblank_handler->interval;
939         sync = vblank_handler->sync;
940
941         LIST_INITHEAD(&clone_list);
942
943         LIST_FOR_EACH_ENTRY_SAFE(v, vv, &private_output->vblank_handler_list, link) {
944                 if (v->interval != interval || v->sync != sync || v->owner_tid != tid)
945                         continue;
946
947                 LIST_DEL(&v->link);
948                 LIST_ADDTAIL(&v->link, &clone_list);
949         }
950
951         if (tdm_debug_module & TDM_DEBUG_COMMIT)
952                 TDM_INFO("----------------------------------------- output(%d) got vblank", private_output->pipe);
953
954         _pthread_mutex_unlock(&private_display->lock);
955         LIST_FOR_EACH_ENTRY_SAFE(v, vv, &clone_list, link) {
956                 if (tdm_debug_module & TDM_DEBUG_COMMIT)
957                         TDM_INFO("handler(%p)", v);
958
959                 LIST_DEL(&v->link);
960
961                 if (v->func)
962                         v->func(v->private_output,
963                                         output_vblank->sequence,
964                                         output_vblank->tv_sec,
965                                         output_vblank->tv_usec,
966                                         v->user_data);
967
968                 free(v);
969         }
970         _pthread_mutex_lock(&private_display->lock);
971
972         if (tdm_debug_module & TDM_DEBUG_COMMIT)
973                 TDM_INFO("-----------------------------------------...");
974 }
975
976 static void
977 _tdm_output_cb_vblank(tdm_output *output_backend, unsigned int sequence,
978                                           unsigned int tv_sec, unsigned int tv_usec, void *user_data)
979 {
980         tdm_private_output_vblank_handler *vblank_handler = user_data;
981         tdm_thread_cb_output_vblank output_vblank;
982         tdm_error ret;
983
984         memset(&output_vblank, 0, sizeof output_vblank);
985         output_vblank.base.type = TDM_THREAD_CB_OUTPUT_VBLANK;
986         output_vblank.base.length = sizeof output_vblank;
987         output_vblank.base.object_stamp = vblank_handler->private_output->stamp;
988         output_vblank.base.data = vblank_handler;
989         output_vblank.base.sync = 0;
990         output_vblank.sequence = sequence;
991         output_vblank.tv_sec = tv_sec;
992         output_vblank.tv_usec = tv_usec;
993
994         vblank_handler->sent_to_frontend = 1;
995
996         if (tdm_debug_module & TDM_DEBUG_COMMIT)
997                 TDM_INFO("output(%d) wait_vblank: handler(%p)", vblank_handler->private_output->pipe, vblank_handler);
998
999         ret = tdm_thread_cb_call(vblank_handler->private_output, &output_vblank.base, 1);
1000         TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
1001 }
1002
1003 static void
1004 _tdm_output_thread_cb_commit(tdm_private_display *private_display, void *object, tdm_thread_cb_base *cb_base, void *user_data)
1005 {
1006         tdm_thread_cb_output_commit *output_commit = (tdm_thread_cb_output_commit *)cb_base;
1007         tdm_private_output_commit_handler *output_commit_handler = output_commit->base.data;
1008         tdm_private_output *private_output = object;
1009         tdm_private_layer *private_layer = NULL;
1010
1011         TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
1012
1013         if (!output_commit_handler)
1014                 return;
1015
1016         assert(output_commit_handler->owner_tid == syscall(SYS_gettid));
1017
1018         tdm_thread_cb_remove(private_output, TDM_THREAD_CB_OUTPUT_COMMIT, output_commit_handler, _tdm_output_thread_cb_commit, NULL);
1019
1020         LIST_DEL(&output_commit_handler->link);
1021
1022         if (tdm_debug_module & TDM_DEBUG_COMMIT) {
1023                 TDM_INFO("----------------------------------------- output(%d) committed", private_output->pipe);
1024                 TDM_INFO("handler(%p)", output_commit_handler);
1025         }
1026
1027         if (private_output->commit_type == TDM_COMMIT_TYPE_OUTPUT) {
1028                 /* In case of layer commit, the below will be handled in the layer commit callback */
1029                 LIST_FOR_EACH_ENTRY(private_layer, &private_output->layer_list, link) {
1030                         if (private_layer->committed_buffer)
1031                                 tdm_layer_committed(private_layer, &private_layer->committed_buffer);
1032                 }
1033         }
1034
1035         if (output_commit_handler->func) {
1036                 _pthread_mutex_unlock(&private_display->lock);
1037                 output_commit_handler->func(private_output,
1038                                                                         output_commit->sequence,
1039                                                                         output_commit->tv_sec,
1040                                                                         output_commit->tv_usec,
1041                                                                         output_commit_handler->user_data);
1042                 _pthread_mutex_lock(&private_display->lock);
1043         }
1044
1045         free(output_commit_handler);
1046
1047         if (tdm_debug_module & TDM_DEBUG_COMMIT)
1048                 TDM_INFO("-----------------------------------------...");
1049 }
1050
1051 static void
1052 _tdm_output_cb_commit(tdm_output *output_backend, unsigned int sequence,
1053                                           unsigned int tv_sec, unsigned int tv_usec, void *user_data)
1054 {
1055         tdm_private_output_commit_handler *output_commit_handler = user_data;
1056         tdm_private_output *private_output;
1057         tdm_thread_cb_output_commit output_commit;
1058         tdm_error ret;
1059
1060         if (output_commit_handler)
1061                 private_output = output_commit_handler->private_output;
1062         else
1063                 private_output = tdm_display_find_private_output(tdm_display_get(), output_backend);
1064
1065         memset(&output_commit, 0, sizeof output_commit);
1066         output_commit.base.type = TDM_THREAD_CB_OUTPUT_COMMIT;
1067         output_commit.base.length = sizeof output_commit;
1068         output_commit.base.object_stamp = private_output->stamp;
1069         output_commit.base.data = output_commit_handler;
1070         output_commit.base.sync = 0;
1071         output_commit.sequence = sequence;
1072         output_commit.tv_sec = tv_sec;
1073         output_commit.tv_usec = tv_usec;
1074
1075         ret = tdm_thread_cb_call(private_output, &output_commit.base, 1);
1076         TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
1077 }
1078
1079 static void
1080 _tdm_voutput_thread_cb_commit(tdm_private_display *private_display, void *object, tdm_thread_cb_base *cb_base, void *user_data)
1081 {
1082         tdm_thread_cb_voutput_commit *voutput_commit = (tdm_thread_cb_voutput_commit *)cb_base;
1083         tdm_private_voutput_commit_handler *voutput_commit_handler = voutput_commit->base.data;
1084         tdm_private_voutput *private_voutput = object;
1085         tdm_private_output *private_output = NULL;
1086
1087         TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
1088
1089         if (!voutput_commit_handler)
1090                 return;
1091
1092         assert(voutput_commit_handler->owner_tid == syscall(SYS_gettid));
1093
1094         tdm_thread_cb_remove(private_voutput, TDM_THREAD_CB_VOUTPUT_COMMIT, voutput_commit_handler, _tdm_voutput_thread_cb_commit, NULL);
1095
1096         LIST_DEL(&voutput_commit_handler->link);
1097
1098         private_output = private_voutput->private_output;
1099         if (tdm_debug_module & TDM_DEBUG_COMMIT) {
1100                 TDM_INFO("----------------------------------------- voutput(%d) committed", private_output->pipe);
1101                 TDM_INFO("handler(%p)", voutput_commit_handler);
1102         }
1103
1104         if (voutput_commit_handler->func && private_voutput->set_voutput_commit != 0) {
1105                 _pthread_mutex_unlock(&private_display->lock);
1106                 voutput_commit_handler->func(private_voutput, voutput_commit_handler->user_data);
1107                 _pthread_mutex_lock(&private_display->lock);
1108         }
1109
1110         free(voutput_commit_handler);
1111
1112         if (tdm_debug_module & TDM_DEBUG_COMMIT)
1113                 TDM_INFO("-----------------------------------------...");
1114 }
1115
1116 static void
1117 _tdm_voutput_cb_commit(tdm_voutput *voutput_backend, unsigned int sequence,
1118                                           unsigned int tv_sec, unsigned int tv_usec, void *user_data)
1119 {
1120         tdm_private_voutput_commit_handler *voutput_commit_handler = NULL;
1121         tdm_private_module *private_module;
1122         tdm_private_voutput *private_voutput = NULL, *v = NULL;
1123         tdm_private_output *private_output;
1124         tdm_thread_cb_voutput_commit voutput_commit;
1125         tdm_error ret;
1126
1127         private_module = tdm_display_get()->virtual_module;
1128
1129         LIST_FOR_EACH_ENTRY(v, &private_module->voutput_list, link) {
1130                 if (v->voutput_backend == voutput_backend) {
1131                         private_voutput = v;
1132                         break;
1133                 }
1134         }
1135         if (!private_voutput) {
1136                 TDM_ERR("cannot find voutput");
1137                 return;
1138         }
1139
1140         if (LIST_LENGTH(&private_voutput->voutput_commit_handler_list) == 0) return;
1141
1142         voutput_commit_handler = LIST_FIRST_ENTRY(&private_voutput->voutput_commit_handler_list, tdm_private_voutput_commit_handler, link);
1143
1144         private_output = private_voutput->private_output;
1145
1146         memset(&voutput_commit, 0, sizeof voutput_commit);
1147         voutput_commit.base.type = TDM_THREAD_CB_VOUTPUT_COMMIT;
1148         voutput_commit.base.length = sizeof voutput_commit;
1149         voutput_commit.base.object_stamp = private_output->stamp;
1150         voutput_commit.base.data = voutput_commit_handler;
1151         voutput_commit.base.sync = 0;
1152         voutput_commit.sequence = sequence;
1153         voutput_commit.tv_sec = tv_sec;
1154         voutput_commit.tv_usec = tv_usec;
1155
1156         ret = tdm_thread_cb_call(private_voutput, &voutput_commit.base, 1);
1157         TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
1158 }
1159
1160 /* add_front: To distinguish between the user vblank handlers and the layer
1161  *            commit vblank handlers. The layer commit handlers will be called
1162  *            before calling the user vblank handlers.
1163  */
1164 static tdm_error
1165 _tdm_output_wait_vblank(tdm_private_output *private_output, int interval, int sync,
1166                                                 tdm_output_vblank_handler func, void *user_data,
1167                                                 unsigned int add_front)
1168 {
1169         tdm_private_module *private_module;
1170         tdm_func_output *func_output;
1171         tdm_private_output_vblank_handler *vblank_handler = NULL, *v = NULL;
1172         unsigned int skip_request = 0;
1173         pid_t tid = syscall(SYS_gettid);
1174         tdm_error ret = TDM_ERROR_NONE;
1175
1176         private_module = private_output->private_module;
1177         func_output = &private_module->func_output;
1178
1179         /* interval SHOULD be at least 1 */
1180         if (interval <= 0)
1181                 interval = 1;
1182
1183         if (!func_output->output_wait_vblank) {
1184                 /* LCOV_EXCL_START */
1185                 TDM_WRN("not implemented!!");
1186                 return TDM_ERROR_NOT_IMPLEMENTED;
1187                 /* LCOV_EXCL_STOP */
1188         }
1189
1190         if (!private_output->regist_vblank_cb) {
1191                 private_output->regist_vblank_cb = 1;
1192                 ret = func_output->output_set_vblank_handler(private_output->output_backend,
1193                                 _tdm_output_cb_vblank);
1194         }
1195
1196         vblank_handler = calloc(1, sizeof(tdm_private_output_vblank_handler));
1197         if (!vblank_handler) {
1198                 /* LCOV_EXCL_START */
1199                 TDM_ERR("failed: alloc memory");
1200                 return TDM_ERROR_OUT_OF_MEMORY;
1201                 /* LCOV_EXCL_STOP */
1202         }
1203
1204         if (tdm_debug_module & TDM_DEBUG_COMMIT)
1205                 TDM_INFO("output(%d) wait_vblank: handler(%p)", private_output->pipe, vblank_handler);
1206
1207         LIST_FOR_EACH_ENTRY(v, &private_output->vblank_handler_list, link) {
1208                 if (v->interval == interval && v->sync == sync && v->owner_tid == tid) {
1209                         skip_request = 1;
1210                         break;
1211                 }
1212         }
1213
1214         if (add_front)
1215                 LIST_ADD(&vblank_handler->link, &private_output->vblank_handler_list);
1216         else
1217                 LIST_ADDTAIL(&vblank_handler->link, &private_output->vblank_handler_list);
1218
1219         vblank_handler->private_output = private_output;
1220         vblank_handler->interval = interval;
1221         vblank_handler->sync = sync;
1222         vblank_handler->func = func;
1223         vblank_handler->user_data = user_data;
1224         vblank_handler->owner_tid = tid;
1225
1226         /* If there is the previous request, we can skip to call output_wait_vblank() */
1227         if (!skip_request) {
1228                 ret = tdm_thread_cb_add(private_output, TDM_THREAD_CB_OUTPUT_VBLANK, vblank_handler, _tdm_output_thread_cb_vblank, NULL);
1229                 if (ret != TDM_ERROR_NONE) {
1230                         TDM_ERR("tdm_thread_cb_add failed");
1231                         goto wait_failed;
1232                 }
1233
1234                 ret = func_output->output_wait_vblank(private_output->output_backend, interval,
1235                                                                                           sync, vblank_handler);
1236                 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
1237
1238                 _tdm_output_vblank_timeout_update(private_output, 1000);
1239
1240                 if (tdm_debug_module & TDM_DEBUG_COMMIT)
1241                         TDM_INFO("output(%d) backend wait_vblank", private_output->pipe);
1242         }
1243
1244         return ret;
1245
1246 wait_failed:
1247         /* LCOV_EXCL_START */
1248         if (vblank_handler) {
1249                 tdm_thread_cb_remove(private_output, TDM_THREAD_CB_OUTPUT_VBLANK, vblank_handler, _tdm_output_thread_cb_vblank, NULL);
1250                 LIST_DEL(&vblank_handler->link);
1251                 free(vblank_handler);
1252         }
1253         return ret;
1254         /* LCOV_EXCL_STOP */
1255 }
1256
1257 EXTERN tdm_error
1258 tdm_output_wait_vblank(tdm_output *output, int interval, int sync,
1259                                            tdm_output_vblank_handler func, void *user_data)
1260 {
1261         OUTPUT_FUNC_ENTRY();
1262         TDM_RETURN_VAL_IF_FAIL(interval > 0, TDM_ERROR_INVALID_PARAMETER);
1263
1264         _pthread_mutex_lock(&private_display->lock);
1265
1266         if (TDM_OUTPUT_DPMS_VSYNC_IS_OFF(private_output->current_dpms_value)) {
1267                 TDM_WRN("output(%d) dpms: %s", private_output->pipe,
1268                                 tdm_dpms_str(private_output->current_dpms_value));
1269                 _pthread_mutex_unlock(&private_display->lock);
1270                 return TDM_ERROR_DPMS_OFF;
1271         }
1272
1273         ret = _tdm_output_wait_vblank(private_output, interval, sync, func, user_data, 0);
1274
1275         _pthread_mutex_unlock(&private_display->lock);
1276
1277         return ret;
1278 }
1279
1280 /* LCOV_EXCL_START */
1281 EXTERN tdm_error
1282 tdm_output_wait_vblank_add_front(tdm_output *output, int interval, int sync,
1283                                                                  tdm_output_vblank_handler func, void *user_data)
1284 {
1285         OUTPUT_FUNC_ENTRY();
1286
1287         _pthread_mutex_lock(&private_display->lock);
1288
1289         if (TDM_OUTPUT_DPMS_VSYNC_IS_OFF(private_output->current_dpms_value)) {
1290                 TDM_WRN("output(%d) dpms: %s", private_output->pipe,
1291                                 tdm_dpms_str(private_output->current_dpms_value));
1292                 _pthread_mutex_unlock(&private_display->lock);
1293                 return TDM_ERROR_DPMS_OFF;
1294         }
1295
1296         ret = _tdm_output_wait_vblank(private_output, interval, sync, func, user_data, 1);
1297
1298         _pthread_mutex_unlock(&private_display->lock);
1299
1300         return ret;
1301 }
1302 /* LCOV_EXCL_STOP */
1303
1304 INTERN void
1305 tdm_output_remove_vblank_handler_internal(tdm_output *output, tdm_output_vblank_handler func, void *user_data)
1306 {
1307         tdm_private_output *private_output = (tdm_private_output*)output;
1308         tdm_private_output_vblank_handler *v = NULL;
1309
1310         TDM_RETURN_IF_FAIL(private_output != NULL);
1311         TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
1312
1313         LIST_FOR_EACH_ENTRY(v, &private_output->vblank_handler_list, link) {
1314                 if (v->func == func && v->user_data == user_data) {
1315                         /* only set func & user_data to NULL. It will be freed when an event occurs */
1316                         v->func = NULL;
1317                         v->user_data = NULL;
1318                         break;
1319                 }
1320         }
1321 }
1322
1323 INTERN void
1324 tdm_output_remove_commit_handler_internal(tdm_output *output, tdm_output_commit_handler func, void *user_data)
1325 {
1326         tdm_private_output *private_output = (tdm_private_output*)output;
1327         tdm_private_output_commit_handler *c = NULL;
1328
1329         TDM_RETURN_IF_FAIL(private_output != NULL);
1330         TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
1331
1332         LIST_FOR_EACH_ENTRY(c, &private_output->output_commit_handler_list, link) {
1333                 if (c->func == func && c->user_data == user_data) {
1334                         /* only set func & user_data to NULL. It will be freed when an event occurs */
1335                         c->func = NULL;
1336                         c->user_data = NULL;
1337                         break;
1338                 }
1339         }
1340 }
1341
1342 EXTERN tdm_error
1343 tdm_output_remove_vblank_handler(tdm_output *output, tdm_output_vblank_handler func, void *user_data)
1344 {
1345         OUTPUT_FUNC_ENTRY();
1346
1347         _pthread_mutex_lock(&private_display->lock);
1348
1349         tdm_output_remove_vblank_handler_internal(output, func, user_data);
1350
1351         _pthread_mutex_unlock(&private_display->lock);
1352
1353         return ret;
1354 }
1355
1356 EXTERN tdm_error
1357 tdm_output_remove_commit_handler(tdm_output *output, tdm_output_commit_handler func, void *user_data)
1358 {
1359         OUTPUT_FUNC_ENTRY();
1360
1361         _pthread_mutex_lock(&private_display->lock);
1362
1363         tdm_output_remove_commit_handler_internal(output, func, user_data);
1364
1365         _pthread_mutex_unlock(&private_display->lock);
1366
1367         return ret;
1368 }
1369
1370 INTERN void
1371 tdm_output_request_mode_set(tdm_output *output, unsigned int index)
1372 {
1373         tdm_private_output *private_output = (tdm_private_output*)output;
1374         tdm_private_output_mode_change_handler *mode_change_handler = NULL;
1375
1376         TDM_RETURN_IF_FAIL(private_output != NULL);
1377
1378         if (LIST_IS_EMPTY(&private_output->mode_change_request_handler_list))
1379                 return;
1380
1381         LIST_FOR_EACH_ENTRY(mode_change_handler, &private_output->mode_change_request_handler_list, link) {
1382                 mode_change_handler->func(mode_change_handler->private_output, index, mode_change_handler->user_data);
1383         }
1384 }
1385
1386 static void
1387 _voutput_commit_func(tdm_voutput *voutput, tbm_surface_h buffer)
1388 {
1389         tdm_private_voutput *private_voutput = (tdm_private_voutput *)voutput;
1390         tdm_error ret;
1391
1392         TDM_RETURN_IF_FAIL(voutput != NULL);
1393         TDM_RETURN_IF_FAIL(buffer != NULL);
1394
1395         ret = tdm_voutput_attach_buffer(private_voutput, buffer);
1396         TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
1397         ret = tdm_voutput_commit_buffer(private_voutput);
1398         TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
1399
1400         return;
1401 }
1402
1403 INTERN tdm_error
1404 tdm_output_set_voutput_commit(tdm_voutput *voutput)
1405 {
1406         tdm_private_display *private_display;
1407         tdm_private_output *private_output;
1408         tdm_private_voutput *private_voutput;
1409         tdm_private_voutput_commit_handler *voutput_commit_handler = NULL;
1410         tdm_error ret = TDM_ERROR_NONE;
1411
1412         TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
1413
1414         private_voutput = (tdm_private_voutput *)voutput;
1415         private_display = private_voutput->private_display;
1416         private_output = (tdm_private_output *)private_voutput->private_output;
1417
1418         _pthread_mutex_lock(&private_display->lock);
1419
1420         if (LIST_LENGTH(&private_voutput->voutput_commit_handler_list) != 0) {
1421                 _pthread_mutex_unlock(&private_display->lock);
1422                 return ret;
1423         }
1424
1425         if (!private_voutput->regist_commit_cb) {
1426                 private_voutput->regist_commit_cb = 1;
1427                 ret = tdm_voutput_set_commit_func(private_voutput, _tdm_voutput_cb_commit);
1428                 if (ret != TDM_ERROR_NONE) {
1429                         TDM_ERR("failed: tdm_voutput_set_commit_func");
1430                         return ret;
1431                 }
1432         }
1433
1434         voutput_commit_handler = calloc(1, sizeof(tdm_private_voutput_commit_handler));
1435         if (!voutput_commit_handler) {
1436                 /* LCOV_EXCL_START */
1437                 TDM_ERR("failed: alloc memory");
1438                 _pthread_mutex_unlock(&private_display->lock);
1439                 return TDM_ERROR_OUT_OF_MEMORY;
1440                 /* LCOV_EXCL_STOP */
1441         }
1442
1443         ret = tdm_thread_cb_add(private_output->private_voutput, TDM_THREAD_CB_VOUTPUT_COMMIT, voutput_commit_handler, _tdm_voutput_thread_cb_commit, NULL);
1444         if (ret != TDM_ERROR_NONE) {
1445                 TDM_ERR("tdm_thread_cb_add failed");
1446                 free(voutput_commit_handler);
1447                 _pthread_mutex_unlock(&private_display->lock);
1448                 return ret;
1449         }
1450
1451         LIST_ADDTAIL(&voutput_commit_handler->link, &private_voutput->voutput_commit_handler_list);
1452         voutput_commit_handler->private_voutput = private_voutput;
1453         voutput_commit_handler->func = _voutput_commit_func;
1454         voutput_commit_handler->owner_tid = syscall(SYS_gettid);
1455
1456         private_voutput->set_voutput_commit = 1;
1457
1458         _pthread_mutex_unlock(&private_display->lock);
1459
1460         return ret;
1461 }
1462
1463 INTERN tdm_error
1464 tdm_output_unset_voutput_commit(tdm_voutput *voutput)
1465 {
1466         tdm_private_display *private_display;
1467         tdm_private_output *private_output;
1468         tdm_private_voutput *private_voutput;
1469         tdm_private_voutput_commit_handler *voutput_commit_handler = NULL;
1470         tdm_error ret = TDM_ERROR_NONE;
1471
1472         TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
1473
1474         private_voutput = (tdm_private_voutput *)voutput;
1475         private_display = private_voutput->private_display;
1476         private_output = (tdm_private_output *)private_voutput->private_output;
1477
1478         _pthread_mutex_lock(&private_display->lock);
1479
1480         private_voutput->set_voutput_commit = 0;
1481
1482         if (private_voutput->regist_commit_cb) {
1483                 private_voutput->regist_commit_cb = 0;
1484                 ret = tdm_voutput_set_commit_func(private_voutput, NULL);
1485                 if (ret != TDM_ERROR_NONE) {
1486                         TDM_ERR("failed: tdm_voutput_set_commit_func");
1487                         _pthread_mutex_unlock(&private_display->lock);
1488                         return ret;
1489                 }
1490         }
1491
1492         if (LIST_LENGTH(&private_voutput->voutput_commit_handler_list) == 0) {
1493                 _pthread_mutex_unlock(&private_display->lock);
1494                 return TDM_ERROR_NONE;
1495         }
1496
1497         if (LIST_LENGTH(&private_output->output_commit_handler_list) != 0) {
1498                 //commiting. don't remove voutput commit thread here
1499                 _pthread_mutex_unlock(&private_display->lock);
1500                 return TDM_ERROR_NONE;
1501         }
1502
1503         if (LIST_LENGTH(&private_voutput->voutput_commit_handler_list) != 0) {
1504                 voutput_commit_handler = LIST_FIRST_ENTRY(&private_voutput->voutput_commit_handler_list, tdm_private_voutput_commit_handler, link);
1505         } else {
1506                 _pthread_mutex_unlock(&private_display->lock);
1507                 return TDM_ERROR_NONE;
1508         }
1509
1510         tdm_thread_cb_remove(private_output->private_voutput, TDM_THREAD_CB_VOUTPUT_COMMIT, voutput_commit_handler, _tdm_voutput_thread_cb_commit, NULL);
1511
1512         _pthread_mutex_unlock(&private_display->lock);
1513
1514         return TDM_ERROR_NONE;
1515 }
1516
1517 INTERN tdm_error
1518 tdm_output_commit_internal(tdm_output *output, int sync, tdm_output_commit_handler func, void *user_data)
1519 {
1520         tdm_private_display *private_display;
1521         tdm_private_output *private_output;
1522         tdm_private_voutput *private_voutput;
1523         tdm_private_module *private_module;
1524         tdm_func_output *func_output;
1525         tdm_private_output_commit_handler *output_commit_handler = NULL;
1526         tdm_private_voutput_commit_handler *voutput_commit_handler = NULL;
1527         tdm_private_layer *private_layer = NULL;
1528         tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_ON;
1529         tdm_error ret = TDM_ERROR_NONE;
1530
1531         TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
1532
1533         private_output = (tdm_private_output*)output;
1534         private_module = private_output->private_module;
1535         private_display = private_module->private_display;
1536         func_output = &private_module->func_output;
1537
1538         if (!func_output->output_commit) {
1539                 /* LCOV_EXCL_START */
1540                 TDM_WRN("not implemented!!");
1541                 return TDM_ERROR_NOT_IMPLEMENTED;
1542                 /* LCOV_EXCL_STOP */
1543         }
1544
1545         ret = tdm_output_get_dpms_internal(output, &dpms_value);
1546         TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, ret);
1547
1548         if (!TDM_OUTPUT_DPMS_VSYNC_IS_OFF(dpms_value)) {
1549                 if (!private_output->regist_commit_cb) {
1550                         private_output->regist_commit_cb = 1;
1551                         ret = func_output->output_set_commit_handler(private_output->output_backend, _tdm_output_cb_commit);
1552                         TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, commit_failed);
1553                 }
1554
1555                 output_commit_handler = calloc(1, sizeof(tdm_private_output_commit_handler));
1556                 if (!output_commit_handler) {
1557                         /* LCOV_EXCL_START */
1558                         TDM_ERR("failed: alloc memory");
1559                         return TDM_ERROR_OUT_OF_MEMORY;
1560                         /* LCOV_EXCL_STOP */
1561                 }
1562
1563                 if (private_module == private_display->virtual_module) {
1564                         if (!private_output->private_voutput) {
1565                                 TDM_ERR("virtual module but don't have voutput");
1566                                 free(output_commit_handler);
1567                                 return TDM_ERROR_BAD_MODULE;
1568                         }
1569                 }
1570
1571                 ret = tdm_thread_cb_add(private_output, TDM_THREAD_CB_OUTPUT_COMMIT, output_commit_handler, _tdm_output_thread_cb_commit, NULL);
1572                 if (ret != TDM_ERROR_NONE) {
1573                         TDM_ERR("tdm_thread_cb_add failed");
1574                         free(output_commit_handler);
1575                         return ret;
1576                 }
1577
1578                 LIST_ADDTAIL(&output_commit_handler->link, &private_output->output_commit_handler_list);
1579                 output_commit_handler->private_output = private_output;
1580                 output_commit_handler->func = func;
1581                 output_commit_handler->user_data = user_data;
1582                 output_commit_handler->owner_tid = syscall(SYS_gettid);
1583
1584                 if (private_module == private_display->virtual_module) {
1585                         private_voutput = private_output->private_voutput;
1586
1587                         /* voutput use only 1 layer */
1588                         private_layer = LIST_FIRST_ENTRY(&private_output->layer_list, tdm_private_layer, link);
1589
1590                         if (LIST_LENGTH(&private_voutput->voutput_commit_handler_list) != 0) {
1591                                 voutput_commit_handler = LIST_FIRST_ENTRY(&private_voutput->voutput_commit_handler_list, tdm_private_voutput_commit_handler, link);
1592                                 voutput_commit_handler->user_data = private_layer->commiting_buffer;
1593                         }
1594                 }
1595
1596                 ret = func_output->output_commit(private_output->output_backend, sync,
1597                                 output_commit_handler);
1598                 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, commit_failed);
1599
1600                 if (tdm_debug_module & TDM_DEBUG_COMMIT)
1601                         TDM_INFO("output(%d) backend commit: handle(%p) func(%p) user_data(%p)",
1602                                         private_output->pipe, output_commit_handler, func, user_data);
1603         }
1604
1605         /* Even if DPMS is off, committed_buffer should be changed because it will be referred
1606          * for tdm_layer_committed() function.
1607          */
1608         LIST_FOR_EACH_ENTRY(private_layer, &private_output->layer_list, link) {
1609                 if (!private_layer->waiting_buffer)
1610                         continue;
1611
1612                 if (private_layer->committed_buffer) {
1613                         tdm_layer_free_buffer(private_layer, private_layer->committed_buffer);
1614                         private_layer->committed_buffer = NULL;
1615                         if (tdm_debug_module & TDM_DEBUG_BUFFER)
1616                                 TDM_INFO("layer(%p) waiting_buffer(%p) committed_buffer(%p)",
1617                                                  private_layer, private_layer->waiting_buffer->buffer,
1618                                                  private_layer->committed_buffer);
1619                 }
1620
1621                 private_layer->committed_buffer = private_layer->waiting_buffer;
1622                 private_layer->waiting_buffer = NULL;
1623                 private_layer->commiting_buffer = NULL;
1624                 if (tdm_debug_module & TDM_DEBUG_BUFFER)
1625                         TDM_INFO("layer(%p) waiting_buffer(%p) committed_buffer(%p)",
1626                                          private_layer, private_layer->waiting_buffer,
1627                                          private_layer->committed_buffer->buffer);
1628         }
1629
1630         if (TDM_OUTPUT_DPMS_VSYNC_IS_OFF(dpms_value)) {
1631                 TDM_WRN("dpms %s. Directly call commit handler instead of commit.", tdm_dpms_str(dpms_value));
1632                 if (func)
1633                         func(output, 0, 0, 0, user_data);
1634         }
1635
1636         return ret;
1637
1638 commit_failed:
1639         /* LCOV_EXCL_START */
1640         if (output_commit_handler) {
1641                 tdm_thread_cb_remove(private_output, TDM_THREAD_CB_OUTPUT_COMMIT, output_commit_handler, _tdm_output_thread_cb_commit, NULL);
1642                 LIST_DEL(&output_commit_handler->link);
1643                 free(output_commit_handler);
1644         }
1645
1646         return ret;
1647         /* LCOV_EXCL_STOP */
1648 }
1649
1650 EXTERN tdm_error
1651 tdm_output_commit(tdm_output *output, int sync, tdm_output_commit_handler func,
1652                                   void *user_data)
1653 {
1654         tdm_private_layer *private_layer = NULL;
1655
1656         OUTPUT_FUNC_ENTRY();
1657
1658         _pthread_mutex_lock(&private_display->lock);
1659
1660         if (private_output->commit_type == TDM_COMMIT_TYPE_NONE)
1661                 private_output->commit_type = TDM_COMMIT_TYPE_OUTPUT;
1662         else if (private_output->commit_type == TDM_COMMIT_TYPE_LAYER) {
1663                 TDM_ERR("Can't supported. Use tdm_layer_commit");
1664                 _pthread_mutex_unlock(&private_display->lock);
1665                 return TDM_ERROR_BAD_REQUEST;
1666         }
1667
1668         if (private_output->commit_per_vblank) {
1669                 TDM_ERR("Use tdm_layer_commit");
1670                 _pthread_mutex_unlock(&private_display->lock);
1671                 return TDM_ERROR_BAD_REQUEST;
1672         }
1673
1674         if (TDM_OUTPUT_DPMS_VSYNC_IS_OFF(private_output->current_dpms_value)) {
1675                 TDM_ERR("output(%d) dpms: %s", private_output->pipe,
1676                                 tdm_dpms_str(private_output->current_dpms_value));
1677                 _pthread_mutex_unlock(&private_display->lock);
1678                 return TDM_ERROR_DPMS_OFF;
1679         }
1680
1681         if (tdm_debug_module & TDM_DEBUG_COMMIT)
1682                 TDM_INFO("output(%d) commit", private_output->pipe);
1683
1684         /* apply the pending data of all layers */
1685         LIST_FOR_EACH_ENTRY(private_layer, &private_output->layer_list, link) {
1686                 tdm_layer_commit_pending_data(private_layer);
1687         }
1688
1689         ret = tdm_output_commit_internal(output, sync, func, user_data);
1690
1691         _pthread_mutex_unlock(&private_display->lock);
1692
1693         return ret;
1694 }
1695
1696 EXTERN tdm_error
1697 tdm_output_set_mode(tdm_output *output, const tdm_output_mode *mode)
1698 {
1699         tdm_private_module *private_module;
1700         tdm_func_output *func_output;
1701         OUTPUT_FUNC_ENTRY();
1702
1703         TDM_RETURN_VAL_IF_FAIL(mode != NULL, TDM_ERROR_INVALID_PARAMETER);
1704
1705         _pthread_mutex_lock(&private_display->lock);
1706
1707         private_module = private_output->private_module;
1708         func_output = &private_module->func_output;
1709
1710         if (!func_output->output_set_mode) {
1711                 /* LCOV_EXCL_START */
1712                 _pthread_mutex_unlock(&private_display->lock);
1713                 TDM_WRN("not implemented!!");
1714                 return TDM_ERROR_NOT_IMPLEMENTED;
1715                 /* LCOV_EXCL_STOP */
1716         }
1717
1718         ret = func_output->output_set_mode(private_output->output_backend, mode);
1719         if (ret == TDM_ERROR_NONE) {
1720                 private_output->current_mode = mode;
1721                 private_output->need_set_target_info = 1;
1722                 TDM_INFO("mode: %dx%d %dhz", mode->hdisplay, mode->vdisplay, mode->vrefresh);
1723         }
1724
1725         _pthread_mutex_unlock(&private_display->lock);
1726
1727         return ret;
1728 }
1729
1730 EXTERN tdm_error
1731 tdm_output_get_mode(tdm_output *output, const tdm_output_mode **mode)
1732 {
1733         OUTPUT_FUNC_ENTRY();
1734
1735         TDM_RETURN_VAL_IF_FAIL(mode != NULL, TDM_ERROR_INVALID_PARAMETER);
1736
1737         _pthread_mutex_lock(&private_display->lock);
1738
1739         *mode = private_output->current_mode;
1740
1741         _pthread_mutex_unlock(&private_display->lock);
1742
1743         return ret;
1744 }
1745
1746 EXTERN tdm_error
1747 tdm_output_set_dpms(tdm_output *output, tdm_output_dpms dpms_value)
1748 {
1749         tdm_private_module *private_module;
1750         tdm_func_output *func_output;
1751         OUTPUT_FUNC_ENTRY();
1752
1753         if (dpms_value > TDM_OUTPUT_DPMS_OFF) {
1754                 if (dpms_value & TDM_OUTPUT_DPMS_DEFAULT_MASK) {
1755                         TDM_ERR("Don't use the low-4bit for an extended DPMS mode: dpms_value(%x)", dpms_value);
1756                         return TDM_ERROR_BAD_REQUEST;
1757                 }
1758
1759                 if (!(private_output->caps.capabilities & TDM_OUTPUT_CAPABILITY_EXTENDED_DPMS)) {
1760                         TDM_ERR("output(%d) doesn't support the extended DPMS control: '%s'",
1761                                         private_output->pipe, tdm_dpms_str(dpms_value));
1762                         return TDM_ERROR_BAD_REQUEST;
1763                 }
1764         }
1765
1766         _pthread_mutex_lock(&private_display->lock);
1767
1768         if (private_output->waiting_dpms_change) {
1769                 TDM_ERR("DPMS is not changed yet. Can't be changed twice");
1770                 _pthread_mutex_unlock(&private_display->lock);
1771                 return TDM_ERROR_BAD_REQUEST;
1772         }
1773
1774         private_module = private_output->private_module;
1775         func_output = &private_module->func_output;
1776
1777         TDM_INFO("output(%d) dpms '%s'", private_output->pipe, tdm_dpms_str(dpms_value));
1778
1779         if (func_output->output_set_dpms)
1780                 ret = func_output->output_set_dpms(private_output->output_backend, dpms_value);
1781         else {
1782                 /* LCOV_EXCL_START */
1783                 ret = TDM_ERROR_NONE;
1784                 TDM_WRN("not implemented!!");
1785                 goto done;
1786                 /* LCOV_EXCL_STOP */
1787         }
1788
1789 done:
1790         if (ret == TDM_ERROR_NONE) {
1791                 if (private_output->current_dpms_value != dpms_value) {
1792                         private_output->current_dpms_value = dpms_value;
1793                         _tdm_output_call_thread_cb_dpms(private_output, dpms_value);
1794                 }
1795                 TDM_INFO("output(%d) dpms '%s' done", private_output->pipe, tdm_dpms_str(dpms_value));
1796         } else {
1797                 tdm_output_dpms temp = TDM_OUTPUT_DPMS_OFF;
1798
1799                 /* update current_dpms_value forcely */
1800                 tdm_output_get_dpms_internal(output, &temp);
1801
1802                 TDM_ERR("output(%d) set_dpms failed: dpms '%s'", private_output->pipe, tdm_dpms_str(temp));
1803         }
1804
1805         _pthread_mutex_unlock(&private_display->lock);
1806
1807         return ret;
1808 }
1809
1810 /* LCOV_EXCL_START */
1811 EXTERN tdm_error
1812 tdm_output_set_dpms_async(tdm_output *output, tdm_output_dpms dpms_value)
1813 {
1814         tdm_private_module *private_module;
1815         tdm_func_output *func_output;
1816         OUTPUT_FUNC_ENTRY();
1817
1818         if (!(private_output->caps.capabilities & TDM_OUTPUT_CAPABILITY_ASYNC_DPMS)) {
1819                 TDM_ERR("output doesn't support the asynchronous DPMS control!");
1820                 return TDM_ERROR_BAD_REQUEST;
1821         }
1822
1823         if (dpms_value > TDM_OUTPUT_DPMS_OFF) {
1824                 if (dpms_value & TDM_OUTPUT_DPMS_DEFAULT_MASK) {
1825                         TDM_ERR("Don't use the low-4bit for an extended DPMS mode: dpms_value(%x)", dpms_value);
1826                         return TDM_ERROR_BAD_REQUEST;
1827                 }
1828
1829                 if (!(private_output->caps.capabilities & TDM_OUTPUT_CAPABILITY_EXTENDED_DPMS)) {
1830                         TDM_ERR("output(%d) doesn't support the extended DPMS control: '%s'",
1831                                         private_output->pipe, tdm_dpms_str(dpms_value));
1832                         return TDM_ERROR_BAD_REQUEST;
1833                 }
1834         }
1835
1836         _pthread_mutex_lock(&private_display->lock);
1837
1838         if (private_output->waiting_dpms_change) {
1839                 TDM_ERR("DPMS is not changed yet. Can't be changed twice");
1840                 _pthread_mutex_unlock(&private_display->lock);
1841                 return TDM_ERROR_BAD_REQUEST;
1842         }
1843
1844         private_module = private_output->private_module;
1845         func_output = &private_module->func_output;
1846         if (!func_output->output_set_dpms_handler) {
1847                 TDM_WRN("not implemented: output_set_dpms_handler");
1848                 _pthread_mutex_unlock(&private_display->lock);
1849                 return TDM_ERROR_NOT_IMPLEMENTED;
1850         }
1851
1852         if (!func_output->output_set_dpms_async) {
1853                 TDM_WRN("not implemented: output_set_dpms_async");
1854                 _pthread_mutex_unlock(&private_display->lock);
1855                 return TDM_ERROR_NOT_IMPLEMENTED;
1856         }
1857
1858         if (!private_output->regist_dpms_cb) {
1859                 private_output->regist_dpms_cb = 1;
1860                 ret = func_output->output_set_dpms_handler(private_output->output_backend,
1861                                 tdm_output_cb_dpms, private_output);
1862                 if (ret != TDM_ERROR_NONE) {
1863                         _pthread_mutex_unlock(&private_display->lock);
1864                         TDM_ERR("Can't set the dpms handler!!");
1865                         return ret;
1866                 }
1867         }
1868
1869         TDM_INFO("output(%d) dpms async '%s'", private_output->pipe, tdm_dpms_str(dpms_value));
1870
1871         ret = func_output->output_set_dpms_async(private_output->output_backend, dpms_value);
1872
1873         if (ret == TDM_ERROR_NONE) {
1874                 private_output->waiting_dpms_change = 1;
1875                 TDM_INFO("output(%d) dpms async '%s' waiting", private_output->pipe, tdm_dpms_str(dpms_value));
1876         } else {
1877                 tdm_output_dpms temp = TDM_OUTPUT_DPMS_OFF;
1878
1879                 /* update current_dpms_value forcely */
1880                 tdm_output_get_dpms_internal(output, &temp);
1881
1882                 TDM_ERR("output(%d) set_dpms_async failed: dpms '%s'", private_output->pipe, tdm_dpms_str(temp));
1883         }
1884
1885         _pthread_mutex_unlock(&private_display->lock);
1886
1887         return ret;
1888 }
1889 /* LCOV_EXCL_STOP */
1890
1891 INTERN tdm_error
1892 tdm_output_get_dpms_internal(tdm_output *output, tdm_output_dpms *dpms_value)
1893 {
1894         tdm_private_output *private_output;
1895         tdm_private_module *private_module;
1896         tdm_func_output *func_output;
1897         tdm_error ret = TDM_ERROR_NONE;
1898
1899         TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
1900
1901         private_output = (tdm_private_output*)output;
1902
1903         /* TODO: this is ugly. But before calling backend's output_get_dpms(), we have
1904          * to check if all backends's DPMS operation has no problem. In future, we'd
1905          * better use new env instead of using commit_per_vblank variable to distinguish
1906          * whether we use the stored value or backend's output_get_dpms.
1907          */
1908         if (!private_output->commit_per_vblank) {
1909                 *dpms_value = private_output->current_dpms_value;
1910                 return TDM_ERROR_NONE;
1911         }
1912
1913         private_module = private_output->private_module;
1914         func_output = &private_module->func_output;
1915
1916         if (!func_output->output_get_dpms) {
1917                 /* LCOV_EXCL_START */
1918                 *dpms_value = private_output->current_dpms_value;
1919                 TDM_WRN("not implemented!!");
1920                 return TDM_ERROR_NONE;
1921                 /* LCOV_EXCL_STOP */
1922         }
1923
1924         ret = func_output->output_get_dpms(private_output->output_backend, dpms_value);
1925         if (ret != TDM_ERROR_NONE) {
1926                 /* LCOV_EXCL_START */
1927                 TDM_ERR("output_get_dpms failed");
1928                 *dpms_value = TDM_OUTPUT_DPMS_OFF;
1929                 /* LCOV_EXCL_STOP */
1930         }
1931
1932         /* checking with backend's value */
1933         if (*dpms_value != private_output->current_dpms_value) {
1934                 TDM_ERR("output(%d) dpms changed suddenly: %s -> %s",
1935                                 private_output->pipe, tdm_dpms_str(private_output->current_dpms_value),
1936                                 tdm_dpms_str(*dpms_value));
1937                 private_output->current_dpms_value = *dpms_value;
1938                 _tdm_output_call_thread_cb_dpms(private_output, *dpms_value);
1939         }
1940
1941         return ret;
1942 }
1943
1944 EXTERN tdm_error
1945 tdm_output_get_dpms(tdm_output *output, tdm_output_dpms *dpms_value)
1946 {
1947         OUTPUT_FUNC_ENTRY();
1948
1949         TDM_RETURN_VAL_IF_FAIL(dpms_value != NULL, TDM_ERROR_INVALID_PARAMETER);
1950
1951         _pthread_mutex_lock(&private_display->lock);
1952
1953         ret = tdm_output_get_dpms_internal(output, dpms_value);
1954
1955         _pthread_mutex_unlock(&private_display->lock);
1956
1957         return ret;
1958 }
1959
1960 EXTERN tdm_error
1961 tdm_output_has_capture_capability(tdm_output *output, unsigned int *has_capability)
1962 {
1963         tdm_private_module *private_module;
1964
1965         OUTPUT_FUNC_ENTRY();
1966
1967         TDM_RETURN_VAL_IF_FAIL(has_capability != NULL, TDM_ERROR_INVALID_PARAMETER);
1968
1969         _pthread_mutex_lock(&private_display->lock);
1970
1971         private_module = private_output->private_module;
1972
1973         if (!(private_module->capabilities & TDM_DISPLAY_CAPABILITY_CAPTURE))
1974                 *has_capability = 0;
1975         else if (!(private_module->caps_capture.capabilities & TDM_CAPTURE_CAPABILITY_OUTPUT))
1976                 *has_capability = 0;
1977         else
1978                 *has_capability = 1;
1979
1980         _pthread_mutex_unlock(&private_display->lock);
1981
1982         return ret;
1983 }
1984
1985 EXTERN tdm_capture *
1986 tdm_output_create_capture(tdm_output *output, tdm_error *error)
1987 {
1988         tdm_capture *capture = NULL;
1989
1990         OUTPUT_FUNC_ENTRY_ERROR();
1991
1992         _pthread_mutex_lock(&private_display->lock);
1993
1994         capture = (tdm_capture *)tdm_capture_create_output_internal(private_output, error);
1995
1996         _pthread_mutex_unlock(&private_display->lock);
1997
1998         return capture;
1999 }
2000
2001 EXTERN tdm_hwc *
2002 tdm_output_get_hwc(tdm_output *output, tdm_error *error)
2003 {
2004         tdm_private_hwc *private_hwc = NULL;
2005
2006         OUTPUT_FUNC_ENTRY_ERROR();
2007
2008         _pthread_mutex_lock(&private_display->lock);
2009
2010         private_hwc = private_output->private_hwc;
2011         if (!private_hwc) {
2012                 if (error)
2013                         *error = TDM_ERROR_BAD_REQUEST;
2014                 _pthread_mutex_unlock(&private_display->lock);
2015                 return NULL;
2016         }
2017
2018         if (error)
2019                 *error = TDM_ERROR_NONE;
2020
2021         _pthread_mutex_unlock(&private_display->lock);
2022
2023         return private_hwc;
2024 }
2025
2026 INTERN tdm_error
2027 tdm_output_choose_commit_per_vblank_mode(tdm_private_output *private_output, int mode)
2028 {
2029         if (!private_output)
2030                 return TDM_ERROR_INVALID_PARAMETER;
2031
2032         if (mode < 0 || mode > 2)
2033                 return TDM_ERROR_INVALID_PARAMETER;
2034
2035         private_output->commit_per_vblank = mode;
2036
2037         if (private_output->commit_per_vblank == 0)
2038                 TDM_INFO("commit per vblank: disable");
2039         else if (private_output->commit_per_vblank == 1)
2040                 TDM_INFO("commit per vblank: enable (1 layer)");
2041         else if (private_output->commit_per_vblank == 2)
2042                 TDM_INFO("commit per vblank: enable (previous commit)");
2043
2044         return TDM_ERROR_NONE;
2045 }
2046
2047 INTERN tdm_voutput *
2048 tdm_voutput_create(tdm_display *dpy, const char *name, tdm_error *error)
2049 {
2050         tdm_private_module *private_module = NULL;
2051         tdm_private_module *current_module = NULL;
2052         tdm_private_voutput *private_voutput = NULL;
2053         tdm_private_output *private_output = NULL;
2054         tdm_func_display *func_display = NULL;
2055         tdm_func_voutput *func_voutput = NULL;
2056         tdm_voutput *voutput_backend = NULL;
2057         tdm_output *output_backend = NULL;
2058         int output_find = 0;
2059         tdm_private_display *private_display;
2060         tdm_error ret = TDM_ERROR_NONE;
2061
2062         TDM_RETURN_VAL_IF_FAIL_WITH_ERROR(dpy != NULL, TDM_ERROR_INVALID_PARAMETER, NULL);
2063         private_display = (tdm_private_display*)dpy;
2064         private_module = private_display->virtual_module;
2065         TDM_RETURN_VAL_IF_FAIL_WITH_ERROR(private_module != NULL, TDM_ERROR_BAD_MODULE, NULL);
2066
2067         _pthread_mutex_lock(&private_display->lock);
2068
2069         if (error) *error = TDM_ERROR_NONE;
2070
2071         private_voutput = calloc(1, sizeof(tdm_private_voutput));
2072         if (!private_voutput) {
2073                 if (error) *error = TDM_ERROR_OUT_OF_MEMORY;
2074                 _pthread_mutex_unlock(&private_display->lock);
2075                 return NULL;
2076         }
2077
2078         func_display = &private_module->func_display;
2079         func_voutput = &private_module->func_voutput;
2080         current_module = private_display->current_module;
2081         private_display->current_module = private_module;
2082
2083         voutput_backend = func_display->voutput_create(private_module->bdata, name, &ret);
2084
2085         if (voutput_backend == NULL || ret != TDM_ERROR_NONE) {
2086                 TDM_ERR("voutput_create fail");
2087                 free(private_voutput);
2088                 if (error) *error = ret;
2089                 private_display->current_module = current_module;
2090                 _pthread_mutex_unlock(&private_display->lock);
2091                 return NULL;
2092         }
2093         private_voutput->voutput_backend = voutput_backend;
2094         private_voutput->private_display = private_display;
2095         private_voutput->private_module = private_module;
2096         LIST_INITHEAD(&private_voutput->voutput_commit_handler_list);
2097
2098         output_backend = func_voutput->voutput_get_output(voutput_backend, &ret);
2099         if (output_backend == NULL || ret != TDM_ERROR_NONE) {
2100                 TDM_ERR("voutput_get_output fail");
2101                 free(private_voutput);
2102                 if (error) *error = ret;
2103                 if (func_voutput->voutput_destroy)
2104                         func_voutput->voutput_destroy(voutput_backend);
2105                 else
2106                         TDM_ERR("no destroy function");
2107                 private_display->current_module = current_module;
2108                 _pthread_mutex_unlock(&private_display->lock);
2109                 return NULL;
2110         }
2111
2112         ret = tdm_display_update_output(private_display->current_module, output_backend);
2113         if (ret != TDM_ERROR_NONE) {
2114                 TDM_ERR("tdm_display_update_output fail");
2115                 free(private_voutput);
2116                 if (func_voutput->voutput_destroy)
2117                         func_voutput->voutput_destroy(voutput_backend);
2118                 else
2119                         TDM_ERR("no destroy function");
2120                 if (error) *error = ret;
2121                 private_display->current_module = current_module;
2122                 _pthread_mutex_unlock(&private_display->lock);
2123                 return NULL;
2124         }
2125
2126         LIST_FOR_EACH_ENTRY(private_output, &private_module->output_list, link) {
2127                 if (private_output->output_backend == output_backend) {
2128                         output_find = 1;
2129                         break;
2130                 }
2131         }
2132
2133         if (output_find != 1) {
2134                 private_output = NULL;
2135                 free(private_voutput);
2136                 if (func_voutput->voutput_destroy)
2137                         func_voutput->voutput_destroy(voutput_backend);
2138                 else
2139                         TDM_ERR("no destroy function");
2140                 private_voutput = NULL;
2141         } else {
2142                 strncpy(private_voutput->name, name, TDM_NAME_LEN - 1);
2143                 private_voutput->name[TDM_NAME_LEN - 1] = '\0';
2144                 strncpy(private_output->name, name, TDM_NAME_LEN - 1);
2145                 private_output->name[TDM_NAME_LEN - 1] = '\0';
2146
2147                 private_voutput->private_output = private_output;
2148                 private_output->private_voutput = private_voutput;
2149
2150                 /* do not use vblank */
2151                 tdm_output_choose_commit_per_vblank_mode(private_output, 0);
2152
2153                 LIST_ADDTAIL(&private_voutput->link, &private_module->voutput_list);
2154         }
2155
2156         private_display->current_module = current_module;
2157
2158         _pthread_mutex_unlock(&private_display->lock);
2159
2160         return private_voutput;
2161 }
2162
2163 INTERN tdm_error
2164 tdm_voutput_destroy(tdm_voutput *voutput)
2165 {
2166         tdm_private_module *private_module = NULL;
2167         tdm_voutput *voutput_backend = NULL;
2168         tdm_func_voutput *func_voutput = NULL;
2169
2170         tdm_private_display *private_display;
2171         tdm_private_voutput *private_voutput;
2172         tdm_private_output *private_output;
2173         tdm_error ret = TDM_ERROR_NONE;
2174         TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
2175         private_voutput = (tdm_private_voutput*)voutput;
2176         private_display = private_voutput->private_display;
2177         private_module = private_voutput->private_module;
2178         TDM_RETURN_VAL_IF_FAIL(private_module == private_display->virtual_module, TDM_ERROR_BAD_MODULE);
2179
2180         _pthread_mutex_lock(&private_display->lock);
2181
2182         func_voutput = &private_module->func_voutput;
2183         voutput_backend = private_voutput->voutput_backend;
2184         private_output = private_voutput->private_output;
2185         tdm_display_destroy_private_output(private_output);
2186         LIST_DEL(&private_voutput->link);
2187         free(private_voutput);
2188         if (func_voutput->voutput_destroy)
2189                 ret = func_voutput->voutput_destroy(voutput_backend);
2190
2191         _pthread_mutex_unlock(&private_display->lock);
2192
2193         return ret;
2194 }
2195
2196 INTERN tdm_error
2197 tdm_voutput_set_available_mode(tdm_voutput *voutput, const tdm_output_mode *modes, int count)
2198 {
2199         tdm_private_display *private_display;
2200         tdm_private_module *private_module = NULL;
2201         tdm_private_voutput *private_voutput;
2202         tdm_func_voutput *func_voutput = NULL;
2203         tdm_error ret = TDM_ERROR_NONE;
2204
2205         TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
2206         TDM_RETURN_VAL_IF_FAIL(modes != NULL, TDM_ERROR_INVALID_PARAMETER);
2207         TDM_RETURN_VAL_IF_FAIL(count != 0, TDM_ERROR_INVALID_PARAMETER);
2208
2209         private_voutput = (tdm_private_voutput*)voutput;
2210         private_display = private_voutput->private_display;
2211         private_module = private_voutput->private_module;
2212         TDM_RETURN_VAL_IF_FAIL(private_module == private_display->virtual_module, TDM_ERROR_BAD_MODULE);
2213
2214         _pthread_mutex_lock(&private_display->lock);
2215
2216         func_voutput = &private_module->func_voutput;
2217         if (func_voutput->voutput_set_available_mode)
2218                 ret = func_voutput->voutput_set_available_mode(private_voutput->voutput_backend, modes, count);
2219         else
2220                 ret = TDM_ERROR_NOT_IMPLEMENTED;
2221
2222         _pthread_mutex_unlock(&private_display->lock);
2223
2224         return ret;
2225 }
2226
2227 INTERN tdm_error
2228 tdm_voutput_set_physical_size(tdm_voutput *voutput, unsigned int mmwidth, unsigned int mmheight)
2229 {
2230         tdm_private_display *private_display;
2231         tdm_private_module *private_module = NULL;
2232         tdm_private_voutput *private_voutput;
2233         tdm_func_voutput *func_voutput = NULL;
2234         tdm_error ret = TDM_ERROR_NONE;
2235
2236         TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
2237         TDM_RETURN_VAL_IF_FAIL(mmwidth != 0, TDM_ERROR_INVALID_PARAMETER);
2238         TDM_RETURN_VAL_IF_FAIL(mmheight != 0, TDM_ERROR_INVALID_PARAMETER);
2239
2240         private_voutput = (tdm_private_voutput*)voutput;
2241         private_display = private_voutput->private_display;
2242         private_module = private_voutput->private_module;
2243         TDM_RETURN_VAL_IF_FAIL(private_module == private_display->virtual_module, TDM_ERROR_BAD_MODULE);
2244
2245         _pthread_mutex_lock(&private_display->lock);
2246
2247         func_voutput = &private_module->func_voutput;
2248         if (func_voutput->voutput_set_physical_size)
2249                 ret = func_voutput->voutput_set_physical_size(private_voutput->voutput_backend, mmwidth, mmheight);
2250         else
2251                 ret = TDM_ERROR_NOT_IMPLEMENTED;
2252
2253         _pthread_mutex_unlock(&private_display->lock);
2254
2255         return ret;
2256 }
2257
2258 INTERN tdm_error
2259 tdm_voutput_connect(tdm_voutput *voutput)
2260 {
2261         tdm_private_display *private_display;
2262         tdm_private_module *private_module = NULL;
2263         tdm_private_voutput *private_voutput;
2264         tdm_func_voutput *func_voutput = NULL;
2265         tdm_error ret = TDM_ERROR_NONE;
2266
2267         TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
2268
2269         private_voutput = (tdm_private_voutput*)voutput;
2270         private_display = private_voutput->private_display;
2271         private_module = private_voutput->private_module;
2272         TDM_RETURN_VAL_IF_FAIL(private_module == private_display->virtual_module, TDM_ERROR_BAD_MODULE);
2273
2274         _pthread_mutex_lock(&private_display->lock);
2275
2276         func_voutput = &private_module->func_voutput;
2277         if (func_voutput->voutput_connect)
2278                 ret = func_voutput->voutput_connect(private_voutput->voutput_backend);
2279         else
2280                 ret = TDM_ERROR_NOT_IMPLEMENTED;
2281
2282         _pthread_mutex_unlock(&private_display->lock);
2283
2284         return ret;
2285 }
2286
2287 INTERN tdm_error
2288 tdm_voutput_disconnect(tdm_voutput *voutput)
2289 {
2290         tdm_private_display *private_display;
2291         tdm_private_module *private_module = NULL;
2292         tdm_private_voutput *private_voutput;
2293         tdm_func_voutput *func_voutput = NULL;
2294         tdm_error ret = TDM_ERROR_NONE;
2295
2296         TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
2297
2298         private_voutput = (tdm_private_voutput*)voutput;
2299         private_display = private_voutput->private_display;
2300         private_module = private_voutput->private_module;
2301         TDM_RETURN_VAL_IF_FAIL(private_module == private_display->virtual_module, TDM_ERROR_BAD_MODULE);
2302
2303         _pthread_mutex_lock(&private_display->lock);
2304
2305         func_voutput = &private_module->func_voutput;
2306         if (func_voutput->voutput_disconnect)
2307                 ret = func_voutput->voutput_disconnect(private_voutput->voutput_backend);
2308         else
2309                 ret = TDM_ERROR_NOT_IMPLEMENTED;
2310
2311         _pthread_mutex_unlock(&private_display->lock);
2312
2313         return ret;
2314 }
2315
2316 /* LCOV_EXCL_START */
2317 INTERN tdm_error
2318 tdm_voutput_set_commit_func(tdm_voutput *voutput, tdm_voutput_commit_handler func)
2319 {
2320         tdm_private_display *private_display;
2321         tdm_private_module *private_module = NULL;
2322         tdm_private_voutput *private_voutput;
2323         tdm_func_voutput *func_voutput = NULL;
2324         tdm_error ret = TDM_ERROR_NONE;
2325
2326         TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
2327
2328         private_voutput = (tdm_private_voutput*)voutput;
2329         private_display = private_voutput->private_display;
2330         private_module = private_voutput->private_module;
2331         TDM_RETURN_VAL_IF_FAIL(private_module == private_display->virtual_module, TDM_ERROR_BAD_MODULE);
2332
2333         func_voutput = &private_module->func_voutput;
2334         if (func_voutput->voutput_set_commit_func)
2335                 ret = func_voutput->voutput_set_commit_func(private_voutput->voutput_backend, func);
2336         else
2337                 ret = TDM_ERROR_NOT_IMPLEMENTED;
2338
2339         return ret;
2340 }
2341
2342 INTERN tdm_error
2343 tdm_voutput_commit_done(tdm_voutput *voutput)
2344 {
2345         tdm_private_display *private_display;
2346         tdm_private_module *private_module = NULL;
2347         tdm_private_voutput *private_voutput;
2348         tdm_func_voutput *func_voutput = NULL;
2349         tdm_error ret = TDM_ERROR_NONE;
2350
2351         TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
2352
2353         private_voutput = (tdm_private_voutput*)voutput;
2354         private_display = private_voutput->private_display;
2355         private_module = private_voutput->private_module;
2356         TDM_RETURN_VAL_IF_FAIL(private_module == private_display->virtual_module, TDM_ERROR_BAD_MODULE);
2357
2358         _pthread_mutex_lock(&private_display->lock);
2359
2360         func_voutput = &private_module->func_voutput;
2361         if (func_voutput->voutput_commit_done)
2362                 ret = func_voutput->voutput_commit_done(private_voutput->voutput_backend);
2363         else
2364                 ret = TDM_ERROR_NOT_IMPLEMENTED;
2365
2366         _pthread_mutex_unlock(&private_display->lock);
2367
2368         return ret;
2369 }
2370 /* LCOV_EXCL_STOP */