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>
|
2017-05-02 02:08:34 +00:00
|
|
|
#include <wayland-server.h>
|
2017-05-01 03:20:48 +00:00
|
|
|
|
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-02 01:00:25 +00:00
|
|
|
static const char *egl_error(void)
|
|
|
|
{
|
|
|
|
switch (eglGetError()) {
|
|
|
|
case EGL_SUCCESS:
|
|
|
|
return "Success";
|
|
|
|
case EGL_NOT_INITIALIZED:
|
|
|
|
return "Not initialized";
|
|
|
|
case EGL_BAD_ACCESS:
|
|
|
|
return "Bad access";
|
|
|
|
case EGL_BAD_ALLOC:
|
|
|
|
return "Bad alloc";
|
|
|
|
case EGL_BAD_ATTRIBUTE:
|
|
|
|
return "Bad attribute";
|
|
|
|
case EGL_BAD_CONTEXT:
|
|
|
|
return "Bad Context";
|
|
|
|
case EGL_BAD_CONFIG:
|
|
|
|
return "Bad Config";
|
|
|
|
case EGL_BAD_CURRENT_SURFACE:
|
|
|
|
return "Bad current surface";
|
|
|
|
case EGL_BAD_DISPLAY:
|
|
|
|
return "Bad display";
|
|
|
|
case EGL_BAD_SURFACE:
|
|
|
|
return "Bad surface";
|
|
|
|
case EGL_BAD_MATCH:
|
|
|
|
return "Bad match";
|
|
|
|
case EGL_BAD_PARAMETER:
|
|
|
|
return "Bad parameter";
|
|
|
|
case EGL_BAD_NATIVE_PIXMAP:
|
|
|
|
return "Bad native pixmap";
|
|
|
|
case EGL_BAD_NATIVE_WINDOW:
|
|
|
|
return "Bad native window";
|
|
|
|
case EGL_CONTEXT_LOST:
|
|
|
|
return "Context lost";
|
|
|
|
default:
|
|
|
|
return "Unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-01 03:20:48 +00:00
|
|
|
// EGL extensions
|
|
|
|
PFNEGLGETPLATFORMDISPLAYEXTPROC get_platform_display;
|
|
|
|
PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC create_platform_window_surface;
|
|
|
|
|
|
|
|
static bool egl_exts()
|
|
|
|
{
|
|
|
|
get_platform_display = (PFNEGLGETPLATFORMDISPLAYEXTPROC)
|
|
|
|
eglGetProcAddress("eglGetPlatformDisplayEXT");
|
|
|
|
|
2017-05-02 01:00:25 +00:00
|
|
|
if (!get_platform_display) {
|
|
|
|
wlr_log(L_ERROR, "Failed to load EGL extension 'eglGetPlatformDisplayEXT'");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-01 03:20:48 +00:00
|
|
|
create_platform_window_surface = (PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC)
|
|
|
|
eglGetProcAddress("eglCreatePlatformWindowSurfaceEXT");
|
|
|
|
|
2017-05-02 01:00:25 +00:00
|
|
|
if (!get_platform_display) {
|
|
|
|
wlr_log(L_ERROR, "Failed to load EGL extension 'eglCreatePlatformWindowSurfaceEXT'");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2017-05-01 03:20:48 +00:00
|
|
|
}
|
|
|
|
|
2017-05-01 05:49:18 +00:00
|
|
|
static bool egl_get_config(EGLDisplay disp, EGLConfig *out)
|
2017-05-01 03:20:48 +00:00
|
|
|
{
|
|
|
|
EGLint count = 0, matched = 0, ret;
|
|
|
|
|
|
|
|
ret = eglGetConfigs(disp, NULL, 0, &count);
|
|
|
|
if (ret == EGL_FALSE || count == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
EGLConfig configs[count];
|
|
|
|
|
|
|
|
ret = eglChooseConfig(disp, NULL, configs, count, &matched);
|
|
|
|
if (ret == EGL_FALSE) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < matched; ++i) {
|
|
|
|
EGLint gbm_format;
|
|
|
|
|
|
|
|
if (!eglGetConfigAttrib(disp,
|
|
|
|
configs[i],
|
|
|
|
EGL_NATIVE_VISUAL_ID,
|
|
|
|
&gbm_format))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (gbm_format == GBM_FORMAT_XRGB8888) {
|
|
|
|
*out = configs[i];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-02 02:08:34 +00:00
|
|
|
static void page_flip_handler(int fd,
|
|
|
|
unsigned seq,
|
|
|
|
unsigned tv_sec,
|
|
|
|
unsigned tv_usec,
|
|
|
|
void *user)
|
|
|
|
{
|
|
|
|
struct wlr_drm_display *disp = user;
|
|
|
|
struct wlr_drm_backend *backend = disp->renderer->backend;
|
|
|
|
|
|
|
|
disp->pageflip_pending = true;
|
|
|
|
if (!disp->cleanup)
|
|
|
|
wl_signal_emit(&backend->signals.display_render, disp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int 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-01 05:49:18 +00:00
|
|
|
bool wlr_drm_renderer_init(struct wlr_drm_renderer *renderer,
|
|
|
|
struct wlr_drm_backend *backend, int fd)
|
2017-05-01 03:20:48 +00:00
|
|
|
{
|
2017-05-02 01:00:25 +00:00
|
|
|
if (!egl_exts())
|
2017-05-01 03:20:48 +00:00
|
|
|
return false;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (eglBindAPI(EGL_OPENGL_ES_API) == EGL_FALSE) {
|
2017-05-02 01:00:25 +00:00
|
|
|
wlr_log(L_ERROR, "Failed to bind to the OpenGL ES API: %s", egl_error());
|
2017-05-01 03:20:48 +00:00
|
|
|
goto error_gbm;
|
|
|
|
}
|
|
|
|
|
2017-05-01 05:49:18 +00:00
|
|
|
renderer->egl.disp = get_platform_display(EGL_PLATFORM_GBM_MESA, renderer->gbm, NULL);
|
|
|
|
if (renderer->egl.disp == EGL_NO_DISPLAY) {
|
2017-05-02 01:00:25 +00:00
|
|
|
wlr_log(L_ERROR, "Failed to create EGL display: %s", egl_error());
|
2017-05-01 03:20:48 +00:00
|
|
|
goto error_gbm;
|
|
|
|
}
|
|
|
|
|
2017-05-01 05:49:18 +00:00
|
|
|
if (eglInitialize(renderer->egl.disp, NULL, NULL) == EGL_FALSE) {
|
2017-05-02 01:00:25 +00:00
|
|
|
wlr_log(L_ERROR, "Failed to initialize EGL: %s", egl_error());
|
2017-05-01 03:20:48 +00:00
|
|
|
goto error_egl;
|
|
|
|
}
|
|
|
|
|
2017-05-01 05:49:18 +00:00
|
|
|
if (!egl_get_config(renderer->egl.disp, &renderer->egl.conf)) {
|
2017-05-02 01:00:25 +00:00
|
|
|
wlr_log(L_ERROR, "Failed to get EGL config");
|
2017-05-01 03:20:48 +00:00
|
|
|
goto error_egl;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const EGLint attribs[] = {EGL_CONTEXT_CLIENT_VERSION, 3, EGL_NONE};
|
2017-05-01 05:49:18 +00:00
|
|
|
|
|
|
|
renderer->egl.context = eglCreateContext(renderer->egl.disp,
|
|
|
|
renderer->egl.conf, EGL_NO_CONTEXT, attribs);
|
|
|
|
|
|
|
|
if (renderer->egl.context == EGL_NO_CONTEXT) {
|
2017-05-02 01:00:25 +00:00
|
|
|
wlr_log(L_ERROR, "Failed to create EGL context: %s", egl_error());
|
2017-05-01 03:20:48 +00:00
|
|
|
goto error_egl;
|
|
|
|
}
|
|
|
|
|
2017-05-02 02:08:34 +00:00
|
|
|
backend->event_src.drm = wl_event_loop_add_fd(backend->event_loop,
|
|
|
|
backend->fd, WL_EVENT_READABLE, drm_event, NULL);
|
|
|
|
if (!backend->event_src.drm) {
|
|
|
|
wlr_log(L_ERROR, "Failed to create DRM event source");
|
|
|
|
goto error_egl;
|
|
|
|
}
|
|
|
|
|
2017-05-01 05:49:18 +00:00
|
|
|
renderer->fd = fd;
|
|
|
|
renderer->backend = backend;
|
|
|
|
|
2017-05-01 03:20:48 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
error_egl:
|
2017-05-01 05:49:18 +00:00
|
|
|
eglTerminate(renderer->egl.disp);
|
2017-05-01 03:20:48 +00:00
|
|
|
eglReleaseThread();
|
|
|
|
eglMakeCurrent(EGL_NO_DISPLAY, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
|
|
|
|
|
|
|
|
error_gbm:
|
2017-05-01 05:49:18 +00:00
|
|
|
gbm_device_destroy(renderer->gbm);
|
2017-05-01 03:20:48 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-01 05:49:18 +00:00
|
|
|
void wlr_drm_renderer_free(struct wlr_drm_renderer *renderer)
|
2017-05-01 03:20:48 +00:00
|
|
|
{
|
2017-05-01 05:49:18 +00:00
|
|
|
if (!renderer)
|
2017-05-01 03:20:48 +00:00
|
|
|
return;
|
|
|
|
|
2017-05-01 05:49:18 +00:00
|
|
|
eglDestroyContext(renderer->egl.disp, renderer->egl.context);
|
|
|
|
eglTerminate(renderer->egl.disp);
|
2017-05-01 03:20:48 +00:00
|
|
|
eglReleaseThread();
|
|
|
|
eglMakeCurrent(EGL_NO_DISPLAY, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
|
|
|
|
|
2017-05-01 05:49:18 +00:00
|
|
|
gbm_device_destroy(renderer->gbm);
|
2017-05-01 03:20:48 +00:00
|
|
|
}
|
|
|
|
|
2017-05-02 02:34:33 +00:00
|
|
|
static int find_id(const void *item, const void *cmp_to)
|
|
|
|
{
|
|
|
|
const struct wlr_drm_display *disp = item;
|
|
|
|
const uint32_t *id = cmp_to;
|
|
|
|
|
|
|
|
if (disp->connector < *id)
|
|
|
|
return -1;
|
|
|
|
else if (disp->connector > *id)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-05-01 05:49:18 +00:00
|
|
|
void wlr_drm_scan_connectors(struct wlr_drm_backend *backend)
|
2017-05-01 03:20:48 +00:00
|
|
|
{
|
2017-05-01 05:49:18 +00:00
|
|
|
drmModeRes *res = drmModeGetResources(backend->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
|
|
|
return;
|
2017-05-02 01:00:25 +00:00
|
|
|
}
|
2017-05-01 03:20:48 +00:00
|
|
|
|
2017-05-02 02:34:33 +00:00
|
|
|
for (int i = 0; i < res->count_connectors; ++i) {
|
|
|
|
uint32_t id = res->connectors[i];
|
2017-05-01 03:20:48 +00:00
|
|
|
|
2017-05-02 02:34:33 +00:00
|
|
|
drmModeConnector *conn = drmModeGetConnector(backend->fd, id);
|
|
|
|
if (!conn) {
|
|
|
|
wlr_log(L_ERROR, "Failed to get DRM connector");
|
|
|
|
continue;
|
2017-05-01 03:20:48 +00:00
|
|
|
}
|
|
|
|
|
2017-05-02 02:34:33 +00:00
|
|
|
struct wlr_drm_display *disp;
|
|
|
|
int index = list_seq_find(backend->displays, find_id, &id);
|
2017-05-01 03:20:48 +00:00
|
|
|
|
2017-05-02 02:34:33 +00:00
|
|
|
if (index == -1) {
|
|
|
|
disp = calloc(1, sizeof *disp);
|
|
|
|
if (!disp) {
|
|
|
|
wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno));
|
|
|
|
drmModeFreeConnector(conn);
|
|
|
|
continue;
|
|
|
|
}
|
2017-05-01 03:20:48 +00:00
|
|
|
|
2017-05-01 05:49:18 +00:00
|
|
|
disp->state = DRM_DISP_DISCONNECTED;
|
2017-05-01 03:20:48 +00:00
|
|
|
disp->connector = res->connectors[i];
|
|
|
|
snprintf(disp->name, sizeof disp->name, "%s-%"PRIu32,
|
|
|
|
conn_name[conn->connector_type],
|
|
|
|
conn->connector_type_id);
|
2017-05-02 02:34:33 +00:00
|
|
|
|
|
|
|
list_add(backend->displays, disp);
|
|
|
|
} else {
|
|
|
|
disp = backend->displays->items[index];
|
2017-05-01 03:20:48 +00:00
|
|
|
}
|
|
|
|
|
2017-05-01 05:49:18 +00:00
|
|
|
if (disp->state == DRM_DISP_DISCONNECTED &&
|
2017-05-01 03:20:48 +00:00
|
|
|
conn->connection == DRM_MODE_CONNECTED) {
|
2017-05-01 05:49:18 +00:00
|
|
|
disp->state = DRM_DISP_NEEDS_MODESET;
|
2017-05-02 02:08:34 +00:00
|
|
|
wl_signal_emit(&backend->signals.display_add, disp);
|
2017-05-01 03:20:48 +00:00
|
|
|
|
2017-05-01 05:49:18 +00:00
|
|
|
} else if (disp->state == DRM_DISP_CONNECTED &&
|
2017-05-01 03:20:48 +00:00
|
|
|
conn->connection != DRM_MODE_CONNECTED) {
|
2017-05-01 05:49:18 +00:00
|
|
|
disp->state = DRM_DISP_DISCONNECTED;
|
2017-05-02 01:00:25 +00:00
|
|
|
wlr_drm_display_free(disp, false);
|
2017-05-02 02:08:34 +00:00
|
|
|
wl_signal_emit(&backend->signals.display_rem, disp);
|
2017-05-01 03:20:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
drmModeFreeConnector(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
drmModeFreeResources(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_fb(struct gbm_bo *bo, void *data)
|
|
|
|
{
|
|
|
|
uint32_t *id = data;
|
|
|
|
|
|
|
|
if (id && *id)
|
|
|
|
drmModeRmFB(gbm_bo_get_fd(bo), *id);
|
|
|
|
|
|
|
|
free(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t get_fb_for_bo(int fd, struct gbm_bo *bo)
|
|
|
|
{
|
|
|
|
uint32_t *id = gbm_bo_get_user_data(bo);
|
|
|
|
|
|
|
|
if (id)
|
|
|
|
return *id;
|
|
|
|
|
|
|
|
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-01 05:49:18 +00:00
|
|
|
static bool display_init_renderer(struct wlr_drm_renderer *renderer,
|
|
|
|
struct wlr_drm_display *disp)
|
2017-05-01 03:20:48 +00:00
|
|
|
{
|
2017-05-01 05:49:18 +00:00
|
|
|
disp->renderer = renderer;
|
|
|
|
|
|
|
|
disp->gbm = gbm_surface_create(renderer->gbm,
|
2017-05-01 03:20:48 +00:00
|
|
|
disp->width, disp->height,
|
|
|
|
GBM_FORMAT_XRGB8888,
|
|
|
|
GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING);
|
2017-05-02 01:00:25 +00:00
|
|
|
if (!disp->gbm) {
|
|
|
|
wlr_log(L_ERROR, "Failed to create GBM surface for %s: %s", disp->name,
|
|
|
|
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
|
|
|
|
2017-05-01 05:49:18 +00:00
|
|
|
disp->egl = create_platform_window_surface(renderer->egl.disp, renderer->egl.conf,
|
2017-05-01 03:20:48 +00:00
|
|
|
disp->gbm, NULL);
|
2017-05-02 01:00:25 +00:00
|
|
|
if (disp->egl == EGL_NO_SURFACE) {
|
|
|
|
wlr_log(L_ERROR, "Failed to create EGL surface for %s: %s", disp->name,
|
|
|
|
egl_error());
|
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
|
|
|
|
|
2017-05-01 05:49:18 +00:00
|
|
|
eglMakeCurrent(renderer->egl.disp, disp->egl, disp->egl, renderer->egl.context);
|
2017-05-01 03:20:48 +00:00
|
|
|
|
|
|
|
glViewport(0, 0, disp->width, disp->height);
|
|
|
|
glClearColor(0.0, 0.0, 0.0, 1.0);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
|
2017-05-01 05:49:18 +00:00
|
|
|
eglSwapBuffers(renderer->egl.disp, disp->egl);
|
2017-05-01 03:20:48 +00:00
|
|
|
|
|
|
|
struct gbm_bo *bo = gbm_surface_lock_front_buffer(disp->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
|
|
|
|
2017-05-01 05:49:18 +00:00
|
|
|
drmModeSetCrtc(renderer->fd, disp->crtc, fb_id, 0, 0,
|
2017-05-01 03:20:48 +00:00
|
|
|
&disp->connector, 1, disp->active_mode);
|
2017-05-01 05:49:18 +00:00
|
|
|
drmModePageFlip(renderer->fd, disp->crtc, fb_id, DRM_MODE_PAGE_FLIP_EVENT, disp);
|
2017-05-01 03:20:48 +00:00
|
|
|
|
|
|
|
gbm_surface_release_buffer(disp->gbm, bo);
|
2017-05-01 05:49:18 +00:00
|
|
|
|
|
|
|
return true;
|
2017-05-01 03:20:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static drmModeModeInfo *select_mode(size_t num_modes,
|
2017-05-01 05:49:18 +00:00
|
|
|
drmModeModeInfo modes[static num_modes],
|
|
|
|
drmModeCrtc *old_crtc,
|
|
|
|
const char *str)
|
2017-05-01 03:20:48 +00:00
|
|
|
{
|
|
|
|
if (strcmp(str, "preferred") == 0)
|
|
|
|
return &modes[0];
|
|
|
|
|
|
|
|
if (strcmp(str, "current") == 0) {
|
|
|
|
if (!old_crtc) {
|
2017-05-02 01:00:25 +00:00
|
|
|
wlr_log(L_ERROR, "Display does not have currently configured mode");
|
2017-05-01 03:20:48 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < num_modes; ++i) {
|
|
|
|
if (memcmp(&modes[i], &old_crtc->mode, sizeof modes[0]) == 0)
|
|
|
|
return &modes[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 01:00:25 +00:00
|
|
|
wlr_log(L_ERROR, "Invalid modesetting string '%s'", str);
|
2017-05-01 03:20:48 +00: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))
|
|
|
|
return &modes[i];
|
|
|
|
}
|
|
|
|
|
2017-05-02 01:00:25 +00:00
|
|
|
wlr_log(L_ERROR, "Unable to find mode %ux%u@%u", width, height, rate);
|
2017-05-01 03:20:48 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-05-01 05:49:18 +00:00
|
|
|
bool wlr_drm_display_modeset(struct wlr_drm_backend *backend,
|
|
|
|
struct wlr_drm_display *disp, const char *str)
|
2017-05-01 03:20:48 +00:00
|
|
|
{
|
2017-05-02 01:00:25 +00:00
|
|
|
wlr_log(L_INFO, "Modesetting %s with '%s'", disp->name, str);
|
|
|
|
|
2017-05-01 05:49:18 +00:00
|
|
|
drmModeConnector *conn = drmModeGetConnector(backend->fd, disp->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", disp->name);
|
|
|
|
goto error;
|
|
|
|
}
|
2017-05-01 03:20:48 +00:00
|
|
|
|
|
|
|
disp->num_modes = conn->count_modes;
|
|
|
|
disp->modes = malloc(sizeof *disp->modes * disp->num_modes);
|
2017-05-02 01:00:25 +00:00
|
|
|
if (!disp->modes) {
|
|
|
|
wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno));
|
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
|
|
|
memcpy(disp->modes, conn->modes, sizeof *disp->modes * disp->num_modes);
|
|
|
|
|
2017-05-02 01:00:25 +00:00
|
|
|
wlr_log(L_INFO, "Detected modes:");
|
|
|
|
for (size_t i = 0; i < disp->num_modes; ++i)
|
|
|
|
wlr_log(L_INFO, " %"PRIu16"@%"PRIu16"@%"PRIu32,
|
|
|
|
disp->modes[i].hdisplay, disp->modes[i].vdisplay,
|
|
|
|
disp->modes[i].vrefresh);
|
|
|
|
|
2017-05-01 05:49:18 +00:00
|
|
|
drmModeEncoder *curr_enc = drmModeGetEncoder(backend->fd, conn->encoder_id);
|
2017-05-01 03:20:48 +00:00
|
|
|
if (curr_enc) {
|
2017-05-01 05:49:18 +00:00
|
|
|
disp->old_crtc = drmModeGetCrtc(backend->fd, curr_enc->crtc_id);
|
2017-05-01 03:20:48 +00:00
|
|
|
free(curr_enc);
|
|
|
|
}
|
|
|
|
|
|
|
|
disp->active_mode = select_mode(disp->num_modes, disp->modes, disp->old_crtc, str);
|
|
|
|
if (!disp->active_mode) {
|
2017-05-02 01:00:25 +00:00
|
|
|
wlr_log(L_ERROR, "Failed to configure %s", disp->name);
|
2017-05-01 03:20:48 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2017-05-01 05:49:18 +00:00
|
|
|
drmModeRes *res = drmModeGetResources(backend->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-01 05:49:18 +00:00
|
|
|
drmModeEncoder *enc = drmModeGetEncoder(backend->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)
|
|
|
|
continue;
|
|
|
|
|
2017-05-01 05:49:18 +00:00
|
|
|
if ((backend->taken_crtcs & (1 << j)) == 0) {
|
|
|
|
backend->taken_crtcs |= 1 << j;
|
2017-05-01 03:20:48 +00:00
|
|
|
disp->crtc = res->crtcs[j];
|
|
|
|
|
|
|
|
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", disp->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
|
|
|
|
2017-05-01 05:49:18 +00:00
|
|
|
disp->state = DRM_DISP_CONNECTED;
|
2017-05-01 03:20:48 +00:00
|
|
|
|
|
|
|
disp->width = disp->active_mode->hdisplay;
|
|
|
|
disp->height = disp->active_mode->vdisplay;
|
|
|
|
|
2017-05-02 01:00:25 +00:00
|
|
|
if (!display_init_renderer(&backend->renderer, disp)) {
|
|
|
|
wlr_log(L_ERROR, "Failed to initalise renderer for %s", disp->name);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
drmModeFreeConnector(conn);
|
|
|
|
|
|
|
|
wlr_log(L_INFO, "Configuring %s with mode %"PRIu16"x%"PRIu16"@%"PRIu32"\n",
|
|
|
|
disp->name, disp->active_mode->hdisplay, disp->active_mode->vdisplay,
|
|
|
|
disp->active_mode->vrefresh);
|
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:
|
2017-05-01 05:49:18 +00:00
|
|
|
disp->state = DRM_DISP_DISCONNECTED;
|
2017-05-01 03:20:48 +00:00
|
|
|
drmModeFreeConnector(conn);
|
2017-05-02 01:00:25 +00:00
|
|
|
free(disp->modes);
|
2017-05-01 03:20:48 +00:00
|
|
|
|
2017-05-02 02:08:34 +00:00
|
|
|
wl_signal_emit(&backend->signals.display_rem, disp);
|
2017-05-01 03:20:48 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-02 01:00:25 +00:00
|
|
|
void wlr_drm_display_free(struct wlr_drm_display *disp, bool restore)
|
2017-05-01 03:20:48 +00:00
|
|
|
{
|
2017-05-01 05:49:18 +00:00
|
|
|
if (!disp || disp->state != DRM_DISP_CONNECTED)
|
2017-05-01 03:20:48 +00:00
|
|
|
return;
|
|
|
|
|
2017-05-01 05:49:18 +00:00
|
|
|
struct wlr_drm_renderer *renderer = disp->renderer;
|
|
|
|
|
2017-05-02 01:00:25 +00:00
|
|
|
eglDestroySurface(renderer->egl.disp, disp->egl);
|
|
|
|
gbm_surface_destroy(disp->gbm);
|
|
|
|
|
|
|
|
free(disp->modes);
|
|
|
|
disp->state = DRM_DISP_DISCONNECTED;
|
|
|
|
|
|
|
|
if (!restore)
|
|
|
|
return;
|
|
|
|
|
2017-05-01 03:20:48 +00:00
|
|
|
drmModeCrtc *crtc = disp->old_crtc;
|
|
|
|
if (crtc) {
|
2017-05-01 05:49:18 +00:00
|
|
|
// Wait for exising page flips to finish
|
|
|
|
|
2017-05-01 03:20:48 +00:00
|
|
|
drmEventContext event = {
|
|
|
|
.version = DRM_EVENT_CONTEXT_VERSION,
|
|
|
|
.page_flip_handler = page_flip_handler,
|
|
|
|
};
|
|
|
|
|
|
|
|
disp->cleanup = true;
|
|
|
|
while (disp->pageflip_pending)
|
2017-05-01 05:49:18 +00:00
|
|
|
drmHandleEvent(renderer->fd, &event);
|
2017-05-01 03:20:48 +00:00
|
|
|
|
2017-05-01 05:49:18 +00:00
|
|
|
drmModeSetCrtc(renderer->fd, crtc->crtc_id, crtc->buffer_id,
|
2017-05-01 03:20:48 +00:00
|
|
|
crtc->x, crtc->y, &disp->connector,
|
|
|
|
1, &crtc->mode);
|
|
|
|
drmModeFreeCrtc(crtc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-01 05:49:18 +00:00
|
|
|
void wlr_drm_display_begin(struct wlr_drm_display *disp)
|
2017-05-01 03:20:48 +00:00
|
|
|
{
|
2017-05-01 05:49:18 +00:00
|
|
|
struct wlr_drm_renderer *renderer = disp->renderer;
|
|
|
|
eglMakeCurrent(renderer->egl.disp, disp->egl, disp->egl, renderer->egl.context);
|
2017-05-01 03:20:48 +00:00
|
|
|
}
|
|
|
|
|
2017-05-01 05:49:18 +00:00
|
|
|
void wlr_drm_display_end(struct wlr_drm_display *disp)
|
2017-05-01 03:20:48 +00:00
|
|
|
{
|
2017-05-01 05:49:18 +00:00
|
|
|
struct wlr_drm_renderer *renderer = disp->renderer;
|
|
|
|
eglSwapBuffers(renderer->egl.disp, disp->egl);
|
2017-05-01 03:20:48 +00:00
|
|
|
|
|
|
|
struct gbm_bo *bo = gbm_surface_lock_front_buffer(disp->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
|
|
|
|
2017-05-01 05:49:18 +00:00
|
|
|
drmModePageFlip(renderer->fd, disp->crtc, fb_id, DRM_MODE_PAGE_FLIP_EVENT, disp);
|
2017-05-01 03:20:48 +00:00
|
|
|
|
|
|
|
gbm_surface_release_buffer(disp->gbm, bo);
|
|
|
|
|
|
|
|
disp->pageflip_pending = false;
|
|
|
|
}
|