Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.wm; |
| 18 | |
| 19 | import static android.app.ActivityManager.ENABLE_TASK_SNAPSHOTS; |
| 20 | import static android.graphics.Bitmap.Config.ARGB_8888; |
| 21 | import static android.graphics.GraphicBuffer.USAGE_HW_TEXTURE; |
| 22 | import static android.graphics.GraphicBuffer.USAGE_SW_READ_NEVER; |
| 23 | import static android.graphics.GraphicBuffer.USAGE_SW_WRITE_NEVER; |
| 24 | import static android.graphics.PixelFormat.RGBA_8888; |
| 25 | |
| 26 | import android.annotation.Nullable; |
| 27 | import android.app.ActivityManager.StackId; |
Jorim Jaggi | e2c77f9 | 2016-12-29 14:57:22 +0100 | [diff] [blame] | 28 | import android.app.ActivityManager.TaskSnapshot; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 29 | import android.graphics.Bitmap; |
| 30 | import android.graphics.Canvas; |
| 31 | import android.graphics.GraphicBuffer; |
| 32 | import android.util.ArraySet; |
| 33 | import android.view.WindowManagerPolicy.StartingSurface; |
| 34 | |
| 35 | import com.android.internal.annotations.VisibleForTesting; |
| 36 | |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame^] | 37 | import java.io.PrintWriter; |
| 38 | |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 39 | /** |
| 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 | */ |
| 52 | class 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 Jaggi | e2c77f9 | 2016-12-29 14:57:22 +0100 | [diff] [blame] | 76 | final TaskSnapshot snapshot = snapshotTask(task); |
| 77 | if (snapshot != null) { |
| 78 | mCache.putSnapshot(task, snapshot); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
Jorim Jaggi | e2c77f9 | 2016-12-29 14:57:22 +0100 | [diff] [blame] | 83 | @Nullable TaskSnapshot getSnapshot(Task task) { |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 84 | 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 Jaggi | e2c77f9 | 2016-12-29 14:57:22 +0100 | [diff] [blame] | 96 | private TaskSnapshot snapshotTask(Task task) { |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame^] | 97 | final AppWindowToken top = task.getTopChild(); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 98 | 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 Jaggi | e2c77f9 | 2016-12-29 14:57:22 +0100 | [diff] [blame] | 112 | return new TaskSnapshot(buffer, top.getConfiguration().orientation, |
| 113 | top.findMainWindow().mStableInsets); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 114 | } |
| 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 Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame^] | 136 | |
| 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 Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 157 | } |