[{"id":22324,"web_url":"https://patchwork.libcamera.org/comment/22324/","msgid":"<164761342016.1484799.1079943824864025849@Monstersaurus>","date":"2022-03-18T14:23:40","subject":"Re: [libcamera-devel] [PATCH v2 3/6] pipeline: raspberrypi: Free\n\tbuffers in the RPiCamera destructor and re-configure","submitter":{"id":97,"url":"https://patchwork.libcamera.org/api/people/97/","name":"Nicolas Dufresne via libcamera-devel","email":"libcamera-devel@lists.libcamera.org"},"content":"Quoting Naushir Patuck via libcamera-devel (2022-03-17 14:08:24)\n> Currently, all framebuffer allocations get freed and cleared on a stop in\n> PipelineHandlerRPi::stopDevice(). If PipelineHandlerRPi::start() is then called\n> without an intermediate PipelineHandlerRPi::configure(), it will re-allocate and\n> prepare all the buffers again, which is unnecessary.\n> \n> Fix this by not freeing the buffer in PipelineHandlerRPi::stopDevice(), but\n> insted doing it in PipelineHandlerRPi::configure(), as the buffers might have\n> to be resized.\n> \n> Add a flag to indicate that buffer allocations need to be done on the next\n> call to PipelineHandlerRPi::start().\n> \n> Signed-off-by: Naushir Patuck <naush@raspberrypi.com>\n> Reviewed-by: David Plowman <david.plowman@raspberrypi.com>\n> Tested-by: David Plowman <david.plowman@raspberrypi.com>\n> ---\n>  .../pipeline/raspberrypi/raspberrypi.cpp      | 33 +++++++++++++------\n>  1 file changed, 23 insertions(+), 10 deletions(-)\n> \n> diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> index 2281b43fc3ac..082273828894 100644\n> --- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> +++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> @@ -185,10 +185,15 @@ public:\n>         RPiCameraData(PipelineHandler *pipe)\n>                 : Camera::Private(pipe), state_(State::Stopped),\n>                   supportsFlips_(false), flipsAlterBayerOrder_(false),\n> -                 dropFrameCount_(0), ispOutputCount_(0)\n> +                 dropFrameCount_(0), reallocate_(true), ispOutputCount_(0)\n>         {\n>         }\n>  \n> +       ~RPiCameraData()\n> +       {\n> +               freeBuffers();\n> +       }\n> +\n>         void freeBuffers();\n>         void frameStarted(uint32_t sequence);\n>  \n> @@ -280,6 +285,9 @@ public:\n>          */\n>         std::optional<int32_t> notifyGainsUnity_;\n>  \n> +       /* Has this camera been reconfigured? */\n> +       bool reallocate_;\n> +\n>  private:\n>         void checkRequestCompleted();\n>         void fillRequestMetadata(const ControlList &bufferControls,\n> @@ -682,7 +690,8 @@ int PipelineHandlerRPi::configure(Camera *camera, CameraConfiguration *config)\n>         RPiCameraData *data = cameraData(camera);\n>         int ret;\n>  \n> -       /* Start by resetting the Unicam and ISP stream states. */\n> +       /* Start by freeing all buffers and reset the Unicam and ISP stream states. */\n> +       data->freeBuffers();\n>         for (auto const stream : data->streams_)\n>                 stream->reset();\n>  \n> @@ -982,12 +991,16 @@ int PipelineHandlerRPi::start(Camera *camera, const ControlList *controls)\n>         RPiCameraData *data = cameraData(camera);\n>         int ret;\n>  \n> -       /* Allocate buffers for internal pipeline usage. */\n> -       ret = prepareBuffers(camera);\n> -       if (ret) {\n> -               LOG(RPI, Error) << \"Failed to allocate buffers\";\n> -               stop(camera);\n> -               return ret;\n> +       if (data->reallocate_) {\n\nOn the first iteration, this isn't a reallocation, so I would have\nprobably used a variable called 'hasBuffers_', and \n\tif (!data->hasBuffers_)\n\nor something to declare the state rather than the action.\n\nBut this looks like it works all the same.\n\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\n> +               /* Allocate buffers for internal pipeline usage. */\n> +               ret = prepareBuffers(camera);\n> +               if (ret) {\n> +                       LOG(RPI, Error) << \"Failed to allocate buffers\";\n> +                       data->freeBuffers();\n> +                       stop(camera);\n> +                       return ret;\n> +               }\n> +               data->reallocate_ = false;\n>         }\n>  \n>         /* Check if a ScalerCrop control was specified. */\n> @@ -1055,8 +1068,6 @@ void PipelineHandlerRPi::stopDevice(Camera *camera)\n>  \n>         /* Stop the IPA. */\n>         data->ipa_->stop();\n> -\n> -       data->freeBuffers();\n>  }\n>  \n>  int PipelineHandlerRPi::queueRequestDevice(Camera *camera, Request *request)\n> @@ -1461,6 +1472,8 @@ void RPiCameraData::freeBuffers()\n>  \n>         for (auto const stream : streams_)\n>                 stream->releaseBuffers();\n> +\n> +       reallocate_ = true;\n>  }\n>  \n>  void RPiCameraData::frameStarted(uint32_t sequence)\n> -- \n> 2.25.1\n>","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 B0BFDBDE17\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 18 Mar 2022 14:23:44 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 2ED30604E8;\n\tFri, 18 Mar 2022 15:23:44 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 40EA4601F5\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 18 Mar 2022 15:23:43 +0100 (CET)","from pendragon.ideasonboard.com\n\t(cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id C6282EE;\n\tFri, 18 Mar 2022 15:23:42 +0100 (CET)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1647613424;\n\tbh=kO9ipQVqSNY3z4qk2tHV+G8hEB9TQAsD/PLb6VXNz/I=;\n\th=In-Reply-To:References:To:Date:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:\n\tFrom;\n\tb=DbpLkRVjGo3hwHlsBRqwpuZs1d1llx7T5JVFy3gPCcInfBX7L/oP3Dpgqgzd1JZWv\n\tOzMFzd27YnUGyXSQ+SnydOuvJ8i3uu5BH57std0+tJpENp727cMKKCFbnV81xpyFoh\n\tFvtunuyz+Flff5sbu2Xlw483F0/TL9AO3FacpUl2S1CLDltso4YQJZudgXONU5xS/u\n\tbq9Sa8heEHi9hhPzXNQaRZam/pUitbmyATS9ajrbEk4Lyv6xdgv2UgQwz1EtuDcSSv\n\t8HDD4rrQvr15bRCuKzUH41xi4dxiuKsBs43MxmhXDAp4ieP3GSQuKgk2W7U0l8fT3U\n\t0iZ1AW5q7CI5A==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1647613422;\n\tbh=kO9ipQVqSNY3z4qk2tHV+G8hEB9TQAsD/PLb6VXNz/I=;\n\th=In-Reply-To:References:Subject:From:To:Date:From;\n\tb=XKP/gM+QIZy7JkSKUmfVegugwC3UMNlt33H4aXDGBeotkw/oHtfufwe6qPXDctGPF\n\t7p7XK5j2/RfYkIGHhSKD/zWxhTfopsC4B9xANNfEE0Zx17v4RDS3FI1TnNs04TWmfy\n\tC+mnnxdBurSPmL+4GIwT+4rtiQSHcUf1OJqHBXPU="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"XKP/gM+Q\"; dkim-atps=neutral","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","In-Reply-To":"<20220317140827.1835029-4-naush@raspberrypi.com>","References":"<20220317140827.1835029-1-naush@raspberrypi.com>\n\t<20220317140827.1835029-4-naush@raspberrypi.com>","To":"Naushir Patuck <naush@raspberrypi.com>,\n\tlibcamera-devel@lists.libcamera.org","Date":"Fri, 18 Mar 2022 14:23:40 +0000","Message-ID":"<164761342016.1484799.1079943824864025849@Monstersaurus>","User-Agent":"alot/0.10","Subject":"Re: [libcamera-devel] [PATCH v2 3/6] pipeline: raspberrypi: Free\n\tbuffers in the RPiCamera destructor and re-configure","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>","From":"Kieran Bingham via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]