From patchwork Tue Sep 10 04:40:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Yang X-Patchwork-Id: 21207 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 36EAFC324C for ; Tue, 10 Sep 2024 04:48:49 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 6D2E1634F7; Tue, 10 Sep 2024 06:48:45 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="StSbiW7E"; dkim-atps=neutral Received: from mail-pg1-x52c.google.com (mail-pg1-x52c.google.com [IPv6:2607:f8b0:4864:20::52c]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 14BAE618F2 for ; Tue, 10 Sep 2024 06:48:43 +0200 (CEST) Received: by mail-pg1-x52c.google.com with SMTP id 41be03b00d2f7-70b2421471aso3366305a12.0 for ; Mon, 09 Sep 2024 21:48:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1725943721; x=1726548521; 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=u898qJhf26WnkN+KdJDHerBl0GLRgYehB01hPG2JNFE=; b=StSbiW7Eusz04CadGMK9fSnL8uuHMokygZxuzhKN0uNi2Nd4BCKkzA09TufUhh8zv/ D7LetYcXcCDKyxkzgyrQ5sfHP4BU8XwghkvAFoZqlEnNhc/RJPlBWtsvX8Ty2yHlOWWe LJkEXwBmlffIwi30UQa9I1KaX3XamSOCtILSE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725943721; x=1726548521; 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=u898qJhf26WnkN+KdJDHerBl0GLRgYehB01hPG2JNFE=; b=khyynUcfMME2j6JPKekyOqAtE18NPdrpo2CH5zS0bmGqsfvIlcFrOf7ug9375Vbk+1 zqvqSCzzuwOfn4LbuBrP3V75QJ0b6cdMEr3WlLkpJV/2trBiZ1OuOVO8hyoH/liwHwWF H/hix/tlS8QifYZKxFFKYirsRii8cUt/lodwLaKhhjCGXr0qgcsQ3mRB73TWrWkKH1/K ViDLuxwgnIZQv332X4fu+zCLELTAv1n0tG7bwxG5rkUtAcS2USW8USutvys/cUoJ1HMJ R7aPtMPoYYRHWc7sLxTQDtSQSyyQVdrAcx9NfAwghke4EGN+7TpzLSn/rNgyKY6+E0/f Pwig== X-Gm-Message-State: AOJu0YwQjuJNWapy3mVcxLVi8VtlMrrgRWCHr2hHy6ptDKsVtFZvTJIE tXq0V6UtQuPEAPj/DWyxSaPugbmEnlzbmjhjCGa6YQEwFvrbMgBsd+7UDAVqUZYdE5akx4/59SA = X-Google-Smtp-Source: AGHT+IF02Wybb9t2aP+Djh0fQx/oel03gbpuwsE+dw2eduLtPINGWb9UNMThw6BKs8UCWr/PZjgeVw== X-Received: by 2002:a05:6a20:4389:b0:1ce:ce51:4405 with SMTP id adf61e73a8af0-1cf1d1ddf8emr12892643637.42.1725943720684; Mon, 09 Sep 2024 21:48:40 -0700 (PDT) Received: from chenghaoyang-low.c.googlers.com.com (0.223.81.34.bc.googleusercontent.com. [34.81.223.0]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71909092522sm530734b3a.111.2024.09.09.21.48.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Sep 2024 21:48:40 -0700 (PDT) From: Harvey Yang X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Harvey Yang , Harvey Yang , Kieran Bingham Subject: [PATCH v12 1/7] libcamera: add DmaBufAllocator::exportBuffers() Date: Tue, 10 Sep 2024 04:40:14 +0000 Message-ID: <20240910044834.2477701-2-chenghaoyang@google.com> X-Mailer: git-send-email 2.46.0.598.g6f2099f65c-goog In-Reply-To: <20240910044834.2477701-1-chenghaoyang@google.com> References: <20240910044834.2477701-1-chenghaoyang@google.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" Add a helper function exportBuffers in DmaBufAllocator to make it easier to use. It'll be used in Virtual Pipeline Handler and SoftwareIsp. Signed-off-by: Harvey Yang Reviewed-by: Kieran Bingham Reviewed-by: Jacopo Mondi --- .../libcamera/internal/dma_buf_allocator.h | 13 +++++ src/libcamera/dma_buf_allocator.cpp | 58 +++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/include/libcamera/internal/dma_buf_allocator.h b/include/libcamera/internal/dma_buf_allocator.h index d2a0a0d1..66d3b419 100644 --- a/include/libcamera/internal/dma_buf_allocator.h +++ b/include/libcamera/internal/dma_buf_allocator.h @@ -7,11 +7,17 @@ #pragma once +#include +#include +#include + #include #include namespace libcamera { +class FrameBuffer; + class DmaBufAllocator { public: @@ -28,7 +34,14 @@ public: bool isValid() const { return providerHandle_.isValid(); } UniqueFD alloc(const char *name, std::size_t size); + int exportBuffers(unsigned int count, + const std::vector &frameSize, + std::vector> *buffers); + private: + std::unique_ptr createBuffer( + std::string name, const std::vector &frameSizes); + UniqueFD allocFromHeap(const char *name, std::size_t size); UniqueFD allocFromUDmaBuf(const char *name, std::size_t size); UniqueFD providerHandle_; diff --git a/src/libcamera/dma_buf_allocator.cpp b/src/libcamera/dma_buf_allocator.cpp index be6efb89..a49d1c60 100644 --- a/src/libcamera/dma_buf_allocator.cpp +++ b/src/libcamera/dma_buf_allocator.cpp @@ -23,6 +23,10 @@ #include #include +#include + +#include "libcamera/internal/formats.h" + /** * \file dma_buf_allocator.cpp * \brief dma-buf allocator @@ -205,4 +209,58 @@ UniqueFD DmaBufAllocator::alloc(const char *name, std::size_t size) return allocFromHeap(name, size); } +/** + * \brief Allocate and export buffers from the DmaBufAllocator + * \param[in] count The number of requested FrameBuffers + * \param[in] frameSizes The sizes of planes in each FrameBuffer + * \param[out] buffers Array of buffers successfully allocated + * + * Planes in a FrameBuffer are allocated with a single dma buf. + * \todo Add the option to allocate each plane with a dma buf respectively. + * + * \return The number of allocated buffers on success or a negative error code + * otherwise + */ +int DmaBufAllocator::exportBuffers(unsigned int count, + const std::vector &frameSizes, + std::vector> *buffers) +{ + for (unsigned int i = 0; i < count; ++i) { + std::unique_ptr buffer = + createBuffer("frame-" + std::to_string(i), frameSizes); + if (!buffer) { + LOG(DmaBufAllocator, Error) << "Unable to create buffer"; + + buffers->clear(); + return -EINVAL; + } + + buffers->push_back(std::move(buffer)); + } + + return count; +} + +std::unique_ptr +DmaBufAllocator::createBuffer(std::string name, + const std::vector &frameSizes) +{ + std::vector planes; + + unsigned int bufferSize = 0, offset = 0; + for (auto frameSize : frameSizes) + bufferSize += frameSize; + + SharedFD fd(alloc(name.c_str(), bufferSize)); + if (!fd.isValid()) + return nullptr; + + for (auto frameSize : frameSizes) { + planes.emplace_back(FrameBuffer::Plane{ fd, offset, frameSize }); + offset += frameSize; + } + + return std::make_unique(planes); +} + } /* namespace libcamera */