LocalDisplayAdapter.java revision c55929a2a5686fe456b19cd54a73b8bde2a4332b
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.state = mState;
165
166                // Assume that all built-in displays that have secure output (eg. HDCP) also
167                // support compositing from gralloc protected buffers.
168                if (mPhys.secure) {
169                    mInfo.flags = DisplayDeviceInfo.FLAG_SECURE
170                            | DisplayDeviceInfo.FLAG_SUPPORTS_PROTECTED_BUFFERS;
171                }
172
173                if (mBuiltInDisplayId == SurfaceControl.BUILT_IN_DISPLAY_ID_MAIN) {
174                    mInfo.name = getContext().getResources().getString(
175                            com.android.internal.R.string.display_manager_built_in_display_name);
176                    mInfo.flags |= DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY
177                            | DisplayDeviceInfo.FLAG_ROTATES_WITH_CONTENT;
178                    mInfo.type = Display.TYPE_BUILT_IN;
179                    mInfo.densityDpi = (int)(mPhys.density * 160 + 0.5f);
180                    mInfo.xDpi = mPhys.xDpi;
181                    mInfo.yDpi = mPhys.yDpi;
182                    mInfo.touch = DisplayDeviceInfo.TOUCH_INTERNAL;
183                } else {
184                    mInfo.type = Display.TYPE_HDMI;
185                    mInfo.flags |= DisplayDeviceInfo.FLAG_PRESENTATION;
186                    mInfo.name = getContext().getResources().getString(
187                            com.android.internal.R.string.display_manager_hdmi_display_name);
188                    mInfo.touch = DisplayDeviceInfo.TOUCH_EXTERNAL;
189                    mInfo.setAssumedDensityForExternalDisplay(mPhys.width, mPhys.height);
190
191                    // For demonstration purposes, allow rotation of the external display.
192                    // In the future we might allow the user to configure this directly.
193                    if ("portrait".equals(SystemProperties.get("persist.demo.hdmirotation"))) {
194                        mInfo.rotation = Surface.ROTATION_270;
195                    }
196
197                    // For demonstration purposes, allow rotation of the external display
198                    // to follow the built-in display.
199                    if (SystemProperties.getBoolean("persist.demo.hdmirotates", false)) {
200                        mInfo.flags |= DisplayDeviceInfo.FLAG_ROTATES_WITH_CONTENT;
201                    }
202                }
203            }
204            return mInfo;
205        }
206
207        @Override
208        public void requestDisplayStateLocked(int state) {
209            if (mState != state) {
210                SurfaceControl.setDisplayPowerMode(getDisplayTokenLocked(),
211                        getPowerModeForState(state));
212                mState = state;
213                updateDeviceInfoLocked();
214            }
215        }
216
217        @Override
218        public void dumpLocked(PrintWriter pw) {
219            super.dumpLocked(pw);
220            pw.println("mBuiltInDisplayId=" + mBuiltInDisplayId);
221            pw.println("mPhys=" + mPhys);
222            pw.println("mState=" + Display.stateToString(mState));
223        }
224
225        private void updateDeviceInfoLocked() {
226            mInfo = null;
227            sendDisplayDeviceEventLocked(this, DISPLAY_DEVICE_EVENT_CHANGED);
228        }
229    }
230
231    private final class HotplugDisplayEventReceiver extends DisplayEventReceiver {
232        public HotplugDisplayEventReceiver(Looper looper) {
233            super(looper);
234        }
235
236        @Override
237        public void onHotplug(long timestampNanos, int builtInDisplayId, boolean connected) {
238            synchronized (getSyncRoot()) {
239                if (connected) {
240                    tryConnectDisplayLocked(builtInDisplayId);
241                } else {
242                    tryDisconnectDisplayLocked(builtInDisplayId);
243                }
244            }
245        }
246    }
247}
248