From patchwork Tue Aug 20 16:23:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Yang X-Patchwork-Id: 20968 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 A7EA4C323E for ; Tue, 20 Aug 2024 17:22:22 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 42961633DA; Tue, 20 Aug 2024 19:22:22 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="QTiHx+sT"; dkim-atps=neutral Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 2B322633B9 for ; Tue, 20 Aug 2024 19:22:16 +0200 (CEST) Received: by mail-wm1-x32a.google.com with SMTP id 5b1f17b1804b1-4280bbdad3dso45736665e9.0 for ; Tue, 20 Aug 2024 10:22:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1724174535; x=1724779335; 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=xhJ1TpNfniDD0lgMah61wWiI0vruYwsEg3JQnpBROMs=; b=QTiHx+sTibzYudyenUtC3JettTDUhA1a3S3/EwHWVYKRRjoBYEQZj6i2tRVLy6TrB1 WhJfPivYbCVOnxLTgXFHtk68N5toyVdlAV8hSRmCGiVsnrYvTBcEAfA1riU9Pu7pmpYY fsfEjBaD6yAatQ9Vf8Q111wjQ192k/U6qT7ZE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724174535; x=1724779335; 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=xhJ1TpNfniDD0lgMah61wWiI0vruYwsEg3JQnpBROMs=; b=Lh4jJXbBA3i9wSO6BGuLEvHFvvx5sK66Tku5+a2bPE1+9c1z8VGqngDIkVN7SlvlA4 6hO//39El/655Hpc/bodbnzNOJWo8q3imjoFknzMD3bgDW4pJI4vxWBkfGPc18E3rLf2 hLseF7d+gSnP86eSm5gZVQREzX4eLe+HdZM80D9cD41xKd60GWbLvFwK7Lmgs8ioWngC oapTkCzV1txTxfVZYo/R0M5hrGq1DSmbJlP6ZkLxCwhNoUpTJGXB3fUOkJ5YmSyNb5jm lmZP+hJJEaVa0tDXKrGq1SnUcpnVYt26QX75wbnOpMbOc8zJUqVlnzLoe/9cxtAVnPk3 6Bxw== X-Gm-Message-State: AOJu0YzDa14XT8yKmgflvR/shGnJnP9C6lQGljPgEEaTqYVAQI9yA/lN lye26F33vDTnFeFsvL/y+8M2Hp86V1jJIrmNyMOEYcp0FKzePI7Tp/upHO8MStPaDWHkQwiOuid jfO3rMGY= X-Google-Smtp-Source: AGHT+IGonbZmSHTbLGIz2b10s+Wy5//AT3oBumPhjOlHMWUOu2bR1D+cxyqvSPJ2aozdV1BBzkyD6g== X-Received: by 2002:a05:600c:46ce:b0:426:593c:935d with SMTP id 5b1f17b1804b1-42abd21f59fmr493395e9.5.1724174534668; Tue, 20 Aug 2024 10:22:14 -0700 (PDT) Received: from chenghaoyang-germany.c.googlers.com.com (161.126.77.34.bc.googleusercontent.com. [34.77.126.161]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-42ab6e90db5sm30328575e9.0.2024.08.20.10.22.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Aug 2024 10:22:14 -0700 (PDT) From: Harvey Yang X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Harvey Yang , Harvey Yang Subject: [PATCH v9 1/8] libcamera: add DmaBufAllocator::exportBuffers() Date: Tue, 20 Aug 2024 16:23:32 +0000 Message-ID: <20240820172202.526547-2-chenghaoyang@google.com> X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog In-Reply-To: <20240820172202.526547-1-chenghaoyang@google.com> References: <20240820172202.526547-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 --- .../libcamera/internal/dma_buf_allocator.h | 12 ++++ src/libcamera/dma_buf_allocator.cpp | 64 ++++++++++++++++++- 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/include/libcamera/internal/dma_buf_allocator.h b/include/libcamera/internal/dma_buf_allocator.h index 36ec1696b..3a9b56b1c 100644 --- a/include/libcamera/internal/dma_buf_allocator.h +++ b/include/libcamera/internal/dma_buf_allocator.h @@ -8,12 +8,16 @@ #pragma once #include +#include #include #include namespace libcamera { +class FrameBuffer; +struct StreamConfiguration; + class DmaBufAllocator { public: @@ -30,7 +34,15 @@ public: bool isValid() const { return providerHandle_.isValid(); } UniqueFD alloc(const char *name, std::size_t size); + int exportBuffers( + std::size_t count, + std::vector frameSize, + std::vector> *buffers); + private: + std::unique_ptr createBuffer( + std::string name, 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 c06eca7d0..2d88b8b2b 100644 --- a/src/libcamera/dma_buf_allocator.cpp +++ b/src/libcamera/dma_buf_allocator.cpp @@ -23,6 +23,11 @@ #include +#include +#include + +#include "libcamera/internal/formats.h" + /** * \file dma_buf_allocator.cpp * \brief dma-buf allocator @@ -130,8 +135,8 @@ DmaBufAllocator::~DmaBufAllocator() = default; /* uClibc doesn't provide the file sealing API. */ #ifndef __DOXYGEN__ #if not HAVE_FILE_SEALS -#define F_ADD_SEALS 1033 -#define F_SEAL_SHRINK 0x0002 +#define F_ADD_SEALS 1033 +#define F_SEAL_SHRINK 0x0002 #endif #endif @@ -243,4 +248,59 @@ UniqueFD DmaBufAllocator::alloc(const char *name, std::size_t size) return allocFromHeap(name, size); } +/** + * \brief Allocate and export buffers for \a stream from the DmaBufAllocator + * \param[in] count The number of FrameBuffers required + * \param[in] frameSizes The sizes of planes in the FrameBuffer + * \param[out] buffers Array of buffers successfully allocated + * + * \return The number of allocated buffers on success or a negative error code + * otherwise + */ +int DmaBufAllocator::exportBuffers( + std::size_t count, + std::vector frameSizes, + std::vector> *buffers) +{ + for (unsigned 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, std::vector frameSizes) +{ + std::vector planes; + + std::size_t 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) { + FrameBuffer::Plane plane; + plane.fd = fd; + plane.offset = offset; + plane.length = frameSize; + planes.push_back(std::move(plane)); + offset += plane.length; + } + + return std::make_unique(planes); +} + } /* namespace libcamera */