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
|
/*
* Copyright (C) 2022 - Vito Caputo - <vcaputo@pengaru.com>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include "til.h"
#include "til_fb.h"
#define CHECKERS_DEFAULT_SIZE 32
#define CHECKERS_DEFAULT_PATTERN CHECKERS_PATTERN_CHECKERED
#define CHECKERS_DEFAULT_DYNAMICS CHECKERS_DYNAMICS_ODD
#define CHECKERS_DEFAULT_DYNAMICS_RATE 1.0
typedef enum checkers_pattern_t {
CHECKERS_PATTERN_CHECKERED,
CHECKERS_PATTERN_RANDOM,
} checkers_pattern_t;
typedef enum checkers_dynamics_t {
CHECKERS_DYNAMICS_ODD,
CHECKERS_DYNAMICS_EVEN,
CHECKERS_DYNAMICS_ALTERNATING,
CHECKERS_DYNAMICS_RANDOM,
} checkers_dynamics_t;
typedef struct checkers_setup_t {
til_setup_t til_setup;
unsigned size;
checkers_pattern_t pattern;
checkers_dynamics_t dynamics;
float rate;
} checkers_setup_t;
typedef struct checkers_context_t {
checkers_setup_t setup;
} checkers_context_t;
static checkers_setup_t checkers_default_setup = {
.size = CHECKERS_DEFAULT_SIZE,
.pattern = CHECKERS_DEFAULT_PATTERN,
.dynamics = CHECKERS_DEFAULT_DYNAMICS,
.rate = CHECKERS_DEFAULT_DYNAMICS_RATE,
};
static void * checkers_create_context(unsigned ticks, unsigned num_cpus, til_setup_t *setup)
{
checkers_context_t *ctxt;
if (!setup)
setup = &checkers_default_setup.til_setup;
ctxt = calloc(1, sizeof(checkers_context_t));
if (!ctxt)
return NULL;
ctxt->setup = *(checkers_setup_t *)setup;
return ctxt;
}
static void checkers_destroy_context(void *context)
{
free(context);
}
static int checkers_fragmenter(void *context, const til_fb_fragment_t *fragment, unsigned number, til_fb_fragment_t *res_fragment)
{
checkers_context_t *ctxt = context;
return til_fb_fragment_tile_single(fragment, ctxt->setup.size, number, res_fragment);
}
static void checkers_prepare_frame(void *context, unsigned ticks, unsigned n_cpus, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
{
*res_fragmenter = checkers_fragmenter;
}
static inline unsigned hash(unsigned x)
{
x = ((x >> 16) ^ x) * 0x61C88647;
x = ((x >> 16) ^ x) * 0x61C88647;
x = ((x >> 16) ^ x) * 0x61C88647;
x = (x >> 16) ^ x;
return x;
}
static void checkers_render_fragment(void *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
{
checkers_context_t *ctxt = context;
int state;
switch (ctxt->setup.pattern) {
case CHECKERS_PATTERN_CHECKERED: {
unsigned tiles_per_row;
tiles_per_row = fragment->frame_width / ctxt->setup.size;
if (tiles_per_row * ctxt->setup.size < fragment->frame_width)
tiles_per_row++;
state = (fragment->number + (fragment->y / ctxt->setup.size) * !(tiles_per_row & 0x1)) & 0x1;
break;
}
case CHECKERS_PATTERN_RANDOM:
state = hash(fragment->number * 0x61C88647) & 0x1;
break;
}
switch (ctxt->setup.dynamics) {
case CHECKERS_DYNAMICS_ODD:
break;
case CHECKERS_DYNAMICS_EVEN:
state = ~state & 0x1;
break;
case CHECKERS_DYNAMICS_ALTERNATING:
state ^= ((unsigned)((float)ticks * ctxt->setup.rate) & 0x1);
break;
case CHECKERS_DYNAMICS_RANDOM: /* note: the big multiply here is just to get up out of the low bits */
state &= hash(fragment->number * 0x61C88647 + (unsigned)((float)ticks * ctxt->setup.rate)) & 0x1;
break;
}
if (!state)
til_fb_fragment_clear(fragment);
else
til_fb_fragment_fill(fragment, 0xffffffff);
}
static int checkers_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 *size;
const char *pattern;
const char *dynamics;
const char *dynamics_rate;
const char *size_values[] = {
"4",
"8",
"16",
"32",
"64",
"128",
NULL
};
const char *pattern_values[] = {
"checkered",
"random",
NULL
};
const char *dynamics_values[] = {
"odd",
"even",
"alternating",
"random",
NULL
};
const char *dynamics_rate_values[] = {
"1.0",
".75",
".5",
".25",
".1",
".01",
".001",
".0001",
NULL
};
int r;
r = til_settings_get_and_describe_value(settings,
&(til_setting_desc_t){
.name = "Checker size",
.key = "size",
.regex = "\\.[0-9]+",
.preferred = TIL_SETTINGS_STR(CHECKERS_DEFAULT_SIZE),
.values = size_values,
.annotations = NULL
},
&size,
res_setting,
res_desc);
if (r)
return r;
r = til_settings_get_and_describe_value(settings,
&(til_setting_desc_t){
.name = "Checkers pattern",
.key = "pattern",
.preferred = pattern_values[0],
.values = pattern_values,
.annotations = NULL
},
&pattern,
res_setting,
res_desc);
if (r)
return r;
r = til_settings_get_and_describe_value(settings,
&(til_setting_desc_t){
.name = "Checkers dynamics",
.key = "dynamics",
.preferred = dynamics_values[0],
.values = dynamics_values,
.annotations = NULL
},
&dynamics,
res_setting,
res_desc);
if (r)
return r;
if (strcasecmp(dynamics, "odd") && strcasecmp(dynamics, "even")) {
r = til_settings_get_and_describe_value(settings,
&(til_setting_desc_t){
.name = "Checkers dynamics rate",
.key = "dynamics_rate",
.preferred = dynamics_rate_values[0],
.values = dynamics_rate_values,
.annotations = NULL
},
&dynamics_rate,
res_setting,
res_desc);
if (r)
return r;
}
if (res_setup) {
checkers_setup_t *setup;
setup = til_setup_new(sizeof(*setup), (void(*)(til_setup_t *))free);
if (!setup)
return -ENOMEM;
sscanf(size, "%u", &setup->size);
if (!strcmp(pattern, "checkered"))
setup->pattern = CHECKERS_PATTERN_CHECKERED;
else if (!strcmp(pattern, "random"))
setup->pattern = CHECKERS_PATTERN_RANDOM;
else {
free(setup);
return -EINVAL;
}
if (!strcmp(dynamics, "odd"))
setup->dynamics = CHECKERS_DYNAMICS_ODD;
else if (!strcmp(dynamics, "even"))
setup->dynamics = CHECKERS_DYNAMICS_EVEN;
else if (!strcmp(dynamics, "alternating"))
setup->dynamics = CHECKERS_DYNAMICS_ALTERNATING;
else if (!strcmp(dynamics, "random"))
setup->dynamics = CHECKERS_DYNAMICS_RANDOM;
else {
free(setup);
return -EINVAL;
}
if (setup->dynamics != CHECKERS_DYNAMICS_ODD && setup->dynamics != CHECKERS_DYNAMICS_EVEN)
sscanf(dynamics_rate, "%f", &setup->rate);
*res_setup = &setup->til_setup;
}
return 0;
}
til_module_t checkers_module = {
.create_context = checkers_create_context,
.destroy_context = checkers_destroy_context,
.prepare_frame = checkers_prepare_frame,
.render_fragment = checkers_render_fragment,
.setup = checkers_setup,
.name = "checkers",
.description = "Checker-patterned overlay (threaded)",
.author = "Vito Caputo <vcaputo@pengaru.com>",
.flags = TIL_MODULE_OVERLAYABLE,
};
|