From patchwork Tue Aug 27 15:33:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Yang X-Patchwork-Id: 21042 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 2B5FDC323E for ; Tue, 27 Aug 2024 15:33:58 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id C25C563460; Tue, 27 Aug 2024 17:33:56 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="bfWpFyg0"; dkim-atps=neutral Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 146EF61901 for ; Tue, 27 Aug 2024 17:33:55 +0200 (CEST) Received: by mail-wr1-x435.google.com with SMTP id ffacd0b85a97d-37182eee02dso3090783f8f.1 for ; Tue, 27 Aug 2024 08:33:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1724772834; x=1725377634; darn=lists.libcamera.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=rQ840Cif1XVGygX4o0Y4c7lfT+PQRq2ZCLmHDu/j4Ss=; b=bfWpFyg0+M0pAA3mBx88M6Vi3iKyYngY3HTlsGguX3dTO3eVp8SFhDCqrlhXkLoSYh O3eZFrXz+yoZL29RiJlO1MKr7yoKy4BBk4XHVfuTIewftJNdv0cWmpGBcwGwuMjv9ebO Y0wiqSM2hVxk2pTaPzwjtgWo205WfmyUSSuH8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724772834; x=1725377634; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=rQ840Cif1XVGygX4o0Y4c7lfT+PQRq2ZCLmHDu/j4Ss=; b=NSsBj8NZ4D8CELArvbsVZIszLi6xWIDHxQoTLCjbHr2lLzM7/7Tq/B52q7Afkdzwpv nGCyG/CoXtMcWVIJUauX6hBBr2fc69MAOHeZq88Q2g/T9L8DCHuW6YH3O0o3Kz2GFFus 6ss7Hb3smkMDvnFcai5hUxmKtMVa/Giej4NihLvR2yns+UeKt60A5x+BQBNxEVYEEFLw YIR3GPXLdMX18m02aKC/ccSD6UL5avZYnGR5eu9ct5UqnrpK2nVA8NZJx/ZiSqsJRDpG vLDXUDq8gMyUZka1cULkR27QPl/QUWENgX61dFsgeOoRZIQwoQdqWUjqGNw8eusYzOi6 dM6g== X-Gm-Message-State: AOJu0YwTV9pakWtdux3xse29mi8SEWLgTa+hO6O9lgWGLQvb5PvY1dwJ cdgM4wj4NehjLaG/M56ye/VrcRwDRSXkEmGGPyeKjtFG4EzLlhMlHNR7+9JkeEjKo1WiizkUIW8 //bSt X-Google-Smtp-Source: AGHT+IGzA6f/m0qzIX5e8DciipV/3PNrIvybR6NYvkiqUeio+IvuDL6XOHnipMoJH+Ew7KQvi5Ns8Q== X-Received: by 2002:adf:fc0d:0:b0:365:aec0:e191 with SMTP id ffacd0b85a97d-3748c88a349mr2162875f8f.21.1724772834110; Tue, 27 Aug 2024 08:33:54 -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-42ac517a442sm191386935e9.36.2024.08.27.08.33.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Aug 2024 08:33:53 -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 1/8] libcamera: add DmaBufAllocator::exportBuffers() Date: Tue, 27 Aug 2024 15:33:38 +0000 Message-ID: <20240827153349.2893413-1-chenghaoyang@google.com> X-Mailer: git-send-email 2.46.0.295.g3b9ea8a38a-goog 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 36ec1696b..49122ed95 100644 --- a/include/libcamera/internal/dma_buf_allocator.h +++ b/include/libcamera/internal/dma_buf_allocator.h @@ -7,13 +7,18 @@ #pragma once +#include #include +#include +#include #include #include namespace libcamera { +class FrameBuffer; + class DmaBufAllocator { public: @@ -30,7 +35,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 be6efb89f..d2ac175f0 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,55 @@ 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 + * + * \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 */