blob: cd07793241942fe83ceeb6f317f42f564844891c [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;
Andrii Kulianfb1bf692017-01-17 11:17:34 -080051import android.util.IntArray;
Jeff Brownbd6e1502012-08-28 03:27:37 -070052import android.util.Slog;
53import android.util.SparseArray;
Jeff Brownfa25bf52012-07-23 19:26:30 -070054import android.view.Display;
55import android.view.DisplayInfo;
Jeff Browna506a6e2013-06-04 00:02:38 -070056import android.view.Surface;
Jeff Brown4ccb8232014-01-16 22:16:42 -080057import android.view.WindowManagerInternal;
Jeff Browna506a6e2013-06-04 00:02:38 -070058
Jeff Brown4ccb8232014-01-16 22:16:42 -080059import com.android.server.DisplayThread;
60import com.android.server.LocalServices;
61import com.android.server.SystemService;
Dianne Hackborn8d044e82013-04-30 17:24:15 -070062import com.android.server.UiThread;
Jeff Brownfa25bf52012-07-23 19:26:30 -070063
64import java.io.FileDescriptor;
65import java.io.PrintWriter;
66import java.util.ArrayList;
Jeff Browna506a6e2013-06-04 00:02:38 -070067import java.util.Arrays;
Jeff Browne75926d2014-09-18 15:24:49 -070068import java.util.List;
Jeff Brown7f3994e2012-12-04 14:04:28 -080069import java.util.concurrent.CopyOnWriteArrayList;
Jeff Brownfa25bf52012-07-23 19:26:30 -070070
71/**
Jeff Brownbd6e1502012-08-28 03:27:37 -070072 * Manages attached displays.
Jeff Brownfa25bf52012-07-23 19:26:30 -070073 * <p>
Jeff Brownbd6e1502012-08-28 03:27:37 -070074 * The {@link DisplayManagerService} manages the global lifecycle of displays,
75 * decides how to configure logical displays based on the physical display devices currently
76 * attached, sends notifications to the system and to applications when the state
77 * changes, and so on.
78 * </p><p>
79 * The display manager service relies on a collection of {@link DisplayAdapter} components,
80 * for discovering and configuring physical display devices attached to the system.
81 * There are separate display adapters for each manner that devices are attached:
82 * one display adapter for built-in local displays, one for simulated non-functional
83 * displays when the system is headless, one for simulated overlay displays used for
84 * development, one for wifi displays, etc.
85 * </p><p>
86 * Display adapters are only weakly coupled to the display manager service.
87 * Display adapters communicate changes in display device state to the display manager
Craig Mautner722285e2012-09-07 13:55:58 -070088 * service asynchronously via a {@link DisplayAdapter.Listener} registered
Jeff Brownbd6e1502012-08-28 03:27:37 -070089 * by the display manager service. This separation of concerns is important for
90 * two main reasons. First, it neatly encapsulates the responsibilities of these
91 * two classes: display adapters handle individual display devices whereas
92 * the display manager service handles the global state. Second, it eliminates
93 * the potential for deadlocks resulting from asynchronous display device discovery.
Jeff Brown4ed8fe72012-08-30 18:18:29 -070094 * </p>
95 *
96 * <h3>Synchronization</h3>
97 * <p>
98 * Because the display manager may be accessed by multiple threads, the synchronization
99 * story gets a little complicated. In particular, the window manager may call into
100 * the display manager while holding a surface transaction with the expectation that
101 * it can apply changes immediately. Unfortunately, that means we can't just do
102 * everything asynchronously (*grump*).
Jeff Brownbd6e1502012-08-28 03:27:37 -0700103 * </p><p>
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700104 * To make this work, all of the objects that belong to the display manager must
105 * use the same lock. We call this lock the synchronization root and it has a unique
106 * type {@link DisplayManagerService.SyncRoot}. Methods that require this lock are
107 * named with the "Locked" suffix.
108 * </p><p>
109 * Where things get tricky is that the display manager is not allowed to make
110 * any potentially reentrant calls, especially into the window manager. We generally
111 * avoid this by making all potentially reentrant out-calls asynchronous.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700112 * </p>
113 */
Jeff Brown4ccb8232014-01-16 22:16:42 -0800114public final class DisplayManagerService extends SystemService {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700115 private static final String TAG = "DisplayManagerService";
Jeff Brownbd6e1502012-08-28 03:27:37 -0700116 private static final boolean DEBUG = false;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700117
Jeff Brownbbd28a22012-09-20 16:47:15 -0700118 // When this system property is set to 0, WFD is forcibly disabled on boot.
119 // When this system property is set to 1, WFD is forcibly enabled on boot.
120 // Otherwise WFD is enabled according to the value of config_enableWifiDisplay.
121 private static final String FORCE_WIFI_DISPLAY_ENABLE = "persist.debug.wfd.enable";
122
Jeff Brownbd6e1502012-08-28 03:27:37 -0700123 private static final long WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT = 10000;
124
125 private static final int MSG_REGISTER_DEFAULT_DISPLAY_ADAPTER = 1;
126 private static final int MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS = 2;
127 private static final int MSG_DELIVER_DISPLAY_EVENT = 3;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700128 private static final int MSG_REQUEST_TRAVERSAL = 4;
Jeff Brownd728bf52012-09-08 18:05:28 -0700129 private static final int MSG_UPDATE_VIEWPORT = 5;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700130
Jeff Brownb880d882014-02-10 19:47:07 -0800131 private final Context mContext;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700132 private final DisplayManagerHandler mHandler;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700133 private final Handler mUiHandler;
134 private final DisplayAdapterListener mDisplayAdapterListener;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800135 private WindowManagerInternal mWindowManagerInternal;
136 private InputManagerInternal mInputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -0700137 private IMediaProjectionManager mProjectionService;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700138
139 // The synchronization root for the display manager.
140 // This lock guards most of the display manager's state.
Craig Mautner0bf6ec92012-12-18 08:33:27 -0800141 // NOTE: This is synchronized on while holding WindowManagerService.mWindowMap so never call
142 // into WindowManagerService methods that require mWindowMap while holding this unless you are
143 // very very sure that no deadlock can occur.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700144 private final SyncRoot mSyncRoot = new SyncRoot();
145
146 // True if in safe mode.
147 // This option may disable certain display adapters.
148 public boolean mSafeMode;
149
150 // True if we are in a special boot mode where only core applications and
151 // services should be started. This option may disable certain display adapters.
152 public boolean mOnlyCore;
153
Jeff Brown27f1d672012-10-17 18:32:34 -0700154 // True if the display manager service should pretend there is only one display
155 // and only tell applications about the existence of the default logical display.
156 // The display manager can still mirror content to secondary displays but applications
157 // cannot present unique content on those displays.
158 // Used for demonstration purposes only.
159 private final boolean mSingleDisplayDemoMode;
160
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700161 // All callback records indexed by calling process id.
162 public final SparseArray<CallbackRecord> mCallbacks =
Jeff Brownbd6e1502012-08-28 03:27:37 -0700163 new SparseArray<CallbackRecord>();
Jeff Brownfa25bf52012-07-23 19:26:30 -0700164
Jeff Brownbd6e1502012-08-28 03:27:37 -0700165 // List of all currently registered display adapters.
166 private final ArrayList<DisplayAdapter> mDisplayAdapters = new ArrayList<DisplayAdapter>();
167
168 // List of all currently connected display devices.
169 private final ArrayList<DisplayDevice> mDisplayDevices = new ArrayList<DisplayDevice>();
170
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700171 // List of all logical displays indexed by logical display id.
172 private final SparseArray<LogicalDisplay> mLogicalDisplays =
173 new SparseArray<LogicalDisplay>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700174 private int mNextNonDefaultDisplayId = Display.DEFAULT_DISPLAY + 1;
175
Jeff Brown7f3994e2012-12-04 14:04:28 -0800176 // List of all display transaction listeners.
177 private final CopyOnWriteArrayList<DisplayTransactionListener> mDisplayTransactionListeners =
178 new CopyOnWriteArrayList<DisplayTransactionListener>();
179
Jeff Brownad9ef192014-04-08 17:26:30 -0700180 // Display power controller.
181 private DisplayPowerController mDisplayPowerController;
182
Jeff Brown037c33e2014-04-09 00:31:55 -0700183 // The overall display state, independent of changes that might influence one
184 // display or another in particular.
Jeff Brown5d6443b2015-04-10 20:15:01 -0700185 private int mGlobalDisplayState = Display.STATE_ON;
186
187 // The overall display brightness.
188 // For now, this only applies to the built-in display but we may split it up eventually.
189 private int mGlobalDisplayBrightness = PowerManager.BRIGHTNESS_DEFAULT;
Jeff Brown9e316a12012-10-08 19:17:06 -0700190
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700191 // Set to true when there are pending display changes that have yet to be applied
192 // to the surface flinger state.
193 private boolean mPendingTraversal;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700194
Jeff Browne08ae382012-09-07 20:36:36 -0700195 // The Wifi display adapter, or null if not registered.
196 private WifiDisplayAdapter mWifiDisplayAdapter;
197
Jeff Brownce468a32013-11-21 16:42:03 -0800198 // The number of active wifi display scan requests.
199 private int mWifiDisplayScanRequestCount;
200
Jeff Browna506a6e2013-06-04 00:02:38 -0700201 // The virtual display adapter, or null if not registered.
202 private VirtualDisplayAdapter mVirtualDisplayAdapter;
203
Jeff Brownd728bf52012-09-08 18:05:28 -0700204 // Viewports of the default display and the display that should receive touch
205 // input from an external source. Used by the input system.
206 private final DisplayViewport mDefaultViewport = new DisplayViewport();
207 private final DisplayViewport mExternalTouchViewport = new DisplayViewport();
208
Jeff Brown89d55462012-09-19 11:33:42 -0700209 // Persistent data store for all internal settings maintained by the display manager service.
210 private final PersistentDataStore mPersistentDataStore = new PersistentDataStore();
211
Jeff Brownbd6e1502012-08-28 03:27:37 -0700212 // Temporary callback list, used when sending display events to applications.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700213 // May be used outside of the lock but only on the handler thread.
214 private final ArrayList<CallbackRecord> mTempCallbacks = new ArrayList<CallbackRecord>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700215
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700216 // Temporary display info, used for comparing display configurations.
217 private final DisplayInfo mTempDisplayInfo = new DisplayInfo();
218
Jeff Brownd728bf52012-09-08 18:05:28 -0700219 // Temporary viewports, used when sending new viewport information to the
220 // input system. May be used outside of the lock but only on the handler thread.
221 private final DisplayViewport mTempDefaultViewport = new DisplayViewport();
222 private final DisplayViewport mTempExternalTouchViewport = new DisplayViewport();
223
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700224 // The default color mode for default displays. Overrides the usual
225 // Display.Display.COLOR_MODE_DEFAULT for displays with the
226 // DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY flag set.
227 private final int mDefaultDisplayDefaultColorMode;
228
Jeff Browne75926d2014-09-18 15:24:49 -0700229 // Temporary list of deferred work to perform when setting the display state.
230 // Only used by requestDisplayState. The field is self-synchronized and only
231 // intended for use inside of the requestGlobalDisplayStateInternal function.
232 private final ArrayList<Runnable> mTempDisplayStateWorkQueue = new ArrayList<Runnable>();
233
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800234 // Lists of UIDs that are present on the displays. Maps displayId -> array of UIDs.
235 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
236
Jeff Brownb880d882014-02-10 19:47:07 -0800237 public DisplayManagerService(Context context) {
238 super(context);
239 mContext = context;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800240 mHandler = new DisplayManagerHandler(DisplayThread.get().getLooper());
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700241 mUiHandler = UiThread.getHandler();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700242 mDisplayAdapterListener = new DisplayAdapterListener();
Jeff Brown27f1d672012-10-17 18:32:34 -0700243 mSingleDisplayDemoMode = SystemProperties.getBoolean("persist.demo.singledisplay", false);
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700244 mDefaultDisplayDefaultColorMode = mContext.getResources().getInteger(
245 com.android.internal.R.integer.config_defaultDisplayDefaultColorMode);
Jeff Brown5d6443b2015-04-10 20:15:01 -0700246
247 PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
248 mGlobalDisplayBrightness = pm.getDefaultScreenBrightnessSetting();
Craig Mautner4f67ba62012-08-02 11:23:00 -0700249 }
250
Jeff Brown4ccb8232014-01-16 22:16:42 -0800251 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -0800252 public void onStart() {
Michael Wright1c9977b2016-07-12 13:30:10 -0700253 // We need to pre-load the persistent data store so it's ready before the default display
254 // adapter is up so that we have it's configuration. We could load it lazily, but since
255 // we're going to have to read it in eventually we may as well do it here rather than after
256 // we've waited for the diplay to register itself with us.
257 mPersistentDataStore.loadIfNeeded();
Jeff Brown4ccb8232014-01-16 22:16:42 -0800258 mHandler.sendEmptyMessage(MSG_REGISTER_DEFAULT_DISPLAY_ADAPTER);
259
260 publishBinderService(Context.DISPLAY_SERVICE, new BinderService(),
261 true /*allowIsolated*/);
262 publishLocalService(DisplayManagerInternal.class, new LocalService());
Justin Klaassen22eb1992016-07-11 20:52:23 -0700263 publishLocalService(DisplayTransformManager.class, new DisplayTransformManager());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800264 }
265
266 @Override
267 public void onBootPhase(int phase) {
268 if (phase == PHASE_WAIT_FOR_DEFAULT_DISPLAY) {
269 synchronized (mSyncRoot) {
270 long timeout = SystemClock.uptimeMillis() + WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT;
271 while (mLogicalDisplays.get(Display.DEFAULT_DISPLAY) == null) {
272 long delay = timeout - SystemClock.uptimeMillis();
273 if (delay <= 0) {
274 throw new RuntimeException("Timeout waiting for default display "
275 + "to be initialized.");
276 }
277 if (DEBUG) {
278 Slog.d(TAG, "waitForDefaultDisplay: waiting, timeout=" + delay);
279 }
280 try {
281 mSyncRoot.wait(delay);
282 } catch (InterruptedException ex) {
283 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700284 }
285 }
286 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700287 }
288
Jeff Brown4ccb8232014-01-16 22:16:42 -0800289 // TODO: Use dependencies or a boot phase
290 public void windowManagerAndInputReady() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700291 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800292 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
293 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner65d11b32012-10-01 13:59:52 -0700294 scheduleTraversalLocked(false);
Jeff Brownd728bf52012-09-08 18:05:28 -0700295 }
296 }
297
298 /**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700299 * Called when the system is ready to go.
300 */
301 public void systemReady(boolean safeMode, boolean onlyCore) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700302 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700303 mSafeMode = safeMode;
304 mOnlyCore = onlyCore;
305 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700306
Jeff Brownbd6e1502012-08-28 03:27:37 -0700307 mHandler.sendEmptyMessage(MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS);
308 }
309
Jeff Brown4ccb8232014-01-16 22:16:42 -0800310 private void registerDisplayTransactionListenerInternal(
311 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800312 // List is self-synchronized copy-on-write.
313 mDisplayTransactionListeners.add(listener);
314 }
315
Jeff Brown4ccb8232014-01-16 22:16:42 -0800316 private void unregisterDisplayTransactionListenerInternal(
317 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800318 // List is self-synchronized copy-on-write.
319 mDisplayTransactionListeners.remove(listener);
320 }
321
Jeff Brown4ccb8232014-01-16 22:16:42 -0800322 private void setDisplayInfoOverrideFromWindowManagerInternal(
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700323 int displayId, DisplayInfo info) {
324 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700325 LogicalDisplay display = mLogicalDisplays.get(displayId);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700326 if (display != null) {
Jeff Brownef981a42013-08-07 14:13:37 -0700327 if (display.setDisplayInfoOverrideFromWindowManagerLocked(info)) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700328 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
Craig Mautner65d11b32012-10-01 13:59:52 -0700329 scheduleTraversalLocked(false);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700330 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700331 }
332 }
333 }
334
Jeff Brown4ccb8232014-01-16 22:16:42 -0800335 private void performTraversalInTransactionFromWindowManagerInternal() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700336 synchronized (mSyncRoot) {
337 if (!mPendingTraversal) {
338 return;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700339 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700340 mPendingTraversal = false;
341
342 performTraversalInTransactionLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700343 }
Jeff Brown7f3994e2012-12-04 14:04:28 -0800344
345 // List is self-synchronized copy-on-write.
346 for (DisplayTransactionListener listener : mDisplayTransactionListeners) {
347 listener.onDisplayTransaction();
348 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700349 }
350
Jeff Brown5d6443b2015-04-10 20:15:01 -0700351 private void requestGlobalDisplayStateInternal(int state, int brightness) {
352 if (state == Display.STATE_UNKNOWN) {
353 state = Display.STATE_ON;
354 }
355 if (state == Display.STATE_OFF) {
356 brightness = PowerManager.BRIGHTNESS_OFF;
357 } else if (brightness < 0) {
358 brightness = PowerManager.BRIGHTNESS_DEFAULT;
359 } else if (brightness > PowerManager.BRIGHTNESS_ON) {
360 brightness = PowerManager.BRIGHTNESS_ON;
361 }
362
Jeff Browne75926d2014-09-18 15:24:49 -0700363 synchronized (mTempDisplayStateWorkQueue) {
364 try {
365 // Update the display state within the lock.
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700366 // Note that we do not need to schedule traversals here although it
367 // may happen as a side-effect of displays changing state.
Jeff Browne75926d2014-09-18 15:24:49 -0700368 synchronized (mSyncRoot) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700369 if (mGlobalDisplayState == state
370 && mGlobalDisplayBrightness == brightness) {
371 return; // no change
Jeff Browne75926d2014-09-18 15:24:49 -0700372 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700373
374 Trace.traceBegin(Trace.TRACE_TAG_POWER, "requestGlobalDisplayState("
375 + Display.stateToString(state)
376 + ", brightness=" + brightness + ")");
377 mGlobalDisplayState = state;
378 mGlobalDisplayBrightness = brightness;
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700379 applyGlobalDisplayStateLocked(mTempDisplayStateWorkQueue);
Jeff Browne75926d2014-09-18 15:24:49 -0700380 }
381
382 // Setting the display power state can take hundreds of milliseconds
383 // to complete so we defer the most expensive part of the work until
384 // after we have exited the critical section to avoid blocking other
385 // threads for a long time.
386 for (int i = 0; i < mTempDisplayStateWorkQueue.size(); i++) {
387 mTempDisplayStateWorkQueue.get(i).run();
388 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700389 Trace.traceEnd(Trace.TRACE_TAG_POWER);
Jeff Browne75926d2014-09-18 15:24:49 -0700390 } finally {
391 mTempDisplayStateWorkQueue.clear();
Jeff Brown9e316a12012-10-08 19:17:06 -0700392 }
393 }
394 }
395
Jeff Brown4ccb8232014-01-16 22:16:42 -0800396 private DisplayInfo getDisplayInfoInternal(int displayId, int callingUid) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700397 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800398 LogicalDisplay display = mLogicalDisplays.get(displayId);
399 if (display != null) {
400 DisplayInfo info = display.getDisplayInfoLocked();
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800401 if (info.hasAccess(callingUid)
402 || isUidPresentOnDisplayInternal(callingUid, displayId)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800403 return info;
404 }
405 }
406 return null;
407 }
408 }
409
410 private int[] getDisplayIdsInternal(int callingUid) {
411 synchronized (mSyncRoot) {
412 final int count = mLogicalDisplays.size();
413 int[] displayIds = new int[count];
414 int n = 0;
415 for (int i = 0; i < count; i++) {
416 LogicalDisplay display = mLogicalDisplays.valueAt(i);
417 DisplayInfo info = display.getDisplayInfoLocked();
418 if (info.hasAccess(callingUid)) {
419 displayIds[n++] = mLogicalDisplays.keyAt(i);
420 }
421 }
422 if (n != count) {
423 displayIds = Arrays.copyOfRange(displayIds, 0, n);
424 }
425 return displayIds;
426 }
427 }
428
429 private void registerCallbackInternal(IDisplayManagerCallback callback, int callingPid) {
430 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700431 if (mCallbacks.get(callingPid) != null) {
432 throw new SecurityException("The calling process has already "
433 + "registered an IDisplayManagerCallback.");
Jeff Brown64a55af2012-08-26 02:47:39 -0700434 }
435
Jeff Brownbd6e1502012-08-28 03:27:37 -0700436 CallbackRecord record = new CallbackRecord(callingPid, callback);
437 try {
438 IBinder binder = callback.asBinder();
439 binder.linkToDeath(record, 0);
440 } catch (RemoteException ex) {
441 // give up
442 throw new RuntimeException(ex);
443 }
444
445 mCallbacks.put(callingPid, record);
446 }
447 }
448
Jeff Brownce468a32013-11-21 16:42:03 -0800449 private void onCallbackDied(CallbackRecord record) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700450 synchronized (mSyncRoot) {
Jeff Brownce468a32013-11-21 16:42:03 -0800451 mCallbacks.remove(record.mPid);
452 stopWifiDisplayScanLocked(record);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700453 }
454 }
455
Jeff Brown4ccb8232014-01-16 22:16:42 -0800456 private void startWifiDisplayScanInternal(int callingPid) {
457 synchronized (mSyncRoot) {
458 CallbackRecord record = mCallbacks.get(callingPid);
459 if (record == null) {
460 throw new IllegalStateException("The calling process has not "
461 + "registered an IDisplayManagerCallback.");
Jeff Browne08ae382012-09-07 20:36:36 -0700462 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800463 startWifiDisplayScanLocked(record);
Jeff Browne08ae382012-09-07 20:36:36 -0700464 }
465 }
466
Jeff Brownce468a32013-11-21 16:42:03 -0800467 private void startWifiDisplayScanLocked(CallbackRecord record) {
468 if (!record.mWifiDisplayScanRequested) {
469 record.mWifiDisplayScanRequested = true;
470 if (mWifiDisplayScanRequestCount++ == 0) {
471 if (mWifiDisplayAdapter != null) {
472 mWifiDisplayAdapter.requestStartScanLocked();
473 }
474 }
475 }
476 }
477
Jeff Brown4ccb8232014-01-16 22:16:42 -0800478 private void stopWifiDisplayScanInternal(int callingPid) {
479 synchronized (mSyncRoot) {
480 CallbackRecord record = mCallbacks.get(callingPid);
481 if (record == null) {
482 throw new IllegalStateException("The calling process has not "
483 + "registered an IDisplayManagerCallback.");
Jeff Brownce468a32013-11-21 16:42:03 -0800484 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800485 stopWifiDisplayScanLocked(record);
Jeff Brownce468a32013-11-21 16:42:03 -0800486 }
487 }
488
489 private void stopWifiDisplayScanLocked(CallbackRecord record) {
490 if (record.mWifiDisplayScanRequested) {
491 record.mWifiDisplayScanRequested = false;
492 if (--mWifiDisplayScanRequestCount == 0) {
493 if (mWifiDisplayAdapter != null) {
494 mWifiDisplayAdapter.requestStopScanLocked();
495 }
496 } else if (mWifiDisplayScanRequestCount < 0) {
Dianne Hackborn8d051722014-10-01 14:59:58 -0700497 Slog.wtf(TAG, "mWifiDisplayScanRequestCount became negative: "
Jeff Brownce468a32013-11-21 16:42:03 -0800498 + mWifiDisplayScanRequestCount);
499 mWifiDisplayScanRequestCount = 0;
500 }
501 }
502 }
503
Jeff Brown4ccb8232014-01-16 22:16:42 -0800504 private void connectWifiDisplayInternal(String address) {
505 synchronized (mSyncRoot) {
506 if (mWifiDisplayAdapter != null) {
507 mWifiDisplayAdapter.requestConnectLocked(address);
Jeff Browne08ae382012-09-07 20:36:36 -0700508 }
Jeff Browne08ae382012-09-07 20:36:36 -0700509 }
510 }
511
Jeff Brown4ccb8232014-01-16 22:16:42 -0800512 private void pauseWifiDisplayInternal() {
513 synchronized (mSyncRoot) {
514 if (mWifiDisplayAdapter != null) {
515 mWifiDisplayAdapter.requestPauseLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700516 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700517 }
518 }
519
Jeff Brown4ccb8232014-01-16 22:16:42 -0800520 private void resumeWifiDisplayInternal() {
521 synchronized (mSyncRoot) {
522 if (mWifiDisplayAdapter != null) {
523 mWifiDisplayAdapter.requestResumeLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700524 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700525 }
526 }
527
Jeff Brown4ccb8232014-01-16 22:16:42 -0800528 private void disconnectWifiDisplayInternal() {
529 synchronized (mSyncRoot) {
530 if (mWifiDisplayAdapter != null) {
531 mWifiDisplayAdapter.requestDisconnectLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700532 }
Jeff Browne08ae382012-09-07 20:36:36 -0700533 }
534 }
535
Jeff Brown4ccb8232014-01-16 22:16:42 -0800536 private void renameWifiDisplayInternal(String address, String alias) {
537 synchronized (mSyncRoot) {
538 if (mWifiDisplayAdapter != null) {
539 mWifiDisplayAdapter.requestRenameLocked(address, alias);
Jeff Brown89d55462012-09-19 11:33:42 -0700540 }
Jeff Brown89d55462012-09-19 11:33:42 -0700541 }
542 }
543
Jeff Brown4ccb8232014-01-16 22:16:42 -0800544 private void forgetWifiDisplayInternal(String address) {
545 synchronized (mSyncRoot) {
546 if (mWifiDisplayAdapter != null) {
547 mWifiDisplayAdapter.requestForgetLocked(address);
Jeff Brown89d55462012-09-19 11:33:42 -0700548 }
Jeff Brown89d55462012-09-19 11:33:42 -0700549 }
550 }
551
Jeff Brown4ccb8232014-01-16 22:16:42 -0800552 private WifiDisplayStatus getWifiDisplayStatusInternal() {
553 synchronized (mSyncRoot) {
554 if (mWifiDisplayAdapter != null) {
555 return mWifiDisplayAdapter.getWifiDisplayStatusLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700556 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800557 return new WifiDisplayStatus();
Jeff Browne08ae382012-09-07 20:36:36 -0700558 }
559 }
560
Michael Wright1c9977b2016-07-12 13:30:10 -0700561 private void requestColorModeInternal(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +0100562 synchronized (mSyncRoot) {
563 LogicalDisplay display = mLogicalDisplays.get(displayId);
564 if (display != null &&
Michael Wright1c9977b2016-07-12 13:30:10 -0700565 display.getRequestedColorModeLocked() != colorMode) {
566 display.setRequestedColorModeLocked(colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +0100567 scheduleTraversalLocked(false);
568 }
569 }
570 }
571
Michael Wright75ee9fc2014-09-01 19:55:22 -0700572 private int createVirtualDisplayInternal(IVirtualDisplayCallback callback,
Michael Wrightc39d47a2014-07-08 18:07:36 -0700573 IMediaProjection projection, int callingUid, String packageName,
Jeff Brown7d00aff2013-08-02 19:03:49 -0700574 String name, int width, int height, int densityDpi, Surface surface, int flags) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800575 synchronized (mSyncRoot) {
576 if (mVirtualDisplayAdapter == null) {
577 Slog.w(TAG, "Rejecting request to create private virtual display "
578 + "because the virtual display adapter is not available.");
579 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700580 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800581
582 DisplayDevice device = mVirtualDisplayAdapter.createVirtualDisplayLocked(
Michael Wright75ee9fc2014-09-01 19:55:22 -0700583 callback, projection, callingUid, packageName,
Michael Wrightc39d47a2014-07-08 18:07:36 -0700584 name, width, height, densityDpi, surface, flags);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800585 if (device == null) {
586 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700587 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700588
Jeff Brown4ccb8232014-01-16 22:16:42 -0800589 handleDisplayDeviceAddedLocked(device);
590 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
591 if (display != null) {
592 return display.getDisplayIdLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700593 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800594
595 // Something weird happened and the logical display was not created.
596 Slog.w(TAG, "Rejecting request to create virtual display "
597 + "because the logical display was not created.");
Michael Wright75ee9fc2014-09-01 19:55:22 -0700598 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800599 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700600 }
601 return -1;
602 }
603
Michael Wright01e840f2014-06-26 16:03:25 -0700604 private void resizeVirtualDisplayInternal(IBinder appToken,
605 int width, int height, int densityDpi) {
606 synchronized (mSyncRoot) {
607 if (mVirtualDisplayAdapter == null) {
608 return;
609 }
610
611 mVirtualDisplayAdapter.resizeVirtualDisplayLocked(appToken, width, height, densityDpi);
612 }
613 }
614
Jeff Brown92207df2014-04-16 13:16:07 -0700615 private void setVirtualDisplaySurfaceInternal(IBinder appToken, Surface surface) {
616 synchronized (mSyncRoot) {
617 if (mVirtualDisplayAdapter == null) {
618 return;
619 }
620
621 mVirtualDisplayAdapter.setVirtualDisplaySurfaceLocked(appToken, surface);
622 }
623 }
624
Jeff Brown4ccb8232014-01-16 22:16:42 -0800625 private void releaseVirtualDisplayInternal(IBinder appToken) {
626 synchronized (mSyncRoot) {
627 if (mVirtualDisplayAdapter == null) {
628 return;
Jeff Browna506a6e2013-06-04 00:02:38 -0700629 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700630
Jeff Brown4ccb8232014-01-16 22:16:42 -0800631 DisplayDevice device =
632 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(appToken);
633 if (device != null) {
634 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700635 }
636 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700637 }
638
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700639 private void registerDefaultDisplayAdapter() {
640 // Register default display adapter.
641 synchronized (mSyncRoot) {
Mike Lockwoode63f6f72013-11-15 11:01:47 -0800642 registerDisplayAdapterLocked(new LocalDisplayAdapter(
643 mSyncRoot, mContext, mHandler, mDisplayAdapterListener));
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700644 }
645 }
646
647 private void registerAdditionalDisplayAdapters() {
648 synchronized (mSyncRoot) {
649 if (shouldRegisterNonEssentialDisplayAdaptersLocked()) {
Jeff Brown89d55462012-09-19 11:33:42 -0700650 registerOverlayDisplayAdapterLocked();
651 registerWifiDisplayAdapterLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700652 registerVirtualDisplayAdapterLocked();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700653 }
654 }
655 }
656
Jeff Brown89d55462012-09-19 11:33:42 -0700657 private void registerOverlayDisplayAdapterLocked() {
658 registerDisplayAdapterLocked(new OverlayDisplayAdapter(
659 mSyncRoot, mContext, mHandler, mDisplayAdapterListener, mUiHandler));
660 }
661
662 private void registerWifiDisplayAdapterLocked() {
663 if (mContext.getResources().getBoolean(
Jeff Brownbbd28a22012-09-20 16:47:15 -0700664 com.android.internal.R.bool.config_enableWifiDisplay)
665 || SystemProperties.getInt(FORCE_WIFI_DISPLAY_ENABLE, -1) == 1) {
Jeff Brown89d55462012-09-19 11:33:42 -0700666 mWifiDisplayAdapter = new WifiDisplayAdapter(
667 mSyncRoot, mContext, mHandler, mDisplayAdapterListener,
668 mPersistentDataStore);
669 registerDisplayAdapterLocked(mWifiDisplayAdapter);
670 }
671 }
672
Jeff Browna506a6e2013-06-04 00:02:38 -0700673 private void registerVirtualDisplayAdapterLocked() {
674 mVirtualDisplayAdapter = new VirtualDisplayAdapter(
675 mSyncRoot, mContext, mHandler, mDisplayAdapterListener);
676 registerDisplayAdapterLocked(mVirtualDisplayAdapter);
677 }
678
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700679 private boolean shouldRegisterNonEssentialDisplayAdaptersLocked() {
680 // In safe mode, we disable non-essential display adapters to give the user
681 // an opportunity to fix broken settings or other problems that might affect
682 // system stability.
683 // In only-core mode, we disable non-essential display adapters to minimize
684 // the number of dependencies that are started while in this mode and to
685 // prevent problems that might occur due to the device being encrypted.
686 return !mSafeMode && !mOnlyCore;
687 }
688
689 private void registerDisplayAdapterLocked(DisplayAdapter adapter) {
690 mDisplayAdapters.add(adapter);
691 adapter.registerLocked();
692 }
693
Jeff Brownbd6e1502012-08-28 03:27:37 -0700694 private void handleDisplayDeviceAdded(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700695 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700696 handleDisplayDeviceAddedLocked(device);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700697 }
698 }
699
Jeff Browna506a6e2013-06-04 00:02:38 -0700700 private void handleDisplayDeviceAddedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700701 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700702 if (mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700703 Slog.w(TAG, "Attempted to add already added display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700704 return;
705 }
706
Jeff Brown10acf6d2015-04-14 14:20:47 -0700707 Slog.i(TAG, "Display device added: " + info);
708 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700709
710 mDisplayDevices.add(device);
Michael Wright1c9977b2016-07-12 13:30:10 -0700711 LogicalDisplay display = addLogicalDisplayLocked(device);
Jeff Brown0033a862014-10-08 12:06:39 -0700712 Runnable work = updateDisplayStateLocked(device);
713 if (work != null) {
714 work.run();
715 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700716 if (display != null && display.getPrimaryDisplayDeviceLocked() == device) {
717 int colorMode = mPersistentDataStore.getColorMode(device);
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700718 if (colorMode == Display.COLOR_MODE_INVALID) {
719 if ((device.getDisplayDeviceInfoLocked().flags
720 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
721 colorMode = mDefaultDisplayDefaultColorMode;
722 } else {
723 colorMode = Display.COLOR_MODE_DEFAULT;
724 }
725 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700726 display.setRequestedColorModeLocked(colorMode);
727 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700728 scheduleTraversalLocked(false);
729 }
730
Jeff Brownbd6e1502012-08-28 03:27:37 -0700731 private void handleDisplayDeviceChanged(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700732 synchronized (mSyncRoot) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700733 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700734 if (!mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700735 Slog.w(TAG, "Attempted to change non-existent display device: " + info);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700736 return;
737 }
738
Jeff Brown10acf6d2015-04-14 14:20:47 -0700739 int diff = device.mDebugLastLoggedDeviceInfo.diff(info);
740 if (diff == DisplayDeviceInfo.DIFF_STATE) {
741 Slog.i(TAG, "Display device changed state: \"" + info.name
742 + "\", " + Display.stateToString(info.state));
743 } else if (diff != 0) {
744 Slog.i(TAG, "Display device changed: " + info);
745 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700746 if ((diff & DisplayDeviceInfo.DIFF_COLOR_MODE) != 0) {
747 try {
748 mPersistentDataStore.setColorMode(device, info.colorMode);
749 } finally {
750 mPersistentDataStore.saveIfNeeded();
751 }
752 }
Jeff Brown10acf6d2015-04-14 14:20:47 -0700753 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browne87bf032012-09-20 18:30:13 -0700754
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700755 device.applyPendingDisplayDeviceInfoChangesLocked();
756 if (updateLogicalDisplaysLocked()) {
Craig Mautner65d11b32012-10-01 13:59:52 -0700757 scheduleTraversalLocked(false);
Jeff Brown64a55af2012-08-26 02:47:39 -0700758 }
759 }
760 }
761
Jeff Brownbd6e1502012-08-28 03:27:37 -0700762 private void handleDisplayDeviceRemoved(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700763 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700764 handleDisplayDeviceRemovedLocked(device);
765 }
766 }
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700767
Jeff Browna506a6e2013-06-04 00:02:38 -0700768 private void handleDisplayDeviceRemovedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700769 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700770 if (!mDisplayDevices.remove(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700771 Slog.w(TAG, "Attempted to remove non-existent display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700772 return;
773 }
774
Jeff Brown10acf6d2015-04-14 14:20:47 -0700775 Slog.i(TAG, "Display device removed: " + info);
776 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700777
Jeff Browna506a6e2013-06-04 00:02:38 -0700778 updateLogicalDisplaysLocked();
779 scheduleTraversalLocked(false);
780 }
781
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700782 private void applyGlobalDisplayStateLocked(List<Runnable> workQueue) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700783 final int count = mDisplayDevices.size();
784 for (int i = 0; i < count; i++) {
785 DisplayDevice device = mDisplayDevices.get(i);
Jeff Browne75926d2014-09-18 15:24:49 -0700786 Runnable runnable = updateDisplayStateLocked(device);
787 if (runnable != null) {
788 workQueue.add(runnable);
789 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700790 }
791 }
792
Jeff Browne75926d2014-09-18 15:24:49 -0700793 private Runnable updateDisplayStateLocked(DisplayDevice device) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700794 // Blank or unblank the display immediately to match the state requested
Jeff Brown037c33e2014-04-09 00:31:55 -0700795 // by the display power controller (if known).
Jeff Browna506a6e2013-06-04 00:02:38 -0700796 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
797 if ((info.flags & DisplayDeviceInfo.FLAG_NEVER_BLANK) == 0) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700798 return device.requestDisplayStateLocked(mGlobalDisplayState, mGlobalDisplayBrightness);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700799 }
Jeff Browne75926d2014-09-18 15:24:49 -0700800 return null;
Craig Mautner4f67ba62012-08-02 11:23:00 -0700801 }
802
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700803 // Adds a new logical display based on the given display device.
804 // Sends notifications if needed.
Michael Wright1c9977b2016-07-12 13:30:10 -0700805 private LogicalDisplay addLogicalDisplayLocked(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700806 DisplayDeviceInfo deviceInfo = device.getDisplayDeviceInfoLocked();
807 boolean isDefault = (deviceInfo.flags
808 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0;
809 if (isDefault && mLogicalDisplays.get(Display.DEFAULT_DISPLAY) != null) {
810 Slog.w(TAG, "Ignoring attempt to add a second default display: " + deviceInfo);
811 isDefault = false;
812 }
813
Jeff Brown27f1d672012-10-17 18:32:34 -0700814 if (!isDefault && mSingleDisplayDemoMode) {
815 Slog.i(TAG, "Not creating a logical display for a secondary display "
816 + " because single display demo mode is enabled: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -0700817 return null;
Jeff Brown27f1d672012-10-17 18:32:34 -0700818 }
819
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700820 final int displayId = assignDisplayIdLocked(isDefault);
821 final int layerStack = assignLayerStackLocked(displayId);
822
Jeff Brownd728bf52012-09-08 18:05:28 -0700823 LogicalDisplay display = new LogicalDisplay(displayId, layerStack, device);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700824 display.updateLocked(mDisplayDevices);
825 if (!display.isValidLocked()) {
826 // This should never happen currently.
827 Slog.w(TAG, "Ignoring display device because the logical display "
828 + "created from it was not considered valid: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -0700829 return null;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700830 }
831
832 mLogicalDisplays.put(displayId, display);
833
834 // Wake up waitForDefaultDisplay.
835 if (isDefault) {
836 mSyncRoot.notifyAll();
837 }
838
839 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_ADDED);
Michael Wright1c9977b2016-07-12 13:30:10 -0700840 return display;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700841 }
842
843 private int assignDisplayIdLocked(boolean isDefault) {
844 return isDefault ? Display.DEFAULT_DISPLAY : mNextNonDefaultDisplayId++;
845 }
846
847 private int assignLayerStackLocked(int displayId) {
848 // Currently layer stacks and display ids are the same.
849 // This need not be the case.
850 return displayId;
851 }
852
853 // Updates all existing logical displays given the current set of display devices.
854 // Removes invalid logical displays.
855 // Sends notifications if needed.
856 private boolean updateLogicalDisplaysLocked() {
857 boolean changed = false;
858 for (int i = mLogicalDisplays.size(); i-- > 0; ) {
859 final int displayId = mLogicalDisplays.keyAt(i);
860 LogicalDisplay display = mLogicalDisplays.valueAt(i);
861
862 mTempDisplayInfo.copyFrom(display.getDisplayInfoLocked());
863 display.updateLocked(mDisplayDevices);
864 if (!display.isValidLocked()) {
865 mLogicalDisplays.removeAt(i);
866 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_REMOVED);
867 changed = true;
868 } else if (!mTempDisplayInfo.equals(display.getDisplayInfoLocked())) {
869 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
870 changed = true;
871 }
872 }
873 return changed;
874 }
875
876 private void performTraversalInTransactionLocked() {
Jeff Brownd728bf52012-09-08 18:05:28 -0700877 // Clear all viewports before configuring displays so that we can keep
878 // track of which ones we have configured.
879 clearViewportsLocked();
880
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700881 // Configure each display device.
882 final int count = mDisplayDevices.size();
883 for (int i = 0; i < count; i++) {
884 DisplayDevice device = mDisplayDevices.get(i);
885 configureDisplayInTransactionLocked(device);
886 device.performTraversalInTransactionLocked();
887 }
Jeff Brownd728bf52012-09-08 18:05:28 -0700888
889 // Tell the input system about these new viewports.
Jeff Brown4ccb8232014-01-16 22:16:42 -0800890 if (mInputManagerInternal != null) {
Jeff Brownd728bf52012-09-08 18:05:28 -0700891 mHandler.sendEmptyMessage(MSG_UPDATE_VIEWPORT);
892 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700893 }
894
Michael Wright3f145a22014-07-22 19:46:03 -0700895 private void setDisplayPropertiesInternal(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700896 float requestedRefreshRate, int requestedModeId, boolean inTraversal) {
Craig Mautner722285e2012-09-07 13:55:58 -0700897 synchronized (mSyncRoot) {
898 LogicalDisplay display = mLogicalDisplays.get(displayId);
Michael Wright3f145a22014-07-22 19:46:03 -0700899 if (display == null) {
900 return;
901 }
902 if (display.hasContentLocked() != hasContent) {
Jeff Brown33041bd2013-08-02 21:11:14 -0700903 if (DEBUG) {
904 Slog.d(TAG, "Display " + displayId + " hasContent flag changed: "
905 + "hasContent=" + hasContent + ", inTraversal=" + inTraversal);
906 }
907
Craig Mautner722285e2012-09-07 13:55:58 -0700908 display.setHasContentLocked(hasContent);
Craig Mautner65d11b32012-10-01 13:59:52 -0700909 scheduleTraversalLocked(inTraversal);
Craig Mautner722285e2012-09-07 13:55:58 -0700910 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700911 if (requestedModeId == 0 && requestedRefreshRate != 0) {
912 // Scan supported modes returned by display.getInfo() to find a mode with the same
913 // size as the default display mode but with the specified refresh rate instead.
914 requestedModeId = display.getDisplayInfoLocked().findDefaultModeByRefreshRate(
915 requestedRefreshRate);
916 }
917 if (display.getRequestedModeIdLocked() != requestedModeId) {
Michael Wright3f145a22014-07-22 19:46:03 -0700918 if (DEBUG) {
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700919 Slog.d(TAG, "Display " + displayId + " switching to mode " + requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -0700920 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700921 display.setRequestedModeIdLocked(requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -0700922 scheduleTraversalLocked(inTraversal);
923 }
Craig Mautner722285e2012-09-07 13:55:58 -0700924 }
Jeff Brownd728bf52012-09-08 18:05:28 -0700925 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700926
Filip Gruszczynskid2e86402015-02-19 13:05:03 -0800927 private void setDisplayOffsetsInternal(int displayId, int x, int y) {
928 synchronized (mSyncRoot) {
929 LogicalDisplay display = mLogicalDisplays.get(displayId);
930 if (display == null) {
931 return;
932 }
933 if (display.getDisplayOffsetXLocked() != x
934 || display.getDisplayOffsetYLocked() != y) {
935 if (DEBUG) {
936 Slog.d(TAG, "Display " + displayId + " burn-in offset set to ("
937 + x + ", " + y + ")");
938 }
939 display.setDisplayOffsetsLocked(x, y);
940 scheduleTraversalLocked(false);
941 }
942 }
943 }
944
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800945 // Updates the lists of UIDs that are present on displays.
946 private void setDisplayAccessUIDsInternal(SparseArray<IntArray> newDisplayAccessUIDs) {
947 synchronized (mSyncRoot) {
948 mDisplayAccessUIDs.clear();
949 for (int i = newDisplayAccessUIDs.size() - 1; i >= 0; i--) {
950 mDisplayAccessUIDs.append(newDisplayAccessUIDs.keyAt(i),
951 newDisplayAccessUIDs.valueAt(i));
952 }
953 }
954 }
955
956 // Checks if provided UID's content is present on the display and UID has access to it.
957 private boolean isUidPresentOnDisplayInternal(int uid, int displayId) {
958 synchronized (mSyncRoot) {
959 final IntArray displayUIDs = mDisplayAccessUIDs.get(displayId);
960 return displayUIDs != null && displayUIDs.indexOf(uid) != -1;
961 }
962 }
963
Jeff Brownd728bf52012-09-08 18:05:28 -0700964 private void clearViewportsLocked() {
965 mDefaultViewport.valid = false;
966 mExternalTouchViewport.valid = false;
Craig Mautner722285e2012-09-07 13:55:58 -0700967 }
968
969 private void configureDisplayInTransactionLocked(DisplayDevice device) {
Jeff Brownd14c8c92014-01-07 18:13:09 -0800970 final DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
971 final boolean ownContent = (info.flags & DisplayDeviceInfo.FLAG_OWN_CONTENT_ONLY) != 0;
Jeff Browna506a6e2013-06-04 00:02:38 -0700972
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700973 // Find the logical display that the display device is showing.
Jeff Brownd14c8c92014-01-07 18:13:09 -0800974 // Certain displays only ever show their own content.
Craig Mautner722285e2012-09-07 13:55:58 -0700975 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
Jeff Brownd14c8c92014-01-07 18:13:09 -0800976 if (!ownContent) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700977 if (display != null && !display.hasContentLocked()) {
978 // If the display does not have any content of its own, then
979 // automatically mirror the default logical display contents.
980 display = null;
981 }
982 if (display == null) {
983 display = mLogicalDisplays.get(Display.DEFAULT_DISPLAY);
984 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700985 }
986
987 // Apply the logical display configuration to the display device.
988 if (display == null) {
989 // TODO: no logical display for the device, blank it
Jeff Brownd728bf52012-09-08 18:05:28 -0700990 Slog.w(TAG, "Missing logical display to use for physical display device: "
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700991 + device.getDisplayDeviceInfoLocked());
Jeff Brownd728bf52012-09-08 18:05:28 -0700992 return;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700993 }
Jeff Brown037c33e2014-04-09 00:31:55 -0700994 display.configureDisplayInTransactionLocked(device, info.state == Display.STATE_OFF);
Jeff Brownd728bf52012-09-08 18:05:28 -0700995
996 // Update the viewports if needed.
Jeff Brownd728bf52012-09-08 18:05:28 -0700997 if (!mDefaultViewport.valid
998 && (info.flags & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
999 setViewportLocked(mDefaultViewport, display, device);
1000 }
1001 if (!mExternalTouchViewport.valid
1002 && info.touch == DisplayDeviceInfo.TOUCH_EXTERNAL) {
1003 setViewportLocked(mExternalTouchViewport, display, device);
1004 }
1005 }
1006
1007 private static void setViewportLocked(DisplayViewport viewport,
1008 LogicalDisplay display, DisplayDevice device) {
1009 viewport.valid = true;
1010 viewport.displayId = display.getDisplayIdLocked();
1011 device.populateViewportLocked(viewport);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001012 }
1013
1014 private LogicalDisplay findLogicalDisplayForDeviceLocked(DisplayDevice device) {
1015 final int count = mLogicalDisplays.size();
1016 for (int i = 0; i < count; i++) {
1017 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1018 if (display.getPrimaryDisplayDeviceLocked() == device) {
1019 return display;
1020 }
1021 }
1022 return null;
1023 }
1024
Jeff Brownbd6e1502012-08-28 03:27:37 -07001025 private void sendDisplayEventLocked(int displayId, int event) {
1026 Message msg = mHandler.obtainMessage(MSG_DELIVER_DISPLAY_EVENT, displayId, event);
1027 mHandler.sendMessage(msg);
1028 }
1029
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001030 // Requests that performTraversalsInTransactionFromWindowManager be called at a
1031 // later time to apply changes to surfaces and displays.
Craig Mautner65d11b32012-10-01 13:59:52 -07001032 private void scheduleTraversalLocked(boolean inTraversal) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001033 if (!mPendingTraversal && mWindowManagerInternal != null) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001034 mPendingTraversal = true;
Craig Mautner65d11b32012-10-01 13:59:52 -07001035 if (!inTraversal) {
1036 mHandler.sendEmptyMessage(MSG_REQUEST_TRAVERSAL);
1037 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001038 }
1039 }
1040
1041 // Runs on Handler thread.
1042 // Delivers display event notifications to callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001043 private void deliverDisplayEvent(int displayId, int event) {
1044 if (DEBUG) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001045 Slog.d(TAG, "Delivering display event: displayId="
1046 + displayId + ", event=" + event);
Jeff Brownfa25bf52012-07-23 19:26:30 -07001047 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07001048
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001049 // Grab the lock and copy the callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001050 final int count;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001051 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001052 count = mCallbacks.size();
1053 mTempCallbacks.clear();
1054 for (int i = 0; i < count; i++) {
1055 mTempCallbacks.add(mCallbacks.valueAt(i));
Craig Mautner4f67ba62012-08-02 11:23:00 -07001056 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001057 }
Craig Mautner4f67ba62012-08-02 11:23:00 -07001058
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001059 // After releasing the lock, send the notifications out.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001060 for (int i = 0; i < count; i++) {
1061 mTempCallbacks.get(i).notifyDisplayEventAsync(displayId, event);
1062 }
1063 mTempCallbacks.clear();
Craig Mautner4f67ba62012-08-02 11:23:00 -07001064 }
1065
Michael Wrightc39d47a2014-07-08 18:07:36 -07001066 private IMediaProjectionManager getProjectionService() {
1067 if (mProjectionService == null) {
1068 IBinder b = ServiceManager.getService(Context.MEDIA_PROJECTION_SERVICE);
1069 mProjectionService = IMediaProjectionManager.Stub.asInterface(b);
1070 }
1071 return mProjectionService;
1072 }
1073
Jeff Brown4ccb8232014-01-16 22:16:42 -08001074 private void dumpInternal(PrintWriter pw) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001075 pw.println("DISPLAY MANAGER (dumpsys display)");
Jeff Brownfa25bf52012-07-23 19:26:30 -07001076
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001077 synchronized (mSyncRoot) {
Jeff Brown9e316a12012-10-08 19:17:06 -07001078 pw.println(" mOnlyCode=" + mOnlyCore);
1079 pw.println(" mSafeMode=" + mSafeMode);
1080 pw.println(" mPendingTraversal=" + mPendingTraversal);
Jeff Brown037c33e2014-04-09 00:31:55 -07001081 pw.println(" mGlobalDisplayState=" + Display.stateToString(mGlobalDisplayState));
Jeff Brown9e316a12012-10-08 19:17:06 -07001082 pw.println(" mNextNonDefaultDisplayId=" + mNextNonDefaultDisplayId);
1083 pw.println(" mDefaultViewport=" + mDefaultViewport);
1084 pw.println(" mExternalTouchViewport=" + mExternalTouchViewport);
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -07001085 pw.println(" mDefaultDisplayDefaultColorMode=" + mDefaultDisplayDefaultColorMode);
Jeff Brown27f1d672012-10-17 18:32:34 -07001086 pw.println(" mSingleDisplayDemoMode=" + mSingleDisplayDemoMode);
Jeff Brownce468a32013-11-21 16:42:03 -08001087 pw.println(" mWifiDisplayScanRequestCount=" + mWifiDisplayScanRequestCount);
Jeff Brown9e316a12012-10-08 19:17:06 -07001088
Jeff Brownbd6e1502012-08-28 03:27:37 -07001089 IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001090 ipw.increaseIndent();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001091
1092 pw.println();
1093 pw.println("Display Adapters: size=" + mDisplayAdapters.size());
Jeff Brown848c2dc2012-08-19 20:18:08 -07001094 for (DisplayAdapter adapter : mDisplayAdapters) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001095 pw.println(" " + adapter.getName());
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001096 adapter.dumpLocked(ipw);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001097 }
Craig Mautner9de49362012-08-02 14:30:30 -07001098
Jeff Brownbd6e1502012-08-28 03:27:37 -07001099 pw.println();
1100 pw.println("Display Devices: size=" + mDisplayDevices.size());
1101 for (DisplayDevice device : mDisplayDevices) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001102 pw.println(" " + device.getDisplayDeviceInfoLocked());
1103 device.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001104 }
1105
1106 final int logicalDisplayCount = mLogicalDisplays.size();
1107 pw.println();
1108 pw.println("Logical Displays: size=" + logicalDisplayCount);
1109 for (int i = 0; i < logicalDisplayCount; i++) {
1110 int displayId = mLogicalDisplays.keyAt(i);
1111 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1112 pw.println(" Display " + displayId + ":");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001113 display.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001114 }
Jeff Brownce468a32013-11-21 16:42:03 -08001115
1116 final int callbackCount = mCallbacks.size();
1117 pw.println();
1118 pw.println("Callbacks: size=" + callbackCount);
1119 for (int i = 0; i < callbackCount; i++) {
1120 CallbackRecord callback = mCallbacks.valueAt(i);
1121 pw.println(" " + i + ": mPid=" + callback.mPid
1122 + ", mWifiDisplayScanRequested=" + callback.mWifiDisplayScanRequested);
1123 }
Jeff Brownad9ef192014-04-08 17:26:30 -07001124
1125 if (mDisplayPowerController != null) {
1126 mDisplayPowerController.dump(pw);
1127 }
Michael Wright1c9977b2016-07-12 13:30:10 -07001128
1129 pw.println();
1130 mPersistentDataStore.dump(pw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001131 }
1132 }
1133
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001134 /**
1135 * This is the object that everything in the display manager locks on.
1136 * We make it an inner class within the {@link DisplayManagerService} to so that it is
1137 * clear that the object belongs to the display manager service and that it is
1138 * a unique object with a special purpose.
1139 */
1140 public static final class SyncRoot {
1141 }
1142
Jeff Brownbd6e1502012-08-28 03:27:37 -07001143 private final class DisplayManagerHandler extends Handler {
1144 public DisplayManagerHandler(Looper looper) {
1145 super(looper, null, true /*async*/);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001146 }
Jeff Brownbf5740e2012-08-19 23:20:02 -07001147
Jeff Brownbd6e1502012-08-28 03:27:37 -07001148 @Override
1149 public void handleMessage(Message msg) {
1150 switch (msg.what) {
1151 case MSG_REGISTER_DEFAULT_DISPLAY_ADAPTER:
1152 registerDefaultDisplayAdapter();
1153 break;
1154
1155 case MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS:
1156 registerAdditionalDisplayAdapters();
1157 break;
1158
1159 case MSG_DELIVER_DISPLAY_EVENT:
1160 deliverDisplayEvent(msg.arg1, msg.arg2);
1161 break;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001162
1163 case MSG_REQUEST_TRAVERSAL:
Jeff Brown4ccb8232014-01-16 22:16:42 -08001164 mWindowManagerInternal.requestTraversalFromDisplayManager();
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001165 break;
Jeff Brownd728bf52012-09-08 18:05:28 -07001166
1167 case MSG_UPDATE_VIEWPORT: {
1168 synchronized (mSyncRoot) {
1169 mTempDefaultViewport.copyFrom(mDefaultViewport);
1170 mTempExternalTouchViewport.copyFrom(mExternalTouchViewport);
1171 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001172 mInputManagerInternal.setDisplayViewports(
Jeff Brownd728bf52012-09-08 18:05:28 -07001173 mTempDefaultViewport, mTempExternalTouchViewport);
1174 break;
1175 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001176 }
1177 }
1178 }
1179
1180 private final class DisplayAdapterListener implements DisplayAdapter.Listener {
1181 @Override
1182 public void onDisplayDeviceEvent(DisplayDevice device, int event) {
1183 switch (event) {
1184 case DisplayAdapter.DISPLAY_DEVICE_EVENT_ADDED:
1185 handleDisplayDeviceAdded(device);
1186 break;
1187
1188 case DisplayAdapter.DISPLAY_DEVICE_EVENT_CHANGED:
1189 handleDisplayDeviceChanged(device);
1190 break;
1191
1192 case DisplayAdapter.DISPLAY_DEVICE_EVENT_REMOVED:
1193 handleDisplayDeviceRemoved(device);
1194 break;
1195 }
1196 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001197
1198 @Override
1199 public void onTraversalRequested() {
1200 synchronized (mSyncRoot) {
Craig Mautner65d11b32012-10-01 13:59:52 -07001201 scheduleTraversalLocked(false);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001202 }
1203 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001204 }
1205
1206 private final class CallbackRecord implements DeathRecipient {
Jeff Brownce468a32013-11-21 16:42:03 -08001207 public final int mPid;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001208 private final IDisplayManagerCallback mCallback;
1209
Jeff Brownce468a32013-11-21 16:42:03 -08001210 public boolean mWifiDisplayScanRequested;
1211
Jeff Brownbd6e1502012-08-28 03:27:37 -07001212 public CallbackRecord(int pid, IDisplayManagerCallback callback) {
1213 mPid = pid;
1214 mCallback = callback;
1215 }
1216
1217 @Override
1218 public void binderDied() {
1219 if (DEBUG) {
1220 Slog.d(TAG, "Display listener for pid " + mPid + " died.");
1221 }
Jeff Brownce468a32013-11-21 16:42:03 -08001222 onCallbackDied(this);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001223 }
1224
1225 public void notifyDisplayEventAsync(int displayId, int event) {
1226 try {
1227 mCallback.onDisplayEvent(displayId, event);
1228 } catch (RemoteException ex) {
1229 Slog.w(TAG, "Failed to notify process "
1230 + mPid + " that displays changed, assuming it died.", ex);
1231 binderDied();
1232 }
1233 }
1234 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001235
1236 private final class BinderService extends IDisplayManager.Stub {
1237 /**
1238 * Returns information about the specified logical display.
1239 *
1240 * @param displayId The logical display id.
1241 * @return The logical display info, or null if the display does not exist. The
1242 * returned object must be treated as immutable.
1243 */
1244 @Override // Binder call
1245 public DisplayInfo getDisplayInfo(int displayId) {
1246 final int callingUid = Binder.getCallingUid();
1247 final long token = Binder.clearCallingIdentity();
1248 try {
1249 return getDisplayInfoInternal(displayId, callingUid);
1250 } finally {
1251 Binder.restoreCallingIdentity(token);
1252 }
1253 }
1254
1255 /**
1256 * Returns the list of all display ids.
1257 */
1258 @Override // Binder call
1259 public int[] getDisplayIds() {
1260 final int callingUid = Binder.getCallingUid();
1261 final long token = Binder.clearCallingIdentity();
1262 try {
1263 return getDisplayIdsInternal(callingUid);
1264 } finally {
1265 Binder.restoreCallingIdentity(token);
1266 }
1267 }
1268
1269 @Override // Binder call
1270 public void registerCallback(IDisplayManagerCallback callback) {
1271 if (callback == null) {
1272 throw new IllegalArgumentException("listener must not be null");
1273 }
1274
1275 final int callingPid = Binder.getCallingPid();
1276 final long token = Binder.clearCallingIdentity();
1277 try {
1278 registerCallbackInternal(callback, callingPid);
1279 } finally {
1280 Binder.restoreCallingIdentity(token);
1281 }
1282 }
1283
1284 @Override // Binder call
1285 public void startWifiDisplayScan() {
1286 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1287 "Permission required to start wifi display scans");
1288
1289 final int callingPid = Binder.getCallingPid();
1290 final long token = Binder.clearCallingIdentity();
1291 try {
1292 startWifiDisplayScanInternal(callingPid);
1293 } finally {
1294 Binder.restoreCallingIdentity(token);
1295 }
1296 }
1297
1298 @Override // Binder call
1299 public void stopWifiDisplayScan() {
1300 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1301 "Permission required to stop wifi display scans");
1302
1303 final int callingPid = Binder.getCallingPid();
1304 final long token = Binder.clearCallingIdentity();
1305 try {
1306 stopWifiDisplayScanInternal(callingPid);
1307 } finally {
1308 Binder.restoreCallingIdentity(token);
1309 }
1310 }
1311
1312 @Override // Binder call
1313 public void connectWifiDisplay(String address) {
1314 if (address == null) {
1315 throw new IllegalArgumentException("address must not be null");
1316 }
1317 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1318 "Permission required to connect to a wifi display");
1319
1320 final long token = Binder.clearCallingIdentity();
1321 try {
1322 connectWifiDisplayInternal(address);
1323 } finally {
1324 Binder.restoreCallingIdentity(token);
1325 }
1326 }
1327
1328 @Override // Binder call
1329 public void disconnectWifiDisplay() {
1330 // This request does not require special permissions.
1331 // Any app can request disconnection from the currently active wifi display.
1332 // This exception should no longer be needed once wifi display control moves
1333 // to the media router service.
1334
1335 final long token = Binder.clearCallingIdentity();
1336 try {
1337 disconnectWifiDisplayInternal();
1338 } finally {
1339 Binder.restoreCallingIdentity(token);
1340 }
1341 }
1342
1343 @Override // Binder call
1344 public void renameWifiDisplay(String address, String alias) {
1345 if (address == null) {
1346 throw new IllegalArgumentException("address must not be null");
1347 }
1348 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1349 "Permission required to rename to a wifi display");
1350
1351 final long token = Binder.clearCallingIdentity();
1352 try {
1353 renameWifiDisplayInternal(address, alias);
1354 } finally {
1355 Binder.restoreCallingIdentity(token);
1356 }
1357 }
1358
1359 @Override // Binder call
1360 public void forgetWifiDisplay(String address) {
1361 if (address == null) {
1362 throw new IllegalArgumentException("address must not be null");
1363 }
1364 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1365 "Permission required to forget to a wifi display");
1366
1367 final long token = Binder.clearCallingIdentity();
1368 try {
1369 forgetWifiDisplayInternal(address);
1370 } finally {
1371 Binder.restoreCallingIdentity(token);
1372 }
1373 }
1374
1375 @Override // Binder call
1376 public void pauseWifiDisplay() {
1377 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1378 "Permission required to pause a wifi display session");
1379
1380 final long token = Binder.clearCallingIdentity();
1381 try {
1382 pauseWifiDisplayInternal();
1383 } finally {
1384 Binder.restoreCallingIdentity(token);
1385 }
1386 }
1387
1388 @Override // Binder call
1389 public void resumeWifiDisplay() {
1390 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1391 "Permission required to resume a wifi display session");
1392
1393 final long token = Binder.clearCallingIdentity();
1394 try {
1395 resumeWifiDisplayInternal();
1396 } finally {
1397 Binder.restoreCallingIdentity(token);
1398 }
1399 }
1400
1401 @Override // Binder call
1402 public WifiDisplayStatus getWifiDisplayStatus() {
1403 // This request does not require special permissions.
1404 // Any app can get information about available wifi displays.
1405
1406 final long token = Binder.clearCallingIdentity();
1407 try {
1408 return getWifiDisplayStatusInternal();
1409 } finally {
1410 Binder.restoreCallingIdentity(token);
1411 }
1412 }
1413
1414 @Override // Binder call
Michael Wright1c9977b2016-07-12 13:30:10 -07001415 public void requestColorMode(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +01001416 mContext.enforceCallingOrSelfPermission(
Michael Wright1c9977b2016-07-12 13:30:10 -07001417 Manifest.permission.CONFIGURE_DISPLAY_COLOR_MODE,
1418 "Permission required to change the display color mode");
Michael Wright58e829f2015-09-15 00:13:26 +01001419 final long token = Binder.clearCallingIdentity();
1420 try {
Michael Wright1c9977b2016-07-12 13:30:10 -07001421 requestColorModeInternal(displayId, colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +01001422 } finally {
1423 Binder.restoreCallingIdentity(token);
1424 }
1425 }
1426
1427 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001428 public int createVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wrightc39d47a2014-07-08 18:07:36 -07001429 IMediaProjection projection, String packageName, String name,
1430 int width, int height, int densityDpi, Surface surface, int flags) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001431 final int callingUid = Binder.getCallingUid();
1432 if (!validatePackageName(callingUid, packageName)) {
1433 throw new SecurityException("packageName must match the calling uid");
1434 }
Michael Wright75ee9fc2014-09-01 19:55:22 -07001435 if (callback == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001436 throw new IllegalArgumentException("appToken must not be null");
1437 }
1438 if (TextUtils.isEmpty(name)) {
1439 throw new IllegalArgumentException("name must be non-null and non-empty");
1440 }
1441 if (width <= 0 || height <= 0 || densityDpi <= 0) {
1442 throw new IllegalArgumentException("width, height, and densityDpi must be "
1443 + "greater than 0");
1444 }
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001445 if (surface != null && surface.isSingleBuffered()) {
Pablo Ceballosaff2f942016-07-29 14:49:55 -07001446 throw new IllegalArgumentException("Surface can't be single-buffered");
1447 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001448
Michael Wright6720be42014-07-29 19:14:16 -07001449 if ((flags & DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC) != 0) {
1450 flags |= DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
1451 }
1452 if ((flags & DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY) != 0) {
1453 flags &= ~DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
1454 }
1455
Michael Wrightc39d47a2014-07-08 18:07:36 -07001456 if (projection != null) {
1457 try {
1458 if (!getProjectionService().isValidMediaProjection(projection)) {
1459 throw new SecurityException("Invalid media projection");
1460 }
Michael Wright6720be42014-07-29 19:14:16 -07001461 flags = projection.applyVirtualDisplayFlags(flags);
Michael Wrightc39d47a2014-07-08 18:07:36 -07001462 } catch (RemoteException e) {
Michael Wright6720be42014-07-29 19:14:16 -07001463 throw new SecurityException("unable to validate media projection or flags");
Michael Wrightc39d47a2014-07-08 18:07:36 -07001464 }
1465 }
1466
Michael Wright6720be42014-07-29 19:14:16 -07001467 if (callingUid != Process.SYSTEM_UID &&
1468 (flags & DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001469 if (!canProjectVideo(projection)) {
1470 throw new SecurityException("Requires CAPTURE_VIDEO_OUTPUT or "
1471 + "CAPTURE_SECURE_VIDEO_OUTPUT permission, or an appropriate "
1472 + "MediaProjection token in order to create a screen sharing virtual "
1473 + "display.");
1474 }
1475 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001476 if ((flags & DisplayManager.VIRTUAL_DISPLAY_FLAG_SECURE) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001477 if (!canProjectSecureVideo(projection)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001478 throw new SecurityException("Requires CAPTURE_SECURE_VIDEO_OUTPUT "
Michael Wrightc39d47a2014-07-08 18:07:36 -07001479 + "or an appropriate MediaProjection token to create a "
1480 + "secure virtual display.");
Jeff Brown4ccb8232014-01-16 22:16:42 -08001481 }
1482 }
1483
1484 final long token = Binder.clearCallingIdentity();
1485 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001486 return createVirtualDisplayInternal(callback, projection, callingUid,
Michael Wrightc39d47a2014-07-08 18:07:36 -07001487 packageName, name, width, height, densityDpi, surface, flags);
Jeff Brown4ccb8232014-01-16 22:16:42 -08001488 } finally {
1489 Binder.restoreCallingIdentity(token);
1490 }
1491 }
1492
1493 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001494 public void resizeVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wright01e840f2014-06-26 16:03:25 -07001495 int width, int height, int densityDpi) {
1496 final long token = Binder.clearCallingIdentity();
1497 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001498 resizeVirtualDisplayInternal(callback.asBinder(), width, height, densityDpi);
Michael Wright01e840f2014-06-26 16:03:25 -07001499 } finally {
1500 Binder.restoreCallingIdentity(token);
1501 }
1502 }
1503
1504 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001505 public void setVirtualDisplaySurface(IVirtualDisplayCallback callback, Surface surface) {
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001506 if (surface != null && surface.isSingleBuffered()) {
1507 throw new IllegalArgumentException("Surface can't be single-buffered");
1508 }
Jeff Brown92207df2014-04-16 13:16:07 -07001509 final long token = Binder.clearCallingIdentity();
1510 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001511 setVirtualDisplaySurfaceInternal(callback.asBinder(), surface);
Jeff Brown92207df2014-04-16 13:16:07 -07001512 } finally {
1513 Binder.restoreCallingIdentity(token);
1514 }
1515 }
1516
1517 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001518 public void releaseVirtualDisplay(IVirtualDisplayCallback callback) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001519 final long token = Binder.clearCallingIdentity();
1520 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001521 releaseVirtualDisplayInternal(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -08001522 } finally {
1523 Binder.restoreCallingIdentity(token);
1524 }
1525 }
1526
1527 @Override // Binder call
1528 public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) {
1529 if (mContext == null
1530 || mContext.checkCallingOrSelfPermission(Manifest.permission.DUMP)
1531 != PackageManager.PERMISSION_GRANTED) {
1532 pw.println("Permission Denial: can't dump DisplayManager from from pid="
1533 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
1534 return;
1535 }
1536
1537 final long token = Binder.clearCallingIdentity();
1538 try {
1539 dumpInternal(pw);
1540 } finally {
1541 Binder.restoreCallingIdentity(token);
1542 }
1543 }
1544
1545 private boolean validatePackageName(int uid, String packageName) {
1546 if (packageName != null) {
1547 String[] packageNames = mContext.getPackageManager().getPackagesForUid(uid);
1548 if (packageNames != null) {
1549 for (String n : packageNames) {
1550 if (n.equals(packageName)) {
1551 return true;
1552 }
1553 }
1554 }
1555 }
1556 return false;
1557 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001558
1559 private boolean canProjectVideo(IMediaProjection projection) {
1560 if (projection != null) {
1561 try {
1562 if (projection.canProjectVideo()) {
1563 return true;
1564 }
1565 } catch (RemoteException e) {
1566 Slog.e(TAG, "Unable to query projection service for permissions", e);
1567 }
1568 }
1569 if (mContext.checkCallingPermission(
1570 android.Manifest.permission.CAPTURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07001571 == PackageManager.PERMISSION_GRANTED) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001572 return true;
1573 }
1574 return canProjectSecureVideo(projection);
1575 }
1576
1577 private boolean canProjectSecureVideo(IMediaProjection projection) {
1578 if (projection != null) {
1579 try {
1580 if (projection.canProjectSecureVideo()){
1581 return true;
1582 }
1583 } catch (RemoteException e) {
1584 Slog.e(TAG, "Unable to query projection service for permissions", e);
1585 }
1586 }
1587 return mContext.checkCallingPermission(
1588 android.Manifest.permission.CAPTURE_SECURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07001589 == PackageManager.PERMISSION_GRANTED;
Michael Wrightc39d47a2014-07-08 18:07:36 -07001590 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001591 }
1592
1593 private final class LocalService extends DisplayManagerInternal {
1594 @Override
Jeff Brown037c33e2014-04-09 00:31:55 -07001595 public void initPowerManagement(final DisplayPowerCallbacks callbacks, Handler handler,
Jeff Brownad9ef192014-04-08 17:26:30 -07001596 SensorManager sensorManager) {
1597 synchronized (mSyncRoot) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001598 DisplayBlanker blanker = new DisplayBlanker() {
1599 @Override
Jeff Brown5d6443b2015-04-10 20:15:01 -07001600 public void requestDisplayState(int state, int brightness) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001601 // The order of operations is important for legacy reasons.
1602 if (state == Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07001603 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07001604 }
1605
1606 callbacks.onDisplayStateChange(state);
1607
1608 if (state != Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07001609 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07001610 }
1611 }
1612 };
Jeff Brownad9ef192014-04-08 17:26:30 -07001613 mDisplayPowerController = new DisplayPowerController(
Jeff Brown037c33e2014-04-09 00:31:55 -07001614 mContext, callbacks, handler, sensorManager, blanker);
Jeff Brownad9ef192014-04-08 17:26:30 -07001615 }
1616 }
1617
1618 @Override
1619 public boolean requestPowerState(DisplayPowerRequest request,
1620 boolean waitForNegativeProximity) {
1621 return mDisplayPowerController.requestPowerState(request,
1622 waitForNegativeProximity);
1623 }
1624
1625 @Override
1626 public boolean isProximitySensorAvailable() {
1627 return mDisplayPowerController.isProximitySensorAvailable();
1628 }
1629
1630 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08001631 public DisplayInfo getDisplayInfo(int displayId) {
1632 return getDisplayInfoInternal(displayId, Process.myUid());
1633 }
1634
1635 @Override
1636 public void registerDisplayTransactionListener(DisplayTransactionListener listener) {
1637 if (listener == null) {
1638 throw new IllegalArgumentException("listener must not be null");
1639 }
1640
1641 registerDisplayTransactionListenerInternal(listener);
1642 }
1643
1644 @Override
1645 public void unregisterDisplayTransactionListener(DisplayTransactionListener listener) {
1646 if (listener == null) {
1647 throw new IllegalArgumentException("listener must not be null");
1648 }
1649
1650 unregisterDisplayTransactionListenerInternal(listener);
1651 }
1652
1653 @Override
1654 public void setDisplayInfoOverrideFromWindowManager(int displayId, DisplayInfo info) {
1655 setDisplayInfoOverrideFromWindowManagerInternal(displayId, info);
1656 }
1657
1658 @Override
1659 public void performTraversalInTransactionFromWindowManager() {
1660 performTraversalInTransactionFromWindowManagerInternal();
1661 }
1662
1663 @Override
Michael Wright3f145a22014-07-22 19:46:03 -07001664 public void setDisplayProperties(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001665 float requestedRefreshRate, int requestedMode, boolean inTraversal) {
1666 setDisplayPropertiesInternal(displayId, hasContent, requestedRefreshRate,
1667 requestedMode, inTraversal);
Jeff Brown4ccb8232014-01-16 22:16:42 -08001668 }
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08001669
1670 @Override
1671 public void setDisplayOffsets(int displayId, int x, int y) {
1672 setDisplayOffsetsInternal(displayId, x, y);
1673 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001674
1675 @Override
1676 public void setDisplayAccessUIDs(SparseArray<IntArray> newDisplayAccessUIDs) {
1677 setDisplayAccessUIDsInternal(newDisplayAccessUIDs);
1678 }
1679
1680 @Override
1681 public boolean isUidPresentOnDisplay(int uid, int displayId) {
1682 return isUidPresentOnDisplayInternal(uid, displayId);
1683 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001684 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07001685}