From patchwork Fri Sep 18 09:42:31 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Naushir Patuck X-Patchwork-Id: 9655 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 CD8FAC3B5B for ; Fri, 18 Sep 2020 09:44:08 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 99E7162FCD; Fri, 18 Sep 2020 11:44:08 +0200 (CEST) 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="dFh78i+M"; dkim-atps=neutral Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id AC5C062FCD for ; Fri, 18 Sep 2020 11:44:05 +0200 (CEST) Received: by mail-wr1-x42c.google.com with SMTP id w5so4935075wrp.8 for ; Fri, 18 Sep 2020 02:44:05 -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=hxq5IjBzS+WFtXE8uRz4DB3Vof4h0VUl6+n57VaVinw=; b=dFh78i+MVzoYpWElAuZu6fWI6gsk9HLfyeXxpY2EJoqZLfayccypX2S19SgSQKcZc/ 465piy5df0rLZMFo01YPk1j+/KMiPCuQ4cRpZcOOExwp1gensUM0qCT3fNZNnt7ojz7k 5gPgVkiYTPj9N2XcfQGLhp3c7t8T5xAoi3L+5fM6LlvZgKE9R09jDEXvFU/JQhKl3Bwx r3KTKHqfwW0REIaFMdoMO2zAQNIfX0PFFe82enxYhTeWSSqP+SMBKJu8iWmcAbGxqEol a0Ro/xWaxhQfv6jEnoN6MIQJYaqkg7aLRw7A3j6+w/kVLsiXrViTtmQUJW15txIHbHZ6 vszw== 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=hxq5IjBzS+WFtXE8uRz4DB3Vof4h0VUl6+n57VaVinw=; b=LgSLpp9zPYrBcmk83G5E0BwLRNQ8AcW/3VSOo2VdrfXGUYV53Pa51260VnEts8NB3Z A0Q1Xd15aAQ5G026EkjFzo90T9vz6zdPFY/R1NmwwHBzGNH6KPuprMaaQ1xXRfppRftV /3I4pRPiHHBOtcm0iSTwKsRsmBYaeQ89ZecsoHfLprRtHWYFgxMZ719aer+VMZ7YhMMU fexi+8w66UJou6VhfKH2BHJuK3R7kG4n/yw4jA936ClaC+60R38rMq05/iIvvGyJH4WA 3/3sM90uagyOk0FemMnvzH1bNweULz/2CEdVlHd5yIdm8H4D2VONFY063gFL51nRPy6Z Ushw== X-Gm-Message-State: AOAM532BOLqnGBhuV7BIbLu4ef1V3RGTkwoimsCjX0nmfHGs6YtUOBhT 3VYzn25Brcr3rdyaIPiNmFeIZbvNphu8ew== X-Google-Smtp-Source: ABdhPJzJMD4BF/TArLXjKhYW3WRr61qAfTMefLr+AVVcFe/AcIgoQvR8cjDIzQiIrGf47WRVlrvIeg== X-Received: by 2002:adf:d4c7:: with SMTP id w7mr37494836wrk.263.1600422244922; Fri, 18 Sep 2020 02:44:04 -0700 (PDT) Received: from naushir-VirtualBox.pitowers.org ([2a00:1098:3142:14:a00:27ff:fe4d:f6a2]) by smtp.gmail.com with ESMTPSA id n2sm4792472wma.29.2020.09.18.02.44.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Sep 2020 02:44:03 -0700 (PDT) From: Naushir Patuck To: libcamera-devel@lists.libcamera.org Date: Fri, 18 Sep 2020 10:42:31 +0100 Message-Id: <20200918094233.5273-11-naush@raspberrypi.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200918094233.5273-1-naush@raspberrypi.com> References: <20200918094233.5273-1-naush@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v8 10/12] pipeline: raspberrypi: Use an unordered_map for the stream buffer list 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" By using a map container, we can easily insert/remove buffers from the buffer list. This will be required to track buffers allocated externally and passed to the pipeline handler through a Request. Replace the buffer index tracking with an id generated internally by the stream object. Signed-off-by: Naushir Patuck Reviewed-by: Kieran Bingham Reviewed-by: Kieran Bingham Reviewed-by: Niklas Söderlund --- .../pipeline/raspberrypi/raspberrypi.cpp | 31 +++++------ .../pipeline/raspberrypi/rpi_stream.cpp | 32 ++++++----- .../pipeline/raspberrypi/rpi_stream.h | 54 +++++++++++++++++-- 3 files changed, 82 insertions(+), 35 deletions(-) diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp index 7d91188c..51544233 100644 --- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp +++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp @@ -890,7 +890,6 @@ int PipelineHandlerRPi::queueAllBuffers(Camera *camera) int PipelineHandlerRPi::prepareBuffers(Camera *camera) { RPiCameraData *data = cameraData(camera); - unsigned int index; int ret; /* @@ -917,18 +916,14 @@ int PipelineHandlerRPi::prepareBuffers(Camera *camera) * This will allow us to identify buffers passed between the pipeline * handler and the IPA. */ - index = 0; for (auto const &b : data->isp_[Isp::Stats].getBuffers()) { - data->ipaBuffers_.push_back({ .id = RPiIpaMask::STATS | index, - .planes = b->planes() }); - index++; + data->ipaBuffers_.push_back({ .id = RPiIpaMask::STATS | b.first, + .planes = b.second->planes() }); } - index = 0; for (auto const &b : data->unicam_[Unicam::Embedded].getBuffers()) { - data->ipaBuffers_.push_back({ .id = RPiIpaMask::EMBEDDED_DATA | index, - .planes = b->planes() }); - index++; + data->ipaBuffers_.push_back({ .id = RPiIpaMask::EMBEDDED_DATA | b.first, + .planes = b.second->planes() }); } data->ipa_->mapBuffers(data->ipaBuffers_); @@ -1127,7 +1122,7 @@ void RPiCameraData::unicamBufferDequeue(FrameBuffer *buffer) return; for (RPi::RPiStream &s : unicam_) { - index = s.getBufferIndex(buffer); + index = s.getBufferId(buffer); if (index != -1) { stream = &s; break; @@ -1178,7 +1173,7 @@ void RPiCameraData::ispInputDequeue(FrameBuffer *buffer) return; LOG(RPI, Debug) << "Stream ISP Input buffer complete" - << ", buffer id " << unicam_[Unicam::Image].getBufferIndex(buffer) + << ", buffer id " << unicam_[Unicam::Image].getBufferId(buffer) << ", timestamp: " << buffer->metadata().timestamp; /* The ISP input buffer gets re-queued into Unicam. */ @@ -1195,7 +1190,7 @@ void RPiCameraData::ispOutputDequeue(FrameBuffer *buffer) return; for (RPi::RPiStream &s : isp_) { - index = s.getBufferIndex(buffer); + index = s.getBufferId(buffer); if (index != -1) { stream = &s; break; @@ -1436,16 +1431,16 @@ void RPiCameraData::tryRunPipeline() /* Set our state to say the pipeline is active. */ state_ = State::Busy; - unsigned int bayerIndex = unicam_[Unicam::Image].getBufferIndex(bayerBuffer); - unsigned int embeddedIndex = unicam_[Unicam::Embedded].getBufferIndex(embeddedBuffer); + unsigned int bayerId = unicam_[Unicam::Image].getBufferId(bayerBuffer); + unsigned int embeddedId = unicam_[Unicam::Embedded].getBufferId(embeddedBuffer); LOG(RPI, Debug) << "Signalling RPI_IPA_EVENT_SIGNAL_ISP_PREPARE:" - << " Bayer buffer id: " << bayerIndex - << " Embedded buffer id: " << embeddedIndex; + << " Bayer buffer id: " << bayerId + << " Embedded buffer id: " << embeddedId; op.operation = RPI_IPA_EVENT_SIGNAL_ISP_PREPARE; - op.data = { RPiIpaMask::EMBEDDED_DATA | embeddedIndex, - RPiIpaMask::BAYER_DATA | bayerIndex }; + op.data = { RPiIpaMask::EMBEDDED_DATA | embeddedId, + RPiIpaMask::BAYER_DATA | bayerId }; ipa_->processEvent(op); } diff --git a/src/libcamera/pipeline/raspberrypi/rpi_stream.cpp b/src/libcamera/pipeline/raspberrypi/rpi_stream.cpp index 80170d64..aee0aa2d 100644 --- a/src/libcamera/pipeline/raspberrypi/rpi_stream.cpp +++ b/src/libcamera/pipeline/raspberrypi/rpi_stream.cpp @@ -44,26 +44,28 @@ bool RPiStream::isExternal() const void RPiStream::setExportedBuffers(std::vector> *buffers) { - std::transform(buffers->begin(), buffers->end(), std::back_inserter(bufferList_), - [](std::unique_ptr &b) { return b.get(); }); + for (auto const &buffer : *buffers) + bufferMap_.emplace(id_.get(), buffer.get()); } -const std::vector &RPiStream::getBuffers() const +const BufferMap &RPiStream::getBuffers() const { - return bufferList_; + return bufferMap_; } -int RPiStream::getBufferIndex(FrameBuffer *buffer) const +int RPiStream::getBufferId(FrameBuffer *buffer) const { if (importOnly_) return -1; - /* Find the buffer in the list, and return the index position. */ - auto it = std::find(bufferList_.begin(), bufferList_.end(), buffer); - if (it != bufferList_.end()) - return std::distance(bufferList_.begin(), it); + /* Find the buffer in the map, and return the buffer id. */ + auto it = std::find_if(bufferMap_.begin(), bufferMap_.end(), + [&buffer](auto const &p) { return p.second == buffer; }); - return -1; + if (it == bufferMap_.end()) + return -1; + + return it->first; } int RPiStream::prepareBuffers(unsigned int count) @@ -86,7 +88,7 @@ int RPiStream::prepareBuffers(unsigned int count) } /* We must import all internal/external exported buffers. */ - count = bufferList_.size(); + count = bufferMap_.size(); } return dev_->importBuffers(count); @@ -139,6 +141,9 @@ void RPiStream::returnBuffer(FrameBuffer *buffer) /* Push this buffer back into the queue to be used again. */ availableBuffers_.push(buffer); + /* Allow the buffer id to be reused. */ + id_.release(getBufferId(buffer)); + /* * Do we have any Request buffers that are waiting to be queued? * If so, do it now as availableBuffers_ will not be empty. @@ -196,12 +201,13 @@ void RPiStream::clearBuffers() availableBuffers_ = std::queue{}; requestBuffers_ = std::queue{}; internalBuffers_.clear(); - bufferList_.clear(); + bufferMap_.clear(); + id_.reset(); } int RPiStream::queueToDevice(FrameBuffer *buffer) { - LOG(RPISTREAM, Debug) << "Queuing buffer " << getBufferIndex(buffer) + LOG(RPISTREAM, Debug) << "Queuing buffer " << getBufferId(buffer) << " for " << name_; int ret = dev_->queueBuffer(buffer); diff --git a/src/libcamera/pipeline/raspberrypi/rpi_stream.h b/src/libcamera/pipeline/raspberrypi/rpi_stream.h index 959e9147..df986367 100644 --- a/src/libcamera/pipeline/raspberrypi/rpi_stream.h +++ b/src/libcamera/pipeline/raspberrypi/rpi_stream.h @@ -9,8 +9,10 @@ #include #include +#include #include +#include #include #include "libcamera/internal/v4l2_videodevice.h" @@ -19,6 +21,8 @@ namespace libcamera { namespace RPi { +using BufferMap = std::unordered_map; + /* * Device stream abstraction for either an internal or external stream. * Used for both Unicam and the ISP. @@ -27,12 +31,13 @@ class RPiStream : public Stream { public: RPiStream() + : id_(RPiIpaMask::ID) { } RPiStream(const char *name, MediaEntity *dev, bool importOnly = false) : external_(false), importOnly_(importOnly), name_(name), - dev_(std::make_unique(dev)) + dev_(std::make_unique(dev)), id_(RPiIpaMask::ID) { } @@ -45,8 +50,8 @@ public: bool isExternal() const; void setExportedBuffers(std::vector> *buffers); - const std::vector &getBuffers() const; - int getBufferIndex(FrameBuffer *buffer) const; + const BufferMap &getBuffers() const; + int getBufferId(FrameBuffer *buffer) const; int prepareBuffers(unsigned int count); int queueBuffer(FrameBuffer *buffer); @@ -56,6 +61,44 @@ public: void releaseBuffers(); private: + class IdGenerator + { + public: + IdGenerator(int max) + : max_(max), id_(0) + { + } + + int get() + { + int id; + if (!recycle_.empty()) { + id = recycle_.front(); + recycle_.pop(); + } else { + id = id_++; + ASSERT(id_ <= max_); + } + return id; + } + + void release(int id) + { + recycle_.push(id); + } + + void reset() + { + id_ = 0; + recycle_ = {}; + } + + private: + int max_; + int id_; + std::queue recycle_; + }; + void clearBuffers(); int queueToDevice(FrameBuffer *buffer); @@ -74,8 +117,11 @@ private: /* The actual device stream. */ std::unique_ptr dev_; + /* Tracks a unique id key for the bufferMap_ */ + IdGenerator id_; + /* All frame buffers associated with this device stream. */ - std::vector bufferList_; + BufferMap bufferMap_; /* * List of frame buffers that we can use if none have been provided by