blob: 4ae6dbe21721a76627d7e3765f1df9740d4b4811 [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 */
Jorim Jaggi35e3f532017-03-17 17:06:50 +0100109 @Nullable TaskSnapshot getSnapshot(int taskId, int userId, boolean restoreFromDisk,
110 boolean reducedResolution) {
111 return mCache.getSnapshot(taskId, userId, restoreFromDisk, reducedResolution);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800112 }
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 Jaggie2c77f92016-12-29 14:57:22 +0100123 private TaskSnapshot snapshotTask(Task task) {
Jorim Jaggi10abe2f2017-01-03 16:44:46 +0100124 final AppWindowToken top = task.getTopChild();
Jorim Jaggi02886a82016-12-06 09:10:06 -0800125 if (top == null) {
126 return null;
127 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +0100128 final GraphicBuffer buffer = top.mDisplayContent.screenshotApplicationsToBuffer(top.token,
129 -1, -1, false, 1.0f, false, true);
130 if (buffer == null) {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800131 return null;
132 }
Jorim Jaggie2c77f92016-12-29 14:57:22 +0100133 return new TaskSnapshot(buffer, top.getConfiguration().orientation,
Jorim Jaggi35e3f532017-03-17 17:06:50 +0100134 top.findMainWindow().mStableInsets, false /* reduced */, 1f /* scale */);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800135 }
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 Lee6d410262017-02-28 15:30:17 -0800145 final Task task = atoken.getTask();
Jorim Jaggi02886a82016-12-06 09:10:06 -0800146
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 Lee6d410262017-02-28 15:30:17 -0800149 if (task != null && !task.isVisible()) {
150 outClosingTasks.add(task);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800151 }
152 }
153 }
154
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +0100155 @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 Jaggi02886a82016-12-06 09:10:06 -0800162 }
Jorim Jaggi10abe2f2017-01-03 16:44:46 +0100163
164 /**
165 * Called when an {@link AppWindowToken} has been removed.
166 */
167 void onAppRemoved(AppWindowToken wtoken) {
Jorim Jaggi7361bab2017-01-16 17:17:58 +0100168 mCache.onAppRemoved(wtoken);
Jorim Jaggi10abe2f2017-01-03 16:44:46 +0100169 }
170
171 /**
172 * Called when the process of an {@link AppWindowToken} has died.
173 */
174 void onAppDied(AppWindowToken wtoken) {
Jorim Jaggi7361bab2017-01-16 17:17:58 +0100175 mCache.onAppDied(wtoken);
Jorim Jaggi10abe2f2017-01-03 16:44:46 +0100176 }
177
Jorim Jaggif9084ec2017-01-16 13:16:59 +0100178 void notifyTaskRemovedFromRecents(int taskId, int userId) {
Jorim Jaggi7361bab2017-01-16 17:17:58 +0100179 mCache.onTaskRemoved(taskId);
Jorim Jaggif9084ec2017-01-16 13:16:59 +0100180 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 Jaggi10abe2f2017-01-03 16:44:46 +0100190 void dump(PrintWriter pw, String prefix) {
191 mCache.dump(pw, prefix);
192 }
Jorim Jaggi02886a82016-12-06 09:10:06 -0800193}