From patchwork Mon May 22 08:35:06 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: 18643 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 DF96CC31E9 for ; Mon, 22 May 2023 08:35:56 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 71D3E62868; Mon, 22 May 2023 10:35:56 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1684744556; bh=zQQ5xbkhnKL+j8UHLvRpkKSft/ozGZNYw7hO55w+//E=; 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=gj/u5WUhyH9L4lHJvU23xNQzGZlqpC9U3sz15XqmzV25NiV0ViaXunFU/MJ0W75Wt mLDb/jsqz/nOmNRwVfF1ZkX3gUj3KOHuAKiTxVqqFy+bSQEkDKtwT8tNmF3ANUAOWp nYYb0lCObbi1Bjx1P7DJ5XDcvxoyGH1XDW7DsgyTphDZQzpN0grsFzdNyrwA+20+Ii HyWuGeXugzO+HRgZ3D5C2lbJDyZPQEn5dLzTgNmKJEm6BwRwi8JjigqAzYTwza991j 8YpqJyN+nJqhuFqahsqosZi9Vvr60g0471X10iOMEaKsMBXLRsDV5GMTLIh1EGgtBX 9Bq42tjt2QEYw== Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 4EEE560536 for ; Mon, 22 May 2023 10:35:54 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="he1Wu9z2"; dkim-atps=neutral Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-1ae6b4c5a53so28568525ad.2 for ; Mon, 22 May 2023 01:35:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1684744552; x=1687336552; 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=ccmT6A0WdJ2K9iDEGYORVcW+9LO7sGp3F8OdVHXV5jQ=; b=he1Wu9z2bLbbp3FWq7CTq+B6TFJuKqveJf79bbmS4UKQfRy/35zQuIHhp/wJfAzEC8 TVRvUGY48RdZgpc7dLQYLIA8ALi38GNx/sAJjWR5daa+V03mVFuNdadw+wgHzXQJhxAU EXSnWFBWo//wRz0tHPyxGOVSMGG39TRwe5Lsw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684744552; x=1687336552; 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=ccmT6A0WdJ2K9iDEGYORVcW+9LO7sGp3F8OdVHXV5jQ=; b=IXr4SY0V1MVHVn+wy3y5nCokYhJ74cUJQzArdY5g94qnl5uwuW22DexmNo/CV4EXeg eerRvvLBzL8IuW5o5AYQW6jIoEWFFz0pSUruzEhszYfeGbc3kXTZ+Z8flups+r5zEypr SIyHqQgoeuj69e2acRcMXtO4Q8y2x24TaE5n4Tz/yMD0pEoBNyW+q+greFOh2mXlpTwp PPcSqzjuJ3KSN7yRK86PrGo1kWd9MqZ4gKQIzYRvmOs9Eu1dW01ZcHAZOjCEdLYlhgt9 uGLApSaa2nZHPnyT/V1AOhn7MZEwVe4YUm697ZXcxe5BPpQ1xHSTfGFPKwBZ+zzJ3n8w FYeA== X-Gm-Message-State: AC+VfDxc3asqnVmNodvFwqDyFuHbWtYQZY3JtkJNRmhCqI8ZJyT9RBUx B1VOEQHDjTEZad1n+UHYWXY8utpRxLVgYWd3BL0= X-Google-Smtp-Source: ACHHUZ7ur+/qNz8x13bqOeOvdmdMNTLcXBBExmHjEPVTTBiNfaN2kDMvRct75g2i9qqzPP+NlB1OoA== X-Received: by 2002:a17:902:ef82:b0:1ac:a887:d344 with SMTP id iz2-20020a170902ef8200b001aca887d344mr10641355plb.19.1684744552553; Mon, 22 May 2023 01:35:52 -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 h1-20020a170902f54100b001a980a23804sm4321253plf.4.2023.05.22.01.35.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 May 2023 01:35:52 -0700 (PDT) X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Date: Mon, 22 May 2023 08:35:06 +0000 Message-ID: <20230522083546.2465448-2-chenghaoyang@google.com> X-Mailer: git-send-email 2.40.1.698.g37aff9b760-goog In-Reply-To: <20230522083546.2465448-1-chenghaoyang@google.com> References: <20230522083546.2465448-1-chenghaoyang@google.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v6 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: 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 | 17 ++- include/libcamera/internal/meson.build | 1 + src/libcamera/heap_allocator.cpp | 130 ++++++++++++++++++ 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, 146 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..92d4488a 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,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..69f65062 --- /dev/null +++ b/src/libcamera/heap_allocator.cpp @@ -0,0 +1,130 @@ +/* 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 + +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 +{ +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/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 Mon May 22 08:35:07 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: 18644 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 726F4C31E9 for ; Mon, 22 May 2023 08:35:58 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 5447D627DC; Mon, 22 May 2023 10:35:57 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1684744557; bh=zUzbFMtsdXvvxDXM+ui/p1b/35MGxQs9X2/3noCu2wo=; 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=htbd1cybnM7nXEc6S4gg+3mP4G8v9wHyfHYecwPorr3fwACx71+TzQCDTLrudLj8G rhqQUe/2j7WpPrw8ub8aT5W0elOihB2rQEc13qIRklmlQkSjORmN22WlGjML5dw+Oy njX5aNEpriJmMOqGK2BuelCbU0j/gKa0043FGmDBTihrIQGyzGSMDmT6/DXz17IuR7 EfX4+cZ0gNJ0YXxgF546uvYI9LUrAfV2CEGxjeC2RwDNKztdnxDPqLHxG7Jtylq438 6AVMZhj/Ym5ycem/DTdBLiEropHrr2QaLVwn00RZRAzA8sqVCWVx5t17EXHZmvhlOO dSEzKdfvSDucA== Received: from mail-pl1-x630.google.com (mail-pl1-x630.google.com [IPv6:2607:f8b0:4864:20::630]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 49FF4627DC for ; Mon, 22 May 2023 10:35:55 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="OGa94fnp"; dkim-atps=neutral Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-1afbc02c602so2137355ad.1 for ; Mon, 22 May 2023 01:35:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1684744553; x=1687336553; 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=JouvgO2fyVzHl6E/gHYv6bTHugQHpSDy2GAoLY5wuro=; b=OGa94fnpm256BRVgFyBkWOWz1KziCko/FZNW5CCJ0XGTaWWTGAeDZuvHkhvH6SvlQQ LQjwor9P3/0Kyf1I7Esw8ym+S7o063GJFEc6ITxYxCEgY4O5JMxDXBY1njxl9PWAzPod LZHQ41E5b5UOskxG89MtoIn8M5Zs45F0oiIrU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684744553; x=1687336553; 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=JouvgO2fyVzHl6E/gHYv6bTHugQHpSDy2GAoLY5wuro=; b=QUrLiOk+7QFEAqPn471u12Dskvr9S/lqbw9giMLRerBgwwleaqA8OGSthBb4ZjJ266 6eG7+VH1tQcieQAIFzQ3H6rFKW6ZHWr4qNNTqDL7XJPp/jM+DJaS1e9vB4gSRgGoKwcp kDWvNs7pBQw4LdLCbpH6XIeWLW8NW+v+uQFOy/mmUfYH9+ir7IHa9CVQZPX/AGUZgcy5 qrx/VGdP8Id8i+Gv5sLI8Kw5HrJyJYKd1Kaqy6Zisc4s2PHs3QXaZbixj6BD99o3/lH4 X5ptRPCcpcUdLkNWRhyI8PNUjZaIoiC7ieaWxtEh6ZHehzfuI/aGTT38TQrzWKsCs44T vBNg== X-Gm-Message-State: AC+VfDxSYKCZuiXYLh8MA+5lCq4QUgLKOSe0yxO4Ltk/PFHJJ0zMe1Sy wRGvQdEKo/TMTMAqswNwV4+9n9Da1Y54rngYiPo= X-Google-Smtp-Source: ACHHUZ6d9Xl3FgOKz4KqrP+j0vdF3zSqKxQyoPasAB85dR9rzVxXCRGNKto9RCln80d9qwkuRykO9Q== X-Received: by 2002:a17:903:2343:b0:1ae:35b8:d5ae with SMTP id c3-20020a170903234300b001ae35b8d5aemr11504092plh.19.1684744553616; Mon, 22 May 2023 01:35:53 -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 h1-20020a170902f54100b001a980a23804sm4321253plf.4.2023.05.22.01.35.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 May 2023 01:35:53 -0700 (PDT) X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Date: Mon, 22 May 2023 08:35:07 +0000 Message-ID: <20230522083546.2465448-3-chenghaoyang@google.com> X-Mailer: git-send-email 2.40.1.698.g37aff9b760-goog In-Reply-To: <20230522083546.2465448-1-chenghaoyang@google.com> References: <20230522083546.2465448-1-chenghaoyang@google.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v6 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: 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 --- src/libcamera/heap_allocator.cpp | 100 +++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/src/libcamera/heap_allocator.cpp b/src/libcamera/heap_allocator.cpp index 69f65062..8f99f590 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 @@ -54,6 +58,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) { @@ -65,6 +77,7 @@ DmaHeap::DmaHeap() continue; } + LOG(HeapAllocator, Info) << "Using DmaHeap allocator"; handle_ = UniqueFD(ret); break; } @@ -105,9 +118,96 @@ 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); + } else { + LOG(HeapAllocator, Info) << "Using UdmaHeap allocator"; + 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 Mon May 22 08:35:08 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: 18645 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 1E4DDC31E9 for ; Mon, 22 May 2023 08:36:01 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id E0F8E62865; Mon, 22 May 2023 10:36:00 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1684744560; bh=0MRzzNK7YdDOow/alZSUpvn6S0XNOZGqFTYfaZmht7Q=; 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=ceph6V0j3ZB5Uj5NN/DWQkJdOKXSkoY8CrCz2GmycpOTT2J5IV9OgkSI+NBWh9AUH Jt0S7VqPWeYrcb3a0uNnlBzQULE82aoQWpHMc9IpeINfBcSE+8jNk4QNNMS+i/OjgU WCGhNaLHHTDiK1OuXZimEmsQCJ7HJrjO33XVzKX7SGO6yYMpIyUwWXW8lYQYUg53C4 uUAJNk5l6lEUyDZWXUM4O6Cmb/8KRFZpOKlV5aQAR7EZBRysrssdgJkHy47EJIW+g3 R1jCEXNkJQpQldn2sYJ/uBICPPlW5UIQNfc/H1+2h6gNrm8smOxIgUl2ekZoAvn/IY dRNnVWpeUWsHw== 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 55FDE627DC for ; Mon, 22 May 2023 10:35:56 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="Ujkpgv9T"; dkim-atps=neutral Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-1ae875bf125so18966145ad.1 for ; Mon, 22 May 2023 01:35:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1684744555; x=1687336555; 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=+HAMbjUgljEg+SmXq+kpb1omhD43ul+/Dp5+qYCcWhE=; b=Ujkpgv9TlHTrh0RLXWBZ/g+AFH5Cx3g9BE0qnPS743yMJLadkzW9ldq7HG6URAImqD 1Gn1cHfS8Tc7uEC5P8IxlcYYPE7DPXicSBON/aIFdgQG8tzzOYbNyxaJ85VS3RdGay6R HTm4tTG09D43Mjn+Y9Zvu8H0JS35GJbbZV5K8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684744555; x=1687336555; 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=+HAMbjUgljEg+SmXq+kpb1omhD43ul+/Dp5+qYCcWhE=; b=PbAfys0MhIot66qcjl4i9AAF7OwYN3T83biyIcy9zVOd5QDLvADSpJgcy9GwsI9v/k KrrMvwuKJNlM1R/11dAj7OtdlXhTXDMfuklIuCX0waXQ+jflrffDSyXWQMhvQH5FTYQp W3ONBKkNkdnPuHkcHRxU0Ka4RXDPne12cXlFRZROi2BuMqJ34tTmBFRusVJcN5PO2rff rZlokqDIM0eNdsTXeeV6vV7wsDIawGiRzgTg6DnCsMCaiTazbV8/yv/FsMctpeE3PhkD ODX0pveUn2mmq/Hay7OivGEl10yWoZtKkF3JPirMIwNvdAYolR/qmXMnX+GX+O5xsr64 hh3w== X-Gm-Message-State: AC+VfDwcKQ9h5oBMWRT5K1mZu581ZnlKy4+1jLTgJ5hp7siN3+lKuxvc BCF5YEVJfJv63QBvmEXjArtAXeFXyhqhKsTPNCg= X-Google-Smtp-Source: ACHHUZ7aNPY/xYWoAJ2jN6H6QWE0J1AAZS8YAu4J7MeSaGOoNpQE37e1JqaKWYH7bjUiOeGR6PBO4Q== X-Received: by 2002:a17:902:e889:b0:1ac:753a:7933 with SMTP id w9-20020a170902e88900b001ac753a7933mr11982573plg.39.1684744554666; Mon, 22 May 2023 01:35:54 -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 h1-20020a170902f54100b001a980a23804sm4321253plf.4.2023.05.22.01.35.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 May 2023 01:35:54 -0700 (PDT) X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Date: Mon, 22 May 2023 08:35:08 +0000 Message-ID: <20230522083546.2465448-4-chenghaoyang@google.com> X-Mailer: git-send-email 2.40.1.698.g37aff9b760-goog In-Reply-To: <20230522083546.2465448-1-chenghaoyang@google.com> References: <20230522083546.2465448-1-chenghaoyang@google.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v6 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: 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 --- include/libcamera/internal/heap_allocator.h | 9 +++ src/libcamera/heap_allocator.cpp | 62 +++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/include/libcamera/internal/heap_allocator.h b/include/libcamera/internal/heap_allocator.h index 92d4488a..92beaaa4 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; +class Stream; class HeapAllocator { @@ -24,7 +28,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 8f99f590..e99cdbe5 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" + namespace libcamera { /* @@ -227,4 +233,60 @@ 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; + + /** \todo We don't support allocating buffers with a dedicated fd per plane */ + 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 */