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