ManagedEGLContext.java revision 717a25dc2a411edb548859cd6870363346c71b01
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 51 = new ArrayList<ManagedEGLContext>(); 52 53 final EGLContext mContext; 54 55 /** 56 * Instantiate to manage the given EGLContext. 57 */ 58 public ManagedEGLContext(EGLContext context) { 59 mContext = context; 60 synchronized (sActive) { 61 sActive.add(this); 62 } 63 } 64 65 /** 66 * Retrieve the EGLContext being managed by the class. 67 */ 68 public EGLContext getContext() { 69 return mContext; 70 } 71 72 /** 73 * Force-terminate the ManagedEGLContext. This will cause 74 * {@link #onTerminate(EGLContext)} to be called. You <em>must</em> 75 * call this when destroying the EGLContext, so that the framework 76 * knows to stop managing it. 77 */ 78 public void terminate() { 79 execTerminate(); 80 } 81 82 void execTerminate() { 83 onTerminate(mContext); 84 } 85 86 /** 87 * Override this method to destroy the EGLContext when appropriate. 88 * <em>Note that this method is always called on the main thread 89 * of the process.</em> If your EGLContext was created on a different 90 * thread, you will need to implement this method to hand off the work 91 * of destroying the context to that thread. 92 */ 93 public abstract void onTerminate(EGLContext context); 94 95 /** @hide */ 96 public static boolean doTerminate() { 97 ArrayList<ManagedEGLContext> active; 98 99 if (Looper.getMainLooper() != Looper.myLooper()) { 100 throw new IllegalStateException("Called on wrong thread"); 101 } 102 103 synchronized (sActive) { 104 // If there are no active managed contexts, we will not even 105 // try to terminate. 106 if (sActive.size() <= 0) { 107 return false; 108 } 109 110 // Need to check how many EGL contexts are actually running, 111 // to compare with how many we are managing. 112 EGL10 egl = (EGL10) EGLContext.getEGL(); 113 EGLDisplay display = egl.eglGetDisplay(EGL_DEFAULT_DISPLAY); 114 115 if (display == EGL_NO_DISPLAY) { 116 Log.w(TAG, "doTerminate failed: no display"); 117 return false; 118 } 119 120 if (EGLImpl.getInitCount(display) != sActive.size()) { 121 Log.w(TAG, "doTerminate failed: EGL count is " + EGLImpl.getInitCount(display) 122 + " but managed count is " + sActive.size()); 123 return false; 124 } 125 126 active = new ArrayList<ManagedEGLContext>(sActive); 127 sActive.clear(); 128 } 129 130 for (int i=0; i<active.size(); i++) { 131 active.get(i).execTerminate(); 132 } 133 134 return true; 135 } 136} 137