Stops ImageWallpaper Thread when phone is idle

Also it should help with the issue where the ImageWallpaper was
consuming more memory.

Bug: 183401309, 183236575
Test: visual
Change-Id: I70072cd3d358035d627e7dd9798afa6d1af26d3e
This commit is contained in:
Michel Comin Escude
2021-03-23 05:46:28 +00:00
parent 1d7bd5166c
commit c5e0476b46

View File

@@ -55,7 +55,7 @@ public class ImageWallpaper extends WallpaperService {
private static final String TAG = ImageWallpaper.class.getSimpleName();
// We delayed destroy render context that subsequent render requests have chance to cancel it.
// This is to avoid destroying then recreating render context in a very short time.
private static final int DELAY_FINISH_RENDERING = 3000;
private static final int DELAY_FINISH_RENDERING = 1000;
private static final @android.annotation.NonNull RectF LOCAL_COLOR_BOUNDS =
new RectF(0, 0, 1, 1);
private static final boolean DEBUG = false;
@@ -107,12 +107,14 @@ public class ImageWallpaper extends WallpaperService {
private ImageWallpaperRenderer mRenderer;
private EglHelper mEglHelper;
private final Runnable mFinishRenderingTask = this::finishRendering;
private final Runnable mInitChoreographerTask = this::initChoreographerInternal;
private int mWidth = 1;
private int mHeight = 1;
private int mImgWidth = 1;
private int mImgHeight = 1;
private volatile float mDozeAmount;
private volatile boolean mNewDozeValue = false;
private volatile boolean mShouldScheduleFrame = false;
GLEngine() {
}
@@ -137,10 +139,7 @@ public class ImageWallpaper extends WallpaperService {
mWidth = window.width();
mMiniBitmap = null;
if (mWorker != null && mWorker.getThreadHandler() != null) {
mWorker.getThreadHandler().post(() -> {
updateMiniBitmap();
Choreographer.getInstance().postFrameCallback(GLEngine.this);
});
mWorker.getThreadHandler().post(this::updateMiniBitmap);
}
mDozeAmount = mStatusBarStateController.getDozeAmount();
@@ -222,15 +221,16 @@ public class ImageWallpaper extends WallpaperService {
@Override
public void onDestroy() {
mMiniBitmap = null;
mStatusBarStateController.removeCallback(this);
mWorker.getThreadHandler().post(() -> {
Choreographer.getInstance().removeFrameCallback(this);
finishChoreographerInternal();
mRenderer.finish();
mRenderer = null;
mEglHelper.finish();
mEglHelper = null;
});
mStatusBarStateController.removeCallback(this);
}
@Override
@@ -345,6 +345,8 @@ public class ImageWallpaper extends WallpaperService {
@Override
public void onDozeAmountChanged(float linear, float eased) {
initChoreographer();
mDozeAmount = linear;
mNewDozeValue = true;
}
@@ -355,8 +357,10 @@ public class ImageWallpaper extends WallpaperService {
postRender();
}
/**
* Important: this method should only be invoked from the ImageWallpaper (worker) Thread.
*/
public void preRender() {
// This method should only be invoked from worker thread.
Trace.beginSection("ImageWallpaper#preRender");
preRenderInternal();
Trace.endSection();
@@ -391,8 +395,10 @@ public class ImageWallpaper extends WallpaperService {
}
}
/**
* Important: this method should only be invoked from the ImageWallpaper (worker) Thread.
*/
public void requestRender() {
// This method should only be invoked from worker thread.
Trace.beginSection("ImageWallpaper#requestRender");
requestRenderInternal();
Trace.endSection();
@@ -416,8 +422,10 @@ public class ImageWallpaper extends WallpaperService {
}
}
/**
* Important: this method should only be invoked from the ImageWallpaper (worker) Thread.
*/
public void postRender() {
// This method should only be invoked from worker thread.
Trace.beginSection("ImageWallpaper#postRender");
scheduleFinishRendering();
Trace.endSection();
@@ -436,6 +444,7 @@ public class ImageWallpaper extends WallpaperService {
private void finishRendering() {
Trace.beginSection("ImageWallpaper#finishRendering");
finishChoreographerInternal();
if (mEglHelper != null) {
mEglHelper.destroyEglSurface();
mEglHelper.destroyEglContext();
@@ -443,6 +452,35 @@ public class ImageWallpaper extends WallpaperService {
Trace.endSection();
}
private void initChoreographer() {
if (!mWorker.getThreadHandler().hasCallbacks(mInitChoreographerTask)
&& !mShouldScheduleFrame) {
mWorker.getThreadHandler().post(mInitChoreographerTask);
}
}
/**
* Subscribes the engine to listen to Choreographer frame events.
* Important: this method should only be invoked from the ImageWallpaper (worker) Thread.
*/
private void initChoreographerInternal() {
if (!mShouldScheduleFrame) {
// Prepare EGL Context and Surface
preRender();
mShouldScheduleFrame = true;
Choreographer.getInstance().postFrameCallback(GLEngine.this);
}
}
/**
* Unsubscribe the engine from listening to Choreographer frame events.
* Important: this method should only be invoked from the ImageWallpaper (worker) Thread.
*/
private void finishChoreographerInternal() {
mShouldScheduleFrame = false;
Choreographer.getInstance().removeFrameCallback(GLEngine.this);
}
private boolean needSupportWideColorGamut() {
return mRenderer.isWcgContent();
}
@@ -469,7 +507,10 @@ public class ImageWallpaper extends WallpaperService {
drawFrame();
mNewDozeValue = false;
}
Choreographer.getInstance().postFrameCallback(this);
if (mShouldScheduleFrame) {
Choreographer.getInstance().postFrameCallback(this);
}
}
}
}