blob: 971989b21219491a011bc1ad8b6b054a55f8e3bf [file] [log] [blame]
Jeff Brownfa25bf52012-07-23 19:26:30 -07001/*
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
Jeff Brownbd6e1502012-08-28 03:27:37 -070019import com.android.internal.util.IndentingPrintWriter;
20
Jeff Brownfa25bf52012-07-23 19:26:30 -070021import android.Manifest;
22import android.content.Context;
23import android.content.pm.PackageManager;
Jeff Brownad9ef192014-04-08 17:26:30 -070024import android.hardware.SensorManager;
Jeff Brown7d00aff2013-08-02 19:03:49 -070025import android.hardware.display.DisplayManager;
Jeff Brownbd6e1502012-08-28 03:27:37 -070026import android.hardware.display.DisplayManagerGlobal;
Jeff Brown4ccb8232014-01-16 22:16:42 -080027import android.hardware.display.DisplayManagerInternal;
28import android.hardware.display.DisplayViewport;
29import android.hardware.display.DisplayManagerInternal.DisplayTransactionListener;
Jeff Brownfa25bf52012-07-23 19:26:30 -070030import android.hardware.display.IDisplayManager;
Jeff Brownbd6e1502012-08-28 03:27:37 -070031import android.hardware.display.IDisplayManagerCallback;
Michael Wright75ee9fc2014-09-01 19:55:22 -070032import android.hardware.display.IVirtualDisplayCallback;
Jeff Browne08ae382012-09-07 20:36:36 -070033import android.hardware.display.WifiDisplayStatus;
Jeff Brown4ccb8232014-01-16 22:16:42 -080034import android.hardware.input.InputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -070035import android.media.projection.IMediaProjection;
36import android.media.projection.IMediaProjectionManager;
Jeff Brownfa25bf52012-07-23 19:26:30 -070037import android.os.Binder;
Jeff Brownbd6e1502012-08-28 03:27:37 -070038import android.os.Handler;
Jeff Brown64a55af2012-08-26 02:47:39 -070039import android.os.IBinder;
Jeff Brown4ccb8232014-01-16 22:16:42 -080040import android.os.IBinder.DeathRecipient;
Jeff Brownbd6e1502012-08-28 03:27:37 -070041import android.os.Looper;
42import android.os.Message;
Jeff Brown5d6443b2015-04-10 20:15:01 -070043import android.os.PowerManager;
Craig Mautner4504de52013-12-20 09:06:56 -080044import android.os.Process;
Jeff Brownbd6e1502012-08-28 03:27:37 -070045import android.os.RemoteException;
Michael Wrightc39d47a2014-07-08 18:07:36 -070046import android.os.ServiceManager;
Jeff Brownbd6e1502012-08-28 03:27:37 -070047import android.os.SystemClock;
Jeff Brownfa25bf52012-07-23 19:26:30 -070048import android.os.SystemProperties;
Jeff Brown5d6443b2015-04-10 20:15:01 -070049import android.os.Trace;
Jeff Browna506a6e2013-06-04 00:02:38 -070050import android.text.TextUtils;
Jeff Brownbd6e1502012-08-28 03:27:37 -070051import android.util.Slog;
52import android.util.SparseArray;
Jeff Brownfa25bf52012-07-23 19:26:30 -070053import android.view.Display;
54import android.view.DisplayInfo;
Jeff Browna506a6e2013-06-04 00:02:38 -070055import android.view.Surface;
Jeff Brown4ccb8232014-01-16 22:16:42 -080056import android.view.WindowManagerInternal;
Jeff Browna506a6e2013-06-04 00:02:38 -070057
Jeff Brown4ccb8232014-01-16 22:16:42 -080058import com.android.server.DisplayThread;
59import com.android.server.LocalServices;
60import com.android.server.SystemService;
Dianne Hackborn8d044e82013-04-30 17:24:15 -070061import com.android.server.UiThread;
Jeff Brownfa25bf52012-07-23 19:26:30 -070062
63import java.io.FileDescriptor;
64import java.io.PrintWriter;
65import java.util.ArrayList;
Jeff Browna506a6e2013-06-04 00:02:38 -070066import java.util.Arrays;
Jeff Browne75926d2014-09-18 15:24:49 -070067import java.util.List;
Jeff Brown7f3994e2012-12-04 14:04:28 -080068import java.util.concurrent.CopyOnWriteArrayList;
Jeff Brownfa25bf52012-07-23 19:26:30 -070069
70/**
Jeff Brownbd6e1502012-08-28 03:27:37 -070071 * Manages attached displays.
Jeff Brownfa25bf52012-07-23 19:26:30 -070072 * <p>
Jeff Brownbd6e1502012-08-28 03:27:37 -070073 * The {@link DisplayManagerService} manages the global lifecycle of displays,
74 * decides how to configure logical displays based on the physical display devices currently
75 * attached, sends notifications to the system and to applications when the state
76 * changes, and so on.
77 * </p><p>
78 * The display manager service relies on a collection of {@link DisplayAdapter} components,
79 * for discovering and configuring physical display devices attached to the system.
80 * There are separate display adapters for each manner that devices are attached:
81 * one display adapter for built-in local displays, one for simulated non-functional
82 * displays when the system is headless, one for simulated overlay displays used for
83 * development, one for wifi displays, etc.
84 * </p><p>
85 * Display adapters are only weakly coupled to the display manager service.
86 * Display adapters communicate changes in display device state to the display manager
Craig Mautner722285e2012-09-07 13:55:58 -070087 * service asynchronously via a {@link DisplayAdapter.Listener} registered
Jeff Brownbd6e1502012-08-28 03:27:37 -070088 * by the display manager service. This separation of concerns is important for
89 * two main reasons. First, it neatly encapsulates the responsibilities of these
90 * two classes: display adapters handle individual display devices whereas
91 * the display manager service handles the global state. Second, it eliminates
92 * the potential for deadlocks resulting from asynchronous display device discovery.
Jeff Brown4ed8fe72012-08-30 18:18:29 -070093 * </p>
94 *
95 * <h3>Synchronization</h3>
96 * <p>
97 * Because the display manager may be accessed by multiple threads, the synchronization
98 * story gets a little complicated. In particular, the window manager may call into
99 * the display manager while holding a surface transaction with the expectation that
100 * it can apply changes immediately. Unfortunately, that means we can't just do
101 * everything asynchronously (*grump*).
Jeff Brownbd6e1502012-08-28 03:27:37 -0700102 * </p><p>
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700103 * To make this work, all of the objects that belong to the display manager must
104 * use the same lock. We call this lock the synchronization root and it has a unique
105 * type {@link DisplayManagerService.SyncRoot}. Methods that require this lock are
106 * named with the "Locked" suffix.
107 * </p><p>
108 * Where things get tricky is that the display manager is not allowed to make
109 * any potentially reentrant calls, especially into the window manager. We generally
110 * avoid this by making all potentially reentrant out-calls asynchronous.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700111 * </p>
112 */
Jeff Brown4ccb8232014-01-16 22:16:42 -0800113public final class DisplayManagerService extends SystemService {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700114 private static final String TAG = "DisplayManagerService";
Jeff Brownbd6e1502012-08-28 03:27:37 -0700115 private static final boolean DEBUG = false;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700116
Jeff Brownbbd28a22012-09-20 16:47:15 -0700117 // When this system property is set to 0, WFD is forcibly disabled on boot.
118 // When this system property is set to 1, WFD is forcibly enabled on boot.
119 // Otherwise WFD is enabled according to the value of config_enableWifiDisplay.
120 private static final String FORCE_WIFI_DISPLAY_ENABLE = "persist.debug.wfd.enable";
121
Jeff Brownbd6e1502012-08-28 03:27:37 -0700122 private static final long WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT = 10000;
123
124 private static final int MSG_REGISTER_DEFAULT_DISPLAY_ADAPTER = 1;
125 private static final int MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS = 2;
126 private static final int MSG_DELIVER_DISPLAY_EVENT = 3;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700127 private static final int MSG_REQUEST_TRAVERSAL = 4;
Jeff Brownd728bf52012-09-08 18:05:28 -0700128 private static final int MSG_UPDATE_VIEWPORT = 5;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700129
Jeff Brownb880d882014-02-10 19:47:07 -0800130 private final Context mContext;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700131 private final DisplayManagerHandler mHandler;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700132 private final Handler mUiHandler;
133 private final DisplayAdapterListener mDisplayAdapterListener;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800134 private WindowManagerInternal mWindowManagerInternal;
135 private InputManagerInternal mInputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -0700136 private IMediaProjectionManager mProjectionService;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700137
138 // The synchronization root for the display manager.
139 // This lock guards most of the display manager's state.
Craig Mautner0bf6ec92012-12-18 08:33:27 -0800140 // NOTE: This is synchronized on while holding WindowManagerService.mWindowMap so never call
141 // into WindowManagerService methods that require mWindowMap while holding this unless you are
142 // very very sure that no deadlock can occur.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700143 private final SyncRoot mSyncRoot = new SyncRoot();
144
145 // True if in safe mode.
146 // This option may disable certain display adapters.
147 public boolean mSafeMode;
148
149 // True if we are in a special boot mode where only core applications and
150 // services should be started. This option may disable certain display adapters.
151 public boolean mOnlyCore;
152
Jeff Brown27f1d672012-10-17 18:32:34 -0700153 // True if the display manager service should pretend there is only one display
154 // and only tell applications about the existence of the default logical display.
155 // The display manager can still mirror content to secondary displays but applications
156 // cannot present unique content on those displays.
157 // Used for demonstration purposes only.
158 private final boolean mSingleDisplayDemoMode;
159
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700160 // All callback records indexed by calling process id.
161 public final SparseArray<CallbackRecord> mCallbacks =
Jeff Brownbd6e1502012-08-28 03:27:37 -0700162 new SparseArray<CallbackRecord>();
Jeff Brownfa25bf52012-07-23 19:26:30 -0700163
Jeff Brownbd6e1502012-08-28 03:27:37 -0700164 // List of all currently registered display adapters.
165 private final ArrayList<DisplayAdapter> mDisplayAdapters = new ArrayList<DisplayAdapter>();
166
167 // List of all currently connected display devices.
168 private final ArrayList<DisplayDevice> mDisplayDevices = new ArrayList<DisplayDevice>();
169
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700170 // List of all logical displays indexed by logical display id.
171 private final SparseArray<LogicalDisplay> mLogicalDisplays =
172 new SparseArray<LogicalDisplay>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700173 private int mNextNonDefaultDisplayId = Display.DEFAULT_DISPLAY + 1;
174
Jeff Brown7f3994e2012-12-04 14:04:28 -0800175 // List of all display transaction listeners.
176 private final CopyOnWriteArrayList<DisplayTransactionListener> mDisplayTransactionListeners =
177 new CopyOnWriteArrayList<DisplayTransactionListener>();
178
Jeff Brownad9ef192014-04-08 17:26:30 -0700179 // Display power controller.
180 private DisplayPowerController mDisplayPowerController;
181
Jeff Brown037c33e2014-04-09 00:31:55 -0700182 // The overall display state, independent of changes that might influence one
183 // display or another in particular.
Jeff Brown5d6443b2015-04-10 20:15:01 -0700184 private int mGlobalDisplayState = Display.STATE_ON;
185
186 // The overall display brightness.
187 // For now, this only applies to the built-in display but we may split it up eventually.
188 private int mGlobalDisplayBrightness = PowerManager.BRIGHTNESS_DEFAULT;
Jeff Brown9e316a12012-10-08 19:17:06 -0700189
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700190 // Set to true when there are pending display changes that have yet to be applied
191 // to the surface flinger state.
192 private boolean mPendingTraversal;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700193
Jeff Browne08ae382012-09-07 20:36:36 -0700194 // The Wifi display adapter, or null if not registered.
195 private WifiDisplayAdapter mWifiDisplayAdapter;
196
Jeff Brownce468a32013-11-21 16:42:03 -0800197 // The number of active wifi display scan requests.
198 private int mWifiDisplayScanRequestCount;
199
Jeff Browna506a6e2013-06-04 00:02:38 -0700200 // The virtual display adapter, or null if not registered.
201 private VirtualDisplayAdapter mVirtualDisplayAdapter;
202
Jeff Brownd728bf52012-09-08 18:05:28 -0700203 // Viewports of the default display and the display that should receive touch
204 // input from an external source. Used by the input system.
205 private final DisplayViewport mDefaultViewport = new DisplayViewport();
206 private final DisplayViewport mExternalTouchViewport = new DisplayViewport();
207
Jeff Brown89d55462012-09-19 11:33:42 -0700208 // Persistent data store for all internal settings maintained by the display manager service.
209 private final PersistentDataStore mPersistentDataStore = new PersistentDataStore();
210
Jeff Brownbd6e1502012-08-28 03:27:37 -0700211 // Temporary callback list, used when sending display events to applications.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700212 // May be used outside of the lock but only on the handler thread.
213 private final ArrayList<CallbackRecord> mTempCallbacks = new ArrayList<CallbackRecord>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700214
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700215 // Temporary display info, used for comparing display configurations.
216 private final DisplayInfo mTempDisplayInfo = new DisplayInfo();
217
Jeff Brownd728bf52012-09-08 18:05:28 -0700218 // Temporary viewports, used when sending new viewport information to the
219 // input system. May be used outside of the lock but only on the handler thread.
220 private final DisplayViewport mTempDefaultViewport = new DisplayViewport();
221 private final DisplayViewport mTempExternalTouchViewport = new DisplayViewport();
222
Jeff Browne75926d2014-09-18 15:24:49 -0700223 // Temporary list of deferred work to perform when setting the display state.
224 // Only used by requestDisplayState. The field is self-synchronized and only
225 // intended for use inside of the requestGlobalDisplayStateInternal function.
226 private final ArrayList<Runnable> mTempDisplayStateWorkQueue = new ArrayList<Runnable>();
227
Jeff Brownb880d882014-02-10 19:47:07 -0800228 public DisplayManagerService(Context context) {
229 super(context);
230 mContext = context;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800231 mHandler = new DisplayManagerHandler(DisplayThread.get().getLooper());
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700232 mUiHandler = UiThread.getHandler();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700233 mDisplayAdapterListener = new DisplayAdapterListener();
Jeff Brown27f1d672012-10-17 18:32:34 -0700234 mSingleDisplayDemoMode = SystemProperties.getBoolean("persist.demo.singledisplay", false);
Jeff Brown5d6443b2015-04-10 20:15:01 -0700235
236 PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
237 mGlobalDisplayBrightness = pm.getDefaultScreenBrightnessSetting();
Craig Mautner4f67ba62012-08-02 11:23:00 -0700238 }
239
Jeff Brown4ccb8232014-01-16 22:16:42 -0800240 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -0800241 public void onStart() {
Michael Wright1c9977b2016-07-12 13:30:10 -0700242 // We need to pre-load the persistent data store so it's ready before the default display
243 // adapter is up so that we have it's configuration. We could load it lazily, but since
244 // we're going to have to read it in eventually we may as well do it here rather than after
245 // we've waited for the diplay to register itself with us.
246 mPersistentDataStore.loadIfNeeded();
Jeff Brown4ccb8232014-01-16 22:16:42 -0800247 mHandler.sendEmptyMessage(MSG_REGISTER_DEFAULT_DISPLAY_ADAPTER);
248
249 publishBinderService(Context.DISPLAY_SERVICE, new BinderService(),
250 true /*allowIsolated*/);
251 publishLocalService(DisplayManagerInternal.class, new LocalService());
Justin Klaassen22eb1992016-07-11 20:52:23 -0700252 publishLocalService(DisplayTransformManager.class, new DisplayTransformManager());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800253 }
254
255 @Override
256 public void onBootPhase(int phase) {
257 if (phase == PHASE_WAIT_FOR_DEFAULT_DISPLAY) {
258 synchronized (mSyncRoot) {
259 long timeout = SystemClock.uptimeMillis() + WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT;
260 while (mLogicalDisplays.get(Display.DEFAULT_DISPLAY) == null) {
261 long delay = timeout - SystemClock.uptimeMillis();
262 if (delay <= 0) {
263 throw new RuntimeException("Timeout waiting for default display "
264 + "to be initialized.");
265 }
266 if (DEBUG) {
267 Slog.d(TAG, "waitForDefaultDisplay: waiting, timeout=" + delay);
268 }
269 try {
270 mSyncRoot.wait(delay);
271 } catch (InterruptedException ex) {
272 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700273 }
274 }
275 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700276 }
277
Jeff Brown4ccb8232014-01-16 22:16:42 -0800278 // TODO: Use dependencies or a boot phase
279 public void windowManagerAndInputReady() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700280 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800281 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
282 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner65d11b32012-10-01 13:59:52 -0700283 scheduleTraversalLocked(false);
Jeff Brownd728bf52012-09-08 18:05:28 -0700284 }
285 }
286
287 /**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700288 * Called when the system is ready to go.
289 */
290 public void systemReady(boolean safeMode, boolean onlyCore) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700291 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700292 mSafeMode = safeMode;
293 mOnlyCore = onlyCore;
294 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700295
Jeff Brownbd6e1502012-08-28 03:27:37 -0700296 mHandler.sendEmptyMessage(MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS);
297 }
298
Jeff Brown4ccb8232014-01-16 22:16:42 -0800299 private void registerDisplayTransactionListenerInternal(
300 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800301 // List is self-synchronized copy-on-write.
302 mDisplayTransactionListeners.add(listener);
303 }
304
Jeff Brown4ccb8232014-01-16 22:16:42 -0800305 private void unregisterDisplayTransactionListenerInternal(
306 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800307 // List is self-synchronized copy-on-write.
308 mDisplayTransactionListeners.remove(listener);
309 }
310
Jeff Brown4ccb8232014-01-16 22:16:42 -0800311 private void setDisplayInfoOverrideFromWindowManagerInternal(
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700312 int displayId, DisplayInfo info) {
313 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700314 LogicalDisplay display = mLogicalDisplays.get(displayId);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700315 if (display != null) {
Jeff Brownef981a42013-08-07 14:13:37 -0700316 if (display.setDisplayInfoOverrideFromWindowManagerLocked(info)) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700317 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
Craig Mautner65d11b32012-10-01 13:59:52 -0700318 scheduleTraversalLocked(false);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700319 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700320 }
321 }
322 }
323
Jeff Brown4ccb8232014-01-16 22:16:42 -0800324 private void performTraversalInTransactionFromWindowManagerInternal() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700325 synchronized (mSyncRoot) {
326 if (!mPendingTraversal) {
327 return;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700328 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700329 mPendingTraversal = false;
330
331 performTraversalInTransactionLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700332 }
Jeff Brown7f3994e2012-12-04 14:04:28 -0800333
334 // List is self-synchronized copy-on-write.
335 for (DisplayTransactionListener listener : mDisplayTransactionListeners) {
336 listener.onDisplayTransaction();
337 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700338 }
339
Jeff Brown5d6443b2015-04-10 20:15:01 -0700340 private void requestGlobalDisplayStateInternal(int state, int brightness) {
341 if (state == Display.STATE_UNKNOWN) {
342 state = Display.STATE_ON;
343 }
344 if (state == Display.STATE_OFF) {
345 brightness = PowerManager.BRIGHTNESS_OFF;
346 } else if (brightness < 0) {
347 brightness = PowerManager.BRIGHTNESS_DEFAULT;
348 } else if (brightness > PowerManager.BRIGHTNESS_ON) {
349 brightness = PowerManager.BRIGHTNESS_ON;
350 }
351
Jeff Browne75926d2014-09-18 15:24:49 -0700352 synchronized (mTempDisplayStateWorkQueue) {
353 try {
354 // Update the display state within the lock.
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700355 // Note that we do not need to schedule traversals here although it
356 // may happen as a side-effect of displays changing state.
Jeff Browne75926d2014-09-18 15:24:49 -0700357 synchronized (mSyncRoot) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700358 if (mGlobalDisplayState == state
359 && mGlobalDisplayBrightness == brightness) {
360 return; // no change
Jeff Browne75926d2014-09-18 15:24:49 -0700361 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700362
363 Trace.traceBegin(Trace.TRACE_TAG_POWER, "requestGlobalDisplayState("
364 + Display.stateToString(state)
365 + ", brightness=" + brightness + ")");
366 mGlobalDisplayState = state;
367 mGlobalDisplayBrightness = brightness;
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700368 applyGlobalDisplayStateLocked(mTempDisplayStateWorkQueue);
Jeff Browne75926d2014-09-18 15:24:49 -0700369 }
370
371 // Setting the display power state can take hundreds of milliseconds
372 // to complete so we defer the most expensive part of the work until
373 // after we have exited the critical section to avoid blocking other
374 // threads for a long time.
375 for (int i = 0; i < mTempDisplayStateWorkQueue.size(); i++) {
376 mTempDisplayStateWorkQueue.get(i).run();
377 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700378 Trace.traceEnd(Trace.TRACE_TAG_POWER);
Jeff Browne75926d2014-09-18 15:24:49 -0700379 } finally {
380 mTempDisplayStateWorkQueue.clear();
Jeff Brown9e316a12012-10-08 19:17:06 -0700381 }
382 }
383 }
384
Jeff Brown4ccb8232014-01-16 22:16:42 -0800385 private DisplayInfo getDisplayInfoInternal(int displayId, int callingUid) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700386 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800387 LogicalDisplay display = mLogicalDisplays.get(displayId);
388 if (display != null) {
389 DisplayInfo info = display.getDisplayInfoLocked();
390 if (info.hasAccess(callingUid)) {
391 return info;
392 }
393 }
394 return null;
395 }
396 }
397
398 private int[] getDisplayIdsInternal(int callingUid) {
399 synchronized (mSyncRoot) {
400 final int count = mLogicalDisplays.size();
401 int[] displayIds = new int[count];
402 int n = 0;
403 for (int i = 0; i < count; i++) {
404 LogicalDisplay display = mLogicalDisplays.valueAt(i);
405 DisplayInfo info = display.getDisplayInfoLocked();
406 if (info.hasAccess(callingUid)) {
407 displayIds[n++] = mLogicalDisplays.keyAt(i);
408 }
409 }
410 if (n != count) {
411 displayIds = Arrays.copyOfRange(displayIds, 0, n);
412 }
413 return displayIds;
414 }
415 }
416
417 private void registerCallbackInternal(IDisplayManagerCallback callback, int callingPid) {
418 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700419 if (mCallbacks.get(callingPid) != null) {
420 throw new SecurityException("The calling process has already "
421 + "registered an IDisplayManagerCallback.");
Jeff Brown64a55af2012-08-26 02:47:39 -0700422 }
423
Jeff Brownbd6e1502012-08-28 03:27:37 -0700424 CallbackRecord record = new CallbackRecord(callingPid, callback);
425 try {
426 IBinder binder = callback.asBinder();
427 binder.linkToDeath(record, 0);
428 } catch (RemoteException ex) {
429 // give up
430 throw new RuntimeException(ex);
431 }
432
433 mCallbacks.put(callingPid, record);
434 }
435 }
436
Jeff Brownce468a32013-11-21 16:42:03 -0800437 private void onCallbackDied(CallbackRecord record) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700438 synchronized (mSyncRoot) {
Jeff Brownce468a32013-11-21 16:42:03 -0800439 mCallbacks.remove(record.mPid);
440 stopWifiDisplayScanLocked(record);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700441 }
442 }
443
Jeff Brown4ccb8232014-01-16 22:16:42 -0800444 private void startWifiDisplayScanInternal(int callingPid) {
445 synchronized (mSyncRoot) {
446 CallbackRecord record = mCallbacks.get(callingPid);
447 if (record == null) {
448 throw new IllegalStateException("The calling process has not "
449 + "registered an IDisplayManagerCallback.");
Jeff Browne08ae382012-09-07 20:36:36 -0700450 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800451 startWifiDisplayScanLocked(record);
Jeff Browne08ae382012-09-07 20:36:36 -0700452 }
453 }
454
Jeff Brownce468a32013-11-21 16:42:03 -0800455 private void startWifiDisplayScanLocked(CallbackRecord record) {
456 if (!record.mWifiDisplayScanRequested) {
457 record.mWifiDisplayScanRequested = true;
458 if (mWifiDisplayScanRequestCount++ == 0) {
459 if (mWifiDisplayAdapter != null) {
460 mWifiDisplayAdapter.requestStartScanLocked();
461 }
462 }
463 }
464 }
465
Jeff Brown4ccb8232014-01-16 22:16:42 -0800466 private void stopWifiDisplayScanInternal(int callingPid) {
467 synchronized (mSyncRoot) {
468 CallbackRecord record = mCallbacks.get(callingPid);
469 if (record == null) {
470 throw new IllegalStateException("The calling process has not "
471 + "registered an IDisplayManagerCallback.");
Jeff Brownce468a32013-11-21 16:42:03 -0800472 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800473 stopWifiDisplayScanLocked(record);
Jeff Brownce468a32013-11-21 16:42:03 -0800474 }
475 }
476
477 private void stopWifiDisplayScanLocked(CallbackRecord record) {
478 if (record.mWifiDisplayScanRequested) {
479 record.mWifiDisplayScanRequested = false;
480 if (--mWifiDisplayScanRequestCount == 0) {
481 if (mWifiDisplayAdapter != null) {
482 mWifiDisplayAdapter.requestStopScanLocked();
483 }
484 } else if (mWifiDisplayScanRequestCount < 0) {
Dianne Hackborn8d051722014-10-01 14:59:58 -0700485 Slog.wtf(TAG, "mWifiDisplayScanRequestCount became negative: "
Jeff Brownce468a32013-11-21 16:42:03 -0800486 + mWifiDisplayScanRequestCount);
487 mWifiDisplayScanRequestCount = 0;
488 }
489 }
490 }
491
Jeff Brown4ccb8232014-01-16 22:16:42 -0800492 private void connectWifiDisplayInternal(String address) {
493 synchronized (mSyncRoot) {
494 if (mWifiDisplayAdapter != null) {
495 mWifiDisplayAdapter.requestConnectLocked(address);
Jeff Browne08ae382012-09-07 20:36:36 -0700496 }
Jeff Browne08ae382012-09-07 20:36:36 -0700497 }
498 }
499
Jeff Brown4ccb8232014-01-16 22:16:42 -0800500 private void pauseWifiDisplayInternal() {
501 synchronized (mSyncRoot) {
502 if (mWifiDisplayAdapter != null) {
503 mWifiDisplayAdapter.requestPauseLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700504 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700505 }
506 }
507
Jeff Brown4ccb8232014-01-16 22:16:42 -0800508 private void resumeWifiDisplayInternal() {
509 synchronized (mSyncRoot) {
510 if (mWifiDisplayAdapter != null) {
511 mWifiDisplayAdapter.requestResumeLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700512 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700513 }
514 }
515
Jeff Brown4ccb8232014-01-16 22:16:42 -0800516 private void disconnectWifiDisplayInternal() {
517 synchronized (mSyncRoot) {
518 if (mWifiDisplayAdapter != null) {
519 mWifiDisplayAdapter.requestDisconnectLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700520 }
Jeff Browne08ae382012-09-07 20:36:36 -0700521 }
522 }
523
Jeff Brown4ccb8232014-01-16 22:16:42 -0800524 private void renameWifiDisplayInternal(String address, String alias) {
525 synchronized (mSyncRoot) {
526 if (mWifiDisplayAdapter != null) {
527 mWifiDisplayAdapter.requestRenameLocked(address, alias);
Jeff Brown89d55462012-09-19 11:33:42 -0700528 }
Jeff Brown89d55462012-09-19 11:33:42 -0700529 }
530 }
531
Jeff Brown4ccb8232014-01-16 22:16:42 -0800532 private void forgetWifiDisplayInternal(String address) {
533 synchronized (mSyncRoot) {
534 if (mWifiDisplayAdapter != null) {
535 mWifiDisplayAdapter.requestForgetLocked(address);
Jeff Brown89d55462012-09-19 11:33:42 -0700536 }
Jeff Brown89d55462012-09-19 11:33:42 -0700537 }
538 }
539
Jeff Brown4ccb8232014-01-16 22:16:42 -0800540 private WifiDisplayStatus getWifiDisplayStatusInternal() {
541 synchronized (mSyncRoot) {
542 if (mWifiDisplayAdapter != null) {
543 return mWifiDisplayAdapter.getWifiDisplayStatusLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700544 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800545 return new WifiDisplayStatus();
Jeff Browne08ae382012-09-07 20:36:36 -0700546 }
547 }
548
Michael Wright1c9977b2016-07-12 13:30:10 -0700549 private void requestColorModeInternal(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +0100550 synchronized (mSyncRoot) {
551 LogicalDisplay display = mLogicalDisplays.get(displayId);
552 if (display != null &&
Michael Wright1c9977b2016-07-12 13:30:10 -0700553 display.getRequestedColorModeLocked() != colorMode) {
554 display.setRequestedColorModeLocked(colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +0100555 scheduleTraversalLocked(false);
556 }
557 }
558 }
559
Michael Wright75ee9fc2014-09-01 19:55:22 -0700560 private int createVirtualDisplayInternal(IVirtualDisplayCallback callback,
Michael Wrightc39d47a2014-07-08 18:07:36 -0700561 IMediaProjection projection, int callingUid, String packageName,
Jeff Brown7d00aff2013-08-02 19:03:49 -0700562 String name, int width, int height, int densityDpi, Surface surface, int flags) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800563 synchronized (mSyncRoot) {
564 if (mVirtualDisplayAdapter == null) {
565 Slog.w(TAG, "Rejecting request to create private virtual display "
566 + "because the virtual display adapter is not available.");
567 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700568 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800569
570 DisplayDevice device = mVirtualDisplayAdapter.createVirtualDisplayLocked(
Michael Wright75ee9fc2014-09-01 19:55:22 -0700571 callback, projection, callingUid, packageName,
Michael Wrightc39d47a2014-07-08 18:07:36 -0700572 name, width, height, densityDpi, surface, flags);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800573 if (device == null) {
574 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700575 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700576
Jeff Brown4ccb8232014-01-16 22:16:42 -0800577 handleDisplayDeviceAddedLocked(device);
578 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
579 if (display != null) {
580 return display.getDisplayIdLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700581 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800582
583 // Something weird happened and the logical display was not created.
584 Slog.w(TAG, "Rejecting request to create virtual display "
585 + "because the logical display was not created.");
Michael Wright75ee9fc2014-09-01 19:55:22 -0700586 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800587 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700588 }
589 return -1;
590 }
591
Michael Wright01e840f2014-06-26 16:03:25 -0700592 private void resizeVirtualDisplayInternal(IBinder appToken,
593 int width, int height, int densityDpi) {
594 synchronized (mSyncRoot) {
595 if (mVirtualDisplayAdapter == null) {
596 return;
597 }
598
599 mVirtualDisplayAdapter.resizeVirtualDisplayLocked(appToken, width, height, densityDpi);
600 }
601 }
602
Jeff Brown92207df2014-04-16 13:16:07 -0700603 private void setVirtualDisplaySurfaceInternal(IBinder appToken, Surface surface) {
604 synchronized (mSyncRoot) {
605 if (mVirtualDisplayAdapter == null) {
606 return;
607 }
608
609 mVirtualDisplayAdapter.setVirtualDisplaySurfaceLocked(appToken, surface);
610 }
611 }
612
Jeff Brown4ccb8232014-01-16 22:16:42 -0800613 private void releaseVirtualDisplayInternal(IBinder appToken) {
614 synchronized (mSyncRoot) {
615 if (mVirtualDisplayAdapter == null) {
616 return;
Jeff Browna506a6e2013-06-04 00:02:38 -0700617 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700618
Jeff Brown4ccb8232014-01-16 22:16:42 -0800619 DisplayDevice device =
620 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(appToken);
621 if (device != null) {
622 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700623 }
624 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700625 }
626
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700627 private void registerDefaultDisplayAdapter() {
628 // Register default display adapter.
629 synchronized (mSyncRoot) {
Mike Lockwoode63f6f72013-11-15 11:01:47 -0800630 registerDisplayAdapterLocked(new LocalDisplayAdapter(
631 mSyncRoot, mContext, mHandler, mDisplayAdapterListener));
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700632 }
633 }
634
635 private void registerAdditionalDisplayAdapters() {
636 synchronized (mSyncRoot) {
637 if (shouldRegisterNonEssentialDisplayAdaptersLocked()) {
Jeff Brown89d55462012-09-19 11:33:42 -0700638 registerOverlayDisplayAdapterLocked();
639 registerWifiDisplayAdapterLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700640 registerVirtualDisplayAdapterLocked();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700641 }
642 }
643 }
644
Jeff Brown89d55462012-09-19 11:33:42 -0700645 private void registerOverlayDisplayAdapterLocked() {
646 registerDisplayAdapterLocked(new OverlayDisplayAdapter(
647 mSyncRoot, mContext, mHandler, mDisplayAdapterListener, mUiHandler));
648 }
649
650 private void registerWifiDisplayAdapterLocked() {
651 if (mContext.getResources().getBoolean(
Jeff Brownbbd28a22012-09-20 16:47:15 -0700652 com.android.internal.R.bool.config_enableWifiDisplay)
653 || SystemProperties.getInt(FORCE_WIFI_DISPLAY_ENABLE, -1) == 1) {
Jeff Brown89d55462012-09-19 11:33:42 -0700654 mWifiDisplayAdapter = new WifiDisplayAdapter(
655 mSyncRoot, mContext, mHandler, mDisplayAdapterListener,
656 mPersistentDataStore);
657 registerDisplayAdapterLocked(mWifiDisplayAdapter);
658 }
659 }
660
Jeff Browna506a6e2013-06-04 00:02:38 -0700661 private void registerVirtualDisplayAdapterLocked() {
662 mVirtualDisplayAdapter = new VirtualDisplayAdapter(
663 mSyncRoot, mContext, mHandler, mDisplayAdapterListener);
664 registerDisplayAdapterLocked(mVirtualDisplayAdapter);
665 }
666
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700667 private boolean shouldRegisterNonEssentialDisplayAdaptersLocked() {
668 // In safe mode, we disable non-essential display adapters to give the user
669 // an opportunity to fix broken settings or other problems that might affect
670 // system stability.
671 // In only-core mode, we disable non-essential display adapters to minimize
672 // the number of dependencies that are started while in this mode and to
673 // prevent problems that might occur due to the device being encrypted.
674 return !mSafeMode && !mOnlyCore;
675 }
676
677 private void registerDisplayAdapterLocked(DisplayAdapter adapter) {
678 mDisplayAdapters.add(adapter);
679 adapter.registerLocked();
680 }
681
Jeff Brownbd6e1502012-08-28 03:27:37 -0700682 private void handleDisplayDeviceAdded(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700683 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700684 handleDisplayDeviceAddedLocked(device);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700685 }
686 }
687
Jeff Browna506a6e2013-06-04 00:02:38 -0700688 private void handleDisplayDeviceAddedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700689 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700690 if (mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700691 Slog.w(TAG, "Attempted to add already added display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700692 return;
693 }
694
Jeff Brown10acf6d2015-04-14 14:20:47 -0700695 Slog.i(TAG, "Display device added: " + info);
696 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700697
698 mDisplayDevices.add(device);
Michael Wright1c9977b2016-07-12 13:30:10 -0700699 LogicalDisplay display = addLogicalDisplayLocked(device);
Jeff Brown0033a862014-10-08 12:06:39 -0700700 Runnable work = updateDisplayStateLocked(device);
701 if (work != null) {
702 work.run();
703 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700704 if (display != null && display.getPrimaryDisplayDeviceLocked() == device) {
705 int colorMode = mPersistentDataStore.getColorMode(device);
706 display.setRequestedColorModeLocked(colorMode);
707 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700708 scheduleTraversalLocked(false);
709 }
710
Jeff Brownbd6e1502012-08-28 03:27:37 -0700711 private void handleDisplayDeviceChanged(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700712 synchronized (mSyncRoot) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700713 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700714 if (!mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700715 Slog.w(TAG, "Attempted to change non-existent display device: " + info);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700716 return;
717 }
718
Jeff Brown10acf6d2015-04-14 14:20:47 -0700719 int diff = device.mDebugLastLoggedDeviceInfo.diff(info);
720 if (diff == DisplayDeviceInfo.DIFF_STATE) {
721 Slog.i(TAG, "Display device changed state: \"" + info.name
722 + "\", " + Display.stateToString(info.state));
723 } else if (diff != 0) {
724 Slog.i(TAG, "Display device changed: " + info);
725 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700726 if ((diff & DisplayDeviceInfo.DIFF_COLOR_MODE) != 0) {
727 try {
728 mPersistentDataStore.setColorMode(device, info.colorMode);
729 } finally {
730 mPersistentDataStore.saveIfNeeded();
731 }
732 }
Jeff Brown10acf6d2015-04-14 14:20:47 -0700733 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browne87bf032012-09-20 18:30:13 -0700734
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700735 device.applyPendingDisplayDeviceInfoChangesLocked();
736 if (updateLogicalDisplaysLocked()) {
Craig Mautner65d11b32012-10-01 13:59:52 -0700737 scheduleTraversalLocked(false);
Jeff Brown64a55af2012-08-26 02:47:39 -0700738 }
739 }
740 }
741
Jeff Brownbd6e1502012-08-28 03:27:37 -0700742 private void handleDisplayDeviceRemoved(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700743 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700744 handleDisplayDeviceRemovedLocked(device);
745 }
746 }
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700747
Jeff Browna506a6e2013-06-04 00:02:38 -0700748 private void handleDisplayDeviceRemovedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700749 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700750 if (!mDisplayDevices.remove(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700751 Slog.w(TAG, "Attempted to remove non-existent display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700752 return;
753 }
754
Jeff Brown10acf6d2015-04-14 14:20:47 -0700755 Slog.i(TAG, "Display device removed: " + info);
756 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700757
Jeff Browna506a6e2013-06-04 00:02:38 -0700758 updateLogicalDisplaysLocked();
759 scheduleTraversalLocked(false);
760 }
761
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700762 private void applyGlobalDisplayStateLocked(List<Runnable> workQueue) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700763 final int count = mDisplayDevices.size();
764 for (int i = 0; i < count; i++) {
765 DisplayDevice device = mDisplayDevices.get(i);
Jeff Browne75926d2014-09-18 15:24:49 -0700766 Runnable runnable = updateDisplayStateLocked(device);
767 if (runnable != null) {
768 workQueue.add(runnable);
769 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700770 }
771 }
772
Jeff Browne75926d2014-09-18 15:24:49 -0700773 private Runnable updateDisplayStateLocked(DisplayDevice device) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700774 // Blank or unblank the display immediately to match the state requested
Jeff Brown037c33e2014-04-09 00:31:55 -0700775 // by the display power controller (if known).
Jeff Browna506a6e2013-06-04 00:02:38 -0700776 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
777 if ((info.flags & DisplayDeviceInfo.FLAG_NEVER_BLANK) == 0) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700778 return device.requestDisplayStateLocked(mGlobalDisplayState, mGlobalDisplayBrightness);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700779 }
Jeff Browne75926d2014-09-18 15:24:49 -0700780 return null;
Craig Mautner4f67ba62012-08-02 11:23:00 -0700781 }
782
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700783 // Adds a new logical display based on the given display device.
784 // Sends notifications if needed.
Michael Wright1c9977b2016-07-12 13:30:10 -0700785 private LogicalDisplay addLogicalDisplayLocked(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700786 DisplayDeviceInfo deviceInfo = device.getDisplayDeviceInfoLocked();
787 boolean isDefault = (deviceInfo.flags
788 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0;
789 if (isDefault && mLogicalDisplays.get(Display.DEFAULT_DISPLAY) != null) {
790 Slog.w(TAG, "Ignoring attempt to add a second default display: " + deviceInfo);
791 isDefault = false;
792 }
793
Jeff Brown27f1d672012-10-17 18:32:34 -0700794 if (!isDefault && mSingleDisplayDemoMode) {
795 Slog.i(TAG, "Not creating a logical display for a secondary display "
796 + " because single display demo mode is enabled: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -0700797 return null;
Jeff Brown27f1d672012-10-17 18:32:34 -0700798 }
799
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700800 final int displayId = assignDisplayIdLocked(isDefault);
801 final int layerStack = assignLayerStackLocked(displayId);
802
Jeff Brownd728bf52012-09-08 18:05:28 -0700803 LogicalDisplay display = new LogicalDisplay(displayId, layerStack, device);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700804 display.updateLocked(mDisplayDevices);
805 if (!display.isValidLocked()) {
806 // This should never happen currently.
807 Slog.w(TAG, "Ignoring display device because the logical display "
808 + "created from it was not considered valid: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -0700809 return null;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700810 }
811
812 mLogicalDisplays.put(displayId, display);
813
814 // Wake up waitForDefaultDisplay.
815 if (isDefault) {
816 mSyncRoot.notifyAll();
817 }
818
819 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_ADDED);
Michael Wright1c9977b2016-07-12 13:30:10 -0700820 return display;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700821 }
822
823 private int assignDisplayIdLocked(boolean isDefault) {
824 return isDefault ? Display.DEFAULT_DISPLAY : mNextNonDefaultDisplayId++;
825 }
826
827 private int assignLayerStackLocked(int displayId) {
828 // Currently layer stacks and display ids are the same.
829 // This need not be the case.
830 return displayId;
831 }
832
833 // Updates all existing logical displays given the current set of display devices.
834 // Removes invalid logical displays.
835 // Sends notifications if needed.
836 private boolean updateLogicalDisplaysLocked() {
837 boolean changed = false;
838 for (int i = mLogicalDisplays.size(); i-- > 0; ) {
839 final int displayId = mLogicalDisplays.keyAt(i);
840 LogicalDisplay display = mLogicalDisplays.valueAt(i);
841
842 mTempDisplayInfo.copyFrom(display.getDisplayInfoLocked());
843 display.updateLocked(mDisplayDevices);
844 if (!display.isValidLocked()) {
845 mLogicalDisplays.removeAt(i);
846 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_REMOVED);
847 changed = true;
848 } else if (!mTempDisplayInfo.equals(display.getDisplayInfoLocked())) {
849 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
850 changed = true;
851 }
852 }
853 return changed;
854 }
855
856 private void performTraversalInTransactionLocked() {
Jeff Brownd728bf52012-09-08 18:05:28 -0700857 // Clear all viewports before configuring displays so that we can keep
858 // track of which ones we have configured.
859 clearViewportsLocked();
860
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700861 // Configure each display device.
862 final int count = mDisplayDevices.size();
863 for (int i = 0; i < count; i++) {
864 DisplayDevice device = mDisplayDevices.get(i);
865 configureDisplayInTransactionLocked(device);
866 device.performTraversalInTransactionLocked();
867 }
Jeff Brownd728bf52012-09-08 18:05:28 -0700868
869 // Tell the input system about these new viewports.
Jeff Brown4ccb8232014-01-16 22:16:42 -0800870 if (mInputManagerInternal != null) {
Jeff Brownd728bf52012-09-08 18:05:28 -0700871 mHandler.sendEmptyMessage(MSG_UPDATE_VIEWPORT);
872 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700873 }
874
Michael Wright3f145a22014-07-22 19:46:03 -0700875 private void setDisplayPropertiesInternal(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700876 float requestedRefreshRate, int requestedModeId, boolean inTraversal) {
Craig Mautner722285e2012-09-07 13:55:58 -0700877 synchronized (mSyncRoot) {
878 LogicalDisplay display = mLogicalDisplays.get(displayId);
Michael Wright3f145a22014-07-22 19:46:03 -0700879 if (display == null) {
880 return;
881 }
882 if (display.hasContentLocked() != hasContent) {
Jeff Brown33041bd2013-08-02 21:11:14 -0700883 if (DEBUG) {
884 Slog.d(TAG, "Display " + displayId + " hasContent flag changed: "
885 + "hasContent=" + hasContent + ", inTraversal=" + inTraversal);
886 }
887
Craig Mautner722285e2012-09-07 13:55:58 -0700888 display.setHasContentLocked(hasContent);
Craig Mautner65d11b32012-10-01 13:59:52 -0700889 scheduleTraversalLocked(inTraversal);
Craig Mautner722285e2012-09-07 13:55:58 -0700890 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700891 if (requestedModeId == 0 && requestedRefreshRate != 0) {
892 // Scan supported modes returned by display.getInfo() to find a mode with the same
893 // size as the default display mode but with the specified refresh rate instead.
894 requestedModeId = display.getDisplayInfoLocked().findDefaultModeByRefreshRate(
895 requestedRefreshRate);
896 }
897 if (display.getRequestedModeIdLocked() != requestedModeId) {
Michael Wright3f145a22014-07-22 19:46:03 -0700898 if (DEBUG) {
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700899 Slog.d(TAG, "Display " + displayId + " switching to mode " + requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -0700900 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700901 display.setRequestedModeIdLocked(requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -0700902 scheduleTraversalLocked(inTraversal);
903 }
Craig Mautner722285e2012-09-07 13:55:58 -0700904 }
Jeff Brownd728bf52012-09-08 18:05:28 -0700905 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700906
Filip Gruszczynskid2e86402015-02-19 13:05:03 -0800907 private void setDisplayOffsetsInternal(int displayId, int x, int y) {
908 synchronized (mSyncRoot) {
909 LogicalDisplay display = mLogicalDisplays.get(displayId);
910 if (display == null) {
911 return;
912 }
913 if (display.getDisplayOffsetXLocked() != x
914 || display.getDisplayOffsetYLocked() != y) {
915 if (DEBUG) {
916 Slog.d(TAG, "Display " + displayId + " burn-in offset set to ("
917 + x + ", " + y + ")");
918 }
919 display.setDisplayOffsetsLocked(x, y);
920 scheduleTraversalLocked(false);
921 }
922 }
923 }
924
Jeff Brownd728bf52012-09-08 18:05:28 -0700925 private void clearViewportsLocked() {
926 mDefaultViewport.valid = false;
927 mExternalTouchViewport.valid = false;
Craig Mautner722285e2012-09-07 13:55:58 -0700928 }
929
930 private void configureDisplayInTransactionLocked(DisplayDevice device) {
Jeff Brownd14c8c92014-01-07 18:13:09 -0800931 final DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
932 final boolean ownContent = (info.flags & DisplayDeviceInfo.FLAG_OWN_CONTENT_ONLY) != 0;
Jeff Browna506a6e2013-06-04 00:02:38 -0700933
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700934 // Find the logical display that the display device is showing.
Jeff Brownd14c8c92014-01-07 18:13:09 -0800935 // Certain displays only ever show their own content.
Craig Mautner722285e2012-09-07 13:55:58 -0700936 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
Jeff Brownd14c8c92014-01-07 18:13:09 -0800937 if (!ownContent) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700938 if (display != null && !display.hasContentLocked()) {
939 // If the display does not have any content of its own, then
940 // automatically mirror the default logical display contents.
941 display = null;
942 }
943 if (display == null) {
944 display = mLogicalDisplays.get(Display.DEFAULT_DISPLAY);
945 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700946 }
947
948 // Apply the logical display configuration to the display device.
949 if (display == null) {
950 // TODO: no logical display for the device, blank it
Jeff Brownd728bf52012-09-08 18:05:28 -0700951 Slog.w(TAG, "Missing logical display to use for physical display device: "
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700952 + device.getDisplayDeviceInfoLocked());
Jeff Brownd728bf52012-09-08 18:05:28 -0700953 return;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700954 }
Jeff Brown037c33e2014-04-09 00:31:55 -0700955 display.configureDisplayInTransactionLocked(device, info.state == Display.STATE_OFF);
Jeff Brownd728bf52012-09-08 18:05:28 -0700956
957 // Update the viewports if needed.
Jeff Brownd728bf52012-09-08 18:05:28 -0700958 if (!mDefaultViewport.valid
959 && (info.flags & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
960 setViewportLocked(mDefaultViewport, display, device);
961 }
962 if (!mExternalTouchViewport.valid
963 && info.touch == DisplayDeviceInfo.TOUCH_EXTERNAL) {
964 setViewportLocked(mExternalTouchViewport, display, device);
965 }
966 }
967
968 private static void setViewportLocked(DisplayViewport viewport,
969 LogicalDisplay display, DisplayDevice device) {
970 viewport.valid = true;
971 viewport.displayId = display.getDisplayIdLocked();
972 device.populateViewportLocked(viewport);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700973 }
974
975 private LogicalDisplay findLogicalDisplayForDeviceLocked(DisplayDevice device) {
976 final int count = mLogicalDisplays.size();
977 for (int i = 0; i < count; i++) {
978 LogicalDisplay display = mLogicalDisplays.valueAt(i);
979 if (display.getPrimaryDisplayDeviceLocked() == device) {
980 return display;
981 }
982 }
983 return null;
984 }
985
Jeff Brownbd6e1502012-08-28 03:27:37 -0700986 private void sendDisplayEventLocked(int displayId, int event) {
987 Message msg = mHandler.obtainMessage(MSG_DELIVER_DISPLAY_EVENT, displayId, event);
988 mHandler.sendMessage(msg);
989 }
990
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700991 // Requests that performTraversalsInTransactionFromWindowManager be called at a
992 // later time to apply changes to surfaces and displays.
Craig Mautner65d11b32012-10-01 13:59:52 -0700993 private void scheduleTraversalLocked(boolean inTraversal) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800994 if (!mPendingTraversal && mWindowManagerInternal != null) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700995 mPendingTraversal = true;
Craig Mautner65d11b32012-10-01 13:59:52 -0700996 if (!inTraversal) {
997 mHandler.sendEmptyMessage(MSG_REQUEST_TRAVERSAL);
998 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700999 }
1000 }
1001
1002 // Runs on Handler thread.
1003 // Delivers display event notifications to callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001004 private void deliverDisplayEvent(int displayId, int event) {
1005 if (DEBUG) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001006 Slog.d(TAG, "Delivering display event: displayId="
1007 + displayId + ", event=" + event);
Jeff Brownfa25bf52012-07-23 19:26:30 -07001008 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07001009
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001010 // Grab the lock and copy the callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001011 final int count;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001012 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001013 count = mCallbacks.size();
1014 mTempCallbacks.clear();
1015 for (int i = 0; i < count; i++) {
1016 mTempCallbacks.add(mCallbacks.valueAt(i));
Craig Mautner4f67ba62012-08-02 11:23:00 -07001017 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001018 }
Craig Mautner4f67ba62012-08-02 11:23:00 -07001019
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001020 // After releasing the lock, send the notifications out.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001021 for (int i = 0; i < count; i++) {
1022 mTempCallbacks.get(i).notifyDisplayEventAsync(displayId, event);
1023 }
1024 mTempCallbacks.clear();
Craig Mautner4f67ba62012-08-02 11:23:00 -07001025 }
1026
Michael Wrightc39d47a2014-07-08 18:07:36 -07001027 private IMediaProjectionManager getProjectionService() {
1028 if (mProjectionService == null) {
1029 IBinder b = ServiceManager.getService(Context.MEDIA_PROJECTION_SERVICE);
1030 mProjectionService = IMediaProjectionManager.Stub.asInterface(b);
1031 }
1032 return mProjectionService;
1033 }
1034
Jeff Brown4ccb8232014-01-16 22:16:42 -08001035 private void dumpInternal(PrintWriter pw) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001036 pw.println("DISPLAY MANAGER (dumpsys display)");
Jeff Brownfa25bf52012-07-23 19:26:30 -07001037
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001038 synchronized (mSyncRoot) {
Jeff Brown9e316a12012-10-08 19:17:06 -07001039 pw.println(" mOnlyCode=" + mOnlyCore);
1040 pw.println(" mSafeMode=" + mSafeMode);
1041 pw.println(" mPendingTraversal=" + mPendingTraversal);
Jeff Brown037c33e2014-04-09 00:31:55 -07001042 pw.println(" mGlobalDisplayState=" + Display.stateToString(mGlobalDisplayState));
Jeff Brown9e316a12012-10-08 19:17:06 -07001043 pw.println(" mNextNonDefaultDisplayId=" + mNextNonDefaultDisplayId);
1044 pw.println(" mDefaultViewport=" + mDefaultViewport);
1045 pw.println(" mExternalTouchViewport=" + mExternalTouchViewport);
Jeff Brown27f1d672012-10-17 18:32:34 -07001046 pw.println(" mSingleDisplayDemoMode=" + mSingleDisplayDemoMode);
Jeff Brownce468a32013-11-21 16:42:03 -08001047 pw.println(" mWifiDisplayScanRequestCount=" + mWifiDisplayScanRequestCount);
Jeff Brown9e316a12012-10-08 19:17:06 -07001048
Jeff Brownbd6e1502012-08-28 03:27:37 -07001049 IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001050 ipw.increaseIndent();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001051
1052 pw.println();
1053 pw.println("Display Adapters: size=" + mDisplayAdapters.size());
Jeff Brown848c2dc2012-08-19 20:18:08 -07001054 for (DisplayAdapter adapter : mDisplayAdapters) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001055 pw.println(" " + adapter.getName());
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001056 adapter.dumpLocked(ipw);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001057 }
Craig Mautner9de49362012-08-02 14:30:30 -07001058
Jeff Brownbd6e1502012-08-28 03:27:37 -07001059 pw.println();
1060 pw.println("Display Devices: size=" + mDisplayDevices.size());
1061 for (DisplayDevice device : mDisplayDevices) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001062 pw.println(" " + device.getDisplayDeviceInfoLocked());
1063 device.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001064 }
1065
1066 final int logicalDisplayCount = mLogicalDisplays.size();
1067 pw.println();
1068 pw.println("Logical Displays: size=" + logicalDisplayCount);
1069 for (int i = 0; i < logicalDisplayCount; i++) {
1070 int displayId = mLogicalDisplays.keyAt(i);
1071 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1072 pw.println(" Display " + displayId + ":");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001073 display.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001074 }
Jeff Brownce468a32013-11-21 16:42:03 -08001075
1076 final int callbackCount = mCallbacks.size();
1077 pw.println();
1078 pw.println("Callbacks: size=" + callbackCount);
1079 for (int i = 0; i < callbackCount; i++) {
1080 CallbackRecord callback = mCallbacks.valueAt(i);
1081 pw.println(" " + i + ": mPid=" + callback.mPid
1082 + ", mWifiDisplayScanRequested=" + callback.mWifiDisplayScanRequested);
1083 }
Jeff Brownad9ef192014-04-08 17:26:30 -07001084
1085 if (mDisplayPowerController != null) {
1086 mDisplayPowerController.dump(pw);
1087 }
Michael Wright1c9977b2016-07-12 13:30:10 -07001088
1089 pw.println();
1090 mPersistentDataStore.dump(pw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001091 }
1092 }
1093
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001094 /**
1095 * This is the object that everything in the display manager locks on.
1096 * We make it an inner class within the {@link DisplayManagerService} to so that it is
1097 * clear that the object belongs to the display manager service and that it is
1098 * a unique object with a special purpose.
1099 */
1100 public static final class SyncRoot {
1101 }
1102
Jeff Brownbd6e1502012-08-28 03:27:37 -07001103 private final class DisplayManagerHandler extends Handler {
1104 public DisplayManagerHandler(Looper looper) {
1105 super(looper, null, true /*async*/);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001106 }
Jeff Brownbf5740e2012-08-19 23:20:02 -07001107
Jeff Brownbd6e1502012-08-28 03:27:37 -07001108 @Override
1109 public void handleMessage(Message msg) {
1110 switch (msg.what) {
1111 case MSG_REGISTER_DEFAULT_DISPLAY_ADAPTER:
1112 registerDefaultDisplayAdapter();
1113 break;
1114
1115 case MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS:
1116 registerAdditionalDisplayAdapters();
1117 break;
1118
1119 case MSG_DELIVER_DISPLAY_EVENT:
1120 deliverDisplayEvent(msg.arg1, msg.arg2);
1121 break;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001122
1123 case MSG_REQUEST_TRAVERSAL:
Jeff Brown4ccb8232014-01-16 22:16:42 -08001124 mWindowManagerInternal.requestTraversalFromDisplayManager();
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001125 break;
Jeff Brownd728bf52012-09-08 18:05:28 -07001126
1127 case MSG_UPDATE_VIEWPORT: {
1128 synchronized (mSyncRoot) {
1129 mTempDefaultViewport.copyFrom(mDefaultViewport);
1130 mTempExternalTouchViewport.copyFrom(mExternalTouchViewport);
1131 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001132 mInputManagerInternal.setDisplayViewports(
Jeff Brownd728bf52012-09-08 18:05:28 -07001133 mTempDefaultViewport, mTempExternalTouchViewport);
1134 break;
1135 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001136 }
1137 }
1138 }
1139
1140 private final class DisplayAdapterListener implements DisplayAdapter.Listener {
1141 @Override
1142 public void onDisplayDeviceEvent(DisplayDevice device, int event) {
1143 switch (event) {
1144 case DisplayAdapter.DISPLAY_DEVICE_EVENT_ADDED:
1145 handleDisplayDeviceAdded(device);
1146 break;
1147
1148 case DisplayAdapter.DISPLAY_DEVICE_EVENT_CHANGED:
1149 handleDisplayDeviceChanged(device);
1150 break;
1151
1152 case DisplayAdapter.DISPLAY_DEVICE_EVENT_REMOVED:
1153 handleDisplayDeviceRemoved(device);
1154 break;
1155 }
1156 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001157
1158 @Override
1159 public void onTraversalRequested() {
1160 synchronized (mSyncRoot) {
Craig Mautner65d11b32012-10-01 13:59:52 -07001161 scheduleTraversalLocked(false);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001162 }
1163 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001164 }
1165
1166 private final class CallbackRecord implements DeathRecipient {
Jeff Brownce468a32013-11-21 16:42:03 -08001167 public final int mPid;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001168 private final IDisplayManagerCallback mCallback;
1169
Jeff Brownce468a32013-11-21 16:42:03 -08001170 public boolean mWifiDisplayScanRequested;
1171
Jeff Brownbd6e1502012-08-28 03:27:37 -07001172 public CallbackRecord(int pid, IDisplayManagerCallback callback) {
1173 mPid = pid;
1174 mCallback = callback;
1175 }
1176
1177 @Override
1178 public void binderDied() {
1179 if (DEBUG) {
1180 Slog.d(TAG, "Display listener for pid " + mPid + " died.");
1181 }
Jeff Brownce468a32013-11-21 16:42:03 -08001182 onCallbackDied(this);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001183 }
1184
1185 public void notifyDisplayEventAsync(int displayId, int event) {
1186 try {
1187 mCallback.onDisplayEvent(displayId, event);
1188 } catch (RemoteException ex) {
1189 Slog.w(TAG, "Failed to notify process "
1190 + mPid + " that displays changed, assuming it died.", ex);
1191 binderDied();
1192 }
1193 }
1194 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001195
1196 private final class BinderService extends IDisplayManager.Stub {
1197 /**
1198 * Returns information about the specified logical display.
1199 *
1200 * @param displayId The logical display id.
1201 * @return The logical display info, or null if the display does not exist. The
1202 * returned object must be treated as immutable.
1203 */
1204 @Override // Binder call
1205 public DisplayInfo getDisplayInfo(int displayId) {
1206 final int callingUid = Binder.getCallingUid();
1207 final long token = Binder.clearCallingIdentity();
1208 try {
1209 return getDisplayInfoInternal(displayId, callingUid);
1210 } finally {
1211 Binder.restoreCallingIdentity(token);
1212 }
1213 }
1214
1215 /**
1216 * Returns the list of all display ids.
1217 */
1218 @Override // Binder call
1219 public int[] getDisplayIds() {
1220 final int callingUid = Binder.getCallingUid();
1221 final long token = Binder.clearCallingIdentity();
1222 try {
1223 return getDisplayIdsInternal(callingUid);
1224 } finally {
1225 Binder.restoreCallingIdentity(token);
1226 }
1227 }
1228
1229 @Override // Binder call
1230 public void registerCallback(IDisplayManagerCallback callback) {
1231 if (callback == null) {
1232 throw new IllegalArgumentException("listener must not be null");
1233 }
1234
1235 final int callingPid = Binder.getCallingPid();
1236 final long token = Binder.clearCallingIdentity();
1237 try {
1238 registerCallbackInternal(callback, callingPid);
1239 } finally {
1240 Binder.restoreCallingIdentity(token);
1241 }
1242 }
1243
1244 @Override // Binder call
1245 public void startWifiDisplayScan() {
1246 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1247 "Permission required to start wifi display scans");
1248
1249 final int callingPid = Binder.getCallingPid();
1250 final long token = Binder.clearCallingIdentity();
1251 try {
1252 startWifiDisplayScanInternal(callingPid);
1253 } finally {
1254 Binder.restoreCallingIdentity(token);
1255 }
1256 }
1257
1258 @Override // Binder call
1259 public void stopWifiDisplayScan() {
1260 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1261 "Permission required to stop wifi display scans");
1262
1263 final int callingPid = Binder.getCallingPid();
1264 final long token = Binder.clearCallingIdentity();
1265 try {
1266 stopWifiDisplayScanInternal(callingPid);
1267 } finally {
1268 Binder.restoreCallingIdentity(token);
1269 }
1270 }
1271
1272 @Override // Binder call
1273 public void connectWifiDisplay(String address) {
1274 if (address == null) {
1275 throw new IllegalArgumentException("address must not be null");
1276 }
1277 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1278 "Permission required to connect to a wifi display");
1279
1280 final long token = Binder.clearCallingIdentity();
1281 try {
1282 connectWifiDisplayInternal(address);
1283 } finally {
1284 Binder.restoreCallingIdentity(token);
1285 }
1286 }
1287
1288 @Override // Binder call
1289 public void disconnectWifiDisplay() {
1290 // This request does not require special permissions.
1291 // Any app can request disconnection from the currently active wifi display.
1292 // This exception should no longer be needed once wifi display control moves
1293 // to the media router service.
1294
1295 final long token = Binder.clearCallingIdentity();
1296 try {
1297 disconnectWifiDisplayInternal();
1298 } finally {
1299 Binder.restoreCallingIdentity(token);
1300 }
1301 }
1302
1303 @Override // Binder call
1304 public void renameWifiDisplay(String address, String alias) {
1305 if (address == null) {
1306 throw new IllegalArgumentException("address must not be null");
1307 }
1308 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1309 "Permission required to rename to a wifi display");
1310
1311 final long token = Binder.clearCallingIdentity();
1312 try {
1313 renameWifiDisplayInternal(address, alias);
1314 } finally {
1315 Binder.restoreCallingIdentity(token);
1316 }
1317 }
1318
1319 @Override // Binder call
1320 public void forgetWifiDisplay(String address) {
1321 if (address == null) {
1322 throw new IllegalArgumentException("address must not be null");
1323 }
1324 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1325 "Permission required to forget to a wifi display");
1326
1327 final long token = Binder.clearCallingIdentity();
1328 try {
1329 forgetWifiDisplayInternal(address);
1330 } finally {
1331 Binder.restoreCallingIdentity(token);
1332 }
1333 }
1334
1335 @Override // Binder call
1336 public void pauseWifiDisplay() {
1337 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1338 "Permission required to pause a wifi display session");
1339
1340 final long token = Binder.clearCallingIdentity();
1341 try {
1342 pauseWifiDisplayInternal();
1343 } finally {
1344 Binder.restoreCallingIdentity(token);
1345 }
1346 }
1347
1348 @Override // Binder call
1349 public void resumeWifiDisplay() {
1350 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1351 "Permission required to resume a wifi display session");
1352
1353 final long token = Binder.clearCallingIdentity();
1354 try {
1355 resumeWifiDisplayInternal();
1356 } finally {
1357 Binder.restoreCallingIdentity(token);
1358 }
1359 }
1360
1361 @Override // Binder call
1362 public WifiDisplayStatus getWifiDisplayStatus() {
1363 // This request does not require special permissions.
1364 // Any app can get information about available wifi displays.
1365
1366 final long token = Binder.clearCallingIdentity();
1367 try {
1368 return getWifiDisplayStatusInternal();
1369 } finally {
1370 Binder.restoreCallingIdentity(token);
1371 }
1372 }
1373
1374 @Override // Binder call
Michael Wright1c9977b2016-07-12 13:30:10 -07001375 public void requestColorMode(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +01001376 mContext.enforceCallingOrSelfPermission(
Michael Wright1c9977b2016-07-12 13:30:10 -07001377 Manifest.permission.CONFIGURE_DISPLAY_COLOR_MODE,
1378 "Permission required to change the display color mode");
Michael Wright58e829f2015-09-15 00:13:26 +01001379 final long token = Binder.clearCallingIdentity();
1380 try {
Michael Wright1c9977b2016-07-12 13:30:10 -07001381 requestColorModeInternal(displayId, colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +01001382 } finally {
1383 Binder.restoreCallingIdentity(token);
1384 }
1385 }
1386
1387 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001388 public int createVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wrightc39d47a2014-07-08 18:07:36 -07001389 IMediaProjection projection, String packageName, String name,
1390 int width, int height, int densityDpi, Surface surface, int flags) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001391 final int callingUid = Binder.getCallingUid();
1392 if (!validatePackageName(callingUid, packageName)) {
1393 throw new SecurityException("packageName must match the calling uid");
1394 }
Michael Wright75ee9fc2014-09-01 19:55:22 -07001395 if (callback == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001396 throw new IllegalArgumentException("appToken must not be null");
1397 }
1398 if (TextUtils.isEmpty(name)) {
1399 throw new IllegalArgumentException("name must be non-null and non-empty");
1400 }
1401 if (width <= 0 || height <= 0 || densityDpi <= 0) {
1402 throw new IllegalArgumentException("width, height, and densityDpi must be "
1403 + "greater than 0");
1404 }
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001405 if (surface != null && surface.isSingleBuffered()) {
Pablo Ceballosaff2f942016-07-29 14:49:55 -07001406 throw new IllegalArgumentException("Surface can't be single-buffered");
1407 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001408
Michael Wright6720be42014-07-29 19:14:16 -07001409 if ((flags & DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC) != 0) {
1410 flags |= DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
1411 }
1412 if ((flags & DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY) != 0) {
1413 flags &= ~DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
1414 }
1415
Michael Wrightc39d47a2014-07-08 18:07:36 -07001416 if (projection != null) {
1417 try {
1418 if (!getProjectionService().isValidMediaProjection(projection)) {
1419 throw new SecurityException("Invalid media projection");
1420 }
Michael Wright6720be42014-07-29 19:14:16 -07001421 flags = projection.applyVirtualDisplayFlags(flags);
Michael Wrightc39d47a2014-07-08 18:07:36 -07001422 } catch (RemoteException e) {
Michael Wright6720be42014-07-29 19:14:16 -07001423 throw new SecurityException("unable to validate media projection or flags");
Michael Wrightc39d47a2014-07-08 18:07:36 -07001424 }
1425 }
1426
Michael Wright6720be42014-07-29 19:14:16 -07001427 if (callingUid != Process.SYSTEM_UID &&
1428 (flags & DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001429 if (!canProjectVideo(projection)) {
1430 throw new SecurityException("Requires CAPTURE_VIDEO_OUTPUT or "
1431 + "CAPTURE_SECURE_VIDEO_OUTPUT permission, or an appropriate "
1432 + "MediaProjection token in order to create a screen sharing virtual "
1433 + "display.");
1434 }
1435 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001436 if ((flags & DisplayManager.VIRTUAL_DISPLAY_FLAG_SECURE) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001437 if (!canProjectSecureVideo(projection)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001438 throw new SecurityException("Requires CAPTURE_SECURE_VIDEO_OUTPUT "
Michael Wrightc39d47a2014-07-08 18:07:36 -07001439 + "or an appropriate MediaProjection token to create a "
1440 + "secure virtual display.");
Jeff Brown4ccb8232014-01-16 22:16:42 -08001441 }
1442 }
1443
1444 final long token = Binder.clearCallingIdentity();
1445 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001446 return createVirtualDisplayInternal(callback, projection, callingUid,
Michael Wrightc39d47a2014-07-08 18:07:36 -07001447 packageName, name, width, height, densityDpi, surface, flags);
Jeff Brown4ccb8232014-01-16 22:16:42 -08001448 } finally {
1449 Binder.restoreCallingIdentity(token);
1450 }
1451 }
1452
1453 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001454 public void resizeVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wright01e840f2014-06-26 16:03:25 -07001455 int width, int height, int densityDpi) {
1456 final long token = Binder.clearCallingIdentity();
1457 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001458 resizeVirtualDisplayInternal(callback.asBinder(), width, height, densityDpi);
Michael Wright01e840f2014-06-26 16:03:25 -07001459 } finally {
1460 Binder.restoreCallingIdentity(token);
1461 }
1462 }
1463
1464 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001465 public void setVirtualDisplaySurface(IVirtualDisplayCallback callback, Surface surface) {
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001466 if (surface != null && surface.isSingleBuffered()) {
1467 throw new IllegalArgumentException("Surface can't be single-buffered");
1468 }
Jeff Brown92207df2014-04-16 13:16:07 -07001469 final long token = Binder.clearCallingIdentity();
1470 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001471 setVirtualDisplaySurfaceInternal(callback.asBinder(), surface);
Jeff Brown92207df2014-04-16 13:16:07 -07001472 } finally {
1473 Binder.restoreCallingIdentity(token);
1474 }
1475 }
1476
1477 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001478 public void releaseVirtualDisplay(IVirtualDisplayCallback callback) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001479 final long token = Binder.clearCallingIdentity();
1480 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001481 releaseVirtualDisplayInternal(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -08001482 } finally {
1483 Binder.restoreCallingIdentity(token);
1484 }
1485 }
1486
1487 @Override // Binder call
1488 public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) {
1489 if (mContext == null
1490 || mContext.checkCallingOrSelfPermission(Manifest.permission.DUMP)
1491 != PackageManager.PERMISSION_GRANTED) {
1492 pw.println("Permission Denial: can't dump DisplayManager from from pid="
1493 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
1494 return;
1495 }
1496
1497 final long token = Binder.clearCallingIdentity();
1498 try {
1499 dumpInternal(pw);
1500 } finally {
1501 Binder.restoreCallingIdentity(token);
1502 }
1503 }
1504
1505 private boolean validatePackageName(int uid, String packageName) {
1506 if (packageName != null) {
1507 String[] packageNames = mContext.getPackageManager().getPackagesForUid(uid);
1508 if (packageNames != null) {
1509 for (String n : packageNames) {
1510 if (n.equals(packageName)) {
1511 return true;
1512 }
1513 }
1514 }
1515 }
1516 return false;
1517 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001518
1519 private boolean canProjectVideo(IMediaProjection projection) {
1520 if (projection != null) {
1521 try {
1522 if (projection.canProjectVideo()) {
1523 return true;
1524 }
1525 } catch (RemoteException e) {
1526 Slog.e(TAG, "Unable to query projection service for permissions", e);
1527 }
1528 }
1529 if (mContext.checkCallingPermission(
1530 android.Manifest.permission.CAPTURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07001531 == PackageManager.PERMISSION_GRANTED) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001532 return true;
1533 }
1534 return canProjectSecureVideo(projection);
1535 }
1536
1537 private boolean canProjectSecureVideo(IMediaProjection projection) {
1538 if (projection != null) {
1539 try {
1540 if (projection.canProjectSecureVideo()){
1541 return true;
1542 }
1543 } catch (RemoteException e) {
1544 Slog.e(TAG, "Unable to query projection service for permissions", e);
1545 }
1546 }
1547 return mContext.checkCallingPermission(
1548 android.Manifest.permission.CAPTURE_SECURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07001549 == PackageManager.PERMISSION_GRANTED;
Michael Wrightc39d47a2014-07-08 18:07:36 -07001550 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001551 }
1552
1553 private final class LocalService extends DisplayManagerInternal {
1554 @Override
Jeff Brown037c33e2014-04-09 00:31:55 -07001555 public void initPowerManagement(final DisplayPowerCallbacks callbacks, Handler handler,
Jeff Brownad9ef192014-04-08 17:26:30 -07001556 SensorManager sensorManager) {
1557 synchronized (mSyncRoot) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001558 DisplayBlanker blanker = new DisplayBlanker() {
1559 @Override
Jeff Brown5d6443b2015-04-10 20:15:01 -07001560 public void requestDisplayState(int state, int brightness) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001561 // The order of operations is important for legacy reasons.
1562 if (state == Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07001563 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07001564 }
1565
1566 callbacks.onDisplayStateChange(state);
1567
1568 if (state != Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07001569 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07001570 }
1571 }
1572 };
Jeff Brownad9ef192014-04-08 17:26:30 -07001573 mDisplayPowerController = new DisplayPowerController(
Jeff Brown037c33e2014-04-09 00:31:55 -07001574 mContext, callbacks, handler, sensorManager, blanker);
Jeff Brownad9ef192014-04-08 17:26:30 -07001575 }
1576 }
1577
1578 @Override
1579 public boolean requestPowerState(DisplayPowerRequest request,
1580 boolean waitForNegativeProximity) {
1581 return mDisplayPowerController.requestPowerState(request,
1582 waitForNegativeProximity);
1583 }
1584
1585 @Override
1586 public boolean isProximitySensorAvailable() {
1587 return mDisplayPowerController.isProximitySensorAvailable();
1588 }
1589
1590 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08001591 public DisplayInfo getDisplayInfo(int displayId) {
1592 return getDisplayInfoInternal(displayId, Process.myUid());
1593 }
1594
1595 @Override
1596 public void registerDisplayTransactionListener(DisplayTransactionListener listener) {
1597 if (listener == null) {
1598 throw new IllegalArgumentException("listener must not be null");
1599 }
1600
1601 registerDisplayTransactionListenerInternal(listener);
1602 }
1603
1604 @Override
1605 public void unregisterDisplayTransactionListener(DisplayTransactionListener listener) {
1606 if (listener == null) {
1607 throw new IllegalArgumentException("listener must not be null");
1608 }
1609
1610 unregisterDisplayTransactionListenerInternal(listener);
1611 }
1612
1613 @Override
1614 public void setDisplayInfoOverrideFromWindowManager(int displayId, DisplayInfo info) {
1615 setDisplayInfoOverrideFromWindowManagerInternal(displayId, info);
1616 }
1617
1618 @Override
1619 public void performTraversalInTransactionFromWindowManager() {
1620 performTraversalInTransactionFromWindowManagerInternal();
1621 }
1622
1623 @Override
Michael Wright3f145a22014-07-22 19:46:03 -07001624 public void setDisplayProperties(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001625 float requestedRefreshRate, int requestedMode, boolean inTraversal) {
1626 setDisplayPropertiesInternal(displayId, hasContent, requestedRefreshRate,
1627 requestedMode, inTraversal);
Jeff Brown4ccb8232014-01-16 22:16:42 -08001628 }
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08001629
1630 @Override
1631 public void setDisplayOffsets(int displayId, int x, int y) {
1632 setDisplayOffsetsInternal(displayId, x, y);
1633 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001634 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07001635}