-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend_xrender.c
312 lines (264 loc) · 12.6 KB
/
backend_xrender.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
void generate_gauss_filter(XRESOURCES* res) {
//interpolate blur kernel
int i;
int j;
double first = 1.0 / (2.0 * M_PI * BLUR_SIGMA * BLUR_SIGMA);
double div = 2.0 * BLUR_SIGMA * BLUR_SIGMA;
double val;
double sum = 0.0;
int half_dim = BLUR_KERNEL_DIM / 2;
// generate gauss values
for (i = half_dim * (-1.0); i < half_dim + 1; i++) {
for (j = (-1.0) * half_dim; j < half_dim + 1; j++) {
val = (first * exp( -1.0 * ((i * i + j * j) / div)));
sum += val;
res->backend.gauss_kernel[i + half_dim][j + half_dim] = val;
}
}
// normalize
for (i = 0; i < BLUR_KERNEL_DIM; i++) {
for (j = 0; j < BLUR_KERNEL_DIM; j++) {
res->backend.gauss_kernel[i][j] /= sum;
}
}
}
int backend_init(XRESOURCES* res, CONFIG* config){
int xdbe_major, xdbe_minor;
int xrender_major, xrender_minor;
int gobo_max_width = 0, gobo_max_height = 0;
unsigned u;
if(!XRenderQueryExtension(res->display, &xrender_major, &xrender_minor)){
fprintf(stderr, "XRender extension not enabled on display\n");
return -1;
}
config->double_buffer = (XdbeQueryExtension(res->display, &xdbe_major, &xdbe_minor) != 0);
XRenderQueryVersion(res->display, &xrender_major, &xrender_minor);
fprintf(stderr, "Xdbe version %d.%d\n", xdbe_major, xdbe_minor);
fprintf(stderr, "Xrender version %d.%d\n", xrender_major, xrender_minor);
//allocate back drawing buffer
if(config->double_buffer){
res->backend.back_buffer = XdbeAllocateBackBufferName(res->display, res->main, XdbeBackground);
}
for(u = 0; u < 256; u++){
if(res->gobo[u].data){
res->gobo[u].ximage = XCreateImage(res->display, DefaultVisual(res->display, res->screen), 32, ZPixmap, 0, (char*)res->gobo[u].data, res->gobo[u].width, res->gobo[u].height, 32, 0);
if(!res->gobo[u].ximage){
fprintf(stderr, "Failed to create XImage for gobo %d\n", u);
return -1;
}
if(res->gobo[u].height > gobo_max_height){
gobo_max_height = res->gobo[u].height;
}
if(res->gobo[u].width > gobo_max_width){
gobo_max_width = res->gobo[u].width;
}
}
}
fprintf(stderr, "Creating gobo pixmaps with dimensions %dx%d\n", gobo_max_width, gobo_max_height);
res->backend.gobo_pixmap = XCreatePixmap(res->display, res->main, gobo_max_width, gobo_max_height, 32);
res->backend.color_pixmap = XCreatePixmap(res->display, res->main, gobo_max_width, gobo_max_height, 32);
if(!res->backend.gobo_pixmap || ! res->backend.color_pixmap){
fprintf(stderr, "Failed to create backing pixmaps\n");
return -1;
}
//debug_gc = XCreateGC(xres->display, gobo_pixmap, GCForeground | GCBackground, &debug_gc_values);
res->backend.window_gc = XCreateGC(res->display, res->backend.gobo_pixmap, 0, NULL);
res->backend.composite_buffer = XRenderCreatePicture(res->display, res->main, XRenderFindStandardFormat(res->display, PictStandardARGB32), 0, 0);
res->backend.color_buffer = XRenderCreatePicture(res->display, res->backend.color_pixmap, XRenderFindStandardFormat(res->display, PictStandardARGB32), 0, 0);
res->backend.alpha_mask = XRenderCreatePicture(res->display, res->backend.gobo_pixmap, XRenderFindStandardFormat(res->display, PictStandardARGB32), 0, 0);
//check XRender filtering capabilities
XFilters* filters = XRenderQueryFilters(res->display, res->backend.gobo_pixmap);
for(u = 0; u < filters->nfilter; u++){
fprintf(stderr, "Available filter %d of %d: %s\n", u + 1, filters->nfilter, filters->filter[u]);
if(!strcmp(filters->filter[u], "convolution")){
fprintf(stderr, "Convolution filter supported, enabling focus effect...\n");
res->backend.blur_enabled = true;
generate_gauss_filter(res);
}
}
XFree(filters);
return 0;
}
int xlaser_reconfigure(XRESOURCES* res){
return 0;
}
int xlaser_render(XRESOURCES* xres, uint8_t* channels){
uint8_t selected_gobo;
//XColor rgb_color;
XRenderColor render_color = {
0
};
XRenderColor clear_color = {
//.alpha = (65535) * ((double)((255.0 - (double)channels[TRACE])/255.0))
.alpha = (255 - channels[TRACE]) << 8 | 0xFF
};
XTransform transform = {{
{XDoubleToFixed(1), XDoubleToFixed(0), XDoubleToFixed(0)},
{XDoubleToFixed(0), XDoubleToFixed(1), XDoubleToFixed(0)},
{XDoubleToFixed(0), XDoubleToFixed(0), XDoubleToFixed(1)}
}};
fprintf(stderr, "Clear color is %u\n", clear_color.alpha);
//legacy compliant code
/*XGCValues debug_gc_values = {
.foreground = WhitePixel(xres->display, xres->screen),
.background = BlackPixel(xres->display, xres->screen)
};
GC debug_gc;*/
//presentation
double scaling_factor = 1.0;
double dimmer_factor = 1.0;
double angle = 0.0;
double angle_sin = 0.0, angle_cos = 0.0;
unsigned x_pos, y_pos;
long shutter_offset = 0, shutter_interval = 0;
//XdbeSwapInfo swap_info;
//set up gobo
//FIXME might only want to update this upon incoming DMX data
for(selected_gobo = channels[GOBO]; selected_gobo >= 0 && !(xres->gobo[selected_gobo].data); selected_gobo--){
}
if(!(xres->gobo[selected_gobo].data)){
fprintf(stderr, "No gobo selection possible\n");
return -1;
}
//read color data
render_color.red = channels[RED] << 8;
render_color.green = channels[GREEN] << 8;
render_color.blue = channels[BLUE] << 8;
//FIXME is this still needed
/*rgb_color.red = channels[RED] << 8;
rgb_color.green = channels[GREEN] << 8;
rgb_color.blue = channels[BLUE] << 8;
XAllocColor(xres->display, xres->colormap, &rgb_color);
debug_gc_values.foreground = rgb_color.pixel;
//XChangeGC(xres->display, debug_gc, GCForeground, &debug_gc_values);*/
//calculate dimmer from input data
dimmer_factor = (double)channels[DIMMER] / 255.0;
//FIXME using the dimmer channel as alpha component does not seem to work with XRenderFillRectangle
//render_color.alpha = channels[DIMMER] << 8;
//process shutter channel
if(channels[SHUTTER]){
if(channels[SHUTTER] >= 1 && channels[SHUTTER] <= 127){
//strobe effect
shutter_offset = xres->last_render.tv_nsec % (long)(1e9 / (channels[SHUTTER] / 8.0));
dimmer_factor = (shutter_offset > 55e6) ? 0:dimmer_factor;
}
else if(channels[SHUTTER] >= 128 && channels[SHUTTER] <= 192){
//flash in
shutter_interval = 1e9 / (channels[SHUTTER] - 127);
shutter_offset = xres->last_render.tv_nsec % shutter_interval;
dimmer_factor *= (double) shutter_offset / (double) shutter_interval;
}
else if(channels[SHUTTER] >= 193 && channels[SHUTTER] <= 255){
//flash out
shutter_interval = 1e9 / (channels[SHUTTER] - 192);
shutter_offset = xres->last_render.tv_nsec % shutter_interval;
dimmer_factor *= 1.0 - ((double) shutter_offset / (double) shutter_interval);
}
}
//apply dimming
render_color.red *= dimmer_factor;
render_color.green *= dimmer_factor;
render_color.blue *= dimmer_factor;
//set up zoom
scaling_factor = (double)(256 - channels[ZOOM]) / 255.0;
if(scaling_factor > 1.0f){
scaling_factor = 1.0f;
}
//set up position
x_pos = ((double)((channels[PAN] << 8) | channels[PAN_FINE]) / 65535.0) * ((double)xres->window_width - ((double)xres->gobo[selected_gobo].width * scaling_factor));
y_pos = ((double)((channels[TILT] << 8) | channels[TILT_FINE]) / 65535.0) * ((double)xres->window_height - ((double)xres->gobo[selected_gobo].height * scaling_factor));
//fprintf(stderr, "PAN/FINE: %d/%d, W: %d, X: %d\n", config->dmx_channels[PAN], config->dmx_channels[PAN_FINE], window_width, x_pos);
//fprintf(stderr, "TILT/FINE: %d/%d, H: %d, Y: %d\n", config->dmx_channels[TILT], config->dmx_channels[TILT_FINE], window_height, y_pos);
//correct position for scaled gobo dimensions
x_pos -= (double)(xres->gobo[selected_gobo].width - ((double)xres->gobo[selected_gobo].width * scaling_factor)) / 2.0;
y_pos -= (double)(xres->gobo[selected_gobo].height - ((double)xres->gobo[selected_gobo].height * scaling_factor)) / 2.0;
//set up rotation
angle = M_PI / 180 * ((double)(channels[ROTATION]) / 255.0) * 360;
angle_sin = sin(angle);
angle_cos = cos(angle);
double center_x = (double)xres->gobo[selected_gobo].width / 2.0;
double center_y = (double)xres->gobo[selected_gobo].height / 2.0;
double transform_x = -center_x * angle_cos + -center_y * angle_sin + scaling_factor * center_x;
double transform_y = center_x * angle_sin + -center_y * angle_cos + scaling_factor * center_y;
//flood-fill the color pixmap TODO benchmark XFillRectangle vs XRenderFillRectangle
//XFillRectangle(xres->display, color_pixmap, debug_gc, 0, 0, window_width, window_height);
XRenderFillRectangle(xres->display, PictOpSrc, xres->backend.color_buffer, &render_color, 0, 0, xres->gobo[selected_gobo].width, xres->gobo[selected_gobo].height);
//fill color pixmap via normal X operation
//XFillRectangle(xres->display, xres->back_buffer, debug_gc, 200, 200, 50, 50);
//XRenderFillRectangle(xres->display, PictOpOver, back_buffer, &render_color, 400, 200, 50, 50);
//XPutImage(xres->display, xres->back_buffer, DefaultGC(xres->display, xres->screen), xres->gobo[selected_gobo].ximage, 0, 0, x_pos, y_pos, xres->gobo[selected_gobo].width, xres->gobo[selected_gobo].height);
XPutImage(xres->display, xres->backend.gobo_pixmap, xres->backend.window_gc, xres->gobo[selected_gobo].ximage, 0, 0, 0, 0, xres->gobo[selected_gobo].width, xres->gobo[selected_gobo].height);
//XPutImage(xres->display, xres->gobo_pixmap, DefaultGC(xres->display, xres->screen), xres->gobo[selected_gobo].ximage, 0, 0, 0, 0, xres->gobo[selected_gobo].width, xres->gobo[selected_gobo].height);
//XPutImage(xres->display, xres->back_buffer, debug_gc, xres->gobo[selected_gobo].ximage, 0, 0, x_pos, y_pos, xres->gobo[selected_gobo].width, xres->gobo[selected_gobo].height);
//TODO fix XRender double buffering
/*if(config->double_buffer){
fprintf(stderr, "Swapping buffers\n");
swap_info.swap_window = xres->main;
swap_info.swap_action = XdbeBackground;
XdbeSwapBuffers(xres->display, &swap_info, 1);
}
else{
fprintf(stderr, "Clearing window\n");
XClearWindow(xres->display, xres->main);
}*/
//set up the transform matrix
transform.matrix[2][2] = XDoubleToFixed(scaling_factor);
transform.matrix[0][0] = XDoubleToFixed(angle_cos);
transform.matrix[0][1] = XDoubleToFixed(angle_sin);
transform.matrix[1][0] = XDoubleToFixed(-angle_sin);
transform.matrix[1][1] = XDoubleToFixed(angle_cos);
transform.matrix[0][2] = XDoubleToFixed(transform_x);
transform.matrix[1][2] = XDoubleToFixed(transform_y);
if(xres->backend.blur_enabled){
if(channels[FOCUS]){
// n x n matrix plus width and height
int blur_kernel_size = BLUR_KERNEL_DIM * BLUR_KERNEL_DIM + 2;
XFixed blur_kernel[blur_kernel_size];
blur_kernel[0] = XDoubleToFixed(BLUR_KERNEL_DIM);
blur_kernel[1] = XDoubleToFixed(BLUR_KERNEL_DIM);
double chan = (double) channels[FOCUS] / 255.0;
double val = 0.0;
unsigned i, j;
for (i = 0; i < BLUR_KERNEL_DIM; i++) {
for (j = 0; j < BLUR_KERNEL_DIM; j++) {
val = chan * xres->backend.gauss_kernel[i][j];
if (i == BLUR_KERNEL_DIM / 2 && j == BLUR_KERNEL_DIM / 2) {
val += 1.0 - chan;
}
blur_kernel[(i * BLUR_KERNEL_DIM) + j + 2] = XDoubleToFixed(val);
}
}
fprintf(stderr, "Applying blur filter\n");
//apply blur
XRenderSetPictureFilter(xres->display, xres->backend.alpha_mask, "convolution", blur_kernel, blur_kernel_size);
}
else{
XRenderSetPictureFilter(xres->display, xres->backend.alpha_mask, "fast", NULL, 0);
}
}
XRenderSetPictureTransform(xres->display, xres->backend.alpha_mask, &transform);
//XRenderSetPictureTransform(xres->display, color_buffer, &transform);
//XClearWindow(xres->display, xres->main);
XRenderFillRectangle(xres->display, PictOpOver, xres->backend.composite_buffer, &clear_color, 0, 0, xres->window_width, xres->window_height);
XRenderComposite(xres->display, PictOpOver, xres->backend.color_buffer, xres->backend.alpha_mask, xres->backend.composite_buffer, 0, 0, 0, 0, x_pos, y_pos, xres->gobo[selected_gobo].width, xres->gobo[selected_gobo].height);
//XRenderComposite(xres->display, PictOpOver, alpha_mask, alpha_mask, color_buffer, 0, 0, 0, 0, 0, 0, xres->gobo[selected_gobo].width, xres->gobo[selected_gobo].height);
//XRenderComposite(xres->display, PictOpOver, color_buffer, None, back_buffer, 0, 0, 0, 0, x_pos, y_pos, xres->gobo[selected_gobo].width, xres->gobo[selected_gobo].height);
//XRenderFillRectangle(xres->display, PictOpSrc, back_buffer, &render_color, 600, 200, 50, 50);
return 0;
}
void backend_free(XRESOURCES* res){
unsigned u;
XRenderFreePicture(res->display, res->backend.composite_buffer);
XRenderFreePicture(res->display, res->backend.alpha_mask);
XRenderFreePicture(res->display, res->backend.color_buffer);
XFreePixmap(res->display, res->backend.gobo_pixmap);
XFreePixmap(res->display, res->backend.color_pixmap);
for(u = 0; u < 256; u++){
if(res->gobo[u].data){
//XDestroyImage also frees the backing data, so stbi_image_free would be a double-free
XDestroyImage(res->gobo[u].ximage);
res->gobo[u].data = NULL;
}
}
XFreeGC(res->display, res->backend.window_gc);
}