From 0cd94f0cf0dd104cee9837dc2961536a078d089e Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Mon, 26 Jun 2017 17:34:15 +1200 Subject: [PATCH] Added software cursor fallback --- backend/drm/drm.c | 21 ++++++----- example/pointer.c | 36 ++++++++----------- example/rotation.c | 4 ++- example/simple.c | 4 +++ example/tablet.c | 2 ++ example/touch.c | 5 ++- include/wlr/interfaces/wlr_output.h | 2 ++ include/wlr/render.h | 2 +- include/wlr/render/interface.h | 2 +- include/wlr/types/wlr_output.h | 10 ++++++ render/CMakeLists.txt | 4 +-- render/gles2/surface.c | 5 ++- render/wlr_surface.c | 4 +-- types/CMakeLists.txt | 1 + types/wlr_output.c | 55 ++++++++++++++++++++++++++--- 15 files changed, 115 insertions(+), 42 deletions(-) diff --git a/backend/drm/drm.c b/backend/drm/drm.c index c6e29b867..2f64066e1 100644 --- a/backend/drm/drm.c +++ b/backend/drm/drm.c @@ -92,13 +92,13 @@ static uint32_t get_fb_for_bo(int fd, struct gbm_bo *bo) { return *id; } -static void wlr_drm_output_begin(struct wlr_output_state *output) { +static void wlr_drm_output_make_current(struct wlr_output_state *output) { struct wlr_drm_renderer *renderer = output->renderer; eglMakeCurrent(renderer->egl.display, output->egl, output->egl, renderer->egl.context); } -static void wlr_drm_output_end(struct wlr_output_state *output) { +static void wlr_drm_output_swap_buffers(struct wlr_output_state *output) { struct wlr_drm_renderer *renderer = output->renderer; if (!eglSwapBuffers(renderer->egl.display, output->egl)) { @@ -207,10 +207,10 @@ static void wlr_drm_output_enable(struct wlr_output_state *output, bool enable) DRM_MODE_DPMS_ON); // Start rendering loop again by drawing a black frame - wlr_drm_output_begin(output); + wlr_drm_output_make_current(output); glClearColor(0.0, 0.0, 0.0, 1.0); glClear(GL_COLOR_BUFFER_BIT); - wlr_drm_output_end(output); + wlr_drm_output_swap_buffers(output); } else { drmModeConnectorSetProperty(state->fd, output->connector, output->props.dpms, DRM_MODE_DPMS_STANDBY); @@ -342,8 +342,13 @@ static bool wlr_drm_output_set_cursor(struct wlr_output_state *output, wlr_log(L_ERROR, "Failed to write cursor to bo"); return false; } - return !drmModeSetCursor(state->fd, output->crtc, - gbm_bo_get_handle(bo).s32, width, height); + + if (drmModeSetCursor(state->fd, output->crtc, gbm_bo_get_handle(bo).u32, width, height)) { + wlr_log_errno(L_INFO, "Failed to set hardware cursor"); + return false; + } + + return true; } static bool wlr_drm_output_move_cursor(struct wlr_output_state *output, @@ -365,6 +370,8 @@ static struct wlr_output_impl output_impl = { .set_cursor = wlr_drm_output_set_cursor, .move_cursor = wlr_drm_output_move_cursor, .destroy = wlr_drm_output_destroy, + .make_current = wlr_drm_output_make_current, + .swap_buffers = wlr_drm_output_swap_buffers, }; static int32_t calculate_refresh_rate(drmModeModeInfo *mode) { @@ -625,9 +632,7 @@ static void page_flip_handler(int fd, unsigned seq, output->pageflip_pending = false; if (output->state == DRM_OUTPUT_CONNECTED && state->session->active) { - wlr_drm_output_begin(output); wl_signal_emit(&output->wlr_output->events.frame, output->wlr_output); - wlr_drm_output_end(output); } } diff --git a/example/pointer.c b/example/pointer.c index 8dba54548..c75188416 100644 --- a/example/pointer.c +++ b/example/pointer.c @@ -21,9 +21,7 @@ #include "cat.h" struct sample_state { - struct wlr_renderer *renderer; - struct wlr_surface *cat_texture; - int cur_x, cur_y; + double cur_x, cur_y; float default_color[4]; float clear_color[4]; }; @@ -33,18 +31,13 @@ static void handle_output_frame(struct output_state *output, struct timespec *ts struct sample_state *sample = state->data; struct wlr_output *wlr_output = output->output; - wlr_renderer_begin(sample->renderer, wlr_output); + wlr_output_make_current(wlr_output); + glClearColor(sample->clear_color[0], sample->clear_color[1], sample->clear_color[2], sample->clear_color[3]); glClear(GL_COLOR_BUFFER_BIT); - float matrix[16]; - wlr_surface_get_matrix(sample->cat_texture, &matrix, - &wlr_output->transform_matrix, sample->cur_x, sample->cur_y); - wlr_render_with_matrix(sample->renderer, - sample->cat_texture, &matrix); - - wlr_renderer_end(sample->renderer); + wlr_output_swap_buffers(wlr_output); } static void handle_pointer_motion(struct pointer_state *pstate, @@ -52,6 +45,11 @@ static void handle_pointer_motion(struct pointer_state *pstate, struct sample_state *state = pstate->compositor->data; state->cur_x += d_x; state->cur_y += d_y; + + struct output_state *output; + wl_list_for_each(output, &pstate->compositor->outputs, link) { + wlr_output_move_cursor(output->output, state->cur_x, state->cur_y); + } } static void handle_pointer_motion_absolute(struct pointer_state *pstate, @@ -59,6 +57,11 @@ static void handle_pointer_motion_absolute(struct pointer_state *pstate, struct sample_state *state = pstate->compositor->data; state->cur_x = x; state->cur_y = y; + + struct output_state *output; + wl_list_for_each(output, &pstate->compositor->outputs, link) { + wlr_output_move_cursor(output->output, state->cur_x, state->cur_y); + } } static void handle_pointer_button(struct pointer_state *pstate, @@ -95,9 +98,8 @@ static void handle_pointer_axis(struct pointer_state *pstate, static void handle_output_add(struct output_state *ostate) { struct wlr_output *wlr_output = ostate->output; - int width = 16, height = 16; if (!wlr_output_set_cursor(wlr_output, cat_tex.pixel_data, - width * 4, width, height)) { + cat_tex.width, cat_tex.width, cat_tex.height)) { wlr_log(L_DEBUG, "Failed to set hardware cursor"); return; } @@ -121,13 +123,5 @@ int main(int argc, char *argv[]) { compositor.pointer_axis_cb = handle_pointer_axis; compositor_init(&compositor); - state.renderer = wlr_gles2_renderer_init(); - state.cat_texture = wlr_render_surface_init(state.renderer); - wlr_surface_attach_pixels(state.cat_texture, GL_RGBA, - cat_tex.width, cat_tex.height, cat_tex.pixel_data); - compositor_run(&compositor); - - wlr_surface_destroy(state.cat_texture); - wlr_renderer_destroy(state.renderer); } diff --git a/example/rotation.c b/example/rotation.c index b8c43d4ca..2dd148d01 100644 --- a/example/rotation.c +++ b/example/rotation.c @@ -46,6 +46,7 @@ static void handle_output_frame(struct output_state *output, struct timespec *ts int32_t width, height; wlr_output_effective_resolution(wlr_output, &width, &height); + wlr_output_make_current(wlr_output); wlr_renderer_begin(sample->renderer, wlr_output); float matrix[16]; @@ -59,6 +60,7 @@ static void handle_output_frame(struct output_state *output, struct timespec *ts } wlr_renderer_end(sample->renderer); + wlr_output_swap_buffers(wlr_output); long ms = (ts->tv_sec - output->last_frame.tv_sec) * 1000 + (ts->tv_nsec - output->last_frame.tv_nsec) / 1000000; @@ -205,7 +207,7 @@ int main(int argc, char *argv[]) { state.renderer = wlr_gles2_renderer_init(); state.cat_texture = wlr_render_surface_init(state.renderer); wlr_surface_attach_pixels(state.cat_texture, GL_RGBA, - cat_tex.width, cat_tex.height, cat_tex.pixel_data); + cat_tex.width, cat_tex.width, cat_tex.height, cat_tex.pixel_data); compositor_run(&compositor); diff --git a/example/simple.c b/example/simple.c index 4bbdf3993..45cb2b939 100644 --- a/example/simple.c +++ b/example/simple.c @@ -34,8 +34,12 @@ void handle_output_frame(struct output_state *output, struct timespec *ts) { sample->dec = inc; } + wlr_output_make_current(output->output); + glClearColor(sample->color[0], sample->color[1], sample->color[2], 1.0); glClear(GL_COLOR_BUFFER_BIT); + + wlr_output_swap_buffers(output->output); } int main() { diff --git a/example/tablet.c b/example/tablet.c index 0343bd0a5..d7f7d042d 100644 --- a/example/tablet.c +++ b/example/tablet.c @@ -41,6 +41,7 @@ static void handle_output_frame(struct output_state *output, struct timespec *ts int32_t width, height; wlr_output_effective_resolution(wlr_output, &width, &height); + wlr_output_make_current(wlr_output); wlr_renderer_begin(sample->renderer, wlr_output); float matrix[16], view[16]; @@ -74,6 +75,7 @@ static void handle_output_frame(struct output_state *output, struct timespec *ts } wlr_renderer_end(sample->renderer); + wlr_output_swap_buffers(wlr_output); } static void handle_tool_axis(struct tablet_tool_state *tstate, diff --git a/example/touch.c b/example/touch.c index 29380ed4f..aca86739d 100644 --- a/example/touch.c +++ b/example/touch.c @@ -38,6 +38,8 @@ static void handle_output_frame(struct output_state *output, struct timespec *ts int32_t width, height; wlr_output_effective_resolution(wlr_output, &width, &height); + + wlr_output_make_current(wlr_output); wlr_renderer_begin(sample->renderer, wlr_output); float matrix[16]; @@ -52,6 +54,7 @@ static void handle_output_frame(struct output_state *output, struct timespec *ts } wlr_renderer_end(sample->renderer); + wlr_output_swap_buffers(wlr_output); } static void handle_touch_down(struct touch_state *tstate, int32_t slot, @@ -104,7 +107,7 @@ int main(int argc, char *argv[]) { state.renderer = wlr_gles2_renderer_init(); state.cat_texture = wlr_render_surface_init(state.renderer); wlr_surface_attach_pixels(state.cat_texture, GL_RGBA, - cat_tex.width, cat_tex.height, cat_tex.pixel_data); + cat_tex.width, cat_tex.width, cat_tex.height, cat_tex.pixel_data); compositor_run(&compositor); diff --git a/include/wlr/interfaces/wlr_output.h b/include/wlr/interfaces/wlr_output.h index 0949bca38..5f9bfdd2a 100644 --- a/include/wlr/interfaces/wlr_output.h +++ b/include/wlr/interfaces/wlr_output.h @@ -13,6 +13,8 @@ struct wlr_output_impl { const uint8_t *buf, int32_t stride, uint32_t width, uint32_t height); bool (*move_cursor)(struct wlr_output_state *state, int x, int y); void (*destroy)(struct wlr_output_state *state); + void (*make_current)(struct wlr_output_state *state); + void (*swap_buffers)(struct wlr_output_state *state); }; struct wlr_output *wlr_output_create(struct wlr_output_impl *impl, diff --git a/include/wlr/render.h b/include/wlr/render.h index 90967dd9a..53df01d27 100644 --- a/include/wlr/render.h +++ b/include/wlr/render.h @@ -59,7 +59,7 @@ struct wlr_surface { * calling this function. */ bool wlr_surface_attach_pixels(struct wlr_surface *surf, uint32_t format, - int width, int height, const unsigned char *pixels); + int stride, int width, int height, const unsigned char *pixels); /** * Attaches pixels from a wl_shm_buffer to this surface. The shm buffer may be * invalidated after calling this function. diff --git a/include/wlr/render/interface.h b/include/wlr/render/interface.h index 222b0c4d1..a9c86f2c8 100644 --- a/include/wlr/render/interface.h +++ b/include/wlr/render/interface.h @@ -31,7 +31,7 @@ struct wlr_renderer *wlr_renderer_init(struct wlr_renderer_state *state, struct wlr_surface_impl { bool (*attach_pixels)(struct wlr_surface_state *state, uint32_t format, - int width, int height, const unsigned char *pixels); + int stride, int width, int height, const unsigned char *pixels); bool (*attach_shm)(struct wlr_surface_state *state, uint32_t format, struct wl_shm_buffer *shm); // TODO: egl diff --git a/include/wlr/types/wlr_output.h b/include/wlr/types/wlr_output.h index 71e1d0fec..da5a7b070 100644 --- a/include/wlr/types/wlr_output.h +++ b/include/wlr/types/wlr_output.h @@ -42,6 +42,14 @@ struct wlr_output { struct wl_signal frame; struct wl_signal resolution; } events; + + struct { + bool is_sw; + int32_t x, y; + uint32_t width, height; + struct wlr_renderer *renderer; + struct wlr_surface *texture; + } cursor; }; void wlr_output_enable(struct wlr_output *output, bool enable); @@ -55,5 +63,7 @@ bool wlr_output_move_cursor(struct wlr_output *output, int x, int y); void wlr_output_destroy(struct wlr_output *output); void wlr_output_effective_resolution(struct wlr_output *output, int *width, int *height); +void wlr_output_make_current(struct wlr_output *output); +void wlr_output_swap_buffers(struct wlr_output *output); #endif diff --git a/render/CMakeLists.txt b/render/CMakeLists.txt index 70ebced2a..4058b50ee 100644 --- a/render/CMakeLists.txt +++ b/render/CMakeLists.txt @@ -1,5 +1,5 @@ -add_library(wlr-render STATIC - matrix.c +add_library(wlr-render + matrix.c wlr_renderer.c wlr_surface.c gles2/shaders.c diff --git a/render/gles2/surface.c b/render/gles2/surface.c index 278b3dc32..c9be6ac2e 100644 --- a/render/gles2/surface.c +++ b/render/gles2/surface.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -10,15 +11,17 @@ #include "render/gles2.h" static bool gles2_surface_attach_pixels(struct wlr_surface_state *surface, - uint32_t format, int width, int height, const unsigned char *pixels) { + uint32_t format, int stride, int width, int height, const unsigned char *pixels) { assert(surface); surface->wlr_surface->width = width; surface->wlr_surface->height = height; surface->wlr_surface->format = format; GL_CALL(glGenTextures(1, &surface->tex_id)); GL_CALL(glBindTexture(GL_TEXTURE_2D, surface->tex_id)); + GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, stride)); GL_CALL(glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, pixels)); + GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, 0)); surface->wlr_surface->valid = true; return true; } diff --git a/render/wlr_surface.c b/render/wlr_surface.c index 46a122056..4e4f41011 100644 --- a/render/wlr_surface.c +++ b/render/wlr_surface.c @@ -20,9 +20,9 @@ void wlr_surface_bind(struct wlr_surface *surface) { } bool wlr_surface_attach_pixels(struct wlr_surface *surface, uint32_t format, - int width, int height, const unsigned char *pixels) { + int stride, int width, int height, const unsigned char *pixels) { return surface->impl->attach_pixels(surface->state, - format, width, height, pixels); + format, stride, width, height, pixels); } bool wlr_surface_attach_shm(struct wlr_surface *surface, uint32_t format, diff --git a/types/CMakeLists.txt b/types/CMakeLists.txt index 72d885c91..648db8205 100644 --- a/types/CMakeLists.txt +++ b/types/CMakeLists.txt @@ -15,5 +15,6 @@ add_library(wlr-types target_link_libraries(wlr-types wlr-util + wlr-render ${WAYLAND_LIBRARIES} ) diff --git a/types/wlr_output.c b/types/wlr_output.c index 4d2e77786..b64fd827d 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -7,6 +7,10 @@ #include #include #include +#include +#include +#include +#include static void wl_output_send_to_resource(struct wl_resource *resource) { assert(resource); @@ -182,16 +186,44 @@ void wlr_output_transform(struct wlr_output *output, bool wlr_output_set_cursor(struct wlr_output *output, const uint8_t *buf, int32_t stride, uint32_t width, uint32_t height) { - if (!output->impl || !output->impl->set_cursor) { - return false; + if (output->impl->set_cursor && output->impl->set_cursor(output->state, buf, + stride, width, height)) { + output->cursor.is_sw = false; + return true; } - return output->impl->set_cursor(output->state, buf, stride, width, height); + + wlr_log(L_INFO, "Falling back to software cursor"); + + output->cursor.is_sw = true; + output->cursor.width = width; + output->cursor.height = height; + + if (!output->cursor.renderer) { + output->cursor.renderer = wlr_gles2_renderer_init(); + } + + if (!output->cursor.texture) { + output->cursor.texture = wlr_render_surface_init(output->cursor.renderer); + } + + wlr_surface_attach_pixels(output->cursor.texture, GL_RGBA, + stride, width, height, buf); + + return true; } bool wlr_output_move_cursor(struct wlr_output *output, int x, int y) { - if (!output->impl || !output->impl->move_cursor) { + output->cursor.x = x; + output->cursor.y = y; + + if (output->cursor.is_sw) { + return true; + } + + if (!output->impl->move_cursor) { return false; } + return output->impl->move_cursor(output->state, x, y); } @@ -216,3 +248,18 @@ void wlr_output_effective_resolution(struct wlr_output *output, *height = output->height; } } + +void wlr_output_make_current(struct wlr_output *output) { + output->impl->make_current(output->state); +} + +void wlr_output_swap_buffers(struct wlr_output *output) { + if (output->cursor.is_sw) { + float matrix[16]; + wlr_surface_get_matrix(output->cursor.texture, &matrix, &output->transform_matrix, + output->cursor.x, output->cursor.y); + wlr_render_with_matrix(output->cursor.renderer, output->cursor.texture, &matrix); + } + + output->impl->swap_buffers(output->state); +}