From patchwork Tue Jun 8 11:03:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naushir Patuck X-Patchwork-Id: 12514 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 DC17DC320B for ; Tue, 8 Jun 2021 11:03:42 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 943FF602A7; Tue, 8 Jun 2021 13:03:41 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=raspberrypi.com header.i=@raspberrypi.com header.b="Qn+rLU5w"; dkim-atps=neutral Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 7377F602A0 for ; Tue, 8 Jun 2021 13:03:40 +0200 (CEST) Received: by mail-wm1-x32f.google.com with SMTP id f16-20020a05600c1550b02901b00c1be4abso1647893wmg.2 for ; Tue, 08 Jun 2021 04:03:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raspberrypi.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=AP35MsAf3IUqIZd38LNp8BO/7DuGvRroW2gGEQZ1KD8=; b=Qn+rLU5wJyzEt8TpEAlDYBrJwQDt+g2cIOa1QKFSP4KVdkOBSxqyFwCg3LGTKkqhHz NBmUvyJZjjVcInP4kTH9xfWyxJ61+dU+iF0E6QAbBioLH895q2SCUCiyR7tOn4DQAXLd jprG4atOsbTRcg7bZ22h1L4rbZm+4nk7h3mRtWaMXa4iuzmau2VM3VixrnXgBtH3c0sl Sv7mZ4Ayxdghb36/m88ei00fuRSv2dtzLGWFUhGxtvxiOG9grW2axKMApb5ZSI/yyOI3 vAxoHeQO9l37HMElCW+dLTt9EHfZKXCL1ZL13b56FgCTF2wCiyWLu0tmgcOeCzHSKpcd f1xw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=AP35MsAf3IUqIZd38LNp8BO/7DuGvRroW2gGEQZ1KD8=; b=bPbOU67gJQRE1LHNvLoTBVK60q4xNo6LhQxPDKzRxEOn4WNQghlwFR7tqzqrtOyTJ3 vwDVFUwRJebcAYWZUX2yNJcJJWeCLWnM5alUXU34FkPCPlVa+HUcf5hzaDBCMOG0E251 uoUNrQqGXzym1x7hYBkXfTxTZ+H2thINxdtdEgxcIO8dIxQHLC8VmnLcci1EznoYQIPs PqN33rRx26m7HE7i1JIsSJSnWjtMoKBZYuXStYw6ZPoZ4Q3CQxyHRoMJQ3XR05tUFLsM CQQlm68vvHu2kiJxfdRbd0YsIVmFYeZBFprRVqBreoqYgoIYzS/y6IKjWT5DEqS3V+Sq vWIg== X-Gm-Message-State: AOAM532cw+wDRlHxwfgu0IQkuIlcklZEKK8dP+z6uvQYG45sCVU34WJW zic6aHsQFvqwlUdKeS5Th3a8SdUrT6fM9g== X-Google-Smtp-Source: ABdhPJzcsXxyssbz9BDJraJVWUS+DvcyVNEPbktYtosS+lTio7Xww8thnCAdgdUTogcB1hGH+lXXLw== X-Received: by 2002:a1c:b783:: with SMTP id h125mr3654115wmf.182.1623150219885; Tue, 08 Jun 2021 04:03:39 -0700 (PDT) Received: from naush-laptop.pitowers.org ([2a00:1098:3142:14:5aef:f665:51b0:d8c0]) by smtp.gmail.com with ESMTPSA id o129sm2639350wmo.22.2021.06.08.04.03.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Jun 2021 04:03:39 -0700 (PDT) From: Naushir Patuck To: libcamera-devel@lists.libcamera.org Date: Tue, 8 Jun 2021 12:03:32 +0100 Message-Id: <20210608110335.4078551-2-naush@raspberrypi.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210608110335.4078551-1-naush@raspberrypi.com> References: <20210608110335.4078551-1-naush@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v6 1/4] libcamera: utils: Add helper class for std::chrono::duration 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" A new utils::Duration class is defined to represent a std::chrono::duration type with double precision nanosecond timebase. Using a double minimises the loss of precision when converting timebases. This helper class may be used by IPAs to represent variables such as frame durations and exposure times. An operator << overload is defined to help with displaying utils::Duration value in stream objects. Currently, this will display the duration value in microseconds. Signed-off-by: Naushir Patuck Reviewed-by: David Plowman Reviewed-by: Jacopo Mondi Reviewed-by: Laurent Pinchart --- include/libcamera/internal/utils.h | 32 +++++++++++++++++ src/libcamera/utils.cpp | 58 ++++++++++++++++++++++++++++++ test/utils.cpp | 45 +++++++++++++++++++++++ 3 files changed, 135 insertions(+) diff --git a/include/libcamera/internal/utils.h b/include/libcamera/internal/utils.h index 83dada7cc16c..15beb0f44172 100644 --- a/include/libcamera/internal/utils.h +++ b/include/libcamera/internal/utils.h @@ -316,8 +316,40 @@ auto enumerate(T (&iterable)[N]) -> details::enumerate_adapter } #endif +class Duration : public std::chrono::duration +{ + using BaseDuration = std::chrono::duration; + +public: + Duration() = default; + + template + constexpr Duration(const std::chrono::duration &d) + : BaseDuration(d) + { + } + + template + double get() const + { + auto const c = std::chrono::duration_cast>(*this); + return c.count(); + } + + explicit constexpr operator bool() const + { + return *this != BaseDuration::zero(); + } +}; + } /* namespace utils */ +#ifndef __DOXYGEN__ +template +std::basic_ostream &operator<<(std::basic_ostream &os, + const utils::Duration &d); +#endif + } /* namespace libcamera */ #endif /* __LIBCAMERA_INTERNAL_UTILS_H__ */ diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp index 826144d3c837..2e7d35fb1173 100644 --- a/src/libcamera/utils.cpp +++ b/src/libcamera/utils.cpp @@ -506,6 +506,64 @@ std::string libcameraSourcePath() * loop, iterates over an indexed view of the \a iterable */ +/** + * \class Duration + * \brief Helper class from std::chrono::duration that represents a time + * duration in nanoseconds with double precision + */ + +/** + * \fn Duration::Duration(const std::chrono::duration &d) + * \brief Construct a Duration by converting an arbitrary std::chrono::duration + * \param[in] d The std::chrono::duration object to convert from + * + * The constructed \a Duration object is internally represented in double + * precision with nanoseconds ticks. + */ + +/** + * \fn Duration::get() + * \brief Retrieve the tick count, converted to the timebase provided by the + * template argument Period of type \a std::ratio + * + * A typical usage example is given below: + * + * \code{.cpp} + * utils::Duration d = 5s; + * double d_in_ms = d.get(); + * \endcode + * + * \return The tick count of the Duration expressed in \a Period + */ + +/** + * \fn Duration::operator bool() + * \brief Boolean operator to test if a \a Duration holds a non-zero time value + * + * \return True if \a Duration is a non-zero time value, False otherwise + */ + } /* namespace utils */ +#ifndef __DOXYGEN__ +template +std::basic_ostream &operator<<(std::basic_ostream &os, + const utils::Duration &d) +{ + std::basic_ostringstream s; + + s.flags(os.flags()); + s.imbue(os.getloc()); + s.setf(std::ios_base::fixed, std::ios_base::floatfield); + s.precision(2); + s << d.get() << "us"; + return os << s.str(); +} + +template +std::basic_ostream> & +operator<< >(std::basic_ostream> &os, + const utils::Duration &d); +#endif + } /* namespace libcamera */ diff --git a/test/utils.cpp b/test/utils.cpp index 7e24c71e4775..f170ae4c2f35 100644 --- a/test/utils.cpp +++ b/test/utils.cpp @@ -20,6 +20,7 @@ using namespace std; using namespace libcamera; +using namespace std::literals::chrono_literals; class UtilsTest : public Test { @@ -128,6 +129,46 @@ protected: return TestPass; } + int testDuration() + { + std::ostringstream os; + utils::Duration exposure; + double ratio; + + exposure = 25ms + 25ms; + if (exposure.get() != 50000.0) { + cerr << "utils::Duration failed to return microsecond count"; + return TestFail; + } + + exposure = 1.0s / 4; + if (exposure != 250ms) { + cerr << "utils::Duration failed scalar divide test"; + return TestFail; + } + + exposure = 5000.5us; + if (!exposure) { + cerr << "utils::Duration failed boolean test"; + return TestFail; + } + + os << exposure; + if (os.str() != "5000.50us") { + cerr << "utils::Duration operator << failed"; + return TestFail; + } + + exposure = 100ms; + ratio = exposure / 25ms; + if (ratio != 4.0) { + cerr << "utils::Duration failed ratio test"; + return TestFail; + } + + return TestPass; + } + int run() { /* utils::hex() test. */ @@ -236,6 +277,10 @@ protected: if (testEnumerate() != TestPass) return TestFail; + /* utils::Duration test. */ + if (testDuration() != TestPass) + return TestFail; + return TestPass; } };