log: enhance debugging log for DPMS change
[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.h"
41 #include "tdm_backend.h"
42 #include "tdm_private.h"
43 #include "tdm_helper.h"
44
45 #define COUNT_MAX   10
46
47 #define OUTPUT_FUNC_ENTRY() \
48         tdm_private_display *private_display; \
49         tdm_private_output *private_output; \
50         tdm_error ret = TDM_ERROR_NONE; /* default TDM_ERROR_NONE */\
51         TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER); \
52         private_output = (tdm_private_output*)output; \
53         private_display = private_output->private_display
54
55 #define OUTPUT_FUNC_ENTRY_ERROR() \
56         tdm_private_display *private_display; \
57         tdm_private_output *private_output; \
58         tdm_error ret = TDM_ERROR_NONE; /* default TDM_ERROR_NONE */\
59         TDM_RETURN_VAL_IF_FAIL_WITH_ERROR(output != NULL, TDM_ERROR_INVALID_PARAMETER, NULL); \
60         private_output = (tdm_private_output*)output; \
61         private_display = private_output->private_display
62
63 EXTERN tdm_error
64 tdm_output_get_model_info(tdm_output *output, const char **maker,
65                                                   const char **model, const char **name)
66 {
67         OUTPUT_FUNC_ENTRY();
68
69         _pthread_mutex_lock(&private_display->lock);
70
71         if (maker)
72                 *maker = private_output->caps.maker;
73         if (model)
74                 *model = private_output->caps.model;
75         if (name)
76                 *name = private_output->caps.name;
77
78         _pthread_mutex_unlock(&private_display->lock);
79
80         return ret;
81 }
82
83 EXTERN tdm_error
84 tdm_output_get_capabilities(tdm_output *output, tdm_output_capability *capabilities)
85 {
86         OUTPUT_FUNC_ENTRY();
87
88         TDM_RETURN_VAL_IF_FAIL(capabilities != NULL, TDM_ERROR_INVALID_PARAMETER);
89
90         _pthread_mutex_lock(&private_display->lock);
91
92         *capabilities = private_output->caps.capabilities;
93
94         _pthread_mutex_unlock(&private_display->lock);
95
96         return ret;
97 }
98
99 EXTERN tdm_error
100 tdm_output_get_conn_status(tdm_output *output, tdm_output_conn_status *status)
101 {
102         OUTPUT_FUNC_ENTRY();
103
104         TDM_RETURN_VAL_IF_FAIL(status != NULL, TDM_ERROR_INVALID_PARAMETER);
105
106         _pthread_mutex_lock(&private_display->lock);
107
108         *status = private_output->caps.status;
109
110         _pthread_mutex_unlock(&private_display->lock);
111
112         return ret;
113 }
114
115 static void
116 _tdm_output_update(tdm_output *output_backend, void *user_data)
117 {
118         tdm_private_display *private_display;
119         tdm_private_output *private_output = user_data;
120         tdm_error ret;
121
122         TDM_RETURN_IF_FAIL(private_output);
123
124         private_display = private_output->private_display;
125
126         ret = tdm_display_update_output(private_display, output_backend, private_output->pipe);
127         TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
128 }
129
130 INTERN void
131 tdm_output_cb_status(tdm_output *output_backend, tdm_output_conn_status status,
132                                          void *user_data)
133 {
134         tdm_private_display *private_display;
135         tdm_private_output *private_output = user_data;
136         tdm_value value;
137
138         TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
139         TDM_RETURN_IF_FAIL(private_output);
140
141         private_display = private_output->private_display;
142
143         if (!tdm_thread_in_display_thread(syscall(SYS_gettid))) {
144                 tdm_thread_cb_output_status output_status;
145                 tdm_error ret;
146
147                 TDM_INFO("output(%d) %s", private_output->pipe, tdm_status_str(status));
148
149                 _tdm_output_update(output_backend, user_data);
150
151                 output_status.base.type = TDM_THREAD_CB_OUTPUT_STATUS;
152                 output_status.base.length = sizeof output_status;
153                 output_status.output_stamp = private_output->stamp;
154                 output_status.status = status;
155                 output_status.user_data = user_data;
156
157                 value.u32 = status;
158                 tdm_output_call_change_handler_internal(private_output,
159                                                                                                 &private_output->change_handler_list_sub,
160                                                                                                 TDM_OUTPUT_CHANGE_CONNECTION,
161                                                                                                 value, 0);
162
163                 ret = tdm_thread_send_cb(private_display->private_loop, &output_status.base);
164                 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
165
166                 return;
167         }
168
169         TDM_INFO("output(%d) %s", private_output->pipe, tdm_status_str(status));
170
171         if (!tdm_thread_is_running())
172                 _tdm_output_update(output_backend, user_data);
173
174         value.u32 = status;
175         tdm_output_call_change_handler_internal(private_output,
176                                                                                         &private_output->change_handler_list_main,
177                                                                                         TDM_OUTPUT_CHANGE_CONNECTION,
178                                                                                         value, 0);
179 }
180
181 EXTERN tdm_error
182 tdm_output_add_change_handler(tdm_output *output,
183                                                           tdm_output_change_handler func,
184                                                           void *user_data)
185 {
186         tdm_private_change_handler *change_handler;
187         OUTPUT_FUNC_ENTRY();
188
189         TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
190
191         pthread_mutex_lock(&private_display->lock);
192
193         change_handler = calloc(1, sizeof(tdm_private_change_handler));
194         if (!change_handler) {
195                 TDM_ERR("failed: alloc memory");
196                 _pthread_mutex_unlock(&private_display->lock);
197                 return TDM_ERROR_OUT_OF_MEMORY;
198         }
199
200         change_handler->private_output = private_output;
201         change_handler->func = func;
202         change_handler->user_data = user_data;
203         change_handler->owner_tid = syscall(SYS_gettid);
204
205         if (!tdm_thread_in_display_thread(change_handler->owner_tid))
206                 LIST_ADDTAIL(&change_handler->link, &private_output->change_handler_list_sub);
207         else
208                 LIST_ADDTAIL(&change_handler->link, &private_output->change_handler_list_main);
209
210         _pthread_mutex_unlock(&private_display->lock);
211
212         return ret;
213 }
214
215 EXTERN void
216 tdm_output_remove_change_handler(tdm_output *output,
217                                                                  tdm_output_change_handler func,
218                                                                  void *user_data)
219 {
220         tdm_private_display *private_display;
221         tdm_private_output *private_output;
222         tdm_private_change_handler *h = NULL, *hh = NULL;
223
224         TDM_RETURN_IF_FAIL(output != NULL);
225         TDM_RETURN_IF_FAIL(func != NULL);
226
227         private_output = (tdm_private_output*)output;
228         private_display = private_output->private_display;
229
230         _pthread_mutex_lock(&private_display->lock);
231
232         LIST_FOR_EACH_ENTRY_SAFE(h, hh, &private_output->change_handler_list_main, link) {
233                 if (h->func != func || h->user_data != user_data)
234                         continue;
235
236                 LIST_DEL(&h->link);
237                 free(h);
238
239                 _pthread_mutex_unlock(&private_display->lock);
240
241                 return;
242         }
243
244         LIST_FOR_EACH_ENTRY_SAFE(h, hh, &private_output->change_handler_list_sub, link) {
245                 if (h->func != func || h->user_data != user_data)
246                         continue;
247
248                 LIST_DEL(&h->link);
249                 free(h);
250
251                 _pthread_mutex_unlock(&private_display->lock);
252
253                 return;
254         }
255
256         _pthread_mutex_unlock(&private_display->lock);
257 }
258
259 EXTERN tdm_error
260 tdm_output_get_output_type(tdm_output *output, tdm_output_type *type)
261 {
262         OUTPUT_FUNC_ENTRY();
263
264         TDM_RETURN_VAL_IF_FAIL(type != NULL, TDM_ERROR_INVALID_PARAMETER);
265
266         _pthread_mutex_lock(&private_display->lock);
267
268         *type = private_output->caps.type;
269
270         _pthread_mutex_unlock(&private_display->lock);
271
272         return ret;
273 }
274
275 EXTERN tdm_error
276 tdm_output_get_layer_count(tdm_output *output, int *count)
277 {
278         tdm_private_layer *private_layer = NULL;
279
280         OUTPUT_FUNC_ENTRY();
281
282         TDM_RETURN_VAL_IF_FAIL(count != NULL, TDM_ERROR_INVALID_PARAMETER);
283
284         _pthread_mutex_lock(&private_display->lock);
285
286         *count = 0;
287         LIST_FOR_EACH_ENTRY(private_layer, &private_output->layer_list, link)
288         (*count)++;
289         if (*count == 0) {
290                 _pthread_mutex_unlock(&private_display->lock);
291                 return TDM_ERROR_NONE;
292         }
293
294         _pthread_mutex_unlock(&private_display->lock);
295
296         return ret;
297 }
298
299
300 EXTERN tdm_layer *
301 tdm_output_get_layer(tdm_output *output, int index, tdm_error *error)
302 {
303         tdm_private_layer *private_layer = NULL;
304
305         OUTPUT_FUNC_ENTRY_ERROR();
306
307         _pthread_mutex_lock(&private_display->lock);
308
309         if (error)
310                 *error = TDM_ERROR_NONE;
311
312         LIST_FOR_EACH_ENTRY(private_layer, &private_output->layer_list, link) {
313                 if (private_layer->index == index) {
314                         _pthread_mutex_unlock(&private_display->lock);
315                         return private_layer;
316                 }
317         }
318
319         _pthread_mutex_unlock(&private_display->lock);
320
321         return NULL;
322 }
323
324 EXTERN tdm_error
325 tdm_output_get_available_properties(tdm_output *output, const tdm_prop **props,
326                                                                         int *count)
327 {
328         OUTPUT_FUNC_ENTRY();
329
330         TDM_RETURN_VAL_IF_FAIL(props != NULL, TDM_ERROR_INVALID_PARAMETER);
331         TDM_RETURN_VAL_IF_FAIL(count != NULL, TDM_ERROR_INVALID_PARAMETER);
332
333         _pthread_mutex_lock(&private_display->lock);
334
335         *props = (const tdm_prop *)private_output->caps.props;
336         *count = private_output->caps.prop_count;
337
338         _pthread_mutex_unlock(&private_display->lock);
339
340         return ret;
341 }
342
343 EXTERN tdm_error
344 tdm_output_get_available_modes(tdm_output *output,
345                                                            const tdm_output_mode **modes, int *count)
346 {
347         OUTPUT_FUNC_ENTRY();
348
349         TDM_RETURN_VAL_IF_FAIL(modes != NULL, TDM_ERROR_INVALID_PARAMETER);
350         TDM_RETURN_VAL_IF_FAIL(count != NULL, TDM_ERROR_INVALID_PARAMETER);
351
352         _pthread_mutex_lock(&private_display->lock);
353
354         *modes = (const tdm_output_mode *)private_output->caps.modes;
355         *count = private_output->caps.mode_count;
356
357         _pthread_mutex_unlock(&private_display->lock);
358
359         return ret;
360 }
361
362 EXTERN tdm_error
363 tdm_output_get_available_size(tdm_output *output, int *min_w, int *min_h,
364                                                           int *max_w, int *max_h, int *preferred_align)
365 {
366         OUTPUT_FUNC_ENTRY();
367
368         _pthread_mutex_lock(&private_display->lock);
369
370         if (min_w)
371                 *min_w = TDM_FRONT_VALUE(private_output->caps.min_w);
372         if (min_h)
373                 *min_h = TDM_FRONT_VALUE(private_output->caps.min_h);
374         if (max_w)
375                 *max_w = TDM_FRONT_VALUE(private_output->caps.max_w);
376         if (max_h)
377                 *max_h = TDM_FRONT_VALUE(private_output->caps.max_h);
378         if (preferred_align)
379                 *preferred_align = TDM_FRONT_VALUE(private_output->caps.preferred_align);
380
381         _pthread_mutex_unlock(&private_display->lock);
382
383         return ret;
384 }
385
386 EXTERN tdm_error
387 tdm_output_get_cursor_available_size(tdm_output *output, int *min_w, int *min_h,
388                                                                          int *max_w, int *max_h, int *preferred_align)
389 {
390         OUTPUT_FUNC_ENTRY();
391
392         _pthread_mutex_lock(&private_display->lock);
393
394         if (!tdm_display_check_module_abi(private_display, 1, 5)) {
395
396                 if (min_w)
397                         *min_w = -1;
398                 if (min_h)
399                         *min_h = -1;
400                 if (max_w)
401                         *max_w = -1;
402                 if (max_h)
403                         *max_h = -1;
404                 if (preferred_align)
405                         *preferred_align = -1;
406
407                 _pthread_mutex_unlock(&private_display->lock);
408
409                 return TDM_ERROR_BAD_MODULE;
410         }
411
412         if (min_w)
413                 *min_w = TDM_FRONT_VALUE(private_output->caps.cursor_min_w);
414         if (min_h)
415                 *min_h = TDM_FRONT_VALUE(private_output->caps.cursor_min_h);
416         if (max_w)
417                 *max_w = TDM_FRONT_VALUE(private_output->caps.cursor_max_w);
418         if (max_h)
419                 *max_h = TDM_FRONT_VALUE(private_output->caps.cursor_max_h);
420         if (preferred_align)
421                 *preferred_align = TDM_FRONT_VALUE(private_output->caps.cursor_preferred_align);
422
423         _pthread_mutex_unlock(&private_display->lock);
424
425         return ret;
426 }
427
428 EXTERN tdm_error
429 tdm_output_get_physical_size(tdm_output *output, unsigned int *mmWidth,
430                                                          unsigned int *mmHeight)
431 {
432         OUTPUT_FUNC_ENTRY();
433
434         _pthread_mutex_lock(&private_display->lock);
435
436         if (mmWidth)
437                 *mmWidth = private_output->caps.mmWidth;
438         if (mmHeight)
439                 *mmHeight = private_output->caps.mmHeight;
440
441         _pthread_mutex_unlock(&private_display->lock);
442
443         return ret;
444 }
445
446 EXTERN tdm_error
447 tdm_output_get_subpixel(tdm_output *output, unsigned int *subpixel)
448 {
449         OUTPUT_FUNC_ENTRY();
450         TDM_RETURN_VAL_IF_FAIL(subpixel != NULL, TDM_ERROR_INVALID_PARAMETER);
451
452         _pthread_mutex_lock(&private_display->lock);
453
454         *subpixel = private_output->caps.subpixel;
455
456         _pthread_mutex_unlock(&private_display->lock);
457
458         return ret;
459 }
460
461 EXTERN tdm_error
462 tdm_output_get_pipe(tdm_output *output, unsigned int *pipe)
463 {
464         OUTPUT_FUNC_ENTRY();
465         TDM_RETURN_VAL_IF_FAIL(pipe != NULL, TDM_ERROR_INVALID_PARAMETER);
466
467         _pthread_mutex_lock(&private_display->lock);
468
469         *pipe = private_output->pipe;
470
471         _pthread_mutex_unlock(&private_display->lock);
472
473         return ret;
474 }
475
476 EXTERN tdm_error
477 tdm_output_get_primary_index(tdm_output *output, int *index)
478 {
479         tdm_private_layer *private_layer = NULL;
480
481         OUTPUT_FUNC_ENTRY();
482         TDM_RETURN_VAL_IF_FAIL(index != NULL, TDM_ERROR_INVALID_PARAMETER);
483
484         _pthread_mutex_lock(&private_display->lock);
485
486         LIST_FOR_EACH_ENTRY(private_layer, &private_output->layer_list, link) {
487                 if (private_layer->caps.capabilities & TDM_LAYER_CAPABILITY_PRIMARY) {
488                         *index = private_layer->index;
489                         break;
490                 }
491         }
492
493         _pthread_mutex_unlock(&private_display->lock);
494
495         return ret;
496 }
497
498 EXTERN tdm_error
499 tdm_output_set_property(tdm_output *output, unsigned int id, tdm_value value)
500 {
501         tdm_func_output *func_output;
502         OUTPUT_FUNC_ENTRY();
503
504         _pthread_mutex_lock(&private_display->lock);
505
506         func_output = &private_display->func_output;
507
508         if (!func_output->output_set_property) {
509                 _pthread_mutex_unlock(&private_display->lock);
510                 TDM_ERR("not implemented!!");
511                 return TDM_ERROR_NOT_IMPLEMENTED;
512         }
513
514         ret = func_output->output_set_property(private_output->output_backend, id,
515                                                                                    value);
516
517         _pthread_mutex_unlock(&private_display->lock);
518
519         return ret;
520 }
521
522 EXTERN tdm_error
523 tdm_output_get_property(tdm_output *output, unsigned int id, tdm_value *value)
524 {
525         tdm_func_output *func_output;
526         OUTPUT_FUNC_ENTRY();
527
528         TDM_RETURN_VAL_IF_FAIL(value != NULL, TDM_ERROR_INVALID_PARAMETER);
529
530         _pthread_mutex_lock(&private_display->lock);
531
532         func_output = &private_display->func_output;
533
534         if (!func_output->output_get_property) {
535                 _pthread_mutex_unlock(&private_display->lock);
536                 TDM_ERR("not implemented!!");
537                 return TDM_ERROR_NOT_IMPLEMENTED;
538         }
539
540         ret = func_output->output_get_property(private_output->output_backend, id,
541                                                                                    value);
542
543         _pthread_mutex_unlock(&private_display->lock);
544
545         return ret;
546 }
547
548 INTERN void
549 tdm_output_cb_vblank(tdm_output *output_backend, unsigned int sequence,
550                                          unsigned int tv_sec, unsigned int tv_usec, void *user_data)
551 {
552         tdm_private_vblank_handler *vblank_handler = user_data;
553         tdm_private_vblank_handler *v = NULL, *vv = NULL;
554         tdm_private_output *private_output;
555         tdm_private_display *private_display;
556         struct list_head clone_list;
557         int interval, sync;
558         pid_t tid = syscall(SYS_gettid);
559
560         TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
561         TDM_RETURN_IF_FAIL(vblank_handler);
562
563         private_output = vblank_handler->private_output;
564         private_display = private_output->private_display;
565
566         if (vblank_handler->owner_tid != tid) {
567                 tdm_thread_cb_output_vblank output_vblank;
568                 tdm_error ret;
569
570                 output_vblank.base.type = TDM_THREAD_CB_OUTPUT_VBLANK;
571                 output_vblank.base.length = sizeof output_vblank;
572                 output_vblank.output_stamp = vblank_handler->private_output->stamp;
573                 output_vblank.sequence = sequence;
574                 output_vblank.tv_sec = tv_sec;
575                 output_vblank.tv_usec = tv_usec;
576                 output_vblank.user_data = user_data;
577
578                 ret = tdm_thread_send_cb(private_display->private_loop, &output_vblank.base);
579                 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
580
581                 return;
582         }
583
584         if (vblank_handler->owner_tid != tid)
585                 TDM_NEVER_GET_HERE();
586
587         interval = vblank_handler->interval;
588         sync = vblank_handler->sync;
589
590         LIST_INITHEAD(&clone_list);
591
592         LIST_FOR_EACH_ENTRY_SAFE(v, vv, &private_output->vblank_handler_list, link) {
593                 if (v->interval != interval || v->sync != sync || v->owner_tid != tid)
594                         continue;
595
596                 LIST_DEL(&v->link);
597                 LIST_ADDTAIL(&v->link, &clone_list);
598         }
599
600         if (tdm_debug_module & TDM_DEBUG_COMMIT)
601                 TDM_INFO("----------------------------------------- output(%d) got vblank", private_output->pipe);
602
603         _pthread_mutex_unlock(&private_display->lock);
604         LIST_FOR_EACH_ENTRY_SAFE(v, vv, &clone_list, link) {
605                 if (tdm_debug_module & TDM_DEBUG_COMMIT)
606                         TDM_INFO("handler(%p)", v);
607
608                 if (v->func)
609                         v->func(v->private_output, sequence, tv_sec, tv_usec, v->user_data);
610                 LIST_DEL(&v->link);
611                 free(v);
612         }
613         _pthread_mutex_lock(&private_display->lock);
614
615         if (tdm_debug_module & TDM_DEBUG_COMMIT)
616                 TDM_INFO("-----------------------------------------...");
617 }
618
619 INTERN void
620 tdm_output_cb_commit(tdm_output *output_backend, unsigned int sequence,
621                                          unsigned int tv_sec, unsigned int tv_usec, void *user_data)
622 {
623         tdm_private_output_commit_handler *output_commit_handler = user_data;
624         tdm_private_display *private_display;
625         tdm_private_output *private_output;
626         tdm_private_layer *private_layer = NULL;
627
628         TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
629
630         if (!output_commit_handler)
631                 return;
632
633         private_output = output_commit_handler->private_output;
634         private_display = private_output->private_display;
635
636         if (output_commit_handler->owner_tid != syscall(SYS_gettid)) {
637                 tdm_thread_cb_output_commit output_commit;
638                 tdm_error ret;
639
640                 output_commit.base.type = TDM_THREAD_CB_OUTPUT_COMMIT;
641                 output_commit.base.length = sizeof output_commit;
642                 output_commit.output_stamp = private_output->stamp;
643                 output_commit.sequence = sequence;
644                 output_commit.tv_sec = tv_sec;
645                 output_commit.tv_usec = tv_usec;
646                 output_commit.user_data = user_data;
647
648                 ret = tdm_thread_send_cb(private_display->private_loop, &output_commit.base);
649                 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
650
651                 return;
652         }
653
654         if (tdm_debug_module & TDM_DEBUG_COMMIT) {
655                 TDM_INFO("----------------------------------------- output(%d) committed", private_output->pipe);
656                 TDM_INFO("handler(%p)", output_commit_handler);
657         }
658
659         if (private_display->commit_type == TDM_COMMIT_TYPE_OUTPUT) {
660                 /* In case of layer commit, the below will be handled in the layer commit callback */
661                 LIST_FOR_EACH_ENTRY(private_layer, &private_output->layer_list, link) {
662                         if (private_layer->committed_buffer)
663                                 tdm_layer_committed(private_layer, &private_layer->committed_buffer);
664                 }
665         }
666
667         if (output_commit_handler->func) {
668                 _pthread_mutex_unlock(&private_display->lock);
669                 output_commit_handler->func(private_output, sequence,
670                                                                         tv_sec, tv_usec, output_commit_handler->user_data);
671                 _pthread_mutex_lock(&private_display->lock);
672         }
673
674         LIST_DEL(&output_commit_handler->link);
675         free(output_commit_handler);
676
677         if (tdm_debug_module & TDM_DEBUG_COMMIT)
678                 TDM_INFO("-----------------------------------------...");
679 }
680
681 /* add_front: To distinguish between the user vblank handlers and the layer
682  *            commit vblank handlers. The layer commit handlers will be called
683  *            before calling the user vblank handlers.
684  */
685 static tdm_error
686 _tdm_output_wait_vblank(tdm_output *output, int interval, int sync,
687                                                 tdm_output_vblank_handler func, void *user_data,
688                                                 unsigned int add_front)
689 {
690         tdm_func_output *func_output;
691         tdm_private_vblank_handler *vblank_handler = NULL, *v = NULL;
692         unsigned int skip_request = 0;
693         pid_t tid = syscall(SYS_gettid);
694
695         OUTPUT_FUNC_ENTRY();
696
697         func_output = &private_display->func_output;
698
699         /* interval SHOULD be at least 1 */
700         if (interval <= 0)
701                 interval = 1;
702
703         if (!func_output->output_wait_vblank) {
704                 TDM_ERR("not implemented!!");
705                 return TDM_ERROR_NOT_IMPLEMENTED;
706         }
707
708         if (!private_output->regist_vblank_cb) {
709                 private_output->regist_vblank_cb = 1;
710                 ret = func_output->output_set_vblank_handler(private_output->output_backend,
711                                 tdm_output_cb_vblank);
712         }
713
714         vblank_handler = calloc(1, sizeof(tdm_private_vblank_handler));
715         if (!vblank_handler) {
716                 TDM_ERR("failed: alloc memory");
717                 return TDM_ERROR_OUT_OF_MEMORY;
718         }
719
720         if (tdm_debug_module & TDM_DEBUG_COMMIT)
721                 TDM_INFO("output(%d) wait_vblank: handler(%p)", private_output->pipe, vblank_handler);
722
723         LIST_FOR_EACH_ENTRY(v, &private_output->vblank_handler_list, link) {
724                 if (v->interval == interval && v->sync == sync && v->owner_tid == tid) {
725                         skip_request = 1;
726                         break;
727                 }
728         }
729
730         if (add_front)
731                 LIST_ADD(&vblank_handler->link, &private_output->vblank_handler_list);
732         else
733                 LIST_ADDTAIL(&vblank_handler->link, &private_output->vblank_handler_list);
734
735         vblank_handler->private_output = private_output;
736         vblank_handler->interval = interval;
737         vblank_handler->sync = sync;
738         vblank_handler->func = func;
739         vblank_handler->user_data = user_data;
740         vblank_handler->owner_tid = tid;
741
742         /* If there is the previous request, we can skip to call output_wait_vblank() */
743         if (!skip_request) {
744                 ret = func_output->output_wait_vblank(private_output->output_backend, interval,
745                                                                                           sync, vblank_handler);
746                 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
747
748                 if (tdm_debug_module & TDM_DEBUG_COMMIT)
749                         TDM_INFO("output(%d) backend wait_vblank", private_output->pipe);
750         }
751
752         return ret;
753
754 wait_failed:
755         if (vblank_handler) {
756                 LIST_DEL(&vblank_handler->link);
757                 free(vblank_handler);
758         }
759         return ret;
760 }
761
762 EXTERN tdm_error
763 tdm_output_wait_vblank(tdm_output *output, int interval, int sync,
764                                            tdm_output_vblank_handler func, void *user_data)
765 {
766         OUTPUT_FUNC_ENTRY();
767
768         _pthread_mutex_lock(&private_display->lock);
769
770         if (private_output->current_dpms_value > TDM_OUTPUT_DPMS_ON) {
771                 TDM_WRN("output(%d) dpms: %s", private_output->pipe,
772                                 tdm_dpms_str(private_output->current_dpms_value));
773                 _pthread_mutex_unlock(&private_display->lock);
774                 return TDM_ERROR_DPMS_OFF;
775         }
776
777         ret = _tdm_output_wait_vblank(output, interval, sync, func, user_data, 0);
778
779         _pthread_mutex_unlock(&private_display->lock);
780
781         return ret;
782 }
783
784 EXTERN tdm_error
785 tdm_output_wait_vblank_add_front(tdm_output *output, int interval, int sync,
786                                                                  tdm_output_vblank_handler func, void *user_data)
787 {
788         OUTPUT_FUNC_ENTRY();
789
790         _pthread_mutex_lock(&private_display->lock);
791
792         if (private_output->current_dpms_value > TDM_OUTPUT_DPMS_ON) {
793                 TDM_WRN("output(%d) dpms: %s", private_output->pipe,
794                                 tdm_dpms_str(private_output->current_dpms_value));
795                 _pthread_mutex_unlock(&private_display->lock);
796                 return TDM_ERROR_DPMS_OFF;
797         }
798
799         ret = _tdm_output_wait_vblank(output, interval, sync, func, user_data, 1);
800
801         _pthread_mutex_unlock(&private_display->lock);
802
803         return ret;
804 }
805
806 INTERN tdm_error
807 tdm_output_commit_internal(tdm_output *output, int sync, tdm_output_commit_handler func, void *user_data)
808 {
809         tdm_func_output *func_output;
810         tdm_private_output_commit_handler *output_commit_handler = NULL;
811         tdm_private_layer *private_layer = NULL;
812         tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_ON;
813
814         OUTPUT_FUNC_ENTRY();
815
816         func_output = &private_display->func_output;
817
818         if (!func_output->output_commit) {
819                 TDM_ERR("not implemented!!");
820                 return TDM_ERROR_NOT_IMPLEMENTED;
821         }
822
823         tdm_output_get_dpms_internal(output, &dpms_value);
824
825         if (dpms_value == TDM_OUTPUT_DPMS_ON) {
826                 if (func) {
827                         if (!private_output->regist_commit_cb) {
828                                 private_output->regist_commit_cb = 1;
829                                 ret = func_output->output_set_commit_handler(private_output->output_backend,
830                                                         tdm_output_cb_commit);
831                                 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, commit_failed);
832                         }
833
834                         output_commit_handler = calloc(1, sizeof(tdm_private_output_commit_handler));
835                         if (!output_commit_handler) {
836                                 TDM_ERR("failed: alloc memory");
837                                 return TDM_ERROR_OUT_OF_MEMORY;
838                         }
839
840                         LIST_ADDTAIL(&output_commit_handler->link, &private_output->output_commit_handler_list);
841                         output_commit_handler->private_output = private_output;
842                         output_commit_handler->func = func;
843                         output_commit_handler->user_data = user_data;
844                         output_commit_handler->owner_tid = syscall(SYS_gettid);
845                 }
846
847                 ret = func_output->output_commit(private_output->output_backend, sync,
848                                 output_commit_handler);
849                 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, commit_failed);
850
851                 if (tdm_debug_module & TDM_DEBUG_COMMIT)
852                         TDM_INFO("output(%d) backend commit: handle(%p) func(%p) user_data(%p)",
853                                         private_output->pipe, output_commit_handler, func, user_data);
854         }
855
856         /* Even if DPMS is off, committed_buffer should be changed because it will be referred
857          * for tdm_layer_committed() function.
858          */
859         LIST_FOR_EACH_ENTRY(private_layer, &private_output->layer_list, link) {
860                 if (!private_layer->waiting_buffer)
861                         continue;
862
863                 private_layer->committed_buffer = private_layer->waiting_buffer;
864                 private_layer->waiting_buffer = NULL;
865                 if (tdm_debug_module & TDM_DEBUG_BUFFER)
866                         TDM_INFO("layer(%p) waiting_buffer(%p) committed_buffer(%p)",
867                                          private_layer, private_layer->waiting_buffer,
868                                          private_layer->committed_buffer->buffer);
869         }
870
871         if (dpms_value != TDM_OUTPUT_DPMS_ON) {
872                 TDM_WRN("TDM_OUTPUT_DPMS_OFF. Directly call commit handler instead of commit.");
873                 if (func)
874                         func(output, 0, 0, 0, user_data);
875         }
876
877         return ret;
878
879 commit_failed:
880         if (output_commit_handler) {
881                 LIST_DEL(&output_commit_handler->link);
882                 free(output_commit_handler);
883         }
884         return ret;
885 }
886
887 EXTERN tdm_error
888 tdm_output_commit(tdm_output *output, int sync, tdm_output_commit_handler func,
889                                   void *user_data)
890 {
891         OUTPUT_FUNC_ENTRY();
892
893         _pthread_mutex_lock(&private_display->lock);
894
895         if (private_display->commit_type == TDM_COMMIT_TYPE_NONE)
896                 private_display->commit_type = TDM_COMMIT_TYPE_OUTPUT;
897         else if (private_display->commit_type == TDM_COMMIT_TYPE_LAYER) {
898                 TDM_ERR("Can't supported. Use tdm_layer_commit");
899                 _pthread_mutex_unlock(&private_display->lock);
900                 return TDM_ERROR_BAD_REQUEST;
901         }
902
903         if (private_display->commit_per_vblank) {
904                 TDM_ERR("Use tdm_layer_commit");
905                 _pthread_mutex_unlock(&private_display->lock);
906                 return TDM_ERROR_BAD_REQUEST;
907         }
908
909         if (private_output->current_dpms_value > TDM_OUTPUT_DPMS_ON) {
910                 TDM_ERR("output(%d) dpms: %s", private_output->pipe,
911                                 tdm_dpms_str(private_output->current_dpms_value));
912                 _pthread_mutex_unlock(&private_display->lock);
913                 return TDM_ERROR_DPMS_OFF;
914         }
915
916         if (tdm_debug_module & TDM_DEBUG_COMMIT)
917                 TDM_INFO("output(%d) commit", private_output->pipe);
918
919         ret = tdm_output_commit_internal(output, sync, func, user_data);
920
921         _pthread_mutex_unlock(&private_display->lock);
922
923         return ret;
924 }
925
926 EXTERN tdm_error
927 tdm_output_set_mode(tdm_output *output, const tdm_output_mode *mode)
928 {
929         tdm_func_output *func_output;
930         OUTPUT_FUNC_ENTRY();
931
932         TDM_RETURN_VAL_IF_FAIL(mode != NULL, TDM_ERROR_INVALID_PARAMETER);
933
934         _pthread_mutex_lock(&private_display->lock);
935
936         func_output = &private_display->func_output;
937
938         if (!func_output->output_set_mode) {
939                 _pthread_mutex_unlock(&private_display->lock);
940                 TDM_ERR("not implemented!!");
941                 return TDM_ERROR_NOT_IMPLEMENTED;
942         }
943
944         ret = func_output->output_set_mode(private_output->output_backend, mode);
945         if (ret == TDM_ERROR_NONE)
946                 private_output->current_mode = mode;
947         _pthread_mutex_unlock(&private_display->lock);
948
949         return ret;
950 }
951
952 EXTERN tdm_error
953 tdm_output_get_mode(tdm_output *output, const tdm_output_mode **mode)
954 {
955         OUTPUT_FUNC_ENTRY();
956
957         TDM_RETURN_VAL_IF_FAIL(mode != NULL, TDM_ERROR_INVALID_PARAMETER);
958
959         _pthread_mutex_lock(&private_display->lock);
960
961         *mode = private_output->current_mode;
962
963         _pthread_mutex_unlock(&private_display->lock);
964
965         return ret;
966 }
967
968 static tdm_error
969 _tdm_output_dpms_changed_timeout(void *user_data)
970 {
971         tdm_private_output *private_output = user_data;
972         tdm_value value;
973
974         value.u32 = private_output->current_dpms_value;
975         tdm_output_call_change_handler_internal(private_output,
976                                                                                         &private_output->change_handler_list_sub,
977                                                                                         TDM_OUTPUT_CHANGE_DPMS,
978                                                                                         value, 0);
979
980         return TDM_ERROR_NONE;
981 }
982
983 INTERN void
984 tdm_output_cb_dpms(tdm_output *output_backend, tdm_output_dpms dpms, void *user_data)
985 {
986         tdm_private_display *private_display;
987         tdm_private_output *private_output = user_data;
988         tdm_value value;
989
990         TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
991         TDM_RETURN_IF_FAIL(private_output);
992
993         private_display = private_output->private_display;
994
995         if (!tdm_thread_in_display_thread(syscall(SYS_gettid))) {
996                 tdm_thread_cb_output_dpms output_dpms;
997                 tdm_error ret;
998
999                 _tdm_output_update(output_backend, user_data);
1000
1001                 output_dpms.base.type = TDM_THREAD_CB_OUTPUT_DPMS;
1002                 output_dpms.base.length = sizeof output_dpms;
1003                 output_dpms.output_stamp = private_output->stamp;
1004                 output_dpms.dpms = dpms;
1005                 output_dpms.user_data = user_data;
1006
1007                 value.u32 = dpms;
1008                 tdm_output_call_change_handler_internal(private_output,
1009                                                                                                 &private_output->change_handler_list_sub,
1010                                                                                                 TDM_OUTPUT_CHANGE_DPMS,
1011                                                                                                 value, 0);
1012
1013                 ret = tdm_thread_send_cb(private_display->private_loop, &output_dpms.base);
1014                 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
1015
1016                 return;
1017         }
1018
1019         private_output->current_dpms_value = dpms;
1020
1021         TDM_INFO("output(%d) dpms %s", private_output->pipe, tdm_dpms_str(dpms));
1022
1023         value.u32 = dpms;
1024         tdm_output_call_change_handler_internal(private_output,
1025                                                                                         &private_output->change_handler_list_main,
1026                                                                                         TDM_OUTPUT_CHANGE_DPMS,
1027                                                                                         value, 0);
1028 }
1029
1030 EXTERN tdm_error
1031 tdm_output_set_dpms(tdm_output *output, tdm_output_dpms dpms_value)
1032 {
1033         tdm_func_output *func_output;
1034         OUTPUT_FUNC_ENTRY();
1035
1036         if (dpms_value > TDM_OUTPUT_DPMS_OFF)
1037                 dpms_value = TDM_OUTPUT_DPMS_OFF;
1038
1039         _pthread_mutex_lock(&private_display->lock);
1040
1041         if (private_output->current_dpms_value == dpms_value) {
1042                 _pthread_mutex_unlock(&private_display->lock);
1043                 return TDM_ERROR_NONE;
1044         }
1045
1046         /** Use timer to call the output change callback of the sub-thread.
1047          * The output change callback of tdm_server and tdm_vblank was called
1048          * in the main thread. And it made the multi thread issue. If we use
1049          * the timer, we can call the sub-thread's output change callback in
1050          * sub-thread.
1051          */
1052         if (!private_output->dpms_changed_timer) {
1053                 private_output->dpms_changed_timer =
1054                         tdm_event_loop_add_timer_handler(private_output->private_display,
1055                                                                                          _tdm_output_dpms_changed_timeout, private_output, NULL);
1056                 if (!private_output->dpms_changed_timer) {
1057                         TDM_ERR("can't create dpms timer!!");
1058                         _pthread_mutex_unlock(&private_display->lock);
1059                         return TDM_ERROR_OUT_OF_MEMORY;
1060                 }
1061         }
1062
1063         func_output = &private_display->func_output;
1064
1065         if (!func_output->output_set_dpms) {
1066                 _pthread_mutex_unlock(&private_display->lock);
1067                 private_output->current_dpms_value = dpms_value;
1068                 TDM_INFO("output(%d) dpms %s", private_output->pipe, tdm_dpms_str(dpms_value));
1069                 TDM_WRN("not implemented!!");
1070                 return TDM_ERROR_NONE;
1071         }
1072
1073         if (func_output->output_set_dpms_handler) {
1074                 if (!private_output->regist_dpms_cb) {
1075                         private_output->regist_dpms_cb = 1;
1076                         ret = func_output->output_set_dpms_handler(private_output->output_backend,
1077                                         tdm_output_cb_dpms, private_output);
1078                         if (ret != TDM_ERROR_NONE) {
1079                                 _pthread_mutex_unlock(&private_display->lock);
1080                                 TDM_ERR("Can't set the dpms handler!!");
1081                                 return ret;
1082                         }
1083                 }
1084         }
1085
1086         ret = func_output->output_set_dpms(private_output->output_backend, dpms_value);
1087
1088         if (ret == TDM_ERROR_NONE && !func_output->output_set_dpms_handler) {
1089                 tdm_value value;
1090
1091                 private_output->current_dpms_value = dpms_value;
1092                 TDM_INFO("output(%d) dpms %s", private_output->pipe, tdm_dpms_str(dpms_value));
1093
1094                 value.u32 = dpms_value;
1095                 tdm_output_call_change_handler_internal(private_output,
1096                                                                                                 &private_output->change_handler_list_main,
1097                                                                                                 TDM_OUTPUT_CHANGE_DPMS,
1098                                                                                                 value, 0);
1099
1100                 if (!LIST_IS_EMPTY(&private_output->change_handler_list_sub)) {
1101                         ret = tdm_event_loop_source_timer_update(private_output->dpms_changed_timer, 1);
1102                         if (ret != TDM_ERROR_NONE)
1103                                 TDM_NEVER_GET_HERE();
1104                 }
1105         }
1106
1107         _pthread_mutex_unlock(&private_display->lock);
1108
1109         return ret;
1110 }
1111
1112 INTERN tdm_error
1113 tdm_output_get_dpms_internal(tdm_output *output, tdm_output_dpms *dpms_value)
1114 {
1115         tdm_func_output *func_output;
1116         OUTPUT_FUNC_ENTRY();
1117
1118         TDM_RETURN_VAL_IF_FAIL(private_output != NULL, TDM_ERROR_INVALID_PARAMETER);
1119
1120         func_output = &private_display->func_output;
1121
1122         if (!func_output->output_get_dpms) {
1123                 *dpms_value = private_output->current_dpms_value;
1124                 TDM_WRN("not implemented!!");
1125                 return TDM_ERROR_NONE;
1126         }
1127
1128         ret = func_output->output_get_dpms(private_output->output_backend, dpms_value);
1129         if (ret != TDM_ERROR_NONE) {
1130                 TDM_ERR("output_get_dpms failed");
1131                 *dpms_value = TDM_OUTPUT_DPMS_OFF;
1132         }
1133
1134         if (*dpms_value != private_output->current_dpms_value) {
1135                 private_output->current_dpms_value = *dpms_value;
1136                 TDM_ERR("output(%d) dpms changed suddenly: %s", private_output->pipe, tdm_dpms_str(*dpms_value));
1137         }
1138
1139         return ret;
1140 }
1141
1142 EXTERN tdm_error
1143 tdm_output_get_dpms(tdm_output *output, tdm_output_dpms *dpms_value)
1144 {
1145         OUTPUT_FUNC_ENTRY();
1146
1147         TDM_RETURN_VAL_IF_FAIL(dpms_value != NULL, TDM_ERROR_INVALID_PARAMETER);
1148
1149         _pthread_mutex_lock(&private_display->lock);
1150
1151         ret = tdm_output_get_dpms_internal(output, dpms_value);
1152
1153         _pthread_mutex_unlock(&private_display->lock);
1154
1155         return ret;
1156 }
1157
1158 EXTERN tdm_capture *
1159 tdm_output_create_capture(tdm_output *output, tdm_error *error)
1160 {
1161         tdm_capture *capture = NULL;
1162
1163         OUTPUT_FUNC_ENTRY_ERROR();
1164
1165         _pthread_mutex_lock(&private_display->lock);
1166
1167         capture = (tdm_capture *)tdm_capture_create_output_internal(private_output, error);
1168
1169         _pthread_mutex_unlock(&private_display->lock);
1170
1171         return capture;
1172 }
1173
1174 INTERN void
1175 tdm_output_call_change_handler_internal(tdm_private_output *private_output,
1176                                                                                 struct list_head *change_handler_list,
1177                                                                                 tdm_output_change_type type,
1178                                                                                 tdm_value value,
1179                                                                                 int no_check_thread_id)
1180 {
1181         tdm_private_display *private_display;
1182         tdm_private_change_handler *change_handler = NULL;
1183
1184         TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
1185         TDM_RETURN_IF_FAIL(private_output);
1186
1187         private_display = private_output->private_display;
1188         if (!tdm_thread_in_display_thread(syscall(SYS_gettid))) {
1189                 if (type & TDM_OUTPUT_CHANGE_CONNECTION)
1190                         TDM_INFO("output(%d) changed: %s (%d)",
1191                                          private_output->pipe, tdm_status_str(value.u32), value.u32);
1192                 if (type & TDM_OUTPUT_CHANGE_DPMS)
1193                         TDM_INFO("output(%d) changed: dpms %s (%d)",
1194                                          private_output->pipe, tdm_dpms_str(value.u32), value.u32);
1195         }
1196
1197         if (LIST_IS_EMPTY(change_handler_list))
1198                 return;
1199
1200         LIST_FOR_EACH_ENTRY(change_handler, change_handler_list, link) {
1201                 if (!no_check_thread_id && change_handler->owner_tid != syscall(SYS_gettid))
1202                         TDM_NEVER_GET_HERE();
1203
1204                 _pthread_mutex_unlock(&private_display->lock);
1205                 change_handler->func(private_output, type,
1206                                                          value, change_handler->user_data);
1207                 _pthread_mutex_lock(&private_display->lock);
1208         }
1209 }