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