tdm_config: add TDM_CONFIG_KEY_GENERAL_HWC_COMMIT_USE_GLOBAL_FPS config define
[platform/core/uifw/libtdm.git] / src / tdm_vblank.c
1 /**************************************************************************
2  *
3  * libtdm
4  *
5  * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
6  *
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>
13  *
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:
21  *
22  * The above copyright notice and this permission notice (including the
23  * next paragraph) shall be included in all copies or substantial portions
24  * of the Software.
25  *
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.
33  *
34 **************************************************************************/
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include "tdm_private.h"
41
42 /* CAUTION:
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.
48  */
49
50 /* TDM vblank
51  * - aligned by HW vblank
52  * - use a tdm_event_loop_source object only.
53  */
54
55 #define TDM_VBLANK_DEFAULT_VREFRESH    30
56
57 /* We expact "(unsigned int)(0.0016667 / 0.0016667) = 1". But it becomes 0. */
58 #define TDM_TIME_MARGIN     0.0000001
59
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 */
63
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.
66  */
67 #define TDM_VBLANK_SEQ_REACHED_MAX(seq, last_seq) \
68         ((seq) < TDM_VBLANK_UINT_1Q && TDM_VBLANK_UINT_3Q < (last_seq))
69
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)
74
75 typedef enum {
76         VBLANK_TYPE_SW,
77         VBLANK_TYPE_SW_FAKE,
78         VBLANK_TYPE_HW,
79         VBLANK_TYPE_HW_SW,
80 } tdm_vblank_wait_type;
81
82 typedef enum {
83         VBLANK_EVENT_TYPE_NONE,
84         VBLANK_EVENT_TYPE_SW,
85         VBLANK_EVENT_TYPE_HW,
86 } tdm_vblank_event_type;
87
88 typedef struct _tdm_vblank_wait_info tdm_vblank_wait_info;
89
90 typedef struct _tdm_private_vblank {
91         struct list_head valid_link;
92
93         double stamp;
94         pid_t owner_tid;
95
96         tdm_display *dpy;
97         tdm_output *output;
98         tdm_output_conn_status connection;
99         unsigned int vrefresh;
100
101         tdm_vblank_event_type last_type;
102         unsigned int check_HW_or_SW;
103         char name[TDM_NAME_LEN];
104         unsigned int fps;
105         unsigned int fps_changeable;
106         unsigned int fps_fixed;
107         int offset;
108         unsigned int enable_fake;
109         unsigned int ignore_global_fps;
110         unsigned int in_create_handler;
111
112         double vblank_gap;
113         unsigned int quotient;
114
115         int last_interval;
116         unsigned int last_seq;
117         double last_time;
118
119         unsigned int add_front;
120         struct wl_resource *resource;
121
122         /* for HW */
123         double HW_vblank_gap;
124         struct list_head HW_wait_list;
125
126         /* for SW */
127         tdm_event_loop_source *SW_timer;
128         struct list_head SW_wait_list;
129
130         /* for timeout */
131         tdm_event_loop_source *timeout_timer;
132 } tdm_private_vblank;
133
134 struct _tdm_vblank_wait_info {
135         struct list_head link;
136         struct list_head valid_link;
137
138         double stamp;
139
140         double req_time;
141         unsigned int interval;
142         int hw_interval;
143
144         tdm_vblank_handler func;
145         void *user_data;
146         tdm_private_vblank *private_vblank;
147
148         tdm_vblank_wait_type type;
149
150         double target_time;
151         unsigned int target_seq;
152 };
153
154 typedef struct _tdm_vblank_create_handler_info {
155         struct list_head link;
156
157         tdm_vblank_create_handler func;
158         void *user_data;
159 } tdm_vblank_create_handler_info;
160
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.
163  */
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;
172
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,
178                                                                          void *user_data);
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);
182
183 #if 0
184 static void
185 _print_list(tdm_private_vblank *private_vblank)
186 {
187         tdm_vblank_wait_info *w = NULL;
188         int count = 0;
189
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);
193                 count++;
194         }
195
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);
199                 count++;
200         }
201
202         if (!count)
203                 VIN("NO_wait");
204 }
205 #endif
206
207 static int
208 tdm_vblank_is_valid(tdm_vblank *vblank)
209 {
210         tdm_private_vblank *v = NULL;
211
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);
217                                 return 1;
218                         }
219                 }
220                 pthread_mutex_unlock(&valid_list_lock);
221
222         }
223         return 0;
224 }
225
226 static void
227 _tdm_vblank_valid_list_add(struct list_head *valid_link, struct list_head *valid_list)
228 {
229         pthread_mutex_lock(&valid_list_lock);
230         LIST_ADDTAIL(valid_link, valid_list);
231         pthread_mutex_unlock(&valid_list_lock);
232 }
233
234 static void
235 _tdm_vblank_valid_list_del(struct list_head *valid_link)
236 {
237         pthread_mutex_lock(&valid_list_lock);
238         LIST_DEL(valid_link);
239         pthread_mutex_unlock(&valid_list_lock);
240 }
241
242 static inline void*
243 _tdm_vblank_find_object(tdm_private_display *private_display, double vblank_stamp)
244 {
245         tdm_private_vblank *v = NULL;
246
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);
251                         return v;
252                 }
253         }
254         pthread_mutex_unlock(&valid_list_lock);
255
256         return NULL;
257 }
258
259 static inline unsigned int
260 _tdm_vblank_check_valid_wait(tdm_vblank_wait_info *wait_info)
261 {
262         tdm_vblank_wait_info *w = NULL;
263
264         if (!wait_info)
265                 return 0;
266
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);
271                         return 1;
272                 }
273         }
274         pthread_mutex_unlock(&valid_list_lock);
275
276         return 0;
277 }
278
279 static inline unsigned int
280 _tdm_vblank_find_wait(tdm_vblank_wait_info *wait_info, struct list_head *list)
281 {
282         tdm_vblank_wait_info *w = NULL;
283
284         if (!wait_info)
285                 return 0;
286
287         LIST_FOR_EACH_ENTRY(w, list, link) {
288                 if (w->stamp == wait_info->stamp)
289                         return 1;
290         }
291
292         return 0;
293 }
294
295 static inline void
296 _tdm_vblank_insert_wait(tdm_vblank_wait_info *wait_info, struct list_head *list)
297 {
298         tdm_vblank_wait_info *w = NULL;
299         tdm_vblank_wait_info *found = NULL;
300
301         if (LIST_IS_EMPTY(list)) {
302                 LIST_ADDTAIL(&wait_info->link, list);
303                 return;
304         }
305
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) {
311                                 found = w;
312                                 continue;
313                         }
314                 } else {
315                         if (w->interval <= wait_info->interval) {
316                                 found = w;
317                                 continue;
318                         }
319                 }
320         }
321
322         if (found)
323                 LIST_ADD(&wait_info->link, &found->link);
324         else
325                 LIST_ADDTAIL(&wait_info->link, list);
326 }
327
328 static void
329 _tdm_vblank_change_to_SW(tdm_private_vblank *private_vblank)
330 {
331         tdm_vblank_wait_info *w = NULL, *ww = NULL;
332
333         LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->HW_wait_list, link) {
334                 LIST_DEL(&w->link);
335
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);
339
340                 w->type = VBLANK_TYPE_SW_FAKE;
341                 _tdm_vblank_wait_SW(w);
342         }
343 }
344
345 static void
346 _tdm_vblank_free_HW_wait(tdm_private_vblank *private_vblank, tdm_error error, unsigned int call_cb)
347 {
348         tdm_vblank_wait_info *w = NULL, *ww = NULL;
349
350         if (!LIST_IS_EMPTY(&private_vblank->HW_wait_list)) {
351                 pid_t pid;
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);
355         }
356
357         LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->HW_wait_list, link) {
358                 LIST_DEL(&w->link);
359                 _tdm_vblank_valid_list_del(&w->valid_link);
360
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);
364
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);
367
368                 if (call_cb && w->func)
369                         w->func(private_vblank, error, 0, 0, 0, w->user_data);
370
371                 free(w);
372         }
373 }
374
375 static void
376 _tdm_vblank_update_output_info(tdm_private_vblank *private_vblank)
377 {
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;
382         tdm_error ret;
383
384         ret = tdm_output_get_conn_status(output, &connection);
385         TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
386
387         ret = tdm_output_get_mode(output, &mode);
388         if (ret == TDM_ERROR_NONE && mode)
389                 vrefresh = mode->vrefresh;
390         else
391                 VWR("mode not setted!!!");
392
393         private_vblank->connection = connection;
394         private_vblank->vrefresh = vrefresh;
395         private_vblank->HW_vblank_gap = 1.0 / private_vblank->vrefresh;
396
397         if (private_vblank->fps_changeable)
398                 private_vblank->fps = vrefresh;
399
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);
403 }
404
405 /* LCOV_EXCL_START */
406 static void
407 _tdm_vblank_cb_output_change(tdm_output *output, tdm_output_change_type type,
408                                                          tdm_value value, void *user_data)
409 {
410         tdm_private_vblank *private_vblank = user_data;
411
412         TDM_RETURN_IF_FAIL(tdm_vblank_is_valid(private_vblank));
413
414         switch (type) {
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);
421                         else {
422                                 tdm_display_lock(private_vblank->dpy);
423                                 _tdm_vblank_timeout_timer_update(private_vblank, 0);
424                                 tdm_display_unlock(private_vblank->dpy);
425
426                                 _tdm_vblank_free_HW_wait(private_vblank, TDM_ERROR_DPMS_OFF, 1);
427                         }
428                 }
429                 break;
430         case TDM_OUTPUT_CHANGE_CONNECTION:
431                 if (private_vblank->connection == value.u32)
432                         break;
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);
439                         else {
440                                 tdm_display_lock(private_vblank->dpy);
441                                 _tdm_vblank_timeout_timer_update(private_vblank, 0);
442                                 tdm_display_unlock(private_vblank->dpy);
443
444                                 _tdm_vblank_free_HW_wait(private_vblank, TDM_ERROR_OUTPUT_DISCONNECTED, 1);
445                         }
446                 }
447                 break;
448         default:
449                 break;
450         }
451 }
452 /* LCOV_EXCL_STOP */
453
454 static tdm_error
455 _tdm_vblank_set_fps(tdm_vblank *vblank, unsigned int fps)
456 {
457         tdm_private_vblank *private_vblank = vblank;
458
459         if (private_vblank->fps_fixed) {
460                 VIN("fps(%u) can't be changed", private_vblank->fps);
461                 return TDM_ERROR_NONE;
462         }
463
464         private_vblank->fps_changeable = 0;
465
466         if (private_vblank->fps == fps)
467                 return TDM_ERROR_NONE;
468
469         private_vblank->fps = fps;
470         private_vblank->check_HW_or_SW = 1;
471
472         VIN("fps(%u) changed", fps);
473
474         return TDM_ERROR_NONE;
475 }
476
477 static tdm_error
478 _tdm_vblank_ignore_global_fps(tdm_vblank *vblank, unsigned int ignore)
479 {
480         tdm_private_vblank *private_vblank = vblank;
481
482         if (private_vblank->ignore_global_fps == ignore)
483                 return TDM_ERROR_NONE;
484
485         private_vblank->ignore_global_fps = ignore;
486         private_vblank->check_HW_or_SW = 1;
487
488         VIN("ignore_global_fps(%u)", private_vblank->ignore_global_fps);
489
490         return TDM_ERROR_NONE;
491 }
492
493 EXTERN tdm_error
494 tdm_vblank_set_client_vblank_fps(unsigned int pid, const char *name, unsigned int fps)
495 {
496         tdm_private_vblank *v = NULL;
497         tdm_error ret = TDM_ERROR_NONE;
498
499         TDM_RETURN_VAL_IF_FAIL(pid > 0, TDM_ERROR_INVALID_PARAMETER);
500         TDM_RETURN_VAL_IF_FAIL(fps > 0, TDM_ERROR_INVALID_PARAMETER);
501
502         TDM_INFO("pid: %u, name: %s, fps: %d", pid, name, fps);
503
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;
508
509                 if (!v->resource)
510                         continue;
511
512                 client = wl_resource_get_client(v->resource);
513                 if (!client)
514                         continue;
515
516                 wl_client_get_credentials(client, &client_pid, NULL, NULL);
517
518                 if (client_pid != pid)
519                         continue;
520
521                 if (name && strncmp(name, TDM_VBLANK_DEFAULT_NAME, TDM_NAME_LEN)) {
522                         if (strncmp(v->name, name, TDM_NAME_LEN))
523                                 continue;
524                 }
525
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);
529                 else
530                         TDM_ERR("(pid:%u) '%s' fps changing failed: %d", pid, v->name, fps);
531         }
532         pthread_mutex_unlock(&valid_list_lock);
533
534         return TDM_ERROR_NONE;
535 }
536
537 EXTERN tdm_error
538 tdm_vblank_set_client_ignore_global_fps(unsigned int pid, const char *name, unsigned int ignore)
539 {
540         tdm_private_vblank *v = NULL;
541         tdm_error ret = TDM_ERROR_NONE;
542
543         TDM_RETURN_VAL_IF_FAIL(pid > 0, TDM_ERROR_INVALID_PARAMETER);
544
545         TDM_INFO("pid: %u, name: %s, ignore: %u", pid, name, ignore);
546
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;
551
552                 if (!v->resource)
553                         continue;
554
555                 client = wl_resource_get_client(v->resource);
556                 if (!client)
557                         continue;
558
559                 wl_client_get_credentials(client, &client_pid, NULL, NULL);
560
561                 if (client_pid != pid)
562                         continue;
563
564                 if (name && strncmp(name, TDM_VBLANK_DEFAULT_NAME, TDM_NAME_LEN)) {
565                         if (strncmp(v->name, name, TDM_NAME_LEN))
566                                 continue;
567                 }
568
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);
572                 else
573                         TDM_ERR("(pid:%u) '%s' ignore changing failed: %u", pid, v->name, ignore);
574         }
575         pthread_mutex_unlock(&valid_list_lock);
576
577         return TDM_ERROR_NONE;
578 }
579
580 EXTERN tdm_error
581 tdm_vblank_enable_global_fps(unsigned int enable, unsigned int fps)
582 {
583         tdm_private_vblank *v = NULL;
584
585         if (enable)
586                 TDM_RETURN_VAL_IF_FAIL(fps > 0, TDM_ERROR_INVALID_PARAMETER);
587
588         TDM_INFO("global fps: %s fps(%d)", (enable) ? "enabled" : "disabled", fps);
589
590         pthread_mutex_lock(&valid_list_lock);
591
592         vblank_global_fps = (enable) ? fps : 0;
593
594         LIST_FOR_EACH_ENTRY(v, &valid_vblank_list, valid_link) {
595                 /* skip if vblank is created in server side */
596                 if (!v->resource)
597                         continue;
598
599                 v->check_HW_or_SW = 1;
600         }
601
602         if (hwc_commit_use_global_fps) {
603                 if (enable)
604                         tdm_hwc_set_vblank(fps);
605                 else
606                         tdm_hwc_unset_vblank();
607         }
608
609         pthread_mutex_unlock(&valid_list_lock);
610
611         return TDM_ERROR_NONE;
612 }
613
614 static void
615 _tdm_vblank_thread_cb_create(tdm_private_display *private_display, void *object, tdm_thread_cb_base *cb_base, void *user_data)
616 {
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;
620
621         TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
622
623         assert(ch_info != NULL);
624
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);
628                 return;
629         }
630
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;
636 }
637
638 static tdm_error
639 _tdm_vblank_call_thread_cb(tdm_private_vblank *private_vblank)
640 {
641         tdm_thread_cb_vblank_create vblank_create;
642         tdm_error ret;
643
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;
651
652         ret = tdm_thread_cb_call(private_vblank->dpy, &vblank_create.base, 1);
653         TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
654
655         return TDM_ERROR_NONE;
656 }
657
658 EXTERN tdm_error
659 tdm_vblank_add_create_handler(tdm_display *dpy, tdm_vblank_create_handler func, void *user_data)
660 {
661         tdm_vblank_create_handler_info *ch_info = NULL;
662         tdm_error ret;
663
664         TDM_RETURN_VAL_IF_FAIL(dpy != NULL, TDM_ERROR_INVALID_PARAMETER);
665         TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
666
667         pthread_mutex_lock(&valid_list_lock);
668
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;
674                 }
675         }
676
677         ch_info = calloc(1, sizeof *ch_info);
678         if (!ch_info) {
679                 TDM_ERR("alloc failed: %m");
680                 pthread_mutex_unlock(&valid_list_lock);
681                 return TDM_ERROR_OUT_OF_MEMORY;
682         }
683
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);
687
688         if (ret != TDM_ERROR_NONE) {
689                 TDM_ERR("tdm_thread_cb_add failed");
690                 free(ch_info);
691                 pthread_mutex_unlock(&valid_list_lock);
692                 return ret;
693         }
694
695         ch_info->func = func;
696         ch_info->user_data = user_data;
697
698         LIST_ADDTAIL(&ch_info->link, &create_handler_list);
699
700         pthread_mutex_unlock(&valid_list_lock);
701
702         return TDM_ERROR_NONE;
703 }
704
705 EXTERN void
706 tdm_vblank_remove_create_handler(tdm_display *dpy, tdm_vblank_create_handler func, void *user_data)
707 {
708         tdm_vblank_create_handler_info *ch_info = NULL, *hh = NULL;
709
710         TDM_RETURN_IF_FAIL(dpy != NULL);
711         TDM_RETURN_IF_FAIL(func != NULL);
712
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)
717                         continue;
718
719                 tdm_thread_cb_remove(dpy, TDM_THREAD_CB_VBLANK_CREATE, NULL, _tdm_vblank_thread_cb_create, ch_info);
720
721                 LIST_DEL(&ch_info->link);
722                 free(ch_info);
723                 pthread_mutex_unlock(&valid_list_lock);
724                 tdm_display_unlock(dpy);
725                 return;
726         }
727         pthread_mutex_unlock(&valid_list_lock);
728         tdm_display_unlock(dpy);
729 }
730
731 static void*
732 _tdm_vblank_find_display(tdm_private_display *private_display, double stamp)
733 {
734         return private_display;
735 }
736
737 INTERN tdm_error
738 tdm_vblank_init(tdm_display *dpy)
739 {
740         if (vblank_list_inited)
741                 return TDM_ERROR_NONE;
742
743         LIST_INITHEAD(&valid_vblank_list);
744         LIST_INITHEAD(&valid_wait_list);
745         LIST_INITHEAD(&create_handler_list);
746
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);
749
750         hwc_commit_use_global_fps = tdm_config_get_int(TDM_CONFIG_KEY_GENERAL_HWC_COMMIT_USE_GLOBAL_FPS, 0);
751
752         vblank_list_inited = 1;
753
754         return TDM_ERROR_NONE;
755 }
756
757 INTERN void
758 tdm_vblank_deinit(tdm_display *dpy)
759 {
760         tdm_vblank_create_handler_info *ch_info = NULL, *hh = NULL;
761
762         if (!vblank_list_inited)
763                 return;
764
765         if (!LIST_IS_EMPTY(&valid_vblank_list))
766                 TDM_NEVER_GET_HERE();
767
768         if (!LIST_IS_EMPTY(&valid_wait_list))
769                 TDM_NEVER_GET_HERE();
770
771         LIST_FOR_EACH_ENTRY_SAFE(ch_info, hh, &create_handler_list, link) {
772                 LIST_DEL(&ch_info->link);
773                 free(ch_info);
774         }
775
776         vblank_list_inited = 0;
777 }
778
779 EXTERN tdm_vblank *
780 tdm_vblank_create(tdm_display *dpy, tdm_output *output, tdm_error *error)
781 {
782         tdm_private_vblank *private_vblank;
783         tdm_error ret;
784
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);
787
788         if (error)
789                 *error = TDM_ERROR_NONE;
790
791         private_vblank = calloc(1, sizeof * private_vblank);
792         if (!private_vblank) {
793                 /* LCOV_EXCL_START */
794                 if (error)
795                         *error = TDM_ERROR_OUT_OF_MEMORY;
796                 TDM_ERR("alloc failed");
797                 return NULL;
798                 /* LCOV_EXCL_STOP */
799         }
800
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);
804                 if (error)
805                         *error = TDM_ERROR_OPERATION_FAILED;
806                 TDM_ERR("tdm_output_add_change_handler failed");
807                 return NULL;
808         }
809
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;
817
818         _tdm_vblank_update_output_info(private_vblank);
819
820         strncpy(private_vblank->name, TDM_VBLANK_DEFAULT_NAME, TDM_NAME_LEN - 1);
821         private_vblank->name[TDM_NAME_LEN - 1] = '\0';
822
823         LIST_INITHEAD(&private_vblank->HW_wait_list);
824         LIST_INITHEAD(&private_vblank->SW_wait_list);
825
826         _tdm_vblank_valid_list_add(&private_vblank->valid_link, &valid_vblank_list);
827
828         VIN("created. vrefresh(%d) connection(%d)",
829                 private_vblank->vrefresh, private_vblank->connection);
830
831         tdm_display_lock(private_vblank->dpy);
832         _tdm_vblank_call_thread_cb(private_vblank);
833         tdm_display_unlock(private_vblank->dpy);
834
835         return (tdm_vblank *)private_vblank;
836 }
837
838 EXTERN void
839 tdm_vblank_destroy(tdm_vblank *vblank)
840 {
841         tdm_private_vblank *private_vblank = vblank;
842         tdm_vblank_wait_info *w = NULL, *ww = NULL;
843
844         TDM_RETURN_IF_FAIL(tdm_vblank_is_valid(vblank));
845
846         if (private_vblank->in_create_handler) {
847                 TDM_ERR("NOT allowed to be called in a create handler");
848                 return;
849         }
850
851         _tdm_vblank_valid_list_del(&private_vblank->valid_link);
852
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);
858         }
859
860         tdm_output_remove_change_handler(private_vblank->output,
861                                                                          _tdm_vblank_cb_output_change, private_vblank);
862
863         _tdm_vblank_free_HW_wait(private_vblank, 0, 0);
864
865         if (!LIST_IS_EMPTY(&private_vblank->SW_wait_list)) {
866                 pid_t pid;
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);
870         }
871
872         LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->SW_wait_list, link) {
873                 LIST_DEL(&w->link);
874                 _tdm_vblank_valid_list_del(&w->valid_link);
875                 free(w);
876         }
877
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);
882         }
883
884         VIN("destroyed");
885
886         free(private_vblank);
887 }
888
889 EXTERN tdm_output *
890 tdm_vblank_get_output(tdm_vblank *vblank, tdm_error *error)
891 {
892         tdm_private_vblank *private_vblank = vblank;
893         tdm_error ret = TDM_ERROR_NONE;
894
895         TDM_RETURN_VAL_IF_FAIL_WITH_ERROR(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER, NULL);
896
897         if (error)
898                 *error = TDM_ERROR_NONE;
899
900         return private_vblank->output;
901 }
902
903 EXTERN tdm_error
904 tdm_vblank_get_client_pid(tdm_vblank *vblank, pid_t *pid)
905 {
906         tdm_private_vblank *private_vblank = vblank;
907         struct wl_client *client;
908
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);
911
912         if (!private_vblank->resource) {
913                 *pid = 0;
914                 return TDM_ERROR_NONE;
915         }
916
917         client = wl_resource_get_client(private_vblank->resource);
918         if (!client) {
919                 *pid = 0;
920                 return TDM_ERROR_NONE;
921         }
922
923         wl_client_get_credentials(client, pid, NULL, NULL);
924
925         return TDM_ERROR_NONE;
926 }
927
928 EXTERN tdm_error
929 tdm_vblank_set_name(tdm_vblank *vblank, const char *name)
930 {
931         tdm_private_vblank *private_vblank = vblank;
932
933         TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
934
935         if (!name)
936                 name = TDM_VBLANK_DEFAULT_NAME;
937
938         strncpy(private_vblank->name, name, TDM_NAME_LEN - 1);
939         private_vblank->name[TDM_NAME_LEN - 1] = '\0';
940
941         VIN("name(%s)", name);
942
943         return TDM_ERROR_NONE;
944 }
945
946 EXTERN tdm_error
947 tdm_vblank_get_name(tdm_vblank *vblank, const char **name)
948 {
949         tdm_private_vblank *private_vblank = vblank;
950
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);
953
954         *name = (const char*)private_vblank->name;
955
956         return TDM_ERROR_NONE;
957 }
958
959 EXTERN tdm_error
960 tdm_vblank_set_fps(tdm_vblank *vblank, unsigned int fps)
961 {
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);
964
965         return _tdm_vblank_set_fps(vblank, fps);
966 }
967
968 EXTERN tdm_error
969 tdm_vblank_get_fps(tdm_vblank *vblank, unsigned int *fps)
970 {
971         tdm_private_vblank *private_vblank = vblank;
972
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);
975
976         *fps = private_vblank->fps;
977
978         return TDM_ERROR_NONE;
979 }
980
981 EXTERN tdm_error
982 tdm_vblank_set_fixed_fps(tdm_vblank *vblank, unsigned int fps)
983 {
984         tdm_private_vblank *private_vblank = vblank;
985
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);
988
989         private_vblank->fps_changeable = 0;
990         private_vblank->fps_fixed = 1;
991
992         if (private_vblank->fps == fps)
993                 return TDM_ERROR_NONE;
994
995         private_vblank->fps = fps;
996         private_vblank->check_HW_or_SW = 1;
997
998         VIN("fps(%u) fixed", fps);
999
1000         return TDM_ERROR_NONE;
1001 }
1002
1003 EXTERN tdm_error
1004 tdm_vblank_ignore_global_fps(tdm_vblank *vblank, unsigned int ignore)
1005 {
1006         TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1007
1008         return _tdm_vblank_ignore_global_fps(vblank, ignore);
1009 }
1010
1011 EXTERN tdm_error
1012 tdm_vblank_set_offset(tdm_vblank *vblank, int offset)
1013 {
1014         tdm_private_vblank *private_vblank = vblank;
1015
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);
1018
1019         if (private_vblank->offset == offset)
1020                 return TDM_ERROR_NONE;
1021
1022         private_vblank->offset = offset;
1023         private_vblank->check_HW_or_SW = 1;
1024
1025         VIN("offset(%u)", private_vblank->offset);
1026
1027         return TDM_ERROR_NONE;
1028 }
1029
1030 EXTERN tdm_error
1031 tdm_vblank_get_offset(tdm_vblank *vblank, int *offset)
1032 {
1033         tdm_private_vblank *private_vblank = vblank;
1034
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);
1037
1038         *offset = private_vblank->offset;
1039
1040         return TDM_ERROR_NONE;
1041 }
1042
1043 EXTERN tdm_error
1044 tdm_vblank_set_enable_fake(tdm_vblank *vblank, unsigned int enable_fake)
1045 {
1046         tdm_private_vblank *private_vblank = vblank;
1047
1048         TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1049
1050         if (private_vblank->enable_fake == enable_fake)
1051                 return TDM_ERROR_NONE;
1052
1053         private_vblank->enable_fake = enable_fake;
1054
1055         VIN("enable_fake(%u)", private_vblank->enable_fake);
1056
1057         return TDM_ERROR_NONE;
1058 }
1059
1060 EXTERN tdm_error
1061 tdm_vblank_get_enable_fake(tdm_vblank *vblank, unsigned int *enable_fake)
1062 {
1063         tdm_private_vblank *private_vblank = vblank;
1064
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);
1067
1068         *enable_fake = private_vblank->enable_fake;
1069
1070         return TDM_ERROR_NONE;
1071 }
1072
1073 static tdm_error
1074 _tdm_vblank_timeout_timer_cb(void *user_data)
1075 {
1076         tdm_private_vblank *private_vblank = user_data;
1077         tdm_vblank_wait_info *w = NULL;
1078         pid_t pid;
1079         const char *proc_name;
1080
1081         TDM_RETURN_VAL_IF_FAIL(private_vblank != NULL, TDM_ERROR_OPERATION_FAILED);
1082
1083         _tdm_vblank_get_client_information(private_vblank, &pid, &proc_name);
1084
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);
1087
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);
1092         }
1093
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);
1097         }
1098
1099         return TDM_ERROR_NONE;
1100 }
1101
1102 static void
1103 _tdm_vblank_timeout_timer_update(tdm_private_vblank *private_vblank, int ms_delay)
1104 {
1105         tdm_error ret;
1106
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,
1111                                                                                          private_vblank,
1112                                                                                          &ret);
1113                 if (!private_vblank->timeout_timer) {
1114                         VER("couldn't add timer");
1115                         return;
1116                 }
1117                 VIN("Create Timeout timer");
1118         }
1119
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");
1123                 return;
1124         }
1125
1126         if (ms_delay != 0)
1127                 VDB("timeout timer updated");
1128         else
1129                 VDB("timeout timer disabled");
1130 }
1131
1132 static tdm_error
1133 _tdm_vblank_sw_timer_update(tdm_private_vblank *private_vblank)
1134 {
1135         tdm_vblank_wait_info *first_wait_info = NULL;
1136         double curr, target;
1137         int ms_delay;
1138         tdm_error ret;
1139
1140         if (LIST_IS_EMPTY(&private_vblank->SW_wait_list)) {
1141                 VER("no wait_info");
1142                 return TDM_ERROR_OPERATION_FAILED;
1143         }
1144
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;
1148
1149         /* ms_delay should be more that 1 */
1150         if (target < curr)
1151                 ms_delay = 1;
1152         else
1153                 ms_delay = ceil((target - curr) * 1000.0);
1154
1155         if (ms_delay < 1)
1156                 ms_delay = 1;
1157
1158         if (ms_delay > 5000)
1159                 VER("wait(%p) curr(%.6f) target(%.6f) ms_delay(%d)", first_wait_info, curr, target, ms_delay);
1160         else
1161                 VDB("wait(%p) curr(%.6f) target(%.6f) ms_delay(%d)", first_wait_info, curr, target, ms_delay);
1162
1163         tdm_display_lock(private_vblank->dpy);
1164
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,
1169                                                                                          private_vblank,
1170                                                                                          &ret);
1171                 if (!private_vblank->SW_timer) {
1172                         tdm_display_unlock(private_vblank->dpy);
1173                         VER("couldn't add timer");
1174                         return ret;
1175                 }
1176
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");
1183                         return ret;
1184                 }
1185
1186                 VIN("Create SW timer");
1187         }
1188
1189         private_vblank->last_interval = ms_delay;
1190
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");
1196                 return ret;
1197                 /* LCOV_EXCL_STOP */
1198         }
1199
1200         tdm_display_unlock(private_vblank->dpy);
1201
1202         return TDM_ERROR_NONE;
1203 }
1204
1205 static void
1206 _tdm_vblank_cb_vblank_HW(tdm_output *output, unsigned int sequence,
1207                                                  unsigned int tv_sec, unsigned int tv_usec,
1208                                                  void *user_data)
1209 {
1210         tdm_vblank_wait_info *wait_info = user_data;
1211         tdm_private_vblank *private_vblank;
1212
1213         if (!_tdm_vblank_check_valid_wait(wait_info)) {
1214                 TDM_ERR("can't find wait(%p) from valid_wait_list", wait_info);
1215                 return;
1216         }
1217
1218         private_vblank = wait_info->private_vblank;
1219         TDM_RETURN_IF_FAIL(tdm_vblank_is_valid(private_vblank));
1220
1221         assert(private_vblank->owner_tid == syscall(SYS_gettid));
1222
1223         if (!_tdm_vblank_find_wait(wait_info, &private_vblank->HW_wait_list)) {
1224                 VER("can't find wait(%p)", wait_info);
1225                 return;
1226         }
1227
1228         if (wait_info->type == VBLANK_TYPE_HW_SW) {
1229                 tdm_error ret;
1230
1231                 LIST_DEL(&wait_info->link);
1232
1233                 /* kernel sends the wrong time of first vblank event. and the time of
1234                  * second vblank is correct.
1235                  */
1236                 wait_info->target_time = TDM_TIME(tv_sec, tv_usec);
1237                 wait_info->target_time += (((double)private_vblank->offset) / 1000.0);
1238
1239                 _tdm_vblank_insert_wait(wait_info, &private_vblank->SW_wait_list);
1240
1241                 ret = _tdm_vblank_sw_timer_update(private_vblank);
1242
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);
1246                         return;
1247                 }
1248
1249                 VER("couldn't update sw timer");
1250         }
1251
1252         tdm_display_lock(private_vblank->dpy);
1253         _tdm_vblank_timeout_timer_update(private_vblank, 0);
1254         tdm_display_unlock(private_vblank->dpy);
1255
1256         LIST_DEL(&wait_info->link);
1257         _tdm_vblank_valid_list_del(&wait_info->valid_link);
1258
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.
1263          */
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);
1268         }
1269
1270         private_vblank->last_seq = wait_info->target_seq;
1271         private_vblank->last_time = TDM_TIME(tv_sec, tv_usec);
1272
1273         VDB("wait(%p) last(%.6f) sequence(%u) done", wait_info, private_vblank->last_time, wait_info->target_seq);
1274
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);
1277
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);
1281
1282         free(wait_info);
1283 }
1284
1285 static tdm_error
1286 _tdm_vblank_wait_HW(tdm_vblank_wait_info *wait_info)
1287 {
1288         tdm_private_vblank *private_vblank = wait_info->private_vblank;
1289         int hw_interval;
1290         tdm_error ret;
1291
1292         if (private_vblank->last_type != VBLANK_EVENT_TYPE_HW) {
1293                 VIN("Use HW vblank");
1294                 private_vblank->last_type = VBLANK_EVENT_TYPE_HW;
1295         }
1296
1297         _tdm_vblank_insert_wait(wait_info, &private_vblank->HW_wait_list);
1298
1299         if (private_vblank->last_time == 0) {
1300                 hw_interval = wait_info->interval * private_vblank->quotient;
1301
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();
1305
1306                 /* +1 ms to call the handler ASAP at the first. no matter for SW timer. */
1307                 wait_info->target_seq = 1;
1308
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);
1311         } else {
1312                 double last, prev, req, curr, target;
1313                 unsigned int skip;
1314
1315                 last = private_vblank->last_time;
1316                 req = wait_info->req_time;
1317
1318                 /* make sure that req time is bigger than last. */
1319                 if (req < last)
1320                         req = last;
1321
1322                 skip = (unsigned int)((req - last) / private_vblank->vblank_gap + TDM_TIME_MARGIN);
1323                 prev = last + private_vblank->vblank_gap * skip;
1324
1325                 curr = tdm_helper_get_time();
1326                 target = last;
1327                 while (target < curr)
1328                         target += (private_vblank->vblank_gap * wait_info->interval);
1329
1330                 hw_interval = (unsigned int)((target - curr) / private_vblank->HW_vblank_gap + TDM_TIME_MARGIN) + 1;
1331
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);
1334
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);
1339                 else
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);
1343         }
1344
1345         private_vblank->last_interval = hw_interval;
1346         wait_info->hw_interval = hw_interval;
1347
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);
1350
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);
1354         else
1355                 ret = tdm_output_wait_vblank(private_vblank->output, hw_interval, 0,
1356                                                                          _tdm_vblank_cb_vblank_HW, wait_info);
1357
1358         if (ret != TDM_ERROR_NONE) {
1359                 LIST_DEL(&wait_info->link);
1360                 return ret;
1361         }
1362
1363         VDB("wait(%p) waiting", wait_info);
1364
1365         return TDM_ERROR_NONE;
1366 }
1367
1368 static void
1369 _tdm_vblank_cb_vblank_SW(tdm_private_display *private_display, void *object, tdm_thread_cb_base *cb_base, void *user_data)
1370 {
1371         tdm_private_vblank *private_vblank = object;
1372         tdm_vblank_wait_info *first_wait_info = NULL, *w = NULL, *ww = NULL;
1373
1374         TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
1375
1376         assert(private_vblank->owner_tid == syscall(SYS_gettid));
1377
1378         if (LIST_IS_EMPTY(&private_vblank->SW_wait_list)) {
1379                 VER("no wait_info");
1380                 return;
1381         }
1382
1383         if (!LIST_IS_EMPTY(&private_vblank->HW_wait_list))
1384                 TDM_NEVER_GET_HERE();
1385
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);
1388
1389         _tdm_vblank_timeout_timer_update(private_vblank, 0);
1390
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);
1393
1394         private_vblank->last_seq = first_wait_info->target_seq;
1395         private_vblank->last_time = first_wait_info->target_time;
1396
1397         VDB("wait(%p) last(%.6f) sequence(%u) done", first_wait_info, private_vblank->last_time, first_wait_info->target_seq);
1398
1399         LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->SW_wait_list, link) {
1400                 if (w->target_time != first_wait_info->target_time)
1401                         break;
1402
1403                 LIST_DEL(&w->link);
1404                 _tdm_vblank_valid_list_del(&w->valid_link);
1405
1406                 if (w->func) {
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),
1410                                         w->user_data);
1411                         tdm_display_lock(private_vblank->dpy);
1412                 }
1413
1414                 free(w);
1415         }
1416 }
1417
1418 static tdm_error
1419 _tdm_vblank_cb_timeout_SW(void *user_data)
1420 {
1421         tdm_private_vblank *private_vblank = user_data;
1422         tdm_thread_cb_vblank_sw vblank_sw;
1423         tdm_error ret;
1424
1425         TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(private_vblank), TDM_ERROR_OPERATION_FAILED);
1426
1427         VIN("timeout_SW -> thread-cb");
1428
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;
1435
1436         ret = tdm_thread_cb_call(private_vblank, &vblank_sw.base, 1);
1437         TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
1438
1439         return TDM_ERROR_NONE;
1440 }
1441
1442 static tdm_error
1443 _tdm_vblank_wait_SW(tdm_vblank_wait_info *wait_info)
1444 {
1445         tdm_private_vblank *private_vblank = wait_info->private_vblank;
1446         tdm_error ret;
1447         double last, prev, req, curr, target;
1448         unsigned int skip;
1449
1450         if (private_vblank->last_type != VBLANK_EVENT_TYPE_SW) {
1451                 tdm_output_dpms dpms;
1452
1453                 tdm_output_get_dpms(private_vblank->output, &dpms);
1454
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;
1459         }
1460
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;
1465
1466                 /* +1 ms to call the handler ASAP at the first. no matter for SW timer. */
1467                 wait_info->target_seq = 1;
1468         }
1469
1470         last = private_vblank->last_time;
1471         req = wait_info->req_time;
1472
1473         /* make sure that req time is bigger than last. */
1474         if (req < last)
1475                 req = last;
1476
1477         skip = (unsigned int)((req - last) / private_vblank->vblank_gap + TDM_TIME_MARGIN);
1478         prev = last + private_vblank->vblank_gap * skip;
1479
1480         curr = tdm_helper_get_time();
1481         target = prev + private_vblank->vblank_gap * wait_info->interval;
1482
1483         while (target < curr)
1484                 target += private_vblank->vblank_gap;
1485
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);
1488
1489         wait_info->target_time = target + (double)(private_vblank->offset) / 1000;
1490
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);
1494
1495         _tdm_vblank_insert_wait(wait_info, &private_vblank->SW_wait_list);
1496
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");
1502                 return ret;
1503                 /* LCOV_EXCL_STOP */
1504         }
1505
1506         return TDM_ERROR_NONE;
1507 }
1508
1509 EXTERN tdm_error
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)
1512 {
1513         tdm_private_vblank *private_vblank = vblank;
1514         tdm_vblank_wait_info *wait_info;
1515         tdm_output_dpms dpms = TDM_OUTPUT_DPMS_OFF;
1516         unsigned int fps;
1517         int dpms_changing = 0;
1518         tdm_error ret;
1519
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);
1523
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;
1527         }
1528
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;
1532         }
1533
1534         tdm_output_get_dpms(private_vblank->output, &dpms);
1535         tdm_output_get_dpms_changing(private_vblank->output, &dpms_changing);
1536
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;
1541                 }
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;
1545                 }
1546
1547                 if (dpms_changing) {
1548                         VER("can't wait a vblank: DPMS is changing");
1549                         return TDM_ERROR_DPMS_OFF;
1550                 }
1551         }
1552
1553         wait_info = calloc(1, sizeof * wait_info);
1554         if (!wait_info) {
1555                 /* LCOV_EXCL_START */
1556                 VER("alloc failed");
1557                 return TDM_ERROR_OUT_OF_MEMORY;
1558                 /* LCOV_EXCL_STOP */
1559         }
1560         _tdm_vblank_update_output_info(private_vblank);
1561
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);
1567         }
1568
1569         LIST_INITHEAD(&wait_info->link);
1570
1571         _tdm_vblank_valid_list_add(&wait_info->valid_link, &valid_wait_list);
1572
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;
1579
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;
1585         else
1586                 fps = vblank_global_fps;
1587
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;
1592         }
1593
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.
1599          */
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;
1607         else
1608                 wait_info->type = VBLANK_TYPE_HW_SW;
1609
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);
1614         } else {
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);
1618
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);
1623                 }
1624         }
1625
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);
1630
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);
1634
1635                 free(wait_info);
1636                 return ret;
1637                 /* LCOV_EXCL_STOP */
1638         }
1639
1640         tdm_display_lock(private_vblank->dpy);
1641         _tdm_vblank_timeout_timer_update(private_vblank, 1000);
1642         tdm_display_unlock(private_vblank->dpy);
1643
1644         return TDM_ERROR_NONE;
1645 }
1646
1647 EXTERN tdm_error
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)
1650 {
1651         tdm_private_vblank *private_vblank = vblank;
1652         unsigned int interval;
1653
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);
1656
1657         /* if the sequence of vblank reaches the max value, sequence can be 0. */
1658
1659         if (private_vblank->last_seq == 0) {
1660                 VWR("can't calculate interval with sequence(%u)", sequence);
1661                 interval = 1;
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;
1665
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);
1669
1670                 /* If target is old or too close(1ms) to current, we call the handler immediately.
1671                  * 2ms? it seems too much.
1672                  */
1673                 if (seq_target < curr) {
1674                         func(vblank, TDM_ERROR_NONE, sequence,
1675                                  TDM_TIME_SEC(seq_target), TDM_TIME_USEC(seq_target),
1676                                  user_data);
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),
1681                                  user_data);
1682                         return TDM_ERROR_NONE;
1683                 }
1684
1685                 interval = (unsigned int)((seq_target - curr) / private_vblank->vblank_gap + TDM_TIME_MARGIN) + 1;
1686         } else {
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),
1691                          user_data);
1692                 return TDM_ERROR_NONE;
1693         }
1694
1695         return tdm_vblank_wait(vblank, req_sec, req_usec, interval, func, user_data);
1696 }
1697
1698 /* LCOV_EXCL_START */
1699 INTERN tdm_error
1700 tdm_vblank_set_add_front(tdm_vblank *vblank, unsigned int add_front)
1701 {
1702         tdm_private_vblank *private_vblank = vblank;
1703
1704         TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1705
1706         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
1707
1708         if (private_vblank->add_front == add_front)
1709                 return TDM_ERROR_NONE;
1710
1711         private_vblank->add_front = add_front;
1712
1713         return TDM_ERROR_NONE;
1714 }
1715 /* LCOV_EXCL_STOP */
1716
1717 INTERN tdm_error
1718 tdm_vblank_set_resource(tdm_vblank *vblank, struct wl_resource *resource)
1719 {
1720         tdm_private_vblank *private_vblank = vblank;
1721
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);
1724
1725         private_vblank->resource = resource;
1726
1727         return TDM_ERROR_NONE;
1728 }
1729
1730 INTERN double
1731 tdm_vblank_get_stamp(tdm_vblank *vblank)
1732 {
1733         tdm_private_vblank *private_vblank = vblank;
1734
1735         TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1736
1737         return private_vblank->stamp;
1738 }
1739
1740 static unsigned int
1741 _tdm_vblank_get_waiting_count(tdm_private_vblank *private_vblank)
1742 {
1743         tdm_vblank_wait_info *wait_info = NULL;
1744         unsigned int count = 0;
1745
1746         LIST_FOR_EACH_ENTRY(wait_info, &private_vblank->HW_wait_list, link) {
1747                 count++;
1748         }
1749
1750         LIST_FOR_EACH_ENTRY(wait_info, &private_vblank->SW_wait_list, link) {
1751                 count++;
1752         }
1753
1754         return count;
1755 }
1756
1757 static void
1758 _tdm_vblank_get_client_information(tdm_private_vblank *private_vblank, pid_t *pid, const char **name)
1759 {
1760         struct wl_client *client;
1761
1762         if (!private_vblank->resource)
1763                 goto no_client;
1764
1765         client = wl_resource_get_client(private_vblank->resource);
1766         if (!client)
1767                 goto no_client;
1768
1769         wl_client_get_credentials(client, pid, NULL, NULL);
1770         *name = tdm_server_get_client_name(*pid);
1771
1772         return;
1773
1774 no_client:
1775         *pid = 0;
1776         *name = NULL;
1777         return;
1778 }
1779
1780 /* LCOV_EXCL_START */
1781 INTERN void
1782 tdm_vblank_get_vblank_list_information(tdm_display *dpy, char *reply, int *len)
1783 {
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);
1787
1788         if (output)
1789                 tdm_output_get_dpms(output, &dpms);
1790
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");
1797
1798         pthread_mutex_lock(&valid_list_lock);
1799         LIST_FOR_EACH_ENTRY(v, &valid_vblank_list, valid_link) {
1800                 const char *proc_name = NULL;
1801                 pid_t pid = 0;
1802                 unsigned int count;
1803
1804                 _tdm_vblank_get_client_information(v, &pid, &proc_name);
1805
1806                 count = _tdm_vblank_get_waiting_count(v);
1807
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);
1818         }
1819         pthread_mutex_unlock(&valid_list_lock);
1820
1821         TDM_SNPRINTF(reply, len, "\n");
1822 }
1823 /* LCOV_EXCL_STOP */
1824