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