研究Surface的创建流程需要一个精简的流程,不然跳转太多,脑子都乱了。先讲一个大致的过程:ViewRootImpl在构造时会new一个Surface,在relayoutWindow方法中传入mSurface,这个跨进程的调用会在binderserver端新构造一个Surface,在WMS中构造SurfaceControl,然后将生成的SurfaceControl传入binderserver端的Surface,最后把这个binderserver端的Surface返回到调用进程,即ViewRootImpl中。下面是调用关键方法:
1.ViewRootImpl的relayoutWindow方法中调用Session的relayout方法来设置mSurface private int relayoutWindow(WindowManager.LayoutParams params, int viewVisibility,
boolean insetsPending) throws RemoteException {
......
int relayoutResult = mWindowSession.relayout(
mWindow, mSeq, params,
(int) (mView.getMeasuredWidth() * appScale + 0.5f),
(int) (mView.getMeasuredHeight() * appScale + 0.5f),
viewVisibility, insetsPending ? WindowManagerGlobal.RELAYOUT_INSETS_PENDING : 0,
mWinFrame, mPendingOverscanInsets, mPendingContentInsets, mPendingVisibleInsets,
mPendingConfiguration, mSurface);
......
return relayoutResult;
}
调用mWindowSession的relayout方法,注意这是个binder调用。
2.onTranct中返回一个新构造的Surface
case TRANSACTION_relayout:
{
......
android.view.Surface _arg12;
_arg12 = new android.view.Surface();//构造一个新的Surface
int _result = this.relayout(_arg0, _arg1, _arg2, _arg3, _arg4, _arg5, _arg6, _arg7, _arg8, _arg9, _arg10, _arg11, _arg12);
......
if ((_arg12!=null)) {
reply.writeInt(1);
_arg12.writeToParcel(reply, android.os.Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
}
return true;
}
3.在Session调用WMS的relayout方法
public int relayout(IWindow window, int seq, WindowManager.LayoutParams attrs,
int requestedWidth, int requestedHeight, int viewFlags,
int flags, Rect outFrame, Rect outOverscanInsets, Rect outContentInsets,
Rect outVisibleInsets, Configuration outConfig, Surface outSurface) {
int res = mService.relayoutWindow(this, window, seq, attrs,
requestedWidth, requestedHeight, viewFlags, flags,
outFrame, outOverscanInsets, outContentInsets, outVisibleInsets,
outConfig, outSurface);
return res;
}
4.WMS的relayout方法中构造SurfaceControl,并用这个SurfaceControl构造传入的Surface
SurfaceControl surfaceControl = winAnimator.createSurfaceLocked();//创建SurfaceControl
if (surfaceControl != null) {
outSurface.copyFrom(surfaceControl);//通过SurfaceControl设置Surface
if (SHOW_TRANSACTIONS) Slog.i(TAG,
" OUT SURFACE " + outSurface + ": copied");
} else {
// For some reason there isn't a surface. Clear the
// caller's object so they see the same state.
outSurface.release();
}
5.winAnimator的createSurfaceLocked方法中生成SurfaceControl
mSurfaceControl = new SurfaceControl(
mSession.mSurfaceSession,
attrs.getTitle().toString(),
w, h, format, flags);
6.将生成的Surface传回ViewRootImpl
public int relayout(android.view.IWindow window, int seq, android.view.WindowManager.LayoutParams attrs, int requestedWidth, int requestedHeight, int viewVisibility, int flags, android.graphics.Rect outFrame, android.graphics.Rect outOverscanInsets, android.graphics.Rect outCo