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 android.opengl2.cts.primitive; 15 16 import android.content.Intent; 17 import android.opengl2.cts.GLActivityIntentKeys; 18 import android.test.ActivityInstrumentationTestCase2; 19 20 import com.android.compatibility.common.util.DeviceReportLog; 21 import com.android.compatibility.common.util.ResultType; 22 import com.android.compatibility.common.util.ResultUnit; 23 24 /** 25 * Runs the Primitive OpenGL ES 2.0 Benchmarks. 26 */ 27 public class GLPrimitiveBenchmark extends ActivityInstrumentationTestCase2<GLPrimitiveActivity> { 28 29 private static final int NUM_FRAMES = 100; 30 private static final int NUM_ITERATIONS = 8; 31 private static final int TIMEOUT = 1000000; 32 private static final String REPORT_LOG_NAME = "CtsOpenGlPerf2TestCases"; 33 GLPrimitiveBenchmark()34 public GLPrimitiveBenchmark() { 35 super(GLPrimitiveActivity.class); 36 } 37 38 /** 39 * Runs the full OpenGL ES 2.0 pipeline test offscreen. 40 */ testFullPipelineOffscreen()41 public void testFullPipelineOffscreen() throws Exception { 42 String streamName = "test_full_pipeline_offscreen"; 43 runBenchmark(BenchmarkName.FullPipeline, true, NUM_FRAMES, NUM_ITERATIONS, TIMEOUT, 44 streamName); 45 } 46 47 /** 48 * Runs the full OpenGL ES 2.0 pipeline test onscreen. 49 */ testFullPipelineOnscreen()50 public void testFullPipelineOnscreen() throws Exception { 51 String streamName = "test_full_pipeline_onscreen"; 52 runBenchmark(BenchmarkName.FullPipeline, false, NUM_FRAMES, NUM_ITERATIONS, TIMEOUT, 53 streamName); 54 } 55 56 /** 57 * Runs the pixel output test offscreen. 58 */ testPixelOutputOffscreen()59 public void testPixelOutputOffscreen() throws Exception { 60 String streamName = "test_pixel_output_offscreen"; 61 runBenchmark(BenchmarkName.PixelOutput, true, NUM_FRAMES, NUM_ITERATIONS, TIMEOUT, 62 streamName); 63 } 64 65 /** 66 * Runs the pixel output test onscreen. 67 */ testPixelOutputOnscreen()68 public void testPixelOutputOnscreen() throws Exception { 69 String streamName = "test_pixel_output_onscreen"; 70 runBenchmark(BenchmarkName.PixelOutput, false, NUM_FRAMES, NUM_ITERATIONS, TIMEOUT, 71 streamName); 72 } 73 74 /** 75 * Runs the shader performance test offscreen. 76 */ testShaderPerfOffscreen()77 public void testShaderPerfOffscreen() throws Exception { 78 String streamName = "test_shader_perf_offscreen"; 79 runBenchmark(BenchmarkName.ShaderPerf, true, NUM_FRAMES, NUM_ITERATIONS, TIMEOUT, 80 streamName); 81 } 82 83 /** 84 * Runs the shader performance test onscreen. 85 */ testShaderPerfOnscreen()86 public void testShaderPerfOnscreen() throws Exception { 87 String streamName = "test_shader_perf_onscreen"; 88 runBenchmark(BenchmarkName.ShaderPerf, false, NUM_FRAMES, NUM_ITERATIONS, TIMEOUT, 89 streamName); 90 } 91 92 /** 93 * Runs the context switch overhead test offscreen. 94 */ testContextSwitchOffscreen()95 public void testContextSwitchOffscreen() throws Exception { 96 String streamName = "test_context_switch_offscreen"; 97 runBenchmark(BenchmarkName.ContextSwitch, true, NUM_FRAMES, NUM_ITERATIONS, TIMEOUT, 98 streamName); 99 } 100 101 /** 102 * Runs the context switch overhead test onscreen. 103 */ testContextSwitchOnscreen()104 public void testContextSwitchOnscreen() throws Exception { 105 String streamName = "test_context_switch_onscreen"; 106 runBenchmark(BenchmarkName.ContextSwitch, false, NUM_FRAMES, NUM_ITERATIONS, TIMEOUT, 107 streamName); 108 } 109 110 /** 111 * Runs the specified test. 112 * 113 * @param benchmark An enum representing the benchmark to run. 114 * @param offscreen Whether to render to an offscreen framebuffer rather than the screen. 115 * @param numFrames The number of frames to render. 116 * @param numIterations The number of iterations to run, each iteration has a bigger workload. 117 * @param timeout The milliseconds to wait for an iteration of the benchmark before timing out. 118 * @param streamName The name of the stream of test metrics. 119 * @throws Exception If the benchmark could not be run. 120 */ runBenchmark(BenchmarkName benchmark, boolean offscreen, int numFrames, int numIterations, int timeout, String streamName)121 private void runBenchmark(BenchmarkName benchmark, boolean offscreen, int numFrames, 122 int numIterations, int timeout, String streamName) throws Exception { 123 String benchmarkName = benchmark.toString(); 124 Intent intent = new Intent(); 125 intent.putExtra(GLActivityIntentKeys.INTENT_EXTRA_BENCHMARK_NAME, benchmarkName); 126 intent.putExtra(GLActivityIntentKeys.INTENT_EXTRA_OFFSCREEN, offscreen); 127 intent.putExtra(GLActivityIntentKeys.INTENT_EXTRA_NUM_FRAMES, numFrames); 128 intent.putExtra(GLActivityIntentKeys.INTENT_EXTRA_NUM_ITERATIONS, numIterations); 129 intent.putExtra(GLActivityIntentKeys.INTENT_EXTRA_TIMEOUT, timeout); 130 131 setActivityIntent(intent); 132 GLPrimitiveActivity activity = getActivity(); 133 if (activity != null) { 134 activity.waitForCompletion(); 135 double[] fpsValues = activity.mFpsValues; 136 double score = 0; 137 for (double d : fpsValues) { 138 score += d; 139 } 140 score /= numIterations;// Average. 141 142 // TODO: maybe standard deviation / RMSE will be useful? 143 144 DeviceReportLog report = new DeviceReportLog(REPORT_LOG_NAME, streamName); 145 report.setSummary("average_fps", score, ResultType.HIGHER_BETTER, ResultUnit.SCORE); 146 report.submit(getInstrumentation()); 147 } 148 } 149 } 150