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