From patchwork Wed Oct 2 16:19:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Klug X-Patchwork-Id: 21473 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 1AEF2BD80A for ; Wed, 2 Oct 2024 16:20:19 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id B485363527; Wed, 2 Oct 2024 18:20:18 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="CfQxDIek"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id EB1A663522 for ; Wed, 2 Oct 2024 18:20:16 +0200 (CEST) Received: from ideasonboard.com (unknown [IPv6:2a00:6020:448c:6c00:b246:b33f:c662:8ae1]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 5FC661011; Wed, 2 Oct 2024 18:18:43 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1727885924; bh=y3BgbDK0QT8E2RlKsd2t4gyUlEbvDDc5/P43dhofsNU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CfQxDIekzXp3wtqlACvenAiC9LE5b27ZCAY0jiRqKRrCfFaAmWCfxo7gqgQseI0Lv KbPq/6B4vaOdz9nzyB18lzCvSAfnUD9ccr4dPfoXEJnjOGFtrFRfgd01XWZbYzRKpT MrWVEvYJ3XtEpLolDsE/Z7BpXS9YsZxQOQXZVg+c= From: Stefan Klug To: libcamera-devel@lists.libcamera.org Cc: Stefan Klug Subject: [PATCH v1 2/8] libcamera: Add a DebugMetadata helper Date: Wed, 2 Oct 2024 18:19:20 +0200 Message-ID: <20241002161933.247091-3-stefan.klug@ideasonboard.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241002161933.247091-1-stefan.klug@ideasonboard.com> References: <20241002161933.247091-1-stefan.klug@ideasonboard.com> MIME-Version: 1.0 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: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Debug metadata often occurs in places where the metadata control list is not available e.g. in queueRequest() or processStatsBuffer() or even in a class far away from the metadata handling code. It is therefore difficult to add debug metadata without adding lot's of boilerplate code. This can be mitigated by recording the metadata and forwarding it to an upstream helper or the metadata control list when they become available. Add such a helper. Signed-off-by: Stefan Klug --- include/libcamera/internal/debug_controls.h | 52 +++++++ include/libcamera/internal/meson.build | 1 + src/libcamera/control_ids_core.yaml | 6 + src/libcamera/debug_controls.cpp | 144 ++++++++++++++++++++ src/libcamera/meson.build | 1 + 5 files changed, 204 insertions(+) create mode 100644 include/libcamera/internal/debug_controls.h create mode 100644 src/libcamera/debug_controls.cpp diff --git a/include/libcamera/internal/debug_controls.h b/include/libcamera/internal/debug_controls.h new file mode 100644 index 000000000000..d95c03e1db1f --- /dev/null +++ b/include/libcamera/internal/debug_controls.h @@ -0,0 +1,52 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2024, Ideas on Board Oy + * + * Debug metadata helpers + */ + +#pragma once + +#include + +namespace libcamera { + +class DebugMetadata +{ +public: + DebugMetadata() = default; + + void checkForEnable(const ControlList &controls); + void enable(bool enable = true); + void assignUpstream(DebugMetadata *upstream); + void assignControlList(ControlList *list); + + template + void set(const Control &ctrl, const V &value) + { + if (upstream_) { + upstream_->set(ctrl, value); + return; + } + + if (!enabled_) + return; + + if (list_) { + list_->set(ctrl, value); + return; + } + + cache_.set(ctrl, value); + } + + void set(unsigned int id, const ControlValue &value); + +private: + bool enabled_ = false; + ControlList *list_ = nullptr; + DebugMetadata *upstream_ = nullptr; + ControlList cache_; +}; + +} /* namespace libcamera */ diff --git a/include/libcamera/internal/meson.build b/include/libcamera/internal/meson.build index 1c5eef9cab80..1dddcd50c90b 100644 --- a/include/libcamera/internal/meson.build +++ b/include/libcamera/internal/meson.build @@ -14,6 +14,7 @@ libcamera_internal_headers = files([ 'control_serializer.h', 'control_validator.h', 'converter.h', + 'debug_controls.h', 'delayed_controls.h', 'device_enumerator.h', 'device_enumerator_sysfs.h', diff --git a/src/libcamera/control_ids_core.yaml b/src/libcamera/control_ids_core.yaml index 1b1bd9507d25..103bcb593c4a 100644 --- a/src/libcamera/control_ids_core.yaml +++ b/src/libcamera/control_ids_core.yaml @@ -967,5 +967,11 @@ controls: The default gamma value must be 2.2 which closely mimics sRGB gamma. Note that this is camera gamma, so it is applied as 1.0/gamma. + + - DebugMetadataEnable: + type: bool + description: | + Enable or disable the debug metadata. + ... diff --git a/src/libcamera/debug_controls.cpp b/src/libcamera/debug_controls.cpp new file mode 100644 index 000000000000..11a92a6b5871 --- /dev/null +++ b/src/libcamera/debug_controls.cpp @@ -0,0 +1,144 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2024, Ideas on Board Oy. + * + * Helper to easily record debug metadata inside libcamera. + */ + +#include "libcamera/internal/debug_controls.h" + +namespace libcamera { + +/** + * \file debug_controls.h + * \brief Helper to easily record debug metadata inside libcamera + */ + +/** + * \class DebugMetadata + * \brief Helper to record metadata for later use + * + * When one wants to record debug metadata, the metadata list is often not + * directly available (either because we are inside process() of an IPA or + * because we are in a closed module). This class allows to record the data and + * at a later point in time forward it either to another DebugMetadata instance + * or to a ControlList. + */ + +/** + * \fn DebugMetadata::checkForEnable + * \brief Check for DebugMetadataEnable in the supplied ControlList + * \param[in] controls The supplied ControlList + * + * Looks for controls::DebugMetadataEnable and enables or disables debug + * metadata handling accordingly. + */ +void DebugMetadata::checkForEnable(const ControlList &controls) +{ + const auto &ctrl = controls.get(controls::DebugMetadataEnable); + if (ctrl) + enable(*ctrl); +} + +/** + * \fn DebugMetadata::enable + * \brief Enables or disabled metadata handling + * \param[in] enable The enable state + * + * Enables or disables metadata handling according to \a enable. When enable is + * false, the cache gets cleared and no further metadata is recorded. + */ +void DebugMetadata::enable(bool enable) +{ + enabled_ = enable; + if (!enabled_) + cache_.clear(); +} + +/** + * \fn DebugMetadata::assignUpstream + * \brief Assign an upstream metadata handler + * \param[in] upstream Pointer to the upstream handler + * + * When a upstream gets set, the cache is copies to that list and all further + * calls to DebugMetadata::set are forwarded to that list. + * + * The upstream can be reset by passing a nullptr. + */ +void DebugMetadata::assignUpstream(DebugMetadata *upstream) +{ + upstream_ = upstream; + + if (!upstream_) + return; + + for (const auto &ctrl : cache_) + upstream_->set(ctrl.first, ctrl.second); + + cache_.clear(); +} + +/** + * \fn DebugMetadata::assignControlList + * \brief Assign a control list + * \param[in] list Pointer to the list + * + * When a list gets set, the cache is copies to that list and all further calls + * to DebugMetadata::set are forwarded to that list. + * + * The upstream can be reset by passing a nullptr. + */ +void DebugMetadata::assignControlList(ControlList *list) +{ + list_ = list; + + if (list_) { + list_->merge(cache_); + cache_.clear(); + } +} + +/** + * \fn DebugMetadata::set(const Control &ctrl, const V &value) + * \brief Set a value + * \param[in] ctrl The ctrl to set + * \param[in] value The control value + * + * Sets the debug metadata for ctrl to value \a value. If an upstream or a + * control list is set, the value gets passed there (upstream takes precedence). + * Otherwise the value is cached until one of them gets assigned. + * + * When the instance is disabled and upstream is not set, the value gets dropped. + */ + +/** + * \fn DebugMetadata::set(unsigned int id, const ControlValue &value) + * \brief Set a value + * \param[in] id The id of the control + * \param[in] value The control value + * + * Sets the debug metadata to value \a value. If an upstream or a control list + * is set, the value gets passed there (upstream takes precedence). + * Otherwise the value is cached until one of them gets assigned. + * + * When the instance is disabled and upstream is not set, the value gets dropped. + */ +void DebugMetadata::set(unsigned int id, const ControlValue &value) +{ + if (upstream_) { + upstream_->set(id, value); + return; + } + + if (!enabled_) + return; + + if (list_) { + list_->set(id, value); + return; + } + + cache_.set(id, value); +} + +} /* namespace libcamera */ diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build index aa9ab0291854..f7b5ee8dcc34 100644 --- a/src/libcamera/meson.build +++ b/src/libcamera/meson.build @@ -25,6 +25,7 @@ libcamera_internal_sources = files([ 'control_validator.cpp', 'converter.cpp', 'delayed_controls.cpp', + 'debug_controls.cpp', 'device_enumerator.cpp', 'device_enumerator_sysfs.cpp', 'dma_buf_allocator.cpp',