blob: 15878f6e414a351ac8375352954436ef7dcdf101 [file] [log] [blame]
Jorim Jaggi02886a82016-12-06 09:10:06 -08001/*
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
17package com.android.server.wm;
18
19import static android.app.ActivityManager.ENABLE_TASK_SNAPSHOTS;
Jorim Jaggi02886a82016-12-06 09:10:06 -080020
21import android.annotation.Nullable;
22import android.app.ActivityManager.StackId;
Jorim Jaggie2c77f92016-12-29 14:57:22 +010023import android.app.ActivityManager.TaskSnapshot;
Jorim Jaggi02886a82016-12-06 09:10:06 -080024import android.graphics.GraphicBuffer;
Jorim Jaggif9084ec2017-01-16 13:16:59 +010025import android.os.Environment;
Jorim Jaggi02886a82016-12-06 09:10:06 -080026import android.util.ArraySet;
27import android.view.WindowManagerPolicy.StartingSurface;
28
29import com.android.internal.annotations.VisibleForTesting;
30
Jorim Jaggi10abe2f2017-01-03 16:44:46 +010031import java.io.PrintWriter;
32
Jorim Jaggi02886a82016-12-06 09:10:06 -080033/**
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 */
46class TaskSnapshotController {
47
48 private final WindowManagerService mService;
Jorim Jaggi02886a82016-12-06 09:10:06 -080049
Jorim Jaggi7361bab2017-01-16 17:17:58 +010050 private final TaskSnapshotCache mCache;
Jorim Jaggif9084ec2017-01-16 13:16:59 +010051 private final TaskSnapshotPersister mPersister = new TaskSnapshotPersister(
52 Environment::getDataSystemCeDirectory);
53 private final TaskSnapshotLoader mLoader = new TaskSnapshotLoader(mPersister);
Jorim Jaggi02886a82016-12-06 09:10:06 -080054 private final ArraySet<Task> mTmpTasks = new ArraySet<>();
55
56 TaskSnapshotController(WindowManagerService service) {
57 mService = service;
Jorim Jaggi7361bab2017-01-16 17:17:58 +010058 mCache = new TaskSnapshotCache(mService, mLoader);
Jorim Jaggi02886a82016-12-06 09:10:06 -080059 }
60
Jorim Jaggif9084ec2017-01-16 13:16:59 +010061 void systemReady() {
62 mPersister.start();
63 }
64
Jorim Jaggi02886a82016-12-06 09:10:06 -080065 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 Jaggie2c77f92016-12-29 14:57:22 +010078 final TaskSnapshot snapshot = snapshotTask(task);
79 if (snapshot != null) {
80 mCache.putSnapshot(task, snapshot);
Jorim Jaggif9084ec2017-01-16 13:16:59 +010081 mPersister.persistSnapshot(task.mTaskId, task.mUserId, snapshot);
Jorim Jaggifb9d78a2017-01-05 18:57:12 +010082 if (task.getController() != null) {
83 task.getController().reportSnapshotChanged(snapshot);
84 }
Jorim Jaggi02886a82016-12-06 09:10:06 -080085 }
86 }
87 }
88
Jorim Jaggi7361bab2017-01-16 17:17:58 +010089 /**
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 Jaggi02886a82016-12-06 09:10:06 -080095 }
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 Jaggie2c77f92016-12-29 14:57:22 +0100106 private TaskSnapshot snapshotTask(Task task) {
Jorim Jaggi10abe2f2017-01-03 16:44:46 +0100107 final AppWindowToken top = task.getTopChild();
Jorim Jaggi02886a82016-12-06 09:10:06 -0800108 if (top == null) {
109 return null;
110 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +0100111 final GraphicBuffer buffer = top.mDisplayContent.screenshotApplicationsToBuffer(top.token,
112 -1, -1, false, 1.0f, false, true);
113 if (buffer == null) {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800114 return null;
115 }
Jorim Jaggie2c77f92016-12-29 14:57:22 +0100116 return new TaskSnapshot(buffer, top.getConfiguration().orientation,
117 top.findMainWindow().mStableInsets);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800118 }
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 Jaggi10abe2f2017-01-03 16:44:46 +0100140
141 /**
142 * Called when an {@link AppWindowToken} has been removed.
143 */
144 void onAppRemoved(AppWindowToken wtoken) {
Jorim Jaggi7361bab2017-01-16 17:17:58 +0100145 mCache.onAppRemoved(wtoken);
Jorim Jaggi10abe2f2017-01-03 16:44:46 +0100146 }
147
148 /**
149 * Called when the process of an {@link AppWindowToken} has died.
150 */
151 void onAppDied(AppWindowToken wtoken) {
Jorim Jaggi7361bab2017-01-16 17:17:58 +0100152 mCache.onAppDied(wtoken);
Jorim Jaggi10abe2f2017-01-03 16:44:46 +0100153 }
154
Jorim Jaggif9084ec2017-01-16 13:16:59 +0100155 void notifyTaskRemovedFromRecents(int taskId, int userId) {
Jorim Jaggi7361bab2017-01-16 17:17:58 +0100156 mCache.onTaskRemoved(taskId);
Jorim Jaggif9084ec2017-01-16 13:16:59 +0100157 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 Jaggi10abe2f2017-01-03 16:44:46 +0100167 void dump(PrintWriter pw, String prefix) {
168 mCache.dump(pw, prefix);
169 }
Jorim Jaggi02886a82016-12-06 09:10:06 -0800170}