Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.wm; |
| 18 | |
Matthew Ng | cb7ac67 | 2017-07-21 17:27:42 -0700 | [diff] [blame] | 19 | import static com.android.server.wm.TaskSnapshotPersister.DISABLE_FULL_SIZED_BITMAPS; |
chaviw | fbe47df | 2017-11-10 16:14:49 -0800 | [diff] [blame] | 20 | import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT; |
Winson Chung | 3e13ef8 | 2017-06-29 12:41:14 -0700 | [diff] [blame] | 21 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME; |
| 22 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM; |
| 23 | |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 24 | import android.annotation.Nullable; |
Jorim Jaggi | 3d73260 | 2017-02-22 17:56:40 +0100 | [diff] [blame] | 25 | import android.app.ActivityManager; |
Jorim Jaggi | e2c77f9 | 2016-12-29 14:57:22 +0100 | [diff] [blame] | 26 | import android.app.ActivityManager.TaskSnapshot; |
Keyvan Amiri | e681cec | 2017-06-05 22:48:26 -0700 | [diff] [blame] | 27 | import android.content.pm.PackageManager; |
Jorim Jaggi | 6aead1c | 2017-05-23 15:07:44 +0200 | [diff] [blame] | 28 | import android.graphics.Bitmap; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 29 | import android.graphics.GraphicBuffer; |
Winson Chung | 02378b4 | 2018-06-04 16:40:09 -0700 | [diff] [blame] | 30 | import android.graphics.PixelFormat; |
John Reck | 32f140aa6 | 2018-10-04 15:08:24 -0700 | [diff] [blame] | 31 | import android.graphics.RecordingCanvas; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 32 | import android.graphics.Rect; |
John Reck | 32f140aa6 | 2018-10-04 15:08:24 -0700 | [diff] [blame] | 33 | import android.graphics.RenderNode; |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 34 | import android.os.Environment; |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 35 | import android.os.Handler; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 36 | import android.util.ArraySet; |
Winson Chung | 3e13ef8 | 2017-06-29 12:41:14 -0700 | [diff] [blame] | 37 | import android.util.Slog; |
chaviw | fbe47df | 2017-11-10 16:14:49 -0800 | [diff] [blame] | 38 | import android.view.SurfaceControl; |
Winson Chung | 9109276 | 2017-05-24 14:08:48 -0700 | [diff] [blame] | 39 | import android.view.ThreadedRenderer; |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 40 | import android.view.WindowManager.LayoutParams; |
Guang Zhu | 09486a3 | 2017-06-06 03:27:44 +0000 | [diff] [blame] | 41 | |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 42 | import com.android.internal.annotations.VisibleForTesting; |
Winson Chung | 51f42d2 | 2018-02-01 14:59:38 -0800 | [diff] [blame] | 43 | import com.android.internal.graphics.ColorUtils; |
Adrian Roos | e99bc05 | 2017-11-20 17:55:31 +0100 | [diff] [blame] | 44 | import com.android.server.policy.WindowManagerPolicy.ScreenOffListener; |
| 45 | import com.android.server.policy.WindowManagerPolicy.StartingSurface; |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 46 | import com.android.server.wm.TaskSnapshotSurface.SystemBarBackgroundPainter; |
Jorim Jaggi | 817ebdd | 2018-03-26 15:46:01 +0200 | [diff] [blame] | 47 | import com.android.server.wm.utils.InsetUtils; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 48 | |
Adrian Roos | e99bc05 | 2017-11-20 17:55:31 +0100 | [diff] [blame] | 49 | import com.google.android.collect.Sets; |
| 50 | |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 51 | import java.io.PrintWriter; |
| 52 | |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 53 | /** |
| 54 | * When an app token becomes invisible, we take a snapshot (bitmap) of the corresponding task and |
| 55 | * put it into our cache. Internally we use gralloc buffers to be able to draw them wherever we |
| 56 | * like without any copying. |
| 57 | * <p> |
| 58 | * System applications may retrieve a snapshot to represent the current state of a task, and draw |
| 59 | * them in their own process. |
| 60 | * <p> |
| 61 | * When we task becomes visible again, we show a starting window with the snapshot as the content to |
| 62 | * make app transitions more responsive. |
| 63 | * <p> |
| 64 | * To access this class, acquire the global window manager lock. |
| 65 | */ |
| 66 | class TaskSnapshotController { |
Winson Chung | 3e13ef8 | 2017-06-29 12:41:14 -0700 | [diff] [blame] | 67 | private static final String TAG = TAG_WITH_CLASS_NAME ? "TaskSnapshotController" : TAG_WM; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 68 | |
Jorim Jaggi | 8f4fe6e | 2017-03-14 18:21:40 +0100 | [diff] [blame] | 69 | /** |
| 70 | * Return value for {@link #getSnapshotMode}: We are allowed to take a real screenshot to be |
| 71 | * used as the snapshot. |
| 72 | */ |
| 73 | @VisibleForTesting |
| 74 | static final int SNAPSHOT_MODE_REAL = 0; |
| 75 | |
| 76 | /** |
| 77 | * Return value for {@link #getSnapshotMode}: We are not allowed to take a real screenshot but |
| 78 | * we should try to use the app theme to create a dummy representation of the app. |
| 79 | */ |
| 80 | @VisibleForTesting |
| 81 | static final int SNAPSHOT_MODE_APP_THEME = 1; |
| 82 | |
| 83 | /** |
| 84 | * Return value for {@link #getSnapshotMode}: We aren't allowed to take any snapshot. |
| 85 | */ |
| 86 | @VisibleForTesting |
| 87 | static final int SNAPSHOT_MODE_NONE = 2; |
| 88 | |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 89 | private final WindowManagerService mService; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 90 | |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 91 | private final TaskSnapshotCache mCache; |
Kevin | 716a9db | 2018-12-18 16:53:39 -0800 | [diff] [blame] | 92 | private final TaskSnapshotPersister mPersister; |
| 93 | private final TaskSnapshotLoader mLoader; |
Winson Chung | 23aa7b1 | 2018-02-01 11:41:43 -0800 | [diff] [blame] | 94 | private final ArraySet<Task> mSkipClosingAppSnapshotTasks = new ArraySet<>(); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 95 | private final ArraySet<Task> mTmpTasks = new ArraySet<>(); |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 96 | private final Handler mHandler = new Handler(); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 97 | |
chaviw | 23ee71c | 2017-12-18 11:29:41 -0800 | [diff] [blame] | 98 | private final Rect mTmpRect = new Rect(); |
| 99 | |
Keyvan Amiri | e681cec | 2017-06-05 22:48:26 -0700 | [diff] [blame] | 100 | /** |
| 101 | * Flag indicating whether we are running on an Android TV device. |
| 102 | */ |
| 103 | private final boolean mIsRunningOnTv; |
| 104 | |
Juho Ha | f5dd7cc | 2017-06-15 11:38:56 +0900 | [diff] [blame] | 105 | /** |
| 106 | * Flag indicating whether we are running on an IoT device. |
| 107 | */ |
| 108 | private final boolean mIsRunningOnIoT; |
| 109 | |
Matthew Ng | 9537819 | 2017-08-16 11:57:00 -0700 | [diff] [blame] | 110 | /** |
| 111 | * Flag indicating whether we are running on an Android Wear device. |
| 112 | */ |
| 113 | private final boolean mIsRunningOnWear; |
| 114 | |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 115 | TaskSnapshotController(WindowManagerService service) { |
| 116 | mService = service; |
Kevin | 716a9db | 2018-12-18 16:53:39 -0800 | [diff] [blame] | 117 | mPersister = new TaskSnapshotPersister(mService, Environment::getDataSystemCeDirectory); |
| 118 | mLoader = new TaskSnapshotLoader(mPersister); |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 119 | mCache = new TaskSnapshotCache(mService, mLoader); |
Keyvan Amiri | e681cec | 2017-06-05 22:48:26 -0700 | [diff] [blame] | 120 | mIsRunningOnTv = mService.mContext.getPackageManager().hasSystemFeature( |
| 121 | PackageManager.FEATURE_LEANBACK); |
Juho Ha | f5dd7cc | 2017-06-15 11:38:56 +0900 | [diff] [blame] | 122 | mIsRunningOnIoT = mService.mContext.getPackageManager().hasSystemFeature( |
| 123 | PackageManager.FEATURE_EMBEDDED); |
Matthew Ng | 9537819 | 2017-08-16 11:57:00 -0700 | [diff] [blame] | 124 | mIsRunningOnWear = mService.mContext.getPackageManager().hasSystemFeature( |
| 125 | PackageManager.FEATURE_WATCH); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 128 | void systemReady() { |
| 129 | mPersister.start(); |
| 130 | } |
| 131 | |
lumark | 588a3e8 | 2018-07-20 18:53:54 +0800 | [diff] [blame] | 132 | void onTransitionStarting(DisplayContent displayContent) { |
| 133 | handleClosingApps(displayContent.mClosingApps); |
Jorim Jaggi | 8b702ed | 2017-01-20 16:59:03 +0100 | [diff] [blame] | 134 | } |
| 135 | |
Jorim Jaggi | 8b702ed | 2017-01-20 16:59:03 +0100 | [diff] [blame] | 136 | /** |
| 137 | * Called when the visibility of an app changes outside of the regular app transition flow. |
| 138 | */ |
| 139 | void notifyAppVisibilityChanged(AppWindowToken appWindowToken, boolean visible) { |
Jorim Jaggi | 8b702ed | 2017-01-20 16:59:03 +0100 | [diff] [blame] | 140 | if (!visible) { |
| 141 | handleClosingApps(Sets.newArraySet(appWindowToken)); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | private void handleClosingApps(ArraySet<AppWindowToken> closingApps) { |
Keyvan Amiri | e681cec | 2017-06-05 22:48:26 -0700 | [diff] [blame] | 146 | if (shouldDisableSnapshots()) { |
Jorim Jaggi | 3d73260 | 2017-02-22 17:56:40 +0100 | [diff] [blame] | 147 | return; |
| 148 | } |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 149 | |
| 150 | // We need to take a snapshot of the task if and only if all activities of the task are |
| 151 | // either closing or hidden. |
Jorim Jaggi | 8b702ed | 2017-01-20 16:59:03 +0100 | [diff] [blame] | 152 | getClosingTasks(closingApps, mTmpTasks); |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 153 | snapshotTasks(mTmpTasks); |
Winson Chung | 23aa7b1 | 2018-02-01 11:41:43 -0800 | [diff] [blame] | 154 | mSkipClosingAppSnapshotTasks.clear(); |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 155 | } |
| 156 | |
Winson Chung | 23aa7b1 | 2018-02-01 11:41:43 -0800 | [diff] [blame] | 157 | /** |
| 158 | * Adds the given {@param tasks} to the list of tasks which should not have their snapshots |
| 159 | * taken upon the next processing of the set of closing apps. The caller is responsible for |
| 160 | * calling {@link #snapshotTasks} to ensure that the task has an up-to-date snapshot. |
| 161 | */ |
| 162 | @VisibleForTesting |
| 163 | void addSkipClosingAppSnapshotTasks(ArraySet<Task> tasks) { |
| 164 | mSkipClosingAppSnapshotTasks.addAll(tasks); |
| 165 | } |
| 166 | |
| 167 | void snapshotTasks(ArraySet<Task> tasks) { |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 168 | for (int i = tasks.size() - 1; i >= 0; i--) { |
| 169 | final Task task = tasks.valueAt(i); |
Jorim Jaggi | 8f4fe6e | 2017-03-14 18:21:40 +0100 | [diff] [blame] | 170 | final int mode = getSnapshotMode(task); |
| 171 | final TaskSnapshot snapshot; |
| 172 | switch (mode) { |
| 173 | case SNAPSHOT_MODE_NONE: |
| 174 | continue; |
| 175 | case SNAPSHOT_MODE_APP_THEME: |
| 176 | snapshot = drawAppThemeSnapshot(task); |
| 177 | break; |
| 178 | case SNAPSHOT_MODE_REAL: |
| 179 | snapshot = snapshotTask(task); |
| 180 | break; |
| 181 | default: |
| 182 | snapshot = null; |
| 183 | break; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 184 | } |
Jorim Jaggi | e2c77f9 | 2016-12-29 14:57:22 +0100 | [diff] [blame] | 185 | if (snapshot != null) { |
Winson Chung | 3e13ef8 | 2017-06-29 12:41:14 -0700 | [diff] [blame] | 186 | final GraphicBuffer buffer = snapshot.getSnapshot(); |
| 187 | if (buffer.getWidth() == 0 || buffer.getHeight() == 0) { |
| 188 | buffer.destroy(); |
| 189 | Slog.e(TAG, "Invalid task snapshot dimensions " + buffer.getWidth() + "x" |
| 190 | + buffer.getHeight()); |
| 191 | } else { |
| 192 | mCache.putSnapshot(task, snapshot); |
| 193 | mPersister.persistSnapshot(task.mTaskId, task.mUserId, snapshot); |
Yunfan Chen | 0e7aff9 | 2018-12-05 16:35:32 -0800 | [diff] [blame] | 194 | task.onSnapshotChanged(snapshot); |
Jorim Jaggi | fb9d78a | 2017-01-05 18:57:12 +0100 | [diff] [blame] | 195 | } |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 200 | /** |
| 201 | * Retrieves a snapshot. If {@param restoreFromDisk} equals {@code true}, DO HOLD THE WINDOW |
| 202 | * MANAGER LOCK WHEN CALLING THIS METHOD! |
| 203 | */ |
Jorim Jaggi | 35e3f53 | 2017-03-17 17:06:50 +0100 | [diff] [blame] | 204 | @Nullable TaskSnapshot getSnapshot(int taskId, int userId, boolean restoreFromDisk, |
| 205 | boolean reducedResolution) { |
Matthew Ng | cb7ac67 | 2017-07-21 17:27:42 -0700 | [diff] [blame] | 206 | return mCache.getSnapshot(taskId, userId, restoreFromDisk, reducedResolution |
| 207 | || DISABLE_FULL_SIZED_BITMAPS); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Creates a starting surface for {@param token} with {@param snapshot}. DO NOT HOLD THE WINDOW |
| 212 | * MANAGER LOCK WHEN CALLING THIS METHOD! |
| 213 | */ |
| 214 | StartingSurface createStartingSurface(AppWindowToken token, |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 215 | TaskSnapshot snapshot) { |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 216 | return TaskSnapshotSurface.create(mService, token, snapshot); |
| 217 | } |
| 218 | |
Issei Suzuki | 88e2142 | 2018-07-24 16:29:09 +0200 | [diff] [blame] | 219 | /** |
| 220 | * Find the window for a given task to take a snapshot. Top child of the task is usually the one |
| 221 | * we're looking for, but during app transitions, trampoline activities can appear in the |
| 222 | * children, which should be ignored. |
| 223 | */ |
| 224 | @Nullable private AppWindowToken findAppTokenForSnapshot(Task task) { |
| 225 | for (int i = task.getChildCount() - 1; i >= 0; --i) { |
| 226 | final AppWindowToken appWindowToken = task.getChildAt(i); |
| 227 | if (appWindowToken == null || !appWindowToken.isSurfaceShowing() |
| 228 | || appWindowToken.findMainWindow() == null) { |
| 229 | continue; |
| 230 | } |
| 231 | final boolean hasVisibleChild = appWindowToken.forAllWindows( |
| 232 | // Ensure at least one window for the top app is visible before attempting to |
| 233 | // take a screenshot. Visible here means that the WSA surface is shown and has |
| 234 | // an alpha greater than 0. |
| 235 | ws -> ws.mWinAnimator != null && ws.mWinAnimator.getShown() |
| 236 | && ws.mWinAnimator.mLastAlpha > 0f, true /* traverseTopToBottom */); |
| 237 | if (hasVisibleChild) { |
| 238 | return appWindowToken; |
| 239 | } |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 240 | } |
Issei Suzuki | 88e2142 | 2018-07-24 16:29:09 +0200 | [diff] [blame] | 241 | return null; |
| 242 | } |
| 243 | |
| 244 | @Nullable private TaskSnapshot snapshotTask(Task task) { |
chaviw | fbe47df | 2017-11-10 16:14:49 -0800 | [diff] [blame] | 245 | if (!mService.mPolicy.isScreenOn()) { |
| 246 | if (DEBUG_SCREENSHOT) { |
| 247 | Slog.i(TAG_WM, "Attempted to take screenshot while display was off."); |
| 248 | } |
| 249 | return null; |
| 250 | } |
| 251 | if (task.getSurfaceControl() == null) { |
chaviw | 7f1fa99 | 2018-01-10 13:52:12 -0800 | [diff] [blame] | 252 | if (DEBUG_SCREENSHOT) { |
Issei Suzuki | 88e2142 | 2018-07-24 16:29:09 +0200 | [diff] [blame] | 253 | Slog.w(TAG_WM, "Failed to take screenshot. No surface control for " + task); |
chaviw | 7f1fa99 | 2018-01-10 13:52:12 -0800 | [diff] [blame] | 254 | } |
| 255 | return null; |
| 256 | } |
| 257 | |
Issei Suzuki | 88e2142 | 2018-07-24 16:29:09 +0200 | [diff] [blame] | 258 | final AppWindowToken appWindowToken = findAppTokenForSnapshot(task); |
| 259 | if (appWindowToken == null) { |
chaviw | 7f1fa99 | 2018-01-10 13:52:12 -0800 | [diff] [blame] | 260 | if (DEBUG_SCREENSHOT) { |
| 261 | Slog.w(TAG_WM, "Failed to take screenshot. No visible windows for " + task); |
| 262 | } |
| 263 | return null; |
| 264 | } |
Issei Suzuki | 88e2142 | 2018-07-24 16:29:09 +0200 | [diff] [blame] | 265 | if (appWindowToken.hasCommittedReparentToAnimationLeash()) { |
| 266 | if (DEBUG_SCREENSHOT) { |
| 267 | Slog.w(TAG_WM, "Failed to take screenshot. App is animating " + appWindowToken); |
| 268 | } |
| 269 | return null; |
| 270 | } |
chaviw | 7f1fa99 | 2018-01-10 13:52:12 -0800 | [diff] [blame] | 271 | |
Matthew Ng | cb7ac67 | 2017-07-21 17:27:42 -0700 | [diff] [blame] | 272 | final boolean isLowRamDevice = ActivityManager.isLowRamDeviceStatic(); |
Kevin | 716a9db | 2018-12-18 16:53:39 -0800 | [diff] [blame] | 273 | final float scaleFraction = isLowRamDevice ? mPersister.getReducedScale() : 1f; |
chaviw | 23ee71c | 2017-12-18 11:29:41 -0800 | [diff] [blame] | 274 | task.getBounds(mTmpRect); |
| 275 | mTmpRect.offsetTo(0, 0); |
chaviw | fbe47df | 2017-11-10 16:14:49 -0800 | [diff] [blame] | 276 | |
Issei Suzuki | 88e2142 | 2018-07-24 16:29:09 +0200 | [diff] [blame] | 277 | final WindowState mainWindow = appWindowToken.findMainWindow(); |
| 278 | if (mainWindow == null) { |
| 279 | Slog.w(TAG_WM, "Failed to take screenshot. No main window for " + task); |
| 280 | return null; |
| 281 | } |
Peiyong Lin | bf9d677 | 2019-03-21 20:53:42 +0000 | [diff] [blame] | 282 | final GraphicBuffer buffer = SurfaceControl.captureLayers( |
| 283 | task.getSurfaceControl().getHandle(), mTmpRect, scaleFraction); |
Juho Ha | d864b44 | 2017-06-12 20:15:31 +0900 | [diff] [blame] | 284 | if (buffer == null || buffer.getWidth() <= 1 || buffer.getHeight() <= 1) { |
chaviw | fbe47df | 2017-11-10 16:14:49 -0800 | [diff] [blame] | 285 | if (DEBUG_SCREENSHOT) { |
chaviw | 7f1fa99 | 2018-01-10 13:52:12 -0800 | [diff] [blame] | 286 | Slog.w(TAG_WM, "Failed to take screenshot for " + task); |
chaviw | fbe47df | 2017-11-10 16:14:49 -0800 | [diff] [blame] | 287 | } |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 288 | return null; |
| 289 | } |
Issei Suzuki | 88e2142 | 2018-07-24 16:29:09 +0200 | [diff] [blame] | 290 | final boolean isWindowTranslucent = mainWindow.getAttrs().format != PixelFormat.OPAQUE; |
Winson Chung | 48b2565 | 2018-10-22 14:04:30 -0700 | [diff] [blame] | 291 | return new TaskSnapshot(appWindowToken.mActivityComponent, buffer, |
| 292 | appWindowToken.getConfiguration().orientation, getInsets(mainWindow), |
| 293 | isLowRamDevice /* reduced */, scaleFraction /* scale */, true /* isRealSnapshot */, |
| 294 | task.getWindowingMode(), getSystemUiVisibility(task), |
Issei Suzuki | 88e2142 | 2018-07-24 16:29:09 +0200 | [diff] [blame] | 295 | !appWindowToken.fillsParent() || isWindowTranslucent); |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 296 | } |
| 297 | |
Keyvan Amiri | e681cec | 2017-06-05 22:48:26 -0700 | [diff] [blame] | 298 | private boolean shouldDisableSnapshots() { |
Jorim Jaggi | e7d2b85 | 2017-08-28 17:55:15 +0200 | [diff] [blame] | 299 | return mIsRunningOnWear || mIsRunningOnTv || mIsRunningOnIoT; |
Keyvan Amiri | e681cec | 2017-06-05 22:48:26 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Jorim Jaggi | 817ebdd | 2018-03-26 15:46:01 +0200 | [diff] [blame] | 302 | private Rect getInsets(WindowState state) { |
Adrian Roos | 20e0789 | 2018-02-23 19:12:01 +0100 | [diff] [blame] | 303 | // XXX(b/72757033): These are insets relative to the window frame, but we're really |
| 304 | // interested in the insets relative to the task bounds. |
chaviw | 9c81e63 | 2018-07-31 11:17:52 -0700 | [diff] [blame] | 305 | final Rect insets = minRect(state.getContentInsets(), state.getStableInsets()); |
Jorim Jaggi | 817ebdd | 2018-03-26 15:46:01 +0200 | [diff] [blame] | 306 | InsetUtils.addInsets(insets, state.mAppToken.getLetterboxInsets()); |
Adrian Roos | 20e0789 | 2018-02-23 19:12:01 +0100 | [diff] [blame] | 307 | return insets; |
| 308 | } |
Adrian Roos | 98a146d | 2017-11-29 16:39:44 +0100 | [diff] [blame] | 309 | |
Adrian Roos | 20e0789 | 2018-02-23 19:12:01 +0100 | [diff] [blame] | 310 | private Rect minRect(Rect rect1, Rect rect2) { |
| 311 | return new Rect(Math.min(rect1.left, rect2.left), |
| 312 | Math.min(rect1.top, rect2.top), |
| 313 | Math.min(rect1.right, rect2.right), |
| 314 | Math.min(rect1.bottom, rect2.bottom)); |
| 315 | } |
Adrian Roos | 98a146d | 2017-11-29 16:39:44 +0100 | [diff] [blame] | 316 | |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 317 | /** |
| 318 | * Retrieves all closing tasks based on the list of closing apps during an app transition. |
| 319 | */ |
| 320 | @VisibleForTesting |
| 321 | void getClosingTasks(ArraySet<AppWindowToken> closingApps, ArraySet<Task> outClosingTasks) { |
| 322 | outClosingTasks.clear(); |
| 323 | for (int i = closingApps.size() - 1; i >= 0; i--) { |
| 324 | final AppWindowToken atoken = closingApps.valueAt(i); |
Bryce Lee | 6d41026 | 2017-02-28 15:30:17 -0800 | [diff] [blame] | 325 | final Task task = atoken.getTask(); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 326 | |
| 327 | // If the task of the app is not visible anymore, it means no other app in that task |
| 328 | // is opening. Thus, the task is closing. |
Winson Chung | 23aa7b1 | 2018-02-01 11:41:43 -0800 | [diff] [blame] | 329 | if (task != null && !task.isVisible() && !mSkipClosingAppSnapshotTasks.contains(task)) { |
Bryce Lee | 6d41026 | 2017-02-28 15:30:17 -0800 | [diff] [blame] | 330 | outClosingTasks.add(task); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | } |
| 334 | |
Jorim Jaggi | 0fe7ce96 | 2017-02-22 16:45:48 +0100 | [diff] [blame] | 335 | @VisibleForTesting |
Jorim Jaggi | 8f4fe6e | 2017-03-14 18:21:40 +0100 | [diff] [blame] | 336 | int getSnapshotMode(Task task) { |
Jorim Jaggi | 0fe7ce96 | 2017-02-22 16:45:48 +0100 | [diff] [blame] | 337 | final AppWindowToken topChild = task.getTopChild(); |
Winson Chung | bff1837 | 2018-04-27 16:12:54 -0700 | [diff] [blame] | 338 | if (!task.isActivityTypeStandardOrUndefined() && !task.isActivityTypeAssistant()) { |
Jorim Jaggi | 8f4fe6e | 2017-03-14 18:21:40 +0100 | [diff] [blame] | 339 | return SNAPSHOT_MODE_NONE; |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 340 | } else if (topChild != null && topChild.shouldUseAppThemeSnapshot()) { |
Jorim Jaggi | 8f4fe6e | 2017-03-14 18:21:40 +0100 | [diff] [blame] | 341 | return SNAPSHOT_MODE_APP_THEME; |
| 342 | } else { |
| 343 | return SNAPSHOT_MODE_REAL; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * If we are not allowed to take a real screenshot, this attempts to represent the app as best |
| 349 | * as possible by using the theme's window background. |
| 350 | */ |
| 351 | private TaskSnapshot drawAppThemeSnapshot(Task task) { |
| 352 | final AppWindowToken topChild = task.getTopChild(); |
| 353 | if (topChild == null) { |
| 354 | return null; |
| 355 | } |
| 356 | final WindowState mainWindow = topChild.findMainWindow(); |
| 357 | if (mainWindow == null) { |
| 358 | return null; |
| 359 | } |
Winson Chung | 51f42d2 | 2018-02-01 14:59:38 -0800 | [diff] [blame] | 360 | final int color = ColorUtils.setAlphaComponent( |
| 361 | task.getTaskDescription().getBackgroundColor(), 255); |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 362 | final int statusBarColor = task.getTaskDescription().getStatusBarColor(); |
| 363 | final int navigationBarColor = task.getTaskDescription().getNavigationBarColor(); |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 364 | final LayoutParams attrs = mainWindow.getAttrs(); |
| 365 | final SystemBarBackgroundPainter decorPainter = new SystemBarBackgroundPainter(attrs.flags, |
| 366 | attrs.privateFlags, attrs.systemUiVisibility, statusBarColor, navigationBarColor); |
Winson Chung | 9109276 | 2017-05-24 14:08:48 -0700 | [diff] [blame] | 367 | final int width = mainWindow.getFrameLw().width(); |
| 368 | final int height = mainWindow.getFrameLw().height(); |
| 369 | |
| 370 | final RenderNode node = RenderNode.create("TaskSnapshotController", null); |
| 371 | node.setLeftTopRightBottom(0, 0, width, height); |
| 372 | node.setClipToBounds(false); |
John Reck | 32f140aa6 | 2018-10-04 15:08:24 -0700 | [diff] [blame] | 373 | final RecordingCanvas c = node.start(width, height); |
Winson Chung | 9109276 | 2017-05-24 14:08:48 -0700 | [diff] [blame] | 374 | c.drawColor(color); |
chaviw | 9c81e63 | 2018-07-31 11:17:52 -0700 | [diff] [blame] | 375 | decorPainter.setInsets(mainWindow.getContentInsets(), mainWindow.getStableInsets()); |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 376 | decorPainter.drawDecors(c, null /* statusBarExcludeFrame */); |
Winson Chung | 9109276 | 2017-05-24 14:08:48 -0700 | [diff] [blame] | 377 | node.end(c); |
| 378 | final Bitmap hwBitmap = ThreadedRenderer.createHardwareBitmap(node, width, height); |
Winson Chung | 6267f99 | 2017-10-06 14:01:10 -0700 | [diff] [blame] | 379 | if (hwBitmap == null) { |
| 380 | return null; |
| 381 | } |
Winson Chung | 02378b4 | 2018-06-04 16:40:09 -0700 | [diff] [blame] | 382 | // Note, the app theme snapshot is never translucent because we enforce a non-translucent |
| 383 | // color above |
Winson Chung | 48b2565 | 2018-10-22 14:04:30 -0700 | [diff] [blame] | 384 | return new TaskSnapshot(topChild.mActivityComponent, hwBitmap.createGraphicBufferHandle(), |
chaviw | 9c81e63 | 2018-07-31 11:17:52 -0700 | [diff] [blame] | 385 | topChild.getConfiguration().orientation, mainWindow.getStableInsets(), |
Winson Chung | f3e412e | 2018-03-08 11:07:40 -0800 | [diff] [blame] | 386 | ActivityManager.isLowRamDeviceStatic() /* reduced */, 1.0f /* scale */, |
Winson Chung | 173020c | 2018-05-04 15:36:47 -0700 | [diff] [blame] | 387 | false /* isRealSnapshot */, task.getWindowingMode(), getSystemUiVisibility(task), |
Winson Chung | 02378b4 | 2018-06-04 16:40:09 -0700 | [diff] [blame] | 388 | false); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 389 | } |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 390 | |
| 391 | /** |
| 392 | * Called when an {@link AppWindowToken} has been removed. |
| 393 | */ |
| 394 | void onAppRemoved(AppWindowToken wtoken) { |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 395 | mCache.onAppRemoved(wtoken); |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | /** |
| 399 | * Called when the process of an {@link AppWindowToken} has died. |
| 400 | */ |
| 401 | void onAppDied(AppWindowToken wtoken) { |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 402 | mCache.onAppDied(wtoken); |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 403 | } |
| 404 | |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 405 | void notifyTaskRemovedFromRecents(int taskId, int userId) { |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 406 | mCache.onTaskRemoved(taskId); |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 407 | mPersister.onTaskRemovedFromRecents(taskId, userId); |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * See {@link TaskSnapshotPersister#removeObsoleteFiles} |
| 412 | */ |
| 413 | void removeObsoleteTaskFiles(ArraySet<Integer> persistentTaskIds, int[] runningUserIds) { |
| 414 | mPersister.removeObsoleteFiles(persistentTaskIds, runningUserIds); |
| 415 | } |
| 416 | |
Jorim Jaggi | a41b729 | 2017-05-11 23:50:34 +0200 | [diff] [blame] | 417 | /** |
| 418 | * Temporarily pauses/unpauses persisting of task snapshots. |
| 419 | * |
| 420 | * @param paused Whether task snapshot persisting should be paused. |
| 421 | */ |
| 422 | void setPersisterPaused(boolean paused) { |
| 423 | mPersister.setPaused(paused); |
| 424 | } |
| 425 | |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 426 | /** |
| 427 | * Called when screen is being turned off. |
| 428 | */ |
| 429 | void screenTurningOff(ScreenOffListener listener) { |
Keyvan Amiri | e681cec | 2017-06-05 22:48:26 -0700 | [diff] [blame] | 430 | if (shouldDisableSnapshots()) { |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 431 | listener.onScreenOff(); |
| 432 | return; |
| 433 | } |
| 434 | |
| 435 | // We can't take a snapshot when screen is off, so take a snapshot now! |
| 436 | mHandler.post(() -> { |
| 437 | try { |
Wale Ogunwale | db485de | 2018-10-29 09:47:07 -0700 | [diff] [blame] | 438 | synchronized (mService.mGlobalLock) { |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 439 | mTmpTasks.clear(); |
| 440 | mService.mRoot.forAllTasks(task -> { |
| 441 | if (task.isVisible()) { |
| 442 | mTmpTasks.add(task); |
| 443 | } |
| 444 | }); |
| 445 | snapshotTasks(mTmpTasks); |
| 446 | } |
| 447 | } finally { |
| 448 | listener.onScreenOff(); |
| 449 | } |
| 450 | }); |
| 451 | } |
| 452 | |
Winson Chung | 173020c | 2018-05-04 15:36:47 -0700 | [diff] [blame] | 453 | /** |
| 454 | * @return The SystemUI visibility flags for the top fullscreen window in the given |
| 455 | * {@param task}. |
| 456 | */ |
| 457 | private int getSystemUiVisibility(Task task) { |
| 458 | final AppWindowToken topFullscreenToken = task.getTopFullscreenAppToken(); |
| 459 | final WindowState topFullscreenWindow = topFullscreenToken != null |
| 460 | ? topFullscreenToken.getTopFullscreenWindow() |
| 461 | : null; |
| 462 | if (topFullscreenWindow != null) { |
| 463 | return topFullscreenWindow.getSystemUiVisibility(); |
| 464 | } |
| 465 | return 0; |
| 466 | } |
| 467 | |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 468 | void dump(PrintWriter pw, String prefix) { |
| 469 | mCache.dump(pw, prefix); |
| 470 | } |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 471 | } |