Skip to content

Commit 19f86e8

Browse files
author
Romain Guy
committed
Invoke onTrimMemory with an EGL context
Bug #6369600 Change-Id: I3ded47c3688ef2f2873495392c35e898357204da
1 parent b023bf8 commit 19f86e8

File tree

3 files changed

+46
-11
lines changed

3 files changed

+46
-11
lines changed

core/java/android/app/ActivityThread.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3765,9 +3765,11 @@ final void handleLowMemory() {
37653765

37663766
final void handleTrimMemory(int level) {
37673767
if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Trimming memory to level: " + level);
3768-
WindowManagerImpl.getDefault().trimMemory(level);
3769-
ArrayList<ComponentCallbacks2> callbacks;
37703768

3769+
final WindowManagerImpl windowManager = WindowManagerImpl.getDefault();
3770+
windowManager.startTrimMemory(level);
3771+
3772+
ArrayList<ComponentCallbacks2> callbacks;
37713773
synchronized (mPackages) {
37723774
callbacks = collectComponentCallbacksLocked(true, null);
37733775
}
@@ -3776,7 +3778,8 @@ final void handleTrimMemory(int level) {
37763778
for (int i = 0; i < N; i++) {
37773779
callbacks.get(i).onTrimMemory(level);
37783780
}
3779-
WindowManagerImpl.getDefault().terminateEgl();
3781+
3782+
windowManager.endTrimMemory();
37803783
}
37813784

37823785
private void setupGraphicsSupport(LoadedApk info) {

core/java/android/view/HardwareRenderer.java

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,28 @@ static HardwareRenderer createGlRenderer(int glVersion, boolean translucent) {
484484
* see {@link android.content.ComponentCallbacks}
485485
*/
486486
static void trimMemory(int level) {
487-
Gl20Renderer.trimMemory(level);
487+
startTrimMemory(level);
488+
endTrimMemory();
489+
}
490+
491+
/**
492+
* Starts the process of trimming memory. Usually this call will setup
493+
* hardware rendering context and reclaim memory.Extra cleanup might
494+
* be required by calling {@link #endTrimMemory()}.
495+
*
496+
* @param level Hint about the amount of memory that should be trimmed,
497+
* see {@link android.content.ComponentCallbacks}
498+
*/
499+
static void startTrimMemory(int level) {
500+
Gl20Renderer.startTrimMemory(level);
501+
}
502+
503+
/**
504+
* Finishes the process of trimming memory. This method will usually
505+
* cleanup special resources used by the memory trimming process.
506+
*/
507+
static void endTrimMemory() {
508+
Gl20Renderer.endTrimMemory();
488509
}
489510

490511
/**
@@ -1122,12 +1143,15 @@ boolean draw(View view, View.AttachInfo attachInfo, HardwareDrawCallbacks callba
11221143
callbacks.onHardwarePostDraw(canvas);
11231144
canvas.restoreToCount(saveCount);
11241145
view.mRecreateDisplayList = false;
1146+
11251147
mFrameCount++;
1148+
11261149
if (mDebugDirtyRegions) {
11271150
if (mDebugPaint == null) {
11281151
mDebugPaint = new Paint();
11291152
mDebugPaint.setColor(0x7fff0000);
11301153
}
1154+
11311155
if (dirty != null && (mFrameCount & 1) == 0) {
11321156
canvas.drawRect(dirty, mDebugPaint);
11331157
}
@@ -1446,7 +1470,7 @@ static HardwareRenderer create(boolean translucent) {
14461470
return null;
14471471
}
14481472

1449-
static void trimMemory(int level) {
1473+
static void startTrimMemory(int level) {
14501474
if (sEgl == null || sEglConfig == null) return;
14511475

14521476
Gl20RendererEglContext managedContext =
@@ -1463,9 +1487,12 @@ static void trimMemory(int level) {
14631487
} else if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
14641488
GLES20Canvas.flushCaches(GLES20Canvas.FLUSH_CACHES_MODERATE);
14651489
}
1490+
}
14661491

1467-
sEgl.eglMakeCurrent(sEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
1468-
EGL_NO_CONTEXT);
1492+
static void endTrimMemory() {
1493+
if (sEgl != null && sEglDisplay != null) {
1494+
sEgl.eglMakeCurrent(sEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
1495+
}
14691496
}
14701497

14711498
private static void usePbufferSurface(EGLContext eglContext) {

core/java/android/view/WindowManagerImpl.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,10 @@ public void closeAll(IBinder token, String who, String what) {
429429

430430
/**
431431
* @param level See {@link android.content.ComponentCallbacks}
432+
*
433+
* @hide
432434
*/
433-
public void trimMemory(int level) {
435+
public void startTrimMemory(int level) {
434436
if (HardwareRenderer.isAvailable()) {
435437
// On low-end gfx devices we trim when memory is moderate;
436438
// on high-end devices we do this when low.
@@ -447,18 +449,21 @@ public void trimMemory(int level) {
447449
}
448450
}
449451
// Force a full memory flush
450-
HardwareRenderer.trimMemory(ComponentCallbacks2.TRIM_MEMORY_COMPLETE);
451452
mNeedsEglTerminate = true;
453+
HardwareRenderer.startTrimMemory(ComponentCallbacks2.TRIM_MEMORY_COMPLETE);
452454
return;
453455
}
454-
HardwareRenderer.trimMemory(level);
456+
457+
HardwareRenderer.startTrimMemory(level);
455458
}
456459
}
457460

458461
/**
459462
* @hide
460463
*/
461-
public void terminateEgl() {
464+
public void endTrimMemory() {
465+
HardwareRenderer.endTrimMemory();
466+
462467
if (mNeedsEglTerminate) {
463468
ManagedEGLContext.doTerminate();
464469
mNeedsEglTerminate = false;

0 commit comments

Comments
 (0)