blob: 7bf4edb2c97a34aa3f540d4c6c9e80e43c4f831b [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 android.annotation.Nullable;
Jorim Jaggie2c77f92016-12-29 14:57:22 +010020import android.app.ActivityManager.TaskSnapshot;
Jorim Jaggi02886a82016-12-06 09:10:06 -080021import android.util.ArrayMap;
Jorim Jaggi7361bab2017-01-16 17:17:58 +010022import android.util.LruCache;
Jorim Jaggi02886a82016-12-06 09:10:06 -080023
Jorim Jaggi10abe2f2017-01-03 16:44:46 +010024import java.io.PrintWriter;
Jorim Jaggi7361bab2017-01-16 17:17:58 +010025import java.util.Map;
26import java.util.Map.Entry;
Jorim Jaggi10abe2f2017-01-03 16:44:46 +010027
Jorim Jaggi02886a82016-12-06 09:10:06 -080028/**
29 * Caches snapshots. See {@link TaskSnapshotController}.
30 * <p>
31 * Access to this class should be guarded by the global window manager lock.
32 */
33class TaskSnapshotCache {
34
Jorim Jaggi7361bab2017-01-16 17:17:58 +010035 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 Jaggi7361bab2017-01-16 17:17:58 +010039
40 TaskSnapshotCache(WindowManagerService service, TaskSnapshotLoader loader) {
41 mService = service;
42 mLoader = loader;
43 }
Jorim Jaggi02886a82016-12-06 09:10:06 -080044
Jorim Jaggie2c77f92016-12-29 14:57:22 +010045 void putSnapshot(Task task, TaskSnapshot snapshot) {
Jorim Jaggi7361bab2017-01-16 17:17:58 +010046 final CacheEntry entry = mRunningCache.get(task.mTaskId);
Jorim Jaggi10abe2f2017-01-03 16:44:46 +010047 if (entry != null) {
48 mAppTaskMap.remove(entry.topApp);
49 }
50 final AppWindowToken top = task.getTopChild();
Jorim Jaggi7361bab2017-01-16 17:17:58 +010051 mAppTaskMap.put(top, task.mTaskId);
52 mRunningCache.put(task.mTaskId, new CacheEntry(snapshot, task.getTopChild()));
Jorim Jaggi10abe2f2017-01-03 16:44:46 +010053 }
54
55 /**
Jorim Jaggi7361bab2017-01-16 17:17:58 +010056 * If {@param restoreFromDisk} equals {@code true}, DO NOT HOLD THE WINDOW MANAGER LOCK!
Jorim Jaggi10abe2f2017-01-03 16:44:46 +010057 */
Jorim Jaggi35e3f532017-03-17 17:06:50 +010058 @Nullable TaskSnapshot getSnapshot(int taskId, int userId, boolean restoreFromDisk,
59 boolean reducedResolution) {
Jorim Jaggi7361bab2017-01-16 17:17:58 +010060
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 Jaggi7361bab2017-01-16 17:17:58 +010067 }
68
69 // Try to restore from disk if asked.
70 if (!restoreFromDisk) {
71 return null;
72 }
Jorim Jaggi35e3f532017-03-17 17:06:50 +010073 return tryRestoreFromDisk(taskId, userId, reducedResolution);
Jorim Jaggi7361bab2017-01-16 17:17:58 +010074 }
75
76 /**
77 * DO NOT HOLD THE WINDOW MANAGER LOCK WHEN CALLING THIS METHOD!
78 */
Jorim Jaggi35e3f532017-03-17 17:06:50 +010079 private TaskSnapshot tryRestoreFromDisk(int taskId, int userId, boolean reducedResolution) {
80 final TaskSnapshot snapshot = mLoader.loadTask(taskId, userId, reducedResolution);
Jorim Jaggi7361bab2017-01-16 17:17:58 +010081 if (snapshot == null) {
82 return null;
83 }
Jorim Jaggi7361bab2017-01-16 17:17:58 +010084 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 Jaggi10abe2f2017-01-03 16:44:46 +010095 }
96
Jorim Jaggi7361bab2017-01-16 17:17:58 +010097 /**
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 Jaggi7361bab2017-01-16 17:17:58 +0100109 }
110
111 private void removeRunningEntry(int taskId) {
112 final CacheEntry entry = mRunningCache.get(taskId);
Jorim Jaggi10abe2f2017-01-03 16:44:46 +0100113 if (entry != null) {
114 mAppTaskMap.remove(entry.topApp);
Jorim Jaggi7361bab2017-01-16 17:17:58 +0100115 mRunningCache.remove(taskId);
Jorim Jaggi10abe2f2017-01-03 16:44:46 +0100116 }
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 Jaggi7361bab2017-01-16 17:17:58 +0100123 for (int i = mRunningCache.size() - 1; i >= 0; i--) {
124 final CacheEntry entry = mRunningCache.valueAt(i);
Jorim Jaggicdef5912017-04-03 17:24:19 +0200125 pw.println(doublePrefix + "Entry taskId=" + mRunningCache.keyAt(i));
126 pw.println(triplePrefix + "topApp=" + entry.topApp);
127 pw.println(triplePrefix + "snapshot=" + entry.snapshot);
Jorim Jaggi10abe2f2017-01-03 16:44:46 +0100128 }
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 Jaggi02886a82016-12-06 09:10:06 -0800143 }
144}