blob: d08c744c38ffad6cbbd5aa33fe3b837d82e19eb9 [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;
28import android.graphics.Bitmap;
29import android.graphics.Canvas;
30import android.graphics.GraphicBuffer;
31import android.util.ArraySet;
32import android.view.WindowManagerPolicy.StartingSurface;
33
34import com.android.internal.annotations.VisibleForTesting;
35
36/**
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;
52 private final TaskSnapshotCache mCache = new TaskSnapshotCache();
53
54 private final ArraySet<Task> mTmpTasks = new ArraySet<>();
55
56 TaskSnapshotController(WindowManagerService service) {
57 mService = service;
58 }
59
60 void onTransitionStarting() {
61 if (!ENABLE_TASK_SNAPSHOTS) {
62 return;
63 }
64
65 // We need to take a snapshot of the task if and only if all activities of the task are
66 // either closing or hidden.
67 getClosingTasks(mService.mClosingApps, mTmpTasks);
68 for (int i = mTmpTasks.size() - 1; i >= 0; i--) {
69 final Task task = mTmpTasks.valueAt(i);
70 if (!canSnapshotTask(task)) {
71 continue;
72 }
73 final GraphicBuffer graphicBuffer = snapshotTask(task);
74 if (graphicBuffer != null) {
75 mCache.putSnapshot(task, graphicBuffer);
76 }
77 }
78 }
79
80 @Nullable GraphicBuffer getSnapshot(Task task) {
81 return mCache.getSnapshot(task);
82 }
83
84 /**
85 * Creates a starting surface for {@param token} with {@param snapshot}. DO NOT HOLD THE WINDOW
86 * MANAGER LOCK WHEN CALLING THIS METHOD!
87 */
88 StartingSurface createStartingSurface(AppWindowToken token,
89 GraphicBuffer snapshot) {
90 return TaskSnapshotSurface.create(mService, token, snapshot);
91 }
92
93 private GraphicBuffer snapshotTask(Task task) {
94 final AppWindowToken top = (AppWindowToken) task.getTop();
95 if (top == null) {
96 return null;
97 }
98 final Bitmap bmp = top.mDisplayContent.screenshotApplications(top.token, -1, -1, false,
99 1.0f, ARGB_8888, false, true, false);
100 if (bmp == null) {
101 return null;
102 }
103 // TODO: Already use a GraphicBuffer when snapshotting the content.
104 final GraphicBuffer buffer = GraphicBuffer.create(bmp.getWidth(), bmp.getHeight(),
105 RGBA_8888, USAGE_HW_TEXTURE | USAGE_SW_WRITE_NEVER | USAGE_SW_READ_NEVER);
106 final Canvas c = buffer.lockCanvas();
107 c.drawBitmap(bmp, 0, 0, null);
108 buffer.unlockCanvasAndPost(c);
109 return buffer;
110 }
111
112 /**
113 * Retrieves all closing tasks based on the list of closing apps during an app transition.
114 */
115 @VisibleForTesting
116 void getClosingTasks(ArraySet<AppWindowToken> closingApps, ArraySet<Task> outClosingTasks) {
117 outClosingTasks.clear();
118 for (int i = closingApps.size() - 1; i >= 0; i--) {
119 final AppWindowToken atoken = closingApps.valueAt(i);
120
121 // If the task of the app is not visible anymore, it means no other app in that task
122 // is opening. Thus, the task is closing.
123 if (atoken.mTask != null && !atoken.mTask.isVisible()) {
124 outClosingTasks.add(closingApps.valueAt(i).mTask);
125 }
126 }
127 }
128
129 private boolean canSnapshotTask(Task task) {
130 return !StackId.isHomeOrRecentsStack(task.mStack.mStackId);
131 }
132}