summaryrefslogtreecommitdiff
path: root/samples/quake/jni/main.cpp
blob: 0df3e602a7ad417c335163f08ddaa156528ae5d9 (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
122
123
124
125
126
127
128
/* //device/apps/Quake/quake/src/QW/client/main.c
**
** Copyright 2007, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
**     http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/

#include <stdio.h>
#include <assert.h>


#include <GLES/gl.h>

#include <utils/Timers.h>

#include <quakedef.h>

#include <android/log.h>
#define LOG_TAG "Quake main"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)


// Timer utilities

#define ENABLE_PMP
#define USE_LOG

#ifdef ENABLE_PMP

static nsecs_t baseTime;
static nsecs_t lastTime;
static const unsigned int kStartTimeStackSize = 100;
static nsecs_t startTimes[kStartTimeStackSize];
static unsigned int startTimeStackPointer;

static
void PMP_Common(const char* fmt, va_list ap, char type)
{
	char buf[1024];
	vsnprintf(buf, sizeof(buf), fmt, ap);
	va_end(ap);

	// Note: Timer acually has less than microsecond resolution, so track time in microseconds:

	nsecs_t time = systemTime(SYSTEM_TIME_THREAD) / 1000;
	if(baseTime == 0)
	{
		baseTime = time;
	}
	time -= baseTime;
	switch(type)
	{
	case '<':
		{
			if(startTimeStackPointer < kStartTimeStackSize)
			{
				startTimes[startTimeStackPointer] = time;
			}
#ifdef USE_LOG
			LOGI("< %lld [%d] %s\n", time, startTimeStackPointer, buf);
#else
			fprintf(stderr, "Quake < %lld %d %s\n", time, startTimeStackPointer, buf);
#endif
			startTimeStackPointer++;
		}
		break;
	case '>':
		{
			nsecs_t elapsed = 0;
			if(startTimeStackPointer > 0)
			{
				--startTimeStackPointer;
				if(startTimeStackPointer < kStartTimeStackSize)
				{
					elapsed = time - startTimes[startTimeStackPointer];
				}
			}
#ifdef USE_LOG
			LOGI("> %lld [%d] %lld %s\n", time, startTimeStackPointer, elapsed, buf);
#else
			fprintf(stderr, "Quake > %lld [%d] %lld %s\n", time, startTimeStackPointer, elapsed, buf);
#endif
		}
		break;
	default:
#ifdef USE_LOG
		LOGI("= %lld %lld %s\n", time, time - lastTime, buf);
#else
		fprintf(stderr, "Quake = %lld %s\n", time, buf);
#endif
		break;
	}
	lastTime = time;
}

void PMP_Begin(const char* fmt,...)
{
	va_list ap;
	va_start(ap, fmt);
	PMP_Common(fmt, ap, '<');
}

void PMP_Event(const char* fmt,...)
{
	va_list ap;
	va_start(ap, fmt);
	PMP_Common(fmt, ap, '=');
}

void PMP_End(const char* fmt,...)
{
	va_list ap;
	va_start(ap, fmt);
	PMP_Common(fmt, ap, '>');
}

#endif // ENABLE_PMP