From patchwork Wed Aug 2 06:51:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cheng-Hao Yang X-Patchwork-Id: 18921 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 9CA91C32A9 for ; Wed, 2 Aug 2023 06:55:03 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id D4DAE627F0; Wed, 2 Aug 2023 08:55:01 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1690959301; bh=NbU3svJ9dJf8o71kciW8iYXkpdjJmbB9d1DtbAtx0Q8=; 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=tah0j8hZgYPcYELNdnvlUT4O1n6y3asg8TMVF40bPI5htnRnPfZf0MMHW9HHaIrgl xjA6R+zXtGE6RemFT4ojM58GvL5ryKkA5y1iAbHk7CSN+NBaFuZjVIk8XwnBpQEGJO g2vRBCk/X4X0RMgetLcPazpUJDTo9vVZyg2iwxRugZOcP2IO0iPz8lmygtuQOwtm2N 82AXrW5K8qBUvcSMCA4ZD8dq93eh0RXpb8yXG+dE/JpPfT1QcfV3pH+bEa98y8GfKq vCppJpPLo8LwUoiqPqsIO3QrGCqD0kdSOMyQDx5iWcYKUM8os4RCyGN4QRSEU7NhMX e1zPxCAcxWk4w== Received: from mail-pf1-x42e.google.com (mail-pf1-x42e.google.com [IPv6:2607:f8b0:4864:20::42e]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id D8B5F61E1A for ; Wed, 2 Aug 2023 08:54:59 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="WtlLgsAP"; dkim-atps=neutral Received: by mail-pf1-x42e.google.com with SMTP id d2e1a72fcca58-686f25d045cso4442350b3a.0 for ; Tue, 01 Aug 2023 23:54:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1690959298; x=1691564098; 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=y5i4ECrxW9gevPh/tneYjpfKD/8bJyg9RHUiQPPTrlE=; b=WtlLgsAPdKzROYxhzRu1dZPHFcYCqsV+OLc1J/CGG7Agez0ZBLS75p5rL2c8tOAIMN JDUJrnl+EZ+n408GVAOZ/SFUc3nQj+OzKkw4bqjBnwLir561xmTqQbmQAkZPqqsvCreP 0lxhzE45ysfeUvHlIRxpOh4cm4Woatq2SQlAk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690959298; x=1691564098; 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=y5i4ECrxW9gevPh/tneYjpfKD/8bJyg9RHUiQPPTrlE=; b=FlEHdBEQozCmX4AlQ7Ej3O74Q9vS6A+p4B6qPx9GUtIm+Idv+rLCR89IHaGkX4L5bZ KgZkKvYQwWnJhasTg1jxsmsxLscDezM528FUiEYzgc+s5Mh93bRnEx5nNVz7W4Eap+of 56JyK5wuaPKGUHmw/MkDGwSDCK4+VamZK3mJyQXabUhmurJVe4tnlZfzYdZiG3+Lq1jQ NWgN3D1l38uJiE/vwEfCdSKRLcc7EqD6MAAtyYvwzmMRM//wz3EzyOPjPBZhSkDLsq8l eBMVKkhOiP90NY9JrFax8qpGDOTbkTK0sWqDzE3PsaKhWINmIUQC34NF2jNZwLybcLl1 I+Uw== X-Gm-Message-State: ABy/qLZV3A009QndKTkweTwYrW1BCBrEouvHSGhC3QCjwirAdvieVLrV IrU0SarDr1actd/fyQctxTB6B65Z15BDX727+t4= X-Google-Smtp-Source: APBJJlGAkQEi/+2lIh3VJqvyhn5Zwm7IdWOonB0dpzhnLZx2wnzUtJ4DwbutSoNovn1mkVfAaeDEDQ== X-Received: by 2002:a05:6a00:2da6:b0:687:41cb:e274 with SMTP id fb38-20020a056a002da600b0068741cbe274mr6866827pfb.9.1690959297911; Tue, 01 Aug 2023 23:54:57 -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 d14-20020aa7868e000000b0065980654baasm9142154pfo.130.2023.08.01.23.54.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Aug 2023 23:54:57 -0700 (PDT) To: libcamera-devel@lists.libcamera.org Date: Wed, 2 Aug 2023 06:51:43 +0000 Message-ID: <20230802065449.2904457-2-chenghaoyang@chromium.org> X-Mailer: git-send-email 2.41.0.585.gd2178a4bd4-goog In-Reply-To: <20230802065449.2904457-1-chenghaoyang@chromium.org> References: <20230802065449.2904457-1-chenghaoyang@chromium.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v7 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: Cheng-Hao Yang Reply-To: Harvey Yang Cc: Harvey Yang Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" 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 | 192 ++++++++++++++++++ src/libcamera/meson.build | 1 + 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 +- 7 files changed, 209 insertions(+), 109 deletions(-) rename src/libcamera/pipeline/rpi/vc4/dma_heaps.h => include/libcamera/internal/heap_allocator.h (57%) 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 57% rename from src/libcamera/pipeline/rpi/vc4/dma_heaps.h rename to include/libcamera/internal/heap_allocator.h index 0a4a8d86..6620fa23 100644 --- a/src/libcamera/pipeline/rpi/vc4/dma_heaps.h +++ b/include/libcamera/internal/heap_allocator.h @@ -1,8 +1,9 @@ /* 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 +14,19 @@ 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 7f1f3440..3030d039 100644 --- a/include/libcamera/internal/meson.build +++ b/include/libcamera/internal/meson.build @@ -27,6 +27,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..2a825d93 --- /dev/null +++ b/src/libcamera/heap_allocator.cpp @@ -0,0 +1,192 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2020, Raspberry Pi Ltd + * Copyright (C) 2023, Google Inc. + * + * heap_allocator.cpp - Helper class for heap buffer allocations. + */ + +#include "libcamera/internal/heap_allocator.h" + +#include +#include +#include +#include + +#include +#include + +#include + +/** + * \file libcamera/internal/heap_allocator.h + * \brief Internal heap buffer allocator support + */ + +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", + "/dev/dma_heap/system" +}; + +LOG_DEFINE_CATEGORY(HeapAllocator) + +/** + * \class Heap + * \brief The virtual base class for different implementations + */ +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_; +}; + +/** + * \var Heap::handle_ + * \brief A common UniqueFD to allocate buffers + */ + +/** + * \fn Heap::isValid() + * \brief Return true if the heap is valid + */ + +/** + * \fn Heap::alloc() + * \brief Return a valid UniqueFD if a heap buffer is allocated + * \param[in] name The name of the allocated buffer + * \param[in] size The size in byte of the allocated buffer + */ + +/** + * \class DmaHeap + * \brief The derived class of Heap with DMA implementation + */ +class DmaHeap : public Heap +{ +public: + DmaHeap(); + ~DmaHeap(); + UniqueFD alloc(const char *name, std::size_t size) override; +}; + +/** + * \brief Construct a DmaHeap with a list of |dmaHeapNames| + */ +DmaHeap::DmaHeap() +{ + for (const char *name : dmaHeapNames) { + int ret = ::open(name, O_RDWR | O_CLOEXEC); + 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; +} + +/** + * \class HeapAllocator + * \brief The allocator that allocates heap buffers + * + * HeapAllocator class is the interface for pipeline handlers to allocate heap + * buffers. It finds a usable heap implementation, if any, and users don't need + * to know the details. Users should check if the HeapAllocator instance is + * valid though before using it. + */ + +/** + * \var HeapAllocator::heap_ + * \brief The heap instance used to allocate heap buffers. + * Should be initialized in the c'tor. + */ + +/** + * \brief Construct a HeapAllocator with DmaHeap implementation + */ +HeapAllocator::HeapAllocator() +{ + heap_ = std::make_unique(); +} + +HeapAllocator::~HeapAllocator() = default; + +/** + * \fn HeapAllocator::isValid() + * \brief Return true if the HeapAllocator works with a Heap implementation + */ +bool HeapAllocator::isValid() const +{ + return heap_->isValid(); +} + +/** + * \fn HeapAllocator::alloc() + * \brief Return a valid UniqueFD if a heap buffer is allocated + * \param[in] name The name of the allocated buffer + * \param[in] size The size in byte of the allocated buffer + */ +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 b24f8296..967f08e6 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/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 Wed Aug 2 06:51:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cheng-Hao Yang X-Patchwork-Id: 18922 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 C429FBDCBF for ; Wed, 2 Aug 2023 06:55:05 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 73689627EB; Wed, 2 Aug 2023 08:55:05 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1690959305; bh=vqo7+QhOPbQhtAzNZWEISoB9rGYX5iYooUeCBKzyYQM=; 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=jN6N73YrgouZJ4pURwJWv8rEPfUq0aS1PZwlRmDA5eV5WgrzGTNOK9qKdIo4Zmkmi /DIfWWC8K9snJcMKCZTpFDI7b3l1wvG/WeaA+GjLC4aoRPT903TGiacn4VWodxR6U2 UAiXRBHj4Cz2wJFr/JjXDs8aBUO67Ikf/yXrRmn7hvjarkXGzuxYyDAr4FYnfmNl53 1httRxm470tuXZ5igu2qa/q4SuDRJhkwNiq5Lbg3l8xLXSsc/4PM4nqBJf08twiRGr aXs7UYpo0LCBvc3O5BSdYFAU1RmETbMpXRtvm6jJAK/eWgmB650eoGw1vtCCjZXAMi hHjKBrwRCjjlg== Received: from mail-pf1-x436.google.com (mail-pf1-x436.google.com [IPv6:2607:f8b0:4864:20::436]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 856AB627EE for ; Wed, 2 Aug 2023 08:55:01 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="YDS/NHiV"; dkim-atps=neutral Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-686ba29ccb1so410645b3a.1 for ; Tue, 01 Aug 2023 23:55:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1690959299; x=1691564099; 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=of3V24Swk733umJsa+sZgpEEgBy/JwWUEV6egzUd+Dc=; b=YDS/NHiVK0uVbk7YW+LnucSteEdZJa+RShDttUQT2dO9q/1D2ySumgKfuKSJN7gtAz E/zLlc3V3nZHGqCTCSCk9qD0c4lon0RX8e8vUIrWBAE19eX/R9v3yDMREoIYa51dtEYs bEThX44BKrNHrUxA9VkJXLbE6N4ezH74YIoZ8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690959299; x=1691564099; 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=of3V24Swk733umJsa+sZgpEEgBy/JwWUEV6egzUd+Dc=; b=PXUwPrwcb9EynKMtTRQZ3BqbgstkOytvPeZAeDoK4gu55QIT5Yawt+BqW/3tftZ/IN cT7RezMzyw978mKYzNBxLK/Ef24BEhhczSg6B86WGdBvy2H+d2yarogNkPQUpFtjj9De 78oRr+9sBZ3WSsVFkTWEsCCV7ujns4cerLH8GSEOxUDvWd8Tqx/+gpsJJG0+rtpr2x2T QQnd80t3/KoDYtZCyeLamI9gMIcvXhJVDvOdDHC9KL5HiMEtivlWgaG6movNTcX+dijC u5O7vhmT7BdWQPDA2fopYrdwCx0bumAJ22ElSRC72BRBlD6uz+fN0921/ZQv5EGXLxGt 2UFg== X-Gm-Message-State: ABy/qLZE4qcZb1HB7TQqp4JvNbjHBy110cu3DK1etpqiQ7t8eTLWA9pJ Qpglo9MarAdYIIN1p9DqoEW2HjRWjLQcJy4X72E= X-Google-Smtp-Source: APBJJlHDuoDsIoW7ncjvKQV4ldP9pVHlqq+5Yt6U8p5kIzXOc4rkQlro/cZVRCc7tjL4Eomv+0epLg== X-Received: by 2002:a05:6a00:2e8d:b0:667:d0ff:6a0f with SMTP id fd13-20020a056a002e8d00b00667d0ff6a0fmr18247228pfb.5.1690959299430; Tue, 01 Aug 2023 23:54:59 -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 d14-20020aa7868e000000b0065980654baasm9142154pfo.130.2023.08.01.23.54.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Aug 2023 23:54:59 -0700 (PDT) To: libcamera-devel@lists.libcamera.org Date: Wed, 2 Aug 2023 06:51:44 +0000 Message-ID: <20230802065449.2904457-3-chenghaoyang@chromium.org> X-Mailer: git-send-email 2.41.0.585.gd2178a4bd4-goog In-Reply-To: <20230802065449.2904457-1-chenghaoyang@chromium.org> References: <20230802065449.2904457-1-chenghaoyang@chromium.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v7 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: Cheng-Hao Yang Reply-To: Harvey Yang Cc: Harvey Yang Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" If DmaHeap is not valid, fall back to UdmaHeap to allocate buffers. Signed-off-by: Harvey Yang --- src/libcamera/heap_allocator.cpp | 105 +++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) diff --git a/src/libcamera/heap_allocator.cpp b/src/libcamera/heap_allocator.cpp index 2a825d93..1caa1469 100644 --- a/src/libcamera/heap_allocator.cpp +++ b/src/libcamera/heap_allocator.cpp @@ -11,10 +11,14 @@ #include #include #include +#include +#include +#include #include #include #include +#include #include @@ -84,6 +88,18 @@ public: UniqueFD alloc(const char *name, std::size_t size) override; }; +/** + * \class UdmaHeap + * \brief The derived class of Heap with Udmabuf implementation + */ +class UdmaHeap : public Heap +{ +public: + UdmaHeap(); + ~UdmaHeap(); + UniqueFD alloc(const char *name, std::size_t size) override; +}; + /** * \brief Construct a DmaHeap with a list of |dmaHeapNames| */ @@ -98,6 +114,7 @@ DmaHeap::DmaHeap() continue; } + LOG(HeapAllocator, Info) << "Using DmaHeap allocator"; handle_ = UniqueFD(ret); break; } @@ -138,6 +155,92 @@ UniqueFD DmaHeap::alloc(const char *name, std::size_t size) return allocFd; } +/** + * \brief Construct a UdmaHeap with `/udev/udmabuf`. + */ +UdmaHeap::UdmaHeap() +{ + int ret = ::open("/dev/udmabuf", O_RDWR); + if (ret < 0) { + ret = errno; + LOG(HeapAllocator, Error) + << "UdmaHeap failed to open allocator: " << strerror(ret); + return; + } + + LOG(HeapAllocator, Info) << "Using UdmaHeap allocator"; + handle_ = UniqueFD(ret); +} + +UdmaHeap::~UdmaHeap() = default; + +UniqueFD UdmaHeap::alloc(const char *name, std::size_t size) +{ + int 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 {}; + } + + if (create.size < size) { + LOG(HeapAllocator, Error) + << "UdmaHeap allocated " << create.size << " bytes instead of " + << size << " bytes"; + return {}; + } + + if (create.size != size) + LOG(HeapAllocator, Warning) + << "UdmaHeap allocated " << create.size << " bytes, " + << "which is greater than requested : " << size << " bytes"; + + /* Fail if not suitable, the allocation will be free'd by UniqueFD */ + LOG(HeapAllocator, Debug) << "UdmaHeap allocated " << create.size << " bytes"; + + /* The underlying memfd is kept as as a reference in the kernel */ + UniqueFD uDma(ret); + + return uDma; +} + /** * \class HeapAllocator * \brief The allocator that allocates heap buffers @@ -160,6 +263,8 @@ UniqueFD DmaHeap::alloc(const char *name, std::size_t size) HeapAllocator::HeapAllocator() { heap_ = std::make_unique(); + if (!isValid()) + heap_ = std::make_unique(); } HeapAllocator::~HeapAllocator() = default; From patchwork Wed Aug 2 06:51:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cheng-Hao Yang X-Patchwork-Id: 18923 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 69EF7C32B0 for ; Wed, 2 Aug 2023 06:55:06 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id F0EE0627EC; Wed, 2 Aug 2023 08:55:05 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1690959306; bh=EUpSCqw8ZvFk90XGcTGbyKhD5FT1P8Mlb4eRm3H2xTk=; 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=dri8xwThQ5K0iEYrcGK2FC8ue2A/hZp6UvFjwBT7rZEd3RVOzXuLCqqwO3JIlI5t+ GwhOjY9w+krNgI7UygM6OqF8IYwvGx62FBaF9HgfsnD8TF1fLWt0TEV3n1ijHkMjcY 7/bdKuV/pK4p/NxmIU88u0UE4dP1Zmj1QRXTugQPGMt8SbCSUNuRc4XNa5mRh1HIkL 3ogwakj125NRS38vbjW0tCjQy3Y6J74+3vgvN1szhA3yDP5uDxujp1NSKwGIR+QX8m Kz+G5DwT/HaBRApC42Zs65JA8sM2xRYhBaqaAYcFw6MVpUPZZxkb2zzvvrxIFic7Na r+Mn0Maz/VR9Q== Received: from mail-pf1-x433.google.com (mail-pf1-x433.google.com [IPv6:2607:f8b0:4864:20::433]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id AB20F627E6 for ; Wed, 2 Aug 2023 08:55:02 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="HPoVEsPm"; dkim-atps=neutral Received: by mail-pf1-x433.google.com with SMTP id d2e1a72fcca58-686f0d66652so6121602b3a.2 for ; Tue, 01 Aug 2023 23:55:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1690959301; x=1691564101; 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=SR8gv5LwHmrnA+J0vzfvBFITtJUKF6RbopGQzNfWe00=; b=HPoVEsPm2QMjL2WA1bDtnATwF9JX5MvNS3MO1tRficDZRqVFiKtwg+nPbG9C6XcVje tv+0NqCvHHAb3SPU/fIGlX6lkfOmqhi5YOf7rg8rS+BIb+mZLQEKYt7SJRYmX0ZJIWbP mbhqfS8TzZeYLw6oStcZirwtuV80iAPg/VK6E= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690959301; x=1691564101; 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=SR8gv5LwHmrnA+J0vzfvBFITtJUKF6RbopGQzNfWe00=; b=dfDin6EqlcJISn0vmho+jzGPUeluLzregCSRh998kWgn+eg4RoJpKYc2deGG/8P1UP uvZfVDHsA1UATr7w+UtzMh9n0KatAnJSJ6rvih8hcqlLI5yZW1pr13HZ68j5Ve1Cymy9 NczNt8kk4WOROaIf/1AHds81DqCxYmRiqJRDzY2dshWm405w2sAkgrbtNvoNw1XyjOeC dlDWBHC4JuYaLP8IduKTvEMjyC1vE5q9vXSfptiAIV8JKgu/wfPSeqg/RNHm93jevI8X VmPptB9VRk+w4SOE93bB1kP2fYkNAy00zKfyxJCvP+ulphAYRvTKv/qKotoAlkrsUPtP HlTQ== X-Gm-Message-State: ABy/qLYyNMNmOmC5IhAq+HiWnSeKFXZLb5ppFT0pYiib3ZrJwAaQh24+ TlMdLyrYeRLHzaS8aqO+Hbtv+FH+/W8lfslKPMU= X-Google-Smtp-Source: APBJJlHBsefdblBYvxLdcESSOQ719nUlWQC9SiI7nSQIHHojOT2Sb8AuVMyUI2u/CeSK7d/Pn+X8ng== X-Received: by 2002:a05:6a21:6d95:b0:134:4f86:7966 with SMTP id wl21-20020a056a216d9500b001344f867966mr19325429pzb.9.1690959300640; Tue, 01 Aug 2023 23:55:00 -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 d14-20020aa7868e000000b0065980654baasm9142154pfo.130.2023.08.01.23.54.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Aug 2023 23:55:00 -0700 (PDT) To: libcamera-devel@lists.libcamera.org Date: Wed, 2 Aug 2023 06:51:45 +0000 Message-ID: <20230802065449.2904457-4-chenghaoyang@chromium.org> X-Mailer: git-send-email 2.41.0.585.gd2178a4bd4-goog In-Reply-To: <20230802065449.2904457-1-chenghaoyang@chromium.org> References: <20230802065449.2904457-1-chenghaoyang@chromium.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v7 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: Cheng-Hao Yang Reply-To: Harvey Yang Cc: Harvey Yang Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Add a helper function exportFrameBuffers in HeapAllocator to make it easier to use. Signed-off-by: Harvey Yang --- include/libcamera/internal/heap_allocator.h | 10 ++++ src/libcamera/heap_allocator.cpp | 60 ++++++++++++++++++++- 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/include/libcamera/internal/heap_allocator.h b/include/libcamera/internal/heap_allocator.h index 6620fa23..064696d5 100644 --- a/include/libcamera/internal/heap_allocator.h +++ b/include/libcamera/internal/heap_allocator.h @@ -9,12 +9,16 @@ #pragma once #include +#include #include namespace libcamera { +class Camera; +class FrameBuffer; class Heap; +struct StreamConfiguration; class HeapAllocator { @@ -25,7 +29,13 @@ public: bool isValid() const; 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); + std::unique_ptr heap_; }; diff --git a/src/libcamera/heap_allocator.cpp b/src/libcamera/heap_allocator.cpp index 1caa1469..a1dba155 100644 --- a/src/libcamera/heap_allocator.cpp +++ b/src/libcamera/heap_allocator.cpp @@ -22,6 +22,12 @@ #include +#include +#include +#include + +#include "libcamera/internal/formats.h" + /** * \file libcamera/internal/heap_allocator.h * \brief Internal heap buffer allocator support @@ -291,7 +297,59 @@ UniqueFD HeapAllocator::alloc(const char *name, std::size_t size) return {}; } - return heap_->alloc(name, size); + std::size_t pageSize = getpagesize(); + /* Allocation size needs to be a direct multiple of pageSize. */ + std::size_t numPage = (size + pageSize - 1) / pageSize; + + return heap_->alloc(name, numPage * pageSize); +} + +int HeapAllocator::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(HeapAllocator, Error) << "Unable to create buffer"; + + buffers->clear(); + return -EINVAL; + } + + buffers->push_back(std::move(buffer)); + } + + return count; +} + +std::unique_ptr HeapAllocator::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 */