blob: 5c62987ec52133f1f73df0a158a09f6bdcb7e840 [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
Jorim Jaggif75d1612018-02-27 15:05:21 +010019import static com.android.server.wm.proto.AlphaAnimationSpecProto.DURATION;
20import static com.android.server.wm.proto.AlphaAnimationSpecProto.FROM;
21import static com.android.server.wm.proto.AlphaAnimationSpecProto.TO;
22import static com.android.server.wm.proto.AnimationSpecProto.ALPHA;
23
Robert Carrf59b8dd2017-10-02 18:58:36 -070024import android.graphics.Rect;
Jorim Jaggif75d1612018-02-27 15:05:21 +010025import android.util.proto.ProtoOutputStream;
26import android.view.SurfaceControl;
Robert Carrf59b8dd2017-10-02 18:58:36 -070027
chaviw2fb06bc2018-01-19 17:09:15 -080028import com.android.internal.annotations.VisibleForTesting;
29
Jorim Jaggif75d1612018-02-27 15:05:21 +010030import java.io.PrintWriter;
31
Robert Carrf59b8dd2017-10-02 18:58:36 -070032/**
33 * Utility class for use by a WindowContainer implementation to add "DimLayer" support, that is
34 * black layers of varying opacity at various Z-levels which create the effect of a Dim.
35 */
36class Dimmer {
37 private static final String TAG = "WindowManager";
chaviw2fb06bc2018-01-19 17:09:15 -080038 private static final int DEFAULT_DIM_ANIM_DURATION = 200;
Robert Carrf59b8dd2017-10-02 18:58:36 -070039
chaviw2fb06bc2018-01-19 17:09:15 -080040 private class DimAnimatable implements SurfaceAnimator.Animatable {
41 private final SurfaceControl mDimLayer;
42
43 private DimAnimatable(SurfaceControl dimLayer) {
44 mDimLayer = dimLayer;
45 }
46
47 @Override
48 public SurfaceControl.Transaction getPendingTransaction() {
49 return mHost.getPendingTransaction();
50 }
51
52 @Override
53 public void commitPendingTransaction() {
54 mHost.commitPendingTransaction();
55 }
56
57 @Override
58 public void onAnimationLeashCreated(SurfaceControl.Transaction t, SurfaceControl leash) {
59 }
60
61 @Override
62 public void onAnimationLeashDestroyed(SurfaceControl.Transaction t) {
63 }
64
65 @Override
chaviw2fb06bc2018-01-19 17:09:15 -080066 public SurfaceControl.Builder makeAnimationLeash() {
67 return mHost.makeAnimationLeash();
68 }
69
70 @Override
71 public SurfaceControl getAnimationLeashParent() {
72 return mHost.getSurfaceControl();
73 }
74
75 @Override
76 public SurfaceControl getSurfaceControl() {
77 return mDimLayer;
78 }
79
80 @Override
81 public SurfaceControl getParentSurfaceControl() {
82 return mHost.getSurfaceControl();
83 }
84
85 @Override
86 public int getSurfaceWidth() {
87 // This will determine the size of the leash created. This should be the size of the
88 // host and not the dim layer since the dim layer may get bigger during animation. If
89 // that occurs, the leash size cannot change so we need to ensure the leash is big
90 // enough that the dim layer can grow.
91 // This works because the mHost will be a Task which has the display bounds.
92 return mHost.getSurfaceWidth();
93 }
94
95 @Override
96 public int getSurfaceHeight() {
97 // See getSurfaceWidth() above for explanation.
98 return mHost.getSurfaceHeight();
99 }
100 }
101
102 @VisibleForTesting
103 class DimState {
104 /**
105 * The layer where property changes should be invoked on.
106 */
107 SurfaceControl mDimLayer;
Robert Carrf59b8dd2017-10-02 18:58:36 -0700108 boolean mDimming;
chaviw2fb06bc2018-01-19 17:09:15 -0800109 boolean isVisible;
110 SurfaceAnimator mSurfaceAnimator;
Robert Carrf59b8dd2017-10-02 18:58:36 -0700111
112 /**
chaviw2fb06bc2018-01-19 17:09:15 -0800113 * Used for Dims not associated with a WindowContainer. See {@link Dimmer#dimAbove} for
Robert Carrf59b8dd2017-10-02 18:58:36 -0700114 * details on Dim lifecycle.
115 */
116 boolean mDontReset;
117
chaviw2fb06bc2018-01-19 17:09:15 -0800118 DimState(SurfaceControl dimLayer) {
119 mDimLayer = dimLayer;
Robert Carrf59b8dd2017-10-02 18:58:36 -0700120 mDimming = true;
chaviw2fb06bc2018-01-19 17:09:15 -0800121 mSurfaceAnimator = new SurfaceAnimator(new DimAnimatable(dimLayer), () -> {
122 if (!mDimming) {
123 mDimLayer.destroy();
124 }
Chavi Weingartenb736e322018-02-23 00:27:54 +0000125 }, mHost.mService);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700126 }
chaviw2fb06bc2018-01-19 17:09:15 -0800127 }
Robert Carrf59b8dd2017-10-02 18:58:36 -0700128
Robert Carrf59b8dd2017-10-02 18:58:36 -0700129 /**
130 * The {@link WindowContainer} that our Dim's are bounded to. We may be dimming on behalf of the
131 * host, some controller of it, or one of the hosts children.
132 */
133 private WindowContainer mHost;
chaviwb792a952018-02-02 11:55:21 -0800134 private WindowContainer mLastRequestedDimContainer;
chaviwf20bd222018-02-01 16:06:52 -0800135 @VisibleForTesting
136 DimState mDimState;
137
138 private final SurfaceAnimatorStarter mSurfaceAnimatorStarter;
139
140 @VisibleForTesting
141 interface SurfaceAnimatorStarter {
142 void startAnimation(SurfaceAnimator surfaceAnimator, SurfaceControl.Transaction t,
143 AnimationAdapter anim, boolean hidden);
144 }
Robert Carrf59b8dd2017-10-02 18:58:36 -0700145
146 Dimmer(WindowContainer host) {
chaviwf20bd222018-02-01 16:06:52 -0800147 this(host, SurfaceAnimator::startAnimation);
148 }
149
150 Dimmer(WindowContainer host, SurfaceAnimatorStarter surfaceAnimatorStarter) {
Robert Carrf59b8dd2017-10-02 18:58:36 -0700151 mHost = host;
chaviwf20bd222018-02-01 16:06:52 -0800152 mSurfaceAnimatorStarter = surfaceAnimatorStarter;
Robert Carrf59b8dd2017-10-02 18:58:36 -0700153 }
154
chaviw2fb06bc2018-01-19 17:09:15 -0800155 private SurfaceControl makeDimLayer() {
156 return mHost.makeChildSurface(null)
Robert Carrf59b8dd2017-10-02 18:58:36 -0700157 .setParent(mHost.getSurfaceControl())
158 .setColorLayer(true)
159 .setName("Dim Layer for - " + mHost.getName())
160 .build();
Robert Carrf59b8dd2017-10-02 18:58:36 -0700161 }
162
163 /**
chaviwb792a952018-02-02 11:55:21 -0800164 * Retrieve the DimState, creating one if it doesn't exist.
Robert Carrf59b8dd2017-10-02 18:58:36 -0700165 */
chaviw2fb06bc2018-01-19 17:09:15 -0800166 private DimState getDimState(WindowContainer container) {
chaviwb792a952018-02-02 11:55:21 -0800167 if (mDimState == null) {
Robert Carrf59b8dd2017-10-02 18:58:36 -0700168 final SurfaceControl ctl = makeDimLayer();
chaviwb792a952018-02-02 11:55:21 -0800169 mDimState = new DimState(ctl);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700170 /**
171 * See documentation on {@link #dimAbove} to understand lifecycle management of Dim's
172 * via state resetting for Dim's with containers.
173 */
174 if (container == null) {
chaviwb792a952018-02-02 11:55:21 -0800175 mDimState.mDontReset = true;
Robert Carrf59b8dd2017-10-02 18:58:36 -0700176 }
Robert Carrf59b8dd2017-10-02 18:58:36 -0700177 }
chaviwb792a952018-02-02 11:55:21 -0800178
179 mLastRequestedDimContainer = container;
180 return mDimState;
Robert Carrf59b8dd2017-10-02 18:58:36 -0700181 }
182
183 private void dim(SurfaceControl.Transaction t, WindowContainer container, int relativeLayer,
184 float alpha) {
185 final DimState d = getDimState(container);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700186 if (container != null) {
chaviwb792a952018-02-02 11:55:21 -0800187 // The dim method is called from WindowState.prepareSurfaces(), which is always called
188 // in the correct Z from lowest Z to highest. This ensures that the dim layer is always
189 // relative to the highest Z layer with a dim.
chaviw2fb06bc2018-01-19 17:09:15 -0800190 t.setRelativeLayer(d.mDimLayer, container.getSurfaceControl(), relativeLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700191 } else {
chaviw2fb06bc2018-01-19 17:09:15 -0800192 t.setLayer(d.mDimLayer, Integer.MAX_VALUE);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700193 }
chaviw2fb06bc2018-01-19 17:09:15 -0800194 t.setAlpha(d.mDimLayer, alpha);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700195
196 d.mDimming = true;
197 }
198
199 /**
200 * Finish a dim started by dimAbove in the case there was no call to dimAbove.
201 *
202 * @param t A Transaction in which to finish the dim.
203 */
204 void stopDim(SurfaceControl.Transaction t) {
205 DimState d = getDimState(null);
chaviw2fb06bc2018-01-19 17:09:15 -0800206 t.hide(d.mDimLayer);
207 d.isVisible = false;
Robert Carrf59b8dd2017-10-02 18:58:36 -0700208 d.mDontReset = false;
209 }
chaviw2fb06bc2018-01-19 17:09:15 -0800210
Robert Carrf59b8dd2017-10-02 18:58:36 -0700211 /**
212 * Place a Dim above the entire host container. The caller is responsible for calling stopDim to
213 * remove this effect. If the Dim can be assosciated with a particular child of the host
214 * consider using the other variant of dimAbove which ties the Dim lifetime to the child
215 * lifetime more explicitly.
216 *
chaviw2fb06bc2018-01-19 17:09:15 -0800217 * @param t A transaction in which to apply the Dim.
Robert Carrf59b8dd2017-10-02 18:58:36 -0700218 * @param alpha The alpha at which to Dim.
219 */
220 void dimAbove(SurfaceControl.Transaction t, float alpha) {
221 dim(t, null, 1, alpha);
222 }
223
224 /**
225 * Place a dim above the given container, which should be a child of the host container.
226 * for each call to {@link WindowContainer#prepareSurfaces} the Dim state will be reset
227 * and the child should call dimAbove again to request the Dim to continue.
228 *
chaviw2fb06bc2018-01-19 17:09:15 -0800229 * @param t A transaction in which to apply the Dim.
Robert Carrf59b8dd2017-10-02 18:58:36 -0700230 * @param container The container which to dim above. Should be a child of our host.
chaviw2fb06bc2018-01-19 17:09:15 -0800231 * @param alpha The alpha at which to Dim.
Robert Carrf59b8dd2017-10-02 18:58:36 -0700232 */
233 void dimAbove(SurfaceControl.Transaction t, WindowContainer container, float alpha) {
234 dim(t, container, 1, alpha);
235 }
236
237 /**
238 * Like {@link #dimAbove} but places the dim below the given container.
239 *
chaviw2fb06bc2018-01-19 17:09:15 -0800240 * @param t A transaction in which to apply the Dim.
Robert Carrf59b8dd2017-10-02 18:58:36 -0700241 * @param container The container which to dim below. Should be a child of our host.
chaviw2fb06bc2018-01-19 17:09:15 -0800242 * @param alpha The alpha at which to Dim.
Robert Carrf59b8dd2017-10-02 18:58:36 -0700243 */
244
245 void dimBelow(SurfaceControl.Transaction t, WindowContainer container, float alpha) {
246 dim(t, container, -1, alpha);
247 }
248
249 /**
250 * Mark all dims as pending completion on the next call to {@link #updateDims}
251 *
252 * This is intended for us by the host container, to be called at the beginning of
253 * {@link WindowContainer#prepareSurfaces}. After calling this, the container should
254 * chain {@link WindowContainer#prepareSurfaces} down to it's children to give them
255 * a chance to request dims to continue.
256 */
257 void resetDimStates() {
chaviwb792a952018-02-02 11:55:21 -0800258 if (mDimState != null && !mDimState.mDontReset) {
259 mDimState.mDimming = false;
Robert Carrf59b8dd2017-10-02 18:58:36 -0700260 }
261 }
262
263 /**
264 * Call after invoking {@link WindowContainer#prepareSurfaces} on children as
265 * described in {@link #resetDimStates}.
266 *
chaviw2fb06bc2018-01-19 17:09:15 -0800267 * @param t A transaction in which to update the dims.
Robert Carrf59b8dd2017-10-02 18:58:36 -0700268 * @param bounds The bounds at which to dim.
269 * @return true if any Dims were updated.
270 */
271 boolean updateDims(SurfaceControl.Transaction t, Rect bounds) {
chaviwb792a952018-02-02 11:55:21 -0800272 if (mDimState == null) {
273 return false;
Robert Carrf59b8dd2017-10-02 18:58:36 -0700274 }
chaviwb792a952018-02-02 11:55:21 -0800275
276 if (!mDimState.mDimming) {
277 startDimExit(mLastRequestedDimContainer, mDimState.mSurfaceAnimator, t);
278 mDimState = null;
279 return false;
280 } else {
281 // TODO: Once we use geometry from hierarchy this falls away.
282 t.setSize(mDimState.mDimLayer, bounds.width(), bounds.height());
283 t.setPosition(mDimState.mDimLayer, bounds.left, bounds.top);
284 if (!mDimState.isVisible) {
285 mDimState.isVisible = true;
286 t.show(mDimState.mDimLayer);
287 startDimEnter(mLastRequestedDimContainer, mDimState.mSurfaceAnimator, t);
288 }
289 return true;
290 }
Robert Carrf59b8dd2017-10-02 18:58:36 -0700291 }
chaviw2fb06bc2018-01-19 17:09:15 -0800292
293 private void startDimEnter(WindowContainer container, SurfaceAnimator animator,
294 SurfaceControl.Transaction t) {
295 startAnim(container, animator, t, 0 /* startAlpha */, 1 /* endAlpha */);
296 }
297
298 private void startDimExit(WindowContainer container, SurfaceAnimator animator,
299 SurfaceControl.Transaction t) {
300 startAnim(container, animator, t, 1 /* startAlpha */, 0 /* endAlpha */);
301 }
302
303 private void startAnim(WindowContainer container, SurfaceAnimator animator,
304 SurfaceControl.Transaction t, float startAlpha, float endAlpha) {
chaviwf20bd222018-02-01 16:06:52 -0800305 mSurfaceAnimatorStarter.startAnimation(animator, t, new LocalAnimationAdapter(
chaviw2fb06bc2018-01-19 17:09:15 -0800306 new AlphaAnimationSpec(startAlpha, endAlpha, getDimDuration(container)),
307 mHost.mService.mSurfaceAnimationRunner), false /* hidden */);
308 }
309
310 private long getDimDuration(WindowContainer container) {
311 // If there's no container, then there isn't an animation occurring while dimming. Set the
312 // duration to 0 so it immediately dims to the set alpha.
313 if (container == null) {
314 return 0;
315 }
316
317 // Otherwise use the same duration as the animation on the WindowContainer
318 AnimationAdapter animationAdapter = container.mSurfaceAnimator.getAnimation();
319 return animationAdapter == null ? DEFAULT_DIM_ANIM_DURATION
320 : animationAdapter.getDurationHint();
321 }
322
323 private static class AlphaAnimationSpec implements LocalAnimationAdapter.AnimationSpec {
324 private final long mDuration;
325 private final float mFromAlpha;
326 private final float mToAlpha;
327
328 AlphaAnimationSpec(float fromAlpha, float toAlpha, long duration) {
329 mFromAlpha = fromAlpha;
330 mToAlpha = toAlpha;
331 mDuration = duration;
332 }
333
334 @Override
335 public long getDuration() {
336 return mDuration;
337 }
338
339 @Override
340 public void apply(SurfaceControl.Transaction t, SurfaceControl sc, long currentPlayTime) {
341 float alpha = ((float) currentPlayTime / getDuration()) * (mToAlpha - mFromAlpha)
342 + mFromAlpha;
343 t.setAlpha(sc, alpha);
344 }
Jorim Jaggif75d1612018-02-27 15:05:21 +0100345
346 @Override
347 public void dump(PrintWriter pw, String prefix) {
348 pw.print(prefix); pw.print("from="); pw.print(mFromAlpha);
349 pw.print(" to="); pw.print(mToAlpha);
350 pw.print(" duration="); pw.println(mDuration);
351 }
352
353 @Override
354 public void writeToProtoInner(ProtoOutputStream proto) {
355 final long token = proto.start(ALPHA);
356 proto.write(FROM, mFromAlpha);
357 proto.write(TO, mToAlpha);
358 proto.write(DURATION, mDuration);
359 proto.end(token);
360 }
chaviw2fb06bc2018-01-19 17:09:15 -0800361 }
Robert Carrf59b8dd2017-10-02 18:58:36 -0700362}