diff --git a/include/types/wlr_buffer.h b/include/types/wlr_buffer.h index f9d2974d..56b50f3d 100644 --- a/include/types/wlr_buffer.h +++ b/include/types/wlr_buffer.h @@ -16,13 +16,16 @@ enum wlr_buffer_cap { }; /** - * Access a pointer to the allocated data from the underlying implementation, - * its format and its stride. + * Get a pointer to a region of memory referring to the buffer's underlying + * storage. The format and stride can be used to interpret the memory region + * contents. * - * The returned pointer should be pointing to a valid memory location for read - * and write operations. + * The returned pointer should be pointing to a valid memory region for read + * and write operations. The returned pointer is only valid up to the next + * buffer_end_data_ptr_access call. */ -bool buffer_get_data_ptr(struct wlr_buffer *buffer, void **data, +bool buffer_begin_data_ptr_access(struct wlr_buffer *buffer, void **data, uint32_t *format, size_t *stride); +void buffer_end_data_ptr_access(struct wlr_buffer *buffer); #endif diff --git a/include/wlr/types/wlr_buffer.h b/include/wlr/types/wlr_buffer.h index 3b8b76f6..47f17cce 100644 --- a/include/wlr/types/wlr_buffer.h +++ b/include/wlr/types/wlr_buffer.h @@ -26,10 +26,11 @@ struct wlr_buffer_impl { void (*destroy)(struct wlr_buffer *buffer); bool (*get_dmabuf)(struct wlr_buffer *buffer, struct wlr_dmabuf_attributes *attribs); - bool (*get_data_ptr)(struct wlr_buffer *buffer, void **data, - uint32_t *format, size_t *stride); bool (*get_shm)(struct wlr_buffer *buffer, struct wlr_shm_attributes *attribs); + bool (*begin_data_ptr_access)(struct wlr_buffer *buffer, void **data, + uint32_t *format, size_t *stride); + void (*end_data_ptr_access)(struct wlr_buffer *buffer); }; /** @@ -47,6 +48,7 @@ struct wlr_buffer { bool dropped; size_t n_locks; + bool accessing_data_ptr; struct { struct wl_signal destroy; diff --git a/render/allocator.c b/render/allocator.c index 4f87d95a..aade40a9 100644 --- a/render/allocator.c +++ b/render/allocator.c @@ -81,7 +81,8 @@ struct wlr_buffer *wlr_allocator_create_buffer(struct wlr_allocator *alloc, return NULL; } if (alloc->buffer_caps & WLR_BUFFER_CAP_DATA_PTR) { - assert(buffer->impl->get_data_ptr); + assert(buffer->impl->begin_data_ptr_access && + buffer->impl->end_data_ptr_access); } if (alloc->buffer_caps & WLR_BUFFER_CAP_DMABUF) { assert(buffer->impl->get_dmabuf); diff --git a/render/drm_dumb_allocator.c b/render/drm_dumb_allocator.c index 9e14e38e..9713e078 100644 --- a/render/drm_dumb_allocator.c +++ b/render/drm_dumb_allocator.c @@ -128,8 +128,8 @@ create_err: return NULL; } -static bool drm_dumb_buffer_get_data_ptr(struct wlr_buffer *wlr_buffer, void **data, - uint32_t *format, size_t *stride) { +static bool drm_dumb_buffer_begin_data_ptr_access(struct wlr_buffer *wlr_buffer, + void **data, uint32_t *format, size_t *stride) { struct wlr_drm_dumb_buffer *buf = drm_dumb_buffer_from_buffer(wlr_buffer); *data = buf->data; *stride = buf->stride; @@ -137,6 +137,10 @@ static bool drm_dumb_buffer_get_data_ptr(struct wlr_buffer *wlr_buffer, void **d return true; } +static void drm_dumb_buffer_end_data_ptr_access(struct wlr_buffer *wlr_buffer) { + // This space is intentionally left blank +} + static bool buffer_get_dmabuf(struct wlr_buffer *wlr_buffer, struct wlr_dmabuf_attributes *attribs) { struct wlr_drm_dumb_buffer *buf = drm_dumb_buffer_from_buffer(wlr_buffer); @@ -153,7 +157,8 @@ static void buffer_destroy(struct wlr_buffer *wlr_buffer) { static const struct wlr_buffer_impl buffer_impl = { .destroy = buffer_destroy, .get_dmabuf = buffer_get_dmabuf, - .get_data_ptr = drm_dumb_buffer_get_data_ptr, + .begin_data_ptr_access = drm_dumb_buffer_begin_data_ptr_access, + .end_data_ptr_access = drm_dumb_buffer_end_data_ptr_access, }; static const struct wlr_allocator_interface allocator_impl; diff --git a/render/pixman/renderer.c b/render/pixman/renderer.c index a5a402f2..ff9375ee 100644 --- a/render/pixman/renderer.c +++ b/render/pixman/renderer.c @@ -86,10 +86,11 @@ static struct wlr_pixman_buffer *create_buffer( void *data = NULL; uint32_t drm_format; size_t stride; - if (!buffer_get_data_ptr(wlr_buffer, &data, &drm_format, &stride)) { + if (!buffer_begin_data_ptr_access(wlr_buffer, &data, &drm_format, &stride)) { wlr_log(WLR_ERROR, "Failed to get buffer data"); goto error_buffer; } + buffer_end_data_ptr_access(wlr_buffer); pixman_format_code_t format = get_pixman_format_from_drm(drm_format); if (format == 0) { @@ -125,6 +126,33 @@ static void pixman_begin(struct wlr_renderer *wlr_renderer, uint32_t width, struct wlr_pixman_renderer *renderer = get_renderer(wlr_renderer); renderer->width = width; renderer->height = height; + + struct wlr_pixman_buffer *buffer = renderer->current_buffer; + assert(buffer != NULL); + + void *data = NULL; + uint32_t drm_format; + size_t stride; + buffer_begin_data_ptr_access(buffer->buffer, &data, &drm_format, &stride); + + // If the data pointer has changed, re-create the Pixman image. This can + // happen if it's a client buffer and the wl_shm_pool has been resized. + if (data != pixman_image_get_data(buffer->image)) { + pixman_format_code_t format = get_pixman_format_from_drm(drm_format); + assert(format != 0); + + pixman_image_unref(buffer->image); + buffer->image = pixman_image_create_bits_no_clear(format, + buffer->buffer->width, buffer->buffer->height, data, stride); + } +} + +static void pixman_end(struct wlr_renderer *wlr_renderer) { + struct wlr_pixman_renderer *renderer = get_renderer(wlr_renderer); + + assert(renderer->current_buffer != NULL); + + buffer_end_data_ptr_access(renderer->current_buffer->buffer); } static void pixman_clear(struct wlr_renderer *wlr_renderer, @@ -413,6 +441,7 @@ static uint32_t pixman_get_render_buffer_caps(void) { static const struct wlr_renderer_impl renderer_impl = { .begin = pixman_begin, + .end = pixman_end, .clear = pixman_clear, .scissor = pixman_scissor, .render_subtexture_with_matrix = pixman_render_subtexture_with_matrix, diff --git a/render/shm_allocator.c b/render/shm_allocator.c index e1a3d9e5..2066b125 100644 --- a/render/shm_allocator.c +++ b/render/shm_allocator.c @@ -31,8 +31,8 @@ static bool buffer_get_shm(struct wlr_buffer *wlr_buffer, return true; } -static bool shm_buffer_get_data_ptr(struct wlr_buffer *wlr_buffer, void **data, - uint32_t *format, size_t *stride) { +static bool shm_buffer_begin_data_ptr_access(struct wlr_buffer *wlr_buffer, + void **data, uint32_t *format, size_t *stride) { struct wlr_shm_buffer *buffer = shm_buffer_from_buffer(wlr_buffer); *data = buffer->data; *format = buffer->shm.format; @@ -40,10 +40,15 @@ static bool shm_buffer_get_data_ptr(struct wlr_buffer *wlr_buffer, void **data, return true; } +static void shm_buffer_end_data_ptr_access(struct wlr_buffer *wlr_buffer) { + // This space is intentionally left blank +} + static const struct wlr_buffer_impl buffer_impl = { .destroy = buffer_destroy, .get_shm = buffer_get_shm, - .get_data_ptr = shm_buffer_get_data_ptr, + .begin_data_ptr_access = shm_buffer_begin_data_ptr_access, + .end_data_ptr_access = shm_buffer_end_data_ptr_access, }; static struct wlr_buffer *allocator_create_buffer( diff --git a/types/wlr_buffer.c b/types/wlr_buffer.c index 10ed7295..c35c8f2f 100644 --- a/types/wlr_buffer.c +++ b/types/wlr_buffer.c @@ -12,6 +12,9 @@ void wlr_buffer_init(struct wlr_buffer *buffer, const struct wlr_buffer_impl *impl, int width, int height) { assert(impl->destroy); + if (impl->begin_data_ptr_access || impl->end_data_ptr_access) { + assert(impl->begin_data_ptr_access && impl->end_data_ptr_access); + } buffer->impl = impl; buffer->width = width; buffer->height = height; @@ -24,6 +27,8 @@ static void buffer_consider_destroy(struct wlr_buffer *buffer) { return; } + assert(!buffer->accessing_data_ptr); + wlr_signal_emit_safe(&buffer->events.destroy, NULL); buffer->impl->destroy(buffer); @@ -67,12 +72,23 @@ bool wlr_buffer_get_dmabuf(struct wlr_buffer *buffer, return buffer->impl->get_dmabuf(buffer, attribs); } -bool buffer_get_data_ptr(struct wlr_buffer *buffer, void **data, +bool buffer_begin_data_ptr_access(struct wlr_buffer *buffer, void **data, uint32_t *format, size_t *stride) { - if (!buffer->impl->get_data_ptr) { + assert(!buffer->accessing_data_ptr); + if (!buffer->impl->begin_data_ptr_access) { + return false; + } + if (!buffer->impl->begin_data_ptr_access(buffer, data, format, stride)) { return false; } - return buffer->impl->get_data_ptr(buffer, data, format, stride); + buffer->accessing_data_ptr = true; + return true; +} + +void buffer_end_data_ptr_access(struct wlr_buffer *buffer) { + assert(buffer->accessing_data_ptr); + buffer->impl->end_data_ptr_access(buffer); + buffer->accessing_data_ptr = false; } bool wlr_buffer_get_shm(struct wlr_buffer *buffer,