blob: dc745f17e698872a33368e9caa960c35b61593b2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.gameperformance;
import java.util.concurrent.CountDownLatch;
import android.app.Activity;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.RelativeLayout;
/**
* Minimal activity that holds different types of views.
* call attachSurfaceView, attachOpenGLView or attachControlView to switch
* the view.
*/
public class GamePerformanceActivity extends Activity {
private CustomSurfaceView mSurfaceView = null;
private CustomOpenGLView mOpenGLView = null;
private CustomControlView mControlView = null;
private RelativeLayout mRootLayout;
private void detachAllViews() {
if (mOpenGLView != null) {
mRootLayout.removeView(mOpenGLView);
mOpenGLView = null;
}
if (mSurfaceView != null) {
mRootLayout.removeView(mSurfaceView);
mSurfaceView = null;
}
if (mControlView != null) {
mRootLayout.removeView(mControlView);
mControlView = null;
}
}
public void attachSurfaceView() throws InterruptedException {
synchronized (mRootLayout) {
if (mSurfaceView != null) {
return;
}
final CountDownLatch latch = new CountDownLatch(1);
runOnUiThread(new Runnable() {
@Override
public void run() {
detachAllViews();
mSurfaceView = new CustomSurfaceView(GamePerformanceActivity.this);
mRootLayout.addView(mSurfaceView);
latch.countDown();
}
});
latch.await();
mSurfaceView.waitForSurfaceReady();
}
}
public void attachOpenGLView() throws InterruptedException {
synchronized (mRootLayout) {
if (mOpenGLView != null) {
return;
}
final CountDownLatch latch = new CountDownLatch(1);
runOnUiThread(new Runnable() {
@Override
public void run() {
detachAllViews();
mOpenGLView = new CustomOpenGLView(GamePerformanceActivity.this);
mRootLayout.addView(mOpenGLView);
latch.countDown();
}
});
latch.await();
}
}
public void attachControlView() throws InterruptedException {
synchronized (mRootLayout) {
if (mControlView != null) {
return;
}
final CountDownLatch latch = new CountDownLatch(1);
runOnUiThread(new Runnable() {
@Override
public void run() {
detachAllViews();
mControlView = new CustomControlView(GamePerformanceActivity.this);
mRootLayout.addView(mControlView);
latch.countDown();
}
});
latch.await();
}
}
public CustomOpenGLView getOpenGLView() {
if (mOpenGLView == null) {
throw new RuntimeException("OpenGL view is not attached");
}
return mOpenGLView;
}
public CustomControlView getControlView() {
if (mControlView == null) {
throw new RuntimeException("Control view is not attached");
}
return mControlView;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
// To layouts in parent. First contains list of Surfaces and second
// controls. Controls stay on top.
mRootLayout = new RelativeLayout(this);
mRootLayout.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
Rect rect = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
mOpenGLView = new CustomOpenGLView(this);
mRootLayout.addView(mOpenGLView);
setContentView(mRootLayout);
}
public void resetFrameTimes() {
if (mSurfaceView != null) {
mSurfaceView.resetFrameTimes();
} else if (mOpenGLView != null) {
mOpenGLView.resetFrameTimes();
} else if (mControlView != null) {
mControlView.resetFrameTimes();
} else {
throw new IllegalStateException("Nothing attached");
}
}
public double getFps() {
if (mSurfaceView != null) {
return mSurfaceView.getFps();
} else if (mOpenGLView != null) {
return mOpenGLView.getFps();
} else if (mControlView != null) {
return mControlView.getFps();
} else {
throw new IllegalStateException("Nothing attached");
}
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
}
|