wlroots/backend/drm/drm.c

427 lines
11 KiB
C
Raw Normal View History

2017-05-01 03:20:48 +00: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 03:20:48 +00:00
2017-05-07 14:00:23 +00:00
#include "backend.h"
2017-05-02 01:00:25 +00:00
#include "backend/drm/backend.h"
#include "backend/drm/drm.h"
#include "common/log.h"
2017-05-01 03:20:48 +00: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",
};
2017-05-03 09:28:44 +00:00
bool wlr_drm_renderer_init(struct wlr_drm_renderer *renderer, int fd) {
2017-05-01 05:49:18 +00:00
renderer->gbm = gbm_create_device(fd);
if (!renderer->gbm) {
2017-05-02 01:00:25 +00:00
wlr_log(L_ERROR, "Failed to create GBM device: %s", strerror(errno));
2017-05-01 03:20:48 +00:00
return false;
}
2017-05-03 05:17:14 +00:00
if (!wlr_egl_init(&renderer->egl, EGL_PLATFORM_GBM_MESA, renderer->gbm)) {
2017-05-03 09:28:44 +00:00
gbm_device_destroy(renderer->gbm);
return false;
}
2017-05-01 05:49:18 +00:00
renderer->fd = fd;
2017-05-01 03:20:48 +00:00
return true;
}
void wlr_drm_renderer_free(struct wlr_drm_renderer *renderer) {
if (!renderer) {
2017-05-01 03:20:48 +00:00
return;
}
2017-05-01 03:20:48 +00:00
2017-05-03 05:17:14 +00:00
wlr_egl_free(&renderer->egl);
2017-05-01 05:49:18 +00:00
gbm_device_destroy(renderer->gbm);
2017-05-01 03:20:48 +00:00
}
static void free_fb(struct gbm_bo *bo, void *data) {
2017-05-01 03:20:48 +00:00
uint32_t *id = data;
if (id && *id) {
2017-05-01 03:20:48 +00:00
drmModeRmFB(gbm_bo_get_fd(bo), *id);
}
2017-05-01 03:20:48 +00:00
free(id);
}
static uint32_t get_fb_for_bo(int fd, struct gbm_bo *bo) {
2017-05-01 03:20:48 +00:00
uint32_t *id = gbm_bo_get_user_data(bo);
if (id) {
2017-05-01 03:20:48 +00:00
return *id;
}
2017-05-01 03:20:48 +00:00
id = calloc(1, sizeof *id);
2017-05-02 01:00:25 +00:00
if (!id) {
wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno));
2017-05-01 03:20:48 +00:00
return 0;
2017-05-02 01:00:25 +00:00
}
2017-05-01 03:20:48 +00: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-03 10:40:19 +00:00
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);
}
void wlr_drm_output_end(struct wlr_drm_output *out) {
struct wlr_drm_renderer *renderer = out->renderer;
eglSwapBuffers(renderer->egl.display, out->egl);
struct gbm_bo *bo = gbm_surface_lock_front_buffer(out->gbm);
uint32_t fb_id = get_fb_for_bo(renderer->fd, bo);
drmModePageFlip(renderer->fd, out->crtc, fb_id, DRM_MODE_PAGE_FLIP_EVENT, out);
gbm_surface_release_buffer(out->gbm, bo);
out->pageflip_pending = true;
2017-05-03 10:40:19 +00:00
}
2017-05-01 05:49:18 +00: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 01:00:25 +00:00
strerror(errno));
2017-05-01 05:49:18 +00:00
return false;
2017-05-02 01:00:25 +00:00
}
2017-05-01 03:20:48 +00: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 05:49:18 +00:00
return false;
2017-05-02 01:00:25 +00:00
}
2017-05-01 03:20:48 +00:00
// Render black frame
eglMakeCurrent(renderer->egl.display, out->egl, out->egl, renderer->egl.context);
2017-05-01 03:20:48 +00:00
glViewport(0, 0, out->width, out->height);
2017-05-01 03:20:48 +00:00
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
eglSwapBuffers(renderer->egl.display, out->egl);
2017-05-01 03:20:48 +00:00
struct gbm_bo *bo = gbm_surface_lock_front_buffer(out->gbm);
2017-05-01 05:49:18 +00:00
uint32_t fb_id = get_fb_for_bo(renderer->fd, bo);
2017-05-01 03:20:48 +00:00
drmModeSetCrtc(renderer->fd, out->crtc, fb_id, 0, 0,
2017-05-03 10:40:19 +00:00
&out->connector, 1, &out->active_mode->mode);
drmModePageFlip(renderer->fd, out->crtc, fb_id, DRM_MODE_PAGE_FLIP_EVENT, out);
2017-05-01 03:20:48 +00:00
gbm_surface_release_buffer(out->gbm, bo);
2017-05-01 05:49:18 +00:00
return true;
2017-05-01 03:20:48 +00:00
}
2017-05-03 10:40:19 +00:00
static int find_id(const void *item, const void *cmp_to) {
const struct wlr_drm_output *out = item;
const uint32_t *id = cmp_to;
if (out->connector < *id) {
return -1;
} else if (out->connector > *id) {
return 1;
} else {
return 0;
}
}
2017-05-07 14:00:23 +00:00
void wlr_drm_scan_connectors(struct wlr_backend_state *state) {
wlr_log(L_INFO, "Scanning DRM connectors");
2017-05-07 14:00:23 +00:00
drmModeRes *res = drmModeGetResources(state->fd);
2017-05-03 10:40:19 +00:00
if (!res) {
wlr_log(L_ERROR, "Failed to get DRM resources");
return;
}
2017-05-01 03:20:48 +00:00
2017-05-03 10:40:19 +00:00
for (int i = 0; i < res->count_connectors; ++i) {
uint32_t id = res->connectors[i];
2017-05-07 14:00:23 +00:00
drmModeConnector *conn = drmModeGetConnector(state->fd, id);
2017-05-03 10:40:19 +00:00
if (!conn) {
wlr_log(L_ERROR, "Failed to get DRM connector");
continue;
2017-05-01 03:20:48 +00:00
}
2017-05-03 10:40:19 +00:00
struct wlr_drm_output *out;
2017-05-07 14:00:23 +00:00
int index = list_seq_find(state->outputs, find_id, &id);
2017-05-03 10:40:19 +00:00
if (index == -1) {
2017-05-07 14:00:23 +00:00
out = calloc(1, sizeof(struct wlr_drm_output));
2017-05-03 10:40:19 +00:00
if (!out) {
wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno));
drmModeFreeConnector(conn);
continue;
}
2017-05-03 10:40:19 +00:00
2017-05-07 14:00:23 +00:00
out->renderer = &state->renderer;
2017-05-03 10:40:19 +00:00
out->state = DRM_OUTPUT_DISCONNECTED;
out->connector = id;
2017-05-03 10:40:19 +00:00
snprintf(out->name, sizeof out->name, "%s-%"PRIu32,
conn_name[conn->connector_type],
conn->connector_type_id);
2017-05-07 14:00:23 +00:00
drmModeEncoder *curr_enc = drmModeGetEncoder(state->fd, conn->encoder_id);
if (curr_enc) {
2017-05-07 14:00:23 +00:00
out->old_crtc = drmModeGetCrtc(state->fd, curr_enc->crtc_id);
free(curr_enc);
}
2017-05-07 14:00:23 +00:00
list_add(state->outputs, out);
2017-05-03 10:40:19 +00:00
wlr_log(L_INFO, "Found display '%s'", out->name);
} else {
2017-05-07 14:00:23 +00:00
out = state->outputs->items[index];
2017-05-01 03:20:48 +00:00
}
2017-05-03 10:40:19 +00:00
if (out->state == DRM_OUTPUT_DISCONNECTED &&
conn->connection == DRM_MODE_CONNECTED) {
2017-05-01 03:20:48 +00:00
2017-05-03 10:40:19 +00:00
wlr_log(L_INFO, "'%s' connected", out->name);
2017-05-01 03:20:48 +00:00
2017-05-03 10:40:19 +00:00
out->modes = malloc(sizeof(*out->modes) * conn->count_modes);
if (!out->modes) {
wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno));
goto error;
}
wlr_log(L_INFO, "Detected modes:");
out->num_modes = conn->count_modes;
for (int i = 0; i < conn->count_modes; ++i) {
drmModeModeInfo *mode = &conn->modes[i];
out->modes[i].width = mode->hdisplay;
out->modes[i].height = mode->vdisplay;
// TODO: Calculate more accurate refresh rate
out->modes[i].rate = mode->vrefresh;
out->modes[i].mode = *mode;
wlr_log(L_INFO, " %"PRIu16"@%"PRIu16"@%"PRIu32,
mode->hdisplay, mode->vdisplay,
mode->vrefresh);
}
2017-05-01 03:20:48 +00:00
2017-05-03 10:40:19 +00:00
out->state = DRM_OUTPUT_NEEDS_MODESET;
wlr_log(L_INFO, "Sending modesetting signal for '%s'", out->name);
2017-05-07 14:00:23 +00:00
wl_signal_emit(&state->backend->events.output_add, out);
2017-05-03 10:40:19 +00:00
} else if (out->state == DRM_OUTPUT_CONNECTED &&
conn->connection != DRM_MODE_CONNECTED) {
wlr_log(L_INFO, "'%s' disconnected", out->name);
2017-05-03 10:40:19 +00:00
wlr_drm_output_cleanup(out, false);
}
2017-05-03 10:40:19 +00:00
error:
drmModeFreeConnector(conn);
2017-05-01 03:20:48 +00:00
}
2017-05-03 10:40:19 +00:00
drmModeFreeResources(res);
}
struct wlr_drm_mode *wlr_drm_output_get_modes(struct wlr_drm_output *out, size_t *count) {
if (out->state == DRM_OUTPUT_DISCONNECTED) {
*count = 0;
return NULL;
}
*count = out->num_modes;
return out->modes;
2017-05-01 03:20:48 +00:00
}
2017-05-03 10:40:19 +00:00
bool wlr_drm_output_modeset(struct wlr_drm_output *out, struct wlr_drm_mode *mode) {
2017-05-07 14:00:23 +00:00
struct wlr_backend_state *state = wl_container_of(out->renderer, state, renderer);
2017-05-03 10:40:19 +00:00
wlr_log(L_INFO, "Modesetting '%s' with '%ux%u@%u'", out->name, mode->width,
mode->height, mode->rate);
2017-05-02 01:00:25 +00:00
2017-05-07 14:00:23 +00:00
drmModeConnector *conn = drmModeGetConnector(state->fd, out->connector);
2017-05-02 01:00:25 +00:00
if (!conn) {
wlr_log(L_ERROR, "Failed to get DRM connector");
2017-05-01 03:20:48 +00:00
goto error;
2017-05-02 01:00:25 +00:00
}
if (conn->connection != DRM_MODE_CONNECTED || conn->count_modes == 0) {
wlr_log(L_ERROR, "%s is not connected", out->name);
2017-05-02 01:00:25 +00:00
goto error;
}
2017-05-01 03:20:48 +00:00
2017-05-07 14:00:23 +00:00
drmModeRes *res = drmModeGetResources(state->fd);
2017-05-02 01:00:25 +00:00
if (!res) {
wlr_log(L_ERROR, "Failed to get DRM resources");
2017-05-01 03:20:48 +00:00
goto error;
2017-05-02 01:00:25 +00:00
}
2017-05-01 03:20:48 +00:00
bool success = false;
for (int i = 0; !success && i < conn->count_encoders; ++i) {
2017-05-07 14:00:23 +00:00
drmModeEncoder *enc = drmModeGetEncoder(state->fd, conn->encoders[i]);
2017-05-01 03:20:48 +00:00
if (!enc)
continue;
for (int j = 0; j < res->count_crtcs; ++j) {
if ((enc->possible_crtcs & (1 << j)) == 0) {
2017-05-01 03:20:48 +00:00
continue;
}
2017-05-01 03:20:48 +00:00
2017-05-07 14:00:23 +00:00
if ((state->taken_crtcs & (1 << j)) == 0) {
state->taken_crtcs |= 1 << j;
out->crtc = res->crtcs[j];
2017-05-01 03:20:48 +00:00
success = true;
break;
}
}
drmModeFreeEncoder(enc);
}
drmModeFreeResources(res);
2017-05-02 01:00:25 +00:00
if (!success) {
wlr_log(L_ERROR, "Failed to find CRTC for %s", out->name);
2017-05-01 03:20:48 +00:00
goto error;
2017-05-02 01:00:25 +00:00
}
2017-05-01 03:20:48 +00:00
out->state = DRM_OUTPUT_CONNECTED;
2017-05-03 10:40:19 +00:00
out->active_mode = mode;
out->width = mode->width;
out->height = mode->height;
2017-05-01 03:20:48 +00:00
2017-05-07 14:00:23 +00:00
if (!display_init_renderer(&state->renderer, out)) {
wlr_log(L_ERROR, "Failed to initalise renderer for %s", out->name);
2017-05-02 01:00:25 +00:00
goto error;
}
drmModeFreeConnector(conn);
2017-05-01 03:20:48 +00:00
return true;
2017-05-02 01:00:25 +00:00
2017-05-01 03:20:48 +00:00
error:
wlr_drm_output_cleanup(out, false);
2017-05-01 03:20:48 +00:00
drmModeFreeConnector(conn);
return false;
}
2017-05-07 14:00:23 +00:00
static void page_flip_handler(int fd, unsigned seq,
unsigned tv_sec, unsigned tv_usec, void *user) {
2017-05-03 10:40:19 +00:00
struct wlr_drm_output *out = user;
2017-05-07 14:00:23 +00:00
struct wlr_backend_state *state = wl_container_of(out->renderer, state, renderer);
2017-05-03 10:40:19 +00:00
out->pageflip_pending = false;
if (out->state == DRM_OUTPUT_CONNECTED) {
2017-05-07 14:00:23 +00:00
wl_signal_emit(&state->backend->events.output_frame, out);
}
2017-05-03 10:40:19 +00:00
}
2017-05-01 03:20:48 +00:00
2017-05-03 10:40:19 +00: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,
};
2017-05-01 05:49:18 +00:00
2017-05-03 10:40:19 +00:00
drmHandleEvent(fd, &event);
2017-05-02 01:00:25 +00:00
2017-05-03 10:40:19 +00:00
return 1;
}
2017-05-02 01:00:25 +00:00
2017-05-03 10:40:19 +00:00
static void restore_output(struct wlr_drm_output *out, int fd) {
// Wait for any pending pageflips to finish
while (out->pageflip_pending) {
wlr_drm_event(fd, 0, NULL);
}
2017-05-03 10:40:19 +00:00
drmModeCrtc *crtc = out->old_crtc;
if (!crtc) {
2017-05-02 01:00:25 +00:00
return;
}
2017-05-02 01:00:25 +00:00
2017-05-03 10:40:19 +00:00
drmModeSetCrtc(fd, crtc->crtc_id, crtc->buffer_id, crtc->x, crtc->y,
&out->connector, 1, &crtc->mode);
drmModeFreeCrtc(crtc);
2017-05-01 03:20:48 +00:00
}
2017-05-03 10:40:19 +00:00
void wlr_drm_output_cleanup(struct wlr_drm_output *out, bool restore) {
if (!out) {
return;
}
2017-05-01 03:20:48 +00:00
2017-05-03 10:40:19 +00:00
struct wlr_drm_renderer *renderer = out->renderer;
2017-05-07 14:00:23 +00:00
struct wlr_backend_state *state = wl_container_of(renderer, state, renderer);
2017-05-01 03:20:48 +00:00
2017-05-03 10:40:19 +00:00
switch (out->state) {
case DRM_OUTPUT_CONNECTED:
eglDestroySurface(renderer->egl.display, out->egl);
gbm_surface_destroy(out->gbm);
out->egl = EGL_NO_SURFACE;
out->gbm = NULL;
/* Fallthrough */
case DRM_OUTPUT_NEEDS_MODESET:
free(out->modes);
out->num_modes = 0;
out->modes = NULL;
out->active_mode = NULL;
out->width = 0;
out->height = 0;
out->state = DRM_OUTPUT_DISCONNECTED;
2017-05-03 10:40:19 +00:00
if (restore) {
restore_output(out, renderer->fd);
}
2017-05-01 03:20:48 +00:00
wlr_log(L_INFO, "Emmiting destruction signal for '%s'", out->name);
2017-05-07 14:00:23 +00:00
wl_signal_emit(&state->backend->events.output_remove, out);
2017-05-03 10:40:19 +00:00
break;
2017-05-01 03:20:48 +00:00
2017-05-03 10:40:19 +00:00
case DRM_OUTPUT_DISCONNECTED:
break;
}
2017-05-01 03:20:48 +00:00
}
const char *wlr_drm_output_get_name(struct wlr_drm_output *out) {
return out->name;
}