Android 8.0 SurfaceFlinger

一、SurfaceFlinger编译模块组成

码路径:frameworks/native/services/surfaceflinger

SurfaceFlinger二进制分成surfaceflinger(main入口)可执行文件和libsurfaceflinger.so库文件。surfaceflinger文件有main_surfaceflinger.cpp文件编译而成,为模块入口,真正实现在so文件中。

Android.mk代码模块编译配置如下:

surfaceflinger

LOCAL_SRC_FILES := \  
    main_surfaceflinger.cpp  
  
....  
LOCAL_MODULE := surfaceflinger  

libsurfaceflinger.so

LOCAL_SRC_FILES := \  
    Client.cpp \  
    ...  
    LayerRejecter.cpp \  
    ...  
    RenderEngine/GLES20RenderEngine.cpp \  
  
LOCAL_MODULE := libsurfaceflinger  
...  
ifeq ($(TARGET_USES_HWC2),true)  
    LOCAL_CFLAGS += -DUSE_HWC2  
    LOCAL_SRC_FILES += \  
        SurfaceFlinger.cpp \  
        DisplayHardware/HWComposer.cpp  
else  
    LOCAL_SRC_FILES += \  
        SurfaceFlinger_hwc1.cpp \  
        DisplayHardware/HWComposer_hwc1.cpp  
endif  
ASFDSFADF
二、SurfaceFlinger可执行二进制文件surfaceflinger
surfaceflinger可执行文件由main_surfaceflinger.cpp  文件独立编译而成,主要负责搭建进程启动环境。直接上代码:
int main(int, char**) {
    // 从8.0开始,Android提供了hidl机制,将原先直接由JNI->Native->HAL的接口调用形式,统一规范成hidl service/client交互形式。该方式从一方面规范和统一了Android Framework和HAL的调用机制,但实际上,从项目维度,这种调用方式对性能上开销,将比直接调用的方式要花费更多的时间。
    startHidlServices();

    signal(SIGPIPE, SIG_IGN);
    // When SF is launched in its own process, limit the number of
    // binder threads to 4.
    ProcessState::self()->setThreadPoolMaxThreadCount(4);

    // start the thread pool
    sp ps(ProcessState::self());
    ps->startThreadPool();
    // 初始化SurfaceFlinger对象,由强指针指向。SurfaceFlinger继承RefBase类,所以此处一旦new出对象赋给sp指针后,将立刻出发SurfaceFlinger类的onFirstRef方法的调用。
    // instantiate surfaceflinger
    sp flinger = new SurfaceFlinger();

    setpriority(PRIO_PROCESS, 0, PRIORITY_URGENT_DISPLAY);

    set_sched_policy(0, SP_FOREGROUND);

    // Put most SurfaceFlinger threads in the system-background cpuset
    // Keeps us from unnecessarily using big cores
    // Do this after the binder thread pool init
    if (cpusets_enabled()) set_cpuset_policy(0, SP_SYSTEM);
    // SurfaceFlinger类正式初始化
    // initialize before clients can connect
    flinger->init();
    // SurfaceFlinger向ServiceManager注册Binder服务,这样在其他进程中,可以通过getService+SERVICE_NAME来获取SurfaceFlinger服务,继而可以和SurfaceFlinger类进行Binder通信。
    // publish surface flinger
    sp sm(defaultServiceManager());
    sm->addService(String16(SurfaceFlinger::getServiceName()), flinger, false);

    // publish GpuService
    sp gpuservice = new GpuService();
    sm->addService(String16(GpuService::SERVICE_NAME), gpuservice, false);

    struct sched_param param = {0};
    param.sched_priority = 2;
    if (sched_setscheduler(0, SCHED_FIFO, ¶m) != 0) {
        ALOGE("Couldn't set SCHED_FIFO");
    }
    // SurfaceFlinger类进入主循环(此处注意SurfaceFlinger类未继承Threads类,不遵循Threads类的接口执行顺序)
    // run surface flinger in this thread
    flinger->run();

    return 0;
}

三、SurfaceFlinger库文件libsurfaceflinger.so

你可能感兴趣的:(Graphic)