2 * Copyright © 2011 Intel Corporation
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.
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.
33 #include "compositor.h"
36 weston_spring_init(struct weston_spring *spring,
37 double k, double current, double target)
40 spring->friction = 400.0;
41 spring->current = current;
42 spring->previous = current;
43 spring->target = target;
44 spring->clip = WESTON_SPRING_OVERSHOOT;
50 weston_spring_update(struct weston_spring *spring, uint32_t msec)
52 double force, v, current, step;
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
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;
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;
73 current + (current - spring->previous) +
75 spring->previous = current;
77 switch (spring->clip) {
78 case WESTON_SPRING_OVERSHOOT:
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;
91 case WESTON_SPRING_BOUNCE:
92 if (spring->current > spring->max) {
94 2 * spring->max - spring->current;
96 2 * spring->max - spring->previous;
97 } else if (spring->current < spring->min) {
99 2 * spring->min - spring->current;
101 2 * spring->min - spring->previous;
106 spring->timestamp += 4;
111 weston_spring_done(struct weston_spring *spring)
113 return fabs(spring->previous - spring->target) < 0.002 &&
114 fabs(spring->current - spring->target) < 0.002;
117 typedef void (*weston_view_animation_frame_func_t)(struct weston_view_animation *animation);
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;
126 weston_view_animation_frame_func_t frame;
127 weston_view_animation_frame_func_t reset;
128 weston_view_animation_done_func_t done;
134 weston_view_animation_destroy(struct weston_view_animation *animation)
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);
143 animation->done(animation, animation->data);
148 handle_animation_view_destroy(struct wl_listener *listener, void *data)
150 struct weston_view_animation *animation =
151 container_of(listener,
152 struct weston_view_animation, listener);
154 weston_view_animation_destroy(animation);
158 weston_view_animation_frame(struct weston_animation *base,
159 struct weston_output *output, uint32_t msecs)
161 struct weston_view_animation *animation =
163 struct weston_view_animation, animation);
165 if (base->frame_counter <= 1)
166 animation->spring.timestamp = msecs;
168 weston_spring_update(&animation->spring, msecs);
170 if (weston_spring_done(&animation->spring)) {
171 weston_view_schedule_repaint(animation->view);
172 weston_view_animation_destroy(animation);
176 if (animation->frame)
177 animation->frame(animation);
179 weston_view_geometry_dirty(animation->view);
180 weston_view_schedule_repaint(animation->view);
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,
192 struct weston_view_animation *animation;
194 animation = malloc(sizeof *animation);
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;
207 weston_matrix_init(&animation->transform.matrix);
208 wl_list_insert(&view->geometry.transformation_list,
209 &animation->transform.link);
211 animation->animation.frame = weston_view_animation_frame;
213 animation->listener.notify = handle_animation_view_destroy;
214 wl_signal_add(&view->destroy_signal, &animation->listener);
216 wl_list_insert(&view->output->animation_list,
217 &animation->animation.link);
223 weston_view_animation_run(struct weston_view_animation *animation)
225 animation->animation.frame_counter = 0;
226 weston_view_animation_frame(&animation->animation, NULL, 0);
230 reset_alpha(struct weston_view_animation *animation)
232 struct weston_view *view = animation->view;
234 view->alpha = animation->stop;
238 zoom_frame(struct weston_view_animation *animation)
240 struct weston_view *es = animation->view;
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);
255 es->alpha = animation->spring.current;
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)
264 struct weston_view_animation *zoom;
266 zoom = weston_view_animation_create(view, start, stop,
267 zoom_frame, reset_alpha,
273 weston_spring_init(&zoom->spring, 300.0, start, stop);
274 zoom->spring.friction = 1400;
275 zoom->spring.previous = start - (stop - start) * 0.03;
277 weston_view_animation_run(zoom);
283 fade_frame(struct weston_view_animation *animation)
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;
290 animation->view->alpha = animation->spring.current;
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)
298 struct weston_view_animation *fade;
300 fade = weston_view_animation_create(view, start, end,
301 fade_frame, reset_alpha,
307 weston_spring_init(&fade->spring, 1000.0, start, end);
308 fade->spring.friction = 4000;
309 fade->spring.previous = start - (end - start) * 0.1;
313 weston_view_animation_run(fade);
319 weston_fade_update(struct weston_view_animation *fade, float target)
321 fade->spring.target = target;
325 stable_fade_frame(struct weston_view_animation *animation)
327 struct weston_view *back_view;
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;
334 animation->view->alpha = animation->spring.current;
336 back_view = (struct weston_view *) animation->private;
338 (animation->spring.target - animation->view->alpha) /
339 (1.0 - animation->view->alpha);
340 weston_view_geometry_dirty(back_view);
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)
348 struct weston_view_animation *fade;
350 fade = weston_view_animation_create(front_view, 0, 0,
351 stable_fade_frame, NULL,
352 done, data, back_view);
357 weston_spring_init(&fade->spring, 400, start, end);
358 fade->spring.friction = 1150;
360 front_view->alpha = start;
361 back_view->alpha = end;
363 weston_view_animation_run(fade);
369 slide_frame(struct weston_view_animation *animation)
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);
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)
384 struct weston_view_animation *animation;
386 animation = weston_view_animation_create(view, start, stop,
387 slide_frame, NULL, done,
392 weston_spring_init(&animation->spring, 400.0, 0.0, 1.0);
393 animation->spring.friction = 600;
394 animation->spring.clip = WESTON_SPRING_BOUNCE;
396 weston_view_animation_run(animation);
401 struct weston_move_animation {
405 weston_view_animation_done_func_t done;
409 move_frame(struct weston_view_animation *animation)
411 struct weston_move_animation *move = animation->private;
413 float progress = animation->spring.current;
416 progress = 1.0 - progress;
418 scale = animation->start +
419 (animation->stop - animation->start) *
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,
429 move_done(struct weston_view_animation *animation, void *data)
431 struct weston_move_animation *move = animation->private;
434 move->done(animation, data);
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)
444 struct weston_move_animation *move;
445 struct weston_view_animation *animation;
447 move = malloc(sizeof(*move));
452 move->reverse = reverse;
455 animation = weston_view_animation_create(view, start, end, move_frame,
456 NULL, move_done, data, move);
458 if (animation == NULL)
461 weston_spring_init(&animation->spring, 400.0, 0.0, 1.0);
462 animation->spring.friction = 1150;
464 weston_view_animation_run(animation);