package version up to 1.17.1
[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 double stamp = 0;
171
172 static void _tdm_vblank_cb_vblank_SW(tdm_private_display *private_display, void *object, tdm_thread_cb_base *cb_base, void *user_data);
173 static tdm_error _tdm_vblank_cb_timeout_SW(void *user_data);
174 static tdm_error _tdm_vblank_wait_SW(tdm_vblank_wait_info *wait_info);
175 static void _tdm_vblank_cb_vblank_HW(tdm_output *output, unsigned int sequence,
176                                                                          unsigned int tv_sec, unsigned int tv_usec,
177                                                                          void *user_data);
178 static void _tdm_vblank_timeout_timer_update(tdm_private_vblank *private_vblank, int ms_delay);
179 static void _tdm_vblank_get_client_information(tdm_private_vblank *private_vblank,
180                                                                                            pid_t *pid, const char **name);
181
182 #if 0
183 static void
184 _print_list(tdm_private_vblank *private_vblank)
185 {
186         tdm_vblank_wait_info *w = NULL;
187         int count = 0;
188
189         LIST_FOR_EACH_ENTRY(w, &private_vblank->HW_wait_list, link) {
190                 VIN("HW_wait(%p): type(%d) req_time(%.6f) target_time(%.6f) target_seq(%u)",
191                         w, w->type, w->req_time, w->target_time, w->target_seq);
192                 count++;
193         }
194
195         LIST_FOR_EACH_ENTRY(w, &private_vblank->SW_wait_list, link) {
196                 VIN("SW_wait(%p): type(%d) req_time(%.6f) target_time(%.6f) target_seq(%u)",
197                         w, w->type, w->req_time, w->target_time, w->target_seq);
198                 count++;
199         }
200
201         if (!count)
202                 VIN("NO_wait");
203 }
204 #endif
205
206 static int
207 tdm_vblank_is_valid(tdm_vblank *vblank)
208 {
209         tdm_private_vblank *v = NULL;
210
211         if (vblank && vblank_list_inited) {
212                 pthread_mutex_lock(&valid_list_lock);
213                 LIST_FOR_EACH_ENTRY(v, &valid_vblank_list, valid_link) {
214                         if (v == (tdm_private_vblank *)vblank) {
215                                 pthread_mutex_unlock(&valid_list_lock);
216                                 return 1;
217                         }
218                 }
219                 pthread_mutex_unlock(&valid_list_lock);
220
221         }
222         return 0;
223 }
224
225 static void
226 _tdm_vblank_valid_list_add(struct list_head *valid_link, struct list_head *valid_list)
227 {
228         pthread_mutex_lock(&valid_list_lock);
229         LIST_ADDTAIL(valid_link, valid_list);
230         pthread_mutex_unlock(&valid_list_lock);
231 }
232
233 static void
234 _tdm_vblank_valid_list_del(struct list_head *valid_link)
235 {
236         pthread_mutex_lock(&valid_list_lock);
237         LIST_DEL(valid_link);
238         pthread_mutex_unlock(&valid_list_lock);
239 }
240
241 static inline void*
242 _tdm_vblank_find_object(tdm_private_display *private_display, double vblank_stamp)
243 {
244         tdm_private_vblank *v = NULL;
245
246         pthread_mutex_lock(&valid_list_lock);
247         LIST_FOR_EACH_ENTRY(v, &valid_vblank_list, valid_link) {
248                 if (v->stamp == vblank_stamp) {
249                         pthread_mutex_unlock(&valid_list_lock);
250                         return v;
251                 }
252         }
253         pthread_mutex_unlock(&valid_list_lock);
254
255         return NULL;
256 }
257
258 static inline unsigned int
259 _tdm_vblank_check_valid_wait(tdm_vblank_wait_info *wait_info)
260 {
261         tdm_vblank_wait_info *w = NULL;
262
263         if (!wait_info)
264                 return 0;
265
266         pthread_mutex_lock(&valid_list_lock);
267         LIST_FOR_EACH_ENTRY(w, &valid_wait_list, valid_link) {
268                 if (w == wait_info) {
269                         pthread_mutex_unlock(&valid_list_lock);
270                         return 1;
271                 }
272         }
273         pthread_mutex_unlock(&valid_list_lock);
274
275         return 0;
276 }
277
278 static inline unsigned int
279 _tdm_vblank_find_wait(tdm_vblank_wait_info *wait_info, struct list_head *list)
280 {
281         tdm_vblank_wait_info *w = NULL;
282
283         if (!wait_info)
284                 return 0;
285
286         LIST_FOR_EACH_ENTRY(w, list, link) {
287                 if (w->stamp == wait_info->stamp)
288                         return 1;
289         }
290
291         return 0;
292 }
293
294 static inline void
295 _tdm_vblank_insert_wait(tdm_vblank_wait_info *wait_info, struct list_head *list)
296 {
297         tdm_vblank_wait_info *w = NULL;
298         tdm_vblank_wait_info *found = NULL;
299
300         if (LIST_IS_EMPTY(list)) {
301                 LIST_ADDTAIL(&wait_info->link, list);
302                 return;
303         }
304
305         LIST_FOR_EACH_ENTRY(w, list, link) {
306                 if (wait_info->type == VBLANK_TYPE_SW) {
307                         if (wait_info->target_time == 0)
308                                 TDM_NEVER_GET_HERE();
309                         if (w->target_time <= wait_info->target_time) {
310                                 found = w;
311                                 continue;
312                         }
313                 } else {
314                         if (w->interval <= wait_info->interval) {
315                                 found = w;
316                                 continue;
317                         }
318                 }
319         }
320
321         if (found)
322                 LIST_ADD(&wait_info->link, &found->link);
323         else
324                 LIST_ADDTAIL(&wait_info->link, list->next);
325 }
326
327 static void
328 _tdm_vblank_change_to_SW(tdm_private_vblank *private_vblank)
329 {
330         tdm_vblank_wait_info *w = NULL, *ww = NULL;
331
332         LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->HW_wait_list, link) {
333                 LIST_DEL(&w->link);
334
335                 tdm_display_lock(private_vblank->dpy);
336                 tdm_output_remove_vblank_handler_internal(private_vblank->output, _tdm_vblank_cb_vblank_HW, w);
337                 tdm_display_unlock(private_vblank->dpy);
338
339                 w->type = VBLANK_TYPE_SW_FAKE;
340                 _tdm_vblank_wait_SW(w);
341         }
342 }
343
344 static void
345 _tdm_vblank_free_HW_wait(tdm_private_vblank *private_vblank, tdm_error error, unsigned int call_cb)
346 {
347         tdm_vblank_wait_info *w = NULL, *ww = NULL;
348
349         if (!LIST_IS_EMPTY(&private_vblank->HW_wait_list)) {
350                 pid_t pid;
351                 const char *proc_name;
352                 _tdm_vblank_get_client_information(private_vblank, &pid, &proc_name);
353                 VWR("TDM HW vblank destroyed forcely!! (pid: %u, name: %s)", pid, proc_name);
354         }
355
356         LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->HW_wait_list, link) {
357                 LIST_DEL(&w->link);
358                 _tdm_vblank_valid_list_del(&w->valid_link);
359
360                 tdm_display_lock(private_vblank->dpy);
361                 tdm_output_remove_vblank_handler_internal(private_vblank->output, _tdm_vblank_cb_vblank_HW, w);
362                 tdm_display_unlock(private_vblank->dpy);
363
364                 VDB("wait(%p) last(%.6f) sequence(%u) done (error:%d, call_cb:%u)",
365                         w, (double)0.0, w->target_seq, error, call_cb);
366
367                 if (call_cb && w->func)
368                         w->func(private_vblank, error, 0, 0, 0, w->user_data);
369
370                 free(w);
371         }
372 }
373
374 static void
375 _tdm_vblank_update_output_info(tdm_private_vblank *private_vblank)
376 {
377         tdm_output *output = private_vblank->output;
378         tdm_output_conn_status connection = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
379         unsigned int vrefresh = TDM_VBLANK_DEFAULT_VREFRESH;
380         const tdm_output_mode *mode = NULL;
381         tdm_error ret;
382
383         ret = tdm_output_get_conn_status(output, &connection);
384         TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
385
386         ret = tdm_output_get_mode(output, &mode);
387         if (ret == TDM_ERROR_NONE && mode)
388                 vrefresh = mode->vrefresh;
389         else
390                 VWR("mode not setted!!!");
391
392         private_vblank->connection = connection;
393         private_vblank->vrefresh = vrefresh;
394         private_vblank->HW_vblank_gap = 1.0 / private_vblank->vrefresh;
395
396         if (private_vblank->fps_changeable)
397                 private_vblank->fps = vrefresh;
398
399         VIN("connection(%d) vrefresh(%d) fps(%d) fps_changeable(%d)",
400                 private_vblank->connection,
401                 private_vblank->vrefresh, private_vblank->fps, private_vblank->fps_changeable);
402 }
403
404 /* LCOV_EXCL_START */
405 static void
406 _tdm_vblank_cb_output_change(tdm_output *output, tdm_output_change_type type,
407                                                          tdm_value value, void *user_data)
408 {
409         tdm_private_vblank *private_vblank = user_data;
410
411         TDM_RETURN_IF_FAIL(tdm_vblank_is_valid(private_vblank));
412
413         switch (type) {
414         case TDM_OUTPUT_CHANGE_DPMS:
415                 VIN("dpms %s", tdm_dpms_str(value.u32));
416                 private_vblank->check_HW_or_SW = 1;
417                 if (TDM_OUTPUT_DPMS_VSYNC_IS_OFF(value.u32)) {
418                         if (private_vblank->enable_fake)
419                                 _tdm_vblank_change_to_SW(private_vblank);
420                         else {
421                                 tdm_display_lock(private_vblank->dpy);
422                                 _tdm_vblank_timeout_timer_update(private_vblank, 0);
423                                 tdm_display_unlock(private_vblank->dpy);
424
425                                 _tdm_vblank_free_HW_wait(private_vblank, TDM_ERROR_DPMS_OFF, 1);
426                         }
427                 }
428                 break;
429         case TDM_OUTPUT_CHANGE_CONNECTION:
430                 if (private_vblank->connection == value.u32)
431                         break;
432                 VIN("output %s", tdm_status_str(value.u32));
433                 _tdm_vblank_update_output_info(private_vblank);
434                 private_vblank->check_HW_or_SW = 1;
435                 if (private_vblank->connection == TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
436                         if (private_vblank->enable_fake)
437                                 _tdm_vblank_change_to_SW(private_vblank);
438                         else {
439                                 tdm_display_lock(private_vblank->dpy);
440                                 _tdm_vblank_timeout_timer_update(private_vblank, 0);
441                                 tdm_display_unlock(private_vblank->dpy);
442
443                                 _tdm_vblank_free_HW_wait(private_vblank, TDM_ERROR_OUTPUT_DISCONNECTED, 1);
444                         }
445                 }
446                 break;
447         default:
448                 break;
449         }
450 }
451 /* LCOV_EXCL_STOP */
452
453 EXTERN tdm_error
454 tdm_vblank_set_client_vblank_fps(unsigned int pid, const char *name, unsigned int fps)
455 {
456         tdm_private_vblank *v = NULL;
457         tdm_error ret = TDM_ERROR_NONE;
458
459         TDM_RETURN_VAL_IF_FAIL(pid > 0, TDM_ERROR_INVALID_PARAMETER);
460         TDM_RETURN_VAL_IF_FAIL(fps > 0, TDM_ERROR_INVALID_PARAMETER);
461
462         TDM_INFO("pid: %u, name: %s, fps: %d", pid, name, fps);
463
464         pthread_mutex_lock(&valid_list_lock);
465         LIST_FOR_EACH_ENTRY(v, &valid_vblank_list, valid_link) {
466                 struct wl_client *client;
467                 pid_t client_pid = 0;
468
469                 if (!v->resource)
470                         continue;
471
472                 client = wl_resource_get_client(v->resource);
473                 if (!client)
474                         continue;
475
476                 wl_client_get_credentials(client, &client_pid, NULL, NULL);
477
478                 if (client_pid != pid)
479                         continue;
480
481                 if (name && strncmp(name, TDM_VBLANK_DEFAULT_NAME, TDM_NAME_LEN)) {
482                         if (strncmp(v->name, name, TDM_NAME_LEN))
483                                 continue;
484                 }
485
486                 ret = tdm_vblank_set_fps(v, fps);
487                 if (ret == TDM_ERROR_NONE)
488                         TDM_INFO("(pid:%u) '%s' fps changed: %d", pid, v->name, fps);
489                 else
490                         TDM_ERR("(pid:%u) '%s' fps changing failed: %d", pid, v->name, fps);
491         }
492         pthread_mutex_unlock(&valid_list_lock);
493
494         return TDM_ERROR_NONE;
495 }
496
497 EXTERN tdm_error
498 tdm_vblank_set_client_ignore_global_fps(unsigned int pid, const char *name, unsigned int ignore)
499 {
500         tdm_private_vblank *v = NULL;
501         tdm_error ret = TDM_ERROR_NONE;
502
503         TDM_RETURN_VAL_IF_FAIL(pid > 0, TDM_ERROR_INVALID_PARAMETER);
504
505         TDM_INFO("pid: %u, name: %s, ignore: %u", pid, name, ignore);
506
507         pthread_mutex_lock(&valid_list_lock);
508         LIST_FOR_EACH_ENTRY(v, &valid_vblank_list, valid_link) {
509                 struct wl_client *client;
510                 pid_t client_pid = 0;
511
512                 if (!v->resource)
513                         continue;
514
515                 client = wl_resource_get_client(v->resource);
516                 if (!client)
517                         continue;
518
519                 wl_client_get_credentials(client, &client_pid, NULL, NULL);
520
521                 if (client_pid != pid)
522                         continue;
523
524                 if (name && strncmp(name, TDM_VBLANK_DEFAULT_NAME, TDM_NAME_LEN)) {
525                         if (strncmp(v->name, name, TDM_NAME_LEN))
526                                 continue;
527                 }
528
529                 ret = tdm_vblank_ignore_global_fps(v, ignore);
530                 if (ret == TDM_ERROR_NONE)
531                         TDM_INFO("(pid:%u) '%s' ignore changed: %u", pid, v->name, ignore);
532                 else
533                         TDM_ERR("(pid:%u) '%s' ignore changing failed: %u", pid, v->name, ignore);
534         }
535         pthread_mutex_unlock(&valid_list_lock);
536
537         return TDM_ERROR_NONE;
538 }
539
540 EXTERN tdm_error
541 tdm_vblank_enable_global_fps(unsigned int enable, unsigned int fps)
542 {
543         tdm_private_vblank *v = NULL;
544
545         if (enable)
546                 TDM_RETURN_VAL_IF_FAIL(fps > 0, TDM_ERROR_INVALID_PARAMETER);
547
548         TDM_INFO("global fps: %s fps(%d)", (enable) ? "enabled" : "disabled", fps);
549
550         pthread_mutex_lock(&valid_list_lock);
551
552         vblank_global_fps = (enable) ? fps : 0;
553
554         LIST_FOR_EACH_ENTRY(v, &valid_vblank_list, valid_link) {
555                 /* skip if vblank is created in server side */
556                 if (!v->resource)
557                         continue;
558
559                 v->check_HW_or_SW = 1;
560         }
561         pthread_mutex_unlock(&valid_list_lock);
562
563         return TDM_ERROR_NONE;
564 }
565
566 static void
567 _tdm_vblank_thread_cb_create(tdm_private_display *private_display, void *object, tdm_thread_cb_base *cb_base, void *user_data)
568 {
569         tdm_thread_cb_vblank_create *vblank_create = (tdm_thread_cb_vblank_create *)cb_base;
570         tdm_vblank_create_handler_info *ch_info = user_data;
571         tdm_private_vblank *private_vblank;
572
573         TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
574
575         assert(ch_info != NULL);
576
577         private_vblank = _tdm_vblank_find_object(private_display, vblank_create->vblank_stamp);
578         if (!private_vblank) {
579                 TDM_DBG("can't find vblank(%.0f) from valid_list", vblank_create->vblank_stamp);
580                 return;
581         }
582
583         private_vblank->in_create_handler = 1;
584         tdm_display_unlock(private_display);
585         ch_info->func(private_vblank, ch_info->user_data);
586         tdm_display_lock(private_display);
587         private_vblank->in_create_handler = 0;
588 }
589
590 static tdm_error
591 _tdm_vblank_call_thread_cb(tdm_private_vblank *private_vblank)
592 {
593         tdm_thread_cb_vblank_create vblank_create;
594         tdm_error ret;
595
596         memset(&vblank_create, 0, sizeof vblank_create);
597         vblank_create.base.type = TDM_THREAD_CB_VBLANK_CREATE;
598         vblank_create.base.length = sizeof vblank_create;
599         vblank_create.base.object_stamp = 1;
600         vblank_create.base.data = NULL;
601         vblank_create.base.sync = 0;
602         vblank_create.vblank_stamp = private_vblank->stamp;
603
604         ret = tdm_thread_cb_call(private_vblank->dpy, &vblank_create.base, 1);
605         TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
606
607         return TDM_ERROR_NONE;
608 }
609
610 EXTERN tdm_error
611 tdm_vblank_add_create_handler(tdm_display *dpy, tdm_vblank_create_handler func, void *user_data)
612 {
613         tdm_vblank_create_handler_info *ch_info = NULL;
614         tdm_error ret;
615
616         TDM_RETURN_VAL_IF_FAIL(dpy != NULL, TDM_ERROR_INVALID_PARAMETER);
617         TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
618
619         pthread_mutex_lock(&valid_list_lock);
620
621         LIST_FOR_EACH_ENTRY(ch_info, &create_handler_list, link) {
622                 if (ch_info->func == func && ch_info->user_data == user_data) {
623                         TDM_ERR("can't be added twice");
624                         pthread_mutex_unlock(&valid_list_lock);
625                         return TDM_ERROR_BAD_REQUEST;
626                 }
627         }
628
629         ch_info = calloc(1, sizeof *ch_info);
630         if (!ch_info) {
631                 TDM_ERR("alloc failed: %m");
632                 pthread_mutex_unlock(&valid_list_lock);
633                 return TDM_ERROR_OUT_OF_MEMORY;
634         }
635
636         tdm_display_lock(dpy);
637         ret = tdm_thread_cb_add(dpy, TDM_THREAD_CB_VBLANK_CREATE, NULL, _tdm_vblank_thread_cb_create, ch_info);
638         tdm_display_unlock(dpy);
639
640         if (ret != TDM_ERROR_NONE) {
641                 TDM_ERR("tdm_thread_cb_add failed");
642                 free(ch_info);
643                 pthread_mutex_unlock(&valid_list_lock);
644                 return ret;
645         }
646
647         ch_info->func = func;
648         ch_info->user_data = user_data;
649
650         LIST_ADDTAIL(&ch_info->link, &create_handler_list);
651
652         pthread_mutex_unlock(&valid_list_lock);
653
654         return TDM_ERROR_NONE;
655 }
656
657 EXTERN void
658 tdm_vblank_remove_create_handler(tdm_display *dpy, tdm_vblank_create_handler func, void *user_data)
659 {
660         tdm_vblank_create_handler_info *ch_info = NULL, *hh = NULL;
661
662         TDM_RETURN_IF_FAIL(dpy != NULL);
663         TDM_RETURN_IF_FAIL(func != NULL);
664
665         tdm_display_lock(dpy);
666         pthread_mutex_lock(&valid_list_lock);
667         LIST_FOR_EACH_ENTRY_SAFE(ch_info, hh, &create_handler_list, link) {
668                 if (ch_info->func != func || ch_info->user_data != user_data)
669                         continue;
670
671                 tdm_thread_cb_remove(dpy, TDM_THREAD_CB_VBLANK_CREATE, NULL, _tdm_vblank_thread_cb_create, ch_info);
672
673                 LIST_DEL(&ch_info->link);
674                 free(ch_info);
675                 pthread_mutex_unlock(&valid_list_lock);
676                 tdm_display_unlock(dpy);
677                 return;
678         }
679         pthread_mutex_unlock(&valid_list_lock);
680         tdm_display_unlock(dpy);
681 }
682
683 static void*
684 _tdm_vblank_find_display(tdm_private_display *private_display, double stamp)
685 {
686         return private_display;
687 }
688
689 INTERN tdm_error
690 tdm_vblank_init(tdm_display *dpy)
691 {
692         if (vblank_list_inited)
693                 return TDM_ERROR_NONE;
694
695         LIST_INITHEAD(&valid_vblank_list);
696         LIST_INITHEAD(&valid_wait_list);
697         LIST_INITHEAD(&create_handler_list);
698
699         tdm_thread_cb_set_find_func(TDM_THREAD_CB_VBLANK_SW, _tdm_vblank_find_object);
700         tdm_thread_cb_set_find_func(TDM_THREAD_CB_VBLANK_CREATE, _tdm_vblank_find_display);
701
702         vblank_list_inited = 1;
703
704         return TDM_ERROR_NONE;
705 }
706
707 INTERN void
708 tdm_vblank_deinit(tdm_display *dpy)
709 {
710         tdm_vblank_create_handler_info *ch_info = NULL, *hh = NULL;
711
712         if (!vblank_list_inited)
713                 return;
714
715         if (!LIST_IS_EMPTY(&valid_vblank_list))
716                 TDM_NEVER_GET_HERE();
717
718         if (!LIST_IS_EMPTY(&valid_wait_list))
719                 TDM_NEVER_GET_HERE();
720
721         LIST_FOR_EACH_ENTRY_SAFE(ch_info, hh, &create_handler_list, link) {
722                 LIST_DEL(&ch_info->link);
723                 free(ch_info);
724         }
725
726         vblank_list_inited = 0;
727 }
728
729 EXTERN tdm_vblank *
730 tdm_vblank_create(tdm_display *dpy, tdm_output *output, tdm_error *error)
731 {
732         tdm_private_vblank *private_vblank;
733         tdm_error ret;
734
735         TDM_RETURN_VAL_IF_FAIL_WITH_ERROR(dpy != NULL, TDM_ERROR_INVALID_PARAMETER, NULL);
736         TDM_RETURN_VAL_IF_FAIL_WITH_ERROR(output != NULL, TDM_ERROR_INVALID_PARAMETER, NULL);
737
738         if (error)
739                 *error = TDM_ERROR_NONE;
740
741         private_vblank = calloc(1, sizeof * private_vblank);
742         if (!private_vblank) {
743                 /* LCOV_EXCL_START */
744                 if (error)
745                         *error = TDM_ERROR_OUT_OF_MEMORY;
746                 TDM_ERR("alloc failed");
747                 return NULL;
748                 /* LCOV_EXCL_STOP */
749         }
750
751         ret = tdm_output_add_change_handler(output, _tdm_vblank_cb_output_change, private_vblank);
752         if (ret != TDM_ERROR_NONE) {
753                 free(private_vblank);
754                 if (error)
755                         *error = TDM_ERROR_OPERATION_FAILED;
756                 TDM_ERR("tdm_output_add_change_handler failed");
757                 return NULL;
758         }
759
760         private_vblank->stamp = ++stamp;
761         private_vblank->owner_tid = syscall(SYS_gettid);
762         private_vblank->dpy = dpy;
763         private_vblank->output = output;
764         private_vblank->check_HW_or_SW = 1;
765         private_vblank->fps_changeable = 1;
766         private_vblank->last_interval = -1;
767
768         _tdm_vblank_update_output_info(private_vblank);
769
770         strncpy(private_vblank->name, TDM_VBLANK_DEFAULT_NAME, TDM_NAME_LEN - 1);
771         private_vblank->name[TDM_NAME_LEN - 1] = '\0';
772
773         LIST_INITHEAD(&private_vblank->HW_wait_list);
774         LIST_INITHEAD(&private_vblank->SW_wait_list);
775
776         _tdm_vblank_valid_list_add(&private_vblank->valid_link, &valid_vblank_list);
777
778         VIN("created. vrefresh(%d) connection(%d)",
779                 private_vblank->vrefresh, private_vblank->connection);
780
781         tdm_display_lock(private_vblank->dpy);
782         _tdm_vblank_call_thread_cb(private_vblank);
783         tdm_display_unlock(private_vblank->dpy);
784
785         return (tdm_vblank *)private_vblank;
786 }
787
788 EXTERN void
789 tdm_vblank_destroy(tdm_vblank *vblank)
790 {
791         tdm_private_vblank *private_vblank = vblank;
792         tdm_vblank_wait_info *w = NULL, *ww = NULL;
793
794         TDM_RETURN_IF_FAIL(tdm_vblank_is_valid(vblank));
795
796         if (private_vblank->in_create_handler) {
797                 TDM_ERR("NOT allowed to be called in a create handler");
798                 return;
799         }
800
801         _tdm_vblank_valid_list_del(&private_vblank->valid_link);
802
803         if (private_vblank->SW_timer) {
804                 tdm_display_lock(private_vblank->dpy);
805                 tdm_thread_cb_remove(private_vblank, TDM_THREAD_CB_VBLANK_SW, NULL, _tdm_vblank_cb_vblank_SW, NULL);
806                 tdm_event_loop_source_remove(private_vblank->SW_timer);
807                 tdm_display_unlock(private_vblank->dpy);
808         }
809
810         tdm_output_remove_change_handler(private_vblank->output,
811                                                                          _tdm_vblank_cb_output_change, private_vblank);
812
813         _tdm_vblank_free_HW_wait(private_vblank, 0, 0);
814
815         if (!LIST_IS_EMPTY(&private_vblank->SW_wait_list)) {
816                 pid_t pid;
817                 const char *proc_name;
818                 _tdm_vblank_get_client_information(private_vblank, &pid, &proc_name);
819                 VER("TDM SW vblank destroyed forcely!! (pid: %u, name: %s)", pid, proc_name);
820         }
821
822         LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->SW_wait_list, link) {
823                 LIST_DEL(&w->link);
824                 _tdm_vblank_valid_list_del(&w->valid_link);
825                 free(w);
826         }
827
828         if (private_vblank->timeout_timer) {
829                 tdm_display_lock(private_vblank->dpy);
830                 tdm_event_loop_source_remove(private_vblank->timeout_timer);
831                 tdm_display_unlock(private_vblank->dpy);
832         }
833
834         VIN("destroyed");
835
836         free(private_vblank);
837 }
838
839 EXTERN tdm_output *
840 tdm_vblank_get_output(tdm_vblank *vblank, tdm_error *error)
841 {
842         tdm_private_vblank *private_vblank = vblank;
843         tdm_error ret = TDM_ERROR_NONE;
844
845         TDM_RETURN_VAL_IF_FAIL_WITH_ERROR(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER, NULL);
846
847         if (error)
848                 *error = TDM_ERROR_NONE;
849
850         return private_vblank->output;
851 }
852
853 EXTERN tdm_error
854 tdm_vblank_get_client_pid(tdm_vblank *vblank, pid_t *pid)
855 {
856         tdm_private_vblank *private_vblank = vblank;
857         struct wl_client *client;
858
859         TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
860         TDM_RETURN_VAL_IF_FAIL(pid != NULL, TDM_ERROR_INVALID_PARAMETER);
861
862         if (!private_vblank->resource) {
863                 *pid = 0;
864                 return TDM_ERROR_NONE;
865         }
866
867         client = wl_resource_get_client(private_vblank->resource);
868         if (!client) {
869                 *pid = 0;
870                 return TDM_ERROR_NONE;
871         }
872
873         wl_client_get_credentials(client, pid, NULL, NULL);
874
875         return TDM_ERROR_NONE;
876 }
877
878 EXTERN tdm_error
879 tdm_vblank_set_name(tdm_vblank *vblank, const char *name)
880 {
881         tdm_private_vblank *private_vblank = vblank;
882
883         TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
884
885         if (!name)
886                 name = TDM_VBLANK_DEFAULT_NAME;
887
888         strncpy(private_vblank->name, name, TDM_NAME_LEN - 1);
889         private_vblank->name[TDM_NAME_LEN - 1] = '\0';
890
891         VIN("name(%s)", name);
892
893         return TDM_ERROR_NONE;
894 }
895
896 EXTERN tdm_error
897 tdm_vblank_get_name(tdm_vblank *vblank, const char **name)
898 {
899         tdm_private_vblank *private_vblank = vblank;
900
901         TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
902         TDM_RETURN_VAL_IF_FAIL(name != NULL, TDM_ERROR_INVALID_PARAMETER);
903
904         *name = (const char*)private_vblank->name;
905
906         return TDM_ERROR_NONE;
907 }
908
909 EXTERN tdm_error
910 tdm_vblank_set_fps(tdm_vblank *vblank, unsigned int fps)
911 {
912         tdm_private_vblank *private_vblank = vblank;
913
914         TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
915         TDM_RETURN_VAL_IF_FAIL(fps > 0, TDM_ERROR_INVALID_PARAMETER);
916
917         if (private_vblank->fps_fixed) {
918                 VIN("fps(%u) can't be changed", private_vblank->fps);
919                 return TDM_ERROR_NONE;
920         }
921
922         private_vblank->fps_changeable = 0;
923
924         if (private_vblank->fps == fps)
925                 return TDM_ERROR_NONE;
926
927         private_vblank->fps = fps;
928         private_vblank->check_HW_or_SW = 1;
929
930         VIN("fps(%u) changed", fps);
931
932         return TDM_ERROR_NONE;
933 }
934
935 EXTERN tdm_error
936 tdm_vblank_get_fps(tdm_vblank *vblank, unsigned int *fps)
937 {
938         tdm_private_vblank *private_vblank = vblank;
939
940         TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
941         TDM_RETURN_VAL_IF_FAIL(fps != NULL, TDM_ERROR_INVALID_PARAMETER);
942
943         *fps = private_vblank->fps;
944
945         return TDM_ERROR_NONE;
946 }
947
948 EXTERN tdm_error
949 tdm_vblank_set_fixed_fps(tdm_vblank *vblank, unsigned int fps)
950 {
951         tdm_private_vblank *private_vblank = vblank;
952
953         TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
954         TDM_RETURN_VAL_IF_FAIL(fps > 0, TDM_ERROR_INVALID_PARAMETER);
955
956         private_vblank->fps_changeable = 0;
957         private_vblank->fps_fixed = 1;
958
959         if (private_vblank->fps == fps)
960                 return TDM_ERROR_NONE;
961
962         private_vblank->fps = fps;
963         private_vblank->check_HW_or_SW = 1;
964
965         VIN("fps(%u) fixed", fps);
966
967         return TDM_ERROR_NONE;
968 }
969
970 EXTERN tdm_error
971 tdm_vblank_ignore_global_fps(tdm_vblank *vblank, unsigned int ignore)
972 {
973         tdm_private_vblank *private_vblank = vblank;
974
975         TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
976
977         if (private_vblank->ignore_global_fps == ignore)
978                 return TDM_ERROR_NONE;
979
980         private_vblank->ignore_global_fps = ignore;
981         private_vblank->check_HW_or_SW = 1;
982
983         VIN("ignore_global_fps(%u)", private_vblank->ignore_global_fps);
984
985         return TDM_ERROR_NONE;
986 }
987
988 EXTERN tdm_error
989 tdm_vblank_set_offset(tdm_vblank *vblank, int offset)
990 {
991         tdm_private_vblank *private_vblank = vblank;
992
993         TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
994         TDM_RETURN_VAL_IF_FAIL(offset >= 0, TDM_ERROR_INVALID_PARAMETER);
995
996         if (private_vblank->offset == offset)
997                 return TDM_ERROR_NONE;
998
999         private_vblank->offset = offset;
1000         private_vblank->check_HW_or_SW = 1;
1001
1002         VIN("offset(%u)", private_vblank->offset);
1003
1004         return TDM_ERROR_NONE;
1005 }
1006
1007 EXTERN tdm_error
1008 tdm_vblank_get_offset(tdm_vblank *vblank, int *offset)
1009 {
1010         tdm_private_vblank *private_vblank = vblank;
1011
1012         TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1013         TDM_RETURN_VAL_IF_FAIL(offset != NULL, TDM_ERROR_INVALID_PARAMETER);
1014
1015         *offset = private_vblank->offset;
1016
1017         return TDM_ERROR_NONE;
1018 }
1019
1020 EXTERN tdm_error
1021 tdm_vblank_set_enable_fake(tdm_vblank *vblank, unsigned int enable_fake)
1022 {
1023         tdm_private_vblank *private_vblank = vblank;
1024
1025         TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1026
1027         if (private_vblank->enable_fake == enable_fake)
1028                 return TDM_ERROR_NONE;
1029
1030         private_vblank->enable_fake = enable_fake;
1031
1032         VIN("enable_fake(%u)", private_vblank->enable_fake);
1033
1034         return TDM_ERROR_NONE;
1035 }
1036
1037 EXTERN tdm_error
1038 tdm_vblank_get_enable_fake(tdm_vblank *vblank, unsigned int *enable_fake)
1039 {
1040         tdm_private_vblank *private_vblank = vblank;
1041
1042         TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1043         TDM_RETURN_VAL_IF_FAIL(enable_fake != NULL, TDM_ERROR_INVALID_PARAMETER);
1044
1045         *enable_fake = private_vblank->enable_fake;
1046
1047         return TDM_ERROR_NONE;
1048 }
1049
1050 static tdm_error
1051 _tdm_vblank_timeout_timer_cb(void *user_data)
1052 {
1053         tdm_private_vblank *private_vblank = user_data;
1054         tdm_vblank_wait_info *w = NULL;
1055         pid_t pid;
1056         const char *proc_name;
1057
1058         TDM_RETURN_VAL_IF_FAIL(private_vblank != NULL, TDM_ERROR_OPERATION_FAILED);
1059
1060         _tdm_vblank_get_client_information(private_vblank, &pid, &proc_name);
1061
1062         VER("TDM vblank TIMEOUT!! (pid: %u, name: %s)", pid, proc_name);
1063         VER("quotient(%u) last_seq(%u) last_time(%.6f)", private_vblank->quotient, private_vblank->last_seq, private_vblank->last_time);
1064
1065         LIST_FOR_EACH_ENTRY(w, &private_vblank->HW_wait_list, link) {
1066                 VER("wait_info(%p) req_time(%.6f) target_seq(%u) interval(%u) hw_interval(%d)",
1067                         w, w->req_time, w->target_seq, w->interval, w->hw_interval);
1068                 tdm_output_vblank_print_wait_information(private_vblank->output, w);
1069         }
1070
1071         LIST_FOR_EACH_ENTRY(w, &private_vblank->SW_wait_list, link) {
1072                 VER("wait_info(%p) req_time(%.6f) target_time(%.6f) target_seq(%u) interval(%u)",
1073                         w, w->req_time, w->target_time, w->target_seq, w->interval);
1074         }
1075
1076         return TDM_ERROR_NONE;
1077 }
1078
1079 static void
1080 _tdm_vblank_timeout_timer_update(tdm_private_vblank *private_vblank, int ms_delay)
1081 {
1082         tdm_error ret;
1083
1084         if (!private_vblank->timeout_timer) {
1085                 private_vblank->timeout_timer =
1086                         tdm_event_loop_add_timer_handler(private_vblank->dpy,
1087                                                                                          _tdm_vblank_timeout_timer_cb,
1088                                                                                          private_vblank,
1089                                                                                          &ret);
1090                 if (!private_vblank->timeout_timer) {
1091                         VER("couldn't add timer");
1092                         return;
1093                 }
1094                 VIN("Create Timeout timer");
1095         }
1096
1097         ret = tdm_event_loop_source_timer_update(private_vblank->timeout_timer, ms_delay);
1098         if (ret != TDM_ERROR_NONE) {
1099                 VER("couldn't update timer");
1100                 return;
1101         }
1102
1103         if (ms_delay != 0)
1104                 VDB("timeout timer updated");
1105         else
1106                 VDB("timeout timer disabled");
1107 }
1108
1109 static tdm_error
1110 _tdm_vblank_sw_timer_update(tdm_private_vblank *private_vblank)
1111 {
1112         tdm_vblank_wait_info *first_wait_info = NULL;
1113         double curr, target;
1114         int ms_delay;
1115         tdm_error ret;
1116
1117         if (LIST_IS_EMPTY(&private_vblank->SW_wait_list)) {
1118                 VER("no wait_info");
1119                 return TDM_ERROR_OPERATION_FAILED;
1120         }
1121
1122         first_wait_info = container_of(private_vblank->SW_wait_list.next, first_wait_info, link);
1123         curr = tdm_helper_get_time();
1124         target = first_wait_info->target_time;
1125
1126         /* ms_delay should be more that 1 */
1127         if (target < curr)
1128                 ms_delay = 1;
1129         else
1130                 ms_delay = ceil((target - curr) * 1000.0);
1131
1132         if (ms_delay < 1)
1133                 ms_delay = 1;
1134
1135         if (ms_delay > 5000)
1136                 VER("wait(%p) curr(%.6f) target(%.6f) ms_delay(%d)", first_wait_info, curr, target, ms_delay);
1137         else
1138                 VDB("wait(%p) curr(%.6f) target(%.6f) ms_delay(%d)", first_wait_info, curr, target, ms_delay);
1139
1140         tdm_display_lock(private_vblank->dpy);
1141
1142         if (!private_vblank->SW_timer) {
1143                 private_vblank->SW_timer =
1144                         tdm_event_loop_add_timer_handler(private_vblank->dpy,
1145                                                                                          _tdm_vblank_cb_timeout_SW,
1146                                                                                          private_vblank,
1147                                                                                          &ret);
1148                 if (!private_vblank->SW_timer) {
1149                         tdm_display_unlock(private_vblank->dpy);
1150                         VER("couldn't add timer");
1151                         return ret;
1152                 }
1153
1154                 ret = tdm_thread_cb_add(private_vblank, TDM_THREAD_CB_VBLANK_SW, NULL, _tdm_vblank_cb_vblank_SW, NULL);
1155                 if (ret != TDM_ERROR_NONE) {
1156                         tdm_event_loop_source_remove(private_vblank->SW_timer);
1157                         private_vblank->SW_timer = NULL;
1158                         tdm_display_unlock(private_vblank->dpy);
1159                         VER("couldn't add thread_cb");
1160                         return ret;
1161                 }
1162
1163                 VIN("Create SW timer");
1164         }
1165
1166         private_vblank->last_interval = ms_delay;
1167
1168         ret = tdm_event_loop_source_timer_update(private_vblank->SW_timer, ms_delay);
1169         if (ret != TDM_ERROR_NONE) {
1170                 /* LCOV_EXCL_START */
1171                 tdm_display_unlock(private_vblank->dpy);
1172                 VER("couldn't update timer");
1173                 return ret;
1174                 /* LCOV_EXCL_STOP */
1175         }
1176
1177         tdm_display_unlock(private_vblank->dpy);
1178
1179         return TDM_ERROR_NONE;
1180 }
1181
1182 static void
1183 _tdm_vblank_cb_vblank_HW(tdm_output *output, unsigned int sequence,
1184                                                  unsigned int tv_sec, unsigned int tv_usec,
1185                                                  void *user_data)
1186 {
1187         tdm_vblank_wait_info *wait_info = user_data;
1188         tdm_private_vblank *private_vblank;
1189
1190         if (!_tdm_vblank_check_valid_wait(wait_info)) {
1191                 TDM_ERR("can't find wait(%p) from valid_wait_list", wait_info);
1192                 return;
1193         }
1194
1195         private_vblank = wait_info->private_vblank;
1196         TDM_RETURN_IF_FAIL(tdm_vblank_is_valid(private_vblank));
1197
1198         assert(private_vblank->owner_tid == syscall(SYS_gettid));
1199
1200         if (!_tdm_vblank_find_wait(wait_info, &private_vblank->HW_wait_list)) {
1201                 VER("can't find wait(%p)", wait_info);
1202                 return;
1203         }
1204
1205         if (wait_info->type == VBLANK_TYPE_HW_SW) {
1206                 tdm_error ret;
1207
1208                 LIST_DEL(&wait_info->link);
1209
1210                 /* kernel sends the wrong time of first vblank event. and the time of
1211                  * second vblank is correct.
1212                  */
1213                 wait_info->target_time = TDM_TIME(tv_sec, tv_usec);
1214                 wait_info->target_time += (((double)private_vblank->offset) / 1000.0);
1215
1216                 _tdm_vblank_insert_wait(wait_info, &private_vblank->SW_wait_list);
1217
1218                 ret = _tdm_vblank_sw_timer_update(private_vblank);
1219
1220                 /* wait_info will be freed in _tdm_vblank_cb_timeout_SW() */
1221                 if (ret == TDM_ERROR_NONE) {
1222                         VIN("wait(%p) SW timer", wait_info);
1223                         return;
1224                 }
1225
1226                 VER("couldn't update sw timer");
1227         }
1228
1229         tdm_display_lock(private_vblank->dpy);
1230         _tdm_vblank_timeout_timer_update(private_vblank, 0);
1231         tdm_display_unlock(private_vblank->dpy);
1232
1233         LIST_DEL(&wait_info->link);
1234         _tdm_vblank_valid_list_del(&wait_info->valid_link);
1235
1236         /* tv_sec & tv_usec shouldn't be zero. But sometimes they are zero in some hardware.
1237          * We need to prohibit that last_time becomes zero because "last_time == 0" means
1238          * that a tdm_vblank object is just created and doesn't have the first vblank event.
1239          * If last_time becomes 0, it would make issue.
1240          */
1241         if (tv_sec == 0 && tv_usec == 0) {
1242                 double curr = tdm_helper_get_time();
1243                 tv_sec = TDM_TIME_SEC(curr);
1244                 tv_usec = TDM_TIME_USEC(curr);
1245         }
1246
1247         private_vblank->last_seq = wait_info->target_seq;
1248         private_vblank->last_time = TDM_TIME(tv_sec, tv_usec);
1249
1250         VDB("wait(%p) last(%.6f) sequence(%u) done", wait_info, private_vblank->last_time, wait_info->target_seq);
1251
1252         if (tdm_ttrace_module & TDM_TTRACE_VBLANK)
1253                 TDM_TRACE_ASYNC_END((int)wait_info->req_time, "TDM_Vblank:%u", (unsigned int)private_vblank->stamp);
1254
1255         if (wait_info->func)
1256                 wait_info->func(private_vblank, TDM_ERROR_NONE, wait_info->target_seq,
1257                                                 tv_sec, tv_usec, wait_info->user_data);
1258
1259         free(wait_info);
1260 }
1261
1262 static tdm_error
1263 _tdm_vblank_wait_HW(tdm_vblank_wait_info *wait_info)
1264 {
1265         tdm_private_vblank *private_vblank = wait_info->private_vblank;
1266         int hw_interval;
1267         tdm_error ret;
1268
1269         if (private_vblank->last_type != VBLANK_EVENT_TYPE_HW) {
1270                 VIN("Use HW vblank");
1271                 private_vblank->last_type = VBLANK_EVENT_TYPE_HW;
1272         }
1273
1274         _tdm_vblank_insert_wait(wait_info, &private_vblank->HW_wait_list);
1275
1276         if (private_vblank->last_time == 0) {
1277                 hw_interval = wait_info->interval * private_vblank->quotient;
1278
1279                 /* SW vblank starts from now. SW vblank doesn't need to be aligned with HW vblank. */
1280                 private_vblank->last_seq = 0;
1281                 private_vblank->last_time = tdm_helper_get_time();
1282
1283                 /* +1 ms to call the handler ASAP at the first. no matter for SW timer. */
1284                 wait_info->target_seq = 1;
1285
1286                 VDB("wait(%p) last(%.6f) hw_itvl(%d) targ_seq(%u)",
1287                         wait_info, private_vblank->last_time, hw_interval, wait_info->target_seq);
1288         } else {
1289                 double last, prev, req, curr, target;
1290                 unsigned int skip;
1291
1292                 last = private_vblank->last_time;
1293                 req = wait_info->req_time;
1294
1295                 /* make sure that req time is bigger than last. */
1296                 if (req < last)
1297                         req = last;
1298
1299                 skip = (unsigned int)((req - last) / private_vblank->vblank_gap + TDM_TIME_MARGIN);
1300                 prev = last + private_vblank->vblank_gap * skip;
1301
1302                 curr = tdm_helper_get_time();
1303                 target = last;
1304                 while (target < curr)
1305                         target += (private_vblank->vblank_gap * wait_info->interval);
1306
1307                 hw_interval = (unsigned int)((target - curr) / private_vblank->HW_vblank_gap + TDM_TIME_MARGIN) + 1;
1308
1309                 wait_info->target_seq = private_vblank->last_seq;
1310                 wait_info->target_seq += (unsigned int)((target - last) / private_vblank->vblank_gap + TDM_TIME_MARGIN);
1311
1312                 if (hw_interval > 300)
1313                         VER("wait(%p) last(%.6f) req(%.6f) prev(%.6f) curr(%.6f) targ(%.6f,%.6f) skip(%u) hw_itvl(%d) targ_seq(%u)",
1314                                 wait_info, last, req - last, prev - last, curr - last, target, target - last,
1315                                 skip, hw_interval, wait_info->target_seq);
1316                 else
1317                         VDB("wait(%p) last(%.6f) req(%.6f) prev(%.6f) curr(%.6f) targ(%.6f,%.6f) skip(%u) hw_itvl(%d) targ_seq(%u)",
1318                                 wait_info, last, req - last, prev - last, curr - last, target, target - last,
1319                                 skip, hw_interval, wait_info->target_seq);
1320         }
1321
1322         private_vblank->last_interval = hw_interval;
1323         wait_info->hw_interval = hw_interval;
1324
1325         if (tdm_ttrace_module & TDM_TTRACE_VBLANK)
1326                 TDM_TRACE_ASYNC_BEGIN((int)wait_info->req_time, "TDM_Vblank:%u", (unsigned int)private_vblank->stamp);
1327
1328         if (private_vblank->add_front)
1329                 ret = tdm_output_wait_vblank_add_front(private_vblank->output, hw_interval, 0,
1330                                                                                            _tdm_vblank_cb_vblank_HW, wait_info);
1331         else
1332                 ret = tdm_output_wait_vblank(private_vblank->output, hw_interval, 0,
1333                                                                          _tdm_vblank_cb_vblank_HW, wait_info);
1334
1335         if (ret != TDM_ERROR_NONE) {
1336                 LIST_DEL(&wait_info->link);
1337                 return ret;
1338         }
1339
1340         VDB("wait(%p) waiting", wait_info);
1341
1342         return TDM_ERROR_NONE;
1343 }
1344
1345 static void
1346 _tdm_vblank_cb_vblank_SW(tdm_private_display *private_display, void *object, tdm_thread_cb_base *cb_base, void *user_data)
1347 {
1348         tdm_private_vblank *private_vblank = object;
1349         tdm_vblank_wait_info *first_wait_info = NULL, *w = NULL, *ww = NULL;
1350
1351         TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
1352
1353         assert(private_vblank->owner_tid == syscall(SYS_gettid));
1354
1355         if (LIST_IS_EMPTY(&private_vblank->SW_wait_list)) {
1356                 VER("no wait_info");
1357                 return;
1358         }
1359
1360         if (!LIST_IS_EMPTY(&private_vblank->HW_wait_list))
1361                 TDM_NEVER_GET_HERE();
1362
1363         first_wait_info = container_of(private_vblank->SW_wait_list.next, first_wait_info, link);
1364         TDM_RETURN_IF_FAIL(first_wait_info != NULL);
1365
1366         _tdm_vblank_timeout_timer_update(private_vblank, 0);
1367
1368         if (private_vblank->last_seq >= first_wait_info->target_seq)
1369                 TDM_ERR("last_seq(%u) target_seq(%u)", private_vblank->last_seq, first_wait_info->target_seq);
1370
1371         private_vblank->last_seq = first_wait_info->target_seq;
1372         private_vblank->last_time = first_wait_info->target_time;
1373
1374         VDB("wait(%p) last(%.6f) sequence(%u) done", first_wait_info, private_vblank->last_time, first_wait_info->target_seq);
1375
1376         LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->SW_wait_list, link) {
1377                 if (w->target_time != first_wait_info->target_time)
1378                         break;
1379
1380                 LIST_DEL(&w->link);
1381                 _tdm_vblank_valid_list_del(&w->valid_link);
1382
1383                 if (w->func) {
1384                         tdm_display_unlock(private_vblank->dpy);
1385                         w->func(private_vblank, TDM_ERROR_NONE, w->target_seq,
1386                                         TDM_TIME_SEC(w->target_time), TDM_TIME_USEC(w->target_time),
1387                                         w->user_data);
1388                         tdm_display_lock(private_vblank->dpy);
1389                 }
1390
1391                 free(w);
1392         }
1393 }
1394
1395 static tdm_error
1396 _tdm_vblank_cb_timeout_SW(void *user_data)
1397 {
1398         tdm_private_vblank *private_vblank = user_data;
1399         tdm_thread_cb_vblank_sw vblank_sw;
1400         tdm_error ret;
1401
1402         TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(private_vblank), TDM_ERROR_OPERATION_FAILED);
1403
1404         VIN("timeout_SW -> thread-cb");
1405
1406         memset(&vblank_sw, 0, sizeof vblank_sw);
1407         vblank_sw.base.type = TDM_THREAD_CB_VBLANK_SW;
1408         vblank_sw.base.length = sizeof vblank_sw;
1409         vblank_sw.base.object_stamp = private_vblank->stamp;
1410         vblank_sw.base.data = NULL;
1411         vblank_sw.base.sync = 0;
1412
1413         ret = tdm_thread_cb_call(private_vblank, &vblank_sw.base, 1);
1414         TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
1415
1416         return TDM_ERROR_NONE;
1417 }
1418
1419 static tdm_error
1420 _tdm_vblank_wait_SW(tdm_vblank_wait_info *wait_info)
1421 {
1422         tdm_private_vblank *private_vblank = wait_info->private_vblank;
1423         tdm_error ret;
1424         double last, prev, req, curr, target;
1425         unsigned int skip;
1426
1427         if (private_vblank->last_type != VBLANK_EVENT_TYPE_SW) {
1428                 tdm_output_dpms dpms;
1429
1430                 tdm_output_get_dpms(private_vblank->output, &dpms);
1431
1432                 VIN("Use SW vblank. connection(%s), dpms(%s), vrefresh(%d), fps(%d), offset(%d)",
1433                         tdm_status_str(private_vblank->connection), tdm_dpms_str(dpms),
1434                         private_vblank->vrefresh, private_vblank->fps, private_vblank->offset);
1435                 private_vblank->last_type = VBLANK_EVENT_TYPE_SW;
1436         }
1437
1438         if (private_vblank->last_time == 0) {
1439                 /* SW vblank starts from now. SW vblank doesn't need to be aligned with HW vblank. */
1440                 private_vblank->last_seq = 0;
1441                 private_vblank->last_time = tdm_helper_get_time() - private_vblank->vblank_gap;
1442
1443                 /* +1 ms to call the handler ASAP at the first. no matter for SW timer. */
1444                 wait_info->target_seq = 1;
1445         }
1446
1447         last = private_vblank->last_time;
1448         req = wait_info->req_time;
1449
1450         /* make sure that req time is bigger than last. */
1451         if (req < last)
1452                 req = last;
1453
1454         skip = (unsigned int)((req - last) / private_vblank->vblank_gap + TDM_TIME_MARGIN);
1455         prev = last + private_vblank->vblank_gap * skip;
1456
1457         curr = tdm_helper_get_time();
1458         target = prev + private_vblank->vblank_gap * wait_info->interval;
1459
1460         while (target < curr)
1461                 target += private_vblank->vblank_gap;
1462
1463         wait_info->target_seq = private_vblank->last_seq;
1464         wait_info->target_seq += (unsigned int)((target - last) / private_vblank->vblank_gap + TDM_TIME_MARGIN);
1465
1466         wait_info->target_time = target + (double)(private_vblank->offset) / 1000;
1467
1468         VDB("wait(%p) last(%.6f) req(%.6f) prev(%.6f) curr(%.6f) targ(%.6f,%.6f) skip(%u) targ_seq(%u)",
1469                 wait_info, last, req - last, prev - last, curr - last,
1470                 target, target - last, skip, wait_info->target_seq);
1471
1472         _tdm_vblank_insert_wait(wait_info, &private_vblank->SW_wait_list);
1473
1474         ret = _tdm_vblank_sw_timer_update(private_vblank);
1475         if (ret != TDM_ERROR_NONE) {
1476                 /* LCOV_EXCL_START */
1477                 LIST_DEL(&wait_info->link);
1478                 VER("couldn't update sw timer");
1479                 return ret;
1480                 /* LCOV_EXCL_STOP */
1481         }
1482
1483         return TDM_ERROR_NONE;
1484 }
1485
1486 EXTERN tdm_error
1487 tdm_vblank_wait(tdm_vblank *vblank, unsigned int req_sec, unsigned int req_usec,
1488                                 unsigned int interval, tdm_vblank_handler func, void *user_data)
1489 {
1490         tdm_private_vblank *private_vblank = vblank;
1491         tdm_vblank_wait_info *wait_info;
1492         tdm_output_dpms dpms = TDM_OUTPUT_DPMS_OFF;
1493         unsigned int fps;
1494         tdm_error ret;
1495
1496         TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1497         TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
1498         TDM_RETURN_VAL_IF_FAIL(interval > 0, TDM_ERROR_INVALID_PARAMETER);
1499
1500         if (private_vblank->in_create_handler) {
1501                 TDM_ERR("NOT allowed to be called in a create handler");
1502                 return TDM_ERROR_BAD_REQUEST;
1503         }
1504
1505         if (private_vblank->owner_tid != syscall(SYS_gettid)) {
1506                 TDM_ERR("SHOULD be called in the owner thread(%d)", (int)private_vblank->owner_tid);
1507                 return TDM_ERROR_BAD_REQUEST;
1508         }
1509
1510         tdm_output_get_dpms(private_vblank->output, &dpms);
1511
1512         if (!private_vblank->enable_fake) {
1513                 if (private_vblank->connection == TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
1514                         VER("can't wait a vblank: output disconnected");
1515                         return TDM_ERROR_OUTPUT_DISCONNECTED;
1516                 }
1517                 if (TDM_OUTPUT_DPMS_VSYNC_IS_OFF(dpms)) {
1518                         VER("can't wait a vblank: DPMS %s", tdm_dpms_str(dpms));
1519                         return TDM_ERROR_DPMS_OFF;
1520                 }
1521         }
1522
1523         wait_info = calloc(1, sizeof * wait_info);
1524         if (!wait_info) {
1525                 /* LCOV_EXCL_START */
1526                 VER("alloc failed");
1527                 return TDM_ERROR_OUT_OF_MEMORY;
1528                 /* LCOV_EXCL_STOP */
1529         }
1530         _tdm_vblank_update_output_info(private_vblank);
1531
1532         /* set request time to current time if 0. This function seems to be called in server side. */
1533         if (req_sec == 0 && req_usec == 0) {
1534                 double curr = tdm_helper_get_time();
1535                 req_sec = TDM_TIME_SEC(curr);
1536                 req_usec = TDM_TIME_USEC(curr);
1537         }
1538
1539         LIST_INITHEAD(&wait_info->link);
1540
1541         _tdm_vblank_valid_list_add(&wait_info->valid_link, &valid_wait_list);
1542
1543         wait_info->stamp = ++stamp;
1544         wait_info->req_time = TDM_TIME(req_sec, req_usec);
1545         wait_info->interval = interval;
1546         wait_info->func = func;
1547         wait_info->user_data = user_data;
1548         wait_info->private_vblank = private_vblank;
1549
1550         if (!private_vblank->resource ||
1551                 private_vblank->ignore_global_fps ||
1552                 vblank_global_fps == 0 ||
1553                 private_vblank->fps < vblank_global_fps)
1554                 fps = private_vblank->fps;
1555         else
1556                 fps = vblank_global_fps;
1557
1558         if (private_vblank->check_HW_or_SW) {
1559                 private_vblank->check_HW_or_SW = 0;
1560                 private_vblank->vblank_gap = 1.0 / fps;
1561                 private_vblank->quotient = private_vblank->vrefresh / fps;
1562         }
1563
1564         /* 1) if fps != factor of vrefresh, SW timer
1565          * 2) if fps == factor of vrefresh && dpms == off, SW timer (Fake HW vblank)
1566          * 2) if fps == factor of vrefresh && dpms == on && offset == 0, HW vblank
1567          * 3) if fps == factor of vrefresh && dpms == on && offset != 0, HW vblank + SW timer
1568          * In case of 1), we really don't need to align with HW vblank.
1569          */
1570         if (private_vblank->vrefresh % fps)
1571                 wait_info->type = VBLANK_TYPE_SW;
1572         else if (TDM_OUTPUT_DPMS_VSYNC_IS_OFF(dpms) ||
1573                          private_vblank->connection != TDM_OUTPUT_CONN_STATUS_MODE_SETTED)
1574                 wait_info->type = VBLANK_TYPE_SW_FAKE;
1575         else if (private_vblank->offset == 0)
1576                 wait_info->type = VBLANK_TYPE_HW;
1577         else
1578                 wait_info->type = VBLANK_TYPE_HW_SW;
1579
1580         if (wait_info->type == VBLANK_TYPE_SW || wait_info->type == VBLANK_TYPE_SW_FAKE) {
1581                 ret = _tdm_vblank_wait_SW(wait_info);
1582                 if (ret != TDM_ERROR_NONE)
1583                         TDM_ERR("_tdm_vblank_wait_SW failed: ret(%d)", ret);
1584         } else {
1585                 ret = _tdm_vblank_wait_HW(wait_info);
1586                 if (ret != TDM_ERROR_NONE)
1587                         TDM_ERR("_tdm_vblank_wait_HW failed: ret(%d)", ret);
1588
1589                 if (ret == TDM_ERROR_OUTPUT_DISCONNECTED || ret == TDM_ERROR_DPMS_OFF) {
1590                         ret = _tdm_vblank_wait_SW(wait_info);
1591                         if (ret != TDM_ERROR_NONE)
1592                                 TDM_ERR("_tdm_vblank_wait_SW failed: ret(%d)", ret);
1593                 }
1594         }
1595
1596         if (ret != TDM_ERROR_NONE) {
1597                 /* LCOV_EXCL_START */
1598                 LIST_DEL(&wait_info->link);
1599                 _tdm_vblank_valid_list_del(&wait_info->valid_link);
1600
1601                 tdm_display_lock(private_vblank->dpy);
1602                 tdm_output_remove_vblank_handler_internal(private_vblank->output, _tdm_vblank_cb_vblank_HW, wait_info);
1603                 tdm_display_unlock(private_vblank->dpy);
1604
1605                 free(wait_info);
1606                 return ret;
1607                 /* LCOV_EXCL_STOP */
1608         }
1609
1610         tdm_display_lock(private_vblank->dpy);
1611         _tdm_vblank_timeout_timer_update(private_vblank, 1000);
1612         tdm_display_unlock(private_vblank->dpy);
1613
1614         return TDM_ERROR_NONE;
1615 }
1616
1617 EXTERN tdm_error
1618 tdm_vblank_wait_seq(tdm_vblank *vblank, unsigned int req_sec, unsigned int req_usec,
1619                                         unsigned int sequence, tdm_vblank_handler func, void *user_data)
1620 {
1621         tdm_private_vblank *private_vblank = vblank;
1622         unsigned int interval;
1623
1624         TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1625         TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
1626
1627         /* if the sequence of vblank reaches the max value, sequence can be 0. */
1628
1629         if (private_vblank->last_seq == 0) {
1630                 VWR("can't calculate interval with sequence(%u)", sequence);
1631                 interval = 1;
1632         } else if (sequence > private_vblank->last_seq ||
1633                            TDM_VBLANK_SEQ_REACHED_MAX(sequence, private_vblank->last_seq)) {
1634                 double last, curr, seq_target;
1635
1636                 last = private_vblank->last_time;
1637                 curr = tdm_helper_get_time();
1638                 seq_target = last + private_vblank->vblank_gap * (sequence - private_vblank->last_seq);
1639
1640                 /* If target is old or too close(1ms) to current, we call the handler immediately.
1641                  * 2ms? it seems too much.
1642                  */
1643                 if (seq_target < curr) {
1644                         func(vblank, TDM_ERROR_NONE, sequence,
1645                                  TDM_TIME_SEC(seq_target), TDM_TIME_USEC(seq_target),
1646                                  user_data);
1647                         return TDM_ERROR_NONE;
1648                 } else if ((seq_target - curr) < 0.001) {  /* 1ms */
1649                         func(vblank, TDM_ERROR_NONE, sequence,
1650                                  TDM_TIME_SEC(curr), TDM_TIME_USEC(curr),
1651                                  user_data);
1652                         return TDM_ERROR_NONE;
1653                 }
1654
1655                 interval = (unsigned int)((seq_target - curr) / private_vblank->vblank_gap + TDM_TIME_MARGIN) + 1;
1656         } else {
1657                 /* this seems error. But we handle this like normal to avoid the unexpected error in cliend side */
1658                 VER("sequence(%u) should be over the last sequence(%u)", sequence, private_vblank->last_seq);
1659                 func(vblank, TDM_ERROR_NONE, private_vblank->last_seq,
1660                          TDM_TIME_SEC(private_vblank->last_time), TDM_TIME_USEC(private_vblank->last_time),
1661                          user_data);
1662                 return TDM_ERROR_NONE;
1663         }
1664
1665         return tdm_vblank_wait(vblank, req_sec, req_usec, interval, func, user_data);
1666 }
1667
1668 /* LCOV_EXCL_START */
1669 INTERN tdm_error
1670 tdm_vblank_set_add_front(tdm_vblank *vblank, unsigned int add_front)
1671 {
1672         tdm_private_vblank *private_vblank = vblank;
1673
1674         TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1675
1676         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
1677
1678         if (private_vblank->add_front == add_front)
1679                 return TDM_ERROR_NONE;
1680
1681         private_vblank->add_front = add_front;
1682
1683         return TDM_ERROR_NONE;
1684 }
1685 /* LCOV_EXCL_STOP */
1686
1687 INTERN tdm_error
1688 tdm_vblank_set_resource(tdm_vblank *vblank, struct wl_resource *resource)
1689 {
1690         tdm_private_vblank *private_vblank = vblank;
1691
1692         TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1693         TDM_RETURN_VAL_IF_FAIL(private_vblank->resource == NULL, TDM_ERROR_OPERATION_FAILED);
1694
1695         private_vblank->resource = resource;
1696
1697         return TDM_ERROR_NONE;
1698 }
1699
1700 INTERN double
1701 tdm_vblank_get_stamp(tdm_vblank *vblank)
1702 {
1703         tdm_private_vblank *private_vblank = vblank;
1704
1705         TDM_RETURN_VAL_IF_FAIL(tdm_vblank_is_valid(vblank), TDM_ERROR_INVALID_PARAMETER);
1706
1707         return private_vblank->stamp;
1708 }
1709
1710 static unsigned int
1711 _tdm_vblank_get_waiting_count(tdm_private_vblank *private_vblank)
1712 {
1713         tdm_vblank_wait_info *wait_info = NULL;
1714         unsigned int count = 0;
1715
1716         LIST_FOR_EACH_ENTRY(wait_info, &private_vblank->HW_wait_list, link) {
1717                 count++;
1718         }
1719
1720         LIST_FOR_EACH_ENTRY(wait_info, &private_vblank->SW_wait_list, link) {
1721                 count++;
1722         }
1723
1724         return count;
1725 }
1726
1727 static void
1728 _tdm_vblank_get_client_information(tdm_private_vblank *private_vblank, pid_t *pid, const char **name)
1729 {
1730         struct wl_client *client;
1731
1732         if (!private_vblank->resource)
1733                 goto no_client;
1734
1735         client = wl_resource_get_client(private_vblank->resource);
1736         if (!client)
1737                 goto no_client;
1738
1739         wl_client_get_credentials(client, pid, NULL, NULL);
1740         *name = tdm_server_get_client_name(*pid);
1741
1742         return;
1743
1744 no_client:
1745         *pid = 0;
1746         *name = NULL;
1747         return;
1748 }
1749
1750 /* LCOV_EXCL_START */
1751 INTERN void
1752 tdm_vblank_get_vblank_list_information(tdm_display *dpy, char *reply, int *len)
1753 {
1754         tdm_private_vblank *v = NULL;
1755         tdm_output_dpms dpms = TDM_OUTPUT_DPMS_OFF;
1756         tdm_output *output = tdm_display_get_output(dpy, 0, NULL);
1757
1758         if (output)
1759                 tdm_output_get_dpms(output, &dpms);
1760
1761         TDM_SNPRINTF(reply, len, "[Client Vblank List]\n");
1762         TDM_SNPRINTF(reply, len, "* DPMS: %s\n", tdm_dpms_str(dpms));
1763         TDM_SNPRINTF(reply, len, "* global fps: %u\n", vblank_global_fps);
1764         TDM_SNPRINTF(reply, len, "----------------------------------------------------------------------------------\n");
1765         TDM_SNPRINTF(reply, len, "Name                 Type Wait Interval Sequence Fps Ignore Offset Fake Process\n");
1766         TDM_SNPRINTF(reply, len, "----------------------------------------------------------------------------------\n");
1767
1768         pthread_mutex_lock(&valid_list_lock);
1769         LIST_FOR_EACH_ENTRY(v, &valid_vblank_list, valid_link) {
1770                 const char *proc_name = NULL;
1771                 pid_t pid = 0;
1772                 unsigned int count;
1773
1774                 _tdm_vblank_get_client_information(v, &pid, &proc_name);
1775
1776                 count = _tdm_vblank_get_waiting_count(v);
1777
1778                 TDM_SNPRINTF(reply, len, "%-8s(%p) ", v->name, v);
1779                 TDM_SNPRINTF(reply, len, "%-4s ", (v->last_type == VBLANK_EVENT_TYPE_HW) ? "HW" : "SW");
1780                 TDM_SNPRINTF(reply, len, "%-4s ", (count > 0) ? "O" : " ");
1781                 TDM_SNPRINTF(reply, len, "%-8d ", v->last_interval);
1782                 TDM_SNPRINTF(reply, len, "%-8u ", v->last_seq);
1783                 TDM_SNPRINTF(reply, len, "%-3u ", v->fps);
1784                 TDM_SNPRINTF(reply, len, "%-6u ", v->ignore_global_fps);
1785                 TDM_SNPRINTF(reply, len, "%-6d ", v->offset);
1786                 TDM_SNPRINTF(reply, len, "%-4u ", v->enable_fake);
1787                 TDM_SNPRINTF(reply, len, "%s(pid:%d)\n", (proc_name) ? proc_name : "Unknown", pid);
1788         }
1789         pthread_mutex_unlock(&valid_list_lock);
1790
1791         TDM_SNPRINTF(reply, len, "\n");
1792 }
1793 /* LCOV_EXCL_STOP */
1794