From patchwork Fri May 21 08:05:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naushir Patuck X-Patchwork-Id: 12333 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 2AD45C31FB for ; Fri, 21 May 2021 08:05:40 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id AC46468923; Fri, 21 May 2021 10:05:39 +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="IVfewFgr"; dkim-atps=neutral Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id BCE826891B for ; Fri, 21 May 2021 10:05:37 +0200 (CEST) Received: by mail-wr1-x42e.google.com with SMTP id z17so20081656wrq.7 for ; Fri, 21 May 2021 01:05:37 -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=yRQJD3FtFPwqzHyk8t5WbR7NjNmKo1823QiCzgBqqEg=; b=IVfewFgrePpL02prm8bD2YxpeG3vFFhYBXwC9LyCbkh2+M3oUVrHfdGikND+HqJMS1 s1ht1grJ8hNjmgZDNL0HgEKUQEsMJMzVZ987dgpwy+F5d96yhdvpuiqBzdEGNP+E46AW jR3X9k+GEH0Lumn96xVLttxLK6zVQOV2A4GHIiGgkCPITvFXirBAz1DUicjASn0181Zc GnphrvGchQgrG32RVrQ2PCPjacKRrPlkEYFIPW++MUvfbErF1ipKSp9wkKVErxddp2+b 6oBHtmc4WE2gaPopgjpE9rPFTcSKkArgpgBsbwVHW4PFuBPvMvhrJ2Za8ZSa+l7E5m2w CSTg== 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=yRQJD3FtFPwqzHyk8t5WbR7NjNmKo1823QiCzgBqqEg=; b=quZIVvwT6H1z2Djc8zxI20ng+t9ltLkz3ZcDsz84dFkWqVFgbKu/iM5eThPj4jZDMx Kb9RV8Ky057/YF8IZbsRIx4IQgl02UG0wlbj5wBQt6KZFqUIVvP2NOecbvJLStFZK9Uw dW/n1yS0s2kwq4LJWzJ+icxA5oJrjj81K9ezKWEnVebPptu5aAKG+hS+wuhN5UXjpYyG ytUyepUs2gS8B42K1MbIqonuMDlB/DlHS7iuwUoZeJMNFXbLWSKRKOWBPengrdwyB/gJ 1W/kfp7OF6wcYz14VvJAHz04CGZxo4tmxMRdAdkoDlMav8UAhnrzhXuJqmU66CpAS1Ur 3YRw== X-Gm-Message-State: AOAM530zSBN7Qnqj7WZONTiZIfd+47mRfk3dJI0ns0ZMFIySpZU8mi83 mdRXg8H25fimOZwuL+WSJnr8QSJUwe80ZA== X-Google-Smtp-Source: ABdhPJx7cfi6U+pLfI2++suwRSH5pvOI/+xiauMfN4ST2Gip66k0T3TyTEpX+/p8BxBx6IhdP3Mcmg== X-Received: by 2002:a5d:638b:: with SMTP id p11mr8213160wru.90.1621584337297; Fri, 21 May 2021 01:05:37 -0700 (PDT) Received: from naush-laptop.pitowers.org ([2a00:1098:3142:14:de31:dc1f:6be5:f58e]) by smtp.gmail.com with ESMTPSA id q186sm5159176wme.20.2021.05.21.01.05.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 May 2021 01:05:36 -0700 (PDT) From: Naushir Patuck To: libcamera-devel@lists.libcamera.org Date: Fri, 21 May 2021 09:05:27 +0100 Message-Id: <20210521080530.37591-2-naush@raspberrypi.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210521080530.37591-1-naush@raspberrypi.com> References: <20210521080530.37591-1-naush@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 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 define 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 --- include/libcamera/internal/utils.h | 42 ++++++++++++++++++++++++++++++ src/libcamera/utils.cpp | 36 +++++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/include/libcamera/internal/utils.h b/include/libcamera/internal/utils.h index 83dada7cc16c..a377d4ea07ab 100644 --- a/include/libcamera/internal/utils.h +++ b/include/libcamera/internal/utils.h @@ -316,6 +316,48 @@ auto enumerate(T (&iterable)[N]) -> details::enumerate_adapter } #endif +using BaseDuration = std::chrono::duration; + +class Duration : public BaseDuration +{ +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(); + } +}; + +#ifndef __DOXYGEN__ +template +static inline std::basic_ostream &operator<<(std::basic_ostream &os, + const 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(); +} +#endif + } /* namespace utils */ } /* namespace libcamera */ diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp index 826144d3c837..3131aa2d6666 100644 --- a/src/libcamera/utils.cpp +++ b/src/libcamera/utils.cpp @@ -506,6 +506,42 @@ std::string libcameraSourcePath() * loop, iterates over an indexed view of the \a iterable */ +/** + * \class Duration + * \brief Helper for std::chrono::duration types. Derived from \a BaseDuration + */ + +/** + * \fn Duration::Duration(const std::chrono::duration &d) + * \brief Copy constructor from a \a std::chrono::duration type. + * \param[in] d The std::chrono::duration object to convert from. + * + * Constructs a \a Duration object from a \a std::chrono::duration object, + * converting the representation to \a BaseDuration type. + */ + +/** + * \fn Duration::get() + * \brief Retrieve the tick count, coverted 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 Tick count + */ + +/** + * \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 */ } /* namespace libcamera */