summaryrefslogtreecommitdiff
path: root/ivi-shell/ivi-layout-transition.c
blob: 904e8d615a2230b4c5f8c32d5510385eb8eb6ade (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
/*
 * Copyright (C) 2014 DENSO CORPORATION
 *
 * Permission to use, copy, modify, distribute, and sell this software and
 * its documentation for any purpose is hereby granted without fee, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of the copyright holders not be used in
 * advertising or publicity pertaining to distribution of the software
 * without specific, written prior permission.  The copyright holders make
 * no representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <time.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>

#include "ivi-layout-export.h"
#include "ivi-layout-private.h"

struct ivi_layout_transition;

typedef void (*ivi_layout_transition_frame_func)(
			struct ivi_layout_transition *transition);
typedef void (*ivi_layout_transition_destroy_func)(
			struct ivi_layout_transition *transition);
typedef int32_t (*ivi_layout_is_transition_func)(void *private_data, void *id);

struct ivi_layout_transition {
	enum ivi_layout_transition_type type;
	void *private_data;
	void *user_data;

	uint32_t time_start;
	uint32_t time_duration;
	uint32_t time_elapsed;
	uint32_t  is_done;
	ivi_layout_is_transition_func is_transition_func;
	ivi_layout_transition_frame_func frame_func;
	ivi_layout_transition_destroy_func destroy_func;
};

struct transition_node {
	struct ivi_layout_transition *transition;
	struct wl_list link;
};

static void layout_transition_destroy(struct ivi_layout_transition *transition);

static struct ivi_layout_transition *
get_transition_from_type_and_id(enum ivi_layout_transition_type type,
				void *id_data)
{
	struct ivi_layout *layout = get_instance();
	struct transition_node *node;
	struct ivi_layout_transition *tran;

	wl_list_for_each(node, &layout->transitions->transition_list, link) {
		tran = node->transition;

		if (tran->type == type &&
		    tran->is_transition_func(tran->private_data, id_data))
			return tran;
	}

	return NULL;
}

int32_t
is_surface_transition(struct ivi_layout_surface *surface)
{
	struct ivi_layout *layout = get_instance();
	struct transition_node *node;
	struct ivi_layout_transition *tran;

	wl_list_for_each(node, &layout->transitions->transition_list, link) {
		tran = node->transition;

		if ((tran->type == IVI_LAYOUT_TRANSITION_VIEW_MOVE_RESIZE ||
		     tran->type == IVI_LAYOUT_TRANSITION_VIEW_RESIZE) &&
		    tran->is_transition_func(tran->private_data, surface))
			return 1;
	}

	return 0;
}

static void
tick_transition(struct ivi_layout_transition *transition, uint32_t timestamp)
{
	const double t = timestamp - transition->time_start;

	if (transition->time_duration <= t) {
		transition->time_elapsed = transition->time_duration;
		transition->is_done = 1;
	} else {
		transition->time_elapsed = t;
	}
}

static float time_to_nowpos(struct ivi_layout_transition *transition)
{
	return sin((float)transition->time_elapsed /
		   (float)transition->time_duration * M_PI_2);
}

static void
do_transition_frame(struct ivi_layout_transition *transition,
		    uint32_t timestamp)
{
	if (0 == transition->time_start)
		transition->time_start = timestamp;

	tick_transition(transition, timestamp);
	transition->frame_func(transition);

	if (transition->is_done)
		layout_transition_destroy(transition);
}

static int32_t
layout_transition_frame(void *data)
{
	struct ivi_layout_transition_set *transitions = data;
	uint32_t fps = 30;
	struct timespec timestamp = {};
	uint32_t msec = 0;
	struct transition_node *node = NULL;
	struct transition_node *next = NULL;

	if (wl_list_empty(&transitions->transition_list)) {
		wl_event_source_timer_update(transitions->event_source, 0);
		return 1;
	}

	wl_event_source_timer_update(transitions->event_source, 1000 / fps);

	clock_gettime(CLOCK_MONOTONIC, &timestamp);/* FIXME */
	msec = (1e+3 * timestamp.tv_sec + 1e-6 * timestamp.tv_nsec);

	wl_list_for_each_safe(node, next, &transitions->transition_list, link) {
		do_transition_frame(node->transition, msec);
	}

	ivi_layout_commit_changes();
	return 1;
}

