| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield |
|---|
| 2 | * |
|---|
| 3 | * This library is open source and may be redistributed and/or modified under |
|---|
| 4 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
|---|
| 5 | * (at your option) any later version. The full license is in LICENSE file |
|---|
| 6 | * included with this distribution, and on the openscenegraph.org website. |
|---|
| 7 | * |
|---|
| 8 | * This library is distributed in the hope that it will be useful, |
|---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | * OpenSceneGraph Public License for more details. |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | #ifndef OSG_TIMER |
|---|
| 15 | #define OSG_TIMER 1 |
|---|
| 16 | |
|---|
| 17 | #include <osg/Export> |
|---|
| 18 | |
|---|
| 19 | namespace osg { |
|---|
| 20 | |
|---|
| 21 | #if defined(_MSC_VER) |
|---|
| 22 | typedef __int64 Timer_t; |
|---|
| 23 | #else |
|---|
| 24 | typedef unsigned long long Timer_t; |
|---|
| 25 | #endif |
|---|
| 26 | |
|---|
| 27 | /** Timer class is used for measuring elapsed time or time between two points. */ |
|---|
| 28 | class OSG_EXPORT Timer { |
|---|
| 29 | |
|---|
| 30 | public: |
|---|
| 31 | |
|---|
| 32 | Timer(); |
|---|
| 33 | ~Timer() {} |
|---|
| 34 | |
|---|
| 35 | static Timer* instance(); |
|---|
| 36 | |
|---|
| 37 | /** Get the timers tick value.*/ |
|---|
| 38 | Timer_t tick() const; |
|---|
| 39 | |
|---|
| 40 | /** Set the start.*/ |
|---|
| 41 | void setStartTick() { _startTick = tick(); } |
|---|
| 42 | void setStartTick(Timer_t t) { _startTick = t; } |
|---|
| 43 | Timer_t getStartTick() const { return _startTick; } |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | /** Get elapsed time in seconds.*/ |
|---|
| 47 | inline double time_s() const { return delta_s(_startTick, tick()); } |
|---|
| 48 | |
|---|
| 49 | /** Get elapsed time in milliseconds.*/ |
|---|
| 50 | inline double time_m() const { return delta_m(_startTick, tick()); } |
|---|
| 51 | |
|---|
| 52 | /** Get elapsed time in micoseconds.*/ |
|---|
| 53 | inline double time_u() const { return delta_u(_startTick, tick()); } |
|---|
| 54 | |
|---|
| 55 | /** Get elapsed time in nanoseconds.*/ |
|---|
| 56 | inline double time_n() const { return delta_n(_startTick, tick()); } |
|---|
| 57 | |
|---|
| 58 | /** Get the time in seconds between timer ticks t1 and t2.*/ |
|---|
| 59 | inline double delta_s( Timer_t t1, Timer_t t2 ) const { return (double)(t2 - t1)*_secsPerTick; } |
|---|
| 60 | |
|---|
| 61 | /** Get the time in milliseconds between timer ticks t1 and t2.*/ |
|---|
| 62 | inline double delta_m( Timer_t t1, Timer_t t2 ) const { return delta_s(t1,t2)*1e3; } |
|---|
| 63 | |
|---|
| 64 | /** Get the time in microseconds between timer ticks t1 and t2.*/ |
|---|
| 65 | inline double delta_u( Timer_t t1, Timer_t t2 ) const { return delta_s(t1,t2)*1e6; } |
|---|
| 66 | |
|---|
| 67 | /** Get the time in nanoseconds between timer ticks t1 and t2.*/ |
|---|
| 68 | inline double delta_n( Timer_t t1, Timer_t t2 ) const { return delta_s(t1,t2)*1e9; } |
|---|
| 69 | |
|---|
| 70 | /** Get the the number of seconds per tick. */ |
|---|
| 71 | inline double getSecondsPerTick() const { return _secsPerTick; } |
|---|
| 72 | |
|---|
| 73 | protected : |
|---|
| 74 | |
|---|
| 75 | Timer_t _startTick; |
|---|
| 76 | double _secsPerTick; |
|---|
| 77 | }; |
|---|
| 78 | |
|---|
| 79 | /** Helper class for timing sections of code. */ |
|---|
| 80 | class ElapsedTime |
|---|
| 81 | { |
|---|
| 82 | public: |
|---|
| 83 | inline ElapsedTime(double* elapsedTime, osg::Timer* timer = 0): |
|---|
| 84 | _time(elapsedTime) |
|---|
| 85 | { |
|---|
| 86 | init(timer); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | inline ElapsedTime(osg::Timer* timer = 0): |
|---|
| 90 | _time(0) |
|---|
| 91 | { |
|---|
| 92 | init(timer); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | inline ~ElapsedTime() |
|---|
| 96 | { |
|---|
| 97 | finish(); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | inline void reset() |
|---|
| 101 | { |
|---|
| 102 | _startTick = _timer->tick(); |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | /** elapsed time in seconds. */ |
|---|
| 106 | inline double elapsedTime() const |
|---|
| 107 | { |
|---|
| 108 | return _timer->delta_s(_startTick, _timer->tick()); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | /** elapsed time in milliseconds. */ |
|---|
| 112 | inline double elapsedTime_m() const |
|---|
| 113 | { |
|---|
| 114 | return _timer->delta_m(_startTick, _timer->tick()); |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | /** elapsed time in microseconds. */ |
|---|
| 118 | inline double elapsedTime_u() const |
|---|
| 119 | { |
|---|
| 120 | return _timer->delta_u(_startTick, _timer->tick()); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | /** elapsed time in nanoseconds. */ |
|---|
| 124 | inline double elapsedTime_n() const |
|---|
| 125 | { |
|---|
| 126 | return _timer->delta_n(_startTick, _timer->tick()); |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | inline void finish() |
|---|
| 130 | { |
|---|
| 131 | Timer_t endTick = _timer->tick(); |
|---|
| 132 | if (_time) *_time += _timer->delta_s(_startTick, endTick); |
|---|
| 133 | _startTick = endTick; |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | protected: |
|---|
| 137 | |
|---|
| 138 | inline void init(osg::Timer* timer) |
|---|
| 139 | { |
|---|
| 140 | if (timer) _timer = timer; |
|---|
| 141 | else _timer = osg::Timer::instance(); |
|---|
| 142 | |
|---|
| 143 | _startTick = _timer->tick(); |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | double* _time; |
|---|
| 147 | Timer* _timer; |
|---|
| 148 | Timer_t _startTick; |
|---|
| 149 | |
|---|
| 150 | }; |
|---|
| 151 | |
|---|
| 152 | } |
|---|
| 153 | #endif |
|---|