Craig Mautner | c2c0a61 | 2014-02-20 20:25:41 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 16 | |
| 17 | package com.android.server.wm; |
| 18 | |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 19 | import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SURFACE_TRACE; |
| 20 | import static com.android.server.wm.WindowManagerDebugConfig.SHOW_SURFACE_ALLOC; |
| 21 | import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS; |
| 22 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME; |
| 23 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM; |
| 24 | |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 25 | import android.graphics.PixelFormat; |
Craig Mautner | 05d2903 | 2013-05-03 13:40:13 -0700 | [diff] [blame] | 26 | import android.graphics.Rect; |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 27 | import android.os.SystemClock; |
| 28 | import android.util.Slog; |
| 29 | import android.view.DisplayInfo; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 30 | import android.view.SurfaceControl; |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 31 | |
| 32 | import java.io.PrintWriter; |
| 33 | |
| 34 | public class DimLayer { |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 35 | private static final String TAG = TAG_WITH_CLASS_NAME ? "DimLayer" : TAG_WM; |
Craig Mautner | c34bc11 | 2013-01-08 15:02:13 -0800 | [diff] [blame] | 36 | private static final boolean DEBUG = false; |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 37 | |
Filip Gruszczynski | 57b6cce | 2015-10-06 09:50:51 -0700 | [diff] [blame] | 38 | public static final float RESIZING_HINT_ALPHA = 0.5f; |
| 39 | |
| 40 | public static final int RESIZING_HINT_DURATION_MS = 0; |
| 41 | |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 42 | /** Actual surface that dims */ |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 43 | SurfaceControl mDimSurface; |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 44 | |
| 45 | /** Last value passed to mDimSurface.setAlpha() */ |
| 46 | float mAlpha = 0; |
| 47 | |
| 48 | /** Last value passed to mDimSurface.setLayer() */ |
| 49 | int mLayer = -1; |
| 50 | |
Craig Mautner | 05d2903 | 2013-05-03 13:40:13 -0700 | [diff] [blame] | 51 | /** Next values to pass to mDimSurface.setPosition() and mDimSurface.setSize() */ |
| 52 | Rect mBounds = new Rect(); |
| 53 | |
| 54 | /** Last values passed to mDimSurface.setPosition() and mDimSurface.setSize() */ |
| 55 | Rect mLastBounds = new Rect(); |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 56 | |
| 57 | /** True after mDimSurface.show() has been called, false after mDimSurface.hide(). */ |
| 58 | private boolean mShowing = false; |
| 59 | |
| 60 | /** Value of mAlpha when beginning transition to mTargetAlpha */ |
| 61 | float mStartAlpha = 0; |
| 62 | |
| 63 | /** Final value of mAlpha following transition */ |
| 64 | float mTargetAlpha = 0; |
| 65 | |
| 66 | /** Time in units of SystemClock.uptimeMillis() at which the current transition started */ |
| 67 | long mStartTime; |
| 68 | |
| 69 | /** Time in milliseconds to take to transition from mStartAlpha to mTargetAlpha */ |
| 70 | long mDuration; |
| 71 | |
Wale Ogunwale | e4a0c57 | 2015-06-30 08:40:31 -0700 | [diff] [blame] | 72 | /** Interface implemented by users of the dim layer */ |
| 73 | interface DimLayerUser { |
| 74 | /** Returns true if the user of the dim layer is fullscreen. */ |
| 75 | boolean isFullscreen(); |
| 76 | /** Returns the display info. of the dim layer user. */ |
| 77 | DisplayInfo getDisplayInfo(); |
Filip Gruszczynski | 0689ae9 | 2015-10-01 12:30:31 -0700 | [diff] [blame] | 78 | /** Gets the bounds of the dim layer user. */ |
Chong Zhang | 4c9ba52a | 2015-11-10 18:36:33 -0800 | [diff] [blame] | 79 | void getDimBounds(Rect outBounds); |
Filip Gruszczynski | 0689ae9 | 2015-10-01 12:30:31 -0700 | [diff] [blame] | 80 | String toShortString(); |
Wale Ogunwale | e4a0c57 | 2015-06-30 08:40:31 -0700 | [diff] [blame] | 81 | } |
| 82 | /** The user of this dim layer. */ |
| 83 | final DimLayerUser mUser; |
Craig Mautner | c7b8a10 | 2013-10-02 16:49:52 -0700 | [diff] [blame] | 84 | |
Wale Ogunwale | e4a0c57 | 2015-06-30 08:40:31 -0700 | [diff] [blame] | 85 | DimLayer(WindowManagerService service, DimLayerUser user, int displayId) { |
| 86 | mUser = user; |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 87 | if (DEBUG) Slog.v(TAG, "Ctor: displayId=" + displayId); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 88 | SurfaceControl.openTransaction(); |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 89 | try { |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 90 | if (DEBUG_SURFACE_TRACE) { |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 91 | mDimSurface = new WindowSurfaceController.SurfaceTrace(service.mFxSession, |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 92 | "DimSurface", |
| 93 | 16, 16, PixelFormat.OPAQUE, |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 94 | SurfaceControl.FX_SURFACE_DIM | SurfaceControl.HIDDEN); |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 95 | } else { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 96 | mDimSurface = new SurfaceControl(service.mFxSession, TAG, |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 97 | 16, 16, PixelFormat.OPAQUE, |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 98 | SurfaceControl.FX_SURFACE_DIM | SurfaceControl.HIDDEN); |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 99 | } |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 100 | if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) Slog.i(TAG, |
| 101 | " DIM " + mDimSurface + ": CREATE"); |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 102 | mDimSurface.setLayerStack(displayId); |
| 103 | } catch (Exception e) { |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 104 | Slog.e(TAG_WM, "Exception creating Dim surface", e); |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 105 | } finally { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 106 | SurfaceControl.closeTransaction(); |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | /** Return true if dim layer is showing */ |
| 111 | boolean isDimming() { |
| 112 | return mTargetAlpha != 0; |
| 113 | } |
| 114 | |
| 115 | /** Return true if in a transition period */ |
| 116 | boolean isAnimating() { |
| 117 | return mTargetAlpha != mAlpha; |
| 118 | } |
| 119 | |
| 120 | float getTargetAlpha() { |
| 121 | return mTargetAlpha; |
| 122 | } |
| 123 | |
Craig Mautner | 13131e7 | 2013-01-11 11:03:33 -0800 | [diff] [blame] | 124 | void setLayer(int layer) { |
| 125 | if (mLayer != layer) { |
| 126 | mLayer = layer; |
| 127 | mDimSurface.setLayer(layer); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | int getLayer() { |
| 132 | return mLayer; |
| 133 | } |
| 134 | |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 135 | private void setAlpha(float alpha) { |
| 136 | if (mAlpha != alpha) { |
| 137 | if (DEBUG) Slog.v(TAG, "setAlpha alpha=" + alpha); |
| 138 | try { |
| 139 | mDimSurface.setAlpha(alpha); |
| 140 | if (alpha == 0 && mShowing) { |
| 141 | if (DEBUG) Slog.v(TAG, "setAlpha hiding"); |
| 142 | mDimSurface.hide(); |
| 143 | mShowing = false; |
| 144 | } else if (alpha > 0 && !mShowing) { |
| 145 | if (DEBUG) Slog.v(TAG, "setAlpha showing"); |
| 146 | mDimSurface.show(); |
| 147 | mShowing = true; |
| 148 | } |
| 149 | } catch (RuntimeException e) { |
| 150 | Slog.w(TAG, "Failure setting alpha immediately", e); |
| 151 | } |
| 152 | mAlpha = alpha; |
| 153 | } |
| 154 | } |
| 155 | |
Craig Mautner | 902945d | 2014-02-20 14:31:32 -0800 | [diff] [blame] | 156 | /** |
Dohyun Lee | 6ce3a37 | 2015-03-23 09:16:34 +0900 | [diff] [blame] | 157 | * NOTE: Must be called with Surface transaction open. |
Craig Mautner | 902945d | 2014-02-20 14:31:32 -0800 | [diff] [blame] | 158 | */ |
Dohyun Lee | 6ce3a37 | 2015-03-23 09:16:34 +0900 | [diff] [blame] | 159 | private void adjustBounds() { |
Craig Mautner | 902945d | 2014-02-20 14:31:32 -0800 | [diff] [blame] | 160 | final int dw, dh; |
| 161 | final float xPos, yPos; |
Wale Ogunwale | e4a0c57 | 2015-06-30 08:40:31 -0700 | [diff] [blame] | 162 | if (!mUser.isFullscreen()) { |
Craig Mautner | 902945d | 2014-02-20 14:31:32 -0800 | [diff] [blame] | 163 | dw = mBounds.width(); |
| 164 | dh = mBounds.height(); |
| 165 | xPos = mBounds.left; |
| 166 | yPos = mBounds.top; |
| 167 | } else { |
| 168 | // Set surface size to screen size. |
Wale Ogunwale | e4a0c57 | 2015-06-30 08:40:31 -0700 | [diff] [blame] | 169 | final DisplayInfo info = mUser.getDisplayInfo(); |
Craig Mautner | 902945d | 2014-02-20 14:31:32 -0800 | [diff] [blame] | 170 | // Multiply by 1.5 so that rotating a frozen surface that includes this does not expose |
| 171 | // a corner. |
| 172 | dw = (int) (info.logicalWidth * 1.5); |
| 173 | dh = (int) (info.logicalHeight * 1.5); |
| 174 | // back off position so 1/4 of Surface is before and 1/4 is after. |
| 175 | xPos = -1 * dw / 6; |
| 176 | yPos = -1 * dh / 6; |
| 177 | } |
| 178 | |
Dohyun Lee | 6ce3a37 | 2015-03-23 09:16:34 +0900 | [diff] [blame] | 179 | mDimSurface.setPosition(xPos, yPos); |
| 180 | mDimSurface.setSize(dw, dh); |
| 181 | |
Craig Mautner | 902945d | 2014-02-20 14:31:32 -0800 | [diff] [blame] | 182 | mLastBounds.set(mBounds); |
Craig Mautner | 902945d | 2014-02-20 14:31:32 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Wale Ogunwale | 0ade61b | 2015-03-24 10:40:32 -0700 | [diff] [blame] | 185 | /** @param bounds The new bounds to set */ |
| 186 | void setBounds(Rect bounds) { |
Craig Mautner | 05d2903 | 2013-05-03 13:40:13 -0700 | [diff] [blame] | 187 | mBounds.set(bounds); |
Craig Mautner | b660b9d | 2014-02-13 10:59:16 -0800 | [diff] [blame] | 188 | if (isDimming() && !mLastBounds.equals(bounds)) { |
Dohyun Lee | 6ce3a37 | 2015-03-23 09:16:34 +0900 | [diff] [blame] | 189 | try { |
Wale Ogunwale | 0ade61b | 2015-03-24 10:40:32 -0700 | [diff] [blame] | 190 | SurfaceControl.openTransaction(); |
Dohyun Lee | 6ce3a37 | 2015-03-23 09:16:34 +0900 | [diff] [blame] | 191 | adjustBounds(); |
| 192 | } catch (RuntimeException e) { |
| 193 | Slog.w(TAG, "Failure setting size", e); |
| 194 | } finally { |
Wale Ogunwale | 0ade61b | 2015-03-24 10:40:32 -0700 | [diff] [blame] | 195 | SurfaceControl.closeTransaction(); |
Dohyun Lee | 6ce3a37 | 2015-03-23 09:16:34 +0900 | [diff] [blame] | 196 | } |
Craig Mautner | b660b9d | 2014-02-13 10:59:16 -0800 | [diff] [blame] | 197 | } |
Craig Mautner | 05d2903 | 2013-05-03 13:40:13 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 200 | /** |
| 201 | * @param duration The time to test. |
| 202 | * @return True if the duration would lead to an earlier end to the current animation. |
| 203 | */ |
| 204 | private boolean durationEndsEarlier(long duration) { |
| 205 | return SystemClock.uptimeMillis() + duration < mStartTime + mDuration; |
| 206 | } |
| 207 | |
| 208 | /** Jump to the end of the animation. |
| 209 | * NOTE: Must be called with Surface transaction open. */ |
| 210 | void show() { |
| 211 | if (isAnimating()) { |
| 212 | if (DEBUG) Slog.v(TAG, "show: immediate"); |
| 213 | show(mLayer, mTargetAlpha, 0); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Begin an animation to a new dim value. |
| 219 | * NOTE: Must be called with Surface transaction open. |
| 220 | * |
| 221 | * @param layer The layer to set the surface to. |
| 222 | * @param alpha The dim value to end at. |
| 223 | * @param duration How long to take to get there in milliseconds. |
| 224 | */ |
| 225 | void show(int layer, float alpha, long duration) { |
| 226 | if (DEBUG) Slog.v(TAG, "show: layer=" + layer + " alpha=" + alpha |
| 227 | + " duration=" + duration); |
| 228 | if (mDimSurface == null) { |
| 229 | Slog.e(TAG, "show: no Surface"); |
| 230 | // Make sure isAnimating() returns false. |
| 231 | mTargetAlpha = mAlpha = 0; |
| 232 | return; |
| 233 | } |
| 234 | |
Dohyun Lee | 6ce3a37 | 2015-03-23 09:16:34 +0900 | [diff] [blame] | 235 | if (!mLastBounds.equals(mBounds)) { |
| 236 | adjustBounds(); |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 237 | } |
Dohyun Lee | 6ce3a37 | 2015-03-23 09:16:34 +0900 | [diff] [blame] | 238 | setLayer(layer); |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 239 | |
| 240 | long curTime = SystemClock.uptimeMillis(); |
| 241 | final boolean animating = isAnimating(); |
| 242 | if ((animating && (mTargetAlpha != alpha || durationEndsEarlier(duration))) |
| 243 | || (!animating && mAlpha != alpha)) { |
| 244 | if (duration <= 0) { |
| 245 | // No animation required, just set values. |
| 246 | setAlpha(alpha); |
| 247 | } else { |
| 248 | // Start or continue animation with new parameters. |
| 249 | mStartAlpha = mAlpha; |
| 250 | mStartTime = curTime; |
| 251 | mDuration = duration; |
| 252 | } |
| 253 | } |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 254 | mTargetAlpha = alpha; |
Filip Gruszczynski | 0689ae9 | 2015-10-01 12:30:31 -0700 | [diff] [blame] | 255 | if (DEBUG) Slog.v(TAG, "show: mStartAlpha=" + mStartAlpha + " mStartTime=" + mStartTime |
| 256 | + " mTargetAlpha=" + mTargetAlpha); |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /** Immediate hide. |
| 260 | * NOTE: Must be called with Surface transaction open. */ |
| 261 | void hide() { |
| 262 | if (mShowing) { |
| 263 | if (DEBUG) Slog.v(TAG, "hide: immediate"); |
| 264 | hide(0); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Gradually fade to transparent. |
| 270 | * NOTE: Must be called with Surface transaction open. |
| 271 | * |
| 272 | * @param duration Time to fade in milliseconds. |
| 273 | */ |
| 274 | void hide(long duration) { |
| 275 | if (mShowing && (mTargetAlpha != 0 || durationEndsEarlier(duration))) { |
| 276 | if (DEBUG) Slog.v(TAG, "hide: duration=" + duration); |
| 277 | show(mLayer, 0, duration); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Advance the dimming per the last #show(int, float, long) call. |
| 283 | * NOTE: Must be called with Surface transaction open. |
| 284 | * |
| 285 | * @return True if animation is still required after this step. |
| 286 | */ |
| 287 | boolean stepAnimation() { |
| 288 | if (mDimSurface == null) { |
| 289 | Slog.e(TAG, "stepAnimation: null Surface"); |
| 290 | // Ensure that isAnimating() returns false; |
| 291 | mTargetAlpha = mAlpha = 0; |
| 292 | return false; |
| 293 | } |
| 294 | |
| 295 | if (isAnimating()) { |
| 296 | final long curTime = SystemClock.uptimeMillis(); |
| 297 | final float alphaDelta = mTargetAlpha - mStartAlpha; |
| 298 | float alpha = mStartAlpha + alphaDelta * (curTime - mStartTime) / mDuration; |
| 299 | if (alphaDelta > 0 && alpha > mTargetAlpha || |
| 300 | alphaDelta < 0 && alpha < mTargetAlpha) { |
| 301 | // Don't exceed limits. |
| 302 | alpha = mTargetAlpha; |
| 303 | } |
| 304 | if (DEBUG) Slog.v(TAG, "stepAnimation: curTime=" + curTime + " alpha=" + alpha); |
| 305 | setAlpha(alpha); |
| 306 | } |
| 307 | |
| 308 | return isAnimating(); |
| 309 | } |
| 310 | |
| 311 | /** Cleanup */ |
| 312 | void destroySurface() { |
| 313 | if (DEBUG) Slog.v(TAG, "destroySurface."); |
| 314 | if (mDimSurface != null) { |
| 315 | mDimSurface.destroy(); |
| 316 | mDimSurface = null; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | public void printTo(String prefix, PrintWriter pw) { |
Craig Mautner | 967212c | 2013-04-13 21:10:58 -0700 | [diff] [blame] | 321 | pw.print(prefix); pw.print("mDimSurface="); pw.print(mDimSurface); |
| 322 | pw.print(" mLayer="); pw.print(mLayer); |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 323 | pw.print(" mAlpha="); pw.println(mAlpha); |
Craig Mautner | 05d2903 | 2013-05-03 13:40:13 -0700 | [diff] [blame] | 324 | pw.print(prefix); pw.print("mLastBounds="); pw.print(mLastBounds.toShortString()); |
| 325 | pw.print(" mBounds="); pw.println(mBounds.toShortString()); |
Craig Mautner | 967212c | 2013-04-13 21:10:58 -0700 | [diff] [blame] | 326 | pw.print(prefix); pw.print("Last animation: "); |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 327 | pw.print(" mDuration="); pw.print(mDuration); |
Craig Mautner | 967212c | 2013-04-13 21:10:58 -0700 | [diff] [blame] | 328 | pw.print(" mStartTime="); pw.print(mStartTime); |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 329 | pw.print(" curTime="); pw.println(SystemClock.uptimeMillis()); |
Craig Mautner | 967212c | 2013-04-13 21:10:58 -0700 | [diff] [blame] | 330 | pw.print(prefix); pw.print(" mStartAlpha="); pw.print(mStartAlpha); |
| 331 | pw.print(" mTargetAlpha="); pw.println(mTargetAlpha); |
Craig Mautner | 1420b93 | 2012-12-28 17:14:38 -0800 | [diff] [blame] | 332 | } |
| 333 | } |