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