summaryrefslogtreecommitdiff
path: root/services/java/com/android/server/display/DisplayManagerService.java
blob: 2ebad1d48ca355494ed7143579667c237f99bf66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.display;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.display.DisplayManager;
import android.hardware.display.IDisplayManager;
import android.os.Binder;
import android.os.SystemProperties;
import android.view.Display;
import android.view.DisplayInfo;
import android.view.Surface;

import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.ArrayList;

/**
 * Manages the properties, media routing and power state of attached displays.
 * <p>
 * The display manager service does not own or directly control the displays.
 * Instead, other components in the system register their display adapters with the
 * display manager service which acts as a central controller.
 * </p>
 */
public final class DisplayManagerService extends IDisplayManager.Stub {
    private static final String TAG = "DisplayManagerService";

    private static final String SYSTEM_HEADLESS = "ro.config.headless";

    private final Object mLock = new Object();

    private final Context mContext;
    private final boolean mHeadless;

    private final ArrayList<DisplayAdapter> mDisplayAdapters = new ArrayList<DisplayAdapter>();
    private final DisplayInfo mDefaultDisplayInfo = new DisplayInfo();

    public DisplayManagerService(Context context) {
        mContext = context;
        mHeadless = SystemProperties.get(SYSTEM_HEADLESS).equals("1");

        registerDefaultDisplayAdapter();
    }

    private void registerDefaultDisplayAdapter() {
        if (mHeadless) {
            registerDisplayAdapter(new HeadlessDisplayAdapter(mContext));
        } else {
            registerDisplayAdapter(new SurfaceFlingerDisplayAdapter(mContext));
        }
    }

    // FIXME: this isn't the right API for the long term
    public void getDefaultExternalDisplayDeviceInfo(DisplayDeviceInfo info) {
        // hardcoded assuming 720p touch screen plugged into HDMI and USB
        // need to redesign this
        info.width = 1280;
        info.height = 720;
    }

    /**
     * Returns true if the device is headless.
     *
     * @return True if the device is headless.
     */
    public boolean isHeadless() {
        return mHeadless;
    }

    /**
     * Save away new DisplayInfo data.
     * @param displayId The local DisplayInfo to store the new data in.
     * @param info The new data to be stored.
     */
    public void setDisplayInfo(int displayId, DisplayInfo info) {
        synchronized (mLock) {
            if (displayId != Display.DEFAULT_DISPLAY) {
                throw new UnsupportedOperationException();
            }
            mDefaultDisplayInfo.copyFrom(info);
        }
    }

    /**
     * Return requested DisplayInfo.
     * @param displayId The data to retrieve.
     * @param outInfo The structure to receive the data.
     */
    @Override // Binder call
    public boolean getDisplayInfo(int displayId, DisplayInfo outInfo) {
        synchronized (mLock) {
            if (displayId != Display.DEFAULT_DISPLAY) {
                return false;
            }
            outInfo.copyFrom(mDefaultDisplayInfo);
            return true;
        }
    }

    private void registerDisplayAdapter(DisplayAdapter adapter) {
        mDisplayAdapters.add(adapter);
        adapter.register(new DisplayAdapter.Listener() {
            @Override
            public void onDisplayDeviceAdded(DisplayDevice device) {
                DisplayDeviceInfo deviceInfo = new DisplayDeviceInfo();
                device.getInfo(deviceInfo);
                copyDisplayInfoFromDeviceInfo(mDefaultDisplayInfo, deviceInfo);
            }

            @Override
            public void onDisplayDeviceRemoved(DisplayDevice device) {
            }
        });
    }

    private void copyDisplayInfoFromDeviceInfo(
            DisplayInfo displayInfo, DisplayDeviceInfo deviceInfo) {
        // Bootstrap the logical display using the physical display.
        displayInfo.appWidth = deviceInfo.width;
        displayInfo.appHeight = deviceInfo.height;
        displayInfo.logicalWidth = deviceInfo.width;
        displayInfo.logicalHeight = deviceInfo.height;
        displayInfo.rotation = Surface.ROTATION_0;
        displayInfo.refreshRate = deviceInfo.refreshRate;
        displayInfo.logicalDensityDpi = deviceInfo.densityDpi;
        displayInfo.physicalXDpi = deviceInfo.xDpi;
        displayInfo.physicalYDpi = deviceInfo.yDpi;
        displayInfo.smallestNominalAppWidth = deviceInfo.width;
        displayInfo.smallestNominalAppHeight = deviceInfo.height;
        displayInfo.largestNominalAppWidth = deviceInfo.width;
        displayInfo.largestNominalAppHeight = deviceInfo.height;
    }

    @Override // Binder call
    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
        if (mContext == null
                || mContext.checkCallingOrSelfPermission(Manifest.permission.DUMP)
                        != PackageManager.PERMISSION_GRANTED) {
            pw.println("Permission Denial: can't dump DisplayManager from from pid="
                    + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
            return;
        }

        pw.println("DISPLAY MANAGER (dumpsys display)\n");

        pw.println("Headless: " + mHeadless);

        synchronized (mLock) {
            for (DisplayAdapter adapter : mDisplayAdapters) {
                pw.println("Adapter: " + adapter.getName());
            }

            pw.println("Default display info: " + mDefaultDisplayInfo);
        }

        pw.println("Default display: "
                + DisplayManager.getInstance().getRealDisplay(Display.DEFAULT_DISPLAY));
    }
}