{"id":9653,"url":"https://patchwork.libcamera.org/api/patches/9653/?format=json","web_url":"https://patchwork.libcamera.org/patch/9653/","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-9-naush@raspberrypi.com>","date":"2020-09-18T09:42:29","name":"[libcamera-devel,v8,08/12] libcamera: pipeline: raspberrypi: Add more robust stream buffer logic","commit_ref":"b752c57c33151eff5b0e003a24f05a4e05465b1f","pull_url":null,"state":"accepted","archived":false,"hash":"f66117af5e1b433d8268e59bd8524c44331f4646","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/9653/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/9653/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/9653/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 E3C05C3B5B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 18 Sep 2020 09:44:05 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id B2C7862FCF;\n\tFri, 18 Sep 2020 11:44:05 +0200 (CEST)","from mail-wr1-x433.google.com (mail-wr1-x433.google.com\n\t[IPv6:2a00:1450:4864:20::433])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 1196E62FBF\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 18 Sep 2020 11:44:03 +0200 (CEST)","by mail-wr1-x433.google.com with SMTP id j2so4939849wrx.7\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 18 Sep 2020 02:44:03 -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.01\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tFri, 18 Sep 2020 02:44:01 -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=\"hF2Ilwxb\"; 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=qXKgORLdFipnKwdsuEbwQtPSTduRWQCqLlS+WdmGLhg=;\n\tb=hF2IlwxbbpcgE73JQiAxCoCYppspsN5SkWBlgWvnxEqPoCa4IrtgFsvBIs7wOGimmw\n\tJtFIAUUPkd5mlucVO50IM2IkM0AF3wgliKqQ4TT7WTFoAPrmxVPnxH0llgQLZcvGJ9MI\n\tXixfypC9MQRScVgp2nJx2gNDxvOVXa3nvJM1cCuQxnpBFlmdKvOD/t+cKkY6GkB3IMXY\n\tT6AXsFGSbc9rNJpLmsums4JGd0YdSAGv39OrBFSVhynsi+X+5nQsgxPrfsrJbk2bFLXo\n\txbPCQTfciFu/Z+T+8tvqFySxx+O2tExH/+DXALVa0pVnrSuv+amrtkXFuif82fM4O8zw\n\tK6pw==","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=qXKgORLdFipnKwdsuEbwQtPSTduRWQCqLlS+WdmGLhg=;\n\tb=N7hrQM8eQGeUf+UUdAh56v11trrPZE2Y4w6Ou4mdPoceUFPVzPz03NeYtMZ8MMqpeb\n\tkOdyz88sAeGYQljxXLSxXCNn+Nvtm4Bq2spjnLySEY1um4tkVeC8Ct6cXjSC73MXhP3l\n\tF9gKhif/LWW82wVt05jxRpFbg+swPz0x6j/giikH3ibZjUll1jPrETTdanms/VMuVhVv\n\toOweFFKF0+U9OCnnE+dElMXRnxRypwsVkDmCJ1zV8q9adD17vpyyX2DOWEyvuq6GxOt4\n\t37qbYuh84aXhjQsp9dIRfB1+ZgEKT+Ho7lhJH7Y6GvzNRKytqXhjy18wdbYxqPt8dfRE\n\tYMGQ==","X-Gm-Message-State":"AOAM5335zCCKdU0EYbq6ZGucaHtvunDa9baiqSRwkyy960NOnGDLCS3P\n\tgm40qWVZE9ZwguUmX5La8LVYI3ju1x6SUQ==","X-Google-Smtp-Source":"ABdhPJxG2pIlJmsaUGcrcdAgQvUfIiRylfmd5iWri9S745xqhl/LaxbuSL0Xnu3VbDHmM1GoJU5ImA==","X-Received":"by 2002:adf:de8b:: with SMTP id\n\tw11mr36595619wrl.401.1600422242406; \n\tFri, 18 Sep 2020 02:44:02 -0700 (PDT)","From":"Naushir Patuck <naush@raspberrypi.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Fri, 18 Sep 2020 10:42:29 +0100","Message-Id":"<20200918094233.5273-9-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 08/12] libcamera: pipeline:\n\traspberrypi: Add more robust stream buffer logic","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=\"utf-8\"","Content-Transfer-Encoding":"base64","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Add further queueing into the RPiStream object to ensure that we always\nfollow the buffer ordering (be it internal or external) given by\nincoming Requests.\n\nThis is essential, otherwise we risk dropping frames that are meant to\nbe part of a Request, and can cause the pipeline to stall indefinitely.\nThis also prevents any possibility of mismatched frame buffers going\nthrough the pipeline and out to the application.\n\nSigned-off-by: Naushir Patuck <naush@raspberrypi.com>\nReviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\nTested-by: David Plowman <david.plowman@raspberrypi.com>\n---\n .../pipeline/raspberrypi/rpi_stream.cpp       | 76 +++++++++++++++----\n .../pipeline/raspberrypi/rpi_stream.h         | 13 +++-\n 2 files changed, 75 insertions(+), 14 deletions(-)","diff":"diff --git a/src/libcamera/pipeline/raspberrypi/rpi_stream.cpp b/src/libcamera/pipeline/raspberrypi/rpi_stream.cpp\nindex b4d737db..b2a5dfa7 100644\n--- a/src/libcamera/pipeline/raspberrypi/rpi_stream.cpp\n+++ b/src/libcamera/pipeline/raspberrypi/rpi_stream.cpp\n@@ -94,38 +94,75 @@ int RPiStream::queueBuffer(FrameBuffer *buffer)\n {\n \t/*\n \t * A nullptr buffer implies an external stream, but no external\n-\t * buffer has been supplied. So, pick one from the availableBuffers_\n-\t * queue.\n+\t * buffer has been supplied in the Request. So, pick one from the\n+\t * availableBuffers_ queue.\n \t */\n \tif (!buffer) {\n \t\tif (availableBuffers_.empty()) {\n-\t\t\tLOG(RPISTREAM, Warning) << \"No buffers available for \"\n+\t\t\tLOG(RPISTREAM, Info) << \"No buffers available for \"\n \t\t\t\t\t\t<< name_;\n-\t\t\treturn -EINVAL;\n+\t\t\t/*\n+\t\t\t * Note that we need to queue an internal buffer as soon\n+\t\t\t * as one becomes available.\n+\t\t\t */\n+\t\t\trequestBuffers_.push(nullptr);\n+\t\t\treturn 0;\n \t\t}\n \n \t\tbuffer = availableBuffers_.front();\n \t\tavailableBuffers_.pop();\n \t}\n \n-\tLOG(RPISTREAM, Debug) << \"Queuing buffer \" << buffer->cookie()\n-\t\t\t      << \" for \" << name_;\n+\t/*\n+\t * If no earlier requests are pending to be queued we can go ahead and\n+\t * queue this buffer into the device.\n+\t */\n+\tif (requestBuffers_.empty())\n+\t\treturn queueToDevice(buffer);\n \n-\tint ret = dev_->queueBuffer(buffer);\n-\tif (ret) {\n-\t\tLOG(RPISTREAM, Error) << \"Failed to queue buffer for \"\n-\t\t\t\t      << name_;\n-\t}\n+\t/*\n+\t * There are earlier Request buffers to be queued, so this buffer must go\n+\t * on the waiting list.\n+\t */\n+\trequestBuffers_.push(buffer);\n \n-\treturn ret;\n+\treturn 0;\n }\n \n void RPiStream::returnBuffer(FrameBuffer *buffer)\n {\n \t/* This can only be called for external streams. */\n-\tassert(external_);\n+\tASSERT(external_);\n \n+\t/* Push this buffer back into the queue to be used again. */\n \tavailableBuffers_.push(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+\t */\n+\twhile (!requestBuffers_.empty()) {\n+\t\tFrameBuffer *buffer = requestBuffers_.front();\n+\n+\t\tif (!buffer) {\n+\t\t\t/*\n+\t\t\t * We want to queue an internal buffer, but none\n+\t\t\t * are available. Can't do anything, quit the loop.\n+\t\t\t */\n+\t\t\tif (availableBuffers_.empty())\n+\t\t\t\tbreak;\n+\n+\t\t\t/*\n+\t\t\t * We want to queue an internal buffer, and at least one\n+\t\t\t * is available.\n+\t\t\t */\n+\t\t\tbuffer = availableBuffers_.front();\n+\t\t\tavailableBuffers_.pop();\n+\t\t}\n+\n+\t\trequestBuffers_.pop();\n+\t\tqueueToDevice(buffer);\n+\t}\n }\n \n int RPiStream::queueAllBuffers()\n@@ -155,10 +192,23 @@ void RPiStream::releaseBuffers()\n void RPiStream::clearBuffers()\n {\n \tavailableBuffers_ = std::queue<FrameBuffer *>{};\n+\trequestBuffers_ = std::queue<FrameBuffer *>{};\n \tinternalBuffers_.clear();\n \tbufferList_.clear();\n }\n \n+int RPiStream::queueToDevice(FrameBuffer *buffer)\n+{\n+\tLOG(RPISTREAM, Debug) << \"Queuing buffer \" << buffer->cookie()\n+\t\t\t      << \" for \" << name_;\n+\n+\tint ret = dev_->queueBuffer(buffer);\n+\tif (ret)\n+\t\tLOG(RPISTREAM, Error) << \"Failed to queue buffer for \"\n+\t\t\t\t      << name_;\n+\treturn ret;\n+}\n+\n } /* namespace RPi */\n \n } /* namespace libcamera */\ndiff --git a/src/libcamera/pipeline/raspberrypi/rpi_stream.h b/src/libcamera/pipeline/raspberrypi/rpi_stream.h\nindex 73954ec7..f42e25b9 100644\n--- a/src/libcamera/pipeline/raspberrypi/rpi_stream.h\n+++ b/src/libcamera/pipeline/raspberrypi/rpi_stream.h\n@@ -57,6 +57,7 @@ public:\n \n private:\n \tvoid clearBuffers();\n+\tint queueToDevice(FrameBuffer *buffer);\n \n \t/*\n \t * Indicates that this stream is active externally, i.e. the buffers\n@@ -73,7 +74,7 @@ private:\n \t/* The actual device stream. */\n \tstd::unique_ptr<V4L2VideoDevice> dev_;\n \n-\t/* All framebuffers associated with this device stream. */\n+\t/* All frame buffers associated with this device stream. */\n \tstd::vector<FrameBuffer *> bufferList_;\n \n \t/*\n@@ -83,6 +84,16 @@ private:\n \t */\n \tstd::queue<FrameBuffer *> availableBuffers_;\n \n+\t/*\n+\t * List of frame buffers that are to be queued into the device from a Request.\n+\t * A nullptr indicates any internal buffer can be used (from availableBuffers_),\n+\t * whereas a valid pointer indicates an external buffer to be queued.\n+\t *\n+\t * Ordering buffers to be queued is important here as it must match the\n+\t * requests coming from the application.\n+\t */\n+\tstd::queue<FrameBuffer *> requestBuffers_;\n+\n \t/*\n \t * This is a list of buffers exported internally. Need to keep this around\n \t * as the stream needs to maintain ownership of these buffers.\n","prefixes":["libcamera-devel","v8","08/12"]}