From patchwork Wed Feb 8 09:59:20 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: 18268 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 924D8C324D for ; Wed, 8 Feb 2023 09:59:34 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 2FEE2625DD; Wed, 8 Feb 2023 10:59:33 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1675850373; bh=hIw5+SRIuWyed/qnCewJkqS3kiJgum8sAja5LyLkdoM=; 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=oISVPCk+0wqZ5o9l0wmyukgD3RP8GXtb5BMX8o7qnlkG8OvKsRMLVLwCUzRoozz7T UBw5Q8/zRnIMIT+rIfpM6uPBp7F3MpY6LuvYiDWUYcQQ7oTEMteoUXr/e/uSg9b9I3 WugLTD9Dym8jK3bwJfFaw9qFg+c4xBbqy74z4xmFJHqMgPMI9ppgobGliEqA403PZl LF27tjRJ22zCS9Ft1ZyiEySsdGGzd9lLN67xgR1KOyNfnZ6TR36WNC7AoZOy8RGaQn Mpz6pQkrlO8rp/hd6hWK3ex8v//3RfWJkafdkgzWLVObF479vhrFKR6Cd1eGQYRoIE iPMa1/mtWIX8A== Received: from mail-pl1-x635.google.com (mail-pl1-x635.google.com [IPv6:2607:f8b0:4864:20::635]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 6FD9F625DD for ; Wed, 8 Feb 2023 10:59:31 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="T/z48UjF"; dkim-atps=neutral Received: by mail-pl1-x635.google.com with SMTP id m2so18698975plg.4 for ; Wed, 08 Feb 2023 01:59:31 -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=T9GJDs4TKummjr4e2ozrtlVsxQUqmk/B31rc2tukESk=; b=T/z48UjF+TRR9iBsdxNYTlfeBmkjl0Zmb5imuwqfRkhM1UuT4lNdsDr/O9ISHGBkPL 5Qe55iF55qZI0GqFfItZAbuotIOLXEYVvRZbnNNWDtwXe6M2YTuoaC6eoPRS0GPoqo7z sH6iJILBhgtb4sQ2pmf40b0VczYVn4lQhMZpo= 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=T9GJDs4TKummjr4e2ozrtlVsxQUqmk/B31rc2tukESk=; b=kn1MFYEQabgz3EtzGwxBJN/joe+80znoCu7RommibC1qI3faKszk3NbSOzXW8HfJAV FeCZp7d5uC9T/cyTtkLFtNElWrnOKKEM5Gpvrvo/Cf2wP7zS/LWoUpwPzI3pwNqB498Z AA9ulYooNeozN8C0uGlRrCjTh9Yy7+88jRizcfSMlE0BlSS1Jzyo/zAjpWnQkR9A6bFb xnBdYVqnDqVQ8d4oIGGECtPfoQXn3rLc4zfgRhaDq5AFJt97esWwSav48Qp6qFN2gfDF +cWfyvQ/gkErrtLmvqZ8uTuQxvjbLz41xKxEShZRKFJAUfBiyDitWwF+Uo/wxow9twVr ZgsQ== X-Gm-Message-State: AO0yUKWS+AHDMxAr3RuHXvH1c1kemYysxMeavghIiRBp+t73zYIrM5vH xR+rnTMGtu9R4/j/d3plR/5vr07D+vEoTVRD X-Google-Smtp-Source: AK7set8Kabb5SK4YdThfj8Hp5SKxQ0y93lxkr7CpzbQQrBf4WUKrELo9nwYsTYU6HCwikrjfVRzWvg== X-Received: by 2002:a05:6a21:6d82:b0:bc:8bd6:d460 with SMTP id wl2-20020a056a216d8200b000bc8bd6d460mr8981655pzb.3.1675850369606; Wed, 08 Feb 2023 01:59:29 -0800 (PST) Received: from chenghaoyang-low.c.googlers.com.com (46.165.189.35.bc.googleusercontent.com. [35.189.165.46]) by smtp.gmail.com with ESMTPSA id m2-20020a17090a4d8200b00230b8402760sm1077773pjh.38.2023.02.08.01.59.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Feb 2023 01:59:29 -0800 (PST) X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Date: Wed, 8 Feb 2023 09:59:20 +0000 Message-Id: <20230208095922.1471175-2-chenghaoyang@google.com> X-Mailer: git-send-email 2.39.1.519.gcb327c4b5f-goog In-Reply-To: <20230208095922.1471175-1-chenghaoyang@google.com> References: <20230208095922.1471175-1-chenghaoyang@google.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v1 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} | 54 ++++++++++++------- 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, 164 insertions(+), 58 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} (56%) 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..96e311d3 --- /dev/null +++ b/include/libcamera/dma_heap.h @@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2023, Google Inc. + * + * 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 56% rename from src/libcamera/pipeline/raspberrypi/dma_heaps.cpp rename to src/libcamera/dma_heap.cpp index 6b644406..8b67e9d0 100644 --- a/src/libcamera/pipeline/raspberrypi/dma_heaps.cpp +++ b/src/libcamera/dma_heap.cpp @@ -1,19 +1,20 @@ /* 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. + * 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,34 @@ 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"; + + int ret = ::open("/dev/udmabuf", O_RDWR, 0); + if (ret < 0) { + ret = errno; + LOG(DmaHeap, Error) + << "Failed to open allocator: " << strerror(ret); + + if (ret == EACCES) { + LOG(DmaHeap, Info) + << "Consider making /dev/udmabuf accessible by the video group"; + LOG(DmaHeap, Info) + << "Alternatively, add your user to the kvm group."; + } + + } else { + handle_ = UniqueFD(ret); + } } DmaHeap::~DmaHeap() = default; @@ -67,24 +83,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 6064a3f0..42b96286 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 8569df17..18325faa 100644 --- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp +++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -41,7 +42,6 @@ #include "libcamera/internal/v4l2_videodevice.h" #include "delayed_controls.h" -#include "dma_heaps.h" #include "rpi_stream.h" using namespace std::chrono_literals; @@ -240,7 +240,7 @@ public: std::vector, MediaLink *>> bridgeDevices_; /* DMAHEAP allocation helper. */ - RPi::DmaHeap dmaHeap_; + HeapAllocator heapAllocator_; SharedFD lsTable_; std::unique_ptr delayedCtrls_; @@ -1206,7 +1206,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"); @@ -1595,9 +1595,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 Wed Feb 8 09:59:21 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: 18269 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 C532ABEFBE for ; Wed, 8 Feb 2023 09:59:35 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id BC199625FA; Wed, 8 Feb 2023 10:59:34 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1675850374; bh=iYUuK3LUwOTGkA1dDlInbKgFb+fMDZte7QHupxP6RvY=; 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=Q6Ad9CaVQROwGSeUfgN9vWVispwLVxdRYsDsrOgIE4p9HxJzjmhs2ijcNjDMtG7hk B7yfUi5+euRrO+tqeUm8VK9Jp9Pu1pOKuH2KRK5MfVER5fcW2ho67jZ7TVuk/RgHzD AGUUcxtEii3xK9u/ho7vAEVmwxmS0A5IlERFAiBX3uPfL4r7dcPTNAqAEmwT6sENVm tB3etjR9zo78xnvJkHW+w+7szkjJV/T/gBTVM3F21pZr8AZIG8LCdEfMYFrusBCtrw LrMyKoeKwpl9rU/0DmW//+f9RX09b5PDapF4j9FMoBV55jOD0r9OPmkQwpXi6/xHWG tda0aR6e/3Bjg== Received: from mail-pl1-x634.google.com (mail-pl1-x634.google.com [IPv6:2607:f8b0:4864:20::634]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 624A8625DD for ; Wed, 8 Feb 2023 10:59:32 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="l/cmdkjN"; dkim-atps=neutral Received: by mail-pl1-x634.google.com with SMTP id k13so18723496plg.0 for ; Wed, 08 Feb 2023 01:59:32 -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=uA1tNYpJ7gWm0FBElHLLWisTjWPNyd1HNlzREIPjWJE=; b=l/cmdkjN2iFzhfkMW4+QsX+u66+F/i/BkExR2HTavGivZUIC5lKH1NaU1FJqocSEVp aie4TzL4liQthi2q9xPIBYomA8FcS2cMXCoup5FBcj4/P8zFiG1PwtOIDj5jHPyIDpsq MqzxOzEjJQgFwuA+Hi5WE013/a4Y0/xx4S2sE= 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=uA1tNYpJ7gWm0FBElHLLWisTjWPNyd1HNlzREIPjWJE=; b=bVsmh8VVnGZQUfeLEJARQGR9PruyCQ/tq6fsynU1FPbN76+QqLtWQJ+RNB3be+2GG8 OHyAC2y4GsnRzlGcRshUOFpSahFj1BWCP2X+X5J0YT1xSctLzE6w6rI6WEbKJibJjtYH kJMLGIque2U2KiehWwairefV4b7FEJz3qHI7AD1sqc2ppAXW8oo4Qb0D7ZXvtzYVYpHY RJvY5BnvjygfSzwej5587PyCPFC0mzI3wRXY7czlqFEivlK97fOm3h3sUh8vGq2N35Ne pyqY9jvdW93MVHAWj8cIplEf8D5NIwxycLjkHGj8lnfdnM/DvwddwtTz80r2+EmqIGun 3zDw== X-Gm-Message-State: AO0yUKUOM0u7FKqvfN9EKN3gcvgpZQ59b4HvogaRZwCsqSZba37RYvpQ fpV1vBXLQoOpxMO9Fhn3dBf7w/63D9soPW5y X-Google-Smtp-Source: AK7set83s9pcf5Ghtpl6MFXNYrZ6ACQ9c/yuN/Lr7TqigGtfQBvnUhkHvH8rg7jbAQwe+etbJa/b9w== X-Received: by 2002:a17:90b:1648:b0:230:d1dc:72d2 with SMTP id il8-20020a17090b164800b00230d1dc72d2mr8170552pjb.38.1675850370756; Wed, 08 Feb 2023 01:59:30 -0800 (PST) Received: from chenghaoyang-low.c.googlers.com.com (46.165.189.35.bc.googleusercontent.com. [35.189.165.46]) by smtp.gmail.com with ESMTPSA id m2-20020a17090a4d8200b00230b8402760sm1077773pjh.38.2023.02.08.01.59.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Feb 2023 01:59:30 -0800 (PST) X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Date: Wed, 8 Feb 2023 09:59:21 +0000 Message-Id: <20230208095922.1471175-3-chenghaoyang@google.com> X-Mailer: git-send-email 2.39.1.519.gcb327c4b5f-goog In-Reply-To: <20230208095922.1471175-1-chenghaoyang@google.com> References: <20230208095922.1471175-1-chenghaoyang@google.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v1 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..b5b86922 --- /dev/null +++ b/include/libcamera/udma_heap.h @@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2023, Google Inc. + * + * 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..2a470f6b --- /dev/null +++ b/src/libcamera/udma_heap.cpp @@ -0,0 +1,117 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2023, Google Inc. + * + * 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 Wed Feb 8 09:59:22 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: 18270 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 A7A69C324D for ; Wed, 8 Feb 2023 09:59:36 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 5B28D62601; Wed, 8 Feb 2023 10:59:36 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1675850376; bh=HbRSjQ39da2Lgx0fziaXvG6iGKLteMFYspzo4RV1DgM=; 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=MktP4IWHnzUJg2IFF1shtxWVE3R9r4FtNXcHTUMgsLVccUuHRACLdLJj7RSqiInbj L/D7uYXzclWSponHeLPCk76oON5hK+6S1WwaMl5gIFSFKLr85pf9vmdpU/uAYTDfV6 jGoNBDz+epaPITxszmJMd1s+9rK/kSRzUH4kaJvMMNBjp5hLpW52XDnzf+Qsw2hCFx HNztDMvWztvxhcj33n8iK1AR8Au4Y6T0MOKfBDe16C2wmRqPA0WftwyiTGaq33NpKD SlpDC1aYV0JdTG8AGhlbA+6sYYfzbfcOppJ66cyhvK9HIEysulUhD3RoWD3UGbwem4 CXGXuiCPa3Ecw== Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id CEE2D625F4 for ; Wed, 8 Feb 2023 10:59:33 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="R8nQ306n"; dkim-atps=neutral Received: by mail-pj1-x102a.google.com with SMTP id c10-20020a17090a1d0a00b0022e63a94799so1863003pjd.2 for ; Wed, 08 Feb 2023 01:59:33 -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=72Bs66NZMpVyTD6wUic29MKfkfKk43ErfuKBDYJ1n2I=; b=R8nQ306nRNqUxDooZMdPxTDhUiTWaCJqdCX93xiD+2GI793fB4kaJufPudkJ8e57n7 FYvBRD/Y54qw/c1xmNMIO4ChU6kt7swNGYtkM+N+OTwZJn2Gcvnmloq9HIY/4rSbSTgO x1q246nKpl5YwukkNJ+a8SQ3Ar+z/Lxr5Lthg= 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=72Bs66NZMpVyTD6wUic29MKfkfKk43ErfuKBDYJ1n2I=; b=ns9VKc+IWdqjgVY7qsa+uYXsWTUOiq7xQvhvaBBkQEUnBrGaLMsr0fN6hKIZEcL728 qm/3q3wOGJ1YeQzL8FA3OKFXvbFwwYKZie26kVclJMeO2wLMHktXAcddxt95uKosrSFK NwxddW+rlqfX6JScaOc04s06UlialHVR4cWv9Z+j/i5+EPzeKXv2kHtAc66r3oJyLsrJ h2Hbd2YRZ5jLIbbEN12ezk+i1WD8GXSBDiAp/EhgnCQNn9NdHeg8+QhvpMyVSJD8PQp+ lr4PoU3yKQ7MvuVFT/f6DmaTDuDmtalLHcg1x067I19J89JH5OszLN/yka2d3T5e/oxk laBg== X-Gm-Message-State: AO0yUKU5h9oirSSzKQF/bGp7StIntXl7s0aypp74hhpJJ+hEBPwpKpfn G+cEgA6jYhP65q6zGvp9QDHiaBKycY6HLitH X-Google-Smtp-Source: AK7set+ChTKXoN1BUUyo/IF1H536ZKJXpWIlTVmuYZhROSazcGWk6y09jz44wBnJ89f6SdAxRdVoJw== X-Received: by 2002:a17:90b:3508:b0:230:ac45:b4a with SMTP id ls8-20020a17090b350800b00230ac450b4amr7860986pjb.21.1675850371909; Wed, 08 Feb 2023 01:59:31 -0800 (PST) Received: from chenghaoyang-low.c.googlers.com.com (46.165.189.35.bc.googleusercontent.com. [35.189.165.46]) by smtp.gmail.com with ESMTPSA id m2-20020a17090a4d8200b00230b8402760sm1077773pjh.38.2023.02.08.01.59.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Feb 2023 01:59:31 -0800 (PST) X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Date: Wed, 8 Feb 2023 09:59:22 +0000 Message-Id: <20230208095922.1471175-4-chenghaoyang@google.com> X-Mailer: git-send-email 2.39.1.519.gcb327c4b5f-goog In-Reply-To: <20230208095922.1471175-1-chenghaoyang@google.com> References: <20230208095922.1471175-1-chenghaoyang@google.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v1 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 */