blob: 1410c21a1c818810d6f425fa67ad226117855481 [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 com.android.server.wm.TaskSnapshotPersister.*;
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.app.ActivityManager.TaskSnapshot;
24import android.graphics.Bitmap;
25import android.graphics.Bitmap.Config;
26import android.graphics.BitmapFactory;
27import android.graphics.BitmapFactory.Options;
28import android.graphics.GraphicBuffer;
29import android.graphics.Rect;
30import android.util.Slog;
31
32import com.android.server.wm.nano.WindowManagerProtos.TaskSnapshotProto;
33
34import java.io.File;
35import java.io.IOException;
36import java.nio.file.Files;
37
38/**
39 * Loads a persisted {@link TaskSnapshot} from disk.
40 * <p>
41 * Do not hold the window manager lock when accessing this class.
42 * <p>
43 * Test class: {@link TaskSnapshotPersisterLoaderTest}
44 */
45class TaskSnapshotLoader {
46
47 private static final String TAG = TAG_WITH_CLASS_NAME ? "TaskSnapshotLoader" : TAG_WM;
48
49 private final TaskSnapshotPersister mPersister;
50
51 TaskSnapshotLoader(TaskSnapshotPersister persister) {
52 mPersister = persister;
53 }
54
55 /**
56 * Loads a task from the disk.
57 * <p>
58 * Do not hold the window manager lock when calling this method, as we directly read data from
59 * disk here, which might be slow.
60 *
61 * @param taskId The id of the task to load.
62 * @param userId The id of the user the task belonged to.
Jorim Jaggi35e3f532017-03-17 17:06:50 +010063 * @param reducedResolution Whether to load a reduced resolution version of the snapshot.
Jorim Jaggif9084ec2017-01-16 13:16:59 +010064 * @return The loaded {@link TaskSnapshot} or {@code null} if it couldn't be loaded.
65 */
Jorim Jaggi35e3f532017-03-17 17:06:50 +010066 TaskSnapshot loadTask(int taskId, int userId, boolean reducedResolution) {
Jorim Jaggif9084ec2017-01-16 13:16:59 +010067 final File protoFile = mPersister.getProtoFile(taskId, userId);
Jorim Jaggi35e3f532017-03-17 17:06:50 +010068 final File bitmapFile = reducedResolution
69 ? mPersister.getReducedResolutionBitmapFile(taskId, userId)
70 : mPersister.getBitmapFile(taskId, userId);
Matthew Ng3531dd02017-08-23 16:41:57 -070071 if (bitmapFile == null || !protoFile.exists() || !bitmapFile.exists()) {
Jorim Jaggif9084ec2017-01-16 13:16:59 +010072 return null;
73 }
74 try {
75 final byte[] bytes = Files.readAllBytes(protoFile.toPath());
76 final TaskSnapshotProto proto = TaskSnapshotProto.parseFrom(bytes);
77 final Options options = new Options();
78 options.inPreferredConfig = Config.HARDWARE;
79 final Bitmap bitmap = BitmapFactory.decodeFile(bitmapFile.getPath(), options);
80 if (bitmap == null) {
81 Slog.w(TAG, "Failed to load bitmap: " + bitmapFile.getPath());
82 return null;
83 }
84 final GraphicBuffer buffer = bitmap.createGraphicBufferHandle();
85 if (buffer == null) {
86 Slog.w(TAG, "Failed to retrieve gralloc buffer for bitmap: "
87 + bitmapFile.getPath());
88 return null;
89 }
90 return new TaskSnapshot(buffer, proto.orientation,
Jorim Jaggi35e3f532017-03-17 17:06:50 +010091 new Rect(proto.insetLeft, proto.insetTop, proto.insetRight, proto.insetBottom),
Winson Chungf3e412e2018-03-08 11:07:40 -080092 reducedResolution, reducedResolution ? REDUCED_SCALE : 1f,
Winson Chung173020c2018-05-04 15:36:47 -070093 proto.isRealSnapshot, proto.windowingMode, proto.systemUiVisibility,
94 proto.isTranslucent);
Jorim Jaggif9084ec2017-01-16 13:16:59 +010095 } catch (IOException e) {
96 Slog.w(TAG, "Unable to load task snapshot data for taskId=" + taskId);
97 return null;
98 }
99 }
100}