From patchwork Fri Oct 13 07:48:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naushir Patuck X-Patchwork-Id: 19125 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 54681C3274 for ; Fri, 13 Oct 2023 07:48:52 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 8F8CE6297B; Fri, 13 Oct 2023 09:48:51 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1697183331; bh=ukEAiab497hGfyJvmVrVc3AQEQ62Am0ig990ebj2BLY=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=QF/C4lYvr5ZrOUsgUVR9PNL88FGocHsxdwgNb1w8lX1PGbQMUrgVJLlv8ww5hTFNG wWS2uSQBgXsf39gdRPouLqhSRp4w6bzhsSFkM7F7vIvabmPLP+5hXpS89BbZZomA3i AhJRF/THRtaaf2bmcI6+qLCny3OxRIhk+OhaoterAHNFmB3eQXAnXox3B+KjqzLn7R BWYxVRh7GEpHFmrQdyE+yxi/r7qNNmnauZQbO9fvWs6Zy3z7aea5u3UYzciBL7VKLr h9I10ibG8GI2cWYDUhuBpVgmZtBhIi4uXLLS71gtskTroH5eSWmBS/64qYHr2pNxa9 AbBewO+HyoaFw== Received: from mail-ed1-x52a.google.com (mail-ed1-x52a.google.com [IPv6:2a00:1450:4864:20::52a]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 7A2FA6295F for ; Fri, 13 Oct 2023 09:48:48 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=raspberrypi.com header.i=@raspberrypi.com header.b="p2ZcR8MH"; dkim-atps=neutral Received: by mail-ed1-x52a.google.com with SMTP id 4fb4d7f45d1cf-53b8f8c6b1fso3165574a12.0 for ; Fri, 13 Oct 2023 00:48:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raspberrypi.com; s=google; t=1697183328; x=1697788128; 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=vTMQlft/OcZG1+L7DUZr/wsKE/ARJQButlEv9E/GrNI=; b=p2ZcR8MHTUVQl2dgS665Rul0juvYvPFd/hCYZdHSBGk1JqL58bybaBNoOpYwzaPEKK sXYdkyfvj80AX4lH+pGaxhm2hC/48VNRqvRq2VAzWiT3uC3ZnVfPuFIAd3ipgnIhPlrn cVnQgqE7uNyNdgTnfNkGronOXZ3JgVjps+Sf5o6K2UcHhOF6T9FYd32W0lw5Prd/vD4J vjr6nohTFUbuo5Br38IUp/YHggsYGitux6mT7dwwPUf2Q/9LDGTrz719OTSKyaek8Vwn 7+pyekWS8+XViZjzYmmqReO1mCpLYxQGWMfOUdiVEg7Pkzvg5rDc8hppAxqiKwL+LAkK PMRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697183328; x=1697788128; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vTMQlft/OcZG1+L7DUZr/wsKE/ARJQButlEv9E/GrNI=; b=QhxMnkYQQAby/i+q4pcwzp25LXEBgLlPZmHc2+O1tdj5JajfK9rhtpRsavxzVuCQyL Qp9SMq4XOHPN0Qz735tVRZY8pgVGlDz55GjU7Y5iGm+iNu1oRXqxfqtD7w+k+kMD4nII 02vnDGm2NPnM01gn8pXQHt5Sbj2gag3W9MTenZJ1mIhbsgOsZlmE6GXwvfcofv/g4IPr 5j3yWo/YOgLe1TB2kh+OughnlOwR78sclZHWEH+P4G6myk4Fj9Inn8U2xrZZwmA0L76h iSGBeVbk10AlnE94GQ8SkpttoJEHjG4dRu9HQGN+3VQ/c/jv/szzUvvLiuawwk4fREjC P8eg== X-Gm-Message-State: AOJu0YzN9DYPvCZ2fiL+UEWkU6IrQqndbD/YyYd77BHnF1qoUs64Z17R GbSnwBe0cMHps9xcRMtL+P/3Q0+l9M1fasyDyp5LZQ== X-Google-Smtp-Source: AGHT+IGHZyuQGj49M31gECS5xIs6IyAEfd7j5c23bR6bGYHJQxHXEEWBlMoN4L5nYQcFs83nEQje3g== X-Received: by 2002:a17:906:530c:b0:9ae:3d8e:b635 with SMTP id h12-20020a170906530c00b009ae3d8eb635mr22227584ejo.24.1697183327626; Fri, 13 Oct 2023 00:48:47 -0700 (PDT) Received: from localhost.localdomain ([93.93.133.154]) by smtp.gmail.com with ESMTPSA id m16-20020a7bca50000000b003fee6e170f9sm1791890wml.45.2023.10.13.00.48.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Oct 2023 00:48:46 -0700 (PDT) To: libcamera-devel@lists.libcamera.org Date: Fri, 13 Oct 2023 08:48:22 +0100 Message-Id: <20231013074841.16972-2-naush@raspberrypi.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231013074841.16972-1-naush@raspberrypi.com> References: <20231013074841.16972-1-naush@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 01/20] pipeline: rpi: Add RequiresMmap flag to RPi::Stream 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: , X-Patchwork-Original-From: Naushir Patuck via libcamera-devel From: Naushir Patuck Reply-To: Naushir Patuck Cc: Jacopo Mondi Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Add a new RequiresMmap flag to the RPi::Stream class indicating that buffers handled by the stream must be mmapped after allocation and cached internally. Add a new member function getBuffer(id) which can be used to obtain the mapped buffers for a given buffer id. Add a new member function acquireBuffer() which can be used to obtain any mapped buffer that has not already been acquired by the caller. As a drive-by, add the header to rpi_stream.cpp as it is needed for the std::find_if() function. Signed-off-by: Naushir Patuck Reviewed-by: David Plowman Reviewed-by: Jacopo Mondi --- .../pipeline/rpi/common/pipeline_base.cpp | 2 +- .../pipeline/rpi/common/rpi_stream.cpp | 49 +++++++++++++++++-- .../pipeline/rpi/common/rpi_stream.h | 32 +++++++++++- src/libcamera/pipeline/rpi/vc4/vc4.cpp | 6 +-- 4 files changed, 80 insertions(+), 9 deletions(-) diff --git a/src/libcamera/pipeline/rpi/common/pipeline_base.cpp b/src/libcamera/pipeline/rpi/common/pipeline_base.cpp index fad710a63c46..825eae80d014 100644 --- a/src/libcamera/pipeline/rpi/common/pipeline_base.cpp +++ b/src/libcamera/pipeline/rpi/common/pipeline_base.cpp @@ -888,7 +888,7 @@ void PipelineHandlerBase::mapBuffers(Camera *camera, const BufferMap &buffers, u */ for (auto const &it : buffers) { bufferIds.push_back(IPABuffer(mask | it.first, - it.second->planes())); + it.second.buffer->planes())); data->bufferIds_.insert(mask | it.first); } diff --git a/src/libcamera/pipeline/rpi/common/rpi_stream.cpp b/src/libcamera/pipeline/rpi/common/rpi_stream.cpp index 7319f510a371..ee827a86b06a 100644 --- a/src/libcamera/pipeline/rpi/common/rpi_stream.cpp +++ b/src/libcamera/pipeline/rpi/common/rpi_stream.cpp @@ -6,6 +6,10 @@ */ #include "rpi_stream.h" +#include +#include +#include + #include /* Maximum number of buffer slots to allocate in the V4L2 device driver. */ @@ -17,8 +21,13 @@ LOG_DEFINE_CATEGORY(RPISTREAM) namespace RPi { +const BufferObject Stream::errorBufferObject{ nullptr, false }; + void Stream::setFlags(StreamFlags flags) { + /* We don't want dynamic mmapping. */ + ASSERT(!(flags & StreamFlag::RequiresMmap)); + flags_ |= flags; /* Import streams cannot be external. */ @@ -27,6 +36,9 @@ void Stream::setFlags(StreamFlags flags) void Stream::clearFlags(StreamFlags flags) { + /* We don't want dynamic mmapping. */ + ASSERT(!(flags & StreamFlag::RequiresMmap)); + flags_ &= ~flags; } @@ -56,7 +68,7 @@ void Stream::resetBuffers() void Stream::setExportedBuffers(std::vector> *buffers) { for (auto const &buffer : *buffers) - bufferMap_.emplace(++id_, buffer.get()); + bufferEmplace(++id_, buffer.get()); } const BufferMap &Stream::getBuffers() const @@ -71,7 +83,7 @@ unsigned int Stream::getBufferId(FrameBuffer *buffer) const /* Find the buffer in the map, and return the buffer id. */ auto it = std::find_if(bufferMap_.begin(), bufferMap_.end(), - [&buffer](auto const &p) { return p.second == buffer; }); + [&buffer](auto const &p) { return p.second.buffer == buffer; }); if (it == bufferMap_.end()) return 0; @@ -81,7 +93,7 @@ unsigned int Stream::getBufferId(FrameBuffer *buffer) const void Stream::setExportedBuffer(FrameBuffer *buffer) { - bufferMap_.emplace(++id_, buffer); + bufferEmplace(++id_, buffer); } int Stream::prepareBuffers(unsigned int count) @@ -180,6 +192,27 @@ void Stream::returnBuffer(FrameBuffer *buffer) } } +const BufferObject &Stream::getBuffer(unsigned int id) +{ + auto const &it = bufferMap_.find(id); + if (it == bufferMap_.end()) + return errorBufferObject; + + return it->second; +} + +const BufferObject &Stream::acquireBuffer() +{ + /* No id provided, so pick up the next available buffer if possible. */ + if (availableBuffers_.empty()) + return errorBufferObject; + + unsigned int id = getBufferId(availableBuffers_.front()); + availableBuffers_.pop(); + + return getBuffer(id); +} + int Stream::queueAllBuffers() { int ret; @@ -204,6 +237,16 @@ void Stream::releaseBuffers() clearBuffers(); } +void Stream::bufferEmplace(unsigned int id, FrameBuffer *buffer) +{ + if (flags_ & StreamFlag::RequiresMmap) + bufferMap_.emplace(std::piecewise_construct, std::forward_as_tuple(id), + std::forward_as_tuple(buffer, true)); + else + bufferMap_.emplace(std::piecewise_construct, std::forward_as_tuple(id), + std::forward_as_tuple(buffer, false)); +} + void Stream::clearBuffers() { availableBuffers_ = std::queue{}; diff --git a/src/libcamera/pipeline/rpi/common/rpi_stream.h b/src/libcamera/pipeline/rpi/common/rpi_stream.h index 889b499782a4..861e9c8e7dab 100644 --- a/src/libcamera/pipeline/rpi/common/rpi_stream.h +++ b/src/libcamera/pipeline/rpi/common/rpi_stream.h @@ -7,22 +7,23 @@ #pragma once +#include #include #include #include #include #include + #include +#include "libcamera/internal/mapped_framebuffer.h" #include "libcamera/internal/v4l2_videodevice.h" namespace libcamera { namespace RPi { -using BufferMap = std::unordered_map; - enum BufferMask { MaskID = 0x00ffff, MaskStats = 0x010000, @@ -30,6 +31,21 @@ enum BufferMask { MaskBayerData = 0x040000, }; +struct BufferObject { + BufferObject(FrameBuffer *b, bool requiresMmap) + : buffer(b), mapped(std::nullopt) + { + if (requiresMmap) + mapped = std::make_optional + (b, MappedFrameBuffer::MapFlag::ReadWrite); + } + + FrameBuffer *buffer; + std::optional mapped; +}; + +using BufferMap = std::unordered_map; + /* * Device stream abstraction for either an internal or external stream. * Used for both Unicam and the ISP. @@ -49,6 +65,11 @@ public: * buffers might be provided by (and returned to) the application. */ External = (1 << 1), + /* + * Indicates that the stream buffers need to be mmaped and returned + * to the pipeline handler when requested. + */ + RequiresMmap = (1 << 2), }; using StreamFlags = Flags; @@ -82,10 +103,17 @@ public: int queueBuffer(FrameBuffer *buffer); void returnBuffer(FrameBuffer *buffer); + const BufferObject &getBuffer(unsigned int id); + const BufferObject &acquireBuffer(); + int queueAllBuffers(); void releaseBuffers(); + /* For error handling. */ + static const BufferObject errorBufferObject; + private: + void bufferEmplace(unsigned int id, FrameBuffer *buffer); void clearBuffers(); int queueToDevice(FrameBuffer *buffer); diff --git a/src/libcamera/pipeline/rpi/vc4/vc4.cpp b/src/libcamera/pipeline/rpi/vc4/vc4.cpp index c189abaabc87..bc90d6324777 100644 --- a/src/libcamera/pipeline/rpi/vc4/vc4.cpp +++ b/src/libcamera/pipeline/rpi/vc4/vc4.cpp @@ -825,7 +825,7 @@ void Vc4CameraData::processStatsComplete(const ipa::RPi::BufferIds &buffers) if (!isRunning()) return; - FrameBuffer *buffer = isp_[Isp::Stats].getBuffers().at(buffers.stats & RPi::MaskID); + FrameBuffer *buffer = isp_[Isp::Stats].getBuffers().at(buffers.stats & RPi::MaskID).buffer; handleStreamBuffer(buffer, &isp_[Isp::Stats]); @@ -842,7 +842,7 @@ void Vc4CameraData::prepareIspComplete(const ipa::RPi::BufferIds &buffers) if (!isRunning()) return; - buffer = unicam_[Unicam::Image].getBuffers().at(bayer & RPi::MaskID); + buffer = unicam_[Unicam::Image].getBuffers().at(bayer & RPi::MaskID).buffer; LOG(RPI, Debug) << "Input re-queue to ISP, buffer id " << (bayer & RPi::MaskID) << ", timestamp: " << buffer->metadata().timestamp; @@ -850,7 +850,7 @@ void Vc4CameraData::prepareIspComplete(const ipa::RPi::BufferIds &buffers) ispOutputCount_ = 0; if (sensorMetadata_ && embeddedId) { - buffer = unicam_[Unicam::Embedded].getBuffers().at(embeddedId & RPi::MaskID); + buffer = unicam_[Unicam::Embedded].getBuffers().at(embeddedId & RPi::MaskID).buffer; handleStreamBuffer(buffer, &unicam_[Unicam::Embedded]); }