use wl_event_loop to support the event handling system
[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
52 #include <tbm_bufmgr.h>
53 #include <tbm_surface_queue.h>
54 #include <wayland-server-core.h>
55
56 #include "tdm_backend.h"
57 #include "tdm_log.h"
58 #include "tdm_list.h"
59
60 #ifdef __cplusplus
61 extern "C" {
62 #endif
63
64 /**
65  * @file tdm_private.h
66  * @brief The private header file for a frontend library
67  */
68
69 extern int tdm_debug_buffer;
70 extern int tdm_debug_mutex;
71
72 #undef EXTERN
73 #undef DEPRECATED
74 #undef INTERN
75
76 #if defined(__GNUC__) && __GNUC__ >= 4
77 #define EXTERN __attribute__ ((visibility("default")))
78 #else
79 #define EXTERN
80 #endif
81
82 #if defined(__GNUC__) && __GNUC__ >= 4
83 #define INTERN __attribute__ ((visibility("hidden")))
84 #else
85 #define INTERN
86 #endif
87
88 #if defined(__GNUC__) && __GNUC__ >= 4
89 #define DEPRECATED __attribute__ ((deprecated))
90 #else
91 #define DEPRECATED
92 #endif
93
94 /* check condition */
95 #define TDM_RETURN_IF_FAIL(cond) {\
96     if (!(cond)) {\
97         TDM_ERR ("'%s' failed", #cond);\
98         return;\
99     }\
100 }
101 #define TDM_RETURN_VAL_IF_FAIL(cond, val) {\
102     if (!(cond)) {\
103         TDM_ERR ("'%s' failed", #cond);\
104         return val;\
105     }\
106 }
107 #define TDM_RETURN_VAL_IF_FAIL_WITH_ERROR(cond, error_v, val) {\
108     if (!(cond)) {\
109         TDM_ERR ("'%s' failed", #cond);\
110         ret = error_v;\
111         if (error) *error = ret;\
112         return val;\
113     }\
114 }
115
116 #define TDM_WARNING_IF_FAIL(cond)  {\
117     if (!(cond))\
118         TDM_ERR ("'%s' failed", #cond);\
119 }
120 #define TDM_GOTO_IF_FAIL(cond, dst) {\
121     if (!(cond)) {\
122         TDM_ERR ("'%s' failed", #cond);\
123         goto dst;\
124     }\
125 }
126
127 #ifdef HAVE_TTRACE
128 #include <ttrace.h>
129 #define TDM_TRACE_BEGIN(NAME) traceBegin(TTRACE_TAG_GRAPHICS, "TDM:"#NAME)
130 #define TDM_TRACE_END() traceEnd(TTRACE_TAG_GRAPHICS)
131 #else
132 #define TDM_TRACE_BEGIN(NAME)
133 #define TDM_TRACE_END()
134 #endif
135
136 #define TDM_NEVER_GET_HERE() TDM_ERR("** NEVER GET HERE **")
137
138 #define TDM_SNPRINTF(p, len, fmt, ARG...)  \
139     do { \
140         if (p && len && *len > 0) \
141         { \
142             int s = snprintf(p, *len, fmt, ##ARG); \
143             p += s; \
144             *len -= s; \
145         } \
146     } while (0)
147
148 #define C(b,m)              (((b) >> (m)) & 0xFF)
149 #define B(c,s)              ((((unsigned int)(c)) & 0xff) << (s))
150 #define FOURCC(a,b,c,d)     (B(d,24) | B(c,16) | B(b,8) | B(a,0))
151 #define FOURCC_STR(id)      C(id,0), C(id,8), C(id,16), C(id,24)
152 #define FOURCC_ID(str)      FOURCC(((char*)str)[0],((char*)str)[1],((char*)str)[2],((char*)str)[3])
153
154 typedef enum {
155         TDM_CAPTURE_TARGET_OUTPUT,
156         TDM_CAPTURE_TARGET_LAYER,
157 } tdm_capture_target;
158
159 typedef struct _tdm_private_display tdm_private_display;
160 typedef struct _tdm_private_output tdm_private_output;
161 typedef struct _tdm_private_layer tdm_private_layer;
162 typedef struct _tdm_private_pp tdm_private_pp;
163 typedef struct _tdm_private_capture tdm_private_capture;
164 typedef struct _tdm_private_vblank_handler tdm_private_vblank_handler;
165 typedef struct _tdm_private_commit_handler tdm_private_commit_handler;
166
167 struct _tdm_private_display {
168         pthread_mutex_t lock;
169         unsigned int init_count;
170
171         /* backend module info */
172         void *module;
173         tdm_backend_module *module_data;
174         tdm_backend_data *bdata;
175
176         /* backend function */
177         tdm_display_capability capabilities;
178         tdm_func_display func_display;
179         tdm_func_output func_output;
180         tdm_func_layer func_layer;
181         tdm_func_pp func_pp;
182         tdm_func_capture func_capture;
183
184         /* backend capability */
185         tdm_caps_display caps_display;
186         tdm_caps_pp caps_pp;
187         tdm_caps_capture caps_capture;
188
189         /* output, pp list */
190         struct list_head output_list;
191         struct list_head pp_list;
192
193         void **outputs_ptr;
194
195         /* for event handling */
196         struct wl_event_loop *event_loop;
197         tdm_event_source *main_source;
198 };
199
200 struct _tdm_private_output {
201         struct list_head link;
202
203         tdm_private_display *private_display;
204
205         tdm_caps_output caps;
206         tdm_output *output_backend;
207
208         unsigned int pipe;
209
210         int regist_vblank_cb;
211         int regist_commit_cb;
212
213         struct list_head layer_list;
214         struct list_head capture_list;
215         struct list_head vblank_handler_list;
216         struct list_head commit_handler_list;
217
218         void **layers_ptr;
219 };
220
221 struct _tdm_private_layer {
222         struct list_head link;
223
224         tdm_private_display *private_display;
225         tdm_private_output *private_output;
226
227         tdm_caps_layer caps;
228         tdm_layer *layer_backend;
229
230         tbm_surface_h pending_buffer;
231         tbm_surface_h waiting_buffer;
232         tbm_surface_h showing_buffer;
233         tbm_surface_queue_h buffer_queue;
234
235         struct list_head capture_list;
236
237         unsigned int usable;
238 };
239
240 struct _tdm_private_pp {
241         struct list_head link;
242
243         tdm_private_display *private_display;
244
245         tdm_pp *pp_backend;
246
247         struct list_head src_pending_buffer_list;
248         struct list_head dst_pending_buffer_list;
249         struct list_head src_buffer_list;
250         struct list_head dst_buffer_list;
251 };
252
253 struct _tdm_private_capture {
254         struct list_head link;
255
256         tdm_capture_target target;
257
258         tdm_private_display *private_display;
259         tdm_private_output *private_output;
260         tdm_private_layer *private_layer;
261
262         tdm_capture *capture_backend;
263
264         struct list_head pending_buffer_list;
265         struct list_head buffer_list;
266 };
267
268 struct _tdm_private_vblank_handler {
269         struct list_head link;
270
271         tdm_private_output *private_output;
272         tdm_output_vblank_handler func;
273         void *user_data;
274 };
275
276 struct _tdm_private_commit_handler {
277         struct list_head link;
278
279         tdm_private_output *private_output;
280         tdm_output_commit_handler func;
281         void *user_data;
282 };
283
284 typedef struct _tdm_buffer_info {
285         tbm_surface_h buffer;
286
287         /* ref_count for backend */
288         int backend_ref_count;
289
290         struct list_head release_funcs;
291         struct list_head destroy_funcs;
292
293         struct list_head *list;
294         struct list_head link;
295 } tdm_buffer_info;
296
297 tdm_private_pp *
298 tdm_pp_create_internal(tdm_private_display *private_display, tdm_error *error);
299 void
300 tdm_pp_destroy_internal(tdm_private_pp *private_pp);
301
302 tdm_private_capture *
303 tdm_capture_create_output_internal(tdm_private_output *private_output,
304                                    tdm_error *error);
305 tdm_private_capture *
306 tdm_capture_create_layer_internal(tdm_private_layer *private_layer,
307                                   tdm_error *error);
308 void
309 tdm_capture_destroy_internal(tdm_private_capture *private_capture);
310
311 /* utility buffer functions for private */
312 tdm_buffer_info*
313 tdm_buffer_get_info(tbm_surface_h buffer);
314 tbm_surface_h
315 tdm_buffer_list_get_first_entry(struct list_head *list);
316 void
317 tdm_buffer_list_dump(struct list_head *list);
318
319 /* event functions for private */
320 tdm_error
321 tdm_event_init(tdm_private_display *private_display);
322 void
323 tdm_event_deinit(tdm_private_display *private_display);
324 void
325 tdm_event_create_main_source(tdm_private_display *private_display);
326 int
327 tdm_event_get_fd(tdm_private_display *private_display);
328 tdm_error
329 tdm_event_dispatch(tdm_private_display *private_display);
330
331 #define _pthread_mutex_lock(l) \
332     do {if (tdm_debug_mutex) TDM_INFO("mutex lock"); pthread_mutex_lock(l);} while (0)
333 #define _pthread_mutex_unlock(l) \
334     do {if (tdm_debug_mutex) TDM_INFO("mutex unlock"); pthread_mutex_unlock(l);} while (0)
335
336 #ifdef __cplusplus
337 }
338 #endif
339
340 #endif /* _TDM_PRIVATE_H_ */