[{"id":21003,"web_url":"https://patchwork.libcamera.org/comment/21003/","msgid":"<YZZGndFCZ3w7+kIB@pendragon.ideasonboard.com>","date":"2021-11-18T12:27:09","subject":"Re: [libcamera-devel] [PATCH v2 2/3] pipeline: raspberrypi: Rework\n\tthe internal buffer allocation scheme","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Naush,\n\nThank you for the patch.\n\nOn Fri, Nov 12, 2021 at 10:03:04AM +0000, Naushir Patuck wrote:\n> For simplicity, the pipeline handler currently look at the maximum number of\n> buffers set in the StreamConfiguration by the user and allocate the same number\n> of internal buffers for all device nodes. This would likely overallocate buffers\n> for some nodes. Rework this logic to try and minimise overallcations without\n> compromising performance.\n> \n> The key change is to mostly decouple the number of internal buffers allocated\n> from number of buffers requested by the user through the StreamConfiguration.\n> \n> For ISP nodes, we only ever need 1 set of internal buffers, as the hardware runs\n> synchronous with the requests and IPA.\n> \n> For Unicam nodes, allocate a minimum for 4 buffers (exported + internal), but\n> also require at least 2 internal buffers to minimise frame drops.\n> \n> Signed-off-by: Naushir Patuck <naush@raspberrypi.com>\n> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> Reviewed-by: Umang Jain <umang.jain@ideasonboard.com>\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n> ---\n>  .../pipeline/raspberrypi/raspberrypi.cpp      | 43 ++++++++++++++-----\n>  1 file changed, 32 insertions(+), 11 deletions(-)\n> \n> diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> index 11d3c2b120dd..4f6c699a4379 100644\n> --- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> +++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> @@ -1211,21 +1211,42 @@ int PipelineHandlerRPi::queueAllBuffers(Camera *camera)\n>  int PipelineHandlerRPi::prepareBuffers(Camera *camera)\n>  {\n>  \tRPiCameraData *data = cameraData(camera);\n> +\tunsigned int numRawBuffers = 0;\n>  \tint ret;\n>  \n> -\t/*\n> -\t * Decide how many internal buffers to allocate. For now, simply look\n> -\t * at how many external buffers will be provided. We'll need to improve\n> -\t * this logic. However, we really must have all streams allocate the same\n> -\t * number of buffers to simplify error handling in queueRequestDevice().\n> -\t */\n> -\tunsigned int maxBuffers = 0;\n> -\tfor (const Stream *s : camera->streams())\n> -\t\tif (static_cast<const RPi::Stream *>(s)->isExternal())\n> -\t\t\tmaxBuffers = std::max(maxBuffers, s->configuration().bufferCount);\n> +\tfor (Stream *s : camera->streams()) {\n> +\t\tif (isRaw(s->configuration().pixelFormat)) {\n> +\t\t\tnumRawBuffers = s->configuration().bufferCount;\n> +\t\t\tbreak;\n> +\t\t}\n> +\t}\n>  \n> +\t/* Decide how many internal buffers to allocate. */\n>  \tfor (auto const stream : data->streams_) {\n> -\t\tret = stream->prepareBuffers(maxBuffers);\n> +\t\tunsigned int numBuffers;\n> +\n> +\t\tif (stream == &data->unicam_[Unicam::Image] ||\n> +\t\t    stream == &data->unicam_[Unicam::Embedded]) {\n> +\t\t\t/*\n> +\t\t\t * For Unicam, allocate a minimum of 4 buffers as we want\n> +\t\t\t * to avoid any frame drops. If an application has configured\n> +\t\t\t * a RAW stream, allocate additional buffers to make up the\n> +\t\t\t * minimum, but ensure we have at least 2 sets of internal\n> +\t\t\t * buffers to use to minimise frame drops.\n> +\t\t\t */\n> +\t\t\tconstexpr unsigned int minBuffers = 4;\n> +\t\t\tnumBuffers = std::max<int>(2, minBuffers - numRawBuffers);\n> +\t\t} else {\n> +\t\t\t/*\n> +\t\t\t * Since the ISP runs synchronous with the IPA and requests,\n> +\t\t\t * we only ever need one set of internal buffers. Any buffers\n> +\t\t\t * the application wants to hold onto will already be exported\n> +\t\t\t * through PipelineHandlerRPi::exportFrameBuffers().\n> +\t\t\t */\n> +\t\t\tnumBuffers = 1;\n> +\t\t}\n> +\n> +\t\tret = stream->prepareBuffers(numBuffers);\n>  \t\tif (ret < 0)\n>  \t\t\treturn ret;\n>  \t}","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 9CE4FBF415\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 18 Nov 2021 12:27:34 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 034E560231;\n\tThu, 18 Nov 2021 13:27:34 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 0E43B60231\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 18 Nov 2021 13:27:32 +0100 (CET)","from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi\n\t[62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 72487E7;\n\tThu, 18 Nov 2021 13:27:31 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"a7zqU848\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1637238451;\n\tbh=MfO5rwARH+uiQYgYfEfc2qzh9VTmVkX0HkFbjWwPnLE=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=a7zqU848qj6/l7/nrHfuJmVltXFSpO25jEikPWBOshFFVu6rVpDJSa6dJb29OKb/e\n\tMuZylpWjpKmLi13AqGs7NUiNmOGSGW6P9zd6IRIHWy4UaoMghraZpBNGGd0PNeqnkn\n\tNbCPx7uU+Kn6G6kQHY2qcZSurxEv8OWBMviTHis8=","Date":"Thu, 18 Nov 2021 14:27:09 +0200","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Naushir Patuck <naush@raspberrypi.com>","Message-ID":"<YZZGndFCZ3w7+kIB@pendragon.ideasonboard.com>","References":"<20211112100305.2217099-1-naush@raspberrypi.com>\n\t<20211112100305.2217099-3-naush@raspberrypi.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20211112100305.2217099-3-naush@raspberrypi.com>","Subject":"Re: [libcamera-devel] [PATCH v2 2/3] pipeline: raspberrypi: Rework\n\tthe internal buffer allocation scheme","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>","Cc":"libcamera-devel@lists.libcamera.org,\n\tRoman Stratiienko <r.stratiienko@gmail.com>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]