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 | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 20 | |
| 21 | import android.annotation.Nullable; |
Jorim Jaggi | 3d73260 | 2017-02-22 17:56:40 +0100 | [diff] [blame] | 22 | import android.app.ActivityManager; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 23 | import android.app.ActivityManager.StackId; |
Jorim Jaggi | e2c77f9 | 2016-12-29 14:57:22 +0100 | [diff] [blame] | 24 | import android.app.ActivityManager.TaskSnapshot; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 25 | import android.graphics.GraphicBuffer; |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 26 | import android.os.Environment; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 27 | import android.util.ArraySet; |
| 28 | import android.view.WindowManagerPolicy.StartingSurface; |
| 29 | |
Jorim Jaggi | 8b702ed | 2017-01-20 16:59:03 +0100 | [diff] [blame] | 30 | import com.google.android.collect.Sets; |
| 31 | |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 32 | import com.android.internal.annotations.VisibleForTesting; |
| 33 | |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 34 | import java.io.PrintWriter; |
| 35 | |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 36 | /** |
| 37 | * When an app token becomes invisible, we take a snapshot (bitmap) of the corresponding task and |
| 38 | * put it into our cache. Internally we use gralloc buffers to be able to draw them wherever we |
| 39 | * like without any copying. |
| 40 | * <p> |
| 41 | * System applications may retrieve a snapshot to represent the current state of a task, and draw |
| 42 | * them in their own process. |
| 43 | * <p> |
| 44 | * When we task becomes visible again, we show a starting window with the snapshot as the content to |
| 45 | * make app transitions more responsive. |
| 46 | * <p> |
| 47 | * To access this class, acquire the global window manager lock. |
| 48 | */ |
| 49 | class TaskSnapshotController { |
| 50 | |
| 51 | private final WindowManagerService mService; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 52 | |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 53 | private final TaskSnapshotCache mCache; |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 54 | private final TaskSnapshotPersister mPersister = new TaskSnapshotPersister( |
| 55 | Environment::getDataSystemCeDirectory); |
| 56 | private final TaskSnapshotLoader mLoader = new TaskSnapshotLoader(mPersister); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 57 | private final ArraySet<Task> mTmpTasks = new ArraySet<>(); |
| 58 | |
| 59 | TaskSnapshotController(WindowManagerService service) { |
| 60 | mService = service; |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 61 | mCache = new TaskSnapshotCache(mService, mLoader); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 64 | void systemReady() { |
| 65 | mPersister.start(); |
| 66 | } |
| 67 | |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 68 | void onTransitionStarting() { |
Jorim Jaggi | 8b702ed | 2017-01-20 16:59:03 +0100 | [diff] [blame] | 69 | handleClosingApps(mService.mClosingApps); |
| 70 | } |
| 71 | |
Jorim Jaggi | 8b702ed | 2017-01-20 16:59:03 +0100 | [diff] [blame] | 72 | /** |
| 73 | * Called when the visibility of an app changes outside of the regular app transition flow. |
| 74 | */ |
| 75 | void notifyAppVisibilityChanged(AppWindowToken appWindowToken, boolean visible) { |
Jorim Jaggi | 8b702ed | 2017-01-20 16:59:03 +0100 | [diff] [blame] | 76 | if (!visible) { |
| 77 | handleClosingApps(Sets.newArraySet(appWindowToken)); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | private void handleClosingApps(ArraySet<AppWindowToken> closingApps) { |
Jorim Jaggi | 3d73260 | 2017-02-22 17:56:40 +0100 | [diff] [blame] | 82 | if (!ENABLE_TASK_SNAPSHOTS || ActivityManager.isLowRamDeviceStatic()) { |
| 83 | return; |
| 84 | } |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 85 | |
| 86 | // We need to take a snapshot of the task if and only if all activities of the task are |
| 87 | // either closing or hidden. |
Jorim Jaggi | 8b702ed | 2017-01-20 16:59:03 +0100 | [diff] [blame] | 88 | getClosingTasks(closingApps, mTmpTasks); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 89 | for (int i = mTmpTasks.size() - 1; i >= 0; i--) { |
| 90 | final Task task = mTmpTasks.valueAt(i); |
| 91 | if (!canSnapshotTask(task)) { |
| 92 | continue; |
| 93 | } |
Jorim Jaggi | e2c77f9 | 2016-12-29 14:57:22 +0100 | [diff] [blame] | 94 | final TaskSnapshot snapshot = snapshotTask(task); |
| 95 | if (snapshot != null) { |
| 96 | mCache.putSnapshot(task, snapshot); |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 97 | mPersister.persistSnapshot(task.mTaskId, task.mUserId, snapshot); |
Jorim Jaggi | fb9d78a | 2017-01-05 18:57:12 +0100 | [diff] [blame] | 98 | if (task.getController() != null) { |
| 99 | task.getController().reportSnapshotChanged(snapshot); |
| 100 | } |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 105 | /** |
| 106 | * Retrieves a snapshot. If {@param restoreFromDisk} equals {@code true}, DO HOLD THE WINDOW |
| 107 | * MANAGER LOCK WHEN CALLING THIS METHOD! |
| 108 | */ |
Jorim Jaggi | 35e3f53 | 2017-03-17 17:06:50 +0100 | [diff] [blame] | 109 | @Nullable TaskSnapshot getSnapshot(int taskId, int userId, boolean restoreFromDisk, |
| 110 | boolean reducedResolution) { |
| 111 | return mCache.getSnapshot(taskId, userId, restoreFromDisk, reducedResolution); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Creates a starting surface for {@param token} with {@param snapshot}. DO NOT HOLD THE WINDOW |
| 116 | * MANAGER LOCK WHEN CALLING THIS METHOD! |
| 117 | */ |
| 118 | StartingSurface createStartingSurface(AppWindowToken token, |
| 119 | GraphicBuffer snapshot) { |
| 120 | return TaskSnapshotSurface.create(mService, token, snapshot); |
| 121 | } |
| 122 | |
Jorim Jaggi | e2c77f9 | 2016-12-29 14:57:22 +0100 | [diff] [blame] | 123 | private TaskSnapshot snapshotTask(Task task) { |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 124 | final AppWindowToken top = task.getTopChild(); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 125 | if (top == null) { |
| 126 | return null; |
| 127 | } |
Jorim Jaggi | 6a7a859 | 2017-01-12 00:44:33 +0100 | [diff] [blame] | 128 | final GraphicBuffer buffer = top.mDisplayContent.screenshotApplicationsToBuffer(top.token, |
| 129 | -1, -1, false, 1.0f, false, true); |
| 130 | if (buffer == null) { |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 131 | return null; |
| 132 | } |
Jorim Jaggi | e2c77f9 | 2016-12-29 14:57:22 +0100 | [diff] [blame] | 133 | return new TaskSnapshot(buffer, top.getConfiguration().orientation, |
Jorim Jaggi | 35e3f53 | 2017-03-17 17:06:50 +0100 | [diff] [blame] | 134 | top.findMainWindow().mStableInsets, false /* reduced */, 1f /* scale */); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Retrieves all closing tasks based on the list of closing apps during an app transition. |
| 139 | */ |
| 140 | @VisibleForTesting |
| 141 | void getClosingTasks(ArraySet<AppWindowToken> closingApps, ArraySet<Task> outClosingTasks) { |
| 142 | outClosingTasks.clear(); |
| 143 | for (int i = closingApps.size() - 1; i >= 0; i--) { |
| 144 | final AppWindowToken atoken = closingApps.valueAt(i); |
Bryce Lee | 6d41026 | 2017-02-28 15:30:17 -0800 | [diff] [blame] | 145 | final Task task = atoken.getTask(); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 146 | |
| 147 | // If the task of the app is not visible anymore, it means no other app in that task |
| 148 | // is opening. Thus, the task is closing. |
Bryce Lee | 6d41026 | 2017-02-28 15:30:17 -0800 | [diff] [blame] | 149 | if (task != null && !task.isVisible()) { |
| 150 | outClosingTasks.add(task); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
Jorim Jaggi | 0fe7ce96 | 2017-02-22 16:45:48 +0100 | [diff] [blame^] | 155 | @VisibleForTesting |
| 156 | boolean canSnapshotTask(Task task) { |
| 157 | // TODO: Figure out what happens when snapshots are disabled. Can we draw a splash screen |
| 158 | // instead? |
| 159 | final AppWindowToken topChild = task.getTopChild(); |
| 160 | return !StackId.isHomeOrRecentsStack(task.mStack.mStackId) |
| 161 | && topChild != null && !topChild.shouldDisablePreviewScreenshots(); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 162 | } |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 163 | |
| 164 | /** |
| 165 | * Called when an {@link AppWindowToken} has been removed. |
| 166 | */ |
| 167 | void onAppRemoved(AppWindowToken wtoken) { |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 168 | mCache.onAppRemoved(wtoken); |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Called when the process of an {@link AppWindowToken} has died. |
| 173 | */ |
| 174 | void onAppDied(AppWindowToken wtoken) { |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 175 | mCache.onAppDied(wtoken); |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 176 | } |
| 177 | |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 178 | void notifyTaskRemovedFromRecents(int taskId, int userId) { |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 179 | mCache.onTaskRemoved(taskId); |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 180 | mPersister.onTaskRemovedFromRecents(taskId, userId); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * See {@link TaskSnapshotPersister#removeObsoleteFiles} |
| 185 | */ |
| 186 | void removeObsoleteTaskFiles(ArraySet<Integer> persistentTaskIds, int[] runningUserIds) { |
| 187 | mPersister.removeObsoleteFiles(persistentTaskIds, runningUserIds); |
| 188 | } |
| 189 | |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 190 | void dump(PrintWriter pw, String prefix) { |
| 191 | mCache.dump(pw, prefix); |
| 192 | } |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 193 | } |