Home | History | Annotate | Download | only in jni
      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 
     22 #include <GLES/gl.h>
     23 
     24 #include <utils/Timers.h>
     25 
     26 #include <quakedef.h>
     27 
     28 #include <android/log.h>
     29 #define LOG_TAG "Quake main"
     30 #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
     31 #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
     32 
     33 
     34 // Timer utilities
     35 
     36 #define ENABLE_PMP
     37 #define USE_LOG
     38 
     39 #ifdef ENABLE_PMP
     40 
     41 static nsecs_t baseTime;
     42 static nsecs_t lastTime;
     43 static const unsigned int kStartTimeStackSize = 100;
     44 static nsecs_t startTimes[kStartTimeStackSize];
     45 static unsigned int startTimeStackPointer;
     46 
     47 static
     48 void PMP_Common(const char* fmt, va_list ap, char type)
     49 {
     50 	char buf[1024];
     51 	vsnprintf(buf, sizeof(buf), fmt, ap);
     52 	va_end(ap);
     53 
     54 	// Note: Timer acually has less than microsecond resolution, so track time in microseconds:
     55 
     56 	nsecs_t time = systemTime(SYSTEM_TIME_THREAD) / 1000;
     57 	if(baseTime == 0)
     58 	{
     59 		baseTime = time;
     60 	}
     61 	time -= baseTime;
     62 	switch(type)
     63 	{
     64 	case '<':
     65 		{
     66 			if(startTimeStackPointer < kStartTimeStackSize)
     67 			{
     68 				startTimes[startTimeStackPointer] = time;
     69 			}
     70 #ifdef USE_LOG
     71 			LOGI("< %lld [%d] %s\n", time, startTimeStackPointer, buf);
     72 #else
     73 			fprintf(stderr, "Quake < %lld %d %s\n", time, startTimeStackPointer, buf);
     74 #endif
     75 			startTimeStackPointer++;
     76 		}
     77 		break;
     78 	case '>':
     79 		{
     80 			nsecs_t elapsed = 0;
     81 			if(startTimeStackPointer > 0)
     82 			{
     83 				--startTimeStackPointer;
     84 				if(startTimeStackPointer < kStartTimeStackSize)
     85 				{
     86 					elapsed = time - startTimes[startTimeStackPointer];
     87 				}
     88 			}
     89 #ifdef USE_LOG
     90 			LOGI("> %lld [%d] %lld %s\n", time, startTimeStackPointer, elapsed, buf);
     91 #else
     92 			fprintf(stderr, "Quake > %lld [%d] %lld %s\n", time, startTimeStackPointer, elapsed, buf);
     93 #endif
     94 		}
     95 		break;
     96 	default:
     97 #ifdef USE_LOG
     98 		LOGI("= %lld %lld %s\n", time, time - lastTime, buf);
     99 #else
    100 		fprintf(stderr, "Quake = %lld %s\n", time, buf);
    101 #endif
    102 		break;
    103 	}
    104 	lastTime = time;
    105 }
    106 
    107 void PMP_Begin(const char* fmt,...)
    108 {
    109 	va_list ap;
    110 	va_start(ap, fmt);
    111 	PMP_Common(fmt, ap, '<');
    112 }
    113 
    114 void PMP_Event(const char* fmt,...)
    115 {
    116 	va_list ap;
    117 	va_start(ap, fmt);
    118 	PMP_Common(fmt, ap, '=');
    119 }
    120 
    121 void PMP_End(const char* fmt,...)
    122 {
    123 	va_list ap;
    124 	va_start(ap, fmt);
    125 	PMP_Common(fmt, ap, '>');
    126 }
    127 
    128 #endif // ENABLE_PMP
    129