From 48ebbd77997566ffec5e296b4d9398c849472e48 Mon Sep 17 00:00:00 2001 From: Ahan Wu Date: Tue, 12 Mar 2019 20:59:13 +0800 Subject: [PATCH] Release the bitmap reference and textures when user changes the wallpaper. We don't need the bitmap after we uploads it to texture so we nullify the reference to get it can be gced. We also need to release the graphic memory when user changes the wallpaper or the graphic memory will increase dramatically. Bug: 128897294 Test: Change wallpaper, observes android profiler Change-Id: I207d67741e6a7643a13ba97fccdab355dd09e30c --- .../com/android/systemui/ImageWallpaper.java | 29 +++++++++---------- .../glwallpaper/ImageGLWallpaper.java | 18 ++++-------- .../glwallpaper/ImageWallpaperRenderer.java | 29 ++++++++++--------- 3 files changed, 35 insertions(+), 41 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/ImageWallpaper.java b/packages/SystemUI/src/com/android/systemui/ImageWallpaper.java index 0113d0560764f..ed2a6b59d1534 100644 --- a/packages/SystemUI/src/com/android/systemui/ImageWallpaper.java +++ b/packages/SystemUI/src/com/android/systemui/ImageWallpaper.java @@ -19,9 +19,7 @@ package com.android.systemui; import android.content.Context; import android.graphics.Rect; import android.opengl.GLSurfaceView; -import android.os.Build; import android.service.wallpaper.WallpaperService; -import android.util.Log; import android.view.SurfaceHolder; import com.android.systemui.glwallpaper.ImageWallpaperRenderer; @@ -33,16 +31,8 @@ import com.android.systemui.glwallpaper.ImageWallpaperRenderer; public class ImageWallpaper extends WallpaperService { private static final String TAG = ImageWallpaper.class.getSimpleName(); - @Override - public void onCreate() { - super.onCreate(); - } - @Override public Engine onCreateEngine() { - if (Build.IS_DEBUGGABLE) { - Log.v(TAG, "We are using GLEngine"); - } return new GLEngine(this); } @@ -72,8 +62,15 @@ public class ImageWallpaper extends WallpaperService { } } + @Override + public void onDestroy() { + if (mWallpaperSurfaceView != null) { + mWallpaperSurfaceView.onPause(); + } + } + private class GLWallpaperSurfaceView extends GLSurfaceView implements ImageGLView { - private WallpaperStatusListener mWallpaperChangedListener; + private WallpaperStatusListener mWallpaperStatusListener; GLWallpaperSurfaceView(Context context) { super(context); @@ -88,18 +85,18 @@ public class ImageWallpaper extends WallpaperService { @Override public void setRenderer(Renderer renderer) { super.setRenderer(renderer); - mWallpaperChangedListener = (WallpaperStatusListener) renderer; + mWallpaperStatusListener = (WallpaperStatusListener) renderer; } private void notifyAmbientModeChanged(boolean inAmbient, long duration) { - if (mWallpaperChangedListener != null) { - mWallpaperChangedListener.onAmbientModeChanged(inAmbient, duration); + if (mWallpaperStatusListener != null) { + mWallpaperStatusListener.onAmbientModeChanged(inAmbient, duration); } } private void notifyOffsetsChanged(float xOffset, float yOffset) { - if (mWallpaperChangedListener != null) { - mWallpaperChangedListener.onOffsetsChanged( + if (mWallpaperStatusListener != null) { + mWallpaperStatusListener.onOffsetsChanged( xOffset, yOffset, getHolder().getSurfaceFrame()); } } diff --git a/packages/SystemUI/src/com/android/systemui/glwallpaper/ImageGLWallpaper.java b/packages/SystemUI/src/com/android/systemui/glwallpaper/ImageGLWallpaper.java index a313336e3d712..21406e57ad7b7 100644 --- a/packages/SystemUI/src/com/android/systemui/glwallpaper/ImageGLWallpaper.java +++ b/packages/SystemUI/src/com/android/systemui/glwallpaper/ImageGLWallpaper.java @@ -110,9 +110,10 @@ class ImageGLWallpaper { mTextureBuffer.position(0); } - void setup() { + void setup(Bitmap bitmap) { setupAttributes(); setupUniforms(); + setupTexture(bitmap); } private void setupAttributes() { @@ -159,7 +160,7 @@ class ImageGLWallpaper { glDrawArrays(GL_TRIANGLES, 0, VERTICES.length / 2); } - void setupTexture(Bitmap bitmap) { + private void setupTexture(Bitmap bitmap) { final int[] tids = new int[1]; if (bitmap == null) { @@ -174,7 +175,7 @@ class ImageGLWallpaper { return; } - // Bind a named texture to a texturing target. + // Bind a named texture to a target. glBindTexture(GL_TEXTURE_2D, tids[0]); // Load the bitmap data and copy it over into the texture object that is currently bound. GLUtils.texImage2D(GL_TEXTURE_2D, 0, bitmap, 0); @@ -195,15 +196,8 @@ class ImageGLWallpaper { glUniform1i(mUniTexture, 0); } - void adjustTextureCoordinates(Bitmap bitmap, int surfaceWidth, int surfaceHeight, - float xOffset, float yOffset) { - if (bitmap == null) { - Log.d(TAG, "adjustTextureCoordinates: invalid bitmap"); - return; - } - - int bitmapWidth = bitmap.getWidth(); - int bitmapHeight = bitmap.getHeight(); + void adjustTextureCoordinates(int bitmapWidth, int bitmapHeight, + int surfaceWidth, int surfaceHeight, float xOffset, float yOffset) { float ratioW = 1f; float ratioH = 1f; float rX = 0f; diff --git a/packages/SystemUI/src/com/android/systemui/glwallpaper/ImageWallpaperRenderer.java b/packages/SystemUI/src/com/android/systemui/glwallpaper/ImageWallpaperRenderer.java index 9df6ba5b1193d..464cbe33c5f0e 100644 --- a/packages/SystemUI/src/com/android/systemui/glwallpaper/ImageWallpaperRenderer.java +++ b/packages/SystemUI/src/com/android/systemui/glwallpaper/ImageWallpaperRenderer.java @@ -58,6 +58,10 @@ public class ImageWallpaperRenderer implements GLSurfaceView.Renderer, private int mWidth = 0; private int mHeight = 0; + private Bitmap mBitmap; + private int mBitmapWidth = 0; + private int mBitmapHeight = 0; + public ImageWallpaperRenderer(Context context, ImageGLView glView) { mWallpaperManager = context.getSystemService(WallpaperManager.class); if (mWallpaperManager == null) { @@ -71,8 +75,12 @@ public class ImageWallpaperRenderer implements GLSurfaceView.Renderer, mGLView = glView; if (mWallpaperManager != null) { + mBitmap = mWallpaperManager.getBitmap(); + mBitmapWidth = mBitmap.getWidth(); + mBitmapHeight = mBitmap.getHeight(); // Compute per85 as transition threshold, this is an async work. - mImageProcessHelper.startComputingPercentile85(mWallpaperManager.getBitmap()); + mImageProcessHelper.startComputingPercentile85(mBitmap); + mWallpaperManager.forgetLoadedWallpaper(); } } @@ -81,8 +89,8 @@ public class ImageWallpaperRenderer implements GLSurfaceView.Renderer, glClearColor(0f, 0f, 0f, 1.0f); mProgram.useGLProgram( R.raw.image_wallpaper_vertex_shader, R.raw.image_wallpaper_fragment_shader); - mWallpaper.setup(); - mWallpaper.setupTexture(mWallpaperManager.getBitmap()); + mWallpaper.setup(mBitmap); + mBitmap = null; } @Override @@ -94,8 +102,8 @@ public class ImageWallpaperRenderer implements GLSurfaceView.Renderer, } mWidth = width; mHeight = height; - mWallpaper.adjustTextureCoordinates(mWallpaperManager.getBitmap(), - width, height, mXOffset, mYOffset); + mWallpaper.adjustTextureCoordinates( + mBitmapWidth, mBitmapHeight, width, height, mXOffset, mYOffset); } @Override @@ -132,13 +140,7 @@ public class ImageWallpaperRenderer implements GLSurfaceView.Renderer, @Override public void onOffsetsChanged(float xOffset, float yOffset, Rect frame) { - if (frame == null || mWallpaperManager == null - || (xOffset == mXOffset && yOffset == mYOffset)) { - return; - } - - Bitmap bitmap = mWallpaperManager.getBitmap(); - if (bitmap == null) { + if (frame == null || (xOffset == mXOffset && yOffset == mYOffset)) { return; } @@ -151,7 +153,8 @@ public class ImageWallpaperRenderer implements GLSurfaceView.Renderer, Log.d(TAG, "onOffsetsChanged: width=" + width + ", height=" + height + ", xOffset=" + mXOffset + ", yOffset=" + mYOffset); } - mWallpaper.adjustTextureCoordinates(bitmap, width, height, mXOffset, mYOffset); + mWallpaper.adjustTextureCoordinates( + mBitmapWidth, mBitmapHeight, width, height, mXOffset, mYOffset); requestRender(); }