struct ivi_layout_transition_set *
ivi_layout_transition_set_create(struct weston_compositor *ec)
{
	struct ivi_layout_transition_set *transitions;
	struct wl_event_loop *loop;

	transitions = malloc(sizeof(*transitions));
	if (transitions == NULL) {
		weston_log("%s: memory allocation fails\n", __func__);
		return NULL;
	}

	wl_list_init(&transitions->transition_list);

	loop = wl_display_get_event_loop(ec->wl_display);
	transitions->event_source =
		wl_event_loop_add_timer(loop, layout_transition_frame,
					transitions);

	return transitions;
}

static void
layout_transition_register(struct ivi_layout_transition *trans)
{
	struct ivi_layout *layout = get_instance();
	struct transition_node *node;

	node = malloc(sizeof(*node));
	if (node == NULL) {
		weston_log("%s: memory allocation fails\n", __func__);
		return;
	}

	node->transition = trans;
	wl_list_insert(&layout->pending_transition_list, &node->link);
}

static void
remove_transition(struct ivi_layout *layout,
		  struct ivi_layout_transition *trans)
{
	struct transition_node *node;
	struct transition_node *next;

	wl_list_for_each_safe(node, next,
			      &layout->transitions->transition_list, link) {
		if (node->transition == trans) {
			wl_list_remove(&node->link);
			free(node);
			return;
		}
	}

	wl_list_for_each_safe(node, next,
			      &layout->pending_transition_list, link) {
		if (node->transition == trans) {
			wl_list_remove(&node->link);
			free(node);
			return;
		}
	}
}

static void
layout_transition_destroy(struct ivi_layout_transition *transition)
{
	struct ivi_layout *layout = get_instance();

	remove_transition(layout, transition);
	if(transition->destroy_func)
		transition->destroy_func(transition);
	free(transition);
}

static struct ivi_layout_transition *
create_layout_transition(void)
{
	struct ivi_layout_transition *transition = malloc(sizeof(*transition));

	if (transition == NULL) {
		weston_log("%s: memory allocation fails\n", __func__);
		return NULL;
	}

	transition->type = IVI_LAYOUT_TRANSITION_MAX;
	transition->time_start = 0;
	transition->time_duration = 300; /* 300ms */
	transition->time_elapsed = 0;

	transition->is_done = 0;

	transition->private_data = NULL;
	transition->user_data = NULL;

	transition->frame_func = NULL;
	transition->destroy_func = NULL;

	return transition;
}

/* move and resize view transition */

struct move_resize_view_data {
	struct ivi_layout_surface *surface;
	int32_t start_x;
	int32_t start_y;
	int32_t end_x;
	int32_t end_y;
	int32_t start_width;
	int32_t start_height;
	int32_t end_width;
	int32_t end_height;
};

static void
transition_move_resize_view_destroy(struct ivi_layout_transition *transition)
{
	struct move_resize_view_data *data =
		(struct move_resize_view_data *)transition->private_data;
	struct ivi_layout_surface *layout_surface = data->surface;

	wl_signal_emit(&layout_surface->configured, layout_surface);

	if (transition->private_data) {
		free(transition->private_data);
		transition->private_data = NULL;
	}
}

static void
transition_move_resize_view_user_frame(struct ivi_layout_transition *transition)
{
	struct move_resize_view_data *mrv = transition->private_data;
	const double current = time_to_nowpos(transition);

	const int32_t destx = mrv->start_x +
		(mrv->end_x - mrv->start_x) * current;

	const int32_t desty = mrv->start_y +
		(mrv->end_y - mrv->start_y) * current;

	const int32_t dest_width = mrv->start_width  +
		(mrv->end_width - mrv->start_width) * current;

	const int32_t dest_height = mrv->start_height +
		(mrv->end_height - mrv->start_height) * current;

	ivi_layout_surface_set_destination_rectangle(mrv->surface,
						     destx, desty,
						     dest_width, dest_height);
}

