Debian Codegraph @

libs/WindowManager/Shell/src/com/android/wm/shell/hidedisplaycutout/HideDisplayCutoutOrganizer.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2020 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.wm.shell.hidedisplaycutout;
18
19 import static android.view.Display.DEFAULT_DISPLAY;
20
21 import android.content.Context;
22 import android.content.res.Configuration;
23 import android.graphics.Insets;
24 import android.graphics.Rect;
25 import android.util.ArrayMap;
26 import android.util.Log;
27 import android.util.RotationUtils;
28 import android.view.Display;
29 import android.view.DisplayCutout;
30 import android.view.Surface;
31 import android.view.SurfaceControl;
32 import android.window.DisplayAreaAppearedInfo;
33 import android.window.DisplayAreaInfo;
34 import android.window.DisplayAreaOrganizer;
35 import android.window.WindowContainerToken;
36 import android.window.WindowContainerTransaction;
37
38 import androidx.annotation.GuardedBy;
39 import androidx.annotation.NonNull;
40 import androidx.annotation.VisibleForTesting;
41
42 import com.android.internal.policy.SystemBarUtils;
43 import com.android.wm.shell.common.DisplayController;
44 import com.android.wm.shell.common.DisplayLayout;
45 import com.android.wm.shell.common.ShellExecutor;
46
47 import java.io.PrintWriter;
48 import java.util.List;
49
50 /**
51 * Manages the display areas of hide display cutout feature.
52 */
53 class HideDisplayCutoutOrganizer extends DisplayAreaOrganizer {
54 private static final String TAG = "HideDisplayCutoutOrganizer";
55
56 private final Context mContext;
57 private final DisplayController mDisplayController;
58
59 @VisibleForTesting
60 @GuardedBy("this")
61 ArrayMap<WindowContainerToken, SurfaceControl> mDisplayAreaMap = new ArrayMap();
62 // The default display bound in natural orientation.
63 private final Rect mDefaultDisplayBounds = new Rect();
64 @VisibleForTesting
65 final Rect mCurrentDisplayBounds = new Rect();
66 // The default display cutout in natural orientation.
67 private Insets mDefaultCutoutInsets = Insets.NONE;
68 private Insets mCurrentCutoutInsets = Insets.NONE;
69 private boolean mIsDefaultPortrait;
70 private int mStatusBarHeight;
71 @VisibleForTesting
72 int mOffsetX;
73 @VisibleForTesting
74 int mOffsetY;
75 @VisibleForTesting
76 int mRotation;
77
78 private final DisplayController.OnDisplaysChangedListener mListener =
79 new DisplayController.OnDisplaysChangedListener() {
80 @Override
81 public void onDisplayAdded(int displayId) {
82 onDisplayChanged(displayId);
83 }
84
85 @Override
86 public void onDisplayConfigurationChanged(int displayId, Configuration newConfig) {
87 onDisplayChanged(displayId);
88 }
89 };
90
91 private void onDisplayChanged(int displayId) {
92 if (displayId != DEFAULT_DISPLAY) {
93 return;
94 }
95 final DisplayLayout displayLayout = mDisplayController.getDisplayLayout(DEFAULT_DISPLAY);
96 if (displayLayout == null) {
97 return;
98 }
99 final boolean rotationChanged = mRotation != displayLayout.rotation();
100 mRotation = displayLayout.rotation();
101 if (rotationChanged || isDisplayBoundsChanged()) {
102 updateBoundsAndOffsets(true /* enabled */);
103 final WindowContainerTransaction wct = new WindowContainerTransaction();
104 final SurfaceControl.Transaction t = new SurfaceControl.Transaction();
105 applyAllBoundsAndOffsets(wct, t);
106 applyTransaction(wct, t);
107 }
108 }
109
110 HideDisplayCutoutOrganizer(Context context, DisplayController displayController,
111 ShellExecutor mainExecutor) {
112 super(mainExecutor);
113 mContext = context;
114 mDisplayController = displayController;
115 }
116
117 @Override
118 public void onDisplayAreaAppeared(@NonNull DisplayAreaInfo displayAreaInfo,
119 @NonNull SurfaceControl leash) {
120 if (!addDisplayAreaInfoAndLeashToMap(displayAreaInfo, leash)) {
121 return;
122 }
123 final WindowContainerTransaction wct = new WindowContainerTransaction();
124 final SurfaceControl.Transaction tx = new SurfaceControl.Transaction();
125 applyBoundsAndOffsets(displayAreaInfo.token, leash, wct, tx);
126 applyTransaction(wct, tx);
127 }
128
129 @Override
130 public void onDisplayAreaVanished(@NonNull DisplayAreaInfo displayAreaInfo) {
131 synchronized (this) {
132 if (!mDisplayAreaMap.containsKey(displayAreaInfo.token)) {
133 Log.w(TAG, "Unrecognized token: " + displayAreaInfo.token);
134 return;
135 }
136
137 final WindowContainerTransaction wct = new WindowContainerTransaction();
138 final SurfaceControl.Transaction t = new SurfaceControl.Transaction();
139 final SurfaceControl leash = mDisplayAreaMap.get(displayAreaInfo.token);
140 applyBoundsAndOffsets(displayAreaInfo.token, leash, wct, t);
141 applyTransaction(wct, t);
142 leash.release();
143 mDisplayAreaMap.remove(displayAreaInfo.token);
144 }
145 }
146
147 private void updateDisplayAreaMap(List<DisplayAreaAppearedInfo> displayAreaInfos) {
148 for (int i = 0; i < displayAreaInfos.size(); i++) {
149 final DisplayAreaInfo info = displayAreaInfos.get(i).getDisplayAreaInfo();
150 final SurfaceControl leash = displayAreaInfos.get(i).getLeash();
151 addDisplayAreaInfoAndLeashToMap(info, leash);
152 }
153 }
154
155 @VisibleForTesting
156 boolean addDisplayAreaInfoAndLeashToMap(@NonNull DisplayAreaInfo displayAreaInfo,
157 @NonNull SurfaceControl leash) {
158 synchronized (this) {
159 if (displayAreaInfo.displayId != DEFAULT_DISPLAY) {
160 return false;
161 }
162 if (mDisplayAreaMap.containsKey(displayAreaInfo.token)) {
163 Log.w(TAG, "Already appeared token: " + displayAreaInfo.token);
164 return false;
165 }
166 mDisplayAreaMap.put(displayAreaInfo.token, leash);
167 return true;
168 }
169 }
170
171 /**
172 * Enables hide display cutout.
173 */
174 void enableHideDisplayCutout() {
175 mDisplayController.addDisplayWindowListener(mListener);
176 final DisplayLayout displayLayout = mDisplayController.getDisplayLayout(DEFAULT_DISPLAY);
177 if (displayLayout != null) {
178 mRotation = displayLayout.rotation();
179 }
180 final List<DisplayAreaAppearedInfo> displayAreaInfos =
181 registerOrganizer(DisplayAreaOrganizer.FEATURE_HIDE_DISPLAY_CUTOUT);
182 updateDisplayAreaMap(displayAreaInfos);
183 updateBoundsAndOffsets(true /* enabled */);
184 final WindowContainerTransaction wct = new WindowContainerTransaction();
185 final SurfaceControl.Transaction t = new SurfaceControl.Transaction();
186 applyAllBoundsAndOffsets(wct, t);
187 applyTransaction(wct, t);
188 }
189
190 /**
191 * Disables hide display cutout.
192 */
193 void disableHideDisplayCutout() {
194 updateBoundsAndOffsets(false /* enabled */);
195 mDisplayController.removeDisplayWindowListener(mListener);
196 unregisterOrganizer();
197 }
198
199 @VisibleForTesting
200 Insets getDisplayCutoutInsetsOfNaturalOrientation() {
201 final Display display = mDisplayController.getDisplay(DEFAULT_DISPLAY);
202 if (display == null) {
203 return Insets.NONE;
204 }
205 DisplayCutout cutout = display.getCutout();
206 Insets insets = cutout != null ? Insets.of(cutout.getSafeInsets()) : Insets.NONE;
207 return mRotation != Surface.ROTATION_0
208 ? RotationUtils.rotateInsets(insets, 4 /* total number of rotation */ - mRotation)
209 : insets;
210 }
211
212 @VisibleForTesting
213 Rect getDisplayBoundsOfNaturalOrientation() {
214 final DisplayLayout displayLayout = mDisplayController.getDisplayLayout(DEFAULT_DISPLAY);
215 if (displayLayout == null) {
216 return new Rect();
217 }
218 final boolean isDisplaySizeFlipped = isDisplaySizeFlipped();
219 return new Rect(
220 0,
221 0,
222 isDisplaySizeFlipped ? displayLayout.height() : displayLayout.width(),
223 isDisplaySizeFlipped ? displayLayout.width() : displayLayout.height());
224 }
225
226 private boolean isDisplaySizeFlipped() {
227 return mRotation == Surface.ROTATION_90 || mRotation == Surface.ROTATION_270;
228 }
229
230 private boolean isDisplayBoundsChanged() {
231 final DisplayLayout displayLayout = mDisplayController.getDisplayLayout(DEFAULT_DISPLAY);
232 if (displayLayout == null) {
233 return false;
234 }
235 final boolean isDisplaySizeFlipped = isDisplaySizeFlipped();
236 final int width = isDisplaySizeFlipped ? displayLayout.height() : displayLayout.width();
237 final int height = isDisplaySizeFlipped ? displayLayout.width() : displayLayout.height();
238 return mDefaultDisplayBounds.isEmpty()
239 || mDefaultDisplayBounds.width() != width
240 || mDefaultDisplayBounds.height() != height;
241 }
242
243 /**
244 * Updates bounds and offsets according to current state.
245 *
246 * @param enabled whether the hide display cutout feature is enabled.
247 */
248 @VisibleForTesting
249 void updateBoundsAndOffsets(boolean enabled) {
250 if (!enabled) {
251 resetBoundsAndOffsets();
252 } else {
253 initDefaultValuesIfNeeded();
254
255 // Reset to default values.
256 mCurrentDisplayBounds.set(mDefaultDisplayBounds);
257 mOffsetX = 0;
258 mOffsetY = 0;
259
260 // Update bounds and insets according to the rotation.
261 mCurrentCutoutInsets = RotationUtils.rotateInsets(mDefaultCutoutInsets, mRotation);
262 if (isDisplaySizeFlipped()) {
263 mCurrentDisplayBounds.set(
264 mCurrentDisplayBounds.top,
265 mCurrentDisplayBounds.left,
266 mCurrentDisplayBounds.bottom,
267 mCurrentDisplayBounds.right);
268 }
269 mCurrentDisplayBounds.inset(mCurrentCutoutInsets);
270 // Replace the top bound with the max(status bar height, cutout height) if there is
271 // cutout on the top side.
272 mStatusBarHeight = getStatusBarHeight();
273 if (mCurrentCutoutInsets.top != 0) {
274 mCurrentDisplayBounds.top = Math.max(mStatusBarHeight, mCurrentCutoutInsets.top);
275 }
276 mOffsetX = mCurrentDisplayBounds.left;
277 mOffsetY = mCurrentDisplayBounds.top;
278 }
279 }
280
281 private void resetBoundsAndOffsets() {
282 mCurrentDisplayBounds.setEmpty();
283 mOffsetX = 0;
284 mOffsetY = 0;
285 }
286
287 private void initDefaultValuesIfNeeded() {
288 if (!isDisplayBoundsChanged()) {
289 return;
290 }
291 mDefaultDisplayBounds.set(getDisplayBoundsOfNaturalOrientation());
292 mDefaultCutoutInsets = getDisplayCutoutInsetsOfNaturalOrientation();
293 mIsDefaultPortrait = mDefaultDisplayBounds.width() < mDefaultDisplayBounds.height();
294 }
295
296 private void applyAllBoundsAndOffsets(
297 WindowContainerTransaction wct, SurfaceControl.Transaction t) {
298 synchronized (this) {
299 mDisplayAreaMap.forEach((token, leash) -> {
300 applyBoundsAndOffsets(token, leash, wct, t);
301 });
302 }
303 }
304
305 @VisibleForTesting
306 void applyBoundsAndOffsets(WindowContainerToken token, SurfaceControl leash,
307 WindowContainerTransaction wct, SurfaceControl.Transaction t) {
308 wct.setBounds(token, mCurrentDisplayBounds);
309 t.setPosition(leash, mOffsetX, mOffsetY);
310 t.setWindowCrop(leash, mCurrentDisplayBounds.width(), mCurrentDisplayBounds.height());
311 }
312
313 @VisibleForTesting
314 void applyTransaction(WindowContainerTransaction wct, SurfaceControl.Transaction t) {
315 applyTransaction(wct);
316 t.apply();
317 }
318
319 @VisibleForTesting
320 int getStatusBarHeight() {
321 return SystemBarUtils.getStatusBarHeight(mContext);
322 }
323
324 void dump(@NonNull PrintWriter pw) {
325 final String prefix = " ";
326 pw.print(TAG);
327 pw.println(" states: ");
328 synchronized (this) {
329 pw.print(prefix);
330 pw.print("mDisplayAreaMap=");
331 pw.println(mDisplayAreaMap);
332 }
333 pw.print(prefix);
334 pw.print("getDisplayBoundsOfNaturalOrientation()=");
335 pw.println(getDisplayBoundsOfNaturalOrientation());
336 pw.print(prefix);
337 pw.print("mDefaultDisplayBounds=");
338 pw.println(mDefaultDisplayBounds);
339 pw.print(prefix);
340 pw.print("mCurrentDisplayBounds=");
341 pw.println(mCurrentDisplayBounds);
342 pw.print(prefix);
343 pw.print("mDefaultCutoutInsets=");
344 pw.println(mDefaultCutoutInsets);
345 pw.print(prefix);
346 pw.print("mCurrentCutoutInsets=");
347 pw.println(mCurrentCutoutInsets);
348 pw.print(prefix);
349 pw.print("mRotation=");
350 pw.println(mRotation);
351 pw.print(prefix);
352 pw.print("mStatusBarHeight=");
353 pw.println(mStatusBarHeight);
354 pw.print(prefix);
355 pw.print("mOffsetX=");
356 pw.println(mOffsetX);
357 pw.print(prefix);
358 pw.print("mOffsetY=");
359 pw.println(mOffsetY);
360 }
361 }