2 * Copyright © 2019 Samsung Electronics co., Ltd. All Rights Reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 #include "PUI_internal.h"
27 #include "PUI_backend.h"
30 #include <wayland-tbm-client.h>
31 #include <tbm_surface_internal.h>
35 #ifndef PUI_MODULE_DIR
36 #define PUI_MODULE_DIR "/usr/lib"
39 static int _pui_init_count = 0;
40 static pui_module_data *pui_module = NULL;
42 int PUI_EVENT_ANI_STARTED = 0;
43 int PUI_EVENT_ANI_STOPPED = 0;
44 int PUI_EVENT_ANI_PAUSED = 0;
45 int PUI_EVENT_ANI_READY_TO_START = 0;
46 int PUI_EVENT_ANI_READY_TO_RESUME = 0;
48 static int KEY_WL_BUFFER = 0xabcdbeaf;
49 static int KEY_CLIENT = 0xdcbabeaf;
52 _pui_magic_string_get(PUI_Magic m)
57 return "None (Freed Object)";
61 return "PUI MODULE (PUI_M)";
65 return "PUI HANDLE (PUI_H)";
69 return "PUI ANI HANDLE (ANI_H)";
73 return "PUI ANI RUNTIME HANDLE (ANI_T)";
82 _pui_magic_fail(const void *p, PUI_Magic m, PUI_Magic req_m, const char *fname)
84 pui_err(" ### PUI ERROR : PUI Magic Check Failed !!! in %s().\n", fname);
87 pui_err(" Given handle/pointer is NULL !\n");
88 else if (m == PUI_MAGIC_NONE)
89 pui_err(" Given handle/pointer has been freed !\n");
91 pui_err(" Given handle/pointer is wrong type\n"
92 " Expected: %08x - %s\n"
93 " Supplied: %08x - %s",
94 (unsigned int)req_m, _pui_magic_string_get(req_m),
95 (unsigned int)m, _pui_magic_string_get(m));
97 /* abort here as a failure of magic can be caused by a memory corruption */
98 pui_err("### PUI MAGIC FAILED ! Abort ! ###\n");
103 pui_error_to_string(pui_error e)
105 pui_error_string str = NULL;
110 str = "PUI_No_Error";
113 case PUI_ERROR_INVALID_ANI_HANDLE:
114 str = "PUI_Invalid_Animation_Handle";
117 case PUI_ERROR_INVALID_ANI_CMD:
118 str = "PUI_Invalid_Animation_Command";
121 case PUI_ERROR_INTERNAL:
122 str = "PUI_Internal_Error";
125 case PUI_ERROR_MANUAL_RENDER_ENABLED:
126 str = "PUI_Manual_Render_Enabled";
129 case PUI_ERROR_UNABLE_SET_MANUAL_RENDER:
130 str = "PUI_Unable_To_Set_Manual_Render";
134 str = "PUI_Unknown_Error";
141 _buffer_release(void *data, struct wl_buffer *buffer)
143 tbm_surface_h surface = (tbm_surface_h)data;
146 tbm_surface_internal_get_user_data(surface, (unsigned long)&KEY_CLIENT, (void **)&handle);
147 tbm_surface_queue_release(handle->tbm_queue, surface);
149 pui_info("[UPDATE] release wl_buffer:%p, surface:%p\n", buffer, surface);
152 static const struct wl_buffer_listener buffer_listener = {
156 pui_ani_control_buffer *
157 pui_display_get_last_buffer(pui_h handle)
159 if (!PUI_MAGIC_CHECK(handle, PUI_MAGIC_PUI_H))
161 PUI_MAGIC_FAIL(handle, PUI_MAGIC_PUI_H, __FUNCTION__);
165 if (handle->last_buffer.size <= 0)
167 pui_err("Failed to get last buffer\n");
171 return &handle->last_buffer;
175 pui_ani_control_buffer *
176 pui_display_get_buffer(pui_h handle)
178 tbm_surface_error_e ret;
179 tbm_surface_h surface;
180 pui_ani_control_buffer *buffer = NULL;
182 if (!PUI_MAGIC_CHECK(handle, PUI_MAGIC_PUI_H))
184 PUI_MAGIC_FAIL(handle, PUI_MAGIC_PUI_H, __FUNCTION__);
188 if (handle->current_surface)
190 pui_debug("[GET BUFFER] Current_surface is not used !\n");
193 if (!tbm_surface_queue_can_dequeue(handle->tbm_queue, 0))
195 pui_err("[GET BUFFER] Failed to dequeue a tbm_surface !\n");
199 ret = tbm_surface_queue_dequeue(handle->tbm_queue, &surface);
201 if (ret != TBM_SURFACE_ERROR_NONE)
203 pui_err("[GET BUFFER] Dequeue err:%d\n", ret);
207 tbm_surface_map(surface, TBM_SURF_OPTION_READ | TBM_SURF_OPTION_WRITE, &handle->current_sinfo);
209 handle->current_surface = surface;
210 buffer = (pui_ani_control_buffer *)&(handle->current_sinfo.planes[0]);
216 pui_display_set_buffer(pui_h handle, pui_ani_control_buffer *buffer)
218 if (!PUI_MAGIC_CHECK(handle, PUI_MAGIC_PUI_H))
220 PUI_MAGIC_FAIL(handle, PUI_MAGIC_PUI_H, __FUNCTION__);
221 return PUI_ERROR_INVALID_HANDLE;
224 if (!handle->current_surface)
226 pui_err("Current_surface is not valid !\n");
227 return PUI_ERROR_INVALID_SURFACE;
230 if (!buffer || !buffer->ptr || !buffer->size)
231 return PUI_ERROR_INVALID_BUFFER;
233 handle->is_buffer_set = 1;
235 return PUI_ERROR_NONE;
239 pui_display_update(pui_h handle)
241 tbm_surface_h surface;
242 tbm_surface_error_e ret;
243 struct wl_buffer *wl_buffer = NULL;
245 if (!PUI_MAGIC_CHECK(handle, PUI_MAGIC_PUI_H))
247 PUI_MAGIC_FAIL(handle, PUI_MAGIC_PUI_H, __FUNCTION__);
248 return PUI_ERROR_INVALID_HANDLE;
251 if (!handle->current_surface)
252 return PUI_ERROR_INVALID_SURFACE;
254 if (!handle->is_buffer_set)
256 pui_err("[UPDATE] Buffer is not set !\n");
257 return PUI_ERROR_INVALID_BUFFER;
260 surface = handle->current_surface;
261 handle->current_surface = NULL;
263 tbm_surface_unmap(surface);
265 ret = tbm_surface_queue_enqueue(handle->tbm_queue, surface);
267 if (ret != TBM_SURFACE_ERROR_NONE)
269 pui_err("[UPDATE] enqueue err:%d\n", ret);
270 return PUI_ERROR_INVALID_SURFACE;
273 ret = tbm_surface_queue_acquire(handle->tbm_queue, &surface);
275 if (ret != TBM_SURFACE_ERROR_NONE)
277 pui_err("[UPDATE] acquire err:%d\n", ret);
278 return PUI_ERROR_INVALID_SURFACE;
281 if (!tbm_surface_internal_get_user_data(surface, (unsigned long)&KEY_WL_BUFFER, (void **)&wl_buffer)) {
282 wl_buffer = wayland_tbm_client_create_buffer(handle->wl_tbm_client, surface);
286 pui_err("[UPDATE] failed to create wl_buffer tbm_surface:%p\n", surface);
287 return PUI_ERROR_INVALID_BUFFER;
290 wl_buffer_add_listener(wl_buffer, &buffer_listener, surface);
292 tbm_surface_internal_add_user_data(surface, (unsigned long)&KEY_WL_BUFFER, NULL);
293 tbm_surface_internal_set_user_data(surface, (unsigned long)&KEY_WL_BUFFER, wl_buffer);
294 tbm_surface_internal_add_user_data(surface, (unsigned long)&KEY_CLIENT, NULL);
295 tbm_surface_internal_set_user_data(surface, (unsigned long)&KEY_CLIENT, handle);
300 pui_err("[UPDATE] dequeue err:%d\n", ret);
301 return PUI_ERROR_INVALID_BUFFER;
304 ecore_wl2_window_buffer_attach(handle->win, wl_buffer, 0, 0, 0);
305 ecore_wl2_window_damage(handle->win, NULL, 0);
306 ecore_wl2_window_commit(handle->win, EINA_TRUE);
308 pui_info("[UPDATE] commit wl_buffer:%p, surface:%p\n", wl_buffer, surface);
310 handle->is_buffer_set = 0;
312 if (handle->last_buffer.size > 0)
313 memcpy(handle->last_buffer.ptr, handle->current_sinfo.planes[0].ptr, handle->last_buffer.size);
315 return PUI_ERROR_NONE;
319 pui_display_manual_render_set(pui_h handle, pui_bool set)
321 pui_ani_h ani_h = NULL;
323 if (!PUI_MAGIC_CHECK(pui_module, PUI_MAGIC_PUI_M))
325 PUI_MAGIC_FAIL(pui_module, PUI_MAGIC_PUI_M, __FUNCTION__);
326 return PUI_ERROR_INVALID_HANDLE;
329 ani_h = handle->current_ani_h;
333 if (pui_ani_status_get(ani_h->ani) == PUI_ANI_STATUS_RUNNING ||
334 pui_ani_status_get(ani_h->ani) == PUI_ANI_STATUS_PAUSED)
336 pui_err("Please stop the current animation.\n");
337 return PUI_ERROR_UNABLE_SET_MANUAL_RENDER;
341 handle->manual_render = !!set;
343 return PUI_ERROR_NONE;
347 pui_display_manual_render_get(pui_h handle)
349 if (!PUI_MAGIC_CHECK(handle, PUI_MAGIC_PUI_H))
351 PUI_MAGIC_FAIL(handle, PUI_MAGIC_PUI_H, __FUNCTION__);
355 return handle->manual_render;
359 pui_display_geometry_get(pui_h handle, int *width, int *height)
361 if (!width || !height)
368 if (!PUI_MAGIC_CHECK(pui_module, PUI_MAGIC_PUI_M))
370 PUI_MAGIC_FAIL(pui_module, PUI_MAGIC_PUI_M, __FUNCTION__);
374 if (!pui_module->backend_module_data) {
375 pui_err("pui module data is not loaded\n");
379 if (!pui_module->backend_module_data->geometry_get)
381 pui_err("Backend doesn't have geometry_get() !\n");
385 return pui_module->backend_module_data->geometry_get(width, height);
389 pui_create(Ecore_Wl2_Window *win)
392 Ecore_Wl2_Display *ewd = ecore_wl2_window_display_get(win);
393 struct wayland_tbm_client *wl_tbm_client = NULL;
399 pui_err("Invalid window or display !\n");
403 if (!PUI_MAGIC_CHECK(pui_module, PUI_MAGIC_PUI_M))
405 PUI_MAGIC_FAIL(pui_module, PUI_MAGIC_PUI_M, __FUNCTION__);
409 wl_tbm_client = wayland_tbm_client_init(ecore_wl2_display_get(ewd));
413 pui_err("Failed to init wayland_tbm_client !\n");
417 handle = (pui_h)calloc(1, sizeof(pui));
424 handle->visibility = 0;
425 handle->wl_tbm_client = wl_tbm_client;
426 handle->ani_handles = NULL;
427 handle->current_ani_h = NULL;
428 handle->manual_render = 0;
429 handle->backend_module_data = pui_module->backend_module_data;
431 res = pui_display_geometry_get(handle, &w, &h);
434 /* NOTE: This is a default value for tbm_queue.
435 * This value is set before this patch.
440 handle->last_buffer.ptr = (unsigned char *)calloc(w * h * 4, sizeof(unsigned char));
441 if (handle->last_buffer.ptr)
442 handle->last_buffer.size = sizeof(unsigned char) * w * h * 4;
444 handle->tbm_queue = wayland_tbm_client_create_surface_queue(handle->wl_tbm_client,
445 ecore_wl2_window_surface_get(handle->win),
446 3, w, h, TBM_FORMAT_ABGR8888);
448 if (!handle->tbm_queue)
450 pui_err("Failed to create a surface queue !");
454 PUI_MAGIC_SET(handle, PUI_MAGIC_PUI_H);
464 pui_destroy(pui_h handle)
466 pui_ani_h ani_h = NULL;
468 if (!PUI_MAGIC_CHECK(handle, PUI_MAGIC_PUI_H))
470 PUI_MAGIC_FAIL(handle, PUI_MAGIC_PUI_H, __FUNCTION__);
474 EINA_LIST_FREE(handle->ani_handles, ani_h)
476 pui_ani_destroy(ani_h);
479 if (handle->tbm_queue)
481 tbm_surface_queue_destroy(handle->tbm_queue);
484 if (handle->wl_tbm_client)
486 wayland_tbm_client_deinit(handle->wl_tbm_client);
487 handle->wl_tbm_client = NULL;
490 if (handle->last_buffer.ptr)
492 free(handle->last_buffer.ptr);
493 handle->last_buffer.ptr = NULL;
494 handle->last_buffer.size = 0;
497 PUI_MAGIC_SET(handle, PUI_MAGIC_NONE);
501 #define PREFIX_LIB "libpui-"
502 #define SUFFIX_LIB ".so"
503 #define DEFAULT_LIB PREFIX_LIB"default-backend"SUFFIX_LIB
506 _pui_load_backend_module(void)
508 //char path[PATH_MAX] = {0, };
509 void *module_info = NULL;
510 pui_backend_module *backend_module_info = NULL;
511 int backend_module_major, backend_module_minor;
512 int pui_backend_major, pui_backend_minor;
514 pui_backend_module_data *backend_module_data = NULL;
516 if (!PUI_MAGIC_CHECK(pui_module, PUI_MAGIC_PUI_M))
518 PUI_MAGIC_FAIL(pui_module, PUI_MAGIC_PUI_M, __FUNCTION__);
522 module_info = dlopen(DEFAULT_LIB, RTLD_LAZY);
526 pui_err("Failed to dlopen(error:%s, path:%s) !\n", dlerror(), DEFAULT_LIB);
530 backend_module_info = dlsym(module_info, "pui_backend_module_info");
532 if (!backend_module_info) {
533 pui_err("Backend module(%s) doesn't have pui_backend_module_info !\n", DEFAULT_LIB);
537 pui_backend_major = PUI_BACKEND_GET_ABI_MAJOR(PUI_BACKEND_AB_VERSION_LAST);
538 pui_backend_minor = PUI_BACKEND_GET_ABI_MINOR(PUI_BACKEND_AB_VERSION_LAST);
540 backend_module_major = PUI_BACKEND_GET_ABI_MAJOR(backend_module_info->abi_version);
541 backend_module_minor = PUI_BACKEND_GET_ABI_MINOR(backend_module_info->abi_version);
543 if (backend_module_major > pui_backend_major) {
544 pui_err("PUI backend module ABI major ver(%d) is newer than the PUI's ver(%d)\n",
545 backend_module_major, pui_backend_major);
547 } else if (backend_module_minor > pui_backend_minor) {
548 pui_err("PUI backend module ABI minor ver(%d) is newer than the PUI's ver(%d)\n",
549 backend_module_minor, pui_backend_minor);
553 if (!backend_module_info->backend_init || !backend_module_info->backend_deinit)
555 pui_err("Backend module doesn't have backend_init/backend_deinit function !\n");
559 backend_module_data = backend_module_info->backend_init();
561 if (!backend_module_data)
563 pui_err("Failed to init module (%s) !\n", DEFAULT_LIB);
567 pui_module->module_info = module_info;
568 pui_module->backend_module_info = backend_module_info;
569 pui_module->backend_module_data = backend_module_data;
574 if (backend_module_info && backend_module_info->backend_deinit)
575 backend_module_info->backend_deinit(backend_module_data);
578 dlclose(module_info);
584 _pui_unload_backend_module(void)
586 if (!PUI_MAGIC_CHECK(pui_module, PUI_MAGIC_PUI_M))
588 PUI_MAGIC_FAIL(pui_module, PUI_MAGIC_PUI_M, __FUNCTION__);
592 if (pui_module->backend_module_info)
594 pui_module->backend_module_info->backend_deinit(pui_module->backend_module_data);
595 pui_module->backend_module_data = NULL;
596 pui_module->backend_module_info = NULL;
599 if (pui_module->module_info)
600 dlclose(pui_module->module_info);
604 _pui_load_backend_collect_animations(void)
608 if (!PUI_MAGIC_CHECK(pui_module, PUI_MAGIC_PUI_M))
610 PUI_MAGIC_FAIL(pui_module, PUI_MAGIC_PUI_M, __FUNCTION__);
614 if (!pui_module->backend_module_data) {
615 pui_err("pui module data is not loaded\n");
619 if (!pui_module->backend_module_data->create_ani_collection)
621 pui_err("Backend doesn't have create_ani_collection() !\n");
625 ret = pui_module->backend_module_data->create_ani_collection();
626 if (ret != PUI_INT_ERROR_NONE) {
627 pui_err("Failed to collect animations data (%s)\n",
628 pui_error_to_string(ret));
635 _pui_load_backend_module();
636 _pui_load_backend_collect_animations();
642 _pui_unload_backend_module();
646 _pui_event_init(void)
648 PUI_EVENT_ANI_STARTED = ecore_event_type_new();
649 PUI_EVENT_ANI_STOPPED = ecore_event_type_new();
650 PUI_EVENT_ANI_PAUSED = ecore_event_type_new();
651 PUI_EVENT_ANI_READY_TO_START = ecore_event_type_new();
652 PUI_EVENT_ANI_READY_TO_RESUME = ecore_event_type_new();
656 _pui_event_shutdown(void)
658 ecore_event_type_flush(PUI_EVENT_ANI_STARTED,
659 PUI_EVENT_ANI_STOPPED,
660 PUI_EVENT_ANI_PAUSED,
661 PUI_EVENT_ANI_READY_TO_START,
662 PUI_EVENT_ANI_READY_TO_RESUME);
664 PUI_EVENT_ANI_STARTED = -1;
665 PUI_EVENT_ANI_STOPPED = -1;
666 PUI_EVENT_ANI_PAUSED = -1;
667 PUI_EVENT_ANI_READY_TO_START = -1;
668 PUI_EVENT_ANI_READY_TO_RESUME = -1;
674 const char *cp = NULL;
676 if (++_pui_init_count != 1)
677 return _pui_init_count;
679 _pui_log_level = PUI_LOG_LEVEL_DEBUG;
681 cp = getenv("LIBPUI_LOG_LEVEL");
685 _pui_log_level = atoi(cp);
690 pui_err("Invalid calling of pui_init() !\n");
694 pui_module = (pui_module_data *)calloc(1, sizeof(pui_module_data));
698 pui_err("Failed to allocate memory for pui module data !\n");
702 PUI_MAGIC_SET(pui_module, PUI_MAGIC_PUI_M);
710 return _pui_init_count;
713 return --_pui_init_count;
719 if (_pui_init_count <= 0)
721 pui_err("Invalid pui init count : %d\n", _pui_init_count);
726 if (!PUI_MAGIC_CHECK(pui_module, PUI_MAGIC_PUI_M))
728 PUI_MAGIC_FAIL(pui_module, PUI_MAGIC_PUI_M, __FUNCTION__);
729 return _pui_init_count;
737 _pui_event_shutdown();
739 ecore_wl2_shutdown();
741 PUI_MAGIC_SET(pui_module, PUI_MAGIC_NONE);
744 return _pui_init_count;