{"id":9657,"url":"https://patchwork.libcamera.org/api/patches/9657/?format=json","web_url":"https://patchwork.libcamera.org/patch/9657/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/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-13-naush@raspberrypi.com>","date":"2020-09-18T09:42:33","name":"[libcamera-devel,v8,12/12] pipeline: ipa: raspberrypi: Handle any externally allocated FrameBuffer","commit_ref":"39798e8777b553ed9ae9863da02e16e417d8307e","pull_url":null,"state":"accepted","archived":false,"hash":"8c7b6ac667f3da88c6c7a12217a8fa3e2eb3a9bc","submitter":{"id":34,"url":"https://patchwork.libcamera.org/api/people/34/?format=json","name":"Naushir Patuck","email":"naush@raspberrypi.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/9657/mbox/","series":[{"id":1295,"url":"https://patchwork.libcamera.org/api/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/9657/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/9657/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 CC51DC3B5B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 18 Sep 2020 09:44:10 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 9308362FBC;\n\tFri, 18 Sep 2020 11:44:10 +0200 (CEST)","from mail-wm1-x334.google.com (mail-wm1-x334.google.com\n\t[IPv6:2a00:1450:4864:20::334])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id C4EF862FC2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 18 Sep 2020 11:44:07 +0200 (CEST)","by mail-wm1-x334.google.com with SMTP id e17so4681851wme.0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 18 Sep 2020 02:44:07 -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.06\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tFri, 18 Sep 2020 02:44:06 -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=\"qsW1Sjpt\"; 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=aNQtb8h4yI4yUYZQspEf/26vhR7dF0lxXt+8riCrbNs=;\n\tb=qsW1SjptF4i+OrNNE3MBOFNV/Pm2CbvC4dTQH3dtFmytAY4QItan/Xp3ZMmoiQAWEX\n\tEGWVwh1GVWc/A3QlwE2aBuT6LW8EOZUTWf0PaESHgJNYmUXGfM4iDMNEX6KePB8K0whs\n\tJFm5dRTDFyN2iKTvho2Zk+uB+Sc0ea9Kk8PWVQdd+dt3QmV8vN47Wsoq/0LLkkkLTKtd\n\tt3QfWmibv+5chRKXciUkGHmrabifCJhmBQ5ZioWqUOrIp3KKk3A5d2P9WktivjtAT2bz\n\tLJk5LV2cef8aP5saJwkDwi7PCVOM5Kt4Tvf0rJQoyOeO05DM3KGRf3dwTRGEPOIzF9Xq\n\tkqKw==","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=aNQtb8h4yI4yUYZQspEf/26vhR7dF0lxXt+8riCrbNs=;\n\tb=I6YWMOB2/yp06bPQzr6esF2UlsYPiHMMpTeug1X+15y5Ak1wvgusqXNRSMGpmKXKnI\n\tnAtI0pZhhzhIyBvqb52RwDb8sFGE2YY4aPcJAapzZxT9wjai14hdbWT1rXglbe/7Sf/P\n\ts9LD1Zc7C1O9oxtmWsKZuhYNBNkw4iRnDrz50KaI1PniPXifR/k4u74osABWutLhpQqe\n\t50eBVKri9p5nwm9xH50gGNs/EEwpPCuPX7tt4Z1C3IhrJRBmYh/Uf2zQkh3jpWidzRsI\n\tAOBlUBnjjWR0WHgy1OtKBqAmHBidQwXRCkvWzbRJm+5KdxgGDPXcM5i/s8RTVcdFMro1\n\tDmGw==","X-Gm-Message-State":"AOAM531dN9BciHWgXe/DZRwX1D3L5I6soPlxUXUpgBYPBTut0i1aus09\n\tZUPrE1DD/mtN7fhqZjMG8z5Hg/s7TkPnHQ==","X-Google-Smtp-Source":"ABdhPJwf0zOUa5+a+eBkJIZrvY6iV+/1Tn8hUb+UliRLOYY7GAq002JsoOBYKOoEiT9OgJ/NRi1AKw==","X-Received":"by 2002:a1c:5988:: with SMTP id n130mr313854wmb.95.1600422247067;\n\tFri, 18 Sep 2020 02:44:07 -0700 (PDT)","From":"Naushir Patuck <naush@raspberrypi.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Fri, 18 Sep 2020 10:42:33 +0100","Message-Id":"<20200918094233.5273-13-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 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       | 14 ++++\n .../pipeline/raspberrypi/rpi_stream.h         |  7 +-\n 5 files changed, 73 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 4e12c7d6..50f07182 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@@ -1297,6 +1307,11 @@ void RPiCameraData::handleStreamBuffer(FrameBuffer *buffer, RPi::RPiStream *stre\n \t\t */\n \t\tRequest *request = requestQueue_.empty() ? nullptr : requestQueue_.front();\n \t\tif (!dropFrameCount_ && request && 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@@ -1315,6 +1330,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@@ -1447,8 +1473,8 @@ void RPiCameraData::tryRunPipeline()\n \t\t\t<< \" Embedded buffer id: \" << embeddedId;\n \n \top.operation = RPI_IPA_EVENT_SIGNAL_ISP_PREPARE;\n-\top.data = { RPiIpaMask::EMBEDDED_DATA | embeddedId,\n-\t\t    RPiIpaMask::BAYER_DATA | bayerId };\n+\top.data = { RPiBufferMask::EMBEDDED_DATA | embeddedId,\n+\t\t    RPiBufferMask::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 aee0aa2d..2dcf96ca 100644\n--- a/src/libcamera/pipeline/raspberrypi/rpi_stream.cpp\n+++ b/src/libcamera/pipeline/raspberrypi/rpi_stream.cpp\n@@ -68,6 +68,20 @@ int RPiStream::getBufferId(FrameBuffer *buffer) const\n \treturn it->first;\n }\n \n+void RPiStream::setExternalBuffer(FrameBuffer *buffer)\n+{\n+\tbufferMap_.emplace(RPiBufferMask::EXTERNAL_BUFFER | id_.get(), buffer);\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 df986367..a2c21bcd 100644\n--- a/src/libcamera/pipeline/raspberrypi/rpi_stream.h\n+++ b/src/libcamera/pipeline/raspberrypi/rpi_stream.h\n@@ -31,13 +31,13 @@ class RPiStream : public Stream\n {\n public:\n \tRPiStream()\n-\t\t: id_(RPiIpaMask::ID)\n+\t\t: id_(RPiBufferMask::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)), id_(RPiIpaMask::ID)\n+\t\t  dev_(std::make_unique<V4L2VideoDevice>(dev)), id_(RPiBufferMask::ID)\n \t{\n \t}\n \n@@ -53,6 +53,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","v8","12/12"]}