OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 #include "embedders/openglui/common/timer.h" |
| 6 |
| 7 #define NANO (+1.0E-9) |
| 8 |
| 9 #ifdef __MACH__ |
| 10 #include <mach/mach.h> |
| 11 #include <mach/mach_time.h> |
| 12 #include <sys/time.h> |
| 13 |
| 14 #define GIGA UINT64_C(1000000000) |
| 15 |
| 16 #define CLOCK_REALTIME 0 |
| 17 #define CLOCK_MONOTONIC 1 |
| 18 |
| 19 double timebase = 0.0; |
| 20 uint64_t timestart = 0; |
| 21 |
| 22 void clock_gettime(int type, timespec* t) { |
| 23 if (!timestart) { |
| 24 mach_timebase_info_data_t tb = { 0, 1 }; |
| 25 mach_timebase_info(&tb); |
| 26 timebase = tb.numer; |
| 27 timebase /= tb.denom; |
| 28 timestart = mach_absolute_time(); |
| 29 } |
| 30 if (type == CLOCK_MONOTONIC) { |
| 31 double diff = (mach_absolute_time() - timestart) * timebase; |
| 32 t->tv_sec = diff * NANO; |
| 33 t->tv_nsec = diff - (t->tv_sec * GIGA); |
| 34 } else { // type == CLOCK_REALTIME |
| 35 struct timeval now; |
| 36 gettimeofday(&now, NULL); |
| 37 t->tv_sec = now.tv_sec; |
| 38 t->tv_nsec = now.tv_usec * 1000; |
| 39 } |
| 40 } |
| 41 #endif |
| 42 |
| 43 Timer::Timer() : elapsed_(0.0f), last_time_(0.0) { |
| 44 } |
| 45 |
| 46 void Timer::reset() { |
| 47 elapsed_ = 0.0f; |
| 48 last_time_ = now(); |
| 49 } |
| 50 |
| 51 void Timer::update() { |
| 52 double current = now(); |
| 53 elapsed_ = (current - last_time_); |
| 54 last_time_ = current; |
| 55 } |
| 56 |
| 57 double Timer::now() { |
| 58 timespec timeval; |
| 59 clock_gettime(CLOCK_MONOTONIC, &timeval); |
| 60 return timeval.tv_sec + (timeval.tv_nsec * NANO); |
| 61 } |
| 62 |
| 63 float Timer::elapsed() { |
| 64 return elapsed_; |
| 65 } |
| 66 |
OLD | NEW |