From patchwork Tue Aug 4 21:46:59 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 9194 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 5A246BD87A for ; Tue, 4 Aug 2020 21:47:19 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 18CAF60557; Tue, 4 Aug 2020 23:47:18 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="UG2j3dVk"; dkim-atps=neutral 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 B8D2360545 for ; Tue, 4 Aug 2020 23:47:16 +0200 (CEST) Received: from Q.local (cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 1B952543; Tue, 4 Aug 2020 23:47:16 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1596577636; bh=DxAIImIc1YcTwfXBvA42JnIiRQoaoMCbU5KR6C5iMW4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UG2j3dVkFlNPSw12NThpily4wDIrDxI20Z0IP3ip47zmg/uu33lG0kMvtBdXZZDY1 7AHRlTk8QdN/sdPyDdAGVosFF6CE7LyzhNZCvQE50rG8bt8LiEy4F9/3a45Ey1Cg83 uXCn7h2yuCpOBOCt4Ux07wUUNqspuaRMHvd3vKNc= From: Kieran Bingham To: libcamera devel Date: Tue, 4 Aug 2020 22:46:59 +0100 Message-Id: <20200804214711.177645-2-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200804214711.177645-1-kieran.bingham@ideasonboard.com> References: <20200804214711.177645-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 01/13] libcamera: buffer: Correct FrameBuffer references X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Update the remaining (capitalised) Buffer references to the new FrameBuffer object that they refer to. Lower-case 'buffer' terms are kept, as a generic reference to a buffer rather than the specific type 'FrameBuffer'. Reviewed-by: Niklas Söderlund Reviewed-by: Laurent Pinchart Signed-off-by: Kieran Bingham --- src/libcamera/buffer.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libcamera/buffer.cpp b/src/libcamera/buffer.cpp index 1a1d4bac7aed..8278f8a92af4 100644 --- a/src/libcamera/buffer.cpp +++ b/src/libcamera/buffer.cpp @@ -174,11 +174,11 @@ FrameBuffer::FrameBuffer(const std::vector &planes, unsigned int cookie) * The intended callers of this method are buffer completion handlers that * need to associate a buffer to the request it belongs to. * - * A Buffer is associated to a request by Request::addBuffer() and the + * A FrameBuffer is associated to a request by Request::addBuffer() and the * association is valid until the buffer completes. The returned request * pointer is valid only during that interval. * - * \return The Request the Buffer belongs to, or nullptr if the buffer is + * \return The Request the FrameBuffer belongs to, or nullptr if the buffer is * not associated with a request */ @@ -224,7 +224,7 @@ FrameBuffer::FrameBuffer(const std::vector &planes, unsigned int cookie) /** * \brief Copy the contents from another buffer - * \param[in] src Buffer to copy + * \param[in] src FrameBuffer to copy * * Copy the buffer contents and metadata from \a src to this buffer. The * destination FrameBuffer shall have the same number of planes as the source From patchwork Tue Aug 4 21:47:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 9195 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 22113BD87A for ; Tue, 4 Aug 2020 21:47:20 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 66ACB60567; Tue, 4 Aug 2020 23:47:19 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="fOrQ3usn"; dkim-atps=neutral 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 4940C60548 for ; Tue, 4 Aug 2020 23:47:17 +0200 (CEST) Received: from Q.local (cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 8867056E; Tue, 4 Aug 2020 23:47:16 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1596577636; bh=xOBkeVTzlN+Qtprad+1Xq3vpczkJ7mQZ5vHIrUpNAHg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fOrQ3usnJ4GNSbdix7AX+NUd19V4uBSly/8s/glnJ1tEJZjdchMOLc5u+JvaRKIR6 5nmhuzQUdhOxVN7jTIsO+9k1KRpbmnRlaD19BqsNU+Ip5OmEi3yoUH33AOS14BsEjZ CSz3ZBoNt5KwND+NJwFwwocWneyJRiPnDxs61l10= From: Kieran Bingham To: libcamera devel Date: Tue, 4 Aug 2020 22:47:00 +0100 Message-Id: <20200804214711.177645-3-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200804214711.177645-1-kieran.bingham@ideasonboard.com> References: <20200804214711.177645-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 02/13] libcamera: formats: add numPlanes helper X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Determine the number of planes used by a format by counting the number of PixelFormatPlaneInfo entries with a valid entry. Reviewed-by: Niklas Söderlund Reviewed-by: Laurent Pinchart Signed-off-by: Kieran Bingham --- v3: - Rename $SUBJECT :D v2: - Rename to numPlanes - Simplify fucntion doc. --- include/libcamera/internal/formats.h | 2 ++ src/libcamera/formats.cpp | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/include/libcamera/internal/formats.h b/include/libcamera/internal/formats.h index b8e474916378..51a8a6b8b0ae 100644 --- a/include/libcamera/internal/formats.h +++ b/include/libcamera/internal/formats.h @@ -46,6 +46,8 @@ public: unsigned int frameSize(const Size &size, const std::array &strides) const; + unsigned int numPlanes() const; + /* \todo Add support for non-contiguous memory planes */ const char *name; PixelFormat format; diff --git a/src/libcamera/formats.cpp b/src/libcamera/formats.cpp index a51e96810aed..ebaae9be3d75 100644 --- a/src/libcamera/formats.cpp +++ b/src/libcamera/formats.cpp @@ -821,4 +821,22 @@ PixelFormatInfo::frameSize(const Size &size, return sum; } +/** + * \brief Retrieve the number of planes represented by the format + * \return The number of planes used by the format + */ +unsigned int PixelFormatInfo::numPlanes() const +{ + unsigned int count = 0; + + for (const PixelFormatPlaneInfo &p : planes) { + if (p.bytesPerGroup == 0) + break; + + count++; + } + + return count; +} + } /* namespace libcamera */ From patchwork Tue Aug 4 21:47:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 9196 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id D945BBD87A for ; Tue, 4 Aug 2020 21:47:20 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id AB09A60589; Tue, 4 Aug 2020 23:47:20 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="KMcxcifl"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 80C2F60550 for ; Tue, 4 Aug 2020 23:47:17 +0200 (CEST) Received: from Q.local (cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 01FC3A60; Tue, 4 Aug 2020 23:47:16 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1596577637; bh=nQrGnvb95ZZ2ujBQJw3JCHSyPYMBXCpLzQNfdgLHXJQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KMcxciflSDtbpjr/ua8LRCJKOx5oA42JMJUvcGhAMkh70HRPnn/jbVyKW69twkZ2X XsdfbO2EZJkYj7zG1uMOLCutWr7RTWCIUUP6uZneiXe0djezbI6UqqQP/YHL2KU+vE 40aM5gtIn34+2m+LqWDW3iQpmy2ynbYx3S6XKXPY= From: Kieran Bingham To: libcamera devel Date: Tue, 4 Aug 2020 22:47:01 +0100 Message-Id: <20200804214711.177645-4-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200804214711.177645-1-kieran.bingham@ideasonboard.com> References: <20200804214711.177645-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 03/13] libcamera: buffer: Create a MappedBuffer X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Provide a MappedFrameBuffer helper class which will map all of the Planes within a FrameBuffer and provide CPU addressable pointers for those planes. The MappedFrameBuffer implements the interface of the MappedBuffer allowing other buffer types to be constructed of the same form, with a common interface and cleanup. This allows MappedBuffer instances to be created from Camera3Buffer types. Mappings are removed upon destruction. Signed-off-by: Kieran Bingham --- v3 - Documentation fixups --- include/libcamera/internal/buffer.h | 47 ++++++++ src/libcamera/buffer.cpp | 162 +++++++++++++++++++++++++++- 2 files changed, 208 insertions(+), 1 deletion(-) create mode 100644 include/libcamera/internal/buffer.h diff --git a/include/libcamera/internal/buffer.h b/include/libcamera/internal/buffer.h new file mode 100644 index 000000000000..e86a003cd8ba --- /dev/null +++ b/include/libcamera/internal/buffer.h @@ -0,0 +1,47 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2020, Google Inc. + * + * buffer.h - Internal buffer handling + */ +#ifndef __LIBCAMERA_INTERNAL_BUFFER_H__ +#define __LIBCAMERA_INTERNAL_BUFFER_H__ + +#include +#include + +#include +#include + +namespace libcamera { + +using MappedPlane = Span; + +class MappedBuffer +{ +public: + MappedBuffer(); + ~MappedBuffer(); + + MappedBuffer(MappedBuffer &&rhs); + MappedBuffer &operator=(MappedBuffer &&rhs); + + bool isValid() const { return valid_; } + int error() const { return error_; } + const std::vector &maps() const { return maps_; } + +protected: + int error_; + bool valid_; + std::vector maps_; +}; + +class MappedFrameBuffer : public MappedBuffer +{ +public: + MappedFrameBuffer(const FrameBuffer *buffer, int flags); +}; + +} /* namespace libcamera */ + +#endif /* __LIBCAMERA_INTERNAL_BUFFER_H__ */ diff --git a/src/libcamera/buffer.cpp b/src/libcamera/buffer.cpp index 8278f8a92af4..5f7dff60a48b 100644 --- a/src/libcamera/buffer.cpp +++ b/src/libcamera/buffer.cpp @@ -6,6 +6,7 @@ */ #include +#include "libcamera/internal/buffer.h" #include #include @@ -15,7 +16,8 @@ #include "libcamera/internal/log.h" /** - * \file buffer.h + * \file libcamera/buffer.h + * \file libcamera/internal/buffer.h * \brief Buffer handling */ @@ -290,4 +292,162 @@ int FrameBuffer::copyFrom(const FrameBuffer *src) return 0; } +/** + * \class MappedBuffer + * \brief Provide an interface to support managing memory mapped buffers + * + * The MappedBuffer interface provides access to a set of MappedPlanes which + * are available for access by a CPU. + * + * The MappedBuffer interface does not implement any valid constructor but + * defines the move operators and destructors for the derived implementations, + * which are able to construct according to their derived types and given + * flags. + * + * This allows treating CPU accessible memory through a generic interface + * regardless of whether it originates from a libcamera FrameBuffer or other + * source. + */ + +/** + * \brief Construct an empty MappedBuffer + * + * A default constructor is required to allow subclassing the MappedBuffer + * class. Construct an initialised, but invalid MappedBuffer. + */ +MappedBuffer::MappedBuffer() + : error_(0), valid_(false) +{ +} + +/** + * \brief Construct a MappedBuffer by taking the \a rhs mappings + * \param[in] rhs The other MappedBuffer + * + * Moving a MappedBuffer moves the mappings contained in the \a rhs to the new + * MappedBuffer and invalidates the \a rhs. No mappings are unmapped or + * destroyed in this process. + */ +MappedBuffer::MappedBuffer(MappedBuffer &&rhs) +{ + *this = std::move(rhs); +} + +/** + * \brief Move assingment operator, move a MappedBuffer by taking the \a rhs mappings + * \param[in] rhs The other MappedBuffer + * + * Moving a MappedBuffer moves the mappings contained in the \a rhs to the new + * MappedBuffer and invalidates the \a rhs. No mappings are unmapped or + * destroyed in this process. + */ +MappedBuffer &MappedBuffer::operator=(MappedBuffer &&rhs) +{ + error_ = rhs.error_; + valid_ = rhs.valid_; + maps_ = std::move(rhs.maps_); + rhs.valid_ = false; + rhs.error_ = 0; + + return *this; +} + +MappedBuffer::~MappedBuffer() +{ + for (MappedPlane map : maps_) + munmap(map.data(), map.size()); +} + +/** + * \fn MappedBuffer::isValid() + * \brief Check if the MappedBuffer instance is valid + * \return True if the MappedBuffer has valid mappings, false otherwise + */ + +/** + * \fn MappedBuffer::error() + * \brief Retrieve the map error status + * + * This function retrieves the error status from the MappedBuffer. + * The error status is a negative number as defined by errno.h. If + * no error occurred, this function returns 0. + * + * \return 0 on success or a negative error code otherwise + */ + +/** + * \fn MappedBuffer::maps() + * \brief Retrieve the mapped planes + * + * This function retrieves the successfully mapped planes stored as a vector + * of Span to provide access to the mapped memory. + * + * \return A vector of the mapped planes. + */ + +/** + * \var MappedBuffer::valid_ + * \brief Stores the status of the mapping + * + * MappedBuffer implementations shall set this to represent if the mapping + * was successfully completed without errors. + */ + +/** + * \var MappedBuffer::error_ + * \brief Stores the error value if present + * + * MappedBuffer implementations shall set this to a negative value as defined + * by errno.h if an error occured during the mapping process + */ + +/** + * \var MappedBuffer::maps_ + * \brief Stores the internal + * + * MappedBuffer implementations shall store the mappings they create in this + * vector which is parsed during destruct to unmap any memory mappings which + * completed successfully. + */ + +/** + * \class MappedFrameBuffer + * \brief Maps a FrameBuffer using the MappedBuffer interface + * + * The MappedFrameBuffer interface maps a FrameBuffer instance + * + * The MappedBuffer interface does not implement any constructor but defines + * the move operators and destructors for the derived implementations, which + * are able to construct according to their derived types and given flags. + */ + +/** + * \brief Map all planes of a FrameBuffer + * \param[in] buffer FrameBuffer to be mapped + * \param[in] flags Protection flags to apply to map + * + * Construct an object to map a frame buffer for CPU access. + * The flags are passed directly to mmap and should be either PROT_READ, + * PROT_WRITE, or a bitwise-or combination of both. + */ +MappedFrameBuffer::MappedFrameBuffer(const FrameBuffer *buffer, int flags) +{ + maps_.reserve(buffer->planes().size()); + + for (const FrameBuffer::Plane &plane : buffer->planes()) { + void *address = mmap(nullptr, plane.length, flags, + MAP_SHARED, plane.fd.fd(), 0); + + if (address == MAP_FAILED) { + error_ = -errno; + LOG(Buffer, Error) << "Failed to mmap plane"; + break; + } + + maps_.emplace_back(static_cast(address), plane.length); + } + + valid_ = buffer->planes().size() == maps_.size(); +} + } /* namespace libcamera */ From patchwork Tue Aug 4 21:47:02 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 9197 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 5131EBD87A for ; Tue, 4 Aug 2020 21:47:21 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id EEC406059B; Tue, 4 Aug 2020 23:47:20 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="SHbusFKn"; dkim-atps=neutral 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 CC3BC60555 for ; Tue, 4 Aug 2020 23:47:17 +0200 (CEST) Received: from Q.local (cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 58A31A66; Tue, 4 Aug 2020 23:47:17 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1596577637; bh=8X9yghRwQQ+CEuljUFWC6/ib0WPrO/zRGEc2udASvCs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SHbusFKnHTSKyxoe5SJ93rK/0o7gg2ZtsOMc55bgV4EjxpLqnC1rM1CMukxfOk8xa CLXSJ3rn5BWDe+UHteu3iHSNyCckQkbmw3LN1Y6rWBE0U39auTkl0qlY1M7AhhkYd1 nLf/Stujc8PisoSJteCZd3pUr52ngF1T87oRlcVk= From: Kieran Bingham To: libcamera devel Date: Tue, 4 Aug 2020 22:47:02 +0100 Message-Id: <20200804214711.177645-5-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200804214711.177645-1-kieran.bingham@ideasonboard.com> References: <20200804214711.177645-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 04/13] test: mapped-buffers: Provide MappedBuffer test X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Provide initial testing framework for the MappedBuffer component. Signed-off-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- test/mapped-buffers.cpp | 113 ++++++++++++++++++++++++++++++++++++++++ test/meson.build | 1 + 2 files changed, 114 insertions(+) create mode 100644 test/mapped-buffers.cpp diff --git a/test/mapped-buffers.cpp b/test/mapped-buffers.cpp new file mode 100644 index 000000000000..4c0557070ca3 --- /dev/null +++ b/test/mapped-buffers.cpp @@ -0,0 +1,113 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2020, Google Inc. + * + * libcamera internal MappedBuffer tests + */ + +#include + +#include "camera_test.h" +#include "test.h" + +#include "libcamera/internal/buffer.h" + +using namespace std; + +namespace { + +class MappedBuffers : public CameraTest, public Test +{ +public: + MappedBuffers() + : CameraTest("VIMC Sensor B") + { + } + +protected: + int init() override + { + if (status_ != TestPass) + return status_; + + config_ = camera_->generateConfiguration({ StreamRole::VideoRecording }); + if (!config_ || config_->size() != 1) { + cout << "Failed to generate default configuration" << endl; + return TestFail; + } + + allocator_ = new FrameBufferAllocator(camera_); + + StreamConfiguration &cfg = config_->at(0); + + if (camera_->acquire()) { + cout << "Failed to acquire the camera" << endl; + return TestFail; + } + + if (camera_->configure(config_.get())) { + cout << "Failed to set default configuration" << endl; + return TestFail; + } + + stream_ = cfg.stream(); + + int ret = allocator_->allocate(stream_); + if (ret < 0) + return TestFail; + + return TestPass; + } + + void cleanup() override + { + delete allocator_; + } + + int run() override + { + const std::unique_ptr &buffer = allocator_->buffers(stream_).front(); + std::vector maps; + + MappedFrameBuffer map(buffer.get(), PROT_READ); + if (!map.isValid()) { + cout << "Failed to successfully map buffer" << endl; + return TestFail; + } + + /* Make sure we can move it. */ + maps.emplace_back(std::move(map)); + + /* But copying is prevented, it would cause double-unmap. */ + // MappedFrameBuffer map_copy = map; + + /* Local map should be invalid (after move). */ + if (map.isValid()) { + cout << "Post-move map should not be valid" << endl; + return TestFail; + } + + /* Test for multiple successful maps on the same buffer. */ + MappedFrameBuffer write_map(buffer.get(), PROT_WRITE); + if (!write_map.isValid()) { + cout << "Failed to map write buffer" << endl; + return TestFail; + } + + MappedFrameBuffer rw_map(buffer.get(), PROT_READ | PROT_WRITE); + if (!rw_map.isValid()) { + cout << "Failed to map RW buffer" << endl; + return TestFail; + } + + return TestPass; + } + + std::unique_ptr config_; + FrameBufferAllocator *allocator_; + Stream *stream_; +}; + +} /* namespace */ + +TEST_REGISTER(MappedBuffers); diff --git a/test/meson.build b/test/meson.build index 775187159dec..376ee6cee175 100644 --- a/test/meson.build +++ b/test/meson.build @@ -31,6 +31,7 @@ internal_tests = [ ['file', 'file.cpp'], ['file-descriptor', 'file-descriptor.cpp'], ['hotplug-cameras', 'hotplug-cameras.cpp'], + ['mapped-buffers', 'mapped-buffers.cpp'], ['message', 'message.cpp'], ['object', 'object.cpp'], ['object-delete', 'object-delete.cpp'], From patchwork Tue Aug 4 21:47:03 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 9198 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 77D87BD87A for ; Tue, 4 Aug 2020 21:47:23 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 4732B60564; Tue, 4 Aug 2020 23:47:23 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="kf+FJGmP"; dkim-atps=neutral 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 4F6C26055E for ; Tue, 4 Aug 2020 23:47:18 +0200 (CEST) Received: from Q.local (cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id B6380D31; Tue, 4 Aug 2020 23:47:17 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1596577637; bh=/YjNwo2R5Sl/LGidDkf5GQj2Hn5NUwo4ESk18Uz7IgU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kf+FJGmPEY6sy3YmfI6rTe31TveFiIcbF7almKkw5U8Zs3mzk/o5xvAZkqcmmes0s VcDoRel1gKXIcc1rE1iGyuj3x0sGsPMFS01M6x93WGQGx3HTC0sMF2HW6HP1GuiP4G mUFZvG4eVSjZ7uucnDQ9MsgGdzte4WS6nLjNbFLs= From: Kieran Bingham To: libcamera devel Date: Tue, 4 Aug 2020 22:47:03 +0100 Message-Id: <20200804214711.177645-6-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200804214711.177645-1-kieran.bingham@ideasonboard.com> References: <20200804214711.177645-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 05/13] libcamera: buffer: Convert copyFrom to use MappedFrameBuffer X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Utilise the new MappedFrameBuffer helper to handle all mapping and unmapping of the copyFrom helper function. Reviewed-by: Laurent Pinchart Signed-off-by: Kieran Bingham --- src/libcamera/buffer.cpp | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/src/libcamera/buffer.cpp b/src/libcamera/buffer.cpp index 5f7dff60a48b..1e3fd3cd0dcd 100644 --- a/src/libcamera/buffer.cpp +++ b/src/libcamera/buffer.cpp @@ -259,32 +259,23 @@ int FrameBuffer::copyFrom(const FrameBuffer *src) } } - for (unsigned int i = 0; i < planes_.size(); i++) { - void *dstmem = mmap(nullptr, planes_[i].length, PROT_WRITE, - MAP_SHARED, planes_[i].fd.fd(), 0); + MappedFrameBuffer source(src, PROT_READ); + MappedFrameBuffer destination(this, PROT_WRITE); - if (dstmem == MAP_FAILED) { - LOG(Buffer, Error) - << "Failed to map destination plane " << i; - metadata_.status = FrameMetadata::FrameError; - return -EINVAL; - } - - void *srcmem = mmap(nullptr, src->planes_[i].length, PROT_READ, - MAP_SHARED, src->planes_[i].fd.fd(), 0); - - if (srcmem == MAP_FAILED) { - munmap(dstmem, planes_[i].length); - LOG(Buffer, Error) - << "Failed to map source plane " << i; - metadata_.status = FrameMetadata::FrameError; - return -EINVAL; - } + if (!source.isValid()) { + LOG(Buffer, Error) << "Failed to map source planes"; + return -EINVAL; + } - memcpy(dstmem, srcmem, src->planes_[i].length); + if (!destination.isValid()) { + LOG(Buffer, Error) << "Failed to map destination planes"; + return -EINVAL; + } - munmap(srcmem, src->planes_[i].length); - munmap(dstmem, planes_[i].length); + for (unsigned int i = 0; i < planes_.size(); i++) { + memcpy(destination.maps()[i].data(), + source.maps()[i].data(), + source.maps()[i].size()); } metadata_ = src->metadata_; From patchwork Tue Aug 4 21:47:04 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 9199 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id C87A3BD87B for ; Tue, 4 Aug 2020 21:47:23 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 977EB605AC; Tue, 4 Aug 2020 23:47:23 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="YaEC8bQU"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id BE69E6054C for ; Tue, 4 Aug 2020 23:47:18 +0200 (CEST) Received: from Q.local (cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 43732D33; Tue, 4 Aug 2020 23:47:18 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1596577638; bh=/hSMByqfE67UwpGMJsfO63cgLSEgycVYv1HQ8NPUG4Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YaEC8bQUE8rHuHNWuVqvoov5p25WA7OUD6sWTzvDAEmmxt7Iz+FjHJXtfJuwHE5Ke 5FBRUOHUAFg/2f0DYJ/qLay85kTim5nvNmVJiH19+te/aPS7yHUitmnJ+IHMiQaZgH EF9khg/7W985cIjn+uAEBvopF92DcVRT9wj22ua8= From: Kieran Bingham To: libcamera devel Date: Tue, 4 Aug 2020 22:47:04 +0100 Message-Id: <20200804214711.177645-7-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200804214711.177645-1-kieran.bingham@ideasonboard.com> References: <20200804214711.177645-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 06/13] android: camera_device: Fix trivial spelling X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Fix the spelling of 'successfully' reported in an error message. Reviewed-by: Laurent Pinchart Reviewed-by: Jacopo Mondi Signed-off-by: Kieran Bingham --- src/android/camera_device.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index b5206fd623c6..84d8c32a90c2 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -1184,7 +1184,7 @@ void CameraDevice::requestComplete(Request *request) std::unique_ptr resultMetadata; if (request->status() != Request::RequestComplete) { - LOG(HAL, Error) << "Request not succesfully completed: " + LOG(HAL, Error) << "Request not successfully completed: " << request->status(); status = CAMERA3_BUFFER_STATUS_ERROR; } From patchwork Tue Aug 4 21:47:05 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 9200 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 47AECBD87A for ; Tue, 4 Aug 2020 21:47:24 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 152A46059F; Tue, 4 Aug 2020 23:47:24 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="oZ8DRloP"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 474A76054E for ; Tue, 4 Aug 2020 23:47:19 +0200 (CEST) Received: from Q.local (cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id AFB10D39; Tue, 4 Aug 2020 23:47:18 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1596577639; bh=r1dCq+bzrAWmHf089Bgrm4j0rwbtXHY5NscqEZFu/9Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oZ8DRloPUZnHSHkA1/6aw1Eq5/TNhqOVdVEApuJmsMtobkBLidZYEAfiAgsSt7e0n 9FMY8gMpVgSbWQeSmlIqFkiNoLQ7qyl3lDzWjcJVcdP5n8CEtoy+SghsDwlmU+I7go YBPEyG2HSMG5vjG1KfSIpFkwdVydIDUiASHyXX9k= From: Kieran Bingham To: libcamera devel Date: Tue, 4 Aug 2020 22:47:05 +0100 Message-Id: <20200804214711.177645-8-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200804214711.177645-1-kieran.bingham@ideasonboard.com> References: <20200804214711.177645-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 07/13] android: camera_device: Report an error in notifyError() X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" If an error is generated, try to be verbose about it in the libcamera logs. Reviewed-by: Niklas Söderlund Reviewed-by: Laurent Pinchart Signed-off-by: Kieran Bingham Reviewed-by: Jacopo Mondi --- v2: - Add todo to note possible debug info expansion - Fix typo - Print .toString() formatted pixel format v3: - Remove colon from todo: --- src/android/camera_device.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index 84d8c32a90c2..e4dce680d46f 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -1258,6 +1258,13 @@ void CameraDevice::notifyError(uint32_t frameNumber, camera3_stream_t *stream) { camera3_notify_msg_t notify = {}; + /* + * \todo Report and identify the stream number or configuration to + * clarify the stream that failed. + */ + LOG(HAL, Error) << "Error occurred on frame " << frameNumber << " (" + << toPixelFormat(stream->format).toString() << ")"; + notify.type = CAMERA3_MSG_ERROR; notify.message.error.error_stream = stream; notify.message.error.frame_number = frameNumber; From patchwork Tue Aug 4 21:47:06 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 9201 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id B5C5DBD87A for ; Tue, 4 Aug 2020 21:47:24 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 864366054A; Tue, 4 Aug 2020 23:47:24 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="B6iGX808"; dkim-atps=neutral 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 B219560550 for ; Tue, 4 Aug 2020 23:47:19 +0200 (CEST) Received: from Q.local (cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 302C4D37; Tue, 4 Aug 2020 23:47:19 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1596577639; bh=ujCUR6FrvnfglleLoNuAc0BOQuUW+UBhQt5qrF8eDOY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=B6iGX808CL9zbAW6TZ4TRvfyMOfttfS9BPrhn/ki/FjZA5/P5w8fUNj5KrWdFj/Em JA4Ye5Da1wmiW9q85+jBh7kl5Sr09RTrJk8ph6zmI21AGZgAfkSg1HryyXGFUyf76W PkPVydmbw4kJHrUaMQ55pgBlMqUeKVSzdfWEV2Aw= From: Kieran Bingham To: libcamera devel Date: Tue, 4 Aug 2020 22:47:06 +0100 Message-Id: <20200804214711.177645-9-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200804214711.177645-1-kieran.bingham@ideasonboard.com> References: <20200804214711.177645-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 08/13] android: camera_device: Only construct required planes X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" The camera3buffer describes the number of filedescriptors given. Don't try to construct more planes than that. Reviewed-by: Laurent Pinchart Signed-off-by: Kieran Bingham --- src/android/camera_device.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index e4dce680d46f..087e226f5a82 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -1082,9 +1082,20 @@ int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list) FrameBuffer *CameraDevice::createFrameBuffer(const buffer_handle_t camera3buffer) { std::vector planes; - for (unsigned int i = 0; i < 3; i++) { + for (int i = 0; i < camera3buffer->numFds; i++) { + /* Skip unused planes. */ + if (camera3buffer->data[i] == -1) + break; + FrameBuffer::Plane plane; plane.fd = FileDescriptor(camera3buffer->data[i]); + if (!plane.fd.isValid()) { + LOG(HAL, Error) << "Failed to obtain FileDescriptor (" + << camera3buffer->data[i] << ") " + << " on plane " << i; + break; + } + /* * Setting length to zero here is OK as the length is only used * to map the memory of the plane. Libcamera do not need to poke From patchwork Tue Aug 4 21:47:07 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 9202 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 3BB31BD87A for ; Tue, 4 Aug 2020 21:47:25 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 053BC60561; Tue, 4 Aug 2020 23:47:25 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="rcj8GHeT"; dkim-atps=neutral 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 145D660555 for ; Tue, 4 Aug 2020 23:47:20 +0200 (CEST) Received: from Q.local (cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 93D3ED3A; Tue, 4 Aug 2020 23:47:19 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1596577639; bh=UrIP0C+Mn5wKh68gwLr6O3QsOlhjsxdlRpDD5geknQQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rcj8GHeTMuq8wdyrqJ/7eTQOOKG35krmSdbzAKuwA4is/ackuGFUY4Mn6DSdoaEwD t4KLp99kKl3ZhRaEBo0VGHlWro2GLMhQpNC+SFWfRHn5ag0pVja2x+sBcCmZjLg2Fy QVhcL3IgnDl5rM3xZo9kCbCpapf7aBEAKF/eRgCE= From: Kieran Bingham To: libcamera devel Date: Tue, 4 Aug 2020 22:47:07 +0100 Message-Id: <20200804214711.177645-10-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200804214711.177645-1-kieran.bingham@ideasonboard.com> References: <20200804214711.177645-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 09/13] android: camera_device: Query plane length X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Use lseek to query the length of planes where possible rather than leaving the plane.length as zero, which prevents mapping buffers for software processing. Reviewed-by: Laurent Pinchart Signed-off-by: Kieran Bingham --- v3 - Return nullptr if we fail to query plane length v2: - no longer support an invalid plane length. --- src/android/camera_device.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index 087e226f5a82..5899154b3e78 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -1096,12 +1096,13 @@ FrameBuffer *CameraDevice::createFrameBuffer(const buffer_handle_t camera3buffer break; } - /* - * Setting length to zero here is OK as the length is only used - * to map the memory of the plane. Libcamera do not need to poke - * at the memory content queued by the HAL. - */ - plane.length = 0; + off_t length = lseek(plane.fd.fd(), 0, SEEK_END); + if (length == -1) { + LOG(HAL, Error) << "Failed to query plane length"; + return nullptr; + } + + plane.length = length; planes.push_back(std::move(plane)); } From patchwork Tue Aug 4 21:47:08 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 9203 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id A04A1BD87A for ; Tue, 4 Aug 2020 21:47:25 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 6E2E3605AB; Tue, 4 Aug 2020 23:47:25 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="p3PyiuAC"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 7415C6055E for ; Tue, 4 Aug 2020 23:47:20 +0200 (CEST) Received: from Q.local (cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id F2D6AD5F; Tue, 4 Aug 2020 23:47:19 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1596577640; bh=xSWyxn4rUgI7fXZbQWv7uKbOnOr9mMB61KLdspvcDKQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=p3PyiuACI98b3n7LD4lihjm+2kgNQjDx5z3kaLhXDzXBRfnCJo/3JOwOt3oOzqCGn N3wCA1LaDqa0c0KNj11TTJE5QdKW7Izpu+hOFpoB7/jZANxuRPEY2sl4Mgku05Ix7i za6J7mruq/udBxwdSxn6L3px1GLolc3M6Yb4D9rs= From: Kieran Bingham To: libcamera devel Date: Tue, 4 Aug 2020 22:47:08 +0100 Message-Id: <20200804214711.177645-11-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200804214711.177645-1-kieran.bingham@ideasonboard.com> References: <20200804214711.177645-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 10/13] android: camera_device: Report configuration changes from validate() X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" When we call validate on a configuration, if there are any adjustments on the configuration, we fail without showing why. Display the stream configuration after the validate stage to aid debugging stream startup failures. Reviewed-by: Jacopo Mondi Signed-off-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- src/android/camera_device.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index 5899154b3e78..4178db952846 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -1047,7 +1047,11 @@ int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list) case CameraConfiguration::Valid: break; case CameraConfiguration::Adjusted: - LOG(HAL, Info) << "Camera configuration adjusted"; + LOG(HAL, Info) << "Camera configuration adjusted:"; + + for (const StreamConfiguration &cfg : *config_) + LOG(HAL, Info) << " : " << cfg.toString(); + config_.reset(); return -EINVAL; case CameraConfiguration::Invalid: From patchwork Tue Aug 4 21:47:09 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 9204 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 0A783BD87B for ; Tue, 4 Aug 2020 21:47:26 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id C650C60555; Tue, 4 Aug 2020 23:47:25 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="tONj3B8j"; dkim-atps=neutral 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 CC1E760597 for ; Tue, 4 Aug 2020 23:47:20 +0200 (CEST) Received: from Q.local (cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 5BFA6D84; Tue, 4 Aug 2020 23:47:20 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1596577640; bh=HSQtq0YpaIaTxOy0WeHU1lEPHqEBZox3cu1j1CXOoiM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tONj3B8j5QOTWrW2NHghgRPiqvQTxo5an89oiROA3u2uOU0SWoI8zcIdVZ65zEN2z pHL45dOyykEG59SH5ZvAkStpwlVVluhSeZY2S6ud2sxq+tf56Q6gb0QCzzPkqkD/fc vb7ovepj/ausU5pKF3UIJzAtlUxOC4ZXxU9RSMi0= From: Kieran Bingham To: libcamera devel Date: Tue, 4 Aug 2020 22:47:09 +0100 Message-Id: <20200804214711.177645-12-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200804214711.177645-1-kieran.bingham@ideasonboard.com> References: <20200804214711.177645-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 11/13] android: camera_device: Generate ResultMetadata earlier X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Generate the ResultMetadata before performing JPEG compression so that JPEG specific metadata can be added to the metadata when it has been processed. Reviewed-by: Jacopo Mondi Signed-off-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- src/android/camera_device.cpp | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index 4178db952846..c529246e115c 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -1198,6 +1198,8 @@ void CameraDevice::requestComplete(Request *request) const std::map &buffers = request->buffers(); camera3_buffer_status status = CAMERA3_BUFFER_STATUS_OK; std::unique_ptr resultMetadata; + Camera3RequestDescriptor *descriptor = + reinterpret_cast(request->cookie()); if (request->status() != Request::RequestComplete) { LOG(HAL, Error) << "Request not successfully completed: " @@ -1205,9 +1207,17 @@ void CameraDevice::requestComplete(Request *request) status = CAMERA3_BUFFER_STATUS_ERROR; } + /* + * \todo The timestamp used for the metadata is currently always taken + * from the first buffer (which may be the first stream) in the Request. + * It might be appropriate to return a 'correct' (as determined by + * pipeline handlers) timestamp in the Request itself. + */ + FrameBuffer *buffer = buffers.begin()->second; + resultMetadata = getResultMetadata(descriptor->frameNumber, + buffer->metadata().timestamp); + /* Prepare to call back the Android camera stack. */ - Camera3RequestDescriptor *descriptor = - reinterpret_cast(request->cookie()); camera3_capture_result_t captureResult = {}; captureResult.frame_number = descriptor->frameNumber; @@ -1220,21 +1230,12 @@ void CameraDevice::requestComplete(Request *request) captureResult.output_buffers = const_cast(descriptor->buffers); - /* - * \todo The timestamp used for the metadata is currently always taken - * from the first buffer (which may be the first stream) in the Request. - * It might be appropriate to return a 'correct' (as determined by - * pipeline handlers) timestamp in the Request itself. - */ - FrameBuffer *buffer = buffers.begin()->second; if (status == CAMERA3_BUFFER_STATUS_OK) { notifyShutter(descriptor->frameNumber, buffer->metadata().timestamp); captureResult.partial_result = 1; - resultMetadata = getResultMetadata(descriptor->frameNumber, - buffer->metadata().timestamp); captureResult.result = resultMetadata->get(); } From patchwork Tue Aug 4 21:47:10 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 9205 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 5A581BD87A for ; Tue, 4 Aug 2020 21:47:26 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 27FB3605AD; Tue, 4 Aug 2020 23:47:26 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="laRgQzFD"; dkim-atps=neutral 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 4349860580 for ; Tue, 4 Aug 2020 23:47:21 +0200 (CEST) Received: from Q.local (cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id BBB12D6E; Tue, 4 Aug 2020 23:47:20 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1596577641; bh=Dj0d6ykbvzP8nbjRPjQMTKtOTv7C+W0PjYil6v4gahI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=laRgQzFDOXItSjWutDosRuTMXA0jQTZSg1mM1bDPVaQfwu+Gra21TX30W+m6BnJeH gDDP4wXpbc4k+H20qaN3VaSoFzDdsQ/EwX+WV4PdziKx14cAmQz2tY82ejDkrGXbRr cKGm+lTvCxl5nYDy7MyuZQctw+ld8hRzbsXIlqRA= From: Kieran Bingham To: libcamera devel Date: Tue, 4 Aug 2020 22:47:10 +0100 Message-Id: <20200804214711.177645-13-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200804214711.177645-1-kieran.bingham@ideasonboard.com> References: <20200804214711.177645-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 12/13] android: Introduce JPEG compression X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Provide a compressor interface and implement a JPEG compressor using libjpeg. Reviewed-by: Jacopo Mondi Signed-off-by: Kieran Bingham v2: - Convert to use the libcamera format information rather than duplicating it ourselves. - Not easy to get the horizontal subsampling - not easy to determine if we have an nvSwap... v3: - Fix frame.error return value (negative inversion) - Fix comments - constify data table - set jpeg image lenght to zero after free - set override on class interface functions. - Remove ununsed fucntion prototype - Rename pixelFormatInfo to pixelFormatInfo_ --- src/android/jpeg/compressor.h | 28 ++++ src/android/jpeg/compressor_jpeg.cpp | 217 +++++++++++++++++++++++++++ src/android/jpeg/compressor_jpeg.h | 45 ++++++ src/android/meson.build | 1 + src/libcamera/meson.build | 2 + 5 files changed, 293 insertions(+) create mode 100644 src/android/jpeg/compressor.h create mode 100644 src/android/jpeg/compressor_jpeg.cpp create mode 100644 src/android/jpeg/compressor_jpeg.h diff --git a/src/android/jpeg/compressor.h b/src/android/jpeg/compressor.h new file mode 100644 index 000000000000..18d8f65eba02 --- /dev/null +++ b/src/android/jpeg/compressor.h @@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2020, Google Inc. + * + * compressor.h - Image compression interface + */ +#ifndef __ANDROID_JPEG_COMPRESSOR_H__ +#define __ANDROID_JPEG_COMPRESSOR_H__ + +#include +#include + +struct CompressedImage { + unsigned char *data; + unsigned long length; +}; + +class Compressor +{ +public: + virtual ~Compressor(){}; + + virtual int configure(const libcamera::StreamConfiguration &cfg) = 0; + virtual int compress(const libcamera::FrameBuffer *source, CompressedImage *image) = 0; + virtual void free(CompressedImage *image) = 0; +}; + +#endif /* __ANDROID_JPEG_COMPRESSOR_H__ */ diff --git a/src/android/jpeg/compressor_jpeg.cpp b/src/android/jpeg/compressor_jpeg.cpp new file mode 100644 index 000000000000..9921a294128c --- /dev/null +++ b/src/android/jpeg/compressor_jpeg.cpp @@ -0,0 +1,217 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2020, Google Inc. + * + * compressor_jpeg.cpp - JPEG compression using libjpeg native API + */ + +#include "compressor_jpeg.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "libcamera/internal/formats.h" +#include "libcamera/internal/log.h" + +using namespace libcamera; + +LOG_DEFINE_CATEGORY(JPEG) + +namespace { + +struct JPEGPixelFormatInfo { + J_COLOR_SPACE colorSpace; + const PixelFormatInfo &pixelFormatInfo; + bool nvSwap; +}; + +const std::map pixelInfo{ + { formats::R8, { JCS_GRAYSCALE, PixelFormatInfo::info(formats::R8), false } }, + + { formats::RGB888, { JCS_EXT_BGR, PixelFormatInfo::info(formats::RGB888), false } }, + { formats::BGR888, { JCS_EXT_RGB, PixelFormatInfo::info(formats::BGR888), false } }, + + { formats::NV12, { JCS_YCbCr, PixelFormatInfo::info(formats::NV12), false } }, + { formats::NV21, { JCS_YCbCr, PixelFormatInfo::info(formats::NV21), true } }, + { formats::NV16, { JCS_YCbCr, PixelFormatInfo::info(formats::NV16), false } }, + { formats::NV61, { JCS_YCbCr, PixelFormatInfo::info(formats::NV61), true } }, + { formats::NV24, { JCS_YCbCr, PixelFormatInfo::info(formats::NV24), false } }, + { formats::NV42, { JCS_YCbCr, PixelFormatInfo::info(formats::NV42), true } }, +}; + +const struct JPEGPixelFormatInfo &findPixelInfo(const PixelFormat &format) +{ + static const struct JPEGPixelFormatInfo invalidPixelFormat { + JCS_UNKNOWN, PixelFormatInfo(), false + }; + + const auto iter = pixelInfo.find(format); + if (iter == pixelInfo.end()) { + LOG(JPEG, Error) << "Unsupported pixel format for JPEG compressor: " + << format.toString(); + return invalidPixelFormat; + } + + return iter->second; +} + +} /* namespace */ + +CompressorJPEG::CompressorJPEG() + : quality_(95) +{ + /* \todo: Expand error handling coverage with a custom handler. */ + compress_.err = jpeg_std_error(&jerr_); + + jpeg_create_compress(&compress_); +} + +CompressorJPEG::~CompressorJPEG() +{ + jpeg_destroy_compress(&compress_); +} + +int CompressorJPEG::configure(const StreamConfiguration &cfg) +{ + const struct JPEGPixelFormatInfo info = findPixelInfo(cfg.pixelFormat); + if (info.colorSpace == JCS_UNKNOWN) + return -ENOTSUP; + + compress_.image_width = cfg.size.width; + compress_.image_height = cfg.size.height; + compress_.in_color_space = info.colorSpace; + + compress_.input_components = info.colorSpace == JCS_GRAYSCALE ? 1 : 3; + + jpeg_set_defaults(&compress_); + jpeg_set_quality(&compress_, quality_, TRUE); + + pixelFormatInfo_ = &info.pixelFormatInfo; + + nv_ = pixelFormatInfo_->numPlanes() == 2; + nvSwap_ = info.nvSwap; + + return 0; +} + +void CompressorJPEG::compressRGB(const libcamera::MappedBuffer *frame) +{ + unsigned char *src = static_cast(frame->maps()[0].data()); + unsigned int stride = pixelFormatInfo_->stride(compress_.image_width, 0); + + JSAMPROW row_pointer[1]; + + while (compress_.next_scanline < compress_.image_height) { + row_pointer[0] = &src[compress_.next_scanline * stride]; + jpeg_write_scanlines(&compress_, row_pointer, 1); + } +} + +/* + * Compress the incoming buffer from a supported NV format. + * This naively unpacks the semi-planar NV12 to a YUV888 format for libjpeg. + * Utilisation of the RAW api will be implemented on top as a performance + * improvement. + */ +void CompressorJPEG::compressNV(const libcamera::MappedBuffer *frame) +{ + std::vector tmprowbuf(compress_.image_width * 3); + + /* + * \todo Use the raw api, and only unpack the cb/cr samples to new line + * buffers. If possible, see if we can set appropriate pixel strides + * too to save even that copy. + * + * Possible hints at: + * https://sourceforge.net/p/libjpeg/mailman/message/30815123/ + */ + unsigned int y_stride = pixelFormatInfo_->stride(compress_.image_width, 0); + unsigned int c_stride = pixelFormatInfo_->stride(compress_.image_width, 1); + + unsigned int horzSubSample = 2 * compress_.image_width / c_stride; + unsigned int vertSubSample = pixelFormatInfo_->planes[1].verticalSubSampling; + + unsigned int c_inc = horzSubSample == 1 ? 2 : 0; + unsigned int cb_pos = nvSwap_ ? 1 : 0; + unsigned int cr_pos = nvSwap_ ? 0 : 1; + + const unsigned char *src = static_cast(frame->maps()[0].data()); + const unsigned char *src_c = src + y_stride * compress_.image_height; + + JSAMPROW row_pointer[1]; + row_pointer[0] = &tmprowbuf[0]; + + for (unsigned int y = 0; y < compress_.image_height; y++) { + unsigned char *dst = &tmprowbuf[0]; + + const unsigned char *src_y = src + y * compress_.image_width; + const unsigned char *src_cb = src_c + (y / vertSubSample) * c_stride + cb_pos; + const unsigned char *src_cr = src_c + (y / vertSubSample) * c_stride + cr_pos; + + for (unsigned int x = 0; x < compress_.image_width; x += 2) { + dst[0] = *src_y; + dst[1] = *src_cb; + dst[2] = *src_cr; + src_y++; + src_cb += c_inc; + src_cr += c_inc; + dst += 3; + + dst[0] = *src_y; + dst[1] = *src_cb; + dst[2] = *src_cr; + src_y++; + src_cb += 2; + src_cr += 2; + dst += 3; + } + + jpeg_write_scanlines(&compress_, row_pointer, 1); + } +} + +int CompressorJPEG::compress(const FrameBuffer *source, CompressedImage *jpeg) +{ + MappedFrameBuffer frame(source, PROT_READ); + if (!frame.isValid()) { + LOG(JPEG, Error) << "Failed to map FrameBuffer : " + << strerror(frame.error()); + return frame.error(); + } + + jpeg_mem_dest(&compress_, &jpeg->data, &jpeg->length); + + jpeg_start_compress(&compress_, TRUE); + + LOG(JPEG, Debug) << "JPEG Compress Starting:" + << " Width: " << compress_.image_width + << " height: " << compress_.image_height; + + if (nv_) + compressNV(&frame); + else + compressRGB(&frame); + + LOG(JPEG, Debug) << "JPEG Compress Completed"; + + jpeg_finish_compress(&compress_); + + return 0; +} + +void CompressorJPEG::free(CompressedImage *jpeg) +{ + ::free(jpeg->data); + jpeg->data = nullptr; + jpeg->length = 0; +} diff --git a/src/android/jpeg/compressor_jpeg.h b/src/android/jpeg/compressor_jpeg.h new file mode 100644 index 000000000000..2284e8a6bff1 --- /dev/null +++ b/src/android/jpeg/compressor_jpeg.h @@ -0,0 +1,45 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2020, Google Inc. + * + * compressor_jpeg.h - JPEG compression using libjpeg + */ +#ifndef __ANDROID_JPEG_COMPRESSOR_JPEG_H__ +#define __ANDROID_JPEG_COMPRESSOR_JPEG_H__ + +#include "compressor.h" + +#include +#include + +#include "libcamera/internal/buffer.h" +#include "libcamera/internal/formats.h" + +#include + +class CompressorJPEG : public Compressor +{ +public: + CompressorJPEG(); + ~CompressorJPEG(); + + int configure(const libcamera::StreamConfiguration &cfg) override; + int compress(const libcamera::FrameBuffer *source, CompressedImage *jpeg) override; + void free(CompressedImage *jpeg) override; + +private: + void compressRGB(const libcamera::MappedBuffer *frame); + void compressNV(const libcamera::MappedBuffer *frame); + + struct jpeg_compress_struct compress_; + struct jpeg_error_mgr jerr_; + + unsigned int quality_; + + const libcamera::PixelFormatInfo *pixelFormatInfo_; + + bool nv_; + bool nvSwap_; +}; + +#endif /* __ANDROID_JPEG_COMPRESSOR_JPEG_H__ */ diff --git a/src/android/meson.build b/src/android/meson.build index 822cad621f01..51dcd99ee62f 100644 --- a/src/android/meson.build +++ b/src/android/meson.build @@ -6,6 +6,7 @@ android_hal_sources = files([ 'camera_device.cpp', 'camera_metadata.cpp', 'camera_ops.cpp', + 'jpeg/compressor_jpeg.cpp', ]) android_camera_metadata_sources = files([ diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build index 3aad4386ffc2..d78e2c1f6eb8 100644 --- a/src/libcamera/meson.build +++ b/src/libcamera/meson.build @@ -124,6 +124,8 @@ if get_option('android') libcamera_sources += android_hal_sources includes += android_includes libcamera_link_with += android_camera_metadata + + libcamera_deps += dependency('libjpeg') endif # We add '/' to the build_rpath as a 'safe' path to act as a boolean flag. From patchwork Tue Aug 4 21:47:11 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 9206 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id B11E3BD87B for ; Tue, 4 Aug 2020 21:47:26 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 7C7F460548; Tue, 4 Aug 2020 23:47:26 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="txfCaUmO"; dkim-atps=neutral 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 977A660548 for ; Tue, 4 Aug 2020 23:47:21 +0200 (CEST) Received: from Q.local (cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 26AD5DB1; Tue, 4 Aug 2020 23:47:21 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1596577641; bh=9Vv5QG/QPYYi8kL7gu3ejLb1PD9UXhUUmZiisXyQtx0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=txfCaUmOPitaFrp6QEObe4PBUTIn02A99bvUXlsSNxikcL8QmxoJyHCxGlXoipC1b 3SMskOt8Fb+9tHq/f+Kt4lQIAhJWZJhXR4bWyxJ1gF93lYWYncZM+Wu1lQ/JAmvAnC h4eighdLOrKUxfZQLP+4TXokePRVi3wAoRarHPvM= From: Kieran Bingham To: libcamera devel Date: Tue, 4 Aug 2020 22:47:11 +0100 Message-Id: <20200804214711.177645-14-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200804214711.177645-1-kieran.bingham@ideasonboard.com> References: <20200804214711.177645-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 13/13] android: camera_device: Support MJPEG stream construction X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" MJPEG streams must be created referencing a libcamera stream. This stream may already be provided by the request configuration, in which case the existing stream is utilised. If no compatible stream is available to encode, a new stream is requested from the libcamera configuration. Signed-off-by: Kieran Bingham Reviewed-by: Jacopo Mondi --- v3 - Add max jpeg size todo - Fix metadata allocations - Use a class member to store the max jpeg buffer size - Remove the scoping level for jpeg blob header - Don't rely on the compressor pointer as a flag - Fix camera metadata size allocations --- src/android/camera_device.cpp | 226 ++++++++++++++++++++++++++++++++-- src/android/camera_device.h | 12 ++ 2 files changed, 229 insertions(+), 9 deletions(-) diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index c529246e115c..433243c3bc56 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -8,6 +8,7 @@ #include "camera_device.h" #include "camera_ops.h" +#include #include #include @@ -22,6 +23,8 @@ #include "camera_metadata.h" #include "system/graphics.h" +#include "jpeg/compressor_jpeg.h" + using namespace libcamera; namespace { @@ -129,6 +132,54 @@ const std::map camera3FormatsMap = { LOG_DECLARE_CATEGORY(HAL); +class MappedCamera3Buffer : public MappedBuffer +{ +public: + MappedCamera3Buffer(const buffer_handle_t camera3buffer, int flags); +}; + +MappedCamera3Buffer::MappedCamera3Buffer(const buffer_handle_t camera3buffer, + int flags) +{ + maps_.reserve(camera3buffer->numFds); + error_ = 0; + + for (int i = 0; i < camera3buffer->numFds; i++) { + if (camera3buffer->data[i] == -1) + continue; + + off_t length = lseek(camera3buffer->data[i], 0, SEEK_END); + if (length < 0) { + error_ = errno; + LOG(HAL, Error) << "Failed to query plane length"; + break; + } + + void *address = mmap(nullptr, length, flags, MAP_SHARED, + camera3buffer->data[i], 0); + if (address == MAP_FAILED) { + error_ = errno; + LOG(HAL, Error) << "Failed to mmap plane"; + break; + } + + maps_.emplace_back(static_cast(address), + static_cast(length)); + } + + valid_ = error_ == 0; +} + +CameraStream::CameraStream(PixelFormat f, Size s) + : index(-1), format(f), size(s), jpeg(nullptr) +{ +} + +CameraStream::~CameraStream() +{ + delete jpeg; +}; + /* * \struct Camera3RequestDescriptor * @@ -167,6 +218,12 @@ CameraDevice::CameraDevice(unsigned int id, const std::shared_ptr &camer facing_(CAMERA_FACING_FRONT), orientation_(0) { camera_->requestCompleted.connect(this, &CameraDevice::requestComplete); + + /* + * \todo Determine a more accurate value for this during + * streamConfiguration. + */ + max_jpeg_buffer_size_ = 13 << 20; /* 13631488 from USB HAL */ } CameraDevice::~CameraDevice() @@ -417,10 +474,10 @@ std::tuple CameraDevice::calculateStaticMetadataSize() { /* * \todo Keep this in sync with the actual number of entries. - * Currently: 50 entries, 647 bytes of static metadata + * Currently: 51 entries, 667 bytes of static metadata */ - uint32_t numEntries = 50; - uint32_t byteSize = 651; + uint32_t numEntries = 51; + uint32_t byteSize = 667; /* * Calculate space occupation in bytes for dynamically built metadata @@ -576,6 +633,12 @@ const camera_metadata_t *CameraDevice::getStaticMetadata() availableThumbnailSizes.data(), availableThumbnailSizes.size()); + /* + * \todo Calculate the maximum JPEG buffer size by asking the compressor + * giving the maximum frame size required. + */ + staticMetadata_->addEntry(ANDROID_JPEG_MAX_SIZE, &max_jpeg_buffer_size_, 1); + /* Sensor static metadata. */ int32_t pixelArraySize[] = { 2592, 1944, @@ -789,6 +852,7 @@ const camera_metadata_t *CameraDevice::getStaticMetadata() ANDROID_CONTROL_AWB_LOCK_AVAILABLE, ANDROID_CONTROL_AVAILABLE_MODES, ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES, + ANDROID_JPEG_MAX_SIZE, ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE, ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE, ANDROID_SENSOR_INFO_SENSITIVITY_RANGE, @@ -855,6 +919,9 @@ const camera_metadata_t *CameraDevice::getStaticMetadata() ANDROID_SENSOR_EXPOSURE_TIME, ANDROID_STATISTICS_LENS_SHADING_MAP_MODE, ANDROID_STATISTICS_SCENE_FLICKER, + ANDROID_JPEG_SIZE, + ANDROID_JPEG_QUALITY, + ANDROID_JPEG_ORIENTATION, }; staticMetadata_->addEntry(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS, availableResultKeys.data(), @@ -1016,8 +1083,10 @@ int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list) */ unsigned int streamIndex = 0; + /* First handle all non-MJPEG streams. */ for (unsigned int i = 0; i < stream_list->num_streams; ++i) { camera3_stream_t *stream = stream_list->streams[i]; + Size size(stream->width, stream->height); PixelFormat format = toPixelFormat(stream->format); @@ -1031,16 +1100,71 @@ int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list) if (!format.isValid()) return -EINVAL; + streams_.emplace_back(format, size); + stream->priv = static_cast(&streams_[i]); + + /* Defer handling of MJPEG streams until all others are known. */ + if (format == formats::MJPEG) + continue; + StreamConfiguration streamConfiguration; - streamConfiguration.size.width = stream->width; - streamConfiguration.size.height = stream->height; + streamConfiguration.size = size; streamConfiguration.pixelFormat = format; config_->addConfiguration(streamConfiguration); streams_[i].index = streamIndex++; - stream->priv = static_cast(&streams_[i]); + } + + /* Now handle MJPEG streams, adding a new stream if required. */ + for (unsigned int i = 0; i < stream_list->num_streams; ++i) { + camera3_stream_t *stream = stream_list->streams[i]; + bool match = false; + + if (streams_[i].format != formats::MJPEG) + continue; + + /* Search for a compatible stream */ + for (unsigned int j = 0; j < config_->size(); j++) { + StreamConfiguration &cfg = config_->at(j); + + /* + * \todo The PixelFormat must also be compatible with + * the encoder. + */ + if (cfg.size == streams_[i].size) { + LOG(HAL, Info) << "Stream " << i + << " using libcamera stream " << j; + + match = true; + streams_[i].index = j; + } + } + + /* + * Without a compatible match for JPEG encoding we must + * introduce a new stream to satisfy the request requirements. + */ + if (!match) { + StreamConfiguration streamConfiguration; + + /* + * \todo: The pixelFormat should be a 'best-fit' choice + * and may require a validation cycle. This is not yet + * handled, and should be considered as part of any + * stream configuration reworks. + */ + streamConfiguration.size.width = stream->width; + streamConfiguration.size.height = stream->height; + streamConfiguration.pixelFormat = formats::NV12; + + LOG(HAL, Info) << "Adding " << streamConfiguration.toString() + << " for MJPEG support"; + + config_->addConfiguration(streamConfiguration); + streams_[i].index = streamIndex++; + } } switch (config_->validate()) { @@ -1067,6 +1191,18 @@ int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list) /* Use the bufferCount confirmed by the validation process. */ stream->max_buffers = cfg.bufferCount; + + /* + * Construct a software compressor for MJPEG streams from the + * chosen libcamera source stream. + */ + if (cameraStream->format == formats::MJPEG) { + cameraStream->jpeg = new CompressorJPEG(); + cameraStream->jpeg->configure(cfg); + } else { + /* Either construct this correctly, or use a better interface */ + cameraStream->jpeg = nullptr; + } } /* @@ -1161,6 +1297,10 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques descriptor->buffers[i].stream = camera3Buffers[i].stream; descriptor->buffers[i].buffer = camera3Buffers[i].buffer; + /* Software streams are handled after hardware streams complete. */ + if (cameraStream->format == formats::MJPEG) + continue; + /* * Create a libcamera buffer using the dmabuf descriptors of * the camera3Buffer for each stream. The FrameBuffer is @@ -1217,8 +1357,76 @@ void CameraDevice::requestComplete(Request *request) resultMetadata = getResultMetadata(descriptor->frameNumber, buffer->metadata().timestamp); - /* Prepare to call back the Android camera stack. */ + /* Handle any JPEG compression */ + for (unsigned int i = 0; i < descriptor->numBuffers; ++i) { + CameraStream *cameraStream = + static_cast(descriptor->buffers[i].stream->priv); + + if (cameraStream->format != formats::MJPEG) + continue; + + Compressor *compressor = cameraStream->jpeg; + /* Only handle streams with compression enabled. */ + if (!compressor) + continue; + + StreamConfiguration *streamConfiguration = &config_->at(cameraStream->index); + Stream *stream = streamConfiguration->stream(); + FrameBuffer *buffer = request->findBuffer(stream); + if (!buffer) { + LOG(HAL, Error) << "Failed to find a source stream buffer"; + continue; + } + + MappedCamera3Buffer mapped(*descriptor->buffers[i].buffer, PROT_READ | PROT_WRITE); + if (!mapped.isValid()) { + LOG(HAL, Error) << "Failed to mmap android blob buffer"; + continue; + } + + CompressedImage output; + output.data = static_cast(mapped.maps()[0].data()); + output.length = mapped.maps()[0].size(); + + int ret = compressor->compress(buffer, &output); + if (ret) { + LOG(HAL, Error) << "Failed to compress stream image"; + status = CAMERA3_BUFFER_STATUS_ERROR; + } + /* + * Fill in the JPEG blob header. + * + * The mapped size of the buffer is being returned as + * substantially larger than the requested JPEG_MAX_SIZE + * (which is referenced from max_jpeg_buffer_size_). Utilise + * this static size to ensure the correct offset of the blob is + * determined. + * + * \todo Investigate if the buffer size mismatch is an issue or + * expected behaviour. + */ + uint8_t *resultPtr = mapped.maps()[0].data() + + max_jpeg_buffer_size_ - + sizeof(struct camera3_jpeg_blob); + auto *blob = reinterpret_cast(resultPtr); + blob->jpeg_blob_id = CAMERA3_JPEG_BLOB_ID; + blob->jpeg_size = output.length; + + /* Update the JPEG result Metadata. */ + resultMetadata->addEntry(ANDROID_JPEG_SIZE, + &output.length, 1); + + const uint32_t jpeg_quality = 95; + resultMetadata->addEntry(ANDROID_JPEG_QUALITY, + &jpeg_quality, 1); + + const uint32_t jpeg_orientation = 0; + resultMetadata->addEntry(ANDROID_JPEG_ORIENTATION, + &jpeg_orientation, 1); + } + + /* Prepare to call back the Android camera stack. */ camera3_capture_result_t captureResult = {}; captureResult.frame_number = descriptor->frameNumber; captureResult.num_output_buffers = descriptor->numBuffers; @@ -1298,10 +1506,10 @@ std::unique_ptr CameraDevice::getResultMetadata(int frame_number { /* * \todo Keep this in sync with the actual number of entries. - * Currently: 12 entries, 36 bytes + * Currently: 18 entries, 62 bytes */ std::unique_ptr resultMetadata = - std::make_unique(15, 50); + std::make_unique(18, 62); if (!resultMetadata->isValid()) { LOG(HAL, Error) << "Failed to allocate static metadata"; return nullptr; diff --git a/src/android/camera_device.h b/src/android/camera_device.h index 00472c219388..cfec5abeffa1 100644 --- a/src/android/camera_device.h +++ b/src/android/camera_device.h @@ -23,15 +23,25 @@ #include "libcamera/internal/log.h" #include "libcamera/internal/message.h" +#include "jpeg/compressor_jpeg.h" + class CameraMetadata; struct CameraStream { + CameraStream(libcamera::PixelFormat, libcamera::Size); + ~CameraStream(); + /* * The index of the libcamera StreamConfiguration as added during * configureStreams(). A single libcamera Stream may be used to deliver * one or more streams to the Android framework. */ unsigned int index; + + libcamera::PixelFormat format; + libcamera::Size size; + + CompressorJPEG *jpeg; }; class CameraDevice : protected libcamera::Loggable @@ -104,6 +114,8 @@ private: int facing_; int orientation_; + + unsigned int max_jpeg_buffer_size_; }; #endif /* __ANDROID_CAMERA_DEVICE_H__ */