2018-05-06 19:35:47 +02:00
|
|
|
#include "modeset.h"
|
|
|
|
|
2018-05-13 16:29:43 +02:00
|
|
|
static int modeset_find_crtc(int fd, drmModeRes *res, drmModeConnector *conn,
|
2018-05-13 20:29:47 +02:00
|
|
|
struct modeset_dev *dev);
|
2018-05-13 16:29:43 +02:00
|
|
|
static int modeset_setup_dev(int fd, drmModeRes *res, drmModeConnector *conn,
|
2018-05-13 20:29:47 +02:00
|
|
|
struct modeset_dev *dev);
|
2018-05-06 19:35:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When the linux kernel detects a graphics-card on your machine, it loads the
|
|
|
|
* correct device driver (located in kernel-tree at ./drivers/gpu/drm/<xy>) and
|
|
|
|
* provides two character-devices to control it. Udev (or whatever hotplugging
|
|
|
|
* application you use) will create them as:
|
|
|
|
* /dev/dri/card0
|
|
|
|
* /dev/dri/controlID64
|
|
|
|
* We only need the first one. You can hard-code this path into your application
|
|
|
|
* like we do here, but it is recommended to use libudev with real hotplugging
|
|
|
|
* and multi-seat support. However, this is beyond the scope of this document.
|
|
|
|
* Also note that if you have multiple graphics-cards, there may also be
|
|
|
|
* /dev/dri/card1, /dev/dri/card2, ...
|
|
|
|
*
|
|
|
|
* We simply use /dev/dri/card0 here but the user can specify another path on
|
|
|
|
* the command line.
|
|
|
|
*
|
|
|
|
* modeset_open(out, node): This small helper function opens the DRM device
|
|
|
|
* which is given as @node. The new fd is stored in @out on success. On failure,
|
|
|
|
* a negative error code is returned.
|
|
|
|
* After opening the file, we also check for the DRM_CAP_DUMB_BUFFER capability.
|
|
|
|
* If the driver supports this capability, we can create simple memory-mapped
|
|
|
|
* buffers without any driver-dependent code. As we want to avoid any radeon,
|
|
|
|
* nvidia, intel, etc. specific code, we depend on DUMB_BUFFERs here.
|
|
|
|
*/
|
|
|
|
|
2018-05-13 16:29:43 +02:00
|
|
|
modeset_dev* modeset_create(int fd)
|
2018-05-06 19:35:47 +02:00
|
|
|
{
|
2018-05-07 17:13:39 +02:00
|
|
|
modeset_dev* ret_dev = 0;
|
2018-05-06 19:35:47 +02:00
|
|
|
drmModeRes *res;
|
|
|
|
drmModeConnector *conn;
|
|
|
|
struct modeset_dev *dev;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
// retrieve resources
|
|
|
|
res = drmModeGetResources(fd);
|
|
|
|
if (!res) {
|
2019-09-07 18:41:46 +02:00
|
|
|
fprintf(stderr, "cannot retrieve DRM resources (%d): %m\n", errno);
|
2018-05-07 17:13:39 +02:00
|
|
|
return 0;
|
2018-05-06 19:35:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// iterate all connectors
|
|
|
|
for (unsigned i = 0; i < res->count_connectors; ++i) {
|
|
|
|
// get information for each connector
|
|
|
|
conn = drmModeGetConnector(fd, res->connectors[i]);
|
|
|
|
if (!conn) {
|
2019-09-07 18:41:46 +02:00
|
|
|
fprintf(stderr, "cannot retrieve DRM connector %u:%u (%d): %m\n", i, res->connectors[i], errno);
|
2018-05-06 19:35:47 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a device structure
|
|
|
|
dev = malloc(sizeof(*dev));
|
|
|
|
memset(dev, 0, sizeof(*dev));
|
|
|
|
dev->conn = conn->connector_id;
|
|
|
|
|
|
|
|
// call helper function to prepare this connector
|
2018-05-13 16:29:43 +02:00
|
|
|
ret = modeset_setup_dev(fd, res, conn, dev);
|
2018-05-06 19:35:47 +02:00
|
|
|
if (ret) {
|
|
|
|
if (ret != -ENOENT) {
|
|
|
|
errno = -ret;
|
2019-09-07 18:41:46 +02:00
|
|
|
fprintf(stderr, "cannot setup device for connector %u:%u (%d): %m\n", i, res->connectors[i], errno);
|
2018-05-06 19:35:47 +02:00
|
|
|
}
|
|
|
|
free(dev);
|
|
|
|
drmModeFreeConnector(conn);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// free connector data and link device into global list
|
|
|
|
drmModeFreeConnector(conn);
|
2018-05-07 17:13:39 +02:00
|
|
|
dev->next = ret_dev;
|
|
|
|
ret_dev = dev;
|
2018-05-06 19:35:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// free resources again
|
|
|
|
drmModeFreeResources(res);
|
|
|
|
|
2018-06-03 14:56:59 +02:00
|
|
|
return ret_dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
int modeset_fb_for_dev(int fd, modeset_dev* dev, _image* buffer)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2018-05-06 19:35:47 +02:00
|
|
|
struct modeset_dev *iter;
|
2018-06-03 14:56:59 +02:00
|
|
|
//struct modeset_buf *buf;
|
|
|
|
for (iter = dev; iter; iter = iter->next) {
|
2018-05-06 19:35:47 +02:00
|
|
|
iter->saved_crtc = drmModeGetCrtc(fd, iter->crtc);
|
2018-06-03 14:56:59 +02:00
|
|
|
ret = drmModeSetCrtc(fd, iter->crtc, buffer->fb, 0, 0,
|
2018-05-13 20:29:47 +02:00
|
|
|
&iter->conn, 1, &iter->mode);
|
2018-05-06 19:35:47 +02:00
|
|
|
if (ret)
|
2019-09-07 18:41:46 +02:00
|
|
|
fprintf(stderr, "cannot set CRTC for connector %u (%d): %m\n",
|
2018-05-13 20:29:47 +02:00
|
|
|
iter->conn, errno);
|
2018-05-06 19:35:47 +02:00
|
|
|
}
|
2018-05-07 17:13:39 +02:00
|
|
|
|
2018-06-03 14:56:59 +02:00
|
|
|
return 0;
|
2018-05-06 19:35:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* modeset_setup_dev() sets up all resources for a single device. It mostly
|
|
|
|
* stays the same, but one thing changes: We allocate two framebuffers instead
|
|
|
|
* of one. That is, we call modeset_create_fb() twice.
|
|
|
|
* We also copy the width/height information into both framebuffers so
|
|
|
|
* modeset_create_fb() can use them without requiring a pointer to modeset_dev.
|
|
|
|
*/
|
|
|
|
|
2018-05-13 16:29:43 +02:00
|
|
|
static int modeset_setup_dev(int fd, drmModeRes *res, drmModeConnector *conn,
|
2018-05-13 20:29:47 +02:00
|
|
|
struct modeset_dev *dev)
|
2018-05-06 19:35:47 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
// check if a monitor is connected
|
|
|
|
if (conn->connection != DRM_MODE_CONNECTED) {
|
2019-09-07 18:41:46 +02:00
|
|
|
fprintf(stderr, "ignoring unused connector %u\n",
|
2018-05-13 20:29:47 +02:00
|
|
|
conn->connector_id);
|
2018-05-06 19:35:47 +02:00
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if there is at least one valid mode
|
|
|
|
if (conn->count_modes == 0) {
|
2019-09-07 18:41:46 +02:00
|
|
|
fprintf(stderr, "no valid mode for connector %u\n",
|
2018-05-13 20:29:47 +02:00
|
|
|
conn->connector_id);
|
2018-05-06 19:35:47 +02:00
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy the mode information into our device structure and into both buffers
|
|
|
|
memcpy(&dev->mode, &conn->modes[0], sizeof(dev->mode));
|
2018-06-03 14:56:59 +02:00
|
|
|
dev->width = conn->modes[0].hdisplay;
|
|
|
|
dev->height = conn->modes[0].vdisplay;
|
2018-05-06 19:35:47 +02:00
|
|
|
printf("mode for connector %u is %ux%u\n",
|
2018-06-03 14:56:59 +02:00
|
|
|
conn->connector_id, dev->width, dev->height);
|
2018-05-06 19:35:47 +02:00
|
|
|
|
|
|
|
// find a crtc for this connector
|
2018-05-13 16:29:43 +02:00
|
|
|
ret = modeset_find_crtc(fd, res, conn, dev);
|
2018-05-06 19:35:47 +02:00
|
|
|
if (ret) {
|
2019-09-07 18:41:46 +02:00
|
|
|
fprintf(stderr, "no valid crtc for connector %u\n",
|
2018-05-13 20:29:47 +02:00
|
|
|
conn->connector_id);
|
2018-05-06 19:35:47 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-06-03 14:56:59 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-06 19:35:47 +02:00
|
|
|
/*
|
|
|
|
* modeset_find_crtc() stays the same.
|
|
|
|
*/
|
|
|
|
|
2018-05-13 16:29:43 +02:00
|
|
|
static int modeset_find_crtc(int fd, drmModeRes *res, drmModeConnector *conn,
|
2018-05-13 20:29:47 +02:00
|
|
|
modeset_dev *dev)
|
2018-05-06 19:35:47 +02:00
|
|
|
{
|
|
|
|
drmModeEncoder *enc;
|
|
|
|
unsigned int i, j;
|
|
|
|
int32_t crtc;
|
|
|
|
struct modeset_dev *iter;
|
|
|
|
|
|
|
|
// first try the currently conected encoder+crtc
|
|
|
|
if (conn->encoder_id)
|
|
|
|
enc = drmModeGetEncoder(fd, conn->encoder_id);
|
|
|
|
else
|
|
|
|
enc = NULL;
|
|
|
|
|
|
|
|
if (enc) {
|
|
|
|
if (enc->crtc_id) {
|
|
|
|
crtc = enc->crtc_id;
|
2018-05-07 17:13:39 +02:00
|
|
|
for (iter = dev; iter; iter = iter->next) {
|
2018-05-06 19:35:47 +02:00
|
|
|
if (iter->crtc == crtc) {
|
|
|
|
crtc = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (crtc >= 0) {
|
|
|
|
drmModeFreeEncoder(enc);
|
|
|
|
dev->crtc = crtc;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
drmModeFreeEncoder(enc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the connector is not currently bound to an encoder or if the
|
|
|
|
* encoder+crtc is already used by another connector (actually unlikely
|
|
|
|
* but lets be safe), iterate all other available encoders to find a
|
|
|
|
* matching CRTC. */
|
|
|
|
for (i = 0; i < conn->count_encoders; ++i) {
|
|
|
|
enc = drmModeGetEncoder(fd, conn->encoders[i]);
|
|
|
|
if (!enc) {
|
2019-09-07 18:41:46 +02:00
|
|
|
fprintf(stderr, "cannot retrieve encoder %u:%u (%d): %m\n",
|
2018-05-13 20:29:47 +02:00
|
|
|
i, conn->encoders[i], errno);
|
2018-05-06 19:35:47 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterate all global CRTCs
|
|
|
|
for (j = 0; j < res->count_crtcs; ++j) {
|
|
|
|
// check whether this CRTC works with the encoder
|
|
|
|
if (!(enc->possible_crtcs & (1 << j)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// check that no other device already uses this CRTC
|
|
|
|
crtc = res->crtcs[j];
|
2018-05-07 17:13:39 +02:00
|
|
|
for (iter = dev; iter; iter = iter->next) {
|
2018-05-06 19:35:47 +02:00
|
|
|
if (iter->crtc == crtc) {
|
|
|
|
crtc = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// we have found a CRTC, so save it and return
|
|
|
|
if (crtc >= 0) {
|
|
|
|
drmModeFreeEncoder(enc);
|
|
|
|
dev->crtc = crtc;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
drmModeFreeEncoder(enc);
|
|
|
|
}
|
|
|
|
|
2019-09-07 18:41:46 +02:00
|
|
|
fprintf(stderr, "cannot find suitable CRTC for connector %u\n",
|
2018-05-13 20:29:47 +02:00
|
|
|
conn->connector_id);
|
2018-05-06 19:35:47 +02:00
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* modeset_create_fb() is mostly the same as before. Buf instead of writing the
|
|
|
|
* fields of a modeset_dev, we now require a buffer pointer passed as @buf.
|
|
|
|
* Please note that buf->width and buf->height are initialized by
|
|
|
|
* modeset_setup_dev() so we can use them here.
|
|
|
|
*/
|
|
|
|
|
2018-06-03 14:56:59 +02:00
|
|
|
int modeset_create_fb(int fd, _image *buf)
|
2018-05-06 19:35:47 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
// create framebuffer object for the dumb-buffer
|
|
|
|
ret = drmModeAddFB(fd, buf->width, buf->height, 24, 32, buf->stride,
|
2018-10-20 16:46:12 +02:00
|
|
|
buf->boundMem->bo, &buf->fb);
|
2018-05-06 19:35:47 +02:00
|
|
|
if (ret) {
|
2019-09-07 18:41:46 +02:00
|
|
|
fprintf(stderr, "cannot create framebuffer (%d): %m\n",
|
2018-05-13 20:29:47 +02:00
|
|
|
errno);
|
2018-05-06 19:35:47 +02:00
|
|
|
ret = -errno;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* modeset_destroy_fb() is a new function. It does exactly the reverse of
|
|
|
|
* modeset_create_fb() and destroys a single framebuffer. The modeset.c example
|
|
|
|
* used to do this directly in modeset_cleanup().
|
|
|
|
* We simply unmap the buffer, remove the drm-FB and destroy the memory buffer.
|
|
|
|
*/
|
|
|
|
|
2018-06-03 14:56:59 +02:00
|
|
|
void modeset_destroy_fb(int fd, _image* buf)
|
2018-05-06 19:35:47 +02:00
|
|
|
{
|
|
|
|
// delete framebuffer
|
|
|
|
drmModeRmFB(fd, buf->fb);
|
|
|
|
}
|
|
|
|
|
2018-06-03 14:56:59 +02:00
|
|
|
void modeset_present_buffer(int fd, modeset_dev* dev, _image* buffer)
|
2018-05-06 19:35:47 +02:00
|
|
|
{
|
2018-05-09 22:02:32 +02:00
|
|
|
//TODO use index!!
|
|
|
|
|
2018-08-22 22:20:29 +02:00
|
|
|
if(!dev->saved_crtc)
|
|
|
|
{
|
|
|
|
int res = modeset_fb_for_dev(fd, dev, buffer); assert(res == 0);
|
|
|
|
}
|
|
|
|
|
2018-05-06 19:35:47 +02:00
|
|
|
struct modeset_dev *iter;
|
|
|
|
int ret;
|
|
|
|
|
2018-06-03 14:56:59 +02:00
|
|
|
for (iter = dev; iter; iter = iter->next)
|
|
|
|
{
|
|
|
|
ret = drmModeSetCrtc(fd, iter->crtc, buffer->fb, 0, 0,
|
2018-05-13 20:29:47 +02:00
|
|
|
&iter->conn, 1, &iter->mode);
|
2018-05-06 19:35:47 +02:00
|
|
|
if (ret)
|
2019-09-07 18:41:46 +02:00
|
|
|
fprintf(stderr, "cannot flip CRTC for connector %u (%d): %m\n",
|
2018-05-13 20:29:47 +02:00
|
|
|
iter->conn, errno);
|
2018-05-06 19:35:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* modeset_cleanup() stays the same as before. But it now calls
|
|
|
|
* modeset_destroy_fb() instead of accessing the framebuffers directly.
|
|
|
|
*/
|
|
|
|
|
2018-05-13 16:29:43 +02:00
|
|
|
void modeset_destroy(int fd, modeset_dev* dev)
|
2018-05-06 19:35:47 +02:00
|
|
|
{
|
|
|
|
struct modeset_dev *iter;
|
|
|
|
|
2018-05-07 17:13:39 +02:00
|
|
|
while (dev) {
|
2018-05-06 19:35:47 +02:00
|
|
|
// remove from global list
|
2018-05-07 17:13:39 +02:00
|
|
|
iter = dev;
|
|
|
|
dev = iter->next;
|
2018-05-06 19:35:47 +02:00
|
|
|
|
|
|
|
// restore saved CRTC configuration
|
|
|
|
drmModeSetCrtc(fd,
|
2018-05-13 20:29:47 +02:00
|
|
|
iter->saved_crtc->crtc_id,
|
|
|
|
iter->saved_crtc->buffer_id,
|
|
|
|
iter->saved_crtc->x,
|
|
|
|
iter->saved_crtc->y,
|
|
|
|
&iter->conn,
|
|
|
|
1,
|
|
|
|
&iter->saved_crtc->mode);
|
2018-05-06 19:35:47 +02:00
|
|
|
drmModeFreeCrtc(iter->saved_crtc);
|
|
|
|
|
|
|
|
// free allocated memory
|
|
|
|
free(iter);
|
|
|
|
}
|
2018-05-07 17:13:39 +02:00
|
|
|
}
|
2018-05-06 19:35:47 +02:00
|
|
|
|
2020-04-16 01:04:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void modeset_enum_displays(int fd, uint32_t* numDisplays, modeset_display** displays)
|
|
|
|
{
|
|
|
|
drmModeResPtr resPtr = drmModeGetResources(fd);
|
|
|
|
|
|
|
|
uint32_t tmpNumDisplays = 0;
|
|
|
|
modeset_display tmpDisplays[16];
|
|
|
|
|
|
|
|
for(uint32_t c = 0; c < resPtr->count_connectors; ++c)
|
|
|
|
{
|
|
|
|
drmModeConnectorPtr connPtr = drmModeGetConnector(fd, resPtr->connectors[c]);
|
|
|
|
|
|
|
|
if(connPtr->connection != DRM_MODE_CONNECTED)
|
|
|
|
{
|
|
|
|
continue; //skip unused connector
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!connPtr->count_modes)
|
|
|
|
{
|
|
|
|
continue; //skip connectors with no valid modes
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(tmpDisplays[tmpNumDisplays].name, connPtr->modes[0].name, 32);
|
|
|
|
|
|
|
|
tmpDisplays[tmpNumDisplays].mmWidth = connPtr->mmWidth;
|
|
|
|
tmpDisplays[tmpNumDisplays].mmHeight = connPtr->mmHeight;
|
|
|
|
tmpDisplays[tmpNumDisplays].resWidth = connPtr->modes[0].hdisplay;
|
|
|
|
tmpDisplays[tmpNumDisplays].resHeight = connPtr->modes[0].vdisplay;
|
|
|
|
tmpDisplays[tmpNumDisplays].connectorID = connPtr->connector_id;
|
|
|
|
|
|
|
|
tmpNumDisplays++;
|
|
|
|
|
|
|
|
assert(tmpNumDisplays < 16);
|
|
|
|
|
|
|
|
drmModeFreeConnector(connPtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
drmModeFreeResources(resPtr);
|
|
|
|
|
|
|
|
*numDisplays = tmpNumDisplays;
|
|
|
|
|
|
|
|
memcpy(*displays, tmpDisplays, tmpNumDisplays * sizeof(modeset_display));
|
|
|
|
}
|
|
|
|
|
|
|
|
void modeset_enum_modes_for_display(int fd, uint32_t display, uint32_t* numModes, modeset_display_mode** modes)
|
|
|
|
{
|
|
|
|
drmModeResPtr resPtr = drmModeGetResources(fd);
|
|
|
|
|
|
|
|
drmModeConnectorPtr connPtr = drmModeGetConnector(fd, display);
|
|
|
|
|
|
|
|
uint32_t tmpNumModes = 0;
|
|
|
|
modeset_display_mode tmpModes[1024];
|
|
|
|
|
|
|
|
for(uint32_t c = 0; c < connPtr->count_modes; ++c)
|
|
|
|
{
|
|
|
|
tmpModes[tmpNumModes].connectorID = display;
|
|
|
|
tmpModes[tmpNumModes].modeID = c;
|
|
|
|
tmpModes[tmpNumModes].refreshRate = connPtr->modes[c].vrefresh;
|
|
|
|
tmpModes[tmpNumModes].resWidth = connPtr->modes[c].hdisplay;
|
|
|
|
tmpModes[tmpNumModes].resHeight = connPtr->modes[c].vdisplay;
|
|
|
|
|
|
|
|
tmpNumModes++;
|
|
|
|
|
|
|
|
assert(tmpNumModes < 1024);
|
|
|
|
}
|
|
|
|
|
|
|
|
drmModeFreeConnector(connPtr);
|
|
|
|
drmModeFreeResources(resPtr);
|
|
|
|
|
|
|
|
*numModes = tmpNumModes;
|
|
|
|
memcpy(*modes, tmpModes, tmpNumModes * sizeof(modeset_display_mode));
|
|
|
|
}
|
|
|
|
|
|
|
|
void modeset_create_surface_for_mode(int fd, uint32_t display, uint32_t mode, modeset_display_surface* surface)
|
|
|
|
{
|
|
|
|
drmModeResPtr resPtr = drmModeGetResources(fd);
|
|
|
|
|
|
|
|
drmModeConnectorPtr connPtr = drmModeGetConnector(fd, display);
|
|
|
|
|
|
|
|
drmModeEncoderPtr encPtr = 0;
|
|
|
|
|
|
|
|
//if current encoder is valid, try to use that
|
|
|
|
if(connPtr->encoder_id)
|
|
|
|
{
|
|
|
|
encPtr = drmModeGetEncoder(fd, connPtr->encoder_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(encPtr)
|
|
|
|
{
|
|
|
|
if(encPtr->crtc_id)
|
|
|
|
{
|
|
|
|
surface->connectorID = display;
|
|
|
|
surface->modeID = mode;
|
|
|
|
surface->encoderID = connPtr->encoder_id;
|
|
|
|
surface->crtcID = encPtr->crtc_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|