From patchwork Tue Mar 28 09:55:32 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: 18486 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 C919DC326C for ; Tue, 28 Mar 2023 09:55:43 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 5295A62749; Tue, 28 Mar 2023 11:55:43 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1679997343; bh=VCDl4ZEryhgcpEmGNnEUQGklBXGY1bp3Bsn4GqvqK3Q=; 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=wZ48BG1Gt32L2kExw9wXk+TzFXQbwvyXFzG6WpPo2Pmtgo5B7utmaHs8wnuQCDulX fvnOhZAEsPseCMli5RBJqjdchnw2nKbHEm81RWzTE+Y8YIstwr1mUQeL4ZvfmK1kBT vXI8RuNnU8OrM0ZI5s17Q3QJmAI+69sHpV2W7qMvfbei8JpxSKcDYbPIT1EWheQ3eu Pqsaayy5ltJwBop+7UbTE/sZAh2q9lFaWqxusuG1H3KrTa2QVOIIAVqG00Plci5AeY YPcdwHW0plKczbvewE5qti/jPtKW8Z6UMOaBf3sknwWonefecKYdsk0Wdn4hRaFW9+ an1SIlz+xj/aQ== Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 003256273D for ; Tue, 28 Mar 2023 11:55:39 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="HsDxPs0Y"; dkim-atps=neutral Received: by mail-pj1-x1034.google.com with SMTP id gp15-20020a17090adf0f00b0023d1bbd9f9eso14609542pjb.0 for ; Tue, 28 Mar 2023 02:55:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1679997338; 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=mVbuPkhxZSj7ypJjZlYmo41mc2qUse3tKD8Pk9JRnps=; b=HsDxPs0YhWNJvXfD6J3zclFxowDyIKIP1UEjlwOOut0ZTWqwJbDlaZG/XjRAkilKhS acNqw7Y4f107pvchsD/sY0B99LZRCfdp+WJydl9CJqUTtnCZEs4p01Y0B6iuEtPqwdWv Nyi3p7gEYqwNqGKw58WVQY7semWkKJqK2AwSs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679997338; 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=mVbuPkhxZSj7ypJjZlYmo41mc2qUse3tKD8Pk9JRnps=; b=QVBPWty4PAYw5idra0tP2czn4oEt5BHeh151Igp0rEeYhSP4N2ofUEaH6MF22Nk0ae RvHud1rbby+I6tbhm5yVvaNWPkfX/g8np7EYe0DMNVq+8nuqcZ+ylKfOoC4BudUh4ka0 d23uxThYdKNxW1i0Aa03AomY/ZBwymPFMi2AbD7lNOg0GKORobiQkPy4hIDjurqCz7Wc 6ThpmiObYX17lnmZbUBGJ9kEABT5TfTlUsxoGMextEzAoC9Yeh/6wANQ8vAb/hrgAhzi k74eX817w93DXFR9+2djAY8ZnahSu0Lk8kxQuC3YJHgDdi9thOKLrnpqFrj0cCyrVlBR NNeg== X-Gm-Message-State: AO0yUKXAQ70p8MPkQcf8+B0gw25nWxfaSARKuDItN6yym+HhyGOMWGoN +6rSILhqPzDXFr1Gna3lfHxM+M97n3kCwUFVq7y1cg== X-Google-Smtp-Source: AK7set9ze93wSjFjxTOMqymXky2fVSu+fs6qFHrmDWm2qZilfJdGIKcVNleuUaCL4SBf1IdgvsM2DA== X-Received: by 2002:a05:6a20:4a13:b0:db:e054:826a with SMTP id fr19-20020a056a204a1300b000dbe054826amr13139968pzb.5.1679997338280; Tue, 28 Mar 2023 02:55:38 -0700 (PDT) Received: from harveyyang.localdomain (1-163-26-93.dynamic-ip.hinet.net. [1.163.26.93]) by smtp.gmail.com with ESMTPSA id v21-20020aa78515000000b006259beddb63sm20241488pfn.44.2023.03.28.02.55.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 02:55:38 -0700 (PDT) X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Date: Tue, 28 Mar 2023 17:55:32 +0800 Message-Id: <20230328095534.3584-2-harveyycyang@gmail.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230328095534.3584-1-harveyycyang@gmail.com> References: <20230328095534.3584-1-harveyycyang@gmail.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 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 --- 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 00600441..198dcc9d 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; @@ -246,7 +246,7 @@ public: std::vector, MediaLink *>> bridgeDevices_; /* DMAHEAP allocation helper. */ - RPi::DmaHeap dmaHeap_; + HeapAllocator heapAllocator_; SharedFD lsTable_; std::unique_ptr delayedCtrls_; @@ -1304,7 +1304,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"); @@ -1692,9 +1692,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 Tue Mar 28 09:55:33 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: 18487 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 1B54EC0F2A for ; Tue, 28 Mar 2023 09:55:45 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 13BDA62756; Tue, 28 Mar 2023 11:55:44 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1679997344; bh=Z1Mpl+qw1ze/oi6AZWCNx6OjkpDpzzECPjhC2lq7LPY=; 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=DygWzgTwo2/zbpOMHLOGADMBY363/3kf7gc+s6SRJxIOfoU4BiUVx3fdxhD48H70o c5WH5OWB99sVMQvWjImpec6vq+Qf48cJkzDrzY9KgK79M+DQWH3ZDU+H2Yd3cj1g48 3UdH5/7DJfeYV0GVV8lBU2omVISr54uP2MLbBlua7ORqMi+6rogx4xMpRFlIJXA5e4 nhgAUiYneU+/Gvqqd/vfsU6tYkTCyIZciArvac/sNjandGVlkjTunFSnTGdbiPVvoy f4eEWdxHZkbJbnTmIE5JdiKxUij9Qp1vnO2gZPCBMplayiDLOAGyd/JLQGwgtmzaeu UUJgQWvx81+gw== Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 275D262749 for ; Tue, 28 Mar 2023 11:55:41 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="m9VnCz3v"; dkim-atps=neutral Received: by mail-pl1-x632.google.com with SMTP id u10so11116751plz.7 for ; Tue, 28 Mar 2023 02:55:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1679997339; 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=4A9iq5ROqHE9gJpemOtkccgsirtSYRj6290oe2xGeM8=; b=m9VnCz3vVWZmfzTeyVBKKG/ah+mTY+KGPXP3uL4dKtLzveD+Cf3AQ84ay95+BZYP7B ed7LXdL5tLEhXYTUJaULcMjwbvhvpIVFCIt7U0RR4NFFU2ZYN1jDzYktoybjtZg0IM0N W6aqWRahZcAGh8DeZPOHN83JTaFGVdPcLsvq8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679997339; 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=4A9iq5ROqHE9gJpemOtkccgsirtSYRj6290oe2xGeM8=; b=IVmQ9KfKAxqfR9hxW1BM/zVZKkEXlHsnMygf359W1Gp8tYfQJDbs/eIUkbGMYngzIk Rnn0UBwFDz1EHuYaMuEYbeIrXrt1pXeJ3Od4w04irHFbfLK5jDkw0WDwFxNx1Z1L2nFl kN4wHyGLsJAN6MkxCNgrRkO6g0/7bhvOnLkhTxLgmfFvm8qTN3ReCwEa8QTvfFmmRXov Wl2tY24Xvx/u8v3LC9TSPHbLPdGCcsbb/p2ApLoXw1oNfVwvwJUR9nyfDqJCnH0CLe2W uWqNCWuRoBdAFxIX1iyF1FIkpjzCJMKC6wT67xGSn7KWBcwr2dzIHmzxqsJQ5Mn83zzF 1+aQ== X-Gm-Message-State: AO0yUKWvszMRKoNZdiip9xk0b66+Cnh8/7voBOM79UAfm9/tS7/wPfw3 C2C+O7I05LM5nRdCrLGR+g9yTlzNCLSdXHshbOECNA== X-Google-Smtp-Source: AK7set89nZKlp+qdTme1kOQqjTVUIGhSZx1yz7JTMiURACpW9EV19uLR4jkrO/9o4D45/ybFGyON3g== X-Received: by 2002:a05:6a20:1aa1:b0:d9:11d2:383f with SMTP id ci33-20020a056a201aa100b000d911d2383fmr11725740pzb.53.1679997339366; Tue, 28 Mar 2023 02:55:39 -0700 (PDT) Received: from harveyyang.localdomain (1-163-26-93.dynamic-ip.hinet.net. [1.163.26.93]) by smtp.gmail.com with ESMTPSA id v21-20020aa78515000000b006259beddb63sm20241488pfn.44.2023.03.28.02.55.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 02:55:39 -0700 (PDT) X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Date: Tue, 28 Mar 2023 17:55:33 +0800 Message-Id: <20230328095534.3584-3-harveyycyang@gmail.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230328095534.3584-1-harveyycyang@gmail.com> References: <20230328095534.3584-1-harveyycyang@gmail.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 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 --- 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 Tue Mar 28 09:55:34 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: 18488 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 D4733C329C for ; Tue, 28 Mar 2023 09:55:45 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 8EC766275E; Tue, 28 Mar 2023 11:55:44 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1679997344; bh=rKkP6APJoA9gPyVjCqIRpYkSaFXNsNkeFUkaXQOrw5A=; 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=hIaOq6Nzy8sBj8Jab8NxHU4b4rSy1sXjc8vD8x6A8vDz6GuQ7VD3CNzzLOrTWfG7a 8gt0OMdQgh1rn2p2Ft1gtXoLhW0vf6ClPVgwN8EYATVb3axauW9hjL8i+b60aiox3F 6rfnJ0E6dcK7H1T7mZEJ54rQINo/IKs1BDK4W6zzxADEvgpLVJXv7AhJ/g8p+GBHUW C5n1BkfXJVpMY/I7+KD9RMS6GzqfRANgQmZpcEmizZzKFWJ6iCLY2PjoMAS50hYYwF xN/tsipGI6moa+Fn1wqizgN0foyEwELKj/GmTu0QW+kmWP8YW0NeZxpAt71VkOcdyT LuD/u3GV//iRg== Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 351246273D for ; Tue, 28 Mar 2023 11:55:42 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="WuXRtnJd"; dkim-atps=neutral Received: by mail-pj1-x1031.google.com with SMTP id d13so10330041pjh.0 for ; Tue, 28 Mar 2023 02:55:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1679997340; 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=AnZvnxHR0i8wKScqjrDBwNbGDayWUHwb6jiPkaTvz0s=; b=WuXRtnJdkWQE388DhJlwPb2HMuu4Ojd0UtJO4Zi4sPHobgT8qekCJKxiwEPF8w3kmw h9AaLN+G+BvjqGfsA6Fx4u6IIbb18dYyorfurMPan47j9tx/wqX96mrd7DAqYvsqbF3r DlzRyqzLg4qOhwQordUEEy4Y1oBYBD0xE3vMQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679997340; 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=AnZvnxHR0i8wKScqjrDBwNbGDayWUHwb6jiPkaTvz0s=; b=4S7jxACPLJ5tKk+1pS8KpY35OolOxaIZQoUA6eSOQHr/L8znBHgf8aWh7kxNiKf4t0 g/1RLKTs2HxpgbsPVQuOG/jqG6LRx5Kkgi5SStYew8/uJLOd8lgnVAUUjBo2gdTQbmQn YaliYwwwbmqrps21ae4hVM+dhVZ9DOh09sWZZLrgDbEFj3blbnTFU72+5RhDjTc7FX4P wpaJDCfosv8P+7sRicvYV7m9DmhcqGlxZ/mtdn+WJw+5gi8mvd03wkaroywzMucg6tdF maD7xragTnc29ZnKqwa8W1J6vwqg1zd2F3xu1dbLj1UiHLAv5ZS2kcbmoAWroK/ZvJv/ +JUQ== X-Gm-Message-State: AO0yUKVw59+hwthOo1RQFQPQ7E/dVgM28ABCOF+kkGJjPripPhukWARl pJ0ZPIQNPGlyyNAJi2lrnIZCXuwnP/lsT61rB6DqNg== X-Google-Smtp-Source: AK7set9R3fnmT4Ti0KtDADvAI7yNjueZeEEZ2DHW/pw9FSTJpxs+pFOKay9p2EZ2m0jSevSPnkDbzA== X-Received: by 2002:a05:6a20:7187:b0:d9:f086:e756 with SMTP id s7-20020a056a20718700b000d9f086e756mr13977925pzb.39.1679997340440; Tue, 28 Mar 2023 02:55:40 -0700 (PDT) Received: from harveyyang.localdomain (1-163-26-93.dynamic-ip.hinet.net. [1.163.26.93]) by smtp.gmail.com with ESMTPSA id v21-20020aa78515000000b006259beddb63sm20241488pfn.44.2023.03.28.02.55.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 02:55:40 -0700 (PDT) X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Date: Tue, 28 Mar 2023 17:55:34 +0800 Message-Id: <20230328095534.3584-4-harveyycyang@gmail.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230328095534.3584-1-harveyycyang@gmail.com> References: <20230328095534.3584-1-harveyycyang@gmail.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 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/heap_allocator.h | 11 ++++++ src/libcamera/heap_allocator.cpp | 56 ++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/include/libcamera/heap_allocator.h b/include/libcamera/heap_allocator.h index cd7ed1a3..076f0951 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,11 @@ namespace libcamera { +class Camera; +class Stream; +class FrameBuffer; +class PixelFormatInfo; + class HeapAllocator { public: @@ -23,7 +29,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(Stream *stream); + std::unique_ptr heap_; }; diff --git a/src/libcamera/heap_allocator.cpp b/src/libcamera/heap_allocator.cpp index 179c2c21..33e9eaca 100644 --- a/src/libcamera/heap_allocator.cpp +++ b/src/libcamera/heap_allocator.cpp @@ -17,7 +17,11 @@ #include +#include #include +#include +#include +#include #include namespace libcamera { @@ -43,4 +47,56 @@ 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; + + int num_page = (stream->configuration().frameSize + getpagesize() - 1) / getpagesize(); + + UniqueFD fd = alloc("Buffer", num_page * getpagesize()); + if (!fd.isValid()) + return nullptr; + + auto info = PixelFormatInfo::info(stream->configuration().pixelFormat); + SharedFD shared_fd(std::move(fd)); + unsigned int offset = 0; + for (long unsigned int 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 */