| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.wm.shell.dagger; |
| 18 |
|
| 19 |
import static android.os.Process.THREAD_PRIORITY_BACKGROUND; |
| 20 |
import static android.os.Process.THREAD_PRIORITY_DISPLAY; |
| 21 |
import static android.os.Process.THREAD_PRIORITY_TOP_APP_BOOST; |
| 22 |
|
| 23 |
import android.content.Context; |
| 24 |
import android.os.Build; |
| 25 |
import android.os.Handler; |
| 26 |
import android.os.HandlerThread; |
| 27 |
import android.os.Looper; |
| 28 |
import android.os.Trace; |
| 29 |
import android.view.Choreographer; |
| 30 |
|
| 31 |
import androidx.annotation.Nullable; |
| 32 |
|
| 33 |
import com.android.wm.shell.R; |
| 34 |
import com.android.wm.shell.common.HandlerExecutor; |
| 35 |
import com.android.wm.shell.common.ShellExecutor; |
| 36 |
import com.android.wm.shell.common.annotations.ExternalMainThread; |
| 37 |
import com.android.wm.shell.common.annotations.ShellAnimationThread; |
| 38 |
import com.android.wm.shell.common.annotations.ShellBackgroundThread; |
| 39 |
import com.android.wm.shell.common.annotations.ShellMainThread; |
| 40 |
import com.android.wm.shell.common.annotations.ShellSplashscreenThread; |
| 41 |
|
| 42 |
import dagger.Module; |
| 43 |
import dagger.Provides; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
@Module |
| 50 |
public abstract class WMShellConcurrencyModule { |
| 51 |
|
| 52 |
private static final int MSGQ_SLOW_DELIVERY_THRESHOLD_MS = 30; |
| 53 |
private static final int MSGQ_SLOW_DISPATCH_THRESHOLD_MS = 30; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public static boolean enableShellMainThread(Context context) { |
| 59 |
return context.getResources().getBoolean(R.bool.config_enableShellMainThread); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
@Provides |
| 73 |
@ExternalMainThread |
| 74 |
public static Handler provideMainHandler() { |
| 75 |
return new Handler(Looper.getMainLooper()); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
@WMSingleton |
| 82 |
@Provides |
| 83 |
@ExternalMainThread |
| 84 |
public static ShellExecutor provideSysUIMainExecutor( |
| 85 |
@ExternalMainThread Handler sysuiMainHandler) { |
| 86 |
return new HandlerExecutor(sysuiMainHandler); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public static HandlerThread createShellMainThread() { |
| 97 |
HandlerThread mainThread = new HandlerThread("wmshell.main", THREAD_PRIORITY_DISPLAY); |
| 98 |
return mainThread; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
@WMSingleton |
| 108 |
@Provides |
| 109 |
@ShellMainThread |
| 110 |
public static Handler provideShellMainHandler(Context context, |
| 111 |
@Nullable @ShellMainThread HandlerThread mainThread, |
| 112 |
@ExternalMainThread Handler sysuiMainHandler) { |
| 113 |
if (enableShellMainThread(context)) { |
| 114 |
if (mainThread == null) { |
| 115 |
|
| 116 |
mainThread = createShellMainThread(); |
| 117 |
mainThread.start(); |
| 118 |
} |
| 119 |
if (Build.IS_DEBUGGABLE) { |
| 120 |
mainThread.getLooper().setTraceTag(Trace.TRACE_TAG_WINDOW_MANAGER); |
| 121 |
mainThread.getLooper().setSlowLogThresholdMs(MSGQ_SLOW_DISPATCH_THRESHOLD_MS, |
| 122 |
MSGQ_SLOW_DELIVERY_THRESHOLD_MS); |
| 123 |
} |
| 124 |
return Handler.createAsync(mainThread.getLooper()); |
| 125 |
} |
| 126 |
return sysuiMainHandler; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
@WMSingleton |
| 133 |
@Provides |
| 134 |
@ShellMainThread |
| 135 |
public static ShellExecutor provideShellMainExecutor(Context context, |
| 136 |
@ShellMainThread Handler mainHandler, |
| 137 |
@ExternalMainThread ShellExecutor sysuiMainExecutor) { |
| 138 |
if (enableShellMainThread(context)) { |
| 139 |
return new HandlerExecutor(mainHandler); |
| 140 |
} |
| 141 |
return sysuiMainExecutor; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
@WMSingleton |
| 150 |
@Provides |
| 151 |
@ShellMainThread |
| 152 |
public static Choreographer provideShellMainChoreographer( |
| 153 |
@ShellMainThread ShellExecutor executor) { |
| 154 |
try { |
| 155 |
final Choreographer[] choreographer = new Choreographer[1]; |
| 156 |
executor.executeBlocking(() -> choreographer[0] = Choreographer.getInstance()); |
| 157 |
return choreographer[0]; |
| 158 |
} catch (InterruptedException e) { |
| 159 |
throw new RuntimeException("Failed to obtain main Choreographer.", e); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
@WMSingleton |
| 167 |
@Provides |
| 168 |
@ShellAnimationThread |
| 169 |
public static ShellExecutor provideShellAnimationExecutor() { |
| 170 |
HandlerThread shellAnimationThread = new HandlerThread("wmshell.anim", |
| 171 |
THREAD_PRIORITY_DISPLAY); |
| 172 |
shellAnimationThread.start(); |
| 173 |
if (Build.IS_DEBUGGABLE) { |
| 174 |
shellAnimationThread.getLooper().setTraceTag(Trace.TRACE_TAG_WINDOW_MANAGER); |
| 175 |
shellAnimationThread.getLooper().setSlowLogThresholdMs(MSGQ_SLOW_DISPATCH_THRESHOLD_MS, |
| 176 |
MSGQ_SLOW_DELIVERY_THRESHOLD_MS); |
| 177 |
} |
| 178 |
return new HandlerExecutor(Handler.createAsync(shellAnimationThread.getLooper())); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
@WMSingleton |
| 185 |
@Provides |
| 186 |
@ShellSplashscreenThread |
| 187 |
public static ShellExecutor provideSplashScreenExecutor() { |
| 188 |
HandlerThread shellSplashscreenThread = new HandlerThread("wmshell.splashscreen", |
| 189 |
THREAD_PRIORITY_TOP_APP_BOOST); |
| 190 |
shellSplashscreenThread.start(); |
| 191 |
return new HandlerExecutor(shellSplashscreenThread.getThreadHandler()); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
@WMSingleton |
| 198 |
@Provides |
| 199 |
@ShellBackgroundThread |
| 200 |
public static Handler provideSharedBackgroundHandler() { |
| 201 |
HandlerThread shellBackgroundThread = new HandlerThread("wmshell.background", |
| 202 |
THREAD_PRIORITY_BACKGROUND); |
| 203 |
shellBackgroundThread.start(); |
| 204 |
return shellBackgroundThread.getThreadHandler(); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
@WMSingleton |
| 211 |
@Provides |
| 212 |
@ShellBackgroundThread |
| 213 |
public static ShellExecutor provideSharedBackgroundExecutor( |
| 214 |
@ShellBackgroundThread Handler handler) { |
| 215 |
return new HandlerExecutor(handler); |
| 216 |
} |
| 217 |
} |