brintos

brintos / linux-shallow public Read only

0
0
Text · 5.9 KiB · 50fba11 Raw
163 lines · plain
1.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later2.. c:namespace:: V4L3 4.. _dmabuf:5 6************************************7Streaming I/O (DMA buffer importing)8************************************9 10The DMABUF framework provides a generic method for sharing buffers11between multiple devices. Device drivers that support DMABUF can export12a DMA buffer to userspace as a file descriptor (known as the exporter13role), import a DMA buffer from userspace using a file descriptor14previously exported for a different or the same device (known as the15importer role), or both. This section describes the DMABUF importer role16API in V4L2.17 18Refer to :ref:`DMABUF exporting <VIDIOC_EXPBUF>` for details about19exporting V4L2 buffers as DMABUF file descriptors.20 21Input and output devices support the streaming I/O method when the22``V4L2_CAP_STREAMING`` flag in the ``capabilities`` field of struct23:c:type:`v4l2_capability` returned by the24:ref:`VIDIOC_QUERYCAP <VIDIOC_QUERYCAP>` ioctl is set. Whether25importing DMA buffers through DMABUF file descriptors is supported is26determined by calling the :ref:`VIDIOC_REQBUFS <VIDIOC_REQBUFS>`27ioctl with the memory type set to ``V4L2_MEMORY_DMABUF``.28 29This I/O method is dedicated to sharing DMA buffers between different30devices, which may be V4L devices or other video-related devices (e.g.31DRM). Buffers (planes) are allocated by a driver on behalf of an32application. Next, these buffers are exported to the application as file33descriptors using an API which is specific for an allocator driver. Only34such file descriptor are exchanged. The descriptors and meta-information35are passed in struct :c:type:`v4l2_buffer` (or in struct36:c:type:`v4l2_plane` in the multi-planar API case). The37driver must be switched into DMABUF I/O mode by calling the38:ref:`VIDIOC_REQBUFS <VIDIOC_REQBUFS>` with the desired buffer type.39 40Example: Initiating streaming I/O with DMABUF file descriptors41==============================================================42 43.. code-block:: c44 45    struct v4l2_requestbuffers reqbuf;46 47    memset(&reqbuf, 0, sizeof (reqbuf));48    reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;49    reqbuf.memory = V4L2_MEMORY_DMABUF;50    reqbuf.count = 1;51 52    if (ioctl(fd, VIDIOC_REQBUFS, &reqbuf) == -1) {53	if (errno == EINVAL)54	    printf("Video capturing or DMABUF streaming is not supported\\n");55	else56	    perror("VIDIOC_REQBUFS");57 58	exit(EXIT_FAILURE);59    }60 61The buffer (plane) file descriptor is passed on the fly with the62:ref:`VIDIOC_QBUF <VIDIOC_QBUF>` ioctl. In case of multiplanar63buffers, every plane can be associated with a different DMABUF64descriptor. Although buffers are commonly cycled, applications can pass65a different DMABUF descriptor at each :ref:`VIDIOC_QBUF <VIDIOC_QBUF>` call.66 67Example: Queueing DMABUF using single plane API68===============================================69 70.. code-block:: c71 72    int buffer_queue(int v4lfd, int index, int dmafd)73    {74	struct v4l2_buffer buf;75 76	memset(&buf, 0, sizeof buf);77	buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;78	buf.memory = V4L2_MEMORY_DMABUF;79	buf.index = index;80	buf.m.fd = dmafd;81 82	if (ioctl(v4lfd, VIDIOC_QBUF, &buf) == -1) {83	    perror("VIDIOC_QBUF");84	    return -1;85	}86 87	return 0;88    }89 90Example 3.6. Queueing DMABUF using multi plane API91==================================================92 93.. code-block:: c94 95    int buffer_queue_mp(int v4lfd, int index, int dmafd[], int n_planes)96    {97	struct v4l2_buffer buf;98	struct v4l2_plane planes[VIDEO_MAX_PLANES];99	int i;100 101	memset(&buf, 0, sizeof buf);102	buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;103	buf.memory = V4L2_MEMORY_DMABUF;104	buf.index = index;105	buf.m.planes = planes;106	buf.length = n_planes;107 108	memset(&planes, 0, sizeof planes);109 110	for (i = 0; i < n_planes; ++i)111	    buf.m.planes[i].m.fd = dmafd[i];112 113	if (ioctl(v4lfd, VIDIOC_QBUF, &buf) == -1) {114	    perror("VIDIOC_QBUF");115	    return -1;116	}117 118	return 0;119    }120 121Captured or displayed buffers are dequeued with the122:ref:`VIDIOC_DQBUF <VIDIOC_QBUF>` ioctl. The driver can unlock the123buffer at any time between the completion of the DMA and this ioctl. The124memory is also unlocked when125:ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>` is called,126:ref:`VIDIOC_REQBUFS <VIDIOC_REQBUFS>`, or when the device is closed.127 128For capturing applications it is customary to enqueue a number of empty129buffers, to start capturing and enter the read loop. Here the130application waits until a filled buffer can be dequeued, and re-enqueues131the buffer when the data is no longer needed. Output applications fill132and enqueue buffers, when enough buffers are stacked up output is133started. In the write loop, when the application runs out of free134buffers it must wait until an empty buffer can be dequeued and reused.135Two methods exist to suspend execution of the application until one or136more buffers can be dequeued. By default :ref:`VIDIOC_DQBUF137<VIDIOC_QBUF>` blocks when no buffer is in the outgoing queue. When the138``O_NONBLOCK`` flag was given to the :c:func:`open()` function,139:ref:`VIDIOC_DQBUF <VIDIOC_QBUF>` returns immediately with an ``EAGAIN``140error code when no buffer is available. The141:c:func:`select()` and :c:func:`poll()`142functions are always available.143 144To start and stop capturing or displaying applications call the145:ref:`VIDIOC_STREAMON <VIDIOC_STREAMON>` and146:ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>` ioctls.147 148.. note::149 150   :ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>` removes all buffers from151   both queues and unlocks all buffers as a side effect. Since there is no152   notion of doing anything "now" on a multitasking system, if an153   application needs to synchronize with another event it should examine154   the struct :c:type:`v4l2_buffer` ``timestamp`` of captured or155   outputted buffers.156 157Drivers implementing DMABUF importing I/O must support the158:ref:`VIDIOC_REQBUFS <VIDIOC_REQBUFS>`, :ref:`VIDIOC_QBUF <VIDIOC_QBUF>`,159:ref:`VIDIOC_DQBUF <VIDIOC_QBUF>`, :ref:`VIDIOC_STREAMON160<VIDIOC_STREAMON>` and :ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>` ioctls,161and the :c:func:`select()` and :c:func:`poll()`162functions.163