From patchwork Thu May 20 15:17:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naushir Patuck X-Patchwork-Id: 12328 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 92BACC31FF for ; Thu, 20 May 2021 15:17:24 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 53C2F68921; Thu, 20 May 2021 17:17:24 +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="DjiZ2j8w"; dkim-atps=neutral Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 5250668918 for ; Thu, 20 May 2021 17:17:21 +0200 (CEST) Received: by mail-wr1-x434.google.com with SMTP id c14so16284196wrx.3 for ; Thu, 20 May 2021 08:17:21 -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=DjiZ2j8w32MslxYdfKXyiX1smrL4gVcYnAVXVjSPvCBHqwCr+Kg/N15ELtZ6E+ndqh q4VaK/5Y3mzvHr7lCTf8Jun4M33k6bSWD1wlygUVuPaz9SktfSP50kvOWwL42So/vyHU myxGTJIYxDJ+cV+qZ7PDr5AZf0idsQV1+TDgWa4czSvqPuonP1Eaptzgaw1MV19T/F8E BuW+nHEnOl3FuK+uxtBtqKElgOVnuFIy6uRTbcDDuX3kfJTvFEdMeO+GX3Pjzzhi1sGI ta45JSQkDkRMnyNaEL6MtBCia5Ie2QSOXR5pbaQaW7AVIy7tK3gsYM6YPWn3JK/DdZbh CzZA== 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=gSe/tNo9YGIrY6EQ/foosSLQKu4spHSx11FNzLBaqxG1QkFSFzev0X8z+O1xj3ZhLz 41nq4qIPNmojhgcNtlkm71kc14gtMkaFXGLlfdSOg+3yrgtSukeNl0W2c3OZ21uv2bQ/ U0XlzU24hUZ5QD4LTgerlqxfi7CqCpqSezz6V5ClPscvdwBx0T0BsW9A37WDVYE7/7Q7 RO3BFmk36jMrqWFIDY0JNrzuAVhKIPIjq04wl76JuosTN2H97NjxlHC89SnBZHjVZQ2c 29RrivT3mEPm6pvUDffJw13MdX7/rXyf7ggM9KTKTdqKAUmnH09cia6EjZxrmeZRDAVB u9Qg== X-Gm-Message-State: AOAM5310kJAV/+Oe9VHmsGhJSCMn6XFM6PVFaDHp1DacWDj02JPpTnPm my+TMORSYX7p6frGAPwZxVIydmBOze8j4g== X-Google-Smtp-Source: ABdhPJyNAwJGtJiG4m1W95EOsi6KXPfmTWZKOt5OtZlnE+aIEU9/iyIlt5L3KRm6m8+o96WICmDGoQ== X-Received: by 2002:a5d:568c:: with SMTP id f12mr5089525wrv.382.1621523840627; Thu, 20 May 2021 08:17:20 -0700 (PDT) Received: from naush-laptop.pitowers.org ([2a00:1098:3142:14:de31:dc1f:6be5:f58e]) by smtp.gmail.com with ESMTPSA id a11sm3583885wrr.48.2021.05.20.08.17.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 May 2021 08:17:20 -0700 (PDT) From: Naushir Patuck To: libcamera-devel@lists.libcamera.org Date: Thu, 20 May 2021 16:17:11 +0100 Message-Id: <20210520151714.3366445-2-naush@raspberrypi.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210520151714.3366445-1-naush@raspberrypi.com> References: <20210520151714.3366445-1-naush@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 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 --- 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 */