blob: 4738318472ead6fbea1a5203c1b87f6272f8f67b [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 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
17package android.opengl;
18
Artur Satayev53ada2a2019-12-10 17:47:56 +000019import android.compat.annotation.UnsupportedAppUsage;
John Reckcf890192016-06-29 13:35:27 -070020import android.content.Context;
21import android.os.Trace;
22import android.util.AttributeSet;
23import android.util.Log;
24import android.view.SurfaceHolder;
25import android.view.SurfaceView;
26
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import java.io.Writer;
Jack Palevich80b3cd62011-11-15 14:51:27 -080028import java.lang.ref.WeakReference;
Jeff Brownf48b0322013-04-19 17:13:33 -070029import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
31import javax.microedition.khronos.egl.EGL10;
32import javax.microedition.khronos.egl.EGL11;
33import javax.microedition.khronos.egl.EGLConfig;
34import javax.microedition.khronos.egl.EGLContext;
35import javax.microedition.khronos.egl.EGLDisplay;
36import javax.microedition.khronos.egl.EGLSurface;
37import javax.microedition.khronos.opengles.GL;
38import javax.microedition.khronos.opengles.GL10;
39
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040/**
41 * An implementation of SurfaceView that uses the dedicated surface for
Jack Palevichb47c6412009-04-09 14:54:12 -070042 * displaying OpenGL rendering.
43 * <p>
44 * A GLSurfaceView provides the following features:
45 * <p>
46 * <ul>
47 * <li>Manages a surface, which is a special piece of memory that can be
48 * composited into the Android view system.
49 * <li>Manages an EGL display, which enables OpenGL to render into a surface.
50 * <li>Accepts a user-provided Renderer object that does the actual rendering.
51 * <li>Renders on a dedicated thread to decouple rendering performance from the
52 * UI thread.
53 * <li>Supports both on-demand and continuous rendering.
54 * <li>Optionally wraps, traces, and/or error-checks the renderer's OpenGL calls.
55 * </ul>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 *
Joe Fernandez61fd1e82011-10-26 13:39:11 -070057 * <div class="special reference">
58 * <h3>Developer Guides</h3>
59 * <p>For more information about how to use OpenGL, read the
60 * <a href="{@docRoot}guide/topics/graphics/opengl.html">OpenGL</a> developer guide.</p>
61 * </div>
62 *
Jack Palevichb47c6412009-04-09 14:54:12 -070063 * <h3>Using GLSurfaceView</h3>
64 * <p>
65 * Typically you use GLSurfaceView by subclassing it and overriding one or more of the
66 * View system input event methods. If your application does not need to override event
67 * methods then GLSurfaceView can be used as-is. For the most part
68 * GLSurfaceView behavior is customized by calling "set" methods rather than by subclassing.
69 * For example, unlike a regular View, drawing is delegated to a separate Renderer object which
70 * is registered with the GLSurfaceView
71 * using the {@link #setRenderer(Renderer)} call.
72 * <p>
73 * <h3>Initializing GLSurfaceView</h3>
74 * All you have to do to initialize a GLSurfaceView is call {@link #setRenderer(Renderer)}.
75 * However, if desired, you can modify the default behavior of GLSurfaceView by calling one or
76 * more of these methods before calling setRenderer:
77 * <ul>
78 * <li>{@link #setDebugFlags(int)}
79 * <li>{@link #setEGLConfigChooser(boolean)}
80 * <li>{@link #setEGLConfigChooser(EGLConfigChooser)}
81 * <li>{@link #setEGLConfigChooser(int, int, int, int, int, int)}
82 * <li>{@link #setGLWrapper(GLWrapper)}
83 * </ul>
84 * <p>
Jack Palevich83835352010-03-04 18:12:44 -080085 * <h4>Specifying the android.view.Surface</h4>
Mathias Agopian29e0bd22012-09-18 15:36:10 -070086 * By default GLSurfaceView will create a PixelFormat.RGB_888 format surface. If a translucent
Jack Palevich83835352010-03-04 18:12:44 -080087 * surface is required, call getHolder().setFormat(PixelFormat.TRANSLUCENT).
88 * The exact format of a TRANSLUCENT surface is device dependent, but it will be
89 * a 32-bit-per-pixel surface with 8 bits per component.
Jack Palevichb47c6412009-04-09 14:54:12 -070090 * <p>
Jack Palevich83835352010-03-04 18:12:44 -080091 * <h4>Choosing an EGL Configuration</h4>
92 * A given Android device may support multiple EGLConfig rendering configurations.
Aurimas Liutikas35ff3b32016-06-07 16:10:40 -070093 * The available configurations may differ in how many channels of data are present, as
Jack Palevich83835352010-03-04 18:12:44 -080094 * well as how many bits are allocated to each channel. Therefore, the first thing
95 * GLSurfaceView has to do when starting to render is choose what EGLConfig to use.
96 * <p>
Mathias Agopian29e0bd22012-09-18 15:36:10 -070097 * By default GLSurfaceView chooses a EGLConfig that has an RGB_888 pixel format,
Jack Palevich83835352010-03-04 18:12:44 -080098 * with at least a 16-bit depth buffer and no stencil.
99 * <p>
100 * If you would prefer a different EGLConfig
101 * you can override the default behavior by calling one of the
Jack Palevichb47c6412009-04-09 14:54:12 -0700102 * setEGLConfigChooser methods.
103 * <p>
104 * <h4>Debug Behavior</h4>
105 * You can optionally modify the behavior of GLSurfaceView by calling
106 * one or more of the debugging methods {@link #setDebugFlags(int)},
107 * and {@link #setGLWrapper}. These methods may be called before and/or after setRenderer, but
108 * typically they are called before setRenderer so that they take effect immediately.
109 * <p>
110 * <h4>Setting a Renderer</h4>
111 * Finally, you must call {@link #setRenderer} to register a {@link Renderer}.
112 * The renderer is
113 * responsible for doing the actual OpenGL rendering.
114 * <p>
115 * <h3>Rendering Mode</h3>
116 * Once the renderer is set, you can control whether the renderer draws
117 * continuously or on-demand by calling
118 * {@link #setRenderMode}. The default is continuous rendering.
119 * <p>
120 * <h3>Activity Life-cycle</h3>
John Reckcf890192016-06-29 13:35:27 -0700121 * A GLSurfaceView must be notified when to pause and resume rendering. GLSurfaceView clients
122 * are required to call {@link #onPause()} when the activity stops and
123 * {@link #onResume()} when the activity starts. These calls allow GLSurfaceView to
Jack Palevichb47c6412009-04-09 14:54:12 -0700124 * pause and resume the rendering thread, and also allow GLSurfaceView to release and recreate
125 * the OpenGL display.
126 * <p>
127 * <h3>Handling events</h3>
128 * <p>
129 * To handle an event you will typically subclass GLSurfaceView and override the
130 * appropriate method, just as you would with any other View. However, when handling
131 * the event, you may need to communicate with the Renderer object
132 * that's running in the rendering thread. You can do this using any
133 * standard Java cross-thread communication mechanism. In addition,
134 * one relatively easy way to communicate with your renderer is
135 * to call
136 * {@link #queueEvent(Runnable)}. For example:
137 * <pre class="prettyprint">
138 * class MyGLSurfaceView extends GLSurfaceView {
139 *
140 * private MyRenderer mMyRenderer;
141 *
142 * public void start() {
143 * mMyRenderer = ...;
144 * setRenderer(mMyRenderer);
145 * }
146 *
147 * public boolean onKeyDown(int keyCode, KeyEvent event) {
148 * if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
149 * queueEvent(new Runnable() {
150 * // This method will be called on the rendering
151 * // thread:
152 * public void run() {
153 * mMyRenderer.handleDpadCenter();
154 * }});
155 * return true;
156 * }
157 * return super.onKeyDown(keyCode, event);
158 * }
159 * }
160 * </pre>
161 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 */
Robert Carr2b3bf722016-01-07 17:50:48 -0800163public class GLSurfaceView extends SurfaceView implements SurfaceHolder.Callback2 {
Jack Palevich2ff6a822010-10-26 18:19:14 -0700164 private final static String TAG = "GLSurfaceView";
165 private final static boolean LOG_ATTACH_DETACH = false;
Jack Palevich57c6a462010-03-25 11:53:27 -0700166 private final static boolean LOG_THREADS = false;
Jeff Brownf48b0322013-04-19 17:13:33 -0700167 private final static boolean LOG_PAUSE_RESUME = false;
Jack Palevich4a659002010-03-27 15:02:59 -0700168 private final static boolean LOG_SURFACE = false;
169 private final static boolean LOG_RENDERER = false;
Jack Palevich4a2221e2010-03-06 10:39:27 -0800170 private final static boolean LOG_RENDERER_DRAW_FRAME = false;
Jack Palevich4a659002010-03-27 15:02:59 -0700171 private final static boolean LOG_EGL = false;
Jack Palevichb47c6412009-04-09 14:54:12 -0700172 /**
173 * The renderer only renders
174 * when the surface is created, or when {@link #requestRender} is called.
175 *
176 * @see #getRenderMode()
177 * @see #setRenderMode(int)
Jack Palevich840e9582010-01-18 15:13:12 +0800178 * @see #requestRender()
Jack Palevichb47c6412009-04-09 14:54:12 -0700179 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 public final static int RENDERMODE_WHEN_DIRTY = 0;
Jack Palevichb47c6412009-04-09 14:54:12 -0700181 /**
182 * The renderer is called
183 * continuously to re-render the scene.
184 *
185 * @see #getRenderMode()
186 * @see #setRenderMode(int)
Jack Palevichb47c6412009-04-09 14:54:12 -0700187 */
Jack Palevich3e832db2009-04-09 15:03:04 -0700188 public final static int RENDERMODE_CONTINUOUSLY = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189
190 /**
Jack Palevichb47c6412009-04-09 14:54:12 -0700191 * Check glError() after every GL call and throw an exception if glError indicates
192 * that an error has occurred. This can be used to help track down which OpenGL ES call
193 * is causing an error.
194 *
195 * @see #getDebugFlags
196 * @see #setDebugFlags
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 */
198 public final static int DEBUG_CHECK_GL_ERROR = 1;
199
200 /**
201 * Log GL calls to the system log at "verbose" level with tag "GLSurfaceView".
Jack Palevichb47c6412009-04-09 14:54:12 -0700202 *
203 * @see #getDebugFlags
204 * @see #setDebugFlags
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 */
206 public final static int DEBUG_LOG_GL_CALLS = 2;
207
Jack Palevichb47c6412009-04-09 14:54:12 -0700208 /**
209 * Standard View constructor. In order to render something, you
210 * must call {@link #setRenderer} to register a renderer.
211 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 public GLSurfaceView(Context context) {
213 super(context);
214 init();
215 }
216
Jack Palevichb47c6412009-04-09 14:54:12 -0700217 /**
218 * Standard View constructor. In order to render something, you
219 * must call {@link #setRenderer} to register a renderer.
220 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 public GLSurfaceView(Context context, AttributeSet attrs) {
222 super(context, attrs);
223 init();
224 }
225
Jack Palevich80b3cd62011-11-15 14:51:27 -0800226 @Override
227 protected void finalize() throws Throwable {
228 try {
229 if (mGLThread != null) {
230 // GLThread may still be running if this view was never
231 // attached to a window.
Jeff Brownf48b0322013-04-19 17:13:33 -0700232 mGLThread.requestExitAndWait();
Jack Palevich80b3cd62011-11-15 14:51:27 -0800233 }
234 } finally {
235 super.finalize();
236 }
237 }
238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 private void init() {
240 // Install a SurfaceHolder.Callback so we get notified when the
241 // underlying surface is created and destroyed
242 SurfaceHolder holder = getHolder();
243 holder.addCallback(this);
Jeff Brownf48b0322013-04-19 17:13:33 -0700244 // setFormat is done by SurfaceView in SDK 2.3 and newer. Uncomment
245 // this statement if back-porting to 2.2 or older:
246 // holder.setFormat(PixelFormat.RGB_565);
247 //
248 // setType is not needed for SDK 2.0 or newer. Uncomment this
249 // statement if back-porting this code to older SDKs.
250 // holder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 }
252
253 /**
Jack Palevichb47c6412009-04-09 14:54:12 -0700254 * Set the glWrapper. If the glWrapper is not null, its
255 * {@link GLWrapper#wrap(GL)} method is called
256 * whenever a surface is created. A GLWrapper can be used to wrap
257 * the GL object that's passed to the renderer. Wrapping a GL
258 * object enables examining and modifying the behavior of the
259 * GL calls made by the renderer.
260 * <p>
261 * Wrapping is typically used for debugging purposes.
262 * <p>
263 * The default value is null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 * @param glWrapper the new GLWrapper
265 */
266 public void setGLWrapper(GLWrapper glWrapper) {
267 mGLWrapper = glWrapper;
268 }
269
270 /**
Jack Palevichb47c6412009-04-09 14:54:12 -0700271 * Set the debug flags to a new value. The value is
272 * constructed by OR-together zero or more
273 * of the DEBUG_CHECK_* constants. The debug flags take effect
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 * whenever a surface is created. The default value is zero.
275 * @param debugFlags the new debug flags
276 * @see #DEBUG_CHECK_GL_ERROR
277 * @see #DEBUG_LOG_GL_CALLS
278 */
279 public void setDebugFlags(int debugFlags) {
280 mDebugFlags = debugFlags;
281 }
282
Jack Palevichb47c6412009-04-09 14:54:12 -0700283 /**
284 * Get the current value of the debug flags.
285 * @return the current value of the debug flags.
286 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 public int getDebugFlags() {
288 return mDebugFlags;
289 }
290
291 /**
Jack Palevich8432b3f2011-01-14 11:45:17 -0800292 * Control whether the EGL context is preserved when the GLSurfaceView is paused and
293 * resumed.
294 * <p>
295 * If set to true, then the EGL context may be preserved when the GLSurfaceView is paused.
John Reckcf890192016-06-29 13:35:27 -0700296 * <p>
297 * Prior to API level 11, whether the EGL context is actually preserved or not
298 * depends upon whether the Android device can support an arbitrary number of
299 * EGL contexts or not. Devices that can only support a limited number of EGL
300 * contexts must release the EGL context in order to allow multiple applications
301 * to share the GPU.
Jack Palevich8432b3f2011-01-14 11:45:17 -0800302 * <p>
303 * If set to false, the EGL context will be released when the GLSurfaceView is paused,
304 * and recreated when the GLSurfaceView is resumed.
305 * <p>
306 *
307 * The default is false.
308 *
309 * @param preserveOnPause preserve the EGL context when paused
310 */
311 public void setPreserveEGLContextOnPause(boolean preserveOnPause) {
312 mPreserveEGLContextOnPause = preserveOnPause;
313 }
314
315 /**
Jack Palevich8432b3f2011-01-14 11:45:17 -0800316 * @return true if the EGL context will be preserved when paused
317 */
318 public boolean getPreserveEGLContextOnPause() {
319 return mPreserveEGLContextOnPause;
320 }
321
322 /**
Jack Palevichb47c6412009-04-09 14:54:12 -0700323 * Set the renderer associated with this view. Also starts the thread that
324 * will call the renderer, which in turn causes the rendering to start.
325 * <p>This method should be called once and only once in the life-cycle of
326 * a GLSurfaceView.
327 * <p>The following GLSurfaceView methods can only be called <em>before</em>
328 * setRenderer is called:
329 * <ul>
330 * <li>{@link #setEGLConfigChooser(boolean)}
331 * <li>{@link #setEGLConfigChooser(EGLConfigChooser)}
332 * <li>{@link #setEGLConfigChooser(int, int, int, int, int, int)}
333 * </ul>
334 * <p>
335 * The following GLSurfaceView methods can only be called <em>after</em>
336 * setRenderer is called:
337 * <ul>
338 * <li>{@link #getRenderMode()}
339 * <li>{@link #onPause()}
340 * <li>{@link #onResume()}
341 * <li>{@link #queueEvent(Runnable)}
342 * <li>{@link #requestRender()}
343 * <li>{@link #setRenderMode(int)}
344 * </ul>
345 *
346 * @param renderer the renderer to use to perform OpenGL drawing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 */
348 public void setRenderer(Renderer renderer) {
Jack Palevich15e1c6d2009-09-21 19:19:25 -0700349 checkRenderThreadState();
Jack Palevicha605a512009-03-25 15:16:50 -0700350 if (mEGLConfigChooser == null) {
Jeff Brownf48b0322013-04-19 17:13:33 -0700351 mEGLConfigChooser = new SimpleEGLConfigChooser(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 }
Jack Palevich15e1c6d2009-09-21 19:19:25 -0700353 if (mEGLContextFactory == null) {
Jeff Brownf48b0322013-04-19 17:13:33 -0700354 mEGLContextFactory = new DefaultContextFactory();
Jack Palevich15e1c6d2009-09-21 19:19:25 -0700355 }
356 if (mEGLWindowSurfaceFactory == null) {
357 mEGLWindowSurfaceFactory = new DefaultWindowSurfaceFactory();
358 }
Jack Palevich2ff6a822010-10-26 18:19:14 -0700359 mRenderer = renderer;
Jack Palevich80b3cd62011-11-15 14:51:27 -0800360 mGLThread = new GLThread(mThisWeakRef);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 mGLThread.start();
362 }
363
364 /**
Jack Palevich15e1c6d2009-09-21 19:19:25 -0700365 * Install a custom EGLContextFactory.
366 * <p>If this method is
367 * called, it must be called before {@link #setRenderer(Renderer)}
368 * is called.
369 * <p>
370 * If this method is not called, then by default
371 * a context will be created with no shared context and
372 * with a null attribute list.
373 */
Jack Palevicha8fecb82009-09-23 14:48:07 -0700374 public void setEGLContextFactory(EGLContextFactory factory) {
Jack Palevich15e1c6d2009-09-21 19:19:25 -0700375 checkRenderThreadState();
376 mEGLContextFactory = factory;
377 }
378
379 /**
Jack Palevich15e1c6d2009-09-21 19:19:25 -0700380 * Install a custom EGLWindowSurfaceFactory.
381 * <p>If this method is
382 * called, it must be called before {@link #setRenderer(Renderer)}
383 * is called.
384 * <p>
385 * If this method is not called, then by default
386 * a window surface will be created with a null attribute list.
387 */
Jack Palevicha8fecb82009-09-23 14:48:07 -0700388 public void setEGLWindowSurfaceFactory(EGLWindowSurfaceFactory factory) {
Jack Palevich15e1c6d2009-09-21 19:19:25 -0700389 checkRenderThreadState();
390 mEGLWindowSurfaceFactory = factory;
391 }
392
393 /**
Jack Palevichb47c6412009-04-09 14:54:12 -0700394 * Install a custom EGLConfigChooser.
395 * <p>If this method is
396 * called, it must be called before {@link #setRenderer(Renderer)}
Jack Palevicha605a512009-03-25 15:16:50 -0700397 * is called.
398 * <p>
Jack Palevichb47c6412009-04-09 14:54:12 -0700399 * If no setEGLConfigChooser method is called, then by default the
Jack Palevich83835352010-03-04 18:12:44 -0800400 * view will choose an EGLConfig that is compatible with the current
401 * android.view.Surface, with a depth buffer depth of
402 * at least 16 bits.
Jack Palevicha605a512009-03-25 15:16:50 -0700403 * @param configChooser
404 */
405 public void setEGLConfigChooser(EGLConfigChooser configChooser) {
Jack Palevich15e1c6d2009-09-21 19:19:25 -0700406 checkRenderThreadState();
Jack Palevicha605a512009-03-25 15:16:50 -0700407 mEGLConfigChooser = configChooser;
408 }
409
410 /**
Jack Palevichb47c6412009-04-09 14:54:12 -0700411 * Install a config chooser which will choose a config
412 * as close to 16-bit RGB as possible, with or without an optional depth
413 * buffer as close to 16-bits as possible.
414 * <p>If this method is
Jack Palevicha605a512009-03-25 15:16:50 -0700415 * called, it must be called before {@link #setRenderer(Renderer)}
416 * is called.
417 * <p>
Jack Palevich83835352010-03-04 18:12:44 -0800418 * If no setEGLConfigChooser method is called, then by default the
Mathias Agopian29e0bd22012-09-18 15:36:10 -0700419 * view will choose an RGB_888 surface with a depth buffer depth of
Jack Palevich83835352010-03-04 18:12:44 -0800420 * at least 16 bits.
Jack Palevicha605a512009-03-25 15:16:50 -0700421 *
422 * @param needDepth
423 */
424 public void setEGLConfigChooser(boolean needDepth) {
Jeff Brownf48b0322013-04-19 17:13:33 -0700425 setEGLConfigChooser(new SimpleEGLConfigChooser(needDepth));
Jack Palevicha605a512009-03-25 15:16:50 -0700426 }
427
428 /**
Jack Palevichb47c6412009-04-09 14:54:12 -0700429 * Install a config chooser which will choose a config
Jack Palevich83835352010-03-04 18:12:44 -0800430 * with at least the specified depthSize and stencilSize,
431 * and exactly the specified redSize, greenSize, blueSize and alphaSize.
Jack Palevichb47c6412009-04-09 14:54:12 -0700432 * <p>If this method is
Jack Palevicha605a512009-03-25 15:16:50 -0700433 * called, it must be called before {@link #setRenderer(Renderer)}
434 * is called.
435 * <p>
Jack Palevichb47c6412009-04-09 14:54:12 -0700436 * If no setEGLConfigChooser method is called, then by default the
Mathias Agopian29e0bd22012-09-18 15:36:10 -0700437 * view will choose an RGB_888 surface with a depth buffer depth of
Jack Palevich83835352010-03-04 18:12:44 -0800438 * at least 16 bits.
Jack Palevicha605a512009-03-25 15:16:50 -0700439 *
440 */
441 public void setEGLConfigChooser(int redSize, int greenSize, int blueSize,
442 int alphaSize, int depthSize, int stencilSize) {
443 setEGLConfigChooser(new ComponentSizeChooser(redSize, greenSize,
Jeff Brownf48b0322013-04-19 17:13:33 -0700444 blueSize, alphaSize, depthSize, stencilSize));
Jack Palevicha605a512009-03-25 15:16:50 -0700445 }
Jack Palevich6ae4da72009-11-19 19:08:52 +0800446
447 /**
448 * Inform the default EGLContextFactory and default EGLConfigChooser
449 * which EGLContext client version to pick.
450 * <p>Use this method to create an OpenGL ES 2.0-compatible context.
451 * Example:
452 * <pre class="prettyprint">
453 * public MyView(Context context) {
454 * super(context);
455 * setEGLContextClientVersion(2); // Pick an OpenGL ES 2.0 context.
456 * setRenderer(new MyRenderer());
457 * }
458 * </pre>
459 * <p>Note: Activities which require OpenGL ES 2.0 should indicate this by
460 * setting @lt;uses-feature android:glEsVersion="0x00020000" /> in the activity's
461 * AndroidManifest.xml file.
462 * <p>If this method is called, it must be called before {@link #setRenderer(Renderer)}
463 * is called.
464 * <p>This method only affects the behavior of the default EGLContexFactory and the
465 * default EGLConfigChooser. If
466 * {@link #setEGLContextFactory(EGLContextFactory)} has been called, then the supplied
467 * EGLContextFactory is responsible for creating an OpenGL ES 2.0-compatible context.
468 * If
469 * {@link #setEGLConfigChooser(EGLConfigChooser)} has been called, then the supplied
470 * EGLConfigChooser is responsible for choosing an OpenGL ES 2.0-compatible config.
471 * @param version The EGLContext client version to choose. Use 2 for OpenGL ES 2.0
Jack Palevich6ae4da72009-11-19 19:08:52 +0800472 */
473 public void setEGLContextClientVersion(int version) {
474 checkRenderThreadState();
475 mEGLContextClientVersion = version;
476 }
477
Jack Palevicha605a512009-03-25 15:16:50 -0700478 /**
Jack Palevichb47c6412009-04-09 14:54:12 -0700479 * Set the rendering mode. When renderMode is
Jack Palevich3e832db2009-04-09 15:03:04 -0700480 * RENDERMODE_CONTINUOUSLY, the renderer is called
Jack Palevichb47c6412009-04-09 14:54:12 -0700481 * repeatedly to re-render the scene. When renderMode
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 * is RENDERMODE_WHEN_DIRTY, the renderer only rendered when the surface
Jack Palevich3e832db2009-04-09 15:03:04 -0700483 * is created, or when {@link #requestRender} is called. Defaults to RENDERMODE_CONTINUOUSLY.
Jack Palevichb47c6412009-04-09 14:54:12 -0700484 * <p>
485 * Using RENDERMODE_WHEN_DIRTY can improve battery life and overall system performance
486 * by allowing the GPU and CPU to idle when the view does not need to be updated.
487 * <p>
488 * This method can only be called after {@link #setRenderer(Renderer)}
489 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 * @param renderMode one of the RENDERMODE_X constants
Jack Palevich3e832db2009-04-09 15:03:04 -0700491 * @see #RENDERMODE_CONTINUOUSLY
Jack Palevichb47c6412009-04-09 14:54:12 -0700492 * @see #RENDERMODE_WHEN_DIRTY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 */
494 public void setRenderMode(int renderMode) {
495 mGLThread.setRenderMode(renderMode);
496 }
497
498 /**
499 * Get the current rendering mode. May be called
500 * from any thread. Must not be called before a renderer has been set.
Jack Palevichb47c6412009-04-09 14:54:12 -0700501 * @return the current rendering mode.
Jack Palevich3e832db2009-04-09 15:03:04 -0700502 * @see #RENDERMODE_CONTINUOUSLY
Jack Palevichb47c6412009-04-09 14:54:12 -0700503 * @see #RENDERMODE_WHEN_DIRTY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 */
505 public int getRenderMode() {
Mathias Agopian78bc2032013-04-18 13:52:58 -0700506 return mGLThread.getRenderMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 }
508
509 /**
Jack Palevichb47c6412009-04-09 14:54:12 -0700510 * Request that the renderer render a frame.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 * This method is typically used when the render mode has been set to
Jack Palevichb47c6412009-04-09 14:54:12 -0700512 * {@link #RENDERMODE_WHEN_DIRTY}, so that frames are only rendered on demand.
513 * May be called
514 * from any thread. Must not be called before a renderer has been set.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 */
516 public void requestRender() {
517 mGLThread.requestRender();
518 }
519
Jack Palevichb47c6412009-04-09 14:54:12 -0700520 /**
521 * This method is part of the SurfaceHolder.Callback interface, and is
522 * not normally called or subclassed by clients of GLSurfaceView.
523 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 public void surfaceCreated(SurfaceHolder holder) {
525 mGLThread.surfaceCreated();
526 }
527
Jack Palevichb47c6412009-04-09 14:54:12 -0700528 /**
529 * This method is part of the SurfaceHolder.Callback interface, and is
530 * not normally called or subclassed by clients of GLSurfaceView.
531 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 public void surfaceDestroyed(SurfaceHolder holder) {
533 // Surface will be destroyed when we return
534 mGLThread.surfaceDestroyed();
535 }
536
Jack Palevichb47c6412009-04-09 14:54:12 -0700537 /**
538 * This method is part of the SurfaceHolder.Callback interface, and is
539 * not normally called or subclassed by clients of GLSurfaceView.
540 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
542 mGLThread.onWindowResize(w, h);
543 }
544
545 /**
Robert Carr25cfa132016-11-16 13:24:09 -0800546 * This method is part of the SurfaceHolder.Callback2 interface, and is
Robert Carr2b3bf722016-01-07 17:50:48 -0800547 * not normally called or subclassed by clients of GLSurfaceView.
548 */
549 @Override
Robert Carr25cfa132016-11-16 13:24:09 -0800550 public void surfaceRedrawNeededAsync(SurfaceHolder holder, Runnable finishDrawing) {
John Reckfd3cee12016-04-18 13:26:48 -0700551 if (mGLThread != null) {
Robert Carr25cfa132016-11-16 13:24:09 -0800552 mGLThread.requestRenderAndNotify(finishDrawing);
John Reckfd3cee12016-04-18 13:26:48 -0700553 }
Robert Carr2b3bf722016-01-07 17:50:48 -0800554 }
555
Robert Carr25cfa132016-11-16 13:24:09 -0800556 /**
557 * This method is part of the SurfaceHolder.Callback2 interface, and is
558 * not normally called or subclassed by clients of GLSurfaceView.
559 */
560 @Deprecated
561 @Override
562 public void surfaceRedrawNeeded(SurfaceHolder holder) {
563 // Since we are part of the framework we know only surfaceRedrawNeededAsync
564 // will be called.
565 }
566
Robert Carr2b3bf722016-01-07 17:50:48 -0800567
568 /**
John Reckcf890192016-06-29 13:35:27 -0700569 * Pause the rendering thread, optionally tearing down the EGL context
570 * depending upon the value of {@link #setPreserveEGLContextOnPause(boolean)}.
571 *
572 * This method should be called when it is no longer desirable for the
573 * GLSurfaceView to continue rendering, such as in response to
574 * {@link android.app.Activity#onStop Activity.onStop}.
575 *
Jack Palevichb47c6412009-04-09 14:54:12 -0700576 * Must not be called before a renderer has been set.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 */
578 public void onPause() {
579 mGLThread.onPause();
580 }
581
582 /**
John Reckcf890192016-06-29 13:35:27 -0700583 * Resumes the rendering thread, re-creating the OpenGL context if necessary. It
584 * is the counterpart to {@link #onPause()}.
585 *
586 * This method should typically be called in
587 * {@link android.app.Activity#onStart Activity.onStart}.
588 *
Jack Palevichb47c6412009-04-09 14:54:12 -0700589 * Must not be called before a renderer has been set.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 */
591 public void onResume() {
592 mGLThread.onResume();
593 }
594
595 /**
Jack Palevichb47c6412009-04-09 14:54:12 -0700596 * Queue a runnable to be run on the GL rendering thread. This can be used
597 * to communicate with the Renderer on the rendering thread.
598 * Must not be called before a renderer has been set.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 * @param r the runnable to be run on the GL rendering thread.
600 */
601 public void queueEvent(Runnable r) {
602 mGLThread.queueEvent(r);
603 }
604
Jack Palevichb47c6412009-04-09 14:54:12 -0700605 /**
606 * This method is used as part of the View class and is not normally
607 * called or subclassed by clients of GLSurfaceView.
Jack Palevich2ff6a822010-10-26 18:19:14 -0700608 */
609 @Override
610 protected void onAttachedToWindow() {
611 super.onAttachedToWindow();
612 if (LOG_ATTACH_DETACH) {
613 Log.d(TAG, "onAttachedToWindow reattach =" + mDetached);
614 }
615 if (mDetached && (mRenderer != null)) {
Jeff Brownf48b0322013-04-19 17:13:33 -0700616 int renderMode = RENDERMODE_CONTINUOUSLY;
617 if (mGLThread != null) {
618 renderMode = mGLThread.getRenderMode();
619 }
Jack Palevich80b3cd62011-11-15 14:51:27 -0800620 mGLThread = new GLThread(mThisWeakRef);
Jeff Brownf48b0322013-04-19 17:13:33 -0700621 if (renderMode != RENDERMODE_CONTINUOUSLY) {
622 mGLThread.setRenderMode(renderMode);
623 }
Jack Palevich2ff6a822010-10-26 18:19:14 -0700624 mGLThread.start();
625 }
626 mDetached = false;
627 }
628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 @Override
John Reck77e4a522014-10-01 10:38:07 -0700630 protected void onDetachedFromWindow() {
Jack Palevich2ff6a822010-10-26 18:19:14 -0700631 if (LOG_ATTACH_DETACH) {
632 Log.d(TAG, "onDetachedFromWindow");
633 }
634 if (mGLThread != null) {
Jeff Brownf48b0322013-04-19 17:13:33 -0700635 mGLThread.requestExitAndWait();
Jack Palevich2ff6a822010-10-26 18:19:14 -0700636 }
637 mDetached = true;
John Reck77e4a522014-10-01 10:38:07 -0700638 super.onDetachedFromWindow();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 }
640
641 // ----------------------------------------------------------------------
642
Jack Palevichb47c6412009-04-09 14:54:12 -0700643 /**
644 * An interface used to wrap a GL interface.
645 * <p>Typically
646 * used for implementing debugging and tracing on top of the default
647 * GL interface. You would typically use this by creating your own class
648 * that implemented all the GL methods by delegating to another GL instance.
649 * Then you could add your own behavior before or after calling the
650 * delegate. All the GLWrapper would do was instantiate and return the
651 * wrapper GL instance:
652 * <pre class="prettyprint">
653 * class MyGLWrapper implements GLWrapper {
654 * GL wrap(GL gl) {
655 * return new MyGLImplementation(gl);
656 * }
657 * static class MyGLImplementation implements GL,GL10,GL11,... {
658 * ...
659 * }
660 * }
661 * </pre>
662 * @see #setGLWrapper(GLWrapper)
663 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 public interface GLWrapper {
Jack Palevichb47c6412009-04-09 14:54:12 -0700665 /**
666 * Wraps a gl interface in another gl interface.
667 * @param gl a GL interface that is to be wrapped.
668 * @return either the input argument or another GL object that wraps the input argument.
669 */
670 GL wrap(GL gl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 }
672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 /**
674 * A generic renderer interface.
Jack Palevichb47c6412009-04-09 14:54:12 -0700675 * <p>
676 * The renderer is responsible for making OpenGL calls to render a frame.
677 * <p>
678 * GLSurfaceView clients typically create their own classes that implement
679 * this interface, and then call {@link GLSurfaceView#setRenderer} to
680 * register the renderer with the GLSurfaceView.
681 * <p>
Joe Fernandez61fd1e82011-10-26 13:39:11 -0700682 *
683 * <div class="special reference">
684 * <h3>Developer Guides</h3>
685 * <p>For more information about how to use OpenGL, read the
686 * <a href="{@docRoot}guide/topics/graphics/opengl.html">OpenGL</a> developer guide.</p>
687 * </div>
688 *
Jack Palevichb47c6412009-04-09 14:54:12 -0700689 * <h3>Threading</h3>
690 * The renderer will be called on a separate thread, so that rendering
691 * performance is decoupled from the UI thread. Clients typically need to
692 * communicate with the renderer from the UI thread, because that's where
693 * input events are received. Clients can communicate using any of the
694 * standard Java techniques for cross-thread communication, or they can
695 * use the {@link GLSurfaceView#queueEvent(Runnable)} convenience method.
696 * <p>
697 * <h3>EGL Context Lost</h3>
698 * There are situations where the EGL rendering context will be lost. This
699 * typically happens when device wakes up after going to sleep. When
700 * the EGL context is lost, all OpenGL resources (such as textures) that are
701 * associated with that context will be automatically deleted. In order to
702 * keep rendering correctly, a renderer must recreate any lost resources
703 * that it still needs. The {@link #onSurfaceCreated(GL10, EGLConfig)} method
704 * is a convenient place to do this.
705 *
706 *
707 * @see #setRenderer(Renderer)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 */
709 public interface Renderer {
710 /**
Jack Palevichb47c6412009-04-09 14:54:12 -0700711 * Called when the surface is created or recreated.
712 * <p>
713 * Called when the rendering thread
Jack Palevich840e9582010-01-18 15:13:12 +0800714 * starts and whenever the EGL context is lost. The EGL context will typically
Jack Palevichb47c6412009-04-09 14:54:12 -0700715 * be lost when the Android device awakes after going to sleep.
716 * <p>
717 * Since this method is called at the beginning of rendering, as well as
718 * every time the EGL context is lost, this method is a convenient place to put
719 * code to create resources that need to be created when the rendering
720 * starts, and that need to be recreated when the EGL context is lost.
721 * Textures are an example of a resource that you might want to create
722 * here.
723 * <p>
724 * Note that when the EGL context is lost, all OpenGL resources associated
725 * with that context will be automatically deleted. You do not need to call
726 * the corresponding "glDelete" methods such as glDeleteTextures to
727 * manually delete these lost resources.
728 * <p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 * @param gl the GL interface. Use <code>instanceof</code> to
730 * test if the interface supports GL11 or higher interfaces.
731 * @param config the EGLConfig of the created surface. Can be used
732 * to create matching pbuffers.
733 */
734 void onSurfaceCreated(GL10 gl, EGLConfig config);
Jack Palevichb47c6412009-04-09 14:54:12 -0700735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 /**
Jack Palevichb47c6412009-04-09 14:54:12 -0700737 * Called when the surface changed size.
738 * <p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 * Called after the surface is created and whenever
Jack Palevichb47c6412009-04-09 14:54:12 -0700740 * the OpenGL ES surface size changes.
741 * <p>
742 * Typically you will set your viewport here. If your camera
743 * is fixed then you could also set your projection matrix here:
744 * <pre class="prettyprint">
745 * void onSurfaceChanged(GL10 gl, int width, int height) {
746 * gl.glViewport(0, 0, width, height);
747 * // for a fixed camera, set the projection too
748 * float ratio = (float) width / height;
749 * gl.glMatrixMode(GL10.GL_PROJECTION);
750 * gl.glLoadIdentity();
751 * gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
752 * }
753 * </pre>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 * @param gl the GL interface. Use <code>instanceof</code> to
755 * test if the interface supports GL11 or higher interfaces.
756 * @param width
757 * @param height
758 */
759 void onSurfaceChanged(GL10 gl, int width, int height);
Jack Palevichb47c6412009-04-09 14:54:12 -0700760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 /**
Jack Palevichb47c6412009-04-09 14:54:12 -0700762 * Called to draw the current frame.
763 * <p>
764 * This method is responsible for drawing the current frame.
765 * <p>
766 * The implementation of this method typically looks like this:
767 * <pre class="prettyprint">
768 * void onDrawFrame(GL10 gl) {
769 * gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
770 * //... other gl calls to render the scene ...
771 * }
772 * </pre>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 * @param gl the GL interface. Use <code>instanceof</code> to
774 * test if the interface supports GL11 or higher interfaces.
775 */
776 void onDrawFrame(GL10 gl);
777 }
778
779 /**
Jack Palevich15e1c6d2009-09-21 19:19:25 -0700780 * An interface for customizing the eglCreateContext and eglDestroyContext calls.
781 * <p>
782 * This interface must be implemented by clients wishing to call
783 * {@link GLSurfaceView#setEGLContextFactory(EGLContextFactory)}
784 */
785 public interface EGLContextFactory {
786 EGLContext createContext(EGL10 egl, EGLDisplay display, EGLConfig eglConfig);
787 void destroyContext(EGL10 egl, EGLDisplay display, EGLContext context);
788 }
789
Jeff Brownf48b0322013-04-19 17:13:33 -0700790 private class DefaultContextFactory implements EGLContextFactory {
791 private int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
Jack Palevich15e1c6d2009-09-21 19:19:25 -0700792
793 public EGLContext createContext(EGL10 egl, EGLDisplay display, EGLConfig config) {
Jeff Brownf48b0322013-04-19 17:13:33 -0700794 int[] attrib_list = {EGL_CONTEXT_CLIENT_VERSION, mEGLContextClientVersion,
Jack Palevich6ae4da72009-11-19 19:08:52 +0800795 EGL10.EGL_NONE };
796
797 return egl.eglCreateContext(display, config, EGL10.EGL_NO_CONTEXT,
798 mEGLContextClientVersion != 0 ? attrib_list : null);
Jack Palevich15e1c6d2009-09-21 19:19:25 -0700799 }
800
801 public void destroyContext(EGL10 egl, EGLDisplay display,
802 EGLContext context) {
Jack Palevich07353a12010-03-23 15:22:03 -0700803 if (!egl.eglDestroyContext(display, context)) {
Jeff Brownf48b0322013-04-19 17:13:33 -0700804 Log.e("DefaultContextFactory", "display:" + display + " context: " + context);
Jack Palevich07353a12010-03-23 15:22:03 -0700805 if (LOG_THREADS) {
Jeff Brownf48b0322013-04-19 17:13:33 -0700806 Log.i("DefaultContextFactory", "tid=" + Thread.currentThread().getId());
Jack Palevich07353a12010-03-23 15:22:03 -0700807 }
Jack Palevich7d73df82012-03-29 16:24:58 -0700808 EglHelper.throwEglException("eglDestroyContex", egl.eglGetError());
Jack Palevich07353a12010-03-23 15:22:03 -0700809 }
Jack Palevich15e1c6d2009-09-21 19:19:25 -0700810 }
811 }
812
813 /**
Jack Palevich15e1c6d2009-09-21 19:19:25 -0700814 * An interface for customizing the eglCreateWindowSurface and eglDestroySurface calls.
815 * <p>
816 * This interface must be implemented by clients wishing to call
Jack Palevichd40dfbb2009-09-24 14:10:17 -0700817 * {@link GLSurfaceView#setEGLWindowSurfaceFactory(EGLWindowSurfaceFactory)}
Jack Palevich15e1c6d2009-09-21 19:19:25 -0700818 */
819 public interface EGLWindowSurfaceFactory {
Jack Palevicha35c1202011-10-14 07:34:05 -0700820 /**
821 * @return null if the surface cannot be constructed.
822 */
Jack Palevich15e1c6d2009-09-21 19:19:25 -0700823 EGLSurface createWindowSurface(EGL10 egl, EGLDisplay display, EGLConfig config,
824 Object nativeWindow);
825 void destroySurface(EGL10 egl, EGLDisplay display, EGLSurface surface);
826 }
827
828 private static class DefaultWindowSurfaceFactory implements EGLWindowSurfaceFactory {
829
830 public EGLSurface createWindowSurface(EGL10 egl, EGLDisplay display,
831 EGLConfig config, Object nativeWindow) {
Jack Palevicha35c1202011-10-14 07:34:05 -0700832 EGLSurface result = null;
833 try {
834 result = egl.eglCreateWindowSurface(display, config, nativeWindow, null);
835 } catch (IllegalArgumentException e) {
Jeff Brownf48b0322013-04-19 17:13:33 -0700836 // This exception indicates that the surface flinger surface
837 // is not valid. This can happen if the surface flinger surface has
Jack Palevicha35c1202011-10-14 07:34:05 -0700838 // been torn down, but the application has not yet been
839 // notified via SurfaceHolder.Callback.surfaceDestroyed.
840 // In theory the application should be notified first,
841 // but in practice sometimes it is not. See b/4588890
842 Log.e(TAG, "eglCreateWindowSurface", e);
843 }
844 return result;
Jack Palevich15e1c6d2009-09-21 19:19:25 -0700845 }
846
847 public void destroySurface(EGL10 egl, EGLDisplay display,
848 EGLSurface surface) {
849 egl.eglDestroySurface(display, surface);
850 }
851 }
852
853 /**
Jack Palevichb47c6412009-04-09 14:54:12 -0700854 * An interface for choosing an EGLConfig configuration from a list of
Jack Palevicha605a512009-03-25 15:16:50 -0700855 * potential configurations.
Jack Palevichb47c6412009-04-09 14:54:12 -0700856 * <p>
857 * This interface must be implemented by clients wishing to call
858 * {@link GLSurfaceView#setEGLConfigChooser(EGLConfigChooser)}
Jack Palevicha605a512009-03-25 15:16:50 -0700859 */
860 public interface EGLConfigChooser {
861 /**
862 * Choose a configuration from the list. Implementors typically
863 * implement this method by calling
Jack Palevichb47c6412009-04-09 14:54:12 -0700864 * {@link EGL10#eglChooseConfig} and iterating through the results. Please consult the
865 * EGL specification available from The Khronos Group to learn how to call eglChooseConfig.
Jack Palevicha605a512009-03-25 15:16:50 -0700866 * @param egl the EGL10 for the current display.
867 * @param display the current display.
868 * @return the chosen configuration.
869 */
870 EGLConfig chooseConfig(EGL10 egl, EGLDisplay display);
871 }
872
Jeff Brownf48b0322013-04-19 17:13:33 -0700873 private abstract class BaseConfigChooser
Jack Palevicha605a512009-03-25 15:16:50 -0700874 implements EGLConfigChooser {
Jeff Brownf48b0322013-04-19 17:13:33 -0700875 public BaseConfigChooser(int[] configSpec) {
876 mConfigSpec = filterConfigSpec(configSpec);
Jack Palevicha605a512009-03-25 15:16:50 -0700877 }
Jack Palevich6ae4da72009-11-19 19:08:52 +0800878
Jack Palevicha605a512009-03-25 15:16:50 -0700879 public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) {
880 int[] num_config = new int[1];
Jack Palevich28023912009-10-13 19:52:06 -0700881 if (!egl.eglChooseConfig(display, mConfigSpec, null, 0,
882 num_config)) {
883 throw new IllegalArgumentException("eglChooseConfig failed");
884 }
Jack Palevicha605a512009-03-25 15:16:50 -0700885
886 int numConfigs = num_config[0];
887
888 if (numConfigs <= 0) {
889 throw new IllegalArgumentException(
890 "No configs match configSpec");
891 }
892
893 EGLConfig[] configs = new EGLConfig[numConfigs];
Jack Palevich28023912009-10-13 19:52:06 -0700894 if (!egl.eglChooseConfig(display, mConfigSpec, configs, numConfigs,
895 num_config)) {
896 throw new IllegalArgumentException("eglChooseConfig#2 failed");
897 }
Jack Palevicha605a512009-03-25 15:16:50 -0700898 EGLConfig config = chooseConfig(egl, display, configs);
899 if (config == null) {
900 throw new IllegalArgumentException("No config chosen");
901 }
902 return config;
903 }
904
905 abstract EGLConfig chooseConfig(EGL10 egl, EGLDisplay display,
906 EGLConfig[] configs);
907
908 protected int[] mConfigSpec;
Jack Palevich6ae4da72009-11-19 19:08:52 +0800909
Jeff Brownf48b0322013-04-19 17:13:33 -0700910 private int[] filterConfigSpec(int[] configSpec) {
Jin Zhebin28a5c592014-01-21 15:27:20 +0800911 if (mEGLContextClientVersion != 2 && mEGLContextClientVersion != 3) {
Jack Palevich6ae4da72009-11-19 19:08:52 +0800912 return configSpec;
913 }
914 /* We know none of the subclasses define EGL_RENDERABLE_TYPE.
915 * And we know the configSpec is well formed.
916 */
917 int len = configSpec.length;
918 int[] newConfigSpec = new int[len + 2];
919 System.arraycopy(configSpec, 0, newConfigSpec, 0, len-1);
920 newConfigSpec[len-1] = EGL10.EGL_RENDERABLE_TYPE;
Jin Zhebin28a5c592014-01-21 15:27:20 +0800921 if (mEGLContextClientVersion == 2) {
922 newConfigSpec[len] = EGL14.EGL_OPENGL_ES2_BIT; /* EGL_OPENGL_ES2_BIT */
923 } else {
924 newConfigSpec[len] = EGLExt.EGL_OPENGL_ES3_BIT_KHR; /* EGL_OPENGL_ES3_BIT_KHR */
925 }
Jack Palevich6ae4da72009-11-19 19:08:52 +0800926 newConfigSpec[len+1] = EGL10.EGL_NONE;
927 return newConfigSpec;
928 }
Jack Palevicha605a512009-03-25 15:16:50 -0700929 }
930
Jack Palevich83835352010-03-04 18:12:44 -0800931 /**
932 * Choose a configuration with exactly the specified r,g,b,a sizes,
933 * and at least the specified depth and stencil sizes.
934 */
Jeff Brownf48b0322013-04-19 17:13:33 -0700935 private class ComponentSizeChooser extends BaseConfigChooser {
Jack Palevicha605a512009-03-25 15:16:50 -0700936 public ComponentSizeChooser(int redSize, int greenSize, int blueSize,
Jeff Brownf48b0322013-04-19 17:13:33 -0700937 int alphaSize, int depthSize, int stencilSize) {
Jack Palevicha605a512009-03-25 15:16:50 -0700938 super(new int[] {
939 EGL10.EGL_RED_SIZE, redSize,
940 EGL10.EGL_GREEN_SIZE, greenSize,
941 EGL10.EGL_BLUE_SIZE, blueSize,
942 EGL10.EGL_ALPHA_SIZE, alphaSize,
943 EGL10.EGL_DEPTH_SIZE, depthSize,
944 EGL10.EGL_STENCIL_SIZE, stencilSize,
Jeff Brownf48b0322013-04-19 17:13:33 -0700945 EGL10.EGL_NONE});
Jack Palevicha605a512009-03-25 15:16:50 -0700946 mValue = new int[1];
947 mRedSize = redSize;
948 mGreenSize = greenSize;
949 mBlueSize = blueSize;
950 mAlphaSize = alphaSize;
951 mDepthSize = depthSize;
952 mStencilSize = stencilSize;
Jeff Brownf48b0322013-04-19 17:13:33 -0700953 }
Jack Palevicha605a512009-03-25 15:16:50 -0700954
955 @Override
956 public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display,
957 EGLConfig[] configs) {
Jack Palevich83835352010-03-04 18:12:44 -0800958 for (EGLConfig config : configs) {
Jack Palevicha605a512009-03-25 15:16:50 -0700959 int d = findConfigAttrib(egl, display, config,
960 EGL10.EGL_DEPTH_SIZE, 0);
961 int s = findConfigAttrib(egl, display, config,
962 EGL10.EGL_STENCIL_SIZE, 0);
Jack Palevich83835352010-03-04 18:12:44 -0800963 if ((d >= mDepthSize) && (s >= mStencilSize)) {
Jeff Brownf48b0322013-04-19 17:13:33 -0700964 int r = findConfigAttrib(egl, display, config,
965 EGL10.EGL_RED_SIZE, 0);
966 int g = findConfigAttrib(egl, display, config,
967 EGL10.EGL_GREEN_SIZE, 0);
968 int b = findConfigAttrib(egl, display, config,
969 EGL10.EGL_BLUE_SIZE, 0);
970 int a = findConfigAttrib(egl, display, config,
971 EGL10.EGL_ALPHA_SIZE, 0);
Jack Palevich83835352010-03-04 18:12:44 -0800972 if ((r == mRedSize) && (g == mGreenSize)
973 && (b == mBlueSize) && (a == mAlphaSize)) {
974 return config;
Mathias Agopian82fd4fc2009-08-20 19:09:34 -0700975 }
Jack Palevicha605a512009-03-25 15:16:50 -0700976 }
977 }
Jack Palevich83835352010-03-04 18:12:44 -0800978 return null;
Jack Palevicha605a512009-03-25 15:16:50 -0700979 }
980
981 private int findConfigAttrib(EGL10 egl, EGLDisplay display,
982 EGLConfig config, int attribute, int defaultValue) {
983
984 if (egl.eglGetConfigAttrib(display, config, attribute, mValue)) {
985 return mValue[0];
986 }
987 return defaultValue;
988 }
989
990 private int[] mValue;
991 // Subclasses can adjust these values:
992 protected int mRedSize;
993 protected int mGreenSize;
994 protected int mBlueSize;
995 protected int mAlphaSize;
996 protected int mDepthSize;
997 protected int mStencilSize;
Jeff Brownf48b0322013-04-19 17:13:33 -0700998 }
Jack Palevicha605a512009-03-25 15:16:50 -0700999
1000 /**
Mathias Agopian29e0bd22012-09-18 15:36:10 -07001001 * This class will choose a RGB_888 surface with
Jack Palevich83835352010-03-04 18:12:44 -08001002 * or without a depth buffer.
Jack Palevicha605a512009-03-25 15:16:50 -07001003 *
1004 */
Jeff Brownf48b0322013-04-19 17:13:33 -07001005 private class SimpleEGLConfigChooser extends ComponentSizeChooser {
1006 public SimpleEGLConfigChooser(boolean withDepthBuffer) {
1007 super(8, 8, 8, 0, withDepthBuffer ? 16 : 0, 0);
Jack Palevicha605a512009-03-25 15:16:50 -07001008 }
1009 }
1010
1011 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 * An EGL helper class.
1013 */
1014
Jack Palevich80b3cd62011-11-15 14:51:27 -08001015 private static class EglHelper {
1016 public EglHelper(WeakReference<GLSurfaceView> glSurfaceViewWeakRef) {
1017 mGLSurfaceViewWeakRef = glSurfaceViewWeakRef;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 }
1019
1020 /**
1021 * Initialize EGL for a given configuration spec.
1022 * @param configSpec
1023 */
Jeff Brownf48b0322013-04-19 17:13:33 -07001024 public void start() {
Jack Palevich07353a12010-03-23 15:22:03 -07001025 if (LOG_EGL) {
Jeff Brownf48b0322013-04-19 17:13:33 -07001026 Log.w("EglHelper", "start() tid=" + Thread.currentThread().getId());
Jack Palevich07353a12010-03-23 15:22:03 -07001027 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 /*
1029 * Get an EGL instance
1030 */
1031 mEgl = (EGL10) EGLContext.getEGL();
1032
1033 /*
1034 * Get to the default display.
1035 */
1036 mEglDisplay = mEgl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
1037
Jack Palevich28023912009-10-13 19:52:06 -07001038 if (mEglDisplay == EGL10.EGL_NO_DISPLAY) {
1039 throw new RuntimeException("eglGetDisplay failed");
1040 }
1041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 /*
1043 * We can now initialize EGL for that display
1044 */
1045 int[] version = new int[2];
Jack Palevich28023912009-10-13 19:52:06 -07001046 if(!mEgl.eglInitialize(mEglDisplay, version)) {
1047 throw new RuntimeException("eglInitialize failed");
1048 }
Jack Palevich80b3cd62011-11-15 14:51:27 -08001049 GLSurfaceView view = mGLSurfaceViewWeakRef.get();
1050 if (view == null) {
1051 mEglConfig = null;
1052 mEglContext = null;
1053 } else {
1054 mEglConfig = view.mEGLConfigChooser.chooseConfig(mEgl, mEglDisplay);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055
Jack Palevich80b3cd62011-11-15 14:51:27 -08001056 /*
1057 * Create an EGL context. We want to do this as rarely as we can, because an
1058 * EGL context is a somewhat heavy object.
1059 */
1060 mEglContext = view.mEGLContextFactory.createContext(mEgl, mEglDisplay, mEglConfig);
1061 }
Jack Palevich2e26fc02009-09-25 19:06:30 -07001062 if (mEglContext == null || mEglContext == EGL10.EGL_NO_CONTEXT) {
Jack Palevich07353a12010-03-23 15:22:03 -07001063 mEglContext = null;
Jack Palevich9bb4dba2009-12-04 17:15:07 +08001064 throwEglException("createContext");
Jack Palevich2e26fc02009-09-25 19:06:30 -07001065 }
Jack Palevich07353a12010-03-23 15:22:03 -07001066 if (LOG_EGL) {
Jeff Brownf48b0322013-04-19 17:13:33 -07001067 Log.w("EglHelper", "createContext " + mEglContext + " tid=" + Thread.currentThread().getId());
Jack Palevich07353a12010-03-23 15:22:03 -07001068 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069
1070 mEglSurface = null;
1071 }
1072
Jack Palevichb50e2af2011-10-28 13:25:12 -07001073 /**
1074 * Create an egl surface for the current SurfaceHolder surface. If a surface
1075 * already exists, destroy it before creating the new surface.
1076 *
1077 * @return true if the surface was created successfully.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 */
Jack Palevich80b3cd62011-11-15 14:51:27 -08001079 public boolean createSurface() {
Jack Palevich07353a12010-03-23 15:22:03 -07001080 if (LOG_EGL) {
Jeff Brownf48b0322013-04-19 17:13:33 -07001081 Log.w("EglHelper", "createSurface() tid=" + Thread.currentThread().getId());
Jack Palevich07353a12010-03-23 15:22:03 -07001082 }
1083 /*
1084 * Check preconditions.
1085 */
1086 if (mEgl == null) {
1087 throw new RuntimeException("egl not initialized");
1088 }
1089 if (mEglDisplay == null) {
1090 throw new RuntimeException("eglDisplay not initialized");
1091 }
1092 if (mEglConfig == null) {
1093 throw new RuntimeException("mEglConfig not initialized");
1094 }
Jack Palevich80b3cd62011-11-15 14:51:27 -08001095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 /*
1097 * The window size has changed, so we need to create a new
1098 * surface.
1099 */
Jack Palevich80b3cd62011-11-15 14:51:27 -08001100 destroySurfaceImp();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101
1102 /*
1103 * Create an EGL surface we can render into.
1104 */
Jack Palevich80b3cd62011-11-15 14:51:27 -08001105 GLSurfaceView view = mGLSurfaceViewWeakRef.get();
1106 if (view != null) {
1107 mEglSurface = view.mEGLWindowSurfaceFactory.createWindowSurface(mEgl,
1108 mEglDisplay, mEglConfig, view.getHolder());
1109 } else {
1110 mEglSurface = null;
1111 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112
Jack Palevich2e26fc02009-09-25 19:06:30 -07001113 if (mEglSurface == null || mEglSurface == EGL10.EGL_NO_SURFACE) {
Jack Palevich0e4aa372010-04-19 22:32:15 +08001114 int error = mEgl.eglGetError();
1115 if (error == EGL10.EGL_BAD_NATIVE_WINDOW) {
Jeff Brownf48b0322013-04-19 17:13:33 -07001116 Log.e("EglHelper", "createWindowSurface returned EGL_BAD_NATIVE_WINDOW.");
Jack Palevich0e4aa372010-04-19 22:32:15 +08001117 }
Jack Palevichb50e2af2011-10-28 13:25:12 -07001118 return false;
Jack Palevich2e26fc02009-09-25 19:06:30 -07001119 }
1120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 /*
1122 * Before we can issue GL commands, we need to make sure
1123 * the context is current and bound to a surface.
1124 */
Jack Palevich2e26fc02009-09-25 19:06:30 -07001125 if (!mEgl.eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, mEglContext)) {
Jack Palevich7d73df82012-03-29 16:24:58 -07001126 /*
1127 * Could not make the context current, probably because the underlying
1128 * SurfaceView surface has been destroyed.
1129 */
Jeff Brownf48b0322013-04-19 17:13:33 -07001130 logEglErrorAsWarning("EGLHelper", "eglMakeCurrent", mEgl.eglGetError());
1131 return false;
Jack Palevich2e26fc02009-09-25 19:06:30 -07001132 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133
Jack Palevichb50e2af2011-10-28 13:25:12 -07001134 return true;
1135 }
1136
1137 /**
1138 * Create a GL object for the current EGL context.
1139 * @return
1140 */
Jeff Brownf48b0322013-04-19 17:13:33 -07001141 GL createGL() {
1142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 GL gl = mEglContext.getGL();
Jack Palevich80b3cd62011-11-15 14:51:27 -08001144 GLSurfaceView view = mGLSurfaceViewWeakRef.get();
1145 if (view != null) {
1146 if (view.mGLWrapper != null) {
1147 gl = view.mGLWrapper.wrap(gl);
1148 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149
Jack Palevich80b3cd62011-11-15 14:51:27 -08001150 if ((view.mDebugFlags & (DEBUG_CHECK_GL_ERROR | DEBUG_LOG_GL_CALLS)) != 0) {
1151 int configFlags = 0;
1152 Writer log = null;
1153 if ((view.mDebugFlags & DEBUG_CHECK_GL_ERROR) != 0) {
1154 configFlags |= GLDebugHelper.CONFIG_CHECK_GL_ERROR;
1155 }
1156 if ((view.mDebugFlags & DEBUG_LOG_GL_CALLS) != 0) {
1157 log = new LogWriter();
1158 }
1159 gl = GLDebugHelper.wrap(gl, configFlags, log);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 }
1162 return gl;
1163 }
1164
1165 /**
1166 * Display the current render surface.
Jack Palevichdb6c78b2012-03-29 12:23:57 -07001167 * @return the EGL error code from eglSwapBuffers.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 */
Jack Palevichdb6c78b2012-03-29 12:23:57 -07001169 public int swap() {
Jack Palevich04b17ab2010-03-15 15:09:30 -07001170 if (! mEgl.eglSwapBuffers(mEglDisplay, mEglSurface)) {
Jack Palevich7d73df82012-03-29 16:24:58 -07001171 return mEgl.eglGetError();
Jack Palevich04b17ab2010-03-15 15:09:30 -07001172 }
Jack Palevichdb6c78b2012-03-29 12:23:57 -07001173 return EGL10.EGL_SUCCESS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 }
1175
Jack Palevich1bd888b2009-09-21 15:59:17 -07001176 public void destroySurface() {
Jack Palevich07353a12010-03-23 15:22:03 -07001177 if (LOG_EGL) {
Jeff Brownf48b0322013-04-19 17:13:33 -07001178 Log.w("EglHelper", "destroySurface() tid=" + Thread.currentThread().getId());
Jack Palevich07353a12010-03-23 15:22:03 -07001179 }
Jack Palevich80b3cd62011-11-15 14:51:27 -08001180 destroySurfaceImp();
1181 }
1182
1183 private void destroySurfaceImp() {
Jack Palevich2e26fc02009-09-25 19:06:30 -07001184 if (mEglSurface != null && mEglSurface != EGL10.EGL_NO_SURFACE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 mEgl.eglMakeCurrent(mEglDisplay, EGL10.EGL_NO_SURFACE,
1186 EGL10.EGL_NO_SURFACE,
1187 EGL10.EGL_NO_CONTEXT);
Jack Palevich80b3cd62011-11-15 14:51:27 -08001188 GLSurfaceView view = mGLSurfaceViewWeakRef.get();
1189 if (view != null) {
1190 view.mEGLWindowSurfaceFactory.destroySurface(mEgl, mEglDisplay, mEglSurface);
1191 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 mEglSurface = null;
1193 }
Jack Palevich1bd888b2009-09-21 15:59:17 -07001194 }
1195
Jeff Brownf48b0322013-04-19 17:13:33 -07001196 public void finish() {
Jack Palevich07353a12010-03-23 15:22:03 -07001197 if (LOG_EGL) {
Jeff Brownf48b0322013-04-19 17:13:33 -07001198 Log.w("EglHelper", "finish() tid=" + Thread.currentThread().getId());
Jack Palevich07353a12010-03-23 15:22:03 -07001199 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 if (mEglContext != null) {
Jack Palevich80b3cd62011-11-15 14:51:27 -08001201 GLSurfaceView view = mGLSurfaceViewWeakRef.get();
1202 if (view != null) {
1203 view.mEGLContextFactory.destroyContext(mEgl, mEglDisplay, mEglContext);
1204 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 mEglContext = null;
1206 }
1207 if (mEglDisplay != null) {
1208 mEgl.eglTerminate(mEglDisplay);
1209 mEglDisplay = null;
1210 }
1211 }
1212
Jack Palevich5aa3adcf32009-11-25 13:56:21 +08001213 private void throwEglException(String function) {
Jack Palevich04b17ab2010-03-15 15:09:30 -07001214 throwEglException(function, mEgl.eglGetError());
1215 }
1216
Jack Palevich7d73df82012-03-29 16:24:58 -07001217 public static void throwEglException(String function, int error) {
1218 String message = formatEglError(function, error);
Jack Palevich07353a12010-03-23 15:22:03 -07001219 if (LOG_THREADS) {
Jeff Brownf48b0322013-04-19 17:13:33 -07001220 Log.e("EglHelper", "throwEglException tid=" + Thread.currentThread().getId() + " "
Jack Palevich7d73df82012-03-29 16:24:58 -07001221 + message);
Jack Palevich07353a12010-03-23 15:22:03 -07001222 }
1223 throw new RuntimeException(message);
Jack Palevich5aa3adcf32009-11-25 13:56:21 +08001224 }
1225
Jack Palevich7d73df82012-03-29 16:24:58 -07001226 public static void logEglErrorAsWarning(String tag, String function, int error) {
1227 Log.w(tag, formatEglError(function, error));
1228 }
1229
1230 public static String formatEglError(String function, int error) {
1231 return function + " failed: " + EGLLogWrapper.getErrorString(error);
1232 }
1233
Jack Palevich80b3cd62011-11-15 14:51:27 -08001234 private WeakReference<GLSurfaceView> mGLSurfaceViewWeakRef;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 EGL10 mEgl;
1236 EGLDisplay mEglDisplay;
1237 EGLSurface mEglSurface;
1238 EGLConfig mEglConfig;
Mathew Inwood6e80c542018-08-09 17:46:43 +01001239 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 EGLContext mEglContext;
Jeff Brownf48b0322013-04-19 17:13:33 -07001241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 }
1243
1244 /**
1245 * A generic GL Thread. Takes care of initializing EGL and GL. Delegates
1246 * to a Renderer instance to do the actual drawing. Can be configured to
1247 * render continuously or on request.
Jeff Brownf48b0322013-04-19 17:13:33 -07001248 *
1249 * All potentially blocking synchronization is done through the
1250 * sGLThreadManager object. This avoids multiple-lock ordering issues.
1251 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 */
Jeff Brownf48b0322013-04-19 17:13:33 -07001253 static class GLThread extends Thread {
1254 GLThread(WeakReference<GLSurfaceView> glSurfaceViewWeakRef) {
1255 super();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 mWidth = 0;
1257 mHeight = 0;
Jeff Brownf48b0322013-04-19 17:13:33 -07001258 mRequestRender = true;
Jack Palevich3e832db2009-04-09 15:03:04 -07001259 mRenderMode = RENDERMODE_CONTINUOUSLY;
Robert Carr2b3bf722016-01-07 17:50:48 -08001260 mWantRenderNotification = false;
Jack Palevich80b3cd62011-11-15 14:51:27 -08001261 mGLSurfaceViewWeakRef = glSurfaceViewWeakRef;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 }
1263
1264 @Override
1265 public void run() {
Jeff Brownf48b0322013-04-19 17:13:33 -07001266 setName("GLThread " + getId());
1267 if (LOG_THREADS) {
1268 Log.i("GLThread", "starting tid=" + getId());
1269 }
1270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 try {
Jeff Brownf48b0322013-04-19 17:13:33 -07001272 guardedRun();
1273 } catch (InterruptedException e) {
1274 // fall thru and exit normally
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 } finally {
Jeff Brownf48b0322013-04-19 17:13:33 -07001276 sGLThreadManager.threadExiting(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 }
1278 }
1279
Jeff Brownf48b0322013-04-19 17:13:33 -07001280 /*
1281 * This private method should only be called inside a
1282 * synchronized(sGLThreadManager) block.
1283 */
1284 private void stopEglSurfaceLocked() {
Jack Palevich840e9582010-01-18 15:13:12 +08001285 if (mHaveEglSurface) {
1286 mHaveEglSurface = false;
Jack Palevich32d41652009-11-08 10:55:56 +08001287 mEglHelper.destroySurface();
Jack Palevich32d41652009-11-08 10:55:56 +08001288 }
1289 }
1290
Jeff Brownf48b0322013-04-19 17:13:33 -07001291 /*
1292 * This private method should only be called inside a
1293 * synchronized(sGLThreadManager) block.
1294 */
1295 private void stopEglContextLocked() {
Jack Palevich840e9582010-01-18 15:13:12 +08001296 if (mHaveEglContext) {
Jeff Brownf48b0322013-04-19 17:13:33 -07001297 mEglHelper.finish();
Jack Palevich840e9582010-01-18 15:13:12 +08001298 mHaveEglContext = false;
Jeff Brownf48b0322013-04-19 17:13:33 -07001299 sGLThreadManager.releaseEglContextLocked(this);
Jack Palevich840e9582010-01-18 15:13:12 +08001300 }
1301 }
Jeff Brownf48b0322013-04-19 17:13:33 -07001302 private void guardedRun() throws InterruptedException {
1303 mEglHelper = new EglHelper(mGLSurfaceViewWeakRef);
1304 mHaveEglContext = false;
1305 mHaveEglSurface = false;
Robert Carr2b3bf722016-01-07 17:50:48 -08001306 mWantRenderNotification = false;
1307
Jeff Brownf48b0322013-04-19 17:13:33 -07001308 try {
1309 GL10 gl = null;
1310 boolean createEglContext = false;
1311 boolean createEglSurface = false;
1312 boolean createGlInterface = false;
1313 boolean lostEglContext = false;
1314 boolean sizeChanged = false;
Robert Carrd4393b22016-01-26 00:18:42 -08001315 boolean wantRenderNotification = false;
Jeff Brownf48b0322013-04-19 17:13:33 -07001316 boolean doRenderNotification = false;
1317 boolean askedToReleaseEglContext = false;
1318 int w = 0;
1319 int h = 0;
1320 Runnable event = null;
Robert Carr25cfa132016-11-16 13:24:09 -08001321 Runnable finishDrawingRunnable = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322
Jeff Brownf48b0322013-04-19 17:13:33 -07001323 while (true) {
1324 synchronized (sGLThreadManager) {
1325 while (true) {
1326 if (mShouldExit) {
1327 return;
1328 }
Jack Palevich67dcd6c2009-11-10 18:26:42 +08001329
Jeff Brownf48b0322013-04-19 17:13:33 -07001330 if (! mEventQueue.isEmpty()) {
1331 event = mEventQueue.remove(0);
1332 break;
1333 }
Jack Palevich67dcd6c2009-11-10 18:26:42 +08001334
Jeff Brownf48b0322013-04-19 17:13:33 -07001335 // Update the pause state.
1336 boolean pausing = false;
1337 if (mPaused != mRequestPaused) {
1338 pausing = mRequestPaused;
1339 mPaused = mRequestPaused;
1340 sGLThreadManager.notifyAll();
1341 if (LOG_PAUSE_RESUME) {
1342 Log.i("GLThread", "mPaused is now " + mPaused + " tid=" + getId());
1343 }
1344 }
Jack Palevich451a2242010-03-25 17:18:39 -07001345
Jeff Brownf48b0322013-04-19 17:13:33 -07001346 // Do we need to give up the EGL context?
1347 if (mShouldReleaseEglContext) {
1348 if (LOG_SURFACE) {
1349 Log.i("GLThread", "releasing EGL context because asked to tid=" + getId());
1350 }
1351 stopEglSurfaceLocked();
1352 stopEglContextLocked();
1353 mShouldReleaseEglContext = false;
1354 askedToReleaseEglContext = true;
1355 }
Jack Palevich1b4ecc62010-07-13 19:09:37 -07001356
Jeff Brownf48b0322013-04-19 17:13:33 -07001357 // Have we lost the EGL context?
1358 if (lostEglContext) {
1359 stopEglSurfaceLocked();
1360 stopEglContextLocked();
1361 lostEglContext = false;
1362 }
1363
1364 // When pausing, release the EGL surface:
1365 if (pausing && mHaveEglSurface) {
1366 if (LOG_SURFACE) {
1367 Log.i("GLThread", "releasing EGL surface because paused tid=" + getId());
1368 }
1369 stopEglSurfaceLocked();
1370 }
1371
1372 // When pausing, optionally release the EGL Context:
1373 if (pausing && mHaveEglContext) {
1374 GLSurfaceView view = mGLSurfaceViewWeakRef.get();
1375 boolean preserveEglContextOnPause = view == null ?
1376 false : view.mPreserveEGLContextOnPause;
John Reckcf890192016-06-29 13:35:27 -07001377 if (!preserveEglContextOnPause) {
Jeff Brownf48b0322013-04-19 17:13:33 -07001378 stopEglContextLocked();
1379 if (LOG_SURFACE) {
1380 Log.i("GLThread", "releasing EGL context because paused tid=" + getId());
1381 }
1382 }
1383 }
1384
Jeff Brownf48b0322013-04-19 17:13:33 -07001385 // Have we lost the SurfaceView surface?
1386 if ((! mHasSurface) && (! mWaitingForSurface)) {
1387 if (LOG_SURFACE) {
1388 Log.i("GLThread", "noticed surfaceView surface lost tid=" + getId());
1389 }
1390 if (mHaveEglSurface) {
1391 stopEglSurfaceLocked();
1392 }
1393 mWaitingForSurface = true;
1394 mSurfaceIsBad = false;
1395 sGLThreadManager.notifyAll();
1396 }
1397
1398 // Have we acquired the surface view surface?
1399 if (mHasSurface && mWaitingForSurface) {
1400 if (LOG_SURFACE) {
1401 Log.i("GLThread", "noticed surfaceView surface acquired tid=" + getId());
1402 }
1403 mWaitingForSurface = false;
1404 sGLThreadManager.notifyAll();
1405 }
1406
1407 if (doRenderNotification) {
1408 if (LOG_SURFACE) {
1409 Log.i("GLThread", "sending render notification tid=" + getId());
1410 }
Robert Carr2b3bf722016-01-07 17:50:48 -08001411 mWantRenderNotification = false;
Jeff Brownf48b0322013-04-19 17:13:33 -07001412 doRenderNotification = false;
1413 mRenderComplete = true;
1414 sGLThreadManager.notifyAll();
1415 }
1416
Robert Carr25cfa132016-11-16 13:24:09 -08001417 if (mFinishDrawingRunnable != null) {
1418 finishDrawingRunnable = mFinishDrawingRunnable;
1419 mFinishDrawingRunnable = null;
1420 }
1421
Jeff Brownf48b0322013-04-19 17:13:33 -07001422 // Ready to draw?
1423 if (readyToDraw()) {
1424
1425 // If we don't have an EGL context, try to acquire one.
1426 if (! mHaveEglContext) {
1427 if (askedToReleaseEglContext) {
1428 askedToReleaseEglContext = false;
John Reckcf890192016-06-29 13:35:27 -07001429 } else {
Jeff Brownf48b0322013-04-19 17:13:33 -07001430 try {
1431 mEglHelper.start();
1432 } catch (RuntimeException t) {
1433 sGLThreadManager.releaseEglContextLocked(this);
1434 throw t;
1435 }
1436 mHaveEglContext = true;
1437 createEglContext = true;
1438
1439 sGLThreadManager.notifyAll();
1440 }
1441 }
1442
1443 if (mHaveEglContext && !mHaveEglSurface) {
1444 mHaveEglSurface = true;
1445 createEglSurface = true;
1446 createGlInterface = true;
1447 sizeChanged = true;
1448 }
1449
1450 if (mHaveEglSurface) {
1451 if (mSizeChanged) {
1452 sizeChanged = true;
1453 w = mWidth;
1454 h = mHeight;
Robert Carr2b3bf722016-01-07 17:50:48 -08001455 mWantRenderNotification = true;
Jeff Brownf48b0322013-04-19 17:13:33 -07001456 if (LOG_SURFACE) {
1457 Log.i("GLThread",
1458 "noticing that we want render notification tid="
1459 + getId());
1460 }
1461
1462 // Destroy and recreate the EGL surface.
1463 createEglSurface = true;
1464
1465 mSizeChanged = false;
1466 }
1467 mRequestRender = false;
1468 sGLThreadManager.notifyAll();
Robert Carrd4393b22016-01-26 00:18:42 -08001469 if (mWantRenderNotification) {
1470 wantRenderNotification = true;
1471 }
Jeff Brownf48b0322013-04-19 17:13:33 -07001472 break;
1473 }
John Recke7f42862017-04-21 17:14:45 -07001474 } else {
1475 if (finishDrawingRunnable != null) {
1476 Log.w(TAG, "Warning, !readyToDraw() but waiting for " +
1477 "draw finished! Early reporting draw finished.");
1478 finishDrawingRunnable.run();
1479 finishDrawingRunnable = null;
1480 }
Jeff Brownf48b0322013-04-19 17:13:33 -07001481 }
Jeff Brownf48b0322013-04-19 17:13:33 -07001482 // By design, this is the only place in a GLThread thread where we wait().
1483 if (LOG_THREADS) {
1484 Log.i("GLThread", "waiting tid=" + getId()
1485 + " mHaveEglContext: " + mHaveEglContext
1486 + " mHaveEglSurface: " + mHaveEglSurface
1487 + " mFinishedCreatingEglSurface: " + mFinishedCreatingEglSurface
1488 + " mPaused: " + mPaused
1489 + " mHasSurface: " + mHasSurface
1490 + " mSurfaceIsBad: " + mSurfaceIsBad
1491 + " mWaitingForSurface: " + mWaitingForSurface
1492 + " mWidth: " + mWidth
1493 + " mHeight: " + mHeight
1494 + " mRequestRender: " + mRequestRender
1495 + " mRenderMode: " + mRenderMode);
1496 }
1497 sGLThreadManager.wait();
Jack Palevich4a2221e2010-03-06 10:39:27 -08001498 }
Jeff Brownf48b0322013-04-19 17:13:33 -07001499 } // end of synchronized(sGLThreadManager)
1500
1501 if (event != null) {
1502 event.run();
1503 event = null;
1504 continue;
Jack Palevich840e9582010-01-18 15:13:12 +08001505 }
1506
Jeff Brownf48b0322013-04-19 17:13:33 -07001507 if (createEglSurface) {
1508 if (LOG_SURFACE) {
1509 Log.w("GLThread", "egl createSurface");
1510 }
1511 if (mEglHelper.createSurface()) {
1512 synchronized(sGLThreadManager) {
1513 mFinishedCreatingEglSurface = true;
1514 sGLThreadManager.notifyAll();
1515 }
1516 } else {
1517 synchronized(sGLThreadManager) {
1518 mFinishedCreatingEglSurface = true;
1519 mSurfaceIsBad = true;
1520 sGLThreadManager.notifyAll();
1521 }
1522 continue;
1523 }
1524 createEglSurface = false;
Jack Palevich15e1c6d2009-09-21 19:19:25 -07001525 }
Jack Palevicha08d46d2009-11-16 18:48:26 +08001526
Jeff Brownf48b0322013-04-19 17:13:33 -07001527 if (createGlInterface) {
1528 gl = (GL10) mEglHelper.createGL();
Mathias Agopian57d019e2013-04-03 17:28:14 -07001529
Jeff Brownf48b0322013-04-19 17:13:33 -07001530 createGlInterface = false;
1531 }
Mathias Agopian57d019e2013-04-03 17:28:14 -07001532
Jeff Brownf48b0322013-04-19 17:13:33 -07001533 if (createEglContext) {
1534 if (LOG_RENDERER) {
1535 Log.w("GLThread", "onSurfaceCreated");
1536 }
1537 GLSurfaceView view = mGLSurfaceViewWeakRef.get();
1538 if (view != null) {
Chris Craik59b429d2015-06-08 17:34:21 -07001539 try {
1540 Trace.traceBegin(Trace.TRACE_TAG_VIEW, "onSurfaceCreated");
1541 view.mRenderer.onSurfaceCreated(gl, mEglHelper.mEglConfig);
1542 } finally {
1543 Trace.traceEnd(Trace.TRACE_TAG_VIEW);
1544 }
Jeff Brownf48b0322013-04-19 17:13:33 -07001545 }
1546 createEglContext = false;
1547 }
Mathias Agopian57d019e2013-04-03 17:28:14 -07001548
Jeff Brownf48b0322013-04-19 17:13:33 -07001549 if (sizeChanged) {
1550 if (LOG_RENDERER) {
1551 Log.w("GLThread", "onSurfaceChanged(" + w + ", " + h + ")");
1552 }
1553 GLSurfaceView view = mGLSurfaceViewWeakRef.get();
1554 if (view != null) {
Chris Craik59b429d2015-06-08 17:34:21 -07001555 try {
1556 Trace.traceBegin(Trace.TRACE_TAG_VIEW, "onSurfaceChanged");
1557 view.mRenderer.onSurfaceChanged(gl, w, h);
1558 } finally {
1559 Trace.traceEnd(Trace.TRACE_TAG_VIEW);
1560 }
Jeff Brownf48b0322013-04-19 17:13:33 -07001561 }
1562 sizeChanged = false;
1563 }
Mathias Agopian57d019e2013-04-03 17:28:14 -07001564
Jeff Brownf48b0322013-04-19 17:13:33 -07001565 if (LOG_RENDERER_DRAW_FRAME) {
1566 Log.w("GLThread", "onDrawFrame tid=" + getId());
1567 }
1568 {
1569 GLSurfaceView view = mGLSurfaceViewWeakRef.get();
1570 if (view != null) {
Chris Craik59b429d2015-06-08 17:34:21 -07001571 try {
1572 Trace.traceBegin(Trace.TRACE_TAG_VIEW, "onDrawFrame");
1573 view.mRenderer.onDrawFrame(gl);
Robert Carr25cfa132016-11-16 13:24:09 -08001574 if (finishDrawingRunnable != null) {
1575 finishDrawingRunnable.run();
1576 finishDrawingRunnable = null;
1577 }
Chris Craik59b429d2015-06-08 17:34:21 -07001578 } finally {
1579 Trace.traceEnd(Trace.TRACE_TAG_VIEW);
1580 }
Jeff Brownf48b0322013-04-19 17:13:33 -07001581 }
1582 }
Jack Palevichdb6c78b2012-03-29 12:23:57 -07001583 int swapError = mEglHelper.swap();
1584 switch (swapError) {
1585 case EGL10.EGL_SUCCESS:
1586 break;
1587 case EGL11.EGL_CONTEXT_LOST:
1588 if (LOG_SURFACE) {
Jeff Brownf48b0322013-04-19 17:13:33 -07001589 Log.i("GLThread", "egl context lost tid=" + getId());
Jack Palevichdb6c78b2012-03-29 12:23:57 -07001590 }
Jeff Brownf48b0322013-04-19 17:13:33 -07001591 lostEglContext = true;
Jack Palevichdb6c78b2012-03-29 12:23:57 -07001592 break;
1593 default:
1594 // Other errors typically mean that the current surface is bad,
Jack Palevich7d73df82012-03-29 16:24:58 -07001595 // probably because the SurfaceView surface has been destroyed,
Jack Palevichdb6c78b2012-03-29 12:23:57 -07001596 // but we haven't been notified yet.
1597 // Log the error to help developers understand why rendering stopped.
Jeff Brownf48b0322013-04-19 17:13:33 -07001598 EglHelper.logEglErrorAsWarning("GLThread", "eglSwapBuffers", swapError);
1599
1600 synchronized(sGLThreadManager) {
1601 mSurfaceIsBad = true;
1602 sGLThreadManager.notifyAll();
1603 }
Jack Palevichdb6c78b2012-03-29 12:23:57 -07001604 break;
Jack Palevicha08d46d2009-11-16 18:48:26 +08001605 }
Jeff Brownf48b0322013-04-19 17:13:33 -07001606
Robert Carrd4393b22016-01-26 00:18:42 -08001607 if (wantRenderNotification) {
Jeff Brownf48b0322013-04-19 17:13:33 -07001608 doRenderNotification = true;
Robert Carrd4393b22016-01-26 00:18:42 -08001609 wantRenderNotification = false;
Jeff Brownf48b0322013-04-19 17:13:33 -07001610 }
Jack Palevicha08d46d2009-11-16 18:48:26 +08001611 }
Jack Palevich840e9582010-01-18 15:13:12 +08001612
Jeff Brownf48b0322013-04-19 17:13:33 -07001613 } finally {
Mathias Agopian57d019e2013-04-03 17:28:14 -07001614 /*
Jeff Brownf48b0322013-04-19 17:13:33 -07001615 * clean-up everything...
Mathias Agopian57d019e2013-04-03 17:28:14 -07001616 */
Jeff Brownf48b0322013-04-19 17:13:33 -07001617 synchronized (sGLThreadManager) {
1618 stopEglSurfaceLocked();
1619 stopEglContextLocked();
1620 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621 }
1622 }
1623
Jeff Brownf48b0322013-04-19 17:13:33 -07001624 public boolean ableToDraw() {
1625 return mHaveEglContext && mHaveEglSurface && readyToDraw();
1626 }
1627
1628 private boolean readyToDraw() {
1629 return (!mPaused) && mHasSurface && (!mSurfaceIsBad)
1630 && (mWidth > 0) && (mHeight > 0)
1631 && (mRequestRender || (mRenderMode == RENDERMODE_CONTINUOUSLY));
1632 }
1633
1634 public void setRenderMode(int renderMode) {
1635 if ( !((RENDERMODE_WHEN_DIRTY <= renderMode) && (renderMode <= RENDERMODE_CONTINUOUSLY)) ) {
1636 throw new IllegalArgumentException("renderMode");
1637 }
1638 synchronized(sGLThreadManager) {
1639 mRenderMode = renderMode;
1640 sGLThreadManager.notifyAll();
1641 }
Mathias Agopian57d019e2013-04-03 17:28:14 -07001642 }
1643
Mathias Agopian78bc2032013-04-18 13:52:58 -07001644 public int getRenderMode() {
Jeff Brownf48b0322013-04-19 17:13:33 -07001645 synchronized(sGLThreadManager) {
1646 return mRenderMode;
1647 }
Mathias Agopian78bc2032013-04-18 13:52:58 -07001648 }
Jeff Brownf48b0322013-04-19 17:13:33 -07001649
1650 public void requestRender() {
1651 synchronized(sGLThreadManager) {
1652 mRequestRender = true;
1653 sGLThreadManager.notifyAll();
1654 }
1655 }
1656
Robert Carr25cfa132016-11-16 13:24:09 -08001657 public void requestRenderAndNotify(Runnable finishDrawing) {
Robert Carr2b3bf722016-01-07 17:50:48 -08001658 synchronized(sGLThreadManager) {
Robert Carrd4393b22016-01-26 00:18:42 -08001659 // If we are already on the GL thread, this means a client callback
1660 // has caused reentrancy, for example via updating the SurfaceView parameters.
1661 // We will return to the client rendering code, so here we don't need to
1662 // do anything.
1663 if (Thread.currentThread() == this) {
1664 return;
1665 }
1666
Robert Carr2b3bf722016-01-07 17:50:48 -08001667 mWantRenderNotification = true;
1668 mRequestRender = true;
1669 mRenderComplete = false;
Bowen Li044fce02022-10-13 23:39:55 +08001670 final Runnable oldCallback = mFinishDrawingRunnable;
1671 mFinishDrawingRunnable = () -> {
1672 if (oldCallback != null) {
1673 oldCallback.run();
1674 }
1675 if (finishDrawing != null) {
1676 finishDrawing.run();
1677 }
1678 };
Robert Carrd4393b22016-01-26 00:18:42 -08001679
Robert Carr2b3bf722016-01-07 17:50:48 -08001680 sGLThreadManager.notifyAll();
Robert Carr2b3bf722016-01-07 17:50:48 -08001681 }
1682 }
1683
Jeff Brownf48b0322013-04-19 17:13:33 -07001684 public void surfaceCreated() {
1685 synchronized(sGLThreadManager) {
1686 if (LOG_THREADS) {
1687 Log.i("GLThread", "surfaceCreated tid=" + getId());
1688 }
1689 mHasSurface = true;
1690 mFinishedCreatingEglSurface = false;
1691 sGLThreadManager.notifyAll();
1692 while (mWaitingForSurface
1693 && !mFinishedCreatingEglSurface
1694 && !mExited) {
1695 try {
1696 sGLThreadManager.wait();
1697 } catch (InterruptedException e) {
1698 Thread.currentThread().interrupt();
1699 }
1700 }
1701 }
1702 }
1703
1704 public void surfaceDestroyed() {
1705 synchronized(sGLThreadManager) {
1706 if (LOG_THREADS) {
1707 Log.i("GLThread", "surfaceDestroyed tid=" + getId());
1708 }
1709 mHasSurface = false;
1710 sGLThreadManager.notifyAll();
1711 while((!mWaitingForSurface) && (!mExited)) {
1712 try {
1713 sGLThreadManager.wait();
1714 } catch (InterruptedException e) {
1715 Thread.currentThread().interrupt();
1716 }
1717 }
1718 }
1719 }
1720
1721 public void onPause() {
1722 synchronized (sGLThreadManager) {
1723 if (LOG_PAUSE_RESUME) {
1724 Log.i("GLThread", "onPause tid=" + getId());
1725 }
1726 mRequestPaused = true;
1727 sGLThreadManager.notifyAll();
1728 while ((! mExited) && (! mPaused)) {
1729 if (LOG_PAUSE_RESUME) {
1730 Log.i("Main thread", "onPause waiting for mPaused.");
1731 }
1732 try {
1733 sGLThreadManager.wait();
1734 } catch (InterruptedException ex) {
1735 Thread.currentThread().interrupt();
1736 }
1737 }
1738 }
1739 }
1740
1741 public void onResume() {
1742 synchronized (sGLThreadManager) {
1743 if (LOG_PAUSE_RESUME) {
1744 Log.i("GLThread", "onResume tid=" + getId());
1745 }
1746 mRequestPaused = false;
1747 mRequestRender = true;
1748 mRenderComplete = false;
1749 sGLThreadManager.notifyAll();
1750 while ((! mExited) && mPaused && (!mRenderComplete)) {
1751 if (LOG_PAUSE_RESUME) {
1752 Log.i("Main thread", "onResume waiting for !mPaused.");
1753 }
1754 try {
1755 sGLThreadManager.wait();
1756 } catch (InterruptedException ex) {
1757 Thread.currentThread().interrupt();
1758 }
1759 }
1760 }
1761 }
1762
1763 public void onWindowResize(int w, int h) {
1764 synchronized (sGLThreadManager) {
1765 mWidth = w;
1766 mHeight = h;
1767 mSizeChanged = true;
1768 mRequestRender = true;
1769 mRenderComplete = false;
Robert Carrd4393b22016-01-26 00:18:42 -08001770
1771 // If we are already on the GL thread, this means a client callback
1772 // has caused reentrancy, for example via updating the SurfaceView parameters.
1773 // We need to process the size change eventually though and update our EGLSurface.
1774 // So we set the parameters and return so they can be processed on our
1775 // next iteration.
1776 if (Thread.currentThread() == this) {
1777 return;
1778 }
1779
Jeff Brownf48b0322013-04-19 17:13:33 -07001780 sGLThreadManager.notifyAll();
1781
1782 // Wait for thread to react to resize and render a frame
1783 while (! mExited && !mPaused && !mRenderComplete
1784 && ableToDraw()) {
1785 if (LOG_SURFACE) {
1786 Log.i("Main thread", "onWindowResize waiting for render complete from tid=" + getId());
1787 }
1788 try {
1789 sGLThreadManager.wait();
1790 } catch (InterruptedException ex) {
1791 Thread.currentThread().interrupt();
1792 }
1793 }
1794 }
1795 }
1796
1797 public void requestExitAndWait() {
1798 // don't call this from GLThread thread or it is a guaranteed
1799 // deadlock!
1800 synchronized(sGLThreadManager) {
1801 mShouldExit = true;
1802 sGLThreadManager.notifyAll();
1803 while (! mExited) {
1804 try {
1805 sGLThreadManager.wait();
1806 } catch (InterruptedException ex) {
1807 Thread.currentThread().interrupt();
1808 }
1809 }
1810 }
1811 }
1812
1813 public void requestReleaseEglContextLocked() {
1814 mShouldReleaseEglContext = true;
1815 sGLThreadManager.notifyAll();
1816 }
1817
1818 /**
1819 * Queue an "event" to be run on the GL rendering thread.
1820 * @param r the runnable to be run on the GL rendering thread.
1821 */
1822 public void queueEvent(Runnable r) {
1823 if (r == null) {
1824 throw new IllegalArgumentException("r must not be null");
1825 }
1826 synchronized(sGLThreadManager) {
1827 mEventQueue.add(r);
1828 sGLThreadManager.notifyAll();
1829 }
1830 }
1831
1832 // Once the thread is started, all accesses to the following member
1833 // variables are protected by the sGLThreadManager monitor
1834 private boolean mShouldExit;
1835 private boolean mExited;
1836 private boolean mRequestPaused;
1837 private boolean mPaused;
1838 private boolean mHasSurface;
1839 private boolean mSurfaceIsBad;
1840 private boolean mWaitingForSurface;
1841 private boolean mHaveEglContext;
1842 private boolean mHaveEglSurface;
1843 private boolean mFinishedCreatingEglSurface;
1844 private boolean mShouldReleaseEglContext;
1845 private int mWidth;
1846 private int mHeight;
1847 private int mRenderMode;
1848 private boolean mRequestRender;
Robert Carr2b3bf722016-01-07 17:50:48 -08001849 private boolean mWantRenderNotification;
Jeff Brownf48b0322013-04-19 17:13:33 -07001850 private boolean mRenderComplete;
1851 private ArrayList<Runnable> mEventQueue = new ArrayList<Runnable>();
1852 private boolean mSizeChanged = true;
Robert Carr25cfa132016-11-16 13:24:09 -08001853 private Runnable mFinishDrawingRunnable = null;
Jeff Brownf48b0322013-04-19 17:13:33 -07001854
1855 // End of member variables protected by the sGLThreadManager monitor.
1856
Mathew Inwood6e80c542018-08-09 17:46:43 +01001857 @UnsupportedAppUsage
Jeff Brownf48b0322013-04-19 17:13:33 -07001858 private EglHelper mEglHelper;
1859
1860 /**
1861 * Set once at thread construction time, nulled out when the parent view is garbage
1862 * called. This weak reference allows the GLSurfaceView to be garbage collected while
1863 * the GLThread is still alive.
1864 */
1865 private WeakReference<GLSurfaceView> mGLSurfaceViewWeakRef;
1866
1867 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001868
1869 static class LogWriter extends Writer {
Jeff Brownf48b0322013-04-19 17:13:33 -07001870
1871 @Override public void close() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 flushBuilder();
1873 }
1874
Jeff Brownf48b0322013-04-19 17:13:33 -07001875 @Override public void flush() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001876 flushBuilder();
1877 }
1878
Jeff Brownf48b0322013-04-19 17:13:33 -07001879 @Override public void write(char[] buf, int offset, int count) {
1880 for(int i = 0; i < count; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 char c = buf[offset + i];
Jeff Brownf48b0322013-04-19 17:13:33 -07001882 if ( c == '\n') {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001883 flushBuilder();
Jeff Brownf48b0322013-04-19 17:13:33 -07001884 }
1885 else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001886 mBuilder.append(c);
1887 }
1888 }
1889 }
1890
1891 private void flushBuilder() {
1892 if (mBuilder.length() > 0) {
Jeff Brownf48b0322013-04-19 17:13:33 -07001893 Log.v("GLSurfaceView", mBuilder.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001894 mBuilder.delete(0, mBuilder.length());
1895 }
1896 }
1897
1898 private StringBuilder mBuilder = new StringBuilder();
1899 }
1900
Jeff Brownf48b0322013-04-19 17:13:33 -07001901
Jack Palevich15e1c6d2009-09-21 19:19:25 -07001902 private void checkRenderThreadState() {
1903 if (mGLThread != null) {
1904 throw new IllegalStateException(
1905 "setRenderer has already been called for this instance.");
1906 }
1907 }
Jeff Brownf48b0322013-04-19 17:13:33 -07001908
1909 private static class GLThreadManager {
1910 private static String TAG = "GLThreadManager";
1911
1912 public synchronized void threadExiting(GLThread thread) {
1913 if (LOG_THREADS) {
1914 Log.i("GLThread", "exiting tid=" + thread.getId());
1915 }
1916 thread.mExited = true;
Jeff Brownf48b0322013-04-19 17:13:33 -07001917 notifyAll();
1918 }
1919
1920 /*
Jeff Brownf48b0322013-04-19 17:13:33 -07001921 * Releases the EGL context. Requires that we are already in the
1922 * sGLThreadManager monitor when this is called.
1923 */
1924 public void releaseEglContextLocked(GLThread thread) {
Jeff Brownf48b0322013-04-19 17:13:33 -07001925 notifyAll();
1926 }
Jeff Brownf48b0322013-04-19 17:13:33 -07001927 }
1928
1929 private static final GLThreadManager sGLThreadManager = new GLThreadManager();
1930
1931 private final WeakReference<GLSurfaceView> mThisWeakRef =
1932 new WeakReference<GLSurfaceView>(this);
Mathew Inwood6e80c542018-08-09 17:46:43 +01001933 @UnsupportedAppUsage
Jeff Brownf48b0322013-04-19 17:13:33 -07001934 private GLThread mGLThread;
Mathew Inwood6e80c542018-08-09 17:46:43 +01001935 @UnsupportedAppUsage
Jeff Brownf48b0322013-04-19 17:13:33 -07001936 private Renderer mRenderer;
1937 private boolean mDetached;
1938 private EGLConfigChooser mEGLConfigChooser;
1939 private EGLContextFactory mEGLContextFactory;
1940 private EGLWindowSurfaceFactory mEGLWindowSurfaceFactory;
1941 private GLWrapper mGLWrapper;
1942 private int mDebugFlags;
1943 private int mEGLContextClientVersion;
1944 private boolean mPreserveEGLContextOnPause;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945}