diff --git a/src/xrt/auxiliary/util/u_time.cpp b/src/xrt/auxiliary/util/u_time.cpp index acfdd2771..27a4b7d87 100644 --- a/src/xrt/auxiliary/util/u_time.cpp +++ b/src/xrt/auxiliary/util/u_time.cpp @@ -115,3 +115,27 @@ time_state_ts_to_monotonic_ns(struct time_state const *state, timepoint_ns times return timestamp + state->offset; } + +#ifdef XRT_OS_WINDOWS +extern "C" void +time_state_to_win32perfcounter(struct time_state const *state, timepoint_ns timestamp, LARGE_INTEGER *out_qpc_ticks) +{ + assert(state != NULL); + assert(out_qpc_ticks != NULL); + + uint64_t ns = time_state_ts_to_monotonic_ns(state, timestamp); + + out_qpc_ticks->QuadPart = ns / os_ns_per_qpc_tick_get(); +} + +extern "C" timepoint_ns +time_state_from_win32perfcounter(struct time_state const *state, const LARGE_INTEGER *qpc_ticks) +{ + assert(state != NULL); + assert(qpc_ticks != NULL); + + uint64_t ns = qpc_ticks->QuadPart * os_ns_per_qpc_tick_get(); + + return time_state_monotonic_to_ts_ns(state, ns); +} +#endif // XRT_OS_WINDOWS diff --git a/src/xrt/auxiliary/util/u_time.h b/src/xrt/auxiliary/util/u_time.h index f2a082cd3..b6007756c 100644 --- a/src/xrt/auxiliary/util/u_time.h +++ b/src/xrt/auxiliary/util/u_time.h @@ -17,9 +17,12 @@ #pragma once +#include "xrt/xrt_compiler.h" + #include #include + #ifdef __cplusplus extern "C" { #endif @@ -285,6 +288,30 @@ time_state_monotonic_to_ts_ns(struct time_state const *state, uint64_t monotonic uint64_t time_state_ts_to_monotonic_ns(struct time_state const *state, timepoint_ns timestamp); +#if defined(XRT_OS_WINDOWS) || defined(XRT_DOXYGEN) +/*! + * Converts a timestamp to Win32 "QPC" ticks. + * + * Should not be called simultaneously with time_state_get_now_and_update. + * + * @public @memberof time_state + * @ingroup aux_util + */ +void +time_state_to_win32perfcounter(struct time_state const *state, timepoint_ns timestamp, LARGE_INTEGER *out_qpc_ticks); + +/*! + * Converts from Win32 "QPC" ticks to timestamp. + * + * Should not be called simultaneously with time_state_get_now_and_update. + * + * @public @memberof time_state + * @ingroup aux_util + */ +timepoint_ns +time_state_from_win32perfcounter(struct time_state const *state, const LARGE_INTEGER *qpc_ticks); +#endif // defined(XRT_OS_WINDOWS) || defined(XRT_DOXYGEN) + #ifdef __cplusplus }