• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 package com.android.server.wm;
17 
18 import android.annotation.Nullable;
19 import android.hardware.HardwareBuffer;
20 import android.util.ArrayMap;
21 import android.window.TaskSnapshot;
22 
23 import com.android.internal.annotations.GuardedBy;
24 
25 import java.io.PrintWriter;
26 import java.util.function.Consumer;
27 
28 /**
29  * Base class for an app snapshot cache
30  * @param <TYPE> The basic type, either Task or ActivityRecord
31  */
32 abstract class SnapshotCache<TYPE extends WindowContainer> {
33     protected final Object mLock = new Object();
34 
35     protected final String mName;
36 
37     @GuardedBy("mLock")
38     protected final ArrayMap<ActivityRecord, Integer> mAppIdMap = new ArrayMap<>();
39 
40     @GuardedBy("mLock")
41     protected final ArrayMap<Integer, CacheEntry> mRunningCache = new ArrayMap<>();
42 
43     protected Consumer<HardwareBuffer> mSafeSnapshotReleaser;
44 
SnapshotCache(String name)45     SnapshotCache(String name) {
46         mName = name;
47     }
48 
putSnapshot(TYPE window, TaskSnapshot snapshot)49     abstract void putSnapshot(TYPE window, TaskSnapshot snapshot);
50 
setSafeSnapshotReleaser(Consumer<HardwareBuffer> safeSnapshotReleaser)51     void setSafeSnapshotReleaser(Consumer<HardwareBuffer> safeSnapshotReleaser) {
52         mSafeSnapshotReleaser = safeSnapshotReleaser;
53     }
54 
clearRunningCache()55     void clearRunningCache() {
56         synchronized (mLock) {
57             mRunningCache.clear();
58         }
59     }
60 
61     @Nullable
getSnapshotInner(Integer id)62     final TaskSnapshot getSnapshotInner(Integer id) {
63         synchronized (mLock) {
64             // Try the running cache.
65             final CacheEntry entry = mRunningCache.get(id);
66             if (entry != null) {
67                 return entry.snapshot;
68             }
69         }
70         return null;
71     }
72 
73     /** Called when an app token has been removed. */
onAppRemoved(ActivityRecord activity)74     void onAppRemoved(ActivityRecord activity) {
75         synchronized (mLock) {
76             final Integer id = mAppIdMap.get(activity);
77             if (id != null) {
78                 removeRunningEntry(id);
79             }
80         }
81     }
82 
83     /** Called when an app window token's process died. */
onAppDied(ActivityRecord activity)84     void onAppDied(ActivityRecord activity) {
85         synchronized (mLock) {
86             final Integer id = mAppIdMap.get(activity);
87             if (id != null) {
88                 removeRunningEntry(id);
89             }
90         }
91     }
92 
onIdRemoved(Integer index)93     void onIdRemoved(Integer index) {
94         removeRunningEntry(index);
95     }
96 
removeRunningEntry(Integer id)97     void removeRunningEntry(Integer id) {
98         synchronized (mLock) {
99             final CacheEntry entry = mRunningCache.get(id);
100             if (entry != null) {
101                 mAppIdMap.remove(entry.topApp);
102                 mRunningCache.remove(id);
103                 entry.snapshot.removeReference(TaskSnapshot.REFERENCE_CACHE);
104             }
105         }
106     }
107 
dump(PrintWriter pw, String prefix)108     void dump(PrintWriter pw, String prefix) {
109         final String doublePrefix = prefix + "  ";
110         final String triplePrefix = doublePrefix + "  ";
111         pw.println(prefix + "SnapshotCache " + mName);
112 
113         synchronized (mLock) {
114             for (int i = mRunningCache.size() - 1; i >= 0; i--) {
115                 final CacheEntry entry = mRunningCache.valueAt(i);
116                 pw.println(doublePrefix + "Entry token=" + mRunningCache.keyAt(i));
117                 pw.println(triplePrefix + "topApp=" + entry.topApp);
118                 pw.println(triplePrefix + "snapshot=" + entry.snapshot);
119             }
120         }
121     }
122 
123     static final class CacheEntry {
124         /** The snapshot. */
125         final TaskSnapshot snapshot;
126         /** The app token that was on top of the task when the snapshot was taken */
127         final ActivityRecord topApp;
CacheEntry(TaskSnapshot snapshot, ActivityRecord topApp)128         CacheEntry(TaskSnapshot snapshot, ActivityRecord topApp) {
129             this.snapshot = snapshot;
130             this.topApp = topApp;
131         }
132     }
133 }
134