rename tdm_event to tdm_event_loop
[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 <stdlib.h>
42 #include <pthread.h>
43 #include <errno.h>
44 #include <unistd.h>
45 #include <limits.h>
46 #include <sys/types.h>
47 #include <sys/stat.h>
48 #include <fcntl.h>
49 #include <dlfcn.h>
50 #include <dirent.h>
51 #include <poll.h>
52 #include <sys/syscall.h>
53 #include <sys/types.h>
54
55 #include <tbm_bufmgr.h>
56 #include <tbm_surface_queue.h>
57
58 #include "tdm_backend.h"
59 #include "tdm_log.h"
60 #include "tdm_list.h"
61
62 #ifdef __cplusplus
63 extern "C" {
64 #endif
65
66 /**
67  * @file tdm_private.h
68  * @brief The private header file for a frontend library
69  */
70
71 extern int tdm_debug_buffer;
72 extern int tdm_debug_mutex;
73 extern int tdm_debug_thread;
74
75 #undef EXTERN
76 #undef DEPRECATED
77 #undef INTERN
78
79 #if defined(__GNUC__) && __GNUC__ >= 4
80 #define EXTERN __attribute__ ((visibility("default")))
81 #else
82 #define EXTERN
83 #endif
84
85 #if defined(__GNUC__) && __GNUC__ >= 4
86 #define INTERN __attribute__ ((visibility("hidden")))
87 #else
88 #define INTERN
89 #endif
90
91 #if defined(__GNUC__) && __GNUC__ >= 4
92 #define DEPRECATED __attribute__ ((deprecated))
93 #else
94 #define DEPRECATED
95 #endif
96
97 /* check condition */
98 #define TDM_RETURN_IF_FAIL(cond) {\
99     if (!(cond)) {\
100         TDM_ERR ("'%s' failed", #cond);\
101         return;\
102     }\
103 }
104 #define TDM_RETURN_VAL_IF_FAIL(cond, val) {\
105     if (!(cond)) {\
106         TDM_ERR ("'%s' failed", #cond);\
107         return val;\
108     }\
109 }
110 #define TDM_RETURN_VAL_IF_FAIL_WITH_ERROR(cond, error_v, val) {\
111     if (!(cond)) {\
112         TDM_ERR ("'%s' failed", #cond);\
113         ret = error_v;\
114         if (error) *error = ret;\
115         return val;\
116     }\
117 }
118
119 #define TDM_WARNING_IF_FAIL(cond)  {\
120     if (!(cond))\
121         TDM_ERR ("'%s' failed", #cond);\
122 }
123 #define TDM_GOTO_IF_FAIL(cond, dst) {\
124     if (!(cond)) {\
125         TDM_ERR ("'%s' failed", #cond);\
126         goto dst;\
127     }\
128 }
129
130 #ifdef HAVE_TTRACE
131 #include <ttrace.h>
132 #define TDM_TRACE_BEGIN(NAME) traceBegin(TTRACE_TAG_GRAPHICS, "TDM:"#NAME)
133 #define TDM_TRACE_END() traceEnd(TTRACE_TAG_GRAPHICS)
134 #else
135 #define TDM_TRACE_BEGIN(NAME)
136 #define TDM_TRACE_END()
137 #endif
138
139 #define TDM_NEVER_GET_HERE() TDM_ERR("** NEVER GET HERE **")
140
141 #define TDM_SNPRINTF(p, len, fmt, ARG...)  \
142     do { \
143         if (p && len && *len > 0) \
144         { \
145             int s = snprintf(p, *len, fmt, ##ARG); \
146             p += s; \
147             *len -= s; \
148         } \
149     } while (0)
150
151 #define C(b,m)              (((b) >> (m)) & 0xFF)
152 #define B(c,s)              ((((unsigned int)(c)) & 0xff) << (s))
153 #define FOURCC(a,b,c,d)     (B(d,24) | B(c,16) | B(b,8) | B(a,0))
154 #define FOURCC_STR(id)      C(id,0), C(id,8), C(id,16), C(id,24)
155 #define FOURCC_ID(str)      FOURCC(((char*)str)[0],((char*)str)[1],((char*)str)[2],((char*)str)[3])
156
157 typedef enum {
158         TDM_CAPTURE_TARGET_OUTPUT,
159         TDM_CAPTURE_TARGET_LAYER,
160 } tdm_capture_target;
161
162 typedef struct _tdm_private_display tdm_private_display;
163 typedef struct _tdm_private_output tdm_private_output;
164 typedef struct _tdm_private_layer tdm_private_layer;
165 typedef struct _tdm_private_pp tdm_private_pp;
166 typedef struct _tdm_private_capture tdm_private_capture;
167 typedef struct _tdm_private_loop tdm_private_loop;
168 typedef struct _tdm_private_thread tdm_private_thread;
169 typedef struct _tdm_private_vblank_handler tdm_private_vblank_handler;
170 typedef struct _tdm_private_commit_handler tdm_private_commit_handler;
171 typedef struct _tdm_private_change_handler tdm_private_change_handler;
172
173 struct _tdm_private_display {
174         pthread_mutex_t lock;
175         unsigned int init_count;
176
177         /* backend module info */
178         void *module;
179         tdm_backend_module *module_data;
180         tdm_backend_data *bdata;
181
182         /* backend function */
183         tdm_display_capability capabilities;
184         tdm_func_display func_display;
185         tdm_func_output func_output;
186         tdm_func_layer func_layer;
187         tdm_func_pp func_pp;
188         tdm_func_capture func_capture;
189
190         /* backend capability */
191         tdm_caps_display caps_display;
192         tdm_caps_pp caps_pp;
193         tdm_caps_capture caps_capture;
194
195         /* output, pp list */
196         struct list_head output_list;
197         struct list_head pp_list;
198         struct list_head capture_list;
199
200         void **outputs_ptr;
201
202         /* for event handling */
203         tdm_private_loop *private_loop;
204
205         /* for own event thread */
206         tdm_private_thread *private_thread;
207 };
208
209 struct _tdm_private_output {
210         struct list_head link;
211
212         unsigned long stamp;
213
214         tdm_private_display *private_display;
215
216         tdm_caps_output caps;
217         tdm_output *output_backend;
218
219         unsigned int pipe;
220
221         int regist_vblank_cb;
222         int regist_commit_cb;
223         int regist_change_cb;
224
225         struct list_head layer_list;
226         struct list_head capture_list;
227         struct list_head vblank_handler_list;
228         struct list_head commit_handler_list;
229         struct list_head change_handler_list;
230
231         void **layers_ptr;
232 };
233
234 struct _tdm_private_layer {
235         struct list_head link;
236
237         tdm_private_display *private_display;
238         tdm_private_output *private_output;
239
240         tdm_caps_layer caps;
241         tdm_layer *layer_backend;
242
243         tbm_surface_h pending_buffer;
244         tbm_surface_h waiting_buffer;
245         tbm_surface_h showing_buffer;
246         tbm_surface_queue_h buffer_queue;
247
248         struct list_head capture_list;
249
250         unsigned int usable;
251 };
252
253 struct _tdm_private_pp {
254         struct list_head link;
255
256         unsigned long stamp;
257
258         tdm_private_display *private_display;
259
260         tdm_pp *pp_backend;
261
262         struct list_head src_pending_buffer_list;
263         struct list_head dst_pending_buffer_list;
264         struct list_head src_buffer_list;
265         struct list_head dst_buffer_list;
266 };
267
268 struct _tdm_private_capture {
269         struct list_head link;
270         struct list_head display_link;
271
272         unsigned long stamp;
273
274         tdm_capture_target target;
275
276         tdm_private_display *private_display;
277         tdm_private_output *private_output;
278         tdm_private_layer *private_layer;
279
280         tdm_capture *capture_backend;
281
282         struct list_head pending_buffer_list;
283         struct list_head buffer_list;
284 };
285
286 struct _tdm_private_vblank_handler {
287         struct list_head link;
288
289         tdm_private_output *private_output;
290         tdm_output_vblank_handler func;
291         void *user_data;
292 };
293
294 struct _tdm_private_commit_handler {
295         struct list_head link;
296
297         tdm_private_output *private_output;
298         tdm_output_commit_handler func;
299         void *user_data;
300 };
301
302 struct _tdm_private_change_handler {
303         struct list_head link;
304
305         tdm_private_output *private_output;
306         tdm_output_change_handler func;
307         void *user_data;
308 };
309
310 typedef struct _tdm_buffer_info {
311         tbm_surface_h buffer;
312
313         /* ref_count for backend */
314         int backend_ref_count;
315
316         struct list_head release_funcs;
317         struct list_head destroy_funcs;
318
319         struct list_head *list;
320         struct list_head link;
321 } tdm_buffer_info;
322
323 tdm_private_output *
324 tdm_display_find_output_stamp(tdm_private_display *private_display,
325                               unsigned long stamp);
326 tdm_private_pp *
327 tdm_pp_find_stamp(tdm_private_display *private_display, unsigned long stamp);
328 tdm_private_capture *
329 tdm_capture_find_stamp(tdm_private_display *private_display, unsigned long stamp);
330
331 void
332 tdm_output_cb_vblank(tdm_output *output_backend, unsigned int sequence,
333                      unsigned int tv_sec, unsigned int tv_usec, void *user_data);
334 void
335 tdm_output_cb_commit(tdm_output *output_backend, unsigned int sequence,
336                      unsigned int tv_sec, unsigned int tv_usec, void *user_data);
337 void
338 tdm_output_cb_status(tdm_output *output_backend, tdm_output_conn_status status,
339                      void *user_data);
340 void
341 tdm_pp_cb_done(tdm_pp *pp_backend, tbm_surface_h src, tbm_surface_h dst,
342                void *user_data);
343 void
344 tdm_capture_cb_done(tdm_capture *capture_backend, tbm_surface_h buffer,
345                     void *user_data);
346
347 void
348 tdm_output_call_change_handler_internal(tdm_private_output *private_output,
349                                         tdm_output_change_type type,
350                                         tdm_value value);
351
352 tdm_private_pp *
353 tdm_pp_create_internal(tdm_private_display *private_display, tdm_error *error);
354 void
355 tdm_pp_destroy_internal(tdm_private_pp *private_pp);
356
357 tdm_private_capture *
358 tdm_capture_create_output_internal(tdm_private_output *private_output,
359                                    tdm_error *error);
360 tdm_private_capture *
361 tdm_capture_create_layer_internal(tdm_private_layer *private_layer,
362                                   tdm_error *error);
363 void
364 tdm_capture_destroy_internal(tdm_private_capture *private_capture);
365
366 /* utility buffer functions for private */
367 tdm_buffer_info*
368 tdm_buffer_get_info(tbm_surface_h buffer);
369 tbm_surface_h
370 tdm_buffer_list_get_first_entry(struct list_head *list);
371 void
372 tdm_buffer_list_dump(struct list_head *list);
373
374 /* event functions for private */
375 tdm_error
376 tdm_event_loop_init(tdm_private_display *private_display);
377 void
378 tdm_event_loop_deinit(tdm_private_display *private_display);
379 void
380 tdm_event_loop_create_backend_source(tdm_private_display *private_display);
381 int
382 tdm_event_loop_get_fd(tdm_private_display *private_display);
383 tdm_error
384 tdm_event_loop_dispatch(tdm_private_display *private_display);
385 tdm_error
386 tdm_event_loop_add_socket(tdm_private_display *private_display, const char *name);
387
388
389 typedef enum {
390         TDM_THREAD_CB_NONE,
391         TDM_THREAD_CB_OUTPUT_COMMIT,
392         TDM_THREAD_CB_OUTPUT_VBLANK,
393         TDM_THREAD_CB_OUTPUT_STATUS,
394         TDM_THREAD_CB_PP_DONE,
395         TDM_THREAD_CB_CAPTURE_DONE,
396 } tdm_thread_cb_type;
397
398 typedef struct _tdm_thread_cb_base tdm_thread_cb_base;
399 typedef struct _tdm_thread_cb_output_vblank tdm_thread_cb_output_commit;
400 typedef struct _tdm_thread_cb_output_vblank tdm_thread_cb_output_vblank;
401 typedef struct _tdm_thread_cb_output_status tdm_thread_cb_output_status;
402 typedef struct _tdm_thread_cb_pp_done tdm_thread_cb_pp_done;
403 typedef struct _tdm_thread_cb_capture_done tdm_thread_cb_capture_done;
404
405 struct _tdm_thread_cb_base {
406         tdm_thread_cb_type type;
407         unsigned int length;
408 };
409
410 struct _tdm_thread_cb_output_vblank {
411         tdm_thread_cb_base base;
412         unsigned long output_stamp;
413         unsigned int sequence;
414         unsigned int tv_sec;
415         unsigned int tv_usec;
416         void *user_data;
417 };
418
419 struct _tdm_thread_cb_output_status {
420         tdm_thread_cb_base base;
421         unsigned long output_stamp;
422         tdm_output_conn_status status;
423         void *user_data;
424 };
425
426 struct _tdm_thread_cb_pp_done {
427         tdm_thread_cb_base base;
428         unsigned long pp_stamp;
429         tbm_surface_h src;
430         tbm_surface_h dst;
431         void *user_data;
432 };
433
434 struct _tdm_thread_cb_capture_done {
435         tdm_thread_cb_base base;
436         unsigned long capture_stamp;
437         tbm_surface_h buffer;
438         void *user_data;
439 };
440
441 tdm_error
442 tdm_thread_init(tdm_private_display *private_display);
443 void
444 tdm_thread_deinit(tdm_private_display *private_display);
445 int
446 tdm_thread_get_fd(tdm_private_display *private_display);
447 tdm_error
448 tdm_thread_send_cb(tdm_private_display *private_display, tdm_thread_cb_base *base);
449 tdm_error
450 tdm_thread_handle_cb(tdm_private_display *private_display);
451 int
452 tdm_thread_in_display_thread(tdm_private_display *private_display);
453
454 unsigned long
455 tdm_helper_get_time_in_millis(void);
456
457 #define _pthread_mutex_lock(l) \
458     do {if (tdm_debug_mutex) TDM_INFO("mutex lock"); pthread_mutex_lock(l);} while (0)
459 #define _pthread_mutex_unlock(l) \
460     do {if (tdm_debug_mutex) TDM_INFO("mutex unlock"); pthread_mutex_unlock(l);} while (0)
461
462 #ifdef __cplusplus
463 }
464 #endif
465
466 #endif /* _TDM_PRIVATE_H_ */