From patchwork Mon Aug 5 13:48: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: 20779 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 51835BE173 for ; Mon, 5 Aug 2024 13:51:14 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 553BD63398; Mon, 5 Aug 2024 15:51:13 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="IlUXxfPS"; dkim-atps=neutral Received: from mail-lj1-x22b.google.com (mail-lj1-x22b.google.com [IPv6:2a00:1450:4864:20::22b]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 654D563381 for ; Mon, 5 Aug 2024 15:51:07 +0200 (CEST) Received: by mail-lj1-x22b.google.com with SMTP id 38308e7fff4ca-2ef2c56d9dcso126901161fa.2 for ; Mon, 05 Aug 2024 06:51:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1722865866; x=1723470666; 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=R/eakiLR90Hp4UnEDHENAwykCkeQ/OvhQjrjwoGtq6g=; b=IlUXxfPS9pznodiE7HmSxVo3xner4qEPY3mZxSWePf2WDl95HluIJ68HLjJlwSVmz3 j8hdtyrhtQxhOYM8Ko68DAfS/VjM8sqBqKvRBHidBcpuagjEaUp5bM7CBwBiAiP75gz9 J8PJXPwFSMeqlSwWsIH42tjlXBE/48oeee+mQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722865866; x=1723470666; 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=R/eakiLR90Hp4UnEDHENAwykCkeQ/OvhQjrjwoGtq6g=; b=MposZ1d9V+cWeO+HHCxf2Fh+WsakrgGbQygZkRbK59aGhivzMWCJ79MqRf+UasP+Zu yqfjb5ocMQjWTuLrrRuJIkem/9GS3CV4BkSYIPaxmZ6DaGbOPE8uA6zc7qF+WOyqCPRl SyPXHF5Irc/nvXFxQridmC1gNtcBanOiVIf7iHID5WiQA9+J+mIZFhonCGHpRX8SekTB IM1MpY27I5THgQDQxjhSiLcnYA43wSlq25j57kB1ccjFZZOr79OfviUUiwdyOI1EciGZ 9QRtak876NhdVQIT/Ibvs2N63YKb92FLikDJOuAqYZBTktRPRmhkz1t/pFVhg/NoaIyy xN1A== X-Gm-Message-State: AOJu0YyskpPIz6V5Bij+vFMhYfw+Vhtt7srHcnnckMg9ZPMpF/J0k/Gj w1W1I5bUAvMiZgSxcGrs6IRctP3on1wxTGjDhTsfCxNZVShF+mbH0IuHAGap/WDBmYVeXdp4L9U ScQ== X-Google-Smtp-Source: AGHT+IEe/O5coO1CJCWor1N32uTCxg9MvbPqgCyvn0c5XBngnzZ3D5+hy0JeDFj28b85Oro0kZa2EQ== X-Received: by 2002:a2e:8747:0:b0:2ef:22bc:6fb0 with SMTP id 38308e7fff4ca-2f15ab01385mr76096561fa.34.1722865866024; Mon, 05 Aug 2024 06:51:06 -0700 (PDT) Received: from chenghaoyang-germany.c.googlers.com.com (49.222.77.34.bc.googleusercontent.com. [34.77.222.49]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-428e8d6555fsm137146675e9.26.2024.08.05.06.51.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Aug 2024 06:51:05 -0700 (PDT) From: Harvey Yang X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Harvey Yang , Harvey Yang Subject: [PATCH v8 1/8] libcamera: add DmaBufAllocator::exportBuffers() Date: Mon, 5 Aug 2024 13:48:32 +0000 Message-ID: <20240805135104.139932-2-chenghaoyang@google.com> X-Mailer: git-send-email 2.46.0.rc2.264.g509ed76dc8-goog In-Reply-To: <20240805135104.139932-1-chenghaoyang@google.com> References: <20240805135104.139932-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 | 55 +++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/include/libcamera/internal/dma_buf_allocator.h b/include/libcamera/internal/dma_buf_allocator.h index 36ec1696..3a9b56b1 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 c06eca7d..a2df1730 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 @@ -243,4 +248,54 @@ 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; + + for (auto frameSize : frameSizes) { + UniqueFD fd = alloc(name.c_str(), frameSize); + if (!fd.isValid()) + return nullptr; + + FrameBuffer::Plane plane; + plane.fd = SharedFD(std::move(fd)); + plane.offset = 0; + plane.length = frameSize; + planes.push_back(std::move(plane)); + } + + return std::make_unique(planes); +} + } /* namespace libcamera */