GLSurfaceView.java revision a08d46d8aaabb53eef4d5dc47272565d50f23c19
1/*
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
19import java.io.Writer;
20import java.util.ArrayList;
21
22import javax.microedition.khronos.egl.EGL10;
23import javax.microedition.khronos.egl.EGL11;
24import javax.microedition.khronos.egl.EGLConfig;
25import javax.microedition.khronos.egl.EGLContext;
26import javax.microedition.khronos.egl.EGLDisplay;
27import javax.microedition.khronos.egl.EGLSurface;
28import javax.microedition.khronos.opengles.GL;
29import javax.microedition.khronos.opengles.GL10;
30
31import android.content.Context;
32import android.util.AttributeSet;
33import android.util.Log;
34import android.view.SurfaceHolder;
35import android.view.SurfaceView;
36
37/**
38 * An implementation of SurfaceView that uses the dedicated surface for
39 * displaying OpenGL rendering.
40 * <p>
41 * A GLSurfaceView provides the following features:
42 * <p>
43 * <ul>
44 * <li>Manages a surface, which is a special piece of memory that can be
45 * composited into the Android view system.
46 * <li>Manages an EGL display, which enables OpenGL to render into a surface.
47 * <li>Accepts a user-provided Renderer object that does the actual rendering.
48 * <li>Renders on a dedicated thread to decouple rendering performance from the
49 * UI thread.
50 * <li>Supports both on-demand and continuous rendering.
51 * <li>Optionally wraps, traces, and/or error-checks the renderer's OpenGL calls.
52 * </ul>
53 *
54 * <h3>Using GLSurfaceView</h3>
55 * <p>
56 * Typically you use GLSurfaceView by subclassing it and overriding one or more of the
57 * View system input event methods. If your application does not need to override event
58 * methods then GLSurfaceView can be used as-is. For the most part
59 * GLSurfaceView behavior is customized by calling "set" methods rather than by subclassing.
60 * For example, unlike a regular View, drawing is delegated to a separate Renderer object which
61 * is registered with the GLSurfaceView
62 * using the {@link #setRenderer(Renderer)} call.
63 * <p>
64 * <h3>Initializing GLSurfaceView</h3>
65 * All you have to do to initialize a GLSurfaceView is call {@link #setRenderer(Renderer)}.
66 * However, if desired, you can modify the default behavior of GLSurfaceView by calling one or
67 * more of these methods before calling setRenderer:
68 * <ul>
69 * <li>{@link #setDebugFlags(int)}
70 * <li>{@link #setEGLConfigChooser(boolean)}
71 * <li>{@link #setEGLConfigChooser(EGLConfigChooser)}
72 * <li>{@link #setEGLConfigChooser(int, int, int, int, int, int)}
73 * <li>{@link #setGLWrapper(GLWrapper)}
74 * </ul>
75 * <p>
76 * <h4>Choosing an EGL Configuration</h4>
77 * A given Android device may support multiple possible types of drawing surfaces.
78 * The available surfaces may differ in how may channels of data are present, as
79 * well as how many bits are allocated to each channel. Therefore, the first thing
80 * GLSurfaceView has to do when starting to render is choose what type of surface to use.
81 * <p>
82 * By default GLSurfaceView chooses an available surface that's closest to a 16-bit R5G6B5 surface
83 * with a 16-bit depth buffer and no stencil. If you would prefer a different surface (for example,
84 * if you do not need a depth buffer) you can override the default behavior by calling one of the
85 * setEGLConfigChooser methods.
86 * <p>
87 * <h4>Debug Behavior</h4>
88 * You can optionally modify the behavior of GLSurfaceView by calling
89 * one or more of the debugging methods {@link #setDebugFlags(int)},
90 * and {@link #setGLWrapper}. These methods may be called before and/or after setRenderer, but
91 * typically they are called before setRenderer so that they take effect immediately.
92 * <p>
93 * <h4>Setting a Renderer</h4>
94 * Finally, you must call {@link #setRenderer} to register a {@link Renderer}.
95 * The renderer is
96 * responsible for doing the actual OpenGL rendering.
97 * <p>
98 * <h3>Rendering Mode</h3>
99 * Once the renderer is set, you can control whether the renderer draws
100 * continuously or on-demand by calling
101 * {@link #setRenderMode}. The default is continuous rendering.
102 * <p>
103 * <h3>Activity Life-cycle</h3>
104 * A GLSurfaceView must be notified when the activity is paused and resumed. GLSurfaceView clients
105 * are required to call {@link #onPause()} when the activity pauses and
106 * {@link #onResume()} when the activity resumes. These calls allow GLSurfaceView to
107 * pause and resume the rendering thread, and also allow GLSurfaceView to release and recreate
108 * the OpenGL display.
109 * <p>
110 * <h3>Handling events</h3>
111 * <p>
112 * To handle an event you will typically subclass GLSurfaceView and override the
113 * appropriate method, just as you would with any other View. However, when handling
114 * the event, you may need to communicate with the Renderer object
115 * that's running in the rendering thread. You can do this using any
116 * standard Java cross-thread communication mechanism. In addition,
117 * one relatively easy way to communicate with your renderer is
118 * to call
119 * {@link #queueEvent(Runnable)}. For example:
120 * <pre class="prettyprint">
121 * class MyGLSurfaceView extends GLSurfaceView {
122 *
123 *     private MyRenderer mMyRenderer;
124 *
125 *     public void start() {
126 *         mMyRenderer = ...;
127 *         setRenderer(mMyRenderer);
128 *     }
129 *
130 *     public boolean onKeyDown(int keyCode, KeyEvent event) {
131 *         if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
132 *             queueEvent(new Runnable() {
133 *                 // This method will be called on the rendering
134 *                 // thread:
135 *                 public void run() {
136 *                     mMyRenderer.handleDpadCenter();
137 *                 }});
138 *             return true;
139 *         }
140 *         return super.onKeyDown(keyCode, event);
141 *     }
142 * }
143 * </pre>
144 *
145 */
146public class GLSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
147    private final static boolean LOG_THREADS = false;
148    private final static boolean LOG_SURFACE = false;
149    private final static boolean LOG_RENDERER = false;
150    // Work-around for bug 2263168
151    private final static boolean DRAW_TWICE_AFTER_SIZE_CHANGED = true;
152    /**
153     * The renderer only renders
154     * when the surface is created, or when {@link #requestRender} is called.
155     *
156     * @see #getRenderMode()
157     * @see #setRenderMode(int)
158     */
159    public final static int RENDERMODE_WHEN_DIRTY = 0;
160    /**
161     * The renderer is called
162     * continuously to re-render the scene.
163     *
164     * @see #getRenderMode()
165     * @see #setRenderMode(int)
166     * @see #requestRender()
167     */
168    public final static int RENDERMODE_CONTINUOUSLY = 1;
169
170    /**
171     * Check glError() after every GL call and throw an exception if glError indicates
172     * that an error has occurred. This can be used to help track down which OpenGL ES call
173     * is causing an error.
174     *
175     * @see #getDebugFlags
176     * @see #setDebugFlags
177     */
178    public final static int DEBUG_CHECK_GL_ERROR = 1;
179
180    /**
181     * Log GL calls to the system log at "verbose" level with tag "GLSurfaceView".
182     *
183     * @see #getDebugFlags
184     * @see #setDebugFlags
185     */
186    public final static int DEBUG_LOG_GL_CALLS = 2;
187
188    /**
189     * Standard View constructor. In order to render something, you
190     * must call {@link #setRenderer} to register a renderer.
191     */
192    public GLSurfaceView(Context context) {
193        super(context);
194        init();
195    }
196
197    /**
198     * Standard View constructor. In order to render something, you
199     * must call {@link #setRenderer} to register a renderer.
200     */
201    public GLSurfaceView(Context context, AttributeSet attrs) {
202        super(context, attrs);
203        init();
204    }
205
206    private void init() {
207        // Install a SurfaceHolder.Callback so we get notified when the
208        // underlying surface is created and destroyed
209        SurfaceHolder holder = getHolder();
210        holder.addCallback(this);
211    }
212
213    /**
214     * Set the glWrapper. If the glWrapper is not null, its
215     * {@link GLWrapper#wrap(GL)} method is called
216     * whenever a surface is created. A GLWrapper can be used to wrap
217     * the GL object that's passed to the renderer. Wrapping a GL
218     * object enables examining and modifying the behavior of the
219     * GL calls made by the renderer.
220     * <p>
221     * Wrapping is typically used for debugging purposes.
222     * <p>
223     * The default value is null.
224     * @param glWrapper the new GLWrapper
225     */
226    public void setGLWrapper(GLWrapper glWrapper) {
227        mGLWrapper = glWrapper;
228    }
229
230    /**
231     * Set the debug flags to a new value. The value is
232     * constructed by OR-together zero or more
233     * of the DEBUG_CHECK_* constants. The debug flags take effect
234     * whenever a surface is created. The default value is zero.
235     * @param debugFlags the new debug flags
236     * @see #DEBUG_CHECK_GL_ERROR
237     * @see #DEBUG_LOG_GL_CALLS
238     */
239    public void setDebugFlags(int debugFlags) {
240        mDebugFlags = debugFlags;
241    }
242
243    /**
244     * Get the current value of the debug flags.
245     * @return the current value of the debug flags.
246     */
247    public int getDebugFlags() {
248        return mDebugFlags;
249    }
250
251    /**
252     * Set the renderer associated with this view. Also starts the thread that
253     * will call the renderer, which in turn causes the rendering to start.
254     * <p>This method should be called once and only once in the life-cycle of
255     * a GLSurfaceView.
256     * <p>The following GLSurfaceView methods can only be called <em>before</em>
257     * setRenderer is called:
258     * <ul>
259     * <li>{@link #setEGLConfigChooser(boolean)}
260     * <li>{@link #setEGLConfigChooser(EGLConfigChooser)}
261     * <li>{@link #setEGLConfigChooser(int, int, int, int, int, int)}
262     * </ul>
263     * <p>
264     * The following GLSurfaceView methods can only be called <em>after</em>
265     * setRenderer is called:
266     * <ul>
267     * <li>{@link #getRenderMode()}
268     * <li>{@link #onPause()}
269     * <li>{@link #onResume()}
270     * <li>{@link #queueEvent(Runnable)}
271     * <li>{@link #requestRender()}
272     * <li>{@link #setRenderMode(int)}
273     * </ul>
274     *
275     * @param renderer the renderer to use to perform OpenGL drawing.
276     */
277    public void setRenderer(Renderer renderer) {
278        checkRenderThreadState();
279        if (mEGLConfigChooser == null) {
280            mEGLConfigChooser = new SimpleEGLConfigChooser(true);
281        }
282        if (mEGLContextFactory == null) {
283            mEGLContextFactory = new DefaultContextFactory();
284        }
285        if (mEGLWindowSurfaceFactory == null) {
286            mEGLWindowSurfaceFactory = new DefaultWindowSurfaceFactory();
287        }
288        mGLThread = new GLThread(renderer);
289        mGLThread.start();
290    }
291
292    /**
293     * Install a custom EGLContextFactory.
294     * <p>If this method is
295     * called, it must be called before {@link #setRenderer(Renderer)}
296     * is called.
297     * <p>
298     * If this method is not called, then by default
299     * a context will be created with no shared context and
300     * with a null attribute list.
301     */
302    public void setEGLContextFactory(EGLContextFactory factory) {
303        checkRenderThreadState();
304        mEGLContextFactory = factory;
305    }
306
307    /**
308     * Install a custom EGLWindowSurfaceFactory.
309     * <p>If this method is
310     * called, it must be called before {@link #setRenderer(Renderer)}
311     * is called.
312     * <p>
313     * If this method is not called, then by default
314     * a window surface will be created with a null attribute list.
315     */
316    public void setEGLWindowSurfaceFactory(EGLWindowSurfaceFactory factory) {
317        checkRenderThreadState();
318        mEGLWindowSurfaceFactory = factory;
319    }
320
321    /**
322     * Install a custom EGLConfigChooser.
323     * <p>If this method is
324     * called, it must be called before {@link #setRenderer(Renderer)}
325     * is called.
326     * <p>
327     * If no setEGLConfigChooser method is called, then by default the
328     * view will choose a config as close to 16-bit RGB as possible, with
329     * a depth buffer as close to 16 bits as possible.
330     * @param configChooser
331     */
332    public void setEGLConfigChooser(EGLConfigChooser configChooser) {
333        checkRenderThreadState();
334        mEGLConfigChooser = configChooser;
335    }
336
337    /**
338     * Install a config chooser which will choose a config
339     * as close to 16-bit RGB as possible, with or without an optional depth
340     * buffer as close to 16-bits as possible.
341     * <p>If this method is
342     * called, it must be called before {@link #setRenderer(Renderer)}
343     * is called.
344     * <p>
345      * If no setEGLConfigChooser method is called, then by default the
346     * view will choose a config as close to 16-bit RGB as possible, with
347     * a depth buffer as close to 16 bits as possible.
348     *
349     * @param needDepth
350     */
351    public void setEGLConfigChooser(boolean needDepth) {
352        setEGLConfigChooser(new SimpleEGLConfigChooser(needDepth));
353    }
354
355    /**
356     * Install a config chooser which will choose a config
357     * with at least the specified component sizes, and as close
358     * to the specified component sizes as possible.
359     * <p>If this method is
360     * called, it must be called before {@link #setRenderer(Renderer)}
361     * is called.
362     * <p>
363     * If no setEGLConfigChooser method is called, then by default the
364     * view will choose a config as close to 16-bit RGB as possible, with
365     * a depth buffer as close to 16 bits as possible.
366     *
367     */
368    public void setEGLConfigChooser(int redSize, int greenSize, int blueSize,
369            int alphaSize, int depthSize, int stencilSize) {
370        setEGLConfigChooser(new ComponentSizeChooser(redSize, greenSize,
371                blueSize, alphaSize, depthSize, stencilSize));
372    }
373    /**
374     * Set the rendering mode. When renderMode is
375     * RENDERMODE_CONTINUOUSLY, the renderer is called
376     * repeatedly to re-render the scene. When renderMode
377     * is RENDERMODE_WHEN_DIRTY, the renderer only rendered when the surface
378     * is created, or when {@link #requestRender} is called. Defaults to RENDERMODE_CONTINUOUSLY.
379     * <p>
380     * Using RENDERMODE_WHEN_DIRTY can improve battery life and overall system performance
381     * by allowing the GPU and CPU to idle when the view does not need to be updated.
382     * <p>
383     * This method can only be called after {@link #setRenderer(Renderer)}
384     *
385     * @param renderMode one of the RENDERMODE_X constants
386     * @see #RENDERMODE_CONTINUOUSLY
387     * @see #RENDERMODE_WHEN_DIRTY
388     */
389    public void setRenderMode(int renderMode) {
390        mGLThread.setRenderMode(renderMode);
391    }
392
393    /**
394     * Get the current rendering mode. May be called
395     * from any thread. Must not be called before a renderer has been set.
396     * @return the current rendering mode.
397     * @see #RENDERMODE_CONTINUOUSLY
398     * @see #RENDERMODE_WHEN_DIRTY
399     */
400    public int getRenderMode() {
401        return mGLThread.getRenderMode();
402    }
403
404    /**
405     * Request that the renderer render a frame.
406     * This method is typically used when the render mode has been set to
407     * {@link #RENDERMODE_WHEN_DIRTY}, so that frames are only rendered on demand.
408     * May be called
409     * from any thread. Must not be called before a renderer has been set.
410     */
411    public void requestRender() {
412        mGLThread.requestRender();
413    }
414
415    /**
416     * This method is part of the SurfaceHolder.Callback interface, and is
417     * not normally called or subclassed by clients of GLSurfaceView.
418     */
419    public void surfaceCreated(SurfaceHolder holder) {
420        mGLThread.surfaceCreated();
421    }
422
423    /**
424     * This method is part of the SurfaceHolder.Callback interface, and is
425     * not normally called or subclassed by clients of GLSurfaceView.
426     */
427    public void surfaceDestroyed(SurfaceHolder holder) {
428        // Surface will be destroyed when we return
429        mGLThread.surfaceDestroyed();
430    }
431
432    /**
433     * This method is part of the SurfaceHolder.Callback interface, and is
434     * not normally called or subclassed by clients of GLSurfaceView.
435     */
436    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
437        mGLThread.onWindowResize(w, h);
438    }
439
440    /**
441     * Inform the view that the activity is paused. The owner of this view must
442     * call this method when the activity is paused. Calling this method will
443     * pause the rendering thread.
444     * Must not be called before a renderer has been set.
445     */
446    public void onPause() {
447        mGLThread.onPause();
448    }
449
450    /**
451     * Inform the view that the activity is resumed. The owner of this view must
452     * call this method when the activity is resumed. Calling this method will
453     * recreate the OpenGL display and resume the rendering
454     * thread.
455     * Must not be called before a renderer has been set.
456     */
457    public void onResume() {
458        mGLThread.onResume();
459    }
460
461    /**
462     * Queue a runnable to be run on the GL rendering thread. This can be used
463     * to communicate with the Renderer on the rendering thread.
464     * Must not be called before a renderer has been set.
465     * @param r the runnable to be run on the GL rendering thread.
466     */
467    public void queueEvent(Runnable r) {
468        mGLThread.queueEvent(r);
469    }
470
471    /**
472     * This method is used as part of the View class and is not normally
473     * called or subclassed by clients of GLSurfaceView.
474     * Must not be called before a renderer has been set.
475     */
476    @Override
477    protected void onDetachedFromWindow() {
478        super.onDetachedFromWindow();
479        mGLThread.requestExitAndWait();
480    }
481
482    // ----------------------------------------------------------------------
483
484    /**
485     * An interface used to wrap a GL interface.
486     * <p>Typically
487     * used for implementing debugging and tracing on top of the default
488     * GL interface. You would typically use this by creating your own class
489     * that implemented all the GL methods by delegating to another GL instance.
490     * Then you could add your own behavior before or after calling the
491     * delegate. All the GLWrapper would do was instantiate and return the
492     * wrapper GL instance:
493     * <pre class="prettyprint">
494     * class MyGLWrapper implements GLWrapper {
495     *     GL wrap(GL gl) {
496     *         return new MyGLImplementation(gl);
497     *     }
498     *     static class MyGLImplementation implements GL,GL10,GL11,... {
499     *         ...
500     *     }
501     * }
502     * </pre>
503     * @see #setGLWrapper(GLWrapper)
504     */
505    public interface GLWrapper {
506        /**
507         * Wraps a gl interface in another gl interface.
508         * @param gl a GL interface that is to be wrapped.
509         * @return either the input argument or another GL object that wraps the input argument.
510         */
511        GL wrap(GL gl);
512    }
513
514    /**
515     * A generic renderer interface.
516     * <p>
517     * The renderer is responsible for making OpenGL calls to render a frame.
518     * <p>
519     * GLSurfaceView clients typically create their own classes that implement
520     * this interface, and then call {@link GLSurfaceView#setRenderer} to
521     * register the renderer with the GLSurfaceView.
522     * <p>
523     * <h3>Threading</h3>
524     * The renderer will be called on a separate thread, so that rendering
525     * performance is decoupled from the UI thread. Clients typically need to
526     * communicate with the renderer from the UI thread, because that's where
527     * input events are received. Clients can communicate using any of the
528     * standard Java techniques for cross-thread communication, or they can
529     * use the {@link GLSurfaceView#queueEvent(Runnable)} convenience method.
530     * <p>
531     * <h3>EGL Context Lost</h3>
532     * There are situations where the EGL rendering context will be lost. This
533     * typically happens when device wakes up after going to sleep. When
534     * the EGL context is lost, all OpenGL resources (such as textures) that are
535     * associated with that context will be automatically deleted. In order to
536     * keep rendering correctly, a renderer must recreate any lost resources
537     * that it still needs. The {@link #onSurfaceCreated(GL10, EGLConfig)} method
538     * is a convenient place to do this.
539     *
540     *
541     * @see #setRenderer(Renderer)
542     */
543    public interface Renderer {
544        /**
545         * Called when the surface is created or recreated.
546         * <p>
547         * Called when the rendering thread
548         * starts and whenever the EGL context is lost. The context will typically
549         * be lost when the Android device awakes after going to sleep.
550         * <p>
551         * Since this method is called at the beginning of rendering, as well as
552         * every time the EGL context is lost, this method is a convenient place to put
553         * code to create resources that need to be created when the rendering
554         * starts, and that need to be recreated when the EGL context is lost.
555         * Textures are an example of a resource that you might want to create
556         * here.
557         * <p>
558         * Note that when the EGL context is lost, all OpenGL resources associated
559         * with that context will be automatically deleted. You do not need to call
560         * the corresponding "glDelete" methods such as glDeleteTextures to
561         * manually delete these lost resources.
562         * <p>
563         * @param gl the GL interface. Use <code>instanceof</code> to
564         * test if the interface supports GL11 or higher interfaces.
565         * @param config the EGLConfig of the created surface. Can be used
566         * to create matching pbuffers.
567         */
568        void onSurfaceCreated(GL10 gl, EGLConfig config);
569
570        /**
571         * Called when the surface changed size.
572         * <p>
573         * Called after the surface is created and whenever
574         * the OpenGL ES surface size changes.
575         * <p>
576         * Typically you will set your viewport here. If your camera
577         * is fixed then you could also set your projection matrix here:
578         * <pre class="prettyprint">
579         * void onSurfaceChanged(GL10 gl, int width, int height) {
580         *     gl.glViewport(0, 0, width, height);
581         *     // for a fixed camera, set the projection too
582         *     float ratio = (float) width / height;
583         *     gl.glMatrixMode(GL10.GL_PROJECTION);
584         *     gl.glLoadIdentity();
585         *     gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
586         * }
587         * </pre>
588         * @param gl the GL interface. Use <code>instanceof</code> to
589         * test if the interface supports GL11 or higher interfaces.
590         * @param width
591         * @param height
592         */
593        void onSurfaceChanged(GL10 gl, int width, int height);
594
595        /**
596         * Called to draw the current frame.
597         * <p>
598         * This method is responsible for drawing the current frame.
599         * <p>
600         * The implementation of this method typically looks like this:
601         * <pre class="prettyprint">
602         * void onDrawFrame(GL10 gl) {
603         *     gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
604         *     //... other gl calls to render the scene ...
605         * }
606         * </pre>
607         * @param gl the GL interface. Use <code>instanceof</code> to
608         * test if the interface supports GL11 or higher interfaces.
609         */
610        void onDrawFrame(GL10 gl);
611    }
612
613    /**
614     * An interface for customizing the eglCreateContext and eglDestroyContext calls.
615     * <p>
616     * This interface must be implemented by clients wishing to call
617     * {@link GLSurfaceView#setEGLContextFactory(EGLContextFactory)}
618     */
619    public interface EGLContextFactory {
620        EGLContext createContext(EGL10 egl, EGLDisplay display, EGLConfig eglConfig);
621        void destroyContext(EGL10 egl, EGLDisplay display, EGLContext context);
622    }
623
624    private static class DefaultContextFactory implements EGLContextFactory {
625
626        public EGLContext createContext(EGL10 egl, EGLDisplay display, EGLConfig config) {
627            return egl.eglCreateContext(display, config, EGL10.EGL_NO_CONTEXT, null);
628        }
629
630        public void destroyContext(EGL10 egl, EGLDisplay display,
631                EGLContext context) {
632            egl.eglDestroyContext(display, context);
633        }
634    }
635
636    /**
637     * An interface for customizing the eglCreateWindowSurface and eglDestroySurface calls.
638     * <p>
639     * This interface must be implemented by clients wishing to call
640     * {@link GLSurfaceView#setEGLWindowSurfaceFactory(EGLWindowSurfaceFactory)}
641     */
642    public interface EGLWindowSurfaceFactory {
643        EGLSurface createWindowSurface(EGL10 egl, EGLDisplay display, EGLConfig config,
644                Object nativeWindow);
645        void destroySurface(EGL10 egl, EGLDisplay display, EGLSurface surface);
646    }
647
648    private static class DefaultWindowSurfaceFactory implements EGLWindowSurfaceFactory {
649
650        public EGLSurface createWindowSurface(EGL10 egl, EGLDisplay display,
651                EGLConfig config, Object nativeWindow) {
652            return egl.eglCreateWindowSurface(display, config, nativeWindow, null);
653        }
654
655        public void destroySurface(EGL10 egl, EGLDisplay display,
656                EGLSurface surface) {
657            egl.eglDestroySurface(display, surface);
658        }
659    }
660
661    /**
662     * An interface for choosing an EGLConfig configuration from a list of
663     * potential configurations.
664     * <p>
665     * This interface must be implemented by clients wishing to call
666     * {@link GLSurfaceView#setEGLConfigChooser(EGLConfigChooser)}
667     */
668    public interface EGLConfigChooser {
669        /**
670         * Choose a configuration from the list. Implementors typically
671         * implement this method by calling
672         * {@link EGL10#eglChooseConfig} and iterating through the results. Please consult the
673         * EGL specification available from The Khronos Group to learn how to call eglChooseConfig.
674         * @param egl the EGL10 for the current display.
675         * @param display the current display.
676         * @return the chosen configuration.
677         */
678        EGLConfig chooseConfig(EGL10 egl, EGLDisplay display);
679    }
680
681    private static abstract class BaseConfigChooser
682            implements EGLConfigChooser {
683        public BaseConfigChooser(int[] configSpec) {
684            mConfigSpec = configSpec;
685        }
686        public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) {
687            int[] num_config = new int[1];
688            egl.eglChooseConfig(display, mConfigSpec, null, 0, num_config);
689
690            int numConfigs = num_config[0];
691
692            if (numConfigs <= 0) {
693                throw new IllegalArgumentException(
694                        "No configs match configSpec");
695            }
696
697            EGLConfig[] configs = new EGLConfig[numConfigs];
698            egl.eglChooseConfig(display, mConfigSpec, configs, numConfigs,
699                    num_config);
700            EGLConfig config = chooseConfig(egl, display, configs);
701            if (config == null) {
702                throw new IllegalArgumentException("No config chosen");
703            }
704            return config;
705        }
706
707        abstract EGLConfig chooseConfig(EGL10 egl, EGLDisplay display,
708                EGLConfig[] configs);
709
710        protected int[] mConfigSpec;
711    }
712
713    private static class ComponentSizeChooser extends BaseConfigChooser {
714        public ComponentSizeChooser(int redSize, int greenSize, int blueSize,
715                int alphaSize, int depthSize, int stencilSize) {
716            super(new int[] {
717                    EGL10.EGL_RED_SIZE, redSize,
718                    EGL10.EGL_GREEN_SIZE, greenSize,
719                    EGL10.EGL_BLUE_SIZE, blueSize,
720                    EGL10.EGL_ALPHA_SIZE, alphaSize,
721                    EGL10.EGL_DEPTH_SIZE, depthSize,
722                    EGL10.EGL_STENCIL_SIZE, stencilSize,
723                    EGL10.EGL_NONE});
724            mValue = new int[1];
725            mRedSize = redSize;
726            mGreenSize = greenSize;
727            mBlueSize = blueSize;
728            mAlphaSize = alphaSize;
729            mDepthSize = depthSize;
730            mStencilSize = stencilSize;
731       }
732
733        @Override
734        public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display,
735                EGLConfig[] configs) {
736            EGLConfig closestConfig = null;
737            int closestDistance = 1000;
738            for(EGLConfig config : configs) {
739                int d = findConfigAttrib(egl, display, config,
740                        EGL10.EGL_DEPTH_SIZE, 0);
741                int s = findConfigAttrib(egl, display, config,
742                        EGL10.EGL_STENCIL_SIZE, 0);
743                if (d >= mDepthSize && s>= mStencilSize) {
744                    int r = findConfigAttrib(egl, display, config,
745                            EGL10.EGL_RED_SIZE, 0);
746                    int g = findConfigAttrib(egl, display, config,
747                             EGL10.EGL_GREEN_SIZE, 0);
748                    int b = findConfigAttrib(egl, display, config,
749                              EGL10.EGL_BLUE_SIZE, 0);
750                    int a = findConfigAttrib(egl, display, config,
751                            EGL10.EGL_ALPHA_SIZE, 0);
752                    int distance = Math.abs(r - mRedSize)
753                                + Math.abs(g - mGreenSize)
754                                + Math.abs(b - mBlueSize)
755                                + Math.abs(a - mAlphaSize);
756                    if (distance < closestDistance) {
757                        closestDistance = distance;
758                        closestConfig = config;
759                    }
760                }
761            }
762            return closestConfig;
763        }
764
765        private int findConfigAttrib(EGL10 egl, EGLDisplay display,
766                EGLConfig config, int attribute, int defaultValue) {
767
768            if (egl.eglGetConfigAttrib(display, config, attribute, mValue)) {
769                return mValue[0];
770            }
771            return defaultValue;
772        }
773
774        private int[] mValue;
775        // Subclasses can adjust these values:
776        protected int mRedSize;
777        protected int mGreenSize;
778        protected int mBlueSize;
779        protected int mAlphaSize;
780        protected int mDepthSize;
781        protected int mStencilSize;
782        }
783
784    /**
785     * This class will choose a supported surface as close to
786     * RGB565 as possible, with or without a depth buffer.
787     *
788     */
789    private static class SimpleEGLConfigChooser extends ComponentSizeChooser {
790        public SimpleEGLConfigChooser(boolean withDepthBuffer) {
791            super(4, 4, 4, 0, withDepthBuffer ? 16 : 0, 0);
792            // Adjust target values. This way we'll accept a 4444 or
793            // 555 buffer if there's no 565 buffer available.
794            mRedSize = 5;
795            mGreenSize = 6;
796            mBlueSize = 5;
797        }
798    }
799
800    /**
801     * An EGL helper class.
802     */
803
804    private class EglHelper {
805        public EglHelper() {
806
807        }
808
809        /**
810         * Initialize EGL for a given configuration spec.
811         * @param configSpec
812         */
813        public void start(){
814            /*
815             * Get an EGL instance
816             */
817            mEgl = (EGL10) EGLContext.getEGL();
818
819            /*
820             * Get to the default display.
821             */
822            mEglDisplay = mEgl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
823
824            /*
825             * We can now initialize EGL for that display
826             */
827            int[] version = new int[2];
828            mEgl.eglInitialize(mEglDisplay, version);
829            mEglConfig = mEGLConfigChooser.chooseConfig(mEgl, mEglDisplay);
830
831            /*
832            * Create an OpenGL ES context. This must be done only once, an
833            * OpenGL context is a somewhat heavy object.
834            */
835            mEglContext = mEGLContextFactory.createContext(mEgl, mEglDisplay, mEglConfig);
836            if (mEglContext == null || mEglContext == EGL10.EGL_NO_CONTEXT) {
837                throw new RuntimeException("createContext failed");
838            }
839
840            mEglSurface = null;
841        }
842
843        /*
844         * React to the creation of a new surface by creating and returning an
845         * OpenGL interface that renders to that surface.
846         */
847        public GL createSurface(SurfaceHolder holder) {
848            /*
849             *  The window size has changed, so we need to create a new
850             *  surface.
851             */
852            if (mEglSurface != null && mEglSurface != EGL10.EGL_NO_SURFACE) {
853
854                /*
855                 * Unbind and destroy the old EGL surface, if
856                 * there is one.
857                 */
858                mEgl.eglMakeCurrent(mEglDisplay, EGL10.EGL_NO_SURFACE,
859                        EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT);
860                mEGLWindowSurfaceFactory.destroySurface(mEgl, mEglDisplay, mEglSurface);
861            }
862
863            /*
864             * Create an EGL surface we can render into.
865             */
866            mEglSurface = mEGLWindowSurfaceFactory.createWindowSurface(mEgl,
867                    mEglDisplay, mEglConfig, holder);
868
869            if (mEglSurface == null || mEglSurface == EGL10.EGL_NO_SURFACE) {
870                throw new RuntimeException("createWindowSurface failed");
871            }
872
873            /*
874             * Before we can issue GL commands, we need to make sure
875             * the context is current and bound to a surface.
876             */
877            if (!mEgl.eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, mEglContext)) {
878                throw new RuntimeException("eglMakeCurrent failed.");
879            }
880
881            GL gl = mEglContext.getGL();
882            if (mGLWrapper != null) {
883                gl = mGLWrapper.wrap(gl);
884            }
885
886            if ((mDebugFlags & (DEBUG_CHECK_GL_ERROR | DEBUG_LOG_GL_CALLS)) != 0) {
887                int configFlags = 0;
888                Writer log = null;
889                if ((mDebugFlags & DEBUG_CHECK_GL_ERROR) != 0) {
890                    configFlags |= GLDebugHelper.CONFIG_CHECK_GL_ERROR;
891                }
892                if ((mDebugFlags & DEBUG_LOG_GL_CALLS) != 0) {
893                    log = new LogWriter();
894                }
895                gl = GLDebugHelper.wrap(gl, configFlags, log);
896            }
897            return gl;
898        }
899
900        /**
901         * Display the current render surface.
902         * @return false if the context has been lost.
903         */
904        public boolean swap() {
905            mEgl.eglSwapBuffers(mEglDisplay, mEglSurface);
906
907            /*
908             * Always check for EGL_CONTEXT_LOST, which means the context
909             * and all associated data were lost (For instance because
910             * the device went to sleep). We need to sleep until we
911             * get a new surface.
912             */
913            return mEgl.eglGetError() != EGL11.EGL_CONTEXT_LOST;
914        }
915
916        public void destroySurface() {
917            if (mEglSurface != null && mEglSurface != EGL10.EGL_NO_SURFACE) {
918                mEgl.eglMakeCurrent(mEglDisplay, EGL10.EGL_NO_SURFACE,
919                        EGL10.EGL_NO_SURFACE,
920                        EGL10.EGL_NO_CONTEXT);
921                mEGLWindowSurfaceFactory.destroySurface(mEgl, mEglDisplay, mEglSurface);
922                mEglSurface = null;
923            }
924        }
925
926        public void finish() {
927            if (mEglContext != null) {
928                mEGLContextFactory.destroyContext(mEgl, mEglDisplay, mEglContext);
929                mEglContext = null;
930            }
931            if (mEglDisplay != null) {
932                mEgl.eglTerminate(mEglDisplay);
933                mEglDisplay = null;
934            }
935        }
936
937        EGL10 mEgl;
938        EGLDisplay mEglDisplay;
939        EGLSurface mEglSurface;
940        EGLConfig mEglConfig;
941        EGLContext mEglContext;
942    }
943
944    /**
945     * A generic GL Thread. Takes care of initializing EGL and GL. Delegates
946     * to a Renderer instance to do the actual drawing. Can be configured to
947     * render continuously or on request.
948     *
949     * All potentially blocking synchronization is done through the
950     * sGLThreadManager object. This avoids multiple-lock ordering issues.
951     *
952     */
953    class GLThread extends Thread {
954        GLThread(Renderer renderer) {
955            super();
956            mWidth = 0;
957            mHeight = 0;
958            mRequestRender = true;
959            mRenderMode = RENDERMODE_CONTINUOUSLY;
960            mRenderer = renderer;
961        }
962
963        @Override
964        public void run() {
965            setName("GLThread " + getId());
966            if (LOG_THREADS) {
967                Log.i("GLThread", "starting tid=" + getId());
968            }
969
970            try {
971                guardedRun();
972            } catch (InterruptedException e) {
973                // fall thru and exit normally
974            } finally {
975                sGLThreadManager.threadExiting(this);
976            }
977        }
978
979        /*
980         * This private method should only be called inside a
981         * synchronized(sGLThreadManager) block.
982         */
983        private void stopEglLocked() {
984            if (mHaveEgl) {
985                mHaveEgl = false;
986                mEglHelper.destroySurface();
987                mEglHelper.finish();
988                sGLThreadManager.releaseEglSurfaceLocked(this);
989            }
990        }
991
992        private void guardedRun() throws InterruptedException {
993            mEglHelper = new EglHelper();
994            try {
995                GL10 gl = null;
996                boolean createEglSurface = false;
997                boolean sizeChanged = false;
998                int w = 0;
999                int h = 0;
1000                Runnable event = null;
1001
1002                while (true) {
1003                    synchronized (sGLThreadManager) {
1004                        while (true) {
1005                            if (mShouldExit) {
1006                                return;
1007                            }
1008
1009                            if (! mEventQueue.isEmpty()) {
1010                                event = mEventQueue.remove(0);
1011                                break;
1012                            }
1013
1014                            // Do we need to release the EGL surface?
1015                            if (mHaveEgl && mPaused) {
1016                                if (LOG_SURFACE) {
1017                                    Log.i("GLThread", "releasing EGL surface because paused tid=" + getId());
1018                                }
1019                                stopEglLocked();
1020                            }
1021
1022                            // Have we lost the surface view surface?
1023                            if ((! mHasSurface) && (! mWaitingForSurface)) {
1024                                if (LOG_SURFACE) {
1025                                    Log.i("GLThread", "noticed surfaceView surface lost tid=" + getId());
1026                                }
1027                                if (mHaveEgl) {
1028                                    stopEglLocked();
1029                                }
1030                                mWaitingForSurface = true;
1031                                sGLThreadManager.notifyAll();
1032                            }
1033
1034                            // Have we acquired the surface view surface?
1035                            if (mHasSurface && mWaitingForSurface) {
1036                                if (LOG_SURFACE) {
1037                                    Log.i("GLThread", "noticed surfaceView surface acquired tid=" + getId());
1038                                }
1039                                mWaitingForSurface = false;
1040                                sGLThreadManager.notifyAll();
1041                            }
1042
1043                            // Ready to draw?
1044                            if ((!mPaused) && mHasSurface
1045                                && (mWidth > 0) && (mHeight > 0)
1046                                && (mRequestRender || (mRenderMode == RENDERMODE_CONTINUOUSLY))) {
1047
1048                                // If we don't have an egl surface, try to acquire one.
1049                                if ((! mHaveEgl) && sGLThreadManager.tryAcquireEglSurfaceLocked(this)) {
1050                                    mHaveEgl = true;
1051                                    mEglHelper.start();
1052                                    createEglSurface = true;
1053                                    sizeChanged = true;
1054                                    sGLThreadManager.notifyAll();
1055                                }
1056
1057                                if (mHaveEgl) {
1058                                    if (mSizeChanged) {
1059                                        sizeChanged = true;
1060                                        w = mWidth;
1061                                        h = mHeight;
1062                                        if (DRAW_TWICE_AFTER_SIZE_CHANGED) {
1063                                            // We keep mRequestRender true so that we draw twice after the size changes.
1064                                            // (Once because of mSizeChanged, the second time because of mRequestRender.)
1065                                            // This forces the updated graphics onto the screen.
1066                                        } else {
1067                                            mRequestRender = false;
1068                                        }
1069                                        mSizeChanged = false;
1070                                    } else {
1071                                        mRequestRender = false;
1072                                    }
1073                                    sGLThreadManager.notifyAll();
1074                                    break;
1075                                }
1076                            }
1077
1078                            // By design, this is the only place in a GLThread thread where we wait().
1079                            if (LOG_THREADS) {
1080                                Log.i("GLThread", "waiting tid=" + getId());
1081                            }
1082                            sGLThreadManager.wait();
1083                        }
1084                    } // end of synchronized(sGLThreadManager)
1085
1086                    if (event != null) {
1087                        event.run();
1088                        event = null;
1089                        continue;
1090                    }
1091
1092                    if (createEglSurface) {
1093                        gl = (GL10) mEglHelper.createSurface(getHolder());
1094                        if (LOG_RENDERER) {
1095                            Log.w("GLThread", "onSurfaceCreated");
1096                        }
1097                        mRenderer.onSurfaceCreated(gl, mEglHelper.mEglConfig);
1098                        createEglSurface = false;
1099                    }
1100
1101                    if (sizeChanged) {
1102                        if (LOG_RENDERER) {
1103                            Log.w("GLThread", "onSurfaceChanged(" + w + ", " + h + ")");
1104                        }
1105                        mRenderer.onSurfaceChanged(gl, w, h);
1106                        sizeChanged = false;
1107                    }
1108
1109                    if (LOG_RENDERER) {
1110                        Log.w("GLThread", "onDrawFrame");
1111                    }
1112                    mRenderer.onDrawFrame(gl);
1113                    if(!mEglHelper.swap()) {
1114                        if (LOG_SURFACE) {
1115                            Log.i("GLThread", "egl surface lost tid=" + getId());
1116                        }
1117                    }
1118                }
1119            } finally {
1120                /*
1121                 * clean-up everything...
1122                 */
1123                synchronized (sGLThreadManager) {
1124                    stopEglLocked();
1125                }
1126            }
1127        }
1128
1129        public void setRenderMode(int renderMode) {
1130            if ( !((RENDERMODE_WHEN_DIRTY <= renderMode) && (renderMode <= RENDERMODE_CONTINUOUSLY)) ) {
1131                throw new IllegalArgumentException("renderMode");
1132            }
1133            synchronized(sGLThreadManager) {
1134                mRenderMode = renderMode;
1135                sGLThreadManager.notifyAll();
1136            }
1137        }
1138
1139        public int getRenderMode() {
1140            synchronized(sGLThreadManager) {
1141                return mRenderMode;
1142            }
1143        }
1144
1145        public void requestRender() {
1146            synchronized(sGLThreadManager) {
1147                mRequestRender = true;
1148                sGLThreadManager.notifyAll();
1149            }
1150        }
1151
1152        public void surfaceCreated() {
1153            synchronized(sGLThreadManager) {
1154                if (LOG_THREADS) {
1155                    Log.i("GLThread", "surfaceCreated tid=" + getId());
1156                }
1157                mHasSurface = true;
1158                sGLThreadManager.notifyAll();
1159            }
1160        }
1161
1162        public void surfaceDestroyed() {
1163            synchronized(sGLThreadManager) {
1164                if (LOG_THREADS) {
1165                    Log.i("GLThread", "surfaceDestroyed tid=" + getId());
1166                }
1167                mHasSurface = false;
1168                sGLThreadManager.notifyAll();
1169                while((!mWaitingForSurface) && (!mExited)) {
1170                    try {
1171                        sGLThreadManager.wait();
1172                    } catch (InterruptedException e) {
1173                        Thread.currentThread().interrupt();
1174                    }
1175                }
1176            }
1177        }
1178
1179        public void onPause() {
1180            synchronized (sGLThreadManager) {
1181                mPaused = true;
1182                sGLThreadManager.notifyAll();
1183            }
1184        }
1185
1186        public void onResume() {
1187            synchronized (sGLThreadManager) {
1188                mPaused = false;
1189                mRequestRender = true;
1190                sGLThreadManager.notifyAll();
1191            }
1192        }
1193
1194        public void onWindowResize(int w, int h) {
1195            synchronized (sGLThreadManager) {
1196                mWidth = w;
1197                mHeight = h;
1198                mSizeChanged = true;
1199                mRequestRender = true;
1200                sGLThreadManager.notifyAll();
1201            }
1202        }
1203
1204        public void requestExitAndWait() {
1205            // don't call this from GLThread thread or it is a guaranteed
1206            // deadlock!
1207            synchronized(sGLThreadManager) {
1208                mShouldExit = true;
1209                sGLThreadManager.notifyAll();
1210                while (! mExited) {
1211                    try {
1212                        sGLThreadManager.wait();
1213                    } catch (InterruptedException ex) {
1214                        Thread.currentThread().interrupt();
1215                    }
1216                }
1217            }
1218        }
1219
1220        /**
1221         * Queue an "event" to be run on the GL rendering thread.
1222         * @param r the runnable to be run on the GL rendering thread.
1223         */
1224        public void queueEvent(Runnable r) {
1225            if (r == null) {
1226                throw new IllegalArgumentException("r must not be null");
1227            }
1228            synchronized(sGLThreadManager) {
1229                mEventQueue.add(r);
1230                sGLThreadManager.notifyAll();
1231            }
1232        }
1233
1234        // Once the thread is started, all accesses to the following member
1235        // variables are protected by the sGLThreadManager monitor
1236        private boolean mShouldExit;
1237        private boolean mExited;
1238        private boolean mPaused;
1239        private boolean mHasSurface;
1240        private boolean mWaitingForSurface;
1241        private boolean mHaveEgl;
1242        private int mWidth;
1243        private int mHeight;
1244        private int mRenderMode;
1245        private boolean mRequestRender;
1246        private ArrayList<Runnable> mEventQueue = new ArrayList<Runnable>();
1247        // End of member variables protected by the sGLThreadManager monitor.
1248
1249        private Renderer mRenderer;
1250        private EglHelper mEglHelper;
1251    }
1252
1253    static class LogWriter extends Writer {
1254
1255        @Override public void close() {
1256            flushBuilder();
1257        }
1258
1259        @Override public void flush() {
1260            flushBuilder();
1261        }
1262
1263        @Override public void write(char[] buf, int offset, int count) {
1264            for(int i = 0; i < count; i++) {
1265                char c = buf[offset + i];
1266                if ( c == '\n') {
1267                    flushBuilder();
1268                }
1269                else {
1270                    mBuilder.append(c);
1271                }
1272            }
1273        }
1274
1275        private void flushBuilder() {
1276            if (mBuilder.length() > 0) {
1277                Log.v("GLSurfaceView", mBuilder.toString());
1278                mBuilder.delete(0, mBuilder.length());
1279            }
1280        }
1281
1282        private StringBuilder mBuilder = new StringBuilder();
1283    }
1284
1285
1286    private void checkRenderThreadState() {
1287        if (mGLThread != null) {
1288            throw new IllegalStateException(
1289                    "setRenderer has already been called for this instance.");
1290        }
1291    }
1292
1293    private static class GLThreadManager {
1294
1295        public synchronized void threadExiting(GLThread thread) {
1296            if (LOG_THREADS) {
1297                Log.i("GLThread", "exiting tid=" +  thread.getId());
1298            }
1299            thread.mExited = true;
1300            if (mEglOwner == thread) {
1301                mEglOwner = null;
1302            }
1303            notifyAll();
1304        }
1305
1306        /*
1307         * Tries once to acquire the right to use an EGL
1308         * surface. Does not block. Requires that we are already
1309         * in the sGLThreadManager monitor when this is called.
1310         * @return true if the right to use an EGL surface was acquired.
1311         */
1312        public boolean tryAcquireEglSurfaceLocked(GLThread thread) {
1313            if (mEglOwner == thread || mEglOwner == null) {
1314                mEglOwner = thread;
1315                notifyAll();
1316                return true;
1317            }
1318            return false;
1319        }
1320        /*
1321         * Releases the EGL surface. Requires that we are already in the
1322         * sGLThreadManager monitor when this is called.
1323         */
1324        public void releaseEglSurfaceLocked(GLThread thread) {
1325            if (mEglOwner == thread) {
1326                mEglOwner = null;
1327            }
1328            notifyAll();
1329        }
1330
1331        private GLThread mEglOwner;
1332    }
1333
1334    private static final GLThreadManager sGLThreadManager = new GLThreadManager();
1335    private boolean mSizeChanged = true;
1336
1337    private GLThread mGLThread;
1338    private EGLConfigChooser mEGLConfigChooser;
1339    private EGLContextFactory mEGLContextFactory;
1340    private EGLWindowSurfaceFactory mEGLWindowSurfaceFactory;
1341    private GLWrapper mGLWrapper;
1342    private int mDebugFlags;
1343}
1344