Skip to content

Commit

Permalink
backend: store dither present option in blur context
Browse files Browse the repository at this point in the history
Signed-off-by: Yuxuan Shui <[email protected]>
  • Loading branch information
yshui committed Apr 12, 2024
1 parent a14a23d commit a8702b7
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 24 deletions.
6 changes: 4 additions & 2 deletions src/backend/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -599,8 +599,10 @@ struct backend_operations {
image_handle (*clone_image)(backend_t *base, image_handle image,
const region_t *reg_visible) attr_nonnull_all;

/// Create a blur context that can be used to call `blur`
void *(*create_blur_context)(backend_t *base, enum blur_method, void *args);
/// Create a blur context that can be used to call `blur` for images with a
/// specific format.
void *(*create_blur_context)(backend_t *base, enum blur_method,
enum backend_image_format format, void *args);
/// Destroy a blur context
void (*destroy_blur_context)(backend_t *base, void *ctx);
/// Get how many pixels outside of the blur area is needed for blur
Expand Down
4 changes: 3 additions & 1 deletion src/backend/dummy/dummy.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ image_handle dummy_clone_image(struct backend_base *base, image_handle image,
}

void *dummy_create_blur_context(struct backend_base *base attr_unused,
enum blur_method method attr_unused, void *args attr_unused) {
enum blur_method method attr_unused,
enum backend_image_format format attr_unused,
void *args attr_unused) {
static int dummy_context;
return &dummy_context;
}
Expand Down
36 changes: 23 additions & 13 deletions src/backend/gl/blur.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ struct gl_blur_context {
int resize_width, resize_height;

int npasses;

enum backend_image_format format;
};

/**
Expand Down Expand Up @@ -256,13 +258,8 @@ bool gl_dual_kawase_blur(double opacity, struct gl_blur_context *bctx, const rec
return true;
}

bool gl_blur_impl(double opacity, struct gl_blur_context *bctx,
struct backend_image *mask, coord_t mask_dst, const region_t *reg_blur,
const region_t *reg_visible attr_unused, GLuint source_texture,
geometry_t source_size, GLuint target_fbo, GLuint default_mask,
bool high_precision) {
bool ret = false;

static bool gl_blur_context_preallocate_textures(struct gl_blur_context *bctx,
struct geometry source_size) {
if (source_size.width != bctx->fb_width || source_size.height != bctx->fb_height) {
// Resize the temporary textures used for blur in case the root
// size changed
Expand All @@ -282,10 +279,9 @@ bool gl_blur_impl(double opacity, struct gl_blur_context *bctx,
}

glBindTexture(GL_TEXTURE_2D, bctx->blur_textures[i]);
GLint format = GL_RGBA8;
if (high_precision) {
format = GL_RGBA16;
}
GLint format = bctx->format == BACKEND_IMAGE_FORMAT_PIXMAP_HIGH
? GL_RGBA16
: GL_RGBA8;
glTexImage2D(GL_TEXTURE_2D, 0, format, tex_size->width,
tex_size->height, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);

Expand All @@ -303,6 +299,14 @@ bool gl_blur_impl(double opacity, struct gl_blur_context *bctx,
}
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
}
return true;
}

bool gl_blur_impl(double opacity, struct gl_blur_context *bctx,
struct backend_image *mask, coord_t mask_dst, const region_t *reg_blur,
const region_t *reg_visible attr_unused, GLuint source_texture,
geometry_t source_size, GLuint target_fbo, GLuint default_mask) {
bool ret = false;

// Remainder: regions are in Xorg coordinates
auto reg_blur_resized =
Expand All @@ -322,6 +326,10 @@ bool gl_blur_impl(double opacity, struct gl_blur_context *bctx,
return true;
}

if (!gl_blur_context_preallocate_textures(bctx, source_size)) {
return false;
}

auto coord = ccalloc(nrects * 16, GLint);
auto indices = ccalloc(nrects * 6, GLuint);
auto extent_height = extent_resized->y2 - extent_resized->y1;
Expand Down Expand Up @@ -408,7 +416,7 @@ bool gl_blur(backend_t *base, double opacity, void *ctx, image_handle mask, coor
return gl_blur_impl(opacity, bctx, (struct backend_image *)mask, mask_dst,
reg_blur, reg_visible, gd->back_texture,
(geometry_t){.width = gd->width, .height = gd->height},
gd->back_fbo, gd->default_mask_texture, gd->dithered_present);
gd->back_fbo, gd->default_mask_texture);
}

static inline void gl_free_blur_shader(gl_blur_shader_t *shader) {
Expand Down Expand Up @@ -828,7 +836,8 @@ bool gl_create_dual_kawase_blur_context(void *blur_context, GLfloat *projection,
return success;
}

void *gl_create_blur_context(backend_t *base, enum blur_method method, void *args) {
void *gl_create_blur_context(backend_t *base, enum blur_method method,
enum backend_image_format format, void *args) {
bool success;
auto gd = (struct gl_data *)base;

Expand Down Expand Up @@ -863,6 +872,7 @@ void *gl_create_blur_context(backend_t *base, enum blur_method method, void *arg
// Texture size will be defined by gl_blur
ctx->blur_textures = ccalloc(ctx->blur_texture_count, GLuint);
ctx->texture_sizes = ccalloc(ctx->blur_texture_count, struct texture_size);
ctx->format = format;
glGenTextures(ctx->blur_texture_count, ctx->blur_textures);

for (int i = 0; i < ctx->blur_texture_count; ++i) {
Expand Down
5 changes: 3 additions & 2 deletions src/backend/gl/gl_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -1261,7 +1261,8 @@ struct backend_shadow_context *gl_create_shadow_context(backend_t *base, double
.size = (int)radius,
.deviation = gaussian_kernel_std_for_size(radius, 0.5 / 256.0),
};
ctx->blur_context = gl_create_blur_context(base, BLUR_METHOD_GAUSSIAN, &args);
ctx->blur_context = gl_create_blur_context(
base, BLUR_METHOD_GAUSSIAN, BACKEND_IMAGE_FORMAT_MASK, &args);
if (!ctx->blur_context) {
log_error("Failed to create shadow context");
free(ctx);
Expand Down Expand Up @@ -1360,7 +1361,7 @@ image_handle gl_shadow_from_mask(backend_t *base, image_handle mask_,
1.0, gsctx->blur_context, NULL, (coord_t){0}, &reg_blur, NULL,
source_texture,
(geometry_t){.width = new_inner->width, .height = new_inner->height},
fbo, gd->default_mask_texture, gd->dithered_present);
fbo, gd->default_mask_texture);
pixman_region32_fini(&reg_blur);
}

Expand Down
6 changes: 3 additions & 3 deletions src/backend/gl/gl_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ bool gl_blur(backend_t *base, double opacity, void *ctx, image_handle mask,
bool gl_blur_impl(double opacity, struct gl_blur_context *bctx,
struct backend_image *mask, coord_t mask_dst, const region_t *reg_blur,
const region_t *reg_visible attr_unused, GLuint source_texture,
geometry_t source_size, GLuint target_fbo, GLuint default_mask,
bool high_precision);
void *gl_create_blur_context(backend_t *base, enum blur_method, void *args);
geometry_t source_size, GLuint target_fbo, GLuint default_mask);
void *gl_create_blur_context(backend_t *base, enum blur_method,
enum backend_image_format format, void *args);
void gl_destroy_blur_context(backend_t *base, void *ctx);
struct backend_shadow_context *gl_create_shadow_context(backend_t *base, double radius);
void gl_destroy_shadow_context(backend_t *base attr_unused, struct backend_shadow_context *ctx);
Expand Down
6 changes: 4 additions & 2 deletions src/backend/xrender/xrender.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "backend/backend.h"
#include "backend/backend_common.h"
#include "common.h"
#include "compiler.h"
#include "config.h"
#include "kernel.h"
#include "log.h"
Expand Down Expand Up @@ -831,8 +832,9 @@ static bool xrender_image_op(backend_t *base, enum image_operations op, image_ha
return true;
}

static void *xrender_create_blur_context(backend_t *base attr_unused,
enum blur_method method, void *args) {
static void *
xrender_create_blur_context(backend_t *base attr_unused, enum blur_method method,
enum backend_image_format format attr_unused, void *args) {
auto ret = ccalloc(1, struct xrender_blur_context);
if (!method || method >= BLUR_METHOD_INVALID) {
ret->method = BLUR_METHOD_NONE;
Expand Down
5 changes: 4 additions & 1 deletion src/picom.c
Original file line number Diff line number Diff line change
Expand Up @@ -687,8 +687,11 @@ static bool initialize_blur(session_t *ps) {
default: return true;
}

enum backend_image_format format = ps->o.dithered_present
? BACKEND_IMAGE_FORMAT_PIXMAP_HIGH
: BACKEND_IMAGE_FORMAT_PIXMAP;
ps->backend_blur_context = ps->backend_data->ops->create_blur_context(
ps->backend_data, ps->o.blur_method, args);
ps->backend_data, ps->o.blur_method, format, args);
return ps->backend_blur_context != NULL;
}

Expand Down

0 comments on commit a8702b7

Please sign in to comment.