change dump API from tdm_helper to tbm_surface_internal
[platform/core/uifw/libtdm.git] / src / tdm_private.h
index f0e838b..65ce287 100644 (file)
@@ -58,11 +58,14 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 #include "tdm_backend.h"
 #include "tdm_log.h"
 #include "tdm_list.h"
+#include "tdm_macro.h"
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
+//#define INIT_BUFMGR
+
 /**
  * @file tdm_private.h
  * @brief The private header file for a frontend library
@@ -72,61 +75,6 @@ extern int tdm_debug_buffer;
 extern int tdm_debug_mutex;
 extern int tdm_debug_thread;
 
-#undef EXTERN
-#undef DEPRECATED
-#undef INTERN
-
-#if defined(__GNUC__) && __GNUC__ >= 4
-#define EXTERN __attribute__ ((visibility("default")))
-#else
-#define EXTERN
-#endif
-
-#if defined(__GNUC__) && __GNUC__ >= 4
-#define INTERN __attribute__ ((visibility("hidden")))
-#else
-#define INTERN
-#endif
-
-#if defined(__GNUC__) && __GNUC__ >= 4
-#define DEPRECATED __attribute__ ((deprecated))
-#else
-#define DEPRECATED
-#endif
-
-/* check condition */
-#define TDM_RETURN_IF_FAIL(cond) {\
-    if (!(cond)) {\
-        TDM_ERR ("'%s' failed", #cond);\
-        return;\
-    }\
-}
-#define TDM_RETURN_VAL_IF_FAIL(cond, val) {\
-    if (!(cond)) {\
-        TDM_ERR ("'%s' failed", #cond);\
-        return val;\
-    }\
-}
-#define TDM_RETURN_VAL_IF_FAIL_WITH_ERROR(cond, error_v, val) {\
-    if (!(cond)) {\
-        TDM_ERR ("'%s' failed", #cond);\
-        ret = error_v;\
-        if (error) *error = ret;\
-        return val;\
-    }\
-}
-
-#define TDM_WARNING_IF_FAIL(cond)  {\
-    if (!(cond))\
-        TDM_ERR ("'%s' failed", #cond);\
-}
-#define TDM_GOTO_IF_FAIL(cond, dst) {\
-    if (!(cond)) {\
-        TDM_ERR ("'%s' failed", #cond);\
-        goto dst;\
-    }\
-}
-
 #ifdef HAVE_TTRACE
 #include <ttrace.h>
 #define TDM_TRACE_BEGIN(NAME) traceBegin(TTRACE_TAG_GRAPHICS, "TDM:"#NAME)
@@ -136,23 +84,10 @@ extern int tdm_debug_thread;
 #define TDM_TRACE_END()
 #endif
 
-#define TDM_NEVER_GET_HERE() TDM_ERR("** NEVER GET HERE **")
+#define prototype_name_fn(res) const char * res##_str(int type)
 
