1 /*
2  * Copyright (C) 2018 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 package android.gameperformance;
17 
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.Random;
21 
22 import javax.microedition.khronos.egl.EGLConfig;
23 import javax.microedition.khronos.opengles.GL10;
24 
25 import android.content.Context;
26 import android.opengl.GLES20;
27 import android.opengl.GLSurfaceView;
28 
29 public class CustomOpenGLView extends GLSurfaceView {
30     private Random mRandom;
31     private List<Long> mFrameTimes;
32 
CustomOpenGLView(Context context)33     public CustomOpenGLView(Context context) {
34         super(context);
35 
36         mRandom = new Random();
37         mFrameTimes = new ArrayList<Long>();
38 
39         setEGLContextClientVersion(2);
40 
41         setRenderer(new GLSurfaceView.Renderer() {
42             @Override
43             public void onSurfaceCreated(GL10 gl, EGLConfig config) {
44                 GLES20.glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
45                 gl.glClearDepthf(1.0f);
46                 gl.glEnable(GL10.GL_DEPTH_TEST);
47                 gl.glDepthFunc(GL10.GL_LEQUAL);
48 
49                 gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,
50                           GL10.GL_NICEST);            }
51 
52             @Override
53             public void onSurfaceChanged(GL10 gl, int width, int height) {
54                 GLES20.glViewport(0, 0, width, height);
55             }
56 
57             @Override
58             public void onDrawFrame(GL10 gl) {
59                 GLES20.glClearColor(
60                         mRandom.nextFloat(), mRandom.nextFloat(), mRandom.nextFloat(), 1.0f);
61                 gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
62                 synchronized (mFrameTimes) {
63                     mFrameTimes.add(System.currentTimeMillis());
64                 }
65             }
66         });
67         setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
68     }
69 
70     /**
71      * Resets frame times in order to calculate fps for different test pass.
72      */
resetFrameTimes()73     public void resetFrameTimes() {
74         synchronized (mFrameTimes) {
75             mFrameTimes.clear();
76         }
77     }
78 
79     /**
80      * Returns current fps based on collected frame times.
81      */
getFps()82     public double getFps() {
83         synchronized (mFrameTimes) {
84             if (mFrameTimes.size() < 2) {
85                 return 0.0f;
86             }
87             return 1000.0 * mFrameTimes.size() /
88                     (mFrameTimes.get(mFrameTimes.size() - 1) - mFrameTimes.get(0));
89         }
90     }
91 }
92