Add rounded corners (#9)

This commit is contained in:
William McKinnon 2022-08-21 17:12:54 -04:00 committed by GitHub
parent c5d08f6085
commit 5c0086b944
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 454 additions and 244 deletions

View file

@ -118,6 +118,7 @@ sway_cmd cmd_client_urgent;
sway_cmd cmd_client_placeholder;
sway_cmd cmd_client_background;
sway_cmd cmd_commands;
sway_cmd cmd_corner_radius;
sway_cmd cmd_create_output;
sway_cmd cmd_default_border;
sway_cmd cmd_default_floating_border;

View file

@ -466,6 +466,9 @@ enum xwayland_mode {
* The configuration struct. The result of loading a config file.
*/
struct sway_config {
// SwayFX config options
int corner_radius;
char *swaynag_command;
struct swaynag_instance swaynag_config_errors;
list_t *symbols;

View file

@ -8,6 +8,7 @@ struct gles2_tex_shader {
GLint proj;
GLint tex;
GLint alpha;
GLint discardOpaque;
GLint pos_attrib;
GLint tex_attrib;
};
@ -43,10 +44,10 @@ void fx_renderer_scissor(struct wlr_box *box);
bool fx_render_subtexture_with_matrix(struct fx_renderer *renderer,
struct wlr_texture *wlr_texture, const struct wlr_fbox *box,
const float matrix[static 9], float alpha);
const float matrix[static 9], float alpha, int radius);
bool fx_render_texture_with_matrix(struct fx_renderer *renderer,
struct wlr_texture *wlr_texture, const float matrix[static 9], float alpha);
struct wlr_texture *wlr_texture, const float matrix[static 9], float alpha, int radius);
void fx_render_rect(struct fx_renderer *renderer, const struct wlr_box *box, const float color[static 4], const float projection[static 9]);

View file

@ -55,6 +55,7 @@ static const struct cmd_handler handlers[] = {
{ "client.placeholder", cmd_client_noop },
{ "client.unfocused", cmd_client_unfocused },
{ "client.urgent", cmd_client_urgent },
{ "corner_radius", cmd_corner_radius },
{ "default_border", cmd_default_border },
{ "default_floating_border", cmd_default_floating_border },
{ "exec", cmd_exec },

View file

@ -0,0 +1,23 @@
#include <string.h>
#include "sway/commands.h"
#include "sway/config.h"
#include "log.h"
struct cmd_results *cmd_corner_radius(int argc, char **argv) {
struct cmd_results *error = NULL;
if ((error = checkarg(argc, "corner_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->corner_radius = value;
// TODO: rerender windows (see opacity cmd)
return cmd_results_new(CMD_SUCCESS, NULL);
}

View file

@ -325,6 +325,9 @@ static void config_defaults(struct sway_config *config) {
color_to_rgba(config->border_colors.background, 0xFFFFFFFF);
// SwayFX defaults
config->corner_radius = 0;
// The keysym to keycode translation
struct xkb_rule_names rules = {0};
config->keysym_translation_state =

View file

@ -4,6 +4,8 @@
- https://github.com/vaxerski/Hyprland/blob/main/src/render/OpenGL.cpp
*/
// TODO: add push / pop_gles2_debug(renderer)?
#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <GLES2/gl2.h>
@ -64,8 +66,57 @@ const GLchar tex_fragment_src_rgba[] =
"uniform sampler2D tex;\n"
"uniform float alpha;\n"
"\n"
"uniform vec2 topLeft;\n"
"uniform vec2 bottomRight;\n"
"uniform vec2 fullSize;\n"
"uniform float radius;\n"
"\n"
"uniform int discardOpaque;\n"
"\n"
"void main() {\n"
" gl_FragColor = texture2D(tex, v_texcoord) * alpha;\n"
" vec4 pixColor = texture2D(tex, v_texcoord);\n"
"\n"
" if (discardOpaque == 1 && pixColor[3] * alpha == 1.0) {\n"
" discard;\n"
" return;\n"
" }\n"
"\n"
" vec2 pixCoord = fullSize * v_texcoord;\n"
"\n"
" if (pixCoord[0] < topLeft[0]) {\n"
" // we're close left\n"
" if (pixCoord[1] < topLeft[1]) {\n"
// top
" if (distance(topLeft, pixCoord) > radius) {\n"
" discard;\n"
" return;\n"
" }\n"
" } else if (pixCoord[1] > bottomRight[1]) {\n"
// bottom
" if (distance(vec2(topLeft[0], bottomRight[1]), pixCoord) > radius) {\n"
" discard;\n"
" return;\n"
" }\n"
" }\n"
" }\n"
" else if (pixCoord[0] > bottomRight[0]) {\n"
// we're close right
" if (pixCoord[1] < topLeft[1]) {\n"
// top
" if (distance(vec2(bottomRight[0], topLeft[1]), pixCoord) > radius) {\n"
" discard;\n"
" return;\n"
" }\n"
" } else if (pixCoord[1] > bottomRight[1]) {\n"
// bottom
" if (distance(bottomRight, pixCoord) > radius) {\n"
" discard;\n"
" return;\n"
" }\n"
" }\n"
" }\n"
"\n"
" gl_FragColor = pixColor * alpha;\n"
"}\n";
const GLchar tex_fragment_src_rgbx[] =
@ -74,7 +125,55 @@ const GLchar tex_fragment_src_rgbx[] =
"uniform sampler2D tex;\n"
"uniform float alpha;\n"
"\n"
"uniform vec2 topLeft;\n"
"uniform vec2 bottomRight;\n"
"uniform vec2 fullSize;\n"
"uniform float radius;\n"
"\n"
"uniform int discardOpaque;\n"
"\n"
"void main() {\n"
"\n"
" if (discardOpaque == 1 && alpha == 1.0) {\n"
" discard;\n"
" return;\n"
" }\n"
"\n"
" vec2 pixCoord = fullSize * v_texcoord;\n"
"\n"
" if (pixCoord[0] < topLeft[0]) {\n"
// we're close left
" if (pixCoord[1] < topLeft[1]) {\n"
// top
" if (distance(topLeft, pixCoord) > radius) {\n"
" discard;\n"
" return;\n"
" }\n"
" } else if (pixCoord[1] > bottomRight[1]) {\n"
// bottom
" if (distance(vec2(topLeft[0], bottomRight[1]), pixCoord) > radius) {\n"
" discard;\n"
" return;\n"
" }\n"
" }\n"
" }\n"
" else if (pixCoord[0] > bottomRight[0]) {\n"
// we're close right
" if (pixCoord[1] < topLeft[1]) {\n"
// top
" if (distance(vec2(bottomRight[0], topLeft[1]), pixCoord) > radius) {\n"
" discard;\n"
" return;\n"
" }\n"
" } else if (pixCoord[1] > bottomRight[1]) {\n"
// bottom
" if (distance(bottomRight, pixCoord) > radius) {\n"
" discard;\n"
" return;\n"
" }\n"
" }\n"
" }\n"
"\n"
" gl_FragColor = vec4(texture2D(tex, v_texcoord).rgb, 1.0) * alpha;\n"
"}\n";
@ -85,8 +184,58 @@ const GLchar tex_fragment_src_external[] =
"uniform samplerExternalOES texture0;\n"
"uniform float alpha;\n"
"\n"
"uniform vec2 topLeft;\n"
"uniform vec2 bottomRight;\n"
"uniform vec2 fullSize;\n"
"uniform float radius;\n"
"\n"
"uniform int discardOpaque;\n"
"\n"
"void main() {\n"
" gl_FragColor = texture2D(texture0, v_texcoord) * alpha;\n"
"\n"
" vec4 pixColor = texture2D(texture0, v_texcoord);\n"
"\n"
" if (discardOpaque == 1 && pixColor[3] * alpha == 1.0) {\n"
" discard;\n"
" return;\n"
" }\n"
"\n"
" vec2 pixCoord = fullSize * v_texcoord;\n"
"\n"
" if (pixCoord[0] < topLeft[0]) {\n"
// we're close left
" if (pixCoord[1] < topLeft[1]) {\n"
// top
" if (distance(topLeft, pixCoord) > radius) {\n"
" discard;\n"
" return;\n"
" }\n"
" } else if (pixCoord[1] > bottomRight[1]) {\n"
// bottom
" if (distance(vec2(topLeft[0], bottomRight[1]), pixCoord) > radius) {\n"
" discard;\n"
" return;\n"
" }\n"
" }\n"
" }\n"
" else if (pixCoord[0] > bottomRight[0]) {\n"
// we're close right
" if (pixCoord[1] < topLeft[1]) {\n"
// top
" if (distance(vec2(bottomRight[0], topLeft[1]), pixCoord) > radius) {\n"
" discard;\n"
" return;\n"
" }\n"
" } else if (pixCoord[1] > bottomRight[1]) {\n"
// bottom
" if (distance(bottomRight, pixCoord) > radius) {\n"
" discard;\n"
" return;\n"
" }\n"
" }\n"
" }\n"
"\n"
" gl_FragColor = pixColor * alpha;\n"
"}\n";
/************************
@ -207,6 +356,7 @@ struct fx_renderer *fx_renderer_create(struct wlr_egl *egl) {
renderer->shaders.tex_rgba.alpha = glGetUniformLocation(prog, "alpha");
renderer->shaders.tex_rgba.pos_attrib = glGetAttribLocation(prog, "pos");
renderer->shaders.tex_rgba.tex_attrib = glGetAttribLocation(prog, "texcoord");
renderer->shaders.tex_rgba.discardOpaque = glGetUniformLocation(prog, "discardOpaque");
prog = link_program(tex_vertex_src, tex_fragment_src_rgbx);
renderer->shaders.tex_rgbx.program = prog;
@ -218,6 +368,7 @@ struct fx_renderer *fx_renderer_create(struct wlr_egl *egl) {
renderer->shaders.tex_rgbx.alpha = glGetUniformLocation(prog, "alpha");
renderer->shaders.tex_rgbx.pos_attrib = glGetAttribLocation(prog, "pos");
renderer->shaders.tex_rgbx.tex_attrib = glGetAttribLocation(prog, "texcoord");
renderer->shaders.tex_rgbx.discardOpaque = glGetUniformLocation(prog, "discardOpaque");
prog = link_program(tex_vertex_src, tex_fragment_src_external);
renderer->shaders.tex_ext.program = prog;
@ -229,8 +380,8 @@ struct fx_renderer *fx_renderer_create(struct wlr_egl *egl) {
renderer->shaders.tex_ext.alpha = glGetUniformLocation(prog, "alpha");
renderer->shaders.tex_ext.pos_attrib = glGetAttribLocation(prog, "pos");
renderer->shaders.tex_ext.tex_attrib = glGetAttribLocation(prog, "texcoord");
renderer->shaders.tex_ext.discardOpaque = glGetUniformLocation(prog, "discardOpaque");
// TODO: if remove renderer->egl, replace below with r->egl
wlr_egl_unset_current(renderer->egl);
sway_log(SWAY_INFO, "GLES2 RENDERER: Shaders Initialized Successfully");
@ -262,7 +413,7 @@ void fx_renderer_begin(struct fx_renderer *renderer, uint32_t width, uint32_t he
}
void fx_renderer_end() {
// TODO
}
void fx_renderer_clear(const float color[static 4]) {
@ -285,7 +436,7 @@ void fx_renderer_scissor(struct wlr_box *box) {
bool fx_render_subtexture_with_matrix(struct fx_renderer *renderer,
struct wlr_texture *wlr_texture, const struct wlr_fbox *box,
const float matrix[static 9], float alpha) {
const float matrix[static 9], float alpha, int radius) {
assert(wlr_texture_is_gles2(wlr_texture));
struct wlr_gles2_texture_attribs texture_attrs;
@ -341,6 +492,7 @@ bool fx_render_subtexture_with_matrix(struct fx_renderer *renderer,
glUniformMatrix3fv(shader->proj, 1, GL_FALSE, gl_matrix);
glUniform1i(shader->tex, 0);
glUniform1f(shader->alpha, alpha);
glUniform1i(shader->discardOpaque, 0); // TODO
const GLfloat x1 = box->x / wlr_texture->width;
const GLfloat y1 = box->y / wlr_texture->height;
@ -353,6 +505,23 @@ bool fx_render_subtexture_with_matrix(struct fx_renderer *renderer,
x1, y2, // bottom left
};
glUniform2f(
glGetUniformLocation(shader->program, "topLeft"),
radius,
radius
);
glUniform2f(
glGetUniformLocation(shader->program, "bottomRight"),
wlr_texture->width - radius,
wlr_texture->height - radius
);
glUniform2f(
glGetUniformLocation(shader->program, "fullSize"),
wlr_texture->width,
wlr_texture->height
);
glUniform1f(glGetUniformLocation(shader->program, "radius"), radius);
glVertexAttribPointer(shader->pos_attrib, 2, GL_FLOAT, GL_FALSE, 0, verts);
glVertexAttribPointer(shader->tex_attrib, 2, GL_FLOAT, GL_FALSE, 0, texcoord);
@ -370,14 +539,14 @@ bool fx_render_subtexture_with_matrix(struct fx_renderer *renderer,
}
bool fx_render_texture_with_matrix(struct fx_renderer *renderer,
struct wlr_texture *wlr_texture, const float matrix[static 9], float alpha) {
struct wlr_texture *wlr_texture, const float matrix[static 9], float alpha, int radius) {
struct wlr_fbox box = {
.x = 0,
.y = 0,
.width = wlr_texture->width,
.height = wlr_texture->height,
};
return fx_render_subtexture_with_matrix(renderer, wlr_texture, &box, matrix, alpha);
return fx_render_subtexture_with_matrix(renderer, wlr_texture, &box, matrix, alpha, radius);
}
void fx_render_rect(struct fx_renderer *renderer, const struct wlr_box *box, const float color[static 4], const float projection[static 9]) {

View file

@ -32,6 +32,7 @@
struct render_data {
pixman_region32_t *damage;
float alpha;
int corner_radius;
struct wlr_box *clip_box;
};
@ -101,7 +102,7 @@ static void set_scale_filter(struct wlr_output *wlr_output,
static void render_texture(struct wlr_output *wlr_output,
pixman_region32_t *output_damage, struct wlr_texture *texture,
const struct wlr_fbox *src_box, const struct wlr_box *dst_box,
const float matrix[static 9], float alpha) {
const float matrix[static 9], float alpha, int corner_radius) {
struct sway_output *output = wlr_output->data;
struct fx_renderer *renderer = output->server->renderer;
@ -121,9 +122,9 @@ static void render_texture(struct wlr_output *wlr_output,
scissor_output(wlr_output, &rects[i]);
set_scale_filter(wlr_output, texture, output->scale_filter);
if (src_box != NULL) {
fx_render_subtexture_with_matrix(renderer, texture, src_box, matrix, alpha);
fx_render_subtexture_with_matrix(renderer, texture, src_box, matrix, alpha, corner_radius);
} else {
fx_render_texture_with_matrix(renderer, texture, matrix, alpha);
fx_render_texture_with_matrix(renderer, texture, matrix, alpha, corner_radius);
}
}
@ -138,6 +139,7 @@ static void render_surface_iterator(struct sway_output *output,
struct wlr_output *wlr_output = output->wlr_output;
pixman_region32_t *output_damage = data->damage;
float alpha = data->alpha;
int corner_radius = data->corner_radius;
struct wlr_texture *texture = wlr_surface_get_texture(surface);
if (!texture) {
@ -164,7 +166,7 @@ static void render_surface_iterator(struct sway_output *output,
}
scale_box(&dst_box, wlr_output->scale);
render_texture(wlr_output, output_damage, texture, &src_box, &dst_box, matrix, alpha);
render_texture(wlr_output, output_damage, texture, &src_box, &dst_box, matrix, alpha, corner_radius);
wlr_presentation_surface_sampled_on_output(server.presentation, surface,
wlr_output);
@ -175,6 +177,7 @@ static void render_layer_toplevel(struct sway_output *output,
struct render_data data = {
.damage = damage,
.alpha = 1.0f,
.corner_radius = 0,
};
output_layer_for_each_toplevel_surface(output, layer_surfaces,
render_surface_iterator, &data);
@ -185,6 +188,7 @@ static void render_layer_popups(struct sway_output *output,
struct render_data data = {
.damage = damage,
.alpha = 1.0f,
.corner_radius = 0,
};
output_layer_for_each_popup_surface(output, layer_surfaces,
render_surface_iterator, &data);
@ -196,6 +200,7 @@ static void render_unmanaged(struct sway_output *output,
struct render_data data = {
.damage = damage,
.alpha = 1.0f,
.corner_radius = 0,
};
output_unmanaged_for_each_surface(output, unmanaged,
render_surface_iterator, &data);
@ -207,6 +212,7 @@ static void render_drag_icons(struct sway_output *output,
struct render_data data = {
.damage = damage,
.alpha = 1.0f,
.corner_radius = 0,
};
output_drag_icons_for_each_surface(output, drag_icons,
render_surface_iterator, &data);
@ -255,10 +261,11 @@ void premultiply_alpha(float color[4], float opacity) {
}
static void render_view_toplevels(struct sway_view *view,
struct sway_output *output, pixman_region32_t *damage, float alpha) {
struct sway_output *output, pixman_region32_t *damage, float alpha, int corner_radius) {
struct render_data data = {
.damage = damage,
.alpha = alpha,
.corner_radius = corner_radius,
};
struct wlr_box clip_box;
if (!container_is_current_floating(view->container)) {
@ -282,13 +289,14 @@ static void render_view_popups(struct sway_view *view,
struct render_data data = {
.damage = damage,
.alpha = alpha,
.corner_radius = config->corner_radius,
};
output_view_for_each_popup_surface(output, view,
render_surface_iterator, &data);
}
static void render_saved_view(struct sway_view *view,
struct sway_output *output, pixman_region32_t *damage, float alpha) {
struct sway_output *output, pixman_region32_t *damage, float alpha, int corner_radius) {
struct wlr_output *wlr_output = output->wlr_output;
if (wl_list_empty(&view->saved_buffers)) {
@ -340,7 +348,7 @@ static void render_saved_view(struct sway_view *view,
scale_box(&dst_box, wlr_output->scale);
render_texture(wlr_output, damage, saved_buf->buffer->texture,
&saved_buf->source_box, &dst_box, matrix, alpha);
&saved_buf->source_box, &dst_box, matrix, alpha, corner_radius);
}
// FIXME: we should set the surface that this saved buffer originates from
@ -355,9 +363,9 @@ static void render_view(struct sway_output *output, pixman_region32_t *damage,
struct sway_container *con, struct border_colors *colors) {
struct sway_view *view = con->view;
if (!wl_list_empty(&view->saved_buffers)) {
render_saved_view(view, output, damage, view->container->alpha);
render_saved_view(view, output, damage, view->container->alpha, config->corner_radius);
} else if (view->surface) {
render_view_toplevels(view, output, damage, view->container->alpha);
render_view_toplevels(view, output, damage, view->container->alpha, config->corner_radius);
}
if (con->current.border == B_NONE || con->current.border == B_CSD) {
@ -521,7 +529,7 @@ static void render_titlebar(struct sway_output *output,
texture_box.width = ob_inner_width;
}
render_texture(output->wlr_output, output_damage, marks_texture,
NULL, &texture_box, matrix, con->alpha);
NULL, &texture_box, matrix, con->alpha, 0);
// Padding above
memcpy(&color, colors->background, sizeof(float) * 4);
@ -597,7 +605,7 @@ static void render_titlebar(struct sway_output *output,
}
render_texture(output->wlr_output, output_damage, title_texture,
NULL, &texture_box, matrix, con->alpha);
NULL, &texture_box, matrix, con->alpha, 0);
// Padding above
memcpy(&color, colors->background, sizeof(float) * 4);
@ -1073,10 +1081,10 @@ void output_render(struct sway_output *output, struct timespec *when,
if (fullscreen_con->view) {
if (!wl_list_empty(&fullscreen_con->view->saved_buffers)) {
render_saved_view(fullscreen_con->view, output, damage, 1.0f);
render_saved_view(fullscreen_con->view, output, damage, 1.0f, 0);
} else if (fullscreen_con->view->surface) {
render_view_toplevels(fullscreen_con->view,
output, damage, 1.0f);
output, damage, 1.0f, 0);
}
} else {
render_container(output, damage, fullscreen_con,

View file

@ -46,6 +46,7 @@ sway_sources = files(
'commands/bind.c',
'commands/border.c',
'commands/client.c',
'commands/corner_radius.c',
'commands/create_output.c',
'commands/default_border.c',
'commands/default_floating_border.c',