Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 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 | |
Daniel Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 23 | #include "config.h" |
| 24 | |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 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; |
Kristian Høgsberg | 7eec9b3 | 2013-06-17 09:15:22 -0400 | [diff] [blame] | 44 | spring->clip = WESTON_SPRING_OVERSHOOT; |
Kristian Høgsberg | 091b096 | 2013-06-17 09:23:14 -0400 | [diff] [blame] | 45 | spring->min = 0.0; |
| 46 | spring->max = 1.0; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 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 | |
Kristian Høgsberg | 7eec9b3 | 2013-06-17 09:15:22 -0400 | [diff] [blame] | 77 | switch (spring->clip) { |
| 78 | case WESTON_SPRING_OVERSHOOT: |
| 79 | break; |
| 80 | |
| 81 | case WESTON_SPRING_CLAMP: |
Kristian Høgsberg | 091b096 | 2013-06-17 09:23:14 -0400 | [diff] [blame] | 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; |
Kristian Høgsberg | 7eec9b3 | 2013-06-17 09:15:22 -0400 | [diff] [blame] | 88 | } |
| 89 | break; |
| 90 | |
| 91 | case WESTON_SPRING_BOUNCE: |
Kristian Høgsberg | 091b096 | 2013-06-17 09:23:14 -0400 | [diff] [blame] | 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; |
Kristian Høgsberg | 7eec9b3 | 2013-06-17 09:15:22 -0400 | [diff] [blame] | 102 | } |
| 103 | break; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 104 | } |
| 105 | |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 106 | spring->timestamp += 4; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | WL_EXPORT int |
| 111 | weston_spring_done(struct weston_spring *spring) |
| 112 | { |
Kristian Høgsberg | c24744e | 2013-06-17 08:59:20 -0400 | [diff] [blame] | 113 | return fabs(spring->previous - spring->target) < 0.002 && |
| 114 | fabs(spring->current - spring->target) < 0.002; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | typedef void (*weston_surface_animation_frame_func_t)(struct weston_surface_animation *animation); |
| 118 | |
| 119 | struct weston_surface_animation { |
| 120 | struct weston_surface *surface; |
| 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_surface_animation_frame_func_t frame; |
| 127 | weston_surface_animation_done_func_t done; |
| 128 | void *data; |
| 129 | }; |
| 130 | |
| 131 | static void |
| 132 | weston_surface_animation_destroy(struct weston_surface_animation *animation) |
| 133 | { |
| 134 | wl_list_remove(&animation->animation.link); |
| 135 | wl_list_remove(&animation->listener.link); |
| 136 | wl_list_remove(&animation->transform.link); |
Pekka Paalanen | c3ce738 | 2013-03-08 14:56:49 +0200 | [diff] [blame] | 137 | weston_surface_geometry_dirty(animation->surface); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 138 | if (animation->done) |
| 139 | animation->done(animation, animation->data); |
| 140 | free(animation); |
| 141 | } |
| 142 | |
| 143 | static void |
| 144 | handle_animation_surface_destroy(struct wl_listener *listener, void *data) |
| 145 | { |
| 146 | struct weston_surface_animation *animation = |
| 147 | container_of(listener, |
| 148 | struct weston_surface_animation, listener); |
| 149 | |
| 150 | weston_surface_animation_destroy(animation); |
| 151 | } |
| 152 | |
| 153 | static void |
| 154 | weston_surface_animation_frame(struct weston_animation *base, |
| 155 | struct weston_output *output, uint32_t msecs) |
| 156 | { |
| 157 | struct weston_surface_animation *animation = |
| 158 | container_of(base, |
| 159 | struct weston_surface_animation, animation); |
| 160 | |
| 161 | if (base->frame_counter <= 1) |
| 162 | animation->spring.timestamp = msecs; |
| 163 | |
| 164 | weston_spring_update(&animation->spring, msecs); |
| 165 | |
| 166 | if (weston_spring_done(&animation->spring)) { |
| 167 | weston_surface_animation_destroy(animation); |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | if (animation->frame) |
| 172 | animation->frame(animation); |
| 173 | |
Pekka Paalanen | c3ce738 | 2013-03-08 14:56:49 +0200 | [diff] [blame] | 174 | weston_surface_geometry_dirty(animation->surface); |
Ander Conselvan de Oliveira | a457563 | 2013-02-21 18:35:23 +0200 | [diff] [blame] | 175 | weston_compositor_schedule_repaint(animation->surface->compositor); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | static struct weston_surface_animation * |
| 179 | weston_surface_animation_run(struct weston_surface *surface, |
| 180 | float start, float stop, |
| 181 | weston_surface_animation_frame_func_t frame, |
| 182 | weston_surface_animation_done_func_t done, |
| 183 | void *data) |
| 184 | { |
| 185 | struct weston_surface_animation *animation; |
| 186 | |
| 187 | animation = malloc(sizeof *animation); |
| 188 | if (!animation) |
| 189 | return NULL; |
| 190 | |
| 191 | animation->surface = surface; |
| 192 | animation->frame = frame; |
| 193 | animation->done = done; |
| 194 | animation->data = data; |
| 195 | animation->start = start; |
| 196 | animation->stop = stop; |
| 197 | weston_matrix_init(&animation->transform.matrix); |
| 198 | wl_list_insert(&surface->geometry.transformation_list, |
| 199 | &animation->transform.link); |
| 200 | weston_spring_init(&animation->spring, 200.0, 0.0, 1.0); |
| 201 | animation->spring.friction = 700; |
| 202 | animation->animation.frame_counter = 0; |
| 203 | animation->animation.frame = weston_surface_animation_frame; |
| 204 | weston_surface_animation_frame(&animation->animation, NULL, 0); |
| 205 | |
| 206 | animation->listener.notify = handle_animation_surface_destroy; |
Jason Ekstrand | 26ed73c | 2013-06-06 22:34:41 -0500 | [diff] [blame] | 207 | wl_signal_add(&surface->destroy_signal, &animation->listener); |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 208 | |
| 209 | wl_list_insert(&surface->output->animation_list, |
| 210 | &animation->animation.link); |
| 211 | |
| 212 | return animation; |
| 213 | } |
| 214 | |
| 215 | static void |
| 216 | zoom_frame(struct weston_surface_animation *animation) |
| 217 | { |
| 218 | struct weston_surface *es = animation->surface; |
| 219 | float scale; |
| 220 | |
| 221 | scale = animation->start + |
| 222 | (animation->stop - animation->start) * |
| 223 | animation->spring.current; |
| 224 | weston_matrix_init(&animation->transform.matrix); |
| 225 | weston_matrix_translate(&animation->transform.matrix, |
| 226 | -0.5f * es->geometry.width, |
| 227 | -0.5f * es->geometry.height, 0); |
| 228 | weston_matrix_scale(&animation->transform.matrix, scale, scale, scale); |
| 229 | weston_matrix_translate(&animation->transform.matrix, |
| 230 | 0.5f * es->geometry.width, |
| 231 | 0.5f * es->geometry.height, 0); |
| 232 | |
| 233 | es->alpha = animation->spring.current; |
| 234 | if (es->alpha > 1.0) |
| 235 | es->alpha = 1.0; |
| 236 | } |
| 237 | |
| 238 | WL_EXPORT struct weston_surface_animation * |
| 239 | weston_zoom_run(struct weston_surface *surface, float start, float stop, |
| 240 | weston_surface_animation_done_func_t done, void *data) |
| 241 | { |
| 242 | return weston_surface_animation_run(surface, start, stop, |
| 243 | zoom_frame, done, data); |
| 244 | } |
| 245 | |
| 246 | static void |
| 247 | fade_frame(struct weston_surface_animation *animation) |
| 248 | { |
Ander Conselvan de Oliveira | 8a91324 | 2013-02-21 18:35:18 +0200 | [diff] [blame] | 249 | if (animation->spring.current > 0.999) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 250 | animation->surface->alpha = 1; |
Ander Conselvan de Oliveira | 8a91324 | 2013-02-21 18:35:18 +0200 | [diff] [blame] | 251 | else if (animation->spring.current < 0.001 ) |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 252 | animation->surface->alpha = 0; |
| 253 | else |
| 254 | animation->surface->alpha = animation->spring.current; |
| 255 | } |
| 256 | |
| 257 | WL_EXPORT struct weston_surface_animation * |
| 258 | weston_fade_run(struct weston_surface *surface, |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 259 | float start, float end, float k, |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 260 | weston_surface_animation_done_func_t done, void *data) |
| 261 | { |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 262 | struct weston_surface_animation *fade; |
| 263 | |
| 264 | fade = weston_surface_animation_run(surface, 0, 0, |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 265 | fade_frame, done, data); |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 266 | |
| 267 | weston_spring_init(&fade->spring, k, start, end); |
Kristian Høgsberg | 5281fb1 | 2013-06-17 10:10:28 -0400 | [diff] [blame] | 268 | |
| 269 | fade->spring.friction = 1400; |
| 270 | fade->spring.previous = -(end - start) * 0.03; |
| 271 | |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 272 | surface->alpha = start; |
| 273 | |
| 274 | return fade; |
| 275 | } |
| 276 | |
| 277 | WL_EXPORT void |
Kristian Høgsberg | 5281fb1 | 2013-06-17 10:10:28 -0400 | [diff] [blame] | 278 | weston_fade_update(struct weston_surface_animation *fade, float target) |
Ander Conselvan de Oliveira | ee41605 | 2013-02-21 18:35:17 +0200 | [diff] [blame] | 279 | { |
Kristian Høgsberg | 5281fb1 | 2013-06-17 10:10:28 -0400 | [diff] [blame] | 280 | fade->spring.target = target; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | static void |
| 284 | slide_frame(struct weston_surface_animation *animation) |
| 285 | { |
| 286 | float scale; |
| 287 | |
| 288 | scale = animation->start + |
| 289 | (animation->stop - animation->start) * |
| 290 | animation->spring.current; |
| 291 | weston_matrix_init(&animation->transform.matrix); |
| 292 | weston_matrix_translate(&animation->transform.matrix, 0, scale, 0); |
| 293 | } |
| 294 | |
| 295 | WL_EXPORT struct weston_surface_animation * |
| 296 | weston_slide_run(struct weston_surface *surface, float start, float stop, |
| 297 | weston_surface_animation_done_func_t done, void *data) |
| 298 | { |
| 299 | struct weston_surface_animation *animation; |
| 300 | |
| 301 | animation = weston_surface_animation_run(surface, start, stop, |
| 302 | slide_frame, done, data); |
| 303 | if (!animation) |
| 304 | return NULL; |
| 305 | |
Kristian Høgsberg | dd2df78 | 2013-06-17 10:31:58 -0400 | [diff] [blame] | 306 | animation->spring.friction = 600; |
| 307 | animation->spring.k = 400; |
| 308 | animation->spring.clip = WESTON_SPRING_BOUNCE; |
Ander Conselvan de Oliveira | cbdebc2 | 2013-02-21 18:35:16 +0200 | [diff] [blame] | 309 | |
| 310 | return animation; |
| 311 | } |