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