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