PUI: add manual render set/get APIs and move ani get/set/update APIs to PUI from...
[platform/core/uifw/libpui.git] / src / PUI.c
index c5e8a94..f7fa28e 100644 (file)
--- a/src/PUI.c
+++ b/src/PUI.c
@@ -1,41 +1,57 @@
-#include "PUI.h"
+/*
+ * Copyright © 2019 Samsung Electronics co., Ltd. All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial
+ * portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
 #include "PUI_internal.h"
 #include "PUI_backend.h"
+#include "PUI.h"
+
+#include <wayland-tbm-client.h>
+#include <tbm_surface_internal.h>
+#include <stdio.h>
+#include <dlfcn.h>
 
 #ifndef PUI_MODULE_DIR
 #define PUI_MODULE_DIR "/usr/lib"
 #endif
 
-#define pui_err(msg, ...)                                                                              \
-       do {                                                                                                            \
-               fprintf(stderr, "[ERROR][%s] " msg, __FUNCTION__, ##__VA_ARGS__);       \
-       } while(0)
-
-#define pui_warn(msg, ...)                                                                             \
-               do {                                                                                                    \
-                       fprintf(stderr, "[WARNING][%s] " msg, __FUNCTION__, ##__VA_ARGS__);     \
-               } while(0)
-
-#define pui_info(msg, ...)                                                                             \
-       do {                                                                                                            \
-               fprintf(stdout, "[INFO][%s] " msg, __FUNCTION__, ##__VA_ARGS__);        \
-       } while(0)
-
 static int _pui_init_count = 0;
-static pui_module_data *pui_module = NUJLL;
+static pui_module_data *pui_module = NULL;
 
-EAPI int PUI_EVENT_ANI_STARTED = 0;
-EAPI int PUI_EVENT_ANI_STOPPED = 0;
-EAPI int PUI_EVENT_ANI_PAUSED = 0;
-EAPI int PUI_EVENT_ANI_READY_TO_START = 0;
-EAPI int PUI_EVENT_ANI_READY_TO_RESUME = 0;
-EAPI int PUI_EVENT_ANI_FRAME_DONE = 0;
-EAPI int PUI_EVENT_ANI_BUFFER_RELEASED = 0;
+int PUI_EVENT_ANI_STARTED = 0;
+int PUI_EVENT_ANI_STOPPED = 0;
+int PUI_EVENT_ANI_PAUSED = 0;
+int PUI_EVENT_ANI_READY_TO_START = 0;
+int PUI_EVENT_ANI_READY_TO_RESUME = 0;
 
-const pui_error_string
+static int KEY_WL_BUFFER = 0xabcdbeaf;
+static int KEY_CLIENT = 0xdcbabeaf;
+
+pui_error_string
 pui_error_to_string(pui_error e)
 {
-       const pui_error_string str = NULL;
+       pui_error_string str = NULL;
 
        switch (e)
        {
@@ -51,14 +67,18 @@ pui_error_to_string(pui_error e)
                        str = "PUI_Invalid_Animation_Command";
                        break;
 
-               case PUI_ERROR_INVALID_ANI_OPT:
-                       str = "PUI_Invalid_Animation_Option";
-                       break;
-
                case PUI_ERROR_INTERNAL:
                        str = "PUI_Internal_Error";
                        break;
 
+               case PUI_ERROR_MANUAL_RENDER_ENABLED:
+                       str = "PUI_Manual_Render_Enabled";
+                       break;
+
+               case PUI_ERROR_UNABLE_SET_MANUAL_RENDER:
+                       str = "PUI_Unable_To_Set_Manual_Render";
+                       break;
+
                default:
                        str = "PUI_Unknown_Error";
        }
@@ -67,28 +87,234 @@ pui_error_to_string(pui_error e)
 }
 
 static void
-_pui_cb_frame(Ecore_Wl2_Window *win, uint32_t timestamp EINA_UNUSED, void *data)
+_buffer_release(void *data, struct wl_buffer *buffer)
 {
-       pui_h handle = (pui_h) data;
+       tbm_surface_h surface = (tbm_surface_h)data;
+       pui_h handle;
+
+       tbm_surface_internal_get_user_data(surface, (unsigned long)&KEY_CLIENT, (void **)&handle);
+       tbm_surface_queue_release(handle->tbm_queue, surface);
 
-       TRACE("Frame done ! (window=%p)\n", win);
+       pui_info("[UPDATE] release wl_buffer:%p, surface:%p\n", buffer, surface);
+}
 
-       // TODO
+static const struct wl_buffer_listener buffer_listener = {
+    _buffer_release
+};
 
-       return;
+pui_ani_control_buffer *
+pui_display_get_buffer(pui_h handle)
+{
+       tbm_surface_error_e ret;
+       tbm_surface_h surface;
+       pui_ani_control_buffer *buffer = NULL;
+
+       if (!handle)
+       {
+               pui_err("Error : PUI_INT_ERROR_INVALID_HANDLE\n");
+               return NULL;
+       }
+
+       if (handle->current_surface)
+       {
+               pui_warn("Current_surface is not used !\n");
+       }
+
+       if (!tbm_surface_queue_can_dequeue(handle->tbm_queue, 0))
+       {
+               pui_err("[UPDATE] Cannot dequeue (error : PUI_ERROR_INTERNAL)\n");
+               return NULL;
+       }
+
+       ret = tbm_surface_queue_dequeue(handle->tbm_queue, &surface);
+
+       if (ret != TBM_SURFACE_ERROR_NONE)
+       {
+               pui_err("[UPDATE] dequeue err:%d\n", ret);
+               return NULL;
+       }
+
+       tbm_surface_map(surface, TBM_SURF_OPTION_READ | TBM_SURF_OPTION_WRITE, &handle->current_sinfo);
+
+       handle->current_surface = surface;
+       buffer = (pui_ani_control_buffer *)&(handle->current_sinfo.planes[0]);
+
+       return buffer;
+}
+
+pui_error
+pui_display_set_buffer(pui_h handle, pui_ani_control_buffer *buffer)
+{
+       if (!handle)
+       {
+               pui_err("Error : PUI_ERROR_INVALID_HANDLE\n");
+               return PUI_ERROR_INVALID_HANDLE;
+       }
+
+       if (!handle->current_surface)
+       {
+               pui_err("Current_surface is not valid !\n");
+               return PUI_ERROR_INVALID_SURFACE;
+       }
+
+       if (!buffer || !buffer->ptr || !buffer->size)
+               return PUI_ERROR_INVALID_BUFFER;
+
+       handle->is_buffer_set = 1;
+
+       return PUI_ERROR_NONE;
+}
+
+pui_error
+pui_display_update(pui_h handle)
+{
+       tbm_surface_h surface;
+       tbm_surface_error_e ret;
+       struct wl_buffer *wl_buffer = NULL;
+
+       if (!handle)
+       {
+               pui_err("Error : PUI_ERROR_INVALID_HANDLE\n");
+               return PUI_ERROR_INVALID_HANDLE;
+       }
+
+       if (!handle->current_surface)
+               return PUI_ERROR_INVALID_SURFACE;
+
+       if (!handle->is_buffer_set)
+       {
+               pui_err("Buffer is not set !\n");
+               return PUI_ERROR_INVALID_BUFFER;
+       }
+
+       surface = handle->current_surface;
+       handle->current_surface = NULL;
+
+       tbm_surface_unmap(surface);
+
+       ret = tbm_surface_queue_enqueue(handle->tbm_queue, surface);
+
+       if (ret != TBM_SURFACE_ERROR_NONE)
+       {
+               pui_err("[UPDATE] enqueue err:%d\n", ret);
+               return PUI_ERROR_INVALID_SURFACE;
+       }
+
+       ret = tbm_surface_queue_acquire(handle->tbm_queue, &surface);
+
+       if (ret != TBM_SURFACE_ERROR_NONE)
+       {
+               pui_err("[UPDATE] acquire err:%d\n", ret);
+               return PUI_ERROR_INVALID_SURFACE;
+       }
+
+       if (!tbm_surface_internal_get_user_data(surface, (unsigned long)&KEY_WL_BUFFER, (void **)&wl_buffer)) {
+               wl_buffer = wayland_tbm_client_create_buffer(handle->wl_tbm_client, surface);
+
+               if (!wl_buffer)
+               {
+                       pui_err("[UPDATE] failed to create wl_buffer tbm_surface:%p\n", surface);
+                       return PUI_ERROR_INVALID_BUFFER;
+               }
+
+               wl_buffer_add_listener(wl_buffer, &buffer_listener, surface);
+
+               tbm_surface_internal_add_user_data(surface, (unsigned long)&KEY_WL_BUFFER, NULL);
+               tbm_surface_internal_set_user_data(surface, (unsigned long)&KEY_WL_BUFFER, wl_buffer);
+               tbm_surface_internal_add_user_data(surface, (unsigned long)&KEY_CLIENT, NULL);
+               tbm_surface_internal_set_user_data(surface, (unsigned long)&KEY_CLIENT, handle);
+       }
+
+       if (!wl_buffer)
+       {
+               pui_err("[UPDATE] dequeue err:%d\n", ret);
+               return PUI_ERROR_INVALID_BUFFER;
+       }
+
+       ecore_wl2_window_buffer_attach(handle->win, wl_buffer, 0, 0, 0);
+       ecore_wl2_window_damage(handle->win, NULL, 0);
+       ecore_wl2_window_commit(handle->win, EINA_TRUE);
+
+       pui_info("[UPDATE] commit wl_buffer:%p, surface:%p\n", wl_buffer, surface);
+
+       handle->is_buffer_set = 0;
+
+       return PUI_ERROR_NONE;
+}
+
+pui_error
+pui_display_manual_render_set(pui_h handle, pui_bool set)
+{
+       pui_ani_h ani_h = NULL;
+
+       if (!handle)
+       {
+               pui_err("Error : PUI_ERROR_INVALID_HANDLE\n");
+               return PUI_ERROR_INVALID_HANDLE;
+       }
+
+       ani_h = handle->current_ani_h;
+
+       if (ani_h)
+       {
+               if (pui_ani_status_get(ani_h->ani) == PUI_ANI_STATUS_RUNNING ||
+                       pui_ani_status_get(ani_h->ani) == PUI_ANI_STATUS_PAUSED)
+               {
+                       pui_err("Please stop the current animation.\n");
+                       return PUI_ERROR_UNABLE_SET_MANUAL_RENDER;
+               }
+       }
+
+       handle->manual_render = !!set;
+
+       return PUI_ERROR_NONE;
+}
+
+pui_bool
+pui_display_manual_render_get(pui_h handle)
+{
+       if (!handle)
+       {
+               pui_err("Error : PUI_ERROR_INVALID_HANDLE\n");
+               return 0;
+       }
+
+       return handle->manual_render;
+}
+
+pui_bool
+pui_display_geometry_get(pui_h handle, int *width, int *height)
+{
+       if (!width || !height)
+               return 0;
+
+       *width = 0;
+       *height = 0;
+
+       if (!pui_module || !pui_module->backend_module_data) {
+               pui_err("pui module data is not loaded\n");
+               return 0;
+       }
+
+       if (!pui_module->backend_module_data->geometry_get)
+       {
+               pui_err("Backend doesn't have geometry_get() !\n");
+               return 0;
+       }
+
+       return pui_module->backend_module_data->geometry_get(width, height);
 }
 
 pui_h
 pui_create(Ecore_Wl2_Window *win)
 {
        pui_h handle = NULL;
-       Ecore_Wl2_display *ewd = ecore_wl2_window_display_get(win);
+       Ecore_Wl2_Display *ewd = ecore_wl2_window_display_get(win);
        struct wayland_tbm_client *wl_tbm_client = NULL;
-       Ecore_Wl2_Frame_Cb_Handle *frame_cb = NULL;
 
        if (!win || !ewd)
        {
-               pui_err("Invalid window or display !");
+               pui_err("Invalid window or display !\n");
                return NULL;
        }
 
@@ -96,11 +322,11 @@ pui_create(Ecore_Wl2_Window *win)
 
        if (!wl_tbm_client)
        {
-               pui_err("Failed to init wayland_tbm_client !");
+               pui_err("Failed to init wayland_tbm_client !\n");
                return NULL;
        }
 
-       handle = (pui_h)calloc(1, sizeof(pui_h));
+       handle = (pui_h)calloc(1, sizeof(pui));
 
        if (!handle)
                return NULL;
@@ -110,11 +336,13 @@ pui_create(Ecore_Wl2_Window *win)
        handle->visibility = 0;
        handle->wl_tbm_client = wl_tbm_client;
        handle->ani_handles = NULL;
+       handle->current_ani_h = NULL;
+       handle->manual_render = 0;
        handle->backend_module_data = pui_module->backend_module_data;
 
        handle->tbm_queue = wayland_tbm_client_create_surface_queue(handle->wl_tbm_client,
                                                                ecore_wl2_window_surface_get(handle->win),
-                                                               2, 100, 100, TBM_FORMAT_ABGR8888);
+                                                               3, 100, 100, TBM_FORMAT_ABGR8888);
 
        if (!handle->tbm_queue)
        {
@@ -133,7 +361,7 @@ err:
 void
 pui_destroy(pui_h handle)
 {
-       pui_ani_h *ani_h = NULL;
+       pui_ani_h ani_h = NULL;
 
        if (!handle)
                return;
@@ -147,7 +375,6 @@ pui_destroy(pui_h handle)
        if (handle->tbm_queue)
        {
                tbm_surface_queue_destroy(handle->tbm_queue);
-               handle->tbm_queue = NULL;
        }
 
        if (handle->wl_tbm_client)
@@ -159,9 +386,9 @@ pui_destroy(pui_h handle)
        free(handle);
 }
 
-#define PREFIX_LIB    "libpui_"
+#define PREFIX_LIB    "libpui-"
 #define SUFFIX_LIB    ".so"
-#define DEFAULT_LIB   PREFIX_LIB"default"SUFFIX_LIB
+#define DEFAULT_LIB   PREFIX_LIB"default-backend"SUFFIX_LIB
 
 static void
 _pui_load_backend_module(void)
@@ -197,11 +424,11 @@ _pui_load_backend_module(void)
        backend_module_minor = PUI_BACKEND_GET_ABI_MINOR(backend_module_info->abi_version);
 
        if (backend_module_major > pui_backend_major) {
-               TBM_ERR("PUI backend module ABI major ver(%d) is newer than the PUI's ver(%d)\n",
+               pui_err("PUI backend module ABI major ver(%d) is newer than the PUI's ver(%d)\n",
                        backend_module_major, pui_backend_major);
                goto err;
        } else if (backend_module_minor > pui_backend_minor) {
-               TBM_ERR("PUI backend module ABI minor ver(%d) is newer than the PUI's ver(%d)\n",
+               pui_err("PUI backend module ABI minor ver(%d) is newer than the PUI's ver(%d)\n",
                        backend_module_minor, pui_backend_minor);
                goto err;
        }
@@ -227,8 +454,8 @@ _pui_load_backend_module(void)
        return;
 
 err:
-       if (backend_module_info)
-               backend_module_info->backend_deinit
+       if (backend_module_info && backend_module_info->backend_init)
+               backend_module_info->backend_deinit(backend_module_data);
 
        if (module_info)
                dlclose(module_info);
@@ -257,9 +484,33 @@ _pui_unload_backend_module(void)
 }
 
 static void
+_pui_load_backend_collect_animations(void)
+{
+       pui_int_error ret;
+
+       if (!pui_module || !pui_module->backend_module_data) {
+               pui_err("pui module data is not loaded\n");
+               return;
+       }
+
+       if (!pui_module->backend_module_data->create_ani_collection)
+       {
+               pui_err("Backend doesn't have create_ani_collection() !\n");
+               return;
+       }
+
+       ret = pui_module->backend_module_data->create_ani_collection();
+       if (ret != PUI_INT_ERROR_NONE) {
+               pui_err("Failed to collect animations data (%s)\n",
+                       pui_error_to_string(ret));
+       }
+}
+
+static void
 _pui_load(void)
 {
        _pui_load_backend_module();
+       _pui_load_backend_collect_animations();
 }
 
 static void
@@ -276,8 +527,6 @@ _pui_event_init(void)
        PUI_EVENT_ANI_PAUSED = ecore_event_type_new();
        PUI_EVENT_ANI_READY_TO_START = ecore_event_type_new();
        PUI_EVENT_ANI_READY_TO_RESUME = ecore_event_type_new();
-       PUI_EVENT_ANI_FRAME_DONE = ecore_event_type_new();
-       PUI_EVENT_ANI_BUFFER_RELEASED = ecore_event_type_new();
 }
 
 static void
@@ -287,17 +536,13 @@ _pui_event_shutdown(void)
                                        PUI_EVENT_ANI_STOPPED,
                                        PUI_EVENT_ANI_PAUSED,
                                        PUI_EVENT_ANI_READY_TO_START,
-                                       PUI_EVENT_ANI_READY_TO_RESUME,
-                                       PUI_EVENT_ANI_FRAME_DONE,
-                                       PUI_EVENT_ANI_BUFFER_RELEASED);
+                                       PUI_EVENT_ANI_READY_TO_RESUME);
 
        PUI_EVENT_ANI_STARTED = -1;
        PUI_EVENT_ANI_STOPPED = -1;
        PUI_EVENT_ANI_PAUSED = -1;
        PUI_EVENT_ANI_READY_TO_START = -1;
        PUI_EVENT_ANI_READY_TO_RESUME = -1;
-       PUI_EVENT_ANI_FRAME_DONE = -1;
-       PUI_EVENT_ANI_BUFFER_RELEASED = -1;
 }
 
 int