debug: add event type
[platform/core/uifw/libtdm.git] / src / tdm_thread.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_private.h"
41
42 static tdm_private_thread *keep_private_thread;
43
44 struct _tdm_private_thread {
45         tdm_private_loop *private_loop;
46
47         pthread_t event_thread;
48
49         pid_t display_tid;
50         pid_t thread_tid;
51
52         /* 0: read, 1: write */
53         int pipe[2];
54 };
55
56 static void*
57 _tdm_thread_main(void *data)
58 {
59         tdm_private_thread *private_thread = (tdm_private_thread*)data;
60         tdm_private_loop *private_loop = private_thread->private_loop;
61         int fd;
62         struct pollfd fds;
63         int ret;
64
65         /* Not lock/unlock for the private_thread and private_loop structure
66          * because they won't be destroyed as long as tdm thread is running.
67          * When they're destroyed, we have already exit the tdm thread.
68          */
69         private_thread->thread_tid = syscall(SYS_gettid);
70
71         TDM_INFO("display_tid:%d, thread_tid: %d",
72                          private_thread->display_tid, private_thread->thread_tid);
73
74         fd = tdm_event_loop_get_fd(private_loop->dpy);
75         if (fd < 0) {
76                 TDM_ERR("couldn't get fd");
77                 goto exit_thread;
78         }
79
80         fds.events = POLLIN;
81         fds.fd = fd;
82         fds.revents = 0;
83
84         while (1) {
85                 if (tdm_debug_module & TDM_DEBUG_EVENT)
86                         TDM_INFO("server flush");
87                 tdm_event_loop_flush(private_loop->dpy);
88
89                 if (tdm_debug_module & TDM_DEBUG_EVENT)
90                         TDM_INFO("fd(%d) polling in", fd);
91
92                 ret = poll(&fds, 1, -1);
93
94                 if (tdm_debug_module & TDM_DEBUG_EVENT)
95                         TDM_INFO("fd(%d) polling out", fd);
96
97                 if (ret < 0) {
98                         if (errno == EINTR || errno == EAGAIN)  /* normal case */
99                                 continue;
100                         else {
101                                 TDM_ERR("poll failed: %m");
102                                 goto exit_thread;
103                         }
104                 }
105
106                 if (tdm_debug_module & TDM_DEBUG_EVENT)
107                         TDM_INFO("thread got events");
108
109                 if (tdm_event_loop_dispatch(private_loop->dpy) < 0)
110                         TDM_ERR("dispatch error");
111         }
112
113 exit_thread:
114         pthread_exit(NULL);
115 }
116
117 /* NOTE: tdm thread doesn't care about multi-thread. */
118 INTERN tdm_error
119 tdm_thread_init(tdm_private_loop *private_loop)
120 {
121         tdm_private_display *private_display;
122         tdm_private_thread *private_thread;
123         int thread;
124
125         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
126         TDM_RETURN_VAL_IF_FAIL(private_loop->dpy, TDM_ERROR_OPERATION_FAILED);
127
128         private_display = private_loop->dpy;
129         TDM_RETURN_VAL_IF_FAIL(private_display->private_loop, TDM_ERROR_OPERATION_FAILED);
130
131         if (private_loop->private_thread)
132                 return TDM_ERROR_NONE;
133
134         /* enable as default */
135         thread = tdm_config_get_int(TDM_CONFIG_KEY_GENERAL_THREAD, 1);
136         if (!thread) {
137                 TDM_INFO("not using a TDM event thread");
138                 return TDM_ERROR_NONE;
139         }
140
141         private_thread = calloc(1, sizeof * private_thread);
142         if (!private_thread) {
143                 TDM_ERR("alloc failed");
144                 return TDM_ERROR_OUT_OF_MEMORY;
145         }
146
147         if (pipe(private_thread->pipe) != 0) {
148                 TDM_ERR("socketpair failed: %m");
149                 free(private_thread);
150                 return TDM_ERROR_OPERATION_FAILED;
151         }
152
153         private_thread->private_loop = private_loop;
154         private_loop->private_thread = private_thread;
155
156         private_thread->display_tid = syscall(SYS_gettid);
157
158         pthread_create(&private_thread->event_thread, NULL, _tdm_thread_main,
159                                    private_thread);
160
161         keep_private_thread = private_thread;
162
163         TDM_INFO("using a TDM event thread. pipe(%d,%d)",
164                          private_thread->pipe[0], private_thread->pipe[1]);
165
166         return TDM_ERROR_NONE;
167 }
168
169 INTERN void
170 tdm_thread_deinit(tdm_private_loop *private_loop)
171 {
172         tdm_private_display *private_display;
173
174         TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
175
176         if (!private_loop->private_thread)
177                 return;
178
179         pthread_cancel(private_loop->private_thread->event_thread);
180
181         private_display = private_loop->dpy;
182
183         /* before falling into the block of pthread_join, we have to unlock the mutex
184          * for subthread to use the mutex.
185          */
186         _pthread_mutex_unlock(&private_display->lock);
187         pthread_join(private_loop->private_thread->event_thread, NULL);
188
189         if (private_loop->private_thread->pipe[0] >= 0)
190                 close(private_loop->private_thread->pipe[0]);
191         if (private_loop->private_thread->pipe[1] >= 0)
192                 close(private_loop->private_thread->pipe[1]);
193
194         free(private_loop->private_thread);
195         private_loop->private_thread = NULL;
196         keep_private_thread = NULL;
197
198         TDM_INFO("Finish a TDM event thread");
199 }
200
201 INTERN int
202 tdm_thread_get_fd(tdm_private_loop *private_loop)
203 {
204         tdm_private_thread *private_thread;
205
206         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
207         TDM_RETURN_VAL_IF_FAIL(private_loop, -1);
208         TDM_RETURN_VAL_IF_FAIL(private_loop->private_thread, -1);
209
210         private_thread = private_loop->private_thread;
211
212         return private_thread->pipe[0];
213 }
214
215 INTERN tdm_error
216 tdm_thread_send_cb(tdm_private_loop *private_loop, tdm_thread_cb_base *base)
217 {
218         tdm_private_thread *private_thread;
219         ssize_t len;
220
221         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
222         TDM_RETURN_VAL_IF_FAIL(base, TDM_ERROR_INVALID_PARAMETER);
223         TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_INVALID_PARAMETER);
224         TDM_RETURN_VAL_IF_FAIL(private_loop->private_thread, TDM_ERROR_INVALID_PARAMETER);
225
226         private_thread = private_loop->private_thread;
227
228         if (tdm_debug_module & TDM_DEBUG_THREAD)
229                 TDM_INFO("fd(%d) type(%d), length(%d)",
230                                  private_thread->pipe[1], base->type, base->length);
231
232         len = write(private_thread->pipe[1], base, base->length);
233         if (len != base->length) {
234                 TDM_ERR("write failed (%d != %d): %m", (int)len, base->length);
235                 return TDM_ERROR_OPERATION_FAILED;
236         }
237
238         return TDM_ERROR_NONE;
239 }
240
241 INTERN tdm_error
242 tdm_thread_handle_cb(tdm_private_loop *private_loop)
243 {
244         tdm_private_display *private_display;
245         tdm_private_thread *private_thread;
246         tdm_thread_cb_base *base;
247         char buffer[1024];
248         unsigned int i;
249         int len;
250
251         /* DON'T check TDM_MUTEX_IS_LOCKED here */
252
253         TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_INVALID_PARAMETER);
254         TDM_RETURN_VAL_IF_FAIL(private_loop->private_thread, TDM_ERROR_INVALID_PARAMETER);
255
256         private_thread = private_loop->private_thread;
257         private_display = private_loop->dpy;
258
259         do {
260                 len = read(private_thread->pipe[0], buffer, sizeof buffer);
261         } while (len < 0 && errno == EINTR);
262
263         if (tdm_debug_module & TDM_DEBUG_THREAD)
264                 TDM_INFO("fd(%d) read length(%d)", private_thread->pipe[0], len);
265
266         if (len < 0) {
267                 TDM_ERR("read failed: errno(%d), len(%d) %m", errno, len);
268                 return TDM_ERROR_OPERATION_FAILED;
269         }
270
271         if (len == 0)
272                 return TDM_ERROR_NONE;
273
274         if (len < sizeof * base) {
275                 TDM_ERR("read failed: len(%d)", len);
276                 return TDM_ERROR_OPERATION_FAILED;
277         }
278
279         _pthread_mutex_lock(&private_display->lock);
280
281         i = 0;
282         while (i < len) {
283                 base = (tdm_thread_cb_base*)&buffer[i];
284                 if (tdm_debug_module & TDM_DEBUG_THREAD)
285                         TDM_INFO("type(%d), length(%d)", base->type, base->length);
286                 switch (base->type) {
287                 case TDM_THREAD_CB_OUTPUT_COMMIT: {
288                         tdm_thread_cb_output_commit *output_commit = (tdm_thread_cb_output_commit*)base;
289                         tdm_output *output_backend =
290                                 tdm_display_find_output_stamp(private_loop->dpy, output_commit->output_stamp);
291                         if (!output_backend) {
292                                 TDM_WRN("no output(%f)", output_commit->output_stamp);
293                                 break;
294                         }
295                         tdm_output_cb_commit(output_backend, output_commit->sequence,
296                                                                  output_commit->tv_sec, output_commit->tv_usec,
297                                                                  output_commit->user_data);
298                         break;
299                 }
300                 case TDM_THREAD_CB_OUTPUT_VBLANK: {
301                         tdm_thread_cb_output_vblank *output_vblank = (tdm_thread_cb_output_vblank*)base;
302                         tdm_output *output_backend =
303                                 tdm_display_find_output_stamp(private_loop->dpy, output_vblank->output_stamp);
304                         if (!output_backend) {
305                                 TDM_WRN("no output(%f)", output_vblank->output_stamp);
306                                 break;
307                         }
308                         tdm_output_cb_vblank(output_backend, output_vblank->sequence,
309                                                                  output_vblank->tv_sec, output_vblank->tv_usec,
310                                                                  output_vblank->user_data);
311                         break;
312                 }
313                 case TDM_THREAD_CB_OUTPUT_STATUS: {
314                         /* LCOV_EXCL_START */
315                         tdm_thread_cb_output_status *output_status = (tdm_thread_cb_output_status*)base;
316                         tdm_output *output_backend =
317                                 tdm_display_find_output_stamp(private_loop->dpy, output_status->output_stamp);
318                         if (!output_backend) {
319                                 TDM_WRN("no output(%f)", output_status->output_stamp);
320                                 break;
321                         }
322                         tdm_output_cb_status(output_backend, output_status->status,
323                                                                  output_status->user_data);
324                         break;
325                         /* LCOV_EXCL_STOP */
326                 }
327                 case TDM_THREAD_CB_OUTPUT_DPMS: {
328                         /* LCOV_EXCL_START */
329                         tdm_thread_cb_output_dpms *output_dpms = (tdm_thread_cb_output_dpms*)base;
330                         tdm_output *output_backend =
331                                 tdm_display_find_output_stamp(private_loop->dpy, output_dpms->output_stamp);
332                         if (!output_backend) {
333                                 TDM_WRN("no output(%f)", output_dpms->output_stamp);
334                                 break;
335                         }
336                         tdm_output_cb_dpms(output_backend, output_dpms->dpms, output_dpms->user_data);
337                         break;
338                         /* LCOV_EXCL_STOP */
339                 }
340                 case TDM_THREAD_CB_PP_DONE: {
341                         tdm_thread_cb_pp_done *pp_done = (tdm_thread_cb_pp_done*)base;
342                         tdm_pp *pp_backend =
343                                 tdm_pp_find_stamp(private_loop->dpy, pp_done->pp_stamp);
344                         if (!pp_backend) {
345                                 TDM_WRN("no pp(%f)", pp_done->pp_stamp);
346                                 break;
347                         }
348                         tdm_pp_cb_done(pp_backend, pp_done->src, pp_done->dst, pp_done->user_data);
349                         break;
350                 }
351                 case TDM_THREAD_CB_CAPTURE_DONE: {
352                         tdm_thread_cb_capture_done *capture_done = (tdm_thread_cb_capture_done*)base;
353                         tdm_capture *capture_backend =
354                                 tdm_capture_find_stamp(private_loop->dpy, capture_done->capture_stamp);
355                         if (!capture_backend) {
356                                 TDM_WRN("no capture(%f)", capture_done->capture_stamp);
357                                 break;
358                         }
359                         tdm_capture_cb_done(capture_backend, capture_done->buffer, capture_done->user_data);
360                         break;
361                 }
362                 case TDM_THREAD_CB_VBLANK_SW: {
363                         tdm_thread_cb_vblank_sw *vblank_sw = (tdm_thread_cb_vblank_sw*)base;
364                         tdm_vblank_cb_vblank_SW(NULL, vblank_sw->vblank_stamp);
365                         break;
366                 }
367                 case TDM_THREAD_CB_VBLANK_CREATE: {
368                         tdm_thread_cb_vblank_create *vblank_create = (tdm_thread_cb_vblank_create*)base;
369                         tdm_vblank_cb_vblank_create(NULL, vblank_create->vblank_stamp);
370                         break;
371                 }
372                 case TDM_THREAD_CB_NEED_VALIDATE: {
373                         tdm_thread_cb_need_validate *ev = (tdm_thread_cb_need_validate*)base;
374                         tdm_output_cb_need_validate(ev->o);
375                         break;
376                 }
377                 default:
378                         break;
379                 }
380                 i += base->length;
381         }
382
383         _pthread_mutex_unlock(&private_display->lock);
384
385         return TDM_ERROR_NONE;
386 }
387
388 INTERN int
389 tdm_thread_in_display_thread(pid_t tid)
390 {
391         if (!keep_private_thread)
392                 return 1;
393
394         /* DON'T check TDM_MUTEX_IS_LOCKED here */
395
396         return (keep_private_thread->display_tid == tid) ? 1 : 0;
397 }
398
399 INTERN int
400 tdm_thread_is_running(void)
401 {
402         /* DON'T check TDM_MUTEX_IS_LOCKED here */
403
404         return (keep_private_thread) ? 1 : 0;
405 }