mirror of
https://gitlab.freedesktop.org/monado/monado.git
synced 2025-01-04 06:06:17 +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
|
// SPDX-License-Identifier: BSL-1.0
|
||||||
/*!
|
/*!
|
||||||
* @file
|
* @file
|
||||||
|
@ -8,20 +8,37 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "u_time.h"
|
#include "u_time.h"
|
||||||
|
#include "xrt/xrt_config.h"
|
||||||
|
#include "xrt/xrt_compiler.h"
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <new>
|
#include <new>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
#ifdef XRT_OS_LINUX
|
||||||
|
#include <sys/time.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
using namespace std::chrono;
|
using namespace std::chrono;
|
||||||
|
|
||||||
struct MatchingTimePoints
|
struct MatchingTimePoints
|
||||||
{
|
{
|
||||||
|
MatchingTimePoints()
|
||||||
|
{
|
||||||
|
#ifdef XRT_OS_LINUX
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &clock_monotonic);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
system_clock::time_point sys = system_clock::now();
|
system_clock::time_point sys = system_clock::now();
|
||||||
steady_clock::time_point steady = steady_clock::now();
|
steady_clock::time_point steady = steady_clock::now();
|
||||||
// high_resolution_clock::time_point highRes;
|
// high_resolution_clock::time_point highRes;
|
||||||
|
|
||||||
|
#ifdef XRT_OS_LINUX
|
||||||
|
struct timespec clock_monotonic;
|
||||||
|
#endif
|
||||||
|
|
||||||
timepoint_ns
|
timepoint_ns
|
||||||
getTimestamp(time_state const &prevState);
|
getTimestamp(time_state const &prevState);
|
||||||
};
|
};
|
||||||
|
@ -134,3 +151,19 @@ time_state_from_timespec(struct time_state const *state,
|
||||||
return state->lastTime +
|
return state->lastTime +
|
||||||
duration_cast<nanoseconds>(sinceLastUpdate).count();
|
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
|
// SPDX-License-Identifier: BSL-1.0
|
||||||
/*!
|
/*!
|
||||||
* @file
|
* @file
|
||||||
* @brief Time-keeping: a clock that is steady, convertible to system time, and
|
* @brief Time-keeping: a clock that is steady, convertible to system time, and
|
||||||
* ideally high-resolution.
|
* 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>
|
* @author Ryan Pavlik <ryan.pavlik@collabora.com>
|
||||||
* @ingroup aux_util
|
* @ingroup aux_util
|
||||||
*
|
*
|
||||||
|
@ -146,6 +151,21 @@ timepoint_ns
|
||||||
time_state_from_timespec(struct time_state const *state,
|
time_state_from_timespec(struct time_state const *state,
|
||||||
const struct timespec *timespecTime);
|
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
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue