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