From patchwork Mon Mar 16 02:41:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Niklas_S=C3=B6derlund?= X-Patchwork-Id: 3113 X-Patchwork-Delegate: niklas.soderlund@ragnatech.se Return-Path: Received: from bin-mail-out-05.binero.net (bin-mail-out-05.binero.net [195.74.38.228]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 5BD1760417 for ; Mon, 16 Mar 2020 03:42:33 +0100 (CET) X-Halon-ID: b7223de9-672f-11ea-9f40-0050569116f7 Authorized-sender: niklas@soderlund.pp.se Received: from bismarck.berto.se (p4fca2392.dip0.t-ipconnect.de [79.202.35.146]) by bin-vsp-out-03.atm.binero.net (Halon) with ESMTPA id b7223de9-672f-11ea-9f40-0050569116f7; Mon, 16 Mar 2020 03:42:02 +0100 (CET) From: =?utf-8?q?Niklas_S=C3=B6derlund?= To: libcamera-devel@lists.libcamera.org Date: Mon, 16 Mar 2020 03:41:44 +0100 Message-Id: <20200316024146.2474424-5-niklas.soderlund@ragnatech.se> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200316024146.2474424-1-niklas.soderlund@ragnatech.se> References: <20200316024146.2474424-1-niklas.soderlund@ragnatech.se> MIME-Version: 1.0 Subject: [libcamera-devel] [RFC 4/6] libcamera: buffer: Add helper to memcopy a FrameBuffer 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: , X-List-Received-Date: Mon, 16 Mar 2020 02:42:33 -0000 This helper may be used to memory copy a while FrameBuffer content to another buffer. The operation is not fast and should not be used without grate care by pipelines. The intended use-case is to have an option to copy out RAW buffers from the middle of an pipeline. Signed-off-by: Niklas Söderlund --- include/libcamera/buffer.h | 1 + src/libcamera/buffer.cpp | 43 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/include/libcamera/buffer.h b/include/libcamera/buffer.h index 8e5ec699e3925eee..669d2591a90e5f37 100644 --- a/include/libcamera/buffer.h +++ b/include/libcamera/buffer.h @@ -60,6 +60,7 @@ public: private: friend class Request; /* Needed to update request_. */ friend class V4L2VideoDevice; /* Needed to update metadata_. */ + friend int FrameBufferMemCopy(FrameBuffer *destination, const FrameBuffer *source); std::vector planes_; diff --git a/src/libcamera/buffer.cpp b/src/libcamera/buffer.cpp index 673a63d3d1658190..f680d1879b57a68b 100644 --- a/src/libcamera/buffer.cpp +++ b/src/libcamera/buffer.cpp @@ -211,4 +211,47 @@ FrameBuffer::FrameBuffer(const std::vector &planes, unsigned int cookie) * core never modifies the buffer cookie. */ +int FrameBufferMemCopy(FrameBuffer *dst, const FrameBuffer *src) +{ + if (dst->planes_.size() != src->planes_.size()) { + LOG(Buffer, Error) << "Different number of planes"; + return -EINVAL; + } + + for (unsigned int i = 0; i < dst->planes_.size(); i++) { + if (dst->planes_[i].length < src->planes_[i].length) { + LOG(Buffer, Error) << "Plane " << i << " is too small"; + return -EINVAL; + } + } + + for (unsigned int i = 0; i < dst->planes_.size(); i++) { + void *out = mmap(NULL, dst->planes_[i].length, PROT_WRITE, MAP_SHARED, + dst->planes_[i].fd.fd(), 0); + + if (out == MAP_FAILED) { + LOG(Buffer, Error) << "Failed to map output plane " << i; + return -EINVAL; + } + + void *in = mmap(NULL, src->planes_[i].length, PROT_READ, MAP_SHARED, + src->planes_[i].fd.fd(), 0); + + if (in == MAP_FAILED) { + munmap(out, dst->planes_[i].length); + LOG(Buffer, Error) << "Failed to map input plane " << i; + return -EINVAL; + } + + memcpy(out, in, src->planes_[i].length); + + munmap(in, src->planes_[i].length); + munmap(out, dst->planes_[i].length); + } + + dst->metadata_ = src->metadata_; + + return 0; +} + } /* namespace libcamera */