1 /**************************************************************************
5 * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
7 * Contact: Eunchul Kim <chulspro.kim@samsung.com>,
8 * JinYoung Jeon <jy0.jeon@samsung.com>,
9 * Taeheon Kim <th908.kim@samsung.com>,
10 * YoungJun Cho <yj44.cho@samsung.com>,
11 * SooChan Lim <sc1.lim@samsung.com>,
12 * Boram Park <sc1.lim@samsung.com>
14 * Permission is hereby granted, free of charge, to any person obtaining a
15 * copy of this software and associated documentation files (the
16 * "Software"), to deal in the Software without restriction, including
17 * without limitation the rights to use, copy, modify, merge, publish,
18 * distribute, sub license, and/or sell copies of the Software, and to
19 * permit persons to whom the Software is furnished to do so, subject to
20 * the following conditions:
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
27 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
29 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
30 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
31 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
32 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 **************************************************************************/
46 #include "tdm_client.h"
48 #include "tdm_macro.h"
51 #include "tdm_private.h"
52 #include "tdm-client-protocol.h"
54 typedef struct _tdm_private_client_vblank tdm_private_client_vblank;
56 typedef struct _tdm_private_client {
57 struct wl_display *display;
58 struct wl_registry *registry;
60 struct list_head output_list;
62 tdm_private_client_vblank *temp_vblank;
65 typedef struct _tdm_private_client_output {
66 tdm_private_client *private_client;
68 char name[TDM_NAME_LEN];
69 struct wl_tdm_output *output;
73 tdm_output_conn_status connection;
75 struct list_head vblank_list;
76 struct list_head change_handler_list;
80 struct list_head link;
81 } tdm_private_client_output;
83 struct _tdm_private_client_vblank {
84 tdm_private_client_output *private_output;
86 struct wl_tdm_vblank *vblank;
87 struct list_head wait_list;
89 char name[TDM_NAME_LEN];
93 unsigned int enable_fake;
99 struct list_head link;
102 typedef struct _tdm_client_output_handler_info {
103 tdm_private_client_output *private_output;
105 tdm_client_output_change_handler func;
108 struct list_head link;
109 } tdm_client_output_handler_info;
111 typedef struct _tdm_client_wait_info {
112 tdm_private_client_vblank *private_vblank;
114 tdm_client_vblank_handler func;
121 struct list_head link;
122 } tdm_client_wait_info;
125 _tdm_client_vblank_cb_done(void *data, struct wl_tdm_vblank *wl_tdm_vblank,
126 uint32_t req_id, uint32_t sequence, uint32_t tv_sec,
127 uint32_t tv_usec, uint32_t error)
129 tdm_private_client_vblank *private_vblank = data;
130 tdm_client_wait_info *w = NULL, *ww = NULL;
132 TDM_RETURN_IF_FAIL(private_vblank != NULL);
134 private_vblank->last_time = TDM_TIME(tv_sec, tv_usec);
136 TDM_DBG("vblank(%p) req_id(%u) sequence(%u) time(%.6f)",
137 private_vblank, req_id, sequence, TDM_TIME(tv_sec, tv_usec));
139 TDM_TRACE_COUNT(ClientDoneVBlank, req_id);
141 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->wait_list, link) {
142 if (w->req_id != req_id)
145 if (w->req_time >= private_vblank->last_time)
146 TDM_ERR("'req(%.6f) < last(%.6f)' failed", w->req_time, private_vblank->last_time);
149 w->func(private_vblank, error, sequence, tv_sec, tv_usec, w->user_data);
160 static const struct wl_tdm_vblank_listener tdm_client_vblank_listener = {
161 _tdm_client_vblank_cb_done,
165 _tdm_client_output_destroy(tdm_private_client_output *private_output)
167 tdm_private_client_vblank *v = NULL, *vv = NULL;
168 tdm_client_output_handler_info *h = NULL, *hh = NULL;
170 LIST_DEL(&private_output->link);
172 LIST_FOR_EACH_ENTRY_SAFE(v, vv, &private_output->vblank_list, link) {
173 TDM_ERR("vblanks SHOULD be destroyed first!");
175 v->private_output = NULL;
178 LIST_FOR_EACH_ENTRY_SAFE(h, hh, &private_output->change_handler_list, link) {
183 wl_tdm_output_destroy(private_output->output);
185 free(private_output);
189 _tdm_client_output_cb_mode(void *data, struct wl_tdm_output *wl_tdm_output,
190 uint32_t width, uint32_t height, uint32_t refresh)
192 tdm_private_client_output *private_output = (tdm_private_client_output*)data;
194 TDM_RETURN_IF_FAIL(private_output != NULL);
196 private_output->width = width;
197 private_output->height = height;
198 private_output->refresh = refresh;
200 TDM_DBG("private_output(%p) wl_tbm_output@%d width(%d) height(%d) refresh(%d)",
201 private_output, wl_proxy_get_id((struct wl_proxy*)private_output->output),
202 width, height, refresh);
206 _tdm_client_output_cb_connection(void *data, struct wl_tdm_output *wl_tdm_output, uint32_t value)
208 tdm_private_client_output *private_output = (tdm_private_client_output*)data;
209 tdm_client_output_handler_info *h = NULL;
212 TDM_RETURN_IF_FAIL(private_output != NULL);
214 if (private_output->connection == value)
217 private_output->connection = value;
219 TDM_DBG("private_output(%p) wl_tbm_output@%d connection(%d)",
221 wl_proxy_get_id((struct wl_proxy*)private_output->output),
225 LIST_FOR_EACH_ENTRY(h, &private_output->change_handler_list, link) {
227 h->func(private_output, TDM_OUTPUT_CHANGE_CONNECTION, v, h->user_data);
232 _tdm_client_output_cb_dpms(void *data, struct wl_tdm_output *wl_tdm_output, uint32_t value)
234 tdm_private_client_output *private_output = (tdm_private_client_output*)data;
235 tdm_client_output_handler_info *h = NULL;
238 TDM_RETURN_IF_FAIL(private_output != NULL);
240 if (private_output->dpms == value)
243 private_output->dpms = value;
245 TDM_DBG("private_output(%p) wl_tbm_output@%d dpms(%d)",
247 wl_proxy_get_id((struct wl_proxy*)private_output->output),
251 LIST_FOR_EACH_ENTRY(h, &private_output->change_handler_list, link) {
253 h->func(private_output, TDM_OUTPUT_CHANGE_DPMS, v, h->user_data);
257 static const struct wl_tdm_output_listener tdm_client_output_listener = {
258 _tdm_client_output_cb_mode,
259 _tdm_client_output_cb_connection,
260 _tdm_client_output_cb_dpms,
264 _tdm_client_cb_global(void *data, struct wl_registry *registry,
265 uint32_t name, const char *interface,
268 tdm_private_client *private_client = data;
270 if (strncmp(interface, "wl_tdm", 6) == 0) {
271 private_client->tdm =
272 wl_registry_bind(registry, name, &wl_tdm_interface, version);
273 TDM_RETURN_IF_FAIL(private_client->tdm != NULL);
275 wl_display_flush(private_client->display);
280 _tdm_client_cb_global_remove(void *data, struct wl_registry *registry, uint32_t name)
284 static const struct wl_registry_listener tdm_client_registry_listener = {
285 _tdm_client_cb_global,
286 _tdm_client_cb_global_remove
290 tdm_client_create(tdm_error *error)
292 tdm_private_client *private_client;
294 private_client = calloc(1, sizeof *private_client);
295 if (!private_client) {
296 TDM_ERR("alloc failed");
298 *error = TDM_ERROR_OUT_OF_MEMORY;
302 LIST_INITHEAD(&private_client->output_list);
304 private_client->display = wl_display_connect("tdm-socket");
305 TDM_GOTO_IF_FAIL(private_client->display != NULL, create_failed);
307 private_client->registry = wl_display_get_registry(private_client->display);
308 TDM_GOTO_IF_FAIL(private_client->registry != NULL, create_failed);
310 wl_registry_add_listener(private_client->registry,
311 &tdm_client_registry_listener, private_client);
312 wl_display_roundtrip(private_client->display);
314 /* check global objects */
315 TDM_GOTO_IF_FAIL(private_client->tdm != NULL, create_failed);
318 *error = TDM_ERROR_NONE;
320 return (tdm_client*)private_client;
322 tdm_client_destroy((tdm_client*)private_client);
324 *error = TDM_ERROR_OPERATION_FAILED;
329 tdm_client_destroy(tdm_client *client)
331 tdm_private_client *private_client = (tdm_private_client*)client;
332 tdm_private_client_output *o = NULL, *oo = NULL;
337 if (private_client->temp_vblank)
338 tdm_client_vblank_destroy(private_client->temp_vblank);
340 LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_client->output_list, link) {
341 _tdm_client_output_destroy(o);
344 if (private_client->tdm)
345 wl_tdm_destroy(private_client->tdm);
346 if (private_client->registry)
347 wl_registry_destroy(private_client->registry);
348 if (private_client->display)
349 wl_display_disconnect(private_client->display);
351 free(private_client);
355 tdm_client_get_fd(tdm_client *client, int *fd)
357 tdm_private_client *private_client;
359 TDM_RETURN_VAL_IF_FAIL(client != NULL, TDM_ERROR_INVALID_PARAMETER);
360 TDM_RETURN_VAL_IF_FAIL(fd != NULL, TDM_ERROR_INVALID_PARAMETER);
362 private_client = (tdm_private_client*)client;
364 *fd = wl_display_get_fd(private_client->display);
366 return TDM_ERROR_OPERATION_FAILED;
368 return TDM_ERROR_NONE;
372 tdm_client_handle_events(tdm_client *client)
374 tdm_private_client *private_client;
376 TDM_RETURN_VAL_IF_FAIL(client != NULL, TDM_ERROR_INVALID_PARAMETER);
378 private_client = (tdm_private_client*)client;
380 wl_display_dispatch(private_client->display);
382 return TDM_ERROR_NONE;
385 typedef struct _tdm_client_vblank_temp {
386 tdm_client_vblank_handler2 func;
388 } tdm_client_vblank_temp;
391 _tdm_client_vblank_handler_temp(tdm_client_vblank *vblank, tdm_error error, unsigned int sequence,
392 unsigned int tv_sec, unsigned int tv_usec, void *user_data)
394 tdm_client_vblank_temp *vblank_temp = user_data;
396 TDM_RETURN_IF_FAIL(vblank_temp != NULL);
398 if (vblank_temp->func)
399 vblank_temp->func(sequence, tv_sec, tv_usec, vblank_temp->user_data);
405 tdm_client_wait_vblank(tdm_client *client, char *name,
406 int sw_timer, int interval, int sync,
407 tdm_client_vblank_handler2 func, void *user_data)
409 tdm_private_client *private_client = (tdm_private_client*)client;
410 tdm_client_output *output;
411 tdm_client_vblank_temp *vblank_temp;
414 TDM_RETURN_VAL_IF_FAIL(private_client != NULL, TDM_ERROR_INVALID_PARAMETER);
415 TDM_RETURN_VAL_IF_FAIL(interval > 0, TDM_ERROR_INVALID_PARAMETER);
416 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
418 if (!private_client->temp_vblank) {
419 output = tdm_client_get_output(client, name, &ret);
420 TDM_RETURN_VAL_IF_FAIL(output != NULL, ret);
422 private_client->temp_vblank = tdm_client_output_create_vblank(output, &ret);
423 TDM_RETURN_VAL_IF_FAIL(private_client->temp_vblank != NULL, ret);
426 tdm_client_vblank_set_enable_fake(private_client->temp_vblank, sw_timer);
427 tdm_client_vblank_set_sync(private_client->temp_vblank, sync);
429 vblank_temp = calloc(1, sizeof *vblank_temp);
430 TDM_RETURN_VAL_IF_FAIL(vblank_temp != NULL, TDM_ERROR_OUT_OF_MEMORY);
432 vblank_temp->func = func;
433 vblank_temp->user_data = user_data;
435 return tdm_client_vblank_wait(private_client->temp_vblank, interval, _tdm_client_vblank_handler_temp, vblank_temp);
439 tdm_client_get_output(tdm_client *client, char *name, tdm_error *error)
441 tdm_private_client *private_client;
442 tdm_private_client_output *private_output = NULL;
445 *error = TDM_ERROR_NONE;
448 TDM_ERR("'!client' failed");
450 *error = TDM_ERROR_INVALID_PARAMETER;
454 private_client = (tdm_private_client*)client;
459 LIST_FOR_EACH_ENTRY(private_output, &private_client->output_list, link) {
460 if (!strncmp(private_output->name, name, TDM_NAME_LEN))
461 return (tdm_client_output*)private_output;
464 private_output = calloc(1, sizeof *private_output);
465 if (!private_output) {
466 TDM_ERR("alloc failed");
468 *error = TDM_ERROR_OUT_OF_MEMORY;
472 private_output->private_client = private_client;
474 snprintf(private_output->name, TDM_NAME_LEN, "%s", name);
475 private_output->output = wl_tdm_create_output(private_client->tdm, private_output->name);
476 if (!private_output->output) {
477 TDM_ERR("couldn't create output resource");
478 free(private_output);
480 *error = TDM_ERROR_OUT_OF_MEMORY;
484 LIST_INITHEAD(&private_output->vblank_list);
485 LIST_INITHEAD(&private_output->change_handler_list);
486 LIST_ADDTAIL(&private_output->link, &private_client->output_list);
488 wl_tdm_output_add_listener(private_output->output,
489 &tdm_client_output_listener, private_output);
490 wl_display_roundtrip(private_client->display);
492 return (tdm_client_output*)private_output;
496 tdm_client_output_add_change_handler(tdm_client_output *output,
497 tdm_client_output_change_handler func,
500 tdm_private_client_output *private_output;
501 tdm_client_output_handler_info *h;
503 TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
504 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
506 private_output = (tdm_private_client_output*)output;
508 h = calloc(1, sizeof *h);
509 TDM_RETURN_VAL_IF_FAIL(h != NULL, TDM_ERROR_OUT_OF_MEMORY);
511 if (LIST_IS_EMPTY(&private_output->change_handler_list))
512 wl_tdm_output_watch_output_changes(private_output->output, 1);
514 h->private_output = private_output;
516 h->user_data = user_data;
517 LIST_ADDTAIL(&h->link, &private_output->change_handler_list);
519 return TDM_ERROR_NOT_IMPLEMENTED;
523 tdm_client_output_remove_change_handler(tdm_client_output *output,
524 tdm_client_output_change_handler func,
527 tdm_private_client_output *private_output;
528 tdm_client_output_handler_info *h = NULL;
530 TDM_RETURN_IF_FAIL(output != NULL);
531 TDM_RETURN_IF_FAIL(func != NULL);
533 private_output = (tdm_private_client_output*)output;
535 LIST_FOR_EACH_ENTRY(h, &private_output->change_handler_list, link) {
536 if (h->func != func || h->user_data != user_data)
542 if (LIST_IS_EMPTY(&private_output->change_handler_list))
543 wl_tdm_output_watch_output_changes(private_output->output, 0);
550 tdm_client_output_get_refresh_rate(tdm_client_output *output, unsigned int *refresh)
552 tdm_private_client_output *private_output;
554 TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
555 TDM_RETURN_VAL_IF_FAIL(refresh != NULL, TDM_ERROR_INVALID_PARAMETER);
557 private_output = (tdm_private_client_output*)output;
559 *refresh = private_output->refresh;
561 return TDM_ERROR_NONE;
565 tdm_client_output_get_conn_status(tdm_client_output *output, tdm_output_conn_status *status)
567 tdm_private_client_output *private_output;
569 TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
570 TDM_RETURN_VAL_IF_FAIL(status != NULL, TDM_ERROR_INVALID_PARAMETER);
572 private_output = (tdm_private_client_output*)output;
574 *status = private_output->connection;
576 return TDM_ERROR_NONE;
580 tdm_client_output_get_dpms(tdm_client_output *output, tdm_output_dpms *dpms)
582 tdm_private_client_output *private_output;
584 TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
585 TDM_RETURN_VAL_IF_FAIL(dpms != NULL, TDM_ERROR_INVALID_PARAMETER);
587 private_output = (tdm_private_client_output*)output;
589 *dpms = private_output->dpms;
591 return TDM_ERROR_NONE;
595 tdm_client_output_create_vblank(tdm_client_output *output, tdm_error *error)
597 tdm_private_client_output *private_output;
598 tdm_private_client_vblank *private_vblank;
601 *error = TDM_ERROR_NONE;
604 TDM_ERR("'!output' failed");
606 *error = TDM_ERROR_INVALID_PARAMETER;
610 private_output = (tdm_private_client_output*)output;
612 private_vblank = calloc(1, sizeof *private_vblank);
613 if (!private_vblank) {
614 TDM_ERR("alloc failed");
616 *error = TDM_ERROR_OUT_OF_MEMORY;
620 private_vblank->private_output = private_output;
622 private_vblank->vblank = wl_tdm_output_create_vblank(private_output->output);
623 if (!private_vblank->vblank) {
624 TDM_ERR("couldn't create vblank resource");
625 free(private_vblank);
627 *error = TDM_ERROR_OUT_OF_MEMORY;
632 private_vblank->fps = private_output->refresh;
633 private_vblank->offset = 0;
634 private_vblank->enable_fake = 0;
636 LIST_INITHEAD(&private_vblank->wait_list);
637 LIST_ADDTAIL(&private_vblank->link, &private_output->vblank_list);
639 wl_tdm_vblank_add_listener(private_vblank->vblank,
640 &tdm_client_vblank_listener, private_vblank);
642 return (tdm_client_vblank*)private_vblank;
646 tdm_client_vblank_destroy(tdm_client_vblank *vblank)
648 tdm_private_client_vblank *private_vblank;
649 tdm_client_wait_info *w = NULL, *ww = NULL;
651 TDM_RETURN_IF_FAIL(vblank != NULL);
653 private_vblank = vblank;
654 LIST_DEL(&private_vblank->link);
656 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->wait_list, link) {
661 wl_tdm_vblank_destroy(private_vblank->vblank);
663 free(private_vblank);
667 tdm_client_vblank_set_name(tdm_client_vblank *vblank, const char *name)
669 tdm_private_client_vblank *private_vblank;
671 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
673 private_vblank = vblank;
676 name = TDM_VBLANK_DEFAULT_NAME;
678 strncpy(private_vblank->name, name, TDM_NAME_LEN - 1);
679 private_vblank->name[TDM_NAME_LEN - 1] = '\0';
681 wl_tdm_vblank_set_name(private_vblank->vblank, private_vblank->name);
683 return TDM_ERROR_NONE;
687 tdm_client_vblank_set_sync(tdm_client_vblank *vblank, unsigned int sync)
689 tdm_private_client_vblank *private_vblank;
691 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
693 private_vblank = vblank;
694 private_vblank->sync = sync;
696 return TDM_ERROR_NONE;
700 tdm_client_vblank_set_fps(tdm_client_vblank *vblank, unsigned int fps)
702 tdm_private_client_vblank *private_vblank;
704 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
705 TDM_RETURN_VAL_IF_FAIL(fps > 0, TDM_ERROR_INVALID_PARAMETER);
707 private_vblank = vblank;
709 if (private_vblank->fps == fps)
710 return TDM_ERROR_NONE;
711 private_vblank->fps = fps;
713 wl_tdm_vblank_set_fps(private_vblank->vblank, fps);
715 return TDM_ERROR_NONE;
719 tdm_client_vblank_set_offset(tdm_client_vblank *vblank, int offset_ms)
721 tdm_private_client_vblank *private_vblank;
723 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
725 private_vblank = vblank;
726 TDM_RETURN_VAL_IF_FAIL(private_vblank->started == 0, TDM_ERROR_BAD_REQUEST);
728 if (private_vblank->offset == offset_ms)
729 return TDM_ERROR_NONE;
730 private_vblank->offset = offset_ms;
732 wl_tdm_vblank_set_offset(private_vblank->vblank, offset_ms);
734 return TDM_ERROR_NONE;
738 tdm_client_vblank_set_enable_fake(tdm_client_vblank *vblank, unsigned int enable_fake)
740 tdm_private_client_vblank *private_vblank;
742 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
744 private_vblank = vblank;
746 if (private_vblank->enable_fake == enable_fake)
747 return TDM_ERROR_NONE;
748 private_vblank->enable_fake = enable_fake;
750 wl_tdm_vblank_set_enable_fake(private_vblank->vblank, enable_fake);
752 return TDM_ERROR_NONE;
756 tdm_client_vblank_wait(tdm_client_vblank *vblank, unsigned int interval, tdm_client_vblank_handler func, void *user_data)
758 tdm_private_client *private_client;
759 tdm_private_client_output *private_output;
760 tdm_private_client_vblank *private_vblank;
761 tdm_client_wait_info *w;
763 unsigned int req_sec, req_usec;
766 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
767 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
768 /* can't support "interval 0" and "getting current_msc" things because
769 * there is a socket communication between TDM client and server. It's impossible
770 * to return the current msc or sequence immediately.
772 TDM_RETURN_VAL_IF_FAIL(interval > 0, TDM_ERROR_INVALID_PARAMETER);
774 private_vblank = vblank;
775 private_output = private_vblank->private_output;
776 private_client = private_output->private_client;
778 if (!private_vblank->started)
779 private_vblank->started = 1;
781 if (private_output->dpms != TDM_OUTPUT_DPMS_ON && !private_vblank->enable_fake) {
782 TDM_INFO("dpms off");
783 return TDM_ERROR_DPMS_OFF;
786 w = calloc(1, sizeof *w);
788 TDM_ERR("alloc failed");
789 return TDM_ERROR_OUT_OF_MEMORY;
792 w->private_vblank = private_vblank;
794 w->user_data = user_data;
796 LIST_ADDTAIL(&w->link, &private_vblank->wait_list);
798 clock_gettime(CLOCK_MONOTONIC, &tp);
799 req_sec = (unsigned int)tp.tv_sec;
800 req_usec = (unsigned int)(tp.tv_nsec / 1000);
802 w->req_id = ++private_output->req_id;
803 w->req_time = TDM_TIME(req_sec, req_usec);
804 w->need_free = (private_vblank->sync) ? 0 : 1;
806 wl_tdm_vblank_wait_vblank(private_vblank->vblank, interval, w->req_id, req_sec, req_usec);
808 TDM_TRACE_COUNT(ClientWaitVBlank, w->req_id);
810 TDM_DBG("vblank(%p) interval(%u) req_id(%d) req(%.6f)",
811 vblank, interval, w->req_id, TDM_TIME(req_sec, req_usec));
813 if (private_vblank->last_time >= w->req_time)
814 TDM_ERR("'last(%.6f) < req(%.6f)' failed", private_vblank->last_time, w->req_time);
816 if (!private_vblank->sync) {
817 wl_display_flush(private_client->display);
818 return TDM_ERROR_NONE;
821 while (ret != -1 && !w->need_free)
822 ret = wl_display_dispatch(private_client->display);
824 clock_gettime(CLOCK_MONOTONIC, &tp);
825 TDM_DBG("block during %d us",
826 ((unsigned int)(tp.tv_sec * 1000000) + (unsigned int)(tp.tv_nsec / 1000))
827 - (req_sec * 1000000 + req_usec));
832 return TDM_ERROR_NONE;
836 tdm_client_vblank_wait_seq(tdm_client_vblank *vblank, unsigned int sequence,
837 tdm_client_vblank_handler func, void *user_data)
839 tdm_private_client *private_client;
840 tdm_private_client_output *private_output;
841 tdm_private_client_vblank *private_vblank;
842 tdm_client_wait_info *w;
844 unsigned int req_sec, req_usec;
847 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
848 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
850 private_vblank = vblank;
851 private_output = private_vblank->private_output;
852 private_client = private_output->private_client;
854 if (!private_vblank->started)
855 private_vblank->started = 1;
857 if (private_output->dpms != TDM_OUTPUT_DPMS_ON && !private_vblank->enable_fake) {
858 TDM_INFO("dpms off");
859 return TDM_ERROR_DPMS_OFF;
862 w = calloc(1, sizeof *w);
864 TDM_ERR("alloc failed");
865 return TDM_ERROR_OUT_OF_MEMORY;
868 w->private_vblank = private_vblank;
870 w->user_data = user_data;
872 LIST_ADDTAIL(&w->link, &private_vblank->wait_list);
874 clock_gettime(CLOCK_MONOTONIC, &tp);
875 req_sec = (unsigned int)tp.tv_sec;
876 req_usec = (unsigned int)(tp.tv_nsec / 1000);
878 w->req_id = ++private_output->req_id;
879 w->req_time = TDM_TIME(req_sec, req_usec);
880 w->need_free = (private_vblank->sync) ? 0 : 1;
882 wl_tdm_vblank_wait_vblank_seq(private_vblank->vblank, sequence, w->req_id, req_sec, req_usec);
884 TDM_TRACE_COUNT(ClientWaitVBlank, w->req_id);
886 TDM_DBG("vblank(%p) sequence(%u) req_id(%d) req(%.6f)",
887 vblank, sequence, w->req_id, TDM_TIME(req_sec, req_usec));
889 if (private_vblank->last_time >= w->req_time)
890 TDM_ERR("'last(%.6f) < req(%.6f)' failed", private_vblank->last_time, w->req_time);
892 if (!private_vblank->sync) {
893 wl_display_flush(private_client->display);
894 return TDM_ERROR_NONE;
897 while (ret != -1 && !w->need_free)
898 ret = wl_display_dispatch(private_client->display);
900 clock_gettime(CLOCK_MONOTONIC, &tp);
901 TDM_DBG("block during %d us",
902 ((unsigned int)(tp.tv_sec * 1000000) + (unsigned int)(tp.tv_nsec / 1000))
903 - (req_sec * 1000000 + req_usec));
908 return TDM_ERROR_NONE;