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