diff options
-rw-r--r-- | backend/drm/drm.c | 101 | ||||
-rw-r--r-- | examples/screenshot.c | 181 | ||||
-rw-r--r-- | include/rootston/desktop.h | 2 | ||||
-rw-r--r-- | include/wlr/render.h | 3 | ||||
-rw-r--r-- | include/wlr/render/egl.h | 28 | ||||
-rw-r--r-- | include/wlr/render/interface.h | 3 | ||||
-rw-r--r-- | include/wlr/types/wlr_linux_dmabuf.h | 84 | ||||
-rw-r--r-- | meson.build | 2 | ||||
-rw-r--r-- | protocol/meson.build | 3 | ||||
-rw-r--r-- | render/egl.c | 157 | ||||
-rw-r--r-- | render/glapi.txt | 2 | ||||
-rw-r--r-- | render/gles2/renderer.c | 3 | ||||
-rw-r--r-- | render/gles2/shaders.c | 60 | ||||
-rw-r--r-- | render/gles2/texture.c | 67 | ||||
-rw-r--r-- | render/wlr_texture.c | 5 | ||||
-rw-r--r-- | rootston/desktop.c | 3 | ||||
-rw-r--r-- | rootston/seat.c | 4 | ||||
-rw-r--r-- | types/meson.build | 1 | ||||
-rw-r--r-- | types/wlr_linux_dmabuf.c | 464 | ||||
-rw-r--r-- | types/wlr_output.c | 58 | ||||
-rw-r--r-- | types/wlr_output_layout.c | 4 | ||||
-rw-r--r-- | types/wlr_surface.c | 4 | ||||
-rw-r--r-- | types/wlr_xdg_shell_v6.c | 4 |
23 files changed, 978 insertions, 265 deletions
diff --git a/backend/drm/drm.c b/backend/drm/drm.c index 3b714300..0d1d527d 100644 --- a/backend/drm/drm.c +++ b/backend/drm/drm.c @@ -544,10 +544,10 @@ static bool wlr_drm_connector_set_cursor(struct wlr_output *output, if (!crtc) { return false; } - struct wlr_drm_plane *plane = crtc->cursor; - // We don't have a real cursor plane, so we make a fake one + struct wlr_drm_plane *plane = crtc->cursor; if (!plane) { + // We don't have a real cursor plane, so we make a fake one plane = calloc(1, sizeof(*plane)); if (!plane) { wlr_log_errno(L_ERROR, "Allocation failed"); @@ -556,16 +556,6 @@ static bool wlr_drm_connector_set_cursor(struct wlr_output *output, crtc->cursor = plane; } - if (!buf && update_pixels) { - // Hide the cursor - plane->cursor_enabled = false; - if (!drm->session->active) { - return true; - } - return drm->iface->crtc_set_cursor(drm, crtc, NULL); - } - plane->cursor_enabled = true; - if (!plane->surf.gbm) { int ret; uint64_t w, h; @@ -607,62 +597,77 @@ static bool wlr_drm_connector_set_cursor(struct wlr_output *output, } } - struct wlr_box hotspot = { - .x = hotspot_x, - .y = hotspot_y, - }; + struct wlr_box hotspot = { .x = hotspot_x, .y = hotspot_y }; enum wl_output_transform transform = wlr_output_transform_invert(output->transform); wlr_box_transform(&hotspot, transform, plane->surf.width, plane->surf.height, &hotspot); - plane->cursor_hotspot_x = hotspot.x; - plane->cursor_hotspot_y = hotspot.y; + + if (plane->cursor_hotspot_x != hotspot.x || + plane->cursor_hotspot_y != hotspot.y) { + // Update cursor hotspot + conn->cursor_x -= hotspot.x - plane->cursor_hotspot_x; + conn->cursor_y -= hotspot.y - plane->cursor_hotspot_y; + plane->cursor_hotspot_x = hotspot.x; + plane->cursor_hotspot_y = hotspot.y; + + if (!drm->iface->crtc_move_cursor(drm, conn->crtc, conn->cursor_x, + conn->cursor_y)) { + return false; + } + + wlr_output_update_needs_swap(output); + } if (!update_pixels) { - // Only update the cursor hotspot + // Don't update cursor image return true; } - struct gbm_bo *bo = plane->cursor_bo; - uint32_t bo_width = gbm_bo_get_width(bo); - uint32_t bo_height = gbm_bo_get_height(bo); - uint32_t bo_stride; - void *bo_data; + plane->cursor_enabled = buf != NULL; - if (!gbm_bo_map(bo, 0, 0, bo_width, bo_height, - GBM_BO_TRANSFER_WRITE, &bo_stride, &bo_data)) { - wlr_log_errno(L_ERROR, "Unable to map buffer"); - return false; - } + if (buf != NULL) { + uint32_t bo_width = gbm_bo_get_width(plane->cursor_bo); + uint32_t bo_height = gbm_bo_get_height(plane->cursor_bo); + + uint32_t bo_stride; + void *bo_data; + if (!gbm_bo_map(plane->cursor_bo, 0, 0, bo_width, bo_height, + GBM_BO_TRANSFER_WRITE, &bo_stride, &bo_data)) { + wlr_log_errno(L_ERROR, "Unable to map buffer"); + return false; + } - wlr_drm_surface_make_current(&plane->surf, NULL); + wlr_drm_surface_make_current(&plane->surf, NULL); - wlr_texture_upload_pixels(plane->wlr_tex, WL_SHM_FORMAT_ARGB8888, - stride, width, height, buf); + wlr_texture_upload_pixels(plane->wlr_tex, WL_SHM_FORMAT_ARGB8888, + stride, width, height, buf); - glViewport(0, 0, plane->surf.width, plane->surf.height); - glClearColor(0.0, 0.0, 0.0, 0.0); - glClear(GL_COLOR_BUFFER_BIT); + glViewport(0, 0, plane->surf.width, plane->surf.height); + glClearColor(0.0, 0.0, 0.0, 0.0); + glClear(GL_COLOR_BUFFER_BIT); - float matrix[16]; - wlr_texture_get_matrix(plane->wlr_tex, &matrix, &plane->matrix, 0, 0); - wlr_render_with_matrix(plane->surf.renderer->wlr_rend, plane->wlr_tex, - &matrix, 1.0f); + float matrix[16]; + wlr_texture_get_matrix(plane->wlr_tex, &matrix, &plane->matrix, 0, 0); + wlr_render_with_matrix(plane->surf.renderer->wlr_rend, plane->wlr_tex, + &matrix, 1.0f); - glFinish(); - glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, bo_stride); - glReadPixels(0, 0, plane->surf.width, plane->surf.height, GL_BGRA_EXT, - GL_UNSIGNED_BYTE, bo_data); - glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, 0); + glFinish(); + glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, bo_stride); + glReadPixels(0, 0, plane->surf.width, plane->surf.height, GL_BGRA_EXT, + GL_UNSIGNED_BYTE, bo_data); + glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, 0); - wlr_drm_surface_swap_buffers(&plane->surf, NULL); + wlr_drm_surface_swap_buffers(&plane->surf, NULL); - gbm_bo_unmap(bo, bo_data); + gbm_bo_unmap(plane->cursor_bo, bo_data); + } if (!drm->session->active) { - return true; + return true; // will be committed when session is resumed } + struct gbm_bo *bo = plane->cursor_enabled ? plane->cursor_bo : NULL; bool ok = drm->iface->crtc_set_cursor(drm, crtc, bo); if (ok) { wlr_output_update_needs_swap(output); @@ -697,7 +702,7 @@ static bool wlr_drm_connector_move_cursor(struct wlr_output *output, conn->cursor_y = box.y; if (!drm->session->active) { - return true; + return true; // will be committed when session is resumed } bool ok = drm->iface->crtc_move_cursor(drm, conn->crtc, box.x, box.y); diff --git a/examples/screenshot.c b/examples/screenshot.c index e73989c6..fb37f32c 100644 --- a/examples/screenshot.c +++ b/examples/screenshot.c @@ -26,6 +26,7 @@ #include <errno.h> #include <fcntl.h> #include <limits.h> +#include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -36,33 +37,22 @@ #include <wayland-client.h> #include <wlr/util/log.h> #include "screenshooter-client-protocol.h" -#include "util/os-compatibility.h" static struct wl_shm *shm = NULL; static struct orbital_screenshooter *screenshooter = NULL; static struct wl_list output_list; -int min_x, min_y, max_x, max_y; -int buffer_copy_done; +static bool buffer_copy_done; struct screenshooter_output { struct wl_output *output; - struct wl_buffer *buffer; - int width, height, offset_x, offset_y; - enum wl_output_transform transform; - void *data; + int width, height; struct wl_list link; }; static void output_handle_geometry(void *data, struct wl_output *wl_output, int x, int y, int physical_width, int physical_height, int subpixel, const char *make, const char *model, int transform) { - struct screenshooter_output *output = wl_output_get_user_data(wl_output); - - if (wl_output == output->output) { - output->offset_x = x; - output->offset_y = y; - output->transform = transform; - } + // No-op } static void output_handle_mode(void *data, struct wl_output *wl_output, @@ -86,7 +76,7 @@ static const struct wl_output_listener output_listener = { }; static void screenshot_done(void *data, struct orbital_screenshot *screenshot) { - buffer_copy_done = 1; + buffer_copy_done = true; } static const struct orbital_screenshot_listener screenshot_listener = { @@ -113,7 +103,7 @@ static void handle_global(void *data, struct wl_registry *registry, static void handle_global_remove(void *data, struct wl_registry *registry, uint32_t name) { - // Unimplemented + // Who cares? } static const struct wl_registry_listener registry_listener = { @@ -123,14 +113,15 @@ static const struct wl_registry_listener registry_listener = { static int backingfile(off_t size) { char template[] = "/tmp/wlroots-shared-XXXXXX"; - int fd, ret; - - fd = mkstemp(template); + int fd = mkstemp(template); if (fd < 0) { return -1; } - while ((ret = ftruncate(fd, size)) == EINTR) {} + int ret; + while ((ret = ftruncate(fd, size)) == EINTR) { + // No-op + } if (ret < 0) { close(fd); return -1; @@ -140,7 +131,6 @@ static int backingfile(off_t size) { return fd; } - static struct wl_buffer *create_shm_buffer(int width, int height, void **data_out) { int stride = width * 4; @@ -170,91 +160,8 @@ static struct wl_buffer *create_shm_buffer(int width, int height, return buffer; } -static void write_image(const char *filename, int width, int height) { - int buffer_stride = width * 4; - - void *data = calloc(1, buffer_stride * height); - if (!data) { - return; - } - - struct screenshooter_output *output, *next; - wl_list_for_each_safe(output, next, &output_list, link) { - int output_stride = output->width * 4; - uint32_t *src = (uint32_t *)output->data; - uint32_t *dst = (uint32_t *)(data + - (output->offset_y - min_y) * buffer_stride + - (output->offset_x - min_x) * 4); - - switch (output->transform) { - case WL_OUTPUT_TRANSFORM_NORMAL: - for (int i = 0; i < output->height; i++) { - memcpy(dst, src, output_stride); - dst += width; - src += output->width; - } - break; - case WL_OUTPUT_TRANSFORM_FLIPPED: - for (int i = 0; i < output->height; ++i) { - for (int j = 0; j < output->width; ++j) { - dst[i * width + j] = - src[i * output->width + output->width - 1 - j]; - } - } - break; - case WL_OUTPUT_TRANSFORM_90: - for (int i = 0; i < output->width; ++i) { - for (int j = 0; j < output->height; ++j) { - dst[i * width + j] = - src[j * output->width + output->width - 1 - i]; - } - } - break; - case WL_OUTPUT_TRANSFORM_FLIPPED_90: - for (int i = 0; i < output->width; ++i) { - for (int j = 0; j < output->height; ++j) { - dst[i * width + j] = - src[(output->height - 1 - j) * output->width + output->width - 1 - i]; - } - } - break; - case WL_OUTPUT_TRANSFORM_180: - for (int i = 0; i < output->height; ++i) { - for (int j = 0; j < output->width; ++j) { - dst[i * width + j] = - src[(output->height - 1 - i) * output->width + output->width - 1 - j]; - } - } - break; - case WL_OUTPUT_TRANSFORM_FLIPPED_180: - for (int i = 0; i < output->height; ++i) { - for (int j = 0; j < output->width; ++j) { - dst[i * width + j] = - src[(output->height - 1 - i) * output->width + j]; - } - } - break; - case WL_OUTPUT_TRANSFORM_270: - for (int i = 0; i < output->width; ++i) { - for (int j = 0; j < output->height; ++j) { - dst[i * width + j] = - src[(output->height - 1 - j) * output->width + i]; - } - } - break; - case WL_OUTPUT_TRANSFORM_FLIPPED_270: - for (int i = 0; i < output->width; ++i) { - for (int j = 0; j < output->height; ++j) { - dst[i * width + j] = - src[j * output->width + i]; - } - } - break; - } - - free(output); - } - +static void write_image(const char *filename, int width, int height, + void *data) { char size[10 + 1 + 10 + 2 + 1]; // int32_t are max 10 digits sprintf(size, "%dx%d+0", width, height); @@ -270,12 +177,11 @@ static void write_image(const char *filename, int width, int height) { exit(EXIT_FAILURE); } else if (child != 0) { close(fd[0]); - if (write(fd[1], data, buffer_stride * height) < 0) { + if (write(fd[1], data, 4 * width * height) < 0) { fprintf(stderr, "write() failed: %s\n", strerror(errno)); exit(EXIT_FAILURE); } close(fd[1]); - free(data); waitpid(child, NULL, 0); } else { close(fd[1]); @@ -293,38 +199,9 @@ static void write_image(const char *filename, int width, int height) { } } -static int set_buffer_size(int *width, int *height) { - int owidth, oheight; - min_x = min_y = INT_MAX; - max_x = max_y = INT_MIN; - - struct screenshooter_output *output; - wl_list_for_each(output, &output_list, link) { - if (output->transform & 0x1) { - owidth = output->height; - oheight = output->width; - } else { - owidth = output->width; - oheight = output->height; - } - min_x = MIN(min_x, output->offset_x); - min_y = MIN(min_y, output->offset_y); - max_x = MAX(max_x, output->offset_x + owidth); - max_y = MAX(max_y, output->offset_y + oheight); - } - - if (max_x <= min_x || max_y <= min_y) { - return -1; - } - - *width = max_x - min_x; - *height = max_y - min_y; - - return 0; -} - int main(int argc, char *argv[]) { wlr_log_init(L_DEBUG, NULL); + struct wl_display * display = wl_display_connect(NULL); if (display == NULL) { fprintf(stderr, "failed to create display: %m\n"); @@ -342,27 +219,31 @@ int main(int argc, char *argv[]) { return -1; } - int width, height; - if (set_buffer_size(&width, &height)) { - fprintf(stderr, "cannot set buffer size\n"); - return -1; - } - + int i = 0; struct screenshooter_output *output; wl_list_for_each(output, &output_list, link) { - output->buffer = create_shm_buffer(output->width, output->height, &output->data); - if (output->buffer == NULL) { + void *data = NULL; + struct wl_buffer *buffer = + create_shm_buffer(output->width, output->height, &data); + if (buffer == NULL) { return -1; } struct orbital_screenshot *screenshot = orbital_screenshooter_shoot( - screenshooter, output->output, output->buffer); - orbital_screenshot_add_listener(screenshot, &screenshot_listener, screenshot); - buffer_copy_done = 0; + screenshooter, output->output, buffer); + orbital_screenshot_add_listener(screenshot, &screenshot_listener, + screenshot); + buffer_copy_done = false; while (!buffer_copy_done) { wl_display_roundtrip(display); } + + char filename[24 + 10]; // int32_t are max 10 digits + snprintf(filename, sizeof(filename), "wayland-screenshot-%d.png", i); + + write_image(filename, output->width, output->height, data); + wl_buffer_destroy(buffer); + ++i; } - write_image("wayland-screenshot.png", width, height); return EXIT_SUCCESS; } diff --git a/include/rootston/desktop.h b/include/rootston/desktop.h index 61fe47b0..5986c96f 100644 --- a/include/rootston/desktop.h +++ b/include/rootston/desktop.h @@ -7,6 +7,7 @@ #include <wlr/types/wlr_compositor.h> #include <wlr/types/wlr_gamma_control.h> #include <wlr/types/wlr_idle.h> +#include <wlr/types/wlr_linux_dmabuf.h> #include <wlr/types/wlr_list.h> #include <wlr/types/wlr_output_layout.h> #include <wlr/types/wlr_output.h> @@ -46,6 +47,7 @@ struct roots_desktop { struct wlr_primary_selection_device_manager *primary_selection_device_manager; struct wlr_idle *idle; struct wlr_idle_inhibit_manager_v1 *idle_inhibit; + struct wlr_linux_dmabuf *linux_dmabuf; struct wl_listener new_output; struct wl_listener layout_change; diff --git a/include/wlr/render.h b/include/wlr/render.h index 747603da..9080175f 100644 --- a/include/wlr/render.h +++ b/include/wlr/render.h @@ -85,6 +85,7 @@ struct wlr_texture { bool valid; uint32_t format; int width, height; + bool inverted_y; struct wl_signal destroy_signal; struct wl_resource *resource; }; @@ -122,6 +123,8 @@ bool wlr_texture_upload_drm(struct wlr_texture *tex, bool wlr_texture_upload_eglimage(struct wlr_texture *tex, EGLImageKHR image, uint32_t width, uint32_t height); +bool wlr_texture_upload_dmabuf(struct wlr_texture *tex, + struct wl_resource *dmabuf_resource); /** * Copies a rectangle of pixels from a wl_shm_buffer onto the texture. The * buffer is not accessed after this function returns. Under some circumstances, diff --git a/include/wlr/render/egl.h b/include/wlr/render/egl.h index 97a28016..f05a9837 100644 --- a/include/wlr/render/egl.h +++ b/include/wlr/render/egl.h @@ -6,6 +6,7 @@ #include <pixman.h> #include <stdbool.h> #include <wayland-server.h> +#include <wlr/types/wlr_linux_dmabuf.h> struct wlr_egl { EGLDisplay display; @@ -18,6 +19,8 @@ struct wlr_egl { struct { bool buffer_age; bool swap_buffers_with_damage; + bool dmabuf_import; + bool dmabuf_import_modifiers; } egl_exts; struct wl_display *wl_display; @@ -62,6 +65,31 @@ EGLImageKHR wlr_egl_create_image(struct wlr_egl *egl, EGLenum target, EGLClientBuffer buffer, const EGLint *attribs); /** + * Creates an egl image from the given dmabuf attributes. Check usability + * of the dmabuf with wlr_egl_check_import_dmabuf once first. + */ +EGLImageKHR wlr_egl_create_image_from_dmabuf(struct wlr_egl *egl, + struct wlr_dmabuf_buffer_attribs *attributes); + +/** + * Try to import the given dmabuf. On success return true false otherwise. + * If this succeeds the dmabuf can be used for rendering on a texture + */ +bool wlr_egl_check_import_dmabuf(struct wlr_egl *egl, + struct wlr_dmabuf_buffer *dmabuf); + +/** + * Get the available dmabuf formats + */ +int wlr_egl_get_dmabuf_formats(struct wlr_egl *egl, int **formats); + +/** + * Get the available dmabuf modifiers for a given format + */ +int wlr_egl_get_dmabuf_modifiers(struct wlr_egl *egl, int format, + uint64_t **modifiers); + +/** * Destroys an egl image created with the given wlr_egl. */ bool wlr_egl_destroy_image(struct wlr_egl *egl, EGLImageKHR image); diff --git a/include/wlr/render/interface.h b/include/wlr/render/interface.h index eda5af1c..2ba584d9 100644 --- a/include/wlr/render/interface.h +++ b/include/wlr/render/interface.h @@ -8,6 +8,7 @@ #include <wlr/render.h> #include <wlr/types/wlr_box.h> #include <wlr/types/wlr_output.h> +#include <wlr/types/wlr_linux_dmabuf.h> struct wlr_renderer_impl; @@ -58,6 +59,8 @@ struct wlr_texture_impl { struct wl_resource *drm_buf); bool (*upload_eglimage)(struct wlr_texture *texture, EGLImageKHR image, uint32_t width, uint32_t height); + bool (*upload_dmabuf)(struct wlr_texture *texture, + struct wl_resource *dmabuf_resource); void (*get_matrix)(struct wlr_texture *state, float (*matrix)[16], const float (*projection)[16], int x, int y); void (*get_buffer_size)(struct wlr_texture *texture, diff --git a/include/wlr/types/wlr_linux_dmabuf.h b/include/wlr/types/wlr_linux_dmabuf.h new file mode 100644 index 00000000..9d71e598 --- /dev/null +++ b/include/wlr/types/wlr_linux_dmabuf.h @@ -0,0 +1,84 @@ +#ifndef WLR_TYPES_WLR_LINUX_DMABUF_H +#define WLR_TYPES_WLR_LINUX_DMABUF_H + +#define WLR_LINUX_DMABUF_MAX_PLANES 4 + +#include <stdint.h> +#include <wayland-server-protocol.h> + +/* So we don't have to pull in linux specific drm headers */ +#ifndef DRM_FORMAT_MOD_INVALID +#define DRM_FORMAT_MOD_INVALID ((1ULL<<56) - 1) +#endif + +struct wlr_dmabuf_buffer_attribs { + /* set via params_add */ + int n_planes; + uint32_t offset[WLR_LINUX_DMABUF_MAX_PLANES]; + uint32_t stride[WLR_LINUX_DMABUF_MAX_PLANES]; + uint64_t modifier[WLR_LINUX_DMABUF_MAX_PLANES]; + int fd[WLR_LINUX_DMABUF_MAX_PLANES]; + /* set via params_create */ + int32_t width; + int32_t height; + uint32_t format; + uint32_t flags; /* enum zlinux_buffer_params_flags */ +}; + +struct wlr_dmabuf_buffer { + struct wlr_egl *egl; + struct wl_resource *buffer_resource; + struct wl_resource *params_resource; + struct wlr_dmabuf_buffer_attribs attributes; +}; + +/** + * Returns true if the given resource was created via the linux-dmabuf + * buffer protocol, false otherwise + */ +bool wlr_dmabuf_resource_is_buffer(struct wl_resource *buffer_resource); + +/** + * Returns the wlr_dmabuf_buffer if the given resource was created + * via the linux-dmabuf buffer protocol + */ +struct wlr_dmabuf_buffer *wlr_dmabuf_buffer_from_buffer_resource( + struct wl_resource *buffer_resource); + +/** + * Returns the wlr_dmabuf_buffer if the given resource was created + * via the linux-dmabuf params protocol + */ +struct wlr_dmabuf_buffer *wlr_dmabuf_buffer_from_params_resource( + struct wl_resource *params_resource); + +/** + * Returns true if the given dmabuf has y-axis inverted, false otherwise + */ +bool wlr_dmabuf_buffer_has_inverted_y(struct wlr_dmabuf_buffer *dmabuf); + +/* the protocol interface */ +struct wlr_linux_dmabuf { + struct wl_global *wl_global; + struct wl_listener display_destroy; + struct wlr_egl *egl; +}; + +/** + * Create linux-dmabuf interface + */ +struct wlr_linux_dmabuf *wlr_linux_dmabuf_create(struct wl_display *display, + struct wlr_egl *egl); +/** + * Destroy the linux-dmabuf interface + */ +void wlr_linux_dmabuf_destroy(struct wlr_linux_dmabuf *linux_dmabuf); + +/** + * Returns the wlr_linux_dmabuf if the given resource was created + * via the linux_dmabuf protocol + */ +struct wlr_linux_dmabuf *wlr_linux_dmabuf_from_resource( + struct wl_resource *resource); + +#endif diff --git a/meson.build b/meson.build index 553d2b88..c7ca038c 100644 --- a/meson.build +++ b/meson.build @@ -196,7 +196,7 @@ git = find_program('git', required: false) if git.found() all_files = run_command( git, - ['--git-dir=@0@/.git'.format(meson.source_root()), + ['--git-dir=@0@/.git'.format(meson.current_source_dir()), 'ls-files', ':/*.[ch]']) all_files = files(all_files.stdout().split()) diff --git a/protocol/meson.build b/protocol/meson.build index 6c87a887..638b0c46 100644 --- a/protocol/meson.build +++ b/protocol/meson.build @@ -21,9 +21,10 @@ wayland_scanner_client = generator( ) protocols = [ - [wl_protocol_dir, 'unstable/xdg-shell/xdg-shell-unstable-v6.xml'], [wl_protocol_dir, 'stable/xdg-shell/xdg-shell.xml'], [wl_protocol_dir, 'unstable/idle-inhibit/idle-inhibit-unstable-v1.xml'], + [wl_protocol_dir, 'unstable/linux-dmabuf/linux-dmabuf-unstable-v1.xml'], + [wl_protocol_dir, 'unstable/xdg-shell/xdg-shell-unstable-v6.xml'], 'gamma-control.xml', 'gtk-primary-selection.xml', 'idle.xml', diff --git a/render/egl.c b/render/egl.c index 0a68d6e5..d60da2ab 100644 --- a/render/egl.c +++ b/render/egl.c @@ -1,4 +1,5 @@ #include <assert.h> +#include <stdio.h> #include <EGL/egl.h> #include <EGL/eglext.h> #include <GLES2/gl2.h> @@ -101,6 +102,26 @@ static bool check_egl_ext(const char *egl_exts, const char *ext) { return false; } +static void print_dmabuf_formats(struct wlr_egl *egl) { + /* Avoid log msg if extension is not present */ + if (!egl->egl_exts.dmabuf_import_modifiers) { + return; + } + + int *formats; + int num = wlr_egl_get_dmabuf_formats(egl, &formats); + if (num < 0) { + return; + } + + char str_formats[num * 5 + 1]; + for (int i = 0; i < num; i++) { + snprintf(&str_formats[i*5], (num - i) * 5 + 1, "%.4s ", (char*)&formats[i]); + } + wlr_log(L_INFO, "Supported dmabuf buffer formats: %s", str_formats); + free(formats); +} + bool wlr_egl_init(struct wlr_egl *egl, EGLenum platform, void *remote_display, EGLint *config_attribs, EGLint visual_id) { if (!load_glapi()) { @@ -167,6 +188,13 @@ bool wlr_egl_init(struct wlr_egl *egl, EGLenum platform, void *remote_display, check_egl_ext(egl->egl_exts_str, "EGL_EXT_swap_buffers_with_damage") || check_egl_ext(egl->egl_exts_str, "EGL_KHR_swap_buffers_with_damage"); + egl->egl_exts.dmabuf_import = + check_egl_ext(egl->egl_exts_str, "EGL_EXT_image_dma_buf_import"); + egl->egl_exts.dmabuf_import_modifiers = + check_egl_ext(egl->egl_exts_str, "EGL_EXT_image_dma_buf_import_modifiers") + && eglQueryDmaBufFormatsEXT && eglQueryDmaBufModifiersEXT; + print_dmabuf_formats(egl); + return true; error: @@ -299,3 +327,132 @@ bool wlr_egl_swap_buffers(struct wlr_egl *egl, EGLSurface surface, } return true; } + +EGLImage wlr_egl_create_image_from_dmabuf(struct wlr_egl *egl, + struct wlr_dmabuf_buffer_attribs *attributes) { + int atti = 0; + EGLint attribs[20]; + attribs[atti++] = EGL_WIDTH; + attribs[atti++] = attributes->width; + attribs[atti++] = EGL_HEIGHT; + attribs[atti++] = attributes->height; + attribs[atti++] = EGL_LINUX_DRM_FOURCC_EXT; + attribs[atti++] = attributes->format; + + bool has_modifier = false; + if (attributes->modifier[0] != DRM_FORMAT_MOD_INVALID) { + if (!egl->egl_exts.dmabuf_import_modifiers) { + return NULL; + } + has_modifier = true; + } + + /* TODO: YUV planes have up four planes but we only support a + single EGLImage for now */ + if (attributes->n_planes > 1) { + return NULL; + } + + attribs[atti++] = EGL_DMA_BUF_PLANE0_FD_EXT; + attribs[atti++] = attributes->fd[0]; + attribs[atti++] = EGL_DMA_BUF_PLANE0_OFFSET_EXT; + attribs[atti++] = attributes->offset[0]; + attribs[atti++] = EGL_DMA_BUF_PLANE0_PITCH_EXT; + attribs[atti++] = attributes->stride[0]; + if (has_modifier) { + attribs[atti++] = EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT; + attribs[atti++] = attributes->modifier[0] & 0xFFFFFFFF; + attribs[atti++] = EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT; + attribs[atti++] = attributes->modifier[0] >> 32; + } + attribs[atti++] = EGL_NONE; + return eglCreateImageKHR(egl->display, EGL_NO_CONTEXT, + EGL_LINUX_DMA_BUF_EXT, NULL, attribs); +} + +#ifndef DRM_FORMAT_BIG_ENDIAN +# define DRM_FORMAT_BIG_ENDIAN 0x80000000 +#endif +bool wlr_egl_check_import_dmabuf(struct wlr_egl *egl, + struct wlr_dmabuf_buffer *dmabuf) { + switch (dmabuf->attributes.format & ~DRM_FORMAT_BIG_ENDIAN) { + /* YUV based formats not yet supported */ + case WL_SHM_FORMAT_YUYV: + case WL_SHM_FORMAT_YVYU: + case WL_SHM_FORMAT_UYVY: + case WL_SHM_FORMAT_VYUY: + case WL_SHM_FORMAT_AYUV: + return false; + default: + break; + } + + EGLImage egl_image = wlr_egl_create_image_from_dmabuf(egl, + &dmabuf->attributes); + if (egl_image) { + /* We can import the image, good. No need to keep it + since wlr_texture_upload_dmabuf will import it again */ + wlr_egl_destroy_image(egl, egl_image); + return true; + } + /* TODO: import yuv dmabufs */ + return false; +} + +int wlr_egl_get_dmabuf_formats(struct wlr_egl *egl, + int **formats) { + if (!egl->egl_exts.dmabuf_import || + !egl->egl_exts.dmabuf_import_modifiers) { + wlr_log(L_ERROR, "dmabuf extension not present"); + return -1; + } + + EGLint num; + if (!eglQueryDmaBufFormatsEXT(egl->display, 0, NULL, &num)) { + wlr_log(L_ERROR, "failed to query number of dmabuf formats"); + return -1; + } + + *formats = calloc(num, sizeof(int)); + if (*formats == NULL) { + wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno)); + return -1; + } + + if (!eglQueryDmaBufFormatsEXT(egl->display, num, *formats, &num)) { + wlr_log(L_ERROR, "failed to query dmabuf format"); + free(*formats); + return -1; + } + return num; +} + +int wlr_egl_get_dmabuf_modifiers(struct wlr_egl *egl, + int format, uint64_t **modifiers) { + if (!egl->egl_exts.dmabuf_import || + !egl->egl_exts.dmabuf_import_modifiers) { + wlr_log(L_ERROR, "dmabuf extension not present"); + return -1; + } + + EGLint num; + if (!eglQueryDmaBufModifiersEXT(egl->display, format, 0, + NULL, NULL, &num)) { + wlr_log(L_ERROR, "failed to query dmabuf number of modifiers"); + return -1; + } + + *modifiers = calloc(num, sizeof(uint64_t)); + if (*modifiers == NULL) { + wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno)); + return -1; + } + + if (!eglQueryDmaBufModifiersEXT(egl->display, format, num, + *modifiers, NULL, &num)) { + wlr_log(L_ERROR, "failed to query dmabuf modifiers"); + free(*modifiers); + return -1; + } + return num; +} diff --git a/render/glapi.txt b/render/glapi.txt index 0b0b452c..02ac7dd8 100644 --- a/render/glapi.txt +++ b/render/glapi.txt @@ -8,3 +8,5 @@ eglCreatePlatformWindowSurfaceEXT -glEGLImageTargetTexture2DOES -eglSwapBuffersWithDamageEXT -eglSwapBuffersWithDamageKHR +-eglQueryDmaBufFormatsEXT +-eglQueryDmaBufModifiersEXT diff --git a/render/gles2/renderer.c b/render/gles2/renderer.c index ad739cf8..ba03f599 100644 --- a/render/gles2/renderer.c +++ b/render/gles2/renderer.c @@ -179,7 +179,8 @@ static bool wlr_gles2_render_texture(struct wlr_renderer *wlr_renderer, wlr_texture_bind(texture); GL_CALL(glUniformMatrix4fv(0, 1, GL_FALSE, *matrix)); - GL_CALL(glUniform1f(2, alpha)); + GL_CALL(glUniform1i(1, texture->inverted_y)); + GL_CALL(glUniform1f(3, alpha)); draw_quad(); return true; } diff --git a/render/gles2/shaders.c b/render/gles2/shaders.c index 46a10248..c8ba2ae6 100644 --- a/render/gles2/shaders.c +++ b/render/gles2/shaders.c @@ -10,17 +10,17 @@ const GLchar quad_vertex_src[] = "varying vec4 v_color;" "varying vec2 v_texcoord;" "mat4 transpose(in mat4 inMatrix) {" -" vec4 i0 = inMatrix[0];" -" vec4 i1 = inMatrix[1];" -" vec4 i2 = inMatrix[2];" -" vec4 i3 = inMatrix[3];" -" mat4 outMatrix = mat4(" -" vec4(i0.x, i1.x, i2.x, i3.x)," -" vec4(i0.y, i1.y, i2.y, i3.y)," -" vec4(i0.z, i1.z, i2.z, i3.z)," -" vec4(i0.w, i1.w, i2.w, i3.w)" -" );" -" return outMatrix;" +" vec4 i0 = inMatrix[0];" +" vec4 i1 = inMatrix[1];" +" vec4 i2 = inMatrix[2];" +" vec4 i3 = inMatrix[3];" +" mat4 outMatrix = mat4(" +" vec4(i0.x, i1.x, i2.x, i3.x)," +" vec4(i0.y, i1.y, i2.y, i3.y)," +" vec4(i0.z, i1.z, i2.z, i3.z)," +" vec4(i0.w, i1.w, i2.w, i3.w)" +" );" +" return outMatrix;" "}" "void main() {" " gl_Position = transpose(proj) * vec4(pos, 0.0, 1.0);" @@ -50,26 +50,30 @@ const GLchar ellipse_fragment_src[] = // Textured quads const GLchar vertex_src[] = "uniform mat4 proj;" +"uniform bool invert_y;" "attribute vec2 pos;" "attribute vec2 texcoord;" "varying vec2 v_texcoord;" "mat4 transpose(in mat4 inMatrix) {" -" vec4 i0 = inMatrix[0];" -" vec4 i1 = inMatrix[1];" -" vec4 i2 = inMatrix[2];" -" vec4 i3 = inMatrix[3];" -" mat4 outMatrix = mat4(" -" vec4(i0.x, i1.x, i2.x, i3.x)," -" vec4(i0.y, i1.y, i2.y, i3.y)," -" vec4(i0.z, i1.z, i2.z, i3.z)," -" vec4(i0.w, i1.w, i2.w, i3.w)" -" );" -"" -" return outMatrix;" +" vec4 i0 = inMatrix[0];" +" vec4 i1 = inMatrix[1];" +" vec4 i2 = inMatrix[2];" +" vec4 i3 = inMatrix[3];" +" mat4 outMatrix = mat4(" +" vec4(i0.x, i1.x, i2.x, i3.x)," +" vec4(i0.y, i1.y, i2.y, i3.y)," +" vec4(i0.z, i1.z, i2.z, i3.z)," +" vec4(i0.w, i1.w, i2.w, i3.w)" +" );" +" return outMatrix;" "}" "void main() {" -" gl_Position = transpose(proj) * vec4(pos, 0.0, 1.0);" -" v_texcoord = texcoord;" +" gl_Position = transpose(proj) * vec4(pos, 0.0, 1.0);" +" if (invert_y) {" +" v_texcoord = vec2(texcoord.s, 1.0 - texcoord.t);" +" } else {" +" v_texcoord = texcoord;" +" }" "}"; const GLchar fragment_src_rgba[] = @@ -78,7 +82,7 @@ const GLchar fragment_src_rgba[] = "uniform sampler2D tex;" "uniform float alpha;" "void main() {" -" gl_FragColor = alpha * texture2D(tex, v_texcoord);" +" gl_FragColor = alpha * texture2D(tex, v_texcoord);" "}"; const GLchar fragment_src_rgbx[] = @@ -87,8 +91,8 @@ const GLchar fragment_src_rgbx[] = "uniform sampler2D tex;" "uniform float alpha;" "void main() {" -" gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb;" -" gl_FragColor.a = alpha;" +" gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb;" +" gl_FragColor.a = alpha;" "}"; const GLchar fragment_src_external[] = diff --git a/render/gles2/texture.c b/render/gles2/texture.c index 241b94a8..775f7cdc 100644 --- a/render/gles2/texture.c +++ b/render/gles2/texture.c @@ -163,7 +163,10 @@ static bool gles2_texture_upload_drm(struct wlr_texture *_tex, (EGLint*)&tex->wlr_texture.height); EGLint inverted_y; - wlr_egl_query_buffer(tex->egl, buf, EGL_WAYLAND_Y_INVERTED_WL, &inverted_y); + if (wlr_egl_query_buffer(tex->egl, buf, EGL_WAYLAND_Y_INVERTED_WL, + &inverted_y)) { + tex->wlr_texture.inverted_y = !!inverted_y; + } GLenum target; const struct pixel_format *pf; @@ -226,6 +229,42 @@ static bool gles2_texture_upload_eglimage(struct wlr_texture *wlr_tex, return true; } + +static bool gles2_texture_upload_dmabuf(struct wlr_texture *_tex, + struct wl_resource *dmabuf_resource) { + struct wlr_gles2_texture *tex = (struct wlr_gles2_texture *)_tex; + struct wlr_dmabuf_buffer *dmabuf = wlr_dmabuf_buffer_from_buffer_resource( + dmabuf_resource); + + if (!tex->egl->egl_exts.dmabuf_import) { + wlr_log(L_ERROR, "Want dmabuf but extension not present"); + return false; + } + + tex->wlr_texture.width = dmabuf->attributes.width; + tex->wlr_texture.height = dmabuf->attributes.height; + + if (tex->image) { + wlr_egl_destroy_image(tex->egl, tex->image); + } + + if (wlr_dmabuf_buffer_has_inverted_y(dmabuf)) { + _tex->inverted_y = true; + } + + GLenum target = GL_TEXTURE_2D; + const struct pixel_format *pf = + gl_format_for_wl_format(WL_SHM_FORMAT_ARGB8888); + gles2_texture_ensure_texture(tex); + GL_CALL(glBindTexture(target, tex->tex_id)); + tex->image = wlr_egl_create_image_from_dmabuf(tex->egl, &dmabuf->attributes); + GL_CALL(glActiveTexture(GL_TEXTURE0)); + GL_CALL(glEGLImageTargetTexture2DOES(target, tex->image)); + tex->pixel_format = pf; + tex->wlr_texture.valid = true; + return true; +} + static void gles2_texture_get_matrix(struct wlr_texture *_texture, float (*matrix)[16], const float (*projection)[16], int x, int y) { struct wlr_gles2_texture *texture = (struct wlr_gles2_texture *)_texture; @@ -239,6 +278,21 @@ static void gles2_texture_get_matrix(struct wlr_texture *_texture, wlr_matrix_mul(projection, matrix, matrix); } + +static bool gles2_texture_get_dmabuf_size(struct wlr_texture *texture, struct + wl_resource *resource, int *width, int *height) { + if (!wlr_dmabuf_resource_is_buffer(resource)) { + return false; + } + + struct wlr_dmabuf_buffer *dmabuf = wlr_dmabuf_buffer_from_buffer_resource( + resource); + *width = dmabuf->attributes.width; + *height = dmabuf->attributes.height; + return true; +} + + static void gles2_texture_get_buffer_size(struct wlr_texture *texture, struct wl_resource *resource, int *width, int *height) { struct wl_shm_buffer *buffer = wl_shm_buffer_get(resource); @@ -249,10 +303,12 @@ static void gles2_texture_get_buffer_size(struct wlr_texture *texture, struct } if (!wlr_egl_query_buffer(tex->egl, resource, EGL_WIDTH, (EGLint*)width)) { - wlr_log(L_ERROR, "could not get size of the buffer " - "(no buffer found)"); - return; - }; + if (!gles2_texture_get_dmabuf_size(texture, resource, + width, height)) { + wlr_log(L_ERROR, "could not get size of the buffer"); + return; + } + } wlr_egl_query_buffer(tex->egl, resource, EGL_HEIGHT, (EGLint*)height); @@ -291,6 +347,7 @@ static struct wlr_texture_impl wlr_texture_impl = { .upload_shm = gles2_texture_upload_shm, .update_shm = gles2_texture_update_shm, .upload_drm = gles2_texture_upload_drm, + .upload_dmabuf = gles2_texture_upload_dmabuf, .upload_eglimage = gles2_texture_upload_eglimage, .get_matrix = gles2_texture_get_matrix, .get_buffer_size = gles2_texture_get_buffer_size, diff --git a/render/wlr_texture.c b/render/wlr_texture.c index a82a16b2..e4e6b9ff 100644 --- a/render/wlr_texture.c +++ b/render/wlr_texture.c @@ -53,6 +53,11 @@ bool wlr_texture_upload_eglimage(struct wlr_texture *texture, return texture->impl->upload_eglimage(texture, image, width, height); } +bool wlr_texture_upload_dmabuf(struct wlr_texture *texture, + struct wl_resource *dmabuf_resource) { + return texture->impl->upload_dmabuf(texture, dmabuf_resource); +} + void wlr_texture_get_matrix(struct wlr_texture *texture, float (*matrix)[16], const float (*projection)[16], int x, int y) { texture->impl->get_matrix(texture, matrix, projection, x, y); diff --git a/rootston/desktop.c b/rootston/desktop.c index 78076e75..9730ca57 100644 --- a/rootston/desktop.c +++ b/rootston/desktop.c @@ -9,6 +9,7 @@ #include <wlr/types/wlr_cursor.h> #include <wlr/types/wlr_gamma_control.h> #include <wlr/types/wlr_idle.h> +#include <wlr/types/wlr_linux_dmabuf.h> #include <wlr/types/wlr_output_layout.h> #include <wlr/types/wlr_idle_inhibit_v1.h> #include <wlr/types/wlr_primary_selection.h> @@ -768,6 +769,8 @@ struct roots_desktop *desktop_create(struct roots_server *server, desktop->idle = wlr_idle_create(server->wl_display); desktop->idle_inhibit = wlr_idle_inhibit_v1_create(server->wl_display); + struct wlr_egl *egl = wlr_backend_get_egl(server->backend); + desktop->linux_dmabuf = wlr_linux_dmabuf_create(server->wl_display, egl); return desktop; } diff --git a/rootston/seat.c b/rootston/seat.c index c4535c7c..d2d211ba 100644 --- a/rootston/seat.c +++ b/rootston/seat.c @@ -514,13 +514,13 @@ static void seat_add_tablet_pad(struct roots_seat *seat, static void handle_tablet_tool_destroy(struct wl_listener *listener, void *data) { - struct roots_pointer *tablet_tool = + struct roots_tablet_tool *tablet_tool = wl_container_of(listener, tablet_tool, device_destroy); struct roots_seat *seat = tablet_tool->seat; - wl_list_remove(&tablet_tool->link); wlr_cursor_detach_input_device(seat->cursor->cursor, tablet_tool->device); wl_list_remove(&tablet_tool->device_destroy.link); + wl_list_remove(&tablet_tool->link); free(tablet_tool); seat_update_capabilities(seat); diff --git a/types/meson.build b/types/meson.build index 703b06ca..11aa5e20 100644 --- a/types/meson.build +++ b/types/meson.build @@ -9,6 +9,7 @@ lib_wlr_types = static_library( 'wlr_idle.c', 'wlr_input_device.c', 'wlr_keyboard.c', + 'wlr_linux_dmabuf.c', 'wlr_list.c', 'wlr_output_damage.c', 'wlr_output_layout.c', diff --git a/types/wlr_linux_dmabuf.c b/types/wlr_linux_dmabuf.c new file mode 100644 index 00000000..3b86166e --- /dev/null +++ b/types/wlr_linux_dmabuf.c @@ -0,0 +1,464 @@ +#ifndef _POSIX_C_SOURCE +#define _POSIX_C_SOURCE 200809L +#endif +#include <assert.h> +#include <stdlib.h> +#include <unistd.h> +#include <wayland-server.h> +#include <wlr/render.h> +#include <wlr/render/egl.h> +#include <wlr/types/wlr_linux_dmabuf.h> +#include <wlr/util/log.h> +#include "linux-dmabuf-unstable-v1-protocol.h" + +static void wl_buffer_destroy(struct wl_client *client, + struct wl_resource *resource) { + wl_resource_destroy(resource); +} + +static const struct wl_buffer_interface wl_buffer_impl = { + wl_buffer_destroy, +}; + +bool wlr_dmabuf_buffer_has_inverted_y(struct wlr_dmabuf_buffer *dmabuf) { + return dmabuf->attributes.flags + & ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_Y_INVERT; +} + +bool wlr_dmabuf_resource_is_buffer(struct wl_resource *buffer_resource) { + if (!wl_resource_instance_of(buffer_resource, &wl_buffer_interface, + &wl_buffer_impl)) { + return false; + } + + struct wlr_dmabuf_buffer *buffer = wl_resource_get_user_data(buffer_resource); + if (buffer && buffer->buffer_resource && !buffer->params_resource && + buffer->buffer_resource == buffer_resource) { + return true; + } + + return false; +} + +struct wlr_dmabuf_buffer *wlr_dmabuf_buffer_from_buffer_resource( + struct wl_resource *buffer_resource) { + assert(wl_resource_instance_of(buffer_resource, &wl_buffer_interface, + &wl_buffer_impl)); + + struct wlr_dmabuf_buffer *buffer = wl_resource_get_user_data(buffer_resource); + assert(buffer); + assert(buffer->buffer_resource); + assert(!buffer->params_resource); + assert(buffer->buffer_resource == buffer_resource); + + return buffer; +} + +static void linux_dmabuf_buffer_destroy(struct wlr_dmabuf_buffer *buffer) { + for (int i = 0; i < buffer->attributes.n_planes; i++) { + close(buffer->attributes.fd[i]); + buffer->attributes.fd[i] = -1; + } + buffer->attributes.n_planes = 0; + free(buffer); +} + +static void params_destroy(struct wl_client *client, struct wl_resource *resource) { + wl_resource_destroy(resource); +} + +static void params_add(struct wl_client *client, + struct wl_resource *params_resource, int32_t name_fd, + uint32_t plane_idx, uint32_t offset, uint32_t stride, + uint32_t modifier_hi, uint32_t modifier_lo) { + struct wlr_dmabuf_buffer *buffer = wlr_dmabuf_buffer_from_params_resource( + params_resource); + + if (!buffer) { + wl_resource_post_error(params_resource, + ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_ALREADY_USED, + "params was already used to create a wl_buffer"); + close(name_fd); + return; + } + + if (plane_idx >= WLR_LINUX_DMABUF_MAX_PLANES) { + wl_resource_post_error(params_resource, + ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_PLANE_IDX, + "plane index %u > %u", plane_idx, WLR_LINUX_DMABUF_MAX_PLANES); + close(name_fd); + return; + } + + if (buffer->attributes.fd[plane_idx] != -1) { + wl_resource_post_error(params_resource, + ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_PLANE_SET, + "a dmabuf with id %d has already been added for plane %u", + buffer->attributes.fd[plane_idx], + plane_idx); + close(name_fd); + return; + } + + buffer->attributes.fd[plane_idx] = name_fd; + buffer->attributes.offset[plane_idx] = offset; + buffer->attributes.stride[plane_idx] = stride; + buffer->attributes.modifier[plane_idx] = ((uint64_t)modifier_hi << 32) | + modifier_lo; + buffer->attributes.n_planes++; +} + +static void handle_buffer_destroy(struct wl_resource *buffer_resource) +{ + struct wlr_dmabuf_buffer *buffer = wlr_dmabuf_buffer_from_buffer_resource( + buffer_resource); + + linux_dmabuf_buffer_destroy(buffer); +} + +static void params_create_common(struct wl_client *client, + struct wl_resource *params_resource, uint32_t buffer_id, int32_t width, + int32_t height, uint32_t format, uint32_t flags) { + if (!wl_resource_get_user_data(params_resource)) { + wl_resource_post_error(params_resource, + ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_ALREADY_USED, + "params was already used to create a wl_buffer"); + return; + } + struct wlr_dmabuf_buffer *buffer = wlr_dmabuf_buffer_from_params_resource( + params_resource); + + /* Switch the linux_dmabuf_buffer object from params resource to + * eventually wl_buffer resource. */ + wl_resource_set_user_data(buffer->params_resource, NULL); + buffer->params_resource = NULL; + + if (!buffer->attributes.n_planes) { + wl_resource_post_error(params_resource, + ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_INCOMPLETE, + "no dmabuf has been added to the params"); + goto err_out; + } + + /* TODO: support more planes */ + if (buffer->attributes.n_planes != 1) { + wl_resource_post_error(params_resource, + ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_INCOMPLETE, + "only single plane buffers supported not %d", + buffer->attributes.n_planes); + goto err_out; + } + + if (buffer->attributes.fd[0] == -1) { + wl_resource_post_error(params_resource, + ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_INCOMPLETE, + "no dmabuf has been added for plane"); + goto err_out; + } + + buffer->attributes.width = width; + buffer->attributes.height = height; + buffer->attributes.format = format; + buffer->attributes.flags = flags; + + if (width < 1 || height < 1) { + wl_resource_post_error(params_resource, + ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_INVALID_DIMENSIONS, + "invalid width %d or height %d", width, height); + goto err_out; + } + + if ((uint64_t)buffer->attributes.offset[0] + buffer->attributes.stride[0] > UINT32_MAX) { + wl_resource_post_error(params_resource, + ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_OUT_OF_BOUNDS, + "size overflow for plane"); + goto err_out; + } + + if ((uint64_t)buffer->attributes.offset[0] + + (uint64_t)buffer->attributes.stride[0] * height > UINT32_MAX) { + wl_resource_post_error(params_resource, + ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_OUT_OF_BOUNDS, + "size overflow for plane"); + goto err_out; + } + + off_t size = lseek(buffer->attributes.fd[0], 0, SEEK_END); + if (size != -1) { /* Skip checks if kernel does no support seek on buffer */ + if (buffer->attributes.offset[0] >= size) { + wl_resource_post_error(params_resource, + ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_OUT_OF_BOUNDS, + "invalid offset %i for plane", + buffer->attributes.offset[0]); + goto err_out; + } + + if (buffer->attributes.offset[0] + buffer->attributes.stride[0] > size) { + wl_resource_post_error(params_resource, + ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_OUT_OF_BOUNDS, + "invalid stride %i for plane", + buffer->attributes.stride[0]); + goto err_out; + } + + if (buffer->attributes.offset[0] + buffer->attributes.stride[0] * height > size) { + wl_resource_post_error(params_resource, + ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_OUT_OF_BOUNDS, + "invalid buffer stride or height for plane"); + goto err_out; + } + } + + /* reject unknown flags */ + if (buffer->attributes.flags & ~ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_Y_INVERT) { + wl_resource_post_error(params_resource, + ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_INVALID_FORMAT, + "Unknown dmabuf flags %"PRIu32, buffer->attributes.flags); + goto err_out; + } + + /* Check if dmabuf is usable */ + if (!wlr_egl_check_import_dmabuf(buffer->egl, buffer)) { + goto err_failed; + } + + buffer->buffer_resource = wl_resource_create(client, &wl_buffer_interface, + 1, buffer_id); + if (!buffer->buffer_resource) { + wl_resource_post_no_memory(params_resource); + goto err_failed; + } + + wl_resource_set_implementation(buffer->buffer_resource, + &wl_buffer_impl, buffer, handle_buffer_destroy); + + /* send 'created' event when the request is not for an immediate + * import, that is buffer_id is zero */ + if (buffer_id == 0) { + zwp_linux_buffer_params_v1_send_created(params_resource, + buffer->buffer_resource); + } + return; + +err_failed: + if (buffer_id == 0) { + zwp_linux_buffer_params_v1_send_failed(params_resource); + } else { + /* since the behavior is left implementation defined by the + * protocol in case of create_immed failure due to an unknown cause, + * we choose to treat it as a fatal error and immediately kill the + * client instead of creating an invalid handle and waiting for it + * to be used. + */ + wl_resource_post_error(params_resource, + ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_INVALID_WL_BUFFER, + "importing the supplied dmabufs failed"); + } +err_out: + linux_dmabuf_buffer_destroy(buffer); + return; +} + +static void params_create(struct wl_client *client, + struct wl_resource *params_resource, + int32_t width, int32_t height,uint32_t format, uint32_t flags) { + params_create_common(client, params_resource, 0, width, height, format, flags); +} + +static void params_create_immed(struct wl_client *client, + struct wl_resource *params_resource, uint32_t buffer_id, + int32_t width, int32_t height,uint32_t format, uint32_t flags) { + params_create_common(client, params_resource, buffer_id, width, height, format, flags); +} + +static const struct zwp_linux_buffer_params_v1_interface linux_buffer_params_impl = { + params_destroy, + params_add, + params_create, + params_create_immed, +}; + +struct wlr_dmabuf_buffer *wlr_dmabuf_buffer_from_params_resource( + struct wl_resource *params_resource) { + assert(wl_resource_instance_of(params_resource, + &zwp_linux_buffer_params_v1_interface, + &linux_buffer_params_impl)); + + struct wlr_dmabuf_buffer *buffer = wl_resource_get_user_data(params_resource); + assert(buffer); + assert(buffer->params_resource); + assert(!buffer->buffer_resource); + assert(buffer->params_resource == params_resource); + + return buffer; +} + +static void handle_params_destroy(struct wl_resource *params_resource) { + /* Check for NULL since wlr_dmabuf_buffer_from_params_resource will choke */ + if (!wl_resource_get_user_data(params_resource)) { + return; + } + + struct wlr_dmabuf_buffer *buffer = + wlr_dmabuf_buffer_from_params_resource(params_resource); + linux_dmabuf_buffer_destroy(buffer); +} + +static void linux_dmabuf_create_params(struct wl_client *client, + struct wl_resource *linux_dmabuf_resource, + uint32_t params_id) { + struct wlr_linux_dmabuf *linux_dmabuf = wlr_linux_dmabuf_from_resource( + linux_dmabuf_resource); + + uint32_t version = wl_resource_get_version(linux_dmabuf_resource); + struct wlr_dmabuf_buffer *buffer = calloc(1, sizeof *buffer); + if (!buffer) { + goto err; + } + + for (int i = 0; i < WLR_LINUX_DMABUF_MAX_PLANES; i++) { + buffer->attributes.fd[i] = -1; + } + + buffer->egl = linux_dmabuf->egl; + buffer->params_resource = wl_resource_create(client, + &zwp_linux_buffer_params_v1_interface, + version, params_id); + if (!buffer->params_resource) { + goto err_free; + } + + wl_resource_set_implementation(buffer->params_resource, + &linux_buffer_params_impl,buffer, handle_params_destroy); + return; + +err_free: + free(buffer); +err: + wl_resource_post_no_memory(linux_dmabuf_resource); +} + +static void linux_dmabuf_destroy(struct wl_client *client, struct wl_resource *resource) { + wl_resource_destroy(resource); +} + +static const struct zwp_linux_dmabuf_v1_interface linux_dmabuf_impl = { + linux_dmabuf_destroy, + linux_dmabuf_create_params +}; + +struct wlr_linux_dmabuf *wlr_linux_dmabuf_from_resource( + struct wl_resource *resource) { + assert(wl_resource_instance_of(resource, &zwp_linux_dmabuf_v1_interface, + &linux_dmabuf_impl)); + + struct wlr_linux_dmabuf *dmabuf = wl_resource_get_user_data(resource); + assert(dmabuf); + return dmabuf; +} + +static void linux_dmabuf_send_modifiers(struct wlr_linux_dmabuf *linux_dmabuf, + struct wl_resource *resource) { + struct wlr_egl *egl = linux_dmabuf->egl; + /* + * Use EGL_EXT_image_dma_buf_import_modifiers to query and advertise + * format/modifier codes. + */ + uint64_t modifier_invalid = DRM_FORMAT_MOD_INVALID; + int *formats = NULL; + int num_formats = wlr_egl_get_dmabuf_formats(egl, &formats); + + if (num_formats < 0) { + return; + } + + for (int i = 0; i < num_formats; i++) { + int num_modifiers; + uint64_t *modifiers = NULL; + + num_modifiers = wlr_egl_get_dmabuf_modifiers(egl, formats[i], + &modifiers); + if (num_modifiers < 0) { + return; + } + /* send DRM_FORMAT_MOD_INVALID token when no modifiers are supported + * for this format */ + if (num_modifiers == 0) { + num_modifiers = 1; + modifiers = &modifier_invalid; + } + for (int j = 0; j < num_modifiers; j++) { + uint32_t modifier_lo = modifiers[j] & 0xFFFFFFFF; + uint32_t modifier_hi = modifiers[j] >> 32; + zwp_linux_dmabuf_v1_send_modifier(resource, formats[i], + modifier_hi, + modifier_lo); + } + if (modifiers != &modifier_invalid) { + free(modifiers); + } + } + free(formats); +} + +static void linux_dmabuf_bind(struct wl_client *client, + void *data, uint32_t version, uint32_t id) { + struct wlr_linux_dmabuf *linux_dmabuf = data; + struct wl_resource *resource = wl_resource_create(client, + &zwp_linux_dmabuf_v1_interface, + version, id); + + if (resource == NULL) { + wl_client_post_no_memory(client); + return; + } + + wl_resource_set_implementation(resource, &linux_dmabuf_impl, + linux_dmabuf, NULL); + if (version < ZWP_LINUX_DMABUF_V1_MODIFIER_SINCE_VERSION) { + return; + } + + linux_dmabuf_send_modifiers(linux_dmabuf, resource); +} + +void wlr_linux_dmabuf_destroy(struct wlr_linux_dmabuf *linux_dmabuf) { + if (!linux_dmabuf) { + return; + } + wl_list_remove(&linux_dmabuf->display_destroy.link); + + wl_global_destroy(linux_dmabuf->wl_global); + free(linux_dmabuf); +} + +static void handle_display_destroy(struct wl_listener *listener, void *data) { + struct wlr_linux_dmabuf *linux_dmabuf = wl_container_of(listener, linux_dmabuf, display_destroy); + wlr_linux_dmabuf_destroy(linux_dmabuf); +} + +struct wlr_linux_dmabuf *wlr_linux_dmabuf_create(struct wl_display *display, + struct wlr_egl *egl) { + struct wlr_linux_dmabuf *linux_dmabuf = + calloc(1, sizeof(struct wlr_linux_dmabuf)); + if (linux_dmabuf == NULL) { + wlr_log(L_ERROR, "could not create simple dmabuf manager"); + return NULL; + } + + linux_dmabuf->display_destroy.notify = handle_display_destroy; + wl_display_add_destroy_listener(display, &linux_dmabuf->display_destroy); + + linux_dmabuf->wl_global = + wl_global_create(display, &zwp_linux_dmabuf_v1_interface, + 3, linux_dmabuf, linux_dmabuf_bind); + + linux_dmabuf->egl = egl; + if (!linux_dmabuf->wl_global) { + wlr_log(L_ERROR, "could not create linux dmabuf v1 wl global"); + free(linux_dmabuf); + return NULL; + } + + return linux_dmabuf; +} diff --git a/types/wlr_output.c b/types/wlr_output.c index 96c9d324..7f19d1fe 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -669,6 +669,31 @@ static void output_cursor_reset(struct wlr_output_cursor *cursor) { } } +static void output_cursor_update_visible(struct wlr_output_cursor *cursor) { + struct wlr_box output_box; + output_box.x = output_box.y = 0; + wlr_output_transformed_resolution(cursor->output, &output_box.width, + &output_box.height); + + struct wlr_box cursor_box; + output_cursor_get_box(cursor, &cursor_box); + + struct wlr_box intersection; + bool visible = + wlr_box_intersection(&output_box, &cursor_box, &intersection); + + if (cursor->surface != NULL) { + if (cursor->visible && !visible) { + wlr_surface_send_leave(cursor->surface, cursor->output); + } + if (!cursor->visible && visible) { + wlr_surface_send_enter(cursor->surface, cursor->output); + } + } + + cursor->visible = visible; +} + bool wlr_output_cursor_set_image(struct wlr_output_cursor *cursor, const uint8_t *pixels, int32_t stride, uint32_t width, uint32_t height, int32_t hotspot_x, int32_t hotspot_y) { @@ -682,6 +707,7 @@ bool wlr_output_cursor_set_image(struct wlr_output_cursor *cursor, cursor->height = height; cursor->hotspot_x = hotspot_x; cursor->hotspot_y = hotspot_y; + output_cursor_update_visible(cursor); struct wlr_output_cursor *hwcur = cursor->output->hardware_cursor; if (cursor->output->impl->set_cursor && (hwcur == NULL || hwcur == cursor)) { @@ -716,31 +742,6 @@ bool wlr_output_cursor_set_image(struct wlr_output_cursor *cursor, stride, width, height, pixels); } -static void output_cursor_update_visible(struct wlr_output_cursor *cursor) { - struct wlr_box output_box; - output_box.x = output_box.y = 0; - wlr_output_transformed_resolution(cursor->output, &output_box.width, - &output_box.height); - - struct wlr_box cursor_box; - output_cursor_get_box(cursor, &cursor_box); - - struct wlr_box intersection; - bool visible = - wlr_box_intersection(&output_box, &cursor_box, &intersection); - - if (cursor->surface != NULL) { - if (cursor->visible && !visible) { - wlr_surface_send_leave(cursor->surface, cursor->output); - } - if (!cursor->visible && visible) { - wlr_surface_send_enter(cursor->surface, cursor->output); - } - } - - cursor->visible = visible; -} - static void output_cursor_commit(struct wlr_output_cursor *cursor) { if (cursor->output->hardware_cursor != cursor) { output_cursor_damage_whole(cursor); @@ -846,12 +847,18 @@ bool wlr_output_cursor_move(struct wlr_output_cursor *cursor, output_cursor_damage_whole(cursor); } + bool was_visible = cursor->visible; x *= cursor->output->scale; y *= cursor->output->scale; cursor->x = x; cursor->y = y; output_cursor_update_visible(cursor); + if (!was_visible && !cursor->visible) { + // Cursor is still hidden, do nothing + return true; + } + if (cursor->output->hardware_cursor != cursor) { output_cursor_damage_whole(cursor); return true; @@ -876,6 +883,7 @@ struct wlr_output_cursor *wlr_output_cursor_create(struct wlr_output *output) { wl_list_init(&cursor->surface_destroy.link); cursor->surface_destroy.notify = output_cursor_handle_destroy; wl_list_insert(&output->cursors, &cursor->link); + cursor->visible = true; // default position is at (0, 0) return cursor; } diff --git a/types/wlr_output_layout.c b/types/wlr_output_layout.c index c305f04d..2462bdd2 100644 --- a/types/wlr_output_layout.c +++ b/types/wlr_output_layout.c @@ -48,6 +48,7 @@ struct wlr_output_layout *wlr_output_layout_create() { static void wlr_output_layout_output_destroy( struct wlr_output_layout_output *l_output) { wlr_signal_emit_safe(&l_output->events.destroy, l_output); + wlr_output_destroy_global(l_output->output); wl_list_remove(&l_output->state->mode.link); wl_list_remove(&l_output->state->scale.link); wl_list_remove(&l_output->state->transform.link); @@ -64,7 +65,7 @@ void wlr_output_layout_destroy(struct wlr_output_layout *layout) { wlr_signal_emit_safe(&layout->events.destroy, layout); - struct wlr_output_layout_output *l_output, *temp = NULL; + struct wlr_output_layout_output *l_output, *temp; wl_list_for_each_safe(l_output, temp, &layout->outputs, link) { wlr_output_layout_output_destroy(l_output); } @@ -291,7 +292,6 @@ void wlr_output_layout_remove(struct wlr_output_layout *layout, wlr_output_layout_output_destroy(l_output); wlr_output_layout_reconfigure(layout); } - wlr_output_destroy_global(output); } void wlr_output_layout_output_coords(struct wlr_output_layout *layout, diff --git a/types/wlr_surface.c b/types/wlr_surface.c index 23966cd1..51bd47bc 100644 --- a/types/wlr_surface.c +++ b/types/wlr_surface.c @@ -325,6 +325,10 @@ static void wlr_surface_apply_damage(struct wlr_surface *surface, surface->current->buffer)) { wlr_texture_upload_drm(surface->texture, surface->current->buffer); goto release; + } else if (wlr_dmabuf_resource_is_buffer( + surface->current->buffer)) { + wlr_texture_upload_dmabuf(surface->texture, surface->current->buffer); + goto release; } else { wlr_log(L_INFO, "Unknown buffer handle attached"); return; diff --git a/types/wlr_xdg_shell_v6.c b/types/wlr_xdg_shell_v6.c index ef6045c2..a0c74e6f 100644 --- a/types/wlr_xdg_shell_v6.c +++ b/types/wlr_xdg_shell_v6.c @@ -13,8 +13,8 @@ #include "util/signal.h" #include "xdg-shell-unstable-v6-protocol.h" -static const char *wlr_desktop_xdg_toplevel_role = "xdg_toplevel"; -static const char *wlr_desktop_xdg_popup_role = "xdg_popup"; +static const char *wlr_desktop_xdg_toplevel_role = "xdg_toplevel_v6"; +static const char *wlr_desktop_xdg_popup_role = "xdg_popup_v6"; struct wlr_xdg_positioner_v6 { struct wl_resource *resource; |