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);
164 struct weston_compositor *compositor =
165 animation->view->surface->compositor;
167 if (base->frame_counter <= 1)
168 animation->spring.timestamp = msecs;
170 weston_spring_update(&animation->spring, msecs);
172 if (weston_spring_done(&animation->spring)) {
173 weston_view_schedule_repaint(animation->view);
174 weston_view_animation_destroy(animation);
178 if (animation->frame)
179 animation->frame(animation);
181 weston_view_geometry_dirty(animation->view);
182 weston_view_schedule_repaint(animation->view);
184 /* The view's output_mask will be zero if its position is
185 * offscreen. Animations should always run but as they are also
186 * run off the repaint cycle, if there's nothing to repaint
187 * the animation stops running. Therefore if we catch this situation
188 * and schedule a repaint on all outputs it will be avoided.
190 if (animation->view->output_mask == 0)
191 weston_compositor_schedule_repaint(compositor);
194 static struct weston_view_animation *
195 weston_view_animation_create(struct weston_view *view,
196 float start, float stop,
197 weston_view_animation_frame_func_t frame,
198 weston_view_animation_frame_func_t reset,
199 weston_view_animation_done_func_t done,
203 struct weston_view_animation *animation;
205 animation = malloc(sizeof *animation);
209 animation->view = view;
210 animation->frame = frame;
211 animation->reset = reset;
212 animation->done = done;
213 animation->data = data;
214 animation->start = start;
215 animation->stop = stop;
216 animation->private = private;
218 weston_matrix_init(&animation->transform.matrix);
219 wl_list_insert(&view->geometry.transformation_list,
220 &animation->transform.link);
222 animation->animation.frame = weston_view_animation_frame;
224 animation->listener.notify = handle_animation_view_destroy;
225 wl_signal_add(&view->destroy_signal, &animation->listener);
227 wl_list_insert(&view->output->animation_list,
228 &animation->animation.link);
234 weston_view_animation_run(struct weston_view_animation *animation)
236 animation->animation.frame_counter = 0;
237 weston_view_animation_frame(&animation->animation, NULL, 0);
241 reset_alpha(struct weston_view_animation *animation)
243 struct weston_view *view = animation->view;
245 view->alpha = animation->stop;
249 zoom_frame(struct weston_view_animation *animation)
251 struct weston_view *es = animation->view;
254 scale = animation->start +
255 (animation->stop - animation->start) *
256 animation->spring.current;
257 weston_matrix_init(&animation->transform.matrix);
258 weston_matrix_translate(&animation->transform.matrix,
259 -0.5f * es->surface->width,
260 -0.5f * es->surface->height, 0);
261 weston_matrix_scale(&animation->transform.matrix, scale, scale, scale);
262 weston_matrix_translate(&animation->transform.matrix,
263 0.5f * es->surface->width,
264 0.5f * es->surface->height, 0);
266 es->alpha = animation->spring.current;
271 WL_EXPORT struct weston_view_animation *
272 weston_zoom_run(struct weston_view *view, float start, float stop,
273 weston_view_animation_done_func_t done, void *data)
275 struct weston_view_animation *zoom;
277 zoom = weston_view_animation_create(view, start, stop,
278 zoom_frame, reset_alpha,
284 weston_spring_init(&zoom->spring, 300.0, start, stop);
285 zoom->spring.friction = 1400;
286 zoom->spring.previous = start - (stop - start) * 0.03;
288 weston_view_animation_run(zoom);
294 fade_frame(struct weston_view_animation *animation)
296 if (animation->spring.current > 0.999)
297 animation->view->alpha = 1;
298 else if (animation->spring.current < 0.001 )
299 animation->view->alpha = 0;
301 animation->view->alpha = animation->spring.current;
304 WL_EXPORT struct weston_view_animation *
305 weston_fade_run(struct weston_view *view,
306 float start, float end, float k,
307 weston_view_animation_done_func_t done, void *data)
309 struct weston_view_animation *fade;
311 fade = weston_view_animation_create(view, start, end,
312 fade_frame, reset_alpha,
318 weston_spring_init(&fade->spring, 1000.0, start, end);
319 fade->spring.friction = 4000;
320 fade->spring.previous = start - (end - start) * 0.1;
324 weston_view_animation_run(fade);
330 weston_fade_update(struct weston_view_animation *fade, float target)
332 fade->spring.target = target;
336 stable_fade_frame(struct weston_view_animation *animation)
338 struct weston_view *back_view;
340 if (animation->spring.current > 0.999)
341 animation->view->alpha = 1;
342 else if (animation->spring.current < 0.001 )
343 animation->view->alpha = 0;
345 animation->view->alpha = animation->spring.current;
347 back_view = (struct weston_view *) animation->private;
349 (animation->spring.target - animation->view->alpha) /
350 (1.0 - animation->view->alpha);
351 weston_view_geometry_dirty(back_view);
354 WL_EXPORT struct weston_view_animation *
355 weston_stable_fade_run(struct weston_view *front_view, float start,
356 struct weston_view *back_view, float end,
357 weston_view_animation_done_func_t done, void *data)
359 struct weston_view_animation *fade;
361 fade = weston_view_animation_create(front_view, 0, 0,
362 stable_fade_frame, NULL,
363 done, data, back_view);
368 weston_spring_init(&fade->spring, 400, start, end);
369 fade->spring.friction = 1150;
371 front_view->alpha = start;
372 back_view->alpha = end;
374 weston_view_animation_run(fade);
380 slide_frame(struct weston_view_animation *animation)
384 scale = animation->start +
385 (animation->stop - animation->start) *
386 animation->spring.current;
387 weston_matrix_init(&animation->transform.matrix);
388 weston_matrix_translate(&animation->transform.matrix, 0, scale, 0);
391 WL_EXPORT struct weston_view_animation *
392 weston_slide_run(struct weston_view *view, float start, float stop,
393 weston_view_animation_done_func_t done, void *data)
395 struct weston_view_animation *animation;
397 animation = weston_view_animation_create(view, start, stop,
398 slide_frame, NULL, done,
403 weston_spring_init(&animation->spring, 400.0, 0.0, 1.0);
404 animation->spring.friction = 600;
405 animation->spring.clip = WESTON_SPRING_BOUNCE;
407 weston_view_animation_run(animation);
412 struct weston_move_animation {
416 weston_view_animation_done_func_t done;
420 move_frame(struct weston_view_animation *animation)
422 struct weston_move_animation *move = animation->private;
424 float progress = animation->spring.current;
427 progress = 1.0 - progress;
429 scale = animation->start +
430 (animation->stop - animation->start) *
432 weston_matrix_init(&animation->transform.matrix);
433 weston_matrix_scale(&animation->transform.matrix, scale, scale, 1.0f);
434 weston_matrix_translate(&animation->transform.matrix,
435 move->dx * progress, move->dy * progress,
440 move_done(struct weston_view_animation *animation, void *data)
442 struct weston_move_animation *move = animation->private;
445 move->done(animation, data);
450 WL_EXPORT struct weston_view_animation *
451 weston_move_scale_run(struct weston_view *view, int dx, int dy,
452 float start, float end, int reverse,
453 weston_view_animation_done_func_t done, void *data)
455 struct weston_move_animation *move;
456 struct weston_view_animation *animation;
458 move = malloc(sizeof(*move));
463 move->reverse = reverse;
466 animation = weston_view_animation_create(view, start, end, move_frame,
467 NULL, move_done, data, move);
469 if (animation == NULL)
472 weston_spring_init(&animation->spring, 400.0, 0.0, 1.0);
473 animation->spring.friction = 1150;
475 weston_view_animation_run(animation);