1 Activity的創(chuàng)建
ActivityThread對于App進程來說膊升,它是App的入口。此外ActivityThread還實現(xiàn)了創(chuàng)建主線程Looper铸磅、dump應(yīng)用內(nèi)存使用情況信卡、獲取應(yīng)用包名等接口。我們看看ActivityThread對于四大組件的作用煮剧,一句話概括,ActivityThread管理著四大組件的生命周期方法的調(diào)用将鸵。
??AMS服務(wù)進程發(fā)出信號觸發(fā)App內(nèi)的ActivityThread通過反射實例化Activity并啟動Activity勉盅,然后調(diào)用activity的attach()方法。
private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
//...
ContextImpl appContext = createBaseContextForActivity(r);
Activity activity = null;
try {
java.lang.ClassLoader cl = appContext.getClassLoader();
// 通過反射創(chuàng)建Activity
activity = mInstrumentation.newActivity(
cl, component.getClassName(), r.intent);
StrictMode.incrementExpectedActivityCount(activity.getClass());
r.intent.setExtrasClassLoader(cl);
r.intent.prepareToEnterProcess();
if (r.state != null) {
r.state.setClassLoader(cl);
}
} catch (Exception e) {
if (!mInstrumentation.onException(activity, e)) {
throw new RuntimeException(
"Unable to instantiate activity " + component
+ ": " + e.toString(), e);
}
}
//調(diào)用activity的attach方法
activity.attach(appContext, this, getInstrumentation(), r.token,
r.ident, app, r.intent, r.activityInfo, title, r.parent,
r.embeddedID, r.lastNonConfigurationInstances, config,
r.referrer, r.voiceInteractor, window, r.configCallback,
r.assistToken);
//...
}
然后開始了Activity的生命周期顶掉。執(zhí)行onCreate()-onStart()-onResume()草娜,onResume()執(zhí)行時頁面還不可見,onResume()完全執(zhí)行完之后的第一個VSYNC信號后頁面才可見痒筒。onResume()將DecorView添加到WindowManagerGlobal中宰闰。
@Override
public void handleResumeActivity(IBinder token, boolean finalStateRequest, boolean isForward,
String reason) {
// ...
// The window is now visible if it has been added, we are not
// simply finishing, and we are not starting another activity.
if (r.window == null && !a.mFinished && willBeVisible) {
// 獲取到PhoneWindow和DecorView
r.window = r.activity.getWindow();
View decor = r.window.getDecorView();
decor.setVisibility(View.INVISIBLE);
ViewManager wm = a.getWindowManager();
WindowManager.LayoutParams l = r.window.getAttributes();
a.mDecor = decor;
l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
l.softInputMode |= forwardBit;
if (r.mPreserveWindow) {
a.mWindowAdded = true;
r.mPreserveWindow = false;
// Normally the ViewRoot sets up callbacks with the Activity
// in addView->ViewRootImpl#setView. If we are instead reusing
// the decor view we have to notify the view root that the
// callbacks may have changed.
ViewRootImpl impl = decor.getViewRootImpl();
if (impl != null) {
impl.notifyChildRebuilt();
}
}
if (a.mVisibleFromClient) {
if (!a.mWindowAdded) {
a.mWindowAdded = true;
// wm是WindowManagerImpl,
// 將DecorView add到App的單例WindowManagerGlobal中
wm.addView(decor, l);
} else {
// The activity will get a callback for this {@link LayoutParams} change
// earlier. However, at that time the decor will not be set (this is set
// in this method), so no action will be taken. This call ensures the
// callback occurs with the decor set.
a.onWindowAttributesChanged(l);
}
}
// If the window has already been added, but during resume
// we started another activity, then don't yet make the
// window visible.
} else if (!willBeVisible) {
if (localLOGV) Slog.v(TAG, "Launch " + r + " mStartedActivity set");
r.hideForNow = true;
}
}
2 Activity的渲染
Activity的attach()方法內(nèi)會初始化一個PhoneWindow對象(一個Activity對應(yīng)一個PhoneWindow對象)。
??Android系統(tǒng)AMS服務(wù)通過Binder與ActivityThread進行通訊簿透,ActivityThread將App內(nèi)所有Activity的頁面進行管理移袍。每個Activity中有一個對應(yīng)的PhoneWindow,每個PhoneWindow有對應(yīng)的DecorView老充,DecorView是布局內(nèi)layout的容器
3 WindowManagerGlobal
每個App都只有一個WindowManagerGlobal對象葡盗,App層的單例對象。ActivityThread通過WindowManagerImpl與WindowManagerGlobal實現(xiàn)通訊啡浊,WindowManagerGlobal用于緩存所有頁面的PhoneWindow觅够、DecorView、ViewRootImpl等界面相關(guān)的數(shù)據(jù)虫啥。
??WindowManagerGlobal內(nèi)部有addView()蔚约、removeView()等增刪查的所有方法都是通過遍歷的形式進行邏輯處理,對外提供服務(wù)涂籽。
??主要給WMS提供管理所有View的便利。由于與WMS是SystemServer進程中砸抛,和App屬于不同進程评雌,所有使用的是Binder進程間通訊。
4 WindowManagerService
WindowManagerService窗口管理服務(wù)簡稱WMS直焙,一臺設(shè)備只有一個WMS景东。WMS管理所有App的全部PhoneWindow。
??WindowManagerGlobal利用Session跨進程通過WMS與SurfaceFlinger通訊奔誓。根據(jù)每個不同的應(yīng)用一一對應(yīng)創(chuàng)建一個Surface斤吐,用于該應(yīng)用的渲染搔涝。
5 建立Surface和SurfaceFlinger連接
SurfaceFlinger服務(wù)主要實現(xiàn)了兩個Binder service用于App連接:
1) SurfaceFlinger
??派生自BnSurfaceComposer,是SurfaceFlinger程序的主服務(wù)和措,在程序啟動時就被構(gòu)造并添加到servicemanager庄呈,相關(guān)代碼在main_surfaceflinger.cpp,服務(wù)名為”SurfaceFlinger”
2) Client
??派生自BnSurfaceComposerClient派阱,在SurfaceFlinger:: createConnection的時候被創(chuàng)建诬留,對應(yīng)一個App Client連接
然后App啟動后,需要通過如下操作和SurfaceFlinger建立會話:
1)通過servicemanager獲取服務(wù)SurfaceFlinger的BpBinder贫母,然后轉(zhuǎn)換成BpSurfaceComposer
2)調(diào)用BpsurfaceComposer.createConnection建立連接文兑,然后將返回的BpBinder轉(zhuǎn)換成BpSurfaceComposeClient
Android接著提供了兩個類用于簡化App端的操作,主要包括:
1)ComposerService
??單列類腺劣,主要封裝跟SurfaceFlinger的連接绿贞,在構(gòu)造時調(diào)用connectlocaked成員函數(shù)連接
“SurfaceFlinger”然后將BpSurfaceCompose保存到成員變量mComposerService
2)SurfaceComposerClient
??封裝跟SurfaceFlinger建立會話連接的操作,在onFirstRef時調(diào)用createConnection建立
會話并將BpSurfaceComposerClient保存到成員變量mClient
3)Composer
??單列類橘原,主要封裝對Layer數(shù)據(jù)配置相關(guān)操作
接下去基于代碼來分析籍铁,封裝好后,App初始化連接很簡單
sp<SurfaceComposerClient> session= new SurfaceComposerClient();
就一行代碼靠柑,接著看構(gòu)造函數(shù)
SurfaceComposerClient::SurfaceComposerClient()
: mStatus(NO_INIT), mComposer(Composer::getInstance()){
}
獲取Composer單例對象并保存到mComposer寨辩,由于SurfaceComposerClient派生自RefBase
class SurfaceComposerClient : public RefBase
所以在其構(gòu)造時,會調(diào)用incStrong第一次增加強引用計數(shù)歼冰,同時onFirstRef會被調(diào)用
void SurfaceComposerClient::onFirstRef() {
sp<ISurfaceComposer> sm(ComposerService::getComposerService());
if (sm != 0) {
sp<ISurfaceComposerClient> conn = sm->createConnection();
if (conn != 0) {
mClient = conn;
mStatus = NO_ERROR;
}
}
}
這個函數(shù)完成了連接的最終操作靡狞,先是通過ComposerService::getComposerService()生成
ComposeService單列,并調(diào)用其connectLocked連接SurfaceFlinger返回BpSurfaceComposer隔嫡,接著調(diào)用sm->createConnection()創(chuàng)建會話并保存到mClient甸怕。接下來看看SurfaceFlinger.createConnection的代碼
sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
{
sp<ISurfaceComposerClient> bclient;
sp<Client> client(new Client(this));
status_t err = client->initCheck();
if (err == NO_ERROR) {
bclient = client;
}
return bclient;
}
很簡單,就是創(chuàng)建Client本地對象并返回
到這里腮恩,App跟SurfaceFlinger的初始化連接已經(jīng)結(jié)束梢杭,接下去就是基于會話對象,創(chuàng)建繪圖表面了
6 流程圖