1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 package com.android.cts.opengl.primitive; 15 16 import android.app.Activity; 17 import android.content.Intent; 18 import android.cts.util.WatchDog; 19 import android.os.Bundle; 20 import android.util.Log; 21 import android.view.SurfaceHolder; 22 import android.view.SurfaceView; 23 import android.view.Surface; 24 25 import com.android.cts.opengl.GLActivityIntentKeys; 26 27 import java.util.concurrent.CountDownLatch; 28 import java.util.concurrent.Semaphore; 29 30 public class GLPrimitiveActivity extends Activity { 31 32 public final static String TAG = "GLPrimitiveActivity"; 33 34 private volatile Exception mException; 35 private volatile Surface mSurface = null; 36 private CountDownLatch mStartSignal = new CountDownLatch(1); 37 private Semaphore mSemaphore = new Semaphore(0); 38 39 private BenchmarkName mBenchmark; 40 private boolean mOffscreen; 41 private int mNumFrames; 42 private int mNumIterations; 43 private int mTimeout; 44 public double[] mFpsValues; 45 46 @Override onCreate(Bundle data)47 public void onCreate(Bundle data) { 48 super.onCreate(data); 49 System.loadLibrary("ctsopengl_jni"); 50 Intent intent = getIntent(); 51 mBenchmark = BenchmarkName.valueOf( 52 intent.getStringExtra(GLActivityIntentKeys.INTENT_EXTRA_BENCHMARK_NAME)); 53 mOffscreen = intent.getBooleanExtra(GLActivityIntentKeys.INTENT_EXTRA_OFFSCREEN, false); 54 mNumFrames = intent.getIntExtra(GLActivityIntentKeys.INTENT_EXTRA_NUM_FRAMES, 0); 55 mNumIterations = intent.getIntExtra(GLActivityIntentKeys.INTENT_EXTRA_NUM_ITERATIONS, 0); 56 mTimeout = intent.getIntExtra(GLActivityIntentKeys.INTENT_EXTRA_TIMEOUT, 0); 57 mFpsValues = new double[mNumIterations]; 58 59 Log.i(TAG, "Benchmark: " + mBenchmark); 60 Log.i(TAG, "Offscreen: " + mOffscreen); 61 Log.i(TAG, "Num Frames: " + mNumFrames); 62 Log.i(TAG, "Num Iterations: " + mNumIterations); 63 Log.i(TAG, "Time Out: " + mTimeout); 64 65 SurfaceView surfaceView = new SurfaceView(this); 66 surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() { 67 @Override 68 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 69 mSurface = holder.getSurface(); 70 mStartSignal.countDown(); 71 } 72 73 @Override 74 public void surfaceCreated(SurfaceHolder holder) {} 75 76 @Override 77 public void surfaceDestroyed(SurfaceHolder holder) {} 78 }); 79 setContentView(surfaceView); 80 // Spawns a worker to run the benchmark. 81 Worker worker = new Worker(); 82 worker.start(); 83 } 84 waitForCompletion()85 public void waitForCompletion() throws Exception { 86 // Wait for semiphore. 87 mSemaphore.acquire(); 88 if (mException != null) { 89 throw mException; 90 } 91 } 92 complete()93 private void complete() { 94 // Release semiphore. 95 mSemaphore.release(); 96 finish(); 97 } 98 setException(Exception e)99 private synchronized void setException(Exception e) { 100 if (mException == null) { 101 mException = e; 102 } 103 } 104 setupFullPipelineBenchmark( Surface surface, boolean offscreen, int workload)105 private static native void setupFullPipelineBenchmark( 106 Surface surface, boolean offscreen, int workload); 107 setupPixelOutputBenchmark( Surface surface, boolean offscreen, int workload)108 private static native void setupPixelOutputBenchmark( 109 Surface surface, boolean offscreen, int workload); 110 setupShaderPerfBenchmark( Surface surface, boolean offscreen, int workload)111 private static native void setupShaderPerfBenchmark( 112 Surface surface, boolean offscreen, int workload); 113 setupContextSwitchBenchmark( Surface surface, boolean offscreen, int workload)114 private static native void setupContextSwitchBenchmark( 115 Surface surface, boolean offscreen, int workload); 116 startBenchmark(int numFrames, double[] frameTimes)117 private static native boolean startBenchmark(int numFrames, double[] frameTimes); 118 119 /** 120 * This thread runs the benchmarks, freeing the UI thread. 121 */ 122 private class Worker extends Thread implements WatchDog.TimeoutCallback { 123 124 private WatchDog watchDog; 125 private volatile boolean success = true; 126 127 @Override run()128 public void run() { 129 try { 130 mStartSignal.await(); 131 } catch (InterruptedException e) { 132 setException(e); 133 complete(); 134 return; 135 } 136 Log.i(TAG, mBenchmark + " Benchmark Started"); 137 // Creates a watchdog to ensure a iteration doesn't exceed the timeout. 138 watchDog = new WatchDog(mTimeout, this); 139 // Used to record the start and end time of the iteration. 140 double[] times = new double[2]; 141 for (int i = 0; i < mNumIterations && success; i++) { 142 // The workload to use for this iteration. 143 int workload = i + 1; 144 // Setup the benchmark. 145 switch (mBenchmark) { 146 case FullPipeline: 147 setupFullPipelineBenchmark(mSurface, mOffscreen, workload); 148 break; 149 case PixelOutput: 150 setupPixelOutputBenchmark(mSurface, mOffscreen, workload); 151 break; 152 case ShaderPerf: 153 setupShaderPerfBenchmark(mSurface, mOffscreen, workload); 154 break; 155 case ContextSwitch: 156 setupContextSwitchBenchmark(mSurface, mOffscreen, workload); 157 break; 158 } 159 watchDog.start(); 160 // Start benchmark. 161 success = startBenchmark(mNumFrames, times); 162 watchDog.stop(); 163 164 if (!success) { 165 setException(new Exception("Benchmark failed to run")); 166 } else { 167 // Calculate FPS. 168 mFpsValues[i] = mNumFrames * 1000.0f / (times[1] - times[0]); 169 } 170 } 171 complete(); 172 Log.i(TAG, mBenchmark + " Benchmark Completed"); 173 } 174 onTimeout()175 public void onTimeout() { 176 setException(new Exception("Benchmark timed out")); 177 complete(); 178 } 179 180 } 181 } 182