blob: 6c00da20b91ee490c597abade60a747f508b518b [file] [log] [blame]
Jeff Brownfa25bf52012-07-23 19:26:30 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.display;
18
Andrii Kulianfc8f82b2017-01-26 13:17:27 -080019import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +010020import static android.hardware.display.DisplayManager
21 .VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD;
Andrii Kulianfc8f82b2017-01-26 13:17:27 -080022import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY;
23import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;
24import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_SECURE;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +010025import static android.hardware.display.DisplayViewport.VIEWPORT_EXTERNAL;
26import static android.hardware.display.DisplayViewport.VIEWPORT_INTERNAL;
27import static android.hardware.display.DisplayViewport.VIEWPORT_VIRTUAL;
Jeff Brownbd6e1502012-08-28 03:27:37 -070028
Jeff Brownfa25bf52012-07-23 19:26:30 -070029import android.Manifest;
Santos Cordonee8931e2017-04-05 10:31:15 -070030import android.annotation.NonNull;
Kenny Guy05ce8092018-01-17 13:44:20 +000031import android.annotation.Nullable;
Michael Wrighteef0e132017-11-21 17:57:52 +000032import android.annotation.UserIdInt;
Kenny Guy29aa30e2017-11-30 13:43:46 +000033import android.app.AppOpsManager;
Jeff Brownfa25bf52012-07-23 19:26:30 -070034import android.content.Context;
35import android.content.pm.PackageManager;
Kenny Guy22bd0442017-10-26 00:15:54 +010036import android.content.pm.ParceledListSlice;
Michael Wrighteedcbf12017-08-16 23:14:54 +010037import android.content.res.Resources;
Dan Gittik122df862018-03-28 16:59:22 +010038import android.content.res.TypedArray;
Peiyong Lin277eaff2019-01-16 16:18:22 -080039import android.graphics.ColorSpace;
Robert Carr5c52b132019-02-15 15:48:11 -080040import android.graphics.GraphicBuffer;
Michael Wrighteedcbf12017-08-16 23:14:54 +010041import android.graphics.Point;
Robert Carr5c52b132019-02-15 15:48:11 -080042import android.graphics.Rect;
Jeff Brownad9ef192014-04-08 17:26:30 -070043import android.hardware.SensorManager;
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +000044import android.hardware.display.AmbientBrightnessDayStats;
Kenny Guy22bd0442017-10-26 00:15:54 +010045import android.hardware.display.BrightnessChangeEvent;
Michael Wrighteef0e132017-11-21 17:57:52 +000046import android.hardware.display.BrightnessConfiguration;
Dan Gittik122df862018-03-28 16:59:22 +010047import android.hardware.display.Curve;
Jeff Brownbd6e1502012-08-28 03:27:37 -070048import android.hardware.display.DisplayManagerGlobal;
Jeff Brown4ccb8232014-01-16 22:16:42 -080049import android.hardware.display.DisplayManagerInternal;
Jeff Brown4ccb8232014-01-16 22:16:42 -080050import android.hardware.display.DisplayManagerInternal.DisplayTransactionListener;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +010051import android.hardware.display.DisplayViewport;
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -070052import android.hardware.display.DisplayedContentSample;
53import android.hardware.display.DisplayedContentSamplingAttributes;
Jeff Brownfa25bf52012-07-23 19:26:30 -070054import android.hardware.display.IDisplayManager;
Jeff Brownbd6e1502012-08-28 03:27:37 -070055import android.hardware.display.IDisplayManagerCallback;
Michael Wright75ee9fc2014-09-01 19:55:22 -070056import android.hardware.display.IVirtualDisplayCallback;
Jeff Browne08ae382012-09-07 20:36:36 -070057import android.hardware.display.WifiDisplayStatus;
Jeff Brown4ccb8232014-01-16 22:16:42 -080058import android.hardware.input.InputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -070059import android.media.projection.IMediaProjection;
60import android.media.projection.IMediaProjectionManager;
Jeff Brownfa25bf52012-07-23 19:26:30 -070061import android.os.Binder;
Jeff Brownbd6e1502012-08-28 03:27:37 -070062import android.os.Handler;
Jeff Brown64a55af2012-08-26 02:47:39 -070063import android.os.IBinder;
Jeff Brown4ccb8232014-01-16 22:16:42 -080064import android.os.IBinder.DeathRecipient;
Jeff Brownbd6e1502012-08-28 03:27:37 -070065import android.os.Looper;
66import android.os.Message;
Jeff Brown5d6443b2015-04-10 20:15:01 -070067import android.os.PowerManager;
Craig Mautner4504de52013-12-20 09:06:56 -080068import android.os.Process;
Jeff Brownbd6e1502012-08-28 03:27:37 -070069import android.os.RemoteException;
Dan Gittik7a32fba2018-03-28 12:19:38 +010070import android.os.ResultReceiver;
Michael Wrightc39d47a2014-07-08 18:07:36 -070071import android.os.ServiceManager;
Dan Gittik7a32fba2018-03-28 12:19:38 +010072import android.os.ShellCallback;
Jeff Brownbd6e1502012-08-28 03:27:37 -070073import android.os.SystemClock;
Jeff Brownfa25bf52012-07-23 19:26:30 -070074import android.os.SystemProperties;
Jeff Brown5d6443b2015-04-10 20:15:01 -070075import android.os.Trace;
Kenny Guy22bd0442017-10-26 00:15:54 +010076import android.os.UserHandle;
Michael Wrighteef0e132017-11-21 17:57:52 +000077import android.os.UserManager;
Dan Gittik7a32fba2018-03-28 12:19:38 +010078import android.provider.Settings;
Jeff Browna506a6e2013-06-04 00:02:38 -070079import android.text.TextUtils;
Andrii Kulianfb1bf692017-01-17 11:17:34 -080080import android.util.IntArray;
Dan Gittik122df862018-03-28 16:59:22 +010081import android.util.Pair;
Jeff Brownbd6e1502012-08-28 03:27:37 -070082import android.util.Slog;
83import android.util.SparseArray;
Dan Gittik122df862018-03-28 16:59:22 +010084import android.util.Spline;
Jeff Brownfa25bf52012-07-23 19:26:30 -070085import android.view.Display;
86import android.view.DisplayInfo;
Jeff Browna506a6e2013-06-04 00:02:38 -070087import android.view.Surface;
Robert Carrae606b42018-02-15 15:36:23 -080088import android.view.SurfaceControl;
Jeff Browna506a6e2013-06-04 00:02:38 -070089
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +010090import com.android.internal.annotations.GuardedBy;
91import com.android.internal.annotations.VisibleForTesting;
92import com.android.internal.util.DumpUtils;
93import com.android.internal.util.IndentingPrintWriter;
Jorim Jaggied7993b2017-03-28 18:50:01 +010094import com.android.server.AnimationThread;
Jeff Brown4ccb8232014-01-16 22:16:42 -080095import com.android.server.DisplayThread;
96import com.android.server.LocalServices;
97import com.android.server.SystemService;
Dianne Hackborn8d044e82013-04-30 17:24:15 -070098import com.android.server.UiThread;
Jorim Jaggi21c39a72017-10-20 15:47:51 +020099import com.android.server.wm.SurfaceAnimationThread;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100100import com.android.server.wm.WindowManagerInternal;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700101
102import java.io.FileDescriptor;
103import java.io.PrintWriter;
104import java.util.ArrayList;
Jeff Browna506a6e2013-06-04 00:02:38 -0700105import java.util.Arrays;
Jeff Browne75926d2014-09-18 15:24:49 -0700106import java.util.List;
Jeff Brown7f3994e2012-12-04 14:04:28 -0800107import java.util.concurrent.CopyOnWriteArrayList;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700108
109/**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700110 * Manages attached displays.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700111 * <p>
Jeff Brownbd6e1502012-08-28 03:27:37 -0700112 * The {@link DisplayManagerService} manages the global lifecycle of displays,
113 * decides how to configure logical displays based on the physical display devices currently
114 * attached, sends notifications to the system and to applications when the state
115 * changes, and so on.
116 * </p><p>
117 * The display manager service relies on a collection of {@link DisplayAdapter} components,
118 * for discovering and configuring physical display devices attached to the system.
119 * There are separate display adapters for each manner that devices are attached:
120 * one display adapter for built-in local displays, one for simulated non-functional
121 * displays when the system is headless, one for simulated overlay displays used for
122 * development, one for wifi displays, etc.
123 * </p><p>
124 * Display adapters are only weakly coupled to the display manager service.
125 * Display adapters communicate changes in display device state to the display manager
Craig Mautner722285e2012-09-07 13:55:58 -0700126 * service asynchronously via a {@link DisplayAdapter.Listener} registered
Jeff Brownbd6e1502012-08-28 03:27:37 -0700127 * by the display manager service. This separation of concerns is important for
128 * two main reasons. First, it neatly encapsulates the responsibilities of these
129 * two classes: display adapters handle individual display devices whereas
130 * the display manager service handles the global state. Second, it eliminates
131 * the potential for deadlocks resulting from asynchronous display device discovery.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700132 * </p>
133 *
134 * <h3>Synchronization</h3>
135 * <p>
136 * Because the display manager may be accessed by multiple threads, the synchronization
137 * story gets a little complicated. In particular, the window manager may call into
138 * the display manager while holding a surface transaction with the expectation that
139 * it can apply changes immediately. Unfortunately, that means we can't just do
140 * everything asynchronously (*grump*).
Jeff Brownbd6e1502012-08-28 03:27:37 -0700141 * </p><p>
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700142 * To make this work, all of the objects that belong to the display manager must
143 * use the same lock. We call this lock the synchronization root and it has a unique
144 * type {@link DisplayManagerService.SyncRoot}. Methods that require this lock are
145 * named with the "Locked" suffix.
146 * </p><p>
147 * Where things get tricky is that the display manager is not allowed to make
148 * any potentially reentrant calls, especially into the window manager. We generally
149 * avoid this by making all potentially reentrant out-calls asynchronous.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700150 * </p>
151 */
Jeff Brown4ccb8232014-01-16 22:16:42 -0800152public final class DisplayManagerService extends SystemService {
Jeff Brownfa25bf52012-07-23 19:26:30 -0700153 private static final String TAG = "DisplayManagerService";
Jeff Brownbd6e1502012-08-28 03:27:37 -0700154 private static final boolean DEBUG = false;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700155
Jeff Brownbbd28a22012-09-20 16:47:15 -0700156 // When this system property is set to 0, WFD is forcibly disabled on boot.
157 // When this system property is set to 1, WFD is forcibly enabled on boot.
158 // Otherwise WFD is enabled according to the value of config_enableWifiDisplay.
159 private static final String FORCE_WIFI_DISPLAY_ENABLE = "persist.debug.wfd.enable";
160
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200161 private static final String PROP_DEFAULT_DISPLAY_TOP_INSET = "persist.sys.displayinset.top";
162
Jeff Brownbd6e1502012-08-28 03:27:37 -0700163 private static final long WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT = 10000;
164
Santos Cordonc22c5632017-06-21 16:03:49 -0700165 private static final int MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS = 1;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700166 private static final int MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS = 2;
167 private static final int MSG_DELIVER_DISPLAY_EVENT = 3;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700168 private static final int MSG_REQUEST_TRAVERSAL = 4;
Jeff Brownd728bf52012-09-08 18:05:28 -0700169 private static final int MSG_UPDATE_VIEWPORT = 5;
Kenny Guy22bd0442017-10-26 00:15:54 +0100170 private static final int MSG_REGISTER_BRIGHTNESS_TRACKER = 6;
Michael Wrighteef0e132017-11-21 17:57:52 +0000171 private static final int MSG_LOAD_BRIGHTNESS_CONFIGURATION = 7;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700172
Jeff Brownb880d882014-02-10 19:47:07 -0800173 private final Context mContext;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700174 private final DisplayManagerHandler mHandler;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700175 private final Handler mUiHandler;
176 private final DisplayAdapterListener mDisplayAdapterListener;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800177 private WindowManagerInternal mWindowManagerInternal;
178 private InputManagerInternal mInputManagerInternal;
Michael Wrightc39d47a2014-07-08 18:07:36 -0700179 private IMediaProjectionManager mProjectionService;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700180
181 // The synchronization root for the display manager.
182 // This lock guards most of the display manager's state.
Craig Mautner0bf6ec92012-12-18 08:33:27 -0800183 // NOTE: This is synchronized on while holding WindowManagerService.mWindowMap so never call
184 // into WindowManagerService methods that require mWindowMap while holding this unless you are
185 // very very sure that no deadlock can occur.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700186 private final SyncRoot mSyncRoot = new SyncRoot();
187
188 // True if in safe mode.
189 // This option may disable certain display adapters.
190 public boolean mSafeMode;
191
192 // True if we are in a special boot mode where only core applications and
193 // services should be started. This option may disable certain display adapters.
194 public boolean mOnlyCore;
195
Jeff Brown27f1d672012-10-17 18:32:34 -0700196 // True if the display manager service should pretend there is only one display
197 // and only tell applications about the existence of the default logical display.
198 // The display manager can still mirror content to secondary displays but applications
199 // cannot present unique content on those displays.
200 // Used for demonstration purposes only.
201 private final boolean mSingleDisplayDemoMode;
202
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700203 // All callback records indexed by calling process id.
204 public final SparseArray<CallbackRecord> mCallbacks =
Jeff Brownbd6e1502012-08-28 03:27:37 -0700205 new SparseArray<CallbackRecord>();
Jeff Brownfa25bf52012-07-23 19:26:30 -0700206
Jeff Brownbd6e1502012-08-28 03:27:37 -0700207 // List of all currently registered display adapters.
208 private final ArrayList<DisplayAdapter> mDisplayAdapters = new ArrayList<DisplayAdapter>();
209
210 // List of all currently connected display devices.
211 private final ArrayList<DisplayDevice> mDisplayDevices = new ArrayList<DisplayDevice>();
212
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700213 // List of all logical displays indexed by logical display id.
214 private final SparseArray<LogicalDisplay> mLogicalDisplays =
215 new SparseArray<LogicalDisplay>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700216 private int mNextNonDefaultDisplayId = Display.DEFAULT_DISPLAY + 1;
217
Jeff Brown7f3994e2012-12-04 14:04:28 -0800218 // List of all display transaction listeners.
219 private final CopyOnWriteArrayList<DisplayTransactionListener> mDisplayTransactionListeners =
220 new CopyOnWriteArrayList<DisplayTransactionListener>();
221
Jeff Brownad9ef192014-04-08 17:26:30 -0700222 // Display power controller.
223 private DisplayPowerController mDisplayPowerController;
224
Jeff Brown037c33e2014-04-09 00:31:55 -0700225 // The overall display state, independent of changes that might influence one
226 // display or another in particular.
Jeff Brown5d6443b2015-04-10 20:15:01 -0700227 private int mGlobalDisplayState = Display.STATE_ON;
228
229 // The overall display brightness.
230 // For now, this only applies to the built-in display but we may split it up eventually.
231 private int mGlobalDisplayBrightness = PowerManager.BRIGHTNESS_DEFAULT;
Jeff Brown9e316a12012-10-08 19:17:06 -0700232
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700233 // Set to true when there are pending display changes that have yet to be applied
234 // to the surface flinger state.
235 private boolean mPendingTraversal;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700236
Jeff Browne08ae382012-09-07 20:36:36 -0700237 // The Wifi display adapter, or null if not registered.
238 private WifiDisplayAdapter mWifiDisplayAdapter;
239
Jeff Brownce468a32013-11-21 16:42:03 -0800240 // The number of active wifi display scan requests.
241 private int mWifiDisplayScanRequestCount;
242
Jeff Browna506a6e2013-06-04 00:02:38 -0700243 // The virtual display adapter, or null if not registered.
244 private VirtualDisplayAdapter mVirtualDisplayAdapter;
245
Michael Wrighteef0e132017-11-21 17:57:52 +0000246 // The User ID of the current user
247 private @UserIdInt int mCurrentUserId;
248
Michael Wrighteedcbf12017-08-16 23:14:54 +0100249 // The stable device screen height and width. These are not tied to a specific display, even
250 // the default display, because they need to be stable over the course of the device's entire
251 // life, even if the default display changes (e.g. a new monitor is plugged into a PC-like
252 // device).
253 private Point mStableDisplaySize = new Point();
254
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200255 // The top inset of the default display.
256 // This gets persisted so that the boot animation knows how to transition from the display's
257 // full size to the size configured by the user. Right now we only persist and animate the top
258 // inset, but theoretically we could do it for all of them.
259 private int mDefaultDisplayTopInset;
260
Jeff Brownd728bf52012-09-08 18:05:28 -0700261 // Viewports of the default display and the display that should receive touch
262 // input from an external source. Used by the input system.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100263 @GuardedBy("mSyncRoot")
264 private final ArrayList<DisplayViewport> mViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700265
Jeff Brown89d55462012-09-19 11:33:42 -0700266 // Persistent data store for all internal settings maintained by the display manager service.
267 private final PersistentDataStore mPersistentDataStore = new PersistentDataStore();
268
Jeff Brownbd6e1502012-08-28 03:27:37 -0700269 // Temporary callback list, used when sending display events to applications.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700270 // May be used outside of the lock but only on the handler thread.
271 private final ArrayList<CallbackRecord> mTempCallbacks = new ArrayList<CallbackRecord>();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700272
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700273 // Temporary display info, used for comparing display configurations.
274 private final DisplayInfo mTempDisplayInfo = new DisplayInfo();
275
Jeff Brownd728bf52012-09-08 18:05:28 -0700276 // Temporary viewports, used when sending new viewport information to the
277 // input system. May be used outside of the lock but only on the handler thread.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100278 private final ArrayList<DisplayViewport> mTempViewports = new ArrayList<>();
Jeff Brownd728bf52012-09-08 18:05:28 -0700279
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700280 // The default color mode for default displays. Overrides the usual
281 // Display.Display.COLOR_MODE_DEFAULT for displays with the
282 // DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY flag set.
283 private final int mDefaultDisplayDefaultColorMode;
284
Jeff Browne75926d2014-09-18 15:24:49 -0700285 // Temporary list of deferred work to perform when setting the display state.
286 // Only used by requestDisplayState. The field is self-synchronized and only
287 // intended for use inside of the requestGlobalDisplayStateInternal function.
288 private final ArrayList<Runnable> mTempDisplayStateWorkQueue = new ArrayList<Runnable>();
289
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800290 // Lists of UIDs that are present on the displays. Maps displayId -> array of UIDs.
291 private final SparseArray<IntArray> mDisplayAccessUIDs = new SparseArray<>();
292
Santos Cordonee8931e2017-04-05 10:31:15 -0700293 private final Injector mInjector;
294
Dan Gittik122df862018-03-28 16:59:22 +0100295 // The minimum brightness curve, which guarantess that any brightness curve that dips below it
296 // is rejected by the system.
297 private final Curve mMinimumBrightnessCurve;
298 private final Spline mMinimumBrightnessSpline;
Peiyong Lin277eaff2019-01-16 16:18:22 -0800299 private final ColorSpace mWideColorSpace;
Dan Gittik122df862018-03-28 16:59:22 +0100300
Jeff Brownb880d882014-02-10 19:47:07 -0800301 public DisplayManagerService(Context context) {
Santos Cordonee8931e2017-04-05 10:31:15 -0700302 this(context, new Injector());
303 }
304
305 @VisibleForTesting
306 DisplayManagerService(Context context, Injector injector) {
Jeff Brownb880d882014-02-10 19:47:07 -0800307 super(context);
Santos Cordonee8931e2017-04-05 10:31:15 -0700308 mInjector = injector;
Jeff Brownb880d882014-02-10 19:47:07 -0800309 mContext = context;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800310 mHandler = new DisplayManagerHandler(DisplayThread.get().getLooper());
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700311 mUiHandler = UiThread.getHandler();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700312 mDisplayAdapterListener = new DisplayAdapterListener();
Jeff Brown27f1d672012-10-17 18:32:34 -0700313 mSingleDisplayDemoMode = SystemProperties.getBoolean("persist.demo.singledisplay", false);
Dan Gittik122df862018-03-28 16:59:22 +0100314 Resources resources = mContext.getResources();
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -0700315 mDefaultDisplayDefaultColorMode = mContext.getResources().getInteger(
Michael Wrighteef0e132017-11-21 17:57:52 +0000316 com.android.internal.R.integer.config_defaultDisplayDefaultColorMode);
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200317 mDefaultDisplayTopInset = SystemProperties.getInt(PROP_DEFAULT_DISPLAY_TOP_INSET, -1);
Dan Gittik122df862018-03-28 16:59:22 +0100318 float[] lux = getFloatArray(resources.obtainTypedArray(
319 com.android.internal.R.array.config_minimumBrightnessCurveLux));
320 float[] nits = getFloatArray(resources.obtainTypedArray(
321 com.android.internal.R.array.config_minimumBrightnessCurveNits));
322 mMinimumBrightnessCurve = new Curve(lux, nits);
323 mMinimumBrightnessSpline = Spline.createSpline(lux, nits);
Jeff Brown5d6443b2015-04-10 20:15:01 -0700324
325 PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
326 mGlobalDisplayBrightness = pm.getDefaultScreenBrightnessSetting();
Michael Wrighteef0e132017-11-21 17:57:52 +0000327 mCurrentUserId = UserHandle.USER_SYSTEM;
Peiyong Lin277eaff2019-01-16 16:18:22 -0800328 ColorSpace[] colorSpaces = SurfaceControl.getCompositionColorSpaces();
329 mWideColorSpace = colorSpaces[1];
Joel Fernandes2d314e12017-04-04 16:32:15 -0700330 }
331
332 public void setupSchedulerPolicies() {
Jorim Jaggied7993b2017-03-28 18:50:01 +0100333 // android.display and android.anim is critical to user experience and we should make sure
Michael Wrighteef0e132017-11-21 17:57:52 +0000334 // it is not in the default foregroup groups, add it to top-app to make sure it uses all
335 // the cores and scheduling settings for top-app when it runs.
Jorim Jaggied7993b2017-03-28 18:50:01 +0100336 Process.setThreadGroupAndCpuset(DisplayThread.get().getThreadId(),
337 Process.THREAD_GROUP_TOP_APP);
338 Process.setThreadGroupAndCpuset(AnimationThread.get().getThreadId(),
339 Process.THREAD_GROUP_TOP_APP);
Jorim Jaggi21c39a72017-10-20 15:47:51 +0200340 Process.setThreadGroupAndCpuset(SurfaceAnimationThread.get().getThreadId(),
341 Process.THREAD_GROUP_TOP_APP);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700342 }
343
Jeff Brown4ccb8232014-01-16 22:16:42 -0800344 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -0800345 public void onStart() {
Michael Wright1c9977b2016-07-12 13:30:10 -0700346 // We need to pre-load the persistent data store so it's ready before the default display
347 // adapter is up so that we have it's configuration. We could load it lazily, but since
348 // 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 -0700349 // we've waited for the display to register itself with us.
Michael Wrighteedcbf12017-08-16 23:14:54 +0100350 synchronized(mSyncRoot) {
351 mPersistentDataStore.loadIfNeeded();
352 loadStableDisplayValuesLocked();
353 }
Santos Cordonc22c5632017-06-21 16:03:49 -0700354 mHandler.sendEmptyMessage(MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800355
356 publishBinderService(Context.DISPLAY_SERVICE, new BinderService(),
357 true /*allowIsolated*/);
358 publishLocalService(DisplayManagerInternal.class, new LocalService());
359 }
360
361 @Override
362 public void onBootPhase(int phase) {
363 if (phase == PHASE_WAIT_FOR_DEFAULT_DISPLAY) {
364 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700365 long timeout = SystemClock.uptimeMillis()
366 + mInjector.getDefaultDisplayDelayTimeout();
367 while (mLogicalDisplays.get(Display.DEFAULT_DISPLAY) == null ||
368 mVirtualDisplayAdapter == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800369 long delay = timeout - SystemClock.uptimeMillis();
370 if (delay <= 0) {
371 throw new RuntimeException("Timeout waiting for default display "
Santos Cordonc22c5632017-06-21 16:03:49 -0700372 + "to be initialized. DefaultDisplay="
373 + mLogicalDisplays.get(Display.DEFAULT_DISPLAY)
374 + ", mVirtualDisplayAdapter=" + mVirtualDisplayAdapter);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800375 }
376 if (DEBUG) {
377 Slog.d(TAG, "waitForDefaultDisplay: waiting, timeout=" + delay);
378 }
379 try {
380 mSyncRoot.wait(delay);
381 } catch (InterruptedException ex) {
382 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700383 }
384 }
385 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700386 }
387
Michael Wrighteef0e132017-11-21 17:57:52 +0000388 @Override
389 public void onSwitchUser(@UserIdInt int newUserId) {
390 final int userSerial = getUserManager().getUserSerialNumber(newUserId);
391 synchronized (mSyncRoot) {
392 if (mCurrentUserId != newUserId) {
393 mCurrentUserId = newUserId;
394 BrightnessConfiguration config =
395 mPersistentDataStore.getBrightnessConfiguration(userSerial);
396 mDisplayPowerController.setBrightnessConfiguration(config);
397 }
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +0000398 mDisplayPowerController.onSwitchUser(newUserId);
Michael Wrighteef0e132017-11-21 17:57:52 +0000399 }
400 }
401
Jeff Brown4ccb8232014-01-16 22:16:42 -0800402 // TODO: Use dependencies or a boot phase
403 public void windowManagerAndInputReady() {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700404 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800405 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
406 mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
Craig Mautner65d11b32012-10-01 13:59:52 -0700407 scheduleTraversalLocked(false);
Jeff Brownd728bf52012-09-08 18:05:28 -0700408 }
409 }
410
411 /**
Jeff Brownbd6e1502012-08-28 03:27:37 -0700412 * Called when the system is ready to go.
413 */
414 public void systemReady(boolean safeMode, boolean onlyCore) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700415 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700416 mSafeMode = safeMode;
417 mOnlyCore = onlyCore;
418 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700419
Jeff Brownbd6e1502012-08-28 03:27:37 -0700420 mHandler.sendEmptyMessage(MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS);
Kenny Guy22bd0442017-10-26 00:15:54 +0100421 mHandler.sendEmptyMessage(MSG_REGISTER_BRIGHTNESS_TRACKER);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700422 }
423
Santos Cordonee8931e2017-04-05 10:31:15 -0700424 @VisibleForTesting
425 Handler getDisplayHandler() {
426 return mHandler;
427 }
428
Michael Wrighteedcbf12017-08-16 23:14:54 +0100429 private void loadStableDisplayValuesLocked() {
430 final Point size = mPersistentDataStore.getStableDisplaySize();
431 if (size.x > 0 && size.y > 0) {
432 // Just set these values directly so we don't write the display persistent data again
433 // unnecessarily
434 mStableDisplaySize.set(size.x, size.y);
435 } else {
436 final Resources res = mContext.getResources();
437 final int width = res.getInteger(
438 com.android.internal.R.integer.config_stableDeviceDisplayWidth);
439 final int height = res.getInteger(
440 com.android.internal.R.integer.config_stableDeviceDisplayHeight);
441 if (width > 0 && height > 0) {
442 setStableDisplaySizeLocked(width, height);
443 }
444 }
445 }
446
447 private Point getStableDisplaySizeInternal() {
448 Point r = new Point();
449 synchronized (mSyncRoot) {
450 if (mStableDisplaySize.x > 0 && mStableDisplaySize.y > 0) {
451 r.set(mStableDisplaySize.x, mStableDisplaySize.y);
452 }
453 }
454 return r;
455 }
456
Jeff Brown4ccb8232014-01-16 22:16:42 -0800457 private void registerDisplayTransactionListenerInternal(
458 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800459 // List is self-synchronized copy-on-write.
460 mDisplayTransactionListeners.add(listener);
461 }
462
Jeff Brown4ccb8232014-01-16 22:16:42 -0800463 private void unregisterDisplayTransactionListenerInternal(
464 DisplayTransactionListener listener) {
Jeff Brown7f3994e2012-12-04 14:04:28 -0800465 // List is self-synchronized copy-on-write.
466 mDisplayTransactionListeners.remove(listener);
467 }
468
Jeff Brown4ccb8232014-01-16 22:16:42 -0800469 private void setDisplayInfoOverrideFromWindowManagerInternal(
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700470 int displayId, DisplayInfo info) {
471 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700472 LogicalDisplay display = mLogicalDisplays.get(displayId);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700473 if (display != null) {
Jeff Brownef981a42013-08-07 14:13:37 -0700474 if (display.setDisplayInfoOverrideFromWindowManagerLocked(info)) {
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200475 handleLogicalDisplayChanged(displayId, display);
Craig Mautner65d11b32012-10-01 13:59:52 -0700476 scheduleTraversalLocked(false);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700477 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700478 }
479 }
480 }
481
Andrii Kuliancd097992017-03-23 18:31:59 -0700482 /**
483 * @see DisplayManagerInternal#getNonOverrideDisplayInfo(int, DisplayInfo)
484 */
485 private void getNonOverrideDisplayInfoInternal(int displayId, DisplayInfo outInfo) {
486 synchronized (mSyncRoot) {
487 final LogicalDisplay display = mLogicalDisplays.get(displayId);
488 if (display != null) {
489 display.getNonOverrideDisplayInfoLocked(outInfo);
490 }
491 }
492 }
493
Santos Cordonee8931e2017-04-05 10:31:15 -0700494 @VisibleForTesting
Robert Carrae606b42018-02-15 15:36:23 -0800495 void performTraversalInternal(SurfaceControl.Transaction t) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700496 synchronized (mSyncRoot) {
497 if (!mPendingTraversal) {
498 return;
Jeff Brownbd6e1502012-08-28 03:27:37 -0700499 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700500 mPendingTraversal = false;
501
Robert Carrae606b42018-02-15 15:36:23 -0800502 performTraversalLocked(t);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700503 }
Jeff Brown7f3994e2012-12-04 14:04:28 -0800504
505 // List is self-synchronized copy-on-write.
506 for (DisplayTransactionListener listener : mDisplayTransactionListeners) {
Vishnu Naire86bd982018-11-28 13:23:17 -0800507 listener.onDisplayTransaction(t);
Jeff Brown7f3994e2012-12-04 14:04:28 -0800508 }
Jeff Brownbd6e1502012-08-28 03:27:37 -0700509 }
510
Jeff Brown5d6443b2015-04-10 20:15:01 -0700511 private void requestGlobalDisplayStateInternal(int state, int brightness) {
512 if (state == Display.STATE_UNKNOWN) {
513 state = Display.STATE_ON;
514 }
515 if (state == Display.STATE_OFF) {
516 brightness = PowerManager.BRIGHTNESS_OFF;
517 } else if (brightness < 0) {
518 brightness = PowerManager.BRIGHTNESS_DEFAULT;
519 } else if (brightness > PowerManager.BRIGHTNESS_ON) {
520 brightness = PowerManager.BRIGHTNESS_ON;
521 }
522
Jeff Browne75926d2014-09-18 15:24:49 -0700523 synchronized (mTempDisplayStateWorkQueue) {
524 try {
525 // Update the display state within the lock.
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700526 // Note that we do not need to schedule traversals here although it
527 // may happen as a side-effect of displays changing state.
Jeff Browne75926d2014-09-18 15:24:49 -0700528 synchronized (mSyncRoot) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700529 if (mGlobalDisplayState == state
530 && mGlobalDisplayBrightness == brightness) {
531 return; // no change
Jeff Browne75926d2014-09-18 15:24:49 -0700532 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700533
534 Trace.traceBegin(Trace.TRACE_TAG_POWER, "requestGlobalDisplayState("
535 + Display.stateToString(state)
536 + ", brightness=" + brightness + ")");
537 mGlobalDisplayState = state;
538 mGlobalDisplayBrightness = brightness;
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700539 applyGlobalDisplayStateLocked(mTempDisplayStateWorkQueue);
Jeff Browne75926d2014-09-18 15:24:49 -0700540 }
541
542 // Setting the display power state can take hundreds of milliseconds
543 // to complete so we defer the most expensive part of the work until
544 // after we have exited the critical section to avoid blocking other
545 // threads for a long time.
546 for (int i = 0; i < mTempDisplayStateWorkQueue.size(); i++) {
547 mTempDisplayStateWorkQueue.get(i).run();
548 }
Jeff Brown5d6443b2015-04-10 20:15:01 -0700549 Trace.traceEnd(Trace.TRACE_TAG_POWER);
Jeff Browne75926d2014-09-18 15:24:49 -0700550 } finally {
551 mTempDisplayStateWorkQueue.clear();
Jeff Brown9e316a12012-10-08 19:17:06 -0700552 }
553 }
554 }
555
Jeff Brown4ccb8232014-01-16 22:16:42 -0800556 private DisplayInfo getDisplayInfoInternal(int displayId, int callingUid) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700557 synchronized (mSyncRoot) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800558 LogicalDisplay display = mLogicalDisplays.get(displayId);
559 if (display != null) {
560 DisplayInfo info = display.getDisplayInfoLocked();
Andrii Kulianfb1bf692017-01-17 11:17:34 -0800561 if (info.hasAccess(callingUid)
562 || isUidPresentOnDisplayInternal(callingUid, displayId)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800563 return info;
564 }
565 }
566 return null;
567 }
568 }
569
570 private int[] getDisplayIdsInternal(int callingUid) {
571 synchronized (mSyncRoot) {
572 final int count = mLogicalDisplays.size();
573 int[] displayIds = new int[count];
574 int n = 0;
575 for (int i = 0; i < count; i++) {
576 LogicalDisplay display = mLogicalDisplays.valueAt(i);
577 DisplayInfo info = display.getDisplayInfoLocked();
578 if (info.hasAccess(callingUid)) {
579 displayIds[n++] = mLogicalDisplays.keyAt(i);
580 }
581 }
582 if (n != count) {
583 displayIds = Arrays.copyOfRange(displayIds, 0, n);
584 }
585 return displayIds;
586 }
587 }
588
589 private void registerCallbackInternal(IDisplayManagerCallback callback, int callingPid) {
590 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -0700591 if (mCallbacks.get(callingPid) != null) {
592 throw new SecurityException("The calling process has already "
593 + "registered an IDisplayManagerCallback.");
Jeff Brown64a55af2012-08-26 02:47:39 -0700594 }
595
Jeff Brownbd6e1502012-08-28 03:27:37 -0700596 CallbackRecord record = new CallbackRecord(callingPid, callback);
597 try {
598 IBinder binder = callback.asBinder();
599 binder.linkToDeath(record, 0);
600 } catch (RemoteException ex) {
601 // give up
602 throw new RuntimeException(ex);
603 }
604
605 mCallbacks.put(callingPid, record);
606 }
607 }
608
Jeff Brownce468a32013-11-21 16:42:03 -0800609 private void onCallbackDied(CallbackRecord record) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700610 synchronized (mSyncRoot) {
Jeff Brownce468a32013-11-21 16:42:03 -0800611 mCallbacks.remove(record.mPid);
612 stopWifiDisplayScanLocked(record);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700613 }
614 }
615
Jeff Brown4ccb8232014-01-16 22:16:42 -0800616 private void startWifiDisplayScanInternal(int callingPid) {
617 synchronized (mSyncRoot) {
618 CallbackRecord record = mCallbacks.get(callingPid);
619 if (record == null) {
620 throw new IllegalStateException("The calling process has not "
621 + "registered an IDisplayManagerCallback.");
Jeff Browne08ae382012-09-07 20:36:36 -0700622 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800623 startWifiDisplayScanLocked(record);
Jeff Browne08ae382012-09-07 20:36:36 -0700624 }
625 }
626
Jeff Brownce468a32013-11-21 16:42:03 -0800627 private void startWifiDisplayScanLocked(CallbackRecord record) {
628 if (!record.mWifiDisplayScanRequested) {
629 record.mWifiDisplayScanRequested = true;
630 if (mWifiDisplayScanRequestCount++ == 0) {
631 if (mWifiDisplayAdapter != null) {
632 mWifiDisplayAdapter.requestStartScanLocked();
633 }
634 }
635 }
636 }
637
Jeff Brown4ccb8232014-01-16 22:16:42 -0800638 private void stopWifiDisplayScanInternal(int callingPid) {
639 synchronized (mSyncRoot) {
640 CallbackRecord record = mCallbacks.get(callingPid);
641 if (record == null) {
642 throw new IllegalStateException("The calling process has not "
643 + "registered an IDisplayManagerCallback.");
Jeff Brownce468a32013-11-21 16:42:03 -0800644 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800645 stopWifiDisplayScanLocked(record);
Jeff Brownce468a32013-11-21 16:42:03 -0800646 }
647 }
648
649 private void stopWifiDisplayScanLocked(CallbackRecord record) {
650 if (record.mWifiDisplayScanRequested) {
651 record.mWifiDisplayScanRequested = false;
652 if (--mWifiDisplayScanRequestCount == 0) {
653 if (mWifiDisplayAdapter != null) {
654 mWifiDisplayAdapter.requestStopScanLocked();
655 }
656 } else if (mWifiDisplayScanRequestCount < 0) {
Dianne Hackborn8d051722014-10-01 14:59:58 -0700657 Slog.wtf(TAG, "mWifiDisplayScanRequestCount became negative: "
Jeff Brownce468a32013-11-21 16:42:03 -0800658 + mWifiDisplayScanRequestCount);
659 mWifiDisplayScanRequestCount = 0;
660 }
661 }
662 }
663
Jeff Brown4ccb8232014-01-16 22:16:42 -0800664 private void connectWifiDisplayInternal(String address) {
665 synchronized (mSyncRoot) {
666 if (mWifiDisplayAdapter != null) {
667 mWifiDisplayAdapter.requestConnectLocked(address);
Jeff Browne08ae382012-09-07 20:36:36 -0700668 }
Jeff Browne08ae382012-09-07 20:36:36 -0700669 }
670 }
671
Jeff Brown4ccb8232014-01-16 22:16:42 -0800672 private void pauseWifiDisplayInternal() {
673 synchronized (mSyncRoot) {
674 if (mWifiDisplayAdapter != null) {
675 mWifiDisplayAdapter.requestPauseLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700676 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700677 }
678 }
679
Jeff Brown4ccb8232014-01-16 22:16:42 -0800680 private void resumeWifiDisplayInternal() {
681 synchronized (mSyncRoot) {
682 if (mWifiDisplayAdapter != null) {
683 mWifiDisplayAdapter.requestResumeLocked();
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700684 }
Chong Zhang1f3ecaa2013-05-03 15:55:36 -0700685 }
686 }
687
Jeff Brown4ccb8232014-01-16 22:16:42 -0800688 private void disconnectWifiDisplayInternal() {
689 synchronized (mSyncRoot) {
690 if (mWifiDisplayAdapter != null) {
691 mWifiDisplayAdapter.requestDisconnectLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700692 }
Jeff Browne08ae382012-09-07 20:36:36 -0700693 }
694 }
695
Jeff Brown4ccb8232014-01-16 22:16:42 -0800696 private void renameWifiDisplayInternal(String address, String alias) {
697 synchronized (mSyncRoot) {
698 if (mWifiDisplayAdapter != null) {
699 mWifiDisplayAdapter.requestRenameLocked(address, alias);
Jeff Brown89d55462012-09-19 11:33:42 -0700700 }
Jeff Brown89d55462012-09-19 11:33:42 -0700701 }
702 }
703
Jeff Brown4ccb8232014-01-16 22:16:42 -0800704 private void forgetWifiDisplayInternal(String address) {
705 synchronized (mSyncRoot) {
706 if (mWifiDisplayAdapter != null) {
707 mWifiDisplayAdapter.requestForgetLocked(address);
Jeff Brown89d55462012-09-19 11:33:42 -0700708 }
Jeff Brown89d55462012-09-19 11:33:42 -0700709 }
710 }
711
Jeff Brown4ccb8232014-01-16 22:16:42 -0800712 private WifiDisplayStatus getWifiDisplayStatusInternal() {
713 synchronized (mSyncRoot) {
714 if (mWifiDisplayAdapter != null) {
715 return mWifiDisplayAdapter.getWifiDisplayStatusLocked();
Jeff Browne08ae382012-09-07 20:36:36 -0700716 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800717 return new WifiDisplayStatus();
Jeff Browne08ae382012-09-07 20:36:36 -0700718 }
719 }
720
Michael Wright1c9977b2016-07-12 13:30:10 -0700721 private void requestColorModeInternal(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +0100722 synchronized (mSyncRoot) {
723 LogicalDisplay display = mLogicalDisplays.get(displayId);
724 if (display != null &&
Michael Wright1c9977b2016-07-12 13:30:10 -0700725 display.getRequestedColorModeLocked() != colorMode) {
726 display.setRequestedColorModeLocked(colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +0100727 scheduleTraversalLocked(false);
728 }
729 }
730 }
731
Michael Wright75ee9fc2014-09-01 19:55:22 -0700732 private int createVirtualDisplayInternal(IVirtualDisplayCallback callback,
Santos Cordonee8931e2017-04-05 10:31:15 -0700733 IMediaProjection projection, int callingUid, String packageName, String name, int width,
734 int height, int densityDpi, Surface surface, int flags, String uniqueId) {
Jeff Brown4ccb8232014-01-16 22:16:42 -0800735 synchronized (mSyncRoot) {
736 if (mVirtualDisplayAdapter == null) {
737 Slog.w(TAG, "Rejecting request to create private virtual display "
738 + "because the virtual display adapter is not available.");
739 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700740 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800741
742 DisplayDevice device = mVirtualDisplayAdapter.createVirtualDisplayLocked(
Santos Cordonee8931e2017-04-05 10:31:15 -0700743 callback, projection, callingUid, packageName, name, width, height, densityDpi,
744 surface, flags, uniqueId);
Jeff Brown4ccb8232014-01-16 22:16:42 -0800745 if (device == null) {
746 return -1;
Jeff Brown7d00aff2013-08-02 19:03:49 -0700747 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700748
Jeff Brown4ccb8232014-01-16 22:16:42 -0800749 handleDisplayDeviceAddedLocked(device);
750 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
751 if (display != null) {
752 return display.getDisplayIdLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700753 }
Jeff Brown4ccb8232014-01-16 22:16:42 -0800754
755 // Something weird happened and the logical display was not created.
756 Slog.w(TAG, "Rejecting request to create virtual display "
757 + "because the logical display was not created.");
Michael Wright75ee9fc2014-09-01 19:55:22 -0700758 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800759 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700760 }
761 return -1;
762 }
763
Michael Wright01e840f2014-06-26 16:03:25 -0700764 private void resizeVirtualDisplayInternal(IBinder appToken,
765 int width, int height, int densityDpi) {
766 synchronized (mSyncRoot) {
767 if (mVirtualDisplayAdapter == null) {
768 return;
769 }
770
771 mVirtualDisplayAdapter.resizeVirtualDisplayLocked(appToken, width, height, densityDpi);
772 }
773 }
774
Jeff Brown92207df2014-04-16 13:16:07 -0700775 private void setVirtualDisplaySurfaceInternal(IBinder appToken, Surface surface) {
776 synchronized (mSyncRoot) {
777 if (mVirtualDisplayAdapter == null) {
778 return;
779 }
780
781 mVirtualDisplayAdapter.setVirtualDisplaySurfaceLocked(appToken, surface);
782 }
783 }
784
Jeff Brown4ccb8232014-01-16 22:16:42 -0800785 private void releaseVirtualDisplayInternal(IBinder appToken) {
786 synchronized (mSyncRoot) {
787 if (mVirtualDisplayAdapter == null) {
788 return;
Jeff Browna506a6e2013-06-04 00:02:38 -0700789 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700790
Jeff Brown4ccb8232014-01-16 22:16:42 -0800791 DisplayDevice device =
792 mVirtualDisplayAdapter.releaseVirtualDisplayLocked(appToken);
793 if (device != null) {
794 handleDisplayDeviceRemovedLocked(device);
Jeff Browna506a6e2013-06-04 00:02:38 -0700795 }
796 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700797 }
798
chaviwda4c6942018-11-07 15:52:56 -0800799 private void setVirtualDisplayStateInternal(IBinder appToken, boolean isOn) {
800 synchronized (mSyncRoot) {
801 if (mVirtualDisplayAdapter == null) {
802 return;
803 }
804
805 mVirtualDisplayAdapter.setVirtualDisplayStateLocked(appToken, isOn);
806 }
807 }
808
Santos Cordonc22c5632017-06-21 16:03:49 -0700809 private void registerDefaultDisplayAdapters() {
810 // Register default display adapters.
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700811 synchronized (mSyncRoot) {
Santos Cordonc22c5632017-06-21 16:03:49 -0700812 // main display adapter
Mike Lockwoode63f6f72013-11-15 11:01:47 -0800813 registerDisplayAdapterLocked(new LocalDisplayAdapter(
814 mSyncRoot, mContext, mHandler, mDisplayAdapterListener));
Santos Cordonc22c5632017-06-21 16:03:49 -0700815
816 // Standalone VR devices rely on a virtual display as their primary display for
817 // 2D UI. We register virtual display adapter along side the main display adapter
818 // here so that it is ready by the time the system sends the home Intent for
819 // early apps like SetupWizard/Launcher. In particular, SUW is displayed using
820 // the virtual display inside VR before any VR-specific apps even run.
821 mVirtualDisplayAdapter = mInjector.getVirtualDisplayAdapter(mSyncRoot, mContext,
822 mHandler, mDisplayAdapterListener);
823 if (mVirtualDisplayAdapter != null) {
824 registerDisplayAdapterLocked(mVirtualDisplayAdapter);
825 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700826 }
827 }
828
829 private void registerAdditionalDisplayAdapters() {
830 synchronized (mSyncRoot) {
831 if (shouldRegisterNonEssentialDisplayAdaptersLocked()) {
Jeff Brown89d55462012-09-19 11:33:42 -0700832 registerOverlayDisplayAdapterLocked();
833 registerWifiDisplayAdapterLocked();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700834 }
835 }
836 }
837
Jeff Brown89d55462012-09-19 11:33:42 -0700838 private void registerOverlayDisplayAdapterLocked() {
839 registerDisplayAdapterLocked(new OverlayDisplayAdapter(
840 mSyncRoot, mContext, mHandler, mDisplayAdapterListener, mUiHandler));
841 }
842
843 private void registerWifiDisplayAdapterLocked() {
844 if (mContext.getResources().getBoolean(
Jeff Brownbbd28a22012-09-20 16:47:15 -0700845 com.android.internal.R.bool.config_enableWifiDisplay)
846 || SystemProperties.getInt(FORCE_WIFI_DISPLAY_ENABLE, -1) == 1) {
Jeff Brown89d55462012-09-19 11:33:42 -0700847 mWifiDisplayAdapter = new WifiDisplayAdapter(
848 mSyncRoot, mContext, mHandler, mDisplayAdapterListener,
849 mPersistentDataStore);
850 registerDisplayAdapterLocked(mWifiDisplayAdapter);
851 }
852 }
853
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700854 private boolean shouldRegisterNonEssentialDisplayAdaptersLocked() {
855 // In safe mode, we disable non-essential display adapters to give the user
856 // an opportunity to fix broken settings or other problems that might affect
857 // system stability.
858 // In only-core mode, we disable non-essential display adapters to minimize
859 // the number of dependencies that are started while in this mode and to
860 // prevent problems that might occur due to the device being encrypted.
861 return !mSafeMode && !mOnlyCore;
862 }
863
864 private void registerDisplayAdapterLocked(DisplayAdapter adapter) {
865 mDisplayAdapters.add(adapter);
866 adapter.registerLocked();
867 }
868
Jeff Brownbd6e1502012-08-28 03:27:37 -0700869 private void handleDisplayDeviceAdded(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700870 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700871 handleDisplayDeviceAddedLocked(device);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700872 }
873 }
874
Jeff Browna506a6e2013-06-04 00:02:38 -0700875 private void handleDisplayDeviceAddedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700876 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700877 if (mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700878 Slog.w(TAG, "Attempted to add already added display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700879 return;
880 }
881
Jeff Brown10acf6d2015-04-14 14:20:47 -0700882 Slog.i(TAG, "Display device added: " + info);
883 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700884
885 mDisplayDevices.add(device);
Michael Wright1c9977b2016-07-12 13:30:10 -0700886 LogicalDisplay display = addLogicalDisplayLocked(device);
Jeff Brown0033a862014-10-08 12:06:39 -0700887 Runnable work = updateDisplayStateLocked(device);
888 if (work != null) {
889 work.run();
890 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700891 scheduleTraversalLocked(false);
892 }
893
Jeff Brownbd6e1502012-08-28 03:27:37 -0700894 private void handleDisplayDeviceChanged(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700895 synchronized (mSyncRoot) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700896 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700897 if (!mDisplayDevices.contains(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700898 Slog.w(TAG, "Attempted to change non-existent display device: " + info);
Jeff Brownbd6e1502012-08-28 03:27:37 -0700899 return;
900 }
901
Jeff Brown10acf6d2015-04-14 14:20:47 -0700902 int diff = device.mDebugLastLoggedDeviceInfo.diff(info);
903 if (diff == DisplayDeviceInfo.DIFF_STATE) {
904 Slog.i(TAG, "Display device changed state: \"" + info.name
905 + "\", " + Display.stateToString(info.state));
906 } else if (diff != 0) {
907 Slog.i(TAG, "Display device changed: " + info);
908 }
Michael Wright1c9977b2016-07-12 13:30:10 -0700909 if ((diff & DisplayDeviceInfo.DIFF_COLOR_MODE) != 0) {
910 try {
911 mPersistentDataStore.setColorMode(device, info.colorMode);
912 } finally {
913 mPersistentDataStore.saveIfNeeded();
914 }
915 }
Jeff Brown10acf6d2015-04-14 14:20:47 -0700916 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browne87bf032012-09-20 18:30:13 -0700917
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700918 device.applyPendingDisplayDeviceInfoChangesLocked();
919 if (updateLogicalDisplaysLocked()) {
Craig Mautner65d11b32012-10-01 13:59:52 -0700920 scheduleTraversalLocked(false);
Jeff Brown64a55af2012-08-26 02:47:39 -0700921 }
922 }
923 }
924
Jeff Brownbd6e1502012-08-28 03:27:37 -0700925 private void handleDisplayDeviceRemoved(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700926 synchronized (mSyncRoot) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700927 handleDisplayDeviceRemovedLocked(device);
928 }
929 }
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700930
Jeff Browna506a6e2013-06-04 00:02:38 -0700931 private void handleDisplayDeviceRemovedLocked(DisplayDevice device) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700932 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
Jeff Browna506a6e2013-06-04 00:02:38 -0700933 if (!mDisplayDevices.remove(device)) {
Jeff Brown10acf6d2015-04-14 14:20:47 -0700934 Slog.w(TAG, "Attempted to remove non-existent display device: " + info);
Jeff Browna506a6e2013-06-04 00:02:38 -0700935 return;
936 }
937
Jeff Brown10acf6d2015-04-14 14:20:47 -0700938 Slog.i(TAG, "Display device removed: " + info);
939 device.mDebugLastLoggedDeviceInfo = info;
Jeff Browna506a6e2013-06-04 00:02:38 -0700940
Jeff Browna506a6e2013-06-04 00:02:38 -0700941 updateLogicalDisplaysLocked();
942 scheduleTraversalLocked(false);
943 }
944
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200945 private void handleLogicalDisplayChanged(int displayId, @NonNull LogicalDisplay display) {
946 if (displayId == Display.DEFAULT_DISPLAY) {
947 recordTopInsetLocked(display);
948 }
949 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_CHANGED);
950 }
951
Jeff Brown8e5d33e2015-06-10 13:05:50 -0700952 private void applyGlobalDisplayStateLocked(List<Runnable> workQueue) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700953 final int count = mDisplayDevices.size();
954 for (int i = 0; i < count; i++) {
955 DisplayDevice device = mDisplayDevices.get(i);
Jeff Browne75926d2014-09-18 15:24:49 -0700956 Runnable runnable = updateDisplayStateLocked(device);
957 if (runnable != null) {
958 workQueue.add(runnable);
959 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700960 }
961 }
962
Jeff Browne75926d2014-09-18 15:24:49 -0700963 private Runnable updateDisplayStateLocked(DisplayDevice device) {
Jeff Browna506a6e2013-06-04 00:02:38 -0700964 // Blank or unblank the display immediately to match the state requested
Jeff Brown037c33e2014-04-09 00:31:55 -0700965 // by the display power controller (if known).
Jeff Browna506a6e2013-06-04 00:02:38 -0700966 DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
967 if ((info.flags & DisplayDeviceInfo.FLAG_NEVER_BLANK) == 0) {
Jeff Brown5d6443b2015-04-10 20:15:01 -0700968 return device.requestDisplayStateLocked(mGlobalDisplayState, mGlobalDisplayBrightness);
Craig Mautner4f67ba62012-08-02 11:23:00 -0700969 }
Jeff Browne75926d2014-09-18 15:24:49 -0700970 return null;
Craig Mautner4f67ba62012-08-02 11:23:00 -0700971 }
972
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700973 // Adds a new logical display based on the given display device.
974 // Sends notifications if needed.
Michael Wright1c9977b2016-07-12 13:30:10 -0700975 private LogicalDisplay addLogicalDisplayLocked(DisplayDevice device) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700976 DisplayDeviceInfo deviceInfo = device.getDisplayDeviceInfoLocked();
977 boolean isDefault = (deviceInfo.flags
978 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0;
979 if (isDefault && mLogicalDisplays.get(Display.DEFAULT_DISPLAY) != null) {
980 Slog.w(TAG, "Ignoring attempt to add a second default display: " + deviceInfo);
981 isDefault = false;
982 }
983
Jeff Brown27f1d672012-10-17 18:32:34 -0700984 if (!isDefault && mSingleDisplayDemoMode) {
985 Slog.i(TAG, "Not creating a logical display for a secondary display "
986 + " because single display demo mode is enabled: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -0700987 return null;
Jeff Brown27f1d672012-10-17 18:32:34 -0700988 }
989
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700990 final int displayId = assignDisplayIdLocked(isDefault);
991 final int layerStack = assignLayerStackLocked(displayId);
992
Jeff Brownd728bf52012-09-08 18:05:28 -0700993 LogicalDisplay display = new LogicalDisplay(displayId, layerStack, device);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700994 display.updateLocked(mDisplayDevices);
995 if (!display.isValidLocked()) {
996 // This should never happen currently.
997 Slog.w(TAG, "Ignoring display device because the logical display "
998 + "created from it was not considered valid: " + deviceInfo);
Michael Wright1c9977b2016-07-12 13:30:10 -0700999 return null;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001000 }
1001
Michael Wrighteedcbf12017-08-16 23:14:54 +01001002 configureColorModeLocked(display, device);
1003 if (isDefault) {
1004 recordStableDisplayStatsIfNeededLocked(display);
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001005 recordTopInsetLocked(display);
Michael Wrighteedcbf12017-08-16 23:14:54 +01001006 }
1007
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001008 mLogicalDisplays.put(displayId, display);
1009
1010 // Wake up waitForDefaultDisplay.
1011 if (isDefault) {
1012 mSyncRoot.notifyAll();
1013 }
1014
1015 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_ADDED);
Michael Wright1c9977b2016-07-12 13:30:10 -07001016 return display;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001017 }
1018
1019 private int assignDisplayIdLocked(boolean isDefault) {
1020 return isDefault ? Display.DEFAULT_DISPLAY : mNextNonDefaultDisplayId++;
1021 }
1022
1023 private int assignLayerStackLocked(int displayId) {
1024 // Currently layer stacks and display ids are the same.
1025 // This need not be the case.
1026 return displayId;
1027 }
1028
Michael Wrighteedcbf12017-08-16 23:14:54 +01001029 private void configureColorModeLocked(LogicalDisplay display, DisplayDevice device) {
1030 if (display.getPrimaryDisplayDeviceLocked() == device) {
1031 int colorMode = mPersistentDataStore.getColorMode(device);
1032 if (colorMode == Display.COLOR_MODE_INVALID) {
1033 if ((device.getDisplayDeviceInfoLocked().flags
1034 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
1035 colorMode = mDefaultDisplayDefaultColorMode;
1036 } else {
1037 colorMode = Display.COLOR_MODE_DEFAULT;
1038 }
1039 }
1040 display.setRequestedColorModeLocked(colorMode);
1041 }
1042 }
1043
1044 // If we've never recorded stable device stats for this device before and they aren't
1045 // explicitly configured, go ahead and record the stable device stats now based on the status
1046 // of the default display at first boot.
1047 private void recordStableDisplayStatsIfNeededLocked(LogicalDisplay d) {
1048 if (mStableDisplaySize.x <= 0 && mStableDisplaySize.y <= 0) {
1049 DisplayInfo info = d.getDisplayInfoLocked();
1050 setStableDisplaySizeLocked(info.getNaturalWidth(), info.getNaturalHeight());
1051 }
1052 }
1053
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001054 private void recordTopInsetLocked(@Nullable LogicalDisplay d) {
MÃ¥rten Kongstad250cb2b2018-11-02 11:01:44 +01001055 if (d == null) {
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001056 return;
1057 }
1058 int topInset = d.getInsets().top;
1059 if (topInset == mDefaultDisplayTopInset) {
1060 return;
1061 }
1062 mDefaultDisplayTopInset = topInset;
1063 SystemProperties.set(PROP_DEFAULT_DISPLAY_TOP_INSET, Integer.toString(topInset));
1064 }
1065
Michael Wrighteedcbf12017-08-16 23:14:54 +01001066 private void setStableDisplaySizeLocked(int width, int height) {
1067 mStableDisplaySize = new Point(width, height);
1068 try {
1069 mPersistentDataStore.setStableDisplaySize(mStableDisplaySize);
1070 } finally {
1071 mPersistentDataStore.saveIfNeeded();
1072 }
1073 }
1074
Dan Gittik122df862018-03-28 16:59:22 +01001075 @VisibleForTesting
1076 Curve getMinimumBrightnessCurveInternal() {
1077 return mMinimumBrightnessCurve;
1078 }
1079
Peiyong Lin277eaff2019-01-16 16:18:22 -08001080 int getPreferredWideGamutColorSpaceIdInternal() {
1081 return mWideColorSpace.getId();
1082 }
1083
Michael Wrighteef0e132017-11-21 17:57:52 +00001084 private void setBrightnessConfigurationForUserInternal(
Dan Gittik7a32fba2018-03-28 12:19:38 +01001085 @Nullable BrightnessConfiguration c, @UserIdInt int userId,
Kenny Guy05ce8092018-01-17 13:44:20 +00001086 @Nullable String packageName) {
Dan Gittik122df862018-03-28 16:59:22 +01001087 validateBrightnessConfiguration(c);
Michael Wrighteef0e132017-11-21 17:57:52 +00001088 final int userSerial = getUserManager().getUserSerialNumber(userId);
1089 synchronized (mSyncRoot) {
1090 try {
Kenny Guy05ce8092018-01-17 13:44:20 +00001091 mPersistentDataStore.setBrightnessConfigurationForUser(c, userSerial,
1092 packageName);
Michael Wrighteef0e132017-11-21 17:57:52 +00001093 } finally {
1094 mPersistentDataStore.saveIfNeeded();
1095 }
1096 if (userId == mCurrentUserId) {
1097 mDisplayPowerController.setBrightnessConfiguration(c);
1098 }
1099 }
1100 }
1101
Dan Gittik122df862018-03-28 16:59:22 +01001102 @VisibleForTesting
1103 void validateBrightnessConfiguration(BrightnessConfiguration config) {
1104 if (config == null) {
1105 return;
1106 }
1107 if (isBrightnessConfigurationTooDark(config)) {
1108 throw new IllegalArgumentException("brightness curve is too dark");
1109 }
1110 }
1111
1112 private boolean isBrightnessConfigurationTooDark(BrightnessConfiguration config) {
1113 Pair<float[], float[]> curve = config.getCurve();
1114 float[] lux = curve.first;
1115 float[] nits = curve.second;
1116 for (int i = 0; i < lux.length; i++) {
1117 if (nits[i] < mMinimumBrightnessSpline.interpolate(lux[i])) {
1118 return true;
1119 }
1120 }
1121 return false;
1122 }
1123
Michael Wrighteef0e132017-11-21 17:57:52 +00001124 private void loadBrightnessConfiguration() {
1125 synchronized (mSyncRoot) {
1126 final int userSerial = getUserManager().getUserSerialNumber(mCurrentUserId);
1127 BrightnessConfiguration config =
1128 mPersistentDataStore.getBrightnessConfiguration(userSerial);
1129 mDisplayPowerController.setBrightnessConfiguration(config);
1130 }
1131 }
1132
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001133 // Updates all existing logical displays given the current set of display devices.
1134 // Removes invalid logical displays.
1135 // Sends notifications if needed.
1136 private boolean updateLogicalDisplaysLocked() {
1137 boolean changed = false;
1138 for (int i = mLogicalDisplays.size(); i-- > 0; ) {
1139 final int displayId = mLogicalDisplays.keyAt(i);
1140 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1141
1142 mTempDisplayInfo.copyFrom(display.getDisplayInfoLocked());
1143 display.updateLocked(mDisplayDevices);
1144 if (!display.isValidLocked()) {
1145 mLogicalDisplays.removeAt(i);
1146 sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_REMOVED);
1147 changed = true;
1148 } else if (!mTempDisplayInfo.equals(display.getDisplayInfoLocked())) {
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001149 handleLogicalDisplayChanged(displayId, display);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001150 changed = true;
1151 }
1152 }
1153 return changed;
1154 }
1155
Robert Carrae606b42018-02-15 15:36:23 -08001156 private void performTraversalLocked(SurfaceControl.Transaction t) {
Jeff Brownd728bf52012-09-08 18:05:28 -07001157 // Clear all viewports before configuring displays so that we can keep
1158 // track of which ones we have configured.
1159 clearViewportsLocked();
1160
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001161 // Configure each display device.
1162 final int count = mDisplayDevices.size();
1163 for (int i = 0; i < count; i++) {
1164 DisplayDevice device = mDisplayDevices.get(i);
Robert Carrae606b42018-02-15 15:36:23 -08001165 configureDisplayLocked(t, device);
1166 device.performTraversalLocked(t);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001167 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001168
1169 // Tell the input system about these new viewports.
Jeff Brown4ccb8232014-01-16 22:16:42 -08001170 if (mInputManagerInternal != null) {
Jeff Brownd728bf52012-09-08 18:05:28 -07001171 mHandler.sendEmptyMessage(MSG_UPDATE_VIEWPORT);
1172 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001173 }
1174
Michael Wright3f145a22014-07-22 19:46:03 -07001175 private void setDisplayPropertiesInternal(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001176 float requestedRefreshRate, int requestedModeId, boolean inTraversal) {
Craig Mautner722285e2012-09-07 13:55:58 -07001177 synchronized (mSyncRoot) {
1178 LogicalDisplay display = mLogicalDisplays.get(displayId);
Michael Wright3f145a22014-07-22 19:46:03 -07001179 if (display == null) {
1180 return;
1181 }
1182 if (display.hasContentLocked() != hasContent) {
Jeff Brown33041bd2013-08-02 21:11:14 -07001183 if (DEBUG) {
1184 Slog.d(TAG, "Display " + displayId + " hasContent flag changed: "
1185 + "hasContent=" + hasContent + ", inTraversal=" + inTraversal);
1186 }
1187
Craig Mautner722285e2012-09-07 13:55:58 -07001188 display.setHasContentLocked(hasContent);
Craig Mautner65d11b32012-10-01 13:59:52 -07001189 scheduleTraversalLocked(inTraversal);
Craig Mautner722285e2012-09-07 13:55:58 -07001190 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001191 if (requestedModeId == 0 && requestedRefreshRate != 0) {
1192 // Scan supported modes returned by display.getInfo() to find a mode with the same
1193 // size as the default display mode but with the specified refresh rate instead.
1194 requestedModeId = display.getDisplayInfoLocked().findDefaultModeByRefreshRate(
1195 requestedRefreshRate);
1196 }
1197 if (display.getRequestedModeIdLocked() != requestedModeId) {
Michael Wright3f145a22014-07-22 19:46:03 -07001198 if (DEBUG) {
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001199 Slog.d(TAG, "Display " + displayId + " switching to mode " + requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -07001200 }
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07001201 display.setRequestedModeIdLocked(requestedModeId);
Michael Wright3f145a22014-07-22 19:46:03 -07001202 scheduleTraversalLocked(inTraversal);
1203 }
Craig Mautner722285e2012-09-07 13:55:58 -07001204 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001205 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001206
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08001207 private void setDisplayOffsetsInternal(int displayId, int x, int y) {
1208 synchronized (mSyncRoot) {
1209 LogicalDisplay display = mLogicalDisplays.get(displayId);
1210 if (display == null) {
1211 return;
1212 }
1213 if (display.getDisplayOffsetXLocked() != x
1214 || display.getDisplayOffsetYLocked() != y) {
1215 if (DEBUG) {
1216 Slog.d(TAG, "Display " + displayId + " burn-in offset set to ("
1217 + x + ", " + y + ")");
1218 }
1219 display.setDisplayOffsetsLocked(x, y);
1220 scheduleTraversalLocked(false);
1221 }
1222 }
1223 }
1224
Sam Lin4c3ac2b2019-02-18 04:50:26 -08001225 private void setDisplayScalingDisabledInternal(int displayId, boolean disable) {
1226 synchronized (mSyncRoot) {
1227 final LogicalDisplay display = mLogicalDisplays.get(displayId);
1228 if (display == null) {
1229 return;
1230 }
1231 if (display.isDisplayScalingDisabled() != disable) {
1232 if (DEBUG) {
1233 Slog.d(TAG, "Display " + displayId + " content scaling disabled = " + disable);
1234 }
1235 display.setDisplayScalingDisabledLocked(disable);
1236 scheduleTraversalLocked(false);
1237 }
1238 }
1239 }
1240
Andrii Kulianfb1bf692017-01-17 11:17:34 -08001241 // Updates the lists of UIDs that are present on displays.
1242 private void setDisplayAccessUIDsInternal(SparseArray<IntArray> newDisplayAccessUIDs) {
1243 synchronized (mSyncRoot) {
1244 mDisplayAccessUIDs.clear();
1245 for (int i = newDisplayAccessUIDs.size() - 1; i >= 0; i--) {
1246 mDisplayAccessUIDs.append(newDisplayAccessUIDs.keyAt(i),
1247 newDisplayAccessUIDs.valueAt(i));
1248 }
1249 }
1250 }
1251
1252 // Checks if provided UID's content is present on the display and UID has access to it.
1253 private boolean isUidPresentOnDisplayInternal(int uid, int displayId) {
1254 synchronized (mSyncRoot) {
1255 final IntArray displayUIDs = mDisplayAccessUIDs.get(displayId);
1256 return displayUIDs != null && displayUIDs.indexOf(uid) != -1;
1257 }
1258 }
1259
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001260 @Nullable
1261 private IBinder getDisplayToken(int displayId) {
1262 synchronized (mSyncRoot) {
1263 final LogicalDisplay display = mLogicalDisplays.get(displayId);
1264 if (display != null) {
1265 final DisplayDevice device = display.getPrimaryDisplayDeviceLocked();
1266 if (device != null) {
1267 return device.getDisplayTokenLocked();
1268 }
1269 }
1270 }
1271
1272 return null;
1273 }
1274
1275 private boolean screenshotInternal(int displayId, Surface outSurface) {
1276 final IBinder token = getDisplayToken(displayId);
1277 if (token == null) {
1278 return false;
1279 }
Robert Carr5c52b132019-02-15 15:48:11 -08001280 final GraphicBuffer gb = SurfaceControl.screenshotToBufferWithSecureLayersUnsafe(
1281 token, new Rect(), 0 /* width */, 0 /* height */, false /* useIdentityTransform */,
1282 0 /* rotation */);
1283 try {
1284 outSurface.attachAndQueueBuffer(gb);
1285 } catch (RuntimeException e) {
1286 Slog.w(TAG, "Failed to take screenshot - " + e.getMessage());
1287 }
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001288 return true;
1289 }
1290
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001291 @VisibleForTesting
1292 DisplayedContentSamplingAttributes getDisplayedContentSamplingAttributesInternal(
1293 int displayId) {
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001294 final IBinder token = getDisplayToken(displayId);
1295 if (token == null) {
1296 return null;
1297 }
1298 return SurfaceControl.getDisplayedContentSamplingAttributes(token);
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001299 }
1300
1301 @VisibleForTesting
1302 boolean setDisplayedContentSamplingEnabledInternal(
1303 int displayId, boolean enable, int componentMask, int maxFrames) {
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001304 final IBinder token = getDisplayToken(displayId);
1305 if (token == null) {
1306 return false;
1307 }
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001308 return SurfaceControl.setDisplayedContentSamplingEnabled(
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001309 token, enable, componentMask, maxFrames);
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001310 }
1311
1312 @VisibleForTesting
1313 DisplayedContentSample getDisplayedContentSampleInternal(int displayId,
1314 long maxFrames, long timestamp) {
Dominik Laskowskie912ae32019-01-26 11:19:36 -08001315 final IBinder token = getDisplayToken(displayId);
1316 if (token == null) {
1317 return null;
1318 }
1319 return SurfaceControl.getDisplayedContentSample(token, maxFrames, timestamp);
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07001320 }
1321
Jeff Brownd728bf52012-09-08 18:05:28 -07001322 private void clearViewportsLocked() {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001323 mViewports.clear();
Craig Mautner722285e2012-09-07 13:55:58 -07001324 }
1325
Robert Carrae606b42018-02-15 15:36:23 -08001326 private void configureDisplayLocked(SurfaceControl.Transaction t, DisplayDevice device) {
Jeff Brownd14c8c92014-01-07 18:13:09 -08001327 final DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked();
1328 final boolean ownContent = (info.flags & DisplayDeviceInfo.FLAG_OWN_CONTENT_ONLY) != 0;
Jeff Browna506a6e2013-06-04 00:02:38 -07001329
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001330 // Find the logical display that the display device is showing.
Jeff Brownd14c8c92014-01-07 18:13:09 -08001331 // Certain displays only ever show their own content.
Craig Mautner722285e2012-09-07 13:55:58 -07001332 LogicalDisplay display = findLogicalDisplayForDeviceLocked(device);
Jeff Brownd14c8c92014-01-07 18:13:09 -08001333 if (!ownContent) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001334 if (display != null && !display.hasContentLocked()) {
1335 // If the display does not have any content of its own, then
1336 // automatically mirror the default logical display contents.
1337 display = null;
1338 }
1339 if (display == null) {
1340 display = mLogicalDisplays.get(Display.DEFAULT_DISPLAY);
1341 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001342 }
1343
1344 // Apply the logical display configuration to the display device.
1345 if (display == null) {
1346 // TODO: no logical display for the device, blank it
Jeff Brownd728bf52012-09-08 18:05:28 -07001347 Slog.w(TAG, "Missing logical display to use for physical display device: "
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001348 + device.getDisplayDeviceInfoLocked());
Jeff Brownd728bf52012-09-08 18:05:28 -07001349 return;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001350 }
Robert Carrae606b42018-02-15 15:36:23 -08001351 display.configureDisplayLocked(t, device, info.state == Display.STATE_OFF);
Arthur Hung41e81e72018-10-31 18:04:56 +08001352 final int viewportType;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001353 // Update the corresponding viewport.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001354 if ((info.flags & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0) {
Arthur Hung41e81e72018-10-31 18:04:56 +08001355 viewportType = VIEWPORT_INTERNAL;
1356 } else if (info.touch == DisplayDeviceInfo.TOUCH_EXTERNAL) {
1357 viewportType = VIEWPORT_EXTERNAL;
1358 } else if (info.touch == DisplayDeviceInfo.TOUCH_VIRTUAL
1359 && !TextUtils.isEmpty(info.uniqueId)) {
1360 viewportType = VIEWPORT_VIRTUAL;
1361 } else {
Arthur Hung46c4e582019-02-12 15:55:28 +08001362 Slog.i(TAG, "Display " + info + " does not support input device matching.");
Arthur Hung41e81e72018-10-31 18:04:56 +08001363 return;
Jeff Brownd728bf52012-09-08 18:05:28 -07001364 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001365
Arthur Hung41e81e72018-10-31 18:04:56 +08001366 populateViewportLocked(viewportType, display.getDisplayIdLocked(), device, info.uniqueId);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001367 }
1368
1369 /**
1370 * Get internal or external viewport. Create it if does not currently exist.
1371 * @param viewportType - either INTERNAL or EXTERNAL
1372 * @return the viewport with the requested type
1373 */
Arthur Hung41e81e72018-10-31 18:04:56 +08001374 private DisplayViewport getViewportLocked(int viewportType, String uniqueId) {
1375 if (viewportType != VIEWPORT_INTERNAL && viewportType != VIEWPORT_EXTERNAL
1376 && viewportType != VIEWPORT_VIRTUAL) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001377 Slog.wtf(TAG, "Cannot call getViewportByTypeLocked for type "
1378 + DisplayViewport.typeToString(viewportType));
1379 return null;
1380 }
Arthur Hung41e81e72018-10-31 18:04:56 +08001381
1382 // Only allow a single INTERNAL or EXTERNAL viewport by forcing their uniqueIds
1383 // to be identical (in particular, empty).
1384 // TODO (b/116824030) allow multiple EXTERNAL viewports and remove this function.
1385 if (viewportType != VIEWPORT_VIRTUAL) {
1386 uniqueId = "";
1387 }
1388
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001389 DisplayViewport viewport;
1390 final int count = mViewports.size();
1391 for (int i = 0; i < count; i++) {
1392 viewport = mViewports.get(i);
Arthur Hung41e81e72018-10-31 18:04:56 +08001393 if (viewport.type == viewportType && uniqueId.equals(viewport.uniqueId)) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001394 return viewport;
1395 }
1396 }
1397
Arthur Hung41e81e72018-10-31 18:04:56 +08001398 // Creates the viewport if none exists.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001399 viewport = new DisplayViewport();
1400 viewport.type = viewportType;
Arthur Hung41e81e72018-10-31 18:04:56 +08001401 viewport.uniqueId = uniqueId;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001402 mViewports.add(viewport);
1403 return viewport;
1404 }
1405
Arthur Hung41e81e72018-10-31 18:04:56 +08001406 private void populateViewportLocked(int viewportType,
1407 int displayId, DisplayDevice device, String uniqueId) {
1408 final DisplayViewport viewport = getViewportLocked(viewportType, uniqueId);
Jeff Brownd728bf52012-09-08 18:05:28 -07001409 device.populateViewportLocked(viewport);
Arthur Hung41e81e72018-10-31 18:04:56 +08001410 viewport.valid = true;
1411 viewport.displayId = displayId;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001412 }
1413
1414 private LogicalDisplay findLogicalDisplayForDeviceLocked(DisplayDevice device) {
1415 final int count = mLogicalDisplays.size();
1416 for (int i = 0; i < count; i++) {
1417 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1418 if (display.getPrimaryDisplayDeviceLocked() == device) {
1419 return display;
1420 }
1421 }
1422 return null;
1423 }
1424
Jeff Brownbd6e1502012-08-28 03:27:37 -07001425 private void sendDisplayEventLocked(int displayId, int event) {
1426 Message msg = mHandler.obtainMessage(MSG_DELIVER_DISPLAY_EVENT, displayId, event);
1427 mHandler.sendMessage(msg);
1428 }
1429
Robert Carrae606b42018-02-15 15:36:23 -08001430 // Requests that performTraversals be called at a
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001431 // later time to apply changes to surfaces and displays.
Craig Mautner65d11b32012-10-01 13:59:52 -07001432 private void scheduleTraversalLocked(boolean inTraversal) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001433 if (!mPendingTraversal && mWindowManagerInternal != null) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001434 mPendingTraversal = true;
Craig Mautner65d11b32012-10-01 13:59:52 -07001435 if (!inTraversal) {
1436 mHandler.sendEmptyMessage(MSG_REQUEST_TRAVERSAL);
1437 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001438 }
1439 }
1440
1441 // Runs on Handler thread.
1442 // Delivers display event notifications to callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001443 private void deliverDisplayEvent(int displayId, int event) {
1444 if (DEBUG) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001445 Slog.d(TAG, "Delivering display event: displayId="
1446 + displayId + ", event=" + event);
Jeff Brownfa25bf52012-07-23 19:26:30 -07001447 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07001448
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001449 // Grab the lock and copy the callbacks.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001450 final int count;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001451 synchronized (mSyncRoot) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001452 count = mCallbacks.size();
1453 mTempCallbacks.clear();
1454 for (int i = 0; i < count; i++) {
1455 mTempCallbacks.add(mCallbacks.valueAt(i));
Craig Mautner4f67ba62012-08-02 11:23:00 -07001456 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001457 }
Craig Mautner4f67ba62012-08-02 11:23:00 -07001458
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001459 // After releasing the lock, send the notifications out.
Jeff Brownbd6e1502012-08-28 03:27:37 -07001460 for (int i = 0; i < count; i++) {
1461 mTempCallbacks.get(i).notifyDisplayEventAsync(displayId, event);
1462 }
1463 mTempCallbacks.clear();
Craig Mautner4f67ba62012-08-02 11:23:00 -07001464 }
1465
Michael Wrightc39d47a2014-07-08 18:07:36 -07001466 private IMediaProjectionManager getProjectionService() {
1467 if (mProjectionService == null) {
1468 IBinder b = ServiceManager.getService(Context.MEDIA_PROJECTION_SERVICE);
1469 mProjectionService = IMediaProjectionManager.Stub.asInterface(b);
1470 }
1471 return mProjectionService;
1472 }
1473
Michael Wrighteef0e132017-11-21 17:57:52 +00001474 private UserManager getUserManager() {
1475 return mContext.getSystemService(UserManager.class);
1476 }
1477
Jeff Brown4ccb8232014-01-16 22:16:42 -08001478 private void dumpInternal(PrintWriter pw) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001479 pw.println("DISPLAY MANAGER (dumpsys display)");
Jeff Brownfa25bf52012-07-23 19:26:30 -07001480
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001481 synchronized (mSyncRoot) {
Jeff Brown9e316a12012-10-08 19:17:06 -07001482 pw.println(" mOnlyCode=" + mOnlyCore);
1483 pw.println(" mSafeMode=" + mSafeMode);
1484 pw.println(" mPendingTraversal=" + mPendingTraversal);
Jeff Brown037c33e2014-04-09 00:31:55 -07001485 pw.println(" mGlobalDisplayState=" + Display.stateToString(mGlobalDisplayState));
Jeff Brown9e316a12012-10-08 19:17:06 -07001486 pw.println(" mNextNonDefaultDisplayId=" + mNextNonDefaultDisplayId);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001487 pw.println(" mViewports=" + mViewports);
Damien Bargiacchi4364bbf2016-11-01 21:44:20 -07001488 pw.println(" mDefaultDisplayDefaultColorMode=" + mDefaultDisplayDefaultColorMode);
Jeff Brown27f1d672012-10-17 18:32:34 -07001489 pw.println(" mSingleDisplayDemoMode=" + mSingleDisplayDemoMode);
Jeff Brownce468a32013-11-21 16:42:03 -08001490 pw.println(" mWifiDisplayScanRequestCount=" + mWifiDisplayScanRequestCount);
Michael Wrighteedcbf12017-08-16 23:14:54 +01001491 pw.println(" mStableDisplaySize=" + mStableDisplaySize);
Kenny Guy2047db92019-02-21 13:04:05 +00001492 pw.println(" mMinimumBrightnessCurve=" + mMinimumBrightnessCurve);
Jeff Brown9e316a12012-10-08 19:17:06 -07001493
Jeff Brownbd6e1502012-08-28 03:27:37 -07001494 IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001495 ipw.increaseIndent();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001496
1497 pw.println();
1498 pw.println("Display Adapters: size=" + mDisplayAdapters.size());
Jeff Brown848c2dc2012-08-19 20:18:08 -07001499 for (DisplayAdapter adapter : mDisplayAdapters) {
Jeff Brownbd6e1502012-08-28 03:27:37 -07001500 pw.println(" " + adapter.getName());
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001501 adapter.dumpLocked(ipw);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001502 }
Craig Mautner9de49362012-08-02 14:30:30 -07001503
Jeff Brownbd6e1502012-08-28 03:27:37 -07001504 pw.println();
1505 pw.println("Display Devices: size=" + mDisplayDevices.size());
1506 for (DisplayDevice device : mDisplayDevices) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001507 pw.println(" " + device.getDisplayDeviceInfoLocked());
1508 device.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001509 }
1510
1511 final int logicalDisplayCount = mLogicalDisplays.size();
1512 pw.println();
1513 pw.println("Logical Displays: size=" + logicalDisplayCount);
1514 for (int i = 0; i < logicalDisplayCount; i++) {
1515 int displayId = mLogicalDisplays.keyAt(i);
1516 LogicalDisplay display = mLogicalDisplays.valueAt(i);
1517 pw.println(" Display " + displayId + ":");
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001518 display.dumpLocked(ipw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001519 }
Jeff Brownce468a32013-11-21 16:42:03 -08001520
1521 final int callbackCount = mCallbacks.size();
1522 pw.println();
1523 pw.println("Callbacks: size=" + callbackCount);
1524 for (int i = 0; i < callbackCount; i++) {
1525 CallbackRecord callback = mCallbacks.valueAt(i);
1526 pw.println(" " + i + ": mPid=" + callback.mPid
1527 + ", mWifiDisplayScanRequested=" + callback.mWifiDisplayScanRequested);
1528 }
Jeff Brownad9ef192014-04-08 17:26:30 -07001529
1530 if (mDisplayPowerController != null) {
1531 mDisplayPowerController.dump(pw);
1532 }
Michael Wright1c9977b2016-07-12 13:30:10 -07001533
1534 pw.println();
1535 mPersistentDataStore.dump(pw);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001536 }
1537 }
1538
Dan Gittik122df862018-03-28 16:59:22 +01001539 private static float[] getFloatArray(TypedArray array) {
1540 int length = array.length();
1541 float[] floatArray = new float[length];
1542 for (int i = 0; i < length; i++) {
1543 floatArray[i] = array.getFloat(i, Float.NaN);
1544 }
1545 array.recycle();
1546 return floatArray;
1547 }
1548
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001549 /**
1550 * This is the object that everything in the display manager locks on.
1551 * We make it an inner class within the {@link DisplayManagerService} to so that it is
1552 * clear that the object belongs to the display manager service and that it is
1553 * a unique object with a special purpose.
1554 */
1555 public static final class SyncRoot {
1556 }
1557
Santos Cordonee8931e2017-04-05 10:31:15 -07001558 @VisibleForTesting
1559 static class Injector {
1560 VirtualDisplayAdapter getVirtualDisplayAdapter(SyncRoot syncRoot, Context context,
1561 Handler handler, DisplayAdapter.Listener displayAdapterListener) {
1562 return new VirtualDisplayAdapter(syncRoot, context, handler, displayAdapterListener);
1563 }
Santos Cordonc22c5632017-06-21 16:03:49 -07001564
1565 long getDefaultDisplayDelayTimeout() {
1566 return WAIT_FOR_DEFAULT_DISPLAY_TIMEOUT;
1567 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001568 }
1569
Alex Sakhartchouk879d24f2017-06-20 22:01:19 -04001570 @VisibleForTesting
1571 DisplayDeviceInfo getDisplayDeviceInfoInternal(int displayId) {
1572 synchronized (mSyncRoot) {
1573 LogicalDisplay display = mLogicalDisplays.get(displayId);
1574 if (display != null) {
1575 DisplayDevice displayDevice = display.getPrimaryDisplayDeviceLocked();
1576 return displayDevice.getDisplayDeviceInfoLocked();
1577 }
1578 return null;
1579 }
1580 }
1581
Jeff Brownbd6e1502012-08-28 03:27:37 -07001582 private final class DisplayManagerHandler extends Handler {
1583 public DisplayManagerHandler(Looper looper) {
1584 super(looper, null, true /*async*/);
Jeff Brown848c2dc2012-08-19 20:18:08 -07001585 }
Jeff Brownbf5740e2012-08-19 23:20:02 -07001586
Jeff Brownbd6e1502012-08-28 03:27:37 -07001587 @Override
1588 public void handleMessage(Message msg) {
1589 switch (msg.what) {
Santos Cordonc22c5632017-06-21 16:03:49 -07001590 case MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS:
1591 registerDefaultDisplayAdapters();
Jeff Brownbd6e1502012-08-28 03:27:37 -07001592 break;
1593
1594 case MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS:
1595 registerAdditionalDisplayAdapters();
1596 break;
1597
1598 case MSG_DELIVER_DISPLAY_EVENT:
1599 deliverDisplayEvent(msg.arg1, msg.arg2);
1600 break;
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001601
1602 case MSG_REQUEST_TRAVERSAL:
Jeff Brown4ccb8232014-01-16 22:16:42 -08001603 mWindowManagerInternal.requestTraversalFromDisplayManager();
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001604 break;
Jeff Brownd728bf52012-09-08 18:05:28 -07001605
1606 case MSG_UPDATE_VIEWPORT: {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001607 final boolean changed;
Jeff Brownd728bf52012-09-08 18:05:28 -07001608 synchronized (mSyncRoot) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001609 changed = !mTempViewports.equals(mViewports);
1610 if (changed) {
1611 mTempViewports.clear();
1612 for (DisplayViewport d : mViewports) {
1613 mTempViewports.add(d.makeCopy());
1614 }
Santos Cordonee8931e2017-04-05 10:31:15 -07001615 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001616 }
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001617 if (changed) {
1618 mInputManagerInternal.setDisplayViewports(mTempViewports);
1619 }
Jeff Brownd728bf52012-09-08 18:05:28 -07001620 break;
1621 }
Kenny Guy22bd0442017-10-26 00:15:54 +01001622
Michael Wrighteef0e132017-11-21 17:57:52 +00001623 case MSG_LOAD_BRIGHTNESS_CONFIGURATION:
1624 loadBrightnessConfiguration();
1625 break;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001626 }
1627 }
1628 }
1629
1630 private final class DisplayAdapterListener implements DisplayAdapter.Listener {
1631 @Override
1632 public void onDisplayDeviceEvent(DisplayDevice device, int event) {
1633 switch (event) {
1634 case DisplayAdapter.DISPLAY_DEVICE_EVENT_ADDED:
1635 handleDisplayDeviceAdded(device);
1636 break;
1637
1638 case DisplayAdapter.DISPLAY_DEVICE_EVENT_CHANGED:
1639 handleDisplayDeviceChanged(device);
1640 break;
1641
1642 case DisplayAdapter.DISPLAY_DEVICE_EVENT_REMOVED:
1643 handleDisplayDeviceRemoved(device);
1644 break;
1645 }
1646 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001647
1648 @Override
1649 public void onTraversalRequested() {
1650 synchronized (mSyncRoot) {
Craig Mautner65d11b32012-10-01 13:59:52 -07001651 scheduleTraversalLocked(false);
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001652 }
1653 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07001654 }
1655
1656 private final class CallbackRecord implements DeathRecipient {
Jeff Brownce468a32013-11-21 16:42:03 -08001657 public final int mPid;
Jeff Brownbd6e1502012-08-28 03:27:37 -07001658 private final IDisplayManagerCallback mCallback;
1659
Jeff Brownce468a32013-11-21 16:42:03 -08001660 public boolean mWifiDisplayScanRequested;
1661
Jeff Brownbd6e1502012-08-28 03:27:37 -07001662 public CallbackRecord(int pid, IDisplayManagerCallback callback) {
1663 mPid = pid;
1664 mCallback = callback;
1665 }
1666
1667 @Override
1668 public void binderDied() {
1669 if (DEBUG) {
1670 Slog.d(TAG, "Display listener for pid " + mPid + " died.");
1671 }
Jeff Brownce468a32013-11-21 16:42:03 -08001672 onCallbackDied(this);
Jeff Brownbd6e1502012-08-28 03:27:37 -07001673 }
1674
1675 public void notifyDisplayEventAsync(int displayId, int event) {
1676 try {
1677 mCallback.onDisplayEvent(displayId, event);
1678 } catch (RemoteException ex) {
1679 Slog.w(TAG, "Failed to notify process "
1680 + mPid + " that displays changed, assuming it died.", ex);
1681 binderDied();
1682 }
1683 }
1684 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001685
Santos Cordonee8931e2017-04-05 10:31:15 -07001686 @VisibleForTesting
1687 final class BinderService extends IDisplayManager.Stub {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001688 /**
1689 * Returns information about the specified logical display.
1690 *
1691 * @param displayId The logical display id.
lumarkec75b422019-01-07 15:58:38 +08001692 * @return The logical display info, return {@code null} if the display does not exist or
1693 * the calling UID isn't present on the display. The returned object must be treated as
1694 * immutable.
Jeff Brown4ccb8232014-01-16 22:16:42 -08001695 */
1696 @Override // Binder call
1697 public DisplayInfo getDisplayInfo(int displayId) {
1698 final int callingUid = Binder.getCallingUid();
1699 final long token = Binder.clearCallingIdentity();
1700 try {
1701 return getDisplayInfoInternal(displayId, callingUid);
1702 } finally {
1703 Binder.restoreCallingIdentity(token);
1704 }
1705 }
1706
1707 /**
1708 * Returns the list of all display ids.
1709 */
1710 @Override // Binder call
1711 public int[] getDisplayIds() {
1712 final int callingUid = Binder.getCallingUid();
1713 final long token = Binder.clearCallingIdentity();
1714 try {
1715 return getDisplayIdsInternal(callingUid);
1716 } finally {
1717 Binder.restoreCallingIdentity(token);
1718 }
1719 }
1720
lumarkec75b422019-01-07 15:58:38 +08001721 @Override // Binder call
1722 public boolean isUidPresentOnDisplay(int uid, int displayId) {
1723 final long token = Binder.clearCallingIdentity();
1724 try {
1725 return isUidPresentOnDisplayInternal(uid, displayId);
1726 } finally {
1727 Binder.restoreCallingIdentity(token);
1728 }
1729 }
1730
Michael Wrighteedcbf12017-08-16 23:14:54 +01001731 /**
1732 * Returns the stable device display size, in pixels.
1733 */
1734 @Override // Binder call
1735 public Point getStableDisplaySize() {
1736 final long token = Binder.clearCallingIdentity();
1737 try {
1738 return getStableDisplaySizeInternal();
1739 } finally {
1740 Binder.restoreCallingIdentity(token);
1741 }
1742 }
1743
Jeff Brown4ccb8232014-01-16 22:16:42 -08001744 @Override // Binder call
1745 public void registerCallback(IDisplayManagerCallback callback) {
1746 if (callback == null) {
1747 throw new IllegalArgumentException("listener must not be null");
1748 }
1749
1750 final int callingPid = Binder.getCallingPid();
1751 final long token = Binder.clearCallingIdentity();
1752 try {
1753 registerCallbackInternal(callback, callingPid);
1754 } finally {
1755 Binder.restoreCallingIdentity(token);
1756 }
1757 }
1758
1759 @Override // Binder call
1760 public void startWifiDisplayScan() {
1761 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1762 "Permission required to start wifi display scans");
1763
1764 final int callingPid = Binder.getCallingPid();
1765 final long token = Binder.clearCallingIdentity();
1766 try {
1767 startWifiDisplayScanInternal(callingPid);
1768 } finally {
1769 Binder.restoreCallingIdentity(token);
1770 }
1771 }
1772
1773 @Override // Binder call
1774 public void stopWifiDisplayScan() {
1775 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1776 "Permission required to stop wifi display scans");
1777
1778 final int callingPid = Binder.getCallingPid();
1779 final long token = Binder.clearCallingIdentity();
1780 try {
1781 stopWifiDisplayScanInternal(callingPid);
1782 } finally {
1783 Binder.restoreCallingIdentity(token);
1784 }
1785 }
1786
1787 @Override // Binder call
1788 public void connectWifiDisplay(String address) {
1789 if (address == null) {
1790 throw new IllegalArgumentException("address must not be null");
1791 }
1792 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1793 "Permission required to connect to a wifi display");
1794
1795 final long token = Binder.clearCallingIdentity();
1796 try {
1797 connectWifiDisplayInternal(address);
1798 } finally {
1799 Binder.restoreCallingIdentity(token);
1800 }
1801 }
1802
1803 @Override // Binder call
1804 public void disconnectWifiDisplay() {
1805 // This request does not require special permissions.
1806 // Any app can request disconnection from the currently active wifi display.
1807 // This exception should no longer be needed once wifi display control moves
1808 // to the media router service.
1809
1810 final long token = Binder.clearCallingIdentity();
1811 try {
1812 disconnectWifiDisplayInternal();
1813 } finally {
1814 Binder.restoreCallingIdentity(token);
1815 }
1816 }
1817
1818 @Override // Binder call
1819 public void renameWifiDisplay(String address, String alias) {
1820 if (address == null) {
1821 throw new IllegalArgumentException("address must not be null");
1822 }
1823 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1824 "Permission required to rename to a wifi display");
1825
1826 final long token = Binder.clearCallingIdentity();
1827 try {
1828 renameWifiDisplayInternal(address, alias);
1829 } finally {
1830 Binder.restoreCallingIdentity(token);
1831 }
1832 }
1833
1834 @Override // Binder call
1835 public void forgetWifiDisplay(String address) {
1836 if (address == null) {
1837 throw new IllegalArgumentException("address must not be null");
1838 }
1839 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1840 "Permission required to forget to a wifi display");
1841
1842 final long token = Binder.clearCallingIdentity();
1843 try {
1844 forgetWifiDisplayInternal(address);
1845 } finally {
1846 Binder.restoreCallingIdentity(token);
1847 }
1848 }
1849
1850 @Override // Binder call
1851 public void pauseWifiDisplay() {
1852 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1853 "Permission required to pause a wifi display session");
1854
1855 final long token = Binder.clearCallingIdentity();
1856 try {
1857 pauseWifiDisplayInternal();
1858 } finally {
1859 Binder.restoreCallingIdentity(token);
1860 }
1861 }
1862
1863 @Override // Binder call
1864 public void resumeWifiDisplay() {
1865 mContext.enforceCallingOrSelfPermission(Manifest.permission.CONFIGURE_WIFI_DISPLAY,
1866 "Permission required to resume a wifi display session");
1867
1868 final long token = Binder.clearCallingIdentity();
1869 try {
1870 resumeWifiDisplayInternal();
1871 } finally {
1872 Binder.restoreCallingIdentity(token);
1873 }
1874 }
1875
1876 @Override // Binder call
1877 public WifiDisplayStatus getWifiDisplayStatus() {
1878 // This request does not require special permissions.
1879 // Any app can get information about available wifi displays.
1880
1881 final long token = Binder.clearCallingIdentity();
1882 try {
1883 return getWifiDisplayStatusInternal();
1884 } finally {
1885 Binder.restoreCallingIdentity(token);
1886 }
1887 }
1888
1889 @Override // Binder call
Michael Wright1c9977b2016-07-12 13:30:10 -07001890 public void requestColorMode(int displayId, int colorMode) {
Michael Wright58e829f2015-09-15 00:13:26 +01001891 mContext.enforceCallingOrSelfPermission(
Michael Wright1c9977b2016-07-12 13:30:10 -07001892 Manifest.permission.CONFIGURE_DISPLAY_COLOR_MODE,
1893 "Permission required to change the display color mode");
Michael Wright58e829f2015-09-15 00:13:26 +01001894 final long token = Binder.clearCallingIdentity();
1895 try {
Michael Wright1c9977b2016-07-12 13:30:10 -07001896 requestColorModeInternal(displayId, colorMode);
Michael Wright58e829f2015-09-15 00:13:26 +01001897 } finally {
1898 Binder.restoreCallingIdentity(token);
1899 }
1900 }
1901
1902 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001903 public int createVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wrightc39d47a2014-07-08 18:07:36 -07001904 IMediaProjection projection, String packageName, String name,
Santos Cordonee8931e2017-04-05 10:31:15 -07001905 int width, int height, int densityDpi, Surface surface, int flags,
1906 String uniqueId) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001907 final int callingUid = Binder.getCallingUid();
1908 if (!validatePackageName(callingUid, packageName)) {
1909 throw new SecurityException("packageName must match the calling uid");
1910 }
Michael Wright75ee9fc2014-09-01 19:55:22 -07001911 if (callback == null) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001912 throw new IllegalArgumentException("appToken must not be null");
1913 }
1914 if (TextUtils.isEmpty(name)) {
1915 throw new IllegalArgumentException("name must be non-null and non-empty");
1916 }
1917 if (width <= 0 || height <= 0 || densityDpi <= 0) {
1918 throw new IllegalArgumentException("width, height, and densityDpi must be "
1919 + "greater than 0");
1920 }
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001921 if (surface != null && surface.isSingleBuffered()) {
Pablo Ceballosaff2f942016-07-29 14:49:55 -07001922 throw new IllegalArgumentException("Surface can't be single-buffered");
1923 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07001924
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001925 if ((flags & VIRTUAL_DISPLAY_FLAG_PUBLIC) != 0) {
1926 flags |= VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
1927
1928 // Public displays can't be allowed to show content when locked.
Andrii Kulian7211d2e2017-01-27 15:58:05 -08001929 if ((flags & VIRTUAL_DISPLAY_FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD) != 0) {
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001930 throw new IllegalArgumentException(
1931 "Public display must not be marked as SHOW_WHEN_LOCKED_INSECURE");
1932 }
Michael Wright6720be42014-07-29 19:14:16 -07001933 }
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001934 if ((flags & VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY) != 0) {
1935 flags &= ~VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
Michael Wright6720be42014-07-29 19:14:16 -07001936 }
1937
Michael Wrightc39d47a2014-07-08 18:07:36 -07001938 if (projection != null) {
1939 try {
1940 if (!getProjectionService().isValidMediaProjection(projection)) {
1941 throw new SecurityException("Invalid media projection");
1942 }
Michael Wright6720be42014-07-29 19:14:16 -07001943 flags = projection.applyVirtualDisplayFlags(flags);
Michael Wrightc39d47a2014-07-08 18:07:36 -07001944 } catch (RemoteException e) {
Michael Wright6720be42014-07-29 19:14:16 -07001945 throw new SecurityException("unable to validate media projection or flags");
Michael Wrightc39d47a2014-07-08 18:07:36 -07001946 }
1947 }
1948
Michael Wright6720be42014-07-29 19:14:16 -07001949 if (callingUid != Process.SYSTEM_UID &&
Andrii Kulianfc8f82b2017-01-26 13:17:27 -08001950 (flags & VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001951 if (!canProjectVideo(projection)) {
1952 throw new SecurityException("Requires CAPTURE_VIDEO_OUTPUT or "
1953 + "CAPTURE_SECURE_VIDEO_OUTPUT permission, or an appropriate "
1954 + "MediaProjection token in order to create a screen sharing virtual "
1955 + "display.");
1956 }
1957 }
Santos Cordonb6992f22018-01-30 14:51:20 -08001958 if (callingUid != Process.SYSTEM_UID && (flags & VIRTUAL_DISPLAY_FLAG_SECURE) != 0) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07001959 if (!canProjectSecureVideo(projection)) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08001960 throw new SecurityException("Requires CAPTURE_SECURE_VIDEO_OUTPUT "
Michael Wrightc39d47a2014-07-08 18:07:36 -07001961 + "or an appropriate MediaProjection token to create a "
1962 + "secure virtual display.");
Jeff Brown4ccb8232014-01-16 22:16:42 -08001963 }
1964 }
1965
1966 final long token = Binder.clearCallingIdentity();
1967 try {
Santos Cordonee8931e2017-04-05 10:31:15 -07001968 return createVirtualDisplayInternal(callback, projection, callingUid, packageName,
1969 name, width, height, densityDpi, surface, flags, uniqueId);
Jeff Brown4ccb8232014-01-16 22:16:42 -08001970 } finally {
1971 Binder.restoreCallingIdentity(token);
1972 }
1973 }
1974
1975 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001976 public void resizeVirtualDisplay(IVirtualDisplayCallback callback,
Michael Wright01e840f2014-06-26 16:03:25 -07001977 int width, int height, int densityDpi) {
1978 final long token = Binder.clearCallingIdentity();
1979 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001980 resizeVirtualDisplayInternal(callback.asBinder(), width, height, densityDpi);
Michael Wright01e840f2014-06-26 16:03:25 -07001981 } finally {
1982 Binder.restoreCallingIdentity(token);
1983 }
1984 }
1985
1986 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07001987 public void setVirtualDisplaySurface(IVirtualDisplayCallback callback, Surface surface) {
Pablo Ceballoseb3370d2016-08-31 15:00:17 -07001988 if (surface != null && surface.isSingleBuffered()) {
1989 throw new IllegalArgumentException("Surface can't be single-buffered");
1990 }
Jeff Brown92207df2014-04-16 13:16:07 -07001991 final long token = Binder.clearCallingIdentity();
1992 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07001993 setVirtualDisplaySurfaceInternal(callback.asBinder(), surface);
Jeff Brown92207df2014-04-16 13:16:07 -07001994 } finally {
1995 Binder.restoreCallingIdentity(token);
1996 }
1997 }
1998
1999 @Override // Binder call
Michael Wright75ee9fc2014-09-01 19:55:22 -07002000 public void releaseVirtualDisplay(IVirtualDisplayCallback callback) {
Jeff Brown4ccb8232014-01-16 22:16:42 -08002001 final long token = Binder.clearCallingIdentity();
2002 try {
Michael Wright75ee9fc2014-09-01 19:55:22 -07002003 releaseVirtualDisplayInternal(callback.asBinder());
Jeff Brown4ccb8232014-01-16 22:16:42 -08002004 } finally {
2005 Binder.restoreCallingIdentity(token);
2006 }
2007 }
2008
2009 @Override // Binder call
chaviwda4c6942018-11-07 15:52:56 -08002010 public void setVirtualDisplayState(IVirtualDisplayCallback callback, boolean isOn) {
2011 final long token = Binder.clearCallingIdentity();
2012 try {
2013 setVirtualDisplayStateInternal(callback.asBinder(), isOn);
2014 } finally {
2015 Binder.restoreCallingIdentity(token);
2016 }
2017 }
2018
2019 @Override // Binder call
Jeff Brown4ccb8232014-01-16 22:16:42 -08002020 public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06002021 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
Jeff Brown4ccb8232014-01-16 22:16:42 -08002022
2023 final long token = Binder.clearCallingIdentity();
2024 try {
2025 dumpInternal(pw);
2026 } finally {
2027 Binder.restoreCallingIdentity(token);
2028 }
2029 }
2030
Kenny Guy22bd0442017-10-26 00:15:54 +01002031 @Override // Binder call
Kenny Guy29aa30e2017-11-30 13:43:46 +00002032 public ParceledListSlice<BrightnessChangeEvent> getBrightnessEvents(String callingPackage) {
Kenny Guy22bd0442017-10-26 00:15:54 +01002033 mContext.enforceCallingOrSelfPermission(
2034 Manifest.permission.BRIGHTNESS_SLIDER_USAGE,
2035 "Permission to read brightness events.");
Kenny Guy29aa30e2017-11-30 13:43:46 +00002036
2037 final int callingUid = Binder.getCallingUid();
2038 AppOpsManager appOpsManager = mContext.getSystemService(AppOpsManager.class);
Jeff Sharkeyd9311192018-04-16 16:50:34 +00002039 final int mode = appOpsManager.noteOp(AppOpsManager.OP_GET_USAGE_STATS,
Kenny Guy29aa30e2017-11-30 13:43:46 +00002040 callingUid, callingPackage);
2041 final boolean hasUsageStats;
2042 if (mode == AppOpsManager.MODE_DEFAULT) {
2043 // The default behavior here is to check if PackageManager has given the app
2044 // permission.
2045 hasUsageStats = mContext.checkCallingPermission(
2046 Manifest.permission.PACKAGE_USAGE_STATS)
2047 == PackageManager.PERMISSION_GRANTED;
2048 } else {
2049 hasUsageStats = mode == AppOpsManager.MODE_ALLOWED;
2050 }
2051
2052 final int userId = UserHandle.getUserId(callingUid);
Kenny Guy22bd0442017-10-26 00:15:54 +01002053 final long token = Binder.clearCallingIdentity();
2054 try {
Michael Wright144aac92017-12-21 18:37:41 +00002055 synchronized (mSyncRoot) {
2056 return mDisplayPowerController.getBrightnessEvents(userId, hasUsageStats);
2057 }
Kenny Guy22bd0442017-10-26 00:15:54 +01002058 } finally {
2059 Binder.restoreCallingIdentity(token);
2060 }
2061 }
2062
Michael Wrighteef0e132017-11-21 17:57:52 +00002063 @Override // Binder call
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002064 public ParceledListSlice<AmbientBrightnessDayStats> getAmbientBrightnessStats() {
2065 mContext.enforceCallingOrSelfPermission(
2066 Manifest.permission.ACCESS_AMBIENT_LIGHT_STATS,
2067 "Permission required to to access ambient light stats.");
2068 final int callingUid = Binder.getCallingUid();
2069 final int userId = UserHandle.getUserId(callingUid);
2070 final long token = Binder.clearCallingIdentity();
2071 try {
2072 synchronized (mSyncRoot) {
2073 return mDisplayPowerController.getAmbientBrightnessStats(userId);
2074 }
2075 } finally {
2076 Binder.restoreCallingIdentity(token);
2077 }
2078 }
2079
2080 @Override // Binder call
Michael Wrighteef0e132017-11-21 17:57:52 +00002081 public void setBrightnessConfigurationForUser(
Kenny Guy05ce8092018-01-17 13:44:20 +00002082 BrightnessConfiguration c, @UserIdInt int userId, String packageName) {
Michael Wrighteef0e132017-11-21 17:57:52 +00002083 mContext.enforceCallingOrSelfPermission(
2084 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
2085 "Permission required to change the display's brightness configuration");
2086 if (userId != UserHandle.getCallingUserId()) {
2087 mContext.enforceCallingOrSelfPermission(
2088 Manifest.permission.INTERACT_ACROSS_USERS,
2089 "Permission required to change the display brightness"
2090 + " configuration of another user");
2091 }
Kenny Guy05ce8092018-01-17 13:44:20 +00002092 if (packageName != null && !validatePackageName(getCallingUid(), packageName)) {
2093 packageName = null;
2094 }
Michael Wrighteef0e132017-11-21 17:57:52 +00002095 final long token = Binder.clearCallingIdentity();
2096 try {
Kenny Guy05ce8092018-01-17 13:44:20 +00002097 setBrightnessConfigurationForUserInternal(c, userId, packageName);
Michael Wrighteef0e132017-11-21 17:57:52 +00002098 } finally {
2099 Binder.restoreCallingIdentity(token);
2100 }
2101 }
2102
Michael Wrightd8460232018-01-16 18:04:59 +00002103 @Override // Binder call
Kenny Guy6d1009f2018-03-14 14:28:23 +00002104 public BrightnessConfiguration getBrightnessConfigurationForUser(int userId) {
2105 mContext.enforceCallingOrSelfPermission(
2106 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
2107 "Permission required to read the display's brightness configuration");
2108 if (userId != UserHandle.getCallingUserId()) {
2109 mContext.enforceCallingOrSelfPermission(
2110 Manifest.permission.INTERACT_ACROSS_USERS,
2111 "Permission required to read the display brightness"
2112 + " configuration of another user");
2113 }
2114 final long token = Binder.clearCallingIdentity();
2115 try {
2116 final int userSerial = getUserManager().getUserSerialNumber(userId);
2117 synchronized (mSyncRoot) {
2118 BrightnessConfiguration config =
2119 mPersistentDataStore.getBrightnessConfiguration(userSerial);
2120 if (config == null) {
2121 config = mDisplayPowerController.getDefaultBrightnessConfiguration();
2122 }
2123 return config;
2124 }
2125 } finally {
2126 Binder.restoreCallingIdentity(token);
2127 }
2128 }
2129
2130 @Override // Binder call
2131 public BrightnessConfiguration getDefaultBrightnessConfiguration() {
2132 mContext.enforceCallingOrSelfPermission(
2133 Manifest.permission.CONFIGURE_DISPLAY_BRIGHTNESS,
2134 "Permission required to read the display's default brightness configuration");
2135 final long token = Binder.clearCallingIdentity();
2136 try {
2137 synchronized (mSyncRoot) {
2138 return mDisplayPowerController.getDefaultBrightnessConfiguration();
2139 }
2140 } finally {
2141 Binder.restoreCallingIdentity(token);
2142 }
2143 }
2144
2145 @Override // Binder call
Michael Wrightd8460232018-01-16 18:04:59 +00002146 public void setTemporaryBrightness(int brightness) {
2147 mContext.enforceCallingOrSelfPermission(
2148 Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS,
2149 "Permission required to set the display's brightness");
2150 final long token = Binder.clearCallingIdentity();
2151 try {
2152 synchronized (mSyncRoot) {
2153 mDisplayPowerController.setTemporaryBrightness(brightness);
2154 }
2155 } finally {
2156 Binder.restoreCallingIdentity(token);
2157 }
2158 }
2159
2160 @Override // Binder call
2161 public void setTemporaryAutoBrightnessAdjustment(float adjustment) {
2162 mContext.enforceCallingOrSelfPermission(
2163 Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS,
2164 "Permission required to set the display's auto brightness adjustment");
2165 final long token = Binder.clearCallingIdentity();
2166 try {
2167 synchronized (mSyncRoot) {
2168 mDisplayPowerController.setTemporaryAutoBrightnessAdjustment(adjustment);
2169 }
2170 } finally {
2171 Binder.restoreCallingIdentity(token);
2172 }
2173 }
2174
Dan Gittik7a32fba2018-03-28 12:19:38 +01002175 @Override // Binder call
2176 public void onShellCommand(FileDescriptor in, FileDescriptor out,
2177 FileDescriptor err, String[] args, ShellCallback callback,
2178 ResultReceiver resultReceiver) {
2179 final long token = Binder.clearCallingIdentity();
2180 try {
2181 DisplayManagerShellCommand command = new DisplayManagerShellCommand(this);
2182 command.exec(this, in, out, err, args, callback, resultReceiver);
2183 } finally {
2184 Binder.restoreCallingIdentity(token);
2185 }
2186 }
2187
Dan Gittik122df862018-03-28 16:59:22 +01002188 @Override // Binder call
2189 public Curve getMinimumBrightnessCurve() {
2190 final long token = Binder.clearCallingIdentity();
2191 try {
2192 return getMinimumBrightnessCurveInternal();
2193 } finally {
2194 Binder.restoreCallingIdentity(token);
2195 }
2196 }
2197
Peiyong Lin277eaff2019-01-16 16:18:22 -08002198 @Override // Binder call
2199 public int getPreferredWideGamutColorSpaceId() {
2200 final long token = Binder.clearCallingIdentity();
2201 try {
2202 return getPreferredWideGamutColorSpaceIdInternal();
2203 } finally {
2204 Binder.restoreCallingIdentity(token);
2205 }
2206 }
2207
Dan Gittik7a32fba2018-03-28 12:19:38 +01002208 void setBrightness(int brightness) {
2209 Settings.System.putIntForUser(mContext.getContentResolver(),
2210 Settings.System.SCREEN_BRIGHTNESS, brightness, UserHandle.USER_CURRENT);
2211 }
2212
2213 void resetBrightnessConfiguration() {
2214 setBrightnessConfigurationForUserInternal(null, mContext.getUserId(),
2215 mContext.getPackageName());
2216 }
2217
Dan Gittika5a2d632019-01-09 14:25:29 +00002218 void setAutoBrightnessLoggingEnabled(boolean enabled) {
2219 if (mDisplayPowerController != null) {
2220 synchronized (mSyncRoot) {
2221 mDisplayPowerController.setAutoBrightnessLoggingEnabled(enabled);
2222 }
2223 }
2224 }
2225
Dan Gittik8dbd7e92018-12-03 15:35:53 +00002226 void setDisplayWhiteBalanceLoggingEnabled(boolean enabled) {
2227 if (mDisplayPowerController != null) {
2228 synchronized (mSyncRoot) {
2229 mDisplayPowerController.setDisplayWhiteBalanceLoggingEnabled(enabled);
2230 }
2231 }
2232 }
2233
2234 void setAmbientColorTemperatureOverride(float cct) {
2235 if (mDisplayPowerController != null) {
2236 synchronized (mSyncRoot) {
2237 mDisplayPowerController.setAmbientColorTemperatureOverride(cct);
2238 }
2239 }
2240 }
2241
Jeff Brown4ccb8232014-01-16 22:16:42 -08002242 private boolean validatePackageName(int uid, String packageName) {
2243 if (packageName != null) {
2244 String[] packageNames = mContext.getPackageManager().getPackagesForUid(uid);
2245 if (packageNames != null) {
2246 for (String n : packageNames) {
2247 if (n.equals(packageName)) {
2248 return true;
2249 }
2250 }
2251 }
2252 }
2253 return false;
2254 }
Michael Wrightc39d47a2014-07-08 18:07:36 -07002255
2256 private boolean canProjectVideo(IMediaProjection projection) {
2257 if (projection != null) {
2258 try {
2259 if (projection.canProjectVideo()) {
2260 return true;
2261 }
2262 } catch (RemoteException e) {
2263 Slog.e(TAG, "Unable to query projection service for permissions", e);
2264 }
2265 }
2266 if (mContext.checkCallingPermission(
2267 android.Manifest.permission.CAPTURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07002268 == PackageManager.PERMISSION_GRANTED) {
Michael Wrightc39d47a2014-07-08 18:07:36 -07002269 return true;
2270 }
2271 return canProjectSecureVideo(projection);
2272 }
2273
2274 private boolean canProjectSecureVideo(IMediaProjection projection) {
2275 if (projection != null) {
2276 try {
2277 if (projection.canProjectSecureVideo()){
2278 return true;
2279 }
2280 } catch (RemoteException e) {
2281 Slog.e(TAG, "Unable to query projection service for permissions", e);
2282 }
2283 }
2284 return mContext.checkCallingPermission(
2285 android.Manifest.permission.CAPTURE_SECURE_VIDEO_OUTPUT)
Michael Wright0ccc2b02014-07-24 18:20:41 -07002286 == PackageManager.PERMISSION_GRANTED;
Michael Wrightc39d47a2014-07-08 18:07:36 -07002287 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08002288 }
2289
2290 private final class LocalService extends DisplayManagerInternal {
2291 @Override
Jeff Brown037c33e2014-04-09 00:31:55 -07002292 public void initPowerManagement(final DisplayPowerCallbacks callbacks, Handler handler,
Jeff Brownad9ef192014-04-08 17:26:30 -07002293 SensorManager sensorManager) {
2294 synchronized (mSyncRoot) {
Jeff Brown037c33e2014-04-09 00:31:55 -07002295 DisplayBlanker blanker = new DisplayBlanker() {
2296 @Override
Jeff Brown5d6443b2015-04-10 20:15:01 -07002297 public void requestDisplayState(int state, int brightness) {
Jeff Brown037c33e2014-04-09 00:31:55 -07002298 // The order of operations is important for legacy reasons.
2299 if (state == Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07002300 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07002301 }
2302
2303 callbacks.onDisplayStateChange(state);
2304
2305 if (state != Display.STATE_OFF) {
Jeff Brown5d6443b2015-04-10 20:15:01 -07002306 requestGlobalDisplayStateInternal(state, brightness);
Jeff Brown037c33e2014-04-09 00:31:55 -07002307 }
2308 }
2309 };
Jeff Brownad9ef192014-04-08 17:26:30 -07002310 mDisplayPowerController = new DisplayPowerController(
Jeff Brown037c33e2014-04-09 00:31:55 -07002311 mContext, callbacks, handler, sensorManager, blanker);
Jeff Brownad9ef192014-04-08 17:26:30 -07002312 }
Michael Wrighteef0e132017-11-21 17:57:52 +00002313
2314 mHandler.sendEmptyMessage(MSG_LOAD_BRIGHTNESS_CONFIGURATION);
Jeff Brownad9ef192014-04-08 17:26:30 -07002315 }
2316
2317 @Override
2318 public boolean requestPowerState(DisplayPowerRequest request,
2319 boolean waitForNegativeProximity) {
Michael Wrighteef0e132017-11-21 17:57:52 +00002320 synchronized (mSyncRoot) {
2321 return mDisplayPowerController.requestPowerState(request,
2322 waitForNegativeProximity);
2323 }
Jeff Brownad9ef192014-04-08 17:26:30 -07002324 }
2325
2326 @Override
2327 public boolean isProximitySensorAvailable() {
Michael Wrighteef0e132017-11-21 17:57:52 +00002328 synchronized (mSyncRoot) {
2329 return mDisplayPowerController.isProximitySensorAvailable();
2330 }
Jeff Brownad9ef192014-04-08 17:26:30 -07002331 }
2332
2333 @Override
Riddle Hsu654a6f92018-07-13 22:59:36 +08002334 public boolean screenshot(int displayId, Surface outSurface) {
Dominik Laskowskie912ae32019-01-26 11:19:36 -08002335 return screenshotInternal(displayId, outSurface);
Riddle Hsu654a6f92018-07-13 22:59:36 +08002336 }
2337
2338 @Override
Jeff Brown4ccb8232014-01-16 22:16:42 -08002339 public DisplayInfo getDisplayInfo(int displayId) {
2340 return getDisplayInfoInternal(displayId, Process.myUid());
2341 }
2342
2343 @Override
2344 public void registerDisplayTransactionListener(DisplayTransactionListener listener) {
2345 if (listener == null) {
2346 throw new IllegalArgumentException("listener must not be null");
2347 }
2348
2349 registerDisplayTransactionListenerInternal(listener);
2350 }
2351
2352 @Override
2353 public void unregisterDisplayTransactionListener(DisplayTransactionListener listener) {
2354 if (listener == null) {
2355 throw new IllegalArgumentException("listener must not be null");
2356 }
2357
2358 unregisterDisplayTransactionListenerInternal(listener);
2359 }
2360
2361 @Override
2362 public void setDisplayInfoOverrideFromWindowManager(int displayId, DisplayInfo info) {
2363 setDisplayInfoOverrideFromWindowManagerInternal(displayId, info);
2364 }
2365
2366 @Override
Andrii Kuliancd097992017-03-23 18:31:59 -07002367 public void getNonOverrideDisplayInfo(int displayId, DisplayInfo outInfo) {
2368 getNonOverrideDisplayInfoInternal(displayId, outInfo);
2369 }
2370
2371 @Override
Robert Carrae606b42018-02-15 15:36:23 -08002372 public void performTraversal(SurfaceControl.Transaction t) {
2373 performTraversalInternal(t);
Jeff Brown4ccb8232014-01-16 22:16:42 -08002374 }
2375
2376 @Override
Michael Wright3f145a22014-07-22 19:46:03 -07002377 public void setDisplayProperties(int displayId, boolean hasContent,
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -07002378 float requestedRefreshRate, int requestedMode, boolean inTraversal) {
2379 setDisplayPropertiesInternal(displayId, hasContent, requestedRefreshRate,
2380 requestedMode, inTraversal);
Jeff Brown4ccb8232014-01-16 22:16:42 -08002381 }
Filip Gruszczynskid2e86402015-02-19 13:05:03 -08002382
2383 @Override
2384 public void setDisplayOffsets(int displayId, int x, int y) {
2385 setDisplayOffsetsInternal(displayId, x, y);
2386 }
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002387
2388 @Override
Sam Lin4c3ac2b2019-02-18 04:50:26 -08002389 public void setDisplayScalingDisabled(int displayId, boolean disableScaling) {
2390 setDisplayScalingDisabledInternal(displayId, disableScaling);
2391 }
2392
2393 @Override
Andrii Kulianfb1bf692017-01-17 11:17:34 -08002394 public void setDisplayAccessUIDs(SparseArray<IntArray> newDisplayAccessUIDs) {
2395 setDisplayAccessUIDsInternal(newDisplayAccessUIDs);
2396 }
2397
2398 @Override
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002399 public void persistBrightnessTrackerState() {
Michael Wright144aac92017-12-21 18:37:41 +00002400 synchronized (mSyncRoot) {
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +00002401 mDisplayPowerController.persistBrightnessTrackerState();
Michael Wright144aac92017-12-21 18:37:41 +00002402 }
Kenny Guycfe7b702017-11-14 21:04:58 +00002403 }
Adrian Roose1d68cd2018-01-17 12:54:50 +01002404
2405 @Override
2406 public void onOverlayChanged() {
2407 synchronized (mSyncRoot) {
Adrian Roos898ec382018-01-17 12:54:50 +01002408 for (int i = 0; i < mDisplayDevices.size(); i++) {
2409 mDisplayDevices.get(i).onOverlayChangedLocked();
Adrian Roose1d68cd2018-01-17 12:54:50 +01002410 }
2411 }
2412 }
Kevin DuBoisbf76b11b2018-09-04 09:14:15 -07002413
2414 @Override
2415 public DisplayedContentSamplingAttributes getDisplayedContentSamplingAttributes(
2416 int displayId) {
2417 return getDisplayedContentSamplingAttributesInternal(displayId);
2418 }
2419
2420 @Override
2421 public boolean setDisplayedContentSamplingEnabled(
2422 int displayId, boolean enable, int componentMask, int maxFrames) {
2423 return setDisplayedContentSamplingEnabledInternal(
2424 displayId, enable, componentMask, maxFrames);
2425 }
2426
2427 @Override
2428 public DisplayedContentSample getDisplayedContentSample(int displayId,
2429 long maxFrames, long timestamp) {
2430 return getDisplayedContentSampleInternal(displayId, maxFrames, timestamp);
2431 }
2432
Jeff Brown4ccb8232014-01-16 22:16:42 -08002433 }
Jeff Brownfa25bf52012-07-23 19:26:30 -07002434}