From patchwork Thu Mar 2 08:13:47 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: 18324 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 48C74BF415 for ; Thu, 2 Mar 2023 08:14:00 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 0AA99626C5; Thu, 2 Mar 2023 09:14:00 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1677744840; bh=f04+rWYpv5HytfIpWU+68S6T+DXQjLFZpICw3vf0OSs=; 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=MVuihu4JNfZzlMs/3k8TTmW3U3bNzZlohFrAKljz88JHu0liiieXZ3zCMBlvpefAn 0ABYg81w3L359un+IGLnvLQVII0J/01ZzTsRVF/UmUr6wtaFfLxIE2YlMTPxUEwxLg UCNR7kr2N3ZtpExHT0Z1tf95JamTsoW+gFnzt3y6MCDJ0/ED1MgUM5s9fRx/SesZ4g zz4YpZKwiJfApsFHcjUavESXTOqHo6C0KNuZGdBvVqA9Tbz4XFso/5yGKxpmgrTSy0 7DnTSG1EL4ktUCAH6C78yGczlgPl2y9TnVUF9vmtI7rcaw1aMuabM/KFHta93D9FJK GB8VGfBJB04kA== Received: from mail-pf1-x42a.google.com (mail-pf1-x42a.google.com [IPv6:2607:f8b0:4864:20::42a]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 063C7626C0 for ; Thu, 2 Mar 2023 09:13:58 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="VojQnyPF"; dkim-atps=neutral Received: by mail-pf1-x42a.google.com with SMTP id y10so9565974pfi.8 for ; Thu, 02 Mar 2023 00:13:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; 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=Z3/IRFilRfX4RdZtfng9VscS05Uz9TtsWjvv1NQFHNY=; b=VojQnyPFDPgo5Y/u6zpkRY7pR1wLOYclNY5AVJX8pjRqPYce7JnMGy/Lbvzo2MU2Wi yE50BGjlNk9BDDrJxaLftFM/y87bX5Og4uWVViHAOE0DF5Mx2eUqi3QL7sJgSUXwd+Wt v+mvrSs6a+CoWNqVQyRiMnzPKG2kZuz/T7oA0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=Z3/IRFilRfX4RdZtfng9VscS05Uz9TtsWjvv1NQFHNY=; b=s9bQllHlNnFAygC8hBlHTsd7htr/HFcjape2BwhGBx0hTIJUQ8sTSuE/G60AFUkUAK an6KAneTMIStPG1yp14UrhfwLdaZMXcsugaiIbA0Pcxn0EMsENOVHM98C19nzMke9ipT wUNNCWbyDQTLD1j0kL/BCedVSrDGYrGzG6xUOXJR/nmXLU0MC4RKIsy7XQrH9JITOn4A 8nBHxC+aohC3txwlIzMHnXBJfegLJC82LzCRh6/wbiYkUv60arau7kAmhXYnh7nwIOts fz5fS33775qMnpa9cTM0GWWYz28YEl2MjdDFj6iygMFv9uPg4we3cyyRHQo3fbzgUR2K 4ypg== X-Gm-Message-State: AO0yUKV9i4qhl2upq3iVWcrJSOobAc7TrYo10lc31eOVFKpKVQv8u3f9 Uh1QgtNpOr4/BMGuInJUluhJFPWNLIe1a+AcCL4= X-Google-Smtp-Source: AK7set9oGWQKG4igyhMhUxxh63QVvqSSFNO3KF8Hov170LcL2qu53xn6bsNnjjGX0SxUHoyQ/dimpw== X-Received: by 2002:a62:4ed0:0:b0:594:26a7:cbd2 with SMTP id c199-20020a624ed0000000b0059426a7cbd2mr1765582pfb.8.1677744836250; Thu, 02 Mar 2023 00:13:56 -0800 (PST) Received: from chenghaoyang-low.c.googlers.com.com (112.157.221.35.bc.googleusercontent.com. [35.221.157.112]) by smtp.gmail.com with ESMTPSA id p9-20020aa78609000000b005a8bdc18453sm9179755pfn.35.2023.03.02.00.13.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Mar 2023 00:13:55 -0800 (PST) X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Date: Thu, 2 Mar 2023 08:13:47 +0000 Message-Id: <20230302081349.3334290-2-chenghaoyang@google.com> X-Mailer: git-send-email 2.39.2.722.g9855ee24e9-goog In-Reply-To: <20230302081349.3334290-1-chenghaoyang@google.com> References: <20230302081349.3334290-1-chenghaoyang@google.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 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 , 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 --- include/libcamera/dma_heap.h | 20 +++++++++ include/libcamera/heap.h | 27 ++++++++++++ include/libcamera/heap_allocator.h | 30 +++++++++++++ include/libcamera/meson.build | 3 ++ .../dma_heaps.cpp => dma_heap.cpp} | 35 +++++++-------- src/libcamera/heap_allocator.cpp | 43 +++++++++++++++++++ src/libcamera/meson.build | 2 + .../pipeline/raspberrypi/dma_heaps.h | 32 -------------- .../pipeline/raspberrypi/meson.build | 1 - .../pipeline/raspberrypi/raspberrypi.cpp | 10 ++--- 10 files changed, 146 insertions(+), 57 deletions(-) create mode 100644 include/libcamera/dma_heap.h create mode 100644 include/libcamera/heap.h create mode 100644 include/libcamera/heap_allocator.h rename src/libcamera/{pipeline/raspberrypi/dma_heaps.cpp => dma_heap.cpp} (69%) create mode 100644 src/libcamera/heap_allocator.cpp delete mode 100644 src/libcamera/pipeline/raspberrypi/dma_heaps.h diff --git a/include/libcamera/dma_heap.h b/include/libcamera/dma_heap.h new file mode 100644 index 00000000..a663c317 --- /dev/null +++ b/include/libcamera/dma_heap.h @@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2020, Raspberry Pi Ltd + * + * dma_heap.h - Dma Heap implementation. + */ + +#include + +namespace libcamera { + +class DmaHeap : public Heap +{ +public: + DmaHeap(); + ~DmaHeap(); + UniqueFD alloc(const char *name, std::size_t size) override; +}; + +} /* namespace libcamera */ diff --git a/include/libcamera/heap.h b/include/libcamera/heap.h new file mode 100644 index 00000000..c49a2ac3 --- /dev/null +++ b/include/libcamera/heap.h @@ -0,0 +1,27 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2023, Google Inc. + * + * heap.h - Heap interface. + */ + +#pragma once + +#include + +#include + +namespace libcamera { + +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_; +}; + +} /* namespace libcamera */ diff --git a/include/libcamera/heap_allocator.h b/include/libcamera/heap_allocator.h new file mode 100644 index 00000000..cd7ed1a3 --- /dev/null +++ b/include/libcamera/heap_allocator.h @@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2023, Google Inc. + * + * heap_allocator.h - Helper class for heap buffer allocations. + */ + +#pragma once + +#include + +#include + +#include + +namespace libcamera { + +class HeapAllocator +{ +public: + HeapAllocator(); + ~HeapAllocator(); + bool isValid() const { return heap_->isValid(); } + UniqueFD alloc(const char *name, std::size_t size); + +private: + std::unique_ptr heap_; +}; + +} /* namespace libcamera */ diff --git a/include/libcamera/meson.build b/include/libcamera/meson.build index 408b7acf..f486630a 100644 --- a/include/libcamera/meson.build +++ b/include/libcamera/meson.build @@ -7,10 +7,13 @@ libcamera_public_headers = files([ 'camera_manager.h', 'color_space.h', 'controls.h', + 'dma_heap.h', 'fence.h', 'framebuffer.h', 'framebuffer_allocator.h', 'geometry.h', + 'heap.h', + 'heap_allocator.h', 'logging.h', 'pixel_format.h', 'request.h', diff --git a/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp b/src/libcamera/dma_heap.cpp similarity index 69% rename from src/libcamera/pipeline/raspberrypi/dma_heaps.cpp rename to src/libcamera/dma_heap.cpp index 6b644406..02415975 100644 --- a/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp +++ b/src/libcamera/dma_heap.cpp @@ -2,18 +2,19 @@ /* * Copyright (C) 2020, Raspberry Pi Ltd * - * dma_heaps.h - Helper class for dma-heap allocations. + * dma_heap.h - Dma Heap implementation. */ -#include "dma_heaps.h" +#include #include #include -#include -#include #include #include +#include +#include + #include /* @@ -30,9 +31,7 @@ static constexpr std::array heapNames = { namespace libcamera { -LOG_DECLARE_CATEGORY(RPI) - -namespace RPi { +LOG_DEFINE_CATEGORY(DmaHeap) DmaHeap::DmaHeap() { @@ -40,17 +39,17 @@ DmaHeap::DmaHeap() int ret = ::open(name, O_RDWR, 0); if (ret < 0) { ret = errno; - LOG(RPI, Debug) << "Failed to open " << name << ": " - << strerror(ret); + LOG(DmaHeap, Debug) << "Failed to open " << name << ": " + << strerror(ret); continue; } - dmaHeapHandle_ = UniqueFD(ret); + handle_ = UniqueFD(ret); break; } - if (!dmaHeapHandle_.isValid()) - LOG(RPI, Error) << "Could not open any dmaHeap device"; + if (!handle_.isValid()) + LOG(DmaHeap, Error) << "Could not open any dmaHeap device"; } DmaHeap::~DmaHeap() = default; @@ -67,24 +66,22 @@ UniqueFD DmaHeap::alloc(const char *name, std::size_t size) alloc.len = size; alloc.fd_flags = O_CLOEXEC | O_RDWR; - ret = ::ioctl(dmaHeapHandle_.get(), DMA_HEAP_IOCTL_ALLOC, &alloc); + ret = ::ioctl(handle_.get(), DMA_HEAP_IOCTL_ALLOC, &alloc); if (ret < 0) { - LOG(RPI, Error) << "dmaHeap allocation failure for " - << name; + LOG(DmaHeap, 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; + LOG(DmaHeap, Error) << "dmaHeap naming failure for " + << name; return {}; } return allocFd; } -} /* namespace RPi */ - } /* namespace libcamera */ diff --git a/src/libcamera/heap_allocator.cpp b/src/libcamera/heap_allocator.cpp new file mode 100644 index 00000000..594b1d6a --- /dev/null +++ b/src/libcamera/heap_allocator.cpp @@ -0,0 +1,43 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2023, Google Inc. + * + * heap_allocator.cpp - Helper class for heap buffer allocations. + */ + +#include + +#include +#include +#include +#include + +#include +#include + +#include + +#include + +namespace libcamera { + +LOG_DEFINE_CATEGORY(HeapAllocator) + +HeapAllocator::HeapAllocator() +{ + heap_ = std::make_unique(); +} + +HeapAllocator::~HeapAllocator() = default; + +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 9869bfe7..ee586c0d 100644 --- a/src/libcamera/meson.build +++ b/src/libcamera/meson.build @@ -17,11 +17,13 @@ libcamera_sources = files([ 'delayed_controls.cpp', 'device_enumerator.cpp', 'device_enumerator_sysfs.cpp', + 'dma_heap.cpp', 'fence.cpp', 'formats.cpp', '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/raspberrypi/dma_heaps.h b/src/libcamera/pipeline/raspberrypi/dma_heaps.h deleted file mode 100644 index 0a4a8d86..00000000 --- a/src/libcamera/pipeline/raspberrypi/dma_heaps.h +++ /dev/null @@ -1,32 +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. - */ - -#pragma once - -#include - -#include - -namespace libcamera { - -namespace RPi { - -class DmaHeap -{ -public: - DmaHeap(); - ~DmaHeap(); - bool isValid() const { return dmaHeapHandle_.isValid(); } - UniqueFD alloc(const char *name, std::size_t size); - -private: - UniqueFD dmaHeapHandle_; -}; - -} /* namespace RPi */ - -} /* namespace libcamera */ diff --git a/src/libcamera/pipeline/raspberrypi/meson.build b/src/libcamera/pipeline/raspberrypi/meson.build index 59e8fb18..7322f0e8 100644 --- a/src/libcamera/pipeline/raspberrypi/meson.build +++ b/src/libcamera/pipeline/raspberrypi/meson.build @@ -2,7 +2,6 @@ libcamera_sources += files([ 'delayed_controls.cpp', - 'dma_heaps.cpp', 'raspberrypi.cpp', 'rpi_stream.cpp', ]) diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp index 84120954..b7e0d031 100644 --- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp +++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -44,7 +45,6 @@ #include "libcamera/internal/yaml_parser.h" #include "delayed_controls.h" -#include "dma_heaps.h" #include "rpi_stream.h" using namespace std::chrono_literals; @@ -245,7 +245,7 @@ public: std::vector, MediaLink *>> bridgeDevices_; /* DMAHEAP allocation helper. */ - RPi::DmaHeap dmaHeap_; + HeapAllocator heapAllocator_; SharedFD lsTable_; std::unique_ptr delayedCtrls_; @@ -1293,7 +1293,7 @@ int PipelineHandlerRPi::registerCamera(MediaDevice *unicam, MediaDevice *isp, Me { std::unique_ptr data = std::make_unique(this); - if (!data->dmaHeap_.isValid()) + if (!data->heapAllocator_.isValid()) return -ENOMEM; MediaEntity *unicamImage = unicam->getEntityByName("unicam-image"); @@ -1680,9 +1680,9 @@ int RPiCameraData::configureIPA(const CameraConfiguration *config, ipa::RPi::IPA /* Always send the user transform to the IPA. */ ipaConfig.transform = static_cast(config->transform); - /* 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 Thu Mar 2 08:13:48 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: 18325 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 4A3C0C3261 for ; Thu, 2 Mar 2023 08:14:01 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 81E43626C6; Thu, 2 Mar 2023 09:14:00 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1677744840; bh=xXtbsxI0eWmqoGx3nXOrPPZbsjPCa3YF70/YiFpcAUs=; 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=k+z08ZZXsP6glH0YGKVGcgbm+35FVvrywgt0wRzNRXmJVPS6hf9ENqwgWVUoxJlrq mV6S91SXL/HRluwwh0BeOU3W3XJMHR37y4/cXy4M0s8EZV6ORZwnD2h1ArkhSGx7dW LZ7x1JZYMPvVtgPzh29zj5XF4P8LGFNYpV7D/4+Zvt+bhJ9vobZwC+m5rscqLDO/9C tMXb4U2guIaEdbp+CIkbHtwrZr7pv0sja9r7TXuEG5ljDemwJxzGy6hB6TgE0tNo5L E6x4AFeHEv/JcbkqxIbjmKvWClTJ4X6TGenEqVp4B3tCzpi6cKzEyREkfj3DNlfCLH 9B4K56QNxdJow== Received: from mail-pf1-x432.google.com (mail-pf1-x432.google.com [IPv6:2607:f8b0:4864:20::432]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 37BD9626C0 for ; Thu, 2 Mar 2023 09:13:59 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="FL2X6hYi"; dkim-atps=neutral Received: by mail-pf1-x432.google.com with SMTP id n5so9554252pfv.11 for ; Thu, 02 Mar 2023 00:13:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; 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=M2Xpln7iZYP5FYEPjKtHSVNkBNC2q8U/NR/LoMx0CF8=; b=FL2X6hYi6F0kIvbJcO29P1lxUOqiOYuLQ2ZvEuYsjdjAsz1H9aJwzZhNCDHAx8TFrE Ek1/9h9RwKA6hpNFUFEgXWJPUvxF/cEyrtV1JWHQLYEyxHgjY1PU2PWrwEyUksrRvdHp Iy5ng4hy+iq/K1Zwgfd/6cSEb61ZRuu1INRoU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=M2Xpln7iZYP5FYEPjKtHSVNkBNC2q8U/NR/LoMx0CF8=; b=PuhGeUHrLnroC3L+eypwvhibGC+HB+Da7bvlWYg8F6/JeFKQ0UueSCCTXrw8nokBbX 8FqEg2Vm32CyRJOGFGGX04AXmojm13YiJwFcb/I2B53ar9pgx5YUwQgq9mOTG2FUwZGg GkhPR5iQ9cXtM53mZMR7nKqilpwzrTWB8FK+yGDlOheVSLKm3aJ3IaejNJ1krUBfelO2 rjqois6t1DO5N+nrAyxHpyg6Il8vgJjiHj3SZI1IWqfs6vRyQmQ/fpYQWYfhlQzkPuaW 4DAne3iiUHvDXuxu9s1RsDrVUEH8OWErOCUtNdoYraxLVETnG25o9hDHql6rV9TEC6yz fJfQ== X-Gm-Message-State: AO0yUKWIzbZoDlgKiIewl4mdniq83VL5ZJRb+Z0VqNb+jq4Vrlp6ahtX U5Fcvr3TWjbHe+y4YfZJShmIXpe7iPw4gUJ6f64= X-Google-Smtp-Source: AK7set81y+z+JO7C1DSg4QtjkP9ALTSBlb885yFjo4rv3x/wMjOz0hopD1XSDkfqP+mzLtAc37E8DQ== X-Received: by 2002:aa7:9a4a:0:b0:5f1:f57a:b0c3 with SMTP id x10-20020aa79a4a000000b005f1f57ab0c3mr8885389pfj.5.1677744837601; Thu, 02 Mar 2023 00:13:57 -0800 (PST) Received: from chenghaoyang-low.c.googlers.com.com (112.157.221.35.bc.googleusercontent.com. [35.221.157.112]) by smtp.gmail.com with ESMTPSA id p9-20020aa78609000000b005a8bdc18453sm9179755pfn.35.2023.03.02.00.13.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Mar 2023 00:13:57 -0800 (PST) X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Date: Thu, 2 Mar 2023 08:13:48 +0000 Message-Id: <20230302081349.3334290-3-chenghaoyang@google.com> X-Mailer: git-send-email 2.39.2.722.g9855ee24e9-goog In-Reply-To: <20230302081349.3334290-1-chenghaoyang@google.com> References: <20230302081349.3334290-1-chenghaoyang@google.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 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 , 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 --- include/libcamera/meson.build | 1 + include/libcamera/udma_heap.h | 20 ++++++ src/libcamera/heap_allocator.cpp | 3 + src/libcamera/meson.build | 1 + src/libcamera/udma_heap.cpp | 117 +++++++++++++++++++++++++++++++ 5 files changed, 142 insertions(+) create mode 100644 include/libcamera/udma_heap.h create mode 100644 src/libcamera/udma_heap.cpp diff --git a/include/libcamera/meson.build b/include/libcamera/meson.build index f486630a..a1414a57 100644 --- a/include/libcamera/meson.build +++ b/include/libcamera/meson.build @@ -19,6 +19,7 @@ libcamera_public_headers = files([ 'request.h', 'stream.h', 'transform.h', + 'udma_heap.h', ]) subdir('base') diff --git a/include/libcamera/udma_heap.h b/include/libcamera/udma_heap.h new file mode 100644 index 00000000..3ae46dc7 --- /dev/null +++ b/include/libcamera/udma_heap.h @@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2023, Ideas on Board Oy + * + * udma_heap.h - Udma Heap implementation. + */ + +#include + +namespace libcamera { + +class UdmaHeap : public Heap +{ +public: + UdmaHeap(); + ~UdmaHeap(); + UniqueFD alloc(const char *name, std::size_t size) override; +}; + +} /* namespace libcamera */ diff --git a/src/libcamera/heap_allocator.cpp b/src/libcamera/heap_allocator.cpp index 594b1d6a..179c2c21 100644 --- a/src/libcamera/heap_allocator.cpp +++ b/src/libcamera/heap_allocator.cpp @@ -18,6 +18,7 @@ #include #include +#include namespace libcamera { @@ -26,6 +27,8 @@ LOG_DEFINE_CATEGORY(HeapAllocator) HeapAllocator::HeapAllocator() { heap_ = std::make_unique(); + if (!isValid()) + heap_ = std::make_unique(); } HeapAllocator::~HeapAllocator() = default; diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build index ee586c0d..bce26d4b 100644 --- a/src/libcamera/meson.build +++ b/src/libcamera/meson.build @@ -45,6 +45,7 @@ libcamera_sources = files([ 'stream.cpp', 'sysfs.cpp', 'transform.cpp', + 'udma_heap.cpp', 'v4l2_device.cpp', 'v4l2_pixelformat.cpp', 'v4l2_subdevice.cpp', diff --git a/src/libcamera/udma_heap.cpp b/src/libcamera/udma_heap.cpp new file mode 100644 index 00000000..050ab80f --- /dev/null +++ b/src/libcamera/udma_heap.cpp @@ -0,0 +1,117 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2023, Ideas on Board Oy + * + * dma_heap.h - Dma Heap implementation. + */ + +#include + +#include +#include +#include +#include +#include +#include + +#include + +#include + +namespace libcamera { + +LOG_DEFINE_CATEGORY(UdmaHeap) + +UdmaHeap::UdmaHeap() +{ + int ret = ::open("/dev/udmabuf", O_RDWR, 0); + if (ret < 0) { + ret = errno; + LOG(UdmaHeap, Error) + << "Failed to open allocator: " << strerror(ret); + + if (ret == EACCES) { + LOG(UdmaHeap, Info) + << "Consider making /dev/udmabuf accessible by the video group"; + LOG(UdmaHeap, Info) + << "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(UdmaHeap, Fatal) << "Allocation attempted without allocator" << name; + return {}; + } + + int ret; + + ret = memfd_create(name, MFD_ALLOW_SEALING); + if (ret < 0) { + ret = errno; + LOG(UdmaHeap, Error) + << "Failed to allocate memfd storage: " + << strerror(ret); + return {}; + } + + UniqueFD memfd(ret); + + ret = ftruncate(memfd.get(), size); + if (ret < 0) { + ret = errno; + LOG(UdmaHeap, Error) + << "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(UdmaHeap, Error) + << "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(UdmaHeap, Error) + << "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(UdmaHeap, Warning) + << "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(UdmaHeap, Debug) << "Allocated " << create.size << " bytes"; + + return uDma; +} + +} /* namespace libcamera */ From patchwork Thu Mar 2 08:13:49 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: 18326 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 5F009BF415 for ; Thu, 2 Mar 2023 08:14:03 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 0F3A2626C1; Thu, 2 Mar 2023 09:14:03 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1677744843; bh=jCT2KScZi8M2XeTWcjx/iXTqRwnAWGbb85nr7/PM9qQ=; 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=Sppshb7rv5PkdkNOVw2cfq7Qhea86owt3BxzWFmpRGq41B2R4mkkwjDlA6sQdauqc 4BZZFINV2e3y8yd9142TYJiT13ELI9et5lIuTVZNna+YfV3MMmxg+ZVEhskWGuOTT8 zrlyRyQUecqQ6cv6YVNRngSvMbgtlv0hCMdmQ4DIBdgbPubCBd3DZgm7JtUtq2CZDB Nn7iuzjXjiJdcsFO9rWY7z7TDPNNeMPwSyu7u3k0SI5EAjrBtNKfAnDgM93cZHopzP CfZrFP8vw95P0jgTW1LJZ7OHKc/mlDGFduHCUptQ7Q1OGrTHP0peCSXqX9COA9PPlI E96vTR1R6/w5Q== 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 98300626CB for ; Thu, 2 Mar 2023 09:14:00 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="X6tEmFvb"; dkim-atps=neutral Received: by mail-pl1-x62c.google.com with SMTP id a9so6610453plh.11 for ; Thu, 02 Mar 2023 00:14:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; 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=XXgeleZza/lqecvBHGW2tNMDFrYD9w7jlc3eZUNxb4M=; b=X6tEmFvb+RtuR9bEWdyV9wPffL6InSDJ637kr3lfgVLbVun5ASPopFVI+dtwTr9GwJ H2Zl6M6nsNmRe2sT/+4dLzUTSLH01/lRy5CbKD4KLxZyKJKlgJaIaJo2QKRQyiDYneUa QJY3TOzACzYWK2nnQbQRX0d58TCYaq5+qdnhg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=XXgeleZza/lqecvBHGW2tNMDFrYD9w7jlc3eZUNxb4M=; b=5qeJK8uXm2GIQ7JJhP/jC7F/+ITvBktYs17gYbAhBKnlxxebmevsFhMOGGKGMkb82P oSThVvC4nVFbDkygAre5QFYxWynEeOP2CozmAz15IYZsNoJybU3jEUlL0ZZ04diGOLA2 SdFhwXuWVT7H4marhcSe6kuxPVTNbXr5rjVHs4QmwbcHj5/9cZgG9U/GBFv3Tibs2zPa Xo7Ff78B7ebV6WlPMDHP+OKRnIVGw/ylEgsu5WCvh8In3UBp5zduRd2NVGeRRDyjDpVk YBxPzcuW5aD5v0rHobk7CQ+TNBn1aZmIulp3zWmgpHgebfdtva+Tgh7zC3w6T/gWxvU0 CTOg== X-Gm-Message-State: AO0yUKUyt95FfsuOcx1Eae9cKHCR+T6fskitdQWJZYOMDO5OuP7OzXLw Kd1KDTjkxNipz4WQT5ls6+gn+Dbn0tZ2jZuX3Ss= X-Google-Smtp-Source: AK7set8RpU+BIuurIpLtTKRwMZ++S7YmXwyJsyppXcdb6AHdQeVWorDRxkURVnvAlhhe0/JQVW88ZA== X-Received: by 2002:a05:6a20:549e:b0:bc:a257:5b2a with SMTP id i30-20020a056a20549e00b000bca2575b2amr10408486pzk.31.1677744838867; Thu, 02 Mar 2023 00:13:58 -0800 (PST) Received: from chenghaoyang-low.c.googlers.com.com (112.157.221.35.bc.googleusercontent.com. [35.221.157.112]) by smtp.gmail.com with ESMTPSA id p9-20020aa78609000000b005a8bdc18453sm9179755pfn.35.2023.03.02.00.13.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Mar 2023 00:13:58 -0800 (PST) X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Date: Thu, 2 Mar 2023 08:13:49 +0000 Message-Id: <20230302081349.3334290-4-chenghaoyang@google.com> X-Mailer: git-send-email 2.39.2.722.g9855ee24e9-goog In-Reply-To: <20230302081349.3334290-1-chenghaoyang@google.com> References: <20230302081349.3334290-1-chenghaoyang@google.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 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 , 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/heap_allocator.h | 10 +++++++ src/libcamera/heap_allocator.cpp | 44 ++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/include/libcamera/heap_allocator.h b/include/libcamera/heap_allocator.h index cd7ed1a3..138596de 100644 --- a/include/libcamera/heap_allocator.h +++ b/include/libcamera/heap_allocator.h @@ -8,6 +8,7 @@ #pragma once #include +#include #include @@ -15,6 +16,10 @@ namespace libcamera { +class Camera; +class Stream; +class FrameBuffer; + class HeapAllocator { public: @@ -23,7 +28,12 @@ public: bool isValid() const { return heap_->isValid(); } UniqueFD alloc(const char *name, std::size_t size); + int exportFrameBuffers(Camera *camera, Stream *stream, + std::vector> *buffers); + private: + std::unique_ptr createBuffer(std::size_t size); + std::unique_ptr heap_; }; diff --git a/src/libcamera/heap_allocator.cpp b/src/libcamera/heap_allocator.cpp index 179c2c21..1faa5585 100644 --- a/src/libcamera/heap_allocator.cpp +++ b/src/libcamera/heap_allocator.cpp @@ -17,7 +17,10 @@ #include +#include #include +#include +#include #include namespace libcamera { @@ -43,4 +46,45 @@ 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 */ + size_t size = stream->configuration().frameSize; + + for (unsigned i = 0; i < count; ++i) { + std::unique_ptr buffer = createBuffer(size); + 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(std::size_t size) +{ + std::vector planes; + + UniqueFD fd = alloc("Buffer", size); + if (!fd.isValid()) + return nullptr; + + FrameBuffer::Plane plane; + plane.fd = SharedFD(std::move(fd)); + plane.offset = 0; + plane.length = size; + + planes.push_back(std::move(plane)); + + return std::make_unique(planes); +} + } /* namespace libcamera */