{"id":8870,"url":"https://patchwork.libcamera.org/api/patches/8870/?format=json","web_url":"https://patchwork.libcamera.org/patch/8870/","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":"<20200720091311.805092-9-naush@raspberrypi.com>","date":"2020-07-20T09:13:10","name":"[libcamera-devel,v4,8/9] libcamera: pipeline: raspberrypi: Add more robust stream buffer logic","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"c32ff0709ea00cdce0eb6a0303f7444c5feb303d","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/8870/mbox/","series":[{"id":1117,"url":"https://patchwork.libcamera.org/api/series/1117/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=1117","date":"2020-07-20T09:13:02","name":"Zero-copy RAW stream work","version":4,"mbox":"https://patchwork.libcamera.org/series/1117/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/8870/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/8870/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 93787BD792\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 20 Jul 2020 09:13:39 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 6203B605C9;\n\tMon, 20 Jul 2020 11:13:39 +0200 (CEST)","from mail-wm1-x330.google.com (mail-wm1-x330.google.com\n\t[IPv6:2a00:1450:4864:20::330])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 7EEBE605BB\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 20 Jul 2020 11:13:35 +0200 (CEST)","by mail-wm1-x330.google.com with SMTP id c80so21520478wme.0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 20 Jul 2020 02:13:35 -0700 (PDT)","from naushir-VirtualBox.patuck.local ([88.97.76.4])\n\tby smtp.gmail.com with ESMTPSA id\n\tu1sm38137185wrb.78.2020.07.20.02.13.33\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tMon, 20 Jul 2020 02:13:34 -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=\"m4dxU722\"; 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=xz6y/lKwinaj+jtgprrtx2kUePux8NUENK0+5gnGFGw=;\n\tb=m4dxU7225Xt5amA0nuZLMvradiY87iiTtMDfxEs+zpudAUM05DBUx6cJm4lPM9IClE\n\t7O9w+422RV2kZ6fv5vC71JW6UXw/6tFhn7QUCaN/JxlrSgGYWjA27Wk5nHUIuSdSTQ6W\n\tmybSzdqqTa75uqQkACmhTstua34fePYPRPH00dRx4N3vEKr1dPRldxP+nyi7EmfxgyBN\n\tpz5v0wr5PnTAYZtkXaLDlZVMWYyQZafcE7yIDVkZ0P+rHXFrLzmUou0Agj/jG95AGPu3\n\tPadZRjw5KHGMsUfBKE1UPfQlv+b+Bi0SMsTFO7uzwQHteCyccCdKdrAJBUoYz7d5A3yz\n\toKGg==","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=xz6y/lKwinaj+jtgprrtx2kUePux8NUENK0+5gnGFGw=;\n\tb=lDcAIbmzXZwyF6f49US074wRfWQFxJo53vUuzxTvPrR9d+NdmJFIOwqh0hrbXzzVrf\n\tH+0aOlPtKKhbvcRRwMHwxbZXDjpRWXx5Cuq18lPHaR6UNQPo8epitdv0fWUf0ZYX71xU\n\tUVUFP3cTReaiRJnbLggxS986Y/Fy1akN3zbzudLT+ZCnN/kyihL+S+Lxg4jLpzw+NA7C\n\ts8PXCnzswtBcMagb3IDWkyhnch9JzG/VNxrEmcYMnVd0gK4G+DCiOGdnUWFMTQOER8ux\n\tUfsZA+QPUpe1damvqNd1xisaHMZout0f4j1W2n3IR1k01DHsx0Vj0HhC8XVl7iWI2QIO\n\tr6dg==","X-Gm-Message-State":"AOAM530js1G3Up+UpJSlKRcuBLeG2H1aRoOJQu/+hE6UFOR1R9B2G8ai\n\ttwHEvHVI1RqfQreX5iHvDCNtyTxXsbG+iQ==","X-Google-Smtp-Source":"ABdhPJwsVwKrq77BlXk+pCoVfs6NXg5osAwBBkN7H3X3G3cfoF06FsMevKWerSgg0iZWU89W8PTbRw==","X-Received":"by 2002:a1c:1f54:: with SMTP id f81mr20484254wmf.4.1595236414662;\n\tMon, 20 Jul 2020 02:13:34 -0700 (PDT)","From":"Naushir Patuck <naush@raspberrypi.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Mon, 20 Jul 2020 10:13:10 +0100","Message-Id":"<20200720091311.805092-9-naush@raspberrypi.com>","X-Mailer":"git-send-email 2.25.1","In-Reply-To":"<20200720091311.805092-1-naush@raspberrypi.com>","References":"<20200720091311.805092-1-naush@raspberrypi.com>","MIME-Version":"1.0","Subject":"[libcamera-devel] [PATCH v4 8/9] libcamera: pipeline: raspberrypi:\n\tAdd 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>\n---\n .../pipeline/raspberrypi/rpi_stream.cpp       | 70 ++++++++++++++++---\n .../pipeline/raspberrypi/rpi_stream.h         | 12 +++-\n 2 files changed, 71 insertions(+), 11 deletions(-)","diff":"diff --git a/src/libcamera/pipeline/raspberrypi/rpi_stream.cpp b/src/libcamera/pipeline/raspberrypi/rpi_stream.cpp\nindex 02f8d3e0..6635a751 100644\n--- a/src/libcamera/pipeline/raspberrypi/rpi_stream.cpp\n+++ b/src/libcamera/pipeline/raspberrypi/rpi_stream.cpp\n@@ -112,25 +112,34 @@ int RPiStream::queueBuffer(FrameBuffer *buffer)\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 requeue an internal buffer as soon\n+\t\t\t * as one becomes available.\n+\t\t\t */\n+\t\t\trequeueBuffers_.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 the buffer into the device.\n+\t */\n+\tif (requeueBuffers_.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 buffers to be queued, so this bufferm ust go on\n+\t * the waiting list.\n+\t */\n+\trequeueBuffers_.push(buffer);\n \n-\treturn ret;\n+\treturn 0;\n }\n \n void RPiStream::returnBuffer(FrameBuffer *buffer)\n@@ -138,7 +147,35 @@ void RPiStream::returnBuffer(FrameBuffer *buffer)\n \t/* This can only be called for external streams. */\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 buffers that are waiting to be queued?\n+\t * If so, do it now as availableBuffers_ will not be empty.\n+\t */\n+\twhile (!requeueBuffers_.empty()) {\n+\t\tFrameBuffer *buffer = requeueBuffers_.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\trequeueBuffers_.pop();\n+\t\tqueueToDevice(buffer);\n+\t}\n }\n \n bool RPiStream::findFrameBuffer(FrameBuffer *buffer) const\n@@ -155,10 +192,23 @@ bool RPiStream::findFrameBuffer(FrameBuffer *buffer) const\n void RPiStream::clearBuffers()\n {\n \tavailableBuffers_ = std::queue<FrameBuffer *>{};\n+\trequeueBuffers_ = 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 af9c2ad2..6222c867 100644\n--- a/src/libcamera/pipeline/raspberrypi/rpi_stream.h\n+++ b/src/libcamera/pipeline/raspberrypi/rpi_stream.h\n@@ -52,6 +52,7 @@ public:\n \n private:\n \tvoid clearBuffers();\n+\tint queueToDevice(FrameBuffer *buffer);\n \t/*\n \t * Indicates that this stream is active externally, i.e. the buffers\n \t * might be provided by (and returned to) the application.\n@@ -63,7 +64,7 @@ private:\n \tstd::string name_;\n \t/* The actual device stream. */\n \tstd::unique_ptr<V4L2VideoDevice> dev_;\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 \t/*\n \t * List of frame buffer that we can use if none have been provided by\n@@ -71,6 +72,15 @@ private:\n \t * buffers exported internally.\n \t */\n \tstd::queue<FrameBuffer *> availableBuffers_;\n+\t/*\n+\t * List of frame buffer that are to be re-queued into the device.\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 *> requeueBuffers_;\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","v4","8/9"]}