-#define TDM_SNPRINTF(p, len, fmt, ARG...)  \
-    do { \
-        if (p && len && *len > 0) \
-        { \
-            int s = snprintf(p, *len, fmt, ##ARG); \
-            p += s; \
-            *len -= s; \
-        } \
-    } while (0)
-
-#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])
+prototype_name_fn(dpms);
+prototype_name_fn(status);
 
 typedef enum {
         TDM_CAPTURE_TARGET_OUTPUT,
@@ -164,7 +99,8 @@ typedef struct _tdm_private_output tdm_private_output;
 typedef struct _tdm_private_layer tdm_private_layer;
 typedef struct _tdm_private_pp tdm_private_pp;
 typedef struct _tdm_private_capture tdm_private_capture;
-typedef struct _tdm_private_event tdm_private_event;
+typedef struct _tdm_private_loop tdm_private_loop;
+typedef struct _tdm_private_server tdm_private_server;
 typedef struct _tdm_private_thread tdm_private_thread;
 typedef struct _tdm_private_vblank_handler tdm_private_vblank_handler;
 typedef struct _tdm_private_commit_handler tdm_private_commit_handler;
@@ -179,6 +115,10 @@ struct _tdm_private_display {
        tdm_backend_module *module_data;
        tdm_backend_data *bdata;
 
+#ifdef INIT_BUFMGR
+       tbm_bufmgr bufmgr;
+#endif
+
        /* backend function */
        tdm_display_capability capabilities;
        tdm_func_display func_display;
@@ -200,10 +140,7 @@ struct _tdm_private_display {
        void **outputs_ptr;
 
        /* for event handling */
-       tdm_private_event *private_event;
-
-       /* for own event thread */
-       tdm_private_thread *private_thread;
+       tdm_private_loop *private_loop;
 };
 
 struct _tdm_private_output {
@@ -217,6 +154,7 @@ struct _tdm_private_output {
        tdm_output *output_backend;
 
        unsigned int pipe;
+       tdm_output_dpms current_dpms_value;
 
        int regist_vblank_cb;
        int regist_commit_cb;
@@ -226,7 +164,10 @@ struct _tdm_private_output {
        struct list_head capture_list;
        struct list_head vblank_handler_list;
        struct list_head commit_handler_list;
-       struct list_head change_handler_list;
+
+       /* seperate list for multi-thread*/
+       struct list_head change_handler_list_main;
+       struct list_head change_handler_list_sub;
 
        void **layers_ptr;
 };
@@ -263,6 +204,8 @@ struct _tdm_private_pp {
        struct list_head dst_pending_buffer_list;
        struct list_head src_buffer_list;
        struct list_head dst_buffer_list;
+
+       pid_t owner_tid;
 };
 
 struct _tdm_private_capture {
@@ -281,6 +224,40 @@ struct _tdm_private_capture {
 
        struct list_head pending_buffer_list;
        struct list_head buffer_list;
+
+       pid_t owner_tid;
+};
+
+/* CAUTION:
+ * Note that we don't need to (un)lock mutex to use this structure. If there is
+ * no TDM thread, all TDM resources are protected by private_display's mutex.
+ * If there is a TDM thread, this struct will be used only in a TDM thread.
+ * So, we don't need to protect this structure by mutex. Not thread-safe.
+ */
+struct _tdm_private_loop {
+       /* TDM uses wl_event_loop to handle various event sources including the TDM
+        * backend's fd.
+        */
+       struct wl_display *wl_display;
+       struct wl_event_loop *wl_loop;
+
+       int backend_fd;
+       tdm_event_loop_source *backend_source;
+
+       /* In event loop, all resources are accessed by this dpy.
+        * CAUTION:
+        * - DO NOT include other private structure in this structure because this
+        *   struct is not protected by mutex.
+        */
+       tdm_display *dpy;
+
+       /* for handling TDM client requests */
+       tdm_private_server *private_server;
+
+       /* To have a TDM event thread. If TDM_THREAD enviroment variable is not set
+        * private_thread is NULL.
+        */
+       tdm_private_thread *private_thread;
 };
 
 struct _tdm_private_vblank_handler {
@@ -289,6 +266,8 @@ struct _tdm_private_vblank_handler {
        tdm_private_output *private_output;
        tdm_output_vblank_handler func;
        void *user_data;
+
+       pid_t owner_tid;
 };
 
 struct _tdm_private_commit_handler {
@@ -297,6 +276,8 @@ struct _tdm_private_commit_handler {
        tdm_private_output *private_output;
        tdm_output_commit_handler func;
        void *user_data;
+
+       pid_t owner_tid;
 };
 
 struct _tdm_private_change_handler {
@@ -305,6 +286,8 @@ struct _tdm_private_change_handler {
        tdm_private_output *private_output;
        tdm_output_change_handler func;
        void *user_data;
+
+       pid_t owner_tid;
 };
 
 typedef struct _tdm_buffer_info {
@@ -320,6 +303,12 @@ typedef struct _tdm_buffer_info {
        struct list_head link;
 } tdm_buffer_info;
 
+const char*
+tdm_get_dpms_str(tdm_output_dpms dpms_value);
+
+int
+tdm_display_check_module_abi(tdm_private_display *private_display, int abimaj, int abimin);
+
 tdm_private_output *
 tdm_display_find_output_stamp(tdm_private_display *private_display,
                               unsigned long stamp);
@@ -346,6 +335,7 @@ tdm_capture_cb_done(tdm_capture *capture_backend, tbm_surface_h buffer,
 
 void
 tdm_output_call_change_handler_internal(tdm_private_output *private_output,
+                                        struct list_head *change_handler_list,
                                         tdm_output_change_type type,
                                         tdm_value value);
 
@@ -373,18 +363,17 @@ tdm_buffer_list_dump(struct list_head *list);
 
 /* event functions for private */
 tdm_error
-tdm_event_init(tdm_private_display *private_display);
+tdm_event_loop_init(tdm_private_display *private_display);
 void
-tdm_event_deinit(tdm_private_display *private_display);
+tdm_event_loop_deinit(tdm_private_display *private_display);
 void
-tdm_event_create_backend_source(tdm_private_display *private_display);
+tdm_event_loop_create_backend_source(tdm_private_display *private_display);
 int
-tdm_event_get_fd(tdm_private_display *private_display);
-tdm_error
-tdm_event_dispatch(tdm_private_display *private_display);
+tdm_event_loop_get_fd(tdm_private_display *private_display);
 tdm_error
-tdm_event_add_socket(tdm_private_display *private_display, const char *name);
-
+tdm_event_loop_dispatch(tdm_private_display *private_display);
+void
+tdm_event_loop_flush(tdm_private_display *private_display);
 
 typedef enum {
        TDM_THREAD_CB_NONE,
@@ -439,25 +428,90 @@ struct _tdm_thread_cb_capture_done {
 };
 
 tdm_error
-tdm_thread_init(tdm_private_display *private_display);
+tdm_thread_init(tdm_private_loop *private_loop);
 void
-tdm_thread_deinit(tdm_private_display *private_display);
+tdm_thread_deinit(tdm_private_loop *private_loop);
 int
-tdm_thread_get_fd(tdm_private_display *private_display);
+tdm_thread_get_fd(tdm_private_loop *private_loop);
 tdm_error
-tdm_thread_send_cb(tdm_private_display *private_display, tdm_thread_cb_base *base);
+tdm_thread_send_cb(tdm_private_loop *private_loop, tdm_thread_cb_base *base);
 tdm_error
-tdm_thread_handle_cb(tdm_private_display *private_display);
+tdm_thread_handle_cb(tdm_private_loop *private_loop);
+int
+tdm_thread_in_display_thread(pid_t tid);
 int
-tdm_thread_in_display_thread(tdm_private_display *private_display);
+tdm_thread_is_running(void);
+
+tdm_error
+tdm_server_init(tdm_private_loop *private_loop);
+void
+tdm_server_deinit(tdm_private_loop *private_loop);
 
 unsigned long
 tdm_helper_get_time_in_millis(void);
+unsigned long
+tdm_helper_get_time_in_micros(void);
+
+extern pthread_mutex_t tdm_mutex_check_lock;
+extern int tdm_mutex_locked;
+extern int tdm_dump_enable;
 
-#define _pthread_mutex_lock(l) \
-    do {if (tdm_debug_mutex) TDM_INFO("mutex lock"); pthread_mutex_lock(l);} while (0)
 #define _pthread_mutex_unlock(l) \
-    do {if (tdm_debug_mutex) TDM_INFO("mutex unlock"); pthread_mutex_unlock(l);} while (0)
+       do { \
+               if (tdm_debug_mutex) \
+                       TDM_INFO("mutex unlock"); \
+               pthread_mutex_lock(&tdm_mutex_check_lock); \
+               tdm_mutex_locked = 0; \
+               pthread_mutex_unlock(&tdm_mutex_check_lock); \
+               pthread_mutex_unlock(l); \
+       } while (0)
+#ifdef TDM_CONFIG_MUTEX_TIMEOUT
+#define MUTEX_TIMEOUT_SEC 5
+#define _pthread_mutex_lock(l) \
+       do { \
+               if (tdm_debug_mutex) \
+                       TDM_INFO("mutex lock"); \
+               struct timespec rtime; \
+               clock_gettime(CLOCK_REALTIME, &rtime); \
+               rtime.tv_sec += MUTEX_TIMEOUT_SEC; \
+               if (pthread_mutex_timedlock(l, &rtime)) { \
+                       TDM_ERR("Mutex lock failed PID %d", getpid()); \
+                       _pthread_mutex_unlock(l); \
+               } \
+               else { \
+                       pthread_mutex_lock(&tdm_mutex_check_lock); \
+                       tdm_mutex_locked = 1; \
+                       pthread_mutex_unlock(&tdm_mutex_check_lock); \
+               } \
+       } while (0)
+#else //TDM_CONFIG_MUTEX_TIMEOUT
+#define _pthread_mutex_lock(l) \
+       do { \
+               if (tdm_debug_mutex) \
+                       TDM_INFO("mutex lock"); \
+               pthread_mutex_lock(l); \
+               pthread_mutex_lock(&tdm_mutex_check_lock); \
+               tdm_mutex_locked = 1; \
+               pthread_mutex_unlock(&tdm_mutex_check_lock); \
+       } while (0)
+#endif //TDM_CONFIG_MUTEX_TIMEOUT
+//#define TDM_MUTEX_IS_LOCKED() (tdm_mutex_locked == 1)
+static inline int TDM_MUTEX_IS_LOCKED(void)
+{
+       int ret;
+       pthread_mutex_lock(&tdm_mutex_check_lock);
+       ret = (tdm_mutex_locked == 1);
+       pthread_mutex_unlock(&tdm_mutex_check_lock);
+       return ret;
+}
+
+tdm_error
+_tdm_display_lock(tdm_display *dpy, const char *func);
+void
+_tdm_display_unlock(tdm_display *dpy, const char *func);
+
+#define tdm_display_lock(dpy)   _tdm_display_lock(dpy, __FUNCTION__)
+#define tdm_display_unlock(dpy)   _tdm_display_unlock(dpy, __FUNCTION__)
 
 #ifdef __cplusplus
 }