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 android.annotation.Nullable; |
Jorim Jaggi | e2c77f9 | 2016-12-29 14:57:22 +0100 | [diff] [blame] | 20 | import android.app.ActivityManager.TaskSnapshot; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 21 | import android.util.ArrayMap; |
| 22 | |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 23 | import java.io.PrintWriter; |
| 24 | |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 25 | /** |
| 26 | * Caches snapshots. See {@link TaskSnapshotController}. |
| 27 | * <p> |
| 28 | * Access to this class should be guarded by the global window manager lock. |
| 29 | */ |
| 30 | class TaskSnapshotCache { |
| 31 | |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 32 | private final WindowManagerService mService; |
| 33 | private final TaskSnapshotLoader mLoader; |
Garfield Tan | e8d84ab | 2019-10-11 09:49:40 -0700 | [diff] [blame] | 34 | private final ArrayMap<ActivityRecord, Integer> mAppTaskMap = new ArrayMap<>(); |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 35 | private final ArrayMap<Integer, CacheEntry> mRunningCache = new ArrayMap<>(); |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 36 | |
| 37 | TaskSnapshotCache(WindowManagerService service, TaskSnapshotLoader loader) { |
| 38 | mService = service; |
| 39 | mLoader = loader; |
| 40 | } |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 41 | |
Jay Aliomer | 8b2671b | 2019-10-24 13:18:06 -0400 | [diff] [blame] | 42 | void clearRunningCache() { |
| 43 | mRunningCache.clear(); |
| 44 | } |
| 45 | |
Jorim Jaggi | e2c77f9 | 2016-12-29 14:57:22 +0100 | [diff] [blame] | 46 | void putSnapshot(Task task, TaskSnapshot snapshot) { |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 47 | final CacheEntry entry = mRunningCache.get(task.mTaskId); |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 48 | if (entry != null) { |
| 49 | mAppTaskMap.remove(entry.topApp); |
| 50 | } |
Wale Ogunwale | a38654f | 2019-11-17 20:37:15 -0800 | [diff] [blame] | 51 | final ActivityRecord top = task.getTopMostActivity(); |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 52 | mAppTaskMap.put(top, task.mTaskId); |
Wale Ogunwale | a38654f | 2019-11-17 20:37:15 -0800 | [diff] [blame] | 53 | mRunningCache.put(task.mTaskId, new CacheEntry(snapshot, top)); |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /** |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 57 | * If {@param restoreFromDisk} equals {@code true}, DO NOT HOLD THE WINDOW MANAGER LOCK! |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 58 | */ |
Jorim Jaggi | 35e3f53 | 2017-03-17 17:06:50 +0100 | [diff] [blame] | 59 | @Nullable TaskSnapshot getSnapshot(int taskId, int userId, boolean restoreFromDisk, |
| 60 | boolean reducedResolution) { |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 61 | |
Wale Ogunwale | db485de | 2018-10-29 09:47:07 -0700 | [diff] [blame] | 62 | synchronized (mService.mGlobalLock) { |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 63 | // Try the running cache. |
| 64 | final CacheEntry entry = mRunningCache.get(taskId); |
| 65 | if (entry != null) { |
| 66 | return entry.snapshot; |
| 67 | } |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | // Try to restore from disk if asked. |
| 71 | if (!restoreFromDisk) { |
| 72 | return null; |
| 73 | } |
Jorim Jaggi | 35e3f53 | 2017-03-17 17:06:50 +0100 | [diff] [blame] | 74 | return tryRestoreFromDisk(taskId, userId, reducedResolution); |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /** |
| 78 | * DO NOT HOLD THE WINDOW MANAGER LOCK WHEN CALLING THIS METHOD! |
| 79 | */ |
Jorim Jaggi | 35e3f53 | 2017-03-17 17:06:50 +0100 | [diff] [blame] | 80 | private TaskSnapshot tryRestoreFromDisk(int taskId, int userId, boolean reducedResolution) { |
| 81 | final TaskSnapshot snapshot = mLoader.loadTask(taskId, userId, reducedResolution); |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 82 | if (snapshot == null) { |
| 83 | return null; |
| 84 | } |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 85 | return snapshot; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Called when an app token has been removed |
| 90 | */ |
Garfield Tan | e8d84ab | 2019-10-11 09:49:40 -0700 | [diff] [blame] | 91 | void onAppRemoved(ActivityRecord activity) { |
| 92 | final Integer taskId = mAppTaskMap.get(activity); |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 93 | if (taskId != null) { |
| 94 | removeRunningEntry(taskId); |
| 95 | } |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 96 | } |
| 97 | |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 98 | /** |
| 99 | * Callend when an app window token's process died. |
| 100 | */ |
Garfield Tan | e8d84ab | 2019-10-11 09:49:40 -0700 | [diff] [blame] | 101 | void onAppDied(ActivityRecord activity) { |
| 102 | final Integer taskId = mAppTaskMap.get(activity); |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 103 | if (taskId != null) { |
| 104 | removeRunningEntry(taskId); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void onTaskRemoved(int taskId) { |
| 109 | removeRunningEntry(taskId); |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Riddle Hsu | 440f88b | 2019-11-06 22:17:35 +0800 | [diff] [blame] | 112 | void removeRunningEntry(int taskId) { |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 113 | final CacheEntry entry = mRunningCache.get(taskId); |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 114 | if (entry != null) { |
| 115 | mAppTaskMap.remove(entry.topApp); |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 116 | mRunningCache.remove(taskId); |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | void dump(PrintWriter pw, String prefix) { |
| 121 | final String doublePrefix = prefix + " "; |
| 122 | final String triplePrefix = doublePrefix + " "; |
| 123 | pw.println(prefix + "SnapshotCache"); |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 124 | for (int i = mRunningCache.size() - 1; i >= 0; i--) { |
| 125 | final CacheEntry entry = mRunningCache.valueAt(i); |
Jorim Jaggi | cdef591 | 2017-04-03 17:24:19 +0200 | [diff] [blame] | 126 | pw.println(doublePrefix + "Entry taskId=" + mRunningCache.keyAt(i)); |
| 127 | pw.println(triplePrefix + "topApp=" + entry.topApp); |
| 128 | pw.println(triplePrefix + "snapshot=" + entry.snapshot); |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
| 132 | private static final class CacheEntry { |
| 133 | |
| 134 | /** The snapshot. */ |
| 135 | final TaskSnapshot snapshot; |
| 136 | |
| 137 | /** The app token that was on top of the task when the snapshot was taken */ |
Garfield Tan | e8d84ab | 2019-10-11 09:49:40 -0700 | [diff] [blame] | 138 | final ActivityRecord topApp; |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 139 | |
Garfield Tan | e8d84ab | 2019-10-11 09:49:40 -0700 | [diff] [blame] | 140 | CacheEntry(TaskSnapshot snapshot, ActivityRecord topApp) { |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 141 | this.snapshot = snapshot; |
| 142 | this.topApp = topApp; |
| 143 | } |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 144 | } |
| 145 | } |