blob: 2f5395119c9d6d8c14c92107a2b708de6f10c8e7 [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
Andrii Kulianfc8f82b2017-01-26 13:17:27 -080019import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +010020import static android.hardware.display.DisplayManager
21 .VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD;
Andrii Kulianfc8f82b2017-01-26 13:17:27 -080022import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY;
23import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;
24import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_SECURE;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +010025import static android.hardware.display.DisplayViewport.VIEWPORT_EXTERNAL;
26import static android.hardware.display.DisplayViewport.VIEWPORT_INTERNAL;
27import static android.hardware.display.DisplayViewport.VIEWPORT_VIRTUAL;
Jeff Brownbd6e1502012-08-28 03:27:37 -070028
Jeff Brownfa25bf52012-07-23 19:26:30 -070029import android.Manifest;
Santos Cordonee8931e2017-04-05 10:31:15 -070030import android.annotation.NonNull;
Kenny Guy05ce8092018-01-17 13:44:20 +000031import android.annotation.Nullable;
Michael Wrighteef0e132017-11-21 17:57:52 +000032import android.annotation.UserIdInt;
Kenny Guy29aa30e2017-11-30 13:43:46 +000033import android.app.AppOpsManager;
Jeff Brownfa25bf52012-07-23 19:26:30 -070034import android.content.Context;
35import android.content.pm.PackageManager;
Kenny Guy22bd0442017-10-26 00:15:54 +010036import android.content.pm.ParceledListSlice;
Michael Wrighteedcbf12017-08-16 23:14:54 +010037import android.content.res.Resources;
Dan Gittik122df862018-03-28 16:59:22 +010038import android.content.res.TypedArray;
Peiyong Lin277eaff2019-01-16 16:18:22 -080039import android.graphics.ColorSpace;
Robert Carr5c52b132019-02-15 15:48:11 -080040import android.graphics.GraphicBuffer;
Michael Wrighteedcbf12017-08-16 23:14:54 +010041import android.graphics.Point;
Robert Carr5c52b132019-02-15 15:48:11 -080042import android.graphics.Rect;
Jeff Brownad9ef192014-04-08 17:26:30 -070043import android.hardware.SensorManager;
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +000044import android.hardware.display.AmbientBrightnessDayStats;
Kenny Guy22bd0442017-10-26 00:15:54 +010045import android.hardware.display.BrightnessChangeEvent;
Michael Wrighteef0e132017-11-21 17:57:52 +000046import android.hardware.display.BrightnessConfiguration;
Dan Gittik122df862018-03-28 16:59:22 +010047import android.hardware.display.Curve;
Jeff Brownbd6e1502012-08-28 03:27:37 -070048import android.hardware.display.DisplayManagerGlobal;
Jeff Brown4ccb8232014-01-16 22:16:42 -080049import android.hardware.display.DisplayManagerInternal;
Jeff Brown4ccb8232014-01-16 22:16:42 -080050import android.hardware.display.DisplayManagerInternal.DisplayTransactionListener;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +010051import android.hardware.display.DisplayViewport;
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -070052import android.hardware.display.DisplayedContentSample;
53import android.hardware.display.DisplayedContentSamplingAttributes;
Jeff Brownfa25bf52012-07-23 19:26:30 -070054import android.hardware.display.IDisplayManager;
Jeff Brownbd6e1502012-08-28 03:27:37 -070055import android.hardware.display.IDisplayManagerCallback;
Michael Wright75ee9fc2014-09-01 19:55:22 -070056import android.hardware.display.IVirtualDisplayCallback;
Jeff Browne08ae382012-09-07 20:36:36 -070057import android.hardware.display.WifiDisplayStatus;
Jeff Brown4ccb8232014-01-16 22:16:42 -080058import android.hardware.input.InputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -070059import android.media.projection.IMediaProjection;
60import android.media.projection.IMediaProjectionManager;
Jeff Brownfa25bf52012-07-23 19:26:30 -070061import android.os.Binder;
Jeff Brownbd6e1502012-08-28 03:27:37 -070062import android.os.Handler;
Jeff Brown64a55af2012-08-26 02:47:39 -070063import android.os.IBinder;
Jeff Brown4ccb8232014-01-16 22:16:42 -080064import android.os.IBinder.DeathRecipient;
Jeff Brownbd6e1502012-08-28 03:27:37 -070065import android.os.Looper;
66import android.os.Message;
Jeff Brown5d6443b2015-04-10 20:15:01 -070067import android.os.PowerManager;
Craig Mautner4504de52013-12-20 09:06:56 -080068import android.os.Process;
Jeff Brownbd6e1502012-08-28 03:27:37 -070069import android.os.RemoteException;
Dan Gittik7a32fba2018-03-28 12:19:38 +010070import android.os.ResultReceiver;
Michael Wrightc39d47a2014-07-08 18:07:36 -070071import android.os.ServiceManager;
Dan Gittik7a32fba2018-03-28 12:19:38 +010072import android.os.ShellCallback;
Jeff Brownbd6e1502012-08-28 03:27:37 -070073import android.os.SystemClock;
Jeff Brownfa25bf52012-07-23 19:26:30 -070074import android.os.SystemProperties;
Jeff Brown5d6443b2015-04-10 20:15:01 -070075import android.os.Trace;
Kenny Guy22bd0442017-10-26 00:15:54 +010076import android.os.UserHandle;
Michael Wrighteef0e132017-11-21 17:57:52 +000077import android.os.UserManager;
Dan Gittik7a32fba2018-03-28 12:19:38 +010078import android.provider.Settings;
Jeff Browna506a6e2013-06-04 00:02:38 -070079import android.text.TextUtils;
Andrii Kulianfb1bf692017-01-17 11:17:34 -080080import android.util.IntArray;
Dan Gittik122df862018-03-28 16:59:22 +010081import android.util.Pair;
Jeff Brownbd6e1502012-08-28 03:27:37 -070082import android.util.Slog;
83import android.util.SparseArray;
Dan Gittik122df862018-03-28 16:59:22 +010084import android.util.Spline;
Jeff Brownfa25bf52012-07-23 19:26:30 -070085import android.view.Display;
86import android.view.DisplayInfo;
Jeff Browna506a6e2013-06-04 00:02:38 -070087import android.view.Surface;
Robert Carrae606b42018-02-15 15:36:23 -080088import android.view.SurfaceControl;
Jeff Browna506a6e2013-06-04 00:02:38 -070089
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +010090import com.android.internal.annotations.GuardedBy;
91import com.android.internal.annotations.VisibleForTesting;
92import com.android.internal.util.DumpUtils;
93import com.android.internal.util.IndentingPrintWriter;
Jorim Jaggied7993b2017-03-28 18:50:01 +010094import com.android.server.AnimationThread;
Jeff Brown4ccb8232014-01-16 22:16:42 -080095import com.android.server.DisplayThread;
96import com.android.server.LocalServices;
97import com.android.server.SystemService;
Dianne Hackborn8d044e82013-04-30 17:24:15 -070098import com.android.server.UiThread;
Jorim Jaggi21c39a72017-10-20 15:47:51 +020099import com.android.server.wm.SurfaceAnimationThread;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100100import com.android.server.wm.WindowManagerInternal;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700101
102import java.io.FileDescriptor;
103import java.io.PrintWriter;
104import java.util.ArrayList;
Jeff Browna506a6e2013-06-04 00:02:38 -0700105import java.util.Arrays;
Jeff Browne75926d2014-09-18 15:24:49 -0700106import java.util.List;
Jeff Brown7f3994e2012-12-04 14:04:28 -0800107import java.util.concurrent.CopyOnWriteArrayList;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700108
109/**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700110 * Manages attached displays.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700111 * <p>
Jeff Brownbd6e1502012-08-28 03:27:37 -0700112 * The {@link DisplayManagerService} manages the global lifecycle of displays,
113 * decides how to configure logical displays based on the physical display devices currently
114 * attached, sends notifications to the system and to applications when the state
115 * changes, and so on.
116 * </p><p>
117 * The display manager service relies on a collection of {@link DisplayAdapter} components,
118 * for discovering and configuring physical display devices attached to the system.
119 * There are separate display adapters for each manner that devices are attached:
120 * one display adapter for built-in local displays, one for simulated non-functional
121 * displays when the system is headless, one for simulated overlay displays used for
122 * development, one for wifi displays, etc.
123 * </p><p>
124 * Display adapters are only weakly coupled to the display manager service.
125 * Display adapters communicate changes in display device state to the display manager
Craig Mautner722285e2012-09-07 13:55:58 -0700126 * service asynchronously via a {@link DisplayAdapter.Listener} registered
Jeff Brownbd6e1502012-08-28 03:27:37 -0700127 * by the display manager service. This separation of concerns is important for
128 * two main reasons. First, it neatly encapsulates the responsibilities of these
129 * two classes: display adapters handle individual display devices whereas
130 * the display manager service handles the global state. Second, it eliminates
131 * the potential for deadlocks resulting from asynchronous display device discovery.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700132 * </p>
133 *
134 * <h3>Synchronization</h3>
135 * <p>
136 * Because the display manager may be accessed by multiple threads, the synchronization
137 * story gets a little complicated. In particular, the window manager may call into
138 * the display manager while holding a surface transaction with the expectation that
139 * it can apply changes immediately. Unfortunately, that means we can't just do
140 * everything asynchronously (*grump*).
Jeff Brownbd6e1502012-08-28 03:27:37 -0700141 * </p><p>
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700142 * To make this work, all of the objects that belong to the display manager must
143 * use the same lock. We call this lock the synchronization root and it has a unique
144 * type {@link DisplayManagerService.SyncRoot}. Methods that require this lock are
145 * named with the "Locked" suffix.
146 * </p><p>
147 * Where things get tricky is that the display manager is not allowed to make
148 * any potentially reentrant calls, especially into the window manager. We generally
149 * avoid this by making all potentially reentrant out-calls asynchronous.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700150 * </p>
151 */
Jeff Brown4ccb8232014-01-16 22:16:42 -0800152public final class DisplayManagerService extends SystemService {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700153 private static final String TAG = "DisplayManagerService";
Jeff Brownbd6e1502012-08-28 03:27:37 -0700154 private static final boolean DEBUG = false;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700155
Jeff Brownbbd28a22012-09-20 16:47:15 -0700156 // When this system property is set to 0, WFD is forcibly disabled on boot.
157 // When this system property is set to 1, WFD is forcibly enabled on boot.
158 // Otherwise WFD is enabled according to the value of config_enableWifiDisplay.
159 private static final String FORCE_WIFI_DISPLAY_ENABLE = "persist.debug.wfd.enable";
160
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200161 private static final String PROP_DEFAULT_DISPLAY_TOP_INSET = "persist.sys.displayinset.top";
162
Jeff Brownbd6e1502012-08-28 03:27:37 -0700163 private static final long WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT = 10000;
164
Santos Cordonc22c5632017-06-21 16:03:49 -0700165 private static final int MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS = 1;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700166 private static final int MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS = 2;
167 private static final int MSG_DELIVER_DISPLAY_EVENT = 3;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700168 private static final int MSG_REQUEST_TRAVERSAL = 4;
Jeff Brownd728bf52012-09-08 18:05:28 -0700169 private static final int MSG_UPDATE_VIEWPORT = 5;
Michael Wrighta3dab232019-02-22 16:54:21 +0000170 private static final int MSG_LOAD_BRIGHTNESS_CONFIGURATION = 6;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700171
Jeff Brownb880d882014-02-10 19:47:07 -0800172 private final Context mContext;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700173 private final DisplayManagerHandler mHandler;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700174 private final Handler mUiHandler;
175 private final DisplayAdapterListener mDisplayAdapterListener;
Michael Wrighta3dab232019-02-22 16:54:21 +0000176 private final DisplayModeDirector mDisplayModeDirector;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800177 private WindowManagerInternal mWindowManagerInternal;
178 private InputManagerInternal mInputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -0700179 private IMediaProjectionManager mProjectionService;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700180
181 // The synchronization root for the display manager.
182 // This lock guards most of the display manager's state.
Craig Mautner0bf6ec92012-12-18 08:33:27 -0800183 // NOTE: This is synchronized on while holding WindowManagerService.mWindowMap so never call
184 // into WindowManagerService methods that require mWindowMap while holding this unless you are
185 // very very sure that no deadlock can occur.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700186 private final SyncRoot mSyncRoot = new SyncRoot();
187
188 // True if in safe mode.
189 // This option may disable certain display adapters.
190 public boolean mSafeMode;
191
192 // True if we are in a special boot mode where only core applications and
193 // services should be started. This option may disable certain display adapters.
194 public boolean mOnlyCore;
195
Jeff Brown27f1d672012-10-17 18:32:34 -0700196 // True if the display manager service should pretend there is only one display
197 // and only tell applications about the existence of the default logical display.
198 // The display manager can still mirror content to secondary displays but applications
199 // cannot present unique content on those displays.
200 // Used for demonstration purposes only.
201 private final boolean mSingleDisplayDemoMode;
202
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700203 // All callback records indexed by calling process id.
204 public final SparseArray<CallbackRecord> mCallbacks =
Jeff Brownbd6e1502012-08-28 03:27:37 -0700205 new SparseArray<CallbackRecord>();
Jeff Brownfa25bf52012-07-23 19:26:30 -0700206
Jeff Brownbd6e1502012-08-28 03:27:37 -0700207 // List of all currently registered display adapters.
208 private final ArrayList<DisplayAdapter> mDisplayAdapters = new ArrayList<DisplayAdapter>();
209
210 // List of all currently connected display devices.
211 private final ArrayList<DisplayDevice> mDisplayDevices = new ArrayList<DisplayDevice>();
212
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700213 // List of all logical displays indexed by logical display id.
214 private final SparseArray<LogicalDisplay> mLogicalDisplays =
215 new SparseArray<LogicalDisplay>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700216 private int mNextNonDefaultDisplayId = Display.DEFAULT_DISPLAY + 1;
217
Jeff Brown7f3994e2012-12-04 14:04:28 -0800218 // List of all display transaction listeners.
219 private final CopyOnWriteArrayList<DisplayTransactionListener> mDisplayTransactionListeners =
220 new CopyOnWriteArrayList<DisplayTransactionListener>();
221
Jeff Brownad9ef192014-04-08 17:26:30 -0700222 // Display power controller.
223 private DisplayPowerController mDisplayPowerController;
224
Jeff Brown037c33e2014-04-09 00:31:55 -0700225 // The overall display state, independent of changes that might influence one
226 // display or another in particular.
Jeff Brown5d6443b2015-04-10 20:15:01 -0700227 private int mGlobalDisplayState = Display.STATE_ON;
228
229 // The overall display brightness.
230 // For now, this only applies to the built-in display but we may split it up eventually.
231 private int mGlobalDisplayBrightness = PowerManager.BRIGHTNESS_DEFAULT;
Jeff Brown9e316a12012-10-08 19:17:06 -0700232
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700233 // Set to true when there are pending display changes that have yet to be applied
234 // to the surface flinger state.
235 private boolean mPendingTraversal;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700236
Jeff Browne08ae382012-09-07 20:36:36 -0700237 // The Wifi display adapter, or null if not registered.
238 private WifiDisplayAdapter mWifiDisplayAdapter;
239
Jeff Brownce468a32013-11-21 16:42:03 -0800240 // The number of active wifi display scan requests.
241 private int mWifiDisplayScanRequestCount;
242
Jeff Browna506a6e2013-06-04 00:02:38 -0700243 // The virtual display adapter, or null if not registered.
244 private VirtualDisplayAdapter mVirtualDisplayAdapter;
245
Michael Wrighteef0e132017-11-21 17:57:52 +0000246 // The User ID of the current user
247 private @UserIdInt int mCurrentUserId;
248
Michael Wrighteedcbf12017-08-16 23:14:54 +0100249 // The stable device screen height and width. These are not tied to a specific display, even
250 // the default display, because they need to be stable over the course of the device's entire
251 // life, even if the default display changes (e.g. a new monitor is plugged into a PC-like
252 // device).
253 private Point mStableDisplaySize = new Point();
254
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200255 // The top inset of the default display.
256 // This gets persisted so that the boot animation knows how to transition from the display's
257 // full size to the size configured by the user. Right now we only persist and animate the top
258 // inset, but theoretically we could do it for all of them.
259 private int mDefaultDisplayTopInset;
260
Jeff Brownd728bf52012-09-08 18:05:28 -0700261 // Viewports of the default display and the display that should receive touch
262 // input from an external source. Used by the input system.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100263 @GuardedBy("mSyncRoot")
264 private final ArrayList<DisplayViewport> mViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700265
Jeff Brown89d55462012-09-19 11:33:42 -0700266 // Persistent data store for all internal settings maintained by the display manager service.
267 private final PersistentDataStore mPersistentDataStore = new PersistentDataStore();
268
Jeff Brownbd6e1502012-08-28 03:27:37 -0700269 // Temporary callback list, used when sending display events to applications.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700270 // May be used outside of the lock but only on the handler thread.
271 private final ArrayList<CallbackRecord> mTempCallbacks = new ArrayList<CallbackRecord>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700272
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700273 // Temporary display info, used for comparing display configurations.
274 private final DisplayInfo mTempDisplayInfo = new DisplayInfo();
275
Jeff Brownd728bf52012-09-08 18:05:28 -0700276 // Temporary viewports, used when sending new viewport information to the
277 // input system. May be used outside of the lock but only on the handler thread.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100278 private final ArrayList<DisplayViewport> mTempViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700279
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700280 // The default color mode for default displays. Overrides the usual
281 // Display.Display.COLOR_MODE_DEFAULT for displays with the
282 // DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY flag set.
283 private final int mDefaultDisplayDefaultColorMode;
284
Jeff Browne75926d2014-09-18 15:24:49 -0700285 // Temporary list of deferred work to perform when setting the display state.
286 // Only used by requestDisplayState. The field is self-synchronized and only
287 // intended for use inside of the requestGlobalDisplayStateInternal function.
288 private final ArrayList<Runnable> mTempDisplayStateWorkQueue = new ArrayList<Runnable>();
289
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800290 // Lists of UIDs that are present on the displays. Maps displayId -> array of UIDs.
291 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
292
Santos Cordonee8931e2017-04-05 10:31:15 -0700293 private final Injector mInjector;
294
Dan Gittik122df862018-03-28 16:59:22 +0100295 // The minimum brightness curve, which guarantess that any brightness curve that dips below it
296 // is rejected by the system.
297 private final Curve mMinimumBrightnessCurve;
298 private final Spline mMinimumBrightnessSpline;
Peiyong Lin277eaff2019-01-16 16:18:22 -0800299 private final ColorSpace mWideColorSpace;
Dan Gittik122df862018-03-28 16:59:22 +0100300
Jeff Brownb880d882014-02-10 19:47:07 -0800301 public DisplayManagerService(Context context) {
Santos Cordonee8931e2017-04-05 10:31:15 -0700302 this(context, new Injector());
303 }
304
305 @VisibleForTesting
306 DisplayManagerService(Context context, Injector injector) {
Jeff Brownb880d882014-02-10 19:47:07 -0800307 super(context);
Santos Cordonee8931e2017-04-05 10:31:15 -0700308 mInjector = injector;
Jeff Brownb880d882014-02-10 19:47:07 -0800309 mContext = context;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800310 mHandler = new DisplayManagerHandler(DisplayThread.get().getLooper());
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700311 mUiHandler = UiThread.getHandler();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700312 mDisplayAdapterListener = new DisplayAdapterListener();
Michael Wrighta3dab232019-02-22 16:54:21 +0000313 mDisplayModeDirector = new DisplayModeDirector(context, mHandler);
Jeff Brown27f1d672012-10-17 18:32:34 -0700314 mSingleDisplayDemoMode = SystemProperties.getBoolean("persist.demo.singledisplay", false);
Dan Gittik122df862018-03-28 16:59:22 +0100315 Resources resources = mContext.getResources();
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700316 mDefaultDisplayDefaultColorMode = mContext.getResources().getInteger(
Michael Wrighteef0e132017-11-21 17:57:52 +0000317 com.android.internal.R.integer.config_defaultDisplayDefaultColorMode);
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200318 mDefaultDisplayTopInset = SystemProperties.getInt(PROP_DEFAULT_DISPLAY_TOP_INSET, -1);
Dan Gittik122df862018-03-28 16:59:22 +0100319 float[] lux = getFloatArray(resources.obtainTypedArray(
320 com.android.internal.R.array.config_minimumBrightnessCurveLux));
321 float[] nits = getFloatArray(resources.obtainTypedArray(
322 com.android.internal.R.array.config_minimumBrightnessCurveNits));
323 mMinimumBrightnessCurve = new Curve(lux, nits);
324 mMinimumBrightnessSpline = Spline.createSpline(lux, nits);
Jeff Brown5d6443b2015-04-10 20:15:01 -0700325
Michael Wrighta3dab232019-02-22 16:54:21 +0000326 PowerManager pm = mContext.getSystemService(PowerManager.class);
Jeff Brown5d6443b2015-04-10 20:15:01 -0700327 mGlobalDisplayBrightness = pm.getDefaultScreenBrightnessSetting();
Michael Wrighteef0e132017-11-21 17:57:52 +0000328 mCurrentUserId = UserHandle.USER_SYSTEM;
Peiyong Lin277eaff2019-01-16 16:18:22 -0800329 ColorSpace[] colorSpaces = SurfaceControl.getCompositionColorSpaces();
330 mWideColorSpace = colorSpaces[1];
Joel Fernandes2d314e12017-04-04 16:32:15 -0700331 }
332
333 public void setupSchedulerPolicies() {
Jorim Jaggied7993b2017-03-28 18:50:01 +0100334 // android.display and android.anim is critical to user experience and we should make sure
Michael Wrighteef0e132017-11-21 17:57:52 +0000335 // it is not in the default foregroup groups, add it to top-app to make sure it uses all
336 // the cores and scheduling settings for top-app when it runs.
Jorim Jaggied7993b2017-03-28 18:50:01 +0100337 Process.setThreadGroupAndCpuset(DisplayThread.get().getThreadId(),
338 Process.THREAD_GROUP_TOP_APP);
339 Process.setThreadGroupAndCpuset(AnimationThread.get().getThreadId(),
340 Process.THREAD_GROUP_TOP_APP);
Jorim Jaggi21c39a72017-10-20 15:47:51 +0200341 Process.setThreadGroupAndCpuset(SurfaceAnimationThread.get().getThreadId(),
342 Process.THREAD_GROUP_TOP_APP);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700343 }
344
Jeff Brown4ccb8232014-01-16 22:16:42 -0800345 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -0800346 public void onStart() {
Michael Wright1c9977b2016-07-12 13:30:10 -0700347 // We need to pre-load the persistent data store so it's ready before the default display
348 // adapter is up so that we have it's configuration. We could load it lazily, but since
349 // 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 -0700350 // we've waited for the display to register itself with us.
Michael Wrighta3dab232019-02-22 16:54:21 +0000351 synchronized (mSyncRoot) {
352 mPersistentDataStore.loadIfNeeded();
353 loadStableDisplayValuesLocked();
Michael Wrighteedcbf12017-08-16 23:14:54 +0100354 }
Santos Cordonc22c5632017-06-21 16:03:49 -0700355 mHandler.sendEmptyMessage(MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800356
357 publishBinderService(Context.DISPLAY_SERVICE, new BinderService(),
358 true /*allowIsolated*/);
359 publishLocalService(DisplayManagerInternal.class, new LocalService());
360 }
361
362 @Override
363 public void onBootPhase(int phase) {
364 if (phase == PHASE_WAIT_FOR_DEFAULT_DISPLAY) {
365 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700366 long timeout = SystemClock.uptimeMillis()
367 + mInjector.getDefaultDisplayDelayTimeout();
368 while (mLogicalDisplays.get(Display.DEFAULT_DISPLAY) == null ||
369 mVirtualDisplayAdapter == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800370 long delay = timeout - SystemClock.uptimeMillis();
371 if (delay <= 0) {
372 throw new RuntimeException("Timeout waiting for default display "
Santos Cordonc22c5632017-06-21 16:03:49 -0700373 + "to be initialized. DefaultDisplay="
374 + mLogicalDisplays.get(Display.DEFAULT_DISPLAY)
375 + ", mVirtualDisplayAdapter=" + mVirtualDisplayAdapter);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800376 }
377 if (DEBUG) {
378 Slog.d(TAG, "waitForDefaultDisplay: waiting, timeout=" + delay);
379 }
380 try {
381 mSyncRoot.wait(delay);
382 } catch (InterruptedException ex) {
383 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700384 }
385 }
386 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700387 }
388
Michael Wrighteef0e132017-11-21 17:57:52 +0000389 @Override
390 public void onSwitchUser(@UserIdInt int newUserId) {
391 final int userSerial = getUserManager().getUserSerialNumber(newUserId);
392 synchronized (mSyncRoot) {
393 if (mCurrentUserId != newUserId) {
394 mCurrentUserId = newUserId;
395 BrightnessConfiguration config =
396 mPersistentDataStore.getBrightnessConfiguration(userSerial);
397 mDisplayPowerController.setBrightnessConfiguration(config);
398 }
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +0000399 mDisplayPowerController.onSwitchUser(newUserId);
Michael Wrighteef0e132017-11-21 17:57:52 +0000400 }
401 }
402
Jeff Brown4ccb8232014-01-16 22:16:42 -0800403 // TODO: Use dependencies or a boot phase
404 public void windowManagerAndInputReady() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700405 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800406 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
407 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner65d11b32012-10-01 13:59:52 -0700408 scheduleTraversalLocked(false);
Jeff Brownd728bf52012-09-08 18:05:28 -0700409 }
410 }
411
412 /**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700413 * Called when the system is ready to go.
414 */
415 public void systemReady(boolean safeMode, boolean onlyCore) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700416 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700417 mSafeMode = safeMode;
418 mOnlyCore = onlyCore;
419 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700420
Michael Wrighta3dab232019-02-22 16:54:21 +0000421 mDisplayModeDirector.setListener(new AllowedDisplayModeObserver());
422 mDisplayModeDirector.start();
423
Jeff Brownbd6e1502012-08-28 03:27:37 -0700424 mHandler.sendEmptyMessage(MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS);
425 }
426
Santos Cordonee8931e2017-04-05 10:31:15 -0700427 @VisibleForTesting
428 Handler getDisplayHandler() {
429 return mHandler;
430 }
431
Michael Wrighteedcbf12017-08-16 23:14:54 +0100432 private void loadStableDisplayValuesLocked() {
433 final Point size = mPersistentDataStore.getStableDisplaySize();
434 if (size.x > 0 && size.y > 0) {
435 // Just set these values directly so we don't write the display persistent data again
436 // unnecessarily
437 mStableDisplaySize.set(size.x, size.y);
438 } else {
439 final Resources res = mContext.getResources();
440 final int width = res.getInteger(
441 com.android.internal.R.integer.config_stableDeviceDisplayWidth);
442 final int height = res.getInteger(
443 com.android.internal.R.integer.config_stableDeviceDisplayHeight);
444 if (width > 0 && height > 0) {
445 setStableDisplaySizeLocked(width, height);
446 }
447 }
448 }
449
450 private Point getStableDisplaySizeInternal() {
451 Point r = new Point();
452 synchronized (mSyncRoot) {
453 if (mStableDisplaySize.x > 0 && mStableDisplaySize.y > 0) {
454 r.set(mStableDisplaySize.x, mStableDisplaySize.y);
455 }
456 }
457 return r;
458 }
459
Jeff Brown4ccb8232014-01-16 22:16:42 -0800460 private void registerDisplayTransactionListenerInternal(
461 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800462 // List is self-synchronized copy-on-write.
463 mDisplayTransactionListeners.add(listener);
464 }
465
Jeff Brown4ccb8232014-01-16 22:16:42 -0800466 private void unregisterDisplayTransactionListenerInternal(
467 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800468 // List is self-synchronized copy-on-write.
469 mDisplayTransactionListeners.remove(listener);
470 }
471
Jeff Brown4ccb8232014-01-16 22:16:42 -0800472 private void setDisplayInfoOverrideFromWindowManagerInternal(
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700473 int displayId, DisplayInfo info) {
474 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700475 LogicalDisplay display = mLogicalDisplays.get(displayId);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700476 if (display != null) {
Jeff Brownef981a42013-08-07 14:13:37 -0700477 if (display.setDisplayInfoOverrideFromWindowManagerLocked(info)) {
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200478 handleLogicalDisplayChanged(displayId, display);
Craig Mautner65d11b32012-10-01 13:59:52 -0700479 scheduleTraversalLocked(false);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700480 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700481 }
482 }
483 }
484
Andrii Kuliancd097992017-03-23 18:31:59 -0700485 /**
486 * @see DisplayManagerInternal#getNonOverrideDisplayInfo(int, DisplayInfo)
487 */
488 private void getNonOverrideDisplayInfoInternal(int displayId, DisplayInfo outInfo) {
489 synchronized (mSyncRoot) {
490 final LogicalDisplay display = mLogicalDisplays.get(displayId);
491 if (display != null) {
492 display.getNonOverrideDisplayInfoLocked(outInfo);
493 }
494 }
495 }
496
Santos Cordonee8931e2017-04-05 10:31:15 -0700497 @VisibleForTesting
Robert Carrae606b42018-02-15 15:36:23 -0800498 void performTraversalInternal(SurfaceControl.Transaction t) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700499 synchronized (mSyncRoot) {
500 if (!mPendingTraversal) {
501 return;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700502 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700503 mPendingTraversal = false;
504
Robert Carrae606b42018-02-15 15:36:23 -0800505 performTraversalLocked(t);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700506 }
Jeff Brown7f3994e2012-12-04 14:04:28 -0800507
508 // List is self-synchronized copy-on-write.
509 for (DisplayTransactionListener listener : mDisplayTransactionListeners) {
Vishnu Naire86bd982018-11-28 13:23:17 -0800510 listener.onDisplayTransaction(t);
Jeff Brown7f3994e2012-12-04 14:04:28 -0800511 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700512 }
513
Jeff Brown5d6443b2015-04-10 20:15:01 -0700514 private void requestGlobalDisplayStateInternal(int state, int brightness) {
515 if (state == Display.STATE_UNKNOWN) {
516 state = Display.STATE_ON;
517 }
518 if (state == Display.STATE_OFF) {
519 brightness = PowerManager.BRIGHTNESS_OFF;
520 } else if (brightness < 0) {
521 brightness = PowerManager.BRIGHTNESS_DEFAULT;
522 } else if (brightness > PowerManager.BRIGHTNESS_ON) {
523 brightness = PowerManager.BRIGHTNESS_ON;
524 }
525
Jeff Browne75926d2014-09-18 15:24:49 -0700526 synchronized (mTempDisplayStateWorkQueue) {
527 try {
528 // Update the display state within the lock.
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700529 // Note that we do not need to schedule traversals here although it
530 // may happen as a side-effect of displays changing state.
Jeff Browne75926d2014-09-18 15:24:49 -0700531 synchronized (mSyncRoot) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700532 if (mGlobalDisplayState == state
533 && mGlobalDisplayBrightness == brightness) {
534 return; // no change
Jeff Browne75926d2014-09-18 15:24:49 -0700535 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700536
537 Trace.traceBegin(Trace.TRACE_TAG_POWER, "requestGlobalDisplayState("
538 + Display.stateToString(state)
539 + ", brightness=" + brightness + ")");
540 mGlobalDisplayState = state;
541 mGlobalDisplayBrightness = brightness;
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700542 applyGlobalDisplayStateLocked(mTempDisplayStateWorkQueue);
Jeff Browne75926d2014-09-18 15:24:49 -0700543 }
544
545 // Setting the display power state can take hundreds of milliseconds
546 // to complete so we defer the most expensive part of the work until
547 // after we have exited the critical section to avoid blocking other
548 // threads for a long time.
549 for (int i = 0; i < mTempDisplayStateWorkQueue.size(); i++) {
550 mTempDisplayStateWorkQueue.get(i).run();
551 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700552 Trace.traceEnd(Trace.TRACE_TAG_POWER);
Jeff Browne75926d2014-09-18 15:24:49 -0700553 } finally {
554 mTempDisplayStateWorkQueue.clear();
Jeff Brown9e316a12012-10-08 19:17:06 -0700555 }
556 }
557 }
558
Jeff Brown4ccb8232014-01-16 22:16:42 -0800559 private DisplayInfo getDisplayInfoInternal(int displayId, int callingUid) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700560 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800561 LogicalDisplay display = mLogicalDisplays.get(displayId);
562 if (display != null) {
563 DisplayInfo info = display.getDisplayInfoLocked();
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800564 if (info.hasAccess(callingUid)
565 || isUidPresentOnDisplayInternal(callingUid, displayId)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800566 return info;
567 }
568 }
569 return null;
570 }
571 }
572
573 private int[] getDisplayIdsInternal(int callingUid) {
574 synchronized (mSyncRoot) {
575 final int count = mLogicalDisplays.size();
576 int[] displayIds = new int[count];
577 int n = 0;
578 for (int i = 0; i < count; i++) {
579 LogicalDisplay display = mLogicalDisplays.valueAt(i);
580 DisplayInfo info = display.getDisplayInfoLocked();
581 if (info.hasAccess(callingUid)) {
582 displayIds[n++] = mLogicalDisplays.keyAt(i);
583 }
584 }
585 if (n != count) {
586 displayIds = Arrays.copyOfRange(displayIds, 0, n);
587 }
588 return displayIds;
589 }
590 }
591
592 private void registerCallbackInternal(IDisplayManagerCallback callback, int callingPid) {
593 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700594 if (mCallbacks.get(callingPid) != null) {
595 throw new SecurityException("The calling process has already "
596 + "registered an IDisplayManagerCallback.");
Jeff Brown64a55af2012-08-26 02:47:39 -0700597 }
598
Jeff Brownbd6e1502012-08-28 03:27:37 -0700599 CallbackRecord record = new CallbackRecord(callingPid, callback);
600 try {
601 IBinder binder = callback.asBinder();
602 binder.linkToDeath(record, 0);
603 } catch (RemoteException ex) {
604 // give up
605 throw new RuntimeException(ex);
606 }
607
608 mCallbacks.put(callingPid, record);
609 }
610 }
611
Jeff Brownce468a32013-11-21 16:42:03 -0800612 private void onCallbackDied(CallbackRecord record) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700613 synchronized (mSyncRoot) {
Jeff Brownce468a32013-11-21 16:42:03 -0800614 mCallbacks.remove(record.mPid);
615 stopWifiDisplayScanLocked(record);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700616 }
617 }
618
Jeff Brown4ccb8232014-01-16 22:16:42 -0800619 private void startWifiDisplayScanInternal(int callingPid) {
620 synchronized (mSyncRoot) {
621 CallbackRecord record = mCallbacks.get(callingPid);
622 if (record == null) {
623 throw new IllegalStateException("The calling process has not "
624 + "registered an IDisplayManagerCallback.");
Jeff Browne08ae382012-09-07 20:36:36 -0700625 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800626 startWifiDisplayScanLocked(record);
Jeff Browne08ae382012-09-07 20:36:36 -0700627 }
628 }
629
Jeff Brownce468a32013-11-21 16:42:03 -0800630 private void startWifiDisplayScanLocked(CallbackRecord record) {
631 if (!record.mWifiDisplayScanRequested) {
632 record.mWifiDisplayScanRequested = true;
633 if (mWifiDisplayScanRequestCount++ == 0) {
634 if (mWifiDisplayAdapter != null) {
635 mWifiDisplayAdapter.requestStartScanLocked();
636 }
637 }
638 }
639 }
640
Jeff Brown4ccb8232014-01-16 22:16:42 -0800641 private void stopWifiDisplayScanInternal(int callingPid) {
642 synchronized (mSyncRoot) {
643 CallbackRecord record = mCallbacks.get(callingPid);
644 if (record == null) {
645 throw new IllegalStateException("The calling process has not "
646 + "registered an IDisplayManagerCallback.");
Jeff Brownce468a32013-11-21 16:42:03 -0800647 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800648 stopWifiDisplayScanLocked(record);
Jeff Brownce468a32013-11-21 16:42:03 -0800649 }
650 }
651
652 private void stopWifiDisplayScanLocked(CallbackRecord record) {
653 if (record.mWifiDisplayScanRequested) {
654 record.mWifiDisplayScanRequested = false;
655 if (--mWifiDisplayScanRequestCount == 0) {
656 if (mWifiDisplayAdapter != null) {
657 mWifiDisplayAdapter.requestStopScanLocked();
658 }
659 } else if (mWifiDisplayScanRequestCount < 0) {
Dianne Hackborn8d051722014-10-01 14:59:58 -0700660 Slog.wtf(TAG, "mWifiDisplayScanRequestCount became negative: "
Jeff Brownce468a32013-11-21 16:42:03 -0800661 + mWifiDisplayScanRequestCount);
662 mWifiDisplayScanRequestCount = 0;
663 }
664 }
665 }
666
Jeff Brown4ccb8232014-01-16 22:16:42 -0800667 private void connectWifiDisplayInternal(String address) {
668 synchronized (mSyncRoot) {
669 if (mWifiDisplayAdapter != null) {
670 mWifiDisplayAdapter.requestConnectLocked(address);
Jeff Browne08ae382012-09-07 20:36:36 -0700671 }
Jeff Browne08ae382012-09-07 20:36:36 -0700672 }
673 }
674
Jeff Brown4ccb8232014-01-16 22:16:42 -0800675 private void pauseWifiDisplayInternal() {
676 synchronized (mSyncRoot) {
677 if (mWifiDisplayAdapter != null) {
678 mWifiDisplayAdapter.requestPauseLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700679 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700680 }
681 }
682
Jeff Brown4ccb8232014-01-16 22:16:42 -0800683 private void resumeWifiDisplayInternal() {
684 synchronized (mSyncRoot) {
685 if (mWifiDisplayAdapter != null) {
686 mWifiDisplayAdapter.requestResumeLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700687 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700688 }
689 }
690
Jeff Brown4ccb8232014-01-16 22:16:42 -0800691 private void disconnectWifiDisplayInternal() {
692 synchronized (mSyncRoot) {
693 if (mWifiDisplayAdapter != null) {
694 mWifiDisplayAdapter.requestDisconnectLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700695 }
Jeff Browne08ae382012-09-07 20:36:36 -0700696 }
697 }
698
Jeff Brown4ccb8232014-01-16 22:16:42 -0800699 private void renameWifiDisplayInternal(String address, String alias) {
700 synchronized (mSyncRoot) {
701 if (mWifiDisplayAdapter != null) {
702 mWifiDisplayAdapter.requestRenameLocked(address, alias);
Jeff Brown89d55462012-09-19 11:33:42 -0700703 }
Jeff Brown89d55462012-09-19 11:33:42 -0700704 }
705 }
706
Jeff Brown4ccb8232014-01-16 22:16:42 -0800707 private void forgetWifiDisplayInternal(String address) {
708 synchronized (mSyncRoot) {
709 if (mWifiDisplayAdapter != null) {
710 mWifiDisplayAdapter.requestForgetLocked(address);
Jeff Brown89d55462012-09-19 11:33:42 -0700711 }
Jeff Brown89d55462012-09-19 11:33:42 -0700712 }
713 }
714
Jeff Brown4ccb8232014-01-16 22:16:42 -0800715 private WifiDisplayStatus getWifiDisplayStatusInternal() {
716 synchronized (mSyncRoot) {
717 if (mWifiDisplayAdapter != null) {
718 return mWifiDisplayAdapter.getWifiDisplayStatusLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700719 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800720 return new WifiDisplayStatus();
Jeff Browne08ae382012-09-07 20:36:36 -0700721 }
722 }
723
Michael Wright1c9977b2016-07-12 13:30:10 -0700724 private void requestColorModeInternal(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +0100725 synchronized (mSyncRoot) {
726 LogicalDisplay display = mLogicalDisplays.get(displayId);
727 if (display != null &&
Michael Wright1c9977b2016-07-12 13:30:10 -0700728 display.getRequestedColorModeLocked() != colorMode) {
729 display.setRequestedColorModeLocked(colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +0100730 scheduleTraversalLocked(false);
731 }
732 }
733 }
734
Michael Wright75ee9fc2014-09-01 19:55:22 -0700735 private int createVirtualDisplayInternal(IVirtualDisplayCallback callback,
Santos Cordonee8931e2017-04-05 10:31:15 -0700736 IMediaProjection projection, int callingUid, String packageName, String name, int width,
737 int height, int densityDpi, Surface surface, int flags, String uniqueId) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800738 synchronized (mSyncRoot) {
739 if (mVirtualDisplayAdapter == null) {
740 Slog.w(TAG, "Rejecting request to create private virtual display "
741 + "because the virtual display adapter is not available.");
742 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700743 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800744
745 DisplayDevice device = mVirtualDisplayAdapter.createVirtualDisplayLocked(
Santos Cordonee8931e2017-04-05 10:31:15 -0700746 callback, projection, callingUid, packageName, name, width, height, densityDpi,
747 surface, flags, uniqueId);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800748 if (device == null) {
749 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700750 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700751
Jeff Brown4ccb8232014-01-16 22:16:42 -0800752 handleDisplayDeviceAddedLocked(device);
753 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
754 if (display != null) {
755 return display.getDisplayIdLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700756 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800757
758 // Something weird happened and the logical display was not created.
759 Slog.w(TAG, "Rejecting request to create virtual display "
760 + "because the logical display was not created.");
Michael Wright75ee9fc2014-09-01 19:55:22 -0700761 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800762 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700763 }
764 return -1;
765 }
766
Michael Wright01e840f2014-06-26 16:03:25 -0700767 private void resizeVirtualDisplayInternal(IBinder appToken,
768 int width, int height, int densityDpi) {
769 synchronized (mSyncRoot) {
770 if (mVirtualDisplayAdapter == null) {
771 return;
772 }
773
774 mVirtualDisplayAdapter.resizeVirtualDisplayLocked(appToken, width, height, densityDpi);
775 }
776 }
777
Jeff Brown92207df2014-04-16 13:16:07 -0700778 private void setVirtualDisplaySurfaceInternal(IBinder appToken, Surface surface) {
779 synchronized (mSyncRoot) {
780 if (mVirtualDisplayAdapter == null) {
781 return;
782 }
783
784 mVirtualDisplayAdapter.setVirtualDisplaySurfaceLocked(appToken, surface);
785 }
786 }
787
Jeff Brown4ccb8232014-01-16 22:16:42 -0800788 private void releaseVirtualDisplayInternal(IBinder appToken) {
789 synchronized (mSyncRoot) {
790 if (mVirtualDisplayAdapter == null) {
791 return;
Jeff Browna506a6e2013-06-04 00:02:38 -0700792 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700793
Jeff Brown4ccb8232014-01-16 22:16:42 -0800794 DisplayDevice device =
795 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(appToken);
796 if (device != null) {
797 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700798 }
799 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700800 }
801
chaviwda4c6942018-11-07 15:52:56 -0800802 private void setVirtualDisplayStateInternal(IBinder appToken, boolean isOn) {
803 synchronized (mSyncRoot) {
804 if (mVirtualDisplayAdapter == null) {
805 return;
806 }
807
808 mVirtualDisplayAdapter.setVirtualDisplayStateLocked(appToken, isOn);
809 }
810 }
811
Santos Cordonc22c5632017-06-21 16:03:49 -0700812 private void registerDefaultDisplayAdapters() {
813 // Register default display adapters.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700814 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700815 // main display adapter
Mike Lockwoode63f6f72013-11-15 11:01:47 -0800816 registerDisplayAdapterLocked(new LocalDisplayAdapter(
817 mSyncRoot, mContext, mHandler, mDisplayAdapterListener));
Santos Cordonc22c5632017-06-21 16:03:49 -0700818
819 // Standalone VR devices rely on a virtual display as their primary display for
820 // 2D UI. We register virtual display adapter along side the main display adapter
821 // here so that it is ready by the time the system sends the home Intent for
822 // early apps like SetupWizard/Launcher. In particular, SUW is displayed using
823 // the virtual display inside VR before any VR-specific apps even run.
824 mVirtualDisplayAdapter = mInjector.getVirtualDisplayAdapter(mSyncRoot, mContext,
825 mHandler, mDisplayAdapterListener);
826 if (mVirtualDisplayAdapter != null) {
827 registerDisplayAdapterLocked(mVirtualDisplayAdapter);
828 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700829 }
830 }
831
832 private void registerAdditionalDisplayAdapters() {
833 synchronized (mSyncRoot) {
834 if (shouldRegisterNonEssentialDisplayAdaptersLocked()) {
Jeff Brown89d55462012-09-19 11:33:42 -0700835 registerOverlayDisplayAdapterLocked();
836 registerWifiDisplayAdapterLocked();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700837 }
838 }
839 }
840
Jeff Brown89d55462012-09-19 11:33:42 -0700841 private void registerOverlayDisplayAdapterLocked() {
842 registerDisplayAdapterLocked(new OverlayDisplayAdapter(
843 mSyncRoot, mContext, mHandler, mDisplayAdapterListener, mUiHandler));
844 }
845
846 private void registerWifiDisplayAdapterLocked() {
847 if (mContext.getResources().getBoolean(
Jeff Brownbbd28a22012-09-20 16:47:15 -0700848 com.android.internal.R.bool.config_enableWifiDisplay)
849 || SystemProperties.getInt(FORCE_WIFI_DISPLAY_ENABLE, -1) == 1) {
Jeff Brown89d55462012-09-19 11:33:42 -0700850 mWifiDisplayAdapter = new WifiDisplayAdapter(
851 mSyncRoot, mContext, mHandler, mDisplayAdapterListener,
852 mPersistentDataStore);
853 registerDisplayAdapterLocked(mWifiDisplayAdapter);
854 }
855 }
856
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700857 private boolean shouldRegisterNonEssentialDisplayAdaptersLocked() {
858 // In safe mode, we disable non-essential display adapters to give the user
859 // an opportunity to fix broken settings or other problems that might affect
860 // system stability.
861 // In only-core mode, we disable non-essential display adapters to minimize
862 // the number of dependencies that are started while in this mode and to
863 // prevent problems that might occur due to the device being encrypted.
864 return !mSafeMode && !mOnlyCore;
865 }
866
867 private void registerDisplayAdapterLocked(DisplayAdapter adapter) {
868 mDisplayAdapters.add(adapter);
869 adapter.registerLocked();
870 }
871
Jeff Brownbd6e1502012-08-28 03:27:37 -0700872 private void handleDisplayDeviceAdded(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700873 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700874 handleDisplayDeviceAddedLocked(device);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700875 }
876 }
877
Jeff Browna506a6e2013-06-04 00:02:38 -0700878 private void handleDisplayDeviceAddedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700879 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700880 if (mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700881 Slog.w(TAG, "Attempted to add already added display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700882 return;
883 }
884
Jeff Brown10acf6d2015-04-14 14:20:47 -0700885 Slog.i(TAG, "Display device added: " + info);
886 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700887
888 mDisplayDevices.add(device);
Michael Wright1c9977b2016-07-12 13:30:10 -0700889 LogicalDisplay display = addLogicalDisplayLocked(device);
Jeff Brown0033a862014-10-08 12:06:39 -0700890 Runnable work = updateDisplayStateLocked(device);
891 if (work != null) {
892 work.run();
893 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700894 scheduleTraversalLocked(false);
895 }
896
Jeff Brownbd6e1502012-08-28 03:27:37 -0700897 private void handleDisplayDeviceChanged(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700898 synchronized (mSyncRoot) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700899 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700900 if (!mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700901 Slog.w(TAG, "Attempted to change non-existent display device: " + info);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700902 return;
903 }
904
Jeff Brown10acf6d2015-04-14 14:20:47 -0700905 int diff = device.mDebugLastLoggedDeviceInfo.diff(info);
906 if (diff == DisplayDeviceInfo.DIFF_STATE) {
907 Slog.i(TAG, "Display device changed state: \"" + info.name
908 + "\", " + Display.stateToString(info.state));
909 } else if (diff != 0) {
910 Slog.i(TAG, "Display device changed: " + info);
911 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700912 if ((diff & DisplayDeviceInfo.DIFF_COLOR_MODE) != 0) {
913 try {
914 mPersistentDataStore.setColorMode(device, info.colorMode);
915 } finally {
916 mPersistentDataStore.saveIfNeeded();
917 }
918 }
Jeff Brown10acf6d2015-04-14 14:20:47 -0700919 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browne87bf032012-09-20 18:30:13 -0700920
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700921 device.applyPendingDisplayDeviceInfoChangesLocked();
922 if (updateLogicalDisplaysLocked()) {
Craig Mautner65d11b32012-10-01 13:59:52 -0700923 scheduleTraversalLocked(false);
Jeff Brown64a55af2012-08-26 02:47:39 -0700924 }
925 }
926 }
927
Jeff Brownbd6e1502012-08-28 03:27:37 -0700928 private void handleDisplayDeviceRemoved(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700929 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700930 handleDisplayDeviceRemovedLocked(device);
931 }
932 }
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700933
Jeff Browna506a6e2013-06-04 00:02:38 -0700934 private void handleDisplayDeviceRemovedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700935 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700936 if (!mDisplayDevices.remove(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700937 Slog.w(TAG, "Attempted to remove non-existent display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700938 return;
939 }
940
Jeff Brown10acf6d2015-04-14 14:20:47 -0700941 Slog.i(TAG, "Display device removed: " + info);
942 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700943
Jeff Browna506a6e2013-06-04 00:02:38 -0700944 updateLogicalDisplaysLocked();
945 scheduleTraversalLocked(false);
946 }
947
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200948 private void handleLogicalDisplayChanged(int displayId, @NonNull LogicalDisplay display) {
949 if (displayId == Display.DEFAULT_DISPLAY) {
950 recordTopInsetLocked(display);
951 }
952 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
953 }
954
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700955 private void applyGlobalDisplayStateLocked(List<Runnable> workQueue) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700956 final int count = mDisplayDevices.size();
957 for (int i = 0; i < count; i++) {
958 DisplayDevice device = mDisplayDevices.get(i);
Jeff Browne75926d2014-09-18 15:24:49 -0700959 Runnable runnable = updateDisplayStateLocked(device);
960 if (runnable != null) {
961 workQueue.add(runnable);
962 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700963 }
964 }
965
Jeff Browne75926d2014-09-18 15:24:49 -0700966 private Runnable updateDisplayStateLocked(DisplayDevice device) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700967 // Blank or unblank the display immediately to match the state requested
Jeff Brown037c33e2014-04-09 00:31:55 -0700968 // by the display power controller (if known).
Jeff Browna506a6e2013-06-04 00:02:38 -0700969 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
970 if ((info.flags & DisplayDeviceInfo.FLAG_NEVER_BLANK) == 0) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700971 return device.requestDisplayStateLocked(mGlobalDisplayState, mGlobalDisplayBrightness);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700972 }
Jeff Browne75926d2014-09-18 15:24:49 -0700973 return null;
Craig Mautner4f67ba62012-08-02 11:23:00 -0700974 }
975
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700976 // Adds a new logical display based on the given display device.
977 // Sends notifications if needed.
Michael Wright1c9977b2016-07-12 13:30:10 -0700978 private LogicalDisplay addLogicalDisplayLocked(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700979 DisplayDeviceInfo deviceInfo = device.getDisplayDeviceInfoLocked();
980 boolean isDefault = (deviceInfo.flags
981 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0;
982 if (isDefault && mLogicalDisplays.get(Display.DEFAULT_DISPLAY) != null) {
983 Slog.w(TAG, "Ignoring attempt to add a second default display: " + deviceInfo);
984 isDefault = false;
985 }
986
Jeff Brown27f1d672012-10-17 18:32:34 -0700987 if (!isDefault && mSingleDisplayDemoMode) {
988 Slog.i(TAG, "Not creating a logical display for a secondary display "
989 + " because single display demo mode is enabled: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -0700990 return null;
Jeff Brown27f1d672012-10-17 18:32:34 -0700991 }
992
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700993 final int displayId = assignDisplayIdLocked(isDefault);
994 final int layerStack = assignLayerStackLocked(displayId);
995
Jeff Brownd728bf52012-09-08 18:05:28 -0700996 LogicalDisplay display = new LogicalDisplay(displayId, layerStack, device);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700997 display.updateLocked(mDisplayDevices);
998 if (!display.isValidLocked()) {
999 // This should never happen currently.
1000 Slog.w(TAG, "Ignoring display device because the logical display "
1001 + "created from it was not considered valid: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -07001002 return null;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001003 }
1004
Michael Wrighteedcbf12017-08-16 23:14:54 +01001005 configureColorModeLocked(display, device);
1006 if (isDefault) {
1007 recordStableDisplayStatsIfNeededLocked(display);
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001008 recordTopInsetLocked(display);
Michael Wrighteedcbf12017-08-16 23:14:54 +01001009 }
1010
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001011 mLogicalDisplays.put(displayId, display);
1012
1013 // Wake up waitForDefaultDisplay.
1014 if (isDefault) {
1015 mSyncRoot.notifyAll();
1016 }
1017
1018 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_ADDED);
Michael Wright1c9977b2016-07-12 13:30:10 -07001019 return display;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001020 }
1021
1022 private int assignDisplayIdLocked(boolean isDefault) {
1023 return isDefault ? Display.DEFAULT_DISPLAY : mNextNonDefaultDisplayId++;
1024 }
1025
1026 private int assignLayerStackLocked(int displayId) {
1027 // Currently layer stacks and display ids are the same.
1028 // This need not be the case.
1029 return displayId;
1030 }
1031
Michael Wrighteedcbf12017-08-16 23:14:54 +01001032 private void configureColorModeLocked(LogicalDisplay display, DisplayDevice device) {
1033 if (display.getPrimaryDisplayDeviceLocked() == device) {
1034 int colorMode = mPersistentDataStore.getColorMode(device);
1035 if (colorMode == Display.COLOR_MODE_INVALID) {
1036 if ((device.getDisplayDeviceInfoLocked().flags
1037 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
1038 colorMode = mDefaultDisplayDefaultColorMode;
1039 } else {
1040 colorMode = Display.COLOR_MODE_DEFAULT;
1041 }
1042 }
1043 display.setRequestedColorModeLocked(colorMode);
1044 }
1045 }
1046
1047 // If we've never recorded stable device stats for this device before and they aren't
1048 // explicitly configured, go ahead and record the stable device stats now based on the status
1049 // of the default display at first boot.
1050 private void recordStableDisplayStatsIfNeededLocked(LogicalDisplay d) {
1051 if (mStableDisplaySize.x <= 0 && mStableDisplaySize.y <= 0) {
1052 DisplayInfo info = d.getDisplayInfoLocked();
1053 setStableDisplaySizeLocked(info.getNaturalWidth(), info.getNaturalHeight());
1054 }
1055 }
1056
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001057 private void recordTopInsetLocked(@Nullable LogicalDisplay d) {
MÃ¥rten Kongstad250cb2b2018-11-02 11:01:44 +01001058 if (d == null) {
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001059 return;
1060 }
1061 int topInset = d.getInsets().top;
1062 if (topInset == mDefaultDisplayTopInset) {
1063 return;
1064 }
1065 mDefaultDisplayTopInset = topInset;
1066 SystemProperties.set(PROP_DEFAULT_DISPLAY_TOP_INSET, Integer.toString(topInset));
1067 }
1068
Michael Wrighteedcbf12017-08-16 23:14:54 +01001069 private void setStableDisplaySizeLocked(int width, int height) {
1070 mStableDisplaySize = new Point(width, height);
1071 try {
1072 mPersistentDataStore.setStableDisplaySize(mStableDisplaySize);
1073 } finally {
1074 mPersistentDataStore.saveIfNeeded();
1075 }
1076 }
1077
Dan Gittik122df862018-03-28 16:59:22 +01001078 @VisibleForTesting
1079 Curve getMinimumBrightnessCurveInternal() {
1080 return mMinimumBrightnessCurve;
1081 }
1082
Peiyong Lin277eaff2019-01-16 16:18:22 -08001083 int getPreferredWideGamutColorSpaceIdInternal() {
1084 return mWideColorSpace.getId();
1085 }
1086
Michael Wrighteef0e132017-11-21 17:57:52 +00001087 private void setBrightnessConfigurationForUserInternal(
Dan Gittik7a32fba2018-03-28 12:19:38 +01001088 @Nullable BrightnessConfiguration c, @UserIdInt int userId,
Kenny Guy05ce8092018-01-17 13:44:20 +00001089 @Nullable String packageName) {
Dan Gittik122df862018-03-28 16:59:22 +01001090 validateBrightnessConfiguration(c);
Michael Wrighteef0e132017-11-21 17:57:52 +00001091 final int userSerial = getUserManager().getUserSerialNumber(userId);
1092 synchronized (mSyncRoot) {
1093 try {
Kenny Guy05ce8092018-01-17 13:44:20 +00001094 mPersistentDataStore.setBrightnessConfigurationForUser(c, userSerial,
1095 packageName);
Michael Wrighteef0e132017-11-21 17:57:52 +00001096 } finally {
1097 mPersistentDataStore.saveIfNeeded();
1098 }
1099 if (userId == mCurrentUserId) {
1100 mDisplayPowerController.setBrightnessConfiguration(c);
1101 }
1102 }
1103 }
1104
Dan Gittik122df862018-03-28 16:59:22 +01001105 @VisibleForTesting
1106 void validateBrightnessConfiguration(BrightnessConfiguration config) {
1107 if (config == null) {
1108 return;
1109 }
1110 if (isBrightnessConfigurationTooDark(config)) {
1111 throw new IllegalArgumentException("brightness curve is too dark");
1112 }
1113 }
1114
1115 private boolean isBrightnessConfigurationTooDark(BrightnessConfiguration config) {
1116 Pair<float[], float[]> curve = config.getCurve();
1117 float[] lux = curve.first;
1118 float[] nits = curve.second;
1119 for (int i = 0; i < lux.length; i++) {
1120 if (nits[i] < mMinimumBrightnessSpline.interpolate(lux[i])) {
1121 return true;
1122 }
1123 }
1124 return false;
1125 }
1126
Michael Wrighteef0e132017-11-21 17:57:52 +00001127 private void loadBrightnessConfiguration() {
1128 synchronized (mSyncRoot) {
1129 final int userSerial = getUserManager().getUserSerialNumber(mCurrentUserId);
1130 BrightnessConfiguration config =
1131 mPersistentDataStore.getBrightnessConfiguration(userSerial);
1132 mDisplayPowerController.setBrightnessConfiguration(config);
1133 }
1134 }
1135
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001136 // Updates all existing logical displays given the current set of display devices.
1137 // Removes invalid logical displays.
1138 // Sends notifications if needed.
1139 private boolean updateLogicalDisplaysLocked() {
1140 boolean changed = false;
1141 for (int i = mLogicalDisplays.size(); i-- > 0; ) {
1142 final int displayId = mLogicalDisplays.keyAt(i);
1143 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1144
1145 mTempDisplayInfo.copyFrom(display.getDisplayInfoLocked());
1146 display.updateLocked(mDisplayDevices);
1147 if (!display.isValidLocked()) {
1148 mLogicalDisplays.removeAt(i);
1149 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_REMOVED);
1150 changed = true;
1151 } else if (!mTempDisplayInfo.equals(display.getDisplayInfoLocked())) {
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001152 handleLogicalDisplayChanged(displayId, display);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001153 changed = true;
1154 }
1155 }
1156 return changed;
1157 }
1158
Robert Carrae606b42018-02-15 15:36:23 -08001159 private void performTraversalLocked(SurfaceControl.Transaction t) {
Jeff Brownd728bf52012-09-08 18:05:28 -07001160 // Clear all viewports before configuring displays so that we can keep
1161 // track of which ones we have configured.
1162 clearViewportsLocked();
1163
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001164 // Configure each display device.
1165 final int count = mDisplayDevices.size();
1166 for (int i = 0; i < count; i++) {
1167 DisplayDevice device = mDisplayDevices.get(i);
Robert Carrae606b42018-02-15 15:36:23 -08001168 configureDisplayLocked(t, device);
1169 device.performTraversalLocked(t);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001170 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001171
1172 // Tell the input system about these new viewports.
Jeff Brown4ccb8232014-01-16 22:16:42 -08001173 if (mInputManagerInternal != null) {
Jeff Brownd728bf52012-09-08 18:05:28 -07001174 mHandler.sendEmptyMessage(MSG_UPDATE_VIEWPORT);
1175 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001176 }
1177
Michael Wright3f145a22014-07-22 19:46:03 -07001178 private void setDisplayPropertiesInternal(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001179 float requestedRefreshRate, int requestedModeId, boolean inTraversal) {
Craig Mautner722285e2012-09-07 13:55:58 -07001180 synchronized (mSyncRoot) {
1181 LogicalDisplay display = mLogicalDisplays.get(displayId);
Michael Wright3f145a22014-07-22 19:46:03 -07001182 if (display == null) {
1183 return;
1184 }
1185 if (display.hasContentLocked() != hasContent) {
Jeff Brown33041bd2013-08-02 21:11:14 -07001186 if (DEBUG) {
1187 Slog.d(TAG, "Display " + displayId + " hasContent flag changed: "
1188 + "hasContent=" + hasContent + ", inTraversal=" + inTraversal);
1189 }
1190
Craig Mautner722285e2012-09-07 13:55:58 -07001191 display.setHasContentLocked(hasContent);
Craig Mautner65d11b32012-10-01 13:59:52 -07001192 scheduleTraversalLocked(inTraversal);
Craig Mautner722285e2012-09-07 13:55:58 -07001193 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001194 if (requestedModeId == 0 && requestedRefreshRate != 0) {
1195 // Scan supported modes returned by display.getInfo() to find a mode with the same
1196 // size as the default display mode but with the specified refresh rate instead.
1197 requestedModeId = display.getDisplayInfoLocked().findDefaultModeByRefreshRate(
1198 requestedRefreshRate);
1199 }
Michael Wrighta3dab232019-02-22 16:54:21 +00001200 mDisplayModeDirector.getAppRequestObserver().setAppRequestedMode(
1201 displayId, requestedModeId);
Craig Mautner722285e2012-09-07 13:55:58 -07001202 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001203 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001204
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08001205 private void setDisplayOffsetsInternal(int displayId, int x, int y) {
1206 synchronized (mSyncRoot) {
1207 LogicalDisplay display = mLogicalDisplays.get(displayId);
1208 if (display == null) {
1209 return;
1210 }
1211 if (display.getDisplayOffsetXLocked() != x
1212 || display.getDisplayOffsetYLocked() != y) {
1213 if (DEBUG) {
1214 Slog.d(TAG, "Display " + displayId + " burn-in offset set to ("
1215 + x + ", " + y + ")");
1216 }
1217 display.setDisplayOffsetsLocked(x, y);
1218 scheduleTraversalLocked(false);
1219 }
1220 }
1221 }
1222
Sam Lin4c3ac2b2019-02-18 04:50:26 -08001223 private void setDisplayScalingDisabledInternal(int displayId, boolean disable) {
1224 synchronized (mSyncRoot) {
1225 final LogicalDisplay display = mLogicalDisplays.get(displayId);
1226 if (display == null) {
1227 return;
1228 }
1229 if (display.isDisplayScalingDisabled() != disable) {
1230 if (DEBUG) {
1231 Slog.d(TAG, "Display " + displayId + " content scaling disabled = " + disable);
1232 }
1233 display.setDisplayScalingDisabledLocked(disable);
1234 scheduleTraversalLocked(false);
1235 }
1236 }
1237 }
1238
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001239 // Updates the lists of UIDs that are present on displays.
1240 private void setDisplayAccessUIDsInternal(SparseArray<IntArray> newDisplayAccessUIDs) {
1241 synchronized (mSyncRoot) {
1242 mDisplayAccessUIDs.clear();
1243 for (int i = newDisplayAccessUIDs.size() - 1; i >= 0; i--) {
1244 mDisplayAccessUIDs.append(newDisplayAccessUIDs.keyAt(i),
1245 newDisplayAccessUIDs.valueAt(i));
1246 }
1247 }
1248 }
1249
1250 // Checks if provided UID's content is present on the display and UID has access to it.
1251 private boolean isUidPresentOnDisplayInternal(int uid, int displayId) {
1252 synchronized (mSyncRoot) {
1253 final IntArray displayUIDs = mDisplayAccessUIDs.get(displayId);
1254 return displayUIDs != null && displayUIDs.indexOf(uid) != -1;
1255 }
1256 }
1257
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001258 @Nullable
1259 private IBinder getDisplayToken(int displayId) {
1260 synchronized (mSyncRoot) {
1261 final LogicalDisplay display = mLogicalDisplays.get(displayId);
1262 if (display != null) {
1263 final DisplayDevice device = display.getPrimaryDisplayDeviceLocked();
1264 if (device != null) {
1265 return device.getDisplayTokenLocked();
1266 }
1267 }
1268 }
1269
1270 return null;
1271 }
1272
1273 private boolean screenshotInternal(int displayId, Surface outSurface) {
1274 final IBinder token = getDisplayToken(displayId);
1275 if (token == null) {
1276 return false;
1277 }
Robert Carr5c52b132019-02-15 15:48:11 -08001278 final GraphicBuffer gb = SurfaceControl.screenshotToBufferWithSecureLayersUnsafe(
1279 token, new Rect(), 0 /* width */, 0 /* height */, false /* useIdentityTransform */,
1280 0 /* rotation */);
1281 try {
1282 outSurface.attachAndQueueBuffer(gb);
1283 } catch (RuntimeException e) {
1284 Slog.w(TAG, "Failed to take screenshot - " + e.getMessage());
1285 }
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001286 return true;
1287 }
1288
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001289 @VisibleForTesting
1290 DisplayedContentSamplingAttributes getDisplayedContentSamplingAttributesInternal(
1291 int displayId) {
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001292 final IBinder token = getDisplayToken(displayId);
1293 if (token == null) {
1294 return null;
1295 }
1296 return SurfaceControl.getDisplayedContentSamplingAttributes(token);
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001297 }
1298
1299 @VisibleForTesting
1300 boolean setDisplayedContentSamplingEnabledInternal(
1301 int displayId, boolean enable, int componentMask, int maxFrames) {
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001302 final IBinder token = getDisplayToken(displayId);
1303 if (token == null) {
1304 return false;
1305 }
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001306 return SurfaceControl.setDisplayedContentSamplingEnabled(
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001307 token, enable, componentMask, maxFrames);
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001308 }
1309
1310 @VisibleForTesting
1311 DisplayedContentSample getDisplayedContentSampleInternal(int displayId,
1312 long maxFrames, long timestamp) {
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001313 final IBinder token = getDisplayToken(displayId);
1314 if (token == null) {
1315 return null;
1316 }
1317 return SurfaceControl.getDisplayedContentSample(token, maxFrames, timestamp);
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001318 }
1319
Michael Wrighta3dab232019-02-22 16:54:21 +00001320 private void onAllowedDisplayModesChangedInternal() {
1321 boolean changed = false;
1322 synchronized (mSyncRoot) {
1323 final int count = mLogicalDisplays.size();
1324 for (int i = 0; i < count; i++) {
1325 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1326 int displayId = mLogicalDisplays.keyAt(i);
1327 int[] allowedModes = mDisplayModeDirector.getAllowedModes(displayId);
1328 // Note that order is important here since not all display devices are capable of
1329 // automatically switching, so we do actually want to check for equality and not
1330 // just equivalent contents (regardless of order).
1331 if (!Arrays.equals(allowedModes, display.getAllowedDisplayModesLocked())) {
1332 display.setAllowedDisplayModesLocked(allowedModes);
1333 changed = true;
1334 }
1335 }
1336 if (changed) {
1337 scheduleTraversalLocked(false);
1338 }
1339 }
1340 }
1341
Jeff Brownd728bf52012-09-08 18:05:28 -07001342 private void clearViewportsLocked() {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001343 mViewports.clear();
Craig Mautner722285e2012-09-07 13:55:58 -07001344 }
1345
Robert Carrae606b42018-02-15 15:36:23 -08001346 private void configureDisplayLocked(SurfaceControl.Transaction t, DisplayDevice device) {
Jeff Brownd14c8c92014-01-07 18:13:09 -08001347 final DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
1348 final boolean ownContent = (info.flags & DisplayDeviceInfo.FLAG_OWN_CONTENT_ONLY) != 0;
Jeff Browna506a6e2013-06-04 00:02:38 -07001349
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001350 // Find the logical display that the display device is showing.
Jeff Brownd14c8c92014-01-07 18:13:09 -08001351 // Certain displays only ever show their own content.
Craig Mautner722285e2012-09-07 13:55:58 -07001352 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
Jeff Brownd14c8c92014-01-07 18:13:09 -08001353 if (!ownContent) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001354 if (display != null && !display.hasContentLocked()) {
1355 // If the display does not have any content of its own, then
1356 // automatically mirror the default logical display contents.
1357 display = null;
1358 }
1359 if (display == null) {
1360 display = mLogicalDisplays.get(Display.DEFAULT_DISPLAY);
1361 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001362 }
1363
1364 // Apply the logical display configuration to the display device.
1365 if (display == null) {
1366 // TODO: no logical display for the device, blank it
Jeff Brownd728bf52012-09-08 18:05:28 -07001367 Slog.w(TAG, "Missing logical display to use for physical display device: "
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001368 + device.getDisplayDeviceInfoLocked());
Jeff Brownd728bf52012-09-08 18:05:28 -07001369 return;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001370 }
Robert Carrae606b42018-02-15 15:36:23 -08001371 display.configureDisplayLocked(t, device, info.state == Display.STATE_OFF);
Arthur Hung41e81e72018-10-31 18:04:56 +08001372 final int viewportType;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001373 // Update the corresponding viewport.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001374 if ((info.flags & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
Arthur Hung41e81e72018-10-31 18:04:56 +08001375 viewportType = VIEWPORT_INTERNAL;
1376 } else if (info.touch == DisplayDeviceInfo.TOUCH_EXTERNAL) {
1377 viewportType = VIEWPORT_EXTERNAL;
1378 } else if (info.touch == DisplayDeviceInfo.TOUCH_VIRTUAL
1379 && !TextUtils.isEmpty(info.uniqueId)) {
1380 viewportType = VIEWPORT_VIRTUAL;
1381 } else {
Arthur Hung46c4e582019-02-12 15:55:28 +08001382 Slog.i(TAG, "Display " + info + " does not support input device matching.");
Arthur Hung41e81e72018-10-31 18:04:56 +08001383 return;
Jeff Brownd728bf52012-09-08 18:05:28 -07001384 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001385
Arthur Hung41e81e72018-10-31 18:04:56 +08001386 populateViewportLocked(viewportType, display.getDisplayIdLocked(), device, info.uniqueId);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001387 }
1388
1389 /**
1390 * Get internal or external viewport. Create it if does not currently exist.
1391 * @param viewportType - either INTERNAL or EXTERNAL
1392 * @return the viewport with the requested type
1393 */
Arthur Hung41e81e72018-10-31 18:04:56 +08001394 private DisplayViewport getViewportLocked(int viewportType, String uniqueId) {
1395 if (viewportType != VIEWPORT_INTERNAL && viewportType != VIEWPORT_EXTERNAL
1396 && viewportType != VIEWPORT_VIRTUAL) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001397 Slog.wtf(TAG, "Cannot call getViewportByTypeLocked for type "
1398 + DisplayViewport.typeToString(viewportType));
1399 return null;
1400 }
Arthur Hung41e81e72018-10-31 18:04:56 +08001401
1402 // Only allow a single INTERNAL or EXTERNAL viewport by forcing their uniqueIds
1403 // to be identical (in particular, empty).
1404 // TODO (b/116824030) allow multiple EXTERNAL viewports and remove this function.
1405 if (viewportType != VIEWPORT_VIRTUAL) {
1406 uniqueId = "";
1407 }
1408
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001409 DisplayViewport viewport;
1410 final int count = mViewports.size();
1411 for (int i = 0; i < count; i++) {
1412 viewport = mViewports.get(i);
Arthur Hung41e81e72018-10-31 18:04:56 +08001413 if (viewport.type == viewportType && uniqueId.equals(viewport.uniqueId)) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001414 return viewport;
1415 }
1416 }
1417
Arthur Hung41e81e72018-10-31 18:04:56 +08001418 // Creates the viewport if none exists.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001419 viewport = new DisplayViewport();
1420 viewport.type = viewportType;
Arthur Hung41e81e72018-10-31 18:04:56 +08001421 viewport.uniqueId = uniqueId;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001422 mViewports.add(viewport);
1423 return viewport;
1424 }
1425
Arthur Hung41e81e72018-10-31 18:04:56 +08001426 private void populateViewportLocked(int viewportType,
1427 int displayId, DisplayDevice device, String uniqueId) {
1428 final DisplayViewport viewport = getViewportLocked(viewportType, uniqueId);
Jeff Brownd728bf52012-09-08 18:05:28 -07001429 device.populateViewportLocked(viewport);
Arthur Hung41e81e72018-10-31 18:04:56 +08001430 viewport.valid = true;
1431 viewport.displayId = displayId;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001432 }
1433
1434 private LogicalDisplay findLogicalDisplayForDeviceLocked(DisplayDevice device) {
1435 final int count = mLogicalDisplays.size();
1436 for (int i = 0; i < count; i++) {
1437 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1438 if (display.getPrimaryDisplayDeviceLocked() == device) {
1439 return display;
1440 }
1441 }
1442 return null;
1443 }
1444
Jeff Brownbd6e1502012-08-28 03:27:37 -07001445 private void sendDisplayEventLocked(int displayId, int event) {
1446 Message msg = mHandler.obtainMessage(MSG_DELIVER_DISPLAY_EVENT, displayId, event);
1447 mHandler.sendMessage(msg);
1448 }
1449
Robert Carrae606b42018-02-15 15:36:23 -08001450 // Requests that performTraversals be called at a
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001451 // later time to apply changes to surfaces and displays.
Craig Mautner65d11b32012-10-01 13:59:52 -07001452 private void scheduleTraversalLocked(boolean inTraversal) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001453 if (!mPendingTraversal && mWindowManagerInternal != null) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001454 mPendingTraversal = true;
Craig Mautner65d11b32012-10-01 13:59:52 -07001455 if (!inTraversal) {
1456 mHandler.sendEmptyMessage(MSG_REQUEST_TRAVERSAL);
1457 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001458 }
1459 }
1460
1461 // Runs on Handler thread.
1462 // Delivers display event notifications to callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001463 private void deliverDisplayEvent(int displayId, int event) {
1464 if (DEBUG) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001465 Slog.d(TAG, "Delivering display event: displayId="
1466 + displayId + ", event=" + event);
Jeff Brownfa25bf52012-07-23 19:26:30 -07001467 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07001468
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001469 // Grab the lock and copy the callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001470 final int count;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001471 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001472 count = mCallbacks.size();
1473 mTempCallbacks.clear();
1474 for (int i = 0; i < count; i++) {
1475 mTempCallbacks.add(mCallbacks.valueAt(i));
Craig Mautner4f67ba62012-08-02 11:23:00 -07001476 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001477 }
Craig Mautner4f67ba62012-08-02 11:23:00 -07001478
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001479 // After releasing the lock, send the notifications out.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001480 for (int i = 0; i < count; i++) {
1481 mTempCallbacks.get(i).notifyDisplayEventAsync(displayId, event);
1482 }
1483 mTempCallbacks.clear();
Craig Mautner4f67ba62012-08-02 11:23:00 -07001484 }
1485
Michael Wrightc39d47a2014-07-08 18:07:36 -07001486 private IMediaProjectionManager getProjectionService() {
1487 if (mProjectionService == null) {
1488 IBinder b = ServiceManager.getService(Context.MEDIA_PROJECTION_SERVICE);
1489 mProjectionService = IMediaProjectionManager.Stub.asInterface(b);
1490 }
1491 return mProjectionService;
1492 }
1493
Michael Wrighteef0e132017-11-21 17:57:52 +00001494 private UserManager getUserManager() {
1495 return mContext.getSystemService(UserManager.class);
1496 }
1497
Jeff Brown4ccb8232014-01-16 22:16:42 -08001498 private void dumpInternal(PrintWriter pw) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001499 pw.println("DISPLAY MANAGER (dumpsys display)");
Jeff Brownfa25bf52012-07-23 19:26:30 -07001500
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001501 synchronized (mSyncRoot) {
Jeff Brown9e316a12012-10-08 19:17:06 -07001502 pw.println(" mOnlyCode=" + mOnlyCore);
1503 pw.println(" mSafeMode=" + mSafeMode);
1504 pw.println(" mPendingTraversal=" + mPendingTraversal);
Jeff Brown037c33e2014-04-09 00:31:55 -07001505 pw.println(" mGlobalDisplayState=" + Display.stateToString(mGlobalDisplayState));
Jeff Brown9e316a12012-10-08 19:17:06 -07001506 pw.println(" mNextNonDefaultDisplayId=" + mNextNonDefaultDisplayId);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001507 pw.println(" mViewports=" + mViewports);
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -07001508 pw.println(" mDefaultDisplayDefaultColorMode=" + mDefaultDisplayDefaultColorMode);
Jeff Brown27f1d672012-10-17 18:32:34 -07001509 pw.println(" mSingleDisplayDemoMode=" + mSingleDisplayDemoMode);
Jeff Brownce468a32013-11-21 16:42:03 -08001510 pw.println(" mWifiDisplayScanRequestCount=" + mWifiDisplayScanRequestCount);
Michael Wrighteedcbf12017-08-16 23:14:54 +01001511 pw.println(" mStableDisplaySize=" + mStableDisplaySize);
Kenny Guy2047db92019-02-21 13:04:05 +00001512 pw.println(" mMinimumBrightnessCurve=" + mMinimumBrightnessCurve);
Jeff Brown9e316a12012-10-08 19:17:06 -07001513
Jeff Brownbd6e1502012-08-28 03:27:37 -07001514 IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001515 ipw.increaseIndent();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001516
1517 pw.println();
1518 pw.println("Display Adapters: size=" + mDisplayAdapters.size());
Jeff Brown848c2dc2012-08-19 20:18:08 -07001519 for (DisplayAdapter adapter : mDisplayAdapters) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001520 pw.println(" " + adapter.getName());
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001521 adapter.dumpLocked(ipw);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001522 }
Craig Mautner9de49362012-08-02 14:30:30 -07001523
Jeff Brownbd6e1502012-08-28 03:27:37 -07001524 pw.println();
1525 pw.println("Display Devices: size=" + mDisplayDevices.size());
1526 for (DisplayDevice device : mDisplayDevices) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001527 pw.println(" " + device.getDisplayDeviceInfoLocked());
1528 device.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001529 }
1530
1531 final int logicalDisplayCount = mLogicalDisplays.size();
1532 pw.println();
1533 pw.println("Logical Displays: size=" + logicalDisplayCount);
1534 for (int i = 0; i < logicalDisplayCount; i++) {
1535 int displayId = mLogicalDisplays.keyAt(i);
1536 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1537 pw.println(" Display " + displayId + ":");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001538 display.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001539 }
Jeff Brownce468a32013-11-21 16:42:03 -08001540
Michael Wrighta3dab232019-02-22 16:54:21 +00001541 pw.println();
1542 mDisplayModeDirector.dump(pw);
1543
Jeff Brownce468a32013-11-21 16:42:03 -08001544 final int callbackCount = mCallbacks.size();
1545 pw.println();
1546 pw.println("Callbacks: size=" + callbackCount);
1547 for (int i = 0; i < callbackCount; i++) {
1548 CallbackRecord callback = mCallbacks.valueAt(i);
1549 pw.println(" " + i + ": mPid=" + callback.mPid
1550 + ", mWifiDisplayScanRequested=" + callback.mWifiDisplayScanRequested);
1551 }
Jeff Brownad9ef192014-04-08 17:26:30 -07001552
1553 if (mDisplayPowerController != null) {
1554 mDisplayPowerController.dump(pw);
1555 }
Michael Wright1c9977b2016-07-12 13:30:10 -07001556
1557 pw.println();
1558 mPersistentDataStore.dump(pw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001559 }
1560 }
1561
Dan Gittik122df862018-03-28 16:59:22 +01001562 private static float[] getFloatArray(TypedArray array) {
1563 int length = array.length();
1564 float[] floatArray = new float[length];
1565 for (int i = 0; i < length; i++) {
1566 floatArray[i] = array.getFloat(i, Float.NaN);
1567 }
1568 array.recycle();
1569 return floatArray;
1570 }
1571
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001572 /**
1573 * This is the object that everything in the display manager locks on.
1574 * We make it an inner class within the {@link DisplayManagerService} to so that it is
1575 * clear that the object belongs to the display manager service and that it is
1576 * a unique object with a special purpose.
1577 */
1578 public static final class SyncRoot {
1579 }
1580
Santos Cordonee8931e2017-04-05 10:31:15 -07001581 @VisibleForTesting
1582 static class Injector {
1583 VirtualDisplayAdapter getVirtualDisplayAdapter(SyncRoot syncRoot, Context context,
1584 Handler handler, DisplayAdapter.Listener displayAdapterListener) {
1585 return new VirtualDisplayAdapter(syncRoot, context, handler, displayAdapterListener);
1586 }
Santos Cordonc22c5632017-06-21 16:03:49 -07001587
1588 long getDefaultDisplayDelayTimeout() {
1589 return WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT;
1590 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001591 }
1592
Alex Sakhartchouk879d24f2017-06-20 22:01:19 -04001593 @VisibleForTesting
1594 DisplayDeviceInfo getDisplayDeviceInfoInternal(int displayId) {
1595 synchronized (mSyncRoot) {
1596 LogicalDisplay display = mLogicalDisplays.get(displayId);
1597 if (display != null) {
1598 DisplayDevice displayDevice = display.getPrimaryDisplayDeviceLocked();
1599 return displayDevice.getDisplayDeviceInfoLocked();
1600 }
1601 return null;
1602 }
1603 }
1604
Jeff Brownbd6e1502012-08-28 03:27:37 -07001605 private final class DisplayManagerHandler extends Handler {
1606 public DisplayManagerHandler(Looper looper) {
1607 super(looper, null, true /*async*/);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001608 }
Jeff Brownbf5740e2012-08-19 23:20:02 -07001609
Jeff Brownbd6e1502012-08-28 03:27:37 -07001610 @Override
1611 public void handleMessage(Message msg) {
1612 switch (msg.what) {
Santos Cordonc22c5632017-06-21 16:03:49 -07001613 case MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS:
1614 registerDefaultDisplayAdapters();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001615 break;
1616
1617 case MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS:
1618 registerAdditionalDisplayAdapters();
1619 break;
1620
1621 case MSG_DELIVER_DISPLAY_EVENT:
1622 deliverDisplayEvent(msg.arg1, msg.arg2);
1623 break;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001624
1625 case MSG_REQUEST_TRAVERSAL:
Jeff Brown4ccb8232014-01-16 22:16:42 -08001626 mWindowManagerInternal.requestTraversalFromDisplayManager();
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001627 break;
Jeff Brownd728bf52012-09-08 18:05:28 -07001628
1629 case MSG_UPDATE_VIEWPORT: {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001630 final boolean changed;
Jeff Brownd728bf52012-09-08 18:05:28 -07001631 synchronized (mSyncRoot) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001632 changed = !mTempViewports.equals(mViewports);
1633 if (changed) {
1634 mTempViewports.clear();
1635 for (DisplayViewport d : mViewports) {
1636 mTempViewports.add(d.makeCopy());
1637 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001638 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001639 }
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001640 if (changed) {
1641 mInputManagerInternal.setDisplayViewports(mTempViewports);
1642 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001643 break;
1644 }
Kenny Guy22bd0442017-10-26 00:15:54 +01001645
Michael Wrighteef0e132017-11-21 17:57:52 +00001646 case MSG_LOAD_BRIGHTNESS_CONFIGURATION:
1647 loadBrightnessConfiguration();
1648 break;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001649 }
1650 }
1651 }
1652
1653 private final class DisplayAdapterListener implements DisplayAdapter.Listener {
1654 @Override
1655 public void onDisplayDeviceEvent(DisplayDevice device, int event) {
1656 switch (event) {
1657 case DisplayAdapter.DISPLAY_DEVICE_EVENT_ADDED:
1658 handleDisplayDeviceAdded(device);
1659 break;
1660
1661 case DisplayAdapter.DISPLAY_DEVICE_EVENT_CHANGED:
1662 handleDisplayDeviceChanged(device);
1663 break;
1664
1665 case DisplayAdapter.DISPLAY_DEVICE_EVENT_REMOVED:
1666 handleDisplayDeviceRemoved(device);
1667 break;
1668 }
1669 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001670
1671 @Override
1672 public void onTraversalRequested() {
1673 synchronized (mSyncRoot) {
Craig Mautner65d11b32012-10-01 13:59:52 -07001674 scheduleTraversalLocked(false);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001675 }
1676 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001677 }
1678
1679 private final class CallbackRecord implements DeathRecipient {
Jeff Brownce468a32013-11-21 16:42:03 -08001680 public final int mPid;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001681 private final IDisplayManagerCallback mCallback;
1682
Jeff Brownce468a32013-11-21 16:42:03 -08001683 public boolean mWifiDisplayScanRequested;
1684
Jeff Brownbd6e1502012-08-28 03:27:37 -07001685 public CallbackRecord(int pid, IDisplayManagerCallback callback) {
1686 mPid = pid;
1687 mCallback = callback;
1688 }
1689
1690 @Override
1691 public void binderDied() {
1692 if (DEBUG) {
1693 Slog.d(TAG, "Display listener for pid " + mPid + " died.");
1694 }
Jeff Brownce468a32013-11-21 16:42:03 -08001695 onCallbackDied(this);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001696 }
1697
1698 public void notifyDisplayEventAsync(int displayId, int event) {
1699 try {
1700 mCallback.onDisplayEvent(displayId, event);
1701 } catch (RemoteException ex) {
1702 Slog.w(TAG, "Failed to notify process "
1703 + mPid + " that displays changed, assuming it died.", ex);
1704 binderDied();
1705 }
1706 }
1707 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001708
Santos Cordonee8931e2017-04-05 10:31:15 -07001709 @VisibleForTesting
1710 final class BinderService extends IDisplayManager.Stub {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001711 /**
1712 * Returns information about the specified logical display.
1713 *
1714 * @param displayId The logical display id.
1715 * @return The logical display info, or null if the display does not exist. The
1716 * returned object must be treated as immutable.
1717 */
1718 @Override // Binder call
1719 public DisplayInfo getDisplayInfo(int displayId) {
1720 final int callingUid = Binder.getCallingUid();
1721 final long token = Binder.clearCallingIdentity();
1722 try {
1723 return getDisplayInfoInternal(displayId, callingUid);
1724 } finally {
1725 Binder.restoreCallingIdentity(token);
1726 }
1727 }
1728
1729 /**
1730 * Returns the list of all display ids.
1731 */
1732 @Override // Binder call
1733 public int[] getDisplayIds() {
1734 final int callingUid = Binder.getCallingUid();
1735 final long token = Binder.clearCallingIdentity();
1736 try {
1737 return getDisplayIdsInternal(callingUid);
1738 } finally {
1739 Binder.restoreCallingIdentity(token);
1740 }
1741 }
1742
Michael Wrighteedcbf12017-08-16 23:14:54 +01001743 /**
1744 * Returns the stable device display size, in pixels.
1745 */
1746 @Override // Binder call
1747 public Point getStableDisplaySize() {
1748 final long token = Binder.clearCallingIdentity();
1749 try {
1750 return getStableDisplaySizeInternal();
1751 } finally {
1752 Binder.restoreCallingIdentity(token);
1753 }
1754 }
1755
Jeff Brown4ccb8232014-01-16 22:16:42 -08001756 @Override // Binder call
1757 public void registerCallback(IDisplayManagerCallback callback) {
1758 if (callback == null) {
1759 throw new IllegalArgumentException("listener must not be null");
1760 }
1761
1762 final int callingPid = Binder.getCallingPid();
1763 final long token = Binder.clearCallingIdentity();
1764 try {
1765 registerCallbackInternal(callback, callingPid);
1766 } finally {
1767 Binder.restoreCallingIdentity(token);
1768 }
1769 }
1770
1771 @Override // Binder call
1772 public void startWifiDisplayScan() {
1773 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1774 "Permission required to start wifi display scans");
1775
1776 final int callingPid = Binder.getCallingPid();
1777 final long token = Binder.clearCallingIdentity();
1778 try {
1779 startWifiDisplayScanInternal(callingPid);
1780 } finally {
1781 Binder.restoreCallingIdentity(token);
1782 }
1783 }
1784
1785 @Override // Binder call
1786 public void stopWifiDisplayScan() {
1787 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1788 "Permission required to stop wifi display scans");
1789
1790 final int callingPid = Binder.getCallingPid();
1791 final long token = Binder.clearCallingIdentity();
1792 try {
1793 stopWifiDisplayScanInternal(callingPid);
1794 } finally {
1795 Binder.restoreCallingIdentity(token);
1796 }
1797 }
1798
1799 @Override // Binder call
1800 public void connectWifiDisplay(String address) {
1801 if (address == null) {
1802 throw new IllegalArgumentException("address must not be null");
1803 }
1804 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1805 "Permission required to connect to a wifi display");
1806
1807 final long token = Binder.clearCallingIdentity();
1808 try {
1809 connectWifiDisplayInternal(address);
1810 } finally {
1811 Binder.restoreCallingIdentity(token);
1812 }
1813 }
1814
1815 @Override // Binder call
1816 public void disconnectWifiDisplay() {
1817 // This request does not require special permissions.
1818 // Any app can request disconnection from the currently active wifi display.
1819 // This exception should no longer be needed once wifi display control moves
1820 // to the media router service.
1821
1822 final long token = Binder.clearCallingIdentity();
1823 try {
1824 disconnectWifiDisplayInternal();
1825 } finally {
1826 Binder.restoreCallingIdentity(token);
1827 }
1828 }
1829
1830 @Override // Binder call
1831 public void renameWifiDisplay(String address, String alias) {
1832 if (address == null) {
1833 throw new IllegalArgumentException("address must not be null");
1834 }
1835 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1836 "Permission required to rename to a wifi display");
1837
1838 final long token = Binder.clearCallingIdentity();
1839 try {
1840 renameWifiDisplayInternal(address, alias);
1841 } finally {
1842 Binder.restoreCallingIdentity(token);
1843 }
1844 }
1845
1846 @Override // Binder call
1847 public void forgetWifiDisplay(String address) {
1848 if (address == null) {
1849 throw new IllegalArgumentException("address must not be null");
1850 }
1851 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1852 "Permission required to forget to a wifi display");
1853
1854 final long token = Binder.clearCallingIdentity();
1855 try {
1856 forgetWifiDisplayInternal(address);
1857 } finally {
1858 Binder.restoreCallingIdentity(token);
1859 }
1860 }
1861
1862 @Override // Binder call
1863 public void pauseWifiDisplay() {
1864 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1865 "Permission required to pause a wifi display session");
1866
1867 final long token = Binder.clearCallingIdentity();
1868 try {
1869 pauseWifiDisplayInternal();
1870 } finally {
1871 Binder.restoreCallingIdentity(token);
1872 }
1873 }
1874
1875 @Override // Binder call
1876 public void resumeWifiDisplay() {
1877 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1878 "Permission required to resume a wifi display session");
1879
1880 final long token = Binder.clearCallingIdentity();
1881 try {
1882 resumeWifiDisplayInternal();
1883 } finally {
1884 Binder.restoreCallingIdentity(token);
1885 }
1886 }
1887
1888 @Override // Binder call
1889 public WifiDisplayStatus getWifiDisplayStatus() {
1890 // This request does not require special permissions.
1891 // Any app can get information about available wifi displays.
1892
1893 final long token = Binder.clearCallingIdentity();
1894 try {
1895 return getWifiDisplayStatusInternal();
1896 } finally {
1897 Binder.restoreCallingIdentity(token);
1898 }
1899 }
1900
1901 @Override // Binder call
Michael Wright1c9977b2016-07-12 13:30:10 -07001902 public void requestColorMode(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +01001903 mContext.enforceCallingOrSelfPermission(
Michael Wright1c9977b2016-07-12 13:30:10 -07001904 Manifest.permission.CONFIGURE_DISPLAY_COLOR_MODE,
1905 "Permission required to change the display color mode");
Michael Wright58e829f2015-09-15 00:13:26 +01001906 final long token = Binder.clearCallingIdentity();
1907 try {
Michael Wright1c9977b2016-07-12 13:30:10 -07001908 requestColorModeInternal(displayId, colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +01001909 } finally {
1910 Binder.restoreCallingIdentity(token);
1911 }
1912 }
1913
1914 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001915 public int createVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wrightc39d47a2014-07-08 18:07:36 -07001916 IMediaProjection projection, String packageName, String name,
Santos Cordonee8931e2017-04-05 10:31:15 -07001917 int width, int height, int densityDpi, Surface surface, int flags,
1918 String uniqueId) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001919 final int callingUid = Binder.getCallingUid();
1920 if (!validatePackageName(callingUid, packageName)) {
1921 throw new SecurityException("packageName must match the calling uid");
1922 }
Michael Wright75ee9fc2014-09-01 19:55:22 -07001923 if (callback == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001924 throw new IllegalArgumentException("appToken must not be null");
1925 }
1926 if (TextUtils.isEmpty(name)) {
1927 throw new IllegalArgumentException("name must be non-null and non-empty");
1928 }
1929 if (width <= 0 || height <= 0 || densityDpi <= 0) {
1930 throw new IllegalArgumentException("width, height, and densityDpi must be "
1931 + "greater than 0");
1932 }
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001933 if (surface != null && surface.isSingleBuffered()) {
Pablo Ceballosaff2f942016-07-29 14:49:55 -07001934 throw new IllegalArgumentException("Surface can't be single-buffered");
1935 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001936
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001937 if ((flags & VIRTUAL_DISPLAY_FLAG_PUBLIC) != 0) {
1938 flags |= VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
1939
1940 // Public displays can't be allowed to show content when locked.
Andrii Kulian7211d2e2017-01-27 15:58:05 -08001941 if ((flags & VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD) != 0) {
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001942 throw new IllegalArgumentException(
1943 "Public display must not be marked as SHOW_WHEN_LOCKED_INSECURE");
1944 }
Michael Wright6720be42014-07-29 19:14:16 -07001945 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001946 if ((flags & VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY) != 0) {
1947 flags &= ~VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
Michael Wright6720be42014-07-29 19:14:16 -07001948 }
1949
Michael Wrightc39d47a2014-07-08 18:07:36 -07001950 if (projection != null) {
1951 try {
1952 if (!getProjectionService().isValidMediaProjection(projection)) {
1953 throw new SecurityException("Invalid media projection");
1954 }
Michael Wright6720be42014-07-29 19:14:16 -07001955 flags = projection.applyVirtualDisplayFlags(flags);
Michael Wrightc39d47a2014-07-08 18:07:36 -07001956 } catch (RemoteException e) {
Michael Wright6720be42014-07-29 19:14:16 -07001957 throw new SecurityException("unable to validate media projection or flags");
Michael Wrightc39d47a2014-07-08 18:07:36 -07001958 }
1959 }
1960
Michael Wright6720be42014-07-29 19:14:16 -07001961 if (callingUid != Process.SYSTEM_UID &&
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001962 (flags & VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001963 if (!canProjectVideo(projection)) {
1964 throw new SecurityException("Requires CAPTURE_VIDEO_OUTPUT or "
1965 + "CAPTURE_SECURE_VIDEO_OUTPUT permission, or an appropriate "
1966 + "MediaProjection token in order to create a screen sharing virtual "
1967 + "display.");
1968 }
1969 }
Santos Cordonb6992f22018-01-30 14:51:20 -08001970 if (callingUid != Process.SYSTEM_UID && (flags & VIRTUAL_DISPLAY_FLAG_SECURE) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001971 if (!canProjectSecureVideo(projection)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001972 throw new SecurityException("Requires CAPTURE_SECURE_VIDEO_OUTPUT "
Michael Wrightc39d47a2014-07-08 18:07:36 -07001973 + "or an appropriate MediaProjection token to create a "
1974 + "secure virtual display.");
Jeff Brown4ccb8232014-01-16 22:16:42 -08001975 }
1976 }
1977
1978 final long token = Binder.clearCallingIdentity();
1979 try {
Santos Cordonee8931e2017-04-05 10:31:15 -07001980 return createVirtualDisplayInternal(callback, projection, callingUid, packageName,
1981 name, width, height, densityDpi, surface, flags, uniqueId);
Jeff Brown4ccb8232014-01-16 22:16:42 -08001982 } finally {
1983 Binder.restoreCallingIdentity(token);
1984 }
1985 }
1986
1987 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001988 public void resizeVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wright01e840f2014-06-26 16:03:25 -07001989 int width, int height, int densityDpi) {
1990 final long token = Binder.clearCallingIdentity();
1991 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001992 resizeVirtualDisplayInternal(callback.asBinder(), width, height, densityDpi);
Michael Wright01e840f2014-06-26 16:03:25 -07001993 } finally {
1994 Binder.restoreCallingIdentity(token);
1995 }
1996 }
1997
1998 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001999 public void setVirtualDisplaySurface(IVirtualDisplayCallback callback, Surface surface) {
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07002000 if (surface != null && surface.isSingleBuffered()) {
2001 throw new IllegalArgumentException("Surface can't be single-buffered");
2002 }
Jeff Brown92207df2014-04-16 13:16:07 -07002003 final long token = Binder.clearCallingIdentity();
2004 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07002005 setVirtualDisplaySurfaceInternal(callback.asBinder(), surface);
Jeff Brown92207df2014-04-16 13:16:07 -07002006 } finally {
2007 Binder.restoreCallingIdentity(token);
2008 }
2009 }
2010
2011 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07002012 public void releaseVirtualDisplay(IVirtualDisplayCallback callback) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08002013 final long token = Binder.clearCallingIdentity();
2014 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07002015 releaseVirtualDisplayInternal(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -08002016 } finally {
2017 Binder.restoreCallingIdentity(token);
2018 }
2019 }
2020
2021 @Override // Binder call
chaviwda4c6942018-11-07 15:52:56 -08002022 public void setVirtualDisplayState(IVirtualDisplayCallback callback, boolean isOn) {
2023 final long token = Binder.clearCallingIdentity();
2024 try {
2025 setVirtualDisplayStateInternal(callback.asBinder(), isOn);
2026 } finally {
2027 Binder.restoreCallingIdentity(token);
2028 }
2029 }
2030
2031 @Override // Binder call
Jeff Brown4ccb8232014-01-16 22:16:42 -08002032 public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06002033 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
Jeff Brown4ccb8232014-01-16 22:16:42 -08002034
2035 final long token = Binder.clearCallingIdentity();
2036 try {
2037 dumpInternal(pw);
2038 } finally {
2039 Binder.restoreCallingIdentity(token);
2040 }
2041 }
2042
Kenny Guy22bd0442017-10-26 00:15:54 +01002043 @Override // Binder call
Kenny Guy29aa30e2017-11-30 13:43:46 +00002044 public ParceledListSlice<BrightnessChangeEvent> getBrightnessEvents(String callingPackage) {
Kenny Guy22bd0442017-10-26 00:15:54 +01002045 mContext.enforceCallingOrSelfPermission(
2046 Manifest.permission.BRIGHTNESS_SLIDER_USAGE,
2047 "Permission to read brightness events.");
Kenny Guy29aa30e2017-11-30 13:43:46 +00002048
2049 final int callingUid = Binder.getCallingUid();
2050 AppOpsManager appOpsManager = mContext.getSystemService(AppOpsManager.class);
Jeff Sharkeyd9311192018-04-16 16:50:34 +00002051 final int mode = appOpsManager.noteOp(AppOpsManager.OP_GET_USAGE_STATS,
Kenny Guy29aa30e2017-11-30 13:43:46 +00002052 callingUid, callingPackage);
2053 final boolean hasUsageStats;
2054 if (mode == AppOpsManager.MODE_DEFAULT) {
2055 // The default behavior here is to check if PackageManager has given the app
2056 // permission.
2057 hasUsageStats = mContext.checkCallingPermission(
2058 Manifest.permission.PACKAGE_USAGE_STATS)
2059 == PackageManager.PERMISSION_GRANTED;
2060 } else {
2061 hasUsageStats = mode == AppOpsManager.MODE_ALLOWED;
2062 }
2063
2064 final int userId = UserHandle.getUserId(callingUid);
Kenny Guy22bd0442017-10-26 00:15:54 +01002065 final long token = Binder.clearCallingIdentity();
2066 try {
Michael Wright144aac92017-12-21 18:37:41 +00002067 synchronized (mSyncRoot) {
2068 return mDisplayPowerController.getBrightnessEvents(userId, hasUsageStats);
2069 }
Kenny Guy22bd0442017-10-26 00:15:54 +01002070 } finally {
2071 Binder.restoreCallingIdentity(token);
2072 }
2073 }
2074
Michael Wrighteef0e132017-11-21 17:57:52 +00002075 @Override // Binder call
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002076 public ParceledListSlice<AmbientBrightnessDayStats> getAmbientBrightnessStats() {
2077 mContext.enforceCallingOrSelfPermission(
2078 Manifest.permission.ACCESS_AMBIENT_LIGHT_STATS,
2079 "Permission required to to access ambient light stats.");
2080 final int callingUid = Binder.getCallingUid();
2081 final int userId = UserHandle.getUserId(callingUid);
2082 final long token = Binder.clearCallingIdentity();
2083 try {
2084 synchronized (mSyncRoot) {
2085 return mDisplayPowerController.getAmbientBrightnessStats(userId);
2086 }
2087 } finally {
2088 Binder.restoreCallingIdentity(token);
2089 }
2090 }
2091
2092 @Override // Binder call
Michael Wrighteef0e132017-11-21 17:57:52 +00002093 public void setBrightnessConfigurationForUser(
Kenny Guy05ce8092018-01-17 13:44:20 +00002094 BrightnessConfiguration c, @UserIdInt int userId, String packageName) {
Michael Wrighteef0e132017-11-21 17:57:52 +00002095 mContext.enforceCallingOrSelfPermission(
2096 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
2097 "Permission required to change the display's brightness configuration");
2098 if (userId != UserHandle.getCallingUserId()) {
2099 mContext.enforceCallingOrSelfPermission(
2100 Manifest.permission.INTERACT_ACROSS_USERS,
2101 "Permission required to change the display brightness"
2102 + " configuration of another user");
2103 }
Kenny Guy05ce8092018-01-17 13:44:20 +00002104 if (packageName != null && !validatePackageName(getCallingUid(), packageName)) {
2105 packageName = null;
2106 }
Michael Wrighteef0e132017-11-21 17:57:52 +00002107 final long token = Binder.clearCallingIdentity();
2108 try {
Kenny Guy05ce8092018-01-17 13:44:20 +00002109 setBrightnessConfigurationForUserInternal(c, userId, packageName);
Michael Wrighteef0e132017-11-21 17:57:52 +00002110 } finally {
2111 Binder.restoreCallingIdentity(token);
2112 }
2113 }
2114
Michael Wrightd8460232018-01-16 18:04:59 +00002115 @Override // Binder call
Kenny Guy6d1009f2018-03-14 14:28:23 +00002116 public BrightnessConfiguration getBrightnessConfigurationForUser(int userId) {
2117 mContext.enforceCallingOrSelfPermission(
2118 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
2119 "Permission required to read the display's brightness configuration");
2120 if (userId != UserHandle.getCallingUserId()) {
2121 mContext.enforceCallingOrSelfPermission(
2122 Manifest.permission.INTERACT_ACROSS_USERS,
2123 "Permission required to read the display brightness"
2124 + " configuration of another user");
2125 }
2126 final long token = Binder.clearCallingIdentity();
2127 try {
2128 final int userSerial = getUserManager().getUserSerialNumber(userId);
2129 synchronized (mSyncRoot) {
2130 BrightnessConfiguration config =
2131 mPersistentDataStore.getBrightnessConfiguration(userSerial);
2132 if (config == null) {
2133 config = mDisplayPowerController.getDefaultBrightnessConfiguration();
2134 }
2135 return config;
2136 }
2137 } finally {
2138 Binder.restoreCallingIdentity(token);
2139 }
2140 }
2141
2142 @Override // Binder call
2143 public BrightnessConfiguration getDefaultBrightnessConfiguration() {
2144 mContext.enforceCallingOrSelfPermission(
2145 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
2146 "Permission required to read the display's default brightness configuration");
2147 final long token = Binder.clearCallingIdentity();
2148 try {
2149 synchronized (mSyncRoot) {
2150 return mDisplayPowerController.getDefaultBrightnessConfiguration();
2151 }
2152 } finally {
2153 Binder.restoreCallingIdentity(token);
2154 }
2155 }
2156
2157 @Override // Binder call
Michael Wrightd8460232018-01-16 18:04:59 +00002158 public void setTemporaryBrightness(int brightness) {
2159 mContext.enforceCallingOrSelfPermission(
2160 Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS,
2161 "Permission required to set the display's brightness");
2162 final long token = Binder.clearCallingIdentity();
2163 try {
2164 synchronized (mSyncRoot) {
2165 mDisplayPowerController.setTemporaryBrightness(brightness);
2166 }
2167 } finally {
2168 Binder.restoreCallingIdentity(token);
2169 }
2170 }
2171
2172 @Override // Binder call
2173 public void setTemporaryAutoBrightnessAdjustment(float adjustment) {
2174 mContext.enforceCallingOrSelfPermission(
2175 Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS,
2176 "Permission required to set the display's auto brightness adjustment");
2177 final long token = Binder.clearCallingIdentity();
2178 try {
2179 synchronized (mSyncRoot) {
2180 mDisplayPowerController.setTemporaryAutoBrightnessAdjustment(adjustment);
2181 }
2182 } finally {
2183 Binder.restoreCallingIdentity(token);
2184 }
2185 }
2186
Dan Gittik7a32fba2018-03-28 12:19:38 +01002187 @Override // Binder call
2188 public void onShellCommand(FileDescriptor in, FileDescriptor out,
2189 FileDescriptor err, String[] args, ShellCallback callback,
2190 ResultReceiver resultReceiver) {
2191 final long token = Binder.clearCallingIdentity();
2192 try {
2193 DisplayManagerShellCommand command = new DisplayManagerShellCommand(this);
2194 command.exec(this, in, out, err, args, callback, resultReceiver);
2195 } finally {
2196 Binder.restoreCallingIdentity(token);
2197 }
2198 }
2199
Dan Gittik122df862018-03-28 16:59:22 +01002200 @Override // Binder call
2201 public Curve getMinimumBrightnessCurve() {
2202 final long token = Binder.clearCallingIdentity();
2203 try {
2204 return getMinimumBrightnessCurveInternal();
2205 } finally {
2206 Binder.restoreCallingIdentity(token);
2207 }
2208 }
2209
Peiyong Lin277eaff2019-01-16 16:18:22 -08002210 @Override // Binder call
2211 public int getPreferredWideGamutColorSpaceId() {
2212 final long token = Binder.clearCallingIdentity();
2213 try {
2214 return getPreferredWideGamutColorSpaceIdInternal();
2215 } finally {
2216 Binder.restoreCallingIdentity(token);
2217 }
2218 }
2219
Dan Gittik7a32fba2018-03-28 12:19:38 +01002220 void setBrightness(int brightness) {
2221 Settings.System.putIntForUser(mContext.getContentResolver(),
2222 Settings.System.SCREEN_BRIGHTNESS, brightness, UserHandle.USER_CURRENT);
2223 }
2224
2225 void resetBrightnessConfiguration() {
2226 setBrightnessConfigurationForUserInternal(null, mContext.getUserId(),
2227 mContext.getPackageName());
2228 }
2229
Dan Gittika5a2d632019-01-09 14:25:29 +00002230 void setAutoBrightnessLoggingEnabled(boolean enabled) {
2231 if (mDisplayPowerController != null) {
2232 synchronized (mSyncRoot) {
2233 mDisplayPowerController.setAutoBrightnessLoggingEnabled(enabled);
2234 }
2235 }
2236 }
2237
Dan Gittik8dbd7e92018-12-03 15:35:53 +00002238 void setDisplayWhiteBalanceLoggingEnabled(boolean enabled) {
2239 if (mDisplayPowerController != null) {
2240 synchronized (mSyncRoot) {
2241 mDisplayPowerController.setDisplayWhiteBalanceLoggingEnabled(enabled);
2242 }
2243 }
2244 }
2245
2246 void setAmbientColorTemperatureOverride(float cct) {
2247 if (mDisplayPowerController != null) {
2248 synchronized (mSyncRoot) {
2249 mDisplayPowerController.setAmbientColorTemperatureOverride(cct);
2250 }
2251 }
2252 }
2253
Jeff Brown4ccb8232014-01-16 22:16:42 -08002254 private boolean validatePackageName(int uid, String packageName) {
2255 if (packageName != null) {
2256 String[] packageNames = mContext.getPackageManager().getPackagesForUid(uid);
2257 if (packageNames != null) {
2258 for (String n : packageNames) {
2259 if (n.equals(packageName)) {
2260 return true;
2261 }
2262 }
2263 }
2264 }
2265 return false;
2266 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07002267
2268 private boolean canProjectVideo(IMediaProjection projection) {
2269 if (projection != null) {
2270 try {
2271 if (projection.canProjectVideo()) {
2272 return true;
2273 }
2274 } catch (RemoteException e) {
2275 Slog.e(TAG, "Unable to query projection service for permissions", e);
2276 }
2277 }
2278 if (mContext.checkCallingPermission(
2279 android.Manifest.permission.CAPTURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07002280 == PackageManager.PERMISSION_GRANTED) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07002281 return true;
2282 }
2283 return canProjectSecureVideo(projection);
2284 }
2285
2286 private boolean canProjectSecureVideo(IMediaProjection projection) {
2287 if (projection != null) {
2288 try {
2289 if (projection.canProjectSecureVideo()){
2290 return true;
2291 }
2292 } catch (RemoteException e) {
2293 Slog.e(TAG, "Unable to query projection service for permissions", e);
2294 }
2295 }
2296 return mContext.checkCallingPermission(
2297 android.Manifest.permission.CAPTURE_SECURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07002298 == PackageManager.PERMISSION_GRANTED;
Michael Wrightc39d47a2014-07-08 18:07:36 -07002299 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08002300 }
2301
2302 private final class LocalService extends DisplayManagerInternal {
2303 @Override
Jeff Brown037c33e2014-04-09 00:31:55 -07002304 public void initPowerManagement(final DisplayPowerCallbacks callbacks, Handler handler,
Jeff Brownad9ef192014-04-08 17:26:30 -07002305 SensorManager sensorManager) {
2306 synchronized (mSyncRoot) {
Jeff Brown037c33e2014-04-09 00:31:55 -07002307 DisplayBlanker blanker = new DisplayBlanker() {
2308 @Override
Jeff Brown5d6443b2015-04-10 20:15:01 -07002309 public void requestDisplayState(int state, int brightness) {
Jeff Brown037c33e2014-04-09 00:31:55 -07002310 // The order of operations is important for legacy reasons.
2311 if (state == Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07002312 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07002313 }
2314
2315 callbacks.onDisplayStateChange(state);
2316
2317 if (state != Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07002318 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07002319 }
2320 }
2321 };
Jeff Brownad9ef192014-04-08 17:26:30 -07002322 mDisplayPowerController = new DisplayPowerController(
Jeff Brown037c33e2014-04-09 00:31:55 -07002323 mContext, callbacks, handler, sensorManager, blanker);
Jeff Brownad9ef192014-04-08 17:26:30 -07002324 }
Michael Wrighteef0e132017-11-21 17:57:52 +00002325
2326 mHandler.sendEmptyMessage(MSG_LOAD_BRIGHTNESS_CONFIGURATION);
Jeff Brownad9ef192014-04-08 17:26:30 -07002327 }
2328
2329 @Override
2330 public boolean requestPowerState(DisplayPowerRequest request,
2331 boolean waitForNegativeProximity) {
Michael Wrighteef0e132017-11-21 17:57:52 +00002332 synchronized (mSyncRoot) {
2333 return mDisplayPowerController.requestPowerState(request,
2334 waitForNegativeProximity);
2335 }
Jeff Brownad9ef192014-04-08 17:26:30 -07002336 }
2337
2338 @Override
2339 public boolean isProximitySensorAvailable() {
Michael Wrighteef0e132017-11-21 17:57:52 +00002340 synchronized (mSyncRoot) {
2341 return mDisplayPowerController.isProximitySensorAvailable();
2342 }
Jeff Brownad9ef192014-04-08 17:26:30 -07002343 }
2344
2345 @Override
Riddle Hsu654a6f92018-07-13 22:59:36 +08002346 public boolean screenshot(int displayId, Surface outSurface) {
Dominik Laskowskie912ae32019-01-26 11:19:36 -08002347 return screenshotInternal(displayId, outSurface);
Riddle Hsu654a6f92018-07-13 22:59:36 +08002348 }
2349
2350 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08002351 public DisplayInfo getDisplayInfo(int displayId) {
2352 return getDisplayInfoInternal(displayId, Process.myUid());
2353 }
2354
2355 @Override
2356 public void registerDisplayTransactionListener(DisplayTransactionListener listener) {
2357 if (listener == null) {
2358 throw new IllegalArgumentException("listener must not be null");
2359 }
2360
2361 registerDisplayTransactionListenerInternal(listener);
2362 }
2363
2364 @Override
2365 public void unregisterDisplayTransactionListener(DisplayTransactionListener listener) {
2366 if (listener == null) {
2367 throw new IllegalArgumentException("listener must not be null");
2368 }
2369
2370 unregisterDisplayTransactionListenerInternal(listener);
2371 }
2372
2373 @Override
2374 public void setDisplayInfoOverrideFromWindowManager(int displayId, DisplayInfo info) {
2375 setDisplayInfoOverrideFromWindowManagerInternal(displayId, info);
2376 }
2377
2378 @Override
Andrii Kuliancd097992017-03-23 18:31:59 -07002379 public void getNonOverrideDisplayInfo(int displayId, DisplayInfo outInfo) {
2380 getNonOverrideDisplayInfoInternal(displayId, outInfo);
2381 }
2382
2383 @Override
Robert Carrae606b42018-02-15 15:36:23 -08002384 public void performTraversal(SurfaceControl.Transaction t) {
2385 performTraversalInternal(t);
Jeff Brown4ccb8232014-01-16 22:16:42 -08002386 }
2387
2388 @Override
Michael Wright3f145a22014-07-22 19:46:03 -07002389 public void setDisplayProperties(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07002390 float requestedRefreshRate, int requestedMode, boolean inTraversal) {
2391 setDisplayPropertiesInternal(displayId, hasContent, requestedRefreshRate,
2392 requestedMode, inTraversal);
Jeff Brown4ccb8232014-01-16 22:16:42 -08002393 }
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08002394
2395 @Override
2396 public void setDisplayOffsets(int displayId, int x, int y) {
2397 setDisplayOffsetsInternal(displayId, x, y);
2398 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002399
2400 @Override
Sam Lin4c3ac2b2019-02-18 04:50:26 -08002401 public void setDisplayScalingDisabled(int displayId, boolean disableScaling) {
2402 setDisplayScalingDisabledInternal(displayId, disableScaling);
2403 }
2404
2405 @Override
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002406 public void setDisplayAccessUIDs(SparseArray<IntArray> newDisplayAccessUIDs) {
2407 setDisplayAccessUIDsInternal(newDisplayAccessUIDs);
2408 }
2409
2410 @Override
2411 public boolean isUidPresentOnDisplay(int uid, int displayId) {
2412 return isUidPresentOnDisplayInternal(uid, displayId);
2413 }
Kenny Guycfe7b702017-11-14 21:04:58 +00002414
2415 @Override
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002416 public void persistBrightnessTrackerState() {
Michael Wright144aac92017-12-21 18:37:41 +00002417 synchronized (mSyncRoot) {
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002418 mDisplayPowerController.persistBrightnessTrackerState();
Michael Wright144aac92017-12-21 18:37:41 +00002419 }
Kenny Guycfe7b702017-11-14 21:04:58 +00002420 }
Adrian Roose1d68cd2018-01-17 12:54:50 +01002421
2422 @Override
2423 public void onOverlayChanged() {
2424 synchronized (mSyncRoot) {
Adrian Roos898ec382018-01-17 12:54:50 +01002425 for (int i = 0; i < mDisplayDevices.size(); i++) {
2426 mDisplayDevices.get(i).onOverlayChangedLocked();
Adrian Roose1d68cd2018-01-17 12:54:50 +01002427 }
2428 }
2429 }
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07002430
2431 @Override
2432 public DisplayedContentSamplingAttributes getDisplayedContentSamplingAttributes(
2433 int displayId) {
2434 return getDisplayedContentSamplingAttributesInternal(displayId);
2435 }
2436
2437 @Override
2438 public boolean setDisplayedContentSamplingEnabled(
2439 int displayId, boolean enable, int componentMask, int maxFrames) {
2440 return setDisplayedContentSamplingEnabledInternal(
2441 displayId, enable, componentMask, maxFrames);
2442 }
2443
2444 @Override
2445 public DisplayedContentSample getDisplayedContentSample(int displayId,
2446 long maxFrames, long timestamp) {
2447 return getDisplayedContentSampleInternal(displayId, maxFrames, timestamp);
2448 }
2449
Jeff Brown4ccb8232014-01-16 22:16:42 -08002450 }
Michael Wrighta3dab232019-02-22 16:54:21 +00002451
2452 class AllowedDisplayModeObserver implements DisplayModeDirector.Listener {
2453 public void onAllowedDisplayModesChanged() {
2454 onAllowedDisplayModesChangedInternal();
2455 }
2456 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07002457}