From patchwork Wed Dec 10 13:09:14 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Naushir Patuck X-Patchwork-Id: 25453 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 059BFC326B for ; Wed, 10 Dec 2025 13:13:12 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 2785961469; Wed, 10 Dec 2025 14:13:10 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=raspberrypi.com header.i=@raspberrypi.com header.b="m5hkXQ8Q"; dkim-atps=neutral Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 70579613F9 for ; Wed, 10 Dec 2025 14:13:07 +0100 (CET) Received: by mail-wr1-x430.google.com with SMTP id ffacd0b85a97d-42b2cff817aso653339f8f.0 for ; Wed, 10 Dec 2025 05:13:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raspberrypi.com; s=google; t=1765372387; x=1765977187; darn=lists.libcamera.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=E7Y5d44nPuHEoo5H5eQlzNCzdHZ7U0jIFghRTYAhT9w=; b=m5hkXQ8Q1sYgZVaSskgyaOhl5fHPf8cvmkpm5YcdNrb2GdODk8CUVVmxAL7u9wwZMn 6IdWjqnnlc8HX3EjevmngzwAr0LPp3SMhW7R2RvIOIWuypXQkE9xJE7fIG3Dd9L6ttmq 2YDljzieuxdLEL7TN637FNG62ngt8Cra2qVP9x4xSGEbhcZg/CSQTVPRyQAwgd9J2rGM CCQaFDMeDAO2+uLvIDPyl4QmsvJ2ZPpwMmvpLB7vUEqi6TM60CZuid2wg0HmAL1xShrz YBPmCQMy/HEzX7JiYC5oiuAKd8Huxn0OYUlX7u2HaaMJ1hCYFDyWXI+Lwaw0PjbHLZaw a+kg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765372387; x=1765977187; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=E7Y5d44nPuHEoo5H5eQlzNCzdHZ7U0jIFghRTYAhT9w=; b=ERgRqrH7y02vyBaU7kQnG87AxvewCbajXBQR2VVuMM6DIQ66+KtVLtlUviJBrZO0Ar rZtewzaIR9ys7bxEwKjvszzQYxJDERYWLoz3Aw3Aa3BXShV1adJeLAKyk1qND7NxhFDO ZyqwhQXdiAyLkgvGvu7Y+qt44dq509IbBaOcg3awaDrhCy5I2HzuOWHnGkseI99sM4Lj e9/ppXQlMAIgB8zbr27k0Yz70e2SCTJz/ICKol9k//ER9h9WfEWNGjKFUNH8EZsF6gMI czClVilxq7g3J3TAAP6Akt55SW/RVSQcB5//WCGosSbxPrjT8pASHFAe34u2VWpk0Vj8 tZ4w== X-Gm-Message-State: AOJu0YyWqGKS3XHzMTfIKBDPmzaFw/VRJceybEGnyczPEym/meDglBzC CMoNajRXg1sYTHbMcs5w7RUALU6UoGw57VPsuzYnFYZvqAC1OzCbO8mbf+FtK4vdPNNlrKJS/bL JcZM+dQc= X-Gm-Gg: AY/fxX40WeCGg8crGmwY56GcHrYTVMX14fRzH8qja+m0R3r4xg86hc0zNbS2CQhjW1+ BJpuQQZKD5r/jFDY9VV8Iz31xdjq6IJltmBGmbxmJahnvIx4hyoebL1HhiXzMNgg4Q2eD4hqEMK bA9IUeixgmNlhyokwUqzlPbRxPTJ8CnS2Un8HjRr7zFUnaCHil2PkgEEOLxwFH/9eD01kCwBJyG Qg7EeFgKtGzu0AygMiExhLnVgCJ4nBKct1cPLyAYLDussKuPsTx5VebuMdHlU5XEPSZp7j2bIDX qtszXBNc4SG+iDrMAOqdwn5HDYOotLaqfh6JXNWrM53sKC1jj5nX4+EGzEvouUTOTl+NC6DJTFr z3yRyATNV9ToxI2XfZdtXxbbRbokzHNjpcFy6CG2We7RJlGg4aPb+HAm+NmF9/zN3D2riM3AN+f yRWwJ0PQ0wIN7TeoMXJNefSHiclupz9iz9l2yBf4UsBg== X-Google-Smtp-Source: AGHT+IGPoHs7nhlJ1MGToD2ByYER9LjyKr0SJzAWKF6fBQjPohF7psOeechCc6vz9QUYwu9Vm1XAow== X-Received: by 2002:a05:6000:288a:b0:429:ebf5:ff1d with SMTP id ffacd0b85a97d-42fa399ba80mr1319332f8f.0.1765372386629; Wed, 10 Dec 2025 05:13:06 -0800 (PST) Received: from naush-dell.pitowers.org ([2a00:1098:3142:1f:2883:e222:5478:b981]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-42f7cbfee71sm37871852f8f.15.2025.12.10.05.13.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Dec 2025 05:13:06 -0800 (PST) From: Naushir Patuck To: libcamera-devel@lists.libcamera.org Cc: Naushir Patuck Subject: [PATCH v1 2/2] pipeline: rpi: Rework internal buffer allocations Date: Wed, 10 Dec 2025 13:09:14 +0000 Message-ID: <20251210131302.81887-3-naush@raspberrypi.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251210131302.81887-1-naush@raspberrypi.com> References: <20251210131302.81887-1-naush@raspberrypi.com> MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" In order to clear the V4L2VideoDevice cache, we must call V4L2VideoDevice::releaseBuffers() when stopping the camera. To do this without releasing the allocated buffers, we have to rework the internal buffer allocation logic. Remove calls to V4L2VideoDevice::importBuffers() and releaseBuffers() from within the RPi::Stream class. Instead, move these calls to the PipelineHandlerBase class, where we can call releaseBuffers(), i.e. clear the V4L2VideoDevice cache unconditionally on stop without de-allocating the internal buffers. The loigc Stream::clearBuffers() can be then moved into releaseBuffers() which will actually de-allocate the internal buffers when required. Closes: https://gitlab.freedesktop.org/camera/libcamera/-/issues/265 Signed-off-by: Naushir Patuck Tested-by: Barnabás Pőcze # rpi4 --- .../pipeline/rpi/common/pipeline_base.cpp | 8 ++++++- .../pipeline/rpi/common/rpi_stream.cpp | 23 ++++++------------- .../pipeline/rpi/common/rpi_stream.h | 1 - 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/libcamera/pipeline/rpi/common/pipeline_base.cpp b/src/libcamera/pipeline/rpi/common/pipeline_base.cpp index 2b61b5d241c5..aa0af367d301 100644 --- a/src/libcamera/pipeline/rpi/common/pipeline_base.cpp +++ b/src/libcamera/pipeline/rpi/common/pipeline_base.cpp @@ -719,8 +719,10 @@ void PipelineHandlerBase::stopDevice(Camera *camera) data->state_ = CameraData::State::Stopped; data->platformStop(); - for (auto const stream : data->streams_) + for (auto const stream : data->streams_) { stream->dev()->streamOff(); + stream->dev()->releaseBuffers(); + } /* Disable SOF event generation. */ data->frontendDevice()->setFrameStartEnabled(false); @@ -901,6 +903,10 @@ int PipelineHandlerBase::queueAllBuffers(Camera *camera) int ret; for (auto const stream : data->streams_) { + ret = stream->dev()->importBuffers(VIDEO_MAX_FRAME); + if (ret < 0) + return ret; + if (stream->getFlags() & StreamFlag::External) continue; diff --git a/src/libcamera/pipeline/rpi/common/rpi_stream.cpp b/src/libcamera/pipeline/rpi/common/rpi_stream.cpp index e73f4b7d31af..f420400dfe18 100644 --- a/src/libcamera/pipeline/rpi/common/rpi_stream.cpp +++ b/src/libcamera/pipeline/rpi/common/rpi_stream.cpp @@ -12,9 +12,6 @@ #include -/* Maximum number of buffer slots to allocate in the V4L2 device driver. */ -static constexpr unsigned int maxV4L2BufferCount = 32; - namespace libcamera { LOG_DEFINE_CATEGORY(RPISTREAM) @@ -108,7 +105,7 @@ void Stream::setExportedBuffer(FrameBuffer *buffer) int Stream::allocateBuffers(unsigned int count) { - int ret; + int ret = 0; if (!(flags_ & StreamFlag::ImportOnly)) { /* Export some frame buffers for internal use. */ @@ -121,7 +118,7 @@ int Stream::allocateBuffers(unsigned int count) resetBuffers(); } - return dev_->importBuffers(maxV4L2BufferCount); + return ret; } int Stream::queueBuffer(FrameBuffer *buffer) @@ -243,8 +240,11 @@ int Stream::queueAllBuffers() void Stream::releaseBuffers() { - dev_->releaseBuffers(); - clearBuffers(); + availableBuffers_ = std::queue{}; + requestBuffers_ = std::queue{}; + internalBuffers_.clear(); + bufferMap_.clear(); + id_ = 0; } void Stream::bufferEmplace(unsigned int id, FrameBuffer *buffer) @@ -257,15 +257,6 @@ void Stream::bufferEmplace(unsigned int id, FrameBuffer *buffer) std::forward_as_tuple(buffer, false)); } -void Stream::clearBuffers() -{ - availableBuffers_ = std::queue{}; - requestBuffers_ = std::queue{}; - internalBuffers_.clear(); - bufferMap_.clear(); - id_ = 0; -} - int Stream::queueToDevice(FrameBuffer *buffer) { LOG(RPISTREAM, Debug) << "Queuing buffer " << getBufferId(buffer) diff --git a/src/libcamera/pipeline/rpi/common/rpi_stream.h b/src/libcamera/pipeline/rpi/common/rpi_stream.h index c267447e5ab5..300a352a7d39 100644 --- a/src/libcamera/pipeline/rpi/common/rpi_stream.h +++ b/src/libcamera/pipeline/rpi/common/rpi_stream.h @@ -140,7 +140,6 @@ public: private: void bufferEmplace(unsigned int id, FrameBuffer *buffer); - void clearBuffers(); int queueToDevice(FrameBuffer *buffer); StreamFlags flags_;