blob: efa72a63ac2e9c807c55e171dc6e8fcd441213e8 [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
Jorim Jaggi10abe2f2017-01-03 16:44:46 +010037import java.io.PrintWriter;
38
Jorim Jaggi02886a82016-12-06 09:10:06 -080039/**
40 * When an app token becomes invisible, we take a snapshot (bitmap) of the corresponding task and
41 * put it into our cache. Internally we use gralloc buffers to be able to draw them wherever we
42 * like without any copying.
43 * <p>
44 * System applications may retrieve a snapshot to represent the current state of a task, and draw
45 * them in their own process.
46 * <p>
47 * When we task becomes visible again, we show a starting window with the snapshot as the content to
48 * make app transitions more responsive.
49 * <p>
50 * To access this class, acquire the global window manager lock.
51 */
52class TaskSnapshotController {
53
54 private final WindowManagerService mService;
55 private final TaskSnapshotCache mCache = new TaskSnapshotCache();
56
57 private final ArraySet<Task> mTmpTasks = new ArraySet<>();
58
59 TaskSnapshotController(WindowManagerService service) {
60 mService = service;
61 }
62
63 void onTransitionStarting() {
64 if (!ENABLE_TASK_SNAPSHOTS) {
65 return;
66 }
67
68 // We need to take a snapshot of the task if and only if all activities of the task are
69 // either closing or hidden.
70 getClosingTasks(mService.mClosingApps, mTmpTasks);
71 for (int i = mTmpTasks.size() - 1; i >= 0; i--) {
72 final Task task = mTmpTasks.valueAt(i);
73 if (!canSnapshotTask(task)) {
74 continue;
75 }
Jorim Jaggie2c77f92016-12-29 14:57:22 +010076 final TaskSnapshot snapshot = snapshotTask(task);
77 if (snapshot != null) {
78 mCache.putSnapshot(task, snapshot);
Jorim Jaggi02886a82016-12-06 09:10:06 -080079 }
80 }
81 }
82
Jorim Jaggie2c77f92016-12-29 14:57:22 +010083 @Nullable TaskSnapshot getSnapshot(Task task) {
Jorim Jaggi02886a82016-12-06 09:10:06 -080084 return mCache.getSnapshot(task);
85 }
86
87 /**
88 * Creates a starting surface for {@param token} with {@param snapshot}. DO NOT HOLD THE WINDOW
89 * MANAGER LOCK WHEN CALLING THIS METHOD!
90 */
91 StartingSurface createStartingSurface(AppWindowToken token,
92 GraphicBuffer snapshot) {
93 return TaskSnapshotSurface.create(mService, token, snapshot);
94 }
95
Jorim Jaggie2c77f92016-12-29 14:57:22 +010096 private TaskSnapshot snapshotTask(Task task) {
Jorim Jaggi10abe2f2017-01-03 16:44:46 +010097 final AppWindowToken top = task.getTopChild();
Jorim Jaggi02886a82016-12-06 09:10:06 -080098 if (top == null) {
99 return null;
100 }
101 final Bitmap bmp = top.mDisplayContent.screenshotApplications(top.token, -1, -1, false,
102 1.0f, ARGB_8888, false, true, false);
103 if (bmp == null) {
104 return null;
105 }
106 // TODO: Already use a GraphicBuffer when snapshotting the content.
107 final GraphicBuffer buffer = GraphicBuffer.create(bmp.getWidth(), bmp.getHeight(),
108 RGBA_8888, USAGE_HW_TEXTURE | USAGE_SW_WRITE_NEVER | USAGE_SW_READ_NEVER);
109 final Canvas c = buffer.lockCanvas();
110 c.drawBitmap(bmp, 0, 0, null);
111 buffer.unlockCanvasAndPost(c);
Jorim Jaggie2c77f92016-12-29 14:57:22 +0100112 return new TaskSnapshot(buffer, top.getConfiguration().orientation,
113 top.findMainWindow().mStableInsets);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800114 }
115
116 /**
117 * Retrieves all closing tasks based on the list of closing apps during an app transition.
118 */
119 @VisibleForTesting
120 void getClosingTasks(ArraySet<AppWindowToken> closingApps, ArraySet<Task> outClosingTasks) {
121 outClosingTasks.clear();
122 for (int i = closingApps.size() - 1; i >= 0; i--) {
123 final AppWindowToken atoken = closingApps.valueAt(i);
124
125 // If the task of the app is not visible anymore, it means no other app in that task
126 // is opening. Thus, the task is closing.
127 if (atoken.mTask != null && !atoken.mTask.isVisible()) {
128 outClosingTasks.add(closingApps.valueAt(i).mTask);
129 }
130 }
131 }
132
133 private boolean canSnapshotTask(Task task) {
134 return !StackId.isHomeOrRecentsStack(task.mStack.mStackId);
135 }
Jorim Jaggi10abe2f2017-01-03 16:44:46 +0100136
137 /**
138 * Called when an {@link AppWindowToken} has been removed.
139 */
140 void onAppRemoved(AppWindowToken wtoken) {
141 // TODO: Clean from both recents and running cache.
142 mCache.cleanCache(wtoken);
143 }
144
145 /**
146 * Called when the process of an {@link AppWindowToken} has died.
147 */
148 void onAppDied(AppWindowToken wtoken) {
149
150 // TODO: Only clean from running cache.
151 mCache.cleanCache(wtoken);
152 }
153
154 void dump(PrintWriter pw, String prefix) {
155 mCache.dump(pw, prefix);
156 }
Jorim Jaggi02886a82016-12-06 09:10:06 -0800157}