android:SurfaceFlinger启动 .

本文深入探讨了Android系统初始化过程中SurfaceFlinger服务的注册与启动过程,详细解释了system_init()函数中如何通过ServiceManager实现服务注册,并通过RefBase机制进行服务实例化与初始化。特别关注了不同Android版本之间的变化,如从Android 2.2到Android 4.0版本中system_init()函数行为的变化,以及如何通过系统属性设置来控制SurfaceFlinger的启动。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android 2.2(froyo)
system_init()(base/cmds/system_server/library/system_init.cpp)中的 SurfaceFlinger::instantiate();会注册SurfaceFlinger服务:

  1. 119 void SurfaceFlinger::instantiate() {  
  2. 120     if(surfaceflinger == NULL)  
  3. 121     {  
  4. 122         surfaceflinger = new SurfaceFlinger();  
  5. 123     }  
  6. 124     defaultServiceManager()->addService(  
  7. 125             String16(“SurfaceFlinger”), surfaceflinger);  
  8. 126 }  
119 void SurfaceFlinger::instantiate() {
120     if(surfaceflinger == NULL)
121     {
122         surfaceflinger = new SurfaceFlinger();
123     }
124     defaultServiceManager()->addService(
125             String16(“SurfaceFlinger”), surfaceflinger);
126 }


而defaultServiceManager()->addService()实际上调用的是BpServiceManager::addService(const String16& name, const sp<IBinder>& service),所以surfaceflinger就转化成了const sp<IBinder>,又由于sp<T>的构造函数为:

  1. 301 template<typename T>  
  2. 302 sp<T>::sp(T* other)  
  3. 303     : m_ptr(other)  
  4. 304 {  
  5. 305     if (other) other->incStrong(this);  
  6. 306 }  
301 template<typename T>
302 sp<T>::sp(T* other)
303     : m_ptr(other)
304 {
305     if (other) other->incStrong(this);
306 }


在又有:

  1. 281 void RefBase::incStrong(const void* id) const  
  2. 282 {  
  3. 283     weakref_impl* const refs = mRefs;  
  4. ….  
  5. 296  
  6. 297     android_atomic_add(-INITIAL_STRONG_VALUE, &refs->mStrong);  
  7. 298     const_cast<RefBase*>(this)->onFirstRef();  
  8. 299 }  
281 void RefBase::incStrong(const void* id) const
282 {
283     weakref_impl* const refs = mRefs;
….
296
297     android_atomic_add(-INITIAL_STRONG_VALUE, &refs->mStrong);
298     const_cast<RefBase*>(this)->onFirstRef();
299 }


所以,最终会调用SurfaceFlinger::onFirstRef()

  1. 376 void SurfaceFlinger::onFirstRef()  
  2. 377 {  
  3. 378 run(“SurfaceFlinger”, PRIORITY_URGENT_DISPLAY);  
  4. 379  
  5. 380 // Wait for the main thread to be done with its initialization   
  6. 381 mReadyToRunBarrier.wait();  
  7. 382 }  
376 void SurfaceFlinger::onFirstRef()
377 {
378 run(“SurfaceFlinger”, PRIORITY_URGENT_DISPLAY);
379
380 // Wait for the main thread to be done with its initialization
381 mReadyToRunBarrier.wait();
382 }


于是SurfaceFlinger就开始运行了(readyToRun()->threadLoop())



Android 4.0(Ice Cream Sandwich)

init.rc中有

  1. 268 setprop system_init.startsurfaceflinger 0  
  2. …  
  3. 409 service surfaceflinger /system/bin/surfaceflinger  
  4. 410 class main  
  5. 411 user system  
  6. 412 group graphics  
  7. 413 onrestart restart zygote  
268 setprop system_init.startsurfaceflinger 0
…
409 service surfaceflinger /system/bin/surfaceflinger
410 class main
411 user system
412 group graphics
413 onrestart restart zygote


所以system_init()将不会启动SurfaceFlinger,而是由init程序启动的。而且instantiate()实际为BinderService::instantiate(),后面的实际启动也是由RefBase机制来进行的。

 

 

加注:

当前在低于4.0的android中, 可以通过设置systeminit.startsurfaceflinger的属性值为0, 来禁止system server启动surfaceflinger.

03-05 17:45:49.335 03608 03868 I SurfaceControl: Transaction reparent sc Surface(name=Splash Screen com.shaizsports.livecricket.tensports)/@0xf93b38a, tx=15496242073844 ,layerId= 11070 ,parentLayerId= -1, parent=null, caller=android.view.SurfaceControl$Transaction.remove:4972 com.android.server.wm.WindowSurfaceController.destroy:143 com.android.server.wm.WindowStateAnimator.destroySurface:872 com.android.server.wm.WindowStateAnimator.destroySurfaceLocked:493 com.android.server.wm.WindowState.destroySurfaceUnchecked:3856 com.android.server.wm.WindowState.destroySurface:3830 com.android.server.wm.WindowState.onExitAnimationDone:5543 com.android.server.wm.WindowStateAnimator.onAnimationFinished:232 com.android.server.wm.WindowState.onAnimationFinished:5831 com.android.server.wm.WindowContainer$$ExternalSyntheticLambda5.onAnimationFinished:0 03-05 17:45:49.335 03608 03868 I SurfaceControl: SurfaceControl release: this = Surface(name=Splash Screen com.shaizsports.livecricket.tensports)/@0xf93b38a , layerId= 11070 , caller=android.view.SurfaceControl$Transaction.remove:4973 com.android.server.wm.WindowSurfaceController.destroy:143 com.android.server.wm.WindowStateAnimator.destroySurface:872 com.android.server.wm.WindowStateAnimator.destroySurfaceLocked:493 com.android.server.wm.WindowState.destroySurfaceUnchecked:3856 03-05 17:45:49.335 03608 03868 D Osense-ComplexSceneManager: handleComplexScene sceneType=2, win=Window{a4cd585 u0 Splash Screen com.shaizsports.livecricket.tensports EXITING},isSurfaceShown=false,type=3,mode=1 03-05 17:45:49.335 03608 03868 I WindowManager: Window{a4cd585 u0 Splash Screen com.shaizsports.livecricket.tensports EXITING} state from HAS_DRAWN to NO_SURFACE; reason: destroySurface这句话什么意思
03-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值