scene: add wlr_scene_buffer

This new scene-graph node displays a wlr_buffer.

Closes: https://github.com/swaywm/wlroots/issues/3161
This commit is contained in:
Simon Ser 2021-09-20 16:05:19 +02:00 committed by Simon Zeni
parent f6f0e010d1
commit 7939bf8cc6
2 changed files with 92 additions and 2 deletions

View File

@ -31,6 +31,7 @@ enum wlr_scene_node_type {
WLR_SCENE_NODE_TREE, WLR_SCENE_NODE_TREE,
WLR_SCENE_NODE_SURFACE, WLR_SCENE_NODE_SURFACE,
WLR_SCENE_NODE_RECT, WLR_SCENE_NODE_RECT,
WLR_SCENE_NODE_BUFFER,
}; };
struct wlr_scene_node_state { struct wlr_scene_node_state {
@ -85,6 +86,16 @@ struct wlr_scene_rect {
float color[4]; float color[4];
}; };
/** A scene-graph node displaying a buffer */
struct wlr_scene_buffer {
struct wlr_scene_node node;
struct wlr_buffer *buffer;
// private state
struct wlr_texture *texture;
};
/** A viewport for an output in the scene-graph */ /** A viewport for an output in the scene-graph */
struct wlr_scene_output { struct wlr_scene_output {
struct wlr_output *output; struct wlr_output *output;
@ -194,6 +205,12 @@ void wlr_scene_rect_set_size(struct wlr_scene_rect *rect, int width, int height)
*/ */
void wlr_scene_rect_set_color(struct wlr_scene_rect *rect, const float color[static 4]); void wlr_scene_rect_set_color(struct wlr_scene_rect *rect, const float color[static 4]);
/**
* Add a node displaying a buffer to the scene-graph.
*/
struct wlr_scene_buffer *wlr_scene_buffer_create(struct wlr_scene_node *parent,
struct wlr_buffer *buffer);
/** /**
* Add a viewport for the specified output to the scene-graph. * Add a viewport for the specified output to the scene-graph.
* *

View File

@ -32,6 +32,12 @@ static struct wlr_scene_rect *scene_rect_from_node(
return (struct wlr_scene_rect *)node; return (struct wlr_scene_rect *)node;
} }
static struct wlr_scene_buffer *scene_buffer_from_node(
struct wlr_scene_node *node) {
assert(node->type == WLR_SCENE_NODE_BUFFER);
return (struct wlr_scene_buffer *)node;
}
static void scene_node_state_init(struct wlr_scene_node_state *state) { static void scene_node_state_init(struct wlr_scene_node_state *state) {
wl_list_init(&state->children); wl_list_init(&state->children);
wl_list_init(&state->link); wl_list_init(&state->link);
@ -102,6 +108,12 @@ void wlr_scene_node_destroy(struct wlr_scene_node *node) {
struct wlr_scene_rect *scene_rect = scene_rect_from_node(node); struct wlr_scene_rect *scene_rect = scene_rect_from_node(node);
free(scene_rect); free(scene_rect);
break; break;
case WLR_SCENE_NODE_BUFFER:;
struct wlr_scene_buffer *scene_buffer = scene_buffer_from_node(node);
wlr_texture_destroy(scene_buffer->texture);
wlr_buffer_unlock(scene_buffer->buffer);
free(scene_buffer);
break;
} }
} }
@ -239,6 +251,38 @@ void wlr_scene_rect_set_color(struct wlr_scene_rect *rect, const float color[sta
scene_node_damage_whole(&rect->node); scene_node_damage_whole(&rect->node);
} }
struct wlr_scene_buffer *wlr_scene_buffer_create(struct wlr_scene_node *parent,
struct wlr_buffer *buffer) {
struct wlr_scene_buffer *scene_buffer = calloc(1, sizeof(*scene_buffer));
if (scene_buffer == NULL) {
return NULL;
}
scene_node_init(&scene_buffer->node, WLR_SCENE_NODE_BUFFER, parent);
scene_buffer->buffer = wlr_buffer_lock(buffer);
scene_node_damage_whole(&scene_buffer->node);
return scene_buffer;
}
static struct wlr_texture *scene_buffer_get_texture(
struct wlr_scene_buffer *scene_buffer, struct wlr_renderer *renderer) {
struct wlr_client_buffer *client_buffer =
wlr_client_buffer_get(scene_buffer->buffer);
if (client_buffer != NULL) {
return client_buffer->texture;
}
if (scene_buffer->texture != NULL) {
return scene_buffer->texture;
}
scene_buffer->texture =
wlr_texture_from_buffer(renderer, scene_buffer->buffer);
return scene_buffer->texture;
}
static int scale_length(int length, int offset, float scale) { static int scale_length(int length, int offset, float scale) {
return round((offset + length) * scale) - round(offset * scale); return round((offset + length) * scale) - round(offset * scale);
} }
@ -278,6 +322,11 @@ static void _scene_node_damage_whole(struct wlr_scene_node *node,
width = scene_rect->width; width = scene_rect->width;
height = scene_rect->height; height = scene_rect->height;
break; break;
case WLR_SCENE_NODE_BUFFER:;
struct wlr_scene_buffer *scene_buffer = scene_buffer_from_node(node);
width = scene_buffer->buffer->width;
height = scene_buffer->buffer->height;
break;
} }
struct wlr_scene_output *scene_output; struct wlr_scene_output *scene_output;
@ -458,6 +507,11 @@ struct wlr_scene_node *wlr_scene_node_at(struct wlr_scene_node *node,
struct wlr_scene_rect *rect = scene_rect_from_node(node); struct wlr_scene_rect *rect = scene_rect_from_node(node);
intersects = lx >= 0 && lx < rect->width && ly >= 0 && ly < rect->height; intersects = lx >= 0 && lx < rect->width && ly >= 0 && ly < rect->height;
break; break;
case WLR_SCENE_NODE_BUFFER:;
struct wlr_scene_buffer *scene_buffer = scene_buffer_from_node(node);
intersects = lx >= 0 && lx < scene_buffer->buffer->width &&
ly >= 0 && ly < scene_buffer->buffer->height;
break;
} }
if (intersects) { if (intersects) {
@ -551,6 +605,8 @@ static void render_node_iterator(struct wlr_scene_node *node,
.y = y, .y = y,
}; };
struct wlr_texture *texture;
float matrix[9];
switch (node->type) { switch (node->type) {
case WLR_SCENE_NODE_ROOT: case WLR_SCENE_NODE_ROOT:
case WLR_SCENE_NODE_TREE: case WLR_SCENE_NODE_TREE:
@ -560,7 +616,7 @@ static void render_node_iterator(struct wlr_scene_node *node,
struct wlr_scene_surface *scene_surface = wlr_scene_surface_from_node(node); struct wlr_scene_surface *scene_surface = wlr_scene_surface_from_node(node);
struct wlr_surface *surface = scene_surface->surface; struct wlr_surface *surface = scene_surface->surface;
struct wlr_texture *texture = wlr_surface_get_texture(surface); texture = wlr_surface_get_texture(surface);
if (texture == NULL) { if (texture == NULL) {
return; return;
} }
@ -569,7 +625,6 @@ static void render_node_iterator(struct wlr_scene_node *node,
box.height = surface->current.height; box.height = surface->current.height;
scale_box(&box, output->scale); scale_box(&box, output->scale);
float matrix[9];
enum wl_output_transform transform = enum wl_output_transform transform =
wlr_output_transform_invert(surface->current.transform); wlr_output_transform_invert(surface->current.transform);
wlr_matrix_project_box(matrix, &box, transform, 0.0, wlr_matrix_project_box(matrix, &box, transform, 0.0,
@ -587,6 +642,24 @@ static void render_node_iterator(struct wlr_scene_node *node,
render_rect(output, output_damage, scene_rect->color, &box, render_rect(output, output_damage, scene_rect->color, &box,
output->transform_matrix); output->transform_matrix);
break; break;
case WLR_SCENE_NODE_BUFFER:;
struct wlr_scene_buffer *scene_buffer = scene_buffer_from_node(node);
struct wlr_renderer *renderer = wlr_backend_get_renderer(output->backend);
texture = scene_buffer_get_texture(scene_buffer, renderer);
if (texture == NULL) {
return;
}
box.width = scene_buffer->buffer->width;
box.height = scene_buffer->buffer->height;
scale_box(&box, data->output->scale);
wlr_matrix_project_box(matrix, &box, WL_OUTPUT_TRANSFORM_NORMAL, 0.0,
output->transform_matrix);
render_texture(output, output_damage, texture, &box, matrix);
break;
} }
} }