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