move the position of the codes
[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
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         int display_server;               /* used by display server */
210         unsigned int bo_cnt;              /* number of bos */
211         struct list_head bo_list;         /* list of bos belonging to bufmgr */
212         struct list_head surf_list;       /* list of surfaces belonging to bufmgr */
213         struct list_head surf_queue_list; /* list of surface queues belonging to bufmgr */
214         struct list_head debug_key_list;  /* list of debug data key list belonging to bufmgr */
215
216         void *module_data;
217         tbm_bufmgr_backend backend;       /* bufmgr backend */
218 };
219
220 /**
221  * @brief tbm_surface : structure for tizen buffer surface
222  *
223  */
224 struct _tbm_surface {
225         tbm_bufmgr bufmgr;                      /* tbm buffer manager */
226
227         tbm_surface_info_s info;        /* tbm surface information */
228
229         int flags;
230
231         int num_bos;                            /* the number of buffer objects */
232
233         tbm_bo bos[4];
234
235         int num_planes;                         /* the number of buffer objects */
236
237         int planes_bo_idx[TBM_SURF_PLANE_MAX];
238
239         int refcnt;
240
241         unsigned int debug_pid;
242
243         struct list_head item_link; /* link of surface */
244
245         struct list_head user_data_list;        /* list of the user_date in surface */
246
247         struct list_head debug_data_list;       /* list of debug data */
248 };
249
250 typedef struct {
251         unsigned long key;
252         void *data;
253         tbm_data_free free_func;
254
255         /* link of user_data */
256         struct list_head item_link;
257 } tbm_user_data;
258
259 typedef struct {
260         char *key;
261         char *value;
262
263         /* link of user_data */
264         struct list_head item_link;
265 } tbm_surface_debug_data;
266
267 tbm_bufmgr _tbm_bufmgr_get_bufmgr(void);
268 int _tbm_bo_set_surface(tbm_bo bo, tbm_surface_h surface);
269 int _tbm_surface_is_valid(tbm_surface_h surface);
270 void _tbm_bo_free(tbm_bo bo);
271
272 /* functions for mutex */
273 int tbm_surface_internal_get_info(tbm_surface_h surface, int opt,
274                                   tbm_surface_info_s *info, int map);
275 void tbm_surface_internal_unmap(tbm_surface_h surface);
276 unsigned int tbm_surface_internal_get_width(tbm_surface_h surface);
277 unsigned int tbm_surface_internal_get_height(tbm_surface_h surface);
278 tbm_format tbm_surface_internal_get_format(tbm_surface_h surface);
279 unsigned int _tbm_surface_internal_get_debug_pid(tbm_surface_h surface);
280 char *_tbm_surface_internal_format_to_str(tbm_format format);
281 char * _tbm_surface_internal_get_debug_data(tbm_surface_h surface, char *key);
282
283 tbm_user_data *user_data_lookup(struct list_head *user_data_list,
284                                 unsigned long key);
285 tbm_user_data *user_data_create(unsigned long key,
286                                 tbm_data_free data_free_func);
287 void user_data_delete(tbm_user_data *user_data);
288
289 int tbm_bufmgr_get_fd_limit(void);
290 tbm_bufmgr tbm_bufmgr_get(void);
291 #endif                                                  /* _TBM_BUFMGR_INT_H_ */