2020-08-26 07:03:09 +00:00
|
|
|
.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
|
2020-09-24 12:04:26 +00:00
|
|
|
.. c:namespace:: V4L
|
2018-08-30 14:15:26 +00:00
|
|
|
|
2016-06-30 13:18:56 +00:00
|
|
|
.. _dmabuf:
|
|
|
|
|
|
|
|
************************************
|
|
|
|
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.
|
|
|
|
|
2016-07-01 16:42:29 +00:00
|
|
|
Refer to :ref:`DMABUF exporting <VIDIOC_EXPBUF>` for details about
|
2016-06-30 13:18:56 +00:00
|
|
|
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
|
2016-08-29 20:37:59 +00:00
|
|
|
:c:type:`v4l2_capability` returned by the
|
2016-07-04 19:43:03 +00:00
|
|
|
:ref:`VIDIOC_QUERYCAP <VIDIOC_QUERYCAP>` ioctl is set. Whether
|
2016-06-30 13:18:56 +00:00
|
|
|
importing DMA buffers through DMABUF file descriptors is supported is
|
2016-07-04 19:43:03 +00:00
|
|
|
determined by calling the :ref:`VIDIOC_REQBUFS <VIDIOC_REQBUFS>`
|
2016-06-30 13:18:56 +00:00
|
|
|
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
|
2016-08-29 20:37:59 +00:00
|
|
|
are passed in struct :c:type:`v4l2_buffer` (or in struct
|
|
|
|
:c:type:`v4l2_plane` in the multi-planar API case). The
|
2016-06-30 13:18:56 +00:00
|
|
|
driver must be switched into DMABUF I/O mode by calling the
|
2016-07-04 19:43:03 +00:00
|
|
|
:ref:`VIDIOC_REQBUFS <VIDIOC_REQBUFS>` with the desired buffer type.
|
2016-06-30 13:18:56 +00:00
|
|
|
|
2016-07-10 11:22:19 +00:00
|
|
|
Example: Initiating streaming I/O with DMABUF file descriptors
|
|
|
|
==============================================================
|
|
|
|
|
2016-06-30 13:18:56 +00:00
|
|
|
.. code-block:: c
|
|
|
|
|
|
|
|
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) {
|
2016-07-04 19:25:48 +00:00
|
|
|
if (errno == EINVAL)
|
|
|
|
printf("Video capturing or DMABUF streaming is not supported\\n");
|
|
|
|
else
|
|
|
|
perror("VIDIOC_REQBUFS");
|
2016-06-30 13:18:56 +00:00
|
|
|
|
2016-07-04 19:25:48 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2016-06-30 13:18:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
The buffer (plane) file descriptor is passed on the fly with the
|
2016-07-04 19:43:03 +00:00
|
|
|
:ref:`VIDIOC_QBUF <VIDIOC_QBUF>` ioctl. In case of multiplanar
|
2016-06-30 13:18:56 +00:00
|
|
|
buffers, every plane can be associated with a different DMABUF
|
|
|
|
descriptor. Although buffers are commonly cycled, applications can pass
|
2016-07-04 19:43:03 +00:00
|
|
|
a different DMABUF descriptor at each :ref:`VIDIOC_QBUF <VIDIOC_QBUF>` call.
|
2016-06-30 13:18:56 +00:00
|
|
|
|
2016-07-10 11:22:19 +00:00
|
|
|
Example: Queueing DMABUF using single plane API
|
|
|
|
===============================================
|
2016-06-30 13:18:56 +00:00
|
|
|
|
|
|
|
.. code-block:: c
|
|
|
|
|
|
|
|
int buffer_queue(int v4lfd, int index, int dmafd)
|
|
|
|
{
|
2016-07-04 19:25:48 +00:00
|
|
|
struct v4l2_buffer buf;
|
2016-06-30 13:18:56 +00:00
|
|
|
|
2016-07-04 19:25:48 +00:00
|
|
|
memset(&buf, 0, sizeof buf);
|
|
|
|
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
buf.memory = V4L2_MEMORY_DMABUF;
|
|
|
|
buf.index = index;
|
|
|
|
buf.m.fd = dmafd;
|
2016-06-30 13:18:56 +00:00
|
|
|
|
2016-07-04 19:25:48 +00:00
|
|
|
if (ioctl(v4lfd, VIDIOC_QBUF, &buf) == -1) {
|
|
|
|
perror("VIDIOC_QBUF");
|
|
|
|
return -1;
|
|
|
|
}
|
2016-06-30 13:18:56 +00:00
|
|
|
|
2016-07-04 19:25:48 +00:00
|
|
|
return 0;
|
2016-06-30 13:18:56 +00:00
|
|
|
}
|
|
|
|
|
2016-07-10 11:22:19 +00:00
|
|
|
Example 3.6. Queueing DMABUF using multi plane API
|
|
|
|
==================================================
|
2016-06-30 13:18:56 +00:00
|
|
|
|
|
|
|
.. code-block:: c
|
|
|
|
|
|
|
|
int buffer_queue_mp(int v4lfd, int index, int dmafd[], int n_planes)
|
|
|
|
{
|
2016-07-04 19:25:48 +00:00
|
|
|
struct v4l2_buffer buf;
|
|
|
|
struct v4l2_plane planes[VIDEO_MAX_PLANES];
|
|
|
|
int i;
|
2016-06-30 13:18:56 +00:00
|
|
|
|
2016-07-04 19:25:48 +00:00
|
|
|
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;
|
2016-06-30 13:18:56 +00:00
|
|
|
|
2016-07-04 19:25:48 +00:00
|
|
|
memset(&planes, 0, sizeof planes);
|
2016-06-30 13:18:56 +00:00
|
|
|
|
2016-07-04 19:25:48 +00:00
|
|
|
for (i = 0; i < n_planes; ++i)
|
|
|
|
buf.m.planes[i].m.fd = dmafd[i];
|
2016-06-30 13:18:56 +00:00
|
|
|
|
2016-07-04 19:25:48 +00:00
|
|
|
if (ioctl(v4lfd, VIDIOC_QBUF, &buf) == -1) {
|
|
|
|
perror("VIDIOC_QBUF");
|
|
|
|
return -1;
|
|
|
|
}
|
2016-06-30 13:18:56 +00:00
|
|
|
|
2016-07-04 19:25:48 +00:00
|
|
|
return 0;
|
2016-06-30 13:18:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Captured or displayed buffers are dequeued with the
|
2016-07-01 16:42:29 +00:00
|
|
|
:ref:`VIDIOC_DQBUF <VIDIOC_QBUF>` ioctl. The driver can unlock the
|
2016-06-30 13:18:56 +00:00
|
|
|
buffer at any time between the completion of the DMA and this ioctl. The
|
|
|
|
memory is also unlocked when
|
2016-07-01 16:42:29 +00:00
|
|
|
:ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>` is called,
|
2016-07-04 19:43:03 +00:00
|
|
|
:ref:`VIDIOC_REQBUFS <VIDIOC_REQBUFS>`, or when the device is closed.
|
2016-06-30 13:18:56 +00:00
|
|
|
|
|
|
|
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
|
2016-07-04 19:43:03 +00:00
|
|
|
more buffers can be dequeued. By default :ref:`VIDIOC_DQBUF
|
|
|
|
<VIDIOC_QBUF>` blocks when no buffer is in the outgoing queue. When the
|
2020-09-24 12:04:26 +00:00
|
|
|
``O_NONBLOCK`` flag was given to the :c:func:`open()` function,
|
2016-07-04 19:43:03 +00:00
|
|
|
:ref:`VIDIOC_DQBUF <VIDIOC_QBUF>` returns immediately with an ``EAGAIN``
|
|
|
|
error code when no buffer is available. The
|
2020-09-24 12:04:26 +00:00
|
|
|
:c:func:`select()` and :c:func:`poll()`
|
2016-06-30 13:18:56 +00:00
|
|
|
functions are always available.
|
|
|
|
|
|
|
|
To start and stop capturing or displaying applications call the
|
2016-07-04 19:43:03 +00:00
|
|
|
:ref:`VIDIOC_STREAMON <VIDIOC_STREAMON>` and
|
2016-07-10 14:57:43 +00:00
|
|
|
:ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>` ioctls.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
:ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>` 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
|
2016-08-29 20:37:59 +00:00
|
|
|
the struct :c:type:`v4l2_buffer` ``timestamp`` of captured or
|
2016-07-10 14:57:43 +00:00
|
|
|
outputted buffers.
|
2016-06-30 13:18:56 +00:00
|
|
|
|
|
|
|
Drivers implementing DMABUF importing I/O must support the
|
2016-07-04 19:43:03 +00:00
|
|
|
:ref:`VIDIOC_REQBUFS <VIDIOC_REQBUFS>`, :ref:`VIDIOC_QBUF <VIDIOC_QBUF>`,
|
2016-07-05 18:27:52 +00:00
|
|
|
:ref:`VIDIOC_DQBUF <VIDIOC_QBUF>`, :ref:`VIDIOC_STREAMON
|
2016-07-04 19:43:03 +00:00
|
|
|
<VIDIOC_STREAMON>` and :ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>` ioctls,
|
2020-09-24 12:04:26 +00:00
|
|
|
and the :c:func:`select()` and :c:func:`poll()`
|
2016-07-04 19:43:03 +00:00
|
|
|
functions.
|