From patchwork Thu Sep 26 16:29:50 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: 21385 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 654F0C3257 for ; Thu, 26 Sep 2024 16:31:53 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 0787F63515; Thu, 26 Sep 2024 18:31:53 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="VXDn8AJn"; dkim-atps=neutral Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id DDCBF63502 for ; Thu, 26 Sep 2024 18:31:48 +0200 (CEST) Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-20551eeba95so11444775ad.2 for ; Thu, 26 Sep 2024 09:31:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1727368307; x=1727973107; 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=VXDn8AJn4N2C5X5nhG74pT5nrvjh000LuelT9p6vz4iqkKHpOcVmvKeTYNRIwE5t/T hZY2ZrfvSMrYxXT7JZcZOkSwHdr+kGMkAs0dfO5XmWJeLaVumnQI83YYugFtdY712DI3 QxRj36KcUg1R789icsS7dQkB+qjEduRnonNlg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727368307; x=1727973107; 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=EhIFoTYU5Ky6a70+TTWKGe3fP4O/kZymEPYI6Z8J3kd4saKde7/M7/7AlozvJhNcOW 1DfFkA4GWC9MMfBMvYCQliu/KaP5NjqSVvPRTySeG80WenDrq+qzVfdqYxqqx8BuTkXp xj3Lu5Uz5aJ1hdRIl03X2UeaHmLBzHtv4jQc39SjRDIpHwumLEu0e/2rydTnl6MT6yXZ 2UoQsT6GoBvbjsXI3o0ctbtVAUZVvKiJgUICiv3/CLSDGlBe8rebkdTWBxLgQspYGJ4K 8ataJ48LJgci2rYyzcFo9trACirbFH9FCMCWXVpPnjfH3SN3jGSL9S+At7htNgGb7ZiQ 83kw== X-Gm-Message-State: AOJu0YwMByNIHJlTK2vLK2gqPKRVZ3A7hiCX5WWvAOqtcCPO3V3RlQm8 ejZxoRILfoi5KZZbHnFnRmBNi1vN9ShimfQFp51IyQECw+hy9Qlf7z8ecO4moOU58YP8lGSTTRc = X-Google-Smtp-Source: AGHT+IFY2OgLfuUWDox+NUEL+BsWVJVZM/P3RvvkHHfugL4VAn6bBbQ/inJ9R0Sg9HpqwcuVtPN2Pw== X-Received: by 2002:a17:903:1c2:b0:205:4613:30bf with SMTP id d9443c01a7336-20b37b915c4mr4700855ad.55.1727368306789; Thu, 26 Sep 2024 09:31:46 -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 d9443c01a7336-20b37e20dffsm469035ad.170.2024.09.26.09.31.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Sep 2024 09:31:46 -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 v13 1/7] libcamera: add DmaBufAllocator::exportBuffers() Date: Thu, 26 Sep 2024 16:29:50 +0000 Message-ID: <20240926163141.1593026-2-chenghaoyang@google.com> X-Mailer: git-send-email 2.46.1.824.gd892dcdcdd-goog In-Reply-To: <20240926163141.1593026-1-chenghaoyang@google.com> References: <20240926163141.1593026-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 */