From patchwork Thu Aug 29 19:47:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Yang X-Patchwork-Id: 21049 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 66B51C323E for ; Thu, 29 Aug 2024 19:57:15 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id A1815634A6; Thu, 29 Aug 2024 21:57:11 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="OJ9Bvj3n"; dkim-atps=neutral Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id DA7DD63458 for ; Thu, 29 Aug 2024 21:57:07 +0200 (CEST) Received: by mail-wm1-x32b.google.com with SMTP id 5b1f17b1804b1-428e0d184b4so8872615e9.2 for ; Thu, 29 Aug 2024 12:57:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1724961427; x=1725566227; 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=ELIT8Vdp9VvtK30ZWCYSufAPpOhd7DlPXyMWDCSWtCM=; b=OJ9Bvj3nx1/rXaoliq+pScgrQJh5TRUL6xgWwcgD54gbX7/RhOltGYUPBTAzZ3vlEu p9WzAedaYhkmm6SUvHrduBy6Gf726aWb/guZc8G9YfomzOueoExfTDGvyEphbCunwxYo NHrXjvwoIX3FaJ/a+YYQpvhjpGnZFTQBnMRDQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724961427; x=1725566227; 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=ELIT8Vdp9VvtK30ZWCYSufAPpOhd7DlPXyMWDCSWtCM=; b=iJutcvOw+8qAE7h0izeteGOgy5tT/vc/yp+JuPISkJlhjE9Sxpjp3o8gUyXVd0vj8j HOMTAo2Js+5tklc1x6otw2wVgojfmuKPrOT3Vg76pXjWTtHInIvwjTL1qCMsTpQRyauq +alz7i279gpbklHV4/b3LjelUnm/SYJbANRKr3X/Hd1VuZQp4pzTa66PjfadUTi8vBcH NOXdXE+meIk/jpGsyRjAVl80eJPlJB9i36x2IkBB2Zm1yPnBDWXTmEWLSgaKMICmlUxF qTrhqg0rZ5+d36to/DqfHe0SJydzA+XIL9D443unmRpSf0NcXdqX8oXpTHH68CMGzBLV Jphw== X-Gm-Message-State: AOJu0YxGVszGM1F5wlr+BiUZ3pyMrxfEbi3ZQ82RATdwsyDYuYRxQg5n U8C6HTBgG/9zPA3mU/v6iw+d7PhvlgsFPDIfVGq7MdyMlTAY/qkYvE2008m8zIEy9rAeSIH3MeM oIw== X-Google-Smtp-Source: AGHT+IEBQa9Gv3TwceuE+Gsm8JoUNIrLMatcIEzg31y8yIyoy1tTunT5GdhWbJBe4uQrPto/z7F38A== X-Received: by 2002:adf:f242:0:b0:360:791c:aff2 with SMTP id ffacd0b85a97d-3749b58e3bdmr2530741f8f.47.1724961426882; Thu, 29 Aug 2024 12:57:06 -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-42ba7b4271fsm54602805e9.29.2024.08.29.12.57.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Aug 2024 12:57:06 -0700 (PDT) From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Harvey Yang , Harvey Yang Subject: [PATCH v10 1/7] libcamera: add DmaBufAllocator::exportBuffers() Date: Thu, 29 Aug 2024 19:47:40 +0000 Message-ID: <20240829195703.1456614-2-chenghaoyang@chromium.org> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog In-Reply-To: <20240829195703.1456614-1-chenghaoyang@chromium.org> References: <20240829195703.1456614-1-chenghaoyang@chromium.org> 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" From: Harvey Yang 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 */