static int32_t
is_transition_move_resize_view_func(struct move_resize_view_data *data,
				    struct ivi_layout_surface *view)
{
	return data->surface == view;
}

static struct ivi_layout_transition *
create_move_resize_view_transition(
			struct ivi_layout_surface *surface,
			int32_t start_x, int32_t start_y,
			int32_t end_x, int32_t end_y,
			int32_t start_width, int32_t start_height,
			int32_t end_width, int32_t end_height,
			ivi_layout_transition_frame_func frame_func,
			ivi_layout_transition_destroy_func destroy_func,
			uint32_t duration)
{
	struct ivi_layout_transition *transition = create_layout_transition();
	if (transition == NULL)
		return NULL;

	struct move_resize_view_data *data = malloc(sizeof(*data));
	if (data == NULL) {
		weston_log("%s: memory allocation fails\n", __func__);
		return NULL;
	}

	transition->type = IVI_LAYOUT_TRANSITION_VIEW_MOVE_RESIZE;
	transition->is_transition_func = (ivi_layout_is_transition_func)is_transition_move_resize_view_func;

	transition->frame_func = frame_func;
	transition->destroy_func = destroy_func;
	transition->private_data = data;

	if (duration != 0)
		transition->time_duration = duration;

	data->surface = surface;
	data->start_x = start_x;
	data->start_y = start_y;
	data->end_x   = end_x;
	data->end_y   = end_y;

	data->start_width  = start_width;
	data->start_height = start_height;
	data->end_width    = end_width;
	data->end_height   = end_height;

	return transition;
}

void
ivi_layout_transition_move_resize_view(struct ivi_layout_surface *surface,
				       int32_t dest_x, int32_t dest_y,
				       int32_t dest_width, int32_t dest_height,
				       uint32_t duration)
{
	struct ivi_layout_transition *transition;
	int32_t start_pos[2] = {
		surface->pending.prop.start_x,
		surface->pending.prop.start_y
	};

	int32_t start_size[2] = {
		surface->pending.prop.start_width,
		surface->pending.prop.start_height
	};

	transition = get_transition_from_type_and_id(
					IVI_LAYOUT_TRANSITION_VIEW_MOVE_RESIZE,
					surface);
	if (transition) {
		struct move_resize_view_data *data = transition->private_data;
		transition->time_start = 0;
		transition->time_duration = duration;

		data->start_x = start_pos[0];
		data->start_y = start_pos[1];
		data->end_x   = dest_x;
		data->end_y   = dest_y;

		data->start_width  = start_size[0];
		data->start_height = start_size[1];
		data->end_width    = dest_width;
		data->end_height   = dest_height;
		return;
	}

	transition = create_move_resize_view_transition(
		surface,
		start_pos[0], start_pos[1],
		dest_x, dest_y,
		start_size[0], start_size[1],
		dest_width, dest_height,
		transition_move_resize_view_user_frame,
		transition_move_resize_view_destroy,
		duration);

	layout_transition_register(transition);
}

/* fade transition */
struct fade_view_data {
	struct ivi_layout_surface *surface;
	double start_alpha;
	double end_alpha;
};

struct store_alpha{
	double alpha;
};

static void
fade_view_user_frame(struct ivi_layout_transition *transition)
{
	struct fade_view_data *fade = transition->private_data;
	struct ivi_layout_surface *surface = fade->surface;

	const double current = time_to_nowpos(transition);
	const double alpha = fade->start_alpha +
		(fade->end_alpha - fade->start_alpha) * current;

	ivi_layout_surface_set_opacity(surface, wl_fixed_from_double(alpha));
	ivi_layout_surface_set_visibility(surface, true);
}

