add the reference count for tbm_surface 92/45892/1
authorSooChan Lim <sc1.lim@samsung.com>
Wed, 12 Aug 2015 05:27:24 +0000 (14:27 +0900)
committerSooChan Lim <sc1.lim@samsung.com>
Wed, 12 Aug 2015 08:03:39 +0000 (17:03 +0900)
manage the reference count of the tbm_surface.

Change-Id: I8a823314b9bf71d826d971cb4986df1d51b573ce

src/tbm_bufmgr_int.h [changed mode: 0644->0755]
src/tbm_surface_internal.c
src/tbm_surface_internal.h

old mode 100644 (file)
new mode 100755 (executable)
index 15b795c..c82646c
@@ -192,6 +192,8 @@ struct _tbm_surface {
 
     int planes_bo_idx[TBM_SURF_PLANE_MAX];
 
+    int refcnt;
+
     struct list_head item_link; /* link of surface */
 };
 
index e9740dd..9f7e4e5 100755 (executable)
@@ -156,6 +156,32 @@ _tbm_surface_internal_query_num_bos (tbm_format format)
     return ret;
 }
 
+static void
+_tbm_surface_internal_destroy (tbm_surface_h surface)
+{
+    int i;
+
+    surface = (struct _tbm_surface *)surface;
+
+    for (i = 0; i < surface->num_bos; i++)
+    {
+        tbm_bo_unref (surface->bos[i]);
+        surface->bos[i] = NULL;
+    }
+
+    LIST_DEL (&surface->item_link);
+
+    free (surface);
+    surface = NULL;
+
+    if(LIST_IS_EMPTY (&g_surface_list))
+    {
+        _deinit_surface_bufmgr ();
+        LIST_DELINIT (&g_surface_list);
+    }
+
+}
+
 
 int
 tbm_surface_internal_query_supported_formats (uint32_t **formats, uint32_t *num)
@@ -400,6 +426,7 @@ tbm_surface_internal_create_with_flags (int width, int height, int format, int f
     surf->info.size = _tbm_surface_internal_query_size (surf);
     surf->info.num_planes = tbm_surface_internal_get_num_planes(format);
     surf->num_bos = _tbm_surface_internal_query_num_bos(format);
+    surf->refcnt = 1;
 
     /* get size, stride and offset bo_idx*/
     for (i = 0; i < surf->info.num_planes; i++)
@@ -485,6 +512,7 @@ tbm_surface_internal_create_with_bos (tbm_surface_info_s *info, tbm_bo *bos, int
     surf->info.format = info->format;
     surf->info.bpp = info->bpp;
     surf->info.num_planes = info->num_planes;
+    surf->refcnt = 1;
 
     /* get size, stride and offset */
     for (i = 0; i < info->num_planes; i++)
@@ -556,35 +584,54 @@ bail1:
 void
 tbm_surface_internal_destroy (tbm_surface_h surface)
 {
-    int i;
-
     if (!surface)
         return;
 
     _tbm_surface_mutex_lock();
 
-    surface = (struct _tbm_surface *)surface;
+    surface->refcnt--;
 
-    for (i = 0; i < surface->num_bos; i++)
-    {
-        tbm_bo_unref (surface->bos[i]);
-        surface->bos[i] = NULL;
-    }
+    if (surface->refcnt > 0)
+        return;
 
-    LIST_DEL (&surface->item_link);
+    if (surface->refcnt == 0)
+        _tbm_surface_internal_destroy(surface);
 
-    free (surface);
-    surface = NULL;
+    _tbm_surface_mutex_unlock();
+}
 
-    if(LIST_IS_EMPTY (&g_surface_list))
-    {
-        _deinit_surface_bufmgr ();
-        LIST_DELINIT (&g_surface_list);
-    }
+
+void
+tbm_surface_internal_ref (tbm_surface_h surface)
+{
+    _tbm_surface_mutex_lock();
+
+    TBM_RETURN_IF_FAIL (surface);
+
+    _tbm_surface_mutex_lock();
+
+    surface->refcnt++;
 
     _tbm_surface_mutex_unlock();
 }
 
+void
+tbm_surface_internal_unref (tbm_surface_h surface)
+{
+    _tbm_surface_mutex_lock();
+
+    TBM_RETURN_IF_FAIL (surface);
+
+    surface->refcnt--;
+
+    if (surface->refcnt > 0)
+        return;
+
+    if (surface->refcnt == 0)
+        _tbm_surface_internal_destroy(surface);
+
+    _tbm_surface_mutex_unlock();
+}
 
 int
 tbm_surface_internal_get_num_bos (tbm_surface_h surface)
index cc94cdb..a9ada40 100755 (executable)
@@ -161,6 +161,18 @@ tbm_surface_h tbm_surface_internal_create_with_bos (tbm_surface_info_s *info, tb
 void tbm_surface_internal_destroy (tbm_surface_h surface);
 
 /**
+ * @brief reference the tbm surface
+    TODO:
+ */
+void tbm_surface_internal_ref (tbm_surface_h surface);
+
+/**
+ * @brief unreference the tbm surface
+    TODO:
+ */
+void tbm_surface_internal_unref (tbm_surface_h surface);
+
+/**
  * @brief Gets the number of buffer objects associated with the tbm_surface.
  * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
  * @param[in] surface : the tbm_surface_h