aboutsummaryrefslogtreecommitdiff
path: root/src/backend
diff options
context:
space:
mode:
Diffstat (limited to 'src/backend')
-rw-r--r--src/backend/backend.c118
-rw-r--r--src/backend/backend.h137
-rw-r--r--src/backend/backend_common.c62
-rw-r--r--src/backend/backend_common.h17
-rw-r--r--src/backend/dummy/dummy.c28
-rw-r--r--src/backend/gl/blur.c900
-rw-r--r--src/backend/gl/egl.c469
-rw-r--r--src/backend/gl/egl.h33
-rw-r--r--src/backend/gl/gl_common.c1479
-rw-r--r--src/backend/gl/gl_common.h100
-rw-r--r--src/backend/gl/glx.c51
-rw-r--r--src/backend/gl/glx.h1
-rw-r--r--src/backend/gl/shaders.c187
-rw-r--r--src/backend/meson.build2
-rw-r--r--src/backend/xrender/xrender.c382
15 files changed, 2744 insertions, 1222 deletions
diff --git a/src/backend/backend.c b/src/backend/backend.c
index b0e562a..9d4d10c 100644
--- a/src/backend/backend.c
+++ b/src/backend/backend.c
@@ -16,6 +16,7 @@
extern struct backend_operations xrender_ops, dummy_ops;
#ifdef CONFIG_OPENGL
extern struct backend_operations glx_ops;
+extern struct backend_operations egl_ops;
#endif
struct backend_operations *backend_list[NUM_BKEND] = {
@@ -23,6 +24,7 @@ struct backend_operations *backend_list[NUM_BKEND] = {
[BKEND_DUMMY] = &dummy_ops,
#ifdef CONFIG_OPENGL
[BKEND_GLX] = &glx_ops,
+ [BKEND_EGL] = &egl_ops,
#endif
};
@@ -44,7 +46,7 @@ region_t get_damage(session_t *ps, bool all_damage) {
} else {
for (int i = 0; i < buffer_age; i++) {
auto curr = ((ps->damage - ps->damage_ring) + i) % ps->ndamage;
- log_trace("damage index: %d, damage ring offset: %ld", i, curr);
+ log_trace("damage index: %d, damage ring offset: %td", i, curr);
dump_region(&ps->damage_ring[curr]);
pixman_region32_union(&region, &region, &ps->damage_ring[curr]);
}
@@ -53,6 +55,31 @@ region_t get_damage(session_t *ps, bool all_damage) {
return region;
}
+void handle_device_reset(session_t *ps) {
+ log_error("Device reset detected");
+ // Wait for reset to complete
+ // Although ideally the backend should return DEVICE_STATUS_NORMAL after a reset
+ // is completed, it's not always possible.
+ //
+ // According to ARB_robustness (emphasis mine):
+ //
+ // "If a reset status other than NO_ERROR is returned and subsequent
+ // calls return NO_ERROR, the context reset was encountered and
+ // completed. If a reset status is repeatedly returned, the context **may**
+ // be in the process of resetting."
+ //
+ // Which means it may also not be in the process of resetting. For example on
+ // AMDGPU devices, Mesa OpenGL always return CONTEXT_RESET after a reset has
+ // started, completed or not.
+ //
+ // So here we blindly wait 5 seconds and hope ourselves best of the luck.
+ sleep(5);
+
+ // Reset picom
+ log_info("Resetting picom after device reset");
+ ev_break(ps->loop, EVBREAK_ALL);
+}
+
static void process_window_for_painting(session_t *ps, struct managed_win* w, void* win_image,
double additional_alpha,
region_t* reg_bound, region_t* reg_visible,
@@ -66,6 +93,7 @@ static void process_window_for_painting(session_t *ps, struct managed_win* w, vo
// reg_visible as a hint. Since window image data outside of the
// damage region won't be painted onto target
region_t reg_visible_local;
+ coord_t window_coord = {.x = w->g.x, .y = w->g.y};
{
// The bounding shape, in window local coordinates
region_t reg_bound_local;
@@ -75,16 +103,15 @@ static void process_window_for_painting(session_t *ps, struct managed_win* w, vo
pixman_region32_init(&reg_visible_local);
pixman_region32_intersect(&reg_visible_local,
- reg_visible, reg_paint);
+ reg_visible, reg_paint);
pixman_region32_translate(&reg_visible_local, -w->g.x,
- -w->g.y);
+ -w->g.y);
// Data outside of the bounding shape won't be visible,
// but it is not necessary to limit the image operations
// to the bounding shape yet. So pass that as the visible
// region, not the clip region.
pixman_region32_intersect(
&reg_visible_local, &reg_visible_local, &reg_bound_local);
- pixman_region32_fini(&reg_bound_local);
}
auto new_img = ps->backend_data->ops->clone_image(
@@ -98,15 +125,18 @@ static void process_window_for_painting(session_t *ps, struct managed_win* w, vo
&reg_visible_local, (double[]){w->frame_opacity});
pixman_region32_fini(&reg_frame);
ps->backend_data->ops->compose(ps->backend_data, new_img,
- w->g.x, w->g.y,
- w->g.x + w->widthb, w->g.y + w->heightb,
- reg_paint_in_bound, reg_visible);
+ window_coord, NULL, window_coord,
+ reg_paint_in_bound, reg_visible);
ps->backend_data->ops->release_image(ps->backend_data, new_img);
pixman_region32_fini(&reg_visible_local);
}
/// paint all windows
void paint_all_new(session_t *ps, struct managed_win *t, bool ignore_damage) {
+ if (ps->backend_data->ops->device_status &&
+ ps->backend_data->ops->device_status(ps->backend_data) != DEVICE_STATUS_NORMAL) {
+ return handle_device_reset(ps);
+ }
if (ps->o.xrender_sync_fence) {
if (ps->xsync_exists && !x_fence_sync(ps->c, ps->sync_fence)) {
log_error("x_fence_sync failed, xrender-sync-fence will be "
@@ -206,7 +236,7 @@ void paint_all_new(session_t *ps, struct managed_win *t, bool ignore_damage) {
if (ps->root_image) {
ps->backend_data->ops->compose(ps->backend_data, ps->root_image,
- 0, 0, ps->root_width, ps->root_height,
+ (coord_t){0}, NULL, (coord_t){0},
&reg_paint, &reg_visible);
} else {
ps->backend_data->ops->fill(ps->backend_data, (struct color){0, 0, 0, 1},
@@ -227,6 +257,12 @@ void paint_all_new(session_t *ps, struct managed_win *t, bool ignore_damage) {
// The bounding shape of the window, in global/target coordinates
// reminder: bounding shape contains the WM frame
auto reg_bound = win_get_bounding_shape_global_by_val(w);
+ auto reg_bound_no_corner =
+ win_get_bounding_shape_global_without_corners_by_val(w);
+
+ if (!w->mask_image && (w->bounding_shaped || w->corner_radius != 0)) {
+ win_bind_mask(ps->backend_data, w);
+ }
// The clip region for the current window, in global/target coordinates
// reg_paint_in_bound \in reg_paint
@@ -250,7 +286,9 @@ void paint_all_new(session_t *ps, struct managed_win *t, bool ignore_damage) {
* (e.g. with shaders), we should consult the backend whether the window
* is transparent or not. for now we will just rely on the force_win_blend
* option */
+
auto real_win_mode = w->mode;
+ coord_t window_coord = {.x = w->g.x, .y = w->g.y};
if (w->blur_background &&
(ps->o.force_win_blend || real_win_mode == WMODE_TRANS ||
@@ -294,7 +332,8 @@ void paint_all_new(session_t *ps, struct managed_win *t, bool ignore_damage) {
// We need to blur the bounding shape of the window
// (reg_paint_in_bound = reg_bound \cap reg_paint)
ps->backend_data->ops->blur(
- ps->backend_data, blur_opacity, ps->backend_blur_context,
+ ps->backend_data, blur_opacity,
+ ps->backend_blur_context, w->mask_image, window_coord,
&reg_paint_in_bound, &reg_visible);
} else {
// Window itself is solid, we only need to blur the frame
@@ -313,9 +352,9 @@ void paint_all_new(session_t *ps, struct managed_win *t, bool ignore_damage) {
pixman_region32_intersect(&reg_blur, &reg_blur,
&reg_visible);
}
- ps->backend_data->ops->blur(ps->backend_data, blur_opacity,
- ps->backend_blur_context,
- &reg_blur, &reg_visible);
+ ps->backend_data->ops->blur(
+ ps->backend_data, blur_opacity, ps->backend_blur_context,
+ w->mask_image, window_coord, &reg_blur, &reg_visible);
pixman_region32_fini(&reg_blur);
}
}
@@ -327,9 +366,6 @@ void paint_all_new(session_t *ps, struct managed_win *t, bool ignore_damage) {
// reg_shadow \in reg_paint
auto reg_shadow = win_extents_by_val(w);
pixman_region32_intersect(&reg_shadow, &reg_shadow, &reg_paint);
- if (!ps->o.wintype_option[w->window_type].full_shadow) {
- pixman_region32_subtract(&reg_shadow, &reg_shadow, &reg_bound);
- }
// Mask out the region we don't want shadow on
if (pixman_region32_not_empty(&ps->shadow_exclude_reg)) {
@@ -365,12 +401,28 @@ void paint_all_new(session_t *ps, struct managed_win *t, bool ignore_damage) {
ps->backend_data->ops->set_image_property(
ps->backend_data, IMAGE_PROPERTY_OPACITY, w->shadow_image,
&w->opacity);
+ coord_t shadow_coord = {.x = w->g.x + w->shadow_dx,
+ .y = w->g.y + w->shadow_dy};
+
+ auto inverted_mask = NULL;
+ if (!ps->o.wintype_option[w->window_type].full_shadow) {
+ pixman_region32_subtract(&reg_shadow, &reg_shadow,
+ &reg_bound_no_corner);
+ if (w->mask_image) {
+ inverted_mask = w->mask_image;
+ ps->backend_data->ops->set_image_property(
+ ps->backend_data, IMAGE_PROPERTY_INVERTED,
+ inverted_mask, (bool[]){true});
+ }
+ }
ps->backend_data->ops->compose(
- ps->backend_data, w->shadow_image,
- w->g.x + w->shadow_dx, w->g.y + w->shadow_dy,
- w->g.x + w->shadow_dx + w->shadow_width,
- w->g.y + w->shadow_dy + w->shadow_height,
- &reg_shadow, &reg_visible);
+ ps->backend_data, w->shadow_image, shadow_coord,
+ inverted_mask, window_coord, &reg_shadow, &reg_visible);
+ if (inverted_mask) {
+ ps->backend_data->ops->set_image_property(
+ ps->backend_data, IMAGE_PROPERTY_INVERTED,
+ inverted_mask, (bool[]){false});
+ }
pixman_region32_fini(&reg_shadow);
}
@@ -395,6 +447,25 @@ void paint_all_new(session_t *ps, struct managed_win *t, bool ignore_damage) {
&dim_opacity);
ps->backend_data->ops->set_image_property(
ps->backend_data, IMAGE_PROPERTY_OPACITY, w->win_image, &w->opacity);
+ ps->backend_data->ops->set_image_property(
+ ps->backend_data, IMAGE_PROPERTY_CORNER_RADIUS, w->win_image,
+ (double[]){w->corner_radius});
+ if (w->corner_radius) {
+ int border_width = w->g.border_width;
+ if (border_width == 0) {
+ // Some WM has borders implemented as WM frames
+ border_width = min3(w->frame_extents.left,
+ w->frame_extents.right,
+ w->frame_extents.bottom);
+ }
+ ps->backend_data->ops->set_image_property(
+ ps->backend_data, IMAGE_PROPERTY_BORDER_WIDTH,
+ w->win_image, &border_width);
+ }
+
+ ps->backend_data->ops->set_image_property(
+ ps->backend_data, IMAGE_PROPERTY_CUSTOM_SHADER, w->win_image,
+ w->fg_shader ? (void *)w->fg_shader->backend_shader : NULL);
}
if (w->opacity * MAX_ALPHA < 1) {
@@ -411,13 +482,12 @@ void paint_all_new(session_t *ps, struct managed_win *t, bool ignore_damage) {
pixman_region32_subtract(&reg_shadow_clip, &reg_shadow_clip, &reg_bound);
}
- // Draw window on target
bool is_animating = 0 <= w->animation_progress && w->animation_progress < 1.0;
+ // Draw window on target
if (w->frame_opacity == 1 && !is_animating) {
ps->backend_data->ops->compose(ps->backend_data, w->win_image,
- w->g.x, w->g.y,
- w->g.x + w->widthb, w->g.y + w->heightb,
- &reg_paint_in_bound, &reg_visible);
+ window_coord, NULL, window_coord,
+ &reg_paint_in_bound, &reg_visible);
} else {
if (is_animating && w->old_win_image) {
assert(w->old_win_image);
diff --git a/src/backend/backend.h b/src/backend/backend.h
index ae107d3..191e814 100644
--- a/src/backend/backend.h
+++ b/src/backend/backend.h
@@ -16,6 +16,8 @@
typedef struct session session_t;
struct managed_win;
+struct backend_shadow_context;
+
struct ev_loop;
struct backend_operations;
@@ -30,12 +32,31 @@ typedef struct backend_base {
// ...
} backend_t;
+typedef struct geometry {
+ int width;
+ int height;
+} geometry_t;
+
+typedef struct coord {
+ int x, y;
+} coord_t;
+
typedef void (*backend_ready_callback_t)(void *);
+// This mimics OpenGL's ARB_robustness extension, which enables detection of GPU context
+// resets.
+// See: https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_robustness.txt, section
+// 2.6 "Graphics Reset Recovery".
+enum device_status {
+ DEVICE_STATUS_NORMAL,
+ DEVICE_STATUS_RESETTING,
+};
+
// When image properties are actually applied to the image, they are applied in a
// particular order:
//
-// Color inversion -> Dimming -> Opacity multiply -> Limit maximum brightness
+// Corner radius -> Color inversion -> Dimming -> Opacity multiply -> Limit maximum
+// brightness
enum image_properties {
// Whether the color of the image is inverted
// 1 boolean, default: false
@@ -54,6 +75,15 @@ enum image_properties {
// brightness down to the max brightness value.
// 1 double, default: 1
IMAGE_PROPERTY_MAX_BRIGHTNESS,
+ // Gives the image a rounded corner.
+ // 1 double, default: 0
+ IMAGE_PROPERTY_CORNER_RADIUS,
+ // Border width
+ // 1 int, default: 0
+ IMAGE_PROPERTY_BORDER_WIDTH,
+ // Custom shader for this window.
+ // 1 pointer to shader struct, default: NULL
+ IMAGE_PROPERTY_CUSTOM_SHADER,
};
enum image_operations {
@@ -61,6 +91,12 @@ enum image_operations {
IMAGE_OP_APPLY_ALPHA,
};
+enum shader_attributes {
+ // Whether the shader needs to be render regardless of whether the window is
+ // updated.
+ SHADER_ATTRIBUTE_ANIMATED = 1,
+};
+
struct gaussian_blur_args {
int size;
double deviation;
@@ -133,26 +169,30 @@ struct backend_operations {
void (*prepare)(backend_t *backend_data, const region_t *reg_damage);
/**
- * Paint the content of an image onto the rendering buffer
+ * Paint the content of an image onto the rendering buffer.
*
- * @param backend_data the backend data
- * @param image_data the image to paint
- * @param dst_x1, dst_y1 the top left corner of the image in the target
- * @param dst_x2, dst_y2 the top right corner of the image in the target
- * @param reg_paint the clip region, in target coordinates
- * @param reg_visible the visible region, in target coordinates
+ * @param backend_data the backend data
+ * @param image_data the image to paint
+ * @param dst_x, dst_y the top left corner of the image in the target
+ * @param mask the mask image, the top left of the mask is aligned with
+ * the top left of the image
+ * @param reg_paint the clip region, in target coordinates
+ * @param reg_visible the visible region, in target coordinates
*/
- void (*compose)(backend_t *backend_data, void *image_data,
- int dst_x1, int dst_y1, int dst_x2, int dst_y2,
- const region_t *reg_paint, const region_t *reg_visible);
+ void (*compose)(backend_t *backend_data, void *image_data, coord_t image_dst,
+ void *mask, coord_t mask_dst, const region_t *reg_paint,
+ const region_t *reg_visible);
/// Fill rectangle of the rendering buffer, mostly for debug purposes, optional.
void (*fill)(backend_t *backend_data, struct color, const region_t *clip);
/// Blur a given region of the rendering buffer.
- bool (*blur)(backend_t *backend_data, double opacity, void *blur_ctx,
- const region_t *reg_blur, const region_t *reg_visible)
- attr_nonnull(1, 3, 4, 5);
+ ///
+ /// The blur is limited by `mask`. `mask_dst` specifies the top left corner of the
+ /// mask is.
+ bool (*blur)(backend_t *backend_data, double opacity, void *blur_ctx, void *mask,
+ coord_t mask_dst, const region_t *reg_blur,
+ const region_t *reg_visible) attr_nonnull(1, 3, 4, 6, 7);
/// Update part of the back buffer with the rendering buffer, then present the
/// back buffer onto the target window (if not back buffered, update part of the
@@ -175,18 +215,67 @@ struct backend_operations {
void *(*bind_pixmap)(backend_t *backend_data, xcb_pixmap_t pixmap,
struct xvisual_info fmt, bool owned);
- /// Create a shadow image based on the parameters
+ /// Create a shadow context for rendering shadows with radius `radius`.
+ /// Default implementation: default_backend_create_shadow_context
+ struct backend_shadow_context *(*create_shadow_context)(backend_t *backend_data,
+ double radius);
+ /// Destroy a shadow context
+ /// Default implementation: default_backend_destroy_shadow_context
+ void (*destroy_shadow_context)(backend_t *backend_data,
+ struct backend_shadow_context *ctx);
+
+ /// Create a shadow image based on the parameters. Resulting image should have a
+ /// size of `width + radisu * 2` x `height + radius * 2`. Radius is set when the
+ /// shadow context is created.
/// Default implementation: default_backend_render_shadow
+ ///
+ /// Required.
void *(*render_shadow)(backend_t *backend_data, int width, int height,
- const conv *kernel, double r, double g, double b, double a);
+ struct backend_shadow_context *ctx, struct color color);
+
+ /// Create a shadow by blurring a mask. `size` is the size of the blur. The
+ /// backend can use whichever blur method is the fastest. The shadow produced
+ /// shoule be consistent with `render_shadow`.
+ ///
+ /// Optional.
+ void *(*shadow_from_mask)(backend_t *backend_data, void *mask,
+ struct backend_shadow_context *ctx, struct color color);
+
+ /// Create a mask image from region `reg`. This region can be used to create
+ /// shadow, or used as a mask for composing. When used as a mask, it should mask
+ /// out everything that is not inside the region used to create it.
+ ///
+ /// Image properties might be set on masks too, at least the INVERTED and
+ /// CORNER_RADIUS properties must be supported. Inversion should invert the inside
+ /// and outside of the mask. Corner radius should exclude the corners from the
+ /// mask. Corner radius should be applied before the inversion.
+ ///
+ /// Required.
+ void *(*make_mask)(backend_t *backend_data, geometry_t size, const region_t *reg);
// ============ Resource management ===========
/// Free resources associated with an image data structure
void (*release_image)(backend_t *backend_data, void *img_data) attr_nonnull(1, 2);
+ /// Create a shader object from a shader source.
+ ///
+ /// Optional
+ void *(*create_shader)(backend_t *backend_data, const char *source)attr_nonnull(1, 2);
+
+ /// Free a shader object.
+ ///
+ /// Required if create_shader is present.
+ void (*destroy_shader)(backend_t *backend_data, void *shader) attr_nonnull(1, 2);
+
// =========== Query ===========
+ /// Get the attributes of a shader.
+ ///
+ /// Optional, Returns a bitmask of attributes, see `shader_attributes`.
+ uint64_t (*get_shader_attributes)(backend_t *backend_data, void *shader)
+ attr_nonnull(1, 2);
+
/// Return if image is not completely opaque.
///
/// This function is needed because some backend might change the content of the
@@ -243,20 +332,6 @@ struct backend_operations {
bool (*image_op)(backend_t *backend_data, enum image_operations op, void *image_data,
const region_t *reg_op, const region_t *reg_visible, void *args);
- /**
- * Read the color of the pixel at given position of the given image. Image
- * properties have no effect.
- *
- * @param backend_data backend_data
- * @param image_data an image data structure previously returned by the
- * backend. the image to read pixel from.
- * @param x, y coordinate of the pixel to read
- * @param[out] color the color of the pixel
- * @return whether the operation is successful
- */
- bool (*read_pixel)(backend_t *backend_data, void *image_data, int x, int y,
- struct color *output);
-
/// Create another instance of the `image_data`. All `image_op` and
/// `set_image_property` calls on the returned image should not affect the
/// original image
@@ -282,6 +357,8 @@ struct backend_operations {
enum driver (*detect_driver)(backend_t *backend_data);
void (*diagnostics)(backend_t *backend_data);
+
+ enum device_status (*device_status)(backend_t *backend_data);
};
extern struct backend_operations *backend_list[];
diff --git a/src/backend/backend_common.c b/src/backend/backend_common.c
index c0377d3..d6fcce2 100644
--- a/src/backend/backend_common.c
+++ b/src/backend/backend_common.c
@@ -92,7 +92,7 @@ make_shadow(xcb_connection_t *c, const conv *kernel, double opacity, int width,
}
unsigned char *data = ximage->data;
- long sstride = ximage->stride;
+ long long sstride = ximage->stride;
// If the window body is smaller than the kernel, we do convolution directly
if (width < r * 2 && height < r * 2) {
@@ -100,7 +100,7 @@ make_shadow(xcb_connection_t *c, const conv *kernel, double opacity, int width,
for (int x = 0; x < swidth; x++) {
double sum = sum_kernel_normalized(
kernel, d - x - 1, d - y - 1, width, height);
- data[y * sstride + x] = (uint8_t)(sum * 255.0);
+ data[y * sstride + x] = (uint8_t)(sum * 255.0 * opacity);
}
}
return ximage;
@@ -118,14 +118,14 @@ make_shadow(xcb_connection_t *c, const conv *kernel, double opacity, int width,
for (int x = 0; x < r * 2; x++) {
double sum = sum_kernel_normalized(kernel, d - x - 1,
d - y - 1, d, height) *
- 255.0;
+ 255.0 * opacity;
data[y * sstride + x] = (uint8_t)sum;
data[y * sstride + swidth - x - 1] = (uint8_t)sum;
}
}
for (int y = 0; y < sheight; y++) {
- double sum =
- sum_kernel_normalized(kernel, 0, d - y - 1, d, height) * 255.0;
+ double sum = sum_kernel_normalized(kernel, 0, d - y - 1, d, height) *
+ 255.0 * opacity;
memset(&data[y * sstride + r * 2], (uint8_t)sum,
(size_t)(width - 2 * r));
}
@@ -137,14 +137,14 @@ make_shadow(xcb_connection_t *c, const conv *kernel, double opacity, int width,
for (int x = 0; x < swidth; x++) {
double sum = sum_kernel_normalized(kernel, d - x - 1,
d - y - 1, width, d) *
- 255.0;
+ 255.0 * opacity;
data[y * sstride + x] = (uint8_t)sum;
data[(sheight - y - 1) * sstride + x] = (uint8_t)sum;
}
}
for (int x = 0; x < swidth; x++) {
- double sum =
- sum_kernel_normalized(kernel, d - x - 1, 0, width, d) * 255.0;
+ double sum = sum_kernel_normalized(kernel, d - x - 1, 0, width, d) *
+ 255.0 * opacity;
for (int y = r * 2; y < height; y++) {
data[y * sstride + x] = (uint8_t)sum;
}
@@ -291,16 +291,16 @@ shadow_picture_err:
return false;
}
-void *
-default_backend_render_shadow(backend_t *backend_data, int width, int height,
- const conv *kernel, double r, double g, double b, double a) {
- xcb_pixmap_t shadow_pixel = solid_picture(backend_data->c, backend_data->root,
- true, 1, r, g, b),
+void *default_backend_render_shadow(backend_t *backend_data, int width, int height,
+ struct backend_shadow_context *sctx, struct color color) {
+ const conv *kernel = (void *)sctx;
+ xcb_pixmap_t shadow_pixel = solid_picture(backend_data->c, backend_data->root, true,
+ 1, color.red, color.green, color.blue),
shadow = XCB_NONE;
xcb_render_picture_t pict = XCB_NONE;
- if (!build_shadow(backend_data->c, backend_data->root, a, width, height, kernel,
- shadow_pixel, &shadow, &pict)) {
+ if (!build_shadow(backend_data->c, backend_data->root, color.alpha, width, height,
+ kernel, shadow_pixel, &shadow, &pict)) {
return NULL;
}
@@ -311,6 +311,34 @@ default_backend_render_shadow(backend_t *backend_data, int width, int height,
return ret;
}
+/// Implement render_shadow with shadow_from_mask
+void *
+backend_render_shadow_from_mask(backend_t *backend_data, int width, int height,
+ struct backend_shadow_context *sctx, struct color color) {
+ region_t reg;
+ pixman_region32_init_rect(&reg, 0, 0, (unsigned int)width, (unsigned int)height);
+ void *mask = backend_data->ops->make_mask(
+ backend_data, (geometry_t){.width = width, .height = height}, &reg);
+ pixman_region32_fini(&reg);
+
+ void *shadow = backend_data->ops->shadow_from_mask(backend_data, mask, sctx, color);
+ backend_data->ops->release_image(backend_data, mask);
+ return shadow;
+}
+
+struct backend_shadow_context *
+default_create_shadow_context(backend_t *backend_data attr_unused, double radius) {
+ auto ret =
+ (struct backend_shadow_context *)gaussian_kernel_autodetect_deviation(radius);
+ sum_kernel_preprocess((conv *)ret);
+ return ret;
+}
+
+void default_destroy_shadow_context(backend_t *backend_data attr_unused,
+ struct backend_shadow_context *sctx) {
+ free_conv((conv *)sctx);
+}
+
static struct conv **generate_box_blur_kernel(struct box_blur_args *args, int *kernel_count) {
int r = args->size * 2 + 1;
assert(r > 0);
@@ -449,7 +477,10 @@ bool default_set_image_property(backend_t *base attr_unused, enum image_properti
tex->ewidth = iargs[0];
tex->eheight = iargs[1];
break;
+ case IMAGE_PROPERTY_CORNER_RADIUS: tex->corner_radius = dargs[0]; break;
case IMAGE_PROPERTY_MAX_BRIGHTNESS: tex->max_brightness = dargs[0]; break;
+ case IMAGE_PROPERTY_BORDER_WIDTH: tex->border_width = *(int *)arg; break;
+ case IMAGE_PROPERTY_CUSTOM_SHADER: break;
}
return true;
@@ -468,6 +499,7 @@ struct backend_image *default_new_backend_image(int w, int h) {
ret->eheight = h;
ret->ewidth = w;
ret->color_inverted = false;
+ ret->corner_radius = 0;
return ret;
}
diff --git a/src/backend/backend_common.h b/src/backend/backend_common.h
index 5c9c806..c72a168 100644
--- a/src/backend/backend_common.h
+++ b/src/backend/backend_common.h
@@ -37,9 +37,11 @@ struct backend_image {
double opacity;
double dim;
double max_brightness;
+ double corner_radius;
// Effective size of the image
int ewidth, eheight;
bool color_inverted;
+ int border_width;
};
bool build_shadow(xcb_connection_t *, xcb_drawable_t, double opacity, int width,
@@ -60,9 +62,18 @@ bool default_is_win_transparent(void *, win *, void *);
/// caveat as `default_is_win_transparent` applies.
bool default_is_frame_transparent(void *, win *, void *);
+void *default_backend_render_shadow(backend_t *backend_data, int width, int height,
+ struct backend_shadow_context *sctx, struct color color);
+
+/// Implement `render_shadow` with `shadow_from_mask`.
void *
-default_backend_render_shadow(backend_t *backend_data, int width, int height,
- const conv *kernel, double r, double g, double b, double a);
+backend_render_shadow_from_mask(backend_t *backend_data, int width, int height,
+ struct backend_shadow_context *sctx, struct color color);
+struct backend_shadow_context *
+default_create_shadow_context(backend_t *backend_data, double radius);
+
+void default_destroy_shadow_context(backend_t *backend_data,
+ struct backend_shadow_context *sctx);
void init_backend_base(struct backend_base *base, session_t *ps);
@@ -70,8 +81,6 @@ struct conv **generate_blur_kernel(enum blur_method method, void *args, int *ker
struct dual_kawase_params *generate_dual_kawase_params(void *args);
void *default_clone_image(backend_t *base, const void *image_data, const region_t *reg);
-void *default_resize_image(backend_t *base, const void *image_data, uint16_t desired_width,
- uint16_t desired_height, const region_t *reg);
bool default_is_image_transparent(backend_t *base attr_unused, void *image_data);
bool default_set_image_property(backend_t *base attr_unused, enum image_properties op,
void *image_data, void *arg);
diff --git a/src/backend/dummy/dummy.c b/src/backend/dummy/dummy.c
index a057b97..7e06fac 100644
--- a/src/backend/dummy/dummy.c
+++ b/src/backend/dummy/dummy.c
@@ -23,6 +23,8 @@ struct dummy_image {
struct dummy_data {
struct backend_base base;
struct dummy_image *images;
+
+ struct backend_image mask;
};
struct backend_base *dummy_init(struct session *ps attr_unused) {
@@ -47,6 +49,9 @@ void dummy_deinit(struct backend_base *data) {
static void dummy_check_image(struct backend_base *base, const struct dummy_image *img) {
auto dummy = (struct dummy_data *)base;
+ if (img == (struct dummy_image *)&dummy->mask) {
+ return;
+ }
struct dummy_image *tmp = NULL;
HASH_FIND_INT(dummy->images, &img->pixmap, tmp);
if (!tmp) {
@@ -56,10 +61,13 @@ static void dummy_check_image(struct backend_base *base, const struct dummy_imag
assert(*tmp->refcount > 0);
}
-void dummy_compose(struct backend_base *base, void *image, int dst_x1 attr_unused,
- int dst_y1 attr_unused, int dst_x2 attr_unused, int dst_y2 attr_unused,
- const region_t *reg_paint attr_unused, const region_t *reg_visible attr_unused) {
+void dummy_compose(struct backend_base *base, void *image, coord_t dst attr_unused,
+ void *mask attr_unused, coord_t mask_dst attr_unused,
+ const region_t *reg_paint attr_unused,
+ const region_t *reg_visible attr_unused) {
+ auto dummy attr_unused = (struct dummy_data *)base;
dummy_check_image(base, image);
+ assert(mask == NULL || mask == &dummy->mask);
}
void dummy_fill(struct backend_base *backend_data attr_unused, struct color c attr_unused,
@@ -67,7 +75,8 @@ void dummy_fill(struct backend_base *backend_data attr_unused, struct color c at
}
bool dummy_blur(struct backend_base *backend_data attr_unused, double opacity attr_unused,
- void *blur_ctx attr_unused, const region_t *reg_blur attr_unused,
+ void *blur_ctx attr_unused, void *mask attr_unused,
+ coord_t mask_dst attr_unused, const region_t *reg_blur attr_unused,
const region_t *reg_visible attr_unused) {
return true;
}
@@ -94,6 +103,9 @@ void *dummy_bind_pixmap(struct backend_base *base, xcb_pixmap_t pixmap,
void dummy_release_image(backend_t *base, void *image) {
auto dummy = (struct dummy_data *)base;
+ if (image == &dummy->mask) {
+ return;
+ }
auto img = (struct dummy_image *)image;
assert(*img->refcount > 0);
(*img->refcount)--;
@@ -121,6 +133,11 @@ bool dummy_image_op(struct backend_base *base, enum image_operations op attr_unu
return true;
}
+void *dummy_make_mask(struct backend_base *base, geometry_t size attr_unused,
+ const region_t *reg attr_unused) {
+ return &(((struct dummy_data *)base)->mask);
+}
+
bool dummy_set_image_property(struct backend_base *base, enum image_properties prop attr_unused,
void *image, void *arg attr_unused) {
dummy_check_image(base, image);
@@ -158,7 +175,10 @@ struct backend_operations dummy_ops = {
.fill = dummy_fill,
.blur = dummy_blur,
.bind_pixmap = dummy_bind_pixmap,
+ .create_shadow_context = default_create_shadow_context,
+ .destroy_shadow_context = default_destroy_shadow_context,
.render_shadow = default_backend_render_shadow,
+ .make_mask = dummy_make_mask,
.release_image = dummy_release_image,
.is_image_transparent = dummy_is_image_transparent,
.buffer_age = dummy_buffer_age,
diff --git a/src/backend/gl/blur.c b/src/backend/gl/blur.c
new file mode 100644
index 0000000..41022f5
--- /dev/null
+++ b/src/backend/gl/blur.c
@@ -0,0 +1,900 @@
+#include <locale.h>
+#include <stdbool.h>
+
+#include <backend/backend.h>
+#include <backend/backend_common.h>
+
+#include "gl_common.h"
+
+struct gl_blur_context {
+ enum blur_method method;
+ gl_blur_shader_t *blur_shader;
+
+ /// Temporary textures used for blurring
+ GLuint *blur_textures;
+ int blur_texture_count;
+ /// Temporary fbos used for blurring
+ GLuint *blur_fbos;
+ int blur_fbo_count;
+
+ /// Cached dimensions of each blur_texture. They are the same size as the target,
+ /// so they are always big enough without resizing.
+ /// Turns out calling glTexImage to resize is expensive, so we avoid that.
+ struct texture_size {
+ int width;
+ int height;
+ } *texture_sizes;
+
+ /// Cached dimensions of the offscreen framebuffer. It's the same size as the
+ /// target but is expanded in either direction by resize_width / resize_height.
+ int fb_width, fb_height;
+
+ /// How much do we need to resize the damaged region for blurring.
+ int resize_width, resize_height;
+
+ int npasses;
+};
+
+/**
+ * Blur contents in a particular region.
+ */
+bool gl_kernel_blur(double opacity, struct gl_blur_context *bctx, const rect_t *extent,
+ struct backend_image *mask, coord_t mask_dst, const GLuint vao[2],
+ const int vao_nelems[2], GLuint source_texture,
+ geometry_t source_size, GLuint target_fbo, GLuint default_mask) {
+ int dst_y_fb_coord = bctx->fb_height - extent->y2;
+
+ int curr = 0;
+ for (int i = 0; i < bctx->npasses; ++i) {
+ const gl_blur_shader_t *p = &bctx->blur_shader[i];
+ assert(p->prog);
+
+ assert(bctx->blur_textures[curr]);
+
+ // The origin to use when sampling from the source texture
+ GLint texorig_x = extent->x1, texorig_y = dst_y_fb_coord;
+ GLint tex_width, tex_height;
+ GLuint src_texture;
+
+ if (i == 0) {
+ src_texture = source_texture;
+ tex_width = source_size.width;
+ tex_height = source_size.height;
+ } else {
+ src_texture = bctx->blur_textures[curr];
+ auto src_size = bctx->texture_sizes[curr];
+ tex_width = src_size.width;
+ tex_height = src_size.height;
+ }
+
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_2D, src_texture);
+ glUseProgram(p->prog);
+ glUniform2f(p->uniform_pixel_norm, 1.0F / (GLfloat)tex_width,
+ 1.0F / (GLfloat)tex_height);
+
+ glActiveTexture(GL_TEXTURE1);
+ glBindTexture(GL_TEXTURE_2D, default_mask);
+
+ glUniform1i(p->uniform_mask_tex, 1);
+ glUniform2f(p->uniform_mask_offset, 0.0F, 0.0F);
+ glUniform1i(p->uniform_mask_inverted, 0);
+ glUniform1f(p->uniform_mask_corner_radius, 0.0F);
+
+ // The number of indices in the selected vertex array
+ GLsizei nelems;
+
+ if (i < bctx->npasses - 1) {
+ assert(bctx->blur_fbos[0]);
+ assert(bctx->blur_textures[!curr]);
+
+ // not last pass, draw into framebuffer, with resized regions
+ glBindVertexArray(vao[1]);
+ nelems = vao_nelems[1];
+ glBindFramebuffer(GL_DRAW_FRAMEBUFFER, bctx->blur_fbos[0]);
+
+ glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
+ GL_TEXTURE_2D, bctx->blur_textures[!curr], 0);
+ glDrawBuffer(GL_COLOR_ATTACHMENT0);
+ if (!gl_check_fb_complete(GL_FRAMEBUFFER)) {
+ return false;
+ }
+
+ glUniform1f(p->uniform_opacity, 1.0F);
+ } else {
+ // last pass, draw directly into the back buffer, with origin
+ // regions. And apply mask if requested
+ if (mask) {
+ auto inner = (struct gl_texture *)mask->inner;
+ glActiveTexture(GL_TEXTURE1);
+ glBindTexture(GL_TEXTURE_2D, inner->texture);
+ glUniform1i(p->uniform_mask_inverted, mask->color_inverted);
+ glUniform1f(p->uniform_mask_corner_radius,
+ (float)mask->corner_radius);
+ glUniform2f(
+ p->uniform_mask_offset, (float)(mask_dst.x),
+ (float)(bctx->fb_height - mask_dst.y - inner->height));
+ }
+ glBindVertexArray(vao[0]);
+ nelems = vao_nelems[0];
+ glBindFramebuffer(GL_FRAMEBUFFER, target_fbo);
+
+ glUniform1f(p->uniform_opacity, (float)opacity);
+ }
+
+ glUniform2f(p->texorig_loc, (GLfloat)texorig_x, (GLfloat)texorig_y);
+ glDrawElements(GL_TRIANGLES, nelems, GL_UNSIGNED_INT, NULL);
+
+ // XXX use multiple draw calls is probably going to be slow than
+ // just simply blur the whole area.
+
+ curr = !curr;
+ }
+
+ return true;
+}
+
+bool gl_dual_kawase_blur(double opacity, struct gl_blur_context *bctx, const rect_t *extent,
+ struct backend_image *mask, coord_t mask_dst, const GLuint vao[2],
+ const int vao_nelems[2], GLuint source_texture,
+ geometry_t source_size, GLuint target_fbo, GLuint default_mask) {
+ int dst_y_fb_coord = bctx->fb_height - extent->y2;
+
+ int iterations = bctx->blur_texture_count;
+ int scale_factor = 1;
+
+ // Kawase downsample pass
+ const gl_blur_shader_t *down_pass = &bctx->blur_shader[0];
+ assert(down_pass->prog);
+ glUseProgram(down_pass->prog);
+
+ glUniform2f(down_pass->texorig_loc, (GLfloat)extent->x1, (GLfloat)dst_y_fb_coord);
+
+ for (int i = 0; i < iterations; ++i) {
+ // Scale output width / height by half in each iteration
+ scale_factor <<= 1;
+
+ GLuint src_texture;
+ int tex_width, tex_height;
+
+ if (i == 0) {
+ // first pass: copy from back buffer
+ src_texture = source_texture;
+ tex_width = source_size.width;
+ tex_height = source_size.height;
+ } else {
+ // copy from previous pass
+ src_texture = bctx->blur_textures[i - 1];
+ auto src_size = bctx->texture_sizes[i - 1];
+ tex_width = src_size.width;
+ tex_height = src_size.height;
+ }
+
+ assert(src_texture);
+ assert(bctx->blur_fbos[i]);
+
+ glBindTexture(GL_TEXTURE_2D, src_texture);
+ glBindVertexArray(vao[1]);
+ auto nelems = vao_nelems[1];
+ glBindFramebuffer(GL_DRAW_FRAMEBUFFER, bctx->blur_fbos[i]);
+ glDrawBuffer(GL_COLOR_ATTACHMENT0);
+
+ glUniform1f(down_pass->scale_loc, (GLfloat)scale_factor);
+
+ glUniform2f(down_pass->uniform_pixel_norm, 1.0F / (GLfloat)tex_width,
+ 1.0F / (GLfloat)tex_height);
+
+ glDrawElements(GL_TRIANGLES, nelems, GL_UNSIGNED_INT, NULL);
+ }
+
+ // Kawase upsample pass
+ const gl_blur_shader_t *up_pass = &bctx->blur_shader[1];
+ assert(up_pass->prog);
+ glUseProgram(up_pass->prog);
+
+ glUniform2f(up_pass->texorig_loc, (GLfloat)extent->x1, (GLfloat)dst_y_fb_coord);
+
+ for (int i = iterations - 1; i >= 0; --i) {
+ // Scale output width / height back by two in each iteration
+ scale_factor >>= 1;
+
+ const GLuint src_texture = bctx->blur_textures[i];
+ assert(src_texture);
+
+ // Calculate normalized half-width/-height of a src pixel
+ auto src_size = bctx->texture_sizes[i];
+ int tex_width = src_size.width;
+ int tex_height = src_size.height;
+
+ // The number of indices in the selected vertex array
+ GLsizei nelems;
+
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_2D, src_texture);
+ glActiveTexture(GL_TEXTURE1);
+ glBindTexture(GL_TEXTURE_2D, default_mask);
+
+ glUniform1i(up_pass->uniform_mask_tex, 1);
+ glUniform2f(up_pass->uniform_mask_offset, 0.0F, 0.0F);
+ glUniform1i(up_pass->uniform_mask_inverted, 0);
+ glUniform1f(up_pass->uniform_mask_corner_radius, 0.0F);
+ if (i > 0) {
+ assert(bctx->blur_fbos[i - 1]);
+
+ // not last pass, draw into next framebuffer
+ glBindVertexArray(vao[1]);
+ nelems = vao_nelems[1];
+ glBindFramebuffer(GL_DRAW_FRAMEBUFFER, bctx->blur_fbos[i - 1]);
+ glDrawBuffer(GL_COLOR_ATTACHMENT0);
+
+ glUniform1f(up_pass->uniform_opacity, (GLfloat)1);
+ } else {
+ // last pass, draw directly into the back buffer
+ if (mask) {
+ auto inner = (struct gl_texture *)mask->inner;
+ glActiveTexture(GL_TEXTURE1);
+ glBindTexture(GL_TEXTURE_2D, inner->texture);
+ glUniform1i(up_pass->uniform_mask_inverted,
+ mask->color_inverted);
+ glUniform1f(up_pass->uniform_mask_corner_radius,
+ (float)mask->corner_radius);
+ glUniform2f(
+ up_pass->uniform_mask_offset, (float)(mask_dst.x),
+ (float)(bctx->fb_height - mask_dst.y - inner->height));
+ }
+ glBindVertexArray(vao[0]);
+ nelems = vao_nelems[0];
+ glBindFramebuffer(GL_DRAW_FRAMEBUFFER, target_fbo);
+
+ glUniform1f(up_pass->uniform_opacity, (GLfloat)opacity);
+ }
+
+ glUniform1f(up_pass->scale_loc, (GLfloat)scale_factor);
+ glUniform2f(up_pass->uniform_pixel_norm, 1.0F / (GLfloat)tex_width,
+ 1.0F / (GLfloat)tex_height);
+
+ glDrawElements(GL_TRIANGLES, nelems, GL_UNSIGNED_INT, NULL);
+ }
+
+ return true;
+}
+
+bool gl_blur_impl(double opacity, struct gl_blur_context *bctx, void *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;
+
+ 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
+ bctx->fb_width = source_size.width;
+ bctx->fb_height = source_size.height;
+
+ for (int i = 0; i < bctx->blur_texture_count; ++i) {
+ auto tex_size = bctx->texture_sizes + i;
+ if (bctx->method == BLUR_METHOD_DUAL_KAWASE) {
+ // Use smaller textures for each iteration (quarter of the
+ // previous texture)
+ tex_size->width = 1 + ((bctx->fb_width - 1) >> (i + 1));
+ tex_size->height = 1 + ((bctx->fb_height - 1) >> (i + 1));
+ } else {
+ tex_size->width = bctx->fb_width;
+ tex_size->height = bctx->fb_height;
+ }
+
+ glBindTexture(GL_TEXTURE_2D, bctx->blur_textures[i]);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, tex_size->width,
+ tex_size->height, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
+
+ if (bctx->method == BLUR_METHOD_DUAL_KAWASE) {
+ // Attach texture to FBO target
+ glBindFramebuffer(GL_DRAW_FRAMEBUFFER, bctx->blur_fbos[i]);
+ glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER,
+ GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
+ bctx->blur_textures[i], 0);
+ if (!gl_check_fb_complete(GL_FRAMEBUFFER)) {
+ glBindFramebuffer(GL_FRAMEBUFFER, 0);
+ return false;
+ }
+ }
+ }
+ glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
+ }
+
+ // Remainder: regions are in Xorg coordinates
+ auto reg_blur_resized =
+ resize_region(reg_blur, bctx->resize_width, bctx->resize_height);
+ const rect_t *extent = pixman_region32_extents((region_t *)reg_blur),
+ *extent_resized = pixman_region32_extents(&reg_blur_resized);
+ int width = extent->x2 - extent->x1, height = extent->y2 - extent->y1;
+ if (width == 0 || height == 0) {
+ return true;
+ }
+
+ int nrects, nrects_resized;
+ const rect_t *rects = pixman_region32_rectangles((region_t *)reg_blur, &nrects),
+ *rects_resized =
+ pixman_region32_rectangles(&reg_blur_resized, &nrects_resized);
+ if (!nrects || !nrects_resized) {
+ return true;
+ }
+
+ auto coord = ccalloc(nrects * 16, GLint);
+ auto indices = ccalloc(nrects * 6, GLuint);
+ auto extent_height = extent_resized->y2 - extent_resized->y1;
+ x_rect_to_coords(
+ nrects, rects, (coord_t){.x = extent_resized->x1, .y = extent_resized->y1},
+ extent_height, bctx->fb_height, source_size.height, false, coord, indices);
+
+ auto coord_resized = ccalloc(nrects_resized * 16, GLint);
+ auto indices_resized = ccalloc(nrects_resized * 6, GLuint);
+ x_rect_to_coords(nrects_resized, rects_resized,
+ (coord_t){.x = extent_resized->x1, .y = extent_resized->y1},
+ extent_height, bctx->fb_height, bctx->fb_height, false,
+ coord_resized, indices_resized);
+ pixman_region32_fini(&reg_blur_resized);
+
+ GLuint vao[2];
+ glGenVertexArrays(2, vao);
+ GLuint bo[4];
+ glGenBuffers(4, bo);
+
+ glBindVertexArray(vao[0]);
+ glBindBuffer(GL_ARRAY_BUFFER, bo[0]);
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bo[1]);
+ glBufferData(GL_ARRAY_BUFFER, (long)sizeof(*coord) * nrects * 16, coord, GL_STATIC_DRAW);
+ glBufferData(GL_ELEMENT_ARRAY_BUFFER, (long)sizeof(*indices) * nrects * 6,
+ indices, GL_STATIC_DRAW);
+ glEnableVertexAttribArray(vert_coord_loc);
+ glEnableVertexAttribArray(vert_in_texcoord_loc);
+ glVertexAttribPointer(vert_coord_loc, 2, GL_INT, GL_FALSE, sizeof(GLint) * 4, NULL);
+ glVertexAttribPointer(vert_in_texcoord_loc, 2, GL_INT, GL_FALSE,
+ sizeof(GLint) * 4, (void *)(sizeof(GLint) * 2));
+
+ glBindVertexArray(vao[1]);
+ glBindBuffer(GL_ARRAY_BUFFER, bo[2]);
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bo[3]);
+ glBufferData(GL_ARRAY_BUFFER, (long)sizeof(*coord_resized) * nrects_resized * 16,
+ coord_resized, GL_STATIC_DRAW);
+ glBufferData(GL_ELEMENT_ARRAY_BUFFER,
+ (long)sizeof(*indices_resized) * nrects_resized * 6, indices_resized,
+ GL_STATIC_DRAW);
+ glEnableVertexAttribArray(vert_coord_loc);
+ glEnableVertexAttribArray(vert_in_texcoord_loc);
+ glVertexAttribPointer(vert_coord_loc, 2, GL_INT, GL_FALSE, sizeof(GLint) * 4, NULL);
+ glVertexAttribPointer(vert_in_texcoord_loc, 2, GL_INT, GL_FALSE,
+ sizeof(GLint) * 4, (void *)(sizeof(GLint) * 2));
+
+ int vao_nelems[2] = {nrects * 6, nrects_resized * 6};
+
+ if (bctx->method == BLUR_METHOD_DUAL_KAWASE) {
+ ret = gl_dual_kawase_blur(opacity, bctx, extent_resized, mask, mask_dst,
+ vao, vao_nelems, source_texture, source_size,
+ target_fbo, default_mask);
+ } else {
+ ret = gl_kernel_blur(opacity, bctx, extent_resized, mask, mask_dst, vao,
+ vao_nelems, source_texture, source_size, target_fbo,
+ default_mask);
+ }
+
+ glBindFramebuffer(GL_FRAMEBUFFER, 0);
+ glActiveTexture(GL_TEXTURE1);
+ glBindTexture(GL_TEXTURE_2D, 0);
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_2D, 0);
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
+ glDeleteBuffers(4, bo);
+ glBindVertexArray(0);
+ glDeleteVertexArrays(2, vao);
+ glUseProgram(0);
+
+ free(indices);
+ free(coord);
+ free(indices_resized);
+ free(coord_resized);
+
+ gl_check_err();
+ return ret;
+}
+
+bool gl_blur(backend_t *base, double opacity, void *ctx, void *mask, coord_t mask_dst,
+ const region_t *reg_blur, const region_t *reg_visible attr_unused) {
+ auto gd = (struct gl_data *)base;
+ auto bctx = (struct gl_blur_context *)ctx;
+ return gl_blur_impl(opacity, bctx, 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);
+}
+
+static inline void gl_free_blur_shader(gl_blur_shader_t *shader) {
+ if (shader->prog) {
+ glDeleteProgram(shader->prog);
+ }
+
+ shader->prog = 0;
+}
+
+void gl_destroy_blur_context(backend_t *base attr_unused, void *ctx) {
+ auto bctx = (struct gl_blur_context *)ctx;
+ // Free GLSL shaders/programs
+ for (int i = 0; i < bctx->npasses; ++i) {
+ gl_free_blur_shader(&bctx->blur_shader[i]);
+ }
+ free(bctx->blur_shader);
+
+ if (bctx->blur_texture_count && bctx->blur_textures) {
+ glDeleteTextures(bctx->blur_texture_count, bctx->blur_textures);
+ free(bctx->blur_textures);
+ }
+ if (bctx->blur_texture_count && bctx->texture_sizes) {
+ free(bctx->texture_sizes);
+ }
+ if (bctx->blur_fbo_count && bctx->blur_fbos) {
+ glDeleteFramebuffers(bctx->blur_fbo_count, bctx->blur_fbos);
+ free(bctx->blur_fbos);
+ }
+
+ bctx->blur_texture_count = 0;
+ bctx->blur_fbo_count = 0;
+
+ free(bctx);
+
+ gl_check_err();
+}
+
+/**
+ * Initialize GL blur filters.
+ */
+bool gl_create_kernel_blur_context(void *blur_context, GLfloat *projection,
+ enum blur_method method, void *args) {
+ bool success = false;
+ auto ctx = (struct gl_blur_context *)blur_context;
+
+ struct conv **kernels;
+
+ int nkernels;
+ ctx->method = BLUR_METHOD_KERNEL;
+ if (method == BLUR_METHOD_KERNEL) {
+ nkernels = ((struct kernel_blur_args *)args)->kernel_count;
+ kernels = ((struct kernel_blur_args *)args)->kernels;
+ } else {
+ kernels = generate_blur_kernel(method, args, &nkernels);
+ }
+
+ if (!nkernels) {
+ ctx->method = BLUR_METHOD_NONE;
+ return true;
+ }
+
+ // Specify required textures and FBOs
+ ctx->blur_texture_count = 2;
+ ctx->blur_fbo_count = 1;
+
+ ctx->blur_shader = ccalloc(max2(2, nkernels), gl_blur_shader_t);
+
+ char *lc_numeric_old = strdup(setlocale(LC_NUMERIC, NULL));
+ // Enforce LC_NUMERIC locale "C" here to make sure decimal point is sane
+ // Thanks to hiciu for reporting.
+ setlocale(LC_NUMERIC, "C");
+
+ // clang-format off
+ static const char *FRAG_SHADER_BLUR = GLSL(330,
+ %s\n // other extension pragmas
+ uniform sampler2D tex_src;
+ uniform vec2 pixel_norm;
+ uniform float opacity;
+ in vec2 texcoord;
+ out vec4 out_color;
+ float mask_factor();
+ void main() {
+ vec2 uv = texcoord * pixel_norm;
+ vec4 sum = vec4(0.0, 0.0, 0.0, 0.0);
+ %s //body of the convolution
+ out_color = sum / float(%.7g) * opacity * mask_factor();
+ }
+ );
+ static const char *FRAG_SHADER_BLUR_ADD = QUOTE(
+ sum += float(%.7g) * texture2D(tex_src, uv + pixel_norm * vec2(%.7g, %.7g));
+ );
+ // clang-format on
+
+ const char *shader_add = FRAG_SHADER_BLUR_ADD;
+ char *extension = strdup("");
+
+ for (int i = 0; i < nkernels; i++) {
+ auto kern = kernels[i];
+ // Build shader
+ int width = kern->w, height = kern->h;
+ int nele = width * height;
+ // '%.7g' is at most 14 characters, inserted 3 times
+ size_t body_len = (strlen(shader_add) + 42) * (uint)nele;
+ char *shader_body = ccalloc(body_len, char);
+ char *pc = shader_body;
+
+ // Make use of the linear interpolation hardware by sampling 2 pixels with
+ // one texture access by sampling between both pixels based on their
+ // relative weight. Easiest done in a single dimension as 2D bilinear
+ // filtering would raise additional constraints on the kernels. Therefore
+ // only use interpolation along the larger dimension.
+ double sum = 0.0;
+ if (width > height) {
+ // use interpolation in x dimension (width)
+ for (int j = 0; j < height; ++j) {
+ for (int k = 0; k < width; k += 2) {
+ double val1, val2;
+ val1 = kern->data[j * width + k];
+ val2 = (k + 1 < width)
+ ? kern->data[j * width + k + 1]
+ : 0;
+
+ double combined_weight = val1 + val2;
+ if (combined_weight == 0) {
+ continue;
+ }
+ sum += combined_weight;
+
+ double offset_x =
+ k + (val2 / combined_weight) - (width / 2);
+ double offset_y = j - (height / 2);
+ pc += snprintf(
+ pc, body_len - (ulong)(pc - shader_body),
+ shader_add, combined_weight, offset_x, offset_y);
+ assert(pc < shader_body + body_len);
+ }
+ }
+ } else {
+ // use interpolation in y dimension (height)
+ for (int j = 0; j < height; j += 2) {
+ for (int k = 0; k < width; ++k) {
+ double val1, val2;
+ val1 = kern->data[j * width + k];
+ val2 = (j + 1 < height)
+ ? kern->data[(j + 1) * width + k]
+ : 0;
+
+ double combined_weight = val1 + val2;
+ if (combined_weight == 0) {
+ continue;
+ }
+ sum += combined_weight;
+
+ double offset_x = k - (width / 2);
+ double offset_y =
+ j + (val2 / combined_weight) - (height / 2);
+ pc += snprintf(
+ pc, body_len - (ulong)(pc - shader_body),
+ shader_add, combined_weight, offset_x, offset_y);
+ assert(pc < shader_body + body_len);
+ }
+ }
+ }
+
+ auto pass = ctx->blur_shader + i;
+ size_t shader_len = strlen(FRAG_SHADER_BLUR) + strlen(extension) +
+ strlen(shader_body) + 10 /* sum */ +
+ 1 /* null terminator */;
+ char *shader_str = ccalloc(shader_len, char);
+ auto real_shader_len = snprintf(shader_str, shader_len, FRAG_SHADER_BLUR,
+ extension, shader_body, sum);
+ CHECK(real_shader_len >= 0);
+ CHECK((size_t)real_shader_len < shader_len);
+ free(shader_body);
+
+ // Build program
+ pass->prog = gl_create_program_from_strv(
+ (const char *[]){vertex_shader, NULL},
+ (const char *[]){shader_str, masking_glsl, NULL});
+ free(shader_str);
+ if (!pass->prog) {
+ log_error("Failed to create GLSL program.");
+ success = false;
+ goto out;
+ }
+ glBindFragDataLocation(pass->prog, 0, "out_color");
+
+ // Get uniform addresses
+ bind_uniform(pass, pixel_norm);
+ bind_uniform(pass, opacity);
+
+ bind_uniform(pass, mask_tex);
+ bind_uniform(pass, mask_offset);
+ bind_uniform(pass, mask_inverted);
+ bind_uniform(pass, mask_corner_radius);
+ log_info("Uniform locations: %d %d %d %d %d", pass->uniform_mask_tex,
+ pass->uniform_mask_offset, pass->uniform_mask_inverted,
+ pass->uniform_mask_corner_radius, pass->uniform_opacity);
+ pass->texorig_loc = glGetUniformLocationChecked(pass->prog, "texorig");
+
+ // Setup projection matrix
+ glUseProgram(pass->prog);
+ int pml = glGetUniformLocationChecked(pass->prog, "projection");
+ glUniformMatrix4fv(pml, 1, false, projection);
+ glUseProgram(0);
+
+ ctx->resize_width += kern->w / 2;
+ ctx->resize_height += kern->h / 2;
+ }
+
+ if (nkernels == 1) {
+ // Generate an extra null pass so we don't need special code path for
+ // the single pass case
+ auto pass = &ctx->blur_shader[1];
+ pass->prog = gl_create_program_from_strv(
+ (const char *[]){vertex_shader, NULL},
+ (const char *[]){copy_with_mask_frag, masking_glsl, NULL});
+ pass->uniform_pixel_norm = -1;
+ pass->uniform_opacity = -1;
+ pass->texorig_loc = glGetUniformLocationChecked(pass->prog, "texorig");
+ bind_uniform(pass, mask_tex);
+ bind_uniform(pass, mask_offset);
+ bind_uniform(pass, mask_inverted);
+ bind_uniform(pass, mask_corner_radius);
+
+ // Setup projection matrix
+ glUseProgram(pass->prog);
+ int pml = glGetUniformLocationChecked(pass->prog, "projection");
+ glUniformMatrix4fv(pml, 1, false, projection);
+ glUseProgram(0);
+
+ ctx->npasses = 2;
+ } else {
+ ctx->npasses = nkernels;
+ }
+
+ success = true;
+out:
+ if (method != BLUR_METHOD_KERNEL) {
+ // We generated the blur kernels, so we need to free them
+ for (int i = 0; i < nkernels; i++) {
+ free(kernels[i]);
+ }
+ free(kernels);
+ }
+
+ free(extension);
+ // Restore LC_NUMERIC
+ setlocale(LC_NUMERIC, lc_numeric_old);
+ free(lc_numeric_old);
+
+ return success;
+}
+
+bool gl_create_dual_kawase_blur_context(void *blur_context, GLfloat *projection,
+ enum blur_method method, void *args) {
+ bool success = false;
+ auto ctx = (struct gl_blur_context *)blur_context;
+
+ ctx->method = method;
+
+ auto blur_params = generate_dual_kawase_params(args);
+
+ // Specify required textures and FBOs
+ ctx->blur_texture_count = blur_params->iterations;
+ ctx->blur_fbo_count = blur_params->iterations;
+
+ ctx->resize_width += blur_params->expand;
+ ctx->resize_height += blur_params->expand;
+
+ ctx->npasses = 2;
+ ctx->blur_shader = ccalloc(ctx->npasses, gl_blur_shader_t);
+
+ char *lc_numeric_old = strdup(setlocale(LC_NUMERIC, NULL));
+ // Enforce LC_NUMERIC locale "C" here to make sure decimal point is sane
+ // Thanks to hiciu for reporting.
+ setlocale(LC_NUMERIC, "C");
+
+ // Dual-kawase downsample shader / program
+ auto down_pass = ctx->blur_shader;
+ {
+ // clang-format off
+ static const char *FRAG_SHADER_DOWN = GLSL(330,
+ uniform sampler2D tex_src;
+ uniform float scale = 1.0;
+ uniform vec2 pixel_norm;
+ in vec2 texcoord;
+ out vec4 out_color;
+ void main() {
+ vec2 offset = %.7g * pixel_norm;
+ vec2 uv = texcoord * pixel_norm * (2.0 / scale);
+ vec4 sum = texture2D(tex_src, uv) * 4.0;
+ sum += texture2D(tex_src, uv - vec2(0.5, 0.5) * offset);
+ sum += texture2D(tex_src, uv + vec2(0.5, 0.5) * offset);
+ sum += texture2D(tex_src, uv + vec2(0.5, -0.5) * offset);
+ sum += texture2D(tex_src, uv - vec2(0.5, -0.5) * offset);
+ out_color = sum / 8.0;
+ }
+ );
+ // clang-format on
+
+ // Build shader
+ size_t shader_len =
+ strlen(FRAG_SHADER_DOWN) + 10 /* offset */ + 1 /* null terminator */;
+ char *shader_str = ccalloc(shader_len, char);
+ auto real_shader_len =
+ snprintf(shader_str, shader_len, FRAG_SHADER_DOWN, blur_params->offset);
+ CHECK(real_shader_len >= 0);
+ CHECK((size_t)real_shader_len < shader_len);
+
+ // Build program
+ down_pass->prog = gl_create_program_from_str(vertex_shader, shader_str);
+ free(shader_str);
+ if (!down_pass->prog) {
+ log_error("Failed to create GLSL program.");
+ success = false;
+ goto out;
+ }
+ glBindFragDataLocation(down_pass->prog, 0, "out_color");
+
+ // Get uniform addresses
+ bind_uniform(down_pass, pixel_norm);
+ down_pass->texorig_loc =
+ glGetUniformLocationChecked(down_pass->prog, "texorig");
+ down_pass->scale_loc =
+ glGetUniformLocationChecked(down_pass->prog, "scale");
+
+ // Setup projection matrix
+ glUseProgram(down_pass->prog);
+ int pml = glGetUniformLocationChecked(down_pass->prog, "projection");
+ glUniformMatrix4fv(pml, 1, false, projection);
+ glUseProgram(0);
+ }
+
+ // Dual-kawase upsample shader / program
+ auto up_pass = ctx->blur_shader + 1;
+ {
+ // clang-format off
+ static const char *FRAG_SHADER_UP = GLSL(330,
+ uniform sampler2D tex_src;
+ uniform float scale = 1.0;
+ uniform vec2 pixel_norm;
+ uniform float opacity;
+ in vec2 texcoord;
+ out vec4 out_color;
+ float mask_factor();
+ void main() {
+ vec2 offset = %.7g * pixel_norm;
+ vec2 uv = texcoord * pixel_norm / (2 * scale);
+ vec4 sum = texture2D(tex_src, uv + vec2(-1.0, 0.0) * offset);
+ sum += texture2D(tex_src, uv + vec2(-0.5, 0.5) * offset) * 2.0;
+ sum += texture2D(tex_src, uv + vec2(0.0, 1.0) * offset);
+ sum += texture2D(tex_src, uv + vec2(0.5, 0.5) * offset) * 2.0;
+ sum += texture2D(tex_src, uv + vec2(1.0, 0.0) * offset);
+ sum += texture2D(tex_src, uv + vec2(0.5, -0.5) * offset) * 2.0;
+ sum += texture2D(tex_src, uv + vec2(0.0, -1.0) * offset);
+ sum += texture2D(tex_src, uv + vec2(-0.5, -0.5) * offset) * 2.0;
+ out_color = sum / 12.0 * opacity * mask_factor();
+ }
+ );
+ // clang-format on
+
+ // Build shader
+ size_t shader_len =
+ strlen(FRAG_SHADER_UP) + 10 /* offset */ + 1 /* null terminator */;
+ char *shader_str = ccalloc(shader_len, char);
+ auto real_shader_len =
+ snprintf(shader_str, shader_len, FRAG_SHADER_UP, blur_params->offset);
+ CHECK(real_shader_len >= 0);
+ CHECK((size_t)real_shader_len < shader_len);
+
+ // Build program
+ up_pass->prog = gl_create_program_from_strv(
+ (const char *[]){vertex_shader, NULL},
+ (const char *[]){shader_str, masking_glsl, NULL});
+ free(shader_str);
+ if (!up_pass->prog) {
+ log_error("Failed to create GLSL program.");
+ success = false;
+ goto out;
+ }
+ glBindFragDataLocation(up_pass->prog, 0, "out_color");
+
+ // Get uniform addresses
+ bind_uniform(up_pass, pixel_norm);
+ bind_uniform(up_pass, opacity);
+
+ bind_uniform(up_pass, mask_tex);
+ bind_uniform(up_pass, mask_offset);
+ bind_uniform(up_pass, mask_inverted);
+ bind_uniform(up_pass, mask_corner_radius);
+
+ up_pass->texorig_loc =
+ glGetUniformLocationChecked(up_pass->prog, "texorig");
+ up_pass->scale_loc = glGetUniformLocationChecked(up_pass->prog, "scale");
+
+ // Setup projection matrix
+ glUseProgram(up_pass->prog);
+ int pml = glGetUniformLocationChecked(up_pass->prog, "projection");
+ glUniformMatrix4fv(pml, 1, false, projection);
+ glUseProgram(0);
+ }
+
+ success = true;
+out:
+ free(blur_params);
+
+ if (!success) {
+ ctx = NULL;
+ }
+
+ // Restore LC_NUMERIC
+ setlocale(LC_NUMERIC, lc_numeric_old);
+ free(lc_numeric_old);
+
+ return success;
+}
+
+void *gl_create_blur_context(backend_t *base, enum blur_method method, void *args) {
+ bool success;
+ auto gd = (struct gl_data *)base;
+
+ auto ctx = ccalloc(1, struct gl_blur_context);
+
+ if (!method || method >= BLUR_METHOD_INVALID) {
+ ctx->method = BLUR_METHOD_NONE;
+ return ctx;
+ }
+
+ // Set projection matrix to gl viewport dimensions so we can use screen
+ // coordinates for all vertices
+ // Note: OpenGL matrices are column major
+ GLint viewport_dimensions[2];
+ glGetIntegerv(GL_MAX_VIEWPORT_DIMS, viewport_dimensions);
+ GLfloat projection_matrix[4][4] = {{2.0F / (GLfloat)viewport_dimensions[0], 0, 0, 0},
+ {0, 2.0F / (GLfloat)viewport_dimensions[1], 0, 0},
+ {0, 0, 0, 0},
+ {-1, -1, 0, 1}};
+
+ if (method == BLUR_METHOD_DUAL_KAWASE) {
+ success = gl_create_dual_kawase_blur_context(ctx, projection_matrix[0],
+ method, args);
+ } else {
+ success =
+ gl_create_kernel_blur_context(ctx, projection_matrix[0], method, args);
+ }
+ if (!success || ctx->method == BLUR_METHOD_NONE) {
+ goto out;
+ }
+
+ // 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);
+ glGenTextures(ctx->blur_texture_count, ctx->blur_textures);
+
+ for (int i = 0; i < ctx->blur_texture_count; ++i) {
+ glBindTexture(GL_TEXTURE_2D, ctx->blur_textures[i]);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+ }
+
+ // Generate FBO and textures when needed
+ ctx->blur_fbos = ccalloc(ctx->blur_fbo_count, GLuint);
+ glGenFramebuffers(ctx->blur_fbo_count, ctx->blur_fbos);
+
+ for (int i = 0; i < ctx->blur_fbo_count; ++i) {
+ if (!ctx->blur_fbos[i]) {
+ log_error("Failed to generate framebuffer objects for blur");
+ success = false;
+ goto out;
+ }
+ }
+
+out:
+ if (!success) {
+ gl_destroy_blur_context(&gd->base, ctx);
+ ctx = NULL;
+ }
+
+ gl_check_err();
+ return ctx;
+}
+
+void gl_get_blur_size(void *blur_context, int *width, int *height) {
+ auto ctx = (struct gl_blur_context *)blur_context;
+ *width = ctx->resize_width;
+ *height = ctx->resize_height;
+}
diff --git a/src/backend/gl/egl.c b/src/backend/gl/egl.c
new file mode 100644
index 0000000..e6d4d90
--- /dev/null
+++ b/src/backend/gl/egl.c
@@ -0,0 +1,469 @@
+// SPDX-License-Identifier: MPL-2.0
+/*
+ * Copyright (c) 2022 Yuxuan Shui <[email protected]>
+ */
+
+#include <X11/Xlib-xcb.h>
+#include <assert.h>
+#include <limits.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
+#include <xcb/xcb.h>
+
+#include "backend/backend.h"
+#include "backend/backend_common.h"
+#include "backend/gl/egl.h"
+#include "backend/gl/gl_common.h"
+#include "common.h"
+#include "compiler.h"
+#include "config.h"
+#include "log.h"
+#include "picom.h"
+#include "utils.h"
+#include "x.h"
+
+struct egl_pixmap {
+ EGLImage image;
+ xcb_pixmap_t pixmap;
+ bool owned;
+};
+
+struct egl_data {
+ struct gl_data gl;
+ EGLDisplay display;
+ EGLSurface target_win;
+ EGLContext ctx;
+};
+
+static PFNGLEGLIMAGETARGETTEXSTORAGEEXTPROC glEGLImageTargetTexStorage = NULL;
+static PFNEGLCREATEIMAGEKHRPROC eglCreateImageProc = NULL;
+static PFNEGLDESTROYIMAGEKHRPROC eglDestroyImageProc = NULL;
+static PFNEGLGETPLATFORMDISPLAYPROC eglGetPlatformDisplayProc = NULL;
+static PFNEGLCREATEPLATFORMWINDOWSURFACEPROC eglCreatePlatformWindowSurfaceProc = NULL;
+
+/**
+ * Free a glx_texture_t.
+ */
+static void egl_release_image(backend_t *base, struct gl_texture *tex) {
+ struct egl_data *gd = (void *)base;
+ struct egl_pixmap *p = tex->user_data;
+ // Release binding
+ if (p->image != EGL_NO_IMAGE) {
+ eglDestroyImageProc(gd->display, p->image);
+ p->image = EGL_NO_IMAGE;
+ }
+
+ if (p->owned) {
+ xcb_free_pixmap(base->c, p->pixmap);
+ p->pixmap = XCB_NONE;
+ }
+
+ free(p);
+ tex->user_data = NULL;
+}
+
+/**
+ * Destroy GLX related resources.
+ */
+void egl_deinit(backend_t *base) {
+ struct egl_data *gd = (void *)base;
+
+ gl_deinit(&gd->gl);
+
+ // Destroy GLX context
+ if (gd->ctx != EGL_NO_CONTEXT) {
+ eglMakeCurrent(gd->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
+ eglDestroyContext(gd->display, gd->ctx);
+ gd->ctx = EGL_NO_CONTEXT;
+ }
+
+ if (gd->target_win != EGL_NO_SURFACE) {
+ eglDestroySurface(gd->display, gd->target_win);
+ gd->target_win = EGL_NO_SURFACE;
+ }
+
+ if (gd->display != EGL_NO_DISPLAY) {
+ eglTerminate(gd->display);
+ gd->display = EGL_NO_DISPLAY;
+ }
+
+ free(gd);
+}
+
+static void *egl_decouple_user_data(backend_t *base attr_unused, void *ud attr_unused) {
+ auto ret = cmalloc(struct egl_pixmap);
+ ret->owned = false;
+ ret->image = EGL_NO_IMAGE;
+ ret->pixmap = 0;
+ return ret;
+}
+
+static bool egl_set_swap_interval(int interval, EGLDisplay dpy) {
+ return eglSwapInterval(dpy, interval);
+}
+
+/**
+ * Initialize OpenGL.
+ */
+static backend_t *egl_init(session_t *ps) {
+ bool success = false;
+ struct egl_data *gd = NULL;
+
+#define get_proc(name, type) \
+ name##Proc = (type)eglGetProcAddress(#name); \
+ if (!name##Proc) { \
+ log_error("Failed to get EGL function " #name); \
+ goto end; \
+ }
+ get_proc(eglCreateImage, PFNEGLCREATEIMAGEKHRPROC);
+ get_proc(eglDestroyImage, PFNEGLDESTROYIMAGEKHRPROC);
+ get_proc(eglGetPlatformDisplay, PFNEGLGETPLATFORMDISPLAYPROC);
+ get_proc(eglCreatePlatformWindowSurface, PFNEGLCREATEPLATFORMWINDOWSURFACEPROC);
+#undef get_proc
+
+ // Check if we have the X11 platform
+ const char *exts = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
+ if (strstr(exts, "EGL_EXT_platform_x11") == NULL) {
+ log_error("X11 platform not available.");
+ return NULL;
+ }
+
+ gd = ccalloc(1, struct egl_data);
+ gd->display = eglGetPlatformDisplayProc(EGL_PLATFORM_X11_EXT, ps->dpy,
+ (EGLAttrib[]){
+ EGL_PLATFORM_X11_SCREEN_EXT,
+ ps->scr,
+ EGL_NONE,
+ });
+ if (gd->display == EGL_NO_DISPLAY) {
+ log_error("Failed to get EGL display.");
+ goto end;
+ }
+
+ EGLint major, minor;
+ if (!eglInitialize(gd->display, &major, &minor)) {
+ log_error("Failed to initialize EGL.");
+ goto end;
+ }
+
+ if (major < 1 || (major == 1 && minor < 5)) {
+ log_error("EGL version too old, need at least 1.5.");
+ goto end;
+ }
+
+ // Check if EGL supports OpenGL
+ const char *apis = eglQueryString(gd->display, EGL_CLIENT_APIS);
+ if (strstr(apis, "OpenGL") == NULL) {
+ log_error("EGL does not support OpenGL.");
+ goto end;
+ }
+
+ eglext_init(gd->display);
+ init_backend_base(&gd->gl.base, ps);
+ if (!eglext.has_EGL_KHR_image_pixmap) {
+ log_error("EGL_KHR_image_pixmap not available.");
+ goto end;
+ }
+
+ int ncfgs = 0;
+ if (eglGetConfigs(gd->display, NULL, 0, &ncfgs) != EGL_TRUE) {
+ log_error("Failed to get EGL configs.");
+ goto end;
+ }
+
+ auto visual_info = x_get_visual_info(ps->c, ps->vis);
+ EGLConfig *cfgs = ccalloc(ncfgs, EGLConfig);
+ // clang-format off
+ if (eglChooseConfig(gd->display,
+ (EGLint[]){
+ EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
+ EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
+ EGL_RED_SIZE, visual_info.red_size,
+ EGL_GREEN_SIZE, visual_info.green_size,
+ EGL_BLUE_SIZE, visual_info.blue_size,
+ EGL_ALPHA_SIZE, visual_info.alpha_size,
+ EGL_STENCIL_SIZE, 1,
+ EGL_CONFIG_CAVEAT, EGL_NONE,
+ EGL_NONE,
+ }, cfgs, ncfgs, &ncfgs) != EGL_TRUE) {
+ log_error("Failed to choose EGL config for the root window.");
+ goto end;
+ }
+ // clang-format on
+
+ EGLConfig target_cfg = cfgs[0];
+ free(cfgs);
+
+ gd->target_win = eglCreatePlatformWindowSurfaceProc(
+ gd->display, target_cfg, (xcb_window_t[]){session_get_target_window(ps)}, NULL);
+ if (gd->target_win == EGL_NO_SURFACE) {
+ log_error("Failed to create EGL surface.");
+ goto end;
+ }
+
+ if (eglBindAPI(EGL_OPENGL_API) != EGL_TRUE) {
+ log_error("Failed to bind OpenGL API.");
+ goto end;
+ }
+
+ gd->ctx = eglCreateContext(gd->display, target_cfg, NULL, NULL);
+ if (gd->ctx == EGL_NO_CONTEXT) {
+ log_error("Failed to get GLX context.");
+ goto end;
+ }
+
+ if (!eglMakeCurrent(gd->display, gd->target_win, gd->target_win, gd->ctx)) {
+ log_error("Failed to attach GLX context.");
+ goto end;
+ }
+
+ if (!gl_init(&gd->gl, ps)) {
+ log_error("Failed to setup OpenGL");
+ goto end;
+ }
+ if (!gd->gl.has_egl_image_storage) {
+ log_error("GL_EXT_EGL_image_storage extension not available.");
+ goto end;
+ }
+
+ glEGLImageTargetTexStorage =
+ (PFNGLEGLIMAGETARGETTEXSTORAGEEXTPROC)eglGetProcAddress("glEGLImageTargetTexS"
+ "torageEXT");
+ if (glEGLImageTargetTexStorage == NULL) {
+ log_error("Failed to get glEGLImageTargetTexStorageEXT.");
+ goto end;
+ }
+
+ gd->gl.decouple_texture_user_data = egl_decouple_user_data;
+ gd->gl.release_user_data = egl_release_image;
+
+ if (ps->o.vsync) {
+ if (!egl_set_swap_interval(1, gd->display)) {
+ log_error("Failed to enable vsync. %#x", eglGetError());
+ }
+ } else {
+ egl_set_swap_interval(0, gd->display);
+ }
+
+ success = true;
+
+end:
+ if (!success) {
+ if (gd != NULL) {
+ egl_deinit(&gd->gl.base);
+ }
+ return NULL;
+ }
+
+ return &gd->gl.base;
+}
+
+static void *
+egl_bind_pixmap(backend_t *base, xcb_pixmap_t pixmap, struct xvisual_info fmt, bool owned) {
+ struct egl_data *gd = (void *)base;
+ struct egl_pixmap *eglpixmap = NULL;
+
+ auto r = xcb_get_geometry_reply(base->c, xcb_get_geometry(base->c, pixmap), NULL);
+ if (!r) {
+ log_error("Invalid pixmap %#010x", pixmap);
+ return NULL;
+ }
+
+ log_trace("Binding pixmap %#010x", pixmap);
+ auto wd = ccalloc(1, struct backend_image);
+ wd->max_brightness = 1;
+ auto inner = ccalloc(1, struct gl_texture);
+ inner->width = wd->ewidth = r->width;
+ inner->height = wd->eheight = r->height;
+ wd->inner = (struct backend_image_inner_base *)inner;
+ free(r);
+
+ log_debug("depth %d", fmt.visual_depth);
+
+ inner->y_inverted = true;
+
+ eglpixmap = cmalloc(struct egl_pixmap);
+ eglpixmap->pixmap = pixmap;
+ eglpixmap->image = eglCreateImageProc(gd->display, gd->ctx, EGL_NATIVE_PIXMAP_KHR,
+ (EGLClientBuffer)(uintptr_t)pixmap, NULL);
+ eglpixmap->owned = owned;
+
+ if (eglpixmap->image == EGL_NO_IMAGE) {
+ log_error("Failed to create eglpixmap for pixmap %#010x", pixmap);
+ goto err;
+ }
+
+ log_trace("EGLImage %p", eglpixmap->image);
+
+ // Create texture
+ inner->user_data = eglpixmap;
+ inner->texture = gl_new_texture(GL_TEXTURE_2D);
+ inner->has_alpha = fmt.alpha_size != 0;
+ wd->opacity = 1;
+ wd->color_inverted = false;
+ wd->dim = 0;
+ wd->inner->refcount = 1;
+ glBindTexture(GL_TEXTURE_2D, inner->texture);
+ glEGLImageTargetTexStorage(GL_TEXTURE_2D, eglpixmap->image, NULL);
+ glBindTexture(GL_TEXTURE_2D, 0);
+
+ gl_check_err();
+ return wd;
+err:
+ if (eglpixmap && eglpixmap->image) {
+ eglDestroyImageProc(gd->display, eglpixmap->image);
+ }
+ free(eglpixmap);
+
+ if (owned) {
+ xcb_free_pixmap(base->c, pixmap);
+ }
+ free(wd);
+ return NULL;
+}
+
+static void egl_present(backend_t *base, const region_t *region attr_unused) {
+ struct egl_data *gd = (void *)base;
+ gl_present(base, region);
+ eglSwapBuffers(gd->display, gd->target_win);
+ if (!gd->gl.is_nvidia) {
+ glFinish();
+ }
+}
+
+static int egl_buffer_age(backend_t *base) {
+ if (!eglext.has_EGL_EXT_buffer_age) {
+ return -1;
+ }
+
+ struct egl_data *gd = (void *)base;
+ EGLint val;
+ eglQuerySurface(gd->display, (EGLSurface)gd->target_win, EGL_BUFFER_AGE_EXT, &val);
+ return (int)val ?: -1;
+}
+
+static void egl_diagnostics(backend_t *base) {
+ struct egl_data *gd = (void *)base;
+ bool warn_software_rendering = false;
+ const char *software_renderer_names[] = {"llvmpipe", "SWR", "softpipe"};
+ auto egl_vendor = eglQueryString(gd->display, EGL_VENDOR);
+ printf("* Driver vendors:\n");
+ printf(" * EGL: %s\n", egl_vendor);
+ if (eglext.has_EGL_MESA_query_driver) {
+ printf(" * EGL driver: %s\n", eglGetDisplayDriverName(gd->display));
+ }
+ printf(" * GL: %s\n", glGetString(GL_VENDOR));
+
+ auto gl_renderer = (const char *)glGetString(GL_RENDERER);
+ printf("* GL renderer: %s\n", gl_renderer);
+ if (strstr(egl_vendor, "Mesa")) {
+ for (size_t i = 0; i < ARR_SIZE(software_renderer_names); i++) {
+ if (strstr(gl_renderer, software_renderer_names[i]) != NULL) {
+ warn_software_rendering = true;
+ break;
+ }
+ }
+ }
+
+ if (warn_software_rendering) {
+ printf("\n(You are using a software renderer. Unless you are doing this\n"
+ "intentionally, this means you don't have a graphics driver\n"
+ "properly installed. Performance will suffer. Please fix this\n"
+ "before reporting your issue.)\n");
+ }
+}
+
+struct backend_operations egl_ops = {
+ .init = egl_init,
+ .deinit = egl_deinit,
+ .bind_pixmap = egl_bind_pixmap,
+ .release_image = gl_release_image,
+ .compose = gl_compose,
+ .image_op = gl_image_op,
+ .set_image_property = gl_set_image_property,
+ .clone_image = default_clone_image,
+ .blur = gl_blur,
+ .is_image_transparent = default_is_image_transparent,
+ .present = egl_present,
+ .buffer_age = egl_buffer_age,
+ .create_shadow_context = gl_create_shadow_context,
+ .destroy_shadow_context = gl_destroy_shadow_context,
+ .render_shadow = backend_render_shadow_from_mask,
+ .shadow_from_mask = gl_shadow_from_mask,
+ .make_mask = gl_make_mask,
+ .fill = gl_fill,
+ .create_blur_context = gl_create_blur_context,
+ .destroy_blur_context = gl_destroy_blur_context,
+ .get_blur_size = gl_get_blur_size,
+ .diagnostics = egl_diagnostics,
+ .device_status = gl_device_status,
+ .create_shader = gl_create_window_shader,
+ .destroy_shader = gl_destroy_window_shader,
+ .get_shader_attributes = gl_get_shader_attributes,
+ .max_buffer_age = 5, // Why?
+};
+
+PFNEGLGETDISPLAYDRIVERNAMEPROC eglGetDisplayDriverName;
+/**
+ * Check if a GLX extension exists.
+ */
+static inline bool egl_has_extension(EGLDisplay dpy, const char *ext) {
+ const char *egl_exts = eglQueryString(dpy, EGL_EXTENSIONS);
+ if (!egl_exts) {
+ log_error("Failed get EGL extension list.");
+ return false;
+ }
+
+ auto inlen = strlen(ext);
+ const char *curr = egl_exts;
+ bool match = false;
+ while (curr && !match) {
+ const char *end = strchr(curr, ' ');
+ if (!end) {
+ // Last extension string
+ match = strcmp(ext, curr) == 0;
+ } else if (curr + inlen == end) {
+ // Length match, do match string
+ match = strncmp(ext, curr, (unsigned long)(end - curr)) == 0;
+ }
+ curr = end ? end + 1 : NULL;
+ }
+
+ if (!match) {
+ log_info("Missing EGL extension %s.", ext);
+ } else {
+ log_info("Found EGL extension %s.", ext);
+ }
+
+ return match;
+}
+
+struct eglext_info eglext = {0};
+
+void eglext_init(EGLDisplay dpy) {
+ if (eglext.initialized) {
+ return;
+ }
+ eglext.initialized = true;
+#define check_ext(name) eglext.has_##name = egl_has_extension(dpy, #name)
+ check_ext(EGL_EXT_buffer_age);
+ check_ext(EGL_EXT_create_context_robustness);
+ check_ext(EGL_KHR_image_pixmap);
+#ifdef EGL_MESA_query_driver
+ check_ext(EGL_MESA_query_driver);
+#endif
+#undef check_ext
+
+ // Checking if the returned function pointer is NULL is not really necessary,
+ // or maybe not even useful, since eglGetProcAddress might always return
+ // something. We are doing it just for completeness' sake.
+
+#ifdef EGL_MESA_query_driver
+ eglGetDisplayDriverName =
+ (PFNEGLGETDISPLAYDRIVERNAMEPROC)eglGetProcAddress("eglGetDisplayDriverName");
+ if (!eglGetDisplayDriverName) {
+ eglext.has_EGL_MESA_query_driver = false;
+ }
+#endif
+}
diff --git a/src/backend/gl/egl.h b/src/backend/gl/egl.h
new file mode 100644
index 0000000..171b173
--- /dev/null
+++ b/src/backend/gl/egl.h
@@ -0,0 +1,33 @@
+// SPDX-License-Identifier: MPL-2.0
+// Copyright (c) Yuxuan Shui <[email protected]>
+#pragma once
+#include <stdbool.h>
+// Older version of glx.h defines function prototypes for these extensions...
+// Rename them to avoid conflicts
+#include <EGL/egl.h>
+#include <EGL/eglext.h>
+#include <GL/gl.h>
+#include <GL/glext.h>
+#include <xcb/render.h>
+#include <xcb/xcb.h>
+
+#include "compiler.h"
+#include "log.h"
+#include "utils.h"
+#include "x.h"
+
+struct eglext_info {
+ bool initialized;
+ bool has_EGL_MESA_query_driver;
+ bool has_EGL_EXT_buffer_age;
+ bool has_EGL_EXT_create_context_robustness;
+ bool has_EGL_KHR_image_pixmap;
+};
+
+extern struct eglext_info eglext;
+
+#ifdef EGL_MESA_query_driver
+extern PFNEGLGETDISPLAYDRIVERNAMEPROC eglGetDisplayDriverName;
+#endif
+
+void eglext_init(EGLDisplay);
diff --git a/src/backend/gl/gl_common.c b/src/backend/gl/gl_common.c
index 8cc5a05..a7d2aab 100644
--- a/src/backend/gl/gl_common.c
+++ b/src/backend/gl/gl_common.c
@@ -2,10 +2,10 @@
// Copyright (c) Yuxuan Shui <[email protected]>
#include <GL/gl.h>
#include <GL/glext.h>
-#include <locale.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
+#include <time.h>
#include <xcb/render.h> // for xcb_render_fixed_t, XXX
#include "backend/backend.h"
@@ -22,51 +22,6 @@
#include "backend/backend_common.h"
#include "backend/gl/gl_common.h"
-#define GLSL(version, ...) "#version " #version "\n" #__VA_ARGS__
-#define QUOTE(...) #__VA_ARGS__
-
-static const GLuint vert_coord_loc = 0;
-static const GLuint vert_in_texcoord_loc = 1;
-
-struct gl_blur_context {
- enum blur_method method;
- gl_blur_shader_t *blur_shader;
-
- /// Temporary textures used for blurring
- GLuint *blur_textures;
- int blur_texture_count;
- /// Temporary fbos used for blurring
- GLuint *blur_fbos;
- int blur_fbo_count;
-
- /// Cached dimensions of each blur_texture. They are the same size as the target,
- /// so they are always big enough without resizing.
- /// Turns out calling glTexImage to resize is expensive, so we avoid that.
- struct texture_size {
- int width;
- int height;
- } * texture_sizes;
-
- /// Cached dimensions of the offscreen framebuffer. It's the same size as the
- /// target but is expanded in either direction by resize_width / resize_height.
- int fb_width, fb_height;
-
- /// How much do we need to resize the damaged region for blurring.
- int resize_width, resize_height;
-
- int npasses;
-};
-
-static GLint glGetUniformLocationChecked(GLuint p, const char *name) {
- auto ret = glGetUniformLocation(p, name);
- if (ret < 0) {
- log_info("Failed to get location of uniform '%s'. This is normal when "
- "using custom shaders.",
- name);
- }
- return ret;
-}
-
GLuint gl_create_shader(GLenum shader_type, const char *shader_str) {
log_trace("===\n%s\n===", shader_str);
@@ -83,7 +38,7 @@ GLuint gl_create_shader(GLenum shader_type, const char *shader_str) {
{
GLint status = GL_FALSE;
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
- if (GL_FALSE == status) {
+ if (status == GL_FALSE) {
GLint log_len = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_len);
if (log_len) {
@@ -103,6 +58,7 @@ end:
glDeleteShader(shader);
shader = 0;
}
+ gl_check_err();
return shader;
}
@@ -115,15 +71,16 @@ GLuint gl_create_program(const GLuint *const shaders, int nshaders) {
goto end;
}
- for (int i = 0; i < nshaders; ++i)
+ for (int i = 0; i < nshaders; ++i) {
glAttachShader(program, shaders[i]);
+ }
glLinkProgram(program);
// Get program status
{
GLint status = GL_FALSE;
glGetProgramiv(program, GL_LINK_STATUS, &status);
- if (GL_FALSE == status) {
+ if (status == GL_FALSE) {
GLint log_len = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &log_len);
if (log_len) {
@@ -138,59 +95,83 @@ GLuint gl_create_program(const GLuint *const shaders, int nshaders) {
end:
if (program) {
- for (int i = 0; i < nshaders; ++i)
+ for (int i = 0; i < nshaders; ++i) {
glDetachShader(program, shaders[i]);
+ }
}
if (program && !success) {
glDeleteProgram(program);
program = 0;
}
+ gl_check_err();
return program;
}
/**
- * @brief Create a program from vertex and fragment shader strings.
+ * @brief Create a program from NULL-terminated arrays of vertex and fragment shader
+ * strings.
*/
-GLuint gl_create_program_from_str(const char *vert_shader_str, const char *frag_shader_str) {
- GLuint vert_shader = 0;
- GLuint frag_shader = 0;
- GLuint prog = 0;
-
- if (vert_shader_str)
- vert_shader = gl_create_shader(GL_VERTEX_SHADER, vert_shader_str);
- if (frag_shader_str)
- frag_shader = gl_create_shader(GL_FRAGMENT_SHADER, frag_shader_str);
+GLuint gl_create_program_from_strv(const char **vert_shaders, const char **frag_shaders) {
+ int vert_count, frag_count;
+ for (vert_count = 0; vert_shaders && vert_shaders[vert_count]; ++vert_count) {
+ }
+ for (frag_count = 0; frag_shaders && frag_shaders[frag_count]; ++frag_count) {
+ }
- {
- GLuint shaders[2];
- int count = 0;
- if (vert_shader) {
- shaders[count++] = vert_shader;
- }
- if (frag_shader) {
- shaders[count++] = frag_shader;
+ GLuint prog = 0;
+ auto shaders = (GLuint *)ccalloc(vert_count + frag_count, GLuint);
+ for (int i = 0; i < vert_count; ++i) {
+ shaders[i] = gl_create_shader(GL_VERTEX_SHADER, vert_shaders[i]);
+ if (shaders[i] == 0) {
+ goto out;
}
- if (count) {
- prog = gl_create_program(shaders, count);
+ }
+ for (int i = 0; i < frag_count; ++i) {
+ shaders[vert_count + i] =
+ gl_create_shader(GL_FRAGMENT_SHADER, frag_shaders[i]);
+ if (shaders[vert_count + i] == 0) {
+ goto out;
}
}
- if (vert_shader)
- glDeleteShader(vert_shader);
- if (frag_shader)
- glDeleteShader(frag_shader);
+ prog = gl_create_program(shaders, vert_count + frag_count);
+
+out:
+ for (int i = 0; i < vert_count + frag_count; ++i) {
+ if (shaders[i] != 0) {
+ glDeleteShader(shaders[i]);
+ }
+ }
+ free(shaders);
+ gl_check_err();
return prog;
}
-static void gl_free_prog_main(gl_win_shader_t *pprogram) {
- if (!pprogram)
+/**
+ * @brief Create a program from vertex and fragment shader strings.
+ */
+GLuint gl_create_program_from_str(const char *vert_shader_str, const char *frag_shader_str) {
+ const char *vert_shaders[2] = {vert_shader_str, NULL};
+ const char *frag_shaders[2] = {frag_shader_str, NULL};
+
+ return gl_create_program_from_strv(vert_shaders, frag_shaders);
+}
+
+void gl_destroy_window_shader(backend_t *backend_data attr_unused, void *shader) {
+ if (!shader) {
return;
+ }
+
+ auto pprogram = (gl_win_shader_t *)shader;
if (pprogram->prog) {
glDeleteProgram(pprogram->prog);
pprogram->prog = 0;
}
+ gl_check_err();
+
+ free(shader);
}
/*
@@ -368,9 +349,15 @@ static GLuint gl_average_texture_color(backend_t *base, struct backend_image *im
* @param reg_visible ignored
*/
static void _gl_compose(backend_t *base, struct backend_image *img, GLuint target,
- GLint *coord, GLuint *indices, int nrects) {
+ struct backend_image *mask, coord_t mask_offset, GLint *coord,
+ GLuint *indices, int nrects) {
+ // FIXME(yshui) breaks when `mask` and `img` doesn't have the same y_inverted
+ // value. but we don't ever hit this problem because all of our
+ // images and masks are y_inverted.
auto gd = (struct gl_data *)base;
auto inner = (struct gl_texture *)img->inner;
+ auto mask_texture =
+ mask ? ((struct gl_texture *)mask->inner)->texture : gd->default_mask_texture;
if (!img || !inner->texture) {
log_error("Missing texture.");
return;
@@ -381,27 +368,64 @@ static void _gl_compose(backend_t *base, struct backend_image *img, GLuint targe
brightness = gl_average_texture_color(base, img);
}
- assert(gd->win_shader.prog);
- glUseProgram(gd->win_shader.prog);
- if (gd->win_shader.unifm_opacity >= 0) {
- glUniform1f(gd->win_shader.unifm_opacity, (float)img->opacity);
+ auto win_shader = inner->shader;
+ if (!win_shader) {
+ win_shader = gd->default_shader;
}
- if (gd->win_shader.unifm_invert_color >= 0) {
- glUniform1i(gd->win_shader.unifm_invert_color, img->color_inverted);
+
+ assert(win_shader);
+ assert(win_shader->prog);
+ glUseProgram(win_shader->prog);
+ if (win_shader->uniform_opacity >= 0) {
+ glUniform1f(win_shader->uniform_opacity, (float)img->opacity);
+ }
+ if (win_shader->uniform_invert_color >= 0) {
+ glUniform1i(win_shader->uniform_invert_color, img->color_inverted);
+ }
+ if (win_shader->uniform_tex >= 0) {
+ glUniform1i(win_shader->uniform_tex, 0);
}
- if (gd->win_shader.unifm_tex >= 0) {
- glUniform1i(gd->win_shader.unifm_tex, 0);
+ if (win_shader->uniform_dim >= 0) {
+ glUniform1f(win_shader->uniform_dim, (float)img->dim);
}
- if (gd->win_shader.unifm_dim >= 0) {
- glUniform1f(gd->win_shader.unifm_dim, (float)img->dim);
+ if (win_shader->uniform_brightness >= 0) {
+ glUniform1i(win_shader->uniform_brightness, 1);
}
- if (gd->win_shader.unifm_brightness >= 0) {
- glUniform1i(gd->win_shader.unifm_brightness, 1);
+ if (win_shader->uniform_max_brightness >= 0) {
+ glUniform1f(win_shader->uniform_max_brightness, (float)img->max_brightness);
}
- if (gd->win_shader.unifm_max_brightness >= 0) {
- glUniform1f(gd->win_shader.unifm_max_brightness, (float)img->max_brightness);
+ if (win_shader->uniform_corner_radius >= 0) {
+ glUniform1f(win_shader->uniform_corner_radius, (float)img->corner_radius);
+ }
+ if (win_shader->uniform_border_width >= 0) {
+ auto border_width = img->border_width;
+ if (border_width > img->corner_radius) {
+ border_width = 0;
+ }
+ glUniform1f(win_shader->uniform_border_width, (float)border_width);
+ }
+ if (win_shader->uniform_time >= 0) {
+ struct timespec ts;
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ glUniform1f(win_shader->uniform_time,
+ (float)ts.tv_sec * 1000.0F + (float)ts.tv_nsec / 1.0e6F);
+ }
+
+ glUniform1i(win_shader->uniform_mask_tex, 2);
+ glUniform2f(win_shader->uniform_mask_offset, (float)mask_offset.x,
+ (float)mask_offset.y);
+ if (mask != NULL) {
+ glUniform1i(win_shader->uniform_mask_inverted, mask->color_inverted);
+ glUniform1f(win_shader->uniform_mask_corner_radius,
+ (GLfloat)mask->corner_radius);
+ } else {
+ glUniform1i(win_shader->uniform_mask_inverted, 0);
+ glUniform1f(win_shader->uniform_mask_corner_radius, 0);
}
+ glActiveTexture(GL_TEXTURE2);
+ glBindTexture(GL_TEXTURE_2D, mask_texture);
+
// log_trace("Draw: %d, %d, %d, %d -> %d, %d (%d, %d) z %d\n",
// x, y, width, height, dx, dy, ptex->width, ptex->height, z);
@@ -437,6 +461,11 @@ static void _gl_compose(backend_t *base, struct backend_image *img, GLuint targe
glDeleteVertexArrays(1, &vao);
// Cleanup
+ glActiveTexture(GL_TEXTURE2);
+ glBindTexture(GL_TEXTURE_2D, 0);
+ glActiveTexture(GL_TEXTURE1);
+ glBindTexture(GL_TEXTURE_2D, 0);
+ glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 0);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glDrawBuffer(GL_BACK);
@@ -454,19 +483,18 @@ static void _gl_compose(backend_t *base, struct backend_image *img, GLuint targe
/// Convert rectangles in X coordinates to OpenGL vertex and texture coordinates
/// @param[in] nrects, rects rectangles
-/// @param[in] dst_x, dst_y origin of the OpenGL texture, affect the calculated texture
+/// @param[in] image_dst origin of the OpenGL texture, affect the calculated texture
/// coordinates
+/// @param[in] extend_height height of the drawing extent
/// @param[in] texture_height height of the OpenGL texture
/// @param[in] root_height height of the back buffer
/// @param[in] y_inverted whether the texture is y inverted
/// @param[out] coord, indices output
-static void
-x_rect_to_coords(int nrects, const rect_t *rects, int dst_x, int dst_y, int texture_height,
- int root_height, bool y_inverted, GLint *coord, GLuint *indices) {
- dst_y = root_height - dst_y;
- if (y_inverted) {
- dst_y -= texture_height;
- }
+void x_rect_to_coords(int nrects, const rect_t *rects, coord_t image_dst,
+ int extent_height, int texture_height, int root_height,
+ bool y_inverted, GLint *coord, GLuint *indices) {
+ image_dst.y = root_height - image_dst.y;
+ image_dst.y -= extent_height;
for (int i = 0; i < nrects; i++) {
// Y-flip. Note after this, crect.y1 > crect.y2
@@ -476,7 +504,8 @@ x_rect_to_coords(int nrects, const rect_t *rects, int dst_x, int dst_y, int text
// Calculate texture coordinates
// (texture_x1, texture_y1), texture coord for the _bottom left_ corner
- GLint texture_x1 = crect.x1 - dst_x, texture_y1 = crect.y2 - dst_y,
+ GLint texture_x1 = crect.x1 - image_dst.x,
+ texture_y1 = crect.y2 - image_dst.y,
texture_x2 = texture_x1 + (crect.x2 - crect.x1),
texture_y2 = texture_y1 + (crect.y1 - crect.y2);
@@ -516,9 +545,9 @@ x_rect_to_coords(int nrects, const rect_t *rects, int dst_x, int dst_y, int text
}
// TODO(yshui) make use of reg_visible
-void gl_compose(backend_t *base, void *image_data,
- int dst_x1, int dst_y1, int dst_x2, int dst_y2,
- const region_t *reg_tgt, const region_t *reg_visible attr_unused) {
+void gl_compose(backend_t *base, void *image_data, coord_t image_dst, void *mask,
+ coord_t mask_dst, const region_t *reg_tgt,
+ const region_t *reg_visible attr_unused) {
auto gd = (struct gl_data *)base;
struct backend_image *img = image_data;
auto inner = (struct gl_texture *)img->inner;
@@ -541,373 +570,43 @@ void gl_compose(backend_t *base, void *image_data,
auto coord = ccalloc(nrects * 16, GLint);
auto indices = ccalloc(nrects * 6, GLuint);
- x_rect_to_coords(nrects, rects, dst_x1, dst_y1, inner->height, gd->height,
- inner->y_inverted, coord, indices);
-
- // Interpolate the texture coordinates into the specified range
- for (unsigned int i = 2; i < 16; i+=4) {
- coord[i+0] = lerp_range(0, dst_x2 - dst_x1, 0, inner->width, coord[i+0]);
- coord[i+1] = lerp_range(0, dst_y2 - dst_y1, 0, inner->height, coord[i+1]);
- }
-
- _gl_compose(base, img, gd->back_fbo, coord, indices, nrects);
+ coord_t mask_offset = {.x = mask_dst.x - image_dst.x, .y = mask_dst.y - image_dst.y};
+ x_rect_to_coords(nrects, rects, image_dst, inner->height, inner->height,
+ gd->height, inner->y_inverted, coord, indices);
+ _gl_compose(base, img, gd->back_fbo, mask, mask_offset, coord, indices, nrects);
free(indices);
free(coord);
}
/**
- * Blur contents in a particular region.
- */
-bool gl_kernel_blur(backend_t *base, double opacity, void *ctx, const rect_t *extent,
- const GLuint vao[2], const int vao_nelems[2]) {
- auto bctx = (struct gl_blur_context *)ctx;
- auto gd = (struct gl_data *)base;
-
- int dst_y_fb_coord = bctx->fb_height - extent->y2;
-
- int curr = 0;
- for (int i = 0; i < bctx->npasses; ++i) {
- const gl_blur_shader_t *p = &bctx->blur_shader[i];
- assert(p->prog);
-
- assert(bctx->blur_textures[curr]);
-
- // The origin to use when sampling from the source texture
- GLint texorig_x = extent->x1, texorig_y = dst_y_fb_coord;
- GLint tex_width, tex_height;
- GLuint src_texture;
-
- if (i == 0) {
- src_texture = gd->back_texture;
- tex_width = gd->width;
- tex_height = gd->height;
- } else {
- src_texture = bctx->blur_textures[curr];
- auto src_size = bctx->texture_sizes[curr];
- tex_width = src_size.width;
- tex_height = src_size.height;
- }
-
- glBindTexture(GL_TEXTURE_2D, src_texture);
- glUseProgram(p->prog);
- glUniform2f(p->unifm_pixel_norm, 1.0f / (GLfloat)tex_width,
- 1.0f / (GLfloat)tex_height);
-
- // The number of indices in the selected vertex array
- GLsizei nelems;
-
- if (i < bctx->npasses - 1) {
- assert(bctx->blur_fbos[0]);
- assert(bctx->blur_textures[!curr]);
-
- // not last pass, draw into framebuffer, with resized regions
- glBindVertexArray(vao[1]);
- nelems = vao_nelems[1];
- glBindFramebuffer(GL_DRAW_FRAMEBUFFER, bctx->blur_fbos[0]);
-
- glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
- GL_TEXTURE_2D, bctx->blur_textures[!curr], 0);
- glDrawBuffer(GL_COLOR_ATTACHMENT0);
- if (!gl_check_fb_complete(GL_FRAMEBUFFER)) {
- return false;
- }
-
- glUniform1f(p->unifm_opacity, 1.0);
- } else {
- // last pass, draw directly into the back buffer, with origin
- // regions
- glBindVertexArray(vao[0]);
- nelems = vao_nelems[0];
- glBindFramebuffer(GL_FRAMEBUFFER, gd->back_fbo);
-
- glUniform1f(p->unifm_opacity, (float)opacity);
- }
-
- glUniform2f(p->texorig_loc, (GLfloat)texorig_x, (GLfloat)texorig_y);
- glDrawElements(GL_TRIANGLES, nelems, GL_UNSIGNED_INT, NULL);
-
- // XXX use multiple draw calls is probably going to be slow than
- // just simply blur the whole area.
-
- curr = !curr;
- }
-
- return true;
-}
-
-bool gl_dual_kawase_blur(backend_t *base, double opacity, void *ctx, const rect_t *extent,
- const GLuint vao[2], const int vao_nelems[2]) {
- auto bctx = (struct gl_blur_context *)ctx;
- auto gd = (struct gl_data *)base;
-
- int dst_y_fb_coord = bctx->fb_height - extent->y2;
-
- int iterations = bctx->blur_texture_count;
- int scale_factor = 1;
-
- // Kawase downsample pass
- const gl_blur_shader_t *down_pass = &bctx->blur_shader[0];
- assert(down_pass->prog);
- glUseProgram(down_pass->prog);
-
- glUniform2f(down_pass->texorig_loc, (GLfloat)extent->x1, (GLfloat)dst_y_fb_coord);
-
- for (int i = 0; i < iterations; ++i) {
- // Scale output width / height by half in each iteration
- scale_factor <<= 1;
-
- GLuint src_texture;
- int tex_width, tex_height;
-
- if (i == 0) {
- // first pass: copy from back buffer
- src_texture = gd->back_texture;
- tex_width = gd->width;
- tex_height = gd->height;
- } else {
- // copy from previous pass
- src_texture = bctx->blur_textures[i - 1];
- auto src_size = bctx->texture_sizes[i - 1];
- tex_width = src_size.width;
- tex_height = src_size.height;
- }
-
- assert(src_texture);
- assert(bctx->blur_fbos[i]);
-
- glBindTexture(GL_TEXTURE_2D, src_texture);
- glBindVertexArray(vao[1]);
- auto nelems = vao_nelems[1];
- glBindFramebuffer(GL_DRAW_FRAMEBUFFER, bctx->blur_fbos[i]);
- glDrawBuffer(GL_COLOR_ATTACHMENT0);
-
- glUniform1f(down_pass->scale_loc, (GLfloat)scale_factor);
-
- glUniform2f(down_pass->unifm_pixel_norm, 1.0f / (GLfloat)tex_width,
- 1.0f / (GLfloat)tex_height);
-
- glDrawElements(GL_TRIANGLES, nelems, GL_UNSIGNED_INT, NULL);
- }
-
- // Kawase upsample pass
- const gl_blur_shader_t *up_pass = &bctx->blur_shader[1];
- assert(up_pass->prog);
- glUseProgram(up_pass->prog);
-
- glUniform2f(up_pass->texorig_loc, (GLfloat)extent->x1, (GLfloat)dst_y_fb_coord);
-
- for (int i = iterations - 1; i >= 0; --i) {
- // Scale output width / height back by two in each iteration
- scale_factor >>= 1;
-
- const GLuint src_texture = bctx->blur_textures[i];
- assert(src_texture);
-
- // Calculate normalized half-width/-height of a src pixel
- auto src_size = bctx->texture_sizes[i];
- int tex_width = src_size.width;
- int tex_height = src_size.height;
-
- // The number of indices in the selected vertex array
- GLsizei nelems;
-
- glBindTexture(GL_TEXTURE_2D, src_texture);
- if (i > 0) {
- assert(bctx->blur_fbos[i - 1]);
-
- // not last pass, draw into next framebuffer
- glBindVertexArray(vao[1]);
- nelems = vao_nelems[1];
- glBindFramebuffer(GL_DRAW_FRAMEBUFFER, bctx->blur_fbos[i - 1]);
- glDrawBuffer(GL_COLOR_ATTACHMENT0);
-
- glUniform1f(up_pass->unifm_opacity, (GLfloat)1);
- } else {
- // last pass, draw directly into the back buffer
- glBindVertexArray(vao[0]);
- nelems = vao_nelems[0];
- glBindFramebuffer(GL_FRAMEBUFFER, gd->back_fbo);
-
- glUniform1f(up_pass->unifm_opacity, (GLfloat)opacity);
- }
-
- glUniform1f(up_pass->scale_loc, (GLfloat)scale_factor);
- glUniform2f(up_pass->unifm_pixel_norm, 1.0f / (GLfloat)tex_width,
- 1.0f / (GLfloat)tex_height);
-
- glDrawElements(GL_TRIANGLES, nelems, GL_UNSIGNED_INT, NULL);
- }
-
- return true;
-}
-
-bool gl_blur(backend_t *base, double opacity, void *ctx, const region_t *reg_blur,
- const region_t *reg_visible attr_unused) {
- auto bctx = (struct gl_blur_context *)ctx;
- auto gd = (struct gl_data *)base;
-
- bool ret = false;
-
- if (gd->width != bctx->fb_width || gd->height != bctx->fb_height) {
- // Resize the temporary textures used for blur in case the root
- // size changed
- bctx->fb_width = gd->width;
- bctx->fb_height = gd->height;
-
- for (int i = 0; i < bctx->blur_texture_count; ++i) {
- auto tex_size = bctx->texture_sizes + i;
- if (bctx->method == BLUR_METHOD_DUAL_KAWASE) {
- // Use smaller textures for each iteration (quarter of the
- // previous texture)
- tex_size->width = 1 + ((bctx->fb_width - 1) >> (i + 1));
- tex_size->height = 1 + ((bctx->fb_height - 1) >> (i + 1));
- } else {
- tex_size->width = bctx->fb_width;
- tex_size->height = bctx->fb_height;
- }
-
- glBindTexture(GL_TEXTURE_2D, bctx->blur_textures[i]);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, tex_size->width,
- tex_size->height, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
-
- if (bctx->method == BLUR_METHOD_DUAL_KAWASE) {
- // Attach texture to FBO target
- glBindFramebuffer(GL_DRAW_FRAMEBUFFER, bctx->blur_fbos[i]);
- glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER,
- GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
- bctx->blur_textures[i], 0);
- if (!gl_check_fb_complete(GL_FRAMEBUFFER)) {
- glBindFramebuffer(GL_FRAMEBUFFER, 0);
- return false;
- }
- }
- }
- glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
- }
-
- // Remainder: regions are in Xorg coordinates
- auto reg_blur_resized =
- resize_region(reg_blur, bctx->resize_width, bctx->resize_height);
- const rect_t *extent = pixman_region32_extents((region_t *)reg_blur),
- *extent_resized = pixman_region32_extents(&reg_blur_resized);
- int width = extent->x2 - extent->x1, height = extent->y2 - extent->y1;
- if (width == 0 || height == 0) {
- return true;
- }
-
- int nrects, nrects_resized;
- const rect_t *rects = pixman_region32_rectangles((region_t *)reg_blur, &nrects),
- *rects_resized =
- pixman_region32_rectangles(&reg_blur_resized, &nrects_resized);
- if (!nrects || !nrects_resized) {
- return true;
- }
-
- auto coord = ccalloc(nrects * 16, GLint);
- auto indices = ccalloc(nrects * 6, GLuint);
- x_rect_to_coords(nrects, rects, extent_resized->x1, extent_resized->y2,
- bctx->fb_height, gd->height, false, coord, indices);
-
- auto coord_resized = ccalloc(nrects_resized * 16, GLint);
- auto indices_resized = ccalloc(nrects_resized * 6, GLuint);
- x_rect_to_coords(nrects_resized, rects_resized, extent_resized->x1,
- extent_resized->y2, bctx->fb_height, bctx->fb_height, false,
- coord_resized, indices_resized);
- pixman_region32_fini(&reg_blur_resized);
-
- GLuint vao[2];
- glGenVertexArrays(2, vao);
- GLuint bo[4];
- glGenBuffers(4, bo);
-
- glBindVertexArray(vao[0]);
- glBindBuffer(GL_ARRAY_BUFFER, bo[0]);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bo[1]);
- glBufferData(GL_ARRAY_BUFFER, (long)sizeof(*coord) * nrects * 16, coord, GL_STATIC_DRAW);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, (long)sizeof(*indices) * nrects * 6,
- indices, GL_STATIC_DRAW);
- glEnableVertexAttribArray(vert_coord_loc);
- glEnableVertexAttribArray(vert_in_texcoord_loc);
- glVertexAttribPointer(vert_coord_loc, 2, GL_INT, GL_FALSE, sizeof(GLint) * 4, NULL);
- glVertexAttribPointer(vert_in_texcoord_loc, 2, GL_INT, GL_FALSE,
- sizeof(GLint) * 4, (void *)(sizeof(GLint) * 2));
-
- glBindVertexArray(vao[1]);
- glBindBuffer(GL_ARRAY_BUFFER, bo[2]);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bo[3]);
- glBufferData(GL_ARRAY_BUFFER, (long)sizeof(*coord_resized) * nrects_resized * 16,
- coord_resized, GL_STATIC_DRAW);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER,
- (long)sizeof(*indices_resized) * nrects_resized * 6, indices_resized,
- GL_STATIC_DRAW);
- glEnableVertexAttribArray(vert_coord_loc);
- glEnableVertexAttribArray(vert_in_texcoord_loc);
- glVertexAttribPointer(vert_coord_loc, 2, GL_INT, GL_FALSE, sizeof(GLint) * 4, NULL);
- glVertexAttribPointer(vert_in_texcoord_loc, 2, GL_INT, GL_FALSE,
- sizeof(GLint) * 4, (void *)(sizeof(GLint) * 2));
-
- int vao_nelems[2] = {nrects * 6, nrects_resized * 6};
-
- if (bctx->method == BLUR_METHOD_DUAL_KAWASE) {
- ret = gl_dual_kawase_blur(base, opacity, ctx, extent_resized, vao, vao_nelems);
- } else {
- ret = gl_kernel_blur(base, opacity, ctx, extent_resized, vao, vao_nelems);
- }
-
- glBindFramebuffer(GL_FRAMEBUFFER, 0);
- glBindTexture(GL_TEXTURE_2D, 0);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
- glDeleteBuffers(4, bo);
- glBindVertexArray(0);
- glDeleteVertexArrays(2, vao);
- glUseProgram(0);
-
- free(indices);
- free(coord);
- free(indices_resized);
- free(coord_resized);
-
- gl_check_err();
- return ret;
-}
-
-// clang-format off
-const char *vertex_shader = GLSL(330,
- uniform mat4 projection;
- uniform float scale = 1.0;
- uniform vec2 texorig;
- layout(location = 0) in vec2 coord;
- layout(location = 1) in vec2 in_texcoord;
- out vec2 texcoord;
- void main() {
- gl_Position = projection * vec4(coord, 0, scale);
- texcoord = in_texcoord + texorig;
- }
-);
-// clang-format on
-
-/**
* Load a GLSL main program from shader strings.
*/
-static int gl_win_shader_from_string(const char *vshader_str, const char *fshader_str,
- gl_win_shader_t *ret) {
+static bool gl_win_shader_from_stringv(const char **vshader_strv,
+ const char **fshader_strv, gl_win_shader_t *ret) {
// Build program
- ret->prog = gl_create_program_from_str(vshader_str, fshader_str);
+ ret->prog = gl_create_program_from_strv(vshader_strv, fshader_strv);
if (!ret->prog) {
log_error("Failed to create GLSL program.");
- return -1;
+ gl_check_err();
+ return false;
}
// Get uniform addresses
- ret->unifm_opacity = glGetUniformLocationChecked(ret->prog, "opacity");
- ret->unifm_invert_color = glGetUniformLocationChecked(ret->prog, "invert_color");
- ret->unifm_tex = glGetUniformLocationChecked(ret->prog, "tex");
- ret->unifm_dim = glGetUniformLocationChecked(ret->prog, "dim");
- ret->unifm_brightness = glGetUniformLocationChecked(ret->prog, "brightness");
- ret->unifm_max_brightness =
- glGetUniformLocationChecked(ret->prog, "max_brightness");
+ bind_uniform(ret, opacity);
+ bind_uniform(ret, invert_color);
+ bind_uniform(ret, tex);
+ bind_uniform(ret, dim);
+ bind_uniform(ret, brightness);
+ bind_uniform(ret, max_brightness);
+ bind_uniform(ret, corner_radius);
+ bind_uniform(ret, border_width);
+ bind_uniform(ret, time);
+
+ bind_uniform(ret, mask_tex);
+ bind_uniform(ret, mask_offset);
+ bind_uniform(ret, mask_inverted);
+ bind_uniform(ret, mask_corner_radius);
gl_check_err();
@@ -934,51 +633,6 @@ void gl_resize(struct gl_data *gd, int width, int height) {
gl_check_err();
}
-// clang-format off
-static const char dummy_frag[] = GLSL(330,
- uniform sampler2D tex;
- in vec2 texcoord;
- void main() {
- gl_FragColor = texelFetch(tex, ivec2(texcoord.xy), 0);
- }
-);
-
-static const char fill_frag[] = GLSL(330,
- uniform vec4 color;
- void main() {
- gl_FragColor = color;
- }
-);
-
-static const char fill_vert[] = GLSL(330,
- layout(location = 0) in vec2 in_coord;
- uniform mat4 projection;
- void main() {
- gl_Position = projection * vec4(in_coord, 0, 1);
- }
-);
-
-static const char interpolating_frag[] = GLSL(330,
- uniform sampler2D tex;
- in vec2 texcoord;
- void main() {
- gl_FragColor = vec4(texture2D(tex, vec2(texcoord.xy), 0).rgb, 1);
- }
-);
-
-static const char interpolating_vert[] = GLSL(330,
- uniform mat4 projection;
- uniform vec2 texsize;
- layout(location = 0) in vec2 in_coord;
- layout(location = 1) in vec2 in_texcoord;
- out vec2 texcoord;
- void main() {
- gl_Position = projection * vec4(in_coord, 0, 1);
- texcoord = in_texcoord / texsize;
- }
-);
-// clang-format on
-
/// Fill a given region in bound framebuffer.
/// @param[in] y_inverted whether the y coordinates in `clip` should be inverted
static void _gl_fill(backend_t *base, struct color c, const region_t *clip, GLuint target,
@@ -1048,9 +702,45 @@ void gl_fill(backend_t *base, struct color c, const region_t *clip) {
return _gl_fill(base, c, clip, gd->back_fbo, gd->height, true);
}
+void *gl_make_mask(backend_t *base, geometry_t size, const region_t *reg) {
+ auto tex = ccalloc(1, struct gl_texture);
+ auto img = default_new_backend_image(size.width, size.height);
+ tex->width = size.width;
+ tex->height = size.height;
+ tex->texture = gl_new_texture(GL_TEXTURE_2D);
+ tex->has_alpha = false;
+ tex->y_inverted = true;
+ img->inner = (struct backend_image_inner_base *)tex;
+ img->inner->refcount = 1;
+
+ glBindTexture(GL_TEXTURE_2D, tex->texture);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, size.width, size.height, 0, GL_RED,
+ GL_UNSIGNED_BYTE, NULL);
+ glBindTexture(GL_TEXTURE_2D, 0);
+
+ GLuint fbo;
+ glBlendFunc(GL_ONE, GL_ZERO);
+ glGenFramebuffers(1, &fbo);
+ glBindFramebuffer(GL_FRAMEBUFFER, fbo);
+ glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
+ tex->texture, 0);
+ glDrawBuffer(GL_COLOR_ATTACHMENT0);
+ glClearColor(0, 0, 0, 1);
+ glClear(GL_COLOR_BUFFER_BIT);
+ _gl_fill(base, (struct color){1, 1, 1, 1}, reg, fbo, size.height, false);
+ glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
+ glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
+ glDeleteFramebuffers(1, &fbo);
+ return img;
+}
+
static void gl_release_image_inner(backend_t *base, struct gl_texture *inner) {
auto gd = (struct gl_data *)base;
- gd->release_user_data(base, inner);
+ if (inner->user_data) {
+ gd->release_user_data(base, inner);
+ }
assert(inner->user_data == NULL);
glDeleteTextures(1, &inner->texture);
@@ -1070,513 +760,44 @@ void gl_release_image(backend_t *base, void *image_data) {
free(wd);
}
-static inline void gl_free_blur_shader(gl_blur_shader_t *shader) {
- if (shader->prog) {
- glDeleteProgram(shader->prog);
- }
+void *gl_create_window_shader(backend_t *backend_data attr_unused, const char *source) {
+ auto win_shader = (gl_win_shader_t *)ccalloc(1, gl_win_shader_t);
- shader->prog = 0;
-}
+ const char *vert_shaders[2] = {vertex_shader, NULL};
+ const char *frag_shaders[4] = {win_shader_glsl, masking_glsl, source, NULL};
-void gl_destroy_blur_context(backend_t *base attr_unused, void *ctx) {
- auto bctx = (struct gl_blur_context *)ctx;
- // Free GLSL shaders/programs
- for (int i = 0; i < bctx->npasses; ++i) {
- gl_free_blur_shader(&bctx->blur_shader[i]);
+ if (!gl_win_shader_from_stringv(vert_shaders, frag_shaders, win_shader)) {
+ free(win_shader);
+ return NULL;
}
- free(bctx->blur_shader);
- if (bctx->blur_texture_count && bctx->blur_textures) {
- glDeleteTextures(bctx->blur_texture_count, bctx->blur_textures);
- free(bctx->blur_textures);
- }
- if (bctx->blur_texture_count && bctx->texture_sizes) {
- free(bctx->texture_sizes);
- }
- if (bctx->blur_fbo_count && bctx->blur_fbos) {
- glDeleteFramebuffers(bctx->blur_fbo_count, bctx->blur_fbos);
- free(bctx->blur_fbos);
- }
-
- bctx->blur_texture_count = 0;
- bctx->blur_fbo_count = 0;
-
- free(bctx);
-
- gl_check_err();
-}
-
-/**
- * Initialize GL blur filters.
- */
-bool gl_create_kernel_blur_context(void *blur_context, GLfloat *projection,
- enum blur_method method, void *args) {
- bool success = false;
- auto ctx = (struct gl_blur_context *)blur_context;
-
- struct conv **kernels;
-
- int nkernels;
- ctx->method = BLUR_METHOD_KERNEL;
- if (method == BLUR_METHOD_KERNEL) {
- nkernels = ((struct kernel_blur_args *)args)->kernel_count;
- kernels = ((struct kernel_blur_args *)args)->kernels;
- } else {
- kernels = generate_blur_kernel(method, args, &nkernels);
- }
-
- if (!nkernels) {
- ctx->method = BLUR_METHOD_NONE;
- return true;
- }
-
- // Specify required textures and FBOs
- ctx->blur_texture_count = 2;
- ctx->blur_fbo_count = 1;
-
- ctx->blur_shader = ccalloc(max2(2, nkernels), gl_blur_shader_t);
-
- char *lc_numeric_old = strdup(setlocale(LC_NUMERIC, NULL));
- // Enforce LC_NUMERIC locale "C" here to make sure decimal point is sane
- // Thanks to hiciu for reporting.
- setlocale(LC_NUMERIC, "C");
-
- // clang-format off
- static const char *FRAG_SHADER_BLUR = GLSL(330,
- %s\n // other extension pragmas
- uniform sampler2D tex_src;
- uniform vec2 pixel_norm;
- uniform float opacity;
- in vec2 texcoord;
- out vec4 out_color;
- void main() {
- vec2 uv = texcoord * pixel_norm;
- vec4 sum = vec4(0.0, 0.0, 0.0, 0.0);
- %s //body of the convolution
- out_color = sum / float(%.7g) * opacity;
- }
- );
- static const char *FRAG_SHADER_BLUR_ADD = QUOTE(
- sum += float(%.7g) * texture2D(tex_src, uv + pixel_norm * vec2(%.7g, %.7g));
- );
- // clang-format on
-
- const char *shader_add = FRAG_SHADER_BLUR_ADD;
- char *extension = strdup("");
-
- for (int i = 0; i < nkernels; i++) {
- auto kern = kernels[i];
- // Build shader
- int width = kern->w, height = kern->h;
- int nele = width * height;
- // '%.7g' is at most 14 characters, inserted 3 times
- size_t body_len = (strlen(shader_add) + 42) * (uint)nele;
- char *shader_body = ccalloc(body_len, char);
- char *pc = shader_body;
-
- // Make use of the linear interpolation hardware by sampling 2 pixels with
- // one texture access by sampling between both pixels based on their
- // relative weight. Easiest done in a single dimension as 2D bilinear
- // filtering would raise additional constraints on the kernels. Therefore
- // only use interpolation along the larger dimension.
- double sum = 0.0;
- if (width > height) {
- // use interpolation in x dimension (width)
- for (int j = 0; j < height; ++j) {
- for (int k = 0; k < width; k += 2) {
- double val1, val2;
- val1 = kern->data[j * width + k];
- val2 = (k + 1 < width)
- ? kern->data[j * width + k + 1]
- : 0;
-
- double combined_weight = val1 + val2;
- if (combined_weight == 0) {
- continue;
- }
- sum += combined_weight;
-
- double offset_x =
- k + (val2 / combined_weight) - (width / 2);
- double offset_y = j - (height / 2);
- pc += snprintf(
- pc, body_len - (ulong)(pc - shader_body),
- shader_add, combined_weight, offset_x, offset_y);
- assert(pc < shader_body + body_len);
- }
- }
- } else {
- // use interpolation in y dimension (height)
- for (int j = 0; j < height; j += 2) {
- for (int k = 0; k < width; ++k) {
- double val1, val2;
- val1 = kern->data[j * width + k];
- val2 = (j + 1 < height)
- ? kern->data[(j + 1) * width + k]
- : 0;
-
- double combined_weight = val1 + val2;
- if (combined_weight == 0) {
- continue;
- }
- sum += combined_weight;
-
- double offset_x = k - (width / 2);
- double offset_y =
- j + (val2 / combined_weight) - (height / 2);
- pc += snprintf(
- pc, body_len - (ulong)(pc - shader_body),
- shader_add, combined_weight, offset_x, offset_y);
- assert(pc < shader_body + body_len);
- }
- }
- }
-
- auto pass = ctx->blur_shader + i;
- size_t shader_len = strlen(FRAG_SHADER_BLUR) + strlen(extension) +
- strlen(shader_body) + 10 /* sum */ +
- 1 /* null terminator */;
- char *shader_str = ccalloc(shader_len, char);
- auto real_shader_len = snprintf(shader_str, shader_len, FRAG_SHADER_BLUR,
- extension, shader_body, sum);
- CHECK(real_shader_len >= 0);
- CHECK((size_t)real_shader_len < shader_len);
- free(shader_body);
-
- // Build program
- pass->prog = gl_create_program_from_str(vertex_shader, shader_str);
- free(shader_str);
- if (!pass->prog) {
- log_error("Failed to create GLSL program.");
- success = false;
- goto out;
- }
- glBindFragDataLocation(pass->prog, 0, "out_color");
-
- // Get uniform addresses
- pass->unifm_pixel_norm =
- glGetUniformLocationChecked(pass->prog, "pixel_norm");
- pass->unifm_opacity = glGetUniformLocationChecked(pass->prog, "opacity");
- pass->texorig_loc = glGetUniformLocationChecked(pass->prog, "texorig");
-
- // Setup projection matrix
- glUseProgram(pass->prog);
- int pml = glGetUniformLocationChecked(pass->prog, "projection");
- glUniformMatrix4fv(pml, 1, false, projection);
- glUseProgram(0);
-
- ctx->resize_width += kern->w / 2;
- ctx->resize_height += kern->h / 2;
- }
-
- if (nkernels == 1) {
- // Generate an extra null pass so we don't need special code path for
- // the single pass case
- auto pass = &ctx->blur_shader[1];
- pass->prog = gl_create_program_from_str(vertex_shader, dummy_frag);
- pass->unifm_pixel_norm = -1;
- pass->unifm_opacity = -1;
- pass->texorig_loc = glGetUniformLocationChecked(pass->prog, "texorig");
-
- // Setup projection matrix
- glUseProgram(pass->prog);
- int pml = glGetUniformLocationChecked(pass->prog, "projection");
- glUniformMatrix4fv(pml, 1, false, projection);
- glUseProgram(0);
-
- ctx->npasses = 2;
- } else {
- ctx->npasses = nkernels;
- }
-
- success = true;
-out:
- if (method != BLUR_METHOD_KERNEL) {
- // We generated the blur kernels, so we need to free them
- for (int i = 0; i < nkernels; i++) {
- free(kernels[i]);
- }
- free(kernels);
- }
-
- free(extension);
- // Restore LC_NUMERIC
- setlocale(LC_NUMERIC, lc_numeric_old);
- free(lc_numeric_old);
-
- return success;
-}
-
-bool gl_create_dual_kawase_blur_context(void *blur_context, GLfloat *projection,
- enum blur_method method, void *args) {
- bool success = false;
- auto ctx = (struct gl_blur_context *)blur_context;
-
- ctx->method = method;
-
- auto blur_params = generate_dual_kawase_params(args);
-
- // Specify required textures and FBOs
- ctx->blur_texture_count = blur_params->iterations;
- ctx->blur_fbo_count = blur_params->iterations;
-
- ctx->resize_width += blur_params->expand;
- ctx->resize_height += blur_params->expand;
-
- ctx->npasses = 2;
- ctx->blur_shader = ccalloc(ctx->npasses, gl_blur_shader_t);
-
- char *lc_numeric_old = strdup(setlocale(LC_NUMERIC, NULL));
- // Enforce LC_NUMERIC locale "C" here to make sure decimal point is sane
- // Thanks to hiciu for reporting.
- setlocale(LC_NUMERIC, "C");
-
- // Dual-kawase downsample shader / program
- auto down_pass = ctx->blur_shader;
- {
- // clang-format off
- static const char *FRAG_SHADER_DOWN = GLSL(330,
- uniform sampler2D tex_src;
- uniform float scale = 1.0;
- uniform vec2 pixel_norm;
- in vec2 texcoord;
- out vec4 out_color;
- void main() {
- vec2 offset = %.7g * pixel_norm;
- vec2 uv = texcoord * pixel_norm * (2.0 / scale);
- vec4 sum = texture2D(tex_src, uv) * 4.0;
- sum += texture2D(tex_src, uv - vec2(0.5, 0.5) * offset);
- sum += texture2D(tex_src, uv + vec2(0.5, 0.5) * offset);
- sum += texture2D(tex_src, uv + vec2(0.5, -0.5) * offset);
- sum += texture2D(tex_src, uv - vec2(0.5, -0.5) * offset);
- out_color = sum / 8.0;
- }
- );
- // clang-format on
-
- // Build shader
- size_t shader_len =
- strlen(FRAG_SHADER_DOWN) + 10 /* offset */ + 1 /* null terminator */;
- char *shader_str = ccalloc(shader_len, char);
- auto real_shader_len =
- snprintf(shader_str, shader_len, FRAG_SHADER_DOWN, blur_params->offset);
- CHECK(real_shader_len >= 0);
- CHECK((size_t)real_shader_len < shader_len);
-
- // Build program
- down_pass->prog = gl_create_program_from_str(vertex_shader, shader_str);
- free(shader_str);
- if (!down_pass->prog) {
- log_error("Failed to create GLSL program.");
- success = false;
- goto out;
- }
- glBindFragDataLocation(down_pass->prog, 0, "out_color");
-
- // Get uniform addresses
- down_pass->unifm_pixel_norm =
- glGetUniformLocationChecked(down_pass->prog, "pixel_norm");
- down_pass->texorig_loc =
- glGetUniformLocationChecked(down_pass->prog, "texorig");
- down_pass->scale_loc =
- glGetUniformLocationChecked(down_pass->prog, "scale");
-
- // Setup projection matrix
- glUseProgram(down_pass->prog);
- int pml = glGetUniformLocationChecked(down_pass->prog, "projection");
- glUniformMatrix4fv(pml, 1, false, projection);
- glUseProgram(0);
- }
-
- // Dual-kawase upsample shader / program
- auto up_pass = ctx->blur_shader + 1;
- {
- // clang-format off
- static const char *FRAG_SHADER_UP = GLSL(330,
- uniform sampler2D tex_src;
- uniform float scale = 1.0;
- uniform vec2 pixel_norm;
- uniform float opacity;
- in vec2 texcoord;
- out vec4 out_color;
- void main() {
- vec2 offset = %.7g * pixel_norm;
- vec2 uv = texcoord * pixel_norm / (2 * scale);
- vec4 sum = texture2D(tex_src, uv + vec2(-1.0, 0.0) * offset);
- sum += texture2D(tex_src, uv + vec2(-0.5, 0.5) * offset) * 2.0;
- sum += texture2D(tex_src, uv + vec2(0.0, 1.0) * offset);
- sum += texture2D(tex_src, uv + vec2(0.5, 0.5) * offset) * 2.0;
- sum += texture2D(tex_src, uv + vec2(1.0, 0.0) * offset);
- sum += texture2D(tex_src, uv + vec2(0.5, -0.5) * offset) * 2.0;
- sum += texture2D(tex_src, uv + vec2(0.0, -1.0) * offset);
- sum += texture2D(tex_src, uv + vec2(-0.5, -0.5) * offset) * 2.0;
- out_color = sum / 12.0 * opacity;
- }
- );
- // clang-format on
-
- // Build shader
- size_t shader_len =
- strlen(FRAG_SHADER_UP) + 10 /* offset */ + 1 /* null terminator */;
- char *shader_str = ccalloc(shader_len, char);
- auto real_shader_len =
- snprintf(shader_str, shader_len, FRAG_SHADER_UP, blur_params->offset);
- CHECK(real_shader_len >= 0);
- CHECK((size_t)real_shader_len < shader_len);
-
- // Build program
- up_pass->prog = gl_create_program_from_str(vertex_shader, shader_str);
- free(shader_str);
- if (!up_pass->prog) {
- log_error("Failed to create GLSL program.");
- success = false;
- goto out;
- }
- glBindFragDataLocation(up_pass->prog, 0, "out_color");
-
- // Get uniform addresses
- up_pass->unifm_pixel_norm =
- glGetUniformLocationChecked(up_pass->prog, "pixel_norm");
- up_pass->unifm_opacity =
- glGetUniformLocationChecked(up_pass->prog, "opacity");
- up_pass->texorig_loc =
- glGetUniformLocationChecked(up_pass->prog, "texorig");
- up_pass->scale_loc = glGetUniformLocationChecked(up_pass->prog, "scale");
-
- // Setup projection matrix
- glUseProgram(up_pass->prog);
- int pml = glGetUniformLocationChecked(up_pass->prog, "projection");
- glUniformMatrix4fv(pml, 1, false, projection);
- glUseProgram(0);
- }
-
- success = true;
-out:
- free(blur_params);
-
- if (!success) {
- ctx = NULL;
- }
-
- // Restore LC_NUMERIC
- setlocale(LC_NUMERIC, lc_numeric_old);
- free(lc_numeric_old);
-
- return success;
-}
-
-void *gl_create_blur_context(backend_t *base, enum blur_method method, void *args) {
- bool success;
- auto gd = (struct gl_data *)base;
-
- auto ctx = ccalloc(1, struct gl_blur_context);
-
- if (!method || method >= BLUR_METHOD_INVALID) {
- ctx->method = BLUR_METHOD_NONE;
- return ctx;
- }
+ GLint viewport_dimensions[2];
+ glGetIntegerv(GL_MAX_VIEWPORT_DIMS, viewport_dimensions);
// Set projection matrix to gl viewport dimensions so we can use screen
// coordinates for all vertices
// Note: OpenGL matrices are column major
- GLint viewport_dimensions[2];
- glGetIntegerv(GL_MAX_VIEWPORT_DIMS, viewport_dimensions);
- GLfloat projection_matrix[4][4] = {{2.0f / (GLfloat)viewport_dimensions[0], 0, 0, 0},
- {0, 2.0f / (GLfloat)viewport_dimensions[1], 0, 0},
+ GLfloat projection_matrix[4][4] = {{2.0F / (GLfloat)viewport_dimensions[0], 0, 0, 0},
+ {0, 2.0F / (GLfloat)viewport_dimensions[1], 0, 0},
{0, 0, 0, 0},
{-1, -1, 0, 1}};
- if (method == BLUR_METHOD_DUAL_KAWASE) {
- success = gl_create_dual_kawase_blur_context(ctx, projection_matrix[0],
- method, args);
- } else {
- success =
- gl_create_kernel_blur_context(ctx, projection_matrix[0], method, args);
- }
- if (!success || ctx->method == BLUR_METHOD_NONE) {
- goto out;
- }
-
- // 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);
- glGenTextures(ctx->blur_texture_count, ctx->blur_textures);
-
- for (int i = 0; i < ctx->blur_texture_count; ++i) {
- glBindTexture(GL_TEXTURE_2D, ctx->blur_textures[i]);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
- }
-
- // Generate FBO and textures when needed
- ctx->blur_fbos = ccalloc(ctx->blur_fbo_count, GLuint);
- glGenFramebuffers(ctx->blur_fbo_count, ctx->blur_fbos);
-
- for (int i = 0; i < ctx->blur_fbo_count; ++i) {
- if (!ctx->blur_fbos[i]) {
- log_error("Failed to generate framebuffer objects for blur");
- success = false;
- goto out;
- }
- }
-
-out:
- if (!success) {
- gl_destroy_blur_context(&gd->base, ctx);
- ctx = NULL;
- }
-
- gl_check_err();
- return ctx;
-}
+ int pml = glGetUniformLocationChecked(win_shader->prog, "projection");
+ glUseProgram(win_shader->prog);
+ glUniformMatrix4fv(pml, 1, false, projection_matrix[0]);
+ glUseProgram(0);
-void gl_get_blur_size(void *blur_context, int *width, int *height) {
- auto ctx = (struct gl_blur_context *)blur_context;
- *width = ctx->resize_width;
- *height = ctx->resize_height;
+ return win_shader;
}
-// clang-format off
-const char *win_shader_glsl = GLSL(330,
- uniform float opacity;
- uniform float dim;
- uniform bool invert_color;
- in vec2 texcoord;
- uniform sampler2D tex;
- uniform sampler2D brightness;
- uniform float max_brightness;
-
- void main() {
- vec4 c = texelFetch(tex, ivec2(texcoord), 0);
- if (invert_color) {
- c = vec4(c.aaa - c.rgb, c.a);
- }
- c = vec4(c.rgb * (1.0 - dim), c.a) * opacity;
-
- vec3 rgb_brightness = texelFetch(brightness, ivec2(0, 0), 0).rgb;
- // Ref: https://en.wikipedia.org/wiki/Relative_luminance
- float brightness = rgb_brightness.r * 0.21 +
- rgb_brightness.g * 0.72 +
- rgb_brightness.b * 0.07;
- if (brightness > max_brightness)
- c.rgb = c.rgb * (max_brightness / brightness);
-
- gl_FragColor = c;
+uint64_t gl_get_shader_attributes(backend_t *backend_data attr_unused, void *shader) {
+ auto win_shader = (gl_win_shader_t *)shader;
+ uint64_t ret = 0;
+ if (glGetUniformLocation(win_shader->prog, "time") >= 0) {
+ ret |= SHADER_ATTRIBUTE_ANIMATED;
}
-);
-
-const char *present_vertex_shader = GLSL(330,
- uniform mat4 projection;
- layout(location = 0) in vec2 coord;
- out vec2 texcoord;
- void main() {
- gl_Position = projection * vec4(coord, 0, 1);
- texcoord = coord;
- }
-);
-// clang-format on
+ return ret;
+}
bool gl_init(struct gl_data *gd, session_t *ps) {
// Initialize GLX data structure
@@ -1602,7 +823,7 @@ bool gl_init(struct gl_data *gd, session_t *ps) {
glViewport(0, 0, viewport_dimensions[0], viewport_dimensions[1]);
// Clear screen
- glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
+ glClearColor(0.0F, 0.0F, 0.0F, 1.0F);
glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glGenFramebuffers(1, &gd->back_fbo);
@@ -1619,24 +840,35 @@ bool gl_init(struct gl_data *gd, session_t *ps) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_2D, 0);
+ gd->default_mask_texture = gl_new_texture(GL_TEXTURE_2D);
+ if (!gd->default_mask_texture) {
+ log_error("Failed to generate a default mask texture");
+ return false;
+ }
+
+ glBindTexture(GL_TEXTURE_2D, gd->default_mask_texture);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, 1, 1, 0, GL_RED, GL_UNSIGNED_BYTE,
+ (GLbyte[]){'\xff'});
+ glBindTexture(GL_TEXTURE_2D, 0);
+
+ // Initialize shaders
+ gd->default_shader = gl_create_window_shader(NULL, win_shader_default);
+ if (!gd->default_shader) {
+ log_error("Failed to create window shaders");
+ return false;
+ }
+
// Set projection matrix to gl viewport dimensions so we can use screen
// coordinates for all vertices
// Note: OpenGL matrices are column major
- GLfloat projection_matrix[4][4] = {{2.0f / (GLfloat)viewport_dimensions[0], 0, 0, 0},
- {0, 2.0f / (GLfloat)viewport_dimensions[1], 0, 0},
+ GLfloat projection_matrix[4][4] = {{2.0F / (GLfloat)viewport_dimensions[0], 0, 0, 0},
+ {0, 2.0F / (GLfloat)viewport_dimensions[1], 0, 0},
{0, 0, 0, 0},
{-1, -1, 0, 1}};
- // Initialize shaders
- gl_win_shader_from_string(vertex_shader, win_shader_glsl, &gd->win_shader);
- int pml = glGetUniformLocationChecked(gd->win_shader.prog, "projection");
- glUseProgram(gd->win_shader.prog);
- glUniformMatrix4fv(pml, 1, false, projection_matrix[0]);
- glUseProgram(0);
-
gd->fill_shader.prog = gl_create_program_from_str(fill_vert, fill_frag);
gd->fill_shader.color_loc = glGetUniformLocation(gd->fill_shader.prog, "color");
- pml = glGetUniformLocationChecked(gd->fill_shader.prog, "projection");
+ int pml = glGetUniformLocationChecked(gd->fill_shader.prog, "projection");
glUseProgram(gd->fill_shader.prog);
glUniformMatrix4fv(pml, 1, false, projection_matrix[0]);
glUseProgram(0);
@@ -1652,6 +884,17 @@ bool gl_init(struct gl_data *gd, session_t *ps) {
glUniformMatrix4fv(pml, 1, false, projection_matrix[0]);
glUseProgram(0);
+ gd->shadow_shader.prog =
+ gl_create_program_from_str(present_vertex_shader, shadow_colorization_frag);
+ gd->shadow_shader.uniform_color =
+ glGetUniformLocationChecked(gd->shadow_shader.prog, "color");
+ pml = glGetUniformLocationChecked(gd->shadow_shader.prog, "projection");
+ glUseProgram(gd->shadow_shader.prog);
+ glUniform1i(glGetUniformLocationChecked(gd->shadow_shader.prog, "tex"), 0);
+ glUniformMatrix4fv(pml, 1, false, projection_matrix[0]);
+ glUseProgram(0);
+ glBindFragDataLocation(gd->shadow_shader.prog, 0, "out_color");
+
gd->brightness_shader.prog =
gl_create_program_from_str(interpolating_vert, interpolating_frag);
if (!gd->brightness_shader.prog) {
@@ -1689,18 +932,24 @@ bool gl_init(struct gl_data *gd, session_t *ps) {
} else {
gd->is_nvidia = false;
}
+ gd->has_robustness = gl_has_extension("GL_ARB_robustness");
+ gd->has_egl_image_storage = gl_has_extension("GL_EXT_EGL_image_storage");
+ gl_check_err();
return true;
}
void gl_deinit(struct gl_data *gd) {
- gl_free_prog_main(&gd->win_shader);
-
if (gd->logger) {
log_remove_target_tls(gd->logger);
gd->logger = NULL;
}
+ if (gd->default_shader) {
+ gl_destroy_window_shader(&gd->base, gd->default_shader);
+ gd->default_shader = NULL;
+ }
+
gl_check_err();
}
@@ -1832,7 +1081,8 @@ static void gl_image_apply_alpha(backend_t *base, struct backend_image *img,
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
inner->texture, 0);
glDrawBuffer(GL_COLOR_ATTACHMENT0);
- _gl_fill(base, (struct color){0, 0, 0, 0}, reg_op, fbo, 0, false);
+ _gl_fill(base, (struct color){0, 0, 0, 0}, reg_op, fbo, inner->height,
+ !inner->y_inverted);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glDeleteFramebuffers(1, &fbo);
@@ -1904,19 +1154,202 @@ bool gl_image_op(backend_t *base, enum image_operations op, void *image_data,
return true;
}
-bool gl_read_pixel(backend_t *base attr_unused, void *image_data, int x, int y,
- struct color *output) {
- struct backend_image *tex = image_data;
- auto inner = (struct gl_texture *)tex->inner;
- GLfloat color[4];
- glReadPixels(x, inner->y_inverted ? inner->height - y : y, 1, 1, GL_RGBA,
- GL_FLOAT, color);
- output->alpha = color[3];
- output->red = color[0];
- output->green = color[1];
- output->blue = color[2];
-
- bool ret = glGetError() == GL_NO_ERROR;
- gl_clear_err();
- return ret;
+bool gl_set_image_property(backend_t *backend_data, enum image_properties prop,
+ void *image_data, void *args) {
+ if (prop != IMAGE_PROPERTY_CUSTOM_SHADER) {
+ return default_set_image_property(backend_data, prop, image_data, args);
+ }
+
+ struct backend_image *img = image_data;
+ auto inner = (struct gl_texture *)img->inner;
+ inner->shader = args;
+ return true;
+}
+
+struct gl_shadow_context {
+ double radius;
+ void *blur_context;
+};
+
+struct backend_shadow_context *gl_create_shadow_context(backend_t *base, double radius) {
+ auto ctx = ccalloc(1, struct gl_shadow_context);
+ ctx->radius = radius;
+ ctx->blur_context = NULL;
+
+ if (radius > 0) {
+ struct gaussian_blur_args args = {
+ .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);
+ if (!ctx->blur_context) {
+ log_error("Failed to create shadow context");
+ free(ctx);
+ return NULL;
+ }
+ }
+ return (struct backend_shadow_context *)ctx;
+}
+
+void gl_destroy_shadow_context(backend_t *base attr_unused, struct backend_shadow_context *ctx) {
+ auto ctx_ = (struct gl_shadow_context *)ctx;
+ if (ctx_->blur_context) {
+ gl_destroy_blur_context(base, (struct backend_blur_context *)ctx_->blur_context);
+ }
+ free(ctx_);
+}
+
+void *gl_shadow_from_mask(backend_t *base, void *mask,
+ struct backend_shadow_context *sctx, struct color color) {
+ log_debug("Create shadow from mask");
+ auto gd = (struct gl_data *)base;
+ auto img = (struct backend_image *)mask;
+ auto inner = (struct gl_texture *)img->inner;
+ auto gsctx = (struct gl_shadow_context *)sctx;
+ int radius = (int)gsctx->radius;
+
+ auto new_inner = ccalloc(1, struct gl_texture);
+ new_inner->width = inner->width + radius * 2;
+ new_inner->height = inner->height + radius * 2;
+ new_inner->texture = gl_new_texture(GL_TEXTURE_2D);
+ new_inner->has_alpha = inner->has_alpha;
+ new_inner->y_inverted = true;
+ auto new_img = default_new_backend_image(new_inner->width, new_inner->height);
+ new_img->inner = (struct backend_image_inner_base *)new_inner;
+ new_img->inner->refcount = 1;
+
+ // Render the mask to a texture, so inversion and corner radius can be
+ // applied.
+ auto source_texture = gl_new_texture(GL_TEXTURE_2D);
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_2D, source_texture);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, new_inner->width, new_inner->height, 0,
+ GL_RED, GL_UNSIGNED_BYTE, NULL);
+ glBindTexture(GL_TEXTURE_2D, 0);
+ GLuint fbo;
+ glGenFramebuffers(1, &fbo);
+ glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
+ glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
+ source_texture, 0);
+ glDrawBuffer(GL_COLOR_ATTACHMENT0);
+ if (img->color_inverted) {
+ // If the mask is inverted, clear the source_texture to white, so the
+ // "outside" of the mask would be correct
+ glClearColor(1, 1, 1, 1);
+ } else {
+ glClearColor(0, 0, 0, 1);
+ }
+ glClear(GL_COLOR_BUFFER_BIT);
+ {
+ // clang-format off
+ // interleaved vertex coordinates and texture coordinates
+ GLint coords[] = {radius , radius , 0 , 0,
+ radius + inner->width, radius , inner->width, 0,
+ radius + inner->width, radius + inner->height, inner->width, inner->height,
+ radius , radius + inner->height, 0 , inner->height,};
+ // clang-format on
+ GLuint indices[] = {0, 1, 2, 2, 3, 0};
+ _gl_compose(base, mask, fbo, NULL, (coord_t){0}, coords, indices, 1);
+ }
+
+ gl_check_err();
+
+ auto tmp_texture = source_texture;
+ if (gsctx->blur_context != NULL) {
+ glActiveTexture(GL_TEXTURE0);
+ tmp_texture = gl_new_texture(GL_TEXTURE_2D);
+ glBindTexture(GL_TEXTURE_2D, tmp_texture);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, new_inner->width,
+ new_inner->height, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
+ glBindTexture(GL_TEXTURE_2D, 0);
+
+ glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
+ glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
+ GL_TEXTURE_2D, tmp_texture, 0);
+
+ region_t reg_blur;
+ pixman_region32_init_rect(&reg_blur, 0, 0, (unsigned int)new_inner->width,
+ (unsigned int)new_inner->height);
+ // gl_blur expects reg_blur to be in X coordinate system (i.e. y flipped),
+ // but we are covering the whole texture so we don't need to worry about
+ // that.
+ gl_blur_impl(
+ 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);
+ pixman_region32_fini(&reg_blur);
+ }
+
+ // Colorize the shadow with color.
+ log_debug("Colorize shadow");
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_2D, new_inner->texture);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, new_inner->width, new_inner->height, 0,
+ GL_RGBA, GL_UNSIGNED_BYTE, NULL);
+ glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
+ glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
+ new_inner->texture, 0);
+ glClearColor(0, 0, 0, 0);
+ glClear(GL_COLOR_BUFFER_BIT);
+
+ glBindTexture(GL_TEXTURE_2D, tmp_texture);
+ glUseProgram(gd->shadow_shader.prog);
+ glUniform4f(gd->shadow_shader.uniform_color, (GLfloat)color.red,
+ (GLfloat)color.green, (GLfloat)color.blue, (GLfloat)color.alpha);
+
+ // clang-format off
+ GLuint indices[] = {0, 1, 2, 2, 3, 0};
+ GLint coord[] = {0 , 0 ,
+ new_inner->width , 0 ,
+ new_inner->width , new_inner->height,
+ 0 , new_inner->height,};
+ // clang-format on
+
+ GLuint vao;
+ glGenVertexArrays(1, &vao);
+ glBindVertexArray(vao);
+
+ GLuint bo[2];
+ glGenBuffers(2, bo);
+ glBindBuffer(GL_ARRAY_BUFFER, bo[0]);
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bo[1]);
+ glBufferData(GL_ARRAY_BUFFER, (long)sizeof(*coord) * 8, coord, GL_STATIC_DRAW);
+ glBufferData(GL_ELEMENT_ARRAY_BUFFER, (long)sizeof(*indices) * 6, indices,
+ GL_STATIC_DRAW);
+
+ glEnableVertexAttribArray(vert_coord_loc);
+ glVertexAttribPointer(vert_coord_loc, 2, GL_INT, GL_FALSE, sizeof(GLint) * 2, NULL);
+
+ glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, NULL);
+
+ glDisableVertexAttribArray(vert_coord_loc);
+ glBindVertexArray(0);
+ glDeleteVertexArrays(1, &vao);
+
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
+ glDeleteBuffers(2, bo);
+
+ glDeleteTextures(1, (GLuint[]){source_texture});
+ if (tmp_texture != source_texture) {
+ glDeleteTextures(1, (GLuint[]){tmp_texture});
+ }
+ glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
+ glDeleteFramebuffers(1, &fbo);
+ gl_check_err();
+ return new_img;
+}
+
+enum device_status gl_device_status(backend_t *base) {
+ auto gd = (struct gl_data *)base;
+ if (!gd->has_robustness) {
+ return DEVICE_STATUS_NORMAL;
+ }
+ if (glGetGraphicsResetStatusARB() == GL_NO_ERROR) {
+ return DEVICE_STATUS_NORMAL;
+ }
+ return DEVICE_STATUS_RESETTING;
}
diff --git a/src/backend/gl/gl_common.h b/src/backend/gl/gl_common.h
index b1d93b0..3a78865 100644
--- a/src/backend/gl/gl_common.h
+++ b/src/backend/gl/gl_common.h
@@ -12,16 +12,40 @@
#define CASESTRRET(s) \
case s: return #s
+struct gl_blur_context;
+
+static inline GLint glGetUniformLocationChecked(GLuint p, const char *name) {
+ auto ret = glGetUniformLocation(p, name);
+ if (ret < 0) {
+ log_info("Failed to get location of uniform '%s'. This is normal when "
+ "using custom shaders.",
+ name);
+ }
+ return ret;
+}
+
+#define bind_uniform(shader, uniform) \
+ (shader)->uniform_##uniform = glGetUniformLocationChecked((shader)->prog, #uniform)
// Program and uniforms for window shader
typedef struct {
+ UT_hash_handle hh;
+ uint32_t id;
GLuint prog;
- GLint unifm_opacity;
- GLint unifm_invert_color;
- GLint unifm_tex;
- GLint unifm_dim;
- GLint unifm_brightness;
- GLint unifm_max_brightness;
+ GLint uniform_opacity;
+ GLint uniform_invert_color;
+ GLint uniform_tex;
+ GLint uniform_dim;
+ GLint uniform_brightness;
+ GLint uniform_max_brightness;
+ GLint uniform_corner_radius;
+ GLint uniform_border_width;
+ GLint uniform_time;
+
+ GLint uniform_mask_tex;
+ GLint uniform_mask_offset;
+ GLint uniform_mask_corner_radius;
+ GLint uniform_mask_inverted;
} gl_win_shader_t;
// Program and uniforms for brightness shader
@@ -29,13 +53,23 @@ typedef struct {
GLuint prog;
} gl_brightness_shader_t;
+typedef struct {
+ GLuint prog;
+ GLint uniform_color;
+} gl_shadow_shader_t;
+
// Program and uniforms for blur shader
typedef struct {
GLuint prog;
- GLint unifm_pixel_norm;
- GLint unifm_opacity;
+ GLint uniform_pixel_norm;
+ GLint uniform_opacity;
GLint texorig_loc;
GLint scale_loc;
+
+ GLint uniform_mask_tex;
+ GLint uniform_mask_offset;
+ GLint uniform_mask_corner_radius;
+ GLint uniform_mask_inverted;
} gl_blur_shader_t;
typedef struct {
@@ -53,6 +87,7 @@ struct gl_texture {
// Textures for auxiliary uses.
GLuint auxiliary_texture[2];
+ gl_win_shader_t *shader;
void *user_data;
};
@@ -60,14 +95,22 @@ struct gl_data {
backend_t base;
// If we are using proprietary NVIDIA driver
bool is_nvidia;
+ // If ARB_robustness extension is present
+ bool has_robustness;
+ // If EXT_EGL_image_storage extension is present
+ bool has_egl_image_storage;
// Height and width of the root window
int height, width;
- gl_win_shader_t win_shader;
+ // Hash-table of window shaders
+ gl_win_shader_t *default_shader;
gl_brightness_shader_t brightness_shader;
gl_fill_shader_t fill_shader;
+ gl_shadow_shader_t shadow_shader;
GLuint back_texture, back_fbo;
GLuint present_prog;
+ GLuint default_mask_texture;
+
/// Called when an gl_texture is decoupled from the texture it refers. Returns
/// the decoupled user_data
void *(*decouple_texture_user_data)(backend_t *base, void *user_data);
@@ -83,16 +126,25 @@ typedef struct session session_t;
#define GL_PROG_MAIN_INIT \
{ .prog = 0, .unifm_opacity = -1, .unifm_invert_color = -1, .unifm_tex = -1, }
+void x_rect_to_coords(int nrects, const rect_t *rects, coord_t image_dst,
+ int extent_height, int texture_height, int root_height,
+ bool y_inverted, GLint *coord, GLuint *indices);
+
GLuint gl_create_shader(GLenum shader_type, const char *shader_str);
GLuint gl_create_program(const GLuint *const shaders, int nshaders);
GLuint gl_create_program_from_str(const char *vert_shader_str, const char *frag_shader_str);
+GLuint gl_create_program_from_strv(const char **vert_shaders, const char **frag_shaders);
+void *gl_create_window_shader(backend_t *backend_data, const char *source);
+void gl_destroy_window_shader(backend_t *backend_data, void *shader);
+uint64_t gl_get_shader_attributes(backend_t *backend_data, void *shader);
+bool gl_set_image_property(backend_t *backend_data, enum image_properties prop,
+ void *image_data, void *args);
/**
* @brief Render a region with texture data.
*/
-void gl_compose(backend_t *, void *ptex,
- int dst_x1, int dst_y1, int dst_x2, int dst_y2,
- const region_t *reg_tgt, const region_t *reg_visible);
+void gl_compose(backend_t *, void *image_data, coord_t image_dst, void *mask,
+ coord_t mask_dst, const region_t *reg_tgt, const region_t *reg_visible);
void gl_resize(struct gl_data *, int width, int height);
@@ -105,19 +157,29 @@ bool gl_image_op(backend_t *base, enum image_operations op, void *image_data,
const region_t *reg_op, const region_t *reg_visible, void *arg);
void gl_release_image(backend_t *base, void *image_data);
+void *gl_make_mask(backend_t *base, geometry_t size, const region_t *reg);
void *gl_clone(backend_t *base, const void *image_data, const region_t *reg_visible);
-bool gl_blur(backend_t *base, double opacity, void *, const region_t *reg_blur,
- const region_t *reg_visible);
+bool gl_blur(backend_t *base, double opacity, void *ctx, void *mask, coord_t mask_dst,
+ const region_t *reg_blur, const region_t *reg_visible);
+bool gl_blur_impl(double opacity, struct gl_blur_context *bctx, void *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);
void *gl_create_blur_context(backend_t *base, enum blur_method, 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);
+void *gl_shadow_from_mask(backend_t *base, void *mask,
+ struct backend_shadow_context *sctx, struct color color);
void gl_get_blur_size(void *blur_context, int *width, int *height);
void gl_fill(backend_t *base, struct color, const region_t *clip);
void gl_present(backend_t *base, const region_t *);
bool gl_read_pixel(backend_t *base, void *image_data, int x, int y, struct color *output);
+enum device_status gl_device_status(backend_t *base);
static inline void gl_delete_texture(GLuint texture) {
glDeleteTextures(1, &texture);
@@ -218,3 +280,13 @@ static inline bool gl_has_extension(const char *ext) {
log_info("Missing GL extension %s.", ext);
return false;
}
+
+static const GLuint vert_coord_loc = 0;
+static const GLuint vert_in_texcoord_loc = 1;
+
+#define GLSL(version, ...) "#version " #version "\n" #__VA_ARGS__
+#define QUOTE(...) #__VA_ARGS__
+
+extern const char vertex_shader[], copy_with_mask_frag[], masking_glsl[], dummy_frag[],
+ fill_frag[], fill_vert[], interpolating_frag[], interpolating_vert[], win_shader_glsl[],
+ win_shader_default[], present_vertex_shader[], shadow_colorization_frag[];
diff --git a/src/backend/gl/glx.c b/src/backend/gl/glx.c
index 1397d19..109bec9 100644
--- a/src/backend/gl/glx.c
+++ b/src/backend/gl/glx.c
@@ -302,16 +302,21 @@ static backend_t *glx_init(session_t *ps) {
continue;
}
- gd->ctx = glXCreateContextAttribsARB(ps->dpy, cfg[i], 0, true,
- (int[]){
- GLX_CONTEXT_MAJOR_VERSION_ARB,
- 3,
- GLX_CONTEXT_MINOR_VERSION_ARB,
- 3,
- GLX_CONTEXT_PROFILE_MASK_ARB,
- GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
- 0,
- });
+ int *attributes = (int[]){GLX_CONTEXT_MAJOR_VERSION_ARB,
+ 3,
+ GLX_CONTEXT_MINOR_VERSION_ARB,
+ 3,
+ GLX_CONTEXT_PROFILE_MASK_ARB,
+ GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
+ 0,
+ 0,
+ 0};
+ if (glxext.has_GLX_ARB_create_context_robustness) {
+ attributes[6] = GLX_CONTEXT_RESET_NOTIFICATION_STRATEGY_ARB;
+ attributes[7] = GLX_LOSE_CONTEXT_ON_RESET_ARB;
+ }
+
+ gd->ctx = glXCreateContextAttribsARB(ps->dpy, cfg[i], 0, true, attributes);
free(cfg);
if (!gd->ctx) {
@@ -388,11 +393,10 @@ glx_bind_pixmap(backend_t *base, xcb_pixmap_t pixmap, struct xvisual_info fmt, b
}
log_trace("Binding pixmap %#010x", pixmap);
- auto wd = ccalloc(1, struct backend_image);
- wd->max_brightness = 1;
+ auto wd = default_new_backend_image(r->width, r->height);
auto inner = ccalloc(1, struct gl_texture);
- inner->width = wd->ewidth = r->width;
- inner->height = wd->eheight = r->height;
+ inner->width = r->width;
+ inner->height = r->height;
wd->inner = (struct backend_image_inner_base *)inner;
free(r);
@@ -440,9 +444,6 @@ glx_bind_pixmap(backend_t *base, xcb_pixmap_t pixmap, struct xvisual_info fmt, b
inner->user_data = glxpixmap;
inner->texture = gl_new_texture(GL_TEXTURE_2D);
inner->has_alpha = fmt.alpha_size != 0;
- wd->opacity = 1;
- wd->color_inverted = false;
- wd->dim = 0;
wd->inner->refcount = 1;
glBindTexture(GL_TEXTURE_2D, inner->texture);
glXBindTexImageEXT(gd->display, glxpixmap->glpixmap, GLX_FRONT_LEFT_EXT, NULL);
@@ -494,7 +495,7 @@ static void glx_diagnostics(backend_t *base) {
auto gl_renderer = (const char *)glGetString(GL_RENDERER);
printf("* GL renderer: %s\n", gl_renderer);
- if (strcmp(glx_vendor, "Mesa Project and SGI")) {
+ if (strcmp(glx_vendor, "Mesa Project and SGI") == 0) {
for (size_t i = 0; i < ARR_SIZE(software_renderer_names); i++) {
if (strstr(gl_renderer, software_renderer_names[i]) != NULL) {
warn_software_rendering = true;
@@ -529,19 +530,26 @@ struct backend_operations glx_ops = {
.release_image = gl_release_image,
.compose = gl_compose,
.image_op = gl_image_op,
- .set_image_property = default_set_image_property,
- .read_pixel = gl_read_pixel,
+ .set_image_property = gl_set_image_property,
.clone_image = default_clone_image,
.blur = gl_blur,
.is_image_transparent = default_is_image_transparent,
.present = glx_present,
.buffer_age = glx_buffer_age,
- .render_shadow = default_backend_render_shadow,
+ .create_shadow_context = gl_create_shadow_context,
+ .destroy_shadow_context = gl_destroy_shadow_context,
+ .render_shadow = backend_render_shadow_from_mask,
+ .shadow_from_mask = gl_shadow_from_mask,
+ .make_mask = gl_make_mask,
.fill = gl_fill,
.create_blur_context = gl_create_blur_context,
.destroy_blur_context = gl_destroy_blur_context,
.get_blur_size = gl_get_blur_size,
.diagnostics = glx_diagnostics,
+ .device_status = gl_device_status,
+ .create_shader = gl_create_window_shader,
+ .destroy_shader = gl_destroy_window_shader,
+ .get_shader_attributes = gl_get_shader_attributes,
.max_buffer_age = 5, // Why?
};
@@ -609,6 +617,7 @@ void glxext_init(Display *dpy, int screen) {
check_ext(GLX_EXT_texture_from_pixmap);
check_ext(GLX_ARB_create_context);
check_ext(GLX_EXT_buffer_age);
+ check_ext(GLX_ARB_create_context_robustness);
#ifdef GLX_MESA_query_renderer
check_ext(GLX_MESA_query_renderer);
#endif
diff --git a/src/backend/gl/glx.h b/src/backend/gl/glx.h
index 1061f0b..44b4da0 100644
--- a/src/backend/gl/glx.h
+++ b/src/backend/gl/glx.h
@@ -55,6 +55,7 @@ struct glxext_info {
bool has_GLX_ARB_create_context;
bool has_GLX_EXT_buffer_age;
bool has_GLX_MESA_query_renderer;
+ bool has_GLX_ARB_create_context_robustness;
};
extern struct glxext_info glxext;
diff --git a/src/backend/gl/shaders.c b/src/backend/gl/shaders.c
new file mode 100644
index 0000000..4a18e62
--- /dev/null
+++ b/src/backend/gl/shaders.c
@@ -0,0 +1,187 @@
+#include "gl_common.h"
+
+// clang-format off
+const char dummy_frag[] = GLSL(330,
+ uniform sampler2D tex;
+ in vec2 texcoord;
+ void main() {
+ gl_FragColor = texelFetch(tex, ivec2(texcoord.xy), 0);
+ }
+);
+
+const char copy_with_mask_frag[] = GLSL(330,
+ uniform sampler2D tex;
+ in vec2 texcoord;
+ float mask_factor();
+ void main() {
+ gl_FragColor = texelFetch(tex, ivec2(texcoord.xy), 0) * mask_factor();
+ }
+);
+
+const char fill_frag[] = GLSL(330,
+ uniform vec4 color;
+ void main() {
+ gl_FragColor = color;
+ }
+);
+
+const char fill_vert[] = GLSL(330,
+ layout(location = 0) in vec2 in_coord;
+ uniform mat4 projection;
+ void main() {
+ gl_Position = projection * vec4(in_coord, 0, 1);
+ }
+);
+
+const char interpolating_frag[] = GLSL(330,
+ uniform sampler2D tex;
+ in vec2 texcoord;
+ void main() {
+ gl_FragColor = vec4(texture2D(tex, vec2(texcoord.xy), 0).rgb, 1);
+ }
+);
+
+const char interpolating_vert[] = GLSL(330,
+ uniform mat4 projection;
+ uniform vec2 texsize;
+ layout(location = 0) in vec2 in_coord;
+ layout(location = 1) in vec2 in_texcoord;
+ out vec2 texcoord;
+ void main() {
+ gl_Position = projection * vec4(in_coord, 0, 1);
+ texcoord = in_texcoord / texsize;
+ }
+);
+const char masking_glsl[] = GLSL(330,
+ uniform sampler2D mask_tex;
+ uniform vec2 mask_offset;
+ uniform float mask_corner_radius;
+ uniform bool mask_inverted;
+ in vec2 texcoord;
+ float mask_rectangle_sdf(vec2 point, vec2 half_size) {
+ vec2 d = abs(point) - half_size;
+ return length(max(d, 0.0));
+ }
+ float mask_factor() {
+ vec2 mask_size = textureSize(mask_tex, 0);
+ vec2 maskcoord = texcoord - mask_offset;
+ vec4 mask = texture2D(mask_tex, maskcoord / mask_size);
+ if (mask_corner_radius != 0) {
+ vec2 inner_size = mask_size - vec2(mask_corner_radius) * 2.0f;
+ float dist = mask_rectangle_sdf(maskcoord - mask_size / 2.0f,
+ inner_size / 2.0f) - mask_corner_radius;
+ if (dist > 0.0f) {
+ mask.r *= (1.0f - clamp(dist, 0.0f, 1.0f));
+ }
+ }
+ if (mask_inverted) {
+ mask.rgb = 1.0 - mask.rgb;
+ }
+ return mask.r;
+ }
+);
+const char win_shader_glsl[] = GLSL(330,
+ uniform float opacity;
+ uniform float dim;
+ uniform float corner_radius;
+ uniform float border_width;
+ uniform bool invert_color;
+ in vec2 texcoord;
+ uniform sampler2D tex;
+ uniform sampler2D brightness;
+ uniform float max_brightness;
+ // Signed distance field for rectangle center at (0, 0), with size of
+ // half_size * 2
+ float rectangle_sdf(vec2 point, vec2 half_size) {
+ vec2 d = abs(point) - half_size;
+ return length(max(d, 0.0));
+ }
+
+ vec4 default_post_processing(vec4 c) {
+ vec4 border_color = texture(tex, vec2(0.0, 0.5));
+ if (invert_color) {
+ c = vec4(c.aaa - c.rgb, c.a);
+ border_color = vec4(border_color.aaa - border_color.rgb, border_color.a);
+ }
+ c = vec4(c.rgb * (1.0 - dim), c.a) * opacity;
+ border_color = vec4(border_color.rgb * (1.0 - dim), border_color.a) * opacity;
+
+ vec3 rgb_brightness = texelFetch(brightness, ivec2(0, 0), 0).rgb;
+ // Ref: https://en.wikipedia.org/wiki/Relative_luminance
+ float brightness = rgb_brightness.r * 0.21 +
+ rgb_brightness.g * 0.72 +
+ rgb_brightness.b * 0.07;
+ if (brightness > max_brightness) {
+ c.rgb = c.rgb * (max_brightness / brightness);
+ border_color.rgb = border_color.rgb * (max_brightness / brightness);
+ }
+
+ // Rim color is the color of the outer rim of the window, if there is no
+ // border, it's the color of the window itself, otherwise it's the border.
+ // Using mix() to avoid a branch here.
+ vec4 rim_color = mix(c, border_color, clamp(border_width, 0.0f, 1.0f));
+
+ vec2 outer_size = vec2(textureSize(tex, 0));
+ vec2 inner_size = outer_size - vec2(corner_radius) * 2.0f;
+ float rect_distance = rectangle_sdf(texcoord - outer_size / 2.0f,
+ inner_size / 2.0f) - corner_radius;
+ if (rect_distance > 0.0f) {
+ c = (1.0f - clamp(rect_distance, 0.0f, 1.0f)) * rim_color;
+ } else {
+ float factor = clamp(rect_distance + border_width, 0.0f, 1.0f);
+ c = (1.0f - factor) * c + factor * border_color;
+ }
+
+ return c;
+ }
+
+ vec4 window_shader();
+ float mask_factor();
+
+ void main() {
+ gl_FragColor = window_shader() * mask_factor();
+ }
+);
+
+const char win_shader_default[] = GLSL(330,
+ in vec2 texcoord;
+ uniform sampler2D tex;
+ vec4 default_post_processing(vec4 c);
+ vec4 window_shader() {
+ vec4 c = texelFetch(tex, ivec2(texcoord), 0);
+ return default_post_processing(c);
+ }
+);
+
+const char present_vertex_shader[] = GLSL(330,
+ uniform mat4 projection;
+ layout(location = 0) in vec2 coord;
+ out vec2 texcoord;
+ void main() {
+ gl_Position = projection * vec4(coord, 0, 1);
+ texcoord = coord;
+ }
+);
+const char vertex_shader[] = GLSL(330,
+ uniform mat4 projection;
+ uniform float scale = 1.0;
+ uniform vec2 texorig;
+ layout(location = 0) in vec2 coord;
+ layout(location = 1) in vec2 in_texcoord;
+ out vec2 texcoord;
+ void main() {
+ gl_Position = projection * vec4(coord, 0, scale);
+ texcoord = in_texcoord + texorig;
+ }
+);
+const char shadow_colorization_frag[] = GLSL(330,
+ uniform vec4 color;
+ uniform sampler2D tex;
+ in vec2 texcoord;
+ out vec4 out_color;
+ void main() {
+ vec4 c = texelFetch(tex, ivec2(texcoord), 0);
+ out_color = c.r * color;
+ }
+);
+// clang-format on
diff --git a/src/backend/meson.build b/src/backend/meson.build
index b8f0ad9..1e8e72b 100644
--- a/src/backend/meson.build
+++ b/src/backend/meson.build
@@ -3,5 +3,5 @@ srcs += [ files('backend_common.c', 'xrender/xrender.c', 'dummy/dummy.c', 'backe
# enable opengl
if get_option('opengl')
- srcs += [ files('gl/gl_common.c', 'gl/glx.c') ]
+ srcs += [ files('gl/gl_common.c', 'gl/glx.c', 'gl/blur.c', 'gl/shaders.c', 'gl/egl.c') ]
endif
diff --git a/src/backend/xrender/xrender.c b/src/backend/xrender/xrender.c
index ccf358b..2b7f8e1 100644
--- a/src/backend/xrender/xrender.c
+++ b/src/backend/xrender/xrender.c
@@ -88,19 +88,153 @@ struct _xrender_image_data_inner {
bool owned;
};
-static void compose_impl(struct _xrender_data *xd, const struct backend_image *img,
- int dst_x1, int dst_y1, int dst_x2, int dst_y2,
- const region_t *reg_paint, const region_t *reg_visible,
- xcb_render_picture_t result) {
- auto alpha_pict = xd->alpha_pict[(int)(img->opacity * MAX_ALPHA)];
+struct xrender_rounded_rectangle_cache {
+ int refcount;
+ // A cached picture of a rounded rectangle. Xorg rasterizes shapes on CPU so it's
+ // exceedingly slow.
+ xcb_render_picture_t p;
+};
+
+struct xrender_image {
+ struct backend_image base;
+
+ struct xrender_rounded_rectangle_cache *rounded_rectangle;
+};
+
+/// Make a picture of size width x height, which has a rounded rectangle of corner_radius
+/// rendered in it.
+struct xrender_rounded_rectangle_cache *
+make_rounded_corner_cache(xcb_connection_t *c, xcb_render_picture_t src,
+ xcb_drawable_t root, int width, int height, int corner_radius) {
+ auto picture = x_create_picture_with_standard(c, root, width, height,
+ XCB_PICT_STANDARD_ARGB_32, 0, NULL);
+ if (picture == XCB_NONE) {
+ return NULL;
+ }
+
+ int inner_height = height - 2 * corner_radius;
+ int cap_height = corner_radius;
+ if (inner_height < 0) {
+ cap_height = height / 2;
+ inner_height = 0;
+ }
+ auto points = ccalloc(cap_height * 4 + 4, xcb_render_pointfix_t);
+ int point_count = 0;
+
+#define ADD_POINT(px, py) \
+ assert(point_count < cap_height * 4 + 4); \
+ points[point_count].x = DOUBLE_TO_XFIXED(px); \
+ points[point_count].y = DOUBLE_TO_XFIXED(py); \
+ point_count += 1;
+
+ // The top cap
+ for (int i = 0; i <= cap_height; i++) {
+ double y = corner_radius - i;
+ double delta = sqrt(corner_radius * corner_radius - y * y);
+ double left = corner_radius - delta;
+ double right = width - corner_radius + delta;
+ if (left >= right) {
+ continue;
+ }
+ ADD_POINT(left, i);
+ ADD_POINT(right, i);
+ }
+
+ // The middle rectangle
+ if (inner_height > 0) {
+ ADD_POINT(0, cap_height + inner_height);
+ ADD_POINT(width, cap_height + inner_height);
+ }
+
+ // The bottom cap
+ for (int i = cap_height + inner_height + 1; i <= height; i++) {
+ double y = corner_radius - (height - i);
+ double delta = sqrt(corner_radius * corner_radius - y * y);
+ double left = corner_radius - delta;
+ double right = width - corner_radius + delta;
+ if (left >= right) {
+ break;
+ }
+ ADD_POINT(left, i);
+ ADD_POINT(right, i);
+ }
+#undef ADD_POINT
+
+ XCB_AWAIT_VOID(xcb_render_tri_strip, c, XCB_RENDER_PICT_OP_SRC, src, picture,
+ x_get_pictfmt_for_standard(c, XCB_PICT_STANDARD_A_8), 0, 0,
+ (uint32_t)point_count, points);
+ free(points);
+ auto ret = ccalloc(1, struct xrender_rounded_rectangle_cache);
+ ret->p = picture;
+ ret->refcount = 1;
+ return ret;
+}
+
+static xcb_render_picture_t process_mask(struct _xrender_data *xd, struct xrender_image *mask,
+ xcb_render_picture_t alpha_pict, bool *allocated) {
+ auto inner = (struct _xrender_image_data_inner *)mask->base.inner;
+ if (!mask->base.color_inverted && mask->base.corner_radius == 0) {
+ *allocated = false;
+ return inner->pict;
+ }
+ const auto tmpw = to_u16_checked(inner->width);
+ const auto tmph = to_u16_checked(inner->height);
+ *allocated = true;
+ x_clear_picture_clip_region(xd->base.c, inner->pict);
+ auto ret = x_create_picture_with_visual(
+ xd->base.c, xd->base.root, inner->width, inner->height, inner->visual,
+ XCB_RENDER_CP_REPEAT,
+ (xcb_render_create_picture_value_list_t[]){XCB_RENDER_REPEAT_PAD});
+ xcb_render_composite(xd->base.c, XCB_RENDER_PICT_OP_SRC, inner->pict, XCB_NONE,
+ ret, 0, 0, 0, 0, 0, 0, tmpw, tmph);
+ // Remember: the mask has a 1-pixel border
+ if (mask->base.corner_radius != 0) {
+ if (mask->rounded_rectangle == NULL) {
+ mask->rounded_rectangle = make_rounded_corner_cache(
+ xd->base.c, xd->white_pixel, xd->base.root, inner->width - 2,
+ inner->height - 2, (int)mask->base.corner_radius);
+ }
+ xcb_render_composite(xd->base.c, XCB_RENDER_PICT_OP_IN_REVERSE,
+ mask->rounded_rectangle->p, XCB_NONE, ret, 0, 0, 0,
+ 0, 1, 1, (uint16_t)(tmpw - 2), (uint16_t)(tmph - 2));
+ }
+
+ if (mask->base.color_inverted) {
+ xcb_render_composite(xd->base.c, XCB_RENDER_PICT_OP_XOR, xd->white_pixel,
+ XCB_NONE, ret, 0, 0, 0, 0, 0, 0, tmpw, tmph);
+ }
+
+ if (alpha_pict != XCB_NONE) {
+ xcb_render_composite(xd->base.c, XCB_RENDER_PICT_OP_SRC, ret, alpha_pict,
+ ret, 0, 0, 0, 0, 0, 0, to_u16_checked(inner->width),
+ to_u16_checked(inner->height));
+ }
+
+ return ret;
+}
+
+static void
+compose_impl(struct _xrender_data *xd, struct xrender_image *xrimg, coord_t dst,
+ struct xrender_image *mask, coord_t mask_dst, const region_t *reg_paint,
+ const region_t *reg_visible, xcb_render_picture_t result) {
+ const struct backend_image *img = &xrimg->base;
+ bool mask_allocated = false;
+ auto mask_pict = xd->alpha_pict[(int)(img->opacity * MAX_ALPHA)];
+ if (mask != NULL) {
+ mask_pict = process_mask(
+ xd, mask, img->opacity < 1.0 ? mask_pict : XCB_NONE, &mask_allocated);
+ }
auto inner = (struct _xrender_image_data_inner *)img->inner;
region_t reg;
bool has_alpha = inner->has_alpha || img->opacity != 1;
- const auto tmpw = to_u16_checked(dst_x2 - dst_x1);
- const auto tmph = to_u16_checked(dst_y2 - dst_y1);
- const auto tmpew = to_u16_checked(dst_x2 - dst_x1);
- const auto tmpeh = to_u16_checked(dst_y2 - dst_y1);
+ const auto tmpw = to_u16_checked(inner->width);
+ const auto tmph = to_u16_checked(inner->height);
+ const auto tmpew = to_u16_checked(img->ewidth);
+ const auto tmpeh = to_u16_checked(img->eheight);
+ // Remember: the mask has a 1-pixel border
+ const auto mask_dst_x = to_i16_checked(dst.x - mask_dst.x + 1);
+ const auto mask_dst_y = to_i16_checked(dst.y - mask_dst.y + 1);
const xcb_render_color_t dim_color = {
.red = 0, .green = 0, .blue = 0, .alpha = (uint16_t)(0xffff * img->dim)};
@@ -111,20 +245,12 @@ static void compose_impl(struct _xrender_data *xd, const struct backend_image *i
pixman_region32_init(&reg);
pixman_region32_intersect(&reg, (region_t *)reg_paint, (region_t *)reg_visible);
x_set_picture_clip_region(xd->base.c, result, 0, 0, &reg);
-
-#define DOUBLE_TO_XFIXED(value) ((xcb_render_fixed_t)(((double)(value)) * 65536))
- {
- const xcb_render_transform_t transform = {
- DOUBLE_TO_XFIXED((double)img->ewidth / (double)tmpew), DOUBLE_TO_XFIXED(0.0), DOUBLE_TO_XFIXED(0.0),
- DOUBLE_TO_XFIXED(0.0), DOUBLE_TO_XFIXED((double)img->eheight / (double)tmpeh), DOUBLE_TO_XFIXED(0.0),
- DOUBLE_TO_XFIXED(0.0), DOUBLE_TO_XFIXED(0.0), DOUBLE_TO_XFIXED(1.0),
- };
- xcb_render_set_picture_transform(xd->base.c, inner->pict, transform);
- xcb_render_set_picture_filter(xd->base.c, inner->pict, 7, "nearest", 0, NULL);
- }
-#undef DOUBLE_TO_XFIXED
-
- if ((img->color_inverted || img->dim != 0) && has_alpha) {
+ if (img->corner_radius != 0 && xrimg->rounded_rectangle == NULL) {
+ xrimg->rounded_rectangle = make_rounded_corner_cache(
+ xd->base.c, xd->white_pixel, xd->base.root, inner->width,
+ inner->height, (int)img->corner_radius);
+ }
+ if (((img->color_inverted || img->dim != 0) && has_alpha) || img->corner_radius != 0) {
// Apply image properties using a temporary image, because the source
// image is transparent. Otherwise the properties can be applied directly
// on the target image.
@@ -133,17 +259,25 @@ static void compose_impl(struct _xrender_data *xd, const struct backend_image *i
inner->height, inner->visual, 0, NULL);
// Set clip region translated to source coordinate
- x_set_picture_clip_region(xd->base.c, tmp_pict, to_i16_checked(-dst_x1),
- to_i16_checked(-dst_y1), &reg);
+ x_set_picture_clip_region(xd->base.c, tmp_pict, to_i16_checked(-dst.x),
+ to_i16_checked(-dst.y), &reg);
// Copy source -> tmp
- xcb_render_composite(xd->base.c, XCB_RENDER_PICT_OP_OVER, inner->pict,
+ xcb_render_composite(xd->base.c, XCB_RENDER_PICT_OP_SRC, inner->pict,
XCB_NONE, tmp_pict, 0, 0, 0, 0, 0, 0, tmpw, tmph);
+
+ if (img->corner_radius != 0 && xrimg->rounded_rectangle != NULL) {
+ // Clip tmp_pict with a rounded rectangle
+ xcb_render_composite(xd->base.c, XCB_RENDER_PICT_OP_IN_REVERSE,
+ xrimg->rounded_rectangle->p, XCB_NONE,
+ tmp_pict, 0, 0, 0, 0, 0, 0, tmpw, tmph);
+ }
+
if (img->color_inverted) {
if (inner->has_alpha) {
auto tmp_pict2 = x_create_picture_with_visual(
xd->base.c, xd->base.root, tmpw, tmph, inner->visual,
0, NULL);
- xcb_render_composite(xd->base.c, XCB_RENDER_PICT_OP_OVER,
+ xcb_render_composite(xd->base.c, XCB_RENDER_PICT_OP_SRC,
tmp_pict, XCB_NONE, tmp_pict2, 0, 0,
0, 0, 0, 0, tmpw, tmph);
@@ -174,30 +308,31 @@ static void compose_impl(struct _xrender_data *xd, const struct backend_image *i
}
xcb_render_composite(xd->base.c, XCB_RENDER_PICT_OP_OVER, tmp_pict,
- alpha_pict, result, 0, 0, 0, 0, to_i16_checked(dst_x1),
- to_i16_checked(dst_y1), tmpew, tmpeh);
+ mask_pict, result, 0, 0, mask_dst_x, mask_dst_y,
+ to_i16_checked(dst.x), to_i16_checked(dst.y), tmpew,
+ tmpeh);
xcb_render_free_picture(xd->base.c, tmp_pict);
} else {
uint8_t op = (has_alpha ? XCB_RENDER_PICT_OP_OVER : XCB_RENDER_PICT_OP_SRC);
- xcb_render_composite(xd->base.c, op, inner->pict, alpha_pict, result, 0,
- 0, 0, 0, to_i16_checked(dst_x1),
- to_i16_checked(dst_y1), tmpew, tmpeh);
+ xcb_render_composite(xd->base.c, op, inner->pict, mask_pict, result, 0, 0,
+ mask_dst_x, mask_dst_y, to_i16_checked(dst.x),
+ to_i16_checked(dst.y), tmpew, tmpeh);
if (img->dim != 0 || img->color_inverted) {
// Apply properties, if we reach here, then has_alpha == false
assert(!has_alpha);
if (img->color_inverted) {
xcb_render_composite(xd->base.c, XCB_RENDER_PICT_OP_DIFFERENCE,
xd->white_pixel, XCB_NONE, result, 0,
- 0, 0, 0, to_i16_checked(dst_x1),
- to_i16_checked(dst_y1), tmpew, tmpeh);
+ 0, 0, 0, to_i16_checked(dst.x),
+ to_i16_checked(dst.y), tmpew, tmpeh);
}
if (img->dim != 0) {
// Dim the actually content of window
xcb_rectangle_t rect = {
- .x = to_i16_checked(dst_x1),
- .y = to_i16_checked(dst_y1),
+ .x = to_i16_checked(dst.x),
+ .y = to_i16_checked(dst.y),
.width = tmpew,
.height = tmpeh,
};
@@ -207,15 +342,17 @@ static void compose_impl(struct _xrender_data *xd, const struct backend_image *i
}
}
}
+ if (mask_allocated) {
+ xcb_render_free_picture(xd->base.c, mask_pict);
+ }
pixman_region32_fini(&reg);
}
-static void compose(backend_t *base, void *img_data,
- int dst_x1, int dst_y1, int dst_x2, int dst_y2,
+static void compose(backend_t *base, void *img_data, coord_t dst, void *mask, coord_t mask_dst,
const region_t *reg_paint, const region_t *reg_visible) {
- // TODO(dccsillag): use dst_{x,y}2
struct _xrender_data *xd = (void *)base;
- return compose_impl(xd, img_data, dst_x1, dst_y1, dst_x2, dst_y2, reg_paint, reg_visible, xd->back[2]);
+ return compose_impl(xd, img_data, dst, mask, mask_dst, reg_paint, reg_visible,
+ xd->back[2]);
}
static void fill(backend_t *base, struct color c, const region_t *clip) {
@@ -236,8 +373,8 @@ static void fill(backend_t *base, struct color c, const region_t *clip) {
.height = to_u16_checked(extent->y2 - extent->y1)}});
}
-static bool blur(backend_t *backend_data, double opacity, void *ctx_,
- const region_t *reg_blur, const region_t *reg_visible) {
+static bool blur(backend_t *backend_data, double opacity, void *ctx_, void *mask,
+ coord_t mask_dst, const region_t *reg_blur, const region_t *reg_visible) {
struct _xrender_blur_context *bctx = ctx_;
if (bctx->method == BLUR_METHOD_NONE) {
return true;
@@ -288,7 +425,12 @@ static bool blur(backend_t *backend_data, double opacity, void *ctx_,
pixman_region32_fini(&clip);
xcb_render_picture_t src_pict = xd->back[2], dst_pict = tmp_picture[0];
- auto alpha_pict = xd->alpha_pict[(int)(opacity * MAX_ALPHA)];
+ auto mask_pict = xd->alpha_pict[(int)(opacity * MAX_ALPHA)];
+ bool mask_allocated = false;
+ if (mask != NULL) {
+ mask_pict = process_mask(xd, mask, opacity != 1.0 ? mask_pict : XCB_NONE,
+ &mask_allocated);
+ }
int current = 0;
x_set_picture_clip_region(c, src_pict, 0, 0, &reg_op_resized);
@@ -324,11 +466,12 @@ static bool blur(backend_t *backend_data, double opacity, void *ctx_,
} else {
x_set_picture_clip_region(c, xd->back[2], 0, 0, &reg_op);
// This is the last pass, and we are doing more than 1 pass
- xcb_render_composite(c, XCB_RENDER_PICT_OP_OVER, src_pict,
- alpha_pict, xd->back[2], 0, 0, 0, 0,
- to_i16_checked(extent_resized->x1),
- to_i16_checked(extent_resized->y1),
- width_resized, height_resized);
+ xcb_render_composite(
+ c, XCB_RENDER_PICT_OP_OVER, src_pict, mask_pict, xd->back[2],
+ 0, 0, to_i16_checked(extent_resized->x1 - mask_dst.x + 1),
+ to_i16_checked(extent_resized->y1 - mask_dst.y + 1),
+ to_i16_checked(extent_resized->x1),
+ to_i16_checked(extent_resized->y1), width_resized, height_resized);
}
// reset filter
@@ -344,8 +487,10 @@ static bool blur(backend_t *backend_data, double opacity, void *ctx_,
if (i == 1) {
x_set_picture_clip_region(c, xd->back[2], 0, 0, &reg_op);
xcb_render_composite(
- c, XCB_RENDER_PICT_OP_OVER, src_pict, alpha_pict, xd->back[2], 0, 0,
- 0, 0, to_i16_checked(extent_resized->x1),
+ c, XCB_RENDER_PICT_OP_OVER, src_pict, mask_pict, xd->back[2], 0, 0,
+ to_i16_checked(extent_resized->x1 - mask_dst.x + 1),
+ to_i16_checked(extent_resized->y1 - mask_dst.y + 1),
+ to_i16_checked(extent_resized->x1),
to_i16_checked(extent_resized->y1), width_resized, height_resized);
}
@@ -366,11 +511,11 @@ bind_pixmap(backend_t *base, xcb_pixmap_t pixmap, struct xvisual_info fmt, bool
return NULL;
}
- auto img = ccalloc(1, struct backend_image);
+ auto img = ccalloc(1, struct xrender_image);
auto inner = ccalloc(1, struct _xrender_image_data_inner);
inner->depth = (uint8_t)fmt.visual_depth;
- inner->width = img->ewidth = r->width;
- inner->height = img->eheight = r->height;
+ inner->width = img->base.ewidth = r->width;
+ inner->height = img->base.eheight = r->height;
inner->pixmap = pixmap;
inner->has_alpha = fmt.alpha_size != 0;
inner->pict =
@@ -379,8 +524,9 @@ bind_pixmap(backend_t *base, xcb_pixmap_t pixmap, struct xvisual_info fmt, bool
inner->visual = fmt.visual;
inner->refcount = 1;
- img->inner = (struct backend_image_inner_base *)inner;
- img->opacity = 1;
+ img->base.inner = (struct backend_image_inner_base *)inner;
+ img->base.opacity = 1;
+ img->rounded_rectangle = NULL;
free(r);
if (inner->pict == XCB_NONE) {
@@ -397,11 +543,28 @@ static void release_image_inner(backend_t *base, struct _xrender_image_data_inne
}
free(inner);
}
+
+static void
+release_rounded_corner_cache(backend_t *base, struct xrender_rounded_rectangle_cache *cache) {
+ if (!cache) {
+ return;
+ }
+
+ assert(cache->refcount > 0);
+ cache->refcount--;
+ if (cache->refcount == 0) {
+ xcb_free_pixmap(base->c, cache->p);
+ free(cache);
+ }
+}
+
static void release_image(backend_t *base, void *image) {
- struct backend_image *img = image;
- img->inner->refcount--;
- if (img->inner->refcount == 0) {
- release_image_inner(base, (void *)img->inner);
+ struct xrender_image *img = image;
+ release_rounded_corner_cache(base, img->rounded_rectangle);
+ img->rounded_rectangle = NULL;
+ img->base.inner->refcount -= 1;
+ if (img->base.inner->refcount == 0) {
+ release_image_inner(base, (void *)img->base.inner);
}
free(img);
}
@@ -523,6 +686,52 @@ new_inner(backend_t *base, int w, int h, xcb_visualid_t visual, uint8_t depth) {
return new_inner;
}
+static void *make_mask(backend_t *base, geometry_t size, const region_t *reg) {
+ struct _xrender_data *xd = (void *)base;
+ // Give the mask a 1 pixel wide border to emulate the clamp to border behavior of
+ // OpenGL textures.
+ auto w16 = to_u16_checked(size.width + 2);
+ auto h16 = to_u16_checked(size.height + 2);
+ auto inner =
+ new_inner(base, size.width + 2, size.height + 2,
+ x_get_visual_for_standard(base->c, XCB_PICT_STANDARD_ARGB_32), 32);
+ xcb_render_change_picture(base->c, inner->pict, XCB_RENDER_CP_REPEAT,
+ (uint32_t[]){XCB_RENDER_REPEAT_PAD});
+ const rect_t *extent = pixman_region32_extents((region_t *)reg);
+ x_set_picture_clip_region(base->c, xd->back[2], 1, 1, reg);
+ xcb_render_fill_rectangles(
+ base->c, XCB_RENDER_PICT_OP_SRC, inner->pict,
+ (xcb_render_color_t){.red = 0, .green = 0, .blue = 0, .alpha = 0xffff}, 1,
+ (xcb_rectangle_t[]){{.x = to_i16_checked(extent->x1 + 1),
+ .y = to_i16_checked(extent->y1 + 1),
+ .width = to_u16_checked(extent->x2 - extent->x1),
+ .height = to_u16_checked(extent->y2 - extent->y1)}});
+ x_clear_picture_clip_region(xd->base.c, inner->pict);
+
+ // Paint the border transparent
+ xcb_render_fill_rectangles(
+ base->c, XCB_RENDER_PICT_OP_SRC, inner->pict,
+ (xcb_render_color_t){.red = 0, .green = 0, .blue = 0, .alpha = 0}, 4,
+ (xcb_rectangle_t[]){{.x = 0, .y = 0, .width = w16, .height = 1},
+ {.x = 0, .y = 0, .width = 1, .height = h16},
+ {.x = 0, .y = (short)(h16 - 1), .width = w16, .height = 1},
+ {.x = (short)(w16 - 1), .y = 0, .width = 1, .height = h16}});
+ inner->refcount = 1;
+
+ auto img = ccalloc(1, struct xrender_image);
+ img->base.eheight = size.height + 2;
+ img->base.ewidth = size.width + 2;
+ img->base.border_width = 0;
+ img->base.color_inverted = false;
+ img->base.corner_radius = 0;
+ img->base.max_brightness = 1;
+ img->base.opacity = 1;
+ img->base.dim = 0;
+ img->base.inner = (struct backend_image_inner_base *)inner;
+ img->rounded_rectangle = NULL;
+ return img;
+}
+
static bool decouple_image(backend_t *base, struct backend_image *img, const region_t *reg) {
if (img->inner->refcount == 1) {
return true;
@@ -648,30 +857,6 @@ static void get_blur_size(void *blur_context, int *width, int *height) {
*height = ctx->resize_height;
}
-static bool
-read_pixel(backend_t *backend_data, void *image_data, int x, int y, struct color *output) {
- auto xd = (struct _xrender_data *)backend_data;
- auto img = (struct backend_image *)image_data;
- auto inner = (struct _xrender_image_data_inner *)img->inner;
-
- auto r = XCB_AWAIT(xcb_get_image, xd->base.c, XCB_IMAGE_FORMAT_XY_PIXMAP, inner->pixmap,
- to_i16_checked(x), to_i16_checked(y), 1, 1, (uint32_t)-1L);
-
- if (!r) {
- return false;
- }
-
- // Color format seems to be BGRA8888, see glamor_format_for_pixmap from the
- // Xserver codebase.
- uint8_t *pixels = xcb_get_image_data(r);
- output->blue = pixels[0] / 255.0;
- output->green = pixels[1] / 255.0;
- output->red = pixels[2] / 255.0;
- output->alpha = pixels[3] / 255.0;
-
- return true;
-}
-
static backend_t *backend_xrender_init(session_t *ps) {
auto xd = ccalloc(1, struct _xrender_data);
init_backend_base(&xd->base, ps);
@@ -751,6 +936,29 @@ err:
return NULL;
}
+void *clone_image(backend_t *base attr_unused, const void *image_data,
+ const region_t *reg_visible attr_unused) {
+ auto new_img = ccalloc(1, struct xrender_image);
+ *new_img = *(struct xrender_image *)image_data;
+ new_img->base.inner->refcount++;
+ if (new_img->rounded_rectangle) {
+ new_img->rounded_rectangle->refcount++;
+ }
+ return new_img;
+}
+
+static bool
+set_image_property(backend_t *base, enum image_properties op, void *image, void *args) {
+ auto xrimg = (struct xrender_image *)image;
+ if (op == IMAGE_PROPERTY_CORNER_RADIUS &&
+ ((double *)args)[0] != xrimg->base.corner_radius) {
+ // Free cached rounded rectangle if corner radius changed
+ release_rounded_corner_cache(base, xrimg->rounded_rectangle);
+ xrimg->rounded_rectangle = NULL;
+ }
+ return default_set_image_property(base, op, image, args);
+}
+
struct backend_operations xrender_ops = {
.init = backend_xrender_init,
.deinit = deinit,
@@ -760,7 +968,10 @@ struct backend_operations xrender_ops = {
.fill = fill,
.bind_pixmap = bind_pixmap,
.release_image = release_image,
+ .create_shadow_context = default_create_shadow_context,
+ .destroy_shadow_context = default_destroy_shadow_context,
.render_shadow = default_backend_render_shadow,
+ .make_mask = make_mask,
//.prepare_win = prepare_win,
//.release_win = release_win,
.is_image_transparent = default_is_image_transparent,
@@ -768,9 +979,8 @@ struct backend_operations xrender_ops = {
.max_buffer_age = 2,
.image_op = image_op,
- .read_pixel = read_pixel,
- .clone_image = default_clone_image,
- .set_image_property = default_set_image_property,
+ .clone_image = clone_image,
+ .set_image_property = set_image_property,
.create_blur_context = create_blur_context,
.destroy_blur_context = destroy_blur_context,
.get_blur_size = get_blur_size,