1 /**************************************************************************
5 * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
7 * Contact: Eunchul Kim <chulspro.kim@samsung.com>,
8 * JinYoung Jeon <jy0.jeon@samsung.com>,
9 * Taeheon Kim <th908.kim@samsung.com>,
10 * YoungJun Cho <yj44.cho@samsung.com>,
11 * SooChan Lim <sc1.lim@samsung.com>,
12 * Boram Park <sc1.lim@samsung.com>
14 * Permission is hereby granted, free of charge, to any person obtaining a
15 * copy of this software and associated documentation files (the
16 * "Software"), to deal in the Software without restriction, including
17 * without limitation the rights to use, copy, modify, merge, publish,
18 * distribute, sub license, and/or sell copies of the Software, and to
19 * permit persons to whom the Software is furnished to do so, subject to
20 * the following conditions:
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
27 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
29 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
30 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
31 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
32 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 **************************************************************************/
41 #include "tdm_private.h"
45 * Basically tdm vblank doesn't care about thread things.
46 * However, to use tdm_event_loop_xxx functions, have to use the internal function.
47 * So need to lock/unlock the mutex of private_display. And valid_vblank_list and
48 * valid_wait_list should be protected by valid_list_lock because tdm_vblank can
49 * be used in multi-thread.
53 * - aligned by HW vblank
54 * - use a tdm_event_loop_source object only.
57 #define TDM_VBLANK_DEFAULT_VREFRESH 30
59 /* We expact "(unsigned int)(0.0016667 / 0.0016667) = 1". But it becomes 0. */
60 #define TDM_TIME_MARGIN 0.0000001
62 #define TDM_VBLANK_UINT_MAX 4294967295U
63 #define TDM_VBLANK_UINT_1Q 1073741823U /* UINT_MAX / 4 */
64 #define TDM_VBLANK_UINT_3Q 3221225471U /* UINT_MAX / 4 * 3 */
66 /* (seq < TDM_VBLANK_UINT_1Q && TDM_VBLANK_UINT_3Q < last_seq) means
67 * that the sequence value has reached the max value and started from 0 again.
69 #define TDM_VBLANK_SEQ_REACHED_MAX(seq, last_seq) \
70 ((seq) < TDM_VBLANK_UINT_1Q && TDM_VBLANK_UINT_3Q < (last_seq))
72 #define VER(fmt, arg...) TDM_ERR("[%p] "fmt, private_vblank, ##arg)
73 #define VWR(fmt, arg...) TDM_WRN("[%p] "fmt, private_vblank, ##arg)
74 #define VIN(fmt, arg...) do { if (tdm_debug_module & TDM_DEBUG_VBLANK) TDM_INFO("[%p] "fmt, private_vblank, ##arg); } while (0)
75 #define VDB(fmt, arg...) do { if (tdm_debug_module & TDM_DEBUG_VBLANK) TDM_DBG("[%p] "fmt, private_vblank, ##arg); } while (0)
82 } tdm_vblank_wait_type;
85 VBLANK_EVENT_TYPE_NONE,
88 } tdm_vblank_event_type;
90 typedef struct _tdm_vblank_wait_info tdm_vblank_wait_info;
92 typedef struct _tdm_private_vblank {
93 struct list_head valid_link;
100 tdm_output_conn_status connection;
101 unsigned int vrefresh;
103 tdm_vblank_event_type last_type;
104 unsigned int check_HW_or_SW;
105 char name[TDM_NAME_LEN];
107 unsigned int fps_changeable;
109 unsigned int enable_fake;
110 unsigned int ignore_global_fps;
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;
129 } tdm_private_vblank;
131 struct _tdm_vblank_wait_info {
132 struct list_head link;
133 struct list_head valid_link;
138 unsigned int interval;
140 tdm_vblank_handler func;
142 tdm_private_vblank *private_vblank;
144 tdm_vblank_wait_type type;
147 unsigned int target_seq;
150 /* valid_vblank_list and valid_wait_list should be protected by valid_list_lock because
151 * tdm_vblank can be used in multi-thread.
153 static pthread_mutex_t valid_list_lock;
154 static struct list_head valid_vblank_list;
155 static struct list_head valid_wait_list;
156 static unsigned int vblank_list_inited;
157 static unsigned int vblank_global_fps;
158 static double stamp = 0;
160 static tdm_error _tdm_vblank_cb_vblank_SW(void *user_data);
161 static tdm_error _tdm_vblank_wait_SW(tdm_vblank_wait_info *wait_info);
162 static void _tdm_vblank_cb_vblank_HW(tdm_output *output, unsigned int sequence,
163 unsigned int tv_sec, unsigned int tv_usec,
168 _print_list(struct list_head *list)
170 tdm_vblank_wait_info *w = NULL;
172 LIST_FOR_EACH_ENTRY(w, list, link) {
173 printf(" %d", w->interval);
180 _tdm_vblank_valid_list_add(struct list_head *valid_link, struct list_head *valid_list)
182 pthread_mutex_lock(&valid_list_lock);
183 LIST_ADDTAIL(valid_link, valid_list);
184 pthread_mutex_unlock(&valid_list_lock);
188 _tdm_vblank_valid_list_del(struct list_head *valid_link)
190 pthread_mutex_lock(&valid_list_lock);
191 LIST_DEL(valid_link);
192 pthread_mutex_unlock(&valid_list_lock);
195 static inline tdm_private_vblank*
196 _tdm_vblank_find(double vblank_stamp)
198 tdm_private_vblank *v = NULL;
203 pthread_mutex_lock(&valid_list_lock);
204 LIST_FOR_EACH_ENTRY(v, &valid_vblank_list, valid_link) {
205 if (v->stamp == vblank_stamp) {
206 pthread_mutex_unlock(&valid_list_lock);
210 pthread_mutex_unlock(&valid_list_lock);
215 static inline unsigned int
216 _tdm_vblank_check_valid_wait(tdm_vblank_wait_info *wait_info)
218 tdm_vblank_wait_info *w = NULL;
223 pthread_mutex_lock(&valid_list_lock);
224 LIST_FOR_EACH_ENTRY(w, &valid_wait_list, valid_link) {
225 if (w == wait_info) {
226 pthread_mutex_unlock(&valid_list_lock);
230 pthread_mutex_unlock(&valid_list_lock);
235 static inline unsigned int
236 _tdm_vblank_find_wait(tdm_vblank_wait_info *wait_info, struct list_head *list)
238 tdm_vblank_wait_info *w = NULL;
243 LIST_FOR_EACH_ENTRY(w, list, link) {
244 if (w->stamp == wait_info->stamp)
252 _tdm_vblank_insert_wait(tdm_vblank_wait_info *wait_info, struct list_head *list)
254 tdm_vblank_wait_info *w = NULL;
255 tdm_vblank_wait_info *found = NULL;
257 if (LIST_IS_EMPTY(list)) {
258 LIST_ADDTAIL(&wait_info->link, list);
262 LIST_FOR_EACH_ENTRY(w, list, link) {
263 if (wait_info->type == VBLANK_TYPE_SW) {
264 if (wait_info->target_time == 0)
265 TDM_NEVER_GET_HERE();
266 if (w->target_time <= wait_info->target_time) {
271 if (w->interval <= wait_info->interval) {
279 LIST_ADD(&wait_info->link, &found->link);
281 LIST_ADDTAIL(&wait_info->link, list->next);
285 _tdm_vblank_change_to_SW(tdm_private_vblank *private_vblank)
287 tdm_vblank_wait_info *w = NULL, *ww = NULL;
289 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->HW_wait_list, link) {
292 tdm_display_lock(private_vblank->dpy);
293 tdm_output_remove_vblank_handler_internal(private_vblank->output, _tdm_vblank_cb_vblank_HW, w);
294 tdm_display_unlock(private_vblank->dpy);
296 w->type = VBLANK_TYPE_SW_FAKE;
297 _tdm_vblank_wait_SW(w);
302 _tdm_vblank_free_HW_wait(tdm_private_vblank *private_vblank, tdm_error error, unsigned int call_cb)
304 tdm_vblank_wait_info *w = NULL, *ww = NULL;
306 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->HW_wait_list, link) {
308 _tdm_vblank_valid_list_del(&w->valid_link);
310 tdm_display_lock(private_vblank->dpy);
311 tdm_output_remove_vblank_handler_internal(private_vblank->output, _tdm_vblank_cb_vblank_HW, w);
312 tdm_display_unlock(private_vblank->dpy);
314 VDB("wait(%p) last(%.6f) sequence(%u) done (error:%d, call_cb:%u)",
315 w, 0, w->target_seq, error, call_cb);
317 if (call_cb && w->func)
318 w->func(private_vblank, error, 0, 0, 0, w->user_data);
325 _tdm_vblank_update_output_info(tdm_private_vblank *private_vblank)
327 tdm_output *output = private_vblank->output;
328 tdm_output_conn_status connection = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
329 unsigned int vrefresh = 0;
332 ret = tdm_output_get_conn_status(output, &connection);
333 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
335 if (connection != TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
336 const tdm_output_mode *mode = NULL;
337 tdm_output_get_mode(output, &mode);
339 vrefresh = mode->vrefresh;
341 VWR("mode not setted!!!");
345 VWR("vrefresh can't be zero !!!");
346 vrefresh = TDM_VBLANK_DEFAULT_VREFRESH;
349 private_vblank->connection = connection;
350 private_vblank->vrefresh = vrefresh;
351 private_vblank->HW_vblank_gap = 1.0 / private_vblank->vrefresh;
353 if (private_vblank->fps_changeable)
354 private_vblank->fps = vrefresh;
356 VIN("connection(%d) vrefresh(%d) fps(%d) fps_changeable(%d)",
357 private_vblank->connection,
358 private_vblank->vrefresh, private_vblank->fps, private_vblank->fps_changeable);
362 _tdm_vblank_cb_output_change(tdm_output *output, tdm_output_change_type type,
363 tdm_value value, void *user_data)
365 tdm_private_vblank *private_vblank = user_data;
367 TDM_RETURN_IF_FAIL(private_vblank != NULL);
370 case TDM_OUTPUT_CHANGE_DPMS:
371 VIN("dpms %s", tdm_dpms_str(value.u32));
372 private_vblank->check_HW_or_SW = 1;
373 if (value.u32 != TDM_OUTPUT_DPMS_ON) {
374 if (private_vblank->enable_fake)
375 _tdm_vblank_change_to_SW(private_vblank);
377 _tdm_vblank_free_HW_wait(private_vblank, TDM_ERROR_DPMS_OFF, 1);
380 case TDM_OUTPUT_CHANGE_CONNECTION:
381 if (private_vblank->connection == value.u32)
383 VIN("output %s", tdm_status_str(value.u32));
384 _tdm_vblank_update_output_info(private_vblank);
385 private_vblank->check_HW_or_SW = 1;
386 if (private_vblank->connection == TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
387 if (private_vblank->enable_fake)
388 _tdm_vblank_change_to_SW(private_vblank);
390 _tdm_vblank_free_HW_wait(private_vblank, TDM_ERROR_OUTPUT_DISCONNECTED, 1);
399 tdm_vblank_set_client_vblank_fps(unsigned int pid, const char *name, unsigned int fps)
401 tdm_private_vblank *v = NULL;
403 TDM_RETURN_VAL_IF_FAIL(pid > 0, TDM_ERROR_INVALID_PARAMETER);
404 TDM_RETURN_VAL_IF_FAIL(fps > 0, TDM_ERROR_INVALID_PARAMETER);
406 TDM_INFO("pid: %u, name: %s, fps: %d", pid, name, fps);
408 pthread_mutex_lock(&valid_list_lock);
409 LIST_FOR_EACH_ENTRY(v, &valid_vblank_list, valid_link) {
410 struct wl_client *client;
411 pid_t client_pid = 0;
416 client = wl_resource_get_client(v->resource);
420 wl_client_get_credentials(client, &client_pid, NULL, NULL);
422 if (client_pid != pid)
425 if (name && strncmp(name, TDM_VBLANK_DEFAULT_NAME, TDM_NAME_LEN)) {
426 if (strncmp(v->name, name, TDM_NAME_LEN))
430 tdm_vblank_set_fps(v, fps);
432 TDM_INFO("(pid:%u) '%s' fps changed: %d", pid, v->name, fps);
434 pthread_mutex_unlock(&valid_list_lock);
436 return TDM_ERROR_NONE;
440 tdm_vblank_enable_global_fps(unsigned int enable, unsigned int fps)
442 tdm_private_vblank *v = NULL;
445 TDM_RETURN_VAL_IF_FAIL(fps > 0, TDM_ERROR_INVALID_PARAMETER);
447 TDM_INFO("global fps: %s fps(%d)", (enable) ? "enabled" : "disabled", fps);
449 pthread_mutex_lock(&valid_list_lock);
451 vblank_global_fps = (enable) ? fps : 0;
453 LIST_FOR_EACH_ENTRY(v, &valid_vblank_list, valid_link) {
454 /* skip if vblank is created in server side */
458 v->check_HW_or_SW = 1;
460 pthread_mutex_unlock(&valid_list_lock);
462 return TDM_ERROR_NONE;
466 tdm_vblank_create(tdm_display *dpy, tdm_output *output, tdm_error *error)
468 tdm_private_vblank *private_vblank;
471 TDM_RETURN_VAL_IF_FAIL_WITH_ERROR(dpy != NULL, TDM_ERROR_INVALID_PARAMETER, NULL);
472 TDM_RETURN_VAL_IF_FAIL_WITH_ERROR(output != NULL, TDM_ERROR_INVALID_PARAMETER, NULL);
475 *error = TDM_ERROR_NONE;
477 if (!vblank_list_inited) {
478 if (pthread_mutex_init(&valid_list_lock, NULL)) {
479 TDM_ERR("mutex init failed: %m");
481 *error = TDM_ERROR_OPERATION_FAILED;
484 LIST_INITHEAD(&valid_vblank_list);
485 LIST_INITHEAD(&valid_wait_list);
486 vblank_list_inited = 1;
489 private_vblank = calloc(1, sizeof * private_vblank);
490 if (!private_vblank) {
492 *error = TDM_ERROR_OUT_OF_MEMORY;
497 tdm_output_add_change_handler(output, _tdm_vblank_cb_output_change, private_vblank);
499 private_vblank->stamp = ++stamp;
500 private_vblank->owner_tid = syscall(SYS_gettid);
501 private_vblank->dpy = dpy;
502 private_vblank->output = output;
503 private_vblank->check_HW_or_SW = 1;
504 private_vblank->fps_changeable = 1;
505 private_vblank->last_interval = -1;
507 _tdm_vblank_update_output_info(private_vblank);
509 strncpy(private_vblank->name, TDM_VBLANK_DEFAULT_NAME, TDM_NAME_LEN - 1);
510 private_vblank->name[TDM_NAME_LEN - 1] = '\0';
512 LIST_INITHEAD(&private_vblank->HW_wait_list);
513 LIST_INITHEAD(&private_vblank->SW_wait_list);
515 _tdm_vblank_valid_list_add(&private_vblank->valid_link, &valid_vblank_list);
517 VIN("created. vrefresh(%d) connection(%d)",
518 private_vblank->vrefresh, private_vblank->connection);
520 return (tdm_vblank *)private_vblank;
524 tdm_vblank_destroy(tdm_vblank *vblank)
526 tdm_private_vblank *private_vblank = vblank;
527 tdm_vblank_wait_info *w = NULL, *ww = NULL;
532 _tdm_vblank_valid_list_del(&private_vblank->valid_link);
534 if (private_vblank->SW_timer) {
535 tdm_display_lock(private_vblank->dpy);
536 tdm_event_loop_source_remove(private_vblank->SW_timer);
537 tdm_display_unlock(private_vblank->dpy);
540 tdm_output_remove_change_handler(private_vblank->output,
541 _tdm_vblank_cb_output_change, private_vblank);
543 _tdm_vblank_free_HW_wait(private_vblank, 0, 0);
545 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->SW_wait_list, link) {
547 _tdm_vblank_valid_list_del(&w->valid_link);
553 free(private_vblank);
557 tdm_vblank_set_name(tdm_vblank *vblank, const char *name)
559 tdm_private_vblank *private_vblank = vblank;
561 TDM_RETURN_VAL_IF_FAIL(private_vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
564 name = TDM_VBLANK_DEFAULT_NAME;
566 strncpy(private_vblank->name, name, TDM_NAME_LEN - 1);
567 private_vblank->name[TDM_NAME_LEN - 1] = '\0';
569 VIN("name(%s)", name);
571 return TDM_ERROR_NONE;
575 tdm_vblank_get_name(tdm_vblank *vblank, const char **name)
577 tdm_private_vblank *private_vblank = vblank;
579 TDM_RETURN_VAL_IF_FAIL(private_vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
580 TDM_RETURN_VAL_IF_FAIL(name != NULL, TDM_ERROR_INVALID_PARAMETER);
582 *name = (const char*)private_vblank->name;
584 return TDM_ERROR_NONE;
588 tdm_vblank_set_fps(tdm_vblank *vblank, unsigned int fps)
590 tdm_private_vblank *private_vblank = vblank;
592 TDM_RETURN_VAL_IF_FAIL(private_vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
593 TDM_RETURN_VAL_IF_FAIL(fps > 0, TDM_ERROR_INVALID_PARAMETER);
595 if (private_vblank->fps != fps || private_vblank->fps_changeable)
596 VIN("fps(%d) fps_changeable(0)", fps);
598 private_vblank->fps_changeable = 0;
600 if (private_vblank->fps == fps)
601 return TDM_ERROR_NONE;
603 private_vblank->fps = fps;
604 private_vblank->check_HW_or_SW = 1;
606 return TDM_ERROR_NONE;
610 tdm_vblank_get_fps(tdm_vblank *vblank, unsigned int *fps)
612 tdm_private_vblank *private_vblank = vblank;
614 TDM_RETURN_VAL_IF_FAIL(private_vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
615 TDM_RETURN_VAL_IF_FAIL(fps != NULL, TDM_ERROR_INVALID_PARAMETER);
617 *fps = private_vblank->fps;
619 return TDM_ERROR_NONE;
623 tdm_vblank_ignore_global_fps(tdm_vblank *vblank, unsigned int ignore)
625 tdm_private_vblank *private_vblank = vblank;
627 TDM_RETURN_VAL_IF_FAIL(private_vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
629 if (private_vblank->ignore_global_fps == ignore)
630 return TDM_ERROR_NONE;
632 private_vblank->ignore_global_fps = ignore;
633 private_vblank->check_HW_or_SW = 1;
635 VIN("ignore_global_fps(%d)", private_vblank->ignore_global_fps);
637 return TDM_ERROR_NONE;
641 tdm_vblank_set_offset(tdm_vblank *vblank, int offset)
643 tdm_private_vblank *private_vblank = vblank;
645 TDM_RETURN_VAL_IF_FAIL(private_vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
647 if (private_vblank->offset == offset)
648 return TDM_ERROR_NONE;
650 private_vblank->offset = offset;
651 private_vblank->check_HW_or_SW = 1;
653 VIN("offset(%d)", private_vblank->offset);
655 return TDM_ERROR_NONE;
659 tdm_vblank_get_offset(tdm_vblank *vblank, int *offset)
661 tdm_private_vblank *private_vblank = vblank;
663 TDM_RETURN_VAL_IF_FAIL(private_vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
664 TDM_RETURN_VAL_IF_FAIL(offset != NULL, TDM_ERROR_INVALID_PARAMETER);
666 *offset = private_vblank->offset;
668 return TDM_ERROR_NONE;
672 tdm_vblank_set_enable_fake(tdm_vblank *vblank, unsigned int enable_fake)
674 tdm_private_vblank *private_vblank = vblank;
676 TDM_RETURN_VAL_IF_FAIL(private_vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
678 if (private_vblank->enable_fake == enable_fake)
679 return TDM_ERROR_NONE;
681 private_vblank->enable_fake = enable_fake;
683 VIN("enable_fake(%d)", private_vblank->enable_fake);
685 return TDM_ERROR_NONE;
689 tdm_vblank_get_enable_fake(tdm_vblank *vblank, unsigned int *enable_fake)
691 tdm_private_vblank *private_vblank = vblank;
693 TDM_RETURN_VAL_IF_FAIL(private_vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
694 TDM_RETURN_VAL_IF_FAIL(enable_fake != NULL, TDM_ERROR_INVALID_PARAMETER);
696 *enable_fake = private_vblank->enable_fake;
698 return TDM_ERROR_NONE;
702 _tdm_vblank_sw_timer_update(tdm_private_vblank *private_vblank)
704 tdm_vblank_wait_info *first_wait_info = NULL;
709 if (LIST_IS_EMPTY(&private_vblank->SW_wait_list)) {
711 return TDM_ERROR_OPERATION_FAILED;
714 first_wait_info = container_of(private_vblank->SW_wait_list.next, first_wait_info, link);
715 curr = tdm_helper_get_time();
716 target = first_wait_info->target_time;
718 /* ms_delay should be more that 1 */
722 ms_delay = ceil((target - curr) * 1000.0);
728 VER("wait(%p) curr(%.6f) target(%.6f) ms_delay(%d)", first_wait_info, curr, target, ms_delay);
730 VDB("wait(%p) curr(%.6f) target(%.6f) ms_delay(%d)", first_wait_info, curr, target, ms_delay);
732 tdm_display_lock(private_vblank->dpy);
734 if (!private_vblank->SW_timer) {
735 private_vblank->SW_timer =
736 tdm_event_loop_add_timer_handler(private_vblank->dpy,
737 _tdm_vblank_cb_vblank_SW,
740 if (!private_vblank->SW_timer) {
741 tdm_display_unlock(private_vblank->dpy);
742 VER("couldn't add timer");
745 VIN("Create SW timer");
748 private_vblank->last_interval = ms_delay;
750 ret = tdm_event_loop_source_timer_update(private_vblank->SW_timer, ms_delay);
751 if (ret != TDM_ERROR_NONE) {
752 tdm_display_unlock(private_vblank->dpy);
753 VER("couldn't update timer");
757 tdm_display_unlock(private_vblank->dpy);
759 return TDM_ERROR_NONE;
763 _tdm_vblank_cb_vblank_HW(tdm_output *output, unsigned int sequence,
764 unsigned int tv_sec, unsigned int tv_usec,
767 tdm_vblank_wait_info *wait_info = user_data;
768 tdm_private_vblank *private_vblank;
770 if (!_tdm_vblank_check_valid_wait(wait_info)) {
771 TDM_DBG("can't find wait(%p) from valid_wait_list", wait_info);
775 private_vblank = wait_info->private_vblank;
776 TDM_RETURN_IF_FAIL(private_vblank != NULL);
778 if (!_tdm_vblank_find_wait(wait_info, &private_vblank->HW_wait_list)) {
779 VDB("can't find wait(%p)", wait_info);
783 if (wait_info->type == VBLANK_TYPE_HW_SW) {
786 LIST_DEL(&wait_info->link);
788 /* kernel sends the wrong time of first vblank event. and the time of
789 * second vblank is correct.
791 wait_info->target_time = TDM_TIME(tv_sec, tv_usec);
792 wait_info->target_time += (((double)private_vblank->offset) / 1000.0);
794 _tdm_vblank_insert_wait(wait_info, &private_vblank->SW_wait_list);
796 ret = _tdm_vblank_sw_timer_update(private_vblank);
798 /* wait_info will be freed in _tdm_vblank_cb_vblank_SW() */
799 if (ret == TDM_ERROR_NONE) {
800 VDB("wait(%p) SW timer", wait_info);
804 VWR("couldn't update sw timer");
807 LIST_DEL(&wait_info->link);
808 _tdm_vblank_valid_list_del(&wait_info->valid_link);
810 /* tv_sec & tv_usec shouldn't be zero. But sometimes they are zero in some hardware.
811 * We need to prohibit that last_time becomes zero because "last_time == 0" means
812 * that a tdm_vblank object is just created and doesn't have the first vblank event.
813 * If last_time becomes 0, it would make issue.
815 if (tv_sec == 0 && tv_usec == 0) {
816 double curr = tdm_helper_get_time();
817 tv_sec = TDM_TIME_SEC(curr);
818 tv_usec = TDM_TIME_USEC(curr);
821 private_vblank->last_seq = wait_info->target_seq;
822 private_vblank->last_time = TDM_TIME(tv_sec, tv_usec);
824 VDB("wait(%p) last(%.6f) sequence(%u) done", wait_info, private_vblank->last_time, wait_info->target_seq);
827 wait_info->func(private_vblank, TDM_ERROR_NONE, wait_info->target_seq,
828 tv_sec, tv_usec, wait_info->user_data);
834 _tdm_vblank_wait_HW(tdm_vblank_wait_info *wait_info)
836 tdm_private_vblank *private_vblank = wait_info->private_vblank;
840 if (private_vblank->last_type != VBLANK_EVENT_TYPE_HW) {
841 VIN("Use HW vblank");
842 private_vblank->last_type = VBLANK_EVENT_TYPE_HW;
845 _tdm_vblank_insert_wait(wait_info, &private_vblank->HW_wait_list);
847 hw_interval = wait_info->interval * private_vblank->quotient;
849 if (private_vblank->last_time == 0) {
850 /* SW vblank starts from now. SW vblank doesn't need to be aligned with HW vblank. */
851 private_vblank->last_seq = 0;
852 private_vblank->last_time = tdm_helper_get_time();
854 /* +1 ms to call the handler ASAP at the first. no matter for SW timer. */
855 wait_info->target_seq = 1;
857 VDB("wait(%p) last(%.6f) hw_itvl(%d) targ_seq(%u)",
858 wait_info, private_vblank->last_time, hw_interval, wait_info->target_seq);
860 double last, prev, req, curr, target;
863 last = private_vblank->last_time;
864 req = wait_info->req_time;
866 /* make sure that req time is bigger than last. */
870 skip = (unsigned int)((req - last) / private_vblank->vblank_gap + TDM_TIME_MARGIN);
871 prev = last + private_vblank->vblank_gap * skip;
873 curr = tdm_helper_get_time();
875 while (target < curr)
876 target += (private_vblank->vblank_gap * wait_info->interval);
878 hw_interval = (unsigned int)((target - curr) / private_vblank->HW_vblank_gap + TDM_TIME_MARGIN) + 1;
880 wait_info->target_seq = private_vblank->last_seq;
881 wait_info->target_seq += (unsigned int)((target - last) / private_vblank->vblank_gap + TDM_TIME_MARGIN);
883 if (hw_interval > 300)
884 VER("wait(%p) last(%.6f) req(%.6f) prev(%.6f) curr(%.6f) targ(%.6f,%.6f) skip(%u) hw_itvl(%d) targ_seq(%u)",
885 wait_info, last, req - last, prev - last, curr - last, target, target - last,
886 skip, hw_interval, wait_info->target_seq);
888 VDB("wait(%p) last(%.6f) req(%.6f) prev(%.6f) curr(%.6f) targ(%.6f,%.6f) skip(%u) hw_itvl(%d) targ_seq(%u)",
889 wait_info, last, req - last, prev - last, curr - last, target, target - last,
890 skip, hw_interval, wait_info->target_seq);
893 private_vblank->last_interval = hw_interval;
895 if (private_vblank->add_front)
896 ret = tdm_output_wait_vblank_add_front(private_vblank->output, hw_interval, 0,
897 _tdm_vblank_cb_vblank_HW, wait_info);
899 ret = tdm_output_wait_vblank(private_vblank->output, hw_interval, 0,
900 _tdm_vblank_cb_vblank_HW, wait_info);
902 if (ret != TDM_ERROR_NONE) {
903 LIST_DEL(&wait_info->link);
907 VDB("wait(%p) waiting", wait_info);
909 return TDM_ERROR_NONE;
913 _tdm_vblank_cb_vblank_SW(void *user_data)
915 tdm_private_vblank *private_vblank = user_data;
917 TDM_RETURN_VAL_IF_FAIL(private_vblank != NULL, TDM_ERROR_OPERATION_FAILED);
919 return tdm_vblank_cb_vblank_SW(private_vblank, 0);
923 tdm_vblank_cb_vblank_SW(tdm_vblank *vblank, double vblank_stamp)
925 tdm_private_vblank *private_vblank;
926 tdm_vblank_wait_info *first_wait_info = NULL, *w = NULL, *ww = NULL;
928 TDM_RETURN_VAL_IF_FAIL(vblank || vblank_stamp > 0, TDM_ERROR_INVALID_PARAMETER);
931 private_vblank = vblank;
933 private_vblank = _tdm_vblank_find(vblank_stamp);
934 if (!private_vblank) {
935 TDM_DBG("can't find vblank(%.0f) from valid_list", vblank_stamp);
936 return TDM_ERROR_NONE;
940 if (private_vblank->owner_tid != syscall(SYS_gettid)) {
941 tdm_thread_cb_vblank_sw vblank_sw;
942 tdm_private_display *private_display = private_vblank->dpy;
945 vblank_sw.base.type = TDM_THREAD_CB_VBLANK_SW;
946 vblank_sw.base.length = sizeof vblank_sw;
947 vblank_sw.vblank_stamp = private_vblank->stamp;
949 ret = tdm_thread_send_cb(private_display->private_loop, &vblank_sw.base);
950 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
952 return TDM_ERROR_NONE;
955 if (LIST_IS_EMPTY(&private_vblank->SW_wait_list)) {
957 return TDM_ERROR_OPERATION_FAILED;
960 first_wait_info = container_of(private_vblank->SW_wait_list.next, first_wait_info, link);
961 TDM_RETURN_VAL_IF_FAIL(first_wait_info != NULL, TDM_ERROR_OPERATION_FAILED);
963 if (private_vblank->last_seq >= first_wait_info->target_seq)
964 TDM_ERR("last_seq(%u) target_seq(%u)", private_vblank->last_seq, first_wait_info->target_seq);
966 private_vblank->last_seq = first_wait_info->target_seq;
967 private_vblank->last_time = first_wait_info->target_time;
969 VDB("wait(%p) last(%.6f) sequence(%u) done", first_wait_info, private_vblank->last_time, first_wait_info->target_seq);
971 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->SW_wait_list, link) {
972 if (w->target_time != first_wait_info->target_time)
976 _tdm_vblank_valid_list_del(&w->valid_link);
979 tdm_display_unlock(private_vblank->dpy);
980 w->func(private_vblank, TDM_ERROR_NONE, w->target_seq,
981 TDM_TIME_SEC(w->target_time), TDM_TIME_USEC(w->target_time),
983 tdm_display_lock(private_vblank->dpy);
989 return TDM_ERROR_NONE;
993 _tdm_vblank_wait_SW(tdm_vblank_wait_info *wait_info)
995 tdm_private_vblank *private_vblank = wait_info->private_vblank;
998 if (private_vblank->last_type != VBLANK_EVENT_TYPE_SW) {
999 VIN("Use SW vblank");
1000 private_vblank->last_type = VBLANK_EVENT_TYPE_SW;
1003 if (private_vblank->last_time == 0) {
1004 /* SW vblank starts from now. SW vblank doesn't need to be aligned with HW vblank. */
1005 private_vblank->last_seq = 0;
1006 private_vblank->last_time = tdm_helper_get_time();
1008 /* +1 ms to call the handler ASAP at the first. no matter for SW timer. */
1009 wait_info->target_seq = 1;
1010 wait_info->target_time = private_vblank->last_time + 0.001;
1012 VDB("wait(%p) last(%.6f) target(%.6f) target sequence(%u)",
1013 wait_info, private_vblank->last_time, wait_info->target_time, wait_info->target_seq);
1015 double last, prev, req, curr, target;
1018 last = private_vblank->last_time;
1019 req = wait_info->req_time;
1021 /* make sure that req time is bigger than last. */
1025 skip = (unsigned int)((req - last) / private_vblank->vblank_gap + TDM_TIME_MARGIN);
1026 prev = last + private_vblank->vblank_gap * skip;
1028 curr = tdm_helper_get_time();
1029 target = prev + private_vblank->vblank_gap * wait_info->interval;
1031 while (target < curr)
1032 target += private_vblank->vblank_gap;
1034 wait_info->target_seq = private_vblank->last_seq;
1035 wait_info->target_seq += (unsigned int)((target - last) / private_vblank->vblank_gap + TDM_TIME_MARGIN);
1037 wait_info->target_time = target;
1039 VDB("wait(%p) last(%.6f) req(%.6f) prev(%.6f) curr(%.6f) targ(%.6f,%.6f) skip(%u) targ_seq(%u)",
1040 wait_info, last, req - last, prev - last, curr - last,
1041 target, target - last, skip, wait_info->target_seq);
1044 _tdm_vblank_insert_wait(wait_info, &private_vblank->SW_wait_list);
1046 ret = _tdm_vblank_sw_timer_update(private_vblank);
1047 if (ret != TDM_ERROR_NONE) {
1048 LIST_DEL(&wait_info->link);
1049 VER("couldn't update sw timer");
1053 return TDM_ERROR_NONE;
1057 tdm_vblank_wait(tdm_vblank *vblank, unsigned int req_sec, unsigned int req_usec,
1058 unsigned int interval, tdm_vblank_handler func, void *user_data)
1060 tdm_private_vblank *private_vblank = vblank;
1061 tdm_vblank_wait_info *wait_info;
1062 tdm_output_dpms dpms = TDM_OUTPUT_DPMS_OFF;
1066 TDM_RETURN_VAL_IF_FAIL(private_vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1067 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
1069 if (private_vblank->owner_tid != syscall(SYS_gettid)) {
1070 TDM_ERR("SHOULD be called in the owner thread(%d)", (int)private_vblank->owner_tid);
1071 return TDM_ERROR_BAD_REQUEST;
1074 tdm_output_get_dpms(private_vblank->output, &dpms);
1076 if (!private_vblank->enable_fake) {
1077 if (private_vblank->connection == TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
1078 VER("can't wait a vblank: output disconnected");
1079 return TDM_ERROR_OUTPUT_DISCONNECTED;
1081 if (dpms != TDM_OUTPUT_DPMS_ON) {
1082 VER("can't wait a vblank: DPMS off");
1083 return TDM_ERROR_DPMS_OFF;
1087 wait_info = calloc(1, sizeof * wait_info);
1089 VER("alloc failed");
1090 return TDM_ERROR_OUT_OF_MEMORY;
1093 /* set request time to current time if 0. This function seems to be called in server side. */
1094 if (req_sec == 0 && req_usec == 0) {
1095 double curr = tdm_helper_get_time();
1096 req_sec = TDM_TIME_SEC(curr);
1097 req_usec = TDM_TIME_USEC(curr);
1100 LIST_INITHEAD(&wait_info->link);
1102 _tdm_vblank_valid_list_add(&wait_info->valid_link, &valid_wait_list);
1104 wait_info->stamp = ++stamp;
1105 wait_info->req_time = TDM_TIME(req_sec, req_usec);
1106 wait_info->interval = interval;
1107 wait_info->func = func;
1108 wait_info->user_data = user_data;
1109 wait_info->private_vblank = private_vblank;
1111 if (private_vblank->ignore_global_fps ||
1112 vblank_global_fps == 0 ||
1113 private_vblank->fps < vblank_global_fps)
1114 fps = private_vblank->fps;
1116 fps = vblank_global_fps;
1118 if (private_vblank->check_HW_or_SW) {
1119 private_vblank->check_HW_or_SW = 0;
1120 private_vblank->vblank_gap = 1.0 / fps;
1121 private_vblank->quotient = private_vblank->vrefresh / fps;
1124 /* 1) if fps != factor of vrefresh, SW timer
1125 * 2) if fps == factor of vrefresh && dpms == off, SW timer (Fake HW vblank)
1126 * 2) if fps == factor of vrefresh && dpms == on && offset == 0, HW vblank
1127 * 3) if fps == factor of vrefresh && dpms == on && offset != 0, HW vblank + SW timer
1128 * In case of 1), we really don't need to align with HW vblank.
1130 if (private_vblank->vrefresh % fps)
1131 wait_info->type = VBLANK_TYPE_SW;
1132 else if (dpms == TDM_OUTPUT_DPMS_OFF ||
1133 private_vblank->connection == TDM_OUTPUT_CONN_STATUS_DISCONNECTED)
1134 wait_info->type = VBLANK_TYPE_SW_FAKE;
1135 else if (private_vblank->offset == 0)
1136 wait_info->type = VBLANK_TYPE_HW;
1138 wait_info->type = VBLANK_TYPE_HW_SW;
1140 if (wait_info->type == VBLANK_TYPE_SW || wait_info->type == VBLANK_TYPE_SW_FAKE)
1141 ret = _tdm_vblank_wait_SW(wait_info);
1143 ret = _tdm_vblank_wait_HW(wait_info);
1144 if (ret == TDM_ERROR_OUTPUT_DISCONNECTED || ret == TDM_ERROR_DPMS_OFF)
1145 ret = _tdm_vblank_wait_SW(wait_info);
1148 if (ret != TDM_ERROR_NONE) {
1149 LIST_DEL(&wait_info->link);
1150 _tdm_vblank_valid_list_del(&wait_info->valid_link);
1152 tdm_display_lock(private_vblank->dpy);
1153 tdm_output_remove_vblank_handler_internal(private_vblank->output, _tdm_vblank_cb_vblank_HW, wait_info);
1154 tdm_display_unlock(private_vblank->dpy);
1160 return TDM_ERROR_NONE;
1164 tdm_vblank_wait_seq(tdm_vblank *vblank, unsigned int req_sec, unsigned int req_usec,
1165 unsigned int sequence, tdm_vblank_handler func, void *user_data)
1167 tdm_private_vblank *private_vblank = vblank;
1168 unsigned int interval;
1170 TDM_RETURN_VAL_IF_FAIL(private_vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1171 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
1173 /* if the sequence of vblank reaches the max value, sequence can be 0. */
1175 if (private_vblank->last_seq == 0) {
1176 TDM_WRN("can't calculate interval with sequence(%u)", sequence);
1178 } else if (sequence > private_vblank->last_seq ||
1179 TDM_VBLANK_SEQ_REACHED_MAX(sequence, private_vblank->last_seq)) {
1180 double last, curr, seq_target;
1182 last = private_vblank->last_time;
1183 curr = tdm_helper_get_time();
1184 seq_target = last + private_vblank->vblank_gap * (sequence - private_vblank->last_seq);
1186 /* If target is old or too close(1ms) to current, we call the handler immediately.
1187 * 2ms? it seems too much.
1189 if (seq_target < curr) {
1190 func(vblank, TDM_ERROR_NONE, sequence,
1191 TDM_TIME_SEC(seq_target), TDM_TIME_USEC(seq_target),
1193 return TDM_ERROR_NONE;
1194 } else if ((seq_target - curr) < 0.001) { /* 1ms */
1195 func(vblank, TDM_ERROR_NONE, sequence,
1196 TDM_TIME_SEC(curr), TDM_TIME_USEC(curr),
1198 return TDM_ERROR_NONE;
1201 interval = (unsigned int)((seq_target - curr) / private_vblank->vblank_gap + TDM_TIME_MARGIN) + 1;
1203 /* this seems error. But we handle this like normal to avoid the unexpected error in cliend side */
1204 TDM_WRN("sequence(%u) should be over the last sequence(%u)", sequence, private_vblank->last_seq);
1205 func(vblank, TDM_ERROR_NONE, private_vblank->last_seq,
1206 TDM_TIME_SEC(private_vblank->last_time), TDM_TIME_USEC(private_vblank->last_time),
1208 return TDM_ERROR_NONE;
1211 return tdm_vblank_wait(vblank, req_sec, req_usec, interval, func, user_data);
1215 tdm_vblank_set_add_front(tdm_vblank *vblank, unsigned int add_front)
1217 tdm_private_vblank *private_vblank = vblank;
1219 TDM_RETURN_VAL_IF_FAIL(private_vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1221 TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
1223 if (private_vblank->add_front == add_front)
1224 return TDM_ERROR_NONE;
1226 private_vblank->add_front = add_front;
1228 return TDM_ERROR_NONE;
1232 tdm_vblank_set_resource(tdm_vblank *vblank, struct wl_resource *resource)
1234 tdm_private_vblank *private_vblank = vblank;
1236 TDM_RETURN_VAL_IF_FAIL(private_vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1237 TDM_RETURN_VAL_IF_FAIL(private_vblank->resource == NULL, TDM_ERROR_OPERATION_FAILED);
1239 private_vblank->resource = resource;
1241 return TDM_ERROR_NONE;
1245 tdm_vblank_get_stamp(tdm_vblank *vblank)
1247 tdm_private_vblank *private_vblank = vblank;
1249 TDM_RETURN_VAL_IF_FAIL(private_vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1251 return private_vblank->stamp;
1255 _tdm_vblank_get_waiting_count(tdm_private_vblank *private_vblank)
1257 tdm_vblank_wait_info *wait_info = NULL;
1258 unsigned int count = 0;
1260 LIST_FOR_EACH_ENTRY(wait_info, &private_vblank->HW_wait_list, link) {
1264 LIST_FOR_EACH_ENTRY(wait_info, &private_vblank->SW_wait_list, link) {
1272 tdm_vblank_get_vblank_list_information(tdm_display *dpy, char *reply, int *len)
1274 tdm_private_vblank *v = NULL;
1275 tdm_output_dpms dpms = TDM_OUTPUT_DPMS_OFF;
1276 tdm_output *output = tdm_display_get_output(dpy, 0, NULL);
1279 tdm_output_get_dpms(output, &dpms);
1281 TDM_SNPRINTF(reply, len, "[Client Vblank List]\n");
1282 TDM_SNPRINTF(reply, len, "* DPMS: %s\n", tdm_dpms_str(dpms));
1283 TDM_SNPRINTF(reply, len, "* global fps: %u\n", vblank_global_fps);
1284 TDM_SNPRINTF(reply, len, "----------------------------------------------------------------------------------\n");
1285 TDM_SNPRINTF(reply, len, "Name Type Wait Interval Sequence Fps Ignore Offset Fake Process\n");
1286 TDM_SNPRINTF(reply, len, "----------------------------------------------------------------------------------\n");
1288 pthread_mutex_lock(&valid_list_lock);
1289 LIST_FOR_EACH_ENTRY(v, &valid_vblank_list, valid_link) {
1290 const char *proc_name = NULL;
1295 struct wl_client *client = wl_resource_get_client(v->resource);
1297 wl_client_get_credentials(client, &pid, NULL, NULL);
1298 proc_name = tdm_server_get_client_name(pid);
1302 count = _tdm_vblank_get_waiting_count(v);
1304 TDM_SNPRINTF(reply, len, "%-8s(%p) ", v->name, v);
1305 TDM_SNPRINTF(reply, len, "%-4s ", (v->last_type == VBLANK_EVENT_TYPE_HW) ? "HW" : "SW");
1306 TDM_SNPRINTF(reply, len, "%-4s ", (count > 0) ? "O" : " ");
1307 TDM_SNPRINTF(reply, len, "%-8d ", v->last_interval);
1308 TDM_SNPRINTF(reply, len, "%-8u ", v->last_seq);
1309 TDM_SNPRINTF(reply, len, "%-3u ", v->fps);
1310 TDM_SNPRINTF(reply, len, "%-6u ", v->ignore_global_fps);
1311 TDM_SNPRINTF(reply, len, "%-6d ", v->offset);
1312 TDM_SNPRINTF(reply, len, "%-4u ", v->enable_fake);
1313 TDM_SNPRINTF(reply, len, "%s(pid:%d)\n", (proc_name) ? proc_name : "Unknown", pid);
1315 pthread_mutex_unlock(&valid_list_lock);
1317 TDM_SNPRINTF(reply, len, "\n");