blob: 4dcbc24ec0232ebebdbe4dc39328fd602ca7f9bb [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
chaviwda4c6942018-11-07 15:52:56 -0800815 private void setVirtualDisplayStateInternal(IBinder appToken, boolean isOn) {
816 synchronized (mSyncRoot) {
817 if (mVirtualDisplayAdapter == null) {
818 return;
819 }
820
821 mVirtualDisplayAdapter.setVirtualDisplayStateLocked(appToken, isOn);
822 }
823 }
824
Santos Cordonc22c5632017-06-21 16:03:49 -0700825 private void registerDefaultDisplayAdapters() {
826 // Register default display adapters.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700827 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700828 // main display adapter
Mike Lockwoode63f6f72013-11-15 11:01:47 -0800829 registerDisplayAdapterLocked(new LocalDisplayAdapter(
830 mSyncRoot, mContext, mHandler, mDisplayAdapterListener));
Santos Cordonc22c5632017-06-21 16:03:49 -0700831
832 // Standalone VR devices rely on a virtual display as their primary display for
833 // 2D UI. We register virtual display adapter along side the main display adapter
834 // here so that it is ready by the time the system sends the home Intent for
835 // early apps like SetupWizard/Launcher. In particular, SUW is displayed using
836 // the virtual display inside VR before any VR-specific apps even run.
837 mVirtualDisplayAdapter = mInjector.getVirtualDisplayAdapter(mSyncRoot, mContext,
838 mHandler, mDisplayAdapterListener);
839 if (mVirtualDisplayAdapter != null) {
840 registerDisplayAdapterLocked(mVirtualDisplayAdapter);
841 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700842 }
843 }
844
845 private void registerAdditionalDisplayAdapters() {
846 synchronized (mSyncRoot) {
847 if (shouldRegisterNonEssentialDisplayAdaptersLocked()) {
Jeff Brown89d55462012-09-19 11:33:42 -0700848 registerOverlayDisplayAdapterLocked();
849 registerWifiDisplayAdapterLocked();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700850 }
851 }
852 }
853
Jeff Brown89d55462012-09-19 11:33:42 -0700854 private void registerOverlayDisplayAdapterLocked() {
855 registerDisplayAdapterLocked(new OverlayDisplayAdapter(
856 mSyncRoot, mContext, mHandler, mDisplayAdapterListener, mUiHandler));
857 }
858
859 private void registerWifiDisplayAdapterLocked() {
860 if (mContext.getResources().getBoolean(
Jeff Brownbbd28a22012-09-20 16:47:15 -0700861 com.android.internal.R.bool.config_enableWifiDisplay)
862 || SystemProperties.getInt(FORCE_WIFI_DISPLAY_ENABLE, -1) == 1) {
Jeff Brown89d55462012-09-19 11:33:42 -0700863 mWifiDisplayAdapter = new WifiDisplayAdapter(
864 mSyncRoot, mContext, mHandler, mDisplayAdapterListener,
865 mPersistentDataStore);
866 registerDisplayAdapterLocked(mWifiDisplayAdapter);
867 }
868 }
869
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700870 private boolean shouldRegisterNonEssentialDisplayAdaptersLocked() {
871 // In safe mode, we disable non-essential display adapters to give the user
872 // an opportunity to fix broken settings or other problems that might affect
873 // system stability.
874 // In only-core mode, we disable non-essential display adapters to minimize
875 // the number of dependencies that are started while in this mode and to
876 // prevent problems that might occur due to the device being encrypted.
877 return !mSafeMode && !mOnlyCore;
878 }
879
880 private void registerDisplayAdapterLocked(DisplayAdapter adapter) {
881 mDisplayAdapters.add(adapter);
882 adapter.registerLocked();
883 }
884
Jeff Brownbd6e1502012-08-28 03:27:37 -0700885 private void handleDisplayDeviceAdded(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700886 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700887 handleDisplayDeviceAddedLocked(device);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700888 }
889 }
890
Jeff Browna506a6e2013-06-04 00:02:38 -0700891 private void handleDisplayDeviceAddedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700892 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700893 if (mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700894 Slog.w(TAG, "Attempted to add already added display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700895 return;
896 }
897
Jeff Brown10acf6d2015-04-14 14:20:47 -0700898 Slog.i(TAG, "Display device added: " + info);
899 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700900
901 mDisplayDevices.add(device);
Michael Wright1c9977b2016-07-12 13:30:10 -0700902 LogicalDisplay display = addLogicalDisplayLocked(device);
Jeff Brown0033a862014-10-08 12:06:39 -0700903 Runnable work = updateDisplayStateLocked(device);
904 if (work != null) {
905 work.run();
906 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700907 scheduleTraversalLocked(false);
908 }
909
Jeff Brownbd6e1502012-08-28 03:27:37 -0700910 private void handleDisplayDeviceChanged(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700911 synchronized (mSyncRoot) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700912 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700913 if (!mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700914 Slog.w(TAG, "Attempted to change non-existent display device: " + info);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700915 return;
916 }
917
Jeff Brown10acf6d2015-04-14 14:20:47 -0700918 int diff = device.mDebugLastLoggedDeviceInfo.diff(info);
919 if (diff == DisplayDeviceInfo.DIFF_STATE) {
920 Slog.i(TAG, "Display device changed state: \"" + info.name
921 + "\", " + Display.stateToString(info.state));
922 } else if (diff != 0) {
923 Slog.i(TAG, "Display device changed: " + info);
924 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700925 if ((diff & DisplayDeviceInfo.DIFF_COLOR_MODE) != 0) {
926 try {
927 mPersistentDataStore.setColorMode(device, info.colorMode);
928 } finally {
929 mPersistentDataStore.saveIfNeeded();
930 }
931 }
Jeff Brown10acf6d2015-04-14 14:20:47 -0700932 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browne87bf032012-09-20 18:30:13 -0700933
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700934 device.applyPendingDisplayDeviceInfoChangesLocked();
935 if (updateLogicalDisplaysLocked()) {
Craig Mautner65d11b32012-10-01 13:59:52 -0700936 scheduleTraversalLocked(false);
Jeff Brown64a55af2012-08-26 02:47:39 -0700937 }
938 }
939 }
940
Jeff Brownbd6e1502012-08-28 03:27:37 -0700941 private void handleDisplayDeviceRemoved(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700942 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700943 handleDisplayDeviceRemovedLocked(device);
944 }
945 }
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700946
Jeff Browna506a6e2013-06-04 00:02:38 -0700947 private void handleDisplayDeviceRemovedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700948 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700949 if (!mDisplayDevices.remove(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700950 Slog.w(TAG, "Attempted to remove non-existent display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700951 return;
952 }
953
Jeff Brown10acf6d2015-04-14 14:20:47 -0700954 Slog.i(TAG, "Display device removed: " + info);
955 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700956
Jeff Browna506a6e2013-06-04 00:02:38 -0700957 updateLogicalDisplaysLocked();
958 scheduleTraversalLocked(false);
959 }
960
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200961 private void handleLogicalDisplayChanged(int displayId, @NonNull LogicalDisplay display) {
962 if (displayId == Display.DEFAULT_DISPLAY) {
963 recordTopInsetLocked(display);
964 }
965 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
966 }
967
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700968 private void applyGlobalDisplayStateLocked(List<Runnable> workQueue) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700969 final int count = mDisplayDevices.size();
970 for (int i = 0; i < count; i++) {
971 DisplayDevice device = mDisplayDevices.get(i);
Jeff Browne75926d2014-09-18 15:24:49 -0700972 Runnable runnable = updateDisplayStateLocked(device);
973 if (runnable != null) {
974 workQueue.add(runnable);
975 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700976 }
977 }
978
Jeff Browne75926d2014-09-18 15:24:49 -0700979 private Runnable updateDisplayStateLocked(DisplayDevice device) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700980 // Blank or unblank the display immediately to match the state requested
Jeff Brown037c33e2014-04-09 00:31:55 -0700981 // by the display power controller (if known).
Jeff Browna506a6e2013-06-04 00:02:38 -0700982 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
983 if ((info.flags & DisplayDeviceInfo.FLAG_NEVER_BLANK) == 0) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700984 return device.requestDisplayStateLocked(mGlobalDisplayState, mGlobalDisplayBrightness);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700985 }
Jeff Browne75926d2014-09-18 15:24:49 -0700986 return null;
Craig Mautner4f67ba62012-08-02 11:23:00 -0700987 }
988
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700989 // Adds a new logical display based on the given display device.
990 // Sends notifications if needed.
Michael Wright1c9977b2016-07-12 13:30:10 -0700991 private LogicalDisplay addLogicalDisplayLocked(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700992 DisplayDeviceInfo deviceInfo = device.getDisplayDeviceInfoLocked();
993 boolean isDefault = (deviceInfo.flags
994 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0;
995 if (isDefault && mLogicalDisplays.get(Display.DEFAULT_DISPLAY) != null) {
996 Slog.w(TAG, "Ignoring attempt to add a second default display: " + deviceInfo);
997 isDefault = false;
998 }
999
Jeff Brown27f1d672012-10-17 18:32:34 -07001000 if (!isDefault && mSingleDisplayDemoMode) {
1001 Slog.i(TAG, "Not creating a logical display for a secondary display "
1002 + " because single display demo mode is enabled: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -07001003 return null;
Jeff Brown27f1d672012-10-17 18:32:34 -07001004 }
1005
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001006 final int displayId = assignDisplayIdLocked(isDefault);
1007 final int layerStack = assignLayerStackLocked(displayId);
1008
Jeff Brownd728bf52012-09-08 18:05:28 -07001009 LogicalDisplay display = new LogicalDisplay(displayId, layerStack, device);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001010 display.updateLocked(mDisplayDevices);
1011 if (!display.isValidLocked()) {
1012 // This should never happen currently.
1013 Slog.w(TAG, "Ignoring display device because the logical display "
1014 + "created from it was not considered valid: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -07001015 return null;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001016 }
1017
Michael Wrighteedcbf12017-08-16 23:14:54 +01001018 configureColorModeLocked(display, device);
1019 if (isDefault) {
1020 recordStableDisplayStatsIfNeededLocked(display);
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001021 recordTopInsetLocked(display);
Michael Wrighteedcbf12017-08-16 23:14:54 +01001022 }
1023
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001024 mLogicalDisplays.put(displayId, display);
1025
1026 // Wake up waitForDefaultDisplay.
1027 if (isDefault) {
1028 mSyncRoot.notifyAll();
1029 }
1030
1031 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_ADDED);
Michael Wright1c9977b2016-07-12 13:30:10 -07001032 return display;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001033 }
1034
1035 private int assignDisplayIdLocked(boolean isDefault) {
1036 return isDefault ? Display.DEFAULT_DISPLAY : mNextNonDefaultDisplayId++;
1037 }
1038
1039 private int assignLayerStackLocked(int displayId) {
1040 // Currently layer stacks and display ids are the same.
1041 // This need not be the case.
1042 return displayId;
1043 }
1044
Michael Wrighteedcbf12017-08-16 23:14:54 +01001045 private void configureColorModeLocked(LogicalDisplay display, DisplayDevice device) {
1046 if (display.getPrimaryDisplayDeviceLocked() == device) {
1047 int colorMode = mPersistentDataStore.getColorMode(device);
1048 if (colorMode == Display.COLOR_MODE_INVALID) {
1049 if ((device.getDisplayDeviceInfoLocked().flags
1050 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
1051 colorMode = mDefaultDisplayDefaultColorMode;
1052 } else {
1053 colorMode = Display.COLOR_MODE_DEFAULT;
1054 }
1055 }
1056 display.setRequestedColorModeLocked(colorMode);
1057 }
1058 }
1059
1060 // If we've never recorded stable device stats for this device before and they aren't
1061 // explicitly configured, go ahead and record the stable device stats now based on the status
1062 // of the default display at first boot.
1063 private void recordStableDisplayStatsIfNeededLocked(LogicalDisplay d) {
1064 if (mStableDisplaySize.x <= 0 && mStableDisplaySize.y <= 0) {
1065 DisplayInfo info = d.getDisplayInfoLocked();
1066 setStableDisplaySizeLocked(info.getNaturalWidth(), info.getNaturalHeight());
1067 }
1068 }
1069
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001070 private void recordTopInsetLocked(@Nullable LogicalDisplay d) {
MÃ¥rten Kongstad250cb2b2018-11-02 11:01:44 +01001071 if (d == null) {
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001072 return;
1073 }
1074 int topInset = d.getInsets().top;
1075 if (topInset == mDefaultDisplayTopInset) {
1076 return;
1077 }
1078 mDefaultDisplayTopInset = topInset;
1079 SystemProperties.set(PROP_DEFAULT_DISPLAY_TOP_INSET, Integer.toString(topInset));
1080 }
1081
Michael Wrighteedcbf12017-08-16 23:14:54 +01001082 private void setStableDisplaySizeLocked(int width, int height) {
1083 mStableDisplaySize = new Point(width, height);
1084 try {
1085 mPersistentDataStore.setStableDisplaySize(mStableDisplaySize);
1086 } finally {
1087 mPersistentDataStore.saveIfNeeded();
1088 }
1089 }
1090
Dan Gittik122df862018-03-28 16:59:22 +01001091 @VisibleForTesting
1092 Curve getMinimumBrightnessCurveInternal() {
1093 return mMinimumBrightnessCurve;
1094 }
1095
Michael Wrighteef0e132017-11-21 17:57:52 +00001096 private void setBrightnessConfigurationForUserInternal(
Dan Gittik7a32fba2018-03-28 12:19:38 +01001097 @Nullable BrightnessConfiguration c, @UserIdInt int userId,
Kenny Guy05ce8092018-01-17 13:44:20 +00001098 @Nullable String packageName) {
Dan Gittik122df862018-03-28 16:59:22 +01001099 validateBrightnessConfiguration(c);
Michael Wrighteef0e132017-11-21 17:57:52 +00001100 final int userSerial = getUserManager().getUserSerialNumber(userId);
1101 synchronized (mSyncRoot) {
1102 try {
Kenny Guy05ce8092018-01-17 13:44:20 +00001103 mPersistentDataStore.setBrightnessConfigurationForUser(c, userSerial,
1104 packageName);
Michael Wrighteef0e132017-11-21 17:57:52 +00001105 } finally {
1106 mPersistentDataStore.saveIfNeeded();
1107 }
1108 if (userId == mCurrentUserId) {
1109 mDisplayPowerController.setBrightnessConfiguration(c);
1110 }
1111 }
1112 }
1113
Dan Gittik122df862018-03-28 16:59:22 +01001114 @VisibleForTesting
1115 void validateBrightnessConfiguration(BrightnessConfiguration config) {
1116 if (config == null) {
1117 return;
1118 }
1119 if (isBrightnessConfigurationTooDark(config)) {
1120 throw new IllegalArgumentException("brightness curve is too dark");
1121 }
1122 }
1123
1124 private boolean isBrightnessConfigurationTooDark(BrightnessConfiguration config) {
1125 Pair<float[], float[]> curve = config.getCurve();
1126 float[] lux = curve.first;
1127 float[] nits = curve.second;
1128 for (int i = 0; i < lux.length; i++) {
1129 if (nits[i] < mMinimumBrightnessSpline.interpolate(lux[i])) {
1130 return true;
1131 }
1132 }
1133 return false;
1134 }
1135
Michael Wrighteef0e132017-11-21 17:57:52 +00001136 private void loadBrightnessConfiguration() {
1137 synchronized (mSyncRoot) {
1138 final int userSerial = getUserManager().getUserSerialNumber(mCurrentUserId);
1139 BrightnessConfiguration config =
1140 mPersistentDataStore.getBrightnessConfiguration(userSerial);
1141 mDisplayPowerController.setBrightnessConfiguration(config);
1142 }
1143 }
1144
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001145 // Updates all existing logical displays given the current set of display devices.
1146 // Removes invalid logical displays.
1147 // Sends notifications if needed.
1148 private boolean updateLogicalDisplaysLocked() {
1149 boolean changed = false;
1150 for (int i = mLogicalDisplays.size(); i-- > 0; ) {
1151 final int displayId = mLogicalDisplays.keyAt(i);
1152 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1153
1154 mTempDisplayInfo.copyFrom(display.getDisplayInfoLocked());
1155 display.updateLocked(mDisplayDevices);
1156 if (!display.isValidLocked()) {
1157 mLogicalDisplays.removeAt(i);
1158 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_REMOVED);
1159 changed = true;
1160 } else if (!mTempDisplayInfo.equals(display.getDisplayInfoLocked())) {
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001161 handleLogicalDisplayChanged(displayId, display);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001162 changed = true;
1163 }
1164 }
1165 return changed;
1166 }
1167
Robert Carrae606b42018-02-15 15:36:23 -08001168 private void performTraversalLocked(SurfaceControl.Transaction t) {
Jeff Brownd728bf52012-09-08 18:05:28 -07001169 // Clear all viewports before configuring displays so that we can keep
1170 // track of which ones we have configured.
1171 clearViewportsLocked();
1172
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001173 // Configure each display device.
1174 final int count = mDisplayDevices.size();
1175 for (int i = 0; i < count; i++) {
1176 DisplayDevice device = mDisplayDevices.get(i);
Robert Carrae606b42018-02-15 15:36:23 -08001177 configureDisplayLocked(t, device);
1178 device.performTraversalLocked(t);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001179 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001180
1181 // Tell the input system about these new viewports.
Jeff Brown4ccb8232014-01-16 22:16:42 -08001182 if (mInputManagerInternal != null) {
Jeff Brownd728bf52012-09-08 18:05:28 -07001183 mHandler.sendEmptyMessage(MSG_UPDATE_VIEWPORT);
1184 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001185 }
1186
Michael Wright3f145a22014-07-22 19:46:03 -07001187 private void setDisplayPropertiesInternal(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001188 float requestedRefreshRate, int requestedModeId, boolean inTraversal) {
Craig Mautner722285e2012-09-07 13:55:58 -07001189 synchronized (mSyncRoot) {
1190 LogicalDisplay display = mLogicalDisplays.get(displayId);
Michael Wright3f145a22014-07-22 19:46:03 -07001191 if (display == null) {
1192 return;
1193 }
1194 if (display.hasContentLocked() != hasContent) {
Jeff Brown33041bd2013-08-02 21:11:14 -07001195 if (DEBUG) {
1196 Slog.d(TAG, "Display " + displayId + " hasContent flag changed: "
1197 + "hasContent=" + hasContent + ", inTraversal=" + inTraversal);
1198 }
1199
Craig Mautner722285e2012-09-07 13:55:58 -07001200 display.setHasContentLocked(hasContent);
Craig Mautner65d11b32012-10-01 13:59:52 -07001201 scheduleTraversalLocked(inTraversal);
Craig Mautner722285e2012-09-07 13:55:58 -07001202 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001203 if (requestedModeId == 0 && requestedRefreshRate != 0) {
1204 // Scan supported modes returned by display.getInfo() to find a mode with the same
1205 // size as the default display mode but with the specified refresh rate instead.
1206 requestedModeId = display.getDisplayInfoLocked().findDefaultModeByRefreshRate(
1207 requestedRefreshRate);
1208 }
1209 if (display.getRequestedModeIdLocked() != requestedModeId) {
Michael Wright3f145a22014-07-22 19:46:03 -07001210 if (DEBUG) {
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001211 Slog.d(TAG, "Display " + displayId + " switching to mode " + requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -07001212 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001213 display.setRequestedModeIdLocked(requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -07001214 scheduleTraversalLocked(inTraversal);
1215 }
Craig Mautner722285e2012-09-07 13:55:58 -07001216 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001217 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001218
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08001219 private void setDisplayOffsetsInternal(int displayId, int x, int y) {
1220 synchronized (mSyncRoot) {
1221 LogicalDisplay display = mLogicalDisplays.get(displayId);
1222 if (display == null) {
1223 return;
1224 }
1225 if (display.getDisplayOffsetXLocked() != x
1226 || display.getDisplayOffsetYLocked() != y) {
1227 if (DEBUG) {
1228 Slog.d(TAG, "Display " + displayId + " burn-in offset set to ("
1229 + x + ", " + y + ")");
1230 }
1231 display.setDisplayOffsetsLocked(x, y);
1232 scheduleTraversalLocked(false);
1233 }
1234 }
1235 }
1236
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001237 // Updates the lists of UIDs that are present on displays.
1238 private void setDisplayAccessUIDsInternal(SparseArray<IntArray> newDisplayAccessUIDs) {
1239 synchronized (mSyncRoot) {
1240 mDisplayAccessUIDs.clear();
1241 for (int i = newDisplayAccessUIDs.size() - 1; i >= 0; i--) {
1242 mDisplayAccessUIDs.append(newDisplayAccessUIDs.keyAt(i),
1243 newDisplayAccessUIDs.valueAt(i));
1244 }
1245 }
1246 }
1247
1248 // Checks if provided UID's content is present on the display and UID has access to it.
1249 private boolean isUidPresentOnDisplayInternal(int uid, int displayId) {
1250 synchronized (mSyncRoot) {
1251 final IntArray displayUIDs = mDisplayAccessUIDs.get(displayId);
1252 return displayUIDs != null && displayUIDs.indexOf(uid) != -1;
1253 }
1254 }
1255
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001256 @VisibleForTesting
1257 DisplayedContentSamplingAttributes getDisplayedContentSamplingAttributesInternal(
1258 int displayId) {
1259 IBinder displayToken = SurfaceControl.getBuiltInDisplay(displayId);
1260 return SurfaceControl.getDisplayedContentSamplingAttributes(displayToken);
1261 }
1262
1263 @VisibleForTesting
1264 boolean setDisplayedContentSamplingEnabledInternal(
1265 int displayId, boolean enable, int componentMask, int maxFrames) {
1266 IBinder displayToken = SurfaceControl.getBuiltInDisplay(displayId);
1267 return SurfaceControl.setDisplayedContentSamplingEnabled(
1268 displayToken, enable, componentMask, maxFrames);
1269 }
1270
1271 @VisibleForTesting
1272 DisplayedContentSample getDisplayedContentSampleInternal(int displayId,
1273 long maxFrames, long timestamp) {
1274 IBinder displayToken = SurfaceControl.getBuiltInDisplay(displayId);
1275 return SurfaceControl.getDisplayedContentSample(
1276 displayToken, maxFrames, timestamp);
1277 }
1278
Jeff Brownd728bf52012-09-08 18:05:28 -07001279 private void clearViewportsLocked() {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001280 mViewports.clear();
Craig Mautner722285e2012-09-07 13:55:58 -07001281 }
1282
Robert Carrae606b42018-02-15 15:36:23 -08001283 private void configureDisplayLocked(SurfaceControl.Transaction t, DisplayDevice device) {
Jeff Brownd14c8c92014-01-07 18:13:09 -08001284 final DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
1285 final boolean ownContent = (info.flags & DisplayDeviceInfo.FLAG_OWN_CONTENT_ONLY) != 0;
Jeff Browna506a6e2013-06-04 00:02:38 -07001286
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001287 // Find the logical display that the display device is showing.
Jeff Brownd14c8c92014-01-07 18:13:09 -08001288 // Certain displays only ever show their own content.
Craig Mautner722285e2012-09-07 13:55:58 -07001289 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
Jeff Brownd14c8c92014-01-07 18:13:09 -08001290 if (!ownContent) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001291 if (display != null && !display.hasContentLocked()) {
1292 // If the display does not have any content of its own, then
1293 // automatically mirror the default logical display contents.
1294 display = null;
1295 }
1296 if (display == null) {
1297 display = mLogicalDisplays.get(Display.DEFAULT_DISPLAY);
1298 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001299 }
1300
1301 // Apply the logical display configuration to the display device.
1302 if (display == null) {
1303 // TODO: no logical display for the device, blank it
Jeff Brownd728bf52012-09-08 18:05:28 -07001304 Slog.w(TAG, "Missing logical display to use for physical display device: "
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001305 + device.getDisplayDeviceInfoLocked());
Jeff Brownd728bf52012-09-08 18:05:28 -07001306 return;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001307 }
Robert Carrae606b42018-02-15 15:36:23 -08001308 display.configureDisplayLocked(t, device, info.state == Display.STATE_OFF);
Arthur Hung41e81e72018-10-31 18:04:56 +08001309 final int viewportType;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001310 // Update the corresponding viewport.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001311 if ((info.flags & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
Arthur Hung41e81e72018-10-31 18:04:56 +08001312 viewportType = VIEWPORT_INTERNAL;
1313 } else if (info.touch == DisplayDeviceInfo.TOUCH_EXTERNAL) {
1314 viewportType = VIEWPORT_EXTERNAL;
1315 } else if (info.touch == DisplayDeviceInfo.TOUCH_VIRTUAL
1316 && !TextUtils.isEmpty(info.uniqueId)) {
1317 viewportType = VIEWPORT_VIRTUAL;
1318 } else {
1319 Slog.wtf(TAG, "Unable to populate viewport for display device: " + info);
1320 return;
Jeff Brownd728bf52012-09-08 18:05:28 -07001321 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001322
Arthur Hung41e81e72018-10-31 18:04:56 +08001323 populateViewportLocked(viewportType, display.getDisplayIdLocked(), device, info.uniqueId);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001324 }
1325
1326 /**
1327 * Get internal or external viewport. Create it if does not currently exist.
1328 * @param viewportType - either INTERNAL or EXTERNAL
1329 * @return the viewport with the requested type
1330 */
Arthur Hung41e81e72018-10-31 18:04:56 +08001331 private DisplayViewport getViewportLocked(int viewportType, String uniqueId) {
1332 if (viewportType != VIEWPORT_INTERNAL && viewportType != VIEWPORT_EXTERNAL
1333 && viewportType != VIEWPORT_VIRTUAL) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001334 Slog.wtf(TAG, "Cannot call getViewportByTypeLocked for type "
1335 + DisplayViewport.typeToString(viewportType));
1336 return null;
1337 }
Arthur Hung41e81e72018-10-31 18:04:56 +08001338
1339 // Only allow a single INTERNAL or EXTERNAL viewport by forcing their uniqueIds
1340 // to be identical (in particular, empty).
1341 // TODO (b/116824030) allow multiple EXTERNAL viewports and remove this function.
1342 if (viewportType != VIEWPORT_VIRTUAL) {
1343 uniqueId = "";
1344 }
1345
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001346 DisplayViewport viewport;
1347 final int count = mViewports.size();
1348 for (int i = 0; i < count; i++) {
1349 viewport = mViewports.get(i);
Arthur Hung41e81e72018-10-31 18:04:56 +08001350 if (viewport.type == viewportType && uniqueId.equals(viewport.uniqueId)) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001351 return viewport;
1352 }
1353 }
1354
Arthur Hung41e81e72018-10-31 18:04:56 +08001355 // Creates the viewport if none exists.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001356 viewport = new DisplayViewport();
1357 viewport.type = viewportType;
Arthur Hung41e81e72018-10-31 18:04:56 +08001358 viewport.uniqueId = uniqueId;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001359 mViewports.add(viewport);
1360 return viewport;
1361 }
1362
Arthur Hung41e81e72018-10-31 18:04:56 +08001363 private void populateViewportLocked(int viewportType,
1364 int displayId, DisplayDevice device, String uniqueId) {
1365 final DisplayViewport viewport = getViewportLocked(viewportType, uniqueId);
Jeff Brownd728bf52012-09-08 18:05:28 -07001366 device.populateViewportLocked(viewport);
Arthur Hung41e81e72018-10-31 18:04:56 +08001367 viewport.valid = true;
1368 viewport.displayId = displayId;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001369 }
1370
1371 private LogicalDisplay findLogicalDisplayForDeviceLocked(DisplayDevice device) {
1372 final int count = mLogicalDisplays.size();
1373 for (int i = 0; i < count; i++) {
1374 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1375 if (display.getPrimaryDisplayDeviceLocked() == device) {
1376 return display;
1377 }
1378 }
1379 return null;
1380 }
1381
Jeff Brownbd6e1502012-08-28 03:27:37 -07001382 private void sendDisplayEventLocked(int displayId, int event) {
1383 Message msg = mHandler.obtainMessage(MSG_DELIVER_DISPLAY_EVENT, displayId, event);
1384 mHandler.sendMessage(msg);
1385 }
1386
Robert Carrae606b42018-02-15 15:36:23 -08001387 // Requests that performTraversals be called at a
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001388 // later time to apply changes to surfaces and displays.
Craig Mautner65d11b32012-10-01 13:59:52 -07001389 private void scheduleTraversalLocked(boolean inTraversal) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001390 if (!mPendingTraversal && mWindowManagerInternal != null) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001391 mPendingTraversal = true;
Craig Mautner65d11b32012-10-01 13:59:52 -07001392 if (!inTraversal) {
1393 mHandler.sendEmptyMessage(MSG_REQUEST_TRAVERSAL);
1394 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001395 }
1396 }
1397
1398 // Runs on Handler thread.
1399 // Delivers display event notifications to callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001400 private void deliverDisplayEvent(int displayId, int event) {
1401 if (DEBUG) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001402 Slog.d(TAG, "Delivering display event: displayId="
1403 + displayId + ", event=" + event);
Jeff Brownfa25bf52012-07-23 19:26:30 -07001404 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07001405
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001406 // Grab the lock and copy the callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001407 final int count;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001408 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001409 count = mCallbacks.size();
1410 mTempCallbacks.clear();
1411 for (int i = 0; i < count; i++) {
1412 mTempCallbacks.add(mCallbacks.valueAt(i));
Craig Mautner4f67ba62012-08-02 11:23:00 -07001413 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001414 }
Craig Mautner4f67ba62012-08-02 11:23:00 -07001415
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001416 // After releasing the lock, send the notifications out.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001417 for (int i = 0; i < count; i++) {
1418 mTempCallbacks.get(i).notifyDisplayEventAsync(displayId, event);
1419 }
1420 mTempCallbacks.clear();
Craig Mautner4f67ba62012-08-02 11:23:00 -07001421 }
1422
Michael Wrightc39d47a2014-07-08 18:07:36 -07001423 private IMediaProjectionManager getProjectionService() {
1424 if (mProjectionService == null) {
1425 IBinder b = ServiceManager.getService(Context.MEDIA_PROJECTION_SERVICE);
1426 mProjectionService = IMediaProjectionManager.Stub.asInterface(b);
1427 }
1428 return mProjectionService;
1429 }
1430
Michael Wrighteef0e132017-11-21 17:57:52 +00001431 private UserManager getUserManager() {
1432 return mContext.getSystemService(UserManager.class);
1433 }
1434
Jeff Brown4ccb8232014-01-16 22:16:42 -08001435 private void dumpInternal(PrintWriter pw) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001436 pw.println("DISPLAY MANAGER (dumpsys display)");
Jeff Brownfa25bf52012-07-23 19:26:30 -07001437
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001438 synchronized (mSyncRoot) {
Jeff Brown9e316a12012-10-08 19:17:06 -07001439 pw.println(" mOnlyCode=" + mOnlyCore);
1440 pw.println(" mSafeMode=" + mSafeMode);
1441 pw.println(" mPendingTraversal=" + mPendingTraversal);
Jeff Brown037c33e2014-04-09 00:31:55 -07001442 pw.println(" mGlobalDisplayState=" + Display.stateToString(mGlobalDisplayState));
Jeff Brown9e316a12012-10-08 19:17:06 -07001443 pw.println(" mNextNonDefaultDisplayId=" + mNextNonDefaultDisplayId);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001444 pw.println(" mViewports=" + mViewports);
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -07001445 pw.println(" mDefaultDisplayDefaultColorMode=" + mDefaultDisplayDefaultColorMode);
Jeff Brown27f1d672012-10-17 18:32:34 -07001446 pw.println(" mSingleDisplayDemoMode=" + mSingleDisplayDemoMode);
Jeff Brownce468a32013-11-21 16:42:03 -08001447 pw.println(" mWifiDisplayScanRequestCount=" + mWifiDisplayScanRequestCount);
Michael Wrighteedcbf12017-08-16 23:14:54 +01001448 pw.println(" mStableDisplaySize=" + mStableDisplaySize);
1449
Jeff Brown9e316a12012-10-08 19:17:06 -07001450
Jeff Brownbd6e1502012-08-28 03:27:37 -07001451 IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001452 ipw.increaseIndent();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001453
1454 pw.println();
1455 pw.println("Display Adapters: size=" + mDisplayAdapters.size());
Jeff Brown848c2dc2012-08-19 20:18:08 -07001456 for (DisplayAdapter adapter : mDisplayAdapters) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001457 pw.println(" " + adapter.getName());
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001458 adapter.dumpLocked(ipw);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001459 }
Craig Mautner9de49362012-08-02 14:30:30 -07001460
Jeff Brownbd6e1502012-08-28 03:27:37 -07001461 pw.println();
1462 pw.println("Display Devices: size=" + mDisplayDevices.size());
1463 for (DisplayDevice device : mDisplayDevices) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001464 pw.println(" " + device.getDisplayDeviceInfoLocked());
1465 device.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001466 }
1467
1468 final int logicalDisplayCount = mLogicalDisplays.size();
1469 pw.println();
1470 pw.println("Logical Displays: size=" + logicalDisplayCount);
1471 for (int i = 0; i < logicalDisplayCount; i++) {
1472 int displayId = mLogicalDisplays.keyAt(i);
1473 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1474 pw.println(" Display " + displayId + ":");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001475 display.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001476 }
Jeff Brownce468a32013-11-21 16:42:03 -08001477
1478 final int callbackCount = mCallbacks.size();
1479 pw.println();
1480 pw.println("Callbacks: size=" + callbackCount);
1481 for (int i = 0; i < callbackCount; i++) {
1482 CallbackRecord callback = mCallbacks.valueAt(i);
1483 pw.println(" " + i + ": mPid=" + callback.mPid
1484 + ", mWifiDisplayScanRequested=" + callback.mWifiDisplayScanRequested);
1485 }
Jeff Brownad9ef192014-04-08 17:26:30 -07001486
1487 if (mDisplayPowerController != null) {
1488 mDisplayPowerController.dump(pw);
1489 }
Michael Wright1c9977b2016-07-12 13:30:10 -07001490
1491 pw.println();
1492 mPersistentDataStore.dump(pw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001493 }
1494 }
1495
Dan Gittik122df862018-03-28 16:59:22 +01001496 private static float[] getFloatArray(TypedArray array) {
1497 int length = array.length();
1498 float[] floatArray = new float[length];
1499 for (int i = 0; i < length; i++) {
1500 floatArray[i] = array.getFloat(i, Float.NaN);
1501 }
1502 array.recycle();
1503 return floatArray;
1504 }
1505
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001506 /**
1507 * This is the object that everything in the display manager locks on.
1508 * We make it an inner class within the {@link DisplayManagerService} to so that it is
1509 * clear that the object belongs to the display manager service and that it is
1510 * a unique object with a special purpose.
1511 */
1512 public static final class SyncRoot {
1513 }
1514
Santos Cordonee8931e2017-04-05 10:31:15 -07001515 @VisibleForTesting
1516 static class Injector {
1517 VirtualDisplayAdapter getVirtualDisplayAdapter(SyncRoot syncRoot, Context context,
1518 Handler handler, DisplayAdapter.Listener displayAdapterListener) {
1519 return new VirtualDisplayAdapter(syncRoot, context, handler, displayAdapterListener);
1520 }
Santos Cordonc22c5632017-06-21 16:03:49 -07001521
1522 long getDefaultDisplayDelayTimeout() {
1523 return WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT;
1524 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001525 }
1526
Alex Sakhartchouk879d24f2017-06-20 22:01:19 -04001527 @VisibleForTesting
1528 DisplayDeviceInfo getDisplayDeviceInfoInternal(int displayId) {
1529 synchronized (mSyncRoot) {
1530 LogicalDisplay display = mLogicalDisplays.get(displayId);
1531 if (display != null) {
1532 DisplayDevice displayDevice = display.getPrimaryDisplayDeviceLocked();
1533 return displayDevice.getDisplayDeviceInfoLocked();
1534 }
1535 return null;
1536 }
1537 }
1538
Jeff Brownbd6e1502012-08-28 03:27:37 -07001539 private final class DisplayManagerHandler extends Handler {
1540 public DisplayManagerHandler(Looper looper) {
1541 super(looper, null, true /*async*/);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001542 }
Jeff Brownbf5740e2012-08-19 23:20:02 -07001543
Jeff Brownbd6e1502012-08-28 03:27:37 -07001544 @Override
1545 public void handleMessage(Message msg) {
1546 switch (msg.what) {
Santos Cordonc22c5632017-06-21 16:03:49 -07001547 case MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS:
1548 registerDefaultDisplayAdapters();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001549 break;
1550
1551 case MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS:
1552 registerAdditionalDisplayAdapters();
1553 break;
1554
1555 case MSG_DELIVER_DISPLAY_EVENT:
1556 deliverDisplayEvent(msg.arg1, msg.arg2);
1557 break;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001558
1559 case MSG_REQUEST_TRAVERSAL:
Jeff Brown4ccb8232014-01-16 22:16:42 -08001560 mWindowManagerInternal.requestTraversalFromDisplayManager();
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001561 break;
Jeff Brownd728bf52012-09-08 18:05:28 -07001562
1563 case MSG_UPDATE_VIEWPORT: {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001564 final boolean changed;
Jeff Brownd728bf52012-09-08 18:05:28 -07001565 synchronized (mSyncRoot) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001566 changed = !mTempViewports.equals(mViewports);
1567 if (changed) {
1568 mTempViewports.clear();
1569 for (DisplayViewport d : mViewports) {
1570 mTempViewports.add(d.makeCopy());
1571 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001572 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001573 }
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001574 if (changed) {
1575 mInputManagerInternal.setDisplayViewports(mTempViewports);
1576 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001577 break;
1578 }
Kenny Guy22bd0442017-10-26 00:15:54 +01001579
Michael Wrighteef0e132017-11-21 17:57:52 +00001580 case MSG_LOAD_BRIGHTNESS_CONFIGURATION:
1581 loadBrightnessConfiguration();
1582 break;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001583 }
1584 }
1585 }
1586
1587 private final class DisplayAdapterListener implements DisplayAdapter.Listener {
1588 @Override
1589 public void onDisplayDeviceEvent(DisplayDevice device, int event) {
1590 switch (event) {
1591 case DisplayAdapter.DISPLAY_DEVICE_EVENT_ADDED:
1592 handleDisplayDeviceAdded(device);
1593 break;
1594
1595 case DisplayAdapter.DISPLAY_DEVICE_EVENT_CHANGED:
1596 handleDisplayDeviceChanged(device);
1597 break;
1598
1599 case DisplayAdapter.DISPLAY_DEVICE_EVENT_REMOVED:
1600 handleDisplayDeviceRemoved(device);
1601 break;
1602 }
1603 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001604
1605 @Override
1606 public void onTraversalRequested() {
1607 synchronized (mSyncRoot) {
Craig Mautner65d11b32012-10-01 13:59:52 -07001608 scheduleTraversalLocked(false);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001609 }
1610 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001611 }
1612
1613 private final class CallbackRecord implements DeathRecipient {
Jeff Brownce468a32013-11-21 16:42:03 -08001614 public final int mPid;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001615 private final IDisplayManagerCallback mCallback;
1616
Jeff Brownce468a32013-11-21 16:42:03 -08001617 public boolean mWifiDisplayScanRequested;
1618
Jeff Brownbd6e1502012-08-28 03:27:37 -07001619 public CallbackRecord(int pid, IDisplayManagerCallback callback) {
1620 mPid = pid;
1621 mCallback = callback;
1622 }
1623
1624 @Override
1625 public void binderDied() {
1626 if (DEBUG) {
1627 Slog.d(TAG, "Display listener for pid " + mPid + " died.");
1628 }
Jeff Brownce468a32013-11-21 16:42:03 -08001629 onCallbackDied(this);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001630 }
1631
1632 public void notifyDisplayEventAsync(int displayId, int event) {
1633 try {
1634 mCallback.onDisplayEvent(displayId, event);
1635 } catch (RemoteException ex) {
1636 Slog.w(TAG, "Failed to notify process "
1637 + mPid + " that displays changed, assuming it died.", ex);
1638 binderDied();
1639 }
1640 }
1641 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001642
Santos Cordonee8931e2017-04-05 10:31:15 -07001643 @VisibleForTesting
1644 final class BinderService extends IDisplayManager.Stub {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001645 /**
1646 * Returns information about the specified logical display.
1647 *
1648 * @param displayId The logical display id.
1649 * @return The logical display info, or null if the display does not exist. The
1650 * returned object must be treated as immutable.
1651 */
1652 @Override // Binder call
1653 public DisplayInfo getDisplayInfo(int displayId) {
1654 final int callingUid = Binder.getCallingUid();
1655 final long token = Binder.clearCallingIdentity();
1656 try {
1657 return getDisplayInfoInternal(displayId, callingUid);
1658 } finally {
1659 Binder.restoreCallingIdentity(token);
1660 }
1661 }
1662
1663 /**
1664 * Returns the list of all display ids.
1665 */
1666 @Override // Binder call
1667 public int[] getDisplayIds() {
1668 final int callingUid = Binder.getCallingUid();
1669 final long token = Binder.clearCallingIdentity();
1670 try {
1671 return getDisplayIdsInternal(callingUid);
1672 } finally {
1673 Binder.restoreCallingIdentity(token);
1674 }
1675 }
1676
Michael Wrighteedcbf12017-08-16 23:14:54 +01001677 /**
1678 * Returns the stable device display size, in pixels.
1679 */
1680 @Override // Binder call
1681 public Point getStableDisplaySize() {
1682 final long token = Binder.clearCallingIdentity();
1683 try {
1684 return getStableDisplaySizeInternal();
1685 } finally {
1686 Binder.restoreCallingIdentity(token);
1687 }
1688 }
1689
Jeff Brown4ccb8232014-01-16 22:16:42 -08001690 @Override // Binder call
1691 public void registerCallback(IDisplayManagerCallback callback) {
1692 if (callback == null) {
1693 throw new IllegalArgumentException("listener must not be null");
1694 }
1695
1696 final int callingPid = Binder.getCallingPid();
1697 final long token = Binder.clearCallingIdentity();
1698 try {
1699 registerCallbackInternal(callback, callingPid);
1700 } finally {
1701 Binder.restoreCallingIdentity(token);
1702 }
1703 }
1704
1705 @Override // Binder call
1706 public void startWifiDisplayScan() {
1707 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1708 "Permission required to start wifi display scans");
1709
1710 final int callingPid = Binder.getCallingPid();
1711 final long token = Binder.clearCallingIdentity();
1712 try {
1713 startWifiDisplayScanInternal(callingPid);
1714 } finally {
1715 Binder.restoreCallingIdentity(token);
1716 }
1717 }
1718
1719 @Override // Binder call
1720 public void stopWifiDisplayScan() {
1721 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1722 "Permission required to stop wifi display scans");
1723
1724 final int callingPid = Binder.getCallingPid();
1725 final long token = Binder.clearCallingIdentity();
1726 try {
1727 stopWifiDisplayScanInternal(callingPid);
1728 } finally {
1729 Binder.restoreCallingIdentity(token);
1730 }
1731 }
1732
1733 @Override // Binder call
1734 public void connectWifiDisplay(String address) {
1735 if (address == null) {
1736 throw new IllegalArgumentException("address must not be null");
1737 }
1738 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1739 "Permission required to connect to a wifi display");
1740
1741 final long token = Binder.clearCallingIdentity();
1742 try {
1743 connectWifiDisplayInternal(address);
1744 } finally {
1745 Binder.restoreCallingIdentity(token);
1746 }
1747 }
1748
1749 @Override // Binder call
1750 public void disconnectWifiDisplay() {
1751 // This request does not require special permissions.
1752 // Any app can request disconnection from the currently active wifi display.
1753 // This exception should no longer be needed once wifi display control moves
1754 // to the media router service.
1755
1756 final long token = Binder.clearCallingIdentity();
1757 try {
1758 disconnectWifiDisplayInternal();
1759 } finally {
1760 Binder.restoreCallingIdentity(token);
1761 }
1762 }
1763
1764 @Override // Binder call
1765 public void renameWifiDisplay(String address, String alias) {
1766 if (address == null) {
1767 throw new IllegalArgumentException("address must not be null");
1768 }
1769 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1770 "Permission required to rename to a wifi display");
1771
1772 final long token = Binder.clearCallingIdentity();
1773 try {
1774 renameWifiDisplayInternal(address, alias);
1775 } finally {
1776 Binder.restoreCallingIdentity(token);
1777 }
1778 }
1779
1780 @Override // Binder call
1781 public void forgetWifiDisplay(String address) {
1782 if (address == null) {
1783 throw new IllegalArgumentException("address must not be null");
1784 }
1785 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1786 "Permission required to forget to a wifi display");
1787
1788 final long token = Binder.clearCallingIdentity();
1789 try {
1790 forgetWifiDisplayInternal(address);
1791 } finally {
1792 Binder.restoreCallingIdentity(token);
1793 }
1794 }
1795
1796 @Override // Binder call
1797 public void pauseWifiDisplay() {
1798 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1799 "Permission required to pause a wifi display session");
1800
1801 final long token = Binder.clearCallingIdentity();
1802 try {
1803 pauseWifiDisplayInternal();
1804 } finally {
1805 Binder.restoreCallingIdentity(token);
1806 }
1807 }
1808
1809 @Override // Binder call
1810 public void resumeWifiDisplay() {
1811 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1812 "Permission required to resume a wifi display session");
1813
1814 final long token = Binder.clearCallingIdentity();
1815 try {
1816 resumeWifiDisplayInternal();
1817 } finally {
1818 Binder.restoreCallingIdentity(token);
1819 }
1820 }
1821
1822 @Override // Binder call
1823 public WifiDisplayStatus getWifiDisplayStatus() {
1824 // This request does not require special permissions.
1825 // Any app can get information about available wifi displays.
1826
1827 final long token = Binder.clearCallingIdentity();
1828 try {
1829 return getWifiDisplayStatusInternal();
1830 } finally {
1831 Binder.restoreCallingIdentity(token);
1832 }
1833 }
1834
1835 @Override // Binder call
Michael Wright1c9977b2016-07-12 13:30:10 -07001836 public void requestColorMode(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +01001837 mContext.enforceCallingOrSelfPermission(
Michael Wright1c9977b2016-07-12 13:30:10 -07001838 Manifest.permission.CONFIGURE_DISPLAY_COLOR_MODE,
1839 "Permission required to change the display color mode");
Michael Wright58e829f2015-09-15 00:13:26 +01001840 final long token = Binder.clearCallingIdentity();
1841 try {
Michael Wright1c9977b2016-07-12 13:30:10 -07001842 requestColorModeInternal(displayId, colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +01001843 } finally {
1844 Binder.restoreCallingIdentity(token);
1845 }
1846 }
1847
1848 @Override // Binder call
Bryan Mawhinney462e29d2018-03-22 15:52:41 +00001849 public void setSaturationLevel(float level) {
1850 mContext.enforceCallingOrSelfPermission(
1851 Manifest.permission.CONTROL_DISPLAY_SATURATION,
1852 "Permission required to set display saturation level");
1853 final long token = Binder.clearCallingIdentity();
1854 try {
1855 setSaturationLevelInternal(level);
1856 } finally {
1857 Binder.restoreCallingIdentity(token);
1858 }
1859 }
1860
1861 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001862 public int createVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wrightc39d47a2014-07-08 18:07:36 -07001863 IMediaProjection projection, String packageName, String name,
Santos Cordonee8931e2017-04-05 10:31:15 -07001864 int width, int height, int densityDpi, Surface surface, int flags,
1865 String uniqueId) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001866 final int callingUid = Binder.getCallingUid();
1867 if (!validatePackageName(callingUid, packageName)) {
1868 throw new SecurityException("packageName must match the calling uid");
1869 }
Michael Wright75ee9fc2014-09-01 19:55:22 -07001870 if (callback == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001871 throw new IllegalArgumentException("appToken must not be null");
1872 }
1873 if (TextUtils.isEmpty(name)) {
1874 throw new IllegalArgumentException("name must be non-null and non-empty");
1875 }
1876 if (width <= 0 || height <= 0 || densityDpi <= 0) {
1877 throw new IllegalArgumentException("width, height, and densityDpi must be "
1878 + "greater than 0");
1879 }
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001880 if (surface != null && surface.isSingleBuffered()) {
Pablo Ceballosaff2f942016-07-29 14:49:55 -07001881 throw new IllegalArgumentException("Surface can't be single-buffered");
1882 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001883
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001884 if ((flags & VIRTUAL_DISPLAY_FLAG_PUBLIC) != 0) {
1885 flags |= VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
1886
1887 // Public displays can't be allowed to show content when locked.
Andrii Kulian7211d2e2017-01-27 15:58:05 -08001888 if ((flags & VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD) != 0) {
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001889 throw new IllegalArgumentException(
1890 "Public display must not be marked as SHOW_WHEN_LOCKED_INSECURE");
1891 }
Michael Wright6720be42014-07-29 19:14:16 -07001892 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001893 if ((flags & VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY) != 0) {
1894 flags &= ~VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
Michael Wright6720be42014-07-29 19:14:16 -07001895 }
1896
Michael Wrightc39d47a2014-07-08 18:07:36 -07001897 if (projection != null) {
1898 try {
1899 if (!getProjectionService().isValidMediaProjection(projection)) {
1900 throw new SecurityException("Invalid media projection");
1901 }
Michael Wright6720be42014-07-29 19:14:16 -07001902 flags = projection.applyVirtualDisplayFlags(flags);
Michael Wrightc39d47a2014-07-08 18:07:36 -07001903 } catch (RemoteException e) {
Michael Wright6720be42014-07-29 19:14:16 -07001904 throw new SecurityException("unable to validate media projection or flags");
Michael Wrightc39d47a2014-07-08 18:07:36 -07001905 }
1906 }
1907
Michael Wright6720be42014-07-29 19:14:16 -07001908 if (callingUid != Process.SYSTEM_UID &&
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001909 (flags & VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001910 if (!canProjectVideo(projection)) {
1911 throw new SecurityException("Requires CAPTURE_VIDEO_OUTPUT or "
1912 + "CAPTURE_SECURE_VIDEO_OUTPUT permission, or an appropriate "
1913 + "MediaProjection token in order to create a screen sharing virtual "
1914 + "display.");
1915 }
1916 }
Santos Cordonb6992f22018-01-30 14:51:20 -08001917 if (callingUid != Process.SYSTEM_UID && (flags & VIRTUAL_DISPLAY_FLAG_SECURE) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001918 if (!canProjectSecureVideo(projection)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001919 throw new SecurityException("Requires CAPTURE_SECURE_VIDEO_OUTPUT "
Michael Wrightc39d47a2014-07-08 18:07:36 -07001920 + "or an appropriate MediaProjection token to create a "
1921 + "secure virtual display.");
Jeff Brown4ccb8232014-01-16 22:16:42 -08001922 }
1923 }
1924
1925 final long token = Binder.clearCallingIdentity();
1926 try {
Santos Cordonee8931e2017-04-05 10:31:15 -07001927 return createVirtualDisplayInternal(callback, projection, callingUid, packageName,
1928 name, width, height, densityDpi, surface, flags, uniqueId);
Jeff Brown4ccb8232014-01-16 22:16:42 -08001929 } finally {
1930 Binder.restoreCallingIdentity(token);
1931 }
1932 }
1933
1934 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001935 public void resizeVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wright01e840f2014-06-26 16:03:25 -07001936 int width, int height, int densityDpi) {
1937 final long token = Binder.clearCallingIdentity();
1938 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001939 resizeVirtualDisplayInternal(callback.asBinder(), width, height, densityDpi);
Michael Wright01e840f2014-06-26 16:03:25 -07001940 } finally {
1941 Binder.restoreCallingIdentity(token);
1942 }
1943 }
1944
1945 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001946 public void setVirtualDisplaySurface(IVirtualDisplayCallback callback, Surface surface) {
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001947 if (surface != null && surface.isSingleBuffered()) {
1948 throw new IllegalArgumentException("Surface can't be single-buffered");
1949 }
Jeff Brown92207df2014-04-16 13:16:07 -07001950 final long token = Binder.clearCallingIdentity();
1951 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001952 setVirtualDisplaySurfaceInternal(callback.asBinder(), surface);
Jeff Brown92207df2014-04-16 13:16:07 -07001953 } finally {
1954 Binder.restoreCallingIdentity(token);
1955 }
1956 }
1957
1958 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001959 public void releaseVirtualDisplay(IVirtualDisplayCallback callback) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001960 final long token = Binder.clearCallingIdentity();
1961 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001962 releaseVirtualDisplayInternal(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -08001963 } finally {
1964 Binder.restoreCallingIdentity(token);
1965 }
1966 }
1967
1968 @Override // Binder call
chaviwda4c6942018-11-07 15:52:56 -08001969 public void setVirtualDisplayState(IVirtualDisplayCallback callback, boolean isOn) {
1970 final long token = Binder.clearCallingIdentity();
1971 try {
1972 setVirtualDisplayStateInternal(callback.asBinder(), isOn);
1973 } finally {
1974 Binder.restoreCallingIdentity(token);
1975 }
1976 }
1977
1978 @Override // Binder call
Jeff Brown4ccb8232014-01-16 22:16:42 -08001979 public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06001980 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
Jeff Brown4ccb8232014-01-16 22:16:42 -08001981
1982 final long token = Binder.clearCallingIdentity();
1983 try {
1984 dumpInternal(pw);
1985 } finally {
1986 Binder.restoreCallingIdentity(token);
1987 }
1988 }
1989
Kenny Guy22bd0442017-10-26 00:15:54 +01001990 @Override // Binder call
Kenny Guy29aa30e2017-11-30 13:43:46 +00001991 public ParceledListSlice<BrightnessChangeEvent> getBrightnessEvents(String callingPackage) {
Kenny Guy22bd0442017-10-26 00:15:54 +01001992 mContext.enforceCallingOrSelfPermission(
1993 Manifest.permission.BRIGHTNESS_SLIDER_USAGE,
1994 "Permission to read brightness events.");
Kenny Guy29aa30e2017-11-30 13:43:46 +00001995
1996 final int callingUid = Binder.getCallingUid();
1997 AppOpsManager appOpsManager = mContext.getSystemService(AppOpsManager.class);
Jeff Sharkeyd9311192018-04-16 16:50:34 +00001998 final int mode = appOpsManager.noteOp(AppOpsManager.OP_GET_USAGE_STATS,
Kenny Guy29aa30e2017-11-30 13:43:46 +00001999 callingUid, callingPackage);
2000 final boolean hasUsageStats;
2001 if (mode == AppOpsManager.MODE_DEFAULT) {
2002 // The default behavior here is to check if PackageManager has given the app
2003 // permission.
2004 hasUsageStats = mContext.checkCallingPermission(
2005 Manifest.permission.PACKAGE_USAGE_STATS)
2006 == PackageManager.PERMISSION_GRANTED;
2007 } else {
2008 hasUsageStats = mode == AppOpsManager.MODE_ALLOWED;
2009 }
2010
2011 final int userId = UserHandle.getUserId(callingUid);
Kenny Guy22bd0442017-10-26 00:15:54 +01002012 final long token = Binder.clearCallingIdentity();
2013 try {
Michael Wright144aac92017-12-21 18:37:41 +00002014 synchronized (mSyncRoot) {
2015 return mDisplayPowerController.getBrightnessEvents(userId, hasUsageStats);
2016 }
Kenny Guy22bd0442017-10-26 00:15:54 +01002017 } finally {
2018 Binder.restoreCallingIdentity(token);
2019 }
2020 }
2021
Michael Wrighteef0e132017-11-21 17:57:52 +00002022 @Override // Binder call
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002023 public ParceledListSlice<AmbientBrightnessDayStats> getAmbientBrightnessStats() {
2024 mContext.enforceCallingOrSelfPermission(
2025 Manifest.permission.ACCESS_AMBIENT_LIGHT_STATS,
2026 "Permission required to to access ambient light stats.");
2027 final int callingUid = Binder.getCallingUid();
2028 final int userId = UserHandle.getUserId(callingUid);
2029 final long token = Binder.clearCallingIdentity();
2030 try {
2031 synchronized (mSyncRoot) {
2032 return mDisplayPowerController.getAmbientBrightnessStats(userId);
2033 }
2034 } finally {
2035 Binder.restoreCallingIdentity(token);
2036 }
2037 }
2038
2039 @Override // Binder call
Michael Wrighteef0e132017-11-21 17:57:52 +00002040 public void setBrightnessConfigurationForUser(
Kenny Guy05ce8092018-01-17 13:44:20 +00002041 BrightnessConfiguration c, @UserIdInt int userId, String packageName) {
Michael Wrighteef0e132017-11-21 17:57:52 +00002042 mContext.enforceCallingOrSelfPermission(
2043 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
2044 "Permission required to change the display's brightness configuration");
2045 if (userId != UserHandle.getCallingUserId()) {
2046 mContext.enforceCallingOrSelfPermission(
2047 Manifest.permission.INTERACT_ACROSS_USERS,
2048 "Permission required to change the display brightness"
2049 + " configuration of another user");
2050 }
Kenny Guy05ce8092018-01-17 13:44:20 +00002051 if (packageName != null && !validatePackageName(getCallingUid(), packageName)) {
2052 packageName = null;
2053 }
Michael Wrighteef0e132017-11-21 17:57:52 +00002054 final long token = Binder.clearCallingIdentity();
2055 try {
Kenny Guy05ce8092018-01-17 13:44:20 +00002056 setBrightnessConfigurationForUserInternal(c, userId, packageName);
Michael Wrighteef0e132017-11-21 17:57:52 +00002057 } finally {
2058 Binder.restoreCallingIdentity(token);
2059 }
2060 }
2061
Michael Wrightd8460232018-01-16 18:04:59 +00002062 @Override // Binder call
Kenny Guy6d1009f2018-03-14 14:28:23 +00002063 public BrightnessConfiguration getBrightnessConfigurationForUser(int userId) {
2064 mContext.enforceCallingOrSelfPermission(
2065 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
2066 "Permission required to read the display's brightness configuration");
2067 if (userId != UserHandle.getCallingUserId()) {
2068 mContext.enforceCallingOrSelfPermission(
2069 Manifest.permission.INTERACT_ACROSS_USERS,
2070 "Permission required to read the display brightness"
2071 + " configuration of another user");
2072 }
2073 final long token = Binder.clearCallingIdentity();
2074 try {
2075 final int userSerial = getUserManager().getUserSerialNumber(userId);
2076 synchronized (mSyncRoot) {
2077 BrightnessConfiguration config =
2078 mPersistentDataStore.getBrightnessConfiguration(userSerial);
2079 if (config == null) {
2080 config = mDisplayPowerController.getDefaultBrightnessConfiguration();
2081 }
2082 return config;
2083 }
2084 } finally {
2085 Binder.restoreCallingIdentity(token);
2086 }
2087 }
2088
2089 @Override // Binder call
2090 public BrightnessConfiguration getDefaultBrightnessConfiguration() {
2091 mContext.enforceCallingOrSelfPermission(
2092 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
2093 "Permission required to read the display's default brightness configuration");
2094 final long token = Binder.clearCallingIdentity();
2095 try {
2096 synchronized (mSyncRoot) {
2097 return mDisplayPowerController.getDefaultBrightnessConfiguration();
2098 }
2099 } finally {
2100 Binder.restoreCallingIdentity(token);
2101 }
2102 }
2103
2104 @Override // Binder call
Michael Wrightd8460232018-01-16 18:04:59 +00002105 public void setTemporaryBrightness(int brightness) {
2106 mContext.enforceCallingOrSelfPermission(
2107 Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS,
2108 "Permission required to set the display's brightness");
2109 final long token = Binder.clearCallingIdentity();
2110 try {
2111 synchronized (mSyncRoot) {
2112 mDisplayPowerController.setTemporaryBrightness(brightness);
2113 }
2114 } finally {
2115 Binder.restoreCallingIdentity(token);
2116 }
2117 }
2118
2119 @Override // Binder call
2120 public void setTemporaryAutoBrightnessAdjustment(float adjustment) {
2121 mContext.enforceCallingOrSelfPermission(
2122 Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS,
2123 "Permission required to set the display's auto brightness adjustment");
2124 final long token = Binder.clearCallingIdentity();
2125 try {
2126 synchronized (mSyncRoot) {
2127 mDisplayPowerController.setTemporaryAutoBrightnessAdjustment(adjustment);
2128 }
2129 } finally {
2130 Binder.restoreCallingIdentity(token);
2131 }
2132 }
2133
Dan Gittik7a32fba2018-03-28 12:19:38 +01002134 @Override // Binder call
2135 public void onShellCommand(FileDescriptor in, FileDescriptor out,
2136 FileDescriptor err, String[] args, ShellCallback callback,
2137 ResultReceiver resultReceiver) {
2138 final long token = Binder.clearCallingIdentity();
2139 try {
2140 DisplayManagerShellCommand command = new DisplayManagerShellCommand(this);
2141 command.exec(this, in, out, err, args, callback, resultReceiver);
2142 } finally {
2143 Binder.restoreCallingIdentity(token);
2144 }
2145 }
2146
Dan Gittik122df862018-03-28 16:59:22 +01002147 @Override // Binder call
2148 public Curve getMinimumBrightnessCurve() {
2149 final long token = Binder.clearCallingIdentity();
2150 try {
2151 return getMinimumBrightnessCurveInternal();
2152 } finally {
2153 Binder.restoreCallingIdentity(token);
2154 }
2155 }
2156
Dan Gittik7a32fba2018-03-28 12:19:38 +01002157 void setBrightness(int brightness) {
2158 Settings.System.putIntForUser(mContext.getContentResolver(),
2159 Settings.System.SCREEN_BRIGHTNESS, brightness, UserHandle.USER_CURRENT);
2160 }
2161
2162 void resetBrightnessConfiguration() {
2163 setBrightnessConfigurationForUserInternal(null, mContext.getUserId(),
2164 mContext.getPackageName());
2165 }
2166
Dan Gittik54478d92018-11-07 14:39:00 +00002167 void setAutoBrightnessLoggingEnabled(boolean enabled) {
2168 if (mDisplayPowerController != null) {
2169 synchronized (mSyncRoot) {
2170 mDisplayPowerController.setAutoBrightnessLoggingEnabled(enabled);
2171 }
2172 }
2173 }
2174
Jeff Brown4ccb8232014-01-16 22:16:42 -08002175 private boolean validatePackageName(int uid, String packageName) {
2176 if (packageName != null) {
2177 String[] packageNames = mContext.getPackageManager().getPackagesForUid(uid);
2178 if (packageNames != null) {
2179 for (String n : packageNames) {
2180 if (n.equals(packageName)) {
2181 return true;
2182 }
2183 }
2184 }
2185 }
2186 return false;
2187 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07002188
2189 private boolean canProjectVideo(IMediaProjection projection) {
2190 if (projection != null) {
2191 try {
2192 if (projection.canProjectVideo()) {
2193 return true;
2194 }
2195 } catch (RemoteException e) {
2196 Slog.e(TAG, "Unable to query projection service for permissions", e);
2197 }
2198 }
2199 if (mContext.checkCallingPermission(
2200 android.Manifest.permission.CAPTURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07002201 == PackageManager.PERMISSION_GRANTED) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07002202 return true;
2203 }
2204 return canProjectSecureVideo(projection);
2205 }
2206
2207 private boolean canProjectSecureVideo(IMediaProjection projection) {
2208 if (projection != null) {
2209 try {
2210 if (projection.canProjectSecureVideo()){
2211 return true;
2212 }
2213 } catch (RemoteException e) {
2214 Slog.e(TAG, "Unable to query projection service for permissions", e);
2215 }
2216 }
2217 return mContext.checkCallingPermission(
2218 android.Manifest.permission.CAPTURE_SECURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07002219 == PackageManager.PERMISSION_GRANTED;
Michael Wrightc39d47a2014-07-08 18:07:36 -07002220 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08002221 }
2222
2223 private final class LocalService extends DisplayManagerInternal {
2224 @Override
Jeff Brown037c33e2014-04-09 00:31:55 -07002225 public void initPowerManagement(final DisplayPowerCallbacks callbacks, Handler handler,
Jeff Brownad9ef192014-04-08 17:26:30 -07002226 SensorManager sensorManager) {
2227 synchronized (mSyncRoot) {
Jeff Brown037c33e2014-04-09 00:31:55 -07002228 DisplayBlanker blanker = new DisplayBlanker() {
2229 @Override
Jeff Brown5d6443b2015-04-10 20:15:01 -07002230 public void requestDisplayState(int state, int brightness) {
Jeff Brown037c33e2014-04-09 00:31:55 -07002231 // The order of operations is important for legacy reasons.
2232 if (state == Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07002233 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07002234 }
2235
2236 callbacks.onDisplayStateChange(state);
2237
2238 if (state != Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07002239 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07002240 }
2241 }
2242 };
Jeff Brownad9ef192014-04-08 17:26:30 -07002243 mDisplayPowerController = new DisplayPowerController(
Jeff Brown037c33e2014-04-09 00:31:55 -07002244 mContext, callbacks, handler, sensorManager, blanker);
Jeff Brownad9ef192014-04-08 17:26:30 -07002245 }
Michael Wrighteef0e132017-11-21 17:57:52 +00002246
2247 mHandler.sendEmptyMessage(MSG_LOAD_BRIGHTNESS_CONFIGURATION);
Jeff Brownad9ef192014-04-08 17:26:30 -07002248 }
2249
2250 @Override
2251 public boolean requestPowerState(DisplayPowerRequest request,
2252 boolean waitForNegativeProximity) {
Michael Wrighteef0e132017-11-21 17:57:52 +00002253 synchronized (mSyncRoot) {
2254 return mDisplayPowerController.requestPowerState(request,
2255 waitForNegativeProximity);
2256 }
Jeff Brownad9ef192014-04-08 17:26:30 -07002257 }
2258
2259 @Override
2260 public boolean isProximitySensorAvailable() {
Michael Wrighteef0e132017-11-21 17:57:52 +00002261 synchronized (mSyncRoot) {
2262 return mDisplayPowerController.isProximitySensorAvailable();
2263 }
Jeff Brownad9ef192014-04-08 17:26:30 -07002264 }
2265
2266 @Override
Riddle Hsu654a6f92018-07-13 22:59:36 +08002267 public boolean screenshot(int displayId, Surface outSurface) {
2268 synchronized (mSyncRoot) {
2269 final LogicalDisplay display = mLogicalDisplays.get(displayId);
2270 if (display != null) {
2271 final DisplayDevice device = display.getPrimaryDisplayDeviceLocked();
2272 if (device != null) {
2273 final IBinder token = device.getDisplayTokenLocked();
2274 if (token != null) {
2275 SurfaceControl.screenshot(token, outSurface);
2276 return true;
2277 }
2278 }
2279 }
2280 }
2281 return false;
2282 }
2283
2284 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08002285 public DisplayInfo getDisplayInfo(int displayId) {
2286 return getDisplayInfoInternal(displayId, Process.myUid());
2287 }
2288
2289 @Override
2290 public void registerDisplayTransactionListener(DisplayTransactionListener listener) {
2291 if (listener == null) {
2292 throw new IllegalArgumentException("listener must not be null");
2293 }
2294
2295 registerDisplayTransactionListenerInternal(listener);
2296 }
2297
2298 @Override
2299 public void unregisterDisplayTransactionListener(DisplayTransactionListener listener) {
2300 if (listener == null) {
2301 throw new IllegalArgumentException("listener must not be null");
2302 }
2303
2304 unregisterDisplayTransactionListenerInternal(listener);
2305 }
2306
2307 @Override
2308 public void setDisplayInfoOverrideFromWindowManager(int displayId, DisplayInfo info) {
2309 setDisplayInfoOverrideFromWindowManagerInternal(displayId, info);
2310 }
2311
2312 @Override
Andrii Kuliancd097992017-03-23 18:31:59 -07002313 public void getNonOverrideDisplayInfo(int displayId, DisplayInfo outInfo) {
2314 getNonOverrideDisplayInfoInternal(displayId, outInfo);
2315 }
2316
2317 @Override
Robert Carrae606b42018-02-15 15:36:23 -08002318 public void performTraversal(SurfaceControl.Transaction t) {
2319 performTraversalInternal(t);
Jeff Brown4ccb8232014-01-16 22:16:42 -08002320 }
2321
2322 @Override
Michael Wright3f145a22014-07-22 19:46:03 -07002323 public void setDisplayProperties(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07002324 float requestedRefreshRate, int requestedMode, boolean inTraversal) {
2325 setDisplayPropertiesInternal(displayId, hasContent, requestedRefreshRate,
2326 requestedMode, inTraversal);
Jeff Brown4ccb8232014-01-16 22:16:42 -08002327 }
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08002328
2329 @Override
2330 public void setDisplayOffsets(int displayId, int x, int y) {
2331 setDisplayOffsetsInternal(displayId, x, y);
2332 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002333
2334 @Override
2335 public void setDisplayAccessUIDs(SparseArray<IntArray> newDisplayAccessUIDs) {
2336 setDisplayAccessUIDsInternal(newDisplayAccessUIDs);
2337 }
2338
2339 @Override
2340 public boolean isUidPresentOnDisplay(int uid, int displayId) {
2341 return isUidPresentOnDisplayInternal(uid, displayId);
2342 }
Kenny Guycfe7b702017-11-14 21:04:58 +00002343
2344 @Override
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002345 public void persistBrightnessTrackerState() {
Michael Wright144aac92017-12-21 18:37:41 +00002346 synchronized (mSyncRoot) {
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002347 mDisplayPowerController.persistBrightnessTrackerState();
Michael Wright144aac92017-12-21 18:37:41 +00002348 }
Kenny Guycfe7b702017-11-14 21:04:58 +00002349 }
Adrian Roose1d68cd2018-01-17 12:54:50 +01002350
2351 @Override
2352 public void onOverlayChanged() {
2353 synchronized (mSyncRoot) {
Adrian Roos898ec382018-01-17 12:54:50 +01002354 for (int i = 0; i < mDisplayDevices.size(); i++) {
2355 mDisplayDevices.get(i).onOverlayChangedLocked();
Adrian Roose1d68cd2018-01-17 12:54:50 +01002356 }
2357 }
2358 }
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07002359
2360 @Override
2361 public DisplayedContentSamplingAttributes getDisplayedContentSamplingAttributes(
2362 int displayId) {
2363 return getDisplayedContentSamplingAttributesInternal(displayId);
2364 }
2365
2366 @Override
2367 public boolean setDisplayedContentSamplingEnabled(
2368 int displayId, boolean enable, int componentMask, int maxFrames) {
2369 return setDisplayedContentSamplingEnabledInternal(
2370 displayId, enable, componentMask, maxFrames);
2371 }
2372
2373 @Override
2374 public DisplayedContentSample getDisplayedContentSample(int displayId,
2375 long maxFrames, long timestamp) {
2376 return getDisplayedContentSampleInternal(displayId, maxFrames, timestamp);
2377 }
2378
Jeff Brown4ccb8232014-01-16 22:16:42 -08002379 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07002380}