[libcamera-devel] [PATCH v6 1/4] libcamera: utils: Add helper class for std::chrono::duration

Naushir Patuck naush at raspberrypi.com
Tue Jun 8 13:03:32 CEST 2021


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 defined to help with displaying utils::Duration value
in stream objects. Currently, this will display the duration value in
microseconds.

Signed-off-by: Naushir Patuck <naush at raspberrypi.com>
Reviewed-by: David Plowman <david.plowman at raspberrypi.com>
Reviewed-by: Jacopo Mondi <jacopo at jmondi.org>
Reviewed-by: Laurent Pinchart <laurent.pinchart at ideasonboard.com>
---
 include/libcamera/internal/utils.h | 32 +++++++++++++++++
 src/libcamera/utils.cpp            | 58 ++++++++++++++++++++++++++++++
 test/utils.cpp                     | 45 +++++++++++++++++++++++
 3 files changed, 135 insertions(+)

diff --git a/include/libcamera/internal/utils.h b/include/libcamera/internal/utils.h
index 83dada7cc16c..15beb0f44172 100644
--- a/include/libcamera/internal/utils.h
+++ b/include/libcamera/internal/utils.h
@@ -316,8 +316,40 @@ auto enumerate(T (&iterable)[N]) -> details::enumerate_adapter<T *>
 }
 #endif
 
+class Duration : public std::chrono::duration<double, std::nano>
+{
+	using BaseDuration = std::chrono::duration<double, std::nano>;
+
+public:
+	Duration() = default;
+
+	template<typename Rep, typename Period>
+	constexpr Duration(const std::chrono::duration<Rep, Period> &d)
+		: BaseDuration(d)
+	{
+	}
+
+	template<typename Period>
+	double get() const
+	{
+		auto const c = std::chrono::duration_cast<std::chrono::duration<double, Period>>(*this);
+		return c.count();
+	}
+
+	explicit constexpr operator bool() const
+	{
+		return *this != BaseDuration::zero();
+	}
+};
+
 } /* namespace utils */
 
+#ifndef __DOXYGEN__
+template<class CharT, class Traits>
+std::basic_ostream<CharT, Traits> &operator<<(std::basic_ostream<CharT, Traits> &os,
+					      const utils::Duration &d);
+#endif
+
 } /* namespace libcamera */
 
 #endif /* __LIBCAMERA_INTERNAL_UTILS_H__ */
diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp
index 826144d3c837..2e7d35fb1173 100644
--- a/src/libcamera/utils.cpp
+++ b/src/libcamera/utils.cpp
@@ -506,6 +506,64 @@ 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<Rep, Period> &d)
+ * \brief Construct a Duration by converting an arbitrary std::chrono::duration
+ * \param[in] d The std::chrono::duration object to convert from
+ *
+ * The constructed \a Duration object is internally represented in double
+ * precision with nanoseconds ticks.
+ */
+
+/**
+ * \fn Duration::get<Period>()
+ * \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<std::milli>();
+ * \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 */
 
+#ifndef __DOXYGEN__
+template<class CharT, class Traits>
+std::basic_ostream<CharT, Traits> &operator<<(std::basic_ostream<CharT, Traits> &os,
+					      const utils::Duration &d)
+{
+	std::basic_ostringstream<CharT, Traits> 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<std::micro>() << "us";
+	return os << s.str();
+}
+
+template
+std::basic_ostream<char, std::char_traits<char>> &
+operator<< <char, std::char_traits<char>>(std::basic_ostream<char, std::char_traits<char>> &os,
+					  const utils::Duration &d);
+#endif
+
 } /* namespace libcamera */
diff --git a/test/utils.cpp b/test/utils.cpp
index 7e24c71e4775..f170ae4c2f35 100644
--- a/test/utils.cpp
+++ b/test/utils.cpp
@@ -20,6 +20,7 @@
 
 using namespace std;
 using namespace libcamera;
+using namespace std::literals::chrono_literals;
 
 class UtilsTest : public Test
 {
@@ -128,6 +129,46 @@ protected:
 		return TestPass;
 	}
 
+	int testDuration()
+	{
+		std::ostringstream os;
+		utils::Duration exposure;
+		double ratio;
+
+		exposure = 25ms + 25ms;
+		if (exposure.get<std::micro>() != 50000.0) {
+			cerr << "utils::Duration failed to return microsecond count";
+			return TestFail;
+		}
+
+		exposure = 1.0s / 4;
+		if (exposure != 250ms) {
+			cerr << "utils::Duration failed scalar divide test";
+			return TestFail;
+		}
+
+		exposure = 5000.5us;
+		if (!exposure) {
+			cerr << "utils::Duration failed boolean test";
+			return TestFail;
+		}
+
+		os << exposure;
+		if (os.str() != "5000.50us") {
+			cerr << "utils::Duration operator << failed";
+			return TestFail;
+		}
+
+		exposure = 100ms;
+		ratio = exposure / 25ms;
+		if (ratio != 4.0) {
+			cerr << "utils::Duration failed ratio test";
+			return TestFail;
+		}
+
+		return TestPass;
+	}
+
 	int run()
 	{
 		/* utils::hex() test. */
@@ -236,6 +277,10 @@ protected:
 		if (testEnumerate() != TestPass)
 			return TestFail;
 
+		/* utils::Duration test. */
+		if (testDuration() != TestPass)
+			return TestFail;
+
 		return TestPass;
 	}
 };
-- 
2.25.1



More information about the libcamera-devel mailing list