From patchwork Tue Jul 20 10:13:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Elder X-Patchwork-Id: 13055 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 2D490C0109 for ; Tue, 20 Jul 2021 10:13:33 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id DADB76854A; Tue, 20 Jul 2021 12:13:32 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="Ql6WQV4r"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id BB29B68547 for ; Tue, 20 Jul 2021 12:13:31 +0200 (CEST) Received: from pyrite.rasen.tech (unknown [IPv6:2400:4051:61:600:2c71:1b79:d06d:5032]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 57439443; Tue, 20 Jul 2021 12:13:30 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1626776011; bh=d1X10EYGPM85ZodyM1tCiEIUhaOTFbIdzGP3+80v8Qw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ql6WQV4rVG3DMHGdzXCt7om0dds4IOCu1VM82Tg67HzeILUguK1hguZe0as4GovEX ypfn7i9mXhsCqBvP19aeItC1hi7/BnE4nu114DT0pS1LMJExrxjizq+XBOp2exBXN6 EPgL1ZoykxS5vAv8z8JwABmfXNT4il+5Dx4a9kGQ= From: Paul Elder To: libcamera-devel@lists.libcamera.org Date: Tue, 20 Jul 2021 19:13:04 +0900 Message-Id: <20210720101307.26010-7-paul.elder@ideasonboard.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210720101307.26010-1-paul.elder@ideasonboard.com> References: <20210720101307.26010-1-paul.elder@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v5 6/9] android: Add helpers for setting android metadata from libcamera controls 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" Add helpers for setting android metadata from libcamera controls. There are two versions, for scalars and collections, both of which take a default value to fill in the android control if the libcamera control is not found. A version for scalars exists for no default, to not set the android control at all if it is not found in libcamera. The functions take two template parameters, the first for the android type, and the second for the libcamera type of the control. They can be different, for example, if the former is an enum and the latter is a boolean, or if the former is an enum (uint8_t) and the latter is an enum (int32_t). The versions that take a default value return the value that was set in the android metadata. Signed-off-by: Paul Elder --- Changes in v4: - remove vector copy from the vector setter Changes in v3: - setMetadata for collection only works with vectors - change enum to enum class - add two template parameters for android type and libcamera type - add docs New in v2 TODO: make ControlList versions so that we can use them in result metadata --- src/android/camera_capabilities.cpp | 137 ++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) diff --git a/src/android/camera_capabilities.cpp b/src/android/camera_capabilities.cpp index c97a17e6..a8fa7ffe 100644 --- a/src/android/camera_capabilities.cpp +++ b/src/android/camera_capabilities.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include @@ -125,6 +126,142 @@ hwLevelStrings = { { ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL_EXTERNAL, "EXTERNAL" }, }; +enum class ControlRange { + Min, + Def, + Max, +}; + +/** + * \brief Set android metadata from libcamera ControlInfo + * \tparam T Type of the control in android + * \tparam V Type of the control in libcamera + * \param[in] metadata Android metadata pack to add the control value to + * \param[in] tag Android metadata tag + * \param[in] controlsInfo libcamera ControlInfoMap from which to find the control info + * \param[in] control libcamera ControlId to find from \a controlsInfo + * \param[in] controlRange Whether to use the min, def, or max value from the control info + * + * Set the android metadata entry in \a metadata with tag \a tag based on the + * control info found for the libcamera control \a control in the libcamera + * ControlInfoMap \a controlsInfo. If no libcamera ControlInfo is found, then + * the function returns without modifying anything. + * + * This function is for scalar values. + */ +template> * = nullptr, + typename V> +void setMetadata(CameraMetadata *metadata, uint32_t tag, + const ControlInfoMap &controlsInfo, const ControlId *control, + enum ControlRange controlRange) +{ + const auto &info = controlsInfo.find(control); + if (info == controlsInfo.end()) + return; + + T ret; + switch (controlRange) { + case ControlRange::Min: + ret = info->second.min().get(); + break; + case ControlRange::Def: + ret = info->second.def().get(); + break; + case ControlRange::Max: + ret = info->second.max().get(); + break; + } + + metadata->addEntry(tag, ret); + return; +} + +/** + * \brief Set android metadata from libcamera ControlInfo or a default value + * \tparam T Type of the control in android + * \tparam U Type of the control in libcamera + * \param[in] metadata Android metadata pack to add the control value to + * \param[in] tag Android metadata tag + * \param[in] controlsInfo libcamera ControlInfoMap from which to find the control info + * \param[in] control libcamera ControlId to find from \a controlsInfo + * \param[in] controlRange Whether to use the min, def, or max value from the control info + * \param[in] defaultValue The value to set in \a metadata if \a control is not found + * + * Set the android metadata entry in \a metadata with tag \a tag based on the + * control info found for the libcamera control \a control in the libcamera + * ControlInfoMap \a controlsInfo. If no libcamera ControlInfo is found, then + * the android metadata entry is set to \a defaultValue. + * + * This function is for scalar values. + */ +template || + std::is_enum_v> * = nullptr> +T setMetadata(CameraMetadata *metadata, uint32_t tag, + const ControlInfoMap &controlsInfo, const ControlId *control, + enum ControlRange controlRange, const V defaultValue) +{ + T ret = defaultValue; + + const auto &info = controlsInfo.find(control); + if (info != controlsInfo.end()) { + switch (controlRange) { + case ControlRange::Min: + ret = info->second.min().get(); + break; + case ControlRange::Def: + ret = info->second.def().get(); + break; + case ControlRange::Max: + ret = info->second.max().get(); + break; + } + } + + metadata->addEntry(tag, ret); + return ret; +} + +/** + * \brief Set android metadata from libcamera ControlInfo or a default value + * \tparam T Type of the control in android + * \tparam V Type of the control in libcamera + * \param[in] metadata Android metadata pack to add the control value to + * \param[in] tag Android metadata tag + * \param[in] controlsInfo libcamera ControlInfoMap from which to find the control info + * \param[in] control libcamera ControlId to find from \a controlsInfo + * \param[in] defaultVector The value to set in \a metadata if \a control is not found + * + * Set the android metadata entry in \a metadata with tag \a tag based on the + * control info found for the libcamera control \a control in the libcamera + * ControlInfoMap \a controlsInfo. If no libcamera ControlInfo is found, then + * the android metadata entry is set to \a defaultVector. + * + * This function is for vector values. + */ +template +std::vector setMetadata(CameraMetadata *metadata, uint32_t tag, + const ControlInfoMap &controlsInfo, + const ControlId *control, + const std::vector &defaultVector) +{ + const auto &info = controlsInfo.find(control); + if (info == controlsInfo.end()) { + metadata->addEntry(tag, defaultVector); + return defaultVector; + } + + std::vector ret(info->second.values().size()); + for (const auto &value : info->second.values()) + ret.push_back(value.get()); + metadata->addEntry(tag, ret); + + return ret; +} + } /* namespace */ bool CameraCapabilities::validateManualSensorCapability(const CameraMetadata *staticMetadata)