ref/unref display_buffer by using buffer's user_data 88/172688/2
authorBoram Park <boram1288.park@samsung.com>
Thu, 15 Mar 2018 06:02:47 +0000 (15:02 +0900)
committerBoram Park <boram1288.park@samsung.com>
Thu, 15 Mar 2018 07:35:32 +0000 (16:35 +0900)
Freeing a buffer before removing a buffer from DRM system makes a issue.
To ensure that a buffer is alive until removing it from DRM, do ref/unref
a buffer.

Change-Id: Iefd13945f91ce1bcb060bcd4ba21a4d5dbc53527

src/tdm_nexell.c
src/tdm_nexell.h
src/tdm_nexell_display.c

index 7d5439581d952252af31f3a9d1ec8e3882b0ee23..f905f8291bef45320f0e3cc2f58c4e3d891442db 100644 (file)
@@ -217,7 +217,6 @@ tdm_nexell_deinit(tdm_backend_data *bdata)
        _tdm_nexell_udev_deinit(nexell_data);
 #endif
 
-       tdm_nexell_display_destroy_buffer_list(nexell_data);
        tdm_nexell_display_destroy_output_list(nexell_data);
 
        if (nexell_data->plane_res)
index bc214c37643868c1a93ef4cef1ae9d369b2912bc..119970ac503af2690cd5f5dcc0868a57a2dede41 100644 (file)
@@ -130,7 +130,6 @@ void         tdm_nexell_display_update_output_status(tdm_nexell_data *nexell_dat
 tdm_error    tdm_nexell_display_create_output_list(tdm_nexell_data *nexell_data);
 void         tdm_nexell_display_destroy_output_list(tdm_nexell_data *nexell_data);
 tdm_error    tdm_nexell_display_create_layer_list(tdm_nexell_data *nexell_data);
-void         tdm_nexell_display_destroy_buffer_list(tdm_nexell_data *nexell_data);
 
 
 #endif /* _TDM_NEXELL_H_ */
index 008adeac55718574546511ccec5eea60a67365dd..5144988b18513d23a0de0de06da3c331973bc519 100644 (file)
@@ -8,6 +8,9 @@
 #include "tdm_nexell.h"
 #include "tdm_nexell_pp.h"
 
+static int tdm_nexell_buffer_key;
+#define TDM_NEXELL_BUFFER_KEY ((unsigned long)&tdm_nexell_buffer_key)
+
 #define MIN_WIDTH   32
 
 typedef struct _tdm_nexell_output_data tdm_nexell_output_data;
@@ -21,8 +24,7 @@ typedef enum {
 } tdm_nexell_event_type;
 
 typedef struct _tdm_nexell_display_buffer {
-       struct list_head link;
-
+       tdm_nexell_data *nexell_data;
        unsigned int fb_id;
        tbm_surface_h buffer;
        int width;
@@ -82,6 +84,89 @@ struct _tdm_nexell_layer_data {
        int display_buffer_changed;
 };
 
+static void
+_tdm_nexell_display_buffer_destroy(void *user_data)
+{
+       tdm_nexell_display_buffer *display_buffer = (tdm_nexell_display_buffer *)user_data;
+
+       if (display_buffer->fb_id > 0) {
+               int ret = drmModeRmFB(display_buffer->nexell_data->drm_fd, display_buffer->fb_id);
+               if (ret < 0) {
+                       TDM_ERR("rm fb failed");
+                       return;
+               }
+               TDM_DBG("drmModeRmFB success!!! fb_id:%d", display_buffer->fb_id);
+       } else
+               TDM_DBG("drmModeRmFB not called fb_id:%d", display_buffer->fb_id);
+
+       free(display_buffer);
+}
+
+static tdm_nexell_display_buffer *
+_tdm_nexell_display_get_buffer(tdm_nexell_data *nexell_data, tbm_surface_h buffer)
+{
+       tdm_nexell_display_buffer *display_buffer = NULL;
+
+       if (!tbm_surface_internal_get_user_data(buffer, TDM_NEXELL_BUFFER_KEY, (void **)&display_buffer)) {
+               unsigned int width;
+               unsigned int height;
+               unsigned int format;
+               unsigned int handles[4] = {0,};
+               unsigned int pitches[4] = {0,};
+               unsigned int offsets[4] = {0,};
+               unsigned int size;
+               tbm_bo bo;
+               int i, count, ret;
+
+               display_buffer = calloc(1, sizeof(tdm_nexell_display_buffer));
+               RETURN_VAL_IF_FAIL(display_buffer != NULL, NULL);
+
+               if (!tbm_surface_internal_add_user_data(buffer, TDM_NEXELL_BUFFER_KEY, _tdm_nexell_display_buffer_destroy)) {
+                       TDM_ERR("FAIL to create user_data for surface %p", buffer);
+                       free(display_buffer);
+                       return NULL;
+               }
+               if (!tbm_surface_internal_set_user_data(buffer, TDM_NEXELL_BUFFER_KEY, display_buffer)) {
+                       TDM_ERR("FAIL to set user_data for surface %p", buffer);
+                       tbm_surface_internal_delete_user_data(buffer, TDM_NEXELL_BUFFER_KEY);
+                       free(display_buffer);
+                       return NULL;
+               }
+
+               display_buffer->nexell_data = nexell_data;
+               display_buffer->buffer = buffer;
+
+               width = tbm_surface_get_width(buffer);
+               height = tbm_surface_get_height(buffer);
+               format = tbm_surface_get_format(buffer);
+               count = tbm_surface_internal_get_num_planes(format);
+               bo = tbm_surface_internal_get_bo(buffer, 0);
+               handles[0] = tbm_bo_get_handle(bo, TBM_DEVICE_DEFAULT).u32;
+               for (i = 1; i < count; i++)
+                       handles[i] = handles[0];
+
+               for (i = 0; i < count; i++)
+                       tbm_surface_internal_get_plane_data(buffer, i, &size, &offsets[i], &pitches[i]);
+
+               ret = drmModeAddFB2(nexell_data->drm_fd, width, height, format,
+                                                       handles, pitches, offsets, &display_buffer->fb_id, 0);
+               if (ret < 0) {
+                       TDM_ERR("add fb failed: %m");
+                       free(display_buffer);
+                       return NULL;
+               }
+               TDM_DBG("nexell_data->drm_fd : %d, display_buffer->fb_id:%u", nexell_data->drm_fd,
+                               display_buffer->fb_id);
+
+               if (IS_RGB(format))
+                       display_buffer->width = pitches[0] >> 2;
+               else
+                       display_buffer->width = pitches[0];
+       }
+
+       return display_buffer;
+}
+
 static drmModeModeInfoPtr
 _tdm_nexell_display_get_mode(tdm_nexell_output_data *output_data)
 {
@@ -106,19 +191,6 @@ _tdm_nexell_display_get_mode(tdm_nexell_output_data *output_data)
        return NULL;
 }
 
-static tdm_nexell_display_buffer *
-_tdm_nexell_display_find_buffer(tdm_nexell_data *nexell_data, tbm_surface_h buffer)
-{
-       tdm_nexell_display_buffer *display_buffer = NULL;
-
-       LIST_FOR_EACH_ENTRY(display_buffer, &nexell_data->buffer_list, link) {
-               if (display_buffer->buffer == buffer)
-                       return display_buffer;
-       }
-
-       return NULL;
-}
-
 static void
 _tdm_nexell_display_to_tdm_mode(drmModeModeInfoPtr drm_mode,
                              tdm_output_mode *tdm_mode)
@@ -245,18 +317,7 @@ _tdm_nexell_display_commit_primary_layer(tdm_nexell_layer_data *layer_data,
        } else if (layer_data->display_buffer_changed) {
                layer_data->display_buffer_changed = 0;
 
-               if (!layer_data->display_buffer) {
-                       if (drmModeSetCrtc(nexell_data->drm_fd, output_data->crtc_id,
-                                          0, 0, 0, NULL, 0, NULL)) {
-                               TDM_ERR("unset crtc failed: %m");
-                               return TDM_ERROR_OPERATION_FAILED;
-                       }
-
-                       if (output_data->status == TDM_OUTPUT_CONN_STATUS_MODE_SETTED)
-                               _tdm_nexell_output_update_status(output_data, TDM_OUTPUT_CONN_STATUS_CONNECTED);
-
-                       *do_waitvblank = 1;
-               } else {
+               if (layer_data->display_buffer) {
                        tdm_nexell_event_data *event_data = calloc(1, sizeof(tdm_nexell_event_data));
 
                        if (!event_data) {
@@ -275,6 +336,9 @@ _tdm_nexell_display_commit_primary_layer(tdm_nexell_layer_data *layer_data,
                        }
                        *do_waitvblank = 0;
                }
+       }else {
+               /* to call a user commit handler whenever committed */
+               *do_waitvblank = 1;
        }
 
        return TDM_ERROR_NONE;
@@ -659,44 +723,6 @@ failed:
 }
 #endif
 
-static void
-_tdm_nexell_display_cb_destroy_buffer(tbm_surface_h buffer, void *user_data)
-{
-       tdm_nexell_data *nexell_data;
-       tdm_nexell_display_buffer *display_buffer;
-       int ret;
-
-       if (!user_data) {
-               TDM_ERR("no user_data");
-               return;
-       }
-       if (!buffer) {
-               TDM_ERR("no buffer");
-               return;
-       }
-
-       nexell_data = (tdm_nexell_data *)user_data;
-
-       display_buffer = _tdm_nexell_display_find_buffer(nexell_data, buffer);
-       if (!display_buffer) {
-               TDM_ERR("no display_buffer");
-               return;
-       }
-       LIST_DEL(&display_buffer->link);
-
-       if (display_buffer->fb_id > 0) {
-               ret = drmModeRmFB(nexell_data->drm_fd, display_buffer->fb_id);
-               if (ret < 0) {
-                       TDM_ERR("rm fb failed");
-                       return;
-               }
-               TDM_DBG("drmModeRmFB success!!! fb_id:%d", display_buffer->fb_id);
-       } else
-               TDM_DBG("drmModeRmFB not called fb_id:%d", display_buffer->fb_id);
-
-       free(display_buffer);
-}
-
 tdm_error
 tdm_nexell_display_create_layer_list(tdm_nexell_data *nexell_data)
 {
@@ -737,6 +763,8 @@ tdm_nexell_display_destroy_output_list(tdm_nexell_data *nexell_data)
                        tdm_nexell_layer_data *l = NULL, *ll = NULL;
                        LIST_FOR_EACH_ENTRY_SAFE(l, ll, &o->layer_list, link) {
                                LIST_DEL(&l->link);
+                               if (l->display_buffer)
+                                       tbm_surface_internal_unref(l->display_buffer->buffer);
                                free(l);
                        }
                }
@@ -963,17 +991,6 @@ failed_create:
        return ret;
 }
 
-void
-tdm_nexell_display_destroy_buffer_list(tdm_nexell_data *nexell_data)
-{
-       tdm_nexell_display_buffer *b = NULL, *bb = NULL;
-
-       LIST_FOR_EACH_ENTRY_SAFE(b, bb, &nexell_data->buffer_list, link) {
-               tdm_buffer_remove_destroy_handler(b->buffer, _tdm_nexell_display_cb_destroy_buffer, nexell_data);
-               _tdm_nexell_display_cb_destroy_buffer(b->buffer, nexell_data);
-       }
-}
-
 tdm_error
 nexell_display_get_capability(tdm_backend_data *bdata, tdm_caps_display *caps)
 {
@@ -1712,74 +1729,24 @@ nexell_layer_set_buffer(tdm_layer *layer, tbm_surface_h buffer)
        tdm_nexell_layer_data *layer_data = layer;
        tdm_nexell_data *nexell_data;
        tdm_nexell_display_buffer *display_buffer;
-       tdm_error err = TDM_ERROR_NONE;
-       int ret, i, count;
 
        RETURN_VAL_IF_FAIL(layer_data, TDM_ERROR_INVALID_PARAMETER);
        RETURN_VAL_IF_FAIL(buffer, TDM_ERROR_INVALID_PARAMETER);
 
        nexell_data = layer_data->nexell_data;
 
-       display_buffer = _tdm_nexell_display_find_buffer(nexell_data, buffer);
+       display_buffer = _tdm_nexell_display_get_buffer(nexell_data, buffer);
        if (!display_buffer) {
-               display_buffer = calloc(1, sizeof(tdm_nexell_display_buffer));
-               if (!display_buffer) {
-                       TDM_ERR("alloc failed");
-                       return TDM_ERROR_OUT_OF_MEMORY;
-               }
-               display_buffer->buffer = buffer;
-
-               err = tdm_buffer_add_destroy_handler(buffer, _tdm_nexell_display_cb_destroy_buffer,
-                                                    nexell_data);
-               if (err != TDM_ERROR_NONE) {
-                       TDM_ERR("add destroy handler fail");
-                       free(display_buffer);
-                       return TDM_ERROR_OPERATION_FAILED;
-               }
-
-               LIST_ADDTAIL(&display_buffer->link, &nexell_data->buffer_list);
+               TDM_ERR("alloc failed");
+               return TDM_ERROR_OUT_OF_MEMORY;
        }
 
-       if (display_buffer->fb_id == 0) {
-               unsigned int width;
-               unsigned int height;
-               unsigned int format;
-               unsigned int handles[4] = {0,};
-               unsigned int pitches[4] = {0,};
-               unsigned int offsets[4] = {0,};
-               unsigned int size;
-               tbm_bo bo;
-
-               width = tbm_surface_get_width(buffer);
-               height = tbm_surface_get_height(buffer);
-               format = tbm_surface_get_format(buffer);
-
-               count = tbm_surface_internal_get_num_planes(format);
-
-               bo = tbm_surface_internal_get_bo(buffer, 0);
-               handles[0] = tbm_bo_get_handle(bo, TBM_DEVICE_DEFAULT).u32;
-               for (i = 1; i < count; i++)
-                       handles[i] = handles[0];
-
-               for (i = 0; i < count; i++)
-                       tbm_surface_internal_get_plane_data(buffer, i, &size, &offsets[i], &pitches[i]);
-
-               ret = drmModeAddFB2(nexell_data->drm_fd, width, height, format,
-                                   handles, pitches, offsets, &display_buffer->fb_id, 0);
-               if (ret < 0) {
-                       TDM_ERR("add fb failed: %m");
-                       return TDM_ERROR_OPERATION_FAILED;
-               }
-               TDM_DBG("nexell_data->drm_fd : %d, display_buffer->fb_id:%u", nexell_data->drm_fd,
-                       display_buffer->fb_id);
-
-               if (IS_RGB(format))
-                       display_buffer->width = pitches[0] >> 2;
-               else
-                       display_buffer->width = pitches[0];
-       }
+       if (layer_data->display_buffer)
+               tbm_surface_internal_unref(layer_data->display_buffer->buffer);
 
        layer_data->display_buffer = display_buffer;
+       tbm_surface_internal_ref(layer_data->display_buffer->buffer);
+
        layer_data->display_buffer_changed = 1;
 
        return TDM_ERROR_NONE;
@@ -1792,7 +1759,11 @@ nexell_layer_unset_buffer(tdm_layer *layer)
 
        RETURN_VAL_IF_FAIL(layer_data, TDM_ERROR_INVALID_PARAMETER);
 
-       layer_data->display_buffer = NULL;
+       if (!(layer_data->capabilities & TDM_LAYER_CAPABILITY_PRIMARY) && layer_data->display_buffer) {
+               tbm_surface_internal_unref(layer_data->display_buffer->buffer);
+               layer_data->display_buffer = NULL;
+       }
+
        layer_data->display_buffer_changed = 1;
 
        return TDM_ERROR_NONE;