From patchwork Sat Sep 7 14:28:26 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: 21193 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 E5216C324C for ; Sat, 7 Sep 2024 14:31:24 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id A5750634FC; Sat, 7 Sep 2024 16:31:21 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="W3J/zO67"; dkim-atps=neutral Received: from mail-pf1-x432.google.com (mail-pf1-x432.google.com [IPv6:2607:f8b0:4864:20::432]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id D0CD6633CC for ; Sat, 7 Sep 2024 16:31:19 +0200 (CEST) Received: by mail-pf1-x432.google.com with SMTP id d2e1a72fcca58-717911ef035so2308952b3a.3 for ; Sat, 07 Sep 2024 07:31:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1725719478; x=1726324278; 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=ykxXuMf7wvHOxfLwxRKz/cbPcfwSaWd3G7r3fOBmhAA=; b=W3J/zO67tqrCQoAEYhPy5HpcQz0B4ixaExqVfE5Hy1GJXPxFQ64tPBqUYo3B1QmRHH loF3IAPaEOvs3F9yh93gJQcfNkZQtd1R5oi8WioWtbQZARiv+qoXYeI06rCVTxgxjttA nUFabilcuB+YHKg6ernbSRAXdJ55oEwSzsF2M= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725719478; x=1726324278; 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=ykxXuMf7wvHOxfLwxRKz/cbPcfwSaWd3G7r3fOBmhAA=; b=UrlV3BSUazrMztNhS/HH4LYn+cpbZQCM97OJ1tRZdM6Q4uSoIriJpQHNdyVgaCBsAt f2BYzSNQFdphNssqxR7XBE2jcrfOWlxaDNVxtqEYce0Y9CIpxIXXk7g1AgG/FreYAXwe 0YUBMmsRhCxdJHPArI561SFh6MOopqk+5uu7njEovBoCKf2g4ZIN7k8MzmgkPzzWPzGD S0tUQFFlomGYumu4Az4xprAt7O8MGDrG14Zyss9afQllmwOVYzmNnT69rl0I4RKwu1j9 7PgeqHriB5QraoFrou5jZWf6TvSiK/vJEPy0ba+MXd0aOnUzcVUcPh2PvA9beIlYK2Oz c5Gw== X-Gm-Message-State: AOJu0YzBIboVGfG0MooWgI7aUMG/lCl0oRC8eo4rsPiNrec54JlqGxJ7 GxGtDHC0JIhM57BI5e2Fn16usooWHvNnPOysuv27+iIe89skTJbY+WtefZUXs6Ki8N+oMMjKmQ4 = X-Google-Smtp-Source: AGHT+IE5tiwhrTyqS7fO21GJvUh3PV83d3SDENceuMWT3pehASWDmyAU6VSpSoz6sBHVP5UJ3Z2oPQ== X-Received: by 2002:a05:6a20:e687:b0:1cf:126c:49fb with SMTP id adf61e73a8af0-1cf1d20c5c7mr5996722637.43.1725719477713; Sat, 07 Sep 2024 07:31:17 -0700 (PDT) Received: from chenghaoyang-low.c.googlers.com.com (0.223.81.34.bc.googleusercontent.com. [34.81.223.0]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20710e33974sm9208935ad.104.2024.09.07.07.31.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Sep 2024 07:31:17 -0700 (PDT) From: Harvey Yang X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Harvey Yang , Harvey Yang Subject: [PATCH v11 1/7] libcamera: add DmaBufAllocator::exportBuffers() Date: Sat, 7 Sep 2024 14:28:26 +0000 Message-ID: <20240907143110.2210711-2-chenghaoyang@google.com> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog In-Reply-To: <20240907143110.2210711-1-chenghaoyang@google.com> References: <20240907143110.2210711-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 --- .../libcamera/internal/dma_buf_allocator.h | 13 +++++ src/libcamera/dma_buf_allocator.cpp | 55 +++++++++++++++++++ 2 files changed, 68 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..d2ac175f 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 */