bufmgr: add tbm_bufmgr_server_init api
[platform/core/uifw/libtbm.git] / src / tbm_bufmgr_int.h
1 /**************************************************************************
2
3 libtbm
4
5 Copyright 2012 Samsung Electronics co., Ltd. All Rights Reserved.
6
7 Contact: SooChan Lim <sc1.lim@samsung.com>, Sangjin Lee <lsj119@samsung.com>
8 Boram Park <boram1288.park@samsung.com>, Changyeon Lee <cyeon.lee@samsung.com>
9
10 Permission is hereby granted, free of charge, to any person obtaining a
11 copy of this software and associated documentation files (the
12 "Software"), to deal in the Software without restriction, including
13 without limitation the rights to use, copy, modify, merge, publish,
14 distribute, sub license, and/or sell copies of the Software, and to
15 permit persons to whom the Software is furnished to do so, subject to
16 the following conditions:
17
18 The above copyright notice and this permission notice (including the
19 next paragraph) shall be included in all copies or substantial portions
20 of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
25 IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
27 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
28 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 **************************************************************************/
31
32 #ifndef _TBM_BUFMGR_INT_H_
33 #define _TBM_BUFMGR_INT_H_
34
35 #include <sys/time.h>
36 #include <unistd.h>
37 #include <limits.h>
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <sys/ioctl.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <fcntl.h>
45 #include <dlfcn.h>
46 #include <dirent.h>
47 #include <string.h>
48 #include <errno.h>
49 #include <pthread.h>
50 #include <tbm_bufmgr.h>
51 #include <tbm_surface.h>
52 #include <tbm_surface_internal.h>
53 #include <tbm_bufmgr_backend.h>
54 #include <tbm_surface_queue.h>
55
56 #define DEBUG
57 #ifdef DEBUG
58 extern int bDebug;
59 extern tbm_bufmgr gBufMgr;
60
61 #define TBM_DBG(...) { if (bDebug&0x1) TBM_LOG_D(__VA_ARGS__); }
62 #define TBM_DBG_LOCK(...) { if (bDebug&0x2) TBM_LOG_D(__VA_ARGS__); }
63 #else
64 #define TBM_DBG(...)
65 #define TBM_DBG_LOCK(...)
66 #endif /* DEBUG */
67
68 #define TRACE
69 #ifdef TRACE
70 extern int bTrace;
71 #endif /* TRACE */
72
73 extern int b_dump_queue;
74
75 #ifdef HAVE_DLOG
76 #include <dlog.h>
77
78 extern int bDlog;
79
80 #ifdef LOG_TAG
81 #undef LOG_TAG
82 #endif
83
84 #define LOG_TAG "TBM"
85
86 #define TBM_LOG_D(fmt, ...) {\
87         if (bDlog) {\
88                 LOGD("[TBM:D] " fmt, ##__VA_ARGS__);\
89         } \
90         else {\
91                 fprintf(stderr, "[TBM:D(%d)(%s:%d)] " fmt, getpid(), __func__, __LINE__, ##__VA_ARGS__);\
92         } \
93 }
94
95 #define TBM_LOG_I(fmt, ...) {\
96         if (bDlog) {\
97                 LOGI("[TBM:I] " fmt, ##__VA_ARGS__);\
98         } \
99         else {\
100                 fprintf(stderr, "[TBM:I(%d)(%s:%d)] " fmt, getpid(), __func__, __LINE__, ##__VA_ARGS__);\
101         } \
102 }
103
104 #define TBM_LOG_W(fmt, ...) {\
105         if (bDlog) {\
106                 LOGW("[TBM:W] " fmt, ##__VA_ARGS__);\
107         } \
108         else {\
109                 fprintf(stderr, "[TBM:W(%d)(%s:%d)] " fmt, getpid(), __func__, __LINE__, ##__VA_ARGS__);\
110         } \
111 }
112
113 #define TBM_LOG_E(fmt, ...) {\
114         if (bDlog) {\
115                 LOGE("[TBM:E] " fmt, ##__VA_ARGS__);\
116         } \
117         else {\
118                 fprintf(stderr, "[TBM:E(%d)(%s:%d)] " fmt, getpid(), __func__, __LINE__, ##__VA_ARGS__);\
119         } \
120 }
121
122 #define TBM_DEBUG(fmt, ...) {\
123         if (bDlog) {\
124                 LOGE("[TBM_DEBUG] " fmt, ##__VA_ARGS__);\
125         } \
126         else {\
127                 fprintf(stderr, "[TBM:DEBUG(%d)] " fmt, getpid(), ##__VA_ARGS__);\
128         } \
129 }
130
131 #ifdef TRACE
132 #define TBM_TRACE(fmt, ...) {\
133         if (bDlog) {\
134                 if (bTrace&0x1) LOGE("[TBM:TRACE] " fmt, ##__VA_ARGS__);\
135         } \
136         else {\
137                 if (bTrace&0x1) fprintf(stderr, "[TBM:TRACE(%d)(%s:%d)] " fmt, getpid(), __func__, __LINE__, ##__VA_ARGS__);\
138         } \
139 }
140 #else
141 #define TBM_TRACE(fmt, ...)
142 #endif /* TRACE */
143
144 #else
145 #define TBM_LOG_D(fmt, ...)   fprintf(stderr, "[TBM:D(%d)(%s:%d)] " fmt, getpid(), __func__, __LINE__, ##__VA_ARGS__)
146 #define TBM_LOG_I(fmt, ...)   fprintf(stderr, "[TBM:I(%d)(%s:%d)] " fmt, getpid(), __func__, __LINE__, ##__VA_ARGS__)
147 #define TBM_LOG_W(fmt, ...)   fprintf(stderr, "[TBM:W(%d)(%s:%d)] " fmt, getpid(), __func__, __LINE__, ##__VA_ARGS__)
148 #define TBM_LOG_E(fmt, ...)   fprintf(stderr, "[TBM:E(%d)(%s:%d)] " fmt, getpid(), __func__, __LINE__, ##__VA_ARGS__)
149 #define TBM_DEBUG(fmt, ...)   fprintf(stderr, "[TBM:DEBUG(%d)] " fmt, getpid(), ##__VA_ARGS__)
150 #ifdef TRACE
151 #define TBM_TRACE(fmt, ...)   { if (bTrace&0x1) fprintf(stderr, "[TBM:TRACE(%d)(%s:%d)] " fmt, getpid(), __func__, __LINE__, ##__VA_ARGS__); }
152 #else
153 #define TBM_TRACE(fmt, ...)
154 #endif /* TRACE */
155 #endif /* HAVE_DLOG */
156
157 /* check condition */
158 #define TBM_RETURN_IF_FAIL(cond) {\
159         if (!(cond)) {\
160                 TBM_LOG_E("'%s' failed.\n", #cond);\
161                 return;\
162         } \
163 }
164 #define TBM_RETURN_VAL_IF_FAIL(cond, val) {\
165         if (!(cond)) {\
166                 TBM_LOG_E("'%s' failed.\n", #cond);\
167                 return val;\
168         } \
169 }
170 #define TBM_GOTO_VAL_IF_FAIL(cond, val) {\
171         if (!(cond)) {\
172                 TBM_LOG_E("'%s' failed.\n", #cond);\
173                 goto val;\
174         } \
175 }
176
177 /* check flags */
178 #define RETURN_CHECK_FLAG(cond) {\
179         if ((cond)) {\
180                 return;\
181         } \
182 }
183 #define RETURN_VAL_CHECK_FLAG(cond, val) {\
184         if ((cond)) {\
185                 return val;\
186         } \
187 }
188
189 /* check validation */
190 #define TBM_BUFMGR_IS_VALID(mgr) (mgr)
191 #define TBM_BO_IS_VALID(bo) (bo && \
192                             TBM_BUFMGR_IS_VALID(bo->bufmgr) && \
193                             bo->item_link.next && \
194                             bo->item_link.next->prev == &bo->item_link)
195 #define TBM_SURFACE_IS_VALID(surf) (surf && \
196                                    TBM_BUFMGR_IS_VALID(surf->bufmgr) && \
197                                    surf->item_link.next && \
198                                    surf->item_link.next->prev == &surf->item_link)
199
200
201 #define TBM_SNRPRINTF(p, len, count, fmt, ARG...)  \
202         do { \
203                 if (p) { \
204                         int rest = len - count; \
205                         int s = snprintf(&p[count], rest, fmt, ##ARG); \
206                         while (s >= rest) { \
207                                 len *= 2; \
208                                 p = realloc(p, len); \
209                                 rest = len - count; \
210                                 s = snprintf(&p[count], rest, fmt, ##ARG); \
211                         } \
212                         count += s; \
213                 } \
214         } while (0)
215
216 struct list_head {
217         struct list_head *prev;
218         struct list_head *next;
219 };
220
221 /**
222  * @brief tbm_bo : buffer object of Tizen Buffer Manager
223  */
224 struct _tbm_bo {
225         tbm_bufmgr bufmgr;               /* tbm buffer manager */
226         int ref_cnt;                     /* ref count of bo */
227         int flags;                       /* TBM_BO_FLAGS :bo memory type */
228         struct list_head user_data_list; /* list of the user_date in bo */
229         void *priv;                      /* bo private */
230         struct list_head item_link;      /* link of bo */
231         tbm_surface_h surface;           /* tbm_surface */
232         int lock_cnt;                    /* lock count of bo */
233         unsigned int map_cnt;            /* device map count */
234 };
235
236 /**
237  * @brief tbm_bufmgr : structure for tizen buffer manager
238  *
239  */
240 struct _tbm_bufmgr {
241         pthread_mutex_t lock;             /* mutex lock */
242         int ref_count;                    /* reference count */
243         int fd;                           /* bufmgr fd */
244         int lock_type;                    /* lock_type of bufmgr */
245         int capabilities;                 /* capabilities of bufmgr */
246         unsigned int bo_cnt;              /* number of bos */
247         struct list_head bo_list;         /* list of bos belonging to bufmgr */
248         struct list_head surf_list;       /* list of surfaces belonging to bufmgr */
249         struct list_head surf_queue_list; /* list of surface queues belonging to bufmgr */
250         struct list_head debug_key_list;  /* list of debug data key list belonging to bufmgr */
251         void *module_data;
252         tbm_bufmgr_backend backend;       /* bufmgr backend */
253         int display_server;               /* used by display server */
254 };
255
256 /**
257  * @brief tbm_surface : structure for tizen buffer surface
258  *
259  */
260 struct _tbm_surface {
261         tbm_bufmgr bufmgr;                      /* tbm buffer manager */
262
263         tbm_surface_info_s info;        /* tbm surface information */
264
265         int flags;
266
267         int num_bos;                            /* the number of buffer objects */
268
269         tbm_bo bos[4];
270
271         int num_planes;                         /* the number of buffer objects */
272
273         int planes_bo_idx[TBM_SURF_PLANE_MAX];
274
275         int refcnt;
276
277         unsigned int debug_pid;
278
279         struct list_head item_link; /* link of surface */
280
281         struct list_head user_data_list;        /* list of the user_date in surface */
282
283         struct list_head debug_data_list;       /* list of debug data */
284 };
285
286 typedef struct {
287         unsigned long key;
288         void *data;
289         tbm_data_free free_func;
290
291         /* link of user_data */
292         struct list_head item_link;
293 } tbm_user_data;
294
295 typedef struct {
296         char *key;
297         char *value;
298
299         /* link of user_data */
300         struct list_head item_link;
301 } tbm_surface_debug_data;
302
303 tbm_bufmgr _tbm_bufmgr_get_bufmgr(void);
304 int _tbm_bo_set_surface(tbm_bo bo, tbm_surface_h surface);
305 int _tbm_surface_is_valid(tbm_surface_h surface);
306
307 /* functions for mutex */
308 int tbm_surface_internal_get_info(tbm_surface_h surface, int opt,
309                                   tbm_surface_info_s *info, int map);
310 void tbm_surface_internal_unmap(tbm_surface_h surface);
311 unsigned int tbm_surface_internal_get_width(tbm_surface_h surface);
312 unsigned int tbm_surface_internal_get_height(tbm_surface_h surface);
313 tbm_format tbm_surface_internal_get_format(tbm_surface_h surface);
314 unsigned int _tbm_surface_internal_get_debug_pid(tbm_surface_h surface);
315 char *_tbm_surface_internal_format_to_str(tbm_format format);
316 char * _tbm_surface_internal_get_debug_data(tbm_surface_h surface, char *key);
317
318 tbm_user_data *user_data_lookup(struct list_head *user_data_list,
319                                 unsigned long key);
320 tbm_user_data *user_data_create(unsigned long key,
321                                 tbm_data_free data_free_func);
322 void user_data_delete(tbm_user_data *user_data);
323
324 int tbm_bufmgr_get_fd_limit(void);
325 #endif                                                  /* _TBM_BUFMGR_INT_H_ */