{"id":9528,"url":"https://patchwork.libcamera.org/api/1.1/patches/9528/?format=json","web_url":"https://patchwork.libcamera.org/patch/9528/","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":"<20200908074913.109244-13-naush@raspberrypi.com>","date":"2020-09-08T07:49:12","name":"[libcamera-devel,v7,12/12] pipeline: ipa: raspberrypi: Handle any externally allocated FrameBuffer","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"18287efc6f0a164aea1d8d035f113e695b3dfce6","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/9528/mbox/","series":[{"id":1271,"url":"https://patchwork.libcamera.org/api/1.1/series/1271/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=1271","date":"2020-09-08T07:49:00","name":"Zero-copy RAW stream work","version":7,"mbox":"https://patchwork.libcamera.org/series/1271/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/9528/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/9528/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 2AFD6BDB1C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue,  8 Sep 2020 07:49:34 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id E5B2F62BF5;\n\tTue,  8 Sep 2020 09:49:33 +0200 (CEST)","from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com\n\t[IPv6:2a00:1450:4864:20::42d])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 330CC62C33\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue,  8 Sep 2020 09:49:32 +0200 (CEST)","by mail-wr1-x42d.google.com with SMTP id j2so18009734wrx.7\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 08 Sep 2020 00:49:32 -0700 (PDT)","from naushir-VirtualBox.patuck.local ([88.97.76.4])\n\tby smtp.gmail.com with ESMTPSA id\n\tq15sm32534502wrr.8.2020.09.08.00.49.30\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tTue, 08 Sep 2020 00:49:30 -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=\"ENwqRFOz\"; 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=+m5t4oaBhjXCa+YRQlZGE+0h7+DM3PcY0Gkj/7069Pc=;\n\tb=ENwqRFOzC4AsjjUKYk2VRb1WII4cACJa1ivp2Hvw5u/6JyaezQLeTbZDuaFa1VApp5\n\tUp3FUnVo2LLHs06mAPJMXAknnyqx2Uf25Ek7sw1t1jmIpFIXLmOCd51ej8oh+sKnrqIC\n\tJCCG7rAK/ZA8wb6PyQ7vNIGVKlMXDOnsvUdY57vdccCrOv1chk+lHSlD7UMgUXoykzGJ\n\tYQmWYWTlH4MbLocI8XyaHkEFnuofOVCpfwQAYmDYND8H8+pZv7OoWFeiQn5ysS5doALa\n\tS1TipXJaClQpFRIzhgQpDRaB8T3xnHXpvQ57HqhDYPdxiAs07jdrBhPVeDPylXiitn6s\n\t0XVw==","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=+m5t4oaBhjXCa+YRQlZGE+0h7+DM3PcY0Gkj/7069Pc=;\n\tb=OTZ6sbZiRkt1oJGZinWywHuLITWjExOHF6ocBK/D7rGP+gWh/+nAoc2DbGb1SCYDio\n\tyyUiCBCHBJ62udvBNOEZDJpQsjmsc8ht82moaEDVVXF1LJGuMhXJ1Bz6y10NhRZbhY5I\n\tiol70w8pph2Y719sUuyGibSHL8BAjpdU4VhjjjhJtVkqo7z7pnGycIIHogdA4ofckFh7\n\tbCTEdA20y5nWnY15fTGp3elC8HFd8shL/ARu07vUOMypcay7jTNvHxHPMcqtEK6WrKSX\n\tbiH4BtxuZIXA69cYMRJ0el7r9nAMFTVTibbnxx1M+Bv1JqNwjO0Tg/YOVagXdD7VSDR0\n\tfgoQ==","X-Gm-Message-State":"AOAM531E/FnyOiIiJbcu7Y2V7wLMqlnCqYusKUNmIRKojodXrFR4yGx/\n\t/xs8EuF5sLsyBF1W7EubrCeNNaJpj3V+5g==","X-Google-Smtp-Source":"ABdhPJwbG03oopFsfkS3PwwegOOA4HNTLgWrZbGhqsrg5iPJbJFLxWAlThJJqs+ytv5v5y++e1uWDw==","X-Received":"by 2002:adf:f750:: with SMTP id\n\tz16mr24305171wrp.377.1599551371491; \n\tTue, 08 Sep 2020 00:49:31 -0700 (PDT)","From":"Naushir Patuck <naush@raspberrypi.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Tue,  8 Sep 2020 08:49:12 +0100","Message-Id":"<20200908074913.109244-13-naush@raspberrypi.com>","X-Mailer":"git-send-email 2.25.1","In-Reply-To":"<20200908074913.109244-1-naush@raspberrypi.com>","References":"<20200908074913.109244-1-naush@raspberrypi.com>","MIME-Version":"1.0","Subject":"[libcamera-devel] [PATCH v7 12/12] pipeline: ipa: raspberrypi:\n\tHandle any externally allocated FrameBuffer","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":"Handle the case where a FrameBuffer that has been externally allocated\n(i.e. not through the v4l2 video device) is passed into a Request.\n\nWe must store the buffer pointer in the stream internal buffer list to\nidentify when used.\n\nSigned-off-by: Naushir Patuck <naush@raspberrypi.com>\n---\n include/libcamera/ipa/raspberrypi.h           | 11 ++--\n src/ipa/raspberrypi/raspberrypi.cpp           |  6 +-\n .../pipeline/raspberrypi/raspberrypi.cpp      | 64 +++++++++++++------\n .../pipeline/raspberrypi/rpi_stream.cpp       | 30 ++++++++-\n .../pipeline/raspberrypi/rpi_stream.h         |  3 +\n 5 files changed, 85 insertions(+), 29 deletions(-)","diff":"diff --git a/include/libcamera/ipa/raspberrypi.h b/include/libcamera/ipa/raspberrypi.h\nindex 262fc6f3..dd6ebeac 100644\n--- a/include/libcamera/ipa/raspberrypi.h\n+++ b/include/libcamera/ipa/raspberrypi.h\n@@ -28,11 +28,12 @@ enum RPiOperations {\n \tRPI_IPA_EVENT_QUEUE_REQUEST,\n };\n \n-enum RPiIpaMask {\n-\tID\t\t= 0x0ffff,\n-\tSTATS\t\t= 0x10000,\n-\tEMBEDDED_DATA\t= 0x20000,\n-\tBAYER_DATA\t= 0x40000\n+enum RPiBufferMask {\n+\tID\t\t= 0x00ffff,\n+\tSTATS\t\t= 0x010000,\n+\tEMBEDDED_DATA\t= 0x020000,\n+\tBAYER_DATA\t= 0x040000,\n+\tEXTERNAL_BUFFER\t= 0x100000,\n };\n \n /* Size of the LS grid allocation. */\ndiff --git a/src/ipa/raspberrypi/raspberrypi.cpp b/src/ipa/raspberrypi/raspberrypi.cpp\nindex 688d2efc..0555cc4e 100644\n--- a/src/ipa/raspberrypi/raspberrypi.cpp\n+++ b/src/ipa/raspberrypi/raspberrypi.cpp\n@@ -352,7 +352,7 @@ void IPARPi::processEvent(const IPAOperationData &event)\n \n \t\tIPAOperationData op;\n \t\top.operation = RPI_IPA_ACTION_STATS_METADATA_COMPLETE;\n-\t\top.data = { bufferId & RPiIpaMask::ID };\n+\t\top.data = { bufferId & RPiBufferMask::ID };\n \t\top.controls = { libcameraMetadata_ };\n \t\tqueueFrameAction.emit(0, op);\n \t\tbreak;\n@@ -373,7 +373,7 @@ void IPARPi::processEvent(const IPAOperationData &event)\n \t\t/* Ready to push the input buffer into the ISP. */\n \t\tIPAOperationData op;\n \t\top.operation = RPI_IPA_ACTION_RUN_ISP;\n-\t\top.data = { bayerbufferId & RPiIpaMask::ID };\n+\t\top.data = { bayerbufferId & RPiBufferMask::ID };\n \t\tqueueFrameAction.emit(0, op);\n \t\tbreak;\n \t}\n@@ -700,7 +700,7 @@ void IPARPi::returnEmbeddedBuffer(unsigned int bufferId)\n {\n \tIPAOperationData op;\n \top.operation = RPI_IPA_ACTION_EMBEDDED_COMPLETE;\n-\top.data = { bufferId & RPiIpaMask::ID };\n+\top.data = { bufferId & RPiBufferMask::ID };\n \tqueueFrameAction.emit(0, op);\n }\n \ndiff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\nindex 5621f182..f5e0d1cd 100644\n--- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n+++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n@@ -151,6 +151,7 @@ public:\n \n \tvoid clearIncompleteRequests();\n \tvoid handleStreamBuffer(FrameBuffer *buffer, RPi::RPiStream *stream);\n+\tvoid handleExternalBuffer(FrameBuffer *buffer, RPi::RPiStream *stream);\n \tvoid handleState();\n \n \tCameraSensor *sensor_;\n@@ -725,23 +726,32 @@ int PipelineHandlerRPi::queueRequestDevice(Camera *camera, Request *request)\n \n \t/* Push all buffers supplied in the Request to the respective streams. */\n \tfor (auto stream : data->streams_) {\n-\t\tif (stream->isExternal()) {\n-\t\t\tFrameBuffer *buffer = request->findBuffer(stream);\n+\t\tif (!stream->isExternal())\n+\t\t\tcontinue;\n+\n+\t\tFrameBuffer *buffer = request->findBuffer(stream);\n+\t\tif (buffer && stream->getBufferId(buffer) == -1) {\n \t\t\t/*\n-\t\t\t * If no buffer is provided by the request for this stream, we\n-\t\t\t * queue a nullptr to the stream to signify that it must use an\n-\t\t\t * internally allocated buffer for this capture request. This\n-\t\t\t * buffer will not be given back to the application, but is used\n-\t\t\t * to support the internal pipeline flow.\n-\t\t\t *\n-\t\t\t * The below queueBuffer() call will do nothing if there are not\n-\t\t\t * enough internal buffers allocated, but this will be handled by\n-\t\t\t * queuing the request for buffers in the RPiStream object.\n+\t\t\t * This buffer is not recognised, so it must have been allocated\n+\t\t\t * outside the v4l2 device. Store it in the stream buffer list\n+\t\t\t * so we can track it.\n \t\t\t */\n-\t\t\tint ret = stream->queueBuffer(buffer);\n-\t\t\tif (ret)\n-\t\t\t\treturn ret;\n+\t\t\tstream->setExternalBuffer(buffer);\n \t\t}\n+\t\t/*\n+\t\t * If no buffer is provided by the request for this stream, we\n+\t\t * queue a nullptr to the stream to signify that it must use an\n+\t\t * internally allocated buffer for this capture request. This\n+\t\t * buffer will not be given back to the application, but is used\n+\t\t * to support the internal pipeline flow.\n+\t\t *\n+\t\t * The below queueBuffer() call will do nothing if there are not\n+\t\t * enough internal buffers allocated, but this will be handled by\n+\t\t * queuing the request for buffers in the RPiStream object.\n+\t\t */\n+\t\tint ret = stream->queueBuffer(buffer);\n+\t\tif (ret)\n+\t\t\treturn ret;\n \t}\n \n \t/* Push the request to the back of the queue. */\n@@ -915,8 +925,8 @@ int PipelineHandlerRPi::prepareBuffers(Camera *camera)\n \t * Pass the stats and embedded data buffers to the IPA. No other\n \t * buffers need to be passed.\n \t */\n-\tmapBuffers(camera, data->isp_[Isp::Stats].getBuffers(), RPiIpaMask::STATS);\n-\tmapBuffers(camera, data->unicam_[Unicam::Embedded].getBuffers(), RPiIpaMask::EMBEDDED_DATA);\n+\tmapBuffers(camera, data->isp_[Isp::Stats].getBuffers(), RPiBufferMask::STATS);\n+\tmapBuffers(camera, data->unicam_[Unicam::Embedded].getBuffers(), RPiBufferMask::EMBEDDED_DATA);\n \n \treturn 0;\n }\n@@ -1219,7 +1229,7 @@ void RPiCameraData::ispOutputDequeue(FrameBuffer *buffer)\n \tif (stream == &isp_[Isp::Stats]) {\n \t\tIPAOperationData op;\n \t\top.operation = RPI_IPA_EVENT_SIGNAL_STAT_READY;\n-\t\top.data = { RPiIpaMask::STATS | static_cast<unsigned int>(index) };\n+\t\top.data = { RPiBufferMask::STATS | static_cast<unsigned int>(index) };\n \t\tipa_->processEvent(op);\n \t} else {\n \t\t/* Any other ISP output can be handed back to the application now. */\n@@ -1293,6 +1303,11 @@ void RPiCameraData::handleStreamBuffer(FrameBuffer *buffer, RPi::RPiStream *stre\n \t\tRequest *request = requestQueue_.front();\n \n \t\tif (!dropFrameCount_ && request->findBuffer(stream) == buffer) {\n+\t\t\t/*\n+\t\t\t * Check if this is an externally provided buffer, and if\n+\t\t\t * so, we must stop tracking it in the pipeline handler.\n+\t\t\t */\n+\t\t\thandleExternalBuffer(buffer, stream);\n \t\t\t/*\n \t\t\t * Tag the buffer as completed, returning it to the\n \t\t\t * application.\n@@ -1311,6 +1326,17 @@ void RPiCameraData::handleStreamBuffer(FrameBuffer *buffer, RPi::RPiStream *stre\n \t}\n }\n \n+void RPiCameraData::handleExternalBuffer(FrameBuffer *buffer, RPi::RPiStream *stream)\n+{\n+\tunsigned int id = stream->getBufferId(buffer);\n+\n+\tif (!(id & RPiBufferMask::EXTERNAL_BUFFER))\n+\t\treturn;\n+\n+\t/* Stop the Stream object from tracking the buffer. */\n+\tstream->removeExternalBuffer(buffer);\n+}\n+\n void RPiCameraData::handleState()\n {\n \tswitch (state_) {\n@@ -1443,8 +1469,8 @@ void RPiCameraData::tryRunPipeline()\n \t\t\t<< \" Embedded buffer id: \" << embeddedIndex;\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 = { RPiBufferMask::EMBEDDED_DATA | embeddedIndex,\n+\t\t    RPiBufferMask::BAYER_DATA | bayerIndex };\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 879e25ba..c09f14c9 100644\n--- a/src/libcamera/pipeline/raspberrypi/rpi_stream.cpp\n+++ b/src/libcamera/pipeline/raspberrypi/rpi_stream.cpp\n@@ -6,6 +6,8 @@\n  */\n #include \"rpi_stream.h\"\n \n+#include <libcamera/ipa/raspberrypi.h>\n+\n #include \"libcamera/internal/log.h\"\n \n namespace libcamera {\n@@ -44,8 +46,13 @@ bool RPiStream::isExternal() const\n \n void RPiStream::setExportedBuffers(std::vector<std::unique_ptr<FrameBuffer>> *buffers)\n {\n-\tfor (auto const &buffer : *buffers)\n-\t\tbufferMap_.emplace(id_++, buffer.get());\n+\t/* Ensure we are using a sensible number of buffers. */\n+\tASSERT(id_ < RPiBufferMask::ID);\n+\n+\tfor (auto const &buffer : *buffers) {\n+\t\tbufferMap_.emplace(id_ & RPiBufferMask::ID, buffer.get());\n+\t\tid_++;\n+\t}\n }\n \n const BufferMap &RPiStream::getBuffers() const\n@@ -68,6 +75,25 @@ int RPiStream::getBufferId(FrameBuffer *buffer) const\n \treturn it->first;\n }\n \n+void RPiStream::setExternalBuffer(FrameBuffer *buffer)\n+{\n+\t/* Ensure we are using a sensible number of buffers. */\n+\tASSERT(id_ < RPiBufferMask::ID);\n+\n+\tbufferMap_.emplace(RPiBufferMask::EXTERNAL_BUFFER | (id_ & RPiBufferMask::ID),\n+\t\t\t   buffer);\n+\tid_++;\n+}\n+\n+void RPiStream::removeExternalBuffer(FrameBuffer *buffer)\n+{\n+\tint id = getBufferId(buffer);\n+\n+\t/* Ensure we have this buffer in the stream, and it is marked external. */\n+\tASSERT(id != -1 && (id & RPiBufferMask::EXTERNAL_BUFFER));\n+\tbufferMap_.erase(id);\n+}\n+\n int RPiStream::prepareBuffers(unsigned int count)\n {\n \tint ret;\ndiff --git a/src/libcamera/pipeline/raspberrypi/rpi_stream.h b/src/libcamera/pipeline/raspberrypi/rpi_stream.h\nindex ed517c22..8b23c4b2 100644\n--- a/src/libcamera/pipeline/raspberrypi/rpi_stream.h\n+++ b/src/libcamera/pipeline/raspberrypi/rpi_stream.h\n@@ -51,6 +51,9 @@ public:\n \tconst BufferMap &getBuffers() const;\n \tint getBufferId(FrameBuffer *buffer) const;\n \n+\tvoid setExternalBuffer(FrameBuffer *buffer);\n+\tvoid removeExternalBuffer(FrameBuffer *buffer);\n+\n \tint prepareBuffers(unsigned int count);\n \tint queueBuffer(FrameBuffer *buffer);\n \tvoid returnBuffer(FrameBuffer *buffer);\n","prefixes":["libcamera-devel","v7","12/12"]}