From patchwork Mon Sep 6 22:56:28 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 13689 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 2CC4EBE175 for ; Mon, 6 Sep 2021 22:57:48 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id DE00569180; Tue, 7 Sep 2021 00:57:47 +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="IcYJjze+"; 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 C435F6917D for ; Tue, 7 Sep 2021 00:57:07 +0200 (CEST) Received: from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 5A184B75; Tue, 7 Sep 2021 00:57:07 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1630969027; bh=9LYXnKijRFq02UZEC0V6piQFmCHLvjmAkL0qY8FYUGk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IcYJjze+hTZoJet9CodqqPDcIPJ3XNAitZ2ZzRE+5qgTqDn2mPYF7Jil6Wyz+ZbvF sDn+WZnckQoXw+w079VqsGNAoVeAE6+1tfWj2HNXmQ5IHgJiZeTvaltB4zSSNUjDE7 YcPLjqdky96l4FDB3g39Pd6wTI5dN2ndumsTtvhU= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Tue, 7 Sep 2021 01:56:28 +0300 Message-Id: <20210906225636.14683-22-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210906225420.13275-1-laurent.pinchart@ideasonboard.com> References: <20210906225420.13275-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 22/30] cam: file_sink: Use Image class to access pixel data 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" Replace the manual implementation of frame buffer mapping with the Image class to improve code sharing. Signed-off-by: Laurent Pinchart Reviewed-by: Jean-Michel Hautbois Reviewed-by: Hirokazu Honda Reviewed-by: Kieran Bingham --- src/cam/file_sink.cpp | 42 +++++++++++++----------------------------- src/cam/file_sink.h | 6 ++++-- 2 files changed, 17 insertions(+), 31 deletions(-) diff --git a/src/cam/file_sink.cpp b/src/cam/file_sink.cpp index 0fc7d621f50b..3c2e565b27a2 100644 --- a/src/cam/file_sink.cpp +++ b/src/cam/file_sink.cpp @@ -5,17 +5,18 @@ * file_sink.cpp - File Sink */ +#include #include #include #include #include #include -#include #include #include #include "file_sink.h" +#include "image.h" using namespace libcamera; @@ -26,12 +27,6 @@ FileSink::FileSink(const std::string &pattern) FileSink::~FileSink() { - for (auto &iter : mappedBuffers_) { - void *memory = iter.second.first; - unsigned int length = iter.second.second; - munmap(memory, length); - } - mappedBuffers_.clear(); } int FileSink::configure(const libcamera::CameraConfiguration &config) @@ -51,23 +46,11 @@ int FileSink::configure(const libcamera::CameraConfiguration &config) void FileSink::mapBuffer(FrameBuffer *buffer) { - /* \todo use MappedFrameBuffer. */ - for (const FrameBuffer::Plane &plane : buffer->planes()) { - const int fd = plane.fd.fd(); - if (mappedBuffers_.find(fd) == mappedBuffers_.end()) { - /** - * \todo Should we try to only map the portions of the - * dmabuf that are used by planes ? - */ - size_t length = lseek(fd, 0, SEEK_END); - void *memory = mmap(NULL, plane.length, PROT_READ, - MAP_SHARED, fd, 0); - mappedBuffers_[fd] = std::make_pair(memory, length); - } + std::unique_ptr image = + Image::fromFrameBuffer(buffer, Image::MapMode::ReadOnly); + assert(image != nullptr); - void *memory = mappedBuffers_[fd].first; - planeData_[&plane] = static_cast(memory) + plane.offset; - } + mappedBuffers_[buffer] = std::move(image); } bool FileSink::processRequest(Request *request) @@ -108,19 +91,20 @@ void FileSink::writeBuffer(const Stream *stream, FrameBuffer *buffer) return; } + Image *image = mappedBuffers_[buffer].get(); + for (unsigned int i = 0; i < buffer->planes().size(); ++i) { - const FrameBuffer::Plane &plane = buffer->planes()[i]; const FrameMetadata::Plane &meta = buffer->metadata().planes()[i]; - uint8_t *data = planeData_[&plane]; - unsigned int length = std::min(meta.bytesused, plane.length); + Span data = image->data(i); + unsigned int length = std::min(meta.bytesused, data.size()); - if (meta.bytesused > plane.length) + if (meta.bytesused > data.size()) std::cerr << "payload size " << meta.bytesused - << " larger than plane size " << plane.length + << " larger than plane size " << data.size() << std::endl; - ret = ::write(fd, data, length); + ret = ::write(fd, data.data(), length); if (ret < 0) { ret = -errno; std::cerr << "write error: " << strerror(-ret) diff --git a/src/cam/file_sink.h b/src/cam/file_sink.h index c12325d955c5..335be93b8732 100644 --- a/src/cam/file_sink.h +++ b/src/cam/file_sink.h @@ -8,12 +8,15 @@ #define __CAM_FILE_SINK_H__ #include +#include #include #include #include "frame_sink.h" +class Image; + class FileSink : public FrameSink { public: @@ -32,8 +35,7 @@ private: std::map streamNames_; std::string pattern_; - std::map> mappedBuffers_; - std::map planeData_; + std::map> mappedBuffers_; }; #endif /* __CAM_FILE_SINK_H__ */