blob: 57de7536409fe202364ab8afbbbedf9bd3b6365a [file] [log] [blame]
Jorim Jaggi02886a82016-12-06 09:10:06 -08001/*
2 * Copyright (C) 2016 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 Jaggi30d64f32017-04-07 16:33:17 +020019import static android.graphics.Color.WHITE;
20import static android.graphics.Color.alpha;
Adrian Roos4c864592019-04-10 14:47:57 +020021import static android.view.View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
22import static android.view.View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
Jorim Jaggi30d64f32017-04-07 16:33:17 +020023import static android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
24import static android.view.WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
25import static android.view.WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES;
26import static android.view.WindowManager.LayoutParams.FLAG_LOCAL_FOCUS_MODE;
Jorim Jaggi02886a82016-12-06 09:10:06 -080027import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
28import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
Jorim Jaggi30d64f32017-04-07 16:33:17 +020029import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
30import static android.view.WindowManager.LayoutParams.FLAG_SCALED;
31import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
32import static android.view.WindowManager.LayoutParams.FLAG_SLIPPERY;
33import static android.view.WindowManager.LayoutParams.FLAG_SPLIT_TOUCH;
Jorim Jaggia6aabac2019-03-11 14:23:16 -070034import static android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
35import static android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
Jorim Jaggi30d64f32017-04-07 16:33:17 +020036import static android.view.WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
Jorim Jaggia6aabac2019-03-11 14:23:16 -070037import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_FORCE_DRAW_BAR_BACKGROUNDS;
Jorim Jaggi02886a82016-12-06 09:10:06 -080038import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Vishnu Naire86bd982018-11-28 13:23:17 -080039
Jorim Jaggi30d64f32017-04-07 16:33:17 +020040import static com.android.internal.policy.DecorView.NAVIGATION_BAR_COLOR_VIEW_ATTRIBUTES;
41import static com.android.internal.policy.DecorView.STATUS_BAR_COLOR_VIEW_ATTRIBUTES;
42import static com.android.internal.policy.DecorView.getColorViewLeftInset;
43import static com.android.internal.policy.DecorView.getColorViewTopInset;
44import static com.android.internal.policy.DecorView.getNavigationBarRect;
Adrian Roosb125e0b2019-10-02 14:55:14 +020045import static com.android.server.wm.ProtoLogGroup.WM_DEBUG_STARTING_WINDOW;
Jorim Jaggi02886a82016-12-06 09:10:06 -080046import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
47import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
48
Jorim Jaggid635a4a2017-05-03 15:21:26 +020049import android.annotation.Nullable;
Jorim Jaggi829b9cd2017-01-23 16:20:53 +010050import android.app.ActivityManager.TaskDescription;
Jorim Jaggi30d64f32017-04-07 16:33:17 +020051import android.app.ActivityManager.TaskSnapshot;
Jorim Jaggid635a4a2017-05-03 15:21:26 +020052import android.app.ActivityThread;
53import android.content.Context;
Jorim Jaggi02886a82016-12-06 09:10:06 -080054import android.graphics.Canvas;
Winson Chung51f42d22018-02-01 14:59:38 -080055import android.graphics.Color;
Jorim Jaggi02886a82016-12-06 09:10:06 -080056import android.graphics.GraphicBuffer;
Jorim Jaggi829b9cd2017-01-23 16:20:53 +010057import android.graphics.Paint;
Valerie Hau30360552020-01-14 16:12:01 -080058import android.graphics.Point;
Jorim Jaggi02886a82016-12-06 09:10:06 -080059import android.graphics.Rect;
60import android.os.Handler;
Jorim Jaggi829b9cd2017-01-23 16:20:53 +010061import android.os.Looper;
Jorim Jaggi02886a82016-12-06 09:10:06 -080062import android.os.Message;
63import android.os.RemoteException;
Jorim Jaggi30d64f32017-04-07 16:33:17 +020064import android.os.SystemClock;
Andrii Kulian44607962017-03-16 11:06:24 -070065import android.util.MergedConfiguration;
Jorim Jaggi02886a82016-12-06 09:10:06 -080066import android.util.Slog;
Adrian Roos5c6b6222017-11-07 17:36:10 +010067import android.view.DisplayCutout;
Jorim Jaggi02886a82016-12-06 09:10:06 -080068import android.view.IWindowSession;
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +000069import android.view.InsetsState;
Jorim Jaggi02886a82016-12-06 09:10:06 -080070import android.view.Surface;
Jorim Jaggi30d64f32017-04-07 16:33:17 +020071import android.view.SurfaceControl;
72import android.view.SurfaceSession;
Jorim Jaggi02886a82016-12-06 09:10:06 -080073import android.view.View;
74import android.view.ViewGroup.LayoutParams;
Tiger Huang4a7835f2019-11-06 00:07:56 +080075import android.view.ViewRootImpl;
Jorim Jaggi02886a82016-12-06 09:10:06 -080076import android.view.WindowManager;
77import android.view.WindowManagerGlobal;
Jorim Jaggi02886a82016-12-06 09:10:06 -080078
Jorim Jaggi30d64f32017-04-07 16:33:17 +020079import com.android.internal.R;
Jorim Jaggi829b9cd2017-01-23 16:20:53 +010080import com.android.internal.annotations.VisibleForTesting;
Jorim Jaggi30d64f32017-04-07 16:33:17 +020081import com.android.internal.policy.DecorView;
Jorim Jaggi02886a82016-12-06 09:10:06 -080082import com.android.internal.view.BaseIWindow;
Adrian Roose99bc052017-11-20 17:55:31 +010083import com.android.server.policy.WindowManagerPolicy.StartingSurface;
Adrian Roosb125e0b2019-10-02 14:55:14 +020084import com.android.server.protolog.common.ProtoLog;
Jorim Jaggi02886a82016-12-06 09:10:06 -080085
86/**
87 * This class represents a starting window that shows a snapshot.
88 * <p>
89 * DO NOT HOLD THE WINDOW MANAGER LOCK WHEN CALLING METHODS OF THIS CLASS!
90 */
91class TaskSnapshotSurface implements StartingSurface {
92
Jorim Jaggi30d64f32017-04-07 16:33:17 +020093 private static final long SIZE_MISMATCH_MINIMUM_TIME_MS = 450;
94
95 /**
96 * When creating the starting window, we use the exact same layout flags such that we end up
97 * with a window with the exact same dimensions etc. However, these flags are not used in layout
98 * and might cause other side effects so we exclude them.
99 */
100 private static final int FLAG_INHERIT_EXCLUDES = FLAG_NOT_FOCUSABLE
101 | FLAG_NOT_TOUCHABLE
102 | FLAG_NOT_TOUCH_MODAL
103 | FLAG_ALT_FOCUSABLE_IM
104 | FLAG_NOT_FOCUSABLE
105 | FLAG_HARDWARE_ACCELERATED
106 | FLAG_IGNORE_CHEEK_PRESSES
107 | FLAG_LOCAL_FOCUS_MODE
108 | FLAG_SLIPPERY
109 | FLAG_WATCH_OUTSIDE_TOUCH
110 | FLAG_SPLIT_TOUCH
111 | FLAG_SCALED
112 | FLAG_SECURE;
113
Jorim Jaggia6aabac2019-03-11 14:23:16 -0700114 private static final int PRIVATE_FLAG_INHERITS = PRIVATE_FLAG_FORCE_DRAW_BAR_BACKGROUNDS;
Jorim Jaggid2616322017-06-07 12:38:19 -0700115
Jorim Jaggi02886a82016-12-06 09:10:06 -0800116 private static final String TAG = TAG_WITH_CLASS_NAME ? "SnapshotStartingWindow" : TAG_WM;
117 private static final int MSG_REPORT_DRAW = 0;
118 private static final String TITLE_FORMAT = "SnapshotStartingWindow for taskId=%s";
Valerie Hau30360552020-01-14 16:12:01 -0800119 private static final Point sSurfaceSize = new Point(); //tmp var for unused relayout param
Jorim Jaggi02886a82016-12-06 09:10:06 -0800120 private final Window mWindow;
121 private final Surface mSurface;
Robert Carr5fea55b2018-12-10 13:05:52 -0800122 private SurfaceControl mSurfaceControl;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200123 private SurfaceControl mChildSurfaceControl;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800124 private final IWindowSession mSession;
125 private final WindowManagerService mService;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200126 private final Rect mTaskBounds;
127 private final Rect mStableInsets = new Rect();
128 private final Rect mContentInsets = new Rect();
129 private final Rect mFrame = new Rect();
Jorim Jaggi4448e1e2017-05-16 22:26:02 +0200130 private TaskSnapshot mSnapshot;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200131 private final CharSequence mTitle;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800132 private boolean mHasDrawn;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200133 private long mShownTime;
134 private final Handler mHandler;
135 private boolean mSizeMismatch;
136 private final Paint mBackgroundPaint = new Paint();
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200137 private final int mStatusBarColor;
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200138 @VisibleForTesting final SystemBarBackgroundPainter mSystemBarBackgroundPainter;
Jorim Jaggi38d44ec2017-06-14 16:04:59 -0700139 private final int mOrientationOnCreation;
chaviw619da692019-06-10 15:39:40 -0700140 private final SurfaceControl.Transaction mTransaction;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800141
Garfield Tane8d84ab2019-10-11 09:49:40 -0700142 static TaskSnapshotSurface create(WindowManagerService service, ActivityRecord activity,
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200143 TaskSnapshot snapshot) {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800144
145 final WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
146 final Window window = new Window();
147 final IWindowSession session = WindowManagerGlobal.getWindowSession();
148 window.setSession(session);
Robert Carr5fea55b2018-12-10 13:05:52 -0800149 final SurfaceControl surfaceControl = new SurfaceControl();
Jorim Jaggi02886a82016-12-06 09:10:06 -0800150 final Rect tmpRect = new Rect();
Adrian Roos5c6b6222017-11-07 17:36:10 +0100151 final DisplayCutout.ParcelableWrapper tmpCutout = new DisplayCutout.ParcelableWrapper();
Jorim Jaggi02886a82016-12-06 09:10:06 -0800152 final Rect tmpFrame = new Rect();
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200153 final Rect taskBounds;
154 final Rect tmpContentInsets = new Rect();
155 final Rect tmpStableInsets = new Rect();
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200156 final InsetsState mTmpInsetsState = new InsetsState();
Andrii Kulian44607962017-03-16 11:06:24 -0700157 final MergedConfiguration tmpMergedConfiguration = new MergedConfiguration();
Adrian Roos4c864592019-04-10 14:47:57 +0200158 final TaskDescription taskDescription = new TaskDescription();
159 taskDescription.setBackgroundColor(WHITE);
Riddle Hsu84bc3d72019-12-19 19:24:07 +0800160 final WindowState topFullscreenOpaqueWindow;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200161 final int sysUiVis;
162 final int windowFlags;
163 final int windowPrivateFlags;
Jorim Jaggi38d44ec2017-06-14 16:04:59 -0700164 final int currentOrientation;
Wale Ogunwaledb485de2018-10-29 09:47:07 -0700165 synchronized (service.mGlobalLock) {
Garfield Tane8d84ab2019-10-11 09:49:40 -0700166 final WindowState mainWindow = activity.findMainWindow();
167 final Task task = activity.getTask();
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +0200168 if (task == null) {
Garfield Tane8d84ab2019-10-11 09:49:40 -0700169 Slog.w(TAG, "TaskSnapshotSurface.create: Failed to find task for activity="
170 + activity);
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +0200171 return null;
172 }
Garfield Tane8d84ab2019-10-11 09:49:40 -0700173 final ActivityRecord topFullscreenActivity =
174 activity.getTask().getTopFullscreenActivity();
175 if (topFullscreenActivity == null) {
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +0200176 Slog.w(TAG, "TaskSnapshotSurface.create: Failed to find top fullscreen for task="
177 + task);
178 return null;
179 }
Riddle Hsu84bc3d72019-12-19 19:24:07 +0800180 topFullscreenOpaqueWindow = topFullscreenActivity.getTopFullscreenOpaqueWindow();
181 if (mainWindow == null || topFullscreenOpaqueWindow == null) {
Garfield Tane8d84ab2019-10-11 09:49:40 -0700182 Slog.w(TAG, "TaskSnapshotSurface.create: Failed to find main window for activity="
183 + activity);
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200184 return null;
185 }
Riddle Hsu84bc3d72019-12-19 19:24:07 +0800186 sysUiVis = topFullscreenOpaqueWindow.getSystemUiVisibility();
187 windowFlags = topFullscreenOpaqueWindow.getAttrs().flags;
188 windowPrivateFlags = topFullscreenOpaqueWindow.getAttrs().privateFlags;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200189
Jorim Jaggi90b3c472018-04-06 16:02:07 +0200190 layoutParams.packageName = mainWindow.getAttrs().packageName;
191 layoutParams.windowAnimations = mainWindow.getAttrs().windowAnimations;
Winson Chungbe9be7f2017-06-28 10:55:22 -0700192 layoutParams.dimAmount = mainWindow.getAttrs().dimAmount;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800193 layoutParams.type = TYPE_APPLICATION_STARTING;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200194 layoutParams.format = snapshot.getSnapshot().getFormat();
195 layoutParams.flags = (windowFlags & ~FLAG_INHERIT_EXCLUDES)
Jorim Jaggi02886a82016-12-06 09:10:06 -0800196 | FLAG_NOT_FOCUSABLE
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200197 | FLAG_NOT_TOUCHABLE;
Wale Ogunwale01ad4342017-06-30 07:07:01 -0700198 layoutParams.privateFlags = windowPrivateFlags & PRIVATE_FLAG_INHERITS;
Garfield Tane8d84ab2019-10-11 09:49:40 -0700199 layoutParams.token = activity.token;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800200 layoutParams.width = LayoutParams.MATCH_PARENT;
201 layoutParams.height = LayoutParams.MATCH_PARENT;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200202 layoutParams.systemUiVisibility = sysUiVis;
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +0200203 layoutParams.setTitle(String.format(TITLE_FORMAT, task.mTaskId));
Bryce Lee6d410262017-02-28 15:30:17 -0800204
Adrian Roos4c864592019-04-10 14:47:57 +0200205 final TaskDescription td = task.getTaskDescription();
206 if (td != null) {
Mehdi Alizadehc5e29902019-11-14 17:18:44 -0800207 taskDescription.copyFromPreserveHiddenFields(td);
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100208 }
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +0200209 taskBounds = new Rect();
210 task.getBounds(taskBounds);
Riddle Hsu84bc3d72019-12-19 19:24:07 +0800211 currentOrientation = topFullscreenOpaqueWindow.getConfiguration().orientation;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800212 }
213 try {
214 final int res = session.addToDisplay(window, window.mSeq, layoutParams,
Garfield Tane8d84ab2019-10-11 09:49:40 -0700215 View.GONE, activity.getDisplayContent().getDisplayId(), tmpFrame, tmpRect, tmpRect,
Jorim Jaggif081f062019-10-24 16:24:54 +0200216 tmpCutout, null, mTmpInsetsState);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800217 if (res < 0) {
218 Slog.w(TAG, "Failed to add snapshot starting window res=" + res);
219 return null;
220 }
221 } catch (RemoteException e) {
222 // Local call.
223 }
224 final TaskSnapshotSurface snapshotSurface = new TaskSnapshotSurface(service, window,
Adrian Roos4c864592019-04-10 14:47:57 +0200225 surfaceControl, snapshot, layoutParams.getTitle(), taskDescription, sysUiVis,
226 windowFlags, windowPrivateFlags, taskBounds,
Jorim Jaggi0dd0cf92019-12-27 15:17:44 +0100227 currentOrientation, topFullscreenOpaqueWindow.getRequestedInsetsState());
Jorim Jaggi02886a82016-12-06 09:10:06 -0800228 window.setOuter(snapshotSurface);
229 try {
chaviwbe43ac82018-04-04 15:14:49 -0700230 session.relayout(window, window.mSeq, layoutParams, -1, -1, View.VISIBLE, 0, -1,
Jorim Jaggif081f062019-10-24 16:24:54 +0200231 tmpFrame, tmpContentInsets, tmpRect, tmpStableInsets, tmpRect,
Valerie Hau30360552020-01-14 16:12:01 -0800232 tmpCutout, tmpMergedConfiguration, surfaceControl, mTmpInsetsState,
233 sSurfaceSize);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800234 } catch (RemoteException e) {
235 // Local call.
236 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200237 snapshotSurface.setFrames(tmpFrame, tmpContentInsets, tmpStableInsets);
238 snapshotSurface.drawSnapshot();
Jorim Jaggi02886a82016-12-06 09:10:06 -0800239 return snapshotSurface;
240 }
241
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100242 @VisibleForTesting
Robert Carr5fea55b2018-12-10 13:05:52 -0800243 TaskSnapshotSurface(WindowManagerService service, Window window, SurfaceControl surfaceControl,
Adrian Roos4c864592019-04-10 14:47:57 +0200244 TaskSnapshot snapshot, CharSequence title, TaskDescription taskDescription,
245 int sysUiVis, int windowFlags, int windowPrivateFlags, Rect taskBounds,
Tiger Huang4a7835f2019-11-06 00:07:56 +0800246 int currentOrientation, InsetsState insetsState) {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800247 mService = service;
Vishnu Nair33197392019-08-30 10:29:37 -0700248 mSurface = service.mSurfaceFactory.get();
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200249 mHandler = new Handler(mService.mH.getLooper());
Jorim Jaggi02886a82016-12-06 09:10:06 -0800250 mSession = WindowManagerGlobal.getWindowSession();
251 mWindow = window;
Robert Carr5fea55b2018-12-10 13:05:52 -0800252 mSurfaceControl = surfaceControl;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200253 mSnapshot = snapshot;
254 mTitle = title;
Adrian Roos4c864592019-04-10 14:47:57 +0200255 int backgroundColor = taskDescription.getBackgroundColor();
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200256 mBackgroundPaint.setColor(backgroundColor != 0 ? backgroundColor : WHITE);
257 mTaskBounds = taskBounds;
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200258 mSystemBarBackgroundPainter = new SystemBarBackgroundPainter(windowFlags,
Tiger Huang4a7835f2019-11-06 00:07:56 +0800259 windowPrivateFlags, sysUiVis, taskDescription, 1f, insetsState);
Adrian Roos4c864592019-04-10 14:47:57 +0200260 mStatusBarColor = taskDescription.getStatusBarColor();
Jorim Jaggi38d44ec2017-06-14 16:04:59 -0700261 mOrientationOnCreation = currentOrientation;
chaviw619da692019-06-10 15:39:40 -0700262 mTransaction = mService.mTransactionFactory.get();
Jorim Jaggi02886a82016-12-06 09:10:06 -0800263 }
264
265 @Override
266 public void remove() {
Wale Ogunwaledb485de2018-10-29 09:47:07 -0700267 synchronized (mService.mGlobalLock) {
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200268 final long now = SystemClock.uptimeMillis();
269 if (mSizeMismatch && now - mShownTime < SIZE_MISMATCH_MINIMUM_TIME_MS) {
270 mHandler.postAtTime(this::remove, mShownTime + SIZE_MISMATCH_MINIMUM_TIME_MS);
Adrian Roosb125e0b2019-10-02 14:55:14 +0200271 ProtoLog.v(WM_DEBUG_STARTING_WINDOW,
272 "Defer removing snapshot surface in %dms", (now - mShownTime));
273
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200274 return;
275 }
276 }
Jorim Jaggi02886a82016-12-06 09:10:06 -0800277 try {
Adrian Roosb125e0b2019-10-02 14:55:14 +0200278 ProtoLog.v(WM_DEBUG_STARTING_WINDOW, "Removing snapshot surface");
Jorim Jaggi02886a82016-12-06 09:10:06 -0800279 mSession.remove(mWindow);
280 } catch (RemoteException e) {
281 // Local call.
282 }
283 }
284
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200285 @VisibleForTesting
286 void setFrames(Rect frame, Rect contentInsets, Rect stableInsets) {
287 mFrame.set(frame);
288 mContentInsets.set(contentInsets);
289 mStableInsets.set(stableInsets);
290 mSizeMismatch = (mFrame.width() != mSnapshot.getSnapshot().getWidth()
291 || mFrame.height() != mSnapshot.getSnapshot().getHeight());
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200292 mSystemBarBackgroundPainter.setInsets(contentInsets, stableInsets);
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200293 }
294
295 private void drawSnapshot() {
Robert Carr5fea55b2018-12-10 13:05:52 -0800296 mSurface.copyFrom(mSurfaceControl);
297
Adrian Roosb125e0b2019-10-02 14:55:14 +0200298 ProtoLog.v(WM_DEBUG_STARTING_WINDOW, "Drawing snapshot surface sizeMismatch=%b",
299 mSizeMismatch);
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200300 if (mSizeMismatch) {
301 // The dimensions of the buffer and the window don't match, so attaching the buffer
302 // will fail. Better create a child window with the exact dimensions and fill the parent
303 // window with the background color!
Peiyong Linccc06b62019-06-25 17:31:09 -0700304 drawSizeMismatchSnapshot();
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200305 } else {
Peiyong Linccc06b62019-06-25 17:31:09 -0700306 drawSizeMatchSnapshot();
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200307 }
Wale Ogunwaledb485de2018-10-29 09:47:07 -0700308 synchronized (mService.mGlobalLock) {
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200309 mShownTime = SystemClock.uptimeMillis();
Jorim Jaggi39367cf2017-03-13 16:41:13 +0100310 mHasDrawn = true;
Jorim Jaggi39367cf2017-03-13 16:41:13 +0100311 }
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200312 reportDrawn();
Jorim Jaggi4448e1e2017-05-16 22:26:02 +0200313
314 // In case window manager leaks us, make sure we don't retain the snapshot.
315 mSnapshot = null;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200316 }
317
Peiyong Linccc06b62019-06-25 17:31:09 -0700318 private void drawSizeMatchSnapshot() {
319 mSurface.attachAndQueueBufferWithColorSpace(mSnapshot.getSnapshot(),
320 mSnapshot.getColorSpace());
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200321 mSurface.release();
322 }
323
Peiyong Linccc06b62019-06-25 17:31:09 -0700324 private void drawSizeMismatchSnapshot() {
Shibin George9696bd32018-02-26 20:29:44 +0530325 if (!mSurface.isValid()) {
326 throw new IllegalStateException("mSurface does not hold a valid surface.");
327 }
Peiyong Linccc06b62019-06-25 17:31:09 -0700328 final GraphicBuffer buffer = mSnapshot.getSnapshot();
Robert Carr5fea55b2018-12-10 13:05:52 -0800329 final SurfaceSession session = new SurfaceSession();
Winson Chung357deda2019-06-23 13:35:41 -0700330 // We consider nearly matched dimensions as there can be rounding errors and the user won't
331 // notice very minute differences from scaling one dimension more than the other
332 final boolean aspectRatioMismatch = Math.abs(
333 ((float) buffer.getWidth() / buffer.getHeight())
334 - ((float) mFrame.width() / mFrame.height())) > 0.01f;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200335
336 // Keep a reference to it such that it doesn't get destroyed when finalized.
Vishnu Nair33197392019-08-30 10:29:37 -0700337 mChildSurfaceControl = mService.mSurfaceControlFactory.apply(session)
Robert Carre625fcf2017-09-01 12:36:28 -0700338 .setName(mTitle + " - task-snapshot-surface")
Vishnu Naire86bd982018-11-28 13:23:17 -0800339 .setBufferSize(buffer.getWidth(), buffer.getHeight())
Robert Carre625fcf2017-09-01 12:36:28 -0700340 .setFormat(buffer.getFormat())
Robert Carr5fea55b2018-12-10 13:05:52 -0800341 .setParent(mSurfaceControl)
Robert Carre625fcf2017-09-01 12:36:28 -0700342 .build();
Vishnu Nair33197392019-08-30 10:29:37 -0700343 Surface surface = mService.mSurfaceFactory.get();
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200344 surface.copyFrom(mChildSurfaceControl);
345
Winson Chung357deda2019-06-23 13:35:41 -0700346 final Rect frame;
chaviw619da692019-06-10 15:39:40 -0700347 // We can just show the surface here as it will still be hidden as the parent is
348 // still hidden.
349 mTransaction.show(mChildSurfaceControl);
350 if (aspectRatioMismatch) {
351 // Clip off ugly navigation bar.
352 final Rect crop = calculateSnapshotCrop();
353 frame = calculateSnapshotFrame(crop);
354 mTransaction.setWindowCrop(mChildSurfaceControl, crop);
355 mTransaction.setPosition(mChildSurfaceControl, frame.left, frame.top);
356 } else {
357 frame = null;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200358 }
chaviw619da692019-06-10 15:39:40 -0700359
360 // Scale the mismatch dimensions to fill the task bounds
361 final float scale = 1 / mSnapshot.getScale();
362 mTransaction.setMatrix(mChildSurfaceControl, scale, 0, 0, scale);
363 mTransaction.apply();
Peiyong Linccc06b62019-06-25 17:31:09 -0700364 surface.attachAndQueueBufferWithColorSpace(buffer, mSnapshot.getColorSpace());
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200365 surface.release();
366
Winson Chung357deda2019-06-23 13:35:41 -0700367 if (aspectRatioMismatch) {
368 final Canvas c = mSurface.lockCanvas(null);
369 drawBackgroundAndBars(c, frame);
370 mSurface.unlockCanvasAndPost(c);
371 mSurface.release();
372 }
Jorim Jaggi02886a82016-12-06 09:10:06 -0800373 }
374
Matthew Ngcb7ac672017-07-21 17:27:42 -0700375 /**
376 * Calculates the snapshot crop in snapshot coordinate space.
377 *
378 * @return crop rect in snapshot coordinate space.
379 */
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100380 @VisibleForTesting
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200381 Rect calculateSnapshotCrop() {
382 final Rect rect = new Rect();
383 rect.set(0, 0, mSnapshot.getSnapshot().getWidth(), mSnapshot.getSnapshot().getHeight());
384 final Rect insets = mSnapshot.getContentInsets();
385
386 // Let's remove all system decorations except the status bar, but only if the task is at the
387 // very top of the screen.
Adrian Roos98a146d2017-11-29 16:39:44 +0100388 final boolean isTop = mTaskBounds.top == 0 && mFrame.top == 0;
Matthew Ngcb7ac672017-07-21 17:27:42 -0700389 rect.inset((int) (insets.left * mSnapshot.getScale()),
Adrian Roos98a146d2017-11-29 16:39:44 +0100390 isTop ? 0 : (int) (insets.top * mSnapshot.getScale()),
Matthew Ngcb7ac672017-07-21 17:27:42 -0700391 (int) (insets.right * mSnapshot.getScale()),
392 (int) (insets.bottom * mSnapshot.getScale()));
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200393 return rect;
394 }
395
Matthew Ngcb7ac672017-07-21 17:27:42 -0700396 /**
397 * Calculates the snapshot frame in window coordinate space from crop.
398 *
399 * @param crop rect that is in snapshot coordinate space.
400 */
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200401 @VisibleForTesting
402 Rect calculateSnapshotFrame(Rect crop) {
403 final Rect frame = new Rect(crop);
Matthew Ngcb7ac672017-07-21 17:27:42 -0700404 final float scale = mSnapshot.getScale();
405
406 // Rescale the frame from snapshot to window coordinate space
407 frame.scale(1 / scale);
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200408
409 // By default, offset it to to top/left corner
Matthew Ngcb7ac672017-07-21 17:27:42 -0700410 frame.offsetTo((int) (-crop.left / scale), (int) (-crop.top / scale));
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200411
412 // However, we also need to make space for the navigation bar on the left side.
413 final int colorViewLeftInset = getColorViewLeftInset(mStableInsets.left,
414 mContentInsets.left);
415 frame.offset(colorViewLeftInset, 0);
416 return frame;
417 }
418
419 @VisibleForTesting
420 void drawBackgroundAndBars(Canvas c, Rect frame) {
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200421 final int statusBarHeight = mSystemBarBackgroundPainter.getStatusBarColorViewHeight();
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200422 final boolean fillHorizontally = c.getWidth() > frame.right;
423 final boolean fillVertically = c.getHeight() > frame.bottom;
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100424 if (fillHorizontally) {
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200425 c.drawRect(frame.right, alpha(mStatusBarColor) == 0xFF ? statusBarHeight : 0,
426 c.getWidth(), fillVertically
427 ? frame.bottom
428 : c.getHeight(),
429 mBackgroundPaint);
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100430 }
431 if (fillVertically) {
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200432 c.drawRect(0, frame.bottom, c.getWidth(), c.getHeight(), mBackgroundPaint);
433 }
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200434 mSystemBarBackgroundPainter.drawDecors(c, frame);
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100435 }
436
Jorim Jaggi02886a82016-12-06 09:10:06 -0800437 private void reportDrawn() {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800438 try {
Vishnu Nairf7645aa2019-06-18 11:14:01 -0700439 mSession.finishDrawing(mWindow, null /* postDrawTransaction */);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800440 } catch (RemoteException e) {
441 // Local call.
442 }
443 }
444
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100445 private static Handler sHandler = new Handler(Looper.getMainLooper()) {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800446
447 @Override
448 public void handleMessage(Message msg) {
449 switch (msg.what) {
450 case MSG_REPORT_DRAW:
451 final boolean hasDrawn;
452 final TaskSnapshotSurface surface = (TaskSnapshotSurface) msg.obj;
Wale Ogunwaledb485de2018-10-29 09:47:07 -0700453 synchronized (surface.mService.mGlobalLock) {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800454 hasDrawn = surface.mHasDrawn;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800455 }
456 if (hasDrawn) {
457 surface.reportDrawn();
458 }
459 break;
460 }
461 }
462 };
463
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200464 @VisibleForTesting
465 static class Window extends BaseIWindow {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800466
467 private TaskSnapshotSurface mOuter;
Jorim Jaggi38d44ec2017-06-14 16:04:59 -0700468
Jorim Jaggi02886a82016-12-06 09:10:06 -0800469 public void setOuter(TaskSnapshotSurface outer) {
470 mOuter = outer;
471 }
472
473 @Override
Jorim Jaggif081f062019-10-24 16:24:54 +0200474 public void resized(Rect frame, Rect contentInsets, Rect visibleInsets,
475 Rect stableInsets, boolean reportDraw,
Andrii Kulian44607962017-03-16 11:06:24 -0700476 MergedConfiguration mergedConfiguration, Rect backDropFrame, boolean forceLayout,
Brad Stenninge0573692019-03-11 13:52:46 -0700477 boolean alwaysConsumeSystemBars, int displayId,
Adrian Roos5c6b6222017-11-07 17:36:10 +0100478 DisplayCutout.ParcelableWrapper displayCutout) {
Jorim Jaggi38d44ec2017-06-14 16:04:59 -0700479 if (mergedConfiguration != null && mOuter != null
480 && mOuter.mOrientationOnCreation
481 != mergedConfiguration.getMergedConfiguration().orientation) {
482
483 // The orientation of the screen is changing. We better remove the snapshot ASAP as
484 // we are going to wait on the new window in any case to unfreeze the screen, and
485 // the starting window is not needed anymore.
486 sHandler.post(mOuter::remove);
487 }
Jorim Jaggi02886a82016-12-06 09:10:06 -0800488 if (reportDraw) {
489 sHandler.obtainMessage(MSG_REPORT_DRAW, mOuter).sendToTarget();
490 }
491 }
492 }
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200493
494 /**
495 * Helper class to draw the background of the system bars in regions the task snapshot isn't
496 * filling the window.
497 */
498 static class SystemBarBackgroundPainter {
499
500 private final Rect mContentInsets = new Rect();
501 private final Rect mStableInsets = new Rect();
502 private final Paint mStatusBarPaint = new Paint();
503 private final Paint mNavigationBarPaint = new Paint();
504 private final int mStatusBarColor;
505 private final int mNavigationBarColor;
506 private final int mWindowFlags;
507 private final int mWindowPrivateFlags;
508 private final int mSysUiVis;
Winson Chungffde2ea2019-06-17 17:19:13 -0700509 private final float mScale;
Tiger Huang4a7835f2019-11-06 00:07:56 +0800510 private final InsetsState mInsetsState;
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200511
Winson Chungffde2ea2019-06-17 17:19:13 -0700512 SystemBarBackgroundPainter(int windowFlags, int windowPrivateFlags, int sysUiVis,
Tiger Huang4a7835f2019-11-06 00:07:56 +0800513 TaskDescription taskDescription, float scale, InsetsState insetsState) {
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200514 mWindowFlags = windowFlags;
515 mWindowPrivateFlags = windowPrivateFlags;
516 mSysUiVis = sysUiVis;
Winson Chungffde2ea2019-06-17 17:19:13 -0700517 mScale = scale;
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200518 final Context context = ActivityThread.currentActivityThread().getSystemUiContext();
Jorim Jaggia6aabac2019-03-11 14:23:16 -0700519 final int semiTransparent = context.getColor(
520 R.color.system_bar_background_semi_transparent);
521 mStatusBarColor = DecorView.calculateBarColor(windowFlags, FLAG_TRANSLUCENT_STATUS,
Adrian Roos4c864592019-04-10 14:47:57 +0200522 semiTransparent, taskDescription.getStatusBarColor(), sysUiVis,
523 SYSTEM_UI_FLAG_LIGHT_STATUS_BAR,
524 taskDescription.getEnsureStatusBarContrastWhenTransparent());
Jorim Jaggia6aabac2019-03-11 14:23:16 -0700525 mNavigationBarColor = DecorView.calculateBarColor(windowFlags,
Adrian Roos4c864592019-04-10 14:47:57 +0200526 FLAG_TRANSLUCENT_NAVIGATION, semiTransparent,
527 taskDescription.getNavigationBarColor(), sysUiVis,
528 SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR,
529 taskDescription.getEnsureNavigationBarContrastWhenTransparent()
530 && context.getResources().getBoolean(R.bool.config_navBarNeedsScrim));
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200531 mStatusBarPaint.setColor(mStatusBarColor);
Adrian Roos4c864592019-04-10 14:47:57 +0200532 mNavigationBarPaint.setColor(mNavigationBarColor);
Tiger Huang4a7835f2019-11-06 00:07:56 +0800533 mInsetsState = insetsState;
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200534 }
535
536 void setInsets(Rect contentInsets, Rect stableInsets) {
537 mContentInsets.set(contentInsets);
538 mStableInsets.set(stableInsets);
539 }
540
541 int getStatusBarColorViewHeight() {
Jorim Jaggia6aabac2019-03-11 14:23:16 -0700542 final boolean forceBarBackground =
543 (mWindowPrivateFlags & PRIVATE_FLAG_FORCE_DRAW_BAR_BACKGROUNDS) != 0;
Tiger Huang4a7835f2019-11-06 00:07:56 +0800544 if (ViewRootImpl.sNewInsetsMode != ViewRootImpl.NEW_INSETS_MODE_FULL
545 ? STATUS_BAR_COLOR_VIEW_ATTRIBUTES.isVisible(
546 mSysUiVis, mStatusBarColor, mWindowFlags, forceBarBackground)
547 : STATUS_BAR_COLOR_VIEW_ATTRIBUTES.isVisible(
548 mInsetsState, mStatusBarColor, mWindowFlags, forceBarBackground)) {
Winson Chungffde2ea2019-06-17 17:19:13 -0700549 return (int) (getColorViewTopInset(mStableInsets.top, mContentInsets.top) * mScale);
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200550 } else {
551 return 0;
552 }
553 }
554
555 private boolean isNavigationBarColorViewVisible() {
Jorim Jaggia6aabac2019-03-11 14:23:16 -0700556 final boolean forceBarBackground =
557 (mWindowPrivateFlags & PRIVATE_FLAG_FORCE_DRAW_BAR_BACKGROUNDS) != 0;
Tiger Huang4a7835f2019-11-06 00:07:56 +0800558 return ViewRootImpl.sNewInsetsMode != ViewRootImpl.NEW_INSETS_MODE_FULL
559 ? NAVIGATION_BAR_COLOR_VIEW_ATTRIBUTES.isVisible(
560 mSysUiVis, mNavigationBarColor, mWindowFlags, forceBarBackground)
561 : NAVIGATION_BAR_COLOR_VIEW_ATTRIBUTES.isVisible(
562 mInsetsState, mNavigationBarColor, mWindowFlags, forceBarBackground);
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200563 }
564
565 void drawDecors(Canvas c, @Nullable Rect alreadyDrawnFrame) {
566 drawStatusBarBackground(c, alreadyDrawnFrame, getStatusBarColorViewHeight());
567 drawNavigationBarBackground(c);
568 }
569
570 @VisibleForTesting
571 void drawStatusBarBackground(Canvas c, @Nullable Rect alreadyDrawnFrame,
572 int statusBarHeight) {
Winson Chung51f42d22018-02-01 14:59:38 -0800573 if (statusBarHeight > 0 && Color.alpha(mStatusBarColor) != 0
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200574 && (alreadyDrawnFrame == null || c.getWidth() > alreadyDrawnFrame.right)) {
Winson Chungffde2ea2019-06-17 17:19:13 -0700575 final int rightInset = (int) (DecorView.getColorViewRightInset(mStableInsets.right,
576 mContentInsets.right) * mScale);
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200577 final int left = alreadyDrawnFrame != null ? alreadyDrawnFrame.right : 0;
578 c.drawRect(left, 0, c.getWidth() - rightInset, statusBarHeight, mStatusBarPaint);
579 }
580 }
581
582 @VisibleForTesting
583 void drawNavigationBarBackground(Canvas c) {
584 final Rect navigationBarRect = new Rect();
585 getNavigationBarRect(c.getWidth(), c.getHeight(), mStableInsets, mContentInsets,
Winson Chungffde2ea2019-06-17 17:19:13 -0700586 navigationBarRect, mScale);
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200587 final boolean visible = isNavigationBarColorViewVisible();
Winson Chung51f42d22018-02-01 14:59:38 -0800588 if (visible && Color.alpha(mNavigationBarColor) != 0 && !navigationBarRect.isEmpty()) {
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200589 c.drawRect(navigationBarRect, mNavigationBarPaint);
590 }
591 }
592 }
Jorim Jaggi02886a82016-12-06 09:10:06 -0800593}