blob: 6d0137551729b542961f11d29e6767bd18a8f7cc [file] [log] [blame]
Jeff Brownfa25bf52012-07-23 19:26:30 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.display;
18
Chilun67a379b2019-04-11 19:49:42 +080019import static android.Manifest.permission.CAPTURE_SECURE_VIDEO_OUTPUT;
20import static android.Manifest.permission.CAPTURE_VIDEO_OUTPUT;
21import static android.Manifest.permission.INTERNAL_SYSTEM_WINDOW;
Andrii Kulianfc8f82b2017-01-26 13:17:27 -080022import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
Chilun67a379b2019-04-11 19:49:42 +080023import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD;
Andrii Kulianfc8f82b2017-01-26 13:17:27 -080024import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY;
25import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;
26import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_SECURE;
Chilun67a379b2019-04-11 19:49:42 +080027import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +010028import static android.hardware.display.DisplayViewport.VIEWPORT_EXTERNAL;
29import static android.hardware.display.DisplayViewport.VIEWPORT_INTERNAL;
30import static android.hardware.display.DisplayViewport.VIEWPORT_VIRTUAL;
Jeff Brownbd6e1502012-08-28 03:27:37 -070031
Jeff Brownfa25bf52012-07-23 19:26:30 -070032import android.Manifest;
Santos Cordonee8931e2017-04-05 10:31:15 -070033import android.annotation.NonNull;
Kenny Guy05ce8092018-01-17 13:44:20 +000034import android.annotation.Nullable;
Michael Wrighteef0e132017-11-21 17:57:52 +000035import android.annotation.UserIdInt;
Kenny Guy29aa30e2017-11-30 13:43:46 +000036import android.app.AppOpsManager;
Jeff Brownfa25bf52012-07-23 19:26:30 -070037import android.content.Context;
38import android.content.pm.PackageManager;
Kenny Guy22bd0442017-10-26 00:15:54 +010039import android.content.pm.ParceledListSlice;
Michael Wrighteedcbf12017-08-16 23:14:54 +010040import android.content.res.Resources;
Dan Gittik122df862018-03-28 16:59:22 +010041import android.content.res.TypedArray;
Peiyong Lin277eaff2019-01-16 16:18:22 -080042import android.graphics.ColorSpace;
Michael Wrighteedcbf12017-08-16 23:14:54 +010043import android.graphics.Point;
Robert Carr5c52b132019-02-15 15:48:11 -080044import android.graphics.Rect;
Jeff Brownad9ef192014-04-08 17:26:30 -070045import android.hardware.SensorManager;
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +000046import android.hardware.display.AmbientBrightnessDayStats;
Kenny Guy22bd0442017-10-26 00:15:54 +010047import android.hardware.display.BrightnessChangeEvent;
Michael Wrighteef0e132017-11-21 17:57:52 +000048import android.hardware.display.BrightnessConfiguration;
Dan Gittik122df862018-03-28 16:59:22 +010049import android.hardware.display.Curve;
Jeff Brownbd6e1502012-08-28 03:27:37 -070050import android.hardware.display.DisplayManagerGlobal;
Jeff Brown4ccb8232014-01-16 22:16:42 -080051import android.hardware.display.DisplayManagerInternal;
Jeff Brown4ccb8232014-01-16 22:16:42 -080052import android.hardware.display.DisplayManagerInternal.DisplayTransactionListener;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +010053import android.hardware.display.DisplayViewport;
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -070054import android.hardware.display.DisplayedContentSample;
55import android.hardware.display.DisplayedContentSamplingAttributes;
Jeff Brownfa25bf52012-07-23 19:26:30 -070056import android.hardware.display.IDisplayManager;
Jeff Brownbd6e1502012-08-28 03:27:37 -070057import android.hardware.display.IDisplayManagerCallback;
Michael Wright75ee9fc2014-09-01 19:55:22 -070058import android.hardware.display.IVirtualDisplayCallback;
Jeff Browne08ae382012-09-07 20:36:36 -070059import android.hardware.display.WifiDisplayStatus;
Jeff Brown4ccb8232014-01-16 22:16:42 -080060import android.hardware.input.InputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -070061import android.media.projection.IMediaProjection;
62import android.media.projection.IMediaProjectionManager;
Jeff Brownfa25bf52012-07-23 19:26:30 -070063import android.os.Binder;
Jeff Brownbd6e1502012-08-28 03:27:37 -070064import android.os.Handler;
Jeff Brown64a55af2012-08-26 02:47:39 -070065import android.os.IBinder;
Jeff Brown4ccb8232014-01-16 22:16:42 -080066import android.os.IBinder.DeathRecipient;
Jeff Brownbd6e1502012-08-28 03:27:37 -070067import android.os.Looper;
68import android.os.Message;
Jeff Brown5d6443b2015-04-10 20:15:01 -070069import android.os.PowerManager;
Craig Mautner4504de52013-12-20 09:06:56 -080070import android.os.Process;
Jeff Brownbd6e1502012-08-28 03:27:37 -070071import android.os.RemoteException;
Dan Gittik7a32fba2018-03-28 12:19:38 +010072import android.os.ResultReceiver;
Michael Wrightc39d47a2014-07-08 18:07:36 -070073import android.os.ServiceManager;
Dan Gittik7a32fba2018-03-28 12:19:38 +010074import android.os.ShellCallback;
Jeff Brownbd6e1502012-08-28 03:27:37 -070075import android.os.SystemClock;
Jeff Brownfa25bf52012-07-23 19:26:30 -070076import android.os.SystemProperties;
Jeff Brown5d6443b2015-04-10 20:15:01 -070077import android.os.Trace;
Kenny Guy22bd0442017-10-26 00:15:54 +010078import android.os.UserHandle;
Michael Wrighteef0e132017-11-21 17:57:52 +000079import android.os.UserManager;
Dan Gittik7a32fba2018-03-28 12:19:38 +010080import android.provider.Settings;
Jeff Browna506a6e2013-06-04 00:02:38 -070081import android.text.TextUtils;
Andrii Kulianfb1bf692017-01-17 11:17:34 -080082import android.util.IntArray;
Dan Gittik122df862018-03-28 16:59:22 +010083import android.util.Pair;
Jeff Brownbd6e1502012-08-28 03:27:37 -070084import android.util.Slog;
85import android.util.SparseArray;
Dan Gittik122df862018-03-28 16:59:22 +010086import android.util.Spline;
Jeff Brownfa25bf52012-07-23 19:26:30 -070087import android.view.Display;
88import android.view.DisplayInfo;
Jeff Browna506a6e2013-06-04 00:02:38 -070089import android.view.Surface;
Robert Carrae606b42018-02-15 15:36:23 -080090import android.view.SurfaceControl;
Jeff Browna506a6e2013-06-04 00:02:38 -070091
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +010092import com.android.internal.annotations.GuardedBy;
93import com.android.internal.annotations.VisibleForTesting;
94import com.android.internal.util.DumpUtils;
95import com.android.internal.util.IndentingPrintWriter;
Jorim Jaggied7993b2017-03-28 18:50:01 +010096import com.android.server.AnimationThread;
Jeff Brown4ccb8232014-01-16 22:16:42 -080097import com.android.server.DisplayThread;
98import com.android.server.LocalServices;
99import com.android.server.SystemService;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700100import com.android.server.UiThread;
Jorim Jaggi21c39a72017-10-20 15:47:51 +0200101import com.android.server.wm.SurfaceAnimationThread;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100102import com.android.server.wm.WindowManagerInternal;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700103
104import java.io.FileDescriptor;
105import java.io.PrintWriter;
106import java.util.ArrayList;
Jeff Browna506a6e2013-06-04 00:02:38 -0700107import java.util.Arrays;
Jeff Browne75926d2014-09-18 15:24:49 -0700108import java.util.List;
Jeff Brown7f3994e2012-12-04 14:04:28 -0800109import java.util.concurrent.CopyOnWriteArrayList;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700110
111/**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700112 * Manages attached displays.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700113 * <p>
Jeff Brownbd6e1502012-08-28 03:27:37 -0700114 * The {@link DisplayManagerService} manages the global lifecycle of displays,
115 * decides how to configure logical displays based on the physical display devices currently
116 * attached, sends notifications to the system and to applications when the state
117 * changes, and so on.
118 * </p><p>
119 * The display manager service relies on a collection of {@link DisplayAdapter} components,
120 * for discovering and configuring physical display devices attached to the system.
121 * There are separate display adapters for each manner that devices are attached:
122 * one display adapter for built-in local displays, one for simulated non-functional
123 * displays when the system is headless, one for simulated overlay displays used for
124 * development, one for wifi displays, etc.
125 * </p><p>
126 * Display adapters are only weakly coupled to the display manager service.
127 * Display adapters communicate changes in display device state to the display manager
Craig Mautner722285e2012-09-07 13:55:58 -0700128 * service asynchronously via a {@link DisplayAdapter.Listener} registered
Jeff Brownbd6e1502012-08-28 03:27:37 -0700129 * by the display manager service. This separation of concerns is important for
130 * two main reasons. First, it neatly encapsulates the responsibilities of these
131 * two classes: display adapters handle individual display devices whereas
132 * the display manager service handles the global state. Second, it eliminates
133 * the potential for deadlocks resulting from asynchronous display device discovery.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700134 * </p>
135 *
136 * <h3>Synchronization</h3>
137 * <p>
138 * Because the display manager may be accessed by multiple threads, the synchronization
139 * story gets a little complicated. In particular, the window manager may call into
140 * the display manager while holding a surface transaction with the expectation that
141 * it can apply changes immediately. Unfortunately, that means we can't just do
142 * everything asynchronously (*grump*).
Jeff Brownbd6e1502012-08-28 03:27:37 -0700143 * </p><p>
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700144 * To make this work, all of the objects that belong to the display manager must
145 * use the same lock. We call this lock the synchronization root and it has a unique
146 * type {@link DisplayManagerService.SyncRoot}. Methods that require this lock are
147 * named with the "Locked" suffix.
148 * </p><p>
149 * Where things get tricky is that the display manager is not allowed to make
150 * any potentially reentrant calls, especially into the window manager. We generally
151 * avoid this by making all potentially reentrant out-calls asynchronous.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700152 * </p>
153 */
Jeff Brown4ccb8232014-01-16 22:16:42 -0800154public final class DisplayManagerService extends SystemService {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700155 private static final String TAG = "DisplayManagerService";
Jeff Brownbd6e1502012-08-28 03:27:37 -0700156 private static final boolean DEBUG = false;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700157
Jeff Brownbbd28a22012-09-20 16:47:15 -0700158 // When this system property is set to 0, WFD is forcibly disabled on boot.
159 // When this system property is set to 1, WFD is forcibly enabled on boot.
160 // Otherwise WFD is enabled according to the value of config_enableWifiDisplay.
161 private static final String FORCE_WIFI_DISPLAY_ENABLE = "persist.debug.wfd.enable";
162
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200163 private static final String PROP_DEFAULT_DISPLAY_TOP_INSET = "persist.sys.displayinset.top";
164
Jeff Brownbd6e1502012-08-28 03:27:37 -0700165 private static final long WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT = 10000;
166
Santos Cordonc22c5632017-06-21 16:03:49 -0700167 private static final int MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS = 1;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700168 private static final int MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS = 2;
169 private static final int MSG_DELIVER_DISPLAY_EVENT = 3;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700170 private static final int MSG_REQUEST_TRAVERSAL = 4;
Jeff Brownd728bf52012-09-08 18:05:28 -0700171 private static final int MSG_UPDATE_VIEWPORT = 5;
Michael Wrighta3dab232019-02-22 16:54:21 +0000172 private static final int MSG_LOAD_BRIGHTNESS_CONFIGURATION = 6;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700173
Jeff Brownb880d882014-02-10 19:47:07 -0800174 private final Context mContext;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700175 private final DisplayManagerHandler mHandler;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700176 private final Handler mUiHandler;
177 private final DisplayAdapterListener mDisplayAdapterListener;
Michael Wrighta3dab232019-02-22 16:54:21 +0000178 private final DisplayModeDirector mDisplayModeDirector;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800179 private WindowManagerInternal mWindowManagerInternal;
180 private InputManagerInternal mInputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -0700181 private IMediaProjectionManager mProjectionService;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700182
183 // The synchronization root for the display manager.
184 // This lock guards most of the display manager's state.
Craig Mautner0bf6ec92012-12-18 08:33:27 -0800185 // NOTE: This is synchronized on while holding WindowManagerService.mWindowMap so never call
186 // into WindowManagerService methods that require mWindowMap while holding this unless you are
187 // very very sure that no deadlock can occur.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700188 private final SyncRoot mSyncRoot = new SyncRoot();
189
190 // True if in safe mode.
191 // This option may disable certain display adapters.
192 public boolean mSafeMode;
193
194 // True if we are in a special boot mode where only core applications and
195 // services should be started. This option may disable certain display adapters.
196 public boolean mOnlyCore;
197
Jeff Brown27f1d672012-10-17 18:32:34 -0700198 // True if the display manager service should pretend there is only one display
199 // and only tell applications about the existence of the default logical display.
200 // The display manager can still mirror content to secondary displays but applications
201 // cannot present unique content on those displays.
202 // Used for demonstration purposes only.
203 private final boolean mSingleDisplayDemoMode;
204
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700205 // All callback records indexed by calling process id.
206 public final SparseArray<CallbackRecord> mCallbacks =
Jeff Brownbd6e1502012-08-28 03:27:37 -0700207 new SparseArray<CallbackRecord>();
Jeff Brownfa25bf52012-07-23 19:26:30 -0700208
Jeff Brownbd6e1502012-08-28 03:27:37 -0700209 // List of all currently registered display adapters.
210 private final ArrayList<DisplayAdapter> mDisplayAdapters = new ArrayList<DisplayAdapter>();
211
212 // List of all currently connected display devices.
213 private final ArrayList<DisplayDevice> mDisplayDevices = new ArrayList<DisplayDevice>();
214
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700215 // List of all logical displays indexed by logical display id.
216 private final SparseArray<LogicalDisplay> mLogicalDisplays =
217 new SparseArray<LogicalDisplay>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700218 private int mNextNonDefaultDisplayId = Display.DEFAULT_DISPLAY + 1;
219
Jeff Brown7f3994e2012-12-04 14:04:28 -0800220 // List of all display transaction listeners.
221 private final CopyOnWriteArrayList<DisplayTransactionListener> mDisplayTransactionListeners =
222 new CopyOnWriteArrayList<DisplayTransactionListener>();
223
Jeff Brownad9ef192014-04-08 17:26:30 -0700224 // Display power controller.
225 private DisplayPowerController mDisplayPowerController;
226
Jeff Brown037c33e2014-04-09 00:31:55 -0700227 // The overall display state, independent of changes that might influence one
228 // display or another in particular.
Jeff Brown5d6443b2015-04-10 20:15:01 -0700229 private int mGlobalDisplayState = Display.STATE_ON;
230
231 // The overall display brightness.
232 // For now, this only applies to the built-in display but we may split it up eventually.
233 private int mGlobalDisplayBrightness = PowerManager.BRIGHTNESS_DEFAULT;
Jeff Brown9e316a12012-10-08 19:17:06 -0700234
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700235 // Set to true when there are pending display changes that have yet to be applied
236 // to the surface flinger state.
237 private boolean mPendingTraversal;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700238
Jeff Browne08ae382012-09-07 20:36:36 -0700239 // The Wifi display adapter, or null if not registered.
240 private WifiDisplayAdapter mWifiDisplayAdapter;
241
Jeff Brownce468a32013-11-21 16:42:03 -0800242 // The number of active wifi display scan requests.
243 private int mWifiDisplayScanRequestCount;
244
Jeff Browna506a6e2013-06-04 00:02:38 -0700245 // The virtual display adapter, or null if not registered.
246 private VirtualDisplayAdapter mVirtualDisplayAdapter;
247
Michael Wrighteef0e132017-11-21 17:57:52 +0000248 // The User ID of the current user
249 private @UserIdInt int mCurrentUserId;
250
Michael Wrighteedcbf12017-08-16 23:14:54 +0100251 // The stable device screen height and width. These are not tied to a specific display, even
252 // the default display, because they need to be stable over the course of the device's entire
253 // life, even if the default display changes (e.g. a new monitor is plugged into a PC-like
254 // device).
255 private Point mStableDisplaySize = new Point();
256
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200257 // The top inset of the default display.
258 // This gets persisted so that the boot animation knows how to transition from the display's
259 // full size to the size configured by the user. Right now we only persist and animate the top
260 // inset, but theoretically we could do it for all of them.
261 private int mDefaultDisplayTopInset;
262
Jeff Brownd728bf52012-09-08 18:05:28 -0700263 // Viewports of the default display and the display that should receive touch
264 // input from an external source. Used by the input system.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100265 @GuardedBy("mSyncRoot")
266 private final ArrayList<DisplayViewport> mViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700267
Jeff Brown89d55462012-09-19 11:33:42 -0700268 // Persistent data store for all internal settings maintained by the display manager service.
269 private final PersistentDataStore mPersistentDataStore = new PersistentDataStore();
270
Jeff Brownbd6e1502012-08-28 03:27:37 -0700271 // Temporary callback list, used when sending display events to applications.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700272 // May be used outside of the lock but only on the handler thread.
273 private final ArrayList<CallbackRecord> mTempCallbacks = new ArrayList<CallbackRecord>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700274
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700275 // Temporary display info, used for comparing display configurations.
276 private final DisplayInfo mTempDisplayInfo = new DisplayInfo();
277
Jeff Brownd728bf52012-09-08 18:05:28 -0700278 // Temporary viewports, used when sending new viewport information to the
279 // input system. May be used outside of the lock but only on the handler thread.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100280 private final ArrayList<DisplayViewport> mTempViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700281
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700282 // The default color mode for default displays. Overrides the usual
283 // Display.Display.COLOR_MODE_DEFAULT for displays with the
284 // DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY flag set.
285 private final int mDefaultDisplayDefaultColorMode;
286
Jeff Browne75926d2014-09-18 15:24:49 -0700287 // Temporary list of deferred work to perform when setting the display state.
288 // Only used by requestDisplayState. The field is self-synchronized and only
289 // intended for use inside of the requestGlobalDisplayStateInternal function.
290 private final ArrayList<Runnable> mTempDisplayStateWorkQueue = new ArrayList<Runnable>();
291
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800292 // Lists of UIDs that are present on the displays. Maps displayId -> array of UIDs.
293 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
294
Santos Cordonee8931e2017-04-05 10:31:15 -0700295 private final Injector mInjector;
296
Dan Gittik122df862018-03-28 16:59:22 +0100297 // The minimum brightness curve, which guarantess that any brightness curve that dips below it
298 // is rejected by the system.
299 private final Curve mMinimumBrightnessCurve;
300 private final Spline mMinimumBrightnessSpline;
Peiyong Lin277eaff2019-01-16 16:18:22 -0800301 private final ColorSpace mWideColorSpace;
Dan Gittik122df862018-03-28 16:59:22 +0100302
Jeff Brownb880d882014-02-10 19:47:07 -0800303 public DisplayManagerService(Context context) {
Santos Cordonee8931e2017-04-05 10:31:15 -0700304 this(context, new Injector());
305 }
306
307 @VisibleForTesting
308 DisplayManagerService(Context context, Injector injector) {
Jeff Brownb880d882014-02-10 19:47:07 -0800309 super(context);
Santos Cordonee8931e2017-04-05 10:31:15 -0700310 mInjector = injector;
Jeff Brownb880d882014-02-10 19:47:07 -0800311 mContext = context;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800312 mHandler = new DisplayManagerHandler(DisplayThread.get().getLooper());
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700313 mUiHandler = UiThread.getHandler();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700314 mDisplayAdapterListener = new DisplayAdapterListener();
Michael Wrighta3dab232019-02-22 16:54:21 +0000315 mDisplayModeDirector = new DisplayModeDirector(context, mHandler);
Jeff Brown27f1d672012-10-17 18:32:34 -0700316 mSingleDisplayDemoMode = SystemProperties.getBoolean("persist.demo.singledisplay", false);
Dan Gittik122df862018-03-28 16:59:22 +0100317 Resources resources = mContext.getResources();
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700318 mDefaultDisplayDefaultColorMode = mContext.getResources().getInteger(
Michael Wrighteef0e132017-11-21 17:57:52 +0000319 com.android.internal.R.integer.config_defaultDisplayDefaultColorMode);
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200320 mDefaultDisplayTopInset = SystemProperties.getInt(PROP_DEFAULT_DISPLAY_TOP_INSET, -1);
Dan Gittik122df862018-03-28 16:59:22 +0100321 float[] lux = getFloatArray(resources.obtainTypedArray(
322 com.android.internal.R.array.config_minimumBrightnessCurveLux));
323 float[] nits = getFloatArray(resources.obtainTypedArray(
324 com.android.internal.R.array.config_minimumBrightnessCurveNits));
325 mMinimumBrightnessCurve = new Curve(lux, nits);
326 mMinimumBrightnessSpline = Spline.createSpline(lux, nits);
Jeff Brown5d6443b2015-04-10 20:15:01 -0700327
Michael Wrighta3dab232019-02-22 16:54:21 +0000328 PowerManager pm = mContext.getSystemService(PowerManager.class);
Jeff Brown5d6443b2015-04-10 20:15:01 -0700329 mGlobalDisplayBrightness = pm.getDefaultScreenBrightnessSetting();
Michael Wrighteef0e132017-11-21 17:57:52 +0000330 mCurrentUserId = UserHandle.USER_SYSTEM;
Peiyong Lin277eaff2019-01-16 16:18:22 -0800331 ColorSpace[] colorSpaces = SurfaceControl.getCompositionColorSpaces();
332 mWideColorSpace = colorSpaces[1];
Joel Fernandes2d314e12017-04-04 16:32:15 -0700333 }
334
335 public void setupSchedulerPolicies() {
Jorim Jaggied7993b2017-03-28 18:50:01 +0100336 // android.display and android.anim is critical to user experience and we should make sure
Michael Wrighteef0e132017-11-21 17:57:52 +0000337 // it is not in the default foregroup groups, add it to top-app to make sure it uses all
338 // the cores and scheduling settings for top-app when it runs.
Jorim Jaggied7993b2017-03-28 18:50:01 +0100339 Process.setThreadGroupAndCpuset(DisplayThread.get().getThreadId(),
340 Process.THREAD_GROUP_TOP_APP);
341 Process.setThreadGroupAndCpuset(AnimationThread.get().getThreadId(),
342 Process.THREAD_GROUP_TOP_APP);
Jorim Jaggi21c39a72017-10-20 15:47:51 +0200343 Process.setThreadGroupAndCpuset(SurfaceAnimationThread.get().getThreadId(),
344 Process.THREAD_GROUP_TOP_APP);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700345 }
346
Jeff Brown4ccb8232014-01-16 22:16:42 -0800347 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -0800348 public void onStart() {
Michael Wright1c9977b2016-07-12 13:30:10 -0700349 // We need to pre-load the persistent data store so it's ready before the default display
350 // adapter is up so that we have it's configuration. We could load it lazily, but since
351 // 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 -0700352 // we've waited for the display to register itself with us.
Michael Wrighta3dab232019-02-22 16:54:21 +0000353 synchronized (mSyncRoot) {
354 mPersistentDataStore.loadIfNeeded();
355 loadStableDisplayValuesLocked();
Michael Wrighteedcbf12017-08-16 23:14:54 +0100356 }
Santos Cordonc22c5632017-06-21 16:03:49 -0700357 mHandler.sendEmptyMessage(MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800358
359 publishBinderService(Context.DISPLAY_SERVICE, new BinderService(),
360 true /*allowIsolated*/);
361 publishLocalService(DisplayManagerInternal.class, new LocalService());
362 }
363
364 @Override
365 public void onBootPhase(int phase) {
366 if (phase == PHASE_WAIT_FOR_DEFAULT_DISPLAY) {
367 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700368 long timeout = SystemClock.uptimeMillis()
369 + mInjector.getDefaultDisplayDelayTimeout();
370 while (mLogicalDisplays.get(Display.DEFAULT_DISPLAY) == null ||
371 mVirtualDisplayAdapter == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800372 long delay = timeout - SystemClock.uptimeMillis();
373 if (delay <= 0) {
374 throw new RuntimeException("Timeout waiting for default display "
Santos Cordonc22c5632017-06-21 16:03:49 -0700375 + "to be initialized. DefaultDisplay="
376 + mLogicalDisplays.get(Display.DEFAULT_DISPLAY)
377 + ", mVirtualDisplayAdapter=" + mVirtualDisplayAdapter);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800378 }
379 if (DEBUG) {
380 Slog.d(TAG, "waitForDefaultDisplay: waiting, timeout=" + delay);
381 }
382 try {
383 mSyncRoot.wait(delay);
384 } catch (InterruptedException ex) {
385 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700386 }
387 }
388 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700389 }
390
Michael Wrighteef0e132017-11-21 17:57:52 +0000391 @Override
392 public void onSwitchUser(@UserIdInt int newUserId) {
393 final int userSerial = getUserManager().getUserSerialNumber(newUserId);
394 synchronized (mSyncRoot) {
395 if (mCurrentUserId != newUserId) {
396 mCurrentUserId = newUserId;
397 BrightnessConfiguration config =
398 mPersistentDataStore.getBrightnessConfiguration(userSerial);
399 mDisplayPowerController.setBrightnessConfiguration(config);
400 }
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +0000401 mDisplayPowerController.onSwitchUser(newUserId);
Michael Wrighteef0e132017-11-21 17:57:52 +0000402 }
403 }
404
Jeff Brown4ccb8232014-01-16 22:16:42 -0800405 // TODO: Use dependencies or a boot phase
406 public void windowManagerAndInputReady() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700407 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800408 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
409 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner65d11b32012-10-01 13:59:52 -0700410 scheduleTraversalLocked(false);
Jeff Brownd728bf52012-09-08 18:05:28 -0700411 }
412 }
413
414 /**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700415 * Called when the system is ready to go.
416 */
417 public void systemReady(boolean safeMode, boolean onlyCore) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700418 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700419 mSafeMode = safeMode;
420 mOnlyCore = onlyCore;
421 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700422
Michael Wrighta3dab232019-02-22 16:54:21 +0000423 mDisplayModeDirector.setListener(new AllowedDisplayModeObserver());
424 mDisplayModeDirector.start();
425
Jeff Brownbd6e1502012-08-28 03:27:37 -0700426 mHandler.sendEmptyMessage(MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS);
427 }
428
Santos Cordonee8931e2017-04-05 10:31:15 -0700429 @VisibleForTesting
430 Handler getDisplayHandler() {
431 return mHandler;
432 }
433
Michael Wrighteedcbf12017-08-16 23:14:54 +0100434 private void loadStableDisplayValuesLocked() {
435 final Point size = mPersistentDataStore.getStableDisplaySize();
436 if (size.x > 0 && size.y > 0) {
437 // Just set these values directly so we don't write the display persistent data again
438 // unnecessarily
439 mStableDisplaySize.set(size.x, size.y);
440 } else {
441 final Resources res = mContext.getResources();
442 final int width = res.getInteger(
443 com.android.internal.R.integer.config_stableDeviceDisplayWidth);
444 final int height = res.getInteger(
445 com.android.internal.R.integer.config_stableDeviceDisplayHeight);
446 if (width > 0 && height > 0) {
447 setStableDisplaySizeLocked(width, height);
448 }
449 }
450 }
451
452 private Point getStableDisplaySizeInternal() {
453 Point r = new Point();
454 synchronized (mSyncRoot) {
455 if (mStableDisplaySize.x > 0 && mStableDisplaySize.y > 0) {
456 r.set(mStableDisplaySize.x, mStableDisplaySize.y);
457 }
458 }
459 return r;
460 }
461
Jeff Brown4ccb8232014-01-16 22:16:42 -0800462 private void registerDisplayTransactionListenerInternal(
463 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800464 // List is self-synchronized copy-on-write.
465 mDisplayTransactionListeners.add(listener);
466 }
467
Jeff Brown4ccb8232014-01-16 22:16:42 -0800468 private void unregisterDisplayTransactionListenerInternal(
469 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800470 // List is self-synchronized copy-on-write.
471 mDisplayTransactionListeners.remove(listener);
472 }
473
Jeff Brown4ccb8232014-01-16 22:16:42 -0800474 private void setDisplayInfoOverrideFromWindowManagerInternal(
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700475 int displayId, DisplayInfo info) {
476 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700477 LogicalDisplay display = mLogicalDisplays.get(displayId);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700478 if (display != null) {
Jeff Brownef981a42013-08-07 14:13:37 -0700479 if (display.setDisplayInfoOverrideFromWindowManagerLocked(info)) {
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200480 handleLogicalDisplayChanged(displayId, display);
Craig Mautner65d11b32012-10-01 13:59:52 -0700481 scheduleTraversalLocked(false);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700482 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700483 }
484 }
485 }
486
Andrii Kuliancd097992017-03-23 18:31:59 -0700487 /**
488 * @see DisplayManagerInternal#getNonOverrideDisplayInfo(int, DisplayInfo)
489 */
490 private void getNonOverrideDisplayInfoInternal(int displayId, DisplayInfo outInfo) {
491 synchronized (mSyncRoot) {
492 final LogicalDisplay display = mLogicalDisplays.get(displayId);
493 if (display != null) {
494 display.getNonOverrideDisplayInfoLocked(outInfo);
495 }
496 }
497 }
498
Santos Cordonee8931e2017-04-05 10:31:15 -0700499 @VisibleForTesting
Robert Carrae606b42018-02-15 15:36:23 -0800500 void performTraversalInternal(SurfaceControl.Transaction t) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700501 synchronized (mSyncRoot) {
502 if (!mPendingTraversal) {
503 return;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700504 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700505 mPendingTraversal = false;
506
Robert Carrae606b42018-02-15 15:36:23 -0800507 performTraversalLocked(t);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700508 }
Jeff Brown7f3994e2012-12-04 14:04:28 -0800509
510 // List is self-synchronized copy-on-write.
511 for (DisplayTransactionListener listener : mDisplayTransactionListeners) {
Vishnu Naire86bd982018-11-28 13:23:17 -0800512 listener.onDisplayTransaction(t);
Jeff Brown7f3994e2012-12-04 14:04:28 -0800513 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700514 }
515
Jeff Brown5d6443b2015-04-10 20:15:01 -0700516 private void requestGlobalDisplayStateInternal(int state, int brightness) {
517 if (state == Display.STATE_UNKNOWN) {
518 state = Display.STATE_ON;
519 }
520 if (state == Display.STATE_OFF) {
521 brightness = PowerManager.BRIGHTNESS_OFF;
522 } else if (brightness < 0) {
523 brightness = PowerManager.BRIGHTNESS_DEFAULT;
524 } else if (brightness > PowerManager.BRIGHTNESS_ON) {
525 brightness = PowerManager.BRIGHTNESS_ON;
526 }
527
Jeff Browne75926d2014-09-18 15:24:49 -0700528 synchronized (mTempDisplayStateWorkQueue) {
529 try {
530 // Update the display state within the lock.
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700531 // Note that we do not need to schedule traversals here although it
532 // may happen as a side-effect of displays changing state.
Jeff Browne75926d2014-09-18 15:24:49 -0700533 synchronized (mSyncRoot) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700534 if (mGlobalDisplayState == state
535 && mGlobalDisplayBrightness == brightness) {
536 return; // no change
Jeff Browne75926d2014-09-18 15:24:49 -0700537 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700538
539 Trace.traceBegin(Trace.TRACE_TAG_POWER, "requestGlobalDisplayState("
540 + Display.stateToString(state)
541 + ", brightness=" + brightness + ")");
542 mGlobalDisplayState = state;
543 mGlobalDisplayBrightness = brightness;
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700544 applyGlobalDisplayStateLocked(mTempDisplayStateWorkQueue);
Jeff Browne75926d2014-09-18 15:24:49 -0700545 }
546
547 // Setting the display power state can take hundreds of milliseconds
548 // to complete so we defer the most expensive part of the work until
549 // after we have exited the critical section to avoid blocking other
550 // threads for a long time.
551 for (int i = 0; i < mTempDisplayStateWorkQueue.size(); i++) {
552 mTempDisplayStateWorkQueue.get(i).run();
553 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700554 Trace.traceEnd(Trace.TRACE_TAG_POWER);
Jeff Browne75926d2014-09-18 15:24:49 -0700555 } finally {
556 mTempDisplayStateWorkQueue.clear();
Jeff Brown9e316a12012-10-08 19:17:06 -0700557 }
558 }
559 }
560
Jeff Brown4ccb8232014-01-16 22:16:42 -0800561 private DisplayInfo getDisplayInfoInternal(int displayId, int callingUid) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700562 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800563 LogicalDisplay display = mLogicalDisplays.get(displayId);
564 if (display != null) {
565 DisplayInfo info = display.getDisplayInfoLocked();
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800566 if (info.hasAccess(callingUid)
567 || isUidPresentOnDisplayInternal(callingUid, displayId)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800568 return info;
569 }
570 }
571 return null;
572 }
573 }
574
575 private int[] getDisplayIdsInternal(int callingUid) {
576 synchronized (mSyncRoot) {
577 final int count = mLogicalDisplays.size();
578 int[] displayIds = new int[count];
579 int n = 0;
580 for (int i = 0; i < count; i++) {
581 LogicalDisplay display = mLogicalDisplays.valueAt(i);
582 DisplayInfo info = display.getDisplayInfoLocked();
583 if (info.hasAccess(callingUid)) {
584 displayIds[n++] = mLogicalDisplays.keyAt(i);
585 }
586 }
587 if (n != count) {
588 displayIds = Arrays.copyOfRange(displayIds, 0, n);
589 }
590 return displayIds;
591 }
592 }
593
594 private void registerCallbackInternal(IDisplayManagerCallback callback, int callingPid) {
595 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700596 if (mCallbacks.get(callingPid) != null) {
597 throw new SecurityException("The calling process has already "
598 + "registered an IDisplayManagerCallback.");
Jeff Brown64a55af2012-08-26 02:47:39 -0700599 }
600
Jeff Brownbd6e1502012-08-28 03:27:37 -0700601 CallbackRecord record = new CallbackRecord(callingPid, callback);
602 try {
603 IBinder binder = callback.asBinder();
604 binder.linkToDeath(record, 0);
605 } catch (RemoteException ex) {
606 // give up
607 throw new RuntimeException(ex);
608 }
609
610 mCallbacks.put(callingPid, record);
611 }
612 }
613
Jeff Brownce468a32013-11-21 16:42:03 -0800614 private void onCallbackDied(CallbackRecord record) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700615 synchronized (mSyncRoot) {
Jeff Brownce468a32013-11-21 16:42:03 -0800616 mCallbacks.remove(record.mPid);
617 stopWifiDisplayScanLocked(record);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700618 }
619 }
620
Jeff Brown4ccb8232014-01-16 22:16:42 -0800621 private void startWifiDisplayScanInternal(int callingPid) {
622 synchronized (mSyncRoot) {
623 CallbackRecord record = mCallbacks.get(callingPid);
624 if (record == null) {
625 throw new IllegalStateException("The calling process has not "
626 + "registered an IDisplayManagerCallback.");
Jeff Browne08ae382012-09-07 20:36:36 -0700627 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800628 startWifiDisplayScanLocked(record);
Jeff Browne08ae382012-09-07 20:36:36 -0700629 }
630 }
631
Jeff Brownce468a32013-11-21 16:42:03 -0800632 private void startWifiDisplayScanLocked(CallbackRecord record) {
633 if (!record.mWifiDisplayScanRequested) {
634 record.mWifiDisplayScanRequested = true;
635 if (mWifiDisplayScanRequestCount++ == 0) {
636 if (mWifiDisplayAdapter != null) {
637 mWifiDisplayAdapter.requestStartScanLocked();
638 }
639 }
640 }
641 }
642
Jeff Brown4ccb8232014-01-16 22:16:42 -0800643 private void stopWifiDisplayScanInternal(int callingPid) {
644 synchronized (mSyncRoot) {
645 CallbackRecord record = mCallbacks.get(callingPid);
646 if (record == null) {
647 throw new IllegalStateException("The calling process has not "
648 + "registered an IDisplayManagerCallback.");
Jeff Brownce468a32013-11-21 16:42:03 -0800649 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800650 stopWifiDisplayScanLocked(record);
Jeff Brownce468a32013-11-21 16:42:03 -0800651 }
652 }
653
654 private void stopWifiDisplayScanLocked(CallbackRecord record) {
655 if (record.mWifiDisplayScanRequested) {
656 record.mWifiDisplayScanRequested = false;
657 if (--mWifiDisplayScanRequestCount == 0) {
658 if (mWifiDisplayAdapter != null) {
659 mWifiDisplayAdapter.requestStopScanLocked();
660 }
661 } else if (mWifiDisplayScanRequestCount < 0) {
Dianne Hackborn8d051722014-10-01 14:59:58 -0700662 Slog.wtf(TAG, "mWifiDisplayScanRequestCount became negative: "
Jeff Brownce468a32013-11-21 16:42:03 -0800663 + mWifiDisplayScanRequestCount);
664 mWifiDisplayScanRequestCount = 0;
665 }
666 }
667 }
668
Jeff Brown4ccb8232014-01-16 22:16:42 -0800669 private void connectWifiDisplayInternal(String address) {
670 synchronized (mSyncRoot) {
671 if (mWifiDisplayAdapter != null) {
672 mWifiDisplayAdapter.requestConnectLocked(address);
Jeff Browne08ae382012-09-07 20:36:36 -0700673 }
Jeff Browne08ae382012-09-07 20:36:36 -0700674 }
675 }
676
Jeff Brown4ccb8232014-01-16 22:16:42 -0800677 private void pauseWifiDisplayInternal() {
678 synchronized (mSyncRoot) {
679 if (mWifiDisplayAdapter != null) {
680 mWifiDisplayAdapter.requestPauseLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700681 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700682 }
683 }
684
Jeff Brown4ccb8232014-01-16 22:16:42 -0800685 private void resumeWifiDisplayInternal() {
686 synchronized (mSyncRoot) {
687 if (mWifiDisplayAdapter != null) {
688 mWifiDisplayAdapter.requestResumeLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700689 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700690 }
691 }
692
Jeff Brown4ccb8232014-01-16 22:16:42 -0800693 private void disconnectWifiDisplayInternal() {
694 synchronized (mSyncRoot) {
695 if (mWifiDisplayAdapter != null) {
696 mWifiDisplayAdapter.requestDisconnectLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700697 }
Jeff Browne08ae382012-09-07 20:36:36 -0700698 }
699 }
700
Jeff Brown4ccb8232014-01-16 22:16:42 -0800701 private void renameWifiDisplayInternal(String address, String alias) {
702 synchronized (mSyncRoot) {
703 if (mWifiDisplayAdapter != null) {
704 mWifiDisplayAdapter.requestRenameLocked(address, alias);
Jeff Brown89d55462012-09-19 11:33:42 -0700705 }
Jeff Brown89d55462012-09-19 11:33:42 -0700706 }
707 }
708
Jeff Brown4ccb8232014-01-16 22:16:42 -0800709 private void forgetWifiDisplayInternal(String address) {
710 synchronized (mSyncRoot) {
711 if (mWifiDisplayAdapter != null) {
712 mWifiDisplayAdapter.requestForgetLocked(address);
Jeff Brown89d55462012-09-19 11:33:42 -0700713 }
Jeff Brown89d55462012-09-19 11:33:42 -0700714 }
715 }
716
Jeff Brown4ccb8232014-01-16 22:16:42 -0800717 private WifiDisplayStatus getWifiDisplayStatusInternal() {
718 synchronized (mSyncRoot) {
719 if (mWifiDisplayAdapter != null) {
720 return mWifiDisplayAdapter.getWifiDisplayStatusLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700721 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800722 return new WifiDisplayStatus();
Jeff Browne08ae382012-09-07 20:36:36 -0700723 }
724 }
725
Michael Wright1c9977b2016-07-12 13:30:10 -0700726 private void requestColorModeInternal(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +0100727 synchronized (mSyncRoot) {
728 LogicalDisplay display = mLogicalDisplays.get(displayId);
729 if (display != null &&
Michael Wright1c9977b2016-07-12 13:30:10 -0700730 display.getRequestedColorModeLocked() != colorMode) {
731 display.setRequestedColorModeLocked(colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +0100732 scheduleTraversalLocked(false);
733 }
734 }
735 }
736
Michael Wright75ee9fc2014-09-01 19:55:22 -0700737 private int createVirtualDisplayInternal(IVirtualDisplayCallback callback,
Santos Cordonee8931e2017-04-05 10:31:15 -0700738 IMediaProjection projection, int callingUid, String packageName, String name, int width,
739 int height, int densityDpi, Surface surface, int flags, String uniqueId) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800740 synchronized (mSyncRoot) {
741 if (mVirtualDisplayAdapter == null) {
742 Slog.w(TAG, "Rejecting request to create private virtual display "
743 + "because the virtual display adapter is not available.");
744 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700745 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800746
747 DisplayDevice device = mVirtualDisplayAdapter.createVirtualDisplayLocked(
Santos Cordonee8931e2017-04-05 10:31:15 -0700748 callback, projection, callingUid, packageName, name, width, height, densityDpi,
749 surface, flags, uniqueId);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800750 if (device == null) {
751 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700752 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700753
Jeff Brown4ccb8232014-01-16 22:16:42 -0800754 handleDisplayDeviceAddedLocked(device);
755 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
756 if (display != null) {
757 return display.getDisplayIdLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700758 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800759
760 // Something weird happened and the logical display was not created.
761 Slog.w(TAG, "Rejecting request to create virtual display "
762 + "because the logical display was not created.");
Michael Wright75ee9fc2014-09-01 19:55:22 -0700763 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800764 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700765 }
766 return -1;
767 }
768
Michael Wright01e840f2014-06-26 16:03:25 -0700769 private void resizeVirtualDisplayInternal(IBinder appToken,
770 int width, int height, int densityDpi) {
771 synchronized (mSyncRoot) {
772 if (mVirtualDisplayAdapter == null) {
773 return;
774 }
775
776 mVirtualDisplayAdapter.resizeVirtualDisplayLocked(appToken, width, height, densityDpi);
777 }
778 }
779
Jeff Brown92207df2014-04-16 13:16:07 -0700780 private void setVirtualDisplaySurfaceInternal(IBinder appToken, Surface surface) {
781 synchronized (mSyncRoot) {
782 if (mVirtualDisplayAdapter == null) {
783 return;
784 }
785
786 mVirtualDisplayAdapter.setVirtualDisplaySurfaceLocked(appToken, surface);
787 }
788 }
789
Jeff Brown4ccb8232014-01-16 22:16:42 -0800790 private void releaseVirtualDisplayInternal(IBinder appToken) {
791 synchronized (mSyncRoot) {
792 if (mVirtualDisplayAdapter == null) {
793 return;
Jeff Browna506a6e2013-06-04 00:02:38 -0700794 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700795
Jeff Brown4ccb8232014-01-16 22:16:42 -0800796 DisplayDevice device =
797 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(appToken);
798 if (device != null) {
799 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700800 }
801 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700802 }
803
chaviwda4c6942018-11-07 15:52:56 -0800804 private void setVirtualDisplayStateInternal(IBinder appToken, boolean isOn) {
805 synchronized (mSyncRoot) {
806 if (mVirtualDisplayAdapter == null) {
807 return;
808 }
809
810 mVirtualDisplayAdapter.setVirtualDisplayStateLocked(appToken, isOn);
811 }
812 }
813
Santos Cordonc22c5632017-06-21 16:03:49 -0700814 private void registerDefaultDisplayAdapters() {
815 // Register default display adapters.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700816 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700817 // main display adapter
Mike Lockwoode63f6f72013-11-15 11:01:47 -0800818 registerDisplayAdapterLocked(new LocalDisplayAdapter(
819 mSyncRoot, mContext, mHandler, mDisplayAdapterListener));
Santos Cordonc22c5632017-06-21 16:03:49 -0700820
821 // Standalone VR devices rely on a virtual display as their primary display for
822 // 2D UI. We register virtual display adapter along side the main display adapter
823 // here so that it is ready by the time the system sends the home Intent for
824 // early apps like SetupWizard/Launcher. In particular, SUW is displayed using
825 // the virtual display inside VR before any VR-specific apps even run.
826 mVirtualDisplayAdapter = mInjector.getVirtualDisplayAdapter(mSyncRoot, mContext,
827 mHandler, mDisplayAdapterListener);
828 if (mVirtualDisplayAdapter != null) {
829 registerDisplayAdapterLocked(mVirtualDisplayAdapter);
830 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700831 }
832 }
833
834 private void registerAdditionalDisplayAdapters() {
835 synchronized (mSyncRoot) {
836 if (shouldRegisterNonEssentialDisplayAdaptersLocked()) {
Jeff Brown89d55462012-09-19 11:33:42 -0700837 registerOverlayDisplayAdapterLocked();
838 registerWifiDisplayAdapterLocked();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700839 }
840 }
841 }
842
Jeff Brown89d55462012-09-19 11:33:42 -0700843 private void registerOverlayDisplayAdapterLocked() {
844 registerDisplayAdapterLocked(new OverlayDisplayAdapter(
845 mSyncRoot, mContext, mHandler, mDisplayAdapterListener, mUiHandler));
846 }
847
848 private void registerWifiDisplayAdapterLocked() {
849 if (mContext.getResources().getBoolean(
Jeff Brownbbd28a22012-09-20 16:47:15 -0700850 com.android.internal.R.bool.config_enableWifiDisplay)
851 || SystemProperties.getInt(FORCE_WIFI_DISPLAY_ENABLE, -1) == 1) {
Jeff Brown89d55462012-09-19 11:33:42 -0700852 mWifiDisplayAdapter = new WifiDisplayAdapter(
853 mSyncRoot, mContext, mHandler, mDisplayAdapterListener,
854 mPersistentDataStore);
855 registerDisplayAdapterLocked(mWifiDisplayAdapter);
856 }
857 }
858
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700859 private boolean shouldRegisterNonEssentialDisplayAdaptersLocked() {
860 // In safe mode, we disable non-essential display adapters to give the user
861 // an opportunity to fix broken settings or other problems that might affect
862 // system stability.
863 // In only-core mode, we disable non-essential display adapters to minimize
864 // the number of dependencies that are started while in this mode and to
865 // prevent problems that might occur due to the device being encrypted.
866 return !mSafeMode && !mOnlyCore;
867 }
868
869 private void registerDisplayAdapterLocked(DisplayAdapter adapter) {
870 mDisplayAdapters.add(adapter);
871 adapter.registerLocked();
872 }
873
Jeff Brownbd6e1502012-08-28 03:27:37 -0700874 private void handleDisplayDeviceAdded(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700875 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700876 handleDisplayDeviceAddedLocked(device);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700877 }
878 }
879
Jeff Browna506a6e2013-06-04 00:02:38 -0700880 private void handleDisplayDeviceAddedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700881 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700882 if (mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700883 Slog.w(TAG, "Attempted to add already added display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700884 return;
885 }
886
Jeff Brown10acf6d2015-04-14 14:20:47 -0700887 Slog.i(TAG, "Display device added: " + info);
888 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700889
890 mDisplayDevices.add(device);
Michael Wright1c9977b2016-07-12 13:30:10 -0700891 LogicalDisplay display = addLogicalDisplayLocked(device);
Jeff Brown0033a862014-10-08 12:06:39 -0700892 Runnable work = updateDisplayStateLocked(device);
893 if (work != null) {
894 work.run();
895 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700896 scheduleTraversalLocked(false);
897 }
898
Jeff Brownbd6e1502012-08-28 03:27:37 -0700899 private void handleDisplayDeviceChanged(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700900 synchronized (mSyncRoot) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700901 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700902 if (!mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700903 Slog.w(TAG, "Attempted to change non-existent display device: " + info);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700904 return;
905 }
906
Jeff Brown10acf6d2015-04-14 14:20:47 -0700907 int diff = device.mDebugLastLoggedDeviceInfo.diff(info);
908 if (diff == DisplayDeviceInfo.DIFF_STATE) {
909 Slog.i(TAG, "Display device changed state: \"" + info.name
910 + "\", " + Display.stateToString(info.state));
911 } else if (diff != 0) {
912 Slog.i(TAG, "Display device changed: " + info);
913 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700914 if ((diff & DisplayDeviceInfo.DIFF_COLOR_MODE) != 0) {
915 try {
916 mPersistentDataStore.setColorMode(device, info.colorMode);
917 } finally {
918 mPersistentDataStore.saveIfNeeded();
919 }
920 }
Jeff Brown10acf6d2015-04-14 14:20:47 -0700921 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browne87bf032012-09-20 18:30:13 -0700922
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700923 device.applyPendingDisplayDeviceInfoChangesLocked();
924 if (updateLogicalDisplaysLocked()) {
Craig Mautner65d11b32012-10-01 13:59:52 -0700925 scheduleTraversalLocked(false);
Jeff Brown64a55af2012-08-26 02:47:39 -0700926 }
927 }
928 }
929
Jeff Brownbd6e1502012-08-28 03:27:37 -0700930 private void handleDisplayDeviceRemoved(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700931 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700932 handleDisplayDeviceRemovedLocked(device);
933 }
934 }
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700935
Jeff Browna506a6e2013-06-04 00:02:38 -0700936 private void handleDisplayDeviceRemovedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700937 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700938 if (!mDisplayDevices.remove(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700939 Slog.w(TAG, "Attempted to remove non-existent display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700940 return;
941 }
942
Jeff Brown10acf6d2015-04-14 14:20:47 -0700943 Slog.i(TAG, "Display device removed: " + info);
944 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700945
Jeff Browna506a6e2013-06-04 00:02:38 -0700946 updateLogicalDisplaysLocked();
947 scheduleTraversalLocked(false);
948 }
949
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200950 private void handleLogicalDisplayChanged(int displayId, @NonNull LogicalDisplay display) {
951 if (displayId == Display.DEFAULT_DISPLAY) {
952 recordTopInsetLocked(display);
953 }
954 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
955 }
956
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700957 private void applyGlobalDisplayStateLocked(List<Runnable> workQueue) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700958 final int count = mDisplayDevices.size();
959 for (int i = 0; i < count; i++) {
960 DisplayDevice device = mDisplayDevices.get(i);
Jeff Browne75926d2014-09-18 15:24:49 -0700961 Runnable runnable = updateDisplayStateLocked(device);
962 if (runnable != null) {
963 workQueue.add(runnable);
964 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700965 }
966 }
967
Jeff Browne75926d2014-09-18 15:24:49 -0700968 private Runnable updateDisplayStateLocked(DisplayDevice device) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700969 // Blank or unblank the display immediately to match the state requested
Jeff Brown037c33e2014-04-09 00:31:55 -0700970 // by the display power controller (if known).
Jeff Browna506a6e2013-06-04 00:02:38 -0700971 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
972 if ((info.flags & DisplayDeviceInfo.FLAG_NEVER_BLANK) == 0) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700973 return device.requestDisplayStateLocked(mGlobalDisplayState, mGlobalDisplayBrightness);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700974 }
Jeff Browne75926d2014-09-18 15:24:49 -0700975 return null;
Craig Mautner4f67ba62012-08-02 11:23:00 -0700976 }
977
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700978 // Adds a new logical display based on the given display device.
979 // Sends notifications if needed.
Michael Wright1c9977b2016-07-12 13:30:10 -0700980 private LogicalDisplay addLogicalDisplayLocked(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700981 DisplayDeviceInfo deviceInfo = device.getDisplayDeviceInfoLocked();
982 boolean isDefault = (deviceInfo.flags
983 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0;
984 if (isDefault && mLogicalDisplays.get(Display.DEFAULT_DISPLAY) != null) {
985 Slog.w(TAG, "Ignoring attempt to add a second default display: " + deviceInfo);
986 isDefault = false;
987 }
988
Jeff Brown27f1d672012-10-17 18:32:34 -0700989 if (!isDefault && mSingleDisplayDemoMode) {
990 Slog.i(TAG, "Not creating a logical display for a secondary display "
991 + " because single display demo mode is enabled: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -0700992 return null;
Jeff Brown27f1d672012-10-17 18:32:34 -0700993 }
994
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700995 final int displayId = assignDisplayIdLocked(isDefault);
996 final int layerStack = assignLayerStackLocked(displayId);
997
Jeff Brownd728bf52012-09-08 18:05:28 -0700998 LogicalDisplay display = new LogicalDisplay(displayId, layerStack, device);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700999 display.updateLocked(mDisplayDevices);
1000 if (!display.isValidLocked()) {
1001 // This should never happen currently.
1002 Slog.w(TAG, "Ignoring display device because the logical display "
1003 + "created from it was not considered valid: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -07001004 return null;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001005 }
1006
Michael Wrighteedcbf12017-08-16 23:14:54 +01001007 configureColorModeLocked(display, device);
1008 if (isDefault) {
1009 recordStableDisplayStatsIfNeededLocked(display);
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001010 recordTopInsetLocked(display);
Michael Wrighteedcbf12017-08-16 23:14:54 +01001011 }
1012
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001013 mLogicalDisplays.put(displayId, display);
1014
1015 // Wake up waitForDefaultDisplay.
1016 if (isDefault) {
1017 mSyncRoot.notifyAll();
1018 }
1019
1020 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_ADDED);
Michael Wright1c9977b2016-07-12 13:30:10 -07001021 return display;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001022 }
1023
1024 private int assignDisplayIdLocked(boolean isDefault) {
1025 return isDefault ? Display.DEFAULT_DISPLAY : mNextNonDefaultDisplayId++;
1026 }
1027
1028 private int assignLayerStackLocked(int displayId) {
1029 // Currently layer stacks and display ids are the same.
1030 // This need not be the case.
1031 return displayId;
1032 }
1033
Michael Wrighteedcbf12017-08-16 23:14:54 +01001034 private void configureColorModeLocked(LogicalDisplay display, DisplayDevice device) {
1035 if (display.getPrimaryDisplayDeviceLocked() == device) {
1036 int colorMode = mPersistentDataStore.getColorMode(device);
1037 if (colorMode == Display.COLOR_MODE_INVALID) {
1038 if ((device.getDisplayDeviceInfoLocked().flags
1039 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
1040 colorMode = mDefaultDisplayDefaultColorMode;
1041 } else {
1042 colorMode = Display.COLOR_MODE_DEFAULT;
1043 }
1044 }
1045 display.setRequestedColorModeLocked(colorMode);
1046 }
1047 }
1048
1049 // If we've never recorded stable device stats for this device before and they aren't
1050 // explicitly configured, go ahead and record the stable device stats now based on the status
1051 // of the default display at first boot.
1052 private void recordStableDisplayStatsIfNeededLocked(LogicalDisplay d) {
1053 if (mStableDisplaySize.x <= 0 && mStableDisplaySize.y <= 0) {
1054 DisplayInfo info = d.getDisplayInfoLocked();
1055 setStableDisplaySizeLocked(info.getNaturalWidth(), info.getNaturalHeight());
1056 }
1057 }
1058
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001059 private void recordTopInsetLocked(@Nullable LogicalDisplay d) {
MÃ¥rten Kongstad250cb2b2018-11-02 11:01:44 +01001060 if (d == null) {
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001061 return;
1062 }
1063 int topInset = d.getInsets().top;
1064 if (topInset == mDefaultDisplayTopInset) {
1065 return;
1066 }
1067 mDefaultDisplayTopInset = topInset;
1068 SystemProperties.set(PROP_DEFAULT_DISPLAY_TOP_INSET, Integer.toString(topInset));
1069 }
1070
Michael Wrighteedcbf12017-08-16 23:14:54 +01001071 private void setStableDisplaySizeLocked(int width, int height) {
1072 mStableDisplaySize = new Point(width, height);
1073 try {
1074 mPersistentDataStore.setStableDisplaySize(mStableDisplaySize);
1075 } finally {
1076 mPersistentDataStore.saveIfNeeded();
1077 }
1078 }
1079
Dan Gittik122df862018-03-28 16:59:22 +01001080 @VisibleForTesting
1081 Curve getMinimumBrightnessCurveInternal() {
1082 return mMinimumBrightnessCurve;
1083 }
1084
Peiyong Lin277eaff2019-01-16 16:18:22 -08001085 int getPreferredWideGamutColorSpaceIdInternal() {
1086 return mWideColorSpace.getId();
1087 }
1088
Michael Wrighteef0e132017-11-21 17:57:52 +00001089 private void setBrightnessConfigurationForUserInternal(
Dan Gittik7a32fba2018-03-28 12:19:38 +01001090 @Nullable BrightnessConfiguration c, @UserIdInt int userId,
Kenny Guy05ce8092018-01-17 13:44:20 +00001091 @Nullable String packageName) {
Dan Gittik122df862018-03-28 16:59:22 +01001092 validateBrightnessConfiguration(c);
Michael Wrighteef0e132017-11-21 17:57:52 +00001093 final int userSerial = getUserManager().getUserSerialNumber(userId);
1094 synchronized (mSyncRoot) {
1095 try {
Kenny Guy05ce8092018-01-17 13:44:20 +00001096 mPersistentDataStore.setBrightnessConfigurationForUser(c, userSerial,
1097 packageName);
Michael Wrighteef0e132017-11-21 17:57:52 +00001098 } finally {
1099 mPersistentDataStore.saveIfNeeded();
1100 }
1101 if (userId == mCurrentUserId) {
1102 mDisplayPowerController.setBrightnessConfiguration(c);
1103 }
1104 }
1105 }
1106
Dan Gittik122df862018-03-28 16:59:22 +01001107 @VisibleForTesting
1108 void validateBrightnessConfiguration(BrightnessConfiguration config) {
1109 if (config == null) {
1110 return;
1111 }
1112 if (isBrightnessConfigurationTooDark(config)) {
1113 throw new IllegalArgumentException("brightness curve is too dark");
1114 }
1115 }
1116
1117 private boolean isBrightnessConfigurationTooDark(BrightnessConfiguration config) {
1118 Pair<float[], float[]> curve = config.getCurve();
1119 float[] lux = curve.first;
1120 float[] nits = curve.second;
1121 for (int i = 0; i < lux.length; i++) {
1122 if (nits[i] < mMinimumBrightnessSpline.interpolate(lux[i])) {
1123 return true;
1124 }
1125 }
1126 return false;
1127 }
1128
Michael Wrighteef0e132017-11-21 17:57:52 +00001129 private void loadBrightnessConfiguration() {
1130 synchronized (mSyncRoot) {
1131 final int userSerial = getUserManager().getUserSerialNumber(mCurrentUserId);
1132 BrightnessConfiguration config =
1133 mPersistentDataStore.getBrightnessConfiguration(userSerial);
1134 mDisplayPowerController.setBrightnessConfiguration(config);
1135 }
1136 }
1137
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001138 // Updates all existing logical displays given the current set of display devices.
1139 // Removes invalid logical displays.
1140 // Sends notifications if needed.
1141 private boolean updateLogicalDisplaysLocked() {
1142 boolean changed = false;
1143 for (int i = mLogicalDisplays.size(); i-- > 0; ) {
1144 final int displayId = mLogicalDisplays.keyAt(i);
1145 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1146
1147 mTempDisplayInfo.copyFrom(display.getDisplayInfoLocked());
1148 display.updateLocked(mDisplayDevices);
1149 if (!display.isValidLocked()) {
1150 mLogicalDisplays.removeAt(i);
1151 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_REMOVED);
1152 changed = true;
1153 } else if (!mTempDisplayInfo.equals(display.getDisplayInfoLocked())) {
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001154 handleLogicalDisplayChanged(displayId, display);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001155 changed = true;
1156 }
1157 }
1158 return changed;
1159 }
1160
Robert Carrae606b42018-02-15 15:36:23 -08001161 private void performTraversalLocked(SurfaceControl.Transaction t) {
Jeff Brownd728bf52012-09-08 18:05:28 -07001162 // Clear all viewports before configuring displays so that we can keep
1163 // track of which ones we have configured.
1164 clearViewportsLocked();
1165
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001166 // Configure each display device.
1167 final int count = mDisplayDevices.size();
1168 for (int i = 0; i < count; i++) {
1169 DisplayDevice device = mDisplayDevices.get(i);
Robert Carrae606b42018-02-15 15:36:23 -08001170 configureDisplayLocked(t, device);
1171 device.performTraversalLocked(t);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001172 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001173
1174 // Tell the input system about these new viewports.
Jeff Brown4ccb8232014-01-16 22:16:42 -08001175 if (mInputManagerInternal != null) {
Jeff Brownd728bf52012-09-08 18:05:28 -07001176 mHandler.sendEmptyMessage(MSG_UPDATE_VIEWPORT);
1177 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001178 }
1179
Michael Wright3f145a22014-07-22 19:46:03 -07001180 private void setDisplayPropertiesInternal(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001181 float requestedRefreshRate, int requestedModeId, boolean inTraversal) {
Craig Mautner722285e2012-09-07 13:55:58 -07001182 synchronized (mSyncRoot) {
1183 LogicalDisplay display = mLogicalDisplays.get(displayId);
Michael Wright3f145a22014-07-22 19:46:03 -07001184 if (display == null) {
1185 return;
1186 }
1187 if (display.hasContentLocked() != hasContent) {
Jeff Brown33041bd2013-08-02 21:11:14 -07001188 if (DEBUG) {
1189 Slog.d(TAG, "Display " + displayId + " hasContent flag changed: "
1190 + "hasContent=" + hasContent + ", inTraversal=" + inTraversal);
1191 }
1192
Craig Mautner722285e2012-09-07 13:55:58 -07001193 display.setHasContentLocked(hasContent);
Craig Mautner65d11b32012-10-01 13:59:52 -07001194 scheduleTraversalLocked(inTraversal);
Craig Mautner722285e2012-09-07 13:55:58 -07001195 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001196 if (requestedModeId == 0 && requestedRefreshRate != 0) {
1197 // Scan supported modes returned by display.getInfo() to find a mode with the same
1198 // size as the default display mode but with the specified refresh rate instead.
1199 requestedModeId = display.getDisplayInfoLocked().findDefaultModeByRefreshRate(
1200 requestedRefreshRate);
1201 }
Michael Wrighta3dab232019-02-22 16:54:21 +00001202 mDisplayModeDirector.getAppRequestObserver().setAppRequestedMode(
1203 displayId, requestedModeId);
Craig Mautner722285e2012-09-07 13:55:58 -07001204 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001205 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001206
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08001207 private void setDisplayOffsetsInternal(int displayId, int x, int y) {
1208 synchronized (mSyncRoot) {
1209 LogicalDisplay display = mLogicalDisplays.get(displayId);
1210 if (display == null) {
1211 return;
1212 }
1213 if (display.getDisplayOffsetXLocked() != x
1214 || display.getDisplayOffsetYLocked() != y) {
1215 if (DEBUG) {
1216 Slog.d(TAG, "Display " + displayId + " burn-in offset set to ("
1217 + x + ", " + y + ")");
1218 }
1219 display.setDisplayOffsetsLocked(x, y);
1220 scheduleTraversalLocked(false);
1221 }
1222 }
1223 }
1224
Sam Lin4c3ac2b2019-02-18 04:50:26 -08001225 private void setDisplayScalingDisabledInternal(int displayId, boolean disable) {
1226 synchronized (mSyncRoot) {
1227 final LogicalDisplay display = mLogicalDisplays.get(displayId);
1228 if (display == null) {
1229 return;
1230 }
1231 if (display.isDisplayScalingDisabled() != disable) {
1232 if (DEBUG) {
1233 Slog.d(TAG, "Display " + displayId + " content scaling disabled = " + disable);
1234 }
1235 display.setDisplayScalingDisabledLocked(disable);
1236 scheduleTraversalLocked(false);
1237 }
1238 }
1239 }
1240
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001241 // Updates the lists of UIDs that are present on displays.
1242 private void setDisplayAccessUIDsInternal(SparseArray<IntArray> newDisplayAccessUIDs) {
1243 synchronized (mSyncRoot) {
1244 mDisplayAccessUIDs.clear();
1245 for (int i = newDisplayAccessUIDs.size() - 1; i >= 0; i--) {
1246 mDisplayAccessUIDs.append(newDisplayAccessUIDs.keyAt(i),
1247 newDisplayAccessUIDs.valueAt(i));
1248 }
1249 }
1250 }
1251
1252 // Checks if provided UID's content is present on the display and UID has access to it.
1253 private boolean isUidPresentOnDisplayInternal(int uid, int displayId) {
1254 synchronized (mSyncRoot) {
1255 final IntArray displayUIDs = mDisplayAccessUIDs.get(displayId);
1256 return displayUIDs != null && displayUIDs.indexOf(uid) != -1;
1257 }
1258 }
1259
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001260 @Nullable
1261 private IBinder getDisplayToken(int displayId) {
1262 synchronized (mSyncRoot) {
1263 final LogicalDisplay display = mLogicalDisplays.get(displayId);
1264 if (display != null) {
1265 final DisplayDevice device = display.getPrimaryDisplayDeviceLocked();
1266 if (device != null) {
1267 return device.getDisplayTokenLocked();
1268 }
1269 }
1270 }
1271
1272 return null;
1273 }
1274
Robert Carr66b5664f2019-04-02 14:18:56 -07001275 private SurfaceControl.ScreenshotGraphicBuffer screenshotInternal(int displayId) {
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001276 final IBinder token = getDisplayToken(displayId);
1277 if (token == null) {
Robert Carr66b5664f2019-04-02 14:18:56 -07001278 return null;
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001279 }
Robert Carr66b5664f2019-04-02 14:18:56 -07001280 return SurfaceControl.screenshotToBufferWithSecureLayersUnsafe(
Peiyong Line3e5efd2019-03-21 20:59:47 +00001281 token, new Rect(), 0 /* width */, 0 /* height */,
1282 false /* useIdentityTransform */, 0 /* rotation */);
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001283 }
1284
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001285 @VisibleForTesting
1286 DisplayedContentSamplingAttributes getDisplayedContentSamplingAttributesInternal(
1287 int displayId) {
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001288 final IBinder token = getDisplayToken(displayId);
1289 if (token == null) {
1290 return null;
1291 }
1292 return SurfaceControl.getDisplayedContentSamplingAttributes(token);
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001293 }
1294
1295 @VisibleForTesting
1296 boolean setDisplayedContentSamplingEnabledInternal(
1297 int displayId, boolean enable, int componentMask, int maxFrames) {
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001298 final IBinder token = getDisplayToken(displayId);
1299 if (token == null) {
1300 return false;
1301 }
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001302 return SurfaceControl.setDisplayedContentSamplingEnabled(
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001303 token, enable, componentMask, maxFrames);
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001304 }
1305
1306 @VisibleForTesting
1307 DisplayedContentSample getDisplayedContentSampleInternal(int displayId,
1308 long maxFrames, long timestamp) {
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001309 final IBinder token = getDisplayToken(displayId);
1310 if (token == null) {
1311 return null;
1312 }
1313 return SurfaceControl.getDisplayedContentSample(token, maxFrames, timestamp);
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001314 }
1315
Michael Wrighta3dab232019-02-22 16:54:21 +00001316 private void onAllowedDisplayModesChangedInternal() {
1317 boolean changed = false;
1318 synchronized (mSyncRoot) {
1319 final int count = mLogicalDisplays.size();
1320 for (int i = 0; i < count; i++) {
1321 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1322 int displayId = mLogicalDisplays.keyAt(i);
1323 int[] allowedModes = mDisplayModeDirector.getAllowedModes(displayId);
1324 // Note that order is important here since not all display devices are capable of
1325 // automatically switching, so we do actually want to check for equality and not
1326 // just equivalent contents (regardless of order).
1327 if (!Arrays.equals(allowedModes, display.getAllowedDisplayModesLocked())) {
1328 display.setAllowedDisplayModesLocked(allowedModes);
1329 changed = true;
1330 }
1331 }
1332 if (changed) {
1333 scheduleTraversalLocked(false);
1334 }
1335 }
1336 }
1337
Jeff Brownd728bf52012-09-08 18:05:28 -07001338 private void clearViewportsLocked() {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001339 mViewports.clear();
Craig Mautner722285e2012-09-07 13:55:58 -07001340 }
1341
Robert Carrae606b42018-02-15 15:36:23 -08001342 private void configureDisplayLocked(SurfaceControl.Transaction t, DisplayDevice device) {
Jeff Brownd14c8c92014-01-07 18:13:09 -08001343 final DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
1344 final boolean ownContent = (info.flags & DisplayDeviceInfo.FLAG_OWN_CONTENT_ONLY) != 0;
Jeff Browna506a6e2013-06-04 00:02:38 -07001345
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001346 // Find the logical display that the display device is showing.
Jeff Brownd14c8c92014-01-07 18:13:09 -08001347 // Certain displays only ever show their own content.
Craig Mautner722285e2012-09-07 13:55:58 -07001348 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
Jeff Brownd14c8c92014-01-07 18:13:09 -08001349 if (!ownContent) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001350 if (display != null && !display.hasContentLocked()) {
1351 // If the display does not have any content of its own, then
1352 // automatically mirror the default logical display contents.
1353 display = null;
1354 }
1355 if (display == null) {
1356 display = mLogicalDisplays.get(Display.DEFAULT_DISPLAY);
1357 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001358 }
1359
1360 // Apply the logical display configuration to the display device.
1361 if (display == null) {
1362 // TODO: no logical display for the device, blank it
Jeff Brownd728bf52012-09-08 18:05:28 -07001363 Slog.w(TAG, "Missing logical display to use for physical display device: "
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001364 + device.getDisplayDeviceInfoLocked());
Jeff Brownd728bf52012-09-08 18:05:28 -07001365 return;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001366 }
Robert Carrae606b42018-02-15 15:36:23 -08001367 display.configureDisplayLocked(t, device, info.state == Display.STATE_OFF);
Arthur Hung41e81e72018-10-31 18:04:56 +08001368 final int viewportType;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001369 // Update the corresponding viewport.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001370 if ((info.flags & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
Arthur Hung41e81e72018-10-31 18:04:56 +08001371 viewportType = VIEWPORT_INTERNAL;
1372 } else if (info.touch == DisplayDeviceInfo.TOUCH_EXTERNAL) {
1373 viewportType = VIEWPORT_EXTERNAL;
1374 } else if (info.touch == DisplayDeviceInfo.TOUCH_VIRTUAL
1375 && !TextUtils.isEmpty(info.uniqueId)) {
1376 viewportType = VIEWPORT_VIRTUAL;
1377 } else {
Arthur Hung46c4e582019-02-12 15:55:28 +08001378 Slog.i(TAG, "Display " + info + " does not support input device matching.");
Arthur Hung41e81e72018-10-31 18:04:56 +08001379 return;
Jeff Brownd728bf52012-09-08 18:05:28 -07001380 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001381
Arthur Hung41e81e72018-10-31 18:04:56 +08001382 populateViewportLocked(viewportType, display.getDisplayIdLocked(), device, info.uniqueId);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001383 }
1384
1385 /**
1386 * Get internal or external viewport. Create it if does not currently exist.
1387 * @param viewportType - either INTERNAL or EXTERNAL
1388 * @return the viewport with the requested type
1389 */
Arthur Hung41e81e72018-10-31 18:04:56 +08001390 private DisplayViewport getViewportLocked(int viewportType, String uniqueId) {
1391 if (viewportType != VIEWPORT_INTERNAL && viewportType != VIEWPORT_EXTERNAL
1392 && viewportType != VIEWPORT_VIRTUAL) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001393 Slog.wtf(TAG, "Cannot call getViewportByTypeLocked for type "
1394 + DisplayViewport.typeToString(viewportType));
1395 return null;
1396 }
Arthur Hung41e81e72018-10-31 18:04:56 +08001397
1398 // Only allow a single INTERNAL or EXTERNAL viewport by forcing their uniqueIds
1399 // to be identical (in particular, empty).
1400 // TODO (b/116824030) allow multiple EXTERNAL viewports and remove this function.
1401 if (viewportType != VIEWPORT_VIRTUAL) {
1402 uniqueId = "";
1403 }
1404
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001405 DisplayViewport viewport;
1406 final int count = mViewports.size();
1407 for (int i = 0; i < count; i++) {
1408 viewport = mViewports.get(i);
Arthur Hung41e81e72018-10-31 18:04:56 +08001409 if (viewport.type == viewportType && uniqueId.equals(viewport.uniqueId)) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001410 return viewport;
1411 }
1412 }
1413
Arthur Hung41e81e72018-10-31 18:04:56 +08001414 // Creates the viewport if none exists.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001415 viewport = new DisplayViewport();
1416 viewport.type = viewportType;
Arthur Hung41e81e72018-10-31 18:04:56 +08001417 viewport.uniqueId = uniqueId;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001418 mViewports.add(viewport);
1419 return viewport;
1420 }
1421
Arthur Hung41e81e72018-10-31 18:04:56 +08001422 private void populateViewportLocked(int viewportType,
1423 int displayId, DisplayDevice device, String uniqueId) {
1424 final DisplayViewport viewport = getViewportLocked(viewportType, uniqueId);
Jeff Brownd728bf52012-09-08 18:05:28 -07001425 device.populateViewportLocked(viewport);
Arthur Hung41e81e72018-10-31 18:04:56 +08001426 viewport.valid = true;
1427 viewport.displayId = displayId;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001428 }
1429
1430 private LogicalDisplay findLogicalDisplayForDeviceLocked(DisplayDevice device) {
1431 final int count = mLogicalDisplays.size();
1432 for (int i = 0; i < count; i++) {
1433 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1434 if (display.getPrimaryDisplayDeviceLocked() == device) {
1435 return display;
1436 }
1437 }
1438 return null;
1439 }
1440
Jeff Brownbd6e1502012-08-28 03:27:37 -07001441 private void sendDisplayEventLocked(int displayId, int event) {
1442 Message msg = mHandler.obtainMessage(MSG_DELIVER_DISPLAY_EVENT, displayId, event);
1443 mHandler.sendMessage(msg);
1444 }
1445
Robert Carrae606b42018-02-15 15:36:23 -08001446 // Requests that performTraversals be called at a
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001447 // later time to apply changes to surfaces and displays.
Craig Mautner65d11b32012-10-01 13:59:52 -07001448 private void scheduleTraversalLocked(boolean inTraversal) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001449 if (!mPendingTraversal && mWindowManagerInternal != null) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001450 mPendingTraversal = true;
Craig Mautner65d11b32012-10-01 13:59:52 -07001451 if (!inTraversal) {
1452 mHandler.sendEmptyMessage(MSG_REQUEST_TRAVERSAL);
1453 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001454 }
1455 }
1456
1457 // Runs on Handler thread.
1458 // Delivers display event notifications to callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001459 private void deliverDisplayEvent(int displayId, int event) {
1460 if (DEBUG) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001461 Slog.d(TAG, "Delivering display event: displayId="
1462 + displayId + ", event=" + event);
Jeff Brownfa25bf52012-07-23 19:26:30 -07001463 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07001464
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001465 // Grab the lock and copy the callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001466 final int count;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001467 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001468 count = mCallbacks.size();
1469 mTempCallbacks.clear();
1470 for (int i = 0; i < count; i++) {
1471 mTempCallbacks.add(mCallbacks.valueAt(i));
Craig Mautner4f67ba62012-08-02 11:23:00 -07001472 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001473 }
Craig Mautner4f67ba62012-08-02 11:23:00 -07001474
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001475 // After releasing the lock, send the notifications out.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001476 for (int i = 0; i < count; i++) {
1477 mTempCallbacks.get(i).notifyDisplayEventAsync(displayId, event);
1478 }
1479 mTempCallbacks.clear();
Craig Mautner4f67ba62012-08-02 11:23:00 -07001480 }
1481
Michael Wrightc39d47a2014-07-08 18:07:36 -07001482 private IMediaProjectionManager getProjectionService() {
1483 if (mProjectionService == null) {
1484 IBinder b = ServiceManager.getService(Context.MEDIA_PROJECTION_SERVICE);
1485 mProjectionService = IMediaProjectionManager.Stub.asInterface(b);
1486 }
1487 return mProjectionService;
1488 }
1489
Michael Wrighteef0e132017-11-21 17:57:52 +00001490 private UserManager getUserManager() {
1491 return mContext.getSystemService(UserManager.class);
1492 }
1493
Jeff Brown4ccb8232014-01-16 22:16:42 -08001494 private void dumpInternal(PrintWriter pw) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001495 pw.println("DISPLAY MANAGER (dumpsys display)");
Jeff Brownfa25bf52012-07-23 19:26:30 -07001496
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001497 synchronized (mSyncRoot) {
Jeff Brown9e316a12012-10-08 19:17:06 -07001498 pw.println(" mOnlyCode=" + mOnlyCore);
1499 pw.println(" mSafeMode=" + mSafeMode);
1500 pw.println(" mPendingTraversal=" + mPendingTraversal);
Jeff Brown037c33e2014-04-09 00:31:55 -07001501 pw.println(" mGlobalDisplayState=" + Display.stateToString(mGlobalDisplayState));
Jeff Brown9e316a12012-10-08 19:17:06 -07001502 pw.println(" mNextNonDefaultDisplayId=" + mNextNonDefaultDisplayId);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001503 pw.println(" mViewports=" + mViewports);
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -07001504 pw.println(" mDefaultDisplayDefaultColorMode=" + mDefaultDisplayDefaultColorMode);
Jeff Brown27f1d672012-10-17 18:32:34 -07001505 pw.println(" mSingleDisplayDemoMode=" + mSingleDisplayDemoMode);
Jeff Brownce468a32013-11-21 16:42:03 -08001506 pw.println(" mWifiDisplayScanRequestCount=" + mWifiDisplayScanRequestCount);
Michael Wrighteedcbf12017-08-16 23:14:54 +01001507 pw.println(" mStableDisplaySize=" + mStableDisplaySize);
Kenny Guy2047db92019-02-21 13:04:05 +00001508 pw.println(" mMinimumBrightnessCurve=" + mMinimumBrightnessCurve);
Jeff Brown9e316a12012-10-08 19:17:06 -07001509
Jeff Brownbd6e1502012-08-28 03:27:37 -07001510 IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001511 ipw.increaseIndent();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001512
1513 pw.println();
1514 pw.println("Display Adapters: size=" + mDisplayAdapters.size());
Jeff Brown848c2dc2012-08-19 20:18:08 -07001515 for (DisplayAdapter adapter : mDisplayAdapters) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001516 pw.println(" " + adapter.getName());
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001517 adapter.dumpLocked(ipw);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001518 }
Craig Mautner9de49362012-08-02 14:30:30 -07001519
Jeff Brownbd6e1502012-08-28 03:27:37 -07001520 pw.println();
1521 pw.println("Display Devices: size=" + mDisplayDevices.size());
1522 for (DisplayDevice device : mDisplayDevices) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001523 pw.println(" " + device.getDisplayDeviceInfoLocked());
1524 device.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001525 }
1526
1527 final int logicalDisplayCount = mLogicalDisplays.size();
1528 pw.println();
1529 pw.println("Logical Displays: size=" + logicalDisplayCount);
1530 for (int i = 0; i < logicalDisplayCount; i++) {
1531 int displayId = mLogicalDisplays.keyAt(i);
1532 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1533 pw.println(" Display " + displayId + ":");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001534 display.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001535 }
Jeff Brownce468a32013-11-21 16:42:03 -08001536
Michael Wrighta3dab232019-02-22 16:54:21 +00001537 pw.println();
1538 mDisplayModeDirector.dump(pw);
1539
Jeff Brownce468a32013-11-21 16:42:03 -08001540 final int callbackCount = mCallbacks.size();
1541 pw.println();
1542 pw.println("Callbacks: size=" + callbackCount);
1543 for (int i = 0; i < callbackCount; i++) {
1544 CallbackRecord callback = mCallbacks.valueAt(i);
1545 pw.println(" " + i + ": mPid=" + callback.mPid
1546 + ", mWifiDisplayScanRequested=" + callback.mWifiDisplayScanRequested);
1547 }
Jeff Brownad9ef192014-04-08 17:26:30 -07001548
1549 if (mDisplayPowerController != null) {
1550 mDisplayPowerController.dump(pw);
1551 }
Michael Wright1c9977b2016-07-12 13:30:10 -07001552
1553 pw.println();
1554 mPersistentDataStore.dump(pw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001555 }
1556 }
1557
Dan Gittik122df862018-03-28 16:59:22 +01001558 private static float[] getFloatArray(TypedArray array) {
1559 int length = array.length();
1560 float[] floatArray = new float[length];
1561 for (int i = 0; i < length; i++) {
1562 floatArray[i] = array.getFloat(i, Float.NaN);
1563 }
1564 array.recycle();
1565 return floatArray;
1566 }
1567
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001568 /**
1569 * This is the object that everything in the display manager locks on.
1570 * We make it an inner class within the {@link DisplayManagerService} to so that it is
1571 * clear that the object belongs to the display manager service and that it is
1572 * a unique object with a special purpose.
1573 */
1574 public static final class SyncRoot {
1575 }
1576
Santos Cordonee8931e2017-04-05 10:31:15 -07001577 @VisibleForTesting
1578 static class Injector {
1579 VirtualDisplayAdapter getVirtualDisplayAdapter(SyncRoot syncRoot, Context context,
1580 Handler handler, DisplayAdapter.Listener displayAdapterListener) {
1581 return new VirtualDisplayAdapter(syncRoot, context, handler, displayAdapterListener);
1582 }
Santos Cordonc22c5632017-06-21 16:03:49 -07001583
1584 long getDefaultDisplayDelayTimeout() {
1585 return WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT;
1586 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001587 }
1588
Alex Sakhartchouk879d24f2017-06-20 22:01:19 -04001589 @VisibleForTesting
1590 DisplayDeviceInfo getDisplayDeviceInfoInternal(int displayId) {
1591 synchronized (mSyncRoot) {
1592 LogicalDisplay display = mLogicalDisplays.get(displayId);
1593 if (display != null) {
1594 DisplayDevice displayDevice = display.getPrimaryDisplayDeviceLocked();
1595 return displayDevice.getDisplayDeviceInfoLocked();
1596 }
1597 return null;
1598 }
1599 }
1600
Jeff Brownbd6e1502012-08-28 03:27:37 -07001601 private final class DisplayManagerHandler extends Handler {
1602 public DisplayManagerHandler(Looper looper) {
1603 super(looper, null, true /*async*/);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001604 }
Jeff Brownbf5740e2012-08-19 23:20:02 -07001605
Jeff Brownbd6e1502012-08-28 03:27:37 -07001606 @Override
1607 public void handleMessage(Message msg) {
1608 switch (msg.what) {
Santos Cordonc22c5632017-06-21 16:03:49 -07001609 case MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS:
1610 registerDefaultDisplayAdapters();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001611 break;
1612
1613 case MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS:
1614 registerAdditionalDisplayAdapters();
1615 break;
1616
1617 case MSG_DELIVER_DISPLAY_EVENT:
1618 deliverDisplayEvent(msg.arg1, msg.arg2);
1619 break;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001620
1621 case MSG_REQUEST_TRAVERSAL:
Jeff Brown4ccb8232014-01-16 22:16:42 -08001622 mWindowManagerInternal.requestTraversalFromDisplayManager();
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001623 break;
Jeff Brownd728bf52012-09-08 18:05:28 -07001624
1625 case MSG_UPDATE_VIEWPORT: {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001626 final boolean changed;
Jeff Brownd728bf52012-09-08 18:05:28 -07001627 synchronized (mSyncRoot) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001628 changed = !mTempViewports.equals(mViewports);
1629 if (changed) {
1630 mTempViewports.clear();
1631 for (DisplayViewport d : mViewports) {
1632 mTempViewports.add(d.makeCopy());
1633 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001634 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001635 }
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001636 if (changed) {
1637 mInputManagerInternal.setDisplayViewports(mTempViewports);
1638 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001639 break;
1640 }
Kenny Guy22bd0442017-10-26 00:15:54 +01001641
Michael Wrighteef0e132017-11-21 17:57:52 +00001642 case MSG_LOAD_BRIGHTNESS_CONFIGURATION:
1643 loadBrightnessConfiguration();
1644 break;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001645 }
1646 }
1647 }
1648
1649 private final class DisplayAdapterListener implements DisplayAdapter.Listener {
1650 @Override
1651 public void onDisplayDeviceEvent(DisplayDevice device, int event) {
1652 switch (event) {
1653 case DisplayAdapter.DISPLAY_DEVICE_EVENT_ADDED:
1654 handleDisplayDeviceAdded(device);
1655 break;
1656
1657 case DisplayAdapter.DISPLAY_DEVICE_EVENT_CHANGED:
1658 handleDisplayDeviceChanged(device);
1659 break;
1660
1661 case DisplayAdapter.DISPLAY_DEVICE_EVENT_REMOVED:
1662 handleDisplayDeviceRemoved(device);
1663 break;
1664 }
1665 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001666
1667 @Override
1668 public void onTraversalRequested() {
1669 synchronized (mSyncRoot) {
Craig Mautner65d11b32012-10-01 13:59:52 -07001670 scheduleTraversalLocked(false);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001671 }
1672 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001673 }
1674
1675 private final class CallbackRecord implements DeathRecipient {
Jeff Brownce468a32013-11-21 16:42:03 -08001676 public final int mPid;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001677 private final IDisplayManagerCallback mCallback;
1678
Jeff Brownce468a32013-11-21 16:42:03 -08001679 public boolean mWifiDisplayScanRequested;
1680
Jeff Brownbd6e1502012-08-28 03:27:37 -07001681 public CallbackRecord(int pid, IDisplayManagerCallback callback) {
1682 mPid = pid;
1683 mCallback = callback;
1684 }
1685
1686 @Override
1687 public void binderDied() {
1688 if (DEBUG) {
1689 Slog.d(TAG, "Display listener for pid " + mPid + " died.");
1690 }
Jeff Brownce468a32013-11-21 16:42:03 -08001691 onCallbackDied(this);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001692 }
1693
1694 public void notifyDisplayEventAsync(int displayId, int event) {
1695 try {
1696 mCallback.onDisplayEvent(displayId, event);
1697 } catch (RemoteException ex) {
1698 Slog.w(TAG, "Failed to notify process "
1699 + mPid + " that displays changed, assuming it died.", ex);
1700 binderDied();
1701 }
1702 }
1703 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001704
Santos Cordonee8931e2017-04-05 10:31:15 -07001705 @VisibleForTesting
1706 final class BinderService extends IDisplayManager.Stub {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001707 /**
1708 * Returns information about the specified logical display.
1709 *
1710 * @param displayId The logical display id.
lumarkec75b422019-01-07 15:58:38 +08001711 * @return The logical display info, return {@code null} if the display does not exist or
1712 * the calling UID isn't present on the display. The returned object must be treated as
1713 * immutable.
Jeff Brown4ccb8232014-01-16 22:16:42 -08001714 */
1715 @Override // Binder call
1716 public DisplayInfo getDisplayInfo(int displayId) {
1717 final int callingUid = Binder.getCallingUid();
1718 final long token = Binder.clearCallingIdentity();
1719 try {
1720 return getDisplayInfoInternal(displayId, callingUid);
1721 } finally {
1722 Binder.restoreCallingIdentity(token);
1723 }
1724 }
1725
1726 /**
1727 * Returns the list of all display ids.
1728 */
1729 @Override // Binder call
1730 public int[] getDisplayIds() {
1731 final int callingUid = Binder.getCallingUid();
1732 final long token = Binder.clearCallingIdentity();
1733 try {
1734 return getDisplayIdsInternal(callingUid);
1735 } finally {
1736 Binder.restoreCallingIdentity(token);
1737 }
1738 }
1739
lumarkec75b422019-01-07 15:58:38 +08001740 @Override // Binder call
1741 public boolean isUidPresentOnDisplay(int uid, int displayId) {
1742 final long token = Binder.clearCallingIdentity();
1743 try {
1744 return isUidPresentOnDisplayInternal(uid, displayId);
1745 } finally {
1746 Binder.restoreCallingIdentity(token);
1747 }
1748 }
1749
Michael Wrighteedcbf12017-08-16 23:14:54 +01001750 /**
1751 * Returns the stable device display size, in pixels.
1752 */
1753 @Override // Binder call
1754 public Point getStableDisplaySize() {
1755 final long token = Binder.clearCallingIdentity();
1756 try {
1757 return getStableDisplaySizeInternal();
1758 } finally {
1759 Binder.restoreCallingIdentity(token);
1760 }
1761 }
1762
Jeff Brown4ccb8232014-01-16 22:16:42 -08001763 @Override // Binder call
1764 public void registerCallback(IDisplayManagerCallback callback) {
1765 if (callback == null) {
1766 throw new IllegalArgumentException("listener must not be null");
1767 }
1768
1769 final int callingPid = Binder.getCallingPid();
1770 final long token = Binder.clearCallingIdentity();
1771 try {
1772 registerCallbackInternal(callback, callingPid);
1773 } finally {
1774 Binder.restoreCallingIdentity(token);
1775 }
1776 }
1777
1778 @Override // Binder call
1779 public void startWifiDisplayScan() {
1780 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1781 "Permission required to start wifi display scans");
1782
1783 final int callingPid = Binder.getCallingPid();
1784 final long token = Binder.clearCallingIdentity();
1785 try {
1786 startWifiDisplayScanInternal(callingPid);
1787 } finally {
1788 Binder.restoreCallingIdentity(token);
1789 }
1790 }
1791
1792 @Override // Binder call
1793 public void stopWifiDisplayScan() {
1794 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1795 "Permission required to stop wifi display scans");
1796
1797 final int callingPid = Binder.getCallingPid();
1798 final long token = Binder.clearCallingIdentity();
1799 try {
1800 stopWifiDisplayScanInternal(callingPid);
1801 } finally {
1802 Binder.restoreCallingIdentity(token);
1803 }
1804 }
1805
1806 @Override // Binder call
1807 public void connectWifiDisplay(String address) {
1808 if (address == null) {
1809 throw new IllegalArgumentException("address must not be null");
1810 }
1811 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1812 "Permission required to connect to a wifi display");
1813
1814 final long token = Binder.clearCallingIdentity();
1815 try {
1816 connectWifiDisplayInternal(address);
1817 } finally {
1818 Binder.restoreCallingIdentity(token);
1819 }
1820 }
1821
1822 @Override // Binder call
1823 public void disconnectWifiDisplay() {
1824 // This request does not require special permissions.
1825 // Any app can request disconnection from the currently active wifi display.
1826 // This exception should no longer be needed once wifi display control moves
1827 // to the media router service.
1828
1829 final long token = Binder.clearCallingIdentity();
1830 try {
1831 disconnectWifiDisplayInternal();
1832 } finally {
1833 Binder.restoreCallingIdentity(token);
1834 }
1835 }
1836
1837 @Override // Binder call
1838 public void renameWifiDisplay(String address, String alias) {
1839 if (address == null) {
1840 throw new IllegalArgumentException("address must not be null");
1841 }
1842 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1843 "Permission required to rename to a wifi display");
1844
1845 final long token = Binder.clearCallingIdentity();
1846 try {
1847 renameWifiDisplayInternal(address, alias);
1848 } finally {
1849 Binder.restoreCallingIdentity(token);
1850 }
1851 }
1852
1853 @Override // Binder call
1854 public void forgetWifiDisplay(String address) {
1855 if (address == null) {
1856 throw new IllegalArgumentException("address must not be null");
1857 }
1858 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1859 "Permission required to forget to a wifi display");
1860
1861 final long token = Binder.clearCallingIdentity();
1862 try {
1863 forgetWifiDisplayInternal(address);
1864 } finally {
1865 Binder.restoreCallingIdentity(token);
1866 }
1867 }
1868
1869 @Override // Binder call
1870 public void pauseWifiDisplay() {
1871 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1872 "Permission required to pause a wifi display session");
1873
1874 final long token = Binder.clearCallingIdentity();
1875 try {
1876 pauseWifiDisplayInternal();
1877 } finally {
1878 Binder.restoreCallingIdentity(token);
1879 }
1880 }
1881
1882 @Override // Binder call
1883 public void resumeWifiDisplay() {
1884 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1885 "Permission required to resume a wifi display session");
1886
1887 final long token = Binder.clearCallingIdentity();
1888 try {
1889 resumeWifiDisplayInternal();
1890 } finally {
1891 Binder.restoreCallingIdentity(token);
1892 }
1893 }
1894
1895 @Override // Binder call
1896 public WifiDisplayStatus getWifiDisplayStatus() {
1897 // This request does not require special permissions.
1898 // Any app can get information about available wifi displays.
1899
1900 final long token = Binder.clearCallingIdentity();
1901 try {
1902 return getWifiDisplayStatusInternal();
1903 } finally {
1904 Binder.restoreCallingIdentity(token);
1905 }
1906 }
1907
1908 @Override // Binder call
Michael Wright1c9977b2016-07-12 13:30:10 -07001909 public void requestColorMode(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +01001910 mContext.enforceCallingOrSelfPermission(
Michael Wright1c9977b2016-07-12 13:30:10 -07001911 Manifest.permission.CONFIGURE_DISPLAY_COLOR_MODE,
1912 "Permission required to change the display color mode");
Michael Wright58e829f2015-09-15 00:13:26 +01001913 final long token = Binder.clearCallingIdentity();
1914 try {
Michael Wright1c9977b2016-07-12 13:30:10 -07001915 requestColorModeInternal(displayId, colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +01001916 } finally {
1917 Binder.restoreCallingIdentity(token);
1918 }
1919 }
1920
1921 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001922 public int createVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wrightc39d47a2014-07-08 18:07:36 -07001923 IMediaProjection projection, String packageName, String name,
Santos Cordonee8931e2017-04-05 10:31:15 -07001924 int width, int height, int densityDpi, Surface surface, int flags,
1925 String uniqueId) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001926 final int callingUid = Binder.getCallingUid();
1927 if (!validatePackageName(callingUid, packageName)) {
1928 throw new SecurityException("packageName must match the calling uid");
1929 }
Michael Wright75ee9fc2014-09-01 19:55:22 -07001930 if (callback == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001931 throw new IllegalArgumentException("appToken must not be null");
1932 }
1933 if (TextUtils.isEmpty(name)) {
1934 throw new IllegalArgumentException("name must be non-null and non-empty");
1935 }
1936 if (width <= 0 || height <= 0 || densityDpi <= 0) {
1937 throw new IllegalArgumentException("width, height, and densityDpi must be "
1938 + "greater than 0");
1939 }
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001940 if (surface != null && surface.isSingleBuffered()) {
Pablo Ceballosaff2f942016-07-29 14:49:55 -07001941 throw new IllegalArgumentException("Surface can't be single-buffered");
1942 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001943
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001944 if ((flags & VIRTUAL_DISPLAY_FLAG_PUBLIC) != 0) {
1945 flags |= VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
1946
1947 // Public displays can't be allowed to show content when locked.
Andrii Kulian7211d2e2017-01-27 15:58:05 -08001948 if ((flags & VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD) != 0) {
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001949 throw new IllegalArgumentException(
1950 "Public display must not be marked as SHOW_WHEN_LOCKED_INSECURE");
1951 }
Michael Wright6720be42014-07-29 19:14:16 -07001952 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001953 if ((flags & VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY) != 0) {
1954 flags &= ~VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
Michael Wright6720be42014-07-29 19:14:16 -07001955 }
1956
Michael Wrightc39d47a2014-07-08 18:07:36 -07001957 if (projection != null) {
1958 try {
1959 if (!getProjectionService().isValidMediaProjection(projection)) {
1960 throw new SecurityException("Invalid media projection");
1961 }
Michael Wright6720be42014-07-29 19:14:16 -07001962 flags = projection.applyVirtualDisplayFlags(flags);
Michael Wrightc39d47a2014-07-08 18:07:36 -07001963 } catch (RemoteException e) {
Michael Wright6720be42014-07-29 19:14:16 -07001964 throw new SecurityException("unable to validate media projection or flags");
Michael Wrightc39d47a2014-07-08 18:07:36 -07001965 }
1966 }
1967
Michael Wright6720be42014-07-29 19:14:16 -07001968 if (callingUid != Process.SYSTEM_UID &&
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001969 (flags & VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001970 if (!canProjectVideo(projection)) {
1971 throw new SecurityException("Requires CAPTURE_VIDEO_OUTPUT or "
1972 + "CAPTURE_SECURE_VIDEO_OUTPUT permission, or an appropriate "
1973 + "MediaProjection token in order to create a screen sharing virtual "
1974 + "display.");
1975 }
1976 }
Santos Cordonb6992f22018-01-30 14:51:20 -08001977 if (callingUid != Process.SYSTEM_UID && (flags & VIRTUAL_DISPLAY_FLAG_SECURE) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001978 if (!canProjectSecureVideo(projection)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001979 throw new SecurityException("Requires CAPTURE_SECURE_VIDEO_OUTPUT "
Michael Wrightc39d47a2014-07-08 18:07:36 -07001980 + "or an appropriate MediaProjection token to create a "
1981 + "secure virtual display.");
Jeff Brown4ccb8232014-01-16 22:16:42 -08001982 }
1983 }
1984
Chilun67a379b2019-04-11 19:49:42 +08001985 // Sometimes users can have sensitive information in system decoration windows. An app
1986 // could create a virtual display with system decorations support and read the user info
1987 // from the surface.
1988 // We should only allow adding flag VIRTUAL_DISPLAY_FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS
1989 // to virtual displays that are owned by the system.
1990 if (callingUid != Process.SYSTEM_UID
1991 && (flags & VIRTUAL_DISPLAY_FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS) != 0) {
1992 if (!checkCallingPermission(INTERNAL_SYSTEM_WINDOW, "createVirtualDisplay()")) {
1993 throw new SecurityException("Requires INTERNAL_SYSTEM_WINDOW permission");
1994 }
1995 }
1996
Jeff Brown4ccb8232014-01-16 22:16:42 -08001997 final long token = Binder.clearCallingIdentity();
1998 try {
Santos Cordonee8931e2017-04-05 10:31:15 -07001999 return createVirtualDisplayInternal(callback, projection, callingUid, packageName,
2000 name, width, height, densityDpi, surface, flags, uniqueId);
Jeff Brown4ccb8232014-01-16 22:16:42 -08002001 } finally {
2002 Binder.restoreCallingIdentity(token);
2003 }
2004 }
2005
2006 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07002007 public void resizeVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wright01e840f2014-06-26 16:03:25 -07002008 int width, int height, int densityDpi) {
2009 final long token = Binder.clearCallingIdentity();
2010 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07002011 resizeVirtualDisplayInternal(callback.asBinder(), width, height, densityDpi);
Michael Wright01e840f2014-06-26 16:03:25 -07002012 } finally {
2013 Binder.restoreCallingIdentity(token);
2014 }
2015 }
2016
2017 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07002018 public void setVirtualDisplaySurface(IVirtualDisplayCallback callback, Surface surface) {
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07002019 if (surface != null && surface.isSingleBuffered()) {
2020 throw new IllegalArgumentException("Surface can't be single-buffered");
2021 }
Jeff Brown92207df2014-04-16 13:16:07 -07002022 final long token = Binder.clearCallingIdentity();
2023 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07002024 setVirtualDisplaySurfaceInternal(callback.asBinder(), surface);
Jeff Brown92207df2014-04-16 13:16:07 -07002025 } finally {
2026 Binder.restoreCallingIdentity(token);
2027 }
2028 }
2029
2030 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07002031 public void releaseVirtualDisplay(IVirtualDisplayCallback callback) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08002032 final long token = Binder.clearCallingIdentity();
2033 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07002034 releaseVirtualDisplayInternal(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -08002035 } finally {
2036 Binder.restoreCallingIdentity(token);
2037 }
2038 }
2039
2040 @Override // Binder call
chaviwda4c6942018-11-07 15:52:56 -08002041 public void setVirtualDisplayState(IVirtualDisplayCallback callback, boolean isOn) {
2042 final long token = Binder.clearCallingIdentity();
2043 try {
2044 setVirtualDisplayStateInternal(callback.asBinder(), isOn);
2045 } finally {
2046 Binder.restoreCallingIdentity(token);
2047 }
2048 }
2049
2050 @Override // Binder call
Jeff Brown4ccb8232014-01-16 22:16:42 -08002051 public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06002052 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
Jeff Brown4ccb8232014-01-16 22:16:42 -08002053
2054 final long token = Binder.clearCallingIdentity();
2055 try {
2056 dumpInternal(pw);
2057 } finally {
2058 Binder.restoreCallingIdentity(token);
2059 }
2060 }
2061
Kenny Guy22bd0442017-10-26 00:15:54 +01002062 @Override // Binder call
Kenny Guy29aa30e2017-11-30 13:43:46 +00002063 public ParceledListSlice<BrightnessChangeEvent> getBrightnessEvents(String callingPackage) {
Kenny Guy22bd0442017-10-26 00:15:54 +01002064 mContext.enforceCallingOrSelfPermission(
2065 Manifest.permission.BRIGHTNESS_SLIDER_USAGE,
2066 "Permission to read brightness events.");
Kenny Guy29aa30e2017-11-30 13:43:46 +00002067
2068 final int callingUid = Binder.getCallingUid();
2069 AppOpsManager appOpsManager = mContext.getSystemService(AppOpsManager.class);
Jeff Sharkeyd9311192018-04-16 16:50:34 +00002070 final int mode = appOpsManager.noteOp(AppOpsManager.OP_GET_USAGE_STATS,
Kenny Guy29aa30e2017-11-30 13:43:46 +00002071 callingUid, callingPackage);
2072 final boolean hasUsageStats;
2073 if (mode == AppOpsManager.MODE_DEFAULT) {
2074 // The default behavior here is to check if PackageManager has given the app
2075 // permission.
2076 hasUsageStats = mContext.checkCallingPermission(
2077 Manifest.permission.PACKAGE_USAGE_STATS)
2078 == PackageManager.PERMISSION_GRANTED;
2079 } else {
2080 hasUsageStats = mode == AppOpsManager.MODE_ALLOWED;
2081 }
2082
2083 final int userId = UserHandle.getUserId(callingUid);
Kenny Guy22bd0442017-10-26 00:15:54 +01002084 final long token = Binder.clearCallingIdentity();
2085 try {
Michael Wright144aac92017-12-21 18:37:41 +00002086 synchronized (mSyncRoot) {
2087 return mDisplayPowerController.getBrightnessEvents(userId, hasUsageStats);
2088 }
Kenny Guy22bd0442017-10-26 00:15:54 +01002089 } finally {
2090 Binder.restoreCallingIdentity(token);
2091 }
2092 }
2093
Michael Wrighteef0e132017-11-21 17:57:52 +00002094 @Override // Binder call
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002095 public ParceledListSlice<AmbientBrightnessDayStats> getAmbientBrightnessStats() {
2096 mContext.enforceCallingOrSelfPermission(
2097 Manifest.permission.ACCESS_AMBIENT_LIGHT_STATS,
2098 "Permission required to to access ambient light stats.");
2099 final int callingUid = Binder.getCallingUid();
2100 final int userId = UserHandle.getUserId(callingUid);
2101 final long token = Binder.clearCallingIdentity();
2102 try {
2103 synchronized (mSyncRoot) {
2104 return mDisplayPowerController.getAmbientBrightnessStats(userId);
2105 }
2106 } finally {
2107 Binder.restoreCallingIdentity(token);
2108 }
2109 }
2110
2111 @Override // Binder call
Michael Wrighteef0e132017-11-21 17:57:52 +00002112 public void setBrightnessConfigurationForUser(
Kenny Guy05ce8092018-01-17 13:44:20 +00002113 BrightnessConfiguration c, @UserIdInt int userId, String packageName) {
Michael Wrighteef0e132017-11-21 17:57:52 +00002114 mContext.enforceCallingOrSelfPermission(
2115 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
2116 "Permission required to change the display's brightness configuration");
2117 if (userId != UserHandle.getCallingUserId()) {
2118 mContext.enforceCallingOrSelfPermission(
2119 Manifest.permission.INTERACT_ACROSS_USERS,
2120 "Permission required to change the display brightness"
2121 + " configuration of another user");
2122 }
Kenny Guy05ce8092018-01-17 13:44:20 +00002123 if (packageName != null && !validatePackageName(getCallingUid(), packageName)) {
2124 packageName = null;
2125 }
Michael Wrighteef0e132017-11-21 17:57:52 +00002126 final long token = Binder.clearCallingIdentity();
2127 try {
Kenny Guy05ce8092018-01-17 13:44:20 +00002128 setBrightnessConfigurationForUserInternal(c, userId, packageName);
Michael Wrighteef0e132017-11-21 17:57:52 +00002129 } finally {
2130 Binder.restoreCallingIdentity(token);
2131 }
2132 }
2133
Michael Wrightd8460232018-01-16 18:04:59 +00002134 @Override // Binder call
Kenny Guy6d1009f2018-03-14 14:28:23 +00002135 public BrightnessConfiguration getBrightnessConfigurationForUser(int userId) {
2136 mContext.enforceCallingOrSelfPermission(
2137 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
2138 "Permission required to read the display's brightness configuration");
2139 if (userId != UserHandle.getCallingUserId()) {
2140 mContext.enforceCallingOrSelfPermission(
2141 Manifest.permission.INTERACT_ACROSS_USERS,
2142 "Permission required to read the display brightness"
2143 + " configuration of another user");
2144 }
2145 final long token = Binder.clearCallingIdentity();
2146 try {
2147 final int userSerial = getUserManager().getUserSerialNumber(userId);
2148 synchronized (mSyncRoot) {
2149 BrightnessConfiguration config =
2150 mPersistentDataStore.getBrightnessConfiguration(userSerial);
2151 if (config == null) {
2152 config = mDisplayPowerController.getDefaultBrightnessConfiguration();
2153 }
2154 return config;
2155 }
2156 } finally {
2157 Binder.restoreCallingIdentity(token);
2158 }
2159 }
2160
2161 @Override // Binder call
2162 public BrightnessConfiguration getDefaultBrightnessConfiguration() {
2163 mContext.enforceCallingOrSelfPermission(
2164 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
2165 "Permission required to read the display's default brightness configuration");
2166 final long token = Binder.clearCallingIdentity();
2167 try {
2168 synchronized (mSyncRoot) {
2169 return mDisplayPowerController.getDefaultBrightnessConfiguration();
2170 }
2171 } finally {
2172 Binder.restoreCallingIdentity(token);
2173 }
2174 }
2175
2176 @Override // Binder call
Michael Wrightd8460232018-01-16 18:04:59 +00002177 public void setTemporaryBrightness(int brightness) {
2178 mContext.enforceCallingOrSelfPermission(
2179 Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS,
2180 "Permission required to set the display's brightness");
2181 final long token = Binder.clearCallingIdentity();
2182 try {
2183 synchronized (mSyncRoot) {
2184 mDisplayPowerController.setTemporaryBrightness(brightness);
2185 }
2186 } finally {
2187 Binder.restoreCallingIdentity(token);
2188 }
2189 }
2190
2191 @Override // Binder call
2192 public void setTemporaryAutoBrightnessAdjustment(float adjustment) {
2193 mContext.enforceCallingOrSelfPermission(
2194 Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS,
2195 "Permission required to set the display's auto brightness adjustment");
2196 final long token = Binder.clearCallingIdentity();
2197 try {
2198 synchronized (mSyncRoot) {
2199 mDisplayPowerController.setTemporaryAutoBrightnessAdjustment(adjustment);
2200 }
2201 } finally {
2202 Binder.restoreCallingIdentity(token);
2203 }
2204 }
2205
Dan Gittik7a32fba2018-03-28 12:19:38 +01002206 @Override // Binder call
2207 public void onShellCommand(FileDescriptor in, FileDescriptor out,
2208 FileDescriptor err, String[] args, ShellCallback callback,
2209 ResultReceiver resultReceiver) {
2210 final long token = Binder.clearCallingIdentity();
2211 try {
2212 DisplayManagerShellCommand command = new DisplayManagerShellCommand(this);
2213 command.exec(this, in, out, err, args, callback, resultReceiver);
2214 } finally {
2215 Binder.restoreCallingIdentity(token);
2216 }
2217 }
2218
Dan Gittik122df862018-03-28 16:59:22 +01002219 @Override // Binder call
2220 public Curve getMinimumBrightnessCurve() {
2221 final long token = Binder.clearCallingIdentity();
2222 try {
2223 return getMinimumBrightnessCurveInternal();
2224 } finally {
2225 Binder.restoreCallingIdentity(token);
2226 }
2227 }
2228
Peiyong Lin277eaff2019-01-16 16:18:22 -08002229 @Override // Binder call
2230 public int getPreferredWideGamutColorSpaceId() {
2231 final long token = Binder.clearCallingIdentity();
2232 try {
2233 return getPreferredWideGamutColorSpaceIdInternal();
2234 } finally {
2235 Binder.restoreCallingIdentity(token);
2236 }
2237 }
2238
Dan Gittik7a32fba2018-03-28 12:19:38 +01002239 void setBrightness(int brightness) {
2240 Settings.System.putIntForUser(mContext.getContentResolver(),
2241 Settings.System.SCREEN_BRIGHTNESS, brightness, UserHandle.USER_CURRENT);
2242 }
2243
2244 void resetBrightnessConfiguration() {
2245 setBrightnessConfigurationForUserInternal(null, mContext.getUserId(),
2246 mContext.getPackageName());
2247 }
2248
Dan Gittika5a2d632019-01-09 14:25:29 +00002249 void setAutoBrightnessLoggingEnabled(boolean enabled) {
2250 if (mDisplayPowerController != null) {
2251 synchronized (mSyncRoot) {
2252 mDisplayPowerController.setAutoBrightnessLoggingEnabled(enabled);
2253 }
2254 }
2255 }
2256
Dan Gittik8dbd7e92018-12-03 15:35:53 +00002257 void setDisplayWhiteBalanceLoggingEnabled(boolean enabled) {
2258 if (mDisplayPowerController != null) {
2259 synchronized (mSyncRoot) {
2260 mDisplayPowerController.setDisplayWhiteBalanceLoggingEnabled(enabled);
2261 }
2262 }
2263 }
2264
2265 void setAmbientColorTemperatureOverride(float cct) {
2266 if (mDisplayPowerController != null) {
2267 synchronized (mSyncRoot) {
2268 mDisplayPowerController.setAmbientColorTemperatureOverride(cct);
2269 }
2270 }
2271 }
2272
Jeff Brown4ccb8232014-01-16 22:16:42 -08002273 private boolean validatePackageName(int uid, String packageName) {
2274 if (packageName != null) {
2275 String[] packageNames = mContext.getPackageManager().getPackagesForUid(uid);
2276 if (packageNames != null) {
2277 for (String n : packageNames) {
2278 if (n.equals(packageName)) {
2279 return true;
2280 }
2281 }
2282 }
2283 }
2284 return false;
2285 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07002286
2287 private boolean canProjectVideo(IMediaProjection projection) {
2288 if (projection != null) {
2289 try {
2290 if (projection.canProjectVideo()) {
2291 return true;
2292 }
2293 } catch (RemoteException e) {
2294 Slog.e(TAG, "Unable to query projection service for permissions", e);
2295 }
2296 }
Chilun67a379b2019-04-11 19:49:42 +08002297 if (checkCallingPermission(CAPTURE_VIDEO_OUTPUT, "canProjectVideo()")) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07002298 return true;
2299 }
2300 return canProjectSecureVideo(projection);
2301 }
2302
2303 private boolean canProjectSecureVideo(IMediaProjection projection) {
2304 if (projection != null) {
2305 try {
2306 if (projection.canProjectSecureVideo()){
2307 return true;
2308 }
2309 } catch (RemoteException e) {
2310 Slog.e(TAG, "Unable to query projection service for permissions", e);
2311 }
2312 }
Chilun67a379b2019-04-11 19:49:42 +08002313 return checkCallingPermission(CAPTURE_SECURE_VIDEO_OUTPUT, "canProjectSecureVideo()");
2314 }
2315
2316 private boolean checkCallingPermission(String permission, String func) {
2317 if (mContext.checkCallingPermission(permission) == PackageManager.PERMISSION_GRANTED) {
2318 return true;
2319 }
2320 final String msg = "Permission Denial: " + func + " from pid=" + Binder.getCallingPid()
2321 + ", uid=" + Binder.getCallingUid() + " requires " + permission;
2322 Slog.w(TAG, msg);
2323 return false;
Michael Wrightc39d47a2014-07-08 18:07:36 -07002324 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08002325 }
2326
2327 private final class LocalService extends DisplayManagerInternal {
2328 @Override
Jeff Brown037c33e2014-04-09 00:31:55 -07002329 public void initPowerManagement(final DisplayPowerCallbacks callbacks, Handler handler,
Jeff Brownad9ef192014-04-08 17:26:30 -07002330 SensorManager sensorManager) {
2331 synchronized (mSyncRoot) {
Jeff Brown037c33e2014-04-09 00:31:55 -07002332 DisplayBlanker blanker = new DisplayBlanker() {
2333 @Override
Jeff Brown5d6443b2015-04-10 20:15:01 -07002334 public void requestDisplayState(int state, int brightness) {
Jeff Brown037c33e2014-04-09 00:31:55 -07002335 // The order of operations is important for legacy reasons.
2336 if (state == Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07002337 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07002338 }
2339
2340 callbacks.onDisplayStateChange(state);
2341
2342 if (state != Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07002343 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07002344 }
2345 }
2346 };
Jeff Brownad9ef192014-04-08 17:26:30 -07002347 mDisplayPowerController = new DisplayPowerController(
Jeff Brown037c33e2014-04-09 00:31:55 -07002348 mContext, callbacks, handler, sensorManager, blanker);
Jeff Brownad9ef192014-04-08 17:26:30 -07002349 }
Michael Wrighteef0e132017-11-21 17:57:52 +00002350
2351 mHandler.sendEmptyMessage(MSG_LOAD_BRIGHTNESS_CONFIGURATION);
Jeff Brownad9ef192014-04-08 17:26:30 -07002352 }
2353
2354 @Override
2355 public boolean requestPowerState(DisplayPowerRequest request,
2356 boolean waitForNegativeProximity) {
Michael Wrighteef0e132017-11-21 17:57:52 +00002357 synchronized (mSyncRoot) {
2358 return mDisplayPowerController.requestPowerState(request,
2359 waitForNegativeProximity);
2360 }
Jeff Brownad9ef192014-04-08 17:26:30 -07002361 }
2362
2363 @Override
2364 public boolean isProximitySensorAvailable() {
Michael Wrighteef0e132017-11-21 17:57:52 +00002365 synchronized (mSyncRoot) {
2366 return mDisplayPowerController.isProximitySensorAvailable();
2367 }
Jeff Brownad9ef192014-04-08 17:26:30 -07002368 }
2369
2370 @Override
Robert Carr66b5664f2019-04-02 14:18:56 -07002371 public SurfaceControl.ScreenshotGraphicBuffer screenshot(int displayId) {
2372 return screenshotInternal(displayId);
Riddle Hsu654a6f92018-07-13 22:59:36 +08002373 }
2374
2375 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08002376 public DisplayInfo getDisplayInfo(int displayId) {
2377 return getDisplayInfoInternal(displayId, Process.myUid());
2378 }
2379
2380 @Override
2381 public void registerDisplayTransactionListener(DisplayTransactionListener listener) {
2382 if (listener == null) {
2383 throw new IllegalArgumentException("listener must not be null");
2384 }
2385
2386 registerDisplayTransactionListenerInternal(listener);
2387 }
2388
2389 @Override
2390 public void unregisterDisplayTransactionListener(DisplayTransactionListener listener) {
2391 if (listener == null) {
2392 throw new IllegalArgumentException("listener must not be null");
2393 }
2394
2395 unregisterDisplayTransactionListenerInternal(listener);
2396 }
2397
2398 @Override
2399 public void setDisplayInfoOverrideFromWindowManager(int displayId, DisplayInfo info) {
2400 setDisplayInfoOverrideFromWindowManagerInternal(displayId, info);
2401 }
2402
2403 @Override
Andrii Kuliancd097992017-03-23 18:31:59 -07002404 public void getNonOverrideDisplayInfo(int displayId, DisplayInfo outInfo) {
2405 getNonOverrideDisplayInfoInternal(displayId, outInfo);
2406 }
2407
2408 @Override
Robert Carrae606b42018-02-15 15:36:23 -08002409 public void performTraversal(SurfaceControl.Transaction t) {
2410 performTraversalInternal(t);
Jeff Brown4ccb8232014-01-16 22:16:42 -08002411 }
2412
2413 @Override
Michael Wright3f145a22014-07-22 19:46:03 -07002414 public void setDisplayProperties(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07002415 float requestedRefreshRate, int requestedMode, boolean inTraversal) {
2416 setDisplayPropertiesInternal(displayId, hasContent, requestedRefreshRate,
2417 requestedMode, inTraversal);
Jeff Brown4ccb8232014-01-16 22:16:42 -08002418 }
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08002419
2420 @Override
2421 public void setDisplayOffsets(int displayId, int x, int y) {
2422 setDisplayOffsetsInternal(displayId, x, y);
2423 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002424
2425 @Override
Sam Lin4c3ac2b2019-02-18 04:50:26 -08002426 public void setDisplayScalingDisabled(int displayId, boolean disableScaling) {
2427 setDisplayScalingDisabledInternal(displayId, disableScaling);
2428 }
2429
2430 @Override
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002431 public void setDisplayAccessUIDs(SparseArray<IntArray> newDisplayAccessUIDs) {
2432 setDisplayAccessUIDsInternal(newDisplayAccessUIDs);
2433 }
2434
2435 @Override
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002436 public void persistBrightnessTrackerState() {
Michael Wright144aac92017-12-21 18:37:41 +00002437 synchronized (mSyncRoot) {
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002438 mDisplayPowerController.persistBrightnessTrackerState();
Michael Wright144aac92017-12-21 18:37:41 +00002439 }
Kenny Guycfe7b702017-11-14 21:04:58 +00002440 }
Adrian Roose1d68cd2018-01-17 12:54:50 +01002441
2442 @Override
2443 public void onOverlayChanged() {
2444 synchronized (mSyncRoot) {
Adrian Roos898ec382018-01-17 12:54:50 +01002445 for (int i = 0; i < mDisplayDevices.size(); i++) {
2446 mDisplayDevices.get(i).onOverlayChangedLocked();
Adrian Roose1d68cd2018-01-17 12:54:50 +01002447 }
2448 }
2449 }
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07002450
2451 @Override
2452 public DisplayedContentSamplingAttributes getDisplayedContentSamplingAttributes(
2453 int displayId) {
2454 return getDisplayedContentSamplingAttributesInternal(displayId);
2455 }
2456
2457 @Override
2458 public boolean setDisplayedContentSamplingEnabled(
2459 int displayId, boolean enable, int componentMask, int maxFrames) {
2460 return setDisplayedContentSamplingEnabledInternal(
2461 displayId, enable, componentMask, maxFrames);
2462 }
2463
2464 @Override
2465 public DisplayedContentSample getDisplayedContentSample(int displayId,
2466 long maxFrames, long timestamp) {
2467 return getDisplayedContentSampleInternal(displayId, maxFrames, timestamp);
2468 }
2469
Jeff Brown4ccb8232014-01-16 22:16:42 -08002470 }
Michael Wrighta3dab232019-02-22 16:54:21 +00002471
2472 class AllowedDisplayModeObserver implements DisplayModeDirector.Listener {
2473 public void onAllowedDisplayModesChanged() {
2474 onAllowedDisplayModesChangedInternal();
2475 }
2476 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07002477}