blob: 1b2eb465399a5d23de8f94f805ad39fc19c565f9 [file] [log] [blame]
Jorim Jaggi245281c2017-06-07 14:33:04 -07001/*
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
17package com.android.server.wm;
18
19import static android.os.Process.THREAD_PRIORITY_DISPLAY;
20import static android.os.Process.myTid;
21import static android.os.Process.setThreadPriority;
22
23import static com.android.server.LockGuard.INDEX_WINDOW;
24import static com.android.server.am.ActivityManagerService.TOP_APP_PRIORITY_BOOST;
25
Winson Chung1ec3e262017-06-09 12:01:20 -070026import com.android.internal.annotations.GuardedBy;
Jorim Jaggi245281c2017-06-07 14:33:04 -070027import com.android.server.AnimationThread;
28import com.android.server.ThreadPriorityBooster;
29
30/**
31 * Window manager version of {@link ThreadPriorityBooster} that boosts even more during app
32 * transitions.
33 */
34class WindowManagerThreadPriorityBooster extends ThreadPriorityBooster {
35
Winson Chung1ec3e262017-06-09 12:01:20 -070036 private final Object mLock = new Object();
37
38 private final int mAnimationThreadId;
39
40 @GuardedBy("mLock")
Jorim Jaggi245281c2017-06-07 14:33:04 -070041 private boolean mAppTransitionRunning;
Winson Chung1ec3e262017-06-09 12:01:20 -070042 @GuardedBy("mLock")
43 private boolean mBoundsAnimationRunning;
Jorim Jaggi245281c2017-06-07 14:33:04 -070044
45 WindowManagerThreadPriorityBooster() {
46 super(THREAD_PRIORITY_DISPLAY, INDEX_WINDOW);
Winson Chung1ec3e262017-06-09 12:01:20 -070047 mAnimationThreadId = AnimationThread.get().getThreadId();
Jorim Jaggi245281c2017-06-07 14:33:04 -070048 }
49
50 @Override
51 public void boost() {
52
53 // Do not boost the animation thread. As the animation thread is changing priorities,
54 // boosting it might mess up the priority because we reset it the the previous priority.
Winson Chung1ec3e262017-06-09 12:01:20 -070055 if (myTid() == mAnimationThreadId) {
Jorim Jaggi245281c2017-06-07 14:33:04 -070056 return;
57 }
58 super.boost();
59 }
60
61 @Override
62 public void reset() {
63
64 // See comment in boost().
Winson Chung1ec3e262017-06-09 12:01:20 -070065 if (myTid() == mAnimationThreadId) {
Jorim Jaggi245281c2017-06-07 14:33:04 -070066 return;
67 }
68 super.reset();
69 }
70
71 void setAppTransitionRunning(boolean running) {
Winson Chung1ec3e262017-06-09 12:01:20 -070072 synchronized (mLock) {
73 if (mAppTransitionRunning != running) {
74 mAppTransitionRunning = running;
75 updatePriorityLocked();
76 }
Jorim Jaggi245281c2017-06-07 14:33:04 -070077 }
Jorim Jaggi245281c2017-06-07 14:33:04 -070078 }
79
Winson Chung1ec3e262017-06-09 12:01:20 -070080 void setBoundsAnimationRunning(boolean running) {
81 synchronized (mLock) {
82 if (mBoundsAnimationRunning != running) {
83 mBoundsAnimationRunning = running;
84 updatePriorityLocked();
85 }
86 }
87 }
88
89 @GuardedBy("mLock")
90 private void updatePriorityLocked() {
91 int priority = (mAppTransitionRunning || mBoundsAnimationRunning)
92 ? TOP_APP_PRIORITY_BOOST : THREAD_PRIORITY_DISPLAY;
93 setBoostToPriority(priority);
94 setThreadPriority(mAnimationThreadId, priority);
Jorim Jaggi245281c2017-06-07 14:33:04 -070095 }
96}