mirror of
https://gitlab.freedesktop.org/monado/monado.git
synced 2025-01-01 04:36:07 +00:00
aux/util: Provide a conversion function from CLOCK_MONOTONIC nanoseconds to adjusted time.
This commit is contained in:
parent
227e53d030
commit
fca21e83b0
|
@ -1,4 +1,4 @@
|
|||
// Copyright 2019, Collabora, Ltd.
|
||||
// Copyright 2019-2020, Collabora, Ltd.
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
/*!
|
||||
* @file
|
||||
|
@ -8,20 +8,37 @@
|
|||
*/
|
||||
|
||||
#include "u_time.h"
|
||||
#include "xrt/xrt_config.h"
|
||||
#include "xrt/xrt_compiler.h"
|
||||
|
||||
#include <chrono>
|
||||
#include <new>
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#ifdef XRT_OS_LINUX
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
|
||||
using namespace std::chrono;
|
||||
|
||||
struct MatchingTimePoints
|
||||
{
|
||||
MatchingTimePoints()
|
||||
{
|
||||
#ifdef XRT_OS_LINUX
|
||||
clock_gettime(CLOCK_MONOTONIC, &clock_monotonic);
|
||||
#endif
|
||||
}
|
||||
system_clock::time_point sys = system_clock::now();
|
||||
steady_clock::time_point steady = steady_clock::now();
|
||||
// high_resolution_clock::time_point highRes;
|
||||
|
||||
#ifdef XRT_OS_LINUX
|
||||
struct timespec clock_monotonic;
|
||||
#endif
|
||||
|
||||
timepoint_ns
|
||||
getTimestamp(time_state const &prevState);
|
||||
};
|
||||
|
@ -134,3 +151,19 @@ time_state_from_timespec(struct time_state const *state,
|
|||
return state->lastTime +
|
||||
duration_cast<nanoseconds>(sinceLastUpdate).count();
|
||||
}
|
||||
|
||||
|
||||
timepoint_ns
|
||||
time_state_from_monotonic_ns(struct time_state const *state,
|
||||
uint64_t monotonic_ns)
|
||||
{
|
||||
assert(state != NULL);
|
||||
auto sinceLastUpdate =
|
||||
seconds{state->lastTimePoints.clock_monotonic.tv_sec} +
|
||||
nanoseconds{state->lastTimePoints.clock_monotonic.tv_nsec} -
|
||||
nanoseconds{monotonic_ns};
|
||||
|
||||
// Offset the last timestamp by that duration.
|
||||
return state->lastTime +
|
||||
duration_cast<nanoseconds>(sinceLastUpdate).count();
|
||||
}
|
||||
|
|
|
@ -1,9 +1,14 @@
|
|||
// Copyright 2019, Collabora, Ltd.
|
||||
// Copyright 2019-2020, Collabora, Ltd.
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
/*!
|
||||
* @file
|
||||
* @brief Time-keeping: a clock that is steady, convertible to system time, and
|
||||
* ideally high-resolution.
|
||||
*
|
||||
* Designed to suit the needs of OpenXR: you can and should use something
|
||||
* simpler (like @ref aux_os_time) for most purposes that aren't in OpenXR
|
||||
* interface code.
|
||||
*
|
||||
* @author Ryan Pavlik <ryan.pavlik@collabora.com>
|
||||
* @ingroup aux_util
|
||||
*
|
||||
|
@ -146,6 +151,21 @@ timepoint_ns
|
|||
time_state_from_timespec(struct time_state const *state,
|
||||
const struct timespec *timespecTime);
|
||||
|
||||
/*!
|
||||
* Convert a monotonic system time (such as from @ref aux_os_time) to an
|
||||
* adjusted integer timestamp.
|
||||
*
|
||||
* Adjustments may need to be applied to achieve the other guarantees that e.g.
|
||||
* CLOCK_MONOTONIC does not provide: this function performs those adjustments.
|
||||
*
|
||||
* Should not be called simultaneously with time_state_get_now_and_update.
|
||||
*
|
||||
* @public @memberof time_state
|
||||
* @ingroup aux_util
|
||||
*/
|
||||
timepoint_ns
|
||||
time_state_from_monotonic_ns(struct time_state const *state,
|
||||
uint64_t monotonic_ns);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue