[{"id":22333,"web_url":"https://patchwork.libcamera.org/comment/22333/","msgid":"<Yjh29jNbNCaU2Vce@pendragon.ideasonboard.com>","date":"2022-03-21T13:00:38","subject":"Re: [libcamera-devel] [PATCH v3 3/5] pipeline: raspberrypi: Free\n\tbuffers in the RPiCamera destructor and re-configure","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 Mon, Mar 21, 2022 at 10:27:00AM +0000, Naushir Patuck via libcamera-devel wrote:\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> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\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..92043962494b 100644\n> --- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> +++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> @@ -185,10 +185,15 @@ public:\n>  \tRPiCameraData(PipelineHandler *pipe)\n>  \t\t: Camera::Private(pipe), state_(State::Stopped),\n>  \t\t  supportsFlips_(false), flipsAlterBayerOrder_(false),\n> -\t\t  dropFrameCount_(0), ispOutputCount_(0)\n> +\t\t  dropFrameCount_(0), buffersAllocated_(false), ispOutputCount_(0)\n>  \t{\n>  \t}\n>  \n> +\t~RPiCameraData()\n> +\t{\n> +\t\tfreeBuffers();\n> +\t}\n> +\n>  \tvoid freeBuffers();\n>  \tvoid frameStarted(uint32_t sequence);\n>  \n> @@ -280,6 +285,9 @@ public:\n>  \t */\n>  \tstd::optional<int32_t> notifyGainsUnity_;\n>  \n> +\t/* Have internal buffers been allocated? */\n> +\tbool buffersAllocated_;\n> +\n>  private:\n>  \tvoid checkRequestCompleted();\n>  \tvoid fillRequestMetadata(const ControlList &bufferControls,\n> @@ -682,7 +690,8 @@ int PipelineHandlerRPi::configure(Camera *camera, CameraConfiguration *config)\n>  \tRPiCameraData *data = cameraData(camera);\n>  \tint ret;\n>  \n> -\t/* Start by resetting the Unicam and ISP stream states. */\n> +\t/* Start by freeing all buffers and reset the Unicam and ISP stream states. */\n> +\tdata->freeBuffers();\n>  \tfor (auto const stream : data->streams_)\n>  \t\tstream->reset();\n>  \n> @@ -982,12 +991,16 @@ int PipelineHandlerRPi::start(Camera *camera, const ControlList *controls)\n>  \tRPiCameraData *data = cameraData(camera);\n>  \tint ret;\n>  \n> -\t/* Allocate buffers for internal pipeline usage. */\n> -\tret = prepareBuffers(camera);\n> -\tif (ret) {\n> -\t\tLOG(RPI, Error) << \"Failed to allocate buffers\";\n> -\t\tstop(camera);\n> -\t\treturn ret;\n> +\tif (!data->buffersAllocated_) {\n> +\t\t/* Allocate buffers for internal pipeline usage. */\n> +\t\tret = prepareBuffers(camera);\n> +\t\tif (ret) {\n> +\t\t\tLOG(RPI, Error) << \"Failed to allocate buffers\";\n> +\t\t\tdata->freeBuffers();\n> +\t\t\tstop(camera);\n> +\t\t\treturn ret;\n> +\t\t}\n> +\t\tdata->buffersAllocated_ = true;\n>  \t}\n>  \n>  \t/* Check if a ScalerCrop control was specified. */\n> @@ -1055,8 +1068,6 @@ void PipelineHandlerRPi::stopDevice(Camera *camera)\n>  \n>  \t/* Stop the IPA. */\n>  \tdata->ipa_->stop();\n> -\n> -\tdata->freeBuffers();\n>  }\n>  \n>  int PipelineHandlerRPi::queueRequestDevice(Camera *camera, Request *request)\n> @@ -1461,6 +1472,8 @@ void RPiCameraData::freeBuffers()\n>  \n>  \tfor (auto const stream : streams_)\n>  \t\tstream->releaseBuffers();\n> +\n> +\tbuffersAllocated_ = false;\n>  }\n>  \n>  void RPiCameraData::frameStarted(uint32_t sequence)","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 AD800BD80A\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 21 Mar 2022 13:00:58 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id D38BF604DC;\n\tMon, 21 Mar 2022 14:00:57 +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 20B20604C6\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 21 Mar 2022 14:00:56 +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 9B4FB291;\n\tMon, 21 Mar 2022 14:00:55 +0100 (CET)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1647867657;\n\tbh=Y0hqKGeODii0iPLD/uxJkaW23Z8vzeUWB+ess6/GfMs=;\n\th=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=K5ecdH3MzZxcE93VuAvnrpROcRp1Ri3gF+MnygSPtiID6mZ5P7b9fcQUfrmX79MPn\n\toflogOtI02DQINI5Pe7lGtZd944QG6dLzWEnxCrL6EU+r5rEBeHnYwyj49N4hnhEYN\n\tCMw9NDXiHXw83p7J0kMmwPr9jMkQu1ijTFX8EYg6Uc+oRQr6nGHomzaXTOeyMgdoxb\n\tqFk/08Fydhx0z0TaaVKfOz9tkXOR+25BYvxHzTjto0BCIFQPWj5oOx0ZrbA7nGqxt6\n\tTSol6GElcDLXaulTuZF76jkao1bswKfpT0U782mgvJsFj3FzqWK1skn/QkyeKKypqb\n\t/Gblhz57Ho5bA==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1647867655;\n\tbh=Y0hqKGeODii0iPLD/uxJkaW23Z8vzeUWB+ess6/GfMs=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=ban1tNOdIMVFK2y/NG9KAE6RBVe80RGOI3yx2MLO6Sw5B+rQmWTCpRWRrk3kcUBd1\n\tYXycUlRJlPMa5r4vxR5Det0T3zIcOTF5mHxTLydsUcusmnpjG0fwGUTBtZruAZmvWU\n\tcz2lpZa1yYcJtMQtbWEMs3B/TCBrbiInU2J9ppvg="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"ban1tNOd\"; dkim-atps=neutral","Date":"Mon, 21 Mar 2022 15:00:38 +0200","To":"Naushir Patuck <naush@raspberrypi.com>","Message-ID":"<Yjh29jNbNCaU2Vce@pendragon.ideasonboard.com>","References":"<20220321102702.1753800-1-naush@raspberrypi.com>\n\t<20220321102702.1753800-4-naush@raspberrypi.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20220321102702.1753800-4-naush@raspberrypi.com>","Subject":"Re: [libcamera-devel] [PATCH v3 3/5] 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":"Laurent Pinchart via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]