wlroots-hyprland/backend/drm/drm.c

443 lines
11 KiB
C
Raw Normal View History

2017-05-01 05:20:48 +02:00
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <errno.h>
#include <xf86drm.h>
#include <xf86drmMode.h>
#include <drm_mode.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <gbm.h>
#include <GLES3/gl3.h>
#include <wayland-server.h>
2017-05-01 05:20:48 +02:00
2017-05-02 03:00:25 +02:00
#include "backend/drm/backend.h"
#include "backend/drm/drm.h"
#include "common/log.h"
2017-05-01 05:20:48 +02:00
static const char *conn_name[] = {
[DRM_MODE_CONNECTOR_Unknown] = "Unknown",
[DRM_MODE_CONNECTOR_VGA] = "VGA",
[DRM_MODE_CONNECTOR_DVII] = "DVI-I",
[DRM_MODE_CONNECTOR_DVID] = "DVI-D",
[DRM_MODE_CONNECTOR_DVIA] = "DVI-A",
[DRM_MODE_CONNECTOR_Composite] = "Composite",
[DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO",
[DRM_MODE_CONNECTOR_LVDS] = "LVDS",
[DRM_MODE_CONNECTOR_Component] = "Component",
[DRM_MODE_CONNECTOR_9PinDIN] = "DIN",
[DRM_MODE_CONNECTOR_DisplayPort] = "DP",
[DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A",
[DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B",
[DRM_MODE_CONNECTOR_TV] = "TV",
[DRM_MODE_CONNECTOR_eDP] = "eDP",
[DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual",
[DRM_MODE_CONNECTOR_DSI] = "DSI",
};
static void page_flip_handler(int fd, unsigned seq, unsigned tv_sec, unsigned tv_usec,
void *user) {
struct wlr_drm_output *out = user;
2017-05-03 11:28:44 +02:00
struct wlr_drm_backend *backend = wl_container_of(out->renderer, backend, renderer);
out->pageflip_pending = true;
if (!out->cleanup) {
wl_signal_emit(&backend->signals.output_render, out);
}
}
2017-05-03 11:28:44 +02:00
int wlr_drm_event(int fd, uint32_t mask, void *data) {
drmEventContext event = {
.version = DRM_EVENT_CONTEXT_VERSION,
.page_flip_handler = page_flip_handler,
};
drmHandleEvent(fd, &event);
return 1;
}
2017-05-03 11:28:44 +02:00
bool wlr_drm_renderer_init(struct wlr_drm_renderer *renderer, int fd) {
2017-05-01 07:49:18 +02:00
renderer->gbm = gbm_create_device(fd);
if (!renderer->gbm) {
2017-05-02 03:00:25 +02:00
wlr_log(L_ERROR, "Failed to create GBM device: %s", strerror(errno));
2017-05-01 05:20:48 +02:00
return false;
}
2017-05-03 07:17:14 +02:00
if (!wlr_egl_init(&renderer->egl, EGL_PLATFORM_GBM_MESA, renderer->gbm)) {
2017-05-03 11:28:44 +02:00
gbm_device_destroy(renderer->gbm);
return false;
}
2017-05-01 07:49:18 +02:00
renderer->fd = fd;
2017-05-01 05:20:48 +02:00
return true;
}
void wlr_drm_renderer_free(struct wlr_drm_renderer *renderer) {
if (!renderer) {
2017-05-01 05:20:48 +02:00
return;
}
2017-05-01 05:20:48 +02:00
2017-05-03 07:17:14 +02:00
wlr_egl_free(&renderer->egl);
2017-05-01 07:49:18 +02:00
gbm_device_destroy(renderer->gbm);
2017-05-01 05:20:48 +02:00
}
static int find_id(const void *item, const void *cmp_to) {
const struct wlr_drm_output *out = item;
2017-05-02 04:34:33 +02:00
const uint32_t *id = cmp_to;
if (out->connector < *id) {
2017-05-02 04:34:33 +02:00
return -1;
} else if (out->connector > *id) {
2017-05-02 04:34:33 +02:00
return 1;
} else {
2017-05-02 04:34:33 +02:00
return 0;
}
2017-05-02 04:34:33 +02:00
}
void wlr_drm_scan_connectors(struct wlr_drm_backend *backend) {
2017-05-01 07:49:18 +02:00
drmModeRes *res = drmModeGetResources(backend->fd);
2017-05-02 03:00:25 +02:00
if (!res) {
wlr_log(L_ERROR, "Failed to get DRM resources");
2017-05-01 05:20:48 +02:00
return;
2017-05-02 03:00:25 +02:00
}
2017-05-01 05:20:48 +02:00
2017-05-02 04:34:33 +02:00
for (int i = 0; i < res->count_connectors; ++i) {
uint32_t id = res->connectors[i];
2017-05-01 05:20:48 +02:00
2017-05-02 04:34:33 +02:00
drmModeConnector *conn = drmModeGetConnector(backend->fd, id);
if (!conn) {
wlr_log(L_ERROR, "Failed to get DRM connector");
continue;
2017-05-01 05:20:48 +02:00
}
struct wlr_drm_output *out;
int index = list_seq_find(backend->outputs, find_id, &id);
2017-05-01 05:20:48 +02:00
2017-05-02 04:34:33 +02:00
if (index == -1) {
out = calloc(1, sizeof *out);
if (!out) {
2017-05-02 04:34:33 +02:00
wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno));
drmModeFreeConnector(conn);
continue;
}
2017-05-01 05:20:48 +02:00
out->renderer = &backend->renderer;
out->state = DRM_OUTPUT_DISCONNECTED;
out->connector = res->connectors[i];
snprintf(out->name, sizeof out->name, "%s-%"PRIu32,
2017-05-01 05:20:48 +02:00
conn_name[conn->connector_type],
conn->connector_type_id);
2017-05-02 04:34:33 +02:00
list_add(backend->outputs, out);
wlr_log(L_INFO, "Found display '%s'", out->name);
2017-05-02 04:34:33 +02:00
} else {
out = backend->outputs->items[index];
2017-05-01 05:20:48 +02:00
}
if (out->state == DRM_OUTPUT_DISCONNECTED &&
conn->connection == DRM_MODE_CONNECTED) {
out->state = DRM_OUTPUT_NEEDS_MODESET;
wlr_log(L_INFO, "Sending modesetting signal for '%s'", out->name);
wl_signal_emit(&backend->signals.output_add, out);
} else if (out->state == DRM_OUTPUT_CONNECTED &&
conn->connection != DRM_MODE_CONNECTED) {
out->state = DRM_OUTPUT_DISCONNECTED;
wlr_drm_output_free(out, false);
wlr_log(L_INFO, "Sending destruction signal for '%s'", out->name);
wl_signal_emit(&backend->signals.output_rem, out);
2017-05-01 05:20:48 +02:00
}
drmModeFreeConnector(conn);
}
drmModeFreeResources(res);
}
static void free_fb(struct gbm_bo *bo, void *data) {
2017-05-01 05:20:48 +02:00
uint32_t *id = data;
if (id && *id) {
2017-05-01 05:20:48 +02:00
drmModeRmFB(gbm_bo_get_fd(bo), *id);
}
2017-05-01 05:20:48 +02:00
free(id);
}
static uint32_t get_fb_for_bo(int fd, struct gbm_bo *bo) {
2017-05-01 05:20:48 +02:00
uint32_t *id = gbm_bo_get_user_data(bo);
if (id) {
2017-05-01 05:20:48 +02:00
return *id;
}
2017-05-01 05:20:48 +02:00
id = calloc(1, sizeof *id);
2017-05-02 03:00:25 +02:00
if (!id) {
wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno));
2017-05-01 05:20:48 +02:00
return 0;
2017-05-02 03:00:25 +02:00
}
2017-05-01 05:20:48 +02:00
drmModeAddFB(fd, 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, id, free_fb);
return *id;
}
2017-05-01 07:49:18 +02:00
static bool display_init_renderer(struct wlr_drm_renderer *renderer,
struct wlr_drm_output *out) {
out->renderer = renderer;
out->gbm = gbm_surface_create(renderer->gbm, out->width, out->height,
GBM_FORMAT_XRGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING);
if (!out->gbm) {
wlr_log(L_ERROR, "Failed to create GBM surface for %s: %s", out->name,
2017-05-02 03:00:25 +02:00
strerror(errno));
2017-05-01 07:49:18 +02:00
return false;
2017-05-02 03:00:25 +02:00
}
2017-05-01 05:20:48 +02:00
out->egl = wlr_egl_create_surface(&renderer->egl, out->gbm);
if (out->egl == EGL_NO_SURFACE) {
wlr_log(L_ERROR, "Failed to create EGL surface for %s", out->name);
2017-05-01 07:49:18 +02:00
return false;
2017-05-02 03:00:25 +02:00
}
2017-05-01 05:20:48 +02:00
// Render black frame
eglMakeCurrent(renderer->egl.display, out->egl, out->egl, renderer->egl.context);
2017-05-01 05:20:48 +02:00
glViewport(0, 0, out->width, out->height);
2017-05-01 05:20:48 +02:00
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
eglSwapBuffers(renderer->egl.display, out->egl);
2017-05-01 05:20:48 +02:00
struct gbm_bo *bo = gbm_surface_lock_front_buffer(out->gbm);
2017-05-01 07:49:18 +02:00
uint32_t fb_id = get_fb_for_bo(renderer->fd, bo);
2017-05-01 05:20:48 +02:00
drmModeSetCrtc(renderer->fd, out->crtc, fb_id, 0, 0,
&out->connector, 1, out->active_mode);
drmModePageFlip(renderer->fd, out->crtc, fb_id, DRM_MODE_PAGE_FLIP_EVENT, out);
2017-05-01 05:20:48 +02:00
gbm_surface_release_buffer(out->gbm, bo);
2017-05-01 07:49:18 +02:00
return true;
2017-05-01 05:20:48 +02:00
}
static drmModeModeInfo *select_mode(size_t num_modes, drmModeModeInfo modes[static num_modes],
drmModeCrtc *old_crtc, const char *str) {
if (strcmp(str, "preferred") == 0) {
2017-05-01 05:20:48 +02:00
return &modes[0];
}
2017-05-01 05:20:48 +02:00
if (strcmp(str, "current") == 0) {
if (!old_crtc) {
2017-05-02 03:00:25 +02:00
wlr_log(L_ERROR, "Display does not have currently configured mode");
2017-05-01 05:20:48 +02:00
return NULL;
}
for (size_t i = 0; i < num_modes; ++i) {
if (memcmp(&modes[i], &old_crtc->mode, sizeof modes[0]) == 0) {
2017-05-01 05:20:48 +02:00
return &modes[i];
}
2017-05-01 05:20:48 +02:00
}
// We should never get here
assert(0);
}
unsigned width = 0;
unsigned height = 0;
unsigned rate = 0;
int ret;
if ((ret = sscanf(str, "%ux%u@%u", &width, &height, &rate)) != 2 && ret != 3) {
2017-05-02 03:00:25 +02:00
wlr_log(L_ERROR, "Invalid modesetting string '%s'", str);
2017-05-01 05:20:48 +02:00
return NULL;
}
for (size_t i = 0; i < num_modes; ++i) {
if (modes[i].hdisplay == width && modes[i].vdisplay == height &&
(!rate || modes[i].vrefresh == rate)) {
2017-05-01 05:20:48 +02:00
return &modes[i];
}
2017-05-01 05:20:48 +02:00
}
2017-05-02 03:00:25 +02:00
wlr_log(L_ERROR, "Unable to find mode %ux%u@%u", width, height, rate);
2017-05-01 05:20:48 +02:00
return NULL;
}
bool wlr_drm_output_modeset(struct wlr_drm_output *out, const char *str) {
2017-05-03 11:28:44 +02:00
struct wlr_drm_backend *backend = wl_container_of(out->renderer, backend, renderer);
wlr_log(L_INFO, "Modesetting %s with '%s'", out->name, str);
2017-05-02 03:00:25 +02:00
drmModeConnector *conn = drmModeGetConnector(backend->fd, out->connector);
2017-05-02 03:00:25 +02:00
if (!conn) {
wlr_log(L_ERROR, "Failed to get DRM connector");
2017-05-01 05:20:48 +02:00
goto error;
2017-05-02 03:00:25 +02:00
}
if (conn->connection != DRM_MODE_CONNECTED || conn->count_modes == 0) {
wlr_log(L_ERROR, "%s is not connected", out->name);
2017-05-02 03:00:25 +02:00
goto error;
}
2017-05-01 05:20:48 +02:00
out->num_modes = conn->count_modes;
out->modes = malloc(sizeof *out->modes * out->num_modes);
if (!out->modes) {
2017-05-02 03:00:25 +02:00
wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno));
2017-05-01 05:20:48 +02:00
goto error;
2017-05-02 03:00:25 +02:00
}
memcpy(out->modes, conn->modes, sizeof *out->modes * out->num_modes);
2017-05-01 05:20:48 +02:00
2017-05-02 03:00:25 +02:00
wlr_log(L_INFO, "Detected modes:");
for (size_t i = 0; i < out->num_modes; ++i) {
2017-05-02 03:00:25 +02:00
wlr_log(L_INFO, " %"PRIu16"@%"PRIu16"@%"PRIu32,
out->modes[i].hdisplay, out->modes[i].vdisplay,
out->modes[i].vrefresh);
}
2017-05-02 03:00:25 +02:00
2017-05-01 07:49:18 +02:00
drmModeEncoder *curr_enc = drmModeGetEncoder(backend->fd, conn->encoder_id);
2017-05-01 05:20:48 +02:00
if (curr_enc) {
out->old_crtc = drmModeGetCrtc(backend->fd, curr_enc->crtc_id);
2017-05-01 05:20:48 +02:00
free(curr_enc);
}
out->active_mode = select_mode(out->num_modes, out->modes,
out->old_crtc, str);
if (!out->active_mode) {
wlr_log(L_ERROR, "Failed to configure %s", out->name);
2017-05-01 05:20:48 +02:00
goto error;
}
2017-05-01 07:49:18 +02:00
drmModeRes *res = drmModeGetResources(backend->fd);
2017-05-02 03:00:25 +02:00
if (!res) {
wlr_log(L_ERROR, "Failed to get DRM resources");
2017-05-01 05:20:48 +02:00
goto error;
2017-05-02 03:00:25 +02:00
}
2017-05-01 05:20:48 +02:00
bool success = false;
for (int i = 0; !success && i < conn->count_encoders; ++i) {
2017-05-01 07:49:18 +02:00
drmModeEncoder *enc = drmModeGetEncoder(backend->fd, conn->encoders[i]);
2017-05-01 05:20:48 +02:00
if (!enc)
continue;
for (int j = 0; j < res->count_crtcs; ++j) {
if ((enc->possible_crtcs & (1 << j)) == 0) {
2017-05-01 05:20:48 +02:00
continue;
}
2017-05-01 05:20:48 +02:00
2017-05-01 07:49:18 +02:00
if ((backend->taken_crtcs & (1 << j)) == 0) {
backend->taken_crtcs |= 1 << j;
out->crtc = res->crtcs[j];
2017-05-01 05:20:48 +02:00
success = true;
break;
}
}
drmModeFreeEncoder(enc);
}
drmModeFreeResources(res);
2017-05-02 03:00:25 +02:00
if (!success) {
wlr_log(L_ERROR, "Failed to find CRTC for %s", out->name);
2017-05-01 05:20:48 +02:00
goto error;
2017-05-02 03:00:25 +02:00
}
2017-05-01 05:20:48 +02:00
out->state = DRM_OUTPUT_CONNECTED;
2017-05-01 05:20:48 +02:00
out->width = out->active_mode->hdisplay;
out->height = out->active_mode->vdisplay;
2017-05-01 05:20:48 +02:00
if (!display_init_renderer(&backend->renderer, out)) {
wlr_log(L_ERROR, "Failed to initalise renderer for %s", out->name);
2017-05-02 03:00:25 +02:00
goto error;
}
drmModeFreeConnector(conn);
2017-05-02 08:13:17 +02:00
wlr_log(L_INFO, "Configuring %s with mode %"PRIu16"x%"PRIu16"@%"PRIu32"",
out->name, out->active_mode->hdisplay, out->active_mode->vdisplay,
out->active_mode->vrefresh);
2017-05-01 05:20:48 +02:00
return true;
2017-05-02 03:00:25 +02:00
2017-05-01 05:20:48 +02:00
error:
out->state = DRM_OUTPUT_DISCONNECTED;
2017-05-01 05:20:48 +02:00
drmModeFreeConnector(conn);
free(out->modes);
2017-05-01 05:20:48 +02:00
wl_signal_emit(&backend->signals.output_rem, out);
2017-05-01 05:20:48 +02:00
return false;
}
void wlr_drm_output_free(struct wlr_drm_output *out, bool restore) {
if (!out || out->state != DRM_OUTPUT_CONNECTED) {
2017-05-01 05:20:48 +02:00
return;
}
2017-05-01 05:20:48 +02:00
struct wlr_drm_renderer *renderer = out->renderer;
2017-05-01 07:49:18 +02:00
eglDestroySurface(renderer->egl.display, out->egl);
gbm_surface_destroy(out->gbm);
2017-05-02 03:00:25 +02:00
free(out->modes);
out->state = DRM_OUTPUT_DISCONNECTED;
2017-05-02 03:00:25 +02:00
if (!restore) {
2017-05-02 03:00:25 +02:00
return;
}
2017-05-02 03:00:25 +02:00
drmModeCrtc *crtc = out->old_crtc;
2017-05-01 05:20:48 +02:00
if (crtc) {
2017-05-01 07:49:18 +02:00
// Wait for exising page flips to finish
2017-05-01 05:20:48 +02:00
drmEventContext event = {
.version = DRM_EVENT_CONTEXT_VERSION,
.page_flip_handler = page_flip_handler,
};
out->cleanup = true;
while (out->pageflip_pending)
2017-05-01 07:49:18 +02:00
drmHandleEvent(renderer->fd, &event);
2017-05-01 05:20:48 +02:00
2017-05-01 07:49:18 +02:00
drmModeSetCrtc(renderer->fd, crtc->crtc_id, crtc->buffer_id,
crtc->x, crtc->y, &out->connector,
2017-05-01 05:20:48 +02:00
1, &crtc->mode);
drmModeFreeCrtc(crtc);
}
}
void wlr_drm_output_begin(struct wlr_drm_output *out) {
struct wlr_drm_renderer *renderer = out->renderer;
eglMakeCurrent(renderer->egl.display, out->egl, out->egl, renderer->egl.context);
2017-05-01 05:20:48 +02:00
}
void wlr_drm_output_end(struct wlr_drm_output *out) {
struct wlr_drm_renderer *renderer = out->renderer;
eglSwapBuffers(renderer->egl.display, out->egl);
2017-05-01 05:20:48 +02:00
struct gbm_bo *bo = gbm_surface_lock_front_buffer(out->gbm);
2017-05-01 07:49:18 +02:00
uint32_t fb_id = get_fb_for_bo(renderer->fd, bo);
2017-05-01 05:20:48 +02:00
drmModePageFlip(renderer->fd, out->crtc, fb_id, DRM_MODE_PAGE_FLIP_EVENT, out);
2017-05-01 05:20:48 +02:00
gbm_surface_release_buffer(out->gbm, bo);
2017-05-01 05:20:48 +02:00
out->pageflip_pending = false;
2017-05-01 05:20:48 +02:00
}