DisplayManagerService.java revision 908aecc3a63c5520d5b11da14a9383f885b7d126
1/*
2 * Copyright (C) 2012 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.display;
18
19import android.Manifest;
20import android.content.Context;
21import android.content.pm.PackageManager;
22import android.hardware.display.IDisplayManager;
23import android.os.Binder;
24import android.os.SystemProperties;
25import android.view.Display;
26import android.view.DisplayInfo;
27import android.view.Surface;
28
29import java.io.FileDescriptor;
30import java.io.PrintWriter;
31import java.util.ArrayList;
32
33/**
34 * Manages the properties, media routing and power state of attached displays.
35 * <p>
36 * The display manager service does not own or directly control the displays.
37 * Instead, other components in the system register their display adapters with the
38 * display manager service which acts as a central controller.
39 * </p>
40 */
41public final class DisplayManagerService extends IDisplayManager.Stub {
42    private static final String TAG = "DisplayManagerService";
43
44    private static final String SYSTEM_HEADLESS = "ro.config.headless";
45
46    private final Object mLock = new Object();
47
48    private Context mContext;
49    private final boolean mHeadless;
50    private final ArrayList<DisplayAdapter> mDisplayAdapters = new ArrayList<DisplayAdapter>();
51
52    // TODO: represent this as a map between logical and physical devices
53    private DisplayInfo mDefaultDisplayInfo;
54    private DisplayDevice mDefaultDisplayDevice;
55    private DisplayDeviceInfo mDefaultDisplayDeviceInfo;
56
57    public DisplayManagerService() {
58        mHeadless = SystemProperties.get(SYSTEM_HEADLESS).equals("1");
59        registerDisplayAdapters();
60        initializeDefaultDisplay();
61    }
62
63    public void setContext(Context context) {
64        mContext = context;
65    }
66
67    // FIXME: this isn't the right API for the long term
68    public void setDefaultDisplayInfo(DisplayInfo info) {
69        synchronized (mLock) {
70            mDefaultDisplayInfo.copyFrom(info);
71        }
72    }
73
74    // FIXME: this isn't the right API for the long term
75    public void getDefaultExternalDisplayDeviceInfo(DisplayDeviceInfo info) {
76        // hardcoded assuming 720p touch screen plugged into HDMI and USB
77        // need to redesign this
78        info.width = 1280;
79        info.height = 720;
80    }
81
82    public boolean isHeadless() {
83        return mHeadless;
84    }
85
86    @Override // Binder call
87    public boolean getDisplayInfo(int displayId, DisplayInfo outInfo) {
88        synchronized (mLock) {
89            if (displayId == Display.DEFAULT_DISPLAY) {
90                outInfo.copyFrom(mDefaultDisplayInfo);
91                return true;
92            }
93            return false;
94        }
95    }
96
97    @Override // Binder call
98    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
99        if (mContext == null
100                || mContext.checkCallingOrSelfPermission(Manifest.permission.DUMP)
101                        != PackageManager.PERMISSION_GRANTED) {
102            pw.println("Permission Denial: can't dump DisplayManager from from pid="
103                    + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
104            return;
105        }
106
107        pw.println("DISPLAY MANAGER (dumpsys display)\n");
108
109        pw.println("Headless: " + mHeadless);
110
111        DisplayDeviceInfo info = new DisplayDeviceInfo();
112        for (DisplayAdapter adapter : mDisplayAdapters) {
113            pw.println("Displays for adapter " + adapter.getName());
114            for (DisplayDevice device : adapter.getDisplayDevices()) {
115                device.getInfo(info);
116                pw.print("  ");
117                pw.println(info);
118            }
119        }
120    }
121
122    private void registerDisplayAdapters() {
123        if (mHeadless) {
124            registerDisplayAdapter(new HeadlessDisplayAdapter());
125        } else {
126            registerDisplayAdapter(new SurfaceFlingerDisplayAdapter());
127        }
128    }
129
130    private void registerDisplayAdapter(DisplayAdapter adapter) {
131        // TODO: do this dynamically
132        mDisplayAdapters.add(adapter);
133        mDefaultDisplayDevice = adapter.getDisplayDevices()[0];
134        mDefaultDisplayDeviceInfo = new DisplayDeviceInfo();
135        mDefaultDisplayDevice.getInfo(mDefaultDisplayDeviceInfo);
136    }
137
138    private void initializeDefaultDisplay() {
139        // Bootstrap the default logical display using the default physical display.
140        mDefaultDisplayInfo = new DisplayInfo();
141        mDefaultDisplayInfo.appWidth = mDefaultDisplayDeviceInfo.width;
142        mDefaultDisplayInfo.appHeight = mDefaultDisplayDeviceInfo.height;
143        mDefaultDisplayInfo.logicalWidth = mDefaultDisplayDeviceInfo.width;
144        mDefaultDisplayInfo.logicalHeight = mDefaultDisplayDeviceInfo.height;
145        mDefaultDisplayInfo.rotation = Surface.ROTATION_0;
146        mDefaultDisplayInfo.refreshRate = mDefaultDisplayDeviceInfo.refreshRate;
147        mDefaultDisplayInfo.logicalDensityDpi = mDefaultDisplayDeviceInfo.densityDpi;
148        mDefaultDisplayInfo.physicalXDpi = mDefaultDisplayDeviceInfo.xDpi;
149        mDefaultDisplayInfo.physicalYDpi = mDefaultDisplayDeviceInfo.yDpi;
150        mDefaultDisplayInfo.smallestNominalAppWidth = mDefaultDisplayDeviceInfo.width;
151        mDefaultDisplayInfo.smallestNominalAppHeight = mDefaultDisplayDeviceInfo.height;
152        mDefaultDisplayInfo.largestNominalAppWidth = mDefaultDisplayDeviceInfo.width;
153        mDefaultDisplayInfo.largestNominalAppHeight = mDefaultDisplayDeviceInfo.height;
154    }
155}
156