最简单的DRM应用程序 (single-buffer)

最简单的DRM应用程序 (single-buffer)

详见:https://blog.csdn.net/hexiaolong2009/article/details/83721242

参考代码路径为:https://github.com/dvdhrm/docs/blob/master/drm-howto/modeset.c

代码:drmdisplay.cpp

#include <utils/Log.h>
#include <unistd.h>
#include <inttypes.h>
#include <cutils/properties.h>

extern "C"{
#include<utils/Log.h>
}

#include <cutils/memory.h>
#include <unistd.h>
#include <utils/Log.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>

#include <xf86drm.h>
#include <xf86drmMode.h>
#include <drm_fourcc.h>
#include <drm.h>
#include <linux/ion.h>
#include <linux/msm_ion.h>
#include <libdrm_macros.h>
#include <thread>
#include <fstream>
#include <mutex>
#include <vector>

struct drmdisplay_dev;
static int drmdisplay_find_crtc(int fd, drmModeRes *res, drmModeConnector *conn,
			     struct drmdisplay_dev *dev);
static int drmdisplay_create_fb(int fd, struct drmdisplay_dev *dev);
static int drmdisplay_setup_dev(int fd, drmModeRes *res, drmModeConnector *conn,
			     struct drmdisplay_dev *dev);
static int drmdisplay_open(int *out, const char *node);
static int drmdisplay_prepare(int fd);
static void drmdisplay_draw(void);
static void drmdisplay_cleanup(int fd);

struct drmdisplay_dev {
    struct drmdisplay_dev *next;

    uint32_t width;
	uint32_t height;
	uint32_t stride;
	size_t size;
	uint32_t handle;
	uint8_t *map;

	drmModeModeInfo mode;
	uint32_t fb;
	uint32_t conn;
	int crtc;
	drmModeCrtc *saved_crtc;
};

static struct drmdisplay_dev *drmdisplay_list = NULL;

static int drmdisplay_open(int *out, const char *node)
{
    int fd, ret;
    uint64_t has_dumb;

    fd = open(node, O_RDWR | O_CLOEXEC);
    if (fd < 0)
    {
        ret = -errno;
        fprintf(stderr, "open file fail: %d",ret);
        return ret;
    }

    if (drmGetCap(fd, DRM_CAP_DUMB_BUFFER, &has_dumb) < 0 || !has_dumb)
    {
        fprintf(stderr, "drm device '%s' does not support dumb buffers \n",node);
        close(fd);
        return -EOPNOTSUPP;
    }

    *out = fd;
    return 0;
}

static int drmdisplay_prepare(int fd)
{
	drmModeRes *res;
	drmModeConnector *conn;
	int i;
	struct drmdisplay_dev *dev;
	int ret;

	/* retrieve resources */
	res = drmModeGetResources(fd);
	if (!res) {
		fprintf(stderr, "cannot retrieve DRM resources (%d): %m\n", errno);
		return -errno;
	}

	/* iterate all connectors */
	fprintf(stderr, "drmdisplay_prepare res->count_connectors (%d): %m\n", res->count_connectors);
	for (i = 0; i < res->count_connectors; ++i) {
		/* get information for each connector */
		conn = drmModeGetConnector(fd, res->connectors[i]);
		if (!conn) {
			fprintf(stderr, "cannot retrieve DRM connector %u:%u (%d): %m\n", i, res->connectors[i], errno);
			continue;
		}

		/* create a device structure */
		// dev = malloc(sizeof(*dev));
     	dev = (struct drmdisplay_dev *)malloc(sizeof(struct drmdisplay_dev));
		memset(dev, 0, sizeof(*dev));
		dev->conn = conn->connector_id;

		/* call helper function to prepare this connector */
		ret = drmdisplay_setup_dev(fd, res, conn, dev);
		if (ret) {
			if (ret != -ENOENT) {
				errno = -ret;
				fprintf(stderr, "cannot setup device for connector %u:%u (%d): %m\n", i, res->connectors[i], errno);
			}
			free(dev);
			drmModeFreeConnector(conn);
			continue;
		}

		/* free connector data and link device into global list */
		drmModeFreeConnector(conn);
		dev->next = drmdisplay_list;
		drmdisplay_list = dev;
	}

	/* free resources again */
	drmModeFreeResources(res);
	return 0;
}


