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