From 7da43ff8b05aa5280e1b94190a8ce157a8131af4 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Thu, 10 Aug 2017 08:26:16 -0400 Subject: [PATCH] Implement wlr_surface_flush_damage --- examples/compositor/main.c | 1 + include/wlr/types/wlr_surface.h | 1 + render/gles2/texture.c | 15 +++++++---- types/wlr_surface.c | 47 ++++++++++++++++++++++++--------- 4 files changed, 47 insertions(+), 17 deletions(-) diff --git a/examples/compositor/main.c b/examples/compositor/main.c index 7ac549e5..fd592600 100644 --- a/examples/compositor/main.c +++ b/examples/compositor/main.c @@ -42,6 +42,7 @@ void handle_output_frame(struct output_state *output, struct timespec *ts) { float matrix[16]; wl_list_for_each(_res, &sample->compositor.surfaces, link) { struct wlr_surface *surface = wl_resource_get_user_data(_res); + wlr_surface_flush_damage(surface); if (surface->texture->valid) { wlr_texture_get_matrix(surface->texture, &matrix, &wlr_output->transform_matrix, 200, 200); diff --git a/include/wlr/types/wlr_surface.h b/include/wlr/types/wlr_surface.h index b80b1de4..e8e747cd 100644 --- a/include/wlr/types/wlr_surface.h +++ b/include/wlr/types/wlr_surface.h @@ -47,5 +47,6 @@ struct wlr_surface { struct wlr_renderer; struct wlr_surface *wlr_surface_create(struct wl_resource *res, struct wlr_renderer *renderer); +void wlr_surface_flush_damage(struct wlr_surface *surface); #endif diff --git a/render/gles2/texture.c b/render/gles2/texture.c index f29f54a3..5126ec23 100644 --- a/render/gles2/texture.c +++ b/render/gles2/texture.c @@ -39,7 +39,9 @@ static bool gles2_texture_update_pixels(struct wlr_texture_state *texture, assert(texture && texture->wlr_texture->valid); // TODO: Test if the unpack subimage extension is supported and adjust the // upload strategy if not - if (texture->wlr_texture->format != format) { + if (!texture->wlr_texture->valid + || texture->wlr_texture->format != format + /* || unpack not supported */) { return gles2_texture_upload_pixels(texture, format, stride, width, height, pixels); } @@ -90,8 +92,10 @@ static bool gles2_texture_update_shm(struct wlr_texture_state *texture, struct wl_shm_buffer *buffer) { // TODO: Test if the unpack subimage extension is supported and adjust the // upload strategy if not - assert(texture && texture->wlr_texture->valid); - if (texture->wlr_texture->format != format) { + assert(texture); + if (!texture->wlr_texture->valid + || texture->wlr_texture->format != format + /* || unpack not supported */) { return gles2_texture_upload_shm(texture, format, buffer); } const struct pixel_format *fmt = texture->pixel_format; @@ -103,8 +107,9 @@ static bool gles2_texture_update_shm(struct wlr_texture_state *texture, GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, pitch)); GL_CALL(glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, x)); GL_CALL(glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, y)); - GL_CALL(glTexImage2D(GL_TEXTURE_2D, 0, fmt->gl_format, width, height, 0, - fmt->gl_format, fmt->gl_type, pixels)); + wlr_log(L_DEBUG, "%dx%d@%d,%d", width, height, x, y); + GL_CALL(glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, + fmt->gl_format, fmt->gl_type, pixels)); GL_CALL(glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0)); GL_CALL(glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0)); diff --git a/types/wlr_surface.c b/types/wlr_surface.c index ee57a05d..06495703 100644 --- a/types/wlr_surface.c +++ b/types/wlr_surface.c @@ -95,18 +95,6 @@ static void surface_commit(struct wl_client *client, if ((surface->pending.invalid & WLR_SURFACE_INVALID_BUFFER)) { surface->current.buffer = surface->pending.buffer; - // TODO: Move to wlr_surface_flush_damage and call from output frame - // callbacks instead of immediately here - if (surface->current.buffer) { - struct wl_shm_buffer *buffer = wl_shm_buffer_get(surface->current.buffer); - if (!buffer) { - wlr_log(L_INFO, "Unknown buffer handle attached"); - } else { - uint32_t format = wl_shm_buffer_get_format(buffer); - wlr_texture_upload_shm(surface->texture, format, buffer); - wl_resource_queue_event(surface->current.buffer, WL_BUFFER_RELEASE); - } - } } if ((surface->pending.invalid & WLR_SURFACE_INVALID_SURFACE_DAMAGE)) { // TODO: Sort out buffer damage too @@ -126,6 +114,41 @@ static void surface_commit(struct wl_client *client, wl_signal_emit(&surface->signals.commit, surface); } +void wlr_surface_flush_damage(struct wlr_surface *surface) { + if (!surface->current.buffer) { + if (surface->texture->valid) { + // TODO: Detach buffers + } + return; + } + struct wl_shm_buffer *buffer = wl_shm_buffer_get(surface->current.buffer); + if (!buffer) { + wlr_log(L_INFO, "Unknown buffer handle attached"); + return; + } + pixman_region32_t damage = surface->current.surface_damage; + if (!pixman_region32_not_empty(&damage)) { + return; + } + int n; + pixman_box32_t *rects = pixman_region32_rectangles(&damage, &n); + uint32_t format = wl_shm_buffer_get_format(buffer); + for (int i = 0; i < n; ++i) { + pixman_box32_t rect = rects[i]; + wlr_log(L_DEBUG, "%d,%d:%d,%d", rect.x1, rect.y1, rect.x2, rect.y2); + if (!wlr_texture_update_shm(surface->texture, format, + rect.x1, rect.y1, + rect.x2 - rect.x1, + rect.y2 - rect.y1, + buffer)) { + break; + } + } + pixman_region32_fini(&surface->current.surface_damage); + pixman_region32_init(&surface->current.surface_damage); + wl_resource_queue_event(surface->current.buffer, WL_BUFFER_RELEASE); +} + static void surface_set_buffer_transform(struct wl_client *client, struct wl_resource *resource, int transform) { wlr_log(L_DEBUG, "TODO: surface surface buffer transform");