Don't wait for brightness changes

On some devices, changing the brightness is quite slow, because the
change needs to wait for the vsync signal. However, when we are also
running a brightness change animation, as soon as the last change has
been sent there is a new one pending, leading to that we never reach
"clean" state in the PowerManagerService and thus we don't notify
clients about the state change, which can delay the animation for waking
up while dozing by as much as a second.

To fix this, if we only issue a brightness change, we don't block on
that because it's not relevant for us.

Bug: 22879546
Change-Id: Idad7e90c436bfbc38581fe8ce24d606e432b7952
diff --git a/services/core/java/com/android/server/display/DisplayPowerState.java b/services/core/java/com/android/server/display/DisplayPowerState.java
index f53ccc9..2eabd32 100644
--- a/services/core/java/com/android/server/display/DisplayPowerState.java
+++ b/services/core/java/com/android/server/display/DisplayPowerState.java
@@ -341,7 +341,8 @@
         private int mPendingBacklight = INITIAL_BACKLIGHT;
         private int mActualState = INITIAL_SCREEN_STATE;
         private int mActualBacklight = INITIAL_BACKLIGHT;
-        private boolean mChangeInProgress;
+        private boolean mStateChangeInProgress;
+        private boolean mBacklightChangeInProgress;
 
         public PhotonicModulator() {
             super("PhotonicModulator");
@@ -349,7 +350,9 @@
 
         public boolean setState(int state, int backlight) {
             synchronized (mLock) {
-                if (state != mPendingState || backlight != mPendingBacklight) {
+                boolean stateChanged = state != mPendingState;
+                boolean backlightChanged = backlight != mPendingBacklight;
+                if (stateChanged || backlightChanged) {
                     if (DEBUG) {
                         Slog.d(TAG, "Requesting new screen state: state="
                                 + Display.stateToString(state) + ", backlight=" + backlight);
@@ -358,12 +361,15 @@
                     mPendingState = state;
                     mPendingBacklight = backlight;
 
-                    if (!mChangeInProgress) {
-                        mChangeInProgress = true;
+                    boolean changeInProgress = mStateChangeInProgress || mBacklightChangeInProgress;
+                    mStateChangeInProgress = stateChanged;
+                    mBacklightChangeInProgress = backlightChanged;
+
+                    if (!changeInProgress) {
                         mLock.notifyAll();
                     }
                 }
-                return !mChangeInProgress;
+                return !mStateChangeInProgress;
             }
         }
 
@@ -375,7 +381,8 @@
                 pw.println("  mPendingBacklight=" + mPendingBacklight);
                 pw.println("  mActualState=" + Display.stateToString(mActualState));
                 pw.println("  mActualBacklight=" + mActualBacklight);
-                pw.println("  mChangeInProgress=" + mChangeInProgress);
+                pw.println("  mStateChangeInProgress=" + mStateChangeInProgress);
+                pw.println("  mBacklightChangeInProgress=" + mBacklightChangeInProgress);
             }
         }
 
@@ -392,10 +399,15 @@
                     stateChanged = (state != mActualState);
                     backlight = mPendingBacklight;
                     backlightChanged = (backlight != mActualBacklight);
-                    if (!stateChanged && !backlightChanged) {
-                        // All changed applied, notify outer class and wait for more.
-                        mChangeInProgress = false;
+                    if (!stateChanged) {
+                        // State changed applied, notify outer class.
                         postScreenUpdateThreadSafe();
+                        mStateChangeInProgress = false;
+                    }
+                    if (!backlightChanged) {
+                        mBacklightChangeInProgress = false;
+                    }
+                    if (!stateChanged && !backlightChanged) {
                         try {
                             mLock.wait();
                         } catch (InterruptedException ex) { }