blob: 0a1a9a20e0da28dbb4c4eccfa22064f850d446a2 [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;
Michael Wrighteedcbf12017-08-16 23:14:54 +010039import android.graphics.Point;
Jeff Brownad9ef192014-04-08 17:26:30 -070040import android.hardware.SensorManager;
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +000041import android.hardware.display.AmbientBrightnessDayStats;
Kenny Guy22bd0442017-10-26 00:15:54 +010042import android.hardware.display.BrightnessChangeEvent;
Michael Wrighteef0e132017-11-21 17:57:52 +000043import android.hardware.display.BrightnessConfiguration;
Dan Gittik122df862018-03-28 16:59:22 +010044import android.hardware.display.Curve;
Jeff Brownbd6e1502012-08-28 03:27:37 -070045import android.hardware.display.DisplayManagerGlobal;
Jeff Brown4ccb8232014-01-16 22:16:42 -080046import android.hardware.display.DisplayManagerInternal;
Jeff Brown4ccb8232014-01-16 22:16:42 -080047import android.hardware.display.DisplayManagerInternal.DisplayTransactionListener;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +010048import android.hardware.display.DisplayViewport;
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -070049import android.hardware.display.DisplayedContentSample;
50import android.hardware.display.DisplayedContentSamplingAttributes;
Jeff Brownfa25bf52012-07-23 19:26:30 -070051import android.hardware.display.IDisplayManager;
Jeff Brownbd6e1502012-08-28 03:27:37 -070052import android.hardware.display.IDisplayManagerCallback;
Michael Wright75ee9fc2014-09-01 19:55:22 -070053import android.hardware.display.IVirtualDisplayCallback;
Jeff Browne08ae382012-09-07 20:36:36 -070054import android.hardware.display.WifiDisplayStatus;
Jeff Brown4ccb8232014-01-16 22:16:42 -080055import android.hardware.input.InputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -070056import android.media.projection.IMediaProjection;
57import android.media.projection.IMediaProjectionManager;
Jeff Brownfa25bf52012-07-23 19:26:30 -070058import android.os.Binder;
Jeff Brownbd6e1502012-08-28 03:27:37 -070059import android.os.Handler;
Jeff Brown64a55af2012-08-26 02:47:39 -070060import android.os.IBinder;
Jeff Brown4ccb8232014-01-16 22:16:42 -080061import android.os.IBinder.DeathRecipient;
Jeff Brownbd6e1502012-08-28 03:27:37 -070062import android.os.Looper;
63import android.os.Message;
Jeff Brown5d6443b2015-04-10 20:15:01 -070064import android.os.PowerManager;
Craig Mautner4504de52013-12-20 09:06:56 -080065import android.os.Process;
Jeff Brownbd6e1502012-08-28 03:27:37 -070066import android.os.RemoteException;
Dan Gittik7a32fba2018-03-28 12:19:38 +010067import android.os.ResultReceiver;
Michael Wrightc39d47a2014-07-08 18:07:36 -070068import android.os.ServiceManager;
Dan Gittik7a32fba2018-03-28 12:19:38 +010069import android.os.ShellCallback;
Jeff Brownbd6e1502012-08-28 03:27:37 -070070import android.os.SystemClock;
Jeff Brownfa25bf52012-07-23 19:26:30 -070071import android.os.SystemProperties;
Jeff Brown5d6443b2015-04-10 20:15:01 -070072import android.os.Trace;
Kenny Guy22bd0442017-10-26 00:15:54 +010073import android.os.UserHandle;
Michael Wrighteef0e132017-11-21 17:57:52 +000074import android.os.UserManager;
Dan Gittik7a32fba2018-03-28 12:19:38 +010075import android.provider.Settings;
Jeff Browna506a6e2013-06-04 00:02:38 -070076import android.text.TextUtils;
Andrii Kulianfb1bf692017-01-17 11:17:34 -080077import android.util.IntArray;
Dan Gittik122df862018-03-28 16:59:22 +010078import android.util.Pair;
Jeff Brownbd6e1502012-08-28 03:27:37 -070079import android.util.Slog;
80import android.util.SparseArray;
Dan Gittik122df862018-03-28 16:59:22 +010081import android.util.Spline;
Jeff Brownfa25bf52012-07-23 19:26:30 -070082import android.view.Display;
83import android.view.DisplayInfo;
Jeff Browna506a6e2013-06-04 00:02:38 -070084import android.view.Surface;
Robert Carrae606b42018-02-15 15:36:23 -080085import android.view.SurfaceControl;
Jeff Browna506a6e2013-06-04 00:02:38 -070086
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +010087import com.android.internal.annotations.GuardedBy;
88import com.android.internal.annotations.VisibleForTesting;
89import com.android.internal.util.DumpUtils;
90import com.android.internal.util.IndentingPrintWriter;
Jorim Jaggied7993b2017-03-28 18:50:01 +010091import com.android.server.AnimationThread;
Jeff Brown4ccb8232014-01-16 22:16:42 -080092import com.android.server.DisplayThread;
93import com.android.server.LocalServices;
94import com.android.server.SystemService;
Dianne Hackborn8d044e82013-04-30 17:24:15 -070095import com.android.server.UiThread;
Jorim Jaggi21c39a72017-10-20 15:47:51 +020096import com.android.server.wm.SurfaceAnimationThread;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +010097import com.android.server.wm.WindowManagerInternal;
Jeff Brownfa25bf52012-07-23 19:26:30 -070098
99import java.io.FileDescriptor;
100import java.io.PrintWriter;
101import java.util.ArrayList;
Jeff Browna506a6e2013-06-04 00:02:38 -0700102import java.util.Arrays;
Jeff Browne75926d2014-09-18 15:24:49 -0700103import java.util.List;
Jeff Brown7f3994e2012-12-04 14:04:28 -0800104import java.util.concurrent.CopyOnWriteArrayList;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700105
106/**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700107 * Manages attached displays.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700108 * <p>
Jeff Brownbd6e1502012-08-28 03:27:37 -0700109 * The {@link DisplayManagerService} manages the global lifecycle of displays,
110 * decides how to configure logical displays based on the physical display devices currently
111 * attached, sends notifications to the system and to applications when the state
112 * changes, and so on.
113 * </p><p>
114 * The display manager service relies on a collection of {@link DisplayAdapter} components,
115 * for discovering and configuring physical display devices attached to the system.
116 * There are separate display adapters for each manner that devices are attached:
117 * one display adapter for built-in local displays, one for simulated non-functional
118 * displays when the system is headless, one for simulated overlay displays used for
119 * development, one for wifi displays, etc.
120 * </p><p>
121 * Display adapters are only weakly coupled to the display manager service.
122 * Display adapters communicate changes in display device state to the display manager
Craig Mautner722285e2012-09-07 13:55:58 -0700123 * service asynchronously via a {@link DisplayAdapter.Listener} registered
Jeff Brownbd6e1502012-08-28 03:27:37 -0700124 * by the display manager service. This separation of concerns is important for
125 * two main reasons. First, it neatly encapsulates the responsibilities of these
126 * two classes: display adapters handle individual display devices whereas
127 * the display manager service handles the global state. Second, it eliminates
128 * the potential for deadlocks resulting from asynchronous display device discovery.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700129 * </p>
130 *
131 * <h3>Synchronization</h3>
132 * <p>
133 * Because the display manager may be accessed by multiple threads, the synchronization
134 * story gets a little complicated. In particular, the window manager may call into
135 * the display manager while holding a surface transaction with the expectation that
136 * it can apply changes immediately. Unfortunately, that means we can't just do
137 * everything asynchronously (*grump*).
Jeff Brownbd6e1502012-08-28 03:27:37 -0700138 * </p><p>
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700139 * To make this work, all of the objects that belong to the display manager must
140 * use the same lock. We call this lock the synchronization root and it has a unique
141 * type {@link DisplayManagerService.SyncRoot}. Methods that require this lock are
142 * named with the "Locked" suffix.
143 * </p><p>
144 * Where things get tricky is that the display manager is not allowed to make
145 * any potentially reentrant calls, especially into the window manager. We generally
146 * avoid this by making all potentially reentrant out-calls asynchronous.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700147 * </p>
148 */
Jeff Brown4ccb8232014-01-16 22:16:42 -0800149public final class DisplayManagerService extends SystemService {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700150 private static final String TAG = "DisplayManagerService";
Jeff Brownbd6e1502012-08-28 03:27:37 -0700151 private static final boolean DEBUG = false;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700152
Jeff Brownbbd28a22012-09-20 16:47:15 -0700153 // When this system property is set to 0, WFD is forcibly disabled on boot.
154 // When this system property is set to 1, WFD is forcibly enabled on boot.
155 // Otherwise WFD is enabled according to the value of config_enableWifiDisplay.
156 private static final String FORCE_WIFI_DISPLAY_ENABLE = "persist.debug.wfd.enable";
157
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200158 private static final String PROP_DEFAULT_DISPLAY_TOP_INSET = "persist.sys.displayinset.top";
159
Jeff Brownbd6e1502012-08-28 03:27:37 -0700160 private static final long WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT = 10000;
161
Santos Cordonc22c5632017-06-21 16:03:49 -0700162 private static final int MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS = 1;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700163 private static final int MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS = 2;
164 private static final int MSG_DELIVER_DISPLAY_EVENT = 3;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700165 private static final int MSG_REQUEST_TRAVERSAL = 4;
Jeff Brownd728bf52012-09-08 18:05:28 -0700166 private static final int MSG_UPDATE_VIEWPORT = 5;
Kenny Guy22bd0442017-10-26 00:15:54 +0100167 private static final int MSG_REGISTER_BRIGHTNESS_TRACKER = 6;
Michael Wrighteef0e132017-11-21 17:57:52 +0000168 private static final int MSG_LOAD_BRIGHTNESS_CONFIGURATION = 7;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700169
Jeff Brownb880d882014-02-10 19:47:07 -0800170 private final Context mContext;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700171 private final DisplayManagerHandler mHandler;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700172 private final Handler mUiHandler;
173 private final DisplayAdapterListener mDisplayAdapterListener;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800174 private WindowManagerInternal mWindowManagerInternal;
175 private InputManagerInternal mInputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -0700176 private IMediaProjectionManager mProjectionService;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700177
178 // The synchronization root for the display manager.
179 // This lock guards most of the display manager's state.
Craig Mautner0bf6ec92012-12-18 08:33:27 -0800180 // NOTE: This is synchronized on while holding WindowManagerService.mWindowMap so never call
181 // into WindowManagerService methods that require mWindowMap while holding this unless you are
182 // very very sure that no deadlock can occur.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700183 private final SyncRoot mSyncRoot = new SyncRoot();
184
185 // True if in safe mode.
186 // This option may disable certain display adapters.
187 public boolean mSafeMode;
188
189 // True if we are in a special boot mode where only core applications and
190 // services should be started. This option may disable certain display adapters.
191 public boolean mOnlyCore;
192
Jeff Brown27f1d672012-10-17 18:32:34 -0700193 // True if the display manager service should pretend there is only one display
194 // and only tell applications about the existence of the default logical display.
195 // The display manager can still mirror content to secondary displays but applications
196 // cannot present unique content on those displays.
197 // Used for demonstration purposes only.
198 private final boolean mSingleDisplayDemoMode;
199
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700200 // All callback records indexed by calling process id.
201 public final SparseArray<CallbackRecord> mCallbacks =
Jeff Brownbd6e1502012-08-28 03:27:37 -0700202 new SparseArray<CallbackRecord>();
Jeff Brownfa25bf52012-07-23 19:26:30 -0700203
Jeff Brownbd6e1502012-08-28 03:27:37 -0700204 // List of all currently registered display adapters.
205 private final ArrayList<DisplayAdapter> mDisplayAdapters = new ArrayList<DisplayAdapter>();
206
207 // List of all currently connected display devices.
208 private final ArrayList<DisplayDevice> mDisplayDevices = new ArrayList<DisplayDevice>();
209
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700210 // List of all logical displays indexed by logical display id.
211 private final SparseArray<LogicalDisplay> mLogicalDisplays =
212 new SparseArray<LogicalDisplay>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700213 private int mNextNonDefaultDisplayId = Display.DEFAULT_DISPLAY + 1;
214
Jeff Brown7f3994e2012-12-04 14:04:28 -0800215 // List of all display transaction listeners.
216 private final CopyOnWriteArrayList<DisplayTransactionListener> mDisplayTransactionListeners =
217 new CopyOnWriteArrayList<DisplayTransactionListener>();
218
Jeff Brownad9ef192014-04-08 17:26:30 -0700219 // Display power controller.
220 private DisplayPowerController mDisplayPowerController;
221
Jeff Brown037c33e2014-04-09 00:31:55 -0700222 // The overall display state, independent of changes that might influence one
223 // display or another in particular.
Jeff Brown5d6443b2015-04-10 20:15:01 -0700224 private int mGlobalDisplayState = Display.STATE_ON;
225
226 // The overall display brightness.
227 // For now, this only applies to the built-in display but we may split it up eventually.
228 private int mGlobalDisplayBrightness = PowerManager.BRIGHTNESS_DEFAULT;
Jeff Brown9e316a12012-10-08 19:17:06 -0700229
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700230 // Set to true when there are pending display changes that have yet to be applied
231 // to the surface flinger state.
232 private boolean mPendingTraversal;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700233
Jeff Browne08ae382012-09-07 20:36:36 -0700234 // The Wifi display adapter, or null if not registered.
235 private WifiDisplayAdapter mWifiDisplayAdapter;
236
Jeff Brownce468a32013-11-21 16:42:03 -0800237 // The number of active wifi display scan requests.
238 private int mWifiDisplayScanRequestCount;
239
Jeff Browna506a6e2013-06-04 00:02:38 -0700240 // The virtual display adapter, or null if not registered.
241 private VirtualDisplayAdapter mVirtualDisplayAdapter;
242
Michael Wrighteef0e132017-11-21 17:57:52 +0000243 // The User ID of the current user
244 private @UserIdInt int mCurrentUserId;
245
Michael Wrighteedcbf12017-08-16 23:14:54 +0100246 // The stable device screen height and width. These are not tied to a specific display, even
247 // the default display, because they need to be stable over the course of the device's entire
248 // life, even if the default display changes (e.g. a new monitor is plugged into a PC-like
249 // device).
250 private Point mStableDisplaySize = new Point();
251
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200252 // The top inset of the default display.
253 // This gets persisted so that the boot animation knows how to transition from the display's
254 // full size to the size configured by the user. Right now we only persist and animate the top
255 // inset, but theoretically we could do it for all of them.
256 private int mDefaultDisplayTopInset;
257
Jeff Brownd728bf52012-09-08 18:05:28 -0700258 // Viewports of the default display and the display that should receive touch
259 // input from an external source. Used by the input system.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100260 @GuardedBy("mSyncRoot")
261 private final ArrayList<DisplayViewport> mViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700262
Jeff Brown89d55462012-09-19 11:33:42 -0700263 // Persistent data store for all internal settings maintained by the display manager service.
264 private final PersistentDataStore mPersistentDataStore = new PersistentDataStore();
265
Jeff Brownbd6e1502012-08-28 03:27:37 -0700266 // Temporary callback list, used when sending display events to applications.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700267 // May be used outside of the lock but only on the handler thread.
268 private final ArrayList<CallbackRecord> mTempCallbacks = new ArrayList<CallbackRecord>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700269
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700270 // Temporary display info, used for comparing display configurations.
271 private final DisplayInfo mTempDisplayInfo = new DisplayInfo();
272
Jeff Brownd728bf52012-09-08 18:05:28 -0700273 // Temporary viewports, used when sending new viewport information to the
274 // input system. May be used outside of the lock but only on the handler thread.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100275 private final ArrayList<DisplayViewport> mTempViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700276
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700277 // The default color mode for default displays. Overrides the usual
278 // Display.Display.COLOR_MODE_DEFAULT for displays with the
279 // DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY flag set.
280 private final int mDefaultDisplayDefaultColorMode;
281
Jeff Browne75926d2014-09-18 15:24:49 -0700282 // Temporary list of deferred work to perform when setting the display state.
283 // Only used by requestDisplayState. The field is self-synchronized and only
284 // intended for use inside of the requestGlobalDisplayStateInternal function.
285 private final ArrayList<Runnable> mTempDisplayStateWorkQueue = new ArrayList<Runnable>();
286
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800287 // Lists of UIDs that are present on the displays. Maps displayId -> array of UIDs.
288 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
289
Santos Cordonee8931e2017-04-05 10:31:15 -0700290 private final Injector mInjector;
291
Dan Gittik122df862018-03-28 16:59:22 +0100292 // The minimum brightness curve, which guarantess that any brightness curve that dips below it
293 // is rejected by the system.
294 private final Curve mMinimumBrightnessCurve;
295 private final Spline mMinimumBrightnessSpline;
296
Jeff Brownb880d882014-02-10 19:47:07 -0800297 public DisplayManagerService(Context context) {
Santos Cordonee8931e2017-04-05 10:31:15 -0700298 this(context, new Injector());
299 }
300
301 @VisibleForTesting
302 DisplayManagerService(Context context, Injector injector) {
Jeff Brownb880d882014-02-10 19:47:07 -0800303 super(context);
Santos Cordonee8931e2017-04-05 10:31:15 -0700304 mInjector = injector;
Jeff Brownb880d882014-02-10 19:47:07 -0800305 mContext = context;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800306 mHandler = new DisplayManagerHandler(DisplayThread.get().getLooper());
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700307 mUiHandler = UiThread.getHandler();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700308 mDisplayAdapterListener = new DisplayAdapterListener();
Jeff Brown27f1d672012-10-17 18:32:34 -0700309 mSingleDisplayDemoMode = SystemProperties.getBoolean("persist.demo.singledisplay", false);
Dan Gittik122df862018-03-28 16:59:22 +0100310 Resources resources = mContext.getResources();
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700311 mDefaultDisplayDefaultColorMode = mContext.getResources().getInteger(
Michael Wrighteef0e132017-11-21 17:57:52 +0000312 com.android.internal.R.integer.config_defaultDisplayDefaultColorMode);
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200313 mDefaultDisplayTopInset = SystemProperties.getInt(PROP_DEFAULT_DISPLAY_TOP_INSET, -1);
Dan Gittik122df862018-03-28 16:59:22 +0100314 float[] lux = getFloatArray(resources.obtainTypedArray(
315 com.android.internal.R.array.config_minimumBrightnessCurveLux));
316 float[] nits = getFloatArray(resources.obtainTypedArray(
317 com.android.internal.R.array.config_minimumBrightnessCurveNits));
318 mMinimumBrightnessCurve = new Curve(lux, nits);
319 mMinimumBrightnessSpline = Spline.createSpline(lux, nits);
Jeff Brown5d6443b2015-04-10 20:15:01 -0700320
321 PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
322 mGlobalDisplayBrightness = pm.getDefaultScreenBrightnessSetting();
Michael Wrighteef0e132017-11-21 17:57:52 +0000323 mCurrentUserId = UserHandle.USER_SYSTEM;
Joel Fernandes2d314e12017-04-04 16:32:15 -0700324 }
325
326 public void setupSchedulerPolicies() {
Jorim Jaggied7993b2017-03-28 18:50:01 +0100327 // android.display and android.anim is critical to user experience and we should make sure
Michael Wrighteef0e132017-11-21 17:57:52 +0000328 // it is not in the default foregroup groups, add it to top-app to make sure it uses all
329 // the cores and scheduling settings for top-app when it runs.
Jorim Jaggied7993b2017-03-28 18:50:01 +0100330 Process.setThreadGroupAndCpuset(DisplayThread.get().getThreadId(),
331 Process.THREAD_GROUP_TOP_APP);
332 Process.setThreadGroupAndCpuset(AnimationThread.get().getThreadId(),
333 Process.THREAD_GROUP_TOP_APP);
Jorim Jaggi21c39a72017-10-20 15:47:51 +0200334 Process.setThreadGroupAndCpuset(SurfaceAnimationThread.get().getThreadId(),
335 Process.THREAD_GROUP_TOP_APP);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700336 }
337
Jeff Brown4ccb8232014-01-16 22:16:42 -0800338 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -0800339 public void onStart() {
Michael Wright1c9977b2016-07-12 13:30:10 -0700340 // We need to pre-load the persistent data store so it's ready before the default display
341 // adapter is up so that we have it's configuration. We could load it lazily, but since
342 // 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 -0700343 // we've waited for the display to register itself with us.
Michael Wrighteedcbf12017-08-16 23:14:54 +0100344 synchronized(mSyncRoot) {
345 mPersistentDataStore.loadIfNeeded();
346 loadStableDisplayValuesLocked();
347 }
Santos Cordonc22c5632017-06-21 16:03:49 -0700348 mHandler.sendEmptyMessage(MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800349
350 publishBinderService(Context.DISPLAY_SERVICE, new BinderService(),
351 true /*allowIsolated*/);
352 publishLocalService(DisplayManagerInternal.class, new LocalService());
Justin Klaassen22eb1992016-07-11 20:52:23 -0700353 publishLocalService(DisplayTransformManager.class, new DisplayTransformManager());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800354 }
355
356 @Override
357 public void onBootPhase(int phase) {
358 if (phase == PHASE_WAIT_FOR_DEFAULT_DISPLAY) {
359 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700360 long timeout = SystemClock.uptimeMillis()
361 + mInjector.getDefaultDisplayDelayTimeout();
362 while (mLogicalDisplays.get(Display.DEFAULT_DISPLAY) == null ||
363 mVirtualDisplayAdapter == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800364 long delay = timeout - SystemClock.uptimeMillis();
365 if (delay <= 0) {
366 throw new RuntimeException("Timeout waiting for default display "
Santos Cordonc22c5632017-06-21 16:03:49 -0700367 + "to be initialized. DefaultDisplay="
368 + mLogicalDisplays.get(Display.DEFAULT_DISPLAY)
369 + ", mVirtualDisplayAdapter=" + mVirtualDisplayAdapter);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800370 }
371 if (DEBUG) {
372 Slog.d(TAG, "waitForDefaultDisplay: waiting, timeout=" + delay);
373 }
374 try {
375 mSyncRoot.wait(delay);
376 } catch (InterruptedException ex) {
377 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700378 }
379 }
380 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700381 }
382
Michael Wrighteef0e132017-11-21 17:57:52 +0000383 @Override
384 public void onSwitchUser(@UserIdInt int newUserId) {
385 final int userSerial = getUserManager().getUserSerialNumber(newUserId);
386 synchronized (mSyncRoot) {
387 if (mCurrentUserId != newUserId) {
388 mCurrentUserId = newUserId;
389 BrightnessConfiguration config =
390 mPersistentDataStore.getBrightnessConfiguration(userSerial);
391 mDisplayPowerController.setBrightnessConfiguration(config);
392 }
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +0000393 mDisplayPowerController.onSwitchUser(newUserId);
Michael Wrighteef0e132017-11-21 17:57:52 +0000394 }
395 }
396
Jeff Brown4ccb8232014-01-16 22:16:42 -0800397 // TODO: Use dependencies or a boot phase
398 public void windowManagerAndInputReady() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700399 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800400 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
401 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner65d11b32012-10-01 13:59:52 -0700402 scheduleTraversalLocked(false);
Jeff Brownd728bf52012-09-08 18:05:28 -0700403 }
404 }
405
406 /**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700407 * Called when the system is ready to go.
408 */
409 public void systemReady(boolean safeMode, boolean onlyCore) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700410 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700411 mSafeMode = safeMode;
412 mOnlyCore = onlyCore;
413 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700414
Jeff Brownbd6e1502012-08-28 03:27:37 -0700415 mHandler.sendEmptyMessage(MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS);
Kenny Guy22bd0442017-10-26 00:15:54 +0100416 mHandler.sendEmptyMessage(MSG_REGISTER_BRIGHTNESS_TRACKER);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700417 }
418
Santos Cordonee8931e2017-04-05 10:31:15 -0700419 @VisibleForTesting
420 Handler getDisplayHandler() {
421 return mHandler;
422 }
423
Michael Wrighteedcbf12017-08-16 23:14:54 +0100424 private void loadStableDisplayValuesLocked() {
425 final Point size = mPersistentDataStore.getStableDisplaySize();
426 if (size.x > 0 && size.y > 0) {
427 // Just set these values directly so we don't write the display persistent data again
428 // unnecessarily
429 mStableDisplaySize.set(size.x, size.y);
430 } else {
431 final Resources res = mContext.getResources();
432 final int width = res.getInteger(
433 com.android.internal.R.integer.config_stableDeviceDisplayWidth);
434 final int height = res.getInteger(
435 com.android.internal.R.integer.config_stableDeviceDisplayHeight);
436 if (width > 0 && height > 0) {
437 setStableDisplaySizeLocked(width, height);
438 }
439 }
440 }
441
442 private Point getStableDisplaySizeInternal() {
443 Point r = new Point();
444 synchronized (mSyncRoot) {
445 if (mStableDisplaySize.x > 0 && mStableDisplaySize.y > 0) {
446 r.set(mStableDisplaySize.x, mStableDisplaySize.y);
447 }
448 }
449 return r;
450 }
451
Jeff Brown4ccb8232014-01-16 22:16:42 -0800452 private void registerDisplayTransactionListenerInternal(
453 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800454 // List is self-synchronized copy-on-write.
455 mDisplayTransactionListeners.add(listener);
456 }
457
Jeff Brown4ccb8232014-01-16 22:16:42 -0800458 private void unregisterDisplayTransactionListenerInternal(
459 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800460 // List is self-synchronized copy-on-write.
461 mDisplayTransactionListeners.remove(listener);
462 }
463
Jeff Brown4ccb8232014-01-16 22:16:42 -0800464 private void setDisplayInfoOverrideFromWindowManagerInternal(
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700465 int displayId, DisplayInfo info) {
466 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700467 LogicalDisplay display = mLogicalDisplays.get(displayId);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700468 if (display != null) {
Jeff Brownef981a42013-08-07 14:13:37 -0700469 if (display.setDisplayInfoOverrideFromWindowManagerLocked(info)) {
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200470 handleLogicalDisplayChanged(displayId, display);
Craig Mautner65d11b32012-10-01 13:59:52 -0700471 scheduleTraversalLocked(false);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700472 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700473 }
474 }
475 }
476
Andrii Kuliancd097992017-03-23 18:31:59 -0700477 /**
478 * @see DisplayManagerInternal#getNonOverrideDisplayInfo(int, DisplayInfo)
479 */
480 private void getNonOverrideDisplayInfoInternal(int displayId, DisplayInfo outInfo) {
481 synchronized (mSyncRoot) {
482 final LogicalDisplay display = mLogicalDisplays.get(displayId);
483 if (display != null) {
484 display.getNonOverrideDisplayInfoLocked(outInfo);
485 }
486 }
487 }
488
Santos Cordonee8931e2017-04-05 10:31:15 -0700489 @VisibleForTesting
Robert Carrae606b42018-02-15 15:36:23 -0800490 void performTraversalInternal(SurfaceControl.Transaction t) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700491 synchronized (mSyncRoot) {
492 if (!mPendingTraversal) {
493 return;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700494 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700495 mPendingTraversal = false;
496
Robert Carrae606b42018-02-15 15:36:23 -0800497 performTraversalLocked(t);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700498 }
Jeff Brown7f3994e2012-12-04 14:04:28 -0800499
500 // List is self-synchronized copy-on-write.
501 for (DisplayTransactionListener listener : mDisplayTransactionListeners) {
Vishnu Naire86bd982018-11-28 13:23:17 -0800502 listener.onDisplayTransaction(t);
Jeff Brown7f3994e2012-12-04 14:04:28 -0800503 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700504 }
505
Jeff Brown5d6443b2015-04-10 20:15:01 -0700506 private void requestGlobalDisplayStateInternal(int state, int brightness) {
507 if (state == Display.STATE_UNKNOWN) {
508 state = Display.STATE_ON;
509 }
510 if (state == Display.STATE_OFF) {
511 brightness = PowerManager.BRIGHTNESS_OFF;
512 } else if (brightness < 0) {
513 brightness = PowerManager.BRIGHTNESS_DEFAULT;
514 } else if (brightness > PowerManager.BRIGHTNESS_ON) {
515 brightness = PowerManager.BRIGHTNESS_ON;
516 }
517
Jeff Browne75926d2014-09-18 15:24:49 -0700518 synchronized (mTempDisplayStateWorkQueue) {
519 try {
520 // Update the display state within the lock.
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700521 // Note that we do not need to schedule traversals here although it
522 // may happen as a side-effect of displays changing state.
Jeff Browne75926d2014-09-18 15:24:49 -0700523 synchronized (mSyncRoot) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700524 if (mGlobalDisplayState == state
525 && mGlobalDisplayBrightness == brightness) {
526 return; // no change
Jeff Browne75926d2014-09-18 15:24:49 -0700527 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700528
529 Trace.traceBegin(Trace.TRACE_TAG_POWER, "requestGlobalDisplayState("
530 + Display.stateToString(state)
531 + ", brightness=" + brightness + ")");
532 mGlobalDisplayState = state;
533 mGlobalDisplayBrightness = brightness;
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700534 applyGlobalDisplayStateLocked(mTempDisplayStateWorkQueue);
Jeff Browne75926d2014-09-18 15:24:49 -0700535 }
536
537 // Setting the display power state can take hundreds of milliseconds
538 // to complete so we defer the most expensive part of the work until
539 // after we have exited the critical section to avoid blocking other
540 // threads for a long time.
541 for (int i = 0; i < mTempDisplayStateWorkQueue.size(); i++) {
542 mTempDisplayStateWorkQueue.get(i).run();
543 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700544 Trace.traceEnd(Trace.TRACE_TAG_POWER);
Jeff Browne75926d2014-09-18 15:24:49 -0700545 } finally {
546 mTempDisplayStateWorkQueue.clear();
Jeff Brown9e316a12012-10-08 19:17:06 -0700547 }
548 }
549 }
550
Jeff Brown4ccb8232014-01-16 22:16:42 -0800551 private DisplayInfo getDisplayInfoInternal(int displayId, int callingUid) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700552 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800553 LogicalDisplay display = mLogicalDisplays.get(displayId);
554 if (display != null) {
555 DisplayInfo info = display.getDisplayInfoLocked();
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800556 if (info.hasAccess(callingUid)
557 || isUidPresentOnDisplayInternal(callingUid, displayId)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800558 return info;
559 }
560 }
561 return null;
562 }
563 }
564
565 private int[] getDisplayIdsInternal(int callingUid) {
566 synchronized (mSyncRoot) {
567 final int count = mLogicalDisplays.size();
568 int[] displayIds = new int[count];
569 int n = 0;
570 for (int i = 0; i < count; i++) {
571 LogicalDisplay display = mLogicalDisplays.valueAt(i);
572 DisplayInfo info = display.getDisplayInfoLocked();
573 if (info.hasAccess(callingUid)) {
574 displayIds[n++] = mLogicalDisplays.keyAt(i);
575 }
576 }
577 if (n != count) {
578 displayIds = Arrays.copyOfRange(displayIds, 0, n);
579 }
580 return displayIds;
581 }
582 }
583
584 private void registerCallbackInternal(IDisplayManagerCallback callback, int callingPid) {
585 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700586 if (mCallbacks.get(callingPid) != null) {
587 throw new SecurityException("The calling process has already "
588 + "registered an IDisplayManagerCallback.");
Jeff Brown64a55af2012-08-26 02:47:39 -0700589 }
590
Jeff Brownbd6e1502012-08-28 03:27:37 -0700591 CallbackRecord record = new CallbackRecord(callingPid, callback);
592 try {
593 IBinder binder = callback.asBinder();
594 binder.linkToDeath(record, 0);
595 } catch (RemoteException ex) {
596 // give up
597 throw new RuntimeException(ex);
598 }
599
600 mCallbacks.put(callingPid, record);
601 }
602 }
603
Jeff Brownce468a32013-11-21 16:42:03 -0800604 private void onCallbackDied(CallbackRecord record) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700605 synchronized (mSyncRoot) {
Jeff Brownce468a32013-11-21 16:42:03 -0800606 mCallbacks.remove(record.mPid);
607 stopWifiDisplayScanLocked(record);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700608 }
609 }
610
Jeff Brown4ccb8232014-01-16 22:16:42 -0800611 private void startWifiDisplayScanInternal(int callingPid) {
612 synchronized (mSyncRoot) {
613 CallbackRecord record = mCallbacks.get(callingPid);
614 if (record == null) {
615 throw new IllegalStateException("The calling process has not "
616 + "registered an IDisplayManagerCallback.");
Jeff Browne08ae382012-09-07 20:36:36 -0700617 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800618 startWifiDisplayScanLocked(record);
Jeff Browne08ae382012-09-07 20:36:36 -0700619 }
620 }
621
Jeff Brownce468a32013-11-21 16:42:03 -0800622 private void startWifiDisplayScanLocked(CallbackRecord record) {
623 if (!record.mWifiDisplayScanRequested) {
624 record.mWifiDisplayScanRequested = true;
625 if (mWifiDisplayScanRequestCount++ == 0) {
626 if (mWifiDisplayAdapter != null) {
627 mWifiDisplayAdapter.requestStartScanLocked();
628 }
629 }
630 }
631 }
632
Jeff Brown4ccb8232014-01-16 22:16:42 -0800633 private void stopWifiDisplayScanInternal(int callingPid) {
634 synchronized (mSyncRoot) {
635 CallbackRecord record = mCallbacks.get(callingPid);
636 if (record == null) {
637 throw new IllegalStateException("The calling process has not "
638 + "registered an IDisplayManagerCallback.");
Jeff Brownce468a32013-11-21 16:42:03 -0800639 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800640 stopWifiDisplayScanLocked(record);
Jeff Brownce468a32013-11-21 16:42:03 -0800641 }
642 }
643
644 private void stopWifiDisplayScanLocked(CallbackRecord record) {
645 if (record.mWifiDisplayScanRequested) {
646 record.mWifiDisplayScanRequested = false;
647 if (--mWifiDisplayScanRequestCount == 0) {
648 if (mWifiDisplayAdapter != null) {
649 mWifiDisplayAdapter.requestStopScanLocked();
650 }
651 } else if (mWifiDisplayScanRequestCount < 0) {
Dianne Hackborn8d051722014-10-01 14:59:58 -0700652 Slog.wtf(TAG, "mWifiDisplayScanRequestCount became negative: "
Jeff Brownce468a32013-11-21 16:42:03 -0800653 + mWifiDisplayScanRequestCount);
654 mWifiDisplayScanRequestCount = 0;
655 }
656 }
657 }
658
Jeff Brown4ccb8232014-01-16 22:16:42 -0800659 private void connectWifiDisplayInternal(String address) {
660 synchronized (mSyncRoot) {
661 if (mWifiDisplayAdapter != null) {
662 mWifiDisplayAdapter.requestConnectLocked(address);
Jeff Browne08ae382012-09-07 20:36:36 -0700663 }
Jeff Browne08ae382012-09-07 20:36:36 -0700664 }
665 }
666
Jeff Brown4ccb8232014-01-16 22:16:42 -0800667 private void pauseWifiDisplayInternal() {
668 synchronized (mSyncRoot) {
669 if (mWifiDisplayAdapter != null) {
670 mWifiDisplayAdapter.requestPauseLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700671 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700672 }
673 }
674
Jeff Brown4ccb8232014-01-16 22:16:42 -0800675 private void resumeWifiDisplayInternal() {
676 synchronized (mSyncRoot) {
677 if (mWifiDisplayAdapter != null) {
678 mWifiDisplayAdapter.requestResumeLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700679 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700680 }
681 }
682
Jeff Brown4ccb8232014-01-16 22:16:42 -0800683 private void disconnectWifiDisplayInternal() {
684 synchronized (mSyncRoot) {
685 if (mWifiDisplayAdapter != null) {
686 mWifiDisplayAdapter.requestDisconnectLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700687 }
Jeff Browne08ae382012-09-07 20:36:36 -0700688 }
689 }
690
Jeff Brown4ccb8232014-01-16 22:16:42 -0800691 private void renameWifiDisplayInternal(String address, String alias) {
692 synchronized (mSyncRoot) {
693 if (mWifiDisplayAdapter != null) {
694 mWifiDisplayAdapter.requestRenameLocked(address, alias);
Jeff Brown89d55462012-09-19 11:33:42 -0700695 }
Jeff Brown89d55462012-09-19 11:33:42 -0700696 }
697 }
698
Jeff Brown4ccb8232014-01-16 22:16:42 -0800699 private void forgetWifiDisplayInternal(String address) {
700 synchronized (mSyncRoot) {
701 if (mWifiDisplayAdapter != null) {
702 mWifiDisplayAdapter.requestForgetLocked(address);
Jeff Brown89d55462012-09-19 11:33:42 -0700703 }
Jeff Brown89d55462012-09-19 11:33:42 -0700704 }
705 }
706
Jeff Brown4ccb8232014-01-16 22:16:42 -0800707 private WifiDisplayStatus getWifiDisplayStatusInternal() {
708 synchronized (mSyncRoot) {
709 if (mWifiDisplayAdapter != null) {
710 return mWifiDisplayAdapter.getWifiDisplayStatusLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700711 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800712 return new WifiDisplayStatus();
Jeff Browne08ae382012-09-07 20:36:36 -0700713 }
714 }
715
Michael Wright1c9977b2016-07-12 13:30:10 -0700716 private void requestColorModeInternal(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +0100717 synchronized (mSyncRoot) {
718 LogicalDisplay display = mLogicalDisplays.get(displayId);
719 if (display != null &&
Michael Wright1c9977b2016-07-12 13:30:10 -0700720 display.getRequestedColorModeLocked() != colorMode) {
721 display.setRequestedColorModeLocked(colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +0100722 scheduleTraversalLocked(false);
723 }
724 }
725 }
726
Bryan Mawhinney462e29d2018-03-22 15:52:41 +0000727 private void setSaturationLevelInternal(float level) {
728 if (level < 0 || level > 1) {
729 throw new IllegalArgumentException("Saturation level must be between 0 and 1");
730 }
731 float[] matrix = (level == 1.0f ? null : computeSaturationMatrix(level));
732 DisplayTransformManager dtm = LocalServices.getService(DisplayTransformManager.class);
733 dtm.setColorMatrix(DisplayTransformManager.LEVEL_COLOR_MATRIX_SATURATION, matrix);
734 }
735
736 private static float[] computeSaturationMatrix(float saturation) {
737 float desaturation = 1.0f - saturation;
738 float[] luminance = {0.231f * desaturation, 0.715f * desaturation, 0.072f * desaturation};
739 float[] matrix = {
740 luminance[0] + saturation, luminance[0], luminance[0], 0,
741 luminance[1], luminance[1] + saturation, luminance[1], 0,
742 luminance[2], luminance[2], luminance[2] + saturation, 0,
743 0, 0, 0, 1
744 };
745 return matrix;
746 }
747
Michael Wright75ee9fc2014-09-01 19:55:22 -0700748 private int createVirtualDisplayInternal(IVirtualDisplayCallback callback,
Santos Cordonee8931e2017-04-05 10:31:15 -0700749 IMediaProjection projection, int callingUid, String packageName, String name, int width,
750 int height, int densityDpi, Surface surface, int flags, String uniqueId) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800751 synchronized (mSyncRoot) {
752 if (mVirtualDisplayAdapter == null) {
753 Slog.w(TAG, "Rejecting request to create private virtual display "
754 + "because the virtual display adapter is not available.");
755 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700756 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800757
758 DisplayDevice device = mVirtualDisplayAdapter.createVirtualDisplayLocked(
Santos Cordonee8931e2017-04-05 10:31:15 -0700759 callback, projection, callingUid, packageName, name, width, height, densityDpi,
760 surface, flags, uniqueId);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800761 if (device == null) {
762 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700763 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700764
Jeff Brown4ccb8232014-01-16 22:16:42 -0800765 handleDisplayDeviceAddedLocked(device);
766 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
767 if (display != null) {
768 return display.getDisplayIdLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700769 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800770
771 // Something weird happened and the logical display was not created.
772 Slog.w(TAG, "Rejecting request to create virtual display "
773 + "because the logical display was not created.");
Michael Wright75ee9fc2014-09-01 19:55:22 -0700774 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800775 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700776 }
777 return -1;
778 }
779
Michael Wright01e840f2014-06-26 16:03:25 -0700780 private void resizeVirtualDisplayInternal(IBinder appToken,
781 int width, int height, int densityDpi) {
782 synchronized (mSyncRoot) {
783 if (mVirtualDisplayAdapter == null) {
784 return;
785 }
786
787 mVirtualDisplayAdapter.resizeVirtualDisplayLocked(appToken, width, height, densityDpi);
788 }
789 }
790
Jeff Brown92207df2014-04-16 13:16:07 -0700791 private void setVirtualDisplaySurfaceInternal(IBinder appToken, Surface surface) {
792 synchronized (mSyncRoot) {
793 if (mVirtualDisplayAdapter == null) {
794 return;
795 }
796
797 mVirtualDisplayAdapter.setVirtualDisplaySurfaceLocked(appToken, surface);
798 }
799 }
800
Jeff Brown4ccb8232014-01-16 22:16:42 -0800801 private void releaseVirtualDisplayInternal(IBinder appToken) {
802 synchronized (mSyncRoot) {
803 if (mVirtualDisplayAdapter == null) {
804 return;
Jeff Browna506a6e2013-06-04 00:02:38 -0700805 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700806
Jeff Brown4ccb8232014-01-16 22:16:42 -0800807 DisplayDevice device =
808 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(appToken);
809 if (device != null) {
810 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700811 }
812 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700813 }
814
Santos Cordonc22c5632017-06-21 16:03:49 -0700815 private void registerDefaultDisplayAdapters() {
816 // Register default display adapters.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700817 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700818 // main display adapter
Mike Lockwoode63f6f72013-11-15 11:01:47 -0800819 registerDisplayAdapterLocked(new LocalDisplayAdapter(
820 mSyncRoot, mContext, mHandler, mDisplayAdapterListener));
Santos Cordonc22c5632017-06-21 16:03:49 -0700821
822 // Standalone VR devices rely on a virtual display as their primary display for
823 // 2D UI. We register virtual display adapter along side the main display adapter
824 // here so that it is ready by the time the system sends the home Intent for
825 // early apps like SetupWizard/Launcher. In particular, SUW is displayed using
826 // the virtual display inside VR before any VR-specific apps even run.
827 mVirtualDisplayAdapter = mInjector.getVirtualDisplayAdapter(mSyncRoot, mContext,
828 mHandler, mDisplayAdapterListener);
829 if (mVirtualDisplayAdapter != null) {
830 registerDisplayAdapterLocked(mVirtualDisplayAdapter);
831 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700832 }
833 }
834
835 private void registerAdditionalDisplayAdapters() {
836 synchronized (mSyncRoot) {
837 if (shouldRegisterNonEssentialDisplayAdaptersLocked()) {
Jeff Brown89d55462012-09-19 11:33:42 -0700838 registerOverlayDisplayAdapterLocked();
839 registerWifiDisplayAdapterLocked();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700840 }
841 }
842 }
843
Jeff Brown89d55462012-09-19 11:33:42 -0700844 private void registerOverlayDisplayAdapterLocked() {
845 registerDisplayAdapterLocked(new OverlayDisplayAdapter(
846 mSyncRoot, mContext, mHandler, mDisplayAdapterListener, mUiHandler));
847 }
848
849 private void registerWifiDisplayAdapterLocked() {
850 if (mContext.getResources().getBoolean(
Jeff Brownbbd28a22012-09-20 16:47:15 -0700851 com.android.internal.R.bool.config_enableWifiDisplay)
852 || SystemProperties.getInt(FORCE_WIFI_DISPLAY_ENABLE, -1) == 1) {
Jeff Brown89d55462012-09-19 11:33:42 -0700853 mWifiDisplayAdapter = new WifiDisplayAdapter(
854 mSyncRoot, mContext, mHandler, mDisplayAdapterListener,
855 mPersistentDataStore);
856 registerDisplayAdapterLocked(mWifiDisplayAdapter);
857 }
858 }
859
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700860 private boolean shouldRegisterNonEssentialDisplayAdaptersLocked() {
861 // In safe mode, we disable non-essential display adapters to give the user
862 // an opportunity to fix broken settings or other problems that might affect
863 // system stability.
864 // In only-core mode, we disable non-essential display adapters to minimize
865 // the number of dependencies that are started while in this mode and to
866 // prevent problems that might occur due to the device being encrypted.
867 return !mSafeMode && !mOnlyCore;
868 }
869
870 private void registerDisplayAdapterLocked(DisplayAdapter adapter) {
871 mDisplayAdapters.add(adapter);
872 adapter.registerLocked();
873 }
874
Jeff Brownbd6e1502012-08-28 03:27:37 -0700875 private void handleDisplayDeviceAdded(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700876 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700877 handleDisplayDeviceAddedLocked(device);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700878 }
879 }
880
Jeff Browna506a6e2013-06-04 00:02:38 -0700881 private void handleDisplayDeviceAddedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700882 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700883 if (mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700884 Slog.w(TAG, "Attempted to add already added display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700885 return;
886 }
887
Jeff Brown10acf6d2015-04-14 14:20:47 -0700888 Slog.i(TAG, "Display device added: " + info);
889 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700890
891 mDisplayDevices.add(device);
Michael Wright1c9977b2016-07-12 13:30:10 -0700892 LogicalDisplay display = addLogicalDisplayLocked(device);
Jeff Brown0033a862014-10-08 12:06:39 -0700893 Runnable work = updateDisplayStateLocked(device);
894 if (work != null) {
895 work.run();
896 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700897 scheduleTraversalLocked(false);
898 }
899
Jeff Brownbd6e1502012-08-28 03:27:37 -0700900 private void handleDisplayDeviceChanged(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700901 synchronized (mSyncRoot) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700902 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700903 if (!mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700904 Slog.w(TAG, "Attempted to change non-existent display device: " + info);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700905 return;
906 }
907
Jeff Brown10acf6d2015-04-14 14:20:47 -0700908 int diff = device.mDebugLastLoggedDeviceInfo.diff(info);
909 if (diff == DisplayDeviceInfo.DIFF_STATE) {
910 Slog.i(TAG, "Display device changed state: \"" + info.name
911 + "\", " + Display.stateToString(info.state));
912 } else if (diff != 0) {
913 Slog.i(TAG, "Display device changed: " + info);
914 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700915 if ((diff & DisplayDeviceInfo.DIFF_COLOR_MODE) != 0) {
916 try {
917 mPersistentDataStore.setColorMode(device, info.colorMode);
918 } finally {
919 mPersistentDataStore.saveIfNeeded();
920 }
921 }
Jeff Brown10acf6d2015-04-14 14:20:47 -0700922 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browne87bf032012-09-20 18:30:13 -0700923
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700924 device.applyPendingDisplayDeviceInfoChangesLocked();
925 if (updateLogicalDisplaysLocked()) {
Craig Mautner65d11b32012-10-01 13:59:52 -0700926 scheduleTraversalLocked(false);
Jeff Brown64a55af2012-08-26 02:47:39 -0700927 }
928 }
929 }
930
Jeff Brownbd6e1502012-08-28 03:27:37 -0700931 private void handleDisplayDeviceRemoved(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700932 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700933 handleDisplayDeviceRemovedLocked(device);
934 }
935 }
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700936
Jeff Browna506a6e2013-06-04 00:02:38 -0700937 private void handleDisplayDeviceRemovedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700938 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700939 if (!mDisplayDevices.remove(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700940 Slog.w(TAG, "Attempted to remove non-existent display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700941 return;
942 }
943
Jeff Brown10acf6d2015-04-14 14:20:47 -0700944 Slog.i(TAG, "Display device removed: " + info);
945 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700946
Jeff Browna506a6e2013-06-04 00:02:38 -0700947 updateLogicalDisplaysLocked();
948 scheduleTraversalLocked(false);
949 }
950
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200951 private void handleLogicalDisplayChanged(int displayId, @NonNull LogicalDisplay display) {
952 if (displayId == Display.DEFAULT_DISPLAY) {
953 recordTopInsetLocked(display);
954 }
955 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
956 }
957
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700958 private void applyGlobalDisplayStateLocked(List<Runnable> workQueue) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700959 final int count = mDisplayDevices.size();
960 for (int i = 0; i < count; i++) {
961 DisplayDevice device = mDisplayDevices.get(i);
Jeff Browne75926d2014-09-18 15:24:49 -0700962 Runnable runnable = updateDisplayStateLocked(device);
963 if (runnable != null) {
964 workQueue.add(runnable);
965 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700966 }
967 }
968
Jeff Browne75926d2014-09-18 15:24:49 -0700969 private Runnable updateDisplayStateLocked(DisplayDevice device) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700970 // Blank or unblank the display immediately to match the state requested
Jeff Brown037c33e2014-04-09 00:31:55 -0700971 // by the display power controller (if known).
Jeff Browna506a6e2013-06-04 00:02:38 -0700972 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
973 if ((info.flags & DisplayDeviceInfo.FLAG_NEVER_BLANK) == 0) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700974 return device.requestDisplayStateLocked(mGlobalDisplayState, mGlobalDisplayBrightness);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700975 }
Jeff Browne75926d2014-09-18 15:24:49 -0700976 return null;
Craig Mautner4f67ba62012-08-02 11:23:00 -0700977 }
978
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700979 // Adds a new logical display based on the given display device.
980 // Sends notifications if needed.
Michael Wright1c9977b2016-07-12 13:30:10 -0700981 private LogicalDisplay addLogicalDisplayLocked(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700982 DisplayDeviceInfo deviceInfo = device.getDisplayDeviceInfoLocked();
983 boolean isDefault = (deviceInfo.flags
984 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0;
985 if (isDefault && mLogicalDisplays.get(Display.DEFAULT_DISPLAY) != null) {
986 Slog.w(TAG, "Ignoring attempt to add a second default display: " + deviceInfo);
987 isDefault = false;
988 }
989
Jeff Brown27f1d672012-10-17 18:32:34 -0700990 if (!isDefault && mSingleDisplayDemoMode) {
991 Slog.i(TAG, "Not creating a logical display for a secondary display "
992 + " because single display demo mode is enabled: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -0700993 return null;
Jeff Brown27f1d672012-10-17 18:32:34 -0700994 }
995
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700996 final int displayId = assignDisplayIdLocked(isDefault);
997 final int layerStack = assignLayerStackLocked(displayId);
998
Jeff Brownd728bf52012-09-08 18:05:28 -0700999 LogicalDisplay display = new LogicalDisplay(displayId, layerStack, device);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001000 display.updateLocked(mDisplayDevices);
1001 if (!display.isValidLocked()) {
1002 // This should never happen currently.
1003 Slog.w(TAG, "Ignoring display device because the logical display "
1004 + "created from it was not considered valid: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -07001005 return null;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001006 }
1007
Michael Wrighteedcbf12017-08-16 23:14:54 +01001008 configureColorModeLocked(display, device);
1009 if (isDefault) {
1010 recordStableDisplayStatsIfNeededLocked(display);
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001011 recordTopInsetLocked(display);
Michael Wrighteedcbf12017-08-16 23:14:54 +01001012 }
1013
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001014 mLogicalDisplays.put(displayId, display);
1015
1016 // Wake up waitForDefaultDisplay.
1017 if (isDefault) {
1018 mSyncRoot.notifyAll();
1019 }
1020
1021 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_ADDED);
Michael Wright1c9977b2016-07-12 13:30:10 -07001022 return display;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001023 }
1024
1025 private int assignDisplayIdLocked(boolean isDefault) {
1026 return isDefault ? Display.DEFAULT_DISPLAY : mNextNonDefaultDisplayId++;
1027 }
1028
1029 private int assignLayerStackLocked(int displayId) {
1030 // Currently layer stacks and display ids are the same.
1031 // This need not be the case.
1032 return displayId;
1033 }
1034
Michael Wrighteedcbf12017-08-16 23:14:54 +01001035 private void configureColorModeLocked(LogicalDisplay display, DisplayDevice device) {
1036 if (display.getPrimaryDisplayDeviceLocked() == device) {
1037 int colorMode = mPersistentDataStore.getColorMode(device);
1038 if (colorMode == Display.COLOR_MODE_INVALID) {
1039 if ((device.getDisplayDeviceInfoLocked().flags
1040 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
1041 colorMode = mDefaultDisplayDefaultColorMode;
1042 } else {
1043 colorMode = Display.COLOR_MODE_DEFAULT;
1044 }
1045 }
1046 display.setRequestedColorModeLocked(colorMode);
1047 }
1048 }
1049
1050 // If we've never recorded stable device stats for this device before and they aren't
1051 // explicitly configured, go ahead and record the stable device stats now based on the status
1052 // of the default display at first boot.
1053 private void recordStableDisplayStatsIfNeededLocked(LogicalDisplay d) {
1054 if (mStableDisplaySize.x <= 0 && mStableDisplaySize.y <= 0) {
1055 DisplayInfo info = d.getDisplayInfoLocked();
1056 setStableDisplaySizeLocked(info.getNaturalWidth(), info.getNaturalHeight());
1057 }
1058 }
1059
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001060 private void recordTopInsetLocked(@Nullable LogicalDisplay d) {
MÃ¥rten Kongstad250cb2b2018-11-02 11:01:44 +01001061 if (d == null) {
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001062 return;
1063 }
1064 int topInset = d.getInsets().top;
1065 if (topInset == mDefaultDisplayTopInset) {
1066 return;
1067 }
1068 mDefaultDisplayTopInset = topInset;
1069 SystemProperties.set(PROP_DEFAULT_DISPLAY_TOP_INSET, Integer.toString(topInset));
1070 }
1071
Michael Wrighteedcbf12017-08-16 23:14:54 +01001072 private void setStableDisplaySizeLocked(int width, int height) {
1073 mStableDisplaySize = new Point(width, height);
1074 try {
1075 mPersistentDataStore.setStableDisplaySize(mStableDisplaySize);
1076 } finally {
1077 mPersistentDataStore.saveIfNeeded();
1078 }
1079 }
1080
Dan Gittik122df862018-03-28 16:59:22 +01001081 @VisibleForTesting
1082 Curve getMinimumBrightnessCurveInternal() {
1083 return mMinimumBrightnessCurve;
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
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001227 // Updates the lists of UIDs that are present on displays.
1228 private void setDisplayAccessUIDsInternal(SparseArray<IntArray> newDisplayAccessUIDs) {
1229 synchronized (mSyncRoot) {
1230 mDisplayAccessUIDs.clear();
1231 for (int i = newDisplayAccessUIDs.size() - 1; i >= 0; i--) {
1232 mDisplayAccessUIDs.append(newDisplayAccessUIDs.keyAt(i),
1233 newDisplayAccessUIDs.valueAt(i));
1234 }
1235 }
1236 }
1237
1238 // Checks if provided UID's content is present on the display and UID has access to it.
1239 private boolean isUidPresentOnDisplayInternal(int uid, int displayId) {
1240 synchronized (mSyncRoot) {
1241 final IntArray displayUIDs = mDisplayAccessUIDs.get(displayId);
1242 return displayUIDs != null && displayUIDs.indexOf(uid) != -1;
1243 }
1244 }
1245
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001246 @VisibleForTesting
1247 DisplayedContentSamplingAttributes getDisplayedContentSamplingAttributesInternal(
1248 int displayId) {
1249 IBinder displayToken = SurfaceControl.getBuiltInDisplay(displayId);
1250 return SurfaceControl.getDisplayedContentSamplingAttributes(displayToken);
1251 }
1252
1253 @VisibleForTesting
1254 boolean setDisplayedContentSamplingEnabledInternal(
1255 int displayId, boolean enable, int componentMask, int maxFrames) {
1256 IBinder displayToken = SurfaceControl.getBuiltInDisplay(displayId);
1257 return SurfaceControl.setDisplayedContentSamplingEnabled(
1258 displayToken, enable, componentMask, maxFrames);
1259 }
1260
1261 @VisibleForTesting
1262 DisplayedContentSample getDisplayedContentSampleInternal(int displayId,
1263 long maxFrames, long timestamp) {
1264 IBinder displayToken = SurfaceControl.getBuiltInDisplay(displayId);
1265 return SurfaceControl.getDisplayedContentSample(
1266 displayToken, maxFrames, timestamp);
1267 }
1268
Jeff Brownd728bf52012-09-08 18:05:28 -07001269 private void clearViewportsLocked() {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001270 mViewports.clear();
Craig Mautner722285e2012-09-07 13:55:58 -07001271 }
1272
Robert Carrae606b42018-02-15 15:36:23 -08001273 private void configureDisplayLocked(SurfaceControl.Transaction t, DisplayDevice device) {
Jeff Brownd14c8c92014-01-07 18:13:09 -08001274 final DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
1275 final boolean ownContent = (info.flags & DisplayDeviceInfo.FLAG_OWN_CONTENT_ONLY) != 0;
Jeff Browna506a6e2013-06-04 00:02:38 -07001276
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001277 // Find the logical display that the display device is showing.
Jeff Brownd14c8c92014-01-07 18:13:09 -08001278 // Certain displays only ever show their own content.
Craig Mautner722285e2012-09-07 13:55:58 -07001279 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
Jeff Brownd14c8c92014-01-07 18:13:09 -08001280 if (!ownContent) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001281 if (display != null && !display.hasContentLocked()) {
1282 // If the display does not have any content of its own, then
1283 // automatically mirror the default logical display contents.
1284 display = null;
1285 }
1286 if (display == null) {
1287 display = mLogicalDisplays.get(Display.DEFAULT_DISPLAY);
1288 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001289 }
1290
1291 // Apply the logical display configuration to the display device.
1292 if (display == null) {
1293 // TODO: no logical display for the device, blank it
Jeff Brownd728bf52012-09-08 18:05:28 -07001294 Slog.w(TAG, "Missing logical display to use for physical display device: "
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001295 + device.getDisplayDeviceInfoLocked());
Jeff Brownd728bf52012-09-08 18:05:28 -07001296 return;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001297 }
Robert Carrae606b42018-02-15 15:36:23 -08001298 display.configureDisplayLocked(t, device, info.state == Display.STATE_OFF);
Arthur Hung41e81e72018-10-31 18:04:56 +08001299 final int viewportType;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001300 // Update the corresponding viewport.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001301 if ((info.flags & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
Arthur Hung41e81e72018-10-31 18:04:56 +08001302 viewportType = VIEWPORT_INTERNAL;
1303 } else if (info.touch == DisplayDeviceInfo.TOUCH_EXTERNAL) {
1304 viewportType = VIEWPORT_EXTERNAL;
1305 } else if (info.touch == DisplayDeviceInfo.TOUCH_VIRTUAL
1306 && !TextUtils.isEmpty(info.uniqueId)) {
1307 viewportType = VIEWPORT_VIRTUAL;
1308 } else {
1309 Slog.wtf(TAG, "Unable to populate viewport for display device: " + info);
1310 return;
Jeff Brownd728bf52012-09-08 18:05:28 -07001311 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001312
Arthur Hung41e81e72018-10-31 18:04:56 +08001313 populateViewportLocked(viewportType, display.getDisplayIdLocked(), device, info.uniqueId);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001314 }
1315
1316 /**
1317 * Get internal or external viewport. Create it if does not currently exist.
1318 * @param viewportType - either INTERNAL or EXTERNAL
1319 * @return the viewport with the requested type
1320 */
Arthur Hung41e81e72018-10-31 18:04:56 +08001321 private DisplayViewport getViewportLocked(int viewportType, String uniqueId) {
1322 if (viewportType != VIEWPORT_INTERNAL && viewportType != VIEWPORT_EXTERNAL
1323 && viewportType != VIEWPORT_VIRTUAL) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001324 Slog.wtf(TAG, "Cannot call getViewportByTypeLocked for type "
1325 + DisplayViewport.typeToString(viewportType));
1326 return null;
1327 }
Arthur Hung41e81e72018-10-31 18:04:56 +08001328
1329 // Only allow a single INTERNAL or EXTERNAL viewport by forcing their uniqueIds
1330 // to be identical (in particular, empty).
1331 // TODO (b/116824030) allow multiple EXTERNAL viewports and remove this function.
1332 if (viewportType != VIEWPORT_VIRTUAL) {
1333 uniqueId = "";
1334 }
1335
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001336 DisplayViewport viewport;
1337 final int count = mViewports.size();
1338 for (int i = 0; i < count; i++) {
1339 viewport = mViewports.get(i);
Arthur Hung41e81e72018-10-31 18:04:56 +08001340 if (viewport.type == viewportType && uniqueId.equals(viewport.uniqueId)) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001341 return viewport;
1342 }
1343 }
1344
Arthur Hung41e81e72018-10-31 18:04:56 +08001345 // Creates the viewport if none exists.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001346 viewport = new DisplayViewport();
1347 viewport.type = viewportType;
Arthur Hung41e81e72018-10-31 18:04:56 +08001348 viewport.uniqueId = uniqueId;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001349 mViewports.add(viewport);
1350 return viewport;
1351 }
1352
Arthur Hung41e81e72018-10-31 18:04:56 +08001353 private void populateViewportLocked(int viewportType,
1354 int displayId, DisplayDevice device, String uniqueId) {
1355 final DisplayViewport viewport = getViewportLocked(viewportType, uniqueId);
Jeff Brownd728bf52012-09-08 18:05:28 -07001356 device.populateViewportLocked(viewport);
Arthur Hung41e81e72018-10-31 18:04:56 +08001357 viewport.valid = true;
1358 viewport.displayId = displayId;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001359 }
1360
1361 private LogicalDisplay findLogicalDisplayForDeviceLocked(DisplayDevice device) {
1362 final int count = mLogicalDisplays.size();
1363 for (int i = 0; i < count; i++) {
1364 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1365 if (display.getPrimaryDisplayDeviceLocked() == device) {
1366 return display;
1367 }
1368 }
1369 return null;
1370 }
1371
Jeff Brownbd6e1502012-08-28 03:27:37 -07001372 private void sendDisplayEventLocked(int displayId, int event) {
1373 Message msg = mHandler.obtainMessage(MSG_DELIVER_DISPLAY_EVENT, displayId, event);
1374 mHandler.sendMessage(msg);
1375 }
1376
Robert Carrae606b42018-02-15 15:36:23 -08001377 // Requests that performTraversals be called at a
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001378 // later time to apply changes to surfaces and displays.
Craig Mautner65d11b32012-10-01 13:59:52 -07001379 private void scheduleTraversalLocked(boolean inTraversal) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001380 if (!mPendingTraversal && mWindowManagerInternal != null) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001381 mPendingTraversal = true;
Craig Mautner65d11b32012-10-01 13:59:52 -07001382 if (!inTraversal) {
1383 mHandler.sendEmptyMessage(MSG_REQUEST_TRAVERSAL);
1384 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001385 }
1386 }
1387
1388 // Runs on Handler thread.
1389 // Delivers display event notifications to callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001390 private void deliverDisplayEvent(int displayId, int event) {
1391 if (DEBUG) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001392 Slog.d(TAG, "Delivering display event: displayId="
1393 + displayId + ", event=" + event);
Jeff Brownfa25bf52012-07-23 19:26:30 -07001394 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07001395
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001396 // Grab the lock and copy the callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001397 final int count;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001398 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001399 count = mCallbacks.size();
1400 mTempCallbacks.clear();
1401 for (int i = 0; i < count; i++) {
1402 mTempCallbacks.add(mCallbacks.valueAt(i));
Craig Mautner4f67ba62012-08-02 11:23:00 -07001403 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001404 }
Craig Mautner4f67ba62012-08-02 11:23:00 -07001405
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001406 // After releasing the lock, send the notifications out.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001407 for (int i = 0; i < count; i++) {
1408 mTempCallbacks.get(i).notifyDisplayEventAsync(displayId, event);
1409 }
1410 mTempCallbacks.clear();
Craig Mautner4f67ba62012-08-02 11:23:00 -07001411 }
1412
Michael Wrightc39d47a2014-07-08 18:07:36 -07001413 private IMediaProjectionManager getProjectionService() {
1414 if (mProjectionService == null) {
1415 IBinder b = ServiceManager.getService(Context.MEDIA_PROJECTION_SERVICE);
1416 mProjectionService = IMediaProjectionManager.Stub.asInterface(b);
1417 }
1418 return mProjectionService;
1419 }
1420
Michael Wrighteef0e132017-11-21 17:57:52 +00001421 private UserManager getUserManager() {
1422 return mContext.getSystemService(UserManager.class);
1423 }
1424
Jeff Brown4ccb8232014-01-16 22:16:42 -08001425 private void dumpInternal(PrintWriter pw) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001426 pw.println("DISPLAY MANAGER (dumpsys display)");
Jeff Brownfa25bf52012-07-23 19:26:30 -07001427
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001428 synchronized (mSyncRoot) {
Jeff Brown9e316a12012-10-08 19:17:06 -07001429 pw.println(" mOnlyCode=" + mOnlyCore);
1430 pw.println(" mSafeMode=" + mSafeMode);
1431 pw.println(" mPendingTraversal=" + mPendingTraversal);
Jeff Brown037c33e2014-04-09 00:31:55 -07001432 pw.println(" mGlobalDisplayState=" + Display.stateToString(mGlobalDisplayState));
Jeff Brown9e316a12012-10-08 19:17:06 -07001433 pw.println(" mNextNonDefaultDisplayId=" + mNextNonDefaultDisplayId);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001434 pw.println(" mViewports=" + mViewports);
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -07001435 pw.println(" mDefaultDisplayDefaultColorMode=" + mDefaultDisplayDefaultColorMode);
Jeff Brown27f1d672012-10-17 18:32:34 -07001436 pw.println(" mSingleDisplayDemoMode=" + mSingleDisplayDemoMode);
Jeff Brownce468a32013-11-21 16:42:03 -08001437 pw.println(" mWifiDisplayScanRequestCount=" + mWifiDisplayScanRequestCount);
Michael Wrighteedcbf12017-08-16 23:14:54 +01001438 pw.println(" mStableDisplaySize=" + mStableDisplaySize);
1439
Jeff Brown9e316a12012-10-08 19:17:06 -07001440
Jeff Brownbd6e1502012-08-28 03:27:37 -07001441 IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001442 ipw.increaseIndent();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001443
1444 pw.println();
1445 pw.println("Display Adapters: size=" + mDisplayAdapters.size());
Jeff Brown848c2dc2012-08-19 20:18:08 -07001446 for (DisplayAdapter adapter : mDisplayAdapters) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001447 pw.println(" " + adapter.getName());
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001448 adapter.dumpLocked(ipw);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001449 }
Craig Mautner9de49362012-08-02 14:30:30 -07001450
Jeff Brownbd6e1502012-08-28 03:27:37 -07001451 pw.println();
1452 pw.println("Display Devices: size=" + mDisplayDevices.size());
1453 for (DisplayDevice device : mDisplayDevices) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001454 pw.println(" " + device.getDisplayDeviceInfoLocked());
1455 device.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001456 }
1457
1458 final int logicalDisplayCount = mLogicalDisplays.size();
1459 pw.println();
1460 pw.println("Logical Displays: size=" + logicalDisplayCount);
1461 for (int i = 0; i < logicalDisplayCount; i++) {
1462 int displayId = mLogicalDisplays.keyAt(i);
1463 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1464 pw.println(" Display " + displayId + ":");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001465 display.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001466 }
Jeff Brownce468a32013-11-21 16:42:03 -08001467
1468 final int callbackCount = mCallbacks.size();
1469 pw.println();
1470 pw.println("Callbacks: size=" + callbackCount);
1471 for (int i = 0; i < callbackCount; i++) {
1472 CallbackRecord callback = mCallbacks.valueAt(i);
1473 pw.println(" " + i + ": mPid=" + callback.mPid
1474 + ", mWifiDisplayScanRequested=" + callback.mWifiDisplayScanRequested);
1475 }
Jeff Brownad9ef192014-04-08 17:26:30 -07001476
1477 if (mDisplayPowerController != null) {
1478 mDisplayPowerController.dump(pw);
1479 }
Michael Wright1c9977b2016-07-12 13:30:10 -07001480
1481 pw.println();
1482 mPersistentDataStore.dump(pw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001483 }
1484 }
1485
Dan Gittik122df862018-03-28 16:59:22 +01001486 private static float[] getFloatArray(TypedArray array) {
1487 int length = array.length();
1488 float[] floatArray = new float[length];
1489 for (int i = 0; i < length; i++) {
1490 floatArray[i] = array.getFloat(i, Float.NaN);
1491 }
1492 array.recycle();
1493 return floatArray;
1494 }
1495
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001496 /**
1497 * This is the object that everything in the display manager locks on.
1498 * We make it an inner class within the {@link DisplayManagerService} to so that it is
1499 * clear that the object belongs to the display manager service and that it is
1500 * a unique object with a special purpose.
1501 */
1502 public static final class SyncRoot {
1503 }
1504
Santos Cordonee8931e2017-04-05 10:31:15 -07001505 @VisibleForTesting
1506 static class Injector {
1507 VirtualDisplayAdapter getVirtualDisplayAdapter(SyncRoot syncRoot, Context context,
1508 Handler handler, DisplayAdapter.Listener displayAdapterListener) {
1509 return new VirtualDisplayAdapter(syncRoot, context, handler, displayAdapterListener);
1510 }
Santos Cordonc22c5632017-06-21 16:03:49 -07001511
1512 long getDefaultDisplayDelayTimeout() {
1513 return WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT;
1514 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001515 }
1516
Alex Sakhartchouk879d24f2017-06-20 22:01:19 -04001517 @VisibleForTesting
1518 DisplayDeviceInfo getDisplayDeviceInfoInternal(int displayId) {
1519 synchronized (mSyncRoot) {
1520 LogicalDisplay display = mLogicalDisplays.get(displayId);
1521 if (display != null) {
1522 DisplayDevice displayDevice = display.getPrimaryDisplayDeviceLocked();
1523 return displayDevice.getDisplayDeviceInfoLocked();
1524 }
1525 return null;
1526 }
1527 }
1528
Jeff Brownbd6e1502012-08-28 03:27:37 -07001529 private final class DisplayManagerHandler extends Handler {
1530 public DisplayManagerHandler(Looper looper) {
1531 super(looper, null, true /*async*/);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001532 }
Jeff Brownbf5740e2012-08-19 23:20:02 -07001533
Jeff Brownbd6e1502012-08-28 03:27:37 -07001534 @Override
1535 public void handleMessage(Message msg) {
1536 switch (msg.what) {
Santos Cordonc22c5632017-06-21 16:03:49 -07001537 case MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS:
1538 registerDefaultDisplayAdapters();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001539 break;
1540
1541 case MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS:
1542 registerAdditionalDisplayAdapters();
1543 break;
1544
1545 case MSG_DELIVER_DISPLAY_EVENT:
1546 deliverDisplayEvent(msg.arg1, msg.arg2);
1547 break;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001548
1549 case MSG_REQUEST_TRAVERSAL:
Jeff Brown4ccb8232014-01-16 22:16:42 -08001550 mWindowManagerInternal.requestTraversalFromDisplayManager();
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001551 break;
Jeff Brownd728bf52012-09-08 18:05:28 -07001552
1553 case MSG_UPDATE_VIEWPORT: {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001554 final boolean changed;
Jeff Brownd728bf52012-09-08 18:05:28 -07001555 synchronized (mSyncRoot) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001556 changed = !mTempViewports.equals(mViewports);
1557 if (changed) {
1558 mTempViewports.clear();
1559 for (DisplayViewport d : mViewports) {
1560 mTempViewports.add(d.makeCopy());
1561 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001562 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001563 }
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001564 if (changed) {
1565 mInputManagerInternal.setDisplayViewports(mTempViewports);
1566 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001567 break;
1568 }
Kenny Guy22bd0442017-10-26 00:15:54 +01001569
Michael Wrighteef0e132017-11-21 17:57:52 +00001570 case MSG_LOAD_BRIGHTNESS_CONFIGURATION:
1571 loadBrightnessConfiguration();
1572 break;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001573 }
1574 }
1575 }
1576
1577 private final class DisplayAdapterListener implements DisplayAdapter.Listener {
1578 @Override
1579 public void onDisplayDeviceEvent(DisplayDevice device, int event) {
1580 switch (event) {
1581 case DisplayAdapter.DISPLAY_DEVICE_EVENT_ADDED:
1582 handleDisplayDeviceAdded(device);
1583 break;
1584
1585 case DisplayAdapter.DISPLAY_DEVICE_EVENT_CHANGED:
1586 handleDisplayDeviceChanged(device);
1587 break;
1588
1589 case DisplayAdapter.DISPLAY_DEVICE_EVENT_REMOVED:
1590 handleDisplayDeviceRemoved(device);
1591 break;
1592 }
1593 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001594
1595 @Override
1596 public void onTraversalRequested() {
1597 synchronized (mSyncRoot) {
Craig Mautner65d11b32012-10-01 13:59:52 -07001598 scheduleTraversalLocked(false);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001599 }
1600 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001601 }
1602
1603 private final class CallbackRecord implements DeathRecipient {
Jeff Brownce468a32013-11-21 16:42:03 -08001604 public final int mPid;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001605 private final IDisplayManagerCallback mCallback;
1606
Jeff Brownce468a32013-11-21 16:42:03 -08001607 public boolean mWifiDisplayScanRequested;
1608
Jeff Brownbd6e1502012-08-28 03:27:37 -07001609 public CallbackRecord(int pid, IDisplayManagerCallback callback) {
1610 mPid = pid;
1611 mCallback = callback;
1612 }
1613
1614 @Override
1615 public void binderDied() {
1616 if (DEBUG) {
1617 Slog.d(TAG, "Display listener for pid " + mPid + " died.");
1618 }
Jeff Brownce468a32013-11-21 16:42:03 -08001619 onCallbackDied(this);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001620 }
1621
1622 public void notifyDisplayEventAsync(int displayId, int event) {
1623 try {
1624 mCallback.onDisplayEvent(displayId, event);
1625 } catch (RemoteException ex) {
1626 Slog.w(TAG, "Failed to notify process "
1627 + mPid + " that displays changed, assuming it died.", ex);
1628 binderDied();
1629 }
1630 }
1631 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001632
Santos Cordonee8931e2017-04-05 10:31:15 -07001633 @VisibleForTesting
1634 final class BinderService extends IDisplayManager.Stub {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001635 /**
1636 * Returns information about the specified logical display.
1637 *
1638 * @param displayId The logical display id.
1639 * @return The logical display info, or null if the display does not exist. The
1640 * returned object must be treated as immutable.
1641 */
1642 @Override // Binder call
1643 public DisplayInfo getDisplayInfo(int displayId) {
1644 final int callingUid = Binder.getCallingUid();
1645 final long token = Binder.clearCallingIdentity();
1646 try {
1647 return getDisplayInfoInternal(displayId, callingUid);
1648 } finally {
1649 Binder.restoreCallingIdentity(token);
1650 }
1651 }
1652
1653 /**
1654 * Returns the list of all display ids.
1655 */
1656 @Override // Binder call
1657 public int[] getDisplayIds() {
1658 final int callingUid = Binder.getCallingUid();
1659 final long token = Binder.clearCallingIdentity();
1660 try {
1661 return getDisplayIdsInternal(callingUid);
1662 } finally {
1663 Binder.restoreCallingIdentity(token);
1664 }
1665 }
1666
Michael Wrighteedcbf12017-08-16 23:14:54 +01001667 /**
1668 * Returns the stable device display size, in pixels.
1669 */
1670 @Override // Binder call
1671 public Point getStableDisplaySize() {
1672 final long token = Binder.clearCallingIdentity();
1673 try {
1674 return getStableDisplaySizeInternal();
1675 } finally {
1676 Binder.restoreCallingIdentity(token);
1677 }
1678 }
1679
Jeff Brown4ccb8232014-01-16 22:16:42 -08001680 @Override // Binder call
1681 public void registerCallback(IDisplayManagerCallback callback) {
1682 if (callback == null) {
1683 throw new IllegalArgumentException("listener must not be null");
1684 }
1685
1686 final int callingPid = Binder.getCallingPid();
1687 final long token = Binder.clearCallingIdentity();
1688 try {
1689 registerCallbackInternal(callback, callingPid);
1690 } finally {
1691 Binder.restoreCallingIdentity(token);
1692 }
1693 }
1694
1695 @Override // Binder call
1696 public void startWifiDisplayScan() {
1697 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1698 "Permission required to start wifi display scans");
1699
1700 final int callingPid = Binder.getCallingPid();
1701 final long token = Binder.clearCallingIdentity();
1702 try {
1703 startWifiDisplayScanInternal(callingPid);
1704 } finally {
1705 Binder.restoreCallingIdentity(token);
1706 }
1707 }
1708
1709 @Override // Binder call
1710 public void stopWifiDisplayScan() {
1711 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1712 "Permission required to stop wifi display scans");
1713
1714 final int callingPid = Binder.getCallingPid();
1715 final long token = Binder.clearCallingIdentity();
1716 try {
1717 stopWifiDisplayScanInternal(callingPid);
1718 } finally {
1719 Binder.restoreCallingIdentity(token);
1720 }
1721 }
1722
1723 @Override // Binder call
1724 public void connectWifiDisplay(String address) {
1725 if (address == null) {
1726 throw new IllegalArgumentException("address must not be null");
1727 }
1728 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1729 "Permission required to connect to a wifi display");
1730
1731 final long token = Binder.clearCallingIdentity();
1732 try {
1733 connectWifiDisplayInternal(address);
1734 } finally {
1735 Binder.restoreCallingIdentity(token);
1736 }
1737 }
1738
1739 @Override // Binder call
1740 public void disconnectWifiDisplay() {
1741 // This request does not require special permissions.
1742 // Any app can request disconnection from the currently active wifi display.
1743 // This exception should no longer be needed once wifi display control moves
1744 // to the media router service.
1745
1746 final long token = Binder.clearCallingIdentity();
1747 try {
1748 disconnectWifiDisplayInternal();
1749 } finally {
1750 Binder.restoreCallingIdentity(token);
1751 }
1752 }
1753
1754 @Override // Binder call
1755 public void renameWifiDisplay(String address, String alias) {
1756 if (address == null) {
1757 throw new IllegalArgumentException("address must not be null");
1758 }
1759 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1760 "Permission required to rename to a wifi display");
1761
1762 final long token = Binder.clearCallingIdentity();
1763 try {
1764 renameWifiDisplayInternal(address, alias);
1765 } finally {
1766 Binder.restoreCallingIdentity(token);
1767 }
1768 }
1769
1770 @Override // Binder call
1771 public void forgetWifiDisplay(String address) {
1772 if (address == null) {
1773 throw new IllegalArgumentException("address must not be null");
1774 }
1775 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1776 "Permission required to forget to a wifi display");
1777
1778 final long token = Binder.clearCallingIdentity();
1779 try {
1780 forgetWifiDisplayInternal(address);
1781 } finally {
1782 Binder.restoreCallingIdentity(token);
1783 }
1784 }
1785
1786 @Override // Binder call
1787 public void pauseWifiDisplay() {
1788 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1789 "Permission required to pause a wifi display session");
1790
1791 final long token = Binder.clearCallingIdentity();
1792 try {
1793 pauseWifiDisplayInternal();
1794 } finally {
1795 Binder.restoreCallingIdentity(token);
1796 }
1797 }
1798
1799 @Override // Binder call
1800 public void resumeWifiDisplay() {
1801 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1802 "Permission required to resume a wifi display session");
1803
1804 final long token = Binder.clearCallingIdentity();
1805 try {
1806 resumeWifiDisplayInternal();
1807 } finally {
1808 Binder.restoreCallingIdentity(token);
1809 }
1810 }
1811
1812 @Override // Binder call
1813 public WifiDisplayStatus getWifiDisplayStatus() {
1814 // This request does not require special permissions.
1815 // Any app can get information about available wifi displays.
1816
1817 final long token = Binder.clearCallingIdentity();
1818 try {
1819 return getWifiDisplayStatusInternal();
1820 } finally {
1821 Binder.restoreCallingIdentity(token);
1822 }
1823 }
1824
1825 @Override // Binder call
Michael Wright1c9977b2016-07-12 13:30:10 -07001826 public void requestColorMode(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +01001827 mContext.enforceCallingOrSelfPermission(
Michael Wright1c9977b2016-07-12 13:30:10 -07001828 Manifest.permission.CONFIGURE_DISPLAY_COLOR_MODE,
1829 "Permission required to change the display color mode");
Michael Wright58e829f2015-09-15 00:13:26 +01001830 final long token = Binder.clearCallingIdentity();
1831 try {
Michael Wright1c9977b2016-07-12 13:30:10 -07001832 requestColorModeInternal(displayId, colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +01001833 } finally {
1834 Binder.restoreCallingIdentity(token);
1835 }
1836 }
1837
1838 @Override // Binder call
Bryan Mawhinney462e29d2018-03-22 15:52:41 +00001839 public void setSaturationLevel(float level) {
1840 mContext.enforceCallingOrSelfPermission(
1841 Manifest.permission.CONTROL_DISPLAY_SATURATION,
1842 "Permission required to set display saturation level");
1843 final long token = Binder.clearCallingIdentity();
1844 try {
1845 setSaturationLevelInternal(level);
1846 } finally {
1847 Binder.restoreCallingIdentity(token);
1848 }
1849 }
1850
1851 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001852 public int createVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wrightc39d47a2014-07-08 18:07:36 -07001853 IMediaProjection projection, String packageName, String name,
Santos Cordonee8931e2017-04-05 10:31:15 -07001854 int width, int height, int densityDpi, Surface surface, int flags,
1855 String uniqueId) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001856 final int callingUid = Binder.getCallingUid();
1857 if (!validatePackageName(callingUid, packageName)) {
1858 throw new SecurityException("packageName must match the calling uid");
1859 }
Michael Wright75ee9fc2014-09-01 19:55:22 -07001860 if (callback == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001861 throw new IllegalArgumentException("appToken must not be null");
1862 }
1863 if (TextUtils.isEmpty(name)) {
1864 throw new IllegalArgumentException("name must be non-null and non-empty");
1865 }
1866 if (width <= 0 || height <= 0 || densityDpi <= 0) {
1867 throw new IllegalArgumentException("width, height, and densityDpi must be "
1868 + "greater than 0");
1869 }
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001870 if (surface != null && surface.isSingleBuffered()) {
Pablo Ceballosaff2f942016-07-29 14:49:55 -07001871 throw new IllegalArgumentException("Surface can't be single-buffered");
1872 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001873
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001874 if ((flags & VIRTUAL_DISPLAY_FLAG_PUBLIC) != 0) {
1875 flags |= VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
1876
1877 // Public displays can't be allowed to show content when locked.
Andrii Kulian7211d2e2017-01-27 15:58:05 -08001878 if ((flags & VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD) != 0) {
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001879 throw new IllegalArgumentException(
1880 "Public display must not be marked as SHOW_WHEN_LOCKED_INSECURE");
1881 }
Michael Wright6720be42014-07-29 19:14:16 -07001882 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001883 if ((flags & VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY) != 0) {
1884 flags &= ~VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
Michael Wright6720be42014-07-29 19:14:16 -07001885 }
1886
Michael Wrightc39d47a2014-07-08 18:07:36 -07001887 if (projection != null) {
1888 try {
1889 if (!getProjectionService().isValidMediaProjection(projection)) {
1890 throw new SecurityException("Invalid media projection");
1891 }
Michael Wright6720be42014-07-29 19:14:16 -07001892 flags = projection.applyVirtualDisplayFlags(flags);
Michael Wrightc39d47a2014-07-08 18:07:36 -07001893 } catch (RemoteException e) {
Michael Wright6720be42014-07-29 19:14:16 -07001894 throw new SecurityException("unable to validate media projection or flags");
Michael Wrightc39d47a2014-07-08 18:07:36 -07001895 }
1896 }
1897
Michael Wright6720be42014-07-29 19:14:16 -07001898 if (callingUid != Process.SYSTEM_UID &&
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001899 (flags & VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001900 if (!canProjectVideo(projection)) {
1901 throw new SecurityException("Requires CAPTURE_VIDEO_OUTPUT or "
1902 + "CAPTURE_SECURE_VIDEO_OUTPUT permission, or an appropriate "
1903 + "MediaProjection token in order to create a screen sharing virtual "
1904 + "display.");
1905 }
1906 }
Santos Cordonb6992f22018-01-30 14:51:20 -08001907 if (callingUid != Process.SYSTEM_UID && (flags & VIRTUAL_DISPLAY_FLAG_SECURE) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001908 if (!canProjectSecureVideo(projection)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001909 throw new SecurityException("Requires CAPTURE_SECURE_VIDEO_OUTPUT "
Michael Wrightc39d47a2014-07-08 18:07:36 -07001910 + "or an appropriate MediaProjection token to create a "
1911 + "secure virtual display.");
Jeff Brown4ccb8232014-01-16 22:16:42 -08001912 }
1913 }
1914
1915 final long token = Binder.clearCallingIdentity();
1916 try {
Santos Cordonee8931e2017-04-05 10:31:15 -07001917 return createVirtualDisplayInternal(callback, projection, callingUid, packageName,
1918 name, width, height, densityDpi, surface, flags, uniqueId);
Jeff Brown4ccb8232014-01-16 22:16:42 -08001919 } finally {
1920 Binder.restoreCallingIdentity(token);
1921 }
1922 }
1923
1924 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001925 public void resizeVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wright01e840f2014-06-26 16:03:25 -07001926 int width, int height, int densityDpi) {
1927 final long token = Binder.clearCallingIdentity();
1928 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001929 resizeVirtualDisplayInternal(callback.asBinder(), width, height, densityDpi);
Michael Wright01e840f2014-06-26 16:03:25 -07001930 } finally {
1931 Binder.restoreCallingIdentity(token);
1932 }
1933 }
1934
1935 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001936 public void setVirtualDisplaySurface(IVirtualDisplayCallback callback, Surface surface) {
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001937 if (surface != null && surface.isSingleBuffered()) {
1938 throw new IllegalArgumentException("Surface can't be single-buffered");
1939 }
Jeff Brown92207df2014-04-16 13:16:07 -07001940 final long token = Binder.clearCallingIdentity();
1941 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001942 setVirtualDisplaySurfaceInternal(callback.asBinder(), surface);
Jeff Brown92207df2014-04-16 13:16:07 -07001943 } finally {
1944 Binder.restoreCallingIdentity(token);
1945 }
1946 }
1947
1948 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001949 public void releaseVirtualDisplay(IVirtualDisplayCallback callback) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001950 final long token = Binder.clearCallingIdentity();
1951 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001952 releaseVirtualDisplayInternal(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -08001953 } finally {
1954 Binder.restoreCallingIdentity(token);
1955 }
1956 }
1957
1958 @Override // Binder call
1959 public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06001960 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
Jeff Brown4ccb8232014-01-16 22:16:42 -08001961
1962 final long token = Binder.clearCallingIdentity();
1963 try {
1964 dumpInternal(pw);
1965 } finally {
1966 Binder.restoreCallingIdentity(token);
1967 }
1968 }
1969
Kenny Guy22bd0442017-10-26 00:15:54 +01001970 @Override // Binder call
Kenny Guy29aa30e2017-11-30 13:43:46 +00001971 public ParceledListSlice<BrightnessChangeEvent> getBrightnessEvents(String callingPackage) {
Kenny Guy22bd0442017-10-26 00:15:54 +01001972 mContext.enforceCallingOrSelfPermission(
1973 Manifest.permission.BRIGHTNESS_SLIDER_USAGE,
1974 "Permission to read brightness events.");
Kenny Guy29aa30e2017-11-30 13:43:46 +00001975
1976 final int callingUid = Binder.getCallingUid();
1977 AppOpsManager appOpsManager = mContext.getSystemService(AppOpsManager.class);
Jeff Sharkeyd9311192018-04-16 16:50:34 +00001978 final int mode = appOpsManager.noteOp(AppOpsManager.OP_GET_USAGE_STATS,
Kenny Guy29aa30e2017-11-30 13:43:46 +00001979 callingUid, callingPackage);
1980 final boolean hasUsageStats;
1981 if (mode == AppOpsManager.MODE_DEFAULT) {
1982 // The default behavior here is to check if PackageManager has given the app
1983 // permission.
1984 hasUsageStats = mContext.checkCallingPermission(
1985 Manifest.permission.PACKAGE_USAGE_STATS)
1986 == PackageManager.PERMISSION_GRANTED;
1987 } else {
1988 hasUsageStats = mode == AppOpsManager.MODE_ALLOWED;
1989 }
1990
1991 final int userId = UserHandle.getUserId(callingUid);
Kenny Guy22bd0442017-10-26 00:15:54 +01001992 final long token = Binder.clearCallingIdentity();
1993 try {
Michael Wright144aac92017-12-21 18:37:41 +00001994 synchronized (mSyncRoot) {
1995 return mDisplayPowerController.getBrightnessEvents(userId, hasUsageStats);
1996 }
Kenny Guy22bd0442017-10-26 00:15:54 +01001997 } finally {
1998 Binder.restoreCallingIdentity(token);
1999 }
2000 }
2001
Michael Wrighteef0e132017-11-21 17:57:52 +00002002 @Override // Binder call
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002003 public ParceledListSlice<AmbientBrightnessDayStats> getAmbientBrightnessStats() {
2004 mContext.enforceCallingOrSelfPermission(
2005 Manifest.permission.ACCESS_AMBIENT_LIGHT_STATS,
2006 "Permission required to to access ambient light stats.");
2007 final int callingUid = Binder.getCallingUid();
2008 final int userId = UserHandle.getUserId(callingUid);
2009 final long token = Binder.clearCallingIdentity();
2010 try {
2011 synchronized (mSyncRoot) {
2012 return mDisplayPowerController.getAmbientBrightnessStats(userId);
2013 }
2014 } finally {
2015 Binder.restoreCallingIdentity(token);
2016 }
2017 }
2018
2019 @Override // Binder call
Michael Wrighteef0e132017-11-21 17:57:52 +00002020 public void setBrightnessConfigurationForUser(
Kenny Guy05ce8092018-01-17 13:44:20 +00002021 BrightnessConfiguration c, @UserIdInt int userId, String packageName) {
Michael Wrighteef0e132017-11-21 17:57:52 +00002022 mContext.enforceCallingOrSelfPermission(
2023 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
2024 "Permission required to change the display's brightness configuration");
2025 if (userId != UserHandle.getCallingUserId()) {
2026 mContext.enforceCallingOrSelfPermission(
2027 Manifest.permission.INTERACT_ACROSS_USERS,
2028 "Permission required to change the display brightness"
2029 + " configuration of another user");
2030 }
Kenny Guy05ce8092018-01-17 13:44:20 +00002031 if (packageName != null && !validatePackageName(getCallingUid(), packageName)) {
2032 packageName = null;
2033 }
Michael Wrighteef0e132017-11-21 17:57:52 +00002034 final long token = Binder.clearCallingIdentity();
2035 try {
Kenny Guy05ce8092018-01-17 13:44:20 +00002036 setBrightnessConfigurationForUserInternal(c, userId, packageName);
Michael Wrighteef0e132017-11-21 17:57:52 +00002037 } finally {
2038 Binder.restoreCallingIdentity(token);
2039 }
2040 }
2041
Michael Wrightd8460232018-01-16 18:04:59 +00002042 @Override // Binder call
Kenny Guy6d1009f2018-03-14 14:28:23 +00002043 public BrightnessConfiguration getBrightnessConfigurationForUser(int userId) {
2044 mContext.enforceCallingOrSelfPermission(
2045 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
2046 "Permission required to read the display's brightness configuration");
2047 if (userId != UserHandle.getCallingUserId()) {
2048 mContext.enforceCallingOrSelfPermission(
2049 Manifest.permission.INTERACT_ACROSS_USERS,
2050 "Permission required to read the display brightness"
2051 + " configuration of another user");
2052 }
2053 final long token = Binder.clearCallingIdentity();
2054 try {
2055 final int userSerial = getUserManager().getUserSerialNumber(userId);
2056 synchronized (mSyncRoot) {
2057 BrightnessConfiguration config =
2058 mPersistentDataStore.getBrightnessConfiguration(userSerial);
2059 if (config == null) {
2060 config = mDisplayPowerController.getDefaultBrightnessConfiguration();
2061 }
2062 return config;
2063 }
2064 } finally {
2065 Binder.restoreCallingIdentity(token);
2066 }
2067 }
2068
2069 @Override // Binder call
2070 public BrightnessConfiguration getDefaultBrightnessConfiguration() {
2071 mContext.enforceCallingOrSelfPermission(
2072 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
2073 "Permission required to read the display's default brightness configuration");
2074 final long token = Binder.clearCallingIdentity();
2075 try {
2076 synchronized (mSyncRoot) {
2077 return mDisplayPowerController.getDefaultBrightnessConfiguration();
2078 }
2079 } finally {
2080 Binder.restoreCallingIdentity(token);
2081 }
2082 }
2083
2084 @Override // Binder call
Michael Wrightd8460232018-01-16 18:04:59 +00002085 public void setTemporaryBrightness(int brightness) {
2086 mContext.enforceCallingOrSelfPermission(
2087 Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS,
2088 "Permission required to set the display's brightness");
2089 final long token = Binder.clearCallingIdentity();
2090 try {
2091 synchronized (mSyncRoot) {
2092 mDisplayPowerController.setTemporaryBrightness(brightness);
2093 }
2094 } finally {
2095 Binder.restoreCallingIdentity(token);
2096 }
2097 }
2098
2099 @Override // Binder call
2100 public void setTemporaryAutoBrightnessAdjustment(float adjustment) {
2101 mContext.enforceCallingOrSelfPermission(
2102 Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS,
2103 "Permission required to set the display's auto brightness adjustment");
2104 final long token = Binder.clearCallingIdentity();
2105 try {
2106 synchronized (mSyncRoot) {
2107 mDisplayPowerController.setTemporaryAutoBrightnessAdjustment(adjustment);
2108 }
2109 } finally {
2110 Binder.restoreCallingIdentity(token);
2111 }
2112 }
2113
Dan Gittik7a32fba2018-03-28 12:19:38 +01002114 @Override // Binder call
2115 public void onShellCommand(FileDescriptor in, FileDescriptor out,
2116 FileDescriptor err, String[] args, ShellCallback callback,
2117 ResultReceiver resultReceiver) {
2118 final long token = Binder.clearCallingIdentity();
2119 try {
2120 DisplayManagerShellCommand command = new DisplayManagerShellCommand(this);
2121 command.exec(this, in, out, err, args, callback, resultReceiver);
2122 } finally {
2123 Binder.restoreCallingIdentity(token);
2124 }
2125 }
2126
Dan Gittik122df862018-03-28 16:59:22 +01002127 @Override // Binder call
2128 public Curve getMinimumBrightnessCurve() {
2129 final long token = Binder.clearCallingIdentity();
2130 try {
2131 return getMinimumBrightnessCurveInternal();
2132 } finally {
2133 Binder.restoreCallingIdentity(token);
2134 }
2135 }
2136
Dan Gittik7a32fba2018-03-28 12:19:38 +01002137 void setBrightness(int brightness) {
2138 Settings.System.putIntForUser(mContext.getContentResolver(),
2139 Settings.System.SCREEN_BRIGHTNESS, brightness, UserHandle.USER_CURRENT);
2140 }
2141
2142 void resetBrightnessConfiguration() {
2143 setBrightnessConfigurationForUserInternal(null, mContext.getUserId(),
2144 mContext.getPackageName());
2145 }
2146
Jeff Brown4ccb8232014-01-16 22:16:42 -08002147 private boolean validatePackageName(int uid, String packageName) {
2148 if (packageName != null) {
2149 String[] packageNames = mContext.getPackageManager().getPackagesForUid(uid);
2150 if (packageNames != null) {
2151 for (String n : packageNames) {
2152 if (n.equals(packageName)) {
2153 return true;
2154 }
2155 }
2156 }
2157 }
2158 return false;
2159 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07002160
2161 private boolean canProjectVideo(IMediaProjection projection) {
2162 if (projection != null) {
2163 try {
2164 if (projection.canProjectVideo()) {
2165 return true;
2166 }
2167 } catch (RemoteException e) {
2168 Slog.e(TAG, "Unable to query projection service for permissions", e);
2169 }
2170 }
2171 if (mContext.checkCallingPermission(
2172 android.Manifest.permission.CAPTURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07002173 == PackageManager.PERMISSION_GRANTED) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07002174 return true;
2175 }
2176 return canProjectSecureVideo(projection);
2177 }
2178
2179 private boolean canProjectSecureVideo(IMediaProjection projection) {
2180 if (projection != null) {
2181 try {
2182 if (projection.canProjectSecureVideo()){
2183 return true;
2184 }
2185 } catch (RemoteException e) {
2186 Slog.e(TAG, "Unable to query projection service for permissions", e);
2187 }
2188 }
2189 return mContext.checkCallingPermission(
2190 android.Manifest.permission.CAPTURE_SECURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07002191 == PackageManager.PERMISSION_GRANTED;
Michael Wrightc39d47a2014-07-08 18:07:36 -07002192 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08002193 }
2194
2195 private final class LocalService extends DisplayManagerInternal {
2196 @Override
Jeff Brown037c33e2014-04-09 00:31:55 -07002197 public void initPowerManagement(final DisplayPowerCallbacks callbacks, Handler handler,
Jeff Brownad9ef192014-04-08 17:26:30 -07002198 SensorManager sensorManager) {
2199 synchronized (mSyncRoot) {
Jeff Brown037c33e2014-04-09 00:31:55 -07002200 DisplayBlanker blanker = new DisplayBlanker() {
2201 @Override
Jeff Brown5d6443b2015-04-10 20:15:01 -07002202 public void requestDisplayState(int state, int brightness) {
Jeff Brown037c33e2014-04-09 00:31:55 -07002203 // The order of operations is important for legacy reasons.
2204 if (state == Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07002205 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07002206 }
2207
2208 callbacks.onDisplayStateChange(state);
2209
2210 if (state != Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07002211 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07002212 }
2213 }
2214 };
Jeff Brownad9ef192014-04-08 17:26:30 -07002215 mDisplayPowerController = new DisplayPowerController(
Jeff Brown037c33e2014-04-09 00:31:55 -07002216 mContext, callbacks, handler, sensorManager, blanker);
Jeff Brownad9ef192014-04-08 17:26:30 -07002217 }
Michael Wrighteef0e132017-11-21 17:57:52 +00002218
2219 mHandler.sendEmptyMessage(MSG_LOAD_BRIGHTNESS_CONFIGURATION);
Jeff Brownad9ef192014-04-08 17:26:30 -07002220 }
2221
2222 @Override
2223 public boolean requestPowerState(DisplayPowerRequest request,
2224 boolean waitForNegativeProximity) {
Michael Wrighteef0e132017-11-21 17:57:52 +00002225 synchronized (mSyncRoot) {
2226 return mDisplayPowerController.requestPowerState(request,
2227 waitForNegativeProximity);
2228 }
Jeff Brownad9ef192014-04-08 17:26:30 -07002229 }
2230
2231 @Override
2232 public boolean isProximitySensorAvailable() {
Michael Wrighteef0e132017-11-21 17:57:52 +00002233 synchronized (mSyncRoot) {
2234 return mDisplayPowerController.isProximitySensorAvailable();
2235 }
Jeff Brownad9ef192014-04-08 17:26:30 -07002236 }
2237
2238 @Override
Riddle Hsu654a6f92018-07-13 22:59:36 +08002239 public boolean screenshot(int displayId, Surface outSurface) {
2240 synchronized (mSyncRoot) {
2241 final LogicalDisplay display = mLogicalDisplays.get(displayId);
2242 if (display != null) {
2243 final DisplayDevice device = display.getPrimaryDisplayDeviceLocked();
2244 if (device != null) {
2245 final IBinder token = device.getDisplayTokenLocked();
2246 if (token != null) {
2247 SurfaceControl.screenshot(token, outSurface);
2248 return true;
2249 }
2250 }
2251 }
2252 }
2253 return false;
2254 }
2255
2256 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08002257 public DisplayInfo getDisplayInfo(int displayId) {
2258 return getDisplayInfoInternal(displayId, Process.myUid());
2259 }
2260
2261 @Override
2262 public void registerDisplayTransactionListener(DisplayTransactionListener listener) {
2263 if (listener == null) {
2264 throw new IllegalArgumentException("listener must not be null");
2265 }
2266
2267 registerDisplayTransactionListenerInternal(listener);
2268 }
2269
2270 @Override
2271 public void unregisterDisplayTransactionListener(DisplayTransactionListener listener) {
2272 if (listener == null) {
2273 throw new IllegalArgumentException("listener must not be null");
2274 }
2275
2276 unregisterDisplayTransactionListenerInternal(listener);
2277 }
2278
2279 @Override
2280 public void setDisplayInfoOverrideFromWindowManager(int displayId, DisplayInfo info) {
2281 setDisplayInfoOverrideFromWindowManagerInternal(displayId, info);
2282 }
2283
2284 @Override
Andrii Kuliancd097992017-03-23 18:31:59 -07002285 public void getNonOverrideDisplayInfo(int displayId, DisplayInfo outInfo) {
2286 getNonOverrideDisplayInfoInternal(displayId, outInfo);
2287 }
2288
2289 @Override
Robert Carrae606b42018-02-15 15:36:23 -08002290 public void performTraversal(SurfaceControl.Transaction t) {
2291 performTraversalInternal(t);
Jeff Brown4ccb8232014-01-16 22:16:42 -08002292 }
2293
2294 @Override
Michael Wright3f145a22014-07-22 19:46:03 -07002295 public void setDisplayProperties(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07002296 float requestedRefreshRate, int requestedMode, boolean inTraversal) {
2297 setDisplayPropertiesInternal(displayId, hasContent, requestedRefreshRate,
2298 requestedMode, inTraversal);
Jeff Brown4ccb8232014-01-16 22:16:42 -08002299 }
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08002300
2301 @Override
2302 public void setDisplayOffsets(int displayId, int x, int y) {
2303 setDisplayOffsetsInternal(displayId, x, y);
2304 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002305
2306 @Override
2307 public void setDisplayAccessUIDs(SparseArray<IntArray> newDisplayAccessUIDs) {
2308 setDisplayAccessUIDsInternal(newDisplayAccessUIDs);
2309 }
2310
2311 @Override
2312 public boolean isUidPresentOnDisplay(int uid, int displayId) {
2313 return isUidPresentOnDisplayInternal(uid, displayId);
2314 }
Kenny Guycfe7b702017-11-14 21:04:58 +00002315
2316 @Override
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002317 public void persistBrightnessTrackerState() {
Michael Wright144aac92017-12-21 18:37:41 +00002318 synchronized (mSyncRoot) {
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002319 mDisplayPowerController.persistBrightnessTrackerState();
Michael Wright144aac92017-12-21 18:37:41 +00002320 }
Kenny Guycfe7b702017-11-14 21:04:58 +00002321 }
Adrian Roose1d68cd2018-01-17 12:54:50 +01002322
2323 @Override
2324 public void onOverlayChanged() {
2325 synchronized (mSyncRoot) {
Adrian Roos898ec382018-01-17 12:54:50 +01002326 for (int i = 0; i < mDisplayDevices.size(); i++) {
2327 mDisplayDevices.get(i).onOverlayChangedLocked();
Adrian Roose1d68cd2018-01-17 12:54:50 +01002328 }
2329 }
2330 }
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07002331
2332 @Override
2333 public DisplayedContentSamplingAttributes getDisplayedContentSamplingAttributes(
2334 int displayId) {
2335 return getDisplayedContentSamplingAttributesInternal(displayId);
2336 }
2337
2338 @Override
2339 public boolean setDisplayedContentSamplingEnabled(
2340 int displayId, boolean enable, int componentMask, int maxFrames) {
2341 return setDisplayedContentSamplingEnabledInternal(
2342 displayId, enable, componentMask, maxFrames);
2343 }
2344
2345 @Override
2346 public DisplayedContentSample getDisplayedContentSample(int displayId,
2347 long maxFrames, long timestamp) {
2348 return getDisplayedContentSampleInternal(displayId, maxFrames, timestamp);
2349 }
2350
Jeff Brown4ccb8232014-01-16 22:16:42 -08002351 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07002352}