From patchwork Mon Nov 16 14:37: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: 10427 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 8834ABE082 for ; Mon, 16 Nov 2020 14:38:08 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 07614632C4; Mon, 16 Nov 2020 15:38:08 +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="TN7wr1Da"; dkim-atps=neutral Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id DE652631B5 for ; Mon, 16 Nov 2020 15:38:06 +0100 (CET) Received: by mail-wr1-x42b.google.com with SMTP id j7so18932889wrp.3 for ; Mon, 16 Nov 2020 06:38:06 -0800 (PST) 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=TParc3S4s8nqUndm88K8cu4/b3BgYUNUUCOTsxzyBco=; b=TN7wr1DaB4/CCl5y0m6CvW+Y8CrXAcUrTyKKrTeIhRZT3nHn4CBEqX6Pj158P9I2Nw cqhJ0JGyOEXqEziTujlBoVcs9hyJHchjIrLrwomGm689AV/TlzzwA4OJkMvMDG7IH/jZ RQcGcALmM4STgz7pbRxd5b6VGusEhNu8jICQ/CLlmrM6vLInicUjOx4R4l5ogCeB5ll2 yd61vwUx2kGSv8rrl65S/l9J/RddNaB9tVbqTTOs0t0owQ0tqPzrfOi6g3mYeI4CKIi4 xH146IvgQB3dwDRLyLlCf36oEd3g/LwL/VewKRoNDM42SqXSLepru+HkeNflvQ2es5/W hkeg== 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=TParc3S4s8nqUndm88K8cu4/b3BgYUNUUCOTsxzyBco=; b=Xhr1MUc4elsry74grtJKpB61M57Ag8pCp207s6ziUTxx4Lokl1LsTUyirvppAVYjqs 5en3liayGlk45ssYGg7E+vxwdKjtgR6ZlULQaqkW8OM7mq9tfStsVbmYesiWUZ6/ZL31 KwzQEDPtUC09VkFquQ9pLArFUNT1NvOpMTOScrdPuq6DVBY6DB3m/+dM7qYuuRgvnAp+ 6u+ftf+2rNnDArBbl30h5+i1gtiIFF5GCiU/AJOkOAy+vffKzZzMeN/Lz1Qw/f+E4NDR S1imnZMqDHkuSj/ZmK/bqPARS9sBgjKmUhQifk2qmCqliYaqUIf/QzFA6oUW8RcVE1zv /Vpw== X-Gm-Message-State: AOAM533UGsA1dBLGYR6h9oH4lyilDSzAr36XNUSb2u0e3Sc17XibZkZG U/9H6vjxEGkYmUaEMrEFpWncHAQ60ykajw== X-Google-Smtp-Source: ABdhPJwc2a/jncu+AIejsnSbiMWje6ok/xQpYZaSjbD8wkgG17lqjCsmj3fSSNs4PtXe6nX03viznA== X-Received: by 2002:adf:fb06:: with SMTP id c6mr20639406wrr.117.1605537486040; Mon, 16 Nov 2020 06:38:06 -0800 (PST) Received: from naushir-VirtualBox.patuck.local ([88.97.76.4]) by smtp.gmail.com with ESMTPSA id j4sm22820810wrn.83.2020.11.16.06.38.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Nov 2020 06:38:05 -0800 (PST) From: Naushir Patuck To: libcamera-devel@lists.libcamera.org Date: Mon, 16 Nov 2020 14:37:56 +0000 Message-Id: <20201116143757.155890-1-naush@raspberrypi.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 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 Reviewed-by: Laurent Pinchart --- .../pipeline/raspberrypi/raspberrypi.cpp | 33 ++++++++++++++----- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp index dd62dfc7..9fe5eb05 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,13 @@ int PipelineHandlerRPi::prepareBuffers(Camera *camera) return ret; } + if (!data->sensorMetadata_) { + for (auto const &it : data->unicam_[Unicam::Embedded].getBuffers()) { + MappedFrameBuffer fb(it.second, PROT_READ | PROT_WRITE); + data->mappedEmbeddedBuffers_.emplace(it.first, std::move(fb)); + } + } + /* * Pass the stats and embedded data buffers to the IPA. No other * buffers need to be passed. @@ -1078,6 +1092,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 +1325,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 Nov 16 14:37:57 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naushir Patuck X-Patchwork-Id: 10428 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 89061BE082 for ; Mon, 16 Nov 2020 14:38:11 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 58DA2632C3; Mon, 16 Nov 2020 15:38:11 +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="bA+QTwVO"; dkim-atps=neutral Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id DCEE5632C1 for ; Mon, 16 Nov 2020 15:38:07 +0100 (CET) Received: by mail-wm1-x32c.google.com with SMTP id s13so23814618wmh.4 for ; Mon, 16 Nov 2020 06:38:07 -0800 (PST) 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=8I6DT3x0SsFQNXMnrPiAb9GclErL8X7Bhpo4v3V43Qc=; b=bA+QTwVO5pmiBt3FKUG3/Vk0sqH4nHJxAQ4WyCRjzLyHX0yh7vvWEHCailJhgllRvP JLcSR7S0Jk0fhXdrRI6HksgDOvz7wtuklZ/+O1Sol1h/OoRKxn1A7yyRRJpGqFaB5SCp sqQvnM6jt252qOS9XAJmMkiYEWrWSMM7kl2GlAgW1qKA/krClQr/Wxoxn3g6ErqyMUmo YmPLOFUD4NLD5DsEzEegBrUsnFh81SfxrDe+kNCjgwCgc6YX1aGt2QyWg+MtOB6iJsgz 5C/tH1dTxb30avZ6lmZLz0HjfXoVbVDu7ANG+tkKwp7yhM6PvBeE+bE7+/sWm8UIPrqP n4xQ== 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=8I6DT3x0SsFQNXMnrPiAb9GclErL8X7Bhpo4v3V43Qc=; b=DSOZEKMQUYbF6wGcB2LsOow1/haG8tif7G5ZybyXiyQlYkwegNWAYM5JoC6U/97c2U 12sxRTU7e7BNZlQ2LHNrcYUBQ10WPJdCgpo5Kfnc8lYeAqnFQmyZ6iUSR5J7xxiYAG5x 8ZnBqoh/6OB9NQSKfM68EytdltAn0L9LYMMvuw00P3V+hJjcxErJ8IWU9auMzMN5Li0Z TM+kd1CqrrC16EU8Jl/NVPzv1r0TLKZ8BiQ0N3C4RwN5leZRQqoZ4NrQ2RdYl7qOu6tl pyv96KNJUBktpZ2ywdL2c3dWi/jY+QXUUNwCA8SToxTqYt5sjQS2rDqgoKow3xlNUiD3 P50A== X-Gm-Message-State: AOAM533Iyop2UQKPe3shhKfNJglqaAb2QiCLdhLErM4BFyLMaaCMpKaV /EJjg9kM3bAp0gD12IUjeMiFFaqOtPQ2Hw== X-Google-Smtp-Source: ABdhPJyKjB2GKoOkzCwLm+7uSmUyWdlYN07RrqVMJBm74mMyXC1WoTOM7y/+xrVJF0FEHQYls23+0w== X-Received: by 2002:a1c:103:: with SMTP id 3mr15534747wmb.81.1605537487294; Mon, 16 Nov 2020 06:38:07 -0800 (PST) Received: from naushir-VirtualBox.patuck.local ([88.97.76.4]) by smtp.gmail.com with ESMTPSA id j4sm22820810wrn.83.2020.11.16.06.38.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Nov 2020 06:38:06 -0800 (PST) From: Naushir Patuck To: libcamera-devel@lists.libcamera.org Date: Mon, 16 Nov 2020 14:37:57 +0000 Message-Id: <20201116143757.155890-2-naush@raspberrypi.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201116143757.155890-1-naush@raspberrypi.com> References: <20201116143757.155890-1-naush@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 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 Reviewed-by: Laurent Pinchart --- src/ipa/raspberrypi/raspberrypi.cpp | 42 +++++++++++------------------ 1 file changed, 15 insertions(+), 27 deletions(-) diff --git a/src/ipa/raspberrypi/raspberrypi.cpp b/src/ipa/raspberrypi/raspberrypi.cpp index 1c255aa2..ac24d5ef 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,18 @@ 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(buffer.planes); + buffers_.emplace(buffer.id, 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 +772,15 @@ 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; - helper_->Parser().SetBufferSize(size); - RPiController::MdParser::Status status = helper_->Parser().Parse(it->second); + Span mem = it->second.maps()[0]; + helper_->Parser().SetBufferSize(mem.size()); + RPiController::MdParser::Status status = helper_->Parser().Parse(mem.data()); if (status != RPiController::MdParser::Status::OK) { LOG(IPARPI, Error) << "Embedded Buffer parsing failed, error " << status; } else { @@ -820,13 +807,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); + Span mem = it->second.maps()[0]; + bcm2835_isp_stats *stats = reinterpret_cast(mem.data()); RPiController::StatisticsPtr statistics = std::make_shared(*stats); controller_.Process(statistics, &rpiMetadata_);