Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
| 19 | import android.os.Handler; |
Jorim Jaggi | ed7993b | 2017-03-28 18:50:01 +0100 | [diff] [blame] | 20 | import android.os.Process; |
Jeff Sharkey | 74cd3de | 2016-04-06 17:40:54 -0600 | [diff] [blame] | 21 | import android.os.Trace; |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 22 | |
Garfield Tan | 92b9365 | 2019-08-07 17:22:35 -0700 | [diff] [blame] | 23 | import com.android.internal.annotations.VisibleForTesting; |
| 24 | |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 25 | /** |
| 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 | */ |
| 31 | public final class DisplayThread extends ServiceThread { |
| 32 | private static DisplayThread sInstance; |
| 33 | private static Handler sHandler; |
| 34 | |
| 35 | private DisplayThread() { |
Jorim Jaggi | ed7993b | 2017-03-28 18:50:01 +0100 | [diff] [blame] | 36 | // 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 Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | private static void ensureThreadLocked() { |
| 42 | if (sInstance == null) { |
| 43 | sInstance = new DisplayThread(); |
| 44 | sInstance.start(); |
Michael Wright | daf72b1 | 2017-11-01 22:24:08 +0000 | [diff] [blame] | 45 | sInstance.getLooper().setTraceTag(Trace.TRACE_TAG_SYSTEM_SERVER); |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 46 | sHandler = new Handler(sInstance.getLooper()); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | public static DisplayThread get() { |
Jeff Brown | bf58d9b | 2014-06-23 16:13:53 -0700 | [diff] [blame] | 51 | synchronized (DisplayThread.class) { |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 52 | ensureThreadLocked(); |
| 53 | return sInstance; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | public static Handler getHandler() { |
Jeff Brown | bf58d9b | 2014-06-23 16:13:53 -0700 | [diff] [blame] | 58 | synchronized (DisplayThread.class) { |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 59 | ensureThreadLocked(); |
| 60 | return sHandler; |
| 61 | } |
| 62 | } |
Garfield Tan | 92b9365 | 2019-08-07 17:22:35 -0700 | [diff] [blame] | 63 | |
| 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 Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 79 | } |