[{"id":37439,"web_url":"https://patchwork.libcamera.org/comment/37439/","msgid":"<cesxjtnipvnj6y4ukohtlabouxgvp3rnus6disezqgrjwfhh42@7pv4sp2uvwyz>","date":"2025-12-18T16:42:17","subject":"Re: [PATCH v2 2/2] pipeline: rpi: Rework internal buffer allocations","submitter":{"id":143,"url":"https://patchwork.libcamera.org/api/people/143/","name":"Jacopo Mondi","email":"jacopo.mondi@ideasonboard.com"},"content":"Hi Naush\n\nOn Thu, Dec 18, 2025 at 12:31:24PM +0000, Naushir Patuck wrote:\n> In order to clear the V4L2VideoDevice cache, we must call\n> V4L2VideoDevice::releaseBuffers() when stopping the camera. To do this\n> without releasing the allocated buffers, we have to rework the internal\n> buffer allocation logic.\n>\n> Remove calls to V4L2VideoDevice::importBuffers() and releaseBuffers()\n> from within the RPi::Stream class. Instead, move these calls to the\n> PipelineHandlerBase class, where we can call releaseBuffers(), i.e. clear\n> the V4L2VideoDevice cache unconditionally on stop without de-allocating\n> the internal buffers.\n>\n> The loigc Stream::clearBuffers() can be then moved into releaseBuffers()\n> which will actually de-allocate the internal buffers when required.\n>\n> Closes: https://gitlab.freedesktop.org/camera/libcamera/-/issues/265\n> Signed-off-by: Naushir Patuck <naush@raspberrypi.com>\n> Tested-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com> # rpi4\n\nThanks\nReviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>\n\n> ---\n>  .../pipeline/rpi/common/pipeline_base.cpp     |  8 ++++++-\n>  .../pipeline/rpi/common/rpi_stream.cpp        | 23 ++++++-------------\n>  .../pipeline/rpi/common/rpi_stream.h          |  1 -\n>  3 files changed, 14 insertions(+), 18 deletions(-)\n>\n> diff --git a/src/libcamera/pipeline/rpi/common/pipeline_base.cpp b/src/libcamera/pipeline/rpi/common/pipeline_base.cpp\n> index 2b61b5d241c5..aa0af367d301 100644\n> --- a/src/libcamera/pipeline/rpi/common/pipeline_base.cpp\n> +++ b/src/libcamera/pipeline/rpi/common/pipeline_base.cpp\n> @@ -719,8 +719,10 @@ void PipelineHandlerBase::stopDevice(Camera *camera)\n>  \tdata->state_ = CameraData::State::Stopped;\n>  \tdata->platformStop();\n>\n> -\tfor (auto const stream : data->streams_)\n> +\tfor (auto const stream : data->streams_) {\n>  \t\tstream->dev()->streamOff();\n> +\t\tstream->dev()->releaseBuffers();\n> +\t}\n>\n>  \t/* Disable SOF event generation. */\n>  \tdata->frontendDevice()->setFrameStartEnabled(false);\n> @@ -901,6 +903,10 @@ int PipelineHandlerBase::queueAllBuffers(Camera *camera)\n>  \tint ret;\n>\n>  \tfor (auto const stream : data->streams_) {\n> +\t\tret = stream->dev()->importBuffers(VIDEO_MAX_FRAME);\n> +\t\tif (ret < 0)\n> +\t\t\treturn ret;\n> +\n>  \t\tif (stream->getFlags() & StreamFlag::External)\n>  \t\t\tcontinue;\n>\n> diff --git a/src/libcamera/pipeline/rpi/common/rpi_stream.cpp b/src/libcamera/pipeline/rpi/common/rpi_stream.cpp\n> index e73f4b7d31af..f420400dfe18 100644\n> --- a/src/libcamera/pipeline/rpi/common/rpi_stream.cpp\n> +++ b/src/libcamera/pipeline/rpi/common/rpi_stream.cpp\n> @@ -12,9 +12,6 @@\n>\n>  #include <libcamera/base/log.h>\n>\n> -/* Maximum number of buffer slots to allocate in the V4L2 device driver. */\n> -static constexpr unsigned int maxV4L2BufferCount = 32;\n> -\n>  namespace libcamera {\n>\n>  LOG_DEFINE_CATEGORY(RPISTREAM)\n> @@ -108,7 +105,7 @@ void Stream::setExportedBuffer(FrameBuffer *buffer)\n>\n>  int Stream::allocateBuffers(unsigned int count)\n>  {\n> -\tint ret;\n> +\tint ret = 0;\n>\n>  \tif (!(flags_ & StreamFlag::ImportOnly)) {\n>  \t\t/* Export some frame buffers for internal use. */\n> @@ -121,7 +118,7 @@ int Stream::allocateBuffers(unsigned int count)\n>  \t\tresetBuffers();\n>  \t}\n>\n> -\treturn dev_->importBuffers(maxV4L2BufferCount);\n> +\treturn ret;\n>  }\n>\n>  int Stream::queueBuffer(FrameBuffer *buffer)\n> @@ -243,8 +240,11 @@ int Stream::queueAllBuffers()\n>\n>  void Stream::releaseBuffers()\n>  {\n> -\tdev_->releaseBuffers();\n> -\tclearBuffers();\n> +\tavailableBuffers_ = std::queue<FrameBuffer *>{};\n> +\trequestBuffers_ = std::queue<FrameBuffer *>{};\n> +\tinternalBuffers_.clear();\n> +\tbufferMap_.clear();\n> +\tid_ = 0;\n>  }\n>\n>  void Stream::bufferEmplace(unsigned int id, FrameBuffer *buffer)\n> @@ -257,15 +257,6 @@ void Stream::bufferEmplace(unsigned int id, FrameBuffer *buffer)\n>  \t\t\t\t   std::forward_as_tuple(buffer, false));\n>  }\n>\n> -void Stream::clearBuffers()\n> -{\n> -\tavailableBuffers_ = std::queue<FrameBuffer *>{};\n> -\trequestBuffers_ = std::queue<FrameBuffer *>{};\n> -\tinternalBuffers_.clear();\n> -\tbufferMap_.clear();\n> -\tid_ = 0;\n> -}\n> -\n>  int Stream::queueToDevice(FrameBuffer *buffer)\n>  {\n>  \tLOG(RPISTREAM, Debug) << \"Queuing buffer \" << getBufferId(buffer)\n> diff --git a/src/libcamera/pipeline/rpi/common/rpi_stream.h b/src/libcamera/pipeline/rpi/common/rpi_stream.h\n> index c267447e5ab5..300a352a7d39 100644\n> --- a/src/libcamera/pipeline/rpi/common/rpi_stream.h\n> +++ b/src/libcamera/pipeline/rpi/common/rpi_stream.h\n> @@ -140,7 +140,6 @@ public:\n>\n>  private:\n>  \tvoid bufferEmplace(unsigned int id, FrameBuffer *buffer);\n> -\tvoid clearBuffers();\n>  \tint queueToDevice(FrameBuffer *buffer);\n>\n>  \tStreamFlags flags_;\n> --\n> 2.51.0\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 21132C3257\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 18 Dec 2025 16:42:22 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 4CBFB61F49;\n\tThu, 18 Dec 2025 17:42:21 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id C9C1861A61\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 18 Dec 2025 17:42:19 +0100 (CET)","from ideasonboard.com (93-46-82-201.ip106.fastwebnet.it\n\t[93.46.82.201])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 7F0671189;\n\tThu, 18 Dec 2025 17:42:12 +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=\"iZr5aTsy\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1766076132;\n\tbh=DTz+/QW0EBp61PKwpITKGI61+Cd5CAszdGueJKICM8Y=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=iZr5aTsyGQ7M/0OO7LRfylC1pYSuEXHbc/pA1f8t7C/ClPS8zE5iQYtBCPwVzWssH\n\tHmSDJlnzsV7Pu5m5CYZ1NMICZ+CzkI87nrarfyjJB4EZe0CnlLpt+KgG3cc6HH6tjt\n\tAoICbMdV/cAHbYwBYPMFGeR67Poti6pTFSoJfUj4=","Date":"Thu, 18 Dec 2025 17:42:17 +0100","From":"Jacopo Mondi <jacopo.mondi@ideasonboard.com>","To":"Naushir Patuck <naush@raspberrypi.com>","Cc":"libcamera-devel@lists.libcamera.org, Jacopo Mondi\n\t<jacopo.mondi@ideasonboard.com>, =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?=\n\t<barnabas.pocze@ideasonboard.com>","Subject":"Re: [PATCH v2 2/2] pipeline: rpi: Rework internal buffer allocations","Message-ID":"<cesxjtnipvnj6y4ukohtlabouxgvp3rnus6disezqgrjwfhh42@7pv4sp2uvwyz>","References":"<20251218123524.130886-1-naush@raspberrypi.com>\n\t<20251218123524.130886-3-naush@raspberrypi.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<20251218123524.130886-3-naush@raspberrypi.com>","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>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]