From patchwork Tue May 25 11:42:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naushir Patuck X-Patchwork-Id: 12410 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 5E4E1C3203 for ; Tue, 25 May 2021 11:42:50 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 9101668927; Tue, 25 May 2021 13:42:48 +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="OCV1cbS9"; dkim-atps=neutral Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id E8F3A6050E for ; Tue, 25 May 2021 13:42:46 +0200 (CEST) Received: by mail-wr1-x42b.google.com with SMTP id x8so31866240wrq.9 for ; Tue, 25 May 2021 04:42:46 -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=XbwArGlWfTb3MkQN+yHRt2+njlOw3CwtqVUMKQwzAaI=; b=OCV1cbS9W2e07GOkWdQQUt7Muk2gXBZVsI2Gs3s7wL5Sb45GTvywfgIdE5xNtlDvKH smF1QHUZldTa/Xh1IUBVVjhwCtmWD2hkJywgLm3y26+meD8FcVif+gFcpybRFVOr3SVC 4LZpKw07VxMA4HI9CmNvK2gEfd86kCl9PU0f5SJQ10Pl9WZ6oMxULXXDMcjL3uUmH/dg RXdQIz7zoVZ/N9BIOnSmYMHGstb6omh+mUmPg46vducVW+wH1AUv7r8leuhwWGaXZYRU 9ksOo1avTuUVoV3ZrT0Yghpctldu8UxbmdLrUMmOEcR9j06DUElCHvF2tAc3gnz19XLH pyjA== 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=XbwArGlWfTb3MkQN+yHRt2+njlOw3CwtqVUMKQwzAaI=; b=gcmP+ITzPYqxjivrJyiI1EW3S/eL5sDFwCeVpb6mxb6qwC+rLE42y65pKp1GD72ElU nU5YM0c6xz6oroDM1m0OXGZFL1fSPur5vtAi26R4r7XtFfbSUtmZSeaJpD9EK98aI5v9 5eFBVVNVq4YpEtnKwDMRO6RIdbmHSOTGEZeggSo4XnJCazivgIDsg+Lm6jCvqdm7bJRh 9fVeyEps48agdrKgQT3NWx43jbaQrNGJdVUoH4s2TRkLcfeEVsMJ/6LEZJ1J4LlwPE1E WTr5hmPY+E2Ajc2lEvLn5i2TcfKGqzBkx5198hnMTLH++/rTdqcfIHzmM4RF49uYktY7 V1Cw== X-Gm-Message-State: AOAM532oSj6YFkJfVx+b3GOSykPxm1jG2XYHU3b3KrLY7uv53lj58MM8 WqMRiE4qqOrMqSUDUvuJTuqoxzJR8xe8MA== X-Google-Smtp-Source: ABdhPJw1kHCzt8PAqsNoLujuaiofMhQajZZxD+clLUKRdSzxbXxdK6Ll5vWVKX1DcgVegbe5ycyrIQ== X-Received: by 2002:adf:ed52:: with SMTP id u18mr26155972wro.379.1621942966386; Tue, 25 May 2021 04:42:46 -0700 (PDT) Received: from naush-laptop.pitowers.org ([2a00:1098:3142:14:f3e2:c7ee:8a9d:4c3a]) by smtp.gmail.com with ESMTPSA id v15sm2471443wmj.39.2021.05.25.04.42.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 May 2021 04:42:45 -0700 (PDT) From: Naushir Patuck To: libcamera-devel@lists.libcamera.org Date: Tue, 25 May 2021 12:42:38 +0100 Message-Id: <20210525114241.906280-2-naush@raspberrypi.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210525114241.906280-1-naush@raspberrypi.com> References: <20210525114241.906280-1-naush@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v5 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 Reviewed-by: Laurent Pinchart --- include/libcamera/internal/utils.h | 42 ++++++++++++++++++++++++++++++ src/libcamera/utils.cpp | 37 ++++++++++++++++++++++++++ 2 files changed, 79 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..6624ff74cdc7 100644 --- a/src/libcamera/utils.cpp +++ b/src/libcamera/utils.cpp @@ -506,6 +506,43 @@ 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 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 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 */ } /* namespace libcamera */