Cubietruck — 20.SurfaceFlinger creation process analysis

1. The startup process of surfaceFlinger
1.Prevent system_init from starting SF
in init.rc

  1. #Set this property so surfaceflingeris not started by system_init
  2. setprop system_init.startsurfaceflinger 0

Now that it says to prevent system_init from starting surfaceFlinger, let’s see how to stop it.
in frameworks/base/cmds/system_server/library/system_init.cpp

  1. extern “C” status_t system_init()
    < /span>
  2. {
  3. char propBuf[PROPERTY_VALUE_MAX];
  4. property_get(“system_init.startsurfaceflinger”, propBuf,“1”);
  5. if (strcmp(propBuf,“1”) == 0){
  6. SurfaceFlinger::instantiate();
  7. }
  8. }

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

  1. service surfaceflinger/system/bin/surfaceflinger
  2. class main
  3. user system
    < /li>
  4. group graphics
  5. onrestart restart zygote

in frameworks/native/cmds/surfaceflinger/main_surfaceflinger.cpp

  1. intmain(int span> argc, char** argv){
  2. SurfaceFlinger::publishAndJ oinThreadPool(true);
  3. ProcessState::self( )>setThreadPoolMaxThreadCount(4);
  4. return 0;
  5. }

in frameworks/native/include/binder/BinderService.h

  1. static void publishAndJoinThreadPool(bool allowIsolated=false){
  2. sp <IServiceManager>sm(defaultServiceManager());< /span>
  3. sm>addService(String16(SERVICE::getServiceName ()), new SERVICE(), allowIsolated);
  4. ProcessState::self()>startThreadPool();
  5. IPCThreadState::self(< span style="word-wrap:break-word; color:rgb(0,0,204)">)>joinThreadPool();
  6. }

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

  1. void Surfa ceFlinger::onFirstRef()
  2. {
  3. mEventQueue< span style="word-wrap:break-word; color:rgb(0,0,204)">.init(this); //Message queue initialization
  4. run(“SurfaceFlinger”, PRIORITY_URGENT_DISPLAY); //Start working thread
  5. mReadyToRunBarrier.wait< span style="word-wrap:break-word; color:rgb(0,0,204)">(); //Wait for waiter sign
  6. }




  1. status_t SurfaceFlinger:: readyToRun()
  2. {
  3. mEGLDisplay= eglGetDisplay(EGL_DEFAULT_DISPLAY);
  4. eglInitialize(mEGLDisplay,NULL,< span style="word-wrap:break-word; color:rgb(0,0,255)">NULL );

  5. mHwc =< /span> new HWComposer(this,*static_cast<HWComposer::EventHandler *>(this));

  6. EGLint format= mHwc>getVisualID( );
  7. mEGLConfig= selectEGLConfig(mEGLDisplay, format) ;
  8. mEGLContext= createGLContext(mEGLDisplay, mEGLConfig);


  9. for (size_t i =0 ; i<DisplayDevice::NUM_DISPLAY_TYPES; i++){
    < /li>
  10. DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i)i);
  11. mDefaultDisplays[i]= new BBinder();
  12. wp<IBinder> token = MDefaultDisplays[i];

  13.         if (mHwc>isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
  14.             bool isSecure = true;
  15.             mCurrentState.displays.add(token, DisplayDeviceState(type));
  16.             sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i);
  17.             sp<S urfaceTextureClient> stc = new SurfaceTextureClient(static_cast< sp<ISurfaceTexture> >(fbs>getBufferQueue()));
  18.             sp<DisplayDevice> hw = new DisplayDevice(this, type, isSecure, token, stc, fbs, mEGLConfig);
  19.             if (> DisplayDevice::DISPLAY_PRIMARY)
  20.                 hw>acquireScreen();            
  21.             mDisplays.add(token, hw);
  22.         }
  23.     }
  24.     sp<const DisplayDevice> hw(getDefaultDisplayDevice());

  25.     DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
  26.     initializeGL(mEGLDisplay);

  27.     mEventThread = new EventThread(this);
  28.     mEventQueue.setEventThread(mEventThread);

  29.     mDrawingState = mCurrentState;

  30.     mReadyToRunBarrier.open();

  31.     initializeDisplays();

  32.     startBootAnim();

  33.     return NO_ERROR;
  34. }

