forked from google/grafika
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextureViewGLActivity.java
295 lines (261 loc) · 11.5 KB
/
TextureViewGLActivity.java
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
* Copyright 2013 Google Inc. All rights reserved.
*
* 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 com.android.grafika;
import android.opengl.GLES20;
import android.opengl.GLES30;
import android.os.Bundle;
import android.util.Log;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.graphics.SurfaceTexture;
import com.android.grafika.gles.EglCore;
import com.android.grafika.gles.WindowSurface;
/**
* Simple demonstration of using GLES to draw on a TextureView.
* <p>
* Note that rendering is a multi-stage process:
* <ol>
* <li>Render thread draws with GL on its local EGLSurface, a window surface it created. The
* window surface is backed by the SurfaceTexture from TextureVIew.
* <li>The SurfaceTexture takes what is rendered onto it and makes it available as a GL texture.
* <li>TextureView takes the GL texture and renders it onto its EGLSurface. That EGLSurface
* is a window surface visible to the compositor.
* </ol>
* It's important to bear in mind that Surface and EGLSurface are related but very
* different things.
* <p>
* Unlike GLSurfaceView, TextureView doesn't manage the EGL config or renderer thread, so we
* take care of that ourselves.
* <p>
* Currently renders frames as fast as possible, without waiting for the consumer.
* <p>
* As part of experimenting with the framework, this allows the renderer thread to continue
* to run as the TextureView is being destroyed (we stop the thread in onDestroy() rather
* than onPause()). Normally the renderer would be stopped when the application pauses.
*/
public class TextureViewGLActivity extends Activity {
private static final String TAG = MainActivity.TAG;
// Experiment with allowing TextureView to release the SurfaceTexture from the callback vs.
// releasing it explicitly ourselves from the draw loop. The latter seems to be problematic
// in 4.4 (KK) -- set the flag to "false", rotate the screen a few times, then check the
// output of "adb shell ps -t | grep `pid grafika`".
//
// Must be static or it'll get reset on every Activity pause/resume.
private static volatile boolean sReleaseInCallback = true;
private TextureView mTextureView;
private Renderer mRenderer;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate");
super.onCreate(savedInstanceState);
// Start up the Renderer thread. It'll sleep until the TextureView is ready.
mRenderer = new Renderer();
mRenderer.start();
setContentView(R.layout.activity_texture_view_gl);
mTextureView = (TextureView) findViewById(R.id.glTextureView);
mTextureView.setSurfaceTextureListener(mRenderer);
}
@Override
protected void onResume() {
super.onResume();
updateControls();
}
@Override
protected void onDestroy() {
Log.d(TAG, "onDestroy");
super.onDestroy();
// Don't do this -- halt the thread in onPause() and wait for it to finish.
mRenderer.halt();
}
/**
* Updates the UI elements to match current state.
*/
private void updateControls() {
Button toggleRelease = (Button) findViewById(R.id.toggleRelease_button);
int id = sReleaseInCallback ?
R.string.toggleReleaseCallbackOff : R.string.toggleReleaseCallbackOn;
toggleRelease.setText(id);
}
/**
* onClick handler for toggleRelease_button.
*/
public void clickToggleRelease(View unused) {
sReleaseInCallback = !sReleaseInCallback;
updateControls();
}
/**
* Handles GL rendering and SurfaceTexture callbacks.
* <p>
* We don't create a Looper, so the SurfaceTexture-by-way-of-TextureView callbacks
* happen on the UI thread.
*/
private static class Renderer extends Thread implements TextureView.SurfaceTextureListener {
private Object mLock = new Object(); // guards mSurfaceTexture, mDone
private SurfaceTexture mSurfaceTexture;
private EglCore mEglCore;
private boolean mDone;
public Renderer() {
super("TextureViewGL Renderer");
}
@Override
public void run() {
while (true) {
SurfaceTexture surfaceTexture = null;
// Latch the SurfaceTexture when it becomes available. We have to wait for
// the TextureView to create it.
synchronized (mLock) {
while (!mDone && (surfaceTexture = mSurfaceTexture) == null) {
try {
mLock.wait();
} catch (InterruptedException ie) {
throw new RuntimeException(ie); // not expected
}
}
if (mDone) {
break;
}
}
Log.d(TAG, "Got surfaceTexture=" + surfaceTexture);
// Create an EGL surface for our new SurfaceTexture. We're not on the same
// thread as the SurfaceTexture, which is a concern for the *consumer*, which
// wants to call updateTexImage(). Because we're the *producer*, i.e. the
// one generating the frames, we don't need to worry about being on the same
// thread.
mEglCore = new EglCore(null, EglCore.FLAG_TRY_GLES3);
WindowSurface windowSurface = new WindowSurface(mEglCore, mSurfaceTexture);
windowSurface.makeCurrent();
// Render frames until we're told to stop or the SurfaceTexture is destroyed.
doAnimation(windowSurface);
windowSurface.release();
mEglCore.release();
if (!sReleaseInCallback) {
Log.i(TAG, "Releasing SurfaceTexture in renderer thread");
surfaceTexture.release();
}
}
Log.d(TAG, "Renderer thread exiting");
}
/**
* Draws updates as fast as the system will allow.
* <p>
* In 4.4, with the synchronous buffer queue queue, the frame rate will be limited.
* In previous (and future) releases, with the async queue, many of the frames we
* render may be dropped.
* <p>
* The correct thing to do here is use Choreographer to schedule frame updates off
* of vsync, but that's not nearly as much fun.
*/
private void doAnimation(WindowSurface eglSurface) {
final int BLOCK_WIDTH = 80;
final int BLOCK_SPEED = 2;
float clearColor = 0.0f;
int xpos = -BLOCK_WIDTH / 2;
int xdir = BLOCK_SPEED;
int width = eglSurface.getWidth();
int height = eglSurface.getHeight();
Log.d(TAG, "Animating " + width + "x" + height + " EGL surface");
while (true) {
// Check to see if the TextureView's SurfaceTexture is still valid.
synchronized (mLock) {
SurfaceTexture surfaceTexture = mSurfaceTexture;
if (surfaceTexture == null) {
Log.d(TAG, "doAnimation exiting");
return;
}
}
// Still alive, render a frame.
GLES20.glClearColor(clearColor, clearColor, clearColor, 1.0f);
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
GLES20.glEnable(GLES20.GL_SCISSOR_TEST);
GLES20.glScissor(xpos, height / 4, BLOCK_WIDTH, height / 2);
GLES20.glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
GLES20.glDisable(GLES20.GL_SCISSOR_TEST);
// Publish the frame. If we overrun the consumer, frames will be dropped,
// so on a sufficiently fast device the animation will run at faster than
// the display refresh rate.
//
// If the SurfaceTexture has been destroyed, this will throw an exception.
eglSurface.swapBuffers();
// Advance state
clearColor += 0.015625f;
if (clearColor > 1.0f) {
clearColor = 0.0f;
}
xpos += xdir;
if (xpos <= -BLOCK_WIDTH / 2 || xpos >= width - BLOCK_WIDTH / 2) {
Log.d(TAG, "change direction");
xdir = -xdir;
}
}
}
/**
* Tells the thread to stop running.
*/
public void halt() {
synchronized (mLock) {
mDone = true;
mLock.notify();
}
}
@Override // will be called on UI thread
public void onSurfaceTextureAvailable(SurfaceTexture st, int width, int height) {
Log.d(TAG, "onSurfaceTextureAvailable(" + width + "x" + height + ")");
synchronized (mLock) {
mSurfaceTexture = st;
mLock.notify();
}
}
@Override // will be called on UI thread
public void onSurfaceTextureSizeChanged(SurfaceTexture st, int width, int height) {
Log.d(TAG, "onSurfaceTextureSizeChanged(" + width + "x" + height + ")");
// TODO: ?
}
@Override // will be called on UI thread
public boolean onSurfaceTextureDestroyed(SurfaceTexture st) {
Log.d(TAG, "onSurfaceTextureDestroyed");
// We set the SurfaceTexture reference to null to tell the Renderer thread that
// it needs to stop. The renderer might be in the middle of drawing, so we want
// to return false here so that the caller doesn't try to release the ST out
// from under us.
//
// In theory.
//
// In 4.4, the buffer queue was changed to be synchronous, which means we block
// in dequeueBuffer(). If the renderer has been running flat out and is currently
// sleeping in eglSwapBuffers(), it's going to be stuck there until somebody
// tears down the SurfaceTexture. So we need to tear it down here to ensure
// that the renderer thread will break. If we don't, the thread sticks there
// forever.
//
// The only down side to releasing it here is we'll get some complaints in logcat
// when eglSwapBuffers() fails.
synchronized (mLock) {
mSurfaceTexture = null;
}
if (sReleaseInCallback) {
Log.i(TAG, "Allowing TextureView to release SurfaceTexture");
}
return sReleaseInCallback;
}
@Override // will be called on UI thread
public void onSurfaceTextureUpdated(SurfaceTexture st) {
//Log.d(TAG, "onSurfaceTextureUpdated");
}
}
}