From patchwork Mon Oct 26 09:53:55 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naushir Patuck X-Patchwork-Id: 10250 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 813CCBDB13 for ; Mon, 26 Oct 2020 09:54:01 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id E31C561E30; Mon, 26 Oct 2020 10:54:00 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=raspberrypi.com header.i=@raspberrypi.com header.b="LaE0/2oo"; dkim-atps=neutral Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id A3E146034D for ; Mon, 26 Oct 2020 10:53:59 +0100 (CET) Received: by mail-wm1-x32f.google.com with SMTP id c194so10953976wme.2 for ; Mon, 26 Oct 2020 02:53:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raspberrypi.com; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=CPeLxGSn9vdBOn7byMXWKovikSEmSr5cXSb8TH9kCYo=; b=LaE0/2ooCy9Pz7g7I3t3lw7nR3JZcgz7ZAzpm7XUyead/Zv6JLBCKN3ggKS26KRqTK fwmlERKzxGg1+KaHa8DVtD+GjbgDzOp//MPiKB1smdzg7U0/F0zTTCyvbVEmENwyFW8P F8HBJuE4wxuP+OoTzVsUcwtlvG0Fq4L8/zoTyunn3GIySh4dvFkWktF/oGKG6CLbEHDx gsWb/zU/kbQGqeJzrd53Huqn/XR6ki2sf0jBaS8rjYELhNBKAU5WFbnP5bKXpbEq9kGL 52mHkdNznLV3qmZroMLVM9fbfE8utjEvV1+Hy/HscZAbrRlSdNca6I9qR85mHsYtVVp6 dv7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=CPeLxGSn9vdBOn7byMXWKovikSEmSr5cXSb8TH9kCYo=; b=o+bttx6CFaFbCdtHJa5n+AoqPpWkWg/hDbzTnGygEj+NY2xDLbYoR1czkqkuO5Zk/p N3NJxNqIcmVhqe5zfD+J9Z10DoW157JFIGoKzPq8/HsvXpcb7mTuvYezAKeFYXib5VRK nVnNvLDxIiWFHOo1Pr66oQdnRlpygWxW1ExAi4DoHmVouwCJOGAtEk1jPQFIsedvNzpI Fshhol/WyLtekgO+d1SdKESNKOj5P7NXCZB2QMKz2LnbMR8PUTjFomMhSvv6UtsadESJ jjrA7ZylnQpsGNcDFNLBjeUz76n+x0eYsUaYZMQ4gDoqCnR5s1T0cqKVt7XdJmvb5Cgu auTw== X-Gm-Message-State: AOAM530KpvHDFjz9aH8znVu9zeMM/NnbY4OZWbOtfpb0c3G3BWWiaD0R spp3HxE8HUDl1Z6nsgc9g/6iFjE5bmn4Yw== X-Google-Smtp-Source: ABdhPJyx2AD76Lry/k01jSqQXGVPW9b9xjpMAow49OhiIrbPKflZf/mfvGfc3rwYMlk5qVkVcMCGfA== X-Received: by 2002:a1c:9a46:: with SMTP id c67mr14545645wme.115.1603706038905; Mon, 26 Oct 2020 02:53:58 -0700 (PDT) Received: from naushir-VirtualBox.pitowers.org ([2a00:1098:3142:14:a00:27ff:fe4d:f6a2]) by smtp.gmail.com with ESMTPSA id l8sm22157508wrn.28.2020.10.26.02.53.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Oct 2020 02:53:58 -0700 (PDT) From: Naushir Patuck To: libcamera-devel@lists.libcamera.org Date: Mon, 26 Oct 2020 09:53:55 +0000 Message-Id: <20201026095356.442605-1-naush@raspberrypi.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 1/2] pipeline: raspberrypi: Use MappedFrameBuffer for embedded data buffers 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 a MappedFrameBuffer to mmap embedded data buffers for the pipeline handler to use in the cases where the sensor does not fill it in. This avoids the need to mmap and unmap on every frame. Signed-off-by: Naushir Patuck Reviewed-by: Kieran Bingham --- .../pipeline/raspberrypi/raspberrypi.cpp | 35 ++++++++++++++----- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp index dd62dfc7..8817915b 100644 --- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp +++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp @@ -24,6 +24,7 @@ #include #include "libcamera/internal/bayer_format.h" +#include "libcamera/internal/buffer.h" #include "libcamera/internal/camera_sensor.h" #include "libcamera/internal/device_enumerator.h" #include "libcamera/internal/ipa_manager.h" @@ -165,6 +166,12 @@ public: /* Stores the ids of the buffers mapped in the IPA. */ std::unordered_set ipaBuffers_; + /* + * Map of (internal) mmaped embedded data buffers, to avoid having to + * map/unmap on every frame. + */ + std::map mappedEmbeddedBuffers_; + /* DMAHEAP allocation helper. */ RPi::DmaHeap dmaHeap_; FileDescriptor lsTable_; @@ -1040,6 +1047,15 @@ int PipelineHandlerRPi::prepareBuffers(Camera *camera) return ret; } + if (!data->sensorMetadata_) { + for (auto const &it : data->unicam_[Unicam::Embedded].getBuffers()) { + data->mappedEmbeddedBuffers_.emplace(std::piecewise_construct, + std::forward_as_tuple(it.first), + std::forward_as_tuple( + MappedFrameBuffer(it.second, PROT_READ | PROT_WRITE))); + } + } + /* * Pass the stats and embedded data buffers to the IPA. No other * buffers need to be passed. @@ -1078,6 +1094,7 @@ void PipelineHandlerRPi::freeBuffers(Camera *camera) std::vector ipaBuffers(data->ipaBuffers_.begin(), data->ipaBuffers_.end()); data->ipa_->unmapBuffers(ipaBuffers); data->ipaBuffers_.clear(); + data->mappedEmbeddedBuffers_.clear(); for (auto const stream : data->streams_) stream->releaseBuffers(); @@ -1310,14 +1327,16 @@ void RPiCameraData::unicamBufferDequeue(FrameBuffer *buffer) * metadata buffer. */ if (!sensorMetadata_) { - const FrameBuffer &fb = buffer->planes(); - uint32_t *mem = static_cast(::mmap(nullptr, fb.planes()[0].length, - PROT_READ | PROT_WRITE, - MAP_SHARED, - fb.planes()[0].fd.fd(), 0)); - mem[0] = ctrl[V4L2_CID_EXPOSURE]; - mem[1] = ctrl[V4L2_CID_ANALOGUE_GAIN]; - munmap(mem, fb.planes()[0].length); + unsigned int bufferId = unicam_[Unicam::Embedded].getBufferId(buffer); + auto it = mappedEmbeddedBuffers_.find(bufferId); + if (it != mappedEmbeddedBuffers_.end()) { + uint32_t *mem = reinterpret_cast(it->second.maps()[0].data()); + mem[0] = ctrl[V4L2_CID_EXPOSURE]; + mem[1] = ctrl[V4L2_CID_ANALOGUE_GAIN]; + } else { + LOG(RPI, Warning) << "Failed to find embedded buffer " + << bufferId; + } } } From patchwork Mon Oct 26 09:53:56 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naushir Patuck X-Patchwork-Id: 10251 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 2B2E7BDB13 for ; Mon, 26 Oct 2020 09:54:02 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 4668961E50; Mon, 26 Oct 2020 10:54:01 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=raspberrypi.com header.i=@raspberrypi.com header.b="ICcPY5mM"; dkim-atps=neutral Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 296986034D for ; Mon, 26 Oct 2020 10:54:00 +0100 (CET) Received: by mail-wr1-x42a.google.com with SMTP id h5so11623683wrv.7 for ; Mon, 26 Oct 2020 02:54:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raspberrypi.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=axdIkUOCwWKpcn72pibLJwnDUtNLUKVs1vvylM5oKSY=; b=ICcPY5mM6cTPkYU0PuAStn9u1irIbwn+FZ5jrc+UO1dYkhzA8/8Dx8im18m/i1+Sx2 x4XJ8ycT6TtSHEs5GENABfsE/Lh4vLTruo9yl9g/TnDeUbLetQEcgPjiiShSi0QlwGl7 zGcqroLiQK1EArRFWn6VQ5ZBWIsHMRAwx7OlclENLxt6OF0DWxO2b4PjfvCuxw/b1UTh Qjp9BKAmThuujT3lPiy2ErOwsywx/AM5oLlZSrswAAONu0DCFm4AVLCBf+trKgtu7c5R b/t4CfexK8rn0oeXO69fWOECnD5/GNhA8GXpveKY2JBcvdSsIF6SZU08tGsa63+UGSXa AAzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=axdIkUOCwWKpcn72pibLJwnDUtNLUKVs1vvylM5oKSY=; b=qbrFwM8BRFt8L66gCyfsAPN7JE/uMOjNvrDEQQBZkXq3MHj1nsXXuSdbsDTj60zjXw Ti/C3e8vfFv+ojAwk+9XJDMY7l1OhZIbHXvnIAFS2a3ffscc3f/oVgsHjVRENCdg96Lk qq82Iblj5po0jnyOWFjtYToxIzElFm3pifPSV6NL1sSyjAL6oWUeWAdm4rW8YXDSa4Sh fbI89LZDpVtkOzNe+hUJqxBymL1qlNKkwMeStzxvR4l5R5yW8DIkZykU9lm7G18BdzNa g66zZeoEO5iT/aUxJpsxwzOC70XI3P1ELRks9vLOGcsT/Rc9xmnOLBDGCJYeFsk+sHm/ 5Gdw== X-Gm-Message-State: AOAM530+pkr/PhcYm+G+PXYA9sLmdxDoqnszHfS5goEg8NgC4xBvkj7T 2OYkA9HgfJCL1OOUeIRItJJ6VPmDo5GdfQ== X-Google-Smtp-Source: ABdhPJx7vx+dNm9+xeiW8e8cZh/EpK5e6/cz6wQEDQ+zIOwr5tYJOaPW0C81uaxl5+Ysawce0kooLA== X-Received: by 2002:adf:c3cd:: with SMTP id d13mr16792964wrg.15.1603706039627; Mon, 26 Oct 2020 02:53:59 -0700 (PDT) Received: from naushir-VirtualBox.pitowers.org ([2a00:1098:3142:14:a00:27ff:fe4d:f6a2]) by smtp.gmail.com with ESMTPSA id l8sm22157508wrn.28.2020.10.26.02.53.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Oct 2020 02:53:59 -0700 (PDT) From: Naushir Patuck To: libcamera-devel@lists.libcamera.org Date: Mon, 26 Oct 2020 09:53:56 +0000 Message-Id: <20201026095356.442605-2-naush@raspberrypi.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201026095356.442605-1-naush@raspberrypi.com> References: <20201026095356.442605-1-naush@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 2/2] ipa: raspberrypi: Use MappedFrameBuffer for the IPA buffers 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" Instead of directly mmaping/unmapping buffers passed to the IPA, use a MappedFrameBuffer. The latter is a cleaner interface, and avoid code duplication. Signed-off-by: Naushir Patuck Reviewed-by: Kieran Bingham --- src/ipa/raspberrypi/raspberrypi.cpp | 43 ++++++++++++----------------- 1 file changed, 17 insertions(+), 26 deletions(-) diff --git a/src/ipa/raspberrypi/raspberrypi.cpp b/src/ipa/raspberrypi/raspberrypi.cpp index 1c255aa2..8d204082 100644 --- a/src/ipa/raspberrypi/raspberrypi.cpp +++ b/src/ipa/raspberrypi/raspberrypi.cpp @@ -24,6 +24,7 @@ #include +#include "libcamera/internal/buffer.h" #include "libcamera/internal/camera_sensor.h" #include "libcamera/internal/log.h" #include "libcamera/internal/utils.h" @@ -110,8 +111,7 @@ private: void applyLS(const struct AlscStatus *lsStatus, ControlList &ctrls); void resampleTable(uint16_t dest[], double const src[12][16], int destW, int destH); - std::map buffers_; - std::map buffersMemory_; + std::map buffers_; ControlInfoMap unicamCtrls_; ControlInfoMap ispCtrls_; @@ -319,31 +319,20 @@ void IPARPi::configure(const CameraSensorInfo &sensorInfo, void IPARPi::mapBuffers(const std::vector &buffers) { for (const IPABuffer &buffer : buffers) { - auto elem = buffers_.emplace(std::piecewise_construct, - std::forward_as_tuple(buffer.id), - std::forward_as_tuple(buffer.planes)); - const FrameBuffer &fb = elem.first->second; - - buffersMemory_[buffer.id] = mmap(nullptr, fb.planes()[0].length, - PROT_READ | PROT_WRITE, MAP_SHARED, - fb.planes()[0].fd.fd(), 0); - - if (buffersMemory_[buffer.id] == MAP_FAILED) { - int ret = -errno; - LOG(IPARPI, Fatal) << "Failed to mmap buffer: " << strerror(-ret); - } + const FrameBuffer fb = FrameBuffer(buffer.planes); + buffers_.emplace(std::piecewise_construct, + std::forward_as_tuple(buffer.id), + std::forward_as_tuple(MappedFrameBuffer(&fb, PROT_READ | PROT_WRITE))); } } void IPARPi::unmapBuffers(const std::vector &ids) { for (unsigned int id : ids) { - const auto fb = buffers_.find(id); - if (fb == buffers_.end()) + auto it = buffers_.find(id); + if (it == buffers_.end()) continue; - munmap(buffersMemory_[id], fb->second.planes()[0].length); - buffersMemory_.erase(id); buffers_.erase(id); } } @@ -785,15 +774,16 @@ void IPARPi::prepareISP(unsigned int bufferId) bool IPARPi::parseEmbeddedData(unsigned int bufferId, struct DeviceStatus &deviceStatus) { - auto it = buffersMemory_.find(bufferId); - if (it == buffersMemory_.end()) { + auto it = buffers_.find(bufferId); + if (it == buffers_.end()) { LOG(IPARPI, Error) << "Could not find embedded buffer!"; return false; } - int size = buffers_.find(bufferId)->second.planes()[0].length; + int size = buffers_.find(bufferId)->second.maps()[0].size(); + void *ptr = buffers_.find(bufferId)->second.maps()[0].data(); helper_->Parser().SetBufferSize(size); - RPiController::MdParser::Status status = helper_->Parser().Parse(it->second); + RPiController::MdParser::Status status = helper_->Parser().Parse(ptr); if (status != RPiController::MdParser::Status::OK) { LOG(IPARPI, Error) << "Embedded Buffer parsing failed, error " << status; } else { @@ -820,13 +810,14 @@ bool IPARPi::parseEmbeddedData(unsigned int bufferId, struct DeviceStatus &devic void IPARPi::processStats(unsigned int bufferId) { - auto it = buffersMemory_.find(bufferId); - if (it == buffersMemory_.end()) { + auto it = buffers_.find(bufferId); + if (it == buffers_.end()) { LOG(IPARPI, Error) << "Could not find stats buffer!"; return; } - bcm2835_isp_stats *stats = static_cast(it->second); + void *ptr = buffers_.find(bufferId)->second.maps()[0].data(); + bcm2835_isp_stats *stats = static_cast(ptr); RPiController::StatisticsPtr statistics = std::make_shared(*stats); controller_.Process(statistics, &rpiMetadata_);