blob: 01d19c04d97e26c8a239ec7aaea9453cea2d5fd5 [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;
Peiyong Lin277eaff2019-01-16 16:18:22 -080099import com.android.server.display.ColorDisplayService.ColorDisplayServiceInternal;
Jorim Jaggi21c39a72017-10-20 15:47:51 +0200100import com.android.server.wm.SurfaceAnimationThread;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100101import com.android.server.wm.WindowManagerInternal;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700102
103import java.io.FileDescriptor;
104import java.io.PrintWriter;
105import java.util.ArrayList;
Jeff Browna506a6e2013-06-04 00:02:38 -0700106import java.util.Arrays;
Jeff Browne75926d2014-09-18 15:24:49 -0700107import java.util.List;
Jeff Brown7f3994e2012-12-04 14:04:28 -0800108import java.util.concurrent.CopyOnWriteArrayList;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700109
110/**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700111 * Manages attached displays.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700112 * <p>
Jeff Brownbd6e1502012-08-28 03:27:37 -0700113 * The {@link DisplayManagerService} manages the global lifecycle of displays,
114 * decides how to configure logical displays based on the physical display devices currently
115 * attached, sends notifications to the system and to applications when the state
116 * changes, and so on.
117 * </p><p>
118 * The display manager service relies on a collection of {@link DisplayAdapter} components,
119 * for discovering and configuring physical display devices attached to the system.
120 * There are separate display adapters for each manner that devices are attached:
121 * one display adapter for built-in local displays, one for simulated non-functional
122 * displays when the system is headless, one for simulated overlay displays used for
123 * development, one for wifi displays, etc.
124 * </p><p>
125 * Display adapters are only weakly coupled to the display manager service.
126 * Display adapters communicate changes in display device state to the display manager
Craig Mautner722285e2012-09-07 13:55:58 -0700127 * service asynchronously via a {@link DisplayAdapter.Listener} registered
Jeff Brownbd6e1502012-08-28 03:27:37 -0700128 * by the display manager service. This separation of concerns is important for
129 * two main reasons. First, it neatly encapsulates the responsibilities of these
130 * two classes: display adapters handle individual display devices whereas
131 * the display manager service handles the global state. Second, it eliminates
132 * the potential for deadlocks resulting from asynchronous display device discovery.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700133 * </p>
134 *
135 * <h3>Synchronization</h3>
136 * <p>
137 * Because the display manager may be accessed by multiple threads, the synchronization
138 * story gets a little complicated. In particular, the window manager may call into
139 * the display manager while holding a surface transaction with the expectation that
140 * it can apply changes immediately. Unfortunately, that means we can't just do
141 * everything asynchronously (*grump*).
Jeff Brownbd6e1502012-08-28 03:27:37 -0700142 * </p><p>
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700143 * To make this work, all of the objects that belong to the display manager must
144 * use the same lock. We call this lock the synchronization root and it has a unique
145 * type {@link DisplayManagerService.SyncRoot}. Methods that require this lock are
146 * named with the "Locked" suffix.
147 * </p><p>
148 * Where things get tricky is that the display manager is not allowed to make
149 * any potentially reentrant calls, especially into the window manager. We generally
150 * avoid this by making all potentially reentrant out-calls asynchronous.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700151 * </p>
152 */
Jeff Brown4ccb8232014-01-16 22:16:42 -0800153public final class DisplayManagerService extends SystemService {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700154 private static final String TAG = "DisplayManagerService";
Jeff Brownbd6e1502012-08-28 03:27:37 -0700155 private static final boolean DEBUG = false;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700156
Jeff Brownbbd28a22012-09-20 16:47:15 -0700157 // When this system property is set to 0, WFD is forcibly disabled on boot.
158 // When this system property is set to 1, WFD is forcibly enabled on boot.
159 // Otherwise WFD is enabled according to the value of config_enableWifiDisplay.
160 private static final String FORCE_WIFI_DISPLAY_ENABLE = "persist.debug.wfd.enable";
161
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200162 private static final String PROP_DEFAULT_DISPLAY_TOP_INSET = "persist.sys.displayinset.top";
163
Jeff Brownbd6e1502012-08-28 03:27:37 -0700164 private static final long WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT = 10000;
165
Santos Cordonc22c5632017-06-21 16:03:49 -0700166 private static final int MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS = 1;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700167 private static final int MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS = 2;
168 private static final int MSG_DELIVER_DISPLAY_EVENT = 3;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700169 private static final int MSG_REQUEST_TRAVERSAL = 4;
Jeff Brownd728bf52012-09-08 18:05:28 -0700170 private static final int MSG_UPDATE_VIEWPORT = 5;
Kenny Guy22bd0442017-10-26 00:15:54 +0100171 private static final int MSG_REGISTER_BRIGHTNESS_TRACKER = 6;
Michael Wrighteef0e132017-11-21 17:57:52 +0000172 private static final int MSG_LOAD_BRIGHTNESS_CONFIGURATION = 7;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700173
Jeff Brownb880d882014-02-10 19:47:07 -0800174 private final Context mContext;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700175 private final DisplayManagerHandler mHandler;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700176 private final Handler mUiHandler;
177 private final DisplayAdapterListener mDisplayAdapterListener;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800178 private WindowManagerInternal mWindowManagerInternal;
179 private InputManagerInternal mInputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -0700180 private IMediaProjectionManager mProjectionService;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700181
182 // The synchronization root for the display manager.
183 // This lock guards most of the display manager's state.
Craig Mautner0bf6ec92012-12-18 08:33:27 -0800184 // NOTE: This is synchronized on while holding WindowManagerService.mWindowMap so never call
185 // into WindowManagerService methods that require mWindowMap while holding this unless you are
186 // very very sure that no deadlock can occur.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700187 private final SyncRoot mSyncRoot = new SyncRoot();
188
189 // True if in safe mode.
190 // This option may disable certain display adapters.
191 public boolean mSafeMode;
192
193 // True if we are in a special boot mode where only core applications and
194 // services should be started. This option may disable certain display adapters.
195 public boolean mOnlyCore;
196
Jeff Brown27f1d672012-10-17 18:32:34 -0700197 // True if the display manager service should pretend there is only one display
198 // and only tell applications about the existence of the default logical display.
199 // The display manager can still mirror content to secondary displays but applications
200 // cannot present unique content on those displays.
201 // Used for demonstration purposes only.
202 private final boolean mSingleDisplayDemoMode;
203
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700204 // All callback records indexed by calling process id.
205 public final SparseArray<CallbackRecord> mCallbacks =
Jeff Brownbd6e1502012-08-28 03:27:37 -0700206 new SparseArray<CallbackRecord>();
Jeff Brownfa25bf52012-07-23 19:26:30 -0700207
Jeff Brownbd6e1502012-08-28 03:27:37 -0700208 // List of all currently registered display adapters.
209 private final ArrayList<DisplayAdapter> mDisplayAdapters = new ArrayList<DisplayAdapter>();
210
211 // List of all currently connected display devices.
212 private final ArrayList<DisplayDevice> mDisplayDevices = new ArrayList<DisplayDevice>();
213
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700214 // List of all logical displays indexed by logical display id.
215 private final SparseArray<LogicalDisplay> mLogicalDisplays =
216 new SparseArray<LogicalDisplay>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700217 private int mNextNonDefaultDisplayId = Display.DEFAULT_DISPLAY + 1;
218
Jeff Brown7f3994e2012-12-04 14:04:28 -0800219 // List of all display transaction listeners.
220 private final CopyOnWriteArrayList<DisplayTransactionListener> mDisplayTransactionListeners =
221 new CopyOnWriteArrayList<DisplayTransactionListener>();
222
Jeff Brownad9ef192014-04-08 17:26:30 -0700223 // Display power controller.
224 private DisplayPowerController mDisplayPowerController;
225
Jeff Brown037c33e2014-04-09 00:31:55 -0700226 // The overall display state, independent of changes that might influence one
227 // display or another in particular.
Jeff Brown5d6443b2015-04-10 20:15:01 -0700228 private int mGlobalDisplayState = Display.STATE_ON;
229
230 // The overall display brightness.
231 // For now, this only applies to the built-in display but we may split it up eventually.
232 private int mGlobalDisplayBrightness = PowerManager.BRIGHTNESS_DEFAULT;
Jeff Brown9e316a12012-10-08 19:17:06 -0700233
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700234 // Set to true when there are pending display changes that have yet to be applied
235 // to the surface flinger state.
236 private boolean mPendingTraversal;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700237
Jeff Browne08ae382012-09-07 20:36:36 -0700238 // The Wifi display adapter, or null if not registered.
239 private WifiDisplayAdapter mWifiDisplayAdapter;
240
Jeff Brownce468a32013-11-21 16:42:03 -0800241 // The number of active wifi display scan requests.
242 private int mWifiDisplayScanRequestCount;
243
Jeff Browna506a6e2013-06-04 00:02:38 -0700244 // The virtual display adapter, or null if not registered.
245 private VirtualDisplayAdapter mVirtualDisplayAdapter;
246
Michael Wrighteef0e132017-11-21 17:57:52 +0000247 // The User ID of the current user
248 private @UserIdInt int mCurrentUserId;
249
Michael Wrighteedcbf12017-08-16 23:14:54 +0100250 // The stable device screen height and width. These are not tied to a specific display, even
251 // the default display, because they need to be stable over the course of the device's entire
252 // life, even if the default display changes (e.g. a new monitor is plugged into a PC-like
253 // device).
254 private Point mStableDisplaySize = new Point();
255
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200256 // The top inset of the default display.
257 // This gets persisted so that the boot animation knows how to transition from the display's
258 // full size to the size configured by the user. Right now we only persist and animate the top
259 // inset, but theoretically we could do it for all of them.
260 private int mDefaultDisplayTopInset;
261
Jeff Brownd728bf52012-09-08 18:05:28 -0700262 // Viewports of the default display and the display that should receive touch
263 // input from an external source. Used by the input system.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100264 @GuardedBy("mSyncRoot")
265 private final ArrayList<DisplayViewport> mViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700266
Jeff Brown89d55462012-09-19 11:33:42 -0700267 // Persistent data store for all internal settings maintained by the display manager service.
268 private final PersistentDataStore mPersistentDataStore = new PersistentDataStore();
269
Jeff Brownbd6e1502012-08-28 03:27:37 -0700270 // Temporary callback list, used when sending display events to applications.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700271 // May be used outside of the lock but only on the handler thread.
272 private final ArrayList<CallbackRecord> mTempCallbacks = new ArrayList<CallbackRecord>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700273
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700274 // Temporary display info, used for comparing display configurations.
275 private final DisplayInfo mTempDisplayInfo = new DisplayInfo();
276
Jeff Brownd728bf52012-09-08 18:05:28 -0700277 // Temporary viewports, used when sending new viewport information to the
278 // input system. May be used outside of the lock but only on the handler thread.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100279 private final ArrayList<DisplayViewport> mTempViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700280
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700281 // The default color mode for default displays. Overrides the usual
282 // Display.Display.COLOR_MODE_DEFAULT for displays with the
283 // DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY flag set.
284 private final int mDefaultDisplayDefaultColorMode;
285
Jeff Browne75926d2014-09-18 15:24:49 -0700286 // Temporary list of deferred work to perform when setting the display state.
287 // Only used by requestDisplayState. The field is self-synchronized and only
288 // intended for use inside of the requestGlobalDisplayStateInternal function.
289 private final ArrayList<Runnable> mTempDisplayStateWorkQueue = new ArrayList<Runnable>();
290
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800291 // Lists of UIDs that are present on the displays. Maps displayId -> array of UIDs.
292 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
293
Santos Cordonee8931e2017-04-05 10:31:15 -0700294 private final Injector mInjector;
295
Dan Gittik122df862018-03-28 16:59:22 +0100296 // The minimum brightness curve, which guarantess that any brightness curve that dips below it
297 // is rejected by the system.
298 private final Curve mMinimumBrightnessCurve;
299 private final Spline mMinimumBrightnessSpline;
Peiyong Lin277eaff2019-01-16 16:18:22 -0800300 private final ColorSpace mWideColorSpace;
Dan Gittik122df862018-03-28 16:59:22 +0100301
Jeff Brownb880d882014-02-10 19:47:07 -0800302 public DisplayManagerService(Context context) {
Santos Cordonee8931e2017-04-05 10:31:15 -0700303 this(context, new Injector());
304 }
305
306 @VisibleForTesting
307 DisplayManagerService(Context context, Injector injector) {
Jeff Brownb880d882014-02-10 19:47:07 -0800308 super(context);
Santos Cordonee8931e2017-04-05 10:31:15 -0700309 mInjector = injector;
Jeff Brownb880d882014-02-10 19:47:07 -0800310 mContext = context;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800311 mHandler = new DisplayManagerHandler(DisplayThread.get().getLooper());
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700312 mUiHandler = UiThread.getHandler();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700313 mDisplayAdapterListener = new DisplayAdapterListener();
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
326 PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
327 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 Wrighteedcbf12017-08-16 23:14:54 +0100351 synchronized(mSyncRoot) {
352 mPersistentDataStore.loadIfNeeded();
353 loadStableDisplayValuesLocked();
354 }
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());
Justin Klaassen22eb1992016-07-11 20:52:23 -0700360 publishLocalService(DisplayTransformManager.class, new DisplayTransformManager());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800361 }
362
363 @Override
364 public void onBootPhase(int phase) {
365 if (phase == PHASE_WAIT_FOR_DEFAULT_DISPLAY) {
366 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700367 long timeout = SystemClock.uptimeMillis()
368 + mInjector.getDefaultDisplayDelayTimeout();
369 while (mLogicalDisplays.get(Display.DEFAULT_DISPLAY) == null ||
370 mVirtualDisplayAdapter == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800371 long delay = timeout - SystemClock.uptimeMillis();
372 if (delay <= 0) {
373 throw new RuntimeException("Timeout waiting for default display "
Santos Cordonc22c5632017-06-21 16:03:49 -0700374 + "to be initialized. DefaultDisplay="
375 + mLogicalDisplays.get(Display.DEFAULT_DISPLAY)
376 + ", mVirtualDisplayAdapter=" + mVirtualDisplayAdapter);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800377 }
378 if (DEBUG) {
379 Slog.d(TAG, "waitForDefaultDisplay: waiting, timeout=" + delay);
380 }
381 try {
382 mSyncRoot.wait(delay);
383 } catch (InterruptedException ex) {
384 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700385 }
386 }
387 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700388 }
389
Michael Wrighteef0e132017-11-21 17:57:52 +0000390 @Override
391 public void onSwitchUser(@UserIdInt int newUserId) {
392 final int userSerial = getUserManager().getUserSerialNumber(newUserId);
393 synchronized (mSyncRoot) {
394 if (mCurrentUserId != newUserId) {
395 mCurrentUserId = newUserId;
396 BrightnessConfiguration config =
397 mPersistentDataStore.getBrightnessConfiguration(userSerial);
398 mDisplayPowerController.setBrightnessConfiguration(config);
399 }
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +0000400 mDisplayPowerController.onSwitchUser(newUserId);
Michael Wrighteef0e132017-11-21 17:57:52 +0000401 }
402 }
403
Jeff Brown4ccb8232014-01-16 22:16:42 -0800404 // TODO: Use dependencies or a boot phase
405 public void windowManagerAndInputReady() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700406 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800407 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
408 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner65d11b32012-10-01 13:59:52 -0700409 scheduleTraversalLocked(false);
Jeff Brownd728bf52012-09-08 18:05:28 -0700410 }
411 }
412
413 /**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700414 * Called when the system is ready to go.
415 */
416 public void systemReady(boolean safeMode, boolean onlyCore) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700417 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700418 mSafeMode = safeMode;
419 mOnlyCore = onlyCore;
420 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700421
Jeff Brownbd6e1502012-08-28 03:27:37 -0700422 mHandler.sendEmptyMessage(MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS);
Kenny Guy22bd0442017-10-26 00:15:54 +0100423 mHandler.sendEmptyMessage(MSG_REGISTER_BRIGHTNESS_TRACKER);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700424 }
425
Santos Cordonee8931e2017-04-05 10:31:15 -0700426 @VisibleForTesting
427 Handler getDisplayHandler() {
428 return mHandler;
429 }
430
Michael Wrighteedcbf12017-08-16 23:14:54 +0100431 private void loadStableDisplayValuesLocked() {
432 final Point size = mPersistentDataStore.getStableDisplaySize();
433 if (size.x > 0 && size.y > 0) {
434 // Just set these values directly so we don't write the display persistent data again
435 // unnecessarily
436 mStableDisplaySize.set(size.x, size.y);
437 } else {
438 final Resources res = mContext.getResources();
439 final int width = res.getInteger(
440 com.android.internal.R.integer.config_stableDeviceDisplayWidth);
441 final int height = res.getInteger(
442 com.android.internal.R.integer.config_stableDeviceDisplayHeight);
443 if (width > 0 && height > 0) {
444 setStableDisplaySizeLocked(width, height);
445 }
446 }
447 }
448
449 private Point getStableDisplaySizeInternal() {
450 Point r = new Point();
451 synchronized (mSyncRoot) {
452 if (mStableDisplaySize.x > 0 && mStableDisplaySize.y > 0) {
453 r.set(mStableDisplaySize.x, mStableDisplaySize.y);
454 }
455 }
456 return r;
457 }
458
Jeff Brown4ccb8232014-01-16 22:16:42 -0800459 private void registerDisplayTransactionListenerInternal(
460 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800461 // List is self-synchronized copy-on-write.
462 mDisplayTransactionListeners.add(listener);
463 }
464
Jeff Brown4ccb8232014-01-16 22:16:42 -0800465 private void unregisterDisplayTransactionListenerInternal(
466 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800467 // List is self-synchronized copy-on-write.
468 mDisplayTransactionListeners.remove(listener);
469 }
470
Jeff Brown4ccb8232014-01-16 22:16:42 -0800471 private void setDisplayInfoOverrideFromWindowManagerInternal(
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700472 int displayId, DisplayInfo info) {
473 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700474 LogicalDisplay display = mLogicalDisplays.get(displayId);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700475 if (display != null) {
Jeff Brownef981a42013-08-07 14:13:37 -0700476 if (display.setDisplayInfoOverrideFromWindowManagerLocked(info)) {
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200477 handleLogicalDisplayChanged(displayId, display);
Craig Mautner65d11b32012-10-01 13:59:52 -0700478 scheduleTraversalLocked(false);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700479 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700480 }
481 }
482 }
483
Andrii Kuliancd097992017-03-23 18:31:59 -0700484 /**
485 * @see DisplayManagerInternal#getNonOverrideDisplayInfo(int, DisplayInfo)
486 */
487 private void getNonOverrideDisplayInfoInternal(int displayId, DisplayInfo outInfo) {
488 synchronized (mSyncRoot) {
489 final LogicalDisplay display = mLogicalDisplays.get(displayId);
490 if (display != null) {
491 display.getNonOverrideDisplayInfoLocked(outInfo);
492 }
493 }
494 }
495
Santos Cordonee8931e2017-04-05 10:31:15 -0700496 @VisibleForTesting
Robert Carrae606b42018-02-15 15:36:23 -0800497 void performTraversalInternal(SurfaceControl.Transaction t) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700498 synchronized (mSyncRoot) {
499 if (!mPendingTraversal) {
500 return;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700501 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700502 mPendingTraversal = false;
503
Robert Carrae606b42018-02-15 15:36:23 -0800504 performTraversalLocked(t);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700505 }
Jeff Brown7f3994e2012-12-04 14:04:28 -0800506
507 // List is self-synchronized copy-on-write.
508 for (DisplayTransactionListener listener : mDisplayTransactionListeners) {
Vishnu Naire86bd982018-11-28 13:23:17 -0800509 listener.onDisplayTransaction(t);
Jeff Brown7f3994e2012-12-04 14:04:28 -0800510 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700511 }
512
Jeff Brown5d6443b2015-04-10 20:15:01 -0700513 private void requestGlobalDisplayStateInternal(int state, int brightness) {
514 if (state == Display.STATE_UNKNOWN) {
515 state = Display.STATE_ON;
516 }
517 if (state == Display.STATE_OFF) {
518 brightness = PowerManager.BRIGHTNESS_OFF;
519 } else if (brightness < 0) {
520 brightness = PowerManager.BRIGHTNESS_DEFAULT;
521 } else if (brightness > PowerManager.BRIGHTNESS_ON) {
522 brightness = PowerManager.BRIGHTNESS_ON;
523 }
524
Jeff Browne75926d2014-09-18 15:24:49 -0700525 synchronized (mTempDisplayStateWorkQueue) {
526 try {
527 // Update the display state within the lock.
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700528 // Note that we do not need to schedule traversals here although it
529 // may happen as a side-effect of displays changing state.
Jeff Browne75926d2014-09-18 15:24:49 -0700530 synchronized (mSyncRoot) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700531 if (mGlobalDisplayState == state
532 && mGlobalDisplayBrightness == brightness) {
533 return; // no change
Jeff Browne75926d2014-09-18 15:24:49 -0700534 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700535
536 Trace.traceBegin(Trace.TRACE_TAG_POWER, "requestGlobalDisplayState("
537 + Display.stateToString(state)
538 + ", brightness=" + brightness + ")");
539 mGlobalDisplayState = state;
540 mGlobalDisplayBrightness = brightness;
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700541 applyGlobalDisplayStateLocked(mTempDisplayStateWorkQueue);
Jeff Browne75926d2014-09-18 15:24:49 -0700542 }
543
544 // Setting the display power state can take hundreds of milliseconds
545 // to complete so we defer the most expensive part of the work until
546 // after we have exited the critical section to avoid blocking other
547 // threads for a long time.
548 for (int i = 0; i < mTempDisplayStateWorkQueue.size(); i++) {
549 mTempDisplayStateWorkQueue.get(i).run();
550 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700551 Trace.traceEnd(Trace.TRACE_TAG_POWER);
Jeff Browne75926d2014-09-18 15:24:49 -0700552 } finally {
553 mTempDisplayStateWorkQueue.clear();
Jeff Brown9e316a12012-10-08 19:17:06 -0700554 }
555 }
556 }
557
Jeff Brown4ccb8232014-01-16 22:16:42 -0800558 private DisplayInfo getDisplayInfoInternal(int displayId, int callingUid) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700559 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800560 LogicalDisplay display = mLogicalDisplays.get(displayId);
561 if (display != null) {
562 DisplayInfo info = display.getDisplayInfoLocked();
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800563 if (info.hasAccess(callingUid)
564 || isUidPresentOnDisplayInternal(callingUid, displayId)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800565 return info;
566 }
567 }
568 return null;
569 }
570 }
571
572 private int[] getDisplayIdsInternal(int callingUid) {
573 synchronized (mSyncRoot) {
574 final int count = mLogicalDisplays.size();
575 int[] displayIds = new int[count];
576 int n = 0;
577 for (int i = 0; i < count; i++) {
578 LogicalDisplay display = mLogicalDisplays.valueAt(i);
579 DisplayInfo info = display.getDisplayInfoLocked();
580 if (info.hasAccess(callingUid)) {
581 displayIds[n++] = mLogicalDisplays.keyAt(i);
582 }
583 }
584 if (n != count) {
585 displayIds = Arrays.copyOfRange(displayIds, 0, n);
586 }
587 return displayIds;
588 }
589 }
590
591 private void registerCallbackInternal(IDisplayManagerCallback callback, int callingPid) {
592 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700593 if (mCallbacks.get(callingPid) != null) {
594 throw new SecurityException("The calling process has already "
595 + "registered an IDisplayManagerCallback.");
Jeff Brown64a55af2012-08-26 02:47:39 -0700596 }
597
Jeff Brownbd6e1502012-08-28 03:27:37 -0700598 CallbackRecord record = new CallbackRecord(callingPid, callback);
599 try {
600 IBinder binder = callback.asBinder();
601 binder.linkToDeath(record, 0);
602 } catch (RemoteException ex) {
603 // give up
604 throw new RuntimeException(ex);
605 }
606
607 mCallbacks.put(callingPid, record);
608 }
609 }
610
Jeff Brownce468a32013-11-21 16:42:03 -0800611 private void onCallbackDied(CallbackRecord record) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700612 synchronized (mSyncRoot) {
Jeff Brownce468a32013-11-21 16:42:03 -0800613 mCallbacks.remove(record.mPid);
614 stopWifiDisplayScanLocked(record);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700615 }
616 }
617
Jeff Brown4ccb8232014-01-16 22:16:42 -0800618 private void startWifiDisplayScanInternal(int callingPid) {
619 synchronized (mSyncRoot) {
620 CallbackRecord record = mCallbacks.get(callingPid);
621 if (record == null) {
622 throw new IllegalStateException("The calling process has not "
623 + "registered an IDisplayManagerCallback.");
Jeff Browne08ae382012-09-07 20:36:36 -0700624 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800625 startWifiDisplayScanLocked(record);
Jeff Browne08ae382012-09-07 20:36:36 -0700626 }
627 }
628
Jeff Brownce468a32013-11-21 16:42:03 -0800629 private void startWifiDisplayScanLocked(CallbackRecord record) {
630 if (!record.mWifiDisplayScanRequested) {
631 record.mWifiDisplayScanRequested = true;
632 if (mWifiDisplayScanRequestCount++ == 0) {
633 if (mWifiDisplayAdapter != null) {
634 mWifiDisplayAdapter.requestStartScanLocked();
635 }
636 }
637 }
638 }
639
Jeff Brown4ccb8232014-01-16 22:16:42 -0800640 private void stopWifiDisplayScanInternal(int callingPid) {
641 synchronized (mSyncRoot) {
642 CallbackRecord record = mCallbacks.get(callingPid);
643 if (record == null) {
644 throw new IllegalStateException("The calling process has not "
645 + "registered an IDisplayManagerCallback.");
Jeff Brownce468a32013-11-21 16:42:03 -0800646 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800647 stopWifiDisplayScanLocked(record);
Jeff Brownce468a32013-11-21 16:42:03 -0800648 }
649 }
650
651 private void stopWifiDisplayScanLocked(CallbackRecord record) {
652 if (record.mWifiDisplayScanRequested) {
653 record.mWifiDisplayScanRequested = false;
654 if (--mWifiDisplayScanRequestCount == 0) {
655 if (mWifiDisplayAdapter != null) {
656 mWifiDisplayAdapter.requestStopScanLocked();
657 }
658 } else if (mWifiDisplayScanRequestCount < 0) {
Dianne Hackborn8d051722014-10-01 14:59:58 -0700659 Slog.wtf(TAG, "mWifiDisplayScanRequestCount became negative: "
Jeff Brownce468a32013-11-21 16:42:03 -0800660 + mWifiDisplayScanRequestCount);
661 mWifiDisplayScanRequestCount = 0;
662 }
663 }
664 }
665
Jeff Brown4ccb8232014-01-16 22:16:42 -0800666 private void connectWifiDisplayInternal(String address) {
667 synchronized (mSyncRoot) {
668 if (mWifiDisplayAdapter != null) {
669 mWifiDisplayAdapter.requestConnectLocked(address);
Jeff Browne08ae382012-09-07 20:36:36 -0700670 }
Jeff Browne08ae382012-09-07 20:36:36 -0700671 }
672 }
673
Jeff Brown4ccb8232014-01-16 22:16:42 -0800674 private void pauseWifiDisplayInternal() {
675 synchronized (mSyncRoot) {
676 if (mWifiDisplayAdapter != null) {
677 mWifiDisplayAdapter.requestPauseLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700678 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700679 }
680 }
681
Jeff Brown4ccb8232014-01-16 22:16:42 -0800682 private void resumeWifiDisplayInternal() {
683 synchronized (mSyncRoot) {
684 if (mWifiDisplayAdapter != null) {
685 mWifiDisplayAdapter.requestResumeLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700686 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700687 }
688 }
689
Jeff Brown4ccb8232014-01-16 22:16:42 -0800690 private void disconnectWifiDisplayInternal() {
691 synchronized (mSyncRoot) {
692 if (mWifiDisplayAdapter != null) {
693 mWifiDisplayAdapter.requestDisconnectLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700694 }
Jeff Browne08ae382012-09-07 20:36:36 -0700695 }
696 }
697
Jeff Brown4ccb8232014-01-16 22:16:42 -0800698 private void renameWifiDisplayInternal(String address, String alias) {
699 synchronized (mSyncRoot) {
700 if (mWifiDisplayAdapter != null) {
701 mWifiDisplayAdapter.requestRenameLocked(address, alias);
Jeff Brown89d55462012-09-19 11:33:42 -0700702 }
Jeff Brown89d55462012-09-19 11:33:42 -0700703 }
704 }
705
Jeff Brown4ccb8232014-01-16 22:16:42 -0800706 private void forgetWifiDisplayInternal(String address) {
707 synchronized (mSyncRoot) {
708 if (mWifiDisplayAdapter != null) {
709 mWifiDisplayAdapter.requestForgetLocked(address);
Jeff Brown89d55462012-09-19 11:33:42 -0700710 }
Jeff Brown89d55462012-09-19 11:33:42 -0700711 }
712 }
713
Jeff Brown4ccb8232014-01-16 22:16:42 -0800714 private WifiDisplayStatus getWifiDisplayStatusInternal() {
715 synchronized (mSyncRoot) {
716 if (mWifiDisplayAdapter != null) {
717 return mWifiDisplayAdapter.getWifiDisplayStatusLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700718 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800719 return new WifiDisplayStatus();
Jeff Browne08ae382012-09-07 20:36:36 -0700720 }
721 }
722
Michael Wright1c9977b2016-07-12 13:30:10 -0700723 private void requestColorModeInternal(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +0100724 synchronized (mSyncRoot) {
725 LogicalDisplay display = mLogicalDisplays.get(displayId);
726 if (display != null &&
Michael Wright1c9977b2016-07-12 13:30:10 -0700727 display.getRequestedColorModeLocked() != colorMode) {
728 display.setRequestedColorModeLocked(colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +0100729 scheduleTraversalLocked(false);
730 }
731 }
732 }
733
Michael Wright75ee9fc2014-09-01 19:55:22 -0700734 private int createVirtualDisplayInternal(IVirtualDisplayCallback callback,
Santos Cordonee8931e2017-04-05 10:31:15 -0700735 IMediaProjection projection, int callingUid, String packageName, String name, int width,
736 int height, int densityDpi, Surface surface, int flags, String uniqueId) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800737 synchronized (mSyncRoot) {
738 if (mVirtualDisplayAdapter == null) {
739 Slog.w(TAG, "Rejecting request to create private virtual display "
740 + "because the virtual display adapter is not available.");
741 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700742 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800743
744 DisplayDevice device = mVirtualDisplayAdapter.createVirtualDisplayLocked(
Santos Cordonee8931e2017-04-05 10:31:15 -0700745 callback, projection, callingUid, packageName, name, width, height, densityDpi,
746 surface, flags, uniqueId);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800747 if (device == null) {
748 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700749 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700750
Jeff Brown4ccb8232014-01-16 22:16:42 -0800751 handleDisplayDeviceAddedLocked(device);
752 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
753 if (display != null) {
754 return display.getDisplayIdLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700755 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800756
757 // Something weird happened and the logical display was not created.
758 Slog.w(TAG, "Rejecting request to create virtual display "
759 + "because the logical display was not created.");
Michael Wright75ee9fc2014-09-01 19:55:22 -0700760 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800761 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700762 }
763 return -1;
764 }
765
Michael Wright01e840f2014-06-26 16:03:25 -0700766 private void resizeVirtualDisplayInternal(IBinder appToken,
767 int width, int height, int densityDpi) {
768 synchronized (mSyncRoot) {
769 if (mVirtualDisplayAdapter == null) {
770 return;
771 }
772
773 mVirtualDisplayAdapter.resizeVirtualDisplayLocked(appToken, width, height, densityDpi);
774 }
775 }
776
Jeff Brown92207df2014-04-16 13:16:07 -0700777 private void setVirtualDisplaySurfaceInternal(IBinder appToken, Surface surface) {
778 synchronized (mSyncRoot) {
779 if (mVirtualDisplayAdapter == null) {
780 return;
781 }
782
783 mVirtualDisplayAdapter.setVirtualDisplaySurfaceLocked(appToken, surface);
784 }
785 }
786
Jeff Brown4ccb8232014-01-16 22:16:42 -0800787 private void releaseVirtualDisplayInternal(IBinder appToken) {
788 synchronized (mSyncRoot) {
789 if (mVirtualDisplayAdapter == null) {
790 return;
Jeff Browna506a6e2013-06-04 00:02:38 -0700791 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700792
Jeff Brown4ccb8232014-01-16 22:16:42 -0800793 DisplayDevice device =
794 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(appToken);
795 if (device != null) {
796 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700797 }
798 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700799 }
800
chaviwda4c6942018-11-07 15:52:56 -0800801 private void setVirtualDisplayStateInternal(IBinder appToken, boolean isOn) {
802 synchronized (mSyncRoot) {
803 if (mVirtualDisplayAdapter == null) {
804 return;
805 }
806
807 mVirtualDisplayAdapter.setVirtualDisplayStateLocked(appToken, isOn);
808 }
809 }
810
Santos Cordonc22c5632017-06-21 16:03:49 -0700811 private void registerDefaultDisplayAdapters() {
812 // Register default display adapters.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700813 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700814 // main display adapter
Mike Lockwoode63f6f72013-11-15 11:01:47 -0800815 registerDisplayAdapterLocked(new LocalDisplayAdapter(
816 mSyncRoot, mContext, mHandler, mDisplayAdapterListener));
Santos Cordonc22c5632017-06-21 16:03:49 -0700817
818 // Standalone VR devices rely on a virtual display as their primary display for
819 // 2D UI. We register virtual display adapter along side the main display adapter
820 // here so that it is ready by the time the system sends the home Intent for
821 // early apps like SetupWizard/Launcher. In particular, SUW is displayed using
822 // the virtual display inside VR before any VR-specific apps even run.
823 mVirtualDisplayAdapter = mInjector.getVirtualDisplayAdapter(mSyncRoot, mContext,
824 mHandler, mDisplayAdapterListener);
825 if (mVirtualDisplayAdapter != null) {
826 registerDisplayAdapterLocked(mVirtualDisplayAdapter);
827 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700828 }
829 }
830
831 private void registerAdditionalDisplayAdapters() {
832 synchronized (mSyncRoot) {
833 if (shouldRegisterNonEssentialDisplayAdaptersLocked()) {
Jeff Brown89d55462012-09-19 11:33:42 -0700834 registerOverlayDisplayAdapterLocked();
835 registerWifiDisplayAdapterLocked();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700836 }
837 }
838 }
839
Jeff Brown89d55462012-09-19 11:33:42 -0700840 private void registerOverlayDisplayAdapterLocked() {
841 registerDisplayAdapterLocked(new OverlayDisplayAdapter(
842 mSyncRoot, mContext, mHandler, mDisplayAdapterListener, mUiHandler));
843 }
844
845 private void registerWifiDisplayAdapterLocked() {
846 if (mContext.getResources().getBoolean(
Jeff Brownbbd28a22012-09-20 16:47:15 -0700847 com.android.internal.R.bool.config_enableWifiDisplay)
848 || SystemProperties.getInt(FORCE_WIFI_DISPLAY_ENABLE, -1) == 1) {
Jeff Brown89d55462012-09-19 11:33:42 -0700849 mWifiDisplayAdapter = new WifiDisplayAdapter(
850 mSyncRoot, mContext, mHandler, mDisplayAdapterListener,
851 mPersistentDataStore);
852 registerDisplayAdapterLocked(mWifiDisplayAdapter);
853 }
854 }
855
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700856 private boolean shouldRegisterNonEssentialDisplayAdaptersLocked() {
857 // In safe mode, we disable non-essential display adapters to give the user
858 // an opportunity to fix broken settings or other problems that might affect
859 // system stability.
860 // In only-core mode, we disable non-essential display adapters to minimize
861 // the number of dependencies that are started while in this mode and to
862 // prevent problems that might occur due to the device being encrypted.
863 return !mSafeMode && !mOnlyCore;
864 }
865
866 private void registerDisplayAdapterLocked(DisplayAdapter adapter) {
867 mDisplayAdapters.add(adapter);
868 adapter.registerLocked();
869 }
870
Jeff Brownbd6e1502012-08-28 03:27:37 -0700871 private void handleDisplayDeviceAdded(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700872 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700873 handleDisplayDeviceAddedLocked(device);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700874 }
875 }
876
Jeff Browna506a6e2013-06-04 00:02:38 -0700877 private void handleDisplayDeviceAddedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700878 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700879 if (mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700880 Slog.w(TAG, "Attempted to add already added display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700881 return;
882 }
883
Jeff Brown10acf6d2015-04-14 14:20:47 -0700884 Slog.i(TAG, "Display device added: " + info);
885 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700886
887 mDisplayDevices.add(device);
Michael Wright1c9977b2016-07-12 13:30:10 -0700888 LogicalDisplay display = addLogicalDisplayLocked(device);
Jeff Brown0033a862014-10-08 12:06:39 -0700889 Runnable work = updateDisplayStateLocked(device);
890 if (work != null) {
891 work.run();
892 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700893 scheduleTraversalLocked(false);
894 }
895
Jeff Brownbd6e1502012-08-28 03:27:37 -0700896 private void handleDisplayDeviceChanged(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700897 synchronized (mSyncRoot) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700898 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700899 if (!mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700900 Slog.w(TAG, "Attempted to change non-existent display device: " + info);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700901 return;
902 }
903
Jeff Brown10acf6d2015-04-14 14:20:47 -0700904 int diff = device.mDebugLastLoggedDeviceInfo.diff(info);
905 if (diff == DisplayDeviceInfo.DIFF_STATE) {
906 Slog.i(TAG, "Display device changed state: \"" + info.name
907 + "\", " + Display.stateToString(info.state));
908 } else if (diff != 0) {
909 Slog.i(TAG, "Display device changed: " + info);
910 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700911 if ((diff & DisplayDeviceInfo.DIFF_COLOR_MODE) != 0) {
912 try {
913 mPersistentDataStore.setColorMode(device, info.colorMode);
914 } finally {
915 mPersistentDataStore.saveIfNeeded();
916 }
917 }
Jeff Brown10acf6d2015-04-14 14:20:47 -0700918 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browne87bf032012-09-20 18:30:13 -0700919
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700920 device.applyPendingDisplayDeviceInfoChangesLocked();
921 if (updateLogicalDisplaysLocked()) {
Craig Mautner65d11b32012-10-01 13:59:52 -0700922 scheduleTraversalLocked(false);
Jeff Brown64a55af2012-08-26 02:47:39 -0700923 }
924 }
925 }
926
Jeff Brownbd6e1502012-08-28 03:27:37 -0700927 private void handleDisplayDeviceRemoved(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700928 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700929 handleDisplayDeviceRemovedLocked(device);
930 }
931 }
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700932
Jeff Browna506a6e2013-06-04 00:02:38 -0700933 private void handleDisplayDeviceRemovedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700934 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700935 if (!mDisplayDevices.remove(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700936 Slog.w(TAG, "Attempted to remove non-existent display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700937 return;
938 }
939
Jeff Brown10acf6d2015-04-14 14:20:47 -0700940 Slog.i(TAG, "Display device removed: " + info);
941 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700942
Jeff Browna506a6e2013-06-04 00:02:38 -0700943 updateLogicalDisplaysLocked();
944 scheduleTraversalLocked(false);
945 }
946
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200947 private void handleLogicalDisplayChanged(int displayId, @NonNull LogicalDisplay display) {
948 if (displayId == Display.DEFAULT_DISPLAY) {
949 recordTopInsetLocked(display);
950 }
951 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
952 }
953
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700954 private void applyGlobalDisplayStateLocked(List<Runnable> workQueue) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700955 final int count = mDisplayDevices.size();
956 for (int i = 0; i < count; i++) {
957 DisplayDevice device = mDisplayDevices.get(i);
Jeff Browne75926d2014-09-18 15:24:49 -0700958 Runnable runnable = updateDisplayStateLocked(device);
959 if (runnable != null) {
960 workQueue.add(runnable);
961 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700962 }
963 }
964
Jeff Browne75926d2014-09-18 15:24:49 -0700965 private Runnable updateDisplayStateLocked(DisplayDevice device) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700966 // Blank or unblank the display immediately to match the state requested
Jeff Brown037c33e2014-04-09 00:31:55 -0700967 // by the display power controller (if known).
Jeff Browna506a6e2013-06-04 00:02:38 -0700968 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
969 if ((info.flags & DisplayDeviceInfo.FLAG_NEVER_BLANK) == 0) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700970 return device.requestDisplayStateLocked(mGlobalDisplayState, mGlobalDisplayBrightness);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700971 }
Jeff Browne75926d2014-09-18 15:24:49 -0700972 return null;
Craig Mautner4f67ba62012-08-02 11:23:00 -0700973 }
974
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700975 // Adds a new logical display based on the given display device.
976 // Sends notifications if needed.
Michael Wright1c9977b2016-07-12 13:30:10 -0700977 private LogicalDisplay addLogicalDisplayLocked(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700978 DisplayDeviceInfo deviceInfo = device.getDisplayDeviceInfoLocked();
979 boolean isDefault = (deviceInfo.flags
980 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0;
981 if (isDefault && mLogicalDisplays.get(Display.DEFAULT_DISPLAY) != null) {
982 Slog.w(TAG, "Ignoring attempt to add a second default display: " + deviceInfo);
983 isDefault = false;
984 }
985
Jeff Brown27f1d672012-10-17 18:32:34 -0700986 if (!isDefault && mSingleDisplayDemoMode) {
987 Slog.i(TAG, "Not creating a logical display for a secondary display "
988 + " because single display demo mode is enabled: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -0700989 return null;
Jeff Brown27f1d672012-10-17 18:32:34 -0700990 }
991
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700992 final int displayId = assignDisplayIdLocked(isDefault);
993 final int layerStack = assignLayerStackLocked(displayId);
994
Jeff Brownd728bf52012-09-08 18:05:28 -0700995 LogicalDisplay display = new LogicalDisplay(displayId, layerStack, device);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700996 display.updateLocked(mDisplayDevices);
997 if (!display.isValidLocked()) {
998 // This should never happen currently.
999 Slog.w(TAG, "Ignoring display device because the logical display "
1000 + "created from it was not considered valid: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -07001001 return null;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001002 }
1003
Michael Wrighteedcbf12017-08-16 23:14:54 +01001004 configureColorModeLocked(display, device);
1005 if (isDefault) {
1006 recordStableDisplayStatsIfNeededLocked(display);
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001007 recordTopInsetLocked(display);
Michael Wrighteedcbf12017-08-16 23:14:54 +01001008 }
1009
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001010 mLogicalDisplays.put(displayId, display);
1011
1012 // Wake up waitForDefaultDisplay.
1013 if (isDefault) {
1014 mSyncRoot.notifyAll();
1015 }
1016
1017 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_ADDED);
Michael Wright1c9977b2016-07-12 13:30:10 -07001018 return display;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001019 }
1020
1021 private int assignDisplayIdLocked(boolean isDefault) {
1022 return isDefault ? Display.DEFAULT_DISPLAY : mNextNonDefaultDisplayId++;
1023 }
1024
1025 private int assignLayerStackLocked(int displayId) {
1026 // Currently layer stacks and display ids are the same.
1027 // This need not be the case.
1028 return displayId;
1029 }
1030
Michael Wrighteedcbf12017-08-16 23:14:54 +01001031 private void configureColorModeLocked(LogicalDisplay display, DisplayDevice device) {
1032 if (display.getPrimaryDisplayDeviceLocked() == device) {
1033 int colorMode = mPersistentDataStore.getColorMode(device);
1034 if (colorMode == Display.COLOR_MODE_INVALID) {
1035 if ((device.getDisplayDeviceInfoLocked().flags
1036 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
1037 colorMode = mDefaultDisplayDefaultColorMode;
1038 } else {
1039 colorMode = Display.COLOR_MODE_DEFAULT;
1040 }
1041 }
1042 display.setRequestedColorModeLocked(colorMode);
1043 }
1044 }
1045
1046 // If we've never recorded stable device stats for this device before and they aren't
1047 // explicitly configured, go ahead and record the stable device stats now based on the status
1048 // of the default display at first boot.
1049 private void recordStableDisplayStatsIfNeededLocked(LogicalDisplay d) {
1050 if (mStableDisplaySize.x <= 0 && mStableDisplaySize.y <= 0) {
1051 DisplayInfo info = d.getDisplayInfoLocked();
1052 setStableDisplaySizeLocked(info.getNaturalWidth(), info.getNaturalHeight());
1053 }
1054 }
1055
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001056 private void recordTopInsetLocked(@Nullable LogicalDisplay d) {
MÃ¥rten Kongstad250cb2b2018-11-02 11:01:44 +01001057 if (d == null) {
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001058 return;
1059 }
1060 int topInset = d.getInsets().top;
1061 if (topInset == mDefaultDisplayTopInset) {
1062 return;
1063 }
1064 mDefaultDisplayTopInset = topInset;
1065 SystemProperties.set(PROP_DEFAULT_DISPLAY_TOP_INSET, Integer.toString(topInset));
1066 }
1067
Michael Wrighteedcbf12017-08-16 23:14:54 +01001068 private void setStableDisplaySizeLocked(int width, int height) {
1069 mStableDisplaySize = new Point(width, height);
1070 try {
1071 mPersistentDataStore.setStableDisplaySize(mStableDisplaySize);
1072 } finally {
1073 mPersistentDataStore.saveIfNeeded();
1074 }
1075 }
1076
Dan Gittik122df862018-03-28 16:59:22 +01001077 @VisibleForTesting
1078 Curve getMinimumBrightnessCurveInternal() {
1079 return mMinimumBrightnessCurve;
1080 }
1081
Peiyong Lin277eaff2019-01-16 16:18:22 -08001082 int getPreferredWideGamutColorSpaceIdInternal() {
1083 return mWideColorSpace.getId();
1084 }
1085
Michael Wrighteef0e132017-11-21 17:57:52 +00001086 private void setBrightnessConfigurationForUserInternal(
Dan Gittik7a32fba2018-03-28 12:19:38 +01001087 @Nullable BrightnessConfiguration c, @UserIdInt int userId,
Kenny Guy05ce8092018-01-17 13:44:20 +00001088 @Nullable String packageName) {
Dan Gittik122df862018-03-28 16:59:22 +01001089 validateBrightnessConfiguration(c);
Michael Wrighteef0e132017-11-21 17:57:52 +00001090 final int userSerial = getUserManager().getUserSerialNumber(userId);
1091 synchronized (mSyncRoot) {
1092 try {
Kenny Guy05ce8092018-01-17 13:44:20 +00001093 mPersistentDataStore.setBrightnessConfigurationForUser(c, userSerial,
1094 packageName);
Michael Wrighteef0e132017-11-21 17:57:52 +00001095 } finally {
1096 mPersistentDataStore.saveIfNeeded();
1097 }
1098 if (userId == mCurrentUserId) {
1099 mDisplayPowerController.setBrightnessConfiguration(c);
1100 }
1101 }
1102 }
1103
Dan Gittik122df862018-03-28 16:59:22 +01001104 @VisibleForTesting
1105 void validateBrightnessConfiguration(BrightnessConfiguration config) {
1106 if (config == null) {
1107 return;
1108 }
1109 if (isBrightnessConfigurationTooDark(config)) {
1110 throw new IllegalArgumentException("brightness curve is too dark");
1111 }
1112 }
1113
1114 private boolean isBrightnessConfigurationTooDark(BrightnessConfiguration config) {
1115 Pair<float[], float[]> curve = config.getCurve();
1116 float[] lux = curve.first;
1117 float[] nits = curve.second;
1118 for (int i = 0; i < lux.length; i++) {
1119 if (nits[i] < mMinimumBrightnessSpline.interpolate(lux[i])) {
1120 return true;
1121 }
1122 }
1123 return false;
1124 }
1125
Michael Wrighteef0e132017-11-21 17:57:52 +00001126 private void loadBrightnessConfiguration() {
1127 synchronized (mSyncRoot) {
1128 final int userSerial = getUserManager().getUserSerialNumber(mCurrentUserId);
1129 BrightnessConfiguration config =
1130 mPersistentDataStore.getBrightnessConfiguration(userSerial);
1131 mDisplayPowerController.setBrightnessConfiguration(config);
1132 }
1133 }
1134
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001135 // Updates all existing logical displays given the current set of display devices.
1136 // Removes invalid logical displays.
1137 // Sends notifications if needed.
1138 private boolean updateLogicalDisplaysLocked() {
1139 boolean changed = false;
1140 for (int i = mLogicalDisplays.size(); i-- > 0; ) {
1141 final int displayId = mLogicalDisplays.keyAt(i);
1142 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1143
1144 mTempDisplayInfo.copyFrom(display.getDisplayInfoLocked());
1145 display.updateLocked(mDisplayDevices);
1146 if (!display.isValidLocked()) {
1147 mLogicalDisplays.removeAt(i);
1148 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_REMOVED);
1149 changed = true;
1150 } else if (!mTempDisplayInfo.equals(display.getDisplayInfoLocked())) {
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001151 handleLogicalDisplayChanged(displayId, display);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001152 changed = true;
1153 }
1154 }
1155 return changed;
1156 }
1157
Robert Carrae606b42018-02-15 15:36:23 -08001158 private void performTraversalLocked(SurfaceControl.Transaction t) {
Jeff Brownd728bf52012-09-08 18:05:28 -07001159 // Clear all viewports before configuring displays so that we can keep
1160 // track of which ones we have configured.
1161 clearViewportsLocked();
1162
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001163 // Configure each display device.
1164 final int count = mDisplayDevices.size();
1165 for (int i = 0; i < count; i++) {
1166 DisplayDevice device = mDisplayDevices.get(i);
Robert Carrae606b42018-02-15 15:36:23 -08001167 configureDisplayLocked(t, device);
1168 device.performTraversalLocked(t);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001169 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001170
1171 // Tell the input system about these new viewports.
Jeff Brown4ccb8232014-01-16 22:16:42 -08001172 if (mInputManagerInternal != null) {
Jeff Brownd728bf52012-09-08 18:05:28 -07001173 mHandler.sendEmptyMessage(MSG_UPDATE_VIEWPORT);
1174 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001175 }
1176
Michael Wright3f145a22014-07-22 19:46:03 -07001177 private void setDisplayPropertiesInternal(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001178 float requestedRefreshRate, int requestedModeId, boolean inTraversal) {
Craig Mautner722285e2012-09-07 13:55:58 -07001179 synchronized (mSyncRoot) {
1180 LogicalDisplay display = mLogicalDisplays.get(displayId);
Michael Wright3f145a22014-07-22 19:46:03 -07001181 if (display == null) {
1182 return;
1183 }
1184 if (display.hasContentLocked() != hasContent) {
Jeff Brown33041bd2013-08-02 21:11:14 -07001185 if (DEBUG) {
1186 Slog.d(TAG, "Display " + displayId + " hasContent flag changed: "
1187 + "hasContent=" + hasContent + ", inTraversal=" + inTraversal);
1188 }
1189
Craig Mautner722285e2012-09-07 13:55:58 -07001190 display.setHasContentLocked(hasContent);
Craig Mautner65d11b32012-10-01 13:59:52 -07001191 scheduleTraversalLocked(inTraversal);
Craig Mautner722285e2012-09-07 13:55:58 -07001192 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001193 if (requestedModeId == 0 && requestedRefreshRate != 0) {
1194 // Scan supported modes returned by display.getInfo() to find a mode with the same
1195 // size as the default display mode but with the specified refresh rate instead.
1196 requestedModeId = display.getDisplayInfoLocked().findDefaultModeByRefreshRate(
1197 requestedRefreshRate);
1198 }
1199 if (display.getRequestedModeIdLocked() != requestedModeId) {
Michael Wright3f145a22014-07-22 19:46:03 -07001200 if (DEBUG) {
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001201 Slog.d(TAG, "Display " + displayId + " switching to mode " + requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -07001202 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001203 display.setRequestedModeIdLocked(requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -07001204 scheduleTraversalLocked(inTraversal);
1205 }
Craig Mautner722285e2012-09-07 13:55:58 -07001206 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001207 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001208
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08001209 private void setDisplayOffsetsInternal(int displayId, int x, int y) {
1210 synchronized (mSyncRoot) {
1211 LogicalDisplay display = mLogicalDisplays.get(displayId);
1212 if (display == null) {
1213 return;
1214 }
1215 if (display.getDisplayOffsetXLocked() != x
1216 || display.getDisplayOffsetYLocked() != y) {
1217 if (DEBUG) {
1218 Slog.d(TAG, "Display " + displayId + " burn-in offset set to ("
1219 + x + ", " + y + ")");
1220 }
1221 display.setDisplayOffsetsLocked(x, y);
1222 scheduleTraversalLocked(false);
1223 }
1224 }
1225 }
1226
Sam Lin4c3ac2b2019-02-18 04:50:26 -08001227 private void setDisplayScalingDisabledInternal(int displayId, boolean disable) {
1228 synchronized (mSyncRoot) {
1229 final LogicalDisplay display = mLogicalDisplays.get(displayId);
1230 if (display == null) {
1231 return;
1232 }
1233 if (display.isDisplayScalingDisabled() != disable) {
1234 if (DEBUG) {
1235 Slog.d(TAG, "Display " + displayId + " content scaling disabled = " + disable);
1236 }
1237 display.setDisplayScalingDisabledLocked(disable);
1238 scheduleTraversalLocked(false);
1239 }
1240 }
1241 }
1242
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001243 // Updates the lists of UIDs that are present on displays.
1244 private void setDisplayAccessUIDsInternal(SparseArray<IntArray> newDisplayAccessUIDs) {
1245 synchronized (mSyncRoot) {
1246 mDisplayAccessUIDs.clear();
1247 for (int i = newDisplayAccessUIDs.size() - 1; i >= 0; i--) {
1248 mDisplayAccessUIDs.append(newDisplayAccessUIDs.keyAt(i),
1249 newDisplayAccessUIDs.valueAt(i));
1250 }
1251 }
1252 }
1253
1254 // Checks if provided UID's content is present on the display and UID has access to it.
1255 private boolean isUidPresentOnDisplayInternal(int uid, int displayId) {
1256 synchronized (mSyncRoot) {
1257 final IntArray displayUIDs = mDisplayAccessUIDs.get(displayId);
1258 return displayUIDs != null && displayUIDs.indexOf(uid) != -1;
1259 }
1260 }
1261
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001262 @Nullable
1263 private IBinder getDisplayToken(int displayId) {
1264 synchronized (mSyncRoot) {
1265 final LogicalDisplay display = mLogicalDisplays.get(displayId);
1266 if (display != null) {
1267 final DisplayDevice device = display.getPrimaryDisplayDeviceLocked();
1268 if (device != null) {
1269 return device.getDisplayTokenLocked();
1270 }
1271 }
1272 }
1273
1274 return null;
1275 }
1276
1277 private boolean screenshotInternal(int displayId, Surface outSurface) {
1278 final IBinder token = getDisplayToken(displayId);
1279 if (token == null) {
1280 return false;
1281 }
Robert Carr5c52b132019-02-15 15:48:11 -08001282 final GraphicBuffer gb = SurfaceControl.screenshotToBufferWithSecureLayersUnsafe(
1283 token, new Rect(), 0 /* width */, 0 /* height */, false /* useIdentityTransform */,
1284 0 /* rotation */);
1285 try {
1286 outSurface.attachAndQueueBuffer(gb);
1287 } catch (RuntimeException e) {
1288 Slog.w(TAG, "Failed to take screenshot - " + e.getMessage());
1289 }
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001290 return true;
1291 }
1292
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001293 @VisibleForTesting
1294 DisplayedContentSamplingAttributes getDisplayedContentSamplingAttributesInternal(
1295 int displayId) {
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001296 final IBinder token = getDisplayToken(displayId);
1297 if (token == null) {
1298 return null;
1299 }
1300 return SurfaceControl.getDisplayedContentSamplingAttributes(token);
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001301 }
1302
1303 @VisibleForTesting
1304 boolean setDisplayedContentSamplingEnabledInternal(
1305 int displayId, boolean enable, int componentMask, int maxFrames) {
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001306 final IBinder token = getDisplayToken(displayId);
1307 if (token == null) {
1308 return false;
1309 }
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001310 return SurfaceControl.setDisplayedContentSamplingEnabled(
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001311 token, enable, componentMask, maxFrames);
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001312 }
1313
1314 @VisibleForTesting
1315 DisplayedContentSample getDisplayedContentSampleInternal(int displayId,
1316 long maxFrames, long timestamp) {
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001317 final IBinder token = getDisplayToken(displayId);
1318 if (token == null) {
1319 return null;
1320 }
1321 return SurfaceControl.getDisplayedContentSample(token, maxFrames, timestamp);
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001322 }
1323
Jeff Brownd728bf52012-09-08 18:05:28 -07001324 private void clearViewportsLocked() {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001325 mViewports.clear();
Craig Mautner722285e2012-09-07 13:55:58 -07001326 }
1327
Robert Carrae606b42018-02-15 15:36:23 -08001328 private void configureDisplayLocked(SurfaceControl.Transaction t, DisplayDevice device) {
Jeff Brownd14c8c92014-01-07 18:13:09 -08001329 final DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
1330 final boolean ownContent = (info.flags & DisplayDeviceInfo.FLAG_OWN_CONTENT_ONLY) != 0;
Jeff Browna506a6e2013-06-04 00:02:38 -07001331
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001332 // Find the logical display that the display device is showing.
Jeff Brownd14c8c92014-01-07 18:13:09 -08001333 // Certain displays only ever show their own content.
Craig Mautner722285e2012-09-07 13:55:58 -07001334 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
Jeff Brownd14c8c92014-01-07 18:13:09 -08001335 if (!ownContent) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001336 if (display != null && !display.hasContentLocked()) {
1337 // If the display does not have any content of its own, then
1338 // automatically mirror the default logical display contents.
1339 display = null;
1340 }
1341 if (display == null) {
1342 display = mLogicalDisplays.get(Display.DEFAULT_DISPLAY);
1343 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001344 }
1345
1346 // Apply the logical display configuration to the display device.
1347 if (display == null) {
1348 // TODO: no logical display for the device, blank it
Jeff Brownd728bf52012-09-08 18:05:28 -07001349 Slog.w(TAG, "Missing logical display to use for physical display device: "
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001350 + device.getDisplayDeviceInfoLocked());
Jeff Brownd728bf52012-09-08 18:05:28 -07001351 return;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001352 }
Robert Carrae606b42018-02-15 15:36:23 -08001353 display.configureDisplayLocked(t, device, info.state == Display.STATE_OFF);
Arthur Hung41e81e72018-10-31 18:04:56 +08001354 final int viewportType;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001355 // Update the corresponding viewport.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001356 if ((info.flags & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
Arthur Hung41e81e72018-10-31 18:04:56 +08001357 viewportType = VIEWPORT_INTERNAL;
1358 } else if (info.touch == DisplayDeviceInfo.TOUCH_EXTERNAL) {
1359 viewportType = VIEWPORT_EXTERNAL;
1360 } else if (info.touch == DisplayDeviceInfo.TOUCH_VIRTUAL
1361 && !TextUtils.isEmpty(info.uniqueId)) {
1362 viewportType = VIEWPORT_VIRTUAL;
1363 } else {
Arthur Hung46c4e582019-02-12 15:55:28 +08001364 Slog.i(TAG, "Display " + info + " does not support input device matching.");
Arthur Hung41e81e72018-10-31 18:04:56 +08001365 return;
Jeff Brownd728bf52012-09-08 18:05:28 -07001366 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001367
Arthur Hung41e81e72018-10-31 18:04:56 +08001368 populateViewportLocked(viewportType, display.getDisplayIdLocked(), device, info.uniqueId);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001369 }
1370
1371 /**
1372 * Get internal or external viewport. Create it if does not currently exist.
1373 * @param viewportType - either INTERNAL or EXTERNAL
1374 * @return the viewport with the requested type
1375 */
Arthur Hung41e81e72018-10-31 18:04:56 +08001376 private DisplayViewport getViewportLocked(int viewportType, String uniqueId) {
1377 if (viewportType != VIEWPORT_INTERNAL && viewportType != VIEWPORT_EXTERNAL
1378 && viewportType != VIEWPORT_VIRTUAL) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001379 Slog.wtf(TAG, "Cannot call getViewportByTypeLocked for type "
1380 + DisplayViewport.typeToString(viewportType));
1381 return null;
1382 }
Arthur Hung41e81e72018-10-31 18:04:56 +08001383
1384 // Only allow a single INTERNAL or EXTERNAL viewport by forcing their uniqueIds
1385 // to be identical (in particular, empty).
1386 // TODO (b/116824030) allow multiple EXTERNAL viewports and remove this function.
1387 if (viewportType != VIEWPORT_VIRTUAL) {
1388 uniqueId = "";
1389 }
1390
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001391 DisplayViewport viewport;
1392 final int count = mViewports.size();
1393 for (int i = 0; i < count; i++) {
1394 viewport = mViewports.get(i);
Arthur Hung41e81e72018-10-31 18:04:56 +08001395 if (viewport.type == viewportType && uniqueId.equals(viewport.uniqueId)) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001396 return viewport;
1397 }
1398 }
1399
Arthur Hung41e81e72018-10-31 18:04:56 +08001400 // Creates the viewport if none exists.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001401 viewport = new DisplayViewport();
1402 viewport.type = viewportType;
Arthur Hung41e81e72018-10-31 18:04:56 +08001403 viewport.uniqueId = uniqueId;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001404 mViewports.add(viewport);
1405 return viewport;
1406 }
1407
Arthur Hung41e81e72018-10-31 18:04:56 +08001408 private void populateViewportLocked(int viewportType,
1409 int displayId, DisplayDevice device, String uniqueId) {
1410 final DisplayViewport viewport = getViewportLocked(viewportType, uniqueId);
Jeff Brownd728bf52012-09-08 18:05:28 -07001411 device.populateViewportLocked(viewport);
Arthur Hung41e81e72018-10-31 18:04:56 +08001412 viewport.valid = true;
1413 viewport.displayId = displayId;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001414 }
1415
1416 private LogicalDisplay findLogicalDisplayForDeviceLocked(DisplayDevice device) {
1417 final int count = mLogicalDisplays.size();
1418 for (int i = 0; i < count; i++) {
1419 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1420 if (display.getPrimaryDisplayDeviceLocked() == device) {
1421 return display;
1422 }
1423 }
1424 return null;
1425 }
1426
Jeff Brownbd6e1502012-08-28 03:27:37 -07001427 private void sendDisplayEventLocked(int displayId, int event) {
1428 Message msg = mHandler.obtainMessage(MSG_DELIVER_DISPLAY_EVENT, displayId, event);
1429 mHandler.sendMessage(msg);
1430 }
1431
Robert Carrae606b42018-02-15 15:36:23 -08001432 // Requests that performTraversals be called at a
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001433 // later time to apply changes to surfaces and displays.
Craig Mautner65d11b32012-10-01 13:59:52 -07001434 private void scheduleTraversalLocked(boolean inTraversal) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001435 if (!mPendingTraversal && mWindowManagerInternal != null) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001436 mPendingTraversal = true;
Craig Mautner65d11b32012-10-01 13:59:52 -07001437 if (!inTraversal) {
1438 mHandler.sendEmptyMessage(MSG_REQUEST_TRAVERSAL);
1439 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001440 }
1441 }
1442
1443 // Runs on Handler thread.
1444 // Delivers display event notifications to callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001445 private void deliverDisplayEvent(int displayId, int event) {
1446 if (DEBUG) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001447 Slog.d(TAG, "Delivering display event: displayId="
1448 + displayId + ", event=" + event);
Jeff Brownfa25bf52012-07-23 19:26:30 -07001449 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07001450
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001451 // Grab the lock and copy the callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001452 final int count;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001453 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001454 count = mCallbacks.size();
1455 mTempCallbacks.clear();
1456 for (int i = 0; i < count; i++) {
1457 mTempCallbacks.add(mCallbacks.valueAt(i));
Craig Mautner4f67ba62012-08-02 11:23:00 -07001458 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001459 }
Craig Mautner4f67ba62012-08-02 11:23:00 -07001460
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001461 // After releasing the lock, send the notifications out.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001462 for (int i = 0; i < count; i++) {
1463 mTempCallbacks.get(i).notifyDisplayEventAsync(displayId, event);
1464 }
1465 mTempCallbacks.clear();
Craig Mautner4f67ba62012-08-02 11:23:00 -07001466 }
1467
Michael Wrightc39d47a2014-07-08 18:07:36 -07001468 private IMediaProjectionManager getProjectionService() {
1469 if (mProjectionService == null) {
1470 IBinder b = ServiceManager.getService(Context.MEDIA_PROJECTION_SERVICE);
1471 mProjectionService = IMediaProjectionManager.Stub.asInterface(b);
1472 }
1473 return mProjectionService;
1474 }
1475
Michael Wrighteef0e132017-11-21 17:57:52 +00001476 private UserManager getUserManager() {
1477 return mContext.getSystemService(UserManager.class);
1478 }
1479
Jeff Brown4ccb8232014-01-16 22:16:42 -08001480 private void dumpInternal(PrintWriter pw) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001481 pw.println("DISPLAY MANAGER (dumpsys display)");
Jeff Brownfa25bf52012-07-23 19:26:30 -07001482
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001483 synchronized (mSyncRoot) {
Jeff Brown9e316a12012-10-08 19:17:06 -07001484 pw.println(" mOnlyCode=" + mOnlyCore);
1485 pw.println(" mSafeMode=" + mSafeMode);
1486 pw.println(" mPendingTraversal=" + mPendingTraversal);
Jeff Brown037c33e2014-04-09 00:31:55 -07001487 pw.println(" mGlobalDisplayState=" + Display.stateToString(mGlobalDisplayState));
Jeff Brown9e316a12012-10-08 19:17:06 -07001488 pw.println(" mNextNonDefaultDisplayId=" + mNextNonDefaultDisplayId);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001489 pw.println(" mViewports=" + mViewports);
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -07001490 pw.println(" mDefaultDisplayDefaultColorMode=" + mDefaultDisplayDefaultColorMode);
Jeff Brown27f1d672012-10-17 18:32:34 -07001491 pw.println(" mSingleDisplayDemoMode=" + mSingleDisplayDemoMode);
Jeff Brownce468a32013-11-21 16:42:03 -08001492 pw.println(" mWifiDisplayScanRequestCount=" + mWifiDisplayScanRequestCount);
Michael Wrighteedcbf12017-08-16 23:14:54 +01001493 pw.println(" mStableDisplaySize=" + mStableDisplaySize);
Kenny Guy2047db92019-02-21 13:04:05 +00001494 pw.println(" mMinimumBrightnessCurve=" + mMinimumBrightnessCurve);
Jeff Brown9e316a12012-10-08 19:17:06 -07001495
Jeff Brownbd6e1502012-08-28 03:27:37 -07001496 IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001497 ipw.increaseIndent();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001498
1499 pw.println();
1500 pw.println("Display Adapters: size=" + mDisplayAdapters.size());
Jeff Brown848c2dc2012-08-19 20:18:08 -07001501 for (DisplayAdapter adapter : mDisplayAdapters) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001502 pw.println(" " + adapter.getName());
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001503 adapter.dumpLocked(ipw);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001504 }
Craig Mautner9de49362012-08-02 14:30:30 -07001505
Jeff Brownbd6e1502012-08-28 03:27:37 -07001506 pw.println();
1507 pw.println("Display Devices: size=" + mDisplayDevices.size());
1508 for (DisplayDevice device : mDisplayDevices) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001509 pw.println(" " + device.getDisplayDeviceInfoLocked());
1510 device.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001511 }
1512
1513 final int logicalDisplayCount = mLogicalDisplays.size();
1514 pw.println();
1515 pw.println("Logical Displays: size=" + logicalDisplayCount);
1516 for (int i = 0; i < logicalDisplayCount; i++) {
1517 int displayId = mLogicalDisplays.keyAt(i);
1518 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1519 pw.println(" Display " + displayId + ":");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001520 display.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001521 }
Jeff Brownce468a32013-11-21 16:42:03 -08001522
1523 final int callbackCount = mCallbacks.size();
1524 pw.println();
1525 pw.println("Callbacks: size=" + callbackCount);
1526 for (int i = 0; i < callbackCount; i++) {
1527 CallbackRecord callback = mCallbacks.valueAt(i);
1528 pw.println(" " + i + ": mPid=" + callback.mPid
1529 + ", mWifiDisplayScanRequested=" + callback.mWifiDisplayScanRequested);
1530 }
Jeff Brownad9ef192014-04-08 17:26:30 -07001531
1532 if (mDisplayPowerController != null) {
1533 mDisplayPowerController.dump(pw);
1534 }
Michael Wright1c9977b2016-07-12 13:30:10 -07001535
1536 pw.println();
1537 mPersistentDataStore.dump(pw);
Daniel Solomon8b72c5b2018-11-25 11:07:13 -08001538
1539 final ColorDisplayServiceInternal cds = LocalServices.getService(
1540 ColorDisplayServiceInternal.class);
1541 if (cds != null) {
1542 pw.println();
1543 cds.dump(pw);
1544 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001545 }
1546 }
1547
Dan Gittik122df862018-03-28 16:59:22 +01001548 private static float[] getFloatArray(TypedArray array) {
1549 int length = array.length();
1550 float[] floatArray = new float[length];
1551 for (int i = 0; i < length; i++) {
1552 floatArray[i] = array.getFloat(i, Float.NaN);
1553 }
1554 array.recycle();
1555 return floatArray;
1556 }
1557
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001558 /**
1559 * This is the object that everything in the display manager locks on.
1560 * We make it an inner class within the {@link DisplayManagerService} to so that it is
1561 * clear that the object belongs to the display manager service and that it is
1562 * a unique object with a special purpose.
1563 */
1564 public static final class SyncRoot {
1565 }
1566
Santos Cordonee8931e2017-04-05 10:31:15 -07001567 @VisibleForTesting
1568 static class Injector {
1569 VirtualDisplayAdapter getVirtualDisplayAdapter(SyncRoot syncRoot, Context context,
1570 Handler handler, DisplayAdapter.Listener displayAdapterListener) {
1571 return new VirtualDisplayAdapter(syncRoot, context, handler, displayAdapterListener);
1572 }
Santos Cordonc22c5632017-06-21 16:03:49 -07001573
1574 long getDefaultDisplayDelayTimeout() {
1575 return WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT;
1576 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001577 }
1578
Alex Sakhartchouk879d24f2017-06-20 22:01:19 -04001579 @VisibleForTesting
1580 DisplayDeviceInfo getDisplayDeviceInfoInternal(int displayId) {
1581 synchronized (mSyncRoot) {
1582 LogicalDisplay display = mLogicalDisplays.get(displayId);
1583 if (display != null) {
1584 DisplayDevice displayDevice = display.getPrimaryDisplayDeviceLocked();
1585 return displayDevice.getDisplayDeviceInfoLocked();
1586 }
1587 return null;
1588 }
1589 }
1590
Jeff Brownbd6e1502012-08-28 03:27:37 -07001591 private final class DisplayManagerHandler extends Handler {
1592 public DisplayManagerHandler(Looper looper) {
1593 super(looper, null, true /*async*/);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001594 }
Jeff Brownbf5740e2012-08-19 23:20:02 -07001595
Jeff Brownbd6e1502012-08-28 03:27:37 -07001596 @Override
1597 public void handleMessage(Message msg) {
1598 switch (msg.what) {
Santos Cordonc22c5632017-06-21 16:03:49 -07001599 case MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS:
1600 registerDefaultDisplayAdapters();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001601 break;
1602
1603 case MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS:
1604 registerAdditionalDisplayAdapters();
1605 break;
1606
1607 case MSG_DELIVER_DISPLAY_EVENT:
1608 deliverDisplayEvent(msg.arg1, msg.arg2);
1609 break;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001610
1611 case MSG_REQUEST_TRAVERSAL:
Jeff Brown4ccb8232014-01-16 22:16:42 -08001612 mWindowManagerInternal.requestTraversalFromDisplayManager();
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001613 break;
Jeff Brownd728bf52012-09-08 18:05:28 -07001614
1615 case MSG_UPDATE_VIEWPORT: {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001616 final boolean changed;
Jeff Brownd728bf52012-09-08 18:05:28 -07001617 synchronized (mSyncRoot) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001618 changed = !mTempViewports.equals(mViewports);
1619 if (changed) {
1620 mTempViewports.clear();
1621 for (DisplayViewport d : mViewports) {
1622 mTempViewports.add(d.makeCopy());
1623 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001624 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001625 }
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001626 if (changed) {
1627 mInputManagerInternal.setDisplayViewports(mTempViewports);
1628 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001629 break;
1630 }
Kenny Guy22bd0442017-10-26 00:15:54 +01001631
Michael Wrighteef0e132017-11-21 17:57:52 +00001632 case MSG_LOAD_BRIGHTNESS_CONFIGURATION:
1633 loadBrightnessConfiguration();
1634 break;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001635 }
1636 }
1637 }
1638
1639 private final class DisplayAdapterListener implements DisplayAdapter.Listener {
1640 @Override
1641 public void onDisplayDeviceEvent(DisplayDevice device, int event) {
1642 switch (event) {
1643 case DisplayAdapter.DISPLAY_DEVICE_EVENT_ADDED:
1644 handleDisplayDeviceAdded(device);
1645 break;
1646
1647 case DisplayAdapter.DISPLAY_DEVICE_EVENT_CHANGED:
1648 handleDisplayDeviceChanged(device);
1649 break;
1650
1651 case DisplayAdapter.DISPLAY_DEVICE_EVENT_REMOVED:
1652 handleDisplayDeviceRemoved(device);
1653 break;
1654 }
1655 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001656
1657 @Override
1658 public void onTraversalRequested() {
1659 synchronized (mSyncRoot) {
Craig Mautner65d11b32012-10-01 13:59:52 -07001660 scheduleTraversalLocked(false);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001661 }
1662 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001663 }
1664
1665 private final class CallbackRecord implements DeathRecipient {
Jeff Brownce468a32013-11-21 16:42:03 -08001666 public final int mPid;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001667 private final IDisplayManagerCallback mCallback;
1668
Jeff Brownce468a32013-11-21 16:42:03 -08001669 public boolean mWifiDisplayScanRequested;
1670
Jeff Brownbd6e1502012-08-28 03:27:37 -07001671 public CallbackRecord(int pid, IDisplayManagerCallback callback) {
1672 mPid = pid;
1673 mCallback = callback;
1674 }
1675
1676 @Override
1677 public void binderDied() {
1678 if (DEBUG) {
1679 Slog.d(TAG, "Display listener for pid " + mPid + " died.");
1680 }
Jeff Brownce468a32013-11-21 16:42:03 -08001681 onCallbackDied(this);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001682 }
1683
1684 public void notifyDisplayEventAsync(int displayId, int event) {
1685 try {
1686 mCallback.onDisplayEvent(displayId, event);
1687 } catch (RemoteException ex) {
1688 Slog.w(TAG, "Failed to notify process "
1689 + mPid + " that displays changed, assuming it died.", ex);
1690 binderDied();
1691 }
1692 }
1693 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001694
Santos Cordonee8931e2017-04-05 10:31:15 -07001695 @VisibleForTesting
1696 final class BinderService extends IDisplayManager.Stub {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001697 /**
1698 * Returns information about the specified logical display.
1699 *
1700 * @param displayId The logical display id.
1701 * @return The logical display info, or null if the display does not exist. The
1702 * returned object must be treated as immutable.
1703 */
1704 @Override // Binder call
1705 public DisplayInfo getDisplayInfo(int displayId) {
1706 final int callingUid = Binder.getCallingUid();
1707 final long token = Binder.clearCallingIdentity();
1708 try {
1709 return getDisplayInfoInternal(displayId, callingUid);
1710 } finally {
1711 Binder.restoreCallingIdentity(token);
1712 }
1713 }
1714
1715 /**
1716 * Returns the list of all display ids.
1717 */
1718 @Override // Binder call
1719 public int[] getDisplayIds() {
1720 final int callingUid = Binder.getCallingUid();
1721 final long token = Binder.clearCallingIdentity();
1722 try {
1723 return getDisplayIdsInternal(callingUid);
1724 } finally {
1725 Binder.restoreCallingIdentity(token);
1726 }
1727 }
1728
Michael Wrighteedcbf12017-08-16 23:14:54 +01001729 /**
1730 * Returns the stable device display size, in pixels.
1731 */
1732 @Override // Binder call
1733 public Point getStableDisplaySize() {
1734 final long token = Binder.clearCallingIdentity();
1735 try {
1736 return getStableDisplaySizeInternal();
1737 } finally {
1738 Binder.restoreCallingIdentity(token);
1739 }
1740 }
1741
Jeff Brown4ccb8232014-01-16 22:16:42 -08001742 @Override // Binder call
1743 public void registerCallback(IDisplayManagerCallback callback) {
1744 if (callback == null) {
1745 throw new IllegalArgumentException("listener must not be null");
1746 }
1747
1748 final int callingPid = Binder.getCallingPid();
1749 final long token = Binder.clearCallingIdentity();
1750 try {
1751 registerCallbackInternal(callback, callingPid);
1752 } finally {
1753 Binder.restoreCallingIdentity(token);
1754 }
1755 }
1756
1757 @Override // Binder call
1758 public void startWifiDisplayScan() {
1759 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1760 "Permission required to start wifi display scans");
1761
1762 final int callingPid = Binder.getCallingPid();
1763 final long token = Binder.clearCallingIdentity();
1764 try {
1765 startWifiDisplayScanInternal(callingPid);
1766 } finally {
1767 Binder.restoreCallingIdentity(token);
1768 }
1769 }
1770
1771 @Override // Binder call
1772 public void stopWifiDisplayScan() {
1773 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1774 "Permission required to stop wifi display scans");
1775
1776 final int callingPid = Binder.getCallingPid();
1777 final long token = Binder.clearCallingIdentity();
1778 try {
1779 stopWifiDisplayScanInternal(callingPid);
1780 } finally {
1781 Binder.restoreCallingIdentity(token);
1782 }
1783 }
1784
1785 @Override // Binder call
1786 public void connectWifiDisplay(String address) {
1787 if (address == null) {
1788 throw new IllegalArgumentException("address must not be null");
1789 }
1790 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1791 "Permission required to connect to a wifi display");
1792
1793 final long token = Binder.clearCallingIdentity();
1794 try {
1795 connectWifiDisplayInternal(address);
1796 } finally {
1797 Binder.restoreCallingIdentity(token);
1798 }
1799 }
1800
1801 @Override // Binder call
1802 public void disconnectWifiDisplay() {
1803 // This request does not require special permissions.
1804 // Any app can request disconnection from the currently active wifi display.
1805 // This exception should no longer be needed once wifi display control moves
1806 // to the media router service.
1807
1808 final long token = Binder.clearCallingIdentity();
1809 try {
1810 disconnectWifiDisplayInternal();
1811 } finally {
1812 Binder.restoreCallingIdentity(token);
1813 }
1814 }
1815
1816 @Override // Binder call
1817 public void renameWifiDisplay(String address, String alias) {
1818 if (address == null) {
1819 throw new IllegalArgumentException("address must not be null");
1820 }
1821 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1822 "Permission required to rename to a wifi display");
1823
1824 final long token = Binder.clearCallingIdentity();
1825 try {
1826 renameWifiDisplayInternal(address, alias);
1827 } finally {
1828 Binder.restoreCallingIdentity(token);
1829 }
1830 }
1831
1832 @Override // Binder call
1833 public void forgetWifiDisplay(String address) {
1834 if (address == null) {
1835 throw new IllegalArgumentException("address must not be null");
1836 }
1837 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1838 "Permission required to forget to a wifi display");
1839
1840 final long token = Binder.clearCallingIdentity();
1841 try {
1842 forgetWifiDisplayInternal(address);
1843 } finally {
1844 Binder.restoreCallingIdentity(token);
1845 }
1846 }
1847
1848 @Override // Binder call
1849 public void pauseWifiDisplay() {
1850 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1851 "Permission required to pause a wifi display session");
1852
1853 final long token = Binder.clearCallingIdentity();
1854 try {
1855 pauseWifiDisplayInternal();
1856 } finally {
1857 Binder.restoreCallingIdentity(token);
1858 }
1859 }
1860
1861 @Override // Binder call
1862 public void resumeWifiDisplay() {
1863 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1864 "Permission required to resume a wifi display session");
1865
1866 final long token = Binder.clearCallingIdentity();
1867 try {
1868 resumeWifiDisplayInternal();
1869 } finally {
1870 Binder.restoreCallingIdentity(token);
1871 }
1872 }
1873
1874 @Override // Binder call
1875 public WifiDisplayStatus getWifiDisplayStatus() {
1876 // This request does not require special permissions.
1877 // Any app can get information about available wifi displays.
1878
1879 final long token = Binder.clearCallingIdentity();
1880 try {
1881 return getWifiDisplayStatusInternal();
1882 } finally {
1883 Binder.restoreCallingIdentity(token);
1884 }
1885 }
1886
1887 @Override // Binder call
Michael Wright1c9977b2016-07-12 13:30:10 -07001888 public void requestColorMode(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +01001889 mContext.enforceCallingOrSelfPermission(
Michael Wright1c9977b2016-07-12 13:30:10 -07001890 Manifest.permission.CONFIGURE_DISPLAY_COLOR_MODE,
1891 "Permission required to change the display color mode");
Michael Wright58e829f2015-09-15 00:13:26 +01001892 final long token = Binder.clearCallingIdentity();
1893 try {
Michael Wright1c9977b2016-07-12 13:30:10 -07001894 requestColorModeInternal(displayId, colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +01001895 } finally {
1896 Binder.restoreCallingIdentity(token);
1897 }
1898 }
1899
1900 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001901 public int createVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wrightc39d47a2014-07-08 18:07:36 -07001902 IMediaProjection projection, String packageName, String name,
Santos Cordonee8931e2017-04-05 10:31:15 -07001903 int width, int height, int densityDpi, Surface surface, int flags,
1904 String uniqueId) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001905 final int callingUid = Binder.getCallingUid();
1906 if (!validatePackageName(callingUid, packageName)) {
1907 throw new SecurityException("packageName must match the calling uid");
1908 }
Michael Wright75ee9fc2014-09-01 19:55:22 -07001909 if (callback == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001910 throw new IllegalArgumentException("appToken must not be null");
1911 }
1912 if (TextUtils.isEmpty(name)) {
1913 throw new IllegalArgumentException("name must be non-null and non-empty");
1914 }
1915 if (width <= 0 || height <= 0 || densityDpi <= 0) {
1916 throw new IllegalArgumentException("width, height, and densityDpi must be "
1917 + "greater than 0");
1918 }
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001919 if (surface != null && surface.isSingleBuffered()) {
Pablo Ceballosaff2f942016-07-29 14:49:55 -07001920 throw new IllegalArgumentException("Surface can't be single-buffered");
1921 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001922
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001923 if ((flags & VIRTUAL_DISPLAY_FLAG_PUBLIC) != 0) {
1924 flags |= VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
1925
1926 // Public displays can't be allowed to show content when locked.
Andrii Kulian7211d2e2017-01-27 15:58:05 -08001927 if ((flags & VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD) != 0) {
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001928 throw new IllegalArgumentException(
1929 "Public display must not be marked as SHOW_WHEN_LOCKED_INSECURE");
1930 }
Michael Wright6720be42014-07-29 19:14:16 -07001931 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001932 if ((flags & VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY) != 0) {
1933 flags &= ~VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
Michael Wright6720be42014-07-29 19:14:16 -07001934 }
1935
Michael Wrightc39d47a2014-07-08 18:07:36 -07001936 if (projection != null) {
1937 try {
1938 if (!getProjectionService().isValidMediaProjection(projection)) {
1939 throw new SecurityException("Invalid media projection");
1940 }
Michael Wright6720be42014-07-29 19:14:16 -07001941 flags = projection.applyVirtualDisplayFlags(flags);
Michael Wrightc39d47a2014-07-08 18:07:36 -07001942 } catch (RemoteException e) {
Michael Wright6720be42014-07-29 19:14:16 -07001943 throw new SecurityException("unable to validate media projection or flags");
Michael Wrightc39d47a2014-07-08 18:07:36 -07001944 }
1945 }
1946
Michael Wright6720be42014-07-29 19:14:16 -07001947 if (callingUid != Process.SYSTEM_UID &&
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001948 (flags & VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001949 if (!canProjectVideo(projection)) {
1950 throw new SecurityException("Requires CAPTURE_VIDEO_OUTPUT or "
1951 + "CAPTURE_SECURE_VIDEO_OUTPUT permission, or an appropriate "
1952 + "MediaProjection token in order to create a screen sharing virtual "
1953 + "display.");
1954 }
1955 }
Santos Cordonb6992f22018-01-30 14:51:20 -08001956 if (callingUid != Process.SYSTEM_UID && (flags & VIRTUAL_DISPLAY_FLAG_SECURE) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001957 if (!canProjectSecureVideo(projection)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001958 throw new SecurityException("Requires CAPTURE_SECURE_VIDEO_OUTPUT "
Michael Wrightc39d47a2014-07-08 18:07:36 -07001959 + "or an appropriate MediaProjection token to create a "
1960 + "secure virtual display.");
Jeff Brown4ccb8232014-01-16 22:16:42 -08001961 }
1962 }
1963
1964 final long token = Binder.clearCallingIdentity();
1965 try {
Santos Cordonee8931e2017-04-05 10:31:15 -07001966 return createVirtualDisplayInternal(callback, projection, callingUid, packageName,
1967 name, width, height, densityDpi, surface, flags, uniqueId);
Jeff Brown4ccb8232014-01-16 22:16:42 -08001968 } finally {
1969 Binder.restoreCallingIdentity(token);
1970 }
1971 }
1972
1973 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001974 public void resizeVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wright01e840f2014-06-26 16:03:25 -07001975 int width, int height, int densityDpi) {
1976 final long token = Binder.clearCallingIdentity();
1977 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001978 resizeVirtualDisplayInternal(callback.asBinder(), width, height, densityDpi);
Michael Wright01e840f2014-06-26 16:03:25 -07001979 } finally {
1980 Binder.restoreCallingIdentity(token);
1981 }
1982 }
1983
1984 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001985 public void setVirtualDisplaySurface(IVirtualDisplayCallback callback, Surface surface) {
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001986 if (surface != null && surface.isSingleBuffered()) {
1987 throw new IllegalArgumentException("Surface can't be single-buffered");
1988 }
Jeff Brown92207df2014-04-16 13:16:07 -07001989 final long token = Binder.clearCallingIdentity();
1990 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001991 setVirtualDisplaySurfaceInternal(callback.asBinder(), surface);
Jeff Brown92207df2014-04-16 13:16:07 -07001992 } finally {
1993 Binder.restoreCallingIdentity(token);
1994 }
1995 }
1996
1997 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001998 public void releaseVirtualDisplay(IVirtualDisplayCallback callback) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001999 final long token = Binder.clearCallingIdentity();
2000 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07002001 releaseVirtualDisplayInternal(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -08002002 } finally {
2003 Binder.restoreCallingIdentity(token);
2004 }
2005 }
2006
2007 @Override // Binder call
chaviwda4c6942018-11-07 15:52:56 -08002008 public void setVirtualDisplayState(IVirtualDisplayCallback callback, boolean isOn) {
2009 final long token = Binder.clearCallingIdentity();
2010 try {
2011 setVirtualDisplayStateInternal(callback.asBinder(), isOn);
2012 } finally {
2013 Binder.restoreCallingIdentity(token);
2014 }
2015 }
2016
2017 @Override // Binder call
Jeff Brown4ccb8232014-01-16 22:16:42 -08002018 public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06002019 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
Jeff Brown4ccb8232014-01-16 22:16:42 -08002020
2021 final long token = Binder.clearCallingIdentity();
2022 try {
2023 dumpInternal(pw);
2024 } finally {
2025 Binder.restoreCallingIdentity(token);
2026 }
2027 }
2028
Kenny Guy22bd0442017-10-26 00:15:54 +01002029 @Override // Binder call
Kenny Guy29aa30e2017-11-30 13:43:46 +00002030 public ParceledListSlice<BrightnessChangeEvent> getBrightnessEvents(String callingPackage) {
Kenny Guy22bd0442017-10-26 00:15:54 +01002031 mContext.enforceCallingOrSelfPermission(
2032 Manifest.permission.BRIGHTNESS_SLIDER_USAGE,
2033 "Permission to read brightness events.");
Kenny Guy29aa30e2017-11-30 13:43:46 +00002034
2035 final int callingUid = Binder.getCallingUid();
2036 AppOpsManager appOpsManager = mContext.getSystemService(AppOpsManager.class);
Jeff Sharkeyd9311192018-04-16 16:50:34 +00002037 final int mode = appOpsManager.noteOp(AppOpsManager.OP_GET_USAGE_STATS,
Kenny Guy29aa30e2017-11-30 13:43:46 +00002038 callingUid, callingPackage);
2039 final boolean hasUsageStats;
2040 if (mode == AppOpsManager.MODE_DEFAULT) {
2041 // The default behavior here is to check if PackageManager has given the app
2042 // permission.
2043 hasUsageStats = mContext.checkCallingPermission(
2044 Manifest.permission.PACKAGE_USAGE_STATS)
2045 == PackageManager.PERMISSION_GRANTED;
2046 } else {
2047 hasUsageStats = mode == AppOpsManager.MODE_ALLOWED;
2048 }
2049
2050 final int userId = UserHandle.getUserId(callingUid);
Kenny Guy22bd0442017-10-26 00:15:54 +01002051 final long token = Binder.clearCallingIdentity();
2052 try {
Michael Wright144aac92017-12-21 18:37:41 +00002053 synchronized (mSyncRoot) {
2054 return mDisplayPowerController.getBrightnessEvents(userId, hasUsageStats);
2055 }
Kenny Guy22bd0442017-10-26 00:15:54 +01002056 } finally {
2057 Binder.restoreCallingIdentity(token);
2058 }
2059 }
2060
Michael Wrighteef0e132017-11-21 17:57:52 +00002061 @Override // Binder call
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002062 public ParceledListSlice<AmbientBrightnessDayStats> getAmbientBrightnessStats() {
2063 mContext.enforceCallingOrSelfPermission(
2064 Manifest.permission.ACCESS_AMBIENT_LIGHT_STATS,
2065 "Permission required to to access ambient light stats.");
2066 final int callingUid = Binder.getCallingUid();
2067 final int userId = UserHandle.getUserId(callingUid);
2068 final long token = Binder.clearCallingIdentity();
2069 try {
2070 synchronized (mSyncRoot) {
2071 return mDisplayPowerController.getAmbientBrightnessStats(userId);
2072 }
2073 } finally {
2074 Binder.restoreCallingIdentity(token);
2075 }
2076 }
2077
2078 @Override // Binder call
Michael Wrighteef0e132017-11-21 17:57:52 +00002079 public void setBrightnessConfigurationForUser(
Kenny Guy05ce8092018-01-17 13:44:20 +00002080 BrightnessConfiguration c, @UserIdInt int userId, String packageName) {
Michael Wrighteef0e132017-11-21 17:57:52 +00002081 mContext.enforceCallingOrSelfPermission(
2082 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
2083 "Permission required to change the display's brightness configuration");
2084 if (userId != UserHandle.getCallingUserId()) {
2085 mContext.enforceCallingOrSelfPermission(
2086 Manifest.permission.INTERACT_ACROSS_USERS,
2087 "Permission required to change the display brightness"
2088 + " configuration of another user");
2089 }
Kenny Guy05ce8092018-01-17 13:44:20 +00002090 if (packageName != null && !validatePackageName(getCallingUid(), packageName)) {
2091 packageName = null;
2092 }
Michael Wrighteef0e132017-11-21 17:57:52 +00002093 final long token = Binder.clearCallingIdentity();
2094 try {
Kenny Guy05ce8092018-01-17 13:44:20 +00002095 setBrightnessConfigurationForUserInternal(c, userId, packageName);
Michael Wrighteef0e132017-11-21 17:57:52 +00002096 } finally {
2097 Binder.restoreCallingIdentity(token);
2098 }
2099 }
2100
Michael Wrightd8460232018-01-16 18:04:59 +00002101 @Override // Binder call
Kenny Guy6d1009f2018-03-14 14:28:23 +00002102 public BrightnessConfiguration getBrightnessConfigurationForUser(int userId) {
2103 mContext.enforceCallingOrSelfPermission(
2104 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
2105 "Permission required to read the display's brightness configuration");
2106 if (userId != UserHandle.getCallingUserId()) {
2107 mContext.enforceCallingOrSelfPermission(
2108 Manifest.permission.INTERACT_ACROSS_USERS,
2109 "Permission required to read the display brightness"
2110 + " configuration of another user");
2111 }
2112 final long token = Binder.clearCallingIdentity();
2113 try {
2114 final int userSerial = getUserManager().getUserSerialNumber(userId);
2115 synchronized (mSyncRoot) {
2116 BrightnessConfiguration config =
2117 mPersistentDataStore.getBrightnessConfiguration(userSerial);
2118 if (config == null) {
2119 config = mDisplayPowerController.getDefaultBrightnessConfiguration();
2120 }
2121 return config;
2122 }
2123 } finally {
2124 Binder.restoreCallingIdentity(token);
2125 }
2126 }
2127
2128 @Override // Binder call
2129 public BrightnessConfiguration getDefaultBrightnessConfiguration() {
2130 mContext.enforceCallingOrSelfPermission(
2131 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
2132 "Permission required to read the display's default brightness configuration");
2133 final long token = Binder.clearCallingIdentity();
2134 try {
2135 synchronized (mSyncRoot) {
2136 return mDisplayPowerController.getDefaultBrightnessConfiguration();
2137 }
2138 } finally {
2139 Binder.restoreCallingIdentity(token);
2140 }
2141 }
2142
2143 @Override // Binder call
Michael Wrightd8460232018-01-16 18:04:59 +00002144 public void setTemporaryBrightness(int brightness) {
2145 mContext.enforceCallingOrSelfPermission(
2146 Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS,
2147 "Permission required to set the display's brightness");
2148 final long token = Binder.clearCallingIdentity();
2149 try {
2150 synchronized (mSyncRoot) {
2151 mDisplayPowerController.setTemporaryBrightness(brightness);
2152 }
2153 } finally {
2154 Binder.restoreCallingIdentity(token);
2155 }
2156 }
2157
2158 @Override // Binder call
2159 public void setTemporaryAutoBrightnessAdjustment(float adjustment) {
2160 mContext.enforceCallingOrSelfPermission(
2161 Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS,
2162 "Permission required to set the display's auto brightness adjustment");
2163 final long token = Binder.clearCallingIdentity();
2164 try {
2165 synchronized (mSyncRoot) {
2166 mDisplayPowerController.setTemporaryAutoBrightnessAdjustment(adjustment);
2167 }
2168 } finally {
2169 Binder.restoreCallingIdentity(token);
2170 }
2171 }
2172
Dan Gittik7a32fba2018-03-28 12:19:38 +01002173 @Override // Binder call
2174 public void onShellCommand(FileDescriptor in, FileDescriptor out,
2175 FileDescriptor err, String[] args, ShellCallback callback,
2176 ResultReceiver resultReceiver) {
2177 final long token = Binder.clearCallingIdentity();
2178 try {
2179 DisplayManagerShellCommand command = new DisplayManagerShellCommand(this);
2180 command.exec(this, in, out, err, args, callback, resultReceiver);
2181 } finally {
2182 Binder.restoreCallingIdentity(token);
2183 }
2184 }
2185
Dan Gittik122df862018-03-28 16:59:22 +01002186 @Override // Binder call
2187 public Curve getMinimumBrightnessCurve() {
2188 final long token = Binder.clearCallingIdentity();
2189 try {
2190 return getMinimumBrightnessCurveInternal();
2191 } finally {
2192 Binder.restoreCallingIdentity(token);
2193 }
2194 }
2195
Peiyong Lin277eaff2019-01-16 16:18:22 -08002196 @Override // Binder call
2197 public int getPreferredWideGamutColorSpaceId() {
2198 final long token = Binder.clearCallingIdentity();
2199 try {
2200 return getPreferredWideGamutColorSpaceIdInternal();
2201 } finally {
2202 Binder.restoreCallingIdentity(token);
2203 }
2204 }
2205
Dan Gittik7a32fba2018-03-28 12:19:38 +01002206 void setBrightness(int brightness) {
2207 Settings.System.putIntForUser(mContext.getContentResolver(),
2208 Settings.System.SCREEN_BRIGHTNESS, brightness, UserHandle.USER_CURRENT);
2209 }
2210
2211 void resetBrightnessConfiguration() {
2212 setBrightnessConfigurationForUserInternal(null, mContext.getUserId(),
2213 mContext.getPackageName());
2214 }
2215
Dan Gittika5a2d632019-01-09 14:25:29 +00002216 void setAutoBrightnessLoggingEnabled(boolean enabled) {
2217 if (mDisplayPowerController != null) {
2218 synchronized (mSyncRoot) {
2219 mDisplayPowerController.setAutoBrightnessLoggingEnabled(enabled);
2220 }
2221 }
2222 }
2223
Dan Gittik8dbd7e92018-12-03 15:35:53 +00002224 void setDisplayWhiteBalanceLoggingEnabled(boolean enabled) {
2225 if (mDisplayPowerController != null) {
2226 synchronized (mSyncRoot) {
2227 mDisplayPowerController.setDisplayWhiteBalanceLoggingEnabled(enabled);
2228 }
2229 }
2230 }
2231
2232 void setAmbientColorTemperatureOverride(float cct) {
2233 if (mDisplayPowerController != null) {
2234 synchronized (mSyncRoot) {
2235 mDisplayPowerController.setAmbientColorTemperatureOverride(cct);
2236 }
2237 }
2238 }
2239
Jeff Brown4ccb8232014-01-16 22:16:42 -08002240 private boolean validatePackageName(int uid, String packageName) {
2241 if (packageName != null) {
2242 String[] packageNames = mContext.getPackageManager().getPackagesForUid(uid);
2243 if (packageNames != null) {
2244 for (String n : packageNames) {
2245 if (n.equals(packageName)) {
2246 return true;
2247 }
2248 }
2249 }
2250 }
2251 return false;
2252 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07002253
2254 private boolean canProjectVideo(IMediaProjection projection) {
2255 if (projection != null) {
2256 try {
2257 if (projection.canProjectVideo()) {
2258 return true;
2259 }
2260 } catch (RemoteException e) {
2261 Slog.e(TAG, "Unable to query projection service for permissions", e);
2262 }
2263 }
2264 if (mContext.checkCallingPermission(
2265 android.Manifest.permission.CAPTURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07002266 == PackageManager.PERMISSION_GRANTED) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07002267 return true;
2268 }
2269 return canProjectSecureVideo(projection);
2270 }
2271
2272 private boolean canProjectSecureVideo(IMediaProjection projection) {
2273 if (projection != null) {
2274 try {
2275 if (projection.canProjectSecureVideo()){
2276 return true;
2277 }
2278 } catch (RemoteException e) {
2279 Slog.e(TAG, "Unable to query projection service for permissions", e);
2280 }
2281 }
2282 return mContext.checkCallingPermission(
2283 android.Manifest.permission.CAPTURE_SECURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07002284 == PackageManager.PERMISSION_GRANTED;
Michael Wrightc39d47a2014-07-08 18:07:36 -07002285 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08002286 }
2287
2288 private final class LocalService extends DisplayManagerInternal {
2289 @Override
Jeff Brown037c33e2014-04-09 00:31:55 -07002290 public void initPowerManagement(final DisplayPowerCallbacks callbacks, Handler handler,
Jeff Brownad9ef192014-04-08 17:26:30 -07002291 SensorManager sensorManager) {
2292 synchronized (mSyncRoot) {
Jeff Brown037c33e2014-04-09 00:31:55 -07002293 DisplayBlanker blanker = new DisplayBlanker() {
2294 @Override
Jeff Brown5d6443b2015-04-10 20:15:01 -07002295 public void requestDisplayState(int state, int brightness) {
Jeff Brown037c33e2014-04-09 00:31:55 -07002296 // The order of operations is important for legacy reasons.
2297 if (state == Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07002298 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07002299 }
2300
2301 callbacks.onDisplayStateChange(state);
2302
2303 if (state != Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07002304 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07002305 }
2306 }
2307 };
Jeff Brownad9ef192014-04-08 17:26:30 -07002308 mDisplayPowerController = new DisplayPowerController(
Jeff Brown037c33e2014-04-09 00:31:55 -07002309 mContext, callbacks, handler, sensorManager, blanker);
Jeff Brownad9ef192014-04-08 17:26:30 -07002310 }
Michael Wrighteef0e132017-11-21 17:57:52 +00002311
2312 mHandler.sendEmptyMessage(MSG_LOAD_BRIGHTNESS_CONFIGURATION);
Jeff Brownad9ef192014-04-08 17:26:30 -07002313 }
2314
2315 @Override
2316 public boolean requestPowerState(DisplayPowerRequest request,
2317 boolean waitForNegativeProximity) {
Michael Wrighteef0e132017-11-21 17:57:52 +00002318 synchronized (mSyncRoot) {
2319 return mDisplayPowerController.requestPowerState(request,
2320 waitForNegativeProximity);
2321 }
Jeff Brownad9ef192014-04-08 17:26:30 -07002322 }
2323
2324 @Override
2325 public boolean isProximitySensorAvailable() {
Michael Wrighteef0e132017-11-21 17:57:52 +00002326 synchronized (mSyncRoot) {
2327 return mDisplayPowerController.isProximitySensorAvailable();
2328 }
Jeff Brownad9ef192014-04-08 17:26:30 -07002329 }
2330
2331 @Override
Riddle Hsu654a6f92018-07-13 22:59:36 +08002332 public boolean screenshot(int displayId, Surface outSurface) {
Dominik Laskowskie912ae32019-01-26 11:19:36 -08002333 return screenshotInternal(displayId, outSurface);
Riddle Hsu654a6f92018-07-13 22:59:36 +08002334 }
2335
2336 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08002337 public DisplayInfo getDisplayInfo(int displayId) {
2338 return getDisplayInfoInternal(displayId, Process.myUid());
2339 }
2340
2341 @Override
2342 public void registerDisplayTransactionListener(DisplayTransactionListener listener) {
2343 if (listener == null) {
2344 throw new IllegalArgumentException("listener must not be null");
2345 }
2346
2347 registerDisplayTransactionListenerInternal(listener);
2348 }
2349
2350 @Override
2351 public void unregisterDisplayTransactionListener(DisplayTransactionListener listener) {
2352 if (listener == null) {
2353 throw new IllegalArgumentException("listener must not be null");
2354 }
2355
2356 unregisterDisplayTransactionListenerInternal(listener);
2357 }
2358
2359 @Override
2360 public void setDisplayInfoOverrideFromWindowManager(int displayId, DisplayInfo info) {
2361 setDisplayInfoOverrideFromWindowManagerInternal(displayId, info);
2362 }
2363
2364 @Override
Andrii Kuliancd097992017-03-23 18:31:59 -07002365 public void getNonOverrideDisplayInfo(int displayId, DisplayInfo outInfo) {
2366 getNonOverrideDisplayInfoInternal(displayId, outInfo);
2367 }
2368
2369 @Override
Robert Carrae606b42018-02-15 15:36:23 -08002370 public void performTraversal(SurfaceControl.Transaction t) {
2371 performTraversalInternal(t);
Jeff Brown4ccb8232014-01-16 22:16:42 -08002372 }
2373
2374 @Override
Michael Wright3f145a22014-07-22 19:46:03 -07002375 public void setDisplayProperties(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07002376 float requestedRefreshRate, int requestedMode, boolean inTraversal) {
2377 setDisplayPropertiesInternal(displayId, hasContent, requestedRefreshRate,
2378 requestedMode, inTraversal);
Jeff Brown4ccb8232014-01-16 22:16:42 -08002379 }
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08002380
2381 @Override
2382 public void setDisplayOffsets(int displayId, int x, int y) {
2383 setDisplayOffsetsInternal(displayId, x, y);
2384 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002385
2386 @Override
Sam Lin4c3ac2b2019-02-18 04:50:26 -08002387 public void setDisplayScalingDisabled(int displayId, boolean disableScaling) {
2388 setDisplayScalingDisabledInternal(displayId, disableScaling);
2389 }
2390
2391 @Override
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002392 public void setDisplayAccessUIDs(SparseArray<IntArray> newDisplayAccessUIDs) {
2393 setDisplayAccessUIDsInternal(newDisplayAccessUIDs);
2394 }
2395
2396 @Override
2397 public boolean isUidPresentOnDisplay(int uid, int displayId) {
2398 return isUidPresentOnDisplayInternal(uid, displayId);
2399 }
Kenny Guycfe7b702017-11-14 21:04:58 +00002400
2401 @Override
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002402 public void persistBrightnessTrackerState() {
Michael Wright144aac92017-12-21 18:37:41 +00002403 synchronized (mSyncRoot) {
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002404 mDisplayPowerController.persistBrightnessTrackerState();
Michael Wright144aac92017-12-21 18:37:41 +00002405 }
Kenny Guycfe7b702017-11-14 21:04:58 +00002406 }
Adrian Roose1d68cd2018-01-17 12:54:50 +01002407
2408 @Override
2409 public void onOverlayChanged() {
2410 synchronized (mSyncRoot) {
Adrian Roos898ec382018-01-17 12:54:50 +01002411 for (int i = 0; i < mDisplayDevices.size(); i++) {
2412 mDisplayDevices.get(i).onOverlayChangedLocked();
Adrian Roose1d68cd2018-01-17 12:54:50 +01002413 }
2414 }
2415 }
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07002416
2417 @Override
2418 public DisplayedContentSamplingAttributes getDisplayedContentSamplingAttributes(
2419 int displayId) {
2420 return getDisplayedContentSamplingAttributesInternal(displayId);
2421 }
2422
2423 @Override
2424 public boolean setDisplayedContentSamplingEnabled(
2425 int displayId, boolean enable, int componentMask, int maxFrames) {
2426 return setDisplayedContentSamplingEnabledInternal(
2427 displayId, enable, componentMask, maxFrames);
2428 }
2429
2430 @Override
2431 public DisplayedContentSample getDisplayedContentSample(int displayId,
2432 long maxFrames, long timestamp) {
2433 return getDisplayedContentSampleInternal(displayId, maxFrames, timestamp);
2434 }
2435
Jeff Brown4ccb8232014-01-16 22:16:42 -08002436 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07002437}