1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.openglperf.cts;
18 
19 import android.app.Activity;
20 import android.content.Intent;
21 import android.os.Bundle;
22 import android.view.WindowManager;
23 
24 import java.util.concurrent.Semaphore;
25 import java.util.concurrent.TimeUnit;
26 
27 /**
28  * Activity which runs GL test and measure FPS with given parameters passed from
29  * Intent. For the meaning of parameters, check {@PlanetsRenderingParam}
30  */
31 public class GlPlanetsActivity extends Activity implements
32         RenderCompletionListener {
33     public static final String INTENT_EXTRA_NUM_PLANETS = "numPlanets";
34     public static final String INTENT_EXTRA_USE_VBO_VERTICES = "useVboVertices";
35     public static final String INTENT_EXTRA_USE_VBO_INDICES = "useVboIndiices";
36     public static final String INTENT_EXTRA_NUM_FRAMES = "numFrames";
37     public static final String INTENT_EXTRA_NUM_INDEX_BUFFERS = "numIndexBuffers";
38 
39     public static final String INTENT_RESULT_FPS = "fps";
40     public static final String INTENT_RESULT_NUM_TRIANGLES = "numTrigngles";
41 
42     private final Semaphore mSem = new Semaphore(0);
43     private float mAverageFps;
44     private int mNumTriangles;
45     private int[] mFrameInterval;
46     private String mRendererName;
47 
48     private PlanetsSurfaceView mView;
49 
waitForGlPlanetsCompletionWithTimeout(long timeoutInSecs)50     public boolean waitForGlPlanetsCompletionWithTimeout(long timeoutInSecs)
51             throws InterruptedException {
52         return mSem.tryAcquire(timeoutInSecs, TimeUnit.SECONDS);
53     }
54 
getAverageFps()55     public float getAverageFps() {
56         return mAverageFps;
57     }
58 
getNumTriangles()59     public int getNumTriangles() {
60         return mNumTriangles;
61     }
62 
getRendererName()63     public String getRendererName() {
64         return mRendererName;
65     }
66 
67     /**
68      * Time interval between each frame's rendering in ms.
69      * The first value will be invalid, so client should discard them.
70      * @return can return null if INTENT_EXTRA_NUM_FRAMES was not set in intent.
71      */
getFrameInterval()72     public int[] getFrameInterval() {
73         return mFrameInterval;
74     }
75 
76     @Override
onCreate(Bundle savedInstanceState)77     public void onCreate(Bundle savedInstanceState) {
78         super.onCreate(savedInstanceState);
79         getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
80                 | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
81                 | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
82         Intent intent = getIntent();
83         PlanetsRenderingParam param = new PlanetsRenderingParam();
84         param.mNumPlanets = intent.getIntExtra(INTENT_EXTRA_NUM_PLANETS, param.mNumPlanets);
85         param.mUseVboForVertices = intent.getBooleanExtra(INTENT_EXTRA_USE_VBO_VERTICES,
86                 param.mUseVboForVertices);
87         param.mUseVboForIndices = intent.getBooleanExtra(INTENT_EXTRA_USE_VBO_INDICES,
88                 param.mUseVboForIndices);
89         param.mNumFrames = intent.getIntExtra(INTENT_EXTRA_NUM_FRAMES, param.mNumFrames);
90         param.mNumIndicesPerVertex = intent.getIntExtra(INTENT_EXTRA_NUM_INDEX_BUFFERS,
91                 param.mNumIndicesPerVertex);
92         mView = new PlanetsSurfaceView(this, param, this);
93         setContentView(mView);
94     }
95 
96     @Override
onRenderCompletion(float averageFps, int numTriangles, int[] frameInterval, String rendererName)97     public void onRenderCompletion(float averageFps, int numTriangles, int[] frameInterval,
98             String rendererName) {
99         mAverageFps = averageFps;
100         mNumTriangles = numTriangles;
101         mFrameInterval = frameInterval;
102         mRendererName = rendererName;
103         mSem.release();
104     }
105 
106     @Override
onResume()107     protected void onResume() {
108         super.onResume();
109         mView.onResume();
110     }
111 
112     @Override
onPause()113     protected void onPause() {
114         super.onPause();
115         mView.onPause();
116     }
117 }
118