summaryrefslogtreecommitdiff
path: root/Runtime/Profiler/TimeHelper.h
blob: d3e9350304207aeab34c7c62caa92f689fbba125 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#ifndef _TIMEHELPER_H_
#define _TIMEHELPER_H_

// The profiler interprets this value as nanoseconds
typedef UInt64 ProfileTimeFormat;
#define kInvalidProfileTime (~ProfileTimeFormat(0))

#if UNITY_IPHONE || UNITY_OSX

	extern "C" { uint64_t mach_absolute_time(void); }
		
	#define ABSOLUTE_TIME UInt64
	#define ABSOLUTE_TIME_INIT(VAR) VAR = 0u;

	#define START_TIME mach_absolute_time()
	#define ELAPSED_TIME(VAR) (START_TIME - VAR)
	#define COMBINED_TIME(VAR1, VAR2) (VAR1 + VAR2)
	#define SUBTRACTED_TIME(VAR1, VAR2) (VAR1 - VAR2)


	ABSOLUTE_TIME SubtractAbsoluteTimeClamp(ABSOLUTE_TIME lhs, ABSOLUTE_TIME rhs);
	ABSOLUTE_TIME DivideAbsoluteTime(ABSOLUTE_TIME elapsedTime, int divisor);
	inline bool IsEqualAbsoluteTime (ABSOLUTE_TIME lhs, ABSOLUTE_TIME rhs) { return rhs == lhs; }
	inline bool IsSmallerAbsoluteTime (ABSOLUTE_TIME lhs, ABSOLUTE_TIME rhs) { return rhs > lhs; }

	ProfileTimeFormat GetProfileTime(ABSOLUTE_TIME elapsedTime);
	ABSOLUTE_TIME ProfileTimeToAbsoluteTime(ProfileTimeFormat elapsedTime);

#elif UNITY_ANDROID || UNITY_PEPPER || UNITY_LINUX || UNITY_FLASH || UNITY_WEBGL || UNITY_BB10 || UNITY_TIZEN

	#include <sys/time.h>
	
	#define ABSOLUTE_TIME UInt64
	#define ABSOLUTE_TIME_INIT(VAR) VAR = 0ull;

	inline const ABSOLUTE_TIME _StartTime() { timeval time; gettimeofday(&time, 0); return time.tv_usec * 1000ULL + time.tv_sec * 1000000000ULL; }
	#define START_TIME _StartTime()
	#define ELAPSED_TIME(VAR) (START_TIME - VAR)
	#define COMBINED_TIME(VAR1, VAR2) (VAR1 + VAR2)
	#define SUBTRACTED_TIME(VAR1, VAR2) (VAR1 - VAR2)

	ABSOLUTE_TIME SubtractAbsoluteTimeClamp(ABSOLUTE_TIME lhs, ABSOLUTE_TIME rhs);
	ABSOLUTE_TIME DivideAbsoluteTime(ABSOLUTE_TIME elapsedTime, int divisor);
	inline bool IsEqualAbsoluteTime (ABSOLUTE_TIME lhs, ABSOLUTE_TIME rhs) { return rhs == lhs; }
	inline bool IsSmallerAbsoluteTime (ABSOLUTE_TIME lhs, ABSOLUTE_TIME rhs) { return rhs > lhs; }

	ProfileTimeFormat GetProfileTime(ABSOLUTE_TIME elapsedTime);
	ABSOLUTE_TIME ProfileTimeToAbsoluteTime(ProfileTimeFormat elapsedTime);

#elif UNITY_WIN || UNITY_XENON 
	#define ABSOLUTE_TIME UInt64
	#define ABSOLUTE_TIME_INIT(VAR) VAR = 0u;

	#define START_TIME GetStartTime()
	#define ELAPSED_TIME(VAR) GetElapsedTime(VAR)
	#define COMBINED_TIME(VAR1, VAR2) VAR1 + VAR2
	#define SUBTRACTED_TIME(VAR1, VAR2) VAR1 - VAR2
	
	inline ABSOLUTE_TIME SubtractAbsoluteTimeClamp(ABSOLUTE_TIME lhs, ABSOLUTE_TIME rhs) { if (lhs > rhs) return lhs - rhs; else return 0; }
	ABSOLUTE_TIME DivideAbsoluteTime(ABSOLUTE_TIME elapsedTime, int divisor);
	inline bool IsEqualAbsoluteTime (ABSOLUTE_TIME lhs, ABSOLUTE_TIME rhs) { return lhs == rhs; }
	inline bool IsSmallerAbsoluteTime (ABSOLUTE_TIME lhs, ABSOLUTE_TIME rhs) { return lhs < rhs; }
	
	ABSOLUTE_TIME GetStartTime();
	ABSOLUTE_TIME GetElapsedTime(ABSOLUTE_TIME startTime);
	ProfileTimeFormat GetProfileTime(ABSOLUTE_TIME elapsedTime);
	ABSOLUTE_TIME ProfileTimeToAbsoluteTime(ProfileTimeFormat elapsedTime);


