最近在看SurfaceSyncGroup,很多代码还是很好理解的,但是其中有几处细节,可能你看的时候没有注意。 因为这个SurfaceSyncGroup名字太长,我后面都会使用SSG缩写代替。
如果你看过代码,你应该可以发现,如果你在同一进程中使用SSG,其实是可以如下操作的。
但是如果跨进程使用,那就要注意了,可能就会有问题了,你以为你创建的是这样子关系
其实你创建的关系和你预期的不一样。
public boolean add(ISurfaceSyncGroup surfaceSyncGroup, boolean parentSyncGroupMerge,
@Nullable Runnable runnable) {
//1.1如果添加的surfaceSyncGroup和当前SurfaceSyncGroup不是同一进程,就是false。
if (isLocalBinder(surfaceSyncGroup.asBinder())) {
boolean didAddLocalSync = addLocalSync(surfaceSyncGroup, parentSyncGroupMerge);
if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) {
Trace.asyncTraceForTrackEnd(Trace.TRACE_TAG_VIEW, mTrackName, hashCode());
}
return didAddLocalSync;
}
synchronized (mLock) {
if (!mHasWMSync) {
// We need to add a signal into WMS since WMS will be creating a new parent
// SurfaceSyncGroup. When the parent SSG in WMS completes, only then do we
// notify the registered listeners that the entire SurfaceSyncGroup is complete.
// This is because the callers don't realize that when adding a different process
// to this SSG, it isn't actually adding to this SSG and really just creating a
// link in WMS. Because of this, the callers would expect the complete listeners
// to only be called when everything, including the other process's
// SurfaceSyncGroups, have completed. Only WMS has that info so we need to send the
// listener to WMS when we set up a server side sync.
mSurfaceSyncGroupCompletedListener = new ISurfaceSyncGroupCompletedListener.Stub() {
@Override
public void onSurfaceSyncGroupComplete() {
synchronized (mLock) {
invokeSyncCompleteCallbacks();
}
}
};
// 1.1.2 然后用自己的token在WM中创建一个替身
if (!addSyncToWm(mToken, false /* parentSyncGroupMerge */,
mSurfaceSyncGroupCompletedListener)) {//跳转到代码分析1.2
mSurfaceSyncGroupCompletedListener = null;
if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) {
Trace.asyncTraceForTrackEnd(Trace.TRACE_TAG_VIEW, mTrackName, hashCode());
}
return false;
}
mHasWMSync = true;
}
}
try {
// 1.1.3 将token传递给子ssg,然后让子的ssg和你的替身建立关系
surfaceSyncGroup.onAddedToSyncGroup(mToken, parentSyncGroupMerge);
} catch (RemoteException e) {
if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) {
Trace.asyncTraceForTrackEnd(Trace.TRACE_TAG_VIEW, mTrackName, hashCode());
}
return false;
}
if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) {
Trace.asyncTraceForTrackEnd(Trace.TRACE_TAG_VIEW, mTrackName, hashCode());
}
return true;
}
1.1.1 如果添加的surfaceSyncGroup和当前SurfaceSyncGroup不是同一进程,就是false。 1.1.2 然后用自己的token在WM中创建一个替身ssg 1.1.3 将token传递给子ssg,然后让子的ssg和你的替身建立关系
当SSG1调用add(SSG2)的时候,SSG1和SSG2都会触发这个函数。 1.2.1 将自己和WM中的替身建立起关系。如果是SSG1,也就意味着和之前Root SSG建立的关系也就断开了。
private boolean addSyncToWm(IBinder token, boolean parentSyncGroupMerge,
@Nullable ISurfaceSyncGroupCompletedListener surfaceSyncGroupCompletedListener) {
try {
if (DEBUG) {
Log.d(TAG, "Attempting to add remote sync to " + mName
+ ". Setting up Sync in WindowManager.");
}
if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) {
Trace.asyncTraceForTrackBegin(Trace.TRACE_TAG_VIEW, mTrackName,
"addSyncToWm=" + token.hashCode(), hashCode());
}
AddToSurfaceSyncGroupResult addToSyncGroupResult = new AddToSurfaceSyncGroupResult();
if (!WindowManagerGlobal.getWindowManagerService().addToSurfaceSyncGroup(token,
parentSyncGroupMerge, surfaceSyncGroupCompletedListener,
addToSyncGroupResult)) {
if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) {
Trace.asyncTraceForTrackEnd(Trace.TRACE_TAG_VIEW, mTrackName, hashCode());
}
return false;
}
//1.2.1 将自己和WM中的替身建立起关系。如果是SSG1,也就意味着和之前Root SSG建立的关系也就断开了。
setTransactionCallbackFromParent(addToSyncGroupResult.mParentSyncGroup,
addToSyncGroupResult.mTransactionReadyCallback);
} catch (RemoteException e) {
if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) {
Trace.asyncTraceForTrackEnd(Trace.TRACE_TAG_VIEW, mTrackName, hashCode());
}
return false;
}
if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) {
Trace.asyncTraceForTrackEnd(Trace.TRACE_TAG_VIEW, mTrackName, hashCode());
}
return true;
}
当SSG1添加到Root SSG的时候,一切还算正常 但是当SSG2添加到SSG1的时候,意外就发生了,Root SSG和SSG1就断开了连接。
private void setTransactionCallbackFromParent(ISurfaceSyncGroup parentSyncGroup,
ITransactionReadyCallback transactionReadyCallback) {
...
synchronized (mLock) {
if (mFinished) {
finished = true;
} else {
// If this SurfaceSyncGroup was already added to a different SurfaceSyncGroup, we
// need to combine everything. We can add the old SurfaceSyncGroup parent to the new
// parent so the new parent doesn't complete until the old parent does.
// Additionally, the old parent will not get the final transaction object and
// instead will send it to the new parent, ensuring that any other SurfaceSyncGroups
// from the original parent are also combined with the new parent SurfaceSyncGroup.
// 2.1.1 如果发现SSG1有parent SSG了, 就将parent ssg(Root SSG)添加到SSG2
if (mParentSyncGroup != null && mParentSyncGroup != parentSyncGroup) {
if (DEBUG) {
Log.d(TAG, "Trying to add to " + parentSyncGroup
+ " but already part of sync group " + mParentSyncGroup + " "
+ mName);
}
try {
//最终会跳转到代码分析 2.2
parentSyncGroup.addToSync(mParentSyncGroup,
true /* parentSyncGroupMerge */);
} catch (RemoteException e) {
}
}
if (DEBUG && mParentSyncGroup == parentSyncGroup) {
Log.d(TAG, "Added to parent that was already the parent");
}
// 2.1.2 保存前一个parent ssg(Root SSG)的callback。
Consumer<Transaction> lastCallback = mTransactionReadyConsumer;
// 2.1.3 将parent ssg 换成 SSG2
mParentSyncGroup = parentSyncGroup;
mTransactionReadyConsumer = (transaction) -> {
if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) {
Trace.asyncTraceForTrackBegin(Trace.TRACE_TAG_VIEW, mTrackName,
"Invoke transactionReadyCallback="
+ transactionReadyCallback.hashCode(), hashCode());
}
// 2.1.4 在SSG1完成任务的时候,虽然SSG1和Root SSG没有名义上的关系了,但是残留在Root SSG mPendingSyncs中SSG1的callback还是要触发一下的,否则会导致Root SSG无法ready。
lastCallback.accept(null);
try {
transactionReadyCallback.onTransactionReady(transaction);
} catch (RemoteException e) {
transaction.apply();
}
if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) {
Trace.asyncTraceForTrackEnd(Trace.TRACE_TAG_VIEW, mTrackName, hashCode());
}
};
addedToSyncListener = mAddedToSyncListener;
}
}
// Invoke the callback outside of the lock when the SurfaceSyncGroup being added was already
// complete.
if (finished) {
try {
transactionReadyCallback.onTransactionReady(null);
} catch (RemoteException e) {
}
} else if (addedToSyncListener != null) {
addedToSyncListener.run();
}
if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) {
Trace.asyncTraceForTrackEnd(Trace.TRACE_TAG_VIEW, mTrackName, hashCode());
}
}
2.1.1 如果发现SSG1有parent SSG了, 就将parent ssg(Root SSG)添加到SSG2 2.1.2 保存前一个parent ssg(Root SSG)的callback。 2.1.3 将parent ssg 换成 SSG2 2.1.4 在SSG1完成任务的时候,虽然SSG1和Root SSG没有名义上的关系了,但是残留在Root SSG mPendingSyncs中SSG1的callback还是要触发一下的,否则会导致Root SSG无法ready。
代码分析2.1中parentSyncGroup.addToSync(mParentSyncGroup, true /* parentSyncGroupMerge */)
最终会跳转到这里,关键的就是parentSyncGroupMerge为true
public ITransactionReadyCallback createTransactionReadyCallback(boolean parentSyncGroupMerge) {
ITransactionReadyCallback transactionReadyCallback =
new ITransactionReadyCallback.Stub() {
@Override
public void onTransactionReady(Transaction t) {
synchronized (mLock) {
if (t != null) {
t.sanitize(Binder.getCallingPid(), Binder.getCallingUid());
// When an older parent sync group is added due to a child syncGroup
// getting added to multiple groups, we need to maintain merge order
// so the older parentSyncGroup transactions are overwritten by
// anything in the newer parentSyncGroup.
// 2.2.1 这里解释了如果为true,为了保证merge的顺序,有一些处理。
if (parentSyncGroupMerge) {
t.merge(mTransaction);
}
mTransaction.merge(t);//p add c
}
mPendingSyncs.remove(this);
if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) {
Trace.instantForTrack(Trace.TRACE_TAG_VIEW, mTrackName,
"onTransactionReady callback=" + hashCode());
}
checkIfSyncIsComplete();
}
}
};
return transactionReadyCallback;
}
2.2.1 这里解释了如果为true,为了保证merge的顺序,有一些处理,用一个图来表示,就是如下图。
SurfaceSyncGroup代码不是很多,但是使用时候要注意这些细节,避免发生错误,具体讲解SurfaceSyncGroup配合BBQ和BlastSyncEngine工作,我还是会选择使用图解视频的方式,给自己留个坑《王小二图解Android-SurfaceSyncGroup》