diff options
Diffstat (limited to 'backend/drm')
-rw-r--r-- | backend/drm/backend.c | 6 | ||||
-rw-r--r-- | backend/drm/drm-atomic.c | 176 | ||||
-rw-r--r-- | backend/drm/drm-legacy.c | 58 | ||||
-rw-r--r-- | backend/drm/drm-properties.c | 2 | ||||
-rw-r--r-- | backend/drm/drm-util.c | 36 | ||||
-rw-r--r-- | backend/drm/drm.c | 109 |
6 files changed, 317 insertions, 70 deletions
diff --git a/backend/drm/backend.c b/backend/drm/backend.c index 68eae890..76aef9b9 100644 --- a/backend/drm/backend.c +++ b/backend/drm/backend.c @@ -53,7 +53,11 @@ 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); + + struct wlr_drm_plane *plane = output->crtc->cursor; + + drm->iface->crtc_set_cursor(drm, output->crtc, + plane ? plane->cursor_bo : NULL); } } else { wlr_log(L_INFO, "DRM fd paused"); diff --git a/backend/drm/drm-atomic.c b/backend/drm/drm-atomic.c new file mode 100644 index 00000000..0b8a7b1d --- /dev/null +++ b/backend/drm/drm-atomic.c @@ -0,0 +1,176 @@ +#include <gbm.h> +#include <xf86drm.h> +#include <xf86drmMode.h> +#include <wlr/util/log.h> +#include "backend/drm.h" +#include "backend/drm-util.h" + +struct atomic { + drmModeAtomicReq *req; + int cursor; + bool failed; +}; + +static void atomic_begin(struct wlr_drm_crtc *crtc, struct atomic *atom) { + if (!crtc->atomic) { + crtc->atomic = drmModeAtomicAlloc(); + if (!crtc->atomic) { + wlr_log_errno(L_ERROR, "Allocation failed"); + atom->failed = true; + return; + } + } + + atom->req = crtc->atomic; + atom->cursor = drmModeAtomicGetCursor(atom->req); + atom->failed = false; +} + +static bool atomic_end(int drm_fd, struct atomic *atom) { + if (atom->failed) { + return false; + } + + uint32_t flags = DRM_MODE_ATOMIC_TEST_ONLY | DRM_MODE_ATOMIC_NONBLOCK; + + if (drmModeAtomicCommit(drm_fd, atom->req, flags, NULL)) { + wlr_log_errno(L_ERROR, "Atomic test failed"); + drmModeAtomicSetCursor(atom->req, atom->cursor); + return false; + } + + return true; +} + +static bool atomic_commit(int drm_fd, struct atomic *atom, struct wlr_output_state *output, + uint32_t flag) { + if (atom->failed) { + return false; + } + + uint32_t flags = DRM_MODE_PAGE_FLIP_EVENT | DRM_MODE_ATOMIC_NONBLOCK | flag; + + int ret = drmModeAtomicCommit(drm_fd, atom->req, flags, output); + if (ret) { + wlr_log_errno(L_ERROR, "Atomic commit failed"); + } + + drmModeAtomicSetCursor(atom->req, 0); + + return !ret; +} + +static inline void atomic_add(struct atomic *atom, uint32_t id, uint32_t prop, uint64_t val) { + if (!atom->failed && drmModeAtomicAddProperty(atom->req, id, prop, val) < 0) { + wlr_log_errno(L_ERROR, "Failed to add atomic DRM property"); + atom->failed = true; + } +} + +static void set_plane_props(struct atomic *atom, struct wlr_drm_plane *plane, + uint32_t crtc_id, uint32_t fb_id, bool set_crtc_xy) { + uint32_t id = plane->id; + const union wlr_drm_plane_props *props = &plane->props; + + // The src_* properties are in 16.16 fixed point + atomic_add(atom, id, props->src_x, 0); + atomic_add(atom, id, props->src_y, 0); + atomic_add(atom, id, props->src_w, plane->width << 16); + atomic_add(atom, id, props->src_h, plane->height << 16); + atomic_add(atom, id, props->crtc_w, plane->width); + atomic_add(atom, id, props->crtc_h, plane->height); + atomic_add(atom, id, props->fb_id, fb_id); + atomic_add(atom, id, props->crtc_id, crtc_id); + if (set_crtc_xy) { + atomic_add(atom, id, props->crtc_x, 0); + atomic_add(atom, id, props->crtc_y, 0); + } +} + +static bool atomic_crtc_pageflip(struct wlr_backend_state *drm, struct wlr_output_state *output, + struct wlr_drm_crtc *crtc, uint32_t fb_id, drmModeModeInfo *mode) { + if (mode) { + if (crtc->mode_id) { + drmModeDestroyPropertyBlob(drm->fd, crtc->mode_id); + } + + if (drmModeCreatePropertyBlob(drm->fd, mode, sizeof(*mode), &crtc->mode_id)) { + wlr_log_errno(L_ERROR, "Unable to create property blob"); + return false; + } + } + + struct atomic atom; + + atomic_begin(crtc, &atom); + atomic_add(&atom, output->connector, output->props.crtc_id, crtc->id); + atomic_add(&atom, crtc->id, crtc->props.mode_id, crtc->mode_id); + atomic_add(&atom, crtc->id, crtc->props.active, 1); + set_plane_props(&atom, crtc->primary, crtc->id, fb_id, true); + return atomic_commit(drm->fd, &atom, output, mode ? DRM_MODE_ATOMIC_ALLOW_MODESET : 0); +} + +static void atomic_conn_enable(struct wlr_backend_state *drm, struct wlr_output_state *output, + bool enable) { + struct wlr_drm_crtc *crtc = output->crtc; + struct atomic atom; + + atomic_begin(crtc, &atom); + atomic_add(&atom, crtc->id, crtc->props.active, enable); + atomic_end(drm->fd, &atom); +} + +bool legacy_crtc_set_cursor(struct wlr_backend_state *drm, struct wlr_drm_crtc *crtc, + struct gbm_bo *bo); + +static bool atomic_crtc_set_cursor(struct wlr_backend_state *drm, struct wlr_drm_crtc *crtc, + struct gbm_bo *bo) { + if (!crtc || !crtc->cursor) { + return true; + } + + struct wlr_drm_plane *plane = crtc->cursor; + // We can't use atomic operations on fake planes + if (plane->id == 0) { + return legacy_crtc_set_cursor(drm, crtc, bo); + } + + struct atomic atom; + + atomic_begin(crtc, &atom); + + if (bo) { + set_plane_props(&atom, plane, crtc->id, get_fb_for_bo(bo), false); + } else { + atomic_add(&atom, plane->id, plane->props.fb_id, 0); + atomic_add(&atom, plane->id, plane->props.crtc_id, 0); + } + + return atomic_end(drm->fd, &atom); +} + +bool legacy_crtc_move_cursor(struct wlr_backend_state *drm, struct wlr_drm_crtc *crtc, + int x, int y); + +static bool atomic_crtc_move_cursor(struct wlr_backend_state *drm, struct wlr_drm_crtc *crtc, + int x, int y) { + struct wlr_drm_plane *plane = crtc->cursor; + // We can't use atomic operations on fake planes + if (plane->id == 0) { + return legacy_crtc_move_cursor(drm, crtc, x, y); + } + + struct atomic atom; + + atomic_begin(crtc, &atom); + atomic_add(&atom, plane->id, plane->props.crtc_x, x); + atomic_add(&atom, plane->id, plane->props.crtc_y, y); + return atomic_end(drm->fd, &atom); +} + +const struct wlr_drm_interface atomic_iface = { + .conn_enable = atomic_conn_enable, + .crtc_pageflip = atomic_crtc_pageflip, + .crtc_set_cursor = atomic_crtc_set_cursor, + .crtc_move_cursor = atomic_crtc_move_cursor, +}; diff --git a/backend/drm/drm-legacy.c b/backend/drm/drm-legacy.c new file mode 100644 index 00000000..f7adac86 --- /dev/null +++ b/backend/drm/drm-legacy.c @@ -0,0 +1,58 @@ +#include <gbm.h> +#include <xf86drm.h> +#include <xf86drmMode.h> +#include <wlr/util/log.h> +#include "backend/drm.h" +#include "backend/drm-util.h" + +static bool legacy_crtc_pageflip(struct wlr_backend_state *drm, struct wlr_output_state *output, + struct wlr_drm_crtc *crtc, uint32_t fb_id, drmModeModeInfo *mode) { + if (mode) { + drmModeSetCrtc(drm->fd, crtc->id, fb_id, 0, 0, + &output->connector, 1, mode); + } + + drmModePageFlip(drm->fd, crtc->id, fb_id, DRM_MODE_PAGE_FLIP_EVENT, output); + + return true; +} + +static void legacy_conn_enable(struct wlr_backend_state *drm, struct wlr_output_state *output, + bool enable) { + drmModeConnectorSetProperty(drm->fd, output->connector, output->props.dpms, + enable ? DRM_MODE_DPMS_ON : DRM_MODE_DPMS_OFF); +} + +bool legacy_crtc_set_cursor(struct wlr_backend_state *drm, struct wlr_drm_crtc *crtc, + struct gbm_bo *bo) { + if (!crtc || !crtc->cursor) { + return true; + } + + if (!bo) { + drmModeSetCursor(drm->fd, crtc->id, 0, 0, 0); + return true; + } + + struct wlr_drm_plane *plane = crtc->cursor; + + if (drmModeSetCursor(drm->fd, crtc->id, gbm_bo_get_handle(bo).u32, + plane->width, plane->height)) { + wlr_log_errno(L_ERROR, "Failed to set hardware cursor"); + return false; + } + + return true; +} + +bool legacy_crtc_move_cursor(struct wlr_backend_state *drm, struct wlr_drm_crtc *crtc, + int x, int y) { + return !drmModeMoveCursor(drm->fd, crtc->id, x, y); +} + +const struct wlr_drm_interface legacy_iface = { + .conn_enable = legacy_conn_enable, + .crtc_pageflip = legacy_crtc_pageflip, + .crtc_set_cursor = legacy_crtc_set_cursor, + .crtc_move_cursor = legacy_crtc_move_cursor, +}; diff --git a/backend/drm/drm-properties.c b/backend/drm/drm-properties.c index 1b6e745f..ef24476f 100644 --- a/backend/drm/drm-properties.c +++ b/backend/drm/drm-properties.c @@ -27,6 +27,8 @@ static const struct prop_info connector_info[] = { static const struct prop_info crtc_info[] = { #define INDEX(name) (offsetof(union wlr_drm_crtc_props, name) / sizeof(uint32_t)) + { "ACTIVE", INDEX(active) }, + { "MODE_ID", INDEX(mode_id) }, { "rotation", INDEX(rotation) }, { "scaling mode", INDEX(scaling_mode) }, #undef INDEX diff --git a/backend/drm/drm-util.c b/backend/drm/drm-util.c index 6159b5de..0f8ee18c 100644 --- a/backend/drm/drm-util.c +++ b/backend/drm/drm-util.c @@ -2,7 +2,9 @@ #include <string.h> #include <drm.h> #include <drm_mode.h> +#include <gbm.h> #include "backend/drm-util.h" +#include <wlr/util/log.h> int32_t calculate_refresh_rate(drmModeModeInfo *mode) { int32_t refresh = (mode->clock * 1000000LL / mode->htotal + @@ -133,6 +135,40 @@ const char *conn_get_name(uint32_t type_id) { } } +static void free_fb(struct gbm_bo *bo, void *data) { + uint32_t id = (uintptr_t)data; + + if (id) { + struct gbm_device *gbm = gbm_bo_get_device(bo); + drmModeRmFB(gbm_device_get_fd(gbm), id); + } +} + +uint32_t get_fb_for_bo(struct gbm_bo *bo) { + uint32_t id = (uintptr_t)gbm_bo_get_user_data(bo); + if (id) { + return id; + } + + struct gbm_device *gbm = gbm_bo_get_device(bo); + + int fd = gbm_device_get_fd(gbm); + uint32_t width = gbm_bo_get_width(bo); + uint32_t height = gbm_bo_get_height(bo); + uint32_t handles[4] = {gbm_bo_get_handle(bo).u32}; + uint32_t pitches[4] = {gbm_bo_get_stride(bo)}; + uint32_t offsets[4] = {gbm_bo_get_offset(bo, 0)}; + uint32_t format = gbm_bo_get_format(bo); + + if (drmModeAddFB2(fd, width, height, format, handles, pitches, offsets, &id, 0)) { + wlr_log_errno(L_ERROR, "Unable to add DRM framebuffer"); + } + + gbm_bo_set_user_data(bo, (void *)(uintptr_t)id, free_fb); + + return id; +} + static inline bool is_taken(size_t n, const uint32_t arr[static n], uint32_t key) { for (size_t i = 0; i < n; ++i) { if (arr[i] == key) { diff --git a/backend/drm/drm.c b/backend/drm/drm.c index c4834081..1c83af3e 100644 --- a/backend/drm/drm.c +++ b/backend/drm/drm.c @@ -23,28 +23,33 @@ #include "backend/drm-util.h" bool wlr_drm_check_features(struct wlr_backend_state *drm) { + extern const struct wlr_drm_interface legacy_iface; + extern const struct wlr_drm_interface atomic_iface; + if (drmSetClientCap(drm->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1)) { - wlr_log(L_INFO, "DRM universal planes unsupported"); + wlr_log(L_ERROR, "DRM universal planes unsupported"); return false; } if (drmSetClientCap(drm->fd, DRM_CLIENT_CAP_ATOMIC, 1)) { - wlr_log(L_INFO, "Atomic modesetting unsupported"); + wlr_log(L_DEBUG, "Atomic modesetting unsupported, using legacy DRM interface"); + drm->iface = &legacy_iface; + } else { + wlr_log(L_DEBUG, "Using atomic DRM interface"); + drm->iface = &atomic_iface; } return true; } -static int cmp_plane(const void *arg1, const void *arg2) -{ +static int cmp_plane(const void *arg1, const void *arg2) { const struct wlr_drm_plane *a = arg1; const struct wlr_drm_plane *b = arg2; return (int)a->type - (int)b->type; } -static bool init_planes(struct wlr_backend_state *drm) -{ +static bool init_planes(struct wlr_backend_state *drm) { drmModePlaneRes *plane_res = drmModeGetPlaneResources(drm->fd); if (!plane_res) { wlr_log_errno(L_ERROR, "Failed to get DRM plane resources"); @@ -150,6 +155,15 @@ void wlr_drm_resources_free(struct wlr_backend_state *drm) { return; } + for (size_t i = 0; i < drm->num_crtcs; ++i) { + struct wlr_drm_crtc *crtc = &drm->crtcs[i]; + + drmModeAtomicFree(crtc->atomic); + if (crtc->mode_id) { + drmModeDestroyPropertyBlob(drm->fd, crtc->mode_id); + } + } + free(drm->crtcs); free(drm->planes); } @@ -180,7 +194,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, uint32_t flags) { + struct wlr_drm_plane *plane, uint32_t width, uint32_t height, uint32_t format, uint32_t flags) { if (plane->width == width && plane->height == height) { return true; } @@ -189,7 +203,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_ARGB8888, GBM_BO_USE_RENDERING | flags); + format, GBM_BO_USE_RENDERING | flags); if (!plane->gbm) { wlr_log_errno(L_ERROR, "Failed to create GBM surface for plane"); return false; @@ -210,6 +224,8 @@ static void wlr_drm_plane_renderer_free(struct wlr_drm_renderer *renderer, return; } + eglMakeCurrent(renderer->egl.display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); + if (plane->front) { gbm_surface_release_buffer(plane->gbm, plane->front); } @@ -245,30 +261,6 @@ static void wlr_drm_plane_renderer_free(struct wlr_drm_renderer *renderer, plane->cursor_bo = NULL; } -static void free_fb(struct gbm_bo *bo, void *data) { - uint32_t id = (uintptr_t)data; - - if (id) { - struct gbm_device *gbm = gbm_bo_get_device(bo); - drmModeRmFB(gbm_device_get_fd(gbm), id); - } -} - -static uint32_t get_fb_for_bo(struct gbm_bo *bo) { - uint32_t id = (uintptr_t)gbm_bo_get_user_data(bo); - if (id) { - return id; - } - - struct gbm_device *gbm = gbm_bo_get_device(bo); - drmModeAddFB(gbm_device_get_fd(gbm), gbm_bo_get_width(bo), gbm_bo_get_height(bo), - 24, 32, gbm_bo_get_stride(bo), gbm_bo_get_handle(bo).u32, &id); - - gbm_bo_set_user_data(bo, (void *)(uintptr_t)id, free_fb); - - return id; -} - static void wlr_drm_plane_make_current(struct wlr_drm_renderer *renderer, struct wlr_drm_plane *plane) { eglMakeCurrent(renderer->egl.display, plane->egl, plane->egl, @@ -292,14 +284,14 @@ static void wlr_drm_output_make_current(struct wlr_output_state *output) { } static void wlr_drm_output_swap_buffers(struct wlr_output_state *output) { + 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->primary; wlr_drm_plane_swap_buffers(renderer, plane); - uint32_t fb_id = get_fb_for_bo(plane->back); - drmModePageFlip(renderer->fd, crtc->id, fb_id, DRM_MODE_PAGE_FLIP_EVENT, output); + drm->iface->crtc_pageflip(drm, output, crtc, get_fb_for_bo(plane->back), NULL); output->pageflip_pending = true; } @@ -308,8 +300,8 @@ void wlr_drm_output_start_renderer(struct wlr_output_state *output) { return; } + struct wlr_backend_state *drm = wl_container_of(output->renderer, drm, renderer); struct wlr_drm_renderer *renderer = output->renderer; - struct wlr_output_mode *mode = output->base->current_mode; struct wlr_drm_crtc *crtc = output->crtc; struct wlr_drm_plane *plane = crtc->primary; @@ -325,28 +317,22 @@ void wlr_drm_output_start_renderer(struct wlr_output_state *output) { bo = plane->back; } - uint32_t fb_id = get_fb_for_bo(bo); - drmModeSetCrtc(renderer->fd, crtc->id, fb_id, 0, 0, - &output->connector, 1, &mode->state->mode); - drmModePageFlip(renderer->fd, crtc->id, fb_id, DRM_MODE_PAGE_FLIP_EVENT, output); + drmModeModeInfo *mode = &output->base->current_mode->state->mode; + drm->iface->crtc_pageflip(drm, output, crtc, get_fb_for_bo(bo), mode); output->pageflip_pending = true; } static void wlr_drm_output_enable(struct wlr_output_state *output, bool enable) { - struct wlr_backend_state *state = - wl_container_of(output->renderer, state, renderer); + struct wlr_backend_state *drm = + wl_container_of(output->renderer, drm, renderer); if (output->state != WLR_DRM_OUTPUT_CONNECTED) { return; } - if (enable) { - drmModeConnectorSetProperty(state->fd, output->connector, output->props.dpms, - DRM_MODE_DPMS_ON); + drm->iface->conn_enable(drm, output, enable); + if (enable) { wlr_drm_output_start_renderer(output); - } else { - drmModeConnectorSetProperty(state->fd, output->connector, output->props.dpms, - DRM_MODE_DPMS_OFF); } } @@ -515,7 +501,8 @@ static bool wlr_drm_output_set_mode(struct wlr_output_state *output, } if (!wlr_drm_plane_renderer_init(&drm->renderer, crtc->primary, - mode->width, mode->height, GBM_BO_USE_SCANOUT)) { + mode->width, mode->height, GBM_FORMAT_XRGB8888, + GBM_BO_USE_SCANOUT)) { wlr_log(L_ERROR, "Failed to initalise renderer for plane"); goto error_enc; } @@ -541,22 +528,6 @@ 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->cursor_bo).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 *drm = wl_container_of(output->renderer, drm, renderer); @@ -565,10 +536,10 @@ static bool wlr_drm_output_set_cursor(struct wlr_output_state *output, struct wlr_drm_plane *plane = crtc->cursor; if (!buf) { - drmModeSetCursor(drm->fd, crtc->id, 0, 0, 0); - return true; + return drm->iface->crtc_set_cursor(drm, crtc, NULL); } + // We don't have a real cursor plane, so we make a fake one if (!plane) { plane = calloc(1, sizeof(*plane)); if (!plane) { @@ -591,7 +562,7 @@ static bool wlr_drm_output_set_cursor(struct wlr_output_state *output, return false; } - if (!wlr_drm_plane_renderer_init(renderer, plane, w, h, 0)) { + if (!wlr_drm_plane_renderer_init(renderer, plane, w, h, GBM_FORMAT_ARGB8888, 0)) { wlr_log(L_ERROR, "Cannot allocate cursor resources"); return false; } @@ -653,14 +624,14 @@ static bool wlr_drm_output_set_cursor(struct wlr_output_state *output, gbm_bo_unmap(bo, bo_data); - return wlr_drm_crtc_set_cursor(drm, crtc); + return drm->iface->crtc_set_cursor(drm, crtc, bo); } static bool wlr_drm_output_move_cursor(struct wlr_output_state *output, int x, int y) { struct wlr_backend_state *drm = wl_container_of(output->renderer, drm, renderer); - return !drmModeMoveCursor(drm->fd, output->crtc->id, x, y); + return drm->iface->crtc_move_cursor(drm, output->crtc, x, y); } static void wlr_drm_output_destroy(struct wlr_output_state *output) { |