use vblank instead of the output commit handler in case of tdm_layer_commit
[platform/core/uifw/libtdm.git] / src / tdm_vblank.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_private.h"
42 #include "tdm_list.h"
43
44 /* CAUTION:
45  * tdm vblank doesn't care about thread things.
46  * However, to use tdm_event_loop_xxx functions, have to use the internal function.
47  * So need to lock/unlock the mutex of private_display.
48  */
49
50 /* TDM vblank
51  * - aligned by HW vblank
52  * - use a tdm_event_loop_source object only.
53  */
54
55 /* We expact "(unsigned int)(0.0016667 / 0.0016667) = 1". But it becomes 0. */
56 #define TDM_TIME_MARGIN     0.0000001
57
58 #define TDM_VBLANK_UINT_MAX 4294967295U
59 #define TDM_VBLANK_UINT_1Q  1073741823U     /* UINT_MAX / 4 */
60 #define TDM_VBLANK_UINT_3Q  3221225471U     /* UINT_MAX / 4 * 3 */
61
62 /* (seq < TDM_VBLANK_UINT_1Q && TDM_VBLANK_UINT_3Q < last_seq) means
63  * that the sequence value has reached the max value and started from 0 again.
64  */
65 #define TDM_VBLANK_SEQ_REACHED_MAX(seq, last_seq) \
66         ((seq) < TDM_VBLANK_UINT_1Q && TDM_VBLANK_UINT_3Q < (last_seq))
67
68 #define VER(fmt, arg...)   TDM_ERR("[%p] "fmt, private_vblank, ##arg)
69 #define VWR(fmt, arg...)   TDM_WRN("[%p] "fmt, private_vblank, ##arg)
70 #define VIN(fmt, arg...)   TDM_INFO("[%p] "fmt, private_vblank, ##arg)
71 #define VDB(fmt, arg...)   TDM_DBG("[%p] "fmt, private_vblank, ##arg)
72
73 typedef enum {
74         VBLANK_TYPE_SW,
75         VBLANK_TYPE_SW_FAKE,
76         VBLANK_TYPE_HW,
77         VBLANK_TYPE_HW_SW,
78 } tdm_vblank_wait_type;
79
80 typedef struct _tdm_vblank_wait_info tdm_vblank_wait_info;
81
82 typedef struct _tdm_private_vblank {
83         struct list_head link;
84
85         double stamp;
86         pid_t owner_tid;
87
88         tdm_display *dpy;
89         tdm_output *output;
90         tdm_output_dpms dpms;
91         unsigned int vrefresh;
92
93         unsigned int check_HW_or_SW;
94         unsigned int fps;
95         int offset;
96         unsigned int enable_fake;
97
98         double vblank_gap;
99         unsigned int quotient;
100
101         unsigned int last_seq;
102         double last_time;
103
104         unsigned int add_front;
105
106         /* for HW */
107         double HW_vblank_gap;
108         struct list_head HW_wait_list;
109
110         /* for SW */
111         tdm_event_loop_source *SW_timer;
112         struct list_head SW_wait_list;
113 } tdm_private_vblank;
114
115 struct _tdm_vblank_wait_info {
116         struct list_head link;
117         struct list_head valid_link;
118
119         double stamp;
120
121         double req_time;
122         unsigned int interval;
123
124         tdm_vblank_handler func;
125         void *user_data;
126         tdm_private_vblank *private_vblank;
127
128         tdm_vblank_wait_type type;
129
130         double target_time;
131         unsigned int target_seq;
132 };
133
134 static pthread_mutex_t vblank_list_lock;
135 static struct list_head vblank_list;
136 static struct list_head valid_wait_list;
137 static unsigned int vblank_list_inited;
138 static double stamp = 0;
139
140 static tdm_error _tdm_vblank_cb_vblank_SW(void *user_data);
141 static tdm_error _tdm_vblank_wait_SW(tdm_vblank_wait_info *wait_info);
142
143 #if 0
144 static void
145 _print_list(struct list_head *list)
146 {
147         tdm_vblank_wait_info *w = NULL;
148
149         LIST_FOR_EACH_ENTRY(w, list, link) {
150                 printf(" %d", w->interval);
151         }
152         printf("\n");
153 }
154 #endif
155
156 static inline tdm_private_vblank*
157 _tdm_vblank_find(double vblank_stamp)
158 {
159         tdm_private_vblank *v = NULL;
160
161         if (!vblank_stamp)
162                 return 0;
163
164         pthread_mutex_lock(&vblank_list_lock);
165         LIST_FOR_EACH_ENTRY(v, &vblank_list, link) {
166                 if (v->stamp == vblank_stamp) {
167                         pthread_mutex_unlock(&vblank_list_lock);
168                         return v;
169                 }
170         }
171         pthread_mutex_unlock(&vblank_list_lock);
172
173         return 0;
174 }
175
176 static inline unsigned int
177 _tdm_vblank_check_valid_wait(tdm_vblank_wait_info *wait_info)
178 {
179         tdm_vblank_wait_info *w = NULL;
180
181         if (!wait_info)
182                 return 0;
183
184         LIST_FOR_EACH_ENTRY(w, &valid_wait_list, valid_link) {
185                 if (w->stamp == wait_info->stamp)
186                         return 1;
187         }
188
189         return 0;
190 }
191
192 static inline unsigned int
193 _tdm_vblank_find_wait(tdm_vblank_wait_info *wait_info, struct list_head *list)
194 {
195         tdm_vblank_wait_info *w = NULL;
196
197         if (!wait_info)
198                 return 0;
199
200         LIST_FOR_EACH_ENTRY(w, list, link) {
201                 if (w->stamp == wait_info->stamp)
202                         return 1;
203         }
204
205         return 0;
206 }
207
208 static inline void
209 _tdm_vblank_insert_wait(tdm_vblank_wait_info *wait_info, struct list_head *list)
210 {
211         tdm_vblank_wait_info *w = NULL;
212         tdm_vblank_wait_info *found = NULL;
213
214         if (LIST_IS_EMPTY(list)) {
215                 LIST_ADDTAIL(&wait_info->link, list);
216                 return;
217         }
218
219         LIST_FOR_EACH_ENTRY(w, list, link) {
220                 if (wait_info->type == VBLANK_TYPE_SW) {
221                         if (wait_info->target_time == 0)
222                                 TDM_NEVER_GET_HERE();
223                         if (w->target_time <= wait_info->target_time) {
224                                 found = w;
225                                 continue;
226                         }
227                 } else {
228                         if (w->interval <= wait_info->interval) {
229                                 found = w;
230                                 continue;
231                         }
232                 }
233         }
234
235         if (found)
236                 LIST_ADD(&wait_info->link, &found->link);
237         else
238                 LIST_ADDTAIL(&wait_info->link, list->next);
239 }
240
241 static void
242 _tdm_vblank_change_to_SW(tdm_private_vblank *private_vblank)
243 {
244         tdm_vblank_wait_info *w = NULL, *ww = NULL;
245
246         LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->HW_wait_list, link) {
247                 LIST_DEL(&w->link);
248                 w->type = VBLANK_TYPE_SW_FAKE;
249                 _tdm_vblank_wait_SW(w);
250         }
251 }
252
253 static void
254 _tdm_vblank_free_HW_wait(tdm_private_vblank *private_vblank, tdm_error error, unsigned int call_cb)
255 {
256         tdm_vblank_wait_info *w = NULL, *ww = NULL;
257
258         LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->HW_wait_list, link) {
259                 LIST_DEL(&w->link);
260                 LIST_DEL(&w->valid_link);
261
262                 if (call_cb && w->func)
263                         w->func(private_vblank, error, 0, 0, 0, w->user_data);
264
265                 free(w);
266         }
267 }
268
269 static void
270 _tdm_vblank_cb_output_change(tdm_output *output, tdm_output_change_type type,
271                                                          tdm_value value, void *user_data)
272 {
273         tdm_private_vblank *private_vblank = user_data;
274
275         TDM_RETURN_IF_FAIL(private_vblank != NULL);
276
277         switch (type) {
278         case TDM_OUTPUT_CHANGE_DPMS:
279                 if (private_vblank->dpms == value.u32)
280                         break;
281                 VIN("dpms %s", tdm_dpms_str(value.u32));
282                 private_vblank->dpms = value.u32;
283                 private_vblank->check_HW_or_SW = 1;
284                 if (private_vblank->dpms != TDM_OUTPUT_DPMS_ON) {
285                         if (private_vblank->enable_fake)
286                                 _tdm_vblank_change_to_SW(private_vblank);
287                         else
288                                 _tdm_vblank_free_HW_wait(private_vblank, TDM_ERROR_DPMS_OFF, 1);
289                 }
290                 break;
291         case TDM_OUTPUT_CHANGE_CONNECTION:
292                 VIN("output %s", tdm_status_str(value.u32));
293                 if (value.u32 == TDM_OUTPUT_CONN_STATUS_DISCONNECTED)
294                         _tdm_vblank_free_HW_wait(private_vblank, 0, 0);
295                 break;
296         default:
297                 break;
298         }
299 }
300
301 EXTERN tdm_vblank *
302 tdm_vblank_create(tdm_display *dpy, tdm_output *output, tdm_error *error)
303 {
304         tdm_private_vblank *private_vblank;
305         const tdm_output_mode *mode = NULL;
306         tdm_output_dpms dpms = TDM_OUTPUT_DPMS_ON;
307         tdm_error ret;
308
309         TDM_RETURN_VAL_IF_FAIL_WITH_ERROR(dpy != NULL, TDM_ERROR_INVALID_PARAMETER, NULL);
310         TDM_RETURN_VAL_IF_FAIL_WITH_ERROR(output != NULL, TDM_ERROR_INVALID_PARAMETER, NULL);
311
312         if (error)
313                 *error = TDM_ERROR_NONE;
314
315         if (!vblank_list_inited) {
316                 if (pthread_mutex_init(&vblank_list_lock, NULL)) {
317                         TDM_ERR("mutex init failed: %m");
318                         if (error)
319                                 *error = TDM_ERROR_OPERATION_FAILED;
320                         return NULL;
321                 }
322                 LIST_INITHEAD(&vblank_list);
323                 LIST_INITHEAD(&valid_wait_list);
324                 vblank_list_inited = 1;
325         }
326
327         tdm_output_get_mode(output, &mode);
328         if (!mode) {
329                 if (error)
330                         *error = TDM_ERROR_OPERATION_FAILED;
331                 TDM_ERR("no mode");
332                 return NULL;
333         }
334
335         tdm_output_get_dpms(output, &dpms);
336
337         private_vblank = calloc(1, sizeof * private_vblank);
338         if (!private_vblank) {
339                 if (error)
340                         *error = TDM_ERROR_OUT_OF_MEMORY;
341                 VER("alloc failed");
342                 return NULL;
343         }
344
345         tdm_output_add_change_handler(output, _tdm_vblank_cb_output_change, private_vblank);
346
347         private_vblank->stamp = ++stamp;
348         private_vblank->owner_tid = syscall(SYS_gettid);
349         private_vblank->dpy = dpy;
350         private_vblank->output = output;
351         private_vblank->dpms = dpms;
352         private_vblank->vrefresh = mode->vrefresh;
353         private_vblank->HW_vblank_gap = 1.0 / private_vblank->vrefresh;
354
355         private_vblank->check_HW_or_SW = 1;
356         private_vblank->fps = mode->vrefresh;
357
358         LIST_INITHEAD(&private_vblank->HW_wait_list);
359         LIST_INITHEAD(&private_vblank->SW_wait_list);
360
361         pthread_mutex_lock(&vblank_list_lock);
362         LIST_ADD(&private_vblank->link, &vblank_list);
363         pthread_mutex_unlock(&vblank_list_lock);
364
365         if (tdm_debug_module & TDM_DEBUG_VBLANK)
366                 VIN("created. vrefresh(%d) dpms(%d)",
367                         private_vblank->vrefresh, private_vblank->dpms);
368
369         return (tdm_vblank *)private_vblank;
370 }
371
372 EXTERN void
373 tdm_vblank_destroy(tdm_vblank *vblank)
374 {
375         tdm_private_vblank *private_vblank = vblank;
376         tdm_vblank_wait_info *w = NULL, *ww = NULL;
377
378         if (!private_vblank)
379                 return;
380
381         pthread_mutex_lock(&vblank_list_lock);
382         LIST_DEL(&private_vblank->link);
383         pthread_mutex_unlock(&vblank_list_lock);
384
385         if (private_vblank->SW_timer) {
386                 tdm_display_lock(private_vblank->dpy);
387                 tdm_event_loop_source_remove(private_vblank->SW_timer);
388                 tdm_display_unlock(private_vblank->dpy);
389         }
390
391         tdm_output_remove_change_handler(private_vblank->output,
392                                                                          _tdm_vblank_cb_output_change, private_vblank);
393
394         _tdm_vblank_free_HW_wait(private_vblank, 0, 0);
395
396         LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->SW_wait_list, link) {
397                 LIST_DEL(&w->link);
398                 LIST_DEL(&w->valid_link);
399                 free(w);
400         }
401
402         if (tdm_debug_module & TDM_DEBUG_VBLANK)
403                 VIN("destroyed");
404
405         free(private_vblank);
406 }
407
408 EXTERN tdm_error
409 tdm_vblank_set_fps(tdm_vblank *vblank, unsigned int fps)
410 {
411         tdm_private_vblank *private_vblank = vblank;
412
413         TDM_RETURN_VAL_IF_FAIL(private_vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
414         TDM_RETURN_VAL_IF_FAIL(fps > 0, TDM_ERROR_INVALID_PARAMETER);
415
416         if (private_vblank->fps == fps)
417                 return TDM_ERROR_NONE;
418
419         private_vblank->fps = fps;
420         private_vblank->check_HW_or_SW = 1;
421
422         if (tdm_debug_module & TDM_DEBUG_VBLANK)
423                 VIN("fps(%d)", private_vblank->fps);
424
425         return TDM_ERROR_NONE;
426 }
427
428 EXTERN tdm_error
429 tdm_vblank_set_offset(tdm_vblank *vblank, int offset)
430 {
431         tdm_private_vblank *private_vblank = vblank;
432
433         TDM_RETURN_VAL_IF_FAIL(private_vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
434
435         if (private_vblank->offset == offset)
436                 return TDM_ERROR_NONE;
437
438         private_vblank->offset = offset;
439         private_vblank->check_HW_or_SW = 1;
440
441         if (tdm_debug_module & TDM_DEBUG_VBLANK)
442                 VIN("offset(%d)", private_vblank->offset);
443
444         return TDM_ERROR_NONE;
445 }
446
447 EXTERN tdm_error
448 tdm_vblank_set_enable_fake(tdm_vblank *vblank, unsigned int enable_fake)
449 {
450         tdm_private_vblank *private_vblank = vblank;
451
452         TDM_RETURN_VAL_IF_FAIL(private_vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
453
454         if (private_vblank->enable_fake == enable_fake)
455                 return TDM_ERROR_NONE;
456
457         private_vblank->enable_fake = enable_fake;
458
459         if (tdm_debug_module & TDM_DEBUG_VBLANK)
460                 VIN("enable_fake(%d)", private_vblank->enable_fake);
461
462         return TDM_ERROR_NONE;
463 }
464
465 EXTERN unsigned int
466 tdm_vblank_get_enable_fake(tdm_vblank *vblank)
467 {
468         tdm_private_vblank *private_vblank = vblank;
469
470         TDM_RETURN_VAL_IF_FAIL(private_vblank != NULL, 0);
471
472         return private_vblank->enable_fake;
473 }
474
475 static tdm_error
476 _tdm_vblank_sw_timer_update(tdm_private_vblank *private_vblank)
477 {
478         tdm_vblank_wait_info *first_wait_info = NULL;
479         double curr, target;
480         int ms_delay;
481         tdm_error ret;
482
483         if (LIST_IS_EMPTY(&private_vblank->SW_wait_list)) {
484                 VER("no wait_info");
485                 return TDM_ERROR_OPERATION_FAILED;
486         }
487
488         first_wait_info = container_of(private_vblank->SW_wait_list.next, first_wait_info, link);
489         curr = tdm_helper_get_time();
490         target = first_wait_info->target_time;
491
492         /* ms_delay should be more that 1 */
493         if (target < curr)
494                 ms_delay = 1;
495         else
496                 ms_delay = ceil((target - curr) * 1000.0);
497
498         if (ms_delay < 1)
499                 ms_delay = 1;
500
501         if (ms_delay > 5000)
502                 VER("wait(%p) curr(%.6f) target(%.6f) ms_delay(%d)", first_wait_info, curr, target, ms_delay);
503         else if (tdm_debug_module & TDM_DEBUG_VBLANK)
504                 VIN("wait(%p) curr(%.6f) target(%.6f) ms_delay(%d)", first_wait_info, curr, target, ms_delay);
505
506         tdm_display_lock(private_vblank->dpy);
507
508         if (!private_vblank->SW_timer) {
509                 private_vblank->SW_timer =
510                         tdm_event_loop_add_timer_handler(private_vblank->dpy,
511                                                                                          _tdm_vblank_cb_vblank_SW,
512                                                                                          private_vblank,
513                                                                                          &ret);
514                 if (!private_vblank->SW_timer) {
515                         tdm_display_unlock(private_vblank->dpy);
516                         VER("couldn't add timer");
517                         return ret;
518                 }
519                 VIN("Use SW vblank");
520         }
521
522         ret = tdm_event_loop_source_timer_update(private_vblank->SW_timer, ms_delay);
523         if (ret != TDM_ERROR_NONE) {
524                 tdm_display_unlock(private_vblank->dpy);
525                 VER("couldn't update timer");
526                 return ret;
527         }
528
529         tdm_display_unlock(private_vblank->dpy);
530
531         return TDM_ERROR_NONE;
532 }
533
534 static void
535 _tdm_vblank_cb_vblank_HW(tdm_output *output, unsigned int sequence,
536                                                  unsigned int tv_sec, unsigned int tv_usec,
537                                                  void *user_data)
538 {
539         tdm_vblank_wait_info *wait_info = user_data;
540         tdm_private_vblank *private_vblank;
541
542         if (!_tdm_vblank_check_valid_wait(wait_info)) {
543                 TDM_DBG("can't find wait(%p) from valid_wait_list", wait_info);
544                 return;
545         }
546
547         private_vblank = wait_info->private_vblank;
548         TDM_RETURN_IF_FAIL(private_vblank != NULL);
549
550         if (!_tdm_vblank_find_wait(wait_info, &private_vblank->HW_wait_list)) {
551                 VDB("can't find wait(%p)", wait_info);
552                 return;
553         }
554
555         if (wait_info->type == VBLANK_TYPE_HW_SW) {
556                 tdm_error ret;
557
558                 LIST_DEL(&wait_info->link);
559
560                 /* kernel sends the wrong time of first vblank event. and the time of
561                  * second vblank is correct.
562                  */
563                 wait_info->target_time = TDM_TIME(tv_sec, tv_usec);
564                 wait_info->target_time += (((double)private_vblank->offset) / 1000.0);
565
566                 _tdm_vblank_insert_wait(wait_info, &private_vblank->SW_wait_list);
567
568                 ret = _tdm_vblank_sw_timer_update(private_vblank);
569
570                 /* wait_info will be freed in _tdm_vblank_cb_vblank_SW() */
571                 if (ret == TDM_ERROR_NONE) {
572                         if (tdm_debug_module & TDM_DEBUG_VBLANK)
573                                 VIN("wait(%p) SW timer", wait_info);
574                         return;
575                 }
576
577                 VWR("couldn't update sw timer");
578         }
579
580         /* tv_sec & tv_usec shouldn't be zero. But sometimes they are zero in some hardware.
581          * We need to prohibit that last_time becomes zero because "last_time == 0" means
582          * that a tdm_vblank object is just created and doesn't have the first vblank event.
583          * If last_time becomes 0, it would make issue.
584          */
585         if (tv_sec == 0 && tv_usec == 0) {
586                 double curr = tdm_helper_get_time();
587                 tv_sec = TDM_TIME_SEC(curr);
588                 tv_usec = TDM_TIME_USEC(curr);
589         }
590
591         private_vblank->last_seq = wait_info->target_seq;
592         private_vblank->last_time = TDM_TIME(tv_sec, tv_usec);
593
594         if (tdm_debug_module & TDM_DEBUG_VBLANK)
595                 VIN("wait(%p) last(%.6f) sequence(%u) done", wait_info, private_vblank->last_time, wait_info->target_seq);
596
597         if (wait_info->func)
598                 wait_info->func(private_vblank, TDM_ERROR_NONE, wait_info->target_seq,
599                                                 tv_sec, tv_usec, wait_info->user_data);
600
601         LIST_DEL(&wait_info->link);
602         LIST_DEL(&wait_info->valid_link);
603         free(wait_info);
604 }
605
606 static tdm_error
607 _tdm_vblank_wait_HW(tdm_vblank_wait_info *wait_info)
608 {
609         tdm_private_vblank *private_vblank = wait_info->private_vblank;
610         int hw_interval;
611         tdm_error ret;
612
613         _tdm_vblank_insert_wait(wait_info, &private_vblank->HW_wait_list);
614
615         hw_interval = wait_info->interval * private_vblank->quotient;
616
617         if (private_vblank->last_time == 0) {
618                 /* SW vblank starts from now. SW vblank doesn't need to be aligned with HW vblank. */
619                 private_vblank->last_seq = 0;
620                 private_vblank->last_time = tdm_helper_get_time();
621
622                 /* +1 ms to call the handler ASAP at the first. no matter for SW timer. */
623                 wait_info->target_seq = 1;
624                 if (tdm_debug_module & TDM_DEBUG_VBLANK)
625                         VIN("wait(%p) last(%.6f) hw_itvl(%d) targ_seq(%u)",
626                                 wait_info, private_vblank->last_time, hw_interval, wait_info->target_seq);
627         } else {
628                 double last, prev, req, curr, target;
629                 unsigned int skip;
630
631                 last = private_vblank->last_time;
632                 req = wait_info->req_time;
633
634                 skip = (unsigned int)((req - last) / private_vblank->vblank_gap + TDM_TIME_MARGIN);
635                 prev = last + private_vblank->vblank_gap * skip;
636
637                 curr = tdm_helper_get_time();
638                 target = prev;
639                 while (target < curr)
640                         target += private_vblank->vblank_gap;
641
642                 hw_interval = (unsigned int)((target - curr) / private_vblank->HW_vblank_gap + TDM_TIME_MARGIN) + 1;
643
644                 wait_info->target_seq = private_vblank->last_seq;
645                 wait_info->target_seq += (unsigned int)((target - last) / private_vblank->vblank_gap + TDM_TIME_MARGIN);
646
647                 if (hw_interval > 300)
648                         VER("wait(%p) last(%.6f) req(%.6f) prev(%.6f) curr(%.6f) targ(%.6f,%.6f) skip(%u) hw_itvl(%d) targ_seq(%u)",
649                                 wait_info, last, req - last, prev - last, curr - last, target, target - last,
650                                 skip, hw_interval, wait_info->target_seq);
651                 else if (tdm_debug_module & TDM_DEBUG_VBLANK)
652                         VIN("wait(%p) last(%.6f) req(%.6f) prev(%.6f) curr(%.6f) targ(%.6f,%.6f) skip(%u) hw_itvl(%d) targ_seq(%u)",
653                                 wait_info, last, req - last, prev - last, curr - last, target, target - last,
654                                 skip, hw_interval, wait_info->target_seq);
655         }
656
657         if (private_vblank->add_front)
658                 ret = tdm_output_wait_vblank_add_front(private_vblank->output, hw_interval, 0,
659                                                                                            _tdm_vblank_cb_vblank_HW, wait_info);
660         else
661                 ret = tdm_output_wait_vblank(private_vblank->output, hw_interval, 0,
662                                                                          _tdm_vblank_cb_vblank_HW, wait_info);
663
664         if (ret != TDM_ERROR_NONE) {
665                 LIST_DEL(&wait_info->link);
666                 return ret;
667         }
668
669         if (tdm_debug_module & TDM_DEBUG_VBLANK)
670                 VIN("wait(%p) waiting", wait_info);
671
672         return TDM_ERROR_NONE;
673 }
674
675 static tdm_error
676 _tdm_vblank_cb_vblank_SW(void *user_data)
677 {
678         tdm_private_vblank *private_vblank = user_data;
679
680         TDM_RETURN_VAL_IF_FAIL(private_vblank != NULL, TDM_ERROR_OPERATION_FAILED);
681
682         return tdm_vblank_cb_vblank_SW(private_vblank, 0);
683 }
684
685 INTERN tdm_error
686 tdm_vblank_cb_vblank_SW(tdm_vblank *vblank, double vblank_stamp)
687 {
688         tdm_private_vblank *private_vblank;
689         tdm_vblank_wait_info *first_wait_info = NULL, *w = NULL, *ww = NULL;
690
691         TDM_RETURN_VAL_IF_FAIL(vblank || vblank_stamp > 0, TDM_ERROR_INVALID_PARAMETER);
692
693         if (vblank)
694                 private_vblank = vblank;
695         else {
696                 private_vblank = _tdm_vblank_find(vblank_stamp);
697                 if (!private_vblank) {
698                         TDM_DBG("can't find vblank(%.0f) from valid_list", vblank_stamp);
699                         return TDM_ERROR_NONE;
700                 }
701         }
702
703         if (private_vblank->owner_tid != syscall(SYS_gettid)) {
704                 tdm_thread_cb_vblank_sw vblank_sw;
705                 tdm_private_display *private_display = private_vblank->dpy;
706                 tdm_error ret;
707
708                 vblank_sw.base.type = TDM_THREAD_CB_VBLANK_SW;
709                 vblank_sw.base.length = sizeof vblank_sw;
710                 vblank_sw.vblank_stamp = private_vblank->stamp;
711
712                 ret = tdm_thread_send_cb(private_display->private_loop, &vblank_sw.base);
713                 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
714
715                 return TDM_ERROR_NONE;
716         }
717
718         if (LIST_IS_EMPTY(&private_vblank->SW_wait_list)) {
719                 VER("no wait_info");
720                 return TDM_ERROR_OPERATION_FAILED;
721         }
722
723         first_wait_info = container_of(private_vblank->SW_wait_list.next, first_wait_info, link);
724         TDM_RETURN_VAL_IF_FAIL(first_wait_info != NULL, TDM_ERROR_OPERATION_FAILED);
725
726         if (private_vblank->last_seq >= first_wait_info->target_seq)
727                 TDM_ERR("last_seq(%u) target_seq(%u)", private_vblank->last_seq, first_wait_info->target_seq);
728
729         private_vblank->last_seq = first_wait_info->target_seq;
730         private_vblank->last_time = first_wait_info->target_time;
731
732         if (tdm_debug_module & TDM_DEBUG_VBLANK)
733                 VIN("wait(%p) last(%.6f) sequence(%u) done", first_wait_info, private_vblank->last_time, first_wait_info->target_seq);
734
735         LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->SW_wait_list, link) {
736                 if (w->target_time != first_wait_info->target_time)
737                         break;
738
739                 LIST_DEL(&w->link);
740                 LIST_DEL(&w->valid_link);
741
742                 if (w->func) {
743                         tdm_display_unlock(private_vblank->dpy);
744                         w->func(private_vblank, TDM_ERROR_NONE, w->target_seq,
745                                         TDM_TIME_SEC(w->target_time), TDM_TIME_USEC(w->target_time),
746                                         w->user_data);
747                         tdm_display_lock(private_vblank->dpy);
748                 }
749
750                 free(w);
751         }
752
753         return TDM_ERROR_NONE;
754 }
755
756 static tdm_error
757 _tdm_vblank_wait_SW(tdm_vblank_wait_info *wait_info)
758 {
759         tdm_private_vblank *private_vblank = wait_info->private_vblank;
760         tdm_error ret;
761
762         if (private_vblank->last_time == 0) {
763                 /* SW vblank starts from now. SW vblank doesn't need to be aligned with HW vblank. */
764                 private_vblank->last_seq = 0;
765                 private_vblank->last_time = tdm_helper_get_time();
766
767                 /* +1 ms to call the handler ASAP at the first. no matter for SW timer. */
768                 wait_info->target_seq = 1;
769                 wait_info->target_time = private_vblank->last_time + 0.001;
770
771                 if (tdm_debug_module & TDM_DEBUG_VBLANK)
772                         VIN("wait(%p) last(%.6f) target(%.6f) target sequence(%u)",
773                                 wait_info, private_vblank->last_time, wait_info->target_time, wait_info->target_seq);
774         } else {
775                 double last, prev, req, curr, target;
776                 unsigned int skip;
777
778                 last = private_vblank->last_time;
779                 req = wait_info->req_time;
780
781                 skip = (unsigned int)((req - last) / private_vblank->vblank_gap + TDM_TIME_MARGIN);
782                 prev = last + private_vblank->vblank_gap * skip;
783
784                 curr = tdm_helper_get_time();
785                 target = prev + private_vblank->vblank_gap * wait_info->interval;
786
787                 while (target < curr)
788                         target += private_vblank->vblank_gap;
789
790                 wait_info->target_seq = private_vblank->last_seq;
791                 wait_info->target_seq += (unsigned int)((target - last) / private_vblank->vblank_gap + TDM_TIME_MARGIN);
792
793                 wait_info->target_time = target;
794
795                 if (tdm_debug_module & TDM_DEBUG_VBLANK)
796                         VIN("wait(%p) last(%.6f) req(%.6f) prev(%.6f) curr(%.6f) targ(%.6f,%.6f) skip(%u) targ_seq(%u)",
797                                 wait_info, last, req - last, prev - last, curr - last,
798                                 target, target - last, skip, wait_info->target_seq);
799         }
800
801         _tdm_vblank_insert_wait(wait_info, &private_vblank->SW_wait_list);
802
803         ret = _tdm_vblank_sw_timer_update(private_vblank);
804         if (ret != TDM_ERROR_NONE) {
805                 LIST_DEL(&wait_info->link);
806                 VER("couldn't update sw timer");
807                 return ret;
808         }
809
810         return TDM_ERROR_NONE;
811 }
812
813 EXTERN tdm_error
814 tdm_vblank_wait(tdm_vblank *vblank, unsigned int req_sec, unsigned int req_usec,
815                                 unsigned int interval, tdm_vblank_handler func, void *user_data)
816 {
817         tdm_private_vblank *private_vblank = vblank;
818         tdm_vblank_wait_info *wait_info;
819         tdm_error ret;
820
821         TDM_RETURN_VAL_IF_FAIL(private_vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
822         TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
823
824         if (private_vblank->owner_tid != syscall(SYS_gettid)) {
825                 TDM_ERR("SHOULD be called in the owner thread(%d)", (int)private_vblank->owner_tid);
826                 return TDM_ERROR_BAD_REQUEST;
827         }
828
829         if (private_vblank->dpms != TDM_OUTPUT_DPMS_ON && !private_vblank->enable_fake) {
830                 VIN("can't wait a vblank because of DPMS off");
831                 return TDM_ERROR_DPMS_OFF;
832         }
833
834         wait_info = calloc(1, sizeof * wait_info);
835         if (!wait_info) {
836                 VER("alloc failed");
837                 return TDM_ERROR_OUT_OF_MEMORY;
838         }
839
840         /* set request time to current time if 0. This function seems to be called in server side. */
841         if (req_sec == 0 && req_usec == 0) {
842                 double curr = tdm_helper_get_time();
843                 req_sec = TDM_TIME_SEC(curr);
844                 req_usec = TDM_TIME_USEC(curr);
845         }
846
847         LIST_INITHEAD(&wait_info->link);
848         LIST_ADDTAIL(&wait_info->valid_link, &valid_wait_list);
849         wait_info->stamp = ++stamp;
850         wait_info->req_time = TDM_TIME(req_sec, req_usec);
851         wait_info->interval = interval;
852         wait_info->func = func;
853         wait_info->user_data = user_data;
854         wait_info->private_vblank = private_vblank;
855
856         if (private_vblank->check_HW_or_SW) {
857                 private_vblank->check_HW_or_SW = 0;
858                 private_vblank->vblank_gap = 1.0 / private_vblank->fps;
859                 private_vblank->quotient = private_vblank->vrefresh / private_vblank->fps;
860         }
861
862         /* 1) if fps != factor of vrefresh, SW timer
863          * 2) if fps == factor of vrefresh && dpms == off, SW timer (Fake HW vblank)
864          * 2) if fps == factor of vrefresh && dpms == on && offset == 0, HW vblank
865          * 3) if fps == factor of vrefresh && dpms == on && offset != 0, HW vblank + SW timer
866          * In case of 1), we really don't need to align with HW vblank.
867          */
868         if (private_vblank->vrefresh % private_vblank->fps)
869                 wait_info->type = VBLANK_TYPE_SW;
870         else if (private_vblank->dpms == TDM_OUTPUT_DPMS_OFF)
871                 wait_info->type = VBLANK_TYPE_SW_FAKE;
872         else if (private_vblank->offset == 0)
873                 wait_info->type = VBLANK_TYPE_HW;
874         else
875                 wait_info->type = VBLANK_TYPE_HW_SW;
876
877         if (wait_info->type == VBLANK_TYPE_SW || wait_info->type == VBLANK_TYPE_SW_FAKE)
878                 ret = _tdm_vblank_wait_SW(wait_info);
879         else {
880                 ret = _tdm_vblank_wait_HW(wait_info);
881                 if (ret == TDM_ERROR_DPMS_OFF)
882                         ret = _tdm_vblank_wait_SW(wait_info);
883         }
884
885         if (ret != TDM_ERROR_NONE) {
886                 LIST_DEL(&wait_info->link);
887                 LIST_DEL(&wait_info->valid_link);
888                 free(wait_info);
889                 return ret;
890         }
891
892         return TDM_ERROR_NONE;
893 }
894
895 EXTERN tdm_error
896 tdm_vblank_wait_seq(tdm_vblank *vblank, unsigned int req_sec, unsigned int req_usec,
897                                         unsigned int sequence, tdm_vblank_handler func, void *user_data)
898 {
899         tdm_private_vblank *private_vblank = vblank;
900         unsigned int interval;
901
902         TDM_RETURN_VAL_IF_FAIL(private_vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
903         TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
904
905         /* if the sequence of vblank reaches the max value, sequence can be 0. */
906
907         if (private_vblank->last_seq == 0) {
908                 TDM_WRN("can't calculate interval with sequence(%u)", sequence);
909                 interval = 1;
910         } else if (sequence > private_vblank->last_seq ||
911                            TDM_VBLANK_SEQ_REACHED_MAX(sequence, private_vblank->last_seq)) {
912                 double last, curr, seq_target;
913
914                 last = private_vblank->last_time;
915                 curr = tdm_helper_get_time();
916                 seq_target = last + private_vblank->vblank_gap * (sequence - private_vblank->last_seq);
917
918                 /* If target is old or too close(1ms) to current, we call the handler immediately.
919                  * 2ms? it seems too much.
920                  */
921                 if (seq_target < curr) {
922                         func(vblank, TDM_ERROR_NONE, sequence,
923                                  TDM_TIME_SEC(seq_target), TDM_TIME_USEC(seq_target),
924                                  user_data);
925                         return TDM_ERROR_NONE;
926                 } else if ((seq_target - curr) < 0.001) {  /* 1ms */
927                         func(vblank, TDM_ERROR_NONE, sequence,
928                                  TDM_TIME_SEC(curr), TDM_TIME_USEC(curr),
929                                  user_data);
930                         return TDM_ERROR_NONE;
931                 }
932
933                 interval = (unsigned int)((seq_target - curr) / private_vblank->vblank_gap + TDM_TIME_MARGIN) + 1;
934         } else {
935                 /* this seems error. But we handle this like normal to avoid the unexpected error in cliend side */
936                 TDM_WRN("sequence(%u) should be over the last sequence(%u)", sequence, private_vblank->last_seq);
937                 func(vblank, TDM_ERROR_NONE, private_vblank->last_seq,
938                          TDM_TIME_SEC(private_vblank->last_time), TDM_TIME_USEC(private_vblank->last_time),
939                          user_data);
940                 return TDM_ERROR_NONE;
941         }
942
943         return tdm_vblank_wait(vblank, req_sec, req_usec, interval, func, user_data);
944 }
945
946 INTERN tdm_error
947 tdm_vblank_set_add_front(tdm_vblank *vblank, unsigned int add_front)
948 {
949         tdm_private_vblank *private_vblank = vblank;
950
951         TDM_RETURN_VAL_IF_FAIL(private_vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
952
953         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
954
955         if (private_vblank->add_front == add_front)
956                 return TDM_ERROR_NONE;
957
958         private_vblank->add_front = add_front;
959
960         return TDM_ERROR_NONE;
961 }
962