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