const sp& layer(layers[i]);
//一层一层的绘制到hw的EglSurface中,硬件合成的层不需要绘制的
layer->draw(hw, clip);
}
}
Surfaceflinnger中创建的FramebufferSurface,Bufferqueue的消费者
class FramebufferSurface : public ConsumerBase, public DisplaySurface
//ConsumerBase包装了BufferQueue的消费监听逻辑,只要重载onFrameAvailable,onBuffersReleased
//有数据要消费的时候会主动调用
ConsumerBase::ConsumerBase(const sp& consumer, bool controlledByApp ){
//监听bufferqueue是否准备好数据了
wp listener = static_cast(this);
sp proxy = new BufferQueue::ProxyConsumerListener(listener);
status_t err = mConsumer->consumerConnect(proxy, controlledByApp);
}
void ConsumerBase::onFrameAvailable() {
sp listener;
{
Mutex::Autolock lock(mMutex);
listener = mFrameAvailableListener.promote();
}
//转发到外部通知一下
if (listener != NULL) {
listener->onFrameAvailable();
}
}
void ConsumerBase::onBuffersReleased() {
Mutex::Autolock lock(mMutex);
//当buffer消费完成后,会清空本类保存的缓存数据
uint32_t mask = 0;
mConsumer->getReleasedBuffers(&mask);
for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
if (mask & (1 << i)) {
freeBufferLocked(i);
}
}
}
status_t ConsumerBase::acquireBufferLocked(BufferQueue::BufferItem *item,
nsecs_t presentWhen) {
//获取准备好的数据
status_t err = mConsumer->acquireBuffer(item, presentWhen);
if (err != NO_ERROR) {
return err;
}
//保存到当前类的缓存中
if (item->mGraphicBuffer != NULL) {
mSlots[item->mBuf].mGraphicBuffer = item->mGraphicBuffer;
}
mSlots[item->mBuf].mFrameNumber = item->mFrameNumber;
mSlots[item->mBuf].mFence = item->mFence;
return OK;
}
-------------------------------------------------------------------------------
class FramebufferSurface : public ConsumerBase,
public DisplaySurface {
void FramebufferSurface::onFrameAvailable() {//重载了这个方法,
//获取准备好的buffer
sp buf;
sp acquireFence;
status_t err = nextBuffer(buf, acquireFence);
if (err != NO_ERROR) {
return;
}
//buffer添加到HWComposer的FrameBufferTagert中
err = mHwc.fbPost(mDisplayType, acquireFence, buf);
}
status_t FramebufferSurface::nextBuffer(sp& outBuffer, sp& outFence) {
Mutex::Autolock lock(mMutex);
//获取准备好的buffer
BufferQueue::BufferItem item;
status_t err = acquireBufferLocked(&item, 0);
//保存到当前值中,
mCurrentBufferSlot = item.mBuf;
mCurrentBuffer = mSlots[mCurrentBufferSlot].mGraphicBuffer;
outFence = item.mFence;
outBuffer = mCurrentBuffer;
return NO_ERROR;
}
//DisplayDevice数据的生产者
DisplayDevice::DisplayDevice(
const sp& flinger,
DisplayType type,
int32_t hwcId,
bool isSecure,
const wp& displayToken,
const sp& displaySurface,
const sp& producer,
EGLConfig config)
{
//通过这个producer创建一个本地窗口mNativeWindow
mNativeWindow = new Surface(producer, false);
ANativeWindow* const window = mNativeWindow.get();
int format;
window->query(window, NATIVE_WINDOW_FORMAT, &format);
if (mType >= DisplayDevice::DISPLAY_VIRTUAL)
window->setSwapInterval(window, 0);
EGLSurface surface;
EGLint w, h;
EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
//本地窗口创建surface,以后操作这个surface会间接填充BufferQueue
// 需要eglSwapeBuffer(display,surface)
//
surface = eglCreateWindowSurface(display, config, window, NULL);
eglQuerySurface(display, surface, EGL_WIDTH, &mDisplayWidth);
eglQuerySurface(display, surface, EGL_HEIGHT, &mDisplayHeight);
mDisplay = display;
mSurface = surface;
mFormat = format;
mPageFlipCount = 0;
mViewport.makeInvalid();
mFrame.makeInvalid();
}