Debian Codegraph @

src/backend/xrender/xrender.c

raw symbols SCIP
1 // SPDX-License-Identifier: MPL-2.0
2 // Copyright (c) Yuxuan Shui <[email protected]>
3 #include <assert.h>
4 #include <math.h>
5 #include <stdint.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <sys/mman.h>
9
10 #include <xcb/composite.h>
11 #include <xcb/present.h>
12 #include <xcb/render.h>
13 #include <xcb/shm.h>
14 #include <xcb/sync.h>
15 #include <xcb/xcb.h>
16 #include <xcb/xproto.h>
17
18 #include <picom/backend.h>
19 #include <picom/types.h>
20
21 #include "backend/backend.h"
22 #include "backend/backend_common.h"
23 #include "backend/driver.h"
24 #include "common.h"
25 #include "compiler.h"
26 #include "config.h"
27 #include "log.h"
28 #include "picom.h"
29 #include "region.h"
30 #include "utils/kernel.h"
31 #include "utils/misc.h"
32 #include "x.h"
33
34 struct xrender_image_data_inner {
35 ivec2 size;
36 enum backend_image_format format;
37 struct xrender_rounded_rectangle_cache *rounded_rectangle;
38 // Pixmap that the client window draws to,
39 // it will contain the content of client window.
40 xcb_pixmap_t pixmap;
41 // A Picture links to the Pixmap
42 xcb_render_picture_t pict;
43 xcb_render_pictformat_t pictfmt;
44 uint8_t depth;
45 // Whether we allocated it this pixmap.
46 // or not, i.e. this pixmap is passed in via xrender_bind_pixmap
47 bool is_pixmap_internal;
48 bool has_alpha;
49 };
50
51 typedef struct xrender_data {
52 struct backend_base base;
53 /// The X ID for the shared memory segment.
54 uint32_t shm_seg_id;
55 /// Shared memory file descriptor allocated by the X server.
56 int shm_fd;
57 /// `shm_fd` mmap'd into memory.
58 void *shm;
59 /// Size of the shared memory segment.
60 uint32_t shm_size;
61 /// Quirks
62 uint32_t quirks;
63 /// Target window
64 xcb_window_t target_win;
65 /// Painting target, it is either the root or the overlay
66 xcb_render_picture_t target;
67 /// Back buffers. Double buffer, with 1 for temporary render use
68 xcb_render_picture_t back[2];
69 /// Fake image to represent the back buffer
70 struct xrender_image_data_inner back_image;
71 /// Damaged region of the back image since the last present
72 region_t back_damaged;
73 /// The back buffer that is for temporary use
74 /// Age of each back buffer.
75 int buffer_age[2];
76 /// The back buffer we should be painting into
77 int curr_back;
78 /// The corresponding pixmap to the back buffer
79 xcb_pixmap_t back_pixmap[2];
80 /// Pictures of pixel of different alpha value, used as a mask to
81 /// paint transparent images
82 xcb_render_picture_t alpha_pict[256];
83
84 // XXX don't know if these are really needed
85
86 /// 1x1 white picture
87 xcb_render_picture_t white_pixel;
88 /// 1x1 black picture
89 xcb_render_picture_t black_pixel;
90 /// 1x1 temporary picture for tinting
91 xcb_render_picture_t tint_pixel;
92
93 xcb_special_event_t *present_event;
94
95 /// Cache an X region to avoid creating and destroying it every frame. A
96 /// workaround for yshui/picom#1166.
97 xcb_xfixes_region_t present_region;
98 /// If vsync is enabled and supported by the current system
99 bool vsync;
100 } xrender_data;
101
102 struct xrender_blur_context {
103 enum blur_method method;
104 /// Blur kernels converted to X format
105 struct x_convolution_kernel **x_blur_kernel;
106
107 int resize_width, resize_height;
108
109 /// Number of blur kernels
110 int x_blur_kernel_count;
111 };
112
113 struct xrender_rounded_rectangle_cache {
114 // A cached picture of a rounded rectangle. Xorg rasterizes shapes on CPU so it's
115 // exceedingly slow.
116 xcb_render_picture_t p;
117 int radius;
118 };
119
120 static void
121 set_picture_scale(struct x_connection *c, xcb_render_picture_t picture, vec2 scale) {
122 xcb_render_transform_t transform = {
123 .matrix11 = DOUBLE_TO_XFIXED(1.0 / scale.x),
124 .matrix22 = DOUBLE_TO_XFIXED(1.0 / scale.y),
125 .matrix33 = DOUBLE_TO_XFIXED(1.0),
126 };
127 x_set_error_action_abort(c, xcb_render_set_picture_transform(c->c, picture, transform));
128 }
129
130 /// Make a picture of size width x height, which has a rounded rectangle of corner_radius
131 /// rendered in it.
132 struct xrender_rounded_rectangle_cache *
133 xrender_make_rounded_corner_cache(struct x_connection *c, xcb_render_picture_t src,
134 int width, int height, int corner_radius) {
135 auto picture = x_create_picture_with_standard(c, width, height,
136 XCB_PICT_STANDARD_ARGB_32, 0, NULL);
137 if (picture == XCB_NONE) {
138 return NULL;
139 }
140
141 int inner_height = height - 2 * corner_radius;
142 int cap_height = corner_radius;
143 if (inner_height < 0) {
144 cap_height = height / 2;
145 inner_height = 0;
146 }
147 auto points = ccalloc(cap_height * 4 + 4, xcb_render_pointfix_t);
148 int point_count = 0;
149
150 #define ADD_POINT(px, py) \
151 assert(point_count < cap_height * 4 + 4); \
152 points[point_count].x = DOUBLE_TO_XFIXED(px); \
153 points[point_count].y = DOUBLE_TO_XFIXED(py); \
154 point_count += 1;
155
156 // The top cap
157 for (int i = 0; i <= cap_height; i++) {
158 double y = corner_radius - i;
159 double delta = sqrt(corner_radius * corner_radius - y * y);
160 double left = corner_radius - delta;
161 double right = width - corner_radius + delta;
162 if (left >= right) {
163 continue;
164 }
165 ADD_POINT(left, i);
166 ADD_POINT(right, i);
167 }
168
169 // The middle rectangle
170 if (inner_height > 0) {
171 ADD_POINT(0, cap_height + inner_height);
172 ADD_POINT(width, cap_height + inner_height);
173 }
174
175 // The bottom cap
176 for (int i = cap_height + inner_height + 1; i <= height; i++) {
177 double y = corner_radius - (height - i);
178 double delta = sqrt(corner_radius * corner_radius - y * y);
179 double left = corner_radius - delta;
180 double right = width - corner_radius + delta;
181 if (left >= right) {
182 break;
183 }
184 ADD_POINT(left, i);
185 ADD_POINT(right, i);
186 }
187 #undef ADD_POINT
188
189 XCB_AWAIT_VOID(xcb_render_tri_strip, c, XCB_RENDER_PICT_OP_SRC, src, picture,
190 x_get_pictfmt_for_standard(c, XCB_PICT_STANDARD_A_8), 0, 0,
191 (uint32_t)point_count, points);
192 free(points);
193 auto ret = ccalloc(1, struct xrender_rounded_rectangle_cache);
194 ret->p = picture;
195 ret->radius = corner_radius;
196 return ret;
197 }
198
199 static void
200 xrender_release_rounded_corner_cache(backend_t *base,
201 struct xrender_rounded_rectangle_cache *cache) {
202 if (!cache) {
203 return;
204 }
205
206 x_free_picture(base->c, cache->p);
207 free(cache);
208 }
209
210 static inline void xrender_set_picture_repeat(struct xrender_data *xd,
211 xcb_render_picture_t pict, uint32_t repeat) {
212 xcb_render_change_picture_value_list_t values = {
213 .repeat = repeat,
214 };
215 x_set_error_action_abort(
216 xd->base.c, xcb_render_change_picture(xd->base.c->c, pict, XCB_RENDER_CP_REPEAT,
217 (uint32_t *)&values));
218 }
219
220 static inline void xrender_record_back_damage(struct xrender_data *xd,
221 struct xrender_image_data_inner *target,
222 const region_t *region) {
223 if (target == &xd->back_image && xd->vsync) {
224 pixman_region32_union(&xd->back_damaged, &xd->back_damaged, region);
225 }
226 }
227
228 /// Normalize a mask, applying inversion and corner radius.
229 ///
230 /// @param extent the extent covered by mask region, in mask coordinate
231 /// @param alpha_pict the picture to use for alpha mask
232 /// @param new_origin the new origin of the normalized mask picture
233 /// @param allocated whether the returned picture is newly allocated
234 static xcb_render_picture_t
235 xrender_process_mask(struct xrender_data *xd, const struct backend_mask_image *mask,
236 rect_t extent, xcb_render_picture_t alpha_pict, vec2 *new_origin,
237 bool *allocated) {
238 auto inner = (struct xrender_image_data_inner *)mask->image;
239 if (!inner) {
240 *allocated = false;
241 return alpha_pict;
242 }
243 if (!mask->inverted && mask->corner_radius == 0 && alpha_pict == XCB_NONE) {
244 *allocated = false;
245 return inner->pict;
246 }
247 auto const w_u16 = to_u16_checked(extent.x2 - extent.x1);
248 auto const h_u16 = to_u16_checked(extent.y2 - extent.y1);
249 *allocated = true;
250 *new_origin =
251 (vec2){.x = extent.x1 + mask->origin.x, .y = extent.y1 + mask->origin.y};
252 x_clear_picture_clip_region(xd->base.c, inner->pict);
253 auto ret = x_create_picture_with_pictfmt(
254 xd->base.c, extent.x2 - extent.x1, extent.y2 - extent.y1, inner->pictfmt,
255 inner->depth, XCB_RENDER_CP_REPEAT,
256 (xcb_render_create_picture_value_list_t[]){XCB_RENDER_REPEAT_NONE});
257 xrender_set_picture_repeat(xd, inner->pict, XCB_RENDER_REPEAT_NONE);
258 xcb_render_composite(xd->base.c->c, XCB_RENDER_PICT_OP_SRC, inner->pict, XCB_NONE,
259 ret, to_i16_checked(extent.x1), to_i16_checked(extent.y1), 0,
260 0, 0, 0, w_u16, h_u16);
261 if (mask->corner_radius != 0) {
262 if (inner->rounded_rectangle != NULL &&
263 inner->rounded_rectangle->radius != (int)mask->corner_radius) {
264 xrender_release_rounded_corner_cache(&xd->base,
265 inner->rounded_rectangle);
266 inner->rounded_rectangle = NULL;
267 }
268 if (inner->rounded_rectangle == NULL) {
269 inner->rounded_rectangle = xrender_make_rounded_corner_cache(
270 xd->base.c, xd->white_pixel, inner->size.width,
271 inner->size.height, (int)mask->corner_radius);
272 }
273 xcb_render_composite(xd->base.c->c, XCB_RENDER_PICT_OP_IN_REVERSE,
274 inner->rounded_rectangle->p, XCB_NONE, ret,
275 to_i16_checked(extent.x1), to_i16_checked(extent.y1),
276 0, 0, 0, 0, w_u16, h_u16);
277 }
278
279 if (mask->inverted) {
280 xcb_render_composite(xd->base.c->c, XCB_RENDER_PICT_OP_XOR, xd->white_pixel,
281 XCB_NONE, ret, 0, 0, 0, 0, 0, 0, w_u16, h_u16);
282 }
283
284 if (alpha_pict != XCB_NONE) {
285 xcb_render_composite(xd->base.c->c, XCB_RENDER_PICT_OP_IN_REVERSE, alpha_pict,
286 XCB_NONE, ret, 0, 0, 0, 0, 0, 0, w_u16, h_u16);
287 }
288
289 return ret;
290 }
291
292 static bool xrender_blit(struct backend_base *base, ivec2 origin,
293 image_handle target_handle, const struct backend_blit_args *args) {
294 auto xd = (struct xrender_data *)base;
295 auto inner = (struct xrender_image_data_inner *)args->source_image;
296 auto target = (struct xrender_image_data_inner *)target_handle;
297 bool mask_allocated = false;
298 // TODO(yshui): Unify handling of tint.alpha with other tint channels. It is like
299 // this now because it came from old opacity handling code and I didn't want to
300 // change it too much. This is awkward since alpha is applied in a separate step
301 // from tint, we have to "undo" the pre-mult alpha in tint_color.
302 auto mask_pict = xd->alpha_pict[(int)(args->tint.alpha * MAX_ALPHA)];
303 auto extent = *pixman_region32_extents(args->target_mask);
304 if (!pixman_region32_not_empty(args->target_mask)) {
305 return true;
306 }
307 int16_t mask_pict_dst_x = 0, mask_pict_dst_y = 0;
308 if (args->source_mask != NULL) {
309 vec2 mask_origin = args->source_mask->origin;
310 auto extent_to_mask = region_translate_rect(
311 extent, vec2_as(vec2_neg(vec2_add(mask_origin, ivec2_as(origin)))));
312 mask_pict = xrender_process_mask(xd, args->source_mask, extent_to_mask,
313 args->tint.alpha < 1.0 ? mask_pict : XCB_NONE,
314 &mask_origin, &mask_allocated);
315 mask_pict_dst_x = to_i16_checked(-mask_origin.x);
316 mask_pict_dst_y = to_i16_checked(-mask_origin.y);
317 }
318
319 // After this point, mask_pict and mask->region have different origins.
320
321 auto tint = color_mult_alpha(args->tint, 1 / args->tint.alpha);
322 bool has_alpha = inner->has_alpha || args->tint.alpha != 1;
323 bool tint_is_one = tint.red == 1 && tint.green == 1 && tint.blue == 1,
324 tint_is_zero = tint.red == 0 && tint.green == 0 && tint.blue == 0;
325 auto const tmpw = to_u16_checked(inner->size.width);
326 auto const tmph = to_u16_checked(inner->size.height);
327 auto const tmpew = to_u16_saturated(args->effective_size.width * args->scale.x);
328 auto const tmpeh = to_u16_saturated(args->effective_size.height * args->scale.y);
329 const xcb_render_color_t tint_color = {
330 .red = (uint16_t)(tint.red * 0xffff),
331 .green = (uint16_t)(tint.green * 0xffff),
332 .blue = (uint16_t)(tint.blue * 0xffff),
333 .alpha = (uint16_t)0xffff,
334 };
335
336 // Clip region of rendered_pict might be set during rendering, clear it to
337 // make sure we get everything into the buffer
338 x_clear_picture_clip_region(xd->base.c, inner->pict);
339 xrender_set_picture_repeat(xd, inner->pict, XCB_RENDER_REPEAT_NORMAL);
340
341 x_set_picture_clip_region(xd->base.c, target->pict, 0, 0, args->target_mask);
342 if (args->corner_radius != 0) {
343 if (inner->rounded_rectangle != NULL &&
344 inner->rounded_rectangle->radius != (int)args->corner_radius) {
345 xrender_release_rounded_corner_cache(&xd->base,
346 inner->rounded_rectangle);
347 inner->rounded_rectangle = NULL;
348 }
349 if (inner->rounded_rectangle == NULL) {
350 inner->rounded_rectangle = xrender_make_rounded_corner_cache(
351 xd->base.c, xd->white_pixel, inner->size.width,
352 inner->size.height, (int)args->corner_radius);
353 }
354 }
355
356 set_picture_scale(xd->base.c, mask_pict, args->scale);
357
358 if ((args->color_inverted && has_alpha) || !tint_is_one || args->corner_radius != 0) {
359 // Apply image properties using a temporary image, because the source
360 // image is transparent or will get transparent corners. Otherwise the
361 // properties can be applied directly on the target image.
362 // Determine pictfmt for the tempoeray image is complicated, see below.
363 xcb_render_pictformat_t pictfmt;
364 uint8_t depth;
365 bool needs_color =
366 // first of all, if target format is mask, we can discard color.
367 target->format != BACKEND_IMAGE_FORMAT_MASK &&
368 // otherwise, either...
369 (
370 // the source image has color...
371 inner->format != BACKEND_IMAGE_FORMAT_MASK ||
372 // or the source image doesn't have color, but tint will give it
373 // color.
374 !tint_is_zero);
375 bool needs_alpha =
376 // mask always needs alpha, it's the only thing it has.
377 target->format == BACKEND_IMAGE_FORMAT_MASK ||
378 // if source has alpha, we need to keep it.
379 inner->has_alpha ||
380 // source doesn't have alpha, but rounding its corner makes the
381 // corner transparent, so we need to add alpha.
382 args->corner_radius != 0 ||
383 // a < 1 alpha tint will also introduce alpha.
384 args->tint.alpha != 1;
385
386 if (needs_alpha && needs_color) {
387 pictfmt = x_get_pictfmt_for_standard(xd->base.c,
388 XCB_PICT_STANDARD_ARGB_32);
389 depth = 32;
390 } else if (needs_color) {
391 pictfmt =
392 x_get_pictfmt_for_standard(xd->base.c, XCB_PICT_STANDARD_RGB_24);
393 depth = 24;
394 } else {
395 assert(needs_alpha);
396 pictfmt =
397 x_get_pictfmt_for_standard(xd->base.c, XCB_PICT_STANDARD_A_8);
398 depth = 8;
399 }
400 xcb_render_fill_rectangles(
401 xd->base.c->c, XCB_RENDER_PICT_OP_SRC, xd->tint_pixel, tint_color, 1,
402 (xcb_rectangle_t[]){{.x = 0, .y = 0, .width = 1, .height = 1}});
403
404 auto tmp_pict = x_create_picture_with_pictfmt(
405 xd->base.c, inner->size.width, inner->size.height, pictfmt, depth, 0, NULL);
406
407 vec2 inverse_scale = (vec2){
408 .x = 1.0 / args->scale.x,
409 .y = 1.0 / args->scale.y,
410 };
411 if (vec2_eq(args->scale, SCALE_IDENTITY)) {
412 x_set_picture_clip_region(
413 xd->base.c, tmp_pict, to_i16_checked(-origin.x),
414 to_i16_checked(-origin.y), args->target_mask);
415 } else {
416 // We need to scale the target_mask back so it's in the source's
417 // coordinate space.
418 scoped_region_t source_mask_region;
419 pixman_region32_init(&source_mask_region);
420 pixman_region32_copy(&source_mask_region, args->target_mask);
421 region_scale(&source_mask_region, origin, inverse_scale);
422 x_set_picture_clip_region(
423 xd->base.c, tmp_pict, to_i16_checked(-origin.x),
424 to_i16_checked(-origin.y), &source_mask_region);
425 }
426
427 auto tint_source = inner->pict;
428 if (args->color_inverted) {
429 auto tmp_pict2 = x_create_picture_with_pictfmt(
430 xd->base.c, tmpw, tmph, inner->pictfmt, inner->depth, 0, NULL);
431 xcb_render_composite(xd->base.c->c, XCB_RENDER_PICT_OP_SRC,
432 inner->pict, XCB_NONE, tmp_pict2, 0, 0, 0, 0,
433 0, 0, tmpw, tmph);
434 tint_source = tmp_pict2;
435 if (inner->has_alpha) {
436 auto tmp_pict3 = x_create_picture_with_pictfmt(
437 xd->base.c, tmpw, tmph, inner->pictfmt, inner->depth,
438 0, NULL);
439 xcb_render_composite(xd->base.c->c, XCB_RENDER_PICT_OP_SRC,
440 tmp_pict2, XCB_NONE, tmp_pict3, 0, 0,
441 0, 0, 0, 0, tmpw, tmph);
442
443 xcb_render_composite(xd->base.c->c,
444 XCB_RENDER_PICT_OP_DIFFERENCE,
445 xd->white_pixel, XCB_NONE, tmp_pict2,
446 0, 0, 0, 0, 0, 0, tmpw, tmph);
447 xcb_render_composite(
448 xd->base.c->c, XCB_RENDER_PICT_OP_IN_REVERSE, tmp_pict3,
449 XCB_NONE, tmp_pict2, 0, 0, 0, 0, 0, 0, tmpw, tmph);
450 x_free_picture(xd->base.c, tmp_pict3);
451 } else {
452 xcb_render_composite(xd->base.c->c,
453 XCB_RENDER_PICT_OP_DIFFERENCE,
454 xd->white_pixel, XCB_NONE, tmp_pict2,
455 0, 0, 0, 0, 0, 0, tmpw, tmph);
456 }
457 }
458
459 // Copy source -> tmp
460 if (inner->format == BACKEND_IMAGE_FORMAT_MASK) {
461 // Mask only has the alpha channel, when using it as src, all
462 // other color channels will be 0. To tint a mask correctly, we
463 // swap the role of src and mask.
464 xcb_render_composite(xd->base.c->c, XCB_RENDER_PICT_OP_SRC,
465 xd->tint_pixel, tint_source, tmp_pict, 0, 0,
466 0, 0, 0, 0, tmpw, tmph);
467 } else {
468 xcb_render_composite(xd->base.c->c, XCB_RENDER_PICT_OP_SRC,
469 tint_source, xd->tint_pixel, tmp_pict, 0, 0,
470 0, 0, 0, 0, tmpw, tmph);
471 }
472
473 if (tint_source != inner->pict) {
474 x_free_picture(xd->base.c, tint_source);
475 }
476
477 if (args->corner_radius != 0 && inner->rounded_rectangle != NULL) {
478 // Clip tmp_pict with a rounded rectangle
479 xcb_render_composite(xd->base.c->c, XCB_RENDER_PICT_OP_IN_REVERSE,
480 inner->rounded_rectangle->p, XCB_NONE,
481 tmp_pict, 0, 0, 0, 0, 0, 0, tmpw, tmph);
482 }
483
484 set_picture_scale(xd->base.c, tmp_pict, args->scale);
485 // Transformations don't affect the picture's clip region, so we need to
486 // set it again
487 x_set_picture_clip_region(xd->base.c, tmp_pict, to_i16_checked(-origin.x),
488 to_i16_checked(-origin.y), args->target_mask);
489
490 xcb_render_composite(xd->base.c->c, XCB_RENDER_PICT_OP_OVER, tmp_pict,
491 mask_pict, target->pict, 0, 0, mask_pict_dst_x,
492 mask_pict_dst_y, to_i16_checked(origin.x),
493 to_i16_checked(origin.y), tmpew, tmpeh);
494 xcb_render_free_picture(xd->base.c->c, tmp_pict);
495 } else {
496 uint8_t op = (has_alpha ? XCB_RENDER_PICT_OP_OVER : XCB_RENDER_PICT_OP_SRC);
497
498 set_picture_scale(xd->base.c, inner->pict, args->scale);
499
500 xcb_render_composite(xd->base.c->c, op, inner->pict, mask_pict,
501 target->pict, 0, 0, mask_pict_dst_x, mask_pict_dst_y,
502 to_i16_checked(origin.x), to_i16_checked(origin.y),
503 tmpew, tmpeh);
504 if (args->color_inverted) {
505 // Apply properties, if we reach here, then has_alpha == false
506 assert(!has_alpha);
507 xcb_render_composite(xd->base.c->c, XCB_RENDER_PICT_OP_DIFFERENCE,
508 xd->white_pixel, XCB_NONE, target->pict, 0,
509 0, 0, 0, to_i16_checked(origin.x),
510 to_i16_checked(origin.y), tmpew, tmpeh);
511 }
512 }
513 if (mask_allocated) {
514 x_free_picture(xd->base.c, mask_pict);
515 }
516 xrender_record_back_damage(xd, target, args->target_mask);
517 return true;
518 }
519
520 static bool
521 xrender_clear(struct backend_base *base, image_handle target_handle, struct color color) {
522 auto xd = (struct xrender_data *)base;
523 auto target = (struct xrender_image_data_inner *)target_handle;
524 xcb_render_color_t col = {
525 .red = (uint16_t)(color.red * 0xffff),
526 .green = (uint16_t)(color.green * 0xffff),
527 .blue = (uint16_t)(color.blue * 0xffff),
528 .alpha = (uint16_t)(color.alpha * 0xffff),
529 };
530 x_clear_picture_clip_region(base->c, target->pict);
531 xcb_render_fill_rectangles(
532 xd->base.c->c, XCB_RENDER_PICT_OP_SRC, target->pict, col, 1,
533 (xcb_rectangle_t[]){{.x = 0,
534 .y = 0,
535 .width = to_u16_checked(target->size.width),
536 .height = to_u16_checked(target->size.height)}});
537 if (target == &xd->back_image) {
538 pixman_region32_clear(&xd->back_damaged);
539 pixman_region32_union_rect(&xd->back_damaged, &xd->back_damaged, 0, 0,
540 (unsigned)target->size.width,
541 (unsigned)target->size.height);
542 }
543 return true;
544 }
545
546 static bool
547 xrender_copy_area(struct backend_base *base, ivec2 origin, image_handle target_handle,
548 image_handle source_handle, const region_t *region) {
549 auto xd = (struct xrender_data *)base;
550 auto source = (struct xrender_image_data_inner *)source_handle;
551 auto target = (struct xrender_image_data_inner *)target_handle;
552 auto extent = pixman_region32_extents(region);
553 x_set_picture_clip_region(base->c, source->pict, 0, 0, region);
554 x_clear_picture_clip_region(base->c, target->pict);
555 xrender_set_picture_repeat(xd, source->pict, XCB_RENDER_REPEAT_PAD);
556 xcb_render_composite(
557 base->c->c, XCB_RENDER_PICT_OP_SRC, source->pict, XCB_NONE, target->pict,
558 to_i16_checked(extent->x1), to_i16_checked(extent->y1), 0, 0,
559 to_i16_checked(origin.x + extent->x1), to_i16_checked(origin.y + extent->y1),
560 to_u16_checked(extent->x2 - extent->x1), to_u16_checked(extent->y2 - extent->y1));
561 xrender_record_back_damage(xd, target, region);
562 return true;
563 }
564
565 static bool xrender_blur(struct backend_base *base, ivec2 origin,
566 image_handle target_handle, const struct backend_blur_args *args) {
567 auto bctx = (struct xrender_blur_context *)args->blur_context;
568 auto source = (struct xrender_image_data_inner *)args->source_image;
569 auto target = (struct xrender_image_data_inner *)target_handle;
570 if (bctx->method == BLUR_METHOD_NONE) {
571 return true;
572 }
573
574 auto xd = (struct xrender_data *)base;
575 auto c = xd->base.c;
576 if (!pixman_region32_not_empty(args->target_mask)) {
577 return true;
578 }
579
580 region_t reg_op_resized =
581 resize_region(args->target_mask, bctx->resize_width, bctx->resize_height);
582
583 const pixman_box32_t *extent_resized = pixman_region32_extents(&reg_op_resized);
584 auto const height_resized = to_u16_checked(extent_resized->y2 - extent_resized->y1);
585 auto const width_resized = to_u16_checked(extent_resized->x2 - extent_resized->x1);
586 static const char *filter0 = "Nearest"; // The "null" filter
587 static const char *filter = "convolution";
588
589 // Create a buffer for storing blurred picture, make it just big enough
590 // for the blur region
591 const uint32_t pic_attrs_mask = XCB_RENDER_CP_REPEAT;
592 const xcb_render_create_picture_value_list_t pic_attrs = {.repeat = XCB_RENDER_REPEAT_PAD};
593 xcb_render_picture_t tmp_picture[2] = {
594 x_create_picture_with_pictfmt(xd->base.c, width_resized, height_resized,
595 source->pictfmt, source->depth, pic_attrs_mask,
596 &pic_attrs),
597 x_create_picture_with_pictfmt(xd->base.c, width_resized, height_resized,
598 source->pictfmt, source->depth, pic_attrs_mask,
599 &pic_attrs)};
600
601 if (!tmp_picture[0] || !tmp_picture[1]) {
602 log_error("Failed to build intermediate Picture.");
603 pixman_region32_fini(&reg_op_resized);
604 return false;
605 }
606
607 region_t clip;
608 pixman_region32_init(&clip);
609 pixman_region32_copy(&clip, &reg_op_resized);
610 pixman_region32_translate(&clip, -extent_resized->x1, -extent_resized->y1);
611 x_set_picture_clip_region(c, tmp_picture[0], 0, 0, &clip);
612 x_set_picture_clip_region(c, tmp_picture[1], 0, 0, &clip);
613 pixman_region32_fini(&clip);
614
615 xcb_render_picture_t src_pict = source->pict;
616 auto mask_pict = xd->alpha_pict[(int)(args->opacity * MAX_ALPHA)];
617 bool mask_allocated = false;
618 vec2 mask_pict_origin = {};
619 if (args->source_mask != NULL) {
620 // Translate the target mask region to the mask's coordinate
621 auto mask_extent = *pixman_region32_extents(args->target_mask);
622 mask_extent = region_translate_rect(
623 mask_extent, vec2_as(vec2_neg(args->source_mask->origin)));
624 mask_pict_origin = args->source_mask->origin;
625 mask_pict = xrender_process_mask(xd, args->source_mask, mask_extent,
626 args->opacity != 1.0 ? mask_pict : XCB_NONE,
627 &mask_pict_origin, &mask_allocated);
628 set_picture_scale(xd->base.c, mask_pict, args->source_mask_scale);
629 mask_pict_origin.x -= extent_resized->x1;
630 mask_pict_origin.y -= extent_resized->y1;
631 }
632 x_set_picture_clip_region(c, src_pict, 0, 0, &reg_op_resized);
633 x_set_picture_clip_region(c, target->pict, 0, 0, args->target_mask);
634
635 // For more than 1 pass, we do:
636 // source -(pass 1)-> tmp0 -(pass 2)-> tmp1 ...
637 // -(pass n)-> tmp0 or tmp1 -(composite)-> target
638 // For 1 pass, we do:
639 // source -(pass 1)-> tmp0 -(composite)-> target
640 xcb_render_picture_t dst_pict = tmp_picture[0];
641 ivec2 src_origin = {.x = extent_resized->x1, .y = extent_resized->y1};
642 int npasses = bctx->x_blur_kernel_count;
643 for (int i = 0; i < npasses; i++) {
644 // Copy from source picture to destination. The filter must
645 // be applied on source picture, to get the nearby pixels outside the
646 // window.
647 xcb_render_set_picture_filter(c->c, src_pict,
648 to_u16_checked(strlen(filter)), filter,
649 to_u32_checked(bctx->x_blur_kernel[i]->size),
650 bctx->x_blur_kernel[i]->kernel);
651
652 // clang-format off
653 xcb_render_composite(c->c, XCB_RENDER_PICT_OP_SRC, src_pict, XCB_NONE, dst_pict,
654 to_i16_checked(src_origin.x) , to_i16_checked(src_origin.y),
655 0 , 0 ,
656 0 , 0 ,
657 width_resized , height_resized);
658 // clang-format on
659
660 // reset filter
661 xcb_render_set_picture_filter(
662 c->c, src_pict, to_u16_checked(strlen(filter0)), filter0, 0, NULL);
663
664 auto next_tmp = src_pict == source->pict ? tmp_picture[1] : src_pict;
665 src_pict = dst_pict;
666 dst_pict = next_tmp;
667 src_origin = (ivec2){.x = 0, .y = 0};
668 }
669
670 // Finally, we composite the last pass to the target picture
671 xcb_render_composite(
672 c->c, XCB_RENDER_PICT_OP_OVER, src_pict, mask_pict, target->pict, 0, 0,
673 to_i16_checked(-mask_pict_origin.x), to_i16_checked(-mask_pict_origin.y),
674 to_i16_checked(origin.x + extent_resized->x1),
675 to_i16_checked(origin.y + extent_resized->y1), width_resized, height_resized);
676
677 if (mask_allocated) {
678 x_free_picture(c, mask_pict);
679 }
680 x_free_picture(c, tmp_picture[0]);
681 x_free_picture(c, tmp_picture[1]);
682 pixman_region32_fini(&reg_op_resized);
683
684 xrender_record_back_damage(xd, target, args->target_mask);
685 return true;
686 }
687
688 static image_handle
689 xrender_bind_pixmap(backend_t *base, xcb_pixmap_t pixmap, struct xvisual_info fmt) {
690 xcb_generic_error_t *e;
691 auto r = xcb_get_geometry_reply(base->c->c, xcb_get_geometry(base->c->c, pixmap), &e);
692 if (!r) {
693 log_error("Invalid pixmap: %#010x", pixmap);
694 x_print_error(base->c, e->full_sequence, e->major_code, e->minor_code,
695 e->error_code);
696 free(e);
697 return NULL;
698 }
699
700 auto img = ccalloc(1, struct xrender_image_data_inner);
701 img->depth = (uint8_t)fmt.visual_depth;
702 img->has_alpha = fmt.alpha_size > 0;
703 img->size = (ivec2){
704 .width = r->width,
705 .height = r->height,
706 };
707 img->format = BACKEND_IMAGE_FORMAT_PIXMAP;
708 img->pixmap = pixmap;
709 xcb_render_create_picture_value_list_t pic_attrs = {.repeat = XCB_RENDER_REPEAT_NORMAL};
710 img->pict = x_create_picture_with_visual_and_pixmap(
711 base->c, fmt.visual, pixmap, XCB_RENDER_CP_REPEAT, &pic_attrs);
712 auto pictfmt_info = x_get_pictform_for_visual(base->c, fmt.visual);
713 img->pictfmt = pictfmt_info->id;
714 assert(pictfmt_info->depth == img->depth);
715 img->is_pixmap_internal = false;
716 free(r);
717
718 if (img->pict == XCB_NONE) {
719 free(img);
720 return NULL;
721 }
722 return (image_handle)img;
723 }
724
725 static xcb_pixmap_t xrender_release_image(backend_t *base, image_handle image) {
726 auto img = (struct xrender_image_data_inner *)image;
727 auto xd = (struct xrender_data *)base;
728 if (img == &xd->back_image) {
729 return XCB_NONE;
730 }
731
732 xrender_release_rounded_corner_cache(base, img->rounded_rectangle);
733 x_free_picture(base->c, img->pict);
734 if (img->is_pixmap_internal && img->pixmap != XCB_NONE) {
735 xcb_free_pixmap(base->c->c, img->pixmap);
736 img->pixmap = XCB_NONE;
737 }
738
739 auto pixmap = img->pixmap;
740 free(img);
741 return pixmap;
742 }
743
744 static void xrender_deinit(backend_t *backend_data) {
745 auto xd = (struct xrender_data *)backend_data;
746 if (xd->shm_seg_id != 0) {
747 xcb_shm_detach(xd->base.c->c, xd->shm_seg_id);
748 }
749 if (xd->shm != NULL) {
750 munmap(xd->shm, xd->shm_size);
751 }
752 if (xd->shm_fd != -1) {
753 close(xd->shm_fd);
754 }
755 for (int i = 0; i < 256; i++) {
756 x_free_picture(xd->base.c, xd->alpha_pict[i]);
757 }
758 x_free_picture(xd->base.c, xd->target);
759 for (int i = 0; i < 2; i++) {
760 if (xd->back[i] != XCB_NONE) {
761 x_free_picture(xd->base.c, xd->back[i]);
762 }
763 if (xd->back_pixmap[i] != XCB_NONE) {
764 xcb_free_pixmap(xd->base.c->c, xd->back_pixmap[i]);
765 }
766 }
767 x_destroy_region(xd->base.c, xd->present_region);
768 if (xd->present_event) {
769 xcb_unregister_for_special_event(xd->base.c->c, xd->present_event);
770 }
771 x_free_picture(xd->base.c, xd->white_pixel);
772 x_free_picture(xd->base.c, xd->black_pixel);
773 x_free_picture(xd->base.c, xd->tint_pixel);
774 free(xd);
775 }
776
777 static bool xrender_present(struct backend_base *base) {
778 auto xd = (struct xrender_data *)base;
779 if (xd->vsync) {
780 // Make sure we got reply from PresentPixmap before waiting for events,
781 // to avoid deadlock
782 auto e = xcb_request_check(
783 base->c->c,
784 xcb_present_pixmap_checked(
785 base->c->c, xd->target_win, xd->back_pixmap[xd->curr_back], 0, XCB_NONE,
786 x_set_region(base->c, xd->present_region, &xd->back_damaged)
787 ? xd->present_region
788 : XCB_NONE,
789 0, 0, XCB_NONE, XCB_NONE, XCB_NONE, 0, 0, 0, 0, 0, NULL));
790 if (e) {
791 log_error("Failed to present pixmap");
792 free(e);
793 return false;
794 }
795 // TODO(yshui) don't block wait for present completion
796 auto pev = (xcb_present_generic_event_t *)xcb_wait_for_special_event(
797 base->c->c, xd->present_event);
798 if (!pev) {
799 // We don't know what happened, maybe X died
800 // But reset buffer age, so in case we do recover, we will
801 // render correctly.
802 xd->buffer_age[0] = xd->buffer_age[1] = -1;
803 return false;
804 }
805 assert(pev->evtype == XCB_PRESENT_COMPLETE_NOTIFY);
806 auto pcev = (xcb_present_complete_notify_event_t *)pev;
807 // log_trace("Present complete: %d %ld", pcev->mode, pcev->msc);
808 xd->buffer_age[xd->curr_back] = 1;
809
810 // buffer_age < 0 means that back buffer is empty
811 if (xd->buffer_age[1 - xd->curr_back] > 0) {
812 xd->buffer_age[1 - xd->curr_back]++;
813 }
814 if (pcev->mode == XCB_PRESENT_COMPLETE_MODE_FLIP) {
815 // We cannot use the pixmap we used anymore
816 xd->curr_back = 1 - xd->curr_back;
817 xd->back_image.pict = xd->back[xd->curr_back];
818 }
819 free(pev);
820 }
821 // Without vsync, we are rendering into the front buffer directly
822 pixman_region32_clear(&xd->back_damaged);
823 return true;
824 }
825
826 static int xrender_buffer_age(backend_t *backend_data) {
827 auto xd = (struct xrender_data *)backend_data;
828 if (!xd->vsync) {
829 // Only the target picture really holds the screen content, and its
830 // content is always up to date. So buffer age is always 1.
831 return 1;
832 }
833 return xd->buffer_age[xd->curr_back];
834 }
835
836 static bool xrender_apply_alpha(struct backend_base *base, image_handle image,
837 double alpha, const region_t *reg_op) {
838 auto xd = (struct xrender_data *)base;
839 auto img = (struct xrender_image_data_inner *)image;
840 assert(reg_op);
841
842 if (!pixman_region32_not_empty(reg_op) || alpha == 1) {
843 return true;
844 }
845
846 auto alpha_pict = xd->alpha_pict[(int)((1 - alpha) * MAX_ALPHA)];
847 x_set_picture_clip_region(base->c, img->pict, 0, 0, reg_op);
848 xcb_render_composite(base->c->c, XCB_RENDER_PICT_OP_OUT_REVERSE, alpha_pict, XCB_NONE,
849 img->pict, 0, 0, 0, 0, 0, 0, to_u16_checked(img->size.width),
850 to_u16_checked(img->size.height));
851 xrender_record_back_damage(xd, img, reg_op);
852 return true;
853 }
854
855 static void *
856 xrender_create_blur_context(backend_t *base attr_unused, enum blur_method method,
857 enum backend_image_format format attr_unused, void *args) {
858 auto ret = ccalloc(1, struct xrender_blur_context);
859 if (!method || method >= BLUR_METHOD_INVALID) {
860 ret->method = BLUR_METHOD_NONE;
861 return ret;
862 }
863 if (method == BLUR_METHOD_DUAL_KAWASE) {
864 log_warn("Blur method 'dual_kawase' is not compatible with the 'xrender' "
865 "backend.");
866 ret->method = BLUR_METHOD_NONE;
867 return ret;
868 }
869
870 ret->method = BLUR_METHOD_KERNEL;
871 struct conv **kernels;
872 int kernel_count;
873 if (method == BLUR_METHOD_KERNEL) {
874 kernels = ((struct kernel_blur_args *)args)->kernels;
875 kernel_count = ((struct kernel_blur_args *)args)->kernel_count;
876 } else {
877 kernels = generate_blur_kernel(method, args, &kernel_count);
878 }
879
880 ret->x_blur_kernel = ccalloc(kernel_count, struct x_convolution_kernel *);
881 for (int i = 0; i < kernel_count; i++) {
882 int center = kernels[i]->h * kernels[i]->w / 2;
883 x_create_convolution_kernel(kernels[i], kernels[i]->data[center],
884 &ret->x_blur_kernel[i]);
885 ret->resize_width += kernels[i]->w / 2;
886 ret->resize_height += kernels[i]->h / 2;
887 }
888 ret->x_blur_kernel_count = kernel_count;
889
890 if (method != BLUR_METHOD_KERNEL) {
891 // Kernels generated by generate_blur_kernel, so we need to free them.
892 for (int i = 0; i < kernel_count; i++) {
893 free(kernels[i]);
894 }
895 free(kernels);
896 }
897 return ret;
898 }
899
900 static void xrender_destroy_blur_context(backend_t *base attr_unused, void *ctx_) {
901 struct xrender_blur_context *ctx = ctx_;
902 for (int i = 0; i < ctx->x_blur_kernel_count; i++) {
903 free(ctx->x_blur_kernel[i]);
904 }
905 free(ctx->x_blur_kernel);
906 free(ctx);
907 }
908
909 static void xrender_get_blur_size(void *blur_context, int *width, int *height) {
910 struct xrender_blur_context *ctx = blur_context;
911 *width = ctx->resize_width;
912 *height = ctx->resize_height;
913 }
914 static const uint32_t initial_shm_size = 16 * 1024 * 1024;
915 static bool ensure_xshm(struct xrender_data *xd, uint32_t size) {
916 if (size <= xd->shm_size) {
917 return true;
918 }
919
920 if (xd->shm_seg_id == XCB_NONE) {
921 assert(xd->shm_fd == -1);
922 assert(xd->shm == NULL);
923 assert(xd->shm_size == 0);
924 }
925
926 auto shm_seg_id = x_new_id(xd->base.c);
927 auto seg = XCB_AWAIT(xcb_shm_create_segment, xd->base.c, shm_seg_id, size, true);
928 if (!seg) {
929 log_error("Failed to create SHM segment.");
930 return false;
931 }
932 assert(seg->nfd == 1);
933 int shm_fd = *xcb_shm_create_segment_reply_fds(xd->base.c->c, seg);
934 free(seg);
935
936 void *shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
937 if (shm == MAP_FAILED) {
938 log_error("Failed to map SHM segment.");
939 xcb_shm_detach(xd->base.c->c, shm_seg_id);
940 close(shm_fd);
941 return false;
942 }
943
944 if (xd->shm_seg_id != XCB_NONE) {
945 munmap(xd->shm, xd->shm_size);
946 close(xd->shm_fd);
947 xcb_shm_detach(xd->base.c->c, xd->shm_seg_id);
948 }
949
950 xd->shm_seg_id = shm_seg_id;
951 xd->shm_fd = shm_fd;
952 xd->shm = shm;
953 xd->shm_size = size;
954 return true;
955 }
956 const struct backend_operations xrender_ops;
957 static backend_t *xrender_init(session_t *ps, xcb_window_t target) {
958 if (ps->o.dithered_present) {
959 log_warn("\"dithered-present\" is not supported by the xrender backend, "
960 "it will be ignored.");
961 }
962 if (ps->o.max_brightness < 1.0) {
963 log_warn("\"max-brightness\" is not supported by the xrender backend, it "
964 "will be ignored.");
965 }
966
967 auto xd = ccalloc(1, struct xrender_data);
968 init_backend_base(&xd->base, ps);
969 xd->base.ops = xrender_ops;
970 xd->shm_fd = -1;
971
972 if (!ensure_xshm(xd, initial_shm_size)) {
973 free(xd);
974 return NULL;
975 }
976
977 for (int i = 0; i <= MAX_ALPHA; ++i) {
978 double o = (double)i / (double)MAX_ALPHA;
979 xd->alpha_pict[i] = solid_picture(&ps->c, false, o, 0, 0, 0);
980 assert(xd->alpha_pict[i] != XCB_NONE);
981 }
982
983 auto root_pictfmt = x_get_pictform_for_visual(&ps->c, ps->c.screen_info->root_visual);
984 assert(root_pictfmt->depth == ps->c.screen_info->root_depth);
985 xd->back_image = (struct xrender_image_data_inner){
986 .pictfmt = root_pictfmt->id,
987 .depth = ps->c.screen_info->root_depth,
988 .has_alpha = false,
989 .format = BACKEND_IMAGE_FORMAT_PIXMAP,
990 .size = {.width = ps->root_width, .height = ps->root_height},
991 };
992 xd->black_pixel = solid_picture(&ps->c, true, 1, 0, 0, 0);
993 xd->white_pixel = solid_picture(&ps->c, true, 1, 1, 1, 1);
994
995 struct xcb_render_create_picture_value_list_t values = {
996 .repeat = XCB_RENDER_REPEAT_NORMAL,
997 .componentalpha = 1,
998 };
999 xd->tint_pixel = x_create_picture_with_pictfmt(
1000 xd->base.c, 1, 1, x_get_pictfmt_for_standard(&ps->c, XCB_PICT_STANDARD_ARGB_32),
1001 32, XCB_RENDER_CP_REPEAT | XCB_RENDER_CP_COMPONENT_ALPHA, &values);
1002
1003 xd->target_win = target;
1004 xcb_render_create_picture_value_list_t pa = {
1005 .subwindowmode = XCB_SUBWINDOW_MODE_INCLUDE_INFERIORS,
1006 };
1007 xd->target = x_create_picture_with_visual_and_pixmap(
1008 &ps->c, ps->c.screen_info->root_visual, xd->target_win,
1009 XCB_RENDER_CP_SUBWINDOW_MODE, &pa);
1010
1011 xd->vsync = ps->o.vsync;
1012 if (ps->c.e.has_present) {
1013 auto eid = x_new_id(&ps->c);
1014 auto e =
1015 xcb_request_check(ps->c.c, xcb_present_select_input_checked(
1016 ps->c.c, eid, xd->target_win,
1017 XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY));
1018 if (e) {
1019 log_error("Cannot select present input, vsync will be disabled");
1020 xd->vsync = false;
1021 free(e);
1022 }
1023
1024 xd->present_event =
1025 xcb_register_for_special_xge(ps->c.c, &xcb_present_id, eid, NULL);
1026 if (!xd->present_event) {
1027 log_error("Cannot register for special XGE, vsync will be "
1028 "disabled");
1029 xd->vsync = false;
1030 }
1031 } else {
1032 xd->vsync = false;
1033 }
1034
1035 if (xd->vsync) {
1036 xd->present_region = x_create_region(&ps->c, &ps->screen_reg);
1037 }
1038
1039 // We might need to do double buffering for vsync, and buffer 0 and 1 are for
1040 // double buffering.
1041 int buffer_count = xd->vsync ? 2 : 0;
1042 for (int i = 0; i < buffer_count; i++) {
1043 xd->back_pixmap[i] = x_create_pixmap(&ps->c, ps->c.screen_info->root_depth,
1044 to_u16_checked(ps->root_width),
1045 to_u16_checked(ps->root_height));
1046 const uint32_t pic_attrs_mask = XCB_RENDER_CP_REPEAT;
1047 const xcb_render_create_picture_value_list_t pic_attrs = {
1048 .repeat = XCB_RENDER_REPEAT_PAD};
1049 xd->back[i] = x_create_picture_with_visual_and_pixmap(
1050 &ps->c, ps->c.screen_info->root_visual, xd->back_pixmap[i],
1051 pic_attrs_mask, &pic_attrs);
1052 xd->buffer_age[i] = -1;
1053 if (xd->back_pixmap[i] == XCB_NONE || xd->back[i] == XCB_NONE) {
1054 log_error("Cannot create pixmap for rendering");
1055 goto err;
1056 }
1057 }
1058 xd->curr_back = 0;
1059 xd->back_image.pict = xd->vsync ? xd->back[xd->curr_back] : xd->target;
1060
1061 xd->quirks |= BACKEND_QUIRK_SLOW_BLUR;
1062
1063 return &xd->base;
1064 err:
1065 xrender_deinit(&xd->base);
1066 return NULL;
1067 }
1068
1069 static image_handle
1070 xrender_new_image(struct backend_base *base, enum backend_image_format format, ivec2 size) {
1071 auto xd = (struct xrender_data *)base;
1072 auto img = ccalloc(1, struct xrender_image_data_inner);
1073 img->format = format;
1074 img->size = size;
1075 img->has_alpha = true;
1076 if (format == BACKEND_IMAGE_FORMAT_MASK) {
1077 img->depth = 8;
1078 img->pictfmt = x_get_pictfmt_for_standard(base->c, XCB_PICT_STANDARD_A_8);
1079 } else {
1080 img->depth = 32;
1081 img->pictfmt =
1082 x_get_pictfmt_for_standard(xd->base.c, XCB_PICT_STANDARD_ARGB_32);
1083 }
1084 img->pixmap = x_create_pixmap(xd->base.c, img->depth, to_u16_checked(size.width),
1085 to_u16_checked(size.height));
1086 if (img->pixmap == XCB_NONE) {
1087 free(img);
1088 return NULL;
1089 }
1090 img->pict = x_create_picture_with_pictfmt_and_pixmap(xd->base.c, img->pictfmt,
1091 img->pixmap, 0, NULL);
1092 if (img->pict == XCB_NONE) {
1093 xcb_free_pixmap(xd->base.c->c, img->pixmap);
1094 free(img);
1095 return NULL;
1096 }
1097 img->is_pixmap_internal = true;
1098 return (image_handle)img;
1099 }
1100
1101 static image_handle
1102 xrender_new_image_from_pixels(struct backend_base *base, enum backend_image_format format,
1103 ivec2 size, int stride, const uint8_t *pixels) {
1104 auto xd = (struct xrender_data *)base;
1105 assert(format == BACKEND_IMAGE_FORMAT_MASK || format == BACKEND_IMAGE_FORMAT_PIXMAP);
1106
1107 size_t bpp = format == BACKEND_IMAGE_FORMAT_MASK ? 1 : 4;
1108 size_t bytes = (size_t)stride * (size_t)size.height * bpp;
1109 if (bytes > UINT32_MAX || size.width > UINT16_MAX || size.height > UINT16_MAX) {
1110 log_error("Image is too big");
1111 return NULL;
1112 }
1113 if (!ensure_xshm(xd, (uint32_t)bytes)) {
1114 return NULL;
1115 }
1116
1117 image_handle ret = xrender_new_image(base, format, size);
1118 if (ret == NULL) {
1119 return ret;
1120 }
1121
1122 memcpy(xd->shm, pixels, bytes);
1123
1124 auto img = (struct xrender_image_data_inner *)ret;
1125
1126 xcb_gcontext_t gc = x_new_id(base->c);
1127 xcb_create_gc(base->c->c, gc, img->pixmap, 0, NULL);
1128 bool success = XCB_AWAIT_VOID(
1129 xcb_shm_put_image, base->c, img->pixmap, gc, (uint16_t)size.width,
1130 (uint16_t)size.height, 0, 0, (uint16_t)size.width, (uint16_t)size.height, 0,
1131 0, (uint8_t)bpp * 8, XCB_IMAGE_FORMAT_Z_PIXMAP, 0, xd->shm_seg_id, 0);
1132 xcb_free_gc(base->c->c, gc);
1133
1134 if (!success) {
1135 xrender_release_image(base, ret);
1136 return NULL;
1137 }
1138 return ret;
1139 }
1140
1141 static uint32_t xrender_image_capabilities(struct backend_base *base attr_unused,
1142 image_handle image attr_unused) {
1143 // All of xrender's picture can be used as both a source and a destination.
1144 return BACKEND_IMAGE_CAP_DST | BACKEND_IMAGE_CAP_SRC;
1145 }
1146
1147 static bool xrender_is_format_supported(struct backend_base *base attr_unused,
1148 enum backend_image_format format) {
1149 return format == BACKEND_IMAGE_FORMAT_MASK || format == BACKEND_IMAGE_FORMAT_PIXMAP;
1150 }
1151
1152 static image_handle xrender_back_buffer(struct backend_base *base) {
1153 auto xd = (struct xrender_data *)base;
1154 return (image_handle)&xd->back_image;
1155 }
1156
1157 uint32_t xrender_quirks(struct backend_base *base) {
1158 return ((struct xrender_data *)base)->quirks;
1159 }
1160
1161 static int xrender_max_buffer_age(struct backend_base *base) {
1162 return ((struct xrender_data *)base)->vsync ? 2 : 1;
1163 }
1164
1165 #define PICOM_BACKEND_XRENDER_MAJOR (0UL)
1166 #define PICOM_BACKEND_XRENDER_MINOR (1UL)
1167
1168 static void xrender_version(struct backend_base * /*base*/, uint64_t *major, uint64_t *minor) {
1169 *major = PICOM_BACKEND_XRENDER_MAJOR;
1170 *minor = PICOM_BACKEND_XRENDER_MINOR;
1171 }
1172
1173 const struct backend_operations xrender_ops = {
1174 .apply_alpha = xrender_apply_alpha,
1175 .back_buffer = xrender_back_buffer,
1176 .bind_pixmap = xrender_bind_pixmap,
1177 .blit = xrender_blit,
1178 .blur = xrender_blur,
1179 .clear = xrender_clear,
1180 .copy_area = xrender_copy_area,
1181 .copy_area_quantize = xrender_copy_area,
1182 .image_capabilities = xrender_image_capabilities,
1183 .is_format_supported = xrender_is_format_supported,
1184 .new_image = xrender_new_image,
1185 .new_image_from_pixels = xrender_new_image_from_pixels,
1186 .present = xrender_present,
1187 .quirks = xrender_quirks,
1188 .version = xrender_version,
1189 .release_image = xrender_release_image,
1190
1191 .init = xrender_init,
1192 .deinit = xrender_deinit,
1193 // TODO(yshui) make blur faster so we can use `backend_render_shadow_from_mask` for
1194 // `render_shadow`, and `backend_compat_shadow_from_mask` for
1195 // `shadow_from_mask`
1196 .buffer_age = xrender_buffer_age,
1197 .max_buffer_age = xrender_max_buffer_age,
1198 .create_blur_context = xrender_create_blur_context,
1199 .destroy_blur_context = xrender_destroy_blur_context,
1200 .get_blur_size = xrender_get_blur_size
1201 // end
1202 };
1203
1204 BACKEND_ENTRYPOINT(xrender_register) {
1205 if (!backend_register(PICOM_BACKEND_MAJOR, PICOM_BACKEND_MINOR, "xrender",
1206 xrender_ops.init, true)) {
1207 log_error("Failed to register xrender backend");
1208 }
1209 }
1210
1211 // vim: set noet sw=8 ts=8: