{"id":12514,"url":"https://patchwork.libcamera.org/api/patches/12514/?format=json","web_url":"https://patchwork.libcamera.org/patch/12514/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20210608110335.4078551-2-naush@raspberrypi.com>","date":"2021-06-08T11:03:32","name":"[libcamera-devel,v6,1/4] libcamera: utils: Add helper class for std::chrono::duration","commit_ref":"5055ca747c4c0d635ed8f2ab272f493748c2f50a","pull_url":null,"state":"accepted","archived":false,"hash":"7f18d867632a161a6c4c41e78a58ea268aa0d0c8","submitter":{"id":34,"url":"https://patchwork.libcamera.org/api/people/34/?format=json","name":"Naushir Patuck","email":"naush@raspberrypi.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/12514/mbox/","series":[{"id":2110,"url":"https://patchwork.libcamera.org/api/series/2110/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2110","date":"2021-06-08T11:03:31","name":"Switch RaspberryPi IPA to use std::chrono::duration","version":6,"mbox":"https://patchwork.libcamera.org/series/2110/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/12514/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/12514/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id DC17DC320B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue,  8 Jun 2021 11:03:42 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 943FF602A7;\n\tTue,  8 Jun 2021 13:03:41 +0200 (CEST)","from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com\n\t[IPv6:2a00:1450:4864:20::32f])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 7377F602A0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue,  8 Jun 2021 13:03:40 +0200 (CEST)","by mail-wm1-x32f.google.com with SMTP id\n\tf16-20020a05600c1550b02901b00c1be4abso1647893wmg.2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 08 Jun 2021 04:03:40 -0700 (PDT)","from naush-laptop.pitowers.org\n\t([2a00:1098:3142:14:5aef:f665:51b0:d8c0])\n\tby smtp.gmail.com with ESMTPSA id\n\to129sm2639350wmo.22.2021.06.08.04.03.39\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tTue, 08 Jun 2021 04:03:39 -0700 (PDT)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (2048-bit key;\n\tunprotected) header.d=raspberrypi.com header.i=@raspberrypi.com\n\theader.b=\"Qn+rLU5w\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=raspberrypi.com; s=google;\n\th=from:to:cc:subject:date:message-id:in-reply-to:references\n\t:mime-version:content-transfer-encoding;\n\tbh=AP35MsAf3IUqIZd38LNp8BO/7DuGvRroW2gGEQZ1KD8=;\n\tb=Qn+rLU5wJyzEt8TpEAlDYBrJwQDt+g2cIOa1QKFSP4KVdkOBSxqyFwCg3LGTKkqhHz\n\tNBmUvyJZjjVcInP4kTH9xfWyxJ61+dU+iF0E6QAbBioLH895q2SCUCiyR7tOn4DQAXLd\n\tjprG4atOsbTRcg7bZ22h1L4rbZm+4nk7h3mRtWaMXa4iuzmau2VM3VixrnXgBtH3c0sl\n\tSv7mZ4Ayxdghb36/m88ei00fuRSv2dtzLGWFUhGxtvxiOG9grW2axKMApb5ZSI/yyOI3\n\tvAxoHeQO9l37HMElCW+dLTt9EHfZKXCL1ZL13b56FgCTF2wCiyWLu0tmgcOeCzHSKpcd\n\tf1xw==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\n\t:references:mime-version:content-transfer-encoding;\n\tbh=AP35MsAf3IUqIZd38LNp8BO/7DuGvRroW2gGEQZ1KD8=;\n\tb=bPbOU67gJQRE1LHNvLoTBVK60q4xNo6LhQxPDKzRxEOn4WNQghlwFR7tqzqrtOyTJ3\n\tvwDVFUwRJebcAYWZUX2yNJcJJWeCLWnM5alUXU34FkPCPlVa+HUcf5hzaDBCMOG0E251\n\tuoUNrQqGXzym1x7hYBkXfTxTZ+H2thINxdtdEgxcIO8dIxQHLC8VmnLcci1EznoYQIPs\n\tPqN33rRx26m7HE7i1JIsSJSnWjtMoKBZYuXStYw6ZPoZ4Q3CQxyHRoMJQ3XR05tUFLsM\n\tCQQlm68vvHu2kiJxfdRbd0YsIVmFYeZBFprRVqBreoqYgoIYzS/y6IKjWT5DEqS3V+Sq\n\tvWIg==","X-Gm-Message-State":"AOAM532cw+wDRlHxwfgu0IQkuIlcklZEKK8dP+z6uvQYG45sCVU34WJW\n\tzic6aHsQFvqwlUdKeS5Th3a8SdUrT6fM9g==","X-Google-Smtp-Source":"ABdhPJzcsXxyssbz9BDJraJVWUS+DvcyVNEPbktYtosS+lTio7Xww8thnCAdgdUTogcB1hGH+lXXLw==","X-Received":"by 2002:a1c:b783:: with SMTP id\n\th125mr3654115wmf.182.1623150219885; \n\tTue, 08 Jun 2021 04:03:39 -0700 (PDT)","From":"Naushir Patuck <naush@raspberrypi.com>","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","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v6 1/4] libcamera: utils: Add helper class\n\tfor std::chrono::duration","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"A new utils::Duration class is defined to represent a std::chrono::duration type\nwith double precision nanosecond timebase. Using a double minimises the loss of\nprecision when converting timebases. This helper class may be used by IPAs to\nrepresent variables such as frame durations and exposure times.\n\nAn operator << overload is defined to help with displaying utils::Duration value\nin stream objects. Currently, this will display the duration value in\nmicroseconds.\n\nSigned-off-by: Naushir Patuck <naush@raspberrypi.com>\nReviewed-by: David Plowman <david.plowman@raspberrypi.com>\nReviewed-by: Jacopo Mondi <jacopo@jmondi.org>\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n include/libcamera/internal/utils.h | 32 +++++++++++++++++\n src/libcamera/utils.cpp            | 58 ++++++++++++++++++++++++++++++\n test/utils.cpp                     | 45 +++++++++++++++++++++++\n 3 files changed, 135 insertions(+)","diff":"diff --git a/include/libcamera/internal/utils.h b/include/libcamera/internal/utils.h\nindex 83dada7cc16c..15beb0f44172 100644\n--- a/include/libcamera/internal/utils.h\n+++ b/include/libcamera/internal/utils.h\n@@ -316,8 +316,40 @@ auto enumerate(T (&iterable)[N]) -> details::enumerate_adapter<T *>\n }\n #endif\n \n+class Duration : public std::chrono::duration<double, std::nano>\n+{\n+\tusing BaseDuration = std::chrono::duration<double, std::nano>;\n+\n+public:\n+\tDuration() = default;\n+\n+\ttemplate<typename Rep, typename Period>\n+\tconstexpr Duration(const std::chrono::duration<Rep, Period> &d)\n+\t\t: BaseDuration(d)\n+\t{\n+\t}\n+\n+\ttemplate<typename Period>\n+\tdouble get() const\n+\t{\n+\t\tauto const c = std::chrono::duration_cast<std::chrono::duration<double, Period>>(*this);\n+\t\treturn c.count();\n+\t}\n+\n+\texplicit constexpr operator bool() const\n+\t{\n+\t\treturn *this != BaseDuration::zero();\n+\t}\n+};\n+\n } /* namespace utils */\n \n+#ifndef __DOXYGEN__\n+template<class CharT, class Traits>\n+std::basic_ostream<CharT, Traits> &operator<<(std::basic_ostream<CharT, Traits> &os,\n+\t\t\t\t\t      const utils::Duration &d);\n+#endif\n+\n } /* namespace libcamera */\n \n #endif /* __LIBCAMERA_INTERNAL_UTILS_H__ */\ndiff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp\nindex 826144d3c837..2e7d35fb1173 100644\n--- a/src/libcamera/utils.cpp\n+++ b/src/libcamera/utils.cpp\n@@ -506,6 +506,64 @@ std::string libcameraSourcePath()\n  * loop, iterates over an indexed view of the \\a iterable\n  */\n \n+/**\n+ * \\class Duration\n+ * \\brief Helper class from std::chrono::duration that represents a time\n+ * duration in nanoseconds with double precision\n+ */\n+\n+/**\n+ * \\fn Duration::Duration(const std::chrono::duration<Rep, Period> &d)\n+ * \\brief Construct a Duration by converting an arbitrary std::chrono::duration\n+ * \\param[in] d The std::chrono::duration object to convert from\n+ *\n+ * The constructed \\a Duration object is internally represented in double\n+ * precision with nanoseconds ticks.\n+ */\n+\n+/**\n+ * \\fn Duration::get<Period>()\n+ * \\brief Retrieve the tick count, converted to the timebase provided by the\n+ * template argument Period of type \\a std::ratio\n+ *\n+ * A typical usage example is given below:\n+ *\n+ * \\code{.cpp}\n+ * utils::Duration d = 5s;\n+ * double d_in_ms = d.get<std::milli>();\n+ * \\endcode\n+ *\n+ * \\return The tick count of the Duration expressed in \\a Period\n+ */\n+\n+/**\n+ * \\fn Duration::operator bool()\n+ * \\brief Boolean operator to test if a \\a Duration holds a non-zero time value\n+ *\n+ * \\return True if \\a Duration is a non-zero time value, False otherwise\n+ */\n+\n } /* namespace utils */\n \n+#ifndef __DOXYGEN__\n+template<class CharT, class Traits>\n+std::basic_ostream<CharT, Traits> &operator<<(std::basic_ostream<CharT, Traits> &os,\n+\t\t\t\t\t      const utils::Duration &d)\n+{\n+\tstd::basic_ostringstream<CharT, Traits> s;\n+\n+\ts.flags(os.flags());\n+\ts.imbue(os.getloc());\n+\ts.setf(std::ios_base::fixed, std::ios_base::floatfield);\n+\ts.precision(2);\n+\ts << d.get<std::micro>() << \"us\";\n+\treturn os << s.str();\n+}\n+\n+template\n+std::basic_ostream<char, std::char_traits<char>> &\n+operator<< <char, std::char_traits<char>>(std::basic_ostream<char, std::char_traits<char>> &os,\n+\t\t\t\t\t  const utils::Duration &d);\n+#endif\n+\n } /* namespace libcamera */\ndiff --git a/test/utils.cpp b/test/utils.cpp\nindex 7e24c71e4775..f170ae4c2f35 100644\n--- a/test/utils.cpp\n+++ b/test/utils.cpp\n@@ -20,6 +20,7 @@\n \n using namespace std;\n using namespace libcamera;\n+using namespace std::literals::chrono_literals;\n \n class UtilsTest : public Test\n {\n@@ -128,6 +129,46 @@ protected:\n \t\treturn TestPass;\n \t}\n \n+\tint testDuration()\n+\t{\n+\t\tstd::ostringstream os;\n+\t\tutils::Duration exposure;\n+\t\tdouble ratio;\n+\n+\t\texposure = 25ms + 25ms;\n+\t\tif (exposure.get<std::micro>() != 50000.0) {\n+\t\t\tcerr << \"utils::Duration failed to return microsecond count\";\n+\t\t\treturn TestFail;\n+\t\t}\n+\n+\t\texposure = 1.0s / 4;\n+\t\tif (exposure != 250ms) {\n+\t\t\tcerr << \"utils::Duration failed scalar divide test\";\n+\t\t\treturn TestFail;\n+\t\t}\n+\n+\t\texposure = 5000.5us;\n+\t\tif (!exposure) {\n+\t\t\tcerr << \"utils::Duration failed boolean test\";\n+\t\t\treturn TestFail;\n+\t\t}\n+\n+\t\tos << exposure;\n+\t\tif (os.str() != \"5000.50us\") {\n+\t\t\tcerr << \"utils::Duration operator << failed\";\n+\t\t\treturn TestFail;\n+\t\t}\n+\n+\t\texposure = 100ms;\n+\t\tratio = exposure / 25ms;\n+\t\tif (ratio != 4.0) {\n+\t\t\tcerr << \"utils::Duration failed ratio test\";\n+\t\t\treturn TestFail;\n+\t\t}\n+\n+\t\treturn TestPass;\n+\t}\n+\n \tint run()\n \t{\n \t\t/* utils::hex() test. */\n@@ -236,6 +277,10 @@ protected:\n \t\tif (testEnumerate() != TestPass)\n \t\t\treturn TestFail;\n \n+\t\t/* utils::Duration test. */\n+\t\tif (testDuration() != TestPass)\n+\t\t\treturn TestFail;\n+\n \t\treturn TestPass;\n \t}\n };\n","prefixes":["libcamera-devel","v6","1/4"]}