summaryrefslogtreecommitdiffstats
path: root/services/java/com/android/server/display/DisplayManagerService.java
blob: 468bf21f8fd7697b1230742a814e955a550bbc6b (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
 * 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.IDisplayManager;
import android.os.Binder;
import android.os.SystemProperties;
import android.util.Slog;
import android.util.SparseArray;
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 Context mContext;
    private final boolean mHeadless;

    private int mDisplayIdSeq = Display.DEFAULT_DISPLAY;

    /** All registered DisplayAdapters. */
    private final ArrayList<DisplayAdapter> mDisplayAdapters = new ArrayList<DisplayAdapter>();

    /** All the DisplayAdapters showing the given displayId. */
    private final SparseArray<ArrayList<DisplayAdapter>> mLogicalToPhysicals =
            new SparseArray<ArrayList<DisplayAdapter>>();

    /** All the DisplayInfos in the system indexed by deviceId */
    private final SparseArray<DisplayInfo> mDisplayInfos = new SparseArray<DisplayInfo>();

    private final ArrayList<DisplayCallback> mCallbacks =
            new ArrayList<DisplayManagerService.DisplayCallback>();

    public DisplayManagerService() {
        mHeadless = SystemProperties.get(SYSTEM_HEADLESS).equals("1");
        registerDefaultDisplayAdapter();
    }

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

    public void setContext(Context context) {
        mContext = context;
    }

    // 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;
    }

    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) {
            DisplayInfo localInfo = mDisplayInfos.get(displayId);
            if (localInfo == null) {
                localInfo = new DisplayInfo();
                mDisplayInfos.put(displayId, localInfo);
            }
            localInfo.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) {
            DisplayInfo localInfo = mDisplayInfos.get(displayId);
            if (localInfo == null) {
                return false;
            }
            outInfo.copyFrom(localInfo);
            return true;
        }
    }

    /**
     * Inform the service of a new physical display. A new logical displayId is created and the new
     * physical display is immediately bound to it. Use removeAdapterFromDisplay to disconnect it.
     *
     * @param adapter The wrapper for information associated with the physical display.
     */
    public void registerDisplayAdapter(DisplayAdapter adapter) {

        int displayId;
        DisplayCallback[] callbacks;

        synchronized (mLock) {
            displayId = mDisplayIdSeq;
            do {
                // Find the next unused displayId. (Pretend like it might ever wrap around).
                mDisplayIdSeq++;
                if (mDisplayIdSeq < 0) {
                    mDisplayIdSeq = Display.DEFAULT_DISPLAY + 1;
                }
            } while (mDisplayInfos.get(mDisplayIdSeq) != null);

            adapter.setDisplayId(displayId);

            createDisplayInfoLocked(displayId, adapter);

            ArrayList<DisplayAdapter> list = new ArrayList<DisplayAdapter>();
            list.add(adapter);
            mLogicalToPhysicals.put(displayId, list);

            mDisplayAdapters.add(adapter);
            callbacks = mCallbacks.toArray(new DisplayCallback[mCallbacks.size()]);
        }

        for (int i = callbacks.length - 1; i >= 0; i--) {
            callbacks[i].displayAdded(displayId);
        }

        // TODO: Notify SurfaceFlinger of new addition.
    }

    /**
     * Connect a logical display to a physical display. Will remove the physical display from any
     * logical display it is currently attached to.
     *
     * @param displayId The logical display. Will be created if it does not already exist.
     * @param adapter The physical display.
     */
    public void addAdapterToDisplay(int displayId, DisplayAdapter adapter) {
        if (adapter == null) {
            // TODO: Or throw NPE?
            Slog.e(TAG, "addDeviceToDisplay: Attempt to add null adapter");
            return;
        }

        synchronized (mLock) {
            if (!mDisplayAdapters.contains(adapter)) {
                // TOOD: Handle unregistered adapter with exception or return value.
                Slog.e(TAG, "addDeviceToDisplay: Attempt to add an unregistered adapter");
                return;
            }

            DisplayInfo displayInfo = mDisplayInfos.get(displayId);
            if (displayInfo == null) {
                createDisplayInfoLocked(displayId, adapter);
            }

            Integer oldDisplayId = adapter.getDisplayId();
            if (oldDisplayId != Display.NO_DISPLAY) {
                if (oldDisplayId == displayId) {
                    // adapter already added to displayId.
                    return;
                }

                removeAdapterLocked(adapter);
            }

            ArrayList<DisplayAdapter> list = mLogicalToPhysicals.get(displayId);
            if (list == null) {
                list = new ArrayList<DisplayAdapter>();
                mLogicalToPhysicals.put(displayId, list);
            }
            list.add(adapter);
            adapter.setDisplayId(displayId);
        }

        // TODO: Notify SurfaceFlinger of new addition.
    }

    /**
     * Disconnect the physical display from whichever logical display it is attached to.
     * @param adapter The physical display to detach.
     */
    public void removeAdapterFromDisplay(DisplayAdapter adapter) {
        if (adapter == null) {
            // TODO: Or throw NPE?
            return;
        }

        synchronized (mLock) {
            if (!mDisplayAdapters.contains(adapter)) {
                // TOOD: Handle unregistered adapter with exception or return value.
                Slog.e(TAG, "removeDeviceFromDisplay: Attempt to remove an unregistered adapter");
                return;
            }

            removeAdapterLocked(adapter);
        }

        // TODO: Notify SurfaceFlinger of removal.
    }

    public void registerDisplayCallback(final DisplayCallback callback) {
        synchronized (mLock) {
            if (!mCallbacks.contains(callback)) {
                mCallbacks.add(callback);
            }
        }
    }

    public void unregisterDisplayCallback(final DisplayCallback callback) {
        synchronized (mLock) {
            mCallbacks.remove(callback);
        }
    }

    /**
     * Create a new logical DisplayInfo and fill it in with information from the physical display.
     * @param displayId The logical identifier.
     * @param adapter The physical display for initial values.
     */
    private void createDisplayInfoLocked(int displayId, DisplayAdapter adapter) {
        DisplayInfo displayInfo = new DisplayInfo();
        DisplayDeviceInfo deviceInfo = new DisplayDeviceInfo();
        adapter.getDisplayDevice().getInfo(deviceInfo);
        copyDisplayInfoFromDeviceInfo(displayInfo, deviceInfo);
        mDisplayInfos.put(displayId, displayInfo);
    }

    /**
     * Disconnect a physical display from its logical display. If there are no more physical
     * displays attached to the logical display, delete the logical display.
     * @param adapter The physical display to detach.
     */
    void removeAdapterLocked(DisplayAdapter adapter) {
        int displayId = adapter.getDisplayId();
        adapter.setDisplayId(Display.NO_DISPLAY);

        ArrayList<DisplayAdapter> list = mLogicalToPhysicals.get(displayId);
        if (list != null) {
            list.remove(adapter);
            if (list.isEmpty()) {
                mLogicalToPhysicals.remove(displayId);
                // TODO: Keep count of Windows attached to logical display and don't delete if
                // there are any outstanding. Also, what keeps the WindowManager from continuing
                // to use the logical display?
                mDisplayInfos.remove(displayId);
            }
        }
    }

    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);

        DisplayDeviceInfo info = new DisplayDeviceInfo();
        for (DisplayAdapter adapter : mDisplayAdapters) {
            pw.println("Display for adapter " + adapter.getName()
                + " assigned to Display " + adapter.getDisplayId());
            DisplayDevice device = adapter.getDisplayDevice();
            pw.print("  ");
            device.getInfo(info);
            pw.println(info);
        }
    }

    public interface DisplayCallback {
        public void displayAdded(int displayId);
        public void displayRemoved(int displayId);
    }
}