From b332d485c0e0af8f224d5734f6badd283b8d192e Mon Sep 17 00:00:00 2001 From: Vishnu Nair Date: Wed, 19 Apr 2023 09:53:51 -0700 Subject: [PATCH] [sf] Release the currently presented buffer when setBuffer is called with null Update callers to explictly unset the buffer when they mean to drop the buffer from the transaction instead of passing a null buffer. Bug: 241271897 Test: presubmit Change-Id: I6d90810be40f96d2a62d21a3d7e769c2a5c3098d --- core/java/android/view/SurfaceControl.java | 20 ++++++++++++++++++- core/jni/android_view_SurfaceControl.cpp | 8 ++++++++ .../com/android/server/wm/WindowState.java | 2 +- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/core/java/android/view/SurfaceControl.java b/core/java/android/view/SurfaceControl.java index bc6a3b540ce7a..2fa03c5ba92ee 100644 --- a/core/java/android/view/SurfaceControl.java +++ b/core/java/android/view/SurfaceControl.java @@ -220,6 +220,7 @@ public final class SurfaceControl implements Parcelable { long newParentNativeObject); private static native void nativeSetBuffer(long transactionObj, long nativeObject, HardwareBuffer buffer, long fencePtr, Consumer releaseCallback); + private static native void nativeUnsetBuffer(long transactionObj, long nativeObject); private static native void nativeSetBufferTransform(long transactionObj, long nativeObject, int transform); private static native void nativeSetDataSpace(long transactionObj, long nativeObject, @@ -3663,6 +3664,22 @@ public final class SurfaceControl implements Parcelable { return setBuffer(sc, buffer, null); } + /** + * Unsets the buffer for the SurfaceControl in the current Transaction. This will not clear + * the buffer being rendered, but resets the buffer state in the Transaction only. The call + * will also invoke the release callback. + * + * Note, this call is different from passing a null buffer to + * {@link SurfaceControl.Transaction#setBuffer} which will release the last displayed + * buffer. + * + * @hide + */ + public Transaction unsetBuffer(SurfaceControl sc) { + nativeUnsetBuffer(mNativeObject, sc.mNativeObject); + return this; + } + /** * Updates the HardwareBuffer displayed for the SurfaceControl. * @@ -3682,7 +3699,8 @@ public final class SurfaceControl implements Parcelable { * until all presentation fences have signaled, ensuring the transaction remains consistent. * * @param sc The SurfaceControl to update - * @param buffer The buffer to be displayed + * @param buffer The buffer to be displayed. Pass in a null buffer to release the last + * displayed buffer. * @param fence The presentation fence. If null or invalid, this is equivalent to * {@link #setBuffer(SurfaceControl, HardwareBuffer)} * @return this diff --git a/core/jni/android_view_SurfaceControl.cpp b/core/jni/android_view_SurfaceControl.cpp index e42c6f107e6dd..9fb462ec7c982 100644 --- a/core/jni/android_view_SurfaceControl.cpp +++ b/core/jni/android_view_SurfaceControl.cpp @@ -613,6 +613,12 @@ static void nativeSetBuffer(JNIEnv* env, jclass clazz, jlong transactionObj, jlo genReleaseCallback(env, releaseCallback)); } +static void nativeUnsetBuffer(JNIEnv* env, jclass clazz, jlong transactionObj, jlong nativeObject) { + auto transaction = reinterpret_cast(transactionObj); + SurfaceControl* const ctrl = reinterpret_cast(nativeObject); + transaction->unsetBuffer(ctrl); +} + static void nativeSetBufferTransform(JNIEnv* env, jclass clazz, jlong transactionObj, jlong nativeObject, jint transform) { auto transaction = reinterpret_cast(transactionObj); @@ -2195,6 +2201,8 @@ static const JNINativeMethod sSurfaceControlMethods[] = { (void*)nativeSetGeometry }, {"nativeSetBuffer", "(JJLandroid/hardware/HardwareBuffer;JLjava/util/function/Consumer;)V", (void*)nativeSetBuffer }, + {"nativeUnsetBuffer", "(JJ)V", (void*)nativeUnsetBuffer }, + {"nativeSetBufferTransform", "(JJI)V", (void*) nativeSetBufferTransform}, {"nativeSetDataSpace", "(JJI)V", (void*)nativeSetDataSpace }, diff --git a/services/core/java/com/android/server/wm/WindowState.java b/services/core/java/com/android/server/wm/WindowState.java index e5a49c3a0ee2c..fc29771b60ea9 100644 --- a/services/core/java/com/android/server/wm/WindowState.java +++ b/services/core/java/com/android/server/wm/WindowState.java @@ -5602,7 +5602,7 @@ class WindowState extends WindowContainer implements WindowManagerP private void dropBufferFrom(Transaction t) { SurfaceControl viewSurface = getClientViewRootSurface(); if (viewSurface == null) return; - t.setBuffer(viewSurface, (android.hardware.HardwareBuffer) null); + t.unsetBuffer(viewSurface); } @Override