1 /* //device/apps/Quake/quake/src/QW/client/main.c 2 ** 3 ** Copyright 2007, The Android Open Source Project 4 ** 5 ** Licensed under the Apache License, Version 2.0 (the "License"); 6 ** you may not use this file except in compliance with the License. 7 ** You may obtain a copy of the License at 8 ** 9 ** http://www.apache.org/licenses/LICENSE-2.0 10 ** 11 ** Unless required by applicable law or agreed to in writing, software 12 ** distributed under the License is distributed on an "AS IS" BASIS, 13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 ** See the License for the specific language governing permissions and 15 ** limitations under the License. 16 */ 17 18 #include <stdio.h> 19 #include <assert.h> 20 21 #define LOG_TAG "Quake" 22 23 #include <GLES/gl.h> 24 #include <utils/Log.h> 25 #include <utils/Timers.h> 26 27 #include <quakedef.h> 28 29 // Timer utilities 30 31 #define ENABLE_PMP 32 #define USE_LOG 33 34 #ifdef ENABLE_PMP 35 36 static nsecs_t baseTime; 37 static nsecs_t lastTime; 38 static const unsigned int kStartTimeStackSize = 100; 39 static nsecs_t startTimes[kStartTimeStackSize]; 40 static unsigned int startTimeStackPointer; 41 42 static 43 void PMP_Common(const char* fmt, va_list ap, char type) 44 { 45 char buf[1024]; 46 vsnprintf(buf, sizeof(buf), fmt, ap); 47 va_end(ap); 48 49 // Note: Timer acually has less than microsecond resolution, so track time in microseconds: 50 51 nsecs_t time = systemTime(SYSTEM_TIME_THREAD) / 1000; 52 if(baseTime == 0) 53 { 54 baseTime = time; 55 } 56 time -= baseTime; 57 switch(type) 58 { 59 case '<': 60 { 61 if(startTimeStackPointer < kStartTimeStackSize) 62 { 63 startTimes[startTimeStackPointer] = time; 64 } 65 #ifdef USE_LOG 66 ALOGI("< %lld [%d] %s\n", time, startTimeStackPointer, buf); 67 #else 68 fprintf(stderr, "Quake < %lld %d %s\n", time, startTimeStackPointer, buf); 69 #endif 70 startTimeStackPointer++; 71 } 72 break; 73 case '>': 74 { 75 nsecs_t elapsed = 0; 76 if(startTimeStackPointer > 0) 77 { 78 --startTimeStackPointer; 79 if(startTimeStackPointer < kStartTimeStackSize) 80 { 81 elapsed = time - startTimes[startTimeStackPointer]; 82 } 83 } 84 #ifdef USE_LOG 85 ALOGI("> %lld [%d] %lld %s\n", time, startTimeStackPointer, elapsed, buf); 86 #else 87 fprintf(stderr, "Quake > %lld [%d] %lld %s\n", time, startTimeStackPointer, elapsed, buf); 88 #endif 89 } 90 break; 91 default: 92 #ifdef USE_LOG 93 ALOGI("= %lld %lld %s\n", time, time - lastTime, buf); 94 #else 95 fprintf(stderr, "Quake = %lld %s\n", time, buf); 96 #endif 97 break; 98 } 99 lastTime = time; 100 } 101 102 void PMP_Begin(const char* fmt,...) 103 { 104 va_list ap; 105 va_start(ap, fmt); 106 PMP_Common(fmt, ap, '<'); 107 } 108 109 void PMP_Event(const char* fmt,...) 110 { 111 va_list ap; 112 va_start(ap, fmt); 113 PMP_Common(fmt, ap, '='); 114 } 115 116 void PMP_End(const char* fmt,...) 117 { 118 va_list ap; 119 va_start(ap, fmt); 120 PMP_Common(fmt, ap, '>'); 121 } 122 123 #endif // ENABLE_PMP 124