#elif UNITY_PS3

	#include <sys/time_util.h>

	#define ABSOLUTE_TIME UInt64
	#define ABSOLUTE_TIME_INIT(VAR) VAR = 0u;

	inline const ABSOLUTE_TIME _StartTime() { ABSOLUTE_TIME tb; SYS_TIMEBASE_GET(tb); return tb; }
	#define START_TIME _StartTime()
	#define ELAPSED_TIME(VAR) START_TIME - VAR
	#define COMBINED_TIME(VAR1, VAR2) VAR1 + VAR2
	#define SUBTRACTED_TIME(VAR1, VAR2) VAR1 - VAR2
	ProfileTimeFormat GetProfileTime(ABSOLUTE_TIME elapsedTime);

	inline ABSOLUTE_TIME SubtractAbsoluteTimeClamp(ABSOLUTE_TIME lhs, ABSOLUTE_TIME rhs) { if (lhs > rhs) return lhs - rhs; else return 0; }
	ABSOLUTE_TIME DivideAbsoluteTime(ABSOLUTE_TIME elapsedTime, int divisor);
	inline bool IsEqualAbsoluteTime (ABSOLUTE_TIME lhs, ABSOLUTE_TIME rhs) { return lhs == rhs; }
	inline bool IsSmallerAbsoluteTime (ABSOLUTE_TIME lhs, ABSOLUTE_TIME rhs) { return lhs < rhs; }
#elif UNITY_WII
	#include <revolution/os.h>
	#define ABSOLUTE_TIME OSTime
	#define ABSOLUTE_TIME_INIT(VAR) VAR = 0u;

	#define START_TIME OSGetTime()
	#define ELAPSED_TIME(VAR) GetElapsedTime(VAR)
	#define COMBINED_TIME(VAR1, VAR2) VAR1 + VAR2
	#define SUBTRACTED_TIME(VAR1, VAR2) VAR1 - VAR2
	
	inline ABSOLUTE_TIME SubtractAbsoluteTimeClamp(ABSOLUTE_TIME lhs, ABSOLUTE_TIME rhs) { if (lhs > rhs) return lhs - rhs; else return 0; }
	ABSOLUTE_TIME DivideAbsoluteTime(ABSOLUTE_TIME elapsedTime, int divisor);
	inline bool IsEqualAbsoluteTime (ABSOLUTE_TIME lhs, ABSOLUTE_TIME rhs) { return lhs == rhs; }
	inline bool IsSmallerAbsoluteTime (ABSOLUTE_TIME lhs, ABSOLUTE_TIME rhs) { return lhs < rhs; }
	
	ABSOLUTE_TIME GetStartTime();
	ABSOLUTE_TIME GetElapsedTime(ABSOLUTE_TIME startTime);
	ProfileTimeFormat GetProfileTime(ABSOLUTE_TIME elapsedTime);
	ABSOLUTE_TIME ProfileTimeToAbsoluteTime(ProfileTimeFormat elapsedTime);

#else
	#error IMPLEMENT ME
#endif

inline float ProfileTimeToSeconds (ProfileTimeFormat elapsedTime) { return elapsedTime * 0.000000001; }
inline float AbsoluteTimeToSeconds (ABSOLUTE_TIME absoluteTime) { return ProfileTimeToSeconds(GetProfileTime(absoluteTime)); }
inline float GetElapsedTimeInSeconds (ABSOLUTE_TIME elapsedTime) { return ProfileTimeToSeconds(GetProfileTime(ELAPSED_TIME(elapsedTime))); }

inline float AbsoluteTimeToMilliseconds (ABSOLUTE_TIME time)
{
	return AbsoluteTimeToSeconds(time) * 1000.0F;
}

#endif /*_TIMEHELPER_H_*/