tbm_bufmgr: add tbm_bufmgr_internal_import_bo_with_fd and use it
[platform/core/uifw/libtbm.git] / src / tbm_bufmgr_int.h
old mode 100755 (executable)
new mode 100644 (file)
index 965da8d..7776b3c
@@ -32,10 +32,12 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 #ifndef _TBM_BUFMGR_INT_H_
 #define _TBM_BUFMGR_INT_H_
 
+#include <sys/time.h>
 #include <unistd.h>
 #include <limits.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include <string.h>
 #include <sys/ioctl.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -44,170 +46,328 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 #include <dirent.h>
 #include <string.h>
 #include <errno.h>
+#include <assert.h>
 #include <pthread.h>
+#include <dlog.h>
 #include <tbm_bufmgr.h>
+#include <tbm_bo.h>
 #include <tbm_surface.h>
 #include <tbm_surface_internal.h>
-#include <tbm_bufmgr_backend.h>
 #include <tbm_surface_queue.h>
+#include <tbm_log.h>
+#include <tbm_bufmgr_backend.h>
+#include <tbm_backend.h>
+#include <tbm_error.h>
+#include <hal/hal-tbm.h>
 
-/* check condition */
-#define TBM_RETURN_IF_FAIL(cond) {\
-    if (!(cond)) {\
-        TBM_LOG ("[%s] : '%s' failed.\n", __FUNCTION__, #cond);\
-        return;\
-    }\
-}
-#define TBM_RETURN_VAL_IF_FAIL(cond, val) {\
-    if (!(cond)) {\
-        TBM_LOG ("[%s] : '%s' failed.\n", __FUNCTION__, #cond);\
-        return val;\
-    }\
-}
+extern tbm_bufmgr gBufMgr;
+extern int b_dump_queue;
+extern int trace_mask;
+
+#define TBM_BO_MAGIC 0xBF011234
+
+#define C(b, m)                (((b) >> (m)) & 0xFF)
+#define B(c, s)                ((((unsigned int)(c)) & 0xff) << (s))
+#define FOURCC(a, b, c, d)     (B(d, 24) | B(c, 16) | B(b, 8) | B(a, 0))
+#define FOURCC_STR(id)         C(id, 0), C(id, 8), C(id, 16), C(id, 24)
+#define FOURCC_ID(str)         FOURCC(((char*)str)[0], ((char*)str)[1], ((char*)str)[2], ((char*)str)[3])
 
 /* check flags */
 #define RETURN_CHECK_FLAG(cond) {\
-    if ((cond)) {\
-        return;\
-    }\
+       if ((cond)) {\
+               return;\
+       } \
 }
 #define RETURN_VAL_CHECK_FLAG(cond, val) {\
-    if ((cond)) {\
-        return val;\
-    }\
+       if ((cond)) {\
+               return val;\
+       } \
 }
 
