392e32dcdf33e8bdf7a2b4235e09c0083f7bc398
[profile/ivi/weston-ivi-shell.git] / src / animation.c
1 /*
2  * Copyright © 2011 Intel Corporation
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and
5  * its documentation for any purpose is hereby granted without fee, provided
6  * that the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation, and that the name of the copyright holders not be used in
9  * advertising or publicity pertaining to distribution of the software
10  * without specific, written prior permission.  The copyright holders make
11  * no representations about the suitability of this software for any
12  * purpose.  It is provided "as is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22
23 #include "config.h"
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include <math.h>
29
30 #include <unistd.h>
31 #include <fcntl.h>
32
33 #include "compositor.h"
34
35 WL_EXPORT void
36 weston_spring_init(struct weston_spring *spring,
37                    double k, double current, double target)
38 {
39         spring->k = k;
40         spring->friction = 400.0;
41         spring->current = current;
42         spring->previous = current;
43         spring->target = target;
44         spring->clip = WESTON_SPRING_OVERSHOOT;
45         spring->min = 0.0;
46         spring->max = 1.0;
47 }
48
49 WL_EXPORT void
50 weston_spring_update(struct weston_spring *spring, uint32_t msec)
51 {
52         double force, v, current, step;
53
54         /* Limit the number of executions of the loop below by ensuring that
55          * the timestamp for last update of the spring is no more than 1s ago.
56          * This handles the case where time moves backwards or forwards in
57          * large jumps.
58          */
59         if (msec - spring->timestamp > 1000) {
60                 weston_log("unexpectedly large timestamp jump (from %u to %u)\n",
61                            spring->timestamp, msec);
62                 spring->timestamp = msec - 1000;
63         }
64
65         step = 0.01;
66         while (4 < msec - spring->timestamp) {
67                 current = spring->current;
68                 v = current - spring->previous;
69                 force = spring->k * (spring->target - current) / 10.0 +
70                         (spring->previous - current) - v * spring->friction;
71
72                 spring->current =
73                         current + (current - spring->previous) +
74                         force * step * step;
75                 spring->previous = current;
76
77                 switch (spring->clip) {
78                 case WESTON_SPRING_OVERSHOOT:
79                         break;
80
81                 case WESTON_SPRING_CLAMP:
82                         if (spring->current > spring->max) {
83                                 spring->current = spring->max;
84                                 spring->previous = spring->max;
85                         } else if (spring->current < 0.0) {
86                                 spring->current = spring->min;
87                                 spring->previous = spring->min;
88                         }
89                         break;
90
91                 case WESTON_SPRING_BOUNCE:
92                         if (spring->current > spring->max) {
93                                 spring->current =
94                                         2 * spring->max - spring->current;
95                                 spring->previous =
96                                         2 * spring->max - spring->previous;
97                         } else if (spring->current < spring->min) {
98                                 spring->current =
99                                         2 * spring->min - spring->current;
100                                 spring->previous =
101                                         2 * spring->min - spring->previous;
102                         }
103                         break;
104                 }
105
106                 spring->timestamp += 4;
107         }
108 }
109
110 WL_EXPORT int
111 weston_spring_done(struct weston_spring *spring)
112 {
113         return fabs(spring->previous - spring->target) < 0.002 &&
114                 fabs(spring->current - spring->target) < 0.002;
115 }
116
117 typedef void (*weston_view_animation_frame_func_t)(struct weston_view_animation *animation);
118
119 struct weston_view_animation {
120         struct weston_view *view;
121         struct weston_animation animation;
122         struct weston_spring spring;
123         struct weston_transform transform;
124         struct wl_listener listener;
125         float start, stop;
126         weston_view_animation_frame_func_t frame;
127         weston_view_animation_frame_func_t reset;
128         weston_view_animation_done_func_t done;
129         void *data;
130         void *private;
131 };
132
133 WL_EXPORT void
134 weston_view_animation_destroy(struct weston_view_animation *animation)
135 {
136         wl_list_remove(&animation->animation.link);
137         wl_list_remove(&animation->listener.link);
138         wl_list_remove(&animation->transform.link);
139         if (animation->reset)
140                 animation->reset(animation);
141         weston_view_geometry_dirty(animation->view);
142         if (animation->done)
143                 animation->done(animation, animation->data);
144         free(animation);
145 }
146
147 static void
148 handle_animation_view_destroy(struct wl_listener *listener, void *data)
149 {
150         struct weston_view_animation *animation =
151                 container_of(listener,
152                              struct weston_view_animation, listener);
153
154         weston_view_animation_destroy(animation);
155 }
156
157 static void
158 weston_view_animation_frame(struct weston_animation *base,
159                             struct weston_output *output, uint32_t msecs)
160 {
161         struct weston_view_animation *animation =
162                 container_of(base,
163                              struct weston_view_animation, animation);
164
165         if (base->frame_counter <= 1)
166                 animation->spring.timestamp = msecs;
167
168         weston_spring_update(&animation->spring, msecs);
169
170         if (weston_spring_done(&animation->spring)) {
171                 weston_view_schedule_repaint(animation->view);
172                 weston_view_animation_destroy(animation);
173                 return;
174         }
175
176         if (animation->frame)
177                 animation->frame(animation);
178
179         weston_view_geometry_dirty(animation->view);
180         weston_view_schedule_repaint(animation->view);
181 }
182
183 static struct weston_view_animation *
184 weston_view_animation_create(struct weston_view *view,
185                              float start, float stop,
186                              weston_view_animation_frame_func_t frame,
187                              weston_view_animation_frame_func_t reset,
188                              weston_view_animation_done_func_t done,
189                              void *data,
190                              void *private)
191 {
192         struct weston_view_animation *animation;
193
194         animation = malloc(sizeof *animation);
195         if (!animation)
196                 return NULL;
197
198         animation->view = view;
199         animation->frame = frame;
200         animation->reset = reset;
201         animation->done = done;
202         animation->data = data;
203         animation->start = start;
204         animation->stop = stop;
205         animation->private = private;
206
207         weston_matrix_init(&animation->transform.matrix);
208         wl_list_insert(&view->geometry.transformation_list,
209                        &animation->transform.link);
210
211         animation->animation.frame = weston_view_animation_frame;
212
213         animation->listener.notify = handle_animation_view_destroy;
214         wl_signal_add(&view->destroy_signal, &animation->listener);
215
216         wl_list_insert(&view->output->animation_list,
217                        &animation->animation.link);
218
219         return animation;
220 }
221
222 static void
223 weston_view_animation_run(struct weston_view_animation *animation)
224 {
225         animation->animation.frame_counter = 0;
226         weston_view_animation_frame(&animation->animation, NULL, 0);
227 }
228
229 static void
230 reset_alpha(struct weston_view_animation *animation)
231 {
232         struct weston_view *view = animation->view;
233
234         view->alpha = animation->stop;
235 }
236
237 static void
238 zoom_frame(struct weston_view_animation *animation)
239 {
240         struct weston_view *es = animation->view;
241         float scale;
242
243         scale = animation->start +
244                 (animation->stop - animation->start) *
245                 animation->spring.current;
246         weston_matrix_init(&animation->transform.matrix);
247         weston_matrix_translate(&animation->transform.matrix,
248                                 -0.5f * es->surface->width,
249                                 -0.5f * es->surface->height, 0);
250         weston_matrix_scale(&animation->transform.matrix, scale, scale, scale);
251         weston_matrix_translate(&animation->transform.matrix,
252                                 0.5f * es->surface->width,
253                                 0.5f * es->surface->height, 0);
254
255         es->alpha = animation->spring.current;
256         if (es->alpha > 1.0)
257                 es->alpha = 1.0;
258 }
259
260 WL_EXPORT struct weston_view_animation *
261 weston_zoom_run(struct weston_view *view, float start, float stop,
262                 weston_view_animation_done_func_t done, void *data)
263 {
264         struct weston_view_animation *zoom;
265
266         zoom = weston_view_animation_create(view, start, stop,
267                                             zoom_frame, reset_alpha,
268                                             done, data, NULL);
269
270         if (zoom == NULL)
271                 return NULL;
272
273         weston_spring_init(&zoom->spring, 300.0, start, stop);
274         zoom->spring.friction = 1400;
275         zoom->spring.previous = start - (stop - start) * 0.03;
276
277         weston_view_animation_run(zoom);
278
279         return zoom;
280 }
281
282 static void
283 fade_frame(struct weston_view_animation *animation)
284 {
285         if (animation->spring.current > 0.999)
286                 animation->view->alpha = 1;
287         else if (animation->spring.current < 0.001 )
288                 animation->view->alpha = 0;
289         else
290                 animation->view->alpha = animation->spring.current;
291 }
292
293 WL_EXPORT struct weston_view_animation *
294 weston_fade_run(struct weston_view *view,
295                 float start, float end, float k,
296                 weston_view_animation_done_func_t done, void *data)
297 {
298         struct weston_view_animation *fade;
299
300         fade = weston_view_animation_create(view, start, end,
301                                             fade_frame, reset_alpha,
302                                             done, data, NULL);
303
304         if (fade == NULL)
305                 return NULL;
306
307         weston_spring_init(&fade->spring, 1000.0, start, end);
308         fade->spring.friction = 4000;
309         fade->spring.previous = start - (end - start) * 0.1;
310
311         view->alpha = start;
312
313         weston_view_animation_run(fade);
314
315         return fade;
316 }
317
318 WL_EXPORT void
319 weston_fade_update(struct weston_view_animation *fade, float target)
320 {
321         fade->spring.target = target;
322 }
323
324 static void
325 stable_fade_frame(struct weston_view_animation *animation)
326 {
327         struct weston_view *back_view;
328
329         if (animation->spring.current > 0.999)
330                 animation->view->alpha = 1;
331         else if (animation->spring.current < 0.001 )
332                 animation->view->alpha = 0;
333         else
334                 animation->view->alpha = animation->spring.current;
335
336         back_view = (struct weston_view *) animation->private;
337         back_view->alpha =
338                 (animation->spring.target - animation->view->alpha) /
339                 (1.0 - animation->view->alpha);
340         weston_view_geometry_dirty(back_view);
341 }
342
343 WL_EXPORT struct weston_view_animation *
344 weston_stable_fade_run(struct weston_view *front_view, float start,
345                 struct weston_view *back_view, float end,
346                 weston_view_animation_done_func_t done, void *data)
347 {
348         struct weston_view_animation *fade;
349
350         fade = weston_view_animation_create(front_view, 0, 0,
351                                             stable_fade_frame, NULL,
352                                             done, data, back_view);
353
354         if (fade == NULL)
355                 return NULL;
356
357         weston_spring_init(&fade->spring, 400, start, end);
358         fade->spring.friction = 1150;
359
360         front_view->alpha = start;
361         back_view->alpha = end;
362
363         weston_view_animation_run(fade);
364
365         return fade;
366 }
367
368 static void
369 slide_frame(struct weston_view_animation *animation)
370 {
371         float scale;
372
373         scale = animation->start +
374                 (animation->stop - animation->start) *
375                 animation->spring.current;
376         weston_matrix_init(&animation->transform.matrix);
377         weston_matrix_translate(&animation->transform.matrix, 0, scale, 0);
378 }
379
380 WL_EXPORT struct weston_view_animation *
381 weston_slide_run(struct weston_view *view, float start, float stop,
382                  weston_view_animation_done_func_t done, void *data)
383 {
384         struct weston_view_animation *animation;
385
386         animation = weston_view_animation_create(view, start, stop,
387                                               slide_frame, NULL, done,
388                                               data, NULL);
389         if (!animation)
390                 return NULL;
391
392         weston_spring_init(&animation->spring, 400.0, 0.0, 1.0);
393         animation->spring.friction = 600;
394         animation->spring.clip = WESTON_SPRING_BOUNCE;
395
396         weston_view_animation_run(animation);
397
398         return animation;
399 }
400
401 struct weston_move_animation {
402         int dx;
403         int dy;
404         int reverse;
405         weston_view_animation_done_func_t done;
406 };
407
408 static void
409 move_frame(struct weston_view_animation *animation)
410 {
411         struct weston_move_animation *move = animation->private;
412         float scale;
413         float progress = animation->spring.current;
414
415         if (move->reverse)
416                 progress = 1.0 - progress;
417
418         scale = animation->start +
419                 (animation->stop - animation->start) *
420                 progress;
421         weston_matrix_init(&animation->transform.matrix);
422         weston_matrix_scale(&animation->transform.matrix, scale, scale, 1.0f);
423         weston_matrix_translate(&animation->transform.matrix,
424                                 move->dx * progress, move->dy * progress,
425                                 0);
426 }
427
428 static void
429 move_done(struct weston_view_animation *animation, void *data)
430 {
431         struct weston_move_animation *move = animation->private;
432
433         if (move->done)
434                 move->done(animation, data);
435
436         free(move);
437 }
438
439 WL_EXPORT struct weston_view_animation *
440 weston_move_scale_run(struct weston_view *view, int dx, int dy,
441                       float start, float end, int reverse,
442                       weston_view_animation_done_func_t done, void *data)
443 {
444         struct weston_move_animation *move;
445         struct weston_view_animation *animation;
446
447         move = malloc(sizeof(*move));
448         if (!move)
449                 return NULL;
450         move->dx = dx;
451         move->dy = dy;
452         move->reverse = reverse;
453         move->done = done;
454
455         animation = weston_view_animation_create(view, start, end, move_frame,
456                                                  NULL, move_done, data, move);
457
458         if (animation == NULL)
459                 return NULL;
460
461         weston_spring_init(&animation->spring, 400.0, 0.0, 1.0);
462         animation->spring.friction = 1150;
463
464         weston_view_animation_run(animation);
465
466         return animation;
467 }