blob: a180a3acfbed61bc296a73a56991fd878e8b279f [file] [log] [blame]
Robert Carrf59b8dd2017-10-02 18:58:36 -07001/*
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
17package com.android.server.wm;
18
Robert Carrf59b8dd2017-10-02 18:58:36 -070019import android.view.SurfaceControl;
20import android.graphics.Rect;
21
chaviw2fb06bc2018-01-19 17:09:15 -080022import com.android.internal.annotations.VisibleForTesting;
23
Robert Carrf59b8dd2017-10-02 18:58:36 -070024/**
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 */
28class Dimmer {
29 private static final String TAG = "WindowManager";
chaviw2fb06bc2018-01-19 17:09:15 -080030 private static final int DEFAULT_DIM_ANIM_DURATION = 200;
Robert Carrf59b8dd2017-10-02 18:58:36 -070031
chaviw2fb06bc2018-01-19 17:09:15 -080032 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
chaviw2fb06bc2018-01-19 17:09:15 -080058 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 Carrf59b8dd2017-10-02 18:58:36 -0700100 boolean mDimming;
chaviw2fb06bc2018-01-19 17:09:15 -0800101 boolean isVisible;
102 SurfaceAnimator mSurfaceAnimator;
Robert Carrf59b8dd2017-10-02 18:58:36 -0700103
104 /**
chaviw2fb06bc2018-01-19 17:09:15 -0800105 * Used for Dims not associated with a WindowContainer. See {@link Dimmer#dimAbove} for
Robert Carrf59b8dd2017-10-02 18:58:36 -0700106 * details on Dim lifecycle.
107 */
108 boolean mDontReset;
109
chaviw2fb06bc2018-01-19 17:09:15 -0800110 DimState(SurfaceControl dimLayer) {
111 mDimLayer = dimLayer;
Robert Carrf59b8dd2017-10-02 18:58:36 -0700112 mDimming = true;
chaviw2fb06bc2018-01-19 17:09:15 -0800113 mSurfaceAnimator = new SurfaceAnimator(new DimAnimatable(dimLayer), () -> {
114 if (!mDimming) {
115 mDimLayer.destroy();
116 }
Chavi Weingartenb736e322018-02-23 00:27:54 +0000117 }, mHost.mService);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700118 }
chaviw2fb06bc2018-01-19 17:09:15 -0800119 }
Robert Carrf59b8dd2017-10-02 18:58:36 -0700120
Robert Carrf59b8dd2017-10-02 18:58:36 -0700121 /**
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;
chaviwb792a952018-02-02 11:55:21 -0800126 private WindowContainer mLastRequestedDimContainer;
chaviwf20bd222018-02-01 16:06:52 -0800127 @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 Carrf59b8dd2017-10-02 18:58:36 -0700137
138 Dimmer(WindowContainer host) {
chaviwf20bd222018-02-01 16:06:52 -0800139 this(host, SurfaceAnimator::startAnimation);
140 }
141
142 Dimmer(WindowContainer host, SurfaceAnimatorStarter surfaceAnimatorStarter) {
Robert Carrf59b8dd2017-10-02 18:58:36 -0700143 mHost = host;
chaviwf20bd222018-02-01 16:06:52 -0800144 mSurfaceAnimatorStarter = surfaceAnimatorStarter;
Robert Carrf59b8dd2017-10-02 18:58:36 -0700145 }
146
chaviw2fb06bc2018-01-19 17:09:15 -0800147 private SurfaceControl makeDimLayer() {
148 return mHost.makeChildSurface(null)
Robert Carrf59b8dd2017-10-02 18:58:36 -0700149 .setParent(mHost.getSurfaceControl())
150 .setColorLayer(true)
151 .setName("Dim Layer for - " + mHost.getName())
152 .build();
Robert Carrf59b8dd2017-10-02 18:58:36 -0700153 }
154
155 /**
chaviwb792a952018-02-02 11:55:21 -0800156 * Retrieve the DimState, creating one if it doesn't exist.
Robert Carrf59b8dd2017-10-02 18:58:36 -0700157 */
chaviw2fb06bc2018-01-19 17:09:15 -0800158 private DimState getDimState(WindowContainer container) {
chaviwb792a952018-02-02 11:55:21 -0800159 if (mDimState == null) {
Robert Carrf59b8dd2017-10-02 18:58:36 -0700160 final SurfaceControl ctl = makeDimLayer();
chaviwb792a952018-02-02 11:55:21 -0800161 mDimState = new DimState(ctl);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700162 /**
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) {
chaviwb792a952018-02-02 11:55:21 -0800167 mDimState.mDontReset = true;
Robert Carrf59b8dd2017-10-02 18:58:36 -0700168 }
Robert Carrf59b8dd2017-10-02 18:58:36 -0700169 }
chaviwb792a952018-02-02 11:55:21 -0800170
171 mLastRequestedDimContainer = container;
172 return mDimState;
Robert Carrf59b8dd2017-10-02 18:58:36 -0700173 }
174
175 private void dim(SurfaceControl.Transaction t, WindowContainer container, int relativeLayer,
176 float alpha) {
177 final DimState d = getDimState(container);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700178 if (container != null) {
chaviwb792a952018-02-02 11:55:21 -0800179 // 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.
chaviw2fb06bc2018-01-19 17:09:15 -0800182 t.setRelativeLayer(d.mDimLayer, container.getSurfaceControl(), relativeLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700183 } else {
chaviw2fb06bc2018-01-19 17:09:15 -0800184 t.setLayer(d.mDimLayer, Integer.MAX_VALUE);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700185 }
chaviw2fb06bc2018-01-19 17:09:15 -0800186 t.setAlpha(d.mDimLayer, alpha);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700187
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);
chaviw2fb06bc2018-01-19 17:09:15 -0800198 t.hide(d.mDimLayer);
199 d.isVisible = false;
Robert Carrf59b8dd2017-10-02 18:58:36 -0700200 d.mDontReset = false;
201 }
chaviw2fb06bc2018-01-19 17:09:15 -0800202
Robert Carrf59b8dd2017-10-02 18:58:36 -0700203 /**
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 *
chaviw2fb06bc2018-01-19 17:09:15 -0800209 * @param t A transaction in which to apply the Dim.
Robert Carrf59b8dd2017-10-02 18:58:36 -0700210 * @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 *
chaviw2fb06bc2018-01-19 17:09:15 -0800221 * @param t A transaction in which to apply the Dim.
Robert Carrf59b8dd2017-10-02 18:58:36 -0700222 * @param container The container which to dim above. Should be a child of our host.
chaviw2fb06bc2018-01-19 17:09:15 -0800223 * @param alpha The alpha at which to Dim.
Robert Carrf59b8dd2017-10-02 18:58:36 -0700224 */
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 *
chaviw2fb06bc2018-01-19 17:09:15 -0800232 * @param t A transaction in which to apply the Dim.
Robert Carrf59b8dd2017-10-02 18:58:36 -0700233 * @param container The container which to dim below. Should be a child of our host.
chaviw2fb06bc2018-01-19 17:09:15 -0800234 * @param alpha The alpha at which to Dim.
Robert Carrf59b8dd2017-10-02 18:58:36 -0700235 */
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() {
chaviwb792a952018-02-02 11:55:21 -0800250 if (mDimState != null && !mDimState.mDontReset) {
251 mDimState.mDimming = false;
Robert Carrf59b8dd2017-10-02 18:58:36 -0700252 }
253 }
254
255 /**
256 * Call after invoking {@link WindowContainer#prepareSurfaces} on children as
257 * described in {@link #resetDimStates}.
258 *
chaviw2fb06bc2018-01-19 17:09:15 -0800259 * @param t A transaction in which to update the dims.
Robert Carrf59b8dd2017-10-02 18:58:36 -0700260 * @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) {
chaviwb792a952018-02-02 11:55:21 -0800264 if (mDimState == null) {
265 return false;
Robert Carrf59b8dd2017-10-02 18:58:36 -0700266 }
chaviwb792a952018-02-02 11:55:21 -0800267
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 Carrf59b8dd2017-10-02 18:58:36 -0700283 }
chaviw2fb06bc2018-01-19 17:09:15 -0800284
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) {
chaviwf20bd222018-02-01 16:06:52 -0800297 mSurfaceAnimatorStarter.startAnimation(animator, t, new LocalAnimationAdapter(
chaviw2fb06bc2018-01-19 17:09:15 -0800298 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 Carrf59b8dd2017-10-02 18:58:36 -0700338}