SurfaceFlinger::init初始化的时候,调用registerCallback通过hwbinder向hwcomposer进程注册回调,注册完成会调用SurfaceFlinger::onHotplugReceived,processDisplayHotplugEventsLocked收到DisplayDevice Connected事件,setupNewDisplayDeviceInternal调用nativeWindowSurface->preallocateBuffers(),然后调用为surface->allocateBuffers(),然后调用生产者mGraphicBufferProducer的allocateBuffers,在BufferQueueProducer::allocateBuffers分配GraphicBuffer。new GraphicBuffer,调用到GraphicBuffer::initWithSize,调用GraphicBufferAllocator::allocate,调用Allocator::allocate,此时mAllocator->allocate会通过hwbinder调用到grallocator进程的具体调用流程如下:
代码文件路径:frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp
void SurfaceFlinger::init() {
......
getBE().mRenderEngine =
RE::impl::RenderEngine::create(HAL_PIXEL_FORMAT_RGBA_8888, hasWideColorDisplay? RE::RenderEngine::WIDE_COLOR_SUPPORT: 0);
LOG_ALWAYS_FATAL_IF(getBE().mRenderEngine == nullptr, "couldn't create RenderEngine");LOG_ALWAYS_FATAL_IF(mVrFlingerRequestsDisplay,
"Starting with vr flinger active is not currently supported.");
getBE().mHwc.reset(
new HWComposer(std::make_unique<Hwc2::impl::Composer>(getBE().mHwcServiceName)));
getBE().mHwc->registerCallback(this, getBE().mComposerSequenceId); //调用registerCallback通过hwbinder向hwcomposer进程注册回调, 注册的对象为SurfaceFlinger// Process any initial hotplug and resulting display changes.
processDisplayHotplugEventsLocked();
LOG_ALWAYS_FATAL_IF(!getBE().mHwc->isConnected(HWC_DISPLAY_PRIMARY),
"Registered composer callback but didn't create the default primary display");// make the default display GLContext current so that we can create textures
// when creating Layers (which may happens before we render something)
getDefaultDisplayDeviceLocked()->makeCurrent();
......
}
注册完成会调用SurfaceFlinger::onHotplugReceived
void SurfaceFlinger::onHotplugReceived(int32_t sequenceId, hwc2_display_t display, HWC2::Connection connection)
{
ConditionalLock lock(mStateLock, std::this_thread::get_id() != mMainThreadId);
mPendingHotplugEvents.emplace_back(HotplugEvent{display, connection});
if (std::this_thread::get_id() == mMainThreadId) {
processDisplayHotplugEventsLocked();
}
setTransactionFlags(eDisplayTransactionNeeded);
}
}
void SurfaceFlinger::processDisplayHotplugEventsLocked() {
processDisplayChangesLocked();
}