tbm_drm_helper: fix Svace TAINTED_INT defect
[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 <pthread.h>
50 #include <tbm_bufmgr.h>
51 #include <tbm_surface.h>
52 #include <tbm_surface_internal.h>
53 #include <tbm_bufmgr_backend.h>
54 #include <tbm_surface_queue.h>
55
56 #define DEBUG
57 #ifdef DEBUG
58 extern int bDebug;
59
60 #define TBM_DBG(...) { if (bDebug&0x1) TBM_LOG_D(__VA_ARGS__); }
61 #define TBM_DBG_LOCK(...) { if (bDebug&0x2) TBM_LOG_D(__VA_ARGS__); }
62 #else
63 #define TBM_DBG(...)
64 #define TBM_DBG_LOCK(...)
65 #endif /* DEBUG */
66
67 #define TRACE
68 #ifdef TRACE
69 extern int bTrace;
70 #endif /* TRACE */
71
72 extern int b_dump_queue;
73
74 #ifdef HAVE_DLOG
75 #include <dlog.h>
76
77 extern int bDlog;
78
79 #ifdef LOG_TAG
80 #undef LOG_TAG
81 #endif
82
83 #define LOG_TAG "TBM"
84
85 #define TBM_LOG_D(fmt, ...) {\
86         if (bDlog) {\
87                 LOGD("[TBM:D] " fmt, ##__VA_ARGS__);\
88         } \
89         else {\
90                 fprintf(stderr, "[TBM:D(%d)(%s:%d)] " fmt, getpid(), __func__, __LINE__, ##__VA_ARGS__);\
91         } \
92 }
93
94 #define TBM_LOG_I(fmt, ...) {\
95         if (bDlog) {\
96                 LOGI("[TBM:I] " fmt, ##__VA_ARGS__);\
97         } \
98         else {\
99                 fprintf(stderr, "[TBM:I(%d)(%s:%d)] " fmt, getpid(), __func__, __LINE__, ##__VA_ARGS__);\
100         } \
101 }
102
103 #define TBM_LOG_W(fmt, ...) {\
104         if (bDlog) {\
105                 LOGW("[TBM:W] " fmt, ##__VA_ARGS__);\
106         } \
107         else {\
108                 fprintf(stderr, "[TBM:W(%d)(%s:%d)] " fmt, getpid(), __func__, __LINE__, ##__VA_ARGS__);\
109         } \
110 }
111
112 #define TBM_LOG_E(fmt, ...) {\
113         if (bDlog) {\
114                 LOGE("[TBM:E] " fmt, ##__VA_ARGS__);\
115         } \
116         else {\
117                 fprintf(stderr, "[TBM:E(%d)(%s:%d)] " fmt, getpid(), __func__, __LINE__, ##__VA_ARGS__);\
118         } \
119 }
120
121 #define TBM_DEBUG(fmt, ...) {\
122         if (bDlog) {\
123                 LOGE("[TBM_DEBUG] " fmt, ##__VA_ARGS__);\
124         } \
125         else {\
126                 fprintf(stderr, "[TBM:DEBUG(%d)] " fmt, getpid(), ##__VA_ARGS__);\
127         } \
128 }
129
130 #ifdef TRACE
131 #define TBM_TRACE(fmt, ...) {\
132         if (bDlog) {\
133                 if (bTrace&0x1) LOGE("[TBM:TRACE] " fmt, ##__VA_ARGS__);\
134         } \
135         else {\
136                 if (bTrace&0x1) fprintf(stderr, "[TBM:TRACE(%d)(%s:%d)] " fmt, getpid(), __func__, __LINE__, ##__VA_ARGS__);\
137         } \
138 }
139 #else
140 #define TBM_TRACE(fmt, ...)
141 #endif /* TRACE */
142
143 #else
144 #define TBM_LOG_D(fmt, ...)   fprintf(stderr, "[TBM:D(%d)(%s:%d)] " fmt, getpid(), __func__, __LINE__, ##__VA_ARGS__)
145 #define TBM_LOG_I(fmt, ...)   fprintf(stderr, "[TBM:I(%d)(%s:%d)] " fmt, getpid(), __func__, __LINE__, ##__VA_ARGS__)
146 #define TBM_LOG_W(fmt, ...)   fprintf(stderr, "[TBM:W(%d)(%s:%d)] " fmt, getpid(), __func__, __LINE__, ##__VA_ARGS__)
147 #define TBM_LOG_E(fmt, ...)   fprintf(stderr, "[TBM:E(%d)(%s:%d)] " fmt, getpid(), __func__, __LINE__, ##__VA_ARGS__)
148 #define TBM_DEBUG(fmt, ...)   fprintf(stderr, "[TBM:DEBUG(%d)] " fmt, getpid(), ##__VA_ARGS__)
149 #ifdef TRACE
150 #define TBM_TRACE(fmt, ...)   { if (bTrace&0x1) fprintf(stderr, "[TBM:TRACE(%d)(%s:%d)] " fmt, getpid(), __func__, __LINE__, ##__VA_ARGS__); }
151 #else
152 #define TBM_TRACE(fmt, ...)
153 #endif /* TRACE */
154 #endif /* HAVE_DLOG */
155
156 /* check condition */
157 #define TBM_RETURN_IF_FAIL(cond) {\
158         if (!(cond)) {\
159                 TBM_LOG_E("'%s' failed.\n", #cond);\
160                 return;\
161         } \
162 }
163 #define TBM_RETURN_VAL_IF_FAIL(cond, val) {\
164         if (!(cond)) {\
165                 TBM_LOG_E("'%s' failed.\n", #cond);\
166                 return val;\
167         } \
168 }
169 #define TBM_GOTO_VAL_IF_FAIL(cond, val) {\
170         if (!(cond)) {\
171                 TBM_LOG_E("'%s' failed.\n", #cond);\
172                 goto val;\
173         } \
174 }
175
176 /* check flags */
177 #define RETURN_CHECK_FLAG(cond) {\
178         if ((cond)) {\
179                 return;\
180         } \
181 }
182 #define RETURN_VAL_CHECK_FLAG(cond, val) {\
183         if ((cond)) {\
184                 return val;\
185         } \
186 }
187
188 /* check validation */
189 #define TBM_BUFMGR_IS_VALID(mgr) (mgr)
190 #define TBM_BO_IS_VALID(bo) (bo && \
191                             TBM_BUFMGR_IS_VALID(bo->bufmgr) && \
192                             bo->item_link.next && \
193                             bo->item_link.next->prev == &bo->item_link)
194 #define TBM_SURFACE_IS_VALID(surf) (surf && \
195                                    TBM_BUFMGR_IS_VALID(surf->bufmgr) && \
196                                    surf->item_link.next && \
197                                    surf->item_link.next->prev == &surf->item_link)
198
199
200 #define TBM_SNPRINTF(p, len, fmt, ARG...)  \
201         do { \
202                 if (p && len && *len > 0) { \
203                         int s = snprintf(p, *len, fmt, ##ARG); \
204                         p += s; \
205                         *len -= s; \
206                 } \
207                 else { \
208                         TBM_DEBUG(fmt, ##ARG);\
209                 } \
210         } while (0)
211
212
213 struct list_head {
214         struct list_head *prev;
215         struct list_head *next;
216 };
217
218 /**
219  * @brief tbm_bo : buffer object of Tizen Buffer Manager
220  */
221 struct _tbm_bo {
222         tbm_bufmgr bufmgr;                      /* tbm buffer manager */
223
224         int ref_cnt;                            /* ref count of bo */
225
226         int flags;                                      /* TBM_BO_FLAGS :bo memory type */
227
228         struct list_head user_data_list;        /* list of the user_date in bo */
229
230         void *priv;                                     /* bo private */
231
232         struct list_head item_link;     /* link of bo */
233
234         tbm_surface_h surface; /* tbm_surface */
235
236         int lock_cnt;                           /* lock count of bo */
237
238         unsigned int map_cnt;           /* device map count */
239 };
240
241 /**
242  * @brief tbm_bufmgr : structure for tizen buffer manager
243  *
244  */
245 struct _tbm_bufmgr {
246         pthread_mutex_t lock;           /* mutex lock */
247
248         int ref_count;                          /*reference count */
249
250         int fd;                                         /* bufmgr fd */
251
252         int lock_type;                          /* lock_type of bufmgr */
253
254         int capabilities;           /* capabilities of bufmgr */
255
256         unsigned int bo_cnt;        /* number of bos */
257
258         struct list_head bo_list;       /* list of bos belonging to bufmgr */
259
260         struct list_head surf_list;     /* list of surfaces belonging to bufmgr */
261
262         struct list_head surf_queue_list; /* list of surface queues belonging to bufmgr */
263
264         struct list_head debug_key_list; /* list of debug data key list belonging to bufmgr */
265
266         void *module_data;
267
268         tbm_bufmgr_backend backend;     /* bufmgr backend */
269 };
270
271 /**
272  * @brief tbm_surface : structure for tizen buffer surface
273  *
274  */
275 struct _tbm_surface {
276         tbm_bufmgr bufmgr;                      /* tbm buffer manager */
277
278         tbm_surface_info_s info;        /* tbm surface information */
279
280         int flags;
281
282         int num_bos;                            /* the number of buffer objects */
283
284         tbm_bo bos[4];
285
286         int num_planes;                         /* the number of buffer objects */
287
288         int planes_bo_idx[TBM_SURF_PLANE_MAX];
289
290         int refcnt;
291
292         unsigned int debug_pid;
293
294         struct list_head item_link; /* link of surface */
295
296         struct list_head user_data_list;        /* list of the user_date in surface */
297
298         struct list_head debug_data_list;       /* list of debug data */
299 };
300
301 typedef struct {
302         unsigned long key;
303         void *data;
304         tbm_data_free free_func;
305
306         /* link of user_data */
307         struct list_head item_link;
308 } tbm_user_data;
309
310 typedef struct {
311         char *key;
312         char *value;
313
314         /* link of user_data */
315         struct list_head item_link;
316 } tbm_surface_debug_data;
317
318 tbm_bufmgr _tbm_bufmgr_get_bufmgr(void);
319 int _tbm_bo_set_surface(tbm_bo bo, tbm_surface_h surface);
320 int _tbm_surface_is_valid(tbm_surface_h surface);
321
322 /* functions for mutex */
323 int tbm_surface_internal_get_info(tbm_surface_h surface, int opt,
324                                   tbm_surface_info_s *info, int map);
325 void tbm_surface_internal_unmap(tbm_surface_h surface);
326 unsigned int tbm_surface_internal_get_width(tbm_surface_h surface);
327 unsigned int tbm_surface_internal_get_height(tbm_surface_h surface);
328 tbm_format tbm_surface_internal_get_format(tbm_surface_h surface);
329 unsigned int _tbm_surface_internal_get_debug_pid(tbm_surface_h surface);
330 char *_tbm_surface_internal_format_to_str(tbm_format format);
331 char * _tbm_surface_internal_get_debug_data(tbm_surface_h surface, char *key);
332
333 tbm_user_data *user_data_lookup(struct list_head *user_data_list,
334                                 unsigned long key);
335 tbm_user_data *user_data_create(unsigned long key,
336                                 tbm_data_free data_free_func);
337 void user_data_delete(tbm_user_data *user_data);
338
339 int tbm_bufmgr_get_fd_limit(void);
340 #endif                                                  /* _TBM_BUFMGR_INT_H_ */