Merge "Clear snapshot buffer preemptively if unused" into tm-dev

This commit is contained in:
Winson Chung
2022-06-14 13:56:10 +00:00
committed by Android (Google) Code Review
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 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) { }

View File

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

View File

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

View File

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

View File

@@ -33,7 +33,14 @@ public interface TaskStackChangeListener {
// Main thread callbacks
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 onActivityUnpinned() { }
default void onActivityRestartAttempt(RunningTaskInfo task, boolean homeTaskVisible,

View File

@@ -294,8 +294,17 @@ public class TaskStackChangeListeners {
Trace.beginSection("onTaskSnapshotChanged");
final TaskSnapshot snapshot = (TaskSnapshot) msg.obj;
final ThumbnailData thumbnail = new ThumbnailData(snapshot);
boolean snapshotConsumed = false;
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();
break;