From patchwork Wed Jul 15 14:07:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naushir Patuck X-Patchwork-Id: 8831 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 C1B1ABD790 for ; Wed, 15 Jul 2020 14:07:25 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 8B02D60BEA; Wed, 15 Jul 2020 16:07:25 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=raspberrypi.com header.i=@raspberrypi.com header.b="UHcIoGol"; dkim-atps=neutral Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 21FED60BEA for ; Wed, 15 Jul 2020 16:07:24 +0200 (CEST) Received: by mail-wr1-x42a.google.com with SMTP id z15so2841996wrl.8 for ; Wed, 15 Jul 2020 07:07:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raspberrypi.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=/KXVsZT/hUhQq4nWWSzSgN5H5zYfjcF3lv06viQPH5Y=; b=UHcIoGolBPRR6kx+byyOBq1IoaXSUeMIRE8CaX6s8tz6Ip0dNzisTgnPSYkcoomTPG utGZRAXwFyQj3sAZbXcZaQA3yV1aJ3pd408j6cWMKa4yyn09E4ZaxOUjfQIRc0w7jdR6 UlUQrOn8zXaTiFv+/xLp8BPIxajoF41hmkHzJfYYS62lGErLwgDDI+G8MiquJw/irJ9q 8lX4lerhPyGLk17kVTsfrG3pbSOjy04jHiw1Bo1OwmB9Hhij/GI1KnPPtlqGoFS9xTij ATpy0Xl0WMnmazGaYGsDaY7sIEQYBQcpCfZR+MisPoFPSndWv4KUEk01igXagkFUGhsF CQwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=/KXVsZT/hUhQq4nWWSzSgN5H5zYfjcF3lv06viQPH5Y=; b=ahJPkauvgei37xP44A4PQHH/0IbeGibs/MMowtcJweL4L5VWqiv4X7EHnq0EPTASMu fylBhFPM9rT4Q2SggRmAfLnMhBQ3rlpasEAikwWr1AmmtxaNMN8z5M0YpoLcSqIWmi3B rykkYr1/Qd02ZuL9HpWtnZQq69qbedWozrErX/sKJmg+8fclLlgQRUC3HqTADFKCO4Wx Man7In6ADPM8auH92O5/gsI4bQAkueq9loTmRDYCaGUgLz94usMcuth2JkWdJfR1//f3 dmCqJ9SxkDAvS1iuuNrhEgtcYP7H0JT/Mx1q1Bf2qqOwIZFsESzI7z7REuC6RKw8nFdl +SJQ== X-Gm-Message-State: AOAM531GesEdd14qhF3k4S3bDOdL7ICwCdYj/WBNAkHyl81OJByQzrW3 y2J1DsJe4alIcIMix6fL0shJ2ZpnNuo= X-Google-Smtp-Source: ABdhPJx1msNgp4C44vzmcxuiOv+wszDMEml5Bjv6z+zd6jSvVOQ3Wl4KrjMFE3Wa4DUk+skUUvYSSw== X-Received: by 2002:a5d:540d:: with SMTP id g13mr10854440wrv.380.1594822043319; Wed, 15 Jul 2020 07:07:23 -0700 (PDT) Received: from naushir-VirtualBox.patuck.local ([88.97.76.4]) by smtp.gmail.com with ESMTPSA id l67sm4195339wml.13.2020.07.15.07.07.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jul 2020 07:07:22 -0700 (PDT) From: Naushir Patuck To: libcamera-devel@lists.libcamera.org Date: Wed, 15 Jul 2020 15:07:01 +0100 Message-Id: <20200715140703.566298-9-naush@raspberrypi.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200715140703.566298-1-naush@raspberrypi.com> References: <20200715140703.566298-1-naush@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 08/10] libcamera: pipeline: raspberrypi: Add more robust stream buffer logic 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" Add further queueing into the RPiStream object to ensure that we always follow the buffer ordering (be it internal or external) given by incoming Requests. This is essential, otherwise we risk dropping frames that are meant to be part of a Request, and can cause the pipeline to stall indefinitely. This also prevents any possibility of mismatched frame buffers going through the pipeline and out to the application. Signed-off-by: Naushir Patuck --- .../pipeline/raspberrypi/rpi_stream.cpp | 69 ++++++++++++++++--- .../pipeline/raspberrypi/rpi_stream.h | 12 +++- 2 files changed, 70 insertions(+), 11 deletions(-) diff --git a/src/libcamera/pipeline/raspberrypi/rpi_stream.cpp b/src/libcamera/pipeline/raspberrypi/rpi_stream.cpp index 73111b95..d9eea3ed 100644 --- a/src/libcamera/pipeline/raspberrypi/rpi_stream.cpp +++ b/src/libcamera/pipeline/raspberrypi/rpi_stream.cpp @@ -116,28 +116,64 @@ int RPiStream::queueBuffer(FrameBuffer *buffer) if (availableBuffers_.empty()) { LOG(RPISTREAM, Warning) << "No buffers available for " << name_; - return -EINVAL; + /* + * Note that we need to requeue an internal buffer as soon + * as one becomes available. + */ + requeueBuffers_.push(nullptr); + return -ENOMEM; } buffer = availableBuffers_.front(); availableBuffers_.pop(); } - LOG(RPISTREAM, Debug) << "Queuing buffer " << buffer->cookie() - << " for " << name_; - - int ret = dev_->queueBuffer(buffer); - if (ret) { - LOG(RPISTREAM, Error) << "Failed to queue buffer for " - << name_; + if (requeueBuffers_.empty()) { + /* + * No earlier requests are pending to be queued, so we can + * go ahead and queue the buffer into the device. + */ + return queueToDevice(buffer); + } else { + /* + * There are earlier buffers to be queued, so this buffer + * must go on the waiting list. + */ + requeueBuffers_.push(buffer); + return 0; } - - return ret; } void RPiStream::returnBuffer(FrameBuffer *buffer) { + /* Push this buffer back into the queue to be used again. */ availableBuffers_.push(buffer); + + /* + * Do we have any buffers that are waiting to be queued? + * If so, do it now as availableBuffers_ will not be empty. + */ + while (!requeueBuffers_.empty()) { + FrameBuffer *buffer = requeueBuffers_.front(); + requeueBuffers_.pop(); + + if (!buffer && !availableBuffers_.empty()) { + /* + * We want to queue an internal buffer, and at + * least one is available. + */ + buffer = availableBuffers_.front(); + availableBuffers_.pop(); + } else if (!buffer && availableBuffers_.empty()) { + /* + * We want to queue an internal buffer, but none + * are available. Can't do anything, quit the loop. + */ + break; + } + + queueToDevice(buffer); + } } bool RPiStream::findFrameBuffer(FrameBuffer *buffer) const @@ -154,10 +190,23 @@ bool RPiStream::findFrameBuffer(FrameBuffer *buffer) const void RPiStream::clearBuffers() { availableBuffers_ = std::queue{}; + requeueBuffers_ = std::queue{}; internalBuffers_.clear(); bufferList_.clear(); } +int RPiStream::queueToDevice(FrameBuffer *buffer) +{ + LOG(RPISTREAM, Debug) << "Queuing buffer " << buffer->cookie() + << " for " << name_; + + int ret = dev_->queueBuffer(buffer); + if (ret) + LOG(RPISTREAM, Error) << "Failed to queue buffer for " + << name_; + return ret; +} + } /* namespace RPi */ } /* namespace libcamera */ diff --git a/src/libcamera/pipeline/raspberrypi/rpi_stream.h b/src/libcamera/pipeline/raspberrypi/rpi_stream.h index dc65e5d5..0e58d261 100644 --- a/src/libcamera/pipeline/raspberrypi/rpi_stream.h +++ b/src/libcamera/pipeline/raspberrypi/rpi_stream.h @@ -51,6 +51,7 @@ public: private: void clearBuffers(); + int queueToDevice(FrameBuffer *buffer); /* * Indicates that this stream is active externally, i.e. the buffers * might be provided by (and returned to) the application. @@ -62,7 +63,7 @@ private: std::string name_; /* The actual device stream. */ std::unique_ptr dev_; - /* All framebuffers associated with this device stream. */ + /* All frame buffers associated with this device stream. */ std::vector bufferList_; /* * List of frame buffer that we can use if none have been provided by @@ -70,6 +71,15 @@ private: * buffers exported internally. */ std::queue availableBuffers_; + /* + * List of frame buffer that are to be re-queued into the device. + * A nullptr indicates any internal buffer can be used (from availableBuffers_), + * whereas a valid pointer indicates an external buffer to be queued. + * + * Ordering buffers to be queued is important here as it must match the + * requests coming from the application. + */ + std::queue requeueBuffers_; /* * This is a list of buffers exported internally. Need to keep this around * as the stream needs to maintain ownership of these buffers.