SurfaceFlinge的入口代码在frameworks/native/services/surfaceflinger/main_surfaceflinger.cpp

启动流程大体分析如下:

1 启动binder 环境 。
2 创建了一个 SurfaceFlinger 对象:

  sp<SurfaceFlinger> flinger = surfaceflinger::createSurfaceFlinger();

3 调用其 flinger->init(); 方法

4 注册为系统服务:

// publish surface flinger
    sp<IServiceManager> sm(defaultServiceManager());
    sm->addService(String16(SurfaceFlinger::getServiceName()), flinger, false,
                   IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL | IServiceManager::DUMP_FLAG_PROTO);
                   

5 注册服务SurfaceComposerAIDL:

 // publish gui::ISurfaceComposer, the new AIDL interface
    sp<SurfaceComposerAIDL> composerAIDL = new SurfaceComposerAIDL(flinger);
    sm->addService(String16("SurfaceFlingerAIDL"), composerAIDL, false,
                   IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL | IServiceManager::DUMP_FLAG_PROTO);
                   

6 注册 DisplayService();

7 调用 flinger->run();
代码是这样的:

void SurfaceFlinger::run() {
    while (true) {
        mEventQueue->waitMessage();
    }
}

无限循环,等looper的信息并处理。
mEventQueue->waitMessage() 是在等looper的信息 。

SurfaceFlinger是一个服务,是由于其实现了 BnInterface 接口,

ISurfaceComposer 位置:
https://cs.android.com/android/platform/superproject/+/master:frameworks/native/libs/gui/include/gui/ISurfaceComposer.h

ISurfaceComposer 就是其对外提供的服务的接口: defines the Binder IPC interface for accessing various SurfaceFlinger features

主要接口说明:

1 bootFinished():

  virtual void bootFinished() = 0;

被 WindowManagerService.java 的 performEnableScreen 方法所调用 ,告诉SurfaceFlinger 可以显示界面了 。

2 createConnection() : Create a connection with SurfaceFlinger.
返回的对象是 一个 ISurfaceComposerClient , 主要有接口 createSurface

以下是一个例子:

int main(int, const char**) {
	ProcessState::self()->startThreadPool();
	sp<SurfaceComposerClient> mClient = new SurfaceComposerClient;
	sp<SurfaceControl> mBGSurfaceControl;
	mClient->initCheck();
	mBGSurfaceControl = mClient->createSurface(String8("11"), 100, 100,
			PIXEL_FORMAT_RGBA_8888, 0, nullptr, LayerMetadata(), nullptr);
	Transaction().setLayer(mBGSurfaceControl, 100).show(mBGSurfaceControl).apply();
	fillSurfaceRGBA8(mBGSurfaceControl, 0, 0, 255);
	IPCThreadState::self()->joinThreadPool();
	return 0;
}

SurfaceComposerClient 的分析:

SurfaceComposerClient 在其内部有一个变量 mClient ,它是通过 surfaceflinger 服务的 createConnection 方法返回的一个客户端 ISurfaceComposerClient, 通过ISurfaceComposerClient 我们就可以 createSurface 了 。

服务端的 createSurface做了什么:

服务端的实现是 SurfaceFlinger.cpp 的 SurfaceFlinger::createLayer 函数 。 将 layer 放入 mCreatedLayers 中 。

SurfaceFlinger 的 layer 合成是在SurfaceFlinger::composite 函数中 。

