forked from Tasssadar/multirom
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanimation.c
471 lines (408 loc) · 11.8 KB
/
animation.c
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
/*
* This file is part of MultiROM.
*
* MultiROM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MultiROM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MultiROM. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <math.h>
#include "log.h"
#include "workers.h"
#include "animation.h"
#include "util.h"
#include "framebuffer.h"
#include "containers.h"
struct anim_list_it
{
int anim_type;
anim_header *anim;
struct anim_list_it *prev;
struct anim_list_it *next;
};
struct anim_list
{
struct anim_list_it *first;
struct anim_list_it *last;
struct anim_list_it **inactive_ctx;
int running;
volatile int in_update_loop;
pthread_mutex_t mutex;
};
static struct anim_list_it EMPTY_CONTEXT;
static struct anim_list anim_list = {
.first = NULL,
.last = NULL,
.inactive_ctx = NULL,
.running = 0,
.in_update_loop = 0,
.mutex = PTHREAD_MUTEX_INITIALIZER,
};
static void anim_list_append(struct anim_list_it *it)
{
pthread_mutex_lock(&anim_list.mutex);
if(!anim_list.first)
{
anim_list.first = anim_list.last = it;
pthread_mutex_unlock(&anim_list.mutex);
return;
}
it->prev = anim_list.last;
anim_list.last->next = it;
anim_list.last = it;
pthread_mutex_unlock(&anim_list.mutex);
}
// anim_list.mutex must be locked
static void anim_list_rm(struct anim_list_it *it)
{
if(it->prev)
it->prev->next = it->next;
else
anim_list.first = it->next;
if(it->next)
it->next->prev = it->prev;
else
anim_list.last = it->prev;
}
// anim_list.mutex must be locked
static void anim_list_clear(void)
{
struct anim_list_it *it, *next;
for(next = anim_list.first; next; )
{
it = next;
next = next->next;
free(it->anim);
free(it);
}
anim_list.first = anim_list.last = NULL;
}
#define OVERSHOOT_TENSION 2.f
static float anim_interpolate(int type, float input)
{
switch(type)
{
default:
case INTERPOLATOR_LINEAR:
return input;
case INTERPOLATOR_DECELERATE:
return (1.f - (1.f - input) * (1.f - input));
case INTERPOLATOR_ACCELERATE:
return input * input;
case INTERPOLATOR_OVERSHOOT:
input -= 1.f;
return (input * input * ((OVERSHOOT_TENSION+1.f) * input + OVERSHOOT_TENSION) + 1.f);
case INTERPOLATOR_ACCEL_DECEL:
return (float)(cos((input + 1) * M_PI) / 2.0f) + 0.5f;
}
}
static inline void anim_int_step(int *prop, int *start, int *last, int *target, float interpolated)
{
if(*target != -1)
{
const int diff = *prop - *last;
*start += diff;
*prop = *start + (int)((*target) * interpolated);
*last = *prop;
}
}
static void item_anim_step(item_anim *anim, float interpolated)
{
fb_item_header *fb_it = anim->item;
anim_int_step(&fb_it->x, &anim->start[0], &anim->last[0], &anim->targetX, interpolated);
anim_int_step(&fb_it->y, &anim->start[1], &anim->last[1], &anim->targetY, interpolated);
anim_int_step(&fb_it->w, &anim->start[2], &anim->last[2], &anim->targetW, interpolated);
anim_int_step(&fb_it->h, &anim->start[3], &anim->last[3], &anim->targetH, interpolated);
}
static void item_anim_on_start(item_anim *anim)
{
fb_item_header *fb_it = anim->item;
anim->start[0] = anim->last[0] = fb_it->x;
anim->start[1] = anim->last[1] = fb_it->y;
anim->start[2] = anim->last[2] = fb_it->w;
anim->start[3] = anim->last[3] = fb_it->h;
if(anim->targetX != -1)
anim->targetX -= fb_it->x;
if(anim->targetY != -1)
anim->targetY -= fb_it->y;
if(anim->targetW != -1)
anim->targetW -= fb_it->w;
if(anim->targetH != -1)
anim->targetH -= fb_it->h;
}
static void item_anim_on_finished(item_anim *anim)
{
if(anim->destroy_item_when_finished)
fb_remove_item(anim->item);
}
static void call_anim_step(call_anim *anim, float interpolated)
{
if(anim->callback)
anim->callback(anim->data, interpolated);
}
static void anim_update(uint32_t diff, void *data)
{
struct anim_list *list = data;
struct anim_list_it *it;
anim_header *anim;
float normalized, interpolated;
int need_draw = 0;
pthread_mutex_lock(&list->mutex);
list->in_update_loop = 1;
for(it = list->first; it; )
{
anim = it->anim;
// Handle offset
if(anim->start_offset)
{
if(anim->start_offset > diff)
anim->start_offset -= diff;
else
anim->start_offset = 0;
it = it->next;
continue;
}
// calculate interpolation
anim->elapsed += diff;
if(anim->elapsed >= anim->duration)
normalized = 1.f;
else
normalized = ((float)anim->elapsed)/anim->duration;
interpolated = anim_interpolate(anim->interpolator, normalized);
// Handle animation step
switch(it->anim_type)
{
case ANIM_TYPE_ITEM:
need_draw = 1;
item_anim_step((item_anim*)anim, interpolated);
break;
case ANIM_TYPE_CALLBACK:
call_anim_step((call_anim*)anim, interpolated);
break;
}
if(anim->on_step_call)
{
pthread_mutex_unlock(&list->mutex);
anim->on_step_call(anim->on_step_data, interpolated);
pthread_mutex_lock(&list->mutex);
}
// remove complete animations
if(anim->elapsed >= anim->duration)
{
if(anim->on_finished_call)
{
pthread_mutex_unlock(&list->mutex);
anim->on_finished_call(anim->on_finished_data);
pthread_mutex_lock(&list->mutex);
}
switch(it->anim_type)
{
case ANIM_TYPE_ITEM:
pthread_mutex_unlock(&list->mutex);
item_anim_on_finished((item_anim*)anim);
pthread_mutex_lock(&list->mutex);
break;
}
struct anim_list_it *to_remove = it;
it = it->next;
anim_list_rm(to_remove);
free(to_remove->anim);
free(to_remove);
}
else
it = it->next;
}
if(need_draw)
fb_request_draw();
list->in_update_loop = 0;
pthread_mutex_unlock(&list->mutex);
}
static uint32_t anim_generate_id(void)
{
static uint32_t id = 0;
return id++;
}
void anim_init(void)
{
if(anim_list.running)
return;
anim_list.running = 1;
workers_add(&anim_update, &anim_list);
}
void anim_stop(int wait_for_finished)
{
if(!anim_list.running)
return;
while(wait_for_finished)
{
pthread_mutex_lock(&anim_list.mutex);
if(!anim_list.first)
{
pthread_mutex_unlock(&anim_list.mutex);
break;
}
pthread_mutex_unlock(&anim_list.mutex);
usleep(10000);
}
anim_list.running = 0;
workers_remove(&anim_update, &anim_list);
pthread_mutex_lock(&anim_list.mutex);
anim_list_clear();
pthread_mutex_unlock(&anim_list.mutex);
}
void anim_cancel(uint32_t id, int only_not_started)
{
if(!anim_list.running)
return;
struct anim_list_it *it;
pthread_mutex_lock(&anim_list.mutex);
for(it = anim_list.first; it; )
{
if(it->anim->id == id && (!only_not_started || it->anim->start_offset == 0))
{
anim_list_rm(it);
free(it->anim);
free(it);
break;
}
else
it = it->next;
}
pthread_mutex_unlock(&anim_list.mutex);
}
void anim_cancel_for(void *fb_item, int only_not_started)
{
if(!anim_list.running)
return;
if(anim_list.in_update_loop && pthread_equal(pthread_self(), workers_get_thread_id()))
return;
struct anim_list_it *it, *to_remove;
anim_header *anim;
pthread_mutex_lock(&anim_list.mutex);
for(it = anim_list.first; it; )
{
anim = it->anim;
if(!anim->cancel_check || (only_not_started && anim->start_offset == 0))
{
it = it->next;
continue;
}
if(anim->cancel_check(anim->cancel_check_data, fb_item))
{
to_remove = it;
it = it->next;
anim_list_rm(to_remove);
free(to_remove->anim);
free(to_remove);
}
else
it = it->next;
}
pthread_mutex_unlock(&anim_list.mutex);
}
void anim_push_context(void)
{
pthread_mutex_lock(&anim_list.mutex);
if(anim_list.first)
{
list_add(anim_list.first, &anim_list.inactive_ctx);
anim_list.first = anim_list.last = NULL;
}
else
{
list_add(&EMPTY_CONTEXT, &anim_list.inactive_ctx);
}
pthread_mutex_unlock(&anim_list.mutex);
}
void anim_pop_context(void)
{
pthread_mutex_lock(&anim_list.mutex);
if(!anim_list.inactive_ctx)
{
pthread_mutex_unlock(&anim_list.mutex);
return;
}
if(anim_list.first)
anim_list_clear();
const int idx = list_item_count(anim_list.inactive_ctx)-1;
struct anim_list_it *last_active_ctx = anim_list.inactive_ctx[idx];
if(last_active_ctx != &EMPTY_CONTEXT)
{
anim_list.first = last_active_ctx;
while(last_active_ctx->next)
last_active_ctx = last_active_ctx->next;
anim_list.last = last_active_ctx;
}
list_rm_at(idx, &anim_list.inactive_ctx, NULL);
pthread_mutex_unlock(&anim_list.mutex);
}
int anim_item_cancel_check(void *item_my, void *item_destroyed)
{
return item_my == item_destroyed;
}
item_anim *item_anim_create(void *fb_item, int duration, int interpolator)
{
item_anim *anim = mzalloc(sizeof(item_anim));
anim->id = anim_generate_id();
anim->item = fb_item;
anim->duration = duration;
anim->interpolator = interpolator;
anim->cancel_check_data = fb_item;
anim->cancel_check = anim_item_cancel_check;
anim->targetX = -1;
anim->targetY = -1;
anim->targetW = -1;
anim->targetH = -1;
return anim;
}
void item_anim_add(item_anim *anim)
{
item_anim_on_start(anim);
struct anim_list_it *it = mzalloc(sizeof(struct anim_list_it));
it->anim_type = ANIM_TYPE_ITEM;
it->anim = (anim_header*)anim;
anim_list_append(it);
}
void item_anim_add_after(item_anim *anim)
{
struct anim_list_it *it;
pthread_mutex_lock(&anim_list.mutex);
for(it = anim_list.first; it; it = it->next)
{
if(it->anim_type == ANIM_TYPE_ITEM && ((item_anim*)it->anim)->item == anim->item)
{
const int u = it->anim->start_offset + it->anim->duration - it->anim->elapsed;
anim->start_offset = imax(anim->start_offset, u);
}
}
pthread_mutex_unlock(&anim_list.mutex);
item_anim_add(anim);
}
call_anim *call_anim_create(void *data, call_anim_callback callback, int duration, int interpolator)
{
call_anim *anim = mzalloc(sizeof(call_anim));
anim->id = anim_generate_id();
anim->data = data;
anim->callback = callback;
anim->duration = duration;
anim->interpolator = interpolator;
return anim;
}
void call_anim_add(call_anim *anim)
{
struct anim_list_it *it = mzalloc(sizeof(struct anim_list_it));
it->anim_type = ANIM_TYPE_CALLBACK;
it->anim = (anim_header*)anim;
anim_list_append(it);
}