blob: 4421d618737a8684eb5e11bd6545a34d6669a4d9 [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;
20import static android.graphics.Bitmap.Config.ARGB_8888;
21import static android.graphics.GraphicBuffer.USAGE_HW_TEXTURE;
22import static android.graphics.GraphicBuffer.USAGE_SW_READ_NEVER;
23import static android.graphics.GraphicBuffer.USAGE_SW_WRITE_NEVER;
24import static android.graphics.PixelFormat.RGBA_8888;
25
26import android.annotation.Nullable;
27import android.app.ActivityManager.StackId;
Jorim Jaggie2c77f92016-12-29 14:57:22 +010028import android.app.ActivityManager.TaskSnapshot;
Jorim Jaggi02886a82016-12-06 09:10:06 -080029import android.graphics.Bitmap;
30import android.graphics.Canvas;
31import android.graphics.GraphicBuffer;
32import android.util.ArraySet;
33import android.view.WindowManagerPolicy.StartingSurface;
34
35import com.android.internal.annotations.VisibleForTesting;
36
37/**
38 * When an app token becomes invisible, we take a snapshot (bitmap) of the corresponding task and
39 * put it into our cache. Internally we use gralloc buffers to be able to draw them wherever we
40 * like without any copying.
41 * <p>
42 * System applications may retrieve a snapshot to represent the current state of a task, and draw
43 * them in their own process.
44 * <p>
45 * When we task becomes visible again, we show a starting window with the snapshot as the content to
46 * make app transitions more responsive.
47 * <p>
48 * To access this class, acquire the global window manager lock.
49 */
50class TaskSnapshotController {
51
52 private final WindowManagerService mService;
53 private final TaskSnapshotCache mCache = new TaskSnapshotCache();
54
55 private final ArraySet<Task> mTmpTasks = new ArraySet<>();
56
57 TaskSnapshotController(WindowManagerService service) {
58 mService = service;
59 }
60
61 void onTransitionStarting() {
62 if (!ENABLE_TASK_SNAPSHOTS) {
63 return;
64 }
65
66 // We need to take a snapshot of the task if and only if all activities of the task are
67 // either closing or hidden.
68 getClosingTasks(mService.mClosingApps, mTmpTasks);
69 for (int i = mTmpTasks.size() - 1; i >= 0; i--) {
70 final Task task = mTmpTasks.valueAt(i);
71 if (!canSnapshotTask(task)) {
72 continue;
73 }
Jorim Jaggie2c77f92016-12-29 14:57:22 +010074 final TaskSnapshot snapshot = snapshotTask(task);
75 if (snapshot != null) {
76 mCache.putSnapshot(task, snapshot);
Jorim Jaggi02886a82016-12-06 09:10:06 -080077 }
78 }
79 }
80
Jorim Jaggie2c77f92016-12-29 14:57:22 +010081 @Nullable TaskSnapshot getSnapshot(Task task) {
Jorim Jaggi02886a82016-12-06 09:10:06 -080082 return mCache.getSnapshot(task);
83 }
84
85 /**
86 * Creates a starting surface for {@param token} with {@param snapshot}. DO NOT HOLD THE WINDOW
87 * MANAGER LOCK WHEN CALLING THIS METHOD!
88 */
89 StartingSurface createStartingSurface(AppWindowToken token,
90 GraphicBuffer snapshot) {
91 return TaskSnapshotSurface.create(mService, token, snapshot);
92 }
93
Jorim Jaggie2c77f92016-12-29 14:57:22 +010094 private TaskSnapshot snapshotTask(Task task) {
Jorim Jaggi02886a82016-12-06 09:10:06 -080095 final AppWindowToken top = (AppWindowToken) task.getTop();
96 if (top == null) {
97 return null;
98 }
99 final Bitmap bmp = top.mDisplayContent.screenshotApplications(top.token, -1, -1, false,
100 1.0f, ARGB_8888, false, true, false);
101 if (bmp == null) {
102 return null;
103 }
104 // TODO: Already use a GraphicBuffer when snapshotting the content.
105 final GraphicBuffer buffer = GraphicBuffer.create(bmp.getWidth(), bmp.getHeight(),
106 RGBA_8888, USAGE_HW_TEXTURE | USAGE_SW_WRITE_NEVER | USAGE_SW_READ_NEVER);
107 final Canvas c = buffer.lockCanvas();
108 c.drawBitmap(bmp, 0, 0, null);
109 buffer.unlockCanvasAndPost(c);
Jorim Jaggie2c77f92016-12-29 14:57:22 +0100110 return new TaskSnapshot(buffer, top.getConfiguration().orientation,
111 top.findMainWindow().mStableInsets);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800112 }
113
114 /**
115 * Retrieves all closing tasks based on the list of closing apps during an app transition.
116 */
117 @VisibleForTesting
118 void getClosingTasks(ArraySet<AppWindowToken> closingApps, ArraySet<Task> outClosingTasks) {
119 outClosingTasks.clear();
120 for (int i = closingApps.size() - 1; i >= 0; i--) {
121 final AppWindowToken atoken = closingApps.valueAt(i);
122
123 // If the task of the app is not visible anymore, it means no other app in that task
124 // is opening. Thus, the task is closing.
125 if (atoken.mTask != null && !atoken.mTask.isVisible()) {
126 outClosingTasks.add(closingApps.valueAt(i).mTask);
127 }
128 }
129 }
130
131 private boolean canSnapshotTask(Task task) {
132 return !StackId.isHomeOrRecentsStack(task.mStack.mStackId);
133 }
134}