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 **************************************************************************/
36 #ifndef _TDM_PRIVATE_H_
37 #define _TDM_PRIVATE_H_
47 #include <sys/types.h>
53 #include <sys/syscall.h>
54 #include <sys/types.h>
57 #include <tbm_bufmgr.h>
58 #include <tbm_surface_queue.h>
60 #include "tdm_backend.h"
63 #include "tdm_macro.h"
73 * @brief The private header file for a frontend library
76 extern int tdm_debug_buffer;
77 extern int tdm_debug_mutex;
78 extern int tdm_debug_thread;
82 #define TDM_TRACE_BEGIN(NAME) traceBegin(TTRACE_TAG_GRAPHICS, "TDM:"#NAME)
83 #define TDM_TRACE_END() traceEnd(TTRACE_TAG_GRAPHICS)
85 #define TDM_TRACE_BEGIN(NAME)
86 #define TDM_TRACE_END()
90 TDM_CAPTURE_TARGET_OUTPUT,
91 TDM_CAPTURE_TARGET_LAYER,
94 typedef struct _tdm_private_display tdm_private_display;
95 typedef struct _tdm_private_output tdm_private_output;
96 typedef struct _tdm_private_layer tdm_private_layer;
97 typedef struct _tdm_private_pp tdm_private_pp;
98 typedef struct _tdm_private_capture tdm_private_capture;
99 typedef struct _tdm_private_loop tdm_private_loop;
100 typedef struct _tdm_private_server tdm_private_server;
101 typedef struct _tdm_private_thread tdm_private_thread;
102 typedef struct _tdm_private_vblank_handler tdm_private_vblank_handler;
103 typedef struct _tdm_private_commit_handler tdm_private_commit_handler;
104 typedef struct _tdm_private_change_handler tdm_private_change_handler;
106 struct _tdm_private_display {
107 pthread_mutex_t lock;
108 unsigned int init_count;
110 /* backend module info */
112 tdm_backend_module *module_data;
113 tdm_backend_data *bdata;
119 /* backend function */
120 tdm_display_capability capabilities;
121 tdm_func_display func_display;
122 tdm_func_output func_output;
123 tdm_func_layer func_layer;
125 tdm_func_capture func_capture;
127 /* backend capability */
128 tdm_caps_display caps_display;
130 tdm_caps_capture caps_capture;
132 /* output, pp list */
133 struct list_head output_list;
134 struct list_head pp_list;
135 struct list_head capture_list;
139 /* for event handling */
140 tdm_private_loop *private_loop;
143 tdm_output **outputs;
146 struct _tdm_private_output {
147 struct list_head link;
151 tdm_private_display *private_display;
153 tdm_caps_output caps;
154 tdm_output *output_backend;
157 tdm_output_dpms current_dpms_value;
159 int regist_vblank_cb;
160 int regist_commit_cb;
161 int regist_change_cb;
163 struct list_head layer_list;
164 struct list_head capture_list;
165 struct list_head vblank_handler_list;
166 struct list_head commit_handler_list;
168 /* seperate list for multi-thread*/
169 struct list_head change_handler_list_main;
170 struct list_head change_handler_list_sub;
175 struct _tdm_private_layer {
176 struct list_head link;
178 tdm_private_display *private_display;
179 tdm_private_output *private_output;
182 tdm_layer *layer_backend;
184 tbm_surface_h pending_buffer;
185 tbm_surface_h waiting_buffer;
186 tbm_surface_h showing_buffer;
187 tbm_surface_queue_h buffer_queue;
189 struct list_head capture_list;
194 struct _tdm_private_pp {
195 struct list_head link;
199 tdm_private_display *private_display;
203 struct list_head pending_buffer_list;
204 struct list_head buffer_list;
209 struct _tdm_private_capture {
210 struct list_head link;
211 struct list_head display_link;
215 tdm_capture_target target;
217 tdm_private_display *private_display;
218 tdm_private_output *private_output;
219 tdm_private_layer *private_layer;
221 tdm_capture *capture_backend;
223 struct list_head pending_buffer_list;
224 struct list_head buffer_list;
230 * Note that we don't need to (un)lock mutex to use this structure. If there is
231 * no TDM thread, all TDM resources are protected by private_display's mutex.
232 * If there is a TDM thread, this struct will be used only in a TDM thread.
233 * So, we don't need to protect this structure by mutex. Not thread-safe.
235 struct _tdm_private_loop {
236 /* TDM uses wl_event_loop to handle various event sources including the TDM
239 struct wl_display *wl_display;
240 struct wl_event_loop *wl_loop;
243 tdm_event_loop_source *backend_source;
245 /* In event loop, all resources are accessed by this dpy.
247 * - DO NOT include other private structure in this structure because this
248 * struct is not protected by mutex.
252 /* for handling TDM client requests */
253 tdm_private_server *private_server;
255 /* To have a TDM event thread. If TDM_THREAD enviroment variable is not set
256 * private_thread is NULL.
258 tdm_private_thread *private_thread;
261 struct _tdm_private_vblank_handler {
262 struct list_head link;
264 tdm_private_output *private_output;
265 tdm_output_vblank_handler func;
271 struct _tdm_private_commit_handler {
272 struct list_head link;
274 tdm_private_output *private_output;
275 tdm_output_commit_handler func;
281 struct _tdm_private_change_handler {
282 struct list_head link;
284 tdm_private_output *private_output;
285 tdm_output_change_handler func;
291 typedef struct _tdm_buffer_info {
292 tbm_surface_h buffer;
294 /* ref_count for backend */
295 int backend_ref_count;
297 struct list_head release_funcs;
298 struct list_head destroy_funcs;
300 struct list_head *list;
301 struct list_head link;
305 tdm_display_check_module_abi(tdm_private_display *private_display, int abimaj, int abimin);
308 tdm_display_find_output_stamp(tdm_private_display *private_display,
309 unsigned long stamp);
311 tdm_pp_find_stamp(tdm_private_display *private_display, unsigned long stamp);
312 tdm_private_capture *
313 tdm_capture_find_stamp(tdm_private_display *private_display, unsigned long stamp);
316 tdm_output_cb_vblank(tdm_output *output_backend, unsigned int sequence,
317 unsigned int tv_sec, unsigned int tv_usec, void *user_data);
319 tdm_output_cb_commit(tdm_output *output_backend, unsigned int sequence,
320 unsigned int tv_sec, unsigned int tv_usec, void *user_data);
322 tdm_output_cb_status(tdm_output *output_backend, tdm_output_conn_status status,
325 tdm_pp_cb_done(tdm_pp *pp_backend, tbm_surface_h src, tbm_surface_h dst,
328 tdm_capture_cb_done(tdm_capture *capture_backend, tbm_surface_h buffer,
332 tdm_output_call_change_handler_internal(tdm_private_output *private_output,
333 struct list_head *change_handler_list,
334 tdm_output_change_type type,
336 int no_check_thread_id);
339 tdm_pp_create_internal(tdm_private_display *private_display, tdm_error *error);
341 tdm_pp_destroy_internal(tdm_private_pp *private_pp);
343 tdm_private_capture *
344 tdm_capture_create_output_internal(tdm_private_output *private_output,
346 tdm_private_capture *
347 tdm_capture_create_layer_internal(tdm_private_layer *private_layer,
350 tdm_capture_destroy_internal(tdm_private_capture *private_capture);
352 /* utility buffer functions for private */
354 tdm_buffer_get_info(tbm_surface_h buffer);
356 tdm_buffer_list_get_first_entry(struct list_head *list);
358 tdm_buffer_list_dump(struct list_head *list);
360 /* event functions for private */
362 tdm_event_loop_init(tdm_private_display *private_display);
364 tdm_event_loop_deinit(tdm_private_display *private_display);
366 tdm_event_loop_create_backend_source(tdm_private_display *private_display);
368 tdm_event_loop_get_fd(tdm_private_display *private_display);
370 tdm_event_loop_dispatch(tdm_private_display *private_display);
372 tdm_event_loop_flush(tdm_private_display *private_display);
376 TDM_THREAD_CB_OUTPUT_COMMIT,
377 TDM_THREAD_CB_OUTPUT_VBLANK,
378 TDM_THREAD_CB_OUTPUT_STATUS,
379 TDM_THREAD_CB_PP_DONE,
380 TDM_THREAD_CB_CAPTURE_DONE,
381 } tdm_thread_cb_type;
383 typedef struct _tdm_thread_cb_base tdm_thread_cb_base;
384 typedef struct _tdm_thread_cb_output_vblank tdm_thread_cb_output_commit;
385 typedef struct _tdm_thread_cb_output_vblank tdm_thread_cb_output_vblank;
386 typedef struct _tdm_thread_cb_output_status tdm_thread_cb_output_status;
387 typedef struct _tdm_thread_cb_pp_done tdm_thread_cb_pp_done;
388 typedef struct _tdm_thread_cb_capture_done tdm_thread_cb_capture_done;
390 struct _tdm_thread_cb_base {
391 tdm_thread_cb_type type;
395 struct _tdm_thread_cb_output_vblank {
396 tdm_thread_cb_base base;
397 unsigned long output_stamp;
398 unsigned int sequence;
400 unsigned int tv_usec;
404 struct _tdm_thread_cb_output_status {
405 tdm_thread_cb_base base;
406 unsigned long output_stamp;
407 tdm_output_conn_status status;
411 struct _tdm_thread_cb_pp_done {
412 tdm_thread_cb_base base;
413 unsigned long pp_stamp;
419 struct _tdm_thread_cb_capture_done {
420 tdm_thread_cb_base base;
421 unsigned long capture_stamp;
422 tbm_surface_h buffer;
427 tdm_thread_init(tdm_private_loop *private_loop);
429 tdm_thread_deinit(tdm_private_loop *private_loop);
431 tdm_thread_get_fd(tdm_private_loop *private_loop);
433 tdm_thread_send_cb(tdm_private_loop *private_loop, tdm_thread_cb_base *base);
435 tdm_thread_handle_cb(tdm_private_loop *private_loop);
437 tdm_thread_in_display_thread(pid_t tid);
439 tdm_thread_is_running(void);
442 tdm_server_init(tdm_private_loop *private_loop);
444 tdm_server_deinit(tdm_private_loop *private_loop);
447 tdm_helper_get_time_in_millis(void);
449 tdm_helper_get_time_in_micros(void);
451 extern pthread_mutex_t tdm_mutex_check_lock;
452 extern int tdm_mutex_locked;
453 extern int tdm_dump_enable;
455 #define _pthread_mutex_unlock(l) \
457 if (tdm_debug_mutex) \
458 TDM_INFO("mutex unlock"); \
459 pthread_mutex_lock(&tdm_mutex_check_lock); \
460 tdm_mutex_locked = 0; \
461 pthread_mutex_unlock(&tdm_mutex_check_lock); \
462 pthread_mutex_unlock(l); \
464 #ifdef TDM_CONFIG_MUTEX_TIMEOUT
465 #define MUTEX_TIMEOUT_SEC 5
466 #define _pthread_mutex_lock(l) \
468 if (tdm_debug_mutex) \
469 TDM_INFO("mutex lock"); \
470 struct timespec rtime; \
471 clock_gettime(CLOCK_REALTIME, &rtime); \
472 rtime.tv_sec += MUTEX_TIMEOUT_SEC; \
473 if (pthread_mutex_timedlock(l, &rtime)) { \
474 TDM_ERR("Mutex lock failed PID %d", getpid()); \
475 _pthread_mutex_unlock(l); \
478 pthread_mutex_lock(&tdm_mutex_check_lock); \
479 tdm_mutex_locked = 1; \
480 pthread_mutex_unlock(&tdm_mutex_check_lock); \
483 #else //TDM_CONFIG_MUTEX_TIMEOUT
484 #define _pthread_mutex_lock(l) \
486 if (tdm_debug_mutex) \
487 TDM_INFO("mutex lock"); \
488 pthread_mutex_lock(l); \
489 pthread_mutex_lock(&tdm_mutex_check_lock); \
490 tdm_mutex_locked = 1; \
491 pthread_mutex_unlock(&tdm_mutex_check_lock); \
493 #endif //TDM_CONFIG_MUTEX_TIMEOUT
494 //#define TDM_MUTEX_IS_LOCKED() (tdm_mutex_locked == 1)
495 static inline int TDM_MUTEX_IS_LOCKED(void)
498 pthread_mutex_lock(&tdm_mutex_check_lock);
499 ret = (tdm_mutex_locked == 1);
500 pthread_mutex_unlock(&tdm_mutex_check_lock);
505 _tdm_display_lock(tdm_display *dpy, const char *func);
507 _tdm_display_unlock(tdm_display *dpy, const char *func);
509 #define tdm_display_lock(dpy) _tdm_display_lock(dpy, __FUNCTION__)
510 #define tdm_display_unlock(dpy) _tdm_display_unlock(dpy, __FUNCTION__)
513 tdm_display_update_output(tdm_private_display *private_display,
514 tdm_output *output_backend, int pipe);
518 * @brief The tdm vblank object
520 typedef void tdm_vblank;
522 typedef void (*tdm_vblank_handler)(tdm_vblank *vblank, tdm_error error, unsigned int sequence,
523 unsigned int tv_sec, unsigned int tv_usec, void *user_data);
526 tdm_vblank_create(tdm_display *dpy, tdm_output *output, tdm_error *error);
528 tdm_vblank_destroy(tdm_vblank *vblank);
530 tdm_vblank_set_fps(tdm_vblank *vblank, unsigned int fps);
532 tdm_vblank_set_offset(tdm_vblank *vblank, int offset);
534 tdm_vblank_set_enable_fake(tdm_vblank *vblank, unsigned int enable_fake);
536 tdm_vblank_wait(tdm_vblank *vblank, unsigned int req_sec, unsigned int req_usec, unsigned int interval, tdm_vblank_handler func, void *user_data);
542 #endif /* _TDM_PRIVATE_H_ */