{"id":25603,"url":"https://patchwork.libcamera.org/api/patches/25603/?format=json","web_url":"https://patchwork.libcamera.org/patch/25603/","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":"<20251218123524.130886-3-naush@raspberrypi.com>","date":"2025-12-18T12:31:24","name":"[v2,2/2] pipeline: rpi: Rework internal buffer allocations","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"89394f7de3ffb106a328fb121c76a571454eb115","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/25603/mbox/","series":[{"id":5671,"url":"https://patchwork.libcamera.org/api/series/5671/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=5671","date":"2025-12-18T12:31:22","name":"RPi: Internal buffer alloaction rework","version":2,"mbox":"https://patchwork.libcamera.org/series/5671/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/25603/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/25603/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 07738C3272\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 18 Dec 2025 12:35:38 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 95C7161B10;\n\tThu, 18 Dec 2025 13:35:37 +0100 (CET)","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 A9CF261603\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 18 Dec 2025 13:35:34 +0100 (CET)","by mail-wm1-x330.google.com with SMTP id\n\t5b1f17b1804b1-47798f4059fso681695e9.2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 18 Dec 2025 04:35:34 -0800 (PST)","from naush-dell.pitowers.org\n\t([2a00:1098:3142:1f:1670:643a:b830:51f7])\n\tby smtp.gmail.com with ESMTPSA id\n\tffacd0b85a97d-4324493fd73sm4769434f8f.13.2025.12.18.04.35.33\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tThu, 18 Dec 2025 04:35:33 -0800 (PST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (2048-bit key;\n\tunprotected) header.d=raspberrypi.com header.i=@raspberrypi.com\n\theader.b=\"eA0iv59j\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=raspberrypi.com; s=google; t=1766061334; x=1766666134;\n\tdarn=lists.libcamera.org; \n\th=content-transfer-encoding:mime-version:references:in-reply-to\n\t:message-id:date:subject:cc:to:from:from:to:cc:subject:date\n\t:message-id:reply-to;\n\tbh=BOdXiYWfo8kvoAXoNdJXmnlFHzLYsyEj3I+WNR70pss=;\n\tb=eA0iv59j3tu+TP+52CbOVj/iobWP4hDwECe0gFmTbl3e8tfBvJ0OiARCRGri1UX8W/\n\t354YTaWK4AUQKB5wegPdbXDMb2FEciQlPaZRF65khEmX+o4Y0iWEzR2gdY7UYhY9Mjqk\n\toqZFaUP58lF16EIC7tSMAlpLpeS1oXQRdq7DsW5N9c6MsnRlFxXtF7iikYgqD0kOEj8q\n\tryorjPV38re1e6IffSf5/ZBYGmwP1OX2CXE1fh7EXP9N6u/OziZBqW5ql4rVGkQDTkEp\n\t9adsBxLtld9s07PaHQC2olkyhw3mVDpO29I12UIu9ebXZzSEqd0zuPt4mopTWLpZ5oft\n\tPH5Q==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20230601; t=1766061334; x=1766666134;\n\th=content-transfer-encoding:mime-version:references:in-reply-to\n\t:message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from\n\t:to:cc:subject:date:message-id:reply-to;\n\tbh=BOdXiYWfo8kvoAXoNdJXmnlFHzLYsyEj3I+WNR70pss=;\n\tb=G8u137YABROkBvB72zlCzTfZQQcyAesMvNs0xQliTc5YQol2KFPmH1ziRyhj7C0Egc\n\t3eiQLUnkyCztOh56f3KAnOl0QyWE60B09eR91JwgMyOfMySF1hvAvjN5H2dbj5pIz3uN\n\tzhxa1gH+0HqNSOAK2huTTLKtxLYrY6pXnEDXEHJRelv7/sIAbKunX2NJigiBHsj5fuaP\n\tfFk2KR8/Vx0/Q9dK9/k2L63VlavUanjnAiaKntbZH781KgeWHLdZ3s/JjJevGOIOhk1L\n\tPmtLj1SR6TdrQcA3R7Yf3ABbqX+stzSrnZCEhG0j9OVcqvCTgSxmsUWtNCMa5AuOD/sa\n\t7dRg==","X-Gm-Message-State":"AOJu0YyyInywWDYZF5C1zYQRQK0HSCOHBO2yDYmfN5+ZiTsGIOaAd5P7\n\tRtScUSd79SHK/maf/KZjwZONfzk1QfU7888OMK7ZQTNUTo8l5b+LX/OogRcy9HyL3OrxMJlrEwl\n\tzQKpQ","X-Gm-Gg":"AY/fxX4c7Abxk4H3q0nw9O1f0rIuLJqAcRuJJFd0Dgh5mhWjxS4V4wF7PoTeA99wS4F\n\tCMbopeBaaylsn68raCFp6T3lT1CGqqjRD1bDzBxNSTaDWUH/xfonh6DtLcLvixh5eD6h2/qvStf\n\trWOV51wyLNReeq496O+y2h0nVG0tlUa0IsvBhdIkk7KCcB4Dkg4WoUcMTN23TFloAy4o1fYI0sR\n\tCCjDPnA9wiSUrVyx1GXhLchrz2OCErgd9fdgtgr6RlBIEKB64mKZN11uk3xMoj5u66EnJ7QL8ep\n\tlVgoN14WgaXO2ro9CBRbz0acPJkJcM1Ct/JwOkEcxfWK5ns73cxW1yPmCUtAw0tFFNam8EzazEK\n\t9/8WbQXsbznFAinkmHikf6kc3ZBY9VFgytMEh6kb4CJ1+a92OU48dqeB6HpKKb8glD/Bi7ItF8V\n\t59brBEUMQkg/38TB1LrA77aFODVMuS5VU=","X-Google-Smtp-Source":"AGHT+IEkx/8wiQuTTdzZj3v8IZijtccJkzl4mwFEhXaxvr1EY13Z15aMRgtPfHmBScNckCH87oRElA==","X-Received":"by 2002:a05:600c:8707:b0:477:5b01:7d42 with SMTP id\n\t5b1f17b1804b1-47be3ced2bdmr11744905e9.5.1766061333879; \n\tThu, 18 Dec 2025 04:35:33 -0800 (PST)","From":"Naushir Patuck <naush@raspberrypi.com>","To":"libcamera-devel@lists.libcamera.org","Cc":"Jacopo Mondi <jacopo.mondi@ideasonboard.com>, Naushir Patuck\n\t<naush@raspberrypi.com>, =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?=\n\t<barnabas.pocze@ideasonboard.com>","Subject":"[PATCH v2 2/2] pipeline: rpi: Rework internal buffer allocations","Date":"Thu, 18 Dec 2025 12:31:24 +0000","Message-ID":"<20251218123524.130886-3-naush@raspberrypi.com>","X-Mailer":"git-send-email 2.51.0","In-Reply-To":"<20251218123524.130886-1-naush@raspberrypi.com>","References":"<20251218123524.130886-1-naush@raspberrypi.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=UTF-8","Content-Transfer-Encoding":"8bit","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>"},"content":"In order to clear the V4L2VideoDevice cache, we must call\nV4L2VideoDevice::releaseBuffers() when stopping the camera. To do this\nwithout releasing the allocated buffers, we have to rework the internal\nbuffer allocation logic.\n\nRemove calls to V4L2VideoDevice::importBuffers() and releaseBuffers()\nfrom within the RPi::Stream class. Instead, move these calls to the\nPipelineHandlerBase class, where we can call releaseBuffers(), i.e. clear\nthe V4L2VideoDevice cache unconditionally on stop without de-allocating\nthe internal buffers.\n\nThe loigc Stream::clearBuffers() can be then moved into releaseBuffers()\nwhich will actually de-allocate the internal buffers when required.\n\nCloses: https://gitlab.freedesktop.org/camera/libcamera/-/issues/265\nSigned-off-by: Naushir Patuck <naush@raspberrypi.com>\nTested-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com> # rpi4\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(-)","diff":"diff --git a/src/libcamera/pipeline/rpi/common/pipeline_base.cpp b/src/libcamera/pipeline/rpi/common/pipeline_base.cpp\nindex 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 \ndiff --git a/src/libcamera/pipeline/rpi/common/rpi_stream.cpp b/src/libcamera/pipeline/rpi/common/rpi_stream.cpp\nindex 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)\ndiff --git a/src/libcamera/pipeline/rpi/common/rpi_stream.h b/src/libcamera/pipeline/rpi/common/rpi_stream.h\nindex 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","prefixes":["v2","2/2"]}