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.concurrent.CountDownLatch; 19 20 import android.app.Activity; 21 import android.graphics.Rect; 22 import android.os.Bundle; 23 import android.view.ViewGroup; 24 import android.view.WindowManager; 25 import android.widget.RelativeLayout; 26 27 /** 28 * Minimal activity that holds SurfaceView or GLSurfaceView. 29 * call attachSurfaceView or attachOpenGLView to switch views. 30 */ 31 public class GamePerformanceActivity extends Activity { 32 private CustomSurfaceView mSurfaceView = null; 33 private CustomOpenGLView mOpenGLView = null; 34 private RelativeLayout mRootLayout; 35 attachSurfaceView()36 public void attachSurfaceView() throws InterruptedException { 37 synchronized (mRootLayout) { 38 if (mSurfaceView != null) { 39 return; 40 } 41 final CountDownLatch latch = new CountDownLatch(1); 42 runOnUiThread(new Runnable() { 43 @Override 44 public void run() { 45 if (mOpenGLView != null) { 46 mRootLayout.removeView(mOpenGLView); 47 mOpenGLView = null; 48 } 49 mSurfaceView = new CustomSurfaceView(GamePerformanceActivity.this); 50 mRootLayout.addView(mSurfaceView); 51 latch.countDown(); 52 } 53 }); 54 latch.await(); 55 mSurfaceView.waitForSurfaceReady(); 56 } 57 } 58 attachOpenGLView()59 public void attachOpenGLView() throws InterruptedException { 60 synchronized (mRootLayout) { 61 if (mOpenGLView != null) { 62 return; 63 } 64 final CountDownLatch latch = new CountDownLatch(1); 65 runOnUiThread(new Runnable() { 66 @Override 67 public void run() { 68 if (mSurfaceView != null) { 69 mRootLayout.removeView(mSurfaceView); 70 mSurfaceView = null; 71 } 72 mOpenGLView = new CustomOpenGLView(GamePerformanceActivity.this); 73 mRootLayout.addView(mOpenGLView); 74 latch.countDown(); 75 } 76 }); 77 latch.await(); 78 } 79 } 80 81 @Override onCreate(Bundle savedInstanceState)82 protected void onCreate(Bundle savedInstanceState) { 83 super.onCreate(savedInstanceState); 84 85 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 86 87 // To layouts in parent. First contains list of Surfaces and second 88 // controls. Controls stay on top. 89 mRootLayout = new RelativeLayout(this); 90 mRootLayout.setLayoutParams(new ViewGroup.LayoutParams( 91 ViewGroup.LayoutParams.MATCH_PARENT, 92 ViewGroup.LayoutParams.MATCH_PARENT)); 93 94 Rect rect = new Rect(); 95 getWindow().getDecorView().getWindowVisibleDisplayFrame(rect); 96 97 mOpenGLView = new CustomOpenGLView(this); 98 mRootLayout.addView(mOpenGLView); 99 100 setContentView(mRootLayout); 101 } 102 resetFrameTimes()103 public void resetFrameTimes() { 104 if (mSurfaceView != null) { 105 mSurfaceView.resetFrameTimes(); 106 } else if (mOpenGLView != null) { 107 mOpenGLView.resetFrameTimes(); 108 } else { 109 throw new IllegalStateException("Nothing attached"); 110 } 111 } 112 getFps()113 public double getFps() { 114 if (mSurfaceView != null) { 115 return mSurfaceView.getFps(); 116 } else if (mOpenGLView != null) { 117 return mOpenGLView.getFps(); 118 } else { 119 throw new IllegalStateException("Nothing attached"); 120 } 121 } 122 123 @Override onResume()124 protected void onResume() { 125 super.onResume(); 126 } 127 128 @Override onPause()129 protected void onPause() { 130 super.onPause(); 131 } 132 }