static int drmdisplay_setup_dev(int fd, drmModeRes *res, drmModeConnector *conn, struct drmdisplay_dev *dev)
{
	int ret;

	/* check if a monitor is connected */
	if (conn->connection != DRM_MODE_CONNECTED) {
		fprintf(stderr, "ignoring unused connector %u\n", conn->connector_id);
		return -ENOENT;
	}

	/* check if there is at least one valid mode */
	if (conn->count_modes == 0) {
		fprintf(stderr, "no valid mode for connector %u\n", conn->connector_id);
		return -EFAULT;
	}

	/* copy the mode information into our device structure */
	memcpy(&dev->mode, &conn->modes[0], sizeof(dev->mode));
	dev->width = conn->modes[0].hdisplay;
	dev->height = conn->modes[0].vdisplay;
	fprintf(stderr, "mode for connector %u is %ux%u\n", conn->connector_id, dev->width, dev->height);

	/* find a crtc for this connector */
	ret = drmdisplay_find_crtc(fd, res, conn, dev);
	if (ret) {
		fprintf(stderr, "no valid crtc for connector %u\n", conn->connector_id);
		return ret;
	}

	/* create a framebuffer for this CRTC */
	ret = drmdisplay_create_fb(fd, dev);
	if (ret) {
		fprintf(stderr, "cannot create framebuffer for connector %u\n", conn->connector_id);
		return ret;
	}

	return 0;
}

static int drmdisplay_find_crtc(int fd, drmModeRes *res, drmModeConnector *conn, struct drmdisplay_dev *dev)
{
	drmModeEncoder *enc;
	int i, j;
	int32_t crtc;
	struct drmdisplay_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;
			for (iter = drmdisplay_list; iter; iter = iter->next) {
				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) {
			fprintf(stderr, "cannot retrieve encoder %u:%u (%d): %m\n", i, conn->encoders[i], errno);
			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];
			for (iter = drmdisplay_list; iter; iter = iter->next) {
				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);
	}

	fprintf(stderr, "cannot find suitable CRTC for connector %u\n",conn->connector_id);
	return -ENOENT;
}

static int drmdisplay_create_fb(int fd, struct drmdisplay_dev *dev)
{
	struct drm_mode_create_dumb creq;
	struct drm_mode_destroy_dumb dreq;
	struct drm_mode_map_dumb mreq;
	int ret;

    /* create dumb buffer */
    memset(&creq, 0, sizeof(creq));

    creq.width = dev->width;
    creq.height = dev->height;
    creq.bpp = 32;

	ret = drmIoctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &creq);
	if (ret < 0) {
		fprintf(stderr, "cannot create dumb buffer (%d): %m\n",errno);
		return -errno;
	}

	dev->stride = creq.pitch;
	dev->size = creq.size;
	dev->handle = creq.handle;

    /* create framebuffer object for the dumb-buffer */
	ret = drmModeAddFB(fd, dev->width, dev->height, 24, 32, dev->stride,
			   dev->handle, &dev->fb);
	if (ret) {
		fprintf(stderr, "cannot create framebuffer (%d): %m\n",errno);
		ret = -errno;
		memset(&dreq, 0, sizeof(dreq));
		dreq.handle = dev->handle;
		drmIoctl(fd, DRM_IOCTL_MODE_DESTROY_DUMB, &dreq);
		return ret;
	}

	/* prepare buffer for memory mapping */
	memset(&mreq, 0, sizeof(mreq));
	mreq.handle = dev->handle;
	ret = drmIoctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &mreq);
	if (ret) {
		fprintf(stderr, "cannot map dumb buffer (%d): %m\n", errno);
		ret = -errno;
		drmModeRmFB(fd, dev->fb);
		return ret;
	}

	/* perform actual memory mapping */
	// dev->map = mmap(0, dev->size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, mreq.offset);
	dev->map = static_cast<uint8_t *>(mmap(0, dev->size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, mreq.offset));
	if (dev->map == MAP_FAILED) {
		fprintf(stderr, "cannot mmap dumb buffer (%d): %m\n", errno);
		ret = -errno;
		drmModeRmFB(fd, dev->fb);
		return ret;
	}

	/* clear the framebuffer to 0 */
	memset(dev->map, 0, dev->size);

	return 0;
}

/*
 * A short helper function to compute a changing color value. No need to
 * understand it.
 */
static uint8_t next_color(bool *up, uint8_t cur, unsigned int mod)
{
	uint8_t next;

	next = cur + (*up ? 1 : -1) * (rand() % mod);
	if ((*up && next < cur) || (!*up && next > cur)) {
		*up = !*up;
		next = cur;
	}

	return next;
}


