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 **************************************************************************/
41 #include "tdm_private.h"
43 #include "tdm-server-protocol.h"
46 * - tdm server doesn't care about thread things.
47 * - DO NOT use the TDM internal functions here.
48 * However, the internal function which does lock/unlock the mutex of
49 * private_display in itself can be called.
50 * - DO NOT use the tdm_private_display structure here.
53 struct _tdm_private_server {
54 tdm_private_loop *private_loop;
55 struct list_head client_list;
56 struct list_head vblank_list;
59 typedef struct _tdm_server_client_info {
60 struct list_head link;
61 tdm_private_server *private_server;
62 struct wl_resource *resource;
64 tdm_output *vblank_output;
66 unsigned int last_tv_sec;
67 unsigned int last_tv_usec;
68 } tdm_server_client_info;
70 typedef struct _tdm_server_vblank_info {
71 struct list_head link;
72 tdm_server_client_info *client_info;
73 struct wl_resource *resource;
76 unsigned int req_usec;
78 tdm_event_loop_source *timer_source;
79 unsigned int timer_target_sec;
80 unsigned int timer_target_usec;
81 } tdm_server_vblank_info;
83 static tdm_private_server *keep_private_server;
84 static int tdm_debug_server;
87 _tdm_server_send_done(tdm_server_vblank_info *vblank_info, unsigned int sequence,
88 unsigned int tv_sec, unsigned int tv_usec);
91 _tdm_server_cb_timer(void *user_data)
93 tdm_server_vblank_info *vblank_info = (tdm_server_vblank_info*)user_data;
95 _tdm_server_send_done(vblank_info, 0,
96 vblank_info->timer_target_sec,
97 vblank_info->timer_target_usec);
99 return TDM_ERROR_NONE;
103 _tdm_server_update_timer(tdm_server_vblank_info *vblank_info, int interval)
105 tdm_server_client_info *client_info = vblank_info->client_info;
106 tdm_private_server *private_server = client_info->private_server;
107 tdm_private_loop *private_loop = private_server->private_loop;
108 unsigned long last, prev_req, req, curr, next;
109 unsigned int ms_delay;
112 ret = tdm_display_lock(private_loop->dpy);
113 TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, ret);
115 if (!vblank_info->timer_source) {
116 vblank_info->timer_source =
117 tdm_event_loop_add_timer_handler(private_loop->dpy,
118 _tdm_server_cb_timer,
121 if (!vblank_info->timer_source) {
122 TDM_ERR("couldn't add timer");
123 tdm_display_unlock(private_loop->dpy);
124 return TDM_ERROR_OPERATION_FAILED;
128 last = (unsigned long)client_info->last_tv_sec * 1000000 + client_info->last_tv_usec;
129 req = (unsigned long)vblank_info->req_sec * 1000000 + vblank_info->req_usec;
130 curr = tdm_helper_get_time_in_micros();
132 prev_req = last + (unsigned int)((req - last) / client_info->vblank_gap) * client_info->vblank_gap;
133 next = prev_req + (unsigned long)(client_info->vblank_gap * interval);
136 next += (unsigned long)client_info->vblank_gap;
138 TDM_DBG("last(%.6lu) req(%.6lu) curr(%.6lu) prev_req(%.6lu) next(%.6lu)",
139 last, req, curr, prev_req, next);
141 ms_delay = (unsigned int)ceil((double)(next - curr) / 1000);
145 TDM_DBG("delay(%lu) ms_delay(%d)", next - curr, ms_delay);
147 ret = tdm_event_loop_source_timer_update(vblank_info->timer_source, ms_delay);
148 if (ret != TDM_ERROR_NONE) {
149 tdm_event_loop_source_remove(vblank_info->timer_source);
150 vblank_info->timer_source = NULL;
151 tdm_display_unlock(private_loop->dpy);
152 TDM_ERR("couldn't update timer");
153 return TDM_ERROR_OPERATION_FAILED;
156 TDM_DBG("timer tick: %d us", (int)(next - last));
158 vblank_info->timer_target_sec = next / 1000000;
159 vblank_info->timer_target_usec = next % 1000000;
161 tdm_display_unlock(private_loop->dpy);
163 return TDM_ERROR_NONE;
167 _tdm_server_send_done(tdm_server_vblank_info *vblank_info, unsigned int sequence,
168 unsigned int tv_sec, unsigned int tv_usec)
170 tdm_server_vblank_info *found;
171 tdm_server_client_info *client_info;
172 unsigned long vtime = (tv_sec * 1000000) + tv_usec;
173 unsigned long curr = tdm_helper_get_time_in_micros();
175 if (!keep_private_server)
178 LIST_FIND_ITEM(vblank_info, &keep_private_server->vblank_list,
179 tdm_server_vblank_info, link, found);
181 TDM_DBG("vblank_info(%p) is destroyed", vblank_info);
185 client_info = vblank_info->client_info;
186 client_info->last_tv_sec = tv_sec;
187 client_info->last_tv_usec = tv_usec;
189 TDM_DBG("wl_tdm_vblank@%d done. tv(%lu) curr(%lu)",
190 wl_resource_get_id(vblank_info->resource), vtime, curr);
192 if (tdm_debug_server) {
193 if (curr - vtime > 1000) /* 1ms */
194 TDM_WRN("delay: %d us", (int)(curr - vtime));
197 wl_tdm_vblank_send_done(vblank_info->resource, sequence, tv_sec, tv_usec);
198 wl_resource_destroy(vblank_info->resource);
202 _tdm_server_cb_output_vblank(tdm_output *output, unsigned int sequence,
203 unsigned int tv_sec, unsigned int tv_usec,
206 tdm_server_vblank_info *vblank_info = (tdm_server_vblank_info*)user_data;
208 _tdm_server_send_done(vblank_info, sequence, tv_sec, tv_usec);
212 destroy_vblank_callback(struct wl_resource *resource)
214 tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
216 if (vblank_info->timer_source) {
217 tdm_private_server *private_server = vblank_info->client_info->private_server;
219 tdm_display_lock(private_server->private_loop->dpy);
220 tdm_event_loop_source_remove(vblank_info->timer_source);
221 tdm_display_unlock(private_server->private_loop->dpy);
224 LIST_DEL(&vblank_info->link);
229 _tdm_server_client_cb_destroy(struct wl_client *client, struct wl_resource *resource)
231 wl_resource_destroy(resource);
235 _tdm_server_client_cb_wait_vblank(struct wl_client *client,
236 struct wl_resource *resource,
237 uint32_t id, const char *name,
238 int32_t sw_timer, int32_t interval,
239 uint32_t req_sec, uint32_t req_usec)
241 tdm_server_client_info *client_info = wl_resource_get_user_data(resource);
242 tdm_private_server *private_server = client_info->private_server;
243 tdm_private_loop *private_loop = private_server->private_loop;
244 tdm_server_vblank_info *vblank_info;
245 struct wl_resource *vblank_resource;
246 tdm_output *found = NULL;
247 tdm_output_dpms dpms_value = TDM_OUTPUT_DPMS_ON;
251 TDM_DBG("The tdm client requests vblank");
253 if (tdm_debug_server) {
254 unsigned long curr = tdm_helper_get_time_in_micros();
255 unsigned long reqtime = (req_sec * 1000000) + req_usec;
256 if (curr - reqtime > 1000) /* 1ms */
257 TDM_WRN("delay(req): %d us", (int)(curr - reqtime));
260 if (client_info->vblank_output) {
262 ret = tdm_output_get_model_info(client_info->vblank_output, NULL, &model, NULL);
263 if (model && !strncmp(model, name, TDM_NAME_LEN))
264 found = client_info->vblank_output;
267 if (!strncmp(name, "primary", TDM_NAME_LEN))
268 found = tdm_display_get_output(private_loop->dpy, 0, NULL);
273 tdm_display_get_output_count(private_loop->dpy, &count);
275 for (i = 0; i < count; i++) {
276 tdm_output *output = tdm_display_get_output(private_loop->dpy, i, NULL);
277 tdm_output_conn_status status;
279 ret = tdm_output_get_conn_status(output, &status);
280 if (ret || status == TDM_OUTPUT_CONN_STATUS_DISCONNECTED)
284 ret = tdm_output_get_model_info(output, NULL, &model, NULL);
288 if (strncmp(model, name, TDM_NAME_LEN))
297 wl_resource_post_error(resource, WL_TDM_CLIENT_ERROR_INVALID_NAME,
298 "There is no '%s' output", name);
299 TDM_ERR("There is no '%s' output", name);
303 if (client_info->vblank_output != found) {
304 const tdm_output_mode *mode = NULL;
306 client_info->vblank_output = found;
308 tdm_output_get_mode(client_info->vblank_output, &mode);
309 if (!mode || mode->vrefresh <= 0) {
310 wl_resource_post_error(resource, WL_TDM_CLIENT_ERROR_OPERATION_FAILED,
311 "couldn't get mode of %s", name);
312 TDM_ERR("couldn't get mode of %s", name);
316 client_info->vblank_gap = (double)1000000 / mode->vrefresh;
317 TDM_INFO("vblank_gap(%.6lf)", client_info->vblank_gap);
320 tdm_output_get_dpms(found, &dpms_value);
322 if (dpms_value != TDM_OUTPUT_DPMS_ON && !sw_timer) {
323 wl_resource_post_error(resource, WL_TDM_CLIENT_ERROR_DPMS_OFF,
324 "dpms '%s'", tdm_get_dpms_str(dpms_value));
325 TDM_ERR("dpms '%s'", tdm_get_dpms_str(dpms_value));
329 vblank_info = calloc(1, sizeof * vblank_info);
331 wl_resource_post_no_memory(resource);
332 TDM_ERR("alloc failed");
336 TDM_DBG("wl_tdm_vblank@%d output(%s) interval(%d)", id, name, interval);
339 wl_resource_create(client, &wl_tdm_vblank_interface,
340 wl_resource_get_version(resource), id);
341 if (!vblank_resource) {
342 wl_resource_post_no_memory(resource);
343 TDM_ERR("wl_resource_create failed");
347 vblank_info->resource = vblank_resource;
348 vblank_info->client_info = client_info;
349 vblank_info->req_sec = req_sec;
350 vblank_info->req_usec = req_usec;
352 if (dpms_value == TDM_OUTPUT_DPMS_ON) {
353 ret = tdm_output_wait_vblank(found, interval, 0,
354 _tdm_server_cb_output_vblank, vblank_info);
355 if (ret != TDM_ERROR_NONE) {
356 wl_resource_post_error(resource, WL_TDM_CLIENT_ERROR_OPERATION_FAILED,
357 "couldn't wait vblank for %s", name);
358 TDM_ERR("couldn't wait vblank for %s", name);
359 goto destroy_resource;
361 } else if (sw_timer) {
362 ret = _tdm_server_update_timer(vblank_info, interval);
363 if (ret != TDM_ERROR_NONE) {
364 wl_resource_post_error(resource, WL_TDM_CLIENT_ERROR_OPERATION_FAILED,
365 "couldn't update timer for %s", name);
366 TDM_ERR("couldn't update timer for %s", name);
367 goto destroy_resource;
370 wl_resource_post_error(resource, WL_TDM_CLIENT_ERROR_OPERATION_FAILED,
371 "bad implementation");
372 TDM_NEVER_GET_HERE();
373 goto destroy_resource;
376 wl_resource_set_implementation(vblank_resource, NULL, vblank_info,
377 destroy_vblank_callback);
379 LIST_ADDTAIL(&vblank_info->link, &private_server->vblank_list);
382 wl_resource_destroy(vblank_resource);
387 static const struct wl_tdm_client_interface tdm_client_implementation = {
388 _tdm_server_client_cb_destroy,
389 _tdm_server_client_cb_wait_vblank,
393 destroy_client_callback(struct wl_resource *resource)
395 tdm_server_client_info *client_info = wl_resource_get_user_data(resource);
396 LIST_DEL(&client_info->link);
401 _tdm_server_cb_create_client(struct wl_client *client,
402 struct wl_resource *resource, uint32_t id)
404 tdm_private_server *private_server = wl_resource_get_user_data(resource);
405 tdm_server_client_info *client_info;
406 struct wl_resource *client_resource;
408 client_info = calloc(1, sizeof * client_info);
410 wl_resource_post_no_memory(resource);
411 TDM_ERR("alloc failed");
416 wl_resource_create(client, &wl_tdm_client_interface,
417 wl_resource_get_version(resource), id);
418 if (!client_resource) {
419 wl_resource_post_no_memory(resource);
421 TDM_ERR("wl_resource_create failed");
425 client_info->private_server = private_server;
426 client_info->resource = client_resource;
428 wl_resource_set_implementation(client_resource, &tdm_client_implementation,
429 client_info, destroy_client_callback);
431 LIST_ADDTAIL(&client_info->link, &private_server->client_list);
434 static const struct wl_tdm_interface tdm_implementation = {
435 _tdm_server_cb_create_client,
439 _tdm_server_bind(struct wl_client *client, void *data,
440 uint32_t version, uint32_t id)
442 struct wl_resource *resource;
444 resource = wl_resource_create(client, &wl_tdm_interface, version, id);
446 wl_client_post_no_memory(client);
450 TDM_DBG("tdm server binding");
452 wl_resource_set_implementation(resource, &tdm_implementation, data, NULL);
456 tdm_server_init(tdm_private_loop *private_loop)
458 tdm_private_server *private_server;
461 debug = getenv("TDM_DEBUG_SERVER");
462 if (debug && (strstr(debug, "1")))
463 tdm_debug_server = 1;
465 if (private_loop->private_server)
466 return TDM_ERROR_NONE;
468 TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_OPERATION_FAILED);
469 TDM_RETURN_VAL_IF_FAIL(private_loop->wl_display, TDM_ERROR_OPERATION_FAILED);
471 if (wl_display_add_socket(private_loop->wl_display, "tdm-socket")) {
472 TDM_ERR("createing a tdm-socket failed");
473 return TDM_ERROR_OPERATION_FAILED;
476 private_server = calloc(1, sizeof * private_server);
477 if (!private_server) {
478 TDM_ERR("alloc failed");
479 return TDM_ERROR_OUT_OF_MEMORY;
482 LIST_INITHEAD(&private_server->client_list);
483 LIST_INITHEAD(&private_server->vblank_list);
485 if (!wl_global_create(private_loop->wl_display, &wl_tdm_interface, 1,
486 private_server, _tdm_server_bind)) {
487 TDM_ERR("creating a global resource failed");
488 free(private_server);
489 return TDM_ERROR_OUT_OF_MEMORY;
492 private_server->private_loop = private_loop;
493 private_loop->private_server = private_server;
494 keep_private_server = private_server;
496 return TDM_ERROR_NONE;
500 tdm_server_deinit(tdm_private_loop *private_loop)
502 tdm_server_vblank_info *v = NULL, *vv = NULL;
503 tdm_server_client_info *c = NULL, *cc = NULL;
504 tdm_private_server *private_server;
506 if (!private_loop->private_server)
509 private_server = private_loop->private_server;
511 LIST_FOR_EACH_ENTRY_SAFE(v, vv, &private_server->vblank_list, link) {
512 wl_resource_destroy(v->resource);
515 LIST_FOR_EACH_ENTRY_SAFE(c, cc, &private_server->client_list, link) {
516 wl_resource_destroy(c->resource);
519 free(private_server);
520 private_loop->private_server = NULL;
521 keep_private_server = NULL;