bool BufferStateLayer::setBuffer(const std::shared_ptr<renderengine::ExternalTexture>& buffer,
const sp<Fence>& acquireFence, nsecs_t postTime,
nsecs_t desiredPresentTime, bool isAutoTimestamp,
const client_cache_t& clientCacheId, uint64_t frameNumber,
std::optional<nsecs_t> dequeueTime, const FrameTimelineInfo& info,
const sp<ITransactionCompletedListener>& releaseBufferListener,
const sp<IBinder>& releaseBufferEndpoint) {
if (mDrawingState.buffer) {
mReleasePreviousBuffer = true;
if (mDrawingState.buffer != mBufferInfo.mBuffer ||
mDrawingState.frameNumber != mBufferInfo.mFrameNumber) {
// If mDrawingState has a buffer, and we are about to update again
// before swapping to drawing state, then the first buffer will be
// dropped and we should decrement the pending buffer count and
// call any release buffer callbacks if set.
callReleaseBufferCallback(mDrawingState.releaseBufferListener,
mDrawingState.buffer->getBuffer(), mDrawingState.frameNumber,
mDrawingState.acquireFence, mTransformHint,
mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(
mOwnerUid));
decrementPendingBufferCount();
if (mDrawingState.bufferSurfaceFrameTX != nullptr &&
mDrawingState.bufferSurfaceFrameTX->getPresentState() != PresentState::Presented) {
addSurfaceFrameDroppedForBuffer(mDrawingState.bufferSurfaceFrameTX);
mDrawingState.bufferSurfaceFrameTX.reset();
}
}
}
- 如果当前
mDrawingState.buffer
不空,执行下面操作。
mReleasePreviousBuffer
设置为 true,在 BufferStateLayer::setTransactionCompletedListeners() 中会把该值设置给 CallbackHandle 的成员 releasePreviousBuffer
。
- 如果
mDrawingState.buffer
和 mBufferInfo.mBuffer
不相同,或 frameNumber
不相同,说明当前 mBufferInfo
还未显示 mDrawingState.buffer
已经更新了,所以要调用 callReleaseBufferCallback()
通知 client,释放了 mDrawingState
中的 buffer。正常情况下不会执行该分支。
mDrawingState.frameNumber = frameNumber;
mDrawingState.releaseBufferListener = releaseBufferListener;
mDrawingState.buffer = buffer;
mDrawingState.clientCacheId = clientCacheId;
mDrawingState.modified = true;
setTransactionFlags(eTransactionNeeded);
...
mDrawingState.width = mDrawingState.buffer->getBuffer()->getWidth();
mDrawingState.height = mDrawingState.buffer->getBuffer()->getHeight();
mDrawingState.releaseBufferEndpoint = releaseBufferEndpoint;
return true;
}
- 略过了 frame event 和 frame trace 相关的代码。
- 设置
width
和 height
。
- 设置
releaseBufferEndpoint
。
- 其实
releaseBufferEndpoint
和 releaseBufferListener
是同一个对象 binder,只是类型不同,前者是 sp<IBinder>
,后者是 sp<ITransactionCompletedListener>
。
- 返回 true,该函数只返回 true。