This repository has been archived by the owner on Aug 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
animation.c
306 lines (257 loc) · 9.59 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
#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
#include <sys/timerfd.h>
#include "oguri.h"
#include "buffers.h"
#include "output.h"
#include "animation.h"
static bool set_timer_milliseconds(int timer_fd, unsigned int delay) {
struct itimerspec spec = {
.it_value = (struct timespec) {
.tv_sec = delay / 1000,
.tv_nsec = (delay % 1000) * (long)1000000,
},
};
int ret = timerfd_settime(timer_fd, 0, &spec, NULL);
if (ret < 0) {
fprintf(stderr, "Timer error (fd %d): %s\n", timer_fd, strerror(errno));
return false;
}
return true;
}
static void scale_image_onto(
cairo_t * cairo,
cairo_surface_t * source,
struct oguri_output * output) {
// TODO: Store scaled width/height on buffer so we only need to pass config
int32_t buffer_width = output->width * output->scale;
int32_t buffer_height = output->height * output->scale;
int anchor = output->config->anchor;
cairo_filter_t filter = output->config->filter;
double width = cairo_image_surface_get_width(source);
double height = cairo_image_surface_get_height(source);
double window_ratio = (double)buffer_width / buffer_height;
double bg_ratio = width / height;
cairo_save(cairo);
cairo_matrix_t matrix;
cairo_matrix_init_identity(&matrix);
cairo_pattern_t * pattern = cairo_pattern_create_for_surface(source);
double scale_x = 0.0;
double scale_y = 0.0;
double offset_x = 0.0;
double offset_y = 0.0;
switch (output->config->scaling_mode) {
case SCALING_MODE_FILL:
if (window_ratio > bg_ratio) {
scale_x = scale_y = (double)buffer_width / width;
if (anchor & ANCHOR_TOP) {
offset_y = 0.0;
}
else if (anchor & ANCHOR_BOTTOM) {
offset_y = ((double)buffer_height / scale_y) - height;
}
else { // ANCHOR_CENTER
offset_y = ((double)buffer_height / 2 / scale_y) - (height / 2);
}
} else {
scale_x = scale_y = (double)buffer_height / height;
if (anchor & ANCHOR_LEFT) {
offset_x = 0.0;
}
else if (anchor & ANCHOR_RIGHT) {
offset_x = ((double)buffer_width / scale_x) - width;
}
else { // ANCHOR_CENTER
offset_x = ((double)buffer_width / 2 / scale_x) - (width / 2);
}
}
break;
case SCALING_MODE_STRETCH:
scale_x = (double)buffer_width / width;
scale_y = (double)buffer_height / height;
break;
case SCALING_MODE_TILE:
scale_x = scale_y = (double)output->scale;
cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
if (anchor & ANCHOR_LEFT) {
offset_x = 0.0;
}
else if (anchor & ANCHOR_RIGHT) {
offset_x = ((double)buffer_width / scale_x) - width;
}
else { // ANCHOR_CENTER
offset_x = ((double)buffer_width / 2 / scale_x) - (width / 2);
}
if (anchor & ANCHOR_TOP) {
offset_y = 0.0;
}
else if (anchor & ANCHOR_BOTTOM) {
offset_y = ((double)buffer_height / scale_y) - height;
}
else { // ANCHOR_CENTER
offset_y = ((double)buffer_height / 2 / scale_y) - (height / 2);
}
break;
}
cairo_matrix_translate(&matrix, -offset_x, -offset_y);
cairo_matrix_scale(&matrix, 1 / scale_x, 1 / scale_y);
cairo_pattern_set_matrix(pattern, &matrix);
cairo_pattern_set_filter(pattern, filter);
cairo_set_source(cairo, pattern);
cairo_paint(cairo);
cairo_pattern_destroy(pattern);
cairo_restore(cairo);
}
int oguri_render_frame(struct oguri_animation * anim) {
bool advanced = gdk_pixbuf_animation_iter_advance(anim->frame_iter, NULL);
GdkPixbuf * image = gdk_pixbuf_animation_iter_get_pixbuf(anim->frame_iter);
// If we've got another frame to display, update our timer. Note that while
// it isn't documented, the various implementations of this function take
// into account the elapsed time of the current frame. This means we can
// safely schedule frames early to redraw when new outputs appear without
// worrying about the timing of the next frame being wrong.
int delay = gdk_pixbuf_animation_iter_get_delay_time(anim->frame_iter);
oguri_animation_schedule_frame(anim, delay);
// Only count a frame if we actually advanced. If a new output was added,
// we may have been called early to render the previous frame again.
if (advanced && anim->first_cycle) {
++anim->frame_count;
}
// It's important to set this _after_ we increment the frame_count for the
// final time.
bool last_frame = gdk_pixbuf_animation_iter_on_currently_loading_frame(
anim->frame_iter);
if (last_frame) {
anim->first_cycle = false;
}
struct oguri_output * output;
wl_list_for_each(output, &anim->outputs, link) {
struct oguri_buffer * buffer = oguri_next_buffer(output);
if (output->cached_frames < anim->frame_count) {
if (!anim->first_cycle) {
// When we're past the first cycle, we want to have as many
// buffers as the animation has frames, because then we can
// keep each resized frame in a buffer instead of re-scaling it
// each time. However, we don't know which frame we started on,
// so just attempt to resize every frame until we've cached
// them all.
if (!oguri_allocate_buffers(output, anim->frame_count)) {
// TODO: This will freeze us at the current frame, probably
// should quit instead.
fprintf(stderr, "Unable to allocate %d frame buffers\n",
anim->frame_count);
return -1;
}
}
// Draw the frame into our source surface, at its native size.
oguri_cairo_surface_paint_pixbuf(anim->source_surface, image);
// Then scale it into the buffer.
scale_image_onto(buffer->cairo, anim->source_surface, output);
wl_surface_set_buffer_scale(output->surface, output->scale);
if (!anim->first_cycle) {
// We count the number of frames we've cached to know when
// we've cached them all, even if we didn't start at the first
// frame of the animation. On the first cycle, however, we
// don't want to cache because we don't know how many there
// will be (or if the animation is even finite, technically).
++output->cached_frames;
}
}
// TODO: This should mark the buffer as busy, but we're not actually
// checking for that anyway.
wl_surface_attach(output->surface, buffer->backing, 0, 0);
wl_surface_damage(output->surface, 0, 0, output->width, output->height);
wl_surface_commit(output->surface);
}
return delay;
}
bool oguri_animation_schedule_frame(
struct oguri_animation * anim, unsigned int delay) {
if (delay > 0) {
return set_timer_milliseconds(anim->timerfd, (unsigned int)delay);
}
else {
return true;
}
}
struct oguri_animation * oguri_animation_create(
struct oguri_state * oguri, char * image_path) {
int event_index = -1;
for (size_t i = OGURI_FIRST_ANIM_EVENT; i < OGURI_EVENT_COUNT; ++i) {
if (oguri->events[i].fd == -1) {
event_index = i;
break;
}
}
if (event_index == -1) {
fprintf(stderr, "No event slots available, too many animations!\n");
return NULL;
}
GError * error = NULL;
GdkPixbufAnimation * image = gdk_pixbuf_animation_new_from_file(
image_path, &error);
if (error || !image) {
fprintf(stderr, "Could not open image '%s': %s\n", image_path, error->message);
return NULL;
}
struct oguri_animation * anim = calloc(1, sizeof(struct oguri_animation));
wl_list_init(&anim->outputs);
anim->oguri = oguri;
anim->path = strdup(image_path);
anim->image = image;
anim->frame_iter = gdk_pixbuf_animation_get_iter(image, NULL);
// There's no way to directly ask for the number of frames in an animation,
// because gdk-pixbuf is designed to work with possibly streaming sources.
// However, when loading from a file, the final frame is always marked as
// the "loading frame". We use this, in combination with the following
// flag, to count the frames ourselves. Once we know how many there are, we
// can start caching the scaled buffers instead of rescaling each time we
// draw. We have to track the total length so we don't allocate an infinite
// number of buffers.
anim->first_cycle = true;
// The first frame drawn does not advance, so we can't count it. Instead,
// just start at 1.
anim->frame_count = 1;
// We're going to make the wild assumption that every frame in the
// animation has the same number of channels.
GdkPixbuf * first = gdk_pixbuf_animation_iter_get_pixbuf(anim->frame_iter);
int channel_count = gdk_pixbuf_get_n_channels(first);
// We need a cairo surface of the image's size to draw each frame into
// while scaling them up. This is as good a place for it as any.
anim->source_surface = cairo_image_surface_create(
(channel_count == 3) ? CAIRO_FORMAT_RGB24 : CAIRO_FORMAT_ARGB32,
gdk_pixbuf_animation_get_width(image),
gdk_pixbuf_animation_get_height(image));
anim->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
oguri->events[event_index] = (struct pollfd) {
.fd = anim->timerfd,
.events = POLLIN,
};
anim->event_index = event_index;
if (!oguri_animation_schedule_frame(anim, 1)) { // Show first frame ASAP.
fprintf(stderr, "Unable to schedule first timer\n");
}
wl_list_insert(oguri->animations.prev, &anim->link);
return anim;
}
void oguri_animation_destroy(struct oguri_animation * anim) {
wl_list_remove(&anim->link);
// Disable the pollfd entry so that another animation can reuse it later.
close(anim->timerfd);
anim->oguri->events[anim->event_index] = (struct pollfd) {
.fd = -1,
.events = 0, // Not strictly necessary, but eases debugging.
};
cairo_surface_destroy(anim->source_surface);
g_object_unref(anim->image);
g_object_unref(anim->frame_iter);
free(anim->path);
// Put all of the associated outputs back into the idle list, in case we
// want to reassign them to a new animation later. Destroying them doesn't
// happen until they are removed from the display, or we are told to exit.
wl_list_insert_list(&anim->oguri->idle_outputs, &anim->outputs);
anim->oguri = NULL;
free(anim);
}