Allow for synchronously applying transactions with a SurfaceView

Exposing this allows for atomically updating a SurfaceControl hierarchy
when new frames arrive as the SurfaceView's content. For instance, a
streaming application can blank out the Surface when switching video
source files to prevent older frames from lingering.

Bug: 187309567
Test: SurfaceViewTests
Change-Id: I9b4df8b3312506f014f97c7f4a890b8242578ede
This commit is contained in:
Alec Mouri
2023-01-10 06:55:21 +00:00
parent 0eb1f98212
commit 53c47d1d40
2 changed files with 37 additions and 2 deletions

View File

@@ -51451,6 +51451,7 @@ package android.view {
ctor public SurfaceView(android.content.Context, android.util.AttributeSet);
ctor public SurfaceView(android.content.Context, android.util.AttributeSet, int);
ctor public SurfaceView(android.content.Context, android.util.AttributeSet, int, int);
method public void applyTransactionToFrame(@NonNull android.view.SurfaceControl.Transaction);
method public android.view.SurfaceHolder getHolder();
method @Nullable public android.os.IBinder getHostToken();
method public android.view.SurfaceControl getSurfaceControl();

View File

@@ -1756,8 +1756,14 @@ public class SurfaceView extends View implements ViewRootImpl.SurfaceChangedCall
};
/**
* Return a SurfaceControl which can be used for parenting Surfaces to
* this SurfaceView.
* Return a SurfaceControl which can be used for parenting Surfaces to this SurfaceView.
*
* Note that this SurfaceControl is effectively read-only. Its only well-defined usage is in
* using the SurfaceControl as a parent for an application's hierarchy of SurfaceControls. All
* other properties of the SurfaceControl, such as its position, may be mutated by the
* SurfaceView at any time which will override what the application is requesting. Do not apply
* any {@link SurfaceControl.Transaction} to this SurfaceControl except for reparenting
* child SurfaceControls. See: {@link SurfaceControl.Transaction#reparent}.
*
* @return The SurfaceControl for this SurfaceView.
*/
@@ -1983,4 +1989,32 @@ public class SurfaceView extends View implements ViewRootImpl.SurfaceChangedCall
public void syncNextFrame(Consumer<Transaction> t) {
mBlastBufferQueue.syncNextTransaction(t);
}
/**
* Adds a transaction that would be applied synchronously with displaying the SurfaceView's next
* frame.
*
* Note that the exact frame that the transaction is applied with is only well-defined when
* SurfaceView rendering is paused prior to calling applyTransactionToFrame(), so that the
* transaction is applied with the next frame rendered after applyTransactionToFrame() is
* called. If frames are continuously rendering to the SurfaceView when
* applyTransactionToFrame() is called, then it is undefined which frame the transaction is
* applied with. It is also possible for the transaction to not be applied if no new frames are
* rendered to the SurfaceView after this is called.
*
* @param transaction The transaction to apply. The system takes ownership of the transaction
* and promises to eventually apply the transaction.
* @throws IllegalStateException if the underlying Surface does not exist (and therefore
* there is no next frame).
*/
public void applyTransactionToFrame(@NonNull SurfaceControl.Transaction transaction) {
synchronized (mSurfaceControlLock) {
if (mBlastBufferQueue == null) {
throw new IllegalStateException("Surface does not exist!");
}
long frameNumber = mBlastBufferQueue.getLastAcquiredFrameNum() + 1;
mBlastBufferQueue.mergeWithNextTransaction(transaction, frameNumber);
}
}
}