wlr_renderer: Switch texture rendering functions to take wlr_raster

Implements automatic texture uploading as both wlr_scene and cursor
handling both are simplified as a result.
This commit is contained in:
Alexander Orzechowski 2022-06-26 22:29:22 -04:00
parent 45ad3d47ad
commit 248a33a026
20 changed files with 142 additions and 166 deletions

View file

@ -93,8 +93,7 @@ static void output_cursor_render(struct wlr_output_cursor *cursor,
struct wlr_renderer *renderer = cursor->output->renderer;
assert(renderer);
struct wlr_texture *texture = cursor->texture;
if (texture == NULL) {
if (!cursor->raster) {
return;
}
@ -118,7 +117,7 @@ static void output_cursor_render(struct wlr_output_cursor *cursor,
pixman_box32_t *rects = pixman_region32_rectangles(&surface_damage, &nrects);
for (int i = 0; i < nrects; ++i) {
output_scissor(cursor->output, &rects[i]);
wlr_render_texture_with_matrix(renderer, texture, matrix, 1.0f);
wlr_render_raster_with_matrix(renderer, cursor->raster, matrix, 1.0f);
}
wlr_renderer_scissor(renderer, NULL);
@ -231,28 +230,28 @@ static struct wlr_buffer *render_cursor_buffer(struct wlr_output_cursor *cursor)
float scale = output->scale;
enum wl_output_transform transform = WL_OUTPUT_TRANSFORM_NORMAL;
struct wlr_texture *texture = cursor->texture;
if (cursor->surface != NULL) {
scale = cursor->surface->current.scale;
transform = cursor->surface->current.transform;
}
if (texture == NULL) {
return NULL;
}
struct wlr_allocator *allocator = output->allocator;
struct wlr_renderer *renderer = output->renderer;
assert(allocator != NULL && renderer != NULL);
int width = texture->width;
int height = texture->height;
if (!cursor->raster) {
return NULL;
}
int width = cursor->raster->width;
int height = cursor->raster->height;
if (output->impl->get_cursor_size) {
// Apply hardware limitations on buffer size
output->impl->get_cursor_size(cursor->output, &width, &height);
if ((int)texture->width > width || (int)texture->height > height) {
wlr_log(WLR_DEBUG, "Cursor texture too large (%dx%d), "
"exceeds hardware limitations (%dx%d)", texture->width,
texture->height, width, height);
if ((int)cursor->raster->width > width || (int)cursor->raster->height > height) {
wlr_log(WLR_DEBUG, "Cursor raster too large (%dx%d), "
"exceeds hardware limitations (%dx%d)", cursor->raster->width,
cursor->raster->height, width, height);
return NULL;
}
}
@ -284,8 +283,8 @@ static struct wlr_buffer *render_cursor_buffer(struct wlr_output_cursor *cursor)
}
struct wlr_box cursor_box = {
.width = texture->width * output->scale / scale,
.height = texture->height * output->scale / scale,
.width = cursor->raster->width * output->scale / scale,
.height = cursor->raster->height * output->scale / scale,
};
float output_matrix[9];
@ -313,7 +312,7 @@ static struct wlr_buffer *render_cursor_buffer(struct wlr_output_cursor *cursor)
}
wlr_renderer_clear(renderer, (float[]){ 0.0, 0.0, 0.0, 0.0 });
wlr_render_texture_with_matrix(renderer, texture, matrix, 1.0);
wlr_render_raster_with_matrix(renderer, cursor->raster, matrix, 1.0);
wlr_renderer_end(renderer);
@ -333,21 +332,16 @@ static bool output_cursor_attempt_hardware(struct wlr_output_cursor *cursor) {
return false;
}
// TODO: try using the surface buffer directly
struct wlr_texture *texture = cursor->texture;
// If the cursor was hidden or was a software cursor, the hardware
// cursor position is outdated
output->impl->move_cursor(cursor->output,
(int)cursor->x, (int)cursor->y);
struct wlr_buffer *buffer = NULL;
if (texture != NULL) {
buffer = render_cursor_buffer(cursor);
if (buffer == NULL) {
wlr_log(WLR_ERROR, "Failed to render cursor buffer");
return false;
}
buffer = render_cursor_buffer(cursor);
if (buffer == NULL) {
wlr_log(WLR_ERROR, "Failed to render cursor buffer");
return false;
}
struct wlr_box hotspot = {
@ -407,15 +401,16 @@ bool wlr_output_cursor_set_buffer(struct wlr_output_cursor *cursor,
output_cursor_update_visible(cursor);
wlr_texture_destroy(cursor->texture);
cursor->texture = NULL;
wlr_raster_unlock(cursor->raster);
cursor->raster = NULL;
cursor->enabled = false;
if (buffer != NULL) {
cursor->texture = wlr_texture_from_buffer(renderer, buffer);
if (cursor->texture == NULL) {
cursor->raster = wlr_raster_create(buffer);
if (!cursor->raster) {
return false;
}
cursor->enabled = true;
}
@ -435,18 +430,14 @@ static void output_cursor_commit(struct wlr_output_cursor *cursor,
output_cursor_damage_whole(cursor);
}
wlr_raster_unlock(cursor->raster);
cursor->raster = NULL;
struct wlr_surface *surface = cursor->surface;
assert(surface != NULL);
wlr_texture_destroy(cursor->texture);
cursor->texture = NULL;
if (surface->current.buffer) {
cursor->texture = wlr_texture_from_buffer(cursor->output->renderer,
surface->current.buffer);
if (cursor->texture == NULL) {
return;
}
cursor->raster = wlr_raster_create(surface->current.buffer);
}
// Some clients commit a cursor surface with a NULL buffer to hide it.
@ -593,7 +584,7 @@ void wlr_output_cursor_destroy(struct wlr_output_cursor *cursor) {
output_set_hardware_cursor(cursor->output, NULL, 0, 0);
cursor->output->hardware_cursor = NULL;
}
wlr_texture_destroy(cursor->texture);
wlr_raster_unlock(cursor->raster);
wl_list_remove(&cursor->link);
free(cursor);
}

View file

@ -101,7 +101,7 @@ void wlr_scene_node_destroy(struct wlr_scene_node *node) {
}
}
wlr_texture_destroy(scene_buffer->texture);
wlr_raster_unlock(scene_buffer->raster);
wlr_buffer_unlock(scene_buffer->buffer);
} else if (node->type == WLR_SCENE_NODE_TREE) {
struct wlr_scene_tree *scene_tree = scene_tree_from_node(node);
@ -345,8 +345,8 @@ void wlr_scene_buffer_set_buffer_with_damage(struct wlr_scene_buffer *scene_buff
scene_node_damage_whole(&scene_buffer->node);
}
wlr_texture_destroy(scene_buffer->texture);
scene_buffer->texture = NULL;
wlr_raster_unlock(scene_buffer->raster);
scene_buffer->raster = NULL;
wlr_buffer_unlock(scene_buffer->buffer);
if (buffer) {
@ -475,17 +475,6 @@ void wlr_scene_buffer_send_frame_done(struct wlr_scene_buffer *scene_buffer,
wlr_signal_emit_safe(&scene_buffer->events.frame_done, now);
}
static struct wlr_texture *scene_buffer_get_texture(
struct wlr_scene_buffer *scene_buffer, struct wlr_renderer *renderer) {
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 void scene_node_get_size(struct wlr_scene_node *node,
int *width, int *height) {
*width = 0;
@ -821,8 +810,8 @@ static void render_rect(struct wlr_output *output,
pixman_region32_fini(&damage);
}
static void render_texture(struct wlr_output *output,
pixman_region32_t *output_damage, struct wlr_texture *texture,
static void render_raster(struct wlr_output *output,
pixman_region32_t *output_damage, struct wlr_raster *raster,
const struct wlr_fbox *src_box, const struct wlr_box *dst_box,
const float matrix[static 9]) {
struct wlr_renderer *renderer = output->renderer;
@ -830,8 +819,8 @@ static void render_texture(struct wlr_output *output,
struct wlr_fbox default_src_box = {0};
if (wlr_fbox_empty(src_box)) {
default_src_box.width = texture->width;
default_src_box.height = texture->height;
default_src_box.width = raster->width;
default_src_box.height = raster->height;
src_box = &default_src_box;
}
@ -845,7 +834,7 @@ static void render_texture(struct wlr_output *output,
pixman_box32_t *rects = pixman_region32_rectangles(&damage, &nrects);
for (int i = 0; i < nrects; ++i) {
scissor_output(output, &rects[i]);
wlr_render_subtexture_with_matrix(renderer, texture, src_box, matrix, 1.0);
wlr_render_subraster_with_matrix(renderer, raster, src_box, matrix, 1.0);
}
pixman_region32_fini(&damage);
@ -870,7 +859,6 @@ static void render_node_iterator(struct wlr_scene_node *node,
scene_node_get_size(node, &dst_box.width, &dst_box.height);
scale_box(&dst_box, output->scale);
struct wlr_texture *texture;
float matrix[9];
enum wl_output_transform transform;
switch (node->type) {
@ -889,18 +877,16 @@ static void render_node_iterator(struct wlr_scene_node *node,
return;
}
struct wlr_renderer *renderer = output->renderer;
texture = scene_buffer_get_texture(scene_buffer, renderer);
if (texture == NULL) {
return;
if (!scene_buffer->raster) {
scene_buffer->raster = wlr_raster_create(scene_buffer->buffer);
}
transform = wlr_output_transform_invert(scene_buffer->transform);
wlr_matrix_project_box(matrix, &dst_box, transform, 0.0,
output->transform_matrix);
render_texture(output, output_damage, texture, &scene_buffer->src_box,
&dst_box, matrix);
render_raster(output, output_damage, scene_buffer->raster,
&scene_buffer->src_box, &dst_box, matrix);
wlr_signal_emit_safe(&scene_buffer->events.output_present, scene_output);
break;

View file

@ -226,14 +226,18 @@ static bool frame_shm_copy(struct wlr_screencopy_frame_v1 *frame,
static bool blit_dmabuf(struct wlr_renderer *renderer,
struct wlr_dmabuf_v1_buffer *dst_dmabuf,
struct wlr_buffer *src_buffer) {
struct wlr_buffer *dst_buffer = wlr_buffer_lock(&dst_dmabuf->base);
struct wlr_texture *src_tex =
wlr_texture_from_buffer(renderer, src_buffer);
if (src_tex == NULL) {
goto error_src_tex;
struct wlr_raster *raster = wlr_raster_create(src_buffer);
if (!raster){
return false;
}
if (!wlr_renderer_raster_upload(renderer, raster)) {
wlr_raster_unlock(raster);
return false;
}
struct wlr_buffer *dst_buffer = wlr_buffer_lock(&dst_dmabuf->base);
float mat[9];
wlr_matrix_identity(mat);
wlr_matrix_scale(mat, dst_buffer->width, dst_buffer->height);
@ -243,17 +247,16 @@ static bool blit_dmabuf(struct wlr_renderer *renderer,
}
wlr_renderer_clear(renderer, (float[]){ 0.0, 0.0, 0.0, 0.0 });
wlr_render_texture_with_matrix(renderer, src_tex, mat, 1.0f);
wlr_render_raster_with_matrix(renderer, raster, mat, 1.0f);
wlr_renderer_end(renderer);
wlr_texture_destroy(src_tex);
wlr_raster_unlock(raster);
wlr_buffer_unlock(dst_buffer);
return true;
error_renderer_begin:
wlr_texture_destroy(src_tex);
error_src_tex:
wlr_raster_unlock(raster);
wlr_buffer_unlock(dst_buffer);
return false;
}