blob: fbb826d5cb483124485b55ace128c2292792fadc [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 Jaggi8f4fe6e2017-03-14 18:21:40 +010020import static android.graphics.GraphicBuffer.USAGE_HW_TEXTURE;
21import static android.graphics.GraphicBuffer.USAGE_SW_READ_NEVER;
22import static android.graphics.GraphicBuffer.USAGE_SW_WRITE_RARELY;
23import static android.graphics.PixelFormat.RGBA_8888;
Jorim Jaggi02886a82016-12-06 09:10:06 -080024
25import android.annotation.Nullable;
Jorim Jaggi3d732602017-02-22 17:56:40 +010026import android.app.ActivityManager;
Jorim Jaggi02886a82016-12-06 09:10:06 -080027import android.app.ActivityManager.StackId;
Jorim Jaggie2c77f92016-12-29 14:57:22 +010028import android.app.ActivityManager.TaskSnapshot;
Jorim Jaggi8f4fe6e2017-03-14 18:21:40 +010029import android.graphics.Canvas;
Jorim Jaggi02886a82016-12-06 09:10:06 -080030import android.graphics.GraphicBuffer;
Jorim Jaggi30d64f32017-04-07 16:33:17 +020031import android.graphics.Rect;
Jorim Jaggif9084ec2017-01-16 13:16:59 +010032import android.os.Environment;
Jorim Jaggi02886a82016-12-06 09:10:06 -080033import android.util.ArraySet;
34import android.view.WindowManagerPolicy.StartingSurface;
35
Jorim Jaggi8b702ed2017-01-20 16:59:03 +010036import com.google.android.collect.Sets;
37
Jorim Jaggi02886a82016-12-06 09:10:06 -080038import com.android.internal.annotations.VisibleForTesting;
39
Jorim Jaggi10abe2f2017-01-03 16:44:46 +010040import java.io.PrintWriter;
41
Jorim Jaggi02886a82016-12-06 09:10:06 -080042/**
43 * When an app token becomes invisible, we take a snapshot (bitmap) of the corresponding task and
44 * put it into our cache. Internally we use gralloc buffers to be able to draw them wherever we
45 * like without any copying.
46 * <p>
47 * System applications may retrieve a snapshot to represent the current state of a task, and draw
48 * them in their own process.
49 * <p>
50 * When we task becomes visible again, we show a starting window with the snapshot as the content to
51 * make app transitions more responsive.
52 * <p>
53 * To access this class, acquire the global window manager lock.
54 */
55class TaskSnapshotController {
56
Jorim Jaggi8f4fe6e2017-03-14 18:21:40 +010057 /**
58 * Return value for {@link #getSnapshotMode}: We are allowed to take a real screenshot to be
59 * used as the snapshot.
60 */
61 @VisibleForTesting
62 static final int SNAPSHOT_MODE_REAL = 0;
63
64 /**
65 * Return value for {@link #getSnapshotMode}: We are not allowed to take a real screenshot but
66 * we should try to use the app theme to create a dummy representation of the app.
67 */
68 @VisibleForTesting
69 static final int SNAPSHOT_MODE_APP_THEME = 1;
70
71 /**
72 * Return value for {@link #getSnapshotMode}: We aren't allowed to take any snapshot.
73 */
74 @VisibleForTesting
75 static final int SNAPSHOT_MODE_NONE = 2;
76
Jorim Jaggi02886a82016-12-06 09:10:06 -080077 private final WindowManagerService mService;
Jorim Jaggi02886a82016-12-06 09:10:06 -080078
Jorim Jaggi7361bab2017-01-16 17:17:58 +010079 private final TaskSnapshotCache mCache;
Jorim Jaggif9084ec2017-01-16 13:16:59 +010080 private final TaskSnapshotPersister mPersister = new TaskSnapshotPersister(
81 Environment::getDataSystemCeDirectory);
82 private final TaskSnapshotLoader mLoader = new TaskSnapshotLoader(mPersister);
Jorim Jaggi02886a82016-12-06 09:10:06 -080083 private final ArraySet<Task> mTmpTasks = new ArraySet<>();
84
85 TaskSnapshotController(WindowManagerService service) {
86 mService = service;
Jorim Jaggi7361bab2017-01-16 17:17:58 +010087 mCache = new TaskSnapshotCache(mService, mLoader);
Jorim Jaggi02886a82016-12-06 09:10:06 -080088 }
89
Jorim Jaggif9084ec2017-01-16 13:16:59 +010090 void systemReady() {
91 mPersister.start();
92 }
93
Jorim Jaggi02886a82016-12-06 09:10:06 -080094 void onTransitionStarting() {
Jorim Jaggi8b702ed2017-01-20 16:59:03 +010095 handleClosingApps(mService.mClosingApps);
96 }
97
Jorim Jaggi8b702ed2017-01-20 16:59:03 +010098 /**
99 * Called when the visibility of an app changes outside of the regular app transition flow.
100 */
101 void notifyAppVisibilityChanged(AppWindowToken appWindowToken, boolean visible) {
Jorim Jaggi8b702ed2017-01-20 16:59:03 +0100102 if (!visible) {
103 handleClosingApps(Sets.newArraySet(appWindowToken));
104 }
105 }
106
107 private void handleClosingApps(ArraySet<AppWindowToken> closingApps) {
Jorim Jaggi3d732602017-02-22 17:56:40 +0100108 if (!ENABLE_TASK_SNAPSHOTS || ActivityManager.isLowRamDeviceStatic()) {
109 return;
110 }
Jorim Jaggi02886a82016-12-06 09:10:06 -0800111
112 // We need to take a snapshot of the task if and only if all activities of the task are
113 // either closing or hidden.
Jorim Jaggi8b702ed2017-01-20 16:59:03 +0100114 getClosingTasks(closingApps, mTmpTasks);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800115 for (int i = mTmpTasks.size() - 1; i >= 0; i--) {
116 final Task task = mTmpTasks.valueAt(i);
Jorim Jaggi8f4fe6e2017-03-14 18:21:40 +0100117 final int mode = getSnapshotMode(task);
118 final TaskSnapshot snapshot;
119 switch (mode) {
120 case SNAPSHOT_MODE_NONE:
121 continue;
122 case SNAPSHOT_MODE_APP_THEME:
123 snapshot = drawAppThemeSnapshot(task);
124 break;
125 case SNAPSHOT_MODE_REAL:
126 snapshot = snapshotTask(task);
127 break;
128 default:
129 snapshot = null;
130 break;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800131 }
Jorim Jaggie2c77f92016-12-29 14:57:22 +0100132 if (snapshot != null) {
133 mCache.putSnapshot(task, snapshot);
Jorim Jaggif9084ec2017-01-16 13:16:59 +0100134 mPersister.persistSnapshot(task.mTaskId, task.mUserId, snapshot);
Jorim Jaggifb9d78a2017-01-05 18:57:12 +0100135 if (task.getController() != null) {
136 task.getController().reportSnapshotChanged(snapshot);
137 }
Jorim Jaggi02886a82016-12-06 09:10:06 -0800138 }
139 }
140 }
141
Jorim Jaggi7361bab2017-01-16 17:17:58 +0100142 /**
143 * Retrieves a snapshot. If {@param restoreFromDisk} equals {@code true}, DO HOLD THE WINDOW
144 * MANAGER LOCK WHEN CALLING THIS METHOD!
145 */
Jorim Jaggi35e3f532017-03-17 17:06:50 +0100146 @Nullable TaskSnapshot getSnapshot(int taskId, int userId, boolean restoreFromDisk,
147 boolean reducedResolution) {
148 return mCache.getSnapshot(taskId, userId, restoreFromDisk, reducedResolution);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800149 }
150
151 /**
152 * Creates a starting surface for {@param token} with {@param snapshot}. DO NOT HOLD THE WINDOW
153 * MANAGER LOCK WHEN CALLING THIS METHOD!
154 */
155 StartingSurface createStartingSurface(AppWindowToken token,
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200156 TaskSnapshot snapshot) {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800157 return TaskSnapshotSurface.create(mService, token, snapshot);
158 }
159
Jorim Jaggie2c77f92016-12-29 14:57:22 +0100160 private TaskSnapshot snapshotTask(Task task) {
Jorim Jaggi10abe2f2017-01-03 16:44:46 +0100161 final AppWindowToken top = task.getTopChild();
Jorim Jaggi02886a82016-12-06 09:10:06 -0800162 if (top == null) {
163 return null;
164 }
Jorim Jaggidd5986e2017-04-11 15:50:19 -0700165 final WindowState mainWindow = top.findMainWindow();
166 if (mainWindow == null) {
167 return null;
168 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +0100169 final GraphicBuffer buffer = top.mDisplayContent.screenshotApplicationsToBuffer(top.token,
170 -1, -1, false, 1.0f, false, true);
171 if (buffer == null) {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800172 return null;
173 }
Jorim Jaggie2c77f92016-12-29 14:57:22 +0100174 return new TaskSnapshot(buffer, top.getConfiguration().orientation,
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200175 minRect(mainWindow.mContentInsets, mainWindow.mStableInsets), false /* reduced */,
176 1f /* scale */);
177 }
178
179 private Rect minRect(Rect rect1, Rect rect2) {
180 return new Rect(Math.min(rect1.left, rect2.left),
181 Math.min(rect1.top, rect2.top),
182 Math.min(rect1.right, rect2.right),
183 Math.min(rect1.bottom, rect2.bottom));
Jorim Jaggi02886a82016-12-06 09:10:06 -0800184 }
185
186 /**
187 * Retrieves all closing tasks based on the list of closing apps during an app transition.
188 */
189 @VisibleForTesting
190 void getClosingTasks(ArraySet<AppWindowToken> closingApps, ArraySet<Task> outClosingTasks) {
191 outClosingTasks.clear();
192 for (int i = closingApps.size() - 1; i >= 0; i--) {
193 final AppWindowToken atoken = closingApps.valueAt(i);
Bryce Lee6d410262017-02-28 15:30:17 -0800194 final Task task = atoken.getTask();
Jorim Jaggi02886a82016-12-06 09:10:06 -0800195
196 // If the task of the app is not visible anymore, it means no other app in that task
197 // is opening. Thus, the task is closing.
Bryce Lee6d410262017-02-28 15:30:17 -0800198 if (task != null && !task.isVisible()) {
199 outClosingTasks.add(task);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800200 }
201 }
202 }
203
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +0100204 @VisibleForTesting
Jorim Jaggi8f4fe6e2017-03-14 18:21:40 +0100205 int getSnapshotMode(Task task) {
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +0100206 final AppWindowToken topChild = task.getTopChild();
Jorim Jaggi8f4fe6e2017-03-14 18:21:40 +0100207 if (StackId.isHomeOrRecentsStack(task.mStack.mStackId)) {
208 return SNAPSHOT_MODE_NONE;
209 } else if (topChild != null && topChild.shouldDisablePreviewScreenshots()) {
210 return SNAPSHOT_MODE_APP_THEME;
211 } else {
212 return SNAPSHOT_MODE_REAL;
213 }
214 }
215
216 /**
217 * If we are not allowed to take a real screenshot, this attempts to represent the app as best
218 * as possible by using the theme's window background.
219 */
220 private TaskSnapshot drawAppThemeSnapshot(Task task) {
221 final AppWindowToken topChild = task.getTopChild();
222 if (topChild == null) {
223 return null;
224 }
225 final WindowState mainWindow = topChild.findMainWindow();
226 if (mainWindow == null) {
227 return null;
228 }
229 final int color = task.getTaskDescription().getBackgroundColor();
230 final GraphicBuffer buffer = GraphicBuffer.create(mainWindow.getFrameLw().width(),
231 mainWindow.getFrameLw().height(),
232 RGBA_8888, USAGE_HW_TEXTURE | USAGE_SW_WRITE_RARELY | USAGE_SW_READ_NEVER);
233 if (buffer == null) {
234 return null;
235 }
236 final Canvas c = buffer.lockCanvas();
237 c.drawColor(color);
238 buffer.unlockCanvasAndPost(c);
239 return new TaskSnapshot(buffer, topChild.getConfiguration().orientation,
240 mainWindow.mStableInsets, false /* reduced */, 1.0f /* scale */);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800241 }
Jorim Jaggi10abe2f2017-01-03 16:44:46 +0100242
243 /**
244 * Called when an {@link AppWindowToken} has been removed.
245 */
246 void onAppRemoved(AppWindowToken wtoken) {
Jorim Jaggi7361bab2017-01-16 17:17:58 +0100247 mCache.onAppRemoved(wtoken);
Jorim Jaggi10abe2f2017-01-03 16:44:46 +0100248 }
249
250 /**
251 * Called when the process of an {@link AppWindowToken} has died.
252 */
253 void onAppDied(AppWindowToken wtoken) {
Jorim Jaggi7361bab2017-01-16 17:17:58 +0100254 mCache.onAppDied(wtoken);
Jorim Jaggi10abe2f2017-01-03 16:44:46 +0100255 }
256
Jorim Jaggif9084ec2017-01-16 13:16:59 +0100257 void notifyTaskRemovedFromRecents(int taskId, int userId) {
Jorim Jaggi7361bab2017-01-16 17:17:58 +0100258 mCache.onTaskRemoved(taskId);
Jorim Jaggif9084ec2017-01-16 13:16:59 +0100259 mPersister.onTaskRemovedFromRecents(taskId, userId);
260 }
261
262 /**
263 * See {@link TaskSnapshotPersister#removeObsoleteFiles}
264 */
265 void removeObsoleteTaskFiles(ArraySet<Integer> persistentTaskIds, int[] runningUserIds) {
266 mPersister.removeObsoleteFiles(persistentTaskIds, runningUserIds);
267 }
268
Jorim Jaggi10abe2f2017-01-03 16:44:46 +0100269 void dump(PrintWriter pw, String prefix) {
270 mCache.dump(pw, prefix);
271 }
Jorim Jaggi02886a82016-12-06 09:10:06 -0800272}