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
17package com.android.server;
18
19import android.os.Handler;
20import android.os.Process;
21import android.os.Trace;
22
23/**
24 * Shared singleton foreground thread for the system.  This is a thread for
25 * operations that affect what's on the display, which needs to have a minimum
26 * of latency.  This thread should pretty much only be used by the WindowManager,
27 * DisplayManager, and InputManager to perform quick operations in real time.
28 */
29public final class DisplayThread extends ServiceThread {
30    private static DisplayThread sInstance;
31    private static Handler sHandler;
32
33    private DisplayThread() {
34        // DisplayThread runs important stuff, but these are not as important as things running in
35        // AnimationThread. Thus, set the priority to one lower.
36        super("android.display", Process.THREAD_PRIORITY_DISPLAY + 1, false /*allowIo*/);
37    }
38
39    private static void ensureThreadLocked() {
40        if (sInstance == null) {
41            sInstance = new DisplayThread();
42            sInstance.start();
43            sInstance.getLooper().setTraceTag(Trace.TRACE_TAG_SYSTEM_SERVER);
44            sHandler = new Handler(sInstance.getLooper());
45        }
46    }
47
48    public static DisplayThread get() {
49        synchronized (DisplayThread.class) {
50            ensureThreadLocked();
51            return sInstance;
52        }
53    }
54
55    public static Handler getHandler() {
56        synchronized (DisplayThread.class) {
57            ensureThreadLocked();
58            return sHandler;
59        }
60    }
61}
62