make the tbm_bo file and the tbm_type_int file.
[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_bo.h>
52 #include <tbm_surface.h>
53 #include <tbm_surface_internal.h>
54 #include <tbm_bufmgr_backend.h>
55 #include <tbm_surface_queue.h>
56
57 #define DEBUG
58 #ifdef DEBUG
59 extern int bDebug;
60 extern tbm_bufmgr gBufMgr;
61
62 #define TBM_DBG(...) { if (bDebug&0x1) TBM_LOG_D(__VA_ARGS__); }
63 #define TBM_DBG_LOCK(...) { if (bDebug&0x2) TBM_LOG_D(__VA_ARGS__); }
64 #else
65 #define TBM_DBG(...)
66 #define TBM_DBG_LOCK(...)
67 #endif /* DEBUG */
68
69 #define TRACE
70 #ifdef TRACE
71 extern int bTrace;
72 #endif /* TRACE */
73
74 extern int b_dump_queue;
75
76 #ifdef HAVE_DLOG
77 #include <dlog.h>
78
79 extern int bDlog;
80
81 #ifdef LOG_TAG
82 #undef LOG_TAG
83 #endif
84
85 #define LOG_TAG "TBM"
86
87 #define TBM_LOG_D(fmt, ...) {\
88         if (bDlog) {\
89                 LOGD("[TBM:D] " fmt, ##__VA_ARGS__);\
90         } \
91         else {\
92                 fprintf(stderr, "[TBM:D(%d)(%s:%d)] " fmt, getpid(), __func__, __LINE__, ##__VA_ARGS__);\
93         } \
94 }
95
96 #define TBM_LOG_I(fmt, ...) {\
97         if (bDlog) {\
98                 LOGI("[TBM:I] " fmt, ##__VA_ARGS__);\
99         } \
100         else {\
101                 fprintf(stderr, "[TBM:I(%d)(%s:%d)] " fmt, getpid(), __func__, __LINE__, ##__VA_ARGS__);\
102         } \
103 }
104
105 #define TBM_LOG_W(fmt, ...) {\
106         if (bDlog) {\
107                 LOGW("[TBM:W] " fmt, ##__VA_ARGS__);\
108         } \
109         else {\
110                 fprintf(stderr, "[TBM:W(%d)(%s:%d)] " fmt, getpid(), __func__, __LINE__, ##__VA_ARGS__);\
111         } \
112 }
113
114 #define TBM_LOG_E(fmt, ...) {\
115         if (bDlog) {\
116                 LOGE("[TBM:E] " fmt, ##__VA_ARGS__);\
117         } \
118         else {\
119                 fprintf(stderr, "[TBM:E(%d)(%s:%d)] " fmt, getpid(), __func__, __LINE__, ##__VA_ARGS__);\
120         } \
121 }
122
123 #define TBM_DEBUG(fmt, ...) {\
124         if (bDlog) {\
125                 LOGE("[TBM_DEBUG] " fmt, ##__VA_ARGS__);\
126         } \
127         else {\
128                 fprintf(stderr, "[TBM:DEBUG(%d)] " fmt, getpid(), ##__VA_ARGS__);\
129         } \
130 }
131
132 #ifdef TRACE
133 #define TBM_TRACE(fmt, ...) {\
134         if (bDlog) {\
135                 if (bTrace&0x1) LOGE("[TBM:TRACE] " fmt, ##__VA_ARGS__);\
136         } \
137         else {\
138                 if (bTrace&0x1) fprintf(stderr, "[TBM:TRACE(%d)(%s:%d)] " fmt, getpid(), __func__, __LINE__, ##__VA_ARGS__);\
139         } \
140 }
141 #else
142 #define TBM_TRACE(fmt, ...)
143 #endif /* TRACE */
144
145 #else
146 #define TBM_LOG_D(fmt, ...)   fprintf(stderr, "[TBM:D(%d)(%s:%d)] " fmt, getpid(), __func__, __LINE__, ##__VA_ARGS__)
147 #define TBM_LOG_I(fmt, ...)   fprintf(stderr, "[TBM:I(%d)(%s:%d)] " fmt, getpid(), __func__, __LINE__, ##__VA_ARGS__)
148 #define TBM_LOG_W(fmt, ...)   fprintf(stderr, "[TBM:W(%d)(%s:%d)] " fmt, getpid(), __func__, __LINE__, ##__VA_ARGS__)
149 #define TBM_LOG_E(fmt, ...)   fprintf(stderr, "[TBM:E(%d)(%s:%d)] " fmt, getpid(), __func__, __LINE__, ##__VA_ARGS__)
150 #define TBM_DEBUG(fmt, ...)   fprintf(stderr, "[TBM:DEBUG(%d)] " fmt, getpid(), ##__VA_ARGS__)
151 #ifdef TRACE
152 #define TBM_TRACE(fmt, ...)   { if (bTrace&0x1) fprintf(stderr, "[TBM:TRACE(%d)(%s:%d)] " fmt, getpid(), __func__, __LINE__, ##__VA_ARGS__); }
153 #else
154 #define TBM_TRACE(fmt, ...)
155 #endif /* TRACE */
156 #endif /* HAVE_DLOG */
157
158 /* check condition */
159 #define TBM_RETURN_IF_FAIL(cond) {\
160         if (!(cond)) {\
161                 TBM_LOG_E("'%s' failed.\n", #cond);\
162                 return;\
163         } \
164 }
165 #define TBM_RETURN_VAL_IF_FAIL(cond, val) {\
166         if (!(cond)) {\
167                 TBM_LOG_E("'%s' failed.\n", #cond);\
168                 return val;\
169         } \
170 }
171 #define TBM_GOTO_VAL_IF_FAIL(cond, val) {\
172         if (!(cond)) {\
173                 TBM_LOG_E("'%s' failed.\n", #cond);\
174                 goto val;\
175         } \
176 }
177
178 /* check flags */
179 #define RETURN_CHECK_FLAG(cond) {\
180         if ((cond)) {\
181                 return;\
182         } \
183 }
184 #define RETURN_VAL_CHECK_FLAG(cond, val) {\
185         if ((cond)) {\
186                 return val;\
187         } \
188 }
189
190 /* check validation */
191 #define TBM_BUFMGR_IS_VALID(mgr) (mgr && mgr == gBufMgr)
192 #define TBM_BO_IS_VALID(bo) (bo && \
193                             TBM_BUFMGR_IS_VALID(bo->bufmgr) && \
194                             bo->item_link.next && \
195                             bo->item_link.next->prev == &bo->item_link)
196 #define TBM_SURFACE_IS_VALID(surf) (surf && \
197                                    TBM_BUFMGR_IS_VALID(surf->bufmgr) && \
198                                    surf->item_link.next && \
199                                    surf->item_link.next->prev == &surf->item_link)
200
201
202 #define TBM_SNRPRINTF(p, len, count, fmt, ARG...)  \
203         do { \
204                 if (p) { \
205                         int rest = len - count; \
206                         int s = snprintf(&p[count], rest, fmt, ##ARG); \
207                         while (s >= rest) { \
208                                 len *= 2; \
209                                 p = realloc(p, len); \
210                                 rest = len - count; \
211                                 s = snprintf(&p[count], rest, fmt, ##ARG); \
212                         } \
213                         count += s; \
214                 } \
215         } while (0)
216
217 struct list_head {
218         struct list_head *prev;
219         struct list_head *next;
220 };
221
222 /**
223  * @brief tbm_bo : buffer object of Tizen Buffer Manager
224  */
225 struct _tbm_bo {
226         tbm_bufmgr bufmgr;               /* tbm buffer manager */
227         int ref_cnt;                     /* ref count of bo */
228         int flags;                       /* TBM_BO_FLAGS :bo memory type */
229         struct list_head user_data_list; /* list of the user_date in bo */
230         void *priv;                      /* bo private */
231         struct list_head item_link;      /* link of bo */
232         tbm_surface_h surface;           /* tbm_surface */
233         int lock_cnt;                    /* lock count of bo */
234         unsigned int map_cnt;            /* device map count */
235 };
236
237 /**
238  * @brief tbm_bufmgr : structure for tizen buffer manager
239  *
240  */
241 struct _tbm_bufmgr {
242         pthread_mutex_t lock;             /* mutex lock */
243         int ref_count;                    /* reference count */
244         int fd;                           /* bufmgr fd */
245         tbm_bufmgr_bo_lock_type bo_lock_type;  /* lock_type of bufmgr */
246         int capabilities;                 /* capabilities of bufmgr */
247         unsigned int bo_cnt;              /* number of bos */
248         struct list_head bo_list;         /* list of bos belonging to bufmgr */
249         struct list_head surf_list;       /* list of surfaces belonging to bufmgr */
250         struct list_head surf_queue_list; /* list of surface queues belonging to bufmgr */
251         struct list_head debug_key_list;  /* list of debug data key list belonging to bufmgr */
252         void *module_data;
253         tbm_bufmgr_backend backend;       /* bufmgr backend */
254         int display_server;               /* used by display server */
255 };
256
257 /**
258  * @brief tbm_surface : structure for tizen buffer surface
259  *
260  */
261 struct _tbm_surface {
262         tbm_bufmgr bufmgr;                      /* tbm buffer manager */
263
264         tbm_surface_info_s info;        /* tbm surface information */
265
266         int flags;
267
268         int num_bos;                            /* the number of buffer objects */
269
270         tbm_bo bos[4];
271
272         int num_planes;                         /* the number of buffer objects */
273
274         int planes_bo_idx[TBM_SURF_PLANE_MAX];
275
276         int refcnt;
277
278         unsigned int debug_pid;
279
280         struct list_head item_link; /* link of surface */
281
282         struct list_head user_data_list;        /* list of the user_date in surface */
283
284         struct list_head debug_data_list;       /* list of debug data */
285 };
286
287 typedef struct {
288         unsigned long key;
289         void *data;
290         tbm_data_free free_func;
291
292         /* link of user_data */
293         struct list_head item_link;
294 } tbm_user_data;
295
296 typedef struct {
297         char *key;
298         char *value;
299
300         /* link of user_data */
301         struct list_head item_link;
302 } tbm_surface_debug_data;
303
304 tbm_bufmgr _tbm_bufmgr_get_bufmgr(void);
305 int _tbm_bo_set_surface(tbm_bo bo, tbm_surface_h surface);
306 int _tbm_surface_is_valid(tbm_surface_h surface);
307 void _tbm_bo_free(tbm_bo bo);
308
309 /* functions for mutex */
310 int tbm_surface_internal_get_info(tbm_surface_h surface, int opt,
311                                   tbm_surface_info_s *info, int map);
312 void tbm_surface_internal_unmap(tbm_surface_h surface);
313 unsigned int tbm_surface_internal_get_width(tbm_surface_h surface);
314 unsigned int tbm_surface_internal_get_height(tbm_surface_h surface);
315 tbm_format tbm_surface_internal_get_format(tbm_surface_h surface);
316 unsigned int _tbm_surface_internal_get_debug_pid(tbm_surface_h surface);
317 char *_tbm_surface_internal_format_to_str(tbm_format format);
318 char * _tbm_surface_internal_get_debug_data(tbm_surface_h surface, char *key);
319
320 tbm_user_data *user_data_lookup(struct list_head *user_data_list,
321                                 unsigned long key);
322 tbm_user_data *user_data_create(unsigned long key,
323                                 tbm_data_free data_free_func);
324 void user_data_delete(tbm_user_data *user_data);
325
326 int tbm_bufmgr_get_fd_limit(void);
327 #endif                                                  /* _TBM_BUFMGR_INT_H_ */