a. 初始化egl
b. 新建 HWComposer
c. 新建 DisplayDevice
    

  1. DisplayDevice::DisplayDevice(const sp<SurfaceFlinger>& flinger, …)
  2. {
  3.     init(config);
  4.     mDisplayDispatcher = new DisplayDispatcher(mFlinger); //新建一个DisplayDispatcher
  5. }

  1. Set this property so surfaceflinger is not started by system_init
  2.     setprop system_init.startsurf aceflinger 0
  1. extern “C” status_t system_init()
  2. {
  3.     char propBuf[PROPERTY_VAL UE_MAX];
  4.     property_get(“system_init.startsurfaceflinger”, propBuf, “1”);
  5.     if (strcmp(propBuf, “1”) == 0) {
  6.         SurfaceFlinger::instantiate( );
  7.     } 
  8. }
  1. service surfaceflinger /system/bin/surfaceflinger
  2.     class main
  3.     user system
  4.     group graphics
  5.     onrestart restart zygote
  1. int main(int argc, char** argv) {
  2.     SurfaceFlinger::publishAndJoinThreadPool(true);
  3.     ProcessState::self()>setThreadPoolMaxThreadCount(4);
  4.     return 0;
  5. }
  1. static void publishAndJoinThreadPool(bool allowIsolated = false) {
  2.     sp<IServiceManager> sm(defaultServiceManager());
  3.     sm>addService(String16(SERVICE::getServiceName()), new SERVICE(), allowIsolated);
  4.     ProcessState::self()>startThreadPool();
  5.     IPCThreadState::self()>joinThreadPool();
  6. }
  1. void SurfaceFlinger::onFirstRef()
  2. {
  3.     mEventQueue.init(this);              //消息队列初始化
  4.     run(“SurfaceFlinger”, PRIORITY_URGENT_DISPLAY);  //启动工作线程
  5.     mReadyToRunBarrier.wait(< span style="word-wrap:break-word; color:rgb(0,0,204)">);   //等侍标志
  6. }
  1. status_t SurfaceFlinger::readyToRun()
  2. {
  3.     mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
  4.     eglInitialize(mEGLDisplay, NULL , NULL);

  5.     mHwc = new HWComposer(this, *static_cast<HWComposer::E ventHandler *>(this));

  6.     EGLint format = mHwc>getVisualID();
  7.     mEGLConfig = selectEGLConfig(mEGLDisplay, format);
  8.     mEGLContext = createGLContext(mEGLDisplay, mEGLConfig);


  9.     for (size_t i=; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
  10.         DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
  11.         mDefaultDisplays[i] = new BBinder();
  12.         wp<IBinder> token = mDefaultDisplays[i];

  13.         if (mHwc>isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
  14.             bool isSecure = true;
  15.             mCurrentState.dis plays.add(token, DisplayDeviceState(type));
  16.             sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc< span style="word-wrap:break-word; color:rgb(0,0,204)">, i);
  17.             sp<SurfaceTextureClient> stc = new SurfaceTextureClient(static_cast< sp<ISurfaceTexture> >(fbs>getBufferQueue()));
  18.             sp<DisplayDevice> hw = new DisplayDevice(this, type, isSecure, token, stc, fbs, mEGLConfig);
  19.             if (> DisplayDevice::DISPLAY_PRIMARY)
  20.                 hw>acquireScreen();            
  21.             mDisplays.add(token, hw);
  22.         }
  23.     }
  24.     sp<const DisplayDevice> hw(getDefaultDisplayDevice());

  25.     DisplayDevice::makeCurrent(mEGLDisplay, hw, mEGLContext);
  26.     initializeGL(mEGLDisplay);

  27.     mEventThread = new EventThread(this);
  28.     mEventQueue.setEventThread(mEventThread);

  29.     mDrawingState = mCurrentState;

  30.     mReadyToRunBarrier.open();

  31.     initializeDisplays();

  32.     startBootAnim();

  33.     return NO_ERROR;
  34. }
  1. DisplayDevice::DisplayDevice(const sp<SurfaceFlinger>& flinger, …)
  2. {
  3.     init(config);
  4.     mDisplayDispatcher = new DisplayDispatcher(mFlinger); //新建一个DisplayDispatcher
  5. }

Leave a Comment

Your email address will not be published.