HardwareRenderer.java revision 95db2b20d7bc0aaf00b1d4418124f5cf0a755d74
1/*
2 * Copyright (C) 2010 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
17
18package android.view;
19
20import android.content.ComponentCallbacks2;
21import android.graphics.Paint;
22import android.graphics.Rect;
23import android.graphics.SurfaceTexture;
24import android.opengl.GLUtils;
25import android.opengl.ManagedEGLContext;
26import android.os.Handler;
27import android.os.Looper;
28import android.os.SystemClock;
29import android.os.SystemProperties;
30import android.util.Log;
31import com.google.android.gles_jni.EGLImpl;
32
33import javax.microedition.khronos.egl.EGL10;
34import javax.microedition.khronos.egl.EGL11;
35import javax.microedition.khronos.egl.EGLConfig;
36import javax.microedition.khronos.egl.EGLContext;
37import javax.microedition.khronos.egl.EGLDisplay;
38import javax.microedition.khronos.egl.EGLSurface;
39import javax.microedition.khronos.opengles.GL;
40
41import java.io.File;
42
43import static javax.microedition.khronos.egl.EGL10.*;
44
45/**
46 * Interface for rendering a ViewAncestor using hardware acceleration.
47 *
48 * @hide
49 */
50public abstract class HardwareRenderer {
51    static final String LOG_TAG = "HardwareRenderer";
52
53    /**
54     * Name of the file that holds the shaders cache.
55     */
56    private static final String CACHE_PATH_SHADERS = "com.android.opengl.shaders_cache";
57
58    /**
59     * Turn on to only refresh the parts of the screen that need updating.
60     * When turned on the property defined by {@link #RENDER_DIRTY_REGIONS_PROPERTY}
61     * must also have the value "true".
62     */
63    public static final boolean RENDER_DIRTY_REGIONS = true;
64
65    /**
66     * System property used to enable or disable dirty regions invalidation.
67     * This property is only queried if {@link #RENDER_DIRTY_REGIONS} is true.
68     * The default value of this property is assumed to be true.
69     *
70     * Possible values:
71     * "true", to enable partial invalidates
72     * "false", to disable partial invalidates
73     */
74    static final String RENDER_DIRTY_REGIONS_PROPERTY = "hwui.render_dirty_regions";
75
76    /**
77     * System property used to enable or disable vsync.
78     * The default value of this property is assumed to be false.
79     *
80     * Possible values:
81     * "true", to disable vsync
82     * "false", to enable vsync
83     */
84    static final String DISABLE_VSYNC_PROPERTY = "hwui.disable_vsync";
85
86    /**
87     * System property used to debug EGL configuration choice.
88     *
89     * Possible values:
90     * "choice", print the chosen configuration only
91     * "all", print all possible configurations
92     */
93    static final String PRINT_CONFIG_PROPERTY = "hwui.print_config";
94
95    /**
96     * Turn on to draw dirty regions every other frame.
97     */
98    private static final boolean DEBUG_DIRTY_REGION = false;
99
100    /**
101     * A process can set this flag to false to prevent the use of hardware
102     * rendering.
103     *
104     * @hide
105     */
106    public static boolean sRendererDisabled = false;
107
108    /**
109     * Further hardware renderer disabling for the system process.
110     *
111     * @hide
112     */
113    public static boolean sSystemRendererDisabled = false;
114
115    private boolean mEnabled;
116    private boolean mRequested = true;
117
118    /**
119     * Invoke this method to disable hardware rendering in the current process.
120     *
121     * @hide
122     */
123    public static void disable(boolean system) {
124        sRendererDisabled = true;
125        if (system) {
126            sSystemRendererDisabled = true;
127        }
128    }
129
130    /**
131     * Indicates whether hardware acceleration is available under any form for
132     * the view hierarchy.
133     *
134     * @return True if the view hierarchy can potentially be hardware accelerated,
135     *         false otherwise
136     */
137    public static boolean isAvailable() {
138        return GLES20Canvas.isAvailable();
139    }
140
141    /**
142     * Destroys the hardware rendering context.
143     *
144     * @param full If true, destroys all associated resources.
145     */
146    abstract void destroy(boolean full);
147
148    /**
149     * Initializes the hardware renderer for the specified surface.
150     *
151     * @param holder The holder for the surface to hardware accelerate.
152     *
153     * @return True if the initialization was successful, false otherwise.
154     */
155    abstract boolean initialize(SurfaceHolder holder) throws Surface.OutOfResourcesException;
156
157    /**
158     * Updates the hardware renderer for the specified surface.
159     *
160     * @param holder The holder for the surface to hardware accelerate
161     */
162    abstract void updateSurface(SurfaceHolder holder) throws Surface.OutOfResourcesException;
163
164    /**
165     * Destroys the layers used by the specified view hierarchy.
166     *
167     * @param view The root of the view hierarchy
168     */
169    abstract void destroyLayers(View view);
170
171    /**
172     * Destroys all hardware rendering resources associated with the specified
173     * view hierarchy.
174     *
175     * @param view The root of the view hierarchy
176     */
177    abstract void destroyHardwareResources(View view);
178
179    /**
180     * This method should be invoked whenever the current hardware renderer
181     * context should be reset.
182     *
183     * @param holder The holder for the surface to hardware accelerate
184     */
185    abstract void invalidate(SurfaceHolder holder);
186
187    /**
188     * This method should be invoked to ensure the hardware renderer is in
189     * valid state (for instance, to ensure the correct EGL context is bound
190     * to the current thread.)
191     *
192     * @return true if the renderer is now valid, false otherwise
193     */
194    abstract boolean validate();
195
196    /**
197     * Setup the hardware renderer for drawing. This is called whenever the
198     * size of the target surface changes or when the surface is first created.
199     *
200     * @param width Width of the drawing surface.
201     * @param height Height of the drawing surface.
202     */
203    abstract void setup(int width, int height);
204
205    /**
206     * Gets the current width of the surface. This is the width that the surface
207     * was last set to in a call to {@link #setup(int, int)}.
208     *
209     * @return the current width of the surface
210     */
211    abstract int getWidth();
212
213    /**
214     * Gets the current height of the surface. This is the height that the surface
215     * was last set to in a call to {@link #setup(int, int)}.
216     *
217     * @return the current width of the surface
218     */
219    abstract int getHeight();
220
221    /**
222     * Gets the current canvas associated with this HardwareRenderer.
223     *
224     * @return the current HardwareCanvas
225     */
226    abstract HardwareCanvas getCanvas();
227
228    /**
229     * Sets the directory to use as a persistent storage for hardware rendering
230     * resources.
231     *
232     * @param cacheDir A directory the current process can write to
233     */
234    public static void setupDiskCache(File cacheDir) {
235        nSetupShadersDiskCache(new File(cacheDir, CACHE_PATH_SHADERS).getAbsolutePath());
236    }
237
238    private static native void nSetupShadersDiskCache(String cacheFile);
239
240    /**
241     * Interface used to receive callbacks whenever a view is drawn by
242     * a hardware renderer instance.
243     */
244    interface HardwareDrawCallbacks {
245        /**
246         * Invoked before a view is drawn by a hardware renderer.
247         *
248         * @param canvas The Canvas used to render the view.
249         */
250        void onHardwarePreDraw(HardwareCanvas canvas);
251
252        /**
253         * Invoked after a view is drawn by a hardware renderer.
254         *
255         * @param canvas The Canvas used to render the view.
256         */
257        void onHardwarePostDraw(HardwareCanvas canvas);
258    }
259
260    /**
261     * Draws the specified view.
262     *
263     * @param view The view to draw.
264     * @param attachInfo AttachInfo tied to the specified view.
265     * @param callbacks Callbacks invoked when drawing happens.
266     * @param dirty The dirty rectangle to update, can be null.
267     *
268     * @return true if the dirty rect was ignored, false otherwise
269     */
270    abstract boolean draw(View view, View.AttachInfo attachInfo, HardwareDrawCallbacks callbacks,
271            Rect dirty);
272
273    /**
274     * Creates a new display list that can be used to record batches of
275     * drawing operations.
276     *
277     * @return A new display list.
278     */
279    abstract DisplayList createDisplayList();
280
281    /**
282     * Creates a new hardware layer. A hardware layer built by calling this
283     * method will be treated as a texture layer, instead of as a render target.
284     *
285     * @param isOpaque Whether the layer should be opaque or not
286     *
287     * @return A hardware layer
288     */
289    abstract HardwareLayer createHardwareLayer(boolean isOpaque);
290
291    /**
292     * Creates a new hardware layer.
293     *
294     * @param width The minimum width of the layer
295     * @param height The minimum height of the layer
296     * @param isOpaque Whether the layer should be opaque or not
297     *
298     * @return A hardware layer
299     */
300    abstract HardwareLayer createHardwareLayer(int width, int height, boolean isOpaque);
301
302    /**
303     * Creates a new {@link SurfaceTexture} that can be used to render into the
304     * specified hardware layer.
305     *
306     *
307     * @param layer The layer to render into using a {@link android.graphics.SurfaceTexture}
308     *
309     * @return A {@link SurfaceTexture}
310     */
311    abstract SurfaceTexture createSurfaceTexture(HardwareLayer layer);
312
313    /**
314     * Initializes the hardware renderer for the specified surface and setup the
315     * renderer for drawing, if needed. This is invoked when the ViewAncestor has
316     * potentially lost the hardware renderer. The hardware renderer should be
317     * reinitialized and setup when the render {@link #isRequested()} and
318     * {@link #isEnabled()}.
319     *
320     * @param width The width of the drawing surface.
321     * @param height The height of the drawing surface.
322     * @param attachInfo The
323     * @param holder
324     */
325    void initializeIfNeeded(int width, int height, View.AttachInfo attachInfo,
326            SurfaceHolder holder) throws Surface.OutOfResourcesException {
327        if (isRequested()) {
328            // We lost the gl context, so recreate it.
329            if (!isEnabled()) {
330                if (initialize(holder)) {
331                    setup(width, height);
332                }
333            }
334        }
335    }
336
337    /**
338     * Creates a hardware renderer using OpenGL.
339     *
340     * @param glVersion The version of OpenGL to use (1 for OpenGL 1, 11 for OpenGL 1.1, etc.)
341     * @param translucent True if the surface is translucent, false otherwise
342     *
343     * @return A hardware renderer backed by OpenGL.
344     */
345    static HardwareRenderer createGlRenderer(int glVersion, boolean translucent) {
346        switch (glVersion) {
347            case 2:
348                return Gl20Renderer.create(translucent);
349        }
350        throw new IllegalArgumentException("Unknown GL version: " + glVersion);
351    }
352
353    /**
354     * Invoke this method when the system is running out of memory. This
355     * method will attempt to recover as much memory as possible, based on
356     * the specified hint.
357     *
358     * @param level Hint about the amount of memory that should be trimmed,
359     *              see {@link android.content.ComponentCallbacks}
360     */
361    static void trimMemory(int level) {
362        Gl20Renderer.trimMemory(level);
363    }
364
365    /**
366     * Indicates whether hardware acceleration is currently enabled.
367     *
368     * @return True if hardware acceleration is in use, false otherwise.
369     */
370    boolean isEnabled() {
371        return mEnabled;
372    }
373
374    /**
375     * Indicates whether hardware acceleration is currently enabled.
376     *
377     * @param enabled True if the hardware renderer is in use, false otherwise.
378     */
379    void setEnabled(boolean enabled) {
380        mEnabled = enabled;
381    }
382
383    /**
384     * Indicates whether hardware acceleration is currently request but not
385     * necessarily enabled yet.
386     *
387     * @return True if requested, false otherwise.
388     */
389    boolean isRequested() {
390        return mRequested;
391    }
392
393    /**
394     * Indicates whether hardware acceleration is currently requested but not
395     * necessarily enabled yet.
396     *
397     * @return True to request hardware acceleration, false otherwise.
398     */
399    void setRequested(boolean requested) {
400        mRequested = requested;
401    }
402
403    @SuppressWarnings({"deprecation"})
404    static abstract class GlRenderer extends HardwareRenderer {
405        // These values are not exposed in our EGL APIs
406        static final int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
407        static final int EGL_OPENGL_ES2_BIT = 4;
408        static final int EGL_SURFACE_TYPE = 0x3033;
409        static final int EGL_SWAP_BEHAVIOR_PRESERVED_BIT = 0x0400;
410
411        static final int SURFACE_STATE_ERROR = 0;
412        static final int SURFACE_STATE_SUCCESS = 1;
413        static final int SURFACE_STATE_UPDATED = 2;
414
415        static EGL10 sEgl;
416        static EGLDisplay sEglDisplay;
417        static EGLConfig sEglConfig;
418        static final Object[] sEglLock = new Object[0];
419        int mWidth = -1, mHeight = -1;
420
421        static final ThreadLocal<Gl20Renderer.Gl20RendererEglContext> sEglContextStorage
422                = new ThreadLocal<Gl20Renderer.Gl20RendererEglContext>();
423
424        EGLContext mEglContext;
425        Thread mEglThread;
426
427        EGLSurface mEglSurface;
428
429        GL mGl;
430        HardwareCanvas mCanvas;
431        int mFrameCount;
432        Paint mDebugPaint;
433
434        static boolean sDirtyRegions;
435        static final boolean sDirtyRegionsRequested;
436        static {
437            String dirtyProperty = SystemProperties.get(RENDER_DIRTY_REGIONS_PROPERTY, "true");
438            //noinspection PointlessBooleanExpression,ConstantConditions
439            sDirtyRegions = RENDER_DIRTY_REGIONS && "true".equalsIgnoreCase(dirtyProperty);
440            sDirtyRegionsRequested = sDirtyRegions;
441        }
442
443        boolean mDirtyRegionsEnabled;
444        final boolean mVsyncDisabled;
445
446        final int mGlVersion;
447        final boolean mTranslucent;
448
449        private boolean mDestroyed;
450
451        private final Rect mRedrawClip = new Rect();
452
453        GlRenderer(int glVersion, boolean translucent) {
454            mGlVersion = glVersion;
455            mTranslucent = translucent;
456
457            final String vsyncProperty = SystemProperties.get(DISABLE_VSYNC_PROPERTY, "false");
458            mVsyncDisabled = "true".equalsIgnoreCase(vsyncProperty);
459            if (mVsyncDisabled) {
460                Log.d(LOG_TAG, "Disabling v-sync");
461            }
462        }
463
464        /**
465         * Indicates whether this renderer instance can track and update dirty regions.
466         */
467        boolean hasDirtyRegions() {
468            return mDirtyRegionsEnabled;
469        }
470
471        /**
472         * Checks for OpenGL errors. If an error has occured, {@link #destroy(boolean)}
473         * is invoked and the requested flag is turned off. The error code is
474         * also logged as a warning.
475         */
476        void checkEglErrors() {
477            if (isEnabled()) {
478                int error = sEgl.eglGetError();
479                if (error != EGL_SUCCESS) {
480                    // something bad has happened revert to
481                    // normal rendering.
482                    Log.w(LOG_TAG, "EGL error: " + GLUtils.getEGLErrorString(error));
483                    fallback(error != EGL11.EGL_CONTEXT_LOST);
484                }
485            }
486        }
487
488        private void fallback(boolean fallback) {
489            destroy(true);
490            if (fallback) {
491                // we'll try again if it was context lost
492                setRequested(false);
493                Log.w(LOG_TAG, "Mountain View, we've had a problem here. "
494                        + "Switching back to software rendering.");
495            }
496        }
497
498        @Override
499        boolean initialize(SurfaceHolder holder) throws Surface.OutOfResourcesException {
500            if (isRequested() && !isEnabled()) {
501                initializeEgl();
502                mGl = createEglSurface(holder);
503                mDestroyed = false;
504
505                if (mGl != null) {
506                    int err = sEgl.eglGetError();
507                    if (err != EGL_SUCCESS) {
508                        destroy(true);
509                        setRequested(false);
510                    } else {
511                        if (mCanvas == null) {
512                            mCanvas = createCanvas();
513                        }
514                        if (mCanvas != null) {
515                            setEnabled(true);
516                        } else {
517                            Log.w(LOG_TAG, "Hardware accelerated Canvas could not be created");
518                        }
519                    }
520
521                    return mCanvas != null;
522                }
523            }
524            return false;
525        }
526
527        @Override
528        void updateSurface(SurfaceHolder holder) throws Surface.OutOfResourcesException {
529            if (isRequested() && isEnabled()) {
530                createEglSurface(holder);
531            }
532        }
533
534        abstract GLES20Canvas createCanvas();
535
536        abstract int[] getConfig(boolean dirtyRegions);
537
538        void initializeEgl() {
539            synchronized (sEglLock) {
540                if (sEgl == null && sEglConfig == null) {
541                    sEgl = (EGL10) EGLContext.getEGL();
542
543                    // Get to the default display.
544                    sEglDisplay = sEgl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
545
546                    if (sEglDisplay == EGL_NO_DISPLAY) {
547                        throw new RuntimeException("eglGetDisplay failed "
548                                + GLUtils.getEGLErrorString(sEgl.eglGetError()));
549                    }
550
551                    // We can now initialize EGL for that display
552                    int[] version = new int[2];
553                    if (!sEgl.eglInitialize(sEglDisplay, version)) {
554                        throw new RuntimeException("eglInitialize failed " +
555                                GLUtils.getEGLErrorString(sEgl.eglGetError()));
556                    }
557
558                    sEglConfig = chooseEglConfig();
559                    if (sEglConfig == null) {
560                        // We tried to use EGL_SWAP_BEHAVIOR_PRESERVED_BIT, try again without
561                        if (sDirtyRegions) {
562                            sDirtyRegions = false;
563                            sEglConfig = chooseEglConfig();
564                            if (sEglConfig == null) {
565                                throw new RuntimeException("eglConfig not initialized");
566                            }
567                        } else {
568                            throw new RuntimeException("eglConfig not initialized");
569                        }
570                    }
571                }
572            }
573
574            Gl20Renderer.Gl20RendererEglContext managedContext = sEglContextStorage.get();
575            mEglContext = managedContext != null ? managedContext.getContext() : null;
576            mEglThread = Thread.currentThread();
577
578            if (mEglContext == null) {
579                mEglContext = createContext(sEgl, sEglDisplay, sEglConfig);
580                sEglContextStorage.set(new Gl20Renderer.Gl20RendererEglContext(mEglContext));
581            }
582        }
583
584        private EGLConfig chooseEglConfig() {
585            EGLConfig[] configs = new EGLConfig[1];
586            int[] configsCount = new int[1];
587            int[] configSpec = getConfig(sDirtyRegions);
588
589            // Debug
590            final String debug = SystemProperties.get(PRINT_CONFIG_PROPERTY, "");
591            if ("all".equalsIgnoreCase(debug)) {
592                sEgl.eglChooseConfig(sEglDisplay, configSpec, null, 0, configsCount);
593
594                EGLConfig[] debugConfigs = new EGLConfig[configsCount[0]];
595                sEgl.eglChooseConfig(sEglDisplay, configSpec, debugConfigs,
596                        configsCount[0], configsCount);
597
598                for (EGLConfig config : debugConfigs) {
599                    printConfig(config);
600                }
601            }
602
603            if (!sEgl.eglChooseConfig(sEglDisplay, configSpec, configs, 1, configsCount)) {
604                throw new IllegalArgumentException("eglChooseConfig failed " +
605                        GLUtils.getEGLErrorString(sEgl.eglGetError()));
606            } else if (configsCount[0] > 0) {
607                if ("choice".equalsIgnoreCase(debug)) {
608                    printConfig(configs[0]);
609                }
610                return configs[0];
611            }
612
613            return null;
614        }
615
616        private void printConfig(EGLConfig config) {
617            int[] value = new int[1];
618
619            Log.d(LOG_TAG, "EGL configuration " + config + ":");
620
621            sEgl.eglGetConfigAttrib(sEglDisplay, config, EGL_RED_SIZE, value);
622            Log.d(LOG_TAG, "  RED_SIZE = " + value[0]);
623
624            sEgl.eglGetConfigAttrib(sEglDisplay, config, EGL_GREEN_SIZE, value);
625            Log.d(LOG_TAG, "  GREEN_SIZE = " + value[0]);
626
627            sEgl.eglGetConfigAttrib(sEglDisplay, config, EGL_BLUE_SIZE, value);
628            Log.d(LOG_TAG, "  BLUE_SIZE = " + value[0]);
629
630            sEgl.eglGetConfigAttrib(sEglDisplay, config, EGL_ALPHA_SIZE, value);
631            Log.d(LOG_TAG, "  ALPHA_SIZE = " + value[0]);
632
633            sEgl.eglGetConfigAttrib(sEglDisplay, config, EGL_DEPTH_SIZE, value);
634            Log.d(LOG_TAG, "  DEPTH_SIZE = " + value[0]);
635
636            sEgl.eglGetConfigAttrib(sEglDisplay, config, EGL_STENCIL_SIZE, value);
637            Log.d(LOG_TAG, "  STENCIL_SIZE = " + value[0]);
638
639            sEgl.eglGetConfigAttrib(sEglDisplay, config, EGL_SURFACE_TYPE, value);
640            Log.d(LOG_TAG, "  SURFACE_TYPE = 0x" + Integer.toHexString(value[0]));
641        }
642
643        GL createEglSurface(SurfaceHolder holder) throws Surface.OutOfResourcesException {
644            // Check preconditions.
645            if (sEgl == null) {
646                throw new RuntimeException("egl not initialized");
647            }
648            if (sEglDisplay == null) {
649                throw new RuntimeException("eglDisplay not initialized");
650            }
651            if (sEglConfig == null) {
652                throw new RuntimeException("eglConfig not initialized");
653            }
654            if (Thread.currentThread() != mEglThread) {
655                throw new IllegalStateException("HardwareRenderer cannot be used "
656                        + "from multiple threads");
657            }
658
659            // In case we need to destroy an existing surface
660            destroySurface();
661
662            // Create an EGL surface we can render into.
663            if (!createSurface(holder)) {
664                return null;
665            }
666
667            /*
668             * Before we can issue GL commands, we need to make sure
669             * the context is current and bound to a surface.
670             */
671            if (!sEgl.eglMakeCurrent(sEglDisplay, mEglSurface, mEglSurface, mEglContext)) {
672                throw new Surface.OutOfResourcesException("eglMakeCurrent failed "
673                        + GLUtils.getEGLErrorString(sEgl.eglGetError()));
674            }
675
676            initCaches();
677
678            // If mDirtyRegions is set, this means we have an EGL configuration
679            // with EGL_SWAP_BEHAVIOR_PRESERVED_BIT set
680            if (sDirtyRegions) {
681                if (!(mDirtyRegionsEnabled = GLES20Canvas.preserveBackBuffer())) {
682                    Log.w(LOG_TAG, "Backbuffer cannot be preserved");
683                }
684            } else if (sDirtyRegionsRequested) {
685                // If mDirtyRegions is not set, our EGL configuration does not
686                // have EGL_SWAP_BEHAVIOR_PRESERVED_BIT; however, the default
687                // swap behavior might be EGL_BUFFER_PRESERVED, which means we
688                // want to set mDirtyRegions. We try to do this only if dirty
689                // regions were initially requested as part of the device
690                // configuration (see RENDER_DIRTY_REGIONS)
691                mDirtyRegionsEnabled = GLES20Canvas.isBackBufferPreserved();
692            }
693
694            return mEglContext.getGL();
695        }
696
697        abstract void initCaches();
698
699        EGLContext createContext(EGL10 egl, EGLDisplay eglDisplay, EGLConfig eglConfig) {
700            int[] attribs = { EGL_CONTEXT_CLIENT_VERSION, mGlVersion, EGL_NONE };
701
702            return egl.eglCreateContext(eglDisplay, eglConfig, EGL_NO_CONTEXT,
703                    mGlVersion != 0 ? attribs : null);
704        }
705
706        @Override
707        void destroy(boolean full) {
708            if (full && mCanvas != null) {
709                mCanvas = null;
710            }
711
712            if (!isEnabled() || mDestroyed) {
713                setEnabled(false);
714                return;
715            }
716
717            destroySurface();
718            setEnabled(false);
719
720            mDestroyed = true;
721            mGl = null;
722        }
723
724        void destroySurface() {
725            if (mEglSurface != null && mEglSurface != EGL_NO_SURFACE) {
726                sEgl.eglMakeCurrent(sEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
727                sEgl.eglDestroySurface(sEglDisplay, mEglSurface);
728                mEglSurface = null;
729            }
730        }
731
732        @Override
733        void invalidate(SurfaceHolder holder) {
734            // Cancels any existing buffer to ensure we'll get a buffer
735            // of the right size before we call eglSwapBuffers
736            sEgl.eglMakeCurrent(sEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
737
738            if (mEglSurface != null && mEglSurface != EGL_NO_SURFACE) {
739                sEgl.eglDestroySurface(sEglDisplay, mEglSurface);
740                mEglSurface = null;
741                setEnabled(false);
742            }
743
744            if (holder.getSurface().isValid()) {
745                if (!createSurface(holder)) {
746                    return;
747                }
748                if (mCanvas != null) {
749                    setEnabled(true);
750                }
751            }
752        }
753
754        private boolean createSurface(SurfaceHolder holder) {
755            mEglSurface = sEgl.eglCreateWindowSurface(sEglDisplay, sEglConfig, holder, null);
756
757            if (mEglSurface == null || mEglSurface == EGL_NO_SURFACE) {
758                int error = sEgl.eglGetError();
759                if (error == EGL_BAD_NATIVE_WINDOW) {
760                    Log.e(LOG_TAG, "createWindowSurface returned EGL_BAD_NATIVE_WINDOW.");
761                    return false;
762                }
763                throw new RuntimeException("createWindowSurface failed "
764                        + GLUtils.getEGLErrorString(error));
765            }
766            return true;
767        }
768
769        @Override
770        boolean validate() {
771            return checkCurrent() != SURFACE_STATE_ERROR;
772        }
773
774        @Override
775        void setup(int width, int height) {
776            if (validate()) {
777                mCanvas.setViewport(width, height);
778                mWidth = width;
779                mHeight = height;
780            }
781        }
782
783        @Override
784        int getWidth() {
785            return mWidth;
786        }
787
788        @Override
789        int getHeight() {
790            return mHeight;
791        }
792
793        @Override
794        HardwareCanvas getCanvas() {
795            return mCanvas;
796        }
797
798        boolean canDraw() {
799            return mGl != null && mCanvas != null;
800        }
801
802        void onPreDraw(Rect dirty) {
803        }
804
805        void onPostDraw() {
806        }
807
808        @Override
809        boolean draw(View view, View.AttachInfo attachInfo, HardwareDrawCallbacks callbacks,
810                Rect dirty) {
811            if (canDraw()) {
812                if (!hasDirtyRegions()) {
813                    dirty = null;
814                }
815                attachInfo.mIgnoreDirtyState = true;
816                attachInfo.mDrawingTime = SystemClock.uptimeMillis();
817
818                view.mPrivateFlags |= View.DRAWN;
819
820                final int surfaceState = checkCurrent();
821                if (surfaceState != SURFACE_STATE_ERROR) {
822                    // We had to change the current surface and/or context, redraw everything
823                    if (surfaceState == SURFACE_STATE_UPDATED) {
824                        dirty = null;
825                    }
826
827                    onPreDraw(dirty);
828
829                    HardwareCanvas canvas = mCanvas;
830                    attachInfo.mHardwareCanvas = canvas;
831
832                    int saveCount = canvas.save();
833                    callbacks.onHardwarePreDraw(canvas);
834
835                    try {
836                        view.mRecreateDisplayList =
837                                (view.mPrivateFlags & View.INVALIDATED) == View.INVALIDATED;
838                        view.mPrivateFlags &= ~View.INVALIDATED;
839
840                        final long getDisplayListStartTime;
841                        if (ViewDebug.DEBUG_LATENCY) {
842                            getDisplayListStartTime = System.nanoTime();
843                        }
844
845                        DisplayList displayList = view.getDisplayList();
846
847                        if (ViewDebug.DEBUG_LATENCY) {
848                            long now = System.nanoTime();
849                            Log.d(ViewDebug.DEBUG_LATENCY_TAG, "- getDisplayList() took "
850                                    + ((now - getDisplayListStartTime) * 0.000001f) + "ms");
851                        }
852
853                        if (displayList != null) {
854                            final long drawDisplayListStartTime;
855                            if (ViewDebug.DEBUG_LATENCY) {
856                                drawDisplayListStartTime = System.nanoTime();
857                            }
858
859                            boolean invalidateNeeded = canvas.drawDisplayList(
860                                    displayList, view.getWidth(),
861                                    view.getHeight(), mRedrawClip);
862
863                            if (ViewDebug.DEBUG_LATENCY) {
864                                long now = System.nanoTime();
865                                Log.d(ViewDebug.DEBUG_LATENCY_TAG, "- drawDisplayList() took "
866                                        + ((now - drawDisplayListStartTime) * 0.000001f)
867                                        + "ms, invalidateNeeded=" + invalidateNeeded + ".");
868                            }
869
870                            if (invalidateNeeded) {
871                                if (mRedrawClip.isEmpty() || view.getParent() == null) {
872                                    view.invalidate();
873                                } else {
874                                    view.getParent().invalidateChild(view, mRedrawClip);
875                                }
876                                mRedrawClip.setEmpty();
877                            }
878                        } else {
879                            // Shouldn't reach here
880                            view.draw(canvas);
881                        }
882
883                        if (DEBUG_DIRTY_REGION) {
884                            if (mDebugPaint == null) {
885                                mDebugPaint = new Paint();
886                                mDebugPaint.setColor(0x7fff0000);
887                            }
888                            if (dirty != null && (mFrameCount++ & 1) == 0) {
889                                canvas.drawRect(dirty, mDebugPaint);
890                            }
891                        }
892                    } finally {
893                        callbacks.onHardwarePostDraw(canvas);
894                        canvas.restoreToCount(saveCount);
895                        view.mRecreateDisplayList = false;
896                    }
897
898                    onPostDraw();
899
900                    attachInfo.mIgnoreDirtyState = false;
901
902                    final long eglSwapBuffersStartTime;
903                    if (ViewDebug.DEBUG_LATENCY) {
904                        eglSwapBuffersStartTime = System.nanoTime();
905                    }
906
907                    sEgl.eglSwapBuffers(sEglDisplay, mEglSurface);
908
909                    if (ViewDebug.DEBUG_LATENCY) {
910                        long now = System.nanoTime();
911                        Log.d(ViewDebug.DEBUG_LATENCY_TAG, "- eglSwapBuffers() took "
912                                + ((now - eglSwapBuffersStartTime) * 0.000001f) + "ms");
913                    }
914
915                    checkEglErrors();
916
917                    return dirty == null;
918                }
919            }
920
921            return false;
922        }
923
924        /**
925         * Ensures the current EGL context is the one we expect.
926         *
927         * @return {@link #SURFACE_STATE_ERROR} if the correct EGL context cannot be made current,
928         *         {@link #SURFACE_STATE_UPDATED} if the EGL context was changed or
929         *         {@link #SURFACE_STATE_SUCCESS} if the EGL context was the correct one
930         */
931        int checkCurrent() {
932            if (mEglThread != Thread.currentThread()) {
933                throw new IllegalStateException("Hardware acceleration can only be used with a " +
934                        "single UI thread.\nOriginal thread: " + mEglThread + "\n" +
935                        "Current thread: " + Thread.currentThread());
936            }
937
938            if (!mEglContext.equals(sEgl.eglGetCurrentContext()) ||
939                    !mEglSurface.equals(sEgl.eglGetCurrentSurface(EGL_DRAW))) {
940                if (!sEgl.eglMakeCurrent(sEglDisplay, mEglSurface, mEglSurface, mEglContext)) {
941                    Log.e(LOG_TAG, "eglMakeCurrent failed " +
942                            GLUtils.getEGLErrorString(sEgl.eglGetError()));
943                    fallback(true);
944                    return SURFACE_STATE_ERROR;
945                } else {
946                    return SURFACE_STATE_UPDATED;
947                }
948            }
949            return SURFACE_STATE_SUCCESS;
950        }
951    }
952
953    /**
954     * Hardware renderer using OpenGL ES 2.0.
955     */
956    static class Gl20Renderer extends GlRenderer {
957        private GLES20Canvas mGlCanvas;
958
959        private static EGLSurface sPbuffer;
960        private static final Object[] sPbufferLock = new Object[0];
961
962        static class Gl20RendererEglContext extends ManagedEGLContext {
963            final Handler mHandler = new Handler();
964
965            public Gl20RendererEglContext(EGLContext context) {
966                super(context);
967            }
968
969            @Override
970            public void onTerminate(final EGLContext eglContext) {
971                // Make sure we do this on the correct thread.
972                if (mHandler.getLooper() != Looper.myLooper()) {
973                    mHandler.post(new Runnable() {
974                        @Override public void run() {
975                            onTerminate(eglContext);
976                        }
977                    });
978                    return;
979                }
980
981                synchronized (sEglLock) {
982                    if (sEgl == null) return;
983
984                    if (EGLImpl.getInitCount(sEglDisplay) == 1) {
985                        usePbufferSurface(eglContext);
986                        GLES20Canvas.terminateCaches();
987
988                        sEgl.eglDestroyContext(sEglDisplay, eglContext);
989                        sEglContextStorage.remove();
990
991                        sEgl.eglDestroySurface(sEglDisplay, sPbuffer);
992                        sEgl.eglMakeCurrent(sEglDisplay, EGL_NO_SURFACE,
993                                EGL_NO_SURFACE, EGL_NO_CONTEXT);
994
995                        sEgl.eglReleaseThread();
996                        sEgl.eglTerminate(sEglDisplay);
997
998                        sEgl = null;
999                        sEglDisplay = null;
1000                        sEglConfig = null;
1001                        sPbuffer = null;
1002                        sEglContextStorage.set(null);
1003                    }
1004                }
1005            }
1006        }
1007
1008        Gl20Renderer(boolean translucent) {
1009            super(2, translucent);
1010        }
1011
1012        @Override
1013        GLES20Canvas createCanvas() {
1014            return mGlCanvas = new GLES20Canvas(mTranslucent);
1015        }
1016
1017        @Override
1018        int[] getConfig(boolean dirtyRegions) {
1019            return new int[] {
1020                    EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1021                    EGL_RED_SIZE, 8,
1022                    EGL_GREEN_SIZE, 8,
1023                    EGL_BLUE_SIZE, 8,
1024                    EGL_ALPHA_SIZE, 8,
1025                    EGL_DEPTH_SIZE, 0,
1026                    EGL_STENCIL_SIZE, 0,
1027                    EGL_SURFACE_TYPE, EGL_WINDOW_BIT |
1028                            (dirtyRegions ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0),
1029                    EGL_NONE
1030            };
1031        }
1032
1033        @Override
1034        void initCaches() {
1035            GLES20Canvas.initCaches();
1036        }
1037
1038        @Override
1039        boolean canDraw() {
1040            return super.canDraw() && mGlCanvas != null;
1041        }
1042
1043        @Override
1044        void onPreDraw(Rect dirty) {
1045            mGlCanvas.onPreDraw(dirty);
1046        }
1047
1048        @Override
1049        void onPostDraw() {
1050            mGlCanvas.onPostDraw();
1051        }
1052
1053        @Override
1054        void destroy(boolean full) {
1055            try {
1056                super.destroy(full);
1057            } finally {
1058                if (full && mGlCanvas != null) {
1059                    mGlCanvas = null;
1060                }
1061            }
1062        }
1063
1064        @Override
1065        void setup(int width, int height) {
1066            super.setup(width, height);
1067            if (mVsyncDisabled) {
1068                GLES20Canvas.disableVsync();
1069            }
1070        }
1071
1072        @Override
1073        DisplayList createDisplayList() {
1074            return new GLES20DisplayList();
1075        }
1076
1077        @Override
1078        HardwareLayer createHardwareLayer(boolean isOpaque) {
1079            return new GLES20TextureLayer(isOpaque);
1080        }
1081
1082        @Override
1083        HardwareLayer createHardwareLayer(int width, int height, boolean isOpaque) {
1084            return new GLES20RenderLayer(width, height, isOpaque);
1085        }
1086
1087        @Override
1088        SurfaceTexture createSurfaceTexture(HardwareLayer layer) {
1089            return ((GLES20TextureLayer) layer).getSurfaceTexture();
1090        }
1091
1092        @Override
1093        void destroyLayers(View view) {
1094            if (view != null && isEnabled() && checkCurrent() != SURFACE_STATE_ERROR) {
1095                destroyHardwareLayer(view);
1096                GLES20Canvas.flushCaches(GLES20Canvas.FLUSH_CACHES_LAYERS);
1097            }
1098        }
1099
1100        private static void destroyHardwareLayer(View view) {
1101            view.destroyLayer();
1102
1103            if (view instanceof ViewGroup) {
1104                ViewGroup group = (ViewGroup) view;
1105
1106                int count = group.getChildCount();
1107                for (int i = 0; i < count; i++) {
1108                    destroyHardwareLayer(group.getChildAt(i));
1109                }
1110            }
1111        }
1112
1113        @Override
1114        void destroyHardwareResources(View view) {
1115            if (view != null) {
1116                boolean needsContext = true;
1117                if (isEnabled() && checkCurrent() != SURFACE_STATE_ERROR) needsContext = false;
1118
1119                if (needsContext) {
1120                    Gl20RendererEglContext managedContext = sEglContextStorage.get();
1121                    if (managedContext == null) return;
1122                    usePbufferSurface(managedContext.getContext());
1123                }
1124
1125                destroyResources(view);
1126                GLES20Canvas.flushCaches(GLES20Canvas.FLUSH_CACHES_LAYERS);
1127            }
1128        }
1129
1130        private static void destroyResources(View view) {
1131            view.destroyHardwareResources();
1132
1133            if (view instanceof ViewGroup) {
1134                ViewGroup group = (ViewGroup) view;
1135
1136                int count = group.getChildCount();
1137                for (int i = 0; i < count; i++) {
1138                    destroyResources(group.getChildAt(i));
1139                }
1140            }
1141        }
1142
1143        static HardwareRenderer create(boolean translucent) {
1144            if (GLES20Canvas.isAvailable()) {
1145                return new Gl20Renderer(translucent);
1146            }
1147            return null;
1148        }
1149
1150        static void trimMemory(int level) {
1151            if (sEgl == null || sEglConfig == null) return;
1152
1153            Gl20RendererEglContext managedContext = sEglContextStorage.get();
1154            // We do not have OpenGL objects
1155            if (managedContext == null) {
1156                return;
1157            } else {
1158                usePbufferSurface(managedContext.getContext());
1159            }
1160
1161            switch (level) {
1162                case ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN:
1163                case ComponentCallbacks2.TRIM_MEMORY_BACKGROUND:
1164                case ComponentCallbacks2.TRIM_MEMORY_MODERATE:
1165                    GLES20Canvas.flushCaches(GLES20Canvas.FLUSH_CACHES_MODERATE);
1166                    break;
1167                case ComponentCallbacks2.TRIM_MEMORY_COMPLETE:
1168                    GLES20Canvas.flushCaches(GLES20Canvas.FLUSH_CACHES_FULL);
1169                    break;
1170            }
1171        }
1172
1173        private static void usePbufferSurface(EGLContext eglContext) {
1174            synchronized (sPbufferLock) {
1175                // Create a temporary 1x1 pbuffer so we have a context
1176                // to clear our OpenGL objects
1177                if (sPbuffer == null) {
1178                    sPbuffer = sEgl.eglCreatePbufferSurface(sEglDisplay, sEglConfig, new int[] {
1179                            EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE
1180                    });
1181                }
1182            }
1183            sEgl.eglMakeCurrent(sEglDisplay, sPbuffer, sPbuffer, eglContext);
1184        }
1185    }
1186}
1187