blob: 348b23401c0696a703c6b0f3ccd62c743b68903c [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;
Jeff Brown7f3994e2012-12-04 14:04:28 -0800114import java.util.concurrent.CopyOnWriteArrayList;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700115
116/**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700117 * Manages attached displays.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700118 * <p>
Jeff Brownbd6e1502012-08-28 03:27:37 -0700119 * The {@link DisplayManagerService} manages the global lifecycle of displays,
120 * decides how to configure logical displays based on the physical display devices currently
121 * attached, sends notifications to the system and to applications when the state
122 * changes, and so on.
123 * </p><p>
124 * The display manager service relies on a collection of {@link DisplayAdapter} components,
125 * for discovering and configuring physical display devices attached to the system.
126 * There are separate display adapters for each manner that devices are attached:
Dominik Laskowski26290bb2020-01-15 16:09:55 -0800127 * one display adapter for physical displays, one for simulated non-functional
Jeff Brownbd6e1502012-08-28 03:27:37 -0700128 * displays when the system is headless, one for simulated overlay displays used for
129 * development, one for wifi displays, etc.
130 * </p><p>
131 * Display adapters are only weakly coupled to the display manager service.
132 * Display adapters communicate changes in display device state to the display manager
Craig Mautner722285e2012-09-07 13:55:58 -0700133 * service asynchronously via a {@link DisplayAdapter.Listener} registered
Jeff Brownbd6e1502012-08-28 03:27:37 -0700134 * by the display manager service. This separation of concerns is important for
135 * two main reasons. First, it neatly encapsulates the responsibilities of these
136 * two classes: display adapters handle individual display devices whereas
137 * the display manager service handles the global state. Second, it eliminates
138 * the potential for deadlocks resulting from asynchronous display device discovery.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700139 * </p>
140 *
141 * <h3>Synchronization</h3>
142 * <p>
143 * Because the display manager may be accessed by multiple threads, the synchronization
144 * story gets a little complicated. In particular, the window manager may call into
145 * the display manager while holding a surface transaction with the expectation that
146 * it can apply changes immediately. Unfortunately, that means we can't just do
147 * everything asynchronously (*grump*).
Jeff Brownbd6e1502012-08-28 03:27:37 -0700148 * </p><p>
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700149 * To make this work, all of the objects that belong to the display manager must
150 * use the same lock. We call this lock the synchronization root and it has a unique
151 * type {@link DisplayManagerService.SyncRoot}. Methods that require this lock are
152 * named with the "Locked" suffix.
153 * </p><p>
154 * Where things get tricky is that the display manager is not allowed to make
155 * any potentially reentrant calls, especially into the window manager. We generally
156 * avoid this by making all potentially reentrant out-calls asynchronous.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700157 * </p>
158 */
Jeff Brown4ccb8232014-01-16 22:16:42 -0800159public final class DisplayManagerService extends SystemService {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700160 private static final String TAG = "DisplayManagerService";
Jeff Brownbd6e1502012-08-28 03:27:37 -0700161 private static final boolean DEBUG = false;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700162
Jeff Brownbbd28a22012-09-20 16:47:15 -0700163 // When this system property is set to 0, WFD is forcibly disabled on boot.
164 // When this system property is set to 1, WFD is forcibly enabled on boot.
165 // Otherwise WFD is enabled according to the value of config_enableWifiDisplay.
166 private static final String FORCE_WIFI_DISPLAY_ENABLE = "persist.debug.wfd.enable";
167
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200168 private static final String PROP_DEFAULT_DISPLAY_TOP_INSET = "persist.sys.displayinset.top";
169
Jeff Brownbd6e1502012-08-28 03:27:37 -0700170 private static final long WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT = 10000;
171
Santos Cordonc22c5632017-06-21 16:03:49 -0700172 private static final int MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS = 1;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700173 private static final int MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS = 2;
174 private static final int MSG_DELIVER_DISPLAY_EVENT = 3;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700175 private static final int MSG_REQUEST_TRAVERSAL = 4;
Jeff Brownd728bf52012-09-08 18:05:28 -0700176 private static final int MSG_UPDATE_VIEWPORT = 5;
Michael Wrighta3dab232019-02-22 16:54:21 +0000177 private static final int MSG_LOAD_BRIGHTNESS_CONFIGURATION = 6;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700178
Jeff Brownb880d882014-02-10 19:47:07 -0800179 private final Context mContext;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700180 private final DisplayManagerHandler mHandler;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700181 private final Handler mUiHandler;
182 private final DisplayAdapterListener mDisplayAdapterListener;
Michael Wrighta3dab232019-02-22 16:54:21 +0000183 private final DisplayModeDirector mDisplayModeDirector;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800184 private WindowManagerInternal mWindowManagerInternal;
185 private InputManagerInternal mInputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -0700186 private IMediaProjectionManager mProjectionService;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700187
188 // The synchronization root for the display manager.
189 // This lock guards most of the display manager's state.
Craig Mautner0bf6ec92012-12-18 08:33:27 -0800190 // NOTE: This is synchronized on while holding WindowManagerService.mWindowMap so never call
191 // into WindowManagerService methods that require mWindowMap while holding this unless you are
192 // very very sure that no deadlock can occur.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700193 private final SyncRoot mSyncRoot = new SyncRoot();
194
195 // True if in safe mode.
196 // This option may disable certain display adapters.
197 public boolean mSafeMode;
198
199 // True if we are in a special boot mode where only core applications and
200 // services should be started. This option may disable certain display adapters.
201 public boolean mOnlyCore;
202
Jeff Brown27f1d672012-10-17 18:32:34 -0700203 // True if the display manager service should pretend there is only one display
204 // and only tell applications about the existence of the default logical display.
205 // The display manager can still mirror content to secondary displays but applications
206 // cannot present unique content on those displays.
207 // Used for demonstration purposes only.
208 private final boolean mSingleDisplayDemoMode;
209
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700210 // All callback records indexed by calling process id.
211 public final SparseArray<CallbackRecord> mCallbacks =
Jeff Brownbd6e1502012-08-28 03:27:37 -0700212 new SparseArray<CallbackRecord>();
Jeff Brownfa25bf52012-07-23 19:26:30 -0700213
Jeff Brownbd6e1502012-08-28 03:27:37 -0700214 // List of all currently registered display adapters.
215 private final ArrayList<DisplayAdapter> mDisplayAdapters = new ArrayList<DisplayAdapter>();
216
217 // List of all currently connected display devices.
218 private final ArrayList<DisplayDevice> mDisplayDevices = new ArrayList<DisplayDevice>();
219
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700220 // List of all logical displays indexed by logical display id.
Tim Murray890ceb52020-01-30 10:12:40 -0800221 // Any modification to mLogicalDisplays must invalidate the DisplayManagerGlobal cache.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700222 private final SparseArray<LogicalDisplay> mLogicalDisplays =
223 new SparseArray<LogicalDisplay>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700224 private int mNextNonDefaultDisplayId = Display.DEFAULT_DISPLAY + 1;
225
Jeff Brown7f3994e2012-12-04 14:04:28 -0800226 // List of all display transaction listeners.
227 private final CopyOnWriteArrayList<DisplayTransactionListener> mDisplayTransactionListeners =
228 new CopyOnWriteArrayList<DisplayTransactionListener>();
229
Jeff Brownad9ef192014-04-08 17:26:30 -0700230 // Display power controller.
231 private DisplayPowerController mDisplayPowerController;
232
Jeff Brown037c33e2014-04-09 00:31:55 -0700233 // The overall display state, independent of changes that might influence one
234 // display or another in particular.
Jeff Brown5d6443b2015-04-10 20:15:01 -0700235 private int mGlobalDisplayState = Display.STATE_ON;
236
237 // The overall display brightness.
Dominik Laskowski26290bb2020-01-15 16:09:55 -0800238 // For now, this only applies to the default display but we may split it up eventually.
Fiona Campbelld4eb2952019-11-04 17:19:56 +0000239 private float mGlobalDisplayBrightness;
Jeff Brown9e316a12012-10-08 19:17:06 -0700240
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700241 // Set to true when there are pending display changes that have yet to be applied
242 // to the surface flinger state.
243 private boolean mPendingTraversal;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700244
Jeff Browne08ae382012-09-07 20:36:36 -0700245 // The Wifi display adapter, or null if not registered.
246 private WifiDisplayAdapter mWifiDisplayAdapter;
247
Jeff Brownce468a32013-11-21 16:42:03 -0800248 // The number of active wifi display scan requests.
249 private int mWifiDisplayScanRequestCount;
250
Jeff Browna506a6e2013-06-04 00:02:38 -0700251 // The virtual display adapter, or null if not registered.
252 private VirtualDisplayAdapter mVirtualDisplayAdapter;
253
Michael Wrighteef0e132017-11-21 17:57:52 +0000254 // The User ID of the current user
255 private @UserIdInt int mCurrentUserId;
256
Michael Wrighteedcbf12017-08-16 23:14:54 +0100257 // The stable device screen height and width. These are not tied to a specific display, even
258 // the default display, because they need to be stable over the course of the device's entire
259 // life, even if the default display changes (e.g. a new monitor is plugged into a PC-like
260 // device).
261 private Point mStableDisplaySize = new Point();
262
Winson72dbe7e2019-04-29 14:55:30 -0700263 // Whether the system has finished booting or not.
264 private boolean mSystemReady;
265
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200266 // The top inset of the default display.
267 // This gets persisted so that the boot animation knows how to transition from the display's
268 // full size to the size configured by the user. Right now we only persist and animate the top
269 // inset, but theoretically we could do it for all of them.
270 private int mDefaultDisplayTopInset;
271
Jeff Brownd728bf52012-09-08 18:05:28 -0700272 // Viewports of the default display and the display that should receive touch
273 // input from an external source. Used by the input system.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100274 @GuardedBy("mSyncRoot")
275 private final ArrayList<DisplayViewport> mViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700276
Jeff Brown89d55462012-09-19 11:33:42 -0700277 // Persistent data store for all internal settings maintained by the display manager service.
278 private final PersistentDataStore mPersistentDataStore = new PersistentDataStore();
279
Jeff Brownbd6e1502012-08-28 03:27:37 -0700280 // Temporary callback list, used when sending display events to applications.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700281 // May be used outside of the lock but only on the handler thread.
282 private final ArrayList<CallbackRecord> mTempCallbacks = new ArrayList<CallbackRecord>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700283
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700284 // Temporary display info, used for comparing display configurations.
285 private final DisplayInfo mTempDisplayInfo = new DisplayInfo();
286
Jeff Brownd728bf52012-09-08 18:05:28 -0700287 // Temporary viewports, used when sending new viewport information to the
288 // input system. May be used outside of the lock but only on the handler thread.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100289 private final ArrayList<DisplayViewport> mTempViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700290
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700291 // The default color mode for default displays. Overrides the usual
292 // Display.Display.COLOR_MODE_DEFAULT for displays with the
293 // DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY flag set.
294 private final int mDefaultDisplayDefaultColorMode;
295
Jeff Browne75926d2014-09-18 15:24:49 -0700296 // Temporary list of deferred work to perform when setting the display state.
297 // Only used by requestDisplayState. The field is self-synchronized and only
298 // intended for use inside of the requestGlobalDisplayStateInternal function.
299 private final ArrayList<Runnable> mTempDisplayStateWorkQueue = new ArrayList<Runnable>();
300
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800301 // Lists of UIDs that are present on the displays. Maps displayId -> array of UIDs.
302 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
303
Santos Cordonee8931e2017-04-05 10:31:15 -0700304 private final Injector mInjector;
305
Dan Gittik122df862018-03-28 16:59:22 +0100306 // The minimum brightness curve, which guarantess that any brightness curve that dips below it
307 // is rejected by the system.
308 private final Curve mMinimumBrightnessCurve;
309 private final Spline mMinimumBrightnessSpline;
Peiyong Lin277eaff2019-01-16 16:18:22 -0800310 private final ColorSpace mWideColorSpace;
Dan Gittik122df862018-03-28 16:59:22 +0100311
Long Lingbc841b02019-07-03 16:43:15 -0700312 private SensorManager mSensorManager;
313
Galia Peycheva088d7c02019-12-13 11:27:23 +0100314 // Whether minimal post processing is allowed by the user.
315 @GuardedBy("mSyncRoot")
316 private boolean mMinimalPostProcessingAllowed;
317
318 // Receives notifications about changes to Settings.
319 private SettingsObserver mSettingsObserver;
320
Jeff Brownb880d882014-02-10 19:47:07 -0800321 public DisplayManagerService(Context context) {
Santos Cordonee8931e2017-04-05 10:31:15 -0700322 this(context, new Injector());
323 }
324
325 @VisibleForTesting
326 DisplayManagerService(Context context, Injector injector) {
Jeff Brownb880d882014-02-10 19:47:07 -0800327 super(context);
Santos Cordonee8931e2017-04-05 10:31:15 -0700328 mInjector = injector;
Jeff Brownb880d882014-02-10 19:47:07 -0800329 mContext = context;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800330 mHandler = new DisplayManagerHandler(DisplayThread.get().getLooper());
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700331 mUiHandler = UiThread.getHandler();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700332 mDisplayAdapterListener = new DisplayAdapterListener();
Michael Wrighta3dab232019-02-22 16:54:21 +0000333 mDisplayModeDirector = new DisplayModeDirector(context, mHandler);
Jeff Brown27f1d672012-10-17 18:32:34 -0700334 mSingleDisplayDemoMode = SystemProperties.getBoolean("persist.demo.singledisplay", false);
Dan Gittik122df862018-03-28 16:59:22 +0100335 Resources resources = mContext.getResources();
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700336 mDefaultDisplayDefaultColorMode = mContext.getResources().getInteger(
Michael Wrighteef0e132017-11-21 17:57:52 +0000337 com.android.internal.R.integer.config_defaultDisplayDefaultColorMode);
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200338 mDefaultDisplayTopInset = SystemProperties.getInt(PROP_DEFAULT_DISPLAY_TOP_INSET, -1);
Dan Gittik122df862018-03-28 16:59:22 +0100339 float[] lux = getFloatArray(resources.obtainTypedArray(
340 com.android.internal.R.array.config_minimumBrightnessCurveLux));
341 float[] nits = getFloatArray(resources.obtainTypedArray(
342 com.android.internal.R.array.config_minimumBrightnessCurveNits));
343 mMinimumBrightnessCurve = new Curve(lux, nits);
344 mMinimumBrightnessSpline = Spline.createSpline(lux, nits);
Jeff Brown5d6443b2015-04-10 20:15:01 -0700345
Michael Wrighta3dab232019-02-22 16:54:21 +0000346 PowerManager pm = mContext.getSystemService(PowerManager.class);
Fiona Campbelld4eb2952019-11-04 17:19:56 +0000347 mGlobalDisplayBrightness = pm.getBrightnessConstraint(
348 PowerManager.BRIGHTNESS_CONSTRAINT_TYPE_DEFAULT);
Michael Wrighteef0e132017-11-21 17:57:52 +0000349 mCurrentUserId = UserHandle.USER_SYSTEM;
Peiyong Lin277eaff2019-01-16 16:18:22 -0800350 ColorSpace[] colorSpaces = SurfaceControl.getCompositionColorSpaces();
351 mWideColorSpace = colorSpaces[1];
Winson72dbe7e2019-04-29 14:55:30 -0700352
353 mSystemReady = false;
Joel Fernandes2d314e12017-04-04 16:32:15 -0700354 }
355
356 public void setupSchedulerPolicies() {
Jorim Jaggied7993b2017-03-28 18:50:01 +0100357 // android.display and android.anim is critical to user experience and we should make sure
Michael Wrighteef0e132017-11-21 17:57:52 +0000358 // it is not in the default foregroup groups, add it to top-app to make sure it uses all
359 // the cores and scheduling settings for top-app when it runs.
Jorim Jaggied7993b2017-03-28 18:50:01 +0100360 Process.setThreadGroupAndCpuset(DisplayThread.get().getThreadId(),
361 Process.THREAD_GROUP_TOP_APP);
362 Process.setThreadGroupAndCpuset(AnimationThread.get().getThreadId(),
363 Process.THREAD_GROUP_TOP_APP);
Jorim Jaggi21c39a72017-10-20 15:47:51 +0200364 Process.setThreadGroupAndCpuset(SurfaceAnimationThread.get().getThreadId(),
365 Process.THREAD_GROUP_TOP_APP);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700366 }
367
Jeff Brown4ccb8232014-01-16 22:16:42 -0800368 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -0800369 public void onStart() {
Michael Wright1c9977b2016-07-12 13:30:10 -0700370 // We need to pre-load the persistent data store so it's ready before the default display
371 // adapter is up so that we have it's configuration. We could load it lazily, but since
372 // 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 -0700373 // we've waited for the display to register itself with us.
Michael Wrighta3dab232019-02-22 16:54:21 +0000374 synchronized (mSyncRoot) {
375 mPersistentDataStore.loadIfNeeded();
376 loadStableDisplayValuesLocked();
Michael Wrighteedcbf12017-08-16 23:14:54 +0100377 }
Santos Cordonc22c5632017-06-21 16:03:49 -0700378 mHandler.sendEmptyMessage(MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800379
Tim Murray890ceb52020-01-30 10:12:40 -0800380 // If there was a runtime restart then we may have stale caches left around, so we need to
381 // make sure to invalidate them upon every start.
382 DisplayManagerGlobal.invalidateLocalDisplayInfoCaches();
383
Jeff Brown4ccb8232014-01-16 22:16:42 -0800384 publishBinderService(Context.DISPLAY_SERVICE, new BinderService(),
385 true /*allowIsolated*/);
386 publishLocalService(DisplayManagerInternal.class, new LocalService());
387 }
388
389 @Override
390 public void onBootPhase(int phase) {
391 if (phase == PHASE_WAIT_FOR_DEFAULT_DISPLAY) {
392 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700393 long timeout = SystemClock.uptimeMillis()
394 + mInjector.getDefaultDisplayDelayTimeout();
395 while (mLogicalDisplays.get(Display.DEFAULT_DISPLAY) == null ||
396 mVirtualDisplayAdapter == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800397 long delay = timeout - SystemClock.uptimeMillis();
398 if (delay <= 0) {
399 throw new RuntimeException("Timeout waiting for default display "
Santos Cordonc22c5632017-06-21 16:03:49 -0700400 + "to be initialized. DefaultDisplay="
401 + mLogicalDisplays.get(Display.DEFAULT_DISPLAY)
402 + ", mVirtualDisplayAdapter=" + mVirtualDisplayAdapter);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800403 }
404 if (DEBUG) {
405 Slog.d(TAG, "waitForDefaultDisplay: waiting, timeout=" + delay);
406 }
407 try {
408 mSyncRoot.wait(delay);
409 } catch (InterruptedException ex) {
410 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700411 }
412 }
413 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700414 }
415
Michael Wrighteef0e132017-11-21 17:57:52 +0000416 @Override
417 public void onSwitchUser(@UserIdInt int newUserId) {
418 final int userSerial = getUserManager().getUserSerialNumber(newUserId);
419 synchronized (mSyncRoot) {
420 if (mCurrentUserId != newUserId) {
421 mCurrentUserId = newUserId;
422 BrightnessConfiguration config =
423 mPersistentDataStore.getBrightnessConfiguration(userSerial);
424 mDisplayPowerController.setBrightnessConfiguration(config);
Galia Peycheva088d7c02019-12-13 11:27:23 +0100425 handleSettingsChange();
Michael Wrighteef0e132017-11-21 17:57:52 +0000426 }
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +0000427 mDisplayPowerController.onSwitchUser(newUserId);
Michael Wrighteef0e132017-11-21 17:57:52 +0000428 }
429 }
430
Jeff Brown4ccb8232014-01-16 22:16:42 -0800431 // TODO: Use dependencies or a boot phase
432 public void windowManagerAndInputReady() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700433 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800434 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
435 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner65d11b32012-10-01 13:59:52 -0700436 scheduleTraversalLocked(false);
Jeff Brownd728bf52012-09-08 18:05:28 -0700437 }
438 }
439
440 /**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700441 * Called when the system is ready to go.
442 */
443 public void systemReady(boolean safeMode, boolean onlyCore) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700444 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700445 mSafeMode = safeMode;
446 mOnlyCore = onlyCore;
Winson72dbe7e2019-04-29 14:55:30 -0700447 mSystemReady = true;
448 // Just in case the top inset changed before the system was ready. At this point, any
449 // relevant configuration should be in place.
450 recordTopInsetLocked(mLogicalDisplays.get(Display.DEFAULT_DISPLAY));
Galia Peycheva088d7c02019-12-13 11:27:23 +0100451
452 updateSettingsLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700453 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700454
Ana Kruleca74a8642019-11-14 00:51:00 +0100455 mDisplayModeDirector.setDesiredDisplayModeSpecsListener(
456 new DesiredDisplayModeSpecsObserver());
Long Lingbc841b02019-07-03 16:43:15 -0700457 mDisplayModeDirector.start(mSensorManager);
Michael Wrighta3dab232019-02-22 16:54:21 +0000458
Jeff Brownbd6e1502012-08-28 03:27:37 -0700459 mHandler.sendEmptyMessage(MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS);
Galia Peycheva088d7c02019-12-13 11:27:23 +0100460
461 mSettingsObserver = new SettingsObserver();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700462 }
463
Santos Cordonee8931e2017-04-05 10:31:15 -0700464 @VisibleForTesting
465 Handler getDisplayHandler() {
466 return mHandler;
467 }
468
Michael Wrighteedcbf12017-08-16 23:14:54 +0100469 private void loadStableDisplayValuesLocked() {
470 final Point size = mPersistentDataStore.getStableDisplaySize();
471 if (size.x > 0 && size.y > 0) {
472 // Just set these values directly so we don't write the display persistent data again
473 // unnecessarily
474 mStableDisplaySize.set(size.x, size.y);
475 } else {
476 final Resources res = mContext.getResources();
477 final int width = res.getInteger(
478 com.android.internal.R.integer.config_stableDeviceDisplayWidth);
479 final int height = res.getInteger(
480 com.android.internal.R.integer.config_stableDeviceDisplayHeight);
481 if (width > 0 && height > 0) {
482 setStableDisplaySizeLocked(width, height);
483 }
484 }
485 }
486
487 private Point getStableDisplaySizeInternal() {
488 Point r = new Point();
489 synchronized (mSyncRoot) {
490 if (mStableDisplaySize.x > 0 && mStableDisplaySize.y > 0) {
491 r.set(mStableDisplaySize.x, mStableDisplaySize.y);
492 }
493 }
494 return r;
495 }
496
Jeff Brown4ccb8232014-01-16 22:16:42 -0800497 private void registerDisplayTransactionListenerInternal(
498 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800499 // List is self-synchronized copy-on-write.
500 mDisplayTransactionListeners.add(listener);
501 }
502
Jeff Brown4ccb8232014-01-16 22:16:42 -0800503 private void unregisterDisplayTransactionListenerInternal(
504 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800505 // List is self-synchronized copy-on-write.
506 mDisplayTransactionListeners.remove(listener);
507 }
508
Jeff Brown4ccb8232014-01-16 22:16:42 -0800509 private void setDisplayInfoOverrideFromWindowManagerInternal(
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700510 int displayId, DisplayInfo info) {
511 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700512 LogicalDisplay display = mLogicalDisplays.get(displayId);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700513 if (display != null) {
Jeff Brownef981a42013-08-07 14:13:37 -0700514 if (display.setDisplayInfoOverrideFromWindowManagerLocked(info)) {
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200515 handleLogicalDisplayChanged(displayId, display);
Craig Mautner65d11b32012-10-01 13:59:52 -0700516 scheduleTraversalLocked(false);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700517 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700518 }
519 }
520 }
521
Andrii Kuliancd097992017-03-23 18:31:59 -0700522 /**
523 * @see DisplayManagerInternal#getNonOverrideDisplayInfo(int, DisplayInfo)
524 */
525 private void getNonOverrideDisplayInfoInternal(int displayId, DisplayInfo outInfo) {
526 synchronized (mSyncRoot) {
527 final LogicalDisplay display = mLogicalDisplays.get(displayId);
528 if (display != null) {
529 display.getNonOverrideDisplayInfoLocked(outInfo);
530 }
531 }
532 }
533
Santos Cordonee8931e2017-04-05 10:31:15 -0700534 @VisibleForTesting
Robert Carrae606b42018-02-15 15:36:23 -0800535 void performTraversalInternal(SurfaceControl.Transaction t) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700536 synchronized (mSyncRoot) {
537 if (!mPendingTraversal) {
538 return;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700539 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700540 mPendingTraversal = false;
541
Robert Carrae606b42018-02-15 15:36:23 -0800542 performTraversalLocked(t);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700543 }
Jeff Brown7f3994e2012-12-04 14:04:28 -0800544
545 // List is self-synchronized copy-on-write.
546 for (DisplayTransactionListener listener : mDisplayTransactionListeners) {
Vishnu Naire86bd982018-11-28 13:23:17 -0800547 listener.onDisplayTransaction(t);
Jeff Brown7f3994e2012-12-04 14:04:28 -0800548 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700549 }
550
Fiona Campbelld4eb2952019-11-04 17:19:56 +0000551 private void requestGlobalDisplayStateInternal(int state, float brightnessState) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700552 if (state == Display.STATE_UNKNOWN) {
553 state = Display.STATE_ON;
554 }
555 if (state == Display.STATE_OFF) {
Fiona Campbelld4eb2952019-11-04 17:19:56 +0000556 brightnessState = PowerManager.BRIGHTNESS_OFF_FLOAT;
557 } else if (brightnessState < PowerManager.BRIGHTNESS_MIN || Float.isNaN(brightnessState)) {
558 brightnessState = PowerManager.BRIGHTNESS_INVALID_FLOAT;
559 } else if (brightnessState > PowerManager.BRIGHTNESS_MAX) {
560 brightnessState = PowerManager.BRIGHTNESS_MAX;
Jeff Brown5d6443b2015-04-10 20:15:01 -0700561 }
562
Jeff Browne75926d2014-09-18 15:24:49 -0700563 synchronized (mTempDisplayStateWorkQueue) {
564 try {
565 // Update the display state within the lock.
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700566 // Note that we do not need to schedule traversals here although it
567 // may happen as a side-effect of displays changing state.
Jeff Browne75926d2014-09-18 15:24:49 -0700568 synchronized (mSyncRoot) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700569 if (mGlobalDisplayState == state
Fiona Campbelld4eb2952019-11-04 17:19:56 +0000570 && mGlobalDisplayBrightness == brightnessState) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700571 return; // no change
Jeff Browne75926d2014-09-18 15:24:49 -0700572 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700573
574 Trace.traceBegin(Trace.TRACE_TAG_POWER, "requestGlobalDisplayState("
575 + Display.stateToString(state)
Fiona Campbelld4eb2952019-11-04 17:19:56 +0000576 + ", brightness=" + brightnessState + ")");
Jeff Brown5d6443b2015-04-10 20:15:01 -0700577 mGlobalDisplayState = state;
Fiona Campbelld4eb2952019-11-04 17:19:56 +0000578 mGlobalDisplayBrightness = brightnessState;
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700579 applyGlobalDisplayStateLocked(mTempDisplayStateWorkQueue);
Jeff Browne75926d2014-09-18 15:24:49 -0700580 }
581
582 // Setting the display power state can take hundreds of milliseconds
583 // to complete so we defer the most expensive part of the work until
584 // after we have exited the critical section to avoid blocking other
585 // threads for a long time.
586 for (int i = 0; i < mTempDisplayStateWorkQueue.size(); i++) {
587 mTempDisplayStateWorkQueue.get(i).run();
588 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700589 Trace.traceEnd(Trace.TRACE_TAG_POWER);
Jeff Browne75926d2014-09-18 15:24:49 -0700590 } finally {
591 mTempDisplayStateWorkQueue.clear();
Jeff Brown9e316a12012-10-08 19:17:06 -0700592 }
593 }
594 }
595
Galia Peycheva088d7c02019-12-13 11:27:23 +0100596 private class SettingsObserver extends ContentObserver {
597 SettingsObserver() {
598 super(mHandler);
599
600 mContext.getContentResolver().registerContentObserver(
601 Settings.Secure.getUriFor(
602 Settings.Secure.MINIMAL_POST_PROCESSING_ALLOWED), false, this);
603 }
604
605 @Override
606 public void onChange(boolean selfChange, Uri uri) {
607 handleSettingsChange();
608 }
609 }
610
611 private void handleSettingsChange() {
612 synchronized (mSyncRoot) {
613 updateSettingsLocked();
614 scheduleTraversalLocked(false);
615 }
616 }
617
618 private void updateSettingsLocked() {
619 mMinimalPostProcessingAllowed = Settings.Secure.getIntForUser(mContext.getContentResolver(),
620 Settings.Secure.MINIMAL_POST_PROCESSING_ALLOWED, 1, UserHandle.USER_CURRENT) != 0;
621 }
622
Jeff Brown4ccb8232014-01-16 22:16:42 -0800623 private DisplayInfo getDisplayInfoInternal(int displayId, int callingUid) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700624 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800625 LogicalDisplay display = mLogicalDisplays.get(displayId);
626 if (display != null) {
627 DisplayInfo info = display.getDisplayInfoLocked();
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800628 if (info.hasAccess(callingUid)
629 || isUidPresentOnDisplayInternal(callingUid, displayId)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800630 return info;
631 }
632 }
633 return null;
634 }
635 }
636
637 private int[] getDisplayIdsInternal(int callingUid) {
638 synchronized (mSyncRoot) {
639 final int count = mLogicalDisplays.size();
640 int[] displayIds = new int[count];
641 int n = 0;
642 for (int i = 0; i < count; i++) {
643 LogicalDisplay display = mLogicalDisplays.valueAt(i);
644 DisplayInfo info = display.getDisplayInfoLocked();
645 if (info.hasAccess(callingUid)) {
646 displayIds[n++] = mLogicalDisplays.keyAt(i);
647 }
648 }
649 if (n != count) {
650 displayIds = Arrays.copyOfRange(displayIds, 0, n);
651 }
652 return displayIds;
653 }
654 }
655
656 private void registerCallbackInternal(IDisplayManagerCallback callback, int callingPid) {
657 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700658 if (mCallbacks.get(callingPid) != null) {
659 throw new SecurityException("The calling process has already "
660 + "registered an IDisplayManagerCallback.");
Jeff Brown64a55af2012-08-26 02:47:39 -0700661 }
662
Jeff Brownbd6e1502012-08-28 03:27:37 -0700663 CallbackRecord record = new CallbackRecord(callingPid, callback);
664 try {
665 IBinder binder = callback.asBinder();
666 binder.linkToDeath(record, 0);
667 } catch (RemoteException ex) {
668 // give up
669 throw new RuntimeException(ex);
670 }
671
672 mCallbacks.put(callingPid, record);
673 }
674 }
675
Jeff Brownce468a32013-11-21 16:42:03 -0800676 private void onCallbackDied(CallbackRecord record) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700677 synchronized (mSyncRoot) {
Jeff Brownce468a32013-11-21 16:42:03 -0800678 mCallbacks.remove(record.mPid);
679 stopWifiDisplayScanLocked(record);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700680 }
681 }
682
Jeff Brown4ccb8232014-01-16 22:16:42 -0800683 private void startWifiDisplayScanInternal(int callingPid) {
684 synchronized (mSyncRoot) {
685 CallbackRecord record = mCallbacks.get(callingPid);
686 if (record == null) {
687 throw new IllegalStateException("The calling process has not "
688 + "registered an IDisplayManagerCallback.");
Jeff Browne08ae382012-09-07 20:36:36 -0700689 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800690 startWifiDisplayScanLocked(record);
Jeff Browne08ae382012-09-07 20:36:36 -0700691 }
692 }
693
Jeff Brownce468a32013-11-21 16:42:03 -0800694 private void startWifiDisplayScanLocked(CallbackRecord record) {
695 if (!record.mWifiDisplayScanRequested) {
696 record.mWifiDisplayScanRequested = true;
697 if (mWifiDisplayScanRequestCount++ == 0) {
698 if (mWifiDisplayAdapter != null) {
699 mWifiDisplayAdapter.requestStartScanLocked();
700 }
701 }
702 }
703 }
704
Jeff Brown4ccb8232014-01-16 22:16:42 -0800705 private void stopWifiDisplayScanInternal(int callingPid) {
706 synchronized (mSyncRoot) {
707 CallbackRecord record = mCallbacks.get(callingPid);
708 if (record == null) {
709 throw new IllegalStateException("The calling process has not "
710 + "registered an IDisplayManagerCallback.");
Jeff Brownce468a32013-11-21 16:42:03 -0800711 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800712 stopWifiDisplayScanLocked(record);
Jeff Brownce468a32013-11-21 16:42:03 -0800713 }
714 }
715
716 private void stopWifiDisplayScanLocked(CallbackRecord record) {
717 if (record.mWifiDisplayScanRequested) {
718 record.mWifiDisplayScanRequested = false;
719 if (--mWifiDisplayScanRequestCount == 0) {
720 if (mWifiDisplayAdapter != null) {
721 mWifiDisplayAdapter.requestStopScanLocked();
722 }
723 } else if (mWifiDisplayScanRequestCount < 0) {
Dianne Hackborn8d051722014-10-01 14:59:58 -0700724 Slog.wtf(TAG, "mWifiDisplayScanRequestCount became negative: "
Jeff Brownce468a32013-11-21 16:42:03 -0800725 + mWifiDisplayScanRequestCount);
726 mWifiDisplayScanRequestCount = 0;
727 }
728 }
729 }
730
Jeff Brown4ccb8232014-01-16 22:16:42 -0800731 private void connectWifiDisplayInternal(String address) {
732 synchronized (mSyncRoot) {
733 if (mWifiDisplayAdapter != null) {
734 mWifiDisplayAdapter.requestConnectLocked(address);
Jeff Browne08ae382012-09-07 20:36:36 -0700735 }
Jeff Browne08ae382012-09-07 20:36:36 -0700736 }
737 }
738
Jeff Brown4ccb8232014-01-16 22:16:42 -0800739 private void pauseWifiDisplayInternal() {
740 synchronized (mSyncRoot) {
741 if (mWifiDisplayAdapter != null) {
742 mWifiDisplayAdapter.requestPauseLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700743 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700744 }
745 }
746
Jeff Brown4ccb8232014-01-16 22:16:42 -0800747 private void resumeWifiDisplayInternal() {
748 synchronized (mSyncRoot) {
749 if (mWifiDisplayAdapter != null) {
750 mWifiDisplayAdapter.requestResumeLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700751 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700752 }
753 }
754
Jeff Brown4ccb8232014-01-16 22:16:42 -0800755 private void disconnectWifiDisplayInternal() {
756 synchronized (mSyncRoot) {
757 if (mWifiDisplayAdapter != null) {
758 mWifiDisplayAdapter.requestDisconnectLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700759 }
Jeff Browne08ae382012-09-07 20:36:36 -0700760 }
761 }
762
Jeff Brown4ccb8232014-01-16 22:16:42 -0800763 private void renameWifiDisplayInternal(String address, String alias) {
764 synchronized (mSyncRoot) {
765 if (mWifiDisplayAdapter != null) {
766 mWifiDisplayAdapter.requestRenameLocked(address, alias);
Jeff Brown89d55462012-09-19 11:33:42 -0700767 }
Jeff Brown89d55462012-09-19 11:33:42 -0700768 }
769 }
770
Jeff Brown4ccb8232014-01-16 22:16:42 -0800771 private void forgetWifiDisplayInternal(String address) {
772 synchronized (mSyncRoot) {
773 if (mWifiDisplayAdapter != null) {
774 mWifiDisplayAdapter.requestForgetLocked(address);
Jeff Brown89d55462012-09-19 11:33:42 -0700775 }
Jeff Brown89d55462012-09-19 11:33:42 -0700776 }
777 }
778
Jeff Brown4ccb8232014-01-16 22:16:42 -0800779 private WifiDisplayStatus getWifiDisplayStatusInternal() {
780 synchronized (mSyncRoot) {
781 if (mWifiDisplayAdapter != null) {
782 return mWifiDisplayAdapter.getWifiDisplayStatusLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700783 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800784 return new WifiDisplayStatus();
Jeff Browne08ae382012-09-07 20:36:36 -0700785 }
786 }
787
Michael Wright1c9977b2016-07-12 13:30:10 -0700788 private void requestColorModeInternal(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +0100789 synchronized (mSyncRoot) {
790 LogicalDisplay display = mLogicalDisplays.get(displayId);
791 if (display != null &&
Michael Wright1c9977b2016-07-12 13:30:10 -0700792 display.getRequestedColorModeLocked() != colorMode) {
793 display.setRequestedColorModeLocked(colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +0100794 scheduleTraversalLocked(false);
795 }
796 }
797 }
798
Michael Wright75ee9fc2014-09-01 19:55:22 -0700799 private int createVirtualDisplayInternal(IVirtualDisplayCallback callback,
b0202.jung925435c2020-01-08 18:46:59 +0900800 IMediaProjection projection, int callingUid, String packageName, Surface surface,
801 int flags, VirtualDisplayConfig virtualDisplayConfig) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800802 synchronized (mSyncRoot) {
803 if (mVirtualDisplayAdapter == null) {
804 Slog.w(TAG, "Rejecting request to create private virtual display "
805 + "because the virtual display adapter is not available.");
806 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700807 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800808
809 DisplayDevice device = mVirtualDisplayAdapter.createVirtualDisplayLocked(
b0202.jung925435c2020-01-08 18:46:59 +0900810 callback, projection, callingUid, packageName, surface, flags,
811 virtualDisplayConfig);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800812 if (device == null) {
813 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700814 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700815
Jeff Brown4ccb8232014-01-16 22:16:42 -0800816 handleDisplayDeviceAddedLocked(device);
817 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
818 if (display != null) {
819 return display.getDisplayIdLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700820 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800821
822 // Something weird happened and the logical display was not created.
823 Slog.w(TAG, "Rejecting request to create virtual display "
824 + "because the logical display was not created.");
Michael Wright75ee9fc2014-09-01 19:55:22 -0700825 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800826 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700827 }
828 return -1;
829 }
830
Michael Wright01e840f2014-06-26 16:03:25 -0700831 private void resizeVirtualDisplayInternal(IBinder appToken,
832 int width, int height, int densityDpi) {
833 synchronized (mSyncRoot) {
834 if (mVirtualDisplayAdapter == null) {
835 return;
836 }
837
838 mVirtualDisplayAdapter.resizeVirtualDisplayLocked(appToken, width, height, densityDpi);
839 }
840 }
841
Jeff Brown92207df2014-04-16 13:16:07 -0700842 private void setVirtualDisplaySurfaceInternal(IBinder appToken, Surface surface) {
843 synchronized (mSyncRoot) {
844 if (mVirtualDisplayAdapter == null) {
845 return;
846 }
847
848 mVirtualDisplayAdapter.setVirtualDisplaySurfaceLocked(appToken, surface);
849 }
850 }
851
Jeff Brown4ccb8232014-01-16 22:16:42 -0800852 private void releaseVirtualDisplayInternal(IBinder appToken) {
853 synchronized (mSyncRoot) {
854 if (mVirtualDisplayAdapter == null) {
855 return;
Jeff Browna506a6e2013-06-04 00:02:38 -0700856 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700857
Jeff Brown4ccb8232014-01-16 22:16:42 -0800858 DisplayDevice device =
859 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(appToken);
860 if (device != null) {
861 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700862 }
863 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700864 }
865
chaviwda4c6942018-11-07 15:52:56 -0800866 private void setVirtualDisplayStateInternal(IBinder appToken, boolean isOn) {
867 synchronized (mSyncRoot) {
868 if (mVirtualDisplayAdapter == null) {
869 return;
870 }
871
872 mVirtualDisplayAdapter.setVirtualDisplayStateLocked(appToken, isOn);
873 }
874 }
875
Santos Cordonc22c5632017-06-21 16:03:49 -0700876 private void registerDefaultDisplayAdapters() {
877 // Register default display adapters.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700878 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700879 // main display adapter
Mike Lockwoode63f6f72013-11-15 11:01:47 -0800880 registerDisplayAdapterLocked(new LocalDisplayAdapter(
881 mSyncRoot, mContext, mHandler, mDisplayAdapterListener));
Santos Cordonc22c5632017-06-21 16:03:49 -0700882
883 // Standalone VR devices rely on a virtual display as their primary display for
884 // 2D UI. We register virtual display adapter along side the main display adapter
885 // here so that it is ready by the time the system sends the home Intent for
886 // early apps like SetupWizard/Launcher. In particular, SUW is displayed using
887 // the virtual display inside VR before any VR-specific apps even run.
888 mVirtualDisplayAdapter = mInjector.getVirtualDisplayAdapter(mSyncRoot, mContext,
889 mHandler, mDisplayAdapterListener);
890 if (mVirtualDisplayAdapter != null) {
891 registerDisplayAdapterLocked(mVirtualDisplayAdapter);
892 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700893 }
894 }
895
896 private void registerAdditionalDisplayAdapters() {
897 synchronized (mSyncRoot) {
898 if (shouldRegisterNonEssentialDisplayAdaptersLocked()) {
Jeff Brown89d55462012-09-19 11:33:42 -0700899 registerOverlayDisplayAdapterLocked();
900 registerWifiDisplayAdapterLocked();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700901 }
902 }
903 }
904
Jeff Brown89d55462012-09-19 11:33:42 -0700905 private void registerOverlayDisplayAdapterLocked() {
906 registerDisplayAdapterLocked(new OverlayDisplayAdapter(
907 mSyncRoot, mContext, mHandler, mDisplayAdapterListener, mUiHandler));
908 }
909
910 private void registerWifiDisplayAdapterLocked() {
911 if (mContext.getResources().getBoolean(
Jeff Brownbbd28a22012-09-20 16:47:15 -0700912 com.android.internal.R.bool.config_enableWifiDisplay)
913 || SystemProperties.getInt(FORCE_WIFI_DISPLAY_ENABLE, -1) == 1) {
Jeff Brown89d55462012-09-19 11:33:42 -0700914 mWifiDisplayAdapter = new WifiDisplayAdapter(
915 mSyncRoot, mContext, mHandler, mDisplayAdapterListener,
916 mPersistentDataStore);
917 registerDisplayAdapterLocked(mWifiDisplayAdapter);
918 }
919 }
920
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700921 private boolean shouldRegisterNonEssentialDisplayAdaptersLocked() {
922 // In safe mode, we disable non-essential display adapters to give the user
923 // an opportunity to fix broken settings or other problems that might affect
924 // system stability.
925 // In only-core mode, we disable non-essential display adapters to minimize
926 // the number of dependencies that are started while in this mode and to
927 // prevent problems that might occur due to the device being encrypted.
928 return !mSafeMode && !mOnlyCore;
929 }
930
931 private void registerDisplayAdapterLocked(DisplayAdapter adapter) {
932 mDisplayAdapters.add(adapter);
933 adapter.registerLocked();
934 }
935
Jeff Brownbd6e1502012-08-28 03:27:37 -0700936 private void handleDisplayDeviceAdded(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700937 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700938 handleDisplayDeviceAddedLocked(device);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700939 }
940 }
941
Jeff Browna506a6e2013-06-04 00:02:38 -0700942 private void handleDisplayDeviceAddedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700943 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700944 if (mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700945 Slog.w(TAG, "Attempted to add already added display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700946 return;
947 }
948
Jeff Brown10acf6d2015-04-14 14:20:47 -0700949 Slog.i(TAG, "Display device added: " + info);
950 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700951
952 mDisplayDevices.add(device);
Michael Wright1c9977b2016-07-12 13:30:10 -0700953 LogicalDisplay display = addLogicalDisplayLocked(device);
Jeff Brown0033a862014-10-08 12:06:39 -0700954 Runnable work = updateDisplayStateLocked(device);
955 if (work != null) {
956 work.run();
957 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700958 scheduleTraversalLocked(false);
959 }
960
Jeff Brownbd6e1502012-08-28 03:27:37 -0700961 private void handleDisplayDeviceChanged(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700962 synchronized (mSyncRoot) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700963 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700964 if (!mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700965 Slog.w(TAG, "Attempted to change non-existent display device: " + info);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700966 return;
967 }
968
Jeff Brown10acf6d2015-04-14 14:20:47 -0700969 int diff = device.mDebugLastLoggedDeviceInfo.diff(info);
970 if (diff == DisplayDeviceInfo.DIFF_STATE) {
971 Slog.i(TAG, "Display device changed state: \"" + info.name
972 + "\", " + Display.stateToString(info.state));
973 } else if (diff != 0) {
974 Slog.i(TAG, "Display device changed: " + info);
975 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700976 if ((diff & DisplayDeviceInfo.DIFF_COLOR_MODE) != 0) {
977 try {
978 mPersistentDataStore.setColorMode(device, info.colorMode);
979 } finally {
980 mPersistentDataStore.saveIfNeeded();
981 }
982 }
Jeff Brown10acf6d2015-04-14 14:20:47 -0700983 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browne87bf032012-09-20 18:30:13 -0700984
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700985 device.applyPendingDisplayDeviceInfoChangesLocked();
986 if (updateLogicalDisplaysLocked()) {
Craig Mautner65d11b32012-10-01 13:59:52 -0700987 scheduleTraversalLocked(false);
Jeff Brown64a55af2012-08-26 02:47:39 -0700988 }
989 }
990 }
991
Jeff Brownbd6e1502012-08-28 03:27:37 -0700992 private void handleDisplayDeviceRemoved(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700993 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700994 handleDisplayDeviceRemovedLocked(device);
995 }
996 }
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700997
Jeff Browna506a6e2013-06-04 00:02:38 -0700998 private void handleDisplayDeviceRemovedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700999 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -07001000 if (!mDisplayDevices.remove(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -07001001 Slog.w(TAG, "Attempted to remove non-existent display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -07001002 return;
1003 }
1004
Jeff Brown10acf6d2015-04-14 14:20:47 -07001005 Slog.i(TAG, "Display device removed: " + info);
1006 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -07001007
Jeff Browna506a6e2013-06-04 00:02:38 -07001008 updateLogicalDisplaysLocked();
1009 scheduleTraversalLocked(false);
1010 }
1011
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001012 private void handleLogicalDisplayChanged(int displayId, @NonNull LogicalDisplay display) {
1013 if (displayId == Display.DEFAULT_DISPLAY) {
1014 recordTopInsetLocked(display);
1015 }
Tim Murray890ceb52020-01-30 10:12:40 -08001016 // We don't bother invalidating the display info caches here because any changes to the
1017 // display info will trigger a cache invalidation inside of LogicalDisplay before we hit
1018 // this point.
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001019 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
1020 }
1021
Tim Murray890ceb52020-01-30 10:12:40 -08001022 private void handleLogicalDisplayRemoved(int displayId) {
1023 DisplayManagerGlobal.invalidateLocalDisplayInfoCaches();
1024 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_REMOVED);
1025 }
1026
Jeff Brown8e5d33e2015-06-10 13:05:50 -07001027 private void applyGlobalDisplayStateLocked(List<Runnable> workQueue) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001028 final int count = mDisplayDevices.size();
1029 for (int i = 0; i < count; i++) {
1030 DisplayDevice device = mDisplayDevices.get(i);
Jeff Browne75926d2014-09-18 15:24:49 -07001031 Runnable runnable = updateDisplayStateLocked(device);
1032 if (runnable != null) {
1033 workQueue.add(runnable);
1034 }
Jeff Browna506a6e2013-06-04 00:02:38 -07001035 }
1036 }
1037
Jeff Browne75926d2014-09-18 15:24:49 -07001038 private Runnable updateDisplayStateLocked(DisplayDevice device) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001039 // Blank or unblank the display immediately to match the state requested
Jeff Brown037c33e2014-04-09 00:31:55 -07001040 // by the display power controller (if known).
Jeff Browna506a6e2013-06-04 00:02:38 -07001041 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
1042 if ((info.flags & DisplayDeviceInfo.FLAG_NEVER_BLANK) == 0) {
Fiona Campbelld4eb2952019-11-04 17:19:56 +00001043 return device.requestDisplayStateLocked(
1044 mGlobalDisplayState, mGlobalDisplayBrightness);
Craig Mautner4f67ba62012-08-02 11:23:00 -07001045 }
Jeff Browne75926d2014-09-18 15:24:49 -07001046 return null;
Craig Mautner4f67ba62012-08-02 11:23:00 -07001047 }
1048
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001049 // Adds a new logical display based on the given display device.
1050 // Sends notifications if needed.
Michael Wright1c9977b2016-07-12 13:30:10 -07001051 private LogicalDisplay addLogicalDisplayLocked(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001052 DisplayDeviceInfo deviceInfo = device.getDisplayDeviceInfoLocked();
1053 boolean isDefault = (deviceInfo.flags
1054 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0;
1055 if (isDefault && mLogicalDisplays.get(Display.DEFAULT_DISPLAY) != null) {
1056 Slog.w(TAG, "Ignoring attempt to add a second default display: " + deviceInfo);
1057 isDefault = false;
1058 }
1059
Jeff Brown27f1d672012-10-17 18:32:34 -07001060 if (!isDefault && mSingleDisplayDemoMode) {
1061 Slog.i(TAG, "Not creating a logical display for a secondary display "
1062 + " because single display demo mode is enabled: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -07001063 return null;
Jeff Brown27f1d672012-10-17 18:32:34 -07001064 }
1065
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001066 final int displayId = assignDisplayIdLocked(isDefault);
1067 final int layerStack = assignLayerStackLocked(displayId);
1068
Jeff Brownd728bf52012-09-08 18:05:28 -07001069 LogicalDisplay display = new LogicalDisplay(displayId, layerStack, device);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001070 display.updateLocked(mDisplayDevices);
1071 if (!display.isValidLocked()) {
1072 // This should never happen currently.
1073 Slog.w(TAG, "Ignoring display device because the logical display "
1074 + "created from it was not considered valid: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -07001075 return null;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001076 }
1077
Michael Wrighteedcbf12017-08-16 23:14:54 +01001078 configureColorModeLocked(display, device);
1079 if (isDefault) {
1080 recordStableDisplayStatsIfNeededLocked(display);
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001081 recordTopInsetLocked(display);
Michael Wrighteedcbf12017-08-16 23:14:54 +01001082 }
1083
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001084 mLogicalDisplays.put(displayId, display);
Tim Murray890ceb52020-01-30 10:12:40 -08001085 DisplayManagerGlobal.invalidateLocalDisplayInfoCaches();
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001086
1087 // Wake up waitForDefaultDisplay.
1088 if (isDefault) {
1089 mSyncRoot.notifyAll();
1090 }
1091
1092 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_ADDED);
Michael Wright1c9977b2016-07-12 13:30:10 -07001093 return display;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001094 }
1095
1096 private int assignDisplayIdLocked(boolean isDefault) {
1097 return isDefault ? Display.DEFAULT_DISPLAY : mNextNonDefaultDisplayId++;
1098 }
1099
1100 private int assignLayerStackLocked(int displayId) {
1101 // Currently layer stacks and display ids are the same.
1102 // This need not be the case.
1103 return displayId;
1104 }
1105
Michael Wrighteedcbf12017-08-16 23:14:54 +01001106 private void configureColorModeLocked(LogicalDisplay display, DisplayDevice device) {
1107 if (display.getPrimaryDisplayDeviceLocked() == device) {
1108 int colorMode = mPersistentDataStore.getColorMode(device);
1109 if (colorMode == Display.COLOR_MODE_INVALID) {
1110 if ((device.getDisplayDeviceInfoLocked().flags
1111 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
1112 colorMode = mDefaultDisplayDefaultColorMode;
1113 } else {
1114 colorMode = Display.COLOR_MODE_DEFAULT;
1115 }
1116 }
1117 display.setRequestedColorModeLocked(colorMode);
1118 }
1119 }
1120
1121 // If we've never recorded stable device stats for this device before and they aren't
1122 // explicitly configured, go ahead and record the stable device stats now based on the status
1123 // of the default display at first boot.
1124 private void recordStableDisplayStatsIfNeededLocked(LogicalDisplay d) {
1125 if (mStableDisplaySize.x <= 0 && mStableDisplaySize.y <= 0) {
1126 DisplayInfo info = d.getDisplayInfoLocked();
1127 setStableDisplaySizeLocked(info.getNaturalWidth(), info.getNaturalHeight());
1128 }
1129 }
1130
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001131 private void recordTopInsetLocked(@Nullable LogicalDisplay d) {
Winson72dbe7e2019-04-29 14:55:30 -07001132 // We must only persist the inset after boot has completed, otherwise we will end up
1133 // overwriting the persisted value before the masking flag has been loaded from the
1134 // resource overlay.
1135 if (!mSystemReady || d == null) {
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001136 return;
1137 }
1138 int topInset = d.getInsets().top;
1139 if (topInset == mDefaultDisplayTopInset) {
1140 return;
1141 }
1142 mDefaultDisplayTopInset = topInset;
1143 SystemProperties.set(PROP_DEFAULT_DISPLAY_TOP_INSET, Integer.toString(topInset));
1144 }
1145
Michael Wrighteedcbf12017-08-16 23:14:54 +01001146 private void setStableDisplaySizeLocked(int width, int height) {
1147 mStableDisplaySize = new Point(width, height);
1148 try {
1149 mPersistentDataStore.setStableDisplaySize(mStableDisplaySize);
1150 } finally {
1151 mPersistentDataStore.saveIfNeeded();
1152 }
1153 }
1154
Dan Gittik122df862018-03-28 16:59:22 +01001155 @VisibleForTesting
1156 Curve getMinimumBrightnessCurveInternal() {
1157 return mMinimumBrightnessCurve;
1158 }
1159
Peiyong Lin277eaff2019-01-16 16:18:22 -08001160 int getPreferredWideGamutColorSpaceIdInternal() {
1161 return mWideColorSpace.getId();
1162 }
1163
Michael Wrighteef0e132017-11-21 17:57:52 +00001164 private void setBrightnessConfigurationForUserInternal(
Dan Gittik7a32fba2018-03-28 12:19:38 +01001165 @Nullable BrightnessConfiguration c, @UserIdInt int userId,
Kenny Guy05ce8092018-01-17 13:44:20 +00001166 @Nullable String packageName) {
Dan Gittik122df862018-03-28 16:59:22 +01001167 validateBrightnessConfiguration(c);
Michael Wrighteef0e132017-11-21 17:57:52 +00001168 final int userSerial = getUserManager().getUserSerialNumber(userId);
1169 synchronized (mSyncRoot) {
1170 try {
Kenny Guy05ce8092018-01-17 13:44:20 +00001171 mPersistentDataStore.setBrightnessConfigurationForUser(c, userSerial,
1172 packageName);
Michael Wrighteef0e132017-11-21 17:57:52 +00001173 } finally {
1174 mPersistentDataStore.saveIfNeeded();
1175 }
1176 if (userId == mCurrentUserId) {
1177 mDisplayPowerController.setBrightnessConfiguration(c);
1178 }
1179 }
1180 }
1181
Dan Gittik122df862018-03-28 16:59:22 +01001182 @VisibleForTesting
1183 void validateBrightnessConfiguration(BrightnessConfiguration config) {
1184 if (config == null) {
1185 return;
1186 }
1187 if (isBrightnessConfigurationTooDark(config)) {
1188 throw new IllegalArgumentException("brightness curve is too dark");
1189 }
1190 }
1191
1192 private boolean isBrightnessConfigurationTooDark(BrightnessConfiguration config) {
1193 Pair<float[], float[]> curve = config.getCurve();
1194 float[] lux = curve.first;
1195 float[] nits = curve.second;
1196 for (int i = 0; i < lux.length; i++) {
1197 if (nits[i] < mMinimumBrightnessSpline.interpolate(lux[i])) {
1198 return true;
1199 }
1200 }
1201 return false;
1202 }
1203
Michael Wrighteef0e132017-11-21 17:57:52 +00001204 private void loadBrightnessConfiguration() {
1205 synchronized (mSyncRoot) {
1206 final int userSerial = getUserManager().getUserSerialNumber(mCurrentUserId);
1207 BrightnessConfiguration config =
1208 mPersistentDataStore.getBrightnessConfiguration(userSerial);
1209 mDisplayPowerController.setBrightnessConfiguration(config);
1210 }
1211 }
1212
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001213 // Updates all existing logical displays given the current set of display devices.
1214 // Removes invalid logical displays.
1215 // Sends notifications if needed.
1216 private boolean updateLogicalDisplaysLocked() {
1217 boolean changed = false;
1218 for (int i = mLogicalDisplays.size(); i-- > 0; ) {
1219 final int displayId = mLogicalDisplays.keyAt(i);
1220 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1221
1222 mTempDisplayInfo.copyFrom(display.getDisplayInfoLocked());
1223 display.updateLocked(mDisplayDevices);
1224 if (!display.isValidLocked()) {
1225 mLogicalDisplays.removeAt(i);
Tim Murray890ceb52020-01-30 10:12:40 -08001226 handleLogicalDisplayRemoved(displayId);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001227 changed = true;
1228 } else if (!mTempDisplayInfo.equals(display.getDisplayInfoLocked())) {
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001229 handleLogicalDisplayChanged(displayId, display);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001230 changed = true;
1231 }
1232 }
1233 return changed;
1234 }
1235
Robert Carrae606b42018-02-15 15:36:23 -08001236 private void performTraversalLocked(SurfaceControl.Transaction t) {
Jeff Brownd728bf52012-09-08 18:05:28 -07001237 // Clear all viewports before configuring displays so that we can keep
1238 // track of which ones we have configured.
1239 clearViewportsLocked();
1240
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001241 // Configure each display device.
1242 final int count = mDisplayDevices.size();
1243 for (int i = 0; i < count; i++) {
1244 DisplayDevice device = mDisplayDevices.get(i);
Robert Carrae606b42018-02-15 15:36:23 -08001245 configureDisplayLocked(t, device);
1246 device.performTraversalLocked(t);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001247 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001248
1249 // Tell the input system about these new viewports.
Jeff Brown4ccb8232014-01-16 22:16:42 -08001250 if (mInputManagerInternal != null) {
Jeff Brownd728bf52012-09-08 18:05:28 -07001251 mHandler.sendEmptyMessage(MSG_UPDATE_VIEWPORT);
1252 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001253 }
1254
Michael Wright3f145a22014-07-22 19:46:03 -07001255 private void setDisplayPropertiesInternal(int displayId, boolean hasContent,
Galia Peycheva088d7c02019-12-13 11:27:23 +01001256 float requestedRefreshRate, int requestedModeId, boolean preferMinimalPostProcessing,
Galia Peycheva056b3ee2019-06-26 14:05:12 +02001257 boolean inTraversal) {
Craig Mautner722285e2012-09-07 13:55:58 -07001258 synchronized (mSyncRoot) {
1259 LogicalDisplay display = mLogicalDisplays.get(displayId);
Michael Wright3f145a22014-07-22 19:46:03 -07001260 if (display == null) {
1261 return;
1262 }
Galia Peycheva056b3ee2019-06-26 14:05:12 +02001263
1264 boolean shouldScheduleTraversal = false;
1265
Michael Wright3f145a22014-07-22 19:46:03 -07001266 if (display.hasContentLocked() != hasContent) {
Jeff Brown33041bd2013-08-02 21:11:14 -07001267 if (DEBUG) {
1268 Slog.d(TAG, "Display " + displayId + " hasContent flag changed: "
1269 + "hasContent=" + hasContent + ", inTraversal=" + inTraversal);
1270 }
1271
Craig Mautner722285e2012-09-07 13:55:58 -07001272 display.setHasContentLocked(hasContent);
Galia Peycheva056b3ee2019-06-26 14:05:12 +02001273 shouldScheduleTraversal = true;
Craig Mautner722285e2012-09-07 13:55:58 -07001274 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001275 if (requestedModeId == 0 && requestedRefreshRate != 0) {
1276 // Scan supported modes returned by display.getInfo() to find a mode with the same
1277 // size as the default display mode but with the specified refresh rate instead.
1278 requestedModeId = display.getDisplayInfoLocked().findDefaultModeByRefreshRate(
1279 requestedRefreshRate);
1280 }
Michael Wrighta3dab232019-02-22 16:54:21 +00001281 mDisplayModeDirector.getAppRequestObserver().setAppRequestedMode(
1282 displayId, requestedModeId);
Galia Peycheva056b3ee2019-06-26 14:05:12 +02001283
Galia Peycheva088d7c02019-12-13 11:27:23 +01001284 if (display.getDisplayInfoLocked().minimalPostProcessingSupported) {
1285 boolean mppRequest = mMinimalPostProcessingAllowed && preferMinimalPostProcessing;
Galia Peycheva056b3ee2019-06-26 14:05:12 +02001286
Galia Peycheva088d7c02019-12-13 11:27:23 +01001287 if (display.getRequestedMinimalPostProcessingLocked() != mppRequest) {
1288 display.setRequestedMinimalPostProcessingLocked(mppRequest);
1289 shouldScheduleTraversal = true;
1290 }
Galia Peycheva056b3ee2019-06-26 14:05:12 +02001291 }
1292
1293 if (shouldScheduleTraversal) {
1294 scheduleTraversalLocked(inTraversal);
1295 }
Craig Mautner722285e2012-09-07 13:55:58 -07001296 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001297 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001298
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08001299 private void setDisplayOffsetsInternal(int displayId, int x, int y) {
1300 synchronized (mSyncRoot) {
1301 LogicalDisplay display = mLogicalDisplays.get(displayId);
1302 if (display == null) {
1303 return;
1304 }
1305 if (display.getDisplayOffsetXLocked() != x
1306 || display.getDisplayOffsetYLocked() != y) {
1307 if (DEBUG) {
1308 Slog.d(TAG, "Display " + displayId + " burn-in offset set to ("
1309 + x + ", " + y + ")");
1310 }
1311 display.setDisplayOffsetsLocked(x, y);
1312 scheduleTraversalLocked(false);
1313 }
1314 }
1315 }
1316
Sam Lin4c3ac2b2019-02-18 04:50:26 -08001317 private void setDisplayScalingDisabledInternal(int displayId, boolean disable) {
1318 synchronized (mSyncRoot) {
1319 final LogicalDisplay display = mLogicalDisplays.get(displayId);
1320 if (display == null) {
1321 return;
1322 }
1323 if (display.isDisplayScalingDisabled() != disable) {
1324 if (DEBUG) {
1325 Slog.d(TAG, "Display " + displayId + " content scaling disabled = " + disable);
1326 }
1327 display.setDisplayScalingDisabledLocked(disable);
1328 scheduleTraversalLocked(false);
1329 }
1330 }
1331 }
1332
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001333 // Updates the lists of UIDs that are present on displays.
1334 private void setDisplayAccessUIDsInternal(SparseArray<IntArray> newDisplayAccessUIDs) {
1335 synchronized (mSyncRoot) {
1336 mDisplayAccessUIDs.clear();
1337 for (int i = newDisplayAccessUIDs.size() - 1; i >= 0; i--) {
1338 mDisplayAccessUIDs.append(newDisplayAccessUIDs.keyAt(i),
1339 newDisplayAccessUIDs.valueAt(i));
1340 }
1341 }
1342 }
1343
1344 // Checks if provided UID's content is present on the display and UID has access to it.
1345 private boolean isUidPresentOnDisplayInternal(int uid, int displayId) {
1346 synchronized (mSyncRoot) {
1347 final IntArray displayUIDs = mDisplayAccessUIDs.get(displayId);
1348 return displayUIDs != null && displayUIDs.indexOf(uid) != -1;
1349 }
1350 }
1351
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001352 @Nullable
1353 private IBinder getDisplayToken(int displayId) {
1354 synchronized (mSyncRoot) {
1355 final LogicalDisplay display = mLogicalDisplays.get(displayId);
1356 if (display != null) {
1357 final DisplayDevice device = display.getPrimaryDisplayDeviceLocked();
1358 if (device != null) {
1359 return device.getDisplayTokenLocked();
1360 }
1361 }
1362 }
1363
1364 return null;
1365 }
1366
Jacky Kao333de4e2020-04-07 15:01:41 +08001367 private SurfaceControl.ScreenshotGraphicBuffer systemScreenshotInternal(int displayId) {
Vinit Nayak627ac302020-01-13 15:50:10 -08001368 synchronized (mSyncRoot) {
1369 final IBinder token = getDisplayToken(displayId);
1370 if (token == null) {
1371 return null;
1372 }
1373 final LogicalDisplay logicalDisplay = mLogicalDisplays.get(displayId);
1374 if (logicalDisplay == null) {
1375 return null;
1376 }
1377
1378 final DisplayInfo displayInfo = logicalDisplay.getDisplayInfoLocked();
Jacky Kao333de4e2020-04-07 15:01:41 +08001379 return SurfaceControl.screenshotToBufferWithSecureLayersUnsafe(token, new Rect(),
1380 displayInfo.getNaturalWidth(), displayInfo.getNaturalHeight(),
1381 false /* useIdentityTransform */, 0 /* rotation */);
1382 }
1383 }
1384
1385 private SurfaceControl.ScreenshotGraphicBuffer userScreenshotInternal(int displayId) {
1386 synchronized (mSyncRoot) {
1387 final IBinder token = getDisplayToken(displayId);
1388 if (token == null) {
1389 return null;
Jacky Kao316477c2020-03-23 16:30:04 +08001390 }
Jacky Kao333de4e2020-04-07 15:01:41 +08001391 final LogicalDisplay logicalDisplay = mLogicalDisplays.get(displayId);
1392 if (logicalDisplay == null) {
1393 return null;
1394 }
1395
1396 final DisplayInfo displayInfo = logicalDisplay.getDisplayInfoLocked();
1397 // Takes screenshot based on current device orientation.
1398 final Display display = DisplayManagerGlobal.getInstance()
1399 .getRealDisplay(displayId);
1400 if (display == null) {
1401 return null;
1402 }
1403 final Point displaySize = new Point();
1404 display.getRealSize(displaySize);
1405
1406 int rotation = displayInfo.rotation;
1407 // TODO (b/153382624) : This workaround solution would be removed after
1408 // SurfaceFlinger fixes the inconsistency with rotation direction issue.
1409 if (rotation == ROTATION_90 || rotation == ROTATION_270) {
1410 rotation = (rotation == ROTATION_90) ? ROTATION_270 : ROTATION_90;
1411 }
1412
1413 return SurfaceControl.screenshotToBuffer(token, new Rect(), displaySize.x,
1414 displaySize.y, false /* useIdentityTransform */, rotation /* rotation */);
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001415 }
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001416 }
1417
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001418 @VisibleForTesting
1419 DisplayedContentSamplingAttributes getDisplayedContentSamplingAttributesInternal(
1420 int displayId) {
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001421 final IBinder token = getDisplayToken(displayId);
1422 if (token == null) {
1423 return null;
1424 }
1425 return SurfaceControl.getDisplayedContentSamplingAttributes(token);
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001426 }
1427
1428 @VisibleForTesting
1429 boolean setDisplayedContentSamplingEnabledInternal(
1430 int displayId, boolean enable, int componentMask, int maxFrames) {
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001431 final IBinder token = getDisplayToken(displayId);
1432 if (token == null) {
1433 return false;
1434 }
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001435 return SurfaceControl.setDisplayedContentSamplingEnabled(
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001436 token, enable, componentMask, maxFrames);
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001437 }
1438
1439 @VisibleForTesting
1440 DisplayedContentSample getDisplayedContentSampleInternal(int displayId,
1441 long maxFrames, long timestamp) {
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001442 final IBinder token = getDisplayToken(displayId);
1443 if (token == null) {
1444 return null;
1445 }
1446 return SurfaceControl.getDisplayedContentSample(token, maxFrames, timestamp);
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001447 }
1448
Santos Cordonc4fd8e62019-12-18 13:01:05 +00001449 void resetBrightnessConfiguration() {
1450 setBrightnessConfigurationForUserInternal(null, mContext.getUserId(),
1451 mContext.getPackageName());
1452 }
1453
1454 void setAutoBrightnessLoggingEnabled(boolean enabled) {
1455 if (mDisplayPowerController != null) {
1456 synchronized (mSyncRoot) {
1457 mDisplayPowerController.setAutoBrightnessLoggingEnabled(enabled);
1458 }
1459 }
1460 }
1461
1462 void setDisplayWhiteBalanceLoggingEnabled(boolean enabled) {
1463 if (mDisplayPowerController != null) {
1464 synchronized (mSyncRoot) {
1465 mDisplayPowerController.setDisplayWhiteBalanceLoggingEnabled(enabled);
1466 }
1467 }
1468 }
1469
1470 void setAmbientColorTemperatureOverride(float cct) {
1471 if (mDisplayPowerController != null) {
1472 synchronized (mSyncRoot) {
1473 mDisplayPowerController.setAmbientColorTemperatureOverride(cct);
1474 }
1475 }
1476 }
1477
Ana Kruleca74a8642019-11-14 00:51:00 +01001478 private void onDesiredDisplayModeSpecsChangedInternal() {
Michael Wrighta3dab232019-02-22 16:54:21 +00001479 boolean changed = false;
1480 synchronized (mSyncRoot) {
1481 final int count = mLogicalDisplays.size();
1482 for (int i = 0; i < count; i++) {
1483 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1484 int displayId = mLogicalDisplays.keyAt(i);
Ana Kruleca74a8642019-11-14 00:51:00 +01001485 DisplayModeDirector.DesiredDisplayModeSpecs desiredDisplayModeSpecs =
1486 mDisplayModeDirector.getDesiredDisplayModeSpecs(displayId);
1487 DisplayModeDirector.DesiredDisplayModeSpecs existingDesiredDisplayModeSpecs =
1488 display.getDesiredDisplayModeSpecsLocked();
1489 if (DEBUG) {
1490 Slog.i(TAG,
1491 "Comparing display specs: " + desiredDisplayModeSpecs
1492 + ", existing: " + existingDesiredDisplayModeSpecs);
1493 }
1494 if (!desiredDisplayModeSpecs.equals(existingDesiredDisplayModeSpecs)) {
1495 display.setDesiredDisplayModeSpecsLocked(desiredDisplayModeSpecs);
Michael Wrighta3dab232019-02-22 16:54:21 +00001496 changed = true;
1497 }
1498 }
1499 if (changed) {
1500 scheduleTraversalLocked(false);
1501 }
1502 }
1503 }
1504
Jeff Brownd728bf52012-09-08 18:05:28 -07001505 private void clearViewportsLocked() {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001506 mViewports.clear();
Craig Mautner722285e2012-09-07 13:55:58 -07001507 }
1508
Robert Carrae606b42018-02-15 15:36:23 -08001509 private void configureDisplayLocked(SurfaceControl.Transaction t, DisplayDevice device) {
Jeff Brownd14c8c92014-01-07 18:13:09 -08001510 final DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
1511 final boolean ownContent = (info.flags & DisplayDeviceInfo.FLAG_OWN_CONTENT_ONLY) != 0;
Jeff Browna506a6e2013-06-04 00:02:38 -07001512
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001513 // Find the logical display that the display device is showing.
Jeff Brownd14c8c92014-01-07 18:13:09 -08001514 // Certain displays only ever show their own content.
Craig Mautner722285e2012-09-07 13:55:58 -07001515 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
Jeff Brownd14c8c92014-01-07 18:13:09 -08001516 if (!ownContent) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001517 if (display != null && !display.hasContentLocked()) {
1518 // If the display does not have any content of its own, then
b0202.jung925435c2020-01-08 18:46:59 +09001519 // automatically mirror the requested logical display contents if possible.
1520 display = mLogicalDisplays.get(device.getDisplayIdToMirrorLocked());
Jeff Browna506a6e2013-06-04 00:02:38 -07001521 }
1522 if (display == null) {
1523 display = mLogicalDisplays.get(Display.DEFAULT_DISPLAY);
1524 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001525 }
1526
1527 // Apply the logical display configuration to the display device.
1528 if (display == null) {
1529 // TODO: no logical display for the device, blank it
Jeff Brownd728bf52012-09-08 18:05:28 -07001530 Slog.w(TAG, "Missing logical display to use for physical display device: "
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001531 + device.getDisplayDeviceInfoLocked());
Jeff Brownd728bf52012-09-08 18:05:28 -07001532 return;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001533 }
Robert Carrae606b42018-02-15 15:36:23 -08001534 display.configureDisplayLocked(t, device, info.state == Display.STATE_OFF);
Arthur Hung41e81e72018-10-31 18:04:56 +08001535 final int viewportType;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001536 // Update the corresponding viewport.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001537 if ((info.flags & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
Arthur Hung41e81e72018-10-31 18:04:56 +08001538 viewportType = VIEWPORT_INTERNAL;
1539 } else if (info.touch == DisplayDeviceInfo.TOUCH_EXTERNAL) {
1540 viewportType = VIEWPORT_EXTERNAL;
1541 } else if (info.touch == DisplayDeviceInfo.TOUCH_VIRTUAL
1542 && !TextUtils.isEmpty(info.uniqueId)) {
1543 viewportType = VIEWPORT_VIRTUAL;
1544 } else {
Arthur Hung46c4e582019-02-12 15:55:28 +08001545 Slog.i(TAG, "Display " + info + " does not support input device matching.");
Arthur Hung41e81e72018-10-31 18:04:56 +08001546 return;
Jeff Brownd728bf52012-09-08 18:05:28 -07001547 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001548
Arthur Hung41e81e72018-10-31 18:04:56 +08001549 populateViewportLocked(viewportType, display.getDisplayIdLocked(), device, info.uniqueId);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001550 }
1551
1552 /**
1553 * Get internal or external viewport. Create it if does not currently exist.
1554 * @param viewportType - either INTERNAL or EXTERNAL
1555 * @return the viewport with the requested type
1556 */
Arthur Hung41e81e72018-10-31 18:04:56 +08001557 private DisplayViewport getViewportLocked(int viewportType, String uniqueId) {
1558 if (viewportType != VIEWPORT_INTERNAL && viewportType != VIEWPORT_EXTERNAL
1559 && viewportType != VIEWPORT_VIRTUAL) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001560 Slog.wtf(TAG, "Cannot call getViewportByTypeLocked for type "
1561 + DisplayViewport.typeToString(viewportType));
1562 return null;
1563 }
Arthur Hung41e81e72018-10-31 18:04:56 +08001564
1565 // Only allow a single INTERNAL or EXTERNAL viewport by forcing their uniqueIds
1566 // to be identical (in particular, empty).
1567 // TODO (b/116824030) allow multiple EXTERNAL viewports and remove this function.
1568 if (viewportType != VIEWPORT_VIRTUAL) {
1569 uniqueId = "";
1570 }
1571
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001572 DisplayViewport viewport;
1573 final int count = mViewports.size();
1574 for (int i = 0; i < count; i++) {
1575 viewport = mViewports.get(i);
Arthur Hung41e81e72018-10-31 18:04:56 +08001576 if (viewport.type == viewportType && uniqueId.equals(viewport.uniqueId)) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001577 return viewport;
1578 }
1579 }
1580
Arthur Hung41e81e72018-10-31 18:04:56 +08001581 // Creates the viewport if none exists.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001582 viewport = new DisplayViewport();
1583 viewport.type = viewportType;
Arthur Hung41e81e72018-10-31 18:04:56 +08001584 viewport.uniqueId = uniqueId;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001585 mViewports.add(viewport);
1586 return viewport;
1587 }
1588
Arthur Hung41e81e72018-10-31 18:04:56 +08001589 private void populateViewportLocked(int viewportType,
1590 int displayId, DisplayDevice device, String uniqueId) {
1591 final DisplayViewport viewport = getViewportLocked(viewportType, uniqueId);
Jeff Brownd728bf52012-09-08 18:05:28 -07001592 device.populateViewportLocked(viewport);
Arthur Hung41e81e72018-10-31 18:04:56 +08001593 viewport.valid = true;
1594 viewport.displayId = displayId;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001595 }
1596
1597 private LogicalDisplay findLogicalDisplayForDeviceLocked(DisplayDevice device) {
1598 final int count = mLogicalDisplays.size();
1599 for (int i = 0; i < count; i++) {
1600 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1601 if (display.getPrimaryDisplayDeviceLocked() == device) {
1602 return display;
1603 }
1604 }
1605 return null;
1606 }
1607
Jeff Brownbd6e1502012-08-28 03:27:37 -07001608 private void sendDisplayEventLocked(int displayId, int event) {
1609 Message msg = mHandler.obtainMessage(MSG_DELIVER_DISPLAY_EVENT, displayId, event);
1610 mHandler.sendMessage(msg);
1611 }
1612
Robert Carrae606b42018-02-15 15:36:23 -08001613 // Requests that performTraversals be called at a
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001614 // later time to apply changes to surfaces and displays.
Craig Mautner65d11b32012-10-01 13:59:52 -07001615 private void scheduleTraversalLocked(boolean inTraversal) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001616 if (!mPendingTraversal && mWindowManagerInternal != null) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001617 mPendingTraversal = true;
Craig Mautner65d11b32012-10-01 13:59:52 -07001618 if (!inTraversal) {
1619 mHandler.sendEmptyMessage(MSG_REQUEST_TRAVERSAL);
1620 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001621 }
1622 }
1623
1624 // Runs on Handler thread.
1625 // Delivers display event notifications to callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001626 private void deliverDisplayEvent(int displayId, int event) {
1627 if (DEBUG) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001628 Slog.d(TAG, "Delivering display event: displayId="
1629 + displayId + ", event=" + event);
Jeff Brownfa25bf52012-07-23 19:26:30 -07001630 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07001631
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001632 // Grab the lock and copy the callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001633 final int count;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001634 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001635 count = mCallbacks.size();
1636 mTempCallbacks.clear();
1637 for (int i = 0; i < count; i++) {
1638 mTempCallbacks.add(mCallbacks.valueAt(i));
Craig Mautner4f67ba62012-08-02 11:23:00 -07001639 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001640 }
Craig Mautner4f67ba62012-08-02 11:23:00 -07001641
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001642 // After releasing the lock, send the notifications out.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001643 for (int i = 0; i < count; i++) {
1644 mTempCallbacks.get(i).notifyDisplayEventAsync(displayId, event);
1645 }
1646 mTempCallbacks.clear();
Craig Mautner4f67ba62012-08-02 11:23:00 -07001647 }
1648
Michael Wrightc39d47a2014-07-08 18:07:36 -07001649 private IMediaProjectionManager getProjectionService() {
1650 if (mProjectionService == null) {
1651 IBinder b = ServiceManager.getService(Context.MEDIA_PROJECTION_SERVICE);
1652 mProjectionService = IMediaProjectionManager.Stub.asInterface(b);
1653 }
1654 return mProjectionService;
1655 }
1656
Michael Wrighteef0e132017-11-21 17:57:52 +00001657 private UserManager getUserManager() {
1658 return mContext.getSystemService(UserManager.class);
1659 }
1660
Jeff Brown4ccb8232014-01-16 22:16:42 -08001661 private void dumpInternal(PrintWriter pw) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001662 pw.println("DISPLAY MANAGER (dumpsys display)");
Jeff Brownfa25bf52012-07-23 19:26:30 -07001663
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001664 synchronized (mSyncRoot) {
Jeff Brown9e316a12012-10-08 19:17:06 -07001665 pw.println(" mOnlyCode=" + mOnlyCore);
1666 pw.println(" mSafeMode=" + mSafeMode);
1667 pw.println(" mPendingTraversal=" + mPendingTraversal);
Jeff Brown037c33e2014-04-09 00:31:55 -07001668 pw.println(" mGlobalDisplayState=" + Display.stateToString(mGlobalDisplayState));
Jeff Brown9e316a12012-10-08 19:17:06 -07001669 pw.println(" mNextNonDefaultDisplayId=" + mNextNonDefaultDisplayId);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001670 pw.println(" mViewports=" + mViewports);
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -07001671 pw.println(" mDefaultDisplayDefaultColorMode=" + mDefaultDisplayDefaultColorMode);
Jeff Brown27f1d672012-10-17 18:32:34 -07001672 pw.println(" mSingleDisplayDemoMode=" + mSingleDisplayDemoMode);
Jeff Brownce468a32013-11-21 16:42:03 -08001673 pw.println(" mWifiDisplayScanRequestCount=" + mWifiDisplayScanRequestCount);
Michael Wrighteedcbf12017-08-16 23:14:54 +01001674 pw.println(" mStableDisplaySize=" + mStableDisplaySize);
Kenny Guy2047db92019-02-21 13:04:05 +00001675 pw.println(" mMinimumBrightnessCurve=" + mMinimumBrightnessCurve);
Jeff Brown9e316a12012-10-08 19:17:06 -07001676
Jeff Brownbd6e1502012-08-28 03:27:37 -07001677 IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001678 ipw.increaseIndent();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001679
1680 pw.println();
1681 pw.println("Display Adapters: size=" + mDisplayAdapters.size());
Jeff Brown848c2dc2012-08-19 20:18:08 -07001682 for (DisplayAdapter adapter : mDisplayAdapters) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001683 pw.println(" " + adapter.getName());
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001684 adapter.dumpLocked(ipw);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001685 }
Craig Mautner9de49362012-08-02 14:30:30 -07001686
Jeff Brownbd6e1502012-08-28 03:27:37 -07001687 pw.println();
1688 pw.println("Display Devices: size=" + mDisplayDevices.size());
1689 for (DisplayDevice device : mDisplayDevices) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001690 pw.println(" " + device.getDisplayDeviceInfoLocked());
1691 device.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001692 }
1693
1694 final int logicalDisplayCount = mLogicalDisplays.size();
1695 pw.println();
1696 pw.println("Logical Displays: size=" + logicalDisplayCount);
1697 for (int i = 0; i < logicalDisplayCount; i++) {
1698 int displayId = mLogicalDisplays.keyAt(i);
1699 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1700 pw.println(" Display " + displayId + ":");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001701 display.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001702 }
Jeff Brownce468a32013-11-21 16:42:03 -08001703
Michael Wrighta3dab232019-02-22 16:54:21 +00001704 pw.println();
1705 mDisplayModeDirector.dump(pw);
1706
Jeff Brownce468a32013-11-21 16:42:03 -08001707 final int callbackCount = mCallbacks.size();
1708 pw.println();
1709 pw.println("Callbacks: size=" + callbackCount);
1710 for (int i = 0; i < callbackCount; i++) {
1711 CallbackRecord callback = mCallbacks.valueAt(i);
1712 pw.println(" " + i + ": mPid=" + callback.mPid
1713 + ", mWifiDisplayScanRequested=" + callback.mWifiDisplayScanRequested);
1714 }
Jeff Brownad9ef192014-04-08 17:26:30 -07001715
1716 if (mDisplayPowerController != null) {
1717 mDisplayPowerController.dump(pw);
1718 }
Michael Wright1c9977b2016-07-12 13:30:10 -07001719
1720 pw.println();
1721 mPersistentDataStore.dump(pw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001722 }
1723 }
1724
Dan Gittik122df862018-03-28 16:59:22 +01001725 private static float[] getFloatArray(TypedArray array) {
1726 int length = array.length();
1727 float[] floatArray = new float[length];
1728 for (int i = 0; i < length; i++) {
1729 floatArray[i] = array.getFloat(i, Float.NaN);
1730 }
1731 array.recycle();
1732 return floatArray;
1733 }
1734
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001735 /**
1736 * This is the object that everything in the display manager locks on.
1737 * We make it an inner class within the {@link DisplayManagerService} to so that it is
1738 * clear that the object belongs to the display manager service and that it is
1739 * a unique object with a special purpose.
1740 */
1741 public static final class SyncRoot {
1742 }
1743
Santos Cordonee8931e2017-04-05 10:31:15 -07001744 @VisibleForTesting
1745 static class Injector {
1746 VirtualDisplayAdapter getVirtualDisplayAdapter(SyncRoot syncRoot, Context context,
1747 Handler handler, DisplayAdapter.Listener displayAdapterListener) {
1748 return new VirtualDisplayAdapter(syncRoot, context, handler, displayAdapterListener);
1749 }
Santos Cordonc22c5632017-06-21 16:03:49 -07001750
1751 long getDefaultDisplayDelayTimeout() {
1752 return WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT;
1753 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001754 }
1755
Alex Sakhartchouk879d24f2017-06-20 22:01:19 -04001756 @VisibleForTesting
1757 DisplayDeviceInfo getDisplayDeviceInfoInternal(int displayId) {
1758 synchronized (mSyncRoot) {
1759 LogicalDisplay display = mLogicalDisplays.get(displayId);
1760 if (display != null) {
1761 DisplayDevice displayDevice = display.getPrimaryDisplayDeviceLocked();
1762 return displayDevice.getDisplayDeviceInfoLocked();
1763 }
1764 return null;
1765 }
1766 }
1767
b0202.jung925435c2020-01-08 18:46:59 +09001768 @VisibleForTesting
1769 int getDisplayIdToMirrorInternal(int displayId) {
1770 synchronized (mSyncRoot) {
1771 LogicalDisplay display = mLogicalDisplays.get(displayId);
1772 if (display != null) {
1773 DisplayDevice displayDevice = display.getPrimaryDisplayDeviceLocked();
1774 return displayDevice.getDisplayIdToMirrorLocked();
1775 }
1776 return Display.INVALID_DISPLAY;
1777 }
1778 }
1779
1780 @VisibleForTesting
1781 Surface getVirtualDisplaySurfaceInternal(IBinder appToken) {
1782 synchronized (mSyncRoot) {
1783 if (mVirtualDisplayAdapter == null) {
1784 return null;
1785 }
1786 return mVirtualDisplayAdapter.getVirtualDisplaySurfaceLocked(appToken);
1787 }
1788 }
1789
Jeff Brownbd6e1502012-08-28 03:27:37 -07001790 private final class DisplayManagerHandler extends Handler {
1791 public DisplayManagerHandler(Looper looper) {
1792 super(looper, null, true /*async*/);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001793 }
Jeff Brownbf5740e2012-08-19 23:20:02 -07001794
Jeff Brownbd6e1502012-08-28 03:27:37 -07001795 @Override
1796 public void handleMessage(Message msg) {
1797 switch (msg.what) {
Santos Cordonc22c5632017-06-21 16:03:49 -07001798 case MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS:
1799 registerDefaultDisplayAdapters();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001800 break;
1801
1802 case MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS:
1803 registerAdditionalDisplayAdapters();
1804 break;
1805
1806 case MSG_DELIVER_DISPLAY_EVENT:
1807 deliverDisplayEvent(msg.arg1, msg.arg2);
1808 break;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001809
1810 case MSG_REQUEST_TRAVERSAL:
Jeff Brown4ccb8232014-01-16 22:16:42 -08001811 mWindowManagerInternal.requestTraversalFromDisplayManager();
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001812 break;
Jeff Brownd728bf52012-09-08 18:05:28 -07001813
1814 case MSG_UPDATE_VIEWPORT: {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001815 final boolean changed;
Jeff Brownd728bf52012-09-08 18:05:28 -07001816 synchronized (mSyncRoot) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001817 changed = !mTempViewports.equals(mViewports);
1818 if (changed) {
1819 mTempViewports.clear();
1820 for (DisplayViewport d : mViewports) {
1821 mTempViewports.add(d.makeCopy());
1822 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001823 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001824 }
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001825 if (changed) {
1826 mInputManagerInternal.setDisplayViewports(mTempViewports);
1827 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001828 break;
1829 }
Kenny Guy22bd0442017-10-26 00:15:54 +01001830
Michael Wrighteef0e132017-11-21 17:57:52 +00001831 case MSG_LOAD_BRIGHTNESS_CONFIGURATION:
1832 loadBrightnessConfiguration();
1833 break;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001834 }
1835 }
1836 }
1837
1838 private final class DisplayAdapterListener implements DisplayAdapter.Listener {
1839 @Override
1840 public void onDisplayDeviceEvent(DisplayDevice device, int event) {
1841 switch (event) {
1842 case DisplayAdapter.DISPLAY_DEVICE_EVENT_ADDED:
1843 handleDisplayDeviceAdded(device);
1844 break;
1845
1846 case DisplayAdapter.DISPLAY_DEVICE_EVENT_CHANGED:
1847 handleDisplayDeviceChanged(device);
1848 break;
1849
1850 case DisplayAdapter.DISPLAY_DEVICE_EVENT_REMOVED:
1851 handleDisplayDeviceRemoved(device);
1852 break;
1853 }
1854 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001855
1856 @Override
1857 public void onTraversalRequested() {
1858 synchronized (mSyncRoot) {
Craig Mautner65d11b32012-10-01 13:59:52 -07001859 scheduleTraversalLocked(false);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001860 }
1861 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001862 }
1863
1864 private final class CallbackRecord implements DeathRecipient {
Jeff Brownce468a32013-11-21 16:42:03 -08001865 public final int mPid;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001866 private final IDisplayManagerCallback mCallback;
1867
Jeff Brownce468a32013-11-21 16:42:03 -08001868 public boolean mWifiDisplayScanRequested;
1869
Jeff Brownbd6e1502012-08-28 03:27:37 -07001870 public CallbackRecord(int pid, IDisplayManagerCallback callback) {
1871 mPid = pid;
1872 mCallback = callback;
1873 }
1874
1875 @Override
1876 public void binderDied() {
1877 if (DEBUG) {
1878 Slog.d(TAG, "Display listener for pid " + mPid + " died.");
1879 }
Jeff Brownce468a32013-11-21 16:42:03 -08001880 onCallbackDied(this);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001881 }
1882
1883 public void notifyDisplayEventAsync(int displayId, int event) {
1884 try {
1885 mCallback.onDisplayEvent(displayId, event);
1886 } catch (RemoteException ex) {
1887 Slog.w(TAG, "Failed to notify process "
1888 + mPid + " that displays changed, assuming it died.", ex);
1889 binderDied();
1890 }
1891 }
1892 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001893
Santos Cordonee8931e2017-04-05 10:31:15 -07001894 @VisibleForTesting
1895 final class BinderService extends IDisplayManager.Stub {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001896 /**
1897 * Returns information about the specified logical display.
1898 *
1899 * @param displayId The logical display id.
lumarkec75b422019-01-07 15:58:38 +08001900 * @return The logical display info, return {@code null} if the display does not exist or
1901 * the calling UID isn't present on the display. The returned object must be treated as
1902 * immutable.
Jeff Brown4ccb8232014-01-16 22:16:42 -08001903 */
1904 @Override // Binder call
1905 public DisplayInfo getDisplayInfo(int displayId) {
1906 final int callingUid = Binder.getCallingUid();
1907 final long token = Binder.clearCallingIdentity();
1908 try {
1909 return getDisplayInfoInternal(displayId, callingUid);
1910 } finally {
1911 Binder.restoreCallingIdentity(token);
1912 }
1913 }
1914
1915 /**
1916 * Returns the list of all display ids.
1917 */
1918 @Override // Binder call
1919 public int[] getDisplayIds() {
1920 final int callingUid = Binder.getCallingUid();
1921 final long token = Binder.clearCallingIdentity();
1922 try {
1923 return getDisplayIdsInternal(callingUid);
1924 } finally {
1925 Binder.restoreCallingIdentity(token);
1926 }
1927 }
1928
lumarkec75b422019-01-07 15:58:38 +08001929 @Override // Binder call
1930 public boolean isUidPresentOnDisplay(int uid, int displayId) {
1931 final long token = Binder.clearCallingIdentity();
1932 try {
1933 return isUidPresentOnDisplayInternal(uid, displayId);
1934 } finally {
1935 Binder.restoreCallingIdentity(token);
1936 }
1937 }
1938
Michael Wrighteedcbf12017-08-16 23:14:54 +01001939 /**
1940 * Returns the stable device display size, in pixels.
1941 */
1942 @Override // Binder call
1943 public Point getStableDisplaySize() {
1944 final long token = Binder.clearCallingIdentity();
1945 try {
1946 return getStableDisplaySizeInternal();
1947 } finally {
1948 Binder.restoreCallingIdentity(token);
1949 }
1950 }
1951
Jeff Brown4ccb8232014-01-16 22:16:42 -08001952 @Override // Binder call
1953 public void registerCallback(IDisplayManagerCallback callback) {
1954 if (callback == null) {
1955 throw new IllegalArgumentException("listener must not be null");
1956 }
1957
1958 final int callingPid = Binder.getCallingPid();
1959 final long token = Binder.clearCallingIdentity();
1960 try {
1961 registerCallbackInternal(callback, callingPid);
1962 } finally {
1963 Binder.restoreCallingIdentity(token);
1964 }
1965 }
1966
1967 @Override // Binder call
1968 public void startWifiDisplayScan() {
1969 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1970 "Permission required to start wifi display scans");
1971
1972 final int callingPid = Binder.getCallingPid();
1973 final long token = Binder.clearCallingIdentity();
1974 try {
1975 startWifiDisplayScanInternal(callingPid);
1976 } finally {
1977 Binder.restoreCallingIdentity(token);
1978 }
1979 }
1980
1981 @Override // Binder call
1982 public void stopWifiDisplayScan() {
1983 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1984 "Permission required to stop wifi display scans");
1985
1986 final int callingPid = Binder.getCallingPid();
1987 final long token = Binder.clearCallingIdentity();
1988 try {
1989 stopWifiDisplayScanInternal(callingPid);
1990 } finally {
1991 Binder.restoreCallingIdentity(token);
1992 }
1993 }
1994
1995 @Override // Binder call
1996 public void connectWifiDisplay(String address) {
1997 if (address == null) {
1998 throw new IllegalArgumentException("address must not be null");
1999 }
2000 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
2001 "Permission required to connect to a wifi display");
2002
2003 final long token = Binder.clearCallingIdentity();
2004 try {
2005 connectWifiDisplayInternal(address);
2006 } finally {
2007 Binder.restoreCallingIdentity(token);
2008 }
2009 }
2010
2011 @Override // Binder call
2012 public void disconnectWifiDisplay() {
2013 // This request does not require special permissions.
2014 // Any app can request disconnection from the currently active wifi display.
2015 // This exception should no longer be needed once wifi display control moves
2016 // to the media router service.
2017
2018 final long token = Binder.clearCallingIdentity();
2019 try {
2020 disconnectWifiDisplayInternal();
2021 } finally {
2022 Binder.restoreCallingIdentity(token);
2023 }
2024 }
2025
2026 @Override // Binder call
2027 public void renameWifiDisplay(String address, String alias) {
2028 if (address == null) {
2029 throw new IllegalArgumentException("address must not be null");
2030 }
2031 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
2032 "Permission required to rename to a wifi display");
2033
2034 final long token = Binder.clearCallingIdentity();
2035 try {
2036 renameWifiDisplayInternal(address, alias);
2037 } finally {
2038 Binder.restoreCallingIdentity(token);
2039 }
2040 }
2041
2042 @Override // Binder call
2043 public void forgetWifiDisplay(String address) {
2044 if (address == null) {
2045 throw new IllegalArgumentException("address must not be null");
2046 }
2047 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
2048 "Permission required to forget to a wifi display");
2049
2050 final long token = Binder.clearCallingIdentity();
2051 try {
2052 forgetWifiDisplayInternal(address);
2053 } finally {
2054 Binder.restoreCallingIdentity(token);
2055 }
2056 }
2057
2058 @Override // Binder call
2059 public void pauseWifiDisplay() {
2060 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
2061 "Permission required to pause a wifi display session");
2062
2063 final long token = Binder.clearCallingIdentity();
2064 try {
2065 pauseWifiDisplayInternal();
2066 } finally {
2067 Binder.restoreCallingIdentity(token);
2068 }
2069 }
2070
2071 @Override // Binder call
2072 public void resumeWifiDisplay() {
2073 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
2074 "Permission required to resume a wifi display session");
2075
2076 final long token = Binder.clearCallingIdentity();
2077 try {
2078 resumeWifiDisplayInternal();
2079 } finally {
2080 Binder.restoreCallingIdentity(token);
2081 }
2082 }
2083
2084 @Override // Binder call
2085 public WifiDisplayStatus getWifiDisplayStatus() {
2086 // This request does not require special permissions.
2087 // Any app can get information about available wifi displays.
2088
2089 final long token = Binder.clearCallingIdentity();
2090 try {
2091 return getWifiDisplayStatusInternal();
2092 } finally {
2093 Binder.restoreCallingIdentity(token);
2094 }
2095 }
2096
2097 @Override // Binder call
Michael Wright1c9977b2016-07-12 13:30:10 -07002098 public void requestColorMode(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +01002099 mContext.enforceCallingOrSelfPermission(
Michael Wright1c9977b2016-07-12 13:30:10 -07002100 Manifest.permission.CONFIGURE_DISPLAY_COLOR_MODE,
2101 "Permission required to change the display color mode");
Michael Wright58e829f2015-09-15 00:13:26 +01002102 final long token = Binder.clearCallingIdentity();
2103 try {
Michael Wright1c9977b2016-07-12 13:30:10 -07002104 requestColorModeInternal(displayId, colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +01002105 } finally {
2106 Binder.restoreCallingIdentity(token);
2107 }
2108 }
2109
2110 @Override // Binder call
b0202.jung925435c2020-01-08 18:46:59 +09002111 public int createVirtualDisplay(VirtualDisplayConfig virtualDisplayConfig,
2112 IVirtualDisplayCallback callback, IMediaProjection projection, String packageName) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08002113 final int callingUid = Binder.getCallingUid();
2114 if (!validatePackageName(callingUid, packageName)) {
2115 throw new SecurityException("packageName must match the calling uid");
2116 }
Michael Wright75ee9fc2014-09-01 19:55:22 -07002117 if (callback == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08002118 throw new IllegalArgumentException("appToken must not be null");
2119 }
b0202.jung925435c2020-01-08 18:46:59 +09002120 if (virtualDisplayConfig == null) {
2121 throw new IllegalArgumentException("virtualDisplayConfig must not be null");
Jeff Brown4ccb8232014-01-16 22:16:42 -08002122 }
b0202.jung925435c2020-01-08 18:46:59 +09002123 final Surface surface = virtualDisplayConfig.getSurface();
2124 int flags = virtualDisplayConfig.getFlags();
2125
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07002126 if (surface != null && surface.isSingleBuffered()) {
Pablo Ceballosaff2f942016-07-29 14:49:55 -07002127 throw new IllegalArgumentException("Surface can't be single-buffered");
2128 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07002129
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002130 if ((flags & VIRTUAL_DISPLAY_FLAG_PUBLIC) != 0) {
2131 flags |= VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
2132
2133 // Public displays can't be allowed to show content when locked.
Andrii Kulian7211d2e2017-01-27 15:58:05 -08002134 if ((flags & VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD) != 0) {
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002135 throw new IllegalArgumentException(
2136 "Public display must not be marked as SHOW_WHEN_LOCKED_INSECURE");
2137 }
Michael Wright6720be42014-07-29 19:14:16 -07002138 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002139 if ((flags & VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY) != 0) {
2140 flags &= ~VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
Michael Wright6720be42014-07-29 19:14:16 -07002141 }
2142
Michael Wrightc39d47a2014-07-08 18:07:36 -07002143 if (projection != null) {
2144 try {
2145 if (!getProjectionService().isValidMediaProjection(projection)) {
2146 throw new SecurityException("Invalid media projection");
2147 }
Michael Wright6720be42014-07-29 19:14:16 -07002148 flags = projection.applyVirtualDisplayFlags(flags);
Michael Wrightc39d47a2014-07-08 18:07:36 -07002149 } catch (RemoteException e) {
Michael Wright6720be42014-07-29 19:14:16 -07002150 throw new SecurityException("unable to validate media projection or flags");
Michael Wrightc39d47a2014-07-08 18:07:36 -07002151 }
2152 }
2153
Michael Wright6720be42014-07-29 19:14:16 -07002154 if (callingUid != Process.SYSTEM_UID &&
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08002155 (flags & VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07002156 if (!canProjectVideo(projection)) {
2157 throw new SecurityException("Requires CAPTURE_VIDEO_OUTPUT or "
2158 + "CAPTURE_SECURE_VIDEO_OUTPUT permission, or an appropriate "
2159 + "MediaProjection token in order to create a screen sharing virtual "
2160 + "display.");
2161 }
2162 }
Santos Cordonb6992f22018-01-30 14:51:20 -08002163 if (callingUid != Process.SYSTEM_UID && (flags & VIRTUAL_DISPLAY_FLAG_SECURE) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07002164 if (!canProjectSecureVideo(projection)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08002165 throw new SecurityException("Requires CAPTURE_SECURE_VIDEO_OUTPUT "
Michael Wrightc39d47a2014-07-08 18:07:36 -07002166 + "or an appropriate MediaProjection token to create a "
2167 + "secure virtual display.");
Jeff Brown4ccb8232014-01-16 22:16:42 -08002168 }
2169 }
2170
Chilun67a379b2019-04-11 19:49:42 +08002171 // Sometimes users can have sensitive information in system decoration windows. An app
2172 // could create a virtual display with system decorations support and read the user info
2173 // from the surface.
2174 // We should only allow adding flag VIRTUAL_DISPLAY_FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS
2175 // to virtual displays that are owned by the system.
2176 if (callingUid != Process.SYSTEM_UID
2177 && (flags & VIRTUAL_DISPLAY_FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS) != 0) {
2178 if (!checkCallingPermission(INTERNAL_SYSTEM_WINDOW, "createVirtualDisplay()")) {
2179 throw new SecurityException("Requires INTERNAL_SYSTEM_WINDOW permission");
2180 }
2181 }
2182
Jeff Brown4ccb8232014-01-16 22:16:42 -08002183 final long token = Binder.clearCallingIdentity();
2184 try {
Santos Cordonee8931e2017-04-05 10:31:15 -07002185 return createVirtualDisplayInternal(callback, projection, callingUid, packageName,
b0202.jung925435c2020-01-08 18:46:59 +09002186 surface, flags, virtualDisplayConfig);
Jeff Brown4ccb8232014-01-16 22:16:42 -08002187 } finally {
2188 Binder.restoreCallingIdentity(token);
2189 }
2190 }
2191
2192 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07002193 public void resizeVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wright01e840f2014-06-26 16:03:25 -07002194 int width, int height, int densityDpi) {
Jeff Chang01baf6c2019-08-20 16:21:25 +08002195 if (width <= 0 || height <= 0 || densityDpi <= 0) {
2196 throw new IllegalArgumentException("width, height, and densityDpi must be "
2197 + "greater than 0");
2198 }
Michael Wright01e840f2014-06-26 16:03:25 -07002199 final long token = Binder.clearCallingIdentity();
2200 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07002201 resizeVirtualDisplayInternal(callback.asBinder(), width, height, densityDpi);
Michael Wright01e840f2014-06-26 16:03:25 -07002202 } finally {
2203 Binder.restoreCallingIdentity(token);
2204 }
2205 }
2206
2207 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07002208 public void setVirtualDisplaySurface(IVirtualDisplayCallback callback, Surface surface) {
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07002209 if (surface != null && surface.isSingleBuffered()) {
2210 throw new IllegalArgumentException("Surface can't be single-buffered");
2211 }
Jeff Brown92207df2014-04-16 13:16:07 -07002212 final long token = Binder.clearCallingIdentity();
2213 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07002214 setVirtualDisplaySurfaceInternal(callback.asBinder(), surface);
Jeff Brown92207df2014-04-16 13:16:07 -07002215 } finally {
2216 Binder.restoreCallingIdentity(token);
2217 }
2218 }
2219
2220 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07002221 public void releaseVirtualDisplay(IVirtualDisplayCallback callback) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08002222 final long token = Binder.clearCallingIdentity();
2223 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07002224 releaseVirtualDisplayInternal(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -08002225 } finally {
2226 Binder.restoreCallingIdentity(token);
2227 }
2228 }
2229
2230 @Override // Binder call
chaviwda4c6942018-11-07 15:52:56 -08002231 public void setVirtualDisplayState(IVirtualDisplayCallback callback, boolean isOn) {
2232 final long token = Binder.clearCallingIdentity();
2233 try {
2234 setVirtualDisplayStateInternal(callback.asBinder(), isOn);
2235 } finally {
2236 Binder.restoreCallingIdentity(token);
2237 }
2238 }
2239
2240 @Override // Binder call
Jeff Brown4ccb8232014-01-16 22:16:42 -08002241 public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06002242 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
Jeff Brown4ccb8232014-01-16 22:16:42 -08002243
2244 final long token = Binder.clearCallingIdentity();
2245 try {
2246 dumpInternal(pw);
2247 } finally {
2248 Binder.restoreCallingIdentity(token);
2249 }
2250 }
2251
Kenny Guy22bd0442017-10-26 00:15:54 +01002252 @Override // Binder call
Kenny Guy29aa30e2017-11-30 13:43:46 +00002253 public ParceledListSlice<BrightnessChangeEvent> getBrightnessEvents(String callingPackage) {
Kenny Guy22bd0442017-10-26 00:15:54 +01002254 mContext.enforceCallingOrSelfPermission(
2255 Manifest.permission.BRIGHTNESS_SLIDER_USAGE,
2256 "Permission to read brightness events.");
Kenny Guy29aa30e2017-11-30 13:43:46 +00002257
2258 final int callingUid = Binder.getCallingUid();
2259 AppOpsManager appOpsManager = mContext.getSystemService(AppOpsManager.class);
Jeff Sharkeyd9311192018-04-16 16:50:34 +00002260 final int mode = appOpsManager.noteOp(AppOpsManager.OP_GET_USAGE_STATS,
Kenny Guy29aa30e2017-11-30 13:43:46 +00002261 callingUid, callingPackage);
2262 final boolean hasUsageStats;
2263 if (mode == AppOpsManager.MODE_DEFAULT) {
2264 // The default behavior here is to check if PackageManager has given the app
2265 // permission.
2266 hasUsageStats = mContext.checkCallingPermission(
2267 Manifest.permission.PACKAGE_USAGE_STATS)
2268 == PackageManager.PERMISSION_GRANTED;
2269 } else {
2270 hasUsageStats = mode == AppOpsManager.MODE_ALLOWED;
2271 }
2272
2273 final int userId = UserHandle.getUserId(callingUid);
Kenny Guy22bd0442017-10-26 00:15:54 +01002274 final long token = Binder.clearCallingIdentity();
2275 try {
Michael Wright144aac92017-12-21 18:37:41 +00002276 synchronized (mSyncRoot) {
2277 return mDisplayPowerController.getBrightnessEvents(userId, hasUsageStats);
2278 }
Kenny Guy22bd0442017-10-26 00:15:54 +01002279 } finally {
2280 Binder.restoreCallingIdentity(token);
2281 }
2282 }
2283
Michael Wrighteef0e132017-11-21 17:57:52 +00002284 @Override // Binder call
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002285 public ParceledListSlice<AmbientBrightnessDayStats> getAmbientBrightnessStats() {
2286 mContext.enforceCallingOrSelfPermission(
2287 Manifest.permission.ACCESS_AMBIENT_LIGHT_STATS,
2288 "Permission required to to access ambient light stats.");
2289 final int callingUid = Binder.getCallingUid();
2290 final int userId = UserHandle.getUserId(callingUid);
2291 final long token = Binder.clearCallingIdentity();
2292 try {
2293 synchronized (mSyncRoot) {
2294 return mDisplayPowerController.getAmbientBrightnessStats(userId);
2295 }
2296 } finally {
2297 Binder.restoreCallingIdentity(token);
2298 }
2299 }
2300
2301 @Override // Binder call
Michael Wrighteef0e132017-11-21 17:57:52 +00002302 public void setBrightnessConfigurationForUser(
Kenny Guy05ce8092018-01-17 13:44:20 +00002303 BrightnessConfiguration c, @UserIdInt int userId, String packageName) {
Michael Wrighteef0e132017-11-21 17:57:52 +00002304 mContext.enforceCallingOrSelfPermission(
2305 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
2306 "Permission required to change the display's brightness configuration");
2307 if (userId != UserHandle.getCallingUserId()) {
2308 mContext.enforceCallingOrSelfPermission(
2309 Manifest.permission.INTERACT_ACROSS_USERS,
2310 "Permission required to change the display brightness"
2311 + " configuration of another user");
2312 }
Kenny Guy05ce8092018-01-17 13:44:20 +00002313 if (packageName != null && !validatePackageName(getCallingUid(), packageName)) {
2314 packageName = null;
2315 }
Michael Wrighteef0e132017-11-21 17:57:52 +00002316 final long token = Binder.clearCallingIdentity();
2317 try {
Kenny Guy05ce8092018-01-17 13:44:20 +00002318 setBrightnessConfigurationForUserInternal(c, userId, packageName);
Michael Wrighteef0e132017-11-21 17:57:52 +00002319 } finally {
2320 Binder.restoreCallingIdentity(token);
2321 }
2322 }
2323
Michael Wrightd8460232018-01-16 18:04:59 +00002324 @Override // Binder call
Kenny Guy6d1009f2018-03-14 14:28:23 +00002325 public BrightnessConfiguration getBrightnessConfigurationForUser(int userId) {
2326 mContext.enforceCallingOrSelfPermission(
2327 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
2328 "Permission required to read the display's brightness configuration");
2329 if (userId != UserHandle.getCallingUserId()) {
2330 mContext.enforceCallingOrSelfPermission(
2331 Manifest.permission.INTERACT_ACROSS_USERS,
2332 "Permission required to read the display brightness"
2333 + " configuration of another user");
2334 }
2335 final long token = Binder.clearCallingIdentity();
2336 try {
2337 final int userSerial = getUserManager().getUserSerialNumber(userId);
2338 synchronized (mSyncRoot) {
2339 BrightnessConfiguration config =
2340 mPersistentDataStore.getBrightnessConfiguration(userSerial);
2341 if (config == null) {
2342 config = mDisplayPowerController.getDefaultBrightnessConfiguration();
2343 }
2344 return config;
2345 }
2346 } finally {
2347 Binder.restoreCallingIdentity(token);
2348 }
2349 }
2350
2351 @Override // Binder call
2352 public BrightnessConfiguration getDefaultBrightnessConfiguration() {
2353 mContext.enforceCallingOrSelfPermission(
2354 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
2355 "Permission required to read the display's default brightness configuration");
2356 final long token = Binder.clearCallingIdentity();
2357 try {
2358 synchronized (mSyncRoot) {
2359 return mDisplayPowerController.getDefaultBrightnessConfiguration();
2360 }
2361 } finally {
2362 Binder.restoreCallingIdentity(token);
2363 }
2364 }
2365
2366 @Override // Binder call
Galia Peycheva35f900a2020-01-09 14:31:05 +01002367 public boolean isMinimalPostProcessingRequested(int displayId) {
2368 synchronized (mSyncRoot) {
2369 return mLogicalDisplays.get(displayId).getRequestedMinimalPostProcessingLocked();
2370 }
2371 }
2372
2373 @Override // Binder call
Fiona Campbelld4eb2952019-11-04 17:19:56 +00002374 public void setTemporaryBrightness(float brightness) {
Michael Wrightd8460232018-01-16 18:04:59 +00002375 mContext.enforceCallingOrSelfPermission(
2376 Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS,
2377 "Permission required to set the display's brightness");
2378 final long token = Binder.clearCallingIdentity();
2379 try {
2380 synchronized (mSyncRoot) {
2381 mDisplayPowerController.setTemporaryBrightness(brightness);
2382 }
2383 } finally {
2384 Binder.restoreCallingIdentity(token);
2385 }
2386 }
2387
2388 @Override // Binder call
2389 public void setTemporaryAutoBrightnessAdjustment(float adjustment) {
2390 mContext.enforceCallingOrSelfPermission(
2391 Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS,
2392 "Permission required to set the display's auto brightness adjustment");
2393 final long token = Binder.clearCallingIdentity();
2394 try {
2395 synchronized (mSyncRoot) {
2396 mDisplayPowerController.setTemporaryAutoBrightnessAdjustment(adjustment);
2397 }
2398 } finally {
2399 Binder.restoreCallingIdentity(token);
2400 }
2401 }
2402
Dan Gittik7a32fba2018-03-28 12:19:38 +01002403 @Override // Binder call
2404 public void onShellCommand(FileDescriptor in, FileDescriptor out,
2405 FileDescriptor err, String[] args, ShellCallback callback,
2406 ResultReceiver resultReceiver) {
Santos Cordonc4fd8e62019-12-18 13:01:05 +00002407 new DisplayManagerShellCommand(DisplayManagerService.this).exec(this, in, out, err,
2408 args, callback, resultReceiver);
Dan Gittik7a32fba2018-03-28 12:19:38 +01002409 }
2410
Dan Gittik122df862018-03-28 16:59:22 +01002411 @Override // Binder call
2412 public Curve getMinimumBrightnessCurve() {
2413 final long token = Binder.clearCallingIdentity();
2414 try {
2415 return getMinimumBrightnessCurveInternal();
2416 } finally {
2417 Binder.restoreCallingIdentity(token);
2418 }
2419 }
2420
Peiyong Lin277eaff2019-01-16 16:18:22 -08002421 @Override // Binder call
2422 public int getPreferredWideGamutColorSpaceId() {
2423 final long token = Binder.clearCallingIdentity();
2424 try {
2425 return getPreferredWideGamutColorSpaceIdInternal();
2426 } finally {
2427 Binder.restoreCallingIdentity(token);
2428 }
2429 }
2430
Jeff Brown4ccb8232014-01-16 22:16:42 -08002431 private boolean validatePackageName(int uid, String packageName) {
2432 if (packageName != null) {
2433 String[] packageNames = mContext.getPackageManager().getPackagesForUid(uid);
2434 if (packageNames != null) {
2435 for (String n : packageNames) {
2436 if (n.equals(packageName)) {
2437 return true;
2438 }
2439 }
2440 }
2441 }
2442 return false;
2443 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07002444
2445 private boolean canProjectVideo(IMediaProjection projection) {
2446 if (projection != null) {
2447 try {
2448 if (projection.canProjectVideo()) {
2449 return true;
2450 }
2451 } catch (RemoteException e) {
2452 Slog.e(TAG, "Unable to query projection service for permissions", e);
2453 }
2454 }
Chilun67a379b2019-04-11 19:49:42 +08002455 if (checkCallingPermission(CAPTURE_VIDEO_OUTPUT, "canProjectVideo()")) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07002456 return true;
2457 }
2458 return canProjectSecureVideo(projection);
2459 }
2460
2461 private boolean canProjectSecureVideo(IMediaProjection projection) {
2462 if (projection != null) {
2463 try {
2464 if (projection.canProjectSecureVideo()){
2465 return true;
2466 }
2467 } catch (RemoteException e) {
2468 Slog.e(TAG, "Unable to query projection service for permissions", e);
2469 }
2470 }
Chilun67a379b2019-04-11 19:49:42 +08002471 return checkCallingPermission(CAPTURE_SECURE_VIDEO_OUTPUT, "canProjectSecureVideo()");
2472 }
2473
2474 private boolean checkCallingPermission(String permission, String func) {
2475 if (mContext.checkCallingPermission(permission) == PackageManager.PERMISSION_GRANTED) {
2476 return true;
2477 }
2478 final String msg = "Permission Denial: " + func + " from pid=" + Binder.getCallingPid()
2479 + ", uid=" + Binder.getCallingUid() + " requires " + permission;
2480 Slog.w(TAG, msg);
2481 return false;
Michael Wrightc39d47a2014-07-08 18:07:36 -07002482 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08002483 }
2484
2485 private final class LocalService extends DisplayManagerInternal {
Galia Peycheva056b3ee2019-06-26 14:05:12 +02002486
Jeff Brown4ccb8232014-01-16 22:16:42 -08002487 @Override
Jeff Brown037c33e2014-04-09 00:31:55 -07002488 public void initPowerManagement(final DisplayPowerCallbacks callbacks, Handler handler,
Jeff Brownad9ef192014-04-08 17:26:30 -07002489 SensorManager sensorManager) {
2490 synchronized (mSyncRoot) {
Jeff Brown037c33e2014-04-09 00:31:55 -07002491 DisplayBlanker blanker = new DisplayBlanker() {
2492 @Override
Fiona Campbelld4eb2952019-11-04 17:19:56 +00002493 public void requestDisplayState(int state, float brightness) {
Jeff Brown037c33e2014-04-09 00:31:55 -07002494 // The order of operations is important for legacy reasons.
2495 if (state == Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07002496 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07002497 }
2498
2499 callbacks.onDisplayStateChange(state);
2500
2501 if (state != Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07002502 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07002503 }
2504 }
2505 };
Jeff Brownad9ef192014-04-08 17:26:30 -07002506 mDisplayPowerController = new DisplayPowerController(
Fiona Campbell172fd4a2020-03-13 16:34:30 +00002507 mContext, callbacks, handler, sensorManager, blanker,
2508 mDisplayDevices.get(Display.DEFAULT_DISPLAY));
Long Lingbc841b02019-07-03 16:43:15 -07002509 mSensorManager = sensorManager;
Jeff Brownad9ef192014-04-08 17:26:30 -07002510 }
Michael Wrighteef0e132017-11-21 17:57:52 +00002511
2512 mHandler.sendEmptyMessage(MSG_LOAD_BRIGHTNESS_CONFIGURATION);
Jeff Brownad9ef192014-04-08 17:26:30 -07002513 }
2514
2515 @Override
2516 public boolean requestPowerState(DisplayPowerRequest request,
2517 boolean waitForNegativeProximity) {
Michael Wrighteef0e132017-11-21 17:57:52 +00002518 synchronized (mSyncRoot) {
2519 return mDisplayPowerController.requestPowerState(request,
2520 waitForNegativeProximity);
2521 }
Jeff Brownad9ef192014-04-08 17:26:30 -07002522 }
2523
2524 @Override
2525 public boolean isProximitySensorAvailable() {
Michael Wrighteef0e132017-11-21 17:57:52 +00002526 synchronized (mSyncRoot) {
2527 return mDisplayPowerController.isProximitySensorAvailable();
2528 }
Jeff Brownad9ef192014-04-08 17:26:30 -07002529 }
2530
2531 @Override
Jacky Kao333de4e2020-04-07 15:01:41 +08002532 public SurfaceControl.ScreenshotGraphicBuffer systemScreenshot(int displayId) {
2533 return systemScreenshotInternal(displayId);
Jacky Kao316477c2020-03-23 16:30:04 +08002534 }
2535
2536 @Override
Jacky Kao333de4e2020-04-07 15:01:41 +08002537 public SurfaceControl.ScreenshotGraphicBuffer userScreenshot(int displayId) {
2538 return userScreenshotInternal(displayId);
Riddle Hsu654a6f92018-07-13 22:59:36 +08002539 }
2540
2541 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08002542 public DisplayInfo getDisplayInfo(int displayId) {
2543 return getDisplayInfoInternal(displayId, Process.myUid());
2544 }
2545
2546 @Override
chaviw945f7302020-02-24 14:35:33 -08002547 public Point getDisplayPosition(int displayId) {
2548 synchronized (mSyncRoot) {
2549 LogicalDisplay display = mLogicalDisplays.get(displayId);
2550 if (display != null) {
2551 return display.getDisplayPosition();
2552 }
2553 return null;
2554 }
2555 }
2556
2557 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08002558 public void registerDisplayTransactionListener(DisplayTransactionListener listener) {
2559 if (listener == null) {
2560 throw new IllegalArgumentException("listener must not be null");
2561 }
2562
2563 registerDisplayTransactionListenerInternal(listener);
2564 }
2565
2566 @Override
2567 public void unregisterDisplayTransactionListener(DisplayTransactionListener listener) {
2568 if (listener == null) {
2569 throw new IllegalArgumentException("listener must not be null");
2570 }
2571
2572 unregisterDisplayTransactionListenerInternal(listener);
2573 }
2574
2575 @Override
2576 public void setDisplayInfoOverrideFromWindowManager(int displayId, DisplayInfo info) {
2577 setDisplayInfoOverrideFromWindowManagerInternal(displayId, info);
2578 }
2579
2580 @Override
Andrii Kuliancd097992017-03-23 18:31:59 -07002581 public void getNonOverrideDisplayInfo(int displayId, DisplayInfo outInfo) {
2582 getNonOverrideDisplayInfoInternal(displayId, outInfo);
2583 }
2584
2585 @Override
Robert Carrae606b42018-02-15 15:36:23 -08002586 public void performTraversal(SurfaceControl.Transaction t) {
2587 performTraversalInternal(t);
Jeff Brown4ccb8232014-01-16 22:16:42 -08002588 }
2589
2590 @Override
Michael Wright3f145a22014-07-22 19:46:03 -07002591 public void setDisplayProperties(int displayId, boolean hasContent,
Galia Peycheva056b3ee2019-06-26 14:05:12 +02002592 float requestedRefreshRate, int requestedMode,
2593 boolean requestedMinimalPostProcessing, boolean inTraversal) {
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07002594 setDisplayPropertiesInternal(displayId, hasContent, requestedRefreshRate,
Galia Peycheva056b3ee2019-06-26 14:05:12 +02002595 requestedMode, requestedMinimalPostProcessing, inTraversal);
Jeff Brown4ccb8232014-01-16 22:16:42 -08002596 }
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08002597
2598 @Override
2599 public void setDisplayOffsets(int displayId, int x, int y) {
2600 setDisplayOffsetsInternal(displayId, x, y);
2601 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002602
2603 @Override
Sam Lin4c3ac2b2019-02-18 04:50:26 -08002604 public void setDisplayScalingDisabled(int displayId, boolean disableScaling) {
2605 setDisplayScalingDisabledInternal(displayId, disableScaling);
2606 }
2607
2608 @Override
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002609 public void setDisplayAccessUIDs(SparseArray<IntArray> newDisplayAccessUIDs) {
2610 setDisplayAccessUIDsInternal(newDisplayAccessUIDs);
2611 }
2612
2613 @Override
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002614 public void persistBrightnessTrackerState() {
Michael Wright144aac92017-12-21 18:37:41 +00002615 synchronized (mSyncRoot) {
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002616 mDisplayPowerController.persistBrightnessTrackerState();
Michael Wright144aac92017-12-21 18:37:41 +00002617 }
Kenny Guycfe7b702017-11-14 21:04:58 +00002618 }
Adrian Roose1d68cd2018-01-17 12:54:50 +01002619
2620 @Override
2621 public void onOverlayChanged() {
2622 synchronized (mSyncRoot) {
Adrian Roos898ec382018-01-17 12:54:50 +01002623 for (int i = 0; i < mDisplayDevices.size(); i++) {
2624 mDisplayDevices.get(i).onOverlayChangedLocked();
Adrian Roose1d68cd2018-01-17 12:54:50 +01002625 }
2626 }
2627 }
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07002628
2629 @Override
2630 public DisplayedContentSamplingAttributes getDisplayedContentSamplingAttributes(
2631 int displayId) {
2632 return getDisplayedContentSamplingAttributesInternal(displayId);
2633 }
2634
2635 @Override
2636 public boolean setDisplayedContentSamplingEnabled(
2637 int displayId, boolean enable, int componentMask, int maxFrames) {
2638 return setDisplayedContentSamplingEnabledInternal(
2639 displayId, enable, componentMask, maxFrames);
2640 }
2641
2642 @Override
2643 public DisplayedContentSample getDisplayedContentSample(int displayId,
2644 long maxFrames, long timestamp) {
2645 return getDisplayedContentSampleInternal(displayId, maxFrames, timestamp);
2646 }
2647
Jeff Brown4ccb8232014-01-16 22:16:42 -08002648 }
Michael Wrighta3dab232019-02-22 16:54:21 +00002649
Ana Kruleca74a8642019-11-14 00:51:00 +01002650 class DesiredDisplayModeSpecsObserver
2651 implements DisplayModeDirector.DesiredDisplayModeSpecsListener {
2652 public void onDesiredDisplayModeSpecsChanged() {
2653 onDesiredDisplayModeSpecsChangedInternal();
Michael Wrighta3dab232019-02-22 16:54:21 +00002654 }
2655 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07002656}