drm学习笔记4-ubuntu DRM API演示

介绍

本文是根据学习何小龙 最简单的DRM应用程序  做的笔记。该文主要熟悉下modeset-single-buffer涉及的API以及在ubuntu环境中演示。

需要给出DRM pipeline的框架图,不详细介绍,方便更好的理解代码

这里借用下何小龙的伪代码:

int main(int argc, char **argv)
{
	/* open the drm device */
	open("/dev/dri/card0");

	/* get crtc/encoder/connector id */
	drmModeGetResources(...);

	/* get connector for display mode */
	drmModeGetConnector(...);

	/* create a dumb-buffer */
	drmIoctl(DRM_IOCTL_MODE_CREATE_DUMB);

	/* bind the dumb-buffer to an FB object */
	drmModeAddFB(...);

	/* map the dumb buffer for userspace drawing */
	drmIoctl(DRM_IOCTL_MODE_MAP_DUMB);
	mmap(...);

	/* start display */
	drmModeSetCrtc(crtc_id, fb_id, connector_id, mode);
}

API

drmModeResPtr drmModeGetResources(int fd)

作用:分配、填充并返回一个 drmModeRes 结构,其中包含有关当前显示配置的信息。

drmModeRes 结构体如下:

typedef struct _drmModeRes {
    // 分配的framebuffer objects的数量
    int count_fbs;
    uint32_t *fbs;

    // 配置中可用的CRTCs,CRTC可以将framebuffer送到显示接收器,
    // 并包含mode timing信号和相对位置信息,CRTCs驱动encoders,encoder负责将像素流转换为特定的显 
    // 示协议(例如,MIPI或HDMI)。
    int count_crtcs;
    uint32_t *crtcs;

    // 系统上可用的物理onnectors,connectors连接到encoders,并包含有关连接的显示接收器的信息(例如,宽度和高度(以毫米为单位)、像素以及各种其他属性)。
    int count_connectors;
    uint32_t *connectors;

    // 设备上的可用编码器,每个encoder可以与CRTC相关联,并且可用于驱动特定的encoder
    int count_encoders;
    uint32_t *encoders;

    // 设备上framebuffer的最大最小高度和宽度
    uint32_t min_width, max_width;
    uint32_t min_height, max_height;
} drmModeRes, *drmModeResPtr;

drmModeConnectorPtr drmModeGetConnector(int fd, uint32_t connectorId)

作用: 获取connector所有信息,通过在connector probe来获取远端的显示设备信息,如EDID.

drmModeConnectorPtr 结构体如下:

typedef struct _drmModeConnector {
	uint32_t connector_id;
	uint32_t encoder_id; /**< Encoder currently connected to */
	uint32_t connector_type;
	uint32_t connector_type_id;
	drmModeConnection connection;
	uint32_t mmWidth, mmHeight; /**< HxW in millimeters */
	drmModeSubPixel subpixel;

	int count_modes;
	drmModeModeInfoPtr modes;

	int count_props;
	uint32_t *props; /**< List of property ids */
	uint64_t *prop_values; /**< List of property values */

	int count_encoders;
	uint32_t *encoders; /**< List of encoder ids */
} drmModeConnector, *drmModeConnectorPtr;


extern int drmModeAddFB(int fd, uint32_t width, uint32_t height, uint8_t depth,
            uint8_t bpp, uint32_t pitch, uint32_t bo_handle, uint32_t *buf_id);

作用:创建一个新的framebuffer作为扫描输出的buffer,传入的buf_id参数是后续操作的FB的句柄。

int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId, uint32_t x, uint32_t y,

        uint32_t *connectors, int count, drmModeModeInfoPtr mode)

作用:当具备了合适CRTC的connector和具体的mode,并且有一个合适size的framebuffer时,只需要执行drmModeSetCrtc函数把每一个framebuffer连接到对应的connector,最后显示到屏幕上。

环境搭建

