Skip to content

Commit c61d70e

Browse files
committed
Cache wallpaper bitmap to avoid re-loading on rotation
Screen rotations force static wallpapers to get recreated. One of the things that happens is that the underlying bitmap resource is loaded. This can be quite expensive for large bitmaps (which is the case on large-display devices). A simple optimization is to retain the bitmap in the wallpaper process, to avoid this re-loading step. We still re-draw and re-upload the texture, but at least we don't re-load the thing. Issue #7324823 Manta wallpaper decode performance is atrocious Change-Id: I0748e275a55992d13704a7dec5910d2dbdc9e2a4
1 parent f6138f0 commit c61d70e

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

packages/SystemUI/src/com/android/systemui/ImageWallpaper.java

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import android.app.ActivityManager;
2020
import android.app.WallpaperManager;
2121
import android.content.BroadcastReceiver;
22+
import android.content.ComponentCallbacks2;
2223
import android.content.Context;
2324
import android.content.Intent;
2425
import android.graphics.Bitmap;
@@ -62,6 +63,8 @@ public class ImageWallpaper extends WallpaperService {
6263

6364
WallpaperManager mWallpaperManager;
6465

66+
DrawableEngine mEngine;
67+
6568
boolean mIsHwAccelerated;
6669

6770
@Override
@@ -77,12 +80,20 @@ public void onCreate() {
7780
}
7881
}
7982

83+
@Override
84+
public void onTrimMemory(int level) {
85+
if (mEngine != null) {
86+
mEngine.trimMemory(level);
87+
}
88+
}
89+
8090
private static boolean isEmulator() {
8191
return "1".equals(SystemProperties.get(PROPERTY_KERNEL_QEMU, "0"));
8292
}
8393

8494
public Engine onCreateEngine() {
85-
return new DrawableEngine();
95+
mEngine = new DrawableEngine();
96+
return mEngine;
8697
}
8798

8899
class DrawableEngine extends Engine {
@@ -155,6 +166,15 @@ public DrawableEngine() {
155166
setFixedSizeAllowed(true);
156167
}
157168

169+
public void trimMemory(int level) {
170+
if (level >= ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW &&
171+
mBackground != null && mIsHwAccelerated) {
172+
mBackground.recycle();
173+
mBackground = null;
174+
mWallpaperManager.forgetLoadedWallpaper();
175+
}
176+
}
177+
158178
@Override
159179
public void onCreate(SurfaceHolder surfaceHolder) {
160180
if (DEBUG) {
@@ -329,16 +349,17 @@ void drawFrameLocked() {
329349
}
330350
} else {
331351
drawWallpaperWithCanvas(sh, availw, availh, xPixels, yPixels);
352+
if (FIXED_SIZED_SURFACE) {
353+
// If the surface is fixed-size, we should only need to
354+
// draw it once and then we'll let the window manager
355+
// position it appropriately. As such, we no longer needed
356+
// the loaded bitmap. Yay!
357+
// hw-accelerated path retains bitmap for faster rotation
358+
mBackground = null;
359+
mWallpaperManager.forgetLoadedWallpaper();
360+
}
332361
}
333362

334-
if (FIXED_SIZED_SURFACE) {
335-
// If the surface is fixed-size, we should only need to
336-
// draw it once and then we'll let the window manager
337-
// position it appropriately. As such, we no longer needed
338-
// the loaded bitmap. Yay!
339-
mBackground = null;
340-
mWallpaperManager.forgetLoadedWallpaper();
341-
}
342363
}
343364

344365
void updateWallpaperLocked() {
@@ -489,8 +510,6 @@ private int loadTexture(Bitmap bitmap) {
489510
GLUtils.texImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bitmap, GL_UNSIGNED_BYTE, 0);
490511
checkGlError();
491512

492-
bitmap.recycle();
493-
494513
return texture;
495514
}
496515

0 commit comments

Comments
 (0)