From patchwork Mon May 24 08:48:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naushir Patuck X-Patchwork-Id: 12374 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 1CB55C3201 for ; Mon, 24 May 2021 08:48:33 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 1EDD268923; Mon, 24 May 2021 10:48:31 +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="s5YhR00A"; dkim-atps=neutral Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id AF5D46050E for ; Mon, 24 May 2021 10:48:29 +0200 (CEST) Received: by mail-wr1-x436.google.com with SMTP id d11so27637446wrw.8 for ; Mon, 24 May 2021 01:48:29 -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=W+Swnp6RToqrqm1MpiH1WAAxqrpawJsMhjuvrg/Bpy8=; b=s5YhR00AAut7k7172eUJ0yNQbeL+R/YX4SD4qL1/rGp3nQRJ4OyNzi7FBYmuNODIml drG0hfkQzzVfRIlR0vy91WVuNPiByGyLJc53oijMmcyvPK0szIuIxU3lGLsBtnw2b9Ys 3UL8I8061QmN1yNuodE+G/zdgaW49CpoYnhS5woE+ep2J2UmLIZDhL2oi+woDMcU0Czk PwBDyNCU/hf1c0APeJphU+W2/jr0un5Z5omlv0S9G7kDjH7potnPnU9Zhgy/v+7KC0CP Gw0Y09cIRMF44O7oOgRz9lahEFZTpkhlSY98BE1PxhyTuNJsHVvdoP3wn/jvImPb0a1u qnjQ== 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=W+Swnp6RToqrqm1MpiH1WAAxqrpawJsMhjuvrg/Bpy8=; b=ICtO6vkytBaTCMeH0y5IOdIButQwuFHMTYyMkKrBQIuIKcbXf7beDu9BDWTmPftjj7 o5QG9cIk0LhxPag+dro96a8yUMpnaOYY1rSwQ7XWh+tXKFb8iQTu4sEz5zkvviUisATa sO40EigRWZvIBKwwbt3oqUEnogPrLOsqS9LCnaITonKEMy4BVOdIpdXE8fP9GEVqpXS4 GGfZs0mZosczQ+vkLEqUfSAfPMHDuicF+AR9akC7KGlSuDAj2qqXSko7c3qiPUe8tKDF 4rNPd4T5hSB41BKrnfQz7AzrCnFSs0CsW/f2ov9X5DNvYpJZT0HrQXdiNxJx8J8P7jU5 uExQ== X-Gm-Message-State: AOAM530RQQw8LXNvCqLxH3F77q+sMXo+qtYwp2JMYumNPAjZkopUIDNM xVerQH7oEtkHcf5FiZKHCTuCEdhmKoO3Qg== X-Google-Smtp-Source: ABdhPJwSBR28nHMwJh23RfYa0lYF1wKwpBeVt7IMPfOl98rHfc01Zr2UUBYfDPLv/QGzFq7itYBPnw== X-Received: by 2002:a05:6000:24f:: with SMTP id m15mr21809869wrz.338.1621846109199; Mon, 24 May 2021 01:48:29 -0700 (PDT) Received: from naush-laptop.pitowers.org ([2a00:1098:3142:14:4bf6:b415:2edf:916a]) by smtp.gmail.com with ESMTPSA id x10sm11275767wrt.65.2021.05.24.01.48.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 May 2021 01:48:28 -0700 (PDT) From: Naushir Patuck To: libcamera-devel@lists.libcamera.org Date: Mon, 24 May 2021 09:48:19 +0100 Message-Id: <20210524084822.3690690-2-naush@raspberrypi.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210524084822.3690690-1-naush@raspberrypi.com> References: <20210524084822.3690690-1-naush@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 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 Reviewed-by: Jacopo Mondi --- include/libcamera/internal/utils.h | 42 ++++++++++++++++++++++++++++++ src/libcamera/utils.cpp | 41 +++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/include/libcamera/internal/utils.h b/include/libcamera/internal/utils.h index 83dada7cc16c..f536f2267d70 100644 --- a/include/libcamera/internal/utils.h +++ b/include/libcamera/internal/utils.h @@ -316,8 +316,50 @@ 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 +static inline 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(); +} +#endif + } /* namespace libcamera */ #endif /* __LIBCAMERA_INTERNAL_UTILS_H__ */ diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp index 826144d3c837..480284c6c2c9 100644 --- a/src/libcamera/utils.cpp +++ b/src/libcamera/utils.cpp @@ -506,6 +506,47 @@ std::string libcameraSourcePath() * loop, iterates over an indexed view of the \a iterable */ +/** + * \class Duration + * \brief Helper class for std::chrono::duration types + */ + +/** + * \typedef Duration::BaseDuration + * \brief Base struct for the \a Duration helper class + */ + +/** + * \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, + * internally converting the representation to \a Duration::BaseDuration type + */ + +/** + * \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 */ } /* namespace libcamera */