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 | |
| 19 | import static android.app.ActivityManager.ENABLE_TASK_SNAPSHOTS; |
Jorim Jaggi | 8f4fe6e | 2017-03-14 18:21:40 +0100 | [diff] [blame] | 20 | import static android.graphics.GraphicBuffer.USAGE_HW_TEXTURE; |
| 21 | import static android.graphics.GraphicBuffer.USAGE_SW_READ_NEVER; |
| 22 | import static android.graphics.GraphicBuffer.USAGE_SW_WRITE_RARELY; |
| 23 | import static android.graphics.PixelFormat.RGBA_8888; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 24 | |
| 25 | import android.annotation.Nullable; |
Jorim Jaggi | 3d73260 | 2017-02-22 17:56:40 +0100 | [diff] [blame] | 26 | import android.app.ActivityManager; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 27 | import android.app.ActivityManager.StackId; |
Jorim Jaggi | e2c77f9 | 2016-12-29 14:57:22 +0100 | [diff] [blame] | 28 | import android.app.ActivityManager.TaskSnapshot; |
Jorim Jaggi | 8f4fe6e | 2017-03-14 18:21:40 +0100 | [diff] [blame] | 29 | import android.graphics.Canvas; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 30 | import android.graphics.GraphicBuffer; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 31 | import android.graphics.Rect; |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 32 | import android.os.Environment; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 33 | import android.util.ArraySet; |
| 34 | import android.view.WindowManagerPolicy.StartingSurface; |
| 35 | |
Jorim Jaggi | 8b702ed | 2017-01-20 16:59:03 +0100 | [diff] [blame] | 36 | import com.google.android.collect.Sets; |
| 37 | |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 38 | import com.android.internal.annotations.VisibleForTesting; |
| 39 | |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 40 | import java.io.PrintWriter; |
| 41 | |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 42 | /** |
| 43 | * When an app token becomes invisible, we take a snapshot (bitmap) of the corresponding task and |
| 44 | * put it into our cache. Internally we use gralloc buffers to be able to draw them wherever we |
| 45 | * like without any copying. |
| 46 | * <p> |
| 47 | * System applications may retrieve a snapshot to represent the current state of a task, and draw |
| 48 | * them in their own process. |
| 49 | * <p> |
| 50 | * When we task becomes visible again, we show a starting window with the snapshot as the content to |
| 51 | * make app transitions more responsive. |
| 52 | * <p> |
| 53 | * To access this class, acquire the global window manager lock. |
| 54 | */ |
| 55 | class TaskSnapshotController { |
| 56 | |
Jorim Jaggi | 8f4fe6e | 2017-03-14 18:21:40 +0100 | [diff] [blame] | 57 | /** |
| 58 | * Return value for {@link #getSnapshotMode}: We are allowed to take a real screenshot to be |
| 59 | * used as the snapshot. |
| 60 | */ |
| 61 | @VisibleForTesting |
| 62 | static final int SNAPSHOT_MODE_REAL = 0; |
| 63 | |
| 64 | /** |
| 65 | * Return value for {@link #getSnapshotMode}: We are not allowed to take a real screenshot but |
| 66 | * we should try to use the app theme to create a dummy representation of the app. |
| 67 | */ |
| 68 | @VisibleForTesting |
| 69 | static final int SNAPSHOT_MODE_APP_THEME = 1; |
| 70 | |
| 71 | /** |
| 72 | * Return value for {@link #getSnapshotMode}: We aren't allowed to take any snapshot. |
| 73 | */ |
| 74 | @VisibleForTesting |
| 75 | static final int SNAPSHOT_MODE_NONE = 2; |
| 76 | |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 77 | private final WindowManagerService mService; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 78 | |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 79 | private final TaskSnapshotCache mCache; |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 80 | private final TaskSnapshotPersister mPersister = new TaskSnapshotPersister( |
| 81 | Environment::getDataSystemCeDirectory); |
| 82 | private final TaskSnapshotLoader mLoader = new TaskSnapshotLoader(mPersister); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 83 | private final ArraySet<Task> mTmpTasks = new ArraySet<>(); |
| 84 | |
| 85 | TaskSnapshotController(WindowManagerService service) { |
| 86 | mService = service; |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 87 | mCache = new TaskSnapshotCache(mService, mLoader); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 90 | void systemReady() { |
| 91 | mPersister.start(); |
| 92 | } |
| 93 | |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 94 | void onTransitionStarting() { |
Jorim Jaggi | 8b702ed | 2017-01-20 16:59:03 +0100 | [diff] [blame] | 95 | handleClosingApps(mService.mClosingApps); |
| 96 | } |
| 97 | |
Jorim Jaggi | 8b702ed | 2017-01-20 16:59:03 +0100 | [diff] [blame] | 98 | /** |
| 99 | * Called when the visibility of an app changes outside of the regular app transition flow. |
| 100 | */ |
| 101 | void notifyAppVisibilityChanged(AppWindowToken appWindowToken, boolean visible) { |
Jorim Jaggi | 8b702ed | 2017-01-20 16:59:03 +0100 | [diff] [blame] | 102 | if (!visible) { |
| 103 | handleClosingApps(Sets.newArraySet(appWindowToken)); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | private void handleClosingApps(ArraySet<AppWindowToken> closingApps) { |
Jorim Jaggi | 3d73260 | 2017-02-22 17:56:40 +0100 | [diff] [blame] | 108 | if (!ENABLE_TASK_SNAPSHOTS || ActivityManager.isLowRamDeviceStatic()) { |
| 109 | return; |
| 110 | } |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 111 | |
| 112 | // We need to take a snapshot of the task if and only if all activities of the task are |
| 113 | // either closing or hidden. |
Jorim Jaggi | 8b702ed | 2017-01-20 16:59:03 +0100 | [diff] [blame] | 114 | getClosingTasks(closingApps, mTmpTasks); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 115 | for (int i = mTmpTasks.size() - 1; i >= 0; i--) { |
| 116 | final Task task = mTmpTasks.valueAt(i); |
Jorim Jaggi | 8f4fe6e | 2017-03-14 18:21:40 +0100 | [diff] [blame] | 117 | final int mode = getSnapshotMode(task); |
| 118 | final TaskSnapshot snapshot; |
| 119 | switch (mode) { |
| 120 | case SNAPSHOT_MODE_NONE: |
| 121 | continue; |
| 122 | case SNAPSHOT_MODE_APP_THEME: |
| 123 | snapshot = drawAppThemeSnapshot(task); |
| 124 | break; |
| 125 | case SNAPSHOT_MODE_REAL: |
| 126 | snapshot = snapshotTask(task); |
| 127 | break; |
| 128 | default: |
| 129 | snapshot = null; |
| 130 | break; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 131 | } |
Jorim Jaggi | e2c77f9 | 2016-12-29 14:57:22 +0100 | [diff] [blame] | 132 | if (snapshot != null) { |
| 133 | mCache.putSnapshot(task, snapshot); |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 134 | mPersister.persistSnapshot(task.mTaskId, task.mUserId, snapshot); |
Jorim Jaggi | fb9d78a | 2017-01-05 18:57:12 +0100 | [diff] [blame] | 135 | if (task.getController() != null) { |
| 136 | task.getController().reportSnapshotChanged(snapshot); |
| 137 | } |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 142 | /** |
| 143 | * Retrieves a snapshot. If {@param restoreFromDisk} equals {@code true}, DO HOLD THE WINDOW |
| 144 | * MANAGER LOCK WHEN CALLING THIS METHOD! |
| 145 | */ |
Jorim Jaggi | 35e3f53 | 2017-03-17 17:06:50 +0100 | [diff] [blame] | 146 | @Nullable TaskSnapshot getSnapshot(int taskId, int userId, boolean restoreFromDisk, |
| 147 | boolean reducedResolution) { |
| 148 | return mCache.getSnapshot(taskId, userId, restoreFromDisk, reducedResolution); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Creates a starting surface for {@param token} with {@param snapshot}. DO NOT HOLD THE WINDOW |
| 153 | * MANAGER LOCK WHEN CALLING THIS METHOD! |
| 154 | */ |
| 155 | StartingSurface createStartingSurface(AppWindowToken token, |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 156 | TaskSnapshot snapshot) { |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 157 | return TaskSnapshotSurface.create(mService, token, snapshot); |
| 158 | } |
| 159 | |
Jorim Jaggi | e2c77f9 | 2016-12-29 14:57:22 +0100 | [diff] [blame] | 160 | private TaskSnapshot snapshotTask(Task task) { |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 161 | final AppWindowToken top = task.getTopChild(); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 162 | if (top == null) { |
| 163 | return null; |
| 164 | } |
Jorim Jaggi | dd5986e | 2017-04-11 15:50:19 -0700 | [diff] [blame^] | 165 | final WindowState mainWindow = top.findMainWindow(); |
| 166 | if (mainWindow == null) { |
| 167 | return null; |
| 168 | } |
Jorim Jaggi | 6a7a859 | 2017-01-12 00:44:33 +0100 | [diff] [blame] | 169 | final GraphicBuffer buffer = top.mDisplayContent.screenshotApplicationsToBuffer(top.token, |
| 170 | -1, -1, false, 1.0f, false, true); |
| 171 | if (buffer == null) { |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 172 | return null; |
| 173 | } |
Jorim Jaggi | e2c77f9 | 2016-12-29 14:57:22 +0100 | [diff] [blame] | 174 | return new TaskSnapshot(buffer, top.getConfiguration().orientation, |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 175 | minRect(mainWindow.mContentInsets, mainWindow.mStableInsets), false /* reduced */, |
| 176 | 1f /* scale */); |
| 177 | } |
| 178 | |
| 179 | private Rect minRect(Rect rect1, Rect rect2) { |
| 180 | return new Rect(Math.min(rect1.left, rect2.left), |
| 181 | Math.min(rect1.top, rect2.top), |
| 182 | Math.min(rect1.right, rect2.right), |
| 183 | Math.min(rect1.bottom, rect2.bottom)); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Retrieves all closing tasks based on the list of closing apps during an app transition. |
| 188 | */ |
| 189 | @VisibleForTesting |
| 190 | void getClosingTasks(ArraySet<AppWindowToken> closingApps, ArraySet<Task> outClosingTasks) { |
| 191 | outClosingTasks.clear(); |
| 192 | for (int i = closingApps.size() - 1; i >= 0; i--) { |
| 193 | final AppWindowToken atoken = closingApps.valueAt(i); |
Bryce Lee | 6d41026 | 2017-02-28 15:30:17 -0800 | [diff] [blame] | 194 | final Task task = atoken.getTask(); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 195 | |
| 196 | // If the task of the app is not visible anymore, it means no other app in that task |
| 197 | // is opening. Thus, the task is closing. |
Bryce Lee | 6d41026 | 2017-02-28 15:30:17 -0800 | [diff] [blame] | 198 | if (task != null && !task.isVisible()) { |
| 199 | outClosingTasks.add(task); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
Jorim Jaggi | 0fe7ce96 | 2017-02-22 16:45:48 +0100 | [diff] [blame] | 204 | @VisibleForTesting |
Jorim Jaggi | 8f4fe6e | 2017-03-14 18:21:40 +0100 | [diff] [blame] | 205 | int getSnapshotMode(Task task) { |
Jorim Jaggi | 0fe7ce96 | 2017-02-22 16:45:48 +0100 | [diff] [blame] | 206 | final AppWindowToken topChild = task.getTopChild(); |
Jorim Jaggi | 8f4fe6e | 2017-03-14 18:21:40 +0100 | [diff] [blame] | 207 | if (StackId.isHomeOrRecentsStack(task.mStack.mStackId)) { |
| 208 | return SNAPSHOT_MODE_NONE; |
| 209 | } else if (topChild != null && topChild.shouldDisablePreviewScreenshots()) { |
| 210 | return SNAPSHOT_MODE_APP_THEME; |
| 211 | } else { |
| 212 | return SNAPSHOT_MODE_REAL; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * If we are not allowed to take a real screenshot, this attempts to represent the app as best |
| 218 | * as possible by using the theme's window background. |
| 219 | */ |
| 220 | private TaskSnapshot drawAppThemeSnapshot(Task task) { |
| 221 | final AppWindowToken topChild = task.getTopChild(); |
| 222 | if (topChild == null) { |
| 223 | return null; |
| 224 | } |
| 225 | final WindowState mainWindow = topChild.findMainWindow(); |
| 226 | if (mainWindow == null) { |
| 227 | return null; |
| 228 | } |
| 229 | final int color = task.getTaskDescription().getBackgroundColor(); |
| 230 | final GraphicBuffer buffer = GraphicBuffer.create(mainWindow.getFrameLw().width(), |
| 231 | mainWindow.getFrameLw().height(), |
| 232 | RGBA_8888, USAGE_HW_TEXTURE | USAGE_SW_WRITE_RARELY | USAGE_SW_READ_NEVER); |
| 233 | if (buffer == null) { |
| 234 | return null; |
| 235 | } |
| 236 | final Canvas c = buffer.lockCanvas(); |
| 237 | c.drawColor(color); |
| 238 | buffer.unlockCanvasAndPost(c); |
| 239 | return new TaskSnapshot(buffer, topChild.getConfiguration().orientation, |
| 240 | mainWindow.mStableInsets, false /* reduced */, 1.0f /* scale */); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 241 | } |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 242 | |
| 243 | /** |
| 244 | * Called when an {@link AppWindowToken} has been removed. |
| 245 | */ |
| 246 | void onAppRemoved(AppWindowToken wtoken) { |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 247 | mCache.onAppRemoved(wtoken); |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Called when the process of an {@link AppWindowToken} has died. |
| 252 | */ |
| 253 | void onAppDied(AppWindowToken wtoken) { |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 254 | mCache.onAppDied(wtoken); |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 255 | } |
| 256 | |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 257 | void notifyTaskRemovedFromRecents(int taskId, int userId) { |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 258 | mCache.onTaskRemoved(taskId); |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 259 | mPersister.onTaskRemovedFromRecents(taskId, userId); |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * See {@link TaskSnapshotPersister#removeObsoleteFiles} |
| 264 | */ |
| 265 | void removeObsoleteTaskFiles(ArraySet<Integer> persistentTaskIds, int[] runningUserIds) { |
| 266 | mPersister.removeObsoleteFiles(persistentTaskIds, runningUserIds); |
| 267 | } |
| 268 | |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 269 | void dump(PrintWriter pw, String prefix) { |
| 270 | mCache.dump(pw, prefix); |
| 271 | } |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 272 | } |