From patchwork Thu Aug 1 07:30:57 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: 20739 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 ECDD9BDC71 for ; Thu, 1 Aug 2024 07:33:54 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id A616C63381; Thu, 1 Aug 2024 09:33:50 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="ml5R8m3m"; dkim-atps=neutral Received: from mail-pg1-x52d.google.com (mail-pg1-x52d.google.com [IPv6:2607:f8b0:4864:20::52d]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id D41C86336E for ; Thu, 1 Aug 2024 09:33:48 +0200 (CEST) Received: by mail-pg1-x52d.google.com with SMTP id 41be03b00d2f7-6c5bcb8e8edso5026235a12.2 for ; Thu, 01 Aug 2024 00:33:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1722497627; x=1723102427; 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=1CEAOFSE3yro1ER8LBRGSD86TGgGuzI5qgNS96RTubY=; b=ml5R8m3mmwbOQYI5sbuQuHRodrwvU282+BlC0R4nv8moB5HTuUXASlmrcT2Srau0Ve JrmnORJQ2Kk9mwg0gSrAxu++RhGR1wEvkM1ElFvQV9s9vjaS0mKe5snifO4mKv/o8W9D eb7A6dFlKg38vITQnaI+iYj0wrSQDx13+aSgc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722497627; x=1723102427; 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=1CEAOFSE3yro1ER8LBRGSD86TGgGuzI5qgNS96RTubY=; b=KVdKI3vSv4dUuXSBe0UWpgvrIeBUwvYfNnUSyU7d2x8bLEXZDSvK0Qx+epK9qAWGM7 vUWL2sud5Cc7rbMeP+8WdgjLeCDkBA0JVZ8LJ65puKFyn8Gq/wzV7iEkfDNTMX5VpSnZ G6zj7pIzKw2ROMxU0Cfgkmt4gPkHvDyckdJ1UgqklWW8uehy2YojknwBnbBz9KVY4HcO B3mNiLNMbjQug2n9Sx8i+uN/HSigjfgKnAR+qsXGN6xtm+ZTcYqRbFkPS2zUWb9nEHdY O2Krdqsq/AcU0FeFlyrWgWsD9HYEdKdRJ9iJs05Z+CeFLygNgpGHjNmkDy3MeCP7Cwqj YGMQ== X-Gm-Message-State: AOJu0YxgIaRcVjjpDwJ/abnVTaotZXUQyfbMkbLnJbIY+2x7qRP+Vl3Z znx+vJd6m5xT9ee8g5H20CV0myTU+ulqMj3u4SGuUeq34Z3opgnXpBLSGSlTPZL+eap7Qf8z1k2 Q2Q== X-Google-Smtp-Source: AGHT+IEXXiXbZ0hKdn9Rgnsspya41rcNpBZAEoYwNF1UpYIQzWmcRZ054Y0ze+LrbAOOBP/gRvIw/Q== X-Received: by 2002:a05:6a20:e607:b0:1c4:818c:299d with SMTP id adf61e73a8af0-1c68cec900bmr1945453637.11.1722497626944; Thu, 01 Aug 2024 00:33:46 -0700 (PDT) Received: from chenghaoyang-low.c.googlers.com.com (237.198.80.34.bc.googleusercontent.com. [34.80.198.237]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fed7cd3dcbsm131839165ad.73.2024.08.01.00.33.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Aug 2024 00:33:46 -0700 (PDT) From: Harvey Yang X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Harvey Yang , Harvey Yang Subject: [PATCH v7 1/7] libcamera: add DmaBufAllocation::exportFrameBuffers() Date: Thu, 1 Aug 2024 07:30:57 +0000 Message-ID: <20240801073339.4061027-2-chenghaoyang@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog In-Reply-To: <20240801073339.4061027-1-chenghaoyang@google.com> References: <20240801073339.4061027-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 exportFrameBuffers in DmaBufAllocator to make it easier to use. It'll be used in Virtual Pipeline Handler specifically. Signed-off-by: Harvey Yang --- .../libcamera/internal/dma_buf_allocator.h | 10 +++ src/libcamera/dma_buf_allocator.cpp | 68 ++++++++++++++++++- 2 files changed, 76 insertions(+), 2 deletions(-) diff --git a/include/libcamera/internal/dma_buf_allocator.h b/include/libcamera/internal/dma_buf_allocator.h index 36ec1696..dd2cc237 100644 --- a/include/libcamera/internal/dma_buf_allocator.h +++ b/include/libcamera/internal/dma_buf_allocator.h @@ -8,12 +8,16 @@ #pragma once #include +#include #include #include namespace libcamera { +class FrameBuffer; +struct StreamConfiguration; + class DmaBufAllocator { public: @@ -30,7 +34,13 @@ public: bool isValid() const { return providerHandle_.isValid(); } UniqueFD alloc(const char *name, std::size_t size); + int exportFrameBuffers( + const StreamConfiguration &config, + std::vector> *buffers); + private: + std::unique_ptr createBuffer(const StreamConfiguration &config); + 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 c06eca7d..6b406880 100644 --- a/src/libcamera/dma_buf_allocator.cpp +++ b/src/libcamera/dma_buf_allocator.cpp @@ -23,6 +23,11 @@ #include +#include +#include + +#include "libcamera/internal/formats.h" + /** * \file dma_buf_allocator.cpp * \brief dma-buf allocator @@ -130,8 +135,8 @@ DmaBufAllocator::~DmaBufAllocator() = default; /* uClibc doesn't provide the file sealing API. */ #ifndef __DOXYGEN__ #if not HAVE_FILE_SEALS -#define F_ADD_SEALS 1033 -#define F_SEAL_SHRINK 0x0002 +#define F_ADD_SEALS 1033 +#define F_SEAL_SHRINK 0x0002 #endif #endif @@ -243,4 +248,63 @@ UniqueFD DmaBufAllocator::alloc(const char *name, std::size_t size) return allocFromHeap(name, size); } +/** + * \brief Allocate and export buffers for \a stream from the DmaBufAllocator + * \param[in] config The config of the stream to allocate buffers for + * \param[out] buffers Array of buffers successfully allocated + * + * Allocates buffers for a stream from the DmaBufAllocator. It's a helper + * function that'll be used in PipelineHandler::exportFrameBuffers(). + * + * \return The number of allocated buffers on success or a negative error code + * otherwise + */ +int DmaBufAllocator::exportFrameBuffers( + const StreamConfiguration &config, + std::vector> *buffers) +{ + unsigned int count = config.bufferCount; + + for (unsigned i = 0; i < count; ++i) { + std::unique_ptr buffer = createBuffer(config); + 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( + const StreamConfiguration &config) +{ + std::vector planes; + + auto info = PixelFormatInfo::info(config.pixelFormat); + for (size_t i = 0; i < info.planes.size(); ++i) { + unsigned int planeSize = info.planeSize(config.size, i); + if (planeSize == 0) + continue; + + UniqueFD fd = alloc("FrameBuffer", planeSize); + if (!fd.isValid()) + return nullptr; + + SharedFD sharedFd(std::move(fd)); + + FrameBuffer::Plane plane; + plane.fd = sharedFd; + plane.offset = 0; + plane.length = planeSize; + planes.push_back(std::move(plane)); + } + + return std::make_unique(planes); +} + } /* namespace libcamera */