blob: a07ade050e92cb5cf5080b66f150f6a7840313e1 [file] [log] [blame]
Jeff Brown4ccb8232014-01-16 22:16:42 -08001/*
2 * Copyright (C) 2013 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;
18
19import android.os.Handler;
Jorim Jaggied7993b2017-03-28 18:50:01 +010020import android.os.Process;
Jeff Sharkey74cd3de2016-04-06 17:40:54 -060021import android.os.Trace;
Jeff Brown4ccb8232014-01-16 22:16:42 -080022
Garfield Tan92b93652019-08-07 17:22:35 -070023import com.android.internal.annotations.VisibleForTesting;
24
Jeff Brown4ccb8232014-01-16 22:16:42 -080025/**
26 * Shared singleton foreground thread for the system. This is a thread for
27 * operations that affect what's on the display, which needs to have a minimum
28 * of latency. This thread should pretty much only be used by the WindowManager,
29 * DisplayManager, and InputManager to perform quick operations in real time.
30 */
31public final class DisplayThread extends ServiceThread {
32 private static DisplayThread sInstance;
33 private static Handler sHandler;
34
35 private DisplayThread() {
Jorim Jaggied7993b2017-03-28 18:50:01 +010036 // DisplayThread runs important stuff, but these are not as important as things running in
37 // AnimationThread. Thus, set the priority to one lower.
38 super("android.display", Process.THREAD_PRIORITY_DISPLAY + 1, false /*allowIo*/);
Jeff Brown4ccb8232014-01-16 22:16:42 -080039 }
40
41 private static void ensureThreadLocked() {
42 if (sInstance == null) {
43 sInstance = new DisplayThread();
44 sInstance.start();
Michael Wrightdaf72b12017-11-01 22:24:08 +000045 sInstance.getLooper().setTraceTag(Trace.TRACE_TAG_SYSTEM_SERVER);
Jeff Brown4ccb8232014-01-16 22:16:42 -080046 sHandler = new Handler(sInstance.getLooper());
47 }
48 }
49
50 public static DisplayThread get() {
Jeff Brownbf58d9b2014-06-23 16:13:53 -070051 synchronized (DisplayThread.class) {
Jeff Brown4ccb8232014-01-16 22:16:42 -080052 ensureThreadLocked();
53 return sInstance;
54 }
55 }
56
57 public static Handler getHandler() {
Jeff Brownbf58d9b2014-06-23 16:13:53 -070058 synchronized (DisplayThread.class) {
Jeff Brown4ccb8232014-01-16 22:16:42 -080059 ensureThreadLocked();
60 return sHandler;
61 }
62 }
Garfield Tan92b93652019-08-07 17:22:35 -070063
64 /**
65 * Disposes current display thread if it's initialized. Should only be used in tests to set up a
66 * new environment.
67 */
68 @VisibleForTesting
69 public static void dispose() {
70 synchronized (DisplayThread.class) {
71 if (sInstance == null) {
72 return;
73 }
74
75 getHandler().runWithScissors(() -> sInstance.quit(), 0 /* timeout */);
76 sInstance = null;
77 }
78 }
Jeff Brown4ccb8232014-01-16 22:16:42 -080079}