DRM 的Property IN_FENCE_FD和OUT_FENCE_PTR使用示例
时间: 2023-08-03 19:09:05 浏览: 131
Property IN_FENCE_FD 和 OUT_FENCE_PTR 都是在 DRM 中用于同步的属性,它们可以用于确保输入和输出的同步信号已经准备就绪。
下面是一个示例代码,展示了如何使用 IN_FENCE_FD 和 OUT_FENCE_PTR 属性:
```c
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <drm/drm.h>
#include <drm/drm_mode.h>
int main() {
int fd;
struct drm_mode_create_dumb create_dumb;
struct drm_mode_map_dumb map_dumb;
struct drm_mode_fb_cmd cmd;
struct drm_gem_close gem_close;
struct drm_mode_atomic atomic;
struct drm_event_fence event_fence;
// 打开 DRM 设备
fd = open("/dev/dri/card0", O_RDWR);
// 创建 input dumb buffer
create_dumb.width = 800;
create_dumb.height = 600;
create_dumb.bpp = 32;
ioctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);
// 映射 input dumb buffer
map_dumb.handle = create_dumb.handle;
ioctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb);
// 配置 input framebuffer
cmd.fb_id = 0;
cmd.width = create_dumb.width;
cmd.height = create_dumb.height;
cmd.pitch = create_dumb.pitch;
cmd.bpp = create_dumb.bpp;
cmd.depth = 24;
cmd.handle = create_dumb.handle;
ioctl(fd, DRM_IOCTL_MODE_ADDFB, &cmd);
// 准备输入同步信号
int in_fence_fd = fence_create(fd, 0);
// 创建 output dumb buffer
create_dumb.width = 800;
create_dumb.height = 600;
create_dumb.bpp = 32;
ioctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);
// 映射 output dumb buffer
map_dumb.handle = create_dumb.handle;
ioctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb);
// 配置 output framebuffer
cmd.fb_id = 0;
cmd.width = create_dumb.width;
cmd.height = create_dumb.height;
cmd.pitch = create_dumb.pitch;
cmd.bpp = create_dumb.bpp;
cmd.depth = 24;
cmd.handle = create_dumb.handle;
ioctl(fd, DRM_IOCTL_MODE_ADDFB, &cmd);
// 准备输出同步信号
struct drm_mode_syncobj_create out_syncobj;
out_syncobj.handle = 0;
ioctl(fd, DRM_IOCTL_MODE_CREATE_SYNCOBJ, &out_syncobj);
// 提交 buffer,并设置 IN_FENCE_FD 和 OUT_FENCE_PTR
atomic.fb_ids_ptr = (uint64_t) &cmd.fb_id;
atomic.crtc_id = 0;
atomic.connector_id = 0;
atomic.count_props = 1;
atomic.user_data = 0;
atomic.flags = DRM_MODE_ATOMIC_NONBLOCK | DRM_MODE_PAGE_FLIP_EVENT | DRM_MODE_ATOMIC_ALLOW_MODESET;
atomic.objs_ptr = (uint64_t) &cmd.fb_id;
atomic.objs = 1;
atomic.fence_fd = in_fence_fd; // 设置 IN_FENCE_FD
atomic.out_fence_ptr = (uint64_t) &out_syncobj.handle; // 设置 OUT_FENCE_PTR
ioctl(fd, DRM_IOCTL_MODE_ATOMIC, &atomic);
// 等待输入同步信号
read(in_fence_fd, &event_fence, sizeof(event_fence));
// 等待输出同步信号
struct drm_mode_syncobj_wait wait;
wait.handles = (uint64_t) &out_syncobj.handle;
wait.count_handles = 1;
wait.timeout_nsec = 0;
ioctl(fd, DRM_IOCTL_MODE_SYNCOBJ_WAIT, &wait);
// 关闭输入 fence 文件描述符
close(in_fence_fd);
// 关闭输出同步对象
struct drm_mode_syncobj_destroy out_syncobj_destroy;
out_syncobj_destroy.handle = out_syncobj.handle;
ioctl(fd, DRM_IOCTL_MODE_DESTROY_SYNCOBJ, &out_syncobj_destroy);
// 关闭 output framebuffer
ioctl(fd, DRM_IOCTL_MODE_RMFB, &cmd.fb_id);
// 关闭 output dumb buffer
gem_close.handle = create_dumb.handle;
ioctl(fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
// 关闭 input framebuffer
ioctl(fd, DRM_IOCTL_MODE_RMFB, &cmd.fb_id);
// 关闭 input dumb buffer
gem_close.handle = create_dumb.handle;
ioctl(fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
// 关闭 DRM 设备
close(fd);
return 0;
}
```
该示例代码演示了如何使用 IN_FENCE_FD 和 OUT_FENCE_PTR 属性来实现输入和输出的同步。在提交 framebuffer 时,设置 IN_FENCE_FD 属性以确保输入同步信号已经准备就绪,并设置 OUT_FENCE_PTR 属性以获取输出同步信号。请注意,这只是一个简单的示例,实际使用时可能需要根据具体需求进行修改和扩展。
阅读全文