blob: 9c762cce7e0faf4c7c8973e2bfc049ce163e715c [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
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700223 // The default color mode for default displays. Overrides the usual
224 // Display.Display.COLOR_MODE_DEFAULT for displays with the
225 // DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY flag set.
226 private final int mDefaultDisplayDefaultColorMode;
227
Jeff Browne75926d2014-09-18 15:24:49 -0700228 // Temporary list of deferred work to perform when setting the display state.
229 // Only used by requestDisplayState. The field is self-synchronized and only
230 // intended for use inside of the requestGlobalDisplayStateInternal function.
231 private final ArrayList<Runnable> mTempDisplayStateWorkQueue = new ArrayList<Runnable>();
232
Jeff Brownb880d882014-02-10 19:47:07 -0800233 public DisplayManagerService(Context context) {
234 super(context);
235 mContext = context;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800236 mHandler = new DisplayManagerHandler(DisplayThread.get().getLooper());
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700237 mUiHandler = UiThread.getHandler();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700238 mDisplayAdapterListener = new DisplayAdapterListener();
Jeff Brown27f1d672012-10-17 18:32:34 -0700239 mSingleDisplayDemoMode = SystemProperties.getBoolean("persist.demo.singledisplay", false);
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700240 mDefaultDisplayDefaultColorMode = mContext.getResources().getInteger(
241 com.android.internal.R.integer.config_defaultDisplayDefaultColorMode);
Jeff Brown5d6443b2015-04-10 20:15:01 -0700242
243 PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
244 mGlobalDisplayBrightness = pm.getDefaultScreenBrightnessSetting();
Craig Mautner4f67ba62012-08-02 11:23:00 -0700245 }
246
Jeff Brown4ccb8232014-01-16 22:16:42 -0800247 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -0800248 public void onStart() {
Michael Wright1c9977b2016-07-12 13:30:10 -0700249 // We need to pre-load the persistent data store so it's ready before the default display
250 // adapter is up so that we have it's configuration. We could load it lazily, but since
251 // we're going to have to read it in eventually we may as well do it here rather than after
252 // we've waited for the diplay to register itself with us.
253 mPersistentDataStore.loadIfNeeded();
Jeff Brown4ccb8232014-01-16 22:16:42 -0800254 mHandler.sendEmptyMessage(MSG_REGISTER_DEFAULT_DISPLAY_ADAPTER);
255
256 publishBinderService(Context.DISPLAY_SERVICE, new BinderService(),
257 true /*allowIsolated*/);
258 publishLocalService(DisplayManagerInternal.class, new LocalService());
Justin Klaassen22eb1992016-07-11 20:52:23 -0700259 publishLocalService(DisplayTransformManager.class, new DisplayTransformManager());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800260 }
261
262 @Override
263 public void onBootPhase(int phase) {
264 if (phase == PHASE_WAIT_FOR_DEFAULT_DISPLAY) {
265 synchronized (mSyncRoot) {
266 long timeout = SystemClock.uptimeMillis() + WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT;
267 while (mLogicalDisplays.get(Display.DEFAULT_DISPLAY) == null) {
268 long delay = timeout - SystemClock.uptimeMillis();
269 if (delay <= 0) {
270 throw new RuntimeException("Timeout waiting for default display "
271 + "to be initialized.");
272 }
273 if (DEBUG) {
274 Slog.d(TAG, "waitForDefaultDisplay: waiting, timeout=" + delay);
275 }
276 try {
277 mSyncRoot.wait(delay);
278 } catch (InterruptedException ex) {
279 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700280 }
281 }
282 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700283 }
284
Jeff Brown4ccb8232014-01-16 22:16:42 -0800285 // TODO: Use dependencies or a boot phase
286 public void windowManagerAndInputReady() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700287 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800288 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
289 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner65d11b32012-10-01 13:59:52 -0700290 scheduleTraversalLocked(false);
Jeff Brownd728bf52012-09-08 18:05:28 -0700291 }
292 }
293
294 /**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700295 * Called when the system is ready to go.
296 */
297 public void systemReady(boolean safeMode, boolean onlyCore) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700298 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700299 mSafeMode = safeMode;
300 mOnlyCore = onlyCore;
301 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700302
Jeff Brownbd6e1502012-08-28 03:27:37 -0700303 mHandler.sendEmptyMessage(MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS);
304 }
305
Jeff Brown4ccb8232014-01-16 22:16:42 -0800306 private void registerDisplayTransactionListenerInternal(
307 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800308 // List is self-synchronized copy-on-write.
309 mDisplayTransactionListeners.add(listener);
310 }
311
Jeff Brown4ccb8232014-01-16 22:16:42 -0800312 private void unregisterDisplayTransactionListenerInternal(
313 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800314 // List is self-synchronized copy-on-write.
315 mDisplayTransactionListeners.remove(listener);
316 }
317
Jeff Brown4ccb8232014-01-16 22:16:42 -0800318 private void setDisplayInfoOverrideFromWindowManagerInternal(
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700319 int displayId, DisplayInfo info) {
320 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700321 LogicalDisplay display = mLogicalDisplays.get(displayId);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700322 if (display != null) {
Jeff Brownef981a42013-08-07 14:13:37 -0700323 if (display.setDisplayInfoOverrideFromWindowManagerLocked(info)) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700324 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
Craig Mautner65d11b32012-10-01 13:59:52 -0700325 scheduleTraversalLocked(false);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700326 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700327 }
328 }
329 }
330
Jeff Brown4ccb8232014-01-16 22:16:42 -0800331 private void performTraversalInTransactionFromWindowManagerInternal() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700332 synchronized (mSyncRoot) {
333 if (!mPendingTraversal) {
334 return;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700335 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700336 mPendingTraversal = false;
337
338 performTraversalInTransactionLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700339 }
Jeff Brown7f3994e2012-12-04 14:04:28 -0800340
341 // List is self-synchronized copy-on-write.
342 for (DisplayTransactionListener listener : mDisplayTransactionListeners) {
343 listener.onDisplayTransaction();
344 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700345 }
346
Jeff Brown5d6443b2015-04-10 20:15:01 -0700347 private void requestGlobalDisplayStateInternal(int state, int brightness) {
348 if (state == Display.STATE_UNKNOWN) {
349 state = Display.STATE_ON;
350 }
351 if (state == Display.STATE_OFF) {
352 brightness = PowerManager.BRIGHTNESS_OFF;
353 } else if (brightness < 0) {
354 brightness = PowerManager.BRIGHTNESS_DEFAULT;
355 } else if (brightness > PowerManager.BRIGHTNESS_ON) {
356 brightness = PowerManager.BRIGHTNESS_ON;
357 }
358
Jeff Browne75926d2014-09-18 15:24:49 -0700359 synchronized (mTempDisplayStateWorkQueue) {
360 try {
361 // Update the display state within the lock.
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700362 // Note that we do not need to schedule traversals here although it
363 // may happen as a side-effect of displays changing state.
Jeff Browne75926d2014-09-18 15:24:49 -0700364 synchronized (mSyncRoot) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700365 if (mGlobalDisplayState == state
366 && mGlobalDisplayBrightness == brightness) {
367 return; // no change
Jeff Browne75926d2014-09-18 15:24:49 -0700368 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700369
370 Trace.traceBegin(Trace.TRACE_TAG_POWER, "requestGlobalDisplayState("
371 + Display.stateToString(state)
372 + ", brightness=" + brightness + ")");
373 mGlobalDisplayState = state;
374 mGlobalDisplayBrightness = brightness;
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700375 applyGlobalDisplayStateLocked(mTempDisplayStateWorkQueue);
Jeff Browne75926d2014-09-18 15:24:49 -0700376 }
377
378 // Setting the display power state can take hundreds of milliseconds
379 // to complete so we defer the most expensive part of the work until
380 // after we have exited the critical section to avoid blocking other
381 // threads for a long time.
382 for (int i = 0; i < mTempDisplayStateWorkQueue.size(); i++) {
383 mTempDisplayStateWorkQueue.get(i).run();
384 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700385 Trace.traceEnd(Trace.TRACE_TAG_POWER);
Jeff Browne75926d2014-09-18 15:24:49 -0700386 } finally {
387 mTempDisplayStateWorkQueue.clear();
Jeff Brown9e316a12012-10-08 19:17:06 -0700388 }
389 }
390 }
391
Jeff Brown4ccb8232014-01-16 22:16:42 -0800392 private DisplayInfo getDisplayInfoInternal(int displayId, int callingUid) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700393 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800394 LogicalDisplay display = mLogicalDisplays.get(displayId);
395 if (display != null) {
396 DisplayInfo info = display.getDisplayInfoLocked();
397 if (info.hasAccess(callingUid)) {
398 return info;
399 }
400 }
401 return null;
402 }
403 }
404
405 private int[] getDisplayIdsInternal(int callingUid) {
406 synchronized (mSyncRoot) {
407 final int count = mLogicalDisplays.size();
408 int[] displayIds = new int[count];
409 int n = 0;
410 for (int i = 0; i < count; i++) {
411 LogicalDisplay display = mLogicalDisplays.valueAt(i);
412 DisplayInfo info = display.getDisplayInfoLocked();
413 if (info.hasAccess(callingUid)) {
414 displayIds[n++] = mLogicalDisplays.keyAt(i);
415 }
416 }
417 if (n != count) {
418 displayIds = Arrays.copyOfRange(displayIds, 0, n);
419 }
420 return displayIds;
421 }
422 }
423
424 private void registerCallbackInternal(IDisplayManagerCallback callback, int callingPid) {
425 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700426 if (mCallbacks.get(callingPid) != null) {
427 throw new SecurityException("The calling process has already "
428 + "registered an IDisplayManagerCallback.");
Jeff Brown64a55af2012-08-26 02:47:39 -0700429 }
430
Jeff Brownbd6e1502012-08-28 03:27:37 -0700431 CallbackRecord record = new CallbackRecord(callingPid, callback);
432 try {
433 IBinder binder = callback.asBinder();
434 binder.linkToDeath(record, 0);
435 } catch (RemoteException ex) {
436 // give up
437 throw new RuntimeException(ex);
438 }
439
440 mCallbacks.put(callingPid, record);
441 }
442 }
443
Jeff Brownce468a32013-11-21 16:42:03 -0800444 private void onCallbackDied(CallbackRecord record) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700445 synchronized (mSyncRoot) {
Jeff Brownce468a32013-11-21 16:42:03 -0800446 mCallbacks.remove(record.mPid);
447 stopWifiDisplayScanLocked(record);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700448 }
449 }
450
Jeff Brown4ccb8232014-01-16 22:16:42 -0800451 private void startWifiDisplayScanInternal(int callingPid) {
452 synchronized (mSyncRoot) {
453 CallbackRecord record = mCallbacks.get(callingPid);
454 if (record == null) {
455 throw new IllegalStateException("The calling process has not "
456 + "registered an IDisplayManagerCallback.");
Jeff Browne08ae382012-09-07 20:36:36 -0700457 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800458 startWifiDisplayScanLocked(record);
Jeff Browne08ae382012-09-07 20:36:36 -0700459 }
460 }
461
Jeff Brownce468a32013-11-21 16:42:03 -0800462 private void startWifiDisplayScanLocked(CallbackRecord record) {
463 if (!record.mWifiDisplayScanRequested) {
464 record.mWifiDisplayScanRequested = true;
465 if (mWifiDisplayScanRequestCount++ == 0) {
466 if (mWifiDisplayAdapter != null) {
467 mWifiDisplayAdapter.requestStartScanLocked();
468 }
469 }
470 }
471 }
472
Jeff Brown4ccb8232014-01-16 22:16:42 -0800473 private void stopWifiDisplayScanInternal(int callingPid) {
474 synchronized (mSyncRoot) {
475 CallbackRecord record = mCallbacks.get(callingPid);
476 if (record == null) {
477 throw new IllegalStateException("The calling process has not "
478 + "registered an IDisplayManagerCallback.");
Jeff Brownce468a32013-11-21 16:42:03 -0800479 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800480 stopWifiDisplayScanLocked(record);
Jeff Brownce468a32013-11-21 16:42:03 -0800481 }
482 }
483
484 private void stopWifiDisplayScanLocked(CallbackRecord record) {
485 if (record.mWifiDisplayScanRequested) {
486 record.mWifiDisplayScanRequested = false;
487 if (--mWifiDisplayScanRequestCount == 0) {
488 if (mWifiDisplayAdapter != null) {
489 mWifiDisplayAdapter.requestStopScanLocked();
490 }
491 } else if (mWifiDisplayScanRequestCount < 0) {
Dianne Hackborn8d051722014-10-01 14:59:58 -0700492 Slog.wtf(TAG, "mWifiDisplayScanRequestCount became negative: "
Jeff Brownce468a32013-11-21 16:42:03 -0800493 + mWifiDisplayScanRequestCount);
494 mWifiDisplayScanRequestCount = 0;
495 }
496 }
497 }
498
Jeff Brown4ccb8232014-01-16 22:16:42 -0800499 private void connectWifiDisplayInternal(String address) {
500 synchronized (mSyncRoot) {
501 if (mWifiDisplayAdapter != null) {
502 mWifiDisplayAdapter.requestConnectLocked(address);
Jeff Browne08ae382012-09-07 20:36:36 -0700503 }
Jeff Browne08ae382012-09-07 20:36:36 -0700504 }
505 }
506
Jeff Brown4ccb8232014-01-16 22:16:42 -0800507 private void pauseWifiDisplayInternal() {
508 synchronized (mSyncRoot) {
509 if (mWifiDisplayAdapter != null) {
510 mWifiDisplayAdapter.requestPauseLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700511 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700512 }
513 }
514
Jeff Brown4ccb8232014-01-16 22:16:42 -0800515 private void resumeWifiDisplayInternal() {
516 synchronized (mSyncRoot) {
517 if (mWifiDisplayAdapter != null) {
518 mWifiDisplayAdapter.requestResumeLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700519 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700520 }
521 }
522
Jeff Brown4ccb8232014-01-16 22:16:42 -0800523 private void disconnectWifiDisplayInternal() {
524 synchronized (mSyncRoot) {
525 if (mWifiDisplayAdapter != null) {
526 mWifiDisplayAdapter.requestDisconnectLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700527 }
Jeff Browne08ae382012-09-07 20:36:36 -0700528 }
529 }
530
Jeff Brown4ccb8232014-01-16 22:16:42 -0800531 private void renameWifiDisplayInternal(String address, String alias) {
532 synchronized (mSyncRoot) {
533 if (mWifiDisplayAdapter != null) {
534 mWifiDisplayAdapter.requestRenameLocked(address, alias);
Jeff Brown89d55462012-09-19 11:33:42 -0700535 }
Jeff Brown89d55462012-09-19 11:33:42 -0700536 }
537 }
538
Jeff Brown4ccb8232014-01-16 22:16:42 -0800539 private void forgetWifiDisplayInternal(String address) {
540 synchronized (mSyncRoot) {
541 if (mWifiDisplayAdapter != null) {
542 mWifiDisplayAdapter.requestForgetLocked(address);
Jeff Brown89d55462012-09-19 11:33:42 -0700543 }
Jeff Brown89d55462012-09-19 11:33:42 -0700544 }
545 }
546
Jeff Brown4ccb8232014-01-16 22:16:42 -0800547 private WifiDisplayStatus getWifiDisplayStatusInternal() {
548 synchronized (mSyncRoot) {
549 if (mWifiDisplayAdapter != null) {
550 return mWifiDisplayAdapter.getWifiDisplayStatusLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700551 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800552 return new WifiDisplayStatus();
Jeff Browne08ae382012-09-07 20:36:36 -0700553 }
554 }
555
Michael Wright1c9977b2016-07-12 13:30:10 -0700556 private void requestColorModeInternal(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +0100557 synchronized (mSyncRoot) {
558 LogicalDisplay display = mLogicalDisplays.get(displayId);
559 if (display != null &&
Michael Wright1c9977b2016-07-12 13:30:10 -0700560 display.getRequestedColorModeLocked() != colorMode) {
561 display.setRequestedColorModeLocked(colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +0100562 scheduleTraversalLocked(false);
563 }
564 }
565 }
566
Michael Wright75ee9fc2014-09-01 19:55:22 -0700567 private int createVirtualDisplayInternal(IVirtualDisplayCallback callback,
Michael Wrightc39d47a2014-07-08 18:07:36 -0700568 IMediaProjection projection, int callingUid, String packageName,
Jeff Brown7d00aff2013-08-02 19:03:49 -0700569 String name, int width, int height, int densityDpi, Surface surface, int flags) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800570 synchronized (mSyncRoot) {
571 if (mVirtualDisplayAdapter == null) {
572 Slog.w(TAG, "Rejecting request to create private virtual display "
573 + "because the virtual display adapter is not available.");
574 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700575 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800576
577 DisplayDevice device = mVirtualDisplayAdapter.createVirtualDisplayLocked(
Michael Wright75ee9fc2014-09-01 19:55:22 -0700578 callback, projection, callingUid, packageName,
Michael Wrightc39d47a2014-07-08 18:07:36 -0700579 name, width, height, densityDpi, surface, flags);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800580 if (device == null) {
581 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700582 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700583
Jeff Brown4ccb8232014-01-16 22:16:42 -0800584 handleDisplayDeviceAddedLocked(device);
585 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
586 if (display != null) {
587 return display.getDisplayIdLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700588 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800589
590 // Something weird happened and the logical display was not created.
591 Slog.w(TAG, "Rejecting request to create virtual display "
592 + "because the logical display was not created.");
Michael Wright75ee9fc2014-09-01 19:55:22 -0700593 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800594 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700595 }
596 return -1;
597 }
598
Michael Wright01e840f2014-06-26 16:03:25 -0700599 private void resizeVirtualDisplayInternal(IBinder appToken,
600 int width, int height, int densityDpi) {
601 synchronized (mSyncRoot) {
602 if (mVirtualDisplayAdapter == null) {
603 return;
604 }
605
606 mVirtualDisplayAdapter.resizeVirtualDisplayLocked(appToken, width, height, densityDpi);
607 }
608 }
609
Jeff Brown92207df2014-04-16 13:16:07 -0700610 private void setVirtualDisplaySurfaceInternal(IBinder appToken, Surface surface) {
611 synchronized (mSyncRoot) {
612 if (mVirtualDisplayAdapter == null) {
613 return;
614 }
615
616 mVirtualDisplayAdapter.setVirtualDisplaySurfaceLocked(appToken, surface);
617 }
618 }
619
Jeff Brown4ccb8232014-01-16 22:16:42 -0800620 private void releaseVirtualDisplayInternal(IBinder appToken) {
621 synchronized (mSyncRoot) {
622 if (mVirtualDisplayAdapter == null) {
623 return;
Jeff Browna506a6e2013-06-04 00:02:38 -0700624 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700625
Jeff Brown4ccb8232014-01-16 22:16:42 -0800626 DisplayDevice device =
627 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(appToken);
628 if (device != null) {
629 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700630 }
631 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700632 }
633
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700634 private void registerDefaultDisplayAdapter() {
635 // Register default display adapter.
636 synchronized (mSyncRoot) {
Mike Lockwoode63f6f72013-11-15 11:01:47 -0800637 registerDisplayAdapterLocked(new LocalDisplayAdapter(
638 mSyncRoot, mContext, mHandler, mDisplayAdapterListener));
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700639 }
640 }
641
642 private void registerAdditionalDisplayAdapters() {
643 synchronized (mSyncRoot) {
644 if (shouldRegisterNonEssentialDisplayAdaptersLocked()) {
Jeff Brown89d55462012-09-19 11:33:42 -0700645 registerOverlayDisplayAdapterLocked();
646 registerWifiDisplayAdapterLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700647 registerVirtualDisplayAdapterLocked();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700648 }
649 }
650 }
651
Jeff Brown89d55462012-09-19 11:33:42 -0700652 private void registerOverlayDisplayAdapterLocked() {
653 registerDisplayAdapterLocked(new OverlayDisplayAdapter(
654 mSyncRoot, mContext, mHandler, mDisplayAdapterListener, mUiHandler));
655 }
656
657 private void registerWifiDisplayAdapterLocked() {
658 if (mContext.getResources().getBoolean(
Jeff Brownbbd28a22012-09-20 16:47:15 -0700659 com.android.internal.R.bool.config_enableWifiDisplay)
660 || SystemProperties.getInt(FORCE_WIFI_DISPLAY_ENABLE, -1) == 1) {
Jeff Brown89d55462012-09-19 11:33:42 -0700661 mWifiDisplayAdapter = new WifiDisplayAdapter(
662 mSyncRoot, mContext, mHandler, mDisplayAdapterListener,
663 mPersistentDataStore);
664 registerDisplayAdapterLocked(mWifiDisplayAdapter);
665 }
666 }
667
Jeff Browna506a6e2013-06-04 00:02:38 -0700668 private void registerVirtualDisplayAdapterLocked() {
669 mVirtualDisplayAdapter = new VirtualDisplayAdapter(
670 mSyncRoot, mContext, mHandler, mDisplayAdapterListener);
671 registerDisplayAdapterLocked(mVirtualDisplayAdapter);
672 }
673
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700674 private boolean shouldRegisterNonEssentialDisplayAdaptersLocked() {
675 // In safe mode, we disable non-essential display adapters to give the user
676 // an opportunity to fix broken settings or other problems that might affect
677 // system stability.
678 // In only-core mode, we disable non-essential display adapters to minimize
679 // the number of dependencies that are started while in this mode and to
680 // prevent problems that might occur due to the device being encrypted.
681 return !mSafeMode && !mOnlyCore;
682 }
683
684 private void registerDisplayAdapterLocked(DisplayAdapter adapter) {
685 mDisplayAdapters.add(adapter);
686 adapter.registerLocked();
687 }
688
Jeff Brownbd6e1502012-08-28 03:27:37 -0700689 private void handleDisplayDeviceAdded(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700690 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700691 handleDisplayDeviceAddedLocked(device);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700692 }
693 }
694
Jeff Browna506a6e2013-06-04 00:02:38 -0700695 private void handleDisplayDeviceAddedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700696 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700697 if (mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700698 Slog.w(TAG, "Attempted to add already added display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700699 return;
700 }
701
Jeff Brown10acf6d2015-04-14 14:20:47 -0700702 Slog.i(TAG, "Display device added: " + info);
703 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700704
705 mDisplayDevices.add(device);
Michael Wright1c9977b2016-07-12 13:30:10 -0700706 LogicalDisplay display = addLogicalDisplayLocked(device);
Jeff Brown0033a862014-10-08 12:06:39 -0700707 Runnable work = updateDisplayStateLocked(device);
708 if (work != null) {
709 work.run();
710 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700711 if (display != null && display.getPrimaryDisplayDeviceLocked() == device) {
712 int colorMode = mPersistentDataStore.getColorMode(device);
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700713 if (colorMode == Display.COLOR_MODE_INVALID) {
714 if ((device.getDisplayDeviceInfoLocked().flags
715 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
716 colorMode = mDefaultDisplayDefaultColorMode;
717 } else {
718 colorMode = Display.COLOR_MODE_DEFAULT;
719 }
720 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700721 display.setRequestedColorModeLocked(colorMode);
722 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700723 scheduleTraversalLocked(false);
724 }
725
Jeff Brownbd6e1502012-08-28 03:27:37 -0700726 private void handleDisplayDeviceChanged(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700727 synchronized (mSyncRoot) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700728 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700729 if (!mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700730 Slog.w(TAG, "Attempted to change non-existent display device: " + info);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700731 return;
732 }
733
Jeff Brown10acf6d2015-04-14 14:20:47 -0700734 int diff = device.mDebugLastLoggedDeviceInfo.diff(info);
735 if (diff == DisplayDeviceInfo.DIFF_STATE) {
736 Slog.i(TAG, "Display device changed state: \"" + info.name
737 + "\", " + Display.stateToString(info.state));
738 } else if (diff != 0) {
739 Slog.i(TAG, "Display device changed: " + info);
740 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700741 if ((diff & DisplayDeviceInfo.DIFF_COLOR_MODE) != 0) {
742 try {
743 mPersistentDataStore.setColorMode(device, info.colorMode);
744 } finally {
745 mPersistentDataStore.saveIfNeeded();
746 }
747 }
Jeff Brown10acf6d2015-04-14 14:20:47 -0700748 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browne87bf032012-09-20 18:30:13 -0700749
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700750 device.applyPendingDisplayDeviceInfoChangesLocked();
751 if (updateLogicalDisplaysLocked()) {
Craig Mautner65d11b32012-10-01 13:59:52 -0700752 scheduleTraversalLocked(false);
Jeff Brown64a55af2012-08-26 02:47:39 -0700753 }
754 }
755 }
756
Jeff Brownbd6e1502012-08-28 03:27:37 -0700757 private void handleDisplayDeviceRemoved(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700758 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700759 handleDisplayDeviceRemovedLocked(device);
760 }
761 }
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700762
Jeff Browna506a6e2013-06-04 00:02:38 -0700763 private void handleDisplayDeviceRemovedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700764 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700765 if (!mDisplayDevices.remove(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700766 Slog.w(TAG, "Attempted to remove non-existent display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700767 return;
768 }
769
Jeff Brown10acf6d2015-04-14 14:20:47 -0700770 Slog.i(TAG, "Display device removed: " + info);
771 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700772
Jeff Browna506a6e2013-06-04 00:02:38 -0700773 updateLogicalDisplaysLocked();
774 scheduleTraversalLocked(false);
775 }
776
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700777 private void applyGlobalDisplayStateLocked(List<Runnable> workQueue) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700778 final int count = mDisplayDevices.size();
779 for (int i = 0; i < count; i++) {
780 DisplayDevice device = mDisplayDevices.get(i);
Jeff Browne75926d2014-09-18 15:24:49 -0700781 Runnable runnable = updateDisplayStateLocked(device);
782 if (runnable != null) {
783 workQueue.add(runnable);
784 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700785 }
786 }
787
Jeff Browne75926d2014-09-18 15:24:49 -0700788 private Runnable updateDisplayStateLocked(DisplayDevice device) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700789 // Blank or unblank the display immediately to match the state requested
Jeff Brown037c33e2014-04-09 00:31:55 -0700790 // by the display power controller (if known).
Jeff Browna506a6e2013-06-04 00:02:38 -0700791 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
792 if ((info.flags & DisplayDeviceInfo.FLAG_NEVER_BLANK) == 0) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700793 return device.requestDisplayStateLocked(mGlobalDisplayState, mGlobalDisplayBrightness);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700794 }
Jeff Browne75926d2014-09-18 15:24:49 -0700795 return null;
Craig Mautner4f67ba62012-08-02 11:23:00 -0700796 }
797
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700798 // Adds a new logical display based on the given display device.
799 // Sends notifications if needed.
Michael Wright1c9977b2016-07-12 13:30:10 -0700800 private LogicalDisplay addLogicalDisplayLocked(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700801 DisplayDeviceInfo deviceInfo = device.getDisplayDeviceInfoLocked();
802 boolean isDefault = (deviceInfo.flags
803 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0;
804 if (isDefault && mLogicalDisplays.get(Display.DEFAULT_DISPLAY) != null) {
805 Slog.w(TAG, "Ignoring attempt to add a second default display: " + deviceInfo);
806 isDefault = false;
807 }
808
Jeff Brown27f1d672012-10-17 18:32:34 -0700809 if (!isDefault && mSingleDisplayDemoMode) {
810 Slog.i(TAG, "Not creating a logical display for a secondary display "
811 + " because single display demo mode is enabled: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -0700812 return null;
Jeff Brown27f1d672012-10-17 18:32:34 -0700813 }
814
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700815 final int displayId = assignDisplayIdLocked(isDefault);
816 final int layerStack = assignLayerStackLocked(displayId);
817
Jeff Brownd728bf52012-09-08 18:05:28 -0700818 LogicalDisplay display = new LogicalDisplay(displayId, layerStack, device);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700819 display.updateLocked(mDisplayDevices);
820 if (!display.isValidLocked()) {
821 // This should never happen currently.
822 Slog.w(TAG, "Ignoring display device because the logical display "
823 + "created from it was not considered valid: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -0700824 return null;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700825 }
826
827 mLogicalDisplays.put(displayId, display);
828
829 // Wake up waitForDefaultDisplay.
830 if (isDefault) {
831 mSyncRoot.notifyAll();
832 }
833
834 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_ADDED);
Michael Wright1c9977b2016-07-12 13:30:10 -0700835 return display;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700836 }
837
838 private int assignDisplayIdLocked(boolean isDefault) {
839 return isDefault ? Display.DEFAULT_DISPLAY : mNextNonDefaultDisplayId++;
840 }
841
842 private int assignLayerStackLocked(int displayId) {
843 // Currently layer stacks and display ids are the same.
844 // This need not be the case.
845 return displayId;
846 }
847
848 // Updates all existing logical displays given the current set of display devices.
849 // Removes invalid logical displays.
850 // Sends notifications if needed.
851 private boolean updateLogicalDisplaysLocked() {
852 boolean changed = false;
853 for (int i = mLogicalDisplays.size(); i-- > 0; ) {
854 final int displayId = mLogicalDisplays.keyAt(i);
855 LogicalDisplay display = mLogicalDisplays.valueAt(i);
856
857 mTempDisplayInfo.copyFrom(display.getDisplayInfoLocked());
858 display.updateLocked(mDisplayDevices);
859 if (!display.isValidLocked()) {
860 mLogicalDisplays.removeAt(i);
861 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_REMOVED);
862 changed = true;
863 } else if (!mTempDisplayInfo.equals(display.getDisplayInfoLocked())) {
864 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
865 changed = true;
866 }
867 }
868 return changed;
869 }
870
871 private void performTraversalInTransactionLocked() {
Jeff Brownd728bf52012-09-08 18:05:28 -0700872 // Clear all viewports before configuring displays so that we can keep
873 // track of which ones we have configured.
874 clearViewportsLocked();
875
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700876 // Configure each display device.
877 final int count = mDisplayDevices.size();
878 for (int i = 0; i < count; i++) {
879 DisplayDevice device = mDisplayDevices.get(i);
880 configureDisplayInTransactionLocked(device);
881 device.performTraversalInTransactionLocked();
882 }
Jeff Brownd728bf52012-09-08 18:05:28 -0700883
884 // Tell the input system about these new viewports.
Jeff Brown4ccb8232014-01-16 22:16:42 -0800885 if (mInputManagerInternal != null) {
Jeff Brownd728bf52012-09-08 18:05:28 -0700886 mHandler.sendEmptyMessage(MSG_UPDATE_VIEWPORT);
887 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700888 }
889
Michael Wright3f145a22014-07-22 19:46:03 -0700890 private void setDisplayPropertiesInternal(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700891 float requestedRefreshRate, int requestedModeId, boolean inTraversal) {
Craig Mautner722285e2012-09-07 13:55:58 -0700892 synchronized (mSyncRoot) {
893 LogicalDisplay display = mLogicalDisplays.get(displayId);
Michael Wright3f145a22014-07-22 19:46:03 -0700894 if (display == null) {
895 return;
896 }
897 if (display.hasContentLocked() != hasContent) {
Jeff Brown33041bd2013-08-02 21:11:14 -0700898 if (DEBUG) {
899 Slog.d(TAG, "Display " + displayId + " hasContent flag changed: "
900 + "hasContent=" + hasContent + ", inTraversal=" + inTraversal);
901 }
902
Craig Mautner722285e2012-09-07 13:55:58 -0700903 display.setHasContentLocked(hasContent);
Craig Mautner65d11b32012-10-01 13:59:52 -0700904 scheduleTraversalLocked(inTraversal);
Craig Mautner722285e2012-09-07 13:55:58 -0700905 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700906 if (requestedModeId == 0 && requestedRefreshRate != 0) {
907 // Scan supported modes returned by display.getInfo() to find a mode with the same
908 // size as the default display mode but with the specified refresh rate instead.
909 requestedModeId = display.getDisplayInfoLocked().findDefaultModeByRefreshRate(
910 requestedRefreshRate);
911 }
912 if (display.getRequestedModeIdLocked() != requestedModeId) {
Michael Wright3f145a22014-07-22 19:46:03 -0700913 if (DEBUG) {
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700914 Slog.d(TAG, "Display " + displayId + " switching to mode " + requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -0700915 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700916 display.setRequestedModeIdLocked(requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -0700917 scheduleTraversalLocked(inTraversal);
918 }
Craig Mautner722285e2012-09-07 13:55:58 -0700919 }
Jeff Brownd728bf52012-09-08 18:05:28 -0700920 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700921
Filip Gruszczynskid2e86402015-02-19 13:05:03 -0800922 private void setDisplayOffsetsInternal(int displayId, int x, int y) {
923 synchronized (mSyncRoot) {
924 LogicalDisplay display = mLogicalDisplays.get(displayId);
925 if (display == null) {
926 return;
927 }
928 if (display.getDisplayOffsetXLocked() != x
929 || display.getDisplayOffsetYLocked() != y) {
930 if (DEBUG) {
931 Slog.d(TAG, "Display " + displayId + " burn-in offset set to ("
932 + x + ", " + y + ")");
933 }
934 display.setDisplayOffsetsLocked(x, y);
935 scheduleTraversalLocked(false);
936 }
937 }
938 }
939
Jeff Brownd728bf52012-09-08 18:05:28 -0700940 private void clearViewportsLocked() {
941 mDefaultViewport.valid = false;
942 mExternalTouchViewport.valid = false;
Craig Mautner722285e2012-09-07 13:55:58 -0700943 }
944
945 private void configureDisplayInTransactionLocked(DisplayDevice device) {
Jeff Brownd14c8c92014-01-07 18:13:09 -0800946 final DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
947 final boolean ownContent = (info.flags & DisplayDeviceInfo.FLAG_OWN_CONTENT_ONLY) != 0;
Jeff Browna506a6e2013-06-04 00:02:38 -0700948
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700949 // Find the logical display that the display device is showing.
Jeff Brownd14c8c92014-01-07 18:13:09 -0800950 // Certain displays only ever show their own content.
Craig Mautner722285e2012-09-07 13:55:58 -0700951 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
Jeff Brownd14c8c92014-01-07 18:13:09 -0800952 if (!ownContent) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700953 if (display != null && !display.hasContentLocked()) {
954 // If the display does not have any content of its own, then
955 // automatically mirror the default logical display contents.
956 display = null;
957 }
958 if (display == null) {
959 display = mLogicalDisplays.get(Display.DEFAULT_DISPLAY);
960 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700961 }
962
963 // Apply the logical display configuration to the display device.
964 if (display == null) {
965 // TODO: no logical display for the device, blank it
Jeff Brownd728bf52012-09-08 18:05:28 -0700966 Slog.w(TAG, "Missing logical display to use for physical display device: "
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700967 + device.getDisplayDeviceInfoLocked());
Jeff Brownd728bf52012-09-08 18:05:28 -0700968 return;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700969 }
Jeff Brown037c33e2014-04-09 00:31:55 -0700970 display.configureDisplayInTransactionLocked(device, info.state == Display.STATE_OFF);
Jeff Brownd728bf52012-09-08 18:05:28 -0700971
972 // Update the viewports if needed.
Jeff Brownd728bf52012-09-08 18:05:28 -0700973 if (!mDefaultViewport.valid
974 && (info.flags & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
975 setViewportLocked(mDefaultViewport, display, device);
976 }
977 if (!mExternalTouchViewport.valid
978 && info.touch == DisplayDeviceInfo.TOUCH_EXTERNAL) {
979 setViewportLocked(mExternalTouchViewport, display, device);
980 }
981 }
982
983 private static void setViewportLocked(DisplayViewport viewport,
984 LogicalDisplay display, DisplayDevice device) {
985 viewport.valid = true;
986 viewport.displayId = display.getDisplayIdLocked();
987 device.populateViewportLocked(viewport);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700988 }
989
990 private LogicalDisplay findLogicalDisplayForDeviceLocked(DisplayDevice device) {
991 final int count = mLogicalDisplays.size();
992 for (int i = 0; i < count; i++) {
993 LogicalDisplay display = mLogicalDisplays.valueAt(i);
994 if (display.getPrimaryDisplayDeviceLocked() == device) {
995 return display;
996 }
997 }
998 return null;
999 }
1000
Jeff Brownbd6e1502012-08-28 03:27:37 -07001001 private void sendDisplayEventLocked(int displayId, int event) {
1002 Message msg = mHandler.obtainMessage(MSG_DELIVER_DISPLAY_EVENT, displayId, event);
1003 mHandler.sendMessage(msg);
1004 }
1005
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001006 // Requests that performTraversalsInTransactionFromWindowManager be called at a
1007 // later time to apply changes to surfaces and displays.
Craig Mautner65d11b32012-10-01 13:59:52 -07001008 private void scheduleTraversalLocked(boolean inTraversal) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001009 if (!mPendingTraversal && mWindowManagerInternal != null) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001010 mPendingTraversal = true;
Craig Mautner65d11b32012-10-01 13:59:52 -07001011 if (!inTraversal) {
1012 mHandler.sendEmptyMessage(MSG_REQUEST_TRAVERSAL);
1013 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001014 }
1015 }
1016
1017 // Runs on Handler thread.
1018 // Delivers display event notifications to callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001019 private void deliverDisplayEvent(int displayId, int event) {
1020 if (DEBUG) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001021 Slog.d(TAG, "Delivering display event: displayId="
1022 + displayId + ", event=" + event);
Jeff Brownfa25bf52012-07-23 19:26:30 -07001023 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07001024
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001025 // Grab the lock and copy the callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001026 final int count;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001027 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001028 count = mCallbacks.size();
1029 mTempCallbacks.clear();
1030 for (int i = 0; i < count; i++) {
1031 mTempCallbacks.add(mCallbacks.valueAt(i));
Craig Mautner4f67ba62012-08-02 11:23:00 -07001032 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001033 }
Craig Mautner4f67ba62012-08-02 11:23:00 -07001034
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001035 // After releasing the lock, send the notifications out.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001036 for (int i = 0; i < count; i++) {
1037 mTempCallbacks.get(i).notifyDisplayEventAsync(displayId, event);
1038 }
1039 mTempCallbacks.clear();
Craig Mautner4f67ba62012-08-02 11:23:00 -07001040 }
1041
Michael Wrightc39d47a2014-07-08 18:07:36 -07001042 private IMediaProjectionManager getProjectionService() {
1043 if (mProjectionService == null) {
1044 IBinder b = ServiceManager.getService(Context.MEDIA_PROJECTION_SERVICE);
1045 mProjectionService = IMediaProjectionManager.Stub.asInterface(b);
1046 }
1047 return mProjectionService;
1048 }
1049
Jeff Brown4ccb8232014-01-16 22:16:42 -08001050 private void dumpInternal(PrintWriter pw) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001051 pw.println("DISPLAY MANAGER (dumpsys display)");
Jeff Brownfa25bf52012-07-23 19:26:30 -07001052
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001053 synchronized (mSyncRoot) {
Jeff Brown9e316a12012-10-08 19:17:06 -07001054 pw.println(" mOnlyCode=" + mOnlyCore);
1055 pw.println(" mSafeMode=" + mSafeMode);
1056 pw.println(" mPendingTraversal=" + mPendingTraversal);
Jeff Brown037c33e2014-04-09 00:31:55 -07001057 pw.println(" mGlobalDisplayState=" + Display.stateToString(mGlobalDisplayState));
Jeff Brown9e316a12012-10-08 19:17:06 -07001058 pw.println(" mNextNonDefaultDisplayId=" + mNextNonDefaultDisplayId);
1059 pw.println(" mDefaultViewport=" + mDefaultViewport);
1060 pw.println(" mExternalTouchViewport=" + mExternalTouchViewport);
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -07001061 pw.println(" mDefaultDisplayDefaultColorMode=" + mDefaultDisplayDefaultColorMode);
Jeff Brown27f1d672012-10-17 18:32:34 -07001062 pw.println(" mSingleDisplayDemoMode=" + mSingleDisplayDemoMode);
Jeff Brownce468a32013-11-21 16:42:03 -08001063 pw.println(" mWifiDisplayScanRequestCount=" + mWifiDisplayScanRequestCount);
Jeff Brown9e316a12012-10-08 19:17:06 -07001064
Jeff Brownbd6e1502012-08-28 03:27:37 -07001065 IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001066 ipw.increaseIndent();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001067
1068 pw.println();
1069 pw.println("Display Adapters: size=" + mDisplayAdapters.size());
Jeff Brown848c2dc2012-08-19 20:18:08 -07001070 for (DisplayAdapter adapter : mDisplayAdapters) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001071 pw.println(" " + adapter.getName());
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001072 adapter.dumpLocked(ipw);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001073 }
Craig Mautner9de49362012-08-02 14:30:30 -07001074
Jeff Brownbd6e1502012-08-28 03:27:37 -07001075 pw.println();
1076 pw.println("Display Devices: size=" + mDisplayDevices.size());
1077 for (DisplayDevice device : mDisplayDevices) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001078 pw.println(" " + device.getDisplayDeviceInfoLocked());
1079 device.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001080 }
1081
1082 final int logicalDisplayCount = mLogicalDisplays.size();
1083 pw.println();
1084 pw.println("Logical Displays: size=" + logicalDisplayCount);
1085 for (int i = 0; i < logicalDisplayCount; i++) {
1086 int displayId = mLogicalDisplays.keyAt(i);
1087 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1088 pw.println(" Display " + displayId + ":");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001089 display.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001090 }
Jeff Brownce468a32013-11-21 16:42:03 -08001091
1092 final int callbackCount = mCallbacks.size();
1093 pw.println();
1094 pw.println("Callbacks: size=" + callbackCount);
1095 for (int i = 0; i < callbackCount; i++) {
1096 CallbackRecord callback = mCallbacks.valueAt(i);
1097 pw.println(" " + i + ": mPid=" + callback.mPid
1098 + ", mWifiDisplayScanRequested=" + callback.mWifiDisplayScanRequested);
1099 }
Jeff Brownad9ef192014-04-08 17:26:30 -07001100
1101 if (mDisplayPowerController != null) {
1102 mDisplayPowerController.dump(pw);
1103 }
Michael Wright1c9977b2016-07-12 13:30:10 -07001104
1105 pw.println();
1106 mPersistentDataStore.dump(pw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001107 }
1108 }
1109
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001110 /**
1111 * This is the object that everything in the display manager locks on.
1112 * We make it an inner class within the {@link DisplayManagerService} to so that it is
1113 * clear that the object belongs to the display manager service and that it is
1114 * a unique object with a special purpose.
1115 */
1116 public static final class SyncRoot {
1117 }
1118
Jeff Brownbd6e1502012-08-28 03:27:37 -07001119 private final class DisplayManagerHandler extends Handler {
1120 public DisplayManagerHandler(Looper looper) {
1121 super(looper, null, true /*async*/);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001122 }
Jeff Brownbf5740e2012-08-19 23:20:02 -07001123
Jeff Brownbd6e1502012-08-28 03:27:37 -07001124 @Override
1125 public void handleMessage(Message msg) {
1126 switch (msg.what) {
1127 case MSG_REGISTER_DEFAULT_DISPLAY_ADAPTER:
1128 registerDefaultDisplayAdapter();
1129 break;
1130
1131 case MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS:
1132 registerAdditionalDisplayAdapters();
1133 break;
1134
1135 case MSG_DELIVER_DISPLAY_EVENT:
1136 deliverDisplayEvent(msg.arg1, msg.arg2);
1137 break;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001138
1139 case MSG_REQUEST_TRAVERSAL:
Jeff Brown4ccb8232014-01-16 22:16:42 -08001140 mWindowManagerInternal.requestTraversalFromDisplayManager();
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001141 break;
Jeff Brownd728bf52012-09-08 18:05:28 -07001142
1143 case MSG_UPDATE_VIEWPORT: {
1144 synchronized (mSyncRoot) {
1145 mTempDefaultViewport.copyFrom(mDefaultViewport);
1146 mTempExternalTouchViewport.copyFrom(mExternalTouchViewport);
1147 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001148 mInputManagerInternal.setDisplayViewports(
Jeff Brownd728bf52012-09-08 18:05:28 -07001149 mTempDefaultViewport, mTempExternalTouchViewport);
1150 break;
1151 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001152 }
1153 }
1154 }
1155
1156 private final class DisplayAdapterListener implements DisplayAdapter.Listener {
1157 @Override
1158 public void onDisplayDeviceEvent(DisplayDevice device, int event) {
1159 switch (event) {
1160 case DisplayAdapter.DISPLAY_DEVICE_EVENT_ADDED:
1161 handleDisplayDeviceAdded(device);
1162 break;
1163
1164 case DisplayAdapter.DISPLAY_DEVICE_EVENT_CHANGED:
1165 handleDisplayDeviceChanged(device);
1166 break;
1167
1168 case DisplayAdapter.DISPLAY_DEVICE_EVENT_REMOVED:
1169 handleDisplayDeviceRemoved(device);
1170 break;
1171 }
1172 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001173
1174 @Override
1175 public void onTraversalRequested() {
1176 synchronized (mSyncRoot) {
Craig Mautner65d11b32012-10-01 13:59:52 -07001177 scheduleTraversalLocked(false);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001178 }
1179 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001180 }
1181
1182 private final class CallbackRecord implements DeathRecipient {
Jeff Brownce468a32013-11-21 16:42:03 -08001183 public final int mPid;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001184 private final IDisplayManagerCallback mCallback;
1185
Jeff Brownce468a32013-11-21 16:42:03 -08001186 public boolean mWifiDisplayScanRequested;
1187
Jeff Brownbd6e1502012-08-28 03:27:37 -07001188 public CallbackRecord(int pid, IDisplayManagerCallback callback) {
1189 mPid = pid;
1190 mCallback = callback;
1191 }
1192
1193 @Override
1194 public void binderDied() {
1195 if (DEBUG) {
1196 Slog.d(TAG, "Display listener for pid " + mPid + " died.");
1197 }
Jeff Brownce468a32013-11-21 16:42:03 -08001198 onCallbackDied(this);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001199 }
1200
1201 public void notifyDisplayEventAsync(int displayId, int event) {
1202 try {
1203 mCallback.onDisplayEvent(displayId, event);
1204 } catch (RemoteException ex) {
1205 Slog.w(TAG, "Failed to notify process "
1206 + mPid + " that displays changed, assuming it died.", ex);
1207 binderDied();
1208 }
1209 }
1210 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001211
1212 private final class BinderService extends IDisplayManager.Stub {
1213 /**
1214 * Returns information about the specified logical display.
1215 *
1216 * @param displayId The logical display id.
1217 * @return The logical display info, or null if the display does not exist. The
1218 * returned object must be treated as immutable.
1219 */
1220 @Override // Binder call
1221 public DisplayInfo getDisplayInfo(int displayId) {
1222 final int callingUid = Binder.getCallingUid();
1223 final long token = Binder.clearCallingIdentity();
1224 try {
1225 return getDisplayInfoInternal(displayId, callingUid);
1226 } finally {
1227 Binder.restoreCallingIdentity(token);
1228 }
1229 }
1230
1231 /**
1232 * Returns the list of all display ids.
1233 */
1234 @Override // Binder call
1235 public int[] getDisplayIds() {
1236 final int callingUid = Binder.getCallingUid();
1237 final long token = Binder.clearCallingIdentity();
1238 try {
1239 return getDisplayIdsInternal(callingUid);
1240 } finally {
1241 Binder.restoreCallingIdentity(token);
1242 }
1243 }
1244
1245 @Override // Binder call
1246 public void registerCallback(IDisplayManagerCallback callback) {
1247 if (callback == null) {
1248 throw new IllegalArgumentException("listener must not be null");
1249 }
1250
1251 final int callingPid = Binder.getCallingPid();
1252 final long token = Binder.clearCallingIdentity();
1253 try {
1254 registerCallbackInternal(callback, callingPid);
1255 } finally {
1256 Binder.restoreCallingIdentity(token);
1257 }
1258 }
1259
1260 @Override // Binder call
1261 public void startWifiDisplayScan() {
1262 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1263 "Permission required to start wifi display scans");
1264
1265 final int callingPid = Binder.getCallingPid();
1266 final long token = Binder.clearCallingIdentity();
1267 try {
1268 startWifiDisplayScanInternal(callingPid);
1269 } finally {
1270 Binder.restoreCallingIdentity(token);
1271 }
1272 }
1273
1274 @Override // Binder call
1275 public void stopWifiDisplayScan() {
1276 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1277 "Permission required to stop wifi display scans");
1278
1279 final int callingPid = Binder.getCallingPid();
1280 final long token = Binder.clearCallingIdentity();
1281 try {
1282 stopWifiDisplayScanInternal(callingPid);
1283 } finally {
1284 Binder.restoreCallingIdentity(token);
1285 }
1286 }
1287
1288 @Override // Binder call
1289 public void connectWifiDisplay(String address) {
1290 if (address == null) {
1291 throw new IllegalArgumentException("address must not be null");
1292 }
1293 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1294 "Permission required to connect to a wifi display");
1295
1296 final long token = Binder.clearCallingIdentity();
1297 try {
1298 connectWifiDisplayInternal(address);
1299 } finally {
1300 Binder.restoreCallingIdentity(token);
1301 }
1302 }
1303
1304 @Override // Binder call
1305 public void disconnectWifiDisplay() {
1306 // This request does not require special permissions.
1307 // Any app can request disconnection from the currently active wifi display.
1308 // This exception should no longer be needed once wifi display control moves
1309 // to the media router service.
1310
1311 final long token = Binder.clearCallingIdentity();
1312 try {
1313 disconnectWifiDisplayInternal();
1314 } finally {
1315 Binder.restoreCallingIdentity(token);
1316 }
1317 }
1318
1319 @Override // Binder call
1320 public void renameWifiDisplay(String address, String alias) {
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 rename to a wifi display");
1326
1327 final long token = Binder.clearCallingIdentity();
1328 try {
1329 renameWifiDisplayInternal(address, alias);
1330 } finally {
1331 Binder.restoreCallingIdentity(token);
1332 }
1333 }
1334
1335 @Override // Binder call
1336 public void forgetWifiDisplay(String address) {
1337 if (address == null) {
1338 throw new IllegalArgumentException("address must not be null");
1339 }
1340 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1341 "Permission required to forget to a wifi display");
1342
1343 final long token = Binder.clearCallingIdentity();
1344 try {
1345 forgetWifiDisplayInternal(address);
1346 } finally {
1347 Binder.restoreCallingIdentity(token);
1348 }
1349 }
1350
1351 @Override // Binder call
1352 public void pauseWifiDisplay() {
1353 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1354 "Permission required to pause a wifi display session");
1355
1356 final long token = Binder.clearCallingIdentity();
1357 try {
1358 pauseWifiDisplayInternal();
1359 } finally {
1360 Binder.restoreCallingIdentity(token);
1361 }
1362 }
1363
1364 @Override // Binder call
1365 public void resumeWifiDisplay() {
1366 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1367 "Permission required to resume a wifi display session");
1368
1369 final long token = Binder.clearCallingIdentity();
1370 try {
1371 resumeWifiDisplayInternal();
1372 } finally {
1373 Binder.restoreCallingIdentity(token);
1374 }
1375 }
1376
1377 @Override // Binder call
1378 public WifiDisplayStatus getWifiDisplayStatus() {
1379 // This request does not require special permissions.
1380 // Any app can get information about available wifi displays.
1381
1382 final long token = Binder.clearCallingIdentity();
1383 try {
1384 return getWifiDisplayStatusInternal();
1385 } finally {
1386 Binder.restoreCallingIdentity(token);
1387 }
1388 }
1389
1390 @Override // Binder call
Michael Wright1c9977b2016-07-12 13:30:10 -07001391 public void requestColorMode(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +01001392 mContext.enforceCallingOrSelfPermission(
Michael Wright1c9977b2016-07-12 13:30:10 -07001393 Manifest.permission.CONFIGURE_DISPLAY_COLOR_MODE,
1394 "Permission required to change the display color mode");
Michael Wright58e829f2015-09-15 00:13:26 +01001395 final long token = Binder.clearCallingIdentity();
1396 try {
Michael Wright1c9977b2016-07-12 13:30:10 -07001397 requestColorModeInternal(displayId, colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +01001398 } finally {
1399 Binder.restoreCallingIdentity(token);
1400 }
1401 }
1402
1403 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001404 public int createVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wrightc39d47a2014-07-08 18:07:36 -07001405 IMediaProjection projection, String packageName, String name,
1406 int width, int height, int densityDpi, Surface surface, int flags) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001407 final int callingUid = Binder.getCallingUid();
1408 if (!validatePackageName(callingUid, packageName)) {
1409 throw new SecurityException("packageName must match the calling uid");
1410 }
Michael Wright75ee9fc2014-09-01 19:55:22 -07001411 if (callback == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001412 throw new IllegalArgumentException("appToken must not be null");
1413 }
1414 if (TextUtils.isEmpty(name)) {
1415 throw new IllegalArgumentException("name must be non-null and non-empty");
1416 }
1417 if (width <= 0 || height <= 0 || densityDpi <= 0) {
1418 throw new IllegalArgumentException("width, height, and densityDpi must be "
1419 + "greater than 0");
1420 }
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001421 if (surface != null && surface.isSingleBuffered()) {
Pablo Ceballosaff2f942016-07-29 14:49:55 -07001422 throw new IllegalArgumentException("Surface can't be single-buffered");
1423 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001424
Michael Wright6720be42014-07-29 19:14:16 -07001425 if ((flags & DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC) != 0) {
1426 flags |= DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
1427 }
1428 if ((flags & DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY) != 0) {
1429 flags &= ~DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
1430 }
1431
Michael Wrightc39d47a2014-07-08 18:07:36 -07001432 if (projection != null) {
1433 try {
1434 if (!getProjectionService().isValidMediaProjection(projection)) {
1435 throw new SecurityException("Invalid media projection");
1436 }
Michael Wright6720be42014-07-29 19:14:16 -07001437 flags = projection.applyVirtualDisplayFlags(flags);
Michael Wrightc39d47a2014-07-08 18:07:36 -07001438 } catch (RemoteException e) {
Michael Wright6720be42014-07-29 19:14:16 -07001439 throw new SecurityException("unable to validate media projection or flags");
Michael Wrightc39d47a2014-07-08 18:07:36 -07001440 }
1441 }
1442
Michael Wright6720be42014-07-29 19:14:16 -07001443 if (callingUid != Process.SYSTEM_UID &&
1444 (flags & DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001445 if (!canProjectVideo(projection)) {
1446 throw new SecurityException("Requires CAPTURE_VIDEO_OUTPUT or "
1447 + "CAPTURE_SECURE_VIDEO_OUTPUT permission, or an appropriate "
1448 + "MediaProjection token in order to create a screen sharing virtual "
1449 + "display.");
1450 }
1451 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001452 if ((flags & DisplayManager.VIRTUAL_DISPLAY_FLAG_SECURE) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001453 if (!canProjectSecureVideo(projection)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001454 throw new SecurityException("Requires CAPTURE_SECURE_VIDEO_OUTPUT "
Michael Wrightc39d47a2014-07-08 18:07:36 -07001455 + "or an appropriate MediaProjection token to create a "
1456 + "secure virtual display.");
Jeff Brown4ccb8232014-01-16 22:16:42 -08001457 }
1458 }
1459
1460 final long token = Binder.clearCallingIdentity();
1461 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001462 return createVirtualDisplayInternal(callback, projection, callingUid,
Michael Wrightc39d47a2014-07-08 18:07:36 -07001463 packageName, name, width, height, densityDpi, surface, flags);
Jeff Brown4ccb8232014-01-16 22:16:42 -08001464 } finally {
1465 Binder.restoreCallingIdentity(token);
1466 }
1467 }
1468
1469 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001470 public void resizeVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wright01e840f2014-06-26 16:03:25 -07001471 int width, int height, int densityDpi) {
1472 final long token = Binder.clearCallingIdentity();
1473 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001474 resizeVirtualDisplayInternal(callback.asBinder(), width, height, densityDpi);
Michael Wright01e840f2014-06-26 16:03:25 -07001475 } finally {
1476 Binder.restoreCallingIdentity(token);
1477 }
1478 }
1479
1480 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001481 public void setVirtualDisplaySurface(IVirtualDisplayCallback callback, Surface surface) {
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001482 if (surface != null && surface.isSingleBuffered()) {
1483 throw new IllegalArgumentException("Surface can't be single-buffered");
1484 }
Jeff Brown92207df2014-04-16 13:16:07 -07001485 final long token = Binder.clearCallingIdentity();
1486 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001487 setVirtualDisplaySurfaceInternal(callback.asBinder(), surface);
Jeff Brown92207df2014-04-16 13:16:07 -07001488 } finally {
1489 Binder.restoreCallingIdentity(token);
1490 }
1491 }
1492
1493 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001494 public void releaseVirtualDisplay(IVirtualDisplayCallback callback) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001495 final long token = Binder.clearCallingIdentity();
1496 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001497 releaseVirtualDisplayInternal(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -08001498 } finally {
1499 Binder.restoreCallingIdentity(token);
1500 }
1501 }
1502
1503 @Override // Binder call
1504 public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) {
1505 if (mContext == null
1506 || mContext.checkCallingOrSelfPermission(Manifest.permission.DUMP)
1507 != PackageManager.PERMISSION_GRANTED) {
1508 pw.println("Permission Denial: can't dump DisplayManager from from pid="
1509 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
1510 return;
1511 }
1512
1513 final long token = Binder.clearCallingIdentity();
1514 try {
1515 dumpInternal(pw);
1516 } finally {
1517 Binder.restoreCallingIdentity(token);
1518 }
1519 }
1520
1521 private boolean validatePackageName(int uid, String packageName) {
1522 if (packageName != null) {
1523 String[] packageNames = mContext.getPackageManager().getPackagesForUid(uid);
1524 if (packageNames != null) {
1525 for (String n : packageNames) {
1526 if (n.equals(packageName)) {
1527 return true;
1528 }
1529 }
1530 }
1531 }
1532 return false;
1533 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001534
1535 private boolean canProjectVideo(IMediaProjection projection) {
1536 if (projection != null) {
1537 try {
1538 if (projection.canProjectVideo()) {
1539 return true;
1540 }
1541 } catch (RemoteException e) {
1542 Slog.e(TAG, "Unable to query projection service for permissions", e);
1543 }
1544 }
1545 if (mContext.checkCallingPermission(
1546 android.Manifest.permission.CAPTURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07001547 == PackageManager.PERMISSION_GRANTED) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001548 return true;
1549 }
1550 return canProjectSecureVideo(projection);
1551 }
1552
1553 private boolean canProjectSecureVideo(IMediaProjection projection) {
1554 if (projection != null) {
1555 try {
1556 if (projection.canProjectSecureVideo()){
1557 return true;
1558 }
1559 } catch (RemoteException e) {
1560 Slog.e(TAG, "Unable to query projection service for permissions", e);
1561 }
1562 }
1563 return mContext.checkCallingPermission(
1564 android.Manifest.permission.CAPTURE_SECURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07001565 == PackageManager.PERMISSION_GRANTED;
Michael Wrightc39d47a2014-07-08 18:07:36 -07001566 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001567 }
1568
1569 private final class LocalService extends DisplayManagerInternal {
1570 @Override
Jeff Brown037c33e2014-04-09 00:31:55 -07001571 public void initPowerManagement(final DisplayPowerCallbacks callbacks, Handler handler,
Jeff Brownad9ef192014-04-08 17:26:30 -07001572 SensorManager sensorManager) {
1573 synchronized (mSyncRoot) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001574 DisplayBlanker blanker = new DisplayBlanker() {
1575 @Override
Jeff Brown5d6443b2015-04-10 20:15:01 -07001576 public void requestDisplayState(int state, int brightness) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001577 // The order of operations is important for legacy reasons.
1578 if (state == Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07001579 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07001580 }
1581
1582 callbacks.onDisplayStateChange(state);
1583
1584 if (state != Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07001585 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07001586 }
1587 }
1588 };
Jeff Brownad9ef192014-04-08 17:26:30 -07001589 mDisplayPowerController = new DisplayPowerController(
Jeff Brown037c33e2014-04-09 00:31:55 -07001590 mContext, callbacks, handler, sensorManager, blanker);
Jeff Brownad9ef192014-04-08 17:26:30 -07001591 }
1592 }
1593
1594 @Override
1595 public boolean requestPowerState(DisplayPowerRequest request,
1596 boolean waitForNegativeProximity) {
1597 return mDisplayPowerController.requestPowerState(request,
1598 waitForNegativeProximity);
1599 }
1600
1601 @Override
1602 public boolean isProximitySensorAvailable() {
1603 return mDisplayPowerController.isProximitySensorAvailable();
1604 }
1605
1606 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08001607 public DisplayInfo getDisplayInfo(int displayId) {
1608 return getDisplayInfoInternal(displayId, Process.myUid());
1609 }
1610
1611 @Override
1612 public void registerDisplayTransactionListener(DisplayTransactionListener listener) {
1613 if (listener == null) {
1614 throw new IllegalArgumentException("listener must not be null");
1615 }
1616
1617 registerDisplayTransactionListenerInternal(listener);
1618 }
1619
1620 @Override
1621 public void unregisterDisplayTransactionListener(DisplayTransactionListener listener) {
1622 if (listener == null) {
1623 throw new IllegalArgumentException("listener must not be null");
1624 }
1625
1626 unregisterDisplayTransactionListenerInternal(listener);
1627 }
1628
1629 @Override
1630 public void setDisplayInfoOverrideFromWindowManager(int displayId, DisplayInfo info) {
1631 setDisplayInfoOverrideFromWindowManagerInternal(displayId, info);
1632 }
1633
1634 @Override
1635 public void performTraversalInTransactionFromWindowManager() {
1636 performTraversalInTransactionFromWindowManagerInternal();
1637 }
1638
1639 @Override
Michael Wright3f145a22014-07-22 19:46:03 -07001640 public void setDisplayProperties(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001641 float requestedRefreshRate, int requestedMode, boolean inTraversal) {
1642 setDisplayPropertiesInternal(displayId, hasContent, requestedRefreshRate,
1643 requestedMode, inTraversal);
Jeff Brown4ccb8232014-01-16 22:16:42 -08001644 }
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08001645
1646 @Override
1647 public void setDisplayOffsets(int displayId, int x, int y) {
1648 setDisplayOffsetsInternal(displayId, x, y);
1649 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001650 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07001651}