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