From 13724144f9bfd22f79d56431061bc55a887c1d10 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Niklas=20S=C3=B6derlund?= <niklas.soderlund@ragnatech.se>
Date: Thu, 21 Nov 2019 16:06:27 +0100
Subject: libcamera: v4l2_videodevice: Extract exportDmabufFd()
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The part in createPlane() that exports a dma buffer from a video device
will be used directly by the FrameBuffer interface. Break it out to a
separate function.

Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 src/libcamera/include/v4l2_videodevice.h |  2 ++
 src/libcamera/v4l2_videodevice.cpp       | 42 ++++++++++++++++++++------------
 2 files changed, 28 insertions(+), 16 deletions(-)

(limited to 'src')

diff --git a/src/libcamera/include/v4l2_videodevice.h b/src/libcamera/include/v4l2_videodevice.h
index fdf11b3a..27ec77cd 100644
--- a/src/libcamera/include/v4l2_videodevice.h
+++ b/src/libcamera/include/v4l2_videodevice.h
@@ -26,6 +26,7 @@ class Buffer;
 class BufferMemory;
 class BufferPool;
 class EventNotifier;
+class FileDescriptor;
 class MediaDevice;
 class MediaEntity;
 
@@ -179,6 +180,7 @@ private:
 	int requestBuffers(unsigned int count);
 	int createPlane(BufferMemory *buffer, unsigned int index,
 			unsigned int plane, unsigned int length);
+	FileDescriptor exportDmabufFd(unsigned int index, unsigned int plane);
 
 	Buffer *dequeueBuffer();
 	void bufferAvailable(EventNotifier *notifier);
diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp
index 81d999e3..4551484c 100644
--- a/src/libcamera/v4l2_videodevice.cpp
+++ b/src/libcamera/v4l2_videodevice.cpp
@@ -22,6 +22,7 @@
 
 #include <libcamera/buffer.h>
 #include <libcamera/event_notifier.h>
+#include <libcamera/file_descriptor.h>
 
 #include "log.h"
 #include "media_device.h"
@@ -902,31 +903,19 @@ int V4L2VideoDevice::exportBuffers(BufferPool *pool)
 int V4L2VideoDevice::createPlane(BufferMemory *buffer, unsigned int index,
 				 unsigned int planeIndex, unsigned int length)
 {
-	struct v4l2_exportbuffer expbuf = {};
-	int ret;
-
 	LOG(V4L2, Debug)
 		<< "Buffer " << index
 		<< " plane " << planeIndex
 		<< ": length=" << length;
 
-	expbuf.type = bufferType_;
-	expbuf.index = index;
-	expbuf.plane = planeIndex;
-	expbuf.flags = O_RDWR;
-
-	ret = ioctl(VIDIOC_EXPBUF, &expbuf);
-	if (ret < 0) {
-		LOG(V4L2, Error)
-			<< "Failed to export buffer: " << strerror(-ret);
-		return ret;
-	}
+	FileDescriptor fd = exportDmabufFd(index, planeIndex);
+	if (!fd.isValid())
+		return -EINVAL;
 
 	FrameBuffer::Plane plane;
-	plane.fd = FileDescriptor(expbuf.fd);
+	plane.fd = fd;
 	plane.length = length;
 	buffer->planes().push_back(plane);
-	::close(expbuf.fd);
 
 	return 0;
 }
@@ -952,6 +941,27 @@ int V4L2VideoDevice::importBuffers(BufferPool *pool)
 	return 0;
 }
 
+FileDescriptor V4L2VideoDevice::exportDmabufFd(unsigned int index,
+					       unsigned int plane)
+{
+	struct v4l2_exportbuffer expbuf = {};
+	int ret;
+
+	expbuf.type = bufferType_;
+	expbuf.index = index;
+	expbuf.plane = plane;
+	expbuf.flags = O_RDWR;
+
+	ret = ioctl(VIDIOC_EXPBUF, &expbuf);
+	if (ret < 0) {
+		LOG(V4L2, Error)
+			<< "Failed to export buffer: " << strerror(-ret);
+		return FileDescriptor();
+	}
+
+	return FileDescriptor(expbuf.fd);
+}
+
 /**
  * \brief Release all internally allocated buffers
  */
-- 
cgit v1.2.1