VSyncCallbackRegistration(VSyncDispatch&, VSyncDispatch::Callback, std::string callbackName

VSyncCallbackRegistration :只要生成一个 VSyncCallbackRegistration 对象,参数VSyncDispatch::Callback就会在VSync事件回调的时候被执行。

frameworks/native/services/surfaceflinger/Scheduler/MessageQueue.cpp 的作用:

1 合成 layer ,

frameworks/native/services/surfaceflinger/Scheduler/Scheduler.cpp 的作用 :

frameworks/native/services/surfaceflinger/Scheduler/Scheduler.h

Scheduler 是 MessageQueue 的子类 。

而 SurfaceFlinger::run() 是在main_surfaceflinger.cpp 中启动 surfaceflinger 的最后一步,那就是进入消息循环处理了 。

Scheduler 的 run 函数的功能 : 等待新的 looper 消息的到来 。 那这个消息是来自哪里 ?是谁发过来的 ?

MessageQueue::Handler::dispatchInvalidate() 是被谁调用的 ? : MessageQueue::eventReceiver

而 MessageQueue::cb_eventReceiver 调用 MessageQueue::eventReceiver ,

然后 MessageQueue::cb_eventReceiver 是 mLooper 的 fd有新的数据时的回调函数 。

现在问题是这个 fd 的数据是哪里写入的?

EventThreadConnection::stealReceiveChannel 的功能:

一个 对象有 2个fd,一个是发送的,一个是接受的 , stealReceiveChannel 就是将这2个fd输出给客户端,然后客户端可以得到 接受的fd,放入looper的监听队列.
如下:

 mutable base::unique_fd mSendFd;
 mutable base::unique_fd mReceiveFd;

可以全局搜索 mSendFd 看是哪里写入数据的 。

frameworks/native/services/surfaceflinger/Scheduler/EventThread.cpp :
EventThread::threadMain:threadMain 函数
无限循环, 只要 mState != State::Quit,则循环读取 mPendingEvents 中的待处理数据,
如 mPendingEvents 中没有元素要处理,则等待其他地方调用 :

  mPendingEvents.push_back();
    mCondition.notify_all();

然后继续处理 。

那有哪些地方调用以上的代码呢? 至少是在 EventThread::onVSyncEvent 有调用 。

DispSyncSource::onDispSyncEvent 调用 EventThread::onVSyncEvent 。

VSyncReactor::的 callback 调用 DispSyncSource::onDispSyncEvent 。

而 VSyncReactor::的 callback 是 VSyncCallbackRegistration 的构造函数参数 。

所以我们就来分析 VSyncCallbackRegistration :

class VSyncCallbackRegistration 的功能 :

VSyncCallbackRegistration 封装了一个 callback ,可以 schedule 这个 callback 在何时执行,
也可以 cancel 掉 一个 已经schedule 但还没有 dispatch 的 callback 。

VSyncCallbackRegistration 的 schedule 函数分析 :

它调用了 VSyncDispatch 的 schedule 函数 。
谁调用了它? :

class VSyncDispatch 的功能 :

VSyncDispatch 功能分析 (VSyncDispatchTimerQueue 是 VSyncDispatch 的子类):

VSyncDispatch 的功能通过 schedule 函数是将 vsync events dispatch 给注册的 callbacks 。

在哪里注册 callback :registerCallback 函数和 unregisterCallback 函数 。

schedule 函数是怎么 dispatch 的 ?

VSyncDispatchTimerQueue 是 VSyncDispatch 的子类 , 它实现了 schedule 函数 。

VSyncDispatchTimerQueue::setTimer:
功能: 最终调用了 c 函数 timerfd_settime 在指定的时间运行 callback 函数 。
被谁调用:VSyncDispatchTimerQueue::schedule() 函数 。

继续我们的问题 :

谁调用了 VSyncDispatch 的 schedule 函数 ?

class VsyncSchedule 的功能 :

负责生成 VsyncController;VsyncDispatch 和 VsyncTracker,并对外提供。

class Scheduler的功能 :

Scheduler 是 MessageQueue 的子类。
监听的fd来自哪里?
run函数是开始等待looper的新信息并处理。
在 SurfaceFlinger ::processDisplayAdded 函数中 创建 一个 Scheduler对象。

class VSyncSource::Callback 的功能 :

有函数 virtual void onVSyncEvent(nsecs_t when, VSyncData vsyncData) = 0;
是发送VSyncEvent时的回调。
class EventThread 是它的子类。
onVSyncEvent 被 DispSyncSource::onVsyncCallback 所调用 。
而 DispSyncSource::onVsyncCallback 只用在 DispSyncSource的 函数变量 mCallbackRepeater的构造函数中 ,

而mCallbackRepeater 被调用只是在 VSyncCallbackRegistration mRegistration 的构造函数中 。

最终要 分析 EventThread::threadMain 做了什么 。

待续。。。。

所以 DispSyncSource::onVsyncCallback 调用 EventThread:: onVSyncEvent ,

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