LocalDisplayAdapter.java revision e8b1aeb51e1e5da64f1d4fd40f2ee1e815886fe5
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.content.Context;
20import android.os.Handler;
21import android.os.IBinder;
22import android.os.Looper;
23import android.os.SystemProperties;
24import android.util.Slog;
25import android.util.SparseArray;
26import android.view.Display;
27import android.view.DisplayEventReceiver;
28import android.view.Surface;
29import android.view.SurfaceControl;
30
31import java.io.PrintWriter;
32
33/**
34 * A display adapter for the local displays managed by Surface Flinger.
35 * <p>
36 * Display adapters are guarded by the {@link DisplayManagerService.SyncRoot} lock.
37 * </p>
38 */
39final class LocalDisplayAdapter extends DisplayAdapter {
40    private static final String TAG = "LocalDisplayAdapter";
41
42    private static final int[] BUILT_IN_DISPLAY_IDS_TO_SCAN = new int[] {
43            SurfaceControl.BUILT_IN_DISPLAY_ID_MAIN,
44            SurfaceControl.BUILT_IN_DISPLAY_ID_HDMI,
45    };
46
47    private final SparseArray<LocalDisplayDevice> mDevices =
48            new SparseArray<LocalDisplayDevice>();
49    private HotplugDisplayEventReceiver mHotplugReceiver;
50
51    // Called with SyncRoot lock held.
52    public LocalDisplayAdapter(DisplayManagerService.SyncRoot syncRoot,
53            Context context, Handler handler, Listener listener) {
54        super(syncRoot, context, handler, listener, TAG);
55    }
56
57    @Override
58    public void registerLocked() {
59        super.registerLocked();
60
61        mHotplugReceiver = new HotplugDisplayEventReceiver(getHandler().getLooper());
62
63        for (int builtInDisplayId : BUILT_IN_DISPLAY_IDS_TO_SCAN) {
64            tryConnectDisplayLocked(builtInDisplayId);
65        }
66    }
67
68    private void tryConnectDisplayLocked(int builtInDisplayId) {
69        IBinder displayToken = SurfaceControl.getBuiltInDisplay(builtInDisplayId);
70        if (displayToken != null) {
71            SurfaceControl.PhysicalDisplayInfo[] configs =
72                    SurfaceControl.getDisplayConfigs(displayToken);
73            if (configs == null) {
74                // There are no valid configs for this device, so we can't use it
75                Slog.w(TAG, "No valid configs found for display device " +
76                        builtInDisplayId);
77                return;
78            }
79            int activeConfig = SurfaceControl.getActiveConfig(displayToken);
80            if (activeConfig < 0) {
81                // There is no active config, and for now we don't have the
82                // policy to set one.
83                Slog.w(TAG, "No active config found for display device " +
84                        builtInDisplayId);
85                return;
86            }
87            LocalDisplayDevice device = mDevices.get(builtInDisplayId);
88            if (device == null) {
89                // Display was added.
90                device = new LocalDisplayDevice(displayToken, builtInDisplayId,
91                        configs[activeConfig]);
92                mDevices.put(builtInDisplayId, device);
93                sendDisplayDeviceEventLocked(device, DISPLAY_DEVICE_EVENT_ADDED);
94            } else if (device.updatePhysicalDisplayInfoLocked(configs[activeConfig])) {
95                // Display properties changed.
96                sendDisplayDeviceEventLocked(device, DISPLAY_DEVICE_EVENT_CHANGED);
97            }
98        } else {
99            // The display is no longer available. Ignore the attempt to add it.
100            // If it was connected but has already been disconnected, we'll get a
101            // disconnect event that will remove it from mDevices.
102        }
103    }
104
105    private void tryDisconnectDisplayLocked(int builtInDisplayId) {
106        LocalDisplayDevice device = mDevices.get(builtInDisplayId);
107        if (device != null) {
108            // Display was removed.
109            mDevices.remove(builtInDisplayId);
110            sendDisplayDeviceEventLocked(device, DISPLAY_DEVICE_EVENT_REMOVED);
111        }
112    }
113
114    static int getPowerModeForState(int state) {
115        switch (state) {
116            case Display.STATE_OFF:
117                return SurfaceControl.POWER_MODE_OFF;
118            case Display.STATE_DOZING:
119                return SurfaceControl.POWER_MODE_DOZE;
120            default:
121                return SurfaceControl.POWER_MODE_NORMAL;
122        }
123    }
124
125    private final class LocalDisplayDevice extends DisplayDevice {
126        private final int mBuiltInDisplayId;
127        private final SurfaceControl.PhysicalDisplayInfo mPhys;
128
129        private DisplayDeviceInfo mInfo;
130        private boolean mHavePendingChanges;
131        private int mState = Display.STATE_UNKNOWN;
132
133        public LocalDisplayDevice(IBinder displayToken, int builtInDisplayId,
134                SurfaceControl.PhysicalDisplayInfo phys) {
135            super(LocalDisplayAdapter.this, displayToken);
136            mBuiltInDisplayId = builtInDisplayId;
137            mPhys = new SurfaceControl.PhysicalDisplayInfo(phys);
138        }
139
140        public boolean updatePhysicalDisplayInfoLocked(SurfaceControl.PhysicalDisplayInfo phys) {
141            if (!mPhys.equals(phys)) {
142                mPhys.copyFrom(phys);
143                mHavePendingChanges = true;
144                return true;
145            }
146            return false;
147        }
148
149        @Override
150        public void applyPendingDisplayDeviceInfoChangesLocked() {
151            if (mHavePendingChanges) {
152                mInfo = null;
153                mHavePendingChanges = false;
154            }
155        }
156
157        @Override
158        public DisplayDeviceInfo getDisplayDeviceInfoLocked() {
159            if (mInfo == null) {
160                mInfo = new DisplayDeviceInfo();
161                mInfo.width = mPhys.width;
162                mInfo.height = mPhys.height;
163                mInfo.refreshRate = mPhys.refreshRate;
164                mInfo.appVsyncOffsetNanos = mPhys.appVsyncOffsetNanos;
165                mInfo.presentationDeadlineNanos = mPhys.presentationDeadlineNanos;
166                mInfo.state = mState;
167
168                // Assume that all built-in displays that have secure output (eg. HDCP) also
169                // support compositing from gralloc protected buffers.
170                if (mPhys.secure) {
171                    mInfo.flags = DisplayDeviceInfo.FLAG_SECURE
172                            | DisplayDeviceInfo.FLAG_SUPPORTS_PROTECTED_BUFFERS;
173                }
174
175                if (mBuiltInDisplayId == SurfaceControl.BUILT_IN_DISPLAY_ID_MAIN) {
176                    mInfo.name = getContext().getResources().getString(
177                            com.android.internal.R.string.display_manager_built_in_display_name);
178                    mInfo.flags |= DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY
179                            | DisplayDeviceInfo.FLAG_ROTATES_WITH_CONTENT;
180                    mInfo.type = Display.TYPE_BUILT_IN;
181                    mInfo.densityDpi = (int)(mPhys.density * 160 + 0.5f);
182                    mInfo.xDpi = mPhys.xDpi;
183                    mInfo.yDpi = mPhys.yDpi;
184                    mInfo.touch = DisplayDeviceInfo.TOUCH_INTERNAL;
185                } else {
186                    mInfo.type = Display.TYPE_HDMI;
187                    mInfo.flags |= DisplayDeviceInfo.FLAG_PRESENTATION;
188                    mInfo.name = getContext().getResources().getString(
189                            com.android.internal.R.string.display_manager_hdmi_display_name);
190                    mInfo.touch = DisplayDeviceInfo.TOUCH_EXTERNAL;
191                    mInfo.setAssumedDensityForExternalDisplay(mPhys.width, mPhys.height);
192
193                    // For demonstration purposes, allow rotation of the external display.
194                    // In the future we might allow the user to configure this directly.
195                    if ("portrait".equals(SystemProperties.get("persist.demo.hdmirotation"))) {
196                        mInfo.rotation = Surface.ROTATION_270;
197                    }
198
199                    // For demonstration purposes, allow rotation of the external display
200                    // to follow the built-in display.
201                    if (SystemProperties.getBoolean("persist.demo.hdmirotates", false)) {
202                        mInfo.flags |= DisplayDeviceInfo.FLAG_ROTATES_WITH_CONTENT;
203                    }
204                }
205            }
206            return mInfo;
207        }
208
209        @Override
210        public void requestDisplayStateLocked(int state) {
211            if (mState != state) {
212                SurfaceControl.setDisplayPowerMode(getDisplayTokenLocked(),
213                        getPowerModeForState(state));
214                mState = state;
215                updateDeviceInfoLocked();
216            }
217        }
218
219        @Override
220        public void dumpLocked(PrintWriter pw) {
221            super.dumpLocked(pw);
222            pw.println("mBuiltInDisplayId=" + mBuiltInDisplayId);
223            pw.println("mPhys=" + mPhys);
224            pw.println("mState=" + Display.stateToString(mState));
225        }
226
227        private void updateDeviceInfoLocked() {
228            mInfo = null;
229            sendDisplayDeviceEventLocked(this, DISPLAY_DEVICE_EVENT_CHANGED);
230        }
231    }
232
233    private final class HotplugDisplayEventReceiver extends DisplayEventReceiver {
234        public HotplugDisplayEventReceiver(Looper looper) {
235            super(looper);
236        }
237
238        @Override
239        public void onHotplug(long timestampNanos, int builtInDisplayId, boolean connected) {
240            synchronized (getSyncRoot()) {
241                if (connected) {
242                    tryConnectDisplayLocked(builtInDisplayId);
243                } else {
244                    tryDisconnectDisplayLocked(builtInDisplayId);
245                }
246            }
247        }
248    }
249}
250