Home | History | Annotate | Download | only in testframerate
      1 /*
      2  * Copyright (C) 2009 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 com.android.testframerate;
     18 
     19 import android.content.Context;
     20 import android.opengl.GLSurfaceView;
     21 import android.os.SystemProperties;
     22 import android.util.AttributeSet;
     23 import android.util.Log;
     24 import android.view.KeyEvent;
     25 import android.view.MotionEvent;
     26 
     27 import java.nio.ByteBuffer;
     28 import java.nio.ByteOrder;
     29 import java.nio.FloatBuffer;
     30 
     31 import javax.microedition.khronos.egl.EGL10;
     32 import javax.microedition.khronos.egl.EGLConfig;
     33 import javax.microedition.khronos.egl.EGLContext;
     34 import javax.microedition.khronos.egl.EGLDisplay;
     35 import javax.microedition.khronos.opengles.GL10;
     36 
     37 import android.opengl.GLES20;
     38 
     39 class TestFramerateView extends GLSurfaceView {
     40     private static String TAG = "TestFramerateView";
     41 
     42     public TestFramerateView(Context context) {
     43         super(context);
     44         setEGLContextClientVersion(2);
     45         setRenderer(new Renderer());
     46     }
     47 
     48     private long mLastTime_us = 0;
     49     private long mNumShortFramesElapsed = 0;
     50     private void registerTime(long now_us) {
     51         long longFrameTime_ms = Integer.parseInt(SystemProperties.get("debug.longframe_ms", "16"));
     52         long elapsedTime_us = now_us - mLastTime_us;
     53         float fps = 1000000.f / elapsedTime_us;
     54         if (mLastTime_us > 0 && elapsedTime_us > longFrameTime_ms*1000) {
     55           Log.v(TAG, "Long frame: " + elapsedTime_us/1000.f + " ms (" + fps + " fps)");
     56           if (mNumShortFramesElapsed > 0) {
     57             Log.v(TAG, "  Short frames since last long frame: " + mNumShortFramesElapsed);
     58             mNumShortFramesElapsed = 0;
     59           }
     60         } else {
     61             ++mNumShortFramesElapsed;
     62         }
     63 
     64         mLastTime_us = now_us;
     65     }
     66 
     67     private class Renderer implements GLSurfaceView.Renderer {
     68         public Renderer() {
     69         }
     70 
     71 
     72         public void onDrawFrame(GL10 gl) {
     73             long now_us = System.nanoTime() / 1000;
     74             registerTime(now_us);
     75 
     76             float red = (now_us % 1000000) / 1000000.f;
     77             float green = (now_us % 2000000) / 2000000.f;
     78             float blue = (now_us % 3000000) / 3000000.f;
     79             GLES20.glClearColor(red, green, blue, 1.0f);
     80             GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
     81         }
     82 
     83         public void onSurfaceChanged(GL10 gl, int width, int height) {
     84             GLES20.glViewport(0, 0, width, height);
     85         }
     86 
     87         public void onSurfaceCreated(GL10 gl, EGLConfig config) {
     88         }
     89 
     90     }
     91 }
     92