Support logging dlog
[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 <unistd.h>
36 #include <limits.h>
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <sys/ioctl.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <fcntl.h>
43 #include <dlfcn.h>
44 #include <dirent.h>
45 #include <string.h>
46 #include <errno.h>
47 #include <pthread.h>
48 #include <tbm_bufmgr.h>
49 #include <tbm_surface.h>
50 #include <tbm_surface_internal.h>
51 #include <tbm_bufmgr_backend.h>
52 #include <tbm_surface_queue.h>
53
54 #define DEBUG
55 #ifdef DEBUG
56 extern int bDebug;
57
58 #define DBG(...) {if (bDebug&0x1) TBM_LOG_D(__VA_ARGS__);}
59 #define DBG_LOCK(...) {if (bDebug&0x2) TBM_LOG_D(__VA_ARGS__);}
60 #else
61 #define DBG(...)
62 #define DBG_LOCK(...)
63 #endif /* DEBUG */
64
65 #ifdef HAVE_DLOG
66 #include <dlog.h>
67
68 extern int bDlog;
69
70 #ifdef LOG_TAG
71 #undef LOG_TAG
72 #endif
73
74 #define LOG_TAG "TBM"
75
76 #define TBM_LOG_D(fmt, ...) {\
77         if (bDlog) {\
78                 LOGD("[TBM:D] " fmt, ##__VA_ARGS__);\
79         } \
80         else {\
81                 fprintf(stderr, "[TBM:D(%d)(%s:%d)] " fmt, getpid(), __func__, __LINE__, ##__VA_ARGS__);\
82         } \
83 }
84
85 #define TBM_LOG_I(fmt, ...) {\
86         if (bDlog) {\
87                 LOGD("[TBM:I] " fmt, ##__VA_ARGS__);\
88         } \
89         else {\
90                 fprintf(stderr, "[TBM:I(%d)(%s:%d)] " fmt, getpid(), __func__, __LINE__, ##__VA_ARGS__);\
91         } \
92 }
93
94 #define TBM_LOG_W(fmt, ...) {\
95         if (bDlog) {\
96                 LOGW("[TBM:W] " fmt, ##__VA_ARGS__);\
97         } \
98         else {\
99                 fprintf(stderr, "[TBM:W(%d)(%s:%d)] " fmt, getpid(), __func__, __LINE__, ##__VA_ARGS__);\
100         } \
101 }
102
103 #define TBM_LOG_E(fmt, ...) {\
104         if (bDlog) {\
105                 LOGE("[TBM:E] " fmt, ##__VA_ARGS__);\
106         } \
107         else {\
108                 fprintf(stderr, "[TBM:E(%d)(%s:%d)] " fmt, getpid(), __func__, __LINE__, ##__VA_ARGS__);\
109         } \
110 }
111 #else
112 #define TBM_LOG_D(fmt, ...)   fprintf(stderr, "[TBM:D(%d)(%s:%d)] " fmt, getpid(), __func__, __LINE__, ##__VA_ARGS__)
113 #define TBM_LOG_I(fmt, ...)   fprintf(stderr, "[TBM:I(%d)(%s:%d)] " fmt, getpid(), __func__, __LINE__, ##__VA_ARGS__)
114 #define TBM_LOG_W(fmt, ...)   fprintf(stderr, "[TBM:W(%d)(%s:%d)] " fmt, getpid(), __func__, __LINE__, ##__VA_ARGS__)
115 #define TBM_LOG_E(fmt, ...)   fprintf(stderr, "[TBM:E(%d)(%s:%d)] " fmt, getpid(), __func__, __LINE__, ##__VA_ARGS__)
116 #endif /* HAVE_DLOG */
117
118 #define TBM_DEBUG(fmt, ...)   fprintf(stderr, "[TBM:DEBUG(%d)] " fmt, getpid(), ##__VA_ARGS__)
119
120 /* check condition */
121 #define TBM_RETURN_IF_FAIL(cond) {\
122         if (!(cond)) {\
123                 TBM_LOG_E("'%s' failed.\n", #cond);\
124                 return;\
125         } \
126 }
127 #define TBM_RETURN_VAL_IF_FAIL(cond, val) {\
128         if (!(cond)) {\
129                 TBM_LOG_E("'%s' failed.\n", #cond);\
130                 return val;\
131         } \
132 }
133 #define TBM_GOTO_VAL_IF_FAIL(cond, val) {\
134         if (!(cond)) {\
135                 TBM_LOG_E("'%s' failed.\n", #cond);\
136                 goto val;\
137         } \
138 }
139
140 /* check flags */
141 #define RETURN_CHECK_FLAG(cond) {\
142         if ((cond)) {\
143                 return;\
144         } \
145 }
146 #define RETURN_VAL_CHECK_FLAG(cond, val) {\
147         if ((cond)) {\
148                 return val;\
149         } \
150 }
151
152 /* check validation */
153 #define TBM_BUFMGR_IS_VALID(mgr) (mgr)
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 struct list_head {
164         struct list_head *prev;
165         struct list_head *next;
166 };
167
168 /**
169  * @brief tbm_bo : buffer object of Tizen Buffer Manager
170  */
171 struct _tbm_bo {
172         tbm_bufmgr bufmgr;                      /* tbm buffer manager */
173
174         int ref_cnt;                            /* ref count of bo */
175
176         int flags;                                      /* TBM_BO_FLAGS :bo memory type */
177
178         struct list_head user_data_list;        /* list of the user_date in bo */
179
180         void *priv;                                     /* bo private */
181
182         struct list_head item_link;     /* link of bo */
183
184         tbm_surface_h surface; /* tbm_surface */
185
186         int lock_cnt;                           /* lock count of bo */
187
188         unsigned int map_cnt;           /* device map count */
189 };
190
191 /**
192  * @brief tbm_bufmgr : structure for tizen buffer manager
193  *
194  */
195 struct _tbm_bufmgr {
196         pthread_mutex_t lock;           /* mutex lock */
197
198         int ref_count;                          /*reference count */
199
200         int fd;                                         /* bufmgr fd */
201
202         int lock_type;                          /* lock_type of bufmgr */
203
204         struct list_head bo_list;       /* list of bos belonging to bufmgr */
205
206         struct list_head surf_list;     /* list of surfaces belonging to bufmgr */
207
208         void *module_data;
209
210         tbm_bufmgr_backend backend;     /* bufmgr backend */
211 };
212
213 /**
214  * @brief tbm_surface : structure for tizen buffer surface
215  *
216  */
217 struct _tbm_surface {
218         tbm_bufmgr bufmgr;                      /* tbm buffer manager */
219
220         tbm_surface_info_s info;        /* tbm surface information */
221
222         int flags;
223
224         int num_bos;                            /* the number of buffer objects */
225
226         tbm_bo bos[4];
227
228         int num_planes;                         /* the number of buffer objects */
229
230         int planes_bo_idx[TBM_SURF_PLANE_MAX];
231
232         int refcnt;
233
234         unsigned int debug_pid;
235
236         struct list_head item_link; /* link of surface */
237
238         struct list_head user_data_list;        /* list of the user_date in surface */
239 };
240
241 typedef struct {
242         unsigned long key;
243         void *data;
244         tbm_data_free free_func;
245
246         /* link of user_data */
247         struct list_head item_link;
248 } tbm_user_data;
249
250 int _tbm_bo_set_surface(tbm_bo bo, tbm_surface_h surface);
251 int _tbm_surface_is_valid(tbm_surface_h surface);
252
253 /* functions for mutex */
254 int tbm_surface_internal_get_info(tbm_surface_h surface, int opt,
255                                   tbm_surface_info_s *info, int map);
256 void tbm_surface_internal_unmap(tbm_surface_h surface);
257 unsigned int tbm_surface_internal_get_width(tbm_surface_h surface);
258 unsigned int tbm_surface_internal_get_height(tbm_surface_h surface);
259 tbm_format tbm_surface_internal_get_format(tbm_surface_h surface);
260 unsigned int _tbm_surface_internal_get_debug_pid(tbm_surface_h surface);
261 char *_tbm_surface_internal_format_to_str(tbm_format format);
262
263 tbm_user_data *user_data_lookup(struct list_head *user_data_list,
264                                 unsigned long key);
265 tbm_user_data *user_data_create(unsigned long key,
266                                 tbm_data_free data_free_func);
267 void user_data_delete(tbm_user_data *user_data);
268
269 #endif                                                  /* _TBM_BUFMGR_INT_H_ */