Nobuhiko Tanibata | 6f9df65 | 2014-11-27 13:22:00 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 DENSO CORPORATION |
| 3 | * |
Bryce Harrington | af637c2 | 2015-06-11 12:55:55 -0700 | [diff] [blame^] | 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: |
Nobuhiko Tanibata | 6f9df65 | 2014-11-27 13:22:00 +0900 | [diff] [blame] | 11 | * |
Bryce Harrington | af637c2 | 2015-06-11 12:55:55 -0700 | [diff] [blame^] | 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. |
Nobuhiko Tanibata | 6f9df65 | 2014-11-27 13:22:00 +0900 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | #include <time.h> |
| 27 | #include <assert.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <stdio.h> |
| 30 | |
| 31 | #include "ivi-layout-export.h" |
| 32 | #include "ivi-layout-private.h" |
| 33 | |
| 34 | struct ivi_layout_transition; |
| 35 | |
| 36 | typedef void (*ivi_layout_transition_frame_func)( |
| 37 | struct ivi_layout_transition *transition); |
| 38 | typedef void (*ivi_layout_transition_destroy_func)( |
| 39 | struct ivi_layout_transition *transition); |
| 40 | typedef int32_t (*ivi_layout_is_transition_func)(void *private_data, void *id); |
| 41 | |
| 42 | struct ivi_layout_transition { |
| 43 | enum ivi_layout_transition_type type; |
| 44 | void *private_data; |
| 45 | void *user_data; |
| 46 | |
| 47 | uint32_t time_start; |
| 48 | uint32_t time_duration; |
| 49 | uint32_t time_elapsed; |
| 50 | uint32_t is_done; |
| 51 | ivi_layout_is_transition_func is_transition_func; |
| 52 | ivi_layout_transition_frame_func frame_func; |
| 53 | ivi_layout_transition_destroy_func destroy_func; |
| 54 | }; |
| 55 | |
| 56 | struct transition_node { |
| 57 | struct ivi_layout_transition *transition; |
| 58 | struct wl_list link; |
| 59 | }; |
| 60 | |
| 61 | static void layout_transition_destroy(struct ivi_layout_transition *transition); |
| 62 | |
| 63 | static struct ivi_layout_transition * |
| 64 | get_transition_from_type_and_id(enum ivi_layout_transition_type type, |
| 65 | void *id_data) |
| 66 | { |
| 67 | struct ivi_layout *layout = get_instance(); |
| 68 | struct transition_node *node; |
| 69 | struct ivi_layout_transition *tran; |
| 70 | |
| 71 | wl_list_for_each(node, &layout->transitions->transition_list, link) { |
| 72 | tran = node->transition; |
| 73 | |
| 74 | if (tran->type == type && |
| 75 | tran->is_transition_func(tran->private_data, id_data)) |
| 76 | return tran; |
| 77 | } |
| 78 | |
| 79 | return NULL; |
| 80 | } |
| 81 | |
Nobuhiko Tanibata | ee8e583 | 2014-12-15 13:25:39 +0900 | [diff] [blame] | 82 | int32_t |
Nobuhiko Tanibata | 6f9df65 | 2014-11-27 13:22:00 +0900 | [diff] [blame] | 83 | is_surface_transition(struct ivi_layout_surface *surface) |
| 84 | { |
| 85 | struct ivi_layout *layout = get_instance(); |
| 86 | struct transition_node *node; |
| 87 | struct ivi_layout_transition *tran; |
| 88 | |
| 89 | wl_list_for_each(node, &layout->transitions->transition_list, link) { |
| 90 | tran = node->transition; |
| 91 | |
| 92 | if ((tran->type == IVI_LAYOUT_TRANSITION_VIEW_MOVE_RESIZE || |
| 93 | tran->type == IVI_LAYOUT_TRANSITION_VIEW_RESIZE) && |
| 94 | tran->is_transition_func(tran->private_data, surface)) |
| 95 | return 1; |
| 96 | } |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static void |
| 102 | tick_transition(struct ivi_layout_transition *transition, uint32_t timestamp) |
| 103 | { |
| 104 | const double t = timestamp - transition->time_start; |
| 105 | |
| 106 | if (transition->time_duration <= t) { |
| 107 | transition->time_elapsed = transition->time_duration; |
| 108 | transition->is_done = 1; |
| 109 | } else { |
| 110 | transition->time_elapsed = t; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | static float time_to_nowpos(struct ivi_layout_transition *transition) |
| 115 | { |
| 116 | return sin((float)transition->time_elapsed / |
| 117 | (float)transition->time_duration * M_PI_2); |
| 118 | } |
| 119 | |
| 120 | static void |
| 121 | do_transition_frame(struct ivi_layout_transition *transition, |
| 122 | uint32_t timestamp) |
| 123 | { |
| 124 | if (0 == transition->time_start) |
| 125 | transition->time_start = timestamp; |
| 126 | |
| 127 | tick_transition(transition, timestamp); |
| 128 | transition->frame_func(transition); |
| 129 | |
| 130 | if (transition->is_done) |
| 131 | layout_transition_destroy(transition); |
| 132 | } |
| 133 | |
| 134 | static int32_t |
| 135 | layout_transition_frame(void *data) |
| 136 | { |
| 137 | struct ivi_layout_transition_set *transitions = data; |
| 138 | uint32_t fps = 30; |
| 139 | struct timespec timestamp = {}; |
| 140 | uint32_t msec = 0; |
| 141 | struct transition_node *node = NULL; |
| 142 | struct transition_node *next = NULL; |
| 143 | |
| 144 | if (wl_list_empty(&transitions->transition_list)) { |
| 145 | wl_event_source_timer_update(transitions->event_source, 0); |
| 146 | return 1; |
| 147 | } |
| 148 | |
| 149 | wl_event_source_timer_update(transitions->event_source, 1000 / fps); |
| 150 | |
| 151 | clock_gettime(CLOCK_MONOTONIC, ×tamp);/* FIXME */ |
| 152 | msec = (1e+3 * timestamp.tv_sec + 1e-6 * timestamp.tv_nsec); |
| 153 | |
| 154 | wl_list_for_each_safe(node, next, &transitions->transition_list, link) { |
| 155 | do_transition_frame(node->transition, msec); |
| 156 | } |
| 157 | |
| 158 | ivi_layout_commit_changes(); |
| 159 | return 1; |
| 160 | } |
| 161 | |
Nobuhiko Tanibata | ee8e583 | 2014-12-15 13:25:39 +0900 | [diff] [blame] | 162 | struct ivi_layout_transition_set * |
Nobuhiko Tanibata | 6f9df65 | 2014-11-27 13:22:00 +0900 | [diff] [blame] | 163 | ivi_layout_transition_set_create(struct weston_compositor *ec) |
| 164 | { |
| 165 | struct ivi_layout_transition_set *transitions; |
| 166 | struct wl_event_loop *loop; |
| 167 | |
| 168 | transitions = malloc(sizeof(*transitions)); |
| 169 | if (transitions == NULL) { |
| 170 | weston_log("%s: memory allocation fails\n", __func__); |
| 171 | return NULL; |
| 172 | } |
| 173 | |
| 174 | wl_list_init(&transitions->transition_list); |
| 175 | |
| 176 | loop = wl_display_get_event_loop(ec->wl_display); |
| 177 | transitions->event_source = |
| 178 | wl_event_loop_add_timer(loop, layout_transition_frame, |
| 179 | transitions); |
| 180 | |
| 181 | return transitions; |
| 182 | } |
| 183 | |
| 184 | static void |
| 185 | layout_transition_register(struct ivi_layout_transition *trans) |
| 186 | { |
| 187 | struct ivi_layout *layout = get_instance(); |
| 188 | struct transition_node *node; |
| 189 | |
| 190 | node = malloc(sizeof(*node)); |
| 191 | if (node == NULL) { |
| 192 | weston_log("%s: memory allocation fails\n", __func__); |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | node->transition = trans; |
| 197 | wl_list_insert(&layout->pending_transition_list, &node->link); |
| 198 | } |
| 199 | |
| 200 | static void |
| 201 | remove_transition(struct ivi_layout *layout, |
| 202 | struct ivi_layout_transition *trans) |
| 203 | { |
| 204 | struct transition_node *node; |
| 205 | struct transition_node *next; |
| 206 | |
| 207 | wl_list_for_each_safe(node, next, |
| 208 | &layout->transitions->transition_list, link) { |
| 209 | if (node->transition == trans) { |
| 210 | wl_list_remove(&node->link); |
| 211 | free(node); |
| 212 | return; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | wl_list_for_each_safe(node, next, |
| 217 | &layout->pending_transition_list, link) { |
| 218 | if (node->transition == trans) { |
| 219 | wl_list_remove(&node->link); |
| 220 | free(node); |
| 221 | return; |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | static void |
| 227 | layout_transition_destroy(struct ivi_layout_transition *transition) |
| 228 | { |
| 229 | struct ivi_layout *layout = get_instance(); |
| 230 | |
| 231 | remove_transition(layout, transition); |
| 232 | if(transition->destroy_func) |
| 233 | transition->destroy_func(transition); |
| 234 | free(transition); |
| 235 | } |
| 236 | |
| 237 | static struct ivi_layout_transition * |
| 238 | create_layout_transition(void) |
| 239 | { |
| 240 | struct ivi_layout_transition *transition = malloc(sizeof(*transition)); |
| 241 | |
| 242 | if (transition == NULL) { |
| 243 | weston_log("%s: memory allocation fails\n", __func__); |
| 244 | return NULL; |
| 245 | } |
| 246 | |
| 247 | transition->type = IVI_LAYOUT_TRANSITION_MAX; |
| 248 | transition->time_start = 0; |
| 249 | transition->time_duration = 300; /* 300ms */ |
| 250 | transition->time_elapsed = 0; |
| 251 | |
| 252 | transition->is_done = 0; |
| 253 | |
| 254 | transition->private_data = NULL; |
| 255 | transition->user_data = NULL; |
| 256 | |
| 257 | transition->frame_func = NULL; |
| 258 | transition->destroy_func = NULL; |
| 259 | |
| 260 | return transition; |
| 261 | } |
| 262 | |
| 263 | /* move and resize view transition */ |
| 264 | |
| 265 | struct move_resize_view_data { |
| 266 | struct ivi_layout_surface *surface; |
| 267 | int32_t start_x; |
| 268 | int32_t start_y; |
| 269 | int32_t end_x; |
| 270 | int32_t end_y; |
| 271 | int32_t start_width; |
| 272 | int32_t start_height; |
| 273 | int32_t end_width; |
| 274 | int32_t end_height; |
| 275 | }; |
| 276 | |
| 277 | static void |
| 278 | transition_move_resize_view_destroy(struct ivi_layout_transition *transition) |
| 279 | { |
| 280 | struct move_resize_view_data *data = |
| 281 | (struct move_resize_view_data *)transition->private_data; |
| 282 | struct ivi_layout_surface *layout_surface = data->surface; |
| 283 | |
| 284 | wl_signal_emit(&layout_surface->configured, layout_surface); |
| 285 | |
| 286 | if (transition->private_data) { |
| 287 | free(transition->private_data); |
| 288 | transition->private_data = NULL; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | static void |
| 293 | transition_move_resize_view_user_frame(struct ivi_layout_transition *transition) |
| 294 | { |
| 295 | struct move_resize_view_data *mrv = transition->private_data; |
| 296 | const double current = time_to_nowpos(transition); |
| 297 | |
| 298 | const int32_t destx = mrv->start_x + |
| 299 | (mrv->end_x - mrv->start_x) * current; |
| 300 | |
| 301 | const int32_t desty = mrv->start_y + |
| 302 | (mrv->end_y - mrv->start_y) * current; |
| 303 | |
| 304 | const int32_t dest_width = mrv->start_width + |
| 305 | (mrv->end_width - mrv->start_width) * current; |
| 306 | |
| 307 | const int32_t dest_height = mrv->start_height + |
| 308 | (mrv->end_height - mrv->start_height) * current; |
| 309 | |
| 310 | ivi_layout_surface_set_destination_rectangle(mrv->surface, |
| 311 | destx, desty, |
| 312 | dest_width, dest_height); |
| 313 | } |
| 314 | |
| 315 | static int32_t |
| 316 | is_transition_move_resize_view_func(struct move_resize_view_data *data, |
| 317 | struct ivi_layout_surface *view) |
| 318 | { |
| 319 | return data->surface == view; |
| 320 | } |
| 321 | |
| 322 | static struct ivi_layout_transition * |
| 323 | create_move_resize_view_transition( |
| 324 | struct ivi_layout_surface *surface, |
| 325 | int32_t start_x, int32_t start_y, |
| 326 | int32_t end_x, int32_t end_y, |
| 327 | int32_t start_width, int32_t start_height, |
| 328 | int32_t end_width, int32_t end_height, |
| 329 | ivi_layout_transition_frame_func frame_func, |
| 330 | ivi_layout_transition_destroy_func destroy_func, |
| 331 | uint32_t duration) |
| 332 | { |
Carlos Olmedo Escobar | 703f502 | 2015-03-02 13:24:36 +0100 | [diff] [blame] | 333 | struct ivi_layout_transition *transition; |
| 334 | struct move_resize_view_data *data; |
| 335 | |
| 336 | transition = create_layout_transition(); |
Carlos Olmedo Escobar | e82ba53 | 2015-01-17 19:43:02 +0100 | [diff] [blame] | 337 | if (transition == NULL) |
| 338 | return NULL; |
Nobuhiko Tanibata | 6f9df65 | 2014-11-27 13:22:00 +0900 | [diff] [blame] | 339 | |
Carlos Olmedo Escobar | 703f502 | 2015-03-02 13:24:36 +0100 | [diff] [blame] | 340 | data = malloc(sizeof(*data)); |
Nobuhiko Tanibata | 6f9df65 | 2014-11-27 13:22:00 +0900 | [diff] [blame] | 341 | if (data == NULL) { |
| 342 | weston_log("%s: memory allocation fails\n", __func__); |
| 343 | return NULL; |
| 344 | } |
| 345 | |
| 346 | transition->type = IVI_LAYOUT_TRANSITION_VIEW_MOVE_RESIZE; |
| 347 | transition->is_transition_func = (ivi_layout_is_transition_func)is_transition_move_resize_view_func; |
| 348 | |
| 349 | transition->frame_func = frame_func; |
| 350 | transition->destroy_func = destroy_func; |
| 351 | transition->private_data = data; |
| 352 | |
| 353 | if (duration != 0) |
| 354 | transition->time_duration = duration; |
| 355 | |
| 356 | data->surface = surface; |
| 357 | data->start_x = start_x; |
| 358 | data->start_y = start_y; |
| 359 | data->end_x = end_x; |
| 360 | data->end_y = end_y; |
| 361 | |
| 362 | data->start_width = start_width; |
| 363 | data->start_height = start_height; |
| 364 | data->end_width = end_width; |
| 365 | data->end_height = end_height; |
| 366 | |
| 367 | return transition; |
| 368 | } |
| 369 | |
Nobuhiko Tanibata | ee8e583 | 2014-12-15 13:25:39 +0900 | [diff] [blame] | 370 | void |
Nobuhiko Tanibata | 6f9df65 | 2014-11-27 13:22:00 +0900 | [diff] [blame] | 371 | ivi_layout_transition_move_resize_view(struct ivi_layout_surface *surface, |
| 372 | int32_t dest_x, int32_t dest_y, |
| 373 | int32_t dest_width, int32_t dest_height, |
| 374 | uint32_t duration) |
| 375 | { |
| 376 | struct ivi_layout_transition *transition; |
| 377 | int32_t start_pos[2] = { |
| 378 | surface->pending.prop.start_x, |
| 379 | surface->pending.prop.start_y |
| 380 | }; |
| 381 | |
| 382 | int32_t start_size[2] = { |
| 383 | surface->pending.prop.start_width, |
| 384 | surface->pending.prop.start_height |
| 385 | }; |
| 386 | |
| 387 | transition = get_transition_from_type_and_id( |
| 388 | IVI_LAYOUT_TRANSITION_VIEW_MOVE_RESIZE, |
| 389 | surface); |
| 390 | if (transition) { |
| 391 | struct move_resize_view_data *data = transition->private_data; |
| 392 | transition->time_start = 0; |
| 393 | transition->time_duration = duration; |
| 394 | |
| 395 | data->start_x = start_pos[0]; |
| 396 | data->start_y = start_pos[1]; |
| 397 | data->end_x = dest_x; |
| 398 | data->end_y = dest_y; |
| 399 | |
| 400 | data->start_width = start_size[0]; |
| 401 | data->start_height = start_size[1]; |
| 402 | data->end_width = dest_width; |
| 403 | data->end_height = dest_height; |
| 404 | return; |
| 405 | } |
| 406 | |
| 407 | transition = create_move_resize_view_transition( |
| 408 | surface, |
| 409 | start_pos[0], start_pos[1], |
| 410 | dest_x, dest_y, |
| 411 | start_size[0], start_size[1], |
| 412 | dest_width, dest_height, |
| 413 | transition_move_resize_view_user_frame, |
| 414 | transition_move_resize_view_destroy, |
| 415 | duration); |
| 416 | |
| 417 | layout_transition_register(transition); |
| 418 | } |
| 419 | |
| 420 | /* fade transition */ |
| 421 | struct fade_view_data { |
| 422 | struct ivi_layout_surface *surface; |
| 423 | double start_alpha; |
| 424 | double end_alpha; |
| 425 | }; |
| 426 | |
| 427 | struct store_alpha{ |
| 428 | double alpha; |
| 429 | }; |
| 430 | |
| 431 | static void |
| 432 | fade_view_user_frame(struct ivi_layout_transition *transition) |
| 433 | { |
| 434 | struct fade_view_data *fade = transition->private_data; |
| 435 | struct ivi_layout_surface *surface = fade->surface; |
| 436 | |
| 437 | const double current = time_to_nowpos(transition); |
| 438 | const double alpha = fade->start_alpha + |
| 439 | (fade->end_alpha - fade->start_alpha) * current; |
| 440 | |
| 441 | ivi_layout_surface_set_opacity(surface, wl_fixed_from_double(alpha)); |
| 442 | ivi_layout_surface_set_visibility(surface, true); |
| 443 | } |
| 444 | |
| 445 | static int32_t |
| 446 | is_transition_fade_view_func(struct fade_view_data *data, |
| 447 | struct ivi_layout_surface *view) |
| 448 | { |
| 449 | return data->surface == view; |
| 450 | } |
| 451 | |
| 452 | static struct ivi_layout_transition * |
| 453 | create_fade_view_transition( |
| 454 | struct ivi_layout_surface *surface, |
| 455 | double start_alpha, double end_alpha, |
| 456 | ivi_layout_transition_frame_func frame_func, |
| 457 | void *user_data, |
| 458 | ivi_layout_transition_destroy_func destroy_func, |
| 459 | uint32_t duration) |
| 460 | { |
Carlos Olmedo Escobar | 703f502 | 2015-03-02 13:24:36 +0100 | [diff] [blame] | 461 | struct ivi_layout_transition *transition; |
| 462 | struct fade_view_data *data; |
| 463 | |
| 464 | transition = create_layout_transition(); |
Carlos Olmedo Escobar | e82ba53 | 2015-01-17 19:43:02 +0100 | [diff] [blame] | 465 | if (transition == NULL) |
| 466 | return NULL; |
Nobuhiko Tanibata | 6f9df65 | 2014-11-27 13:22:00 +0900 | [diff] [blame] | 467 | |
Carlos Olmedo Escobar | 703f502 | 2015-03-02 13:24:36 +0100 | [diff] [blame] | 468 | data = malloc(sizeof(*data)); |
Nobuhiko Tanibata | 6f9df65 | 2014-11-27 13:22:00 +0900 | [diff] [blame] | 469 | if (data == NULL) { |
| 470 | weston_log("%s: memory allocation fails\n", __func__); |
| 471 | return NULL; |
| 472 | } |
| 473 | |
| 474 | transition->type = IVI_LAYOUT_TRANSITION_VIEW_FADE; |
| 475 | transition->is_transition_func = (ivi_layout_is_transition_func)is_transition_fade_view_func; |
| 476 | |
| 477 | transition->user_data = user_data; |
| 478 | transition->private_data = data; |
| 479 | transition->frame_func = frame_func; |
| 480 | transition->destroy_func = destroy_func; |
| 481 | |
| 482 | if (duration != 0) |
| 483 | transition->time_duration = duration; |
| 484 | |
| 485 | data->surface = surface; |
| 486 | data->start_alpha = start_alpha; |
| 487 | data->end_alpha = end_alpha; |
| 488 | |
| 489 | return transition; |
| 490 | } |
| 491 | |
| 492 | static void |
| 493 | create_visibility_transition(struct ivi_layout_surface *surface, |
| 494 | double start_alpha, |
| 495 | double dest_alpha, |
| 496 | void *user_data, |
| 497 | ivi_layout_transition_destroy_func destroy_func, |
| 498 | uint32_t duration) |
| 499 | { |
| 500 | struct ivi_layout_transition *transition = NULL; |
| 501 | |
| 502 | transition = create_fade_view_transition( |
| 503 | surface, |
| 504 | start_alpha, dest_alpha, |
| 505 | fade_view_user_frame, |
| 506 | user_data, |
| 507 | destroy_func, |
| 508 | duration); |
| 509 | |
| 510 | layout_transition_register(transition); |
| 511 | } |
| 512 | |
| 513 | static void |
| 514 | visibility_on_transition_destroy(struct ivi_layout_transition *transition) |
| 515 | { |
| 516 | struct fade_view_data *data = transition->private_data; |
| 517 | struct store_alpha *user_data = transition->user_data; |
| 518 | |
| 519 | ivi_layout_surface_set_visibility(data->surface, true); |
| 520 | |
| 521 | free(data); |
| 522 | transition->private_data = NULL; |
| 523 | |
| 524 | free(user_data); |
| 525 | transition->user_data = NULL; |
| 526 | } |
| 527 | |
Nobuhiko Tanibata | ee8e583 | 2014-12-15 13:25:39 +0900 | [diff] [blame] | 528 | void |
Nobuhiko Tanibata | 6f9df65 | 2014-11-27 13:22:00 +0900 | [diff] [blame] | 529 | ivi_layout_transition_visibility_on(struct ivi_layout_surface *surface, |
| 530 | uint32_t duration) |
| 531 | { |
| 532 | struct ivi_layout_transition *transition; |
| 533 | bool is_visible = ivi_layout_surface_get_visibility(surface); |
| 534 | wl_fixed_t dest_alpha = ivi_layout_surface_get_opacity(surface); |
| 535 | struct store_alpha *user_data = NULL; |
| 536 | wl_fixed_t start_alpha = 0.0; |
| 537 | struct fade_view_data *data = NULL; |
| 538 | |
| 539 | transition = get_transition_from_type_and_id( |
| 540 | IVI_LAYOUT_TRANSITION_VIEW_FADE, |
| 541 | surface); |
| 542 | if (transition) { |
| 543 | start_alpha = ivi_layout_surface_get_opacity(surface); |
| 544 | user_data = transition->user_data; |
| 545 | data = transition->private_data; |
| 546 | |
| 547 | transition->time_start = 0; |
| 548 | transition->time_duration = duration; |
| 549 | transition->destroy_func = visibility_on_transition_destroy; |
| 550 | |
| 551 | data->start_alpha = wl_fixed_to_double(start_alpha); |
| 552 | data->end_alpha = user_data->alpha; |
| 553 | return; |
| 554 | } |
| 555 | |
| 556 | if (is_visible) |
| 557 | return; |
| 558 | |
| 559 | user_data = malloc(sizeof(*user_data)); |
| 560 | if (user_data == NULL) { |
| 561 | weston_log("%s: memory allocation fails\n", __func__); |
| 562 | return; |
| 563 | } |
| 564 | |
| 565 | user_data->alpha = wl_fixed_to_double(dest_alpha); |
| 566 | |
| 567 | create_visibility_transition(surface, |
| 568 | 0.0, // start_alpha |
| 569 | wl_fixed_to_double(dest_alpha), |
| 570 | user_data, |
| 571 | visibility_on_transition_destroy, |
| 572 | duration); |
| 573 | } |
| 574 | |
| 575 | static void |
| 576 | visibility_off_transition_destroy(struct ivi_layout_transition *transition) |
| 577 | { |
| 578 | struct fade_view_data *data = transition->private_data; |
| 579 | struct store_alpha *user_data = transition->user_data; |
| 580 | |
| 581 | ivi_layout_surface_set_visibility(data->surface, false); |
| 582 | |
| 583 | ivi_layout_surface_set_opacity(data->surface, |
| 584 | wl_fixed_from_double(user_data->alpha)); |
| 585 | |
| 586 | free(data); |
| 587 | transition->private_data = NULL; |
| 588 | |
| 589 | free(user_data); |
| 590 | transition->user_data= NULL; |
| 591 | } |
| 592 | |
Nobuhiko Tanibata | ee8e583 | 2014-12-15 13:25:39 +0900 | [diff] [blame] | 593 | void |
Nobuhiko Tanibata | 6f9df65 | 2014-11-27 13:22:00 +0900 | [diff] [blame] | 594 | ivi_layout_transition_visibility_off(struct ivi_layout_surface *surface, |
| 595 | uint32_t duration) |
| 596 | { |
| 597 | struct ivi_layout_transition *transition; |
| 598 | wl_fixed_t start_alpha = ivi_layout_surface_get_opacity(surface); |
| 599 | struct store_alpha* user_data = NULL; |
| 600 | struct fade_view_data* data = NULL; |
| 601 | |
| 602 | transition = |
| 603 | get_transition_from_type_and_id(IVI_LAYOUT_TRANSITION_VIEW_FADE, |
| 604 | surface); |
| 605 | if (transition) { |
| 606 | data = transition->private_data; |
| 607 | |
| 608 | transition->time_start = 0; |
| 609 | transition->time_duration = duration; |
| 610 | transition->destroy_func = visibility_off_transition_destroy; |
| 611 | |
| 612 | data->start_alpha = wl_fixed_to_double(start_alpha); |
| 613 | data->end_alpha = 0; |
| 614 | return; |
| 615 | } |
| 616 | |
| 617 | user_data = malloc(sizeof(*user_data)); |
| 618 | if (user_data == NULL) { |
| 619 | weston_log("%s: memory allocation fails\n", __func__); |
| 620 | return; |
| 621 | } |
| 622 | |
| 623 | user_data->alpha = wl_fixed_to_double(start_alpha); |
| 624 | |
| 625 | create_visibility_transition(surface, |
| 626 | wl_fixed_to_double(start_alpha), |
| 627 | 0.0, // dest_alpha |
| 628 | user_data, |
| 629 | visibility_off_transition_destroy, |
| 630 | duration); |
| 631 | } |
| 632 | |
| 633 | /* move layer transition */ |
| 634 | |
| 635 | struct move_layer_data { |
| 636 | struct ivi_layout_layer *layer; |
| 637 | int32_t start_x; |
| 638 | int32_t start_y; |
| 639 | int32_t end_x; |
| 640 | int32_t end_y; |
| 641 | ivi_layout_transition_destroy_user_func destroy_func; |
| 642 | }; |
| 643 | |
| 644 | static void |
| 645 | transition_move_layer_user_frame(struct ivi_layout_transition *transition) |
| 646 | { |
| 647 | struct move_layer_data *data = transition->private_data; |
| 648 | struct ivi_layout_layer *layer = data->layer; |
| 649 | |
| 650 | const float current = time_to_nowpos(transition); |
| 651 | |
| 652 | const int32_t dest_x = data->start_x + |
| 653 | (data->end_x - data->start_x) * current; |
| 654 | |
| 655 | const int32_t dest_y = data->start_y + |
| 656 | (data->end_y - data->start_y) * current; |
| 657 | |
| 658 | ivi_layout_layer_set_position(layer, dest_x, dest_y); |
| 659 | } |
| 660 | |
| 661 | static void |
| 662 | transition_move_layer_destroy(struct ivi_layout_transition *transition) |
| 663 | { |
| 664 | struct move_layer_data *data = transition->private_data; |
| 665 | |
| 666 | if(data->destroy_func) |
| 667 | data->destroy_func(transition->user_data); |
| 668 | |
| 669 | free(data); |
| 670 | transition->private_data = NULL; |
| 671 | } |
| 672 | |
| 673 | static int32_t |
| 674 | is_transition_move_layer_func(struct move_layer_data *data, |
| 675 | struct ivi_layout_layer *layer) |
| 676 | { |
| 677 | return data->layer == layer; |
| 678 | } |
| 679 | |
| 680 | |
| 681 | static struct ivi_layout_transition * |
| 682 | create_move_layer_transition( |
| 683 | struct ivi_layout_layer *layer, |
| 684 | int32_t start_x, int32_t start_y, |
| 685 | int32_t end_x, int32_t end_y, |
| 686 | void *user_data, |
| 687 | ivi_layout_transition_destroy_user_func destroy_user_func, |
| 688 | uint32_t duration) |
| 689 | { |
Carlos Olmedo Escobar | 703f502 | 2015-03-02 13:24:36 +0100 | [diff] [blame] | 690 | struct ivi_layout_transition *transition; |
| 691 | struct move_layer_data *data; |
| 692 | |
| 693 | transition = create_layout_transition(); |
Carlos Olmedo Escobar | e82ba53 | 2015-01-17 19:43:02 +0100 | [diff] [blame] | 694 | if (transition == NULL) |
| 695 | return NULL; |
Nobuhiko Tanibata | 6f9df65 | 2014-11-27 13:22:00 +0900 | [diff] [blame] | 696 | |
Carlos Olmedo Escobar | 703f502 | 2015-03-02 13:24:36 +0100 | [diff] [blame] | 697 | data = malloc(sizeof(*data)); |
Nobuhiko Tanibata | 6f9df65 | 2014-11-27 13:22:00 +0900 | [diff] [blame] | 698 | if (data == NULL) { |
| 699 | weston_log("%s: memory allocation fails\n", __func__); |
| 700 | return NULL; |
| 701 | } |
| 702 | |
| 703 | transition->type = IVI_LAYOUT_TRANSITION_LAYER_MOVE; |
| 704 | transition->is_transition_func = (ivi_layout_is_transition_func)is_transition_move_layer_func; |
| 705 | |
| 706 | transition->frame_func = transition_move_layer_user_frame; |
| 707 | transition->destroy_func = transition_move_layer_destroy; |
| 708 | transition->private_data = data; |
| 709 | transition->user_data = user_data; |
| 710 | |
| 711 | if (duration != 0) |
| 712 | transition->time_duration = duration; |
| 713 | |
| 714 | data->layer = layer; |
| 715 | data->start_x = start_x; |
| 716 | data->start_y = start_y; |
| 717 | data->end_x = end_x; |
| 718 | data->end_y = end_y; |
| 719 | data->destroy_func = destroy_user_func; |
| 720 | |
| 721 | return transition; |
| 722 | } |
| 723 | |
Nobuhiko Tanibata | ee8e583 | 2014-12-15 13:25:39 +0900 | [diff] [blame] | 724 | void |
Nobuhiko Tanibata | 6f9df65 | 2014-11-27 13:22:00 +0900 | [diff] [blame] | 725 | ivi_layout_transition_move_layer(struct ivi_layout_layer *layer, |
| 726 | int32_t dest_x, int32_t dest_y, |
| 727 | uint32_t duration) |
| 728 | { |
| 729 | int32_t start_pos_x = 0; |
| 730 | int32_t start_pos_y = 0; |
| 731 | struct ivi_layout_transition *transition = NULL; |
| 732 | |
| 733 | ivi_layout_layer_get_position(layer, &start_pos_x, &start_pos_y); |
| 734 | |
| 735 | transition = create_move_layer_transition( |
| 736 | layer, |
| 737 | start_pos_x, start_pos_y, |
| 738 | dest_x, dest_y, |
| 739 | NULL, NULL, |
| 740 | duration); |
| 741 | |
| 742 | layout_transition_register(transition); |
| 743 | |
| 744 | return; |
| 745 | } |
| 746 | |
Nobuhiko Tanibata | ee8e583 | 2014-12-15 13:25:39 +0900 | [diff] [blame] | 747 | void |
Nobuhiko Tanibata | 6f9df65 | 2014-11-27 13:22:00 +0900 | [diff] [blame] | 748 | ivi_layout_transition_move_layer_cancel(struct ivi_layout_layer *layer) |
| 749 | { |
| 750 | struct ivi_layout_transition *transition = |
| 751 | get_transition_from_type_and_id( |
| 752 | IVI_LAYOUT_TRANSITION_LAYER_MOVE, |
| 753 | layer); |
| 754 | if (transition) { |
| 755 | layout_transition_destroy(transition); |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | /* fade layer transition */ |
| 760 | struct fade_layer_data { |
| 761 | struct ivi_layout_layer *layer; |
| 762 | uint32_t is_fade_in; |
| 763 | double start_alpha; |
| 764 | double end_alpha; |
| 765 | ivi_layout_transition_destroy_user_func destroy_func; |
| 766 | }; |
| 767 | |
| 768 | static void |
| 769 | transition_fade_layer_destroy(struct ivi_layout_transition *transition) |
| 770 | { |
| 771 | struct fade_layer_data *data = transition->private_data; |
| 772 | transition->private_data = NULL; |
| 773 | |
| 774 | free(data); |
| 775 | } |
| 776 | |
| 777 | static void |
| 778 | transition_fade_layer_user_frame(struct ivi_layout_transition *transition) |
| 779 | { |
| 780 | double current = time_to_nowpos(transition); |
| 781 | struct fade_layer_data *data = transition->private_data; |
| 782 | double alpha = data->start_alpha + |
| 783 | (data->end_alpha - data->start_alpha) * current; |
| 784 | wl_fixed_t fixed_alpha = wl_fixed_from_double(alpha); |
| 785 | |
| 786 | int32_t is_done = transition->is_done; |
| 787 | bool is_visible = !is_done || data->is_fade_in; |
| 788 | |
| 789 | ivi_layout_layer_set_opacity(data->layer, fixed_alpha); |
| 790 | ivi_layout_layer_set_visibility(data->layer, is_visible); |
| 791 | } |
| 792 | |
| 793 | static int32_t |
| 794 | is_transition_fade_layer_func(struct fade_layer_data *data, |
| 795 | struct ivi_layout_layer *layer) |
| 796 | { |
| 797 | return data->layer == layer; |
| 798 | } |
| 799 | |
Nobuhiko Tanibata | ee8e583 | 2014-12-15 13:25:39 +0900 | [diff] [blame] | 800 | void |
Nobuhiko Tanibata | 6f9df65 | 2014-11-27 13:22:00 +0900 | [diff] [blame] | 801 | ivi_layout_transition_fade_layer( |
| 802 | struct ivi_layout_layer *layer, |
| 803 | uint32_t is_fade_in, |
| 804 | double start_alpha, double end_alpha, |
| 805 | void* user_data, |
| 806 | ivi_layout_transition_destroy_user_func destroy_func, |
| 807 | uint32_t duration) |
| 808 | { |
| 809 | struct ivi_layout_transition *transition; |
| 810 | struct fade_layer_data *data = NULL; |
| 811 | wl_fixed_t fixed_opacity = 0.0; |
| 812 | double now_opacity = 0.0; |
| 813 | double remain = 0.0; |
| 814 | |
| 815 | transition = get_transition_from_type_and_id( |
| 816 | IVI_LAYOUT_TRANSITION_LAYER_FADE, |
| 817 | layer); |
| 818 | if (transition) { |
| 819 | /* transition update */ |
| 820 | data = transition->private_data; |
| 821 | |
| 822 | /* FIXME */ |
| 823 | fixed_opacity = ivi_layout_layer_get_opacity(layer); |
| 824 | now_opacity = wl_fixed_to_double(fixed_opacity); |
| 825 | remain = 0.0; |
| 826 | |
| 827 | data->is_fade_in = is_fade_in; |
| 828 | data->start_alpha = now_opacity; |
| 829 | data->end_alpha = end_alpha; |
| 830 | |
| 831 | remain = is_fade_in? 1.0 - now_opacity : now_opacity; |
| 832 | transition->time_start = 0; |
| 833 | transition->time_elapsed = 0; |
| 834 | transition->time_duration = duration * remain; |
| 835 | |
| 836 | return; |
| 837 | } |
| 838 | |
| 839 | transition = create_layout_transition(); |
Carlos Olmedo Escobar | e82ba53 | 2015-01-17 19:43:02 +0100 | [diff] [blame] | 840 | if (transition == NULL) |
| 841 | return; |
Nobuhiko Tanibata | 6f9df65 | 2014-11-27 13:22:00 +0900 | [diff] [blame] | 842 | |
Carlos Olmedo Escobar | e82ba53 | 2015-01-17 19:43:02 +0100 | [diff] [blame] | 843 | data = malloc(sizeof(*data)); |
Nobuhiko Tanibata | 6f9df65 | 2014-11-27 13:22:00 +0900 | [diff] [blame] | 844 | if (data == NULL) { |
| 845 | weston_log("%s: memory allocation fails\n", __func__); |
| 846 | return; |
| 847 | } |
| 848 | |
| 849 | transition->type = IVI_LAYOUT_TRANSITION_LAYER_FADE; |
| 850 | transition->is_transition_func = (ivi_layout_is_transition_func)is_transition_fade_layer_func; |
| 851 | |
| 852 | transition->private_data = data; |
| 853 | transition->user_data = user_data; |
| 854 | |
| 855 | transition->frame_func = transition_fade_layer_user_frame; |
| 856 | transition->destroy_func = transition_fade_layer_destroy; |
| 857 | |
| 858 | if (duration != 0) |
| 859 | transition->time_duration = duration; |
| 860 | |
| 861 | data->layer = layer; |
| 862 | data->is_fade_in = is_fade_in; |
| 863 | data->start_alpha = start_alpha; |
| 864 | data->end_alpha = end_alpha; |
| 865 | data->destroy_func = destroy_func; |
| 866 | |
| 867 | layout_transition_register(transition); |
| 868 | |
| 869 | return; |
| 870 | } |
| 871 | |