https://source.android.com/devices/graphics/architecture.html这篇文章中有这么一段话:
When an app comes to the foreground, the WindowManager service asks SurfaceFlinger for a drawing surface. SurfaceFlinger creates a "layer" - the primary component of which is a BufferQueue - for which SurfaceFlinger acts as the consumer. A Binder object for the producer side is passed through the WindowManager to the app, which can then start sending frames directly to SurfaceFlinger.
以下是对这段话的代码解释
前面这张图上最终的nativeCreate会在JNI层调用SurfaceComposerClient的createSurface函数。
SurfaceComposerClient::createSurface调用了它的成员mClient的函数createSurface。
来看看mClient是什么东东:
SurfaceComposerClient::onFirstRef函数:
void SurfaceComposerClient::onFirstRef() {
sp<ISurfaceComposer> sm(ComposerService::getComposerService());
if (sm != 0) {
sp<ISurfaceComposerClient> conn = sm->createConnection();
if (conn != 0) {
mClient = conn;
mStatus = NO_ERROR;
}
}
}
再看ComposerService::getComposerService:
/*static*/ sp<ISurfaceComposer> ComposerService::getComposerService() {
ComposerService& instance = ComposerService::getInstance();
Mutex::Autolock _l(instance.mLock);
if (instance.mComposerService == NULL) {
ComposerService::getInstance().connectLocked();
assert(instance.mComposerService != NULL);
ALOGD("ComposerService reconnected");
}
return instance.mComposerService;
}
继续找:
void ComposerService::connectLocked() {
const String16 name("SurfaceFlinger");
while (getService(name, &mComposerService) != NO_ERROR) {
usleep(250000);
}
assert(mComposerService != NULL);
.......
mComposerService就是surfaceflinger了,也就是onFirstRef里面的sm。mClient是createConnection的返回值,再去看看:
sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
{
sp<ISurfaceComposerClient> bclient;
sp<Client> client(new Client(this));
status_t err = client->initCheck();
if (err == NO_ERROR) {
bclient = client;
}
return bclient;
}
这个Client就定义在和SurfaceFliner.cpp同一个目录下的Client.cpp文件里。那么最上面的createSurface最终就是穿过binder调到了这个Client的createSurface。
Client/SurfaceFlinger里的消息队列机制就不看了,最终会调:flinger->createLayer
createDisplay的调用顺序:
SurfaceComposerClient::createDisplay->Composer::createDisplay->ComposerService.mComposerService::createDisplay->BpSurfaceComposer::createDisplay(透过Binder)->BnSurfaceComposer::onTransact(case CREATE_DISPLAY调用子类也就是SurfaceFliner)->SurfaceFlinger::createDisplay
note: ComposerService里的定义:sp<ISurfaceComposer> mComposerService
SurfaceFlinger的init函数里,有三个thread,分别解释如下:
sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync,
vsyncPhaseOffsetNs, true, "app");
mEventThread = new EventThread(vsyncSrc, *this);
sp<VSyncSource> sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync,
sfVsyncPhaseOffsetNs, true, "sf");
mSFEventThread = new EventThread(sfVsyncSrc, *this);
对于第一个和第二个EventThread,前面注释有说:The software vsync event is when SurfaceFlinger and Choreographer-based applications run each frame.
第三个线程:
mEventControlThread = new EventControlThread(this);
mEventControlThread->run("EventControl", PRIORITY_URGENT_DISPLAY);
这个线程用来enable/disable硬件的vsync信号。
还有HWC的创建:
mHwc = new HWComposer(this);
mHwc->setEventHandler(static_cast<HWComposer::EventHandler*>(this));
这说明HWC的事件会发到SurfaceFlinger来处理。
待续
博客参考: http://blog.csdn.net/mandagod/article/details/48655301