Merge pull request #182 from ascent12/drm-multi-gpu

DRM Multi-GPU
This commit is contained in:
Drew DeVault 2017-10-02 08:46:06 -04:00 committed by GitHub
commit 87a0cb7ba3
22 changed files with 974 additions and 737 deletions

View File

@ -91,15 +91,9 @@ struct wlr_backend *wlr_backend_autocreate(struct wl_display *display) {
return NULL;
}
int gpu = wlr_session_find_gpu(session);
if (gpu == -1) {
wlr_log(L_ERROR, "Failed to open DRM device");
goto error_session;
}
backend = wlr_multi_backend_create(session);
if (!backend) {
goto error_gpu;
goto error_session;
}
struct wlr_backend *libinput = wlr_libinput_backend_create(display, session);
@ -107,21 +101,37 @@ struct wlr_backend *wlr_backend_autocreate(struct wl_display *display) {
goto error_multi;
}
struct wlr_backend *drm = wlr_drm_backend_create(display, session, gpu);
if (!drm) {
goto error_libinput;
wlr_multi_backend_add(backend, libinput);
int gpus[8];
size_t num_gpus = wlr_session_find_gpus(session, 8, gpus);
struct wlr_backend *primary_drm = NULL;
wlr_log(L_INFO, "Found %zu GPUs", num_gpus);
for (size_t i = 0; i < num_gpus; ++i) {
struct wlr_backend *drm = wlr_drm_backend_create(display, session,
gpus[i], primary_drm);
if (!drm) {
wlr_log(L_ERROR, "Failed to open DRM device");
continue;
}
if (!primary_drm) {
primary_drm = drm;
}
wlr_multi_backend_add(backend, drm);
}
if (!primary_drm) {
wlr_log(L_ERROR, "Failed to open any DRM device");
goto error_multi;
}
wlr_multi_backend_add(backend, libinput);
wlr_multi_backend_add(backend, drm);
return backend;
error_libinput:
wlr_backend_destroy(libinput);
error_multi:
wlr_backend_destroy(backend);
error_gpu:
wlr_session_close_file(session, gpu);
error_session:
wlr_session_destroy(session);
return NULL;

View File

@ -2,8 +2,9 @@
#include <xf86drm.h>
#include <xf86drmMode.h>
#include <wlr/util/log.h>
#include "backend/drm.h"
#include "backend/drm-util.h"
#include "backend/drm/drm.h"
#include "backend/drm/iface.h"
#include "backend/drm/util.h"
struct atomic {
drmModeAtomicReq *req;
@ -43,23 +44,23 @@ static bool atomic_end(int drm_fd, struct atomic *atom) {
}
static bool atomic_commit(int drm_fd, struct atomic *atom,
struct wlr_drm_output *output, uint32_t flag, bool modeset) {
struct wlr_drm_connector *conn, uint32_t flag, bool modeset) {
if (atom->failed) {
return false;
}
uint32_t flags = DRM_MODE_PAGE_FLIP_EVENT | flag;
int ret = drmModeAtomicCommit(drm_fd, atom->req, flags, output);
int ret = drmModeAtomicCommit(drm_fd, atom->req, flags, conn);
if (ret) {
wlr_log_errno(L_ERROR, "%s: Atomic commit failed (%s)",
output->output.name, modeset ? "modeset" : "pageflip");
conn->output.name, modeset ? "modeset" : "pageflip");
// Try to commit without new changes
drmModeAtomicSetCursor(atom->req, atom->cursor);
if (drmModeAtomicCommit(drm_fd, atom->req, flags, output)) {
if (drmModeAtomicCommit(drm_fd, atom->req, flags, conn)) {
wlr_log_errno(L_ERROR, "%s: Atomic commit failed (%s)",
output->output.name, modeset ? "modeset" : "pageflip");
conn->output.name, modeset ? "modeset" : "pageflip");
}
}
@ -83,10 +84,10 @@ static void set_plane_props(struct atomic *atom, struct wlr_drm_plane *plane,
// 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->src_w, plane->surf.width << 16);
atomic_add(atom, id, props->src_h, plane->surf.height << 16);
atomic_add(atom, id, props->crtc_w, plane->surf.width);
atomic_add(atom, id, props->crtc_h, plane->surf.height);
atomic_add(atom, id, props->fb_id, fb_id);
atomic_add(atom, id, props->crtc_id, crtc_id);
if (set_crtc_xy) {
@ -95,16 +96,16 @@ static void set_plane_props(struct atomic *atom, struct wlr_drm_plane *plane,
}
}
static bool atomic_crtc_pageflip(struct wlr_drm_backend *backend,
struct wlr_drm_output *output,
static bool atomic_crtc_pageflip(struct wlr_drm_backend *drm,
struct wlr_drm_connector *conn,
struct wlr_drm_crtc *crtc,
uint32_t fb_id, drmModeModeInfo *mode) {
if (mode) {
if (crtc->mode_id) {
drmModeDestroyPropertyBlob(backend->fd, crtc->mode_id);
drmModeDestroyPropertyBlob(drm->fd, crtc->mode_id);
}
if (drmModeCreatePropertyBlob(backend->fd, mode, sizeof(*mode), &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;
}
@ -113,29 +114,29 @@ static bool atomic_crtc_pageflip(struct wlr_drm_backend *backend,
struct atomic atom;
atomic_begin(crtc, &atom);
atomic_add(&atom, output->connector, output->props.crtc_id, crtc->id);
atomic_add(&atom, conn->id, conn->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(backend->fd, &atom, output,
return atomic_commit(drm->fd, &atom, conn,
mode ? DRM_MODE_ATOMIC_ALLOW_MODESET : DRM_MODE_ATOMIC_NONBLOCK,
mode);
}
static void atomic_conn_enable(struct wlr_drm_backend *backend,
struct wlr_drm_output *output, bool enable) {
struct wlr_drm_crtc *crtc = output->crtc;
static void atomic_conn_enable(struct wlr_drm_backend *drm,
struct wlr_drm_connector *conn, bool enable) {
struct wlr_drm_crtc *crtc = conn->crtc;
struct atomic atom;
atomic_begin(crtc, &atom);
atomic_add(&atom, crtc->id, crtc->props.active, enable);
atomic_end(backend->fd, &atom);
atomic_end(drm->fd, &atom);
}
bool legacy_crtc_set_cursor(struct wlr_drm_backend *backend,
bool legacy_crtc_set_cursor(struct wlr_drm_backend *drm,
struct wlr_drm_crtc *crtc, struct gbm_bo *bo);
static bool atomic_crtc_set_cursor(struct wlr_drm_backend *backend,
static bool atomic_crtc_set_cursor(struct wlr_drm_backend *drm,
struct wlr_drm_crtc *crtc, struct gbm_bo *bo) {
if (!crtc || !crtc->cursor) {
return true;
@ -144,7 +145,7 @@ static bool atomic_crtc_set_cursor(struct wlr_drm_backend *backend,
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(backend, crtc, bo);
return legacy_crtc_set_cursor(drm, crtc, bo);
}
struct atomic atom;
@ -158,18 +159,18 @@ static bool atomic_crtc_set_cursor(struct wlr_drm_backend *backend,
atomic_add(&atom, plane->id, plane->props.crtc_id, 0);
}
return atomic_end(backend->fd, &atom);
return atomic_end(drm->fd, &atom);
}
bool legacy_crtc_move_cursor(struct wlr_drm_backend *backend,
bool legacy_crtc_move_cursor(struct wlr_drm_backend *drm,
struct wlr_drm_crtc *crtc, int x, int y);
static bool atomic_crtc_move_cursor(struct wlr_drm_backend *backend,
static bool atomic_crtc_move_cursor(struct wlr_drm_backend *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(backend, crtc, x, y);
return legacy_crtc_move_cursor(drm, crtc, x, y);
}
struct atomic atom;
@ -177,7 +178,7 @@ static bool atomic_crtc_move_cursor(struct wlr_drm_backend *backend,
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(backend->fd, &atom);
return atomic_end(drm->fd, &atom);
}
const struct wlr_drm_interface atomic_iface = {

View File

@ -6,45 +6,45 @@
#include <assert.h>
#include <wayland-server.h>
#include <xf86drm.h>
#include <sys/stat.h>
#include <wlr/backend/session.h>
#include <wlr/backend/interface.h>
#include <wlr/interfaces/wlr_output.h>
#include <wlr/util/list.h>
#include <wlr/util/log.h>
#include <wlr/egl.h>
#include "backend/drm.h"
#include "backend/drm/drm.h"
static bool wlr_drm_backend_start(struct wlr_backend *_backend) {
struct wlr_drm_backend *backend = (struct wlr_drm_backend *)_backend;
wlr_drm_scan_connectors(backend);
static bool wlr_drm_backend_start(struct wlr_backend *backend) {
struct wlr_drm_backend *drm = (struct wlr_drm_backend *)backend;
wlr_drm_scan_connectors(drm);
return true;
}
static void wlr_drm_backend_destroy(struct wlr_backend *_backend) {
if (!_backend) {
static void wlr_drm_backend_destroy(struct wlr_backend *backend) {
if (!backend) {
return;
}
struct wlr_drm_backend *backend = (struct wlr_drm_backend *)_backend;
wlr_drm_restore_outputs(backend);
struct wlr_drm_backend *drm = (struct wlr_drm_backend *)backend;
for (size_t i = 0; backend->outputs && i < backend->outputs->length; ++i) {
struct wlr_drm_output *output = backend->outputs->items[i];
wlr_output_destroy(&output->output);
wlr_drm_restore_outputs(drm);
for (size_t i = 0; drm->outputs && i < drm->outputs->length; ++i) {
struct wlr_drm_connector *conn = drm->outputs->items[i];
wlr_output_destroy(&conn->output);
}
wlr_drm_renderer_free(&backend->renderer);
wlr_drm_resources_free(backend);
wlr_session_close_file(backend->session, backend->fd);
wl_event_source_remove(backend->drm_event);
list_free(backend->outputs);
free(backend);
wlr_drm_renderer_finish(&drm->renderer);
wlr_drm_resources_free(drm);
wlr_session_close_file(drm->session, drm->fd);
wl_event_source_remove(drm->drm_event);
list_free(drm->outputs);
free(drm);
}
static struct wlr_egl *wlr_drm_backend_get_egl(struct wlr_backend *_backend) {
struct wlr_drm_backend *backend = (struct wlr_drm_backend *)_backend;
return &backend->renderer.egl;
static struct wlr_egl *wlr_drm_backend_get_egl(struct wlr_backend *backend) {
struct wlr_drm_backend *drm = (struct wlr_drm_backend *)backend;
return &drm->renderer.egl;
}
static struct wlr_backend_impl backend_impl = {
@ -58,23 +58,22 @@ bool wlr_backend_is_drm(struct wlr_backend *b) {
}
static void session_signal(struct wl_listener *listener, void *data) {
struct wlr_drm_backend *backend =
wl_container_of(listener, backend, session_signal);
struct wlr_drm_backend *drm = wl_container_of(listener, drm, session_signal);
struct wlr_session *session = data;
if (session->active) {
wlr_log(L_INFO, "DRM fd resumed");
for (size_t i = 0; i < backend->outputs->length; ++i) {
struct wlr_drm_output *output = backend->outputs->items[i];
wlr_drm_output_start_renderer(output);
for (size_t i = 0; i < drm->outputs->length; ++i) {
struct wlr_drm_connector *conn = drm->outputs->items[i];
wlr_drm_connector_start_renderer(conn);
if (!output->crtc) {
if (!conn->crtc) {
continue;
}
struct wlr_drm_plane *plane = output->crtc->cursor;
backend->iface->crtc_set_cursor(backend, output->crtc,
struct wlr_drm_plane *plane = conn->crtc->cursor;
drm->iface->crtc_set_cursor(drm, conn->crtc,
plane ? plane->cursor_bo : NULL);
}
} else {
@ -83,19 +82,19 @@ static void session_signal(struct wl_listener *listener, void *data) {
}
static void drm_invalidated(struct wl_listener *listener, void *data) {
struct wlr_drm_backend *backend =
wl_container_of(listener, backend, drm_invalidated);
struct wlr_drm_backend *drm = wl_container_of(listener, drm, drm_invalidated);
char *name = drmGetDeviceNameFromFd2(backend->fd);
char *name = drmGetDeviceNameFromFd2(drm->fd);
wlr_log(L_DEBUG, "%s invalidated", name);
free(name);
wlr_drm_scan_connectors(backend);
wlr_drm_scan_connectors(drm);
}
struct wlr_backend *wlr_drm_backend_create(struct wl_display *display,
struct wlr_session *session, int gpu_fd) {
struct wlr_session *session, int gpu_fd, struct wlr_backend *parent) {
assert(display && session && gpu_fd >= 0);
assert(!parent || wlr_backend_is_drm(parent));
char *name = drmGetDeviceNameFromFd2(gpu_fd);
drmVersion *version = drmGetVersion(gpu_fd);
@ -103,69 +102,64 @@ struct wlr_backend *wlr_drm_backend_create(struct wl_display *display,
free(name);
drmFreeVersion(version);
struct wlr_drm_backend *backend = calloc(1, sizeof(struct wlr_drm_backend));
if (!backend) {
struct wlr_drm_backend *drm = calloc(1, sizeof(struct wlr_drm_backend));
if (!drm) {
wlr_log_errno(L_ERROR, "Allocation failed");
return NULL;
}
wlr_backend_init(&backend->backend, &backend_impl);
wlr_backend_init(&drm->backend, &backend_impl);
backend->session = session;
backend->outputs = list_create();
if (!backend->outputs) {
drm->session = session;
drm->outputs = list_create();
if (!drm->outputs) {
wlr_log(L_ERROR, "Failed to allocate list");
goto error_backend;
}
backend->fd = gpu_fd;
drm->fd = gpu_fd;
drm->parent = (struct wlr_drm_backend *)parent;
struct stat st;
if (fstat(backend->fd, &st) < 0) {
wlr_log_errno(L_ERROR, "Stat failed");
}
backend->dev = st.st_rdev;
drm->drm_invalidated.notify = drm_invalidated;
wlr_session_signal_add(session, gpu_fd, &drm->drm_invalidated);
backend->drm_invalidated.notify = drm_invalidated;
wlr_session_signal_add(session, gpu_fd, &backend->drm_invalidated);
backend->display = display;
drm->display = display;
struct wl_event_loop *event_loop = wl_display_get_event_loop(display);
backend->drm_event = wl_event_loop_add_fd(event_loop, backend->fd,
drm->drm_event = wl_event_loop_add_fd(event_loop, drm->fd,
WL_EVENT_READABLE, wlr_drm_event, NULL);
if (!backend->drm_event) {
if (!drm->drm_event) {
wlr_log(L_ERROR, "Failed to create DRM event source");
goto error_fd;
}
backend->session_signal.notify = session_signal;
wl_signal_add(&session->session_signal, &backend->session_signal);
drm->session_signal.notify = session_signal;
wl_signal_add(&session->session_signal, &drm->session_signal);
if (!wlr_drm_check_features(backend)) {
if (!wlr_drm_check_features(drm)) {
goto error_event;
}
if (!wlr_drm_resources_init(backend)) {
if (!wlr_drm_resources_init(drm)) {
goto error_event;
}
if (!wlr_drm_renderer_init(&backend->renderer, backend->fd)) {
if (!wlr_drm_renderer_init(drm, &drm->renderer)) {
wlr_log(L_ERROR, "Failed to initialize renderer");
goto error_event;
}
if (!wlr_egl_bind_display(&backend->renderer.egl, display)) {
if (!wlr_egl_bind_display(&drm->renderer.egl, display)) {
wlr_log(L_INFO, "Failed to bind egl/wl display: %s", egl_error());
}
return &backend->backend;
return &drm->backend;
error_event:
wl_event_source_remove(backend->drm_event);
wl_event_source_remove(drm->drm_event);
error_fd:
wlr_session_close_file(backend->session, backend->fd);
list_free(backend->outputs);
wlr_session_close_file(drm->session, drm->fd);
list_free(drm->outputs);
error_backend:
free(backend);
free(drm);
return NULL;
}

View File

@ -1,65 +0,0 @@
#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_drm_backend *backend,
struct wlr_drm_output *output, struct wlr_drm_crtc *crtc,
uint32_t fb_id, drmModeModeInfo *mode) {
if (mode) {
if (drmModeSetCrtc(backend->fd, crtc->id, fb_id, 0, 0,
&output->connector, 1, mode)) {
wlr_log_errno(L_ERROR, "%s: Failed to set CRTC", output->output.name);
return false;
}
}
if (drmModePageFlip(backend->fd, crtc->id, fb_id, DRM_MODE_PAGE_FLIP_EVENT, output)) {
wlr_log_errno(L_ERROR, "%s: Failed to page flip", output->output.name);
return false;
}
return true;
}
static void legacy_conn_enable(struct wlr_drm_backend *backend,
struct wlr_drm_output *output, bool enable) {
drmModeConnectorSetProperty(backend->fd, output->connector, output->props.dpms,
enable ? DRM_MODE_DPMS_ON : DRM_MODE_DPMS_OFF);
}
bool legacy_crtc_set_cursor(struct wlr_drm_backend *backend,
struct wlr_drm_crtc *crtc, struct gbm_bo *bo) {
if (!crtc || !crtc->cursor) {
return true;
}
if (!bo) {
drmModeSetCursor(backend->fd, crtc->id, 0, 0, 0);
return true;
}
struct wlr_drm_plane *plane = crtc->cursor;
if (drmModeSetCursor(backend->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_drm_backend *backend,
struct wlr_drm_crtc *crtc, int x, int y) {
return !drmModeMoveCursor(backend->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,
};

File diff suppressed because it is too large Load Diff

66
backend/drm/legacy.c Normal file
View File

@ -0,0 +1,66 @@
#include <gbm.h>
#include <xf86drm.h>
#include <xf86drmMode.h>
#include <wlr/util/log.h>
#include "backend/drm/drm.h"
#include "backend/drm/iface.h"
#include "backend/drm/util.h"
static bool legacy_crtc_pageflip(struct wlr_drm_backend *drm,
struct wlr_drm_connector *conn, struct wlr_drm_crtc *crtc,
uint32_t fb_id, drmModeModeInfo *mode) {
if (mode) {
if (drmModeSetCrtc(drm->fd, crtc->id, fb_id, 0, 0,
&conn->id, 1, mode)) {
wlr_log_errno(L_ERROR, "%s: Failed to set CRTC", conn->output.name);
return false;
}
}
if (drmModePageFlip(drm->fd, crtc->id, fb_id, DRM_MODE_PAGE_FLIP_EVENT, conn)) {
wlr_log_errno(L_ERROR, "%s: Failed to page flip", conn->output.name);
return false;
}
return true;
}
static void legacy_conn_enable(struct wlr_drm_backend *drm,
struct wlr_drm_connector *conn, bool enable) {
drmModeConnectorSetProperty(drm->fd, conn->id, conn->props.dpms,
enable ? DRM_MODE_DPMS_ON : DRM_MODE_DPMS_OFF);
}
bool legacy_crtc_set_cursor(struct wlr_drm_backend *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->surf.width, plane->surf.height)) {
wlr_log_errno(L_ERROR, "Failed to set hardware cursor");
return false;
}
return true;
}
bool legacy_crtc_move_cursor(struct wlr_drm_backend *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,
};

View File

@ -5,7 +5,7 @@
#include <xf86drm.h>
#include <xf86drmMode.h>
#include <wlr/util/log.h>
#include "backend/drm-properties.h"
#include "backend/drm/properties.h"
/*
* Creates a mapping between property names and an array index where to store

250
backend/drm/renderer.c Normal file
View File

@ -0,0 +1,250 @@
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include <gbm.h>
#include <GLES2/gl2.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <wayland-util.h>
#include <wlr/egl.h>
#include <wlr/util/log.h>
#include <wlr/render/matrix.h>
#include <wlr/render/gles2.h>
#include <wlr/render.h>
#include "backend/drm/drm.h"
bool wlr_drm_renderer_init(struct wlr_drm_backend *drm,
struct wlr_drm_renderer *renderer) {
renderer->gbm = gbm_create_device(drm->fd);
if (!renderer->gbm) {
wlr_log(L_ERROR, "Failed to create GBM device");
return false;
}
if (!wlr_egl_init(&renderer->egl, EGL_PLATFORM_GBM_MESA,
GBM_FORMAT_ARGB8888, renderer->gbm)) {
goto error_gbm;
}
renderer->wlr_rend = wlr_gles2_renderer_create(&drm->backend);
if (!renderer->wlr_rend) {
wlr_log(L_ERROR, "Failed to create WLR renderer");
goto error_egl;
}
renderer->fd = drm->fd;
return true;
error_egl:
wlr_egl_free(&renderer->egl);
error_gbm:
gbm_device_destroy(renderer->gbm);
return false;
}
void wlr_drm_renderer_finish(struct wlr_drm_renderer *renderer) {
if (!renderer) {
return;
}
wlr_renderer_destroy(renderer->wlr_rend);
wlr_egl_free(&renderer->egl);
gbm_device_destroy(renderer->gbm);
}
bool wlr_drm_surface_init(struct wlr_drm_surface *surf,
struct wlr_drm_renderer *renderer, uint32_t width, uint32_t height,
uint32_t format, uint32_t flags) {
if (surf->width == width && surf->height == height) {
return true;
}
surf->renderer = renderer;
surf->width = width;
surf->height = height;
surf->gbm = gbm_surface_create(renderer->gbm, width, height,
format, GBM_BO_USE_RENDERING | flags);
if (!surf->gbm) {
wlr_log_errno(L_ERROR, "Failed to create GBM surface");
goto error_zero;
}
surf->egl = wlr_egl_create_surface(&renderer->egl, surf->gbm);
if (surf->egl == EGL_NO_SURFACE) {
wlr_log(L_ERROR, "Failed to create EGL surface");
goto error_gbm;
}
return true;
error_gbm:
gbm_surface_destroy(surf->gbm);
error_zero:
memset(surf, 0, sizeof(*surf));
return false;
}
void wlr_drm_surface_finish(struct wlr_drm_surface *surf) {
if (!surf || !surf->renderer) {
return;
}
eglMakeCurrent(surf->renderer->egl.display, EGL_NO_SURFACE, EGL_NO_SURFACE,
EGL_NO_CONTEXT);
if (surf->front) {
gbm_surface_release_buffer(surf->gbm, surf->front);
}
if (surf->back) {
gbm_surface_release_buffer(surf->gbm, surf->back);
}
if (surf->egl) {
eglDestroySurface(surf->renderer->egl.display, surf->egl);
}
if (surf->gbm) {
gbm_surface_destroy(surf->gbm);
}
memset(surf, 0, sizeof(*surf));
}
void wlr_drm_surface_make_current(struct wlr_drm_surface *surf) {
eglMakeCurrent(surf->renderer->egl.display, surf->egl, surf->egl,
surf->renderer->egl.context);
}
struct gbm_bo *wlr_drm_surface_swap_buffers(struct wlr_drm_surface *surf) {
if (surf->front) {
gbm_surface_release_buffer(surf->gbm, surf->front);
}
eglSwapBuffers(surf->renderer->egl.display, surf->egl);
surf->front = surf->back;
surf->back = gbm_surface_lock_front_buffer(surf->gbm);
return surf->back;
}
struct gbm_bo *wlr_drm_surface_get_front(struct wlr_drm_surface *surf) {
if (surf->front) {
return surf->front;
}
wlr_drm_surface_make_current(surf);
glViewport(0, 0, surf->width, surf->height);
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
return wlr_drm_surface_swap_buffers(surf);
}
void wlr_drm_surface_post(struct wlr_drm_surface *surf) {
if (surf->front) {
gbm_surface_release_buffer(surf->gbm, surf->front);
surf->front = NULL;
}
}
struct tex {
struct wlr_egl *egl;
EGLImageKHR img;
struct wlr_texture *tex;
};
static void free_eglimage(struct gbm_bo *bo, void *data) {
struct tex *tex = data;
wlr_egl_destroy_image(tex->egl, tex->img);
wlr_texture_destroy(tex->tex);
free(tex);
}
static struct wlr_texture *get_tex_for_bo(struct wlr_drm_renderer *renderer, struct gbm_bo *bo) {
struct tex *tex = gbm_bo_get_user_data(bo);
if (tex) {
return tex->tex;
}
tex = malloc(sizeof(*tex));
if (!tex) {
wlr_log_errno(L_ERROR, "Allocation failed");
return NULL;
}
tex->egl = &renderer->egl;
int dmabuf_fd = gbm_bo_get_fd(bo);
uint32_t width = gbm_bo_get_width(bo);
uint32_t height = gbm_bo_get_height(bo);
EGLint attribs[] = {
EGL_WIDTH, width,
EGL_HEIGHT, height,
EGL_LINUX_DRM_FOURCC_EXT, gbm_bo_get_format(bo),
EGL_DMA_BUF_PLANE0_FD_EXT, dmabuf_fd,
EGL_DMA_BUF_PLANE0_OFFSET_EXT, gbm_bo_get_offset(bo, 0),
EGL_DMA_BUF_PLANE0_PITCH_EXT, gbm_bo_get_stride_for_plane(bo, 0),
EGL_IMAGE_PRESERVED_KHR, EGL_FALSE,
EGL_NONE,
};
tex->img = renderer->egl.eglCreateImageKHR(renderer->egl.display, EGL_NO_CONTEXT,
EGL_LINUX_DMA_BUF_EXT, NULL, attribs);
if (!tex->img) {
wlr_log(L_ERROR, "Failed to create EGL image: %s", egl_error());
abort();
}
tex->tex = wlr_render_texture_create(renderer->wlr_rend);
wlr_texture_upload_eglimage(tex->tex, tex->img, width, height);
gbm_bo_set_user_data(bo, tex, free_eglimage);
return tex->tex;
}
struct gbm_bo *wlr_drm_surface_mgpu_copy(struct wlr_drm_surface *dest, struct gbm_bo *src) {
wlr_drm_surface_make_current(dest);
struct wlr_texture *tex = get_tex_for_bo(dest->renderer, src);
static const float matrix[16] = {
[0] = 2.0f,
[3] = -1.0f,
[5] = 2.0f,
[7] = -1.0f,
[10] = 1.0f,
[15] = 1.0f,
};
glViewport(0, 0, dest->width, dest->height);
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
wlr_render_with_matrix(dest->renderer->wlr_rend, tex, &matrix);
return wlr_drm_surface_swap_buffers(dest);
}
bool wlr_drm_plane_surfaces_init(struct wlr_drm_plane *plane, struct wlr_drm_backend *drm,
int32_t width, uint32_t height, uint32_t format) {
if (!drm->parent) {
return wlr_drm_surface_init(&plane->surf, &drm->renderer, width, height,
format, GBM_BO_USE_SCANOUT);
}
if (!wlr_drm_surface_init(&plane->surf, &drm->parent->renderer,
width, height, format, GBM_BO_USE_LINEAR)) {
return false;
}
if (!wlr_drm_surface_init(&plane->mgpu_surf, &drm->renderer,
width, height, format, GBM_BO_USE_SCANOUT)) {
wlr_drm_surface_finish(&plane->surf);
return false;
}
return true;
}

View File

@ -3,7 +3,7 @@
#include <drm.h>
#include <drm_mode.h>
#include <gbm.h>
#include "backend/drm-util.h"
#include "backend/drm/util.h"
#include <wlr/util/log.h>
int32_t calculate_refresh_rate(drmModeModeInfo *mode) {

View File

@ -3,12 +3,13 @@ backend_files = files(
'session/direct-ipc.c',
'session/direct.c',
'session/session.c',
'drm/atomic.c',
'drm/backend.c',
'drm/drm.c',
'drm/drm-atomic.c',
'drm/drm-legacy.c',
'drm/drm-properties.c',
'drm/drm-util.c',
'drm/legacy.c',
'drm/properties.c',
'drm/renderer.c',
'drm/util.c',
'libinput/backend.c',
'libinput/events.c',
'libinput/keyboard.c',

View File

@ -1,7 +1,8 @@
#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <stddef.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <libudev.h>
@ -191,18 +192,16 @@ bool wlr_session_change_vt(struct wlr_session *session, unsigned vt) {
/* Tests if 'path' is KMS compatible by trying to open it.
* It leaves the open device in *fd_out it it succeeds.
*/
static bool device_is_kms(struct wlr_session *restrict session,
const char *restrict path, int *restrict fd_out) {
static int open_if_kms(struct wlr_session *restrict session, const char *restrict path) {
int fd;
if (!path) {
return false;
return -1;
}
fd = wlr_session_open_file(session, path);
if (fd < 0) {
return false;
return -1;
}
drmModeRes *res = drmModeGetResources(fd);
@ -216,26 +215,54 @@ static bool device_is_kms(struct wlr_session *restrict session,
goto out_res;
}
if (*fd_out >= 0) {
wlr_session_close_file(session, *fd_out);
}
*fd_out = fd;
drmModeFreeResources(res);
return true;
return fd;
out_res:
drmModeFreeResources(res);
out_fd:
wlr_session_close_file(session, fd);
return false;
return -1;
}
static size_t explicit_find_gpus(struct wlr_session *session,
size_t ret_len, int ret[static ret_len], const char *str) {
char *gpus = strdup(str);
if (!gpus) {
wlr_log_errno(L_ERROR, "Allocation failed");
return 0;
}
size_t i = 0;
char *save;
char *ptr = strtok_r(gpus, ":", &save);
do {
if (i >= ret_len) {
break;
}
ret[i] = open_if_kms(session, ptr);
if (ret[i] < 0) {
wlr_log(L_ERROR, "Unable to open %s as DRM device", ptr);
} else {
++i;
}
} while ((ptr = strtok_r(NULL, ":", &save)));
free(ptr);
return i;
}
/* Tries to find the primary GPU by checking for the "boot_vga" attribute.
* If it's not found, it returns the first valid GPU it finds.
*/
int wlr_session_find_gpu(struct wlr_session *session) {
size_t wlr_session_find_gpus(struct wlr_session *session,
size_t ret_len, int ret[static ret_len]) {
const char *explicit = getenv("WLR_DRM_DEVICES");
if (explicit) {
return explicit_find_gpus(session, ret_len, ret, explicit);
}
struct udev_enumerate *en = udev_enumerate_new(session->udev);
if (!en) {
wlr_log(L_ERROR, "Failed to create udev enumeration");
@ -247,9 +274,13 @@ int wlr_session_find_gpu(struct wlr_session *session) {
udev_enumerate_scan_devices(en);
struct udev_list_entry *entry;
int fd = -1;
size_t i = 0;
udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(en)) {
if (i == ret_len) {
break;
}
bool is_boot_vga = false;
const char *path = udev_list_entry_get_name(entry);
@ -258,15 +289,13 @@ int wlr_session_find_gpu(struct wlr_session *session) {
continue;
}
/*
const char *seat = udev_device_get_property_value(dev, "ID_SEAT");
if (!seat)
seat = "seat0";
if (strcmp(session->seat, seat) != 0) {
if (session->seat[0] && strcmp(session->seat, seat) != 0) {
udev_device_unref(dev);
continue;
}
*/
// This is owned by 'dev', so we don't need to free it
struct udev_device *pci =
@ -279,27 +308,25 @@ int wlr_session_find_gpu(struct wlr_session *session) {
}
}
// We already have a valid GPU
if (!is_boot_vga && fd >= 0) {
udev_device_unref(dev);
continue;
}
path = udev_device_get_devnode(dev);
if (!device_is_kms(session, path, &fd)) {
int fd = open_if_kms(session, udev_device_get_devnode(dev));
if (fd < 0) {
udev_device_unref(dev);
continue;
}
udev_device_unref(dev);
// We've found the primary GPU
ret[i] = fd;
if (is_boot_vga) {
break;
int tmp = ret[0];
ret[0] = ret[i];
ret[i] = tmp;
}
++i;
}
udev_enumerate_unref(en);
return fd;
return i;
}

View File

@ -1,5 +1,5 @@
#ifndef BACKEND_DRM_H
#define BACKEND_DRM_H
#ifndef BACKEND_DRM_DRM_H
#define BACKEND_DRM_DRM_H
#include <stdbool.h>
#include <stddef.h>
@ -15,7 +15,9 @@
#include <wlr/egl.h>
#include <wlr/util/list.h>
#include "drm-properties.h"
#include "iface.h"
#include "properties.h"
#include "renderer.h"
struct wlr_drm_plane {
uint32_t type;
@ -23,13 +25,8 @@ struct wlr_drm_plane {
uint32_t possible_crtcs;
uint32_t width, height;
struct gbm_surface *gbm;
EGLSurface egl;
struct gbm_bo *front;
struct gbm_bo *back;
struct wlr_drm_surface surf;
struct wlr_drm_surface mgpu_surf;
// Only used by cursor
float matrix[16];
@ -59,34 +56,13 @@ struct wlr_drm_crtc {
struct wl_list connectors;
};
struct wlr_drm_connector {
struct wlr_output *base;
uint32_t id;
struct wlr_drm_crtc *crtc;
union wlr_drm_connector_props props;
struct wl_list link;
};
struct wlr_drm_renderer {
int fd;
struct gbm_device *gbm;
struct wlr_egl egl;
};
bool wlr_drm_renderer_init(struct wlr_drm_renderer *renderer, int fd);
void wlr_drm_renderer_free(struct wlr_drm_renderer *renderer);
struct wlr_drm_interface;
struct wlr_drm_backend {
struct wlr_backend backend;
struct wlr_drm_backend *parent;
const struct wlr_drm_interface *iface;
int fd;
dev_t dev;
size_t num_crtcs;
struct wlr_drm_crtc *crtcs;
@ -117,30 +93,30 @@ struct wlr_drm_backend {
struct wl_listener session_signal;
struct wl_listener drm_invalidated;
uint32_t taken_crtcs;
list_t *outputs;
struct wlr_drm_renderer renderer;
struct wlr_session *session;
};
enum wlr_drm_output_state {
WLR_DRM_OUTPUT_DISCONNECTED,
WLR_DRM_OUTPUT_NEEDS_MODESET,
WLR_DRM_OUTPUT_CLEANUP,
WLR_DRM_OUTPUT_CONNECTED,
enum wlr_drm_connector_state {
WLR_DRM_CONN_DISCONNECTED,
WLR_DRM_CONN_NEEDS_MODESET,
WLR_DRM_CONN_CLEANUP,
WLR_DRM_CONN_CONNECTED,
};
struct wlr_drm_output_mode {
struct wlr_drm_mode {
struct wlr_output_mode wlr_mode;
drmModeModeInfo mode;
drmModeModeInfo drm_mode;
};
struct wlr_drm_output {
struct wlr_drm_connector {
struct wlr_output output;
struct wlr_drm_backend *drm;
enum wlr_drm_output_state state;
uint32_t connector;
enum wlr_drm_connector_state state;
uint32_t id;
struct wlr_drm_crtc *crtc;
uint32_t possible_crtc;
@ -152,37 +128,18 @@ struct wlr_drm_output {
drmModeCrtc *old_crtc;
struct wlr_drm_renderer *renderer;
bool pageflip_pending;
struct wl_event_source *retry_pageflip;
};
// Used to provide atomic or legacy DRM functions
struct wlr_drm_interface {
// Enable or disable DPMS for output
void (*conn_enable)(struct wlr_drm_backend *backend,
struct wlr_drm_output *output, bool enable);
// Pageflip on crtc. If mode is non-NULL perform a full modeset using it.
bool (*crtc_pageflip)(struct wlr_drm_backend *backend,
struct wlr_drm_output *output, struct wlr_drm_crtc *crtc,
uint32_t fb_id, drmModeModeInfo *mode);
// Enable the cursor buffer on crtc. Set bo to NULL to disable
bool (*crtc_set_cursor)(struct wlr_drm_backend *backend,
struct wlr_drm_crtc *crtc, struct gbm_bo *bo);
// Move the cursor on crtc
bool (*crtc_move_cursor)(struct wlr_drm_backend *backend,
struct wlr_drm_crtc *crtc, int x, int y);
};
bool wlr_drm_check_features(struct wlr_drm_backend *drm);
bool wlr_drm_resources_init(struct wlr_drm_backend *drm);
void wlr_drm_resources_free(struct wlr_drm_backend *drm);
void wlr_drm_restore_outputs(struct wlr_drm_backend *drm);
void wlr_drm_output_cleanup(struct wlr_drm_output *output);
void wlr_drm_connector_cleanup(struct wlr_drm_connector *conn);
void wlr_drm_scan_connectors(struct wlr_drm_backend *state);
int wlr_drm_event(int fd, uint32_t mask, void *data);
void wlr_drm_output_start_renderer(struct wlr_drm_output *output);
void wlr_drm_connector_start_renderer(struct wlr_drm_connector *conn);
#endif

View File

@ -0,0 +1,35 @@
#ifndef BACKEND_DRM_IFACE_H
#define BACKEND_DRM_IFACE_H
#include <stdbool.h>
#include <stdint.h>
#include <gbm.h>
#include <xf86drm.h>
#include <xf86drmMode.h>
struct wlr_drm_backend;
struct wlr_drm_connector;
struct wlr_drm_crtc;
// Used to provide atomic or legacy DRM functions
struct wlr_drm_interface {
// Enable or disable DPMS for connector
void (*conn_enable)(struct wlr_drm_backend *drm,
struct wlr_drm_connector *conn, bool enable);
// Pageflip on crtc. If mode is non-NULL perform a full modeset using it.
bool (*crtc_pageflip)(struct wlr_drm_backend *drm,
struct wlr_drm_connector *conn, struct wlr_drm_crtc *crtc,
uint32_t fb_id, drmModeModeInfo *mode);
// Enable the cursor buffer on crtc. Set bo to NULL to disable
bool (*crtc_set_cursor)(struct wlr_drm_backend *drm,
struct wlr_drm_crtc *crtc, struct gbm_bo *bo);
// Move the cursor on crtc
bool (*crtc_move_cursor)(struct wlr_drm_backend *drm,
struct wlr_drm_crtc *crtc, int x, int y);
};
extern const struct wlr_drm_interface atomic_iface;
extern const struct wlr_drm_interface legacy_iface;
#endif

View File

@ -0,0 +1,54 @@
#ifndef BACKEND_DRM_RENDERER_H
#define BACKEND_DRM_RENDERER_H
#include <stdbool.h>
#include <stdint.h>
#include <EGL/egl.h>
#include <gbm.h>
#include <wlr/render.h>
struct wlr_drm_backend;
struct wlr_drm_plane;
struct wlr_drm_renderer {
int fd;
struct gbm_device *gbm;
struct wlr_egl egl;
struct wlr_renderer *wlr_rend;
};
struct wlr_drm_surface {
struct wlr_drm_renderer *renderer;
uint32_t width;
uint32_t height;
struct gbm_surface *gbm;
EGLSurface egl;
struct gbm_bo *front;
struct gbm_bo *back;
};
bool wlr_drm_renderer_init(struct wlr_drm_backend *drm,
struct wlr_drm_renderer *renderer);
void wlr_drm_renderer_finish(struct wlr_drm_renderer *renderer);
bool wlr_drm_surface_init(struct wlr_drm_surface *surf,
struct wlr_drm_renderer *renderer, uint32_t width, uint32_t height,
uint32_t format, uint32_t flags);
bool wlr_drm_plane_surfaces_init(struct wlr_drm_plane *plane, struct wlr_drm_backend *drm,
int32_t width, uint32_t height, uint32_t format);
void wlr_drm_surface_finish(struct wlr_drm_surface *surf);
void wlr_drm_surface_make_current(struct wlr_drm_surface *surf);
struct gbm_bo *wlr_drm_surface_swap_buffers(struct wlr_drm_surface *surf);
struct gbm_bo *wlr_drm_surface_get_front(struct wlr_drm_surface *surf);
void wlr_drm_surface_post(struct wlr_drm_surface *surf);
struct gbm_bo *wlr_drm_surface_mgpu_copy(struct wlr_drm_surface *dest, struct gbm_bo *src);
#endif

View File

@ -6,7 +6,7 @@
#include <wlr/backend.h>
struct wlr_backend *wlr_drm_backend_create(struct wl_display *display,
struct wlr_session *session, int gpu_fd);
struct wlr_session *session, int gpu_fd, struct wlr_backend *parent);
bool wlr_backend_is_drm(struct wlr_backend *backend);

View File

@ -72,12 +72,13 @@ int wlr_session_open_file(struct wlr_session *session, const char *path);
void wlr_session_close_file(struct wlr_session *session, int fd);
void wlr_session_signal_add(struct wlr_session *session, int fd,
struct wl_listener *listener);
struct wl_listener *listener);
/*
* Changes the virtual terminal.
*/
bool wlr_session_change_vt(struct wlr_session *session, unsigned vt);
int wlr_session_find_gpu(struct wlr_session *session);
size_t wlr_session_find_gpus(struct wlr_session *session,
size_t ret_len, int ret[static ret_len]);
#endif

View File

@ -2,6 +2,8 @@
#define WLR_RENDER_H
#include <stdint.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <wayland-server-protocol.h>
#include <wlr/types/wlr_output.h>
@ -93,8 +95,11 @@ bool wlr_texture_upload_shm(struct wlr_texture *tex, uint32_t format,
* texture. The wl_resource is not used after this call.
* Will fail (return false) if the given resource is no drm buffer.
*/
bool wlr_texture_upload_drm(struct wlr_texture *tex,
struct wl_resource *drm_buffer);
bool wlr_texture_upload_drm(struct wlr_texture *tex,
struct wl_resource *drm_buffer);
bool wlr_texture_upload_eglimage(struct wlr_texture *tex,
EGLImageKHR image, uint32_t width, uint32_t height);
/**
* Copies a rectangle of pixels from a wl_shm_buffer onto the texture. The

View File

@ -2,6 +2,8 @@
#define WLR_RENDER_INTERFACE_H
#include <wayland-server-protocol.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <stdbool.h>
#include <wlr/render.h>
#include <wlr/types/wlr_output.h>
@ -45,6 +47,8 @@ struct wlr_texture_impl {
int x, int y, int width, int height, struct wl_shm_buffer *shm);
bool (*upload_drm)(struct wlr_texture *texture,
struct wl_resource *drm_buf);
bool (*upload_eglimage)(struct wlr_texture *texture, EGLImageKHR image,
uint32_t width, uint32_t height);
void (*get_matrix)(struct wlr_texture *state,
float (*matrix)[16], const float (*projection)[16], int x, int y);
void (*get_buffer_size)(struct wlr_texture *texture,

View File

@ -205,6 +205,25 @@ static bool gles2_texture_upload_drm(struct wlr_texture *_tex,
return true;
}
static bool gles2_texture_upload_eglimage(struct wlr_texture *wlr_tex,
EGLImageKHR image, uint32_t width, uint32_t height) {
struct wlr_gles2_texture *tex = (struct wlr_gles2_texture *)wlr_tex;
tex->image = image;
tex->pixel_format = &external_pixel_format;
tex->wlr_texture.valid = true;
tex->wlr_texture.width = width;
tex->wlr_texture.height = height;
gles2_texture_ensure_texture(tex);
GL_CALL(glActiveTexture(GL_TEXTURE0));
GL_CALL(glBindTexture(GL_TEXTURE_EXTERNAL_OES, tex->tex_id));
GL_CALL(glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, tex->image));
return true;
}
static void gles2_texture_get_matrix(struct wlr_texture *_texture,
float (*matrix)[16], const float (*projection)[16], int x, int y) {
struct wlr_gles2_texture *texture = (struct wlr_gles2_texture *)_texture;
@ -270,6 +289,7 @@ static struct wlr_texture_impl wlr_texture_impl = {
.upload_shm = gles2_texture_upload_shm,
.update_shm = gles2_texture_update_shm,
.upload_drm = gles2_texture_upload_drm,
.upload_eglimage = gles2_texture_upload_eglimage,
.get_matrix = gles2_texture_get_matrix,
.get_buffer_size = gles2_texture_get_buffer_size,
.bind = gles2_texture_bind,

View File

@ -48,6 +48,11 @@ bool wlr_texture_upload_drm(struct wlr_texture *texture,
return texture->impl->upload_drm(texture, drm_buffer);
}
bool wlr_texture_upload_eglimage(struct wlr_texture *texture,
EGLImageKHR image, uint32_t width, uint32_t height) {
return texture->impl->upload_eglimage(texture, image, width, height);
}
void wlr_texture_get_matrix(struct wlr_texture *texture,
float (*matrix)[16], const float (*projection)[16], int x, int y) {
texture->impl->get_matrix(texture, matrix, projection, x, y);