Debian Codegraph @

src/backend/backend_common.c

raw symbols SCIP
1 // SPDX-License-Identifier: MPL-2.0
2 // Copyright (c) Yuxuan Shui <[email protected]>
3 #include <math.h>
4 #include <stddef.h>
5 #include <stdint.h>
6 #include <string.h>
7 #include <xcb/render.h>
8 #include <xcb/xcb_image.h>
9 #include <xcb/xcb_renderutil.h>
10
11 #include "common.h"
12 #include "config.h"
13 #include "log.h"
14 #include "utils/kernel.h"
15 #include "utils/misc.h"
16 #include "wm/win.h"
17 #include "x.h"
18
19 #include "backend_common.h"
20
21 /**
22 * Generate a 1x1 <code>Picture</code> of a particular color.
23 */
24 xcb_render_picture_t
25 solid_picture(struct x_connection *c, bool argb, double a, double r, double g, double b) {
26 xcb_pixmap_t pixmap;
27 xcb_render_picture_t picture;
28 xcb_render_create_picture_value_list_t pa;
29 xcb_render_color_t col;
30 xcb_rectangle_t rect;
31
32 pixmap = x_create_pixmap(c, argb ? 32 : 8, 1, 1);
33 if (!pixmap) {
34 return XCB_NONE;
35 }
36
37 pa.repeat = XCB_RENDER_REPEAT_NORMAL;
38 picture = x_create_picture_with_standard_and_pixmap(
39 c, argb ? XCB_PICT_STANDARD_ARGB_32 : XCB_PICT_STANDARD_A_8, pixmap,
40 XCB_RENDER_CP_REPEAT, &pa);
41
42 if (!picture) {
43 xcb_free_pixmap(c->c, pixmap);
44 return XCB_NONE;
45 }
46
47 col.alpha = (uint16_t)(a * 0xffff);
48 col.red = (uint16_t)(r * 0xffff);
49 col.green = (uint16_t)(g * 0xffff);
50 col.blue = (uint16_t)(b * 0xffff);
51
52 rect.x = 0;
53 rect.y = 0;
54 rect.width = 1;
55 rect.height = 1;
56
57 xcb_render_fill_rectangles(c->c, XCB_RENDER_PICT_OP_SRC, picture, col, 1, &rect);
58 xcb_free_pixmap(c->c, pixmap);
59
60 return picture;
61 }
62
63 uint8_t *make_shadow(struct x_connection *c, const conv *kernel, ivec2 window_size,
64 ivec2 *out_shadow_size, int *out_shadow_stride) {
65 /*
66 * We classify shadows into 4 kinds of regions
67 * r = shadow radius
68 * (0, 0) is the top left of the window itself
69 * -r r width-r width+r
70 * -r +-----+---------+-----+
71 * | 1 | 2 | 1 |
72 * r +-----+---------+-----+
73 * | 2 | 3 | 2 |
74 * height-r +-----+---------+-----+
75 * | 1 | 2 | 1 |
76 * height+r +-----+---------+-----+
77 */
78 const double *shadow_sum = kernel->rsum;
79 assert(shadow_sum);
80 // We only support square kernels for shadow
81 assert(kernel->w == kernel->h);
82 int width = window_size.width, height = window_size.height;
83 int d = kernel->w;
84 int r = d / 2;
85 int swidth = width + r * 2, sheight = height + r * 2;
86
87 assert(d % 2 == 1);
88 assert(d > 0);
89
90 int scanline_pad = 0;
91 auto setup = xcb_get_setup(c->c);
92 xcb_format_t *fmt = xcb_setup_pixmap_formats(setup);
93 xcb_format_t *fmtend = fmt + xcb_setup_pixmap_formats_length(setup);
94 for (; fmt != fmtend; ++fmt) {
95 if (fmt->depth == 8) {
96 scanline_pad = fmt->scanline_pad;
97 break;
98 }
99 }
100 if (scanline_pad == 0) {
101 log_error("Couldn't find valid pixmap format with depth 8.");
102 return NULL;
103 }
104 scanline_pad = scanline_pad / 8 - 1;
105 int sstride = (swidth + scanline_pad) & (~scanline_pad);
106
107 uint8_t *data = calloc((size_t)sheight, (size_t)sstride);
108 if (!data) {
109 log_error("failed to allocate memory for shadow mask");
110 return NULL;
111 }
112
113 out_shadow_size->width = swidth;
114 out_shadow_size->height = sheight;
115 *out_shadow_stride = sstride;
116
117 // If the window body is smaller than the kernel, we do convolution directly
118 if (width < r * 2 && height < r * 2) {
119 for (int y = 0; y < sheight; y++) {
120 for (int x = 0; x < swidth; x++) {
121 double sum = sum_kernel_normalized(
122 kernel, d - x - 1, d - y - 1, width, height);
123 data[y * sstride + x] = (uint8_t)(sum * 255.0);
124 }
125 }
126 return data;
127 }
128
129 if (height < r * 2) {
130 // Implies width >= r * 2
131 // If the window height is smaller than the kernel, we divide
132 // the window like this:
133 // -r r width-r width+r
134 // +------+-------------+------+
135 // | | | |
136 // +------+-------------+------+
137 for (int y = 0; y < sheight; y++) {
138 for (int x = 0; x < r * 2; x++) {
139 double sum = sum_kernel_normalized(kernel, d - x - 1,
140 d - y - 1, d, height) *
141 255.0;
142 data[y * sstride + x] = (uint8_t)sum;
143 data[y * sstride + swidth - x - 1] = (uint8_t)sum;
144 }
145 }
146 for (int y = 0; y < sheight; y++) {
147 double sum =
148 sum_kernel_normalized(kernel, 0, d - y - 1, d, height) * 255.0;
149 memset(&data[y * sstride + r * 2], (uint8_t)sum,
150 (size_t)(width - 2 * r));
151 }
152 return data;
153 }
154 if (width < r * 2) {
155 // Similarly, for width smaller than kernel
156 for (int y = 0; y < r * 2; y++) {
157 for (int x = 0; x < swidth; x++) {
158 double sum = sum_kernel_normalized(kernel, d - x - 1,
159 d - y - 1, width, d) *
160 255.0;
161 data[y * sstride + x] = (uint8_t)sum;
162 data[(sheight - y - 1) * sstride + x] = (uint8_t)sum;
163 }
164 }
165 for (int x = 0; x < swidth; x++) {
166 double sum =
167 sum_kernel_normalized(kernel, d - x - 1, 0, width, d) * 255.0;
168 for (int y = r * 2; y < height; y++) {
169 data[y * sstride + x] = (uint8_t)sum;
170 }
171 }
172 return data;
173 }
174
175 // Implies: width >= r * 2 && height >= r * 2
176
177 // Fill part 3
178 for (int y = r; y < height + r; y++) {
179 memset(data + (ptrdiff_t)sstride * y + r, 0xff, (size_t)width);
180 }
181
182 // Part 1
183 for (int y = 0; y < r * 2; y++) {
184 for (int x = 0; x < r * 2; x++) {
185 double tmpsum = shadow_sum[y * d + x] * 255.0;
186 data[y * sstride + x] = (uint8_t)tmpsum;
187 data[(sheight - y - 1) * sstride + x] = (uint8_t)tmpsum;
188 data[(sheight - y - 1) * sstride + (swidth - x - 1)] = (uint8_t)tmpsum;
189 data[y * sstride + (swidth - x - 1)] = (uint8_t)tmpsum;
190 }
191 }
192
193 // Part 2, top/bottom
194 for (int y = 0; y < r * 2; y++) {
195 double tmpsum = shadow_sum[d * y + d - 1] * 255.0;
196 memset(&data[y * sstride + r * 2], (uint8_t)tmpsum, (size_t)(width - r * 2));
197 memset(&data[(sheight - y - 1) * sstride + r * 2], (uint8_t)tmpsum,
198 (size_t)(width - r * 2));
199 }
200
201 // Part 2, left/right
202 for (int x = 0; x < r * 2; x++) {
203 double tmpsum = shadow_sum[d * (d - 1) + x] * 255.0;
204 for (int y = r * 2; y < height; y++) {
205 data[y * sstride + x] = (uint8_t)tmpsum;
206 data[y * sstride + (swidth - x - 1)] = (uint8_t)tmpsum;
207 }
208 }
209
210 return data;
211 }
212
213 static struct conv **generate_box_blur_kernel(struct box_blur_args *args, int *kernel_count) {
214 int r = args->size * 2 + 1;
215 assert(r > 0);
216 auto ret = ccalloc(2, struct conv *);
217 ret[0] = cvalloc(sizeof(struct conv) + sizeof(double) * (size_t)r);
218 ret[1] = cvalloc(sizeof(struct conv) + sizeof(double) * (size_t)r);
219 ret[0]->w = r;
220 ret[0]->h = 1;
221 ret[1]->w = 1;
222 ret[1]->h = r;
223 for (int i = 0; i < r; i++) {
224 ret[0]->data[i] = 1;
225 ret[1]->data[i] = 1;
226 }
227 *kernel_count = 2;
228 return ret;
229 }
230
231 static struct conv **
232 generate_gaussian_blur_kernel(struct gaussian_blur_args *args, int *kernel_count) {
233 int r = args->size * 2 + 1;
234 assert(r > 0);
235 auto ret = ccalloc(2, struct conv *);
236 ret[0] = cvalloc(sizeof(struct conv) + sizeof(double) * (size_t)r);
237 ret[1] = cvalloc(sizeof(struct conv) + sizeof(double) * (size_t)r);
238 ret[0]->w = r;
239 ret[0]->h = 1;
240 ret[1]->w = 1;
241 ret[1]->h = r;
242 for (int i = 0; i <= args->size; i++) {
243 ret[0]->data[i] = ret[0]->data[r - i - 1] =
244 1.0 / (sqrt(2.0 * M_PI) * args->deviation) *
245 exp(-(args->size - i) * (args->size - i) /
246 (2 * args->deviation * args->deviation));
247 ret[1]->data[i] = ret[1]->data[r - i - 1] = ret[0]->data[i];
248 }
249 *kernel_count = 2;
250 return ret;
251 }
252
253 /// Generate blur kernels for gaussian and box blur methods. Generated kernel is not
254 /// normalized, and the center element will always be 1.
255 struct conv **generate_blur_kernel(enum blur_method method, void *args, int *kernel_count) {
256 switch (method) {
257 case BLUR_METHOD_BOX: return generate_box_blur_kernel(args, kernel_count);
258 case BLUR_METHOD_GAUSSIAN:
259 return generate_gaussian_blur_kernel(args, kernel_count);
260 default: break;
261 }
262 return NULL;
263 }
264
265 /// Generate kernel parameters for dual-kawase blur method. Falls back on approximating
266 /// standard gauss radius if strength is zero or below.
267 struct dual_kawase_params *generate_dual_kawase_params(void *args) {
268 struct dual_kawase_blur_args *blur_args = args;
269 static const struct {
270 int iterations; /// Number of down- and upsample iterations
271 float offset; /// Sample offset in half-pixels
272 int min_radius; /// Approximate gauss-blur with at least this
273 /// radius and std-deviation
274 } strength_levels[20] = {
275 {.iterations = 1, .offset = 1.25F, .min_radius = 1}, // LVL 1
276 {.iterations = 1, .offset = 2.25F, .min_radius = 6}, // LVL 2
277 {.iterations = 2, .offset = 2.00F, .min_radius = 11}, // LVL 3
278 {.iterations = 2, .offset = 3.00F, .min_radius = 17}, // LVL 4
279 {.iterations = 2, .offset = 4.25F, .min_radius = 24}, // LVL 5
280 {.iterations = 3, .offset = 2.50F, .min_radius = 32}, // LVL 6
281 {.iterations = 3, .offset = 3.25F, .min_radius = 40}, // LVL 7
282 {.iterations = 3, .offset = 4.25F, .min_radius = 51}, // LVL 8
283 {.iterations = 3, .offset = 5.50F, .min_radius = 67}, // LVL 9
284 {.iterations = 4, .offset = 3.25F, .min_radius = 83}, // LVL 10
285 {.iterations = 4, .offset = 4.00F, .min_radius = 101}, // LVL 11
286 {.iterations = 4, .offset = 5.00F, .min_radius = 123}, // LVL 12
287 {.iterations = 4, .offset = 6.00F, .min_radius = 148}, // LVL 13
288 {.iterations = 4, .offset = 7.25F, .min_radius = 178}, // LVL 14
289 {.iterations = 4, .offset = 8.25F, .min_radius = 208}, // LVL 15
290 {.iterations = 5, .offset = 4.50F, .min_radius = 236}, // LVL 16
291 {.iterations = 5, .offset = 5.25F, .min_radius = 269}, // LVL 17
292 {.iterations = 5, .offset = 6.25F, .min_radius = 309}, // LVL 18
293 {.iterations = 5, .offset = 7.25F, .min_radius = 357}, // LVL 19
294 {.iterations = 5, .offset = 8.50F, .min_radius = 417}, // LVL 20
295 };
296
297 auto params = ccalloc(1, struct dual_kawase_params);
298 params->iterations = 0;
299 params->offset = 1.0F;
300
301 if (blur_args->strength <= 0 && blur_args->size) {
302 // find highest level that approximates blur-strength with the selected
303 // gaussian blur-radius
304 int lvl = 1;
305 while (strength_levels[lvl - 1].min_radius < blur_args->size && lvl < 20) {
306 ++lvl;
307 }
308 blur_args->strength = lvl;
309 }
310 if (blur_args->strength <= 0) {
311 // default value
312 blur_args->strength = 5;
313 }
314
315 assert(blur_args->strength > 0 && blur_args->strength <= 20);
316 params->iterations = strength_levels[blur_args->strength - 1].iterations;
317 params->offset = strength_levels[blur_args->strength - 1].offset;
318
319 // Expand sample area to cover the smallest texture / highest selected iteration:
320 // - Smallest texture dimensions are halved `iterations`-times
321 // - Upsample needs pixels two-times `offset` away from the border
322 // - Plus one for interpolation differences
323 params->expand = (1 << params->iterations) * 2 * (int)ceilf(params->offset) + 1;
324
325 return params;
326 }
327
328 void init_backend_base(struct backend_base *base, session_t *ps) {
329 base->c = &ps->c;
330 base->loop = ps->loop;
331 base->busy = false;
332 base->ops = (struct backend_operations){};
333 }
334
335 uint32_t backend_no_quirks(struct backend_base *base attr_unused) {
336 return 0;
337 }