ManagedEGLContext.java revision 31f2c2e94656530fbf6282803e62edb47e9a894d
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
17package android.opengl;
18
19import static javax.microedition.khronos.egl.EGL10.EGL_DEFAULT_DISPLAY;
20import static javax.microedition.khronos.egl.EGL10.EGL_NO_DISPLAY;
21
22import java.util.ArrayList;
23
24import javax.microedition.khronos.egl.EGL10;
25import javax.microedition.khronos.egl.EGLContext;
26import javax.microedition.khronos.egl.EGLDisplay;
27
28import android.os.Looper;
29import android.util.Log;
30
31import com.google.android.gles_jni.EGLImpl;
32
33/**
34 * The per-process memory overhead of hardware accelerated graphics can
35 * be quite large on some devices.  For small memory devices, being able to
36 * terminate all EGL contexts so that this graphics driver memory can be
37 * reclaimed can significant improve the overall behavior of the device.  This
38 * class helps app developers participate in releasing their EGL context
39 * when appropriate and possible.
40 *
41 * <p>To use, simple instantiate this class with the EGLContext you create.
42 * When you have done this, if the device is getting low on memory and all
43 * of the currently created EGL contexts in the process are being managed
44 * through this class, then they will all be asked to terminate through the
45 * call to {@link #onTerminate}.
46 */
47public abstract class ManagedEGLContext {
48    static final String TAG = "ManagedEGLContext";
49
50    static final ArrayList<ManagedEGLContext> sActive = new ArrayList<ManagedEGLContext>();
51
52    final EGLContext mContext;
53
54    /**
55     * Instantiate to manage the given EGLContext.
56     */
57    public ManagedEGLContext(EGLContext context) {
58        mContext = context;
59        synchronized (sActive) {
60            sActive.add(this);
61        }
62    }
63
64    /**
65     * Retrieve the EGLContext being managed by the class.
66     */
67    public EGLContext getContext() {
68        return mContext;
69    }
70
71    /**
72     * Force-terminate the ManagedEGLContext.  This will cause
73     * {@link #onTerminate(EGLContext)} to be called.  You <em>must</em>
74     * call this when destroying the EGLContext, so that the framework
75     * knows to stop managing it.
76     */
77    public void terminate() {
78        execTerminate();
79    }
80
81    void execTerminate() {
82        onTerminate(mContext);
83    }
84
85    /**
86     * Override this method to destroy the EGLContext when appropriate.
87     * <em>Note that this method is always called on the main thread
88     * of the process.</em>  If your EGLContext was created on a different
89     * thread, you will need to implement this method to hand off the work
90     * of destroying the context to that thread.
91     */
92    public abstract void onTerminate(EGLContext context);
93
94    /** @hide */
95    public static boolean doTerminate() {
96        ArrayList<ManagedEGLContext> active;
97
98        if (Looper.getMainLooper() != Looper.myLooper()) {
99            throw new IllegalStateException("Called on wrong thread");
100        }
101
102        synchronized (sActive) {
103            // If there are no active managed contexts, we will not even
104            // try to terminate.
105            if (sActive.size() <= 0) {
106                return false;
107            }
108
109            // Need to check how many EGL contexts are actually running,
110            // to compare with how many we are managing.
111            EGL10 egl = (EGL10) EGLContext.getEGL();
112            EGLDisplay display = egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
113
114            if (display == EGL_NO_DISPLAY) {
115                Log.w(TAG, "doTerminate failed: no display");
116                return false;
117            }
118
119            if (EGLImpl.getInitCount(display) != sActive.size()) {
120                Log.w(TAG, "doTerminate failed: EGL count is " + EGLImpl.getInitCount(display)
121                        + " but managed count is " + sActive.size());
122                return false;
123            }
124
125            active = new ArrayList<ManagedEGLContext>(sActive);
126            sActive.clear();
127        }
128
129        for (int i = 0; i < active.size(); i++) {
130            active.get(i).execTerminate();
131        }
132
133        return true;
134    }
135}
136