+#define TBM_TRACE_BO(fmt, args...) \
+       do { \
+               if (trace_mask&TBM_BUFGMR_DEBUG_TRACE_BO) { \
+                       struct timespec ts; \
+                       clock_gettime(CLOCK_MONOTONIC, &ts); \
+                       tbm_log_print(TBM_LOG_LEVEL_INFO, "[%5d.%06d][%d][%s %d](TRACE)"fmt, \
+                                                 (int)ts.tv_sec, (int)ts.tv_nsec / 1000, \
+                                                 (int)syscall(SYS_gettid), __FUNCTION__, __LINE__, ##args);  \
+               } \
+       } while (0)
+
+#define TBM_TRACE_SURFACE_INTERNAL(fmt, args...) \
+       do { \
+               if (trace_mask&TBM_BUFGMR_DEBUG_TRACE_SURFACE_INTERNAL) { \
+                       struct timespec ts; \
+                       clock_gettime(CLOCK_MONOTONIC, &ts); \
+                       tbm_log_print(TBM_LOG_LEVEL_INFO, "[%5d.%06d][%d][%s %d](TRACE)"fmt, \
+                                                 (int)ts.tv_sec, (int)ts.tv_nsec / 1000, \
+                                                 (int)syscall(SYS_gettid), __FUNCTION__, __LINE__, ##args);  \
+               } \
+       } while (0)
+
+#define TBM_TRACE_SURFACE(fmt, args...) \
+       do { \
+               if (trace_mask&TBM_BUFGMR_DEBUG_TRACE_SURFACE) { \
+                       struct timespec ts; \
+                       clock_gettime(CLOCK_MONOTONIC, &ts); \
+                       tbm_log_print(TBM_LOG_LEVEL_INFO, "[%5d.%06d][%d][%s %d](TRACE)"fmt, \
+                                                 (int)ts.tv_sec, (int)ts.tv_nsec / 1000, \
+                                                 (int)syscall(SYS_gettid), __FUNCTION__, __LINE__, ##args);  \
+               } \
+       } while (0)
+
+#define TBM_TRACE_SURFACE_QUEUE(fmt, args...) \
+       do { \
+               if (trace_mask&TBM_BUFGMR_DEBUG_TRACE_SURFACE_QUEUE) { \
+                       struct timespec ts; \
+                       clock_gettime(CLOCK_MONOTONIC, &ts); \
+                       tbm_log_print(TBM_LOG_LEVEL_INFO, "[%5d.%06d][%d][%s %d](TRACE)"fmt, \
+                                                 (int)ts.tv_sec, (int)ts.tv_nsec / 1000, \
+                                                 (int)syscall(SYS_gettid), __FUNCTION__, __LINE__, ##args);  \
+               } \
+       } while (0)
+
+#define TBM_TRACE(fmt, args...) \
+       do { \
+               if (trace_mask&0x1) { \
+                       struct timespec ts; \
+                       clock_gettime(CLOCK_MONOTONIC, &ts); \
+                       tbm_log_print(TBM_LOG_LEVEL_INFO, "[%5d.%06d][%d][%s %d](TRACE)"fmt, \
+                                                 (int)ts.tv_sec, (int)ts.tv_nsec / 1000, \
+                                                 (int)syscall(SYS_gettid), __FUNCTION__, __LINE__, ##args);  \
+               } \
+       } while (0)
+
 
 /* check validation */
-#define TBM_BUFMGR_IS_VALID(mgr) (mgr)
+#define TBM_BUFMGR_IS_VALID(mgr) (mgr && mgr == gBufMgr)
 #define TBM_BO_IS_VALID(bo) (bo && \
-                         TBM_BUFMGR_IS_VALID(bo->bufmgr) && \
-                         bo->item_link.next && \
-                         bo->item_link.next->prev == &bo->item_link)
+                           TBM_BUFMGR_IS_VALID(bo->bufmgr) && \
+                           bo->item_link.next && \
+                           bo->item_link.next->prev == &bo->item_link)
 #define TBM_SURFACE_IS_VALID(surf) (surf && \
-                         TBM_BUFMGR_IS_VALID(surf->bufmgr) && \
-                         surf->item_link.next && \
-                         surf->item_link.next->prev == &surf->item_link)
-
-#define TBM_ALL_CTRL_BACKEND_VALID(flags) \
-        ((flags&TBM_CACHE_CTRL_BACKEND) &&\
-        (flags&TBM_LOCK_CTRL_BACKEND))
-#define TBM_CACHE_CTRL_BACKEND_VALID(flags) \
-        (flags&TBM_CACHE_CTRL_BACKEND)
-#define TBM_LOCK_CTRL_BACKEND_VALID(flags) \
-        (flags&TBM_LOCK_CTRL_BACKEND)
-
-#define TBM_DEBUG(fmt, ...)   fprintf (stderr, "[TBM:DEBUG] " fmt, ##__VA_ARGS__)
-#define TBM_LOG(fmt, ...)  fprintf (stderr, "[TBM:%s] " fmt, __func__, ##__VA_ARGS__)
-
-typedef union _tbm_bo_cache_state tbm_bo_cache_state;
-
-struct list_head
-{
-    struct list_head *prev;
-    struct list_head *next;
+                                  TBM_BUFMGR_IS_VALID(surf->bufmgr) && \
+                                  surf->item_link.next && \
+                                  surf->item_link.next->prev == &surf->item_link)
+
+
+#define TBM_SNRPRINTF(p, len, count, fmt, ARG...)  \
+       do { \
+               if (p) { \
+                       int rest = len - count; \
+                       int s = snprintf(&p[count], rest, fmt, ##ARG); \
+                       while (s >= rest) { \
+                               len *= 2; \
+                               p = realloc(p, len); \
+                               rest = len - count; \
+                               s = snprintf(&p[count], rest, fmt, ##ARG); \
+                       } \
+                       count += s; \
+               } \
+       } while (0)
+
+struct list_head {
+       struct list_head *prev;
+       struct list_head *next;
 };
 
-union _tbm_bo_cache_state
-{
-    unsigned int val;
-    struct {
-        unsigned int cntFlush:16;    /*Flush all index for sync*/
-        unsigned int isCacheable:1;
-        unsigned int isCached:1;
-        unsigned int isDirtied:2;
-    } data;
-};
+typedef enum _tbm_module_type {
+       TBM_MODULE_TYPE_NONE,
+       TBM_MODULE_TYPE_HAL_TBM,
+       TBM_MODULE_TYPE_TBM_BACKEND,
+       TBM_MODULE_TYPE_BUFMGR_BACKEND,
+} tbm_module_type;
+
+typedef struct _tbm_module {
+       tbm_module_type type;
+
+       void *module_data;                         /* backend module */
+       tbm_bufmgr_backend   backend;              /* bufmgr backend (will be DEPRECATED) */
+
+       tbm_backend_module       *backend_module_data;  /* backend module data */
+       tbm_backend_bufmgr_data  *bufmgr_data;          /* backend data of the backend module */
+       tbm_backend_bufmgr_func  *bufmgr_func;          /* backend functions for bufmgr */
+       tbm_backend_bo_func      *bo_func;              /* backend functions for bo */
+
+       int use_hal_tbm;                                /* use hal-api-tbm */
+       int auth_wl_socket_created;                     /* create wayland socket for authenticated drm_fd */
+       int auth_fd;
+       hal_tbm_backend          *hal_backend;          /* hal-api-tbm backend */
+       hal_tbm_bufmgr           *hal_bufmgr;           /* hal-api-tbm bufmgr */
+} tbm_module;
 
 /**
  * @brief tbm_bo : buffer object of Tizen Buffer Manager
  */
-struct _tbm_bo
-{
-    tbm_bufmgr bufmgr; /* tbm buffer manager */
-
-    int ref_cnt;       /* ref count of bo */
+struct _tbm_bo {
+       unsigned int magic;              /* tbm bo magic number */
+       tbm_bufmgr bufmgr;               /* tbm buffer manager */
+       int ref_cnt;                     /* ref count of bo */
+       int flags;                       /* TBM_BO_FLAGS :bo memory type */
+       struct list_head user_data_list; /* list of the user_date in bo */
+       void *priv;                      /* bo private  (will be DEPRECATED) */
+       struct list_head item_link;      /* link of bo */
+       tbm_surface_h surface;           /* tbm_surface */
+       int lock_cnt;                    /* lock count of bo */
+       unsigned int map_cnt;            /* device map count */
+
+       tbm_backend_bo_data *bo_data;    /* bo data of the backend module */
+       int get_from_hal_surface;        /* bo_data has be detroyed by hal backend */
+};
 
-    int flags;         /* TBM_BO_FLAGS :bo memory type */
+/**
+ * @brief tbm_bufmgr : structure for tizen buffer manager
+ *
+ */
+struct _tbm_bufmgr {
+       int ref_count;                    /* reference count */
+       int fd;                           /* bufmgr fd */
+       tbm_bufmgr_bo_lock_type bo_lock_type;  /* lock_type of bufmgr */
+       int capabilities;                 /* capabilities of bufmgr */
+       int display_server;               /* used by display server */
+       unsigned int bo_cnt;              /* number of bos */
+       struct list_head bo_list;         /* list of bos belonging to bufmgr */
+       struct list_head surf_list;       /* list of surfaces belonging to bufmgr */
+       struct list_head surf_queue_list; /* list of surface queues belonging to bufmgr */
+       struct list_head debug_key_list;  /* list of debug data key list belonging to bufmgr */
+
+       //TODO: tbm_module *module;
+       tbm_module *module;               /* tbm module */
+
+       //TODO: replace this to data in tbm_module *module
+       void *module_data;                         /* backend module */
+       tbm_bufmgr_backend   backend;              /* bufmgr backend (will be DEPRECATED) */
+
+       tbm_backend_module       *backend_module_data;  /* backend module data */
+       tbm_backend_bufmgr_data  *bufmgr_data;          /* backend data of the backend module */
+       tbm_backend_bufmgr_func  *bufmgr_func;          /* backend functions for bufmgr */
+       tbm_backend_bo_func      *bo_func;              /* backend functions for bo */
+
+       int use_hal_tbm;                                /* use hal-api-tbm */
+       int auth_wl_socket_created;                     /* create wayland socket for authenticated drm_fd */
+       int auth_fd;
+       hal_tbm_backend          *hal_backend;          /* hal-api-tbm backend */
+       hal_tbm_bufmgr           *hal_bufmgr;           /* hal-api-tbm bufmgr */
+};
 
-    unsigned int tgl_key; /*global key for tizen global lock */
+/**
+ * @brief tbm_surface : structure for tizen buffer surface
+ *
+ */
+struct _tbm_surface {
+       unsigned int magic;         /* tbm surface magic number */
 
-    /* for cache control */
-    unsigned int map_cnt; /* device map count */
-    tbm_bo_cache_state cache_state; /*cache state */
+       tbm_bufmgr bufmgr;                      /* tbm buffer manager */
 
-    int lock_cnt; /* lock count of bo */
+       tbm_surface_info_s info;        /* tbm surface information */
 
-    struct list_head user_data_list; /* list of the user_date in bo */
+       int flags;
 
-    void *priv; /* bo private */
+       int num_bos;                            /* the number of buffer objects */
 
-    struct list_head item_link; /* link of bo */
+       tbm_bo bos[4];
 
-    tbm_bo_handle default_handle; /*default handle */
-};
+       int num_planes;                         /* the number of buffer objects */
 
-/**
- * @brief tbm_bufmgr : structure for tizen buffer manager
- *
- */
-struct _tbm_bufmgr
-{
-    pthread_mutex_t lock; /* mutex lock */
+       int planes_bo_idx[TBM_SURF_PLANE_MAX];
 
-    int ref_count; /*reference count */
+       int refcnt;
 
-    int fd;  /* bufmgr fd */
+       unsigned int debug_pid;
 
-    int fd_flag; /* flag set 1 when bufmgr fd open in tbm_bufmgr_init*/
+       struct list_head item_link; /* link of surface */
 
-    int lock_fd; /* fd of tizen global lock */
+       struct list_head user_data_list;        /* list of the user_data in surface */
 
-    int lock_type; /* lock_type of bufmgr */
+       struct list_head debug_data_list;       /* list of debug data */
 
-    int use_map_cache; /* flag to use the map_cahce */
+       struct list_head destroy_funcs; /* list of destory callback function */
 
-    struct list_head bo_list; /* list of bos belonging to bufmgr */
+       struct {
+               int x;
+               int y;
+               int width;
+               int height;
+       } damage;
 
-    struct list_head surf_list; /* list of surfaces belonging to bufmgr */
+       hal_tbm_surface *hal_surface; // hal_tbm_surface
+};
 
-    void *module_data;
+typedef struct {
+       unsigned long key;
+       void *data;
+       tbm_data_free free_func;
 
-    tbm_bufmgr_backend backend; /* bufmgr backend */
-};
+       /* link of user_data */
+       struct list_head item_link;
+} tbm_user_data;
 
-/**
- * @brief tbm_surface : structure for tizen buffer surface
- *
- */
-struct _tbm_surface {
-    tbm_bufmgr bufmgr;  /* tbm buffer manager */
+typedef struct {
+       char *key;
+       char *value;
 
-    tbm_surface_info_s info;  /* tbm surface information */
+       /* link of user_data */
+       struct list_head item_link;
+} tbm_surface_debug_data;
 
-    int flags;
+typedef struct _tbm_surface_destroy_func_info {
+       tbm_surface_internal_destroy_handler destroy_func;
+       void *user_data;
 
-    int num_bos;  /* the number of buffer objects */
+       struct list_head item_link;
+} tbm_surface_destroy_func_info;
 
-    tbm_bo bos[4];
+tbm_bufmgr _tbm_bufmgr_get_bufmgr(void);
+int _tbm_bo_set_surface(tbm_bo bo, tbm_surface_h surface);
+int _tbm_surface_is_valid(tbm_surface_h surface);
+void _tbm_bo_free(tbm_bo bo);
 
-    int num_planes;  /* the number of buffer objects */
+int tbm_surface_internal_get_info(tbm_surface_h surface, int opt,
+                                 tbm_surface_info_s *info, int map);
+void tbm_surface_internal_unmap(tbm_surface_h surface);
+unsigned int tbm_surface_internal_get_width(tbm_surface_h surface);
+unsigned int tbm_surface_internal_get_height(tbm_surface_h surface);
+tbm_format tbm_surface_internal_get_format(tbm_surface_h surface);
+unsigned int _tbm_surface_internal_get_debug_pid(tbm_surface_h surface);
+char *_tbm_surface_internal_format_to_str(tbm_format format);
+char * _tbm_surface_internal_get_debug_data(tbm_surface_h surface, char *key);
 
-    int planes_bo_idx[TBM_SURF_PLANE_MAX];
+tbm_user_data *user_data_lookup(struct list_head *user_data_list,
+                               unsigned long key);
+tbm_user_data *user_data_create(unsigned long key,
+                               tbm_data_free data_free_func);
+void user_data_delete(tbm_user_data *user_data);
 
-    int refcnt;
+int tbm_bufmgr_get_fd_limit(void);
+tbm_bufmgr tbm_bufmgr_get(void);
 
-    struct list_head item_link; /* link of surface */
-};
+void _tbm_set_last_result(tbm_error_e err);
 
-int tbm_bufmgr_get_drm_fd_x11(void);
-int tbm_bufmgr_get_drm_fd_wayland(void);
+char *_tbm_flag_to_str(int f);
 
 /* functions for mutex */
-int          tbm_surface_internal_get_info (tbm_surface_h surface, int opt, tbm_surface_info_s *info, int map);
-void         tbm_surface_internal_unmap (tbm_surface_h surface);
-unsigned int tbm_surface_internal_get_width (tbm_surface_h surface);
-unsigned int tbm_surface_internal_get_height (tbm_surface_h surface);
-tbm_format   tbm_surface_internal_get_format (tbm_surface_h surface);
-
-#endif  /* _TBM_BUFMGR_INT_H_ */
+void _tbm_bufmgr_mutex_lock(void);
+void _tbm_bufmgr_mutex_unlock(void);
+tbm_bo tbm_bufmgr_internal_find_bo(tbm_bufmgr bufmgr, tbm_bo bo);
+
+tbm_bo tbm_bufmgr_internal_alloc_bo(tbm_bufmgr bufmgr, int size, int flags, tbm_error_e *error);
+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);
+tbm_bo tbm_bufmgr_internal_alloc_bo_with_bo_data(tbm_bufmgr bufmgr, tbm_backend_bo_data *bo_data, int flags);
+tbm_bo tbm_bufmgr_internal_import_bo_with_key(tbm_bufmgr bufmgr, tbm_key key, tbm_error_e *error);
+tbm_bo tbm_bufmgr_internal_import_bo_with_fd(tbm_bufmgr bufmgr, tbm_fd fd, tbm_error_e *error);
+
+/* tbm_module functions */
+tbm_module *tbm_module_load(int fd);
+void        tbm_module_unload(tbm_module *module);
+
+int                  tbm_module_bufmgr_get_capabilities(tbm_module *module, tbm_error_e *error);
+tbm_error_e          tbm_module_bufmgr_bind_native_display(tbm_module *module, void *native_display);
+tbm_error_e          tbm_module_bufmgr_get_supported_formats(tbm_module *module, uint32_t **formats, uint32_t *num);
+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);
+tbm_backend_bo_data *tbm_module_bufmgr_bo_alloc(tbm_module *module, tbm_bo bo, int size, int flags, tbm_error_e *error);
+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);
+tbm_backend_bo_data *tbm_module_bufmgr_bo_import_fd(tbm_module *module, tbm_bo bo, tbm_fd fd, tbm_error_e *error);
+tbm_backend_bo_data *tbm_module_bufmgr_bo_import_key(tbm_module *module, tbm_bo bo, tbm_key key, tbm_error_e *error);
+
+#endif /* _TBM_BUFMGR_INT_H_ */