1. The startup process of surfaceFlinger
1.Prevent system_init from starting SF
in init.rc
- #Set this property so surfaceflingeris not started by system_init
- setprop system_init.startsurfaceflinger 0
Now that it says to prevent system_init from starting surfaceFlinger, let’s see how to stop it. span>
in frameworks/base/cmds/system_server/library/system_init.cpp
- extern “C” status_t system_init()
< /span> - {
- char propBuf[PROPERTY_VALUE_MAX];
- property_get(“system_init.startsurfaceflinger”, propBuf,“1”);
- if (strcmp(propBuf,“1”) == 0){
- SurfaceFlinger::instantiate();
- }
- }
Get system_init.startsurfaceflinger The value of this attribute, if If it is 1, start sf.
But if this attribute value is set to 0 in init.rc, this will not start sf.
2. SF’s real startup process
still in init.rc
- service surfaceflinger/system/bin/surfaceflinger
- class main
- user system
< /li> - group graphics
- onrestart restart zygote
in frameworks/native/cmds/surfaceflinger/main_surfaceflinger.cpp
- intmain(int span> argc, char** argv){
- SurfaceFlinger::publishAndJ oinThreadPool(true);
- ProcessState::self( )–>setThreadPoolMaxThreadCount(4);
- return 0;
- }
in frameworks/native/include/binder/BinderService.h
- static void publishAndJoinThreadPool(bool allowIsolated=false){
- sp <IServiceManager>sm(defaultServiceManager());< /span>
- sm–>addService(String16(SERVICE::getServiceName ()), new SERVICE(), allowIsolated);
- ProcessState: span>:self()–
> startThreadPool();
- IPCThreadState::self(< span style="word-wrap:break-word; color:rgb(0,0,204)">) –>joinThreadPool();
- }
The new SERVICE is a new surfaceflinger, enter the constructor of surfaceflinger < br style="word-wrap:break-word; color:rgb(102,102,102); font-family:宋体,Arial; font-size:16px; line-height:26px"> 3. onFirstRef starts the main thread
Because SurfaceFlinger inherits from class Thread, Thread inherits from RefBase
In frameworks/native/include/utils/Thread.h, class Thread: virtual public RefBase
- void Surfa ceFlinger::onFirstRef()
- {
- mEventQueue< span style="word-wrap:break-word; color:rgb(0,0,204)">.init(this); //Message queue initialization
- run(“SurfaceFlinger”, PRIORITY_URGENT_DISPLAY); //Start working thread
- mReadyToRunBarrier.wait< span style="word-wrap:break-word; color:rgb(0,0,204)">(); //Wait for waiter sign
- }
- status_t SurfaceFlinger:: readyToRun()
- {
- mEGLDisplay= eglGetDisplay
(EGL_DEFAULT_DISPLAY);
li>- eglInitialize(mEGLDisplay,NULL,< span style="word-wrap:break-word; color:rgb(0,0,255)">NULL );
- mHwc =< /span> new HWComposer(this,*static_cast<HWComposer::EventHandler *>(this));
- EGLint format= mHwc–>getVisualID( );
- mEGLConfig= selectEGLConfig(mEGLDisplay, format) ;
- mEGLContext= createGLContext(mEGLDisplay, mEGLConfig);
- for (size_t i =0 ; i<DisplayDevice::NUM_DISPLAY_TYPES; i++){
< /li>- DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i)i) span>;
- mDefaultDisplays[i]
= new BBinder();
- wp<IBinder> token = MDefaultDisplays[i];
- if (mHwc–>isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
- bool isSecure = true;
- mCurrentState.displays.add(token, DisplayDeviceState(type));
- sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i);
- sp<S urfaceTextureClient> stc = new SurfaceTextureClient(static_cast< sp<ISurfaceTexture> >(fbs–>getBufferQueue()));
- sp<DisplayDevice> hw = new DisplayDevice(this, type, isSecure, token, stc, fbs, mEGLConfig);
- if (i > DisplayDevice::DISPLAY_PRIMARY)
- hw–>acquireScreen();
- mDisplays.add(token, hw);
- }
- }
- sp<const DisplayDevice> hw(getDefaultDisplayDevice());
- DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
- initializeGL(mEGLDisplay);
- mEventThread = new EventThread(this);
- mEventQueue.setEventThread(mEventThread);
- mDrawingState = mCurrentState;
- mReadyToRunBarrier.open();
- initializeDisplays();
- startBootAnim();
- return NO_ERROR;
- }
a. 初始化egl
b. 新建 HWComposer
c. 新建 DisplayDevice
- DisplayDevice::DisplayDevice(const sp<SurfaceFlinger>& flinger, …)
- {
- init(config);
- mDisplayDispatcher = new DisplayDispatcher(mFlinger); //新建一个DisplayDispatcher
- }
- # Set this property so surfaceflinger is not started by system_init
- setprop system_init.startsurf aceflinger 0
- extern “C” status_t system_init()
- {
- char propBuf[PROPERTY_VAL UE_MAX];
- property_get(“system_init.startsurfaceflinger”, propBuf, “1”);
- if (strcmp(propBuf, “1”) == 0) {
- SurfaceFlinger::instantiate( );
- }
- }
- service surfaceflinger /system/bin/surfaceflinger
- class main
- user system
- group graphics
- onrestart restart zygote
- int main(int argc, char** argv) {
- SurfaceFlinger::publishAndJoinThreadPool(true);
- ProcessState::self()–>setThreadPoolMaxThreadCount(4);
- return 0;
- }
- static void publishAndJoinThreadPool(bool allowIsolated = false) {
- sp<IServiceManager> sm(defaultServiceManager());
- sm–>addService(String16(SERVICE::getServiceName()), new SERVICE(), allowIsolated);
- ProcessState::self()–>startThreadPool();
- IPCThreadState::self()–>joinThreadPool();
- }
- void SurfaceFlinger::onFirstRef()
- {
- mEventQueue.init(this); //消息队列初始化
- run(“SurfaceFlinger”, PRIORITY_URGENT_DISPLAY); //启动工作线程
- mReadyToRunBarrier.wait(< span style="word-wrap:break-word; color:rgb(0,0,204)">); //等侍标志
- }
- status_t SurfaceFlinger::readyToRun()
- {
- mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
- eglInitialize(mEGLDisplay, NULL , NULL);
-
- mHwc = new HWComposer(this, *static_cast<HWComposer::E ventHandler *>(this));
-
- EGLint format = mHwc–>getVisualID();
- mEGLConfig = selectEGLConfig(mEGLDisplay, format);
- mEGLContext = createGLContext(mEGLDisplay, mEGLConfig);
-
-
- for (size_t i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
- DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
- mDefaultDisplays[i] = new BBinder();
- wp<IBinder> token = mDefaultDisplays[i];
-
- if (mHwc–>isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
- bool isSecure = true;
- mCurrentState.dis plays.add(token, DisplayDeviceState(type));
- sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc< span style="word-wrap:break-word; color:rgb(0,0,204)">, i);
- sp<SurfaceTextureClient> stc = new SurfaceTextureClient(static_cast< sp<ISurfaceTexture> >(fbs
–>getBufferQueue()));
- sp<DisplayDevice> hw = new DisplayDevice(this, type, isSecure, token, stc, fbs, mEGLConfig);
- if (i > DisplayDevice::DISPLAY_PRIMARY)
- hw–>acquireScreen();
- mDisplays.add(token, hw
);
- }
- }
- sp<const DisplayDevice> hw(getDefaultDisplayDevice());
-
- DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
- initializeGL(mEGLDisplay);
-
- mEventThread = new EventThread(this);
- mEventQueue.setEventThread(mEventThread);
-
- mDrawingState = mCurrentState;
-
- mReadyToRunBarrier.open();
-
- initializeDisplays();
-
- startBootAnim();
-
- return NO_ERROR;
- }
- DisplayDevice::DisplayDevice(const sp<SurfaceFlinger>& flinger, …)
- {
- init(config);
- mDisplayDispatcher = new DisplayDispatcher(mFlinger); //新建一个DisplayDispatcher
- }