1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
|
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.hardware.display;
import static android.hardware.display.DisplayManager.EventsMask;
import static android.view.Display.HdrCapabilities.HdrType;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.PropertyInvalidatedCache;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.Context;
import android.content.pm.ParceledListSlice;
import android.content.res.Resources;
import android.graphics.ColorSpace;
import android.graphics.Point;
import android.hardware.display.DisplayManager.DisplayListener;
import android.hardware.graphics.common.DisplayDecorationSupport;
import android.media.projection.IMediaProjection;
import android.media.projection.MediaProjection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.Log;
import android.util.Pair;
import android.util.SparseArray;
import android.view.Display;
import android.view.DisplayAdjustments;
import android.view.DisplayInfo;
import android.view.Surface;
import com.android.internal.annotations.VisibleForTesting;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Executor;
/**
* Manager communication with the display manager service on behalf of
* an application process. You're probably looking for {@link DisplayManager}.
*
* @hide
*/
public final class DisplayManagerGlobal {
private static final String TAG = "DisplayManager";
private static final boolean DEBUG = false;
// True if display info and display ids should be cached.
//
// FIXME: The cache is currently disabled because it's unclear whether we have the
// necessary guarantees that the caches will always be flushed before clients
// attempt to observe their new state. For example, depending on the order
// in which the binder transactions take place, we might have a problem where
// an application could start processing a configuration change due to a display
// orientation change before the display info cache has actually been invalidated.
private static final boolean USE_CACHE = false;
@IntDef(prefix = {"SWITCHING_TYPE_"}, value = {
EVENT_DISPLAY_ADDED,
EVENT_DISPLAY_CHANGED,
EVENT_DISPLAY_REMOVED,
EVENT_DISPLAY_BRIGHTNESS_CHANGED
})
@Retention(RetentionPolicy.SOURCE)
public @interface DisplayEvent {}
public static final int EVENT_DISPLAY_ADDED = 1;
public static final int EVENT_DISPLAY_CHANGED = 2;
public static final int EVENT_DISPLAY_REMOVED = 3;
public static final int EVENT_DISPLAY_BRIGHTNESS_CHANGED = 4;
@UnsupportedAppUsage
private static DisplayManagerGlobal sInstance;
// Guarded by mLock
private boolean mDispatchNativeCallbacks = false;
private float mNativeCallbackReportedRefreshRate;
private final Object mLock = new Object();
@UnsupportedAppUsage
private final IDisplayManager mDm;
private DisplayManagerCallback mCallback;
private @EventsMask long mRegisteredEventsMask = 0;
private final ArrayList<DisplayListenerDelegate> mDisplayListeners = new ArrayList<>();
private final SparseArray<DisplayInfo> mDisplayInfoCache = new SparseArray<>();
private final ColorSpace mWideColorSpace;
private int[] mDisplayIdCache;
private int mWifiDisplayScanNestCount;
@VisibleForTesting
public DisplayManagerGlobal(IDisplayManager dm) {
mDm = dm;
try {
mWideColorSpace =
ColorSpace.get(
ColorSpace.Named.values()[mDm.getPreferredWideGamutColorSpaceId()]);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
private PropertyInvalidatedCache<Integer, DisplayInfo> mDisplayCache =
new PropertyInvalidatedCache<Integer, DisplayInfo>(
8, // size of display cache
CACHE_KEY_DISPLAY_INFO_PROPERTY) {
@Override
public DisplayInfo recompute(Integer id) {
try {
return mDm.getDisplayInfo(id);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
};
/**
* Gets an instance of the display manager global singleton.
*
* @return The display manager instance, may be null early in system startup
* before the display manager has been fully initialized.
*/
@UnsupportedAppUsage
public static DisplayManagerGlobal getInstance() {
synchronized (DisplayManagerGlobal.class) {
if (sInstance == null) {
IBinder b = ServiceManager.getService(Context.DISPLAY_SERVICE);
if (b != null) {
sInstance = new DisplayManagerGlobal(IDisplayManager.Stub.asInterface(b));
}
}
return sInstance;
}
}
/**
* Get information about a particular logical display.
*
* @param displayId The logical display id.
* @return Information about the specified display, or null if it does not exist.
* This object belongs to an internal cache and should be treated as if it were immutable.
*/
@UnsupportedAppUsage
public DisplayInfo getDisplayInfo(int displayId) {
synchronized (mLock) {
return getDisplayInfoLocked(displayId);
}
}
/**
* Gets information about a particular logical display
* See {@link getDisplayInfo}, but assumes that {@link mLock} is held
*/
private @Nullable DisplayInfo getDisplayInfoLocked(int displayId) {
DisplayInfo info = null;
if (mDisplayCache != null) {
info = mDisplayCache.query(displayId);
} else {
try {
info = mDm.getDisplayInfo(displayId);
} catch (RemoteException ex) {
ex.rethrowFromSystemServer();
}
}
if (info == null) {
return null;
}
registerCallbackIfNeededLocked();
if (DEBUG) {
Log.d(TAG, "getDisplayInfo: displayId=" + displayId + ", info=" + info);
}
return info;
}
/**
* Gets all currently valid logical display ids.
*
* @return An array containing all display ids.
*/
@UnsupportedAppUsage
public int[] getDisplayIds() {
return getDisplayIds(/* includeDisabled= */ false);
}
/**
* Gets all currently valid logical display ids.
*
* @param includeDisabled True if the returned list of displays includes disabled displays.
* @return An array containing all display ids.
*/
public int[] getDisplayIds(boolean includeDisabled) {
try {
synchronized (mLock) {
if (USE_CACHE) {
if (mDisplayIdCache != null) {
return mDisplayIdCache;
}
}
int[] displayIds = mDm.getDisplayIds(includeDisabled);
if (USE_CACHE) {
mDisplayIdCache = displayIds;
}
registerCallbackIfNeededLocked();
return displayIds;
}
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* Check if specified UID's content is present on display and should be granted access to it.
*
* @param uid UID to be checked.
* @param displayId id of the display where presence of the content is checked.
* @return {@code true} if UID is present on display, {@code false} otherwise.
*/
public boolean isUidPresentOnDisplay(int uid, int displayId) {
try {
return mDm.isUidPresentOnDisplay(uid, displayId);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* Gets information about a logical display.
*
* The display metrics may be adjusted to provide compatibility
* for legacy applications or limited screen areas.
*
* @param displayId The logical display id.
* @param daj The compatibility info and activityToken.
* @return The display object, or null if there is no display with the given id.
*/
public Display getCompatibleDisplay(int displayId, DisplayAdjustments daj) {
DisplayInfo displayInfo = getDisplayInfo(displayId);
if (displayInfo == null) {
return null;
}
return new Display(this, displayId, displayInfo, daj);
}
/**
* Gets information about a logical display.
*
* The display metrics may be adjusted to provide compatibility
* for legacy applications or limited screen areas.
*
* @param displayId The logical display id.
* @param resources Resources providing compatibility info.
* @return The display object, or null if there is no display with the given id.
*/
public Display getCompatibleDisplay(int displayId, Resources resources) {
DisplayInfo displayInfo = getDisplayInfo(displayId);
if (displayInfo == null) {
return null;
}
return new Display(this, displayId, displayInfo, resources);
}
/**
* Gets information about a logical display without applying any compatibility metrics.
*
* @param displayId The logical display id.
* @return The display object, or null if there is no display with the given id.
*/
@UnsupportedAppUsage
public Display getRealDisplay(int displayId) {
return getCompatibleDisplay(displayId, DisplayAdjustments.DEFAULT_DISPLAY_ADJUSTMENTS);
}
/**
* Register a listener for display-related changes.
*
* @param listener The listener that will be called when display changes occur.
* @param handler Handler for the thread that will be receiving the callbacks. May be null.
* If null, listener will use the handler for the current thread, and if still null,
* the handler for the main thread.
* If that is still null, a runtime exception will be thrown.
*/
public void registerDisplayListener(@NonNull DisplayListener listener,
@Nullable Handler handler, @EventsMask long eventsMask) {
if (listener == null) {
throw new IllegalArgumentException("listener must not be null");
}
if (eventsMask == 0) {
throw new IllegalArgumentException("The set of events to listen to must not be empty.");
}
synchronized (mLock) {
int index = findDisplayListenerLocked(listener);
if (index < 0) {
Looper looper = getLooperForHandler(handler);
mDisplayListeners.add(new DisplayListenerDelegate(listener, looper, eventsMask));
registerCallbackIfNeededLocked();
} else {
mDisplayListeners.get(index).setEventsMask(eventsMask);
}
updateCallbackIfNeededLocked();
}
}
public void unregisterDisplayListener(DisplayListener listener) {
if (listener == null) {
throw new IllegalArgumentException("listener must not be null");
}
synchronized (mLock) {
int index = findDisplayListenerLocked(listener);
if (index >= 0) {
DisplayListenerDelegate d = mDisplayListeners.get(index);
d.clearEvents();
mDisplayListeners.remove(index);
updateCallbackIfNeededLocked();
}
}
}
private static Looper getLooperForHandler(@Nullable Handler handler) {
Looper looper = handler != null ? handler.getLooper() : Looper.myLooper();
if (looper == null) {
looper = Looper.getMainLooper();
}
if (looper == null) {
throw new RuntimeException("Could not get Looper for the UI thread.");
}
return looper;
}
private int findDisplayListenerLocked(DisplayListener listener) {
final int numListeners = mDisplayListeners.size();
for (int i = 0; i < numListeners; i++) {
if (mDisplayListeners.get(i).mListener == listener) {
return i;
}
}
return -1;
}
@EventsMask
private int calculateEventsMaskLocked() {
int mask = 0;
final int numListeners = mDisplayListeners.size();
for (int i = 0; i < numListeners; i++) {
mask |= mDisplayListeners.get(i).mEventsMask;
}
if (mDispatchNativeCallbacks) {
mask |= DisplayManager.EVENT_FLAG_DISPLAY_ADDED
| DisplayManager.EVENT_FLAG_DISPLAY_CHANGED
| DisplayManager.EVENT_FLAG_DISPLAY_REMOVED;
}
return mask;
}
private void registerCallbackIfNeededLocked() {
if (mCallback == null) {
mCallback = new DisplayManagerCallback();
updateCallbackIfNeededLocked();
}
}
private void updateCallbackIfNeededLocked() {
int mask = calculateEventsMaskLocked();
if (mask != mRegisteredEventsMask) {
try {
mDm.registerCallbackWithEventMask(mCallback, mask);
mRegisteredEventsMask = mask;
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
}
private void handleDisplayEvent(int displayId, @DisplayEvent int event) {
synchronized (mLock) {
if (USE_CACHE) {
mDisplayInfoCache.remove(displayId);
if (event == EVENT_DISPLAY_ADDED || event == EVENT_DISPLAY_REMOVED) {
mDisplayIdCache = null;
}
}
final int numListeners = mDisplayListeners.size();
DisplayInfo info = getDisplayInfo(displayId);
for (int i = 0; i < numListeners; i++) {
mDisplayListeners.get(i).sendDisplayEvent(displayId, event, info);
}
if (event == EVENT_DISPLAY_CHANGED && mDispatchNativeCallbacks) {
// Choreographer only supports a single display, so only dispatch refresh rate
// changes for the default display.
if (displayId == Display.DEFAULT_DISPLAY) {
// We can likely save a binder hop if we attach the refresh rate onto the
// listener.
DisplayInfo display = getDisplayInfoLocked(displayId);
if (display != null
&& mNativeCallbackReportedRefreshRate != display.getRefreshRate()) {
mNativeCallbackReportedRefreshRate = display.getRefreshRate();
// Signal native callbacks if we ever set a refresh rate.
nSignalNativeCallbacks(mNativeCallbackReportedRefreshRate);
}
}
}
}
}
public void startWifiDisplayScan() {
synchronized (mLock) {
if (mWifiDisplayScanNestCount++ == 0) {
registerCallbackIfNeededLocked();
try {
mDm.startWifiDisplayScan();
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
}
}
public void stopWifiDisplayScan() {
synchronized (mLock) {
if (--mWifiDisplayScanNestCount == 0) {
try {
mDm.stopWifiDisplayScan();
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
} else if (mWifiDisplayScanNestCount < 0) {
Log.wtf(TAG, "Wifi display scan nest count became negative: "
+ mWifiDisplayScanNestCount);
mWifiDisplayScanNestCount = 0;
}
}
}
public void connectWifiDisplay(String deviceAddress) {
if (deviceAddress == null) {
throw new IllegalArgumentException("deviceAddress must not be null");
}
try {
mDm.connectWifiDisplay(deviceAddress);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
public void pauseWifiDisplay() {
try {
mDm.pauseWifiDisplay();
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
public void resumeWifiDisplay() {
try {
mDm.resumeWifiDisplay();
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
@UnsupportedAppUsage
public void disconnectWifiDisplay() {
try {
mDm.disconnectWifiDisplay();
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
public void renameWifiDisplay(String deviceAddress, String alias) {
if (deviceAddress == null) {
throw new IllegalArgumentException("deviceAddress must not be null");
}
try {
mDm.renameWifiDisplay(deviceAddress, alias);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
public void forgetWifiDisplay(String deviceAddress) {
if (deviceAddress == null) {
throw new IllegalArgumentException("deviceAddress must not be null");
}
try {
mDm.forgetWifiDisplay(deviceAddress);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
@UnsupportedAppUsage
public WifiDisplayStatus getWifiDisplayStatus() {
try {
return mDm.getWifiDisplayStatus();
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* Sets the HDR types that have been disabled by user.
* @param userDisabledHdrTypes the HDR types to disable. The HDR types are any of
*/
public void setUserDisabledHdrTypes(@HdrType int[] userDisabledHdrTypes) {
try {
mDm.setUserDisabledHdrTypes(userDisabledHdrTypes);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* Sets whether or not the user disabled HDR types are returned from
* {@link Display#getHdrCapabilities}.
*
* @param areUserDisabledHdrTypesAllowed If true, the user-disabled
* types are ignored and returned, if the display supports them. If
* false, the user-disabled types are taken into consideration and
* are never returned, even if the display supports them.
*/
public void setAreUserDisabledHdrTypesAllowed(boolean areUserDisabledHdrTypesAllowed) {
try {
mDm.setAreUserDisabledHdrTypesAllowed(areUserDisabledHdrTypesAllowed);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* Returns whether or not the user-disabled HDR types are returned from
* {@link Display#getHdrCapabilities}.
*/
public boolean areUserDisabledHdrTypesAllowed() {
try {
return mDm.areUserDisabledHdrTypesAllowed();
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* Returns the HDR formats disabled by the user.
*
*/
public int[] getUserDisabledHdrTypes() {
try {
return mDm.getUserDisabledHdrTypes();
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
public void requestColorMode(int displayId, int colorMode) {
try {
mDm.requestColorMode(displayId, colorMode);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
public VirtualDisplay createVirtualDisplay(@NonNull Context context, MediaProjection projection,
@NonNull VirtualDisplayConfig virtualDisplayConfig, VirtualDisplay.Callback callback,
@Nullable Executor executor, @Nullable Context windowContext) {
VirtualDisplayCallback callbackWrapper = new VirtualDisplayCallback(callback, executor);
IMediaProjection projectionToken = projection != null ? projection.getProjection() : null;
int displayId;
try {
displayId = mDm.createVirtualDisplay(virtualDisplayConfig, callbackWrapper,
projectionToken, context.getPackageName());
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
return createVirtualDisplayWrapper(virtualDisplayConfig, windowContext, callbackWrapper,
displayId);
}
/**
* Create a VirtualDisplay wrapper object for a newly created virtual display ; to be called
* once the display has been created in system_server.
*/
@Nullable
public VirtualDisplay createVirtualDisplayWrapper(VirtualDisplayConfig virtualDisplayConfig,
Context windowContext, IVirtualDisplayCallback callbackWrapper, int displayId) {
if (displayId < 0) {
Log.e(TAG, "Could not create virtual display: " + virtualDisplayConfig.getName());
return null;
}
Display display = getRealDisplay(displayId);
if (display == null) {
Log.wtf(TAG, "Could not obtain display info for newly created "
+ "virtual display: " + virtualDisplayConfig.getName());
try {
mDm.releaseVirtualDisplay(callbackWrapper);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
return null;
}
return new VirtualDisplay(this, display, callbackWrapper,
virtualDisplayConfig.getSurface(), windowContext);
}
public void setVirtualDisplaySurface(IVirtualDisplayCallback token, Surface surface) {
try {
mDm.setVirtualDisplaySurface(token, surface);
setVirtualDisplayState(token, surface != null);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
public void resizeVirtualDisplay(IVirtualDisplayCallback token,
int width, int height, int densityDpi) {
try {
mDm.resizeVirtualDisplay(token, width, height, densityDpi);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
public void releaseVirtualDisplay(IVirtualDisplayCallback token) {
try {
mDm.releaseVirtualDisplay(token);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
void setVirtualDisplayState(IVirtualDisplayCallback token, boolean isOn) {
try {
mDm.setVirtualDisplayState(token, isOn);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* Gets the stable device display size, in pixels.
*/
public Point getStableDisplaySize() {
try {
return mDm.getStableDisplaySize();
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* Retrieves brightness change events.
*/
public List<BrightnessChangeEvent> getBrightnessEvents(String callingPackage) {
try {
ParceledListSlice<BrightnessChangeEvent> events =
mDm.getBrightnessEvents(callingPackage);
if (events == null) {
return Collections.emptyList();
}
return events.getList();
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* Retrieves Brightness Info for the specified display.
*/
public BrightnessInfo getBrightnessInfo(int displayId) {
try {
return mDm.getBrightnessInfo(displayId);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* Gets the preferred wide gamut color space for all displays.
* The wide gamut color space is returned from composition pipeline
* based on hardware capability.
*
* @hide
*/
public ColorSpace getPreferredWideGamutColorSpace() {
return mWideColorSpace;
}
/**
* Sets the global brightness configuration for a given user.
*
* @hide
*/
public void setBrightnessConfigurationForUser(BrightnessConfiguration c, int userId,
String packageName) {
try {
mDm.setBrightnessConfigurationForUser(c, userId, packageName);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* Sets the brightness configuration for a given display.
*
* @hide
*/
public void setBrightnessConfigurationForDisplay(BrightnessConfiguration c,
String uniqueDisplayId, int userId, String packageName) {
try {
mDm.setBrightnessConfigurationForDisplay(c, uniqueDisplayId, userId, packageName);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* Gets the brightness configuration for a given display or null if one hasn't been set.
*
* @hide
*/
public BrightnessConfiguration getBrightnessConfigurationForDisplay(String uniqueDisplayId,
int userId) {
try {
return mDm.getBrightnessConfigurationForDisplay(uniqueDisplayId, userId);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* Gets the global brightness configuration for a given user or null if one hasn't been set.
*
* @hide
*/
public BrightnessConfiguration getBrightnessConfigurationForUser(int userId) {
try {
return mDm.getBrightnessConfigurationForUser(userId);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* Gets the default brightness configuration or null if one hasn't been configured.
*
* @hide
*/
public BrightnessConfiguration getDefaultBrightnessConfiguration() {
try {
return mDm.getDefaultBrightnessConfiguration();
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* Gets the last requested minimal post processing setting for the display with displayId.
*
* @hide
*/
public boolean isMinimalPostProcessingRequested(int displayId) {
try {
return mDm.isMinimalPostProcessingRequested(displayId);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* Temporarily sets the brightness of the display.
* <p>
* Requires the {@link android.Manifest.permission#CONTROL_DISPLAY_BRIGHTNESS} permission.
* </p>
*
* @param brightness The brightness value from 0.0f to 1.0f.
*
* @hide Requires signature permission.
*/
public void setTemporaryBrightness(int displayId, float brightness) {
try {
mDm.setTemporaryBrightness(displayId, brightness);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* Sets the brightness of the display.
*
* @param brightness The brightness value from 0.0f to 1.0f.
*
* @hide
*/
public void setBrightness(int displayId, float brightness) {
try {
mDm.setBrightness(displayId, brightness);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* Report whether/how the display supports DISPLAY_DECORATION.
*
* @param displayId The display whose support is being queried.
*
* @hide
*/
public DisplayDecorationSupport getDisplayDecorationSupport(int displayId) {
try {
return mDm.getDisplayDecorationSupport(displayId);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* Gets the brightness of the display.
*
* @param displayId The display from which to get the brightness
*
* @hide
*/
public float getBrightness(int displayId) {
try {
return mDm.getBrightness(displayId);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* Temporarily sets the auto brightness adjustment factor.
* <p>
* Requires the {@link android.Manifest.permission#CONTROL_DISPLAY_BRIGHTNESS} permission.
* </p>
*
* @param adjustment The adjustment factor from -1.0 to 1.0.
*
* @hide Requires signature permission.
*/
public void setTemporaryAutoBrightnessAdjustment(float adjustment) {
try {
mDm.setTemporaryAutoBrightnessAdjustment(adjustment);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* Returns the minimum brightness curve, which guarantess that any brightness curve that dips
* below it is rejected by the system.
* This prevent auto-brightness from setting the screen so dark as to prevent the user from
* resetting or disabling it, and maps lux to the absolute minimum nits that are still readable
* in that ambient brightness.
*
* @return The minimum brightness curve (as lux values and their corresponding nits values).
*/
public Pair<float[], float[]> getMinimumBrightnessCurve() {
try {
Curve curve = mDm.getMinimumBrightnessCurve();
return Pair.create(curve.getX(), curve.getY());
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* Retrieves ambient brightness stats.
*/
public List<AmbientBrightnessDayStats> getAmbientBrightnessStats() {
try {
ParceledListSlice<AmbientBrightnessDayStats> stats = mDm.getAmbientBrightnessStats();
if (stats == null) {
return Collections.emptyList();
}
return stats.getList();
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* Sets the default display mode, according to the refresh rate and the resolution chosen by the
* user.
*/
public void setUserPreferredDisplayMode(int displayId, Display.Mode mode) {
try {
mDm.setUserPreferredDisplayMode(displayId, mode);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* Returns the user preferred display mode.
*/
public Display.Mode getUserPreferredDisplayMode(int displayId) {
try {
return mDm.getUserPreferredDisplayMode(displayId);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* Returns the system preferred display mode.
*/
public Display.Mode getSystemPreferredDisplayMode(int displayId) {
try {
return mDm.getSystemPreferredDisplayMode(displayId);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* When enabled the app requested display resolution and refresh rate is always selected
* in DisplayModeDirector regardless of user settings and policies for low brightness, low
* battery etc.
*/
public void setShouldAlwaysRespectAppRequestedMode(boolean enabled) {
try {
mDm.setShouldAlwaysRespectAppRequestedMode(enabled);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* Returns whether DisplayModeDirector is running in a mode which always selects the app
* requested display mode and ignores user settings and policies for low brightness, low
* battery etc.
*/
public boolean shouldAlwaysRespectAppRequestedMode() {
try {
return mDm.shouldAlwaysRespectAppRequestedMode();
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* Sets the refresh rate switching type.
*
* @hide
*/
public void setRefreshRateSwitchingType(@DisplayManager.SwitchingType int newValue) {
try {
mDm.setRefreshRateSwitchingType(newValue);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
/**
* Returns the refresh rate switching type.
*
* @hide
*/
@DisplayManager.SwitchingType
public int getRefreshRateSwitchingType() {
try {
return mDm.getRefreshRateSwitchingType();
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
private final class DisplayManagerCallback extends IDisplayManagerCallback.Stub {
@Override
public void onDisplayEvent(int displayId, @DisplayEvent int event) {
if (DEBUG) {
Log.d(TAG, "onDisplayEvent: displayId=" + displayId + ", event=" + event);
}
handleDisplayEvent(displayId, event);
}
}
private static final class DisplayListenerDelegate extends Handler {
public final DisplayListener mListener;
public volatile long mEventsMask;
private final DisplayInfo mDisplayInfo = new DisplayInfo();
DisplayListenerDelegate(DisplayListener listener, @NonNull Looper looper,
@EventsMask long eventsMask) {
super(looper, null, true /*async*/);
mListener = listener;
mEventsMask = eventsMask;
}
public void sendDisplayEvent(int displayId, @DisplayEvent int event, DisplayInfo info) {
Message msg = obtainMessage(event, displayId, 0, info);
sendMessage(msg);
}
public void clearEvents() {
removeCallbacksAndMessages(null);
}
public void setEventsMask(@EventsMask long newEventsMask) {
mEventsMask = newEventsMask;
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case EVENT_DISPLAY_ADDED:
if ((mEventsMask & DisplayManager.EVENT_FLAG_DISPLAY_ADDED) != 0) {
mListener.onDisplayAdded(msg.arg1);
}
break;
case EVENT_DISPLAY_CHANGED:
if ((mEventsMask & DisplayManager.EVENT_FLAG_DISPLAY_CHANGED) != 0) {
DisplayInfo newInfo = (DisplayInfo) msg.obj;
if (newInfo != null && !newInfo.equals(mDisplayInfo)) {
mDisplayInfo.copyFrom(newInfo);
mListener.onDisplayChanged(msg.arg1);
}
}
break;
case EVENT_DISPLAY_BRIGHTNESS_CHANGED:
if ((mEventsMask & DisplayManager.EVENT_FLAG_DISPLAY_BRIGHTNESS) != 0) {
mListener.onDisplayChanged(msg.arg1);
}
break;
case EVENT_DISPLAY_REMOVED:
if ((mEventsMask & DisplayManager.EVENT_FLAG_DISPLAY_REMOVED) != 0) {
mListener.onDisplayRemoved(msg.arg1);
}
break;
}
}
}
/**
* Assists in dispatching VirtualDisplay lifecycle event callbacks on a given Executor.
*/
public static final class VirtualDisplayCallback extends IVirtualDisplayCallback.Stub {
@Nullable private final VirtualDisplay.Callback mCallback;
@Nullable private final Executor mExecutor;
/**
* Creates a virtual display callback.
*
* @param callback The callback to call for virtual display events, or {@code null} if the
* caller does not wish to receive callback events.
* @param executor The executor to call the {@code callback} on. Must not be {@code null} if
* the callback is not {@code null}.
*/
public VirtualDisplayCallback(VirtualDisplay.Callback callback, Executor executor) {
mCallback = callback;
mExecutor = mCallback != null ? Objects.requireNonNull(executor) : null;
}
// These methods are called from the binder thread, but the AIDL is oneway, so it should be
// safe to call the callback on arbitrary executors directly without risking blocking
// the system.
@Override // Binder call
public void onPaused() {
if (mCallback != null) {
mExecutor.execute(mCallback::onPaused);
}
}
@Override // Binder call
public void onResumed() {
if (mCallback != null) {
mExecutor.execute(mCallback::onResumed);
}
}
@Override // Binder call
public void onStopped() {
if (mCallback != null) {
mExecutor.execute(mCallback::onStopped);
}
}
}
/**
* Name of the property containing a unique token which changes every time we update the
* system's display configuration.
*/
public static final String CACHE_KEY_DISPLAY_INFO_PROPERTY =
"cache_key.display_info";
/**
* Invalidates the contents of the display info cache for all applications. Can only
* be called by system_server.
*/
public static void invalidateLocalDisplayInfoCaches() {
PropertyInvalidatedCache.invalidateCache(CACHE_KEY_DISPLAY_INFO_PROPERTY);
}
/**
* Disables the binder call cache.
*/
public void disableLocalDisplayInfoCaches() {
mDisplayCache = null;
}
private static native void nSignalNativeCallbacks(float refreshRate);
/**
* Called from AChoreographer via JNI.
* Registers AChoreographer so that refresh rate callbacks can be dispatched from DMS.
* Public for unit testing to be able to call this method.
*/
@VisibleForTesting
public void registerNativeChoreographerForRefreshRateCallbacks() {
synchronized (mLock) {
mDispatchNativeCallbacks = true;
registerCallbackIfNeededLocked();
updateCallbackIfNeededLocked();
DisplayInfo display = getDisplayInfoLocked(Display.DEFAULT_DISPLAY);
if (display != null) {
// We need to tell AChoreographer instances the current refresh rate so that apps
// can get it for free once a callback first registers.
mNativeCallbackReportedRefreshRate = display.getRefreshRate();
nSignalNativeCallbacks(mNativeCallbackReportedRefreshRate);
}
}
}
/**
* Called from AChoreographer via JNI.
* Unregisters AChoreographer from receiving refresh rate callbacks.
* Public for unit testing to be able to call this method.
*/
@VisibleForTesting
public void unregisterNativeChoreographerForRefreshRateCallbacks() {
synchronized (mLock) {
mDispatchNativeCallbacks = false;
updateCallbackIfNeededLocked();
}
}
}
|