1 /**************************************************************************
5 * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
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>
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:
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
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.
34 **************************************************************************/
41 #include "tdm_private.h"
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.
51 * - aligned by HW vblank
52 * - use a tdm_event_loop_source object only.
55 /* We expact "(unsigned int)(0.0016667 / 0.0016667) = 1". But it becomes 0. */
56 #define TDM_TIME_MARGIN 0.0000001
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 */
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.
65 #define TDM_VBLANK_SEQ_REACHED_MAX(seq, last_seq) \
66 ((seq) < TDM_VBLANK_UINT_1Q && TDM_VBLANK_UINT_3Q < (last_seq))
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)
78 } tdm_vblank_wait_type;
80 typedef struct _tdm_vblank_wait_info tdm_vblank_wait_info;
82 typedef struct _tdm_private_vblank {
83 struct list_head link;
91 unsigned int vrefresh;
93 unsigned int check_HW_or_SW;
96 unsigned int enable_fake;
99 unsigned int quotient;
101 unsigned int last_seq;
104 unsigned int add_front;
107 double HW_vblank_gap;
108 struct list_head HW_wait_list;
111 tdm_event_loop_source *SW_timer;
112 struct list_head SW_wait_list;
113 } tdm_private_vblank;
115 struct _tdm_vblank_wait_info {
116 struct list_head link;
117 struct list_head valid_link;
122 unsigned int interval;
124 tdm_vblank_handler func;
126 tdm_private_vblank *private_vblank;
128 tdm_vblank_wait_type type;
131 unsigned int target_seq;
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;
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);
145 _print_list(struct list_head *list)
147 tdm_vblank_wait_info *w = NULL;
149 LIST_FOR_EACH_ENTRY(w, list, link) {
150 printf(" %d", w->interval);
156 static inline tdm_private_vblank*
157 _tdm_vblank_find(double vblank_stamp)
159 tdm_private_vblank *v = NULL;
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);
171 pthread_mutex_unlock(&vblank_list_lock);
176 static inline unsigned int
177 _tdm_vblank_check_valid_wait(tdm_vblank_wait_info *wait_info)
179 tdm_vblank_wait_info *w = NULL;
184 LIST_FOR_EACH_ENTRY(w, &valid_wait_list, valid_link) {
185 if (w->stamp == wait_info->stamp)
192 static inline unsigned int
193 _tdm_vblank_find_wait(tdm_vblank_wait_info *wait_info, struct list_head *list)
195 tdm_vblank_wait_info *w = NULL;
200 LIST_FOR_EACH_ENTRY(w, list, link) {
201 if (w->stamp == wait_info->stamp)
209 _tdm_vblank_insert_wait(tdm_vblank_wait_info *wait_info, struct list_head *list)
211 tdm_vblank_wait_info *w = NULL;
212 tdm_vblank_wait_info *found = NULL;
214 if (LIST_IS_EMPTY(list)) {
215 LIST_ADDTAIL(&wait_info->link, list);
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) {
228 if (w->interval <= wait_info->interval) {
236 LIST_ADD(&wait_info->link, &found->link);
238 LIST_ADDTAIL(&wait_info->link, list->next);
242 _tdm_vblank_change_to_SW(tdm_private_vblank *private_vblank)
244 tdm_vblank_wait_info *w = NULL, *ww = NULL;
246 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->HW_wait_list, link) {
248 w->type = VBLANK_TYPE_SW_FAKE;
249 _tdm_vblank_wait_SW(w);
254 _tdm_vblank_free_HW_wait(tdm_private_vblank *private_vblank, tdm_error error, unsigned int call_cb)
256 tdm_vblank_wait_info *w = NULL, *ww = NULL;
258 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->HW_wait_list, link) {
260 LIST_DEL(&w->valid_link);
262 if (call_cb && w->func)
263 w->func(private_vblank, error, 0, 0, 0, w->user_data);
270 _tdm_vblank_cb_output_change(tdm_output *output, tdm_output_change_type type,
271 tdm_value value, void *user_data)
273 tdm_private_vblank *private_vblank = user_data;
275 TDM_RETURN_IF_FAIL(private_vblank != NULL);
278 case TDM_OUTPUT_CHANGE_DPMS:
279 if (private_vblank->dpms == value.u32)
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);
288 _tdm_vblank_free_HW_wait(private_vblank, TDM_ERROR_DPMS_OFF, 1);
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);
302 tdm_vblank_create(tdm_display *dpy, tdm_output *output, tdm_error *error)
304 tdm_private_vblank *private_vblank;
305 const tdm_output_mode *mode = NULL;
306 tdm_output_dpms dpms = TDM_OUTPUT_DPMS_ON;
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);
313 *error = TDM_ERROR_NONE;
315 if (!vblank_list_inited) {
316 if (pthread_mutex_init(&vblank_list_lock, NULL)) {
317 TDM_ERR("mutex init failed: %m");
319 *error = TDM_ERROR_OPERATION_FAILED;
322 LIST_INITHEAD(&vblank_list);
323 LIST_INITHEAD(&valid_wait_list);
324 vblank_list_inited = 1;
327 tdm_output_get_mode(output, &mode);
330 *error = TDM_ERROR_OPERATION_FAILED;
335 tdm_output_get_dpms(output, &dpms);
337 private_vblank = calloc(1, sizeof * private_vblank);
338 if (!private_vblank) {
340 *error = TDM_ERROR_OUT_OF_MEMORY;
345 tdm_output_add_change_handler(output, _tdm_vblank_cb_output_change, private_vblank);
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;
355 private_vblank->check_HW_or_SW = 1;
356 private_vblank->fps = mode->vrefresh;
358 LIST_INITHEAD(&private_vblank->HW_wait_list);
359 LIST_INITHEAD(&private_vblank->SW_wait_list);
361 pthread_mutex_lock(&vblank_list_lock);
362 LIST_ADD(&private_vblank->link, &vblank_list);
363 pthread_mutex_unlock(&vblank_list_lock);
365 if (tdm_debug_module & TDM_DEBUG_VBLANK)
366 VIN("created. vrefresh(%d) dpms(%d)",
367 private_vblank->vrefresh, private_vblank->dpms);
369 return (tdm_vblank *)private_vblank;
373 tdm_vblank_destroy(tdm_vblank *vblank)
375 tdm_private_vblank *private_vblank = vblank;
376 tdm_vblank_wait_info *w = NULL, *ww = NULL;
381 pthread_mutex_lock(&vblank_list_lock);
382 LIST_DEL(&private_vblank->link);
383 pthread_mutex_unlock(&vblank_list_lock);
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);
391 tdm_output_remove_change_handler(private_vblank->output,
392 _tdm_vblank_cb_output_change, private_vblank);
394 _tdm_vblank_free_HW_wait(private_vblank, 0, 0);
396 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->SW_wait_list, link) {
398 LIST_DEL(&w->valid_link);
402 if (tdm_debug_module & TDM_DEBUG_VBLANK)
405 free(private_vblank);
409 tdm_vblank_set_fps(tdm_vblank *vblank, unsigned int fps)
411 tdm_private_vblank *private_vblank = vblank;
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);
416 if (private_vblank->fps == fps)
417 return TDM_ERROR_NONE;
419 private_vblank->fps = fps;
420 private_vblank->check_HW_or_SW = 1;
422 if (tdm_debug_module & TDM_DEBUG_VBLANK)
423 VIN("fps(%d)", private_vblank->fps);
425 return TDM_ERROR_NONE;
429 tdm_vblank_set_offset(tdm_vblank *vblank, int offset)
431 tdm_private_vblank *private_vblank = vblank;
433 TDM_RETURN_VAL_IF_FAIL(private_vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
435 if (private_vblank->offset == offset)
436 return TDM_ERROR_NONE;
438 private_vblank->offset = offset;
439 private_vblank->check_HW_or_SW = 1;
441 if (tdm_debug_module & TDM_DEBUG_VBLANK)
442 VIN("offset(%d)", private_vblank->offset);
444 return TDM_ERROR_NONE;
448 tdm_vblank_set_enable_fake(tdm_vblank *vblank, unsigned int enable_fake)
450 tdm_private_vblank *private_vblank = vblank;
452 TDM_RETURN_VAL_IF_FAIL(private_vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
454 if (private_vblank->enable_fake == enable_fake)
455 return TDM_ERROR_NONE;
457 private_vblank->enable_fake = enable_fake;
459 if (tdm_debug_module & TDM_DEBUG_VBLANK)
460 VIN("enable_fake(%d)", private_vblank->enable_fake);
462 return TDM_ERROR_NONE;
466 tdm_vblank_get_enable_fake(tdm_vblank *vblank)
468 tdm_private_vblank *private_vblank = vblank;
470 TDM_RETURN_VAL_IF_FAIL(private_vblank != NULL, 0);
472 return private_vblank->enable_fake;
476 _tdm_vblank_sw_timer_update(tdm_private_vblank *private_vblank)
478 tdm_vblank_wait_info *first_wait_info = NULL;
483 if (LIST_IS_EMPTY(&private_vblank->SW_wait_list)) {
485 return TDM_ERROR_OPERATION_FAILED;
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;
492 /* ms_delay should be more that 1 */
496 ms_delay = ceil((target - curr) * 1000.0);
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);
506 tdm_display_lock(private_vblank->dpy);
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,
514 if (!private_vblank->SW_timer) {
515 tdm_display_unlock(private_vblank->dpy);
516 VER("couldn't add timer");
519 VIN("Use SW vblank");
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");
529 tdm_display_unlock(private_vblank->dpy);
531 return TDM_ERROR_NONE;
535 _tdm_vblank_cb_vblank_HW(tdm_output *output, unsigned int sequence,
536 unsigned int tv_sec, unsigned int tv_usec,
539 tdm_vblank_wait_info *wait_info = user_data;
540 tdm_private_vblank *private_vblank;
542 if (!_tdm_vblank_check_valid_wait(wait_info)) {
543 TDM_DBG("can't find wait(%p) from valid_wait_list", wait_info);
547 private_vblank = wait_info->private_vblank;
548 TDM_RETURN_IF_FAIL(private_vblank != NULL);
550 if (!_tdm_vblank_find_wait(wait_info, &private_vblank->HW_wait_list)) {
551 VDB("can't find wait(%p)", wait_info);
555 if (wait_info->type == VBLANK_TYPE_HW_SW) {
558 LIST_DEL(&wait_info->link);
560 /* kernel sends the wrong time of first vblank event. and the time of
561 * second vblank is correct.
563 wait_info->target_time = TDM_TIME(tv_sec, tv_usec);
564 wait_info->target_time += (((double)private_vblank->offset) / 1000.0);
566 _tdm_vblank_insert_wait(wait_info, &private_vblank->SW_wait_list);
568 ret = _tdm_vblank_sw_timer_update(private_vblank);
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);
577 VWR("couldn't update sw timer");
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.
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);
591 private_vblank->last_seq = wait_info->target_seq;
592 private_vblank->last_time = TDM_TIME(tv_sec, tv_usec);
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);
598 wait_info->func(private_vblank, TDM_ERROR_NONE, wait_info->target_seq,
599 tv_sec, tv_usec, wait_info->user_data);
601 LIST_DEL(&wait_info->link);
602 LIST_DEL(&wait_info->valid_link);
607 _tdm_vblank_wait_HW(tdm_vblank_wait_info *wait_info)
609 tdm_private_vblank *private_vblank = wait_info->private_vblank;
613 _tdm_vblank_insert_wait(wait_info, &private_vblank->HW_wait_list);
615 hw_interval = wait_info->interval * private_vblank->quotient;
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();
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);
628 double last, prev, req, curr, target;
631 last = private_vblank->last_time;
632 req = wait_info->req_time;
634 skip = (unsigned int)((req - last) / private_vblank->vblank_gap + TDM_TIME_MARGIN);
635 prev = last + private_vblank->vblank_gap * skip;
637 curr = tdm_helper_get_time();
639 while (target < curr)
640 target += private_vblank->vblank_gap;
642 hw_interval = (unsigned int)((target - curr) / private_vblank->HW_vblank_gap + TDM_TIME_MARGIN) + 1;
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);
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);
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);
661 ret = tdm_output_wait_vblank(private_vblank->output, hw_interval, 0,
662 _tdm_vblank_cb_vblank_HW, wait_info);
664 if (ret != TDM_ERROR_NONE) {
665 LIST_DEL(&wait_info->link);
669 if (tdm_debug_module & TDM_DEBUG_VBLANK)
670 VIN("wait(%p) waiting", wait_info);
672 return TDM_ERROR_NONE;
676 _tdm_vblank_cb_vblank_SW(void *user_data)
678 tdm_private_vblank *private_vblank = user_data;
680 TDM_RETURN_VAL_IF_FAIL(private_vblank != NULL, TDM_ERROR_OPERATION_FAILED);
682 return tdm_vblank_cb_vblank_SW(private_vblank, 0);
686 tdm_vblank_cb_vblank_SW(tdm_vblank *vblank, double vblank_stamp)
688 tdm_private_vblank *private_vblank;
689 tdm_vblank_wait_info *first_wait_info = NULL, *w = NULL, *ww = NULL;
691 TDM_RETURN_VAL_IF_FAIL(vblank || vblank_stamp > 0, TDM_ERROR_INVALID_PARAMETER);
694 private_vblank = vblank;
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;
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;
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;
712 ret = tdm_thread_send_cb(private_display->private_loop, &vblank_sw.base);
713 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
715 return TDM_ERROR_NONE;
718 if (LIST_IS_EMPTY(&private_vblank->SW_wait_list)) {
720 return TDM_ERROR_OPERATION_FAILED;
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);
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);
729 private_vblank->last_seq = first_wait_info->target_seq;
730 private_vblank->last_time = first_wait_info->target_time;
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);
735 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->SW_wait_list, link) {
736 if (w->target_time != first_wait_info->target_time)
740 LIST_DEL(&w->valid_link);
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),
747 tdm_display_lock(private_vblank->dpy);
753 return TDM_ERROR_NONE;
757 _tdm_vblank_wait_SW(tdm_vblank_wait_info *wait_info)
759 tdm_private_vblank *private_vblank = wait_info->private_vblank;
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();
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;
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);
775 double last, prev, req, curr, target;
778 last = private_vblank->last_time;
779 req = wait_info->req_time;
781 skip = (unsigned int)((req - last) / private_vblank->vblank_gap + TDM_TIME_MARGIN);
782 prev = last + private_vblank->vblank_gap * skip;
784 curr = tdm_helper_get_time();
785 target = prev + private_vblank->vblank_gap * wait_info->interval;
787 while (target < curr)
788 target += private_vblank->vblank_gap;
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);
793 wait_info->target_time = target;
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);
801 _tdm_vblank_insert_wait(wait_info, &private_vblank->SW_wait_list);
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");
810 return TDM_ERROR_NONE;
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)
817 tdm_private_vblank *private_vblank = vblank;
818 tdm_vblank_wait_info *wait_info;
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);
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;
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;
834 wait_info = calloc(1, sizeof * wait_info);
837 return TDM_ERROR_OUT_OF_MEMORY;
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);
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;
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;
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.
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;
875 wait_info->type = VBLANK_TYPE_HW_SW;
877 if (wait_info->type == VBLANK_TYPE_SW || wait_info->type == VBLANK_TYPE_SW_FAKE)
878 ret = _tdm_vblank_wait_SW(wait_info);
880 ret = _tdm_vblank_wait_HW(wait_info);
881 if (ret == TDM_ERROR_DPMS_OFF)
882 ret = _tdm_vblank_wait_SW(wait_info);
885 if (ret != TDM_ERROR_NONE) {
886 LIST_DEL(&wait_info->link);
887 LIST_DEL(&wait_info->valid_link);
892 return TDM_ERROR_NONE;
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)
899 tdm_private_vblank *private_vblank = vblank;
900 unsigned int interval;
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);
905 /* if the sequence of vblank reaches the max value, sequence can be 0. */
907 if (private_vblank->last_seq == 0) {
908 TDM_WRN("can't calculate interval with sequence(%u)", sequence);
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;
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);
918 /* If target is old or too close(1ms) to current, we call the handler immediately.
919 * 2ms? it seems too much.
921 if (seq_target < curr) {
922 func(vblank, TDM_ERROR_NONE, sequence,
923 TDM_TIME_SEC(seq_target), TDM_TIME_USEC(seq_target),
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),
930 return TDM_ERROR_NONE;
933 interval = (unsigned int)((seq_target - curr) / private_vblank->vblank_gap + TDM_TIME_MARGIN) + 1;
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),
940 return TDM_ERROR_NONE;
943 return tdm_vblank_wait(vblank, req_sec, req_usec, interval, func, user_data);
947 tdm_vblank_set_add_front(tdm_vblank *vblank, unsigned int add_front)
949 tdm_private_vblank *private_vblank = vblank;
951 TDM_RETURN_VAL_IF_FAIL(private_vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
953 TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
955 if (private_vblank->add_front == add_front)
956 return TDM_ERROR_NONE;
958 private_vblank->add_front = add_front;
960 return TDM_ERROR_NONE;