From patchwork Tue Oct 22 07:43:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Yang X-Patchwork-Id: 21725 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 E2EC1C3272 for ; Tue, 22 Oct 2024 07:45:59 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id A1B0265398; Tue, 22 Oct 2024 09:45:59 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="oJpwRUvA"; dkim-atps=neutral Received: from mail-ot1-x32f.google.com (mail-ot1-x32f.google.com [IPv6:2607:f8b0:4864:20::32f]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 357DA65395 for ; Tue, 22 Oct 2024 09:45:53 +0200 (CEST) Received: by mail-ot1-x32f.google.com with SMTP id 46e09a7af769-718119fc061so2087376a34.2 for ; Tue, 22 Oct 2024 00:45:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1729583151; x=1730187951; 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=6BE58G+d8eNOduzGj/+av0/CduK1GmcHJNUUTiaSAbo=; b=oJpwRUvACEnQkWmTTzyg+HAHkcT6adkyHkVV2tf5BkIlNOuxHLG/jX53EOLFGcDDEl vpwHdCLLQFdV3olA6KzfcJ0nHAyzo358/JrNMkdeHqNffh4t8r1oj2316zDZtFW17aBi l+dv7v0reoAP1ezqqbWgNaC09A4Jmu+QsKhlg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729583151; x=1730187951; 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=6BE58G+d8eNOduzGj/+av0/CduK1GmcHJNUUTiaSAbo=; b=S+YWRNNSAz6+a+hRqwdP24djitD3p0rKh34MpoTv97xauhnW8c6osqUOy4hR8DvGpt CH1a5HvBi5NmX+MfX8SchRykYSLi2IwkXY+SoiMRAyJ6PEXUjb5i5K1daYWPajr3BGCK TiS5x/EiHTfIdRYhuVXSCHdafd0eSGEb6A3r9daZh5iUu5bQUZkHdeKOxfYeK5ULqY9K dDK1BC5USHRtXbNgL8uaCg3cU62SaIldZHRCrFAWmnmO0CKIUNr0HPFSQ+5UsZ+HayfM XetXzCadL1/iQZPFDfM5UfC9pwGdtVNq++Uu1HyILXBipVNIHH5HHo7kOeg0nkMQll7z mPpw== X-Gm-Message-State: AOJu0YwIndAScSpGgBS/VXx5cnpXFtJrYRydVKy4Ka+55SGzAOzszCtJ IMUgcybJna8q9es26IDK7i2/6i4QhPlqdJt6b9KyaBhTybI2TL5UkBD9vS/xO5jR2u5M3n6OBsk = X-Google-Smtp-Source: AGHT+IF6jedLDpIgT3SLOGuRrzIqiwLQkCRaSn/RXU0/KGlGt9EhvH8PGIpbRont1s3z6hpXQnxxpg== X-Received: by 2002:a05:6870:c113:b0:287:cb2:c0e with SMTP id 586e51a60fabf-2892c2dee9amr12691295fac.12.1729583150783; Tue, 22 Oct 2024 00:45:50 -0700 (PDT) Received: from chenghaoyang-low.c.googlers.com.com (27.247.221.35.bc.googleusercontent.com. [35.221.247.27]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71ec13d75aesm4091024b3a.124.2024.10.22.00.45.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Oct 2024 00:45:50 -0700 (PDT) From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Harvey Yang , Kieran Bingham , Jacopo Mondi Subject: [PATCH v16 1/7] libcamera: add DmaBufAllocator::exportBuffers() Date: Tue, 22 Oct 2024 07:43:37 +0000 Message-ID: <20241022074544.3790451-2-chenghaoyang@chromium.org> X-Mailer: git-send-email 2.47.0.105.g07ac214952-goog In-Reply-To: <20241022074544.3790451-1-chenghaoyang@chromium.org> References: <20241022074544.3790451-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" 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 d2a0a0d19..9211d4837 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 &planeSizes, + std::vector> *buffers); + private: + std::unique_ptr createBuffer( + std::string name, const std::vector &planeSizes); + 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..262eb53a0 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] planeSizes 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 &planeSizes, + std::vector> *buffers) +{ + for (unsigned int i = 0; i < count; ++i) { + std::unique_ptr buffer = + createBuffer("frame-" + std::to_string(i), planeSizes); + 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 &planeSizes) +{ + std::vector planes; + + unsigned int frameSize = 0, offset = 0; + for (auto planeSize : planeSizes) + frameSize += planeSize; + + SharedFD fd(alloc(name.c_str(), frameSize)); + if (!fd.isValid()) + return nullptr; + + for (auto planeSize : planeSizes) { + planes.emplace_back(FrameBuffer::Plane{ fd, offset, planeSize }); + offset += planeSize; + } + + return std::make_unique(planes); +} + } /* namespace libcamera */