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->next);
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_client_vblank_fps(unsigned int pid, const char *name, unsigned int fps)
456 tdm_private_vblank *v = NULL;
457 tdm_error ret = TDM_ERROR_NONE;
459 TDM_RETURN_VAL_IF_FAIL(pid > 0, TDM_ERROR_INVALID_PARAMETER);
460 TDM_RETURN_VAL_IF_FAIL(fps > 0, TDM_ERROR_INVALID_PARAMETER);
462 TDM_INFO("pid: %u, name: %s, fps: %d", pid, name, fps);
464 pthread_mutex_lock(&valid_list_lock);
465 LIST_FOR_EACH_ENTRY(v, &valid_vblank_list, valid_link) {
466 struct wl_client *client;
467 pid_t client_pid = 0;
472 client = wl_resource_get_client(v->resource);
476 wl_client_get_credentials(client, &client_pid, NULL, NULL);
478 if (client_pid != pid)
481 if (name && strncmp(name, TDM_VBLANK_DEFAULT_NAME, TDM_NAME_LEN)) {
482 if (strncmp(v->name, name, TDM_NAME_LEN))
486 ret = tdm_vblank_set_fps(v, fps);
487 if (ret == TDM_ERROR_NONE)
488 TDM_INFO("(pid:%u) '%s' fps changed: %d", pid, v->name, fps);
490 TDM_ERR("(pid:%u) '%s' fps changing failed: %d", pid, v->name, fps);
492 pthread_mutex_unlock(&valid_list_lock);
494 return TDM_ERROR_NONE;
498 tdm_vblank_set_client_ignore_global_fps(unsigned int pid, const char *name, unsigned int ignore)
500 tdm_private_vblank *v = NULL;
501 tdm_error ret = TDM_ERROR_NONE;
503 TDM_RETURN_VAL_IF_FAIL(pid > 0, TDM_ERROR_INVALID_PARAMETER);
505 TDM_INFO("pid: %u, name: %s, ignore: %u", pid, name, ignore);
507 pthread_mutex_lock(&valid_list_lock);
508 LIST_FOR_EACH_ENTRY(v, &valid_vblank_list, valid_link) {
509 struct wl_client *client;
510 pid_t client_pid = 0;
515 client = wl_resource_get_client(v->resource);
519 wl_client_get_credentials(client, &client_pid, NULL, NULL);
521 if (client_pid != pid)
524 if (name && strncmp(name, TDM_VBLANK_DEFAULT_NAME, TDM_NAME_LEN)) {
525 if (strncmp(v->name, name, TDM_NAME_LEN))
529 ret = tdm_vblank_ignore_global_fps(v, ignore);
530 if (ret == TDM_ERROR_NONE)
531 TDM_INFO("(pid:%u) '%s' ignore changed: %u", pid, v->name, ignore);
533 TDM_ERR("(pid:%u) '%s' ignore changing failed: %u", pid, v->name, ignore);
535 pthread_mutex_unlock(&valid_list_lock);
537 return TDM_ERROR_NONE;
541 tdm_vblank_enable_global_fps(unsigned int enable, unsigned int fps)
543 tdm_private_vblank *v = NULL;
546 TDM_RETURN_VAL_IF_FAIL(fps > 0, TDM_ERROR_INVALID_PARAMETER);
548 TDM_INFO("global fps: %s fps(%d)", (enable) ? "enabled" : "disabled", fps);
550 pthread_mutex_lock(&valid_list_lock);
552 vblank_global_fps = (enable) ? fps : 0;
554 LIST_FOR_EACH_ENTRY(v, &valid_vblank_list, valid_link) {
555 /* skip if vblank is created in server side */
559 v->check_HW_or_SW = 1;
561 pthread_mutex_unlock(&valid_list_lock);
563 return TDM_ERROR_NONE;
567 _tdm_vblank_thread_cb_create(tdm_private_display *private_display, void *object, tdm_thread_cb_base *cb_base, void *user_data)
569 tdm_thread_cb_vblank_create *vblank_create = (tdm_thread_cb_vblank_create *)cb_base;
570 tdm_vblank_create_handler_info *ch_info = user_data;
571 tdm_private_vblank *private_vblank;
573 TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
575 assert(ch_info != NULL);
577 private_vblank = _tdm_vblank_find_object(private_display, vblank_create->vblank_stamp);
578 if (!private_vblank) {
579 TDM_DBG("can't find vblank(%.0f) from valid_list", vblank_create->vblank_stamp);
583 private_vblank->in_create_handler = 1;
584 tdm_display_unlock(private_display);
585 ch_info->func(private_vblank, ch_info->user_data);
586 tdm_display_lock(private_display);
587 private_vblank->in_create_handler = 0;
591 _tdm_vblank_call_thread_cb(tdm_private_vblank *private_vblank)
593 tdm_thread_cb_vblank_create vblank_create;
596 memset(&vblank_create, 0, sizeof vblank_create);
597 vblank_create.base.type = TDM_THREAD_CB_VBLANK_CREATE;
598 vblank_create.base.length = sizeof vblank_create;
599 vblank_create.base.object_stamp = 1;
600 vblank_create.base.data = NULL;
601 vblank_create.base.sync = 0;
602 vblank_create.vblank_stamp = private_vblank->stamp;
604 ret = tdm_thread_cb_call(private_vblank->dpy, &vblank_create.base, 1);
605 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
607 return TDM_ERROR_NONE;
611 tdm_vblank_add_create_handler(tdm_display *dpy, tdm_vblank_create_handler func, void *user_data)
613 tdm_vblank_create_handler_info *ch_info = NULL;
616 TDM_RETURN_VAL_IF_FAIL(dpy != NULL, TDM_ERROR_INVALID_PARAMETER);
617 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
619 pthread_mutex_lock(&valid_list_lock);
621 LIST_FOR_EACH_ENTRY(ch_info, &create_handler_list, link) {
622 if (ch_info->func == func && ch_info->user_data == user_data) {
623 TDM_ERR("can't be added twice");
624 pthread_mutex_unlock(&valid_list_lock);
625 return TDM_ERROR_BAD_REQUEST;
629 ch_info = calloc(1, sizeof *ch_info);
631 TDM_ERR("alloc failed: %m");
632 pthread_mutex_unlock(&valid_list_lock);
633 return TDM_ERROR_OUT_OF_MEMORY;
636 tdm_display_lock(dpy);
637 ret = tdm_thread_cb_add(dpy, TDM_THREAD_CB_VBLANK_CREATE, NULL, _tdm_vblank_thread_cb_create, ch_info);
638 tdm_display_unlock(dpy);
640 if (ret != TDM_ERROR_NONE) {
641 TDM_ERR("tdm_thread_cb_add failed");
643 pthread_mutex_unlock(&valid_list_lock);
647 ch_info->func = func;
648 ch_info->user_data = user_data;
650 LIST_ADDTAIL(&ch_info->link, &create_handler_list);
652 pthread_mutex_unlock(&valid_list_lock);
654 return TDM_ERROR_NONE;
658 tdm_vblank_remove_create_handler(tdm_display *dpy, tdm_vblank_create_handler func, void *user_data)
660 tdm_vblank_create_handler_info *ch_info = NULL, *hh = NULL;
662 TDM_RETURN_IF_FAIL(dpy != NULL);
663 TDM_RETURN_IF_FAIL(func != NULL);
665 tdm_display_lock(dpy);
666 pthread_mutex_lock(&valid_list_lock);
667 LIST_FOR_EACH_ENTRY_SAFE(ch_info, hh, &create_handler_list, link) {
668 if (ch_info->func != func || ch_info->user_data != user_data)
671 tdm_thread_cb_remove(dpy, TDM_THREAD_CB_VBLANK_CREATE, NULL, _tdm_vblank_thread_cb_create, ch_info);
673 LIST_DEL(&ch_info->link);
675 pthread_mutex_unlock(&valid_list_lock);
676 tdm_display_unlock(dpy);
679 pthread_mutex_unlock(&valid_list_lock);
680 tdm_display_unlock(dpy);
684 _tdm_vblank_find_display(tdm_private_display *private_display, double stamp)
686 return private_display;
690 tdm_vblank_init(tdm_display *dpy)
692 if (vblank_list_inited)
693 return TDM_ERROR_NONE;
695 LIST_INITHEAD(&valid_vblank_list);
696 LIST_INITHEAD(&valid_wait_list);
697 LIST_INITHEAD(&create_handler_list);
699 tdm_thread_cb_set_find_func(TDM_THREAD_CB_VBLANK_SW, _tdm_vblank_find_object);
700 tdm_thread_cb_set_find_func(TDM_THREAD_CB_VBLANK_CREATE, _tdm_vblank_find_display);
702 vblank_list_inited = 1;
704 return TDM_ERROR_NONE;
708 tdm_vblank_deinit(tdm_display *dpy)
710 tdm_vblank_create_handler_info *ch_info = NULL, *hh = NULL;
712 if (!vblank_list_inited)
715 if (!LIST_IS_EMPTY(&valid_vblank_list))
716 TDM_NEVER_GET_HERE();
718 if (!LIST_IS_EMPTY(&valid_wait_list))
719 TDM_NEVER_GET_HERE();
721 LIST_FOR_EACH_ENTRY_SAFE(ch_info, hh, &create_handler_list, link) {
722 LIST_DEL(&ch_info->link);
726 vblank_list_inited = 0;
730 tdm_vblank_create(tdm_display *dpy, tdm_output *output, tdm_error *error)
732 tdm_private_vblank *private_vblank;
735 TDM_RETURN_VAL_IF_FAIL_WITH_ERROR(dpy != NULL, TDM_ERROR_INVALID_PARAMETER, NULL);
736 TDM_RETURN_VAL_IF_FAIL_WITH_ERROR(output != NULL, TDM_ERROR_INVALID_PARAMETER, NULL);
739 *error = TDM_ERROR_NONE;
741 private_vblank = calloc(1, sizeof * private_vblank);
742 if (!private_vblank) {
743 /* LCOV_EXCL_START */
745 *error = TDM_ERROR_OUT_OF_MEMORY;
746 TDM_ERR("alloc failed");
751 ret = tdm_output_add_change_handler(output, _tdm_vblank_cb_output_change, private_vblank);
752 if (ret != TDM_ERROR_NONE) {
753 free(private_vblank);
755 *error = TDM_ERROR_OPERATION_FAILED;
756 TDM_ERR("tdm_output_add_change_handler failed");
760 private_vblank->stamp = ++stamp;
761 private_vblank->owner_tid = syscall(SYS_gettid);
762 private_vblank->dpy = dpy;
763 private_vblank->output = output;
764 private_vblank->check_HW_or_SW = 1;
765 private_vblank->fps_changeable = 1;
766 private_vblank->last_interval = -1;
768 _tdm_vblank_update_output_info(private_vblank);
770 strncpy(private_vblank->name, TDM_VBLANK_DEFAULT_NAME, TDM_NAME_LEN - 1);
771 private_vblank->name[TDM_NAME_LEN - 1] = '\0';
773 LIST_INITHEAD(&private_vblank->HW_wait_list);
774 LIST_INITHEAD(&private_vblank->SW_wait_list);
776 _tdm_vblank_valid_list_add(&private_vblank->valid_link, &valid_vblank_list);
778 VIN("created. vrefresh(%d) connection(%d)",
779 private_vblank->vrefresh, private_vblank->connection);
781 tdm_display_lock(private_vblank->dpy);
782 _tdm_vblank_call_thread_cb(private_vblank);
783 tdm_display_unlock(private_vblank->dpy);
785 return (tdm_vblank *)private_vblank;
789 tdm_vblank_destroy(tdm_vblank *vblank)
791 tdm_private_vblank *private_vblank = vblank;
792 tdm_vblank_wait_info *w = NULL, *ww = NULL;
794 TDM_RETURN_IF_FAIL(tdm_vblank_is_valid(vblank));
796 if (private_vblank->in_create_handler) {
797 TDM_ERR("NOT allowed to be called in a create handler");
801 _tdm_vblank_valid_list_del(&private_vblank->valid_link);
803 if (private_vblank->SW_timer) {
804 tdm_display_lock(private_vblank->dpy);
805 tdm_thread_cb_remove(private_vblank, TDM_THREAD_CB_VBLANK_SW, NULL, _tdm_vblank_cb_vblank_SW, NULL);
806 tdm_event_loop_source_remove(private_vblank->SW_timer);
807 tdm_display_unlock(private_vblank->dpy);
810 tdm_output_remove_change_handler(private_vblank->output,
811 _tdm_vblank_cb_output_change, private_vblank);
813 _tdm_vblank_free_HW_wait(private_vblank, 0, 0);
815 if (!LIST_IS_EMPTY(&private_vblank->SW_wait_list)) {
817 const char *proc_name;
818 _tdm_vblank_get_client_information(private_vblank, &pid, &proc_name);
819 VER("TDM SW vblank destroyed forcely!! (pid: %u, name: %s)", pid, proc_name);
822 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->SW_wait_list, link) {
824 _tdm_vblank_valid_list_del(&w->valid_link);
828 if (private_vblank->timeout_timer) {
829 tdm_display_lock(private_vblank->dpy);
830 tdm_event_loop_source_remove(private_vblank->timeout_timer);
831 tdm_display_unlock(private_vblank->dpy);
836 free(private_vblank);
840 tdm_vblank_get_output(tdm_vblank *vblank, tdm_error *error)
842 tdm_private_vblank *private_vblank = vblank;
843 tdm_error ret = TDM_ERROR_NONE;
845 TDM_RETURN_VAL_IF_FAIL_WITH_ERROR(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER, NULL);
848 *error = TDM_ERROR_NONE;
850 return private_vblank->output;
854 tdm_vblank_get_client_pid(tdm_vblank *vblank, pid_t *pid)
856 tdm_private_vblank *private_vblank = vblank;
857 struct wl_client *client;
859 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
860 TDM_RETURN_VAL_IF_FAIL(pid != NULL, TDM_ERROR_INVALID_PARAMETER);
862 if (!private_vblank->resource) {
864 return TDM_ERROR_NONE;
867 client = wl_resource_get_client(private_vblank->resource);
870 return TDM_ERROR_NONE;
873 wl_client_get_credentials(client, pid, NULL, NULL);
875 return TDM_ERROR_NONE;
879 tdm_vblank_set_name(tdm_vblank *vblank, const char *name)
881 tdm_private_vblank *private_vblank = vblank;
883 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
886 name = TDM_VBLANK_DEFAULT_NAME;
888 strncpy(private_vblank->name, name, TDM_NAME_LEN - 1);
889 private_vblank->name[TDM_NAME_LEN - 1] = '\0';
891 VIN("name(%s)", name);
893 return TDM_ERROR_NONE;
897 tdm_vblank_get_name(tdm_vblank *vblank, const char **name)
899 tdm_private_vblank *private_vblank = vblank;
901 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
902 TDM_RETURN_VAL_IF_FAIL(name != NULL, TDM_ERROR_INVALID_PARAMETER);
904 *name = (const char*)private_vblank->name;
906 return TDM_ERROR_NONE;
910 tdm_vblank_set_fps(tdm_vblank *vblank, unsigned int fps)
912 tdm_private_vblank *private_vblank = vblank;
914 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
915 TDM_RETURN_VAL_IF_FAIL(fps > 0, TDM_ERROR_INVALID_PARAMETER);
917 if (private_vblank->fps_fixed) {
918 VIN("fps(%u) can't be changed", private_vblank->fps);
919 return TDM_ERROR_NONE;
922 private_vblank->fps_changeable = 0;
924 if (private_vblank->fps == fps)
925 return TDM_ERROR_NONE;
927 private_vblank->fps = fps;
928 private_vblank->check_HW_or_SW = 1;
930 VIN("fps(%u) changed", fps);
932 return TDM_ERROR_NONE;
936 tdm_vblank_get_fps(tdm_vblank *vblank, unsigned int *fps)
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(fps != NULL, TDM_ERROR_INVALID_PARAMETER);
943 *fps = private_vblank->fps;
945 return TDM_ERROR_NONE;
949 tdm_vblank_set_fixed_fps(tdm_vblank *vblank, unsigned int fps)
951 tdm_private_vblank *private_vblank = vblank;
953 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
954 TDM_RETURN_VAL_IF_FAIL(fps > 0, TDM_ERROR_INVALID_PARAMETER);
956 private_vblank->fps_changeable = 0;
957 private_vblank->fps_fixed = 1;
959 if (private_vblank->fps == fps)
960 return TDM_ERROR_NONE;
962 private_vblank->fps = fps;
963 private_vblank->check_HW_or_SW = 1;
965 VIN("fps(%u) fixed", fps);
967 return TDM_ERROR_NONE;
971 tdm_vblank_ignore_global_fps(tdm_vblank *vblank, unsigned int ignore)
973 tdm_private_vblank *private_vblank = vblank;
975 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
977 if (private_vblank->ignore_global_fps == ignore)
978 return TDM_ERROR_NONE;
980 private_vblank->ignore_global_fps = ignore;
981 private_vblank->check_HW_or_SW = 1;
983 VIN("ignore_global_fps(%u)", private_vblank->ignore_global_fps);
985 return TDM_ERROR_NONE;
989 tdm_vblank_set_offset(tdm_vblank *vblank, int offset)
991 tdm_private_vblank *private_vblank = vblank;
993 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
994 TDM_RETURN_VAL_IF_FAIL(offset >= 0, TDM_ERROR_INVALID_PARAMETER);
996 if (private_vblank->offset == offset)
997 return TDM_ERROR_NONE;
999 private_vblank->offset = offset;
1000 private_vblank->check_HW_or_SW = 1;
1002 VIN("offset(%u)", private_vblank->offset);
1004 return TDM_ERROR_NONE;
1008 tdm_vblank_get_offset(tdm_vblank *vblank, int *offset)
1010 tdm_private_vblank *private_vblank = vblank;
1012 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1013 TDM_RETURN_VAL_IF_FAIL(offset != NULL, TDM_ERROR_INVALID_PARAMETER);
1015 *offset = private_vblank->offset;
1017 return TDM_ERROR_NONE;
1021 tdm_vblank_set_enable_fake(tdm_vblank *vblank, unsigned int enable_fake)
1023 tdm_private_vblank *private_vblank = vblank;
1025 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1027 if (private_vblank->enable_fake == enable_fake)
1028 return TDM_ERROR_NONE;
1030 private_vblank->enable_fake = enable_fake;
1032 VIN("enable_fake(%u)", private_vblank->enable_fake);
1034 return TDM_ERROR_NONE;
1038 tdm_vblank_get_enable_fake(tdm_vblank *vblank, unsigned int *enable_fake)
1040 tdm_private_vblank *private_vblank = vblank;
1042 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1043 TDM_RETURN_VAL_IF_FAIL(enable_fake != NULL, TDM_ERROR_INVALID_PARAMETER);
1045 *enable_fake = private_vblank->enable_fake;
1047 return TDM_ERROR_NONE;
1051 _tdm_vblank_timeout_timer_cb(void *user_data)
1053 tdm_private_vblank *private_vblank = user_data;
1054 tdm_vblank_wait_info *w = NULL;
1056 const char *proc_name;
1058 TDM_RETURN_VAL_IF_FAIL(private_vblank != NULL, TDM_ERROR_OPERATION_FAILED);
1060 _tdm_vblank_get_client_information(private_vblank, &pid, &proc_name);
1062 VER("TDM vblank TIMEOUT!! (pid: %u, name: %s)", pid, proc_name);
1063 VER("quotient(%u) last_seq(%u) last_time(%.6f)", private_vblank->quotient, private_vblank->last_seq, private_vblank->last_time);
1065 LIST_FOR_EACH_ENTRY(w, &private_vblank->HW_wait_list, link) {
1066 VER("wait_info(%p) req_time(%.6f) target_seq(%u) interval(%u) hw_interval(%d)",
1067 w, w->req_time, w->target_seq, w->interval, w->hw_interval);
1068 tdm_output_vblank_print_wait_information(private_vblank->output, w);
1071 LIST_FOR_EACH_ENTRY(w, &private_vblank->SW_wait_list, link) {
1072 VER("wait_info(%p) req_time(%.6f) target_time(%.6f) target_seq(%u) interval(%u)",
1073 w, w->req_time, w->target_time, w->target_seq, w->interval);
1076 return TDM_ERROR_NONE;
1080 _tdm_vblank_timeout_timer_update(tdm_private_vblank *private_vblank, int ms_delay)
1084 if (!private_vblank->timeout_timer) {
1085 private_vblank->timeout_timer =
1086 tdm_event_loop_add_timer_handler(private_vblank->dpy,
1087 _tdm_vblank_timeout_timer_cb,
1090 if (!private_vblank->timeout_timer) {
1091 VER("couldn't add timer");
1094 VIN("Create Timeout timer");
1097 ret = tdm_event_loop_source_timer_update(private_vblank->timeout_timer, ms_delay);
1098 if (ret != TDM_ERROR_NONE) {
1099 VER("couldn't update timer");
1104 VDB("timeout timer updated");
1106 VDB("timeout timer disabled");
1110 _tdm_vblank_sw_timer_update(tdm_private_vblank *private_vblank)
1112 tdm_vblank_wait_info *first_wait_info = NULL;
1113 double curr, target;
1117 if (LIST_IS_EMPTY(&private_vblank->SW_wait_list)) {
1118 VER("no wait_info");
1119 return TDM_ERROR_OPERATION_FAILED;
1122 first_wait_info = container_of(private_vblank->SW_wait_list.next, first_wait_info, link);
1123 curr = tdm_helper_get_time();
1124 target = first_wait_info->target_time;
1126 /* ms_delay should be more that 1 */
1130 ms_delay = ceil((target - curr) * 1000.0);
1135 if (ms_delay > 5000)
1136 VER("wait(%p) curr(%.6f) target(%.6f) ms_delay(%d)", first_wait_info, curr, target, ms_delay);
1138 VDB("wait(%p) curr(%.6f) target(%.6f) ms_delay(%d)", first_wait_info, curr, target, ms_delay);
1140 tdm_display_lock(private_vblank->dpy);
1142 if (!private_vblank->SW_timer) {
1143 private_vblank->SW_timer =
1144 tdm_event_loop_add_timer_handler(private_vblank->dpy,
1145 _tdm_vblank_cb_timeout_SW,
1148 if (!private_vblank->SW_timer) {
1149 tdm_display_unlock(private_vblank->dpy);
1150 VER("couldn't add timer");
1154 ret = tdm_thread_cb_add(private_vblank, TDM_THREAD_CB_VBLANK_SW, NULL, _tdm_vblank_cb_vblank_SW, NULL);
1155 if (ret != TDM_ERROR_NONE) {
1156 tdm_event_loop_source_remove(private_vblank->SW_timer);
1157 private_vblank->SW_timer = NULL;
1158 tdm_display_unlock(private_vblank->dpy);
1159 VER("couldn't add thread_cb");
1163 VIN("Create SW timer");
1166 private_vblank->last_interval = ms_delay;
1168 ret = tdm_event_loop_source_timer_update(private_vblank->SW_timer, ms_delay);
1169 if (ret != TDM_ERROR_NONE) {
1170 /* LCOV_EXCL_START */
1171 tdm_display_unlock(private_vblank->dpy);
1172 VER("couldn't update timer");
1174 /* LCOV_EXCL_STOP */
1177 tdm_display_unlock(private_vblank->dpy);
1179 return TDM_ERROR_NONE;
1183 _tdm_vblank_cb_vblank_HW(tdm_output *output, unsigned int sequence,
1184 unsigned int tv_sec, unsigned int tv_usec,
1187 tdm_vblank_wait_info *wait_info = user_data;
1188 tdm_private_vblank *private_vblank;
1190 if (!_tdm_vblank_check_valid_wait(wait_info)) {
1191 TDM_ERR("can't find wait(%p) from valid_wait_list", wait_info);
1195 private_vblank = wait_info->private_vblank;
1196 TDM_RETURN_IF_FAIL(tdm_vblank_is_valid(private_vblank));
1198 assert(private_vblank->owner_tid == syscall(SYS_gettid));
1200 if (!_tdm_vblank_find_wait(wait_info, &private_vblank->HW_wait_list)) {
1201 VER("can't find wait(%p)", wait_info);
1205 if (wait_info->type == VBLANK_TYPE_HW_SW) {
1208 LIST_DEL(&wait_info->link);
1210 /* kernel sends the wrong time of first vblank event. and the time of
1211 * second vblank is correct.
1213 wait_info->target_time = TDM_TIME(tv_sec, tv_usec);
1214 wait_info->target_time += (((double)private_vblank->offset) / 1000.0);
1216 _tdm_vblank_insert_wait(wait_info, &private_vblank->SW_wait_list);
1218 ret = _tdm_vblank_sw_timer_update(private_vblank);
1220 /* wait_info will be freed in _tdm_vblank_cb_timeout_SW() */
1221 if (ret == TDM_ERROR_NONE) {
1222 VIN("wait(%p) SW timer", wait_info);
1226 VER("couldn't update sw timer");
1229 tdm_display_lock(private_vblank->dpy);
1230 _tdm_vblank_timeout_timer_update(private_vblank, 0);
1231 tdm_display_unlock(private_vblank->dpy);
1233 LIST_DEL(&wait_info->link);
1234 _tdm_vblank_valid_list_del(&wait_info->valid_link);
1236 /* tv_sec & tv_usec shouldn't be zero. But sometimes they are zero in some hardware.
1237 * We need to prohibit that last_time becomes zero because "last_time == 0" means
1238 * that a tdm_vblank object is just created and doesn't have the first vblank event.
1239 * If last_time becomes 0, it would make issue.
1241 if (tv_sec == 0 && tv_usec == 0) {
1242 double curr = tdm_helper_get_time();
1243 tv_sec = TDM_TIME_SEC(curr);
1244 tv_usec = TDM_TIME_USEC(curr);
1247 private_vblank->last_seq = wait_info->target_seq;
1248 private_vblank->last_time = TDM_TIME(tv_sec, tv_usec);
1250 VDB("wait(%p) last(%.6f) sequence(%u) done", wait_info, private_vblank->last_time, wait_info->target_seq);
1252 if (tdm_ttrace_module & TDM_TTRACE_VBLANK)
1253 TDM_TRACE_ASYNC_END((int)wait_info->req_time, "TDM_Vblank:%u", (unsigned int)private_vblank->stamp);
1255 if (wait_info->func)
1256 wait_info->func(private_vblank, TDM_ERROR_NONE, wait_info->target_seq,
1257 tv_sec, tv_usec, wait_info->user_data);
1263 _tdm_vblank_wait_HW(tdm_vblank_wait_info *wait_info)
1265 tdm_private_vblank *private_vblank = wait_info->private_vblank;
1269 if (private_vblank->last_type != VBLANK_EVENT_TYPE_HW) {
1270 VIN("Use HW vblank");
1271 private_vblank->last_type = VBLANK_EVENT_TYPE_HW;
1274 _tdm_vblank_insert_wait(wait_info, &private_vblank->HW_wait_list);
1276 if (private_vblank->last_time == 0) {
1277 hw_interval = wait_info->interval * private_vblank->quotient;
1279 /* SW vblank starts from now. SW vblank doesn't need to be aligned with HW vblank. */
1280 private_vblank->last_seq = 0;
1281 private_vblank->last_time = tdm_helper_get_time();
1283 /* +1 ms to call the handler ASAP at the first. no matter for SW timer. */
1284 wait_info->target_seq = 1;
1286 VDB("wait(%p) last(%.6f) hw_itvl(%d) targ_seq(%u)",
1287 wait_info, private_vblank->last_time, hw_interval, wait_info->target_seq);
1289 double last, prev, req, curr, target;
1292 last = private_vblank->last_time;
1293 req = wait_info->req_time;
1295 /* make sure that req time is bigger than last. */
1299 skip = (unsigned int)((req - last) / private_vblank->vblank_gap + TDM_TIME_MARGIN);
1300 prev = last + private_vblank->vblank_gap * skip;
1302 curr = tdm_helper_get_time();
1304 while (target < curr)
1305 target += (private_vblank->vblank_gap * wait_info->interval);
1307 hw_interval = (unsigned int)((target - curr) / private_vblank->HW_vblank_gap + TDM_TIME_MARGIN) + 1;
1309 wait_info->target_seq = private_vblank->last_seq;
1310 wait_info->target_seq += (unsigned int)((target - last) / private_vblank->vblank_gap + TDM_TIME_MARGIN);
1312 if (hw_interval > 300)
1313 VER("wait(%p) last(%.6f) req(%.6f) prev(%.6f) curr(%.6f) targ(%.6f,%.6f) skip(%u) hw_itvl(%d) targ_seq(%u)",
1314 wait_info, last, req - last, prev - last, curr - last, target, target - last,
1315 skip, hw_interval, wait_info->target_seq);
1317 VDB("wait(%p) last(%.6f) req(%.6f) prev(%.6f) curr(%.6f) targ(%.6f,%.6f) skip(%u) hw_itvl(%d) targ_seq(%u)",
1318 wait_info, last, req - last, prev - last, curr - last, target, target - last,
1319 skip, hw_interval, wait_info->target_seq);
1322 private_vblank->last_interval = hw_interval;
1323 wait_info->hw_interval = hw_interval;
1325 if (tdm_ttrace_module & TDM_TTRACE_VBLANK)
1326 TDM_TRACE_ASYNC_BEGIN((int)wait_info->req_time, "TDM_Vblank:%u", (unsigned int)private_vblank->stamp);
1328 if (private_vblank->add_front)
1329 ret = tdm_output_wait_vblank_add_front(private_vblank->output, hw_interval, 0,
1330 _tdm_vblank_cb_vblank_HW, wait_info);
1332 ret = tdm_output_wait_vblank(private_vblank->output, hw_interval, 0,
1333 _tdm_vblank_cb_vblank_HW, wait_info);
1335 if (ret != TDM_ERROR_NONE) {
1336 LIST_DEL(&wait_info->link);
1340 VDB("wait(%p) waiting", wait_info);
1342 return TDM_ERROR_NONE;
1346 _tdm_vblank_cb_vblank_SW(tdm_private_display *private_display, void *object, tdm_thread_cb_base *cb_base, void *user_data)
1348 tdm_private_vblank *private_vblank = object;
1349 tdm_vblank_wait_info *first_wait_info = NULL, *w = NULL, *ww = NULL;
1351 TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
1353 assert(private_vblank->owner_tid == syscall(SYS_gettid));
1355 if (LIST_IS_EMPTY(&private_vblank->SW_wait_list)) {
1356 VER("no wait_info");
1360 if (!LIST_IS_EMPTY(&private_vblank->HW_wait_list))
1361 TDM_NEVER_GET_HERE();
1363 first_wait_info = container_of(private_vblank->SW_wait_list.next, first_wait_info, link);
1364 TDM_RETURN_IF_FAIL(first_wait_info != NULL);
1366 _tdm_vblank_timeout_timer_update(private_vblank, 0);
1368 if (private_vblank->last_seq >= first_wait_info->target_seq)
1369 TDM_ERR("last_seq(%u) target_seq(%u)", private_vblank->last_seq, first_wait_info->target_seq);
1371 private_vblank->last_seq = first_wait_info->target_seq;
1372 private_vblank->last_time = first_wait_info->target_time;
1374 VDB("wait(%p) last(%.6f) sequence(%u) done", first_wait_info, private_vblank->last_time, first_wait_info->target_seq);
1376 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->SW_wait_list, link) {
1377 if (w->target_time != first_wait_info->target_time)
1381 _tdm_vblank_valid_list_del(&w->valid_link);
1384 tdm_display_unlock(private_vblank->dpy);
1385 w->func(private_vblank, TDM_ERROR_NONE, w->target_seq,
1386 TDM_TIME_SEC(w->target_time), TDM_TIME_USEC(w->target_time),
1388 tdm_display_lock(private_vblank->dpy);
1396 _tdm_vblank_cb_timeout_SW(void *user_data)
1398 tdm_private_vblank *private_vblank = user_data;
1399 tdm_thread_cb_vblank_sw vblank_sw;
1402 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(private_vblank), TDM_ERROR_OPERATION_FAILED);
1404 VIN("timeout_SW -> thread-cb");
1406 memset(&vblank_sw, 0, sizeof vblank_sw);
1407 vblank_sw.base.type = TDM_THREAD_CB_VBLANK_SW;
1408 vblank_sw.base.length = sizeof vblank_sw;
1409 vblank_sw.base.object_stamp = private_vblank->stamp;
1410 vblank_sw.base.data = NULL;
1411 vblank_sw.base.sync = 0;
1413 ret = tdm_thread_cb_call(private_vblank, &vblank_sw.base, 1);
1414 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
1416 return TDM_ERROR_NONE;
1420 _tdm_vblank_wait_SW(tdm_vblank_wait_info *wait_info)
1422 tdm_private_vblank *private_vblank = wait_info->private_vblank;
1424 double last, prev, req, curr, target;
1427 if (private_vblank->last_type != VBLANK_EVENT_TYPE_SW) {
1428 tdm_output_dpms dpms;
1430 tdm_output_get_dpms(private_vblank->output, &dpms);
1432 VIN("Use SW vblank. connection(%s), dpms(%s), vrefresh(%d), fps(%d), offset(%d)",
1433 tdm_status_str(private_vblank->connection), tdm_dpms_str(dpms),
1434 private_vblank->vrefresh, private_vblank->fps, private_vblank->offset);
1435 private_vblank->last_type = VBLANK_EVENT_TYPE_SW;
1438 if (private_vblank->last_time == 0) {
1439 /* SW vblank starts from now. SW vblank doesn't need to be aligned with HW vblank. */
1440 private_vblank->last_seq = 0;
1441 private_vblank->last_time = tdm_helper_get_time() - private_vblank->vblank_gap;
1443 /* +1 ms to call the handler ASAP at the first. no matter for SW timer. */
1444 wait_info->target_seq = 1;
1447 last = private_vblank->last_time;
1448 req = wait_info->req_time;
1450 /* make sure that req time is bigger than last. */
1454 skip = (unsigned int)((req - last) / private_vblank->vblank_gap + TDM_TIME_MARGIN);
1455 prev = last + private_vblank->vblank_gap * skip;
1457 curr = tdm_helper_get_time();
1458 target = prev + private_vblank->vblank_gap * wait_info->interval;
1460 while (target < curr)
1461 target += private_vblank->vblank_gap;
1463 wait_info->target_seq = private_vblank->last_seq;
1464 wait_info->target_seq += (unsigned int)((target - last) / private_vblank->vblank_gap + TDM_TIME_MARGIN);
1466 wait_info->target_time = target + (double)(private_vblank->offset) / 1000;
1468 VDB("wait(%p) last(%.6f) req(%.6f) prev(%.6f) curr(%.6f) targ(%.6f,%.6f) skip(%u) targ_seq(%u)",
1469 wait_info, last, req - last, prev - last, curr - last,
1470 target, target - last, skip, wait_info->target_seq);
1472 _tdm_vblank_insert_wait(wait_info, &private_vblank->SW_wait_list);
1474 ret = _tdm_vblank_sw_timer_update(private_vblank);
1475 if (ret != TDM_ERROR_NONE) {
1476 /* LCOV_EXCL_START */
1477 LIST_DEL(&wait_info->link);
1478 VER("couldn't update sw timer");
1480 /* LCOV_EXCL_STOP */
1483 return TDM_ERROR_NONE;
1487 tdm_vblank_wait(tdm_vblank *vblank, unsigned int req_sec, unsigned int req_usec,
1488 unsigned int interval, tdm_vblank_handler func, void *user_data)
1490 tdm_private_vblank *private_vblank = vblank;
1491 tdm_vblank_wait_info *wait_info;
1492 tdm_output_dpms dpms = TDM_OUTPUT_DPMS_OFF;
1496 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1497 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
1498 TDM_RETURN_VAL_IF_FAIL(interval > 0, TDM_ERROR_INVALID_PARAMETER);
1500 if (private_vblank->in_create_handler) {
1501 TDM_ERR("NOT allowed to be called in a create handler");
1502 return TDM_ERROR_BAD_REQUEST;
1505 if (private_vblank->owner_tid != syscall(SYS_gettid)) {
1506 TDM_ERR("SHOULD be called in the owner thread(%d)", (int)private_vblank->owner_tid);
1507 return TDM_ERROR_BAD_REQUEST;
1510 tdm_output_get_dpms(private_vblank->output, &dpms);
1512 if (!private_vblank->enable_fake) {
1513 if (private_vblank->connection == TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
1514 VER("can't wait a vblank: output disconnected");
1515 return TDM_ERROR_OUTPUT_DISCONNECTED;
1517 if (TDM_OUTPUT_DPMS_VSYNC_IS_OFF(dpms)) {
1518 VER("can't wait a vblank: DPMS %s", tdm_dpms_str(dpms));
1519 return TDM_ERROR_DPMS_OFF;
1523 wait_info = calloc(1, sizeof * wait_info);
1525 /* LCOV_EXCL_START */
1526 VER("alloc failed");
1527 return TDM_ERROR_OUT_OF_MEMORY;
1528 /* LCOV_EXCL_STOP */
1530 _tdm_vblank_update_output_info(private_vblank);
1532 /* set request time to current time if 0. This function seems to be called in server side. */
1533 if (req_sec == 0 && req_usec == 0) {
1534 double curr = tdm_helper_get_time();
1535 req_sec = TDM_TIME_SEC(curr);
1536 req_usec = TDM_TIME_USEC(curr);
1539 LIST_INITHEAD(&wait_info->link);
1541 _tdm_vblank_valid_list_add(&wait_info->valid_link, &valid_wait_list);
1543 wait_info->stamp = ++stamp;
1544 wait_info->req_time = TDM_TIME(req_sec, req_usec);
1545 wait_info->interval = interval;
1546 wait_info->func = func;
1547 wait_info->user_data = user_data;
1548 wait_info->private_vblank = private_vblank;
1550 if (!private_vblank->resource ||
1551 private_vblank->ignore_global_fps ||
1552 vblank_global_fps == 0 ||
1553 private_vblank->fps < vblank_global_fps)
1554 fps = private_vblank->fps;
1556 fps = vblank_global_fps;
1558 if (private_vblank->check_HW_or_SW) {
1559 private_vblank->check_HW_or_SW = 0;
1560 private_vblank->vblank_gap = 1.0 / fps;
1561 private_vblank->quotient = private_vblank->vrefresh / fps;
1564 /* 1) if fps != factor of vrefresh, SW timer
1565 * 2) if fps == factor of vrefresh && dpms == off, SW timer (Fake HW vblank)
1566 * 2) if fps == factor of vrefresh && dpms == on && offset == 0, HW vblank
1567 * 3) if fps == factor of vrefresh && dpms == on && offset != 0, HW vblank + SW timer
1568 * In case of 1), we really don't need to align with HW vblank.
1570 if (private_vblank->vrefresh % fps)
1571 wait_info->type = VBLANK_TYPE_SW;
1572 else if (TDM_OUTPUT_DPMS_VSYNC_IS_OFF(dpms) ||
1573 private_vblank->connection != TDM_OUTPUT_CONN_STATUS_MODE_SETTED)
1574 wait_info->type = VBLANK_TYPE_SW_FAKE;
1575 else if (private_vblank->offset == 0)
1576 wait_info->type = VBLANK_TYPE_HW;
1578 wait_info->type = VBLANK_TYPE_HW_SW;
1580 if (wait_info->type == VBLANK_TYPE_SW || wait_info->type == VBLANK_TYPE_SW_FAKE) {
1581 ret = _tdm_vblank_wait_SW(wait_info);
1582 if (ret != TDM_ERROR_NONE)
1583 TDM_ERR("_tdm_vblank_wait_SW failed: ret(%d)", ret);
1585 ret = _tdm_vblank_wait_HW(wait_info);
1586 if (ret != TDM_ERROR_NONE)
1587 TDM_ERR("_tdm_vblank_wait_HW failed: ret(%d)", ret);
1589 if (ret == TDM_ERROR_OUTPUT_DISCONNECTED || ret == TDM_ERROR_DPMS_OFF) {
1590 ret = _tdm_vblank_wait_SW(wait_info);
1591 if (ret != TDM_ERROR_NONE)
1592 TDM_ERR("_tdm_vblank_wait_SW failed: ret(%d)", ret);
1596 if (ret != TDM_ERROR_NONE) {
1597 /* LCOV_EXCL_START */
1598 LIST_DEL(&wait_info->link);
1599 _tdm_vblank_valid_list_del(&wait_info->valid_link);
1601 tdm_display_lock(private_vblank->dpy);
1602 tdm_output_remove_vblank_handler_internal(private_vblank->output, _tdm_vblank_cb_vblank_HW, wait_info);
1603 tdm_display_unlock(private_vblank->dpy);
1607 /* LCOV_EXCL_STOP */
1610 tdm_display_lock(private_vblank->dpy);
1611 _tdm_vblank_timeout_timer_update(private_vblank, 1000);
1612 tdm_display_unlock(private_vblank->dpy);
1614 return TDM_ERROR_NONE;
1618 tdm_vblank_wait_seq(tdm_vblank *vblank, unsigned int req_sec, unsigned int req_usec,
1619 unsigned int sequence, tdm_vblank_handler func, void *user_data)
1621 tdm_private_vblank *private_vblank = vblank;
1622 unsigned int interval;
1624 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1625 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
1627 /* if the sequence of vblank reaches the max value, sequence can be 0. */
1629 if (private_vblank->last_seq == 0) {
1630 VWR("can't calculate interval with sequence(%u)", sequence);
1632 } else if (sequence > private_vblank->last_seq ||
1633 TDM_VBLANK_SEQ_REACHED_MAX(sequence, private_vblank->last_seq)) {
1634 double last, curr, seq_target;
1636 last = private_vblank->last_time;
1637 curr = tdm_helper_get_time();
1638 seq_target = last + private_vblank->vblank_gap * (sequence - private_vblank->last_seq);
1640 /* If target is old or too close(1ms) to current, we call the handler immediately.
1641 * 2ms? it seems too much.
1643 if (seq_target < curr) {
1644 func(vblank, TDM_ERROR_NONE, sequence,
1645 TDM_TIME_SEC(seq_target), TDM_TIME_USEC(seq_target),
1647 return TDM_ERROR_NONE;
1648 } else if ((seq_target - curr) < 0.001) { /* 1ms */
1649 func(vblank, TDM_ERROR_NONE, sequence,
1650 TDM_TIME_SEC(curr), TDM_TIME_USEC(curr),
1652 return TDM_ERROR_NONE;
1655 interval = (unsigned int)((seq_target - curr) / private_vblank->vblank_gap + TDM_TIME_MARGIN) + 1;
1657 /* this seems error. But we handle this like normal to avoid the unexpected error in cliend side */
1658 VER("sequence(%u) should be over the last sequence(%u)", sequence, private_vblank->last_seq);
1659 func(vblank, TDM_ERROR_NONE, private_vblank->last_seq,
1660 TDM_TIME_SEC(private_vblank->last_time), TDM_TIME_USEC(private_vblank->last_time),
1662 return TDM_ERROR_NONE;
1665 return tdm_vblank_wait(vblank, req_sec, req_usec, interval, func, user_data);
1668 /* LCOV_EXCL_START */
1670 tdm_vblank_set_add_front(tdm_vblank *vblank, unsigned int add_front)
1672 tdm_private_vblank *private_vblank = vblank;
1674 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1676 TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
1678 if (private_vblank->add_front == add_front)
1679 return TDM_ERROR_NONE;
1681 private_vblank->add_front = add_front;
1683 return TDM_ERROR_NONE;
1685 /* LCOV_EXCL_STOP */
1688 tdm_vblank_set_resource(tdm_vblank *vblank, struct wl_resource *resource)
1690 tdm_private_vblank *private_vblank = vblank;
1692 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1693 TDM_RETURN_VAL_IF_FAIL(private_vblank->resource == NULL, TDM_ERROR_OPERATION_FAILED);
1695 private_vblank->resource = resource;
1697 return TDM_ERROR_NONE;
1701 tdm_vblank_get_stamp(tdm_vblank *vblank)
1703 tdm_private_vblank *private_vblank = vblank;
1705 TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1707 return private_vblank->stamp;
1711 _tdm_vblank_get_waiting_count(tdm_private_vblank *private_vblank)
1713 tdm_vblank_wait_info *wait_info = NULL;
1714 unsigned int count = 0;
1716 LIST_FOR_EACH_ENTRY(wait_info, &private_vblank->HW_wait_list, link) {
1720 LIST_FOR_EACH_ENTRY(wait_info, &private_vblank->SW_wait_list, link) {
1728 _tdm_vblank_get_client_information(tdm_private_vblank *private_vblank, pid_t *pid, const char **name)
1730 struct wl_client *client;
1732 if (!private_vblank->resource)
1735 client = wl_resource_get_client(private_vblank->resource);
1739 wl_client_get_credentials(client, pid, NULL, NULL);
1740 *name = tdm_server_get_client_name(*pid);
1750 /* LCOV_EXCL_START */
1752 tdm_vblank_get_vblank_list_information(tdm_display *dpy, char *reply, int *len)
1754 tdm_private_vblank *v = NULL;
1755 tdm_output_dpms dpms = TDM_OUTPUT_DPMS_OFF;
1756 tdm_output *output = tdm_display_get_output(dpy, 0, NULL);
1759 tdm_output_get_dpms(output, &dpms);
1761 TDM_SNPRINTF(reply, len, "[Client Vblank List]\n");
1762 TDM_SNPRINTF(reply, len, "* DPMS: %s\n", tdm_dpms_str(dpms));
1763 TDM_SNPRINTF(reply, len, "* global fps: %u\n", vblank_global_fps);
1764 TDM_SNPRINTF(reply, len, "----------------------------------------------------------------------------------\n");
1765 TDM_SNPRINTF(reply, len, "Name Type Wait Interval Sequence Fps Ignore Offset Fake Process\n");
1766 TDM_SNPRINTF(reply, len, "----------------------------------------------------------------------------------\n");
1768 pthread_mutex_lock(&valid_list_lock);
1769 LIST_FOR_EACH_ENTRY(v, &valid_vblank_list, valid_link) {
1770 const char *proc_name = NULL;
1774 _tdm_vblank_get_client_information(v, &pid, &proc_name);
1776 count = _tdm_vblank_get_waiting_count(v);
1778 TDM_SNPRINTF(reply, len, "%-8s(%p) ", v->name, v);
1779 TDM_SNPRINTF(reply, len, "%-4s ", (v->last_type == VBLANK_EVENT_TYPE_HW) ? "HW" : "SW");
1780 TDM_SNPRINTF(reply, len, "%-4s ", (count > 0) ? "O" : " ");
1781 TDM_SNPRINTF(reply, len, "%-8d ", v->last_interval);
1782 TDM_SNPRINTF(reply, len, "%-8u ", v->last_seq);
1783 TDM_SNPRINTF(reply, len, "%-3u ", v->fps);
1784 TDM_SNPRINTF(reply, len, "%-6u ", v->ignore_global_fps);
1785 TDM_SNPRINTF(reply, len, "%-6d ", v->offset);
1786 TDM_SNPRINTF(reply, len, "%-4u ", v->enable_fake);
1787 TDM_SNPRINTF(reply, len, "%s(pid:%d)\n", (proc_name) ? proc_name : "Unknown", pid);
1789 pthread_mutex_unlock(&valid_list_lock);
1791 TDM_SNPRINTF(reply, len, "\n");
1793 /* LCOV_EXCL_STOP */