Choreographer机制和卡顿优化

本文详细解析了Android中的Choreographer机制,指出Skipped frames并不直接意味着UI卡顿,而是可能存在过多的消息。介绍了Choreographer如何处理帧同步,以及卡顿优化的两种方案:通过Looper打印日志匹配和使用Choreographer.FrameCallback。重点讨论了如何避免主线程执行过多工作导致的延迟,并提供了理解帧间隔和Vsync信号的关键点。
摘要由CSDN通过智能技术生成

mHavePendingVsync = false;

doFrame(mTimestampNanos, mFrame);

}

这里调用的doframe方法

void doFrame(long frameTimeNanos, int frame) {

final long startNanos;

synchronized (mLock) {

if (!mFrameScheduled) {

return; // no work to do

}

if (DEBUG_JANK && mDebugPrintNextFrameTimeDelta) {

mDebugPrintNextFrameTimeDelta = false;

Log.d(TAG, "Frame time delta: "

  • ((frameTimeNanos - mLastFrameTimeNanos) * 0.000001f) + " ms");

}

long intendedFrameTimeNanos = frameTimeNanos;

startNanos = System.nanoTime();

final long jitterNanos = startNanos - frameTimeNanos;

if (jitterNanos >= mFrameIntervalNanos) {

final long skippedFrames = jitterNanos / mFrameIntervalNanos;

if (skippedFrames >= SKIPPED_FRAME_WARNING_LIMIT) {

Log.i(TAG, "Skipped " + skippedFrames + " frames! "

  • “The application may be doing too much work on its main thread.”);

}

final long lastFrameOffset = jitterNanos % mFrameIntervalNanos;

if (DEBUG_JANK) {

Log.d(TAG, "Missed vsync by " + (jitterNanos * 0.000001f) + " ms "

  • "which is more than the frame interval of "

  • (mFrameIntervalNanos * 0.000001f) + " ms! "

  • "Skipping " + skippedFrames + " frames and setting frame "

  • “time to " + (lastFrameOffset * 0.000001f) + " ms in the past.”);

}

frameTimeNanos = startNanos - lastFrameOffset;

}

if (frameTimeNanos < mLastFrameTimeNanos) {

if (DEBUG_JANK) {

Log.d(TAG, "Frame time appears to be going backwards. May be due to a "

  • “previously skipped frame. Waiting for next vsync.”);

}

scheduleVsyncLocked();

return;

}

if (mFPSDivisor > 1) {

long timeSinceVsync = frameTimeNanos - mLastFrameTimeNanos;

if (timeSinceVsync < (mFrameIntervalNanos * mFPSDivisor) && timeSinceVsync > 0) {

scheduleVsyncLocked();

return;

}

}

mFrameInfo.setVsync(intendedFrameTimeNanos, frameTimeNanos);

mFrameScheduled = false;

mLastFrameTimeNanos = frameTimeNanos;

}

try {

Trace.traceBegin(Trace.TRACE_TAG_VIEW, “Choreographer#doFrame”);

AnimationUtils.lockAnimationClock(fr

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值