From patchwork Mon Jul 20 22:42:28 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 8896 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 3D11AC0109 for ; Mon, 20 Jul 2020 22:42:43 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 5695760890; Tue, 21 Jul 2020 00:42:40 +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="SPkAmrb5"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 60DBC605BB for ; Tue, 21 Jul 2020 00:42:38 +0200 (CEST) Received: from localhost.localdomain (cpc89242-aztw30-2-0-cust488.18-1.cable.virginm.net [86.31.129.233]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id E29122A4; Tue, 21 Jul 2020 00:42:37 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1595284958; bh=Ao/ddpZ6hPuJ7yXhHH3kjZ7OtE5OS8ZtmY1Lkks+YPM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SPkAmrb5AlGw3nalq1T0WNOtyaWGpFTF+2g8+SiPBRfrC6/gXwo8PZE8ZZugJzGQ2 6XJh8zrrkf4UtUlJTJ+5A1T5sieaxZ+CnbWG2zl2+MJfRPDDoG/csPNo9mXKdT9ELp 6bfqsTPcDRlvdBi1SR80UHvybH+s9jx8WkBAvpEw= From: Kieran Bingham To: libcamera devel Date: Mon, 20 Jul 2020 23:42:28 +0100 Message-Id: <20200720224232.153717-5-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200720224232.153717-1-kieran.bingham@ideasonboard.com> References: <20200720224232.153717-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [RFC PATCH 4/8] buffer: Provide a base MappedBuffer class 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" Move the interface of the MappedFrameBuffer to a MappedBuffer class to provide a common interface of mapped buffers from different sources. This allows MappedBuffer instances to be created from Camera3Buffer types. Signed-off-by: Kieran Bingham --- Now we can create our new base class, the MappedBuffer. I can see this going further to also push the mmap down to a MappedDmaBuf, but that can be on top/later or if this general automatic mapping approach is desired at all.... include/libcamera/buffer.h | 16 +++++--- src/libcamera/buffer.cpp | 80 +++++++++++++++++++++++++++++++------- 2 files changed, 76 insertions(+), 20 deletions(-) diff --git a/include/libcamera/buffer.h b/include/libcamera/buffer.h index 881d736da2db..558204e5a09b 100644 --- a/include/libcamera/buffer.h +++ b/include/libcamera/buffer.h @@ -71,13 +71,12 @@ private: unsigned int cookie_; }; -class MappedFrameBuffer { +class MappedBuffer { public: - MappedFrameBuffer(const FrameBuffer *buffer, int flags); - ~MappedFrameBuffer(); - + MappedBuffer(); /* Move constructor only, copying is not permitted. */ - MappedFrameBuffer(MappedFrameBuffer &&rhs); + MappedBuffer(MappedBuffer &&rhs); + ~MappedBuffer(); struct MappedPlane { void *address; @@ -88,12 +87,17 @@ public: int error() const { return error_; } const std::vector &maps() const { return maps_; } -private: +protected: int error_; bool valid_; std::vector maps_; }; +class MappedFrameBuffer : public MappedBuffer { +public: + MappedFrameBuffer(const FrameBuffer *buffer, int flags); +}; + } /* namespace libcamera */ #endif /* __LIBCAMERA_BUFFER_H__ */ diff --git a/src/libcamera/buffer.cpp b/src/libcamera/buffer.cpp index af5345b91195..0fe12ab93b58 100644 --- a/src/libcamera/buffer.cpp +++ b/src/libcamera/buffer.cpp @@ -281,6 +281,72 @@ int FrameBuffer::copyFrom(const FrameBuffer *src) return 0; } +/** + * \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 Move constructor, create 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) +{ + error_ = rhs.error_; + valid_ = rhs.valid_; + maps_ = std::move(rhs.maps_); + rhs.valid_ = false; + rhs.error_ = 0; +} + +MappedBuffer::~MappedBuffer() +{ + for (MappedPlane map : maps_) + munmap(map.address, map.length); +} + +/** + * \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. + */ + +/** + * \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 + */ + /** * \brief Map all planes of a FrameBuffer * \param[in] src Buffer to be mapped @@ -291,7 +357,6 @@ int FrameBuffer::copyFrom(const FrameBuffer *src) * PROT_WRITE, or a bitwise-or combination of both. */ MappedFrameBuffer::MappedFrameBuffer(const FrameBuffer *buffer, int flags) - : error_(0) { maps_.reserve(buffer->planes().size()); @@ -311,17 +376,4 @@ MappedFrameBuffer::MappedFrameBuffer(const FrameBuffer *buffer, int flags) valid_ = buffer->planes().size() == maps_.size(); } -MappedFrameBuffer::~MappedFrameBuffer() -{ - for (MappedPlane map : maps_) - munmap(map.address, map.length); -} - -MappedFrameBuffer::MappedFrameBuffer(MappedFrameBuffer &&rhs) -{ - error_ = rhs.error_; - valid_ = rhs.valid_; - maps_ = std::move(rhs.maps_); -} - } /* namespace libcamera */