diff options
| author | 2021-03-25 17:05:52 +0000 | |
|---|---|---|
| committer | 2021-03-25 17:05:52 +0000 | |
| commit | 8a42d0300c6302d45d0d04f448f20898ac883cc2 (patch) | |
| tree | fd8677edbb6ab6815f2944effa89170022c9f43b | |
| parent | 1dad2faa7f1c8228a2d7be9153a6ec631b1120b6 (diff) | |
| parent | c5e0476b46cba2739604078c60842bb6d0bb036b (diff) | |
Merge "Stops ImageWallpaper Thread when phone is idle" into sc-dev
| -rw-r--r-- | packages/SystemUI/src/com/android/systemui/ImageWallpaper.java | 65 |
1 files changed, 53 insertions, 12 deletions
diff --git a/packages/SystemUI/src/com/android/systemui/ImageWallpaper.java b/packages/SystemUI/src/com/android/systemui/ImageWallpaper.java index 8b68ae959ebf..721b7586240c 100644 --- a/packages/SystemUI/src/com/android/systemui/ImageWallpaper.java +++ b/packages/SystemUI/src/com/android/systemui/ImageWallpaper.java @@ -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); + } } } } |