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;
79 unsigned int watch_output_changes;
81 struct list_head link;
82 } tdm_private_client_output;
84 struct _tdm_private_client_vblank {
85 tdm_private_client_output *private_output;
87 struct wl_tdm_vblank *vblank;
88 struct list_head wait_list;
90 char name[TDM_NAME_LEN];
94 unsigned int enable_fake;
100 struct list_head link;
103 typedef struct _tdm_client_output_handler_info {
104 tdm_private_client_output *private_output;
106 tdm_client_output_change_handler func;
109 struct list_head link;
110 } tdm_client_output_handler_info;
112 typedef struct _tdm_client_wait_info {
113 tdm_private_client_vblank *private_vblank;
115 tdm_client_vblank_handler func;
122 struct list_head link;
123 } tdm_client_wait_info;
126 _tdm_client_vblank_cb_done(void *data, struct wl_tdm_vblank *wl_tdm_vblank,
127 uint32_t req_id, uint32_t sequence, uint32_t tv_sec,
128 uint32_t tv_usec, uint32_t error)
130 tdm_private_client_vblank *private_vblank = data;
131 tdm_client_wait_info *w = NULL, *ww = NULL;
133 TDM_RETURN_IF_FAIL(private_vblank != NULL);
135 private_vblank->last_time = TDM_TIME(tv_sec, tv_usec);
137 TDM_DBG("vblank(%p) req_id(%u) sequence(%u) time(%.6f)",
138 private_vblank, req_id, sequence, TDM_TIME(tv_sec, tv_usec));
140 TDM_TRACE_COUNT(ClientDoneVBlank, req_id);
142 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->wait_list, link) {
143 if (w->req_id != req_id)
146 if (w->req_time >= private_vblank->last_time)
147 TDM_WRN("'req(%.6f) < last(%.6f)' failed", w->req_time, private_vblank->last_time);
150 w->func(private_vblank, error, sequence, tv_sec, tv_usec, w->user_data);
161 static const struct wl_tdm_vblank_listener tdm_client_vblank_listener = {
162 _tdm_client_vblank_cb_done,
166 _tdm_client_output_destroy(tdm_private_client_output *private_output)
168 tdm_private_client_vblank *v = NULL, *vv = NULL;
169 tdm_client_output_handler_info *h = NULL, *hh = NULL;
171 LIST_DEL(&private_output->link);
173 LIST_FOR_EACH_ENTRY_SAFE(v, vv, &private_output->vblank_list, link) {
174 TDM_ERR("vblanks SHOULD be destroyed first!");
176 v->private_output = NULL;
179 LIST_FOR_EACH_ENTRY_SAFE(h, hh, &private_output->change_handler_list, link) {
184 wl_tdm_output_destroy(private_output->output);
186 free(private_output);
190 _tdm_client_output_cb_mode(void *data, struct wl_tdm_output *wl_tdm_output,
191 uint32_t width, uint32_t height, uint32_t refresh)
193 tdm_private_client_output *private_output = (tdm_private_client_output*)data;
195 TDM_RETURN_IF_FAIL(private_output != NULL);
197 private_output->width = width;
198 private_output->height = height;
199 private_output->refresh = refresh;
201 TDM_DBG("private_output(%p) wl_tbm_output@%d width(%d) height(%d) refresh(%d)",
202 private_output, wl_proxy_get_id((struct wl_proxy*)private_output->output),
203 width, height, refresh);
207 _tdm_client_output_cb_connection(void *data, struct wl_tdm_output *wl_tdm_output, uint32_t value)
209 tdm_private_client_output *private_output = (tdm_private_client_output*)data;
210 tdm_client_output_handler_info *h = NULL;
213 TDM_RETURN_IF_FAIL(private_output != NULL);
215 if (private_output->connection == value)
218 private_output->connection = value;
220 TDM_DBG("private_output(%p) wl_tbm_output@%d connection(%d)",
222 wl_proxy_get_id((struct wl_proxy*)private_output->output),
226 LIST_FOR_EACH_ENTRY(h, &private_output->change_handler_list, link) {
228 h->func(private_output, TDM_OUTPUT_CHANGE_CONNECTION, v, h->user_data);
233 _tdm_client_output_cb_dpms(void *data, struct wl_tdm_output *wl_tdm_output, uint32_t value)
235 tdm_private_client_output *private_output = (tdm_private_client_output*)data;
236 tdm_client_output_handler_info *h = NULL;
239 TDM_RETURN_IF_FAIL(private_output != NULL);
241 if (private_output->dpms == value)
244 private_output->dpms = value;
246 TDM_DBG("private_output(%p) wl_tbm_output@%d dpms(%d)",
248 wl_proxy_get_id((struct wl_proxy*)private_output->output),
252 LIST_FOR_EACH_ENTRY(h, &private_output->change_handler_list, link) {
254 h->func(private_output, TDM_OUTPUT_CHANGE_DPMS, v, h->user_data);
258 static const struct wl_tdm_output_listener tdm_client_output_listener = {
259 _tdm_client_output_cb_mode,
260 _tdm_client_output_cb_connection,
261 _tdm_client_output_cb_dpms,
265 _tdm_client_cb_global(void *data, struct wl_registry *registry,
266 uint32_t name, const char *interface,
269 tdm_private_client *private_client = data;
271 if (strncmp(interface, "wl_tdm", 6) == 0) {
272 private_client->tdm =
273 wl_registry_bind(registry, name, &wl_tdm_interface, version);
274 TDM_RETURN_IF_FAIL(private_client->tdm != NULL);
276 wl_display_flush(private_client->display);
281 _tdm_client_cb_global_remove(void *data, struct wl_registry *registry, uint32_t name)
285 static const struct wl_registry_listener tdm_client_registry_listener = {
286 _tdm_client_cb_global,
287 _tdm_client_cb_global_remove
291 tdm_client_create(tdm_error *error)
293 tdm_private_client *private_client;
295 private_client = calloc(1, sizeof *private_client);
296 if (!private_client) {
297 TDM_ERR("alloc failed");
299 *error = TDM_ERROR_OUT_OF_MEMORY;
303 LIST_INITHEAD(&private_client->output_list);
305 private_client->display = wl_display_connect("tdm-socket");
306 TDM_GOTO_IF_FAIL(private_client->display != NULL, create_failed);
308 private_client->registry = wl_display_get_registry(private_client->display);
309 TDM_GOTO_IF_FAIL(private_client->registry != NULL, create_failed);
311 wl_registry_add_listener(private_client->registry,
312 &tdm_client_registry_listener, private_client);
313 wl_display_roundtrip(private_client->display);
315 /* check global objects */
316 TDM_GOTO_IF_FAIL(private_client->tdm != NULL, create_failed);
319 *error = TDM_ERROR_NONE;
321 return (tdm_client*)private_client;
323 tdm_client_destroy((tdm_client*)private_client);
325 *error = TDM_ERROR_OPERATION_FAILED;
330 tdm_client_destroy(tdm_client *client)
332 tdm_private_client *private_client = (tdm_private_client*)client;
333 tdm_private_client_output *o = NULL, *oo = NULL;
338 if (private_client->temp_vblank)
339 tdm_client_vblank_destroy(private_client->temp_vblank);
341 LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_client->output_list, link) {
342 _tdm_client_output_destroy(o);
345 if (private_client->tdm)
346 wl_tdm_destroy(private_client->tdm);
347 if (private_client->registry)
348 wl_registry_destroy(private_client->registry);
349 if (private_client->display)
350 wl_display_disconnect(private_client->display);
352 free(private_client);
356 tdm_client_get_fd(tdm_client *client, int *fd)
358 tdm_private_client *private_client;
360 TDM_RETURN_VAL_IF_FAIL(client != NULL, TDM_ERROR_INVALID_PARAMETER);
361 TDM_RETURN_VAL_IF_FAIL(fd != NULL, TDM_ERROR_INVALID_PARAMETER);
363 private_client = (tdm_private_client*)client;
365 *fd = wl_display_get_fd(private_client->display);
367 return TDM_ERROR_OPERATION_FAILED;
369 return TDM_ERROR_NONE;
373 tdm_client_handle_events(tdm_client *client)
375 tdm_private_client *private_client;
377 TDM_RETURN_VAL_IF_FAIL(client != NULL, TDM_ERROR_INVALID_PARAMETER);
379 private_client = (tdm_private_client*)client;
381 wl_display_dispatch(private_client->display);
383 return TDM_ERROR_NONE;
386 typedef struct _tdm_client_vblank_temp {
387 tdm_client_vblank_handler2 func;
389 } tdm_client_vblank_temp;
392 _tdm_client_vblank_handler_temp(tdm_client_vblank *vblank, tdm_error error, unsigned int sequence,
393 unsigned int tv_sec, unsigned int tv_usec, void *user_data)
395 tdm_client_vblank_temp *vblank_temp = user_data;
397 TDM_RETURN_IF_FAIL(vblank_temp != NULL);
399 if (vblank_temp->func)
400 vblank_temp->func(sequence, tv_sec, tv_usec, vblank_temp->user_data);
406 tdm_client_wait_vblank(tdm_client *client, char *name,
407 int sw_timer, int interval, int sync,
408 tdm_client_vblank_handler2 func, void *user_data)
410 tdm_private_client *private_client = (tdm_private_client*)client;
411 tdm_client_output *output;
412 tdm_client_vblank_temp *vblank_temp;
415 TDM_RETURN_VAL_IF_FAIL(private_client != NULL, TDM_ERROR_INVALID_PARAMETER);
416 TDM_RETURN_VAL_IF_FAIL(interval > 0, TDM_ERROR_INVALID_PARAMETER);
417 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
419 if (!private_client->temp_vblank) {
420 output = tdm_client_get_output(client, name, &ret);
421 TDM_RETURN_VAL_IF_FAIL(output != NULL, ret);
423 private_client->temp_vblank = tdm_client_output_create_vblank(output, &ret);
424 TDM_RETURN_VAL_IF_FAIL(private_client->temp_vblank != NULL, ret);
427 tdm_client_vblank_set_enable_fake(private_client->temp_vblank, sw_timer);
428 tdm_client_vblank_set_sync(private_client->temp_vblank, sync);
430 vblank_temp = calloc(1, sizeof *vblank_temp);
431 TDM_RETURN_VAL_IF_FAIL(vblank_temp != NULL, TDM_ERROR_OUT_OF_MEMORY);
433 vblank_temp->func = func;
434 vblank_temp->user_data = user_data;
436 return tdm_client_vblank_wait(private_client->temp_vblank, interval, _tdm_client_vblank_handler_temp, vblank_temp);
440 tdm_client_get_output(tdm_client *client, char *name, tdm_error *error)
442 tdm_private_client *private_client;
443 tdm_private_client_output *private_output = NULL;
446 *error = TDM_ERROR_NONE;
449 TDM_ERR("'!client' failed");
451 *error = TDM_ERROR_INVALID_PARAMETER;
455 private_client = (tdm_private_client*)client;
460 LIST_FOR_EACH_ENTRY(private_output, &private_client->output_list, link) {
461 if (!strncmp(private_output->name, name, TDM_NAME_LEN))
462 return (tdm_client_output*)private_output;
465 private_output = calloc(1, sizeof *private_output);
466 if (!private_output) {
467 TDM_ERR("alloc failed");
469 *error = TDM_ERROR_OUT_OF_MEMORY;
473 private_output->private_client = private_client;
475 snprintf(private_output->name, TDM_NAME_LEN, "%s", name);
476 private_output->output = wl_tdm_create_output(private_client->tdm, private_output->name);
477 if (!private_output->output) {
478 TDM_ERR("couldn't create output resource");
479 free(private_output);
481 *error = TDM_ERROR_OUT_OF_MEMORY;
485 LIST_INITHEAD(&private_output->vblank_list);
486 LIST_INITHEAD(&private_output->change_handler_list);
487 LIST_ADDTAIL(&private_output->link, &private_client->output_list);
489 wl_tdm_output_add_listener(private_output->output,
490 &tdm_client_output_listener, private_output);
491 wl_display_roundtrip(private_client->display);
493 return (tdm_client_output*)private_output;
497 tdm_client_output_add_change_handler(tdm_client_output *output,
498 tdm_client_output_change_handler func,
501 tdm_private_client_output *private_output;
502 tdm_client_output_handler_info *h;
504 TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
505 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
507 private_output = (tdm_private_client_output*)output;
509 h = calloc(1, sizeof *h);
510 TDM_RETURN_VAL_IF_FAIL(h != NULL, TDM_ERROR_OUT_OF_MEMORY);
512 if (LIST_IS_EMPTY(&private_output->change_handler_list)) {
513 wl_tdm_output_watch_output_changes(private_output->output, 1);
515 /* TODO: this is very tricky.
516 * If a client adds the change_handler, we might be able to guess that
517 * the client will watch the tdm client's fd and handle tdm events in
518 * event loop. Otherwise, we CAN'T make sure if a client has event loop
519 * which handles tdm events.
521 private_output->watch_output_changes = 1;
524 h->private_output = private_output;
526 h->user_data = user_data;
527 LIST_ADDTAIL(&h->link, &private_output->change_handler_list);
529 return TDM_ERROR_NOT_IMPLEMENTED;
533 tdm_client_output_remove_change_handler(tdm_client_output *output,
534 tdm_client_output_change_handler func,
537 tdm_private_client_output *private_output;
538 tdm_client_output_handler_info *h = NULL;
540 TDM_RETURN_IF_FAIL(output != NULL);
541 TDM_RETURN_IF_FAIL(func != NULL);
543 private_output = (tdm_private_client_output*)output;
545 LIST_FOR_EACH_ENTRY(h, &private_output->change_handler_list, link) {
546 if (h->func != func || h->user_data != user_data)
552 if (LIST_IS_EMPTY(&private_output->change_handler_list))
553 wl_tdm_output_watch_output_changes(private_output->output, 0);
560 tdm_client_output_get_refresh_rate(tdm_client_output *output, unsigned int *refresh)
562 tdm_private_client_output *private_output;
563 tdm_private_client *private_client;
565 TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
566 TDM_RETURN_VAL_IF_FAIL(refresh != NULL, TDM_ERROR_INVALID_PARAMETER);
568 private_output = (tdm_private_client_output*)output;
570 if (private_output->watch_output_changes) {
571 *refresh = private_output->refresh;
572 return TDM_ERROR_NONE;
575 private_client = private_output->private_client;
576 TDM_RETURN_VAL_IF_FAIL(private_client != NULL, TDM_ERROR_INVALID_PARAMETER);
578 wl_tdm_output_get_mode(private_output->output);
579 wl_display_roundtrip(private_client->display);
581 *refresh = private_output->refresh;
583 return TDM_ERROR_NONE;
587 tdm_client_output_get_conn_status(tdm_client_output *output, tdm_output_conn_status *status)
589 tdm_private_client_output *private_output;
590 tdm_private_client *private_client;
592 TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
593 TDM_RETURN_VAL_IF_FAIL(status != NULL, TDM_ERROR_INVALID_PARAMETER);
595 private_output = (tdm_private_client_output*)output;
597 if (private_output->watch_output_changes) {
598 *status = private_output->connection;
599 return TDM_ERROR_NONE;
602 private_client = private_output->private_client;
603 TDM_RETURN_VAL_IF_FAIL(private_client != NULL, TDM_ERROR_INVALID_PARAMETER);
605 wl_tdm_output_get_connection(private_output->output);
606 wl_display_roundtrip(private_client->display);
608 *status = private_output->connection;
610 return TDM_ERROR_NONE;
614 tdm_client_output_get_dpms(tdm_client_output *output, tdm_output_dpms *dpms)
616 tdm_private_client_output *private_output;
617 tdm_private_client *private_client;
619 TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
620 TDM_RETURN_VAL_IF_FAIL(dpms != NULL, TDM_ERROR_INVALID_PARAMETER);
622 private_output = (tdm_private_client_output*)output;
624 if (private_output->watch_output_changes) {
625 *dpms = private_output->dpms;
626 return TDM_ERROR_NONE;
629 private_client = private_output->private_client;
630 TDM_RETURN_VAL_IF_FAIL(private_client != NULL, TDM_ERROR_INVALID_PARAMETER);
632 wl_tdm_output_get_dpms(private_output->output);
633 wl_display_roundtrip(private_client->display);
635 *dpms = private_output->dpms;
637 return TDM_ERROR_NONE;
641 tdm_client_output_create_vblank(tdm_client_output *output, tdm_error *error)
643 tdm_private_client_output *private_output;
644 tdm_private_client_vblank *private_vblank;
647 *error = TDM_ERROR_NONE;
650 TDM_ERR("'!output' failed");
652 *error = TDM_ERROR_INVALID_PARAMETER;
656 private_output = (tdm_private_client_output*)output;
658 private_vblank = calloc(1, sizeof *private_vblank);
659 if (!private_vblank) {
660 TDM_ERR("alloc failed");
662 *error = TDM_ERROR_OUT_OF_MEMORY;
666 private_vblank->private_output = private_output;
668 private_vblank->vblank = wl_tdm_output_create_vblank(private_output->output);
669 if (!private_vblank->vblank) {
670 TDM_ERR("couldn't create vblank resource");
671 free(private_vblank);
673 *error = TDM_ERROR_OUT_OF_MEMORY;
678 private_vblank->fps = private_output->refresh;
679 private_vblank->offset = 0;
680 private_vblank->enable_fake = 0;
682 LIST_INITHEAD(&private_vblank->wait_list);
683 LIST_ADDTAIL(&private_vblank->link, &private_output->vblank_list);
685 wl_tdm_vblank_add_listener(private_vblank->vblank,
686 &tdm_client_vblank_listener, private_vblank);
688 return (tdm_client_vblank*)private_vblank;
692 tdm_client_vblank_destroy(tdm_client_vblank *vblank)
694 tdm_private_client_vblank *private_vblank;
695 tdm_client_wait_info *w = NULL, *ww = NULL;
697 TDM_RETURN_IF_FAIL(vblank != NULL);
699 private_vblank = vblank;
700 LIST_DEL(&private_vblank->link);
702 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->wait_list, link) {
707 wl_tdm_vblank_destroy(private_vblank->vblank);
709 free(private_vblank);
713 tdm_client_vblank_set_name(tdm_client_vblank *vblank, const char *name)
715 tdm_private_client_vblank *private_vblank;
717 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
719 private_vblank = vblank;
722 name = TDM_VBLANK_DEFAULT_NAME;
724 strncpy(private_vblank->name, name, TDM_NAME_LEN - 1);
725 private_vblank->name[TDM_NAME_LEN - 1] = '\0';
727 wl_tdm_vblank_set_name(private_vblank->vblank, private_vblank->name);
729 return TDM_ERROR_NONE;
733 tdm_client_vblank_set_sync(tdm_client_vblank *vblank, unsigned int sync)
735 tdm_private_client_vblank *private_vblank;
737 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
739 private_vblank = vblank;
740 private_vblank->sync = sync;
742 return TDM_ERROR_NONE;
746 tdm_client_vblank_set_fps(tdm_client_vblank *vblank, unsigned int fps)
748 tdm_private_client_vblank *private_vblank;
750 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
751 TDM_RETURN_VAL_IF_FAIL(fps > 0, TDM_ERROR_INVALID_PARAMETER);
753 private_vblank = vblank;
755 if (private_vblank->fps == fps)
756 return TDM_ERROR_NONE;
757 private_vblank->fps = fps;
759 wl_tdm_vblank_set_fps(private_vblank->vblank, fps);
761 return TDM_ERROR_NONE;
765 tdm_client_vblank_set_offset(tdm_client_vblank *vblank, int offset_ms)
767 tdm_private_client_vblank *private_vblank;
769 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
771 private_vblank = vblank;
772 TDM_RETURN_VAL_IF_FAIL(private_vblank->started == 0, TDM_ERROR_BAD_REQUEST);
774 if (private_vblank->offset == offset_ms)
775 return TDM_ERROR_NONE;
776 private_vblank->offset = offset_ms;
778 wl_tdm_vblank_set_offset(private_vblank->vblank, offset_ms);
780 return TDM_ERROR_NONE;
784 tdm_client_vblank_set_enable_fake(tdm_client_vblank *vblank, unsigned int enable_fake)
786 tdm_private_client_vblank *private_vblank;
788 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
790 private_vblank = vblank;
792 if (private_vblank->enable_fake == enable_fake)
793 return TDM_ERROR_NONE;
794 private_vblank->enable_fake = enable_fake;
796 wl_tdm_vblank_set_enable_fake(private_vblank->vblank, enable_fake);
798 return TDM_ERROR_NONE;
802 tdm_client_vblank_wait(tdm_client_vblank *vblank, unsigned int interval, tdm_client_vblank_handler func, void *user_data)
804 tdm_private_client *private_client;
805 tdm_private_client_output *private_output;
806 tdm_private_client_vblank *private_vblank;
807 tdm_client_wait_info *w;
809 unsigned int req_sec, req_usec;
812 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
813 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
814 /* can't support "interval 0" and "getting current_msc" things because
815 * there is a socket communication between TDM client and server. It's impossible
816 * to return the current msc or sequence immediately.
818 TDM_RETURN_VAL_IF_FAIL(interval > 0, TDM_ERROR_INVALID_PARAMETER);
820 private_vblank = vblank;
821 private_output = private_vblank->private_output;
822 private_client = private_output->private_client;
824 if (!private_vblank->started)
825 private_vblank->started = 1;
827 if (private_output->watch_output_changes)
828 if (private_output->dpms != TDM_OUTPUT_DPMS_ON && !private_vblank->enable_fake) {
829 TDM_INFO("dpms off");
830 return TDM_ERROR_DPMS_OFF;
833 w = calloc(1, sizeof *w);
835 TDM_ERR("alloc failed");
836 return TDM_ERROR_OUT_OF_MEMORY;
839 w->private_vblank = private_vblank;
841 w->user_data = user_data;
843 LIST_ADDTAIL(&w->link, &private_vblank->wait_list);
845 clock_gettime(CLOCK_MONOTONIC, &tp);
846 req_sec = (unsigned int)tp.tv_sec;
847 req_usec = (unsigned int)(tp.tv_nsec / 1000);
849 w->req_id = ++private_output->req_id;
850 w->req_time = TDM_TIME(req_sec, req_usec);
851 w->need_free = (private_vblank->sync) ? 0 : 1;
853 wl_tdm_vblank_wait_vblank(private_vblank->vblank, interval, w->req_id, req_sec, req_usec);
855 TDM_TRACE_COUNT(ClientWaitVBlank, w->req_id);
857 TDM_DBG("vblank(%p) interval(%u) req_id(%d) req(%.6f)",
858 vblank, interval, w->req_id, TDM_TIME(req_sec, req_usec));
860 if (private_vblank->last_time >= w->req_time)
861 TDM_ERR("'last(%.6f) < req(%.6f)' failed", private_vblank->last_time, w->req_time);
863 if (!private_vblank->sync) {
864 wl_display_flush(private_client->display);
865 return TDM_ERROR_NONE;
868 while (ret != -1 && !w->need_free)
869 ret = wl_display_dispatch(private_client->display);
871 clock_gettime(CLOCK_MONOTONIC, &tp);
872 TDM_DBG("block during %d us",
873 ((unsigned int)(tp.tv_sec * 1000000) + (unsigned int)(tp.tv_nsec / 1000))
874 - (req_sec * 1000000 + req_usec));
879 return TDM_ERROR_NONE;
883 tdm_client_vblank_wait_seq(tdm_client_vblank *vblank, unsigned int sequence,
884 tdm_client_vblank_handler func, void *user_data)
886 tdm_private_client *private_client;
887 tdm_private_client_output *private_output;
888 tdm_private_client_vblank *private_vblank;
889 tdm_client_wait_info *w;
891 unsigned int req_sec, req_usec;
894 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
895 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
897 private_vblank = vblank;
898 private_output = private_vblank->private_output;
899 private_client = private_output->private_client;
901 if (!private_vblank->started)
902 private_vblank->started = 1;
904 if (private_output->watch_output_changes)
905 if (private_output->dpms != TDM_OUTPUT_DPMS_ON && !private_vblank->enable_fake) {
906 TDM_INFO("dpms off");
907 return TDM_ERROR_DPMS_OFF;
910 w = calloc(1, sizeof *w);
912 TDM_ERR("alloc failed");
913 return TDM_ERROR_OUT_OF_MEMORY;
916 w->private_vblank = private_vblank;
918 w->user_data = user_data;
920 LIST_ADDTAIL(&w->link, &private_vblank->wait_list);
922 clock_gettime(CLOCK_MONOTONIC, &tp);
923 req_sec = (unsigned int)tp.tv_sec;
924 req_usec = (unsigned int)(tp.tv_nsec / 1000);
926 w->req_id = ++private_output->req_id;
927 w->req_time = TDM_TIME(req_sec, req_usec);
928 w->need_free = (private_vblank->sync) ? 0 : 1;
930 wl_tdm_vblank_wait_vblank_seq(private_vblank->vblank, sequence, w->req_id, req_sec, req_usec);
932 TDM_TRACE_COUNT(ClientWaitVBlank, w->req_id);
934 TDM_DBG("vblank(%p) sequence(%u) req_id(%d) req(%.6f)",
935 vblank, sequence, w->req_id, TDM_TIME(req_sec, req_usec));
937 if (private_vblank->last_time >= w->req_time)
938 TDM_ERR("'last(%.6f) < req(%.6f)' failed", private_vblank->last_time, w->req_time);
940 if (!private_vblank->sync) {
941 wl_display_flush(private_client->display);
942 return TDM_ERROR_NONE;
945 while (ret != -1 && !w->need_free)
946 ret = wl_display_dispatch(private_client->display);
948 clock_gettime(CLOCK_MONOTONIC, &tp);
949 TDM_DBG("block during %d us",
950 ((unsigned int)(tp.tv_sec * 1000000) + (unsigned int)(tp.tv_nsec / 1000))
951 - (req_sec * 1000000 + req_usec));
956 return TDM_ERROR_NONE;