Refactor rendering code
This commit is contained in:
parent
516f5454ad
commit
e550e22c0b
|
@ -38,191 +38,201 @@ struct sway_container *output_by_name(const char *name) {
|
||||||
*/
|
*/
|
||||||
static void rotate_child_position(double *sx, double *sy, double sw, double sh,
|
static void rotate_child_position(double *sx, double *sy, double sw, double sh,
|
||||||
double pw, double ph, float rotation) {
|
double pw, double ph, float rotation) {
|
||||||
if (rotation != 0.0) {
|
if (rotation == 0.0f) {
|
||||||
// Coordinates relative to the center of the subsurface
|
return;
|
||||||
double ox = *sx - pw/2 + sw/2,
|
|
||||||
oy = *sy - ph/2 + sh/2;
|
|
||||||
// Rotated coordinates
|
|
||||||
double rx = cos(-rotation)*ox - sin(-rotation)*oy,
|
|
||||||
ry = cos(-rotation)*oy + sin(-rotation)*ox;
|
|
||||||
*sx = rx + pw/2 - sw/2;
|
|
||||||
*sy = ry + ph/2 - sh/2;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Coordinates relative to the center of the subsurface
|
||||||
|
double ox = *sx - pw/2 + sw/2,
|
||||||
|
oy = *sy - ph/2 + sh/2;
|
||||||
|
// Rotated coordinates
|
||||||
|
double rx = cos(-rotation)*ox - sin(-rotation)*oy,
|
||||||
|
ry = cos(-rotation)*oy + sin(-rotation)*ox;
|
||||||
|
*sx = rx + pw/2 - sw/2;
|
||||||
|
*sy = ry + ph/2 - sh/2;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks whether a surface at (lx, ly) intersects an output. If `box` is not
|
* Contains a surface's root geometry information. For instance, when rendering
|
||||||
* NULL, it populates it with the surface box in the output, in output-local
|
* a popup, this will contain the parent view's position and size.
|
||||||
* coordinates.
|
|
||||||
*/
|
*/
|
||||||
static bool surface_intersect_output(struct wlr_surface *surface,
|
struct root_geometry {
|
||||||
struct wlr_output_layout *output_layout, struct wlr_output *wlr_output,
|
double x, y;
|
||||||
double ox, double oy, float rotation, struct wlr_box *box) {
|
int width, height;
|
||||||
if (box != NULL) {
|
float rotation;
|
||||||
box->x = ox * wlr_output->scale;
|
};
|
||||||
box->y = oy * wlr_output->scale;
|
|
||||||
box->width = surface->current->width * wlr_output->scale;
|
static bool get_surface_box(struct root_geometry *geo,
|
||||||
box->height = surface->current->height * wlr_output->scale;
|
struct sway_output *output, struct wlr_surface *surface, int sx, int sy,
|
||||||
|
struct wlr_box *surface_box) {
|
||||||
|
int sw = surface->current->width;
|
||||||
|
int sh = surface->current->height;
|
||||||
|
|
||||||
|
double _sx = sx, _sy = sy;
|
||||||
|
rotate_child_position(&_sx, &_sy, sw, sh, geo->width, geo->height,
|
||||||
|
geo->rotation);
|
||||||
|
|
||||||
|
struct wlr_box box = {
|
||||||
|
.x = geo->x + _sx,
|
||||||
|
.y = geo->y + _sy,
|
||||||
|
.width = sw,
|
||||||
|
.height = sh,
|
||||||
|
};
|
||||||
|
if (surface_box != NULL) {
|
||||||
|
memcpy(surface_box, &box, sizeof(struct wlr_box));
|
||||||
}
|
}
|
||||||
|
|
||||||
struct wlr_box layout_box = {
|
struct wlr_box rotated_box;
|
||||||
.x = wlr_output->lx + ox, .y = wlr_output->ly + oy,
|
wlr_box_rotated_bounds(&box, geo->rotation, &rotated_box);
|
||||||
.width = surface->current->width, .height = surface->current->height,
|
|
||||||
|
struct wlr_box output_box = {
|
||||||
|
.width = output->swayc->width,
|
||||||
|
.height = output->swayc->height,
|
||||||
};
|
};
|
||||||
wlr_box_rotated_bounds(&layout_box, rotation, &layout_box);
|
|
||||||
return wlr_output_layout_intersects(output_layout, wlr_output, &layout_box);
|
struct wlr_box intersection;
|
||||||
|
return wlr_box_intersection(&output_box, &rotated_box, &intersection);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void render_surface(struct wlr_surface *surface,
|
static void output_surface_for_each_surface(struct wlr_surface *surface,
|
||||||
struct wlr_output *wlr_output, struct timespec *when,
|
double ox, double oy, float rotation, struct root_geometry *geo,
|
||||||
double ox, double oy, float rotation, float alpha) {
|
wlr_surface_iterator_func_t iterator, void *user_data) {
|
||||||
struct wlr_renderer *renderer =
|
geo->x = ox;
|
||||||
wlr_backend_get_renderer(wlr_output->backend);
|
geo->y = oy;
|
||||||
|
geo->width = surface->current->width;
|
||||||
|
geo->height = surface->current->height;
|
||||||
|
geo->rotation = rotation;
|
||||||
|
|
||||||
|
wlr_surface_for_each_surface(surface, iterator, user_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void output_view_for_each_surface(struct sway_view *view,
|
||||||
|
struct root_geometry *geo, wlr_surface_iterator_func_t iterator,
|
||||||
|
void *user_data) {
|
||||||
|
geo->x = view->swayc->x;
|
||||||
|
geo->y = view->swayc->y;
|
||||||
|
geo->width = view->surface->current->width;
|
||||||
|
geo->height = view->surface->current->height;
|
||||||
|
geo->rotation = 0; // TODO
|
||||||
|
|
||||||
|
view_for_each_surface(view, iterator, user_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void scale_box(struct wlr_box *box, float scale) {
|
||||||
|
box->x *= scale;
|
||||||
|
box->y *= scale;
|
||||||
|
box->width *= scale;
|
||||||
|
box->height *= scale;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct render_data {
|
||||||
|
struct root_geometry root_geo;
|
||||||
|
struct sway_output *output;
|
||||||
|
struct timespec *when;
|
||||||
|
float alpha;
|
||||||
|
};
|
||||||
|
|
||||||
|
static void render_surface_iterator(struct wlr_surface *surface, int sx, int sy,
|
||||||
|
void *_data) {
|
||||||
|
struct render_data *data = _data;
|
||||||
|
struct wlr_output *wlr_output = data->output->wlr_output;
|
||||||
|
struct timespec *when = data->when;
|
||||||
|
float rotation = data->root_geo.rotation;
|
||||||
|
float alpha = data->alpha;
|
||||||
|
|
||||||
if (!wlr_surface_has_buffer(surface)) {
|
if (!wlr_surface_has_buffer(surface)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct wlr_output_layout *layout = root_container.sway_root->output_layout;
|
|
||||||
|
|
||||||
struct wlr_box box;
|
struct wlr_box box;
|
||||||
bool intersects = surface_intersect_output(surface, layout, wlr_output,
|
bool intersects = get_surface_box(&data->root_geo, data->output, surface,
|
||||||
ox, oy, rotation, &box);
|
sx, sy, &box);
|
||||||
if (intersects) {
|
if (!intersects) {
|
||||||
float matrix[9];
|
return;
|
||||||
enum wl_output_transform transform =
|
|
||||||
wlr_output_transform_invert(surface->current->transform);
|
|
||||||
wlr_matrix_project_box(matrix, &box, transform, rotation,
|
|
||||||
wlr_output->transform_matrix);
|
|
||||||
|
|
||||||
wlr_render_texture_with_matrix(renderer, surface->texture,
|
|
||||||
matrix, alpha);
|
|
||||||
|
|
||||||
wlr_surface_send_frame_done(surface, when);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct wlr_subsurface *subsurface;
|
struct wlr_renderer *renderer =
|
||||||
wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
|
wlr_backend_get_renderer(wlr_output->backend);
|
||||||
struct wlr_surface_state *state = subsurface->surface->current;
|
if (!sway_assert(renderer != NULL,
|
||||||
double sx = state->subsurface_position.x;
|
"expected the output backend to have a renderer")) {
|
||||||
double sy = state->subsurface_position.y;
|
return;
|
||||||
rotate_child_position(&sx, &sy, state->width, state->height,
|
}
|
||||||
surface->current->width, surface->current->height, rotation);
|
|
||||||
|
|
||||||
render_surface(subsurface->surface, wlr_output, when,
|
scale_box(&box, wlr_output->scale);
|
||||||
ox + sx, oy + sy, rotation, alpha);
|
|
||||||
|
float matrix[9];
|
||||||
|
enum wl_output_transform transform =
|
||||||
|
wlr_output_transform_invert(surface->current->transform);
|
||||||
|
wlr_matrix_project_box(matrix, &box, transform, rotation,
|
||||||
|
wlr_output->transform_matrix);
|
||||||
|
|
||||||
|
wlr_render_texture_with_matrix(renderer, surface->texture,
|
||||||
|
matrix, alpha);
|
||||||
|
|
||||||
|
// TODO: don't send the frame done event now
|
||||||
|
wlr_surface_send_frame_done(surface, when);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void render_surface(struct sway_output *output, struct timespec *when,
|
||||||
|
struct wlr_surface *surface, double ox, double oy, float rotation) {
|
||||||
|
struct render_data data = {
|
||||||
|
.output = output,
|
||||||
|
.when = when,
|
||||||
|
.alpha = 1.0f,
|
||||||
|
};
|
||||||
|
|
||||||
|
output_surface_for_each_surface(surface, ox, oy, rotation, &data.root_geo,
|
||||||
|
render_surface_iterator, &data);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void render_view(struct sway_output *output, struct timespec *when,
|
||||||
|
struct sway_view *view) {
|
||||||
|
struct render_data data = {
|
||||||
|
.output = output,
|
||||||
|
.when = when,
|
||||||
|
.alpha = view->swayc->alpha,
|
||||||
|
};
|
||||||
|
|
||||||
|
output_view_for_each_surface(view, &data.root_geo,
|
||||||
|
render_surface_iterator, &data);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void render_layer(struct sway_output *output, struct timespec *when,
|
||||||
|
struct wl_list *layer_surfaces) {
|
||||||
|
struct sway_layer_surface *layer_surface;
|
||||||
|
wl_list_for_each(layer_surface, layer_surfaces, link) {
|
||||||
|
struct wlr_layer_surface *wlr_layer_surface =
|
||||||
|
layer_surface->layer_surface;
|
||||||
|
render_surface(output, when, wlr_layer_surface->surface,
|
||||||
|
layer_surface->geo.x, layer_surface->geo.y, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void render_xdg_v6_popups(struct wlr_xdg_surface_v6 *surface,
|
struct render_view_data {
|
||||||
struct wlr_output *wlr_output, struct timespec *when, double base_x,
|
|
||||||
double base_y, float rotation, float alpha) {
|
|
||||||
double width = surface->surface->current->width;
|
|
||||||
double height = surface->surface->current->height;
|
|
||||||
|
|
||||||
struct wlr_xdg_popup_v6 *popup_state;
|
|
||||||
wl_list_for_each(popup_state, &surface->popups, link) {
|
|
||||||
struct wlr_xdg_surface_v6 *popup = popup_state->base;
|
|
||||||
if (!popup->configured) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
double popup_width = popup->surface->current->width;
|
|
||||||
double popup_height = popup->surface->current->height;
|
|
||||||
|
|
||||||
double popup_sx, popup_sy;
|
|
||||||
wlr_xdg_surface_v6_popup_get_position(popup, &popup_sx, &popup_sy);
|
|
||||||
rotate_child_position(&popup_sx, &popup_sy, popup_width, popup_height,
|
|
||||||
width, height, rotation);
|
|
||||||
|
|
||||||
render_surface(popup->surface, wlr_output, when,
|
|
||||||
base_x + popup_sx, base_y + popup_sy, rotation, alpha);
|
|
||||||
render_xdg_v6_popups(popup, wlr_output, when,
|
|
||||||
base_x + popup_sx, base_y + popup_sy, rotation, alpha);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void render_wl_shell_surface(struct wlr_wl_shell_surface *surface,
|
|
||||||
struct wlr_output *wlr_output, struct timespec *when,
|
|
||||||
double lx, double ly, float rotation, float alpha,
|
|
||||||
bool is_child) {
|
|
||||||
if (is_child || surface->state != WLR_WL_SHELL_SURFACE_STATE_POPUP) {
|
|
||||||
render_surface(surface->surface, wlr_output, when,
|
|
||||||
lx, ly, rotation, alpha);
|
|
||||||
|
|
||||||
double width = surface->surface->current->width;
|
|
||||||
double height = surface->surface->current->height;
|
|
||||||
|
|
||||||
struct wlr_wl_shell_surface *popup;
|
|
||||||
wl_list_for_each(popup, &surface->popups, popup_link) {
|
|
||||||
double popup_width = popup->surface->current->width;
|
|
||||||
double popup_height = popup->surface->current->height;
|
|
||||||
|
|
||||||
double popup_x = popup->transient_state->x;
|
|
||||||
double popup_y = popup->transient_state->y;
|
|
||||||
rotate_child_position(&popup_x, &popup_y, popup_width, popup_height,
|
|
||||||
width, height, rotation);
|
|
||||||
|
|
||||||
render_wl_shell_surface(popup, wlr_output, when,
|
|
||||||
lx + popup_x, ly + popup_y, rotation, alpha, true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct render_data {
|
|
||||||
struct sway_output *output;
|
struct sway_output *output;
|
||||||
struct timespec *when;
|
struct timespec *when;
|
||||||
};
|
};
|
||||||
|
|
||||||
static void render_view(struct sway_container *view, void *data) {
|
static void render_view_iterator(struct sway_container *con, void *_data) {
|
||||||
struct render_data *rdata = data;
|
struct render_view_data *data = _data;
|
||||||
struct sway_output *output = rdata->output;
|
|
||||||
struct timespec *when = rdata->when;
|
|
||||||
struct wlr_output *wlr_output = output->wlr_output;
|
|
||||||
struct sway_view *sway_view = view->sway_view;
|
|
||||||
struct wlr_surface *surface = sway_view->surface;
|
|
||||||
float alpha = sway_view->swayc->alpha;
|
|
||||||
|
|
||||||
if (!surface) {
|
if (!sway_assert(con->type == C_VIEW, "expected a view")) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (sway_view->type) {
|
render_view(data->output, data->when, con->sway_view);
|
||||||
case SWAY_VIEW_XDG_SHELL_V6: {
|
|
||||||
int window_offset_x = view->sway_view->wlr_xdg_surface_v6->geometry.x;
|
|
||||||
int window_offset_y = view->sway_view->wlr_xdg_surface_v6->geometry.y;
|
|
||||||
render_surface(surface, wlr_output, when,
|
|
||||||
view->x - window_offset_x, view->y - window_offset_y, 0, alpha);
|
|
||||||
render_xdg_v6_popups(sway_view->wlr_xdg_surface_v6, wlr_output,
|
|
||||||
when, view->x - window_offset_x, view->y - window_offset_y, 0, alpha);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case SWAY_VIEW_WL_SHELL:
|
|
||||||
render_wl_shell_surface(sway_view->wlr_wl_shell_surface, wlr_output,
|
|
||||||
when, view->x, view->y, 0, alpha, false);
|
|
||||||
break;
|
|
||||||
case SWAY_VIEW_XWAYLAND:
|
|
||||||
render_surface(surface, wlr_output, when, view->x, view->y, 0, alpha);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void render_layer(struct sway_output *output, struct timespec *when,
|
|
||||||
struct wl_list *layer) {
|
|
||||||
struct sway_layer_surface *sway_layer;
|
|
||||||
wl_list_for_each(sway_layer, layer, link) {
|
|
||||||
struct wlr_layer_surface *layer = sway_layer->layer_surface;
|
|
||||||
render_surface(layer->surface, output->wlr_output, when,
|
|
||||||
sway_layer->geo.x, sway_layer->geo.y, 0, 1.0f);
|
|
||||||
wlr_surface_send_frame_done(layer->surface, when);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void render_output(struct sway_output *output, struct timespec *when,
|
static void render_output(struct sway_output *output, struct timespec *when,
|
||||||
pixman_region32_t *damage) {
|
pixman_region32_t *damage) {
|
||||||
struct wlr_output *wlr_output = output->wlr_output;
|
struct wlr_output *wlr_output = output->wlr_output;
|
||||||
|
|
||||||
struct wlr_renderer *renderer =
|
struct wlr_renderer *renderer =
|
||||||
wlr_backend_get_renderer(wlr_output->backend);
|
wlr_backend_get_renderer(wlr_output->backend);
|
||||||
|
if (!sway_assert(renderer != NULL,
|
||||||
|
"expected the output backend to have a renderer")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
wlr_renderer_begin(renderer, wlr_output->width, wlr_output->height);
|
wlr_renderer_begin(renderer, wlr_output->width, wlr_output->height);
|
||||||
|
|
||||||
|
@ -231,24 +241,15 @@ static void render_output(struct sway_output *output, struct timespec *when,
|
||||||
goto renderer_end;
|
goto renderer_end;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: don't damage the whole output here
|
|
||||||
int width, height;
|
|
||||||
wlr_output_transformed_resolution(wlr_output, &width, &height);
|
|
||||||
pixman_region32_union_rect(damage, damage, 0, 0, width, height);
|
|
||||||
|
|
||||||
float clear_color[] = {0.25f, 0.25f, 0.25f, 1.0f};
|
float clear_color[] = {0.25f, 0.25f, 0.25f, 1.0f};
|
||||||
wlr_renderer_clear(renderer, clear_color);
|
wlr_renderer_clear(renderer, clear_color);
|
||||||
|
|
||||||
struct wlr_output_layout *output_layout =
|
|
||||||
root_container.sway_root->output_layout;
|
|
||||||
const struct wlr_box *output_box =
|
|
||||||
wlr_output_layout_get_box(output_layout, wlr_output);
|
|
||||||
|
|
||||||
render_layer(output, when,
|
render_layer(output, when,
|
||||||
&output->layers[ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND]);
|
&output->layers[ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND]);
|
||||||
render_layer(output, when,
|
render_layer(output, when,
|
||||||
&output->layers[ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM]);
|
&output->layers[ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM]);
|
||||||
|
|
||||||
|
// Render all views in the current workspace
|
||||||
struct sway_seat *seat = input_manager_current_seat(input_manager);
|
struct sway_seat *seat = input_manager_current_seat(input_manager);
|
||||||
struct sway_container *focus =
|
struct sway_container *focus =
|
||||||
seat_get_focus_inactive(seat, output->swayc);
|
seat_get_focus_inactive(seat, output->swayc);
|
||||||
|
@ -258,36 +259,21 @@ static void render_output(struct sway_output *output, struct timespec *when,
|
||||||
}
|
}
|
||||||
struct sway_container *workspace = focus->type == C_WORKSPACE ?
|
struct sway_container *workspace = focus->type == C_WORKSPACE ?
|
||||||
focus : container_parent(focus, C_WORKSPACE);
|
focus : container_parent(focus, C_WORKSPACE);
|
||||||
|
struct render_view_data data = { .output = output, .when = when };
|
||||||
|
container_descendants(workspace, C_VIEW, render_view_iterator, &data);
|
||||||
|
|
||||||
struct render_data rdata = {
|
// Render unmanaged views on top
|
||||||
.output = output,
|
|
||||||
.when = when,
|
|
||||||
};
|
|
||||||
container_descendants(workspace, C_VIEW, render_view, &rdata);
|
|
||||||
|
|
||||||
// render unmanaged views on top
|
|
||||||
struct wl_list *unmanaged = &root_container.sway_root->xwayland_unmanaged;
|
struct wl_list *unmanaged = &root_container.sway_root->xwayland_unmanaged;
|
||||||
struct sway_xwayland_unmanaged *unmanaged_surface;
|
struct sway_xwayland_unmanaged *unmanaged_surface;
|
||||||
wl_list_for_each(unmanaged_surface, unmanaged, link) {
|
wl_list_for_each(unmanaged_surface, unmanaged, link) {
|
||||||
struct wlr_xwayland_surface *xsurface =
|
struct wlr_xwayland_surface *xsurface =
|
||||||
unmanaged_surface->wlr_xwayland_surface;
|
unmanaged_surface->wlr_xwayland_surface;
|
||||||
|
double ox = unmanaged_surface->lx - output->swayc->x;
|
||||||
const struct wlr_box view_box = {
|
double oy = unmanaged_surface->ly - output->swayc->y;
|
||||||
.x = unmanaged_surface->lx,
|
render_surface(output, when, xsurface->surface, ox, oy, 0);
|
||||||
.y = unmanaged_surface->ly,
|
|
||||||
.width = xsurface->width,
|
|
||||||
.height = xsurface->height,
|
|
||||||
};
|
|
||||||
struct wlr_box intersection;
|
|
||||||
if (!wlr_box_intersection(&view_box, output_box, &intersection)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
render_surface(xsurface->surface, wlr_output, &output->last_frame,
|
|
||||||
view_box.x - output_box->x, view_box.y - output_box->y, 0, 1.0f);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Consider revising this when fullscreen windows are supported
|
// TODO: consider revising this when fullscreen windows are supported
|
||||||
render_layer(output, when,
|
render_layer(output, when,
|
||||||
&output->layers[ZWLR_LAYER_SHELL_V1_LAYER_TOP]);
|
&output->layers[ZWLR_LAYER_SHELL_V1_LAYER_TOP]);
|
||||||
render_layer(output, when,
|
render_layer(output, when,
|
||||||
|
|
Loading…
Reference in a new issue