DMA buffer importing


3.4. Streaming I/O (DMA buffer importing)

The DMABUF framework provides a generic method for sharing buffers between multiple devices. Device drivers that support DMABUF can export a DMA buffer to userspace as a file descriptor (known as the exporter role), import a DMA buffer from userspace using a file descriptor previously exported for a different or the same device (known as the importer role), or both. This section describes the DMABUF importer role API in V4L2.

Refer to DMABUF exporting for details about exporting V4L2 buffers as DMABUF file descriptors.

Input and output devices support the streaming I/O method when the V4L2_CAP_STREAMING flag in the capabilities field of struct v4l2_capability returned by the VIDIOC_QUERYCAP ioctl is set. Whether importing DMA buffers through DMABUF file descriptors is supported is determined by calling the VIDIOC_REQBUFS ioctl with the memory type set to V4L2_MEMORY_DMABUF.

This I/O method is dedicated to sharing DMA buffers between different devices, which may be V4L devices or other video-related devices (e.g. DRM). Buffers (planes) are allocated by a driver on behalf of an application. Next, these buffers are exported to the application as file descriptors using an API which is specific for an allocator driver. Only such file descriptor are exchanged. The descriptors and meta-information are passed in struct v4l2_buffer (or in struct v4l2_plane in the multi-planar API case). The driver must be switched into DMABUF I/O mode by calling the VIDIOC_REQBUFS with the desired buffer type.

3.4.1. Example: Initiating streaming I/O with DMABUF file descriptors

struct v4l2_requestbuffers reqbuf;

memset(&reqbuf, 0, sizeof (reqbuf));
reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
reqbuf.memory = V4L2_MEMORY_DMABUF;
reqbuf.count = 1;

if (ioctl(fd, VIDIOC_REQBUFS, &reqbuf) == -1) {
    if (errno == EINVAL)
        printf("Video capturing or DMABUF streaming is not supported\\n");
    else
        perror("VIDIOC_REQBUFS");

    exit(EXIT_FAILURE);
}

The buffer (plane) file descriptor is passed on the fly with the VIDIOC_QBUF ioctl. In case of multiplanar buffers, every plane can be associated with a different DMABUF descriptor. Although buffers are commonly cycled, applications can pass a different DMABUF descriptor at each VIDIOC_QBUF call.

3.4.2. Example: Queueing DMABUF using single plane API

int buffer_queue(int v4lfd, int index, int dmafd)
{
    struct v4l2_buffer buf;

    memset(&buf, 0, sizeof buf);
    buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    buf.memory = V4L2_MEMORY_DMABUF;
    buf.index = index;
    buf.m.fd = dmafd;

    if (ioctl(v4lfd, VIDIOC_QBUF, &buf) == -1) {
        perror("VIDIOC_QBUF");
        return -1;
    }

    return 0;
}

3.4.3. Example 3.6. Queueing DMABUF using multi plane API

int buffer_queue_mp(int v4lfd, int index, int dmafd[], int n_planes)
{
    struct v4l2_buffer buf;
    struct v4l2_plane planes[VIDEO_MAX_PLANES];
    int i;

    memset(&buf, 0, sizeof buf);
    buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
    buf.memory = V4L2_MEMORY_DMABUF;
    buf.index = index;
    buf.m.planes = planes;
    buf.length = n_planes;

    memset(&planes, 0, sizeof planes);

    for (i = 0; i < n_planes; ++i)
        buf.m.planes[i].m.fd = dmafd[i];

    if (ioctl(v4lfd, VIDIOC_QBUF, &buf) == -1) {
        perror("VIDIOC_QBUF");
        return -1;
    }

    return 0;
}

Captured or displayed buffers are dequeued with the VIDIOC_DQBUF ioctl. The driver can unlock the buffer at any time between the completion of the DMA and this ioctl. The memory is also unlocked when VIDIOC_STREAMOFF is called,VIDIOC_REQBUFS, or when the device is closed.

For capturing applications it is customary to enqueue a number of empty buffers, to start capturing and enter the read loop. Here the application waits until a filled buffer can be dequeued, and re-enqueues the buffer when the data is no longer needed. Output applications fill and enqueue buffers, when enough buffers are stacked up output is started. In the write loop, when the application runs out of free buffers it must wait until an empty buffer can be dequeued and reused. Two methods exist to suspend execution of the application until one or more buffers can be dequeued. By default VIDIOC_DQBUF blocks when no buffer is in the outgoing queue. When the O_NONBLOCK flag was given to the open() function,VIDIOC_DQBUF returns immediately with an EAGAIN error code when no buffer is available. The select() and poll() functions are always available.

To start and stop capturing or displaying applications call the VIDIOC_STREAMON and VIDIOC_STREAMOFF ioctls.

Note

VIDIOC_STREAMOFF removes all buffers from both queues and unlocks all buffers as a side effect. Since there is no notion of doing anything “now” on a multitasking system, if an application needs to synchronize with another event it should examine the struct v4l2_buffer timestamp of captured or outputted buffers.

Drivers implementing DMABUF importing I/O must support the VIDIOC_REQBUFSVIDIOC_QBUFVIDIOC_DQBUFVIDIOC_STREAMON and VIDIOC_STREAMOFF ioctls, and the select() and poll() functions.

### 配置 IntelliJ IDEA 中 Maven 解决导入问题 当遇到 `import` 导入报错以及更新 Maven 提示 "Unable to import maven project: See logs for details"[^1] 的情况时,可以按照以下方法来配置和解决问题。 #### 设置全局工具配置 确保已安装并正确设置了 JDK 和 Maven 版本。通过进入 **File | Settings (Ctrl+Alt+S)** 或者对于 macOS 用户来说是 **IntelliJ IDEA | Preferences** 来访问设置对话框,在左侧导航栏中选择 **Build, Execution, Deployment | Build Tools | Maven** 并确认选择了合适的 Maven 安装路径。 #### 更新索引与重新加载项目结构 有时由于缓存原因可能会导致无法正常解析依赖项或模块定义文件 pom.xml 。此时应该尝试刷新本地仓库索引: - 打开命令面板 (**View | Tool Windows | Maven Projects**); - 右键点击项目的根节点,选择 **Reimport All Maven Projects** 选项以强制重新读取 POM 文件及其子模块的信息;如果这一步骤未能成功,则继续执行下一步操作; - 尝试清理整个工作区内的构建产物再重试:右击任意空白处 -> Invalidate Caches / Restart... ,之后重启 IDE 让其重建内部数据库以便更好地识别源码中的变化。 #### 修改日志级别查看错误详情 为了更精确地定位具体是什么地方出了差错,可适当调整日志记录等级从而获取更多调试信息帮助分析问题所在之处。前往 **Help | Edit Custom VM Options** 添加如下参数 `-Didea.maven.embedder.log.level=DEBUG` 后保存更改并重启应用程序使新设定生效。 ```xml <properties> <!-- Other properties --> </properties> <!-- Add this snippet inside <build> tag of your root or module's pom.xml file --> <build> ... <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- More plugins here as needed --> </plugins> </pluginManagement> ... </build> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值