演示环境:Ubuntu22.04、libdrm-2.4.110

借用何小龙代码:modeset-single-buffer.c

#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <time.h>
#include <unistd.h>
#include <xf86drm.h>
#include <xf86drmMode.h>

struct buffer_object {
	uint32_t width;
	uint32_t height;
	uint32_t pitch;
	uint32_t handle;
	uint32_t size;
	uint8_t *vaddr;
	uint32_t fb_id;
};

struct buffer_object buf;

static int modeset_create_fb(int fd, struct buffer_object *bo)
{
	struct drm_mode_create_dumb create = {};
 	struct drm_mode_map_dumb map = {};

	/* create a dumb-buffer, the pixel format is XRGB888 */
	create.width = bo->width;
	create.height = bo->height;
	create.bpp = 32;
	drmIoctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &create);

	/* bind the dumb-buffer to an FB object */
	bo->pitch = create.pitch;
	bo->size = create.size;
	bo->handle = create.handle;
	drmModeAddFB(fd, bo->width, bo->height, 24, 32, bo->pitch,
			   bo->handle, &bo->fb_id);

	/* map the dumb-buffer to userspace */
	map.handle = create.handle;
	drmIoctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &map);

	bo->vaddr = mmap(0, create.size, PROT_READ | PROT_WRITE,
			MAP_SHARED, fd, map.offset);

	/* initialize the dumb-buffer with white-color */
	memset(bo->vaddr, 0xff, bo->size);

	return 0;
}

static void modeset_destroy_fb(int fd, struct buffer_object *bo)
{
	struct drm_mode_destroy_dumb destroy = {};

	drmModeRmFB(fd, bo->fb_id);

	munmap(bo->vaddr, bo->size);

	destroy.handle = bo->handle;
	drmIoctl(fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy);
}

int main(int argc, char **argv)
{
	int fd;
	drmModeConnector *conn;
	drmModeRes *res;
	uint32_t conn_id;
	uint32_t crtc_id;

	fd = open("/dev/dri/card0", O_RDWR | O_CLOEXEC);

	res = drmModeGetResources(fd);
	crtc_id = res->crtcs[0];
	conn_id = res->connectors[0];

	conn = drmModeGetConnector(fd, conn_id);
	buf.width = conn->modes[0].hdisplay;
	buf.height = conn->modes[0].vdisplay;

	modeset_create_fb(fd, &buf);

	drmModeSetCrtc(fd, crtc_id, buf.fb_id,
			0, 0, &conn_id, 1, &conn->modes[0]);

	getchar();

	modeset_destroy_fb(fd, &buf);

	drmModeFreeConnector(conn);
	drmModeFreeResources(res);

	close(fd);

	return 0;
}

步骤:

  •  修改编译文件,有2个地方需要修改:
  1. libdrm-xxx/tests/meson.build文件中添加:subdir('modeset-single-buffer')
  2. libdrm-xxx/tests/modeset-single-buffer文件夹下新建meson.build,内容如下:
mode = executable(
  'modeset-single-buffer',
  files('modeset-single-buffer.c'),
  c_args : [libdrm_c_args, '-Wno-pointer-arith'],
  include_directories : [inc_root, inc_tests, inc_drm],
  dependencies : [dep_threads, dep_cairo],
  link_with : [libdrm, libutil],
  install : with_install_tests,
)
mkdir build &&
cd    build &&

meson --buildtype=release   \
      -Dudev=true           \
      -Dvalgrind=false      &&
ninja
  •  生成路径:libdrm-xxx/build/tests/modeset-single-buffer

  •  安装libdrm到系统,不然运行的时候出不来效果,需要通过API调用到驱动层。这里需要切换到root用户
//1、切换到root用户
sudo -i

//2、安装
ninja install
  •  运行

需要切换到命令行模式:ubuntu22.04的快捷键是:CRTL+ALT+F6,切回桌面:CRTL+ALT+F1.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜卓

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值