virtual: add protocol to share wl_buffer(tbm_surface) with tdm_client
[platform/core/uifw/libtdm.git] / client / tdm_client.c
index acd832e..53434f1 100644 (file)
@@ -33,6 +33,8 @@
  *
 **************************************************************************/
 
+#define WL_HIDE_DEPRECATED
+
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
@@ -53,6 +55,8 @@
 #include "tdm.h"
 #include "tdm_private.h"
 
+#define TDM_ARRAY_NTH_DATA(array, type, n) (((type*)((array)->data)) + n)
+
 typedef struct _tdm_private_client_vblank tdm_private_client_vblank;
 
 typedef struct _tdm_private_client {
@@ -89,9 +93,15 @@ typedef struct _tdm_private_client_output {
        struct list_head link;
 } tdm_private_client_output;
 
+typedef struct _tdm_private_client_buffer {
+       struct list_head link;
+       struct wl_buffer *wl_buffer;
+} tdm_private_client_buffer;
+
 typedef struct _tdm_private_client_voutput {
     tdm_private_client_output base;
        struct wl_tdm_voutput *wl_voutput;
+       struct list_head commit_handler_list;
 
        struct
        {
@@ -99,7 +109,14 @@ typedef struct _tdm_private_client_voutput {
                tdm_client_output_mode *modes;
        } available_modes;
 
+       unsigned int mmwidth;
+       unsigned int mmheight;
+
        uint32_t msg;
+
+       struct list_head buffer_list;
+       tbm_bufmgr bufmgr;
+       tdm_private_client_buffer *commit_buffer;
 } tdm_private_client_voutput;
 
 struct _tdm_private_client_vblank {
@@ -148,6 +165,17 @@ typedef struct _tdm_client_wait_info {
        struct list_head call_link;
 } tdm_client_wait_info;
 
+typedef struct _tdm_client_voutput_commit_handler_info
+{
+       tdm_private_client_voutput *private_voutput;
+
+       tdm_client_voutput_commit_handler func;
+       void *user_data;
+
+       struct list_head link;
+       struct list_head call_link;
+} tdm_client_voutput_commit_handler_info;
+
 static unsigned int
 _tdm_client_check_wl_error(tdm_private_client *private_client, const char *func, int line)
 {
@@ -1605,6 +1633,250 @@ tdm_client_vblank_is_waiting(tdm_client_vblank *vblank)
        return (LIST_LENGTH(&private_vblank->wait_list) > 0) ? 1 : 0;
 }
 
+static tbm_surface_h
+_tdm_client_voutput_create_surface_from_param(tbm_bufmgr bufmgr,
+                                                        int is_fd,
+                                                        int32_t width,
+                                                        int32_t height,
+                                                        uint32_t format,
+                                                        int32_t bpp,
+                                                        int32_t size,
+                                                        int32_t num_plane,
+                                                        struct wl_array *plane_buf_idx,
+                                                        struct wl_array *plane_offset,
+                                                        struct wl_array *plane_stride,
+                                                        struct wl_array *plane_size,
+                                                        uint32_t flags,
+                                                        int32_t num_buf,
+                                                        uint32_t buf0,
+                                                        uint32_t buf1,
+                                                        uint32_t buf2)
+{
+       int32_t names[TBM_SURF_PLANE_MAX] = { -1, -1, -1, -1};
+       tbm_surface_info_s info = { 0, };
+       tbm_bo bos[TBM_SURF_PLANE_MAX];
+       int i, numPlane, numName;
+       tbm_surface_h tbm_surface;
+
+       numPlane = tbm_surface_internal_get_num_planes(format);
+       TDM_RETURN_VAL_IF_FAIL(numPlane == num_plane, NULL);
+
+       info.width = width;
+       info.height = height;
+       info.format = format;
+       info.bpp = bpp;
+       info.size = size;
+       info.num_planes = numPlane;
+
+       /*Fill plane info*/
+       for (i = 0; i < numPlane; i++) {
+               info.planes[i].offset = *TDM_ARRAY_NTH_DATA(plane_offset, int32_t, i);
+               info.planes[i].stride = *TDM_ARRAY_NTH_DATA(plane_stride, int32_t, i);
+               info.planes[i].size = *TDM_ARRAY_NTH_DATA(plane_size, int32_t, i);
+       }
+
+       /*Fill buffer*/
+       numName = num_buf;
+       names[0] = buf0;
+       names[1] = buf1;
+       names[2] = buf2;
+
+       for (i = 0; i < numName; i++) {
+               if (is_fd)
+                       bos[i] = tbm_bo_import_fd(bufmgr, names[i]);
+               else
+                       bos[i] = tbm_bo_import(bufmgr, names[i]);
+       }
+
+       tbm_surface = tbm_surface_internal_create_with_bos(&info, bos, numName);
+       if (tbm_surface == NULL) {
+               if (is_fd) {
+                       close(buf0);
+                       close(buf1);
+                       close(buf2);
+               }
+               return NULL;
+       }
+
+       if (is_fd) {
+               close(buf0);
+               close(buf1);
+               close(buf2);
+       }
+
+       for (i = 0; i < numName; i++)
+               tbm_bo_unref(bos[i]);
+
+       return tbm_surface;
+}
+static void
+tdm_client_voutput_cb_buffer_import_with_id(void *data,
+               struct wl_tdm_voutput *wl_voutput,
+               struct wl_buffer *wl_buffer,
+               int32_t width,
+               int32_t height,
+               uint32_t format,
+               int32_t bpp,
+               int32_t size,
+               int32_t num_plane,
+               struct wl_array *plane_buf_idx,
+               struct wl_array *plane_offset,
+               struct wl_array *plane_stride,
+               struct wl_array *plane_size,
+               uint32_t flags,
+               int32_t num_buf,
+               uint32_t buf0,
+               uint32_t buf1,
+               uint32_t buf2)
+{
+       tdm_private_client_voutput *private_voutput = (tdm_private_client_voutput *)data;
+       tdm_private_client_buffer *buffer = NULL;
+       tbm_surface_h tbm_surface;
+
+       TDM_RETURN_IF_FAIL(private_voutput != NULL);
+
+       buffer = calloc(1, sizeof *buffer);
+       TDM_RETURN_IF_FAIL(buffer != NULL);
+
+       tbm_surface = _tdm_client_voutput_create_surface_from_param(private_voutput->bufmgr, 0,
+                             width, height, format, bpp, size,
+                             num_plane,
+                             plane_buf_idx, plane_offset, plane_stride, plane_size,
+                             0,
+                             num_buf,
+                             buf0, buf1, buf2);
+       TDM_GOTO_IF_FAIL(tbm_surface != NULL, fail);
+
+       tbm_surface_internal_ref(tbm_surface);
+       wl_buffer_set_user_data(wl_buffer, tbm_surface);
+
+       buffer->wl_buffer = wl_buffer;
+
+       LIST_ADDTAIL(&buffer->link, &private_voutput->buffer_list);
+
+       return;
+
+fail:
+       if (buffer)
+               free(buffer);
+
+       if (wl_buffer)
+               wl_buffer_destroy(wl_buffer);
+}
+
+static void
+tdm_client_voutput_cb_buffer_import_with_fd(void *data,
+               struct wl_tdm_voutput *wl_voutput,
+               struct wl_buffer *wl_buffer,
+               int32_t width,
+               int32_t height,
+               uint32_t format,
+               int32_t bpp,
+               int32_t size,
+               int32_t num_plane,
+               struct wl_array *plane_buf_idx,
+               struct wl_array *plane_offset,
+               struct wl_array *plane_stride,
+               struct wl_array *plane_size,
+               uint32_t flags,
+               int32_t num_buf,
+               int32_t buf0,
+               int32_t buf1,
+               int32_t buf2)
+{
+       tdm_private_client_voutput *private_voutput = (tdm_private_client_voutput *)data;
+       tdm_private_client_buffer *buffer = NULL;
+       tbm_surface_h tbm_surface;
+
+       TDM_RETURN_IF_FAIL(private_voutput != NULL);
+
+       buffer = calloc(1, sizeof *buffer);
+       TDM_RETURN_IF_FAIL(buffer != NULL);
+
+       tbm_surface = _tdm_client_voutput_create_surface_from_param(private_voutput->bufmgr, 1,
+                             width, height, format, bpp, size,
+                             num_plane,
+                             plane_buf_idx, plane_offset, plane_stride, plane_size,
+                             0,
+                             num_buf,
+                             buf0, buf1, buf2);
+       TDM_GOTO_IF_FAIL(tbm_surface != NULL, fail);
+
+       tbm_surface_internal_ref(tbm_surface);
+       wl_buffer_set_user_data(wl_buffer, tbm_surface);
+
+       buffer->wl_buffer = wl_buffer;
+
+       LIST_ADDTAIL(&buffer->link, &private_voutput->buffer_list);
+
+       return;
+
+fail:
+       if (buffer)
+               free(buffer);
+
+       if (wl_buffer)
+               wl_buffer_destroy(wl_buffer);
+}
+
+static void
+tdm_client_voutput_cb_buffer_destroy(void *data,
+               struct wl_tdm_voutput *wl_voutput,
+               struct wl_buffer *wl_buffer)
+{
+       tdm_private_client_voutput *private_voutput = (tdm_private_client_voutput *)data;
+       tdm_private_client_buffer *cb = NULL, *cbb = NULL;
+       tbm_surface_h tbm_surface = NULL;
+
+       TDM_RETURN_IF_FAIL(private_voutput != NULL);
+
+       LIST_FOR_EACH_ENTRY_SAFE(cb, cbb, &private_voutput->buffer_list, link) {
+               if (wl_buffer == cb->wl_buffer) {
+                       LIST_DEL(&cb->link);
+
+                       tbm_surface = (tbm_surface_h)wl_buffer_get_user_data(wl_buffer);
+                       if (tbm_surface)
+                               tbm_surface_internal_unref(tbm_surface);
+
+                       wl_buffer_set_user_data(wl_buffer, NULL);
+                       wl_buffer_destroy(wl_buffer);
+
+                       free(cb);
+
+                       break;
+               }
+       }
+
+       return;
+}
+
+void
+tdm_client_voutput_cb_commit(void *data, struct wl_tdm_voutput *wl_voutput)
+{
+       tdm_private_client_voutput *private_voutput;
+       tdm_private_client *private_client;
+       tdm_client_voutput_commit_handler_info *h = NULL, *hh = NULL;
+       struct list_head call_list;
+
+       private_voutput = (tdm_private_client_voutput *)data;
+       TDM_RETURN_IF_FAIL(private_voutput != NULL);
+
+       private_client = private_voutput->base.private_client;
+
+       LIST_INITHEAD(&call_list);
+
+       LIST_FOR_EACH_ENTRY(h, &private_voutput->commit_handler_list, link) {
+               LIST_ADDTAIL(&h->call_link, &call_list);
+       }
+
+       pthread_mutex_unlock(&private_client->lock);
+       LIST_FOR_EACH_ENTRY_SAFE(h, hh, &call_list, call_link) {
+               if (h->func)
+                       h->func(private_voutput, h->user_data);
+       }
+       pthread_mutex_lock(&private_client->lock);
+}
+
 void
 tdm_client_voutput_cb_ack_message(void *data, struct wl_tdm_voutput *wl_voutput, uint32_t msg)
 {
@@ -1614,6 +1886,10 @@ tdm_client_voutput_cb_ack_message(void *data, struct wl_tdm_voutput *wl_voutput,
 }
 
 static const struct wl_tdm_voutput_listener tdm_client_voutput_lisntener = {
+       tdm_client_voutput_cb_buffer_import_with_id,
+       tdm_client_voutput_cb_buffer_import_with_fd,
+       tdm_client_voutput_cb_buffer_destroy,
+       tdm_client_voutput_cb_commit,
        tdm_client_voutput_cb_ack_message
 };
 
@@ -1680,10 +1956,26 @@ tdm_client_create_voutput(tdm_client *client, const char *name, tdm_error *error
                TDM_ERR("alloc failed");
                if (error)
                        *error = TDM_ERROR_OUT_OF_MEMORY;
+               pthread_mutex_unlock(&private_client->lock);
                return NULL;
                /* LOCV_EXCL_STOP */
        }
 
+       private_voutput->bufmgr = tbm_bufmgr_init(-1);
+       if (private_voutput->bufmgr == NULL) {
+               /* LCOV_EXCL_START */
+               TDM_ERR("fail tbm_bufmgr_init");
+               free(private_voutput);
+               if (error)
+                       *error = TDM_ERROR_OUT_OF_MEMORY;
+               pthread_mutex_unlock(&private_client->lock);
+               return NULL;
+               /* LCOV_EXCL_STOP */
+       }
+
+       LIST_INITHEAD(&private_voutput->commit_handler_list);
+       LIST_INITHEAD(&private_voutput->buffer_list);
+
        private_voutput->base.private_client = private_client;
 
        private_voutput->wl_voutput = wl_tdm_create_voutput((struct wl_tdm *)wrapper, private_voutput->base.name);
@@ -1733,10 +2025,43 @@ void
 tdm_client_voutput_destroy(tdm_client_voutput *voutput)
 {
        tdm_private_client_voutput *private_voutput = (tdm_private_client_voutput *)voutput;
+       tdm_client_voutput_commit_handler_info *h = NULL, *hh = NULL;
 
        if (!private_voutput)
                return;
 
+       if (!(LIST_IS_EMPTY(&private_voutput->buffer_list))) {
+               tdm_private_client_buffer *cb = NULL, *cbb = NULL;
+
+               LIST_FOR_EACH_ENTRY_SAFE(cb, cbb, &private_voutput->buffer_list, link) {
+                       tbm_surface_h tbm_surface = NULL;
+
+                       if (!cb) continue;
+
+                       LIST_DEL(&cb->link);
+
+                       tbm_surface = (tbm_surface_h)wl_buffer_get_user_data(cb->wl_buffer);
+                       if (tbm_surface)
+                               tbm_surface_internal_unref(tbm_surface);
+
+                       wl_buffer_set_user_data(cb->wl_buffer, NULL);
+                       wl_buffer_destroy(cb->wl_buffer);
+
+                       free(cb);
+               }
+       }
+
+       if (private_voutput->bufmgr)
+               tbm_bufmgr_deinit(private_voutput->bufmgr);
+
+       if (private_voutput->available_modes.modes)
+               free(private_voutput->available_modes.modes);
+
+       LIST_FOR_EACH_ENTRY_SAFE(h, hh, &private_voutput->commit_handler_list, link) {
+               LIST_DEL(&h->link);
+               free(h);
+       }
+
        wl_tdm_voutput_destroy(private_voutput->wl_voutput);
 
        free(private_voutput);
@@ -1772,11 +2097,108 @@ tdm_client_voutput_set_available_modes(tdm_client_voutput *voutput, const tdm_cl
 }
 
 tdm_error
-tdm_client_voutput_set_physical_size(tdm_client_voutput *voutput, int mmWidth, int mmHeight)
+tdm_client_voutput_set_physical_size(tdm_client_voutput *voutput, unsigned int mmWidth, unsigned int mmHeight)
+{
+       tdm_private_client_voutput *private_voutput;
+
+       TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
+       TDM_RETURN_VAL_IF_FAIL(mmWidth != 0, TDM_ERROR_INVALID_PARAMETER);
+       TDM_RETURN_VAL_IF_FAIL(mmHeight != 0, TDM_ERROR_INVALID_PARAMETER);
+
+       private_voutput = (tdm_private_client_voutput *)voutput;
+
+       if (private_voutput->base.connection == TDM_OUTPUT_CONN_STATUS_CONNECTED)
+               return TDM_ERROR_BAD_REQUEST;
+
+       private_voutput->mmwidth = mmWidth;
+       private_voutput->mmheight = mmHeight;
+
+       return TDM_ERROR_NONE;
+}
+
+tdm_error
+tdm_client_voutput_add_commit_handler(tdm_client_voutput *voutput,
+                                                                         tdm_client_voutput_commit_handler func,
+                                                                         void *user_data)
+{
+       tdm_private_client_voutput *private_voutput;
+       tdm_private_client *private_client;
+       tdm_client_voutput_commit_handler_info *h = NULL;
+
+       TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
+       TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
+
+       private_voutput = (tdm_private_client_voutput *)voutput;
+       private_client = private_voutput->base.private_client;
+
+       LIST_FOR_EACH_ENTRY(h, &private_voutput->commit_handler_list, link) {
+               if (h->func == func && h->user_data == user_data) {
+                       TDM_ERR("can't add twice");
+                       return TDM_ERROR_BAD_REQUEST;
+               }
+       }
+
+       h = calloc(1, sizeof *h);
+       TDM_RETURN_VAL_IF_FAIL(h != NULL, TDM_ERROR_OUT_OF_MEMORY);
+
+       pthread_mutex_lock(&private_client->lock);
+
+       if (CHECK_WL_PROTOCOL_ERROR(private_client)) {
+               free(h);
+               pthread_mutex_unlock(&private_client->lock);
+               return TDM_ERROR_PROTOCOL_ERROR;
+       }
+
+       h->private_voutput = private_voutput;
+       h->func = func;
+       h->user_data = user_data;
+       LIST_ADDTAIL(&h->link, &private_voutput->commit_handler_list);
+       LIST_INITHEAD(&h->call_link);
+
+       pthread_mutex_unlock(&private_client->lock);
+
+       return TDM_ERROR_NONE;
+}
+
+void
+tdm_client_voutput_remove_commit_handler(tdm_client_voutput *voutput,
+                                                                                tdm_client_voutput_commit_handler func,
+                                                                                void *user_data)
+{
+       tdm_private_client_voutput *private_voutput;
+       tdm_private_client *private_client;
+       tdm_client_voutput_commit_handler_info *h = NULL;
+
+       TDM_RETURN_IF_FAIL(voutput != NULL);
+       TDM_RETURN_IF_FAIL(func != NULL);
+
+       private_voutput = (tdm_private_client_voutput *)voutput;
+       private_client = private_voutput->base.private_client;
+
+       pthread_mutex_lock(&private_client->lock);
+
+       LIST_FOR_EACH_ENTRY(h, &private_voutput->commit_handler_list, link) {
+               if (h->func != func || h->user_data != user_data)
+                       continue;
+
+               LIST_DEL(&h->link);
+               free(h);
+
+               pthread_mutex_unlock(&private_client->lock);
+       }
+
+       pthread_mutex_unlock(&private_client->lock);
+}
+
+tdm_error
+tdm_client_voutput_commit_done(tdm_client_voutput *voutput)
 {
+       tdm_private_client_voutput *private_voutput;
+
        TDM_RETURN_VAL_IF_FAIL(voutput != NULL, TDM_ERROR_INVALID_PARAMETER);
-       TDM_RETURN_VAL_IF_FAIL(mmWidth > 0, TDM_ERROR_INVALID_PARAMETER);
-       TDM_RETURN_VAL_IF_FAIL(mmHeight > 0, TDM_ERROR_INVALID_PARAMETER);
+
+       private_voutput = (tdm_private_client_voutput *)voutput;
+       wl_tdm_voutput_commit_done(private_voutput->wl_voutput);
 
        return TDM_ERROR_NONE;
 }
@@ -1821,13 +2243,30 @@ tdm_client_output_set_mode(tdm_client_output *output, const tdm_client_output_mo
        return TDM_ERROR_NONE;
 }
 
+void
+_tdm_client_voutput_send_available_modes(tdm_private_client_voutput *private_voutput)
+{
+       tdm_client_output_mode *modes, *mode;
+       struct wl_array array;
+       int i, size;
+
+       modes = private_voutput->available_modes.modes;
+       size = sizeof(tdm_client_output_mode);
+
+       wl_array_init(&array);
+       for (i = 0; i < private_voutput->available_modes.count; i++) {
+               mode = wl_array_add(&array, size);
+               memcpy(mode, &modes[i], size);
+       }
+       wl_tdm_voutput_set_available_modes(private_voutput->wl_voutput, &array);
+       wl_array_release(&array);
+}
+
 tdm_error
 tdm_client_output_connect(tdm_client_output *output)
 {
        tdm_private_client_output *private_output;
        tdm_private_client_voutput *private_voutput;
-       tdm_client_output_mode *modes;
-       int i;
 
        TDM_RETURN_VAL_IF_FAIL(output != NULL, TDM_ERROR_INVALID_PARAMETER);
 
@@ -1839,19 +2278,9 @@ tdm_client_output_connect(tdm_client_output *output)
 
        private_output->connection = TDM_OUTPUT_CONN_STATUS_CONNECTED;
 
-       modes = private_voutput->available_modes.modes;
-       for (i = 0; i < private_voutput->available_modes.count; i++)
-       {
-               wl_tdm_voutput_set_available_modes(private_voutput->wl_voutput, i,
-                                                                                  modes[i].clock, modes[i].hdisplay,
-                                                                                  modes[i].hsync_start, modes[i].hsync_end,
-                                                                                  modes[i].htotal, modes[i].hskew,
-                                                                                  modes[i].vdisplay, modes[i].vsync_start,
-                                                                                  modes[i].vsync_end, modes[i].vtotal,
-                                                                                  modes[i].vscan, modes[i].vrefresh,
-                                                                                  modes[i].flags, modes[i].type,
-                                                                                  modes[i].name);
-       }
+       _tdm_client_voutput_send_available_modes(private_voutput);
+
+       wl_tdm_voutput_set_physical_size(private_voutput->wl_voutput, private_voutput->mmwidth, private_voutput->mmheight);
 
        wl_tdm_voutput_connect(private_voutput->wl_voutput);