目录

1. SurfaceFlinger程序入口

/frameworks/native/services/surfaceflinger/main_surfaceflinger.cpp

1int main(int, char**) { 2    signal(SIGPIPE, SIG_IGN); 3  4    hardware::configureRpcThreadpool(1 /* maxThreads */, 5            false /* callerWillJoin */); 6  7    startGraphicsAllocatorService(); 8  9    // When SF is launched in its own process, limit the number of 10    // binder threads to 4. 11    ProcessState::self()->setThreadPoolMaxThreadCount(4); 12  13    // Set uclamp.min setting on all threads, maybe an overkill but we want 14    // to cover important threads like RenderEngine. 15    if (SurfaceFlinger::setSchedAttr(true) != NO_ERROR) { 16        ALOGW("Couldn't set uclamp.min: %s\n", strerror(errno)); 17    } 18  19    // The binder threadpool we start will inherit sched policy and priority 20    // of (this) creating thread. We want the binder thread pool to have 21    // SCHED_FIFO policy and priority 1 (lowest RT priority) 22    // Once the pool is created we reset this thread's priority back to 23    // original. 24    int newPriority = 0; 25    int origPolicy = sched_getscheduler(0); 26    struct sched_param origSchedParam; 27  28    int errorInPriorityModification = sched_getparam(0, &origSchedParam); 29    if (errorInPriorityModification == 0) { 30        int policy = SCHED_FIFO; 31        newPriority = sched_get_priority_min(policy); 32  33        struct sched_param param; 34        param.sched_priority = newPriority; 35  36        errorInPriorityModification = sched_setscheduler(0, policy, &param); 37    } 38  39    // start the thread pool 40    sp<ProcessState> ps(ProcessState::self()); 41    ps->startThreadPool(); 42  43    // Reset current thread's policy and priority 44    if (errorInPriorityModification == 0) { 45        errorInPriorityModification = sched_setscheduler(0, origPolicy, &origSchedParam); 46    } else { 47        ALOGE("Failed to set SurfaceFlinger binder threadpool priority to SCHED_FIFO"); 48    } 49  50    // instantiate surfaceflinger 51    sp<SurfaceFlinger> flinger = surfaceflinger::createSurfaceFlinger(); 52  53    // Set the minimum policy of surfaceflinger node to be SCHED_FIFO. 54    // So any thread with policy/priority lower than {SCHED_FIFO, 1}, will run 55    // at least with SCHED_FIFO policy and priority 1. 56    if (errorInPriorityModification == 0) { 57        flinger->setMinSchedulerPolicy(SCHED_FIFO, newPriority); 58    } 59  60    setpriority(PRIO_PROCESS, 0, PRIORITY_URGENT_DISPLAY); 61  62    set_sched_policy(0, SP_FOREGROUND); 63  64    // Put most SurfaceFlinger threads in the system-background cpuset 65    // Keeps us from unnecessarily using big cores 66    // Do this after the binder thread pool init 67    if (cpusets_enabled()) set_cpuset_policy(0, SP_SYSTEM); 68  69    // initialize before clients can connect 70    flinger->init(); 71  72    // publish surface flinger 73    sp<IServiceManager> sm(defaultServiceManager()); 74    sm->addService(String16(SurfaceFlinger::getServiceName()), flinger, false, 75                   IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL | IServiceManager::DUMP_FLAG_PROTO); 76  77    startDisplayService(); // dependency on SF getting registered above 78  79    if (SurfaceFlinger::setSchedFifo(true) != NO_ERROR) { 80        ALOGW("Couldn't set to SCHED_FIFO: %s", strerror(errno)); 81    } 82  83    // run surface flinger in this thread 84    flinger->run();/ Use StartPropertySetThread instead. 85} 86  87  88void SurfaceFlinger::onFirstRef() { 89    // std::unique_ptr<MessageQueue> mEventQueue; 90    mEventQueue->init(this); 91} 92  93void MessageQueue::init(const sp<SurfaceFlinger>& flinger) { 94    mFlinger = flinger; 95    mLooper = new Looper(true); 96    mHandler = new Handler(*this); 97}

main方法是surfaceflinger的入口。该方法中关注到sp<SurfaceFlinger> flinger = surfaceflinger::createSurfaceFlinger()。这里创建了surfaceFlinger,实际上这里的sp隐含了一个方法调用——onFirstRef()。这个方法有点特殊,在代码层面看不到调用关系。实际上它是当强指针(sp-strong point)第一次被引用时,会被调用。继续可以看到在onFirstRef中执行了mEventQueue的init方法。继续看这个init方法,实际上创建了Looer和handler两个成员。这连个成员的作用是循环接受和发送消息,处理消息的作用。

注意到flinger→init()方法。该方法中调用mCompositionEngine->setRenderEngine(renderengine::RenderEngine::create初始化了renderEngine。renderEngine是用来做软件合成的。

1// Use StartPropertySetThread instead. 2void SurfaceFlinger::init() { 3ALOGI( "SurfaceFlinger's main thread ready to run. " 4"Initializing graphics H/W..."); 5Mutex::Autolock _l(mStateLock); 6  7  8  9// Get a RenderEngine for the given display / config (can't fail) 10// TODO(b/77156734): We need to stop casting and use HAL types when possible. 11// Sending maxFrameBufferAcquiredBuffers as the cache size is tightly tuned to single-display. 12mCompositionEngine->setRenderEngine(renderengine::RenderEngine::create( 13renderengine::RenderEngineCreationArgs::Builder() 14.setPixelFormat(static_cast<int32_t>(defaultCompositionPixelFormat)) 15.setImageCacheSize(maxFrameBufferAcquiredBuffers) 16.setUseColorManagerment(useColorManagement) 17.setEnableProtectedContext(enable_protected_contents(false)) 18.setPrecacheToneMapperShaderOnly(false) 19.setSupportsBackgroundBlur(mSupportsBlur) 20.setContextPriority( 21useContextPriority 22? renderengine::RenderEngine::ContextPriority::REALTIME 23: renderengine::RenderEngine::ContextPriority::MEDIUM) 24.build()));

最后调用flinger->run()方法,进入消息等待中。