static int32_t
is_transition_fade_view_func(struct fade_view_data *data,
			     struct ivi_layout_surface *view)
{
	return data->surface == view;
}

static struct ivi_layout_transition *
create_fade_view_transition(
			struct ivi_layout_surface *surface,
			double start_alpha, double end_alpha,
			ivi_layout_transition_frame_func frame_func,
			void *user_data,
			ivi_layout_transition_destroy_func destroy_func,
			uint32_t duration)
{
	struct ivi_layout_transition *transition = create_layout_transition();
	if (transition == NULL)
		return NULL;

	struct fade_view_data *data = malloc(sizeof(*data));
	if (data == NULL) {
		weston_log("%s: memory allocation fails\n", __func__);
		return NULL;
	}

	transition->type = IVI_LAYOUT_TRANSITION_VIEW_FADE;
	transition->is_transition_func = (ivi_layout_is_transition_func)is_transition_fade_view_func;

	transition->user_data = user_data;
	transition->private_data = data;
	transition->frame_func = frame_func;
	transition->destroy_func = destroy_func;

	if (duration != 0)
		transition->time_duration = duration;

	data->surface = surface;
	data->start_alpha = start_alpha;
	data->end_alpha   = end_alpha;

	return transition;
}

static void
create_visibility_transition(struct ivi_layout_surface *surface,
			     double start_alpha,
			     double dest_alpha,
			     void *user_data,
			     ivi_layout_transition_destroy_func destroy_func,
			     uint32_t duration)
{
	struct ivi_layout_transition *transition = NULL;

	transition = create_fade_view_transition(
		surface,
		start_alpha, dest_alpha,
		fade_view_user_frame,
		user_data,
		destroy_func,
		duration);

	layout_transition_register(transition);
}

static void
visibility_on_transition_destroy(struct ivi_layout_transition *transition)
{
	struct fade_view_data *data = transition->private_data;
	struct store_alpha *user_data = transition->user_data;

	ivi_layout_surface_set_visibility(data->surface, true);

	free(data);
	transition->private_data = NULL;

	free(user_data);
	transition->user_data = NULL;
}

void
ivi_layout_transition_visibility_on(struct ivi_layout_surface *surface,
				    uint32_t duration)
{
	struct ivi_layout_transition *transition;
	bool is_visible = ivi_layout_surface_get_visibility(surface);
	wl_fixed_t dest_alpha = ivi_layout_surface_get_opacity(surface);
	struct store_alpha *user_data = NULL;
	wl_fixed_t start_alpha = 0.0;
	struct fade_view_data *data = NULL;

	transition = get_transition_from_type_and_id(
					IVI_LAYOUT_TRANSITION_VIEW_FADE,
					surface);
	if (transition) {
		start_alpha = ivi_layout_surface_get_opacity(surface);
		user_data = transition->user_data;
		data = transition->private_data;

		transition->time_start = 0;
		transition->time_duration = duration;
		transition->destroy_func = visibility_on_transition_destroy;

		data->start_alpha = wl_fixed_to_double(start_alpha);
		data->end_alpha = user_data->alpha;
		return;
	}

	if (is_visible)
		return;

	user_data = malloc(sizeof(*user_data));
	if (user_data == NULL) {
		weston_log("%s: memory allocation fails\n", __func__);
		return;
	}

	user_data->alpha = wl_fixed_to_double(dest_alpha);

	create_visibility_transition(surface,
				     0.0, // start_alpha
				     wl_fixed_to_double(dest_alpha),
				     user_data,
				     visibility_on_transition_destroy,
				     duration);
}

static void
visibility_off_transition_destroy(struct ivi_layout_transition *transition)
{
	struct fade_view_data *data = transition->private_data;
	struct store_alpha *user_data = transition->user_data;

	ivi_layout_surface_set_visibility(data->surface, false);

	ivi_layout_surface_set_opacity(data->surface,
				       wl_fixed_from_double(user_data->alpha));

	free(data);
	transition->private_data = NULL;

	free(user_data);
	transition->user_data= NULL;
}

