screenshot: use mutex lock for screenshot
[platform/core/api/efl-util.git] / src / efl_util.c
index 2436df1..f5ffd75 100644 (file)
@@ -30,6 +30,7 @@
 #include <tbm_surface_internal.h>
 #include <Elementary.h>
 #include <Ecore_Evas.h>
+#include <pthread.h>
 
 #include <Ecore_Wayland.h>
 #include <wayland-client.h>
@@ -1451,6 +1452,57 @@ struct _efl_util_screenshot_h
 
 /* scrrenshot handle */
 static efl_util_screenshot_h g_screenshot;
+static Eina_Bool shot_mutex_init;
+static pthread_mutex_t shot_lock;
+
+static Eina_Bool
+_screenshot_mutex_init(void)
+{
+   if (shot_mutex_init)
+     return EINA_TRUE;
+
+   if (pthread_mutex_init(&shot_lock, NULL))
+     {
+        fprintf(stderr, "[screenshot] fail: mutex init"); /*LCOV_EXCL_LINE*/
+        return EINA_FALSE; /*LCOV_EXCL_LINE*/
+     }
+
+   shot_mutex_init = EINA_TRUE;
+
+   return EINA_TRUE;
+}
+
+static Eina_Bool
+_screenshot_mutex_destory(void)
+{
+   if (!shot_mutex_init)
+     return EINA_TRUE;
+
+   if (pthread_mutex_destroy(&shot_lock))
+     {
+        fprintf(stderr, "[screenshot] fail: mutex destory"); /*LCOV_EXCL_LINE*/
+        return EINA_FALSE; /*LCOV_EXCL_LINE*/
+     }
+
+   shot_mutex_init = EINA_FALSE;
+
+   return EINA_TRUE;
+}
+
+void
+_screenshot_mutex_lock(void)
+{
+   if (!_screenshot_mutex_init())
+     return;
+
+   pthread_mutex_lock(&shot_lock);
+}
+
+void
+_screenshot_mutex_unlock(void)
+{
+   pthread_mutex_unlock(&shot_lock);
+}
 
 API efl_util_screenshot_h
 efl_util_screenshot_initialize(int width, int height)
@@ -1463,12 +1515,15 @@ efl_util_screenshot_initialize(int width, int height)
    EINA_SAFETY_ON_FALSE_GOTO(width > 0, fail_param);
    EINA_SAFETY_ON_FALSE_GOTO(height > 0, fail_param);
 
+   _screenshot_mutex_lock();
+
    if (!_eflutil.wl.shot.screenshooter)
      {
         ret = _wl_init();
         if (ret == (int)EINA_FALSE)
           {
              set_last_result(EFL_UTIL_ERROR_INVALID_PARAMETER);
+             _screenshot_mutex_unlock();
              return NULL;
           }
         wl_display_roundtrip_queue(_eflutil.wl.dpy, _eflutil.wl.queue);
@@ -1509,6 +1564,8 @@ efl_util_screenshot_initialize(int width, int height)
              g_screenshot->height = height;
           }
 
+        _screenshot_mutex_unlock();
+
         return g_screenshot;
      }
 
@@ -1526,6 +1583,8 @@ efl_util_screenshot_initialize(int width, int height)
 
    screenshooter_set_user_data(_eflutil.wl.shot.screenshooter, &screenshot->shot_done);
 
+   _screenshot_mutex_unlock();
+
    return g_screenshot;
 
 fail_param:
@@ -1541,6 +1600,7 @@ fail_memory:
 fail_init:
    if (screenshot)
      efl_util_screenshot_deinitialize(screenshot);
+   _screenshot_mutex_unlock();
    if (display_wrapper)
      wl_proxy_wrapper_destroy(display_wrapper);
    set_last_result(EFL_UTIL_ERROR_SCREENSHOT_INIT_FAIL);
@@ -1550,8 +1610,14 @@ fail_init:
 API int
 efl_util_screenshot_deinitialize(efl_util_screenshot_h screenshot)
 {
+   _screenshot_mutex_lock();
+
    if (!screenshot)
-     return EFL_UTIL_ERROR_INVALID_PARAMETER;
+     {
+        _screenshot_mutex_unlock();
+        _screenshot_mutex_destory();
+        return EFL_UTIL_ERROR_INVALID_PARAMETER;
+     }
 
    free(screenshot);
    g_screenshot = NULL;
@@ -1573,6 +1639,9 @@ efl_util_screenshot_deinitialize(efl_util_screenshot_h screenshot)
         _eflutil.wl.shot.tz_screenshooter = NULL;
      }
 
+   _screenshot_mutex_unlock();
+   _screenshot_mutex_destory();
+
    return EFL_UTIL_ERROR_NONE;
 }
 
@@ -1585,9 +1654,12 @@ efl_util_screenshot_take_tbm_surface(efl_util_screenshot_h screenshot)
    Efl_Util_Wl_Output_Info *output;
    int ret = 0;
 
+   _screenshot_mutex_lock();
+
    if (!screenshot || (screenshot != g_screenshot))
      {
         set_last_result(EFL_UTIL_ERROR_INVALID_PARAMETER);
+        _screenshot_mutex_unlock();
         return NULL;
      }
 
@@ -1629,6 +1701,8 @@ efl_util_screenshot_take_tbm_surface(efl_util_screenshot_h screenshot)
    /* reset shot_done for next screenshot */
    screenshot->shot_done = EINA_FALSE;
 
+   _screenshot_mutex_unlock();
+
    return t_surface;
 
 fail:
@@ -1639,5 +1713,7 @@ fail:
 
    set_last_result(EFL_UTIL_ERROR_SCREENSHOT_EXECUTION_FAIL);
 
+   _screenshot_mutex_unlock();
+
    return NULL;
 }