From patchwork Tue Feb 14 09:37:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Yang X-Patchwork-Id: 18281 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 4C410C324E for ; Tue, 14 Feb 2023 09:37:46 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id F1F60625FA; Tue, 14 Feb 2023 10:37:45 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1676367466; bh=TQQuU+2lqJOFPv4XNWda6pdJVlZpFzQ381XZsJkbuX0=; 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=eBp/dNoltqHN2HaEma8zhV7nFK7nv2Q/vbalHSSpIwzamlkxCKBiYRAWE1XtsqHPf zzGSMCljHrpGv2w+C5WZ0nzrcE0db3VjzQVsoO6a67en9tswVseUqoGYAb/aGqurfe rmWnSu0P5+DZFOOhCyfXpJBGHBQ+mVRcwrjwM4AURXeuPhsF1B8R7QcKrc2vUxYLoL 1HTKB60/ilZLyVikRsOFQ89oZhxAEJ1JDGijQgINZ0vxy9iDjLQPsmpF+W9d7IuNrh x9uwVqrJQuAx/NzScs6KFBY8H33N979DGAeY9PKp7o9b4ubAaCnBbbT29EhsuV0KF4 QYSh2grpSgHMA== Received: from mail-pg1-x531.google.com (mail-pg1-x531.google.com [IPv6:2607:f8b0:4864:20::531]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 2A59A625E3 for ; Tue, 14 Feb 2023 10:37:44 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="JY4dXPvh"; dkim-atps=neutral Received: by mail-pg1-x531.google.com with SMTP id v3so9863067pgh.4 for ; Tue, 14 Feb 2023 01:37:44 -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=GluqLnJbDOw0maX5MfqSGdLEbUHCvjlqu7pxccUlRUE=; b=JY4dXPvhBOCw8j2trY5oiyDYMp6FHeQ8YBXk7waZEpSEFdBDig3dJHiXOUMJrpsoSZ i0vf8q2Ta/ha6rAba6XmdHgC2kciQ3M4pFLFDA2hdkESMCCtZrTsK0kRvyKNN5dKrPYa +frVpNgiUin1sVozcTHFvixKS1znmt6aDNEho= 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=GluqLnJbDOw0maX5MfqSGdLEbUHCvjlqu7pxccUlRUE=; b=JJJkjRa7dstx+zxBLRAZqqKphH/7bB0qyupBNCB/B3/UVZy9dtUgc2EiJmzhahHdjE /dDyyyPxwlY7GpmZDP4sYOS9WacbqdQP4xfzGW/NYS5J33m1lH7J+UNCp+ElVISiQwI0 07rJtm0GqUhpjo4PNZr3krmMzds0MijeMvHD41oYmfaEzcOj/WtP+M+/8AjafD8GYA1D gkwNicAyObA453Fq7MtsNWodZgf9gc+Wbn6fxxbRqpfdul2hsN++G6dZ3D+mrgTABnKk P/KT7vyz7iMMhLfXZaDWvtApsi2/fK6KTHIJQq9EiotEp9Zl7aISajd/wxMqO4Z+zGiG PrEQ== X-Gm-Message-State: AO0yUKU2J6EFRS0JDNG9hsgmAuChCna03H+0uEHz8MqfEFKli4fNQerN ujZxfZccTo70KXaA/HytJRRO8LTxISs512Q+ X-Google-Smtp-Source: AK7set/INTyeefi37EFADZvpfufrB43xjA7FUkR0hze6ljAiB37YDV0lnLR7kHMHxcRDMlHpvkdYFQ== X-Received: by 2002:aa7:98c2:0:b0:5a8:acd9:765b with SMTP id e2-20020aa798c2000000b005a8acd9765bmr1627920pfm.16.1676367462215; Tue, 14 Feb 2023 01:37:42 -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 z9-20020aa791c9000000b0059416691b64sm9696172pfa.19.2023.02.14.01.37.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Feb 2023 01:37:41 -0800 (PST) X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Date: Tue, 14 Feb 2023 09:37:34 +0000 Message-Id: <20230214093736.4067033-2-chenghaoyang@google.com> X-Mailer: git-send-email 2.39.1.581.gbfd45094c4-goog In-Reply-To: <20230214093736.4067033-1-chenghaoyang@google.com> References: <20230214093736.4067033-1-chenghaoyang@google.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 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: Harvey 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} | 52 ++++++++++++------- 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, 163 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} (58%) 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 58% rename from src/libcamera/pipeline/raspberrypi/dma_heaps.cpp rename to src/libcamera/dma_heap.cpp index 6b644406..38f603b0 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,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 Tue Feb 14 09:37:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Yang X-Patchwork-Id: 18282 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 C612CBDB1C for ; Tue, 14 Feb 2023 09:37:49 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 7E4EB625F1; Tue, 14 Feb 2023 10:37:49 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1676367469; bh=LyDwwOzJh3NNCme0YWpxnLmWxx2LE7Yt59dY93zJl9s=; 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=WA27QZVxhPhDuxkv4lBoHPwZVQFWtI60+F38CCDMEQHG28SDfTgZpAQCes5Zqm634 8B7503Qjl4E2HtOrkKRYB4LKLg1InMG43tcwQvLx2sTOE4iH7BLmfj30Bg9gizng2U SukHRtUF5uGZTyhyp5N2IoHGgjtLbnxnSlXBLIWEwX+QeBaZdK4xNEFzGgfL6Bc23A B4OAvjLoh0h09VRZNgZ5Z9T9AbKtZteX0Sn0C8UgP79vQaw6pCMqS0pOJTAyi43DjX oJSljEIwutQ1TD0PUCRSjsz9BBnAOuUxcXjip7EkRsCQ4NDOnaoutG07+xKOkeBD0G 9ghmh8o1EFLZg== Received: from mail-pj1-x1036.google.com (mail-pj1-x1036.google.com [IPv6:2607:f8b0:4864:20::1036]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 0E081625F8 for ; Tue, 14 Feb 2023 10:37:45 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="MEy2Ru6C"; dkim-atps=neutral Received: by mail-pj1-x1036.google.com with SMTP id bg2so5251136pjb.4 for ; Tue, 14 Feb 2023 01:37:44 -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=QpaG0t6PXjS7w3qMEAfXxXkv8IRrMJWTik1fs5WR5l8=; b=MEy2Ru6Ci/YG5rNw1dZcFRUbT4t313s+HL+XLrfNwIFVfU3UsUMJXkMuRB4QRswhxn 90Eg3V2tCyBo+X9Or42W5XSST5yb1d3RCRMur1oEqGA4MY2X9JMWlRbaP4s7x5I9Sv2X 85H8rwpCDSrkw/ChCPps/hxORK1mrlE4zrCpE= 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=QpaG0t6PXjS7w3qMEAfXxXkv8IRrMJWTik1fs5WR5l8=; b=NV7MveF0mYoTy8MWJ69w3191fl1/ztNkMZgXOSjmT/q11yPEuEpM2c9AsDhbMp3f8b d3YNUrtW0RCMj+x9ON7uN+6zS70AudvuJ+6gH5pJMN/bSR/erMkWB2LkgQLPlQQpN+UD GC2OGzqwzgTk7bA1GXkqrj/scDPhKGnXFSqHm8QzMSONIkWX9pCapOPtL6/CP84sJsFZ +umHOE+lAB4lVtDSGwQIgAerkTkJFdXn/u5DggiLQ3G0YMzoCyXzNMsau1qm3boOZ56C EVbojD6akqBkw9ne+WLQCGm2LdWVZeMaFz0tQfdapKGqKvVVvSf4K/xXVp150e9Wc5BI y4Og== X-Gm-Message-State: AO0yUKUo42nx+FcVdzjk6h62dcrzbBaUFVh/D4q0rQeqWw6DtbgLqDcG ugeQp9cIIttDmVhaUB756nrZB5VoN8FTxJxe X-Google-Smtp-Source: AK7set+zE1oZCSx/fHSsqM4ZfcLSZJbK896BMsr4dcrf3z1EU+pFRk09UZXeqV7p0DC36JDkVf6O0w== X-Received: by 2002:a05:6a20:691a:b0:be:e0c3:5003 with SMTP id q26-20020a056a20691a00b000bee0c35003mr1992302pzj.5.1676367463423; Tue, 14 Feb 2023 01:37:43 -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 z9-20020aa791c9000000b0059416691b64sm9696172pfa.19.2023.02.14.01.37.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Feb 2023 01:37:43 -0800 (PST) X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Date: Tue, 14 Feb 2023 09:37:35 +0000 Message-Id: <20230214093736.4067033-3-chenghaoyang@google.com> X-Mailer: git-send-email 2.39.1.581.gbfd45094c4-goog In-Reply-To: <20230214093736.4067033-1-chenghaoyang@google.com> References: <20230214093736.4067033-1-chenghaoyang@google.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 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: Harvey 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 Tue Feb 14 09:37:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harvey Yang X-Patchwork-Id: 18283 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 4D614C324E for ; Tue, 14 Feb 2023 09:37:50 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id EC854625E3; Tue, 14 Feb 2023 10:37:49 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1676367469; bh=crF47FA90nPFDW1PTQ1EIWr0ELfwIeXg28krgyX/O2k=; 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=akqZd28cUoGmz6AxSfh5LVZBVeuCoKgYWiCm3CqNwQHERRIrWd8wmuXczBodvA4Bd sdaXLjAysogi0/lViqKr+pauvaJ7j59VTQw2C/3J5dOG3uQSGXGE0bFfILu0OGHM2S M9rgnJleEI0dQci08zqgFKMF1N5DF69wuWd7EdfSqFyiv+1+4Qk1yZ6u/9PNQlGDWu 6HPbAdQGd1fbt9y51sbFg1U0gvLu8Blt3b4LsSA6mCmnKbSjZDEQAYusERrLsThNJp 16QHdUk03zU1h/peVLi5sIFME/4+uqx4SP92NmG3abuPDISFYRvu//69fIeEImePFP L7jyZAHh4J7YA== Received: from mail-pg1-x52a.google.com (mail-pg1-x52a.google.com [IPv6:2607:f8b0:4864:20::52a]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 483D5625FE for ; Tue, 14 Feb 2023 10:37:46 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="N3I7xe66"; dkim-atps=neutral Received: by mail-pg1-x52a.google.com with SMTP id x31so9859380pgl.6 for ; Tue, 14 Feb 2023 01:37:46 -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=5ed0e0o/PIWFeUY6+86mpSLl+MQcwNx1H9NaiJxqlCY=; b=N3I7xe66b7KQtpJ9MtLKmySf+wCvNanvQYSDfwIMk1naHoe3d5PONflV1Wz0tbj/+5 jzy61OBQBU1GBB8TFbCKmzcEhvGta/5KA2Py2okixFlFr1SZS13QK+0FqFVM6ePSypOD 8YvvOeJSdOHzhYKi9g96IV4epw84jd4D7BlMY= 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=5ed0e0o/PIWFeUY6+86mpSLl+MQcwNx1H9NaiJxqlCY=; b=eCP9bHB0zViE/4MoAQzr6JhI5MIJCTUECgLnXJFiY3ApnUXkvxVSKHnptPdN426EW9 5Ek+KsirKReTtI0grp1LZj5jxwD9YIUsvtJ4QC1muoaFG/vSHDLb72i3LrUBt7OC2MmZ 6eTb+A28zFWwbE2q0iWF53nx8DSiBUVz9qJtpTbf/BPnUXplJ3zlG/IW9BcZLqTRUDWi Nul2zadTw1vtXPyxFuhpXLW2KoMnPdRQZf560DCZxZSXGDJe5StbJgoSalekYHqPowdm y6tbOQC7+1S5EoeLKzxRQiVlpZYt/tc0e4bhCXvCWcaFhAC4DTo79ro20q0OYknpdTkO pXsQ== X-Gm-Message-State: AO0yUKW/UkeCGfiZDwu1OfODZt0ReqeYjafbCtBOfY9lkbLS0laMUFjE MDOEiKbJhmx7CguqW4QSNFqYnL7HYH4nwBV7 X-Google-Smtp-Source: AK7set9qEFc04dS2yPGmwulwmsCzVusXo0AqIb2Ys9peTGMwRkYG3LAEvyF9t0592/Xma8DF/XcSYQ== X-Received: by 2002:aa7:9ad8:0:b0:5a8:808a:d3ce with SMTP id x24-20020aa79ad8000000b005a8808ad3cemr1677167pfp.8.1676367464676; Tue, 14 Feb 2023 01:37:44 -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 z9-20020aa791c9000000b0059416691b64sm9696172pfa.19.2023.02.14.01.37.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Feb 2023 01:37:44 -0800 (PST) X-Google-Original-From: Harvey Yang To: libcamera-devel@lists.libcamera.org Date: Tue, 14 Feb 2023 09:37:36 +0000 Message-Id: <20230214093736.4067033-4-chenghaoyang@google.com> X-Mailer: git-send-email 2.39.1.581.gbfd45094c4-goog In-Reply-To: <20230214093736.4067033-1-chenghaoyang@google.com> References: <20230214093736.4067033-1-chenghaoyang@google.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 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: Harvey 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 */