Merge "Stops ImageWallpaper Thread when phone is idle" into sc-dev

This commit is contained in:
Michel Comin Escude
2021-03-25 17:05:52 +00:00
committed by Android (Google) Code Review

View File

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