PUI: fix frame_cb and pui_ani_control not to make irregular timer callback call
[platform/core/uifw/libpui.git] / src / PUI_ani.c
1 /*
2  * Copyright © 2019 Samsung Electronics co., Ltd. All Rights Reserved.
3  *
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:
11  *
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.
15  *
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
23  * SOFTWARE.
24  */
25
26 #include "PUI_internal.h"
27 #include "PUI_backend.h"
28 #include "PUI.h"
29 #include <Eina.h>
30
31 pui_int_error
32 _pui_ani_error_to_int_error(pui_error e)
33 {
34         pui_int_error ei;
35
36         switch (e)
37         {
38                 case PUI_ERROR_NONE:
39                         ei = PUI_INT_ERROR_NONE;
40                         break;
41
42                 case PUI_ERROR_INVALID_HANDLE:
43                         ei = PUI_INT_ERROR_INVALID_HANDLE;
44                         break;
45
46                 case PUI_ERROR_INVALID_SURFACE:
47                         ei = PUI_INT_ERROR_INVALID_SURFACE;
48                         break;
49
50                 case PUI_ERROR_INVALID_BUFFER:
51                         ei = PUI_INT_ERROR_INVALID_BUFFER;
52                         break;
53
54                 default:
55                         ei = PUI_INT_ERROR_UNKNOWN;
56                         break;
57         }
58
59         return ei;
60 }
61
62 static void
63 _pui_ani_cb_frame_done(Ecore_Wl2_Window *win, uint32_t timestamp EINA_UNUSED, void *data)
64 {
65         pui_h handle = (pui_h) data;
66
67         pui_info("Frame done ! (window=0x%x)\n", ecore_wl2_window_id_get(win));
68
69         /* TODO : make use of handle */
70         (void) handle;
71
72         return;
73 }
74
75 pui_ani_control_buffer *
76 pui_ani_get_buffer(pui_ani_h ani_h)
77 {
78         pui_h handle = NULL;
79         pui_ani_control_buffer *buffer = NULL;
80
81         if (!ani_h)
82         {
83                 pui_err("Invalid pui ani handle !\n");
84                 return NULL;
85         }
86
87         handle = ani_h->pui_handle;
88         buffer = pui_display_get_buffer(handle);
89
90         return buffer;
91 }
92
93 pui_int_error
94 pui_ani_set_buffer(pui_ani_h ani_h, pui_ani_control_buffer *buffer)
95 {
96         pui_error e = PUI_ERROR_NONE;
97         pui_h handle = NULL;
98
99         handle = ani_h->pui_handle;
100         e = pui_display_set_buffer(handle, buffer);
101
102         return _pui_ani_error_to_int_error(e);
103 }
104
105 pui_int_error
106 pui_ani_update(pui_ani_h ani_h)
107 {
108         pui_error e = PUI_ERROR_NONE;
109         pui_h handle = NULL;
110
111         if (!ani_h)
112         {
113                 pui_err("Invalid ani handle !\n");
114                 return PUI_INT_ERROR_INVALID_HANDLE;
115         }
116
117         handle = ani_h->pui_handle;
118         e = pui_display_update(handle);
119
120         return _pui_ani_error_to_int_error(e);
121 }
122
123 static Eina_Bool
124 _pui_ani_frame_cb(void *data)
125 {
126         Eina_Bool ret;
127         Ecore_Timer *timer = NULL;
128
129         pui_ani_t *ani = (pui_ani_t *)data;
130
131         if (!ani)
132         {
133                 pui_err("Invalid pui ani !\n");
134                 return ECORE_CALLBACK_CANCEL;
135         }
136
137         if (!ani->backend_frame_cb)
138         {
139                 pui_err("Invalid backend frame_cb !\n");
140                 return ECORE_CALLBACK_CANCEL;
141         }
142
143         if (!ani->frame_cb_timer)
144         {
145                 pui_err("Invalid frame_cb timer !\n");
146                 return ECORE_CALLBACK_CANCEL;
147         }
148
149         pui_info("...\n");
150
151         ret = (Eina_Bool)ani->backend_frame_cb(ani, ++ani->serial);
152
153         if (!ret)
154         {
155                 pui_err("Failed on backend's frame_cb !Frame_cb will be removed forcefuly !\n");
156
157                 pui_ani_remove_frame_cb(ani);
158
159                 return EINA_FALSE;
160         }
161
162         if (ret && PUI_ANI_STATUS_STARTED == ani->status)
163                 pui_ani_status_update(ani, PUI_ANI_STATUS_RUNNING);
164
165         ecore_timer_del(ani->frame_cb_timer);
166         ani->frame_cb_timer = NULL;
167
168         timer = ecore_timer_add(ani->frame_interval, _pui_ani_frame_cb, ani);
169
170         if (!timer)
171         {
172                 pui_err("Failed to add ecore timer !\n");
173                 return 0;
174         }
175
176         ani->frame_cb_timer = timer;
177
178         return EINA_FALSE;
179 }
180
181 pui_bool
182 pui_ani_add_frame_cb(pui_ani_t *ani, pui_bool (*frame_cb)(void *data, int serial), double frame_interval)
183 {
184         Ecore_Timer *timer = NULL;
185
186         if (!ani)
187         {
188                 pui_err("Invalid put ani !\n");
189                 return 0;
190         }
191
192         if (frame_interval <= 0.0f)
193         {
194                 pui_err("Invalid frame interval (%.2f) ! frame interval must be larger than 0.\n", frame_interval);
195                 return 0;
196         }
197
198         ani->frame_cb = _pui_ani_frame_cb;
199         ani->backend_frame_cb = frame_cb;
200         ani->frame_interval = frame_interval;
201         ani->frame_cb_data = ani;
202         ani->serial = 0;
203
204         timer = ecore_timer_add(frame_interval, _pui_ani_frame_cb, ani);
205
206         if (!timer)
207         {
208                 pui_err("Failed to add ecore timer !\n");
209                 return 0;
210         }
211
212         ani->frame_cb_timer = timer;
213
214         pui_info("[Frame callback added][ani id=%s] frame_interval=%.2f\n", ani->id, frame_interval);
215
216         /* call frame_cb for starting the first frame */
217         _pui_ani_frame_cb(ani);
218
219         return 1;
220 }
221
222 void
223 pui_ani_remove_frame_cb(pui_ani_t *ani)
224 {
225         if (!ani)
226         {
227                 pui_err("Invalid put ani !\n");
228                 return;
229         }
230
231         if (ani->frame_cb_timer)
232         {
233                 ecore_timer_del(ani->frame_cb_timer);
234                 ani->frame_cb_timer = NULL;
235         }
236
237         ani->frame_cb = NULL;
238         ani->backend_frame_cb = NULL;
239         ani->frame_interval = 0;
240         ani->frame_cb_data = NULL;
241
242         pui_info("[Frame callback removed][ani id=%s]\n", ani->id);
243 }
244
245 pui_id
246 pui_ani_get_id(pui_ani_h ani_h)
247 {
248         if (!ani_h || !ani_h->ani)
249                 return NULL;
250
251         return ani_h->ani->id;
252 }
253
254 pui_ani_cmd
255 pui_ani_get_cmd(pui_ani_h ani_h)
256 {
257         if (!ani_h || !ani_h->ani)
258                 return PUI_ANI_CMD_NONE;
259
260         return ani_h->ani->cmd;
261 }
262
263 int
264 pui_ani_get_repeat(pui_ani_h ani_h)
265 {
266         if (!ani_h || !ani_h->ani)
267                 return 0;
268
269         return ani_h->ani->repeat;
270 }
271
272 pui_backend_ani_data *
273 pui_ani_get_ani_data(pui_ani_t *ani)
274 {
275         if (!ani)
276                 return NULL;
277
278         return ani->ani_data;
279 }
280
281 void
282 pui_ani_status_update(pui_ani_t *ani, pui_ani_status status)
283 {
284         int ev_type = -1;
285         pui_ani_h ani_h;
286         PUI_Event_Animation_Status *e = NULL;
287
288         if (!ani)
289         {
290                 pui_err("Invalid pui ani !\n");
291                 return;
292         }
293
294         if (ani->status == status)
295                 return;
296
297         ani_h = ani->ani_h;
298
299         e = (PUI_Event_Animation_Status *)calloc(1, sizeof(PUI_Event_Animation_Status));
300
301         if (!e)
302         {
303                 pui_err("Failed to allocate memory for PUI Event !\n");
304                 return;
305         }
306
307         ani->status = status;
308         e->ani_h = ani_h;
309         e->win = ecore_wl2_window_id_get(ani_h->pui_handle->win);
310         e->status = status;
311
312         switch (status)
313         {
314                 case PUI_ANI_STATUS_STARTED:
315                         ev_type = PUI_EVENT_ANI_STARTED;
316                         pui_info("[Status update][ani id:%s] PUI_EVENT_ANI_STARTED event has been added.\n", ani->id);
317                         break;
318
319                 case PUI_ANI_STATUS_RUNNING:
320                         pui_info("[Status update][ani id:%s] PUI_ANI_STATUS_RUNNING !\n", ani->id);
321                         break;
322
323                 case PUI_ANI_STATUS_PAUSED:
324                         ev_type = PUI_EVENT_ANI_PAUSED;
325                         pui_info("[Status update][ani id:%s] PUI_EVENT_ANI_PAUSED event has been added.\n", ani->id);
326                         break;
327
328                 case PUI_ANI_STATUS_STOPPED:
329                         ev_type = PUI_EVENT_ANI_STOPPED;
330                         pui_info("[Status update][ani id:%s] PUI_EVENT_ANI_STOPPED event has been added.\n", ani->id);
331                         break;
332
333                 default:
334                         pui_err("Unknown status !(ani status=%d, id=%s) !\n", status, ani->id);
335                         return;
336         }
337
338         if (ev_type > 0)
339         {
340                 ecore_event_add(ev_type, e, NULL, ani_h);
341         }
342 }
343
344 pui_ani_status
345 pui_ani_status_get(pui_ani_t *ani)
346 {
347         pui_ani_status status = PUI_ANI_STATUS_UNKNOWN;
348
349         if (!ani)
350         {
351                 pui_err("Invalid pui ani !\n");
352                 return status;
353         }
354
355         return ani->status;
356 }
357
358 static pui_error
359 _pui_ani_control_with_force(pui_ani_h ani_h, pui_ani_cmd cmd, int repeat, pui_bool force)
360 {
361         pui_int_error ei = PUI_INT_ERROR_NONE;
362         pui_ani_t *ani = NULL;
363         pui_h handle = NULL;
364         pui_backend_ani_func *ani_func = NULL;
365
366         if (!ani_h)
367                 return PUI_ERROR_INVALID_ANI_HANDLE;
368
369         if (cmd < PUI_ANI_CMD_START || cmd >= PUI_ANI_CMD_LAST)
370         {
371                 pui_err("Invalid cmd ! (cmd=%d)\n", cmd);
372                 return PUI_ERROR_INVALID_ANI_CMD;
373         }
374
375         if (repeat < -1)
376         {
377                 pui_err("Invalid repeat count ! (repeat=%d)\n", repeat);
378                 return PUI_ERROR_INVALID_ANI_REPEAT;
379         }
380
381         handle = ani_h->pui_handle;
382         ani = ani_h->ani;
383
384         if (!ani || !ani->ani_data)
385         {
386                 pui_err("Invalid ani or ani_data !\n");
387                 return PUI_ERROR_INTERNAL;
388         }
389
390         ani_func = ani->ani_data->ani_func;
391
392         ani->cmd = cmd;
393         ani->repeat = repeat;
394
395         if (cmd == PUI_ANI_CMD_START)
396         {
397                 if (handle->manual_render)
398                 {
399                         pui_err("Manual render has been set ! Unable to start animation.\n");
400                         return PUI_ERROR_MANUAL_RENDER_ENABLED;
401                 }
402
403                 if (handle->current_ani_h && handle->current_ani_h != ani_h)
404                 {
405                         pui_ani_t *current_ani = handle->current_ani_h->ani;
406
407                         if (current_ani->status >= PUI_ANI_STATUS_STARTED &&
408                                 current_ani->status <= PUI_ANI_STATUS_RUNNING)
409                         {
410                                 pui_info("current_ani id=%s, status=%d\n", current_ani->id, current_ani->status);
411
412                                 ei = _pui_ani_control_with_force(handle->current_ani_h, PUI_ANI_CMD_STOP, 0, force);
413
414                                 if (ei != PUI_INT_ERROR_NONE)
415                                         pui_info("Failed to stop running previous animation ! (id:%s)\n", current_ani->id);
416
417                                 current_ani = NULL;
418                                 handle->current_ani_h = NULL;
419                         }
420                 }
421
422                 ei = ani_func->ani_start(ani, repeat);
423
424                 if (ei != PUI_INT_ERROR_NONE)
425                 {
426                         pui_err("Error on starting animation ! (id:%s, repeat:%d, status=%d))\n", ani->id, repeat, ani->status);
427
428                         if (ani->status != PUI_ANI_STATUS_RUNNING)
429                                 _pui_ani_control_with_force(ani_h, PUI_ANI_CMD_STOP, 0, 0);
430
431                         return PUI_ERROR_INTERNAL;
432                 }
433
434                 handle->current_ani_h = ani_h;
435         }
436         else//cmd == PUI_ANI_CMD_STOP
437         {
438                 ei = ani_func->ani_stop(ani, force);
439
440                 if (ani->frame_cb_timer)
441                         pui_ani_remove_frame_cb(ani);
442
443                 if (ei != PUI_INT_ERROR_NONE)
444                 {
445                         pui_err("Failied on stopping animation ! (id:%s)\n", ani->id);
446
447                         if (ani->status != PUI_ANI_STATUS_STOPPED)
448                                 pui_ani_status_update(ani, PUI_ANI_STATUS_STOPPED);
449
450                         return PUI_ERROR_INTERNAL;
451                 }
452
453                 return PUI_ERROR_NONE;
454         }
455
456         ani_h->frame_done_cb = ecore_wl2_window_frame_callback_add(handle->win, _pui_ani_cb_frame_done, handle);
457
458         if (!ani_h->frame_done_cb)
459         {
460                 pui_err("Failed to add frame callback !");
461                 goto err;
462         }
463
464         return PUI_ERROR_NONE;
465
466 err:
467         return PUI_ERROR_INTERNAL;
468 }
469
470 pui_error
471 pui_ani_control(pui_ani_h ani_h, pui_ani_cmd cmd, int repeat)
472 {
473         return _pui_ani_control_with_force(ani_h, cmd, repeat, 0);
474 }
475
476 static Eina_Bool
477 _cb_visibility_change(void *data, int type EINA_UNUSED, void *event)
478 {
479         pui_ani_h ani_h =  (pui_ani_h)data;
480         pui_ani_t *ani = ani_h->ani;
481         pui_h ph = ani_h->pui_handle;
482
483         Ecore_Wl2_Event_Window_Visibility_Change *ev;
484         PUI_Event_Animation_Status *e = NULL;
485
486         ev = event;
487
488         /* check if this is needed */
489         ph->visibility = !(ev->fully_obscured);
490
491         if (ev->fully_obscured)
492         {
493                 if (ani->status == PUI_ANI_STATUS_RUNNING)
494                 {
495                         pui_info("animation(%s) will be stopped as it lost its priority !\n", ani->id);
496
497                         _pui_ani_control_with_force(ani_h, PUI_ANI_CMD_STOP, 0, 1);
498                 }
499         }
500         else
501         {
502                 if (ani->status == PUI_ANI_STATUS_PAUSED)
503                 {
504                         e = (PUI_Event_Animation_Status *)calloc(1, sizeof(PUI_Event_Animation_Status));
505
506                         if (!e)
507                         {
508                                 pui_err("Failed to allocate memory for PUI Event !\n");
509                                 return ECORE_CALLBACK_PASS_ON;
510                         }
511
512                         e->ani_h = ani_h;
513                         e->win = ev->win;
514                         e->status = ani->status;
515
516                         if(ani->status == PUI_ANI_STATUS_PAUSED)
517                         {
518                                 pui_info("[Event added][ani id:%s] PUI_EVENT_ANI_READY_TO_RESUME event has been added.\n", ani_h->id);
519                                 ecore_event_add(PUI_EVENT_ANI_READY_TO_RESUME, e, NULL, ani_h);
520                         }
521                 }
522         }
523
524         return ECORE_CALLBACK_PASS_ON;
525 }
526
527 static void
528 _pui_ani_event_handlers_init(pui_ani_h ani_h)
529 {
530         Ecore_Event_Handler *h = NULL;
531
532         if (!ani_h)
533         {
534                 pui_err("Invalid handle !\n");
535                 return;
536         }
537
538         if (!ani_h->ecore_event_hdls)
539                 ani_h->ecore_event_hdls = eina_array_new(1);
540
541         h = ecore_event_handler_add(ECORE_WL2_EVENT_WINDOW_VISIBILITY_CHANGE, _cb_visibility_change, ani_h);
542         eina_array_push(ani_h->ecore_event_hdls, h);
543
544 }
545
546 static void
547 _pui_ani_event_handlers_shutdown(pui_ani_h ani_h)
548 {
549         if (!ani_h)
550         {
551                 pui_err("Invalid handle !\n");
552                 return;
553         }
554
555         if (ani_h->ecore_event_hdls)
556         {
557                 while (eina_array_count(ani_h->ecore_event_hdls))
558                         ecore_event_handler_del(eina_array_pop(ani_h->ecore_event_hdls));
559
560                 eina_array_free(ani_h->ecore_event_hdls);
561                 ani_h->ecore_event_hdls = NULL;
562         }
563 }
564
565 pui_ani_h
566 pui_ani_create(pui_h handle, pui_id id)
567 {
568         pui_ani_h ani_h = NULL;
569         pui_ani_t *ani = NULL;
570         pui_backend_ani_data *ani_data = NULL;
571
572         if (!handle || !handle->backend_module_data)
573         {
574                 pui_err("Invalid pui handle or backend module data !\n");
575                 return NULL;
576         }
577
578         ani_data = handle->backend_module_data->ani_create(id);
579
580         if (!ani_data)
581         {
582                 pui_err("Invalid ani data from backend module data !\n");
583                 return NULL;
584         }
585
586         ani_h = (pui_ani_h)calloc(1, sizeof(pui_ani));
587
588         if (!ani_h)
589         {
590                 pui_err("Failed to allocate memory for pui ani handle !\n");
591                 goto err;
592         }
593
594         ani_h->id = strdup(id);
595         ani_h->pui_handle = handle;
596         ani_h->ecore_event_hdls = NULL;
597
598         _pui_ani_event_handlers_init(ani_h);
599
600         ani = (pui_ani_t *)calloc(1, sizeof(pui_ani_t));
601
602         if (!ani)
603         {
604                 pui_err("Failed to allocate memory for pui ani mgr !\n");
605                 goto err;
606         }
607
608         ani->ani_h = ani_h;
609         ani->id = id;
610         ani->cmd = PUI_ANI_CMD_NONE;
611         ani->repeat = 0;
612         ani->status = PUI_ANI_STATUS_INITIAL;
613         ani->ani_data = ani_data;
614
615         ani_h->ani = ani;
616
617         handle->ani_handles = eina_list_append(handle->ani_handles, ani_h);
618
619         return ani_h;
620
621 err:
622         if (ani_data)
623         {
624                 handle->backend_module_data->ani_destroy(ani_data);
625         }
626
627         if (ani_h)
628                 free(ani_h);
629
630         return NULL;
631 }
632
633 void
634 pui_ani_destroy(pui_ani_h ani_h)
635 {
636         pui_h handle = NULL;
637         pui_ani_t *ani = NULL;
638         pui_backend_module_data *backend_module_data = NULL;
639
640         if (!ani_h || !ani_h->pui_handle)
641                 return;
642
643         handle = ani_h->pui_handle;
644         ani = ani_h->ani;
645
646         /* stop the animation being played already if any */
647         if (ani->status == PUI_ANI_STATUS_STARTED || ani->status == PUI_ANI_STATUS_RUNNING)
648                 pui_ani_control(ani_h, PUI_ANI_CMD_STOP, 0);
649
650         backend_module_data = handle->backend_module_data;
651         backend_module_data->ani_destroy(ani->ani_data);
652         ani->ani_data = NULL;
653
654         if (ani->frame_cb_timer)
655         {
656                 ecore_timer_del(ani->frame_cb_timer);
657                 ani->frame_cb_timer = NULL;
658         }
659
660         free(ani_h->ani);
661
662         _pui_ani_event_handlers_shutdown(ani_h);
663
664         if (ani_h->frame_done_cb)
665         {
666                 ecore_wl2_window_frame_callback_del(ani_h->frame_done_cb);
667                 ani_h->frame_done_cb = NULL;
668         }
669
670         handle->ani_handles = eina_list_remove(handle->ani_handles, ani_h);
671
672         free(ani_h->id);
673         free(ani_h);
674 }
675