Skip to content

DRM_README

SweerItTer edited this page Feb 1, 2026 · 3 revisions

DRM 模块 API 文档

概述

DRM 模块提供 Linux DRM(Direct Rendering Manager)设备的封装,支持显示管理和硬件加速图层合成。

模块组成

核心类

文档 说明
DeviceController DeviceController DRM 设备控制器
DrmLayer DrmLayer DRM 图层(未完成)
PlanesCompositor PlanesCompositor 图层组合器

工具类

文档 说明
DrmBpp DrmBpp DRM 格式和BPP(未完成)

功能特性

设备管理

  • DRM 设备打开和初始化
  • CRTC 和 Connector 查询
  • Plane 查询和管理
  • 热插拔监控

图层管理

  • 添加和移除图层
  • 更新图层属性
  • 图层组合
  • 原子提交

显示管理

  • 帧缓冲区管理
  • Fence 同步
  • 模式设置
  • 显示控制

使用示例

打开设备

// 打开 DRM 设备
auto controller = DeviceController::create("/dev/dri/card0");

if (!controller) {
    fprintf(stderr, "Failed to open DRM device\n");
}

// 注册资源回调
controller->registerResourceCallback([](DeviceEvent event, uint32_t id) {
    if (event == DeviceEvent::CONNECTOR_CONNECTED) {
        printf("Connector %d connected\n", id);
    } else if (event == DeviceEvent::CONNECTOR_DISCONNECTED) {
        printf("Connector %d disconnected\n", id);
    }
});

查询 CRTC

// 查询 CRTC
auto crtcs = controller->getPossibleCrtcs();

for (auto crtc : crtcs) {
    printf("CRTC ID: %d\n", crtc.id);
    printf("  Possible connectors: %zu\n", crtc.possible_connectors.size());
    printf("  Possible planes: %zu\n", crtc.possible_planes.size());
}

查询 Plane

// 查询 Plane
auto planes = controller->getPossiblePlanes();

for (auto plane : planes) {
    printf("Plane ID: %d\n", plane.id);
    printf("  Type: %d\n", plane.type);
    printf("  Formats: %zu\n", plane.formats.size());
}

创建帧缓冲区

// 创建帧缓冲区
uint32_t fb_id = controller->createFramebuffer(
    dma_fd,
    width,
    height,
    format,
    0,  // handle
    0   // pitch
);

if (fb_id == 0) {
    fprintf(stderr, "Failed to create framebuffer\n");
}

使用 PlanesCompositor

// 创建组合器
PlanesCompositor compositor(controller);

// 创建图层
auto layer = std::make_unique<DrmLayer>(plane_id);
compositor.addLayer(std::move(layer));

// 更新图层属性
LayerProperties props;
props.crtc_id = crtc_id;
props.fb_id = fb_id;
props.crtc_x = 0;
props.crtc_y = 0;
props.crtc_w = 1920;
props.crtc_h = 1080;
compositor.updateLayer(plane_id, props);

// 提交
compositor.commit();

Fence 同步

// 获取输入 Fence
int in_fence = rga_get_out_fence();

// 设置输入 Fence
compositor.setInFence(in_fence);

// 设置输出 Fence
int out_fence = -1;
compositor.setOutFence(&out_fence);

// 提交
compositor.commit();

// 等待输出 Fence
wait_for_fence(out_fence);

// 关闭 Fence
close(in_fence);
close(out_fence);

数据流

显示流程

DMA-BUF → Plane → CRTC → Connector → 显示器

原子提交流程

添加/更新图层 → 创建原子请求 → 添加属性 → 执行提交 → 等待完成

线程安全

  • DeviceController 内部使用互斥锁保护资源访问
  • PlanesCompositor 使用 DRM 原子提交机制
  • Fence 使用 Linux Fence 机制

性能优化

  • 使用原子提交减少闪烁
  • 使用 Fence 同步硬件操作
  • 使用 DMA-BUF 实现零拷贝
  • 合理使用图层组合

错误处理

  • 检查所有函数的返回值
  • 处理设备不支持的情况
  • 处理热插拔事件

相关模块

参考资料

注意事项

  1. 设备权限: 确保有访问 DRM 设备的权限
  2. 原子提交: 使用原子提交避免闪烁
  3. Fence 管理: 使用完 Fence 后要关闭
  4. 热插拔: 处理热插拔事件
  5. 格式支持: 确保硬件支持对应的格式
  6. 图层限制: 注意硬件的图层数量限制
  7. 性能: 合理使用图层组合
  8. 同步: 使用 Fence 同步硬件操作

版本历史

  • v1.0 - 初始版本,支持基本显示管理

主页

API 文档

DMA 模块

DRM 模块

NET 模块

V4L2 模块

V4L2Param 模块

RGA 模块

MPP 模块

Sys 模块

Mouse 模块

Utils 模块

Clone this wiki locally