add tdm_common.h to share enumeration and structure with client
[platform/core/uifw/libtdm.git] / src / tdm_private.h
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 #ifndef _TDM_PRIVATE_H_
37 #define _TDM_PRIVATE_H_
38
39 #include <stdio.h>
40 #include <string.h>
41 #include <strings.h>
42 #include <stdlib.h>
43 #include <pthread.h>
44 #include <errno.h>
45 #include <unistd.h>
46 #include <limits.h>
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #include <fcntl.h>
50 #include <dlfcn.h>
51 #include <dirent.h>
52 #include <poll.h>
53 #include <sys/syscall.h>
54 #include <sys/types.h>
55
56 #include <tbm_bufmgr.h>
57 #include <tbm_surface_queue.h>
58
59 #include "tdm_backend.h"
60 #include "tdm_log.h"
61 #include "tdm_list.h"
62 #include "tdm_macro.h"
63
64 #ifdef __cplusplus
65 extern "C" {
66 #endif
67
68 //#define INIT_BUFMGR
69
70 /**
71  * @file tdm_private.h
72  * @brief The private header file for a frontend library
73  */
74
75 extern int tdm_debug_buffer;
76 extern int tdm_debug_mutex;
77 extern int tdm_debug_thread;
78
79 #ifdef HAVE_TTRACE
80 #include <ttrace.h>
81 #define TDM_TRACE_BEGIN(NAME) traceBegin(TTRACE_TAG_GRAPHICS, "TDM:"#NAME)
82 #define TDM_TRACE_END() traceEnd(TTRACE_TAG_GRAPHICS)
83 #else
84 #define TDM_TRACE_BEGIN(NAME)
85 #define TDM_TRACE_END()
86 #endif
87
88 typedef enum {
89         TDM_CAPTURE_TARGET_OUTPUT,
90         TDM_CAPTURE_TARGET_LAYER,
91 } tdm_capture_target;
92
93 typedef struct _tdm_private_display tdm_private_display;
94 typedef struct _tdm_private_output tdm_private_output;
95 typedef struct _tdm_private_layer tdm_private_layer;
96 typedef struct _tdm_private_pp tdm_private_pp;
97 typedef struct _tdm_private_capture tdm_private_capture;
98 typedef struct _tdm_private_loop tdm_private_loop;
99 typedef struct _tdm_private_server tdm_private_server;
100 typedef struct _tdm_private_thread tdm_private_thread;
101 typedef struct _tdm_private_vblank_handler tdm_private_vblank_handler;
102 typedef struct _tdm_private_commit_handler tdm_private_commit_handler;
103 typedef struct _tdm_private_change_handler tdm_private_change_handler;
104
105 struct _tdm_private_display {
106         pthread_mutex_t lock;
107         unsigned int init_count;
108
109         /* backend module info */
110         void *module;
111         tdm_backend_module *module_data;
112         tdm_backend_data *bdata;
113
114 #ifdef INIT_BUFMGR
115         tbm_bufmgr bufmgr;
116 #endif
117
118         /* backend function */
119         tdm_display_capability capabilities;
120         tdm_func_display func_display;
121         tdm_func_output func_output;
122         tdm_func_layer func_layer;
123         tdm_func_pp func_pp;
124         tdm_func_capture func_capture;
125
126         /* backend capability */
127         tdm_caps_display caps_display;
128         tdm_caps_pp caps_pp;
129         tdm_caps_capture caps_capture;
130
131         /* output, pp list */
132         struct list_head output_list;
133         struct list_head pp_list;
134         struct list_head capture_list;
135
136         void **outputs_ptr;
137
138         /* for event handling */
139         tdm_private_loop *private_loop;
140
141         /* output order */
142         tdm_output **outputs;
143 };
144
145 struct _tdm_private_output {
146         struct list_head link;
147
148         unsigned long stamp;
149
150         tdm_private_display *private_display;
151
152         tdm_caps_output caps;
153         tdm_output *output_backend;
154
155         unsigned int pipe;
156         tdm_output_dpms current_dpms_value;
157
158         int regist_vblank_cb;
159         int regist_commit_cb;
160         int regist_change_cb;
161
162         struct list_head layer_list;
163         struct list_head capture_list;
164         struct list_head vblank_handler_list;
165         struct list_head commit_handler_list;
166
167         /* seperate list for multi-thread*/
168         struct list_head change_handler_list_main;
169         struct list_head change_handler_list_sub;
170
171         void **layers_ptr;
172 };
173
174 struct _tdm_private_layer {
175         struct list_head link;
176
177         tdm_private_display *private_display;
178         tdm_private_output *private_output;
179
180         tdm_caps_layer caps;
181         tdm_layer *layer_backend;
182
183         tbm_surface_h pending_buffer;
184         tbm_surface_h waiting_buffer;
185         tbm_surface_h showing_buffer;
186         tbm_surface_queue_h buffer_queue;
187
188         struct list_head capture_list;
189
190         unsigned int usable;
191 };
192
193 struct _tdm_private_pp {
194         struct list_head link;
195
196         unsigned long stamp;
197
198         tdm_private_display *private_display;
199
200         tdm_pp *pp_backend;
201
202         struct list_head pending_buffer_list;
203         struct list_head buffer_list;
204
205         pid_t owner_tid;
206 };
207
208 struct _tdm_private_capture {
209         struct list_head link;
210         struct list_head display_link;
211
212         unsigned long stamp;
213
214         tdm_capture_target target;
215
216         tdm_private_display *private_display;
217         tdm_private_output *private_output;
218         tdm_private_layer *private_layer;
219
220         tdm_capture *capture_backend;
221
222         struct list_head pending_buffer_list;
223         struct list_head buffer_list;
224
225         pid_t owner_tid;
226 };
227
228 /* CAUTION:
229  * Note that we don't need to (un)lock mutex to use this structure. If there is
230  * no TDM thread, all TDM resources are protected by private_display's mutex.
231  * If there is a TDM thread, this struct will be used only in a TDM thread.
232  * So, we don't need to protect this structure by mutex. Not thread-safe.
233  */
234 struct _tdm_private_loop {
235         /* TDM uses wl_event_loop to handle various event sources including the TDM
236          * backend's fd.
237          */
238         struct wl_display *wl_display;
239         struct wl_event_loop *wl_loop;
240
241         int backend_fd;
242         tdm_event_loop_source *backend_source;
243
244         /* In event loop, all resources are accessed by this dpy.
245          * CAUTION:
246          * - DO NOT include other private structure in this structure because this
247          *   struct is not protected by mutex.
248          */
249         tdm_display *dpy;
250
251         /* for handling TDM client requests */
252         tdm_private_server *private_server;
253
254         /* To have a TDM event thread. If TDM_THREAD enviroment variable is not set
255          * private_thread is NULL.
256          */
257         tdm_private_thread *private_thread;
258 };
259
260 struct _tdm_private_vblank_handler {
261         struct list_head link;
262
263         tdm_private_output *private_output;
264         tdm_output_vblank_handler func;
265         void *user_data;
266
267         pid_t owner_tid;
268 };
269
270 struct _tdm_private_commit_handler {
271         struct list_head link;
272
273         tdm_private_output *private_output;
274         tdm_output_commit_handler func;
275         void *user_data;
276
277         pid_t owner_tid;
278 };
279
280 struct _tdm_private_change_handler {
281         struct list_head link;
282
283         tdm_private_output *private_output;
284         tdm_output_change_handler func;
285         void *user_data;
286
287         pid_t owner_tid;
288 };
289
290 typedef struct _tdm_buffer_info {
291         tbm_surface_h buffer;
292
293         /* ref_count for backend */
294         int backend_ref_count;
295
296         struct list_head release_funcs;
297         struct list_head destroy_funcs;
298
299         struct list_head *list;
300         struct list_head link;
301 } tdm_buffer_info;
302
303 int
304 tdm_display_check_module_abi(tdm_private_display *private_display, int abimaj, int abimin);
305
306 tdm_private_output *
307 tdm_display_find_output_stamp(tdm_private_display *private_display,
308                                                           unsigned long stamp);
309 tdm_private_pp *
310 tdm_pp_find_stamp(tdm_private_display *private_display, unsigned long stamp);
311 tdm_private_capture *
312 tdm_capture_find_stamp(tdm_private_display *private_display, unsigned long stamp);
313
314 void
315 tdm_output_cb_vblank(tdm_output *output_backend, unsigned int sequence,
316                                          unsigned int tv_sec, unsigned int tv_usec, void *user_data);
317 void
318 tdm_output_cb_commit(tdm_output *output_backend, unsigned int sequence,
319                                          unsigned int tv_sec, unsigned int tv_usec, void *user_data);
320 void
321 tdm_output_cb_status(tdm_output *output_backend, tdm_output_conn_status status,
322                                          void *user_data);
323 void
324 tdm_pp_cb_done(tdm_pp *pp_backend, tbm_surface_h src, tbm_surface_h dst,
325                            void *user_data);
326 void
327 tdm_capture_cb_done(tdm_capture *capture_backend, tbm_surface_h buffer,
328                                         void *user_data);
329
330 void
331 tdm_output_call_change_handler_internal(tdm_private_output *private_output,
332                                                                                 struct list_head *change_handler_list,
333                                                                                 tdm_output_change_type type,
334                                                                                 tdm_value value);
335
336 tdm_private_pp *
337 tdm_pp_create_internal(tdm_private_display *private_display, tdm_error *error);
338 void
339 tdm_pp_destroy_internal(tdm_private_pp *private_pp);
340
341 tdm_private_capture *
342 tdm_capture_create_output_internal(tdm_private_output *private_output,
343                                                                    tdm_error *error);
344 tdm_private_capture *
345 tdm_capture_create_layer_internal(tdm_private_layer *private_layer,
346                                                                   tdm_error *error);
347 void
348 tdm_capture_destroy_internal(tdm_private_capture *private_capture);
349
350 /* utility buffer functions for private */
351 tdm_buffer_info*
352 tdm_buffer_get_info(tbm_surface_h buffer);
353 tbm_surface_h
354 tdm_buffer_list_get_first_entry(struct list_head *list);
355 void
356 tdm_buffer_list_dump(struct list_head *list);
357
358 /* event functions for private */
359 tdm_error
360 tdm_event_loop_init(tdm_private_display *private_display);
361 void
362 tdm_event_loop_deinit(tdm_private_display *private_display);
363 void
364 tdm_event_loop_create_backend_source(tdm_private_display *private_display);
365 int
366 tdm_event_loop_get_fd(tdm_private_display *private_display);
367 tdm_error
368 tdm_event_loop_dispatch(tdm_private_display *private_display);
369 void
370 tdm_event_loop_flush(tdm_private_display *private_display);
371
372 typedef enum {
373         TDM_THREAD_CB_NONE,
374         TDM_THREAD_CB_OUTPUT_COMMIT,
375         TDM_THREAD_CB_OUTPUT_VBLANK,
376         TDM_THREAD_CB_OUTPUT_STATUS,
377         TDM_THREAD_CB_PP_DONE,
378         TDM_THREAD_CB_CAPTURE_DONE,
379 } tdm_thread_cb_type;
380
381 typedef struct _tdm_thread_cb_base tdm_thread_cb_base;
382 typedef struct _tdm_thread_cb_output_vblank tdm_thread_cb_output_commit;
383 typedef struct _tdm_thread_cb_output_vblank tdm_thread_cb_output_vblank;
384 typedef struct _tdm_thread_cb_output_status tdm_thread_cb_output_status;
385 typedef struct _tdm_thread_cb_pp_done tdm_thread_cb_pp_done;
386 typedef struct _tdm_thread_cb_capture_done tdm_thread_cb_capture_done;
387
388 struct _tdm_thread_cb_base {
389         tdm_thread_cb_type type;
390         unsigned int length;
391 };
392
393 struct _tdm_thread_cb_output_vblank {
394         tdm_thread_cb_base base;
395         unsigned long output_stamp;
396         unsigned int sequence;
397         unsigned int tv_sec;
398         unsigned int tv_usec;
399         void *user_data;
400 };
401
402 struct _tdm_thread_cb_output_status {
403         tdm_thread_cb_base base;
404         unsigned long output_stamp;
405         tdm_output_conn_status status;
406         void *user_data;
407 };
408
409 struct _tdm_thread_cb_pp_done {
410         tdm_thread_cb_base base;
411         unsigned long pp_stamp;
412         tbm_surface_h src;
413         tbm_surface_h dst;
414         void *user_data;
415 };
416
417 struct _tdm_thread_cb_capture_done {
418         tdm_thread_cb_base base;
419         unsigned long capture_stamp;
420         tbm_surface_h buffer;
421         void *user_data;
422 };
423
424 tdm_error
425 tdm_thread_init(tdm_private_loop *private_loop);
426 void
427 tdm_thread_deinit(tdm_private_loop *private_loop);
428 int
429 tdm_thread_get_fd(tdm_private_loop *private_loop);
430 tdm_error
431 tdm_thread_send_cb(tdm_private_loop *private_loop, tdm_thread_cb_base *base);
432 tdm_error
433 tdm_thread_handle_cb(tdm_private_loop *private_loop);
434 int
435 tdm_thread_in_display_thread(pid_t tid);
436 int
437 tdm_thread_is_running(void);
438
439 tdm_error
440 tdm_server_init(tdm_private_loop *private_loop);
441 void
442 tdm_server_deinit(tdm_private_loop *private_loop);
443
444 unsigned long
445 tdm_helper_get_time_in_millis(void);
446 unsigned long
447 tdm_helper_get_time_in_micros(void);
448
449 extern pthread_mutex_t tdm_mutex_check_lock;
450 extern int tdm_mutex_locked;
451 extern int tdm_dump_enable;
452
453 #define _pthread_mutex_unlock(l) \
454         do { \
455                 if (tdm_debug_mutex) \
456                         TDM_INFO("mutex unlock"); \
457                 pthread_mutex_lock(&tdm_mutex_check_lock); \
458                 tdm_mutex_locked = 0; \
459                 pthread_mutex_unlock(&tdm_mutex_check_lock); \
460                 pthread_mutex_unlock(l); \
461         } while (0)
462 #ifdef TDM_CONFIG_MUTEX_TIMEOUT
463 #define MUTEX_TIMEOUT_SEC 5
464 #define _pthread_mutex_lock(l) \
465         do { \
466                 if (tdm_debug_mutex) \
467                         TDM_INFO("mutex lock"); \
468                 struct timespec rtime; \
469                 clock_gettime(CLOCK_REALTIME, &rtime); \
470                 rtime.tv_sec += MUTEX_TIMEOUT_SEC; \
471                 if (pthread_mutex_timedlock(l, &rtime)) { \
472                         TDM_ERR("Mutex lock failed PID %d", getpid()); \
473                         _pthread_mutex_unlock(l); \
474                 } \
475                 else { \
476                         pthread_mutex_lock(&tdm_mutex_check_lock); \
477                         tdm_mutex_locked = 1; \
478                         pthread_mutex_unlock(&tdm_mutex_check_lock); \
479                 } \
480         } while (0)
481 #else //TDM_CONFIG_MUTEX_TIMEOUT
482 #define _pthread_mutex_lock(l) \
483         do { \
484                 if (tdm_debug_mutex) \
485                         TDM_INFO("mutex lock"); \
486                 pthread_mutex_lock(l); \
487                 pthread_mutex_lock(&tdm_mutex_check_lock); \
488                 tdm_mutex_locked = 1; \
489                 pthread_mutex_unlock(&tdm_mutex_check_lock); \
490         } while (0)
491 #endif //TDM_CONFIG_MUTEX_TIMEOUT
492 //#define TDM_MUTEX_IS_LOCKED() (tdm_mutex_locked == 1)
493 static inline int TDM_MUTEX_IS_LOCKED(void)
494 {
495         int ret;
496         pthread_mutex_lock(&tdm_mutex_check_lock);
497         ret = (tdm_mutex_locked == 1);
498         pthread_mutex_unlock(&tdm_mutex_check_lock);
499         return ret;
500 }
501
502 tdm_error
503 _tdm_display_lock(tdm_display *dpy, const char *func);
504 void
505 _tdm_display_unlock(tdm_display *dpy, const char *func);
506
507 #define tdm_display_lock(dpy)   _tdm_display_lock(dpy, __FUNCTION__)
508 #define tdm_display_unlock(dpy)   _tdm_display_unlock(dpy, __FUNCTION__)
509
510 tdm_error
511 tdm_display_update_output(tdm_private_display *private_display,
512                                                   tdm_output *output_backend, int pipe);
513
514 #ifdef __cplusplus
515 }
516 #endif
517
518 #endif /* _TDM_PRIVATE_H_ */