blob: 1e7cb24d53b47b62e74383ec79044d17cc23538f [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
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 Carrf59b8dd2017-10-02 18:58:36 -0700105 boolean mDimming;
chaviw2fb06bc2018-01-19 17:09:15 -0800106 boolean isVisible;
107 SurfaceAnimator mSurfaceAnimator;
Robert Carrf59b8dd2017-10-02 18:58:36 -0700108
109 /**
chaviw2fb06bc2018-01-19 17:09:15 -0800110 * Used for Dims not associated with a WindowContainer. See {@link Dimmer#dimAbove} for
Robert Carrf59b8dd2017-10-02 18:58:36 -0700111 * details on Dim lifecycle.
112 */
113 boolean mDontReset;
114
chaviw2fb06bc2018-01-19 17:09:15 -0800115 DimState(SurfaceControl dimLayer) {
116 mDimLayer = dimLayer;
Robert Carrf59b8dd2017-10-02 18:58:36 -0700117 mDimming = true;
chaviw2fb06bc2018-01-19 17:09:15 -0800118 mSurfaceAnimator = new SurfaceAnimator(new DimAnimatable(dimLayer), () -> {
119 if (!mDimming) {
120 mDimLayer.destroy();
121 }
122 }, mHost.mService.mAnimator::addAfterPrepareSurfacesRunnable, mHost.mService);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700123 }
chaviw2fb06bc2018-01-19 17:09:15 -0800124 }
Robert Carrf59b8dd2017-10-02 18:58:36 -0700125
Robert Carrf59b8dd2017-10-02 18:58:36 -0700126 /**
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;
chaviwb792a952018-02-02 11:55:21 -0800131 private WindowContainer mLastRequestedDimContainer;
132 private DimState mDimState;
Robert Carrf59b8dd2017-10-02 18:58:36 -0700133
134 Dimmer(WindowContainer host) {
135 mHost = host;
136 }
137
chaviw2fb06bc2018-01-19 17:09:15 -0800138 private SurfaceControl makeDimLayer() {
139 return mHost.makeChildSurface(null)
Robert Carrf59b8dd2017-10-02 18:58:36 -0700140 .setParent(mHost.getSurfaceControl())
141 .setColorLayer(true)
142 .setName("Dim Layer for - " + mHost.getName())
143 .build();
Robert Carrf59b8dd2017-10-02 18:58:36 -0700144 }
145
146 /**
chaviwb792a952018-02-02 11:55:21 -0800147 * Retrieve the DimState, creating one if it doesn't exist.
Robert Carrf59b8dd2017-10-02 18:58:36 -0700148 */
chaviw2fb06bc2018-01-19 17:09:15 -0800149 private DimState getDimState(WindowContainer container) {
chaviwb792a952018-02-02 11:55:21 -0800150 if (mDimState == null) {
Robert Carrf59b8dd2017-10-02 18:58:36 -0700151 final SurfaceControl ctl = makeDimLayer();
chaviwb792a952018-02-02 11:55:21 -0800152 mDimState = new DimState(ctl);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700153 /**
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) {
chaviwb792a952018-02-02 11:55:21 -0800158 mDimState.mDontReset = true;
Robert Carrf59b8dd2017-10-02 18:58:36 -0700159 }
Robert Carrf59b8dd2017-10-02 18:58:36 -0700160 }
chaviwb792a952018-02-02 11:55:21 -0800161
162 mLastRequestedDimContainer = container;
163 return mDimState;
Robert Carrf59b8dd2017-10-02 18:58:36 -0700164 }
165
166 private void dim(SurfaceControl.Transaction t, WindowContainer container, int relativeLayer,
167 float alpha) {
168 final DimState d = getDimState(container);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700169 if (container != null) {
chaviwb792a952018-02-02 11:55:21 -0800170 // 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.
chaviw2fb06bc2018-01-19 17:09:15 -0800173 t.setRelativeLayer(d.mDimLayer, container.getSurfaceControl(), relativeLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700174 } else {
chaviw2fb06bc2018-01-19 17:09:15 -0800175 t.setLayer(d.mDimLayer, Integer.MAX_VALUE);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700176 }
chaviw2fb06bc2018-01-19 17:09:15 -0800177 t.setAlpha(d.mDimLayer, alpha);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700178
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);
chaviw2fb06bc2018-01-19 17:09:15 -0800189 t.hide(d.mDimLayer);
190 d.isVisible = false;
Robert Carrf59b8dd2017-10-02 18:58:36 -0700191 d.mDontReset = false;
192 }
chaviw2fb06bc2018-01-19 17:09:15 -0800193
Robert Carrf59b8dd2017-10-02 18:58:36 -0700194 /**
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 *
chaviw2fb06bc2018-01-19 17:09:15 -0800200 * @param t A transaction in which to apply the Dim.
Robert Carrf59b8dd2017-10-02 18:58:36 -0700201 * @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 *
chaviw2fb06bc2018-01-19 17:09:15 -0800212 * @param t A transaction in which to apply the Dim.
Robert Carrf59b8dd2017-10-02 18:58:36 -0700213 * @param container The container which to dim above. Should be a child of our host.
chaviw2fb06bc2018-01-19 17:09:15 -0800214 * @param alpha The alpha at which to Dim.
Robert Carrf59b8dd2017-10-02 18:58:36 -0700215 */
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 *
chaviw2fb06bc2018-01-19 17:09:15 -0800223 * @param t A transaction in which to apply the Dim.
Robert Carrf59b8dd2017-10-02 18:58:36 -0700224 * @param container The container which to dim below. Should be a child of our host.
chaviw2fb06bc2018-01-19 17:09:15 -0800225 * @param alpha The alpha at which to Dim.
Robert Carrf59b8dd2017-10-02 18:58:36 -0700226 */
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() {
chaviwb792a952018-02-02 11:55:21 -0800241 if (mDimState != null && !mDimState.mDontReset) {
242 mDimState.mDimming = false;
Robert Carrf59b8dd2017-10-02 18:58:36 -0700243 }
244 }
245
246 /**
247 * Call after invoking {@link WindowContainer#prepareSurfaces} on children as
248 * described in {@link #resetDimStates}.
249 *
chaviw2fb06bc2018-01-19 17:09:15 -0800250 * @param t A transaction in which to update the dims.
Robert Carrf59b8dd2017-10-02 18:58:36 -0700251 * @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) {
chaviwb792a952018-02-02 11:55:21 -0800255 if (mDimState == null) {
256 return false;
Robert Carrf59b8dd2017-10-02 18:58:36 -0700257 }
chaviwb792a952018-02-02 11:55:21 -0800258
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 Carrf59b8dd2017-10-02 18:58:36 -0700274 }
chaviw2fb06bc2018-01-19 17:09:15 -0800275
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 Carrf59b8dd2017-10-02 18:58:36 -0700329}