Jorim Jaggi | 245281c | 2017-06-07 14:33:04 -0700 | [diff] [blame] | 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 static android.os.Process.THREAD_PRIORITY_DISPLAY; |
| 20 | import static android.os.Process.myTid; |
| 21 | import static android.os.Process.setThreadPriority; |
| 22 | |
| 23 | import static com.android.server.LockGuard.INDEX_WINDOW; |
| 24 | import static com.android.server.am.ActivityManagerService.TOP_APP_PRIORITY_BOOST; |
| 25 | |
Winson Chung | 1ec3e26 | 2017-06-09 12:01:20 -0700 | [diff] [blame] | 26 | import com.android.internal.annotations.GuardedBy; |
Jorim Jaggi | 245281c | 2017-06-07 14:33:04 -0700 | [diff] [blame] | 27 | import com.android.server.AnimationThread; |
| 28 | import com.android.server.ThreadPriorityBooster; |
| 29 | |
| 30 | /** |
| 31 | * Window manager version of {@link ThreadPriorityBooster} that boosts even more during app |
| 32 | * transitions. |
| 33 | */ |
| 34 | class WindowManagerThreadPriorityBooster extends ThreadPriorityBooster { |
| 35 | |
Winson Chung | 1ec3e26 | 2017-06-09 12:01:20 -0700 | [diff] [blame] | 36 | private final Object mLock = new Object(); |
| 37 | |
| 38 | private final int mAnimationThreadId; |
Jorim Jaggi | b0fc817 | 2017-11-23 17:04:08 +0000 | [diff] [blame] | 39 | private final int mSurfaceAnimationThreadId; |
Winson Chung | 1ec3e26 | 2017-06-09 12:01:20 -0700 | [diff] [blame] | 40 | |
| 41 | @GuardedBy("mLock") |
Jorim Jaggi | 245281c | 2017-06-07 14:33:04 -0700 | [diff] [blame] | 42 | private boolean mAppTransitionRunning; |
Winson Chung | 1ec3e26 | 2017-06-09 12:01:20 -0700 | [diff] [blame] | 43 | @GuardedBy("mLock") |
| 44 | private boolean mBoundsAnimationRunning; |
Jorim Jaggi | 245281c | 2017-06-07 14:33:04 -0700 | [diff] [blame] | 45 | |
| 46 | WindowManagerThreadPriorityBooster() { |
| 47 | super(THREAD_PRIORITY_DISPLAY, INDEX_WINDOW); |
Winson Chung | 1ec3e26 | 2017-06-09 12:01:20 -0700 | [diff] [blame] | 48 | mAnimationThreadId = AnimationThread.get().getThreadId(); |
Jorim Jaggi | b0fc817 | 2017-11-23 17:04:08 +0000 | [diff] [blame] | 49 | mSurfaceAnimationThreadId = SurfaceAnimationThread.get().getThreadId(); |
Jorim Jaggi | 245281c | 2017-06-07 14:33:04 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public void boost() { |
| 54 | |
Jorim Jaggi | b0fc817 | 2017-11-23 17:04:08 +0000 | [diff] [blame] | 55 | // Do not boost the animation threads. As the animation threads are changing priorities, |
Jorim Jaggi | 245281c | 2017-06-07 14:33:04 -0700 | [diff] [blame] | 56 | // boosting it might mess up the priority because we reset it the the previous priority. |
Jorim Jaggi | b0fc817 | 2017-11-23 17:04:08 +0000 | [diff] [blame] | 57 | final int myTid = myTid(); |
| 58 | if (myTid == mAnimationThreadId || myTid == mSurfaceAnimationThreadId) { |
Jorim Jaggi | 245281c | 2017-06-07 14:33:04 -0700 | [diff] [blame] | 59 | return; |
| 60 | } |
| 61 | super.boost(); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public void reset() { |
| 66 | |
| 67 | // See comment in boost(). |
Jorim Jaggi | b0fc817 | 2017-11-23 17:04:08 +0000 | [diff] [blame] | 68 | final int myTid = myTid(); |
| 69 | if (myTid == mAnimationThreadId || myTid == mSurfaceAnimationThreadId) { |
Jorim Jaggi | 245281c | 2017-06-07 14:33:04 -0700 | [diff] [blame] | 70 | return; |
| 71 | } |
| 72 | super.reset(); |
| 73 | } |
| 74 | |
| 75 | void setAppTransitionRunning(boolean running) { |
Winson Chung | 1ec3e26 | 2017-06-09 12:01:20 -0700 | [diff] [blame] | 76 | synchronized (mLock) { |
| 77 | if (mAppTransitionRunning != running) { |
| 78 | mAppTransitionRunning = running; |
| 79 | updatePriorityLocked(); |
| 80 | } |
Jorim Jaggi | 245281c | 2017-06-07 14:33:04 -0700 | [diff] [blame] | 81 | } |
Jorim Jaggi | 245281c | 2017-06-07 14:33:04 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Winson Chung | 1ec3e26 | 2017-06-09 12:01:20 -0700 | [diff] [blame] | 84 | void setBoundsAnimationRunning(boolean running) { |
| 85 | synchronized (mLock) { |
| 86 | if (mBoundsAnimationRunning != running) { |
| 87 | mBoundsAnimationRunning = running; |
| 88 | updatePriorityLocked(); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | @GuardedBy("mLock") |
| 94 | private void updatePriorityLocked() { |
| 95 | int priority = (mAppTransitionRunning || mBoundsAnimationRunning) |
| 96 | ? TOP_APP_PRIORITY_BOOST : THREAD_PRIORITY_DISPLAY; |
| 97 | setBoostToPriority(priority); |
| 98 | setThreadPriority(mAnimationThreadId, priority); |
Jorim Jaggi | b0fc817 | 2017-11-23 17:04:08 +0000 | [diff] [blame] | 99 | setThreadPriority(mSurfaceAnimationThreadId, priority); |
Jorim Jaggi | 245281c | 2017-06-07 14:33:04 -0700 | [diff] [blame] | 100 | } |
| 101 | } |