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 different types of views. 29 * call attachSurfaceView, attachOpenGLView or attachControlView to switch 30 * the view. 31 */ 32 public class GamePerformanceActivity extends Activity { 33 private CustomSurfaceView mSurfaceView = null; 34 private CustomOpenGLView mOpenGLView = null; 35 private CustomControlView mControlView = null; 36 37 private RelativeLayout mRootLayout; 38 detachAllViews()39 private void detachAllViews() { 40 if (mOpenGLView != null) { 41 mRootLayout.removeView(mOpenGLView); 42 mOpenGLView = null; 43 } 44 if (mSurfaceView != null) { 45 mRootLayout.removeView(mSurfaceView); 46 mSurfaceView = null; 47 } 48 if (mControlView != null) { 49 mRootLayout.removeView(mControlView); 50 mControlView = null; 51 } 52 } 53 attachSurfaceView()54 public void attachSurfaceView() throws InterruptedException { 55 synchronized (mRootLayout) { 56 if (mSurfaceView != null) { 57 return; 58 } 59 final CountDownLatch latch = new CountDownLatch(1); 60 runOnUiThread(new Runnable() { 61 @Override 62 public void run() { 63 detachAllViews(); 64 mSurfaceView = new CustomSurfaceView(GamePerformanceActivity.this); 65 mRootLayout.addView(mSurfaceView); 66 latch.countDown(); 67 } 68 }); 69 latch.await(); 70 mSurfaceView.waitForSurfaceReady(); 71 } 72 } 73 attachOpenGLView()74 public void attachOpenGLView() throws InterruptedException { 75 synchronized (mRootLayout) { 76 if (mOpenGLView != null) { 77 return; 78 } 79 final CountDownLatch latch = new CountDownLatch(1); 80 runOnUiThread(new Runnable() { 81 @Override 82 public void run() { 83 detachAllViews(); 84 mOpenGLView = new CustomOpenGLView(GamePerformanceActivity.this); 85 mRootLayout.addView(mOpenGLView); 86 latch.countDown(); 87 } 88 }); 89 latch.await(); 90 } 91 } 92 attachControlView()93 public void attachControlView() throws InterruptedException { 94 synchronized (mRootLayout) { 95 if (mControlView != null) { 96 return; 97 } 98 final CountDownLatch latch = new CountDownLatch(1); 99 runOnUiThread(new Runnable() { 100 @Override 101 public void run() { 102 detachAllViews(); 103 mControlView = new CustomControlView(GamePerformanceActivity.this); 104 mRootLayout.addView(mControlView); 105 latch.countDown(); 106 } 107 }); 108 latch.await(); 109 } 110 } 111 112 getOpenGLView()113 public CustomOpenGLView getOpenGLView() { 114 if (mOpenGLView == null) { 115 throw new RuntimeException("OpenGL view is not attached"); 116 } 117 return mOpenGLView; 118 } 119 getControlView()120 public CustomControlView getControlView() { 121 if (mControlView == null) { 122 throw new RuntimeException("Control view is not attached"); 123 } 124 return mControlView; 125 } 126 127 @Override onCreate(Bundle savedInstanceState)128 protected void onCreate(Bundle savedInstanceState) { 129 super.onCreate(savedInstanceState); 130 131 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 132 133 // To layouts in parent. First contains list of Surfaces and second 134 // controls. Controls stay on top. 135 mRootLayout = new RelativeLayout(this); 136 mRootLayout.setLayoutParams(new ViewGroup.LayoutParams( 137 ViewGroup.LayoutParams.MATCH_PARENT, 138 ViewGroup.LayoutParams.MATCH_PARENT)); 139 140 Rect rect = new Rect(); 141 getWindow().getDecorView().getWindowVisibleDisplayFrame(rect); 142 143 mOpenGLView = new CustomOpenGLView(this); 144 mRootLayout.addView(mOpenGLView); 145 146 setContentView(mRootLayout); 147 } 148 resetFrameTimes()149 public void resetFrameTimes() { 150 if (mSurfaceView != null) { 151 mSurfaceView.resetFrameTimes(); 152 } else if (mOpenGLView != null) { 153 mOpenGLView.resetFrameTimes(); 154 } else if (mControlView != null) { 155 mControlView.resetFrameTimes(); 156 } else { 157 throw new IllegalStateException("Nothing attached"); 158 } 159 } 160 getFps()161 public double getFps() { 162 if (mSurfaceView != null) { 163 return mSurfaceView.getFps(); 164 } else if (mOpenGLView != null) { 165 return mOpenGLView.getFps(); 166 } else if (mControlView != null) { 167 return mControlView.getFps(); 168 } else { 169 throw new IllegalStateException("Nothing attached"); 170 } 171 } 172 173 @Override onResume()174 protected void onResume() { 175 super.onResume(); 176 } 177 178 @Override onPause()179 protected void onPause() { 180 super.onPause(); 181 } 182 }