1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17 package com.android.server.wm; 18 19 import android.content.ComponentName; 20 import android.os.Process; 21 import android.service.vr.IPersistentVrStateCallbacks; 22 import android.util.Slog; 23 import android.util.proto.ProtoOutputStream; 24 import android.util.proto.ProtoUtils; 25 26 import com.android.server.LocalServices; 27 import com.android.server.am.ActivityManagerService; 28 import com.android.server.am.ProcessList; 29 import com.android.server.am.VrControllerProto; 30 import com.android.server.vr.VrManagerInternal; 31 32 /** 33 * Helper class for {@link ActivityManagerService} responsible for VrMode-related ActivityManager 34 * functionality. 35 * 36 * <p>Specifically, this class is responsible for: 37 * <ul> 38 * <li>Adjusting the scheduling of VR render threads while in VR mode. 39 * <li>Handling ActivityManager calls to set a VR or a 'persistent' VR thread. 40 * <li>Tracking the state of ActivityManagerService's view of VR-related behavior flags. 41 * </ul> 42 * 43 * <p>This is NOT the class that manages the system VR mode lifecycle. The class responsible for 44 * handling everything related to VR mode state changes (e.g. the lifecycles of the associated 45 * VrListenerService, VrStateCallbacks, VR HAL etc.) is VrManagerService. 46 * 47 * <p>This class is exclusively for use by ActivityManagerService. Do not add callbacks or other 48 * functionality to this for things that belong in VrManagerService. 49 */ 50 final class VrController { 51 private static final String TAG = "VrController"; 52 53 // VR state flags. 54 private static final int FLAG_NON_VR_MODE = 0; 55 private static final int FLAG_VR_MODE = 1; 56 private static final int FLAG_PERSISTENT_VR_MODE = 2; 57 58 // Keep the enum lists in sync 59 private static int[] ORIG_ENUMS = new int[] { 60 FLAG_NON_VR_MODE, 61 FLAG_VR_MODE, 62 FLAG_PERSISTENT_VR_MODE, 63 }; 64 private static int[] PROTO_ENUMS = new int[] { 65 VrControllerProto.FLAG_NON_VR_MODE, 66 VrControllerProto.FLAG_VR_MODE, 67 VrControllerProto.FLAG_PERSISTENT_VR_MODE, 68 }; 69 70 // Invariants maintained for mVrState 71 // 72 // Always true: 73 // - Only a single VR-related thread will have elevated scheduling priorities at a time 74 // across all threads in all processes (and for all possible running modes). 75 // 76 // Always true while FLAG_PERSISTENT_VR_MODE is set: 77 // - An application has set a flag to run in persistent VR mode the next time VR mode is 78 // entered. The device may or may not be in VR mode. 79 // - mVrState will contain FLAG_PERSISTENT_VR_MODE 80 // - An application may set a persistent VR thread that gains elevated scheduling 81 // priorities via a call to setPersistentVrThread. 82 // - Calls to set a regular (non-persistent) VR thread via setVrThread will fail, and 83 // thread that had previously elevated its scheduling priority in this way is returned 84 // to its normal scheduling priority. 85 // 86 // Always true while FLAG_VR_MODE is set: 87 // - The current top application is running in VR mode. 88 // - mVrState will contain FLAG_VR_MODE 89 // 90 // While FLAG_VR_MODE is set without FLAG_PERSISTENT_VR_MODE: 91 // - The current top application may set one of its threads to run at an elevated 92 // scheduling priority via a call to setVrThread. 93 // 94 // While FLAG_VR_MODE is set with FLAG_PERSISTENT_VR_MODE: 95 // - The current top application may NOT set one of its threads to run at an elevated 96 // scheduling priority via a call to setVrThread (instead, the persistent VR thread will 97 // be kept if an application has set one). 98 // 99 // While mVrState == FLAG_NON_VR_MODE: 100 // - Calls to setVrThread will fail. 101 // - Calls to setPersistentVrThread will fail. 102 // - No threads will have elevated scheduling priority for VR. 103 // 104 private volatile int mVrState = FLAG_NON_VR_MODE; 105 106 // The single VR render thread on the device that is given elevated scheduling priority. 107 private int mVrRenderThreadTid = 0; 108 109 private final Object mGlobalAmLock; 110 111 private final IPersistentVrStateCallbacks mPersistentVrModeListener = 112 new IPersistentVrStateCallbacks.Stub() { 113 @Override 114 public void onPersistentVrStateChanged(boolean enabled) { 115 synchronized(mGlobalAmLock) { 116 // Note: This is the only place where mVrState should have its 117 // FLAG_PERSISTENT_VR_MODE setting changed. 118 if (enabled) { 119 setVrRenderThreadLocked(0, ProcessList.SCHED_GROUP_TOP_APP, true); 120 mVrState |= FLAG_PERSISTENT_VR_MODE; 121 } else { 122 setPersistentVrRenderThreadLocked(0, true); 123 mVrState &= ~FLAG_PERSISTENT_VR_MODE; 124 } 125 } 126 } 127 }; 128 129 /** 130 * Create new VrController instance. 131 * 132 * @param globalAmLock the global ActivityManagerService lock. 133 */ VrController(final Object globalAmLock)134 public VrController(final Object globalAmLock) { 135 mGlobalAmLock = globalAmLock; 136 } 137 138 /** 139 * Called when ActivityManagerService receives its systemReady call during boot. 140 */ onSystemReady()141 public void onSystemReady() { 142 VrManagerInternal vrManagerInternal = LocalServices.getService(VrManagerInternal.class); 143 if (vrManagerInternal != null) { 144 vrManagerInternal.addPersistentVrModeStateListener(mPersistentVrModeListener); 145 } 146 } 147 148 /** 149 * Called without lock to determine whether to call {@link #onTopProcChangedLocked} in lock. It 150 * is used to optimize performance for the path that may have lock contention frequently. 151 */ isInterestingToSchedGroup()152 boolean isInterestingToSchedGroup() { 153 return (mVrState & (FLAG_VR_MODE | FLAG_PERSISTENT_VR_MODE)) != 0; 154 } 155 156 /** 157 * Called when ActivityManagerService's TOP_APP process has changed. 158 * 159 * <p>Note: This must be called with the global ActivityManagerService lock held. 160 * 161 * @param proc is the WindowProcessController of the process that entered or left the TOP_APP 162 * scheduling group. 163 */ onTopProcChangedLocked(WindowProcessController proc)164 public void onTopProcChangedLocked(WindowProcessController proc) { 165 final int curSchedGroup = proc.getCurrentSchedulingGroup(); 166 if (curSchedGroup == ProcessList.SCHED_GROUP_TOP_APP) { 167 setVrRenderThreadLocked(proc.mVrThreadTid, curSchedGroup, true); 168 } else { 169 if (proc.mVrThreadTid == mVrRenderThreadTid) { 170 clearVrRenderThreadLocked(true); 171 } 172 } 173 } 174 175 /** 176 * Called when ActivityManagerService is switching VR mode for the TOP_APP process. 177 * 178 * @param record the ActivityRecord of the activity changing the system VR mode. 179 * @return {@code true} if the VR state changed. 180 */ onVrModeChanged(ActivityRecord record)181 public boolean onVrModeChanged(ActivityRecord record) { 182 // This message means that the top focused activity enabled VR mode (or an activity 183 // that previously set this has become focused). 184 VrManagerInternal vrService = LocalServices.getService(VrManagerInternal.class); 185 if (vrService == null) { 186 // VR mode isn't supported on this device. 187 return false; 188 } 189 boolean vrMode; 190 ComponentName requestedPackage; 191 ComponentName callingPackage; 192 int userId; 193 int processId = -1; 194 boolean changed = false; 195 synchronized (mGlobalAmLock) { 196 vrMode = record.requestedVrComponent != null; 197 requestedPackage = record.requestedVrComponent; 198 userId = record.mUserId; 199 callingPackage = record.info.getComponentName(); 200 201 // Tell the VrController that a VR mode change is requested. 202 changed = changeVrModeLocked(vrMode, record.app); 203 204 if (record.app != null) { 205 processId = record.app.getPid(); 206 } 207 } 208 209 // Tell VrManager that a VR mode changed is requested, VrManager will handle 210 // notifying all non-AM dependencies if needed. 211 vrService.setVrMode(vrMode, requestedPackage, userId, processId, callingPackage); 212 return changed; 213 } 214 215 /** 216 * Called to set an application's VR thread. 217 * 218 * <p>This will fail if the system is not in VR mode, the system has the persistent VR flag set, 219 * or the scheduling group of the thread is not for the current top app. If this succeeds, any 220 * previous VR thread will be returned to a normal sheduling priority; if this fails, the 221 * scheduling for the previous thread will be unaffected. 222 * 223 * <p>Note: This must be called with the global ActivityManagerService lock and the 224 * mPidsSelfLocked object locks held. 225 * 226 * @param tid the tid of the thread to set, or 0 to unset the current thread. 227 * @param pid the pid of the process owning the thread to set. 228 * @param proc the WindowProcessController of the process owning the thread to set. 229 */ setVrThreadLocked(int tid, int pid, WindowProcessController proc)230 public void setVrThreadLocked(int tid, int pid, WindowProcessController proc) { 231 if (hasPersistentVrFlagSet()) { 232 Slog.w(TAG, "VR thread cannot be set in persistent VR mode!"); 233 return; 234 } 235 if (proc == null) { 236 Slog.w(TAG, "Persistent VR thread not set, calling process doesn't exist!"); 237 return; 238 } 239 if (tid != 0) { 240 enforceThreadInProcess(tid, pid); 241 } 242 if (!inVrMode()) { 243 Slog.w(TAG, "VR thread cannot be set when not in VR mode!"); 244 } else { 245 setVrRenderThreadLocked(tid, proc.getCurrentSchedulingGroup(), false); 246 } 247 proc.mVrThreadTid = (tid > 0) ? tid : 0; 248 } 249 250 /** 251 * Called to set an application's persistent VR thread. 252 * 253 * <p>This will fail if the system does not have the persistent VR flag set. If this succeeds, 254 * any previous VR thread will be returned to a normal sheduling priority; if this fails, 255 * the scheduling for the previous thread will be unaffected. 256 * 257 * <p>Note: This must be called with the global ActivityManagerService lock and the 258 * mPidsSelfLocked object locks held. 259 * 260 * @param tid the tid of the thread to set, or 0 to unset the current thread. 261 * @param pid the pid of the process owning the thread to set. 262 * @param proc the process owning the thread to set. 263 */ setPersistentVrThreadLocked(int tid, int pid, WindowProcessController proc)264 public void setPersistentVrThreadLocked(int tid, int pid, WindowProcessController proc) { 265 if (!hasPersistentVrFlagSet()) { 266 Slog.w(TAG, "Persistent VR thread may only be set in persistent VR mode!"); 267 return; 268 } 269 if (proc == null) { 270 Slog.w(TAG, "Persistent VR thread not set, calling process doesn't exist!"); 271 return; 272 } 273 if (tid != 0) { 274 enforceThreadInProcess(tid, pid); 275 } 276 setPersistentVrRenderThreadLocked(tid, false); 277 } 278 279 /** 280 * Return {@code true} when UI features incompatible with VR mode should be disabled. 281 * 282 * <p>Note: This must be called with the global ActivityManagerService lock held. 283 */ shouldDisableNonVrUiLocked()284 public boolean shouldDisableNonVrUiLocked() { 285 return mVrState != FLAG_NON_VR_MODE; 286 } 287 288 /** 289 * Called when to update this VrController instance's state when the system VR mode is being 290 * changed. 291 * 292 * <p>Note: This must be called with the global ActivityManagerService lock held. 293 * 294 * @param vrMode {@code true} if the system VR mode is being enabled. 295 * @param proc the WindowProcessController of the process enabling the system VR mode. 296 * 297 * @return {@code true} if our state changed. 298 */ changeVrModeLocked(boolean vrMode, WindowProcessController proc)299 private boolean changeVrModeLocked(boolean vrMode, WindowProcessController proc) { 300 final int oldVrState = mVrState; 301 302 // This is the only place where mVrState should have its FLAG_VR_MODE setting 303 // changed. 304 if (vrMode) { 305 mVrState |= FLAG_VR_MODE; 306 } else { 307 mVrState &= ~FLAG_VR_MODE; 308 } 309 310 boolean changed = (oldVrState != mVrState); 311 312 if (changed) { 313 if (proc != null) { 314 if (proc.mVrThreadTid > 0) { 315 setVrRenderThreadLocked( 316 proc.mVrThreadTid, proc.getCurrentSchedulingGroup(), false); 317 } 318 } else { 319 clearVrRenderThreadLocked(false); 320 } 321 } 322 return changed; 323 } 324 325 /** 326 * Set the given thread as the new VR thread, and give it special scheduling priority. 327 * 328 * <p>If the current thread is this thread, do nothing. If the current thread is different from 329 * the given thread, the current thread will be returned to a normal scheduling priority. 330 * 331 * @param newTid the tid of the thread to set, or 0 to unset the current thread. 332 * @param suppressLogs {@code true} if any error logging should be disabled. 333 * 334 * @return the tid of the thread configured to run at the scheduling priority for VR 335 * mode after this call completes (this may be the previous thread). 336 */ updateVrRenderThreadLocked(int newTid, boolean suppressLogs)337 private int updateVrRenderThreadLocked(int newTid, boolean suppressLogs) { 338 if (mVrRenderThreadTid == newTid) { 339 return mVrRenderThreadTid; 340 } 341 342 if (mVrRenderThreadTid > 0) { 343 ActivityManagerService.scheduleAsRegularPriority(mVrRenderThreadTid, suppressLogs); 344 mVrRenderThreadTid = 0; 345 } 346 347 if (newTid > 0) { 348 mVrRenderThreadTid = newTid; 349 ActivityManagerService.scheduleAsFifoPriority(mVrRenderThreadTid, suppressLogs); 350 } 351 return mVrRenderThreadTid; 352 } 353 354 /** 355 * Set special scheduling for the given application persistent VR thread, if allowed. 356 * 357 * <p>This will fail if the system does not have the persistent VR flag set. If this succeeds, 358 * any previous VR thread will be returned to a normal sheduling priority; if this fails, 359 * the scheduling for the previous thread will be unaffected. 360 * 361 * @param newTid the tid of the thread to set, or 0 to unset the current thread. 362 * @param suppressLogs {@code true} if any error logging should be disabled. 363 * 364 * @return the tid of the thread configured to run at the scheduling priority for VR 365 * mode after this call completes (this may be the previous thread). 366 */ setPersistentVrRenderThreadLocked(int newTid, boolean suppressLogs)367 private int setPersistentVrRenderThreadLocked(int newTid, boolean suppressLogs) { 368 if (!hasPersistentVrFlagSet()) { 369 if (!suppressLogs) { 370 Slog.w(TAG, "Failed to set persistent VR thread, " 371 + "system not in persistent VR mode."); 372 } 373 return mVrRenderThreadTid; 374 } 375 return updateVrRenderThreadLocked(newTid, suppressLogs); 376 } 377 378 /** 379 * Set special scheduling for the given application VR thread, if allowed. 380 * 381 * <p>This will fail if the system is not in VR mode, the system has the persistent VR flag set, 382 * or the scheduling group of the thread is not for the current top app. If this succeeds, any 383 * previous VR thread will be returned to a normal sheduling priority; if this fails, the 384 * scheduling for the previous thread will be unaffected. 385 * 386 * @param newTid the tid of the thread to set, or 0 to unset the current thread. 387 * @param schedGroup the current scheduling group of the thread to set. 388 * @param suppressLogs {@code true} if any error logging should be disabled. 389 * 390 * @return the tid of the thread configured to run at the scheduling priority for VR 391 * mode after this call completes (this may be the previous thread). 392 */ setVrRenderThreadLocked(int newTid, int schedGroup, boolean suppressLogs)393 private int setVrRenderThreadLocked(int newTid, int schedGroup, boolean suppressLogs) { 394 boolean inVr = inVrMode(); 395 boolean inPersistentVr = hasPersistentVrFlagSet(); 396 if (!inVr || inPersistentVr || schedGroup != ProcessList.SCHED_GROUP_TOP_APP) { 397 if (!suppressLogs) { 398 String reason = "caller is not the current top application."; 399 if (!inVr) { 400 reason = "system not in VR mode."; 401 } else if (inPersistentVr) { 402 reason = "system in persistent VR mode."; 403 } 404 Slog.w(TAG, "Failed to set VR thread, " + reason); 405 } 406 return mVrRenderThreadTid; 407 } 408 return updateVrRenderThreadLocked(newTid, suppressLogs); 409 } 410 411 /** 412 * Unset any special scheduling used for the current VR render thread, and return it to normal 413 * scheduling priority. 414 * 415 * @param suppressLogs {@code true} if any error logging should be disabled. 416 */ clearVrRenderThreadLocked(boolean suppressLogs)417 private void clearVrRenderThreadLocked(boolean suppressLogs) { 418 updateVrRenderThreadLocked(0, suppressLogs); 419 } 420 421 /** 422 * Check that the given tid is running in the process for the given pid, and throw an exception 423 * if not. 424 */ enforceThreadInProcess(int tid, int pid)425 private void enforceThreadInProcess(int tid, int pid) { 426 if (!Process.isThreadInProcess(pid, tid)) { 427 throw new IllegalArgumentException("VR thread does not belong to process"); 428 } 429 } 430 431 /** 432 * True when the system is in VR mode. 433 */ inVrMode()434 private boolean inVrMode() { 435 return (mVrState & FLAG_VR_MODE) != 0; 436 } 437 438 /** 439 * True when the persistent VR mode flag has been set. 440 * 441 * Note: Currently this does not necessarily mean that the system is in VR mode. 442 */ hasPersistentVrFlagSet()443 private boolean hasPersistentVrFlagSet() { 444 return (mVrState & FLAG_PERSISTENT_VR_MODE) != 0; 445 } 446 447 @Override toString()448 public String toString() { 449 return String.format("[VrState=0x%x,VrRenderThreadTid=%d]", mVrState, mVrRenderThreadTid); 450 } 451 dumpDebug(ProtoOutputStream proto, long fieldId)452 void dumpDebug(ProtoOutputStream proto, long fieldId) { 453 final long token = proto.start(fieldId); 454 ProtoUtils.writeBitWiseFlagsToProtoEnum(proto, VrControllerProto.VR_MODE, 455 mVrState, ORIG_ENUMS, PROTO_ENUMS); 456 proto.write(VrControllerProto.RENDER_THREAD_ID, mVrRenderThreadTid); 457 proto.end(token); 458 } 459 } 460