Android为了增强界面流畅度引入了VSync机制。VSync信号一般由硬件产生,为了兼容一些老的机器,也可以软件产生。因些,就可以分为硬件VSync和软件VSync。VSync的生成逻辑主要在HWComposer里面。相关文件如下:
- frameworks/native/services/surfaceflinger/DisplayHardware/HWComposer.cpp
- frameworks/native/services/surfaceflinger/DisplayHardware/HWComposer.h
HWComposer在SurfaceFlinger的init函数里创建:
void SurfaceFlinger::init() {
ALOGI( "SurfaceFlinger's main thread ready to run. "
"Initializing graphics H/W...");
......
// Initialize the H/W composer object. There may or may not be an
// actual hardware composer underneath.
mHwc = new HWComposer(this,
*static_cast<HWComposer::EventHandler *>(this));
......
}
- SurfaceFlinger成员变量保存了HWComposer对象指针
- 创建HWComposer时,使用SurfaceFlinger作为参数传入
HWComposer::HWComposer(
const sp<SurfaceFlinger>& flinger,
EventHandler& handler)
: mFlinger(flinger),
mFbDev(0), mHwc(0), mNumDisplays(1),
mCBContext(new cb_context),
mEventHandler(handler),
mDebugForceFakeVSync(false)
{
......
bool needVSyncThread = true;
// Note: some devices may insist that the FB HAL be opened before HWC.
int fberr = loadFbHalModule();
loadHwcModule();
.......
if (mHwc) {