protected void updateSurface() {
...
final ViewRootImpl viewRoot = getViewRootImpl();
if (viewRoot == null) {
return;
}
if (viewRoot.mSurface == null || !viewRoot.mSurface.isValid()) {
notifySurfaceDestroyed();
tryReleaseSurfaces();
return;
}
mSurface
是 null
或 isValid()
是 false
,则先调用 notifySurfaceDestroyed()
通知 SurfaceHolder.Callback,同时会调用 SurfaceView.tryReleaseSurfaces() 释放 mSurface
、mSurfaceControl
和 mBackgroundControl
。 int myWidth = mRequestedWidth;
if (myWidth <= 0) myWidth = getWidth();
int myHeight = mRequestedHeight;
if (myHeight <= 0) myHeight = getHeight();
final float alpha = getFixedAlpha();
final boolean formatChanged = mFormat != mRequestedFormat;
final boolean visibleChanged = mVisible != mRequestedVisible;
final boolean alphaChanged = mSurfaceAlpha != alpha;
final boolean creating = (mSurfaceControl == null || formatChanged || visibleChanged)
&& mRequestedVisible;
final boolean sizeChanged = mSurfaceWidth != myWidth || mSurfaceHeight != myHeight;
final boolean windowVisibleChanged = mWindowVisibility != mLastWindowVisibility;
boolean redrawNeeded = false;
getLocationInSurface(mLocation);
final boolean positionChanged = mWindowSpaceLeft != mLocation[0]
|| mWindowSpaceTop != mLocation[1];
final boolean layoutSizeChanged = getWidth() != mScreenRect.width()
|| getHeight() != mScreenRect.height();
if (creating || formatChanged || sizeChanged || visibleChanged ||
(mUseAlpha && alphaChanged) || windowVisibleChanged ||
positionChanged || layoutSizeChanged) {
...
try {
mVisible = mRequestedVisible;
mWindowSpaceLeft = mLocation[0];
mWindowSpaceTop = mLocation[1];
mSurfaceWidth = myWidth;
mSurfaceHeight = myHeight;
mFormat = mRequestedFormat;
mLastWindowVisibility = mWindowVisibility;
mTransformHint = viewRoot.getBufferTransformHint();
...
if (creating) {
updateOpaqueFlag();
final String name = "SurfaceView[" + viewRoot.getTitle().toString() + "]";
if (mUseBlastAdapter) {
createBlastSurfaceControls(viewRoot, name, geometryTransaction);
} else {
mDeferredDestroySurfaceControl = createSurfaceControls(viewRoot, name);
}
} else if (mSurfaceControl == null) {
return;
}
createSurfaceControls()
返回上一个 SurfaceControl,设置给 mDeferredDestroySurfaceControl
在下一帧绘制结束之后销毁,在 performDrawFinished()
。Layer 的层次结构如下所示:
graph TD
ViewRootImpl.mSurfaceControl
--> ViewRootImpl.mBoundsLayer
--> SurfaceView.mSurfaceControl
SurfaceView.mSurfaceControl
--> SurfaceView.mBlastSurfaceControl
SurfaceView.mSurfaceControl
--> SurfaceView.mBackgoundControl
final boolean realSizeChanged = performSurfaceTransaction(viewRoot,
translator, creating, sizeChanged, hintChanged, geometryTransaction);
final boolean redrawNeeded = sizeChanged || creating || hintChanged
|| (mVisible && !mDrawFinished);
try {
SurfaceHolder.Callback[] callbacks = null;
final boolean surfaceChanged = creating;
if (mSurfaceCreated && (surfaceChanged || (!visible && visibleChanged))) {
mSurfaceCreated = false;
notifySurfaceDestroyed();
}
copySurface(creating /* surfaceControlCreated */, sizeChanged);
mSurfaceCreated
是 true
时,如果正在创建新 SurfaceControl,即 creating
是 true
,或从可见到不可见,则设置 mSurfaceCreated 为 false,并调用 notifySurfaceDestroyed()
通知。mSurface
. if (visible && mSurface.isValid()) {
if (!mSurfaceCreated && (surfaceChanged || visibleChanged)) {
mSurfaceCreated = true;
mIsCreating = true;
if (callbacks == null) {
callbacks = getSurfaceCallbacks();
}
for (SurfaceHolder.Callback c : callbacks) {
c.surfaceCreated(mSurfaceHolder);
}
}
if (creating || formatChanged || sizeChanged
|| visibleChanged || realSizeChanged) {
if (callbacks == null) {
callbacks = getSurfaceCallbacks();
}
for (SurfaceHolder.Callback c : callbacks) {
c.surfaceChanged(mSurfaceHolder, mFormat, myWidth, myHeight);
}
}
if (redrawNeeded) {
if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " "
+ "surfaceRedrawNeeded");
if (callbacks == null) {
callbacks = getSurfaceCallbacks();
}
mPendingReportDraws++;
viewRoot.drawPending();
SurfaceCallbackHelper sch =
new SurfaceCallbackHelper(this::onDrawFinished);
sch.dispatchSurfaceRedrawNeededAsync(mSurfaceHolder, callbacks);
}
}
} finally {
mIsCreating = false;
if (mSurfaceControl != null && !mSurfaceCreated) {
releaseSurfaces();
}
}
} catch (Exception ex) {
Log.e(TAG, "Exception configuring surface", ex);
}
}
}
mSurface
是有效的,则判断是 surface 新创建还是 surface 属性变化回调相应的接口。