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