blob: 713f9c8f173d70eb0d7e1e0ed305d46464b847e0 [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
Charles Chenb28fb722020-05-21 17:19:32 +080019import static android.Manifest.permission.ADD_TRUSTED_DISPLAY;
Chilun67a379b2019-04-11 19:49:42 +080020import static android.Manifest.permission.CAPTURE_SECURE_VIDEO_OUTPUT;
21import static android.Manifest.permission.CAPTURE_VIDEO_OUTPUT;
22import static android.Manifest.permission.INTERNAL_SYSTEM_WINDOW;
Andrii Kulianfc8f82b2017-01-26 13:17:27 -080023import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
Chilun67a379b2019-04-11 19:49:42 +080024import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD;
Andrii Kulianfc8f82b2017-01-26 13:17:27 -080025import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY;
26import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;
27import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_SECURE;
Chilun67a379b2019-04-11 19:49:42 +080028import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS;
Charles Chenb28fb722020-05-21 17:19:32 +080029import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_TRUSTED;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +010030import static android.hardware.display.DisplayViewport.VIEWPORT_EXTERNAL;
31import static android.hardware.display.DisplayViewport.VIEWPORT_INTERNAL;
32import static android.hardware.display.DisplayViewport.VIEWPORT_VIRTUAL;
Jacky Kao333de4e2020-04-07 15:01:41 +080033import static android.view.Surface.ROTATION_270;
34import static android.view.Surface.ROTATION_90;
Jeff Brownbd6e1502012-08-28 03:27:37 -070035
Jeff Brownfa25bf52012-07-23 19:26:30 -070036import android.Manifest;
Santos Cordonee8931e2017-04-05 10:31:15 -070037import android.annotation.NonNull;
Kenny Guy05ce8092018-01-17 13:44:20 +000038import android.annotation.Nullable;
Michael Wrighteef0e132017-11-21 17:57:52 +000039import android.annotation.UserIdInt;
Kenny Guy29aa30e2017-11-30 13:43:46 +000040import android.app.AppOpsManager;
Jeff Brownfa25bf52012-07-23 19:26:30 -070041import android.content.Context;
42import android.content.pm.PackageManager;
Kenny Guy22bd0442017-10-26 00:15:54 +010043import android.content.pm.ParceledListSlice;
Michael Wrighteedcbf12017-08-16 23:14:54 +010044import android.content.res.Resources;
Dan Gittik122df862018-03-28 16:59:22 +010045import android.content.res.TypedArray;
Galia Peycheva088d7c02019-12-13 11:27:23 +010046import android.database.ContentObserver;
Peiyong Lin277eaff2019-01-16 16:18:22 -080047import android.graphics.ColorSpace;
Michael Wrighteedcbf12017-08-16 23:14:54 +010048import android.graphics.Point;
Robert Carr5c52b132019-02-15 15:48:11 -080049import android.graphics.Rect;
Jeff Brownad9ef192014-04-08 17:26:30 -070050import android.hardware.SensorManager;
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +000051import android.hardware.display.AmbientBrightnessDayStats;
Kenny Guy22bd0442017-10-26 00:15:54 +010052import android.hardware.display.BrightnessChangeEvent;
Michael Wrighteef0e132017-11-21 17:57:52 +000053import android.hardware.display.BrightnessConfiguration;
Dan Gittik122df862018-03-28 16:59:22 +010054import android.hardware.display.Curve;
Jeff Brownbd6e1502012-08-28 03:27:37 -070055import android.hardware.display.DisplayManagerGlobal;
Jeff Brown4ccb8232014-01-16 22:16:42 -080056import android.hardware.display.DisplayManagerInternal;
Jeff Brown4ccb8232014-01-16 22:16:42 -080057import android.hardware.display.DisplayManagerInternal.DisplayTransactionListener;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +010058import android.hardware.display.DisplayViewport;
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -070059import android.hardware.display.DisplayedContentSample;
60import android.hardware.display.DisplayedContentSamplingAttributes;
Jeff Brownfa25bf52012-07-23 19:26:30 -070061import android.hardware.display.IDisplayManager;
Jeff Brownbd6e1502012-08-28 03:27:37 -070062import android.hardware.display.IDisplayManagerCallback;
Michael Wright75ee9fc2014-09-01 19:55:22 -070063import android.hardware.display.IVirtualDisplayCallback;
b0202.jung925435c2020-01-08 18:46:59 +090064import android.hardware.display.VirtualDisplayConfig;
Jeff Browne08ae382012-09-07 20:36:36 -070065import android.hardware.display.WifiDisplayStatus;
Jeff Brown4ccb8232014-01-16 22:16:42 -080066import android.hardware.input.InputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -070067import android.media.projection.IMediaProjection;
68import android.media.projection.IMediaProjectionManager;
Galia Peycheva088d7c02019-12-13 11:27:23 +010069import android.net.Uri;
Jeff Brownfa25bf52012-07-23 19:26:30 -070070import android.os.Binder;
Jeff Brownbd6e1502012-08-28 03:27:37 -070071import android.os.Handler;
Jeff Brown64a55af2012-08-26 02:47:39 -070072import android.os.IBinder;
Jeff Brown4ccb8232014-01-16 22:16:42 -080073import android.os.IBinder.DeathRecipient;
Jeff Brownbd6e1502012-08-28 03:27:37 -070074import android.os.Looper;
75import android.os.Message;
Jeff Brown5d6443b2015-04-10 20:15:01 -070076import android.os.PowerManager;
Craig Mautner4504de52013-12-20 09:06:56 -080077import android.os.Process;
Jeff Brownbd6e1502012-08-28 03:27:37 -070078import android.os.RemoteException;
Dan Gittik7a32fba2018-03-28 12:19:38 +010079import android.os.ResultReceiver;
Michael Wrightc39d47a2014-07-08 18:07:36 -070080import android.os.ServiceManager;
Dan Gittik7a32fba2018-03-28 12:19:38 +010081import android.os.ShellCallback;
Jeff Brownbd6e1502012-08-28 03:27:37 -070082import android.os.SystemClock;
Jeff Brownfa25bf52012-07-23 19:26:30 -070083import android.os.SystemProperties;
Jeff Brown5d6443b2015-04-10 20:15:01 -070084import android.os.Trace;
Kenny Guy22bd0442017-10-26 00:15:54 +010085import android.os.UserHandle;
Michael Wrighteef0e132017-11-21 17:57:52 +000086import android.os.UserManager;
Galia Peycheva088d7c02019-12-13 11:27:23 +010087import android.provider.Settings;
Jeff Browna506a6e2013-06-04 00:02:38 -070088import android.text.TextUtils;
Charles Chenc947b102020-10-13 01:14:25 +000089import android.util.EventLog;
Andrii Kulianfb1bf692017-01-17 11:17:34 -080090import android.util.IntArray;
Dan Gittik122df862018-03-28 16:59:22 +010091import android.util.Pair;
Jeff Brownbd6e1502012-08-28 03:27:37 -070092import android.util.Slog;
93import android.util.SparseArray;
Dan Gittik122df862018-03-28 16:59:22 +010094import android.util.Spline;
Jeff Brownfa25bf52012-07-23 19:26:30 -070095import android.view.Display;
96import android.view.DisplayInfo;
Jeff Browna506a6e2013-06-04 00:02:38 -070097import android.view.Surface;
Robert Carrae606b42018-02-15 15:36:23 -080098import android.view.SurfaceControl;
Jeff Browna506a6e2013-06-04 00:02:38 -070099
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100100import com.android.internal.annotations.GuardedBy;
101import com.android.internal.annotations.VisibleForTesting;
102import com.android.internal.util.DumpUtils;
103import com.android.internal.util.IndentingPrintWriter;
Jorim Jaggied7993b2017-03-28 18:50:01 +0100104import com.android.server.AnimationThread;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800105import com.android.server.DisplayThread;
106import com.android.server.LocalServices;
107import com.android.server.SystemService;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700108import com.android.server.UiThread;
Jorim Jaggi21c39a72017-10-20 15:47:51 +0200109import com.android.server.wm.SurfaceAnimationThread;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100110import com.android.server.wm.WindowManagerInternal;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700111
112import java.io.FileDescriptor;
113import java.io.PrintWriter;
114import java.util.ArrayList;
Jeff Browna506a6e2013-06-04 00:02:38 -0700115import java.util.Arrays;
Jeff Browne75926d2014-09-18 15:24:49 -0700116import java.util.List;
Chris Yed6d35592020-05-01 11:36:33 -0700117import java.util.Optional;
Jeff Brown7f3994e2012-12-04 14:04:28 -0800118import java.util.concurrent.CopyOnWriteArrayList;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700119
120/**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700121 * Manages attached displays.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700122 * <p>
Jeff Brownbd6e1502012-08-28 03:27:37 -0700123 * The {@link DisplayManagerService} manages the global lifecycle of displays,
124 * decides how to configure logical displays based on the physical display devices currently
125 * attached, sends notifications to the system and to applications when the state
126 * changes, and so on.
127 * </p><p>
128 * The display manager service relies on a collection of {@link DisplayAdapter} components,
129 * for discovering and configuring physical display devices attached to the system.
130 * There are separate display adapters for each manner that devices are attached:
Dominik Laskowski26290bb2020-01-15 16:09:55 -0800131 * one display adapter for physical displays, one for simulated non-functional
Jeff Brownbd6e1502012-08-28 03:27:37 -0700132 * displays when the system is headless, one for simulated overlay displays used for
133 * development, one for wifi displays, etc.
134 * </p><p>
135 * Display adapters are only weakly coupled to the display manager service.
136 * Display adapters communicate changes in display device state to the display manager
Craig Mautner722285e2012-09-07 13:55:58 -0700137 * service asynchronously via a {@link DisplayAdapter.Listener} registered
Jeff Brownbd6e1502012-08-28 03:27:37 -0700138 * by the display manager service. This separation of concerns is important for
139 * two main reasons. First, it neatly encapsulates the responsibilities of these
140 * two classes: display adapters handle individual display devices whereas
141 * the display manager service handles the global state. Second, it eliminates
142 * the potential for deadlocks resulting from asynchronous display device discovery.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700143 * </p>
144 *
145 * <h3>Synchronization</h3>
146 * <p>
147 * Because the display manager may be accessed by multiple threads, the synchronization
148 * story gets a little complicated. In particular, the window manager may call into
149 * the display manager while holding a surface transaction with the expectation that
150 * it can apply changes immediately. Unfortunately, that means we can't just do
151 * everything asynchronously (*grump*).
Jeff Brownbd6e1502012-08-28 03:27:37 -0700152 * </p><p>
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700153 * To make this work, all of the objects that belong to the display manager must
154 * use the same lock. We call this lock the synchronization root and it has a unique
155 * type {@link DisplayManagerService.SyncRoot}. Methods that require this lock are
156 * named with the "Locked" suffix.
157 * </p><p>
158 * Where things get tricky is that the display manager is not allowed to make
159 * any potentially reentrant calls, especially into the window manager. We generally
160 * avoid this by making all potentially reentrant out-calls asynchronous.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700161 * </p>
162 */
Jeff Brown4ccb8232014-01-16 22:16:42 -0800163public final class DisplayManagerService extends SystemService {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700164 private static final String TAG = "DisplayManagerService";
Jeff Brownbd6e1502012-08-28 03:27:37 -0700165 private static final boolean DEBUG = false;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700166
Jeff Brownbbd28a22012-09-20 16:47:15 -0700167 // When this system property is set to 0, WFD is forcibly disabled on boot.
168 // When this system property is set to 1, WFD is forcibly enabled on boot.
169 // Otherwise WFD is enabled according to the value of config_enableWifiDisplay.
170 private static final String FORCE_WIFI_DISPLAY_ENABLE = "persist.debug.wfd.enable";
171
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200172 private static final String PROP_DEFAULT_DISPLAY_TOP_INSET = "persist.sys.displayinset.top";
173
Jeff Brownbd6e1502012-08-28 03:27:37 -0700174 private static final long WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT = 10000;
175
Santos Cordonc22c5632017-06-21 16:03:49 -0700176 private static final int MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS = 1;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700177 private static final int MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS = 2;
178 private static final int MSG_DELIVER_DISPLAY_EVENT = 3;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700179 private static final int MSG_REQUEST_TRAVERSAL = 4;
Jeff Brownd728bf52012-09-08 18:05:28 -0700180 private static final int MSG_UPDATE_VIEWPORT = 5;
Michael Wrighta3dab232019-02-22 16:54:21 +0000181 private static final int MSG_LOAD_BRIGHTNESS_CONFIGURATION = 6;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700182
Jeff Brownb880d882014-02-10 19:47:07 -0800183 private final Context mContext;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700184 private final DisplayManagerHandler mHandler;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700185 private final Handler mUiHandler;
186 private final DisplayAdapterListener mDisplayAdapterListener;
Michael Wrighta3dab232019-02-22 16:54:21 +0000187 private final DisplayModeDirector mDisplayModeDirector;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800188 private WindowManagerInternal mWindowManagerInternal;
189 private InputManagerInternal mInputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -0700190 private IMediaProjectionManager mProjectionService;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700191
192 // The synchronization root for the display manager.
193 // This lock guards most of the display manager's state.
Craig Mautner0bf6ec92012-12-18 08:33:27 -0800194 // NOTE: This is synchronized on while holding WindowManagerService.mWindowMap so never call
195 // into WindowManagerService methods that require mWindowMap while holding this unless you are
196 // very very sure that no deadlock can occur.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700197 private final SyncRoot mSyncRoot = new SyncRoot();
198
199 // True if in safe mode.
200 // This option may disable certain display adapters.
201 public boolean mSafeMode;
202
203 // True if we are in a special boot mode where only core applications and
204 // services should be started. This option may disable certain display adapters.
205 public boolean mOnlyCore;
206
Jeff Brown27f1d672012-10-17 18:32:34 -0700207 // True if the display manager service should pretend there is only one display
208 // and only tell applications about the existence of the default logical display.
209 // The display manager can still mirror content to secondary displays but applications
210 // cannot present unique content on those displays.
211 // Used for demonstration purposes only.
212 private final boolean mSingleDisplayDemoMode;
213
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700214 // All callback records indexed by calling process id.
215 public final SparseArray<CallbackRecord> mCallbacks =
Jeff Brownbd6e1502012-08-28 03:27:37 -0700216 new SparseArray<CallbackRecord>();
Jeff Brownfa25bf52012-07-23 19:26:30 -0700217
Jeff Brownbd6e1502012-08-28 03:27:37 -0700218 // List of all currently registered display adapters.
219 private final ArrayList<DisplayAdapter> mDisplayAdapters = new ArrayList<DisplayAdapter>();
220
221 // List of all currently connected display devices.
222 private final ArrayList<DisplayDevice> mDisplayDevices = new ArrayList<DisplayDevice>();
223
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700224 // List of all logical displays indexed by logical display id.
Tim Murray890ceb52020-01-30 10:12:40 -0800225 // Any modification to mLogicalDisplays must invalidate the DisplayManagerGlobal cache.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700226 private final SparseArray<LogicalDisplay> mLogicalDisplays =
227 new SparseArray<LogicalDisplay>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700228 private int mNextNonDefaultDisplayId = Display.DEFAULT_DISPLAY + 1;
229
Jeff Brown7f3994e2012-12-04 14:04:28 -0800230 // List of all display transaction listeners.
231 private final CopyOnWriteArrayList<DisplayTransactionListener> mDisplayTransactionListeners =
232 new CopyOnWriteArrayList<DisplayTransactionListener>();
233
Jeff Brownad9ef192014-04-08 17:26:30 -0700234 // Display power controller.
235 private DisplayPowerController mDisplayPowerController;
236
Jeff Brown037c33e2014-04-09 00:31:55 -0700237 // The overall display state, independent of changes that might influence one
238 // display or another in particular.
Jeff Brown5d6443b2015-04-10 20:15:01 -0700239 private int mGlobalDisplayState = Display.STATE_ON;
240
241 // The overall display brightness.
Dominik Laskowski26290bb2020-01-15 16:09:55 -0800242 // For now, this only applies to the default display but we may split it up eventually.
Fiona Campbelld4eb2952019-11-04 17:19:56 +0000243 private float mGlobalDisplayBrightness;
Jeff Brown9e316a12012-10-08 19:17:06 -0700244
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700245 // Set to true when there are pending display changes that have yet to be applied
246 // to the surface flinger state.
247 private boolean mPendingTraversal;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700248
Jeff Browne08ae382012-09-07 20:36:36 -0700249 // The Wifi display adapter, or null if not registered.
250 private WifiDisplayAdapter mWifiDisplayAdapter;
251
Jeff Brownce468a32013-11-21 16:42:03 -0800252 // The number of active wifi display scan requests.
253 private int mWifiDisplayScanRequestCount;
254
Jeff Browna506a6e2013-06-04 00:02:38 -0700255 // The virtual display adapter, or null if not registered.
256 private VirtualDisplayAdapter mVirtualDisplayAdapter;
257
Michael Wrighteef0e132017-11-21 17:57:52 +0000258 // The User ID of the current user
259 private @UserIdInt int mCurrentUserId;
260
Michael Wrighteedcbf12017-08-16 23:14:54 +0100261 // The stable device screen height and width. These are not tied to a specific display, even
262 // the default display, because they need to be stable over the course of the device's entire
263 // life, even if the default display changes (e.g. a new monitor is plugged into a PC-like
264 // device).
265 private Point mStableDisplaySize = new Point();
266
Winson72dbe7e2019-04-29 14:55:30 -0700267 // Whether the system has finished booting or not.
268 private boolean mSystemReady;
269
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200270 // The top inset of the default display.
271 // This gets persisted so that the boot animation knows how to transition from the display's
272 // full size to the size configured by the user. Right now we only persist and animate the top
273 // inset, but theoretically we could do it for all of them.
274 private int mDefaultDisplayTopInset;
275
Jeff Brownd728bf52012-09-08 18:05:28 -0700276 // Viewports of the default display and the display that should receive touch
277 // input from an external source. Used by the input system.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100278 @GuardedBy("mSyncRoot")
279 private final ArrayList<DisplayViewport> mViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700280
Jeff Brown89d55462012-09-19 11:33:42 -0700281 // Persistent data store for all internal settings maintained by the display manager service.
282 private final PersistentDataStore mPersistentDataStore = new PersistentDataStore();
283
Jeff Brownbd6e1502012-08-28 03:27:37 -0700284 // Temporary callback list, used when sending display events to applications.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700285 // May be used outside of the lock but only on the handler thread.
286 private final ArrayList<CallbackRecord> mTempCallbacks = new ArrayList<CallbackRecord>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700287
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700288 // Temporary display info, used for comparing display configurations.
289 private final DisplayInfo mTempDisplayInfo = new DisplayInfo();
290
Jeff Brownd728bf52012-09-08 18:05:28 -0700291 // Temporary viewports, used when sending new viewport information to the
292 // input system. May be used outside of the lock but only on the handler thread.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100293 private final ArrayList<DisplayViewport> mTempViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700294
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700295 // The default color mode for default displays. Overrides the usual
296 // Display.Display.COLOR_MODE_DEFAULT for displays with the
297 // DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY flag set.
298 private final int mDefaultDisplayDefaultColorMode;
299
Jeff Browne75926d2014-09-18 15:24:49 -0700300 // Temporary list of deferred work to perform when setting the display state.
301 // Only used by requestDisplayState. The field is self-synchronized and only
302 // intended for use inside of the requestGlobalDisplayStateInternal function.
303 private final ArrayList<Runnable> mTempDisplayStateWorkQueue = new ArrayList<Runnable>();
304
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800305 // Lists of UIDs that are present on the displays. Maps displayId -> array of UIDs.
306 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
307
Santos Cordonee8931e2017-04-05 10:31:15 -0700308 private final Injector mInjector;
309
Dan Gittik122df862018-03-28 16:59:22 +0100310 // The minimum brightness curve, which guarantess that any brightness curve that dips below it
311 // is rejected by the system.
312 private final Curve mMinimumBrightnessCurve;
313 private final Spline mMinimumBrightnessSpline;
Peiyong Lin277eaff2019-01-16 16:18:22 -0800314 private final ColorSpace mWideColorSpace;
Dan Gittik122df862018-03-28 16:59:22 +0100315
Long Lingbc841b02019-07-03 16:43:15 -0700316 private SensorManager mSensorManager;
317
Galia Peycheva088d7c02019-12-13 11:27:23 +0100318 // Whether minimal post processing is allowed by the user.
319 @GuardedBy("mSyncRoot")
320 private boolean mMinimalPostProcessingAllowed;
321
322 // Receives notifications about changes to Settings.
323 private SettingsObserver mSettingsObserver;
324
Jeff Brownb880d882014-02-10 19:47:07 -0800325 public DisplayManagerService(Context context) {
Santos Cordonee8931e2017-04-05 10:31:15 -0700326 this(context, new Injector());
327 }
328
329 @VisibleForTesting
330 DisplayManagerService(Context context, Injector injector) {
Jeff Brownb880d882014-02-10 19:47:07 -0800331 super(context);
Santos Cordonee8931e2017-04-05 10:31:15 -0700332 mInjector = injector;
Jeff Brownb880d882014-02-10 19:47:07 -0800333 mContext = context;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800334 mHandler = new DisplayManagerHandler(DisplayThread.get().getLooper());
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700335 mUiHandler = UiThread.getHandler();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700336 mDisplayAdapterListener = new DisplayAdapterListener();
Michael Wrighta3dab232019-02-22 16:54:21 +0000337 mDisplayModeDirector = new DisplayModeDirector(context, mHandler);
Jeff Brown27f1d672012-10-17 18:32:34 -0700338 mSingleDisplayDemoMode = SystemProperties.getBoolean("persist.demo.singledisplay", false);
Dan Gittik122df862018-03-28 16:59:22 +0100339 Resources resources = mContext.getResources();
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700340 mDefaultDisplayDefaultColorMode = mContext.getResources().getInteger(
Michael Wrighteef0e132017-11-21 17:57:52 +0000341 com.android.internal.R.integer.config_defaultDisplayDefaultColorMode);
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200342 mDefaultDisplayTopInset = SystemProperties.getInt(PROP_DEFAULT_DISPLAY_TOP_INSET, -1);
Dan Gittik122df862018-03-28 16:59:22 +0100343 float[] lux = getFloatArray(resources.obtainTypedArray(
344 com.android.internal.R.array.config_minimumBrightnessCurveLux));
345 float[] nits = getFloatArray(resources.obtainTypedArray(
346 com.android.internal.R.array.config_minimumBrightnessCurveNits));
347 mMinimumBrightnessCurve = new Curve(lux, nits);
348 mMinimumBrightnessSpline = Spline.createSpline(lux, nits);
Jeff Brown5d6443b2015-04-10 20:15:01 -0700349
Michael Wrighta3dab232019-02-22 16:54:21 +0000350 PowerManager pm = mContext.getSystemService(PowerManager.class);
Fiona Campbelld4eb2952019-11-04 17:19:56 +0000351 mGlobalDisplayBrightness = pm.getBrightnessConstraint(
352 PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_DEFAULT);
Michael Wrighteef0e132017-11-21 17:57:52 +0000353 mCurrentUserId = UserHandle.USER_SYSTEM;
Peiyong Lin277eaff2019-01-16 16:18:22 -0800354 ColorSpace[] colorSpaces = SurfaceControl.getCompositionColorSpaces();
355 mWideColorSpace = colorSpaces[1];
Winson72dbe7e2019-04-29 14:55:30 -0700356
357 mSystemReady = false;
Joel Fernandes2d314e12017-04-04 16:32:15 -0700358 }
359
360 public void setupSchedulerPolicies() {
Jorim Jaggied7993b2017-03-28 18:50:01 +0100361 // android.display and android.anim is critical to user experience and we should make sure
Michael Wrighteef0e132017-11-21 17:57:52 +0000362 // it is not in the default foregroup groups, add it to top-app to make sure it uses all
363 // the cores and scheduling settings for top-app when it runs.
Jorim Jaggied7993b2017-03-28 18:50:01 +0100364 Process.setThreadGroupAndCpuset(DisplayThread.get().getThreadId(),
365 Process.THREAD_GROUP_TOP_APP);
366 Process.setThreadGroupAndCpuset(AnimationThread.get().getThreadId(),
367 Process.THREAD_GROUP_TOP_APP);
Jorim Jaggi21c39a72017-10-20 15:47:51 +0200368 Process.setThreadGroupAndCpuset(SurfaceAnimationThread.get().getThreadId(),
369 Process.THREAD_GROUP_TOP_APP);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700370 }
371
Jeff Brown4ccb8232014-01-16 22:16:42 -0800372 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -0800373 public void onStart() {
Michael Wright1c9977b2016-07-12 13:30:10 -0700374 // We need to pre-load the persistent data store so it's ready before the default display
375 // adapter is up so that we have it's configuration. We could load it lazily, but since
376 // 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 -0700377 // we've waited for the display to register itself with us.
Michael Wrighta3dab232019-02-22 16:54:21 +0000378 synchronized (mSyncRoot) {
379 mPersistentDataStore.loadIfNeeded();
380 loadStableDisplayValuesLocked();
Michael Wrighteedcbf12017-08-16 23:14:54 +0100381 }
Santos Cordonc22c5632017-06-21 16:03:49 -0700382 mHandler.sendEmptyMessage(MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800383
Tim Murray890ceb52020-01-30 10:12:40 -0800384 // If there was a runtime restart then we may have stale caches left around, so we need to
385 // make sure to invalidate them upon every start.
386 DisplayManagerGlobal.invalidateLocalDisplayInfoCaches();
387
Jeff Brown4ccb8232014-01-16 22:16:42 -0800388 publishBinderService(Context.DISPLAY_SERVICE, new BinderService(),
389 true /*allowIsolated*/);
390 publishLocalService(DisplayManagerInternal.class, new LocalService());
391 }
392
393 @Override
394 public void onBootPhase(int phase) {
395 if (phase == PHASE_WAIT_FOR_DEFAULT_DISPLAY) {
396 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700397 long timeout = SystemClock.uptimeMillis()
398 + mInjector.getDefaultDisplayDelayTimeout();
399 while (mLogicalDisplays.get(Display.DEFAULT_DISPLAY) == null ||
400 mVirtualDisplayAdapter == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800401 long delay = timeout - SystemClock.uptimeMillis();
402 if (delay <= 0) {
403 throw new RuntimeException("Timeout waiting for default display "
Santos Cordonc22c5632017-06-21 16:03:49 -0700404 + "to be initialized. DefaultDisplay="
405 + mLogicalDisplays.get(Display.DEFAULT_DISPLAY)
406 + ", mVirtualDisplayAdapter=" + mVirtualDisplayAdapter);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800407 }
408 if (DEBUG) {
409 Slog.d(TAG, "waitForDefaultDisplay: waiting, timeout=" + delay);
410 }
411 try {
412 mSyncRoot.wait(delay);
413 } catch (InterruptedException ex) {
414 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700415 }
416 }
417 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700418 }
419
Michael Wrighteef0e132017-11-21 17:57:52 +0000420 @Override
421 public void onSwitchUser(@UserIdInt int newUserId) {
422 final int userSerial = getUserManager().getUserSerialNumber(newUserId);
423 synchronized (mSyncRoot) {
424 if (mCurrentUserId != newUserId) {
425 mCurrentUserId = newUserId;
426 BrightnessConfiguration config =
427 mPersistentDataStore.getBrightnessConfiguration(userSerial);
428 mDisplayPowerController.setBrightnessConfiguration(config);
Galia Peycheva088d7c02019-12-13 11:27:23 +0100429 handleSettingsChange();
Michael Wrighteef0e132017-11-21 17:57:52 +0000430 }
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +0000431 mDisplayPowerController.onSwitchUser(newUserId);
Michael Wrighteef0e132017-11-21 17:57:52 +0000432 }
433 }
434
Jeff Brown4ccb8232014-01-16 22:16:42 -0800435 // TODO: Use dependencies or a boot phase
436 public void windowManagerAndInputReady() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700437 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800438 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
439 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner65d11b32012-10-01 13:59:52 -0700440 scheduleTraversalLocked(false);
Jeff Brownd728bf52012-09-08 18:05:28 -0700441 }
442 }
443
444 /**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700445 * Called when the system is ready to go.
446 */
447 public void systemReady(boolean safeMode, boolean onlyCore) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700448 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700449 mSafeMode = safeMode;
450 mOnlyCore = onlyCore;
Winson72dbe7e2019-04-29 14:55:30 -0700451 mSystemReady = true;
452 // Just in case the top inset changed before the system was ready. At this point, any
453 // relevant configuration should be in place.
454 recordTopInsetLocked(mLogicalDisplays.get(Display.DEFAULT_DISPLAY));
Galia Peycheva088d7c02019-12-13 11:27:23 +0100455
456 updateSettingsLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700457 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700458
Ana Kruleca74a8642019-11-14 00:51:00 +0100459 mDisplayModeDirector.setDesiredDisplayModeSpecsListener(
460 new DesiredDisplayModeSpecsObserver());
Long Lingbc841b02019-07-03 16:43:15 -0700461 mDisplayModeDirector.start(mSensorManager);
Michael Wrighta3dab232019-02-22 16:54:21 +0000462
Jeff Brownbd6e1502012-08-28 03:27:37 -0700463 mHandler.sendEmptyMessage(MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS);
Galia Peycheva088d7c02019-12-13 11:27:23 +0100464
465 mSettingsObserver = new SettingsObserver();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700466 }
467
Santos Cordonee8931e2017-04-05 10:31:15 -0700468 @VisibleForTesting
469 Handler getDisplayHandler() {
470 return mHandler;
471 }
472
Michael Wrighteedcbf12017-08-16 23:14:54 +0100473 private void loadStableDisplayValuesLocked() {
474 final Point size = mPersistentDataStore.getStableDisplaySize();
475 if (size.x > 0 && size.y > 0) {
476 // Just set these values directly so we don't write the display persistent data again
477 // unnecessarily
478 mStableDisplaySize.set(size.x, size.y);
479 } else {
480 final Resources res = mContext.getResources();
481 final int width = res.getInteger(
482 com.android.internal.R.integer.config_stableDeviceDisplayWidth);
483 final int height = res.getInteger(
484 com.android.internal.R.integer.config_stableDeviceDisplayHeight);
485 if (width > 0 && height > 0) {
486 setStableDisplaySizeLocked(width, height);
487 }
488 }
489 }
490
491 private Point getStableDisplaySizeInternal() {
492 Point r = new Point();
493 synchronized (mSyncRoot) {
494 if (mStableDisplaySize.x > 0 && mStableDisplaySize.y > 0) {
495 r.set(mStableDisplaySize.x, mStableDisplaySize.y);
496 }
497 }
498 return r;
499 }
500
Jeff Brown4ccb8232014-01-16 22:16:42 -0800501 private void registerDisplayTransactionListenerInternal(
502 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800503 // List is self-synchronized copy-on-write.
504 mDisplayTransactionListeners.add(listener);
505 }
506
Jeff Brown4ccb8232014-01-16 22:16:42 -0800507 private void unregisterDisplayTransactionListenerInternal(
508 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800509 // List is self-synchronized copy-on-write.
510 mDisplayTransactionListeners.remove(listener);
511 }
512
Jeff Brown4ccb8232014-01-16 22:16:42 -0800513 private void setDisplayInfoOverrideFromWindowManagerInternal(
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700514 int displayId, DisplayInfo info) {
515 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700516 LogicalDisplay display = mLogicalDisplays.get(displayId);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700517 if (display != null) {
Jeff Brownef981a42013-08-07 14:13:37 -0700518 if (display.setDisplayInfoOverrideFromWindowManagerLocked(info)) {
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200519 handleLogicalDisplayChanged(displayId, display);
Craig Mautner65d11b32012-10-01 13:59:52 -0700520 scheduleTraversalLocked(false);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700521 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700522 }
523 }
524 }
525
Andrii Kuliancd097992017-03-23 18:31:59 -0700526 /**
527 * @see DisplayManagerInternal#getNonOverrideDisplayInfo(int, DisplayInfo)
528 */
529 private void getNonOverrideDisplayInfoInternal(int displayId, DisplayInfo outInfo) {
530 synchronized (mSyncRoot) {
531 final LogicalDisplay display = mLogicalDisplays.get(displayId);
532 if (display != null) {
533 display.getNonOverrideDisplayInfoLocked(outInfo);
534 }
535 }
536 }
537
Santos Cordonee8931e2017-04-05 10:31:15 -0700538 @VisibleForTesting
Robert Carrae606b42018-02-15 15:36:23 -0800539 void performTraversalInternal(SurfaceControl.Transaction t) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700540 synchronized (mSyncRoot) {
541 if (!mPendingTraversal) {
542 return;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700543 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700544 mPendingTraversal = false;
545
Robert Carrae606b42018-02-15 15:36:23 -0800546 performTraversalLocked(t);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700547 }
Jeff Brown7f3994e2012-12-04 14:04:28 -0800548
549 // List is self-synchronized copy-on-write.
550 for (DisplayTransactionListener listener : mDisplayTransactionListeners) {
Vishnu Naire86bd982018-11-28 13:23:17 -0800551 listener.onDisplayTransaction(t);
Jeff Brown7f3994e2012-12-04 14:04:28 -0800552 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700553 }
554
Fiona Campbelld4eb2952019-11-04 17:19:56 +0000555 private void requestGlobalDisplayStateInternal(int state, float brightnessState) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700556 if (state == Display.STATE_UNKNOWN) {
557 state = Display.STATE_ON;
558 }
559 if (state == Display.STATE_OFF) {
Fiona Campbelld4eb2952019-11-04 17:19:56 +0000560 brightnessState = PowerManager.BRIGHTNESS_OFF_FLOAT;
Fiona Campbell57af7c82020-04-02 12:56:15 +0100561 } else if (brightnessState != PowerManager.BRIGHTNESS_OFF_FLOAT
562 && brightnessState < PowerManager.BRIGHTNESS_MIN) {
Fiona Campbelld4eb2952019-11-04 17:19:56 +0000563 brightnessState = PowerManager.BRIGHTNESS_INVALID_FLOAT;
564 } else if (brightnessState > PowerManager.BRIGHTNESS_MAX) {
565 brightnessState = PowerManager.BRIGHTNESS_MAX;
Jeff Brown5d6443b2015-04-10 20:15:01 -0700566 }
567
Jeff Browne75926d2014-09-18 15:24:49 -0700568 synchronized (mTempDisplayStateWorkQueue) {
569 try {
570 // Update the display state within the lock.
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700571 // Note that we do not need to schedule traversals here although it
572 // may happen as a side-effect of displays changing state.
Jeff Browne75926d2014-09-18 15:24:49 -0700573 synchronized (mSyncRoot) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700574 if (mGlobalDisplayState == state
Fiona Campbelld4eb2952019-11-04 17:19:56 +0000575 && mGlobalDisplayBrightness == brightnessState) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700576 return; // no change
Jeff Browne75926d2014-09-18 15:24:49 -0700577 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700578
579 Trace.traceBegin(Trace.TRACE_TAG_POWER, "requestGlobalDisplayState("
580 + Display.stateToString(state)
Fiona Campbelld4eb2952019-11-04 17:19:56 +0000581 + ", brightness=" + brightnessState + ")");
Jeff Brown5d6443b2015-04-10 20:15:01 -0700582 mGlobalDisplayState = state;
Fiona Campbelld4eb2952019-11-04 17:19:56 +0000583 mGlobalDisplayBrightness = brightnessState;
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700584 applyGlobalDisplayStateLocked(mTempDisplayStateWorkQueue);
Jeff Browne75926d2014-09-18 15:24:49 -0700585 }
586
587 // Setting the display power state can take hundreds of milliseconds
588 // to complete so we defer the most expensive part of the work until
589 // after we have exited the critical section to avoid blocking other
590 // threads for a long time.
591 for (int i = 0; i < mTempDisplayStateWorkQueue.size(); i++) {
592 mTempDisplayStateWorkQueue.get(i).run();
593 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700594 Trace.traceEnd(Trace.TRACE_TAG_POWER);
Jeff Browne75926d2014-09-18 15:24:49 -0700595 } finally {
596 mTempDisplayStateWorkQueue.clear();
Jeff Brown9e316a12012-10-08 19:17:06 -0700597 }
598 }
599 }
600
Galia Peycheva088d7c02019-12-13 11:27:23 +0100601 private class SettingsObserver extends ContentObserver {
602 SettingsObserver() {
603 super(mHandler);
604
605 mContext.getContentResolver().registerContentObserver(
606 Settings.Secure.getUriFor(
607 Settings.Secure.MINIMAL_POST_PROCESSING_ALLOWED), false, this);
608 }
609
610 @Override
611 public void onChange(boolean selfChange, Uri uri) {
612 handleSettingsChange();
613 }
614 }
615
616 private void handleSettingsChange() {
617 synchronized (mSyncRoot) {
618 updateSettingsLocked();
619 scheduleTraversalLocked(false);
620 }
621 }
622
623 private void updateSettingsLocked() {
624 mMinimalPostProcessingAllowed = Settings.Secure.getIntForUser(mContext.getContentResolver(),
625 Settings.Secure.MINIMAL_POST_PROCESSING_ALLOWED, 1, UserHandle.USER_CURRENT) != 0;
626 }
627
Jeff Brown4ccb8232014-01-16 22:16:42 -0800628 private DisplayInfo getDisplayInfoInternal(int displayId, int callingUid) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700629 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800630 LogicalDisplay display = mLogicalDisplays.get(displayId);
631 if (display != null) {
632 DisplayInfo info = display.getDisplayInfoLocked();
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800633 if (info.hasAccess(callingUid)
634 || isUidPresentOnDisplayInternal(callingUid, displayId)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800635 return info;
636 }
637 }
638 return null;
639 }
640 }
641
642 private int[] getDisplayIdsInternal(int callingUid) {
643 synchronized (mSyncRoot) {
644 final int count = mLogicalDisplays.size();
645 int[] displayIds = new int[count];
646 int n = 0;
647 for (int i = 0; i < count; i++) {
648 LogicalDisplay display = mLogicalDisplays.valueAt(i);
649 DisplayInfo info = display.getDisplayInfoLocked();
650 if (info.hasAccess(callingUid)) {
651 displayIds[n++] = mLogicalDisplays.keyAt(i);
652 }
653 }
654 if (n != count) {
655 displayIds = Arrays.copyOfRange(displayIds, 0, n);
656 }
657 return displayIds;
658 }
659 }
660
661 private void registerCallbackInternal(IDisplayManagerCallback callback, int callingPid) {
662 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700663 if (mCallbacks.get(callingPid) != null) {
664 throw new SecurityException("The calling process has already "
665 + "registered an IDisplayManagerCallback.");
Jeff Brown64a55af2012-08-26 02:47:39 -0700666 }
667
Jeff Brownbd6e1502012-08-28 03:27:37 -0700668 CallbackRecord record = new CallbackRecord(callingPid, callback);
669 try {
670 IBinder binder = callback.asBinder();
671 binder.linkToDeath(record, 0);
672 } catch (RemoteException ex) {
673 // give up
674 throw new RuntimeException(ex);
675 }
676
677 mCallbacks.put(callingPid, record);
678 }
679 }
680
Jeff Brownce468a32013-11-21 16:42:03 -0800681 private void onCallbackDied(CallbackRecord record) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700682 synchronized (mSyncRoot) {
Jeff Brownce468a32013-11-21 16:42:03 -0800683 mCallbacks.remove(record.mPid);
684 stopWifiDisplayScanLocked(record);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700685 }
686 }
687
Jeff Brown4ccb8232014-01-16 22:16:42 -0800688 private void startWifiDisplayScanInternal(int callingPid) {
689 synchronized (mSyncRoot) {
690 CallbackRecord record = mCallbacks.get(callingPid);
691 if (record == null) {
692 throw new IllegalStateException("The calling process has not "
693 + "registered an IDisplayManagerCallback.");
Jeff Browne08ae382012-09-07 20:36:36 -0700694 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800695 startWifiDisplayScanLocked(record);
Jeff Browne08ae382012-09-07 20:36:36 -0700696 }
697 }
698
Jeff Brownce468a32013-11-21 16:42:03 -0800699 private void startWifiDisplayScanLocked(CallbackRecord record) {
700 if (!record.mWifiDisplayScanRequested) {
701 record.mWifiDisplayScanRequested = true;
702 if (mWifiDisplayScanRequestCount++ == 0) {
703 if (mWifiDisplayAdapter != null) {
704 mWifiDisplayAdapter.requestStartScanLocked();
705 }
706 }
707 }
708 }
709
Jeff Brown4ccb8232014-01-16 22:16:42 -0800710 private void stopWifiDisplayScanInternal(int callingPid) {
711 synchronized (mSyncRoot) {
712 CallbackRecord record = mCallbacks.get(callingPid);
713 if (record == null) {
714 throw new IllegalStateException("The calling process has not "
715 + "registered an IDisplayManagerCallback.");
Jeff Brownce468a32013-11-21 16:42:03 -0800716 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800717 stopWifiDisplayScanLocked(record);
Jeff Brownce468a32013-11-21 16:42:03 -0800718 }
719 }
720
721 private void stopWifiDisplayScanLocked(CallbackRecord record) {
722 if (record.mWifiDisplayScanRequested) {
723 record.mWifiDisplayScanRequested = false;
724 if (--mWifiDisplayScanRequestCount == 0) {
725 if (mWifiDisplayAdapter != null) {
726 mWifiDisplayAdapter.requestStopScanLocked();
727 }
728 } else if (mWifiDisplayScanRequestCount < 0) {
Dianne Hackborn8d051722014-10-01 14:59:58 -0700729 Slog.wtf(TAG, "mWifiDisplayScanRequestCount became negative: "
Jeff Brownce468a32013-11-21 16:42:03 -0800730 + mWifiDisplayScanRequestCount);
731 mWifiDisplayScanRequestCount = 0;
732 }
733 }
734 }
735
Jeff Brown4ccb8232014-01-16 22:16:42 -0800736 private void connectWifiDisplayInternal(String address) {
737 synchronized (mSyncRoot) {
738 if (mWifiDisplayAdapter != null) {
739 mWifiDisplayAdapter.requestConnectLocked(address);
Jeff Browne08ae382012-09-07 20:36:36 -0700740 }
Jeff Browne08ae382012-09-07 20:36:36 -0700741 }
742 }
743
Jeff Brown4ccb8232014-01-16 22:16:42 -0800744 private void pauseWifiDisplayInternal() {
745 synchronized (mSyncRoot) {
746 if (mWifiDisplayAdapter != null) {
747 mWifiDisplayAdapter.requestPauseLocked();
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 resumeWifiDisplayInternal() {
753 synchronized (mSyncRoot) {
754 if (mWifiDisplayAdapter != null) {
755 mWifiDisplayAdapter.requestResumeLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700756 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700757 }
758 }
759
Jeff Brown4ccb8232014-01-16 22:16:42 -0800760 private void disconnectWifiDisplayInternal() {
761 synchronized (mSyncRoot) {
762 if (mWifiDisplayAdapter != null) {
763 mWifiDisplayAdapter.requestDisconnectLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700764 }
Jeff Browne08ae382012-09-07 20:36:36 -0700765 }
766 }
767
Jeff Brown4ccb8232014-01-16 22:16:42 -0800768 private void renameWifiDisplayInternal(String address, String alias) {
769 synchronized (mSyncRoot) {
770 if (mWifiDisplayAdapter != null) {
771 mWifiDisplayAdapter.requestRenameLocked(address, alias);
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 void forgetWifiDisplayInternal(String address) {
777 synchronized (mSyncRoot) {
778 if (mWifiDisplayAdapter != null) {
779 mWifiDisplayAdapter.requestForgetLocked(address);
Jeff Brown89d55462012-09-19 11:33:42 -0700780 }
Jeff Brown89d55462012-09-19 11:33:42 -0700781 }
782 }
783
Jeff Brown4ccb8232014-01-16 22:16:42 -0800784 private WifiDisplayStatus getWifiDisplayStatusInternal() {
785 synchronized (mSyncRoot) {
786 if (mWifiDisplayAdapter != null) {
787 return mWifiDisplayAdapter.getWifiDisplayStatusLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700788 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800789 return new WifiDisplayStatus();
Jeff Browne08ae382012-09-07 20:36:36 -0700790 }
791 }
792
Michael Wright1c9977b2016-07-12 13:30:10 -0700793 private void requestColorModeInternal(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +0100794 synchronized (mSyncRoot) {
795 LogicalDisplay display = mLogicalDisplays.get(displayId);
796 if (display != null &&
Michael Wright1c9977b2016-07-12 13:30:10 -0700797 display.getRequestedColorModeLocked() != colorMode) {
798 display.setRequestedColorModeLocked(colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +0100799 scheduleTraversalLocked(false);
800 }
801 }
802 }
803
Michael Wright75ee9fc2014-09-01 19:55:22 -0700804 private int createVirtualDisplayInternal(IVirtualDisplayCallback callback,
b0202.jung925435c2020-01-08 18:46:59 +0900805 IMediaProjection projection, int callingUid, String packageName, Surface surface,
806 int flags, VirtualDisplayConfig virtualDisplayConfig) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800807 synchronized (mSyncRoot) {
808 if (mVirtualDisplayAdapter == null) {
809 Slog.w(TAG, "Rejecting request to create private virtual display "
810 + "because the virtual display adapter is not available.");
811 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700812 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800813
814 DisplayDevice device = mVirtualDisplayAdapter.createVirtualDisplayLocked(
b0202.jung925435c2020-01-08 18:46:59 +0900815 callback, projection, callingUid, packageName, surface, flags,
816 virtualDisplayConfig);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800817 if (device == null) {
818 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700819 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700820
Jeff Brown4ccb8232014-01-16 22:16:42 -0800821 handleDisplayDeviceAddedLocked(device);
822 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
823 if (display != null) {
824 return display.getDisplayIdLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700825 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800826
827 // Something weird happened and the logical display was not created.
828 Slog.w(TAG, "Rejecting request to create virtual display "
829 + "because the logical display was not created.");
Michael Wright75ee9fc2014-09-01 19:55:22 -0700830 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800831 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700832 }
833 return -1;
834 }
835
Michael Wright01e840f2014-06-26 16:03:25 -0700836 private void resizeVirtualDisplayInternal(IBinder appToken,
837 int width, int height, int densityDpi) {
838 synchronized (mSyncRoot) {
839 if (mVirtualDisplayAdapter == null) {
840 return;
841 }
842
843 mVirtualDisplayAdapter.resizeVirtualDisplayLocked(appToken, width, height, densityDpi);
844 }
845 }
846
Jeff Brown92207df2014-04-16 13:16:07 -0700847 private void setVirtualDisplaySurfaceInternal(IBinder appToken, Surface surface) {
848 synchronized (mSyncRoot) {
849 if (mVirtualDisplayAdapter == null) {
850 return;
851 }
852
853 mVirtualDisplayAdapter.setVirtualDisplaySurfaceLocked(appToken, surface);
854 }
855 }
856
Jeff Brown4ccb8232014-01-16 22:16:42 -0800857 private void releaseVirtualDisplayInternal(IBinder appToken) {
858 synchronized (mSyncRoot) {
859 if (mVirtualDisplayAdapter == null) {
860 return;
Jeff Browna506a6e2013-06-04 00:02:38 -0700861 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700862
Jeff Brown4ccb8232014-01-16 22:16:42 -0800863 DisplayDevice device =
864 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(appToken);
865 if (device != null) {
866 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700867 }
868 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700869 }
870
chaviwda4c6942018-11-07 15:52:56 -0800871 private void setVirtualDisplayStateInternal(IBinder appToken, boolean isOn) {
872 synchronized (mSyncRoot) {
873 if (mVirtualDisplayAdapter == null) {
874 return;
875 }
876
877 mVirtualDisplayAdapter.setVirtualDisplayStateLocked(appToken, isOn);
878 }
879 }
880
Santos Cordonc22c5632017-06-21 16:03:49 -0700881 private void registerDefaultDisplayAdapters() {
882 // Register default display adapters.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700883 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700884 // main display adapter
Mike Lockwoode63f6f72013-11-15 11:01:47 -0800885 registerDisplayAdapterLocked(new LocalDisplayAdapter(
886 mSyncRoot, mContext, mHandler, mDisplayAdapterListener));
Santos Cordonc22c5632017-06-21 16:03:49 -0700887
888 // Standalone VR devices rely on a virtual display as their primary display for
889 // 2D UI. We register virtual display adapter along side the main display adapter
890 // here so that it is ready by the time the system sends the home Intent for
891 // early apps like SetupWizard/Launcher. In particular, SUW is displayed using
892 // the virtual display inside VR before any VR-specific apps even run.
893 mVirtualDisplayAdapter = mInjector.getVirtualDisplayAdapter(mSyncRoot, mContext,
894 mHandler, mDisplayAdapterListener);
895 if (mVirtualDisplayAdapter != null) {
896 registerDisplayAdapterLocked(mVirtualDisplayAdapter);
897 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700898 }
899 }
900
901 private void registerAdditionalDisplayAdapters() {
902 synchronized (mSyncRoot) {
903 if (shouldRegisterNonEssentialDisplayAdaptersLocked()) {
Jeff Brown89d55462012-09-19 11:33:42 -0700904 registerOverlayDisplayAdapterLocked();
905 registerWifiDisplayAdapterLocked();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700906 }
907 }
908 }
909
Jeff Brown89d55462012-09-19 11:33:42 -0700910 private void registerOverlayDisplayAdapterLocked() {
911 registerDisplayAdapterLocked(new OverlayDisplayAdapter(
912 mSyncRoot, mContext, mHandler, mDisplayAdapterListener, mUiHandler));
913 }
914
915 private void registerWifiDisplayAdapterLocked() {
916 if (mContext.getResources().getBoolean(
Jeff Brownbbd28a22012-09-20 16:47:15 -0700917 com.android.internal.R.bool.config_enableWifiDisplay)
918 || SystemProperties.getInt(FORCE_WIFI_DISPLAY_ENABLE, -1) == 1) {
Jeff Brown89d55462012-09-19 11:33:42 -0700919 mWifiDisplayAdapter = new WifiDisplayAdapter(
920 mSyncRoot, mContext, mHandler, mDisplayAdapterListener,
921 mPersistentDataStore);
922 registerDisplayAdapterLocked(mWifiDisplayAdapter);
923 }
924 }
925
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700926 private boolean shouldRegisterNonEssentialDisplayAdaptersLocked() {
927 // In safe mode, we disable non-essential display adapters to give the user
928 // an opportunity to fix broken settings or other problems that might affect
929 // system stability.
930 // In only-core mode, we disable non-essential display adapters to minimize
931 // the number of dependencies that are started while in this mode and to
932 // prevent problems that might occur due to the device being encrypted.
933 return !mSafeMode && !mOnlyCore;
934 }
935
936 private void registerDisplayAdapterLocked(DisplayAdapter adapter) {
937 mDisplayAdapters.add(adapter);
938 adapter.registerLocked();
939 }
940
Jeff Brownbd6e1502012-08-28 03:27:37 -0700941 private void handleDisplayDeviceAdded(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700942 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700943 handleDisplayDeviceAddedLocked(device);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700944 }
945 }
946
Jeff Browna506a6e2013-06-04 00:02:38 -0700947 private void handleDisplayDeviceAddedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700948 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700949 if (mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700950 Slog.w(TAG, "Attempted to add already added display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700951 return;
952 }
953
Jeff Brown10acf6d2015-04-14 14:20:47 -0700954 Slog.i(TAG, "Display device added: " + info);
955 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700956
957 mDisplayDevices.add(device);
Michael Wright1c9977b2016-07-12 13:30:10 -0700958 LogicalDisplay display = addLogicalDisplayLocked(device);
Jeff Brown0033a862014-10-08 12:06:39 -0700959 Runnable work = updateDisplayStateLocked(device);
960 if (work != null) {
961 work.run();
962 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700963 scheduleTraversalLocked(false);
964 }
965
Jeff Brownbd6e1502012-08-28 03:27:37 -0700966 private void handleDisplayDeviceChanged(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700967 synchronized (mSyncRoot) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700968 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700969 if (!mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700970 Slog.w(TAG, "Attempted to change non-existent display device: " + info);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700971 return;
972 }
973
Jeff Brown10acf6d2015-04-14 14:20:47 -0700974 int diff = device.mDebugLastLoggedDeviceInfo.diff(info);
975 if (diff == DisplayDeviceInfo.DIFF_STATE) {
976 Slog.i(TAG, "Display device changed state: \"" + info.name
977 + "\", " + Display.stateToString(info.state));
Chris Yed6d35592020-05-01 11:36:33 -0700978 final Optional<Integer> viewportType = getViewportType(info);
979 if (viewportType.isPresent()) {
980 for (DisplayViewport d : mViewports) {
981 if (d.type == viewportType.get() && info.uniqueId.equals(d.uniqueId)) {
982 // Update display view port power state
983 d.isActive = Display.isActiveState(info.state);
984 }
985 }
986 if (mInputManagerInternal != null) {
987 mHandler.sendEmptyMessage(MSG_UPDATE_VIEWPORT);
988 }
989 }
Jeff Brown10acf6d2015-04-14 14:20:47 -0700990 } else if (diff != 0) {
991 Slog.i(TAG, "Display device changed: " + info);
992 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700993 if ((diff & DisplayDeviceInfo.DIFF_COLOR_MODE) != 0) {
994 try {
995 mPersistentDataStore.setColorMode(device, info.colorMode);
996 } finally {
997 mPersistentDataStore.saveIfNeeded();
998 }
999 }
Jeff Brown10acf6d2015-04-14 14:20:47 -07001000 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browne87bf032012-09-20 18:30:13 -07001001
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001002 device.applyPendingDisplayDeviceInfoChangesLocked();
1003 if (updateLogicalDisplaysLocked()) {
Craig Mautner65d11b32012-10-01 13:59:52 -07001004 scheduleTraversalLocked(false);
Jeff Brown64a55af2012-08-26 02:47:39 -07001005 }
1006 }
1007 }
1008
Jeff Brownbd6e1502012-08-28 03:27:37 -07001009 private void handleDisplayDeviceRemoved(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001010 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001011 handleDisplayDeviceRemovedLocked(device);
1012 }
1013 }
Jeff Brown8e5d33e2015-06-10 13:05:50 -07001014
Jeff Browna506a6e2013-06-04 00:02:38 -07001015 private void handleDisplayDeviceRemovedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -07001016 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -07001017 if (!mDisplayDevices.remove(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -07001018 Slog.w(TAG, "Attempted to remove non-existent display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -07001019 return;
1020 }
1021
Jeff Brown10acf6d2015-04-14 14:20:47 -07001022 Slog.i(TAG, "Display device removed: " + info);
1023 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -07001024
Jeff Browna506a6e2013-06-04 00:02:38 -07001025 updateLogicalDisplaysLocked();
1026 scheduleTraversalLocked(false);
1027 }
1028
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001029 private void handleLogicalDisplayChanged(int displayId, @NonNull LogicalDisplay display) {
1030 if (displayId == Display.DEFAULT_DISPLAY) {
1031 recordTopInsetLocked(display);
1032 }
Tim Murray890ceb52020-01-30 10:12:40 -08001033 // We don't bother invalidating the display info caches here because any changes to the
1034 // display info will trigger a cache invalidation inside of LogicalDisplay before we hit
1035 // this point.
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001036 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
1037 }
1038
Tim Murray890ceb52020-01-30 10:12:40 -08001039 private void handleLogicalDisplayRemoved(int displayId) {
1040 DisplayManagerGlobal.invalidateLocalDisplayInfoCaches();
1041 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_REMOVED);
1042 }
1043
Jeff Brown8e5d33e2015-06-10 13:05:50 -07001044 private void applyGlobalDisplayStateLocked(List<Runnable> workQueue) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001045 final int count = mDisplayDevices.size();
1046 for (int i = 0; i < count; i++) {
1047 DisplayDevice device = mDisplayDevices.get(i);
Jeff Browne75926d2014-09-18 15:24:49 -07001048 Runnable runnable = updateDisplayStateLocked(device);
1049 if (runnable != null) {
1050 workQueue.add(runnable);
1051 }
Jeff Browna506a6e2013-06-04 00:02:38 -07001052 }
1053 }
1054
Jeff Browne75926d2014-09-18 15:24:49 -07001055 private Runnable updateDisplayStateLocked(DisplayDevice device) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001056 // Blank or unblank the display immediately to match the state requested
Jeff Brown037c33e2014-04-09 00:31:55 -07001057 // by the display power controller (if known).
Jeff Browna506a6e2013-06-04 00:02:38 -07001058 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
1059 if ((info.flags & DisplayDeviceInfo.FLAG_NEVER_BLANK) == 0) {
Fiona Campbelld4eb2952019-11-04 17:19:56 +00001060 return device.requestDisplayStateLocked(
1061 mGlobalDisplayState, mGlobalDisplayBrightness);
Craig Mautner4f67ba62012-08-02 11:23:00 -07001062 }
Jeff Browne75926d2014-09-18 15:24:49 -07001063 return null;
Craig Mautner4f67ba62012-08-02 11:23:00 -07001064 }
1065
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001066 // Adds a new logical display based on the given display device.
1067 // Sends notifications if needed.
Michael Wright1c9977b2016-07-12 13:30:10 -07001068 private LogicalDisplay addLogicalDisplayLocked(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001069 DisplayDeviceInfo deviceInfo = device.getDisplayDeviceInfoLocked();
1070 boolean isDefault = (deviceInfo.flags
1071 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0;
1072 if (isDefault && mLogicalDisplays.get(Display.DEFAULT_DISPLAY) != null) {
1073 Slog.w(TAG, "Ignoring attempt to add a second default display: " + deviceInfo);
1074 isDefault = false;
1075 }
1076
Jeff Brown27f1d672012-10-17 18:32:34 -07001077 if (!isDefault && mSingleDisplayDemoMode) {
1078 Slog.i(TAG, "Not creating a logical display for a secondary display "
1079 + " because single display demo mode is enabled: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -07001080 return null;
Jeff Brown27f1d672012-10-17 18:32:34 -07001081 }
1082
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001083 final int displayId = assignDisplayIdLocked(isDefault);
1084 final int layerStack = assignLayerStackLocked(displayId);
1085
Jeff Brownd728bf52012-09-08 18:05:28 -07001086 LogicalDisplay display = new LogicalDisplay(displayId, layerStack, device);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001087 display.updateLocked(mDisplayDevices);
1088 if (!display.isValidLocked()) {
1089 // This should never happen currently.
1090 Slog.w(TAG, "Ignoring display device because the logical display "
1091 + "created from it was not considered valid: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -07001092 return null;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001093 }
1094
Michael Wrighteedcbf12017-08-16 23:14:54 +01001095 configureColorModeLocked(display, device);
1096 if (isDefault) {
1097 recordStableDisplayStatsIfNeededLocked(display);
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001098 recordTopInsetLocked(display);
Michael Wrighteedcbf12017-08-16 23:14:54 +01001099 }
1100
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001101 mLogicalDisplays.put(displayId, display);
Tim Murray890ceb52020-01-30 10:12:40 -08001102 DisplayManagerGlobal.invalidateLocalDisplayInfoCaches();
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001103
1104 // Wake up waitForDefaultDisplay.
1105 if (isDefault) {
1106 mSyncRoot.notifyAll();
1107 }
1108
1109 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_ADDED);
Michael Wright1c9977b2016-07-12 13:30:10 -07001110 return display;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001111 }
1112
1113 private int assignDisplayIdLocked(boolean isDefault) {
1114 return isDefault ? Display.DEFAULT_DISPLAY : mNextNonDefaultDisplayId++;
1115 }
1116
1117 private int assignLayerStackLocked(int displayId) {
1118 // Currently layer stacks and display ids are the same.
1119 // This need not be the case.
1120 return displayId;
1121 }
1122
Michael Wrighteedcbf12017-08-16 23:14:54 +01001123 private void configureColorModeLocked(LogicalDisplay display, DisplayDevice device) {
1124 if (display.getPrimaryDisplayDeviceLocked() == device) {
1125 int colorMode = mPersistentDataStore.getColorMode(device);
1126 if (colorMode == Display.COLOR_MODE_INVALID) {
1127 if ((device.getDisplayDeviceInfoLocked().flags
1128 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
1129 colorMode = mDefaultDisplayDefaultColorMode;
1130 } else {
1131 colorMode = Display.COLOR_MODE_DEFAULT;
1132 }
1133 }
1134 display.setRequestedColorModeLocked(colorMode);
1135 }
1136 }
1137
1138 // If we've never recorded stable device stats for this device before and they aren't
1139 // explicitly configured, go ahead and record the stable device stats now based on the status
1140 // of the default display at first boot.
1141 private void recordStableDisplayStatsIfNeededLocked(LogicalDisplay d) {
1142 if (mStableDisplaySize.x <= 0 && mStableDisplaySize.y <= 0) {
1143 DisplayInfo info = d.getDisplayInfoLocked();
1144 setStableDisplaySizeLocked(info.getNaturalWidth(), info.getNaturalHeight());
1145 }
1146 }
1147
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001148 private void recordTopInsetLocked(@Nullable LogicalDisplay d) {
Winson72dbe7e2019-04-29 14:55:30 -07001149 // We must only persist the inset after boot has completed, otherwise we will end up
1150 // overwriting the persisted value before the masking flag has been loaded from the
1151 // resource overlay.
1152 if (!mSystemReady || d == null) {
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001153 return;
1154 }
1155 int topInset = d.getInsets().top;
1156 if (topInset == mDefaultDisplayTopInset) {
1157 return;
1158 }
1159 mDefaultDisplayTopInset = topInset;
1160 SystemProperties.set(PROP_DEFAULT_DISPLAY_TOP_INSET, Integer.toString(topInset));
1161 }
1162
Michael Wrighteedcbf12017-08-16 23:14:54 +01001163 private void setStableDisplaySizeLocked(int width, int height) {
1164 mStableDisplaySize = new Point(width, height);
1165 try {
1166 mPersistentDataStore.setStableDisplaySize(mStableDisplaySize);
1167 } finally {
1168 mPersistentDataStore.saveIfNeeded();
1169 }
1170 }
1171
Dan Gittik122df862018-03-28 16:59:22 +01001172 @VisibleForTesting
1173 Curve getMinimumBrightnessCurveInternal() {
1174 return mMinimumBrightnessCurve;
1175 }
1176
Peiyong Lin277eaff2019-01-16 16:18:22 -08001177 int getPreferredWideGamutColorSpaceIdInternal() {
1178 return mWideColorSpace.getId();
1179 }
1180
Michael Wrighteef0e132017-11-21 17:57:52 +00001181 private void setBrightnessConfigurationForUserInternal(
Dan Gittik7a32fba2018-03-28 12:19:38 +01001182 @Nullable BrightnessConfiguration c, @UserIdInt int userId,
Kenny Guy05ce8092018-01-17 13:44:20 +00001183 @Nullable String packageName) {
Dan Gittik122df862018-03-28 16:59:22 +01001184 validateBrightnessConfiguration(c);
Michael Wrighteef0e132017-11-21 17:57:52 +00001185 final int userSerial = getUserManager().getUserSerialNumber(userId);
1186 synchronized (mSyncRoot) {
1187 try {
Kenny Guy05ce8092018-01-17 13:44:20 +00001188 mPersistentDataStore.setBrightnessConfigurationForUser(c, userSerial,
1189 packageName);
Michael Wrighteef0e132017-11-21 17:57:52 +00001190 } finally {
1191 mPersistentDataStore.saveIfNeeded();
1192 }
1193 if (userId == mCurrentUserId) {
1194 mDisplayPowerController.setBrightnessConfiguration(c);
1195 }
1196 }
1197 }
1198
Dan Gittik122df862018-03-28 16:59:22 +01001199 @VisibleForTesting
1200 void validateBrightnessConfiguration(BrightnessConfiguration config) {
1201 if (config == null) {
1202 return;
1203 }
1204 if (isBrightnessConfigurationTooDark(config)) {
1205 throw new IllegalArgumentException("brightness curve is too dark");
1206 }
1207 }
1208
1209 private boolean isBrightnessConfigurationTooDark(BrightnessConfiguration config) {
1210 Pair<float[], float[]> curve = config.getCurve();
1211 float[] lux = curve.first;
1212 float[] nits = curve.second;
1213 for (int i = 0; i < lux.length; i++) {
1214 if (nits[i] < mMinimumBrightnessSpline.interpolate(lux[i])) {
1215 return true;
1216 }
1217 }
1218 return false;
1219 }
1220
Michael Wrighteef0e132017-11-21 17:57:52 +00001221 private void loadBrightnessConfiguration() {
1222 synchronized (mSyncRoot) {
1223 final int userSerial = getUserManager().getUserSerialNumber(mCurrentUserId);
1224 BrightnessConfiguration config =
1225 mPersistentDataStore.getBrightnessConfiguration(userSerial);
1226 mDisplayPowerController.setBrightnessConfiguration(config);
1227 }
1228 }
1229
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001230 // Updates all existing logical displays given the current set of display devices.
1231 // Removes invalid logical displays.
1232 // Sends notifications if needed.
1233 private boolean updateLogicalDisplaysLocked() {
1234 boolean changed = false;
1235 for (int i = mLogicalDisplays.size(); i-- > 0; ) {
1236 final int displayId = mLogicalDisplays.keyAt(i);
1237 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1238
1239 mTempDisplayInfo.copyFrom(display.getDisplayInfoLocked());
1240 display.updateLocked(mDisplayDevices);
1241 if (!display.isValidLocked()) {
1242 mLogicalDisplays.removeAt(i);
Tim Murray890ceb52020-01-30 10:12:40 -08001243 handleLogicalDisplayRemoved(displayId);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001244 changed = true;
1245 } else if (!mTempDisplayInfo.equals(display.getDisplayInfoLocked())) {
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001246 handleLogicalDisplayChanged(displayId, display);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001247 changed = true;
1248 }
1249 }
1250 return changed;
1251 }
1252
Robert Carrae606b42018-02-15 15:36:23 -08001253 private void performTraversalLocked(SurfaceControl.Transaction t) {
Jeff Brownd728bf52012-09-08 18:05:28 -07001254 // Clear all viewports before configuring displays so that we can keep
1255 // track of which ones we have configured.
1256 clearViewportsLocked();
1257
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001258 // Configure each display device.
1259 final int count = mDisplayDevices.size();
1260 for (int i = 0; i < count; i++) {
1261 DisplayDevice device = mDisplayDevices.get(i);
Robert Carrae606b42018-02-15 15:36:23 -08001262 configureDisplayLocked(t, device);
1263 device.performTraversalLocked(t);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001264 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001265
1266 // Tell the input system about these new viewports.
Jeff Brown4ccb8232014-01-16 22:16:42 -08001267 if (mInputManagerInternal != null) {
Jeff Brownd728bf52012-09-08 18:05:28 -07001268 mHandler.sendEmptyMessage(MSG_UPDATE_VIEWPORT);
1269 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001270 }
1271
Michael Wright3f145a22014-07-22 19:46:03 -07001272 private void setDisplayPropertiesInternal(int displayId, boolean hasContent,
Galia Peycheva088d7c02019-12-13 11:27:23 +01001273 float requestedRefreshRate, int requestedModeId, boolean preferMinimalPostProcessing,
Galia Peycheva056b3ee2019-06-26 14:05:12 +02001274 boolean inTraversal) {
Craig Mautner722285e2012-09-07 13:55:58 -07001275 synchronized (mSyncRoot) {
1276 LogicalDisplay display = mLogicalDisplays.get(displayId);
Michael Wright3f145a22014-07-22 19:46:03 -07001277 if (display == null) {
1278 return;
1279 }
Galia Peycheva056b3ee2019-06-26 14:05:12 +02001280
1281 boolean shouldScheduleTraversal = false;
1282
Michael Wright3f145a22014-07-22 19:46:03 -07001283 if (display.hasContentLocked() != hasContent) {
Jeff Brown33041bd2013-08-02 21:11:14 -07001284 if (DEBUG) {
1285 Slog.d(TAG, "Display " + displayId + " hasContent flag changed: "
1286 + "hasContent=" + hasContent + ", inTraversal=" + inTraversal);
1287 }
1288
Craig Mautner722285e2012-09-07 13:55:58 -07001289 display.setHasContentLocked(hasContent);
Galia Peycheva056b3ee2019-06-26 14:05:12 +02001290 shouldScheduleTraversal = true;
Craig Mautner722285e2012-09-07 13:55:58 -07001291 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001292 if (requestedModeId == 0 && requestedRefreshRate != 0) {
1293 // Scan supported modes returned by display.getInfo() to find a mode with the same
1294 // size as the default display mode but with the specified refresh rate instead.
1295 requestedModeId = display.getDisplayInfoLocked().findDefaultModeByRefreshRate(
1296 requestedRefreshRate);
1297 }
Michael Wrighta3dab232019-02-22 16:54:21 +00001298 mDisplayModeDirector.getAppRequestObserver().setAppRequestedMode(
1299 displayId, requestedModeId);
Galia Peycheva056b3ee2019-06-26 14:05:12 +02001300
Galia Peycheva088d7c02019-12-13 11:27:23 +01001301 if (display.getDisplayInfoLocked().minimalPostProcessingSupported) {
1302 boolean mppRequest = mMinimalPostProcessingAllowed && preferMinimalPostProcessing;
Galia Peycheva056b3ee2019-06-26 14:05:12 +02001303
Galia Peycheva088d7c02019-12-13 11:27:23 +01001304 if (display.getRequestedMinimalPostProcessingLocked() != mppRequest) {
1305 display.setRequestedMinimalPostProcessingLocked(mppRequest);
1306 shouldScheduleTraversal = true;
1307 }
Galia Peycheva056b3ee2019-06-26 14:05:12 +02001308 }
1309
1310 if (shouldScheduleTraversal) {
1311 scheduleTraversalLocked(inTraversal);
1312 }
Craig Mautner722285e2012-09-07 13:55:58 -07001313 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001314 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001315
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08001316 private void setDisplayOffsetsInternal(int displayId, int x, int y) {
1317 synchronized (mSyncRoot) {
1318 LogicalDisplay display = mLogicalDisplays.get(displayId);
1319 if (display == null) {
1320 return;
1321 }
1322 if (display.getDisplayOffsetXLocked() != x
1323 || display.getDisplayOffsetYLocked() != y) {
1324 if (DEBUG) {
1325 Slog.d(TAG, "Display " + displayId + " burn-in offset set to ("
1326 + x + ", " + y + ")");
1327 }
1328 display.setDisplayOffsetsLocked(x, y);
1329 scheduleTraversalLocked(false);
1330 }
1331 }
1332 }
1333
Sam Lin4c3ac2b2019-02-18 04:50:26 -08001334 private void setDisplayScalingDisabledInternal(int displayId, boolean disable) {
1335 synchronized (mSyncRoot) {
1336 final LogicalDisplay display = mLogicalDisplays.get(displayId);
1337 if (display == null) {
1338 return;
1339 }
1340 if (display.isDisplayScalingDisabled() != disable) {
1341 if (DEBUG) {
1342 Slog.d(TAG, "Display " + displayId + " content scaling disabled = " + disable);
1343 }
1344 display.setDisplayScalingDisabledLocked(disable);
1345 scheduleTraversalLocked(false);
1346 }
1347 }
1348 }
1349
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001350 // Updates the lists of UIDs that are present on displays.
1351 private void setDisplayAccessUIDsInternal(SparseArray<IntArray> newDisplayAccessUIDs) {
1352 synchronized (mSyncRoot) {
1353 mDisplayAccessUIDs.clear();
1354 for (int i = newDisplayAccessUIDs.size() - 1; i >= 0; i--) {
1355 mDisplayAccessUIDs.append(newDisplayAccessUIDs.keyAt(i),
1356 newDisplayAccessUIDs.valueAt(i));
1357 }
1358 }
1359 }
1360
1361 // Checks if provided UID's content is present on the display and UID has access to it.
1362 private boolean isUidPresentOnDisplayInternal(int uid, int displayId) {
1363 synchronized (mSyncRoot) {
1364 final IntArray displayUIDs = mDisplayAccessUIDs.get(displayId);
1365 return displayUIDs != null && displayUIDs.indexOf(uid) != -1;
1366 }
1367 }
1368
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001369 @Nullable
1370 private IBinder getDisplayToken(int displayId) {
1371 synchronized (mSyncRoot) {
1372 final LogicalDisplay display = mLogicalDisplays.get(displayId);
1373 if (display != null) {
1374 final DisplayDevice device = display.getPrimaryDisplayDeviceLocked();
1375 if (device != null) {
1376 return device.getDisplayTokenLocked();
1377 }
1378 }
1379 }
1380
1381 return null;
1382 }
1383
Jacky Kao333de4e2020-04-07 15:01:41 +08001384 private SurfaceControl.ScreenshotGraphicBuffer systemScreenshotInternal(int displayId) {
Vinit Nayak627ac302020-01-13 15:50:10 -08001385 synchronized (mSyncRoot) {
1386 final IBinder token = getDisplayToken(displayId);
1387 if (token == null) {
1388 return null;
1389 }
1390 final LogicalDisplay logicalDisplay = mLogicalDisplays.get(displayId);
1391 if (logicalDisplay == null) {
1392 return null;
1393 }
1394
1395 final DisplayInfo displayInfo = logicalDisplay.getDisplayInfoLocked();
Jacky Kao333de4e2020-04-07 15:01:41 +08001396 return SurfaceControl.screenshotToBufferWithSecureLayersUnsafe(token, new Rect(),
1397 displayInfo.getNaturalWidth(), displayInfo.getNaturalHeight(),
1398 false /* useIdentityTransform */, 0 /* rotation */);
1399 }
1400 }
1401
1402 private SurfaceControl.ScreenshotGraphicBuffer userScreenshotInternal(int displayId) {
1403 synchronized (mSyncRoot) {
1404 final IBinder token = getDisplayToken(displayId);
1405 if (token == null) {
1406 return null;
Jacky Kao316477c2020-03-23 16:30:04 +08001407 }
Jacky Kao333de4e2020-04-07 15:01:41 +08001408 final LogicalDisplay logicalDisplay = mLogicalDisplays.get(displayId);
1409 if (logicalDisplay == null) {
1410 return null;
1411 }
1412
1413 final DisplayInfo displayInfo = logicalDisplay.getDisplayInfoLocked();
1414 // Takes screenshot based on current device orientation.
1415 final Display display = DisplayManagerGlobal.getInstance()
1416 .getRealDisplay(displayId);
1417 if (display == null) {
1418 return null;
1419 }
1420 final Point displaySize = new Point();
1421 display.getRealSize(displaySize);
1422
1423 int rotation = displayInfo.rotation;
1424 // TODO (b/153382624) : This workaround solution would be removed after
1425 // SurfaceFlinger fixes the inconsistency with rotation direction issue.
1426 if (rotation == ROTATION_90 || rotation == ROTATION_270) {
1427 rotation = (rotation == ROTATION_90) ? ROTATION_270 : ROTATION_90;
1428 }
1429
1430 return SurfaceControl.screenshotToBuffer(token, new Rect(), displaySize.x,
1431 displaySize.y, false /* useIdentityTransform */, rotation /* rotation */);
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001432 }
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001433 }
1434
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001435 @VisibleForTesting
1436 DisplayedContentSamplingAttributes getDisplayedContentSamplingAttributesInternal(
1437 int displayId) {
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001438 final IBinder token = getDisplayToken(displayId);
1439 if (token == null) {
1440 return null;
1441 }
1442 return SurfaceControl.getDisplayedContentSamplingAttributes(token);
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001443 }
1444
1445 @VisibleForTesting
1446 boolean setDisplayedContentSamplingEnabledInternal(
1447 int displayId, boolean enable, int componentMask, int maxFrames) {
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001448 final IBinder token = getDisplayToken(displayId);
1449 if (token == null) {
1450 return false;
1451 }
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001452 return SurfaceControl.setDisplayedContentSamplingEnabled(
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001453 token, enable, componentMask, maxFrames);
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001454 }
1455
1456 @VisibleForTesting
1457 DisplayedContentSample getDisplayedContentSampleInternal(int displayId,
1458 long maxFrames, long timestamp) {
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001459 final IBinder token = getDisplayToken(displayId);
1460 if (token == null) {
1461 return null;
1462 }
1463 return SurfaceControl.getDisplayedContentSample(token, maxFrames, timestamp);
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001464 }
1465
Santos Cordonc4fd8e62019-12-18 13:01:05 +00001466 void resetBrightnessConfiguration() {
1467 setBrightnessConfigurationForUserInternal(null, mContext.getUserId(),
1468 mContext.getPackageName());
1469 }
1470
1471 void setAutoBrightnessLoggingEnabled(boolean enabled) {
1472 if (mDisplayPowerController != null) {
1473 synchronized (mSyncRoot) {
1474 mDisplayPowerController.setAutoBrightnessLoggingEnabled(enabled);
1475 }
1476 }
1477 }
1478
1479 void setDisplayWhiteBalanceLoggingEnabled(boolean enabled) {
1480 if (mDisplayPowerController != null) {
1481 synchronized (mSyncRoot) {
1482 mDisplayPowerController.setDisplayWhiteBalanceLoggingEnabled(enabled);
1483 }
1484 }
1485 }
1486
raylinhsuf6247e32021-01-11 11:56:27 +08001487 void setDisplayModeDirectorLoggingEnabled(boolean enabled) {
1488 synchronized (mSyncRoot) {
1489 if (mDisplayModeDirector != null) {
1490 mDisplayModeDirector.setLoggingEnabled(enabled);
1491 }
1492 }
1493 }
1494
Santos Cordonc4fd8e62019-12-18 13:01:05 +00001495 void setAmbientColorTemperatureOverride(float cct) {
1496 if (mDisplayPowerController != null) {
1497 synchronized (mSyncRoot) {
1498 mDisplayPowerController.setAmbientColorTemperatureOverride(cct);
1499 }
1500 }
1501 }
1502
Ana Kruleca74a8642019-11-14 00:51:00 +01001503 private void onDesiredDisplayModeSpecsChangedInternal() {
Michael Wrighta3dab232019-02-22 16:54:21 +00001504 boolean changed = false;
1505 synchronized (mSyncRoot) {
1506 final int count = mLogicalDisplays.size();
1507 for (int i = 0; i < count; i++) {
1508 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1509 int displayId = mLogicalDisplays.keyAt(i);
Ana Kruleca74a8642019-11-14 00:51:00 +01001510 DisplayModeDirector.DesiredDisplayModeSpecs desiredDisplayModeSpecs =
1511 mDisplayModeDirector.getDesiredDisplayModeSpecs(displayId);
1512 DisplayModeDirector.DesiredDisplayModeSpecs existingDesiredDisplayModeSpecs =
1513 display.getDesiredDisplayModeSpecsLocked();
1514 if (DEBUG) {
1515 Slog.i(TAG,
1516 "Comparing display specs: " + desiredDisplayModeSpecs
1517 + ", existing: " + existingDesiredDisplayModeSpecs);
1518 }
1519 if (!desiredDisplayModeSpecs.equals(existingDesiredDisplayModeSpecs)) {
1520 display.setDesiredDisplayModeSpecsLocked(desiredDisplayModeSpecs);
Michael Wrighta3dab232019-02-22 16:54:21 +00001521 changed = true;
1522 }
1523 }
1524 if (changed) {
1525 scheduleTraversalLocked(false);
1526 }
1527 }
1528 }
1529
Jeff Brownd728bf52012-09-08 18:05:28 -07001530 private void clearViewportsLocked() {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001531 mViewports.clear();
Craig Mautner722285e2012-09-07 13:55:58 -07001532 }
1533
Chris Yed6d35592020-05-01 11:36:33 -07001534 private Optional<Integer> getViewportType(DisplayDeviceInfo info) {
1535 // Get the corresponding viewport type.
1536 if ((info.flags & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
1537 return Optional.of(VIEWPORT_INTERNAL);
1538 } else if (info.touch == DisplayDeviceInfo.TOUCH_EXTERNAL) {
1539 return Optional.of(VIEWPORT_EXTERNAL);
1540 } else if (info.touch == DisplayDeviceInfo.TOUCH_VIRTUAL
1541 && !TextUtils.isEmpty(info.uniqueId)) {
1542 return Optional.of(VIEWPORT_VIRTUAL);
1543 } else {
1544 if (DEBUG) {
1545 Slog.i(TAG, "Display " + info + " does not support input device matching.");
1546 }
1547 }
1548 return Optional.empty();
1549 }
1550
Robert Carrae606b42018-02-15 15:36:23 -08001551 private void configureDisplayLocked(SurfaceControl.Transaction t, DisplayDevice device) {
Jeff Brownd14c8c92014-01-07 18:13:09 -08001552 final DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
1553 final boolean ownContent = (info.flags & DisplayDeviceInfo.FLAG_OWN_CONTENT_ONLY) != 0;
Jeff Browna506a6e2013-06-04 00:02:38 -07001554
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001555 // Find the logical display that the display device is showing.
Jeff Brownd14c8c92014-01-07 18:13:09 -08001556 // Certain displays only ever show their own content.
Craig Mautner722285e2012-09-07 13:55:58 -07001557 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
Jeff Brownd14c8c92014-01-07 18:13:09 -08001558 if (!ownContent) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001559 if (display != null && !display.hasContentLocked()) {
1560 // If the display does not have any content of its own, then
b0202.jung925435c2020-01-08 18:46:59 +09001561 // automatically mirror the requested logical display contents if possible.
1562 display = mLogicalDisplays.get(device.getDisplayIdToMirrorLocked());
Jeff Browna506a6e2013-06-04 00:02:38 -07001563 }
1564 if (display == null) {
1565 display = mLogicalDisplays.get(Display.DEFAULT_DISPLAY);
1566 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001567 }
1568
1569 // Apply the logical display configuration to the display device.
1570 if (display == null) {
1571 // TODO: no logical display for the device, blank it
Jeff Brownd728bf52012-09-08 18:05:28 -07001572 Slog.w(TAG, "Missing logical display to use for physical display device: "
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001573 + device.getDisplayDeviceInfoLocked());
Jeff Brownd728bf52012-09-08 18:05:28 -07001574 return;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001575 }
Robert Carrae606b42018-02-15 15:36:23 -08001576 display.configureDisplayLocked(t, device, info.state == Display.STATE_OFF);
Chris Yed6d35592020-05-01 11:36:33 -07001577 final Optional<Integer> viewportType = getViewportType(info);
1578 if (viewportType.isPresent()) {
1579 populateViewportLocked(viewportType.get(), display.getDisplayIdLocked(), device, info);
Jeff Brownd728bf52012-09-08 18:05:28 -07001580 }
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001581 }
1582
1583 /**
1584 * Get internal or external viewport. Create it if does not currently exist.
1585 * @param viewportType - either INTERNAL or EXTERNAL
1586 * @return the viewport with the requested type
1587 */
Arthur Hung41e81e72018-10-31 18:04:56 +08001588 private DisplayViewport getViewportLocked(int viewportType, String uniqueId) {
1589 if (viewportType != VIEWPORT_INTERNAL && viewportType != VIEWPORT_EXTERNAL
1590 && viewportType != VIEWPORT_VIRTUAL) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001591 Slog.wtf(TAG, "Cannot call getViewportByTypeLocked for type "
1592 + DisplayViewport.typeToString(viewportType));
1593 return null;
1594 }
Arthur Hung41e81e72018-10-31 18:04:56 +08001595
1596 // Only allow a single INTERNAL or EXTERNAL viewport by forcing their uniqueIds
1597 // to be identical (in particular, empty).
1598 // TODO (b/116824030) allow multiple EXTERNAL viewports and remove this function.
1599 if (viewportType != VIEWPORT_VIRTUAL) {
1600 uniqueId = "";
1601 }
1602
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001603 DisplayViewport viewport;
1604 final int count = mViewports.size();
1605 for (int i = 0; i < count; i++) {
1606 viewport = mViewports.get(i);
Arthur Hung41e81e72018-10-31 18:04:56 +08001607 if (viewport.type == viewportType && uniqueId.equals(viewport.uniqueId)) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001608 return viewport;
1609 }
1610 }
1611
Arthur Hung41e81e72018-10-31 18:04:56 +08001612 // Creates the viewport if none exists.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001613 viewport = new DisplayViewport();
1614 viewport.type = viewportType;
Arthur Hung41e81e72018-10-31 18:04:56 +08001615 viewport.uniqueId = uniqueId;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001616 mViewports.add(viewport);
1617 return viewport;
1618 }
1619
Chris Yed6d35592020-05-01 11:36:33 -07001620 private void populateViewportLocked(int viewportType, int displayId, DisplayDevice device,
1621 DisplayDeviceInfo info) {
1622 final DisplayViewport viewport = getViewportLocked(viewportType, info.uniqueId);
Jeff Brownd728bf52012-09-08 18:05:28 -07001623 device.populateViewportLocked(viewport);
Arthur Hung41e81e72018-10-31 18:04:56 +08001624 viewport.valid = true;
1625 viewport.displayId = displayId;
Chris Yed6d35592020-05-01 11:36:33 -07001626 viewport.isActive = Display.isActiveState(info.state);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001627 }
1628
1629 private LogicalDisplay findLogicalDisplayForDeviceLocked(DisplayDevice device) {
1630 final int count = mLogicalDisplays.size();
1631 for (int i = 0; i < count; i++) {
1632 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1633 if (display.getPrimaryDisplayDeviceLocked() == device) {
1634 return display;
1635 }
1636 }
1637 return null;
1638 }
1639
Jeff Brownbd6e1502012-08-28 03:27:37 -07001640 private void sendDisplayEventLocked(int displayId, int event) {
1641 Message msg = mHandler.obtainMessage(MSG_DELIVER_DISPLAY_EVENT, displayId, event);
1642 mHandler.sendMessage(msg);
1643 }
1644
Robert Carrae606b42018-02-15 15:36:23 -08001645 // Requests that performTraversals be called at a
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001646 // later time to apply changes to surfaces and displays.
Craig Mautner65d11b32012-10-01 13:59:52 -07001647 private void scheduleTraversalLocked(boolean inTraversal) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001648 if (!mPendingTraversal && mWindowManagerInternal != null) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001649 mPendingTraversal = true;
Craig Mautner65d11b32012-10-01 13:59:52 -07001650 if (!inTraversal) {
1651 mHandler.sendEmptyMessage(MSG_REQUEST_TRAVERSAL);
1652 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001653 }
1654 }
1655
1656 // Runs on Handler thread.
1657 // Delivers display event notifications to callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001658 private void deliverDisplayEvent(int displayId, int event) {
1659 if (DEBUG) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001660 Slog.d(TAG, "Delivering display event: displayId="
1661 + displayId + ", event=" + event);
Jeff Brownfa25bf52012-07-23 19:26:30 -07001662 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07001663
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001664 // Grab the lock and copy the callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001665 final int count;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001666 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001667 count = mCallbacks.size();
1668 mTempCallbacks.clear();
1669 for (int i = 0; i < count; i++) {
1670 mTempCallbacks.add(mCallbacks.valueAt(i));
Craig Mautner4f67ba62012-08-02 11:23:00 -07001671 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001672 }
Craig Mautner4f67ba62012-08-02 11:23:00 -07001673
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001674 // After releasing the lock, send the notifications out.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001675 for (int i = 0; i < count; i++) {
1676 mTempCallbacks.get(i).notifyDisplayEventAsync(displayId, event);
1677 }
1678 mTempCallbacks.clear();
Craig Mautner4f67ba62012-08-02 11:23:00 -07001679 }
1680
Michael Wrightc39d47a2014-07-08 18:07:36 -07001681 private IMediaProjectionManager getProjectionService() {
1682 if (mProjectionService == null) {
1683 IBinder b = ServiceManager.getService(Context.MEDIA_PROJECTION_SERVICE);
1684 mProjectionService = IMediaProjectionManager.Stub.asInterface(b);
1685 }
1686 return mProjectionService;
1687 }
1688
Michael Wrighteef0e132017-11-21 17:57:52 +00001689 private UserManager getUserManager() {
1690 return mContext.getSystemService(UserManager.class);
1691 }
1692
Jeff Brown4ccb8232014-01-16 22:16:42 -08001693 private void dumpInternal(PrintWriter pw) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001694 pw.println("DISPLAY MANAGER (dumpsys display)");
Jeff Brownfa25bf52012-07-23 19:26:30 -07001695
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001696 synchronized (mSyncRoot) {
Jeff Brown9e316a12012-10-08 19:17:06 -07001697 pw.println(" mOnlyCode=" + mOnlyCore);
1698 pw.println(" mSafeMode=" + mSafeMode);
1699 pw.println(" mPendingTraversal=" + mPendingTraversal);
Jeff Brown037c33e2014-04-09 00:31:55 -07001700 pw.println(" mGlobalDisplayState=" + Display.stateToString(mGlobalDisplayState));
Jeff Brown9e316a12012-10-08 19:17:06 -07001701 pw.println(" mNextNonDefaultDisplayId=" + mNextNonDefaultDisplayId);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001702 pw.println(" mViewports=" + mViewports);
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -07001703 pw.println(" mDefaultDisplayDefaultColorMode=" + mDefaultDisplayDefaultColorMode);
Jeff Brown27f1d672012-10-17 18:32:34 -07001704 pw.println(" mSingleDisplayDemoMode=" + mSingleDisplayDemoMode);
Jeff Brownce468a32013-11-21 16:42:03 -08001705 pw.println(" mWifiDisplayScanRequestCount=" + mWifiDisplayScanRequestCount);
Michael Wrighteedcbf12017-08-16 23:14:54 +01001706 pw.println(" mStableDisplaySize=" + mStableDisplaySize);
Kenny Guy2047db92019-02-21 13:04:05 +00001707 pw.println(" mMinimumBrightnessCurve=" + mMinimumBrightnessCurve);
Jeff Brown9e316a12012-10-08 19:17:06 -07001708
Jeff Brownbd6e1502012-08-28 03:27:37 -07001709 IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001710 ipw.increaseIndent();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001711
1712 pw.println();
1713 pw.println("Display Adapters: size=" + mDisplayAdapters.size());
Jeff Brown848c2dc2012-08-19 20:18:08 -07001714 for (DisplayAdapter adapter : mDisplayAdapters) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001715 pw.println(" " + adapter.getName());
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001716 adapter.dumpLocked(ipw);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001717 }
Craig Mautner9de49362012-08-02 14:30:30 -07001718
Jeff Brownbd6e1502012-08-28 03:27:37 -07001719 pw.println();
1720 pw.println("Display Devices: size=" + mDisplayDevices.size());
1721 for (DisplayDevice device : mDisplayDevices) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001722 pw.println(" " + device.getDisplayDeviceInfoLocked());
1723 device.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001724 }
1725
1726 final int logicalDisplayCount = mLogicalDisplays.size();
1727 pw.println();
1728 pw.println("Logical Displays: size=" + logicalDisplayCount);
1729 for (int i = 0; i < logicalDisplayCount; i++) {
1730 int displayId = mLogicalDisplays.keyAt(i);
1731 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1732 pw.println(" Display " + displayId + ":");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001733 display.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001734 }
Jeff Brownce468a32013-11-21 16:42:03 -08001735
Michael Wrighta3dab232019-02-22 16:54:21 +00001736 pw.println();
1737 mDisplayModeDirector.dump(pw);
1738
Jeff Brownce468a32013-11-21 16:42:03 -08001739 final int callbackCount = mCallbacks.size();
1740 pw.println();
1741 pw.println("Callbacks: size=" + callbackCount);
1742 for (int i = 0; i < callbackCount; i++) {
1743 CallbackRecord callback = mCallbacks.valueAt(i);
1744 pw.println(" " + i + ": mPid=" + callback.mPid
1745 + ", mWifiDisplayScanRequested=" + callback.mWifiDisplayScanRequested);
1746 }
Jeff Brownad9ef192014-04-08 17:26:30 -07001747
1748 if (mDisplayPowerController != null) {
1749 mDisplayPowerController.dump(pw);
1750 }
Michael Wright1c9977b2016-07-12 13:30:10 -07001751
1752 pw.println();
1753 mPersistentDataStore.dump(pw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001754 }
1755 }
1756
Dan Gittik122df862018-03-28 16:59:22 +01001757 private static float[] getFloatArray(TypedArray array) {
1758 int length = array.length();
1759 float[] floatArray = new float[length];
1760 for (int i = 0; i < length; i++) {
1761 floatArray[i] = array.getFloat(i, Float.NaN);
1762 }
1763 array.recycle();
1764 return floatArray;
1765 }
1766
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001767 /**
1768 * This is the object that everything in the display manager locks on.
1769 * We make it an inner class within the {@link DisplayManagerService} to so that it is
1770 * clear that the object belongs to the display manager service and that it is
1771 * a unique object with a special purpose.
1772 */
1773 public static final class SyncRoot {
1774 }
1775
Santos Cordonee8931e2017-04-05 10:31:15 -07001776 @VisibleForTesting
1777 static class Injector {
1778 VirtualDisplayAdapter getVirtualDisplayAdapter(SyncRoot syncRoot, Context context,
1779 Handler handler, DisplayAdapter.Listener displayAdapterListener) {
1780 return new VirtualDisplayAdapter(syncRoot, context, handler, displayAdapterListener);
1781 }
Santos Cordonc22c5632017-06-21 16:03:49 -07001782
1783 long getDefaultDisplayDelayTimeout() {
1784 return WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT;
1785 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001786 }
1787
Alex Sakhartchouk879d24f2017-06-20 22:01:19 -04001788 @VisibleForTesting
1789 DisplayDeviceInfo getDisplayDeviceInfoInternal(int displayId) {
1790 synchronized (mSyncRoot) {
1791 LogicalDisplay display = mLogicalDisplays.get(displayId);
1792 if (display != null) {
1793 DisplayDevice displayDevice = display.getPrimaryDisplayDeviceLocked();
1794 return displayDevice.getDisplayDeviceInfoLocked();
1795 }
1796 return null;
1797 }
1798 }
1799
b0202.jung925435c2020-01-08 18:46:59 +09001800 @VisibleForTesting
1801 int getDisplayIdToMirrorInternal(int displayId) {
1802 synchronized (mSyncRoot) {
1803 LogicalDisplay display = mLogicalDisplays.get(displayId);
1804 if (display != null) {
1805 DisplayDevice displayDevice = display.getPrimaryDisplayDeviceLocked();
1806 return displayDevice.getDisplayIdToMirrorLocked();
1807 }
1808 return Display.INVALID_DISPLAY;
1809 }
1810 }
1811
1812 @VisibleForTesting
1813 Surface getVirtualDisplaySurfaceInternal(IBinder appToken) {
1814 synchronized (mSyncRoot) {
1815 if (mVirtualDisplayAdapter == null) {
1816 return null;
1817 }
1818 return mVirtualDisplayAdapter.getVirtualDisplaySurfaceLocked(appToken);
1819 }
1820 }
1821
Jeff Brownbd6e1502012-08-28 03:27:37 -07001822 private final class DisplayManagerHandler extends Handler {
1823 public DisplayManagerHandler(Looper looper) {
1824 super(looper, null, true /*async*/);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001825 }
Jeff Brownbf5740e2012-08-19 23:20:02 -07001826
Jeff Brownbd6e1502012-08-28 03:27:37 -07001827 @Override
1828 public void handleMessage(Message msg) {
1829 switch (msg.what) {
Santos Cordonc22c5632017-06-21 16:03:49 -07001830 case MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS:
1831 registerDefaultDisplayAdapters();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001832 break;
1833
1834 case MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS:
1835 registerAdditionalDisplayAdapters();
1836 break;
1837
1838 case MSG_DELIVER_DISPLAY_EVENT:
1839 deliverDisplayEvent(msg.arg1, msg.arg2);
1840 break;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001841
1842 case MSG_REQUEST_TRAVERSAL:
Jeff Brown4ccb8232014-01-16 22:16:42 -08001843 mWindowManagerInternal.requestTraversalFromDisplayManager();
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001844 break;
Jeff Brownd728bf52012-09-08 18:05:28 -07001845
1846 case MSG_UPDATE_VIEWPORT: {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001847 final boolean changed;
Jeff Brownd728bf52012-09-08 18:05:28 -07001848 synchronized (mSyncRoot) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001849 changed = !mTempViewports.equals(mViewports);
1850 if (changed) {
1851 mTempViewports.clear();
1852 for (DisplayViewport d : mViewports) {
1853 mTempViewports.add(d.makeCopy());
1854 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001855 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001856 }
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001857 if (changed) {
1858 mInputManagerInternal.setDisplayViewports(mTempViewports);
1859 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001860 break;
1861 }
Kenny Guy22bd0442017-10-26 00:15:54 +01001862
Michael Wrighteef0e132017-11-21 17:57:52 +00001863 case MSG_LOAD_BRIGHTNESS_CONFIGURATION:
1864 loadBrightnessConfiguration();
1865 break;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001866 }
1867 }
1868 }
1869
1870 private final class DisplayAdapterListener implements DisplayAdapter.Listener {
1871 @Override
1872 public void onDisplayDeviceEvent(DisplayDevice device, int event) {
1873 switch (event) {
1874 case DisplayAdapter.DISPLAY_DEVICE_EVENT_ADDED:
1875 handleDisplayDeviceAdded(device);
1876 break;
1877
1878 case DisplayAdapter.DISPLAY_DEVICE_EVENT_CHANGED:
1879 handleDisplayDeviceChanged(device);
1880 break;
1881
1882 case DisplayAdapter.DISPLAY_DEVICE_EVENT_REMOVED:
1883 handleDisplayDeviceRemoved(device);
1884 break;
1885 }
1886 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001887
1888 @Override
1889 public void onTraversalRequested() {
1890 synchronized (mSyncRoot) {
Craig Mautner65d11b32012-10-01 13:59:52 -07001891 scheduleTraversalLocked(false);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001892 }
1893 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001894 }
1895
1896 private final class CallbackRecord implements DeathRecipient {
Jeff Brownce468a32013-11-21 16:42:03 -08001897 public final int mPid;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001898 private final IDisplayManagerCallback mCallback;
1899
Jeff Brownce468a32013-11-21 16:42:03 -08001900 public boolean mWifiDisplayScanRequested;
1901
Jeff Brownbd6e1502012-08-28 03:27:37 -07001902 public CallbackRecord(int pid, IDisplayManagerCallback callback) {
1903 mPid = pid;
1904 mCallback = callback;
1905 }
1906
1907 @Override
1908 public void binderDied() {
1909 if (DEBUG) {
1910 Slog.d(TAG, "Display listener for pid " + mPid + " died.");
1911 }
Jeff Brownce468a32013-11-21 16:42:03 -08001912 onCallbackDied(this);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001913 }
1914
1915 public void notifyDisplayEventAsync(int displayId, int event) {
1916 try {
1917 mCallback.onDisplayEvent(displayId, event);
1918 } catch (RemoteException ex) {
1919 Slog.w(TAG, "Failed to notify process "
1920 + mPid + " that displays changed, assuming it died.", ex);
1921 binderDied();
1922 }
1923 }
1924 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001925
Santos Cordonee8931e2017-04-05 10:31:15 -07001926 @VisibleForTesting
1927 final class BinderService extends IDisplayManager.Stub {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001928 /**
1929 * Returns information about the specified logical display.
1930 *
1931 * @param displayId The logical display id.
lumarkec75b422019-01-07 15:58:38 +08001932 * @return The logical display info, return {@code null} if the display does not exist or
1933 * the calling UID isn't present on the display. The returned object must be treated as
1934 * immutable.
Jeff Brown4ccb8232014-01-16 22:16:42 -08001935 */
1936 @Override // Binder call
1937 public DisplayInfo getDisplayInfo(int displayId) {
1938 final int callingUid = Binder.getCallingUid();
1939 final long token = Binder.clearCallingIdentity();
1940 try {
1941 return getDisplayInfoInternal(displayId, callingUid);
1942 } finally {
1943 Binder.restoreCallingIdentity(token);
1944 }
1945 }
1946
1947 /**
1948 * Returns the list of all display ids.
1949 */
1950 @Override // Binder call
1951 public int[] getDisplayIds() {
1952 final int callingUid = Binder.getCallingUid();
1953 final long token = Binder.clearCallingIdentity();
1954 try {
1955 return getDisplayIdsInternal(callingUid);
1956 } finally {
1957 Binder.restoreCallingIdentity(token);
1958 }
1959 }
1960
lumarkec75b422019-01-07 15:58:38 +08001961 @Override // Binder call
1962 public boolean isUidPresentOnDisplay(int uid, int displayId) {
1963 final long token = Binder.clearCallingIdentity();
1964 try {
1965 return isUidPresentOnDisplayInternal(uid, displayId);
1966 } finally {
1967 Binder.restoreCallingIdentity(token);
1968 }
1969 }
1970
Michael Wrighteedcbf12017-08-16 23:14:54 +01001971 /**
1972 * Returns the stable device display size, in pixels.
1973 */
1974 @Override // Binder call
1975 public Point getStableDisplaySize() {
1976 final long token = Binder.clearCallingIdentity();
1977 try {
1978 return getStableDisplaySizeInternal();
1979 } finally {
1980 Binder.restoreCallingIdentity(token);
1981 }
1982 }
1983
Jeff Brown4ccb8232014-01-16 22:16:42 -08001984 @Override // Binder call
1985 public void registerCallback(IDisplayManagerCallback callback) {
1986 if (callback == null) {
1987 throw new IllegalArgumentException("listener must not be null");
1988 }
1989
1990 final int callingPid = Binder.getCallingPid();
1991 final long token = Binder.clearCallingIdentity();
1992 try {
1993 registerCallbackInternal(callback, callingPid);
1994 } finally {
1995 Binder.restoreCallingIdentity(token);
1996 }
1997 }
1998
1999 @Override // Binder call
2000 public void startWifiDisplayScan() {
2001 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
2002 "Permission required to start wifi display scans");
2003
2004 final int callingPid = Binder.getCallingPid();
2005 final long token = Binder.clearCallingIdentity();
2006 try {
2007 startWifiDisplayScanInternal(callingPid);
2008 } finally {
2009 Binder.restoreCallingIdentity(token);
2010 }
2011 }
2012
2013 @Override // Binder call
2014 public void stopWifiDisplayScan() {
2015 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
2016 "Permission required to stop wifi display scans");
2017
2018 final int callingPid = Binder.getCallingPid();
2019 final long token = Binder.clearCallingIdentity();
2020 try {
2021 stopWifiDisplayScanInternal(callingPid);
2022 } finally {
2023 Binder.restoreCallingIdentity(token);
2024 }
2025 }
2026
2027 @Override // Binder call
2028 public void connectWifiDisplay(String address) {
2029 if (address == null) {
2030 throw new IllegalArgumentException("address must not be null");
2031 }
2032 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
2033 "Permission required to connect to a wifi display");
2034
2035 final long token = Binder.clearCallingIdentity();
2036 try {
2037 connectWifiDisplayInternal(address);
2038 } finally {
2039 Binder.restoreCallingIdentity(token);
2040 }
2041 }
2042
2043 @Override // Binder call
2044 public void disconnectWifiDisplay() {
2045 // This request does not require special permissions.
2046 // Any app can request disconnection from the currently active wifi display.
2047 // This exception should no longer be needed once wifi display control moves
2048 // to the media router service.
2049
2050 final long token = Binder.clearCallingIdentity();
2051 try {
2052 disconnectWifiDisplayInternal();
2053 } finally {
2054 Binder.restoreCallingIdentity(token);
2055 }
2056 }
2057
2058 @Override // Binder call
2059 public void renameWifiDisplay(String address, String alias) {
2060 if (address == null) {
2061 throw new IllegalArgumentException("address must not be null");
2062 }
2063 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
2064 "Permission required to rename to a wifi display");
2065
2066 final long token = Binder.clearCallingIdentity();
2067 try {
2068 renameWifiDisplayInternal(address, alias);
2069 } finally {
2070 Binder.restoreCallingIdentity(token);
2071 }
2072 }
2073
2074 @Override // Binder call
2075 public void forgetWifiDisplay(String address) {
2076 if (address == null) {
2077 throw new IllegalArgumentException("address must not be null");
2078 }
2079 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
2080 "Permission required to forget to a wifi display");
2081
2082 final long token = Binder.clearCallingIdentity();
2083 try {
2084 forgetWifiDisplayInternal(address);
2085 } finally {
2086 Binder.restoreCallingIdentity(token);
2087 }
2088 }
2089
2090 @Override // Binder call
2091 public void pauseWifiDisplay() {
2092 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
2093 "Permission required to pause a wifi display session");
2094
2095 final long token = Binder.clearCallingIdentity();
2096 try {
2097 pauseWifiDisplayInternal();
2098 } finally {
2099 Binder.restoreCallingIdentity(token);
2100 }
2101 }
2102
2103 @Override // Binder call
2104 public void resumeWifiDisplay() {
2105 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
2106 "Permission required to resume a wifi display session");
2107
2108 final long token = Binder.clearCallingIdentity();
2109 try {
2110 resumeWifiDisplayInternal();
2111 } finally {
2112 Binder.restoreCallingIdentity(token);
2113 }
2114 }
2115
2116 @Override // Binder call
2117 public WifiDisplayStatus getWifiDisplayStatus() {
2118 // This request does not require special permissions.
2119 // Any app can get information about available wifi displays.
2120
2121 final long token = Binder.clearCallingIdentity();
2122 try {
2123 return getWifiDisplayStatusInternal();
2124 } finally {
2125 Binder.restoreCallingIdentity(token);
2126 }
2127 }
2128
2129 @Override // Binder call
Michael Wright1c9977b2016-07-12 13:30:10 -07002130 public void requestColorMode(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +01002131 mContext.enforceCallingOrSelfPermission(
Michael Wright1c9977b2016-07-12 13:30:10 -07002132 Manifest.permission.CONFIGURE_DISPLAY_COLOR_MODE,
2133 "Permission required to change the display color mode");
Michael Wright58e829f2015-09-15 00:13:26 +01002134 final long token = Binder.clearCallingIdentity();
2135 try {
Michael Wright1c9977b2016-07-12 13:30:10 -07002136 requestColorModeInternal(displayId, colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +01002137 } finally {
2138 Binder.restoreCallingIdentity(token);
2139 }
2140 }
2141
2142 @Override // Binder call
b0202.jung925435c2020-01-08 18:46:59 +09002143 public int createVirtualDisplay(VirtualDisplayConfig virtualDisplayConfig,
2144 IVirtualDisplayCallback callback, IMediaProjection projection, String packageName) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08002145 final int callingUid = Binder.getCallingUid();
2146 if (!validatePackageName(callingUid, packageName)) {
2147 throw new SecurityException("packageName must match the calling uid");
2148 }
Michael Wright75ee9fc2014-09-01 19:55:22 -07002149 if (callback == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08002150 throw new IllegalArgumentException("appToken must not be null");
2151 }
b0202.jung925435c2020-01-08 18:46:59 +09002152 if (virtualDisplayConfig == null) {
2153 throw new IllegalArgumentException("virtualDisplayConfig must not be null");
Jeff Brown4ccb8232014-01-16 22:16:42 -08002154 }
b0202.jung925435c2020-01-08 18:46:59 +09002155 final Surface surface = virtualDisplayConfig.getSurface();
2156 int flags = virtualDisplayConfig.getFlags();
2157
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07002158 if (surface != null && surface.isSingleBuffered()) {
Pablo Ceballosaff2f942016-07-29 14:49:55 -07002159 throw new IllegalArgumentException("Surface can't be single-buffered");
2160 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07002161
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002162 if ((flags & VIRTUAL_DISPLAY_FLAG_PUBLIC) != 0) {
2163 flags |= VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
2164
2165 // Public displays can't be allowed to show content when locked.
Andrii Kulian7211d2e2017-01-27 15:58:05 -08002166 if ((flags & VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD) != 0) {
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002167 throw new IllegalArgumentException(
2168 "Public display must not be marked as SHOW_WHEN_LOCKED_INSECURE");
2169 }
Michael Wright6720be42014-07-29 19:14:16 -07002170 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002171 if ((flags & VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY) != 0) {
2172 flags &= ~VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
Michael Wright6720be42014-07-29 19:14:16 -07002173 }
2174
Michael Wrightc39d47a2014-07-08 18:07:36 -07002175 if (projection != null) {
2176 try {
2177 if (!getProjectionService().isValidMediaProjection(projection)) {
2178 throw new SecurityException("Invalid media projection");
2179 }
Michael Wright6720be42014-07-29 19:14:16 -07002180 flags = projection.applyVirtualDisplayFlags(flags);
Michael Wrightc39d47a2014-07-08 18:07:36 -07002181 } catch (RemoteException e) {
Michael Wright6720be42014-07-29 19:14:16 -07002182 throw new SecurityException("unable to validate media projection or flags");
Michael Wrightc39d47a2014-07-08 18:07:36 -07002183 }
2184 }
2185
Michael Wright6720be42014-07-29 19:14:16 -07002186 if (callingUid != Process.SYSTEM_UID &&
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002187 (flags & VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07002188 if (!canProjectVideo(projection)) {
2189 throw new SecurityException("Requires CAPTURE_VIDEO_OUTPUT or "
2190 + "CAPTURE_SECURE_VIDEO_OUTPUT permission, or an appropriate "
2191 + "MediaProjection token in order to create a screen sharing virtual "
2192 + "display.");
2193 }
2194 }
Santos Cordonb6992f22018-01-30 14:51:20 -08002195 if (callingUid != Process.SYSTEM_UID && (flags & VIRTUAL_DISPLAY_FLAG_SECURE) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07002196 if (!canProjectSecureVideo(projection)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08002197 throw new SecurityException("Requires CAPTURE_SECURE_VIDEO_OUTPUT "
Michael Wrightc39d47a2014-07-08 18:07:36 -07002198 + "or an appropriate MediaProjection token to create a "
2199 + "secure virtual display.");
Jeff Brown4ccb8232014-01-16 22:16:42 -08002200 }
2201 }
2202
Charles Chenc947b102020-10-13 01:14:25 +00002203 if (callingUid != Process.SYSTEM_UID && (flags & VIRTUAL_DISPLAY_FLAG_TRUSTED) != 0) {
2204 if (!checkCallingPermission(ADD_TRUSTED_DISPLAY, "createVirtualDisplay()")) {
2205 EventLog.writeEvent(0x534e4554, "162627132", callingUid,
2206 "Attempt to create a trusted display without holding permission!");
2207 throw new SecurityException("Requires ADD_TRUSTED_DISPLAY permission to "
2208 + "create a trusted virtual display.");
2209 }
2210 }
2211
2212 if ((flags & VIRTUAL_DISPLAY_FLAG_TRUSTED) == 0) {
Charles Chenb28fb722020-05-21 17:19:32 +08002213 flags &= ~VIRTUAL_DISPLAY_FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS;
2214 }
2215
Chilun67a379b2019-04-11 19:49:42 +08002216 // Sometimes users can have sensitive information in system decoration windows. An app
2217 // could create a virtual display with system decorations support and read the user info
2218 // from the surface.
2219 // We should only allow adding flag VIRTUAL_DISPLAY_FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS
Charles Chenb28fb722020-05-21 17:19:32 +08002220 // to trusted virtual displays.
2221 final int trustedDisplayWithSysDecorFlag =
2222 (VIRTUAL_DISPLAY_FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS
2223 | VIRTUAL_DISPLAY_FLAG_TRUSTED);
2224 if ((flags & trustedDisplayWithSysDecorFlag)
2225 == VIRTUAL_DISPLAY_FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS
2226 && !checkCallingPermission(INTERNAL_SYSTEM_WINDOW, "createVirtualDisplay()")) {
Chilun67a379b2019-04-11 19:49:42 +08002227 throw new SecurityException("Requires INTERNAL_SYSTEM_WINDOW permission");
Chilun67a379b2019-04-11 19:49:42 +08002228 }
2229
Jeff Brown4ccb8232014-01-16 22:16:42 -08002230 final long token = Binder.clearCallingIdentity();
2231 try {
Santos Cordonee8931e2017-04-05 10:31:15 -07002232 return createVirtualDisplayInternal(callback, projection, callingUid, packageName,
b0202.jung925435c2020-01-08 18:46:59 +09002233 surface, flags, virtualDisplayConfig);
Jeff Brown4ccb8232014-01-16 22:16:42 -08002234 } finally {
2235 Binder.restoreCallingIdentity(token);
2236 }
2237 }
2238
2239 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07002240 public void resizeVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wright01e840f2014-06-26 16:03:25 -07002241 int width, int height, int densityDpi) {
Jeff Chang01baf6c2019-08-20 16:21:25 +08002242 if (width <= 0 || height <= 0 || densityDpi <= 0) {
2243 throw new IllegalArgumentException("width, height, and densityDpi must be "
2244 + "greater than 0");
2245 }
Michael Wright01e840f2014-06-26 16:03:25 -07002246 final long token = Binder.clearCallingIdentity();
2247 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07002248 resizeVirtualDisplayInternal(callback.asBinder(), width, height, densityDpi);
Michael Wright01e840f2014-06-26 16:03:25 -07002249 } finally {
2250 Binder.restoreCallingIdentity(token);
2251 }
2252 }
2253
2254 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07002255 public void setVirtualDisplaySurface(IVirtualDisplayCallback callback, Surface surface) {
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07002256 if (surface != null && surface.isSingleBuffered()) {
2257 throw new IllegalArgumentException("Surface can't be single-buffered");
2258 }
Jeff Brown92207df2014-04-16 13:16:07 -07002259 final long token = Binder.clearCallingIdentity();
2260 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07002261 setVirtualDisplaySurfaceInternal(callback.asBinder(), surface);
Jeff Brown92207df2014-04-16 13:16:07 -07002262 } finally {
2263 Binder.restoreCallingIdentity(token);
2264 }
2265 }
2266
2267 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07002268 public void releaseVirtualDisplay(IVirtualDisplayCallback callback) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08002269 final long token = Binder.clearCallingIdentity();
2270 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07002271 releaseVirtualDisplayInternal(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -08002272 } finally {
2273 Binder.restoreCallingIdentity(token);
2274 }
2275 }
2276
2277 @Override // Binder call
chaviwda4c6942018-11-07 15:52:56 -08002278 public void setVirtualDisplayState(IVirtualDisplayCallback callback, boolean isOn) {
2279 final long token = Binder.clearCallingIdentity();
2280 try {
2281 setVirtualDisplayStateInternal(callback.asBinder(), isOn);
2282 } finally {
2283 Binder.restoreCallingIdentity(token);
2284 }
2285 }
2286
2287 @Override // Binder call
Jeff Brown4ccb8232014-01-16 22:16:42 -08002288 public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06002289 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
Jeff Brown4ccb8232014-01-16 22:16:42 -08002290
2291 final long token = Binder.clearCallingIdentity();
2292 try {
2293 dumpInternal(pw);
2294 } finally {
2295 Binder.restoreCallingIdentity(token);
2296 }
2297 }
2298
Kenny Guy22bd0442017-10-26 00:15:54 +01002299 @Override // Binder call
Kenny Guy29aa30e2017-11-30 13:43:46 +00002300 public ParceledListSlice<BrightnessChangeEvent> getBrightnessEvents(String callingPackage) {
Kenny Guy22bd0442017-10-26 00:15:54 +01002301 mContext.enforceCallingOrSelfPermission(
2302 Manifest.permission.BRIGHTNESS_SLIDER_USAGE,
2303 "Permission to read brightness events.");
Kenny Guy29aa30e2017-11-30 13:43:46 +00002304
2305 final int callingUid = Binder.getCallingUid();
2306 AppOpsManager appOpsManager = mContext.getSystemService(AppOpsManager.class);
Jeff Sharkeyd9311192018-04-16 16:50:34 +00002307 final int mode = appOpsManager.noteOp(AppOpsManager.OP_GET_USAGE_STATS,
Kenny Guy29aa30e2017-11-30 13:43:46 +00002308 callingUid, callingPackage);
2309 final boolean hasUsageStats;
2310 if (mode == AppOpsManager.MODE_DEFAULT) {
2311 // The default behavior here is to check if PackageManager has given the app
2312 // permission.
2313 hasUsageStats = mContext.checkCallingPermission(
2314 Manifest.permission.PACKAGE_USAGE_STATS)
2315 == PackageManager.PERMISSION_GRANTED;
2316 } else {
2317 hasUsageStats = mode == AppOpsManager.MODE_ALLOWED;
2318 }
2319
2320 final int userId = UserHandle.getUserId(callingUid);
Kenny Guy22bd0442017-10-26 00:15:54 +01002321 final long token = Binder.clearCallingIdentity();
2322 try {
Michael Wright144aac92017-12-21 18:37:41 +00002323 synchronized (mSyncRoot) {
2324 return mDisplayPowerController.getBrightnessEvents(userId, hasUsageStats);
2325 }
Kenny Guy22bd0442017-10-26 00:15:54 +01002326 } finally {
2327 Binder.restoreCallingIdentity(token);
2328 }
2329 }
2330
Michael Wrighteef0e132017-11-21 17:57:52 +00002331 @Override // Binder call
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002332 public ParceledListSlice<AmbientBrightnessDayStats> getAmbientBrightnessStats() {
2333 mContext.enforceCallingOrSelfPermission(
2334 Manifest.permission.ACCESS_AMBIENT_LIGHT_STATS,
2335 "Permission required to to access ambient light stats.");
2336 final int callingUid = Binder.getCallingUid();
2337 final int userId = UserHandle.getUserId(callingUid);
2338 final long token = Binder.clearCallingIdentity();
2339 try {
2340 synchronized (mSyncRoot) {
2341 return mDisplayPowerController.getAmbientBrightnessStats(userId);
2342 }
2343 } finally {
2344 Binder.restoreCallingIdentity(token);
2345 }
2346 }
2347
2348 @Override // Binder call
Michael Wrighteef0e132017-11-21 17:57:52 +00002349 public void setBrightnessConfigurationForUser(
Kenny Guy05ce8092018-01-17 13:44:20 +00002350 BrightnessConfiguration c, @UserIdInt int userId, String packageName) {
Michael Wrighteef0e132017-11-21 17:57:52 +00002351 mContext.enforceCallingOrSelfPermission(
2352 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
2353 "Permission required to change the display's brightness configuration");
2354 if (userId != UserHandle.getCallingUserId()) {
2355 mContext.enforceCallingOrSelfPermission(
2356 Manifest.permission.INTERACT_ACROSS_USERS,
2357 "Permission required to change the display brightness"
2358 + " configuration of another user");
2359 }
Kenny Guy05ce8092018-01-17 13:44:20 +00002360 if (packageName != null && !validatePackageName(getCallingUid(), packageName)) {
2361 packageName = null;
2362 }
Michael Wrighteef0e132017-11-21 17:57:52 +00002363 final long token = Binder.clearCallingIdentity();
2364 try {
Kenny Guy05ce8092018-01-17 13:44:20 +00002365 setBrightnessConfigurationForUserInternal(c, userId, packageName);
Michael Wrighteef0e132017-11-21 17:57:52 +00002366 } finally {
2367 Binder.restoreCallingIdentity(token);
2368 }
2369 }
2370
Michael Wrightd8460232018-01-16 18:04:59 +00002371 @Override // Binder call
Kenny Guy6d1009f2018-03-14 14:28:23 +00002372 public BrightnessConfiguration getBrightnessConfigurationForUser(int userId) {
2373 mContext.enforceCallingOrSelfPermission(
2374 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
2375 "Permission required to read the display's brightness configuration");
2376 if (userId != UserHandle.getCallingUserId()) {
2377 mContext.enforceCallingOrSelfPermission(
2378 Manifest.permission.INTERACT_ACROSS_USERS,
2379 "Permission required to read the display brightness"
2380 + " configuration of another user");
2381 }
2382 final long token = Binder.clearCallingIdentity();
2383 try {
2384 final int userSerial = getUserManager().getUserSerialNumber(userId);
2385 synchronized (mSyncRoot) {
2386 BrightnessConfiguration config =
2387 mPersistentDataStore.getBrightnessConfiguration(userSerial);
2388 if (config == null) {
2389 config = mDisplayPowerController.getDefaultBrightnessConfiguration();
2390 }
2391 return config;
2392 }
2393 } finally {
2394 Binder.restoreCallingIdentity(token);
2395 }
2396 }
2397
2398 @Override // Binder call
2399 public BrightnessConfiguration getDefaultBrightnessConfiguration() {
2400 mContext.enforceCallingOrSelfPermission(
2401 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
2402 "Permission required to read the display's default brightness configuration");
2403 final long token = Binder.clearCallingIdentity();
2404 try {
2405 synchronized (mSyncRoot) {
2406 return mDisplayPowerController.getDefaultBrightnessConfiguration();
2407 }
2408 } finally {
2409 Binder.restoreCallingIdentity(token);
2410 }
2411 }
2412
2413 @Override // Binder call
Galia Peycheva35f900a2020-01-09 14:31:05 +01002414 public boolean isMinimalPostProcessingRequested(int displayId) {
2415 synchronized (mSyncRoot) {
2416 return mLogicalDisplays.get(displayId).getRequestedMinimalPostProcessingLocked();
2417 }
2418 }
2419
2420 @Override // Binder call
Fiona Campbelld4eb2952019-11-04 17:19:56 +00002421 public void setTemporaryBrightness(float brightness) {
Michael Wrightd8460232018-01-16 18:04:59 +00002422 mContext.enforceCallingOrSelfPermission(
2423 Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS,
2424 "Permission required to set the display's brightness");
2425 final long token = Binder.clearCallingIdentity();
2426 try {
2427 synchronized (mSyncRoot) {
2428 mDisplayPowerController.setTemporaryBrightness(brightness);
2429 }
2430 } finally {
2431 Binder.restoreCallingIdentity(token);
2432 }
2433 }
2434
2435 @Override // Binder call
2436 public void setTemporaryAutoBrightnessAdjustment(float adjustment) {
2437 mContext.enforceCallingOrSelfPermission(
2438 Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS,
2439 "Permission required to set the display's auto brightness adjustment");
2440 final long token = Binder.clearCallingIdentity();
2441 try {
2442 synchronized (mSyncRoot) {
2443 mDisplayPowerController.setTemporaryAutoBrightnessAdjustment(adjustment);
2444 }
2445 } finally {
2446 Binder.restoreCallingIdentity(token);
2447 }
2448 }
2449
Dan Gittik7a32fba2018-03-28 12:19:38 +01002450 @Override // Binder call
2451 public void onShellCommand(FileDescriptor in, FileDescriptor out,
2452 FileDescriptor err, String[] args, ShellCallback callback,
2453 ResultReceiver resultReceiver) {
Santos Cordonc4fd8e62019-12-18 13:01:05 +00002454 new DisplayManagerShellCommand(DisplayManagerService.this).exec(this, in, out, err,
2455 args, callback, resultReceiver);
Dan Gittik7a32fba2018-03-28 12:19:38 +01002456 }
2457
Dan Gittik122df862018-03-28 16:59:22 +01002458 @Override // Binder call
2459 public Curve getMinimumBrightnessCurve() {
2460 final long token = Binder.clearCallingIdentity();
2461 try {
2462 return getMinimumBrightnessCurveInternal();
2463 } finally {
2464 Binder.restoreCallingIdentity(token);
2465 }
2466 }
2467
Peiyong Lin277eaff2019-01-16 16:18:22 -08002468 @Override // Binder call
2469 public int getPreferredWideGamutColorSpaceId() {
2470 final long token = Binder.clearCallingIdentity();
2471 try {
2472 return getPreferredWideGamutColorSpaceIdInternal();
2473 } finally {
2474 Binder.restoreCallingIdentity(token);
2475 }
2476 }
2477
Jeff Brown4ccb8232014-01-16 22:16:42 -08002478 private boolean validatePackageName(int uid, String packageName) {
2479 if (packageName != null) {
2480 String[] packageNames = mContext.getPackageManager().getPackagesForUid(uid);
2481 if (packageNames != null) {
2482 for (String n : packageNames) {
2483 if (n.equals(packageName)) {
2484 return true;
2485 }
2486 }
2487 }
2488 }
2489 return false;
2490 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07002491
2492 private boolean canProjectVideo(IMediaProjection projection) {
2493 if (projection != null) {
2494 try {
2495 if (projection.canProjectVideo()) {
2496 return true;
2497 }
2498 } catch (RemoteException e) {
2499 Slog.e(TAG, "Unable to query projection service for permissions", e);
2500 }
2501 }
Chilun67a379b2019-04-11 19:49:42 +08002502 if (checkCallingPermission(CAPTURE_VIDEO_OUTPUT, "canProjectVideo()")) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07002503 return true;
2504 }
2505 return canProjectSecureVideo(projection);
2506 }
2507
2508 private boolean canProjectSecureVideo(IMediaProjection projection) {
2509 if (projection != null) {
2510 try {
2511 if (projection.canProjectSecureVideo()){
2512 return true;
2513 }
2514 } catch (RemoteException e) {
2515 Slog.e(TAG, "Unable to query projection service for permissions", e);
2516 }
2517 }
Chilun67a379b2019-04-11 19:49:42 +08002518 return checkCallingPermission(CAPTURE_SECURE_VIDEO_OUTPUT, "canProjectSecureVideo()");
2519 }
2520
2521 private boolean checkCallingPermission(String permission, String func) {
2522 if (mContext.checkCallingPermission(permission) == PackageManager.PERMISSION_GRANTED) {
2523 return true;
2524 }
2525 final String msg = "Permission Denial: " + func + " from pid=" + Binder.getCallingPid()
2526 + ", uid=" + Binder.getCallingUid() + " requires " + permission;
2527 Slog.w(TAG, msg);
2528 return false;
Michael Wrightc39d47a2014-07-08 18:07:36 -07002529 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08002530 }
2531
2532 private final class LocalService extends DisplayManagerInternal {
Galia Peycheva056b3ee2019-06-26 14:05:12 +02002533
Jeff Brown4ccb8232014-01-16 22:16:42 -08002534 @Override
Jeff Brown037c33e2014-04-09 00:31:55 -07002535 public void initPowerManagement(final DisplayPowerCallbacks callbacks, Handler handler,
Jeff Brownad9ef192014-04-08 17:26:30 -07002536 SensorManager sensorManager) {
2537 synchronized (mSyncRoot) {
Jeff Brown037c33e2014-04-09 00:31:55 -07002538 DisplayBlanker blanker = new DisplayBlanker() {
2539 @Override
Fiona Campbelld4eb2952019-11-04 17:19:56 +00002540 public void requestDisplayState(int state, float brightness) {
Jeff Brown037c33e2014-04-09 00:31:55 -07002541 // The order of operations is important for legacy reasons.
2542 if (state == Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07002543 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07002544 }
2545
2546 callbacks.onDisplayStateChange(state);
2547
2548 if (state != Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07002549 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07002550 }
2551 }
2552 };
Jeff Brownad9ef192014-04-08 17:26:30 -07002553 mDisplayPowerController = new DisplayPowerController(
Fiona Campbell172fd4a2020-03-13 16:34:30 +00002554 mContext, callbacks, handler, sensorManager, blanker,
2555 mDisplayDevices.get(Display.DEFAULT_DISPLAY));
Long Lingbc841b02019-07-03 16:43:15 -07002556 mSensorManager = sensorManager;
Jeff Brownad9ef192014-04-08 17:26:30 -07002557 }
Michael Wrighteef0e132017-11-21 17:57:52 +00002558
2559 mHandler.sendEmptyMessage(MSG_LOAD_BRIGHTNESS_CONFIGURATION);
Jeff Brownad9ef192014-04-08 17:26:30 -07002560 }
2561
2562 @Override
2563 public boolean requestPowerState(DisplayPowerRequest request,
2564 boolean waitForNegativeProximity) {
Michael Wrighteef0e132017-11-21 17:57:52 +00002565 synchronized (mSyncRoot) {
Santos Cordon31c24e02020-07-29 11:54:23 +01002566 return mDisplayPowerController.requestPowerState(request, waitForNegativeProximity);
Michael Wrighteef0e132017-11-21 17:57:52 +00002567 }
Jeff Brownad9ef192014-04-08 17:26:30 -07002568 }
2569
2570 @Override
2571 public boolean isProximitySensorAvailable() {
Michael Wrighteef0e132017-11-21 17:57:52 +00002572 synchronized (mSyncRoot) {
2573 return mDisplayPowerController.isProximitySensorAvailable();
2574 }
Jeff Brownad9ef192014-04-08 17:26:30 -07002575 }
2576
2577 @Override
Jacky Kao333de4e2020-04-07 15:01:41 +08002578 public SurfaceControl.ScreenshotGraphicBuffer systemScreenshot(int displayId) {
2579 return systemScreenshotInternal(displayId);
Jacky Kao316477c2020-03-23 16:30:04 +08002580 }
2581
2582 @Override
Jacky Kao333de4e2020-04-07 15:01:41 +08002583 public SurfaceControl.ScreenshotGraphicBuffer userScreenshot(int displayId) {
2584 return userScreenshotInternal(displayId);
Riddle Hsu654a6f92018-07-13 22:59:36 +08002585 }
2586
2587 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08002588 public DisplayInfo getDisplayInfo(int displayId) {
2589 return getDisplayInfoInternal(displayId, Process.myUid());
2590 }
2591
2592 @Override
chaviw945f7302020-02-24 14:35:33 -08002593 public Point getDisplayPosition(int displayId) {
2594 synchronized (mSyncRoot) {
2595 LogicalDisplay display = mLogicalDisplays.get(displayId);
2596 if (display != null) {
2597 return display.getDisplayPosition();
2598 }
2599 return null;
2600 }
2601 }
2602
2603 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08002604 public void registerDisplayTransactionListener(DisplayTransactionListener listener) {
2605 if (listener == null) {
2606 throw new IllegalArgumentException("listener must not be null");
2607 }
2608
2609 registerDisplayTransactionListenerInternal(listener);
2610 }
2611
2612 @Override
2613 public void unregisterDisplayTransactionListener(DisplayTransactionListener listener) {
2614 if (listener == null) {
2615 throw new IllegalArgumentException("listener must not be null");
2616 }
2617
2618 unregisterDisplayTransactionListenerInternal(listener);
2619 }
2620
2621 @Override
2622 public void setDisplayInfoOverrideFromWindowManager(int displayId, DisplayInfo info) {
2623 setDisplayInfoOverrideFromWindowManagerInternal(displayId, info);
2624 }
2625
2626 @Override
Andrii Kuliancd097992017-03-23 18:31:59 -07002627 public void getNonOverrideDisplayInfo(int displayId, DisplayInfo outInfo) {
2628 getNonOverrideDisplayInfoInternal(displayId, outInfo);
2629 }
2630
2631 @Override
Robert Carrae606b42018-02-15 15:36:23 -08002632 public void performTraversal(SurfaceControl.Transaction t) {
2633 performTraversalInternal(t);
Jeff Brown4ccb8232014-01-16 22:16:42 -08002634 }
2635
2636 @Override
Michael Wright3f145a22014-07-22 19:46:03 -07002637 public void setDisplayProperties(int displayId, boolean hasContent,
Galia Peycheva056b3ee2019-06-26 14:05:12 +02002638 float requestedRefreshRate, int requestedMode,
2639 boolean requestedMinimalPostProcessing, boolean inTraversal) {
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07002640 setDisplayPropertiesInternal(displayId, hasContent, requestedRefreshRate,
Galia Peycheva056b3ee2019-06-26 14:05:12 +02002641 requestedMode, requestedMinimalPostProcessing, inTraversal);
Jeff Brown4ccb8232014-01-16 22:16:42 -08002642 }
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08002643
2644 @Override
2645 public void setDisplayOffsets(int displayId, int x, int y) {
2646 setDisplayOffsetsInternal(displayId, x, y);
2647 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002648
2649 @Override
Sam Lin4c3ac2b2019-02-18 04:50:26 -08002650 public void setDisplayScalingDisabled(int displayId, boolean disableScaling) {
2651 setDisplayScalingDisabledInternal(displayId, disableScaling);
2652 }
2653
2654 @Override
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002655 public void setDisplayAccessUIDs(SparseArray<IntArray> newDisplayAccessUIDs) {
2656 setDisplayAccessUIDsInternal(newDisplayAccessUIDs);
2657 }
2658
2659 @Override
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002660 public void persistBrightnessTrackerState() {
Michael Wright144aac92017-12-21 18:37:41 +00002661 synchronized (mSyncRoot) {
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002662 mDisplayPowerController.persistBrightnessTrackerState();
Michael Wright144aac92017-12-21 18:37:41 +00002663 }
Kenny Guycfe7b702017-11-14 21:04:58 +00002664 }
Adrian Roose1d68cd2018-01-17 12:54:50 +01002665
2666 @Override
2667 public void onOverlayChanged() {
2668 synchronized (mSyncRoot) {
Adrian Roos898ec382018-01-17 12:54:50 +01002669 for (int i = 0; i < mDisplayDevices.size(); i++) {
2670 mDisplayDevices.get(i).onOverlayChangedLocked();
Adrian Roose1d68cd2018-01-17 12:54:50 +01002671 }
2672 }
2673 }
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07002674
2675 @Override
2676 public DisplayedContentSamplingAttributes getDisplayedContentSamplingAttributes(
2677 int displayId) {
2678 return getDisplayedContentSamplingAttributesInternal(displayId);
2679 }
2680
2681 @Override
2682 public boolean setDisplayedContentSamplingEnabled(
2683 int displayId, boolean enable, int componentMask, int maxFrames) {
2684 return setDisplayedContentSamplingEnabledInternal(
2685 displayId, enable, componentMask, maxFrames);
2686 }
2687
2688 @Override
2689 public DisplayedContentSample getDisplayedContentSample(int displayId,
2690 long maxFrames, long timestamp) {
2691 return getDisplayedContentSampleInternal(displayId, maxFrames, timestamp);
2692 }
2693
Santos Cordon31c24e02020-07-29 11:54:23 +01002694 @Override
2695 public void ignoreProximitySensorUntilChanged() {
2696 mDisplayPowerController.ignoreProximitySensorUntilChanged();
2697 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08002698 }
Michael Wrighta3dab232019-02-22 16:54:21 +00002699
Ana Kruleca74a8642019-11-14 00:51:00 +01002700 class DesiredDisplayModeSpecsObserver
2701 implements DisplayModeDirector.DesiredDisplayModeSpecsListener {
2702 public void onDesiredDisplayModeSpecsChanged() {
2703 onDesiredDisplayModeSpecsChangedInternal();
Michael Wrighta3dab232019-02-22 16:54:21 +00002704 }
2705 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07002706}