Merge pull request #23 from ascent12/cursor

Software cursor + Hardware cursor improvements
This commit is contained in:
Drew DeVault 2017-06-26 18:23:52 -04:00 committed by GitHub
commit ce471f3371
16 changed files with 174 additions and 88 deletions

View File

@ -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);
@ -296,54 +296,65 @@ static void wlr_drm_output_transform(struct wlr_output_state *output,
output->wlr_output->transform = transform;
}
static void wlr_drm_cursor_bo_update(struct wlr_output_state *output,
uint32_t width, uint32_t height) {
if (output->cursor_width == width && output->cursor_height == height) {
return;
}
wlr_log(L_DEBUG, "Allocating new cursor bos");
struct wlr_backend_state *state =
wl_container_of(output->renderer, state, renderer);
for (size_t i = 0; i < 2; ++i) {
output->cursor_bo[i] = gbm_bo_create(state->renderer.gbm,
width, height, GBM_FORMAT_ARGB8888,
GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE);
if (!output->cursor_bo[i]) {
wlr_log(L_ERROR, "Failed to create cursor bo");
return;
}
if (!get_fb_for_bo(state->fd, output->cursor_bo[i])) {
wlr_log(L_ERROR, "Failed to create cursor fb");
return;
}
}
}
static bool wlr_drm_output_set_cursor(struct wlr_output_state *output,
const uint8_t *buf, int32_t stride, uint32_t width, uint32_t height) {
struct wlr_backend_state *state =
wl_container_of(output->renderer, state, renderer);
struct wlr_backend_state *state = wl_container_of(output->renderer, state, renderer);
if (!buf) {
drmModeSetCursor(state->fd, output->crtc, 0, 0, 0);
return true;
}
wlr_drm_cursor_bo_update(output, width, height);
uint64_t bo_width, bo_height;
int ret;
ret = drmGetCap(state->fd, DRM_CAP_CURSOR_WIDTH, &bo_width);
bo_width = ret ? 64 : bo_width;
ret = drmGetCap(state->fd, DRM_CAP_CURSOR_HEIGHT, &bo_height);
bo_height = ret ? 64 : bo_height;
if (width > bo_width || height > bo_width) {
wlr_log(L_INFO, "Cursor too large (max %dx%d)", (int)bo_width, (int)bo_height);
return false;
}
for (int i = 0; i < 2; ++i) {
if (output->cursor_bo[i]) {
continue;
}
output->cursor_bo[i] = gbm_bo_create(state->renderer.gbm, bo_width, bo_height,
GBM_FORMAT_RGBA8888, GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE);
if (!output->cursor_bo[i]) {
wlr_log(L_ERROR, "Failed to create cursor bo");
return false;
}
}
struct gbm_bo *bo;
output->current_cursor ^= 1;
bo = output->cursor_bo[output->current_cursor];
uint32_t _buf[width * height];
memset(_buf, 0, sizeof(_buf));
uint32_t bo_stride = gbm_bo_get_stride(bo);
uint8_t tmp[bo_stride * height];
memset(tmp, 0, sizeof(tmp));
for (size_t i = 0; i < height; ++i) {
memcpy(_buf + i * width,
buf + i * stride,
width * 4);
memcpy(tmp + i * bo_stride, buf + i * stride * 4, width * 4);
}
if (gbm_bo_write(bo, _buf, sizeof(_buf)) < 0) {
if (gbm_bo_write(bo, tmp, sizeof(tmp)) < 0) {
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);
uint32_t bo_handle = gbm_bo_get_handle(bo).u32;
if (drmModeSetCursor(state->fd, output->crtc, bo_handle, bo_width, bo_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 +376,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 +638,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);
}
}

View File

@ -15,29 +15,34 @@ static void surface_frame_callback(void *data, struct wl_callback *cb, uint32_t
struct wlr_output_state *output = data;
assert(output);
if (!eglMakeCurrent(output->backend->egl.display,
output->egl_surface, output->egl_surface,
output->backend->egl.context)) {
wlr_log(L_ERROR, "eglMakeCurrent failed: %s", egl_error());
return;
}
wlr_log(L_DEBUG, "frame_callback");
struct wlr_output *wlr_output = output->wlr_output;
wl_signal_emit(&wlr_output->events.frame, wlr_output);
output->frame_callback = wl_surface_frame(output->surface);
wl_callback_add_listener(output->frame_callback, &frame_listener, output);
wl_callback_destroy(cb);
if (!eglSwapBuffers(output->backend->egl.display, output->egl_surface)) {
wlr_log(L_ERROR, "eglSwapBuffers failed: %s", egl_error());
return;
}
}
static struct wl_callback_listener frame_listener = {
.done = surface_frame_callback
};
static void wlr_wl_output_make_current(struct wlr_output_state *output) {
wlr_log(L_DEBUG, "make_current");
if (!eglMakeCurrent(output->backend->egl.display,
output->egl_surface, output->egl_surface,
output->backend->egl.context)) {
wlr_log(L_ERROR, "eglMakeCurrent failed: %s", egl_error());
}
}
static void wlr_wl_output_swap_buffers(struct wlr_output_state *output) {
wlr_log(L_DEBUG, "swap_buffers");
output->frame_callback = wl_surface_frame(output->surface);
wl_callback_add_listener(output->frame_callback, &frame_listener, output);
if (!eglSwapBuffers(output->backend->egl.display, output->egl_surface)) {
wlr_log(L_ERROR, "eglSwapBuffers failed: %s", egl_error());
}
}
static void wlr_wl_output_transform(struct wlr_output_state *output,
enum wl_output_transform transform) {
output->wlr_output->transform = transform;
@ -55,6 +60,8 @@ static void wlr_wl_output_destroy(struct wlr_output_state *output) {
static struct wlr_output_impl output_impl = {
.transform = wlr_wl_output_transform,
.destroy = wlr_wl_output_destroy,
.make_current = wlr_wl_output_make_current,
.swap_buffers = wlr_wl_output_swap_buffers,
};
void handle_ping(void* data, struct wl_shell_surface* ssurface, uint32_t serial) {

View File

@ -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);
}

View File

@ -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);

View File

@ -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() {

View File

@ -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,

View File

@ -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);

View File

@ -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,

View File

@ -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.

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -2,6 +2,7 @@
#include <stdlib.h>
#include <assert.h>
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include <wayland-util.h>
#include <wayland-server-protocol.h>
#include <wlr/render.h>
@ -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;
}

View File

@ -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,

View File

@ -15,5 +15,6 @@ add_library(wlr-types
target_link_libraries(wlr-types
wlr-util
wlr-render
${WAYLAND_LIBRARIES}
)

View File

@ -7,6 +7,10 @@
#include <wlr/interfaces/wlr_output.h>
#include <wlr/util/list.h>
#include <wlr/util/log.h>
#include <GLES2/gl2.h>
#include <wlr/render/matrix.h>
#include <wlr/render/gles2.h>
#include <wlr/render.h>
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);
}