Allow output name "primary" in tdm_client_wait_vblank
[platform/core/uifw/libtdm.git] / src / tdm_server.c
1 /**************************************************************************
2  *
3  * libtdm
4  *
5  * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
6  *
7  * Contact: Eunchul Kim <chulspro.kim@samsung.com>,
8  *          JinYoung Jeon <jy0.jeon@samsung.com>,
9  *          Taeheon Kim <th908.kim@samsung.com>,
10  *          YoungJun Cho <yj44.cho@samsung.com>,
11  *          SooChan Lim <sc1.lim@samsung.com>,
12  *          Boram Park <sc1.lim@samsung.com>
13  *
14  * Permission is hereby granted, free of charge, to any person obtaining a
15  * copy of this software and associated documentation files (the
16  * "Software"), to deal in the Software without restriction, including
17  * without limitation the rights to use, copy, modify, merge, publish,
18  * distribute, sub license, and/or sell copies of the Software, and to
19  * permit persons to whom the Software is furnished to do so, subject to
20  * the following conditions:
21  *
22  * The above copyright notice and this permission notice (including the
23  * next paragraph) shall be included in all copies or substantial portions
24  * of the Software.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
27  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
29  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
30  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
31  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
32  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33  *
34 **************************************************************************/
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include "tdm.h"
41 #include "tdm_private.h"
42 #include "tdm_list.h"
43 #include "tdm-server-protocol.h"
44
45 /* CAUTION:
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.
51  */
52
53 struct _tdm_private_server {
54         tdm_private_loop *private_loop;
55         struct list_head client_list;
56         struct list_head vblank_list;
57 };
58
59 typedef struct _tdm_server_client_info {
60         struct list_head link;
61         tdm_private_server *private_server;
62         struct wl_resource *resource;
63
64         tdm_output *vblank_output;
65         double vblank_gap;
66         unsigned int last_tv_sec;
67         unsigned int last_tv_usec;
68 } tdm_server_client_info;
69
70 typedef struct _tdm_server_vblank_info {
71         struct list_head link;
72         tdm_server_client_info *client_info;
73         struct wl_resource *resource;
74
75         unsigned int req_sec;
76         unsigned int req_usec;
77
78         tdm_event_loop_source *timer_source;
79         unsigned int timer_target_sec;
80         unsigned int timer_target_usec;
81 } tdm_server_vblank_info;
82
83 static tdm_private_server *keep_private_server;
84 static int tdm_debug_server;
85
86 static void
87 _tdm_server_send_done(tdm_server_vblank_info *vblank_info, unsigned int sequence,
88                                           unsigned int tv_sec, unsigned int tv_usec);
89
90 static tdm_error
91 _tdm_server_cb_timer(void *user_data)
92 {
93         tdm_server_vblank_info *vblank_info = (tdm_server_vblank_info*)user_data;
94
95         _tdm_server_send_done(vblank_info, 0,
96                                                   vblank_info->timer_target_sec,
97                                                   vblank_info->timer_target_usec);
98
99         return TDM_ERROR_NONE;
100 }
101
102 static tdm_error
103 _tdm_server_update_timer(tdm_server_vblank_info *vblank_info, int interval)
104 {
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;
110         tdm_error ret;
111
112         ret = tdm_display_lock(private_loop->dpy);
113         TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, ret);
114
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,
119                                                                                          vblank_info,
120                                                                                          NULL);
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;
125                 }
126         }
127
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();
131
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);
134
135         while (next < curr)
136                 next += (unsigned long)client_info->vblank_gap;
137
138         TDM_DBG("last(%.6lu) req(%.6lu) curr(%.6lu) prev_req(%.6lu) next(%.6lu)",
139                         last, req, curr, prev_req, next);
140
141         ms_delay = (unsigned int)ceil((double)(next - curr) / 1000);
142         if (ms_delay == 0)
143                 ms_delay = 1;
144
145         TDM_DBG("delay(%lu) ms_delay(%d)", next - curr, ms_delay);
146
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;
154         }
155
156         TDM_DBG("timer tick: %d us", (int)(next - last));
157
158         vblank_info->timer_target_sec = next / 1000000;
159         vblank_info->timer_target_usec = next % 1000000;
160
161         tdm_display_unlock(private_loop->dpy);
162
163         return TDM_ERROR_NONE;
164 }
165
166 static void
167 _tdm_server_send_done(tdm_server_vblank_info *vblank_info, unsigned int sequence,
168                                           unsigned int tv_sec, unsigned int tv_usec)
169 {
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();
174
175         if (!keep_private_server)
176                 return;
177
178         LIST_FIND_ITEM(vblank_info, &keep_private_server->vblank_list,
179                                    tdm_server_vblank_info, link, found);
180         if (!found) {
181                 TDM_DBG("vblank_info(%p) is destroyed", vblank_info);
182                 return;
183         }
184
185         client_info = vblank_info->client_info;
186         client_info->last_tv_sec = tv_sec;
187         client_info->last_tv_usec = tv_usec;
188
189         TDM_DBG("wl_tdm_vblank@%d done. tv(%lu) curr(%lu)",
190                         wl_resource_get_id(vblank_info->resource), vtime, curr);
191
192         if (tdm_debug_server) {
193                 if (curr - vtime > 1000) /* 1ms */
194                         TDM_WRN("delay: %d us", (int)(curr - vtime));
195         }
196
197         wl_tdm_vblank_send_done(vblank_info->resource, sequence, tv_sec, tv_usec);
198         wl_resource_destroy(vblank_info->resource);
199 }
200
201 static void
202 _tdm_server_cb_output_vblank(tdm_output *output, unsigned int sequence,
203                                                          unsigned int tv_sec, unsigned int tv_usec,
204                                                          void *user_data)
205 {
206         tdm_server_vblank_info *vblank_info = (tdm_server_vblank_info*)user_data;
207
208         _tdm_server_send_done(vblank_info, sequence, tv_sec, tv_usec);
209 }
210
211 static void
212 destroy_vblank_callback(struct wl_resource *resource)
213 {
214         tdm_server_vblank_info *vblank_info = wl_resource_get_user_data(resource);
215
216         if (vblank_info->timer_source) {
217                 tdm_private_server *private_server = vblank_info->client_info->private_server;
218
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);
222         }
223
224         LIST_DEL(&vblank_info->link);
225         free(vblank_info);
226 }
227
228 static void
229 _tdm_server_client_cb_destroy(struct wl_client *client, struct wl_resource *resource)
230 {
231         wl_resource_destroy(resource);
232 }
233
234 static void
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)
240 {
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;
248         tdm_error ret;
249         const char *model;
250
251         TDM_DBG("The tdm client requests vblank");
252
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));
258         }
259
260         if (client_info->vblank_output) {
261                 model = NULL;
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;
265         }
266
267         if (!strncmp(name, "primary", TDM_NAME_LEN))
268                 found = tdm_display_get_output(private_loop->dpy, 0, NULL);
269
270         if (!found) {
271                 int count = 0, i;
272
273                 tdm_display_get_output_count(private_loop->dpy, &count);
274
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;
278
279                         ret = tdm_output_get_conn_status(output, &status);
280                         if (ret || status == TDM_OUTPUT_CONN_STATUS_DISCONNECTED)
281                                 continue;
282
283                         model = NULL;
284                         ret = tdm_output_get_model_info(output, NULL, &model, NULL);
285                         if (ret || !model)
286                                 continue;
287
288                         if (strncmp(model, name, TDM_NAME_LEN))
289                                 continue;
290
291                         found = output;
292                         break;
293                 }
294         }
295
296         if (!found) {
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);
300                 return;
301         }
302
303         if (client_info->vblank_output != found) {
304                 const tdm_output_mode *mode = NULL;
305
306                 client_info->vblank_output = found;
307
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);
313                         return;
314                 }
315
316                 client_info->vblank_gap = (double)1000000 / mode->vrefresh;
317                 TDM_INFO("vblank_gap(%.6lf)", client_info->vblank_gap);
318         }
319
320         tdm_output_get_dpms(found, &dpms_value);
321
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));
326                 return;
327         }
328
329         vblank_info = calloc(1, sizeof * vblank_info);
330         if (!vblank_info) {
331                 wl_resource_post_no_memory(resource);
332                 TDM_ERR("alloc failed");
333                 return;
334         }
335
336         TDM_DBG("wl_tdm_vblank@%d output(%s) interval(%d)", id, name, interval);
337
338         vblank_resource =
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");
344                 goto free_info;
345         }
346
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;
351
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;
360                 }
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;
368                 }
369         } else {
370                 wl_resource_post_error(resource, WL_TDM_CLIENT_ERROR_OPERATION_FAILED,
371                                                            "bad implementation");
372                 TDM_NEVER_GET_HERE();
373                 goto destroy_resource;
374         }
375
376         wl_resource_set_implementation(vblank_resource, NULL, vblank_info,
377                                                                    destroy_vblank_callback);
378
379         LIST_ADDTAIL(&vblank_info->link, &private_server->vblank_list);
380         return;
381 destroy_resource:
382         wl_resource_destroy(vblank_resource);
383 free_info:
384         free(vblank_info);
385 }
386
387 static const struct wl_tdm_client_interface tdm_client_implementation = {
388         _tdm_server_client_cb_destroy,
389         _tdm_server_client_cb_wait_vblank,
390 };
391
392 static void
393 destroy_client_callback(struct wl_resource *resource)
394 {
395         tdm_server_client_info *client_info = wl_resource_get_user_data(resource);
396         LIST_DEL(&client_info->link);
397         free(client_info);
398 }
399
400 static void
401 _tdm_server_cb_create_client(struct wl_client *client,
402                                                          struct wl_resource *resource, uint32_t id)
403 {
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;
407
408         client_info = calloc(1, sizeof * client_info);
409         if (!client_info) {
410                 wl_resource_post_no_memory(resource);
411                 TDM_ERR("alloc failed");
412                 return;
413         }
414
415         client_resource =
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);
420                 free(client_info);
421                 TDM_ERR("wl_resource_create failed");
422                 return;
423         }
424
425         client_info->private_server = private_server;
426         client_info->resource = client_resource;
427
428         wl_resource_set_implementation(client_resource, &tdm_client_implementation,
429                                                                    client_info, destroy_client_callback);
430
431         LIST_ADDTAIL(&client_info->link, &private_server->client_list);
432 }
433
434 static const struct wl_tdm_interface tdm_implementation = {
435         _tdm_server_cb_create_client,
436 };
437
438 static void
439 _tdm_server_bind(struct wl_client *client, void *data,
440                                  uint32_t version, uint32_t id)
441 {
442         struct wl_resource *resource;
443
444         resource = wl_resource_create(client, &wl_tdm_interface, version, id);
445         if (!resource) {
446                 wl_client_post_no_memory(client);
447                 return;
448         }
449
450         TDM_DBG("tdm server binding");
451
452         wl_resource_set_implementation(resource, &tdm_implementation, data, NULL);
453 }
454
455 INTERN tdm_error
456 tdm_server_init(tdm_private_loop *private_loop)
457 {
458         tdm_private_server *private_server;
459         const char *debug;
460
461         debug = getenv("TDM_DEBUG_SERVER");
462         if (debug && (strstr(debug, "1")))
463                 tdm_debug_server = 1;
464
465         if (private_loop->private_server)
466                 return TDM_ERROR_NONE;
467
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);
470
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;
474         }
475
476         private_server = calloc(1, sizeof * private_server);
477         if (!private_server) {
478                 TDM_ERR("alloc failed");
479                 return TDM_ERROR_OUT_OF_MEMORY;
480         }
481
482         LIST_INITHEAD(&private_server->client_list);
483         LIST_INITHEAD(&private_server->vblank_list);
484
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;
490         }
491
492         private_server->private_loop = private_loop;
493         private_loop->private_server = private_server;
494         keep_private_server = private_server;
495
496         return TDM_ERROR_NONE;
497 }
498
499 INTERN void
500 tdm_server_deinit(tdm_private_loop *private_loop)
501 {
502         tdm_server_vblank_info *v = NULL, *vv = NULL;
503         tdm_server_client_info *c = NULL, *cc = NULL;
504         tdm_private_server *private_server;
505
506         if (!private_loop->private_server)
507                 return;
508
509         private_server = private_loop->private_server;
510
511         LIST_FOR_EACH_ENTRY_SAFE(v, vv, &private_server->vblank_list, link) {
512                 wl_resource_destroy(v->resource);
513         }
514
515         LIST_FOR_EACH_ENTRY_SAFE(c, cc, &private_server->client_list, link) {
516                 wl_resource_destroy(c->resource);
517         }
518
519         free(private_server);
520         private_loop->private_server = NULL;
521         keep_private_server = NULL;
522 }