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