Merge pull request #1366 from emersion/render-software-cursors

Render software cursors in compositor
This commit is contained in:
Drew DeVault 2018-11-13 14:47:32 -05:00 committed by GitHub
commit 040d62de00
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 152 additions and 280 deletions

View File

@ -106,11 +106,6 @@ struct wlr_output {
struct wl_event_source *idle_frame;
struct wlr_surface *fullscreen_surface;
struct wl_listener fullscreen_surface_commit;
struct wl_listener fullscreen_surface_destroy;
int fullscreen_width, fullscreen_height;
struct wl_list cursors; // wlr_output_cursor::link
struct wlr_output_cursor *hardware_cursor;
int software_cursor_locks; // number of locks forcing software cursors
@ -226,8 +221,6 @@ bool wlr_output_set_gamma(struct wlr_output *output, size_t size,
const uint16_t *r, const uint16_t *g, const uint16_t *b);
bool wlr_output_export_dmabuf(struct wlr_output *output,
struct wlr_dmabuf_attributes *attribs);
void wlr_output_set_fullscreen_surface(struct wlr_output *output,
struct wlr_surface *surface);
struct wlr_output *wlr_output_from_resource(struct wl_resource *resource);
/**
* Locks the output to only use software cursors instead of hardware cursors.
@ -237,6 +230,12 @@ struct wlr_output *wlr_output_from_resource(struct wl_resource *resource);
* a lock.
*/
void wlr_output_lock_software_cursors(struct wlr_output *output, bool lock);
/**
* Renders software cursors. This is a utility function that can be called when
* compositors render.
*/
void wlr_output_render_software_cursors(struct wlr_output *output,
pixman_region32_t *damage);
struct wlr_output_cursor *wlr_output_cursor_create(struct wlr_output *output);

View File

@ -1,11 +1,12 @@
#include <assert.h>
#include <stdio.h>
#include <drm_fourcc.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <stdio.h>
#include <stdlib.h>
#include <drm_fourcc.h>
#include <wlr/render/egl.h>
#include <wlr/util/log.h>
#include <wlr/util/region.h>
#include "glapi.h"
static bool egl_get_config(EGLDisplay disp, EGLint *attribs, EGLConfig *out,
@ -329,9 +330,18 @@ bool wlr_egl_swap_buffers(struct wlr_egl *egl, EGLSurface surface,
EGLBoolean ret;
if (damage != NULL && (egl->exts.swap_buffers_with_damage_ext ||
egl->exts.swap_buffers_with_damage_khr)) {
EGLint width = 0, height = 0;
eglQuerySurface(egl->display, surface, EGL_WIDTH, &width);
eglQuerySurface(egl->display, surface, EGL_HEIGHT, &height);
pixman_region32_t flipped_damage;
pixman_region32_init(&flipped_damage);
wlr_region_transform(&flipped_damage, damage,
WL_OUTPUT_TRANSFORM_FLIPPED_180, width, height);
int nrects;
pixman_box32_t *rects =
pixman_region32_rectangles(damage, &nrects);
pixman_region32_rectangles(&flipped_damage, &nrects);
EGLint egl_damage[4 * nrects];
for (int i = 0; i < nrects; ++i) {
egl_damage[4*i] = rects[i].x1;
@ -340,6 +350,8 @@ bool wlr_egl_swap_buffers(struct wlr_egl *egl, EGLSurface surface,
egl_damage[4*i + 3] = rects[i].y2 - rects[i].y1;
}
pixman_region32_fini(&flipped_damage);
if (egl->exts.swap_buffers_with_damage_ext) {
ret = eglSwapBuffersWithDamageEXT(egl->display, surface, egl_damage,
nrects);

View File

@ -157,10 +157,6 @@ static void output_for_each_surface(struct roots_output *output,
if (output->fullscreen_view != NULL) {
struct roots_view *view = output->fullscreen_view;
if (wlr_output->fullscreen_surface == view->wlr_surface) {
// The surface is managed by the wlr_output
return;
}
view_for_each_surface(view, layout_data, iterator, user_data);
@ -388,26 +384,6 @@ static void render_layer(struct roots_output *output,
&data->layout, data);
}
static bool has_standalone_surface(struct roots_view *view) {
if (!wl_list_empty(&view->wlr_surface->subsurfaces)) {
return false;
}
switch (view->type) {
case ROOTS_XDG_SHELL_V6_VIEW:
return wl_list_empty(&view->xdg_surface_v6->popups);
case ROOTS_XDG_SHELL_VIEW:
return wl_list_empty(&view->xdg_surface->popups);
case ROOTS_WL_SHELL_VIEW:
return wl_list_empty(&view->wl_shell_surface->popups);
#if WLR_HAS_XWAYLAND
case ROOTS_XWAYLAND_VIEW:
return wl_list_empty(&view->xwayland_surface->children);
#endif
}
return true;
}
static void surface_send_frame_done(struct wlr_surface *surface, int sx, int sy,
void *_data) {
struct render_data *data = _data;
@ -459,17 +435,8 @@ static void render_output(struct roots_output *output) {
output_box->y;
view_move(view, view_x, view_y);
if (has_standalone_surface(view) &&
wl_list_empty(&output->layers[ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY])) {
wlr_output_set_fullscreen_surface(wlr_output, view->wlr_surface);
} else {
wlr_output_set_fullscreen_surface(wlr_output, NULL);
}
// Fullscreen views are rendered on a black background
clear_color[0] = clear_color[1] = clear_color[2] = 0;
} else {
wlr_output_set_fullscreen_surface(wlr_output, NULL);
}
bool needs_swap;
@ -518,11 +485,6 @@ static void render_output(struct roots_output *output) {
if (output->fullscreen_view != NULL) {
struct roots_view *view = output->fullscreen_view;
if (wlr_output->fullscreen_surface == view->wlr_surface) {
// The output will render the fullscreen view
goto renderer_end;
}
if (view->wlr_surface != NULL) {
view_for_each_surface(view, &data.layout, render_surface, &data);
}
@ -556,6 +518,7 @@ static void render_output(struct roots_output *output) {
&output->layers[ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY]);
renderer_end:
wlr_output_render_software_cursors(wlr_output, &damage);
wlr_renderer_scissor(renderer, NULL);
wlr_renderer_end(renderer);

View File

@ -294,7 +294,6 @@ void wlr_output_destroy(struct wlr_output *output) {
wl_list_remove(&output->display_destroy.link);
wlr_output_destroy_global(output);
wlr_output_set_fullscreen_surface(output, NULL);
wlr_signal_emit_safe(&output->events.destroy, output);
@ -340,132 +339,6 @@ bool wlr_output_make_current(struct wlr_output *output, int *buffer_age) {
return output->impl->make_current(output, buffer_age);
}
static void output_scissor(struct wlr_output *output, pixman_box32_t *rect) {
struct wlr_renderer *renderer = wlr_backend_get_renderer(output->backend);
assert(renderer);
struct wlr_box box = {
.x = rect->x1,
.y = rect->y1,
.width = rect->x2 - rect->x1,
.height = rect->y2 - rect->y1,
};
int ow, oh;
wlr_output_transformed_resolution(output, &ow, &oh);
// Scissor is in renderer coordinates, ie. upside down
enum wl_output_transform transform =
wlr_output_transform_invert(output->transform);
wlr_box_transform(&box, transform, ow, oh, &box);
wlr_renderer_scissor(renderer, &box);
}
static void output_fullscreen_surface_get_box(struct wlr_output *output,
struct wlr_surface *surface, struct wlr_box *box) {
int width, height;
wlr_output_effective_resolution(output, &width, &height);
int x = (width - surface->current.width) / 2;
int y = (height - surface->current.height) / 2;
box->x = x * output->scale;
box->y = y * output->scale;
box->width = surface->current.width * output->scale;
box->height = surface->current.height * output->scale;
}
static void output_fullscreen_surface_render(struct wlr_output *output,
struct wlr_surface *surface, const struct timespec *when,
pixman_region32_t *damage) {
struct wlr_renderer *renderer = wlr_backend_get_renderer(output->backend);
assert(renderer);
struct wlr_texture *texture = wlr_surface_get_texture(surface);
if (texture == NULL) {
wlr_renderer_clear(renderer, (float[]){0, 0, 0, 1});
return;
}
struct wlr_box box;
output_fullscreen_surface_get_box(output, surface, &box);
float matrix[9];
enum wl_output_transform transform =
wlr_output_transform_invert(surface->current.transform);
wlr_matrix_project_box(matrix, &box, transform, 0,
output->transform_matrix);
int nrects;
pixman_box32_t *rects = pixman_region32_rectangles(damage, &nrects);
for (int i = 0; i < nrects; ++i) {
output_scissor(output, &rects[i]);
wlr_renderer_clear(renderer, (float[]){0, 0, 0, 1});
wlr_render_texture_with_matrix(surface->renderer, texture, matrix, 1.0f);
}
wlr_renderer_scissor(renderer, NULL);
wlr_surface_send_frame_done(surface, when);
}
/**
* Returns the cursor box, scaled for its output.
*/
static void output_cursor_get_box(struct wlr_output_cursor *cursor,
struct wlr_box *box) {
box->x = cursor->x - cursor->hotspot_x;
box->y = cursor->y - cursor->hotspot_y;
box->width = cursor->width;
box->height = cursor->height;
}
static void output_cursor_render(struct wlr_output_cursor *cursor,
const struct timespec *when, pixman_region32_t *damage) {
struct wlr_renderer *renderer =
wlr_backend_get_renderer(cursor->output->backend);
assert(renderer);
struct wlr_texture *texture = cursor->texture;
if (cursor->surface != NULL) {
texture = wlr_surface_get_texture(cursor->surface);
}
if (texture == NULL) {
return;
}
struct wlr_box box;
output_cursor_get_box(cursor, &box);
pixman_region32_t surface_damage;
pixman_region32_init(&surface_damage);
pixman_region32_union_rect(&surface_damage, &surface_damage, box.x, box.y,
box.width, box.height);
pixman_region32_intersect(&surface_damage, &surface_damage, damage);
if (!pixman_region32_not_empty(&surface_damage)) {
goto surface_damage_finish;
}
float matrix[9];
wlr_matrix_project_box(matrix, &box, WL_OUTPUT_TRANSFORM_NORMAL, 0,
cursor->output->transform_matrix);
int nrects;
pixman_box32_t *rects = pixman_region32_rectangles(&surface_damage, &nrects);
for (int i = 0; i < nrects; ++i) {
output_scissor(cursor->output, &rects[i]);
wlr_render_texture_with_matrix(renderer, texture, matrix, 1.0f);
}
wlr_renderer_scissor(renderer, NULL);
if (cursor->surface != NULL) {
wlr_surface_send_frame_done(cursor->surface, when);
}
surface_damage_finish:
pixman_region32_fini(&surface_damage);
}
bool wlr_output_swap_buffers(struct wlr_output *output, struct timespec *when,
pixman_region32_t *damage) {
if (output->frame_pending) {
@ -480,37 +353,12 @@ bool wlr_output_swap_buffers(struct wlr_output *output, struct timespec *when,
int width, height;
wlr_output_transformed_resolution(output, &width, &height);
pixman_region32_t render_damage;
pixman_region32_init(&render_damage);
pixman_region32_union_rect(&render_damage, &render_damage, 0, 0,
width, height);
if (damage != NULL) {
// Damage tracking supported
pixman_region32_intersect(&render_damage, &render_damage, damage);
}
struct timespec now;
if (when == NULL) {
clock_gettime(CLOCK_MONOTONIC, &now);
when = &now;
}
if (pixman_region32_not_empty(&render_damage)) {
if (output->fullscreen_surface != NULL) {
output_fullscreen_surface_render(output, output->fullscreen_surface,
when, &render_damage);
}
struct wlr_output_cursor *cursor;
wl_list_for_each(cursor, &output->cursors, link) {
if (!cursor->enabled || !cursor->visible ||
output->hardware_cursor == cursor) {
continue;
}
output_cursor_render(cursor, when, &render_damage);
}
}
struct wlr_output_event_swap_buffers event = {
.output = output,
.when = when,
@ -518,22 +366,40 @@ bool wlr_output_swap_buffers(struct wlr_output *output, struct timespec *when,
};
wlr_signal_emit_safe(&output->events.swap_buffers, &event);
pixman_region32_t render_damage;
pixman_region32_init(&render_damage);
pixman_region32_union_rect(&render_damage, &render_damage, 0, 0,
width, height);
if (damage != NULL) {
// Damage tracking supported
pixman_region32_intersect(&render_damage, &render_damage, damage);
}
// Transform damage into renderer coordinates, ie. upside down
enum wl_output_transform transform = wlr_output_transform_compose(
wlr_output_transform_invert(output->transform),
WL_OUTPUT_TRANSFORM_FLIPPED_180);
wlr_region_transform(&render_damage, &render_damage, transform, width,
height);
// TODO: take transformed coords, make the renderer flip the damage
enum wl_output_transform transform =
wlr_output_transform_invert(output->transform);
wlr_region_transform(&render_damage, &render_damage, transform,
width, height);
if (!output->impl->swap_buffers(output, damage ? &render_damage : NULL)) {
pixman_region32_fini(&render_damage);
return false;
}
pixman_region32_fini(&render_damage);
struct wlr_output_cursor *cursor;
wl_list_for_each(cursor, &output->cursors, link) {
if (!cursor->enabled || !cursor->visible || cursor->surface == NULL) {
continue;
}
wlr_surface_send_frame_done(cursor->surface, when);
}
output->frame_pending = true;
output->needs_swap = false;
pixman_region32_clear(&output->damage);
pixman_region32_fini(&render_damage);
return true;
}
@ -626,76 +492,6 @@ void wlr_output_damage_whole(struct wlr_output *output) {
wlr_output_update_needs_swap(output);
}
static void output_fullscreen_surface_reset(struct wlr_output *output) {
if (output->fullscreen_surface != NULL) {
wl_list_remove(&output->fullscreen_surface_commit.link);
wl_list_remove(&output->fullscreen_surface_destroy.link);
output->fullscreen_surface = NULL;
wlr_output_damage_whole(output);
}
}
static void output_fullscreen_surface_handle_commit(
struct wl_listener *listener, void *data) {
struct wlr_output *output = wl_container_of(listener, output,
fullscreen_surface_commit);
struct wlr_surface *surface = output->fullscreen_surface;
if (output->fullscreen_width != surface->current.width ||
output->fullscreen_height != surface->current.height) {
output->fullscreen_width = surface->current.width;
output->fullscreen_height = surface->current.height;
wlr_output_damage_whole(output);
return;
}
struct wlr_box box;
output_fullscreen_surface_get_box(output, surface, &box);
pixman_region32_t damage;
pixman_region32_init(&damage);
pixman_region32_copy(&damage, &surface->current.surface_damage);
wlr_region_scale(&damage, &damage, output->scale);
pixman_region32_translate(&damage, box.x, box.y);
pixman_region32_union(&output->damage, &output->damage, &damage);
pixman_region32_fini(&damage);
wlr_output_update_needs_swap(output);
}
static void output_fullscreen_surface_handle_destroy(
struct wl_listener *listener, void *data) {
struct wlr_output *output = wl_container_of(listener, output,
fullscreen_surface_destroy);
output_fullscreen_surface_reset(output);
}
void wlr_output_set_fullscreen_surface(struct wlr_output *output,
struct wlr_surface *surface) {
// TODO: hardware fullscreen
if (output->fullscreen_surface == surface) {
return;
}
output_fullscreen_surface_reset(output);
output->fullscreen_surface = surface;
wlr_output_damage_whole(output);
if (surface == NULL) {
return;
}
output->fullscreen_surface_commit.notify =
output_fullscreen_surface_handle_commit;
wl_signal_add(&surface->events.commit, &output->fullscreen_surface_commit);
output->fullscreen_surface_destroy.notify =
output_fullscreen_surface_handle_destroy;
wl_signal_add(&surface->events.destroy,
&output->fullscreen_surface_destroy);
}
struct wlr_output *wlr_output_from_resource(struct wl_resource *resource) {
assert(wl_resource_instance_of(resource, &wl_output_interface,
&output_impl));
@ -728,6 +524,111 @@ void wlr_output_lock_software_cursors(struct wlr_output *output, bool lock) {
// again.
}
static void output_scissor(struct wlr_output *output, pixman_box32_t *rect) {
struct wlr_renderer *renderer = wlr_backend_get_renderer(output->backend);
assert(renderer);
struct wlr_box box = {
.x = rect->x1,
.y = rect->y1,
.width = rect->x2 - rect->x1,
.height = rect->y2 - rect->y1,
};
int ow, oh;
wlr_output_transformed_resolution(output, &ow, &oh);
enum wl_output_transform transform =
wlr_output_transform_invert(output->transform);
wlr_box_transform(&box, transform, ow, oh, &box);
wlr_renderer_scissor(renderer, &box);
}
static void output_cursor_get_box(struct wlr_output_cursor *cursor,
struct wlr_box *box);
static void output_cursor_render(struct wlr_output_cursor *cursor,
pixman_region32_t *damage) {
struct wlr_renderer *renderer =
wlr_backend_get_renderer(cursor->output->backend);
assert(renderer);
struct wlr_texture *texture = cursor->texture;
if (cursor->surface != NULL) {
texture = wlr_surface_get_texture(cursor->surface);
}
if (texture == NULL) {
return;
}
struct wlr_box box;
output_cursor_get_box(cursor, &box);
pixman_region32_t surface_damage;
pixman_region32_init(&surface_damage);
pixman_region32_union_rect(&surface_damage, &surface_damage, box.x, box.y,
box.width, box.height);
pixman_region32_intersect(&surface_damage, &surface_damage, damage);
if (!pixman_region32_not_empty(&surface_damage)) {
goto surface_damage_finish;
}
float matrix[9];
wlr_matrix_project_box(matrix, &box, WL_OUTPUT_TRANSFORM_NORMAL, 0,
cursor->output->transform_matrix);
int nrects;
pixman_box32_t *rects = pixman_region32_rectangles(&surface_damage, &nrects);
for (int i = 0; i < nrects; ++i) {
output_scissor(cursor->output, &rects[i]);
wlr_render_texture_with_matrix(renderer, texture, matrix, 1.0f);
}
wlr_renderer_scissor(renderer, NULL);
surface_damage_finish:
pixman_region32_fini(&surface_damage);
}
void wlr_output_render_software_cursors(struct wlr_output *output,
pixman_region32_t *damage) {
int width, height;
wlr_output_transformed_resolution(output, &width, &height);
pixman_region32_t render_damage;
pixman_region32_init(&render_damage);
pixman_region32_union_rect(&render_damage, &render_damage, 0, 0,
width, height);
if (damage != NULL) {
// Damage tracking supported
pixman_region32_intersect(&render_damage, &render_damage, damage);
}
if (pixman_region32_not_empty(&render_damage)) {
struct wlr_output_cursor *cursor;
wl_list_for_each(cursor, &output->cursors, link) {
if (!cursor->enabled || !cursor->visible ||
output->hardware_cursor == cursor) {
continue;
}
output_cursor_render(cursor, &render_damage);
}
}
pixman_region32_fini(&render_damage);
}
/**
* Returns the cursor box, scaled for its output.
*/
static void output_cursor_get_box(struct wlr_output_cursor *cursor,
struct wlr_box *box) {
box->x = cursor->x - cursor->hotspot_x;
box->y = cursor->y - cursor->hotspot_y;
box->width = cursor->width;
box->height = cursor->height;
}
static void output_cursor_damage_whole(struct wlr_output_cursor *cursor) {
struct wlr_box box;
@ -861,9 +762,6 @@ static void output_cursor_commit(struct wlr_output_cursor *cursor,
}
if (output_cursor_attempt_hardware(cursor)) {
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
wlr_surface_send_frame_done(surface, &now);
return;
}