介绍
本文是根据学习何小龙 最简单的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;
}
步骤:
- 先搭建编译libdrm的环境,该编译是在modetest编译环境的基础上修改的。modetest编译环境具体参考drm 学习笔记-1:ubuntu编译运行modetest
- 把modeset-single-buffer的文件夹下载后放入到libdrm-xxx/tests/,目录结构如下图:
- 修改编译文件,有2个地方需要修改:
- libdrm-xxx/tests/meson.build文件中添加:subdir('modeset-single-buffer')
- 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,
)
- 编译libdrm,具体可参考:
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.