From a93fc8afd60646243ee635b443fc2a17e2d97a18 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Tue, 2 May 2023 21:32:51 +0200 Subject: render: introduce blend mode Allow callers to pick the blend mode when rendering a rect. The "none" mode can be used to disable blending and clear rects. --- include/wlr/render/wlr_renderer.h | 12 ++++++++++++ render/pass.c | 11 ++++++++++- render/pixman/pass.c | 13 ++++++++++++- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/include/wlr/render/wlr_renderer.h b/include/wlr/render/wlr_renderer.h index f6b58df5..33668067 100644 --- a/include/wlr/render/wlr_renderer.h +++ b/include/wlr/render/wlr_renderer.h @@ -177,6 +177,16 @@ struct wlr_render_pass *wlr_renderer_begin_buffer_pass( */ bool wlr_render_pass_submit(struct wlr_render_pass *render_pass); +/** + * Blend modes. + */ +enum wlr_render_blend_mode { + /* Pre-multiplied alpha (default) */ + WLR_RENDER_BLEND_MODE_PREMULTIPLIED, + /* Blending is disabled */ + WLR_RENDER_BLEND_MODE_NONE, +}; + struct wlr_render_texture_options { /* Source texture */ struct wlr_texture *texture; @@ -215,6 +225,8 @@ struct wlr_render_rect_options { struct wlr_render_color color; /* Clip region, leave NULL to disable clipping */ const pixman_region32_t *clip; + /* Blend mode */ + enum wlr_render_blend_mode blend_mode; }; /** diff --git a/render/pass.c b/render/pass.c index 17c5d247..026e807b 100644 --- a/render/pass.c +++ b/render/pass.c @@ -134,6 +134,8 @@ static void legacy_add_rect(struct wlr_render_pass *wlr_pass, pixman_region32_t clip; get_clip_region(pass, options->clip, &clip); + pixman_region32_intersect_rect(&clip, &clip, + options->box.x, options->box.y, options->box.width, options->box.height); float color[4] = { options->color.r, @@ -146,7 +148,14 @@ static void legacy_add_rect(struct wlr_render_pass *wlr_pass, const pixman_box32_t *rects = pixman_region32_rectangles(&clip, &rects_len); for (int i = 0; i < rects_len; i++) { scissor(pass->renderer, &rects[i]); - wlr_render_quad_with_matrix(pass->renderer, color, matrix); + switch (options->blend_mode) { + case WLR_RENDER_BLEND_MODE_PREMULTIPLIED: + wlr_render_quad_with_matrix(pass->renderer, color, matrix); + break; + case WLR_RENDER_BLEND_MODE_NONE: + wlr_renderer_clear(pass->renderer, color); + break; + } } wlr_renderer_scissor(pass->renderer, NULL); diff --git a/render/pixman/pass.c b/render/pixman/pass.c index f6e7db0d..3f0927ee 100644 --- a/render/pixman/pass.c +++ b/render/pixman/pass.c @@ -143,6 +143,17 @@ static void render_pass_add_rect(struct wlr_render_pass *wlr_pass, struct wlr_pixman_buffer *buffer = pass->buffer; struct wlr_box box = options->box; + pixman_op_t op = 0; + switch (options->blend_mode) { + case WLR_RENDER_BLEND_MODE_PREMULTIPLIED: + op = options->color.a == 1 ? PIXMAN_OP_SRC : PIXMAN_OP_OVER; + break; + case WLR_RENDER_BLEND_MODE_NONE: + op = PIXMAN_OP_SRC; + break; + } + assert(op != 0); + struct pixman_color color = { .red = options->color.r * 0xFFFF, .green = options->color.g * 0xFFFF, @@ -153,7 +164,7 @@ static void render_pass_add_rect(struct wlr_render_pass *wlr_pass, pixman_image_t *fill = pixman_image_create_solid_fill(&color); pixman_image_set_clip_region32(buffer->image, (pixman_region32_t *)options->clip); - pixman_image_composite32(PIXMAN_OP_OVER, fill, NULL, buffer->image, + pixman_image_composite32(op, fill, NULL, buffer->image, 0, 0, 0, 0, box.x, box.y, box.width, box.height); pixman_image_set_clip_region32(buffer->image, NULL); -- cgit v1.2.3