/*
 * drmdisplay_draw(): This draws a solid color into all configured framebuffers.
 * Every 100ms the color changes to a slightly different color so we get some
 * kind of smoothly changing color-gradient.
 *
 * The color calculation can be ignored as it is pretty boring. So the
 * interesting stuff is iterating over "drmdisplay_list" and then through all lines
 * and width. We then set each pixel individually to the current color.
 *
 * We do this 50 times as we sleep 100ms after each redraw round. This makes
 * 50*100ms = 5000ms = 5s so it takes about 5seconds to finish this loop.
 *
 * Please note that we draw directly into the framebuffer. This means that you
 * will see flickering as the monitor might refresh while we redraw the screen.
 * To avoid this you would need to use two framebuffers and a call to
 * drmModeSetCrtc() to switch between both buffers.
 * You can also use drmModePageFlip() to do a vsync'ed pageflip. But this is
 * beyond the scope of this document.
 */

static void drmdisplay_draw(void)
{
	uint8_t r, g, b;
	bool r_up, g_up, b_up;
	unsigned int i, j, k, off;
	struct drmdisplay_dev *iter;

	srand(time(NULL));
	r = rand() % 0xff;
	g = rand() % 0xff;
	b = rand() % 0xff;
	r_up = g_up = b_up = true;

	for (i = 0; i < 50; ++i) {
		r = next_color(&r_up, r, 20);
		g = next_color(&g_up, g, 10);
		b = next_color(&b_up, b, 5);

		for (iter = drmdisplay_list; iter; iter = iter->next) {
			for (j = 0; j < iter->height; ++j) {
				for (k = 0; k < iter->width; ++k) {
					off = iter->stride * j + k * 4;
					*(uint32_t*)&iter->map[off] =
						     (r << 16) | (g << 8) | b;
				}
			}
		}

		usleep(100000);
	}
}

static void drmdisplay_cleanup(int fd)
{
	struct drmdisplay_dev *iter;
	struct drm_mode_destroy_dumb dreq;

	while (drmdisplay_list) {
		/* remove from global list */
		iter = drmdisplay_list;
		drmdisplay_list = iter->next;

		/* restore saved CRTC configuration */
		drmModeSetCrtc(fd,
			       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);
		drmModeFreeCrtc(iter->saved_crtc);

		/* unmap buffer */
		munmap(iter->map, iter->size);

		/* delete framebuffer */
		drmModeRmFB(fd, iter->fb);

		/* delete dumb buffer */
		memset(&dreq, 0, sizeof(dreq));
		dreq.handle = iter->handle;
		drmIoctl(fd, DRM_IOCTL_MODE_DESTROY_DUMB, &dreq);

		/* free allocated memory */
		free(iter);
	}
}

int main() {
    fprintf(stderr, "drmdisplay starting\n");
	int ret, fd;
	const char *card;
	struct drmdisplay_dev *iter;

    card = "/dev/dri/card2";

	/* open the DRM device */
	ret = drmdisplay_open(&fd, card);
	if (ret) {
		fprintf(stderr, "drmdisplay modeset_open fail\n");
		if (ret) {
			errno = -ret;
			fprintf(stderr, "modeset failed with error %d: %m\n", errno);
		} else {
			fprintf(stderr, "exiting\n");
		}
		return ret;
	}
	/* prepare all connectors and CRTCs */
	ret = drmdisplay_prepare(fd);
	if (ret) {
		fprintf(stderr, "drmdisplay drmdisplay_prepare fail\n");
		close(fd);
		return ret;
	}
	/* perform actual modesetting on each found connector+CRTC */
	for (iter = drmdisplay_list; iter; iter = iter->next) {
		iter->saved_crtc = drmModeGetCrtc(fd, iter->crtc);
		ret = drmModeSetCrtc(fd, iter->crtc, iter->fb, 0, 0,
				     &iter->conn, 1, &iter->mode);
		if (ret)
			fprintf(stderr, "cannot set CRTC for connector %u (%d): %m\n", iter->conn, errno);
	}

	/* draw some colors for 5seconds */
	drmdisplay_draw();

	/* cleanup everything */
	drmdisplay_cleanup(fd);

	ret = 0;
    fprintf(stderr, "drmdisplay stopped.  Exiting.\n");

    return ret;
}```

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值