From patchwork Mon Jul 20 22:42:25 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 8893 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 1B6BDC2E67 for ; Mon, 20 Jul 2020 22:42:41 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 56A8A605C6; Tue, 21 Jul 2020 00:42:39 +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="Plh3sJek"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 633CD60554 for ; Tue, 21 Jul 2020 00:42:37 +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 D0B6F563; Tue, 21 Jul 2020 00:42:36 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1595284957; bh=d1o20GUpXdd+g4xj2BYIhLaUMX9Oe9GZzEWjnPvXOe0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Plh3sJekRKDwB3zh4HuVdiu6eRFWPMAIHzkHWkhkcCDREpbc2NAUJyYrVh9w2ru/O FCLolSZWV1KnR8S7eBklcAp56f+8XQuVjwP4HP96GC0XkVzV4MNL1mHObs0UOwT043 k02c7OhC/XE9r57nsZf992R/PxdyftiSn5C6yTlk= From: Kieran Bingham To: libcamera devel Date: Mon, 20 Jul 2020 23:42:25 +0100 Message-Id: <20200720224232.153717-2-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 1/8] libcamera: buffer: Create a 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" Provide a MappedFrameBuffer helper class which will map all of the Planes within a FrameBuffer and provide CPU addressable pointers for those planes. Mappings are removed upon destruction. Signed-off-by: Kieran Bingham --- This introduces an automatically mapping/unmapping MappedFrameBuffer object, with a move constructor (essential to prevent un-desirable unmapping). include/libcamera/buffer.h | 23 ++++++++++++++++++++ src/libcamera/buffer.cpp | 43 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/include/libcamera/buffer.h b/include/libcamera/buffer.h index 6bb2e4f8558f..881d736da2db 100644 --- a/include/libcamera/buffer.h +++ b/include/libcamera/buffer.h @@ -71,6 +71,29 @@ private: unsigned int cookie_; }; +class MappedFrameBuffer { +public: + MappedFrameBuffer(const FrameBuffer *buffer, int flags); + ~MappedFrameBuffer(); + + /* Move constructor only, copying is not permitted. */ + MappedFrameBuffer(MappedFrameBuffer &&rhs); + + struct MappedPlane { + void *address; + size_t length; + }; + + bool isValid() const { return valid_; } + int error() const { return error_; } + const std::vector &maps() const { return maps_; } + +private: + int error_; + bool valid_; + std::vector maps_; +}; + } /* namespace libcamera */ #endif /* __LIBCAMERA_BUFFER_H__ */ diff --git a/src/libcamera/buffer.cpp b/src/libcamera/buffer.cpp index 1a1d4bac7aed..28b43d937f57 100644 --- a/src/libcamera/buffer.cpp +++ b/src/libcamera/buffer.cpp @@ -290,4 +290,47 @@ int FrameBuffer::copyFrom(const FrameBuffer *src) return 0; } +/** + * \brief Map all planes of a FrameBuffer + * \param[in] src Buffer 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) + : error_(0) +{ + 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"; + continue; + } + + maps_.push_back({address, plane.length}); + } + + 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 */