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 <boram1288.park@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 **************************************************************************/
40 #include "tdm_private.h"
43 * Basically tdm vblank doesn't care about thread things.
44 * However, to use tdm_event_loop_xxx functions, have to use the internal function.
45 * So need to lock/unlock the mutex of private_display. And valid_vblank_list and
46 * valid_wait_list should be protected by valid_list_lock because tdm_vblank can
47 * be used in multi-thread.
51 * - aligned by HW vblank
52 * - use a tdm_event_loop_source object only.
55 #define TDM_VBLANK_DEFAULT_VREFRESH 30
57 /* We expact "(unsigned int)(0.0016667 / 0.0016667) = 1". But it becomes 0. */
58 #define TDM_TIME_MARGIN 0.0000001
60 #define TDM_VBLANK_UINT_MAX 4294967295U
61 #define TDM_VBLANK_UINT_1Q 1073741823U /* UINT_MAX / 4 */
62 #define TDM_VBLANK_UINT_3Q 3221225471U /* UINT_MAX / 4 * 3 */
64 /* (seq < TDM_VBLANK_UINT_1Q && TDM_VBLANK_UINT_3Q < last_seq) means
65 * that the sequence value has reached the max value and started from 0 again.
67 #define TDM_VBLANK_SEQ_REACHED_MAX(seq, last_seq) \
68 ((seq) < TDM_VBLANK_UINT_1Q && TDM_VBLANK_UINT_3Q < (last_seq))
70 #define VER(fmt, arg...) TDM_ERR("[%p] "fmt, private_vblank, ##arg)
71 #define VWR(fmt, arg...) TDM_WRN("[%p] "fmt, private_vblank, ##arg)
72 #define VIN(fmt, arg...) do { if (tdm_debug_module & TDM_DEBUG_VBLANK) TDM_INFO("[%p] "fmt, private_vblank, ##arg); } while (0)
73 #define VDB(fmt, arg...) do { if (tdm_debug_module & TDM_DEBUG_VBLANK) TDM_DBG("[%p] "fmt, private_vblank, ##arg); } while (0)
80 } tdm_vblank_wait_type;
83 VBLANK_EVENT_TYPE_NONE,
86 } tdm_vblank_event_type;
88 typedef struct _tdm_vblank_wait_info tdm_vblank_wait_info;
90 typedef struct _tdm_private_vblank {
91 struct list_head valid_link;
98 tdm_output_conn_status connection;
99 unsigned int vrefresh;
101 tdm_vblank_event_type last_type;
102 unsigned int check_HW_or_SW;
103 char name[TDM_NAME_LEN];
105 unsigned int fps_changeable;
106 unsigned int fps_fixed;
108 unsigned int enable_fake;
109 unsigned int ignore_global_fps;
110 unsigned int in_create_handler;
113 unsigned int quotient;
116 unsigned int last_seq;
119 unsigned int add_front;
120 struct wl_resource *resource;
123 double HW_vblank_gap;
124 struct list_head HW_wait_list;
127 tdm_event_loop_source *SW_timer;
128 struct list_head SW_wait_list;
131 tdm_event_loop_source *timeout_timer;
132 } tdm_private_vblank;
134 struct _tdm_vblank_wait_info {
135 struct list_head link;
136 struct list_head valid_link;
141 unsigned int interval;
144 tdm_vblank_handler func;
146 tdm_private_vblank *private_vblank;
148 tdm_vblank_wait_type type;
151 unsigned int target_seq;
154 typedef struct _tdm_vblank_create_handler_info {
155 struct list_head link;
157 tdm_vblank_create_handler func;
159 } tdm_vblank_create_handler_info;
161 /* valid_vblank_list, valid_wait_list and create_handler_list should be protected
162 * by valid_list_lock because tdm_vblank can be used in multi-thread.
164 static pthread_mutex_t valid_list_lock = PTHREAD_MUTEX_INITIALIZER;
165 static struct list_head valid_vblank_list;
166 static struct list_head valid_wait_list;
167 static struct list_head create_handler_list;
168 static unsigned int vblank_list_inited;
169 static unsigned int vblank_global_fps;
170 static double stamp = 0;
172 static void _tdm_vblank_cb_vblank_SW(tdm_private_display *private_display, void *object, tdm_thread_cb_base *cb_base, void *user_data);
173 static tdm_error _tdm_vblank_cb_timeout_SW(void *user_data);
174 static tdm_error _tdm_vblank_wait_SW(tdm_vblank_wait_info *wait_info);
175 static void _tdm_vblank_cb_vblank_HW(tdm_output *output, unsigned int sequence,
176 unsigned int tv_sec, unsigned int tv_usec,
178 static void _tdm_vblank_timeout_timer_update(tdm_private_vblank *private_vblank, int ms_delay);
179 static void _tdm_vblank_get_client_information(tdm_private_vblank *private_vblank,
180 pid_t *pid, const char **name);
184 _print_list(tdm_private_vblank *private_vblank)
186 tdm_vblank_wait_info *w = NULL;
189 LIST_FOR_EACH_ENTRY(w, &private_vblank->HW_wait_list, link) {
190 VIN("HW_wait(%p): type(%d) req_time(%.6f) target_time(%.6f) target_seq(%u)",
191 w, w->type, w->req_time, w->target_time, w->target_seq);
195 LIST_FOR_EACH_ENTRY(w, &private_vblank->SW_wait_list, link) {
196 VIN("SW_wait(%p): type(%d) req_time(%.6f) target_time(%.6f) target_seq(%u)",
197 w, w->type, w->req_time, w->target_time, w->target_seq);
207 tdm_vblank_is_valid(tdm_vblank *vblank)
209 tdm_private_vblank *v = NULL;
211 if (vblank && vblank_list_inited) {
212 pthread_mutex_lock(&valid_list_lock);
213 LIST_FOR_EACH_ENTRY(v, &valid_vblank_list, valid_link) {
214 if (v == (tdm_private_vblank *)vblank) {
215 pthread_mutex_unlock(&valid_list_lock);
219 pthread_mutex_unlock(&valid_list_lock);
226 _tdm_vblank_valid_list_add(struct list_head *valid_link, struct list_head *valid_list)
228 pthread_mutex_lock(&valid_list_lock);
229 LIST_ADDTAIL(valid_link, valid_list);
230 pthread_mutex_unlock(&valid_list_lock);
234 _tdm_vblank_valid_list_del(struct list_head *valid_link)
236 pthread_mutex_lock(&valid_list_lock);
237 LIST_DEL(valid_link);
238 pthread_mutex_unlock(&valid_list_lock);
242 _tdm_vblank_find_object(tdm_private_display *private_display, double vblank_stamp)
244 tdm_private_vblank *v = NULL;
246 pthread_mutex_lock(&valid_list_lock);
247 LIST_FOR_EACH_ENTRY(v, &valid_vblank_list, valid_link) {
248 if (v->stamp == vblank_stamp) {
249 pthread_mutex_unlock(&valid_list_lock);
253 pthread_mutex_unlock(&valid_list_lock);
258 static inline unsigned int
259 _tdm_vblank_check_valid_wait(tdm_vblank_wait_info *wait_info)
261 tdm_vblank_wait_info *w = NULL;
266 pthread_mutex_lock(&valid_list_lock);
267 LIST_FOR_EACH_ENTRY(w, &valid_wait_list, valid_link) {
268 if (w == wait_info) {
269 pthread_mutex_unlock(&valid_list_lock);
273 pthread_mutex_unlock(&valid_list_lock);
278 static inline unsigned int
279 _tdm_vblank_find_wait(tdm_vblank_wait_info *wait_info, struct list_head *list)
281 tdm_vblank_wait_info *w = NULL;
286 LIST_FOR_EACH_ENTRY(w, list, link) {
287 if (w->stamp == wait_info->stamp)
295 _tdm_vblank_insert_wait(tdm_vblank_wait_info *wait_info, struct list_head *list)
297 tdm_vblank_wait_info *w = NULL;
298 tdm_vblank_wait_info *found = NULL;
300 if (LIST_IS_EMPTY(list)) {
301 LIST_ADDTAIL(&wait_info->link, list);
305 LIST_FOR_EACH_ENTRY(w, list, link) {
306 if (wait_info->type == VBLANK_TYPE_SW) {
307 if (wait_info->target_time == 0)
308 TDM_NEVER_GET_HERE();
309 if (w->target_time <= wait_info->target_time) {
314 if (w->interval <= wait_info->interval) {
322 LIST_ADD(&wait_info->link, &found->link);
324 LIST_ADDTAIL(&wait_info->link, list);
328 _tdm_vblank_change_to_SW(tdm_private_vblank *private_vblank)
330 tdm_vblank_wait_info *w = NULL, *ww = NULL;
332 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->HW_wait_list, link) {
335 tdm_display_lock(private_vblank->dpy);
336 tdm_output_remove_vblank_handler_internal(private_vblank->output, _tdm_vblank_cb_vblank_HW, w);
337 tdm_display_unlock(private_vblank->dpy);
339 w->type = VBLANK_TYPE_SW_FAKE;
340 _tdm_vblank_wait_SW(w);
345 _tdm_vblank_free_HW_wait(tdm_private_vblank *private_vblank, tdm_error error, unsigned int call_cb)
347 tdm_vblank_wait_info *w = NULL, *ww = NULL;
349 if (!LIST_IS_EMPTY(&private_vblank->HW_wait_list)) {
351 const char *proc_name;
352 _tdm_vblank_get_client_information(private_vblank, &pid, &proc_name);
353 VWR("TDM HW vblank destroyed forcely!! (pid: %u, name: %s)", pid, proc_name);
356 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->HW_wait_list, link) {
358 _tdm_vblank_valid_list_del(&w->valid_link);
360 tdm_display_lock(private_vblank->dpy);
361 tdm_output_remove_vblank_handler_internal(private_vblank->output, _tdm_vblank_cb_vblank_HW, w);
362 tdm_display_unlock(private_vblank->dpy);
364 VDB("wait(%p) last(%.6f) sequence(%u) done (error:%d, call_cb:%u)",
365 w, (double)0.0, w->target_seq, error, call_cb);
367 if (call_cb && w->func)
368 w->func(private_vblank, error, 0, 0, 0, w->user_data);
375 _tdm_vblank_update_output_info(tdm_private_vblank *private_vblank)
377 tdm_output *output = private_vblank->output;
378 tdm_output_conn_status connection = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
379 unsigned int vrefresh = TDM_VBLANK_DEFAULT_VREFRESH;
380 const tdm_output_mode *mode = NULL;
383 ret = tdm_output_get_conn_status(output, &connection);
384 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
386 ret = tdm_output_get_mode(output, &mode);
387 if (ret == TDM_ERROR_NONE && mode)
388 vrefresh = mode->vrefresh;
390 VWR("mode not setted!!!");
392 private_vblank->connection = connection;
393 private_vblank->vrefresh = vrefresh;
394 private_vblank->HW_vblank_gap = 1.0 / private_vblank->vrefresh;
396 if (private_vblank->fps_changeable)
397 private_vblank->fps = vrefresh;
399 VIN("connection(%d) vrefresh(%d) fps(%d) fps_changeable(%d)",
400 private_vblank->connection,
401 private_vblank->vrefresh, private_vblank->fps, private_vblank->fps_changeable);
404 /* LCOV_EXCL_START */
406 _tdm_vblank_cb_output_change(tdm_output *output, tdm_output_change_type type,
407 tdm_value value, void *user_data)
409 tdm_private_vblank *private_vblank = user_data;
411 TDM_RETURN_IF_FAIL(tdm_vblank_is_valid(private_vblank));
414 case TDM_OUTPUT_CHANGE_DPMS:
415 VIN("dpms %s", tdm_dpms_str(value.u32));
416 private_vblank->check_HW_or_SW = 1;
417 if (TDM_OUTPUT_DPMS_VSYNC_IS_OFF(value.u32)) {
418 if (private_vblank->enable_fake)
419 _tdm_vblank_change_to_SW(private_vblank);
421 tdm_display_lock(private_vblank->dpy);
422 _tdm_vblank_timeout_timer_update(private_vblank, 0);
423 tdm_display_unlock(private_vblank->dpy);
425 _tdm_vblank_free_HW_wait(private_vblank, TDM_ERROR_DPMS_OFF, 1);
429 case TDM_OUTPUT_CHANGE_CONNECTION:
430 if (private_vblank->connection == value.u32)
432 VIN("output %s", tdm_status_str(value.u32));
433 _tdm_vblank_update_output_info(private_vblank);
434 private_vblank->check_HW_or_SW = 1;
435 if (private_vblank->connection == TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
436 if (private_vblank->enable_fake)
437 _tdm_vblank_change_to_SW(private_vblank);
439 tdm_display_lock(private_vblank->dpy);
440 _tdm_vblank_timeout_timer_update(private_vblank, 0);
441 tdm_display_unlock(private_vblank->dpy);
443 _tdm_vblank_free_HW_wait(private_vblank, TDM_ERROR_OUTPUT_DISCONNECTED, 1);
454 _tdm_vblank_set_fps(tdm_vblank *vblank, unsigned int fps)
456 tdm_private_vblank *private_vblank = vblank;
458 if (private_vblank->fps_fixed) {
459 VIN("fps(%u) can't be changed", private_vblank->fps);
460 return TDM_ERROR_NONE;
463 private_vblank->fps_changeable = 0;
465 if (private_vblank->fps == fps)
466 return TDM_ERROR_NONE;
468 private_vblank->fps = fps;
469 private_vblank->check_HW_or_SW = 1;
471 VIN("fps(%u) changed", fps);
473 return TDM_ERROR_NONE;
477 _tdm_vblank_ignore_global_fps(tdm_vblank *vblank, unsigned int ignore)
479 tdm_private_vblank *private_vblank = vblank;
481 if (private_vblank->ignore_global_fps == ignore)
482 return TDM_ERROR_NONE;
484 private_vblank->ignore_global_fps = ignore;
485 private_vblank->check_HW_or_SW = 1;
487 VIN("ignore_global_fps(%u)", private_vblank->ignore_global_fps);
489 return TDM_ERROR_NONE;
493 tdm_vblank_set_client_vblank_fps(unsigned int pid, const char *name, unsigned int fps)
495 tdm_private_vblank *v = NULL;
496 tdm_error ret = TDM_ERROR_NONE;
498 TDM_RETURN_VAL_IF_FAIL(pid > 0, TDM_ERROR_INVALID_PARAMETER);
499 TDM_RETURN_VAL_IF_FAIL(fps > 0, TDM_ERROR_INVALID_PARAMETER);
501 TDM_INFO("pid: %u, name: %s, fps: %d", pid, name, fps);
503 pthread_mutex_lock(&valid_list_lock);
504 LIST_FOR_EACH_ENTRY(v, &valid_vblank_list, valid_link) {
505 struct wl_client *client;
506 pid_t client_pid = 0;
511 client = wl_resource_get_client(v->resource);
515 wl_client_get_credentials(client, &client_pid, NULL, NULL);
517 if (client_pid != pid)
520 if (name && strncmp(name, TDM_VBLANK_DEFAULT_NAME, TDM_NAME_LEN)) {
521 if (strncmp(v->name, name, TDM_NAME_LEN))
525 ret = _tdm_vblank_set_fps(v, fps);
526 if (ret == TDM_ERROR_NONE)
527 TDM_INFO("(pid:%u) '%s' fps changed: %d", pid, v->name, fps);
529 TDM_ERR("(pid:%u) '%s' fps changing failed: %d", pid, v->name, fps);
531 pthread_mutex_unlock(&valid_list_lock);
533 return TDM_ERROR_NONE;
537 tdm_vblank_set_client_ignore_global_fps(unsigned int pid, const char *name, unsigned int ignore)
539 tdm_private_vblank *v = NULL;
540 tdm_error ret = TDM_ERROR_NONE;
542 TDM_RETURN_VAL_IF_FAIL(pid > 0, TDM_ERROR_INVALID_PARAMETER);
544 TDM_INFO("pid: %u, name: %s, ignore: %u", pid, name, ignore);
546 pthread_mutex_lock(&valid_list_lock);
547 LIST_FOR_EACH_ENTRY(v, &valid_vblank_list, valid_link) {
548 struct wl_client *client;
549 pid_t client_pid = 0;
554 client = wl_resource_get_client(v->resource);
558 wl_client_get_credentials(client, &client_pid, NULL, NULL);
560 if (client_pid != pid)
563 if (name && strncmp(name, TDM_VBLANK_DEFAULT_NAME, TDM_NAME_LEN)) {
564 if (strncmp(v->name, name, TDM_NAME_LEN))
568 ret = _tdm_vblank_ignore_global_fps(v, ignore);
569 if (ret == TDM_ERROR_NONE)
570 TDM_INFO("(pid:%u) '%s' ignore changed: %u", pid, v->name, ignore);
572 TDM_ERR("(pid:%u) '%s' ignore changing failed: %u", pid, v->name, ignore);
574 pthread_mutex_unlock(&valid_list_lock);
576 return TDM_ERROR_NONE;
580 tdm_vblank_enable_global_fps(unsigned int enable, unsigned int fps)
582 tdm_private_vblank *v = NULL;
585 TDM_RETURN_VAL_IF_FAIL(fps > 0, TDM_ERROR_INVALID_PARAMETER);
587 TDM_INFO("global fps: %s fps(%d)", (enable) ? "enabled" : "disabled", fps);
589 pthread_mutex_lock(&valid_list_lock);
591 vblank_global_fps = (enable) ? fps : 0;
593 LIST_FOR_EACH_ENTRY(v, &valid_vblank_list, valid_link) {
594 /* skip if vblank is created in server side */
598 v->check_HW_or_SW = 1;
600 pthread_mutex_unlock(&valid_list_lock);
602 return TDM_ERROR_NONE;
606 _tdm_vblank_thread_cb_create(tdm_private_display *private_display, void *object, tdm_thread_cb_base *cb_base, void *user_data)
608 tdm_thread_cb_vblank_create *vblank_create = (tdm_thread_cb_vblank_create *)cb_base;
609 tdm_vblank_create_handler_info *ch_info = user_data;
610 tdm_private_vblank *private_vblank;
612 TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
614 assert(ch_info != NULL);
616 private_vblank = _tdm_vblank_find_object(private_display, vblank_create->vblank_stamp);
617 if (!private_vblank) {
618 TDM_DBG("can't find vblank(%.0f) from valid_list", vblank_create->vblank_stamp);
622 private_vblank->in_create_handler = 1;
623 tdm_display_unlock(private_display);
624 ch_info->func(private_vblank, ch_info->user_data);
625 tdm_display_lock(private_display);
626 private_vblank->in_create_handler = 0;
630 _tdm_vblank_call_thread_cb(tdm_private_vblank *private_vblank)
632 tdm_thread_cb_vblank_create vblank_create;
635 memset(&vblank_create, 0, sizeof vblank_create);
636 vblank_create.base.type = TDM_THREAD_CB_VBLANK_CREATE;
637 vblank_create.base.length = sizeof vblank_create;
638 vblank_create.base.object_stamp = 1;
639 vblank_create.base.data = NULL;
640 vblank_create.base.sync = 0;
641 vblank_create.vblank_stamp = private_vblank->stamp;
643 ret = tdm_thread_cb_call(private_vblank->dpy, &vblank_create.base, 1);
644 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
646 return TDM_ERROR_NONE;
650 tdm_vblank_add_create_handler(tdm_display *dpy, tdm_vblank_create_handler func, void *user_data)
652 tdm_vblank_create_handler_info *ch_info = NULL;
655 TDM_RETURN_VAL_IF_FAIL(dpy != NULL, TDM_ERROR_INVALID_PARAMETER);
656 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
658 pthread_mutex_lock(&valid_list_lock);
660 LIST_FOR_EACH_ENTRY(ch_info, &create_handler_list, link) {
661 if (ch_info->func == func && ch_info->user_data == user_data) {
662 TDM_ERR("can't be added twice");
663 pthread_mutex_unlock(&valid_list_lock);
664 return TDM_ERROR_BAD_REQUEST;
668 ch_info = calloc(1, sizeof *ch_info);
670 TDM_ERR("alloc failed: %m");
671 pthread_mutex_unlock(&valid_list_lock);
672 return TDM_ERROR_OUT_OF_MEMORY;
675 tdm_display_lock(dpy);
676 ret = tdm_thread_cb_add(dpy, TDM_THREAD_CB_VBLANK_CREATE, NULL, _tdm_vblank_thread_cb_create, ch_info);
677 tdm_display_unlock(dpy);
679 if (ret != TDM_ERROR_NONE) {
680 TDM_ERR("tdm_thread_cb_add failed");
682 pthread_mutex_unlock(&valid_list_lock);
686 ch_info->func = func;
687 ch_info->user_data = user_data;
689 LIST_ADDTAIL(&ch_info->link, &create_handler_list);
691 pthread_mutex_unlock(&valid_list_lock);
693 return TDM_ERROR_NONE;
697 tdm_vblank_remove_create_handler(tdm_display *dpy, tdm_vblank_create_handler func, void *user_data)
699 tdm_vblank_create_handler_info *ch_info = NULL, *hh = NULL;
701 TDM_RETURN_IF_FAIL(dpy != NULL);
702 TDM_RETURN_IF_FAIL(func != NULL);
704 tdm_display_lock(dpy);
705 pthread_mutex_lock(&valid_list_lock);
706 LIST_FOR_EACH_ENTRY_SAFE(ch_info, hh, &create_handler_list, link) {
707 if (ch_info->func != func || ch_info->user_data != user_data)
710 tdm_thread_cb_remove(dpy, TDM_THREAD_CB_VBLANK_CREATE, NULL, _tdm_vblank_thread_cb_create, ch_info);
712 LIST_DEL(&ch_info->link);
714 pthread_mutex_unlock(&valid_list_lock);
715 tdm_display_unlock(dpy);
718 pthread_mutex_unlock(&valid_list_lock);
719 tdm_display_unlock(dpy);
723 _tdm_vblank_find_display(tdm_private_display *private_display, double stamp)
725 return private_display;
729 tdm_vblank_init(tdm_display *dpy)
731 if (vblank_list_inited)
732 return TDM_ERROR_NONE;
734 LIST_INITHEAD(&valid_vblank_list);
735 LIST_INITHEAD(&valid_wait_list);
736 LIST_INITHEAD(&create_handler_list);
738 tdm_thread_cb_set_find_func(TDM_THREAD_CB_VBLANK_SW, _tdm_vblank_find_object);
739 tdm_thread_cb_set_find_func(TDM_THREAD_CB_VBLANK_CREATE, _tdm_vblank_find_display);
741 vblank_list_inited = 1;
743 return TDM_ERROR_NONE;
747 tdm_vblank_deinit(tdm_display *dpy)
749 tdm_vblank_create_handler_info *ch_info = NULL, *hh = NULL;
751 if (!vblank_list_inited)
754 if (!LIST_IS_EMPTY(&valid_vblank_list))
755 TDM_NEVER_GET_HERE();
757 if (!LIST_IS_EMPTY(&valid_wait_list))
758 TDM_NEVER_GET_HERE();
760 LIST_FOR_EACH_ENTRY_SAFE(ch_info, hh, &create_handler_list, link) {
761 LIST_DEL(&ch_info->link);
765 vblank_list_inited = 0;
769 tdm_vblank_create(tdm_display *dpy, tdm_output *output, tdm_error *error)
771 tdm_private_vblank *private_vblank;
774 TDM_RETURN_VAL_IF_FAIL_WITH_ERROR(dpy != NULL, TDM_ERROR_INVALID_PARAMETER, NULL);
775 TDM_RETURN_VAL_IF_FAIL_WITH_ERROR(output != NULL, TDM_ERROR_INVALID_PARAMETER, NULL);
778 *error = TDM_ERROR_NONE;
780 private_vblank = calloc(1, sizeof * private_vblank);
781 if (!private_vblank) {
782 /* LCOV_EXCL_START */
784 *error = TDM_ERROR_OUT_OF_MEMORY;
785 TDM_ERR("alloc failed");
790 ret = tdm_output_add_change_handler(output, _tdm_vblank_cb_output_change, private_vblank);
791 if (ret != TDM_ERROR_NONE) {
792 free(private_vblank);
794 *error = TDM_ERROR_OPERATION_FAILED;
795 TDM_ERR("tdm_output_add_change_handler failed");
799 private_vblank->stamp = ++stamp;
800 private_vblank->owner_tid = syscall(SYS_gettid);
801 private_vblank->dpy = dpy;
802 private_vblank->output = output;
803 private_vblank->check_HW_or_SW = 1;
804 private_vblank->fps_changeable = 1;
805 private_vblank->last_interval = -1;
807 _tdm_vblank_update_output_info(private_vblank);
809 strncpy(private_vblank->name, TDM_VBLANK_DEFAULT_NAME, TDM_NAME_LEN - 1);
810 private_vblank->name[TDM_NAME_LEN - 1] = '\0';
812 LIST_INITHEAD(&private_vblank->HW_wait_list);
813 LIST_INITHEAD(&private_vblank->SW_wait_list);
815 _tdm_vblank_valid_list_add(&private_vblank->valid_link, &valid_vblank_list);
817 VIN("created. vrefresh(%d) connection(%d)",
818 private_vblank->vrefresh, private_vblank->connection);
820 tdm_display_lock(private_vblank->dpy);
821 _tdm_vblank_call_thread_cb(private_vblank);
822 tdm_display_unlock(private_vblank->dpy);
824 return (tdm_vblank *)private_vblank;
828 tdm_vblank_destroy(tdm_vblank *vblank)
830 tdm_private_vblank *private_vblank = vblank;
831 tdm_vblank_wait_info *w = NULL, *ww = NULL;
833 TDM_RETURN_IF_FAIL(tdm_vblank_is_valid(vblank));
835 if (private_vblank->in_create_handler) {
836 TDM_ERR("NOT allowed to be called in a create handler");
840 _tdm_vblank_valid_list_del(&private_vblank->valid_link);
842 if (private_vblank->SW_timer) {
843 tdm_display_lock(private_vblank->dpy);
844 tdm_thread_cb_remove(private_vblank, TDM_THREAD_CB_VBLANK_SW, NULL, _tdm_vblank_cb_vblank_SW, NULL);
845 tdm_event_loop_source_remove(private_vblank->SW_timer);
846 tdm_display_unlock(private_vblank->dpy);
849 tdm_output_remove_change_handler(private_vblank->output,
850 _tdm_vblank_cb_output_change, private_vblank);
852 _tdm_vblank_free_HW_wait(private_vblank, 0, 0);
854 if (!LIST_IS_EMPTY(&private_vblank->SW_wait_list)) {
856 const char *proc_name;
857 _tdm_vblank_get_client_information(private_vblank, &pid, &proc_name);
858 VER("TDM SW vblank destroyed forcely!! (pid: %u, name: %s)", pid, proc_name);
861 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->SW_wait_list, link) {
863 _tdm_vblank_valid_list_del(&w->valid_link);
867 if (private_vblank->timeout_timer) {
868 tdm_display_lock(private_vblank->dpy);
869 tdm_event_loop_source_remove(private_vblank->timeout_timer);
870 tdm_display_unlock(private_vblank->dpy);
875 free(private_vblank);
879 tdm_vblank_get_output(tdm_vblank *vblank, tdm_error *error)
881 tdm_private_vblank *private_vblank = vblank;
882 tdm_error ret = TDM_ERROR_NONE;
884 TDM_RETURN_VAL_IF_FAIL_WITH_ERROR(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER, NULL);
887 *error = TDM_ERROR_NONE;
889 return private_vblank->output;
893 tdm_vblank_get_client_pid(tdm_vblank *vblank, pid_t *pid)
895 tdm_private_vblank *private_vblank = vblank;
896 struct wl_client *client;
898 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
899 TDM_RETURN_VAL_IF_FAIL(pid != NULL, TDM_ERROR_INVALID_PARAMETER);
901 if (!private_vblank->resource) {
903 return TDM_ERROR_NONE;
906 client = wl_resource_get_client(private_vblank->resource);
909 return TDM_ERROR_NONE;
912 wl_client_get_credentials(client, pid, NULL, NULL);
914 return TDM_ERROR_NONE;
918 tdm_vblank_set_name(tdm_vblank *vblank, const char *name)
920 tdm_private_vblank *private_vblank = vblank;
922 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
925 name = TDM_VBLANK_DEFAULT_NAME;
927 strncpy(private_vblank->name, name, TDM_NAME_LEN - 1);
928 private_vblank->name[TDM_NAME_LEN - 1] = '\0';
930 VIN("name(%s)", name);
932 return TDM_ERROR_NONE;
936 tdm_vblank_get_name(tdm_vblank *vblank, const char **name)
938 tdm_private_vblank *private_vblank = vblank;
940 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
941 TDM_RETURN_VAL_IF_FAIL(name != NULL, TDM_ERROR_INVALID_PARAMETER);
943 *name = (const char*)private_vblank->name;
945 return TDM_ERROR_NONE;
949 tdm_vblank_set_fps(tdm_vblank *vblank, unsigned int fps)
951 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
952 TDM_RETURN_VAL_IF_FAIL(fps > 0, TDM_ERROR_INVALID_PARAMETER);
954 return _tdm_vblank_set_fps(vblank, fps);
958 tdm_vblank_get_fps(tdm_vblank *vblank, unsigned int *fps)
960 tdm_private_vblank *private_vblank = vblank;
962 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
963 TDM_RETURN_VAL_IF_FAIL(fps != NULL, TDM_ERROR_INVALID_PARAMETER);
965 *fps = private_vblank->fps;
967 return TDM_ERROR_NONE;
971 tdm_vblank_set_fixed_fps(tdm_vblank *vblank, unsigned int fps)
973 tdm_private_vblank *private_vblank = vblank;
975 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
976 TDM_RETURN_VAL_IF_FAIL(fps > 0, TDM_ERROR_INVALID_PARAMETER);
978 private_vblank->fps_changeable = 0;
979 private_vblank->fps_fixed = 1;
981 if (private_vblank->fps == fps)
982 return TDM_ERROR_NONE;
984 private_vblank->fps = fps;
985 private_vblank->check_HW_or_SW = 1;
987 VIN("fps(%u) fixed", fps);
989 return TDM_ERROR_NONE;
993 tdm_vblank_ignore_global_fps(tdm_vblank *vblank, unsigned int ignore)
995 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
997 return _tdm_vblank_ignore_global_fps(vblank, ignore);
1001 tdm_vblank_set_offset(tdm_vblank *vblank, int offset)
1003 tdm_private_vblank *private_vblank = vblank;
1005 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1006 TDM_RETURN_VAL_IF_FAIL(offset >= 0, TDM_ERROR_INVALID_PARAMETER);
1008 if (private_vblank->offset == offset)
1009 return TDM_ERROR_NONE;
1011 private_vblank->offset = offset;
1012 private_vblank->check_HW_or_SW = 1;
1014 VIN("offset(%u)", private_vblank->offset);
1016 return TDM_ERROR_NONE;
1020 tdm_vblank_get_offset(tdm_vblank *vblank, int *offset)
1022 tdm_private_vblank *private_vblank = vblank;
1024 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1025 TDM_RETURN_VAL_IF_FAIL(offset != NULL, TDM_ERROR_INVALID_PARAMETER);
1027 *offset = private_vblank->offset;
1029 return TDM_ERROR_NONE;
1033 tdm_vblank_set_enable_fake(tdm_vblank *vblank, unsigned int enable_fake)
1035 tdm_private_vblank *private_vblank = vblank;
1037 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1039 if (private_vblank->enable_fake == enable_fake)
1040 return TDM_ERROR_NONE;
1042 private_vblank->enable_fake = enable_fake;
1044 VIN("enable_fake(%u)", private_vblank->enable_fake);
1046 return TDM_ERROR_NONE;
1050 tdm_vblank_get_enable_fake(tdm_vblank *vblank, unsigned int *enable_fake)
1052 tdm_private_vblank *private_vblank = vblank;
1054 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1055 TDM_RETURN_VAL_IF_FAIL(enable_fake != NULL, TDM_ERROR_INVALID_PARAMETER);
1057 *enable_fake = private_vblank->enable_fake;
1059 return TDM_ERROR_NONE;
1063 _tdm_vblank_timeout_timer_cb(void *user_data)
1065 tdm_private_vblank *private_vblank = user_data;
1066 tdm_vblank_wait_info *w = NULL;
1068 const char *proc_name;
1070 TDM_RETURN_VAL_IF_FAIL(private_vblank != NULL, TDM_ERROR_OPERATION_FAILED);
1072 _tdm_vblank_get_client_information(private_vblank, &pid, &proc_name);
1074 VER("TDM vblank TIMEOUT!! (pid: %u, name: %s)", pid, proc_name);
1075 VER("quotient(%u) last_seq(%u) last_time(%.6f)", private_vblank->quotient, private_vblank->last_seq, private_vblank->last_time);
1077 LIST_FOR_EACH_ENTRY(w, &private_vblank->HW_wait_list, link) {
1078 VER("wait_info(%p) req_time(%.6f) target_seq(%u) interval(%u) hw_interval(%d)",
1079 w, w->req_time, w->target_seq, w->interval, w->hw_interval);
1080 tdm_output_vblank_print_wait_information(private_vblank->output, w);
1083 LIST_FOR_EACH_ENTRY(w, &private_vblank->SW_wait_list, link) {
1084 VER("wait_info(%p) req_time(%.6f) target_time(%.6f) target_seq(%u) interval(%u)",
1085 w, w->req_time, w->target_time, w->target_seq, w->interval);
1088 return TDM_ERROR_NONE;
1092 _tdm_vblank_timeout_timer_update(tdm_private_vblank *private_vblank, int ms_delay)
1096 if (!private_vblank->timeout_timer) {
1097 private_vblank->timeout_timer =
1098 tdm_event_loop_add_timer_handler(private_vblank->dpy,
1099 _tdm_vblank_timeout_timer_cb,
1102 if (!private_vblank->timeout_timer) {
1103 VER("couldn't add timer");
1106 VIN("Create Timeout timer");
1109 ret = tdm_event_loop_source_timer_update(private_vblank->timeout_timer, ms_delay);
1110 if (ret != TDM_ERROR_NONE) {
1111 VER("couldn't update timer");
1116 VDB("timeout timer updated");
1118 VDB("timeout timer disabled");
1122 _tdm_vblank_sw_timer_update(tdm_private_vblank *private_vblank)
1124 tdm_vblank_wait_info *first_wait_info = NULL;
1125 double curr, target;
1129 if (LIST_IS_EMPTY(&private_vblank->SW_wait_list)) {
1130 VER("no wait_info");
1131 return TDM_ERROR_OPERATION_FAILED;
1134 first_wait_info = container_of(private_vblank->SW_wait_list.next, first_wait_info, link);
1135 curr = tdm_helper_get_time();
1136 target = first_wait_info->target_time;
1138 /* ms_delay should be more that 1 */
1142 ms_delay = ceil((target - curr) * 1000.0);
1147 if (ms_delay > 5000)
1148 VER("wait(%p) curr(%.6f) target(%.6f) ms_delay(%d)", first_wait_info, curr, target, ms_delay);
1150 VDB("wait(%p) curr(%.6f) target(%.6f) ms_delay(%d)", first_wait_info, curr, target, ms_delay);
1152 tdm_display_lock(private_vblank->dpy);
1154 if (!private_vblank->SW_timer) {
1155 private_vblank->SW_timer =
1156 tdm_event_loop_add_timer_handler(private_vblank->dpy,
1157 _tdm_vblank_cb_timeout_SW,
1160 if (!private_vblank->SW_timer) {
1161 tdm_display_unlock(private_vblank->dpy);
1162 VER("couldn't add timer");
1166 ret = tdm_thread_cb_add(private_vblank, TDM_THREAD_CB_VBLANK_SW, NULL, _tdm_vblank_cb_vblank_SW, NULL);
1167 if (ret != TDM_ERROR_NONE) {
1168 tdm_event_loop_source_remove(private_vblank->SW_timer);
1169 private_vblank->SW_timer = NULL;
1170 tdm_display_unlock(private_vblank->dpy);
1171 VER("couldn't add thread_cb");
1175 VIN("Create SW timer");
1178 private_vblank->last_interval = ms_delay;
1180 ret = tdm_event_loop_source_timer_update(private_vblank->SW_timer, ms_delay);
1181 if (ret != TDM_ERROR_NONE) {
1182 /* LCOV_EXCL_START */
1183 tdm_display_unlock(private_vblank->dpy);
1184 VER("couldn't update timer");
1186 /* LCOV_EXCL_STOP */
1189 tdm_display_unlock(private_vblank->dpy);
1191 return TDM_ERROR_NONE;
1195 _tdm_vblank_cb_vblank_HW(tdm_output *output, unsigned int sequence,
1196 unsigned int tv_sec, unsigned int tv_usec,
1199 tdm_vblank_wait_info *wait_info = user_data;
1200 tdm_private_vblank *private_vblank;
1202 if (!_tdm_vblank_check_valid_wait(wait_info)) {
1203 TDM_ERR("can't find wait(%p) from valid_wait_list", wait_info);
1207 private_vblank = wait_info->private_vblank;
1208 TDM_RETURN_IF_FAIL(tdm_vblank_is_valid(private_vblank));
1210 assert(private_vblank->owner_tid == syscall(SYS_gettid));
1212 if (!_tdm_vblank_find_wait(wait_info, &private_vblank->HW_wait_list)) {
1213 VER("can't find wait(%p)", wait_info);
1217 if (wait_info->type == VBLANK_TYPE_HW_SW) {
1220 LIST_DEL(&wait_info->link);
1222 /* kernel sends the wrong time of first vblank event. and the time of
1223 * second vblank is correct.
1225 wait_info->target_time = TDM_TIME(tv_sec, tv_usec);
1226 wait_info->target_time += (((double)private_vblank->offset) / 1000.0);
1228 _tdm_vblank_insert_wait(wait_info, &private_vblank->SW_wait_list);
1230 ret = _tdm_vblank_sw_timer_update(private_vblank);
1232 /* wait_info will be freed in _tdm_vblank_cb_timeout_SW() */
1233 if (ret == TDM_ERROR_NONE) {
1234 VIN("wait(%p) SW timer", wait_info);
1238 VER("couldn't update sw timer");
1241 tdm_display_lock(private_vblank->dpy);
1242 _tdm_vblank_timeout_timer_update(private_vblank, 0);
1243 tdm_display_unlock(private_vblank->dpy);
1245 LIST_DEL(&wait_info->link);
1246 _tdm_vblank_valid_list_del(&wait_info->valid_link);
1248 /* tv_sec & tv_usec shouldn't be zero. But sometimes they are zero in some hardware.
1249 * We need to prohibit that last_time becomes zero because "last_time == 0" means
1250 * that a tdm_vblank object is just created and doesn't have the first vblank event.
1251 * If last_time becomes 0, it would make issue.
1253 if (tv_sec == 0 && tv_usec == 0) {
1254 double curr = tdm_helper_get_time();
1255 tv_sec = TDM_TIME_SEC(curr);
1256 tv_usec = TDM_TIME_USEC(curr);
1259 private_vblank->last_seq = wait_info->target_seq;
1260 private_vblank->last_time = TDM_TIME(tv_sec, tv_usec);
1262 VDB("wait(%p) last(%.6f) sequence(%u) done", wait_info, private_vblank->last_time, wait_info->target_seq);
1264 if (tdm_ttrace_module & TDM_TTRACE_VBLANK)
1265 TDM_TRACE_ASYNC_END((int)wait_info->req_time, "TDM_Vblank:%u", (unsigned int)private_vblank->stamp);
1267 if (wait_info->func)
1268 wait_info->func(private_vblank, TDM_ERROR_NONE, wait_info->target_seq,
1269 tv_sec, tv_usec, wait_info->user_data);
1275 _tdm_vblank_wait_HW(tdm_vblank_wait_info *wait_info)
1277 tdm_private_vblank *private_vblank = wait_info->private_vblank;
1281 if (private_vblank->last_type != VBLANK_EVENT_TYPE_HW) {
1282 VIN("Use HW vblank");
1283 private_vblank->last_type = VBLANK_EVENT_TYPE_HW;
1286 _tdm_vblank_insert_wait(wait_info, &private_vblank->HW_wait_list);
1288 if (private_vblank->last_time == 0) {
1289 hw_interval = wait_info->interval * private_vblank->quotient;
1291 /* SW vblank starts from now. SW vblank doesn't need to be aligned with HW vblank. */
1292 private_vblank->last_seq = 0;
1293 private_vblank->last_time = tdm_helper_get_time();
1295 /* +1 ms to call the handler ASAP at the first. no matter for SW timer. */
1296 wait_info->target_seq = 1;
1298 VDB("wait(%p) last(%.6f) hw_itvl(%d) targ_seq(%u)",
1299 wait_info, private_vblank->last_time, hw_interval, wait_info->target_seq);
1301 double last, prev, req, curr, target;
1304 last = private_vblank->last_time;
1305 req = wait_info->req_time;
1307 /* make sure that req time is bigger than last. */
1311 skip = (unsigned int)((req - last) / private_vblank->vblank_gap + TDM_TIME_MARGIN);
1312 prev = last + private_vblank->vblank_gap * skip;
1314 curr = tdm_helper_get_time();
1316 while (target < curr)
1317 target += (private_vblank->vblank_gap * wait_info->interval);
1319 hw_interval = (unsigned int)((target - curr) / private_vblank->HW_vblank_gap + TDM_TIME_MARGIN) + 1;
1321 wait_info->target_seq = private_vblank->last_seq;
1322 wait_info->target_seq += (unsigned int)((target - last) / private_vblank->vblank_gap + TDM_TIME_MARGIN);
1324 if (hw_interval > 300)
1325 VER("wait(%p) last(%.6f) req(%.6f) prev(%.6f) curr(%.6f) targ(%.6f,%.6f) skip(%u) hw_itvl(%d) targ_seq(%u)",
1326 wait_info, last, req - last, prev - last, curr - last, target, target - last,
1327 skip, hw_interval, wait_info->target_seq);
1329 VDB("wait(%p) last(%.6f) req(%.6f) prev(%.6f) curr(%.6f) targ(%.6f,%.6f) skip(%u) hw_itvl(%d) targ_seq(%u)",
1330 wait_info, last, req - last, prev - last, curr - last, target, target - last,
1331 skip, hw_interval, wait_info->target_seq);
1334 private_vblank->last_interval = hw_interval;
1335 wait_info->hw_interval = hw_interval;
1337 if (tdm_ttrace_module & TDM_TTRACE_VBLANK)
1338 TDM_TRACE_ASYNC_BEGIN((int)wait_info->req_time, "TDM_Vblank:%u", (unsigned int)private_vblank->stamp);
1340 if (private_vblank->add_front)
1341 ret = tdm_output_wait_vblank_add_front(private_vblank->output, hw_interval, 0,
1342 _tdm_vblank_cb_vblank_HW, wait_info);
1344 ret = tdm_output_wait_vblank(private_vblank->output, hw_interval, 0,
1345 _tdm_vblank_cb_vblank_HW, wait_info);
1347 if (ret != TDM_ERROR_NONE) {
1348 LIST_DEL(&wait_info->link);
1352 VDB("wait(%p) waiting", wait_info);
1354 return TDM_ERROR_NONE;
1358 _tdm_vblank_cb_vblank_SW(tdm_private_display *private_display, void *object, tdm_thread_cb_base *cb_base, void *user_data)
1360 tdm_private_vblank *private_vblank = object;
1361 tdm_vblank_wait_info *first_wait_info = NULL, *w = NULL, *ww = NULL;
1363 TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
1365 assert(private_vblank->owner_tid == syscall(SYS_gettid));
1367 if (LIST_IS_EMPTY(&private_vblank->SW_wait_list)) {
1368 VER("no wait_info");
1372 if (!LIST_IS_EMPTY(&private_vblank->HW_wait_list))
1373 TDM_NEVER_GET_HERE();
1375 first_wait_info = container_of(private_vblank->SW_wait_list.next, first_wait_info, link);
1376 TDM_RETURN_IF_FAIL(first_wait_info != NULL);
1378 _tdm_vblank_timeout_timer_update(private_vblank, 0);
1380 if (private_vblank->last_seq >= first_wait_info->target_seq)
1381 TDM_ERR("last_seq(%u) target_seq(%u)", private_vblank->last_seq, first_wait_info->target_seq);
1383 private_vblank->last_seq = first_wait_info->target_seq;
1384 private_vblank->last_time = first_wait_info->target_time;
1386 VDB("wait(%p) last(%.6f) sequence(%u) done", first_wait_info, private_vblank->last_time, first_wait_info->target_seq);
1388 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->SW_wait_list, link) {
1389 if (w->target_time != first_wait_info->target_time)
1393 _tdm_vblank_valid_list_del(&w->valid_link);
1396 tdm_display_unlock(private_vblank->dpy);
1397 w->func(private_vblank, TDM_ERROR_NONE, w->target_seq,
1398 TDM_TIME_SEC(w->target_time), TDM_TIME_USEC(w->target_time),
1400 tdm_display_lock(private_vblank->dpy);
1408 _tdm_vblank_cb_timeout_SW(void *user_data)
1410 tdm_private_vblank *private_vblank = user_data;
1411 tdm_thread_cb_vblank_sw vblank_sw;
1414 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(private_vblank), TDM_ERROR_OPERATION_FAILED);
1416 VIN("timeout_SW -> thread-cb");
1418 memset(&vblank_sw, 0, sizeof vblank_sw);
1419 vblank_sw.base.type = TDM_THREAD_CB_VBLANK_SW;
1420 vblank_sw.base.length = sizeof vblank_sw;
1421 vblank_sw.base.object_stamp = private_vblank->stamp;
1422 vblank_sw.base.data = NULL;
1423 vblank_sw.base.sync = 0;
1425 ret = tdm_thread_cb_call(private_vblank, &vblank_sw.base, 1);
1426 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
1428 return TDM_ERROR_NONE;
1432 _tdm_vblank_wait_SW(tdm_vblank_wait_info *wait_info)
1434 tdm_private_vblank *private_vblank = wait_info->private_vblank;
1436 double last, prev, req, curr, target;
1439 if (private_vblank->last_type != VBLANK_EVENT_TYPE_SW) {
1440 tdm_output_dpms dpms;
1442 tdm_output_get_dpms(private_vblank->output, &dpms);
1444 VIN("Use SW vblank. connection(%s), dpms(%s), vrefresh(%d), fps(%d), offset(%d)",
1445 tdm_status_str(private_vblank->connection), tdm_dpms_str(dpms),
1446 private_vblank->vrefresh, private_vblank->fps, private_vblank->offset);
1447 private_vblank->last_type = VBLANK_EVENT_TYPE_SW;
1450 if (private_vblank->last_time == 0) {
1451 /* SW vblank starts from now. SW vblank doesn't need to be aligned with HW vblank. */
1452 private_vblank->last_seq = 0;
1453 private_vblank->last_time = tdm_helper_get_time() - private_vblank->vblank_gap;
1455 /* +1 ms to call the handler ASAP at the first. no matter for SW timer. */
1456 wait_info->target_seq = 1;
1459 last = private_vblank->last_time;
1460 req = wait_info->req_time;
1462 /* make sure that req time is bigger than last. */
1466 skip = (unsigned int)((req - last) / private_vblank->vblank_gap + TDM_TIME_MARGIN);
1467 prev = last + private_vblank->vblank_gap * skip;
1469 curr = tdm_helper_get_time();
1470 target = prev + private_vblank->vblank_gap * wait_info->interval;
1472 while (target < curr)
1473 target += private_vblank->vblank_gap;
1475 wait_info->target_seq = private_vblank->last_seq;
1476 wait_info->target_seq += (unsigned int)((target - last) / private_vblank->vblank_gap + TDM_TIME_MARGIN);
1478 wait_info->target_time = target + (double)(private_vblank->offset) / 1000;
1480 VDB("wait(%p) last(%.6f) req(%.6f) prev(%.6f) curr(%.6f) targ(%.6f,%.6f) skip(%u) targ_seq(%u)",
1481 wait_info, last, req - last, prev - last, curr - last,
1482 target, target - last, skip, wait_info->target_seq);
1484 _tdm_vblank_insert_wait(wait_info, &private_vblank->SW_wait_list);
1486 ret = _tdm_vblank_sw_timer_update(private_vblank);
1487 if (ret != TDM_ERROR_NONE) {
1488 /* LCOV_EXCL_START */
1489 LIST_DEL(&wait_info->link);
1490 VER("couldn't update sw timer");
1492 /* LCOV_EXCL_STOP */
1495 return TDM_ERROR_NONE;
1499 tdm_vblank_wait(tdm_vblank *vblank, unsigned int req_sec, unsigned int req_usec,
1500 unsigned int interval, tdm_vblank_handler func, void *user_data)
1502 tdm_private_vblank *private_vblank = vblank;
1503 tdm_vblank_wait_info *wait_info;
1504 tdm_output_dpms dpms = TDM_OUTPUT_DPMS_OFF;
1508 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1509 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
1510 TDM_RETURN_VAL_IF_FAIL(interval > 0, TDM_ERROR_INVALID_PARAMETER);
1512 if (private_vblank->in_create_handler) {
1513 TDM_ERR("NOT allowed to be called in a create handler");
1514 return TDM_ERROR_BAD_REQUEST;
1517 if (private_vblank->owner_tid != syscall(SYS_gettid)) {
1518 TDM_ERR("SHOULD be called in the owner thread(%d)", (int)private_vblank->owner_tid);
1519 return TDM_ERROR_BAD_REQUEST;
1522 tdm_output_get_dpms(private_vblank->output, &dpms);
1524 if (!private_vblank->enable_fake) {
1525 if (private_vblank->connection == TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
1526 VER("can't wait a vblank: output disconnected");
1527 return TDM_ERROR_OUTPUT_DISCONNECTED;
1529 if (TDM_OUTPUT_DPMS_VSYNC_IS_OFF(dpms)) {
1530 VER("can't wait a vblank: DPMS %s", tdm_dpms_str(dpms));
1531 return TDM_ERROR_DPMS_OFF;
1535 wait_info = calloc(1, sizeof * wait_info);
1537 /* LCOV_EXCL_START */
1538 VER("alloc failed");
1539 return TDM_ERROR_OUT_OF_MEMORY;
1540 /* LCOV_EXCL_STOP */
1542 _tdm_vblank_update_output_info(private_vblank);
1544 /* set request time to current time if 0. This function seems to be called in server side. */
1545 if (req_sec == 0 && req_usec == 0) {
1546 double curr = tdm_helper_get_time();
1547 req_sec = TDM_TIME_SEC(curr);
1548 req_usec = TDM_TIME_USEC(curr);
1551 LIST_INITHEAD(&wait_info->link);
1553 _tdm_vblank_valid_list_add(&wait_info->valid_link, &valid_wait_list);
1555 wait_info->stamp = ++stamp;
1556 wait_info->req_time = TDM_TIME(req_sec, req_usec);
1557 wait_info->interval = interval;
1558 wait_info->func = func;
1559 wait_info->user_data = user_data;
1560 wait_info->private_vblank = private_vblank;
1562 if (!private_vblank->resource ||
1563 private_vblank->ignore_global_fps ||
1564 vblank_global_fps == 0 ||
1565 private_vblank->fps < vblank_global_fps)
1566 fps = private_vblank->fps;
1568 fps = vblank_global_fps;
1570 if (private_vblank->check_HW_or_SW) {
1571 private_vblank->check_HW_or_SW = 0;
1572 private_vblank->vblank_gap = 1.0 / fps;
1573 private_vblank->quotient = private_vblank->vrefresh / fps;
1576 /* 1) if fps != factor of vrefresh, SW timer
1577 * 2) if fps == factor of vrefresh && dpms == off, SW timer (Fake HW vblank)
1578 * 3) if fps == factor of vrefresh && dpms == on && offset == 0, HW vblank
1579 * 4) if fps == factor of vrefresh && dpms == on && offset != 0, HW vblank + SW timer
1580 * In case of 1), we really don't need to align with HW vblank.
1582 if (private_vblank->vrefresh % fps)
1583 wait_info->type = VBLANK_TYPE_SW;
1584 else if (TDM_OUTPUT_DPMS_VSYNC_IS_OFF(dpms) ||
1585 private_vblank->connection != TDM_OUTPUT_CONN_STATUS_MODE_SETTED)
1586 wait_info->type = VBLANK_TYPE_SW_FAKE;
1587 else if (private_vblank->offset == 0)
1588 wait_info->type = VBLANK_TYPE_HW;
1590 wait_info->type = VBLANK_TYPE_HW_SW;
1592 if (wait_info->type == VBLANK_TYPE_SW || wait_info->type == VBLANK_TYPE_SW_FAKE) {
1593 ret = _tdm_vblank_wait_SW(wait_info);
1594 if (ret != TDM_ERROR_NONE)
1595 TDM_ERR("_tdm_vblank_wait_SW failed: ret(%d)", ret);
1597 ret = _tdm_vblank_wait_HW(wait_info);
1598 if (ret != TDM_ERROR_NONE)
1599 TDM_ERR("_tdm_vblank_wait_HW failed: ret(%d)", ret);
1601 if (ret == TDM_ERROR_OUTPUT_DISCONNECTED || ret == TDM_ERROR_DPMS_OFF) {
1602 ret = _tdm_vblank_wait_SW(wait_info);
1603 if (ret != TDM_ERROR_NONE)
1604 TDM_ERR("_tdm_vblank_wait_SW failed: ret(%d)", ret);
1608 if (ret != TDM_ERROR_NONE) {
1609 /* LCOV_EXCL_START */
1610 LIST_DEL(&wait_info->link);
1611 _tdm_vblank_valid_list_del(&wait_info->valid_link);
1613 tdm_display_lock(private_vblank->dpy);
1614 tdm_output_remove_vblank_handler_internal(private_vblank->output, _tdm_vblank_cb_vblank_HW, wait_info);
1615 tdm_display_unlock(private_vblank->dpy);
1619 /* LCOV_EXCL_STOP */
1622 tdm_display_lock(private_vblank->dpy);
1623 _tdm_vblank_timeout_timer_update(private_vblank, 1000);
1624 tdm_display_unlock(private_vblank->dpy);
1626 return TDM_ERROR_NONE;
1630 tdm_vblank_wait_seq(tdm_vblank *vblank, unsigned int req_sec, unsigned int req_usec,
1631 unsigned int sequence, tdm_vblank_handler func, void *user_data)
1633 tdm_private_vblank *private_vblank = vblank;
1634 unsigned int interval;
1636 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1637 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
1639 /* if the sequence of vblank reaches the max value, sequence can be 0. */
1641 if (private_vblank->last_seq == 0) {
1642 VWR("can't calculate interval with sequence(%u)", sequence);
1644 } else if (sequence > private_vblank->last_seq ||
1645 TDM_VBLANK_SEQ_REACHED_MAX(sequence, private_vblank->last_seq)) {
1646 double last, curr, seq_target;
1648 last = private_vblank->last_time;
1649 curr = tdm_helper_get_time();
1650 seq_target = last + private_vblank->vblank_gap * (sequence - private_vblank->last_seq);
1652 /* If target is old or too close(1ms) to current, we call the handler immediately.
1653 * 2ms? it seems too much.
1655 if (seq_target < curr) {
1656 func(vblank, TDM_ERROR_NONE, sequence,
1657 TDM_TIME_SEC(seq_target), TDM_TIME_USEC(seq_target),
1659 return TDM_ERROR_NONE;
1660 } else if ((seq_target - curr) < 0.001) { /* 1ms */
1661 func(vblank, TDM_ERROR_NONE, sequence,
1662 TDM_TIME_SEC(curr), TDM_TIME_USEC(curr),
1664 return TDM_ERROR_NONE;
1667 interval = (unsigned int)((seq_target - curr) / private_vblank->vblank_gap + TDM_TIME_MARGIN) + 1;
1669 /* this seems error. But we handle this like normal to avoid the unexpected error in cliend side */
1670 VER("sequence(%u) should be over the last sequence(%u)", sequence, private_vblank->last_seq);
1671 func(vblank, TDM_ERROR_NONE, private_vblank->last_seq,
1672 TDM_TIME_SEC(private_vblank->last_time), TDM_TIME_USEC(private_vblank->last_time),
1674 return TDM_ERROR_NONE;
1677 return tdm_vblank_wait(vblank, req_sec, req_usec, interval, func, user_data);
1680 /* LCOV_EXCL_START */
1682 tdm_vblank_set_add_front(tdm_vblank *vblank, unsigned int add_front)
1684 tdm_private_vblank *private_vblank = vblank;
1686 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1688 TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
1690 if (private_vblank->add_front == add_front)
1691 return TDM_ERROR_NONE;
1693 private_vblank->add_front = add_front;
1695 return TDM_ERROR_NONE;
1697 /* LCOV_EXCL_STOP */
1700 tdm_vblank_set_resource(tdm_vblank *vblank, struct wl_resource *resource)
1702 tdm_private_vblank *private_vblank = vblank;
1704 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1705 TDM_RETURN_VAL_IF_FAIL(private_vblank->resource == NULL, TDM_ERROR_OPERATION_FAILED);
1707 private_vblank->resource = resource;
1709 return TDM_ERROR_NONE;
1713 tdm_vblank_get_stamp(tdm_vblank *vblank)
1715 tdm_private_vblank *private_vblank = vblank;
1717 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1719 return private_vblank->stamp;
1723 _tdm_vblank_get_waiting_count(tdm_private_vblank *private_vblank)
1725 tdm_vblank_wait_info *wait_info = NULL;
1726 unsigned int count = 0;
1728 LIST_FOR_EACH_ENTRY(wait_info, &private_vblank->HW_wait_list, link) {
1732 LIST_FOR_EACH_ENTRY(wait_info, &private_vblank->SW_wait_list, link) {
1740 _tdm_vblank_get_client_information(tdm_private_vblank *private_vblank, pid_t *pid, const char **name)
1742 struct wl_client *client;
1744 if (!private_vblank->resource)
1747 client = wl_resource_get_client(private_vblank->resource);
1751 wl_client_get_credentials(client, pid, NULL, NULL);
1752 *name = tdm_server_get_client_name(*pid);
1762 /* LCOV_EXCL_START */
1764 tdm_vblank_get_vblank_list_information(tdm_display *dpy, char *reply, int *len)
1766 tdm_private_vblank *v = NULL;
1767 tdm_output_dpms dpms = TDM_OUTPUT_DPMS_OFF;
1768 tdm_output *output = tdm_display_get_output(dpy, 0, NULL);
1771 tdm_output_get_dpms(output, &dpms);
1773 TDM_SNPRINTF(reply, len, "[Client Vblank List]\n");
1774 TDM_SNPRINTF(reply, len, "* DPMS: %s\n", tdm_dpms_str(dpms));
1775 TDM_SNPRINTF(reply, len, "* global fps: %u\n", vblank_global_fps);
1776 TDM_SNPRINTF(reply, len, "----------------------------------------------------------------------------------\n");
1777 TDM_SNPRINTF(reply, len, "Name Type Wait Interval Sequence Fps Ignore Offset Fake Process\n");
1778 TDM_SNPRINTF(reply, len, "----------------------------------------------------------------------------------\n");
1780 pthread_mutex_lock(&valid_list_lock);
1781 LIST_FOR_EACH_ENTRY(v, &valid_vblank_list, valid_link) {
1782 const char *proc_name = NULL;
1786 _tdm_vblank_get_client_information(v, &pid, &proc_name);
1788 count = _tdm_vblank_get_waiting_count(v);
1790 TDM_SNPRINTF(reply, len, "%-8s(%p) ", v->name, v);
1791 TDM_SNPRINTF(reply, len, "%-4s ", (v->last_type == VBLANK_EVENT_TYPE_HW) ? "HW" : "SW");
1792 TDM_SNPRINTF(reply, len, "%-4s ", (count > 0) ? "O" : " ");
1793 TDM_SNPRINTF(reply, len, "%-8d ", v->last_interval);
1794 TDM_SNPRINTF(reply, len, "%-8u ", v->last_seq);
1795 TDM_SNPRINTF(reply, len, "%-3u ", v->fps);
1796 TDM_SNPRINTF(reply, len, "%-6u ", v->ignore_global_fps);
1797 TDM_SNPRINTF(reply, len, "%-6d ", v->offset);
1798 TDM_SNPRINTF(reply, len, "%-4u ", v->enable_fake);
1799 TDM_SNPRINTF(reply, len, "%s(pid:%d)\n", (proc_name) ? proc_name : "Unknown", pid);
1801 pthread_mutex_unlock(&valid_list_lock);
1803 TDM_SNPRINTF(reply, len, "\n");
1805 /* LCOV_EXCL_STOP */