Merge "Clear snapshot buffer preemptively if unused" into tm-dev am: 87b7e43b14

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/18800614

Change-Id: I666cc9688530619fa8b5f71d2e4aa9e2e7c51aca
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Winson Chung
2022-06-14 16:06:45 +00:00
committed by Automerger Merge Worker
6 changed files with 42 additions and 5 deletions

View File

@@ -54,7 +54,13 @@ public interface TaskStackListenerCallback {
default void onTaskDescriptionChanged(RunningTaskInfo taskInfo) { } default void onTaskDescriptionChanged(RunningTaskInfo taskInfo) { }
default void onTaskSnapshotChanged(int taskId, TaskSnapshot snapshot) { } /**
* @return whether the snapshot is consumed and the lifecycle of the snapshot extends beyond
* the lifecycle of this callback.
*/
default boolean onTaskSnapshotChanged(int taskId, TaskSnapshot snapshot) {
return false;
}
default void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) { } default void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) { }

View File

@@ -275,9 +275,15 @@ public class TaskStackListenerImpl extends TaskStackListener implements Handler.
} }
case ON_TASK_SNAPSHOT_CHANGED: { case ON_TASK_SNAPSHOT_CHANGED: {
Trace.beginSection("onTaskSnapshotChanged"); Trace.beginSection("onTaskSnapshotChanged");
final TaskSnapshot snapshot = (TaskSnapshot) msg.obj;
boolean snapshotConsumed = false;
for (int i = mTaskStackListeners.size() - 1; i >= 0; i--) { for (int i = mTaskStackListeners.size() - 1; i >= 0; i--) {
mTaskStackListeners.get(i).onTaskSnapshotChanged(msg.arg1, boolean consumed = mTaskStackListeners.get(i).onTaskSnapshotChanged(
(TaskSnapshot) msg.obj); msg.arg1, snapshot);
snapshotConsumed |= consumed;
}
if (!snapshotConsumed && snapshot.getHardwareBuffer() != null) {
snapshot.getHardwareBuffer().close();
} }
Trace.endSection(); Trace.endSection();
break; break;

View File

@@ -389,6 +389,9 @@ public class TaskSnapshotWindow {
reportDrawn(); reportDrawn();
// In case window manager leaks us, make sure we don't retain the snapshot. // In case window manager leaks us, make sure we don't retain the snapshot.
if (mSnapshot.getHardwareBuffer() != null) {
mSnapshot.getHardwareBuffer().close();
}
mSnapshot = null; mSnapshot = null;
mSurfaceControl.release(); mSurfaceControl.release();
} }

View File

@@ -65,6 +65,12 @@ public class ThumbnailData {
snapshotId = 0; snapshotId = 0;
} }
public void recycleBitmap() {
if (thumbnail != null) {
thumbnail.recycle();
}
}
private static Bitmap makeThumbnail(TaskSnapshot snapshot) { private static Bitmap makeThumbnail(TaskSnapshot snapshot) {
Bitmap thumbnail = null; Bitmap thumbnail = null;
try (final HardwareBuffer buffer = snapshot.getHardwareBuffer()) { try (final HardwareBuffer buffer = snapshot.getHardwareBuffer()) {

View File

@@ -33,7 +33,14 @@ public interface TaskStackChangeListener {
// Main thread callbacks // Main thread callbacks
default void onTaskStackChanged() { } default void onTaskStackChanged() { }
default void onTaskSnapshotChanged(int taskId, ThumbnailData snapshot) { }
/**
* @return whether the snapshot is consumed and the lifecycle of the snapshot extends beyond
* the lifecycle of this callback.
*/
default boolean onTaskSnapshotChanged(int taskId, ThumbnailData snapshot) {
return false;
}
default void onActivityPinned(String packageName, int userId, int taskId, int stackId) { } default void onActivityPinned(String packageName, int userId, int taskId, int stackId) { }
default void onActivityUnpinned() { } default void onActivityUnpinned() { }
default void onActivityRestartAttempt(RunningTaskInfo task, boolean homeTaskVisible, default void onActivityRestartAttempt(RunningTaskInfo task, boolean homeTaskVisible,

View File

@@ -294,8 +294,17 @@ public class TaskStackChangeListeners {
Trace.beginSection("onTaskSnapshotChanged"); Trace.beginSection("onTaskSnapshotChanged");
final TaskSnapshot snapshot = (TaskSnapshot) msg.obj; final TaskSnapshot snapshot = (TaskSnapshot) msg.obj;
final ThumbnailData thumbnail = new ThumbnailData(snapshot); final ThumbnailData thumbnail = new ThumbnailData(snapshot);
boolean snapshotConsumed = false;
for (int i = mTaskStackListeners.size() - 1; i >= 0; i--) { for (int i = mTaskStackListeners.size() - 1; i >= 0; i--) {
mTaskStackListeners.get(i).onTaskSnapshotChanged(msg.arg1, thumbnail); boolean consumed = mTaskStackListeners.get(i).onTaskSnapshotChanged(
msg.arg1, thumbnail);
snapshotConsumed |= consumed;
}
if (!snapshotConsumed) {
thumbnail.recycleBitmap();
if (snapshot.getHardwareBuffer() != null) {
snapshot.getHardwareBuffer().close();
}
} }
Trace.endSection(); Trace.endSection();
break; break;