Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | */ |
| 16 | |
| 17 | package com.android.server.wm; |
| 18 | |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 19 | import android.view.SurfaceControl; |
| 20 | import android.graphics.Rect; |
| 21 | |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 22 | import com.android.internal.annotations.VisibleForTesting; |
| 23 | |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 24 | /** |
| 25 | * Utility class for use by a WindowContainer implementation to add "DimLayer" support, that is |
| 26 | * black layers of varying opacity at various Z-levels which create the effect of a Dim. |
| 27 | */ |
| 28 | class Dimmer { |
| 29 | private static final String TAG = "WindowManager"; |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 30 | private static final int DEFAULT_DIM_ANIM_DURATION = 200; |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 31 | |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 32 | private class DimAnimatable implements SurfaceAnimator.Animatable { |
| 33 | private final SurfaceControl mDimLayer; |
| 34 | |
| 35 | private DimAnimatable(SurfaceControl dimLayer) { |
| 36 | mDimLayer = dimLayer; |
| 37 | } |
| 38 | |
| 39 | @Override |
| 40 | public SurfaceControl.Transaction getPendingTransaction() { |
| 41 | return mHost.getPendingTransaction(); |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public void commitPendingTransaction() { |
| 46 | mHost.commitPendingTransaction(); |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public void onAnimationLeashCreated(SurfaceControl.Transaction t, SurfaceControl leash) { |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public void onAnimationLeashDestroyed(SurfaceControl.Transaction t) { |
| 55 | } |
| 56 | |
| 57 | @Override |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 58 | public SurfaceControl.Builder makeAnimationLeash() { |
| 59 | return mHost.makeAnimationLeash(); |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public SurfaceControl getAnimationLeashParent() { |
| 64 | return mHost.getSurfaceControl(); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public SurfaceControl getSurfaceControl() { |
| 69 | return mDimLayer; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public SurfaceControl getParentSurfaceControl() { |
| 74 | return mHost.getSurfaceControl(); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public int getSurfaceWidth() { |
| 79 | // This will determine the size of the leash created. This should be the size of the |
| 80 | // host and not the dim layer since the dim layer may get bigger during animation. If |
| 81 | // that occurs, the leash size cannot change so we need to ensure the leash is big |
| 82 | // enough that the dim layer can grow. |
| 83 | // This works because the mHost will be a Task which has the display bounds. |
| 84 | return mHost.getSurfaceWidth(); |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public int getSurfaceHeight() { |
| 89 | // See getSurfaceWidth() above for explanation. |
| 90 | return mHost.getSurfaceHeight(); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | @VisibleForTesting |
| 95 | class DimState { |
| 96 | /** |
| 97 | * The layer where property changes should be invoked on. |
| 98 | */ |
| 99 | SurfaceControl mDimLayer; |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 100 | boolean mDimming; |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 101 | boolean isVisible; |
| 102 | SurfaceAnimator mSurfaceAnimator; |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 103 | |
| 104 | /** |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 105 | * Used for Dims not associated with a WindowContainer. See {@link Dimmer#dimAbove} for |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 106 | * details on Dim lifecycle. |
| 107 | */ |
| 108 | boolean mDontReset; |
| 109 | |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 110 | DimState(SurfaceControl dimLayer) { |
| 111 | mDimLayer = dimLayer; |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 112 | mDimming = true; |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 113 | mSurfaceAnimator = new SurfaceAnimator(new DimAnimatable(dimLayer), () -> { |
| 114 | if (!mDimming) { |
| 115 | mDimLayer.destroy(); |
| 116 | } |
Chavi Weingarten | b736e32 | 2018-02-23 00:27:54 +0000 | [diff] [blame^] | 117 | }, mHost.mService); |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 118 | } |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 119 | } |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 120 | |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 121 | /** |
| 122 | * The {@link WindowContainer} that our Dim's are bounded to. We may be dimming on behalf of the |
| 123 | * host, some controller of it, or one of the hosts children. |
| 124 | */ |
| 125 | private WindowContainer mHost; |
chaviw | b792a95 | 2018-02-02 11:55:21 -0800 | [diff] [blame] | 126 | private WindowContainer mLastRequestedDimContainer; |
chaviw | f20bd22 | 2018-02-01 16:06:52 -0800 | [diff] [blame] | 127 | @VisibleForTesting |
| 128 | DimState mDimState; |
| 129 | |
| 130 | private final SurfaceAnimatorStarter mSurfaceAnimatorStarter; |
| 131 | |
| 132 | @VisibleForTesting |
| 133 | interface SurfaceAnimatorStarter { |
| 134 | void startAnimation(SurfaceAnimator surfaceAnimator, SurfaceControl.Transaction t, |
| 135 | AnimationAdapter anim, boolean hidden); |
| 136 | } |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 137 | |
| 138 | Dimmer(WindowContainer host) { |
chaviw | f20bd22 | 2018-02-01 16:06:52 -0800 | [diff] [blame] | 139 | this(host, SurfaceAnimator::startAnimation); |
| 140 | } |
| 141 | |
| 142 | Dimmer(WindowContainer host, SurfaceAnimatorStarter surfaceAnimatorStarter) { |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 143 | mHost = host; |
chaviw | f20bd22 | 2018-02-01 16:06:52 -0800 | [diff] [blame] | 144 | mSurfaceAnimatorStarter = surfaceAnimatorStarter; |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 145 | } |
| 146 | |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 147 | private SurfaceControl makeDimLayer() { |
| 148 | return mHost.makeChildSurface(null) |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 149 | .setParent(mHost.getSurfaceControl()) |
| 150 | .setColorLayer(true) |
| 151 | .setName("Dim Layer for - " + mHost.getName()) |
| 152 | .build(); |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /** |
chaviw | b792a95 | 2018-02-02 11:55:21 -0800 | [diff] [blame] | 156 | * Retrieve the DimState, creating one if it doesn't exist. |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 157 | */ |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 158 | private DimState getDimState(WindowContainer container) { |
chaviw | b792a95 | 2018-02-02 11:55:21 -0800 | [diff] [blame] | 159 | if (mDimState == null) { |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 160 | final SurfaceControl ctl = makeDimLayer(); |
chaviw | b792a95 | 2018-02-02 11:55:21 -0800 | [diff] [blame] | 161 | mDimState = new DimState(ctl); |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 162 | /** |
| 163 | * See documentation on {@link #dimAbove} to understand lifecycle management of Dim's |
| 164 | * via state resetting for Dim's with containers. |
| 165 | */ |
| 166 | if (container == null) { |
chaviw | b792a95 | 2018-02-02 11:55:21 -0800 | [diff] [blame] | 167 | mDimState.mDontReset = true; |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 168 | } |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 169 | } |
chaviw | b792a95 | 2018-02-02 11:55:21 -0800 | [diff] [blame] | 170 | |
| 171 | mLastRequestedDimContainer = container; |
| 172 | return mDimState; |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | private void dim(SurfaceControl.Transaction t, WindowContainer container, int relativeLayer, |
| 176 | float alpha) { |
| 177 | final DimState d = getDimState(container); |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 178 | if (container != null) { |
chaviw | b792a95 | 2018-02-02 11:55:21 -0800 | [diff] [blame] | 179 | // The dim method is called from WindowState.prepareSurfaces(), which is always called |
| 180 | // in the correct Z from lowest Z to highest. This ensures that the dim layer is always |
| 181 | // relative to the highest Z layer with a dim. |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 182 | t.setRelativeLayer(d.mDimLayer, container.getSurfaceControl(), relativeLayer); |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 183 | } else { |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 184 | t.setLayer(d.mDimLayer, Integer.MAX_VALUE); |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 185 | } |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 186 | t.setAlpha(d.mDimLayer, alpha); |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 187 | |
| 188 | d.mDimming = true; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Finish a dim started by dimAbove in the case there was no call to dimAbove. |
| 193 | * |
| 194 | * @param t A Transaction in which to finish the dim. |
| 195 | */ |
| 196 | void stopDim(SurfaceControl.Transaction t) { |
| 197 | DimState d = getDimState(null); |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 198 | t.hide(d.mDimLayer); |
| 199 | d.isVisible = false; |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 200 | d.mDontReset = false; |
| 201 | } |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 202 | |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 203 | /** |
| 204 | * Place a Dim above the entire host container. The caller is responsible for calling stopDim to |
| 205 | * remove this effect. If the Dim can be assosciated with a particular child of the host |
| 206 | * consider using the other variant of dimAbove which ties the Dim lifetime to the child |
| 207 | * lifetime more explicitly. |
| 208 | * |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 209 | * @param t A transaction in which to apply the Dim. |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 210 | * @param alpha The alpha at which to Dim. |
| 211 | */ |
| 212 | void dimAbove(SurfaceControl.Transaction t, float alpha) { |
| 213 | dim(t, null, 1, alpha); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Place a dim above the given container, which should be a child of the host container. |
| 218 | * for each call to {@link WindowContainer#prepareSurfaces} the Dim state will be reset |
| 219 | * and the child should call dimAbove again to request the Dim to continue. |
| 220 | * |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 221 | * @param t A transaction in which to apply the Dim. |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 222 | * @param container The container which to dim above. Should be a child of our host. |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 223 | * @param alpha The alpha at which to Dim. |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 224 | */ |
| 225 | void dimAbove(SurfaceControl.Transaction t, WindowContainer container, float alpha) { |
| 226 | dim(t, container, 1, alpha); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Like {@link #dimAbove} but places the dim below the given container. |
| 231 | * |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 232 | * @param t A transaction in which to apply the Dim. |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 233 | * @param container The container which to dim below. Should be a child of our host. |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 234 | * @param alpha The alpha at which to Dim. |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 235 | */ |
| 236 | |
| 237 | void dimBelow(SurfaceControl.Transaction t, WindowContainer container, float alpha) { |
| 238 | dim(t, container, -1, alpha); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Mark all dims as pending completion on the next call to {@link #updateDims} |
| 243 | * |
| 244 | * This is intended for us by the host container, to be called at the beginning of |
| 245 | * {@link WindowContainer#prepareSurfaces}. After calling this, the container should |
| 246 | * chain {@link WindowContainer#prepareSurfaces} down to it's children to give them |
| 247 | * a chance to request dims to continue. |
| 248 | */ |
| 249 | void resetDimStates() { |
chaviw | b792a95 | 2018-02-02 11:55:21 -0800 | [diff] [blame] | 250 | if (mDimState != null && !mDimState.mDontReset) { |
| 251 | mDimState.mDimming = false; |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Call after invoking {@link WindowContainer#prepareSurfaces} on children as |
| 257 | * described in {@link #resetDimStates}. |
| 258 | * |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 259 | * @param t A transaction in which to update the dims. |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 260 | * @param bounds The bounds at which to dim. |
| 261 | * @return true if any Dims were updated. |
| 262 | */ |
| 263 | boolean updateDims(SurfaceControl.Transaction t, Rect bounds) { |
chaviw | b792a95 | 2018-02-02 11:55:21 -0800 | [diff] [blame] | 264 | if (mDimState == null) { |
| 265 | return false; |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 266 | } |
chaviw | b792a95 | 2018-02-02 11:55:21 -0800 | [diff] [blame] | 267 | |
| 268 | if (!mDimState.mDimming) { |
| 269 | startDimExit(mLastRequestedDimContainer, mDimState.mSurfaceAnimator, t); |
| 270 | mDimState = null; |
| 271 | return false; |
| 272 | } else { |
| 273 | // TODO: Once we use geometry from hierarchy this falls away. |
| 274 | t.setSize(mDimState.mDimLayer, bounds.width(), bounds.height()); |
| 275 | t.setPosition(mDimState.mDimLayer, bounds.left, bounds.top); |
| 276 | if (!mDimState.isVisible) { |
| 277 | mDimState.isVisible = true; |
| 278 | t.show(mDimState.mDimLayer); |
| 279 | startDimEnter(mLastRequestedDimContainer, mDimState.mSurfaceAnimator, t); |
| 280 | } |
| 281 | return true; |
| 282 | } |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 283 | } |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 284 | |
| 285 | private void startDimEnter(WindowContainer container, SurfaceAnimator animator, |
| 286 | SurfaceControl.Transaction t) { |
| 287 | startAnim(container, animator, t, 0 /* startAlpha */, 1 /* endAlpha */); |
| 288 | } |
| 289 | |
| 290 | private void startDimExit(WindowContainer container, SurfaceAnimator animator, |
| 291 | SurfaceControl.Transaction t) { |
| 292 | startAnim(container, animator, t, 1 /* startAlpha */, 0 /* endAlpha */); |
| 293 | } |
| 294 | |
| 295 | private void startAnim(WindowContainer container, SurfaceAnimator animator, |
| 296 | SurfaceControl.Transaction t, float startAlpha, float endAlpha) { |
chaviw | f20bd22 | 2018-02-01 16:06:52 -0800 | [diff] [blame] | 297 | mSurfaceAnimatorStarter.startAnimation(animator, t, new LocalAnimationAdapter( |
chaviw | 2fb06bc | 2018-01-19 17:09:15 -0800 | [diff] [blame] | 298 | new AlphaAnimationSpec(startAlpha, endAlpha, getDimDuration(container)), |
| 299 | mHost.mService.mSurfaceAnimationRunner), false /* hidden */); |
| 300 | } |
| 301 | |
| 302 | private long getDimDuration(WindowContainer container) { |
| 303 | // If there's no container, then there isn't an animation occurring while dimming. Set the |
| 304 | // duration to 0 so it immediately dims to the set alpha. |
| 305 | if (container == null) { |
| 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | // Otherwise use the same duration as the animation on the WindowContainer |
| 310 | AnimationAdapter animationAdapter = container.mSurfaceAnimator.getAnimation(); |
| 311 | return animationAdapter == null ? DEFAULT_DIM_ANIM_DURATION |
| 312 | : animationAdapter.getDurationHint(); |
| 313 | } |
| 314 | |
| 315 | private static class AlphaAnimationSpec implements LocalAnimationAdapter.AnimationSpec { |
| 316 | private final long mDuration; |
| 317 | private final float mFromAlpha; |
| 318 | private final float mToAlpha; |
| 319 | |
| 320 | AlphaAnimationSpec(float fromAlpha, float toAlpha, long duration) { |
| 321 | mFromAlpha = fromAlpha; |
| 322 | mToAlpha = toAlpha; |
| 323 | mDuration = duration; |
| 324 | } |
| 325 | |
| 326 | @Override |
| 327 | public long getDuration() { |
| 328 | return mDuration; |
| 329 | } |
| 330 | |
| 331 | @Override |
| 332 | public void apply(SurfaceControl.Transaction t, SurfaceControl sc, long currentPlayTime) { |
| 333 | float alpha = ((float) currentPlayTime / getDuration()) * (mToAlpha - mFromAlpha) |
| 334 | + mFromAlpha; |
| 335 | t.setAlpha(sc, alpha); |
| 336 | } |
| 337 | } |
Robert Carr | f59b8dd | 2017-10-02 18:58:36 -0700 | [diff] [blame] | 338 | } |