void
ivi_layout_transition_visibility_off(struct ivi_layout_surface *surface,
				     uint32_t duration)
{
	struct ivi_layout_transition *transition;
	wl_fixed_t start_alpha = ivi_layout_surface_get_opacity(surface);
	struct store_alpha* user_data = NULL;
	struct fade_view_data* data = NULL;

	transition =
		get_transition_from_type_and_id(IVI_LAYOUT_TRANSITION_VIEW_FADE,
						surface);
	if (transition) {
		data = transition->private_data;

		transition->time_start = 0;
		transition->time_duration = duration;
		transition->destroy_func = visibility_off_transition_destroy;

		data->start_alpha = wl_fixed_to_double(start_alpha);
		data->end_alpha = 0;
		return;
	}

	user_data = malloc(sizeof(*user_data));
	if (user_data == NULL) {
		weston_log("%s: memory allocation fails\n", __func__);
		return;
	}

	user_data->alpha = wl_fixed_to_double(start_alpha);

	create_visibility_transition(surface,
				     wl_fixed_to_double(start_alpha),
				     0.0, // dest_alpha
				     user_data,
				     visibility_off_transition_destroy,
				     duration);
}

/* move layer transition */

struct move_layer_data {
	struct ivi_layout_layer *layer;
	int32_t start_x;
	int32_t start_y;
	int32_t end_x;
	int32_t end_y;
	ivi_layout_transition_destroy_user_func destroy_func;
};

static void
transition_move_layer_user_frame(struct ivi_layout_transition *transition)
{
	struct move_layer_data *data = transition->private_data;
	struct ivi_layout_layer *layer = data->layer;

	const float  current = time_to_nowpos(transition);

	const int32_t dest_x = data->start_x +
		(data->end_x - data->start_x) * current;

	const int32_t dest_y = data->start_y +
		(data->end_y - data->start_y) * current;

	ivi_layout_layer_set_position(layer, dest_x, dest_y);
}

static void
transition_move_layer_destroy(struct ivi_layout_transition *transition)
{
	struct move_layer_data *data = transition->private_data;

	if(data->destroy_func)
		data->destroy_func(transition->user_data);

	free(data);
	transition->private_data = NULL;
}

static int32_t
is_transition_move_layer_func(struct move_layer_data *data,
			      struct ivi_layout_layer *layer)
{
	return data->layer == layer;
}


static struct ivi_layout_transition *
create_move_layer_transition(
		struct ivi_layout_layer *layer,
		int32_t start_x, int32_t start_y,
		int32_t end_x, int32_t end_y,
		void *user_data,
		ivi_layout_transition_destroy_user_func destroy_user_func,
		uint32_t duration)
{
	struct ivi_layout_transition *transition = create_layout_transition();
	if (transition == NULL)
		return NULL;

	struct move_layer_data *data = malloc(sizeof(*data));
	if (data == NULL) {
		weston_log("%s: memory allocation fails\n", __func__);
		return NULL;
	}

	transition->type = IVI_LAYOUT_TRANSITION_LAYER_MOVE;
	transition->is_transition_func = (ivi_layout_is_transition_func)is_transition_move_layer_func;

	transition->frame_func = transition_move_layer_user_frame;
	transition->destroy_func = transition_move_layer_destroy;
	transition->private_data = data;
	transition->user_data = user_data;

	if (duration != 0)
		transition->time_duration = duration;

	data->layer = layer;
	data->start_x = start_x;
	data->start_y = start_y;
	data->end_x   = end_x;
	data->end_y   = end_y;
	data->destroy_func = destroy_user_func;

	return transition;
}

