Skip to content

Commit

Permalink
render: Use wlr_render_pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Nefsen402 committed Apr 27, 2023
1 parent 5ac2a9e commit 84e5e79
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 82 deletions.
2 changes: 2 additions & 0 deletions include/sway/output.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ struct render_context {
struct sway_output *output;
struct wlr_renderer *renderer;
const pixman_region32_t *output_damage;

struct wlr_render_pass *pass;
};

struct sway_output *output_create(struct wlr_output *wlr_output);
Expand Down
26 changes: 24 additions & 2 deletions sway/desktop/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <wayland-server-core.h>
#include <wlr/config.h>
#include <wlr/backend/headless.h>
#include <wlr/render/swapchain.h>
#include <wlr/render/wlr_renderer.h>
#include <wlr/types/wlr_buffer.h>
#include <wlr/types/wlr_matrix.h>
Expand Down Expand Up @@ -604,9 +605,21 @@ static int output_repaint_timer_handler(void *data) {
}
}

if (!wlr_output_configure_primary_swapchain(wlr_output, NULL, &wlr_output->swapchain)) {
return false;
}

int buffer_age;
if (!wlr_output_attach_render(output->wlr_output, &buffer_age)) {
return 0;
struct wlr_buffer *buffer = wlr_swapchain_acquire(wlr_output->swapchain, &buffer_age);
if (buffer == NULL) {
return false;
}

struct wlr_render_pass *render_pass = wlr_renderer_begin_buffer_pass(
wlr_output->renderer, buffer);
if (render_pass == NULL) {
wlr_buffer_unlock(buffer);
return false;
}

pixman_region32_t damage;
Expand All @@ -623,6 +636,7 @@ static int output_repaint_timer_handler(void *data) {
.output_damage = &damage,
.renderer = wlr_output->renderer,
.output = output,
.pass = render_pass,
};

struct timespec now;
Expand All @@ -632,6 +646,14 @@ static int output_repaint_timer_handler(void *data) {

pixman_region32_fini(&damage);

if (!wlr_render_pass_submit(render_pass)) {
wlr_buffer_unlock(buffer);
return false;
}

wlr_output_attach_buffer(wlr_output, buffer);
wlr_buffer_unlock(buffer);

if (!wlr_output_commit(wlr_output)) {
return 0;
}
Expand Down
154 changes: 74 additions & 80 deletions sway/desktop/render.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ struct render_data {
struct wlr_box *clip_box;
};

static void transform_output_damage(pixman_region32_t *damage, struct wlr_output *output) {
int ow, oh;
wlr_output_transformed_resolution(output, &ow, &oh);
enum wl_output_transform transform =
wlr_output_transform_invert(output->transform);
wlr_region_transform(damage, damage, transform, ow, oh);
}

static void transform_output_box(struct wlr_box *box, struct wlr_output *output) {
int ow, oh;
wlr_output_transformed_resolution(output, &ow, &oh);
enum wl_output_transform transform =
wlr_output_transform_invert(output->transform);
wlr_box_transform(box, box, transform, ow, oh);
}


/**
* Apply scale to a width or height.
*
Expand All @@ -54,28 +71,6 @@ static int scale_length(int length, int offset, float scale) {
return roundf((offset + length) * scale) - roundf(offset * scale);
}

static void scissor_output(struct wlr_output *wlr_output,
pixman_box32_t *rect) {
struct wlr_renderer *renderer = wlr_output->renderer;
assert(renderer);

struct wlr_box box = {
.x = rect->x1,
.y = rect->y1,
.width = rect->x2 - rect->x1,
.height = rect->y2 - rect->y1,
};

int ow, oh;
wlr_output_transformed_resolution(wlr_output, &ow, &oh);

enum wl_output_transform transform =
wlr_output_transform_invert(wlr_output->transform);
wlr_box_transform(&box, &box, transform, ow, oh);

wlr_renderer_scissor(renderer, &box);
}

static void set_scale_filter(struct wlr_output *wlr_output,
struct wlr_texture *texture, enum scale_filter_mode scale_filter) {
#if WLR_HAS_GLES2_RENDERER
Expand Down Expand Up @@ -105,17 +100,11 @@ static void set_scale_filter(struct wlr_output *wlr_output,
static void render_texture(struct render_context *ctx, struct wlr_texture *texture,
const struct wlr_fbox *src_box, const struct wlr_box *dst_box,
enum wl_output_transform transform, float alpha) {
struct wlr_renderer *renderer = ctx->renderer;
struct sway_output *output = ctx->output;

struct wlr_box proj_box = *dst_box;
scale_box(&proj_box, output->wlr_output->scale);

float matrix[9];
enum wl_output_transform inv_transform = wlr_output_transform_invert(transform);
wlr_matrix_project_box(matrix, &proj_box, inv_transform, 0,
output->wlr_output->transform_matrix);

pixman_region32_t damage;
pixman_region32_init(&damage);
pixman_region32_union_rect(&damage, &damage, dst_box->x, dst_box->y,
Expand All @@ -126,17 +115,19 @@ static void render_texture(struct render_context *ctx, struct wlr_texture *textu
goto damage_finish;
}

int nrects;
pixman_box32_t *rects = pixman_region32_rectangles(&damage, &nrects);
for (int i = 0; i < nrects; ++i) {
scissor_output(output->wlr_output, &rects[i]);
set_scale_filter(output->wlr_output, texture, output->scale_filter);
if (src_box != NULL) {
wlr_render_subtexture_with_matrix(renderer, texture, src_box, matrix, alpha);
} else {
wlr_render_texture_with_matrix(renderer, texture, matrix, alpha);
}
}
transform_output_box(&proj_box, output->wlr_output);
transform_output_damage(&damage, output->wlr_output);
transform = wlr_output_transform_compose(transform, output->wlr_output->transform);

set_scale_filter(output->wlr_output, texture, output->scale_filter);
wlr_render_pass_add_texture(ctx->pass, &(struct wlr_render_texture_options) {
.texture = texture,
.src_box = *src_box,
.dst_box = proj_box,
.transform = transform,
.alpha = &alpha,
.clip = &damage,
});

damage_finish:
pixman_region32_fini(&damage);
Expand Down Expand Up @@ -218,7 +209,6 @@ static void render_drag_icons(struct render_context *ctx, struct wl_list *drag_i
void render_rect(struct render_context *ctx, const struct wlr_box *_box,
float color[static 4]) {
struct wlr_output *wlr_output = ctx->output->wlr_output;
struct wlr_renderer *renderer = ctx->renderer;

struct wlr_box box;
memcpy(&box, _box, sizeof(struct wlr_box));
Expand All @@ -235,13 +225,18 @@ void render_rect(struct render_context *ctx, const struct wlr_box *_box,
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]);
wlr_render_rect(renderer, &box, color,
wlr_output->transform_matrix);
}
transform_output_damage(&damage, ctx->output->wlr_output);

wlr_render_pass_add_rect(ctx->pass, &(struct wlr_render_rect_options){
.box = box,
.color = {
.r = color[0],
.g = color[1],
.b = color[2],
.a = color[3],
},
.clip = &damage,
});

damage_finish:
pixman_region32_fini(&damage);
Expand Down Expand Up @@ -1006,7 +1001,6 @@ static void render_seatops(struct render_context *ctx) {

void output_render(struct render_context *ctx) {
struct wlr_output *wlr_output = ctx->output->wlr_output;
struct wlr_renderer *renderer = ctx->renderer;
struct sway_output *output = ctx->output;
const pixman_region32_t *damage = ctx->output_damage;

Expand All @@ -1020,32 +1014,39 @@ void output_render(struct render_context *ctx) {
fullscreen_con = workspace->current.fullscreen;
}

if (!wlr_renderer_begin(renderer, wlr_output->width, wlr_output->height)) {
return;
}

if (!pixman_region32_not_empty(damage)) {
// Output isn't damaged but needs buffer swap
goto renderer_end;
}

if (debug.damage == DAMAGE_HIGHLIGHT) {
wlr_renderer_clear(renderer, (float[]){1, 1, 0, 1});
wlr_render_pass_add_rect(ctx->pass, &(struct wlr_render_rect_options){
.box = { .width = output->width, .height = output->height },
.color = { .r = 1, .g = 1, .b = 0, .a = 1 },
});
}

pixman_region32_t background;
pixman_region32_init(&background);
pixman_region32_copy(&background, damage);

transform_output_damage(&background, wlr_output);

if (server.session_lock.locked) {
float clear_color[] = {0.0f, 0.0f, 0.0f, 1.0f};
struct wlr_render_color clear_color = {
.a = 1.0f
};
if (server.session_lock.lock == NULL) {
// abandoned lock -> red BG
clear_color[0] = 1.f;
}
int nrects;
pixman_box32_t *rects = pixman_region32_rectangles(damage, &nrects);
for (int i = 0; i < nrects; ++i) {
scissor_output(wlr_output, &rects[i]);
wlr_renderer_clear(renderer, clear_color);
clear_color.r = 1.f;
}

wlr_render_pass_add_rect(ctx->pass, &(struct wlr_render_rect_options){
.box = { .width = output->width, .height = output->height },
.color = clear_color,
.clip = &background,
});

if (server.session_lock.lock != NULL) {
struct render_data data = {
.alpha = 1.0f,
Expand Down Expand Up @@ -1073,14 +1074,11 @@ void output_render(struct render_context *ctx) {
}

if (fullscreen_con) {
float clear_color[] = {0.0f, 0.0f, 0.0f, 1.0f};

int nrects;
pixman_box32_t *rects = pixman_region32_rectangles(damage, &nrects);
for (int i = 0; i < nrects; ++i) {
scissor_output(wlr_output, &rects[i]);
wlr_renderer_clear(renderer, clear_color);
}
wlr_render_pass_add_rect(ctx->pass, &(struct wlr_render_rect_options){
.box = { .width = output->width, .height = output->height },
.color = { .r = 0, .g = 0, .b = 0, .a = 1 },
.clip = &background,
});

if (fullscreen_con->view) {
if (!wl_list_empty(&fullscreen_con->view->saved_buffers)) {
Expand All @@ -1104,14 +1102,11 @@ void output_render(struct render_context *ctx) {
render_unmanaged(ctx, &root->xwayland_unmanaged);
#endif
} else {
float clear_color[] = {0.25f, 0.25f, 0.25f, 1.0f};

int nrects;
pixman_box32_t *rects = pixman_region32_rectangles(damage, &nrects);
for (int i = 0; i < nrects; ++i) {
scissor_output(wlr_output, &rects[i]);
wlr_renderer_clear(renderer, clear_color);
}
wlr_render_pass_add_rect(ctx->pass, &(struct wlr_render_rect_options){
.box = { .width = output->width, .height = output->height },
.color = { .r = 0.25f, .g = 0.25f, .b = 0.25f, .a = 1 },
.clip = &background,
});

render_layer_toplevel(ctx,
&output->layers[ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND]);
Expand Down Expand Up @@ -1150,7 +1145,6 @@ void output_render(struct render_context *ctx) {
render_drag_icons(ctx, &root->drag_icons);

renderer_end:
wlr_renderer_scissor(renderer, NULL);
wlr_output_render_software_cursors(wlr_output, damage);
wlr_renderer_end(renderer);
pixman_region32_fini(&background);
wlr_output_add_software_cursors_to_render_pass(wlr_output, ctx->pass, damage);
}

0 comments on commit 84e5e79

Please sign in to comment.