blob: a23205124f74f3cb04df8110b70407a470a50dc1 [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
Chilun67a379b2019-04-11 19:49:42 +080019import static android.Manifest.permission.CAPTURE_SECURE_VIDEO_OUTPUT;
20import static android.Manifest.permission.CAPTURE_VIDEO_OUTPUT;
21import static android.Manifest.permission.INTERNAL_SYSTEM_WINDOW;
Andrii Kulianfc8f82b2017-01-26 13:17:27 -080022import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
Chilun67a379b2019-04-11 19:49:42 +080023import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD;
Andrii Kulianfc8f82b2017-01-26 13:17:27 -080024import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY;
25import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;
26import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_SECURE;
Chilun67a379b2019-04-11 19:49:42 +080027import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +010028import static android.hardware.display.DisplayViewport.VIEWPORT_EXTERNAL;
29import static android.hardware.display.DisplayViewport.VIEWPORT_INTERNAL;
30import static android.hardware.display.DisplayViewport.VIEWPORT_VIRTUAL;
Jeff Brownbd6e1502012-08-28 03:27:37 -070031
Jeff Brownfa25bf52012-07-23 19:26:30 -070032import android.Manifest;
Santos Cordonee8931e2017-04-05 10:31:15 -070033import android.annotation.NonNull;
Kenny Guy05ce8092018-01-17 13:44:20 +000034import android.annotation.Nullable;
Michael Wrighteef0e132017-11-21 17:57:52 +000035import android.annotation.UserIdInt;
Kenny Guy29aa30e2017-11-30 13:43:46 +000036import android.app.AppOpsManager;
Jeff Brownfa25bf52012-07-23 19:26:30 -070037import android.content.Context;
38import android.content.pm.PackageManager;
Kenny Guy22bd0442017-10-26 00:15:54 +010039import android.content.pm.ParceledListSlice;
Michael Wrighteedcbf12017-08-16 23:14:54 +010040import android.content.res.Resources;
Dan Gittik122df862018-03-28 16:59:22 +010041import android.content.res.TypedArray;
Galia Peycheva088d7c02019-12-13 11:27:23 +010042import android.database.ContentObserver;
Peiyong Lin277eaff2019-01-16 16:18:22 -080043import android.graphics.ColorSpace;
Michael Wrighteedcbf12017-08-16 23:14:54 +010044import android.graphics.Point;
Robert Carr5c52b132019-02-15 15:48:11 -080045import android.graphics.Rect;
Jeff Brownad9ef192014-04-08 17:26:30 -070046import android.hardware.SensorManager;
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +000047import android.hardware.display.AmbientBrightnessDayStats;
Kenny Guy22bd0442017-10-26 00:15:54 +010048import android.hardware.display.BrightnessChangeEvent;
Michael Wrighteef0e132017-11-21 17:57:52 +000049import android.hardware.display.BrightnessConfiguration;
Dan Gittik122df862018-03-28 16:59:22 +010050import android.hardware.display.Curve;
Jeff Brownbd6e1502012-08-28 03:27:37 -070051import android.hardware.display.DisplayManagerGlobal;
Jeff Brown4ccb8232014-01-16 22:16:42 -080052import android.hardware.display.DisplayManagerInternal;
Jeff Brown4ccb8232014-01-16 22:16:42 -080053import android.hardware.display.DisplayManagerInternal.DisplayTransactionListener;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +010054import android.hardware.display.DisplayViewport;
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -070055import android.hardware.display.DisplayedContentSample;
56import android.hardware.display.DisplayedContentSamplingAttributes;
Jeff Brownfa25bf52012-07-23 19:26:30 -070057import android.hardware.display.IDisplayManager;
Jeff Brownbd6e1502012-08-28 03:27:37 -070058import android.hardware.display.IDisplayManagerCallback;
Michael Wright75ee9fc2014-09-01 19:55:22 -070059import android.hardware.display.IVirtualDisplayCallback;
Jeff Browne08ae382012-09-07 20:36:36 -070060import android.hardware.display.WifiDisplayStatus;
Jeff Brown4ccb8232014-01-16 22:16:42 -080061import android.hardware.input.InputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -070062import android.media.projection.IMediaProjection;
63import android.media.projection.IMediaProjectionManager;
Galia Peycheva088d7c02019-12-13 11:27:23 +010064import android.net.Uri;
Jeff Brownfa25bf52012-07-23 19:26:30 -070065import android.os.Binder;
Jeff Brownbd6e1502012-08-28 03:27:37 -070066import android.os.Handler;
Jeff Brown64a55af2012-08-26 02:47:39 -070067import android.os.IBinder;
Jeff Brown4ccb8232014-01-16 22:16:42 -080068import android.os.IBinder.DeathRecipient;
Jeff Brownbd6e1502012-08-28 03:27:37 -070069import android.os.Looper;
70import android.os.Message;
Jeff Brown5d6443b2015-04-10 20:15:01 -070071import android.os.PowerManager;
Craig Mautner4504de52013-12-20 09:06:56 -080072import android.os.Process;
Jeff Brownbd6e1502012-08-28 03:27:37 -070073import android.os.RemoteException;
Dan Gittik7a32fba2018-03-28 12:19:38 +010074import android.os.ResultReceiver;
Michael Wrightc39d47a2014-07-08 18:07:36 -070075import android.os.ServiceManager;
Dan Gittik7a32fba2018-03-28 12:19:38 +010076import android.os.ShellCallback;
Jeff Brownbd6e1502012-08-28 03:27:37 -070077import android.os.SystemClock;
Jeff Brownfa25bf52012-07-23 19:26:30 -070078import android.os.SystemProperties;
Jeff Brown5d6443b2015-04-10 20:15:01 -070079import android.os.Trace;
Kenny Guy22bd0442017-10-26 00:15:54 +010080import android.os.UserHandle;
Michael Wrighteef0e132017-11-21 17:57:52 +000081import android.os.UserManager;
Galia Peycheva088d7c02019-12-13 11:27:23 +010082import android.provider.Settings;
Jeff Browna506a6e2013-06-04 00:02:38 -070083import android.text.TextUtils;
Andrii Kulianfb1bf692017-01-17 11:17:34 -080084import android.util.IntArray;
Dan Gittik122df862018-03-28 16:59:22 +010085import android.util.Pair;
Jeff Brownbd6e1502012-08-28 03:27:37 -070086import android.util.Slog;
87import android.util.SparseArray;
Dan Gittik122df862018-03-28 16:59:22 +010088import android.util.Spline;
Jeff Brownfa25bf52012-07-23 19:26:30 -070089import android.view.Display;
90import android.view.DisplayInfo;
Jeff Browna506a6e2013-06-04 00:02:38 -070091import android.view.Surface;
Robert Carrae606b42018-02-15 15:36:23 -080092import android.view.SurfaceControl;
Jeff Browna506a6e2013-06-04 00:02:38 -070093
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +010094import com.android.internal.annotations.GuardedBy;
95import com.android.internal.annotations.VisibleForTesting;
96import com.android.internal.util.DumpUtils;
97import com.android.internal.util.IndentingPrintWriter;
Jorim Jaggied7993b2017-03-28 18:50:01 +010098import com.android.server.AnimationThread;
Jeff Brown4ccb8232014-01-16 22:16:42 -080099import com.android.server.DisplayThread;
100import com.android.server.LocalServices;
101import com.android.server.SystemService;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700102import com.android.server.UiThread;
Jorim Jaggi21c39a72017-10-20 15:47:51 +0200103import com.android.server.wm.SurfaceAnimationThread;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100104import com.android.server.wm.WindowManagerInternal;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700105
106import java.io.FileDescriptor;
107import java.io.PrintWriter;
108import java.util.ArrayList;
Jeff Browna506a6e2013-06-04 00:02:38 -0700109import java.util.Arrays;
Jeff Browne75926d2014-09-18 15:24:49 -0700110import java.util.List;
Jeff Brown7f3994e2012-12-04 14:04:28 -0800111import java.util.concurrent.CopyOnWriteArrayList;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700112
113/**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700114 * Manages attached displays.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700115 * <p>
Jeff Brownbd6e1502012-08-28 03:27:37 -0700116 * The {@link DisplayManagerService} manages the global lifecycle of displays,
117 * decides how to configure logical displays based on the physical display devices currently
118 * attached, sends notifications to the system and to applications when the state
119 * changes, and so on.
120 * </p><p>
121 * The display manager service relies on a collection of {@link DisplayAdapter} components,
122 * for discovering and configuring physical display devices attached to the system.
123 * There are separate display adapters for each manner that devices are attached:
Dominik Laskowski26290bb2020-01-15 16:09:55 -0800124 * one display adapter for physical displays, one for simulated non-functional
Jeff Brownbd6e1502012-08-28 03:27:37 -0700125 * displays when the system is headless, one for simulated overlay displays used for
126 * development, one for wifi displays, etc.
127 * </p><p>
128 * Display adapters are only weakly coupled to the display manager service.
129 * Display adapters communicate changes in display device state to the display manager
Craig Mautner722285e2012-09-07 13:55:58 -0700130 * service asynchronously via a {@link DisplayAdapter.Listener} registered
Jeff Brownbd6e1502012-08-28 03:27:37 -0700131 * by the display manager service. This separation of concerns is important for
132 * two main reasons. First, it neatly encapsulates the responsibilities of these
133 * two classes: display adapters handle individual display devices whereas
134 * the display manager service handles the global state. Second, it eliminates
135 * the potential for deadlocks resulting from asynchronous display device discovery.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700136 * </p>
137 *
138 * <h3>Synchronization</h3>
139 * <p>
140 * Because the display manager may be accessed by multiple threads, the synchronization
141 * story gets a little complicated. In particular, the window manager may call into
142 * the display manager while holding a surface transaction with the expectation that
143 * it can apply changes immediately. Unfortunately, that means we can't just do
144 * everything asynchronously (*grump*).
Jeff Brownbd6e1502012-08-28 03:27:37 -0700145 * </p><p>
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700146 * To make this work, all of the objects that belong to the display manager must
147 * use the same lock. We call this lock the synchronization root and it has a unique
148 * type {@link DisplayManagerService.SyncRoot}. Methods that require this lock are
149 * named with the "Locked" suffix.
150 * </p><p>
151 * Where things get tricky is that the display manager is not allowed to make
152 * any potentially reentrant calls, especially into the window manager. We generally
153 * avoid this by making all potentially reentrant out-calls asynchronous.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700154 * </p>
155 */
Jeff Brown4ccb8232014-01-16 22:16:42 -0800156public final class DisplayManagerService extends SystemService {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700157 private static final String TAG = "DisplayManagerService";
Jeff Brownbd6e1502012-08-28 03:27:37 -0700158 private static final boolean DEBUG = false;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700159
Jeff Brownbbd28a22012-09-20 16:47:15 -0700160 // When this system property is set to 0, WFD is forcibly disabled on boot.
161 // When this system property is set to 1, WFD is forcibly enabled on boot.
162 // Otherwise WFD is enabled according to the value of config_enableWifiDisplay.
163 private static final String FORCE_WIFI_DISPLAY_ENABLE = "persist.debug.wfd.enable";
164
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200165 private static final String PROP_DEFAULT_DISPLAY_TOP_INSET = "persist.sys.displayinset.top";
166
Jeff Brownbd6e1502012-08-28 03:27:37 -0700167 private static final long WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT = 10000;
168
Santos Cordonc22c5632017-06-21 16:03:49 -0700169 private static final int MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS = 1;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700170 private static final int MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS = 2;
171 private static final int MSG_DELIVER_DISPLAY_EVENT = 3;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700172 private static final int MSG_REQUEST_TRAVERSAL = 4;
Jeff Brownd728bf52012-09-08 18:05:28 -0700173 private static final int MSG_UPDATE_VIEWPORT = 5;
Michael Wrighta3dab232019-02-22 16:54:21 +0000174 private static final int MSG_LOAD_BRIGHTNESS_CONFIGURATION = 6;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700175
Jeff Brownb880d882014-02-10 19:47:07 -0800176 private final Context mContext;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700177 private final DisplayManagerHandler mHandler;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700178 private final Handler mUiHandler;
179 private final DisplayAdapterListener mDisplayAdapterListener;
Michael Wrighta3dab232019-02-22 16:54:21 +0000180 private final DisplayModeDirector mDisplayModeDirector;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800181 private WindowManagerInternal mWindowManagerInternal;
182 private InputManagerInternal mInputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -0700183 private IMediaProjectionManager mProjectionService;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700184
185 // The synchronization root for the display manager.
186 // This lock guards most of the display manager's state.
Craig Mautner0bf6ec92012-12-18 08:33:27 -0800187 // NOTE: This is synchronized on while holding WindowManagerService.mWindowMap so never call
188 // into WindowManagerService methods that require mWindowMap while holding this unless you are
189 // very very sure that no deadlock can occur.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700190 private final SyncRoot mSyncRoot = new SyncRoot();
191
192 // True if in safe mode.
193 // This option may disable certain display adapters.
194 public boolean mSafeMode;
195
196 // True if we are in a special boot mode where only core applications and
197 // services should be started. This option may disable certain display adapters.
198 public boolean mOnlyCore;
199
Jeff Brown27f1d672012-10-17 18:32:34 -0700200 // True if the display manager service should pretend there is only one display
201 // and only tell applications about the existence of the default logical display.
202 // The display manager can still mirror content to secondary displays but applications
203 // cannot present unique content on those displays.
204 // Used for demonstration purposes only.
205 private final boolean mSingleDisplayDemoMode;
206
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700207 // All callback records indexed by calling process id.
208 public final SparseArray<CallbackRecord> mCallbacks =
Jeff Brownbd6e1502012-08-28 03:27:37 -0700209 new SparseArray<CallbackRecord>();
Jeff Brownfa25bf52012-07-23 19:26:30 -0700210
Jeff Brownbd6e1502012-08-28 03:27:37 -0700211 // List of all currently registered display adapters.
212 private final ArrayList<DisplayAdapter> mDisplayAdapters = new ArrayList<DisplayAdapter>();
213
214 // List of all currently connected display devices.
215 private final ArrayList<DisplayDevice> mDisplayDevices = new ArrayList<DisplayDevice>();
216
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700217 // List of all logical displays indexed by logical display id.
Tim Murray890ceb52020-01-30 10:12:40 -0800218 // Any modification to mLogicalDisplays must invalidate the DisplayManagerGlobal cache.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700219 private final SparseArray<LogicalDisplay> mLogicalDisplays =
220 new SparseArray<LogicalDisplay>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700221 private int mNextNonDefaultDisplayId = Display.DEFAULT_DISPLAY + 1;
222
Jeff Brown7f3994e2012-12-04 14:04:28 -0800223 // List of all display transaction listeners.
224 private final CopyOnWriteArrayList<DisplayTransactionListener> mDisplayTransactionListeners =
225 new CopyOnWriteArrayList<DisplayTransactionListener>();
226
Jeff Brownad9ef192014-04-08 17:26:30 -0700227 // Display power controller.
228 private DisplayPowerController mDisplayPowerController;
229
Jeff Brown037c33e2014-04-09 00:31:55 -0700230 // The overall display state, independent of changes that might influence one
231 // display or another in particular.
Jeff Brown5d6443b2015-04-10 20:15:01 -0700232 private int mGlobalDisplayState = Display.STATE_ON;
233
234 // The overall display brightness.
Dominik Laskowski26290bb2020-01-15 16:09:55 -0800235 // For now, this only applies to the default display but we may split it up eventually.
Fiona Campbelld4eb2952019-11-04 17:19:56 +0000236 private float mGlobalDisplayBrightness;
Jeff Brown9e316a12012-10-08 19:17:06 -0700237
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700238 // Set to true when there are pending display changes that have yet to be applied
239 // to the surface flinger state.
240 private boolean mPendingTraversal;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700241
Jeff Browne08ae382012-09-07 20:36:36 -0700242 // The Wifi display adapter, or null if not registered.
243 private WifiDisplayAdapter mWifiDisplayAdapter;
244
Jeff Brownce468a32013-11-21 16:42:03 -0800245 // The number of active wifi display scan requests.
246 private int mWifiDisplayScanRequestCount;
247
Jeff Browna506a6e2013-06-04 00:02:38 -0700248 // The virtual display adapter, or null if not registered.
249 private VirtualDisplayAdapter mVirtualDisplayAdapter;
250
Michael Wrighteef0e132017-11-21 17:57:52 +0000251 // The User ID of the current user
252 private @UserIdInt int mCurrentUserId;
253
Michael Wrighteedcbf12017-08-16 23:14:54 +0100254 // The stable device screen height and width. These are not tied to a specific display, even
255 // the default display, because they need to be stable over the course of the device's entire
256 // life, even if the default display changes (e.g. a new monitor is plugged into a PC-like
257 // device).
258 private Point mStableDisplaySize = new Point();
259
Winson72dbe7e2019-04-29 14:55:30 -0700260 // Whether the system has finished booting or not.
261 private boolean mSystemReady;
262
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200263 // The top inset of the default display.
264 // This gets persisted so that the boot animation knows how to transition from the display's
265 // full size to the size configured by the user. Right now we only persist and animate the top
266 // inset, but theoretically we could do it for all of them.
267 private int mDefaultDisplayTopInset;
268
Jeff Brownd728bf52012-09-08 18:05:28 -0700269 // Viewports of the default display and the display that should receive touch
270 // input from an external source. Used by the input system.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100271 @GuardedBy("mSyncRoot")
272 private final ArrayList<DisplayViewport> mViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700273
Jeff Brown89d55462012-09-19 11:33:42 -0700274 // Persistent data store for all internal settings maintained by the display manager service.
275 private final PersistentDataStore mPersistentDataStore = new PersistentDataStore();
276
Jeff Brownbd6e1502012-08-28 03:27:37 -0700277 // Temporary callback list, used when sending display events to applications.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700278 // May be used outside of the lock but only on the handler thread.
279 private final ArrayList<CallbackRecord> mTempCallbacks = new ArrayList<CallbackRecord>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700280
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700281 // Temporary display info, used for comparing display configurations.
282 private final DisplayInfo mTempDisplayInfo = new DisplayInfo();
283
Jeff Brownd728bf52012-09-08 18:05:28 -0700284 // Temporary viewports, used when sending new viewport information to the
285 // input system. May be used outside of the lock but only on the handler thread.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100286 private final ArrayList<DisplayViewport> mTempViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700287
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700288 // The default color mode for default displays. Overrides the usual
289 // Display.Display.COLOR_MODE_DEFAULT for displays with the
290 // DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY flag set.
291 private final int mDefaultDisplayDefaultColorMode;
292
Jeff Browne75926d2014-09-18 15:24:49 -0700293 // Temporary list of deferred work to perform when setting the display state.
294 // Only used by requestDisplayState. The field is self-synchronized and only
295 // intended for use inside of the requestGlobalDisplayStateInternal function.
296 private final ArrayList<Runnable> mTempDisplayStateWorkQueue = new ArrayList<Runnable>();
297
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800298 // Lists of UIDs that are present on the displays. Maps displayId -> array of UIDs.
299 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
300
Santos Cordonee8931e2017-04-05 10:31:15 -0700301 private final Injector mInjector;
302
Dan Gittik122df862018-03-28 16:59:22 +0100303 // The minimum brightness curve, which guarantess that any brightness curve that dips below it
304 // is rejected by the system.
305 private final Curve mMinimumBrightnessCurve;
306 private final Spline mMinimumBrightnessSpline;
Peiyong Lin277eaff2019-01-16 16:18:22 -0800307 private final ColorSpace mWideColorSpace;
Dan Gittik122df862018-03-28 16:59:22 +0100308
Long Lingbc841b02019-07-03 16:43:15 -0700309 private SensorManager mSensorManager;
310
Galia Peycheva088d7c02019-12-13 11:27:23 +0100311 // Whether minimal post processing is allowed by the user.
312 @GuardedBy("mSyncRoot")
313 private boolean mMinimalPostProcessingAllowed;
314
315 // Receives notifications about changes to Settings.
316 private SettingsObserver mSettingsObserver;
317
Jeff Brownb880d882014-02-10 19:47:07 -0800318 public DisplayManagerService(Context context) {
Santos Cordonee8931e2017-04-05 10:31:15 -0700319 this(context, new Injector());
320 }
321
322 @VisibleForTesting
323 DisplayManagerService(Context context, Injector injector) {
Jeff Brownb880d882014-02-10 19:47:07 -0800324 super(context);
Santos Cordonee8931e2017-04-05 10:31:15 -0700325 mInjector = injector;
Jeff Brownb880d882014-02-10 19:47:07 -0800326 mContext = context;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800327 mHandler = new DisplayManagerHandler(DisplayThread.get().getLooper());
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700328 mUiHandler = UiThread.getHandler();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700329 mDisplayAdapterListener = new DisplayAdapterListener();
Michael Wrighta3dab232019-02-22 16:54:21 +0000330 mDisplayModeDirector = new DisplayModeDirector(context, mHandler);
Jeff Brown27f1d672012-10-17 18:32:34 -0700331 mSingleDisplayDemoMode = SystemProperties.getBoolean("persist.demo.singledisplay", false);
Dan Gittik122df862018-03-28 16:59:22 +0100332 Resources resources = mContext.getResources();
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700333 mDefaultDisplayDefaultColorMode = mContext.getResources().getInteger(
Michael Wrighteef0e132017-11-21 17:57:52 +0000334 com.android.internal.R.integer.config_defaultDisplayDefaultColorMode);
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200335 mDefaultDisplayTopInset = SystemProperties.getInt(PROP_DEFAULT_DISPLAY_TOP_INSET, -1);
Dan Gittik122df862018-03-28 16:59:22 +0100336 float[] lux = getFloatArray(resources.obtainTypedArray(
337 com.android.internal.R.array.config_minimumBrightnessCurveLux));
338 float[] nits = getFloatArray(resources.obtainTypedArray(
339 com.android.internal.R.array.config_minimumBrightnessCurveNits));
340 mMinimumBrightnessCurve = new Curve(lux, nits);
341 mMinimumBrightnessSpline = Spline.createSpline(lux, nits);
Jeff Brown5d6443b2015-04-10 20:15:01 -0700342
Michael Wrighta3dab232019-02-22 16:54:21 +0000343 PowerManager pm = mContext.getSystemService(PowerManager.class);
Fiona Campbelld4eb2952019-11-04 17:19:56 +0000344 mGlobalDisplayBrightness = pm.getBrightnessConstraint(
345 PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_DEFAULT);
Michael Wrighteef0e132017-11-21 17:57:52 +0000346 mCurrentUserId = UserHandle.USER_SYSTEM;
Peiyong Lin277eaff2019-01-16 16:18:22 -0800347 ColorSpace[] colorSpaces = SurfaceControl.getCompositionColorSpaces();
348 mWideColorSpace = colorSpaces[1];
Winson72dbe7e2019-04-29 14:55:30 -0700349
350 mSystemReady = false;
Joel Fernandes2d314e12017-04-04 16:32:15 -0700351 }
352
353 public void setupSchedulerPolicies() {
Jorim Jaggied7993b2017-03-28 18:50:01 +0100354 // android.display and android.anim is critical to user experience and we should make sure
Michael Wrighteef0e132017-11-21 17:57:52 +0000355 // it is not in the default foregroup groups, add it to top-app to make sure it uses all
356 // the cores and scheduling settings for top-app when it runs.
Jorim Jaggied7993b2017-03-28 18:50:01 +0100357 Process.setThreadGroupAndCpuset(DisplayThread.get().getThreadId(),
358 Process.THREAD_GROUP_TOP_APP);
359 Process.setThreadGroupAndCpuset(AnimationThread.get().getThreadId(),
360 Process.THREAD_GROUP_TOP_APP);
Jorim Jaggi21c39a72017-10-20 15:47:51 +0200361 Process.setThreadGroupAndCpuset(SurfaceAnimationThread.get().getThreadId(),
362 Process.THREAD_GROUP_TOP_APP);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700363 }
364
Jeff Brown4ccb8232014-01-16 22:16:42 -0800365 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -0800366 public void onStart() {
Michael Wright1c9977b2016-07-12 13:30:10 -0700367 // We need to pre-load the persistent data store so it's ready before the default display
368 // adapter is up so that we have it's configuration. We could load it lazily, but since
369 // we're going to have to read it in eventually we may as well do it here rather than after
Santos Cordonc22c5632017-06-21 16:03:49 -0700370 // we've waited for the display to register itself with us.
Michael Wrighta3dab232019-02-22 16:54:21 +0000371 synchronized (mSyncRoot) {
372 mPersistentDataStore.loadIfNeeded();
373 loadStableDisplayValuesLocked();
Michael Wrighteedcbf12017-08-16 23:14:54 +0100374 }
Santos Cordonc22c5632017-06-21 16:03:49 -0700375 mHandler.sendEmptyMessage(MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800376
Tim Murray890ceb52020-01-30 10:12:40 -0800377 // If there was a runtime restart then we may have stale caches left around, so we need to
378 // make sure to invalidate them upon every start.
379 DisplayManagerGlobal.invalidateLocalDisplayInfoCaches();
380
Jeff Brown4ccb8232014-01-16 22:16:42 -0800381 publishBinderService(Context.DISPLAY_SERVICE, new BinderService(),
382 true /*allowIsolated*/);
383 publishLocalService(DisplayManagerInternal.class, new LocalService());
384 }
385
386 @Override
387 public void onBootPhase(int phase) {
388 if (phase == PHASE_WAIT_FOR_DEFAULT_DISPLAY) {
389 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700390 long timeout = SystemClock.uptimeMillis()
391 + mInjector.getDefaultDisplayDelayTimeout();
392 while (mLogicalDisplays.get(Display.DEFAULT_DISPLAY) == null ||
393 mVirtualDisplayAdapter == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800394 long delay = timeout - SystemClock.uptimeMillis();
395 if (delay <= 0) {
396 throw new RuntimeException("Timeout waiting for default display "
Santos Cordonc22c5632017-06-21 16:03:49 -0700397 + "to be initialized. DefaultDisplay="
398 + mLogicalDisplays.get(Display.DEFAULT_DISPLAY)
399 + ", mVirtualDisplayAdapter=" + mVirtualDisplayAdapter);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800400 }
401 if (DEBUG) {
402 Slog.d(TAG, "waitForDefaultDisplay: waiting, timeout=" + delay);
403 }
404 try {
405 mSyncRoot.wait(delay);
406 } catch (InterruptedException ex) {
407 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700408 }
409 }
410 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700411 }
412
Michael Wrighteef0e132017-11-21 17:57:52 +0000413 @Override
414 public void onSwitchUser(@UserIdInt int newUserId) {
415 final int userSerial = getUserManager().getUserSerialNumber(newUserId);
416 synchronized (mSyncRoot) {
417 if (mCurrentUserId != newUserId) {
418 mCurrentUserId = newUserId;
419 BrightnessConfiguration config =
420 mPersistentDataStore.getBrightnessConfiguration(userSerial);
421 mDisplayPowerController.setBrightnessConfiguration(config);
Galia Peycheva088d7c02019-12-13 11:27:23 +0100422 handleSettingsChange();
Michael Wrighteef0e132017-11-21 17:57:52 +0000423 }
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +0000424 mDisplayPowerController.onSwitchUser(newUserId);
Michael Wrighteef0e132017-11-21 17:57:52 +0000425 }
426 }
427
Jeff Brown4ccb8232014-01-16 22:16:42 -0800428 // TODO: Use dependencies or a boot phase
429 public void windowManagerAndInputReady() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700430 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800431 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
432 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner65d11b32012-10-01 13:59:52 -0700433 scheduleTraversalLocked(false);
Jeff Brownd728bf52012-09-08 18:05:28 -0700434 }
435 }
436
437 /**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700438 * Called when the system is ready to go.
439 */
440 public void systemReady(boolean safeMode, boolean onlyCore) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700441 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700442 mSafeMode = safeMode;
443 mOnlyCore = onlyCore;
Winson72dbe7e2019-04-29 14:55:30 -0700444 mSystemReady = true;
445 // Just in case the top inset changed before the system was ready. At this point, any
446 // relevant configuration should be in place.
447 recordTopInsetLocked(mLogicalDisplays.get(Display.DEFAULT_DISPLAY));
Galia Peycheva088d7c02019-12-13 11:27:23 +0100448
449 updateSettingsLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700450 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700451
Ana Kruleca74a8642019-11-14 00:51:00 +0100452 mDisplayModeDirector.setDesiredDisplayModeSpecsListener(
453 new DesiredDisplayModeSpecsObserver());
Long Lingbc841b02019-07-03 16:43:15 -0700454 mDisplayModeDirector.start(mSensorManager);
Michael Wrighta3dab232019-02-22 16:54:21 +0000455
Jeff Brownbd6e1502012-08-28 03:27:37 -0700456 mHandler.sendEmptyMessage(MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS);
Galia Peycheva088d7c02019-12-13 11:27:23 +0100457
458 mSettingsObserver = new SettingsObserver();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700459 }
460
Santos Cordonee8931e2017-04-05 10:31:15 -0700461 @VisibleForTesting
462 Handler getDisplayHandler() {
463 return mHandler;
464 }
465
Michael Wrighteedcbf12017-08-16 23:14:54 +0100466 private void loadStableDisplayValuesLocked() {
467 final Point size = mPersistentDataStore.getStableDisplaySize();
468 if (size.x > 0 && size.y > 0) {
469 // Just set these values directly so we don't write the display persistent data again
470 // unnecessarily
471 mStableDisplaySize.set(size.x, size.y);
472 } else {
473 final Resources res = mContext.getResources();
474 final int width = res.getInteger(
475 com.android.internal.R.integer.config_stableDeviceDisplayWidth);
476 final int height = res.getInteger(
477 com.android.internal.R.integer.config_stableDeviceDisplayHeight);
478 if (width > 0 && height > 0) {
479 setStableDisplaySizeLocked(width, height);
480 }
481 }
482 }
483
484 private Point getStableDisplaySizeInternal() {
485 Point r = new Point();
486 synchronized (mSyncRoot) {
487 if (mStableDisplaySize.x > 0 && mStableDisplaySize.y > 0) {
488 r.set(mStableDisplaySize.x, mStableDisplaySize.y);
489 }
490 }
491 return r;
492 }
493
Jeff Brown4ccb8232014-01-16 22:16:42 -0800494 private void registerDisplayTransactionListenerInternal(
495 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800496 // List is self-synchronized copy-on-write.
497 mDisplayTransactionListeners.add(listener);
498 }
499
Jeff Brown4ccb8232014-01-16 22:16:42 -0800500 private void unregisterDisplayTransactionListenerInternal(
501 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800502 // List is self-synchronized copy-on-write.
503 mDisplayTransactionListeners.remove(listener);
504 }
505
Jeff Brown4ccb8232014-01-16 22:16:42 -0800506 private void setDisplayInfoOverrideFromWindowManagerInternal(
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700507 int displayId, DisplayInfo info) {
508 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700509 LogicalDisplay display = mLogicalDisplays.get(displayId);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700510 if (display != null) {
Jeff Brownef981a42013-08-07 14:13:37 -0700511 if (display.setDisplayInfoOverrideFromWindowManagerLocked(info)) {
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200512 handleLogicalDisplayChanged(displayId, display);
Craig Mautner65d11b32012-10-01 13:59:52 -0700513 scheduleTraversalLocked(false);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700514 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700515 }
516 }
517 }
518
Andrii Kuliancd097992017-03-23 18:31:59 -0700519 /**
520 * @see DisplayManagerInternal#getNonOverrideDisplayInfo(int, DisplayInfo)
521 */
522 private void getNonOverrideDisplayInfoInternal(int displayId, DisplayInfo outInfo) {
523 synchronized (mSyncRoot) {
524 final LogicalDisplay display = mLogicalDisplays.get(displayId);
525 if (display != null) {
526 display.getNonOverrideDisplayInfoLocked(outInfo);
527 }
528 }
529 }
530
Santos Cordonee8931e2017-04-05 10:31:15 -0700531 @VisibleForTesting
Robert Carrae606b42018-02-15 15:36:23 -0800532 void performTraversalInternal(SurfaceControl.Transaction t) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700533 synchronized (mSyncRoot) {
534 if (!mPendingTraversal) {
535 return;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700536 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700537 mPendingTraversal = false;
538
Robert Carrae606b42018-02-15 15:36:23 -0800539 performTraversalLocked(t);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700540 }
Jeff Brown7f3994e2012-12-04 14:04:28 -0800541
542 // List is self-synchronized copy-on-write.
543 for (DisplayTransactionListener listener : mDisplayTransactionListeners) {
Vishnu Naire86bd982018-11-28 13:23:17 -0800544 listener.onDisplayTransaction(t);
Jeff Brown7f3994e2012-12-04 14:04:28 -0800545 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700546 }
547
Fiona Campbelld4eb2952019-11-04 17:19:56 +0000548 private void requestGlobalDisplayStateInternal(int state, float brightnessState) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700549 if (state == Display.STATE_UNKNOWN) {
550 state = Display.STATE_ON;
551 }
552 if (state == Display.STATE_OFF) {
Fiona Campbelld4eb2952019-11-04 17:19:56 +0000553 brightnessState = PowerManager.BRIGHTNESS_OFF_FLOAT;
554 } else if (brightnessState < PowerManager.BRIGHTNESS_MIN || Float.isNaN(brightnessState)) {
555 brightnessState = PowerManager.BRIGHTNESS_INVALID_FLOAT;
556 } else if (brightnessState > PowerManager.BRIGHTNESS_MAX) {
557 brightnessState = PowerManager.BRIGHTNESS_MAX;
Jeff Brown5d6443b2015-04-10 20:15:01 -0700558 }
559
Jeff Browne75926d2014-09-18 15:24:49 -0700560 synchronized (mTempDisplayStateWorkQueue) {
561 try {
562 // Update the display state within the lock.
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700563 // Note that we do not need to schedule traversals here although it
564 // may happen as a side-effect of displays changing state.
Jeff Browne75926d2014-09-18 15:24:49 -0700565 synchronized (mSyncRoot) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700566 if (mGlobalDisplayState == state
Fiona Campbelld4eb2952019-11-04 17:19:56 +0000567 && mGlobalDisplayBrightness == brightnessState) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700568 return; // no change
Jeff Browne75926d2014-09-18 15:24:49 -0700569 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700570
571 Trace.traceBegin(Trace.TRACE_TAG_POWER, "requestGlobalDisplayState("
572 + Display.stateToString(state)
Fiona Campbelld4eb2952019-11-04 17:19:56 +0000573 + ", brightness=" + brightnessState + ")");
Jeff Brown5d6443b2015-04-10 20:15:01 -0700574 mGlobalDisplayState = state;
Fiona Campbelld4eb2952019-11-04 17:19:56 +0000575 mGlobalDisplayBrightness = brightnessState;
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700576 applyGlobalDisplayStateLocked(mTempDisplayStateWorkQueue);
Jeff Browne75926d2014-09-18 15:24:49 -0700577 }
578
579 // Setting the display power state can take hundreds of milliseconds
580 // to complete so we defer the most expensive part of the work until
581 // after we have exited the critical section to avoid blocking other
582 // threads for a long time.
583 for (int i = 0; i < mTempDisplayStateWorkQueue.size(); i++) {
584 mTempDisplayStateWorkQueue.get(i).run();
585 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700586 Trace.traceEnd(Trace.TRACE_TAG_POWER);
Jeff Browne75926d2014-09-18 15:24:49 -0700587 } finally {
588 mTempDisplayStateWorkQueue.clear();
Jeff Brown9e316a12012-10-08 19:17:06 -0700589 }
590 }
591 }
592
Galia Peycheva088d7c02019-12-13 11:27:23 +0100593 private class SettingsObserver extends ContentObserver {
594 SettingsObserver() {
595 super(mHandler);
596
597 mContext.getContentResolver().registerContentObserver(
598 Settings.Secure.getUriFor(
599 Settings.Secure.MINIMAL_POST_PROCESSING_ALLOWED), false, this);
600 }
601
602 @Override
603 public void onChange(boolean selfChange, Uri uri) {
604 handleSettingsChange();
605 }
606 }
607
608 private void handleSettingsChange() {
609 synchronized (mSyncRoot) {
610 updateSettingsLocked();
611 scheduleTraversalLocked(false);
612 }
613 }
614
615 private void updateSettingsLocked() {
616 mMinimalPostProcessingAllowed = Settings.Secure.getIntForUser(mContext.getContentResolver(),
617 Settings.Secure.MINIMAL_POST_PROCESSING_ALLOWED, 1, UserHandle.USER_CURRENT) != 0;
618 }
619
Jeff Brown4ccb8232014-01-16 22:16:42 -0800620 private DisplayInfo getDisplayInfoInternal(int displayId, int callingUid) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700621 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800622 LogicalDisplay display = mLogicalDisplays.get(displayId);
623 if (display != null) {
624 DisplayInfo info = display.getDisplayInfoLocked();
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800625 if (info.hasAccess(callingUid)
626 || isUidPresentOnDisplayInternal(callingUid, displayId)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800627 return info;
628 }
629 }
630 return null;
631 }
632 }
633
634 private int[] getDisplayIdsInternal(int callingUid) {
635 synchronized (mSyncRoot) {
636 final int count = mLogicalDisplays.size();
637 int[] displayIds = new int[count];
638 int n = 0;
639 for (int i = 0; i < count; i++) {
640 LogicalDisplay display = mLogicalDisplays.valueAt(i);
641 DisplayInfo info = display.getDisplayInfoLocked();
642 if (info.hasAccess(callingUid)) {
643 displayIds[n++] = mLogicalDisplays.keyAt(i);
644 }
645 }
646 if (n != count) {
647 displayIds = Arrays.copyOfRange(displayIds, 0, n);
648 }
649 return displayIds;
650 }
651 }
652
653 private void registerCallbackInternal(IDisplayManagerCallback callback, int callingPid) {
654 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700655 if (mCallbacks.get(callingPid) != null) {
656 throw new SecurityException("The calling process has already "
657 + "registered an IDisplayManagerCallback.");
Jeff Brown64a55af2012-08-26 02:47:39 -0700658 }
659
Jeff Brownbd6e1502012-08-28 03:27:37 -0700660 CallbackRecord record = new CallbackRecord(callingPid, callback);
661 try {
662 IBinder binder = callback.asBinder();
663 binder.linkToDeath(record, 0);
664 } catch (RemoteException ex) {
665 // give up
666 throw new RuntimeException(ex);
667 }
668
669 mCallbacks.put(callingPid, record);
670 }
671 }
672
Jeff Brownce468a32013-11-21 16:42:03 -0800673 private void onCallbackDied(CallbackRecord record) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700674 synchronized (mSyncRoot) {
Jeff Brownce468a32013-11-21 16:42:03 -0800675 mCallbacks.remove(record.mPid);
676 stopWifiDisplayScanLocked(record);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700677 }
678 }
679
Jeff Brown4ccb8232014-01-16 22:16:42 -0800680 private void startWifiDisplayScanInternal(int callingPid) {
681 synchronized (mSyncRoot) {
682 CallbackRecord record = mCallbacks.get(callingPid);
683 if (record == null) {
684 throw new IllegalStateException("The calling process has not "
685 + "registered an IDisplayManagerCallback.");
Jeff Browne08ae382012-09-07 20:36:36 -0700686 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800687 startWifiDisplayScanLocked(record);
Jeff Browne08ae382012-09-07 20:36:36 -0700688 }
689 }
690
Jeff Brownce468a32013-11-21 16:42:03 -0800691 private void startWifiDisplayScanLocked(CallbackRecord record) {
692 if (!record.mWifiDisplayScanRequested) {
693 record.mWifiDisplayScanRequested = true;
694 if (mWifiDisplayScanRequestCount++ == 0) {
695 if (mWifiDisplayAdapter != null) {
696 mWifiDisplayAdapter.requestStartScanLocked();
697 }
698 }
699 }
700 }
701
Jeff Brown4ccb8232014-01-16 22:16:42 -0800702 private void stopWifiDisplayScanInternal(int callingPid) {
703 synchronized (mSyncRoot) {
704 CallbackRecord record = mCallbacks.get(callingPid);
705 if (record == null) {
706 throw new IllegalStateException("The calling process has not "
707 + "registered an IDisplayManagerCallback.");
Jeff Brownce468a32013-11-21 16:42:03 -0800708 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800709 stopWifiDisplayScanLocked(record);
Jeff Brownce468a32013-11-21 16:42:03 -0800710 }
711 }
712
713 private void stopWifiDisplayScanLocked(CallbackRecord record) {
714 if (record.mWifiDisplayScanRequested) {
715 record.mWifiDisplayScanRequested = false;
716 if (--mWifiDisplayScanRequestCount == 0) {
717 if (mWifiDisplayAdapter != null) {
718 mWifiDisplayAdapter.requestStopScanLocked();
719 }
720 } else if (mWifiDisplayScanRequestCount < 0) {
Dianne Hackborn8d051722014-10-01 14:59:58 -0700721 Slog.wtf(TAG, "mWifiDisplayScanRequestCount became negative: "
Jeff Brownce468a32013-11-21 16:42:03 -0800722 + mWifiDisplayScanRequestCount);
723 mWifiDisplayScanRequestCount = 0;
724 }
725 }
726 }
727
Jeff Brown4ccb8232014-01-16 22:16:42 -0800728 private void connectWifiDisplayInternal(String address) {
729 synchronized (mSyncRoot) {
730 if (mWifiDisplayAdapter != null) {
731 mWifiDisplayAdapter.requestConnectLocked(address);
Jeff Browne08ae382012-09-07 20:36:36 -0700732 }
Jeff Browne08ae382012-09-07 20:36:36 -0700733 }
734 }
735
Jeff Brown4ccb8232014-01-16 22:16:42 -0800736 private void pauseWifiDisplayInternal() {
737 synchronized (mSyncRoot) {
738 if (mWifiDisplayAdapter != null) {
739 mWifiDisplayAdapter.requestPauseLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700740 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700741 }
742 }
743
Jeff Brown4ccb8232014-01-16 22:16:42 -0800744 private void resumeWifiDisplayInternal() {
745 synchronized (mSyncRoot) {
746 if (mWifiDisplayAdapter != null) {
747 mWifiDisplayAdapter.requestResumeLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700748 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700749 }
750 }
751
Jeff Brown4ccb8232014-01-16 22:16:42 -0800752 private void disconnectWifiDisplayInternal() {
753 synchronized (mSyncRoot) {
754 if (mWifiDisplayAdapter != null) {
755 mWifiDisplayAdapter.requestDisconnectLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700756 }
Jeff Browne08ae382012-09-07 20:36:36 -0700757 }
758 }
759
Jeff Brown4ccb8232014-01-16 22:16:42 -0800760 private void renameWifiDisplayInternal(String address, String alias) {
761 synchronized (mSyncRoot) {
762 if (mWifiDisplayAdapter != null) {
763 mWifiDisplayAdapter.requestRenameLocked(address, alias);
Jeff Brown89d55462012-09-19 11:33:42 -0700764 }
Jeff Brown89d55462012-09-19 11:33:42 -0700765 }
766 }
767
Jeff Brown4ccb8232014-01-16 22:16:42 -0800768 private void forgetWifiDisplayInternal(String address) {
769 synchronized (mSyncRoot) {
770 if (mWifiDisplayAdapter != null) {
771 mWifiDisplayAdapter.requestForgetLocked(address);
Jeff Brown89d55462012-09-19 11:33:42 -0700772 }
Jeff Brown89d55462012-09-19 11:33:42 -0700773 }
774 }
775
Jeff Brown4ccb8232014-01-16 22:16:42 -0800776 private WifiDisplayStatus getWifiDisplayStatusInternal() {
777 synchronized (mSyncRoot) {
778 if (mWifiDisplayAdapter != null) {
779 return mWifiDisplayAdapter.getWifiDisplayStatusLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700780 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800781 return new WifiDisplayStatus();
Jeff Browne08ae382012-09-07 20:36:36 -0700782 }
783 }
784
Michael Wright1c9977b2016-07-12 13:30:10 -0700785 private void requestColorModeInternal(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +0100786 synchronized (mSyncRoot) {
787 LogicalDisplay display = mLogicalDisplays.get(displayId);
788 if (display != null &&
Michael Wright1c9977b2016-07-12 13:30:10 -0700789 display.getRequestedColorModeLocked() != colorMode) {
790 display.setRequestedColorModeLocked(colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +0100791 scheduleTraversalLocked(false);
792 }
793 }
794 }
795
Michael Wright75ee9fc2014-09-01 19:55:22 -0700796 private int createVirtualDisplayInternal(IVirtualDisplayCallback callback,
Santos Cordonee8931e2017-04-05 10:31:15 -0700797 IMediaProjection projection, int callingUid, String packageName, String name, int width,
798 int height, int densityDpi, Surface surface, int flags, String uniqueId) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800799 synchronized (mSyncRoot) {
800 if (mVirtualDisplayAdapter == null) {
801 Slog.w(TAG, "Rejecting request to create private virtual display "
802 + "because the virtual display adapter is not available.");
803 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700804 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800805
806 DisplayDevice device = mVirtualDisplayAdapter.createVirtualDisplayLocked(
Santos Cordonee8931e2017-04-05 10:31:15 -0700807 callback, projection, callingUid, packageName, name, width, height, densityDpi,
808 surface, flags, uniqueId);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800809 if (device == null) {
810 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700811 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700812
Jeff Brown4ccb8232014-01-16 22:16:42 -0800813 handleDisplayDeviceAddedLocked(device);
814 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
815 if (display != null) {
816 return display.getDisplayIdLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700817 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800818
819 // Something weird happened and the logical display was not created.
820 Slog.w(TAG, "Rejecting request to create virtual display "
821 + "because the logical display was not created.");
Michael Wright75ee9fc2014-09-01 19:55:22 -0700822 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800823 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700824 }
825 return -1;
826 }
827
Michael Wright01e840f2014-06-26 16:03:25 -0700828 private void resizeVirtualDisplayInternal(IBinder appToken,
829 int width, int height, int densityDpi) {
830 synchronized (mSyncRoot) {
831 if (mVirtualDisplayAdapter == null) {
832 return;
833 }
834
835 mVirtualDisplayAdapter.resizeVirtualDisplayLocked(appToken, width, height, densityDpi);
836 }
837 }
838
Jeff Brown92207df2014-04-16 13:16:07 -0700839 private void setVirtualDisplaySurfaceInternal(IBinder appToken, Surface surface) {
840 synchronized (mSyncRoot) {
841 if (mVirtualDisplayAdapter == null) {
842 return;
843 }
844
845 mVirtualDisplayAdapter.setVirtualDisplaySurfaceLocked(appToken, surface);
846 }
847 }
848
Jeff Brown4ccb8232014-01-16 22:16:42 -0800849 private void releaseVirtualDisplayInternal(IBinder appToken) {
850 synchronized (mSyncRoot) {
851 if (mVirtualDisplayAdapter == null) {
852 return;
Jeff Browna506a6e2013-06-04 00:02:38 -0700853 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700854
Jeff Brown4ccb8232014-01-16 22:16:42 -0800855 DisplayDevice device =
856 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(appToken);
857 if (device != null) {
858 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700859 }
860 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700861 }
862
chaviwda4c6942018-11-07 15:52:56 -0800863 private void setVirtualDisplayStateInternal(IBinder appToken, boolean isOn) {
864 synchronized (mSyncRoot) {
865 if (mVirtualDisplayAdapter == null) {
866 return;
867 }
868
869 mVirtualDisplayAdapter.setVirtualDisplayStateLocked(appToken, isOn);
870 }
871 }
872
Santos Cordonc22c5632017-06-21 16:03:49 -0700873 private void registerDefaultDisplayAdapters() {
874 // Register default display adapters.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700875 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700876 // main display adapter
Mike Lockwoode63f6f72013-11-15 11:01:47 -0800877 registerDisplayAdapterLocked(new LocalDisplayAdapter(
878 mSyncRoot, mContext, mHandler, mDisplayAdapterListener));
Santos Cordonc22c5632017-06-21 16:03:49 -0700879
880 // Standalone VR devices rely on a virtual display as their primary display for
881 // 2D UI. We register virtual display adapter along side the main display adapter
882 // here so that it is ready by the time the system sends the home Intent for
883 // early apps like SetupWizard/Launcher. In particular, SUW is displayed using
884 // the virtual display inside VR before any VR-specific apps even run.
885 mVirtualDisplayAdapter = mInjector.getVirtualDisplayAdapter(mSyncRoot, mContext,
886 mHandler, mDisplayAdapterListener);
887 if (mVirtualDisplayAdapter != null) {
888 registerDisplayAdapterLocked(mVirtualDisplayAdapter);
889 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700890 }
891 }
892
893 private void registerAdditionalDisplayAdapters() {
894 synchronized (mSyncRoot) {
895 if (shouldRegisterNonEssentialDisplayAdaptersLocked()) {
Jeff Brown89d55462012-09-19 11:33:42 -0700896 registerOverlayDisplayAdapterLocked();
897 registerWifiDisplayAdapterLocked();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700898 }
899 }
900 }
901
Jeff Brown89d55462012-09-19 11:33:42 -0700902 private void registerOverlayDisplayAdapterLocked() {
903 registerDisplayAdapterLocked(new OverlayDisplayAdapter(
904 mSyncRoot, mContext, mHandler, mDisplayAdapterListener, mUiHandler));
905 }
906
907 private void registerWifiDisplayAdapterLocked() {
908 if (mContext.getResources().getBoolean(
Jeff Brownbbd28a22012-09-20 16:47:15 -0700909 com.android.internal.R.bool.config_enableWifiDisplay)
910 || SystemProperties.getInt(FORCE_WIFI_DISPLAY_ENABLE, -1) == 1) {
Jeff Brown89d55462012-09-19 11:33:42 -0700911 mWifiDisplayAdapter = new WifiDisplayAdapter(
912 mSyncRoot, mContext, mHandler, mDisplayAdapterListener,
913 mPersistentDataStore);
914 registerDisplayAdapterLocked(mWifiDisplayAdapter);
915 }
916 }
917
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700918 private boolean shouldRegisterNonEssentialDisplayAdaptersLocked() {
919 // In safe mode, we disable non-essential display adapters to give the user
920 // an opportunity to fix broken settings or other problems that might affect
921 // system stability.
922 // In only-core mode, we disable non-essential display adapters to minimize
923 // the number of dependencies that are started while in this mode and to
924 // prevent problems that might occur due to the device being encrypted.
925 return !mSafeMode && !mOnlyCore;
926 }
927
928 private void registerDisplayAdapterLocked(DisplayAdapter adapter) {
929 mDisplayAdapters.add(adapter);
930 adapter.registerLocked();
931 }
932
Jeff Brownbd6e1502012-08-28 03:27:37 -0700933 private void handleDisplayDeviceAdded(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700934 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700935 handleDisplayDeviceAddedLocked(device);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700936 }
937 }
938
Jeff Browna506a6e2013-06-04 00:02:38 -0700939 private void handleDisplayDeviceAddedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700940 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700941 if (mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700942 Slog.w(TAG, "Attempted to add already added display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700943 return;
944 }
945
Jeff Brown10acf6d2015-04-14 14:20:47 -0700946 Slog.i(TAG, "Display device added: " + info);
947 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700948
949 mDisplayDevices.add(device);
Michael Wright1c9977b2016-07-12 13:30:10 -0700950 LogicalDisplay display = addLogicalDisplayLocked(device);
Jeff Brown0033a862014-10-08 12:06:39 -0700951 Runnable work = updateDisplayStateLocked(device);
952 if (work != null) {
953 work.run();
954 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700955 scheduleTraversalLocked(false);
956 }
957
Jeff Brownbd6e1502012-08-28 03:27:37 -0700958 private void handleDisplayDeviceChanged(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700959 synchronized (mSyncRoot) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700960 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700961 if (!mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700962 Slog.w(TAG, "Attempted to change non-existent display device: " + info);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700963 return;
964 }
965
Jeff Brown10acf6d2015-04-14 14:20:47 -0700966 int diff = device.mDebugLastLoggedDeviceInfo.diff(info);
967 if (diff == DisplayDeviceInfo.DIFF_STATE) {
968 Slog.i(TAG, "Display device changed state: \"" + info.name
969 + "\", " + Display.stateToString(info.state));
970 } else if (diff != 0) {
971 Slog.i(TAG, "Display device changed: " + info);
972 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700973 if ((diff & DisplayDeviceInfo.DIFF_COLOR_MODE) != 0) {
974 try {
975 mPersistentDataStore.setColorMode(device, info.colorMode);
976 } finally {
977 mPersistentDataStore.saveIfNeeded();
978 }
979 }
Jeff Brown10acf6d2015-04-14 14:20:47 -0700980 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browne87bf032012-09-20 18:30:13 -0700981
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700982 device.applyPendingDisplayDeviceInfoChangesLocked();
983 if (updateLogicalDisplaysLocked()) {
Craig Mautner65d11b32012-10-01 13:59:52 -0700984 scheduleTraversalLocked(false);
Jeff Brown64a55af2012-08-26 02:47:39 -0700985 }
986 }
987 }
988
Jeff Brownbd6e1502012-08-28 03:27:37 -0700989 private void handleDisplayDeviceRemoved(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700990 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700991 handleDisplayDeviceRemovedLocked(device);
992 }
993 }
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700994
Jeff Browna506a6e2013-06-04 00:02:38 -0700995 private void handleDisplayDeviceRemovedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700996 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700997 if (!mDisplayDevices.remove(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700998 Slog.w(TAG, "Attempted to remove non-existent display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700999 return;
1000 }
1001
Jeff Brown10acf6d2015-04-14 14:20:47 -07001002 Slog.i(TAG, "Display device removed: " + info);
1003 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -07001004
Jeff Browna506a6e2013-06-04 00:02:38 -07001005 updateLogicalDisplaysLocked();
1006 scheduleTraversalLocked(false);
1007 }
1008
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001009 private void handleLogicalDisplayChanged(int displayId, @NonNull LogicalDisplay display) {
1010 if (displayId == Display.DEFAULT_DISPLAY) {
1011 recordTopInsetLocked(display);
1012 }
Tim Murray890ceb52020-01-30 10:12:40 -08001013 // We don't bother invalidating the display info caches here because any changes to the
1014 // display info will trigger a cache invalidation inside of LogicalDisplay before we hit
1015 // this point.
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001016 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
1017 }
1018
Tim Murray890ceb52020-01-30 10:12:40 -08001019 private void handleLogicalDisplayRemoved(int displayId) {
1020 DisplayManagerGlobal.invalidateLocalDisplayInfoCaches();
1021 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_REMOVED);
1022 }
1023
Jeff Brown8e5d33e2015-06-10 13:05:50 -07001024 private void applyGlobalDisplayStateLocked(List<Runnable> workQueue) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001025 final int count = mDisplayDevices.size();
1026 for (int i = 0; i < count; i++) {
1027 DisplayDevice device = mDisplayDevices.get(i);
Jeff Browne75926d2014-09-18 15:24:49 -07001028 Runnable runnable = updateDisplayStateLocked(device);
1029 if (runnable != null) {
1030 workQueue.add(runnable);
1031 }
Jeff Browna506a6e2013-06-04 00:02:38 -07001032 }
1033 }
1034
Jeff Browne75926d2014-09-18 15:24:49 -07001035 private Runnable updateDisplayStateLocked(DisplayDevice device) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001036 // Blank or unblank the display immediately to match the state requested
Jeff Brown037c33e2014-04-09 00:31:55 -07001037 // by the display power controller (if known).
Jeff Browna506a6e2013-06-04 00:02:38 -07001038 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
1039 if ((info.flags & DisplayDeviceInfo.FLAG_NEVER_BLANK) == 0) {
Fiona Campbelld4eb2952019-11-04 17:19:56 +00001040 return device.requestDisplayStateLocked(
1041 mGlobalDisplayState, mGlobalDisplayBrightness);
Craig Mautner4f67ba62012-08-02 11:23:00 -07001042 }
Jeff Browne75926d2014-09-18 15:24:49 -07001043 return null;
Craig Mautner4f67ba62012-08-02 11:23:00 -07001044 }
1045
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001046 // Adds a new logical display based on the given display device.
1047 // Sends notifications if needed.
Michael Wright1c9977b2016-07-12 13:30:10 -07001048 private LogicalDisplay addLogicalDisplayLocked(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001049 DisplayDeviceInfo deviceInfo = device.getDisplayDeviceInfoLocked();
1050 boolean isDefault = (deviceInfo.flags
1051 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0;
1052 if (isDefault && mLogicalDisplays.get(Display.DEFAULT_DISPLAY) != null) {
1053 Slog.w(TAG, "Ignoring attempt to add a second default display: " + deviceInfo);
1054 isDefault = false;
1055 }
1056
Jeff Brown27f1d672012-10-17 18:32:34 -07001057 if (!isDefault && mSingleDisplayDemoMode) {
1058 Slog.i(TAG, "Not creating a logical display for a secondary display "
1059 + " because single display demo mode is enabled: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -07001060 return null;
Jeff Brown27f1d672012-10-17 18:32:34 -07001061 }
1062
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001063 final int displayId = assignDisplayIdLocked(isDefault);
1064 final int layerStack = assignLayerStackLocked(displayId);
1065
Jeff Brownd728bf52012-09-08 18:05:28 -07001066 LogicalDisplay display = new LogicalDisplay(displayId, layerStack, device);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001067 display.updateLocked(mDisplayDevices);
1068 if (!display.isValidLocked()) {
1069 // This should never happen currently.
1070 Slog.w(TAG, "Ignoring display device because the logical display "
1071 + "created from it was not considered valid: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -07001072 return null;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001073 }
1074
Michael Wrighteedcbf12017-08-16 23:14:54 +01001075 configureColorModeLocked(display, device);
1076 if (isDefault) {
1077 recordStableDisplayStatsIfNeededLocked(display);
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001078 recordTopInsetLocked(display);
Michael Wrighteedcbf12017-08-16 23:14:54 +01001079 }
1080
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001081 mLogicalDisplays.put(displayId, display);
Tim Murray890ceb52020-01-30 10:12:40 -08001082 DisplayManagerGlobal.invalidateLocalDisplayInfoCaches();
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001083
1084 // Wake up waitForDefaultDisplay.
1085 if (isDefault) {
1086 mSyncRoot.notifyAll();
1087 }
1088
1089 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_ADDED);
Michael Wright1c9977b2016-07-12 13:30:10 -07001090 return display;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001091 }
1092
1093 private int assignDisplayIdLocked(boolean isDefault) {
1094 return isDefault ? Display.DEFAULT_DISPLAY : mNextNonDefaultDisplayId++;
1095 }
1096
1097 private int assignLayerStackLocked(int displayId) {
1098 // Currently layer stacks and display ids are the same.
1099 // This need not be the case.
1100 return displayId;
1101 }
1102
Michael Wrighteedcbf12017-08-16 23:14:54 +01001103 private void configureColorModeLocked(LogicalDisplay display, DisplayDevice device) {
1104 if (display.getPrimaryDisplayDeviceLocked() == device) {
1105 int colorMode = mPersistentDataStore.getColorMode(device);
1106 if (colorMode == Display.COLOR_MODE_INVALID) {
1107 if ((device.getDisplayDeviceInfoLocked().flags
1108 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
1109 colorMode = mDefaultDisplayDefaultColorMode;
1110 } else {
1111 colorMode = Display.COLOR_MODE_DEFAULT;
1112 }
1113 }
1114 display.setRequestedColorModeLocked(colorMode);
1115 }
1116 }
1117
1118 // If we've never recorded stable device stats for this device before and they aren't
1119 // explicitly configured, go ahead and record the stable device stats now based on the status
1120 // of the default display at first boot.
1121 private void recordStableDisplayStatsIfNeededLocked(LogicalDisplay d) {
1122 if (mStableDisplaySize.x <= 0 && mStableDisplaySize.y <= 0) {
1123 DisplayInfo info = d.getDisplayInfoLocked();
1124 setStableDisplaySizeLocked(info.getNaturalWidth(), info.getNaturalHeight());
1125 }
1126 }
1127
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001128 private void recordTopInsetLocked(@Nullable LogicalDisplay d) {
Winson72dbe7e2019-04-29 14:55:30 -07001129 // We must only persist the inset after boot has completed, otherwise we will end up
1130 // overwriting the persisted value before the masking flag has been loaded from the
1131 // resource overlay.
1132 if (!mSystemReady || d == null) {
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001133 return;
1134 }
1135 int topInset = d.getInsets().top;
1136 if (topInset == mDefaultDisplayTopInset) {
1137 return;
1138 }
1139 mDefaultDisplayTopInset = topInset;
1140 SystemProperties.set(PROP_DEFAULT_DISPLAY_TOP_INSET, Integer.toString(topInset));
1141 }
1142
Michael Wrighteedcbf12017-08-16 23:14:54 +01001143 private void setStableDisplaySizeLocked(int width, int height) {
1144 mStableDisplaySize = new Point(width, height);
1145 try {
1146 mPersistentDataStore.setStableDisplaySize(mStableDisplaySize);
1147 } finally {
1148 mPersistentDataStore.saveIfNeeded();
1149 }
1150 }
1151
Dan Gittik122df862018-03-28 16:59:22 +01001152 @VisibleForTesting
1153 Curve getMinimumBrightnessCurveInternal() {
1154 return mMinimumBrightnessCurve;
1155 }
1156
Peiyong Lin277eaff2019-01-16 16:18:22 -08001157 int getPreferredWideGamutColorSpaceIdInternal() {
1158 return mWideColorSpace.getId();
1159 }
1160
Michael Wrighteef0e132017-11-21 17:57:52 +00001161 private void setBrightnessConfigurationForUserInternal(
Dan Gittik7a32fba2018-03-28 12:19:38 +01001162 @Nullable BrightnessConfiguration c, @UserIdInt int userId,
Kenny Guy05ce8092018-01-17 13:44:20 +00001163 @Nullable String packageName) {
Dan Gittik122df862018-03-28 16:59:22 +01001164 validateBrightnessConfiguration(c);
Michael Wrighteef0e132017-11-21 17:57:52 +00001165 final int userSerial = getUserManager().getUserSerialNumber(userId);
1166 synchronized (mSyncRoot) {
1167 try {
Kenny Guy05ce8092018-01-17 13:44:20 +00001168 mPersistentDataStore.setBrightnessConfigurationForUser(c, userSerial,
1169 packageName);
Michael Wrighteef0e132017-11-21 17:57:52 +00001170 } finally {
1171 mPersistentDataStore.saveIfNeeded();
1172 }
1173 if (userId == mCurrentUserId) {
1174 mDisplayPowerController.setBrightnessConfiguration(c);
1175 }
1176 }
1177 }
1178
Dan Gittik122df862018-03-28 16:59:22 +01001179 @VisibleForTesting
1180 void validateBrightnessConfiguration(BrightnessConfiguration config) {
1181 if (config == null) {
1182 return;
1183 }
1184 if (isBrightnessConfigurationTooDark(config)) {
1185 throw new IllegalArgumentException("brightness curve is too dark");
1186 }
1187 }
1188
1189 private boolean isBrightnessConfigurationTooDark(BrightnessConfiguration config) {
1190 Pair<float[], float[]> curve = config.getCurve();
1191 float[] lux = curve.first;
1192 float[] nits = curve.second;
1193 for (int i = 0; i < lux.length; i++) {
1194 if (nits[i] < mMinimumBrightnessSpline.interpolate(lux[i])) {
1195 return true;
1196 }
1197 }
1198 return false;
1199 }
1200
Michael Wrighteef0e132017-11-21 17:57:52 +00001201 private void loadBrightnessConfiguration() {
1202 synchronized (mSyncRoot) {
1203 final int userSerial = getUserManager().getUserSerialNumber(mCurrentUserId);
1204 BrightnessConfiguration config =
1205 mPersistentDataStore.getBrightnessConfiguration(userSerial);
1206 mDisplayPowerController.setBrightnessConfiguration(config);
1207 }
1208 }
1209
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001210 // Updates all existing logical displays given the current set of display devices.
1211 // Removes invalid logical displays.
1212 // Sends notifications if needed.
1213 private boolean updateLogicalDisplaysLocked() {
1214 boolean changed = false;
1215 for (int i = mLogicalDisplays.size(); i-- > 0; ) {
1216 final int displayId = mLogicalDisplays.keyAt(i);
1217 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1218
1219 mTempDisplayInfo.copyFrom(display.getDisplayInfoLocked());
1220 display.updateLocked(mDisplayDevices);
1221 if (!display.isValidLocked()) {
1222 mLogicalDisplays.removeAt(i);
Tim Murray890ceb52020-01-30 10:12:40 -08001223 handleLogicalDisplayRemoved(displayId);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001224 changed = true;
1225 } else if (!mTempDisplayInfo.equals(display.getDisplayInfoLocked())) {
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001226 handleLogicalDisplayChanged(displayId, display);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001227 changed = true;
1228 }
1229 }
1230 return changed;
1231 }
1232
Robert Carrae606b42018-02-15 15:36:23 -08001233 private void performTraversalLocked(SurfaceControl.Transaction t) {
Jeff Brownd728bf52012-09-08 18:05:28 -07001234 // Clear all viewports before configuring displays so that we can keep
1235 // track of which ones we have configured.
1236 clearViewportsLocked();
1237
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001238 // Configure each display device.
1239 final int count = mDisplayDevices.size();
1240 for (int i = 0; i < count; i++) {
1241 DisplayDevice device = mDisplayDevices.get(i);
Robert Carrae606b42018-02-15 15:36:23 -08001242 configureDisplayLocked(t, device);
1243 device.performTraversalLocked(t);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001244 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001245
1246 // Tell the input system about these new viewports.
Jeff Brown4ccb8232014-01-16 22:16:42 -08001247 if (mInputManagerInternal != null) {
Jeff Brownd728bf52012-09-08 18:05:28 -07001248 mHandler.sendEmptyMessage(MSG_UPDATE_VIEWPORT);
1249 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001250 }
1251
Michael Wright3f145a22014-07-22 19:46:03 -07001252 private void setDisplayPropertiesInternal(int displayId, boolean hasContent,
Galia Peycheva088d7c02019-12-13 11:27:23 +01001253 float requestedRefreshRate, int requestedModeId, boolean preferMinimalPostProcessing,
Galia Peycheva056b3ee2019-06-26 14:05:12 +02001254 boolean inTraversal) {
Craig Mautner722285e2012-09-07 13:55:58 -07001255 synchronized (mSyncRoot) {
1256 LogicalDisplay display = mLogicalDisplays.get(displayId);
Michael Wright3f145a22014-07-22 19:46:03 -07001257 if (display == null) {
1258 return;
1259 }
Galia Peycheva056b3ee2019-06-26 14:05:12 +02001260
1261 boolean shouldScheduleTraversal = false;
1262
Michael Wright3f145a22014-07-22 19:46:03 -07001263 if (display.hasContentLocked() != hasContent) {
Jeff Brown33041bd2013-08-02 21:11:14 -07001264 if (DEBUG) {
1265 Slog.d(TAG, "Display " + displayId + " hasContent flag changed: "
1266 + "hasContent=" + hasContent + ", inTraversal=" + inTraversal);
1267 }
1268
Craig Mautner722285e2012-09-07 13:55:58 -07001269 display.setHasContentLocked(hasContent);
Galia Peycheva056b3ee2019-06-26 14:05:12 +02001270 shouldScheduleTraversal = true;
Craig Mautner722285e2012-09-07 13:55:58 -07001271 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001272 if (requestedModeId == 0 && requestedRefreshRate != 0) {
1273 // Scan supported modes returned by display.getInfo() to find a mode with the same
1274 // size as the default display mode but with the specified refresh rate instead.
1275 requestedModeId = display.getDisplayInfoLocked().findDefaultModeByRefreshRate(
1276 requestedRefreshRate);
1277 }
Michael Wrighta3dab232019-02-22 16:54:21 +00001278 mDisplayModeDirector.getAppRequestObserver().setAppRequestedMode(
1279 displayId, requestedModeId);
Galia Peycheva056b3ee2019-06-26 14:05:12 +02001280
Galia Peycheva088d7c02019-12-13 11:27:23 +01001281 if (display.getDisplayInfoLocked().minimalPostProcessingSupported) {
1282 boolean mppRequest = mMinimalPostProcessingAllowed && preferMinimalPostProcessing;
Galia Peycheva056b3ee2019-06-26 14:05:12 +02001283
Galia Peycheva088d7c02019-12-13 11:27:23 +01001284 if (display.getRequestedMinimalPostProcessingLocked() != mppRequest) {
1285 display.setRequestedMinimalPostProcessingLocked(mppRequest);
1286 shouldScheduleTraversal = true;
1287 }
Galia Peycheva056b3ee2019-06-26 14:05:12 +02001288 }
1289
1290 if (shouldScheduleTraversal) {
1291 scheduleTraversalLocked(inTraversal);
1292 }
Craig Mautner722285e2012-09-07 13:55:58 -07001293 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001294 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001295
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08001296 private void setDisplayOffsetsInternal(int displayId, int x, int y) {
1297 synchronized (mSyncRoot) {
1298 LogicalDisplay display = mLogicalDisplays.get(displayId);
1299 if (display == null) {
1300 return;
1301 }
1302 if (display.getDisplayOffsetXLocked() != x
1303 || display.getDisplayOffsetYLocked() != y) {
1304 if (DEBUG) {
1305 Slog.d(TAG, "Display " + displayId + " burn-in offset set to ("
1306 + x + ", " + y + ")");
1307 }
1308 display.setDisplayOffsetsLocked(x, y);
1309 scheduleTraversalLocked(false);
1310 }
1311 }
1312 }
1313
Sam Lin4c3ac2b2019-02-18 04:50:26 -08001314 private void setDisplayScalingDisabledInternal(int displayId, boolean disable) {
1315 synchronized (mSyncRoot) {
1316 final LogicalDisplay display = mLogicalDisplays.get(displayId);
1317 if (display == null) {
1318 return;
1319 }
1320 if (display.isDisplayScalingDisabled() != disable) {
1321 if (DEBUG) {
1322 Slog.d(TAG, "Display " + displayId + " content scaling disabled = " + disable);
1323 }
1324 display.setDisplayScalingDisabledLocked(disable);
1325 scheduleTraversalLocked(false);
1326 }
1327 }
1328 }
1329
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001330 // Updates the lists of UIDs that are present on displays.
1331 private void setDisplayAccessUIDsInternal(SparseArray<IntArray> newDisplayAccessUIDs) {
1332 synchronized (mSyncRoot) {
1333 mDisplayAccessUIDs.clear();
1334 for (int i = newDisplayAccessUIDs.size() - 1; i >= 0; i--) {
1335 mDisplayAccessUIDs.append(newDisplayAccessUIDs.keyAt(i),
1336 newDisplayAccessUIDs.valueAt(i));
1337 }
1338 }
1339 }
1340
1341 // Checks if provided UID's content is present on the display and UID has access to it.
1342 private boolean isUidPresentOnDisplayInternal(int uid, int displayId) {
1343 synchronized (mSyncRoot) {
1344 final IntArray displayUIDs = mDisplayAccessUIDs.get(displayId);
1345 return displayUIDs != null && displayUIDs.indexOf(uid) != -1;
1346 }
1347 }
1348
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001349 @Nullable
1350 private IBinder getDisplayToken(int displayId) {
1351 synchronized (mSyncRoot) {
1352 final LogicalDisplay display = mLogicalDisplays.get(displayId);
1353 if (display != null) {
1354 final DisplayDevice device = display.getPrimaryDisplayDeviceLocked();
1355 if (device != null) {
1356 return device.getDisplayTokenLocked();
1357 }
1358 }
1359 }
1360
1361 return null;
1362 }
1363
Robert Carr66b5664f2019-04-02 14:18:56 -07001364 private SurfaceControl.ScreenshotGraphicBuffer screenshotInternal(int displayId) {
Vinit Nayak627ac302020-01-13 15:50:10 -08001365 synchronized (mSyncRoot) {
1366 final IBinder token = getDisplayToken(displayId);
1367 if (token == null) {
1368 return null;
1369 }
1370 final LogicalDisplay logicalDisplay = mLogicalDisplays.get(displayId);
1371 if (logicalDisplay == null) {
1372 return null;
1373 }
1374
1375 final DisplayInfo displayInfo = logicalDisplay.getDisplayInfoLocked();
1376 return SurfaceControl.screenshotToBufferWithSecureLayersUnsafe(token, new Rect(),
1377 displayInfo.getNaturalWidth(), displayInfo.getNaturalHeight(),
1378 false /* useIdentityTransform */, 0 /* rotation */);
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001379 }
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001380 }
1381
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001382 @VisibleForTesting
1383 DisplayedContentSamplingAttributes getDisplayedContentSamplingAttributesInternal(
1384 int displayId) {
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001385 final IBinder token = getDisplayToken(displayId);
1386 if (token == null) {
1387 return null;
1388 }
1389 return SurfaceControl.getDisplayedContentSamplingAttributes(token);
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001390 }
1391
1392 @VisibleForTesting
1393 boolean setDisplayedContentSamplingEnabledInternal(
1394 int displayId, boolean enable, int componentMask, int maxFrames) {
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001395 final IBinder token = getDisplayToken(displayId);
1396 if (token == null) {
1397 return false;
1398 }
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001399 return SurfaceControl.setDisplayedContentSamplingEnabled(
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001400 token, enable, componentMask, maxFrames);
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001401 }
1402
1403 @VisibleForTesting
1404 DisplayedContentSample getDisplayedContentSampleInternal(int displayId,
1405 long maxFrames, long timestamp) {
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001406 final IBinder token = getDisplayToken(displayId);
1407 if (token == null) {
1408 return null;
1409 }
1410 return SurfaceControl.getDisplayedContentSample(token, maxFrames, timestamp);
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001411 }
1412
Santos Cordonc4fd8e62019-12-18 13:01:05 +00001413 void resetBrightnessConfiguration() {
1414 setBrightnessConfigurationForUserInternal(null, mContext.getUserId(),
1415 mContext.getPackageName());
1416 }
1417
1418 void setAutoBrightnessLoggingEnabled(boolean enabled) {
1419 if (mDisplayPowerController != null) {
1420 synchronized (mSyncRoot) {
1421 mDisplayPowerController.setAutoBrightnessLoggingEnabled(enabled);
1422 }
1423 }
1424 }
1425
1426 void setDisplayWhiteBalanceLoggingEnabled(boolean enabled) {
1427 if (mDisplayPowerController != null) {
1428 synchronized (mSyncRoot) {
1429 mDisplayPowerController.setDisplayWhiteBalanceLoggingEnabled(enabled);
1430 }
1431 }
1432 }
1433
1434 void setAmbientColorTemperatureOverride(float cct) {
1435 if (mDisplayPowerController != null) {
1436 synchronized (mSyncRoot) {
1437 mDisplayPowerController.setAmbientColorTemperatureOverride(cct);
1438 }
1439 }
1440 }
1441
Ana Kruleca74a8642019-11-14 00:51:00 +01001442 private void onDesiredDisplayModeSpecsChangedInternal() {
Michael Wrighta3dab232019-02-22 16:54:21 +00001443 boolean changed = false;
1444 synchronized (mSyncRoot) {
1445 final int count = mLogicalDisplays.size();
1446 for (int i = 0; i < count; i++) {
1447 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1448 int displayId = mLogicalDisplays.keyAt(i);
Ana Kruleca74a8642019-11-14 00:51:00 +01001449 DisplayModeDirector.DesiredDisplayModeSpecs desiredDisplayModeSpecs =
1450 mDisplayModeDirector.getDesiredDisplayModeSpecs(displayId);
1451 DisplayModeDirector.DesiredDisplayModeSpecs existingDesiredDisplayModeSpecs =
1452 display.getDesiredDisplayModeSpecsLocked();
1453 if (DEBUG) {
1454 Slog.i(TAG,
1455 "Comparing display specs: " + desiredDisplayModeSpecs
1456 + ", existing: " + existingDesiredDisplayModeSpecs);
1457 }
1458 if (!desiredDisplayModeSpecs.equals(existingDesiredDisplayModeSpecs)) {
1459 display.setDesiredDisplayModeSpecsLocked(desiredDisplayModeSpecs);
Michael Wrighta3dab232019-02-22 16:54:21 +00001460 changed = true;
1461 }
1462 }
1463 if (changed) {
1464 scheduleTraversalLocked(false);
1465 }
1466 }
1467 }
1468
Jeff Brownd728bf52012-09-08 18:05:28 -07001469 private void clearViewportsLocked() {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001470 mViewports.clear();
Craig Mautner722285e2012-09-07 13:55:58 -07001471 }
1472
Robert Carrae606b42018-02-15 15:36:23 -08001473 private void configureDisplayLocked(SurfaceControl.Transaction t, DisplayDevice device) {
Jeff Brownd14c8c92014-01-07 18:13:09 -08001474 final DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
1475 final boolean ownContent = (info.flags & DisplayDeviceInfo.FLAG_OWN_CONTENT_ONLY) != 0;
Jeff Browna506a6e2013-06-04 00:02:38 -07001476
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001477 // Find the logical display that the display device is showing.
Jeff Brownd14c8c92014-01-07 18:13:09 -08001478 // Certain displays only ever show their own content.
Craig Mautner722285e2012-09-07 13:55:58 -07001479 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
Jeff Brownd14c8c92014-01-07 18:13:09 -08001480 if (!ownContent) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001481 if (display != null && !display.hasContentLocked()) {
1482 // If the display does not have any content of its own, then
1483 // automatically mirror the default logical display contents.
1484 display = null;
1485 }
1486 if (display == null) {
1487 display = mLogicalDisplays.get(Display.DEFAULT_DISPLAY);
1488 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001489 }
1490
1491 // Apply the logical display configuration to the display device.
1492 if (display == null) {
1493 // TODO: no logical display for the device, blank it
Jeff Brownd728bf52012-09-08 18:05:28 -07001494 Slog.w(TAG, "Missing logical display to use for physical display device: "
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001495 + device.getDisplayDeviceInfoLocked());
Jeff Brownd728bf52012-09-08 18:05:28 -07001496 return;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001497 }
Robert Carrae606b42018-02-15 15:36:23 -08001498 display.configureDisplayLocked(t, device, info.state == Display.STATE_OFF);
Arthur Hung41e81e72018-10-31 18:04:56 +08001499 final int viewportType;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001500 // Update the corresponding viewport.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001501 if ((info.flags & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
Arthur Hung41e81e72018-10-31 18:04:56 +08001502 viewportType = VIEWPORT_INTERNAL;
1503 } else if (info.touch == DisplayDeviceInfo.TOUCH_EXTERNAL) {
1504 viewportType = VIEWPORT_EXTERNAL;
1505 } else if (info.touch == DisplayDeviceInfo.TOUCH_VIRTUAL
1506 && !TextUtils.isEmpty(info.uniqueId)) {
1507 viewportType = VIEWPORT_VIRTUAL;
1508 } else {
Arthur Hung46c4e582019-02-12 15:55:28 +08001509 Slog.i(TAG, "Display " + info + " does not support input device matching.");
Arthur Hung41e81e72018-10-31 18:04:56 +08001510 return;
Jeff Brownd728bf52012-09-08 18:05:28 -07001511 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001512
Arthur Hung41e81e72018-10-31 18:04:56 +08001513 populateViewportLocked(viewportType, display.getDisplayIdLocked(), device, info.uniqueId);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001514 }
1515
1516 /**
1517 * Get internal or external viewport. Create it if does not currently exist.
1518 * @param viewportType - either INTERNAL or EXTERNAL
1519 * @return the viewport with the requested type
1520 */
Arthur Hung41e81e72018-10-31 18:04:56 +08001521 private DisplayViewport getViewportLocked(int viewportType, String uniqueId) {
1522 if (viewportType != VIEWPORT_INTERNAL && viewportType != VIEWPORT_EXTERNAL
1523 && viewportType != VIEWPORT_VIRTUAL) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001524 Slog.wtf(TAG, "Cannot call getViewportByTypeLocked for type "
1525 + DisplayViewport.typeToString(viewportType));
1526 return null;
1527 }
Arthur Hung41e81e72018-10-31 18:04:56 +08001528
1529 // Only allow a single INTERNAL or EXTERNAL viewport by forcing their uniqueIds
1530 // to be identical (in particular, empty).
1531 // TODO (b/116824030) allow multiple EXTERNAL viewports and remove this function.
1532 if (viewportType != VIEWPORT_VIRTUAL) {
1533 uniqueId = "";
1534 }
1535
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001536 DisplayViewport viewport;
1537 final int count = mViewports.size();
1538 for (int i = 0; i < count; i++) {
1539 viewport = mViewports.get(i);
Arthur Hung41e81e72018-10-31 18:04:56 +08001540 if (viewport.type == viewportType && uniqueId.equals(viewport.uniqueId)) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001541 return viewport;
1542 }
1543 }
1544
Arthur Hung41e81e72018-10-31 18:04:56 +08001545 // Creates the viewport if none exists.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001546 viewport = new DisplayViewport();
1547 viewport.type = viewportType;
Arthur Hung41e81e72018-10-31 18:04:56 +08001548 viewport.uniqueId = uniqueId;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001549 mViewports.add(viewport);
1550 return viewport;
1551 }
1552
Arthur Hung41e81e72018-10-31 18:04:56 +08001553 private void populateViewportLocked(int viewportType,
1554 int displayId, DisplayDevice device, String uniqueId) {
1555 final DisplayViewport viewport = getViewportLocked(viewportType, uniqueId);
Jeff Brownd728bf52012-09-08 18:05:28 -07001556 device.populateViewportLocked(viewport);
Arthur Hung41e81e72018-10-31 18:04:56 +08001557 viewport.valid = true;
1558 viewport.displayId = displayId;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001559 }
1560
1561 private LogicalDisplay findLogicalDisplayForDeviceLocked(DisplayDevice device) {
1562 final int count = mLogicalDisplays.size();
1563 for (int i = 0; i < count; i++) {
1564 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1565 if (display.getPrimaryDisplayDeviceLocked() == device) {
1566 return display;
1567 }
1568 }
1569 return null;
1570 }
1571
Jeff Brownbd6e1502012-08-28 03:27:37 -07001572 private void sendDisplayEventLocked(int displayId, int event) {
1573 Message msg = mHandler.obtainMessage(MSG_DELIVER_DISPLAY_EVENT, displayId, event);
1574 mHandler.sendMessage(msg);
1575 }
1576
Robert Carrae606b42018-02-15 15:36:23 -08001577 // Requests that performTraversals be called at a
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001578 // later time to apply changes to surfaces and displays.
Craig Mautner65d11b32012-10-01 13:59:52 -07001579 private void scheduleTraversalLocked(boolean inTraversal) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001580 if (!mPendingTraversal && mWindowManagerInternal != null) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001581 mPendingTraversal = true;
Craig Mautner65d11b32012-10-01 13:59:52 -07001582 if (!inTraversal) {
1583 mHandler.sendEmptyMessage(MSG_REQUEST_TRAVERSAL);
1584 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001585 }
1586 }
1587
1588 // Runs on Handler thread.
1589 // Delivers display event notifications to callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001590 private void deliverDisplayEvent(int displayId, int event) {
1591 if (DEBUG) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001592 Slog.d(TAG, "Delivering display event: displayId="
1593 + displayId + ", event=" + event);
Jeff Brownfa25bf52012-07-23 19:26:30 -07001594 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07001595
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001596 // Grab the lock and copy the callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001597 final int count;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001598 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001599 count = mCallbacks.size();
1600 mTempCallbacks.clear();
1601 for (int i = 0; i < count; i++) {
1602 mTempCallbacks.add(mCallbacks.valueAt(i));
Craig Mautner4f67ba62012-08-02 11:23:00 -07001603 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001604 }
Craig Mautner4f67ba62012-08-02 11:23:00 -07001605
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001606 // After releasing the lock, send the notifications out.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001607 for (int i = 0; i < count; i++) {
1608 mTempCallbacks.get(i).notifyDisplayEventAsync(displayId, event);
1609 }
1610 mTempCallbacks.clear();
Craig Mautner4f67ba62012-08-02 11:23:00 -07001611 }
1612
Michael Wrightc39d47a2014-07-08 18:07:36 -07001613 private IMediaProjectionManager getProjectionService() {
1614 if (mProjectionService == null) {
1615 IBinder b = ServiceManager.getService(Context.MEDIA_PROJECTION_SERVICE);
1616 mProjectionService = IMediaProjectionManager.Stub.asInterface(b);
1617 }
1618 return mProjectionService;
1619 }
1620
Michael Wrighteef0e132017-11-21 17:57:52 +00001621 private UserManager getUserManager() {
1622 return mContext.getSystemService(UserManager.class);
1623 }
1624
Jeff Brown4ccb8232014-01-16 22:16:42 -08001625 private void dumpInternal(PrintWriter pw) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001626 pw.println("DISPLAY MANAGER (dumpsys display)");
Jeff Brownfa25bf52012-07-23 19:26:30 -07001627
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001628 synchronized (mSyncRoot) {
Jeff Brown9e316a12012-10-08 19:17:06 -07001629 pw.println(" mOnlyCode=" + mOnlyCore);
1630 pw.println(" mSafeMode=" + mSafeMode);
1631 pw.println(" mPendingTraversal=" + mPendingTraversal);
Jeff Brown037c33e2014-04-09 00:31:55 -07001632 pw.println(" mGlobalDisplayState=" + Display.stateToString(mGlobalDisplayState));
Jeff Brown9e316a12012-10-08 19:17:06 -07001633 pw.println(" mNextNonDefaultDisplayId=" + mNextNonDefaultDisplayId);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001634 pw.println(" mViewports=" + mViewports);
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -07001635 pw.println(" mDefaultDisplayDefaultColorMode=" + mDefaultDisplayDefaultColorMode);
Jeff Brown27f1d672012-10-17 18:32:34 -07001636 pw.println(" mSingleDisplayDemoMode=" + mSingleDisplayDemoMode);
Jeff Brownce468a32013-11-21 16:42:03 -08001637 pw.println(" mWifiDisplayScanRequestCount=" + mWifiDisplayScanRequestCount);
Michael Wrighteedcbf12017-08-16 23:14:54 +01001638 pw.println(" mStableDisplaySize=" + mStableDisplaySize);
Kenny Guy2047db92019-02-21 13:04:05 +00001639 pw.println(" mMinimumBrightnessCurve=" + mMinimumBrightnessCurve);
Jeff Brown9e316a12012-10-08 19:17:06 -07001640
Jeff Brownbd6e1502012-08-28 03:27:37 -07001641 IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001642 ipw.increaseIndent();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001643
1644 pw.println();
1645 pw.println("Display Adapters: size=" + mDisplayAdapters.size());
Jeff Brown848c2dc2012-08-19 20:18:08 -07001646 for (DisplayAdapter adapter : mDisplayAdapters) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001647 pw.println(" " + adapter.getName());
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001648 adapter.dumpLocked(ipw);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001649 }
Craig Mautner9de49362012-08-02 14:30:30 -07001650
Jeff Brownbd6e1502012-08-28 03:27:37 -07001651 pw.println();
1652 pw.println("Display Devices: size=" + mDisplayDevices.size());
1653 for (DisplayDevice device : mDisplayDevices) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001654 pw.println(" " + device.getDisplayDeviceInfoLocked());
1655 device.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001656 }
1657
1658 final int logicalDisplayCount = mLogicalDisplays.size();
1659 pw.println();
1660 pw.println("Logical Displays: size=" + logicalDisplayCount);
1661 for (int i = 0; i < logicalDisplayCount; i++) {
1662 int displayId = mLogicalDisplays.keyAt(i);
1663 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1664 pw.println(" Display " + displayId + ":");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001665 display.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001666 }
Jeff Brownce468a32013-11-21 16:42:03 -08001667
Michael Wrighta3dab232019-02-22 16:54:21 +00001668 pw.println();
1669 mDisplayModeDirector.dump(pw);
1670
Jeff Brownce468a32013-11-21 16:42:03 -08001671 final int callbackCount = mCallbacks.size();
1672 pw.println();
1673 pw.println("Callbacks: size=" + callbackCount);
1674 for (int i = 0; i < callbackCount; i++) {
1675 CallbackRecord callback = mCallbacks.valueAt(i);
1676 pw.println(" " + i + ": mPid=" + callback.mPid
1677 + ", mWifiDisplayScanRequested=" + callback.mWifiDisplayScanRequested);
1678 }
Jeff Brownad9ef192014-04-08 17:26:30 -07001679
1680 if (mDisplayPowerController != null) {
1681 mDisplayPowerController.dump(pw);
1682 }
Michael Wright1c9977b2016-07-12 13:30:10 -07001683
1684 pw.println();
1685 mPersistentDataStore.dump(pw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001686 }
1687 }
1688
Dan Gittik122df862018-03-28 16:59:22 +01001689 private static float[] getFloatArray(TypedArray array) {
1690 int length = array.length();
1691 float[] floatArray = new float[length];
1692 for (int i = 0; i < length; i++) {
1693 floatArray[i] = array.getFloat(i, Float.NaN);
1694 }
1695 array.recycle();
1696 return floatArray;
1697 }
1698
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001699 /**
1700 * This is the object that everything in the display manager locks on.
1701 * We make it an inner class within the {@link DisplayManagerService} to so that it is
1702 * clear that the object belongs to the display manager service and that it is
1703 * a unique object with a special purpose.
1704 */
1705 public static final class SyncRoot {
1706 }
1707
Santos Cordonee8931e2017-04-05 10:31:15 -07001708 @VisibleForTesting
1709 static class Injector {
1710 VirtualDisplayAdapter getVirtualDisplayAdapter(SyncRoot syncRoot, Context context,
1711 Handler handler, DisplayAdapter.Listener displayAdapterListener) {
1712 return new VirtualDisplayAdapter(syncRoot, context, handler, displayAdapterListener);
1713 }
Santos Cordonc22c5632017-06-21 16:03:49 -07001714
1715 long getDefaultDisplayDelayTimeout() {
1716 return WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT;
1717 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001718 }
1719
Alex Sakhartchouk879d24f2017-06-20 22:01:19 -04001720 @VisibleForTesting
1721 DisplayDeviceInfo getDisplayDeviceInfoInternal(int displayId) {
1722 synchronized (mSyncRoot) {
1723 LogicalDisplay display = mLogicalDisplays.get(displayId);
1724 if (display != null) {
1725 DisplayDevice displayDevice = display.getPrimaryDisplayDeviceLocked();
1726 return displayDevice.getDisplayDeviceInfoLocked();
1727 }
1728 return null;
1729 }
1730 }
1731
Jeff Brownbd6e1502012-08-28 03:27:37 -07001732 private final class DisplayManagerHandler extends Handler {
1733 public DisplayManagerHandler(Looper looper) {
1734 super(looper, null, true /*async*/);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001735 }
Jeff Brownbf5740e2012-08-19 23:20:02 -07001736
Jeff Brownbd6e1502012-08-28 03:27:37 -07001737 @Override
1738 public void handleMessage(Message msg) {
1739 switch (msg.what) {
Santos Cordonc22c5632017-06-21 16:03:49 -07001740 case MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS:
1741 registerDefaultDisplayAdapters();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001742 break;
1743
1744 case MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS:
1745 registerAdditionalDisplayAdapters();
1746 break;
1747
1748 case MSG_DELIVER_DISPLAY_EVENT:
1749 deliverDisplayEvent(msg.arg1, msg.arg2);
1750 break;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001751
1752 case MSG_REQUEST_TRAVERSAL:
Jeff Brown4ccb8232014-01-16 22:16:42 -08001753 mWindowManagerInternal.requestTraversalFromDisplayManager();
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001754 break;
Jeff Brownd728bf52012-09-08 18:05:28 -07001755
1756 case MSG_UPDATE_VIEWPORT: {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001757 final boolean changed;
Jeff Brownd728bf52012-09-08 18:05:28 -07001758 synchronized (mSyncRoot) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001759 changed = !mTempViewports.equals(mViewports);
1760 if (changed) {
1761 mTempViewports.clear();
1762 for (DisplayViewport d : mViewports) {
1763 mTempViewports.add(d.makeCopy());
1764 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001765 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001766 }
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001767 if (changed) {
1768 mInputManagerInternal.setDisplayViewports(mTempViewports);
1769 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001770 break;
1771 }
Kenny Guy22bd0442017-10-26 00:15:54 +01001772
Michael Wrighteef0e132017-11-21 17:57:52 +00001773 case MSG_LOAD_BRIGHTNESS_CONFIGURATION:
1774 loadBrightnessConfiguration();
1775 break;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001776 }
1777 }
1778 }
1779
1780 private final class DisplayAdapterListener implements DisplayAdapter.Listener {
1781 @Override
1782 public void onDisplayDeviceEvent(DisplayDevice device, int event) {
1783 switch (event) {
1784 case DisplayAdapter.DISPLAY_DEVICE_EVENT_ADDED:
1785 handleDisplayDeviceAdded(device);
1786 break;
1787
1788 case DisplayAdapter.DISPLAY_DEVICE_EVENT_CHANGED:
1789 handleDisplayDeviceChanged(device);
1790 break;
1791
1792 case DisplayAdapter.DISPLAY_DEVICE_EVENT_REMOVED:
1793 handleDisplayDeviceRemoved(device);
1794 break;
1795 }
1796 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001797
1798 @Override
1799 public void onTraversalRequested() {
1800 synchronized (mSyncRoot) {
Craig Mautner65d11b32012-10-01 13:59:52 -07001801 scheduleTraversalLocked(false);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001802 }
1803 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001804 }
1805
1806 private final class CallbackRecord implements DeathRecipient {
Jeff Brownce468a32013-11-21 16:42:03 -08001807 public final int mPid;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001808 private final IDisplayManagerCallback mCallback;
1809
Jeff Brownce468a32013-11-21 16:42:03 -08001810 public boolean mWifiDisplayScanRequested;
1811
Jeff Brownbd6e1502012-08-28 03:27:37 -07001812 public CallbackRecord(int pid, IDisplayManagerCallback callback) {
1813 mPid = pid;
1814 mCallback = callback;
1815 }
1816
1817 @Override
1818 public void binderDied() {
1819 if (DEBUG) {
1820 Slog.d(TAG, "Display listener for pid " + mPid + " died.");
1821 }
Jeff Brownce468a32013-11-21 16:42:03 -08001822 onCallbackDied(this);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001823 }
1824
1825 public void notifyDisplayEventAsync(int displayId, int event) {
1826 try {
1827 mCallback.onDisplayEvent(displayId, event);
1828 } catch (RemoteException ex) {
1829 Slog.w(TAG, "Failed to notify process "
1830 + mPid + " that displays changed, assuming it died.", ex);
1831 binderDied();
1832 }
1833 }
1834 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001835
Santos Cordonee8931e2017-04-05 10:31:15 -07001836 @VisibleForTesting
1837 final class BinderService extends IDisplayManager.Stub {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001838 /**
1839 * Returns information about the specified logical display.
1840 *
1841 * @param displayId The logical display id.
lumarkec75b422019-01-07 15:58:38 +08001842 * @return The logical display info, return {@code null} if the display does not exist or
1843 * the calling UID isn't present on the display. The returned object must be treated as
1844 * immutable.
Jeff Brown4ccb8232014-01-16 22:16:42 -08001845 */
1846 @Override // Binder call
1847 public DisplayInfo getDisplayInfo(int displayId) {
1848 final int callingUid = Binder.getCallingUid();
1849 final long token = Binder.clearCallingIdentity();
1850 try {
1851 return getDisplayInfoInternal(displayId, callingUid);
1852 } finally {
1853 Binder.restoreCallingIdentity(token);
1854 }
1855 }
1856
1857 /**
1858 * Returns the list of all display ids.
1859 */
1860 @Override // Binder call
1861 public int[] getDisplayIds() {
1862 final int callingUid = Binder.getCallingUid();
1863 final long token = Binder.clearCallingIdentity();
1864 try {
1865 return getDisplayIdsInternal(callingUid);
1866 } finally {
1867 Binder.restoreCallingIdentity(token);
1868 }
1869 }
1870
lumarkec75b422019-01-07 15:58:38 +08001871 @Override // Binder call
1872 public boolean isUidPresentOnDisplay(int uid, int displayId) {
1873 final long token = Binder.clearCallingIdentity();
1874 try {
1875 return isUidPresentOnDisplayInternal(uid, displayId);
1876 } finally {
1877 Binder.restoreCallingIdentity(token);
1878 }
1879 }
1880
Michael Wrighteedcbf12017-08-16 23:14:54 +01001881 /**
1882 * Returns the stable device display size, in pixels.
1883 */
1884 @Override // Binder call
1885 public Point getStableDisplaySize() {
1886 final long token = Binder.clearCallingIdentity();
1887 try {
1888 return getStableDisplaySizeInternal();
1889 } finally {
1890 Binder.restoreCallingIdentity(token);
1891 }
1892 }
1893
Jeff Brown4ccb8232014-01-16 22:16:42 -08001894 @Override // Binder call
1895 public void registerCallback(IDisplayManagerCallback callback) {
1896 if (callback == null) {
1897 throw new IllegalArgumentException("listener must not be null");
1898 }
1899
1900 final int callingPid = Binder.getCallingPid();
1901 final long token = Binder.clearCallingIdentity();
1902 try {
1903 registerCallbackInternal(callback, callingPid);
1904 } finally {
1905 Binder.restoreCallingIdentity(token);
1906 }
1907 }
1908
1909 @Override // Binder call
1910 public void startWifiDisplayScan() {
1911 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1912 "Permission required to start wifi display scans");
1913
1914 final int callingPid = Binder.getCallingPid();
1915 final long token = Binder.clearCallingIdentity();
1916 try {
1917 startWifiDisplayScanInternal(callingPid);
1918 } finally {
1919 Binder.restoreCallingIdentity(token);
1920 }
1921 }
1922
1923 @Override // Binder call
1924 public void stopWifiDisplayScan() {
1925 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1926 "Permission required to stop wifi display scans");
1927
1928 final int callingPid = Binder.getCallingPid();
1929 final long token = Binder.clearCallingIdentity();
1930 try {
1931 stopWifiDisplayScanInternal(callingPid);
1932 } finally {
1933 Binder.restoreCallingIdentity(token);
1934 }
1935 }
1936
1937 @Override // Binder call
1938 public void connectWifiDisplay(String address) {
1939 if (address == null) {
1940 throw new IllegalArgumentException("address must not be null");
1941 }
1942 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1943 "Permission required to connect to a wifi display");
1944
1945 final long token = Binder.clearCallingIdentity();
1946 try {
1947 connectWifiDisplayInternal(address);
1948 } finally {
1949 Binder.restoreCallingIdentity(token);
1950 }
1951 }
1952
1953 @Override // Binder call
1954 public void disconnectWifiDisplay() {
1955 // This request does not require special permissions.
1956 // Any app can request disconnection from the currently active wifi display.
1957 // This exception should no longer be needed once wifi display control moves
1958 // to the media router service.
1959
1960 final long token = Binder.clearCallingIdentity();
1961 try {
1962 disconnectWifiDisplayInternal();
1963 } finally {
1964 Binder.restoreCallingIdentity(token);
1965 }
1966 }
1967
1968 @Override // Binder call
1969 public void renameWifiDisplay(String address, String alias) {
1970 if (address == null) {
1971 throw new IllegalArgumentException("address must not be null");
1972 }
1973 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1974 "Permission required to rename to a wifi display");
1975
1976 final long token = Binder.clearCallingIdentity();
1977 try {
1978 renameWifiDisplayInternal(address, alias);
1979 } finally {
1980 Binder.restoreCallingIdentity(token);
1981 }
1982 }
1983
1984 @Override // Binder call
1985 public void forgetWifiDisplay(String address) {
1986 if (address == null) {
1987 throw new IllegalArgumentException("address must not be null");
1988 }
1989 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1990 "Permission required to forget to a wifi display");
1991
1992 final long token = Binder.clearCallingIdentity();
1993 try {
1994 forgetWifiDisplayInternal(address);
1995 } finally {
1996 Binder.restoreCallingIdentity(token);
1997 }
1998 }
1999
2000 @Override // Binder call
2001 public void pauseWifiDisplay() {
2002 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
2003 "Permission required to pause a wifi display session");
2004
2005 final long token = Binder.clearCallingIdentity();
2006 try {
2007 pauseWifiDisplayInternal();
2008 } finally {
2009 Binder.restoreCallingIdentity(token);
2010 }
2011 }
2012
2013 @Override // Binder call
2014 public void resumeWifiDisplay() {
2015 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
2016 "Permission required to resume a wifi display session");
2017
2018 final long token = Binder.clearCallingIdentity();
2019 try {
2020 resumeWifiDisplayInternal();
2021 } finally {
2022 Binder.restoreCallingIdentity(token);
2023 }
2024 }
2025
2026 @Override // Binder call
2027 public WifiDisplayStatus getWifiDisplayStatus() {
2028 // This request does not require special permissions.
2029 // Any app can get information about available wifi displays.
2030
2031 final long token = Binder.clearCallingIdentity();
2032 try {
2033 return getWifiDisplayStatusInternal();
2034 } finally {
2035 Binder.restoreCallingIdentity(token);
2036 }
2037 }
2038
2039 @Override // Binder call
Michael Wright1c9977b2016-07-12 13:30:10 -07002040 public void requestColorMode(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +01002041 mContext.enforceCallingOrSelfPermission(
Michael Wright1c9977b2016-07-12 13:30:10 -07002042 Manifest.permission.CONFIGURE_DISPLAY_COLOR_MODE,
2043 "Permission required to change the display color mode");
Michael Wright58e829f2015-09-15 00:13:26 +01002044 final long token = Binder.clearCallingIdentity();
2045 try {
Michael Wright1c9977b2016-07-12 13:30:10 -07002046 requestColorModeInternal(displayId, colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +01002047 } finally {
2048 Binder.restoreCallingIdentity(token);
2049 }
2050 }
2051
2052 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07002053 public int createVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wrightc39d47a2014-07-08 18:07:36 -07002054 IMediaProjection projection, String packageName, String name,
Santos Cordonee8931e2017-04-05 10:31:15 -07002055 int width, int height, int densityDpi, Surface surface, int flags,
2056 String uniqueId) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08002057 final int callingUid = Binder.getCallingUid();
2058 if (!validatePackageName(callingUid, packageName)) {
2059 throw new SecurityException("packageName must match the calling uid");
2060 }
Michael Wright75ee9fc2014-09-01 19:55:22 -07002061 if (callback == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08002062 throw new IllegalArgumentException("appToken must not be null");
2063 }
2064 if (TextUtils.isEmpty(name)) {
2065 throw new IllegalArgumentException("name must be non-null and non-empty");
2066 }
2067 if (width <= 0 || height <= 0 || densityDpi <= 0) {
2068 throw new IllegalArgumentException("width, height, and densityDpi must be "
2069 + "greater than 0");
2070 }
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07002071 if (surface != null && surface.isSingleBuffered()) {
Pablo Ceballosaff2f942016-07-29 14:49:55 -07002072 throw new IllegalArgumentException("Surface can't be single-buffered");
2073 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07002074
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002075 if ((flags & VIRTUAL_DISPLAY_FLAG_PUBLIC) != 0) {
2076 flags |= VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
2077
2078 // Public displays can't be allowed to show content when locked.
Andrii Kulian7211d2e2017-01-27 15:58:05 -08002079 if ((flags & VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD) != 0) {
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002080 throw new IllegalArgumentException(
2081 "Public display must not be marked as SHOW_WHEN_LOCKED_INSECURE");
2082 }
Michael Wright6720be42014-07-29 19:14:16 -07002083 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002084 if ((flags & VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY) != 0) {
2085 flags &= ~VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
Michael Wright6720be42014-07-29 19:14:16 -07002086 }
2087
Michael Wrightc39d47a2014-07-08 18:07:36 -07002088 if (projection != null) {
2089 try {
2090 if (!getProjectionService().isValidMediaProjection(projection)) {
2091 throw new SecurityException("Invalid media projection");
2092 }
Michael Wright6720be42014-07-29 19:14:16 -07002093 flags = projection.applyVirtualDisplayFlags(flags);
Michael Wrightc39d47a2014-07-08 18:07:36 -07002094 } catch (RemoteException e) {
Michael Wright6720be42014-07-29 19:14:16 -07002095 throw new SecurityException("unable to validate media projection or flags");
Michael Wrightc39d47a2014-07-08 18:07:36 -07002096 }
2097 }
2098
Michael Wright6720be42014-07-29 19:14:16 -07002099 if (callingUid != Process.SYSTEM_UID &&
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002100 (flags & VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07002101 if (!canProjectVideo(projection)) {
2102 throw new SecurityException("Requires CAPTURE_VIDEO_OUTPUT or "
2103 + "CAPTURE_SECURE_VIDEO_OUTPUT permission, or an appropriate "
2104 + "MediaProjection token in order to create a screen sharing virtual "
2105 + "display.");
2106 }
2107 }
Santos Cordonb6992f22018-01-30 14:51:20 -08002108 if (callingUid != Process.SYSTEM_UID && (flags & VIRTUAL_DISPLAY_FLAG_SECURE) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07002109 if (!canProjectSecureVideo(projection)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08002110 throw new SecurityException("Requires CAPTURE_SECURE_VIDEO_OUTPUT "
Michael Wrightc39d47a2014-07-08 18:07:36 -07002111 + "or an appropriate MediaProjection token to create a "
2112 + "secure virtual display.");
Jeff Brown4ccb8232014-01-16 22:16:42 -08002113 }
2114 }
2115
Chilun67a379b2019-04-11 19:49:42 +08002116 // Sometimes users can have sensitive information in system decoration windows. An app
2117 // could create a virtual display with system decorations support and read the user info
2118 // from the surface.
2119 // We should only allow adding flag VIRTUAL_DISPLAY_FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS
2120 // to virtual displays that are owned by the system.
2121 if (callingUid != Process.SYSTEM_UID
2122 && (flags & VIRTUAL_DISPLAY_FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS) != 0) {
2123 if (!checkCallingPermission(INTERNAL_SYSTEM_WINDOW, "createVirtualDisplay()")) {
2124 throw new SecurityException("Requires INTERNAL_SYSTEM_WINDOW permission");
2125 }
2126 }
2127
Jeff Brown4ccb8232014-01-16 22:16:42 -08002128 final long token = Binder.clearCallingIdentity();
2129 try {
Santos Cordonee8931e2017-04-05 10:31:15 -07002130 return createVirtualDisplayInternal(callback, projection, callingUid, packageName,
2131 name, width, height, densityDpi, surface, flags, uniqueId);
Jeff Brown4ccb8232014-01-16 22:16:42 -08002132 } finally {
2133 Binder.restoreCallingIdentity(token);
2134 }
2135 }
2136
2137 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07002138 public void resizeVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wright01e840f2014-06-26 16:03:25 -07002139 int width, int height, int densityDpi) {
Jeff Chang01baf6c2019-08-20 16:21:25 +08002140 if (width <= 0 || height <= 0 || densityDpi <= 0) {
2141 throw new IllegalArgumentException("width, height, and densityDpi must be "
2142 + "greater than 0");
2143 }
Michael Wright01e840f2014-06-26 16:03:25 -07002144 final long token = Binder.clearCallingIdentity();
2145 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07002146 resizeVirtualDisplayInternal(callback.asBinder(), width, height, densityDpi);
Michael Wright01e840f2014-06-26 16:03:25 -07002147 } finally {
2148 Binder.restoreCallingIdentity(token);
2149 }
2150 }
2151
2152 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07002153 public void setVirtualDisplaySurface(IVirtualDisplayCallback callback, Surface surface) {
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07002154 if (surface != null && surface.isSingleBuffered()) {
2155 throw new IllegalArgumentException("Surface can't be single-buffered");
2156 }
Jeff Brown92207df2014-04-16 13:16:07 -07002157 final long token = Binder.clearCallingIdentity();
2158 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07002159 setVirtualDisplaySurfaceInternal(callback.asBinder(), surface);
Jeff Brown92207df2014-04-16 13:16:07 -07002160 } finally {
2161 Binder.restoreCallingIdentity(token);
2162 }
2163 }
2164
2165 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07002166 public void releaseVirtualDisplay(IVirtualDisplayCallback callback) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08002167 final long token = Binder.clearCallingIdentity();
2168 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07002169 releaseVirtualDisplayInternal(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -08002170 } finally {
2171 Binder.restoreCallingIdentity(token);
2172 }
2173 }
2174
2175 @Override // Binder call
chaviwda4c6942018-11-07 15:52:56 -08002176 public void setVirtualDisplayState(IVirtualDisplayCallback callback, boolean isOn) {
2177 final long token = Binder.clearCallingIdentity();
2178 try {
2179 setVirtualDisplayStateInternal(callback.asBinder(), isOn);
2180 } finally {
2181 Binder.restoreCallingIdentity(token);
2182 }
2183 }
2184
2185 @Override // Binder call
Jeff Brown4ccb8232014-01-16 22:16:42 -08002186 public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06002187 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
Jeff Brown4ccb8232014-01-16 22:16:42 -08002188
2189 final long token = Binder.clearCallingIdentity();
2190 try {
2191 dumpInternal(pw);
2192 } finally {
2193 Binder.restoreCallingIdentity(token);
2194 }
2195 }
2196
Kenny Guy22bd0442017-10-26 00:15:54 +01002197 @Override // Binder call
Kenny Guy29aa30e2017-11-30 13:43:46 +00002198 public ParceledListSlice<BrightnessChangeEvent> getBrightnessEvents(String callingPackage) {
Kenny Guy22bd0442017-10-26 00:15:54 +01002199 mContext.enforceCallingOrSelfPermission(
2200 Manifest.permission.BRIGHTNESS_SLIDER_USAGE,
2201 "Permission to read brightness events.");
Kenny Guy29aa30e2017-11-30 13:43:46 +00002202
2203 final int callingUid = Binder.getCallingUid();
2204 AppOpsManager appOpsManager = mContext.getSystemService(AppOpsManager.class);
Jeff Sharkeyd9311192018-04-16 16:50:34 +00002205 final int mode = appOpsManager.noteOp(AppOpsManager.OP_GET_USAGE_STATS,
Kenny Guy29aa30e2017-11-30 13:43:46 +00002206 callingUid, callingPackage);
2207 final boolean hasUsageStats;
2208 if (mode == AppOpsManager.MODE_DEFAULT) {
2209 // The default behavior here is to check if PackageManager has given the app
2210 // permission.
2211 hasUsageStats = mContext.checkCallingPermission(
2212 Manifest.permission.PACKAGE_USAGE_STATS)
2213 == PackageManager.PERMISSION_GRANTED;
2214 } else {
2215 hasUsageStats = mode == AppOpsManager.MODE_ALLOWED;
2216 }
2217
2218 final int userId = UserHandle.getUserId(callingUid);
Kenny Guy22bd0442017-10-26 00:15:54 +01002219 final long token = Binder.clearCallingIdentity();
2220 try {
Michael Wright144aac92017-12-21 18:37:41 +00002221 synchronized (mSyncRoot) {
2222 return mDisplayPowerController.getBrightnessEvents(userId, hasUsageStats);
2223 }
Kenny Guy22bd0442017-10-26 00:15:54 +01002224 } finally {
2225 Binder.restoreCallingIdentity(token);
2226 }
2227 }
2228
Michael Wrighteef0e132017-11-21 17:57:52 +00002229 @Override // Binder call
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002230 public ParceledListSlice<AmbientBrightnessDayStats> getAmbientBrightnessStats() {
2231 mContext.enforceCallingOrSelfPermission(
2232 Manifest.permission.ACCESS_AMBIENT_LIGHT_STATS,
2233 "Permission required to to access ambient light stats.");
2234 final int callingUid = Binder.getCallingUid();
2235 final int userId = UserHandle.getUserId(callingUid);
2236 final long token = Binder.clearCallingIdentity();
2237 try {
2238 synchronized (mSyncRoot) {
2239 return mDisplayPowerController.getAmbientBrightnessStats(userId);
2240 }
2241 } finally {
2242 Binder.restoreCallingIdentity(token);
2243 }
2244 }
2245
2246 @Override // Binder call
Michael Wrighteef0e132017-11-21 17:57:52 +00002247 public void setBrightnessConfigurationForUser(
Kenny Guy05ce8092018-01-17 13:44:20 +00002248 BrightnessConfiguration c, @UserIdInt int userId, String packageName) {
Michael Wrighteef0e132017-11-21 17:57:52 +00002249 mContext.enforceCallingOrSelfPermission(
2250 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
2251 "Permission required to change the display's brightness configuration");
2252 if (userId != UserHandle.getCallingUserId()) {
2253 mContext.enforceCallingOrSelfPermission(
2254 Manifest.permission.INTERACT_ACROSS_USERS,
2255 "Permission required to change the display brightness"
2256 + " configuration of another user");
2257 }
Kenny Guy05ce8092018-01-17 13:44:20 +00002258 if (packageName != null && !validatePackageName(getCallingUid(), packageName)) {
2259 packageName = null;
2260 }
Michael Wrighteef0e132017-11-21 17:57:52 +00002261 final long token = Binder.clearCallingIdentity();
2262 try {
Kenny Guy05ce8092018-01-17 13:44:20 +00002263 setBrightnessConfigurationForUserInternal(c, userId, packageName);
Michael Wrighteef0e132017-11-21 17:57:52 +00002264 } finally {
2265 Binder.restoreCallingIdentity(token);
2266 }
2267 }
2268
Michael Wrightd8460232018-01-16 18:04:59 +00002269 @Override // Binder call
Kenny Guy6d1009f2018-03-14 14:28:23 +00002270 public BrightnessConfiguration getBrightnessConfigurationForUser(int userId) {
2271 mContext.enforceCallingOrSelfPermission(
2272 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
2273 "Permission required to read the display's brightness configuration");
2274 if (userId != UserHandle.getCallingUserId()) {
2275 mContext.enforceCallingOrSelfPermission(
2276 Manifest.permission.INTERACT_ACROSS_USERS,
2277 "Permission required to read the display brightness"
2278 + " configuration of another user");
2279 }
2280 final long token = Binder.clearCallingIdentity();
2281 try {
2282 final int userSerial = getUserManager().getUserSerialNumber(userId);
2283 synchronized (mSyncRoot) {
2284 BrightnessConfiguration config =
2285 mPersistentDataStore.getBrightnessConfiguration(userSerial);
2286 if (config == null) {
2287 config = mDisplayPowerController.getDefaultBrightnessConfiguration();
2288 }
2289 return config;
2290 }
2291 } finally {
2292 Binder.restoreCallingIdentity(token);
2293 }
2294 }
2295
2296 @Override // Binder call
2297 public BrightnessConfiguration getDefaultBrightnessConfiguration() {
2298 mContext.enforceCallingOrSelfPermission(
2299 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
2300 "Permission required to read the display's default brightness configuration");
2301 final long token = Binder.clearCallingIdentity();
2302 try {
2303 synchronized (mSyncRoot) {
2304 return mDisplayPowerController.getDefaultBrightnessConfiguration();
2305 }
2306 } finally {
2307 Binder.restoreCallingIdentity(token);
2308 }
2309 }
2310
2311 @Override // Binder call
Galia Peycheva35f900a2020-01-09 14:31:05 +01002312 public boolean isMinimalPostProcessingRequested(int displayId) {
2313 synchronized (mSyncRoot) {
2314 return mLogicalDisplays.get(displayId).getRequestedMinimalPostProcessingLocked();
2315 }
2316 }
2317
2318 @Override // Binder call
Fiona Campbelld4eb2952019-11-04 17:19:56 +00002319 public void setTemporaryBrightness(float brightness) {
Michael Wrightd8460232018-01-16 18:04:59 +00002320 mContext.enforceCallingOrSelfPermission(
2321 Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS,
2322 "Permission required to set the display's brightness");
2323 final long token = Binder.clearCallingIdentity();
2324 try {
2325 synchronized (mSyncRoot) {
2326 mDisplayPowerController.setTemporaryBrightness(brightness);
2327 }
2328 } finally {
2329 Binder.restoreCallingIdentity(token);
2330 }
2331 }
2332
2333 @Override // Binder call
2334 public void setTemporaryAutoBrightnessAdjustment(float adjustment) {
2335 mContext.enforceCallingOrSelfPermission(
2336 Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS,
2337 "Permission required to set the display's auto brightness adjustment");
2338 final long token = Binder.clearCallingIdentity();
2339 try {
2340 synchronized (mSyncRoot) {
2341 mDisplayPowerController.setTemporaryAutoBrightnessAdjustment(adjustment);
2342 }
2343 } finally {
2344 Binder.restoreCallingIdentity(token);
2345 }
2346 }
2347
Dan Gittik7a32fba2018-03-28 12:19:38 +01002348 @Override // Binder call
2349 public void onShellCommand(FileDescriptor in, FileDescriptor out,
2350 FileDescriptor err, String[] args, ShellCallback callback,
2351 ResultReceiver resultReceiver) {
Santos Cordonc4fd8e62019-12-18 13:01:05 +00002352 new DisplayManagerShellCommand(DisplayManagerService.this).exec(this, in, out, err,
2353 args, callback, resultReceiver);
Dan Gittik7a32fba2018-03-28 12:19:38 +01002354 }
2355
Dan Gittik122df862018-03-28 16:59:22 +01002356 @Override // Binder call
2357 public Curve getMinimumBrightnessCurve() {
2358 final long token = Binder.clearCallingIdentity();
2359 try {
2360 return getMinimumBrightnessCurveInternal();
2361 } finally {
2362 Binder.restoreCallingIdentity(token);
2363 }
2364 }
2365
Peiyong Lin277eaff2019-01-16 16:18:22 -08002366 @Override // Binder call
2367 public int getPreferredWideGamutColorSpaceId() {
2368 final long token = Binder.clearCallingIdentity();
2369 try {
2370 return getPreferredWideGamutColorSpaceIdInternal();
2371 } finally {
2372 Binder.restoreCallingIdentity(token);
2373 }
2374 }
2375
Jeff Brown4ccb8232014-01-16 22:16:42 -08002376 private boolean validatePackageName(int uid, String packageName) {
2377 if (packageName != null) {
2378 String[] packageNames = mContext.getPackageManager().getPackagesForUid(uid);
2379 if (packageNames != null) {
2380 for (String n : packageNames) {
2381 if (n.equals(packageName)) {
2382 return true;
2383 }
2384 }
2385 }
2386 }
2387 return false;
2388 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07002389
2390 private boolean canProjectVideo(IMediaProjection projection) {
2391 if (projection != null) {
2392 try {
2393 if (projection.canProjectVideo()) {
2394 return true;
2395 }
2396 } catch (RemoteException e) {
2397 Slog.e(TAG, "Unable to query projection service for permissions", e);
2398 }
2399 }
Chilun67a379b2019-04-11 19:49:42 +08002400 if (checkCallingPermission(CAPTURE_VIDEO_OUTPUT, "canProjectVideo()")) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07002401 return true;
2402 }
2403 return canProjectSecureVideo(projection);
2404 }
2405
2406 private boolean canProjectSecureVideo(IMediaProjection projection) {
2407 if (projection != null) {
2408 try {
2409 if (projection.canProjectSecureVideo()){
2410 return true;
2411 }
2412 } catch (RemoteException e) {
2413 Slog.e(TAG, "Unable to query projection service for permissions", e);
2414 }
2415 }
Chilun67a379b2019-04-11 19:49:42 +08002416 return checkCallingPermission(CAPTURE_SECURE_VIDEO_OUTPUT, "canProjectSecureVideo()");
2417 }
2418
2419 private boolean checkCallingPermission(String permission, String func) {
2420 if (mContext.checkCallingPermission(permission) == PackageManager.PERMISSION_GRANTED) {
2421 return true;
2422 }
2423 final String msg = "Permission Denial: " + func + " from pid=" + Binder.getCallingPid()
2424 + ", uid=" + Binder.getCallingUid() + " requires " + permission;
2425 Slog.w(TAG, msg);
2426 return false;
Michael Wrightc39d47a2014-07-08 18:07:36 -07002427 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08002428 }
2429
2430 private final class LocalService extends DisplayManagerInternal {
Galia Peycheva056b3ee2019-06-26 14:05:12 +02002431
Jeff Brown4ccb8232014-01-16 22:16:42 -08002432 @Override
Jeff Brown037c33e2014-04-09 00:31:55 -07002433 public void initPowerManagement(final DisplayPowerCallbacks callbacks, Handler handler,
Jeff Brownad9ef192014-04-08 17:26:30 -07002434 SensorManager sensorManager) {
2435 synchronized (mSyncRoot) {
Jeff Brown037c33e2014-04-09 00:31:55 -07002436 DisplayBlanker blanker = new DisplayBlanker() {
2437 @Override
Fiona Campbelld4eb2952019-11-04 17:19:56 +00002438 public void requestDisplayState(int state, float brightness) {
Jeff Brown037c33e2014-04-09 00:31:55 -07002439 // The order of operations is important for legacy reasons.
2440 if (state == Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07002441 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07002442 }
2443
2444 callbacks.onDisplayStateChange(state);
2445
2446 if (state != Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07002447 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07002448 }
2449 }
2450 };
Jeff Brownad9ef192014-04-08 17:26:30 -07002451 mDisplayPowerController = new DisplayPowerController(
Jeff Brown037c33e2014-04-09 00:31:55 -07002452 mContext, callbacks, handler, sensorManager, blanker);
Long Lingbc841b02019-07-03 16:43:15 -07002453 mSensorManager = sensorManager;
Jeff Brownad9ef192014-04-08 17:26:30 -07002454 }
Michael Wrighteef0e132017-11-21 17:57:52 +00002455
2456 mHandler.sendEmptyMessage(MSG_LOAD_BRIGHTNESS_CONFIGURATION);
Jeff Brownad9ef192014-04-08 17:26:30 -07002457 }
2458
2459 @Override
2460 public boolean requestPowerState(DisplayPowerRequest request,
2461 boolean waitForNegativeProximity) {
Michael Wrighteef0e132017-11-21 17:57:52 +00002462 synchronized (mSyncRoot) {
2463 return mDisplayPowerController.requestPowerState(request,
2464 waitForNegativeProximity);
2465 }
Jeff Brownad9ef192014-04-08 17:26:30 -07002466 }
2467
2468 @Override
2469 public boolean isProximitySensorAvailable() {
Michael Wrighteef0e132017-11-21 17:57:52 +00002470 synchronized (mSyncRoot) {
2471 return mDisplayPowerController.isProximitySensorAvailable();
2472 }
Jeff Brownad9ef192014-04-08 17:26:30 -07002473 }
2474
2475 @Override
Robert Carr66b5664f2019-04-02 14:18:56 -07002476 public SurfaceControl.ScreenshotGraphicBuffer screenshot(int displayId) {
2477 return screenshotInternal(displayId);
Riddle Hsu654a6f92018-07-13 22:59:36 +08002478 }
2479
2480 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08002481 public DisplayInfo getDisplayInfo(int displayId) {
2482 return getDisplayInfoInternal(displayId, Process.myUid());
2483 }
2484
2485 @Override
2486 public void registerDisplayTransactionListener(DisplayTransactionListener listener) {
2487 if (listener == null) {
2488 throw new IllegalArgumentException("listener must not be null");
2489 }
2490
2491 registerDisplayTransactionListenerInternal(listener);
2492 }
2493
2494 @Override
2495 public void unregisterDisplayTransactionListener(DisplayTransactionListener listener) {
2496 if (listener == null) {
2497 throw new IllegalArgumentException("listener must not be null");
2498 }
2499
2500 unregisterDisplayTransactionListenerInternal(listener);
2501 }
2502
2503 @Override
2504 public void setDisplayInfoOverrideFromWindowManager(int displayId, DisplayInfo info) {
2505 setDisplayInfoOverrideFromWindowManagerInternal(displayId, info);
2506 }
2507
2508 @Override
Andrii Kuliancd097992017-03-23 18:31:59 -07002509 public void getNonOverrideDisplayInfo(int displayId, DisplayInfo outInfo) {
2510 getNonOverrideDisplayInfoInternal(displayId, outInfo);
2511 }
2512
2513 @Override
Robert Carrae606b42018-02-15 15:36:23 -08002514 public void performTraversal(SurfaceControl.Transaction t) {
2515 performTraversalInternal(t);
Jeff Brown4ccb8232014-01-16 22:16:42 -08002516 }
2517
2518 @Override
Michael Wright3f145a22014-07-22 19:46:03 -07002519 public void setDisplayProperties(int displayId, boolean hasContent,
Galia Peycheva056b3ee2019-06-26 14:05:12 +02002520 float requestedRefreshRate, int requestedMode,
2521 boolean requestedMinimalPostProcessing, boolean inTraversal) {
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07002522 setDisplayPropertiesInternal(displayId, hasContent, requestedRefreshRate,
Galia Peycheva056b3ee2019-06-26 14:05:12 +02002523 requestedMode, requestedMinimalPostProcessing, inTraversal);
Jeff Brown4ccb8232014-01-16 22:16:42 -08002524 }
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08002525
2526 @Override
2527 public void setDisplayOffsets(int displayId, int x, int y) {
2528 setDisplayOffsetsInternal(displayId, x, y);
2529 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002530
2531 @Override
Sam Lin4c3ac2b2019-02-18 04:50:26 -08002532 public void setDisplayScalingDisabled(int displayId, boolean disableScaling) {
2533 setDisplayScalingDisabledInternal(displayId, disableScaling);
2534 }
2535
2536 @Override
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002537 public void setDisplayAccessUIDs(SparseArray<IntArray> newDisplayAccessUIDs) {
2538 setDisplayAccessUIDsInternal(newDisplayAccessUIDs);
2539 }
2540
2541 @Override
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002542 public void persistBrightnessTrackerState() {
Michael Wright144aac92017-12-21 18:37:41 +00002543 synchronized (mSyncRoot) {
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002544 mDisplayPowerController.persistBrightnessTrackerState();
Michael Wright144aac92017-12-21 18:37:41 +00002545 }
Kenny Guycfe7b702017-11-14 21:04:58 +00002546 }
Adrian Roose1d68cd2018-01-17 12:54:50 +01002547
2548 @Override
2549 public void onOverlayChanged() {
2550 synchronized (mSyncRoot) {
Adrian Roos898ec382018-01-17 12:54:50 +01002551 for (int i = 0; i < mDisplayDevices.size(); i++) {
2552 mDisplayDevices.get(i).onOverlayChangedLocked();
Adrian Roose1d68cd2018-01-17 12:54:50 +01002553 }
2554 }
2555 }
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07002556
2557 @Override
2558 public DisplayedContentSamplingAttributes getDisplayedContentSamplingAttributes(
2559 int displayId) {
2560 return getDisplayedContentSamplingAttributesInternal(displayId);
2561 }
2562
2563 @Override
2564 public boolean setDisplayedContentSamplingEnabled(
2565 int displayId, boolean enable, int componentMask, int maxFrames) {
2566 return setDisplayedContentSamplingEnabledInternal(
2567 displayId, enable, componentMask, maxFrames);
2568 }
2569
2570 @Override
2571 public DisplayedContentSample getDisplayedContentSample(int displayId,
2572 long maxFrames, long timestamp) {
2573 return getDisplayedContentSampleInternal(displayId, maxFrames, timestamp);
2574 }
2575
Jeff Brown4ccb8232014-01-16 22:16:42 -08002576 }
Michael Wrighta3dab232019-02-22 16:54:21 +00002577
Ana Kruleca74a8642019-11-14 00:51:00 +01002578 class DesiredDisplayModeSpecsObserver
2579 implements DisplayModeDirector.DesiredDisplayModeSpecsListener {
2580 public void onDesiredDisplayModeSpecsChanged() {
2581 onDesiredDisplayModeSpecsChangedInternal();
Michael Wrighta3dab232019-02-22 16:54:21 +00002582 }
2583 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07002584}