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 _cb_window_show(void *data, int type EINA_UNUSED, void *event)
37 pui_ani_h handle = (pui_ani_h)data;
38 Ecore_Wl2_Event_Window_Show *ev = (Ecore_Wl2_Event_Window_Show *)event;
46 return ECORE_CALLBACK_PASS_ON;
50 _cb_focus_in(void *data, int type EINA_UNUSED, void *event)
52 pui_ani_h handle = (pui_ani_h)data;
53 Ecore_Wl2_Event_Focus_In *ev = (Ecore_Wl2_Event_Focus_In *)event;
61 return ECORE_CALLBACK_PASS_ON;
65 _cb_focus_out(void *data, int type EINA_UNUSED, void *event)
67 pui_ani_h handle = (pui_ani_h)data;
68 Ecore_Wl2_Event_Focus_Out *ev = (Ecore_Wl2_Event_Focus_Out *)event;
76 return ECORE_CALLBACK_PASS_ON;
80 _cb_visibility_change(void *data, int type EINA_UNUSED, void *event)
82 pui_ani_h ani_h = (pui_ani_h)data;
83 pui_ani_t *ani = ani_h->ani;
84 pui_h ph = ani_h->pui_handle;
86 Ecore_Wl2_Event_Window_Visibility_Change *ev;
87 PUI_Event_Animation_Status *e = NULL;
91 pui_info("[pui_ani] Visibility change (window=0x%x, fully_obscured=%d)\n", ev->win, ev->fully_obscured);
93 /* check if this is needed */
94 ph->visibility = !(ev->fully_obscured);
96 if (ev->fully_obscured)
98 pui_info("ani->id=%s, ani->status=%d\n", ani->id, ani->status);
100 if (ani->status == PUI_ANI_STATUS_RUNNING)
102 pui_ani_control(ani_h, PUI_ANI_CMD_STOP, 0);
107 if (ani->status == PUI_ANI_STATUS_STOPPED || ani->status == PUI_ANI_STATUS_PAUSED)
109 e = (PUI_Event_Animation_Status *)calloc(1, sizeof(PUI_Event_Animation_Status));
113 pui_err("Failed to allocate memory for PUI Event !\n");
114 return ECORE_CALLBACK_PASS_ON;
119 e->status = ani->status;
121 if (ani->status == PUI_ANI_STATUS_STOPPED)
123 ecore_event_add(PUI_EVENT_ANI_READY_TO_START, e, NULL, ani_h);
125 else if(ani->status == PUI_ANI_STATUS_PAUSED)
127 ecore_event_add(PUI_EVENT_ANI_READY_TO_RESUME, e, NULL, ani_h);
132 return ECORE_CALLBACK_PASS_ON;
136 _pui_ani_cb_frame_done(Ecore_Wl2_Window *win, uint32_t timestamp EINA_UNUSED, void *data)
138 pui_h handle = (pui_h) data;
140 pui_info("Frame done ! (window=%p)\n", win);
149 _buffer_release(void *data, struct wl_buffer *buffer)
151 tbm_surface_h surface = (tbm_surface_h)data;
154 tbm_surface_internal_get_user_data(surface, (unsigned long)&KEY_CLIENT, (void **)&handle);
155 tbm_surface_queue_release(handle->tbm_queue, surface);
157 pui_info("[UPDATE] release wl_buffer:%p, surface:%p\n", buffer, surface);
160 static const struct wl_buffer_listener buffer_listener = {
164 pui_ani_control_buffer *
165 pui_ani_get_buffer(pui_ani_h ani_h)
168 tbm_surface_error_e ret;
169 tbm_surface_h surface;
170 pui_ani_control_buffer *buffer = NULL;
174 pui_err("Invalid pui ani handle !\n");
178 handle = ani_h->pui_handle;
182 pui_err("Error : PUI_INT_ERROR_INVALID_HANDLE\n");
186 if (handle->current_surface)
188 pui_warn("Current_surface is not used !\n");
191 if (!tbm_surface_queue_can_dequeue(handle->tbm_queue, 0))
193 pui_err("[UPDATE] Cannot dequeue (error : PUI_ERROR_INTERNAL)\n");
197 ret = tbm_surface_queue_dequeue(handle->tbm_queue, &surface);
199 if (ret != TBM_SURFACE_ERROR_NONE)
201 pui_err("[UPDATE] dequeue err:%d\n", ret);
205 tbm_surface_map(surface, TBM_SURF_OPTION_READ | TBM_SURF_OPTION_WRITE, &handle->current_sinfo);
207 handle->current_surface = surface;
208 buffer = (pui_ani_control_buffer *)&(handle->current_sinfo.planes[0]);
214 pui_ani_set_buffer(pui_ani_h ani_h, pui_ani_control_buffer *buffer)
220 pui_err("Invalid ani handle !\n");
221 return PUI_INT_ERROR_INVALID_HANDLE;
224 handle = ani_h->pui_handle;
228 pui_err("Error : PUI_INT_ERROR_INVALID_HANDLE\n");
229 return PUI_INT_ERROR_INVALID_HANDLE;
232 if (!handle->current_surface)
234 pui_err("Current_surface is not valid !\n");
235 return PUI_INT_ERROR_INVALID_SURFACE;
238 if (!buffer || !buffer->ptr || !buffer->size)
239 return PUI_INT_ERROR_INVALID_BUFFER;
241 handle->is_buffer_set = 1;
243 return PUI_INT_ERROR_NONE;
247 pui_ani_update(pui_ani_h ani_h)
249 tbm_surface_h surface;
250 tbm_surface_error_e ret;
251 struct wl_buffer *wl_buffer = NULL;
257 pui_err("Invalid ani handle !\n");
258 return PUI_INT_ERROR_INVALID_HANDLE;
261 handle = ani_h->pui_handle;
265 pui_err("Error : PUI_INT_ERROR_INVALID_HANDLE\n");
266 return PUI_INT_ERROR_INVALID_HANDLE;
269 if (!handle->current_surface)
270 return PUI_INT_ERROR_INVALID_SURFACE;
272 if (!handle->is_buffer_set)
274 pui_err("Buffer is not set !\n");
275 return PUI_INT_ERROR_INVALID_BUFFER;
278 surface = handle->current_surface;
279 handle->current_surface = NULL;
281 tbm_surface_unmap(surface);
283 ret = tbm_surface_queue_enqueue(handle->tbm_queue, surface);
285 if (ret != TBM_SURFACE_ERROR_NONE)
287 pui_err("[UPDATE] enqueue err:%d\n", ret);
288 return PUI_INT_ERROR_INVALID_SURFACE;
291 ret = tbm_surface_queue_acquire(handle->tbm_queue, &surface);
293 if (ret != TBM_SURFACE_ERROR_NONE)
295 pui_err("[UPDATE] acquire err:%d\n", ret);
296 return PUI_INT_ERROR_INVALID_SURFACE;
299 if (!tbm_surface_internal_get_user_data(surface, (unsigned long)&KEY_WL_BUFFER, (void **)&wl_buffer)) {
300 wl_buffer = wayland_tbm_client_create_buffer(handle->wl_tbm_client, surface);
304 pui_err("[UPDATE] failed to create wl_buffer tbm_surface:%p\n", surface);
305 return PUI_INT_ERROR_INVALID_BUFFER;
308 wl_buffer_add_listener(wl_buffer, &buffer_listener, surface);
310 tbm_surface_internal_add_user_data(surface, (unsigned long)&KEY_WL_BUFFER, NULL);
311 tbm_surface_internal_set_user_data(surface, (unsigned long)&KEY_WL_BUFFER, wl_buffer);
312 tbm_surface_internal_add_user_data(surface, (unsigned long)&KEY_CLIENT, NULL);
313 tbm_surface_internal_set_user_data(surface, (unsigned long)&KEY_CLIENT, handle);
318 pui_err("[UPDATE] dequeue err:%d\n", ret);
319 return PUI_INT_ERROR_INVALID_BUFFER;
322 ecore_wl2_window_buffer_attach(handle->win, wl_buffer, 0, 0, 0);
323 ecore_wl2_window_damage(handle->win, NULL, 0);
324 ecore_wl2_window_commit(handle->win, EINA_TRUE);
326 pui_info("[UPDATE] commit wl_buffer:%p, surface:%p\n", wl_buffer, surface);
328 handle->is_buffer_set = 0;
330 return PUI_INT_ERROR_NONE;
334 _pui_ani_event_handlers_init(pui_ani_h ani_h)
336 Ecore_Event_Handler *h = NULL;
340 pui_err("Invalid handle !\n");
344 if (!ani_h->ecore_event_hdls)
345 ani_h->ecore_event_hdls = eina_array_new(5);
347 h = ecore_event_handler_add(ECORE_WL2_EVENT_WINDOW_SHOW, _cb_window_show, ani_h);
348 eina_array_push(ani_h->ecore_event_hdls, h);
350 h = ecore_event_handler_add(ECORE_WL2_EVENT_FOCUS_IN, _cb_focus_in, ani_h);
351 eina_array_push(ani_h->ecore_event_hdls, h);
353 h = ecore_event_handler_add(ECORE_WL2_EVENT_FOCUS_OUT, _cb_focus_out, ani_h);
354 eina_array_push(ani_h->ecore_event_hdls, h);
356 h = ecore_event_handler_add(ECORE_WL2_EVENT_WINDOW_VISIBILITY_CHANGE, _cb_visibility_change, ani_h);
357 eina_array_push(ani_h->ecore_event_hdls, h);
362 _pui_ani_event_handlers_shutdown(pui_ani_h ani_h)
366 pui_err("Invalid handle !\n");
370 if (ani_h->ecore_event_hdls)
372 while (eina_array_count(ani_h->ecore_event_hdls))
373 ecore_event_handler_del(eina_array_pop(ani_h->ecore_event_hdls));
375 eina_array_free(ani_h->ecore_event_hdls);
376 ani_h->ecore_event_hdls = NULL;
381 _pui_ani_frame_cb(void *data)
385 pui_ani_t *ani = (pui_ani_t *)data;
389 pui_err("Invalid pui ani !\n");
390 return ECORE_CALLBACK_CANCEL;
393 if (!ani->backend_frame_cb)
395 pui_err("Invalid backend frame_cb !\n");
396 return ECORE_CALLBACK_CANCEL;
399 if (!ani->frame_cb_timer)
401 pui_err("Invalid frame_cb timer !\n");
402 return ECORE_CALLBACK_CANCEL;
405 pui_info("frame_cb !\n");
407 ret = (Eina_Bool)ani->backend_frame_cb(ani, ++ani->serial);
411 pui_err("Failed on backend's frame_cb !Frame_cb will be removed forcefuly !\n");
412 pui_ani_remove_frame_cb(ani);
415 pui_info("ret from ani->backend_frame_cb = %d, ani->status=%d\n", ret, ani->status);
417 if (ret && PUI_ANI_STATUS_STARTED == ani->status)
418 pui_ani_status_update(ani, PUI_ANI_STATUS_RUNNING);
424 pui_ani_add_frame_cb(pui_ani_t *ani, pui_bool (*frame_cb)(void *data, int serial), double frame_interval)
426 Ecore_Timer *timer = NULL;
430 pui_err("Invalid put ani !\n");
434 pui_info("ani->id=%s, frame_interval=%.2f\n", ani->id, frame_interval);
437 if (frame_interval <= 0.0f)
439 pui_err("Invalid frame interval (%.2f) ! frame interval must be larger than 0.\n", frame_interval);
443 ani->frame_cb = _pui_ani_frame_cb;
444 ani->backend_frame_cb = frame_cb;
445 ani->frame_interval = frame_interval;
446 ani->frame_cb_data = ani;
449 timer = ecore_timer_add(frame_interval, _pui_ani_frame_cb, ani);
453 pui_err("Failed to add ecore timer !\n");
457 ani->frame_cb_timer = timer;
459 /* call frame_cb for starting the first frame */
460 _pui_ani_frame_cb(ani);
466 pui_ani_remove_frame_cb(pui_ani_t *ani)
471 pui_info("ani->id=%s\n", ani->id);
474 if (ani->frame_cb_timer)
476 ecore_timer_del(ani->frame_cb_timer);
477 ani->frame_cb_timer = NULL;
480 ani->frame_cb = NULL;
481 ani->backend_frame_cb = NULL;
482 ani->frame_interval = 0;
483 ani->frame_cb_data = NULL;
487 pui_ani_get_id(pui_ani_h ani_h)
489 if (!ani_h || !ani_h->ani)
492 return ani_h->ani->id;
496 pui_ani_get_cmd(pui_ani_h ani_h)
498 if (!ani_h || !ani_h->ani)
499 return PUI_ANI_CMD_NONE;
501 return ani_h->ani->cmd;
505 pui_ani_get_repeat(pui_ani_h ani_h)
507 if (!ani_h || !ani_h->ani)
510 return ani_h->ani->repeat;
513 pui_backend_ani_data *
514 pui_ani_get_ani_data(pui_ani_t *ani)
519 return ani->ani_data;
523 pui_ani_status_update(pui_ani_t *ani, pui_ani_status status)
527 PUI_Event_Animation_Status *e = NULL;
531 pui_err("Invalid pui ani !\n");
535 if (ani->status == status)
540 e = (PUI_Event_Animation_Status *)calloc(1, sizeof(PUI_Event_Animation_Status));
544 pui_err("Failed to allocate memory for PUI Event !\n");
548 ani->status = status;
550 e->win = ecore_wl2_window_id_get(ani_h->pui_handle->win);
553 pui_info("ani->id=%s, status=%d\n", ani->id, status);
557 case PUI_ANI_STATUS_STARTED:
558 ev_type = PUI_EVENT_ANI_STARTED;
561 case PUI_ANI_STATUS_RUNNING:
564 case PUI_ANI_STATUS_PAUSED:
565 ev_type = PUI_EVENT_ANI_PAUSED;
568 case PUI_ANI_STATUS_STOPPED:
569 ev_type = PUI_EVENT_ANI_STOPPED;
573 pui_err("Unknown status !(ani status=%d, id=%s) !\n", status, ani->id);
579 ecore_event_add(ev_type, e, NULL, ani_h);
584 pui_ani_status_get(pui_ani_t *ani)
586 pui_ani_status status = PUI_ANI_STATUS_UNKNOWN;
590 pui_err("Invalid pui ani !\n");
598 pui_ani_control(pui_ani_h ani_h, pui_ani_cmd cmd, int repeat)
600 pui_int_error ei = PUI_INT_ERROR_NONE;
601 pui_ani_t *ani = NULL;
603 pui_backend_ani_func *ani_func = NULL;
606 return PUI_ERROR_INVALID_ANI_HANDLE;
608 if (cmd < PUI_ANI_CMD_START || cmd >= PUI_ANI_CMD_LAST)
610 pui_err("Invalid cmd ! (cmd=%d)\n", cmd);
611 return PUI_ERROR_INVALID_ANI_CMD;
616 pui_err("Invalid repeat count ! (repeat=%d)\n", repeat);
617 return PUI_ERROR_INVALID_ANI_REPEAT;
620 handle = ani_h->pui_handle;
623 if (!ani || !ani->ani_data)
625 pui_err("Invalid ani or ani_data !\n");
626 return PUI_ERROR_INTERNAL;
629 ani_func = ani->ani_data->ani_func;
632 ani->repeat = repeat;
634 if (cmd == PUI_ANI_CMD_START)
636 if (handle->current_ani_h && handle->current_ani_h != ani_h)
638 pui_ani_t *current_ani = handle->current_ani_h->ani;
640 if (current_ani->status >= PUI_ANI_STATUS_STARTED &&
641 current_ani->status <= PUI_ANI_STATUS_RUNNING)
643 pui_info("current_ani id=%s, status=%d\n", current_ani->id, current_ani->status);
645 ei = pui_ani_control(handle->current_ani_h, PUI_ANI_CMD_STOP, 0);
647 if (ei != PUI_INT_ERROR_NONE)
648 pui_info("Failed to stop running previous animation ! (id:%s)\n", current_ani->id);
651 handle->current_ani_h = NULL;
655 ei = ani_func->ani_start(ani, repeat);
657 if (ei != PUI_INT_ERROR_NONE)
659 pui_err("Error on starting animation ! (id:%s, repeat:%d, status=%d))\n", ani->id, repeat, ani->status);
661 if (ani->status != PUI_ANI_STATUS_RUNNING)
662 pui_ani_control(ani_h, PUI_ANI_CMD_STOP, 0);
664 return PUI_ERROR_INTERNAL;
667 handle->current_ani_h = ani_h;
669 else//cmd == PUI_ANI_CMD_STOP
671 ei = ani_func->ani_stop(ani);
673 if (ei != PUI_INT_ERROR_NONE)
675 pui_err("Failied on stopping animation ! (id:%s)\n", ani->id);
677 if (ani->status != PUI_ANI_STATUS_STOPPED)
678 pui_ani_status_update(ani, PUI_ANI_STATUS_STOPPED);
680 if (ani->frame_cb_timer)
681 pui_ani_remove_frame_cb(ani);
683 return PUI_ERROR_INTERNAL;
686 return PUI_ERROR_NONE;
689 ani_h->frame_done_cb = ecore_wl2_window_frame_callback_add(handle->win, _pui_ani_cb_frame_done, handle);
691 if (!ani_h->frame_done_cb)
693 pui_err("Failed to add frame callback !");
697 return PUI_ERROR_NONE;
700 return PUI_ERROR_INTERNAL;
704 pui_ani_create(pui_h handle, pui_id id)
706 pui_ani_h ani_h = NULL;
707 pui_ani_t *ani = NULL;
708 pui_backend_ani_data *ani_data = NULL;
710 if (!handle || !handle->backend_module_data)
712 pui_err("Invalid pui handle or backend module data !\n");
716 ani_data = handle->backend_module_data->ani_create(id);
720 pui_err("Invalid ani data from backend module data !\n");
724 ani_h = (pui_ani_h)calloc(1, sizeof(pui_ani));
728 pui_err("Failed to allocate memory for pui ani handle !\n");
732 ani_h->id = strdup(id);
733 ani_h->pui_handle = handle;
734 ani_h->ecore_event_hdls = NULL;
736 _pui_ani_event_handlers_init(ani_h);
738 ani = (pui_ani_t *)calloc(1, sizeof(pui_ani_t));
742 pui_err("Failed to allocate memory for pui ani mgr !\n");
748 ani->cmd = PUI_ANI_CMD_NONE;
750 ani->status = PUI_ANI_STATUS_INITIAL;
751 ani->ani_data = ani_data;
755 handle->ani_handles = eina_list_append(handle->ani_handles, ani_h);
762 handle->backend_module_data->ani_destroy(ani_data);
772 pui_ani_destroy(pui_ani_h ani_h)
775 pui_ani_t *ani = NULL;
776 pui_backend_module_data *backend_module_data = NULL;
778 if (!ani_h || !ani_h->pui_handle)
781 handle = ani_h->pui_handle;
784 /* stop the animation being played already if any */
785 if (ani->status != PUI_ANI_STATUS_STOPPED)
786 pui_ani_control(ani_h, PUI_ANI_CMD_STOP, 0);
788 backend_module_data = handle->backend_module_data;
789 backend_module_data->ani_destroy(ani->ani_data);
790 ani->ani_data = NULL;
792 if (ani->frame_cb_timer)
794 ecore_timer_del(ani->frame_cb_timer);
795 ani->frame_cb_timer = NULL;
800 _pui_ani_event_handlers_shutdown(ani_h);
802 if (ani_h->frame_done_cb)
804 ecore_wl2_window_frame_callback_del(ani_h->frame_done_cb);
805 ani_h->frame_done_cb = NULL;
808 handle->ani_handles = eina_list_remove(handle->ani_handles, ani_h);