blob: 67d2be874a315733e58f014bc4667ab49f78f4d4 [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;
Jorim Jaggi30d64f32017-04-07 16:33:17 +020021import static android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
22import static android.view.WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
23import static android.view.WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES;
24import static android.view.WindowManager.LayoutParams.FLAG_LOCAL_FOCUS_MODE;
Jorim Jaggi02886a82016-12-06 09:10:06 -080025import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
26import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
Jorim Jaggi30d64f32017-04-07 16:33:17 +020027import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
28import static android.view.WindowManager.LayoutParams.FLAG_SCALED;
29import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
30import static android.view.WindowManager.LayoutParams.FLAG_SLIPPERY;
31import static android.view.WindowManager.LayoutParams.FLAG_SPLIT_TOUCH;
32import static android.view.WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
33import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_FORCE_DRAW_STATUS_BAR_BACKGROUND;
Jorim Jaggi02886a82016-12-06 09:10:06 -080034import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Jorim Jaggi30d64f32017-04-07 16:33:17 +020035import static com.android.internal.policy.DecorView.NAVIGATION_BAR_COLOR_VIEW_ATTRIBUTES;
36import static com.android.internal.policy.DecorView.STATUS_BAR_COLOR_VIEW_ATTRIBUTES;
37import static com.android.internal.policy.DecorView.getColorViewLeftInset;
38import static com.android.internal.policy.DecorView.getColorViewTopInset;
39import static com.android.internal.policy.DecorView.getNavigationBarRect;
Jorim Jaggie4b0f282017-05-17 15:10:29 +020040import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STARTING_WINDOW;
Jorim Jaggi02886a82016-12-06 09:10:06 -080041import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
42import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
43
Jorim Jaggid635a4a2017-05-03 15:21:26 +020044import android.annotation.Nullable;
Jorim Jaggi829b9cd2017-01-23 16:20:53 +010045import android.app.ActivityManager.TaskDescription;
Jorim Jaggi30d64f32017-04-07 16:33:17 +020046import android.app.ActivityManager.TaskSnapshot;
Jorim Jaggid635a4a2017-05-03 15:21:26 +020047import android.app.ActivityThread;
48import android.content.Context;
Jorim Jaggi02886a82016-12-06 09:10:06 -080049import android.graphics.Canvas;
Winson Chung51f42d22018-02-01 14:59:38 -080050import android.graphics.Color;
Jorim Jaggi02886a82016-12-06 09:10:06 -080051import android.graphics.GraphicBuffer;
Jorim Jaggi829b9cd2017-01-23 16:20:53 +010052import android.graphics.Paint;
Jorim Jaggi02886a82016-12-06 09:10:06 -080053import android.graphics.Rect;
54import android.os.Handler;
Jorim Jaggi829b9cd2017-01-23 16:20:53 +010055import android.os.Looper;
Jorim Jaggi02886a82016-12-06 09:10:06 -080056import android.os.Message;
57import android.os.RemoteException;
Jorim Jaggi30d64f32017-04-07 16:33:17 +020058import android.os.SystemClock;
Andrii Kulian44607962017-03-16 11:06:24 -070059import android.util.MergedConfiguration;
Jorim Jaggi02886a82016-12-06 09:10:06 -080060import android.util.Slog;
Adrian Roos5c6b6222017-11-07 17:36:10 +010061import android.view.DisplayCutout;
Jorim Jaggi02886a82016-12-06 09:10:06 -080062import android.view.IWindowSession;
63import android.view.Surface;
Jorim Jaggi30d64f32017-04-07 16:33:17 +020064import android.view.SurfaceControl;
65import android.view.SurfaceSession;
Jorim Jaggi02886a82016-12-06 09:10:06 -080066import android.view.View;
67import android.view.ViewGroup.LayoutParams;
68import android.view.WindowManager;
69import android.view.WindowManagerGlobal;
Jorim Jaggi02886a82016-12-06 09:10:06 -080070
Jorim Jaggi30d64f32017-04-07 16:33:17 +020071import com.android.internal.R;
Jorim Jaggi829b9cd2017-01-23 16:20:53 +010072import com.android.internal.annotations.VisibleForTesting;
Jorim Jaggi30d64f32017-04-07 16:33:17 +020073import com.android.internal.policy.DecorView;
Jorim Jaggi02886a82016-12-06 09:10:06 -080074import com.android.internal.view.BaseIWindow;
Adrian Roose99bc052017-11-20 17:55:31 +010075import com.android.server.policy.WindowManagerPolicy.StartingSurface;
Jorim Jaggi02886a82016-12-06 09:10:06 -080076
77/**
78 * This class represents a starting window that shows a snapshot.
79 * <p>
80 * DO NOT HOLD THE WINDOW MANAGER LOCK WHEN CALLING METHODS OF THIS CLASS!
81 */
82class TaskSnapshotSurface implements StartingSurface {
83
Jorim Jaggi30d64f32017-04-07 16:33:17 +020084 private static final long SIZE_MISMATCH_MINIMUM_TIME_MS = 450;
85
86 /**
87 * When creating the starting window, we use the exact same layout flags such that we end up
88 * with a window with the exact same dimensions etc. However, these flags are not used in layout
89 * and might cause other side effects so we exclude them.
90 */
91 private static final int FLAG_INHERIT_EXCLUDES = FLAG_NOT_FOCUSABLE
92 | FLAG_NOT_TOUCHABLE
93 | FLAG_NOT_TOUCH_MODAL
94 | FLAG_ALT_FOCUSABLE_IM
95 | FLAG_NOT_FOCUSABLE
96 | FLAG_HARDWARE_ACCELERATED
97 | FLAG_IGNORE_CHEEK_PRESSES
98 | FLAG_LOCAL_FOCUS_MODE
99 | FLAG_SLIPPERY
100 | FLAG_WATCH_OUTSIDE_TOUCH
101 | FLAG_SPLIT_TOUCH
102 | FLAG_SCALED
103 | FLAG_SECURE;
104
Jorim Jaggid2616322017-06-07 12:38:19 -0700105 private static final int PRIVATE_FLAG_INHERITS = PRIVATE_FLAG_FORCE_DRAW_STATUS_BAR_BACKGROUND;
106
Jorim Jaggi02886a82016-12-06 09:10:06 -0800107 private static final String TAG = TAG_WITH_CLASS_NAME ? "SnapshotStartingWindow" : TAG_WM;
108 private static final int MSG_REPORT_DRAW = 0;
109 private static final String TITLE_FORMAT = "SnapshotStartingWindow for taskId=%s";
110 private final Window mWindow;
111 private final Surface mSurface;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200112 private SurfaceControl mChildSurfaceControl;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800113 private final IWindowSession mSession;
114 private final WindowManagerService mService;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200115 private final Rect mTaskBounds;
116 private final Rect mStableInsets = new Rect();
117 private final Rect mContentInsets = new Rect();
118 private final Rect mFrame = new Rect();
Jorim Jaggi4448e1e2017-05-16 22:26:02 +0200119 private TaskSnapshot mSnapshot;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200120 private final CharSequence mTitle;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800121 private boolean mHasDrawn;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200122 private long mShownTime;
123 private final Handler mHandler;
124 private boolean mSizeMismatch;
125 private final Paint mBackgroundPaint = new Paint();
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200126 private final int mStatusBarColor;
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200127 @VisibleForTesting final SystemBarBackgroundPainter mSystemBarBackgroundPainter;
Jorim Jaggi38d44ec2017-06-14 16:04:59 -0700128 private final int mOrientationOnCreation;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800129
130 static TaskSnapshotSurface create(WindowManagerService service, AppWindowToken token,
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200131 TaskSnapshot snapshot) {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800132
133 final WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
134 final Window window = new Window();
135 final IWindowSession session = WindowManagerGlobal.getWindowSession();
136 window.setSession(session);
137 final Surface surface = new Surface();
138 final Rect tmpRect = new Rect();
Adrian Roos5c6b6222017-11-07 17:36:10 +0100139 final DisplayCutout.ParcelableWrapper tmpCutout = new DisplayCutout.ParcelableWrapper();
Jorim Jaggi02886a82016-12-06 09:10:06 -0800140 final Rect tmpFrame = new Rect();
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200141 final Rect taskBounds;
142 final Rect tmpContentInsets = new Rect();
143 final Rect tmpStableInsets = new Rect();
Andrii Kulian44607962017-03-16 11:06:24 -0700144 final MergedConfiguration tmpMergedConfiguration = new MergedConfiguration();
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200145 int backgroundColor = WHITE;
146 int statusBarColor = 0;
147 int navigationBarColor = 0;
148 final int sysUiVis;
149 final int windowFlags;
150 final int windowPrivateFlags;
Jorim Jaggi38d44ec2017-06-14 16:04:59 -0700151 final int currentOrientation;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800152 synchronized (service.mWindowMap) {
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200153 final WindowState mainWindow = token.findMainWindow();
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +0200154 final Task task = token.getTask();
155 if (task == null) {
156 Slog.w(TAG, "TaskSnapshotSurface.create: Failed to find task for token="
157 + token);
158 return null;
159 }
160 final AppWindowToken topFullscreenToken = token.getTask().getTopFullscreenAppToken();
161 if (topFullscreenToken == null) {
162 Slog.w(TAG, "TaskSnapshotSurface.create: Failed to find top fullscreen for task="
163 + task);
164 return null;
165 }
Jorim Jaggi87fdbcb2017-08-17 13:41:11 +0200166 final WindowState topFullscreenWindow = topFullscreenToken.getTopFullscreenWindow();
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +0200167 if (mainWindow == null || topFullscreenWindow == null) {
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200168 Slog.w(TAG, "TaskSnapshotSurface.create: Failed to find main window for token="
169 + token);
170 return null;
171 }
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +0200172 sysUiVis = topFullscreenWindow.getSystemUiVisibility();
173 windowFlags = topFullscreenWindow.getAttrs().flags;
174 windowPrivateFlags = topFullscreenWindow.getAttrs().privateFlags;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200175
Jorim Jaggi90b3c472018-04-06 16:02:07 +0200176 layoutParams.packageName = mainWindow.getAttrs().packageName;
177 layoutParams.windowAnimations = mainWindow.getAttrs().windowAnimations;
Winson Chungbe9be7f2017-06-28 10:55:22 -0700178 layoutParams.dimAmount = mainWindow.getAttrs().dimAmount;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800179 layoutParams.type = TYPE_APPLICATION_STARTING;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200180 layoutParams.format = snapshot.getSnapshot().getFormat();
181 layoutParams.flags = (windowFlags & ~FLAG_INHERIT_EXCLUDES)
Jorim Jaggi02886a82016-12-06 09:10:06 -0800182 | FLAG_NOT_FOCUSABLE
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200183 | FLAG_NOT_TOUCHABLE;
Wale Ogunwale01ad4342017-06-30 07:07:01 -0700184 layoutParams.privateFlags = windowPrivateFlags & PRIVATE_FLAG_INHERITS;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800185 layoutParams.token = token.token;
186 layoutParams.width = LayoutParams.MATCH_PARENT;
187 layoutParams.height = LayoutParams.MATCH_PARENT;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200188 layoutParams.systemUiVisibility = sysUiVis;
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +0200189 layoutParams.setTitle(String.format(TITLE_FORMAT, task.mTaskId));
Bryce Lee6d410262017-02-28 15:30:17 -0800190
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +0200191 final TaskDescription taskDescription = task.getTaskDescription();
192 if (taskDescription != null) {
193 backgroundColor = taskDescription.getBackgroundColor();
194 statusBarColor = taskDescription.getStatusBarColor();
195 navigationBarColor = taskDescription.getNavigationBarColor();
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100196 }
Jorim Jaggie6c6ecb2017-07-20 18:09:20 +0200197 taskBounds = new Rect();
198 task.getBounds(taskBounds);
199 currentOrientation = topFullscreenWindow.getConfiguration().orientation;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800200 }
201 try {
202 final int res = session.addToDisplay(window, window.mSeq, layoutParams,
Adrian Roos9e370f22018-03-06 18:19:45 +0100203 View.GONE, token.getDisplayContent().getDisplayId(), tmpFrame, tmpRect, tmpRect,
Adrian Roos5c6b6222017-11-07 17:36:10 +0100204 tmpRect, tmpCutout, null);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800205 if (res < 0) {
206 Slog.w(TAG, "Failed to add snapshot starting window res=" + res);
207 return null;
208 }
209 } catch (RemoteException e) {
210 // Local call.
211 }
212 final TaskSnapshotSurface snapshotSurface = new TaskSnapshotSurface(service, window,
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200213 surface, snapshot, layoutParams.getTitle(), backgroundColor, statusBarColor,
Jorim Jaggi38d44ec2017-06-14 16:04:59 -0700214 navigationBarColor, sysUiVis, windowFlags, windowPrivateFlags, taskBounds,
215 currentOrientation);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800216 window.setOuter(snapshotSurface);
217 try {
chaviwbe43ac82018-04-04 15:14:49 -0700218 session.relayout(window, window.mSeq, layoutParams, -1, -1, View.VISIBLE, 0, -1,
219 tmpFrame, tmpRect, tmpContentInsets, tmpRect, tmpStableInsets, tmpRect, tmpRect,
Adrian Roos5c6b6222017-11-07 17:36:10 +0100220 tmpCutout, tmpMergedConfiguration, surface);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800221 } catch (RemoteException e) {
222 // Local call.
223 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200224 snapshotSurface.setFrames(tmpFrame, tmpContentInsets, tmpStableInsets);
225 snapshotSurface.drawSnapshot();
Jorim Jaggi02886a82016-12-06 09:10:06 -0800226 return snapshotSurface;
227 }
228
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100229 @VisibleForTesting
230 TaskSnapshotSurface(WindowManagerService service, Window window, Surface surface,
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200231 TaskSnapshot snapshot, CharSequence title, int backgroundColor, int statusBarColor,
232 int navigationBarColor, int sysUiVis, int windowFlags, int windowPrivateFlags,
Jorim Jaggi38d44ec2017-06-14 16:04:59 -0700233 Rect taskBounds, int currentOrientation) {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800234 mService = service;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200235 mHandler = new Handler(mService.mH.getLooper());
Jorim Jaggi02886a82016-12-06 09:10:06 -0800236 mSession = WindowManagerGlobal.getWindowSession();
237 mWindow = window;
238 mSurface = surface;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200239 mSnapshot = snapshot;
240 mTitle = title;
241 mBackgroundPaint.setColor(backgroundColor != 0 ? backgroundColor : WHITE);
242 mTaskBounds = taskBounds;
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200243 mSystemBarBackgroundPainter = new SystemBarBackgroundPainter(windowFlags,
244 windowPrivateFlags, sysUiVis, statusBarColor, navigationBarColor);
245 mStatusBarColor = statusBarColor;
Jorim Jaggi38d44ec2017-06-14 16:04:59 -0700246 mOrientationOnCreation = currentOrientation;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800247 }
248
249 @Override
250 public void remove() {
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200251 synchronized (mService.mWindowMap) {
252 final long now = SystemClock.uptimeMillis();
253 if (mSizeMismatch && now - mShownTime < SIZE_MISMATCH_MINIMUM_TIME_MS) {
254 mHandler.postAtTime(this::remove, mShownTime + SIZE_MISMATCH_MINIMUM_TIME_MS);
Jorim Jaggie4b0f282017-05-17 15:10:29 +0200255 if (DEBUG_STARTING_WINDOW) {
256 Slog.v(TAG, "Defer removing snapshot surface in " + (now - mShownTime) + "ms");
257 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200258 return;
259 }
260 }
Jorim Jaggi02886a82016-12-06 09:10:06 -0800261 try {
Jorim Jaggie4b0f282017-05-17 15:10:29 +0200262 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Removing snapshot surface");
Jorim Jaggi02886a82016-12-06 09:10:06 -0800263 mSession.remove(mWindow);
264 } catch (RemoteException e) {
265 // Local call.
266 }
267 }
268
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200269 @VisibleForTesting
270 void setFrames(Rect frame, Rect contentInsets, Rect stableInsets) {
271 mFrame.set(frame);
272 mContentInsets.set(contentInsets);
273 mStableInsets.set(stableInsets);
274 mSizeMismatch = (mFrame.width() != mSnapshot.getSnapshot().getWidth()
275 || mFrame.height() != mSnapshot.getSnapshot().getHeight());
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200276 mSystemBarBackgroundPainter.setInsets(contentInsets, stableInsets);
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200277 }
278
279 private void drawSnapshot() {
280 final GraphicBuffer buffer = mSnapshot.getSnapshot();
Jorim Jaggie4b0f282017-05-17 15:10:29 +0200281 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Drawing snapshot surface sizeMismatch="
282 + mSizeMismatch);
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200283 if (mSizeMismatch) {
284 // The dimensions of the buffer and the window don't match, so attaching the buffer
285 // will fail. Better create a child window with the exact dimensions and fill the parent
286 // window with the background color!
287 drawSizeMismatchSnapshot(buffer);
288 } else {
289 drawSizeMatchSnapshot(buffer);
290 }
Jorim Jaggi39367cf2017-03-13 16:41:13 +0100291 synchronized (mService.mWindowMap) {
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200292 mShownTime = SystemClock.uptimeMillis();
Jorim Jaggi39367cf2017-03-13 16:41:13 +0100293 mHasDrawn = true;
Jorim Jaggi39367cf2017-03-13 16:41:13 +0100294 }
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200295 reportDrawn();
Jorim Jaggi4448e1e2017-05-16 22:26:02 +0200296
297 // In case window manager leaks us, make sure we don't retain the snapshot.
298 mSnapshot = null;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200299 }
300
301 private void drawSizeMatchSnapshot(GraphicBuffer buffer) {
302 mSurface.attachAndQueueBuffer(buffer);
303 mSurface.release();
304 }
305
306 private void drawSizeMismatchSnapshot(GraphicBuffer buffer) {
Shibin George9696bd32018-02-26 20:29:44 +0530307 if (!mSurface.isValid()) {
308 throw new IllegalStateException("mSurface does not hold a valid surface.");
309 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200310 final SurfaceSession session = new SurfaceSession(mSurface);
311
312 // Keep a reference to it such that it doesn't get destroyed when finalized.
Robert Carre625fcf2017-09-01 12:36:28 -0700313 mChildSurfaceControl = new SurfaceControl.Builder(session)
314 .setName(mTitle + " - task-snapshot-surface")
315 .setSize(buffer.getWidth(), buffer.getHeight())
316 .setFormat(buffer.getFormat())
317 .build();
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200318 Surface surface = new Surface();
319 surface.copyFrom(mChildSurfaceControl);
320
321 // Clip off ugly navigation bar.
322 final Rect crop = calculateSnapshotCrop();
323 final Rect frame = calculateSnapshotFrame(crop);
324 SurfaceControl.openTransaction();
325 try {
326 // We can just show the surface here as it will still be hidden as the parent is
327 // still hidden.
328 mChildSurfaceControl.show();
329 mChildSurfaceControl.setWindowCrop(crop);
330 mChildSurfaceControl.setPosition(frame.left, frame.top);
Matthew Ngcb7ac672017-07-21 17:27:42 -0700331
332 // Scale the mismatch dimensions to fill the task bounds
333 final float scale = 1 / mSnapshot.getScale();
334 mChildSurfaceControl.setMatrix(scale, 0, 0, scale);
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200335 } finally {
336 SurfaceControl.closeTransaction();
337 }
338 surface.attachAndQueueBuffer(buffer);
339 surface.release();
340
341 final Canvas c = mSurface.lockCanvas(null);
342 drawBackgroundAndBars(c, frame);
343 mSurface.unlockCanvasAndPost(c);
Jorim Jaggi2f24b652017-01-18 02:17:37 +0100344 mSurface.release();
Jorim Jaggi02886a82016-12-06 09:10:06 -0800345 }
346
Matthew Ngcb7ac672017-07-21 17:27:42 -0700347 /**
348 * Calculates the snapshot crop in snapshot coordinate space.
349 *
350 * @return crop rect in snapshot coordinate space.
351 */
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100352 @VisibleForTesting
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200353 Rect calculateSnapshotCrop() {
354 final Rect rect = new Rect();
355 rect.set(0, 0, mSnapshot.getSnapshot().getWidth(), mSnapshot.getSnapshot().getHeight());
356 final Rect insets = mSnapshot.getContentInsets();
357
358 // Let's remove all system decorations except the status bar, but only if the task is at the
359 // very top of the screen.
Adrian Roos98a146d2017-11-29 16:39:44 +0100360 final boolean isTop = mTaskBounds.top == 0 && mFrame.top == 0;
Matthew Ngcb7ac672017-07-21 17:27:42 -0700361 rect.inset((int) (insets.left * mSnapshot.getScale()),
Adrian Roos98a146d2017-11-29 16:39:44 +0100362 isTop ? 0 : (int) (insets.top * mSnapshot.getScale()),
Matthew Ngcb7ac672017-07-21 17:27:42 -0700363 (int) (insets.right * mSnapshot.getScale()),
364 (int) (insets.bottom * mSnapshot.getScale()));
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200365 return rect;
366 }
367
Matthew Ngcb7ac672017-07-21 17:27:42 -0700368 /**
369 * Calculates the snapshot frame in window coordinate space from crop.
370 *
371 * @param crop rect that is in snapshot coordinate space.
372 */
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200373 @VisibleForTesting
374 Rect calculateSnapshotFrame(Rect crop) {
375 final Rect frame = new Rect(crop);
Matthew Ngcb7ac672017-07-21 17:27:42 -0700376 final float scale = mSnapshot.getScale();
377
378 // Rescale the frame from snapshot to window coordinate space
379 frame.scale(1 / scale);
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200380
381 // By default, offset it to to top/left corner
Matthew Ngcb7ac672017-07-21 17:27:42 -0700382 frame.offsetTo((int) (-crop.left / scale), (int) (-crop.top / scale));
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200383
384 // However, we also need to make space for the navigation bar on the left side.
385 final int colorViewLeftInset = getColorViewLeftInset(mStableInsets.left,
386 mContentInsets.left);
387 frame.offset(colorViewLeftInset, 0);
388 return frame;
389 }
390
391 @VisibleForTesting
392 void drawBackgroundAndBars(Canvas c, Rect frame) {
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200393 final int statusBarHeight = mSystemBarBackgroundPainter.getStatusBarColorViewHeight();
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200394 final boolean fillHorizontally = c.getWidth() > frame.right;
395 final boolean fillVertically = c.getHeight() > frame.bottom;
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100396 if (fillHorizontally) {
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200397 c.drawRect(frame.right, alpha(mStatusBarColor) == 0xFF ? statusBarHeight : 0,
398 c.getWidth(), fillVertically
399 ? frame.bottom
400 : c.getHeight(),
401 mBackgroundPaint);
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100402 }
403 if (fillVertically) {
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200404 c.drawRect(0, frame.bottom, c.getWidth(), c.getHeight(), mBackgroundPaint);
405 }
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200406 mSystemBarBackgroundPainter.drawDecors(c, frame);
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100407 }
408
Jorim Jaggi02886a82016-12-06 09:10:06 -0800409 private void reportDrawn() {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800410 try {
411 mSession.finishDrawing(mWindow);
412 } catch (RemoteException e) {
413 // Local call.
414 }
415 }
416
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100417 private static Handler sHandler = new Handler(Looper.getMainLooper()) {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800418
419 @Override
420 public void handleMessage(Message msg) {
421 switch (msg.what) {
422 case MSG_REPORT_DRAW:
423 final boolean hasDrawn;
424 final TaskSnapshotSurface surface = (TaskSnapshotSurface) msg.obj;
425 synchronized (surface.mService.mWindowMap) {
426 hasDrawn = surface.mHasDrawn;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800427 }
428 if (hasDrawn) {
429 surface.reportDrawn();
430 }
431 break;
432 }
433 }
434 };
435
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200436 @VisibleForTesting
437 static class Window extends BaseIWindow {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800438
439 private TaskSnapshotSurface mOuter;
Jorim Jaggi38d44ec2017-06-14 16:04:59 -0700440
Jorim Jaggi02886a82016-12-06 09:10:06 -0800441 public void setOuter(TaskSnapshotSurface outer) {
442 mOuter = outer;
443 }
444
445 @Override
446 public void resized(Rect frame, Rect overscanInsets, Rect contentInsets, Rect visibleInsets,
Andrii Kulian44607962017-03-16 11:06:24 -0700447 Rect stableInsets, Rect outsets, boolean reportDraw,
448 MergedConfiguration mergedConfiguration, Rect backDropFrame, boolean forceLayout,
Adrian Roos5c6b6222017-11-07 17:36:10 +0100449 boolean alwaysConsumeNavBar, int displayId,
450 DisplayCutout.ParcelableWrapper displayCutout) {
Jorim Jaggi38d44ec2017-06-14 16:04:59 -0700451 if (mergedConfiguration != null && mOuter != null
452 && mOuter.mOrientationOnCreation
453 != mergedConfiguration.getMergedConfiguration().orientation) {
454
455 // The orientation of the screen is changing. We better remove the snapshot ASAP as
456 // we are going to wait on the new window in any case to unfreeze the screen, and
457 // the starting window is not needed anymore.
458 sHandler.post(mOuter::remove);
459 }
Jorim Jaggi02886a82016-12-06 09:10:06 -0800460 if (reportDraw) {
461 sHandler.obtainMessage(MSG_REPORT_DRAW, mOuter).sendToTarget();
462 }
463 }
464 }
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200465
466 /**
467 * Helper class to draw the background of the system bars in regions the task snapshot isn't
468 * filling the window.
469 */
470 static class SystemBarBackgroundPainter {
471
472 private final Rect mContentInsets = new Rect();
473 private final Rect mStableInsets = new Rect();
474 private final Paint mStatusBarPaint = new Paint();
475 private final Paint mNavigationBarPaint = new Paint();
476 private final int mStatusBarColor;
477 private final int mNavigationBarColor;
478 private final int mWindowFlags;
479 private final int mWindowPrivateFlags;
480 private final int mSysUiVis;
481
482 SystemBarBackgroundPainter( int windowFlags, int windowPrivateFlags, int sysUiVis,
483 int statusBarColor, int navigationBarColor) {
484 mWindowFlags = windowFlags;
485 mWindowPrivateFlags = windowPrivateFlags;
486 mSysUiVis = sysUiVis;
487 final Context context = ActivityThread.currentActivityThread().getSystemUiContext();
488 mStatusBarColor = DecorView.calculateStatusBarColor(windowFlags,
489 context.getColor(R.color.system_bar_background_semi_transparent),
490 statusBarColor);
491 mNavigationBarColor = navigationBarColor;
492 mStatusBarPaint.setColor(mStatusBarColor);
493 mNavigationBarPaint.setColor(navigationBarColor);
494 }
495
496 void setInsets(Rect contentInsets, Rect stableInsets) {
497 mContentInsets.set(contentInsets);
498 mStableInsets.set(stableInsets);
499 }
500
501 int getStatusBarColorViewHeight() {
502 final boolean forceStatusBarBackground =
503 (mWindowPrivateFlags & PRIVATE_FLAG_FORCE_DRAW_STATUS_BAR_BACKGROUND) != 0;
504 if (STATUS_BAR_COLOR_VIEW_ATTRIBUTES.isVisible(
505 mSysUiVis, mStatusBarColor, mWindowFlags, forceStatusBarBackground)) {
506 return getColorViewTopInset(mStableInsets.top, mContentInsets.top);
507 } else {
508 return 0;
509 }
510 }
511
512 private boolean isNavigationBarColorViewVisible() {
513 return NAVIGATION_BAR_COLOR_VIEW_ATTRIBUTES.isVisible(
514 mSysUiVis, mNavigationBarColor, mWindowFlags, false /* force */);
515 }
516
517 void drawDecors(Canvas c, @Nullable Rect alreadyDrawnFrame) {
518 drawStatusBarBackground(c, alreadyDrawnFrame, getStatusBarColorViewHeight());
519 drawNavigationBarBackground(c);
520 }
521
522 @VisibleForTesting
523 void drawStatusBarBackground(Canvas c, @Nullable Rect alreadyDrawnFrame,
524 int statusBarHeight) {
Winson Chung51f42d22018-02-01 14:59:38 -0800525 if (statusBarHeight > 0 && Color.alpha(mStatusBarColor) != 0
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200526 && (alreadyDrawnFrame == null || c.getWidth() > alreadyDrawnFrame.right)) {
527 final int rightInset = DecorView.getColorViewRightInset(mStableInsets.right,
528 mContentInsets.right);
529 final int left = alreadyDrawnFrame != null ? alreadyDrawnFrame.right : 0;
530 c.drawRect(left, 0, c.getWidth() - rightInset, statusBarHeight, mStatusBarPaint);
531 }
532 }
533
534 @VisibleForTesting
535 void drawNavigationBarBackground(Canvas c) {
536 final Rect navigationBarRect = new Rect();
537 getNavigationBarRect(c.getWidth(), c.getHeight(), mStableInsets, mContentInsets,
538 navigationBarRect);
539 final boolean visible = isNavigationBarColorViewVisible();
Winson Chung51f42d22018-02-01 14:59:38 -0800540 if (visible && Color.alpha(mNavigationBarColor) != 0 && !navigationBarRect.isEmpty()) {
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200541 c.drawRect(navigationBarRect, mNavigationBarPaint);
542 }
543 }
544 }
Jorim Jaggi02886a82016-12-06 09:10:06 -0800545}