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-protocol.h>
48 #include "tdm_client.h"
50 #include "tdm_macro.h"
53 #include "tdm_private.h"
55 typedef struct _tdm_private_client_vblank tdm_private_client_vblank;
57 typedef struct _tdm_private_client {
60 struct wl_display *display;
61 struct wl_event_queue *queue;
62 struct wl_registry *registry;
64 struct list_head output_list;
66 unsigned int enable_ttrace;
69 tdm_private_client_vblank *temp_vblank;
72 typedef struct _tdm_private_client_output {
73 tdm_private_client *private_client;
75 char name[TDM_NAME_LEN];
76 struct wl_tdm_output *output;
80 tdm_output_conn_status connection;
82 struct list_head vblank_list;
83 struct list_head change_handler_list;
86 unsigned int watch_output_changes;
88 struct list_head link;
89 } tdm_private_client_output;
91 struct _tdm_private_client_vblank {
92 tdm_private_client_output *private_output;
94 struct wl_tdm_vblank *vblank;
95 struct list_head wait_list;
97 char name[TDM_NAME_LEN];
101 unsigned int enable_fake;
102 unsigned int enable_ttrace;
104 unsigned int started;
110 struct list_head link;
113 typedef struct _tdm_client_output_handler_info {
114 tdm_private_client_output *private_output;
116 tdm_client_output_change_handler func;
119 struct list_head link;
120 struct list_head call_link;
121 } tdm_client_output_handler_info;
123 typedef struct _tdm_client_wait_info {
124 tdm_private_client_vblank *private_vblank;
126 tdm_client_vblank_handler func;
133 struct list_head link;
134 struct list_head call_link;
135 } tdm_client_wait_info;
138 _tdm_client_vblank_cb_stamp(void *data, struct wl_tdm_vblank *wl_tdm_vblank, uint32_t stamp)
140 tdm_private_client_vblank *private_vblank = data;
141 tdm_private_client *private_client;
143 TDM_RETURN_IF_FAIL(private_vblank != NULL);
145 private_vblank->stamp = stamp;
147 TDM_RETURN_IF_FAIL(private_vblank->private_output != NULL);
148 private_client = private_vblank->private_output->private_client;
150 private_client->stamp = stamp;
153 /* LCOV_EXCL_START */
155 _tdm_client_vblank_cb_done(void *data, struct wl_tdm_vblank *wl_tdm_vblank,
156 uint32_t req_id, uint32_t sequence, uint32_t tv_sec,
157 uint32_t tv_usec, uint32_t error)
159 tdm_private_client_vblank *private_vblank = data;
160 tdm_private_client *private_client;
161 tdm_client_wait_info *w = NULL, *wait_info = NULL;
163 TDM_RETURN_IF_FAIL(private_vblank != NULL);
165 private_client = private_vblank->private_output->private_client;
167 private_vblank->last_time = TDM_TIME(tv_sec, tv_usec);
169 TDM_DBG("vblank(%p) req_id(%u) sequence(%u) time(%.6f)",
170 private_vblank, req_id, sequence, TDM_TIME(tv_sec, tv_usec));
172 LIST_FOR_EACH_ENTRY(w, &private_vblank->wait_list, link) {
173 if (w->req_id != req_id)
181 TDM_ERR("no wait infomation for req_id(%d)", req_id);
185 if (private_vblank->enable_ttrace)
186 TDM_TRACE_ASYNC_END((int)wait_info->req_time, "TDM_Client_Vblank:%u", private_vblank->stamp);
188 if (wait_info->req_time >= private_vblank->last_time)
189 TDM_WRN("'req(%.6f) < last(%.6f)' failed. error(%d)", wait_info->req_time, private_vblank->last_time, error);
191 if (wait_info->need_free)
192 LIST_DEL(&wait_info->link);
194 if (wait_info->func) {
195 pthread_mutex_unlock(&private_client->lock);
196 wait_info->func(private_vblank, error, sequence, tv_sec, tv_usec, wait_info->user_data);
197 pthread_mutex_lock(&private_client->lock);
200 if (wait_info->need_free)
203 wait_info->need_free = 1;
207 /* LCOV_EXCL_START */
209 _tdm_client_vblank_cb_ttrace(void *data, struct wl_tdm_vblank *wl_tdm_vblank, uint32_t enable)
211 tdm_private_client_vblank *private_vblank = data;
212 tdm_private_client *private_client;
214 TDM_RETURN_IF_FAIL(private_vblank != NULL);
216 private_vblank->enable_ttrace = enable;
218 TDM_RETURN_IF_FAIL(private_vblank->private_output != NULL);
219 private_client = private_vblank->private_output->private_client;
221 private_client->enable_ttrace = enable;
225 static const struct wl_tdm_vblank_listener tdm_client_vblank_listener = {
226 _tdm_client_vblank_cb_stamp,
227 _tdm_client_vblank_cb_done,
228 _tdm_client_vblank_cb_ttrace,
232 _tdm_client_output_destroy(tdm_private_client_output *private_output)
234 tdm_private_client_vblank *v = NULL, *vv = NULL;
235 tdm_client_output_handler_info *h = NULL, *hh = NULL;
237 LIST_DEL(&private_output->link);
239 LIST_FOR_EACH_ENTRY_SAFE(v, vv, &private_output->vblank_list, link) {
240 TDM_ERR("vblanks SHOULD be destroyed first!");
242 v->private_output = NULL;
245 LIST_FOR_EACH_ENTRY_SAFE(h, hh, &private_output->change_handler_list, link) {
250 wl_tdm_output_destroy(private_output->output);
252 free(private_output);
256 _tdm_client_output_cb_mode(void *data, struct wl_tdm_output *wl_tdm_output,
257 uint32_t width, uint32_t height, uint32_t refresh, uint32_t error)
259 tdm_private_client_output *private_output = (tdm_private_client_output*)data;
261 TDM_RETURN_IF_FAIL(private_output != NULL);
263 private_output->width = width;
264 private_output->height = height;
265 private_output->refresh = refresh;
267 if (error != TDM_ERROR_NONE)
268 TDM_INFO("mode event error: %d", error);
270 TDM_DBG("private_output(%p) wl_tbm_output@%d width(%d) height(%d) refresh(%d)",
271 private_output, wl_proxy_get_id((struct wl_proxy*)private_output->output),
272 width, height, refresh);
276 _tdm_client_output_cb_connection(void *data, struct wl_tdm_output *wl_tdm_output, uint32_t value, uint32_t error)
278 tdm_private_client_output *private_output = (tdm_private_client_output*)data;
279 tdm_private_client *private_client;
280 tdm_client_output_handler_info *h = NULL, *hh = NULL;
282 struct list_head call_list;
284 TDM_RETURN_IF_FAIL(private_output != NULL);
286 private_client = private_output->private_client;
288 if (private_output->connection == value)
291 private_output->connection = value;
293 if (error != TDM_ERROR_NONE)
294 TDM_INFO("connection event error: %d", error);
296 TDM_DBG("private_output(%p) wl_tbm_output@%d connection(%d)",
298 wl_proxy_get_id((struct wl_proxy*)private_output->output),
301 LIST_INITHEAD(&call_list);
303 LIST_FOR_EACH_ENTRY(h, &private_output->change_handler_list, link) {
304 LIST_ADDTAIL(&h->call_link, &call_list);
308 pthread_mutex_unlock(&private_client->lock);
309 LIST_FOR_EACH_ENTRY_SAFE(h, hh, &call_list, call_link) {
311 h->func(private_output, TDM_OUTPUT_CHANGE_CONNECTION, v, h->user_data);
313 pthread_mutex_lock(&private_client->lock);
317 _tdm_client_output_cb_dpms(void *data, struct wl_tdm_output *wl_tdm_output, uint32_t value, uint32_t error)
319 tdm_private_client_output *private_output = (tdm_private_client_output*)data;
320 tdm_private_client *private_client;
321 tdm_client_output_handler_info *h = NULL, *hh = NULL;
323 struct list_head call_list;
325 TDM_RETURN_IF_FAIL(private_output != NULL);
327 private_client = private_output->private_client;
329 /* If value is extended value, we handle it as DPMS on in client side
330 * The extended DPMS value is valid only in server side.
331 * Or, need to export to client side also?
333 if (value > TDM_OUTPUT_DPMS_OFF)
334 value = TDM_OUTPUT_DPMS_ON;
336 if (private_output->dpms == value)
339 private_output->dpms = value;
341 if (error != TDM_ERROR_NONE)
342 TDM_INFO("dpms event error: %d", error);
344 TDM_DBG("private_output(%p) wl_tbm_output@%d dpms(%d)",
346 wl_proxy_get_id((struct wl_proxy*)private_output->output),
349 LIST_INITHEAD(&call_list);
351 LIST_FOR_EACH_ENTRY(h, &private_output->change_handler_list, link) {
352 LIST_ADDTAIL(&h->call_link, &call_list);
356 pthread_mutex_unlock(&private_client->lock);
357 LIST_FOR_EACH_ENTRY_SAFE(h, hh, &call_list, call_link) {
359 h->func(private_output, TDM_OUTPUT_CHANGE_DPMS, v, h->user_data);
361 pthread_mutex_lock(&private_client->lock);
364 static const struct wl_tdm_output_listener tdm_client_output_listener = {
365 _tdm_client_output_cb_mode,
366 _tdm_client_output_cb_connection,
367 _tdm_client_output_cb_dpms,
371 _tdm_client_cb_global(void *data, struct wl_registry *registry,
372 uint32_t name, const char *interface,
375 tdm_private_client *private_client = data;
377 if (strncmp(interface, "wl_tdm", 6) == 0) {
378 private_client->tdm =
379 wl_registry_bind(registry, name, &wl_tdm_interface, version);
380 TDM_RETURN_IF_FAIL(private_client->tdm != NULL);
382 wl_display_flush(private_client->display);
386 /* LCOV_EXCL_START */
388 _tdm_client_cb_global_remove(void *data, struct wl_registry *registry, uint32_t name)
393 static const struct wl_registry_listener tdm_client_registry_listener = {
394 _tdm_client_cb_global,
395 _tdm_client_cb_global_remove
399 tdm_client_create(tdm_error *error)
401 tdm_private_client *private_client;
403 private_client = calloc(1, sizeof *private_client);
404 if (!private_client) {
405 /* LCOV_EXCL_START */
407 TDM_ERR("alloc failed");
409 *error = TDM_ERROR_OUT_OF_MEMORY;
415 if (pthread_mutex_init(&private_client->lock, NULL)) {
416 TDM_ERR("mutex init failed: %m");
417 free(private_client);
419 *error = TDM_ERROR_OUT_OF_MEMORY;
423 LIST_INITHEAD(&private_client->output_list);
425 private_client->display = wl_display_connect("tdm-socket");
426 TDM_GOTO_IF_FAIL(private_client->display != NULL, create_failed);
428 private_client->queue = wl_display_create_queue(private_client->display);
429 TDM_GOTO_IF_FAIL(private_client->queue != NULL, create_failed);
431 private_client->registry = wl_display_get_registry(private_client->display);
432 TDM_GOTO_IF_FAIL(private_client->registry != NULL, create_failed);
434 wl_registry_add_listener(private_client->registry,
435 &tdm_client_registry_listener, private_client);
436 wl_display_roundtrip(private_client->display);
438 /* check global objects */
439 TDM_GOTO_IF_FAIL(private_client->tdm != NULL, create_failed);
442 *error = TDM_ERROR_NONE;
444 return (tdm_client*)private_client;
446 tdm_client_destroy((tdm_client*)private_client);
448 *error = TDM_ERROR_OPERATION_FAILED;
453 tdm_client_destroy(tdm_client *client)
455 tdm_private_client *private_client = (tdm_private_client*)client;
456 tdm_private_client_output *o = NULL, *oo = NULL;
461 pthread_mutex_lock(&private_client->lock);
463 if (private_client->temp_vblank)
464 tdm_client_vblank_destroy(private_client->temp_vblank);
466 LIST_FOR_EACH_ENTRY_SAFE(o, oo, &private_client->output_list, link) {
467 _tdm_client_output_destroy(o);
470 if (private_client->tdm)
471 wl_tdm_destroy(private_client->tdm);
472 if (private_client->registry)
473 wl_registry_destroy(private_client->registry);
474 if (private_client->queue)
475 wl_event_queue_destroy(private_client->queue);
476 if (private_client->display)
477 wl_display_disconnect(private_client->display);
479 pthread_mutex_unlock(&private_client->lock);
480 pthread_mutex_destroy(&private_client->lock);
482 free(private_client);
486 tdm_client_get_fd(tdm_client *client, int *fd)
488 tdm_private_client *private_client;
490 TDM_RETURN_VAL_IF_FAIL(client != NULL, TDM_ERROR_INVALID_PARAMETER);
491 TDM_RETURN_VAL_IF_FAIL(fd != NULL, TDM_ERROR_INVALID_PARAMETER);
493 private_client = (tdm_private_client*)client;
495 pthread_mutex_lock(&private_client->lock);
497 *fd = wl_display_get_fd(private_client->display);
499 pthread_mutex_unlock(&private_client->lock);
502 return TDM_ERROR_OPERATION_FAILED;
504 return TDM_ERROR_NONE;
508 tdm_client_handle_events(tdm_client *client)
510 tdm_private_client *private_client;
512 TDM_RETURN_VAL_IF_FAIL(client != NULL, TDM_ERROR_INVALID_PARAMETER);
514 /* LCOV_EXCL_START */
515 private_client = (tdm_private_client*)client;
517 pthread_mutex_lock(&private_client->lock);
519 if (private_client->enable_ttrace)
520 TDM_TRACE_ASYNC_BEGIN((int)private_client->stamp, "TDM_Client_Events:%u", (unsigned int)private_client->stamp);
522 wl_display_dispatch(private_client->display);
524 if (private_client->enable_ttrace)
525 TDM_TRACE_ASYNC_END((int)private_client->stamp, "TDM_Client_Events:%u", (unsigned int)private_client->stamp);
527 pthread_mutex_unlock(&private_client->lock);
529 return TDM_ERROR_NONE;
533 typedef struct _tdm_client_vblank_temp {
534 tdm_client_vblank_handler2 func;
536 } tdm_client_vblank_temp;
538 /* LCOV_EXCL_START */
540 _tdm_client_vblank_handler_temp(tdm_client_vblank *vblank, tdm_error error, unsigned int sequence,
541 unsigned int tv_sec, unsigned int tv_usec, void *user_data)
543 tdm_client_vblank_temp *vblank_temp = user_data;
544 tdm_private_client_vblank *private_vblank;
545 tdm_private_client *private_client;
547 TDM_RETURN_IF_FAIL(vblank_temp != NULL);
548 TDM_RETURN_IF_FAIL(vblank != NULL);
550 private_vblank = vblank;
551 private_client = private_vblank->private_output->private_client;
553 if (vblank_temp->func) {
554 pthread_mutex_unlock(&private_client->lock);
555 vblank_temp->func(sequence, tv_sec, tv_usec, vblank_temp->user_data);
556 pthread_mutex_lock(&private_client->lock);
563 /* LCOV_EXCL_START */ /* deprecated */
565 tdm_client_wait_vblank(tdm_client *client, char *name,
566 int sw_timer, int interval, int sync,
567 tdm_client_vblank_handler2 func, void *user_data)
569 tdm_private_client *private_client = (tdm_private_client*)client;
570 tdm_client_output *output;
571 tdm_client_vblank_temp *vblank_temp;
574 TDM_RETURN_VAL_IF_FAIL(private_client != NULL, TDM_ERROR_INVALID_PARAMETER);
575 TDM_RETURN_VAL_IF_FAIL(interval > 0, TDM_ERROR_INVALID_PARAMETER);
576 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
578 if (!private_client->temp_vblank) {
579 output = tdm_client_get_output(client, name, &ret);
580 TDM_RETURN_VAL_IF_FAIL(output != NULL, ret);
582 private_client->temp_vblank = tdm_client_output_create_vblank(output, &ret);
583 TDM_RETURN_VAL_IF_FAIL(private_client->temp_vblank != NULL, ret);
586 tdm_client_vblank_set_enable_fake(private_client->temp_vblank, sw_timer);
587 tdm_client_vblank_set_sync(private_client->temp_vblank, sync);
589 vblank_temp = calloc(1, sizeof *vblank_temp);
590 TDM_RETURN_VAL_IF_FAIL(vblank_temp != NULL, TDM_ERROR_OUT_OF_MEMORY);
592 vblank_temp->func = func;
593 vblank_temp->user_data = user_data;
595 return tdm_client_vblank_wait(private_client->temp_vblank, interval, _tdm_client_vblank_handler_temp, vblank_temp);
600 tdm_client_get_output(tdm_client *client, char *name, tdm_error *error)
602 tdm_private_client *private_client;
603 tdm_private_client_output *private_output = NULL;
604 struct wl_proxy *wrapper;
607 *error = TDM_ERROR_NONE;
610 TDM_ERR("'!client' failed");
612 *error = TDM_ERROR_INVALID_PARAMETER;
616 private_client = (tdm_private_client*)client;
618 pthread_mutex_lock(&private_client->lock);
623 LIST_FOR_EACH_ENTRY(private_output, &private_client->output_list, link) {
624 if (!strncmp(private_output->name, name, TDM_NAME_LEN)) {
625 pthread_mutex_unlock(&private_client->lock);
626 return (tdm_client_output*)private_output;
630 wrapper = wl_proxy_create_wrapper(private_client->tdm);
632 TDM_ERR("create output_wrapper failed");
634 *error = TDM_ERROR_OUT_OF_MEMORY;
635 pthread_mutex_unlock(&private_client->lock);
639 wl_proxy_set_queue(wrapper, private_client->queue);
641 private_output = calloc(1, sizeof *private_output);
642 if (!private_output) {
643 /* LCOV_EXCL_START */
644 wl_proxy_wrapper_destroy(wrapper);
645 TDM_ERR("alloc failed");
647 *error = TDM_ERROR_OUT_OF_MEMORY;
648 pthread_mutex_unlock(&private_client->lock);
654 private_output->private_client = private_client;
656 snprintf(private_output->name, TDM_NAME_LEN, "%s", name);
657 private_output->output = wl_tdm_create_output((struct wl_tdm *)wrapper, private_output->name);
658 wl_proxy_wrapper_destroy(wrapper);
659 if (!private_output->output) {
660 /* LCOV_EXCL_START */
662 TDM_ERR("couldn't create output resource");
663 free(private_output);
665 *error = TDM_ERROR_OUT_OF_MEMORY;
666 pthread_mutex_unlock(&private_client->lock);
672 LIST_INITHEAD(&private_output->vblank_list);
673 LIST_INITHEAD(&private_output->change_handler_list);
674 LIST_ADDTAIL(&private_output->link, &private_client->output_list);
676 wl_tdm_output_add_listener(private_output->output,
677 &tdm_client_output_listener, private_output);
678 wl_display_roundtrip_queue(private_client->display, private_client->queue);
680 wl_proxy_set_queue((struct wl_proxy *)private_output->output, NULL);
682 pthread_mutex_unlock(&private_client->lock);
684 return (tdm_client_output*)private_output;
688 tdm_client_output_add_change_handler(tdm_client_output *output,
689 tdm_client_output_change_handler func,
692 tdm_private_client_output *private_output;
693 tdm_private_client *private_client;
694 tdm_client_output_handler_info *h;
696 TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
697 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
699 private_output = (tdm_private_client_output*)output;
700 private_client = private_output->private_client;
702 h = calloc(1, sizeof *h);
703 TDM_RETURN_VAL_IF_FAIL(h != NULL, TDM_ERROR_OUT_OF_MEMORY);
705 pthread_mutex_lock(&private_client->lock);
707 if (LIST_IS_EMPTY(&private_output->change_handler_list)) {
708 wl_tdm_output_watch_output_changes(private_output->output, 1);
710 /* TODO: this is very tricky.
711 * If a client adds the change_handler, we might be able to guess that
712 * the client will watch the tdm client's fd and handle tdm events in
713 * event loop. Otherwise, we CAN'T make sure if a client has event loop
714 * which handles tdm events.
716 private_output->watch_output_changes = 1;
719 h->private_output = private_output;
721 h->user_data = user_data;
722 LIST_ADDTAIL(&h->link, &private_output->change_handler_list);
723 LIST_INITHEAD(&h->call_link);
725 pthread_mutex_unlock(&private_client->lock);
727 return TDM_ERROR_NONE;
731 tdm_client_output_remove_change_handler(tdm_client_output *output,
732 tdm_client_output_change_handler func,
735 tdm_private_client_output *private_output;
736 tdm_private_client *private_client;
737 tdm_client_output_handler_info *h = NULL;
739 TDM_RETURN_IF_FAIL(output != NULL);
740 TDM_RETURN_IF_FAIL(func != NULL);
742 private_output = (tdm_private_client_output*)output;
743 private_client = private_output->private_client;
745 pthread_mutex_lock(&private_client->lock);
747 LIST_FOR_EACH_ENTRY(h, &private_output->change_handler_list, link) {
748 if (h->func != func || h->user_data != user_data)
754 if (LIST_IS_EMPTY(&private_output->change_handler_list))
755 wl_tdm_output_watch_output_changes(private_output->output, 0);
757 pthread_mutex_unlock(&private_client->lock);
762 pthread_mutex_unlock(&private_client->lock);
766 tdm_client_output_get_refresh_rate(tdm_client_output *output, unsigned int *refresh)
768 tdm_private_client_output *private_output;
769 tdm_private_client *private_client;
771 TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
772 TDM_RETURN_VAL_IF_FAIL(refresh != NULL, TDM_ERROR_INVALID_PARAMETER);
774 private_output = (tdm_private_client_output*)output;
775 private_client = private_output->private_client;
777 pthread_mutex_lock(&private_client->lock);
779 if (private_output->watch_output_changes) {
780 *refresh = private_output->refresh;
781 pthread_mutex_unlock(&private_client->lock);
782 return TDM_ERROR_NONE;
785 wl_proxy_set_queue((struct wl_proxy *)private_output->output, private_client->queue);
786 wl_tdm_output_get_mode(private_output->output);
787 wl_display_roundtrip_queue(private_client->display, private_client->queue);
788 wl_proxy_set_queue((struct wl_proxy *)private_output->output, NULL);
790 *refresh = private_output->refresh;
792 pthread_mutex_unlock(&private_client->lock);
794 return TDM_ERROR_NONE;
798 tdm_client_output_get_conn_status(tdm_client_output *output, tdm_output_conn_status *status)
800 tdm_private_client_output *private_output;
801 tdm_private_client *private_client;
803 TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
804 TDM_RETURN_VAL_IF_FAIL(status != NULL, TDM_ERROR_INVALID_PARAMETER);
806 private_output = (tdm_private_client_output*)output;
807 private_client = private_output->private_client;
809 pthread_mutex_lock(&private_client->lock);
811 if (private_output->watch_output_changes) {
812 *status = private_output->connection;
813 pthread_mutex_unlock(&private_client->lock);
814 return TDM_ERROR_NONE;
817 wl_proxy_set_queue((struct wl_proxy *)private_output->output, private_client->queue);
818 wl_tdm_output_get_connection(private_output->output);
819 wl_display_roundtrip_queue(private_client->display, private_client->queue);
820 wl_proxy_set_queue((struct wl_proxy *)private_output->output, NULL);
822 *status = private_output->connection;
824 pthread_mutex_unlock(&private_client->lock);
826 return TDM_ERROR_NONE;
830 tdm_client_output_get_dpms(tdm_client_output *output, tdm_output_dpms *dpms)
832 tdm_private_client_output *private_output;
833 tdm_private_client *private_client;
835 TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
836 TDM_RETURN_VAL_IF_FAIL(dpms != NULL, TDM_ERROR_INVALID_PARAMETER);
838 private_output = (tdm_private_client_output*)output;
839 private_client = private_output->private_client;
841 if (private_output->watch_output_changes) {
842 *dpms = private_output->dpms;
843 return TDM_ERROR_NONE;
846 pthread_mutex_lock(&private_client->lock);
848 wl_proxy_set_queue((struct wl_proxy *)private_output->output, private_client->queue);
849 wl_tdm_output_get_dpms(private_output->output);
850 wl_display_roundtrip_queue(private_client->display, private_client->queue);
851 wl_proxy_set_queue((struct wl_proxy *)private_output->output, NULL);
853 *dpms = private_output->dpms;
855 pthread_mutex_unlock(&private_client->lock);
857 return TDM_ERROR_NONE;
861 tdm_client_output_create_vblank(tdm_client_output *output, tdm_error *error)
863 tdm_private_client *private_client;
864 tdm_private_client_output *private_output;
865 tdm_private_client_vblank *private_vblank;
866 struct wl_proxy *wrapper;
869 *error = TDM_ERROR_NONE;
872 TDM_ERR("'!output' failed");
874 *error = TDM_ERROR_INVALID_PARAMETER;
878 private_output = (tdm_private_client_output*)output;
879 private_client = private_output->private_client;
881 pthread_mutex_lock(&private_client->lock);
883 wrapper = wl_proxy_create_wrapper(private_output->output);
885 TDM_ERR("create output_wrapper failed");
887 *error = TDM_ERROR_OUT_OF_MEMORY;
888 pthread_mutex_unlock(&private_client->lock);
892 wl_proxy_set_queue(wrapper, private_client->queue);
894 private_vblank = calloc(1, sizeof *private_vblank);
895 if (!private_vblank) {
896 /* LCOV_EXCL_START */
898 TDM_ERR("alloc failed");
899 wl_proxy_wrapper_destroy(wrapper);
901 *error = TDM_ERROR_OUT_OF_MEMORY;
902 pthread_mutex_unlock(&private_client->lock);
908 private_vblank->private_output = private_output;
910 private_vblank->vblank = wl_tdm_output_create_vblank((struct wl_tdm_output*)wrapper);
911 wl_proxy_wrapper_destroy(wrapper);
912 if (!private_vblank->vblank) {
913 /* LCOV_EXCL_START */
915 TDM_ERR("couldn't create vblank resource");
916 free(private_vblank);
918 *error = TDM_ERROR_OUT_OF_MEMORY;
919 pthread_mutex_unlock(&private_client->lock);
926 private_vblank->fps = private_output->refresh;
927 private_vblank->offset = 0;
928 private_vblank->enable_fake = 0;
930 LIST_INITHEAD(&private_vblank->wait_list);
931 LIST_ADDTAIL(&private_vblank->link, &private_output->vblank_list);
933 wl_tdm_vblank_add_listener(private_vblank->vblank,
934 &tdm_client_vblank_listener, private_vblank);
935 wl_display_roundtrip_queue(private_client->display, private_client->queue);
937 wl_proxy_set_queue((struct wl_proxy *)private_vblank->vblank, NULL);
939 pthread_mutex_unlock(&private_client->lock);
941 return (tdm_client_vblank*)private_vblank;
945 tdm_client_vblank_destroy(tdm_client_vblank *vblank)
947 tdm_private_client_vblank *private_vblank;
948 tdm_private_client *private_client;
949 tdm_client_wait_info *w = NULL, *ww = NULL;
951 TDM_RETURN_IF_FAIL(vblank != NULL);
953 private_vblank = vblank;
954 private_client = private_vblank->private_output->private_client;
956 pthread_mutex_lock(&private_client->lock);
958 LIST_DEL(&private_vblank->link);
960 LIST_FOR_EACH_ENTRY_SAFE(w, ww, &private_vblank->wait_list, link) {
965 wl_tdm_vblank_destroy(private_vblank->vblank);
967 free(private_vblank);
969 pthread_mutex_unlock(&private_client->lock);
973 tdm_client_vblank_set_name(tdm_client_vblank *vblank, const char *name)
975 tdm_private_client_vblank *private_vblank;
976 tdm_private_client *private_client;
978 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
980 private_vblank = vblank;
981 private_client = private_vblank->private_output->private_client;
983 pthread_mutex_lock(&private_client->lock);
986 name = TDM_VBLANK_DEFAULT_NAME;
988 strncpy(private_vblank->name, name, TDM_NAME_LEN - 1);
989 private_vblank->name[TDM_NAME_LEN - 1] = '\0';
991 wl_tdm_vblank_set_name(private_vblank->vblank, private_vblank->name);
993 pthread_mutex_unlock(&private_client->lock);
995 return TDM_ERROR_NONE;
999 tdm_client_vblank_set_sync(tdm_client_vblank *vblank, unsigned int sync)
1001 tdm_private_client_vblank *private_vblank;
1002 tdm_private_client *private_client;
1004 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1006 private_vblank = vblank;
1007 private_client = private_vblank->private_output->private_client;
1009 pthread_mutex_lock(&private_client->lock);
1010 private_vblank->sync = sync;
1011 pthread_mutex_unlock(&private_client->lock);
1013 return TDM_ERROR_NONE;
1017 tdm_client_vblank_set_fps(tdm_client_vblank *vblank, unsigned int fps)
1019 tdm_private_client_vblank *private_vblank;
1020 tdm_private_client *private_client;
1022 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1023 TDM_RETURN_VAL_IF_FAIL(fps > 0, TDM_ERROR_INVALID_PARAMETER);
1025 private_vblank = vblank;
1026 private_client = private_vblank->private_output->private_client;
1028 pthread_mutex_lock(&private_client->lock);
1030 if (private_vblank->fps == fps) {
1031 pthread_mutex_unlock(&private_client->lock);
1032 return TDM_ERROR_NONE;
1035 private_vblank->fps = fps;
1037 wl_tdm_vblank_set_fps(private_vblank->vblank, fps);
1039 pthread_mutex_unlock(&private_client->lock);
1041 return TDM_ERROR_NONE;
1045 tdm_client_vblank_set_offset(tdm_client_vblank *vblank, int offset_ms)
1047 tdm_private_client_vblank *private_vblank;
1048 tdm_private_client *private_client;
1050 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1052 private_vblank = vblank;
1053 TDM_RETURN_VAL_IF_FAIL(private_vblank->started == 0, TDM_ERROR_BAD_REQUEST);
1055 private_client = private_vblank->private_output->private_client;
1057 pthread_mutex_lock(&private_client->lock);
1059 if (private_vblank->offset == offset_ms) {
1060 pthread_mutex_unlock(&private_client->lock);
1061 return TDM_ERROR_NONE;
1064 private_vblank->offset = offset_ms;
1066 wl_tdm_vblank_set_offset(private_vblank->vblank, offset_ms);
1068 pthread_mutex_unlock(&private_client->lock);
1070 return TDM_ERROR_NONE;
1074 tdm_client_vblank_set_enable_fake(tdm_client_vblank *vblank, unsigned int enable_fake)
1076 tdm_private_client_vblank *private_vblank;
1077 tdm_private_client *private_client;
1079 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1081 private_vblank = vblank;
1082 private_client = private_vblank->private_output->private_client;
1084 pthread_mutex_lock(&private_client->lock);
1086 if (private_vblank->enable_fake == enable_fake) {
1087 pthread_mutex_unlock(&private_client->lock);
1088 return TDM_ERROR_NONE;
1091 private_vblank->enable_fake = enable_fake;
1093 wl_tdm_vblank_set_enable_fake(private_vblank->vblank, enable_fake);
1095 pthread_mutex_unlock(&private_client->lock);
1097 return TDM_ERROR_NONE;
1101 tdm_client_vblank_wait(tdm_client_vblank *vblank, unsigned int interval, tdm_client_vblank_handler func, void *user_data)
1103 tdm_private_client *private_client;
1104 tdm_private_client_output *private_output;
1105 tdm_private_client_vblank *private_vblank;
1106 tdm_client_wait_info *w;
1108 unsigned int req_sec, req_usec;
1111 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1112 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
1113 /* can't support "interval 0" and "getting current_msc" things because
1114 * there is a socket communication between TDM client and server. It's impossible
1115 * to return the current msc or sequence immediately.
1117 TDM_RETURN_VAL_IF_FAIL(interval > 0, TDM_ERROR_INVALID_PARAMETER);
1119 private_vblank = vblank;
1120 private_output = private_vblank->private_output;
1121 private_client = private_output->private_client;
1123 pthread_mutex_lock(&private_client->lock);
1125 if (!private_vblank->started)
1126 private_vblank->started = 1;
1128 if (private_output->watch_output_changes && !private_vblank->enable_fake) {
1129 if (private_output->connection == TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
1130 TDM_ERR("output disconnected");
1131 pthread_mutex_unlock(&private_client->lock);
1132 return TDM_ERROR_OUTPUT_DISCONNECTED;
1134 if (TDM_OUTPUT_DPMS_VSYNC_IS_OFF(private_output->dpms)) {
1135 TDM_ERR("dpms %s", tdm_dpms_str(private_output->dpms));
1136 pthread_mutex_unlock(&private_client->lock);
1137 return TDM_ERROR_DPMS_OFF;
1141 w = calloc(1, sizeof *w);
1143 /* LCOV_EXCL_START */
1145 TDM_ERR("alloc failed");
1146 pthread_mutex_unlock(&private_client->lock);
1147 return TDM_ERROR_OUT_OF_MEMORY;
1149 /* LCOV_EXCL_STOP */
1152 w->private_vblank = private_vblank;
1154 w->user_data = user_data;
1156 LIST_ADDTAIL(&w->link, &private_vblank->wait_list);
1157 LIST_INITHEAD(&w->call_link);
1159 clock_gettime(CLOCK_MONOTONIC, &tp);
1160 req_sec = (unsigned int)tp.tv_sec;
1161 req_usec = (unsigned int)(tp.tv_nsec / 1000);
1163 w->req_id = ++private_output->req_id;
1164 w->req_time = TDM_TIME(req_sec, req_usec);
1165 w->need_free = (private_vblank->sync) ? 0 : 1;
1167 wl_tdm_vblank_wait_vblank(private_vblank->vblank, interval, w->req_id, req_sec, req_usec);
1169 if (private_vblank->enable_ttrace)
1170 TDM_TRACE_ASYNC_BEGIN((int)w->req_time, "TDM_Client_Vblank:%u", private_vblank->stamp);
1172 TDM_DBG("vblank(%p) interval(%u) req_id(%d) req(%.6f)",
1173 vblank, interval, w->req_id, w->req_time);
1175 private_vblank->req_time = w->req_time;
1177 if (private_vblank->last_time >= w->req_time)
1178 TDM_ERR("'last(%.6f) < req(%.6f)' failed", private_vblank->last_time, w->req_time);
1180 if (!private_vblank->sync) {
1181 wl_display_flush(private_client->display);
1182 pthread_mutex_unlock(&private_client->lock);
1183 return TDM_ERROR_NONE;
1186 /* LCOV_EXCL_START */
1188 while (ret != -1 && !w->need_free)
1189 ret = wl_display_dispatch(private_client->display);
1191 clock_gettime(CLOCK_MONOTONIC, &tp);
1192 TDM_DBG("block during %d us",
1193 ((unsigned int)(tp.tv_sec * 1000000) + (unsigned int)(tp.tv_nsec / 1000))
1194 - (req_sec * 1000000 + req_usec));
1199 pthread_mutex_unlock(&private_client->lock);
1201 return TDM_ERROR_NONE;
1203 /* LCOV_EXCL_STOP */
1207 tdm_client_vblank_wait_seq(tdm_client_vblank *vblank, unsigned int sequence,
1208 tdm_client_vblank_handler func, void *user_data)
1210 tdm_private_client *private_client;
1211 tdm_private_client_output *private_output;
1212 tdm_private_client_vblank *private_vblank;
1213 tdm_client_wait_info *w;
1215 unsigned int req_sec, req_usec;
1218 TDM_RETURN_VAL_IF_FAIL(vblank != NULL, TDM_ERROR_INVALID_PARAMETER);
1219 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
1221 private_vblank = vblank;
1222 private_output = private_vblank->private_output;
1223 private_client = private_output->private_client;
1225 pthread_mutex_lock(&private_client->lock);
1227 if (!private_vblank->started)
1228 private_vblank->started = 1;
1230 if (private_output->watch_output_changes && !private_vblank->enable_fake) {
1231 if (private_output->connection == TDM_OUTPUT_CONN_STATUS_DISCONNECTED) {
1232 TDM_ERR("output disconnected");
1233 pthread_mutex_unlock(&private_client->lock);
1234 return TDM_ERROR_OUTPUT_DISCONNECTED;
1236 if (TDM_OUTPUT_DPMS_VSYNC_IS_OFF(private_output->dpms)) {
1237 TDM_ERR("dpms %s", tdm_dpms_str(private_output->dpms));
1238 pthread_mutex_unlock(&private_client->lock);
1239 return TDM_ERROR_DPMS_OFF;
1243 w = calloc(1, sizeof *w);
1245 /* LCOV_EXCL_START */
1247 TDM_ERR("alloc failed");
1248 pthread_mutex_unlock(&private_client->lock);
1249 return TDM_ERROR_OUT_OF_MEMORY;
1251 /* LCOV_EXCL_STOP */
1254 w->private_vblank = private_vblank;
1256 w->user_data = user_data;
1258 LIST_ADDTAIL(&w->link, &private_vblank->wait_list);
1259 LIST_INITHEAD(&w->call_link);
1261 clock_gettime(CLOCK_MONOTONIC, &tp);
1262 req_sec = (unsigned int)tp.tv_sec;
1263 req_usec = (unsigned int)(tp.tv_nsec / 1000);
1265 w->req_id = ++private_output->req_id;
1266 w->req_time = TDM_TIME(req_sec, req_usec);
1267 w->need_free = (private_vblank->sync) ? 0 : 1;
1269 wl_tdm_vblank_wait_vblank_seq(private_vblank->vblank, sequence, w->req_id, req_sec, req_usec);
1271 if (private_vblank->enable_ttrace)
1272 TDM_TRACE_ASYNC_BEGIN((int)w->req_time, "TDM_Client_Vblank:%u", private_vblank->stamp);
1274 TDM_DBG("vblank(%p) sequence(%u) req_id(%d) req(%.6f)",
1275 vblank, sequence, w->req_id, w->req_time);
1277 private_vblank->req_time = w->req_time;
1279 if (private_vblank->last_time >= w->req_time)
1280 TDM_ERR("'last(%.6f) < req(%.6f)' failed", private_vblank->last_time, w->req_time);
1282 if (!private_vblank->sync) {
1283 wl_display_flush(private_client->display);
1284 pthread_mutex_unlock(&private_client->lock);
1285 return TDM_ERROR_NONE;
1288 /* LCOV_EXCL_START */
1290 while (ret != -1 && !w->need_free)
1291 ret = wl_display_dispatch(private_client->display);
1293 clock_gettime(CLOCK_MONOTONIC, &tp);
1294 TDM_DBG("block during %d us",
1295 ((unsigned int)(tp.tv_sec * 1000000) + (unsigned int)(tp.tv_nsec / 1000))
1296 - (req_sec * 1000000 + req_usec));
1301 pthread_mutex_unlock(&private_client->lock);
1303 return TDM_ERROR_NONE;
1305 /* LCOV_EXCL_STOP */