From patchwork Fri Jul 26 09:54:22 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: 20702 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 E4957BDC71 for ; Fri, 26 Jul 2024 09:56:40 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id B744463373; Fri, 26 Jul 2024 11:56:36 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="hjDYzAmq"; dkim-atps=neutral Received: from mail-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id C9F6A61994 for ; Fri, 26 Jul 2024 11:56:33 +0200 (CEST) Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-1fc587361b6so4101725ad.2 for ; Fri, 26 Jul 2024 02:56:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1721987792; x=1722592592; 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=92eGQpIxfgbeaJOI/KHsvmgmx/r97AIsQENTaLGeeDQ=; b=hjDYzAmq3wjtPEoA2Zmv4CgpDjlSzlFmFGQ8glCOji7N4zrzsFL0O3mv8d8IthdfzR Pu8u5I64sBBbBC7vAr/OQZw70EznGgLL3eK3plxGNdHi/0JrZJ201c34zrAuoQ7RavTA HsnzitZS+ctw4Yvc5uYL7h1uDi3FCuW3jCeYM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721987792; x=1722592592; 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=92eGQpIxfgbeaJOI/KHsvmgmx/r97AIsQENTaLGeeDQ=; b=o5W159/Ks/JJ44CQ4BBDYIEdJ5L/xYr/j13qskY+WPqV3hT3X8Ej22Mik+Atu0xms9 aTe3PWU7FgBykQt5KmiZn6TLib/DgVL4y3mWxXNaq/jJkDqaHiivc2mCTRRkejseBwwB eNgL7WbLlNYK+lujiyGIoe2G9B64uIjohKEAkXbf6lViwtj18FbUr0ZePhtlFHCZOvjX Uxmp0zR1Hrl20B3cvPiTgGO46pXczZtZyz26+JCZ/RUzRyPyrBXSCYoe9nooom9As/Wd Rj36CB2+XH8vBo7r5qhUMDPbOux9aDV0RX9w6ZtwEelw15Z07/mjxUqtDEwBc6SOC5vG jQ/g== X-Gm-Message-State: AOJu0YzVifsEbBKEpxJrL7C69KTEp3JyavVMUusHVIcflNtfYtnDpOSo rmyCT/NbGt8dvACU2lW5cSGEu6fFJq8DVWWJex4NHdbannCxpgYuvYN4UGo64N8uwKQDzUbSBxl v8Q== X-Google-Smtp-Source: AGHT+IH84Yf4ZXnaYFaUuZpLvEgTgj9LaBY99bOsQtsBMrgf2k8tEzCNzob5U5mcl4x32vF4sa0jfw== X-Received: by 2002:a17:902:c409:b0:1fd:8c42:61ca with SMTP id d9443c01a7336-1fed9255795mr53627135ad.25.1721987791931; Fri, 26 Jul 2024 02:56:31 -0700 (PDT) Received: from chenghaoyang-low.c.googlers.com.com (148.175.199.104.bc.googleusercontent.com. [104.199.175.148]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fed7c7f63asm28396665ad.46.2024.07.26.02.56.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jul 2024 02:56:31 -0700 (PDT) From: Harvey Yang To: libcamera-devel@lists.libcamera.org Cc: Harvey Yang , Harvey Yang Subject: [PATCH v6 1/7] Add a helper function exportFrameBuffers in DmaBufAllocator to make it easier to use. Date: Fri, 26 Jul 2024 09:54:22 +0000 Message-ID: <20240726095626.3632402-2-chenghaoyang@chromium.org> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog In-Reply-To: <20240726095626.3632402-1-chenghaoyang@chromium.org> References: <20240726095626.3632402-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" From: Harvey Yang It'll be used in Virtual Pipeline Handler specifically. Signed-off-by: Harvey Yang Change-Id: Ieac63dc7b2aef7dda525e59cce232dd63d316417 --- .../libcamera/internal/dma_buf_allocator.h | 10 ++++ src/libcamera/dma_buf_allocator.cpp | 57 ++++++++++++++++++- 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/include/libcamera/internal/dma_buf_allocator.h b/include/libcamera/internal/dma_buf_allocator.h index 36ec1696b..dd2cc2372 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 c06eca7d0..04079b682 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,52 @@ UniqueFD DmaBufAllocator::alloc(const char *name, std::size_t size) return allocFromHeap(name, size); } +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 */