blob: dee6cd02917f98bed4cfe53fc9622bce42e47db2 [file] [log] [blame]
Jeff Brownfa25bf52012-07-23 19:26:30 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.display;
18
Chilun67a379b2019-04-11 19:49:42 +080019import static android.Manifest.permission.CAPTURE_SECURE_VIDEO_OUTPUT;
20import static android.Manifest.permission.CAPTURE_VIDEO_OUTPUT;
21import static android.Manifest.permission.INTERNAL_SYSTEM_WINDOW;
Andrii Kulianfc8f82b2017-01-26 13:17:27 -080022import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
Chilun67a379b2019-04-11 19:49:42 +080023import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD;
Andrii Kulianfc8f82b2017-01-26 13:17:27 -080024import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY;
25import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;
26import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_SECURE;
Chilun67a379b2019-04-11 19:49:42 +080027import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +010028import static android.hardware.display.DisplayViewport.VIEWPORT_EXTERNAL;
29import static android.hardware.display.DisplayViewport.VIEWPORT_INTERNAL;
30import static android.hardware.display.DisplayViewport.VIEWPORT_VIRTUAL;
Jacky Kao333de4e2020-04-07 15:01:41 +080031import static android.view.Surface.ROTATION_270;
32import static android.view.Surface.ROTATION_90;
Jeff Brownbd6e1502012-08-28 03:27:37 -070033
Jeff Brownfa25bf52012-07-23 19:26:30 -070034import android.Manifest;
Santos Cordonee8931e2017-04-05 10:31:15 -070035import android.annotation.NonNull;
Kenny Guy05ce8092018-01-17 13:44:20 +000036import android.annotation.Nullable;
Michael Wrighteef0e132017-11-21 17:57:52 +000037import android.annotation.UserIdInt;
Kenny Guy29aa30e2017-11-30 13:43:46 +000038import android.app.AppOpsManager;
Jeff Brownfa25bf52012-07-23 19:26:30 -070039import android.content.Context;
40import android.content.pm.PackageManager;
Kenny Guy22bd0442017-10-26 00:15:54 +010041import android.content.pm.ParceledListSlice;
Michael Wrighteedcbf12017-08-16 23:14:54 +010042import android.content.res.Resources;
Dan Gittik122df862018-03-28 16:59:22 +010043import android.content.res.TypedArray;
Galia Peycheva088d7c02019-12-13 11:27:23 +010044import android.database.ContentObserver;
Peiyong Lin277eaff2019-01-16 16:18:22 -080045import android.graphics.ColorSpace;
Michael Wrighteedcbf12017-08-16 23:14:54 +010046import android.graphics.Point;
Robert Carr5c52b132019-02-15 15:48:11 -080047import android.graphics.Rect;
Jeff Brownad9ef192014-04-08 17:26:30 -070048import android.hardware.SensorManager;
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +000049import android.hardware.display.AmbientBrightnessDayStats;
Kenny Guy22bd0442017-10-26 00:15:54 +010050import android.hardware.display.BrightnessChangeEvent;
Michael Wrighteef0e132017-11-21 17:57:52 +000051import android.hardware.display.BrightnessConfiguration;
Dan Gittik122df862018-03-28 16:59:22 +010052import android.hardware.display.Curve;
Jeff Brownbd6e1502012-08-28 03:27:37 -070053import android.hardware.display.DisplayManagerGlobal;
Jeff Brown4ccb8232014-01-16 22:16:42 -080054import android.hardware.display.DisplayManagerInternal;
Jeff Brown4ccb8232014-01-16 22:16:42 -080055import android.hardware.display.DisplayManagerInternal.DisplayTransactionListener;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +010056import android.hardware.display.DisplayViewport;
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -070057import android.hardware.display.DisplayedContentSample;
58import android.hardware.display.DisplayedContentSamplingAttributes;
Jeff Brownfa25bf52012-07-23 19:26:30 -070059import android.hardware.display.IDisplayManager;
Jeff Brownbd6e1502012-08-28 03:27:37 -070060import android.hardware.display.IDisplayManagerCallback;
Michael Wright75ee9fc2014-09-01 19:55:22 -070061import android.hardware.display.IVirtualDisplayCallback;
b0202.jung925435c2020-01-08 18:46:59 +090062import android.hardware.display.VirtualDisplayConfig;
Jeff Browne08ae382012-09-07 20:36:36 -070063import android.hardware.display.WifiDisplayStatus;
Jeff Brown4ccb8232014-01-16 22:16:42 -080064import android.hardware.input.InputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -070065import android.media.projection.IMediaProjection;
66import android.media.projection.IMediaProjectionManager;
Galia Peycheva088d7c02019-12-13 11:27:23 +010067import android.net.Uri;
Jeff Brownfa25bf52012-07-23 19:26:30 -070068import android.os.Binder;
Jeff Brownbd6e1502012-08-28 03:27:37 -070069import android.os.Handler;
Jeff Brown64a55af2012-08-26 02:47:39 -070070import android.os.IBinder;
Jeff Brown4ccb8232014-01-16 22:16:42 -080071import android.os.IBinder.DeathRecipient;
Jeff Brownbd6e1502012-08-28 03:27:37 -070072import android.os.Looper;
73import android.os.Message;
Jeff Brown5d6443b2015-04-10 20:15:01 -070074import android.os.PowerManager;
Craig Mautner4504de52013-12-20 09:06:56 -080075import android.os.Process;
Jeff Brownbd6e1502012-08-28 03:27:37 -070076import android.os.RemoteException;
Dan Gittik7a32fba2018-03-28 12:19:38 +010077import android.os.ResultReceiver;
Michael Wrightc39d47a2014-07-08 18:07:36 -070078import android.os.ServiceManager;
Dan Gittik7a32fba2018-03-28 12:19:38 +010079import android.os.ShellCallback;
Jeff Brownbd6e1502012-08-28 03:27:37 -070080import android.os.SystemClock;
Jeff Brownfa25bf52012-07-23 19:26:30 -070081import android.os.SystemProperties;
Jeff Brown5d6443b2015-04-10 20:15:01 -070082import android.os.Trace;
Kenny Guy22bd0442017-10-26 00:15:54 +010083import android.os.UserHandle;
Michael Wrighteef0e132017-11-21 17:57:52 +000084import android.os.UserManager;
Galia Peycheva088d7c02019-12-13 11:27:23 +010085import android.provider.Settings;
Jeff Browna506a6e2013-06-04 00:02:38 -070086import android.text.TextUtils;
Andrii Kulianfb1bf692017-01-17 11:17:34 -080087import android.util.IntArray;
Dan Gittik122df862018-03-28 16:59:22 +010088import android.util.Pair;
Jeff Brownbd6e1502012-08-28 03:27:37 -070089import android.util.Slog;
90import android.util.SparseArray;
Dan Gittik122df862018-03-28 16:59:22 +010091import android.util.Spline;
Jeff Brownfa25bf52012-07-23 19:26:30 -070092import android.view.Display;
93import android.view.DisplayInfo;
Jeff Browna506a6e2013-06-04 00:02:38 -070094import android.view.Surface;
Robert Carrae606b42018-02-15 15:36:23 -080095import android.view.SurfaceControl;
Jeff Browna506a6e2013-06-04 00:02:38 -070096
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +010097import com.android.internal.annotations.GuardedBy;
98import com.android.internal.annotations.VisibleForTesting;
99import com.android.internal.util.DumpUtils;
100import com.android.internal.util.IndentingPrintWriter;
Jorim Jaggied7993b2017-03-28 18:50:01 +0100101import com.android.server.AnimationThread;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800102import com.android.server.DisplayThread;
103import com.android.server.LocalServices;
104import com.android.server.SystemService;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700105import com.android.server.UiThread;
Jorim Jaggi21c39a72017-10-20 15:47:51 +0200106import com.android.server.wm.SurfaceAnimationThread;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100107import com.android.server.wm.WindowManagerInternal;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700108
109import java.io.FileDescriptor;
110import java.io.PrintWriter;
111import java.util.ArrayList;
Jeff Browna506a6e2013-06-04 00:02:38 -0700112import java.util.Arrays;
Jeff Browne75926d2014-09-18 15:24:49 -0700113import java.util.List;
Chris Yed6d35592020-05-01 11:36:33 -0700114import java.util.Optional;
Jeff Brown7f3994e2012-12-04 14:04:28 -0800115import java.util.concurrent.CopyOnWriteArrayList;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700116
117/**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700118 * Manages attached displays.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700119 * <p>
Jeff Brownbd6e1502012-08-28 03:27:37 -0700120 * The {@link DisplayManagerService} manages the global lifecycle of displays,
121 * decides how to configure logical displays based on the physical display devices currently
122 * attached, sends notifications to the system and to applications when the state
123 * changes, and so on.
124 * </p><p>
125 * The display manager service relies on a collection of {@link DisplayAdapter} components,
126 * for discovering and configuring physical display devices attached to the system.
127 * There are separate display adapters for each manner that devices are attached:
Dominik Laskowski26290bb2020-01-15 16:09:55 -0800128 * one display adapter for physical displays, one for simulated non-functional
Jeff Brownbd6e1502012-08-28 03:27:37 -0700129 * displays when the system is headless, one for simulated overlay displays used for
130 * development, one for wifi displays, etc.
131 * </p><p>
132 * Display adapters are only weakly coupled to the display manager service.
133 * Display adapters communicate changes in display device state to the display manager
Craig Mautner722285e2012-09-07 13:55:58 -0700134 * service asynchronously via a {@link DisplayAdapter.Listener} registered
Jeff Brownbd6e1502012-08-28 03:27:37 -0700135 * by the display manager service. This separation of concerns is important for
136 * two main reasons. First, it neatly encapsulates the responsibilities of these
137 * two classes: display adapters handle individual display devices whereas
138 * the display manager service handles the global state. Second, it eliminates
139 * the potential for deadlocks resulting from asynchronous display device discovery.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700140 * </p>
141 *
142 * <h3>Synchronization</h3>
143 * <p>
144 * Because the display manager may be accessed by multiple threads, the synchronization
145 * story gets a little complicated. In particular, the window manager may call into
146 * the display manager while holding a surface transaction with the expectation that
147 * it can apply changes immediately. Unfortunately, that means we can't just do
148 * everything asynchronously (*grump*).
Jeff Brownbd6e1502012-08-28 03:27:37 -0700149 * </p><p>
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700150 * To make this work, all of the objects that belong to the display manager must
151 * use the same lock. We call this lock the synchronization root and it has a unique
152 * type {@link DisplayManagerService.SyncRoot}. Methods that require this lock are
153 * named with the "Locked" suffix.
154 * </p><p>
155 * Where things get tricky is that the display manager is not allowed to make
156 * any potentially reentrant calls, especially into the window manager. We generally
157 * avoid this by making all potentially reentrant out-calls asynchronous.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700158 * </p>
159 */
Jeff Brown4ccb8232014-01-16 22:16:42 -0800160public final class DisplayManagerService extends SystemService {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700161 private static final String TAG = "DisplayManagerService";
Jeff Brownbd6e1502012-08-28 03:27:37 -0700162 private static final boolean DEBUG = false;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700163
Jeff Brownbbd28a22012-09-20 16:47:15 -0700164 // When this system property is set to 0, WFD is forcibly disabled on boot.
165 // When this system property is set to 1, WFD is forcibly enabled on boot.
166 // Otherwise WFD is enabled according to the value of config_enableWifiDisplay.
167 private static final String FORCE_WIFI_DISPLAY_ENABLE = "persist.debug.wfd.enable";
168
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200169 private static final String PROP_DEFAULT_DISPLAY_TOP_INSET = "persist.sys.displayinset.top";
170
Jeff Brownbd6e1502012-08-28 03:27:37 -0700171 private static final long WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT = 10000;
172
Santos Cordonc22c5632017-06-21 16:03:49 -0700173 private static final int MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS = 1;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700174 private static final int MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS = 2;
175 private static final int MSG_DELIVER_DISPLAY_EVENT = 3;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700176 private static final int MSG_REQUEST_TRAVERSAL = 4;
Jeff Brownd728bf52012-09-08 18:05:28 -0700177 private static final int MSG_UPDATE_VIEWPORT = 5;
Michael Wrighta3dab232019-02-22 16:54:21 +0000178 private static final int MSG_LOAD_BRIGHTNESS_CONFIGURATION = 6;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700179
Jeff Brownb880d882014-02-10 19:47:07 -0800180 private final Context mContext;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700181 private final DisplayManagerHandler mHandler;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700182 private final Handler mUiHandler;
183 private final DisplayAdapterListener mDisplayAdapterListener;
Michael Wrighta3dab232019-02-22 16:54:21 +0000184 private final DisplayModeDirector mDisplayModeDirector;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800185 private WindowManagerInternal mWindowManagerInternal;
186 private InputManagerInternal mInputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -0700187 private IMediaProjectionManager mProjectionService;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700188
189 // The synchronization root for the display manager.
190 // This lock guards most of the display manager's state.
Craig Mautner0bf6ec92012-12-18 08:33:27 -0800191 // NOTE: This is synchronized on while holding WindowManagerService.mWindowMap so never call
192 // into WindowManagerService methods that require mWindowMap while holding this unless you are
193 // very very sure that no deadlock can occur.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700194 private final SyncRoot mSyncRoot = new SyncRoot();
195
196 // True if in safe mode.
197 // This option may disable certain display adapters.
198 public boolean mSafeMode;
199
200 // True if we are in a special boot mode where only core applications and
201 // services should be started. This option may disable certain display adapters.
202 public boolean mOnlyCore;
203
Jeff Brown27f1d672012-10-17 18:32:34 -0700204 // True if the display manager service should pretend there is only one display
205 // and only tell applications about the existence of the default logical display.
206 // The display manager can still mirror content to secondary displays but applications
207 // cannot present unique content on those displays.
208 // Used for demonstration purposes only.
209 private final boolean mSingleDisplayDemoMode;
210
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700211 // All callback records indexed by calling process id.
212 public final SparseArray<CallbackRecord> mCallbacks =
Jeff Brownbd6e1502012-08-28 03:27:37 -0700213 new SparseArray<CallbackRecord>();
Jeff Brownfa25bf52012-07-23 19:26:30 -0700214
Jeff Brownbd6e1502012-08-28 03:27:37 -0700215 // List of all currently registered display adapters.
216 private final ArrayList<DisplayAdapter> mDisplayAdapters = new ArrayList<DisplayAdapter>();
217
218 // List of all currently connected display devices.
219 private final ArrayList<DisplayDevice> mDisplayDevices = new ArrayList<DisplayDevice>();
220
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700221 // List of all logical displays indexed by logical display id.
Tim Murray890ceb52020-01-30 10:12:40 -0800222 // Any modification to mLogicalDisplays must invalidate the DisplayManagerGlobal cache.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700223 private final SparseArray<LogicalDisplay> mLogicalDisplays =
224 new SparseArray<LogicalDisplay>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700225 private int mNextNonDefaultDisplayId = Display.DEFAULT_DISPLAY + 1;
226
Jeff Brown7f3994e2012-12-04 14:04:28 -0800227 // List of all display transaction listeners.
228 private final CopyOnWriteArrayList<DisplayTransactionListener> mDisplayTransactionListeners =
229 new CopyOnWriteArrayList<DisplayTransactionListener>();
230
Jeff Brownad9ef192014-04-08 17:26:30 -0700231 // Display power controller.
232 private DisplayPowerController mDisplayPowerController;
233
Jeff Brown037c33e2014-04-09 00:31:55 -0700234 // The overall display state, independent of changes that might influence one
235 // display or another in particular.
Jeff Brown5d6443b2015-04-10 20:15:01 -0700236 private int mGlobalDisplayState = Display.STATE_ON;
237
238 // The overall display brightness.
Dominik Laskowski26290bb2020-01-15 16:09:55 -0800239 // For now, this only applies to the default display but we may split it up eventually.
Fiona Campbelld4eb2952019-11-04 17:19:56 +0000240 private float mGlobalDisplayBrightness;
Jeff Brown9e316a12012-10-08 19:17:06 -0700241
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700242 // Set to true when there are pending display changes that have yet to be applied
243 // to the surface flinger state.
244 private boolean mPendingTraversal;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700245
Jeff Browne08ae382012-09-07 20:36:36 -0700246 // The Wifi display adapter, or null if not registered.
247 private WifiDisplayAdapter mWifiDisplayAdapter;
248
Jeff Brownce468a32013-11-21 16:42:03 -0800249 // The number of active wifi display scan requests.
250 private int mWifiDisplayScanRequestCount;
251
Jeff Browna506a6e2013-06-04 00:02:38 -0700252 // The virtual display adapter, or null if not registered.
253 private VirtualDisplayAdapter mVirtualDisplayAdapter;
254
Michael Wrighteef0e132017-11-21 17:57:52 +0000255 // The User ID of the current user
256 private @UserIdInt int mCurrentUserId;
257
Michael Wrighteedcbf12017-08-16 23:14:54 +0100258 // The stable device screen height and width. These are not tied to a specific display, even
259 // the default display, because they need to be stable over the course of the device's entire
260 // life, even if the default display changes (e.g. a new monitor is plugged into a PC-like
261 // device).
262 private Point mStableDisplaySize = new Point();
263
Winson72dbe7e2019-04-29 14:55:30 -0700264 // Whether the system has finished booting or not.
265 private boolean mSystemReady;
266
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200267 // The top inset of the default display.
268 // This gets persisted so that the boot animation knows how to transition from the display's
269 // full size to the size configured by the user. Right now we only persist and animate the top
270 // inset, but theoretically we could do it for all of them.
271 private int mDefaultDisplayTopInset;
272
Jeff Brownd728bf52012-09-08 18:05:28 -0700273 // Viewports of the default display and the display that should receive touch
274 // input from an external source. Used by the input system.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100275 @GuardedBy("mSyncRoot")
276 private final ArrayList<DisplayViewport> mViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700277
Jeff Brown89d55462012-09-19 11:33:42 -0700278 // Persistent data store for all internal settings maintained by the display manager service.
279 private final PersistentDataStore mPersistentDataStore = new PersistentDataStore();
280
Jeff Brownbd6e1502012-08-28 03:27:37 -0700281 // Temporary callback list, used when sending display events to applications.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700282 // May be used outside of the lock but only on the handler thread.
283 private final ArrayList<CallbackRecord> mTempCallbacks = new ArrayList<CallbackRecord>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700284
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700285 // Temporary display info, used for comparing display configurations.
286 private final DisplayInfo mTempDisplayInfo = new DisplayInfo();
287
Jeff Brownd728bf52012-09-08 18:05:28 -0700288 // Temporary viewports, used when sending new viewport information to the
289 // input system. May be used outside of the lock but only on the handler thread.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100290 private final ArrayList<DisplayViewport> mTempViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700291
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700292 // The default color mode for default displays. Overrides the usual
293 // Display.Display.COLOR_MODE_DEFAULT for displays with the
294 // DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY flag set.
295 private final int mDefaultDisplayDefaultColorMode;
296
Jeff Browne75926d2014-09-18 15:24:49 -0700297 // Temporary list of deferred work to perform when setting the display state.
298 // Only used by requestDisplayState. The field is self-synchronized and only
299 // intended for use inside of the requestGlobalDisplayStateInternal function.
300 private final ArrayList<Runnable> mTempDisplayStateWorkQueue = new ArrayList<Runnable>();
301
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800302 // Lists of UIDs that are present on the displays. Maps displayId -> array of UIDs.
303 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
304
Santos Cordonee8931e2017-04-05 10:31:15 -0700305 private final Injector mInjector;
306
Dan Gittik122df862018-03-28 16:59:22 +0100307 // The minimum brightness curve, which guarantess that any brightness curve that dips below it
308 // is rejected by the system.
309 private final Curve mMinimumBrightnessCurve;
310 private final Spline mMinimumBrightnessSpline;
Peiyong Lin277eaff2019-01-16 16:18:22 -0800311 private final ColorSpace mWideColorSpace;
Dan Gittik122df862018-03-28 16:59:22 +0100312
Long Lingbc841b02019-07-03 16:43:15 -0700313 private SensorManager mSensorManager;
314
Galia Peycheva088d7c02019-12-13 11:27:23 +0100315 // Whether minimal post processing is allowed by the user.
316 @GuardedBy("mSyncRoot")
317 private boolean mMinimalPostProcessingAllowed;
318
319 // Receives notifications about changes to Settings.
320 private SettingsObserver mSettingsObserver;
321
Jeff Brownb880d882014-02-10 19:47:07 -0800322 public DisplayManagerService(Context context) {
Santos Cordonee8931e2017-04-05 10:31:15 -0700323 this(context, new Injector());
324 }
325
326 @VisibleForTesting
327 DisplayManagerService(Context context, Injector injector) {
Jeff Brownb880d882014-02-10 19:47:07 -0800328 super(context);
Santos Cordonee8931e2017-04-05 10:31:15 -0700329 mInjector = injector;
Jeff Brownb880d882014-02-10 19:47:07 -0800330 mContext = context;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800331 mHandler = new DisplayManagerHandler(DisplayThread.get().getLooper());
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700332 mUiHandler = UiThread.getHandler();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700333 mDisplayAdapterListener = new DisplayAdapterListener();
Michael Wrighta3dab232019-02-22 16:54:21 +0000334 mDisplayModeDirector = new DisplayModeDirector(context, mHandler);
Jeff Brown27f1d672012-10-17 18:32:34 -0700335 mSingleDisplayDemoMode = SystemProperties.getBoolean("persist.demo.singledisplay", false);
Dan Gittik122df862018-03-28 16:59:22 +0100336 Resources resources = mContext.getResources();
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700337 mDefaultDisplayDefaultColorMode = mContext.getResources().getInteger(
Michael Wrighteef0e132017-11-21 17:57:52 +0000338 com.android.internal.R.integer.config_defaultDisplayDefaultColorMode);
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200339 mDefaultDisplayTopInset = SystemProperties.getInt(PROP_DEFAULT_DISPLAY_TOP_INSET, -1);
Dan Gittik122df862018-03-28 16:59:22 +0100340 float[] lux = getFloatArray(resources.obtainTypedArray(
341 com.android.internal.R.array.config_minimumBrightnessCurveLux));
342 float[] nits = getFloatArray(resources.obtainTypedArray(
343 com.android.internal.R.array.config_minimumBrightnessCurveNits));
344 mMinimumBrightnessCurve = new Curve(lux, nits);
345 mMinimumBrightnessSpline = Spline.createSpline(lux, nits);
Jeff Brown5d6443b2015-04-10 20:15:01 -0700346
Michael Wrighta3dab232019-02-22 16:54:21 +0000347 PowerManager pm = mContext.getSystemService(PowerManager.class);
Fiona Campbelld4eb2952019-11-04 17:19:56 +0000348 mGlobalDisplayBrightness = pm.getBrightnessConstraint(
349 PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_DEFAULT);
Michael Wrighteef0e132017-11-21 17:57:52 +0000350 mCurrentUserId = UserHandle.USER_SYSTEM;
Peiyong Lin277eaff2019-01-16 16:18:22 -0800351 ColorSpace[] colorSpaces = SurfaceControl.getCompositionColorSpaces();
352 mWideColorSpace = colorSpaces[1];
Winson72dbe7e2019-04-29 14:55:30 -0700353
354 mSystemReady = false;
Joel Fernandes2d314e12017-04-04 16:32:15 -0700355 }
356
357 public void setupSchedulerPolicies() {
Jorim Jaggied7993b2017-03-28 18:50:01 +0100358 // android.display and android.anim is critical to user experience and we should make sure
Michael Wrighteef0e132017-11-21 17:57:52 +0000359 // it is not in the default foregroup groups, add it to top-app to make sure it uses all
360 // the cores and scheduling settings for top-app when it runs.
Jorim Jaggied7993b2017-03-28 18:50:01 +0100361 Process.setThreadGroupAndCpuset(DisplayThread.get().getThreadId(),
362 Process.THREAD_GROUP_TOP_APP);
363 Process.setThreadGroupAndCpuset(AnimationThread.get().getThreadId(),
364 Process.THREAD_GROUP_TOP_APP);
Jorim Jaggi21c39a72017-10-20 15:47:51 +0200365 Process.setThreadGroupAndCpuset(SurfaceAnimationThread.get().getThreadId(),
366 Process.THREAD_GROUP_TOP_APP);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700367 }
368
Jeff Brown4ccb8232014-01-16 22:16:42 -0800369 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -0800370 public void onStart() {
Michael Wright1c9977b2016-07-12 13:30:10 -0700371 // We need to pre-load the persistent data store so it's ready before the default display
372 // adapter is up so that we have it's configuration. We could load it lazily, but since
373 // 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 -0700374 // we've waited for the display to register itself with us.
Michael Wrighta3dab232019-02-22 16:54:21 +0000375 synchronized (mSyncRoot) {
376 mPersistentDataStore.loadIfNeeded();
377 loadStableDisplayValuesLocked();
Michael Wrighteedcbf12017-08-16 23:14:54 +0100378 }
Santos Cordonc22c5632017-06-21 16:03:49 -0700379 mHandler.sendEmptyMessage(MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800380
Tim Murray890ceb52020-01-30 10:12:40 -0800381 // If there was a runtime restart then we may have stale caches left around, so we need to
382 // make sure to invalidate them upon every start.
383 DisplayManagerGlobal.invalidateLocalDisplayInfoCaches();
384
Jeff Brown4ccb8232014-01-16 22:16:42 -0800385 publishBinderService(Context.DISPLAY_SERVICE, new BinderService(),
386 true /*allowIsolated*/);
387 publishLocalService(DisplayManagerInternal.class, new LocalService());
388 }
389
390 @Override
391 public void onBootPhase(int phase) {
392 if (phase == PHASE_WAIT_FOR_DEFAULT_DISPLAY) {
393 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700394 long timeout = SystemClock.uptimeMillis()
395 + mInjector.getDefaultDisplayDelayTimeout();
396 while (mLogicalDisplays.get(Display.DEFAULT_DISPLAY) == null ||
397 mVirtualDisplayAdapter == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800398 long delay = timeout - SystemClock.uptimeMillis();
399 if (delay <= 0) {
400 throw new RuntimeException("Timeout waiting for default display "
Santos Cordonc22c5632017-06-21 16:03:49 -0700401 + "to be initialized. DefaultDisplay="
402 + mLogicalDisplays.get(Display.DEFAULT_DISPLAY)
403 + ", mVirtualDisplayAdapter=" + mVirtualDisplayAdapter);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800404 }
405 if (DEBUG) {
406 Slog.d(TAG, "waitForDefaultDisplay: waiting, timeout=" + delay);
407 }
408 try {
409 mSyncRoot.wait(delay);
410 } catch (InterruptedException ex) {
411 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700412 }
413 }
414 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700415 }
416
Michael Wrighteef0e132017-11-21 17:57:52 +0000417 @Override
418 public void onSwitchUser(@UserIdInt int newUserId) {
419 final int userSerial = getUserManager().getUserSerialNumber(newUserId);
420 synchronized (mSyncRoot) {
421 if (mCurrentUserId != newUserId) {
422 mCurrentUserId = newUserId;
423 BrightnessConfiguration config =
424 mPersistentDataStore.getBrightnessConfiguration(userSerial);
425 mDisplayPowerController.setBrightnessConfiguration(config);
Galia Peycheva088d7c02019-12-13 11:27:23 +0100426 handleSettingsChange();
Michael Wrighteef0e132017-11-21 17:57:52 +0000427 }
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +0000428 mDisplayPowerController.onSwitchUser(newUserId);
Michael Wrighteef0e132017-11-21 17:57:52 +0000429 }
430 }
431
Jeff Brown4ccb8232014-01-16 22:16:42 -0800432 // TODO: Use dependencies or a boot phase
433 public void windowManagerAndInputReady() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700434 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800435 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
436 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner65d11b32012-10-01 13:59:52 -0700437 scheduleTraversalLocked(false);
Jeff Brownd728bf52012-09-08 18:05:28 -0700438 }
439 }
440
441 /**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700442 * Called when the system is ready to go.
443 */
444 public void systemReady(boolean safeMode, boolean onlyCore) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700445 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700446 mSafeMode = safeMode;
447 mOnlyCore = onlyCore;
Winson72dbe7e2019-04-29 14:55:30 -0700448 mSystemReady = true;
449 // Just in case the top inset changed before the system was ready. At this point, any
450 // relevant configuration should be in place.
451 recordTopInsetLocked(mLogicalDisplays.get(Display.DEFAULT_DISPLAY));
Galia Peycheva088d7c02019-12-13 11:27:23 +0100452
453 updateSettingsLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700454 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700455
Ana Kruleca74a8642019-11-14 00:51:00 +0100456 mDisplayModeDirector.setDesiredDisplayModeSpecsListener(
457 new DesiredDisplayModeSpecsObserver());
Long Lingbc841b02019-07-03 16:43:15 -0700458 mDisplayModeDirector.start(mSensorManager);
Michael Wrighta3dab232019-02-22 16:54:21 +0000459
Jeff Brownbd6e1502012-08-28 03:27:37 -0700460 mHandler.sendEmptyMessage(MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS);
Galia Peycheva088d7c02019-12-13 11:27:23 +0100461
462 mSettingsObserver = new SettingsObserver();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700463 }
464
Santos Cordonee8931e2017-04-05 10:31:15 -0700465 @VisibleForTesting
466 Handler getDisplayHandler() {
467 return mHandler;
468 }
469
Michael Wrighteedcbf12017-08-16 23:14:54 +0100470 private void loadStableDisplayValuesLocked() {
471 final Point size = mPersistentDataStore.getStableDisplaySize();
472 if (size.x > 0 && size.y > 0) {
473 // Just set these values directly so we don't write the display persistent data again
474 // unnecessarily
475 mStableDisplaySize.set(size.x, size.y);
476 } else {
477 final Resources res = mContext.getResources();
478 final int width = res.getInteger(
479 com.android.internal.R.integer.config_stableDeviceDisplayWidth);
480 final int height = res.getInteger(
481 com.android.internal.R.integer.config_stableDeviceDisplayHeight);
482 if (width > 0 && height > 0) {
483 setStableDisplaySizeLocked(width, height);
484 }
485 }
486 }
487
488 private Point getStableDisplaySizeInternal() {
489 Point r = new Point();
490 synchronized (mSyncRoot) {
491 if (mStableDisplaySize.x > 0 && mStableDisplaySize.y > 0) {
492 r.set(mStableDisplaySize.x, mStableDisplaySize.y);
493 }
494 }
495 return r;
496 }
497
Jeff Brown4ccb8232014-01-16 22:16:42 -0800498 private void registerDisplayTransactionListenerInternal(
499 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800500 // List is self-synchronized copy-on-write.
501 mDisplayTransactionListeners.add(listener);
502 }
503
Jeff Brown4ccb8232014-01-16 22:16:42 -0800504 private void unregisterDisplayTransactionListenerInternal(
505 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800506 // List is self-synchronized copy-on-write.
507 mDisplayTransactionListeners.remove(listener);
508 }
509
Jeff Brown4ccb8232014-01-16 22:16:42 -0800510 private void setDisplayInfoOverrideFromWindowManagerInternal(
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700511 int displayId, DisplayInfo info) {
512 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700513 LogicalDisplay display = mLogicalDisplays.get(displayId);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700514 if (display != null) {
Jeff Brownef981a42013-08-07 14:13:37 -0700515 if (display.setDisplayInfoOverrideFromWindowManagerLocked(info)) {
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200516 handleLogicalDisplayChanged(displayId, display);
Craig Mautner65d11b32012-10-01 13:59:52 -0700517 scheduleTraversalLocked(false);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700518 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700519 }
520 }
521 }
522
Andrii Kuliancd097992017-03-23 18:31:59 -0700523 /**
524 * @see DisplayManagerInternal#getNonOverrideDisplayInfo(int, DisplayInfo)
525 */
526 private void getNonOverrideDisplayInfoInternal(int displayId, DisplayInfo outInfo) {
527 synchronized (mSyncRoot) {
528 final LogicalDisplay display = mLogicalDisplays.get(displayId);
529 if (display != null) {
530 display.getNonOverrideDisplayInfoLocked(outInfo);
531 }
532 }
533 }
534
Santos Cordonee8931e2017-04-05 10:31:15 -0700535 @VisibleForTesting
Robert Carrae606b42018-02-15 15:36:23 -0800536 void performTraversalInternal(SurfaceControl.Transaction t) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700537 synchronized (mSyncRoot) {
538 if (!mPendingTraversal) {
539 return;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700540 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700541 mPendingTraversal = false;
542
Robert Carrae606b42018-02-15 15:36:23 -0800543 performTraversalLocked(t);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700544 }
Jeff Brown7f3994e2012-12-04 14:04:28 -0800545
546 // List is self-synchronized copy-on-write.
547 for (DisplayTransactionListener listener : mDisplayTransactionListeners) {
Vishnu Naire86bd982018-11-28 13:23:17 -0800548 listener.onDisplayTransaction(t);
Jeff Brown7f3994e2012-12-04 14:04:28 -0800549 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700550 }
551
Fiona Campbelld4eb2952019-11-04 17:19:56 +0000552 private void requestGlobalDisplayStateInternal(int state, float brightnessState) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700553 if (state == Display.STATE_UNKNOWN) {
554 state = Display.STATE_ON;
555 }
556 if (state == Display.STATE_OFF) {
Fiona Campbelld4eb2952019-11-04 17:19:56 +0000557 brightnessState = PowerManager.BRIGHTNESS_OFF_FLOAT;
Fiona Campbell57af7c82020-04-02 12:56:15 +0100558 } else if (brightnessState != PowerManager.BRIGHTNESS_OFF_FLOAT
559 && brightnessState < PowerManager.BRIGHTNESS_MIN) {
Fiona Campbelld4eb2952019-11-04 17:19:56 +0000560 brightnessState = PowerManager.BRIGHTNESS_INVALID_FLOAT;
561 } else if (brightnessState > PowerManager.BRIGHTNESS_MAX) {
562 brightnessState = PowerManager.BRIGHTNESS_MAX;
Jeff Brown5d6443b2015-04-10 20:15:01 -0700563 }
564
Jeff Browne75926d2014-09-18 15:24:49 -0700565 synchronized (mTempDisplayStateWorkQueue) {
566 try {
567 // Update the display state within the lock.
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700568 // Note that we do not need to schedule traversals here although it
569 // may happen as a side-effect of displays changing state.
Jeff Browne75926d2014-09-18 15:24:49 -0700570 synchronized (mSyncRoot) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700571 if (mGlobalDisplayState == state
Fiona Campbelld4eb2952019-11-04 17:19:56 +0000572 && mGlobalDisplayBrightness == brightnessState) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700573 return; // no change
Jeff Browne75926d2014-09-18 15:24:49 -0700574 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700575
576 Trace.traceBegin(Trace.TRACE_TAG_POWER, "requestGlobalDisplayState("
577 + Display.stateToString(state)
Fiona Campbelld4eb2952019-11-04 17:19:56 +0000578 + ", brightness=" + brightnessState + ")");
Jeff Brown5d6443b2015-04-10 20:15:01 -0700579 mGlobalDisplayState = state;
Fiona Campbelld4eb2952019-11-04 17:19:56 +0000580 mGlobalDisplayBrightness = brightnessState;
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700581 applyGlobalDisplayStateLocked(mTempDisplayStateWorkQueue);
Jeff Browne75926d2014-09-18 15:24:49 -0700582 }
583
584 // Setting the display power state can take hundreds of milliseconds
585 // to complete so we defer the most expensive part of the work until
586 // after we have exited the critical section to avoid blocking other
587 // threads for a long time.
588 for (int i = 0; i < mTempDisplayStateWorkQueue.size(); i++) {
589 mTempDisplayStateWorkQueue.get(i).run();
590 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700591 Trace.traceEnd(Trace.TRACE_TAG_POWER);
Jeff Browne75926d2014-09-18 15:24:49 -0700592 } finally {
593 mTempDisplayStateWorkQueue.clear();
Jeff Brown9e316a12012-10-08 19:17:06 -0700594 }
595 }
596 }
597
Galia Peycheva088d7c02019-12-13 11:27:23 +0100598 private class SettingsObserver extends ContentObserver {
599 SettingsObserver() {
600 super(mHandler);
601
602 mContext.getContentResolver().registerContentObserver(
603 Settings.Secure.getUriFor(
604 Settings.Secure.MINIMAL_POST_PROCESSING_ALLOWED), false, this);
605 }
606
607 @Override
608 public void onChange(boolean selfChange, Uri uri) {
609 handleSettingsChange();
610 }
611 }
612
613 private void handleSettingsChange() {
614 synchronized (mSyncRoot) {
615 updateSettingsLocked();
616 scheduleTraversalLocked(false);
617 }
618 }
619
620 private void updateSettingsLocked() {
621 mMinimalPostProcessingAllowed = Settings.Secure.getIntForUser(mContext.getContentResolver(),
622 Settings.Secure.MINIMAL_POST_PROCESSING_ALLOWED, 1, UserHandle.USER_CURRENT) != 0;
623 }
624
Jeff Brown4ccb8232014-01-16 22:16:42 -0800625 private DisplayInfo getDisplayInfoInternal(int displayId, int callingUid) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700626 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800627 LogicalDisplay display = mLogicalDisplays.get(displayId);
628 if (display != null) {
629 DisplayInfo info = display.getDisplayInfoLocked();
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800630 if (info.hasAccess(callingUid)
631 || isUidPresentOnDisplayInternal(callingUid, displayId)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800632 return info;
633 }
634 }
635 return null;
636 }
637 }
638
639 private int[] getDisplayIdsInternal(int callingUid) {
640 synchronized (mSyncRoot) {
641 final int count = mLogicalDisplays.size();
642 int[] displayIds = new int[count];
643 int n = 0;
644 for (int i = 0; i < count; i++) {
645 LogicalDisplay display = mLogicalDisplays.valueAt(i);
646 DisplayInfo info = display.getDisplayInfoLocked();
647 if (info.hasAccess(callingUid)) {
648 displayIds[n++] = mLogicalDisplays.keyAt(i);
649 }
650 }
651 if (n != count) {
652 displayIds = Arrays.copyOfRange(displayIds, 0, n);
653 }
654 return displayIds;
655 }
656 }
657
658 private void registerCallbackInternal(IDisplayManagerCallback callback, int callingPid) {
659 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700660 if (mCallbacks.get(callingPid) != null) {
661 throw new SecurityException("The calling process has already "
662 + "registered an IDisplayManagerCallback.");
Jeff Brown64a55af2012-08-26 02:47:39 -0700663 }
664
Jeff Brownbd6e1502012-08-28 03:27:37 -0700665 CallbackRecord record = new CallbackRecord(callingPid, callback);
666 try {
667 IBinder binder = callback.asBinder();
668 binder.linkToDeath(record, 0);
669 } catch (RemoteException ex) {
670 // give up
671 throw new RuntimeException(ex);
672 }
673
674 mCallbacks.put(callingPid, record);
675 }
676 }
677
Jeff Brownce468a32013-11-21 16:42:03 -0800678 private void onCallbackDied(CallbackRecord record) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700679 synchronized (mSyncRoot) {
Jeff Brownce468a32013-11-21 16:42:03 -0800680 mCallbacks.remove(record.mPid);
681 stopWifiDisplayScanLocked(record);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700682 }
683 }
684
Jeff Brown4ccb8232014-01-16 22:16:42 -0800685 private void startWifiDisplayScanInternal(int callingPid) {
686 synchronized (mSyncRoot) {
687 CallbackRecord record = mCallbacks.get(callingPid);
688 if (record == null) {
689 throw new IllegalStateException("The calling process has not "
690 + "registered an IDisplayManagerCallback.");
Jeff Browne08ae382012-09-07 20:36:36 -0700691 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800692 startWifiDisplayScanLocked(record);
Jeff Browne08ae382012-09-07 20:36:36 -0700693 }
694 }
695
Jeff Brownce468a32013-11-21 16:42:03 -0800696 private void startWifiDisplayScanLocked(CallbackRecord record) {
697 if (!record.mWifiDisplayScanRequested) {
698 record.mWifiDisplayScanRequested = true;
699 if (mWifiDisplayScanRequestCount++ == 0) {
700 if (mWifiDisplayAdapter != null) {
701 mWifiDisplayAdapter.requestStartScanLocked();
702 }
703 }
704 }
705 }
706
Jeff Brown4ccb8232014-01-16 22:16:42 -0800707 private void stopWifiDisplayScanInternal(int callingPid) {
708 synchronized (mSyncRoot) {
709 CallbackRecord record = mCallbacks.get(callingPid);
710 if (record == null) {
711 throw new IllegalStateException("The calling process has not "
712 + "registered an IDisplayManagerCallback.");
Jeff Brownce468a32013-11-21 16:42:03 -0800713 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800714 stopWifiDisplayScanLocked(record);
Jeff Brownce468a32013-11-21 16:42:03 -0800715 }
716 }
717
718 private void stopWifiDisplayScanLocked(CallbackRecord record) {
719 if (record.mWifiDisplayScanRequested) {
720 record.mWifiDisplayScanRequested = false;
721 if (--mWifiDisplayScanRequestCount == 0) {
722 if (mWifiDisplayAdapter != null) {
723 mWifiDisplayAdapter.requestStopScanLocked();
724 }
725 } else if (mWifiDisplayScanRequestCount < 0) {
Dianne Hackborn8d051722014-10-01 14:59:58 -0700726 Slog.wtf(TAG, "mWifiDisplayScanRequestCount became negative: "
Jeff Brownce468a32013-11-21 16:42:03 -0800727 + mWifiDisplayScanRequestCount);
728 mWifiDisplayScanRequestCount = 0;
729 }
730 }
731 }
732
Jeff Brown4ccb8232014-01-16 22:16:42 -0800733 private void connectWifiDisplayInternal(String address) {
734 synchronized (mSyncRoot) {
735 if (mWifiDisplayAdapter != null) {
736 mWifiDisplayAdapter.requestConnectLocked(address);
Jeff Browne08ae382012-09-07 20:36:36 -0700737 }
Jeff Browne08ae382012-09-07 20:36:36 -0700738 }
739 }
740
Jeff Brown4ccb8232014-01-16 22:16:42 -0800741 private void pauseWifiDisplayInternal() {
742 synchronized (mSyncRoot) {
743 if (mWifiDisplayAdapter != null) {
744 mWifiDisplayAdapter.requestPauseLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700745 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700746 }
747 }
748
Jeff Brown4ccb8232014-01-16 22:16:42 -0800749 private void resumeWifiDisplayInternal() {
750 synchronized (mSyncRoot) {
751 if (mWifiDisplayAdapter != null) {
752 mWifiDisplayAdapter.requestResumeLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700753 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700754 }
755 }
756
Jeff Brown4ccb8232014-01-16 22:16:42 -0800757 private void disconnectWifiDisplayInternal() {
758 synchronized (mSyncRoot) {
759 if (mWifiDisplayAdapter != null) {
760 mWifiDisplayAdapter.requestDisconnectLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700761 }
Jeff Browne08ae382012-09-07 20:36:36 -0700762 }
763 }
764
Jeff Brown4ccb8232014-01-16 22:16:42 -0800765 private void renameWifiDisplayInternal(String address, String alias) {
766 synchronized (mSyncRoot) {
767 if (mWifiDisplayAdapter != null) {
768 mWifiDisplayAdapter.requestRenameLocked(address, alias);
Jeff Brown89d55462012-09-19 11:33:42 -0700769 }
Jeff Brown89d55462012-09-19 11:33:42 -0700770 }
771 }
772
Jeff Brown4ccb8232014-01-16 22:16:42 -0800773 private void forgetWifiDisplayInternal(String address) {
774 synchronized (mSyncRoot) {
775 if (mWifiDisplayAdapter != null) {
776 mWifiDisplayAdapter.requestForgetLocked(address);
Jeff Brown89d55462012-09-19 11:33:42 -0700777 }
Jeff Brown89d55462012-09-19 11:33:42 -0700778 }
779 }
780
Jeff Brown4ccb8232014-01-16 22:16:42 -0800781 private WifiDisplayStatus getWifiDisplayStatusInternal() {
782 synchronized (mSyncRoot) {
783 if (mWifiDisplayAdapter != null) {
784 return mWifiDisplayAdapter.getWifiDisplayStatusLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700785 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800786 return new WifiDisplayStatus();
Jeff Browne08ae382012-09-07 20:36:36 -0700787 }
788 }
789
Michael Wright1c9977b2016-07-12 13:30:10 -0700790 private void requestColorModeInternal(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +0100791 synchronized (mSyncRoot) {
792 LogicalDisplay display = mLogicalDisplays.get(displayId);
793 if (display != null &&
Michael Wright1c9977b2016-07-12 13:30:10 -0700794 display.getRequestedColorModeLocked() != colorMode) {
795 display.setRequestedColorModeLocked(colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +0100796 scheduleTraversalLocked(false);
797 }
798 }
799 }
800
Michael Wright75ee9fc2014-09-01 19:55:22 -0700801 private int createVirtualDisplayInternal(IVirtualDisplayCallback callback,
b0202.jung925435c2020-01-08 18:46:59 +0900802 IMediaProjection projection, int callingUid, String packageName, Surface surface,
803 int flags, VirtualDisplayConfig virtualDisplayConfig) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800804 synchronized (mSyncRoot) {
805 if (mVirtualDisplayAdapter == null) {
806 Slog.w(TAG, "Rejecting request to create private virtual display "
807 + "because the virtual display adapter is not available.");
808 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700809 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800810
811 DisplayDevice device = mVirtualDisplayAdapter.createVirtualDisplayLocked(
b0202.jung925435c2020-01-08 18:46:59 +0900812 callback, projection, callingUid, packageName, surface, flags,
813 virtualDisplayConfig);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800814 if (device == null) {
815 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700816 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700817
Jeff Brown4ccb8232014-01-16 22:16:42 -0800818 handleDisplayDeviceAddedLocked(device);
819 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
820 if (display != null) {
821 return display.getDisplayIdLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700822 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800823
824 // Something weird happened and the logical display was not created.
825 Slog.w(TAG, "Rejecting request to create virtual display "
826 + "because the logical display was not created.");
Michael Wright75ee9fc2014-09-01 19:55:22 -0700827 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800828 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700829 }
830 return -1;
831 }
832
Michael Wright01e840f2014-06-26 16:03:25 -0700833 private void resizeVirtualDisplayInternal(IBinder appToken,
834 int width, int height, int densityDpi) {
835 synchronized (mSyncRoot) {
836 if (mVirtualDisplayAdapter == null) {
837 return;
838 }
839
840 mVirtualDisplayAdapter.resizeVirtualDisplayLocked(appToken, width, height, densityDpi);
841 }
842 }
843
Jeff Brown92207df2014-04-16 13:16:07 -0700844 private void setVirtualDisplaySurfaceInternal(IBinder appToken, Surface surface) {
845 synchronized (mSyncRoot) {
846 if (mVirtualDisplayAdapter == null) {
847 return;
848 }
849
850 mVirtualDisplayAdapter.setVirtualDisplaySurfaceLocked(appToken, surface);
851 }
852 }
853
Jeff Brown4ccb8232014-01-16 22:16:42 -0800854 private void releaseVirtualDisplayInternal(IBinder appToken) {
855 synchronized (mSyncRoot) {
856 if (mVirtualDisplayAdapter == null) {
857 return;
Jeff Browna506a6e2013-06-04 00:02:38 -0700858 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700859
Jeff Brown4ccb8232014-01-16 22:16:42 -0800860 DisplayDevice device =
861 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(appToken);
862 if (device != null) {
863 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700864 }
865 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700866 }
867
chaviwda4c6942018-11-07 15:52:56 -0800868 private void setVirtualDisplayStateInternal(IBinder appToken, boolean isOn) {
869 synchronized (mSyncRoot) {
870 if (mVirtualDisplayAdapter == null) {
871 return;
872 }
873
874 mVirtualDisplayAdapter.setVirtualDisplayStateLocked(appToken, isOn);
875 }
876 }
877
Santos Cordonc22c5632017-06-21 16:03:49 -0700878 private void registerDefaultDisplayAdapters() {
879 // Register default display adapters.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700880 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700881 // main display adapter
Mike Lockwoode63f6f72013-11-15 11:01:47 -0800882 registerDisplayAdapterLocked(new LocalDisplayAdapter(
883 mSyncRoot, mContext, mHandler, mDisplayAdapterListener));
Santos Cordonc22c5632017-06-21 16:03:49 -0700884
885 // Standalone VR devices rely on a virtual display as their primary display for
886 // 2D UI. We register virtual display adapter along side the main display adapter
887 // here so that it is ready by the time the system sends the home Intent for
888 // early apps like SetupWizard/Launcher. In particular, SUW is displayed using
889 // the virtual display inside VR before any VR-specific apps even run.
890 mVirtualDisplayAdapter = mInjector.getVirtualDisplayAdapter(mSyncRoot, mContext,
891 mHandler, mDisplayAdapterListener);
892 if (mVirtualDisplayAdapter != null) {
893 registerDisplayAdapterLocked(mVirtualDisplayAdapter);
894 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700895 }
896 }
897
898 private void registerAdditionalDisplayAdapters() {
899 synchronized (mSyncRoot) {
900 if (shouldRegisterNonEssentialDisplayAdaptersLocked()) {
Jeff Brown89d55462012-09-19 11:33:42 -0700901 registerOverlayDisplayAdapterLocked();
902 registerWifiDisplayAdapterLocked();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700903 }
904 }
905 }
906
Jeff Brown89d55462012-09-19 11:33:42 -0700907 private void registerOverlayDisplayAdapterLocked() {
908 registerDisplayAdapterLocked(new OverlayDisplayAdapter(
909 mSyncRoot, mContext, mHandler, mDisplayAdapterListener, mUiHandler));
910 }
911
912 private void registerWifiDisplayAdapterLocked() {
913 if (mContext.getResources().getBoolean(
Jeff Brownbbd28a22012-09-20 16:47:15 -0700914 com.android.internal.R.bool.config_enableWifiDisplay)
915 || SystemProperties.getInt(FORCE_WIFI_DISPLAY_ENABLE, -1) == 1) {
Jeff Brown89d55462012-09-19 11:33:42 -0700916 mWifiDisplayAdapter = new WifiDisplayAdapter(
917 mSyncRoot, mContext, mHandler, mDisplayAdapterListener,
918 mPersistentDataStore);
919 registerDisplayAdapterLocked(mWifiDisplayAdapter);
920 }
921 }
922
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700923 private boolean shouldRegisterNonEssentialDisplayAdaptersLocked() {
924 // In safe mode, we disable non-essential display adapters to give the user
925 // an opportunity to fix broken settings or other problems that might affect
926 // system stability.
927 // In only-core mode, we disable non-essential display adapters to minimize
928 // the number of dependencies that are started while in this mode and to
929 // prevent problems that might occur due to the device being encrypted.
930 return !mSafeMode && !mOnlyCore;
931 }
932
933 private void registerDisplayAdapterLocked(DisplayAdapter adapter) {
934 mDisplayAdapters.add(adapter);
935 adapter.registerLocked();
936 }
937
Jeff Brownbd6e1502012-08-28 03:27:37 -0700938 private void handleDisplayDeviceAdded(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700939 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700940 handleDisplayDeviceAddedLocked(device);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700941 }
942 }
943
Jeff Browna506a6e2013-06-04 00:02:38 -0700944 private void handleDisplayDeviceAddedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700945 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700946 if (mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700947 Slog.w(TAG, "Attempted to add already added display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700948 return;
949 }
950
Jeff Brown10acf6d2015-04-14 14:20:47 -0700951 Slog.i(TAG, "Display device added: " + info);
952 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700953
954 mDisplayDevices.add(device);
Michael Wright1c9977b2016-07-12 13:30:10 -0700955 LogicalDisplay display = addLogicalDisplayLocked(device);
Jeff Brown0033a862014-10-08 12:06:39 -0700956 Runnable work = updateDisplayStateLocked(device);
957 if (work != null) {
958 work.run();
959 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700960 scheduleTraversalLocked(false);
961 }
962
Jeff Brownbd6e1502012-08-28 03:27:37 -0700963 private void handleDisplayDeviceChanged(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700964 synchronized (mSyncRoot) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700965 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700966 if (!mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700967 Slog.w(TAG, "Attempted to change non-existent display device: " + info);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700968 return;
969 }
970
Jeff Brown10acf6d2015-04-14 14:20:47 -0700971 int diff = device.mDebugLastLoggedDeviceInfo.diff(info);
972 if (diff == DisplayDeviceInfo.DIFF_STATE) {
973 Slog.i(TAG, "Display device changed state: \"" + info.name
974 + "\", " + Display.stateToString(info.state));
Chris Yed6d35592020-05-01 11:36:33 -0700975 final Optional<Integer> viewportType = getViewportType(info);
976 if (viewportType.isPresent()) {
977 for (DisplayViewport d : mViewports) {
978 if (d.type == viewportType.get() && info.uniqueId.equals(d.uniqueId)) {
979 // Update display view port power state
980 d.isActive = Display.isActiveState(info.state);
981 }
982 }
983 if (mInputManagerInternal != null) {
984 mHandler.sendEmptyMessage(MSG_UPDATE_VIEWPORT);
985 }
986 }
Jeff Brown10acf6d2015-04-14 14:20:47 -0700987 } else if (diff != 0) {
988 Slog.i(TAG, "Display device changed: " + info);
989 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700990 if ((diff & DisplayDeviceInfo.DIFF_COLOR_MODE) != 0) {
991 try {
992 mPersistentDataStore.setColorMode(device, info.colorMode);
993 } finally {
994 mPersistentDataStore.saveIfNeeded();
995 }
996 }
Jeff Brown10acf6d2015-04-14 14:20:47 -0700997 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browne87bf032012-09-20 18:30:13 -0700998
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700999 device.applyPendingDisplayDeviceInfoChangesLocked();
1000 if (updateLogicalDisplaysLocked()) {
Craig Mautner65d11b32012-10-01 13:59:52 -07001001 scheduleTraversalLocked(false);
Jeff Brown64a55af2012-08-26 02:47:39 -07001002 }
1003 }
1004 }
1005
Jeff Brownbd6e1502012-08-28 03:27:37 -07001006 private void handleDisplayDeviceRemoved(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001007 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001008 handleDisplayDeviceRemovedLocked(device);
1009 }
1010 }
Jeff Brown8e5d33e2015-06-10 13:05:50 -07001011
Jeff Browna506a6e2013-06-04 00:02:38 -07001012 private void handleDisplayDeviceRemovedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -07001013 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -07001014 if (!mDisplayDevices.remove(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -07001015 Slog.w(TAG, "Attempted to remove non-existent display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -07001016 return;
1017 }
1018
Jeff Brown10acf6d2015-04-14 14:20:47 -07001019 Slog.i(TAG, "Display device removed: " + info);
1020 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -07001021
Jeff Browna506a6e2013-06-04 00:02:38 -07001022 updateLogicalDisplaysLocked();
1023 scheduleTraversalLocked(false);
1024 }
1025
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001026 private void handleLogicalDisplayChanged(int displayId, @NonNull LogicalDisplay display) {
1027 if (displayId == Display.DEFAULT_DISPLAY) {
1028 recordTopInsetLocked(display);
1029 }
Tim Murray890ceb52020-01-30 10:12:40 -08001030 // We don't bother invalidating the display info caches here because any changes to the
1031 // display info will trigger a cache invalidation inside of LogicalDisplay before we hit
1032 // this point.
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001033 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
1034 }
1035
Tim Murray890ceb52020-01-30 10:12:40 -08001036 private void handleLogicalDisplayRemoved(int displayId) {
1037 DisplayManagerGlobal.invalidateLocalDisplayInfoCaches();
1038 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_REMOVED);
1039 }
1040
Jeff Brown8e5d33e2015-06-10 13:05:50 -07001041 private void applyGlobalDisplayStateLocked(List<Runnable> workQueue) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001042 final int count = mDisplayDevices.size();
1043 for (int i = 0; i < count; i++) {
1044 DisplayDevice device = mDisplayDevices.get(i);
Jeff Browne75926d2014-09-18 15:24:49 -07001045 Runnable runnable = updateDisplayStateLocked(device);
1046 if (runnable != null) {
1047 workQueue.add(runnable);
1048 }
Jeff Browna506a6e2013-06-04 00:02:38 -07001049 }
1050 }
1051
Jeff Browne75926d2014-09-18 15:24:49 -07001052 private Runnable updateDisplayStateLocked(DisplayDevice device) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001053 // Blank or unblank the display immediately to match the state requested
Jeff Brown037c33e2014-04-09 00:31:55 -07001054 // by the display power controller (if known).
Jeff Browna506a6e2013-06-04 00:02:38 -07001055 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
1056 if ((info.flags & DisplayDeviceInfo.FLAG_NEVER_BLANK) == 0) {
Fiona Campbelld4eb2952019-11-04 17:19:56 +00001057 return device.requestDisplayStateLocked(
1058 mGlobalDisplayState, mGlobalDisplayBrightness);
Craig Mautner4f67ba62012-08-02 11:23:00 -07001059 }
Jeff Browne75926d2014-09-18 15:24:49 -07001060 return null;
Craig Mautner4f67ba62012-08-02 11:23:00 -07001061 }
1062
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001063 // Adds a new logical display based on the given display device.
1064 // Sends notifications if needed.
Michael Wright1c9977b2016-07-12 13:30:10 -07001065 private LogicalDisplay addLogicalDisplayLocked(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001066 DisplayDeviceInfo deviceInfo = device.getDisplayDeviceInfoLocked();
1067 boolean isDefault = (deviceInfo.flags
1068 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0;
1069 if (isDefault && mLogicalDisplays.get(Display.DEFAULT_DISPLAY) != null) {
1070 Slog.w(TAG, "Ignoring attempt to add a second default display: " + deviceInfo);
1071 isDefault = false;
1072 }
1073
Jeff Brown27f1d672012-10-17 18:32:34 -07001074 if (!isDefault && mSingleDisplayDemoMode) {
1075 Slog.i(TAG, "Not creating a logical display for a secondary display "
1076 + " because single display demo mode is enabled: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -07001077 return null;
Jeff Brown27f1d672012-10-17 18:32:34 -07001078 }
1079
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001080 final int displayId = assignDisplayIdLocked(isDefault);
1081 final int layerStack = assignLayerStackLocked(displayId);
1082
Jeff Brownd728bf52012-09-08 18:05:28 -07001083 LogicalDisplay display = new LogicalDisplay(displayId, layerStack, device);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001084 display.updateLocked(mDisplayDevices);
1085 if (!display.isValidLocked()) {
1086 // This should never happen currently.
1087 Slog.w(TAG, "Ignoring display device because the logical display "
1088 + "created from it was not considered valid: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -07001089 return null;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001090 }
1091
Michael Wrighteedcbf12017-08-16 23:14:54 +01001092 configureColorModeLocked(display, device);
1093 if (isDefault) {
1094 recordStableDisplayStatsIfNeededLocked(display);
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001095 recordTopInsetLocked(display);
Michael Wrighteedcbf12017-08-16 23:14:54 +01001096 }
1097
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001098 mLogicalDisplays.put(displayId, display);
Tim Murray890ceb52020-01-30 10:12:40 -08001099 DisplayManagerGlobal.invalidateLocalDisplayInfoCaches();
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001100
1101 // Wake up waitForDefaultDisplay.
1102 if (isDefault) {
1103 mSyncRoot.notifyAll();
1104 }
1105
1106 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_ADDED);
Michael Wright1c9977b2016-07-12 13:30:10 -07001107 return display;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001108 }
1109
1110 private int assignDisplayIdLocked(boolean isDefault) {
1111 return isDefault ? Display.DEFAULT_DISPLAY : mNextNonDefaultDisplayId++;
1112 }
1113
1114 private int assignLayerStackLocked(int displayId) {
1115 // Currently layer stacks and display ids are the same.
1116 // This need not be the case.
1117 return displayId;
1118 }
1119
Michael Wrighteedcbf12017-08-16 23:14:54 +01001120 private void configureColorModeLocked(LogicalDisplay display, DisplayDevice device) {
1121 if (display.getPrimaryDisplayDeviceLocked() == device) {
1122 int colorMode = mPersistentDataStore.getColorMode(device);
1123 if (colorMode == Display.COLOR_MODE_INVALID) {
1124 if ((device.getDisplayDeviceInfoLocked().flags
1125 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
1126 colorMode = mDefaultDisplayDefaultColorMode;
1127 } else {
1128 colorMode = Display.COLOR_MODE_DEFAULT;
1129 }
1130 }
1131 display.setRequestedColorModeLocked(colorMode);
1132 }
1133 }
1134
1135 // If we've never recorded stable device stats for this device before and they aren't
1136 // explicitly configured, go ahead and record the stable device stats now based on the status
1137 // of the default display at first boot.
1138 private void recordStableDisplayStatsIfNeededLocked(LogicalDisplay d) {
1139 if (mStableDisplaySize.x <= 0 && mStableDisplaySize.y <= 0) {
1140 DisplayInfo info = d.getDisplayInfoLocked();
1141 setStableDisplaySizeLocked(info.getNaturalWidth(), info.getNaturalHeight());
1142 }
1143 }
1144
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001145 private void recordTopInsetLocked(@Nullable LogicalDisplay d) {
Winson72dbe7e2019-04-29 14:55:30 -07001146 // We must only persist the inset after boot has completed, otherwise we will end up
1147 // overwriting the persisted value before the masking flag has been loaded from the
1148 // resource overlay.
1149 if (!mSystemReady || d == null) {
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001150 return;
1151 }
1152 int topInset = d.getInsets().top;
1153 if (topInset == mDefaultDisplayTopInset) {
1154 return;
1155 }
1156 mDefaultDisplayTopInset = topInset;
1157 SystemProperties.set(PROP_DEFAULT_DISPLAY_TOP_INSET, Integer.toString(topInset));
1158 }
1159
Michael Wrighteedcbf12017-08-16 23:14:54 +01001160 private void setStableDisplaySizeLocked(int width, int height) {
1161 mStableDisplaySize = new Point(width, height);
1162 try {
1163 mPersistentDataStore.setStableDisplaySize(mStableDisplaySize);
1164 } finally {
1165 mPersistentDataStore.saveIfNeeded();
1166 }
1167 }
1168
Dan Gittik122df862018-03-28 16:59:22 +01001169 @VisibleForTesting
1170 Curve getMinimumBrightnessCurveInternal() {
1171 return mMinimumBrightnessCurve;
1172 }
1173
Peiyong Lin277eaff2019-01-16 16:18:22 -08001174 int getPreferredWideGamutColorSpaceIdInternal() {
1175 return mWideColorSpace.getId();
1176 }
1177
Michael Wrighteef0e132017-11-21 17:57:52 +00001178 private void setBrightnessConfigurationForUserInternal(
Dan Gittik7a32fba2018-03-28 12:19:38 +01001179 @Nullable BrightnessConfiguration c, @UserIdInt int userId,
Kenny Guy05ce8092018-01-17 13:44:20 +00001180 @Nullable String packageName) {
Dan Gittik122df862018-03-28 16:59:22 +01001181 validateBrightnessConfiguration(c);
Michael Wrighteef0e132017-11-21 17:57:52 +00001182 final int userSerial = getUserManager().getUserSerialNumber(userId);
1183 synchronized (mSyncRoot) {
1184 try {
Kenny Guy05ce8092018-01-17 13:44:20 +00001185 mPersistentDataStore.setBrightnessConfigurationForUser(c, userSerial,
1186 packageName);
Michael Wrighteef0e132017-11-21 17:57:52 +00001187 } finally {
1188 mPersistentDataStore.saveIfNeeded();
1189 }
1190 if (userId == mCurrentUserId) {
1191 mDisplayPowerController.setBrightnessConfiguration(c);
1192 }
1193 }
1194 }
1195
Dan Gittik122df862018-03-28 16:59:22 +01001196 @VisibleForTesting
1197 void validateBrightnessConfiguration(BrightnessConfiguration config) {
1198 if (config == null) {
1199 return;
1200 }
1201 if (isBrightnessConfigurationTooDark(config)) {
1202 throw new IllegalArgumentException("brightness curve is too dark");
1203 }
1204 }
1205
1206 private boolean isBrightnessConfigurationTooDark(BrightnessConfiguration config) {
1207 Pair<float[], float[]> curve = config.getCurve();
1208 float[] lux = curve.first;
1209 float[] nits = curve.second;
1210 for (int i = 0; i < lux.length; i++) {
1211 if (nits[i] < mMinimumBrightnessSpline.interpolate(lux[i])) {
1212 return true;
1213 }
1214 }
1215 return false;
1216 }
1217
Michael Wrighteef0e132017-11-21 17:57:52 +00001218 private void loadBrightnessConfiguration() {
1219 synchronized (mSyncRoot) {
1220 final int userSerial = getUserManager().getUserSerialNumber(mCurrentUserId);
1221 BrightnessConfiguration config =
1222 mPersistentDataStore.getBrightnessConfiguration(userSerial);
1223 mDisplayPowerController.setBrightnessConfiguration(config);
1224 }
1225 }
1226
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001227 // Updates all existing logical displays given the current set of display devices.
1228 // Removes invalid logical displays.
1229 // Sends notifications if needed.
1230 private boolean updateLogicalDisplaysLocked() {
1231 boolean changed = false;
1232 for (int i = mLogicalDisplays.size(); i-- > 0; ) {
1233 final int displayId = mLogicalDisplays.keyAt(i);
1234 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1235
1236 mTempDisplayInfo.copyFrom(display.getDisplayInfoLocked());
1237 display.updateLocked(mDisplayDevices);
1238 if (!display.isValidLocked()) {
1239 mLogicalDisplays.removeAt(i);
Tim Murray890ceb52020-01-30 10:12:40 -08001240 handleLogicalDisplayRemoved(displayId);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001241 changed = true;
1242 } else if (!mTempDisplayInfo.equals(display.getDisplayInfoLocked())) {
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001243 handleLogicalDisplayChanged(displayId, display);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001244 changed = true;
1245 }
1246 }
1247 return changed;
1248 }
1249
Robert Carrae606b42018-02-15 15:36:23 -08001250 private void performTraversalLocked(SurfaceControl.Transaction t) {
Jeff Brownd728bf52012-09-08 18:05:28 -07001251 // Clear all viewports before configuring displays so that we can keep
1252 // track of which ones we have configured.
1253 clearViewportsLocked();
1254
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001255 // Configure each display device.
1256 final int count = mDisplayDevices.size();
1257 for (int i = 0; i < count; i++) {
1258 DisplayDevice device = mDisplayDevices.get(i);
Robert Carrae606b42018-02-15 15:36:23 -08001259 configureDisplayLocked(t, device);
1260 device.performTraversalLocked(t);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001261 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001262
1263 // Tell the input system about these new viewports.
Jeff Brown4ccb8232014-01-16 22:16:42 -08001264 if (mInputManagerInternal != null) {
Jeff Brownd728bf52012-09-08 18:05:28 -07001265 mHandler.sendEmptyMessage(MSG_UPDATE_VIEWPORT);
1266 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001267 }
1268
Michael Wright3f145a22014-07-22 19:46:03 -07001269 private void setDisplayPropertiesInternal(int displayId, boolean hasContent,
Galia Peycheva088d7c02019-12-13 11:27:23 +01001270 float requestedRefreshRate, int requestedModeId, boolean preferMinimalPostProcessing,
Galia Peycheva056b3ee2019-06-26 14:05:12 +02001271 boolean inTraversal) {
Craig Mautner722285e2012-09-07 13:55:58 -07001272 synchronized (mSyncRoot) {
1273 LogicalDisplay display = mLogicalDisplays.get(displayId);
Michael Wright3f145a22014-07-22 19:46:03 -07001274 if (display == null) {
1275 return;
1276 }
Galia Peycheva056b3ee2019-06-26 14:05:12 +02001277
1278 boolean shouldScheduleTraversal = false;
1279
Michael Wright3f145a22014-07-22 19:46:03 -07001280 if (display.hasContentLocked() != hasContent) {
Jeff Brown33041bd2013-08-02 21:11:14 -07001281 if (DEBUG) {
1282 Slog.d(TAG, "Display " + displayId + " hasContent flag changed: "
1283 + "hasContent=" + hasContent + ", inTraversal=" + inTraversal);
1284 }
1285
Craig Mautner722285e2012-09-07 13:55:58 -07001286 display.setHasContentLocked(hasContent);
Galia Peycheva056b3ee2019-06-26 14:05:12 +02001287 shouldScheduleTraversal = true;
Craig Mautner722285e2012-09-07 13:55:58 -07001288 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001289 if (requestedModeId == 0 && requestedRefreshRate != 0) {
1290 // Scan supported modes returned by display.getInfo() to find a mode with the same
1291 // size as the default display mode but with the specified refresh rate instead.
1292 requestedModeId = display.getDisplayInfoLocked().findDefaultModeByRefreshRate(
1293 requestedRefreshRate);
1294 }
Michael Wrighta3dab232019-02-22 16:54:21 +00001295 mDisplayModeDirector.getAppRequestObserver().setAppRequestedMode(
1296 displayId, requestedModeId);
Galia Peycheva056b3ee2019-06-26 14:05:12 +02001297
Galia Peycheva088d7c02019-12-13 11:27:23 +01001298 if (display.getDisplayInfoLocked().minimalPostProcessingSupported) {
1299 boolean mppRequest = mMinimalPostProcessingAllowed && preferMinimalPostProcessing;
Galia Peycheva056b3ee2019-06-26 14:05:12 +02001300
Galia Peycheva088d7c02019-12-13 11:27:23 +01001301 if (display.getRequestedMinimalPostProcessingLocked() != mppRequest) {
1302 display.setRequestedMinimalPostProcessingLocked(mppRequest);
1303 shouldScheduleTraversal = true;
1304 }
Galia Peycheva056b3ee2019-06-26 14:05:12 +02001305 }
1306
1307 if (shouldScheduleTraversal) {
1308 scheduleTraversalLocked(inTraversal);
1309 }
Craig Mautner722285e2012-09-07 13:55:58 -07001310 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001311 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001312
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08001313 private void setDisplayOffsetsInternal(int displayId, int x, int y) {
1314 synchronized (mSyncRoot) {
1315 LogicalDisplay display = mLogicalDisplays.get(displayId);
1316 if (display == null) {
1317 return;
1318 }
1319 if (display.getDisplayOffsetXLocked() != x
1320 || display.getDisplayOffsetYLocked() != y) {
1321 if (DEBUG) {
1322 Slog.d(TAG, "Display " + displayId + " burn-in offset set to ("
1323 + x + ", " + y + ")");
1324 }
1325 display.setDisplayOffsetsLocked(x, y);
1326 scheduleTraversalLocked(false);
1327 }
1328 }
1329 }
1330
Sam Lin4c3ac2b2019-02-18 04:50:26 -08001331 private void setDisplayScalingDisabledInternal(int displayId, boolean disable) {
1332 synchronized (mSyncRoot) {
1333 final LogicalDisplay display = mLogicalDisplays.get(displayId);
1334 if (display == null) {
1335 return;
1336 }
1337 if (display.isDisplayScalingDisabled() != disable) {
1338 if (DEBUG) {
1339 Slog.d(TAG, "Display " + displayId + " content scaling disabled = " + disable);
1340 }
1341 display.setDisplayScalingDisabledLocked(disable);
1342 scheduleTraversalLocked(false);
1343 }
1344 }
1345 }
1346
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001347 // Updates the lists of UIDs that are present on displays.
1348 private void setDisplayAccessUIDsInternal(SparseArray<IntArray> newDisplayAccessUIDs) {
1349 synchronized (mSyncRoot) {
1350 mDisplayAccessUIDs.clear();
1351 for (int i = newDisplayAccessUIDs.size() - 1; i >= 0; i--) {
1352 mDisplayAccessUIDs.append(newDisplayAccessUIDs.keyAt(i),
1353 newDisplayAccessUIDs.valueAt(i));
1354 }
1355 }
1356 }
1357
1358 // Checks if provided UID's content is present on the display and UID has access to it.
1359 private boolean isUidPresentOnDisplayInternal(int uid, int displayId) {
1360 synchronized (mSyncRoot) {
1361 final IntArray displayUIDs = mDisplayAccessUIDs.get(displayId);
1362 return displayUIDs != null && displayUIDs.indexOf(uid) != -1;
1363 }
1364 }
1365
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001366 @Nullable
1367 private IBinder getDisplayToken(int displayId) {
1368 synchronized (mSyncRoot) {
1369 final LogicalDisplay display = mLogicalDisplays.get(displayId);
1370 if (display != null) {
1371 final DisplayDevice device = display.getPrimaryDisplayDeviceLocked();
1372 if (device != null) {
1373 return device.getDisplayTokenLocked();
1374 }
1375 }
1376 }
1377
1378 return null;
1379 }
1380
Jacky Kao333de4e2020-04-07 15:01:41 +08001381 private SurfaceControl.ScreenshotGraphicBuffer systemScreenshotInternal(int displayId) {
Vinit Nayak627ac302020-01-13 15:50:10 -08001382 synchronized (mSyncRoot) {
1383 final IBinder token = getDisplayToken(displayId);
1384 if (token == null) {
1385 return null;
1386 }
1387 final LogicalDisplay logicalDisplay = mLogicalDisplays.get(displayId);
1388 if (logicalDisplay == null) {
1389 return null;
1390 }
1391
1392 final DisplayInfo displayInfo = logicalDisplay.getDisplayInfoLocked();
Jacky Kao333de4e2020-04-07 15:01:41 +08001393 return SurfaceControl.screenshotToBufferWithSecureLayersUnsafe(token, new Rect(),
1394 displayInfo.getNaturalWidth(), displayInfo.getNaturalHeight(),
1395 false /* useIdentityTransform */, 0 /* rotation */);
1396 }
1397 }
1398
1399 private SurfaceControl.ScreenshotGraphicBuffer userScreenshotInternal(int displayId) {
1400 synchronized (mSyncRoot) {
1401 final IBinder token = getDisplayToken(displayId);
1402 if (token == null) {
1403 return null;
Jacky Kao316477c2020-03-23 16:30:04 +08001404 }
Jacky Kao333de4e2020-04-07 15:01:41 +08001405 final LogicalDisplay logicalDisplay = mLogicalDisplays.get(displayId);
1406 if (logicalDisplay == null) {
1407 return null;
1408 }
1409
1410 final DisplayInfo displayInfo = logicalDisplay.getDisplayInfoLocked();
1411 // Takes screenshot based on current device orientation.
1412 final Display display = DisplayManagerGlobal.getInstance()
1413 .getRealDisplay(displayId);
1414 if (display == null) {
1415 return null;
1416 }
1417 final Point displaySize = new Point();
1418 display.getRealSize(displaySize);
1419
1420 int rotation = displayInfo.rotation;
1421 // TODO (b/153382624) : This workaround solution would be removed after
1422 // SurfaceFlinger fixes the inconsistency with rotation direction issue.
1423 if (rotation == ROTATION_90 || rotation == ROTATION_270) {
1424 rotation = (rotation == ROTATION_90) ? ROTATION_270 : ROTATION_90;
1425 }
1426
1427 return SurfaceControl.screenshotToBuffer(token, new Rect(), displaySize.x,
1428 displaySize.y, false /* useIdentityTransform */, rotation /* rotation */);
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001429 }
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001430 }
1431
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001432 @VisibleForTesting
1433 DisplayedContentSamplingAttributes getDisplayedContentSamplingAttributesInternal(
1434 int displayId) {
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001435 final IBinder token = getDisplayToken(displayId);
1436 if (token == null) {
1437 return null;
1438 }
1439 return SurfaceControl.getDisplayedContentSamplingAttributes(token);
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001440 }
1441
1442 @VisibleForTesting
1443 boolean setDisplayedContentSamplingEnabledInternal(
1444 int displayId, boolean enable, int componentMask, int maxFrames) {
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001445 final IBinder token = getDisplayToken(displayId);
1446 if (token == null) {
1447 return false;
1448 }
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001449 return SurfaceControl.setDisplayedContentSamplingEnabled(
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001450 token, enable, componentMask, maxFrames);
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001451 }
1452
1453 @VisibleForTesting
1454 DisplayedContentSample getDisplayedContentSampleInternal(int displayId,
1455 long maxFrames, long timestamp) {
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001456 final IBinder token = getDisplayToken(displayId);
1457 if (token == null) {
1458 return null;
1459 }
1460 return SurfaceControl.getDisplayedContentSample(token, maxFrames, timestamp);
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001461 }
1462
Santos Cordonc4fd8e62019-12-18 13:01:05 +00001463 void resetBrightnessConfiguration() {
1464 setBrightnessConfigurationForUserInternal(null, mContext.getUserId(),
1465 mContext.getPackageName());
1466 }
1467
1468 void setAutoBrightnessLoggingEnabled(boolean enabled) {
1469 if (mDisplayPowerController != null) {
1470 synchronized (mSyncRoot) {
1471 mDisplayPowerController.setAutoBrightnessLoggingEnabled(enabled);
1472 }
1473 }
1474 }
1475
1476 void setDisplayWhiteBalanceLoggingEnabled(boolean enabled) {
1477 if (mDisplayPowerController != null) {
1478 synchronized (mSyncRoot) {
1479 mDisplayPowerController.setDisplayWhiteBalanceLoggingEnabled(enabled);
1480 }
1481 }
1482 }
1483
1484 void setAmbientColorTemperatureOverride(float cct) {
1485 if (mDisplayPowerController != null) {
1486 synchronized (mSyncRoot) {
1487 mDisplayPowerController.setAmbientColorTemperatureOverride(cct);
1488 }
1489 }
1490 }
1491
Ana Kruleca74a8642019-11-14 00:51:00 +01001492 private void onDesiredDisplayModeSpecsChangedInternal() {
Michael Wrighta3dab232019-02-22 16:54:21 +00001493 boolean changed = false;
1494 synchronized (mSyncRoot) {
1495 final int count = mLogicalDisplays.size();
1496 for (int i = 0; i < count; i++) {
1497 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1498 int displayId = mLogicalDisplays.keyAt(i);
Ana Kruleca74a8642019-11-14 00:51:00 +01001499 DisplayModeDirector.DesiredDisplayModeSpecs desiredDisplayModeSpecs =
1500 mDisplayModeDirector.getDesiredDisplayModeSpecs(displayId);
1501 DisplayModeDirector.DesiredDisplayModeSpecs existingDesiredDisplayModeSpecs =
1502 display.getDesiredDisplayModeSpecsLocked();
1503 if (DEBUG) {
1504 Slog.i(TAG,
1505 "Comparing display specs: " + desiredDisplayModeSpecs
1506 + ", existing: " + existingDesiredDisplayModeSpecs);
1507 }
1508 if (!desiredDisplayModeSpecs.equals(existingDesiredDisplayModeSpecs)) {
1509 display.setDesiredDisplayModeSpecsLocked(desiredDisplayModeSpecs);
Michael Wrighta3dab232019-02-22 16:54:21 +00001510 changed = true;
1511 }
1512 }
1513 if (changed) {
1514 scheduleTraversalLocked(false);
1515 }
1516 }
1517 }
1518
Jeff Brownd728bf52012-09-08 18:05:28 -07001519 private void clearViewportsLocked() {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001520 mViewports.clear();
Craig Mautner722285e2012-09-07 13:55:58 -07001521 }
1522
Chris Yed6d35592020-05-01 11:36:33 -07001523 private Optional<Integer> getViewportType(DisplayDeviceInfo info) {
1524 // Get the corresponding viewport type.
1525 if ((info.flags & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
1526 return Optional.of(VIEWPORT_INTERNAL);
1527 } else if (info.touch == DisplayDeviceInfo.TOUCH_EXTERNAL) {
1528 return Optional.of(VIEWPORT_EXTERNAL);
1529 } else if (info.touch == DisplayDeviceInfo.TOUCH_VIRTUAL
1530 && !TextUtils.isEmpty(info.uniqueId)) {
1531 return Optional.of(VIEWPORT_VIRTUAL);
1532 } else {
1533 if (DEBUG) {
1534 Slog.i(TAG, "Display " + info + " does not support input device matching.");
1535 }
1536 }
1537 return Optional.empty();
1538 }
1539
Robert Carrae606b42018-02-15 15:36:23 -08001540 private void configureDisplayLocked(SurfaceControl.Transaction t, DisplayDevice device) {
Jeff Brownd14c8c92014-01-07 18:13:09 -08001541 final DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
1542 final boolean ownContent = (info.flags & DisplayDeviceInfo.FLAG_OWN_CONTENT_ONLY) != 0;
Jeff Browna506a6e2013-06-04 00:02:38 -07001543
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001544 // Find the logical display that the display device is showing.
Jeff Brownd14c8c92014-01-07 18:13:09 -08001545 // Certain displays only ever show their own content.
Craig Mautner722285e2012-09-07 13:55:58 -07001546 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
Jeff Brownd14c8c92014-01-07 18:13:09 -08001547 if (!ownContent) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001548 if (display != null && !display.hasContentLocked()) {
1549 // If the display does not have any content of its own, then
b0202.jung925435c2020-01-08 18:46:59 +09001550 // automatically mirror the requested logical display contents if possible.
1551 display = mLogicalDisplays.get(device.getDisplayIdToMirrorLocked());
Jeff Browna506a6e2013-06-04 00:02:38 -07001552 }
1553 if (display == null) {
1554 display = mLogicalDisplays.get(Display.DEFAULT_DISPLAY);
1555 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001556 }
1557
1558 // Apply the logical display configuration to the display device.
1559 if (display == null) {
1560 // TODO: no logical display for the device, blank it
Jeff Brownd728bf52012-09-08 18:05:28 -07001561 Slog.w(TAG, "Missing logical display to use for physical display device: "
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001562 + device.getDisplayDeviceInfoLocked());
Jeff Brownd728bf52012-09-08 18:05:28 -07001563 return;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001564 }
Robert Carrae606b42018-02-15 15:36:23 -08001565 display.configureDisplayLocked(t, device, info.state == Display.STATE_OFF);
Chris Yed6d35592020-05-01 11:36:33 -07001566 final Optional<Integer> viewportType = getViewportType(info);
1567 if (viewportType.isPresent()) {
1568 populateViewportLocked(viewportType.get(), display.getDisplayIdLocked(), device, info);
Jeff Brownd728bf52012-09-08 18:05:28 -07001569 }
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001570 }
1571
1572 /**
1573 * Get internal or external viewport. Create it if does not currently exist.
1574 * @param viewportType - either INTERNAL or EXTERNAL
1575 * @return the viewport with the requested type
1576 */
Arthur Hung41e81e72018-10-31 18:04:56 +08001577 private DisplayViewport getViewportLocked(int viewportType, String uniqueId) {
1578 if (viewportType != VIEWPORT_INTERNAL && viewportType != VIEWPORT_EXTERNAL
1579 && viewportType != VIEWPORT_VIRTUAL) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001580 Slog.wtf(TAG, "Cannot call getViewportByTypeLocked for type "
1581 + DisplayViewport.typeToString(viewportType));
1582 return null;
1583 }
Arthur Hung41e81e72018-10-31 18:04:56 +08001584
1585 // Only allow a single INTERNAL or EXTERNAL viewport by forcing their uniqueIds
1586 // to be identical (in particular, empty).
1587 // TODO (b/116824030) allow multiple EXTERNAL viewports and remove this function.
1588 if (viewportType != VIEWPORT_VIRTUAL) {
1589 uniqueId = "";
1590 }
1591
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001592 DisplayViewport viewport;
1593 final int count = mViewports.size();
1594 for (int i = 0; i < count; i++) {
1595 viewport = mViewports.get(i);
Arthur Hung41e81e72018-10-31 18:04:56 +08001596 if (viewport.type == viewportType && uniqueId.equals(viewport.uniqueId)) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001597 return viewport;
1598 }
1599 }
1600
Arthur Hung41e81e72018-10-31 18:04:56 +08001601 // Creates the viewport if none exists.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001602 viewport = new DisplayViewport();
1603 viewport.type = viewportType;
Arthur Hung41e81e72018-10-31 18:04:56 +08001604 viewport.uniqueId = uniqueId;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001605 mViewports.add(viewport);
1606 return viewport;
1607 }
1608
Chris Yed6d35592020-05-01 11:36:33 -07001609 private void populateViewportLocked(int viewportType, int displayId, DisplayDevice device,
1610 DisplayDeviceInfo info) {
1611 final DisplayViewport viewport = getViewportLocked(viewportType, info.uniqueId);
Jeff Brownd728bf52012-09-08 18:05:28 -07001612 device.populateViewportLocked(viewport);
Arthur Hung41e81e72018-10-31 18:04:56 +08001613 viewport.valid = true;
1614 viewport.displayId = displayId;
Chris Yed6d35592020-05-01 11:36:33 -07001615 viewport.isActive = Display.isActiveState(info.state);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001616 }
1617
1618 private LogicalDisplay findLogicalDisplayForDeviceLocked(DisplayDevice device) {
1619 final int count = mLogicalDisplays.size();
1620 for (int i = 0; i < count; i++) {
1621 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1622 if (display.getPrimaryDisplayDeviceLocked() == device) {
1623 return display;
1624 }
1625 }
1626 return null;
1627 }
1628
Jeff Brownbd6e1502012-08-28 03:27:37 -07001629 private void sendDisplayEventLocked(int displayId, int event) {
1630 Message msg = mHandler.obtainMessage(MSG_DELIVER_DISPLAY_EVENT, displayId, event);
1631 mHandler.sendMessage(msg);
1632 }
1633
Robert Carrae606b42018-02-15 15:36:23 -08001634 // Requests that performTraversals be called at a
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001635 // later time to apply changes to surfaces and displays.
Craig Mautner65d11b32012-10-01 13:59:52 -07001636 private void scheduleTraversalLocked(boolean inTraversal) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001637 if (!mPendingTraversal && mWindowManagerInternal != null) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001638 mPendingTraversal = true;
Craig Mautner65d11b32012-10-01 13:59:52 -07001639 if (!inTraversal) {
1640 mHandler.sendEmptyMessage(MSG_REQUEST_TRAVERSAL);
1641 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001642 }
1643 }
1644
1645 // Runs on Handler thread.
1646 // Delivers display event notifications to callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001647 private void deliverDisplayEvent(int displayId, int event) {
1648 if (DEBUG) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001649 Slog.d(TAG, "Delivering display event: displayId="
1650 + displayId + ", event=" + event);
Jeff Brownfa25bf52012-07-23 19:26:30 -07001651 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07001652
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001653 // Grab the lock and copy the callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001654 final int count;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001655 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001656 count = mCallbacks.size();
1657 mTempCallbacks.clear();
1658 for (int i = 0; i < count; i++) {
1659 mTempCallbacks.add(mCallbacks.valueAt(i));
Craig Mautner4f67ba62012-08-02 11:23:00 -07001660 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001661 }
Craig Mautner4f67ba62012-08-02 11:23:00 -07001662
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001663 // After releasing the lock, send the notifications out.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001664 for (int i = 0; i < count; i++) {
1665 mTempCallbacks.get(i).notifyDisplayEventAsync(displayId, event);
1666 }
1667 mTempCallbacks.clear();
Craig Mautner4f67ba62012-08-02 11:23:00 -07001668 }
1669
Michael Wrightc39d47a2014-07-08 18:07:36 -07001670 private IMediaProjectionManager getProjectionService() {
1671 if (mProjectionService == null) {
1672 IBinder b = ServiceManager.getService(Context.MEDIA_PROJECTION_SERVICE);
1673 mProjectionService = IMediaProjectionManager.Stub.asInterface(b);
1674 }
1675 return mProjectionService;
1676 }
1677
Michael Wrighteef0e132017-11-21 17:57:52 +00001678 private UserManager getUserManager() {
1679 return mContext.getSystemService(UserManager.class);
1680 }
1681
Jeff Brown4ccb8232014-01-16 22:16:42 -08001682 private void dumpInternal(PrintWriter pw) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001683 pw.println("DISPLAY MANAGER (dumpsys display)");
Jeff Brownfa25bf52012-07-23 19:26:30 -07001684
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001685 synchronized (mSyncRoot) {
Jeff Brown9e316a12012-10-08 19:17:06 -07001686 pw.println(" mOnlyCode=" + mOnlyCore);
1687 pw.println(" mSafeMode=" + mSafeMode);
1688 pw.println(" mPendingTraversal=" + mPendingTraversal);
Jeff Brown037c33e2014-04-09 00:31:55 -07001689 pw.println(" mGlobalDisplayState=" + Display.stateToString(mGlobalDisplayState));
Jeff Brown9e316a12012-10-08 19:17:06 -07001690 pw.println(" mNextNonDefaultDisplayId=" + mNextNonDefaultDisplayId);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001691 pw.println(" mViewports=" + mViewports);
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -07001692 pw.println(" mDefaultDisplayDefaultColorMode=" + mDefaultDisplayDefaultColorMode);
Jeff Brown27f1d672012-10-17 18:32:34 -07001693 pw.println(" mSingleDisplayDemoMode=" + mSingleDisplayDemoMode);
Jeff Brownce468a32013-11-21 16:42:03 -08001694 pw.println(" mWifiDisplayScanRequestCount=" + mWifiDisplayScanRequestCount);
Michael Wrighteedcbf12017-08-16 23:14:54 +01001695 pw.println(" mStableDisplaySize=" + mStableDisplaySize);
Kenny Guy2047db92019-02-21 13:04:05 +00001696 pw.println(" mMinimumBrightnessCurve=" + mMinimumBrightnessCurve);
Jeff Brown9e316a12012-10-08 19:17:06 -07001697
Jeff Brownbd6e1502012-08-28 03:27:37 -07001698 IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001699 ipw.increaseIndent();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001700
1701 pw.println();
1702 pw.println("Display Adapters: size=" + mDisplayAdapters.size());
Jeff Brown848c2dc2012-08-19 20:18:08 -07001703 for (DisplayAdapter adapter : mDisplayAdapters) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001704 pw.println(" " + adapter.getName());
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001705 adapter.dumpLocked(ipw);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001706 }
Craig Mautner9de49362012-08-02 14:30:30 -07001707
Jeff Brownbd6e1502012-08-28 03:27:37 -07001708 pw.println();
1709 pw.println("Display Devices: size=" + mDisplayDevices.size());
1710 for (DisplayDevice device : mDisplayDevices) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001711 pw.println(" " + device.getDisplayDeviceInfoLocked());
1712 device.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001713 }
1714
1715 final int logicalDisplayCount = mLogicalDisplays.size();
1716 pw.println();
1717 pw.println("Logical Displays: size=" + logicalDisplayCount);
1718 for (int i = 0; i < logicalDisplayCount; i++) {
1719 int displayId = mLogicalDisplays.keyAt(i);
1720 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1721 pw.println(" Display " + displayId + ":");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001722 display.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001723 }
Jeff Brownce468a32013-11-21 16:42:03 -08001724
Michael Wrighta3dab232019-02-22 16:54:21 +00001725 pw.println();
1726 mDisplayModeDirector.dump(pw);
1727
Jeff Brownce468a32013-11-21 16:42:03 -08001728 final int callbackCount = mCallbacks.size();
1729 pw.println();
1730 pw.println("Callbacks: size=" + callbackCount);
1731 for (int i = 0; i < callbackCount; i++) {
1732 CallbackRecord callback = mCallbacks.valueAt(i);
1733 pw.println(" " + i + ": mPid=" + callback.mPid
1734 + ", mWifiDisplayScanRequested=" + callback.mWifiDisplayScanRequested);
1735 }
Jeff Brownad9ef192014-04-08 17:26:30 -07001736
1737 if (mDisplayPowerController != null) {
1738 mDisplayPowerController.dump(pw);
1739 }
Michael Wright1c9977b2016-07-12 13:30:10 -07001740
1741 pw.println();
1742 mPersistentDataStore.dump(pw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001743 }
1744 }
1745
Dan Gittik122df862018-03-28 16:59:22 +01001746 private static float[] getFloatArray(TypedArray array) {
1747 int length = array.length();
1748 float[] floatArray = new float[length];
1749 for (int i = 0; i < length; i++) {
1750 floatArray[i] = array.getFloat(i, Float.NaN);
1751 }
1752 array.recycle();
1753 return floatArray;
1754 }
1755
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001756 /**
1757 * This is the object that everything in the display manager locks on.
1758 * We make it an inner class within the {@link DisplayManagerService} to so that it is
1759 * clear that the object belongs to the display manager service and that it is
1760 * a unique object with a special purpose.
1761 */
1762 public static final class SyncRoot {
1763 }
1764
Santos Cordonee8931e2017-04-05 10:31:15 -07001765 @VisibleForTesting
1766 static class Injector {
1767 VirtualDisplayAdapter getVirtualDisplayAdapter(SyncRoot syncRoot, Context context,
1768 Handler handler, DisplayAdapter.Listener displayAdapterListener) {
1769 return new VirtualDisplayAdapter(syncRoot, context, handler, displayAdapterListener);
1770 }
Santos Cordonc22c5632017-06-21 16:03:49 -07001771
1772 long getDefaultDisplayDelayTimeout() {
1773 return WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT;
1774 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001775 }
1776
Alex Sakhartchouk879d24f2017-06-20 22:01:19 -04001777 @VisibleForTesting
1778 DisplayDeviceInfo getDisplayDeviceInfoInternal(int displayId) {
1779 synchronized (mSyncRoot) {
1780 LogicalDisplay display = mLogicalDisplays.get(displayId);
1781 if (display != null) {
1782 DisplayDevice displayDevice = display.getPrimaryDisplayDeviceLocked();
1783 return displayDevice.getDisplayDeviceInfoLocked();
1784 }
1785 return null;
1786 }
1787 }
1788
b0202.jung925435c2020-01-08 18:46:59 +09001789 @VisibleForTesting
1790 int getDisplayIdToMirrorInternal(int displayId) {
1791 synchronized (mSyncRoot) {
1792 LogicalDisplay display = mLogicalDisplays.get(displayId);
1793 if (display != null) {
1794 DisplayDevice displayDevice = display.getPrimaryDisplayDeviceLocked();
1795 return displayDevice.getDisplayIdToMirrorLocked();
1796 }
1797 return Display.INVALID_DISPLAY;
1798 }
1799 }
1800
1801 @VisibleForTesting
1802 Surface getVirtualDisplaySurfaceInternal(IBinder appToken) {
1803 synchronized (mSyncRoot) {
1804 if (mVirtualDisplayAdapter == null) {
1805 return null;
1806 }
1807 return mVirtualDisplayAdapter.getVirtualDisplaySurfaceLocked(appToken);
1808 }
1809 }
1810
Jeff Brownbd6e1502012-08-28 03:27:37 -07001811 private final class DisplayManagerHandler extends Handler {
1812 public DisplayManagerHandler(Looper looper) {
1813 super(looper, null, true /*async*/);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001814 }
Jeff Brownbf5740e2012-08-19 23:20:02 -07001815
Jeff Brownbd6e1502012-08-28 03:27:37 -07001816 @Override
1817 public void handleMessage(Message msg) {
1818 switch (msg.what) {
Santos Cordonc22c5632017-06-21 16:03:49 -07001819 case MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS:
1820 registerDefaultDisplayAdapters();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001821 break;
1822
1823 case MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS:
1824 registerAdditionalDisplayAdapters();
1825 break;
1826
1827 case MSG_DELIVER_DISPLAY_EVENT:
1828 deliverDisplayEvent(msg.arg1, msg.arg2);
1829 break;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001830
1831 case MSG_REQUEST_TRAVERSAL:
Jeff Brown4ccb8232014-01-16 22:16:42 -08001832 mWindowManagerInternal.requestTraversalFromDisplayManager();
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001833 break;
Jeff Brownd728bf52012-09-08 18:05:28 -07001834
1835 case MSG_UPDATE_VIEWPORT: {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001836 final boolean changed;
Jeff Brownd728bf52012-09-08 18:05:28 -07001837 synchronized (mSyncRoot) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001838 changed = !mTempViewports.equals(mViewports);
1839 if (changed) {
1840 mTempViewports.clear();
1841 for (DisplayViewport d : mViewports) {
1842 mTempViewports.add(d.makeCopy());
1843 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001844 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001845 }
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001846 if (changed) {
1847 mInputManagerInternal.setDisplayViewports(mTempViewports);
1848 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001849 break;
1850 }
Kenny Guy22bd0442017-10-26 00:15:54 +01001851
Michael Wrighteef0e132017-11-21 17:57:52 +00001852 case MSG_LOAD_BRIGHTNESS_CONFIGURATION:
1853 loadBrightnessConfiguration();
1854 break;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001855 }
1856 }
1857 }
1858
1859 private final class DisplayAdapterListener implements DisplayAdapter.Listener {
1860 @Override
1861 public void onDisplayDeviceEvent(DisplayDevice device, int event) {
1862 switch (event) {
1863 case DisplayAdapter.DISPLAY_DEVICE_EVENT_ADDED:
1864 handleDisplayDeviceAdded(device);
1865 break;
1866
1867 case DisplayAdapter.DISPLAY_DEVICE_EVENT_CHANGED:
1868 handleDisplayDeviceChanged(device);
1869 break;
1870
1871 case DisplayAdapter.DISPLAY_DEVICE_EVENT_REMOVED:
1872 handleDisplayDeviceRemoved(device);
1873 break;
1874 }
1875 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001876
1877 @Override
1878 public void onTraversalRequested() {
1879 synchronized (mSyncRoot) {
Craig Mautner65d11b32012-10-01 13:59:52 -07001880 scheduleTraversalLocked(false);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001881 }
1882 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001883 }
1884
1885 private final class CallbackRecord implements DeathRecipient {
Jeff Brownce468a32013-11-21 16:42:03 -08001886 public final int mPid;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001887 private final IDisplayManagerCallback mCallback;
1888
Jeff Brownce468a32013-11-21 16:42:03 -08001889 public boolean mWifiDisplayScanRequested;
1890
Jeff Brownbd6e1502012-08-28 03:27:37 -07001891 public CallbackRecord(int pid, IDisplayManagerCallback callback) {
1892 mPid = pid;
1893 mCallback = callback;
1894 }
1895
1896 @Override
1897 public void binderDied() {
1898 if (DEBUG) {
1899 Slog.d(TAG, "Display listener for pid " + mPid + " died.");
1900 }
Jeff Brownce468a32013-11-21 16:42:03 -08001901 onCallbackDied(this);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001902 }
1903
1904 public void notifyDisplayEventAsync(int displayId, int event) {
1905 try {
1906 mCallback.onDisplayEvent(displayId, event);
1907 } catch (RemoteException ex) {
1908 Slog.w(TAG, "Failed to notify process "
1909 + mPid + " that displays changed, assuming it died.", ex);
1910 binderDied();
1911 }
1912 }
1913 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001914
Santos Cordonee8931e2017-04-05 10:31:15 -07001915 @VisibleForTesting
1916 final class BinderService extends IDisplayManager.Stub {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001917 /**
1918 * Returns information about the specified logical display.
1919 *
1920 * @param displayId The logical display id.
lumarkec75b422019-01-07 15:58:38 +08001921 * @return The logical display info, return {@code null} if the display does not exist or
1922 * the calling UID isn't present on the display. The returned object must be treated as
1923 * immutable.
Jeff Brown4ccb8232014-01-16 22:16:42 -08001924 */
1925 @Override // Binder call
1926 public DisplayInfo getDisplayInfo(int displayId) {
1927 final int callingUid = Binder.getCallingUid();
1928 final long token = Binder.clearCallingIdentity();
1929 try {
1930 return getDisplayInfoInternal(displayId, callingUid);
1931 } finally {
1932 Binder.restoreCallingIdentity(token);
1933 }
1934 }
1935
1936 /**
1937 * Returns the list of all display ids.
1938 */
1939 @Override // Binder call
1940 public int[] getDisplayIds() {
1941 final int callingUid = Binder.getCallingUid();
1942 final long token = Binder.clearCallingIdentity();
1943 try {
1944 return getDisplayIdsInternal(callingUid);
1945 } finally {
1946 Binder.restoreCallingIdentity(token);
1947 }
1948 }
1949
lumarkec75b422019-01-07 15:58:38 +08001950 @Override // Binder call
1951 public boolean isUidPresentOnDisplay(int uid, int displayId) {
1952 final long token = Binder.clearCallingIdentity();
1953 try {
1954 return isUidPresentOnDisplayInternal(uid, displayId);
1955 } finally {
1956 Binder.restoreCallingIdentity(token);
1957 }
1958 }
1959
Michael Wrighteedcbf12017-08-16 23:14:54 +01001960 /**
1961 * Returns the stable device display size, in pixels.
1962 */
1963 @Override // Binder call
1964 public Point getStableDisplaySize() {
1965 final long token = Binder.clearCallingIdentity();
1966 try {
1967 return getStableDisplaySizeInternal();
1968 } finally {
1969 Binder.restoreCallingIdentity(token);
1970 }
1971 }
1972
Jeff Brown4ccb8232014-01-16 22:16:42 -08001973 @Override // Binder call
1974 public void registerCallback(IDisplayManagerCallback callback) {
1975 if (callback == null) {
1976 throw new IllegalArgumentException("listener must not be null");
1977 }
1978
1979 final int callingPid = Binder.getCallingPid();
1980 final long token = Binder.clearCallingIdentity();
1981 try {
1982 registerCallbackInternal(callback, callingPid);
1983 } finally {
1984 Binder.restoreCallingIdentity(token);
1985 }
1986 }
1987
1988 @Override // Binder call
1989 public void startWifiDisplayScan() {
1990 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1991 "Permission required to start wifi display scans");
1992
1993 final int callingPid = Binder.getCallingPid();
1994 final long token = Binder.clearCallingIdentity();
1995 try {
1996 startWifiDisplayScanInternal(callingPid);
1997 } finally {
1998 Binder.restoreCallingIdentity(token);
1999 }
2000 }
2001
2002 @Override // Binder call
2003 public void stopWifiDisplayScan() {
2004 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
2005 "Permission required to stop wifi display scans");
2006
2007 final int callingPid = Binder.getCallingPid();
2008 final long token = Binder.clearCallingIdentity();
2009 try {
2010 stopWifiDisplayScanInternal(callingPid);
2011 } finally {
2012 Binder.restoreCallingIdentity(token);
2013 }
2014 }
2015
2016 @Override // Binder call
2017 public void connectWifiDisplay(String address) {
2018 if (address == null) {
2019 throw new IllegalArgumentException("address must not be null");
2020 }
2021 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
2022 "Permission required to connect to a wifi display");
2023
2024 final long token = Binder.clearCallingIdentity();
2025 try {
2026 connectWifiDisplayInternal(address);
2027 } finally {
2028 Binder.restoreCallingIdentity(token);
2029 }
2030 }
2031
2032 @Override // Binder call
2033 public void disconnectWifiDisplay() {
2034 // This request does not require special permissions.
2035 // Any app can request disconnection from the currently active wifi display.
2036 // This exception should no longer be needed once wifi display control moves
2037 // to the media router service.
2038
2039 final long token = Binder.clearCallingIdentity();
2040 try {
2041 disconnectWifiDisplayInternal();
2042 } finally {
2043 Binder.restoreCallingIdentity(token);
2044 }
2045 }
2046
2047 @Override // Binder call
2048 public void renameWifiDisplay(String address, String alias) {
2049 if (address == null) {
2050 throw new IllegalArgumentException("address must not be null");
2051 }
2052 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
2053 "Permission required to rename to a wifi display");
2054
2055 final long token = Binder.clearCallingIdentity();
2056 try {
2057 renameWifiDisplayInternal(address, alias);
2058 } finally {
2059 Binder.restoreCallingIdentity(token);
2060 }
2061 }
2062
2063 @Override // Binder call
2064 public void forgetWifiDisplay(String address) {
2065 if (address == null) {
2066 throw new IllegalArgumentException("address must not be null");
2067 }
2068 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
2069 "Permission required to forget to a wifi display");
2070
2071 final long token = Binder.clearCallingIdentity();
2072 try {
2073 forgetWifiDisplayInternal(address);
2074 } finally {
2075 Binder.restoreCallingIdentity(token);
2076 }
2077 }
2078
2079 @Override // Binder call
2080 public void pauseWifiDisplay() {
2081 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
2082 "Permission required to pause a wifi display session");
2083
2084 final long token = Binder.clearCallingIdentity();
2085 try {
2086 pauseWifiDisplayInternal();
2087 } finally {
2088 Binder.restoreCallingIdentity(token);
2089 }
2090 }
2091
2092 @Override // Binder call
2093 public void resumeWifiDisplay() {
2094 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
2095 "Permission required to resume a wifi display session");
2096
2097 final long token = Binder.clearCallingIdentity();
2098 try {
2099 resumeWifiDisplayInternal();
2100 } finally {
2101 Binder.restoreCallingIdentity(token);
2102 }
2103 }
2104
2105 @Override // Binder call
2106 public WifiDisplayStatus getWifiDisplayStatus() {
2107 // This request does not require special permissions.
2108 // Any app can get information about available wifi displays.
2109
2110 final long token = Binder.clearCallingIdentity();
2111 try {
2112 return getWifiDisplayStatusInternal();
2113 } finally {
2114 Binder.restoreCallingIdentity(token);
2115 }
2116 }
2117
2118 @Override // Binder call
Michael Wright1c9977b2016-07-12 13:30:10 -07002119 public void requestColorMode(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +01002120 mContext.enforceCallingOrSelfPermission(
Michael Wright1c9977b2016-07-12 13:30:10 -07002121 Manifest.permission.CONFIGURE_DISPLAY_COLOR_MODE,
2122 "Permission required to change the display color mode");
Michael Wright58e829f2015-09-15 00:13:26 +01002123 final long token = Binder.clearCallingIdentity();
2124 try {
Michael Wright1c9977b2016-07-12 13:30:10 -07002125 requestColorModeInternal(displayId, colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +01002126 } finally {
2127 Binder.restoreCallingIdentity(token);
2128 }
2129 }
2130
2131 @Override // Binder call
b0202.jung925435c2020-01-08 18:46:59 +09002132 public int createVirtualDisplay(VirtualDisplayConfig virtualDisplayConfig,
2133 IVirtualDisplayCallback callback, IMediaProjection projection, String packageName) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08002134 final int callingUid = Binder.getCallingUid();
2135 if (!validatePackageName(callingUid, packageName)) {
2136 throw new SecurityException("packageName must match the calling uid");
2137 }
Michael Wright75ee9fc2014-09-01 19:55:22 -07002138 if (callback == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08002139 throw new IllegalArgumentException("appToken must not be null");
2140 }
b0202.jung925435c2020-01-08 18:46:59 +09002141 if (virtualDisplayConfig == null) {
2142 throw new IllegalArgumentException("virtualDisplayConfig must not be null");
Jeff Brown4ccb8232014-01-16 22:16:42 -08002143 }
b0202.jung925435c2020-01-08 18:46:59 +09002144 final Surface surface = virtualDisplayConfig.getSurface();
2145 int flags = virtualDisplayConfig.getFlags();
2146
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07002147 if (surface != null && surface.isSingleBuffered()) {
Pablo Ceballosaff2f942016-07-29 14:49:55 -07002148 throw new IllegalArgumentException("Surface can't be single-buffered");
2149 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07002150
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002151 if ((flags & VIRTUAL_DISPLAY_FLAG_PUBLIC) != 0) {
2152 flags |= VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
2153
2154 // Public displays can't be allowed to show content when locked.
Andrii Kulian7211d2e2017-01-27 15:58:05 -08002155 if ((flags & VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD) != 0) {
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002156 throw new IllegalArgumentException(
2157 "Public display must not be marked as SHOW_WHEN_LOCKED_INSECURE");
2158 }
Michael Wright6720be42014-07-29 19:14:16 -07002159 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002160 if ((flags & VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY) != 0) {
2161 flags &= ~VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
Michael Wright6720be42014-07-29 19:14:16 -07002162 }
2163
Michael Wrightc39d47a2014-07-08 18:07:36 -07002164 if (projection != null) {
2165 try {
2166 if (!getProjectionService().isValidMediaProjection(projection)) {
2167 throw new SecurityException("Invalid media projection");
2168 }
Michael Wright6720be42014-07-29 19:14:16 -07002169 flags = projection.applyVirtualDisplayFlags(flags);
Michael Wrightc39d47a2014-07-08 18:07:36 -07002170 } catch (RemoteException e) {
Michael Wright6720be42014-07-29 19:14:16 -07002171 throw new SecurityException("unable to validate media projection or flags");
Michael Wrightc39d47a2014-07-08 18:07:36 -07002172 }
2173 }
2174
Michael Wright6720be42014-07-29 19:14:16 -07002175 if (callingUid != Process.SYSTEM_UID &&
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002176 (flags & VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07002177 if (!canProjectVideo(projection)) {
2178 throw new SecurityException("Requires CAPTURE_VIDEO_OUTPUT or "
2179 + "CAPTURE_SECURE_VIDEO_OUTPUT permission, or an appropriate "
2180 + "MediaProjection token in order to create a screen sharing virtual "
2181 + "display.");
2182 }
2183 }
Santos Cordonb6992f22018-01-30 14:51:20 -08002184 if (callingUid != Process.SYSTEM_UID && (flags & VIRTUAL_DISPLAY_FLAG_SECURE) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07002185 if (!canProjectSecureVideo(projection)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08002186 throw new SecurityException("Requires CAPTURE_SECURE_VIDEO_OUTPUT "
Michael Wrightc39d47a2014-07-08 18:07:36 -07002187 + "or an appropriate MediaProjection token to create a "
2188 + "secure virtual display.");
Jeff Brown4ccb8232014-01-16 22:16:42 -08002189 }
2190 }
2191
Chilun67a379b2019-04-11 19:49:42 +08002192 // Sometimes users can have sensitive information in system decoration windows. An app
2193 // could create a virtual display with system decorations support and read the user info
2194 // from the surface.
2195 // We should only allow adding flag VIRTUAL_DISPLAY_FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS
2196 // to virtual displays that are owned by the system.
2197 if (callingUid != Process.SYSTEM_UID
2198 && (flags & VIRTUAL_DISPLAY_FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS) != 0) {
2199 if (!checkCallingPermission(INTERNAL_SYSTEM_WINDOW, "createVirtualDisplay()")) {
2200 throw new SecurityException("Requires INTERNAL_SYSTEM_WINDOW permission");
2201 }
2202 }
2203
Jeff Brown4ccb8232014-01-16 22:16:42 -08002204 final long token = Binder.clearCallingIdentity();
2205 try {
Santos Cordonee8931e2017-04-05 10:31:15 -07002206 return createVirtualDisplayInternal(callback, projection, callingUid, packageName,
b0202.jung925435c2020-01-08 18:46:59 +09002207 surface, flags, virtualDisplayConfig);
Jeff Brown4ccb8232014-01-16 22:16:42 -08002208 } finally {
2209 Binder.restoreCallingIdentity(token);
2210 }
2211 }
2212
2213 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07002214 public void resizeVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wright01e840f2014-06-26 16:03:25 -07002215 int width, int height, int densityDpi) {
Jeff Chang01baf6c2019-08-20 16:21:25 +08002216 if (width <= 0 || height <= 0 || densityDpi <= 0) {
2217 throw new IllegalArgumentException("width, height, and densityDpi must be "
2218 + "greater than 0");
2219 }
Michael Wright01e840f2014-06-26 16:03:25 -07002220 final long token = Binder.clearCallingIdentity();
2221 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07002222 resizeVirtualDisplayInternal(callback.asBinder(), width, height, densityDpi);
Michael Wright01e840f2014-06-26 16:03:25 -07002223 } finally {
2224 Binder.restoreCallingIdentity(token);
2225 }
2226 }
2227
2228 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07002229 public void setVirtualDisplaySurface(IVirtualDisplayCallback callback, Surface surface) {
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07002230 if (surface != null && surface.isSingleBuffered()) {
2231 throw new IllegalArgumentException("Surface can't be single-buffered");
2232 }
Jeff Brown92207df2014-04-16 13:16:07 -07002233 final long token = Binder.clearCallingIdentity();
2234 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07002235 setVirtualDisplaySurfaceInternal(callback.asBinder(), surface);
Jeff Brown92207df2014-04-16 13:16:07 -07002236 } finally {
2237 Binder.restoreCallingIdentity(token);
2238 }
2239 }
2240
2241 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07002242 public void releaseVirtualDisplay(IVirtualDisplayCallback callback) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08002243 final long token = Binder.clearCallingIdentity();
2244 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07002245 releaseVirtualDisplayInternal(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -08002246 } finally {
2247 Binder.restoreCallingIdentity(token);
2248 }
2249 }
2250
2251 @Override // Binder call
chaviwda4c6942018-11-07 15:52:56 -08002252 public void setVirtualDisplayState(IVirtualDisplayCallback callback, boolean isOn) {
2253 final long token = Binder.clearCallingIdentity();
2254 try {
2255 setVirtualDisplayStateInternal(callback.asBinder(), isOn);
2256 } finally {
2257 Binder.restoreCallingIdentity(token);
2258 }
2259 }
2260
2261 @Override // Binder call
Jeff Brown4ccb8232014-01-16 22:16:42 -08002262 public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06002263 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
Jeff Brown4ccb8232014-01-16 22:16:42 -08002264
2265 final long token = Binder.clearCallingIdentity();
2266 try {
2267 dumpInternal(pw);
2268 } finally {
2269 Binder.restoreCallingIdentity(token);
2270 }
2271 }
2272
Kenny Guy22bd0442017-10-26 00:15:54 +01002273 @Override // Binder call
Kenny Guy29aa30e2017-11-30 13:43:46 +00002274 public ParceledListSlice<BrightnessChangeEvent> getBrightnessEvents(String callingPackage) {
Kenny Guy22bd0442017-10-26 00:15:54 +01002275 mContext.enforceCallingOrSelfPermission(
2276 Manifest.permission.BRIGHTNESS_SLIDER_USAGE,
2277 "Permission to read brightness events.");
Kenny Guy29aa30e2017-11-30 13:43:46 +00002278
2279 final int callingUid = Binder.getCallingUid();
2280 AppOpsManager appOpsManager = mContext.getSystemService(AppOpsManager.class);
Jeff Sharkeyd9311192018-04-16 16:50:34 +00002281 final int mode = appOpsManager.noteOp(AppOpsManager.OP_GET_USAGE_STATS,
Kenny Guy29aa30e2017-11-30 13:43:46 +00002282 callingUid, callingPackage);
2283 final boolean hasUsageStats;
2284 if (mode == AppOpsManager.MODE_DEFAULT) {
2285 // The default behavior here is to check if PackageManager has given the app
2286 // permission.
2287 hasUsageStats = mContext.checkCallingPermission(
2288 Manifest.permission.PACKAGE_USAGE_STATS)
2289 == PackageManager.PERMISSION_GRANTED;
2290 } else {
2291 hasUsageStats = mode == AppOpsManager.MODE_ALLOWED;
2292 }
2293
2294 final int userId = UserHandle.getUserId(callingUid);
Kenny Guy22bd0442017-10-26 00:15:54 +01002295 final long token = Binder.clearCallingIdentity();
2296 try {
Michael Wright144aac92017-12-21 18:37:41 +00002297 synchronized (mSyncRoot) {
2298 return mDisplayPowerController.getBrightnessEvents(userId, hasUsageStats);
2299 }
Kenny Guy22bd0442017-10-26 00:15:54 +01002300 } finally {
2301 Binder.restoreCallingIdentity(token);
2302 }
2303 }
2304
Michael Wrighteef0e132017-11-21 17:57:52 +00002305 @Override // Binder call
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002306 public ParceledListSlice<AmbientBrightnessDayStats> getAmbientBrightnessStats() {
2307 mContext.enforceCallingOrSelfPermission(
2308 Manifest.permission.ACCESS_AMBIENT_LIGHT_STATS,
2309 "Permission required to to access ambient light stats.");
2310 final int callingUid = Binder.getCallingUid();
2311 final int userId = UserHandle.getUserId(callingUid);
2312 final long token = Binder.clearCallingIdentity();
2313 try {
2314 synchronized (mSyncRoot) {
2315 return mDisplayPowerController.getAmbientBrightnessStats(userId);
2316 }
2317 } finally {
2318 Binder.restoreCallingIdentity(token);
2319 }
2320 }
2321
2322 @Override // Binder call
Michael Wrighteef0e132017-11-21 17:57:52 +00002323 public void setBrightnessConfigurationForUser(
Kenny Guy05ce8092018-01-17 13:44:20 +00002324 BrightnessConfiguration c, @UserIdInt int userId, String packageName) {
Michael Wrighteef0e132017-11-21 17:57:52 +00002325 mContext.enforceCallingOrSelfPermission(
2326 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
2327 "Permission required to change the display's brightness configuration");
2328 if (userId != UserHandle.getCallingUserId()) {
2329 mContext.enforceCallingOrSelfPermission(
2330 Manifest.permission.INTERACT_ACROSS_USERS,
2331 "Permission required to change the display brightness"
2332 + " configuration of another user");
2333 }
Kenny Guy05ce8092018-01-17 13:44:20 +00002334 if (packageName != null && !validatePackageName(getCallingUid(), packageName)) {
2335 packageName = null;
2336 }
Michael Wrighteef0e132017-11-21 17:57:52 +00002337 final long token = Binder.clearCallingIdentity();
2338 try {
Kenny Guy05ce8092018-01-17 13:44:20 +00002339 setBrightnessConfigurationForUserInternal(c, userId, packageName);
Michael Wrighteef0e132017-11-21 17:57:52 +00002340 } finally {
2341 Binder.restoreCallingIdentity(token);
2342 }
2343 }
2344
Michael Wrightd8460232018-01-16 18:04:59 +00002345 @Override // Binder call
Kenny Guy6d1009f2018-03-14 14:28:23 +00002346 public BrightnessConfiguration getBrightnessConfigurationForUser(int userId) {
2347 mContext.enforceCallingOrSelfPermission(
2348 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
2349 "Permission required to read the display's brightness configuration");
2350 if (userId != UserHandle.getCallingUserId()) {
2351 mContext.enforceCallingOrSelfPermission(
2352 Manifest.permission.INTERACT_ACROSS_USERS,
2353 "Permission required to read the display brightness"
2354 + " configuration of another user");
2355 }
2356 final long token = Binder.clearCallingIdentity();
2357 try {
2358 final int userSerial = getUserManager().getUserSerialNumber(userId);
2359 synchronized (mSyncRoot) {
2360 BrightnessConfiguration config =
2361 mPersistentDataStore.getBrightnessConfiguration(userSerial);
2362 if (config == null) {
2363 config = mDisplayPowerController.getDefaultBrightnessConfiguration();
2364 }
2365 return config;
2366 }
2367 } finally {
2368 Binder.restoreCallingIdentity(token);
2369 }
2370 }
2371
2372 @Override // Binder call
2373 public BrightnessConfiguration getDefaultBrightnessConfiguration() {
2374 mContext.enforceCallingOrSelfPermission(
2375 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
2376 "Permission required to read the display's default brightness configuration");
2377 final long token = Binder.clearCallingIdentity();
2378 try {
2379 synchronized (mSyncRoot) {
2380 return mDisplayPowerController.getDefaultBrightnessConfiguration();
2381 }
2382 } finally {
2383 Binder.restoreCallingIdentity(token);
2384 }
2385 }
2386
2387 @Override // Binder call
Galia Peycheva35f900a2020-01-09 14:31:05 +01002388 public boolean isMinimalPostProcessingRequested(int displayId) {
2389 synchronized (mSyncRoot) {
2390 return mLogicalDisplays.get(displayId).getRequestedMinimalPostProcessingLocked();
2391 }
2392 }
2393
2394 @Override // Binder call
Fiona Campbelld4eb2952019-11-04 17:19:56 +00002395 public void setTemporaryBrightness(float brightness) {
Michael Wrightd8460232018-01-16 18:04:59 +00002396 mContext.enforceCallingOrSelfPermission(
2397 Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS,
2398 "Permission required to set the display's brightness");
2399 final long token = Binder.clearCallingIdentity();
2400 try {
2401 synchronized (mSyncRoot) {
2402 mDisplayPowerController.setTemporaryBrightness(brightness);
2403 }
2404 } finally {
2405 Binder.restoreCallingIdentity(token);
2406 }
2407 }
2408
2409 @Override // Binder call
2410 public void setTemporaryAutoBrightnessAdjustment(float adjustment) {
2411 mContext.enforceCallingOrSelfPermission(
2412 Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS,
2413 "Permission required to set the display's auto brightness adjustment");
2414 final long token = Binder.clearCallingIdentity();
2415 try {
2416 synchronized (mSyncRoot) {
2417 mDisplayPowerController.setTemporaryAutoBrightnessAdjustment(adjustment);
2418 }
2419 } finally {
2420 Binder.restoreCallingIdentity(token);
2421 }
2422 }
2423
Dan Gittik7a32fba2018-03-28 12:19:38 +01002424 @Override // Binder call
2425 public void onShellCommand(FileDescriptor in, FileDescriptor out,
2426 FileDescriptor err, String[] args, ShellCallback callback,
2427 ResultReceiver resultReceiver) {
Santos Cordonc4fd8e62019-12-18 13:01:05 +00002428 new DisplayManagerShellCommand(DisplayManagerService.this).exec(this, in, out, err,
2429 args, callback, resultReceiver);
Dan Gittik7a32fba2018-03-28 12:19:38 +01002430 }
2431
Dan Gittik122df862018-03-28 16:59:22 +01002432 @Override // Binder call
2433 public Curve getMinimumBrightnessCurve() {
2434 final long token = Binder.clearCallingIdentity();
2435 try {
2436 return getMinimumBrightnessCurveInternal();
2437 } finally {
2438 Binder.restoreCallingIdentity(token);
2439 }
2440 }
2441
Peiyong Lin277eaff2019-01-16 16:18:22 -08002442 @Override // Binder call
2443 public int getPreferredWideGamutColorSpaceId() {
2444 final long token = Binder.clearCallingIdentity();
2445 try {
2446 return getPreferredWideGamutColorSpaceIdInternal();
2447 } finally {
2448 Binder.restoreCallingIdentity(token);
2449 }
2450 }
2451
Jeff Brown4ccb8232014-01-16 22:16:42 -08002452 private boolean validatePackageName(int uid, String packageName) {
2453 if (packageName != null) {
2454 String[] packageNames = mContext.getPackageManager().getPackagesForUid(uid);
2455 if (packageNames != null) {
2456 for (String n : packageNames) {
2457 if (n.equals(packageName)) {
2458 return true;
2459 }
2460 }
2461 }
2462 }
2463 return false;
2464 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07002465
2466 private boolean canProjectVideo(IMediaProjection projection) {
2467 if (projection != null) {
2468 try {
2469 if (projection.canProjectVideo()) {
2470 return true;
2471 }
2472 } catch (RemoteException e) {
2473 Slog.e(TAG, "Unable to query projection service for permissions", e);
2474 }
2475 }
Chilun67a379b2019-04-11 19:49:42 +08002476 if (checkCallingPermission(CAPTURE_VIDEO_OUTPUT, "canProjectVideo()")) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07002477 return true;
2478 }
2479 return canProjectSecureVideo(projection);
2480 }
2481
2482 private boolean canProjectSecureVideo(IMediaProjection projection) {
2483 if (projection != null) {
2484 try {
2485 if (projection.canProjectSecureVideo()){
2486 return true;
2487 }
2488 } catch (RemoteException e) {
2489 Slog.e(TAG, "Unable to query projection service for permissions", e);
2490 }
2491 }
Chilun67a379b2019-04-11 19:49:42 +08002492 return checkCallingPermission(CAPTURE_SECURE_VIDEO_OUTPUT, "canProjectSecureVideo()");
2493 }
2494
2495 private boolean checkCallingPermission(String permission, String func) {
2496 if (mContext.checkCallingPermission(permission) == PackageManager.PERMISSION_GRANTED) {
2497 return true;
2498 }
2499 final String msg = "Permission Denial: " + func + " from pid=" + Binder.getCallingPid()
2500 + ", uid=" + Binder.getCallingUid() + " requires " + permission;
2501 Slog.w(TAG, msg);
2502 return false;
Michael Wrightc39d47a2014-07-08 18:07:36 -07002503 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08002504 }
2505
2506 private final class LocalService extends DisplayManagerInternal {
Galia Peycheva056b3ee2019-06-26 14:05:12 +02002507
Jeff Brown4ccb8232014-01-16 22:16:42 -08002508 @Override
Jeff Brown037c33e2014-04-09 00:31:55 -07002509 public void initPowerManagement(final DisplayPowerCallbacks callbacks, Handler handler,
Jeff Brownad9ef192014-04-08 17:26:30 -07002510 SensorManager sensorManager) {
2511 synchronized (mSyncRoot) {
Jeff Brown037c33e2014-04-09 00:31:55 -07002512 DisplayBlanker blanker = new DisplayBlanker() {
2513 @Override
Fiona Campbelld4eb2952019-11-04 17:19:56 +00002514 public void requestDisplayState(int state, float brightness) {
Jeff Brown037c33e2014-04-09 00:31:55 -07002515 // The order of operations is important for legacy reasons.
2516 if (state == Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07002517 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07002518 }
2519
2520 callbacks.onDisplayStateChange(state);
2521
2522 if (state != Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07002523 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07002524 }
2525 }
2526 };
Jeff Brownad9ef192014-04-08 17:26:30 -07002527 mDisplayPowerController = new DisplayPowerController(
Fiona Campbell172fd4a2020-03-13 16:34:30 +00002528 mContext, callbacks, handler, sensorManager, blanker,
2529 mDisplayDevices.get(Display.DEFAULT_DISPLAY));
Long Lingbc841b02019-07-03 16:43:15 -07002530 mSensorManager = sensorManager;
Jeff Brownad9ef192014-04-08 17:26:30 -07002531 }
Michael Wrighteef0e132017-11-21 17:57:52 +00002532
2533 mHandler.sendEmptyMessage(MSG_LOAD_BRIGHTNESS_CONFIGURATION);
Jeff Brownad9ef192014-04-08 17:26:30 -07002534 }
2535
2536 @Override
2537 public boolean requestPowerState(DisplayPowerRequest request,
2538 boolean waitForNegativeProximity) {
Michael Wrighteef0e132017-11-21 17:57:52 +00002539 synchronized (mSyncRoot) {
2540 return mDisplayPowerController.requestPowerState(request,
2541 waitForNegativeProximity);
2542 }
Jeff Brownad9ef192014-04-08 17:26:30 -07002543 }
2544
2545 @Override
2546 public boolean isProximitySensorAvailable() {
Michael Wrighteef0e132017-11-21 17:57:52 +00002547 synchronized (mSyncRoot) {
2548 return mDisplayPowerController.isProximitySensorAvailable();
2549 }
Jeff Brownad9ef192014-04-08 17:26:30 -07002550 }
2551
2552 @Override
Jacky Kao333de4e2020-04-07 15:01:41 +08002553 public SurfaceControl.ScreenshotGraphicBuffer systemScreenshot(int displayId) {
2554 return systemScreenshotInternal(displayId);
Jacky Kao316477c2020-03-23 16:30:04 +08002555 }
2556
2557 @Override
Jacky Kao333de4e2020-04-07 15:01:41 +08002558 public SurfaceControl.ScreenshotGraphicBuffer userScreenshot(int displayId) {
2559 return userScreenshotInternal(displayId);
Riddle Hsu654a6f92018-07-13 22:59:36 +08002560 }
2561
2562 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08002563 public DisplayInfo getDisplayInfo(int displayId) {
2564 return getDisplayInfoInternal(displayId, Process.myUid());
2565 }
2566
2567 @Override
chaviw945f7302020-02-24 14:35:33 -08002568 public Point getDisplayPosition(int displayId) {
2569 synchronized (mSyncRoot) {
2570 LogicalDisplay display = mLogicalDisplays.get(displayId);
2571 if (display != null) {
2572 return display.getDisplayPosition();
2573 }
2574 return null;
2575 }
2576 }
2577
2578 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08002579 public void registerDisplayTransactionListener(DisplayTransactionListener listener) {
2580 if (listener == null) {
2581 throw new IllegalArgumentException("listener must not be null");
2582 }
2583
2584 registerDisplayTransactionListenerInternal(listener);
2585 }
2586
2587 @Override
2588 public void unregisterDisplayTransactionListener(DisplayTransactionListener listener) {
2589 if (listener == null) {
2590 throw new IllegalArgumentException("listener must not be null");
2591 }
2592
2593 unregisterDisplayTransactionListenerInternal(listener);
2594 }
2595
2596 @Override
2597 public void setDisplayInfoOverrideFromWindowManager(int displayId, DisplayInfo info) {
2598 setDisplayInfoOverrideFromWindowManagerInternal(displayId, info);
2599 }
2600
2601 @Override
Andrii Kuliancd097992017-03-23 18:31:59 -07002602 public void getNonOverrideDisplayInfo(int displayId, DisplayInfo outInfo) {
2603 getNonOverrideDisplayInfoInternal(displayId, outInfo);
2604 }
2605
2606 @Override
Robert Carrae606b42018-02-15 15:36:23 -08002607 public void performTraversal(SurfaceControl.Transaction t) {
2608 performTraversalInternal(t);
Jeff Brown4ccb8232014-01-16 22:16:42 -08002609 }
2610
2611 @Override
Michael Wright3f145a22014-07-22 19:46:03 -07002612 public void setDisplayProperties(int displayId, boolean hasContent,
Galia Peycheva056b3ee2019-06-26 14:05:12 +02002613 float requestedRefreshRate, int requestedMode,
2614 boolean requestedMinimalPostProcessing, boolean inTraversal) {
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07002615 setDisplayPropertiesInternal(displayId, hasContent, requestedRefreshRate,
Galia Peycheva056b3ee2019-06-26 14:05:12 +02002616 requestedMode, requestedMinimalPostProcessing, inTraversal);
Jeff Brown4ccb8232014-01-16 22:16:42 -08002617 }
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08002618
2619 @Override
2620 public void setDisplayOffsets(int displayId, int x, int y) {
2621 setDisplayOffsetsInternal(displayId, x, y);
2622 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002623
2624 @Override
Sam Lin4c3ac2b2019-02-18 04:50:26 -08002625 public void setDisplayScalingDisabled(int displayId, boolean disableScaling) {
2626 setDisplayScalingDisabledInternal(displayId, disableScaling);
2627 }
2628
2629 @Override
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002630 public void setDisplayAccessUIDs(SparseArray<IntArray> newDisplayAccessUIDs) {
2631 setDisplayAccessUIDsInternal(newDisplayAccessUIDs);
2632 }
2633
2634 @Override
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002635 public void persistBrightnessTrackerState() {
Michael Wright144aac92017-12-21 18:37:41 +00002636 synchronized (mSyncRoot) {
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002637 mDisplayPowerController.persistBrightnessTrackerState();
Michael Wright144aac92017-12-21 18:37:41 +00002638 }
Kenny Guycfe7b702017-11-14 21:04:58 +00002639 }
Adrian Roose1d68cd2018-01-17 12:54:50 +01002640
2641 @Override
2642 public void onOverlayChanged() {
2643 synchronized (mSyncRoot) {
Adrian Roos898ec382018-01-17 12:54:50 +01002644 for (int i = 0; i < mDisplayDevices.size(); i++) {
2645 mDisplayDevices.get(i).onOverlayChangedLocked();
Adrian Roose1d68cd2018-01-17 12:54:50 +01002646 }
2647 }
2648 }
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07002649
2650 @Override
2651 public DisplayedContentSamplingAttributes getDisplayedContentSamplingAttributes(
2652 int displayId) {
2653 return getDisplayedContentSamplingAttributesInternal(displayId);
2654 }
2655
2656 @Override
2657 public boolean setDisplayedContentSamplingEnabled(
2658 int displayId, boolean enable, int componentMask, int maxFrames) {
2659 return setDisplayedContentSamplingEnabledInternal(
2660 displayId, enable, componentMask, maxFrames);
2661 }
2662
2663 @Override
2664 public DisplayedContentSample getDisplayedContentSample(int displayId,
2665 long maxFrames, long timestamp) {
2666 return getDisplayedContentSampleInternal(displayId, maxFrames, timestamp);
2667 }
2668
Jeff Brown4ccb8232014-01-16 22:16:42 -08002669 }
Michael Wrighta3dab232019-02-22 16:54:21 +00002670
Ana Kruleca74a8642019-11-14 00:51:00 +01002671 class DesiredDisplayModeSpecsObserver
2672 implements DisplayModeDirector.DesiredDisplayModeSpecsListener {
2673 public void onDesiredDisplayModeSpecsChanged() {
2674 onDesiredDisplayModeSpecsChangedInternal();
Michael Wrighta3dab232019-02-22 16:54:21 +00002675 }
2676 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07002677}