feat: Add box shadows (#64)

Co-authored-by: Erik Reider <35975961+ErikReider@users.noreply.github.com>
This commit is contained in:
William McKinnon 2023-01-18 01:49:26 -05:00 committed by GitHub
parent 1baba77c74
commit 588ea8e290
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 388 additions and 13 deletions

View file

@ -13,6 +13,10 @@ Sway is an incredible window manager, and certainly one of the most well establi
+ Corner radius: `corner_radius <val>`
+ Application saturation: `for_window [CRITERIA HERE] saturation <set|plus|minus> <val 0.0 <-> 2.0>`
+ Window shadows:
- `shadows on|off`
- `shadow_blur_radius <integer value 0 - 100>`
- `shadow_color <hex color with alpha> ex, #0000007F`
+ Dim unfocused windows:
- `dim_inactive <float value 0.0 - 1.0>`
- `dim_inactive_colors.unfocused <hex color> ex, #000000FF`
@ -22,7 +26,6 @@ Sway is an incredible window manager, and certainly one of the most well establi
+ fade in / out animations
+ window movement animations
+ drop shadows
+ blur
## Installation

View file

@ -24,6 +24,10 @@ set $menu dmenu_path | dmenu | xargs swaymsg exec --
# window corner radius in px
corner_radius 10
shadows off
shadow_blur_radius 20
shadow_color #0000007F
# inactive window fade amount. 0.0 = no dimming, 1.0 = fully dimmed
dim_inactive 0.0
dim_inactive_colors.unfocused #000000FF

View file

@ -178,6 +178,9 @@ sway_cmd cmd_scratchpad;
sway_cmd cmd_seamless_mouse;
sway_cmd cmd_set;
sway_cmd cmd_shortcuts_inhibitor;
sway_cmd cmd_shadow_blur_radius;
sway_cmd cmd_shadow_color;
sway_cmd cmd_shadows;
sway_cmd cmd_show_marks;
sway_cmd cmd_smart_borders;
sway_cmd cmd_smart_gaps;

View file

@ -482,6 +482,9 @@ struct sway_config {
float unfocused[4];
float urgent[4];
} dim_inactive_colors;
bool shadow_enabled;
int shadow_blur_sigma;
float shadow_color[4];
char *swaynag_command;
struct swaynag_instance swaynag_config_errors;

View file

@ -47,6 +47,8 @@ struct fx_renderer {
float projection[9];
GLuint stencil_buffer_id;
struct {
bool OES_egl_image_external;
} exts;
@ -84,6 +86,17 @@ struct fx_renderer {
GLint half_thickness;
} corner;
struct {
GLuint program;
GLint proj;
GLint color;
GLint pos_attrib;
GLint position;
GLint size;
GLint blur_sigma;
GLint corner_radius;
} box_shadow;
struct gles2_tex_shader tex_rgba;
struct gles2_tex_shader tex_rgbx;
struct gles2_tex_shader tex_ext;
@ -118,4 +131,7 @@ void fx_render_border_corner(struct fx_renderer *renderer, const struct wlr_box
const float color[static 4], const float projection[static 9],
enum corner_location corner_location, int radius, int border_thickness);
void fx_render_box_shadow(struct fx_renderer *renderer, const struct wlr_box *box,
const float color[static 4], const float projection[static 9], int radius, float blur_sigma);
#endif

View file

@ -113,6 +113,8 @@ struct sway_container {
// Hidden scratchpad containers have a NULL parent.
bool scratchpad;
bool shadow_enabled;
float saturation;
float alpha;

View file

@ -88,6 +88,9 @@ static const struct cmd_handler handlers[] = {
{ "popup_during_fullscreen", cmd_popup_during_fullscreen },
{ "seat", cmd_seat },
{ "set", cmd_set },
{ "shadow_blur_radius", cmd_shadow_blur_radius },
{ "shadow_color", cmd_shadow_color },
{ "shadows", cmd_shadows },
{ "show_marks", cmd_show_marks },
{ "smart_borders", cmd_smart_borders },
{ "smart_gaps", cmd_smart_gaps },

View file

@ -0,0 +1,25 @@
#include <string.h>
#include "sway/commands.h"
#include "sway/config.h"
#include "sway/tree/arrange.h"
#include "sway/tree/container.h"
#include "log.h"
struct cmd_results *cmd_shadow_blur_radius(int argc, char **argv) {
struct cmd_results *error = NULL;
if ((error = checkarg(argc, "shadow_blur_radius", EXPECTED_EQUAL_TO, 1))) {
return error;
}
char *inv;
int value = strtol(argv[0], &inv, 10);
if (*inv != '\0' || value < 0 || value > 99) {
return cmd_results_new(CMD_FAILURE, "Invalid size specified");
}
config->shadow_blur_sigma = value;
arrange_root();
return cmd_results_new(CMD_SUCCESS, NULL);
}

View file

@ -0,0 +1,25 @@
#include "log.h"
#include "sway/commands.h"
#include "sway/config.h"
#include "sway/output.h"
#include "sway/tree/arrange.h"
#include "sway/tree/container.h"
#include "util.h"
struct cmd_results *cmd_shadow_color(int argc, char **argv) {
struct cmd_results *error = NULL;
if ((error = checkarg(argc, "shadow_color", EXPECTED_AT_LEAST, 1))) {
return error;
}
uint32_t color;
if (!parse_color(argv[0], &color)) {
return cmd_results_new(CMD_INVALID, "Invalid %s color %s",
"shadow_color", argv[0]);
}
color_to_rgba(config->shadow_color, color);
arrange_root();
return cmd_results_new(CMD_SUCCESS, NULL);
}

31
sway/commands/shadows.c Normal file
View file

@ -0,0 +1,31 @@
#include <string.h>
#include "sway/commands.h"
#include "sway/config.h"
#include "sway/tree/arrange.h"
#include "sway/tree/view.h"
#include "sway/tree/container.h"
#include "log.h"
#include "stringop.h"
#include "util.h"
struct cmd_results *cmd_shadows(int argc, char **argv) {
struct cmd_results *error = checkarg(argc, "shadows", EXPECTED_AT_LEAST, 1);
if (error) {
return error;
}
struct sway_container *con = config->handler_context.container;
bool result = parse_boolean(argv[0], config->shadow_enabled);
if (con == NULL) {
config->shadow_enabled = result;
} else {
con->shadow_enabled = result;
container_damage_whole(con);
}
arrange_root();
return cmd_results_new(CMD_SUCCESS, NULL);
}

View file

@ -338,6 +338,9 @@ static void config_defaults(struct sway_config *config) {
config->dim_inactive = 0.0f;
color_to_rgba(config->dim_inactive_colors.unfocused, 0x000000FF);
color_to_rgba(config->dim_inactive_colors.urgent, 0x900000FF);
config->shadow_enabled = false;
config->shadow_blur_sigma = 20.0f;
color_to_rgba(config->shadow_color, 0x0000007F);
// The keysym to keycode translation
struct xkb_rule_names rules = {0};

View file

@ -24,6 +24,7 @@
#include "quad_round_tl_frag_src.h"
#include "quad_round_tr_frag_src.h"
#include "corner_frag_src.h"
#include "box_shadow_frag_src.h"
#include "tex_rgba_frag_src.h"
#include "tex_rgbx_frag_src.h"
#include "tex_external_frag_src.h"
@ -108,6 +109,7 @@ static GLuint compile_shader(GLuint type, const GLchar *src) {
GLint ok;
glGetShaderiv(shader, GL_COMPILE_STATUS, &ok);
if (ok == GL_FALSE) {
sway_log(SWAY_ERROR, "Failed to compile shader");
glDeleteShader(shader);
shader = 0;
}
@ -140,6 +142,7 @@ static GLuint link_program(const GLchar *vert_src, const GLchar *frag_src) {
GLint ok;
glGetProgramiv(prog, GL_LINK_STATUS, &ok);
if (ok == GL_FALSE) {
sway_log(SWAY_ERROR, "Failed to link shader");
glDeleteProgram(prog);
goto error;
}
@ -294,6 +297,20 @@ struct fx_renderer *fx_renderer_create(struct wlr_egl *egl) {
renderer->shaders.corner.half_size = glGetUniformLocation(prog, "half_size");
renderer->shaders.corner.half_thickness = glGetUniformLocation(prog, "half_thickness");
// box shadow shader
prog = link_program(common_vert_src, box_shadow_frag_src);
renderer->shaders.box_shadow.program = prog;
if (!renderer->shaders.box_shadow.program) {
goto error;
}
renderer->shaders.box_shadow.proj = glGetUniformLocation(prog, "proj");
renderer->shaders.box_shadow.color = glGetUniformLocation(prog, "color");
renderer->shaders.box_shadow.pos_attrib = glGetAttribLocation(prog, "pos");
renderer->shaders.box_shadow.position = glGetUniformLocation(prog, "position");
renderer->shaders.box_shadow.size = glGetUniformLocation(prog, "size");
renderer->shaders.box_shadow.blur_sigma = glGetUniformLocation(prog, "blur_sigma");
renderer->shaders.box_shadow.corner_radius = glGetUniformLocation(prog, "corner_radius");
// fragment shaders
prog = link_program(common_vert_src, tex_rgba_frag_src);
if (!init_frag_shader(&renderer->shaders.tex_rgba, prog)) {
@ -323,6 +340,7 @@ error:
glDeleteProgram(renderer->shaders.rounded_tl_quad.program);
glDeleteProgram(renderer->shaders.rounded_tr_quad.program);
glDeleteProgram(renderer->shaders.corner.program);
glDeleteProgram(renderer->shaders.box_shadow.program);
glDeleteProgram(renderer->shaders.tex_rgba.program);
glDeleteProgram(renderer->shaders.tex_rgbx.program);
glDeleteProgram(renderer->shaders.tex_ext.program);
@ -340,6 +358,15 @@ error:
}
void fx_renderer_begin(struct fx_renderer *renderer, uint32_t width, uint32_t height) {
// Create and render the stencil buffer
if (renderer->stencil_buffer_id == 0) {
glGenRenderbuffers(1, &renderer->stencil_buffer_id);
}
glBindRenderbuffer(GL_RENDERBUFFER, renderer->stencil_buffer_id);
glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
GL_RENDERBUFFER, renderer->stencil_buffer_id);
glViewport(0, 0, width, height);
// refresh projection matrix
@ -354,8 +381,9 @@ void fx_renderer_end() {
}
void fx_renderer_clear(const float color[static 4]) {
glClearColor(color[0], color[1], color[2], color[3]);
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(color[0], color[1], color[2], color[3]);
glClearStencil(0);
glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
}
void fx_renderer_scissor(struct wlr_box *box) {
@ -616,3 +644,80 @@ void fx_render_border_corner(struct fx_renderer *renderer, const struct wlr_box
glDisableVertexAttribArray(renderer->shaders.corner.pos_attrib);
}
// TODO: alpha input arg?
void fx_render_box_shadow(struct fx_renderer *renderer, const struct wlr_box *box,
const float color[static 4], const float projection[static 9],
int corner_radius, float blur_sigma) {
if (box->width == 0 || box->height == 0) {
return;
}
assert(box->width > 0 && box->height > 0);
float matrix[9];
wlr_matrix_project_box(matrix, box, WL_OUTPUT_TRANSFORM_NORMAL, 0, projection);
float gl_matrix[9];
wlr_matrix_multiply(gl_matrix, renderer->projection, matrix);
// TODO: investigate why matrix is flipped prior to this cmd
// wlr_matrix_multiply(gl_matrix, flip_180, gl_matrix);
wlr_matrix_transpose(gl_matrix, gl_matrix);
// Init stencil work
// NOTE: Alpha needs to be set to 1.0 to be able to discard any "empty" pixels
const float col[4] = {0.0, 0.0, 0.0, 1.0};
struct wlr_box inner_box;
memcpy(&inner_box, box, sizeof(struct wlr_box));
inner_box.x += blur_sigma;
inner_box.y += blur_sigma;
inner_box.width -= blur_sigma * 2;
inner_box.height -= blur_sigma * 2;
glEnable(GL_STENCIL_TEST);
glClearStencil(0);
glClear(GL_STENCIL_BUFFER_BIT);
glStencilFunc(GL_ALWAYS, 1, 0xFF);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
// Disable writing to color buffer
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
// Draw the rounded rect as a mask
fx_render_rounded_rect(renderer, &inner_box, col, projection, corner_radius, ALL);
// Close the mask
glStencilFunc(GL_NOTEQUAL, 1, 0xFF);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
// Reenable writing to color buffer
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
// blending will practically always be needed (unless we have a madman
// who uses opaque shadows with zero sigma), so just enable it
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glUseProgram(renderer->shaders.box_shadow.program);
glUniformMatrix3fv(renderer->shaders.box_shadow.proj, 1, GL_FALSE, gl_matrix);
glUniform4f(renderer->shaders.box_shadow.color, color[0], color[1], color[2], color[3]);
glUniform1f(renderer->shaders.box_shadow.blur_sigma, blur_sigma);
glUniform1f(renderer->shaders.box_shadow.corner_radius, corner_radius);
glUniform2f(renderer->shaders.box_shadow.size, box->width, box->height);
glUniform2f(renderer->shaders.box_shadow.position, box->x, box->y);
glVertexAttribPointer(renderer->shaders.box_shadow.pos_attrib, 2, GL_FLOAT, GL_FALSE,
0, verts);
glEnableVertexAttribArray(renderer->shaders.box_shadow.pos_attrib);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDisableVertexAttribArray(renderer->shaders.box_shadow.pos_attrib);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glClearStencil(0);
glClear(GL_STENCIL_BUFFER_BIT);
glDisable(GL_STENCIL_TEST);
}

View file

@ -746,11 +746,12 @@ static void damage_child_views_iterator(struct sway_container *con,
void output_damage_whole_container(struct sway_output *output,
struct sway_container *con) {
// Pad the box by 1px, because the width is a double and might be a fraction
int shadow_sigma = con->shadow_enabled ? config->shadow_blur_sigma : 0;
struct wlr_box box = {
.x = con->current.x - output->lx - 1,
.y = con->current.y - output->ly - 1,
.width = con->current.width + 2,
.height = con->current.height + 2,
.x = con->current.x - output->lx - 1 - shadow_sigma,
.y = con->current.y - output->ly - 1 - shadow_sigma,
.width = con->current.width + 2 + shadow_sigma * 2,
.height = con->current.height + 2 + shadow_sigma * 2,
};
scale_box(&box, output->wlr_output->scale);
if (wlr_damage_ring_add_box(&output->damage_ring, &box)) {

View file

@ -328,6 +328,47 @@ damage_finish:
pixman_region32_fini(&damage);
}
// _box.x and .y are expected to be layout-local
// _box.width and .height are expected to be output-buffer-local
void render_box_shadow(struct sway_output *output, pixman_region32_t *output_damage,
const struct wlr_box *_box, const float color[static 4],
float blur_sigma, float corner_radius, float border_thickness) {
struct wlr_output *wlr_output = output->wlr_output;
struct fx_renderer *renderer = output->server->renderer;
struct wlr_box box;
memcpy(&box, _box, sizeof(struct wlr_box));
box.x -= output->lx * wlr_output->scale + blur_sigma;
box.y -= output->ly * wlr_output->scale + blur_sigma;
box.width += 2 * blur_sigma;
box.height += 2 * blur_sigma;
// Uses the outer radii of the window for a more realistic look
corner_radius = corner_radius + border_thickness;
pixman_region32_t damage;
pixman_region32_init(&damage);
pixman_region32_union_rect(&damage, &damage, box.x, box.y,
box.width, box.height);
pixman_region32_intersect(&damage, &damage, output_damage);
bool damaged = pixman_region32_not_empty(&damage);
if (!damaged) {
goto damage_finish;
}
int nrects;
pixman_box32_t *rects = pixman_region32_rectangles(&damage, &nrects);
for (int i = 0; i < nrects; ++i) {
scissor_output(wlr_output, &rects[i]);
fx_render_box_shadow(renderer, &box, color,
wlr_output->transform_matrix, corner_radius, blur_sigma);
}
damage_finish:
pixman_region32_fini(&damage);
}
void premultiply_alpha(float color[4], float opacity) {
color[3] *= opacity;
color[0] *= color[3];
@ -431,7 +472,7 @@ static void render_saved_view(struct sway_view *view, struct sway_output *output
}
/**
* Render a view's surface and left/bottom/right borders.
* Render a view's surface, shadow, and left/bottom/right borders.
*/
static void render_view(struct sway_output *output, pixman_region32_t *damage,
struct sway_container *con, struct border_colors *colors,
@ -443,7 +484,22 @@ static void render_view(struct sway_output *output, pixman_region32_t *damage,
render_view_toplevels(view, output, damage, deco_data);
}
if (con->current.border == B_NONE || con->current.border == B_CSD) {
// if CSD borders, don't render borders or shadow
if (con->current.border == B_CSD) {
return;
}
// render shadow
if (con->shadow_enabled && config->shadow_blur_sigma > 0 && config->shadow_color[3] > 0.0) {
struct sway_container_state *state = &con->current;
struct wlr_box box = { state->x, state->y, state->width, state->height };
scale_box(&box, output->wlr_output->scale);
render_box_shadow(output, damage, &box, config->shadow_color,
config->shadow_blur_sigma, con->corner_radius,
state->border_thickness);
}
if (con->current.border == B_NONE) {
return;
}

View file

@ -0,0 +1,74 @@
// Writeup: https://madebyevan.com/shaders/fast-rounded-rectangle-shadows/
precision mediump float;
varying vec4 v_color;
varying vec2 v_texcoord;
uniform vec2 position;
uniform vec2 size;
uniform float blur_sigma;
uniform float corner_radius;
float gaussian(float x, float sigma) {
const float pi = 3.141592653589793;
return exp(-(x * x) / (2.0 * sigma * sigma)) / (sqrt(2.0 * pi) * sigma);
}
// approximates the error function, needed for the gaussian integral
vec2 erf(vec2 x) {
vec2 s = sign(x), a = abs(x);
x = 1.0 + (0.278393 + (0.230389 + 0.078108 * (a * a)) * a) * a;
x *= x;
return s - s / (x * x);
}
// return the blurred mask along the x dimension
float roundedBoxShadowX(float x, float y, float sigma, float corner, vec2 halfSize) {
float delta = min(halfSize.y - corner - abs(y), 0.0);
float curved = halfSize.x - corner + sqrt(max(0.0, corner * corner - delta * delta));
vec2 integral = 0.5 + 0.5 * erf((x + vec2(-curved, curved)) * (sqrt(0.5) / sigma));
return integral.y - integral.x;
}
// return the mask for the shadow of a box from lower to upper
float roundedBoxShadow(vec2 lower, vec2 upper, vec2 point, float sigma, float corner_radius) {
// Center everything to make the math easier
vec2 center = (lower + upper) * 0.5;
vec2 halfSize = (upper - lower) * 0.5;
point -= center;
// The signal is only non-zero in a limited range, so don't waste samples
float low = point.y - halfSize.y;
float high = point.y + halfSize.y;
float start = clamp(-3.0 * sigma, low, high);
float end = clamp(3.0 * sigma, low, high);
// Accumulate samples (we can get away with surprisingly few samples)
float step = (end - start) / 4.0;
float y = start + step * 0.5;
float value = 0.0;
for (int i = 0; i < 4; i++) {
value += roundedBoxShadowX(point.x, point.y - y, sigma, corner_radius, halfSize) * gaussian(y, sigma) * step;
y += step;
}
return value;
}
// per-pixel "random" number between 0 and 1
float random() {
return fract(sin(dot(vec2(12.9898, 78.233), gl_FragCoord.xy)) * 43758.5453);
}
void main() {
float frag_alpha = v_color.a * roundedBoxShadow(
position + blur_sigma,
position + size - blur_sigma,
gl_FragCoord.xy, blur_sigma * 0.5,
corner_radius);
// dither the alpha to break up color bands
frag_alpha += (random() - 0.5) / 128.0;
gl_FragColor = vec4(v_color.rgb, frag_alpha);
}

View file

@ -7,6 +7,7 @@ shaders = [
'quad_round_tl.frag',
'quad_round_tr.frag',
'corner.frag',
'box_shadow.frag',
'tex_rgba.frag',
'tex_rgbx.frag',
'tex_external.frag',

View file

@ -7,9 +7,13 @@ uniform vec2 position;
uniform float radius;
void main() {
vec2 half_size = size / 2.0;
vec2 half_size = size * 0.5;
vec2 q = abs(gl_FragCoord.xy - position - half_size) - half_size + radius;
float distance = min(max(q.x,q.y),0.0) + length(max(q,0.0)) - radius;
float smoothedAlpha = 1.0 - smoothstep(-1.0, 1.0, distance);
float dist = min(max(q.x,q.y), 0.0) + length(max(q, 0.0)) - radius;
float smoothedAlpha = 1.0 - smoothstep(-1.0, 0.5, dist);
gl_FragColor = mix(vec4(0), v_color, smoothedAlpha);
if (gl_FragColor.a == 0.0) {
discard;
}
}

View file

@ -106,6 +106,9 @@ sway_sources = files(
'commands/seat/shortcuts_inhibitor.c',
'commands/seat/xcursor_theme.c',
'commands/set.c',
'commands/shadow_blur_radius.c',
'commands/shadow_color.c',
'commands/shadows.c',
'commands/show_marks.c',
'commands/shortcuts_inhibitor.c',
'commands/smart_borders.c',
@ -250,3 +253,4 @@ executable(
link_with: [lib_sway_common],
install: true
)

View file

@ -659,6 +659,17 @@ The default colors are:
*dim_inactive_colors.urgent* <hex color>
The color to dim inactive urgent windows with. Example color: #900000FF
*shadows* <value>
Adjusts if shadows should be enabled or not (on|off). Can also be set per
window with *for_window*.
*shadow_blur_radius* <value>
Adjusts the shadow blur radius of windows between 0 (disabled) and 100
while 20 is the default value.
*shadow_color* <hex color with alpha>
The shadow color. Default color: #0000007F
*default_border* normal|none|pixel [<n>]
Set default border style for new tiled windows.

View file

@ -42,6 +42,7 @@ struct sway_container *container_create(struct sway_view *view) {
c->view = view;
c->alpha = 1.0f;
c->saturation = 1.0f;
c->shadow_enabled = config->shadow_enabled;
c->corner_radius = config->corner_radius;
if (!view) {