Updated DRM cursor rendering

This commit is contained in:
Scott Anderson 2017-08-06 21:38:40 +12:00
parent aedfa27d3a
commit 1db97a9af9
6 changed files with 173 additions and 132 deletions

View File

@ -53,6 +53,7 @@ static void session_signal(struct wl_listener *listener, void *data) {
for (size_t i = 0; i < drm->outputs->length; ++i) {
struct wlr_output_state *output = drm->outputs->items[i];
wlr_drm_output_start_renderer(output);
wlr_drm_crtc_set_cursor(drm, output->crtc);
}
} else {
wlr_log(L_INFO, "DRM fd paused");

View File

@ -15,6 +15,9 @@
#include <wlr/backend/interface.h>
#include <wlr/interfaces/wlr_output.h>
#include <wlr/util/log.h>
#include <wlr/render/matrix.h>
#include <wlr/render/gles2.h>
#include <wlr/render.h>
#include "drm.h"
#include "drm-util.h"
@ -174,7 +177,7 @@ void wlr_drm_renderer_free(struct wlr_drm_renderer *renderer) {
}
static bool wlr_drm_plane_renderer_init(struct wlr_drm_renderer *renderer,
struct wlr_drm_plane *plane, uint32_t width, uint32_t height) {
struct wlr_drm_plane *plane, uint32_t width, uint32_t height, uint32_t flags) {
if (plane->width == width && plane->height == height) {
return true;
}
@ -183,7 +186,7 @@ static bool wlr_drm_plane_renderer_init(struct wlr_drm_renderer *renderer,
plane->height = height;
plane->gbm = gbm_surface_create(renderer->gbm, width, height,
GBM_FORMAT_XRGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING);
GBM_FORMAT_ARGB8888, GBM_BO_USE_RENDERING | flags);
if (!plane->gbm) {
wlr_log_errno(L_ERROR, "Failed to create GBM surface for plane");
return false;
@ -203,8 +206,6 @@ static void wlr_drm_plane_renderer_free(struct wlr_drm_renderer *renderer,
if (!renderer || !plane)
return;
wlr_log(L_DEBUG, "%s called", __func__);
if (plane->front)
gbm_surface_release_buffer(plane->gbm, plane->front);
if (plane->back)
@ -215,12 +216,19 @@ static void wlr_drm_plane_renderer_free(struct wlr_drm_renderer *renderer,
if (plane->gbm)
gbm_surface_destroy(plane->gbm);
if (plane->wlr_surf)
wlr_surface_destroy(plane->wlr_surf);
if (plane->wlr_rend)
wlr_renderer_destroy(plane->wlr_rend);
plane->width = 0;
plane->height = 0;
plane->egl = EGL_NO_SURFACE;
plane->gbm = NULL;
plane->front = NULL;
plane->back = NULL;
plane->wlr_rend = NULL;
plane->wlr_surf = NULL;
}
static void free_fb(struct gbm_bo *bo, void *data) {
@ -480,7 +488,7 @@ static bool wlr_drm_output_set_mode(struct wlr_output_state *output,
continue;
if (!wlr_drm_plane_renderer_init(&drm->renderer, crtc->primary,
mode->width, mode->height)) {
mode->width, mode->height, GBM_BO_USE_SCANOUT)) {
wlr_log(L_ERROR, "Failed to initalise renderer for plane");
goto error_enc;
}
@ -506,79 +514,95 @@ static void wlr_drm_output_transform(struct wlr_output_state *output,
output->base->transform = transform;
}
bool wlr_drm_crtc_set_cursor(struct wlr_backend_state *drm, struct wlr_drm_crtc *crtc) {
if (!crtc || !crtc->cursor || !crtc->cursor->gbm)
return true;
struct wlr_drm_plane *plane = crtc->cursor;
uint32_t bo_handle = gbm_bo_get_handle(plane->back).u32;
if (drmModeSetCursor(drm->fd, crtc->id, bo_handle, plane->width, plane->height)) {
wlr_log_errno(L_ERROR, "Failed to set hardware cursor");
return false;
}
return true;
}
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 *drm = wl_container_of(output->renderer, drm, renderer);
struct wlr_drm_renderer *renderer = output->renderer;
struct wlr_drm_crtc *crtc = output->crtc;
struct wlr_drm_plane *plane = crtc->cursor;
if (!buf) {
drmModeSetCursor(state->fd, output->crtc->id, 0, 0, 0);
drmModeSetCursor(drm->fd, crtc->id, 0, 0, 0);
return true;
}
if (!gbm_device_is_format_supported(state->renderer.gbm,
GBM_FORMAT_ARGB8888, GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE)) {
wlr_log(L_ERROR, "Failed to create cursor bo: ARGB8888 pixel format is "
"unsupported on this device");
if (!plane) {
plane = calloc(1, sizeof(*plane));
if (!plane) {
wlr_log_errno(L_ERROR, "Allocation failed");
return false;
}
crtc->cursor = plane;
}
uint64_t bo_width, bo_height;
if (!plane->gbm) {
int ret;
uint64_t w, h;
ret = drmGetCap(drm->fd, DRM_CAP_CURSOR_WIDTH, &w);
w = ret ? 64 : w;
ret = drmGetCap(drm->fd, DRM_CAP_CURSOR_HEIGHT, &h);
h = ret ? 64 : h;
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);
if (width > w || height > h) {
wlr_log(L_INFO, "Cursor too large (max %dx%d)", (int)w, (int)h);
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_ARGB8888, 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 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(tmp + i * bo_stride, buf + i * stride * 4, width * 4);
}
if (gbm_bo_write(bo, tmp, sizeof(tmp)) < 0) {
wlr_log(L_ERROR, "Failed to write cursor to bo");
if (!wlr_drm_plane_renderer_init(renderer, plane, w, h, GBM_BO_USE_CURSOR)) {
wlr_log(L_ERROR, "Cannot allocate cursor resources");
return false;
}
uint32_t bo_handle = gbm_bo_get_handle(bo).u32;
if (drmModeSetCursor(state->fd, output->crtc->id, bo_handle, bo_width, bo_height)) {
wlr_log_errno(L_INFO, "Failed to set hardware cursor");
wlr_matrix_surface(plane->matrix, plane->width, plane->height,
output->base->transform);
plane->wlr_rend = wlr_gles2_renderer_init();
if (!plane->wlr_rend)
return false;
plane->wlr_surf = wlr_render_surface_init(plane->wlr_rend);
if (!plane->wlr_surf)
return false;
}
return true;
wlr_drm_plane_make_current(renderer, plane);
wlr_surface_attach_pixels(plane->wlr_surf, WL_SHM_FORMAT_ARGB8888,
stride, width, height, buf);
glViewport(0, 0, plane->width, plane->height);
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
float matrix[16];
wlr_surface_get_matrix(plane->wlr_surf, &matrix, &plane->matrix, 0, 0);
wlr_render_with_matrix(plane->wlr_rend, plane->wlr_surf, &matrix);
wlr_drm_plane_swap_buffers(renderer, plane);
return wlr_drm_crtc_set_cursor(drm, crtc);
}
static bool wlr_drm_output_move_cursor(struct wlr_output_state *output,
int x, int y) {
struct wlr_backend_state *state =
wl_container_of(output->renderer, state, renderer);
return !drmModeMoveCursor(state->fd, output->crtc->id, x, y);
struct wlr_backend_state *drm =
wl_container_of(output->renderer, drm, renderer);
return !drmModeMoveCursor(drm->fd, output->crtc->id, x, y);
}
static void wlr_drm_output_destroy(struct wlr_output_state *output) {
@ -786,9 +810,14 @@ void wlr_drm_output_cleanup(struct wlr_output_state *output, bool restore) {
restore = false;
}
wlr_drm_plane_renderer_free(renderer, output->crtc->overlay);
wlr_drm_plane_renderer_free(renderer, output->crtc->primary);
wlr_drm_plane_renderer_free(renderer, output->crtc->cursor);
struct wlr_drm_crtc *crtc = output->crtc;
for (int i = 0; i < 3; ++i) {
wlr_drm_plane_renderer_free(renderer, crtc->planes[i]);
if (crtc->planes[i] && crtc->planes[i]->id == 0) {
free(crtc->planes[i]);
crtc->planes[i] = NULL;
}
}
output->crtc = NULL;
output->possible_crtc = 0;

View File

@ -21,11 +21,9 @@
struct wlr_drm_plane {
uint32_t type;
uint32_t id;
uint32_t fb_id;
uint32_t possible_crtcs;
int32_t x, y;
uint32_t width, height;
struct gbm_surface *gbm;
@ -34,6 +32,11 @@ struct wlr_drm_plane {
struct gbm_bo *front;
struct gbm_bo *back;
// Only used by cursor
float matrix[16];
struct wlr_renderer *wlr_rend;
struct wlr_surface *wlr_surf;
union wlr_drm_plane_props props;
};
@ -159,5 +162,6 @@ void wlr_drm_scan_connectors(struct wlr_backend_state *state);
int wlr_drm_event(int fd, uint32_t mask, void *data);
void wlr_drm_output_start_renderer(struct wlr_output_state *output);
bool wlr_drm_crtc_set_cursor(struct wlr_backend_state *drm, struct wlr_drm_crtc *crtc);
#endif

View File

@ -1,10 +1,16 @@
#ifndef _WLR_RENDER_MATRIX_H
#define _WLR_RENDER_MATRIX_H
#include <stdint.h>
void wlr_matrix_identity(float (*output)[16]);
void wlr_matrix_translate(float (*output)[16], float x, float y, float z);
void wlr_matrix_scale(float (*output)[16], float x, float y, float z);
void wlr_matrix_rotate(float (*output)[16], float radians);
void wlr_matrix_mul(const float (*x)[16], const float (*y)[16], float (*product)[16]);
enum wl_output_transform;
void wlr_matrix_surface(float mat[static 16], int32_t width, int32_t height,
enum wl_output_transform transform);
#endif

View File

@ -1,5 +1,6 @@
#include <string.h>
#include <math.h>
#include <wayland-server-protocol.h>
#include <wlr/render/matrix.h>
/* Obtains the index for the given row/column */
@ -81,3 +82,62 @@ void wlr_matrix_mul(const float (*x)[16], const float (*y)[16], float (*product)
};
memcpy(*product, _product, sizeof(_product));
}
static const float transforms[][4] = {
[WL_OUTPUT_TRANSFORM_NORMAL] = {
1.0f, 0.0f,
0.0f, -1.0f,
},
[WL_OUTPUT_TRANSFORM_90] = {
0.0f, -1.0f,
-1.0f, 0.0f,
},
[WL_OUTPUT_TRANSFORM_180] = {
-1.0f, 0.0f,
0.0f, 1.0f,
},
[WL_OUTPUT_TRANSFORM_270] = {
0.0f, 1.0f,
1.0f, 0.0f,
},
[WL_OUTPUT_TRANSFORM_FLIPPED] = {
-1.0f, 0.0f,
0.0f, -1.0f,
},
[WL_OUTPUT_TRANSFORM_FLIPPED_90] = {
0.0f, 1.0f,
-1.0f, 0.0f,
},
[WL_OUTPUT_TRANSFORM_FLIPPED_180] = {
1.0f, 0.0f,
0.0f, 1.0f,
},
[WL_OUTPUT_TRANSFORM_FLIPPED_270] = {
0.0f, -1.0f,
1.0f, 0.0f,
},
};
// Equivilent to glOrtho(0, width, 0, height, 1, -1) with the transform applied
void wlr_matrix_surface(float mat[static 16], int32_t width, int32_t height,
enum wl_output_transform transform) {
memset(mat, 0, sizeof(*mat) * 16);
const float *t = transforms[transform];
float x = 2.0f / width;
float y = 2.0f / height;
// Rotation + relection
mat[0] = x * t[0];
mat[1] = x * t[1];
mat[4] = y * t[2];
mat[5] = y * t[3];
// Translation
mat[3] = -copysign(1.0f, mat[0] + mat[1]);
mat[7] = -copysign(1.0f, mat[4] + mat[5]);
// Identity
mat[10] = 1.0f;
mat[15] = 1.0f;
}

View File

@ -88,67 +88,8 @@ struct wl_global *wlr_output_create_global(
return wl_global;
}
static const float transforms[][4] = {
[WL_OUTPUT_TRANSFORM_NORMAL] = {
1.0f, 0.0f,
0.0f, -1.0f,
},
[WL_OUTPUT_TRANSFORM_90] = {
0.0f, -1.0f,
-1.0f, 0.0f,
},
[WL_OUTPUT_TRANSFORM_180] = {
-1.0f, 0.0f,
0.0f, 1.0f,
},
[WL_OUTPUT_TRANSFORM_270] = {
0.0f, 1.0f,
1.0f, 0.0f,
},
[WL_OUTPUT_TRANSFORM_FLIPPED] = {
-1.0f, 0.0f,
0.0f, -1.0f,
},
[WL_OUTPUT_TRANSFORM_FLIPPED_90] = {
0.0f, 1.0f,
-1.0f, 0.0f,
},
[WL_OUTPUT_TRANSFORM_FLIPPED_180] = {
1.0f, 0.0f,
0.0f, 1.0f,
},
[WL_OUTPUT_TRANSFORM_FLIPPED_270] = {
0.0f, -1.0f,
1.0f, 0.0f,
},
};
// Equivilent to glOrtho(0, width, 0, height, 1, -1) with the transform applied
static void set_matrix(float mat[static 16], int32_t width, int32_t height,
enum wl_output_transform transform) {
memset(mat, 0, sizeof(*mat) * 16);
const float *t = transforms[transform];
float x = 2.0f / width;
float y = 2.0f / height;
// Rotation + relection
mat[0] = x * t[0];
mat[1] = x * t[1];
mat[4] = y * t[2];
mat[5] = y * t[3];
// Translation
mat[3] = -copysign(1.0f, mat[0] + mat[1]);
mat[7] = -copysign(1.0f, mat[4] + mat[5]);
// Identity
mat[10] = 1.0f;
mat[15] = 1.0f;
}
void wlr_output_update_matrix(struct wlr_output *output) {
set_matrix(output->transform_matrix, output->width, output->height, output->transform);
wlr_matrix_surface(output->transform_matrix, output->width, output->height, output->transform);
}
struct wlr_output *wlr_output_create(struct wlr_output_impl *impl,