From patchwork Sun Feb 3 11:00:55 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 491 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id AEF0260B1B for ; Sun, 3 Feb 2019 12:01:07 +0100 (CET) Received: from localhost.localdomain (218.182-78-194.adsl-static.isp.belgacom.be [194.78.182.218]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 5B4BFD4B; Sun, 3 Feb 2019 12:01:07 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1549191667; bh=ApbnUKVZtAMnEj3TtinpMY7Bpb+TnV7zi2Y0DX8u63s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=D39BaXHSADMjrhNtQt/oaCJAsLYGcVq9GQ4Fgty9zCUnBoHonPlrn8bQB9ELWjOk9 A30v9jq4RugIEA36XvRiKELVlykuWcp5jzYSuGJaaYgkLPIml9aiLtkw/Nvo1E11Xy JbcVYtGaKvCK44EOA5ISDRF465VBfav0XtTpVdNE= From: Kieran Bingham To: LibCamera Devel Date: Sun, 3 Feb 2019 12:00:55 +0100 Message-Id: <20190203110102.5663-5-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190203110102.5663-1-kieran.bingham@ideasonboard.com> References: <20190203110102.5663-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 04/11] libcamera: v4l2_device: Support exporting buffers X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 03 Feb 2019 11:01:07 -0000 Implement the VIDIOC_EXPBUF functionality allowing our buffers to be exported to other V4L2Device objects. Export buffers when they are allocated during a call to requestBuffers() Signed-off-by: Kieran Bingham --- src/libcamera/include/v4l2_device.h | 2 + src/libcamera/v4l2_device.cpp | 59 +++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/src/libcamera/include/v4l2_device.h b/src/libcamera/include/v4l2_device.h index 6599ce2d761c..f445f98f97a4 100644 --- a/src/libcamera/include/v4l2_device.h +++ b/src/libcamera/include/v4l2_device.h @@ -109,6 +109,8 @@ private: int setFormatMultiplane(V4L2DeviceFormat *fmt); int requestBuffers(unsigned int qty, std::vector &buffers); + int exportBuffer(Buffer *buffer); + int exportBuffers(BufferPool *pool); std::string deviceNode_; int fd_; diff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp index 8bcd8bbc34f6..728478a1ae8f 100644 --- a/src/libcamera/v4l2_device.cpp +++ b/src/libcamera/v4l2_device.cpp @@ -340,6 +340,13 @@ BufferPool *V4L2Device::requestBuffers(unsigned int qty) return nullptr; } + ret = exportBuffers(bufferPool_); + if (ret) { + delete bufferPool_; + bufferPool_ = nullptr; + return nullptr; + } + return bufferPool_; } @@ -414,6 +421,58 @@ int V4L2Device::requestBuffers(unsigned int qty, std::vector &buffers) return 0; } +int V4L2Device::exportBuffer(Buffer *buffer) +{ + unsigned int i = 0; + for (Plane *plane : buffer->planes()) { + struct v4l2_exportbuffer expbuf = {}; + int ret; + + expbuf.type = bufferType_; + expbuf.index = buffer->index(); + expbuf.plane = i++; + + ret = ioctl(fd_, VIDIOC_EXPBUF, &expbuf); + if (ret < 0) { + ret = -errno; + LOG(V4L2, Error) + << "Failed to export buffer: " << strerror(-ret); + return ret; + } + + plane->setDmabuf(expbuf.fd); + } + + return 0; +} + +int V4L2Device::exportBuffers(BufferPool *pool) +{ + for (Buffer *buffer : pool->buffers()) { + int ret = exportBuffer(buffer); + if (ret) + return ret; + + /** + * \todo: Remove this mmap + * It is a temporary work around to have all internal buffers + * mapped for early development. + * + * Applications will be expected to map buffers if necessary. + */ + ret = buffer->mmap(); + if (ret) { + ret = -errno; + LOG(V4L2, Error) + << "Failed to mmap a buffer instance: " + << strerror(-ret); + return ret; + } + } + + return 0; +} + /** * \brief Retrieve the image format set on the V4L2 device * \return 0 for success, a negative error code otherwise