From patchwork Mon Sep 30 06:29:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cheng-Hao Yang X-Patchwork-Id: 21415 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 B7D23C32D4 for ; Mon, 30 Sep 2024 06:33:58 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id A318F63518; Mon, 30 Sep 2024 08:33:57 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="G57J0jVc"; dkim-atps=neutral Received: from mail-pf1-x434.google.com (mail-pf1-x434.google.com [IPv6:2607:f8b0:4864:20::434]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 246B46350F for ; Mon, 30 Sep 2024 08:33:51 +0200 (CEST) Received: by mail-pf1-x434.google.com with SMTP id d2e1a72fcca58-71971d2099cso3268312b3a.2 for ; Sun, 29 Sep 2024 23:33:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1727678029; x=1728282829; 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=KYSCW2eznOAuCeDgQqEiD1QpvzOUGC3v4J7VHyWiUIo=; b=G57J0jVcJvBbmetJ62YzWj+tJA3sklKQAyKmy/j/KFUDQ69A/0cqPpSTl8Z0Sxj6o8 IxXOGC2Dc2rAfWWYmR7LhjOnSVUYwTU4zvSpta6RyjP6ZfXcaEmcduabQc+m0QKfydJG v+ICQxNh5hjFMxbwvOaJq4tE7aVNpvQa5bymE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727678029; x=1728282829; 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=KYSCW2eznOAuCeDgQqEiD1QpvzOUGC3v4J7VHyWiUIo=; b=CSbGZ4ix3RdXY3loxOdwD0yhZin5Vr0sJpE6BlwJizZp1ozII02DO0fta5OUNLBtb/ /JaVdiv/UBA9OpF7WiPunomY34MvY1eTtlaCJK2mexjYHiLli7NkBy51EWXoPaBQAEIE Q4oi6x3P5WIEVee/G06/S1xqZwfkBd+ku61j3SrFF9dsw1Yx16ZalEy0C1OkpB/FuRCS pzVFLCaGAERxQXQfyMozqcdxhqHoBgmjDNGskyeXg4UD7NoVUXFUKBDBHrSEV4lYSrIm VMw6T+DMjrxJNbAmJEp8Qq2HA+88RBFS0vAQw/lrXT4uQuiYiXPfxbMIW0DWkQVSsAMk DBRw== X-Gm-Message-State: AOJu0YxPRfVou+7rd5dTAtPBFiPHUI0nII4EpuD5Mzo65UYWsX38HJP7 5XOMOpzZPIw1lVuEcWX1Q4TG/HZNeaVBCZYkXBPC8lxlRmrr9j+k8VmJKYcsCAE3RQ9Q8wOuEuI = X-Google-Smtp-Source: AGHT+IHTM1CmX5ehzEM3kxh5RF41j7+zN9LyNjVIo54n3wAf6QiO4XLetsfQYZFw/WJyUBwveuBwVw== X-Received: by 2002:a05:6a00:4fd2:b0:717:87d6:fdd2 with SMTP id d2e1a72fcca58-71b25f00ff4mr19143193b3a.4.1727678029187; Sun, 29 Sep 2024 23:33:49 -0700 (PDT) Received: from chenghaoyang-low.c.googlers.com.com (208.158.221.35.bc.googleusercontent.com. [35.221.158.208]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71b265166e2sm5487671b3a.131.2024.09.29.23.33.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Sep 2024 23:33:48 -0700 (PDT) From: Harvey Yang X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Harvey Yang , Harvey Yang , Kieran Bingham , Jacopo Mondi Subject: [PATCH v14 1/7] libcamera: add DmaBufAllocator::exportBuffers() Date: Mon, 30 Sep 2024 06:29:42 +0000 Message-ID: <20240930063342.3014837-2-chenghaoyang@google.com> X-Mailer: git-send-email 2.46.1.824.gd892dcdcdd-goog In-Reply-To: <20240930063342.3014837-1-chenghaoyang@google.com> References: <20240930063342.3014837-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 | 57 +++++++++++++++++++ 2 files changed, 70 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..f8ed3de2 100644 --- a/src/libcamera/dma_buf_allocator.cpp +++ b/src/libcamera/dma_buf_allocator.cpp @@ -22,6 +22,9 @@ #include #include +#include + +#include /** * \file dma_buf_allocator.cpp @@ -205,4 +208,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 */