一、无锁状态下,灭屏后亮屏设置界面滑动无效。
问题分析:
(1)可在其他应用界面复现,非应用问题。
(2)问题必现,点击动作有效但无动画效果,滑动后界面不刷新但可确认对应位置在后台已发生变化,非输入系统错误。
(3)滑动后界面没有刷新,截图后与当前设备显示相同,排除显示系统的硬件问题,为显示系统的软件问题。
问题排查:
(1)根据绘制流程添加log分析。performTraversals(performMeasure,performLayout,performDraw),此处没有问题。
(2)分析performDraw流程添加log分析。performDraw(ViewTreeObserver.draw()/drawSoftware),发现performDraw正常运行,ViewTreeObserver.draw()在出现问题后不再调用。
(3)根据(2),继续追查问题发现ViewRootImpl.draw()中[surface.isValid()=true],进而追踪surface无效的原因。
(4)根据代码发现每次灭屏后会调用ViewRootImpl.setWindowStopped(true)进而都会mSurface.release()。
(5)亮屏由于没有锁屏界面,灭屏时的界面没有发生任何变化且非第一绘制,不会重新使能surface。
void setWindowStopped(boolean stopped) {
if (mStopped != stopped) {
mStopped = stopped;
final ThreadedRenderer renderer = mAttachInfo.mThreadedRenderer;
if (renderer != null) {
if (DEBUG_DRAW) Log.d(mTag, "WindowStopped on " + getTitle() + " set to " + mStopped);
renderer.setStopped(mStopped);
}
if (!mStopped) {
scheduleTraversals();
} else {
if (renderer != null) {
renderer.destroyHardwareResources(mView);
}
}
for (int i = 0; i < mWindowStoppedCallbacks.size(); i++) {
mWindowStoppedCallbacks.get(i).windowStopped(stopped);
}
if (mStopped) {
mSurface.release();
}
}
}
问题总结:
此问题是由于灭屏后mSurface.release(),而亮屏时没有触发重现使能surface而导致的。
二、桌面有手势按键(quickstep)的时候,出现桌面anr事件:
----- pid 10005 at 2019-01-01 12:22:46 -----
Cmd line: com.android.launcher3
"main" prio=5 tid=1 Blocked
| group="main" sCount=1 dsCount=0 flags=1 obj=0x75e5fab0 self=0x70f3a14c00
| sysTid=10005 nice=-10 cgrp=default sched=0/0 handle=0x717a1b5548
| state=S schedstat=( 481187776 24084236 354 ) utm=39 stm=8 core=1 HZ=100
| stack=0x7ffbeae000-0x7ffbeb0000 stackSize=8MB
| held mutexes=
at com.android.quickstep.NormalizedIconLoader.getBitmapInfo(NormalizedIconLoader.java:-1)
- waiting to lock <0x0b405da7> (a com.android.quickstep.NormalizedIconLoader) held by thread 10
at com.android.quickstep.NormalizedIconLoader.getDefaultIcon(NormalizedIconLoader.java:58)
- locked <0x0d1af254> (a android.util.SparseArray)
at com.android.systemui.shared.recents.model.RecentsTaskLoader.loadTaskData(RecentsTaskLoader.java:185)
at com.android.quickstep.views.RecentsView.loadVisibleTaskData(RecentsView.java:672)
at com.android.quickstep.views.RecentsView.resetTaskVisuals(RecentsView.java:567)
at com.android.quickstep.views.RecentsView.applyLoadPlan(RecentsView.java:543)
at com.android.quickstep.views.RecentsView.lambda$w02bBzSWizaR4dIzSj9kQ73I7BA(RecentsView.java:-1)
at com.android.quickstep.views.-$$Lambda$RecentsView$w02bBzSWizaR4dIzSj9kQ73I7BA.accept(lambda:-1)
at com.android.quickstep.RecentsModel.lambda$loadTasks$1(RecentsModel.java:142)
at com.android.quickstep.-$$Lambda$RecentsModel$rZAN1-eRHSYycmOWL-UzK9dzJ1Q.run(lambda:-1)
at com.android.launcher3.util.LooperExecutor.execute(LooperExecutor.java:43)
at com.android.quickstep.RecentsModel.loadTasks(RecentsModel.java:142)
at com.android.quickstep.views.RecentsView.setCurrentTask(RecentsView.java:775)
at com.android.quickstep.views.RecentsView.showTask(RecentsView.java:745)
at com.android.quickstep.WindowTransformSwipeHandler.onLauncherStart(WindowTransformSwipeHandler.java:409)
at com.android.quickstep.WindowTransformSwipeHandler.lambda$oTmdTx9KEnt4AhNDszi5oKfUOY4(WindowTransformSwipeHandler.java:-1)
at com.android.quickstep.-$$Lambda$WindowTransformSwipeHandler$oTmdTx9KEnt4AhNDszi5oKfUOY4.onActivityStart(lambda:-1)
at com.android.launcher3.BaseDraggingActivity.onStart(BaseDraggingActivity.java:235)
at com.android.launcher3.Launcher.onStart(Launcher.java:848)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1391)
at android.app.Activity.performStart(Activity.java:7157)
at android.app.ActivityThread.handleStartActivity(ActivityThread.java:2968)
at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:180)
at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:165)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:142)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1836)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6702)
at java.lang.reflect.Method.invoke(Native method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:911)
"Binder:10005_3" prio=5 tid=10 Waiting
| group="main" sCount=1 dsCount=0 flags=1 obj=0x12e04f78 self=0x70ed23c000
| sysTid=10018 nice=0 cgrp=default sched=0/0 handle=0x70debee4f0
| state=S schedstat=( 51705932 1946846 88 ) utm=2 stm=2 core=2 HZ=100
| stack=0x70deaf3000-0x70deaf5000 stackSize=1009KB
| held mutexes=
at java.lang.Object.wait(Native method)
- waiting on <0x095edfc0> (a java.lang.Object)
at java.lang.Thread.parkFor$(Thread.java:2137)
- locked <0x095edfc0> (a java.lang.Object)
at sun.misc.Unsafe.park(Unsafe.java:358)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:190)
at java.util.concurrent.FutureTask.awaitDone(FutureTask.java:450)
at java.util.concurrent.FutureTask.get(FutureTask.java:192)
at com.android.launcher3.LauncherAppState.getInstance(LauncherAppState.java:66)
at com.android.launcher3.LauncherAppState.getIDP(LauncherAppState.java:174)
at com.android.launcher3.graphics.LauncherIcons.(LauncherIcons.java:133)
at com.android.launcher3.graphics.LauncherIcons.obtain(LauncherIcons.java:91)
at com.android.quickstep.NormalizedIconLoader.getBitmapInfo(NormalizedIconLoader.java:76)
- locked <0x0b405da7> (a com.android.quickstep.NormalizedIconLoader)
at com.android.quickstep.NormalizedIconLoader.getBadgedActivityIcon(NormalizedIconLoader.java:88)
at com.android.systemui.shared.recents.model.IconLoader.createNewIconForTask(IconLoader.java:134)
at com.android.systemui.shared.recents.model.IconLoader.getAndInvalidateIfModified(IconLoader.java:94)
at com.android.systemui.shared.recents.model.RecentsTaskLoader.getAndUpdateActivityIcon(RecentsTaskLoader.java:325)
at com.android.systemui.shared.recents.model.RecentsTaskLoadPlan.executePlan(RecentsTaskLoadPlan.java:182)
at com.android.systemui.shared.recents.model.RecentsTaskLoader.loadTasks(RecentsTaskLoader.java:173)
- locked <0x0411f4f9> (a com.android.quickstep.RecentsModel$1)
at com.android.quickstep.RecentsModel.onTaskStackChangedBackground(RecentsModel.java:214)
at com.android.systemui.shared.system.TaskStackChangeListeners.onTaskStackChanged(TaskStackChangeListeners.java:79)
at android.app.ITaskStackListener$Stub.onTransact(ITaskStackListener.java:50)
at android.os.Binder.execTransact(Binder.java:731)
问题分析:
从anr文件上来看是
(1)线程"Binder:10005_3" locked <0x0b405da7> (a com.android.quickstep.NormalizedIconLoader)
(2)主线程"main" waiting to lock <0x0b405da7> (a com.android.quickstep.NormalizedIconLoader) held by thread 1
那么可以确认是由于线程"Binder:10005_3"运行时间过长而导致的UI线程出现ANR的。
进一步分析"Binder:10005_3"发现,"Binder:10005_3"是在运行LauncherAppState.getInstance耗时太长造成的,而耗时太长的原因是由于线程使用FutureTask属性在阻塞等待其他线程的唤醒,而等待的线程是那个呢?我们看下LauncherAppState.getInstance的代码:
public static LauncherAppState getInstance(final Context context) {
if (INSTANCE == null) {
if (Looper.myLooper() == Looper.getMainLooper()) {
INSTANCE = new LauncherAppState(context.getApplicationContext());
} else {
try {
return new MainThreadExecutor().submit(new Callable() {
@Override
public LauncherAppState call() throws Exception {
return LauncherAppState.getInstance(context);
}
}).get();
} catch (InterruptedException|ExecutionException e) {
throw new RuntimeException(e);
}
}
}
return INSTANCE;
}
从上面函数可以看到LauncherAppState是一个典型的单例模式,而它的创建被限制在了UI线程中,也就是说只有UI线程才能创建这个单例模式的LauncherAppState,而线程"Binder:10005_3"创建时需要用到MainThreadExecutor去阻塞的等待主线程"main"创建完成之后才能继续运行(此处使用了Future类并发编程模型)。而主线程"main"此时却在等待着线程"Binder:10005_3"一直locked的锁 <0x0b405da7>,而此行为也导致了主线程"main"的Loop流程阻塞,进而导致线程"Binder:10005_3"永远无法拿到LauncherAppState的实现。
问题流程图如下: