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"
31 static int KEY_WL_BUFFER = 0xabcdbeaf;
32 static int KEY_CLIENT = 0xdcbabeaf;
35 _pui_ani_cb_frame_done(Ecore_Wl2_Window *win, uint32_t timestamp EINA_UNUSED, void *data)
37 pui_h handle = (pui_h) data;
39 pui_info("Frame done ! (window=0x%x)\n", ecore_wl2_window_id_get(win));
41 /* TODO : make use of handle */
48 _buffer_release(void *data, struct wl_buffer *buffer)
50 tbm_surface_h surface = (tbm_surface_h)data;
53 tbm_surface_internal_get_user_data(surface, (unsigned long)&KEY_CLIENT, (void **)&handle);
54 tbm_surface_queue_release(handle->tbm_queue, surface);
56 pui_info("[UPDATE] release wl_buffer:%p, surface:%p\n", buffer, surface);
59 static const struct wl_buffer_listener buffer_listener = {
63 pui_ani_control_buffer *
64 pui_ani_get_buffer(pui_ani_h ani_h)
67 tbm_surface_error_e ret;
68 tbm_surface_h surface;
69 pui_ani_control_buffer *buffer = NULL;
73 pui_err("Invalid pui ani handle !\n");
77 handle = ani_h->pui_handle;
81 pui_err("Error : PUI_INT_ERROR_INVALID_HANDLE\n");
85 if (handle->current_surface)
87 pui_warn("Current_surface is not used !\n");
90 if (!tbm_surface_queue_can_dequeue(handle->tbm_queue, 0))
92 pui_err("[UPDATE] Cannot dequeue (error : PUI_ERROR_INTERNAL)\n");
96 ret = tbm_surface_queue_dequeue(handle->tbm_queue, &surface);
98 if (ret != TBM_SURFACE_ERROR_NONE)
100 pui_err("[UPDATE] dequeue err:%d\n", ret);
104 tbm_surface_map(surface, TBM_SURF_OPTION_READ | TBM_SURF_OPTION_WRITE, &handle->current_sinfo);
106 handle->current_surface = surface;
107 buffer = (pui_ani_control_buffer *)&(handle->current_sinfo.planes[0]);
113 pui_ani_set_buffer(pui_ani_h ani_h, pui_ani_control_buffer *buffer)
119 pui_err("Invalid ani handle !\n");
120 return PUI_INT_ERROR_INVALID_HANDLE;
123 handle = ani_h->pui_handle;
127 pui_err("Error : PUI_INT_ERROR_INVALID_HANDLE\n");
128 return PUI_INT_ERROR_INVALID_HANDLE;
131 if (!handle->current_surface)
133 pui_err("Current_surface is not valid !\n");
134 return PUI_INT_ERROR_INVALID_SURFACE;
137 if (!buffer || !buffer->ptr || !buffer->size)
138 return PUI_INT_ERROR_INVALID_BUFFER;
140 handle->is_buffer_set = 1;
142 return PUI_INT_ERROR_NONE;
146 pui_ani_update(pui_ani_h ani_h)
148 tbm_surface_h surface;
149 tbm_surface_error_e ret;
150 struct wl_buffer *wl_buffer = NULL;
156 pui_err("Invalid ani handle !\n");
157 return PUI_INT_ERROR_INVALID_HANDLE;
160 handle = ani_h->pui_handle;
164 pui_err("Error : PUI_INT_ERROR_INVALID_HANDLE\n");
165 return PUI_INT_ERROR_INVALID_HANDLE;
168 if (!handle->current_surface)
169 return PUI_INT_ERROR_INVALID_SURFACE;
171 if (!handle->is_buffer_set)
173 pui_err("Buffer is not set !\n");
174 return PUI_INT_ERROR_INVALID_BUFFER;
177 surface = handle->current_surface;
178 handle->current_surface = NULL;
180 tbm_surface_unmap(surface);
182 ret = tbm_surface_queue_enqueue(handle->tbm_queue, surface);
184 if (ret != TBM_SURFACE_ERROR_NONE)
186 pui_err("[UPDATE] enqueue err:%d\n", ret);
187 return PUI_INT_ERROR_INVALID_SURFACE;
190 ret = tbm_surface_queue_acquire(handle->tbm_queue, &surface);
192 if (ret != TBM_SURFACE_ERROR_NONE)
194 pui_err("[UPDATE] acquire err:%d\n", ret);
195 return PUI_INT_ERROR_INVALID_SURFACE;
198 if (!tbm_surface_internal_get_user_data(surface, (unsigned long)&KEY_WL_BUFFER, (void **)&wl_buffer)) {
199 wl_buffer = wayland_tbm_client_create_buffer(handle->wl_tbm_client, surface);
203 pui_err("[UPDATE] failed to create wl_buffer tbm_surface:%p\n", surface);
204 return PUI_INT_ERROR_INVALID_BUFFER;
207 wl_buffer_add_listener(wl_buffer, &buffer_listener, surface);
209 tbm_surface_internal_add_user_data(surface, (unsigned long)&KEY_WL_BUFFER, NULL);
210 tbm_surface_internal_set_user_data(surface, (unsigned long)&KEY_WL_BUFFER, wl_buffer);
211 tbm_surface_internal_add_user_data(surface, (unsigned long)&KEY_CLIENT, NULL);
212 tbm_surface_internal_set_user_data(surface, (unsigned long)&KEY_CLIENT, handle);
217 pui_err("[UPDATE] dequeue err:%d\n", ret);
218 return PUI_INT_ERROR_INVALID_BUFFER;
221 ecore_wl2_window_buffer_attach(handle->win, wl_buffer, 0, 0, 0);
222 ecore_wl2_window_damage(handle->win, NULL, 0);
223 ecore_wl2_window_commit(handle->win, EINA_TRUE);
225 pui_info("[UPDATE] commit wl_buffer:%p, surface:%p\n", wl_buffer, surface);
227 handle->is_buffer_set = 0;
229 return PUI_INT_ERROR_NONE;
233 _pui_ani_frame_cb(void *data)
237 pui_ani_t *ani = (pui_ani_t *)data;
241 pui_err("Invalid pui ani !\n");
242 return ECORE_CALLBACK_CANCEL;
245 if (!ani->backend_frame_cb)
247 pui_err("Invalid backend frame_cb !\n");
248 return ECORE_CALLBACK_CANCEL;
251 if (!ani->frame_cb_timer)
253 pui_err("Invalid frame_cb timer !\n");
254 return ECORE_CALLBACK_CANCEL;
259 ret = (Eina_Bool)ani->backend_frame_cb(ani, ++ani->serial);
263 pui_err("Failed on backend's frame_cb !Frame_cb will be removed forcefuly !\n");
265 pui_ani_remove_frame_cb(ani);
270 if (ret && PUI_ANI_STATUS_STARTED == ani->status)
271 pui_ani_status_update(ani, PUI_ANI_STATUS_RUNNING);
277 pui_ani_add_frame_cb(pui_ani_t *ani, pui_bool (*frame_cb)(void *data, int serial), double frame_interval)
279 Ecore_Timer *timer = NULL;
283 pui_err("Invalid put ani !\n");
287 if (frame_interval <= 0.0f)
289 pui_err("Invalid frame interval (%.2f) ! frame interval must be larger than 0.\n", frame_interval);
293 ani->frame_cb = _pui_ani_frame_cb;
294 ani->backend_frame_cb = frame_cb;
295 ani->frame_interval = frame_interval;
296 ani->frame_cb_data = ani;
299 timer = ecore_timer_add(frame_interval, _pui_ani_frame_cb, ani);
303 pui_err("Failed to add ecore timer !\n");
307 ani->frame_cb_timer = timer;
309 pui_info("[Frame callback added][ani id=%s] frame_interval=%.2f\n", ani->id, frame_interval);
311 /* call frame_cb for starting the first frame */
312 _pui_ani_frame_cb(ani);
318 pui_ani_remove_frame_cb(pui_ani_t *ani)
322 pui_err("Invalid put ani !\n");
326 if (ani->frame_cb_timer)
328 ecore_timer_del(ani->frame_cb_timer);
329 ani->frame_cb_timer = NULL;
332 ani->frame_cb = NULL;
333 ani->backend_frame_cb = NULL;
334 ani->frame_interval = 0;
335 ani->frame_cb_data = NULL;
337 pui_info("[Frame callback removed][ani id=%s]\n", ani->id);
341 pui_ani_get_id(pui_ani_h ani_h)
343 if (!ani_h || !ani_h->ani)
346 return ani_h->ani->id;
350 pui_ani_get_cmd(pui_ani_h ani_h)
352 if (!ani_h || !ani_h->ani)
353 return PUI_ANI_CMD_NONE;
355 return ani_h->ani->cmd;
359 pui_ani_get_repeat(pui_ani_h ani_h)
361 if (!ani_h || !ani_h->ani)
364 return ani_h->ani->repeat;
367 pui_backend_ani_data *
368 pui_ani_get_ani_data(pui_ani_t *ani)
373 return ani->ani_data;
377 pui_ani_status_update(pui_ani_t *ani, pui_ani_status status)
381 PUI_Event_Animation_Status *e = NULL;
385 pui_err("Invalid pui ani !\n");
389 if (ani->status == status)
394 e = (PUI_Event_Animation_Status *)calloc(1, sizeof(PUI_Event_Animation_Status));
398 pui_err("Failed to allocate memory for PUI Event !\n");
402 ani->status = status;
404 e->win = ecore_wl2_window_id_get(ani_h->pui_handle->win);
409 case PUI_ANI_STATUS_STARTED:
410 ev_type = PUI_EVENT_ANI_STARTED;
411 pui_info("[Status update][ani id:%s] PUI_EVENT_ANI_STARTED event has been added.\n", ani->id);
414 case PUI_ANI_STATUS_RUNNING:
415 pui_info("[Status update][ani id:%s] PUI_ANI_STATUS_RUNNING !\n", ani->id);
418 case PUI_ANI_STATUS_PAUSED:
419 ev_type = PUI_EVENT_ANI_PAUSED;
420 pui_info("[Status update][ani id:%s] PUI_EVENT_ANI_PAUSED event has been added.\n", ani->id);
423 case PUI_ANI_STATUS_STOPPED:
424 ev_type = PUI_EVENT_ANI_STOPPED;
425 pui_info("[Status update][ani id:%s] PUI_EVENT_ANI_STOPPED event has been added.\n", ani->id);
429 pui_err("Unknown status !(ani status=%d, id=%s) !\n", status, ani->id);
435 ecore_event_add(ev_type, e, NULL, ani_h);
440 pui_ani_status_get(pui_ani_t *ani)
442 pui_ani_status status = PUI_ANI_STATUS_UNKNOWN;
446 pui_err("Invalid pui ani !\n");
454 _pui_ani_control_with_force(pui_ani_h ani_h, pui_ani_cmd cmd, int repeat, pui_bool force)
456 pui_int_error ei = PUI_INT_ERROR_NONE;
457 pui_ani_t *ani = NULL;
459 pui_backend_ani_func *ani_func = NULL;
462 return PUI_ERROR_INVALID_ANI_HANDLE;
464 if (cmd < PUI_ANI_CMD_START || cmd >= PUI_ANI_CMD_LAST)
466 pui_err("Invalid cmd ! (cmd=%d)\n", cmd);
467 return PUI_ERROR_INVALID_ANI_CMD;
472 pui_err("Invalid repeat count ! (repeat=%d)\n", repeat);
473 return PUI_ERROR_INVALID_ANI_REPEAT;
476 handle = ani_h->pui_handle;
479 if (!ani || !ani->ani_data)
481 pui_err("Invalid ani or ani_data !\n");
482 return PUI_ERROR_INTERNAL;
485 ani_func = ani->ani_data->ani_func;
488 ani->repeat = repeat;
490 if (cmd == PUI_ANI_CMD_START)
492 if (handle->current_ani_h && handle->current_ani_h != ani_h)
494 pui_ani_t *current_ani = handle->current_ani_h->ani;
496 if (current_ani->status >= PUI_ANI_STATUS_STARTED &&
497 current_ani->status <= PUI_ANI_STATUS_RUNNING)
499 pui_info("current_ani id=%s, status=%d\n", current_ani->id, current_ani->status);
501 ei = _pui_ani_control_with_force(handle->current_ani_h, PUI_ANI_CMD_STOP, 0, force);
503 if (ei != PUI_INT_ERROR_NONE)
504 pui_info("Failed to stop running previous animation ! (id:%s)\n", current_ani->id);
507 handle->current_ani_h = NULL;
511 ei = ani_func->ani_start(ani, repeat);
513 if (ei != PUI_INT_ERROR_NONE)
515 pui_err("Error on starting animation ! (id:%s, repeat:%d, status=%d))\n", ani->id, repeat, ani->status);
517 if (ani->status != PUI_ANI_STATUS_RUNNING)
518 _pui_ani_control_with_force(ani_h, PUI_ANI_CMD_STOP, 0, 0);
520 return PUI_ERROR_INTERNAL;
523 handle->current_ani_h = ani_h;
525 else//cmd == PUI_ANI_CMD_STOP
527 ei = ani_func->ani_stop(ani, force);
529 if (ei != PUI_INT_ERROR_NONE)
531 pui_err("Failied on stopping animation ! (id:%s)\n", ani->id);
533 if (ani->status != PUI_ANI_STATUS_STOPPED)
534 pui_ani_status_update(ani, PUI_ANI_STATUS_STOPPED);
536 if (ani->frame_cb_timer)
537 pui_ani_remove_frame_cb(ani);
539 return PUI_ERROR_INTERNAL;
542 return PUI_ERROR_NONE;
545 ani_h->frame_done_cb = ecore_wl2_window_frame_callback_add(handle->win, _pui_ani_cb_frame_done, handle);
547 if (!ani_h->frame_done_cb)
549 pui_err("Failed to add frame callback !");
553 return PUI_ERROR_NONE;
556 return PUI_ERROR_INTERNAL;
560 pui_ani_control(pui_ani_h ani_h, pui_ani_cmd cmd, int repeat)
562 return _pui_ani_control_with_force(ani_h, cmd, repeat, 0);
566 _cb_visibility_change(void *data, int type EINA_UNUSED, void *event)
568 pui_ani_h ani_h = (pui_ani_h)data;
569 pui_ani_t *ani = ani_h->ani;
570 pui_h ph = ani_h->pui_handle;
572 Ecore_Wl2_Event_Window_Visibility_Change *ev;
573 PUI_Event_Animation_Status *e = NULL;
577 /* check if this is needed */
578 ph->visibility = !(ev->fully_obscured);
580 if (ev->fully_obscured)
582 if (ani->status == PUI_ANI_STATUS_RUNNING)
584 pui_info("animation(%s) will be stopped as it lost its priority !\n", ani->id);
586 _pui_ani_control_with_force(ani_h, PUI_ANI_CMD_STOP, 0, 1);
591 if (ani->status == PUI_ANI_STATUS_PAUSED)
593 e = (PUI_Event_Animation_Status *)calloc(1, sizeof(PUI_Event_Animation_Status));
597 pui_err("Failed to allocate memory for PUI Event !\n");
598 return ECORE_CALLBACK_PASS_ON;
603 e->status = ani->status;
605 if(ani->status == PUI_ANI_STATUS_PAUSED)
607 pui_info("[Event added][ani id:%s] PUI_EVENT_ANI_READY_TO_RESUME event has been added.\n", ani_h->id);
608 ecore_event_add(PUI_EVENT_ANI_READY_TO_RESUME, e, NULL, ani_h);
613 return ECORE_CALLBACK_PASS_ON;
617 _pui_ani_event_handlers_init(pui_ani_h ani_h)
619 Ecore_Event_Handler *h = NULL;
623 pui_err("Invalid handle !\n");
627 if (!ani_h->ecore_event_hdls)
628 ani_h->ecore_event_hdls = eina_array_new(1);
630 h = ecore_event_handler_add(ECORE_WL2_EVENT_WINDOW_VISIBILITY_CHANGE, _cb_visibility_change, ani_h);
631 eina_array_push(ani_h->ecore_event_hdls, h);
636 _pui_ani_event_handlers_shutdown(pui_ani_h ani_h)
640 pui_err("Invalid handle !\n");
644 if (ani_h->ecore_event_hdls)
646 while (eina_array_count(ani_h->ecore_event_hdls))
647 ecore_event_handler_del(eina_array_pop(ani_h->ecore_event_hdls));
649 eina_array_free(ani_h->ecore_event_hdls);
650 ani_h->ecore_event_hdls = NULL;
655 pui_ani_create(pui_h handle, pui_id id)
657 pui_ani_h ani_h = NULL;
658 pui_ani_t *ani = NULL;
659 pui_backend_ani_data *ani_data = NULL;
661 if (!handle || !handle->backend_module_data)
663 pui_err("Invalid pui handle or backend module data !\n");
667 ani_data = handle->backend_module_data->ani_create(id);
671 pui_err("Invalid ani data from backend module data !\n");
675 ani_h = (pui_ani_h)calloc(1, sizeof(pui_ani));
679 pui_err("Failed to allocate memory for pui ani handle !\n");
683 ani_h->id = strdup(id);
684 ani_h->pui_handle = handle;
685 ani_h->ecore_event_hdls = NULL;
687 _pui_ani_event_handlers_init(ani_h);
689 ani = (pui_ani_t *)calloc(1, sizeof(pui_ani_t));
693 pui_err("Failed to allocate memory for pui ani mgr !\n");
699 ani->cmd = PUI_ANI_CMD_NONE;
701 ani->status = PUI_ANI_STATUS_INITIAL;
702 ani->ani_data = ani_data;
706 handle->ani_handles = eina_list_append(handle->ani_handles, ani_h);
713 handle->backend_module_data->ani_destroy(ani_data);
723 pui_ani_destroy(pui_ani_h ani_h)
726 pui_ani_t *ani = NULL;
727 pui_backend_module_data *backend_module_data = NULL;
729 if (!ani_h || !ani_h->pui_handle)
732 handle = ani_h->pui_handle;
735 /* stop the animation being played already if any */
736 if (ani->status == PUI_ANI_STATUS_STARTED || ani->status == PUI_ANI_STATUS_RUNNING)
737 pui_ani_control(ani_h, PUI_ANI_CMD_STOP, 0);
739 backend_module_data = handle->backend_module_data;
740 backend_module_data->ani_destroy(ani->ani_data);
741 ani->ani_data = NULL;
743 if (ani->frame_cb_timer)
745 ecore_timer_del(ani->frame_cb_timer);
746 ani->frame_cb_timer = NULL;
751 _pui_ani_event_handlers_shutdown(ani_h);
753 if (ani_h->frame_done_cb)
755 ecore_wl2_window_frame_callback_del(ani_h->frame_done_cb);
756 ani_h->frame_done_cb = NULL;
759 handle->ani_handles = eina_list_remove(handle->ani_handles, ani_h);