default_backend: do not allocate frame whenever each frames
[platform/core/uifw/libpui.git] / backends / voice / default_ani_speaking.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 "../default_backend.h"
27
28 static default_frame_info_t *ani_frame = NULL;
29
30 static void
31 _ani_backend_speaking_get_led_rgb(default_frame_info_t *frame, int led_idx, unsigned int *r, unsigned int *g, unsigned int *b)
32 {
33         if (!frame) return;
34         if (!r || !g || !b) return;
35         if (led_idx > frame->num_led) return;
36
37         *r = (frame->leds[led_idx].color & LED_MASK_RED) >> 16;
38         *g = (frame->leds[led_idx].color & LED_MASK_GREEN) >> 8;
39         *b = (frame->leds[led_idx].color & LED_MASK_BLUE);
40 }
41
42 static default_frame_info_t *
43 _ani_backend_speaking_get_frame(default_ani_info *ani_info)
44 {
45         default_frame_info_t *key_frame, *key_frame2;
46         int idx, idx2;
47         unsigned int r, g, b, r2, g2, b2;
48         int r3, g3, b3;
49         double div;
50
51         if (ani_info->frame_idx == 0)
52         {
53                 ani_info->frame_max = (unsigned int)(ani_info->frames[ani_info->key_frame_cur].frame_duration / ani_info->interval);
54         }
55
56         idx = ani_info->key_frame_cur;
57         idx2 = (idx + 1) % ani_info->num_key_frames;
58
59         key_frame = &ani_info->frames[idx];
60         key_frame2 = &ani_info->frames[idx2];
61
62         div = (double)(ani_info->frame_idx) / (double)ani_info->frame_max;
63         r = g = b = r2 = g2 = b2 = 0x0;
64         for (int i = 0; i < key_frame->num_led; i++)
65         {
66                 _ani_backend_speaking_get_led_rgb(key_frame, i, &r, &g, &b);
67                 _ani_backend_speaking_get_led_rgb(key_frame2, i, &r2, &g2, &b2);
68                 r3 = (int)(r2 - r) * div + r;
69                 g3 = (int)(g2 - g) * div + g;
70                 b3 = (int)(b2 - b) * div + b;
71
72                 ani_frame->leds[i].color = (r3 << 16) + (g3 << 8) + (b3);
73         }
74         ani_info->frame_idx++;
75
76         if (ani_info->frame_idx >= ani_info->frame_max)
77         {
78                 ani_info->frame_idx = 0;
79                 ani_info->key_frame_cur = (ani_info->key_frame_cur + 1) % ani_info->num_key_frames;
80                 if (ani_info->repeat >= 0 && ani_info->key_frame_cur == 0)
81                         ani_info->repeat_cur++;
82         }
83
84         return ani_frame;
85 }
86
87 static pui_bool
88 _ani_backend_speaking_frame_cb(void *data, int serial)
89 {
90         pui_int_error e = PUI_INT_ERROR_NONE;
91         pui_ani_t *ani = (pui_ani_t *)data;
92         pui_backend_ani_data *ani_data = NULL;
93         pui_ani_control_buffer *buffer = NULL;
94         default_frame_info_t *frame;
95         unsigned int r = 0x0, g = 0x0, b = 0x0;
96
97         ani_data = pui_backend_ani_get_ani_data(ani);
98         default_ani_info *ani_info = (default_ani_info *)ani_data->ani_info;
99
100         /* TODO : make use of ani_info */
101         //(void) ani_info;
102
103         buffer = pui_backend_ani_get_buffer(ani);
104         if (!buffer) {
105                 pui_err("Failed to get buffer for animation\n");
106                 return (pui_bool)0;
107         }
108
109         frame = _ani_backend_speaking_get_frame(ani_info);
110         for(int i = 0; i<12; i++)
111         {
112                 _ani_backend_speaking_get_led_rgb(frame, i, &r, &g, &b);
113                 buffer->ptr[4*i] = 0;
114                 buffer->ptr[4*i + 1] = b; /* BLUE */
115                 buffer->ptr[4*i + 2] = g; /* GREEN */
116                 buffer->ptr[4*i + 3] = r; /* RED */
117         }
118         backend_util_cleanup_frame(frame);
119
120         e = pui_backend_ani_set_buffer(ani, buffer);
121
122         if (e != PUI_INT_ERROR_NONE)
123         {
124                 pui_err("Failed on setting buffer on animation !(e=%d)\n", e);
125                 return (pui_bool)0;
126         }
127
128         e = pui_backend_ani_update(ani);
129
130         if (e != PUI_INT_ERROR_NONE)
131         {
132                 pui_err("Failed on updating animation !(e=%d)\n", e);
133                 return (pui_bool)0;
134         }
135
136         pui_info("... update (serial=%d), (repeat| cur: %d, want: %d)\n",
137                 serial, ani_info->repeat_cur, ani_info->repeat);
138
139         if (ani_info->repeat >= 0 &&
140                 ani_info->repeat_cur >= ani_info->repeat)
141         {
142                 ani_data->ani_func->ani_stop(ani, EINA_FALSE);
143         }
144
145         return (pui_bool)1;
146 }
147
148 pui_error
149 _ani_speaking_start(pui_ani_t *ani, int repeat)
150 {
151         pui_bool ret = 0;
152         pui_int_error e = PUI_INT_ERROR_NONE;
153         pui_backend_ani_data *ani_data = NULL;
154
155         ani_data = pui_backend_ani_get_ani_data(ani);
156         default_ani_info *info = (default_ani_info *)ani_data->ani_info;
157
158         pui_info("... info->id: %s, repeat : %d, interval: %d\n", info->id, repeat, info->interval);
159
160         pui_backend_ani_status_update(ani, PUI_ANI_STATUS_STARTED);
161         if (repeat == 0) info->repeat = 1;
162         else info->repeat = repeat;
163
164         info->key_frame_cur = 0;
165         info->frame_idx = 0;
166         info->repeat_cur = 0;
167
168         if (!ani_frame)
169                 ani_frame = backend_util_alloc_frame(info);
170         ERROR_CHECK(ani_frame, return PUI_INT_ERROR_INVALID_RESOURCES, "Failed to alloc memory for frame\n");
171
172         ret = pui_backend_ani_add_frame_cb(ani, _ani_backend_speaking_frame_cb, info->interval / 1000.0);
173         if (!ret)
174         {
175                 pui_err("Failed to add frame callback !\n");
176                 e = PUI_INT_ERROR_INVALID_RESOURCES;
177         }
178
179         return e;
180 }
181
182 pui_error
183 _ani_speaking_stop(pui_ani_t *ani, pui_bool force)
184 {
185         pui_int_error e = PUI_INT_ERROR_NONE;
186         pui_backend_ani_data *ani_data = NULL;
187
188         ani_data = pui_backend_ani_get_ani_data(ani);
189         default_ani_info *info = (default_ani_info *)ani_data->ani_info;
190
191         //TODO
192         (void) info;
193
194         pui_info("... info->id: %s, force=%d\n", info->id, force);
195
196         pui_backend_ani_remove_frame_cb(ani);
197
198         if (force)
199                 pui_backend_ani_status_update(ani, PUI_ANI_STATUS_PAUSED);
200         else
201                 pui_backend_ani_status_update(ani, PUI_ANI_STATUS_STOPPED);
202
203         if (ani_frame)
204         {
205                 backend_util_free_frame(ani_frame);
206                 ani_frame = NULL;
207         }
208
209         return e;
210 }
211
212
213 void
214 pui_default_backend_ani_speaking_func_set(pui_backend_ani_func *func)
215 {
216         if (!func) return;
217
218         func->ani_start = _ani_speaking_start;
219         func->ani_stop = _ani_speaking_stop;
220 }
221
222