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 unsigned int hwc_commit_use_global_fps;
171 static double stamp = 0;
173 static void _tdm_vblank_cb_vblank_SW(tdm_private_display *private_display, void *object, tdm_thread_cb_base *cb_base, void *user_data);
174 static tdm_error _tdm_vblank_cb_timeout_SW(void *user_data);
175 static tdm_error _tdm_vblank_wait_SW(tdm_vblank_wait_info *wait_info);
176 static void _tdm_vblank_cb_vblank_HW(tdm_output *output, unsigned int sequence,
177 unsigned int tv_sec, unsigned int tv_usec,
179 static void _tdm_vblank_timeout_timer_update(tdm_private_vblank *private_vblank, int ms_delay);
180 static void _tdm_vblank_get_client_information(tdm_private_vblank *private_vblank,
181 pid_t *pid, const char **name);
185 _print_list(tdm_private_vblank *private_vblank)
187 tdm_vblank_wait_info *w = NULL;
190 LIST_FOR_EACH_ENTRY(w, &private_vblank->HW_wait_list, link) {
191 VIN("HW_wait(%p): type(%d) req_time(%.6f) target_time(%.6f) target_seq(%u)",
192 w, w->type, w->req_time, w->target_time, w->target_seq);
196 LIST_FOR_EACH_ENTRY(w, &private_vblank->SW_wait_list, link) {
197 VIN("SW_wait(%p): type(%d) req_time(%.6f) target_time(%.6f) target_seq(%u)",
198 w, w->type, w->req_time, w->target_time, w->target_seq);
208 tdm_vblank_is_valid(tdm_vblank *vblank)
210 tdm_private_vblank *v = NULL;
212 if (vblank && vblank_list_inited) {
213 pthread_mutex_lock(&valid_list_lock);
214 LIST_FOR_EACH_ENTRY(v, &valid_vblank_list, valid_link) {
215 if (v == (tdm_private_vblank *)vblank) {
216 pthread_mutex_unlock(&valid_list_lock);
220 pthread_mutex_unlock(&valid_list_lock);
227 _tdm_vblank_valid_list_add(struct list_head *valid_link, struct list_head *valid_list)
229 pthread_mutex_lock(&valid_list_lock);
230 LIST_ADDTAIL(valid_link, valid_list);
231 pthread_mutex_unlock(&valid_list_lock);
235 _tdm_vblank_valid_list_del(struct list_head *valid_link)
237 pthread_mutex_lock(&valid_list_lock);
238 LIST_DEL(valid_link);
239 pthread_mutex_unlock(&valid_list_lock);
243 _tdm_vblank_find_object(tdm_private_display *private_display, double vblank_stamp)
245 tdm_private_vblank *v = NULL;
247 pthread_mutex_lock(&valid_list_lock);
248 LIST_FOR_EACH_ENTRY(v, &valid_vblank_list, valid_link) {
249 if (v->stamp == vblank_stamp) {
250 pthread_mutex_unlock(&valid_list_lock);
254 pthread_mutex_unlock(&valid_list_lock);
259 static inline unsigned int
260 _tdm_vblank_check_valid_wait(tdm_vblank_wait_info *wait_info)
262 tdm_vblank_wait_info *w = NULL;
267 pthread_mutex_lock(&valid_list_lock);
268 LIST_FOR_EACH_ENTRY(w, &valid_wait_list, valid_link) {
269 if (w == wait_info) {
270 pthread_mutex_unlock(&valid_list_lock);
274 pthread_mutex_unlock(&valid_list_lock);
279 static inline unsigned int
280 _tdm_vblank_find_wait(tdm_vblank_wait_info *wait_info, struct list_head *list)
282 tdm_vblank_wait_info *w = NULL;
287 LIST_FOR_EACH_ENTRY(w, list, link) {
288 if (w->stamp == wait_info->stamp)
296 _tdm_vblank_insert_wait(tdm_vblank_wait_info *wait_info, struct list_head *list)
298 tdm_vblank_wait_info *w = NULL;
299 tdm_vblank_wait_info *found = NULL;
301 if (LIST_IS_EMPTY(list)) {
302 LIST_ADDTAIL(&wait_info->link, list);
306 LIST_FOR_EACH_ENTRY(w, list, link) {
307 if (wait_info->type == VBLANK_TYPE_SW) {
308 if (wait_info->target_time == 0)
309 TDM_NEVER_GET_HERE();
310 if (w->target_time <= wait_info->target_time) {
315 if (w->interval <= wait_info->interval) {
323 LIST_ADD(&wait_info->link, &found->link);
325 LIST_ADDTAIL(&wait_info->link, list);
329 _tdm_vblank_change_to_SW(tdm_private_vblank *private_vblank)
331 tdm_vblank_wait_info *w = NULL, *ww = NULL;
333 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->HW_wait_list, link) {
336 tdm_display_lock(private_vblank->dpy);
337 tdm_output_remove_vblank_handler_internal(private_vblank->output, _tdm_vblank_cb_vblank_HW, w);
338 tdm_display_unlock(private_vblank->dpy);
340 w->type = VBLANK_TYPE_SW_FAKE;
341 _tdm_vblank_wait_SW(w);
346 _tdm_vblank_free_HW_wait(tdm_private_vblank *private_vblank, tdm_error error, unsigned int call_cb)
348 tdm_vblank_wait_info *w = NULL, *ww = NULL;
350 if (!LIST_IS_EMPTY(&private_vblank->HW_wait_list)) {
352 const char *proc_name;
353 _tdm_vblank_get_client_information(private_vblank, &pid, &proc_name);
354 VWR("TDM HW vblank destroyed forcely!! (pid: %u, name: %s)", pid, proc_name);
357 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->HW_wait_list, link) {
359 _tdm_vblank_valid_list_del(&w->valid_link);
361 tdm_display_lock(private_vblank->dpy);
362 tdm_output_remove_vblank_handler_internal(private_vblank->output, _tdm_vblank_cb_vblank_HW, w);
363 tdm_display_unlock(private_vblank->dpy);
365 VDB("wait(%p) last(%.6f) sequence(%u) done (error:%d, call_cb:%u)",
366 w, (double)0.0, w->target_seq, error, call_cb);
368 if (call_cb && w->func)
369 w->func(private_vblank, error, 0, 0, 0, w->user_data);
376 _tdm_vblank_update_output_info(tdm_private_vblank *private_vblank)
378 tdm_output *output = private_vblank->output;
379 tdm_output_conn_status connection = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
380 unsigned int vrefresh = TDM_VBLANK_DEFAULT_VREFRESH;
381 const tdm_output_mode *mode = NULL;
384 ret = tdm_output_get_conn_status(output, &connection);
385 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
387 ret = tdm_output_get_mode(output, &mode);
388 if (ret == TDM_ERROR_NONE && mode)
389 vrefresh = mode->vrefresh;
391 VWR("mode not setted!!!");
393 private_vblank->connection = connection;
394 private_vblank->vrefresh = vrefresh;
395 private_vblank->HW_vblank_gap = 1.0 / private_vblank->vrefresh;
397 if (private_vblank->fps_changeable)
398 private_vblank->fps = vrefresh;
400 VIN("connection(%d) vrefresh(%d) fps(%d) fps_changeable(%d)",
401 private_vblank->connection,
402 private_vblank->vrefresh, private_vblank->fps, private_vblank->fps_changeable);
405 /* LCOV_EXCL_START */
407 _tdm_vblank_cb_output_change(tdm_output *output, tdm_output_change_type type,
408 tdm_value value, void *user_data)
410 tdm_private_vblank *private_vblank = user_data;
412 TDM_RETURN_IF_FAIL(tdm_vblank_is_valid(private_vblank));
415 case TDM_OUTPUT_CHANGE_DPMS:
416 VIN("dpms %s", tdm_dpms_str(value.u32));
417 private_vblank->check_HW_or_SW = 1;
418 if (TDM_OUTPUT_DPMS_VSYNC_IS_OFF(value.u32)) {
419 if (private_vblank->enable_fake)
420 _tdm_vblank_change_to_SW(private_vblank);
422 tdm_display_lock(private_vblank->dpy);
423 _tdm_vblank_timeout_timer_update(private_vblank, 0);
424 tdm_display_unlock(private_vblank->dpy);
426 _tdm_vblank_free_HW_wait(private_vblank, TDM_ERROR_DPMS_OFF, 1);
430 case TDM_OUTPUT_CHANGE_CONNECTION:
431 if (private_vblank->connection == value.u32)
433 VIN("output %s", tdm_status_str(value.u32));
434 _tdm_vblank_update_output_info(private_vblank);
435 private_vblank->check_HW_or_SW = 1;
436 if (private_vblank->connection == TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
437 if (private_vblank->enable_fake)
438 _tdm_vblank_change_to_SW(private_vblank);
440 tdm_display_lock(private_vblank->dpy);
441 _tdm_vblank_timeout_timer_update(private_vblank, 0);
442 tdm_display_unlock(private_vblank->dpy);
444 _tdm_vblank_free_HW_wait(private_vblank, TDM_ERROR_OUTPUT_DISCONNECTED, 1);
455 _tdm_vblank_set_fps(tdm_vblank *vblank, unsigned int fps)
457 tdm_private_vblank *private_vblank = vblank;
459 if (private_vblank->fps_fixed) {
460 VIN("fps(%u) can't be changed", private_vblank->fps);
461 return TDM_ERROR_NONE;
464 private_vblank->fps_changeable = 0;
466 if (private_vblank->fps == fps)
467 return TDM_ERROR_NONE;
469 private_vblank->fps = fps;
470 private_vblank->check_HW_or_SW = 1;
472 VIN("fps(%u) changed", fps);
474 return TDM_ERROR_NONE;
478 _tdm_vblank_ignore_global_fps(tdm_vblank *vblank, unsigned int ignore)
480 tdm_private_vblank *private_vblank = vblank;
482 if (private_vblank->ignore_global_fps == ignore)
483 return TDM_ERROR_NONE;
485 private_vblank->ignore_global_fps = ignore;
486 private_vblank->check_HW_or_SW = 1;
488 VIN("ignore_global_fps(%u)", private_vblank->ignore_global_fps);
490 return TDM_ERROR_NONE;
494 tdm_vblank_set_client_vblank_fps(unsigned int pid, const char *name, unsigned int fps)
496 tdm_private_vblank *v = NULL;
497 tdm_error ret = TDM_ERROR_NONE;
499 TDM_RETURN_VAL_IF_FAIL(pid > 0, TDM_ERROR_INVALID_PARAMETER);
500 TDM_RETURN_VAL_IF_FAIL(fps > 0, TDM_ERROR_INVALID_PARAMETER);
502 TDM_INFO("pid: %u, name: %s, fps: %d", pid, name, fps);
504 pthread_mutex_lock(&valid_list_lock);
505 LIST_FOR_EACH_ENTRY(v, &valid_vblank_list, valid_link) {
506 struct wl_client *client;
507 pid_t client_pid = 0;
512 client = wl_resource_get_client(v->resource);
516 wl_client_get_credentials(client, &client_pid, NULL, NULL);
518 if (client_pid != pid)
521 if (name && strncmp(name, TDM_VBLANK_DEFAULT_NAME, TDM_NAME_LEN)) {
522 if (strncmp(v->name, name, TDM_NAME_LEN))
526 ret = _tdm_vblank_set_fps(v, fps);
527 if (ret == TDM_ERROR_NONE)
528 TDM_INFO("(pid:%u) '%s' fps changed: %d", pid, v->name, fps);
530 TDM_ERR("(pid:%u) '%s' fps changing failed: %d", pid, v->name, fps);
532 pthread_mutex_unlock(&valid_list_lock);
534 return TDM_ERROR_NONE;
538 tdm_vblank_set_client_ignore_global_fps(unsigned int pid, const char *name, unsigned int ignore)
540 tdm_private_vblank *v = NULL;
541 tdm_error ret = TDM_ERROR_NONE;
543 TDM_RETURN_VAL_IF_FAIL(pid > 0, TDM_ERROR_INVALID_PARAMETER);
545 TDM_INFO("pid: %u, name: %s, ignore: %u", pid, name, ignore);
547 pthread_mutex_lock(&valid_list_lock);
548 LIST_FOR_EACH_ENTRY(v, &valid_vblank_list, valid_link) {
549 struct wl_client *client;
550 pid_t client_pid = 0;
555 client = wl_resource_get_client(v->resource);
559 wl_client_get_credentials(client, &client_pid, NULL, NULL);
561 if (client_pid != pid)
564 if (name && strncmp(name, TDM_VBLANK_DEFAULT_NAME, TDM_NAME_LEN)) {
565 if (strncmp(v->name, name, TDM_NAME_LEN))
569 ret = _tdm_vblank_ignore_global_fps(v, ignore);
570 if (ret == TDM_ERROR_NONE)
571 TDM_INFO("(pid:%u) '%s' ignore changed: %u", pid, v->name, ignore);
573 TDM_ERR("(pid:%u) '%s' ignore changing failed: %u", pid, v->name, ignore);
575 pthread_mutex_unlock(&valid_list_lock);
577 return TDM_ERROR_NONE;
581 tdm_vblank_enable_global_fps(unsigned int enable, unsigned int fps)
583 tdm_private_vblank *v = NULL;
586 TDM_RETURN_VAL_IF_FAIL(fps > 0, TDM_ERROR_INVALID_PARAMETER);
588 TDM_INFO("global fps: %s fps(%d)", (enable) ? "enabled" : "disabled", fps);
590 pthread_mutex_lock(&valid_list_lock);
592 vblank_global_fps = (enable) ? fps : 0;
594 LIST_FOR_EACH_ENTRY(v, &valid_vblank_list, valid_link) {
595 /* skip if vblank is created in server side */
599 v->check_HW_or_SW = 1;
602 if (hwc_commit_use_global_fps) {
604 tdm_hwc_set_vblank(fps);
606 tdm_hwc_unset_vblank();
609 pthread_mutex_unlock(&valid_list_lock);
611 return TDM_ERROR_NONE;
615 _tdm_vblank_thread_cb_create(tdm_private_display *private_display, void *object, tdm_thread_cb_base *cb_base, void *user_data)
617 tdm_thread_cb_vblank_create *vblank_create = (tdm_thread_cb_vblank_create *)cb_base;
618 tdm_vblank_create_handler_info *ch_info = user_data;
619 tdm_private_vblank *private_vblank;
621 TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
623 assert(ch_info != NULL);
625 private_vblank = _tdm_vblank_find_object(private_display, vblank_create->vblank_stamp);
626 if (!private_vblank) {
627 TDM_DBG("can't find vblank(%.0f) from valid_list", vblank_create->vblank_stamp);
631 private_vblank->in_create_handler = 1;
632 tdm_display_unlock(private_display);
633 ch_info->func(private_vblank, ch_info->user_data);
634 tdm_display_lock(private_display);
635 private_vblank->in_create_handler = 0;
639 _tdm_vblank_call_thread_cb(tdm_private_vblank *private_vblank)
641 tdm_thread_cb_vblank_create vblank_create;
644 memset(&vblank_create, 0, sizeof vblank_create);
645 vblank_create.base.type = TDM_THREAD_CB_VBLANK_CREATE;
646 vblank_create.base.length = sizeof vblank_create;
647 vblank_create.base.object_stamp = 1;
648 vblank_create.base.data = NULL;
649 vblank_create.base.sync = 0;
650 vblank_create.vblank_stamp = private_vblank->stamp;
652 ret = tdm_thread_cb_call(private_vblank->dpy, &vblank_create.base, 1);
653 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
655 return TDM_ERROR_NONE;
659 tdm_vblank_add_create_handler(tdm_display *dpy, tdm_vblank_create_handler func, void *user_data)
661 tdm_vblank_create_handler_info *ch_info = NULL;
664 TDM_RETURN_VAL_IF_FAIL(dpy != NULL, TDM_ERROR_INVALID_PARAMETER);
665 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
667 pthread_mutex_lock(&valid_list_lock);
669 LIST_FOR_EACH_ENTRY(ch_info, &create_handler_list, link) {
670 if (ch_info->func == func && ch_info->user_data == user_data) {
671 TDM_ERR("can't be added twice");
672 pthread_mutex_unlock(&valid_list_lock);
673 return TDM_ERROR_BAD_REQUEST;
677 ch_info = calloc(1, sizeof *ch_info);
679 TDM_ERR("alloc failed: %m");
680 pthread_mutex_unlock(&valid_list_lock);
681 return TDM_ERROR_OUT_OF_MEMORY;
684 tdm_display_lock(dpy);
685 ret = tdm_thread_cb_add(dpy, TDM_THREAD_CB_VBLANK_CREATE, NULL, _tdm_vblank_thread_cb_create, ch_info);
686 tdm_display_unlock(dpy);
688 if (ret != TDM_ERROR_NONE) {
689 TDM_ERR("tdm_thread_cb_add failed");
691 pthread_mutex_unlock(&valid_list_lock);
695 ch_info->func = func;
696 ch_info->user_data = user_data;
698 LIST_ADDTAIL(&ch_info->link, &create_handler_list);
700 pthread_mutex_unlock(&valid_list_lock);
702 return TDM_ERROR_NONE;
706 tdm_vblank_remove_create_handler(tdm_display *dpy, tdm_vblank_create_handler func, void *user_data)
708 tdm_vblank_create_handler_info *ch_info = NULL, *hh = NULL;
710 TDM_RETURN_IF_FAIL(dpy != NULL);
711 TDM_RETURN_IF_FAIL(func != NULL);
713 tdm_display_lock(dpy);
714 pthread_mutex_lock(&valid_list_lock);
715 LIST_FOR_EACH_ENTRY_SAFE(ch_info, hh, &create_handler_list, link) {
716 if (ch_info->func != func || ch_info->user_data != user_data)
719 tdm_thread_cb_remove(dpy, TDM_THREAD_CB_VBLANK_CREATE, NULL, _tdm_vblank_thread_cb_create, ch_info);
721 LIST_DEL(&ch_info->link);
723 pthread_mutex_unlock(&valid_list_lock);
724 tdm_display_unlock(dpy);
727 pthread_mutex_unlock(&valid_list_lock);
728 tdm_display_unlock(dpy);
732 _tdm_vblank_find_display(tdm_private_display *private_display, double stamp)
734 return private_display;
738 tdm_vblank_init(tdm_display *dpy)
740 if (vblank_list_inited)
741 return TDM_ERROR_NONE;
743 LIST_INITHEAD(&valid_vblank_list);
744 LIST_INITHEAD(&valid_wait_list);
745 LIST_INITHEAD(&create_handler_list);
747 tdm_thread_cb_set_find_func(TDM_THREAD_CB_VBLANK_SW, _tdm_vblank_find_object);
748 tdm_thread_cb_set_find_func(TDM_THREAD_CB_VBLANK_CREATE, _tdm_vblank_find_display);
750 hwc_commit_use_global_fps = tdm_config_get_int(TDM_CONFIG_KEY_GENERAL_HWC_COMMIT_USE_GLOBAL_FPS, 0);
752 vblank_list_inited = 1;
754 return TDM_ERROR_NONE;
758 tdm_vblank_deinit(tdm_display *dpy)
760 tdm_vblank_create_handler_info *ch_info = NULL, *hh = NULL;
762 if (!vblank_list_inited)
765 if (!LIST_IS_EMPTY(&valid_vblank_list))
766 TDM_NEVER_GET_HERE();
768 if (!LIST_IS_EMPTY(&valid_wait_list))
769 TDM_NEVER_GET_HERE();
771 LIST_FOR_EACH_ENTRY_SAFE(ch_info, hh, &create_handler_list, link) {
772 LIST_DEL(&ch_info->link);
776 vblank_list_inited = 0;
780 tdm_vblank_create(tdm_display *dpy, tdm_output *output, tdm_error *error)
782 tdm_private_vblank *private_vblank;
785 TDM_RETURN_VAL_IF_FAIL_WITH_ERROR(dpy != NULL, TDM_ERROR_INVALID_PARAMETER, NULL);
786 TDM_RETURN_VAL_IF_FAIL_WITH_ERROR(output != NULL, TDM_ERROR_INVALID_PARAMETER, NULL);
789 *error = TDM_ERROR_NONE;
791 private_vblank = calloc(1, sizeof * private_vblank);
792 if (!private_vblank) {
793 /* LCOV_EXCL_START */
795 *error = TDM_ERROR_OUT_OF_MEMORY;
796 TDM_ERR("alloc failed");
801 ret = tdm_output_add_change_handler(output, _tdm_vblank_cb_output_change, private_vblank);
802 if (ret != TDM_ERROR_NONE) {
803 free(private_vblank);
805 *error = TDM_ERROR_OPERATION_FAILED;
806 TDM_ERR("tdm_output_add_change_handler failed");
810 private_vblank->stamp = ++stamp;
811 private_vblank->owner_tid = syscall(SYS_gettid);
812 private_vblank->dpy = dpy;
813 private_vblank->output = output;
814 private_vblank->check_HW_or_SW = 1;
815 private_vblank->fps_changeable = 1;
816 private_vblank->last_interval = -1;
818 _tdm_vblank_update_output_info(private_vblank);
820 strncpy(private_vblank->name, TDM_VBLANK_DEFAULT_NAME, TDM_NAME_LEN - 1);
821 private_vblank->name[TDM_NAME_LEN - 1] = '\0';
823 LIST_INITHEAD(&private_vblank->HW_wait_list);
824 LIST_INITHEAD(&private_vblank->SW_wait_list);
826 _tdm_vblank_valid_list_add(&private_vblank->valid_link, &valid_vblank_list);
828 VIN("created. vrefresh(%d) connection(%d)",
829 private_vblank->vrefresh, private_vblank->connection);
831 tdm_display_lock(private_vblank->dpy);
832 _tdm_vblank_call_thread_cb(private_vblank);
833 tdm_display_unlock(private_vblank->dpy);
835 return (tdm_vblank *)private_vblank;
839 tdm_vblank_destroy(tdm_vblank *vblank)
841 tdm_private_vblank *private_vblank = vblank;
842 tdm_vblank_wait_info *w = NULL, *ww = NULL;
844 TDM_RETURN_IF_FAIL(tdm_vblank_is_valid(vblank));
846 if (private_vblank->in_create_handler) {
847 TDM_ERR("NOT allowed to be called in a create handler");
851 _tdm_vblank_valid_list_del(&private_vblank->valid_link);
853 if (private_vblank->SW_timer) {
854 tdm_display_lock(private_vblank->dpy);
855 tdm_thread_cb_remove(private_vblank, TDM_THREAD_CB_VBLANK_SW, NULL, _tdm_vblank_cb_vblank_SW, NULL);
856 tdm_event_loop_source_remove(private_vblank->SW_timer);
857 tdm_display_unlock(private_vblank->dpy);
860 tdm_output_remove_change_handler(private_vblank->output,
861 _tdm_vblank_cb_output_change, private_vblank);
863 _tdm_vblank_free_HW_wait(private_vblank, 0, 0);
865 if (!LIST_IS_EMPTY(&private_vblank->SW_wait_list)) {
867 const char *proc_name;
868 _tdm_vblank_get_client_information(private_vblank, &pid, &proc_name);
869 VER("TDM SW vblank destroyed forcely!! (pid: %u, name: %s)", pid, proc_name);
872 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->SW_wait_list, link) {
874 _tdm_vblank_valid_list_del(&w->valid_link);
878 if (private_vblank->timeout_timer) {
879 tdm_display_lock(private_vblank->dpy);
880 tdm_event_loop_source_remove(private_vblank->timeout_timer);
881 tdm_display_unlock(private_vblank->dpy);
886 free(private_vblank);
890 tdm_vblank_get_output(tdm_vblank *vblank, tdm_error *error)
892 tdm_private_vblank *private_vblank = vblank;
893 tdm_error ret = TDM_ERROR_NONE;
895 TDM_RETURN_VAL_IF_FAIL_WITH_ERROR(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER, NULL);
898 *error = TDM_ERROR_NONE;
900 return private_vblank->output;
904 tdm_vblank_get_client_pid(tdm_vblank *vblank, pid_t *pid)
906 tdm_private_vblank *private_vblank = vblank;
907 struct wl_client *client;
909 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
910 TDM_RETURN_VAL_IF_FAIL(pid != NULL, TDM_ERROR_INVALID_PARAMETER);
912 if (!private_vblank->resource) {
914 return TDM_ERROR_NONE;
917 client = wl_resource_get_client(private_vblank->resource);
920 return TDM_ERROR_NONE;
923 wl_client_get_credentials(client, pid, NULL, NULL);
925 return TDM_ERROR_NONE;
929 tdm_vblank_set_name(tdm_vblank *vblank, const char *name)
931 tdm_private_vblank *private_vblank = vblank;
933 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
936 name = TDM_VBLANK_DEFAULT_NAME;
938 strncpy(private_vblank->name, name, TDM_NAME_LEN - 1);
939 private_vblank->name[TDM_NAME_LEN - 1] = '\0';
941 VIN("name(%s)", name);
943 return TDM_ERROR_NONE;
947 tdm_vblank_get_name(tdm_vblank *vblank, const char **name)
949 tdm_private_vblank *private_vblank = vblank;
951 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
952 TDM_RETURN_VAL_IF_FAIL(name != NULL, TDM_ERROR_INVALID_PARAMETER);
954 *name = (const char*)private_vblank->name;
956 return TDM_ERROR_NONE;
960 tdm_vblank_set_fps(tdm_vblank *vblank, unsigned int fps)
962 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
963 TDM_RETURN_VAL_IF_FAIL(fps > 0, TDM_ERROR_INVALID_PARAMETER);
965 return _tdm_vblank_set_fps(vblank, fps);
969 tdm_vblank_get_fps(tdm_vblank *vblank, unsigned int *fps)
971 tdm_private_vblank *private_vblank = vblank;
973 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
974 TDM_RETURN_VAL_IF_FAIL(fps != NULL, TDM_ERROR_INVALID_PARAMETER);
976 *fps = private_vblank->fps;
978 return TDM_ERROR_NONE;
982 tdm_vblank_set_fixed_fps(tdm_vblank *vblank, unsigned int fps)
984 tdm_private_vblank *private_vblank = vblank;
986 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
987 TDM_RETURN_VAL_IF_FAIL(fps > 0, TDM_ERROR_INVALID_PARAMETER);
989 private_vblank->fps_changeable = 0;
990 private_vblank->fps_fixed = 1;
992 if (private_vblank->fps == fps)
993 return TDM_ERROR_NONE;
995 private_vblank->fps = fps;
996 private_vblank->check_HW_or_SW = 1;
998 VIN("fps(%u) fixed", fps);
1000 return TDM_ERROR_NONE;
1004 tdm_vblank_ignore_global_fps(tdm_vblank *vblank, unsigned int ignore)
1006 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1008 return _tdm_vblank_ignore_global_fps(vblank, ignore);
1012 tdm_vblank_set_offset(tdm_vblank *vblank, int offset)
1014 tdm_private_vblank *private_vblank = vblank;
1016 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1017 TDM_RETURN_VAL_IF_FAIL(offset >= 0, TDM_ERROR_INVALID_PARAMETER);
1019 if (private_vblank->offset == offset)
1020 return TDM_ERROR_NONE;
1022 private_vblank->offset = offset;
1023 private_vblank->check_HW_or_SW = 1;
1025 VIN("offset(%u)", private_vblank->offset);
1027 return TDM_ERROR_NONE;
1031 tdm_vblank_get_offset(tdm_vblank *vblank, int *offset)
1033 tdm_private_vblank *private_vblank = vblank;
1035 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1036 TDM_RETURN_VAL_IF_FAIL(offset != NULL, TDM_ERROR_INVALID_PARAMETER);
1038 *offset = private_vblank->offset;
1040 return TDM_ERROR_NONE;
1044 tdm_vblank_set_enable_fake(tdm_vblank *vblank, unsigned int enable_fake)
1046 tdm_private_vblank *private_vblank = vblank;
1048 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1050 if (private_vblank->enable_fake == enable_fake)
1051 return TDM_ERROR_NONE;
1053 private_vblank->enable_fake = enable_fake;
1055 VIN("enable_fake(%u)", private_vblank->enable_fake);
1057 return TDM_ERROR_NONE;
1061 tdm_vblank_get_enable_fake(tdm_vblank *vblank, unsigned int *enable_fake)
1063 tdm_private_vblank *private_vblank = vblank;
1065 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1066 TDM_RETURN_VAL_IF_FAIL(enable_fake != NULL, TDM_ERROR_INVALID_PARAMETER);
1068 *enable_fake = private_vblank->enable_fake;
1070 return TDM_ERROR_NONE;
1074 _tdm_vblank_timeout_timer_cb(void *user_data)
1076 tdm_private_vblank *private_vblank = user_data;
1077 tdm_vblank_wait_info *w = NULL;
1079 const char *proc_name;
1081 TDM_RETURN_VAL_IF_FAIL(private_vblank != NULL, TDM_ERROR_OPERATION_FAILED);
1083 _tdm_vblank_get_client_information(private_vblank, &pid, &proc_name);
1085 VER("TDM vblank TIMEOUT!! (pid: %u, name: %s)", pid, proc_name);
1086 VER("quotient(%u) last_seq(%u) last_time(%.6f)", private_vblank->quotient, private_vblank->last_seq, private_vblank->last_time);
1088 LIST_FOR_EACH_ENTRY(w, &private_vblank->HW_wait_list, link) {
1089 VER("wait_info(%p) req_time(%.6f) target_seq(%u) interval(%u) hw_interval(%d)",
1090 w, w->req_time, w->target_seq, w->interval, w->hw_interval);
1091 tdm_output_vblank_print_wait_information(private_vblank->output, w);
1094 LIST_FOR_EACH_ENTRY(w, &private_vblank->SW_wait_list, link) {
1095 VER("wait_info(%p) req_time(%.6f) target_time(%.6f) target_seq(%u) interval(%u)",
1096 w, w->req_time, w->target_time, w->target_seq, w->interval);
1099 return TDM_ERROR_NONE;
1103 _tdm_vblank_timeout_timer_update(tdm_private_vblank *private_vblank, int ms_delay)
1107 if (!private_vblank->timeout_timer) {
1108 private_vblank->timeout_timer =
1109 tdm_event_loop_add_timer_handler(private_vblank->dpy,
1110 _tdm_vblank_timeout_timer_cb,
1113 if (!private_vblank->timeout_timer) {
1114 VER("couldn't add timer");
1117 VIN("Create Timeout timer");
1120 ret = tdm_event_loop_source_timer_update(private_vblank->timeout_timer, ms_delay);
1121 if (ret != TDM_ERROR_NONE) {
1122 VER("couldn't update timer");
1127 VDB("timeout timer updated");
1129 VDB("timeout timer disabled");
1133 _tdm_vblank_sw_timer_update(tdm_private_vblank *private_vblank)
1135 tdm_vblank_wait_info *first_wait_info = NULL;
1136 double curr, target;
1140 if (LIST_IS_EMPTY(&private_vblank->SW_wait_list)) {
1141 VER("no wait_info");
1142 return TDM_ERROR_OPERATION_FAILED;
1145 first_wait_info = container_of(private_vblank->SW_wait_list.next, first_wait_info, link);
1146 curr = tdm_helper_get_time();
1147 target = first_wait_info->target_time;
1149 /* ms_delay should be more that 1 */
1153 ms_delay = ceil((target - curr) * 1000.0);
1158 if (ms_delay > 5000)
1159 VER("wait(%p) curr(%.6f) target(%.6f) ms_delay(%d)", first_wait_info, curr, target, ms_delay);
1161 VDB("wait(%p) curr(%.6f) target(%.6f) ms_delay(%d)", first_wait_info, curr, target, ms_delay);
1163 tdm_display_lock(private_vblank->dpy);
1165 if (!private_vblank->SW_timer) {
1166 private_vblank->SW_timer =
1167 tdm_event_loop_add_timer_handler(private_vblank->dpy,
1168 _tdm_vblank_cb_timeout_SW,
1171 if (!private_vblank->SW_timer) {
1172 tdm_display_unlock(private_vblank->dpy);
1173 VER("couldn't add timer");
1177 ret = tdm_thread_cb_add(private_vblank, TDM_THREAD_CB_VBLANK_SW, NULL, _tdm_vblank_cb_vblank_SW, NULL);
1178 if (ret != TDM_ERROR_NONE) {
1179 tdm_event_loop_source_remove(private_vblank->SW_timer);
1180 private_vblank->SW_timer = NULL;
1181 tdm_display_unlock(private_vblank->dpy);
1182 VER("couldn't add thread_cb");
1186 VIN("Create SW timer");
1189 private_vblank->last_interval = ms_delay;
1191 ret = tdm_event_loop_source_timer_update(private_vblank->SW_timer, ms_delay);
1192 if (ret != TDM_ERROR_NONE) {
1193 /* LCOV_EXCL_START */
1194 tdm_display_unlock(private_vblank->dpy);
1195 VER("couldn't update timer");
1197 /* LCOV_EXCL_STOP */
1200 tdm_display_unlock(private_vblank->dpy);
1202 return TDM_ERROR_NONE;
1206 _tdm_vblank_cb_vblank_HW(tdm_output *output, unsigned int sequence,
1207 unsigned int tv_sec, unsigned int tv_usec,
1210 tdm_vblank_wait_info *wait_info = user_data;
1211 tdm_private_vblank *private_vblank;
1213 if (!_tdm_vblank_check_valid_wait(wait_info)) {
1214 TDM_ERR("can't find wait(%p) from valid_wait_list", wait_info);
1218 private_vblank = wait_info->private_vblank;
1219 TDM_RETURN_IF_FAIL(tdm_vblank_is_valid(private_vblank));
1221 assert(private_vblank->owner_tid == syscall(SYS_gettid));
1223 if (!_tdm_vblank_find_wait(wait_info, &private_vblank->HW_wait_list)) {
1224 VER("can't find wait(%p)", wait_info);
1228 if (wait_info->type == VBLANK_TYPE_HW_SW) {
1231 LIST_DEL(&wait_info->link);
1233 /* kernel sends the wrong time of first vblank event. and the time of
1234 * second vblank is correct.
1236 wait_info->target_time = TDM_TIME(tv_sec, tv_usec);
1237 wait_info->target_time += (((double)private_vblank->offset) / 1000.0);
1239 _tdm_vblank_insert_wait(wait_info, &private_vblank->SW_wait_list);
1241 ret = _tdm_vblank_sw_timer_update(private_vblank);
1243 /* wait_info will be freed in _tdm_vblank_cb_timeout_SW() */
1244 if (ret == TDM_ERROR_NONE) {
1245 VIN("wait(%p) SW timer", wait_info);
1249 VER("couldn't update sw timer");
1252 tdm_display_lock(private_vblank->dpy);
1253 _tdm_vblank_timeout_timer_update(private_vblank, 0);
1254 tdm_display_unlock(private_vblank->dpy);
1256 LIST_DEL(&wait_info->link);
1257 _tdm_vblank_valid_list_del(&wait_info->valid_link);
1259 /* tv_sec & tv_usec shouldn't be zero. But sometimes they are zero in some hardware.
1260 * We need to prohibit that last_time becomes zero because "last_time == 0" means
1261 * that a tdm_vblank object is just created and doesn't have the first vblank event.
1262 * If last_time becomes 0, it would make issue.
1264 if (tv_sec == 0 && tv_usec == 0) {
1265 double curr = tdm_helper_get_time();
1266 tv_sec = TDM_TIME_SEC(curr);
1267 tv_usec = TDM_TIME_USEC(curr);
1270 private_vblank->last_seq = wait_info->target_seq;
1271 private_vblank->last_time = TDM_TIME(tv_sec, tv_usec);
1273 VDB("wait(%p) last(%.6f) sequence(%u) done", wait_info, private_vblank->last_time, wait_info->target_seq);
1275 if (tdm_ttrace_module & TDM_TTRACE_VBLANK)
1276 TDM_TRACE_ASYNC_END((int)wait_info->req_time, "TDM_Vblank:%u", (unsigned int)private_vblank->stamp);
1278 if (wait_info->func)
1279 wait_info->func(private_vblank, TDM_ERROR_NONE, wait_info->target_seq,
1280 tv_sec, tv_usec, wait_info->user_data);
1286 _tdm_vblank_wait_HW(tdm_vblank_wait_info *wait_info)
1288 tdm_private_vblank *private_vblank = wait_info->private_vblank;
1292 if (private_vblank->last_type != VBLANK_EVENT_TYPE_HW) {
1293 VIN("Use HW vblank");
1294 private_vblank->last_type = VBLANK_EVENT_TYPE_HW;
1297 _tdm_vblank_insert_wait(wait_info, &private_vblank->HW_wait_list);
1299 if (private_vblank->last_time == 0) {
1300 hw_interval = wait_info->interval * private_vblank->quotient;
1302 /* SW vblank starts from now. SW vblank doesn't need to be aligned with HW vblank. */
1303 private_vblank->last_seq = 0;
1304 private_vblank->last_time = tdm_helper_get_time();
1306 /* +1 ms to call the handler ASAP at the first. no matter for SW timer. */
1307 wait_info->target_seq = 1;
1309 VDB("wait(%p) last(%.6f) hw_itvl(%d) targ_seq(%u)",
1310 wait_info, private_vblank->last_time, hw_interval, wait_info->target_seq);
1312 double last, prev, req, curr, target;
1315 last = private_vblank->last_time;
1316 req = wait_info->req_time;
1318 /* make sure that req time is bigger than last. */
1322 skip = (unsigned int)((req - last) / private_vblank->vblank_gap + TDM_TIME_MARGIN);
1323 prev = last + private_vblank->vblank_gap * skip;
1325 curr = tdm_helper_get_time();
1327 while (target < curr)
1328 target += (private_vblank->vblank_gap * wait_info->interval);
1330 hw_interval = (unsigned int)((target - curr) / private_vblank->HW_vblank_gap + TDM_TIME_MARGIN) + 1;
1332 wait_info->target_seq = private_vblank->last_seq;
1333 wait_info->target_seq += (unsigned int)((target - last) / private_vblank->vblank_gap + TDM_TIME_MARGIN);
1335 if (hw_interval > 300)
1336 VER("wait(%p) last(%.6f) req(%.6f) prev(%.6f) curr(%.6f) targ(%.6f,%.6f) skip(%u) hw_itvl(%d) targ_seq(%u)",
1337 wait_info, last, req - last, prev - last, curr - last, target, target - last,
1338 skip, hw_interval, wait_info->target_seq);
1340 VDB("wait(%p) last(%.6f) req(%.6f) prev(%.6f) curr(%.6f) targ(%.6f,%.6f) skip(%u) hw_itvl(%d) targ_seq(%u)",
1341 wait_info, last, req - last, prev - last, curr - last, target, target - last,
1342 skip, hw_interval, wait_info->target_seq);
1345 private_vblank->last_interval = hw_interval;
1346 wait_info->hw_interval = hw_interval;
1348 if (tdm_ttrace_module & TDM_TTRACE_VBLANK)
1349 TDM_TRACE_ASYNC_BEGIN((int)wait_info->req_time, "TDM_Vblank:%u", (unsigned int)private_vblank->stamp);
1351 if (private_vblank->add_front)
1352 ret = tdm_output_wait_vblank_add_front(private_vblank->output, hw_interval, 0,
1353 _tdm_vblank_cb_vblank_HW, wait_info);
1355 ret = tdm_output_wait_vblank(private_vblank->output, hw_interval, 0,
1356 _tdm_vblank_cb_vblank_HW, wait_info);
1358 if (ret != TDM_ERROR_NONE) {
1359 LIST_DEL(&wait_info->link);
1363 VDB("wait(%p) waiting", wait_info);
1365 return TDM_ERROR_NONE;
1369 _tdm_vblank_cb_vblank_SW(tdm_private_display *private_display, void *object, tdm_thread_cb_base *cb_base, void *user_data)
1371 tdm_private_vblank *private_vblank = object;
1372 tdm_vblank_wait_info *first_wait_info = NULL, *w = NULL, *ww = NULL;
1374 TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
1376 assert(private_vblank->owner_tid == syscall(SYS_gettid));
1378 if (LIST_IS_EMPTY(&private_vblank->SW_wait_list)) {
1379 VER("no wait_info");
1383 if (!LIST_IS_EMPTY(&private_vblank->HW_wait_list))
1384 TDM_NEVER_GET_HERE();
1386 first_wait_info = container_of(private_vblank->SW_wait_list.next, first_wait_info, link);
1387 TDM_RETURN_IF_FAIL(first_wait_info != NULL);
1389 _tdm_vblank_timeout_timer_update(private_vblank, 0);
1391 if (private_vblank->last_seq >= first_wait_info->target_seq)
1392 TDM_ERR("last_seq(%u) target_seq(%u)", private_vblank->last_seq, first_wait_info->target_seq);
1394 private_vblank->last_seq = first_wait_info->target_seq;
1395 private_vblank->last_time = first_wait_info->target_time;
1397 VDB("wait(%p) last(%.6f) sequence(%u) done", first_wait_info, private_vblank->last_time, first_wait_info->target_seq);
1399 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->SW_wait_list, link) {
1400 if (w->target_time != first_wait_info->target_time)
1404 _tdm_vblank_valid_list_del(&w->valid_link);
1407 tdm_display_unlock(private_vblank->dpy);
1408 w->func(private_vblank, TDM_ERROR_NONE, w->target_seq,
1409 TDM_TIME_SEC(w->target_time), TDM_TIME_USEC(w->target_time),
1411 tdm_display_lock(private_vblank->dpy);
1419 _tdm_vblank_cb_timeout_SW(void *user_data)
1421 tdm_private_vblank *private_vblank = user_data;
1422 tdm_thread_cb_vblank_sw vblank_sw;
1425 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(private_vblank), TDM_ERROR_OPERATION_FAILED);
1427 VIN("timeout_SW -> thread-cb");
1429 memset(&vblank_sw, 0, sizeof vblank_sw);
1430 vblank_sw.base.type = TDM_THREAD_CB_VBLANK_SW;
1431 vblank_sw.base.length = sizeof vblank_sw;
1432 vblank_sw.base.object_stamp = private_vblank->stamp;
1433 vblank_sw.base.data = NULL;
1434 vblank_sw.base.sync = 0;
1436 ret = tdm_thread_cb_call(private_vblank, &vblank_sw.base, 1);
1437 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
1439 return TDM_ERROR_NONE;
1443 _tdm_vblank_wait_SW(tdm_vblank_wait_info *wait_info)
1445 tdm_private_vblank *private_vblank = wait_info->private_vblank;
1447 double last, prev, req, curr, target;
1450 if (private_vblank->last_type != VBLANK_EVENT_TYPE_SW) {
1451 tdm_output_dpms dpms;
1453 tdm_output_get_dpms(private_vblank->output, &dpms);
1455 VIN("Use SW vblank. connection(%s), dpms(%s), vrefresh(%d), fps(%d), offset(%d)",
1456 tdm_status_str(private_vblank->connection), tdm_dpms_str(dpms),
1457 private_vblank->vrefresh, private_vblank->fps, private_vblank->offset);
1458 private_vblank->last_type = VBLANK_EVENT_TYPE_SW;
1461 if (private_vblank->last_time == 0) {
1462 /* SW vblank starts from now. SW vblank doesn't need to be aligned with HW vblank. */
1463 private_vblank->last_seq = 0;
1464 private_vblank->last_time = tdm_helper_get_time() - private_vblank->vblank_gap;
1466 /* +1 ms to call the handler ASAP at the first. no matter for SW timer. */
1467 wait_info->target_seq = 1;
1470 last = private_vblank->last_time;
1471 req = wait_info->req_time;
1473 /* make sure that req time is bigger than last. */
1477 skip = (unsigned int)((req - last) / private_vblank->vblank_gap + TDM_TIME_MARGIN);
1478 prev = last + private_vblank->vblank_gap * skip;
1480 curr = tdm_helper_get_time();
1481 target = prev + private_vblank->vblank_gap * wait_info->interval;
1483 while (target < curr)
1484 target += private_vblank->vblank_gap;
1486 wait_info->target_seq = private_vblank->last_seq;
1487 wait_info->target_seq += (unsigned int)((target - last) / private_vblank->vblank_gap + TDM_TIME_MARGIN);
1489 wait_info->target_time = target + (double)(private_vblank->offset) / 1000;
1491 VDB("wait(%p) last(%.6f) req(%.6f) prev(%.6f) curr(%.6f) targ(%.6f,%.6f) skip(%u) targ_seq(%u)",
1492 wait_info, last, req - last, prev - last, curr - last,
1493 target, target - last, skip, wait_info->target_seq);
1495 _tdm_vblank_insert_wait(wait_info, &private_vblank->SW_wait_list);
1497 ret = _tdm_vblank_sw_timer_update(private_vblank);
1498 if (ret != TDM_ERROR_NONE) {
1499 /* LCOV_EXCL_START */
1500 LIST_DEL(&wait_info->link);
1501 VER("couldn't update sw timer");
1503 /* LCOV_EXCL_STOP */
1506 return TDM_ERROR_NONE;
1510 tdm_vblank_wait(tdm_vblank *vblank, unsigned int req_sec, unsigned int req_usec,
1511 unsigned int interval, tdm_vblank_handler func, void *user_data)
1513 tdm_private_vblank *private_vblank = vblank;
1514 tdm_vblank_wait_info *wait_info;
1515 tdm_output_dpms dpms = TDM_OUTPUT_DPMS_OFF;
1517 int dpms_changing = 0;
1520 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1521 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
1522 TDM_RETURN_VAL_IF_FAIL(interval > 0, TDM_ERROR_INVALID_PARAMETER);
1524 if (private_vblank->in_create_handler) {
1525 TDM_ERR("NOT allowed to be called in a create handler");
1526 return TDM_ERROR_BAD_REQUEST;
1529 if (private_vblank->owner_tid != syscall(SYS_gettid)) {
1530 TDM_ERR("SHOULD be called in the owner thread(%d)", (int)private_vblank->owner_tid);
1531 return TDM_ERROR_BAD_REQUEST;
1534 tdm_output_get_dpms(private_vblank->output, &dpms);
1535 tdm_output_get_dpms_changing(private_vblank->output, &dpms_changing);
1537 if (!private_vblank->enable_fake) {
1538 if (private_vblank->connection == TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
1539 VER("can't wait a vblank: output disconnected");
1540 return TDM_ERROR_OUTPUT_DISCONNECTED;
1542 if (TDM_OUTPUT_DPMS_VSYNC_IS_OFF(dpms)) {
1543 VER("can't wait a vblank: DPMS %s", tdm_dpms_str(dpms));
1544 return TDM_ERROR_DPMS_OFF;
1547 if (dpms_changing) {
1548 VER("can't wait a vblank: DPMS is changing");
1549 return TDM_ERROR_DPMS_OFF;
1553 wait_info = calloc(1, sizeof * wait_info);
1555 /* LCOV_EXCL_START */
1556 VER("alloc failed");
1557 return TDM_ERROR_OUT_OF_MEMORY;
1558 /* LCOV_EXCL_STOP */
1560 _tdm_vblank_update_output_info(private_vblank);
1562 /* set request time to current time if 0. This function seems to be called in server side. */
1563 if (req_sec == 0 && req_usec == 0) {
1564 double curr = tdm_helper_get_time();
1565 req_sec = TDM_TIME_SEC(curr);
1566 req_usec = TDM_TIME_USEC(curr);
1569 LIST_INITHEAD(&wait_info->link);
1571 _tdm_vblank_valid_list_add(&wait_info->valid_link, &valid_wait_list);
1573 wait_info->stamp = ++stamp;
1574 wait_info->req_time = TDM_TIME(req_sec, req_usec);
1575 wait_info->interval = interval;
1576 wait_info->func = func;
1577 wait_info->user_data = user_data;
1578 wait_info->private_vblank = private_vblank;
1580 if (!private_vblank->resource ||
1581 private_vblank->ignore_global_fps ||
1582 vblank_global_fps == 0 ||
1583 private_vblank->fps < vblank_global_fps)
1584 fps = private_vblank->fps;
1586 fps = vblank_global_fps;
1588 if (private_vblank->check_HW_or_SW) {
1589 private_vblank->check_HW_or_SW = 0;
1590 private_vblank->vblank_gap = 1.0 / fps;
1591 private_vblank->quotient = private_vblank->vrefresh / fps;
1594 /* 1) if fps != factor of vrefresh, SW timer
1595 * 2) if fps == factor of vrefresh && dpms == off, SW timer (Fake HW vblank)
1596 * 3) if fps == factor of vrefresh && dpms == on && offset == 0, HW vblank
1597 * 4) if fps == factor of vrefresh && dpms == on && offset != 0, HW vblank + SW timer
1598 * In case of 1), we really don't need to align with HW vblank.
1600 if (private_vblank->vrefresh % fps)
1601 wait_info->type = VBLANK_TYPE_SW;
1602 else if (TDM_OUTPUT_DPMS_VSYNC_IS_OFF(dpms) || dpms_changing ||
1603 private_vblank->connection != TDM_OUTPUT_CONN_STATUS_MODE_SETTED)
1604 wait_info->type = VBLANK_TYPE_SW_FAKE;
1605 else if (private_vblank->offset == 0)
1606 wait_info->type = VBLANK_TYPE_HW;
1608 wait_info->type = VBLANK_TYPE_HW_SW;
1610 if (wait_info->type == VBLANK_TYPE_SW || wait_info->type == VBLANK_TYPE_SW_FAKE) {
1611 ret = _tdm_vblank_wait_SW(wait_info);
1612 if (ret != TDM_ERROR_NONE)
1613 TDM_ERR("_tdm_vblank_wait_SW failed: ret(%d)", ret);
1615 ret = _tdm_vblank_wait_HW(wait_info);
1616 if (ret != TDM_ERROR_NONE)
1617 TDM_ERR("_tdm_vblank_wait_HW failed: ret(%d)", ret);
1619 if (ret == TDM_ERROR_OUTPUT_DISCONNECTED || ret == TDM_ERROR_DPMS_OFF) {
1620 ret = _tdm_vblank_wait_SW(wait_info);
1621 if (ret != TDM_ERROR_NONE)
1622 TDM_ERR("_tdm_vblank_wait_SW failed: ret(%d)", ret);
1626 if (ret != TDM_ERROR_NONE) {
1627 /* LCOV_EXCL_START */
1628 LIST_DEL(&wait_info->link);
1629 _tdm_vblank_valid_list_del(&wait_info->valid_link);
1631 tdm_display_lock(private_vblank->dpy);
1632 tdm_output_remove_vblank_handler_internal(private_vblank->output, _tdm_vblank_cb_vblank_HW, wait_info);
1633 tdm_display_unlock(private_vblank->dpy);
1637 /* LCOV_EXCL_STOP */
1640 tdm_display_lock(private_vblank->dpy);
1641 _tdm_vblank_timeout_timer_update(private_vblank, 1000);
1642 tdm_display_unlock(private_vblank->dpy);
1644 return TDM_ERROR_NONE;
1648 tdm_vblank_wait_seq(tdm_vblank *vblank, unsigned int req_sec, unsigned int req_usec,
1649 unsigned int sequence, tdm_vblank_handler func, void *user_data)
1651 tdm_private_vblank *private_vblank = vblank;
1652 unsigned int interval;
1654 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1655 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
1657 /* if the sequence of vblank reaches the max value, sequence can be 0. */
1659 if (private_vblank->last_seq == 0) {
1660 VWR("can't calculate interval with sequence(%u)", sequence);
1662 } else if (sequence > private_vblank->last_seq ||
1663 TDM_VBLANK_SEQ_REACHED_MAX(sequence, private_vblank->last_seq)) {
1664 double last, curr, seq_target;
1666 last = private_vblank->last_time;
1667 curr = tdm_helper_get_time();
1668 seq_target = last + private_vblank->vblank_gap * (sequence - private_vblank->last_seq);
1670 /* If target is old or too close(1ms) to current, we call the handler immediately.
1671 * 2ms? it seems too much.
1673 if (seq_target < curr) {
1674 func(vblank, TDM_ERROR_NONE, sequence,
1675 TDM_TIME_SEC(seq_target), TDM_TIME_USEC(seq_target),
1677 return TDM_ERROR_NONE;
1678 } else if ((seq_target - curr) < 0.001) { /* 1ms */
1679 func(vblank, TDM_ERROR_NONE, sequence,
1680 TDM_TIME_SEC(curr), TDM_TIME_USEC(curr),
1682 return TDM_ERROR_NONE;
1685 interval = (unsigned int)((seq_target - curr) / private_vblank->vblank_gap + TDM_TIME_MARGIN) + 1;
1687 /* this seems error. But we handle this like normal to avoid the unexpected error in cliend side */
1688 VER("sequence(%u) should be over the last sequence(%u)", sequence, private_vblank->last_seq);
1689 func(vblank, TDM_ERROR_NONE, private_vblank->last_seq,
1690 TDM_TIME_SEC(private_vblank->last_time), TDM_TIME_USEC(private_vblank->last_time),
1692 return TDM_ERROR_NONE;
1695 return tdm_vblank_wait(vblank, req_sec, req_usec, interval, func, user_data);
1698 /* LCOV_EXCL_START */
1700 tdm_vblank_set_add_front(tdm_vblank *vblank, unsigned int add_front)
1702 tdm_private_vblank *private_vblank = vblank;
1704 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1706 TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
1708 if (private_vblank->add_front == add_front)
1709 return TDM_ERROR_NONE;
1711 private_vblank->add_front = add_front;
1713 return TDM_ERROR_NONE;
1715 /* LCOV_EXCL_STOP */
1718 tdm_vblank_set_resource(tdm_vblank *vblank, struct wl_resource *resource)
1720 tdm_private_vblank *private_vblank = vblank;
1722 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1723 TDM_RETURN_VAL_IF_FAIL(private_vblank->resource == NULL, TDM_ERROR_OPERATION_FAILED);
1725 private_vblank->resource = resource;
1727 return TDM_ERROR_NONE;
1731 tdm_vblank_get_stamp(tdm_vblank *vblank)
1733 tdm_private_vblank *private_vblank = vblank;
1735 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1737 return private_vblank->stamp;
1741 _tdm_vblank_get_waiting_count(tdm_private_vblank *private_vblank)
1743 tdm_vblank_wait_info *wait_info = NULL;
1744 unsigned int count = 0;
1746 LIST_FOR_EACH_ENTRY(wait_info, &private_vblank->HW_wait_list, link) {
1750 LIST_FOR_EACH_ENTRY(wait_info, &private_vblank->SW_wait_list, link) {
1758 _tdm_vblank_get_client_information(tdm_private_vblank *private_vblank, pid_t *pid, const char **name)
1760 struct wl_client *client;
1762 if (!private_vblank->resource)
1765 client = wl_resource_get_client(private_vblank->resource);
1769 wl_client_get_credentials(client, pid, NULL, NULL);
1770 *name = tdm_server_get_client_name(*pid);
1780 /* LCOV_EXCL_START */
1782 tdm_vblank_get_vblank_list_information(tdm_display *dpy, char *reply, int *len)
1784 tdm_private_vblank *v = NULL;
1785 tdm_output_dpms dpms = TDM_OUTPUT_DPMS_OFF;
1786 tdm_output *output = tdm_display_get_output(dpy, 0, NULL);
1789 tdm_output_get_dpms(output, &dpms);
1791 TDM_SNPRINTF(reply, len, "[Client Vblank List]\n");
1792 TDM_SNPRINTF(reply, len, "* DPMS: %s\n", tdm_dpms_str(dpms));
1793 TDM_SNPRINTF(reply, len, "* global fps: %u\n", vblank_global_fps);
1794 TDM_SNPRINTF(reply, len, "----------------------------------------------------------------------------------\n");
1795 TDM_SNPRINTF(reply, len, "Name Type Wait Interval Sequence Fps Ignore Offset Fake Process\n");
1796 TDM_SNPRINTF(reply, len, "----------------------------------------------------------------------------------\n");
1798 pthread_mutex_lock(&valid_list_lock);
1799 LIST_FOR_EACH_ENTRY(v, &valid_vblank_list, valid_link) {
1800 const char *proc_name = NULL;
1804 _tdm_vblank_get_client_information(v, &pid, &proc_name);
1806 count = _tdm_vblank_get_waiting_count(v);
1808 TDM_SNPRINTF(reply, len, "%-8s(%p) ", v->name, v);
1809 TDM_SNPRINTF(reply, len, "%-4s ", (v->last_type == VBLANK_EVENT_TYPE_HW) ? "HW" : "SW");
1810 TDM_SNPRINTF(reply, len, "%-4s ", (count > 0) ? "O" : " ");
1811 TDM_SNPRINTF(reply, len, "%-8d ", v->last_interval);
1812 TDM_SNPRINTF(reply, len, "%-8u ", v->last_seq);
1813 TDM_SNPRINTF(reply, len, "%-3u ", v->fps);
1814 TDM_SNPRINTF(reply, len, "%-6u ", v->ignore_global_fps);
1815 TDM_SNPRINTF(reply, len, "%-6d ", v->offset);
1816 TDM_SNPRINTF(reply, len, "%-4u ", v->enable_fake);
1817 TDM_SNPRINTF(reply, len, "%s(pid:%d)\n", (proc_name) ? proc_name : "Unknown", pid);
1819 pthread_mutex_unlock(&valid_list_lock);
1821 TDM_SNPRINTF(reply, len, "\n");
1823 /* LCOV_EXCL_STOP */