Skip to content

Commit 717a25d

Browse files
author
Dianne Hackborn
committed
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
1 parent 4c6a65b commit 717a25d

File tree

4 files changed

+204
-38
lines changed

4 files changed

+204
-38
lines changed

api/current.txt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14237,6 +14237,13 @@ package android.opengl {
1423714237
method public static void texSubImage2D(int, int, int, int, android.graphics.Bitmap, int, int);
1423814238
}
1423914239

14240+
public abstract class ManagedEGLContext {
14241+
ctor public ManagedEGLContext(javax.microedition.khronos.egl.EGLContext);
14242+
method public javax.microedition.khronos.egl.EGLContext getContext();
14243+
method public abstract void onTerminate(javax.microedition.khronos.egl.EGLContext);
14244+
method public void terminate();
14245+
}
14246+
1424014247
public class Matrix {
1424114248
ctor public Matrix();
1424214249
method public static void frustumM(float[], int, float, float, float, float, float, float);
@@ -18419,14 +18426,14 @@ package android.renderscript {
1841918426
ctor public RSSurfaceView(android.content.Context);
1842018427
ctor public RSSurfaceView(android.content.Context, android.util.AttributeSet);
1842118428
method public android.renderscript.RenderScriptGL createRenderScriptGL(android.renderscript.RenderScriptGL.SurfaceConfig);
18422-
method public void destroyRenderScriptGL();
18429+
method public synchronized void destroyRenderScriptGL();
1842318430
method public android.renderscript.RenderScriptGL getRenderScriptGL();
1842418431
method public void pause();
1842518432
method public void resume();
1842618433
method public void setRenderScriptGL(android.renderscript.RenderScriptGL);
18427-
method public void surfaceChanged(android.view.SurfaceHolder, int, int, int);
18434+
method public synchronized void surfaceChanged(android.view.SurfaceHolder, int, int, int);
1842818435
method public void surfaceCreated(android.view.SurfaceHolder);
18429-
method public void surfaceDestroyed(android.view.SurfaceHolder);
18436+
method public synchronized void surfaceDestroyed(android.view.SurfaceHolder);
1843018437
}
1843118438

1843218439
public class RSTextureView extends android.view.TextureView implements android.view.TextureView.SurfaceTextureListener {

core/java/android/view/HardwareRenderer.java

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
import android.graphics.Rect;
2323
import android.graphics.SurfaceTexture;
2424
import android.opengl.GLUtils;
25+
import android.opengl.ManagedEGLContext;
26+
import android.os.Handler;
27+
import android.os.Looper;
2528
import android.os.SystemClock;
2629
import android.os.SystemProperties;
2730
import android.util.Log;
@@ -409,7 +412,8 @@ static abstract class GlRenderer extends HardwareRenderer {
409412
static final Object[] sEglLock = new Object[0];
410413
int mWidth = -1, mHeight = -1;
411414

412-
static final ThreadLocal<EGLContext> sEglContextStorage = new ThreadLocal<EGLContext>();
415+
static final ThreadLocal<Gl20Renderer.MyEGLContext> sEglContextStorage
416+
= new ThreadLocal<Gl20Renderer.MyEGLContext>();
413417

414418
EGLContext mEglContext;
415419
Thread mEglThread;
@@ -561,12 +565,13 @@ void initializeEgl() {
561565
}
562566
}
563567

564-
mEglContext = sEglContextStorage.get();
568+
Gl20Renderer.MyEGLContext managedContext = sEglContextStorage.get();
569+
mEglContext = managedContext != null ? managedContext.getContext() : null;
565570
mEglThread = Thread.currentThread();
566571

567572
if (mEglContext == null) {
568573
mEglContext = createContext(sEgl, sEglDisplay, sEglConfig);
569-
sEglContextStorage.set(mEglContext);
574+
sEglContextStorage.set(new Gl20Renderer.MyEGLContext(mEglContext));
570575
}
571576
}
572577

@@ -904,6 +909,51 @@ static class Gl20Renderer extends GlRenderer {
904909
private static EGLSurface sPbuffer;
905910
private static final Object[] sPbufferLock = new Object[0];
906911

912+
static class MyEGLContext extends ManagedEGLContext {
913+
final Handler mHandler = new Handler();
914+
915+
public MyEGLContext(EGLContext context) {
916+
super(context);
917+
}
918+
919+
@Override
920+
public void onTerminate(final EGLContext eglContext) {
921+
// Make sure we do this on the correct thread.
922+
if (mHandler.getLooper() != Looper.myLooper()) {
923+
mHandler.post(new Runnable() {
924+
@Override public void run() {
925+
onTerminate(eglContext);
926+
}
927+
});
928+
return;
929+
}
930+
931+
synchronized (sEglLock) {
932+
if (sEgl == null) return;
933+
934+
if (EGLImpl.getInitCount(sEglDisplay) == 1) {
935+
usePbufferSurface(eglContext);
936+
GLES20Canvas.terminateCaches();
937+
938+
sEgl.eglDestroyContext(sEglDisplay, eglContext);
939+
sEglContextStorage.remove();
940+
941+
sEgl.eglDestroySurface(sEglDisplay, sPbuffer);
942+
sEgl.eglMakeCurrent(sEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
943+
944+
sEgl.eglReleaseThread();
945+
sEgl.eglTerminate(sEglDisplay);
946+
947+
sEgl = null;
948+
sEglDisplay = null;
949+
sEglConfig = null;
950+
sPbuffer = null;
951+
sEglContextStorage.set(null);
952+
}
953+
}
954+
}
955+
}
956+
907957
Gl20Renderer(boolean translucent) {
908958
super(2, translucent);
909959
}
@@ -1020,12 +1070,12 @@ static HardwareRenderer create(boolean translucent) {
10201070
static void trimMemory(int level) {
10211071
if (sEgl == null || sEglConfig == null) return;
10221072

1023-
EGLContext eglContext = sEglContextStorage.get();
1073+
Gl20Renderer.MyEGLContext managedContext = sEglContextStorage.get();
10241074
// We do not have OpenGL objects
1025-
if (eglContext == null) {
1075+
if (managedContext == null) {
10261076
return;
10271077
} else {
1028-
usePbufferSurface(eglContext);
1078+
usePbufferSurface(managedContext.getContext());
10291079
}
10301080

10311081
switch (level) {
@@ -1052,33 +1102,5 @@ private static void usePbufferSurface(EGLContext eglContext) {
10521102
}
10531103
sEgl.eglMakeCurrent(sEglDisplay, sPbuffer, sPbuffer, eglContext);
10541104
}
1055-
1056-
static void terminate() {
1057-
synchronized (sEglLock) {
1058-
if (sEgl == null) return;
1059-
1060-
if (EGLImpl.getInitCount(sEglDisplay) == 1) {
1061-
EGLContext eglContext = sEglContextStorage.get();
1062-
if (eglContext == null) return;
1063-
1064-
usePbufferSurface(eglContext);
1065-
GLES20Canvas.terminateCaches();
1066-
1067-
sEgl.eglDestroyContext(sEglDisplay, eglContext);
1068-
sEglContextStorage.remove();
1069-
1070-
sEgl.eglDestroySurface(sEglDisplay, sPbuffer);
1071-
sEgl.eglMakeCurrent(sEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
1072-
1073-
sEgl.eglReleaseThread();
1074-
sEgl.eglTerminate(sEglDisplay);
1075-
1076-
sEgl = null;
1077-
sEglDisplay = null;
1078-
sEglConfig = null;
1079-
sPbuffer = null;
1080-
}
1081-
}
1082-
}
10831105
}
10841106
}

core/java/android/view/WindowManagerImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import android.content.res.CompatibilityInfo;
2222
import android.content.res.Configuration;
2323
import android.graphics.PixelFormat;
24+
import android.opengl.ManagedEGLContext;
2425
import android.os.IBinder;
2526
import android.util.AndroidRuntimeException;
2627
import android.util.Log;
@@ -428,7 +429,7 @@ public void trimMemory(int level) {
428429
}
429430
}
430431
// Terminate the hardware renderer to free all resources
431-
HardwareRenderer.terminate();
432+
ManagedEGLContext.doTerminate();
432433
break;
433434
}
434435
// high end gfx devices fall through to next case
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
package android.opengl;
18+
19+
import static javax.microedition.khronos.egl.EGL10.EGL_DEFAULT_DISPLAY;
20+
import static javax.microedition.khronos.egl.EGL10.EGL_NO_DISPLAY;
21+
22+
import java.util.ArrayList;
23+
24+
import javax.microedition.khronos.egl.EGL10;
25+
import javax.microedition.khronos.egl.EGLContext;
26+
import javax.microedition.khronos.egl.EGLDisplay;
27+
28+
import android.os.Looper;
29+
import android.util.Log;
30+
31+
import 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+
*/
47+
public 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+
}

0 commit comments

Comments
 (0)