blob: 5995bba2c4ff6b4aa9a8d7aea492d00cd6924a2c [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;
Jorim Jaggi3d732602017-02-22 17:56:40 +010022import android.app.ActivityManager;
Jorim Jaggi02886a82016-12-06 09:10:06 -080023import android.app.ActivityManager.StackId;
Jorim Jaggie2c77f92016-12-29 14:57:22 +010024import android.app.ActivityManager.TaskSnapshot;
Jorim Jaggi02886a82016-12-06 09:10:06 -080025import android.graphics.GraphicBuffer;
Jorim Jaggif9084ec2017-01-16 13:16:59 +010026import android.os.Environment;
Jorim Jaggi02886a82016-12-06 09:10:06 -080027import android.util.ArraySet;
28import android.view.WindowManagerPolicy.StartingSurface;
29
Jorim Jaggi8b702ed2017-01-20 16:59:03 +010030import com.google.android.collect.Sets;
31
Jorim Jaggi02886a82016-12-06 09:10:06 -080032import com.android.internal.annotations.VisibleForTesting;
33
Jorim Jaggi10abe2f2017-01-03 16:44:46 +010034import java.io.PrintWriter;
35
Jorim Jaggi02886a82016-12-06 09:10:06 -080036/**
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 */
49class TaskSnapshotController {
50
51 private final WindowManagerService mService;
Jorim Jaggi02886a82016-12-06 09:10:06 -080052
Jorim Jaggi7361bab2017-01-16 17:17:58 +010053 private final TaskSnapshotCache mCache;
Jorim Jaggif9084ec2017-01-16 13:16:59 +010054 private final TaskSnapshotPersister mPersister = new TaskSnapshotPersister(
55 Environment::getDataSystemCeDirectory);
56 private final TaskSnapshotLoader mLoader = new TaskSnapshotLoader(mPersister);
Jorim Jaggi02886a82016-12-06 09:10:06 -080057 private final ArraySet<Task> mTmpTasks = new ArraySet<>();
58
59 TaskSnapshotController(WindowManagerService service) {
60 mService = service;
Jorim Jaggi7361bab2017-01-16 17:17:58 +010061 mCache = new TaskSnapshotCache(mService, mLoader);
Jorim Jaggi02886a82016-12-06 09:10:06 -080062 }
63
Jorim Jaggif9084ec2017-01-16 13:16:59 +010064 void systemReady() {
65 mPersister.start();
66 }
67
Jorim Jaggi02886a82016-12-06 09:10:06 -080068 void onTransitionStarting() {
Jorim Jaggi8b702ed2017-01-20 16:59:03 +010069 handleClosingApps(mService.mClosingApps);
70 }
71
Jorim Jaggi8b702ed2017-01-20 16:59:03 +010072 /**
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 Jaggi8b702ed2017-01-20 16:59:03 +010076 if (!visible) {
77 handleClosingApps(Sets.newArraySet(appWindowToken));
78 }
79 }
80
81 private void handleClosingApps(ArraySet<AppWindowToken> closingApps) {
Jorim Jaggi3d732602017-02-22 17:56:40 +010082 if (!ENABLE_TASK_SNAPSHOTS || ActivityManager.isLowRamDeviceStatic()) {
83 return;
84 }
Jorim Jaggi02886a82016-12-06 09:10:06 -080085
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 Jaggi8b702ed2017-01-20 16:59:03 +010088 getClosingTasks(closingApps, mTmpTasks);
Jorim Jaggi02886a82016-12-06 09:10:06 -080089 for (int i = mTmpTasks.size() - 1; i >= 0; i--) {
90 final Task task = mTmpTasks.valueAt(i);
91 if (!canSnapshotTask(task)) {
92 continue;
93 }
Jorim Jaggie2c77f92016-12-29 14:57:22 +010094 final TaskSnapshot snapshot = snapshotTask(task);
95 if (snapshot != null) {
96 mCache.putSnapshot(task, snapshot);
Jorim Jaggif9084ec2017-01-16 13:16:59 +010097 mPersister.persistSnapshot(task.mTaskId, task.mUserId, snapshot);
Jorim Jaggifb9d78a2017-01-05 18:57:12 +010098 if (task.getController() != null) {
99 task.getController().reportSnapshotChanged(snapshot);
100 }
Jorim Jaggi02886a82016-12-06 09:10:06 -0800101 }
102 }
103 }
104
Jorim Jaggi7361bab2017-01-16 17:17:58 +0100105 /**
106 * Retrieves a snapshot. If {@param restoreFromDisk} equals {@code true}, DO HOLD THE WINDOW
107 * MANAGER LOCK WHEN CALLING THIS METHOD!
108 */
109 @Nullable TaskSnapshot getSnapshot(int taskId, int userId, boolean restoreFromDisk) {
110 return mCache.getSnapshot(taskId, userId, restoreFromDisk);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800111 }
112
113 /**
114 * Creates a starting surface for {@param token} with {@param snapshot}. DO NOT HOLD THE WINDOW
115 * MANAGER LOCK WHEN CALLING THIS METHOD!
116 */
117 StartingSurface createStartingSurface(AppWindowToken token,
118 GraphicBuffer snapshot) {
119 return TaskSnapshotSurface.create(mService, token, snapshot);
120 }
121
Jorim Jaggie2c77f92016-12-29 14:57:22 +0100122 private TaskSnapshot snapshotTask(Task task) {
Jorim Jaggi10abe2f2017-01-03 16:44:46 +0100123 final AppWindowToken top = task.getTopChild();
Jorim Jaggi02886a82016-12-06 09:10:06 -0800124 if (top == null) {
125 return null;
126 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +0100127 final GraphicBuffer buffer = top.mDisplayContent.screenshotApplicationsToBuffer(top.token,
128 -1, -1, false, 1.0f, false, true);
129 if (buffer == null) {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800130 return null;
131 }
Jorim Jaggie2c77f92016-12-29 14:57:22 +0100132 return new TaskSnapshot(buffer, top.getConfiguration().orientation,
133 top.findMainWindow().mStableInsets);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800134 }
135
136 /**
137 * Retrieves all closing tasks based on the list of closing apps during an app transition.
138 */
139 @VisibleForTesting
140 void getClosingTasks(ArraySet<AppWindowToken> closingApps, ArraySet<Task> outClosingTasks) {
141 outClosingTasks.clear();
142 for (int i = closingApps.size() - 1; i >= 0; i--) {
143 final AppWindowToken atoken = closingApps.valueAt(i);
Bryce Lee6d410262017-02-28 15:30:17 -0800144 final Task task = atoken.getTask();
Jorim Jaggi02886a82016-12-06 09:10:06 -0800145
146 // If the task of the app is not visible anymore, it means no other app in that task
147 // is opening. Thus, the task is closing.
Bryce Lee6d410262017-02-28 15:30:17 -0800148 if (task != null && !task.isVisible()) {
149 outClosingTasks.add(task);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800150 }
151 }
152 }
153
154 private boolean canSnapshotTask(Task task) {
155 return !StackId.isHomeOrRecentsStack(task.mStack.mStackId);
156 }
Jorim Jaggi10abe2f2017-01-03 16:44:46 +0100157
158 /**
159 * Called when an {@link AppWindowToken} has been removed.
160 */
161 void onAppRemoved(AppWindowToken wtoken) {
Jorim Jaggi7361bab2017-01-16 17:17:58 +0100162 mCache.onAppRemoved(wtoken);
Jorim Jaggi10abe2f2017-01-03 16:44:46 +0100163 }
164
165 /**
166 * Called when the process of an {@link AppWindowToken} has died.
167 */
168 void onAppDied(AppWindowToken wtoken) {
Jorim Jaggi7361bab2017-01-16 17:17:58 +0100169 mCache.onAppDied(wtoken);
Jorim Jaggi10abe2f2017-01-03 16:44:46 +0100170 }
171
Jorim Jaggif9084ec2017-01-16 13:16:59 +0100172 void notifyTaskRemovedFromRecents(int taskId, int userId) {
Jorim Jaggi7361bab2017-01-16 17:17:58 +0100173 mCache.onTaskRemoved(taskId);
Jorim Jaggif9084ec2017-01-16 13:16:59 +0100174 mPersister.onTaskRemovedFromRecents(taskId, userId);
175 }
176
177 /**
178 * See {@link TaskSnapshotPersister#removeObsoleteFiles}
179 */
180 void removeObsoleteTaskFiles(ArraySet<Integer> persistentTaskIds, int[] runningUserIds) {
181 mPersister.removeObsoleteFiles(persistentTaskIds, runningUserIds);
182 }
183
Jorim Jaggi10abe2f2017-01-03 16:44:46 +0100184 void dump(PrintWriter pw, String prefix) {
185 mCache.dump(pw, prefix);
186 }
Jorim Jaggi02886a82016-12-06 09:10:06 -0800187}