blob: 297e2880a455439efba35c8d61dde9a8f5388ea2 [file] [log] [blame]
Jorim Jaggif9084ec2017-01-16 13:16:59 +01001/*
2 * Copyright (C) 2017 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
Jorim Jaggi35e3f532017-03-17 17:06:50 +010019import static android.graphics.Bitmap.CompressFormat.*;
Jorim Jaggif9084ec2017-01-16 13:16:59 +010020import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
21import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
22
23import android.annotation.TestApi;
24import android.app.ActivityManager.TaskSnapshot;
25import android.graphics.Bitmap;
26import android.graphics.Bitmap.CompressFormat;
Jorim Jaggi2dae8552017-05-02 14:10:58 +020027import android.graphics.Bitmap.Config;
Jorim Jaggif9084ec2017-01-16 13:16:59 +010028import android.os.Process;
29import android.os.SystemClock;
30import android.util.ArraySet;
31import android.util.Slog;
32
33import com.android.internal.annotations.GuardedBy;
34import com.android.internal.annotations.VisibleForTesting;
35import com.android.internal.os.AtomicFile;
36import com.android.server.wm.nano.WindowManagerProtos.TaskSnapshotProto;
37
38import java.io.File;
39import java.io.FileOutputStream;
40import java.io.IOException;
41import java.util.ArrayDeque;
Jorim Jaggief3651c2017-05-18 23:58:09 +020042import java.util.ArrayList;
Jorim Jaggif9084ec2017-01-16 13:16:59 +010043
44/**
45 * Persists {@link TaskSnapshot}s to disk.
46 * <p>
47 * Test class: {@link TaskSnapshotPersisterLoaderTest}
48 */
49class TaskSnapshotPersister {
50
51 private static final String TAG = TAG_WITH_CLASS_NAME ? "TaskSnapshotPersister" : TAG_WM;
52 private static final String SNAPSHOTS_DIRNAME = "snapshots";
Jorim Jaggi35e3f532017-03-17 17:06:50 +010053 private static final String REDUCED_POSTFIX = "_reduced";
54 static final float REDUCED_SCALE = 0.5f;
Jorim Jaggif9084ec2017-01-16 13:16:59 +010055 private static final long DELAY_MS = 100;
Jorim Jaggi35e3f532017-03-17 17:06:50 +010056 private static final int QUALITY = 95;
Jorim Jaggif9084ec2017-01-16 13:16:59 +010057 private static final String PROTO_EXTENSION = ".proto";
Jorim Jaggi35e3f532017-03-17 17:06:50 +010058 private static final String BITMAP_EXTENSION = ".jpg";
Jorim Jaggief3651c2017-05-18 23:58:09 +020059 private static final int MAX_STORE_QUEUE_DEPTH = 2;
Jorim Jaggif9084ec2017-01-16 13:16:59 +010060
61 @GuardedBy("mLock")
62 private final ArrayDeque<WriteQueueItem> mWriteQueue = new ArrayDeque<>();
63 @GuardedBy("mLock")
Jorim Jaggief3651c2017-05-18 23:58:09 +020064 private final ArrayDeque<StoreWriteQueueItem> mStoreQueueItems = new ArrayDeque<>();
65 @GuardedBy("mLock")
Jorim Jaggif9084ec2017-01-16 13:16:59 +010066 private boolean mQueueIdling;
Jorim Jaggia41b7292017-05-11 23:50:34 +020067 @GuardedBy("mLock")
68 private boolean mPaused;
Jorim Jaggif9084ec2017-01-16 13:16:59 +010069 private boolean mStarted;
70 private final Object mLock = new Object();
71 private final DirectoryResolver mDirectoryResolver;
72
73 /**
74 * The list of ids of the tasks that have been persisted since {@link #removeObsoleteFiles} was
75 * called.
76 */
77 @GuardedBy("mLock")
78 private final ArraySet<Integer> mPersistedTaskIdsSinceLastRemoveObsolete = new ArraySet<>();
79
80 TaskSnapshotPersister(DirectoryResolver resolver) {
81 mDirectoryResolver = resolver;
82 }
83
84 /**
85 * Starts persisting.
86 */
87 void start() {
88 if (!mStarted) {
89 mStarted = true;
90 mPersister.start();
91 }
92 }
93
94 /**
95 * Persists a snapshot of a task to disk.
96 *
97 * @param taskId The id of the task that needs to be persisted.
98 * @param userId The id of the user this tasks belongs to.
99 * @param snapshot The snapshot to persist.
100 */
101 void persistSnapshot(int taskId, int userId, TaskSnapshot snapshot) {
102 synchronized (mLock) {
103 mPersistedTaskIdsSinceLastRemoveObsolete.add(taskId);
104 sendToQueueLocked(new StoreWriteQueueItem(taskId, userId, snapshot));
105 }
106 }
107
108 /**
109 * Callend when a task has been removed.
110 *
111 * @param taskId The id of task that has been removed.
112 * @param userId The id of the user the task belonged to.
113 */
114 void onTaskRemovedFromRecents(int taskId, int userId) {
115 synchronized (mLock) {
116 mPersistedTaskIdsSinceLastRemoveObsolete.remove(taskId);
117 sendToQueueLocked(new DeleteWriteQueueItem(taskId, userId));
118 }
119 }
120
121 /**
122 * In case a write/delete operation was lost because the system crashed, this makes sure to
123 * clean up the directory to remove obsolete files.
124 *
125 * @param persistentTaskIds A set of task ids that exist in our in-memory model.
126 * @param runningUserIds The ids of the list of users that have tasks loaded in our in-memory
127 * model.
128 */
129 void removeObsoleteFiles(ArraySet<Integer> persistentTaskIds, int[] runningUserIds) {
130 synchronized (mLock) {
131 mPersistedTaskIdsSinceLastRemoveObsolete.clear();
132 sendToQueueLocked(new RemoveObsoleteFilesQueueItem(persistentTaskIds, runningUserIds));
133 }
134 }
135
Jorim Jaggia41b7292017-05-11 23:50:34 +0200136 void setPaused(boolean paused) {
137 synchronized (mLock) {
138 mPaused = paused;
139 if (!paused) {
140 mLock.notifyAll();
141 }
142 }
143 }
144
Jorim Jaggif9084ec2017-01-16 13:16:59 +0100145 @TestApi
146 void waitForQueueEmpty() {
147 while (true) {
148 synchronized (mLock) {
149 if (mWriteQueue.isEmpty() && mQueueIdling) {
150 return;
151 }
152 }
153 SystemClock.sleep(100);
154 }
155 }
156
157 @GuardedBy("mLock")
158 private void sendToQueueLocked(WriteQueueItem item) {
159 mWriteQueue.offer(item);
Jorim Jaggief3651c2017-05-18 23:58:09 +0200160 item.onQueuedLocked();
161 ensureStoreQueueDepthLocked();
Jorim Jaggia41b7292017-05-11 23:50:34 +0200162 if (!mPaused) {
163 mLock.notifyAll();
164 }
Jorim Jaggif9084ec2017-01-16 13:16:59 +0100165 }
166
Jorim Jaggief3651c2017-05-18 23:58:09 +0200167 @GuardedBy("mLock")
168 private void ensureStoreQueueDepthLocked() {
169 while (mStoreQueueItems.size() > MAX_STORE_QUEUE_DEPTH) {
170 final StoreWriteQueueItem item = mStoreQueueItems.poll();
171 mWriteQueue.remove(item);
172 Slog.i(TAG, "Queue is too deep! Purged item with taskid=" + item.mTaskId);
173 }
174 }
175
Jorim Jaggif9084ec2017-01-16 13:16:59 +0100176 private File getDirectory(int userId) {
177 return new File(mDirectoryResolver.getSystemDirectoryForUser(userId), SNAPSHOTS_DIRNAME);
178 }
179
180 File getProtoFile(int taskId, int userId) {
181 return new File(getDirectory(userId), taskId + PROTO_EXTENSION);
182 }
183
184 File getBitmapFile(int taskId, int userId) {
185 return new File(getDirectory(userId), taskId + BITMAP_EXTENSION);
186 }
187
Jorim Jaggi35e3f532017-03-17 17:06:50 +0100188 File getReducedResolutionBitmapFile(int taskId, int userId) {
189 return new File(getDirectory(userId), taskId + REDUCED_POSTFIX + BITMAP_EXTENSION);
190 }
191
Jorim Jaggif9084ec2017-01-16 13:16:59 +0100192 private boolean createDirectory(int userId) {
193 final File dir = getDirectory(userId);
194 return dir.exists() || dir.mkdirs();
195 }
196
197 private void deleteSnapshot(int taskId, int userId) {
198 final File protoFile = getProtoFile(taskId, userId);
199 final File bitmapFile = getBitmapFile(taskId, userId);
Jorim Jaggi35e3f532017-03-17 17:06:50 +0100200 final File bitmapReducedFile = getReducedResolutionBitmapFile(taskId, userId);
Jorim Jaggif9084ec2017-01-16 13:16:59 +0100201 protoFile.delete();
202 bitmapFile.delete();
Jorim Jaggi35e3f532017-03-17 17:06:50 +0100203 bitmapReducedFile.delete();
Jorim Jaggif9084ec2017-01-16 13:16:59 +0100204 }
205
206 interface DirectoryResolver {
207 File getSystemDirectoryForUser(int userId);
208 }
209
210 private Thread mPersister = new Thread("TaskSnapshotPersister") {
211 public void run() {
212 android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
213 while (true) {
214 WriteQueueItem next;
215 synchronized (mLock) {
Jorim Jaggia41b7292017-05-11 23:50:34 +0200216 if (mPaused) {
217 next = null;
218 } else {
219 next = mWriteQueue.poll();
Jorim Jaggief3651c2017-05-18 23:58:09 +0200220 if (next != null) {
221 next.onDequeuedLocked();
222 }
Jorim Jaggia41b7292017-05-11 23:50:34 +0200223 }
Jorim Jaggif9084ec2017-01-16 13:16:59 +0100224 }
225 if (next != null) {
226 next.write();
227 SystemClock.sleep(DELAY_MS);
228 }
229 synchronized (mLock) {
Jorim Jaggi2f9c7a22017-05-16 14:03:08 +0200230 final boolean writeQueueEmpty = mWriteQueue.isEmpty();
231 if (!writeQueueEmpty && !mPaused) {
Jorim Jaggif9084ec2017-01-16 13:16:59 +0100232 continue;
233 }
234 try {
Jorim Jaggi2f9c7a22017-05-16 14:03:08 +0200235 mQueueIdling = writeQueueEmpty;
Jorim Jaggif9084ec2017-01-16 13:16:59 +0100236 mLock.wait();
237 mQueueIdling = false;
238 } catch (InterruptedException e) {
239 }
240 }
241 }
242 }
243 };
244
245 private abstract class WriteQueueItem {
246 abstract void write();
Jorim Jaggief3651c2017-05-18 23:58:09 +0200247
248 /**
249 * Called when this queue item has been put into the queue.
250 */
251 void onQueuedLocked() {
252 }
253
254 /**
255 * Called when this queue item has been taken out of the queue.
256 */
257 void onDequeuedLocked() {
258 }
Jorim Jaggif9084ec2017-01-16 13:16:59 +0100259 }
260
261 private class StoreWriteQueueItem extends WriteQueueItem {
262 private final int mTaskId;
263 private final int mUserId;
264 private final TaskSnapshot mSnapshot;
265
266 StoreWriteQueueItem(int taskId, int userId, TaskSnapshot snapshot) {
267 mTaskId = taskId;
268 mUserId = userId;
269 mSnapshot = snapshot;
270 }
271
272 @Override
Jorim Jaggief3651c2017-05-18 23:58:09 +0200273 void onQueuedLocked() {
274 mStoreQueueItems.offer(this);
275 }
276
277 @Override
278 void onDequeuedLocked() {
279 mStoreQueueItems.remove(this);
280 }
281
282 @Override
Jorim Jaggif9084ec2017-01-16 13:16:59 +0100283 void write() {
284 if (!createDirectory(mUserId)) {
285 Slog.e(TAG, "Unable to create snapshot directory for user dir="
286 + getDirectory(mUserId));
287 }
288 boolean failed = false;
289 if (!writeProto()) {
290 failed = true;
291 }
292 if (!writeBuffer()) {
293 writeBuffer();
294 failed = true;
295 }
296 if (failed) {
297 deleteSnapshot(mTaskId, mUserId);
298 }
299 }
300
301 boolean writeProto() {
302 final TaskSnapshotProto proto = new TaskSnapshotProto();
303 proto.orientation = mSnapshot.getOrientation();
304 proto.insetLeft = mSnapshot.getContentInsets().left;
305 proto.insetTop = mSnapshot.getContentInsets().top;
306 proto.insetRight = mSnapshot.getContentInsets().right;
307 proto.insetBottom = mSnapshot.getContentInsets().bottom;
308 final byte[] bytes = TaskSnapshotProto.toByteArray(proto);
309 final File file = getProtoFile(mTaskId, mUserId);
310 final AtomicFile atomicFile = new AtomicFile(file);
311 FileOutputStream fos = null;
312 try {
313 fos = atomicFile.startWrite();
314 fos.write(bytes);
315 atomicFile.finishWrite(fos);
316 } catch (IOException e) {
317 atomicFile.failWrite(fos);
318 Slog.e(TAG, "Unable to open " + file + " for persisting. " + e);
319 return false;
320 }
321 return true;
322 }
323
324 boolean writeBuffer() {
325 final File file = getBitmapFile(mTaskId, mUserId);
Jorim Jaggi35e3f532017-03-17 17:06:50 +0100326 final File reducedFile = getReducedResolutionBitmapFile(mTaskId, mUserId);
Jorim Jaggif9084ec2017-01-16 13:16:59 +0100327 final Bitmap bitmap = Bitmap.createHardwareBitmap(mSnapshot.getSnapshot());
Jorim Jaggi2dae8552017-05-02 14:10:58 +0200328 final Bitmap swBitmap = bitmap.copy(Config.ARGB_8888, false /* isMutable */);
329 final Bitmap reduced = Bitmap.createScaledBitmap(swBitmap,
Jorim Jaggi35e3f532017-03-17 17:06:50 +0100330 (int) (bitmap.getWidth() * REDUCED_SCALE),
331 (int) (bitmap.getHeight() * REDUCED_SCALE), true /* filter */);
Jorim Jaggif9084ec2017-01-16 13:16:59 +0100332 try {
333 FileOutputStream fos = new FileOutputStream(file);
Jorim Jaggi2dae8552017-05-02 14:10:58 +0200334 swBitmap.compress(JPEG, QUALITY, fos);
Jorim Jaggif9084ec2017-01-16 13:16:59 +0100335 fos.close();
Jorim Jaggi35e3f532017-03-17 17:06:50 +0100336 FileOutputStream reducedFos = new FileOutputStream(reducedFile);
337 reduced.compress(JPEG, QUALITY, reducedFos);
338 reducedFos.close();
Jorim Jaggif9084ec2017-01-16 13:16:59 +0100339 } catch (IOException e) {
Jorim Jaggi35e3f532017-03-17 17:06:50 +0100340 Slog.e(TAG, "Unable to open " + file + " or " + reducedFile +" for persisting.", e);
Jorim Jaggif9084ec2017-01-16 13:16:59 +0100341 return false;
342 }
343 return true;
344 }
345 }
346
347 private class DeleteWriteQueueItem extends WriteQueueItem {
348 private final int mTaskId;
349 private final int mUserId;
350
351 DeleteWriteQueueItem(int taskId, int userId) {
352 mTaskId = taskId;
353 mUserId = userId;
354 }
355
356 @Override
357 void write() {
358 deleteSnapshot(mTaskId, mUserId);
359 }
360 }
361
362 @VisibleForTesting
363 class RemoveObsoleteFilesQueueItem extends WriteQueueItem {
364 private final ArraySet<Integer> mPersistentTaskIds;
365 private final int[] mRunningUserIds;
366
367 @VisibleForTesting
368 RemoveObsoleteFilesQueueItem(ArraySet<Integer> persistentTaskIds,
369 int[] runningUserIds) {
370 mPersistentTaskIds = persistentTaskIds;
371 mRunningUserIds = runningUserIds;
372 }
373
374 @Override
375 void write() {
376 final ArraySet<Integer> newPersistedTaskIds;
377 synchronized (mLock) {
378 newPersistedTaskIds = new ArraySet<>(mPersistedTaskIdsSinceLastRemoveObsolete);
379 }
380 for (int userId : mRunningUserIds) {
381 final File dir = getDirectory(userId);
382 final String[] files = dir.list();
383 if (files == null) {
384 continue;
385 }
386 for (String file : files) {
387 final int taskId = getTaskId(file);
388 if (!mPersistentTaskIds.contains(taskId)
389 && !newPersistedTaskIds.contains(taskId)) {
390 new File(dir, file).delete();
391 }
392 }
393 }
394 }
395
396 @VisibleForTesting
397 int getTaskId(String fileName) {
398 if (!fileName.endsWith(PROTO_EXTENSION) && !fileName.endsWith(BITMAP_EXTENSION)) {
399 return -1;
400 }
401 final int end = fileName.lastIndexOf('.');
402 if (end == -1) {
403 return -1;
404 }
Jorim Jaggi35e3f532017-03-17 17:06:50 +0100405 String name = fileName.substring(0, end);
406 if (name.endsWith(REDUCED_POSTFIX)) {
407 name = name.substring(0, name.length() - REDUCED_POSTFIX.length());
408 }
Jorim Jaggif9084ec2017-01-16 13:16:59 +0100409 try {
Jorim Jaggi35e3f532017-03-17 17:06:50 +0100410 return Integer.parseInt(name);
Jorim Jaggif9084ec2017-01-16 13:16:59 +0100411 } catch (NumberFormatException e) {
412 return -1;
413 }
414 }
415 }
416}