void BufferStateLayer::onLayerDisplayed(const sp<Fence>& releaseFence) {
if (!releaseFence->isValid()) {
return;
}
// The previous release fence notifies the client that SurfaceFlinger is done with the previous
// buffer that was presented on this layer. The first transaction that came in this frame that
// replaced the previous buffer on this layer needs this release fence, because the fence will
// let the client know when that previous buffer is removed from the screen.
//
// Every other transaction on this layer does not need a release fence because no other
// Transactions that were set on this layer this frame are going to have their preceeding buffer
// removed from the display this frame.
//
// For example, if we have 3 transactions this frame. The first transaction doesn't contain a
// buffer so it doesn't need a previous release fence because the layer still needs the previous
// buffer. The second transaction contains a buffer so it needs a previous release fence because
// the previous buffer will be released this frame. The third transaction also contains a
// buffer. It replaces the buffer in the second transaction. The buffer in the second
// transaction will now no longer be presented so it is released immediately and the third
// transaction doesn't need a previous release fence.
sp<CallbackHandle> ch;
for (auto& handle : mDrawingState.callbackHandles) {
if (handle->releasePreviousBuffer &&
mDrawingState.releaseBufferEndpoint == handle->listener) {
ch = handle;
break;
}
}
auto status = addReleaseFence(ch, releaseFence);
if (status != OK) {
ALOGE("Failed to add release fence for layer %s", getName().c_str());
}
mPreviousReleaseFence = releaseFence;
// Prevent tracing the same release multiple times.
if (mPreviousFrameNumber != mPreviousReleasedFrameNumber) {
mPreviousReleasedFrameNumber = mPreviousFrameNumber;
}
}
- 如果
releaseFence
无效,返回。
mPreviousReleaseFence
用于通知 client,上一个 buffer 已经显示过了。
- 注释说明,只有当前帧的第一个要替换上一个已经显示的 buffer 的事务才需要 release fence,其他的事务不需要。
- 遍历 callbackHandles,查找
releasePreviousBuffer
是 true,且 releaseBufferEndPoint 和 listener 相同的 CallbackHandle。
- 调用 BufferStateLayer::addReleaseFence() 设置
previousReleaseFence
。
- 同时设置
mPreviousReleaseFence
,它是当前正在显示的 buffer 的 fence。
- 更新
mPreviousReleasedFrameNumber
。