Merge "Refactor SurfaceSyncer to use SurfaceSyncGroups instead of syncId"
diff --git a/core/java/android/view/AttachedSurfaceControl.java b/core/java/android/view/AttachedSurfaceControl.java
index bd468d9..5dae313 100644
--- a/core/java/android/view/AttachedSurfaceControl.java
+++ b/core/java/android/view/AttachedSurfaceControl.java
@@ -17,10 +17,10 @@
import android.annotation.NonNull;
import android.annotation.Nullable;
-import android.annotation.SystemApi;
import android.annotation.UiThread;
import android.graphics.Region;
import android.hardware.HardwareBuffer;
+import android.window.SurfaceSyncGroup;
/**
* Provides an interface to the root-Surface of a View Hierarchy or Window. This
@@ -138,4 +138,12 @@
*/
default void setTouchableRegion(@Nullable Region r) {
}
+
+ /**
+ * Returns a SyncTarget that can be used to sync {@link AttachedSurfaceControl} in a
+ * {@link SurfaceSyncGroup}
+ *
+ * @hide
+ */
+ SurfaceSyncGroup.SyncTarget getSyncTarget();
}
diff --git a/core/java/android/view/SurfaceView.java b/core/java/android/view/SurfaceView.java
index c7dbc07..70a5eda 100644
--- a/core/java/android/view/SurfaceView.java
+++ b/core/java/android/view/SurfaceView.java
@@ -47,7 +47,7 @@
import android.view.SurfaceControl.Transaction;
import android.view.accessibility.AccessibilityNodeInfo;
import android.view.accessibility.IAccessibilityEmbeddedConnection;
-import android.window.SurfaceSyncer;
+import android.window.SurfaceSyncGroup;
import com.android.internal.view.SurfaceCallbackHelper;
@@ -210,8 +210,7 @@
private int mSurfaceFlags = SurfaceControl.HIDDEN;
- private final SurfaceSyncer mSurfaceSyncer = new SurfaceSyncer();
- private final ArraySet<Integer> mSyncIds = new ArraySet<>();
+ private final ArraySet<SurfaceSyncGroup> mSyncGroups = new ArraySet<>();
/**
* Transaction that should be used from the render thread. This transaction is only thread safe
@@ -1077,20 +1076,21 @@
}
private void handleSyncNoBuffer(SurfaceHolder.Callback[] callbacks) {
- final int syncId = mSurfaceSyncer.setupSync(this::onDrawFinished);
+ final SurfaceSyncGroup syncGroup = new SurfaceSyncGroup();
+ synchronized (mSyncGroups) {
+ mSyncGroups.add(syncGroup);
+ }
- mSurfaceSyncer.addToSync(syncId, syncBufferCallback -> redrawNeededAsync(callbacks,
+ syncGroup.addToSync(syncBufferCallback -> redrawNeededAsync(callbacks,
() -> {
syncBufferCallback.onBufferReady(null);
- synchronized (mSyncIds) {
- mSyncIds.remove(syncId);
+ onDrawFinished();
+ synchronized (mSyncGroups) {
+ mSyncGroups.remove(syncGroup);
}
}));
- mSurfaceSyncer.markSyncReady(syncId);
- synchronized (mSyncIds) {
- mSyncIds.add(syncId);
- }
+ syncGroup.markSyncReady();
}
private void redrawNeededAsync(SurfaceHolder.Callback[] callbacks,
@@ -1106,9 +1106,9 @@
public void surfaceSyncStarted() {
ViewRootImpl viewRoot = getViewRootImpl();
if (viewRoot != null) {
- synchronized (mSyncIds) {
- for (int syncId : mSyncIds) {
- viewRoot.mergeSync(syncId, mSurfaceSyncer);
+ synchronized (mSyncGroups) {
+ for (SurfaceSyncGroup syncGroup : mSyncGroups) {
+ viewRoot.mergeSync(syncGroup);
}
}
}
diff --git a/core/java/android/view/ViewRootImpl.java b/core/java/android/view/ViewRootImpl.java
index fbfeac7..c1a5a3a2 100644
--- a/core/java/android/view/ViewRootImpl.java
+++ b/core/java/android/view/ViewRootImpl.java
@@ -197,7 +197,7 @@
import android.window.CompatOnBackInvokedCallback;
import android.window.OnBackInvokedCallback;
import android.window.OnBackInvokedDispatcher;
-import android.window.SurfaceSyncer;
+import android.window.SurfaceSyncGroup;
import android.window.WindowOnBackInvokedDispatcher;
import com.android.internal.R;
@@ -829,9 +829,8 @@
return mHandwritingInitiator;
}
- private final SurfaceSyncer mSurfaceSyncer = new SurfaceSyncer();
- private int mSyncId = UNSET_SYNC_ID;
- private SurfaceSyncer.SyncBufferCallback mSyncBufferCallback;
+ private SurfaceSyncGroup mSyncGroup;
+ private SurfaceSyncGroup.SyncBufferCallback mSyncBufferCallback;
private int mNumSyncsInProgress = 0;
private HashSet<ScrollCaptureCallback> mRootScrollCaptureCallbacks;
@@ -3594,8 +3593,8 @@
mSyncBufferCallback = null;
mSyncBuffer = false;
if (isInLocalSync()) {
- mSurfaceSyncer.markSyncReady(mSyncId);
- mSyncId = UNSET_SYNC_ID;
+ mSyncGroup.markSyncReady();
+ mSyncGroup = null;
}
}
}
@@ -3607,17 +3606,19 @@
}
final int seqId = mSyncSeqId;
- mSyncId = mSurfaceSyncer.setupSync(transaction -> {
+ mSyncGroup = new SurfaceSyncGroup(transaction -> {
// Callback will be invoked on executor thread so post to main thread.
mHandler.postAtFrontOfQueue(() -> {
- mSurfaceChangedTransaction.merge(transaction);
+ if (transaction != null) {
+ mSurfaceChangedTransaction.merge(transaction);
+ }
reportDrawFinished(seqId);
});
});
if (DEBUG_BLAST) {
- Log.d(mTag, "Setup new sync id=" + mSyncId);
+ Log.d(mTag, "Setup new sync id=" + mSyncGroup);
}
- mSurfaceSyncer.addToSync(mSyncId, mSyncTarget);
+ mSyncGroup.addToSync(mSyncTarget);
notifySurfaceSyncStarted();
}
@@ -4255,11 +4256,11 @@
return mAttachInfo.mThreadedRenderer != null && mAttachInfo.mThreadedRenderer.isEnabled();
}
- void addToSync(SurfaceSyncer.SyncTarget syncable) {
+ void addToSync(SurfaceSyncGroup.SyncTarget syncable) {
if (!isInLocalSync()) {
return;
}
- mSurfaceSyncer.addToSync(mSyncId, syncable);
+ mSyncGroup.addToSync(syncable);
}
/**
@@ -4267,7 +4268,7 @@
* within VRI.
*/
public boolean isInLocalSync() {
- return mSyncId != UNSET_SYNC_ID;
+ return mSyncGroup != null;
}
private void addFrameCommitCallbackIfNeeded() {
@@ -4392,7 +4393,7 @@
}
if (mSurfaceHolder != null && mSurface.isValid()) {
- final SurfaceSyncer.SyncBufferCallback syncBufferCallback = mSyncBufferCallback;
+ final SurfaceSyncGroup.SyncBufferCallback syncBufferCallback = mSyncBufferCallback;
SurfaceCallbackHelper sch = new SurfaceCallbackHelper(() ->
mHandler.post(() -> syncBufferCallback.onBufferReady(null)));
mSyncBufferCallback = null;
@@ -10929,7 +10930,7 @@
}
private void registerCallbacksForSync(boolean syncBuffer,
- final SurfaceSyncer.SyncBufferCallback syncBufferCallback) {
+ final SurfaceSyncGroup.SyncBufferCallback syncBufferCallback) {
if (!isHardwareEnabled()) {
return;
}
@@ -11002,9 +11003,9 @@
});
}
- public final SurfaceSyncer.SyncTarget mSyncTarget = new SurfaceSyncer.SyncTarget() {
+ public final SurfaceSyncGroup.SyncTarget mSyncTarget = new SurfaceSyncGroup.SyncTarget() {
@Override
- public void onReadyToSync(SurfaceSyncer.SyncBufferCallback syncBufferCallback) {
+ public void onReadyToSync(SurfaceSyncGroup.SyncBufferCallback syncBufferCallback) {
readyToSync(syncBufferCallback);
}
@@ -11018,7 +11019,12 @@
}
};
- private void readyToSync(SurfaceSyncer.SyncBufferCallback syncBufferCallback) {
+ @Override
+ public SurfaceSyncGroup.SyncTarget getSyncTarget() {
+ return mSyncTarget;
+ }
+
+ private void readyToSync(SurfaceSyncGroup.SyncBufferCallback syncBufferCallback) {
mNumSyncsInProgress++;
if (!isInLocalSync()) {
// Always sync the buffer if the sync request did not come from VRI.
@@ -11041,10 +11047,10 @@
}
}
- void mergeSync(int syncId, SurfaceSyncer otherSyncer) {
+ void mergeSync(SurfaceSyncGroup otherSyncGroup) {
if (!isInLocalSync()) {
return;
}
- mSurfaceSyncer.merge(mSyncId, syncId, otherSyncer);
+ mSyncGroup.merge(otherSyncGroup);
}
}
diff --git a/core/java/android/window/SurfaceSyncGroup.java b/core/java/android/window/SurfaceSyncGroup.java
new file mode 100644
index 0000000..5672697
--- /dev/null
+++ b/core/java/android/window/SurfaceSyncGroup.java
@@ -0,0 +1,388 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.window;
+
+import android.annotation.Nullable;
+import android.annotation.UiThread;
+import android.util.ArraySet;
+import android.util.Log;
+import android.util.Pair;
+import android.view.AttachedSurfaceControl;
+import android.view.SurfaceControl.Transaction;
+import android.view.SurfaceView;
+
+import com.android.internal.annotations.GuardedBy;
+
+import java.util.Set;
+import java.util.concurrent.Executor;
+import java.util.function.Consumer;
+import java.util.function.Supplier;
+
+/**
+ * Used to organize syncs for surfaces.
+ *
+ * The SurfaceSyncGroup allows callers to add desired syncs into a set and wait for them to all
+ * complete before getting a callback. The purpose of the SurfaceSyncGroup is to be an accounting
+ * mechanism so each sync implementation doesn't need to handle it themselves. The SurfaceSyncGroup
+ * class is used the following way.
+ *
+ * 1. {@link #SurfaceSyncGroup()} constructor is called
+ * 2. {@link #addToSync(SyncTarget)} is called for every SyncTarget object that wants to be
+ * included in the sync. If the addSync is called for an {@link AttachedSurfaceControl} or
+ * {@link SurfaceView} it needs to be called on the UI thread. When addToSync is called, it's
+ * guaranteed that any UI updates that were requested before addToSync but after the last frame
+ * drew, will be included in the sync.
+ * 3. {@link #markSyncReady()} should be called when all the {@link SyncTarget}s have been added
+ * to the SurfaceSyncGroup. At this point, the SurfaceSyncGroup is closed and no more SyncTargets
+ * can be added to it.
+ * 4. The SurfaceSyncGroup will gather the data for each SyncTarget using the steps described below.
+ * When all the SyncTargets have finished, the syncRequestComplete will be invoked and the
+ * transaction will either be applied or sent to the caller. In most cases, only the
+ * SurfaceSyncGroup should be handling the Transaction object directly. However, there are some
+ * cases where the framework needs to send the Transaction elsewhere, like in ViewRootImpl, so that
+ * option is provided.
+ *
+ * The following is what happens within the {@link SurfaceSyncGroup}
+ * 1. Each SyncTarget will get a {@link SyncTarget#onReadyToSync} callback that contains a
+ * {@link SyncBufferCallback}.
+ * 2. Each {@link SyncTarget} needs to invoke {@link SyncBufferCallback#onBufferReady(Transaction)}.
+ * This makes sure the SurfaceSyncGroup knows when the SyncTarget is complete, allowing the
+ * SurfaceSyncGroup to get the Transaction that contains the buffer.
+ * 3. When the final SyncBufferCallback finishes for the SurfaceSyncGroup, in most cases the
+ * transaction is applied and then the sync complete callbacks are invoked, letting the callers know
+ * the sync is now complete.
+ *
+ * @hide
+ */
+public final class SurfaceSyncGroup {
+ private static final String TAG = "SurfaceSyncGroup";
+ private static final boolean DEBUG = false;
+
+ private static Supplier<Transaction> sTransactionFactory = Transaction::new;
+
+ /**
+ * Class that collects the {@link SyncTarget}s and notifies when all the surfaces have
+ * a frame ready.
+ */
+ private final Object mLock = new Object();
+
+ @GuardedBy("mLock")
+ private final Set<Integer> mPendingSyncs = new ArraySet<>();
+ @GuardedBy("mLock")
+ private final Transaction mTransaction = sTransactionFactory.get();
+ @GuardedBy("mLock")
+ private boolean mSyncReady;
+ @GuardedBy("mLock")
+ private final Set<SyncTarget> mSyncTargets = new ArraySet<>();
+
+ @GuardedBy("mLock")
+ private Consumer<Transaction> mSyncRequestCompleteCallback;
+
+ @GuardedBy("mLock")
+ private final Set<SurfaceSyncGroup> mMergedSyncGroups = new ArraySet<>();
+
+ @GuardedBy("mLock")
+ private boolean mFinished;
+
+ @GuardedBy("mLock")
+ private final ArraySet<Pair<Executor, Runnable>> mSyncCompleteCallbacks = new ArraySet<>();
+
+ /**
+ * @hide
+ */
+ public static void setTransactionFactory(Supplier<Transaction> transactionFactory) {
+ sTransactionFactory = transactionFactory;
+ }
+
+ /**
+ * Starts a sync and will automatically apply the final, merged transaction.
+ */
+ public SurfaceSyncGroup() {
+ this(transaction -> {
+ if (transaction != null) {
+ transaction.apply();
+ }
+ });
+
+ }
+
+ /**
+ * Creates a sync.
+ *
+ * @param syncRequestComplete The complete callback that contains the syncId and transaction
+ * with all the sync data merged. The Transaction passed back can be
+ * null.
+ *
+ * NOTE: Only should be used by ViewRootImpl
+ * @hide
+ */
+ public SurfaceSyncGroup(Consumer<Transaction> syncRequestComplete) {
+ mSyncRequestCompleteCallback = transaction -> {
+ syncRequestComplete.accept(transaction);
+ synchronized (mLock) {
+ for (Pair<Executor, Runnable> callback : mSyncCompleteCallbacks) {
+ callback.first.execute(callback.second);
+ }
+ }
+ };
+
+ if (DEBUG) {
+ Log.d(TAG, "setupSync");
+ }
+ }
+
+ /**
+ * Add a {@link Runnable} to be executed when the sync completes.
+ *
+ * @param executor The Executor to invoke the Runnable on
+ * @param runnable The Runnable to get called
+ */
+ public void addSyncCompleteCallback(Executor executor, Runnable runnable) {
+ synchronized (mLock) {
+ mSyncCompleteCallbacks.add(new Pair<>(executor, runnable));
+ }
+ }
+
+ /**
+ * Add a SurfaceView to a sync set. This is different than
+ * {@link #addToSync(AttachedSurfaceControl)} because it requires the caller to notify the start
+ * and finish drawing in order to sync.
+ *
+ * @param surfaceView The SurfaceView to add to the sync.
+ * @param frameCallbackConsumer The callback that's invoked to allow the caller to notify
+ * the
+ * Syncer when the SurfaceView has started drawing and
+ * finished.
+ * @return true if the SurfaceView was successfully added to the SyncGroup, false otherwise.
+ */
+ @UiThread
+ public boolean addToSync(SurfaceView surfaceView,
+ Consumer<SurfaceViewFrameCallback> frameCallbackConsumer) {
+ return addToSync(new SurfaceViewSyncTarget(surfaceView, frameCallbackConsumer));
+ }
+
+ /**
+ * Add a View's rootView to a sync set.
+ *
+ * @param viewRoot The viewRoot that will be add to the sync set
+ * @return true if the View was successfully added to the SyncGroup, false otherwise.
+ */
+ @UiThread
+ public boolean addToSync(@Nullable AttachedSurfaceControl viewRoot) {
+ if (viewRoot == null) {
+ return false;
+ }
+ return addToSync(viewRoot.getSyncTarget());
+ }
+
+ /**
+ * Add a {@link SyncTarget} to a sync set. The sync set will wait for all
+ * SyncableSurfaces to complete before notifying.
+ *
+ * @param syncTarget A SyncableSurface that implements how to handle syncing
+ * buffers.
+ * @return true if the SyncTarget was successfully added to the SyncGroup, false otherwise.
+ */
+ public boolean addToSync(SyncTarget syncTarget) {
+ SyncBufferCallback syncBufferCallback = new SyncBufferCallback() {
+ @Override
+ public void onBufferReady(Transaction t) {
+ synchronized (mLock) {
+ if (t != null) {
+ mTransaction.merge(t);
+ }
+ mPendingSyncs.remove(hashCode());
+ checkIfSyncIsComplete();
+ }
+ }
+ };
+
+ synchronized (mLock) {
+ if (mSyncReady) {
+ Log.e(TAG, "Sync " + this + " was already marked as ready. No more "
+ + "SyncTargets can be added.");
+ return false;
+ }
+ mPendingSyncs.add(syncBufferCallback.hashCode());
+ mSyncTargets.add(syncTarget);
+ }
+ syncTarget.onReadyToSync(syncBufferCallback);
+ return true;
+ }
+
+ /**
+ * Mark the sync set as ready to complete. No more data can be added to the specified
+ * syncId.
+ * Once the sync set is marked as ready, it will be able to complete once all Syncables in the
+ * set have completed their sync
+ */
+ public void markSyncReady() {
+ synchronized (mLock) {
+ mSyncReady = true;
+ checkIfSyncIsComplete();
+ }
+ }
+
+ @GuardedBy("mLock")
+ private void checkIfSyncIsComplete() {
+ if (!mSyncReady || !mPendingSyncs.isEmpty() || !mMergedSyncGroups.isEmpty()) {
+ if (DEBUG) {
+ Log.d(TAG, "Syncable is not complete. mSyncReady=" + mSyncReady
+ + " mPendingSyncs=" + mPendingSyncs.size() + " mergedSyncs="
+ + mMergedSyncGroups.size());
+ }
+ return;
+ }
+
+ if (DEBUG) {
+ Log.d(TAG, "Successfully finished sync id=" + this);
+ }
+
+ for (SyncTarget syncTarget : mSyncTargets) {
+ syncTarget.onSyncComplete();
+ }
+ mSyncTargets.clear();
+ mSyncRequestCompleteCallback.accept(mTransaction);
+ mFinished = true;
+ }
+
+ /**
+ * Add a Transaction to this sync set. This allows the caller to provide other info that
+ * should be synced with the buffers.
+ */
+ public void addTransactionToSync(Transaction t) {
+ synchronized (mLock) {
+ mTransaction.merge(t);
+ }
+ }
+
+ private void updateCallback(Consumer<Transaction> transactionConsumer) {
+ synchronized (mLock) {
+ if (mFinished) {
+ Log.e(TAG, "Attempting to merge SyncGroup " + this + " when sync is"
+ + " already complete");
+ transactionConsumer.accept(null);
+ }
+
+ final Consumer<Transaction> oldCallback = mSyncRequestCompleteCallback;
+ mSyncRequestCompleteCallback = transaction -> {
+ oldCallback.accept(null);
+ transactionConsumer.accept(transaction);
+ };
+ }
+ }
+
+ /**
+ * Merge a SyncGroup into this SyncGroup. Since SyncGroups could still have pending SyncTargets,
+ * we need to make sure those can still complete before the mergeTo SyncGroup is considered
+ * complete.
+ *
+ * We keep track of all the merged SyncGroups until they are marked as done, and then they
+ * are removed from the set. This SyncGroup is not considered done until all the merged
+ * SyncGroups are done.
+ *
+ * When the merged SyncGroup is complete, it will invoke the original syncRequestComplete
+ * callback but send an empty transaction to ensure the changes are applied early. This
+ * is needed in case the original sync is relying on the callback to continue processing.
+ *
+ * @param otherSyncGroup The other SyncGroup to merge into this one.
+ */
+ public void merge(SurfaceSyncGroup otherSyncGroup) {
+ synchronized (mLock) {
+ mMergedSyncGroups.add(otherSyncGroup);
+ }
+ otherSyncGroup.updateCallback(transaction -> {
+ synchronized (mLock) {
+ mMergedSyncGroups.remove(otherSyncGroup);
+ if (transaction != null) {
+ mTransaction.merge(transaction);
+ }
+ checkIfSyncIsComplete();
+ }
+ });
+ }
+
+ /**
+ * Wrapper class to help synchronize SurfaceViews
+ */
+ private static class SurfaceViewSyncTarget implements SyncTarget {
+ private final SurfaceView mSurfaceView;
+ private final Consumer<SurfaceViewFrameCallback> mFrameCallbackConsumer;
+
+ SurfaceViewSyncTarget(SurfaceView surfaceView,
+ Consumer<SurfaceViewFrameCallback> frameCallbackConsumer) {
+ mSurfaceView = surfaceView;
+ mFrameCallbackConsumer = frameCallbackConsumer;
+ }
+
+ @Override
+ public void onReadyToSync(SyncBufferCallback syncBufferCallback) {
+ mFrameCallbackConsumer.accept(
+ () -> mSurfaceView.syncNextFrame(syncBufferCallback::onBufferReady));
+ }
+ }
+
+ /**
+ * A SyncTarget that can be added to a sync set.
+ */
+ public interface SyncTarget {
+ /**
+ * Called when the Syncable is ready to begin handing a sync request. When invoked, the
+ * implementor is required to call {@link SyncBufferCallback#onBufferReady(Transaction)}
+ * and {@link SyncBufferCallback#onBufferReady(Transaction)} in order for this Syncable
+ * to be marked as complete.
+ *
+ * Always invoked on the thread that initiated the call to {@link #addToSync(SyncTarget)}
+ *
+ * @param syncBufferCallback A SyncBufferCallback that the caller must invoke onBufferReady
+ */
+ void onReadyToSync(SyncBufferCallback syncBufferCallback);
+
+ /**
+ * There's no guarantee about the thread this callback is invoked on.
+ */
+ default void onSyncComplete() {
+ }
+ }
+
+ /**
+ * Interface so the SurfaceSyncer can know when it's safe to start and when everything has been
+ * completed. The caller should invoke the calls when the rendering has started and finished a
+ * frame.
+ */
+ public interface SyncBufferCallback {
+ /**
+ * Invoked when the transaction contains the buffer and is ready to sync.
+ *
+ * @param t The transaction that contains the buffer to be synced. This can be null if
+ * there's nothing to sync
+ */
+ void onBufferReady(@Nullable Transaction t);
+ }
+
+ /**
+ * A frame callback that is used to synchronize SurfaceViews. The owner of the SurfaceView must
+ * implement onFrameStarted when trying to sync the SurfaceView. This is to ensure the sync
+ * knows when the frame is ready to add to the sync.
+ */
+ public interface SurfaceViewFrameCallback {
+ /**
+ * Called when the SurfaceView is going to render a frame
+ */
+ void onFrameStarted();
+ }
+}
diff --git a/core/java/android/window/SurfaceSyncGroup.md b/core/java/android/window/SurfaceSyncGroup.md
new file mode 100644
index 0000000..659aade
--- /dev/null
+++ b/core/java/android/window/SurfaceSyncGroup.md
@@ -0,0 +1,86 @@
+## SurfaceSyncGroup
+
+### Overview
+
+A generic way for data to be gathered so multiple surfaces can be synced. This is intended to be used with Views, SurfaceView, and any other surface that wants to be involved in a sync. This allows different parts of the Android system to synchronize different windows and layers themselves without having to go through WindowManagerService
+
+### Code
+
+SurfaceSyncGroup is a class that manages sync requests and reports back when all participants in the sync are ready.
+
+##### Constructor
+The first step is to create a sync request. This is done by creating a new `SurfaceSyncGroup`.
+There are two constructors: one that accepts a `Consumer<Transaction>` and one that's empty. The empty constructor will automatically apply the final transaction. The second constructor should only be used by ViewRootImpl. The purpose of this one is to allow the caller to get back the merged transaction without it being applied. ViewRootImpl uses it to send the transaction to WindowManagerService to be synced there. Using this one for other cases is unsafe because the caller may hold the transaction longer than expected and prevent buffers from being latched and released.
+
+##### markSyncReady
+
+When the caller has added all the `SyncTarget` to the sync, they should call `markSyncReady()` If the caller doesn't call this, the sync will never complete since the SurfaceSyncGroup wants to give the caller a chance to add all the SyncTargets before considering the sync ready. Before `markSyncReady` is called, the `SyncTargets` can actually produce a frame, which will just be held in a transaction until all other `SyncTargets` are ready AND `markSyncReady` has been called. Once markSyncReady has been called, you cannot add any more `SyncTargets` to that particular SurfaceSyncGroup.
+
+##### addToSync
+
+The caller will invoke `addToSync` for every `SyncTarget` that it wants included. There are a few helper methods since the most common cases are Views and SurfaceView
+* `addToSync(AttachedSurfaceControl)` - This is used for syncing the root of the View, specificially the ViewRootImpl
+* `addToSync(SurfaceView, Consumer<SurfaceViewFrameCallback>)` - This is to sync a SurfaceView. Since SurfaceViews are rendered by the app, the caller will be expected to provide a way to get back the buffer to sync. More details about that [below](#surfaceviewframecallback)
+* `addToSync(SyncTarget)` - This is the generic method. It can be used to sync arbitrary info. The SyncTarget interface has required methods that need to be implemented to properly get the transaction to sync.
+
+When calling addToSync with either AttachedSurfaceControl or SurfaceView, it must be called on the UI Thread. This is to ensure consistent behavior, where any UI changes done while still on the UI thread are included in this frame. The next vsync will pick up those changes and request to draw.
+
+##### addTransactionToSync
+
+This is a simple method that allows callers to add generic Transactions to the sync. The caller invokes `addTransactionToSync(Transaction)`. This can be used for any additional things that need to be included in the same SyncGroup.
+
+##### merge
+
+To add more flexibility to Syncs, an API is provided to merge SurfaceSyncGroups. The caller provides the SurfaceSyncGroup it wants merged. The current SurfaceSyncGroup will now wait for the passed in SurfaceSyncGroup to complete, as well as its own SyncTargets to complete before invoking the callback. The passed in SurfaceSyncGroup will also get a complete callback but only when its SurfaceSyncGroup completes, not the one it merged into. If a `Consumer<Transaction>` was passed in to the SurfaceSyncGroup, it will get back an emtpy Transaction so it can't accidentally apply things that were meant to be merged.
+
+##### addSyncCompleteCallback
+
+This allows callers to receive a callback when the sync is complete. The method takes in an Executor and a Runnable that will be invoked when the SurfaceSyncGroup has completed. The Executor is used to invoke the callback on the desired thread. You can add more than one callback.
+
+##### SyncTarget
+
+This interface is used to handle syncs. The interface has two methods
+* `onReadyToSync(SyncBufferCallback)` - This one must be implemented. The sync will notify the `SyncTarget` so it can invoke the `SyncBufferCallback`, letting the sync know when it's ready.
+* `onSyncComplete()` - This method is optional. It's used to notify the `SyncTarget` that the entire sync is complete. This is similar to the callback sent in `setupSync`, but it's invoked to the `SyncTargets` rather than the caller who started the sync. This is used by ViewRootImpl to restore the state when the entire sync is done
+
+When syncing ViewRootImpl, these methods are implemented already since ViewRootImpl handles the rendering requests and timing.
+
+##### SyncBufferCallback
+
+This interface is used to tell the sync that this SyncTarget is ready. There's only method here, `onBufferReady(Transaction)`, that sends back the transaction that contains the data to be synced, normally with a buffer.
+
+##### SurfaceViewFrameCallback
+
+As mentioned above, SurfaceViews are a special case because the buffers produced are handled by the app, and not the framework. Because of this, the SurfaceSyncGroup doesn't know which frame to sync. Therefore, to sync SurfaceViews, the caller must provide a way to notify the SurfaceSyncGroup that it's going to render a buffer and that this next buffer is the one to sync. The `SurfaceViewFrameCallback` has one method `onFrameStarted()`. When this is invoked, the SurfaceSyncGroup sets up a request to sync the next buffer for the SurfaceView.
+
+
+### Example
+
+A simple example where you want to sync two windows and also include a transaction in the sync
+
+```java
+SurfaceSyncGroup syncGroup = new SurfaceSyncGroup();
+SyncGroup.addSyncCompleteCallback(mMainThreadExecutor, () -> {
+ Log.d(TAG, "syncComplete");
+};
+syncGroup.addToSync(view1.getRootSurfaceControl());
+syncGroup.addToSync(view2.getRootSurfaceControl());
+syncGroup.addTransactionToSync(transaction);
+syncGroup.markSyncReady();
+```
+
+A SurfaceView example:
+See `frameworks/base/tests/SurfaceViewSyncTest` for a working example
+
+```java
+SurfaceSyncGroup syncGroup = new SurfaceSyncGroup();
+syncGroup.addSyncCompleteCallback(mMainThreadExecutor, () -> {
+ Log.d(TAG, "syncComplete");
+};
+syncGroup.addToSync(container.getRootSurfaceControl());
+syncGroup.addToSync(surfaceView, frameCallback -> {
+ // Call this when the SurfaceView is ready to render a new frame with the changes.
+ frameCallback.onFrameStarted()
+}
+syncGroup.markSyncReady();
+```
\ No newline at end of file
diff --git a/core/java/android/window/SurfaceSyncer.java b/core/java/android/window/SurfaceSyncer.java
deleted file mode 100644
index e6eb071..0000000
--- a/core/java/android/window/SurfaceSyncer.java
+++ /dev/null
@@ -1,472 +0,0 @@
-/*
- * Copyright (C) 2022 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package android.window;
-
-import android.annotation.NonNull;
-import android.annotation.Nullable;
-import android.annotation.UiThread;
-import android.os.Handler;
-import android.os.Looper;
-import android.util.ArraySet;
-import android.util.Log;
-import android.util.SparseArray;
-import android.view.SurfaceControl.Transaction;
-import android.view.SurfaceView;
-import android.view.View;
-import android.view.ViewRootImpl;
-
-import com.android.internal.annotations.GuardedBy;
-
-import java.util.Set;
-import java.util.function.Consumer;
-import java.util.function.Supplier;
-
-/**
- * Used to organize syncs for surfaces.
- *
- * The SurfaceSyncer allows callers to add desired syncs into a set and wait for them to all
- * complete before getting a callback. The purpose of the Syncer is to be an accounting mechanism
- * so each sync implementation doesn't need to handle it themselves. The Syncer class is used the
- * following way.
- *
- * 1. {@link #setupSync(Runnable)} is called
- * 2. {@link #addToSync(int, SyncTarget)} is called for every SyncTarget object that wants to be
- * included in the sync. If the addSync is called for a View or SurfaceView it needs to be called
- * on the UI thread. When addToSync is called, it's guaranteed that any UI updates that were
- * requested before addToSync but after the last frame drew, will be included in the sync.
- * 3. {@link #markSyncReady(int)} should be called when all the {@link SyncTarget}s have been added
- * to the SyncSet. Now the SyncSet is closed and no more SyncTargets can be added to it.
- * 4. The SyncSet will gather the data for each SyncTarget using the steps described below. When
- * all the SyncTargets have finished, the syncRequestComplete will be invoked and the transaction
- * will either be applied or sent to the caller. In most cases, only the SurfaceSyncer should be
- * handling the Transaction object directly. However, there are some cases where the framework
- * needs to send the Transaction elsewhere, like in ViewRootImpl, so that option is provided.
- *
- * The following is what happens within the {@link SyncSet}
- * 1. Each SyncableTarget will get a {@link SyncTarget#onReadyToSync} callback that contains
- * a {@link SyncBufferCallback}.
- * 2. Each {@link SyncTarget} needs to invoke {@link SyncBufferCallback#onBufferReady(Transaction)}.
- * This makes sure the SyncSet knows when the SyncTarget is complete, allowing the SyncSet to get
- * the Transaction that contains the buffer.
- * 3. When the final SyncBufferCallback finishes for the SyncSet, the syncRequestComplete Consumer
- * will be invoked with the transaction that contains all information requested in the sync. This
- * could include buffers and geometry changes. The buffer update will include the UI changes that
- * were requested for the View.
- *
- * @hide
- */
-public class SurfaceSyncer {
- private static final String TAG = "SurfaceSyncer";
- private static final boolean DEBUG = false;
-
- private static Supplier<Transaction> sTransactionFactory = Transaction::new;
-
- private final Object mSyncSetLock = new Object();
- @GuardedBy("mSyncSetLock")
- private final SparseArray<SyncSet> mSyncSets = new SparseArray<>();
- @GuardedBy("mSyncSetLock")
- private int mIdCounter = 0;
-
- /**
- * @hide
- */
- public static void setTransactionFactory(Supplier<Transaction> transactionFactory) {
- sTransactionFactory = transactionFactory;
- }
-
- /**
- * Starts a sync and will automatically apply the final, merged transaction.
- *
- * @param onComplete The runnable that is invoked when the sync has completed. This will run on
- * the same thread that the sync was started on.
- * @return The syncId for the newly created sync.
- * @see #setupSync(Consumer)
- */
- public int setupSync(@Nullable Runnable onComplete) {
- Handler handler = new Handler(Looper.myLooper());
- return setupSync(transaction -> {
- transaction.apply();
- if (onComplete != null) {
- handler.post(onComplete);
- }
- });
- }
-
- /**
- * Starts a sync.
- *
- * @param syncRequestComplete The complete callback that contains the syncId and transaction
- * with all the sync data merged.
- * @return The syncId for the newly created sync.
- * @hide
- * @see #setupSync(Runnable)
- */
- public int setupSync(@NonNull Consumer<Transaction> syncRequestComplete) {
- synchronized (mSyncSetLock) {
- final int syncId = mIdCounter++;
- if (DEBUG) {
- Log.d(TAG, "setupSync " + syncId);
- }
- SyncSet syncSet = new SyncSet(syncId, transaction -> {
- synchronized (mSyncSetLock) {
- mSyncSets.remove(syncId);
- }
- syncRequestComplete.accept(transaction);
- });
- mSyncSets.put(syncId, syncSet);
- return syncId;
- }
- }
-
- /**
- * Mark the sync set as ready to complete. No more data can be added to the specified syncId.
- * Once the sync set is marked as ready, it will be able to complete once all Syncables in the
- * set have completed their sync
- *
- * @param syncId The syncId to mark as ready.
- */
- public void markSyncReady(int syncId) {
- SyncSet syncSet;
- synchronized (mSyncSetLock) {
- syncSet = mSyncSets.get(syncId);
- }
- if (syncSet == null) {
- Log.e(TAG, "Failed to find syncSet for syncId=" + syncId);
- return;
- }
- syncSet.markSyncReady();
- }
-
- /**
- * Merge another SyncSet into the specified syncId.
- * @param syncId The current syncId to merge into
- * @param otherSyncId The other syncId to be merged
- * @param otherSurfaceSyncer The other SurfaceSyncer where the otherSyncId is from
- */
- public void merge(int syncId, int otherSyncId, SurfaceSyncer otherSurfaceSyncer) {
- SyncSet syncSet;
- synchronized (mSyncSetLock) {
- syncSet = mSyncSets.get(syncId);
- }
-
- SyncSet otherSyncSet = otherSurfaceSyncer.getAndValidateSyncSet(otherSyncId);
- if (otherSyncSet == null) {
- return;
- }
-
- if (DEBUG) {
- Log.d(TAG,
- "merge id=" + otherSyncId + " from=" + otherSurfaceSyncer + " into id=" + syncId
- + " from" + this);
- }
- syncSet.merge(otherSyncSet);
- }
-
- /**
- * Add a SurfaceView to a sync set. This is different than {@link #addToSync(int, View)} because
- * it requires the caller to notify the start and finish drawing in order to sync.
- *
- * @param syncId The syncId to add an entry to.
- * @param surfaceView The SurfaceView to add to the sync.
- * @param frameCallbackConsumer The callback that's invoked to allow the caller to notify the
- * Syncer when the SurfaceView has started drawing and finished.
- *
- * @return true if the SurfaceView was successfully added to the SyncSet, false otherwise.
- */
- @UiThread
- public boolean addToSync(int syncId, SurfaceView surfaceView,
- Consumer<SurfaceViewFrameCallback> frameCallbackConsumer) {
- return addToSync(syncId, new SurfaceViewSyncTarget(surfaceView, frameCallbackConsumer));
- }
-
- /**
- * Add a View's rootView to a sync set.
- *
- * @param syncId The syncId to add an entry to.
- * @param view The view where the root will be add to the sync set
- *
- * @return true if the View was successfully added to the SyncSet, false otherwise.
- */
- @UiThread
- public boolean addToSync(int syncId, @NonNull View view) {
- ViewRootImpl viewRoot = view.getViewRootImpl();
- if (viewRoot == null) {
- return false;
- }
- return addToSync(syncId, viewRoot.mSyncTarget);
- }
-
- /**
- * Add a {@link SyncTarget} to a sync set. The sync set will wait for all
- * SyncableSurfaces to complete before notifying.
- *
- * @param syncId The syncId to add an entry to.
- * @param syncTarget A SyncableSurface that implements how to handle syncing
- * buffers.
- *
- * @return true if the SyncTarget was successfully added to the SyncSet, false otherwise.
- */
- public boolean addToSync(int syncId, @NonNull SyncTarget syncTarget) {
- SyncSet syncSet = getAndValidateSyncSet(syncId);
- if (syncSet == null) {
- return false;
- }
- if (DEBUG) {
- Log.d(TAG, "addToSync id=" + syncId);
- }
- return syncSet.addSyncableSurface(syncTarget);
- }
-
- /**
- * Add a transaction to a specific sync so it can be merged along with the frames from the
- * Syncables in the set. This is so the caller can add arbitrary transaction info that will be
- * applied at the same time as the buffers
- * @param syncId The syncId where the transaction will be merged to.
- * @param t The transaction to merge in the sync set.
- */
- public void addTransactionToSync(int syncId, Transaction t) {
- SyncSet syncSet = getAndValidateSyncSet(syncId);
- if (syncSet != null) {
- syncSet.addTransactionToSync(t);
- }
- }
-
- private SyncSet getAndValidateSyncSet(int syncId) {
- SyncSet syncSet;
- synchronized (mSyncSetLock) {
- syncSet = mSyncSets.get(syncId);
- }
- if (syncSet == null) {
- Log.e(TAG, "Failed to find sync for id=" + syncId);
- return null;
- }
- return syncSet;
- }
-
- /**
- * A SyncTarget that can be added to a sync set.
- */
- public interface SyncTarget {
- /**
- * Called when the Syncable is ready to begin handing a sync request. When invoked, the
- * implementor is required to call {@link SyncBufferCallback#onBufferReady(Transaction)}
- * and {@link SyncBufferCallback#onBufferReady(Transaction)} in order for this Syncable
- * to be marked as complete.
- *
- * Always invoked on the thread that initiated the call to
- * {@link #addToSync(int, SyncTarget)}
- *
- * @param syncBufferCallback A SyncBufferCallback that the caller must invoke onBufferReady
- */
- void onReadyToSync(SyncBufferCallback syncBufferCallback);
-
- /**
- * There's no guarantee about the thread this callback is invoked on.
- */
- default void onSyncComplete() {}
- }
-
- /**
- * Interface so the SurfaceSyncer can know when it's safe to start and when everything has been
- * completed. The caller should invoke the calls when the rendering has started and finished a
- * frame.
- */
- public interface SyncBufferCallback {
- /**
- * Invoked when the transaction contains the buffer and is ready to sync.
- *
- * @param t The transaction that contains the buffer to be synced. This can be null if
- * there's nothing to sync
- */
- void onBufferReady(@Nullable Transaction t);
- }
-
- /**
- * Class that collects the {@link SyncTarget}s and notifies when all the surfaces have
- * a frame ready.
- */
- private static class SyncSet {
- private final Object mLock = new Object();
-
- @GuardedBy("mLock")
- private final Set<Integer> mPendingSyncs = new ArraySet<>();
- @GuardedBy("mLock")
- private final Transaction mTransaction = sTransactionFactory.get();
- @GuardedBy("mLock")
- private boolean mSyncReady;
- @GuardedBy("mLock")
- private final Set<SyncTarget> mSyncTargets = new ArraySet<>();
-
- private final int mSyncId;
- @GuardedBy("mLock")
- private Consumer<Transaction> mSyncRequestCompleteCallback;
-
- @GuardedBy("mLock")
- private final Set<SyncSet> mMergedSyncSets = new ArraySet<>();
-
- @GuardedBy("mLock")
- private boolean mFinished;
-
- private SyncSet(int syncId, Consumer<Transaction> syncRequestComplete) {
- mSyncId = syncId;
- mSyncRequestCompleteCallback = syncRequestComplete;
- }
-
- boolean addSyncableSurface(SyncTarget syncTarget) {
- SyncBufferCallback syncBufferCallback = new SyncBufferCallback() {
- @Override
- public void onBufferReady(Transaction t) {
- synchronized (mLock) {
- if (t != null) {
- mTransaction.merge(t);
- }
- mPendingSyncs.remove(hashCode());
- checkIfSyncIsComplete();
- }
- }
- };
-
- synchronized (mLock) {
- if (mSyncReady) {
- Log.e(TAG, "Sync " + mSyncId + " was already marked as ready. No more "
- + "SyncTargets can be added.");
- return false;
- }
- mPendingSyncs.add(syncBufferCallback.hashCode());
- mSyncTargets.add(syncTarget);
- }
- syncTarget.onReadyToSync(syncBufferCallback);
- return true;
- }
-
- void markSyncReady() {
- synchronized (mLock) {
- mSyncReady = true;
- checkIfSyncIsComplete();
- }
- }
-
- @GuardedBy("mLock")
- private void checkIfSyncIsComplete() {
- if (!mSyncReady || !mPendingSyncs.isEmpty() || !mMergedSyncSets.isEmpty()) {
- if (DEBUG) {
- Log.d(TAG, "Syncable is not complete. mSyncReady=" + mSyncReady
- + " mPendingSyncs=" + mPendingSyncs.size() + " mergedSyncs="
- + mMergedSyncSets.size());
- }
- return;
- }
-
- if (DEBUG) {
- Log.d(TAG, "Successfully finished sync id=" + mSyncId);
- }
-
- for (SyncTarget syncTarget : mSyncTargets) {
- syncTarget.onSyncComplete();
- }
- mSyncTargets.clear();
- mSyncRequestCompleteCallback.accept(mTransaction);
- mFinished = true;
- }
-
- /**
- * Add a Transaction to this sync set. This allows the caller to provide other info that
- * should be synced with the buffers.
- */
- void addTransactionToSync(Transaction t) {
- synchronized (mLock) {
- mTransaction.merge(t);
- }
- }
-
- public void updateCallback(Consumer<Transaction> transactionConsumer) {
- synchronized (mLock) {
- if (mFinished) {
- Log.e(TAG, "Attempting to merge SyncSet " + mSyncId + " when sync is"
- + " already complete");
- transactionConsumer.accept(new Transaction());
- }
-
- final Consumer<Transaction> oldCallback = mSyncRequestCompleteCallback;
- mSyncRequestCompleteCallback = transaction -> {
- oldCallback.accept(new Transaction());
- transactionConsumer.accept(transaction);
- };
- }
- }
-
- /**
- * Merge a SyncSet into this SyncSet. Since SyncSets could still have pending SyncTargets,
- * we need to make sure those can still complete before the mergeTo syncSet is considered
- * complete.
- *
- * We keep track of all the merged SyncSets until they are marked as done, and then they
- * are removed from the set. This SyncSet is not considered done until all the merged
- * SyncSets are done.
- *
- * When the merged SyncSet is complete, it will invoke the original syncRequestComplete
- * callback but send an empty transaction to ensure the changes are applied early. This
- * is needed in case the original sync is relying on the callback to continue processing.
- *
- * @param otherSyncSet The other SyncSet to merge into this one.
- */
- public void merge(SyncSet otherSyncSet) {
- synchronized (mLock) {
- mMergedSyncSets.add(otherSyncSet);
- }
- otherSyncSet.updateCallback(transaction -> {
- synchronized (mLock) {
- mMergedSyncSets.remove(otherSyncSet);
- mTransaction.merge(transaction);
- checkIfSyncIsComplete();
- }
- });
- }
- }
-
- /**
- * Wrapper class to help synchronize SurfaceViews
- */
- private static class SurfaceViewSyncTarget implements SyncTarget {
- private final SurfaceView mSurfaceView;
- private final Consumer<SurfaceViewFrameCallback> mFrameCallbackConsumer;
-
- SurfaceViewSyncTarget(SurfaceView surfaceView,
- Consumer<SurfaceViewFrameCallback> frameCallbackConsumer) {
- mSurfaceView = surfaceView;
- mFrameCallbackConsumer = frameCallbackConsumer;
- }
-
- @Override
- public void onReadyToSync(SyncBufferCallback syncBufferCallback) {
- mFrameCallbackConsumer.accept(
- () -> mSurfaceView.syncNextFrame(syncBufferCallback::onBufferReady));
- }
- }
-
- /**
- * A frame callback that is used to synchronize SurfaceViews. The owner of the SurfaceView must
- * implement onFrameStarted when trying to sync the SurfaceView. This is to ensure the sync
- * knows when the frame is ready to add to the sync.
- */
- public interface SurfaceViewFrameCallback {
- /**
- * Called when the SurfaceView is going to render a frame
- */
- void onFrameStarted();
- }
-}
diff --git a/core/java/android/window/SurfaceSyncer.md b/core/java/android/window/SurfaceSyncer.md
deleted file mode 100644
index 1394bd1..0000000
--- a/core/java/android/window/SurfaceSyncer.md
+++ /dev/null
@@ -1,80 +0,0 @@
-## SurfaceSyncer
-
-### Overview
-
-A generic way for data to be gathered so multiple surfaces can be synced. This is intended to be used with Views, SurfaceView, and any other surface that wants to be involved in a sync. This allows different parts of the Android system to synchronize different windows and layers themselves without having to go through WindowManagerService
-
-### Code
-
-SurfaceSyncer is a class that manages sync requests and reports back when all participants in the sync are ready.
-
-##### setupSync
-The first step is to create a sync request. This is done by calling `setupSync`.
-There are two setupSync methods: one that accepts a `Runnable` and one that accepts a `Consumer<Transaction>`. The `setupSync(Runnable)` will automatically apply the final transaction and invokes the Runnable to notify the caller that the sync is complete. The `Runnable` can be null since not all cases care about the sync being complete. The second `setupSync(Consumer<Transaction>)` should only be used by ViewRootImpl. The purpose of this one is to allow the caller to get back the merged transaction without it being applied. ViewRootImpl uses it to send the transaction to WindowManagerService to be synced there. Using this one for other cases is unsafe because the caller may hold the transaction longer than expected and prevent buffers from being latched and released.
-
-The setupSync returns a syncId which is used for the other APIs
-
-##### markSyncReady
-
-When the caller has added all the `SyncTarget` to the sync, they should call `markSyncReady(syncId)` If the caller doesn't call this, the sync will never complete since the SurfaceSyncer wants to give the caller a chance to add all the SyncTargets before considering the sync ready. Before `markSyncReady` is called, the `SyncTargets` can actually produce a frame, which will just be held in a transaction until all other `SyncTargets` are ready AND `markSyncReady` has been called. Once markSyncReady has been called, you cannot add any more `SyncTargets` to that particular syncId.
-
-##### addToSync
-
-The caller will invoke `addToSync` for every `SyncTarget` that it wants included. The caller must specify which syncId they want to add the `SyncTarget` too since there can be multiple syncs open at the same time. There are a few helper methods since the most common cases are Views and SurfaceView
-* `addToSync(int syncId, View)` - This is used for syncing the root of the View, specificially the ViewRootImpl
-* `addToSync(int syncId, SurfaceView, Consumer<SurfaceViewFrameCallback>)` - This is to sync a SurfaceView. Since SurfaceViews are rendered by the app, the caller will be expected to provide a way to get back the buffer to sync. More details about that [below](#surfaceviewframecallback)
-* `addToSync(int syncId, SyncTarget)` - This is the generic method. It can be used to sync arbitrary info. The SyncTarget interface has required methods that need to be implemented to properly get the transaction to sync.
-
-When calling addToSync with either View or SurfaceView, it must occur on the UI Thread. This is to ensure consistent behavior, where any UI changes done while still on the UI thread are included in this frame. The next vsync will pick up those changes and request to draw.
-
-##### addTransactionToSync
-
-This is a simple method that allows callers to add generic Transactions to the sync. The caller invokes `addTransactionToSync(int syncId, Transaction)`. This can be used for things that need to be synced with content, but aren't updating content themselves.
-
-##### SyncTarget
-
-This interface is used to handle syncs. The interface has two methods
-* `onReadyToSync(SyncBufferCallback)` - This one must be implemented. The sync will notify the `SyncTarget` so it can invoke the `SyncBufferCallback`, letting the sync know when it's ready.
-* `onSyncComplete()` - This method is optional. It's used to notify the `SyncTarget` that the entire sync is complete. This is similar to the callback sent in `setupSync`, but it's invoked to the `SyncTargets` rather than the caller who started the sync. This is used by ViewRootImpl to restore the state when the entire sync is done
-
-When syncing ViewRootImpl, these methods are implemented already since ViewRootImpl handles the rendering requests and timing.
-
-##### SyncBufferCallback
-
-This interface is used to tell the sync that this SyncTarget is ready. There's only method here, `onBufferReady(Transaction)`, that sends back the transaction that contains the data to be synced, normally with a buffer.
-
-##### SurfaceViewFrameCallback
-
-As mentioned above, SurfaceViews are a special case because the buffers produced are handled by the app, and not the framework. Because of this, the syncer doesn't know which frame to sync. Therefore, to sync SurfaceViews, the caller must provide a way to notify the syncer that it's going to render a buffer and that this next buffer is the one to sync. The `SurfaceViewFrameCallback` has one method `onFrameStarted()`. When this is invoked, the Syncer sets up a request to sync the next buffer for the SurfaceView.
-
-
-### Example
-
-A simple example where you want to sync two windows and also include a transaction in the sync
-
-```java
-SurfaceSyncer syncer = new SurfaceSyncer();
-int syncId = syncer.setupSync(() -> {
- Log.d(TAG, "syncComplete");
-};
-syncer.addToSync(syncId, view1);
-syncer.addToSync(syncId, view2);
-syncer.addTransactionToSync(syncId, transaction);
-syncer.markSyncReady(syncId);
-```
-
-A SurfaceView example:
-See `frameworks/base/tests/SurfaceViewSyncTest` for a working example
-
-```java
-SurfaceSyncer syncer = new SurfaceSyncer();
-int syncId = syncer.setupSync(() -> {
- Log.d(TAG, "syncComplete");
-};
-syncer.addToSync(syncId, container);
-syncer.addToSync(syncId, surfaceView, frameCallback -> {
- // Call this when the SurfaceView is ready to render a new frame with the changes.
- frameCallback.onFrameStarted()
-}
-syncer.markSyncReady(syncId);
-```
\ No newline at end of file
diff --git a/packages/SystemUI/animation/src/com/android/systemui/animation/ViewRootSync.kt b/packages/SystemUI/animation/src/com/android/systemui/animation/ViewRootSync.kt
index 77640f1..163cab4 100644
--- a/packages/SystemUI/animation/src/com/android/systemui/animation/ViewRootSync.kt
+++ b/packages/SystemUI/animation/src/com/android/systemui/animation/ViewRootSync.kt
@@ -1,12 +1,11 @@
package com.android.systemui.animation
import android.view.View
-import android.window.SurfaceSyncer
+import android.window.SurfaceSyncGroup
/** A util class to synchronize 2 view roots. */
// TODO(b/200284684): Remove this class.
object ViewRootSync {
- private var surfaceSyncer: SurfaceSyncer? = null
/**
* Synchronize the next draw between the view roots of [view] and [otherView], then run [then].
@@ -29,13 +28,11 @@
return
}
- surfaceSyncer =
- SurfaceSyncer().apply {
- val syncId = setupSync(Runnable { then() })
- addToSync(syncId, view)
- addToSync(syncId, otherView)
- markSyncReady(syncId)
- }
+ val syncGroup = SurfaceSyncGroup()
+ syncGroup.addSyncCompleteCallback(view.context.mainExecutor) { then() }
+ syncGroup.addToSync(view.rootSurfaceControl)
+ syncGroup.addToSync(otherView.rootSurfaceControl)
+ syncGroup.markSyncReady()
}
/** A Java-friendly API for [synchronizeNextDraw]. */
diff --git a/services/tests/wmtests/src/com/android/server/wm/SurfaceSyncerContinuousTest.java b/services/tests/wmtests/src/com/android/server/wm/SurfaceSyncGroupContinuousTest.java
similarity index 88%
rename from services/tests/wmtests/src/com/android/server/wm/SurfaceSyncerContinuousTest.java
rename to services/tests/wmtests/src/com/android/server/wm/SurfaceSyncGroupContinuousTest.java
index 1e32500..8c49c26 100644
--- a/services/tests/wmtests/src/com/android/server/wm/SurfaceSyncerContinuousTest.java
+++ b/services/tests/wmtests/src/com/android/server/wm/SurfaceSyncGroupContinuousTest.java
@@ -24,7 +24,7 @@
import android.os.PowerManager;
import android.view.SurfaceControl;
import android.view.cts.surfacevalidator.CapturedActivity;
-import android.window.SurfaceSyncer;
+import android.window.SurfaceSyncGroup;
import androidx.test.rule.ActivityTestRule;
@@ -35,7 +35,7 @@
import java.util.Objects;
-public class SurfaceSyncerContinuousTest {
+public class SurfaceSyncGroupContinuousTest {
@Rule
public TestName mName = new TestName();
@@ -60,7 +60,7 @@
@Test
public void testSurfaceViewSyncDuringResize() throws Throwable {
- SurfaceSyncer.setTransactionFactory(SurfaceControl.Transaction::new);
- mCapturedActivity.verifyTest(new SurfaceSyncerValidatorTestCase(), mName);
+ SurfaceSyncGroup.setTransactionFactory(SurfaceControl.Transaction::new);
+ mCapturedActivity.verifyTest(new SurfaceSyncGroupValidatorTestCase(), mName);
}
}
diff --git a/services/tests/wmtests/src/com/android/server/wm/SurfaceSyncerTest.java b/services/tests/wmtests/src/com/android/server/wm/SurfaceSyncGroupTest.java
similarity index 61%
rename from services/tests/wmtests/src/com/android/server/wm/SurfaceSyncerTest.java
rename to services/tests/wmtests/src/com/android/server/wm/SurfaceSyncGroupTest.java
index 8738295..846a506 100644
--- a/services/tests/wmtests/src/com/android/server/wm/SurfaceSyncerTest.java
+++ b/services/tests/wmtests/src/com/android/server/wm/SurfaceSyncGroupTest.java
@@ -23,7 +23,7 @@
import android.platform.test.annotations.Presubmit;
import android.view.SurfaceControl;
-import android.window.SurfaceSyncer;
+import android.window.SurfaceSyncGroup;
import androidx.test.filters.SmallTest;
@@ -35,22 +35,20 @@
@SmallTest
@Presubmit
-public class SurfaceSyncerTest {
- private SurfaceSyncer mSurfaceSyncer;
+public class SurfaceSyncGroupTest {
@Before
public void setup() {
- mSurfaceSyncer = new SurfaceSyncer();
- SurfaceSyncer.setTransactionFactory(StubTransaction::new);
+ SurfaceSyncGroup.setTransactionFactory(StubTransaction::new);
}
@Test
public void testSyncOne() throws InterruptedException {
final CountDownLatch finishedLatch = new CountDownLatch(1);
- int startSyncId = mSurfaceSyncer.setupSync(transaction -> finishedLatch.countDown());
+ SurfaceSyncGroup syncGroup = new SurfaceSyncGroup(transaction -> finishedLatch.countDown());
SyncTarget syncTarget = new SyncTarget();
- mSurfaceSyncer.addToSync(startSyncId, syncTarget);
- mSurfaceSyncer.markSyncReady(startSyncId);
+ syncGroup.addToSync(syncTarget);
+ syncGroup.markSyncReady();
syncTarget.onBufferReady();
@@ -61,15 +59,15 @@
@Test
public void testSyncMultiple() throws InterruptedException {
final CountDownLatch finishedLatch = new CountDownLatch(1);
- int startSyncId = mSurfaceSyncer.setupSync(transaction -> finishedLatch.countDown());
+ SurfaceSyncGroup syncGroup = new SurfaceSyncGroup(transaction -> finishedLatch.countDown());
SyncTarget syncTarget1 = new SyncTarget();
SyncTarget syncTarget2 = new SyncTarget();
SyncTarget syncTarget3 = new SyncTarget();
- mSurfaceSyncer.addToSync(startSyncId, syncTarget1);
- mSurfaceSyncer.addToSync(startSyncId, syncTarget2);
- mSurfaceSyncer.addToSync(startSyncId, syncTarget3);
- mSurfaceSyncer.markSyncReady(startSyncId);
+ syncGroup.addToSync(syncTarget1);
+ syncGroup.addToSync(syncTarget2);
+ syncGroup.addToSync(syncTarget3);
+ syncGroup.markSyncReady();
syncTarget1.onBufferReady();
assertNotEquals(0, finishedLatch.getCount());
@@ -84,39 +82,36 @@
}
@Test
- public void testInvalidSyncId() {
- assertFalse(mSurfaceSyncer.addToSync(0, new SyncTarget()));
- }
-
- @Test
- public void testAddSyncWhenSyncComplete() throws InterruptedException {
+ public void testAddSyncWhenSyncComplete() {
final CountDownLatch finishedLatch = new CountDownLatch(1);
- int startSyncId = mSurfaceSyncer.setupSync(transaction -> finishedLatch.countDown());
+ SurfaceSyncGroup syncGroup = new SurfaceSyncGroup(transaction -> finishedLatch.countDown());
SyncTarget syncTarget1 = new SyncTarget();
SyncTarget syncTarget2 = new SyncTarget();
- assertTrue(mSurfaceSyncer.addToSync(startSyncId, syncTarget1));
- mSurfaceSyncer.markSyncReady(startSyncId);
+ assertTrue(syncGroup.addToSync(syncTarget1));
+ syncGroup.markSyncReady();
// Adding to a sync that has been completed is also invalid since the sync id has been
// cleared.
- assertFalse(mSurfaceSyncer.addToSync(startSyncId, syncTarget2));
+ assertFalse(syncGroup.addToSync(syncTarget2));
}
@Test
- public void testMultipleSyncSets() throws InterruptedException {
+ public void testMultiplesyncGroups() throws InterruptedException {
final CountDownLatch finishedLatch1 = new CountDownLatch(1);
final CountDownLatch finishedLatch2 = new CountDownLatch(1);
- int startSyncId1 = mSurfaceSyncer.setupSync(transaction -> finishedLatch1.countDown());
- int startSyncId2 = mSurfaceSyncer.setupSync(transaction -> finishedLatch2.countDown());
+ SurfaceSyncGroup syncGroup1 = new SurfaceSyncGroup(
+ transaction -> finishedLatch1.countDown());
+ SurfaceSyncGroup syncGroup2 = new SurfaceSyncGroup(
+ transaction -> finishedLatch2.countDown());
SyncTarget syncTarget1 = new SyncTarget();
SyncTarget syncTarget2 = new SyncTarget();
- assertTrue(mSurfaceSyncer.addToSync(startSyncId1, syncTarget1));
- assertTrue(mSurfaceSyncer.addToSync(startSyncId2, syncTarget2));
- mSurfaceSyncer.markSyncReady(startSyncId1);
- mSurfaceSyncer.markSyncReady(startSyncId2);
+ assertTrue(syncGroup1.addToSync(syncTarget1));
+ assertTrue(syncGroup2.addToSync(syncTarget2));
+ syncGroup1.markSyncReady();
+ syncGroup2.markSyncReady();
syncTarget1.onBufferReady();
@@ -134,19 +129,21 @@
public void testMergeSync() throws InterruptedException {
final CountDownLatch finishedLatch1 = new CountDownLatch(1);
final CountDownLatch finishedLatch2 = new CountDownLatch(1);
- int startSyncId1 = mSurfaceSyncer.setupSync(transaction -> finishedLatch1.countDown());
- int startSyncId2 = mSurfaceSyncer.setupSync(transaction -> finishedLatch2.countDown());
+ SurfaceSyncGroup syncGroup1 = new SurfaceSyncGroup(
+ transaction -> finishedLatch1.countDown());
+ SurfaceSyncGroup syncGroup2 = new SurfaceSyncGroup(
+ transaction -> finishedLatch2.countDown());
SyncTarget syncTarget1 = new SyncTarget();
SyncTarget syncTarget2 = new SyncTarget();
- assertTrue(mSurfaceSyncer.addToSync(startSyncId1, syncTarget1));
- assertTrue(mSurfaceSyncer.addToSync(startSyncId2, syncTarget2));
- mSurfaceSyncer.markSyncReady(startSyncId1);
- mSurfaceSyncer.merge(startSyncId2, startSyncId1, mSurfaceSyncer);
- mSurfaceSyncer.markSyncReady(startSyncId2);
+ assertTrue(syncGroup1.addToSync(syncTarget1));
+ assertTrue(syncGroup2.addToSync(syncTarget2));
+ syncGroup1.markSyncReady();
+ syncGroup2.merge(syncGroup1);
+ syncGroup2.markSyncReady();
- // Finish syncTarget2 first to test that the syncSet is not complete until the merged sync
+ // Finish syncTarget2 first to test that the syncGroup is not complete until the merged sync
// is also done.
syncTarget2.onBufferReady();
finishedLatch2.await(1, TimeUnit.SECONDS);
@@ -167,23 +164,25 @@
public void testMergeSyncAlreadyComplete() throws InterruptedException {
final CountDownLatch finishedLatch1 = new CountDownLatch(1);
final CountDownLatch finishedLatch2 = new CountDownLatch(1);
- int startSyncId1 = mSurfaceSyncer.setupSync(transaction -> finishedLatch1.countDown());
- int startSyncId2 = mSurfaceSyncer.setupSync(transaction -> finishedLatch2.countDown());
+ SurfaceSyncGroup syncGroup1 = new SurfaceSyncGroup(
+ transaction -> finishedLatch1.countDown());
+ SurfaceSyncGroup syncGroup2 = new SurfaceSyncGroup(
+ transaction -> finishedLatch2.countDown());
SyncTarget syncTarget1 = new SyncTarget();
SyncTarget syncTarget2 = new SyncTarget();
- assertTrue(mSurfaceSyncer.addToSync(startSyncId1, syncTarget1));
- assertTrue(mSurfaceSyncer.addToSync(startSyncId2, syncTarget2));
- mSurfaceSyncer.markSyncReady(startSyncId1);
+ assertTrue(syncGroup1.addToSync(syncTarget1));
+ assertTrue(syncGroup2.addToSync(syncTarget2));
+ syncGroup1.markSyncReady();
syncTarget1.onBufferReady();
// The first sync will still get a callback when it's sync requirements are done.
finishedLatch1.await(5, TimeUnit.SECONDS);
assertEquals(0, finishedLatch1.getCount());
- mSurfaceSyncer.merge(startSyncId2, startSyncId1, mSurfaceSyncer);
- mSurfaceSyncer.markSyncReady(startSyncId2);
+ syncGroup2.merge(syncGroup1);
+ syncGroup2.markSyncReady();
syncTarget2.onBufferReady();
// Verify that the second sync will receive complete since the merged sync was already
@@ -192,11 +191,11 @@
assertEquals(0, finishedLatch2.getCount());
}
- private static class SyncTarget implements SurfaceSyncer.SyncTarget {
- private SurfaceSyncer.SyncBufferCallback mSyncBufferCallback;
+ private static class SyncTarget implements SurfaceSyncGroup.SyncTarget {
+ private SurfaceSyncGroup.SyncBufferCallback mSyncBufferCallback;
@Override
- public void onReadyToSync(SurfaceSyncer.SyncBufferCallback syncBufferCallback) {
+ public void onReadyToSync(SurfaceSyncGroup.SyncBufferCallback syncBufferCallback) {
mSyncBufferCallback = syncBufferCallback;
}
diff --git a/services/tests/wmtests/src/com/android/server/wm/SurfaceSyncerValidatorTestCase.java b/services/tests/wmtests/src/com/android/server/wm/SurfaceSyncGroupValidatorTestCase.java
similarity index 87%
rename from services/tests/wmtests/src/com/android/server/wm/SurfaceSyncerValidatorTestCase.java
rename to services/tests/wmtests/src/com/android/server/wm/SurfaceSyncGroupValidatorTestCase.java
index d65b80a..2df3085 100644
--- a/services/tests/wmtests/src/com/android/server/wm/SurfaceSyncerValidatorTestCase.java
+++ b/services/tests/wmtests/src/com/android/server/wm/SurfaceSyncGroupValidatorTestCase.java
@@ -31,18 +31,18 @@
import android.view.cts.surfacevalidator.ISurfaceValidatorTestCase;
import android.view.cts.surfacevalidator.PixelChecker;
import android.widget.FrameLayout;
-import android.window.SurfaceSyncer;
+import android.window.SurfaceSyncGroup;
import androidx.annotation.NonNull;
/**
* A validator class that will create a SurfaceView and then update its size over and over. The code
* will request to sync the SurfaceView content with the main window and validate that there was
- * never an empty area (black color). The test uses {@link SurfaceSyncer} class to gather the
+ * never an empty area (black color). The test uses {@link SurfaceSyncGroup} class to gather the
* content it wants to synchronize.
*/
-public class SurfaceSyncerValidatorTestCase implements ISurfaceValidatorTestCase {
- private static final String TAG = "SurfaceSyncerValidatorTestCase";
+public class SurfaceSyncGroupValidatorTestCase implements ISurfaceValidatorTestCase {
+ private static final String TAG = "SurfaceSyncGroupValidatorTestCase";
private final Runnable mRunnable = new Runnable() {
@Override
@@ -55,12 +55,11 @@
private Handler mHandler;
private SurfaceView mSurfaceView;
private boolean mLastExpanded = true;
- private final SurfaceSyncer mSurfaceSyncer = new SurfaceSyncer();
private RenderingThread mRenderingThread;
private FrameLayout mParent;
- private int mLastSyncId = -1;
+ private SurfaceSyncGroup mSyncGroup;
final SurfaceHolder.Callback mCallback = new SurfaceHolder.Callback() {
@Override
@@ -76,11 +75,11 @@
@Override
public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width,
int height) {
- if (mLastSyncId >= 0) {
- mSurfaceSyncer.addToSync(mLastSyncId, mSurfaceView, frameCallback ->
+ if (mSyncGroup != null) {
+ mSyncGroup.addToSync(mSurfaceView, frameCallback ->
mRenderingThread.setFrameCallback(frameCallback));
- mSurfaceSyncer.markSyncReady(mLastSyncId);
- mLastSyncId = -1;
+ mSyncGroup.markSyncReady();
+ mSyncGroup = null;
}
}
@@ -118,7 +117,7 @@
}
public void updateSurfaceViewSize() {
- if (mRenderingThread == null || mLastSyncId >= 0 || !mRenderingThread.isReadyToSync()) {
+ if (mRenderingThread == null || mSyncGroup != null || !mRenderingThread.isReadyToSync()) {
return;
}
@@ -133,8 +132,8 @@
mLastExpanded = !mLastExpanded;
mRenderingThread.pauseRendering();
- mLastSyncId = mSurfaceSyncer.setupSync(() -> { });
- mSurfaceSyncer.addToSync(mLastSyncId, mParent);
+ mSyncGroup = new SurfaceSyncGroup();
+ mSyncGroup.addToSync(mParent.getRootSurfaceControl());
ViewGroup.LayoutParams svParams = mSurfaceView.getLayoutParams();
svParams.height = height;
@@ -143,7 +142,7 @@
private static class RenderingThread extends HandlerThread {
private final SurfaceHolder mSurfaceHolder;
- private SurfaceSyncer.SurfaceViewFrameCallback mFrameCallback;
+ private SurfaceSyncGroup.SurfaceViewFrameCallback mFrameCallback;
private boolean mPauseRendering;
private boolean mComplete;
@@ -202,7 +201,7 @@
return mFrameCallback == null;
}
}
- public void setFrameCallback(SurfaceSyncer.SurfaceViewFrameCallback frameCallback) {
+ public void setFrameCallback(SurfaceSyncGroup.SurfaceViewFrameCallback frameCallback) {
synchronized (this) {
mFrameCallback = frameCallback;
mPauseRendering = false;
diff --git a/tests/SurfaceViewSyncTest/src/com/android/test/SurfaceViewSyncActivity.java b/tests/SurfaceViewSyncTest/src/com/android/test/SurfaceViewSyncActivity.java
index ab7f24a..03f61fa4 100644
--- a/tests/SurfaceViewSyncTest/src/com/android/test/SurfaceViewSyncActivity.java
+++ b/tests/SurfaceViewSyncTest/src/com/android/test/SurfaceViewSyncActivity.java
@@ -35,11 +35,11 @@
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Switch;
-import android.window.SurfaceSyncer;
+import android.window.SurfaceSyncGroup;
/**
* Test app that allows the user to resize the SurfaceView and have the new buffer sync with the
- * main window. This tests that {@link SurfaceSyncer} is working correctly.
+ * main window. This tests that {@link SurfaceSyncGroup} is working correctly.
*/
public class SurfaceViewSyncActivity extends Activity implements SurfaceHolder.Callback {
private static final String TAG = "SurfaceViewSyncActivity";
@@ -49,12 +49,10 @@
private RenderingThread mRenderingThread;
- private final SurfaceSyncer mSurfaceSyncer = new SurfaceSyncer();
-
private Button mExpandButton;
private Switch mEnableSyncSwitch;
- private int mLastSyncId = -1;
+ private SurfaceSyncGroup mSyncGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -76,7 +74,7 @@
}
private void updateSurfaceViewSize(Rect bounds, View container) {
- if (mLastSyncId >= 0) {
+ if (mSyncGroup != null) {
return;
}
@@ -91,8 +89,8 @@
mLastExpanded = !mLastExpanded;
if (mEnableSyncSwitch.isChecked()) {
- mLastSyncId = mSurfaceSyncer.setupSync(() -> { });
- mSurfaceSyncer.addToSync(mLastSyncId, container);
+ mSyncGroup = new SurfaceSyncGroup();
+ mSyncGroup.addToSync(container.getRootSurfaceControl());
}
ViewGroup.LayoutParams svParams = mSurfaceView.getLayoutParams();
@@ -112,14 +110,14 @@
@Override
public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
if (mEnableSyncSwitch.isChecked()) {
- if (mLastSyncId < 0) {
+ if (mSyncGroup == null) {
mRenderingThread.renderFrame(null, width, height);
return;
}
- mSurfaceSyncer.addToSync(mLastSyncId, mSurfaceView, frameCallback ->
+ mSyncGroup.addToSync(mSurfaceView, frameCallback ->
mRenderingThread.renderFrame(frameCallback, width, height));
- mSurfaceSyncer.markSyncReady(mLastSyncId);
- mLastSyncId = -1;
+ mSyncGroup.markSyncReady();
+ mSyncGroup = null;
} else {
mRenderingThread.renderFrame(null, width, height);
}
@@ -133,7 +131,7 @@
private static class RenderingThread extends HandlerThread {
private final SurfaceHolder mSurfaceHolder;
private Handler mHandler;
- private SurfaceSyncer.SurfaceViewFrameCallback mFrameCallback;
+ private SurfaceSyncGroup.SurfaceViewFrameCallback mFrameCallback;
private final Point mSurfaceSize = new Point();
int mColorValue = 0;
@@ -147,7 +145,7 @@
mPaint.setTextSize(100);
}
- public void renderFrame(SurfaceSyncer.SurfaceViewFrameCallback frameCallback, int width,
+ public void renderFrame(SurfaceSyncGroup.SurfaceViewFrameCallback frameCallback, int width,
int height) {
if (mHandler != null) {
mHandler.post(() -> {