| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 29 |
|
| 30 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 113 |
public CustomOpenGLView getOpenGLView() { |
| 114 |
if (mOpenGLView == null) { |
| 115 |
throw new RuntimeException("OpenGL view is not attached"); |
| 116 |
} |
| 117 |
return mOpenGLView; |
| 118 |
} |
| 119 |
|
| 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 |
| 128 |
protected void onCreate(Bundle savedInstanceState) { |
| 129 |
super.onCreate(savedInstanceState); |
| 130 |
|
| 131 |
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); |
| 132 |
|
| 133 |
|
| 134 |
|
| 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 |
|
| 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 |
|
| 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 |
| 174 |
protected void onResume() { |
| 175 |
super.onResume(); |
| 176 |
} |
| 177 |
|
| 178 |
@Override |
| 179 |
protected void onPause() { |
| 180 |
super.onPause(); |
| 181 |
} |
| 182 |
} |