Add new ManagedEGLContext class to help apps participate in memory trimming.
This class provides an API for an application to know when it is time to destroy its EGL context when memory is being trimmed. By having this in the framework, we can still detect whether it will be useful to destroy any EGL contexts (because we know if doing so will destroy all of them). Change-Id: I1eac8d640052778052926b875c7928008f752182
This commit is contained in:
@ -14237,6 +14237,13 @@ package android.opengl {
|
||||
method public static void texSubImage2D(int, int, int, int, android.graphics.Bitmap, int, int);
|
||||
}
|
||||
|
||||
public abstract class ManagedEGLContext {
|
||||
ctor public ManagedEGLContext(javax.microedition.khronos.egl.EGLContext);
|
||||
method public javax.microedition.khronos.egl.EGLContext getContext();
|
||||
method public abstract void onTerminate(javax.microedition.khronos.egl.EGLContext);
|
||||
method public void terminate();
|
||||
}
|
||||
|
||||
public class Matrix {
|
||||
ctor public Matrix();
|
||||
method public static void frustumM(float[], int, float, float, float, float, float, float);
|
||||
@ -18419,14 +18426,14 @@ package android.renderscript {
|
||||
ctor public RSSurfaceView(android.content.Context);
|
||||
ctor public RSSurfaceView(android.content.Context, android.util.AttributeSet);
|
||||
method public android.renderscript.RenderScriptGL createRenderScriptGL(android.renderscript.RenderScriptGL.SurfaceConfig);
|
||||
method public void destroyRenderScriptGL();
|
||||
method public synchronized void destroyRenderScriptGL();
|
||||
method public android.renderscript.RenderScriptGL getRenderScriptGL();
|
||||
method public void pause();
|
||||
method public void resume();
|
||||
method public void setRenderScriptGL(android.renderscript.RenderScriptGL);
|
||||
method public void surfaceChanged(android.view.SurfaceHolder, int, int, int);
|
||||
method public synchronized void surfaceChanged(android.view.SurfaceHolder, int, int, int);
|
||||
method public void surfaceCreated(android.view.SurfaceHolder);
|
||||
method public void surfaceDestroyed(android.view.SurfaceHolder);
|
||||
method public synchronized void surfaceDestroyed(android.view.SurfaceHolder);
|
||||
}
|
||||
|
||||
public class RSTextureView extends android.view.TextureView implements android.view.TextureView.SurfaceTextureListener {
|
||||
|
@ -22,6 +22,9 @@ import android.graphics.Paint;
|
||||
import android.graphics.Rect;
|
||||
import android.graphics.SurfaceTexture;
|
||||
import android.opengl.GLUtils;
|
||||
import android.opengl.ManagedEGLContext;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.os.SystemClock;
|
||||
import android.os.SystemProperties;
|
||||
import android.util.Log;
|
||||
@ -409,7 +412,8 @@ public abstract class HardwareRenderer {
|
||||
static final Object[] sEglLock = new Object[0];
|
||||
int mWidth = -1, mHeight = -1;
|
||||
|
||||
static final ThreadLocal<EGLContext> sEglContextStorage = new ThreadLocal<EGLContext>();
|
||||
static final ThreadLocal<Gl20Renderer.MyEGLContext> sEglContextStorage
|
||||
= new ThreadLocal<Gl20Renderer.MyEGLContext>();
|
||||
|
||||
EGLContext mEglContext;
|
||||
Thread mEglThread;
|
||||
@ -561,12 +565,13 @@ public abstract class HardwareRenderer {
|
||||
}
|
||||
}
|
||||
|
||||
mEglContext = sEglContextStorage.get();
|
||||
Gl20Renderer.MyEGLContext managedContext = sEglContextStorage.get();
|
||||
mEglContext = managedContext != null ? managedContext.getContext() : null;
|
||||
mEglThread = Thread.currentThread();
|
||||
|
||||
if (mEglContext == null) {
|
||||
mEglContext = createContext(sEgl, sEglDisplay, sEglConfig);
|
||||
sEglContextStorage.set(mEglContext);
|
||||
sEglContextStorage.set(new Gl20Renderer.MyEGLContext(mEglContext));
|
||||
}
|
||||
}
|
||||
|
||||
@ -904,6 +909,51 @@ public abstract class HardwareRenderer {
|
||||
private static EGLSurface sPbuffer;
|
||||
private static final Object[] sPbufferLock = new Object[0];
|
||||
|
||||
static class MyEGLContext extends ManagedEGLContext {
|
||||
final Handler mHandler = new Handler();
|
||||
|
||||
public MyEGLContext(EGLContext context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTerminate(final EGLContext eglContext) {
|
||||
// Make sure we do this on the correct thread.
|
||||
if (mHandler.getLooper() != Looper.myLooper()) {
|
||||
mHandler.post(new Runnable() {
|
||||
@Override public void run() {
|
||||
onTerminate(eglContext);
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
synchronized (sEglLock) {
|
||||
if (sEgl == null) return;
|
||||
|
||||
if (EGLImpl.getInitCount(sEglDisplay) == 1) {
|
||||
usePbufferSurface(eglContext);
|
||||
GLES20Canvas.terminateCaches();
|
||||
|
||||
sEgl.eglDestroyContext(sEglDisplay, eglContext);
|
||||
sEglContextStorage.remove();
|
||||
|
||||
sEgl.eglDestroySurface(sEglDisplay, sPbuffer);
|
||||
sEgl.eglMakeCurrent(sEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
|
||||
|
||||
sEgl.eglReleaseThread();
|
||||
sEgl.eglTerminate(sEglDisplay);
|
||||
|
||||
sEgl = null;
|
||||
sEglDisplay = null;
|
||||
sEglConfig = null;
|
||||
sPbuffer = null;
|
||||
sEglContextStorage.set(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Gl20Renderer(boolean translucent) {
|
||||
super(2, translucent);
|
||||
}
|
||||
@ -1020,12 +1070,12 @@ public abstract class HardwareRenderer {
|
||||
static void trimMemory(int level) {
|
||||
if (sEgl == null || sEglConfig == null) return;
|
||||
|
||||
EGLContext eglContext = sEglContextStorage.get();
|
||||
Gl20Renderer.MyEGLContext managedContext = sEglContextStorage.get();
|
||||
// We do not have OpenGL objects
|
||||
if (eglContext == null) {
|
||||
if (managedContext == null) {
|
||||
return;
|
||||
} else {
|
||||
usePbufferSurface(eglContext);
|
||||
usePbufferSurface(managedContext.getContext());
|
||||
}
|
||||
|
||||
switch (level) {
|
||||
@ -1052,33 +1102,5 @@ public abstract class HardwareRenderer {
|
||||
}
|
||||
sEgl.eglMakeCurrent(sEglDisplay, sPbuffer, sPbuffer, eglContext);
|
||||
}
|
||||
|
||||
static void terminate() {
|
||||
synchronized (sEglLock) {
|
||||
if (sEgl == null) return;
|
||||
|
||||
if (EGLImpl.getInitCount(sEglDisplay) == 1) {
|
||||
EGLContext eglContext = sEglContextStorage.get();
|
||||
if (eglContext == null) return;
|
||||
|
||||
usePbufferSurface(eglContext);
|
||||
GLES20Canvas.terminateCaches();
|
||||
|
||||
sEgl.eglDestroyContext(sEglDisplay, eglContext);
|
||||
sEglContextStorage.remove();
|
||||
|
||||
sEgl.eglDestroySurface(sEglDisplay, sPbuffer);
|
||||
sEgl.eglMakeCurrent(sEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
|
||||
|
||||
sEgl.eglReleaseThread();
|
||||
sEgl.eglTerminate(sEglDisplay);
|
||||
|
||||
sEgl = null;
|
||||
sEglDisplay = null;
|
||||
sEglConfig = null;
|
||||
sPbuffer = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ import android.content.ComponentCallbacks2;
|
||||
import android.content.res.CompatibilityInfo;
|
||||
import android.content.res.Configuration;
|
||||
import android.graphics.PixelFormat;
|
||||
import android.opengl.ManagedEGLContext;
|
||||
import android.os.IBinder;
|
||||
import android.util.AndroidRuntimeException;
|
||||
import android.util.Log;
|
||||
@ -428,7 +429,7 @@ public class WindowManagerImpl implements WindowManager {
|
||||
}
|
||||
}
|
||||
// Terminate the hardware renderer to free all resources
|
||||
HardwareRenderer.terminate();
|
||||
ManagedEGLContext.doTerminate();
|
||||
break;
|
||||
}
|
||||
// high end gfx devices fall through to next case
|
||||
|
136
opengl/java/android/opengl/ManagedEGLContext.java
Normal file
136
opengl/java/android/opengl/ManagedEGLContext.java
Normal file
@ -0,0 +1,136 @@
|
||||
/*
|
||||
* Copyright (C) 2010 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package android.opengl;
|
||||
|
||||
import static javax.microedition.khronos.egl.EGL10.EGL_DEFAULT_DISPLAY;
|
||||
import static javax.microedition.khronos.egl.EGL10.EGL_NO_DISPLAY;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import javax.microedition.khronos.egl.EGL10;
|
||||
import javax.microedition.khronos.egl.EGLContext;
|
||||
import javax.microedition.khronos.egl.EGLDisplay;
|
||||
|
||||
import android.os.Looper;
|
||||
import android.util.Log;
|
||||
|
||||
import com.google.android.gles_jni.EGLImpl;
|
||||
|
||||
/**
|
||||
* The per-process memory overhead of hardware accelerated graphics can
|
||||
* be quite large on some devices. For small memory devices, being able to
|
||||
* terminate all EGL contexts so that this graphics driver memory can be
|
||||
* reclaimed can significant improve the overall behavior of the device. This
|
||||
* class helps app developers participate in releasing their EGL context
|
||||
* when appropriate and possible.
|
||||
*
|
||||
* <p>To use, simple instantiate this class with the EGLContext you create.
|
||||
* When you have done this, if the device is getting low on memory and all
|
||||
* of the currently created EGL contexts in the process are being managed
|
||||
* through this class, then they will all be asked to terminate through the
|
||||
* call to {@link #onTerminate}.
|
||||
*/
|
||||
public abstract class ManagedEGLContext {
|
||||
static final String TAG = "ManagedEGLContext";
|
||||
|
||||
static final ArrayList<ManagedEGLContext> sActive
|
||||
= new ArrayList<ManagedEGLContext>();
|
||||
|
||||
final EGLContext mContext;
|
||||
|
||||
/**
|
||||
* Instantiate to manage the given EGLContext.
|
||||
*/
|
||||
public ManagedEGLContext(EGLContext context) {
|
||||
mContext = context;
|
||||
synchronized (sActive) {
|
||||
sActive.add(this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the EGLContext being managed by the class.
|
||||
*/
|
||||
public EGLContext getContext() {
|
||||
return mContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* Force-terminate the ManagedEGLContext. This will cause
|
||||
* {@link #onTerminate(EGLContext)} to be called. You <em>must</em>
|
||||
* call this when destroying the EGLContext, so that the framework
|
||||
* knows to stop managing it.
|
||||
*/
|
||||
public void terminate() {
|
||||
execTerminate();
|
||||
}
|
||||
|
||||
void execTerminate() {
|
||||
onTerminate(mContext);
|
||||
}
|
||||
|
||||
/**
|
||||
* Override this method to destroy the EGLContext when appropriate.
|
||||
* <em>Note that this method is always called on the main thread
|
||||
* of the process.</em> If your EGLContext was created on a different
|
||||
* thread, you will need to implement this method to hand off the work
|
||||
* of destroying the context to that thread.
|
||||
*/
|
||||
public abstract void onTerminate(EGLContext context);
|
||||
|
||||
/** @hide */
|
||||
public static boolean doTerminate() {
|
||||
ArrayList<ManagedEGLContext> active;
|
||||
|
||||
if (Looper.getMainLooper() != Looper.myLooper()) {
|
||||
throw new IllegalStateException("Called on wrong thread");
|
||||
}
|
||||
|
||||
synchronized (sActive) {
|
||||
// If there are no active managed contexts, we will not even
|
||||
// try to terminate.
|
||||
if (sActive.size() <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Need to check how many EGL contexts are actually running,
|
||||
// to compare with how many we are managing.
|
||||
EGL10 egl = (EGL10) EGLContext.getEGL();
|
||||
EGLDisplay display = egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
|
||||
|
||||
if (display == EGL_NO_DISPLAY) {
|
||||
Log.w(TAG, "doTerminate failed: no display");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (EGLImpl.getInitCount(display) != sActive.size()) {
|
||||
Log.w(TAG, "doTerminate failed: EGL count is " + EGLImpl.getInitCount(display)
|
||||
+ " but managed count is " + sActive.size());
|
||||
return false;
|
||||
}
|
||||
|
||||
active = new ArrayList<ManagedEGLContext>(sActive);
|
||||
sActive.clear();
|
||||
}
|
||||
|
||||
for (int i=0; i<active.size(); i++) {
|
||||
active.get(i).execTerminate();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user