server: check null of current mode for voutput set mode
[platform/core/uifw/libtdm.git] / src / tdm_server.c
index 31ddf14..6c4c6ab 100644 (file)
@@ -72,6 +72,7 @@ typedef struct _tdm_server_voutput_buffer {
        struct list_head link;
        struct wl_resource *wl_buffer;
        tbm_surface_h buffer;
+       int need_reset;
 } tdm_server_voutput_buffer;
 
 typedef struct _tdm_server_voutput_info {
@@ -93,6 +94,7 @@ typedef struct _tdm_server_voutput_info {
        struct list_head buffer_list;
        tdm_server_voutput_buffer *attach_buffer;
        int committing;
+       unsigned int request_commit;
 } tdm_server_voutput_info;
 
 typedef struct _tdm_server_vblank_info {
@@ -869,8 +871,8 @@ _tdm_voutput_cb_set_mode(struct wl_client *client, struct wl_resource *resource,
        tdm_server_voutput_info *voutput_info = NULL;
        tdm_output *output = NULL;
        tdm_output_conn_status status = TDM_OUTPUT_CONN_STATUS_DISCONNECTED;
-       const tdm_output_mode *modes, *mode;
-
+       const tdm_output_mode *modes, *mode, *current_mode;
+       tdm_server_voutput_buffer *vb = NULL, *vbb = NULL;
        int count = 0;
        tdm_error ret;
 
@@ -888,8 +890,21 @@ _tdm_voutput_cb_set_mode(struct wl_client *client, struct wl_resource *resource,
        mode = &modes[index];
        TDM_DBG("mode set request to index:%d (%dx%d, %d)", index, mode->hdisplay, mode->vdisplay, mode->vrefresh);
 
-       ret = tdm_output_set_mode(output, mode);
+       ret = tdm_output_get_mode(output, &current_mode);
        TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
+       if (mode == current_mode) {
+               TDM_DBG("same mode");
+               return;
+       }
+
+       if (current_mode && ((mode->hdisplay != current_mode->hdisplay) || (mode->vdisplay != current_mode->vdisplay))) {
+               LIST_FOR_EACH_ENTRY_SAFE(vb, vbb, &voutput_info->buffer_list, link) {
+                       if (vb->wl_buffer == voutput_info->attach_buffer->wl_buffer)
+                               voutput_info->attach_buffer->need_reset = 1;
+                       else
+                               wl_tdm_voutput_send_destroy_buffer(voutput_info->resource, vb->wl_buffer);
+               }
+       }
 
        tdm_output_request_mode_set(voutput_info->output, index);
 }
@@ -902,9 +917,12 @@ _tdm_voutput_cb_connect(struct wl_client *client, struct wl_resource *resource)
        voutput_info = wl_resource_get_user_data(resource);
        voutput_info->status = TDM_OUTPUT_CONN_STATUS_CONNECTED;
 
+       voutput_info->request_commit = 1;
+
        tdm_voutput_set_physical_size(voutput_info->voutput, voutput_info->mmwidth, voutput_info->mmheight);
        tdm_voutput_set_available_mode(voutput_info->voutput, voutput_info->available_modes.modes, voutput_info->available_modes.count);
        tdm_voutput_connect(voutput_info->voutput);
+       tdm_output_set_voutput_commit(voutput_info->voutput);
 }
 
 static void
@@ -922,11 +940,19 @@ _tdm_voutput_cb_disconnect(struct wl_client *client, struct wl_resource *resourc
        voutput_info->mmwidth = 0;
        voutput_info->mmheight = 0;
 
+       if (voutput_info->request_commit == 1) {
+               tdm_output_unset_voutput_commit(voutput_info->voutput);
+               voutput_info->request_commit = 0;
+       }
+
        if (voutput_info->attach_buffer) {
                tbm_surface_h buffer = voutput_info->attach_buffer->buffer;
                tbm_surface_internal_unref(buffer);
                voutput_info->committing = 0;
+               if (voutput_info->attach_buffer->need_reset)
+                       wl_tdm_voutput_send_destroy_buffer(voutput_info->resource, voutput_info->attach_buffer->wl_buffer);
                voutput_info->attach_buffer = NULL;
+               tdm_voutput_commit_done(voutput_info->voutput);
        }
 
        tdm_voutput_disconnect(voutput_info->voutput);
@@ -947,8 +973,13 @@ _tdm_voutput_cb_commit_done(struct wl_client *client, struct wl_resource *resour
        buffer = voutput_info->attach_buffer->buffer;
        tbm_surface_internal_unref(buffer);
        voutput_info->committing = 0;
+       if (voutput_info->attach_buffer->need_reset)
+               wl_tdm_voutput_send_destroy_buffer(voutput_info->resource, voutput_info->attach_buffer->wl_buffer);
        voutput_info->attach_buffer = NULL;
 
+       if (voutput_info->request_commit == 1)
+               tdm_output_set_voutput_commit(voutput_info->voutput);
+
        tdm_voutput_commit_done(voutput_info->voutput);
 }
 
@@ -1136,7 +1167,6 @@ _tdm_output_get_voutput_buffer(tdm_server_voutput_info *voutput_info, tbm_surfac
 
        LIST_FOR_EACH_ENTRY(vb, &voutput_info->buffer_list, link) {
                if (vb && vb->buffer == buffer) {
-                       tbm_surface_internal_ref(vb->buffer);
                        return vb;
                }
        }
@@ -1194,6 +1224,7 @@ tdm_voutput_attach_buffer(tdm_voutput *voutput, tbm_surface_h buffer)
 
        voutput_info->attach_buffer = voutput_buffer;
 
+       tbm_surface_internal_ref(buffer);
        wl_tdm_voutput_send_attach_buffer(voutput_info->resource, voutput_buffer->wl_buffer);
 
        return TDM_ERROR_NONE;
@@ -1217,7 +1248,6 @@ tdm_voutput_commit_buffer(tdm_voutput *voutput)
        TDM_RETURN_VAL_IF_FAIL(voutput_info->attach_buffer != NULL, TDM_ERROR_OPERATION_FAILED);
        TDM_RETURN_VAL_IF_FAIL(voutput_info->committing == 0, TDM_ERROR_OPERATION_FAILED);
 
-       tbm_surface_internal_ref(voutput_info->attach_buffer->buffer);
        voutput_info->committing = 1;
 
        wl_tdm_voutput_send_commit(voutput_info->resource);
@@ -1237,10 +1267,20 @@ tdm_voutput_cb_resource_destroy(struct wl_resource *resource)
 
        voutput = voutput_info->voutput;
 
-       if (voutput)
+       if (voutput_info->request_commit)
+               tdm_output_unset_voutput_commit(voutput_info->voutput);
+
+       if (voutput) {
+               if (voutput_info->request_commit == 1) {
+                       tdm_output_unset_voutput_commit(voutput_info->voutput);
+                       voutput_info->request_commit = 0;
+                       tdm_voutput_disconnect(voutput_info->voutput);
+               }
+
                ret = tdm_voutput_destroy(voutput);
-       if (ret != TDM_ERROR_NONE)
-               TDM_ERR("_tdm_voutput_cb_destroy fail");
+               if (ret != TDM_ERROR_NONE)
+                       TDM_ERR("_tdm_voutput_cb_destroy fail");
+       }
 
        if (voutput_info->attach_buffer) {
                tbm_surface_h buffer = voutput_info->attach_buffer->buffer;