From patchwork Tue May 16 08:03:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Yang X-Patchwork-Id: 18636 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 DE54BC3284 for ; Tue, 16 May 2023 08:05:10 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 9D6B562867; Tue, 16 May 2023 10:05:10 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1684224310; bh=HYjmhKzl5fDbLuKAJnTnGHJSjSrYYNJ7Dpghg+ylnW0=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=ZV8rDdsknJAKLHu9McL3hLGQX37+/3FjF/GiAvpjpySr57gLRa/o2dPgDbHgu0BtB RGcNh6V7UvTJDWYCK/bOeEOzRLqbzxH3khv3Sk5vZA0iy81G4aLqDKnOtCk9F/2ZM0 LDuRPb/xztJikh3H4qcuyYThL6zFKsFShXgGIhZPCL8DTXjbDo24lj/sl6DEJFPa8i XJAsDeGICXlSBgmUfwnM+vqDxBJru22T4BHUq5N6SGXiW9QYH8Qg92BJ9kAwq9KnHT mdcrnlXK4KrbYpGcD440MtOVVxoEpBZhpEG+O/NlrUFBLGGRoV9fYD3eHwEnnm/bJx jNuRsHhbCGQTg== Received: from mail-pf1-x42c.google.com (mail-pf1-x42c.google.com [IPv6:2607:f8b0:4864:20::42c]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 6E9D562861 for ; Tue, 16 May 2023 10:05:08 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="HwAEoSTB"; dkim-atps=neutral Received: by mail-pf1-x42c.google.com with SMTP id d2e1a72fcca58-64ab2a37812so3586188b3a.1 for ; Tue, 16 May 2023 01:05:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1684224307; x=1686816307; 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=bFVqGdygJtpR3Pal7WbKP0thQNa1erVHwx+INxh1dLI=; b=HwAEoSTB9MWmTqTmA6YL3JpIjkVEEIlLNEMP9x959rzHyYeKJD8KpOlANWuET41w9+ FPCXwLUUBra7KQsOcbIBPV2VZL+pyYjHakjSbBAMThohJAHcOtzom04u46JNj+dbHQy+ wsFyix9QAl5WCj+wWEWUZfuakJEwV8J9VmjXw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684224307; x=1686816307; 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=bFVqGdygJtpR3Pal7WbKP0thQNa1erVHwx+INxh1dLI=; b=T0Xfo9u0pTztqbXR/xzGWp/Szdm7RxuEXWQtQku10fP48b1Ai4+MsZ5bfo3R3ooDmv YxAkzI6ueGSWGI6BBNikbzE7fMae0z4USqbzg9I0AeYNgiQBYxOUry6ihGs6oTYmHHNk S18+uS+Z50lq1xoTrvc6mUjpZBwsFHRCXTK8XabJEwuWjtbGjlaR58rhj5EZRsZPTA0y fn8Jqxz8AgG/lKibI8X1y9TFQfXZTOlMESsmNbNpxAqnqZFIvcXWzCJQAq5ZhqLTYELn wwYcfOdbbCuDw83mNqZTB7/+JtdEgQl7s6Zjx0kROfm3SNXr/Y9oRLgWXPGTKKVEvCmG cpnQ== X-Gm-Message-State: AC+VfDxZjBBWnGIXSgBlEGsN23cgC6X00Kx8ihGtVJxbZUTtO2+wGZii CoY1HzwWwkT52IT+4Ma58OQS9kOZg/8nLrax2izGUQ== X-Google-Smtp-Source: ACHHUZ6iYO1eO6EtCGQsq0sW1v/eKHpmFdAtXUPw9P3kCUCYy2Qwcotwt7y9CIHCZo9cJUMLYY/yEg== X-Received: by 2002:a17:90a:d78f:b0:23f:962e:825d with SMTP id z15-20020a17090ad78f00b0023f962e825dmr43247994pju.1.1684224306463; Tue, 16 May 2023 01:05:06 -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 kd14-20020a17090b348e00b00252df595c01sm996881pjb.52.2023.05.16.01.05.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 May 2023 01:05:06 -0700 (PDT) X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Date: Tue, 16 May 2023 08:03:17 +0000 Message-ID: <20230516080459.711347-2-chenghaoyang@google.com> X-Mailer: git-send-email 2.40.1.606.ga4b1b128d6-goog In-Reply-To: <20230516080459.711347-1-chenghaoyang@google.com> References: <20230516080459.711347-1-chenghaoyang@google.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v5 1/3] libcamera: Move DmaHeap to HeapAllocator as a base class 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: , X-Patchwork-Original-From: Harvey Yang via libcamera-devel From: Harvey Yang Reply-To: Harvey Yang Cc: Cheng-Hao Yang Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" From: Cheng-Hao Yang As other components (like the WIP virtual pipeline handler) also need a heap allocator, move DmaHeap from raspberry pi pipeline handler to a general HeapAllocator as a base class. Signed-off-by: Harvey Yang --- .../libcamera/internal/heap_allocator.h | 18 ++- include/libcamera/internal/meson.build | 1 + src/libcamera/heap_allocator.cpp | 128 ++++++++++++++++++ src/libcamera/meson.build | 1 + src/libcamera/pipeline/meson.build | 5 +- src/libcamera/pipeline/rpi/vc4/dma_heaps.cpp | 90 ------------ src/libcamera/pipeline/rpi/vc4/meson.build | 5 +- src/libcamera/pipeline/rpi/vc4/vc4.cpp | 11 +- 8 files changed, 145 insertions(+), 114 deletions(-) rename src/libcamera/pipeline/rpi/vc4/dma_heaps.h => include/libcamera/internal/heap_allocator.h (50%) create mode 100644 src/libcamera/heap_allocator.cpp delete mode 100644 src/libcamera/pipeline/rpi/vc4/dma_heaps.cpp diff --git a/src/libcamera/pipeline/rpi/vc4/dma_heaps.h b/include/libcamera/internal/heap_allocator.h similarity index 50% rename from src/libcamera/pipeline/rpi/vc4/dma_heaps.h rename to include/libcamera/internal/heap_allocator.h index 0a4a8d86..d061fdce 100644 --- a/src/libcamera/pipeline/rpi/vc4/dma_heaps.h +++ b/include/libcamera/internal/heap_allocator.h @@ -1,8 +1,8 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ /* - * Copyright (C) 2020, Raspberry Pi Ltd + * Copyright (C) 2023, Google Inc. * - * dma_heaps.h - Helper class for dma-heap allocations. + * heap_allocator.h - Helper class for heap buffer allocations. */ #pragma once @@ -13,20 +13,18 @@ namespace libcamera { -namespace RPi { +class Heap; -class DmaHeap +class HeapAllocator { public: - DmaHeap(); - ~DmaHeap(); - bool isValid() const { return dmaHeapHandle_.isValid(); } + HeapAllocator(); + ~HeapAllocator(); + bool isValid() const; UniqueFD alloc(const char *name, std::size_t size); private: - UniqueFD dmaHeapHandle_; + std::unique_ptr heap_; }; -} /* namespace RPi */ - } /* namespace libcamera */ diff --git a/include/libcamera/internal/meson.build b/include/libcamera/internal/meson.build index d7508805..9d25c289 100644 --- a/include/libcamera/internal/meson.build +++ b/include/libcamera/internal/meson.build @@ -26,6 +26,7 @@ libcamera_internal_headers = files([ 'device_enumerator_udev.h', 'formats.h', 'framebuffer.h', + 'heap_allocator.h', 'ipa_manager.h', 'ipa_module.h', 'ipa_proxy.h', diff --git a/src/libcamera/heap_allocator.cpp b/src/libcamera/heap_allocator.cpp new file mode 100644 index 00000000..e9476de5 --- /dev/null +++ b/src/libcamera/heap_allocator.cpp @@ -0,0 +1,128 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2020, Raspberry Pi Ltd + * + * heap_allocator.cpp - Helper class for heap buffer allocations. + */ + +#include "libcamera/internal/heap_allocator.h" + +#include +#include +#include +#include + +#include +#include + +#include + +namespace libcamera { + +/* + * /dev/dma-heap/linux,cma is the dma-heap allocator, which allows dmaheap-cma + * to only have to worry about importing. + * + * Annoyingly, should the cma heap size be specified on the kernel command line + * instead of DT, the heap gets named "reserved" instead. + */ +static constexpr std::array dmaHeapNames = { + "/dev/dma_heap/linux,cma", + "/dev/dma_heap/reserved" +}; + +LOG_DEFINE_CATEGORY(HeapAllocator) + +class Heap +{ +public: + virtual ~Heap() = default; + bool isValid() const { return handle_.isValid(); } + virtual UniqueFD alloc(const char *name, std::size_t size) = 0; + +protected: + UniqueFD handle_; +}; + +class DmaHeap : public Heap +{ +public: + DmaHeap(); + ~DmaHeap(); + UniqueFD alloc(const char *name, std::size_t size) override; +}; + +DmaHeap::DmaHeap() +{ + for (const char *name : dmaHeapNames) { + int ret = ::open(name, O_RDWR | O_CLOEXEC, 0); + if (ret < 0) { + ret = errno; + LOG(HeapAllocator, Debug) << "DmaHeap failed to open " << name << ": " + << strerror(ret); + continue; + } + + handle_ = UniqueFD(ret); + break; + } + + if (!handle_.isValid()) + LOG(HeapAllocator, Error) << "DmaHeap could not open any dmaHeap device"; +} + +DmaHeap::~DmaHeap() = default; + +UniqueFD DmaHeap::alloc(const char *name, std::size_t size) +{ + int ret; + + if (!name) + return {}; + + struct dma_heap_allocation_data alloc = {}; + + alloc.len = size; + alloc.fd_flags = O_CLOEXEC | O_RDWR; + + ret = ::ioctl(handle_.get(), DMA_HEAP_IOCTL_ALLOC, &alloc); + if (ret < 0) { + LOG(HeapAllocator, Error) << "DmaHeap allocation failure for " + << name; + return {}; + } + + UniqueFD allocFd(alloc.fd); + ret = ::ioctl(allocFd.get(), DMA_BUF_SET_NAME, name); + if (ret < 0) { + LOG(HeapAllocator, Error) << "DmaHeap naming failure for " + << name; + return {}; + } + + return allocFd; +} + +HeapAllocator::HeapAllocator() +{ + heap_ = std::make_unique(); +} + +HeapAllocator::~HeapAllocator() = default; + +bool HeapAllocator::isValid() const +{ + return heap_->isValid(); +} + +UniqueFD HeapAllocator::alloc(const char *name, std::size_t size) +{ + if (!isValid()) { + LOG(HeapAllocator, Fatal) << "Allocation attempted without allocator" << name; + return {}; + } + + return heap_->alloc(name, size); +} + +} /* namespace libcamera */ diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build index d4385041..aa2d32a0 100644 --- a/src/libcamera/meson.build +++ b/src/libcamera/meson.build @@ -22,6 +22,7 @@ libcamera_sources = files([ 'framebuffer.cpp', 'framebuffer_allocator.cpp', 'geometry.cpp', + 'heap_allocator.cpp', 'ipa_controls.cpp', 'ipa_data_serializer.cpp', 'ipa_interface.cpp', diff --git a/src/libcamera/pipeline/meson.build b/src/libcamera/pipeline/meson.build index 8a61991c..b6160d34 100644 --- a/src/libcamera/pipeline/meson.build +++ b/src/libcamera/pipeline/meson.build @@ -12,9 +12,6 @@ foreach pipeline : pipelines continue endif - subdirs += pipeline subdir(pipeline) - - # Don't reuse the pipeline variable below, the subdirectory may have - # overwritten it. + subdirs += pipeline endforeach diff --git a/src/libcamera/pipeline/rpi/vc4/dma_heaps.cpp b/src/libcamera/pipeline/rpi/vc4/dma_heaps.cpp deleted file mode 100644 index 317b1fc1..00000000 --- a/src/libcamera/pipeline/rpi/vc4/dma_heaps.cpp +++ /dev/null @@ -1,90 +0,0 @@ -/* SPDX-License-Identifier: LGPL-2.1-or-later */ -/* - * Copyright (C) 2020, Raspberry Pi Ltd - * - * dma_heaps.h - Helper class for dma-heap allocations. - */ - -#include "dma_heaps.h" - -#include -#include -#include -#include -#include -#include - -#include - -/* - * /dev/dma-heap/linux,cma is the dma-heap allocator, which allows dmaheap-cma - * to only have to worry about importing. - * - * Annoyingly, should the cma heap size be specified on the kernel command line - * instead of DT, the heap gets named "reserved" instead. - */ -static constexpr std::array heapNames = { - "/dev/dma_heap/linux,cma", - "/dev/dma_heap/reserved" -}; - -namespace libcamera { - -LOG_DECLARE_CATEGORY(RPI) - -namespace RPi { - -DmaHeap::DmaHeap() -{ - for (const char *name : heapNames) { - int ret = ::open(name, O_RDWR | O_CLOEXEC, 0); - if (ret < 0) { - ret = errno; - LOG(RPI, Debug) << "Failed to open " << name << ": " - << strerror(ret); - continue; - } - - dmaHeapHandle_ = UniqueFD(ret); - break; - } - - if (!dmaHeapHandle_.isValid()) - LOG(RPI, Error) << "Could not open any dmaHeap device"; -} - -DmaHeap::~DmaHeap() = default; - -UniqueFD DmaHeap::alloc(const char *name, std::size_t size) -{ - int ret; - - if (!name) - return {}; - - struct dma_heap_allocation_data alloc = {}; - - alloc.len = size; - alloc.fd_flags = O_CLOEXEC | O_RDWR; - - ret = ::ioctl(dmaHeapHandle_.get(), DMA_HEAP_IOCTL_ALLOC, &alloc); - if (ret < 0) { - LOG(RPI, Error) << "dmaHeap allocation failure for " - << name; - return {}; - } - - UniqueFD allocFd(alloc.fd); - ret = ::ioctl(allocFd.get(), DMA_BUF_SET_NAME, name); - if (ret < 0) { - LOG(RPI, Error) << "dmaHeap naming failure for " - << name; - return {}; - } - - return allocFd; -} - -} /* namespace RPi */ - -} /* namespace libcamera */ diff --git a/src/libcamera/pipeline/rpi/vc4/meson.build b/src/libcamera/pipeline/rpi/vc4/meson.build index cdb049c5..b2b79735 100644 --- a/src/libcamera/pipeline/rpi/vc4/meson.build +++ b/src/libcamera/pipeline/rpi/vc4/meson.build @@ -1,8 +1,5 @@ # SPDX-License-Identifier: CC0-1.0 -libcamera_sources += files([ - 'dma_heaps.cpp', - 'vc4.cpp', -]) +libcamera_sources += files(['vc4.cpp']) subdir('data') diff --git a/src/libcamera/pipeline/rpi/vc4/vc4.cpp b/src/libcamera/pipeline/rpi/vc4/vc4.cpp index 018cf488..410ecfaf 100644 --- a/src/libcamera/pipeline/rpi/vc4/vc4.cpp +++ b/src/libcamera/pipeline/rpi/vc4/vc4.cpp @@ -12,12 +12,11 @@ #include #include "libcamera/internal/device_enumerator.h" +#include "libcamera/internal/heap_allocator.h" #include "../common/pipeline_base.h" #include "../common/rpi_stream.h" -#include "dma_heaps.h" - using namespace std::chrono_literals; namespace libcamera { @@ -87,7 +86,7 @@ public: RPi::Device isp_; /* DMAHEAP allocation helper. */ - RPi::DmaHeap dmaHeap_; + HeapAllocator heapAllocator_; SharedFD lsTable_; struct Config { @@ -296,7 +295,7 @@ int PipelineHandlerVc4::platformRegister(std::unique_ptr &camer { Vc4CameraData *data = static_cast(cameraData.get()); - if (!data->dmaHeap_.isValid()) + if (!data->heapAllocator_.isValid()) return -ENOMEM; MediaEntity *unicamImage = unicam->getEntityByName("unicam-image"); @@ -670,9 +669,9 @@ int Vc4CameraData::platformConfigureIpa(ipa::RPi::ConfigParams ¶ms) { params.ispControls = isp_[Isp::Input].dev()->controls(); - /* Allocate the lens shading table via dmaHeap and pass to the IPA. */ + /* Allocate the lens shading table via heapAllocator and pass to the IPA. */ if (!lsTable_.isValid()) { - lsTable_ = SharedFD(dmaHeap_.alloc("ls_grid", ipa::RPi::MaxLsGridSize)); + lsTable_ = SharedFD(heapAllocator_.alloc("ls_grid", ipa::RPi::MaxLsGridSize)); if (!lsTable_.isValid()) return -ENOMEM; From patchwork Tue May 16 08:03:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Yang X-Patchwork-Id: 18637 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 F3461C3284 for ; Tue, 16 May 2023 08:05:11 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 3260162865; Tue, 16 May 2023 10:05:11 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1684224311; bh=hp6AasDtyYXfBbyBlBaboF2P3cxvD6pPwbnACzMoONU=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=QUqjfzIHgiRWGX62z6r7zjUCa4ifdR0GKYwcB8KGQ87QU+uL7BukkXcA6oguHNUpS 3xcswOw3h2ymfKJHav6fTRN5ed7l/h/eYiX9vUJYzzj7Icl8EeuiprV9le35owO2+F fSUVgJHmHFGfWbfTyg4dND18bhTxwF7ckAFZVS7j6v/PBWf0orM8bYmjZXYWtHASbI pbDOfk8Mu4qPv/Vz9hi0g9Pp5vMtGWmP5VToeWZOCu+hfLTouq8mjratRYz5NLlLdX NCH/RmX6EFO+OXIrk8qm/bKdYLqwfrAV9QwNJ8lQ1iJtQ7Jt391gXicJOD7/SyTFi3 X/UP7gNghNbdQ== Received: from mail-pg1-x536.google.com (mail-pg1-x536.google.com [IPv6:2607:f8b0:4864:20::536]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 4F08E627DC for ; Tue, 16 May 2023 10:05:09 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="lNy+pn7N"; dkim-atps=neutral Received: by mail-pg1-x536.google.com with SMTP id 41be03b00d2f7-52cb78647ecso8702367a12.1 for ; Tue, 16 May 2023 01:05:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1684224307; x=1686816307; 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=3fBg1wkczRxbLUVCBAHdLdU8OPNKRAjOXjhoze11edA=; b=lNy+pn7N9zfLZEN701Mqna66VxB3bIkAUOd1BkOMiQex2nmlew3lRrmQfLjDz2R6iS ErimenBtee8ZhMrNOXLoyY9jH9PnEPiNLFtUWzqS+9HloKaJ2aSW3YGcp13V9QEKEjlu ew1YbII5EYd9fSNh+37TbI5bdz2vxIPQLOLfY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684224307; x=1686816307; 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=3fBg1wkczRxbLUVCBAHdLdU8OPNKRAjOXjhoze11edA=; b=DBRvrlqC6HyMEA6T/Ml9BSu+o4z9Gws+RPQENNvHSQraHKUYpYGm3ixMiiUkTscsZ3 GNd8CuAqAN2dY7Zetf+t5HtqaJXvZcKl90aSNmal82DivdWMG1zEId3AwQcFG+p/kXAy b8MVWXp/e6niR9JjTnTv6PWxRkNt46leRa+fXWsG7mYIgRth30wgfAACDIME3S3OO/Od OFNTB1aPiNd6zTRxSQjrxSrflaFguSqgI3CQABr2JyvJqQFQ9KYrZ91FEKCq3JmvnS/1 igkT6sGWGM4sZ+4pIYYom9MUb1aMLjGj4D27GrwyD3D8scu5H+vuSpvWy7u+evzqxC2U evXg== X-Gm-Message-State: AC+VfDxpIarxORDQ3MOvbgu5ZCQ/1VWzS+0QsNw/LT8jbVpzGvd+5S3o Ok5dZZd2RZwdAIPgObPnYkioEDzXCUDMqb/3m3MdCg== X-Google-Smtp-Source: ACHHUZ6Yc11jgLnWuruTCJojhSH2lrQwguDW6Bn5NnOgwMMjK9LqunMXye3FZNdaYGd62XVSM93PgQ== X-Received: by 2002:a17:90a:880b:b0:250:484e:355f with SMTP id s11-20020a17090a880b00b00250484e355fmr32134459pjn.49.1684224307625; Tue, 16 May 2023 01:05:07 -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 kd14-20020a17090b348e00b00252df595c01sm996881pjb.52.2023.05.16.01.05.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 May 2023 01:05:07 -0700 (PDT) X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Date: Tue, 16 May 2023 08:03:18 +0000 Message-ID: <20230516080459.711347-3-chenghaoyang@google.com> X-Mailer: git-send-email 2.40.1.606.ga4b1b128d6-goog In-Reply-To: <20230516080459.711347-1-chenghaoyang@google.com> References: <20230516080459.711347-1-chenghaoyang@google.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v5 2/3] libcamera: Add UdmaHeap if DmaHeap is not valid 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: , X-Patchwork-Original-From: Harvey Yang via libcamera-devel From: Harvey Yang Reply-To: Harvey Yang Cc: Cheng-Hao Yang Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" From: Cheng-Hao Yang If DmaHeap is not valid, fall back to UdmaHeap to allocate buffers. Signed-off-by: Harvey Yang Reviewed-by: Kieran Bingham --- src/libcamera/heap_allocator.cpp | 106 +++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/src/libcamera/heap_allocator.cpp b/src/libcamera/heap_allocator.cpp index e9476de5..682a7e01 100644 --- a/src/libcamera/heap_allocator.cpp +++ b/src/libcamera/heap_allocator.cpp @@ -10,10 +10,14 @@ #include #include #include +#include +#include +#include #include #include #include +#include #include @@ -52,6 +56,14 @@ public: UniqueFD alloc(const char *name, std::size_t size) override; }; +class UdmaHeap : public Heap +{ +public: + UdmaHeap(); + ~UdmaHeap(); + UniqueFD alloc(const char *name, std::size_t size) override; +}; + DmaHeap::DmaHeap() { for (const char *name : dmaHeapNames) { @@ -103,9 +115,103 @@ UniqueFD DmaHeap::alloc(const char *name, std::size_t size) return allocFd; } +UdmaHeap::UdmaHeap() +{ + int ret = ::open("/dev/udmabuf", O_RDWR, 0); + if (ret < 0) { + ret = errno; + LOG(HeapAllocator, Error) + << "UdmaHeap failed to open allocator: " << strerror(ret); + + if (ret == EACCES) { + LOG(HeapAllocator, Info) + << "UdmaHeap: Consider making /dev/udmabuf accessible by the video group"; + LOG(HeapAllocator, Info) + << "UdmaHeap: Alternatively, add your user to the kvm group."; + } + + } else { + handle_ = UniqueFD(ret); + } +} + +UdmaHeap::~UdmaHeap() = default; + +UniqueFD UdmaHeap::alloc(const char *name, std::size_t size) +{ + if (!isValid()) { + LOG(HeapAllocator, Fatal) << "UdmaHeap: Allocation attempted without allocator" << name; + return {}; + } + + int ret; + + ret = memfd_create(name, MFD_ALLOW_SEALING); + if (ret < 0) { + ret = errno; + LOG(HeapAllocator, Error) + << "UdmaHeap failed to allocate memfd storage: " + << strerror(ret); + return {}; + } + + UniqueFD memfd(ret); + + ret = ftruncate(memfd.get(), size); + if (ret < 0) { + ret = errno; + LOG(HeapAllocator, Error) + << "UdmaHeap failed to set memfd size: " << strerror(ret); + return {}; + } + + /* UdmaHeap Buffers *must* have the F_SEAL_SHRINK seal */ + ret = fcntl(memfd.get(), F_ADD_SEALS, F_SEAL_SHRINK); + if (ret < 0) { + ret = errno; + LOG(HeapAllocator, Error) + << "UdmaHeap failed to seal the memfd: " << strerror(ret); + return {}; + } + + struct udmabuf_create create; + + create.memfd = memfd.get(); + create.flags = UDMABUF_FLAGS_CLOEXEC; + create.offset = 0; + create.size = size; + + ret = ::ioctl(handle_.get(), UDMABUF_CREATE, &create); + if (ret < 0) { + ret = errno; + LOG(HeapAllocator, Error) + << "UdmaHeap failed to allocate " << size << " bytes: " + << strerror(ret); + return {}; + } + + /* The underlying memfd is kept as as a reference in the kernel */ + UniqueFD uDma(ret); + + if (create.size != size) + LOG(HeapAllocator, Warning) + << "UdmaHeap allocated " << create.size << " bytes instead of " + << size << " bytes"; + + /* Fail if not suitable, the allocation will be free'd by UniqueFD */ + if (create.size < size) + return {}; + + LOG(HeapAllocator, Debug) << "UdmaHeap allocated " << create.size << " bytes"; + + return uDma; +} + HeapAllocator::HeapAllocator() { heap_ = std::make_unique(); + if (!isValid()) + heap_ = std::make_unique(); } HeapAllocator::~HeapAllocator() = default; From patchwork Tue May 16 08:03:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Yang X-Patchwork-Id: 18638 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 B53C6C3285 for ; Tue, 16 May 2023 08:05:12 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id A8A8D6286E; Tue, 16 May 2023 10:05:11 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1684224311; bh=txUIkZ3u9xSb2SNYvo9IkUSeGXC5nQtPTUZA9q/8tfI=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=CFIT9I6PtnZC3jIBvKk7rxHACrD+/GuLQOw/Jj0L7iueu2h+/e9dDv7Cggp588NkY DpnRpMaz5Rhx6OsH0QeCRMqG2l2ra8oao8p7cZ7s98VYaMpGBxOYgV6GtrusR65S+P e6TDlcf822uJ0LNBAzmty7QS/CwSU/o+J3eymX5JcRE6x0ATbAfnQO2v6xSOx0EZQx xsXXmRONNZutGQPTDI4e3y6HYsGQGzLvJ2LxLescSxR+GOyYibsbNdJX9xMxt7H6qn XmFY4rHj6WyjlzXe6nJazTSx5v+l0tfCxfj6iLTZHafOnQ4CoZ/MTK0AvoZQCjfwka J4UY5NmSRgI+A== Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 37EAB62861 for ; Tue, 16 May 2023 10:05:10 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="P1WUp9z1"; dkim-atps=neutral Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-1aaea3909d1so128993285ad.2 for ; Tue, 16 May 2023 01:05:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1684224308; x=1686816308; 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=ApWxcQmz7KVlqjwqFr0G5ziyjKDyN2vbRBPSrxD8FOk=; b=P1WUp9z18A4UXqUTxw3ZE98c7BtHVNb7F/sVqJYUsE+lo+BggN+PkalO08aOl7Mn7+ kiPlvnUni+5cXNT2WlmhnRE9J5puCS1G0XMQVDf2W/mDbnNXoudkWsJXSdqWMY45jwzt hojZ2KQtxmX+/A9o8siOgXgrqWEaROqnUK0nA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684224308; x=1686816308; 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=ApWxcQmz7KVlqjwqFr0G5ziyjKDyN2vbRBPSrxD8FOk=; b=UKh44KyuriSVCPN1SCzl85EVF5JHd1lmKR/jV4jKzIIYnYZd3OOKA4O4VQiqsGfPry FNM6odGJPbRPtv7xbuYBtWQb22KlSBuXDZPm978E5LZiZg2meEzCi9UIDN1H2t31zs2o r38kXOZpL28vljiB/U85Q+EHHp+wNRxLfjU0GQPcpjN1uisPybXJ2A2OC7diUuL0ygpe YXhq6rVcZp5SS0ggle+s/H68mKdE+Gx0lTUy6g+2KTXMqjJjGQ5lp7MCZ+7wql2EMMle 7kPdgChu5JoTbxqckGNn+OxoN+7tHryfKobg+QP2WM+BCzOHHyXFlsuLxp71lQJmQcO0 r9gw== X-Gm-Message-State: AC+VfDweRA9QXKwkzFjgWuzy0YW7P3odTo0f5pWCJiG/kWRv6HaMn6mP yEnFM02daAIuHgEvD3uG1MA/52hxsTrf9CSx38oYTQ== X-Google-Smtp-Source: ACHHUZ7+ZsXKe4OzadC7O6u390DSP/aHvVlwNvAbndQGjgYDTL8aTcMtQC1RxUlDtUNioymZIkMZcg== X-Received: by 2002:a17:902:f548:b0:1ad:cb4b:1d50 with SMTP id h8-20020a170902f54800b001adcb4b1d50mr22055162plf.43.1684224308636; Tue, 16 May 2023 01:05:08 -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 kd14-20020a17090b348e00b00252df595c01sm996881pjb.52.2023.05.16.01.05.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 May 2023 01:05:08 -0700 (PDT) X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Date: Tue, 16 May 2023 08:03:19 +0000 Message-ID: <20230516080459.711347-4-chenghaoyang@google.com> X-Mailer: git-send-email 2.40.1.606.ga4b1b128d6-goog In-Reply-To: <20230516080459.711347-1-chenghaoyang@google.com> References: <20230516080459.711347-1-chenghaoyang@google.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v5 3/3] libcamera: Add exportFrameBuffers in HeapAllocator 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: , X-Patchwork-Original-From: Harvey Yang via libcamera-devel From: Harvey Yang Reply-To: Harvey Yang Cc: Cheng-Hao Yang Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" From: Cheng-Hao Yang Add a helper function exportFrameBuffers in HeapAllocator to make it easier to use. Signed-off-by: Harvey Yang Reviewed-by: Jacopo Mondi --- include/libcamera/internal/heap_allocator.h | 9 +++ src/libcamera/heap_allocator.cpp | 61 +++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/include/libcamera/internal/heap_allocator.h b/include/libcamera/internal/heap_allocator.h index d061fdce..b6488fbc 100644 --- a/include/libcamera/internal/heap_allocator.h +++ b/include/libcamera/internal/heap_allocator.h @@ -8,12 +8,16 @@ #pragma once #include +#include #include namespace libcamera { +class Camera; +class FrameBuffer; class Heap; +class Stream; class HeapAllocator { @@ -23,7 +27,12 @@ public: bool isValid() const; UniqueFD alloc(const char *name, std::size_t size); + int exportFrameBuffers(Camera *camera, Stream *stream, + std::vector> *buffers); + private: + std::unique_ptr createBuffer(Stream *stream); + std::unique_ptr heap_; }; diff --git a/src/libcamera/heap_allocator.cpp b/src/libcamera/heap_allocator.cpp index 682a7e01..ce3a815d 100644 --- a/src/libcamera/heap_allocator.cpp +++ b/src/libcamera/heap_allocator.cpp @@ -21,6 +21,12 @@ #include +#include +#include +#include + +#include "libcamera/internal/formats.h" + namespace libcamera { /* @@ -231,4 +237,59 @@ UniqueFD HeapAllocator::alloc(const char *name, std::size_t size) return heap_->alloc(name, size); } +int HeapAllocator::exportFrameBuffers([[maybe_unused]] Camera *camera, Stream *stream, + std::vector> *buffers) +{ + unsigned int count = stream->configuration().bufferCount; + + /** \todo Support multiplanar allocations */ + + for (unsigned i = 0; i < count; ++i) { + std::unique_ptr buffer = createBuffer(stream); + if (!buffer) { + LOG(HeapAllocator, Error) << "Unable to create buffer"; + + buffers->clear(); + return -EINVAL; + } + + buffers->push_back(std::move(buffer)); + } + + return count; +} + +std::unique_ptr HeapAllocator::createBuffer(Stream *stream) +{ + std::vector planes; + + unsigned int frameSize = stream->configuration().frameSize; + int pageSize = getpagesize(); + // Allocation size need to be a direct multiple of |pageSize|. + unsigned int numPage = (frameSize + pageSize - 1) / pageSize; + + UniqueFD fd = alloc("FrameBuffer", numPage * pageSize); + if (!fd.isValid()) + return nullptr; + + auto info = PixelFormatInfo::info(stream->configuration().pixelFormat); + SharedFD shared_fd(std::move(fd)); + unsigned int offset = 0; + for (size_t i = 0; i < info.planes.size(); ++i) { + unsigned int planeSize = info.planeSize(stream->configuration().size, i); + if (planeSize == 0) + continue; + + FrameBuffer::Plane plane; + plane.fd = shared_fd; + plane.offset = offset; + plane.length = planeSize; + planes.push_back(std::move(plane)); + + offset += planeSize; + } + + return std::make_unique(planes); +} + } /* namespace libcamera */