{"id":9655,"url":"https://patchwork.libcamera.org/api/1.1/patches/9655/?format=json","web_url":"https://patchwork.libcamera.org/patch/9655/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20200918094233.5273-11-naush@raspberrypi.com>","date":"2020-09-18T09:42:31","name":"[libcamera-devel,v8,10/12] pipeline: raspberrypi: Use an unordered_map for the stream buffer list","commit_ref":"e1784ca88359827824f3b5a1efc60c15c9783564","pull_url":null,"state":"accepted","archived":false,"hash":"f34fe817063fc2456faae571d379702979aee7b0","submitter":{"id":34,"url":"https://patchwork.libcamera.org/api/1.1/people/34/?format=json","name":"Naushir Patuck","email":"naush@raspberrypi.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/9655/mbox/","series":[{"id":1295,"url":"https://patchwork.libcamera.org/api/1.1/series/1295/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=1295","date":"2020-09-18T09:42:21","name":"Zero-copy RAW stream work","version":8,"mbox":"https://patchwork.libcamera.org/series/1295/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/9655/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/9655/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id CD8FAC3B5B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 18 Sep 2020 09:44:08 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 99E7162FCD;\n\tFri, 18 Sep 2020 11:44:08 +0200 (CEST)","from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com\n\t[IPv6:2a00:1450:4864:20::42c])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id AC5C062FCD\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 18 Sep 2020 11:44:05 +0200 (CEST)","by mail-wr1-x42c.google.com with SMTP id w5so4935075wrp.8\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 18 Sep 2020 02:44:05 -0700 (PDT)","from naushir-VirtualBox.pitowers.org\n\t([2a00:1098:3142:14:a00:27ff:fe4d:f6a2])\n\tby smtp.gmail.com with ESMTPSA id\n\tn2sm4792472wma.29.2020.09.18.02.44.03\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tFri, 18 Sep 2020 02:44:03 -0700 (PDT)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (2048-bit key;\n\tunprotected) header.d=raspberrypi.com header.i=@raspberrypi.com\n\theader.b=\"dFh78i+M\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=raspberrypi.com; s=google;\n\th=from:to:cc:subject:date:message-id:in-reply-to:references\n\t:mime-version:content-transfer-encoding;\n\tbh=hxq5IjBzS+WFtXE8uRz4DB3Vof4h0VUl6+n57VaVinw=;\n\tb=dFh78i+MVzoYpWElAuZu6fWI6gsk9HLfyeXxpY2EJoqZLfayccypX2S19SgSQKcZc/\n\t465piy5df0rLZMFo01YPk1j+/KMiPCuQ4cRpZcOOExwp1gensUM0qCT3fNZNnt7ojz7k\n\t5gPgVkiYTPj9N2XcfQGLhp3c7t8T5xAoi3L+5fM6LlvZgKE9R09jDEXvFU/JQhKl3Bwx\n\tr3KTKHqfwW0REIaFMdoMO2zAQNIfX0PFFe82enxYhTeWSSqP+SMBKJu8iWmcAbGxqEol\n\ta0Ro/xWaxhQfv6jEnoN6MIQJYaqkg7aLRw7A3j6+w/kVLsiXrViTtmQUJW15txIHbHZ6\n\tvszw==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\n\t:references:mime-version:content-transfer-encoding;\n\tbh=hxq5IjBzS+WFtXE8uRz4DB3Vof4h0VUl6+n57VaVinw=;\n\tb=LgSLpp9zPYrBcmk83G5E0BwLRNQ8AcW/3VSOo2VdrfXGUYV53Pa51260VnEts8NB3Z\n\tA0Q1Xd15aAQ5G026EkjFzo90T9vz6zdPFY/R1NmwwHBzGNH6KPuprMaaQ1xXRfppRftV\n\t/3I4pRPiHHBOtcm0iSTwKsRsmBYaeQ89ZecsoHfLprRtHWYFgxMZ719aer+VMZ7YhMMU\n\tfexi+8w66UJou6VhfKH2BHJuK3R7kG4n/yw4jA936ClaC+60R38rMq05/iIvvGyJH4WA\n\t3/3sM90uagyOk0FemMnvzH1bNweULz/2CEdVlHd5yIdm8H4D2VONFY063gFL51nRPy6Z\n\tUshw==","X-Gm-Message-State":"AOAM532BOLqnGBhuV7BIbLu4ef1V3RGTkwoimsCjX0nmfHGs6YtUOBhT\n\t3VYzn25Brcr3rdyaIPiNmFeIZbvNphu8ew==","X-Google-Smtp-Source":"ABdhPJzJMD4BF/TArLXjKhYW3WRr61qAfTMefLr+AVVcFe/AcIgoQvR8cjDIzQiIrGf47WRVlrvIeg==","X-Received":"by 2002:adf:d4c7:: with SMTP id\n\tw7mr37494836wrk.263.1600422244922; \n\tFri, 18 Sep 2020 02:44:04 -0700 (PDT)","From":"Naushir Patuck <naush@raspberrypi.com>","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\n\tunordered_map for the stream buffer list","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Content-Type":"text/plain; charset=\"us-ascii\"","Content-Transfer-Encoding":"7bit","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"By using a map container, we can easily insert/remove buffers from the\nbuffer list. This will be required to track buffers allocated externally\nand passed to the pipeline handler through a Request.\n\nReplace the buffer index tracking with an id generated internally by the\nstream object.\n\nSigned-off-by: Naushir Patuck <naush@raspberrypi.com>\n---\n .../pipeline/raspberrypi/raspberrypi.cpp      | 31 +++++------\n .../pipeline/raspberrypi/rpi_stream.cpp       | 32 ++++++-----\n .../pipeline/raspberrypi/rpi_stream.h         | 54 +++++++++++++++++--\n 3 files changed, 82 insertions(+), 35 deletions(-)","diff":"diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\nindex 7d91188c..51544233 100644\n--- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n+++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n@@ -890,7 +890,6 @@ int PipelineHandlerRPi::queueAllBuffers(Camera *camera)\n int PipelineHandlerRPi::prepareBuffers(Camera *camera)\n {\n \tRPiCameraData *data = cameraData(camera);\n-\tunsigned int index;\n \tint ret;\n \n \t/*\n@@ -917,18 +916,14 @@ int PipelineHandlerRPi::prepareBuffers(Camera *camera)\n \t * This will allow us to identify buffers passed between the pipeline\n \t * handler and the IPA.\n \t */\n-\tindex = 0;\n \tfor (auto const &b : data->isp_[Isp::Stats].getBuffers()) {\n-\t\tdata->ipaBuffers_.push_back({ .id = RPiIpaMask::STATS | index,\n-\t\t\t\t\t      .planes = b->planes() });\n-\t\tindex++;\n+\t\tdata->ipaBuffers_.push_back({ .id = RPiIpaMask::STATS | b.first,\n+\t\t\t\t\t      .planes = b.second->planes() });\n \t}\n \n-\tindex = 0;\n \tfor (auto const &b : data->unicam_[Unicam::Embedded].getBuffers()) {\n-\t\tdata->ipaBuffers_.push_back({ .id = RPiIpaMask::EMBEDDED_DATA | index,\n-\t\t\t\t\t      .planes = b->planes() });\n-\t\tindex++;\n+\t\tdata->ipaBuffers_.push_back({ .id = RPiIpaMask::EMBEDDED_DATA | b.first,\n+\t\t\t\t\t      .planes = b.second->planes() });\n \t}\n \n \tdata->ipa_->mapBuffers(data->ipaBuffers_);\n@@ -1127,7 +1122,7 @@ void RPiCameraData::unicamBufferDequeue(FrameBuffer *buffer)\n \t\treturn;\n \n \tfor (RPi::RPiStream &s : unicam_) {\n-\t\tindex = s.getBufferIndex(buffer);\n+\t\tindex = s.getBufferId(buffer);\n \t\tif (index != -1) {\n \t\t\tstream = &s;\n \t\t\tbreak;\n@@ -1178,7 +1173,7 @@ void RPiCameraData::ispInputDequeue(FrameBuffer *buffer)\n \t\treturn;\n \n \tLOG(RPI, Debug) << \"Stream ISP Input buffer complete\"\n-\t\t\t<< \", buffer id \" << unicam_[Unicam::Image].getBufferIndex(buffer)\n+\t\t\t<< \", buffer id \" << unicam_[Unicam::Image].getBufferId(buffer)\n \t\t\t<< \", timestamp: \" << buffer->metadata().timestamp;\n \n \t/* The ISP input buffer gets re-queued into Unicam. */\n@@ -1195,7 +1190,7 @@ void RPiCameraData::ispOutputDequeue(FrameBuffer *buffer)\n \t\treturn;\n \n \tfor (RPi::RPiStream &s : isp_) {\n-\t\tindex = s.getBufferIndex(buffer);\n+\t\tindex = s.getBufferId(buffer);\n \t\tif (index != -1) {\n \t\t\tstream = &s;\n \t\t\tbreak;\n@@ -1436,16 +1431,16 @@ void RPiCameraData::tryRunPipeline()\n \t/* Set our state to say the pipeline is active. */\n \tstate_ = State::Busy;\n \n-\tunsigned int bayerIndex = unicam_[Unicam::Image].getBufferIndex(bayerBuffer);\n-\tunsigned int embeddedIndex = unicam_[Unicam::Embedded].getBufferIndex(embeddedBuffer);\n+\tunsigned int bayerId = unicam_[Unicam::Image].getBufferId(bayerBuffer);\n+\tunsigned int embeddedId = unicam_[Unicam::Embedded].getBufferId(embeddedBuffer);\n \n \tLOG(RPI, Debug) << \"Signalling RPI_IPA_EVENT_SIGNAL_ISP_PREPARE:\"\n-\t\t\t<< \" Bayer buffer id: \" << bayerIndex\n-\t\t\t<< \" Embedded buffer id: \" << embeddedIndex;\n+\t\t\t<< \" Bayer buffer id: \" << bayerId\n+\t\t\t<< \" Embedded buffer id: \" << embeddedId;\n \n \top.operation = RPI_IPA_EVENT_SIGNAL_ISP_PREPARE;\n-\top.data = { RPiIpaMask::EMBEDDED_DATA | embeddedIndex,\n-\t\t    RPiIpaMask::BAYER_DATA | bayerIndex };\n+\top.data = { RPiIpaMask::EMBEDDED_DATA | embeddedId,\n+\t\t    RPiIpaMask::BAYER_DATA | bayerId };\n \tipa_->processEvent(op);\n }\n \ndiff --git a/src/libcamera/pipeline/raspberrypi/rpi_stream.cpp b/src/libcamera/pipeline/raspberrypi/rpi_stream.cpp\nindex 80170d64..aee0aa2d 100644\n--- a/src/libcamera/pipeline/raspberrypi/rpi_stream.cpp\n+++ b/src/libcamera/pipeline/raspberrypi/rpi_stream.cpp\n@@ -44,26 +44,28 @@ bool RPiStream::isExternal() const\n \n void RPiStream::setExportedBuffers(std::vector<std::unique_ptr<FrameBuffer>> *buffers)\n {\n-\tstd::transform(buffers->begin(), buffers->end(), std::back_inserter(bufferList_),\n-\t\t       [](std::unique_ptr<FrameBuffer> &b) { return b.get(); });\n+\tfor (auto const &buffer : *buffers)\n+\t\tbufferMap_.emplace(id_.get(), buffer.get());\n }\n \n-const std::vector<FrameBuffer *> &RPiStream::getBuffers() const\n+const BufferMap &RPiStream::getBuffers() const\n {\n-\treturn bufferList_;\n+\treturn bufferMap_;\n }\n \n-int RPiStream::getBufferIndex(FrameBuffer *buffer) const\n+int RPiStream::getBufferId(FrameBuffer *buffer) const\n {\n \tif (importOnly_)\n \t\treturn -1;\n \n-\t/* Find the buffer in the list, and return the index position. */\n-\tauto it = std::find(bufferList_.begin(), bufferList_.end(), buffer);\n-\tif (it != bufferList_.end())\n-\t\treturn std::distance(bufferList_.begin(), it);\n+\t/* Find the buffer in the map, and return the buffer id. */\n+\tauto it = std::find_if(bufferMap_.begin(), bufferMap_.end(),\n+\t\t\t       [&buffer](auto const &p) { return p.second == buffer; });\n \n-\treturn -1;\n+\tif (it == bufferMap_.end())\n+\t\treturn -1;\n+\n+\treturn it->first;\n }\n \n int RPiStream::prepareBuffers(unsigned int count)\n@@ -86,7 +88,7 @@ int RPiStream::prepareBuffers(unsigned int count)\n \t\t}\n \n \t\t/* We must import all internal/external exported buffers. */\n-\t\tcount = bufferList_.size();\n+\t\tcount = bufferMap_.size();\n \t}\n \n \treturn dev_->importBuffers(count);\n@@ -139,6 +141,9 @@ void RPiStream::returnBuffer(FrameBuffer *buffer)\n \t/* Push this buffer back into the queue to be used again. */\n \tavailableBuffers_.push(buffer);\n \n+\t/* Allow the buffer id to be reused. */\n+\tid_.release(getBufferId(buffer));\n+\n \t/*\n \t * Do we have any Request buffers that are waiting to be queued?\n \t * If so, do it now as availableBuffers_ will not be empty.\n@@ -196,12 +201,13 @@ void RPiStream::clearBuffers()\n \tavailableBuffers_ = std::queue<FrameBuffer *>{};\n \trequestBuffers_ = std::queue<FrameBuffer *>{};\n \tinternalBuffers_.clear();\n-\tbufferList_.clear();\n+\tbufferMap_.clear();\n+\tid_.reset();\n }\n \n int RPiStream::queueToDevice(FrameBuffer *buffer)\n {\n-\tLOG(RPISTREAM, Debug) << \"Queuing buffer \" << getBufferIndex(buffer)\n+\tLOG(RPISTREAM, Debug) << \"Queuing buffer \" << getBufferId(buffer)\n \t\t\t      << \" for \" << name_;\n \n \tint ret = dev_->queueBuffer(buffer);\ndiff --git a/src/libcamera/pipeline/raspberrypi/rpi_stream.h b/src/libcamera/pipeline/raspberrypi/rpi_stream.h\nindex 959e9147..df986367 100644\n--- a/src/libcamera/pipeline/raspberrypi/rpi_stream.h\n+++ b/src/libcamera/pipeline/raspberrypi/rpi_stream.h\n@@ -9,8 +9,10 @@\n \n #include <queue>\n #include <string>\n+#include <unordered_map>\n #include <vector>\n \n+#include <libcamera/ipa/raspberrypi.h>\n #include <libcamera/stream.h>\n \n #include \"libcamera/internal/v4l2_videodevice.h\"\n@@ -19,6 +21,8 @@ namespace libcamera {\n \n namespace RPi {\n \n+using BufferMap = std::unordered_map<unsigned int, FrameBuffer *>;\n+\n /*\n  * Device stream abstraction for either an internal or external stream.\n  * Used for both Unicam and the ISP.\n@@ -27,12 +31,13 @@ class RPiStream : public Stream\n {\n public:\n \tRPiStream()\n+\t\t: id_(RPiIpaMask::ID)\n \t{\n \t}\n \n \tRPiStream(const char *name, MediaEntity *dev, bool importOnly = false)\n \t\t: external_(false), importOnly_(importOnly), name_(name),\n-\t\t  dev_(std::make_unique<V4L2VideoDevice>(dev))\n+\t\t  dev_(std::make_unique<V4L2VideoDevice>(dev)), id_(RPiIpaMask::ID)\n \t{\n \t}\n \n@@ -45,8 +50,8 @@ public:\n \tbool isExternal() const;\n \n \tvoid setExportedBuffers(std::vector<std::unique_ptr<FrameBuffer>> *buffers);\n-\tconst std::vector<FrameBuffer *> &getBuffers() const;\n-\tint getBufferIndex(FrameBuffer *buffer) const;\n+\tconst BufferMap &getBuffers() const;\n+\tint getBufferId(FrameBuffer *buffer) const;\n \n \tint prepareBuffers(unsigned int count);\n \tint queueBuffer(FrameBuffer *buffer);\n@@ -56,6 +61,44 @@ public:\n \tvoid releaseBuffers();\n \n private:\n+\tclass IdGenerator\n+\t{\n+\tpublic:\n+\t\tIdGenerator(int max)\n+\t\t\t: max_(max), id_(0)\n+\t\t{\n+\t\t}\n+\n+\t\tint get()\n+\t\t{\n+\t\t\tint id;\n+\t\t\tif (!recycle_.empty()) {\n+\t\t\t\tid = recycle_.front();\n+\t\t\t\trecycle_.pop();\n+\t\t\t} else {\n+\t\t\t\tid = id_++;\n+\t\t\t\tASSERT(id_ <= max_);\n+\t\t\t}\n+\t\t\treturn id;\n+\t\t}\n+\n+\t\tvoid release(int id)\n+\t\t{\n+\t\t\trecycle_.push(id);\n+\t\t}\n+\n+\t\tvoid reset()\n+\t\t{\n+\t\t\tid_ = 0;\n+\t\t\trecycle_ = {};\n+\t\t}\n+\n+\tprivate:\n+\t\tint max_;\n+\t\tint id_;\n+\t\tstd::queue<int> recycle_;\n+\t};\n+\n \tvoid clearBuffers();\n \tint queueToDevice(FrameBuffer *buffer);\n \n@@ -74,8 +117,11 @@ private:\n \t/* The actual device stream. */\n \tstd::unique_ptr<V4L2VideoDevice> dev_;\n \n+\t/* Tracks a unique id key for the bufferMap_ */\n+\tIdGenerator id_;\n+\n \t/* All frame buffers associated with this device stream. */\n-\tstd::vector<FrameBuffer *> bufferList_;\n+\tBufferMap bufferMap_;\n \n \t/*\n \t * List of frame buffers that we can use if none have been provided by\n","prefixes":["libcamera-devel","v8","10/12"]}