ZygoteConnection.java
=> boolean runOnce() 最後會 fork 出新的 process
#################################################
ActivityThread.java
main ()
=> handleLaunchActivity
-----------------------------------------------------------------------------------------------------------------
=> performLaunchActivity
// 生出 Activity
=> activity = mInstrumentation.newActivity(cl,component.getClassName(), r.intent);
=> activity.attach
// 生出 PhoneWindow, 並取得 WindowManager "WindowManagerImpl", 他的 parentWindow 是 mWindow (PhoneWindow)
=> mWindow = new PhoneWindow(this);
=> mWindow.setWindowManager(
(WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
mToken, mComponent.flattenToString(),
(info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
=> mWindowManager = ((WindowManagerImpl)wm).createLocalWindowManager(this);
// new WindowManagerImpl(mDisplay, parentWindow);
// 呼叫 Activity 的 onCreate (這裡已經被你的Activity override掉了)
=> onCreate
// 呼叫setContentView();
=> setContentView(R.layout.activity_main);
// 呼叫 PhoneWindow::setContentView()
=> getWindow().setContentView(view);
// 取得 mDecor 以及 mContentParent
// 其中 mContentParent 是 ViewGroup, 在創建的時候我們會把這個 mDecor帶入
=> installDecor(); // new DecorView(getContext(), -1);
=> generateLaout(mDecor); // ViewGroup contentParent = (ViewGroup)findViewById(ID_ANDROID_CONTENT);
// 呼叫 addView, 這邊的 view 是一開始帶進來的 R.layout.activity_main
=> mContentParent.addView(view, params);
// 將傳入的 view 作為 child 保存起來, 並指定 parent為自己
=> addViewInner(child, index, params, false);
=> child.mParent = this;
-----------------------------------------------------------------------------------------------------------------
=> handleResumeActivity
// 呼叫 Activity 的 onResume()Activity的onResume()方法
=> ActivityClientRecord r = performResumeActivity(token, clearHide);
// 呼叫 WindowManager, 也就是 WindowManagerImpl 的 addView
=> wm.addView(decor, l);
// 實作在 WindowManagerGlobal
=> mGlobal.addView(view, params, mDisplay, mParentWindow);
// 建立 ViewRootImpl
=> ViewRootImpl root;
root = new ViewRootImpl(view.getContext(), display);
// 在 ViewRootImpl 的建構子內部又透過了 windowManager 去呼叫 openSession
// 最後取得了 Session 用來作為跟 WindowManagerService 通信的手段
=> sWindowSession = windowManager.openSession(
new IWindowSessionCallback.Stub() {
@Override
public void onAnimatorScaleChanged(float scale) {
ValueAnimator.setDurationScale(scale);
}
},
imm.getClient(), imm.getInputContext());
// Session session = new Session(this, callback, client, inputContext);
// 對於 ViewRootImpl 來說是他內部的 mWindowSession
// 另外這裡還 new 了一個 W class, 用處是收取發生的事件
// 他繼承了 IWindow, 可以看到裡面實作的 API 諸如 dispatchGetNewSurface, dispatchAppVisibility 這種
mWindow = new W(this);
// 注意 ViewRootImpl 內部有個 mSurface 對象, 之後會拿這個對象來繪圖
=> final Surface mSurface = new Surface();
// 接著我們繼續回到 WindowManagerGlobal 的 addView 函數,
// 接下來是呼叫了 ViewRootImpl 的 setView, 這邊的 view 參數是 decorView 而不是一開始傳入的 R.layout.activity_main
=> root.setView(view, wparams, panelParentView);
// ViewRootImpl 的 requestLayout 可就做了不少事情
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
=> requestLayout();
// 規劃了接下來的 Traversal callback
=> scheduleTraversals();
// Posts a callback to run on the next frame
// The callback runs once then is automatically removed.
// 怎麼觸發的後面再看, 我們先跳離 requestLayout()
=> mChoreographer.postCallback(
Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null);
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// mWindowSession 就是前面 new 出來的 Session
=> mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes,
getHostVisibility(), mDisplay.getDisplayId(),
mAttachInfo.mContentInsets, mAttachInfo.mStableInsets,
mAttachInfo.mOutsets, mInputChannel);
// mService 是 WindowManagerService (前面有提到說透過 Session 來跟 WindowManagerService 溝通)
=> mService.addWindow(this, window, seq, attrs, viewVisibility, displayId,
outContentInsets, outStableInsets, outOutsets, outInputChannel);
// 創建一個 WindowState 對象, 並呼叫他的 attach() 函數
=> WindowState win = new WindowState(this, session, client, token,
attachedWindow, appOp[0], seq, attrs, viewVisibility, displayContent);
=> win.attach();
// 呼叫 Session 的 windowAddedLocked();
=> mSession.windowAddedLocked();
// new 出一個 SurfaceSession 對象
=> mSurfaceSession = new SurfaceSessionSurfaceSession();
/** Create a new connection with the surface flinger. */
// 跟 SurfaceFlinger 搭上線了
=> mNativeClient = nativeCreate();
// 生成了一個 SurfaceComposerClient 對象, 他之後會被用來跟 SurfaceControl 進行溝通
=> SurfaceComposerClient* client = new SurfaceComposerClient();
// 把這個 WindowState 對象放進mWindowMap, 後面畫圖的時候會用到
=> mWindowMap.put(client.asBinder(), win);
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// 回到 scheduleTraversals(), 從 mTraversalRunnable 進入
=> final TraversalRunnable mTraversalRunnable = new TraversalRunnable();
=> doTraversal();
=> performTraversals();
**************************************************************************
=> relayoutResult = relayoutWindow(params, viewVisibility, insetsPending);
=> performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
=> performLayout(lp, desiredWindowWidth, desiredWindowHeight);
=> performDraw();
**************************************************************************
// 先從 relayoutWindow 開始
=> 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,
mPendingStableInsets, mPendingOutsets, mPendingConfiguration, mSurface);
// Session
// 注意這個 mSurface 傳入以後命名變成了 outSurface (這是在 ViewRootImpl 建構子生成的那個 Surface 對象
=> mService.relayoutWindow(this, window, seq, attrs,
requestedWidth, requestedHeight, viewFlags, flags,
outFrame, outOverscanInsets, outContentInsets, outVisibleInsets,
outStableInsets, outsets, outConfig, outSurface);
// WindowManagerService
// 拿出剛剛在 addWindow 生出來的 WindowState
=> WindowState win = windowForClientLocked(session, client, false); // WindowState win = mWindowMap.get(client);
=> WindowStateAnimator winAnimator = win.mWinAnimator; // 在 WindowState 建構時, new WindowStateAnimator(this);
// 建構出 SurfaceControl ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
=> SurfaceControl surfaceControl = winAnimator.createSurfaceLocked();
=> mSurfaceControl = new SurfaceControl(
mSession.mSurfaceSession,
attrs.getTitle().toString(),
width, height, format, flags);
// 又是一個 nativeCreate, 這次是經由 SurfaceComposerClient 產生一個 SurfaceControl 對象
=> mNativeObject = nativeCreate(session, name, w, h, format, flags);
=> sp<SurfaceComposerClient> client(android_view_SurfaceSession_getClient(env, sessionObj));
// 在 SurfaceComposerClient 的 createSurface 還會去申請 gbp (IGraphicBufferProducer)
=> sp<SurfaceControl> surface = client->createSurface(String8(name.c_str()), w, h, format, flags);
// 這裡的 mClient 是 SuffaceFlinger 的 Client 對象
=> mClient->createSurface(name, w, h, format, flags, &handle, &gbp);
=> sp<MessageBase> msg = new MessageCreateLayer(mFlinger.get(), name, this, w, h, format, flags, handle, gbp);
// 這裡會通過 SurfaceFlinger 的 mEventQueue 才會完成
/*
簡易流程大致如下:
1.) SurfaceFlinger Init 的時候, mEventQueue 開始等待 Message
=> PollOnce, 時間是無限長, 最後會停在 epoll_wait 等待事件
2.) 透過 mFlinger->postMessageSync(msg); 塞入 Message
3.) epoll_wait() return, 取得各個 Message (messageEnvelope = mMessageEnvelopes.itemAt(0);)
4.) 取出 Message 中的 handler 跟 message, 執行 handler->handleMessage(message);
在這個 case 中會先執行完 handler
virtual bool handler() {
result = flinger->createLayer(name, client, w, h, format, flags,
handle, gbp);
return true;
}
並且將 barrier unlock (open)
void MessageBase::handleMessage(const Message&) {
this->handler();
barrier.open();
};
*/
// createLayer
=> SurfaceFlinger::createNormalLayer
// 取得 handle 與 bufferProducer
=> *outLayer = new Layer(this, client, name, w, h, flags);
status_t err = (*outLayer)->setBuffers(w, h, format, flags);
if (err == NO_ERROR) {
*handle = (*outLayer)->getHandle();
*gbp = (*outLayer)->getProducer();
}
// 將 Layer 放置到 client 上
=> result = addClientLayer(client, *handle, *gbp, layer);
// ------------------------------------------------------------------ 建構出 SurfaceControl
// 接著呼叫 outSurface 也就是 Surface 對象的 copyFrom
=> outSurface.copyFrom(surfaceControl);
=> long newNativeObject = nativeCreateFromSurfaceControl(surfaceControlPtr);
// 取得 native 的 Surface 對象
=> sp<Surface> surface(ctrl->getSurface());
// 這裡的 mGraphicBufferProducer 就是前面帶進去的 gbp, 已經在 SurfaceFlinger 裡面創建了
// 它就是 mProducer = new MonitoredProducer(producer, mFlinger);
=> mSurfaceData = new Surface(mGraphicBufferProducer, false);\
// 將這個 native 的 Surface 對象保存起來
=> setNativeObjectLocked(newNativeObject); // mNativeObject = ptr;
**************************************************************************
// 待續...接下來是 performMeasure, performLayout, performDraw
沒有留言:
張貼留言