output: always use hardware cursors if available

This changes the `wlr_output_impl.set_cursor` function to take a
`wlr_texture` instead of a byte buffer. This simplifies the
DRM and Wayland backends since they were creating textures from
the byte buffer anyway.

With this commit, performance should be improved when moving the
cursor since outputs don't need to be re-rendered anymore.
This commit is contained in:
emersion 2018-05-01 21:38:04 +01:00
parent 509d38425c
commit 225d182765
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
6 changed files with 113 additions and 140 deletions

View File

@ -537,8 +537,8 @@ static void drm_connector_transform(struct wlr_output *output,
} }
static bool drm_connector_set_cursor(struct wlr_output *output, static bool drm_connector_set_cursor(struct wlr_output *output,
const uint8_t *buf, int32_t stride, uint32_t width, uint32_t height, struct wlr_texture *texture, int32_t hotspot_x, int32_t hotspot_y,
int32_t hotspot_x, int32_t hotspot_y, bool update_pixels) { bool update_texture) {
struct wlr_drm_connector *conn = (struct wlr_drm_connector *)output; struct wlr_drm_connector *conn = (struct wlr_drm_connector *)output;
struct wlr_drm_backend *drm = (struct wlr_drm_backend *)output->backend; struct wlr_drm_backend *drm = (struct wlr_drm_backend *)output->backend;
struct wlr_drm_renderer *renderer = &drm->renderer; struct wlr_drm_renderer *renderer = &drm->renderer;
@ -567,11 +567,6 @@ static bool drm_connector_set_cursor(struct wlr_output *output,
ret = drmGetCap(drm->fd, DRM_CAP_CURSOR_HEIGHT, &h); ret = drmGetCap(drm->fd, DRM_CAP_CURSOR_HEIGHT, &h);
h = ret ? 64 : h; h = ret ? 64 : h;
if (width > w || height > h) {
wlr_log(L_INFO, "Cursor too large (max %dx%d)", (int)w, (int)h);
return false;
}
if (!init_drm_surface(&plane->surf, renderer, w, h, if (!init_drm_surface(&plane->surf, renderer, w, h,
GBM_FORMAT_ARGB8888, 0)) { GBM_FORMAT_ARGB8888, 0)) {
wlr_log(L_ERROR, "Cannot allocate cursor resources"); wlr_log(L_ERROR, "Cannot allocate cursor resources");
@ -612,14 +607,22 @@ static bool drm_connector_set_cursor(struct wlr_output *output,
wlr_output_update_needs_swap(output); wlr_output_update_needs_swap(output);
} }
if (!update_pixels) { if (!update_texture) {
// Don't update cursor image // Don't update cursor image
return true; return true;
} }
plane->cursor_enabled = buf != NULL; plane->cursor_enabled = false;
if (texture != NULL) {
int width, height;
wlr_texture_get_size(texture, &width, &height);
if (width > (int)plane->surf.width || height > (int)plane->surf.height) {
wlr_log(L_ERROR, "Cursor too large (max %dx%d)",
(int)plane->surf.width, (int)plane->surf.height);
return false;
}
if (buf != NULL) {
uint32_t bo_width = gbm_bo_get_width(plane->cursor_bo); 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_height = gbm_bo_get_height(plane->cursor_bo);
@ -635,13 +638,6 @@ static bool drm_connector_set_cursor(struct wlr_output *output,
struct wlr_renderer *rend = plane->surf.renderer->wlr_rend; struct wlr_renderer *rend = plane->surf.renderer->wlr_rend;
struct wlr_texture *texture = wlr_texture_from_pixels(rend,
WL_SHM_FORMAT_ARGB8888, stride, width, height, buf);
if (texture == NULL) {
wlr_log(L_ERROR, "Unable to create texture");
return false;
}
wlr_renderer_begin(rend, plane->surf.width, plane->surf.height); wlr_renderer_begin(rend, plane->surf.width, plane->surf.height);
wlr_renderer_clear(rend, (float[]){ 0.0, 0.0, 0.0, 0.0 }); wlr_renderer_clear(rend, (float[]){ 0.0, 0.0, 0.0, 0.0 });
wlr_render_texture(rend, texture, plane->matrix, 0, 0, 1.0f); wlr_render_texture(rend, texture, plane->matrix, 0, 0, 1.0f);
@ -652,8 +648,9 @@ static bool drm_connector_set_cursor(struct wlr_output *output,
swap_drm_surface_buffers(&plane->surf, NULL); swap_drm_surface_buffers(&plane->surf, NULL);
wlr_texture_destroy(texture);
gbm_bo_unmap(plane->cursor_bo, bo_data); gbm_bo_unmap(plane->cursor_bo, bo_data);
plane->cursor_enabled = true;
} }
if (!drm->session->active) { if (!drm->session->active) {

View File

@ -164,8 +164,16 @@ struct wlr_backend *wlr_wl_backend_create(struct wl_display *display,
goto error_registry; goto error_registry;
} }
static EGLint config_attribs[] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_RED_SIZE, 1,
EGL_GREEN_SIZE, 1,
EGL_BLUE_SIZE, 1,
EGL_ALPHA_SIZE, 1,
EGL_NONE,
};
if (!wlr_egl_init(&backend->egl, EGL_PLATFORM_WAYLAND_EXT, if (!wlr_egl_init(&backend->egl, EGL_PLATFORM_WAYLAND_EXT,
backend->remote_display, NULL, WL_SHM_FORMAT_ARGB8888)) { backend->remote_display, config_attribs, WL_SHM_FORMAT_ARGB8888)) {
wlr_log(L_ERROR, "Could not initialize EGL"); wlr_log(L_ERROR, "Could not initialize EGL");
goto error_egl; goto error_egl;
} }

View File

@ -9,6 +9,7 @@
#include <wayland-client.h> #include <wayland-client.h>
#include <wlr/interfaces/wlr_output.h> #include <wlr/interfaces/wlr_output.h>
#include <wlr/render/wlr_renderer.h> #include <wlr/render/wlr_renderer.h>
#include <wlr/types/wlr_matrix.h>
#include <wlr/util/log.h> #include <wlr/util/log.h>
#include "backend/wayland.h" #include "backend/wayland.h"
#include "util/signal.h" #include "util/signal.h"
@ -72,8 +73,8 @@ static void output_transform(struct wlr_output *_output,
} }
static bool output_set_cursor(struct wlr_output *_output, static bool output_set_cursor(struct wlr_output *_output,
const uint8_t *buf, int32_t stride, uint32_t width, uint32_t height, struct wlr_texture *texture, int32_t hotspot_x, int32_t hotspot_y,
int32_t hotspot_x, int32_t hotspot_y, bool update_pixels) { bool update_texture) {
struct wlr_wl_output *output = struct wlr_wl_output *output =
(struct wlr_wl_output *)_output; (struct wlr_wl_output *)_output;
struct wlr_wl_backend *backend = output->backend; struct wlr_wl_backend *backend = output->backend;
@ -82,77 +83,48 @@ static bool output_set_cursor(struct wlr_output *_output,
output->cursor.hotspot_x = hotspot_x; output->cursor.hotspot_x = hotspot_x;
output->cursor.hotspot_y = hotspot_y; output->cursor.hotspot_y = hotspot_y;
if (!update_pixels) { if (!update_texture) {
// Update hotspot without changing cursor image // Update hotspot without changing cursor image
update_wl_output_cursor(output); update_wl_output_cursor(output);
return true; return true;
} }
if (!buf) {
// Hide cursor
if (output->cursor.surface) {
wl_surface_destroy(output->cursor.surface);
munmap(output->cursor.data, output->cursor.buf_size);
output->cursor.surface = NULL;
output->cursor.buf_size = 0;
}
update_wl_output_cursor(output);
return true;
}
if (!backend->shm || !backend->pointer) { if (output->cursor.surface == NULL) {
wlr_log(L_INFO, "cannot set cursor: no wl_shm or wl_pointer");
return false;
}
if (!output->cursor.surface) {
output->cursor.surface = output->cursor.surface =
wl_compositor_create_surface(output->backend->compositor); wl_compositor_create_surface(backend->compositor);
} }
struct wl_surface *surface = output->cursor.surface;
uint32_t size = stride * height; if (texture != NULL) {
if (output->cursor.buf_size != size) { int width, height;
if (output->cursor.buffer) { wlr_texture_get_size(texture, &width, &height);
wl_buffer_destroy(output->cursor.buffer);
if (output->cursor.egl_window == NULL) {
output->cursor.egl_window =
wl_egl_window_create(surface, width, height);
} }
wl_egl_window_resize(output->cursor.egl_window, width, height, 0, 0);
if (size > output->cursor.buf_size) { EGLSurface egl_surface =
if (output->cursor.pool) { wlr_egl_create_surface(&backend->egl, output->cursor.egl_window);
wl_shm_pool_destroy(output->cursor.pool);
output->cursor.pool = NULL;
munmap(output->cursor.data, output->cursor.buf_size);
}
}
if (!output->cursor.pool) { wlr_egl_make_current(&backend->egl, egl_surface, NULL);
int fd = os_create_anonymous_file(size);
if (fd < 0) {
wlr_log_errno(L_INFO,
"creating anonymous file for cursor buffer failed");
return false;
}
output->cursor.data = mmap(NULL, size, PROT_READ | PROT_WRITE, float matrix[9];
MAP_SHARED, fd, 0); wlr_matrix_projection(matrix, width, height, WL_OUTPUT_TRANSFORM_NORMAL);
if (output->cursor.data == MAP_FAILED) {
close(fd);
wlr_log_errno(L_INFO, "mmap failed");
return false;
}
output->cursor.pool = wl_shm_create_pool(backend->shm, fd, size); wlr_renderer_begin(backend->renderer, width, height);
close(fd); wlr_renderer_clear(backend->renderer, (float[]){ 0.0, 0.0, 0.0, 0.0 });
} wlr_render_texture(backend->renderer, texture, matrix, 0, 0, 1.0);
wlr_renderer_end(backend->renderer);
output->cursor.buffer = wl_shm_pool_create_buffer(output->cursor.pool, wlr_egl_swap_buffers(&backend->egl, egl_surface, NULL);
0, width, height, stride, WL_SHM_FORMAT_ARGB8888); wlr_egl_destroy_surface(&backend->egl, egl_surface);
output->cursor.buf_size = size; } else {
wl_surface_attach(surface, NULL, 0, 0);
wl_surface_commit(surface);
} }
memcpy(output->cursor.data, buf, size);
wl_surface_attach(output->cursor.surface, output->cursor.buffer, 0, 0);
wl_surface_damage(output->cursor.surface, 0, 0, width, height);
wl_surface_commit(output->cursor.surface);
update_wl_output_cursor(output); update_wl_output_cursor(output);
return true; return true;
} }
@ -166,16 +138,9 @@ static void output_destroy(struct wlr_output *wlr_output) {
wl_list_remove(&output->link); wl_list_remove(&output->link);
if (output->cursor.buf_size != 0) { if (output->cursor.egl_window != NULL) {
assert(output->cursor.data); wl_egl_window_destroy(output->cursor.egl_window);
assert(output->cursor.buffer);
assert(output->cursor.pool);
wl_buffer_destroy(output->cursor.buffer);
munmap(output->cursor.data, output->cursor.buf_size);
wl_shm_pool_destroy(output->cursor.pool);
} }
if (output->cursor.surface) { if (output->cursor.surface) {
wl_surface_destroy(output->cursor.surface); wl_surface_destroy(output->cursor.surface);
} }

View File

@ -40,25 +40,22 @@ struct wlr_wl_output {
struct wlr_output wlr_output; struct wlr_output wlr_output;
struct wlr_wl_backend *backend; struct wlr_wl_backend *backend;
struct wl_list link;
struct wl_surface *surface; struct wl_surface *surface;
struct wl_callback *frame_callback;
struct zxdg_surface_v6 *xdg_surface; struct zxdg_surface_v6 *xdg_surface;
struct zxdg_toplevel_v6 *xdg_toplevel; struct zxdg_toplevel_v6 *xdg_toplevel;
struct wl_egl_window *egl_window; struct wl_egl_window *egl_window;
struct wl_callback *frame_callback; EGLSurface egl_surface;
struct {
struct wl_shm_pool *pool;
void *buffer; // actually a (client-side) struct wl_buffer *
uint32_t buf_size;
uint8_t *data;
struct wl_surface *surface;
int32_t hotspot_x, hotspot_y;
} cursor;
uint32_t enter_serial; uint32_t enter_serial;
void *egl_surface; struct {
struct wl_list link; struct wl_surface *surface;
struct wl_egl_window *egl_window;
int32_t hotspot_x, hotspot_y;
} cursor;
}; };
struct wlr_wl_input_device { struct wlr_wl_input_device {

View File

@ -13,9 +13,8 @@ struct wlr_output_impl {
int32_t height, int32_t refresh); int32_t height, int32_t refresh);
void (*transform)(struct wlr_output *output, void (*transform)(struct wlr_output *output,
enum wl_output_transform transform); enum wl_output_transform transform);
bool (*set_cursor)(struct wlr_output *output, const uint8_t *buf, bool (*set_cursor)(struct wlr_output *output, struct wlr_texture *texture,
int32_t stride, uint32_t width, uint32_t height, int32_t hotspot_x, int32_t hotspot_y, bool update_texture);
int32_t hotspot_x, int32_t hotspot_y, bool update_pixels);
bool (*move_cursor)(struct wlr_output *output, int x, int y); bool (*move_cursor)(struct wlr_output *output, int x, int y);
void (*destroy)(struct wlr_output *output); void (*destroy)(struct wlr_output *output);
bool (*make_current)(struct wlr_output *output, int *buffer_age); bool (*make_current)(struct wlr_output *output, int *buffer_age);

View File

@ -698,6 +698,27 @@ static void output_cursor_update_visible(struct wlr_output_cursor *cursor) {
cursor->visible = visible; cursor->visible = visible;
} }
static bool output_cursor_attempt_hardware(struct wlr_output_cursor *cursor) {
struct wlr_texture *texture = cursor->texture;
if (cursor->surface != NULL) {
texture = cursor->surface->texture;
}
struct wlr_output_cursor *hwcur = cursor->output->hardware_cursor;
if (cursor->output->impl->set_cursor && (hwcur == NULL || hwcur == cursor)) {
if (cursor->output->impl->move_cursor && hwcur != cursor) {
cursor->output->impl->move_cursor(cursor->output,
(int)cursor->x, (int)cursor->y);
}
if (cursor->output->impl->set_cursor(cursor->output, texture,
cursor->hotspot_x, cursor->hotspot_y, true)) {
cursor->output->hardware_cursor = cursor;
return true;
}
}
return false;
}
bool wlr_output_cursor_set_image(struct wlr_output_cursor *cursor, bool wlr_output_cursor_set_image(struct wlr_output_cursor *cursor,
const uint8_t *pixels, int32_t stride, uint32_t width, uint32_t height, const uint8_t *pixels, int32_t stride, uint32_t width, uint32_t height,
int32_t hotspot_x, int32_t hotspot_y) { int32_t hotspot_x, int32_t hotspot_y) {
@ -713,33 +734,26 @@ bool wlr_output_cursor_set_image(struct wlr_output_cursor *cursor,
cursor->hotspot_y = hotspot_y; cursor->hotspot_y = hotspot_y;
output_cursor_update_visible(cursor); output_cursor_update_visible(cursor);
struct wlr_output_cursor *hwcur = cursor->output->hardware_cursor; wlr_texture_destroy(cursor->texture);
if (cursor->output->impl->set_cursor && (hwcur == NULL || hwcur == cursor)) { cursor->texture = NULL;
if (cursor->output->impl->move_cursor && hwcur != cursor) {
cursor->output->impl->move_cursor(cursor->output, cursor->enabled = false;
(int)cursor->x, (int)cursor->y); if (pixels != NULL) {
} cursor->texture = wlr_texture_from_pixels(renderer,
int ok = cursor->output->impl->set_cursor(cursor->output, pixels, WL_SHM_FORMAT_ARGB8888, stride, width, height, pixels);
stride, width, height, hotspot_x, hotspot_y, true); if (cursor->texture == NULL) {
if (ok) { return false;
cursor->output->hardware_cursor = cursor;
return true;
} }
cursor->enabled = true;
}
if (output_cursor_attempt_hardware(cursor)) {
return true;
} }
wlr_log(L_DEBUG, "Falling back to software cursor"); wlr_log(L_DEBUG, "Falling back to software cursor");
output_cursor_damage_whole(cursor); output_cursor_damage_whole(cursor);
return true;
cursor->enabled = pixels != NULL;
if (!cursor->enabled) {
return true;
}
wlr_texture_destroy(cursor->texture);
cursor->texture = wlr_texture_from_pixels(renderer, WL_SHM_FORMAT_ARGB8888,
stride, width, height, pixels);
return cursor->texture != NULL;
} }
static void output_cursor_commit(struct wlr_output_cursor *cursor) { static void output_cursor_commit(struct wlr_output_cursor *cursor) {
@ -752,15 +766,15 @@ static void output_cursor_commit(struct wlr_output_cursor *cursor) {
cursor->width = cursor->surface->current->width * cursor->output->scale; cursor->width = cursor->surface->current->width * cursor->output->scale;
cursor->height = cursor->surface->current->height * cursor->output->scale; cursor->height = cursor->surface->current->height * cursor->output->scale;
if (cursor->output->hardware_cursor != cursor) { if (output_cursor_attempt_hardware(cursor)) {
output_cursor_damage_whole(cursor);
} else {
// TODO: upload pixels
struct timespec now; struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now); clock_gettime(CLOCK_MONOTONIC, &now);
wlr_surface_send_frame_done(cursor->surface, &now); wlr_surface_send_frame_done(cursor->surface, &now);
return;
} }
// Fallback to software cursor
output_cursor_damage_whole(cursor);
} }
static void output_cursor_handle_commit(struct wl_listener *listener, static void output_cursor_handle_commit(struct wl_listener *listener,
@ -800,7 +814,7 @@ void wlr_output_cursor_set_surface(struct wlr_output_cursor *cursor,
if (cursor->output->hardware_cursor == cursor && if (cursor->output->hardware_cursor == cursor &&
cursor->output->impl->set_cursor) { cursor->output->impl->set_cursor) {
cursor->output->impl->set_cursor(cursor->output, NULL, 0, 0, 0, cursor->output->impl->set_cursor(cursor->output, NULL,
hotspot_x, hotspot_y, false); hotspot_x, hotspot_y, false);
} }
return; return;
@ -808,15 +822,6 @@ void wlr_output_cursor_set_surface(struct wlr_output_cursor *cursor,
output_cursor_reset(cursor); output_cursor_reset(cursor);
// Disable hardware cursor for surfaces
// TODO: support hardware cursors
if (cursor->output->hardware_cursor == cursor &&
cursor->output->impl->set_cursor) {
cursor->output->impl->set_cursor(cursor->output, NULL, 0, 0, 0, 0, 0,
true);
cursor->output->hardware_cursor = NULL;
}
cursor->surface = surface; cursor->surface = surface;
cursor->hotspot_x = hotspot_x; cursor->hotspot_x = hotspot_x;
cursor->hotspot_y = hotspot_y; cursor->hotspot_y = hotspot_y;
@ -833,7 +838,10 @@ void wlr_output_cursor_set_surface(struct wlr_output_cursor *cursor,
cursor->width = 0; cursor->width = 0;
cursor->height = 0; cursor->height = 0;
// TODO: if hardware cursor, disable cursor if (cursor->output->hardware_cursor == cursor &&
cursor->output->impl->set_cursor) {
cursor->output->impl->set_cursor(cursor->output, NULL, 0, 0, false);
}
} }
} }
@ -896,8 +904,7 @@ void wlr_output_cursor_destroy(struct wlr_output_cursor *cursor) {
if (cursor->output->hardware_cursor == cursor) { if (cursor->output->hardware_cursor == cursor) {
// If this cursor was the hardware cursor, disable it // If this cursor was the hardware cursor, disable it
if (cursor->output->impl->set_cursor) { if (cursor->output->impl->set_cursor) {
cursor->output->impl->set_cursor(cursor->output, NULL, 0, 0, 0, 0, cursor->output->impl->set_cursor(cursor->output, NULL, 0, 0, true);
0, true);
} }
cursor->output->hardware_cursor = NULL; cursor->output->hardware_cursor = NULL;
} }