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
|
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "til.h"
#include "til_fb.h"
#include "til_settings.h"
#include "til_util.h"
#include "txt/txt.h"
/* Copyright (C) 2020 - Vito Caputo <vcaputo@pengaru.com> */
/* This implements a rudimentary compositing module for layering
* the output from other modules into a single frame.
*/
/* some TODOs:
* - support randomizing settings and context resets, configurable
* - maybe add a way for the user to supply the settings on the cli
* for the composed layers. That might actually need to be a more
* general solution in the top-level rototiller code, since the
* other meta modules like montage and rtv could probably benefit
* from the ability to feed in settings to the underlying modules.
*/
typedef struct compose_layer_t {
const til_module_t *module;
void *module_ctxt;
char *settings;
} compose_layer_t;
typedef struct compose_context_t {
unsigned n_cpus;
size_t n_layers;
compose_layer_t layers[];
} compose_context_t;
typedef struct compose_setup_t {
til_setup_t til_setup;
size_t n_layers;
char *layers[];
} compose_setup_t;
static void * compose_create_context(unsigned ticks, unsigned num_cpus, til_setup_t *setup);
static void compose_destroy_context(void *context);
static void compose_prepare_frame(void *context, unsigned ticks, unsigned n_cpus, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter);
static int compose_setup(const til_settings_t *settings, til_setting_t **res_setting, const til_setting_desc_t **res_desc, til_setup_t **res_setup);
static compose_setup_t compose_default_setup = {
.layers = { "drizzle", "stars", "spiro", "plato", NULL },
};
til_module_t compose_module = {
.create_context = compose_create_context,
.destroy_context = compose_destroy_context,
.prepare_frame = compose_prepare_frame,
.name = "compose",
.description = "Layered modules compositor",
.setup = compose_setup,
};
static void * compose_create_context(unsigned ticks, unsigned num_cpus, til_setup_t *setup)
{
compose_context_t *ctxt;
size_t n;
if (!setup)
setup = &compose_default_setup.til_setup;
for (n = 0; ((compose_setup_t *)setup)->layers[n]; n++);
ctxt = calloc(1, sizeof(compose_context_t) + n * sizeof(compose_layer_t));
if (!ctxt)
return NULL;
ctxt->n_cpus = num_cpus;
for (int i = 0; i < n; i++) {
const til_module_t *layer_module;
til_setup_t *layer_setup = NULL;
layer_module = til_lookup_module(((compose_setup_t *)setup)->layers[i]);
(void) til_module_randomize_setup(layer_module, &layer_setup, NULL);
ctxt->layers[i].module = layer_module;
(void) til_module_create_context(layer_module, ticks, layer_setup, &ctxt->layers[i].module_ctxt);
til_setup_free(layer_setup);
ctxt->n_layers++;
}
return ctxt;
}
static void compose_destroy_context(void *context)
{
compose_context_t *ctxt = context;
for (int i = 0; i < ctxt->n_layers; i++)
til_module_destroy_context(ctxt->layers[i].module, ctxt->layers[i].module_ctxt);
free(context);
}
static void compose_prepare_frame(void *context, unsigned ticks, unsigned n_cpus, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
{
compose_context_t *ctxt = context;
til_fb_fragment_clear(fragment);
for (int i = 0; i < ctxt->n_layers; i++)
til_module_render(ctxt->layers[i].module, ctxt->layers[i].module_ctxt, ticks, fragment);
}
/* return a randomized valid layers= setting */
static char * compose_random_layers_setting(void)
{
size_t n_modules, n_rand_overlays, n_overlayable = 0, base_idx;
char *layers = NULL;
const til_module_t **modules;
til_get_modules(&modules, &n_modules);
for (size_t i = 0; i < n_modules; i++) {
if (modules[i]->flags & TIL_MODULE_OVERLAYABLE)
n_overlayable++;
}
base_idx = rand() % (n_modules - n_overlayable);
for (size_t i = 0, j = 0; !layers && i < n_modules; i++) {
if (modules[i]->flags & TIL_MODULE_OVERLAYABLE)
continue;
if (j++ == base_idx)
layers = strdup(modules[i]->name);
}
/* TODO FIXME: this doesn't prevent duplicate overlays in the random set,
* which generally is undesirable - but actually watching the results, is
* sometimes interesting. Maybe another module flag is necessary for indicating
* manifold-appropriate overlays.
*/
n_rand_overlays = 1 + (rand() % (n_overlayable - 1));
for (size_t n = 0; n < n_rand_overlays; n++) {
size_t rand_idx = rand() % n_overlayable;
for (size_t i = 0, j = 0; i < n_modules; i++) {
if (!(modules[i]->flags & TIL_MODULE_OVERLAYABLE))
continue;
if (j++ == rand_idx) {
char *new;
new = realloc(layers, strlen(layers) + 1 + strlen(modules[i]->name) + 1);
if (!new) {
free(layers);
return NULL;
}
strcat(new, ":");
strcat(new, modules[i]->name);
layers = new;
break;
}
}
}
return layers;
}
static int compose_setup(const til_settings_t *settings, til_setting_t **res_setting, const til_setting_desc_t **res_desc, til_setup_t **res_setup)
{
const char *layers;
int r;
r = til_settings_get_and_describe_value(settings,
&(til_setting_desc_t){
.name = "Colon-separated list of module layers, in draw-order",
.key = "layers",
.preferred = "drizzle:stars:spiro:plato",
.annotations = NULL,
.random = compose_random_layers_setting,
},
&layers,
res_setting,
res_desc);
if (r)
return r;
/* turn layers colon-separated list into a null-terminated array of strings */
if (res_setup) {
compose_setup_t *setup;
const til_module_t **modules;
size_t n_modules;
char *toklayers, *layer;
int n = 2;
til_get_modules(&modules, &n_modules);
toklayers = strdup(layers);
if (!toklayers)
return -ENOMEM;
layer = strtok(toklayers, ":");
if (!layer)
return -EINVAL;
setup = til_setup_new(sizeof(*setup), (void(*)(til_setup_t *))free);
if (!setup)
return -ENOMEM;
do {
compose_setup_t *new;
size_t i;
/* other meta-modules like montage and rtv may need to
* have some consideration here, but for now I'm just
* going to let the user potentially compose with montage
* or rtv as one of the layers.
*/
if (!strcmp(layer, "compose")) { /* XXX: prevent infinite recursion */
til_setup_free(&setup->til_setup);
return -EINVAL;
}
for (i = 0; i < n_modules; i++) {
if (!strcmp(layer, modules[i]->name))
break;
}
if (i >= n_modules) {
til_setup_free(&setup->til_setup);
return -EINVAL;
}
new = realloc(setup, sizeof(*setup) + n * sizeof(*setup->layers));
if (!new) {
til_setup_free(&setup->til_setup);
return -ENOMEM;
}
new->layers[n - 2] = layer;
new->layers[n - 1] = NULL;
n++;
setup = new;
} while (layer = strtok(NULL, ":"));
*res_setup = &setup->til_setup;
}
return 0;
}
|