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 extern EAPI void ecore_wl2_window_buffer_attach(Ecore_Wl2_Window *win, void *buffer, int x, int y, Eina_Bool implicit);
40 extern EAPI void ecore_wl2_window_damage(Ecore_Wl2_Window *window, Eina_Rectangle *rects, unsigned int count);
42 static int _pui_init_count = 0;
43 static pui_module_data *pui_module = NULL;
45 int PUI_EVENT_ANI_STARTED = 0;
46 int PUI_EVENT_ANI_STOPPED = 0;
47 int PUI_EVENT_ANI_PAUSED = 0;
48 int PUI_EVENT_ANI_READY_TO_START = 0;
49 int PUI_EVENT_ANI_READY_TO_RESUME = 0;
51 static int KEY_WL_BUFFER = 0xabcdbeaf;
52 static int KEY_CLIENT = 0xdcbabeaf;
55 _pui_magic_string_get(PUI_Magic m)
60 return "None (Freed Object)";
64 return "PUI MODULE (PUI_M)";
68 return "PUI HANDLE (PUI_H)";
72 return "PUI ANI HANDLE (ANI_H)";
76 return "PUI ANI RUNTIME HANDLE (ANI_T)";
85 _pui_magic_fail(const void *p, PUI_Magic m, PUI_Magic req_m, const char *fname)
87 pui_err(" ### PUI ERROR : PUI Magic Check Failed !!! in %s().\n", fname);
90 pui_err(" Given handle/pointer is NULL !\n");
91 else if (m == PUI_MAGIC_NONE)
92 pui_err(" Given handle/pointer has been freed !\n");
94 pui_err(" Given handle/pointer is wrong type\n"
95 " Expected: %08x - %s\n"
96 " Supplied: %08x - %s",
97 (unsigned int)req_m, _pui_magic_string_get(req_m),
98 (unsigned int)m, _pui_magic_string_get(m));
100 /* abort here as a failure of magic can be caused by a memory corruption */
101 pui_err("### PUI MAGIC FAILED ! Abort ! ###\n");
106 pui_error_to_string(pui_error e)
108 pui_error_string str = NULL;
113 str = "PUI_No_Error";
116 case PUI_ERROR_INVALID_ANI_HANDLE:
117 str = "PUI_Invalid_Animation_Handle";
120 case PUI_ERROR_INVALID_ANI_CMD:
121 str = "PUI_Invalid_Animation_Command";
124 case PUI_ERROR_INTERNAL:
125 str = "PUI_Internal_Error";
128 case PUI_ERROR_MANUAL_RENDER_ENABLED:
129 str = "PUI_Manual_Render_Enabled";
132 case PUI_ERROR_UNABLE_SET_MANUAL_RENDER:
133 str = "PUI_Unable_To_Set_Manual_Render";
137 str = "PUI_Unknown_Error";
144 _buffer_release(void *data, struct wl_buffer *buffer)
146 tbm_surface_h surface = (tbm_surface_h)data;
149 tbm_surface_internal_get_user_data(surface, (unsigned long)&KEY_CLIENT, (void **)&handle);
150 tbm_surface_queue_release(handle->tbm_queue, surface);
152 pui_info("[UPDATE] release wl_buffer:%p, surface:%p\n", buffer, surface);
155 static const struct wl_buffer_listener buffer_listener = {
159 pui_ani_control_buffer *
160 pui_display_get_last_buffer(pui_h handle)
162 if (!PUI_MAGIC_CHECK(handle, PUI_MAGIC_PUI_H))
164 PUI_MAGIC_FAIL(handle, PUI_MAGIC_PUI_H, __FUNCTION__);
168 if (handle->last_buffer.size <= 0)
170 pui_err("Failed to get last buffer\n");
174 return &handle->last_buffer;
178 pui_ani_control_buffer *
179 pui_display_get_buffer(pui_h handle)
181 tbm_surface_error_e ret;
182 tbm_surface_h surface;
183 pui_ani_control_buffer *buffer = NULL;
185 if (!PUI_MAGIC_CHECK(handle, PUI_MAGIC_PUI_H))
187 PUI_MAGIC_FAIL(handle, PUI_MAGIC_PUI_H, __FUNCTION__);
191 if (handle->current_surface)
193 pui_debug("[GET BUFFER] Current_surface is not used !\n");
196 if (!tbm_surface_queue_can_dequeue(handle->tbm_queue, 0))
198 pui_err("[GET BUFFER] Failed to dequeue a tbm_surface !\n");
202 ret = tbm_surface_queue_dequeue(handle->tbm_queue, &surface);
204 if (ret != TBM_SURFACE_ERROR_NONE)
206 pui_err("[GET BUFFER] Dequeue err:%d\n", ret);
210 tbm_surface_map(surface, TBM_SURF_OPTION_READ | TBM_SURF_OPTION_WRITE, &handle->current_sinfo);
212 handle->current_surface = surface;
213 buffer = (pui_ani_control_buffer *)&(handle->current_sinfo.planes[0]);
219 pui_display_set_buffer(pui_h handle, pui_ani_control_buffer *buffer)
221 if (!PUI_MAGIC_CHECK(handle, PUI_MAGIC_PUI_H))
223 PUI_MAGIC_FAIL(handle, PUI_MAGIC_PUI_H, __FUNCTION__);
224 return PUI_ERROR_INVALID_HANDLE;
227 if (!handle->current_surface)
229 pui_err("Current_surface is not valid !\n");
230 return PUI_ERROR_INVALID_SURFACE;
233 if (!buffer || !buffer->ptr || !buffer->size)
234 return PUI_ERROR_INVALID_BUFFER;
236 handle->is_buffer_set = 1;
238 return PUI_ERROR_NONE;
242 pui_display_update(pui_h handle)
244 tbm_surface_h surface;
245 tbm_surface_error_e ret;
246 struct wl_buffer *wl_buffer = NULL;
248 if (!PUI_MAGIC_CHECK(handle, PUI_MAGIC_PUI_H))
250 PUI_MAGIC_FAIL(handle, PUI_MAGIC_PUI_H, __FUNCTION__);
251 return PUI_ERROR_INVALID_HANDLE;
254 if (!handle->current_surface)
255 return PUI_ERROR_INVALID_SURFACE;
257 if (!handle->is_buffer_set)
259 pui_err("[UPDATE] Buffer is not set !\n");
260 return PUI_ERROR_INVALID_BUFFER;
263 surface = handle->current_surface;
264 handle->current_surface = NULL;
266 tbm_surface_unmap(surface);
268 ret = tbm_surface_queue_enqueue(handle->tbm_queue, surface);
270 if (ret != TBM_SURFACE_ERROR_NONE)
272 pui_err("[UPDATE] enqueue err:%d\n", ret);
273 return PUI_ERROR_INVALID_SURFACE;
276 ret = tbm_surface_queue_acquire(handle->tbm_queue, &surface);
278 if (ret != TBM_SURFACE_ERROR_NONE)
280 pui_err("[UPDATE] acquire err:%d\n", ret);
281 return PUI_ERROR_INVALID_SURFACE;
284 if (!tbm_surface_internal_get_user_data(surface, (unsigned long)&KEY_WL_BUFFER, (void **)&wl_buffer)) {
285 wl_buffer = wayland_tbm_client_create_buffer(handle->wl_tbm_client, surface);
289 pui_err("[UPDATE] failed to create wl_buffer tbm_surface:%p\n", surface);
290 return PUI_ERROR_INVALID_BUFFER;
293 wl_buffer_add_listener(wl_buffer, &buffer_listener, surface);
295 tbm_surface_internal_add_user_data(surface, (unsigned long)&KEY_WL_BUFFER, NULL);
296 tbm_surface_internal_set_user_data(surface, (unsigned long)&KEY_WL_BUFFER, wl_buffer);
297 tbm_surface_internal_add_user_data(surface, (unsigned long)&KEY_CLIENT, NULL);
298 tbm_surface_internal_set_user_data(surface, (unsigned long)&KEY_CLIENT, handle);
303 pui_err("[UPDATE] dequeue err:%d\n", ret);
304 return PUI_ERROR_INVALID_BUFFER;
307 ecore_wl2_window_buffer_attach(handle->win, wl_buffer, 0, 0, 0);
308 ecore_wl2_window_damage(handle->win, NULL, 0);
309 ecore_wl2_window_commit(handle->win, EINA_TRUE);
311 pui_info("[UPDATE] commit wl_buffer:%p, surface:%p\n", wl_buffer, surface);
313 handle->is_buffer_set = 0;
315 if (handle->last_buffer.size > 0)
316 memcpy(handle->last_buffer.ptr, handle->current_sinfo.planes[0].ptr, handle->last_buffer.size);
318 return PUI_ERROR_NONE;
322 pui_display_manual_render_set(pui_h handle, pui_bool set)
324 pui_ani_h ani_h = NULL;
326 if (!PUI_MAGIC_CHECK(pui_module, PUI_MAGIC_PUI_M))
328 PUI_MAGIC_FAIL(pui_module, PUI_MAGIC_PUI_M, __FUNCTION__);
329 return PUI_ERROR_INVALID_HANDLE;
332 ani_h = handle->current_ani_h;
336 if (pui_ani_status_get(ani_h->ani) == PUI_ANI_STATUS_RUNNING ||
337 pui_ani_status_get(ani_h->ani) == PUI_ANI_STATUS_PAUSED)
339 pui_err("Please stop the current animation.\n");
340 return PUI_ERROR_UNABLE_SET_MANUAL_RENDER;
344 handle->manual_render = !!set;
346 return PUI_ERROR_NONE;
350 pui_display_manual_render_get(pui_h handle)
352 if (!PUI_MAGIC_CHECK(handle, PUI_MAGIC_PUI_H))
354 PUI_MAGIC_FAIL(handle, PUI_MAGIC_PUI_H, __FUNCTION__);
358 return handle->manual_render;
362 pui_display_geometry_get(pui_h handle, int *width, int *height)
364 if (!width || !height)
371 if (!PUI_MAGIC_CHECK(pui_module, PUI_MAGIC_PUI_M))
373 PUI_MAGIC_FAIL(pui_module, PUI_MAGIC_PUI_M, __FUNCTION__);
377 if (!pui_module->backend_module_data) {
378 pui_err("pui module data is not loaded\n");
382 if (!pui_module->backend_module_data->geometry_get)
384 pui_err("Backend doesn't have geometry_get() !\n");
388 return pui_module->backend_module_data->geometry_get(width, height);
392 pui_create(Ecore_Wl2_Window *win)
395 Ecore_Wl2_Display *ewd = ecore_wl2_window_display_get(win);
396 struct wayland_tbm_client *wl_tbm_client = NULL;
402 pui_err("Invalid window or display !\n");
406 if (!PUI_MAGIC_CHECK(pui_module, PUI_MAGIC_PUI_M))
408 PUI_MAGIC_FAIL(pui_module, PUI_MAGIC_PUI_M, __FUNCTION__);
412 wl_tbm_client = wayland_tbm_client_init(ecore_wl2_display_get(ewd));
416 pui_err("Failed to init wayland_tbm_client !\n");
420 handle = (pui_h)calloc(1, sizeof(pui));
427 handle->visibility = 0;
428 handle->wl_tbm_client = wl_tbm_client;
429 handle->ani_handles = NULL;
430 handle->current_ani_h = NULL;
431 handle->manual_render = 0;
432 handle->backend_module_data = pui_module->backend_module_data;
434 res = pui_display_geometry_get(handle, &w, &h);
437 /* NOTE: This is a default value for tbm_queue.
438 * This value is set before this patch.
443 handle->last_buffer.ptr = (unsigned char *)calloc(w * h * 4, sizeof(unsigned char));
444 if (handle->last_buffer.ptr)
445 handle->last_buffer.size = sizeof(unsigned char) * w * h * 4;
447 handle->tbm_queue = wayland_tbm_client_create_surface_queue(handle->wl_tbm_client,
448 ecore_wl2_window_surface_get(handle->win),
449 3, w, h, TBM_FORMAT_ABGR8888);
451 if (!handle->tbm_queue)
453 pui_err("Failed to create a surface queue !");
457 PUI_MAGIC_SET(handle, PUI_MAGIC_PUI_H);
467 pui_destroy(pui_h handle)
469 pui_ani_h ani_h = NULL;
471 if (!PUI_MAGIC_CHECK(handle, PUI_MAGIC_PUI_H))
473 PUI_MAGIC_FAIL(handle, PUI_MAGIC_PUI_H, __FUNCTION__);
477 EINA_LIST_FREE(handle->ani_handles, ani_h)
479 pui_ani_destroy(ani_h);
482 if (handle->tbm_queue)
484 tbm_surface_queue_destroy(handle->tbm_queue);
487 if (handle->wl_tbm_client)
489 wayland_tbm_client_deinit(handle->wl_tbm_client);
490 handle->wl_tbm_client = NULL;
493 if (handle->last_buffer.ptr)
495 free(handle->last_buffer.ptr);
496 handle->last_buffer.ptr = NULL;
497 handle->last_buffer.size = 0;
500 PUI_MAGIC_SET(handle, PUI_MAGIC_NONE);
504 #define PREFIX_LIB "libpui-"
505 #define SUFFIX_LIB ".so"
506 #define DEFAULT_LIB PREFIX_LIB"default-backend"SUFFIX_LIB
509 _pui_load_backend_module(void)
511 //char path[PATH_MAX] = {0, };
512 void *module_info = NULL;
513 pui_backend_module *backend_module_info = NULL;
514 int backend_module_major, backend_module_minor;
515 int pui_backend_major, pui_backend_minor;
517 pui_backend_module_data *backend_module_data = NULL;
519 if (!PUI_MAGIC_CHECK(pui_module, PUI_MAGIC_PUI_M))
521 PUI_MAGIC_FAIL(pui_module, PUI_MAGIC_PUI_M, __FUNCTION__);
525 module_info = dlopen(DEFAULT_LIB, RTLD_LAZY);
529 pui_err("Failed to dlopen(error:%s, path:%s) !\n", dlerror(), DEFAULT_LIB);
533 backend_module_info = dlsym(module_info, "pui_backend_module_info");
535 if (!backend_module_info) {
536 pui_err("Backend module(%s) doesn't have pui_backend_module_info !\n", DEFAULT_LIB);
540 pui_backend_major = PUI_BACKEND_GET_ABI_MAJOR(PUI_BACKEND_AB_VERSION_LAST);
541 pui_backend_minor = PUI_BACKEND_GET_ABI_MINOR(PUI_BACKEND_AB_VERSION_LAST);
543 backend_module_major = PUI_BACKEND_GET_ABI_MAJOR(backend_module_info->abi_version);
544 backend_module_minor = PUI_BACKEND_GET_ABI_MINOR(backend_module_info->abi_version);
546 if (backend_module_major > pui_backend_major) {
547 pui_err("PUI backend module ABI major ver(%d) is newer than the PUI's ver(%d)\n",
548 backend_module_major, pui_backend_major);
550 } else if (backend_module_minor > pui_backend_minor) {
551 pui_err("PUI backend module ABI minor ver(%d) is newer than the PUI's ver(%d)\n",
552 backend_module_minor, pui_backend_minor);
556 if (!backend_module_info->backend_init || !backend_module_info->backend_deinit)
558 pui_err("Backend module doesn't have backend_init/backend_deinit function !\n");
562 backend_module_data = backend_module_info->backend_init();
564 if (!backend_module_data)
566 pui_err("Failed to init module (%s) !\n", DEFAULT_LIB);
570 pui_module->module_info = module_info;
571 pui_module->backend_module_info = backend_module_info;
572 pui_module->backend_module_data = backend_module_data;
577 if (backend_module_info && backend_module_info->backend_deinit)
578 backend_module_info->backend_deinit(backend_module_data);
581 dlclose(module_info);
587 _pui_unload_backend_module(void)
589 if (!PUI_MAGIC_CHECK(pui_module, PUI_MAGIC_PUI_M))
591 PUI_MAGIC_FAIL(pui_module, PUI_MAGIC_PUI_M, __FUNCTION__);
595 if (pui_module->backend_module_info)
597 pui_module->backend_module_info->backend_deinit(pui_module->backend_module_data);
598 pui_module->backend_module_data = NULL;
599 pui_module->backend_module_info = NULL;
602 if (pui_module->module_info)
603 dlclose(pui_module->module_info);
607 _pui_load_backend_collect_animations(void)
611 if (!PUI_MAGIC_CHECK(pui_module, PUI_MAGIC_PUI_M))
613 PUI_MAGIC_FAIL(pui_module, PUI_MAGIC_PUI_M, __FUNCTION__);
617 if (!pui_module->backend_module_data) {
618 pui_err("pui module data is not loaded\n");
622 if (!pui_module->backend_module_data->create_ani_collection)
624 pui_err("Backend doesn't have create_ani_collection() !\n");
628 ret = pui_module->backend_module_data->create_ani_collection();
629 if (ret != PUI_INT_ERROR_NONE) {
630 pui_err("Failed to collect animations data (%s)\n",
631 pui_error_to_string(ret));
638 _pui_load_backend_module();
639 _pui_load_backend_collect_animations();
645 _pui_unload_backend_module();
649 _pui_event_init(void)
651 PUI_EVENT_ANI_STARTED = ecore_event_type_new();
652 PUI_EVENT_ANI_STOPPED = ecore_event_type_new();
653 PUI_EVENT_ANI_PAUSED = ecore_event_type_new();
654 PUI_EVENT_ANI_READY_TO_START = ecore_event_type_new();
655 PUI_EVENT_ANI_READY_TO_RESUME = ecore_event_type_new();
659 _pui_event_shutdown(void)
661 ecore_event_type_flush(PUI_EVENT_ANI_STARTED,
662 PUI_EVENT_ANI_STOPPED,
663 PUI_EVENT_ANI_PAUSED,
664 PUI_EVENT_ANI_READY_TO_START,
665 PUI_EVENT_ANI_READY_TO_RESUME);
667 PUI_EVENT_ANI_STARTED = -1;
668 PUI_EVENT_ANI_STOPPED = -1;
669 PUI_EVENT_ANI_PAUSED = -1;
670 PUI_EVENT_ANI_READY_TO_START = -1;
671 PUI_EVENT_ANI_READY_TO_RESUME = -1;
677 const char *cp = NULL;
679 if (++_pui_init_count != 1)
680 return _pui_init_count;
682 _pui_log_level = PUI_LOG_LEVEL_DEBUG;
684 cp = getenv("LIBPUI_LOG_LEVEL");
688 _pui_log_level = atoi(cp);
693 pui_err("Invalid calling of pui_init() !\n");
697 pui_module = (pui_module_data *)calloc(1, sizeof(pui_module_data));
701 pui_err("Failed to allocate memory for pui module data !\n");
705 PUI_MAGIC_SET(pui_module, PUI_MAGIC_PUI_M);
713 return _pui_init_count;
716 return --_pui_init_count;
722 if (_pui_init_count <= 0)
724 pui_err("Invalid pui init count : %d\n", _pui_init_count);
729 if (!PUI_MAGIC_CHECK(pui_module, PUI_MAGIC_PUI_M))
731 PUI_MAGIC_FAIL(pui_module, PUI_MAGIC_PUI_M, __FUNCTION__);
732 return _pui_init_count;
740 _pui_event_shutdown();
742 ecore_wl2_shutdown();
744 PUI_MAGIC_SET(pui_module, PUI_MAGIC_NONE);
747 return _pui_init_count;