blob: 24cd7d1fa5289622446158c8ba5c21ec70e87d01 [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;
Peter Kalauskasfe0a4132020-01-31 16:50:42 -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;
Peter Kalauskasfe0a4132020-01-31 16:50:42 -080057import android.graphics.Matrix;
Jorim Jaggi829b9cd2017-01-23 16:20:53 +010058import android.graphics.Paint;
Valerie Hau30360552020-01-14 16:12:01 -080059import android.graphics.Point;
Jorim Jaggi02886a82016-12-06 09:10:06 -080060import android.graphics.Rect;
Peter Kalauskasfe0a4132020-01-31 16:50:42 -080061import android.graphics.RectF;
Jorim Jaggi02886a82016-12-06 09:10:06 -080062import android.os.Handler;
Jorim Jaggi829b9cd2017-01-23 16:20:53 +010063import android.os.Looper;
Jorim Jaggi02886a82016-12-06 09:10:06 -080064import android.os.Message;
65import android.os.RemoteException;
Jorim Jaggi30d64f32017-04-07 16:33:17 +020066import android.os.SystemClock;
Andrii Kulian44607962017-03-16 11:06:24 -070067import android.util.MergedConfiguration;
Jorim Jaggi02886a82016-12-06 09:10:06 -080068import android.util.Slog;
Adrian Roos5c6b6222017-11-07 17:36:10 +010069import android.view.DisplayCutout;
Jorim Jaggi02886a82016-12-06 09:10:06 -080070import android.view.IWindowSession;
Tiger Huang0426a332020-03-29 01:17:08 +080071import android.view.InsetsSourceControl;
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +000072import android.view.InsetsState;
Jorim Jaggi02886a82016-12-06 09:10:06 -080073import android.view.Surface;
Jorim Jaggi30d64f32017-04-07 16:33:17 +020074import android.view.SurfaceControl;
75import android.view.SurfaceSession;
Jorim Jaggi02886a82016-12-06 09:10:06 -080076import android.view.View;
77import android.view.ViewGroup.LayoutParams;
Tiger Huang4a7835f2019-11-06 00:07:56 +080078import android.view.ViewRootImpl;
Jorim Jaggi02886a82016-12-06 09:10:06 -080079import android.view.WindowManager;
80import android.view.WindowManagerGlobal;
Jorim Jaggi02886a82016-12-06 09:10:06 -080081
Jorim Jaggi30d64f32017-04-07 16:33:17 +020082import com.android.internal.R;
Jorim Jaggi829b9cd2017-01-23 16:20:53 +010083import com.android.internal.annotations.VisibleForTesting;
Jorim Jaggi30d64f32017-04-07 16:33:17 +020084import com.android.internal.policy.DecorView;
Jorim Jaggi02886a82016-12-06 09:10:06 -080085import com.android.internal.view.BaseIWindow;
Adrian Roose99bc052017-11-20 17:55:31 +010086import com.android.server.policy.WindowManagerPolicy.StartingSurface;
Adrian Roosb125e0b2019-10-02 14:55:14 +020087import com.android.server.protolog.common.ProtoLog;
Jorim Jaggi02886a82016-12-06 09:10:06 -080088
89/**
90 * This class represents a starting window that shows a snapshot.
91 * <p>
92 * DO NOT HOLD THE WINDOW MANAGER LOCK WHEN CALLING METHODS OF THIS CLASS!
93 */
94class TaskSnapshotSurface implements StartingSurface {
95
Jorim Jaggi30d64f32017-04-07 16:33:17 +020096 private static final long SIZE_MISMATCH_MINIMUM_TIME_MS = 450;
97
98 /**
99 * When creating the starting window, we use the exact same layout flags such that we end up
100 * with a window with the exact same dimensions etc. However, these flags are not used in layout
101 * and might cause other side effects so we exclude them.
102 */
103 private static final int FLAG_INHERIT_EXCLUDES = FLAG_NOT_FOCUSABLE
104 | FLAG_NOT_TOUCHABLE
105 | FLAG_NOT_TOUCH_MODAL
106 | FLAG_ALT_FOCUSABLE_IM
107 | FLAG_NOT_FOCUSABLE
108 | FLAG_HARDWARE_ACCELERATED
109 | FLAG_IGNORE_CHEEK_PRESSES
110 | FLAG_LOCAL_FOCUS_MODE
111 | FLAG_SLIPPERY
112 | FLAG_WATCH_OUTSIDE_TOUCH
113 | FLAG_SPLIT_TOUCH
114 | FLAG_SCALED
115 | FLAG_SECURE;
116
Jorim Jaggia6aabac2019-03-11 14:23:16 -0700117 private static final int PRIVATE_FLAG_INHERITS = PRIVATE_FLAG_FORCE_DRAW_BAR_BACKGROUNDS;
Jorim Jaggid2616322017-06-07 12:38:19 -0700118
Jorim Jaggi02886a82016-12-06 09:10:06 -0800119 private static final String TAG = TAG_WITH_CLASS_NAME ? "SnapshotStartingWindow" : TAG_WM;
120 private static final int MSG_REPORT_DRAW = 0;
121 private static final String TITLE_FORMAT = "SnapshotStartingWindow for taskId=%s";
Robert Carr2e20bcd2020-01-22 13:32:38 -0800122
123 //tmp vars for unused relayout params
124 private static final Point sTmpSurfaceSize = new Point();
125 private static final SurfaceControl sTmpSurfaceControl = new SurfaceControl();
126
Jorim Jaggi02886a82016-12-06 09:10:06 -0800127 private final Window mWindow;
128 private final Surface mSurface;
Robert Carr5fea55b2018-12-10 13:05:52 -0800129 private SurfaceControl mSurfaceControl;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200130 private SurfaceControl mChildSurfaceControl;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800131 private final IWindowSession mSession;
132 private final WindowManagerService mService;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200133 private final Rect mTaskBounds;
134 private final Rect mStableInsets = new Rect();
135 private final Rect mContentInsets = new Rect();
136 private final Rect mFrame = new Rect();
Jorim Jaggi4448e1e2017-05-16 22:26:02 +0200137 private TaskSnapshot mSnapshot;
Peter Kalauskasfe0a4132020-01-31 16:50:42 -0800138 private final RectF mTmpSnapshotSize = new RectF();
139 private final RectF mTmpDstFrame = new RectF();
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200140 private final CharSequence mTitle;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800141 private boolean mHasDrawn;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200142 private long mShownTime;
143 private final Handler mHandler;
144 private boolean mSizeMismatch;
145 private final Paint mBackgroundPaint = new Paint();
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200146 private final int mStatusBarColor;
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200147 @VisibleForTesting final SystemBarBackgroundPainter mSystemBarBackgroundPainter;
Jorim Jaggi38d44ec2017-06-14 16:04:59 -0700148 private final int mOrientationOnCreation;
chaviw619da692019-06-10 15:39:40 -0700149 private final SurfaceControl.Transaction mTransaction;
Peter Kalauskasfe0a4132020-01-31 16:50:42 -0800150 private final Matrix mSnapshotMatrix = new Matrix();
151 private final float[] mTmpFloat9 = new float[9];
Jorim Jaggi02886a82016-12-06 09:10:06 -0800152
Garfield Tane8d84ab2019-10-11 09:49:40 -0700153 static TaskSnapshotSurface create(WindowManagerService service, ActivityRecord activity,
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200154 TaskSnapshot snapshot) {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800155
156 final WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
157 final Window window = new Window();
158 final IWindowSession session = WindowManagerGlobal.getWindowSession();
159 window.setSession(session);
Robert Carr5fea55b2018-12-10 13:05:52 -0800160 final SurfaceControl surfaceControl = new SurfaceControl();
Jorim Jaggi02886a82016-12-06 09:10:06 -0800161 final Rect tmpRect = new Rect();
Adrian Roos5c6b6222017-11-07 17:36:10 +0100162 final DisplayCutout.ParcelableWrapper tmpCutout = new DisplayCutout.ParcelableWrapper();
Jorim Jaggi02886a82016-12-06 09:10:06 -0800163 final Rect tmpFrame = new Rect();
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200164 final Rect taskBounds;
165 final Rect tmpContentInsets = new Rect();
166 final Rect tmpStableInsets = new Rect();
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200167 final InsetsState mTmpInsetsState = new InsetsState();
Tiger Huang0426a332020-03-29 01:17:08 +0800168 final InsetsSourceControl[] mTempControls = new InsetsSourceControl[0];
Andrii Kulian44607962017-03-16 11:06:24 -0700169 final MergedConfiguration tmpMergedConfiguration = new MergedConfiguration();
Adrian Roos4c864592019-04-10 14:47:57 +0200170 final TaskDescription taskDescription = new TaskDescription();
171 taskDescription.setBackgroundColor(WHITE);
Riddle Hsu84bc3d72019-12-19 19:24:07 +0800172 final WindowState topFullscreenOpaqueWindow;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200173 final int sysUiVis;
174 final int windowFlags;
175 final int windowPrivateFlags;
Jorim Jaggi38d44ec2017-06-14 16:04:59 -0700176 final int currentOrientation;
Wale Ogunwaledb485de2018-10-29 09:47:07 -0700177 synchronized (service.mGlobalLock) {
Garfield Tane8d84ab2019-10-11 09:49:40 -0700178 final WindowState mainWindow = activity.findMainWindow();
179 final Task task = activity.getTask();
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +0200180 if (task == null) {
Garfield Tane8d84ab2019-10-11 09:49:40 -0700181 Slog.w(TAG, "TaskSnapshotSurface.create: Failed to find task for activity="
182 + activity);
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +0200183 return null;
184 }
Garfield Tane8d84ab2019-10-11 09:49:40 -0700185 final ActivityRecord topFullscreenActivity =
186 activity.getTask().getTopFullscreenActivity();
187 if (topFullscreenActivity == null) {
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +0200188 Slog.w(TAG, "TaskSnapshotSurface.create: Failed to find top fullscreen for task="
189 + task);
190 return null;
191 }
Riddle Hsu84bc3d72019-12-19 19:24:07 +0800192 topFullscreenOpaqueWindow = topFullscreenActivity.getTopFullscreenOpaqueWindow();
193 if (mainWindow == null || topFullscreenOpaqueWindow == null) {
Garfield Tane8d84ab2019-10-11 09:49:40 -0700194 Slog.w(TAG, "TaskSnapshotSurface.create: Failed to find main window for activity="
195 + activity);
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200196 return null;
197 }
Riddle Hsu84bc3d72019-12-19 19:24:07 +0800198 sysUiVis = topFullscreenOpaqueWindow.getSystemUiVisibility();
Jorim Jaggie670be72020-01-30 22:18:00 +0100199 WindowManager.LayoutParams attrs = topFullscreenOpaqueWindow.mAttrs;
200 windowFlags = attrs.flags;
201 windowPrivateFlags = attrs.privateFlags;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200202
Jorim Jaggi90b3c472018-04-06 16:02:07 +0200203 layoutParams.packageName = mainWindow.getAttrs().packageName;
204 layoutParams.windowAnimations = mainWindow.getAttrs().windowAnimations;
Winson Chungbe9be7f2017-06-28 10:55:22 -0700205 layoutParams.dimAmount = mainWindow.getAttrs().dimAmount;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800206 layoutParams.type = TYPE_APPLICATION_STARTING;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200207 layoutParams.format = snapshot.getSnapshot().getFormat();
208 layoutParams.flags = (windowFlags & ~FLAG_INHERIT_EXCLUDES)
Jorim Jaggi02886a82016-12-06 09:10:06 -0800209 | FLAG_NOT_FOCUSABLE
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200210 | FLAG_NOT_TOUCHABLE;
Wale Ogunwale01ad4342017-06-30 07:07:01 -0700211 layoutParams.privateFlags = windowPrivateFlags & PRIVATE_FLAG_INHERITS;
Garfield Tane8d84ab2019-10-11 09:49:40 -0700212 layoutParams.token = activity.token;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800213 layoutParams.width = LayoutParams.MATCH_PARENT;
214 layoutParams.height = LayoutParams.MATCH_PARENT;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200215 layoutParams.systemUiVisibility = sysUiVis;
Jorim Jaggie670be72020-01-30 22:18:00 +0100216 layoutParams.insetsFlags.behavior
217 = topFullscreenOpaqueWindow.mAttrs.insetsFlags.behavior;
218 layoutParams.insetsFlags.appearance
219 = topFullscreenOpaqueWindow.mAttrs.insetsFlags.appearance;
shawnlin4d2a2f42020-04-14 14:34:16 +0800220 layoutParams.layoutInDisplayCutoutMode = attrs.layoutInDisplayCutoutMode;
Jorim Jaggie670be72020-01-30 22:18:00 +0100221 layoutParams.setFitInsetsTypes(attrs.getFitInsetsTypes());
222 layoutParams.setFitInsetsSides(attrs.getFitInsetsSides());
223 layoutParams.setFitInsetsIgnoringVisibility(attrs.isFitInsetsIgnoringVisibility());
224
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +0200225 layoutParams.setTitle(String.format(TITLE_FORMAT, task.mTaskId));
Bryce Lee6d410262017-02-28 15:30:17 -0800226
Adrian Roos4c864592019-04-10 14:47:57 +0200227 final TaskDescription td = task.getTaskDescription();
228 if (td != null) {
Mehdi Alizadehc5e29902019-11-14 17:18:44 -0800229 taskDescription.copyFromPreserveHiddenFields(td);
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100230 }
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +0200231 taskBounds = new Rect();
232 task.getBounds(taskBounds);
Riddle Hsu84bc3d72019-12-19 19:24:07 +0800233 currentOrientation = topFullscreenOpaqueWindow.getConfiguration().orientation;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800234 }
235 try {
236 final int res = session.addToDisplay(window, window.mSeq, layoutParams,
Tiger Huang0426a332020-03-29 01:17:08 +0800237 View.GONE, activity.getDisplayContent().getDisplayId(), tmpFrame, tmpRect,
238 tmpRect, tmpCutout, null, mTmpInsetsState, mTempControls);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800239 if (res < 0) {
240 Slog.w(TAG, "Failed to add snapshot starting window res=" + res);
241 return null;
242 }
243 } catch (RemoteException e) {
244 // Local call.
245 }
246 final TaskSnapshotSurface snapshotSurface = new TaskSnapshotSurface(service, window,
Adrian Roos4c864592019-04-10 14:47:57 +0200247 surfaceControl, snapshot, layoutParams.getTitle(), taskDescription, sysUiVis,
248 windowFlags, windowPrivateFlags, taskBounds,
Jorim Jaggi0dd0cf92019-12-27 15:17:44 +0100249 currentOrientation, topFullscreenOpaqueWindow.getRequestedInsetsState());
Jorim Jaggi02886a82016-12-06 09:10:06 -0800250 window.setOuter(snapshotSurface);
251 try {
chaviwbe43ac82018-04-04 15:14:49 -0700252 session.relayout(window, window.mSeq, layoutParams, -1, -1, View.VISIBLE, 0, -1,
Jorim Jaggif081f062019-10-24 16:24:54 +0200253 tmpFrame, tmpContentInsets, tmpRect, tmpStableInsets, tmpRect,
Valerie Hau30360552020-01-14 16:12:01 -0800254 tmpCutout, tmpMergedConfiguration, surfaceControl, mTmpInsetsState,
Tiger Huang0426a332020-03-29 01:17:08 +0800255 mTempControls, sTmpSurfaceSize, sTmpSurfaceControl);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800256 } catch (RemoteException e) {
257 // Local call.
258 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200259 snapshotSurface.setFrames(tmpFrame, tmpContentInsets, tmpStableInsets);
260 snapshotSurface.drawSnapshot();
Jorim Jaggi02886a82016-12-06 09:10:06 -0800261 return snapshotSurface;
262 }
263
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100264 @VisibleForTesting
Robert Carr5fea55b2018-12-10 13:05:52 -0800265 TaskSnapshotSurface(WindowManagerService service, Window window, SurfaceControl surfaceControl,
Adrian Roos4c864592019-04-10 14:47:57 +0200266 TaskSnapshot snapshot, CharSequence title, TaskDescription taskDescription,
267 int sysUiVis, int windowFlags, int windowPrivateFlags, Rect taskBounds,
Tiger Huang4a7835f2019-11-06 00:07:56 +0800268 int currentOrientation, InsetsState insetsState) {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800269 mService = service;
Vishnu Nair33197392019-08-30 10:29:37 -0700270 mSurface = service.mSurfaceFactory.get();
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200271 mHandler = new Handler(mService.mH.getLooper());
Jorim Jaggi02886a82016-12-06 09:10:06 -0800272 mSession = WindowManagerGlobal.getWindowSession();
273 mWindow = window;
Robert Carr5fea55b2018-12-10 13:05:52 -0800274 mSurfaceControl = surfaceControl;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200275 mSnapshot = snapshot;
276 mTitle = title;
Adrian Roos4c864592019-04-10 14:47:57 +0200277 int backgroundColor = taskDescription.getBackgroundColor();
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200278 mBackgroundPaint.setColor(backgroundColor != 0 ? backgroundColor : WHITE);
279 mTaskBounds = taskBounds;
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200280 mSystemBarBackgroundPainter = new SystemBarBackgroundPainter(windowFlags,
Tiger Huang4a7835f2019-11-06 00:07:56 +0800281 windowPrivateFlags, sysUiVis, taskDescription, 1f, insetsState);
Adrian Roos4c864592019-04-10 14:47:57 +0200282 mStatusBarColor = taskDescription.getStatusBarColor();
Jorim Jaggi38d44ec2017-06-14 16:04:59 -0700283 mOrientationOnCreation = currentOrientation;
chaviw619da692019-06-10 15:39:40 -0700284 mTransaction = mService.mTransactionFactory.get();
Jorim Jaggi02886a82016-12-06 09:10:06 -0800285 }
286
287 @Override
288 public void remove() {
Wale Ogunwaledb485de2018-10-29 09:47:07 -0700289 synchronized (mService.mGlobalLock) {
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200290 final long now = SystemClock.uptimeMillis();
291 if (mSizeMismatch && now - mShownTime < SIZE_MISMATCH_MINIMUM_TIME_MS) {
292 mHandler.postAtTime(this::remove, mShownTime + SIZE_MISMATCH_MINIMUM_TIME_MS);
Adrian Roosb125e0b2019-10-02 14:55:14 +0200293 ProtoLog.v(WM_DEBUG_STARTING_WINDOW,
294 "Defer removing snapshot surface in %dms", (now - mShownTime));
295
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200296 return;
297 }
298 }
Jorim Jaggi02886a82016-12-06 09:10:06 -0800299 try {
Adrian Roosb125e0b2019-10-02 14:55:14 +0200300 ProtoLog.v(WM_DEBUG_STARTING_WINDOW, "Removing snapshot surface");
Jorim Jaggi02886a82016-12-06 09:10:06 -0800301 mSession.remove(mWindow);
302 } catch (RemoteException e) {
303 // Local call.
304 }
305 }
306
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200307 @VisibleForTesting
308 void setFrames(Rect frame, Rect contentInsets, Rect stableInsets) {
309 mFrame.set(frame);
310 mContentInsets.set(contentInsets);
311 mStableInsets.set(stableInsets);
312 mSizeMismatch = (mFrame.width() != mSnapshot.getSnapshot().getWidth()
313 || mFrame.height() != mSnapshot.getSnapshot().getHeight());
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200314 mSystemBarBackgroundPainter.setInsets(contentInsets, stableInsets);
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200315 }
316
317 private void drawSnapshot() {
Robert Carr5fea55b2018-12-10 13:05:52 -0800318 mSurface.copyFrom(mSurfaceControl);
319
Adrian Roosb125e0b2019-10-02 14:55:14 +0200320 ProtoLog.v(WM_DEBUG_STARTING_WINDOW, "Drawing snapshot surface sizeMismatch=%b",
321 mSizeMismatch);
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200322 if (mSizeMismatch) {
323 // The dimensions of the buffer and the window don't match, so attaching the buffer
324 // will fail. Better create a child window with the exact dimensions and fill the parent
325 // window with the background color!
Peiyong Linccc06b62019-06-25 17:31:09 -0700326 drawSizeMismatchSnapshot();
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200327 } else {
Peiyong Linccc06b62019-06-25 17:31:09 -0700328 drawSizeMatchSnapshot();
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200329 }
Wale Ogunwaledb485de2018-10-29 09:47:07 -0700330 synchronized (mService.mGlobalLock) {
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200331 mShownTime = SystemClock.uptimeMillis();
Jorim Jaggi39367cf2017-03-13 16:41:13 +0100332 mHasDrawn = true;
Jorim Jaggi39367cf2017-03-13 16:41:13 +0100333 }
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200334 reportDrawn();
Jorim Jaggi4448e1e2017-05-16 22:26:02 +0200335
336 // In case window manager leaks us, make sure we don't retain the snapshot.
337 mSnapshot = null;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200338 }
339
Peiyong Linccc06b62019-06-25 17:31:09 -0700340 private void drawSizeMatchSnapshot() {
341 mSurface.attachAndQueueBufferWithColorSpace(mSnapshot.getSnapshot(),
342 mSnapshot.getColorSpace());
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200343 mSurface.release();
344 }
345
Peiyong Linccc06b62019-06-25 17:31:09 -0700346 private void drawSizeMismatchSnapshot() {
Shibin George9696bd32018-02-26 20:29:44 +0530347 if (!mSurface.isValid()) {
348 throw new IllegalStateException("mSurface does not hold a valid surface.");
349 }
Peiyong Linccc06b62019-06-25 17:31:09 -0700350 final GraphicBuffer buffer = mSnapshot.getSnapshot();
Robert Carr5fea55b2018-12-10 13:05:52 -0800351 final SurfaceSession session = new SurfaceSession();
Winson Chung357deda2019-06-23 13:35:41 -0700352 // We consider nearly matched dimensions as there can be rounding errors and the user won't
353 // notice very minute differences from scaling one dimension more than the other
354 final boolean aspectRatioMismatch = Math.abs(
355 ((float) buffer.getWidth() / buffer.getHeight())
356 - ((float) mFrame.width() / mFrame.height())) > 0.01f;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200357
358 // Keep a reference to it such that it doesn't get destroyed when finalized.
Vishnu Nair33197392019-08-30 10:29:37 -0700359 mChildSurfaceControl = mService.mSurfaceControlFactory.apply(session)
Robert Carre625fcf2017-09-01 12:36:28 -0700360 .setName(mTitle + " - task-snapshot-surface")
Vishnu Naire86bd982018-11-28 13:23:17 -0800361 .setBufferSize(buffer.getWidth(), buffer.getHeight())
Robert Carre625fcf2017-09-01 12:36:28 -0700362 .setFormat(buffer.getFormat())
Robert Carr5fea55b2018-12-10 13:05:52 -0800363 .setParent(mSurfaceControl)
Robert Carre625fcf2017-09-01 12:36:28 -0700364 .build();
Vishnu Nair33197392019-08-30 10:29:37 -0700365 Surface surface = mService.mSurfaceFactory.get();
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200366 surface.copyFrom(mChildSurfaceControl);
367
Winson Chung357deda2019-06-23 13:35:41 -0700368 final Rect frame;
chaviw619da692019-06-10 15:39:40 -0700369 // We can just show the surface here as it will still be hidden as the parent is
370 // still hidden.
371 mTransaction.show(mChildSurfaceControl);
372 if (aspectRatioMismatch) {
373 // Clip off ugly navigation bar.
374 final Rect crop = calculateSnapshotCrop();
375 frame = calculateSnapshotFrame(crop);
376 mTransaction.setWindowCrop(mChildSurfaceControl, crop);
377 mTransaction.setPosition(mChildSurfaceControl, frame.left, frame.top);
shawnlin4d2a2f42020-04-14 14:34:16 +0800378 mTmpSnapshotSize.set(crop);
Peter Kalauskasfe0a4132020-01-31 16:50:42 -0800379 mTmpDstFrame.set(frame);
chaviw619da692019-06-10 15:39:40 -0700380 } else {
381 frame = null;
shawnlin4d2a2f42020-04-14 14:34:16 +0800382 mTmpSnapshotSize.set(0, 0, buffer.getWidth(), buffer.getHeight());
Peter Kalauskasfe0a4132020-01-31 16:50:42 -0800383 mTmpDstFrame.set(mFrame);
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200384 }
chaviw619da692019-06-10 15:39:40 -0700385
386 // Scale the mismatch dimensions to fill the task bounds
Peter Kalauskasfe0a4132020-01-31 16:50:42 -0800387 mSnapshotMatrix.setRectToRect(mTmpSnapshotSize, mTmpDstFrame, Matrix.ScaleToFit.FILL);
388 mTransaction.setMatrix(mChildSurfaceControl, mSnapshotMatrix, mTmpFloat9);
389
chaviw619da692019-06-10 15:39:40 -0700390 mTransaction.apply();
Peiyong Linccc06b62019-06-25 17:31:09 -0700391 surface.attachAndQueueBufferWithColorSpace(buffer, mSnapshot.getColorSpace());
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200392 surface.release();
393
Winson Chung357deda2019-06-23 13:35:41 -0700394 if (aspectRatioMismatch) {
395 final Canvas c = mSurface.lockCanvas(null);
396 drawBackgroundAndBars(c, frame);
397 mSurface.unlockCanvasAndPost(c);
398 mSurface.release();
399 }
Jorim Jaggi02886a82016-12-06 09:10:06 -0800400 }
401
Matthew Ngcb7ac672017-07-21 17:27:42 -0700402 /**
403 * Calculates the snapshot crop in snapshot coordinate space.
404 *
405 * @return crop rect in snapshot coordinate space.
406 */
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100407 @VisibleForTesting
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200408 Rect calculateSnapshotCrop() {
409 final Rect rect = new Rect();
410 rect.set(0, 0, mSnapshot.getSnapshot().getWidth(), mSnapshot.getSnapshot().getHeight());
411 final Rect insets = mSnapshot.getContentInsets();
412
Peter Kalauskasfe0a4132020-01-31 16:50:42 -0800413 final float scaleX = (float) mSnapshot.getSnapshot().getWidth() / mSnapshot.getTaskSize().x;
414 final float scaleY =
415 (float) mSnapshot.getSnapshot().getHeight() / mSnapshot.getTaskSize().y;
416
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200417 // Let's remove all system decorations except the status bar, but only if the task is at the
418 // very top of the screen.
Adrian Roos98a146d2017-11-29 16:39:44 +0100419 final boolean isTop = mTaskBounds.top == 0 && mFrame.top == 0;
Peter Kalauskasfe0a4132020-01-31 16:50:42 -0800420 rect.inset((int) (insets.left * scaleX),
421 isTop ? 0 : (int) (insets.top * scaleY),
422 (int) (insets.right * scaleX),
423 (int) (insets.bottom * scaleY));
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200424 return rect;
425 }
426
Matthew Ngcb7ac672017-07-21 17:27:42 -0700427 /**
428 * Calculates the snapshot frame in window coordinate space from crop.
429 *
430 * @param crop rect that is in snapshot coordinate space.
431 */
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200432 @VisibleForTesting
433 Rect calculateSnapshotFrame(Rect crop) {
Peter Kalauskasfe0a4132020-01-31 16:50:42 -0800434 final float scaleX = (float) mSnapshot.getSnapshot().getWidth() / mSnapshot.getTaskSize().x;
435 final float scaleY =
436 (float) mSnapshot.getSnapshot().getHeight() / mSnapshot.getTaskSize().y;
Matthew Ngcb7ac672017-07-21 17:27:42 -0700437
438 // Rescale the frame from snapshot to window coordinate space
shawnlin4d2a2f42020-04-14 14:34:16 +0800439 final Rect frame = new Rect(0, 0,
440 (int) (crop.width() / scaleX + 0.5f),
441 (int) (crop.height() / scaleY + 0.5f)
Peter Kalauskasfe0a4132020-01-31 16:50:42 -0800442 );
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200443
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200444 // However, we also need to make space for the navigation bar on the left side.
445 final int colorViewLeftInset = getColorViewLeftInset(mStableInsets.left,
446 mContentInsets.left);
447 frame.offset(colorViewLeftInset, 0);
448 return frame;
449 }
450
451 @VisibleForTesting
452 void drawBackgroundAndBars(Canvas c, Rect frame) {
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200453 final int statusBarHeight = mSystemBarBackgroundPainter.getStatusBarColorViewHeight();
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200454 final boolean fillHorizontally = c.getWidth() > frame.right;
455 final boolean fillVertically = c.getHeight() > frame.bottom;
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100456 if (fillHorizontally) {
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200457 c.drawRect(frame.right, alpha(mStatusBarColor) == 0xFF ? statusBarHeight : 0,
458 c.getWidth(), fillVertically
459 ? frame.bottom
460 : c.getHeight(),
461 mBackgroundPaint);
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100462 }
463 if (fillVertically) {
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200464 c.drawRect(0, frame.bottom, c.getWidth(), c.getHeight(), mBackgroundPaint);
465 }
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200466 mSystemBarBackgroundPainter.drawDecors(c, frame);
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100467 }
468
Jorim Jaggi02886a82016-12-06 09:10:06 -0800469 private void reportDrawn() {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800470 try {
Vishnu Nairf7645aa2019-06-18 11:14:01 -0700471 mSession.finishDrawing(mWindow, null /* postDrawTransaction */);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800472 } catch (RemoteException e) {
473 // Local call.
474 }
475 }
476
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100477 private static Handler sHandler = new Handler(Looper.getMainLooper()) {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800478
479 @Override
480 public void handleMessage(Message msg) {
481 switch (msg.what) {
482 case MSG_REPORT_DRAW:
483 final boolean hasDrawn;
484 final TaskSnapshotSurface surface = (TaskSnapshotSurface) msg.obj;
Wale Ogunwaledb485de2018-10-29 09:47:07 -0700485 synchronized (surface.mService.mGlobalLock) {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800486 hasDrawn = surface.mHasDrawn;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800487 }
488 if (hasDrawn) {
489 surface.reportDrawn();
490 }
491 break;
492 }
493 }
494 };
495
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200496 @VisibleForTesting
497 static class Window extends BaseIWindow {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800498
499 private TaskSnapshotSurface mOuter;
Jorim Jaggi38d44ec2017-06-14 16:04:59 -0700500
Jorim Jaggi02886a82016-12-06 09:10:06 -0800501 public void setOuter(TaskSnapshotSurface outer) {
502 mOuter = outer;
503 }
504
505 @Override
Jorim Jaggif081f062019-10-24 16:24:54 +0200506 public void resized(Rect frame, Rect contentInsets, Rect visibleInsets,
507 Rect stableInsets, boolean reportDraw,
Andrii Kulian44607962017-03-16 11:06:24 -0700508 MergedConfiguration mergedConfiguration, Rect backDropFrame, boolean forceLayout,
Brad Stenninge0573692019-03-11 13:52:46 -0700509 boolean alwaysConsumeSystemBars, int displayId,
Adrian Roos5c6b6222017-11-07 17:36:10 +0100510 DisplayCutout.ParcelableWrapper displayCutout) {
Jorim Jaggi38d44ec2017-06-14 16:04:59 -0700511 if (mergedConfiguration != null && mOuter != null
512 && mOuter.mOrientationOnCreation
513 != mergedConfiguration.getMergedConfiguration().orientation) {
514
515 // The orientation of the screen is changing. We better remove the snapshot ASAP as
516 // we are going to wait on the new window in any case to unfreeze the screen, and
517 // the starting window is not needed anymore.
518 sHandler.post(mOuter::remove);
519 }
Jorim Jaggi02886a82016-12-06 09:10:06 -0800520 if (reportDraw) {
521 sHandler.obtainMessage(MSG_REPORT_DRAW, mOuter).sendToTarget();
522 }
523 }
524 }
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200525
526 /**
527 * Helper class to draw the background of the system bars in regions the task snapshot isn't
528 * filling the window.
529 */
530 static class SystemBarBackgroundPainter {
531
532 private final Rect mContentInsets = new Rect();
533 private final Rect mStableInsets = new Rect();
534 private final Paint mStatusBarPaint = new Paint();
535 private final Paint mNavigationBarPaint = new Paint();
536 private final int mStatusBarColor;
537 private final int mNavigationBarColor;
538 private final int mWindowFlags;
539 private final int mWindowPrivateFlags;
540 private final int mSysUiVis;
Winson Chungffde2ea2019-06-17 17:19:13 -0700541 private final float mScale;
Tiger Huang4a7835f2019-11-06 00:07:56 +0800542 private final InsetsState mInsetsState;
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200543
Winson Chungffde2ea2019-06-17 17:19:13 -0700544 SystemBarBackgroundPainter(int windowFlags, int windowPrivateFlags, int sysUiVis,
Tiger Huang4a7835f2019-11-06 00:07:56 +0800545 TaskDescription taskDescription, float scale, InsetsState insetsState) {
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200546 mWindowFlags = windowFlags;
547 mWindowPrivateFlags = windowPrivateFlags;
548 mSysUiVis = sysUiVis;
Winson Chungffde2ea2019-06-17 17:19:13 -0700549 mScale = scale;
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200550 final Context context = ActivityThread.currentActivityThread().getSystemUiContext();
Jorim Jaggia6aabac2019-03-11 14:23:16 -0700551 final int semiTransparent = context.getColor(
552 R.color.system_bar_background_semi_transparent);
553 mStatusBarColor = DecorView.calculateBarColor(windowFlags, FLAG_TRANSLUCENT_STATUS,
Adrian Roos4c864592019-04-10 14:47:57 +0200554 semiTransparent, taskDescription.getStatusBarColor(), sysUiVis,
555 SYSTEM_UI_FLAG_LIGHT_STATUS_BAR,
556 taskDescription.getEnsureStatusBarContrastWhenTransparent());
Jorim Jaggia6aabac2019-03-11 14:23:16 -0700557 mNavigationBarColor = DecorView.calculateBarColor(windowFlags,
Adrian Roos4c864592019-04-10 14:47:57 +0200558 FLAG_TRANSLUCENT_NAVIGATION, semiTransparent,
559 taskDescription.getNavigationBarColor(), sysUiVis,
560 SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR,
561 taskDescription.getEnsureNavigationBarContrastWhenTransparent()
562 && context.getResources().getBoolean(R.bool.config_navBarNeedsScrim));
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200563 mStatusBarPaint.setColor(mStatusBarColor);
Adrian Roos4c864592019-04-10 14:47:57 +0200564 mNavigationBarPaint.setColor(mNavigationBarColor);
Tiger Huang4a7835f2019-11-06 00:07:56 +0800565 mInsetsState = insetsState;
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200566 }
567
568 void setInsets(Rect contentInsets, Rect stableInsets) {
569 mContentInsets.set(contentInsets);
570 mStableInsets.set(stableInsets);
571 }
572
573 int getStatusBarColorViewHeight() {
Jorim Jaggia6aabac2019-03-11 14:23:16 -0700574 final boolean forceBarBackground =
575 (mWindowPrivateFlags & PRIVATE_FLAG_FORCE_DRAW_BAR_BACKGROUNDS) != 0;
Tiger Huang4a7835f2019-11-06 00:07:56 +0800576 if (ViewRootImpl.sNewInsetsMode != ViewRootImpl.NEW_INSETS_MODE_FULL
577 ? STATUS_BAR_COLOR_VIEW_ATTRIBUTES.isVisible(
578 mSysUiVis, mStatusBarColor, mWindowFlags, forceBarBackground)
579 : STATUS_BAR_COLOR_VIEW_ATTRIBUTES.isVisible(
580 mInsetsState, mStatusBarColor, mWindowFlags, forceBarBackground)) {
Winson Chungffde2ea2019-06-17 17:19:13 -0700581 return (int) (getColorViewTopInset(mStableInsets.top, mContentInsets.top) * mScale);
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200582 } else {
583 return 0;
584 }
585 }
586
587 private boolean isNavigationBarColorViewVisible() {
Jorim Jaggia6aabac2019-03-11 14:23:16 -0700588 final boolean forceBarBackground =
589 (mWindowPrivateFlags & PRIVATE_FLAG_FORCE_DRAW_BAR_BACKGROUNDS) != 0;
Tiger Huang4a7835f2019-11-06 00:07:56 +0800590 return ViewRootImpl.sNewInsetsMode != ViewRootImpl.NEW_INSETS_MODE_FULL
591 ? NAVIGATION_BAR_COLOR_VIEW_ATTRIBUTES.isVisible(
592 mSysUiVis, mNavigationBarColor, mWindowFlags, forceBarBackground)
593 : NAVIGATION_BAR_COLOR_VIEW_ATTRIBUTES.isVisible(
594 mInsetsState, mNavigationBarColor, mWindowFlags, forceBarBackground);
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200595 }
596
597 void drawDecors(Canvas c, @Nullable Rect alreadyDrawnFrame) {
598 drawStatusBarBackground(c, alreadyDrawnFrame, getStatusBarColorViewHeight());
599 drawNavigationBarBackground(c);
600 }
601
602 @VisibleForTesting
603 void drawStatusBarBackground(Canvas c, @Nullable Rect alreadyDrawnFrame,
604 int statusBarHeight) {
Winson Chung51f42d22018-02-01 14:59:38 -0800605 if (statusBarHeight > 0 && Color.alpha(mStatusBarColor) != 0
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200606 && (alreadyDrawnFrame == null || c.getWidth() > alreadyDrawnFrame.right)) {
Winson Chungffde2ea2019-06-17 17:19:13 -0700607 final int rightInset = (int) (DecorView.getColorViewRightInset(mStableInsets.right,
608 mContentInsets.right) * mScale);
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200609 final int left = alreadyDrawnFrame != null ? alreadyDrawnFrame.right : 0;
610 c.drawRect(left, 0, c.getWidth() - rightInset, statusBarHeight, mStatusBarPaint);
611 }
612 }
613
614 @VisibleForTesting
615 void drawNavigationBarBackground(Canvas c) {
616 final Rect navigationBarRect = new Rect();
617 getNavigationBarRect(c.getWidth(), c.getHeight(), mStableInsets, mContentInsets,
Winson Chungffde2ea2019-06-17 17:19:13 -0700618 navigationBarRect, mScale);
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200619 final boolean visible = isNavigationBarColorViewVisible();
Winson Chung51f42d22018-02-01 14:59:38 -0800620 if (visible && Color.alpha(mNavigationBarColor) != 0 && !navigationBarRect.isEmpty()) {
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200621 c.drawRect(navigationBarRect, mNavigationBarPaint);
622 }
623 }
624 }
Jorim Jaggi02886a82016-12-06 09:10:06 -0800625}