void
ivi_layout_transition_move_layer(struct ivi_layout_layer *layer,
				 int32_t dest_x, int32_t dest_y,
				 uint32_t duration)
{
	int32_t start_pos_x = 0;
	int32_t start_pos_y = 0;
	struct ivi_layout_transition *transition = NULL;

	ivi_layout_layer_get_position(layer, &start_pos_x, &start_pos_y);

	transition = create_move_layer_transition(
		layer,
		start_pos_x, start_pos_y,
		dest_x, dest_y,
		NULL, NULL,
		duration);

	layout_transition_register(transition);

	return;
}

void
ivi_layout_transition_move_layer_cancel(struct ivi_layout_layer *layer)
{
	struct ivi_layout_transition *transition =
		get_transition_from_type_and_id(
					IVI_LAYOUT_TRANSITION_LAYER_MOVE,
					layer);
	if (transition) {
		layout_transition_destroy(transition);
	}
}

/* fade layer transition */
struct fade_layer_data {
	struct ivi_layout_layer *layer;
	uint32_t is_fade_in;
	double start_alpha;
	double end_alpha;
	ivi_layout_transition_destroy_user_func destroy_func;
};

static void
transition_fade_layer_destroy(struct ivi_layout_transition *transition)
{
	struct fade_layer_data *data = transition->private_data;
	transition->private_data = NULL;

	free(data);
}

static void
transition_fade_layer_user_frame(struct ivi_layout_transition *transition)
{
	double current = time_to_nowpos(transition);
	struct fade_layer_data *data = transition->private_data;
	double alpha = data->start_alpha +
		(data->end_alpha - data->start_alpha) * current;
	wl_fixed_t fixed_alpha = wl_fixed_from_double(alpha);

	int32_t is_done = transition->is_done;
	bool is_visible = !is_done || data->is_fade_in;

	ivi_layout_layer_set_opacity(data->layer, fixed_alpha);
	ivi_layout_layer_set_visibility(data->layer, is_visible);
}

static int32_t
is_transition_fade_layer_func(struct fade_layer_data *data,
			      struct ivi_layout_layer *layer)
{
	return data->layer == layer;
}

void
ivi_layout_transition_fade_layer(
			struct ivi_layout_layer *layer,
			uint32_t is_fade_in,
			double start_alpha, double end_alpha,
			void* user_data,
			ivi_layout_transition_destroy_user_func destroy_func,
			uint32_t duration)
{
	struct ivi_layout_transition *transition;
	struct fade_layer_data *data = NULL;
	wl_fixed_t fixed_opacity = 0.0;
	double now_opacity = 0.0;
	double remain = 0.0;

	transition = get_transition_from_type_and_id(
					IVI_LAYOUT_TRANSITION_LAYER_FADE,
					layer);
	if (transition) {
		/* transition update */
		data = transition->private_data;

		/* FIXME */
		fixed_opacity = ivi_layout_layer_get_opacity(layer);
		now_opacity = wl_fixed_to_double(fixed_opacity);
		remain = 0.0;

		data->is_fade_in = is_fade_in;
		data->start_alpha = now_opacity;
		data->end_alpha = end_alpha;

		remain = is_fade_in? 1.0 - now_opacity : now_opacity;
		transition->time_start = 0;
		transition->time_elapsed = 0;
		transition->time_duration = duration * remain;

		return;
	}

	transition = create_layout_transition();
	if (transition == NULL)
		return;

	data = malloc(sizeof(*data));
	if (data == NULL) {
		weston_log("%s: memory allocation fails\n", __func__);
		return;
	}

	transition->type = IVI_LAYOUT_TRANSITION_LAYER_FADE;
	transition->is_transition_func = (ivi_layout_is_transition_func)is_transition_fade_layer_func;

	transition->private_data = data;
	transition->user_data = user_data;

	transition->frame_func = transition_fade_layer_user_frame;
	transition->destroy_func = transition_fade_layer_destroy;

	if (duration != 0)
		transition->time_duration = duration;

	data->layer = layer;
	data->is_fade_in = is_fade_in;
	data->start_alpha = start_alpha;
	data->end_alpha = end_alpha;
	data->destroy_func = destroy_func;

	layout_transition_register(transition);

	return;
}