tbm_module: add tbm_backend_surface_data structure
[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 #include <hal/hal-tbm.h>
62 #include "tbm_module.h"
63
64 extern tbm_bufmgr gBufMgr;
65 extern int b_dump_queue;
66 extern int trace_mask;
67
68 #define TBM_BO_MAGIC 0xBF011234
69
70 #define C(b, m)                (((b) >> (m)) & 0xFF)
71 #define B(c, s)                ((((unsigned int)(c)) & 0xff) << (s))
72 #define FOURCC(a, b, c, d)     (B(d, 24) | B(c, 16) | B(b, 8) | B(a, 0))
73 #define FOURCC_STR(id)         C(id, 0), C(id, 8), C(id, 16), C(id, 24)
74 #define FOURCC_ID(str)         FOURCC(((char*)str)[0], ((char*)str)[1], ((char*)str)[2], ((char*)str)[3])
75
76 /* check flags */
77 #define RETURN_CHECK_FLAG(cond) {\
78         if ((cond)) {\
79                 return;\
80         } \
81 }
82 #define RETURN_VAL_CHECK_FLAG(cond, val) {\
83         if ((cond)) {\
84                 return val;\
85         } \
86 }
87
88 #define TBM_TRACE_BO(fmt, args...) \
89         do { \
90                 if (trace_mask&TBM_BUFGMR_DEBUG_TRACE_BO) { \
91                         struct timespec ts; \
92                         clock_gettime(CLOCK_MONOTONIC, &ts); \
93                         tbm_log_print(TBM_LOG_LEVEL_INFO, "[%5d.%06d][%d][%s %d](TRACE)"fmt, \
94                                                   (int)ts.tv_sec, (int)ts.tv_nsec / 1000, \
95                                                   (int)syscall(SYS_gettid), __FUNCTION__, __LINE__, ##args);  \
96                 } \
97         } while (0)
98
99 #define TBM_TRACE_SURFACE_INTERNAL(fmt, args...) \
100         do { \
101                 if (trace_mask&TBM_BUFGMR_DEBUG_TRACE_SURFACE_INTERNAL) { \
102                         struct timespec ts; \
103                         clock_gettime(CLOCK_MONOTONIC, &ts); \
104                         tbm_log_print(TBM_LOG_LEVEL_INFO, "[%5d.%06d][%d][%s %d](TRACE)"fmt, \
105                                                   (int)ts.tv_sec, (int)ts.tv_nsec / 1000, \
106                                                   (int)syscall(SYS_gettid), __FUNCTION__, __LINE__, ##args);  \
107                 } \
108         } while (0)
109
110 #define TBM_TRACE_SURFACE(fmt, args...) \
111         do { \
112                 if (trace_mask&TBM_BUFGMR_DEBUG_TRACE_SURFACE) { \
113                         struct timespec ts; \
114                         clock_gettime(CLOCK_MONOTONIC, &ts); \
115                         tbm_log_print(TBM_LOG_LEVEL_INFO, "[%5d.%06d][%d][%s %d](TRACE)"fmt, \
116                                                   (int)ts.tv_sec, (int)ts.tv_nsec / 1000, \
117                                                   (int)syscall(SYS_gettid), __FUNCTION__, __LINE__, ##args);  \
118                 } \
119         } while (0)
120
121 #define TBM_TRACE_SURFACE_QUEUE(fmt, args...) \
122         do { \
123                 if (trace_mask&TBM_BUFGMR_DEBUG_TRACE_SURFACE_QUEUE) { \
124                         struct timespec ts; \
125                         clock_gettime(CLOCK_MONOTONIC, &ts); \
126                         tbm_log_print(TBM_LOG_LEVEL_INFO, "[%5d.%06d][%d][%s %d](TRACE)"fmt, \
127                                                   (int)ts.tv_sec, (int)ts.tv_nsec / 1000, \
128                                                   (int)syscall(SYS_gettid), __FUNCTION__, __LINE__, ##args);  \
129                 } \
130         } while (0)
131
132 #define TBM_TRACE(fmt, args...) \
133         do { \
134                 if (trace_mask&0x1) { \
135                         struct timespec ts; \
136                         clock_gettime(CLOCK_MONOTONIC, &ts); \
137                         tbm_log_print(TBM_LOG_LEVEL_INFO, "[%5d.%06d][%d][%s %d](TRACE)"fmt, \
138                                                   (int)ts.tv_sec, (int)ts.tv_nsec / 1000, \
139                                                   (int)syscall(SYS_gettid), __FUNCTION__, __LINE__, ##args);  \
140                 } \
141         } while (0)
142
143
144 /* check validation */
145 #define TBM_BUFMGR_IS_VALID(mgr) (mgr && mgr == gBufMgr)
146 #define TBM_BO_IS_VALID(bo) (bo && \
147                             TBM_BUFMGR_IS_VALID(bo->bufmgr) && \
148                             bo->item_link.next && \
149                             bo->item_link.next->prev == &bo->item_link)
150 #define TBM_SURFACE_IS_VALID(surf) (surf && \
151                                    TBM_BUFMGR_IS_VALID(surf->bufmgr) && \
152                                    surf->item_link.next && \
153                                    surf->item_link.next->prev == &surf->item_link)
154
155
156 #define TBM_SNRPRINTF(p, len, count, fmt, ARG...)  \
157         do { \
158                 if (p) { \
159                         int rest = len - count; \
160                         int s = snprintf(&p[count], rest, fmt, ##ARG); \
161                         while (s >= rest) { \
162                                 len *= 2; \
163                                 p = realloc(p, len); \
164                                 rest = len - count; \
165                                 s = snprintf(&p[count], rest, fmt, ##ARG); \
166                         } \
167                         count += s; \
168                 } \
169         } while (0)
170
171 struct list_head {
172         struct list_head *prev;
173         struct list_head *next;
174 };
175
176 /**
177  * @brief tbm_bo : buffer object of Tizen Buffer Manager
178  */
179 struct _tbm_bo {
180         unsigned int magic;              /* tbm bo magic number */
181         tbm_bufmgr bufmgr;               /* tbm buffer manager */
182         int ref_cnt;                     /* ref count of bo */
183         int flags;                       /* TBM_BO_FLAGS :bo memory type */
184         struct list_head user_data_list; /* list of the user_date in bo */
185         void *priv;                      /* bo private  (will be DEPRECATED) */
186         struct list_head item_link;      /* link of bo */
187         tbm_surface_h surface;           /* tbm_surface */
188         int lock_cnt;                    /* lock count of bo */
189         unsigned int map_cnt;            /* device map count */
190
191         tbm_backend_bo_data *bo_data;    /* bo data of the backend module */
192         int get_from_hal_surface;        /* bo_data has be detroyed by hal backend */
193 };
194
195 /**
196  * @brief tbm_bufmgr : structure for tizen buffer manager
197  *
198  */
199 struct _tbm_bufmgr {
200         int ref_count;                    /* reference count */
201         int fd;                           /* bufmgr fd */
202         tbm_bufmgr_bo_lock_type bo_lock_type;  /* lock_type of bufmgr */
203         int capabilities;                 /* capabilities of bufmgr */
204         int display_server;               /* used by display server */
205         unsigned int bo_cnt;              /* number of bos */
206         struct list_head bo_list;         /* list of bos belonging to bufmgr */
207         struct list_head surf_list;       /* list of surfaces belonging to bufmgr */
208         struct list_head surf_queue_list; /* list of surface queues belonging to bufmgr */
209         struct list_head debug_key_list;  /* list of debug data key list belonging to bufmgr */
210
211         //TODO: tbm_module *module;
212         tbm_module *module;               /* tbm module */
213
214         //TODO: replace this to data in tbm_module *module
215         void *module_data;                         /* backend module */
216         tbm_bufmgr_backend   backend;              /* bufmgr backend (will be DEPRECATED) */
217
218         tbm_backend_module       *backend_module_data;  /* backend module data */
219         tbm_backend_bufmgr_data  *bufmgr_data;          /* backend data of the backend module */
220         tbm_backend_bufmgr_func  *bufmgr_func;          /* backend functions for bufmgr */
221         tbm_backend_bo_func      *bo_func;              /* backend functions for bo */
222
223         int use_hal_tbm;                                /* use hal-api-tbm */
224         int auth_wl_socket_created;                     /* create wayland socket for authenticated drm_fd */
225         int auth_fd;
226         hal_tbm_backend          *hal_backend;          /* hal-api-tbm backend */
227         hal_tbm_bufmgr           *hal_bufmgr;           /* hal-api-tbm bufmgr */
228 };
229
230 /**
231  * @brief tbm_surface : structure for tizen buffer surface
232  *
233  */
234 struct _tbm_surface {
235         unsigned int magic;         /* tbm surface magic number */
236
237         tbm_bufmgr bufmgr;                      /* tbm buffer manager */
238
239         tbm_surface_info_s info;        /* tbm surface information */
240
241         int flags;
242
243         int num_bos;                            /* the number of buffer objects */
244
245         tbm_bo bos[4];
246
247         int num_planes;                         /* the number of buffer objects */
248
249         int planes_bo_idx[TBM_SURF_PLANE_MAX];
250
251         int refcnt;
252
253         unsigned int debug_pid;
254
255         struct list_head item_link; /* link of surface */
256
257         struct list_head user_data_list;        /* list of the user_data in surface */
258
259         struct list_head debug_data_list;       /* list of debug data */
260
261         struct list_head destroy_funcs; /* list of destory callback function */
262
263         struct {
264                 int x;
265                 int y;
266                 int width;
267                 int height;
268         } damage;
269
270         hal_tbm_surface *hal_surface; // hal_tbm_surface
271 };
272
273 typedef struct {
274         unsigned long key;
275         void *data;
276         tbm_data_free free_func;
277
278         /* link of user_data */
279         struct list_head item_link;
280 } tbm_user_data;
281
282 typedef struct {
283         char *key;
284         char *value;
285
286         /* link of user_data */
287         struct list_head item_link;
288 } tbm_surface_debug_data;
289
290 typedef struct _tbm_surface_destroy_func_info {
291         tbm_surface_internal_destroy_handler destroy_func;
292         void *user_data;
293
294         struct list_head item_link;
295 } tbm_surface_destroy_func_info;
296
297 tbm_bufmgr _tbm_bufmgr_get_bufmgr(void);
298 int _tbm_bo_set_surface(tbm_bo bo, tbm_surface_h surface);
299 int _tbm_surface_is_valid(tbm_surface_h surface);
300 void _tbm_bo_free(tbm_bo bo);
301
302 int tbm_surface_internal_get_info(tbm_surface_h surface, int opt,
303                                   tbm_surface_info_s *info, int map);
304 void tbm_surface_internal_unmap(tbm_surface_h surface);
305 unsigned int tbm_surface_internal_get_width(tbm_surface_h surface);
306 unsigned int tbm_surface_internal_get_height(tbm_surface_h surface);
307 tbm_format tbm_surface_internal_get_format(tbm_surface_h surface);
308 unsigned int _tbm_surface_internal_get_debug_pid(tbm_surface_h surface);
309 char *_tbm_surface_internal_format_to_str(tbm_format format);
310 char * _tbm_surface_internal_get_debug_data(tbm_surface_h surface, char *key);
311
312 tbm_user_data *user_data_lookup(struct list_head *user_data_list,
313                                 unsigned long key);
314 tbm_user_data *user_data_create(unsigned long key,
315                                 tbm_data_free data_free_func);
316 void user_data_delete(tbm_user_data *user_data);
317
318 int tbm_bufmgr_get_fd_limit(void);
319 tbm_bufmgr tbm_bufmgr_get(void);
320
321 void _tbm_set_last_result(tbm_error_e err);
322
323 char *_tbm_flag_to_str(int f);
324
325 /* functions for mutex */
326 void _tbm_bufmgr_mutex_lock(void);
327 void _tbm_bufmgr_mutex_unlock(void);
328 tbm_bo tbm_bufmgr_internal_find_bo(tbm_bufmgr bufmgr, tbm_bo bo);
329
330 tbm_bo tbm_bufmgr_internal_alloc_bo(tbm_bufmgr bufmgr, int size, int flags, tbm_error_e *error);
331 tbm_bo tbm_bufmgr_internal_alloc_bo_with_format(tbm_bufmgr bufmgr, int format, int bo_idx, int width, int bpp, int height, tbm_bo_memory_type flags, tbm_error_e *error);
332 tbm_bo tbm_bufmgr_internal_alloc_bo_with_bo_data(tbm_bufmgr bufmgr, tbm_backend_bo_data *bo_data, int flags);
333 tbm_bo tbm_bufmgr_internal_import_bo_with_key(tbm_bufmgr bufmgr, tbm_key key, tbm_error_e *error);
334 tbm_bo tbm_bufmgr_internal_import_bo_with_fd(tbm_bufmgr bufmgr, tbm_fd fd, tbm_error_e *error);
335
336 #endif /* _TBM_BUFMGR_INT_H_ */