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 **************************************************************************/
40 #include "tdm_private.h"
42 static tdm_private_thread *keep_private_thread;
44 struct _tdm_private_thread {
45 tdm_private_loop *private_loop;
47 pthread_t event_thread;
52 /* 0: read, 1: write */
57 _tdm_thread_main(void *data)
59 tdm_private_thread *private_thread = (tdm_private_thread*)data;
60 tdm_private_loop *private_loop = private_thread->private_loop;
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.
69 private_thread->thread_tid = syscall(SYS_gettid);
71 TDM_INFO("display_tid:%d, thread_tid: %d",
72 private_thread->display_tid, private_thread->thread_tid);
74 fd = tdm_event_loop_get_fd(private_loop->dpy);
76 TDM_ERR("couldn't get fd");
85 if (tdm_debug_module & TDM_DEBUG_EVENT)
86 TDM_INFO("server flush");
87 tdm_event_loop_flush(private_loop->dpy);
89 if (tdm_debug_module & TDM_DEBUG_EVENT)
90 TDM_INFO("fd(%d) polling in", fd);
92 ret = poll(&fds, 1, -1);
94 if (tdm_debug_module & TDM_DEBUG_EVENT)
95 TDM_INFO("fd(%d) polling out", fd);
98 if (errno == EINTR || errno == EAGAIN) /* normal case */
101 TDM_ERR("poll failed: %m");
106 if (tdm_debug_module & TDM_DEBUG_EVENT)
107 TDM_INFO("thread got events");
109 if (tdm_event_loop_dispatch(private_loop->dpy) < 0)
110 TDM_ERR("dispatch error");
117 /* NOTE: tdm thread doesn't care about multi-thread. */
119 tdm_thread_init(tdm_private_loop *private_loop)
121 tdm_private_display *private_display;
122 tdm_private_thread *private_thread;
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);
128 private_display = private_loop->dpy;
129 TDM_RETURN_VAL_IF_FAIL(private_display->private_loop, TDM_ERROR_OPERATION_FAILED);
131 if (private_loop->private_thread)
132 return TDM_ERROR_NONE;
134 /* enable as default */
135 thread = tdm_config_get_int(TDM_CONFIG_KEY_GENERAL_THREAD, 1);
137 TDM_INFO("not using a TDM event thread");
138 return TDM_ERROR_NONE;
141 private_thread = calloc(1, sizeof * private_thread);
142 if (!private_thread) {
143 TDM_ERR("alloc failed");
144 return TDM_ERROR_OUT_OF_MEMORY;
147 if (pipe(private_thread->pipe) != 0) {
148 TDM_ERR("socketpair failed: %m");
149 free(private_thread);
150 return TDM_ERROR_OPERATION_FAILED;
153 private_thread->private_loop = private_loop;
154 private_loop->private_thread = private_thread;
156 private_thread->display_tid = syscall(SYS_gettid);
158 pthread_create(&private_thread->event_thread, NULL, _tdm_thread_main,
161 keep_private_thread = private_thread;
163 TDM_INFO("using a TDM event thread. pipe(%d,%d)",
164 private_thread->pipe[0], private_thread->pipe[1]);
166 return TDM_ERROR_NONE;
170 tdm_thread_deinit(tdm_private_loop *private_loop)
172 tdm_private_display *private_display;
174 TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
176 if (!private_loop->private_thread)
179 pthread_cancel(private_loop->private_thread->event_thread);
181 private_display = private_loop->dpy;
183 /* before falling into the block of pthread_join, we have to unlock the mutex
184 * for subthread to use the mutex.
186 _pthread_mutex_unlock(&private_display->lock);
187 pthread_join(private_loop->private_thread->event_thread, NULL);
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]);
194 free(private_loop->private_thread);
195 private_loop->private_thread = NULL;
196 keep_private_thread = NULL;
198 TDM_INFO("Finish a TDM event thread");
202 tdm_thread_get_fd(tdm_private_loop *private_loop)
204 tdm_private_thread *private_thread;
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);
210 private_thread = private_loop->private_thread;
212 return private_thread->pipe[0];
216 tdm_thread_send_cb(tdm_private_loop *private_loop, tdm_thread_cb_base *base)
218 tdm_private_thread *private_thread;
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);
226 private_thread = private_loop->private_thread;
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);
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;
238 return TDM_ERROR_NONE;
242 tdm_thread_handle_cb(tdm_private_loop *private_loop)
244 tdm_private_display *private_display;
245 tdm_private_thread *private_thread;
246 tdm_thread_cb_base *base;
251 /* DON'T check TDM_MUTEX_IS_LOCKED here */
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);
256 private_thread = private_loop->private_thread;
257 private_display = private_loop->dpy;
260 len = read(private_thread->pipe[0], buffer, sizeof buffer);
261 } while (len < 0 && errno == EINTR);
263 if (tdm_debug_module & TDM_DEBUG_THREAD)
264 TDM_INFO("fd(%d) read length(%d)", private_thread->pipe[0], len);
267 TDM_ERR("read failed: errno(%d), len(%d) %m", errno, len);
268 return TDM_ERROR_OPERATION_FAILED;
272 return TDM_ERROR_NONE;
274 if (len < sizeof * base) {
275 TDM_ERR("read failed: len(%d)", len);
276 return TDM_ERROR_OPERATION_FAILED;
279 _pthread_mutex_lock(&private_display->lock);
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);
295 tdm_output_cb_commit(output_backend, output_commit->sequence,
296 output_commit->tv_sec, output_commit->tv_usec,
297 output_commit->user_data);
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);
308 tdm_output_cb_vblank(output_backend, output_vblank->sequence,
309 output_vblank->tv_sec, output_vblank->tv_usec,
310 output_vblank->user_data);
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);
322 tdm_output_cb_status(output_backend, output_status->status,
323 output_status->user_data);
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);
336 tdm_output_cb_dpms(output_backend, output_dpms->dpms, output_dpms->user_data);
340 case TDM_THREAD_CB_PP_DONE: {
341 tdm_thread_cb_pp_done *pp_done = (tdm_thread_cb_pp_done*)base;
343 tdm_pp_find_stamp(private_loop->dpy, pp_done->pp_stamp);
345 TDM_WRN("no pp(%f)", pp_done->pp_stamp);
348 tdm_pp_cb_done(pp_backend, pp_done->src, pp_done->dst, pp_done->user_data);
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);
359 tdm_capture_cb_done(capture_backend, capture_done->buffer, capture_done->user_data);
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);
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);
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);
383 _pthread_mutex_unlock(&private_display->lock);
385 return TDM_ERROR_NONE;
389 tdm_thread_in_display_thread(pid_t tid)
391 if (!keep_private_thread)
394 /* DON'T check TDM_MUTEX_IS_LOCKED here */
396 return (keep_private_thread->display_tid == tid) ? 1 : 0;
400 tdm_thread_is_running(void)
402 /* DON'T check TDM_MUTEX_IS_LOCKED here */
404 return (keep_private_thread) ? 1 : 0;