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
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/display/window_tree_host_manager.h"
#include <cmath>
#include <map>
#include <memory>
#include <utility>
#include "ash/accessibility/magnifier/fullscreen_magnifier_controller.h"
#include "ash/accessibility/magnifier/partial_magnifier_controller.h"
#include "ash/display/cursor_window_controller.h"
#include "ash/display/mirror_window_controller.h"
#include "ash/display/refresh_rate_controller.h"
#include "ash/display/root_window_transformers.h"
#include "ash/frame_throttler/frame_throttling_controller.h"
#include "ash/host/ash_window_tree_host.h"
#include "ash/host/ash_window_tree_host_init_params.h"
#include "ash/host/root_window_transformer.h"
#include "ash/root_window_controller.h"
#include "ash/root_window_settings.h"
#include "ash/rounded_display/rounded_display_provider.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/system/status_area_widget.h"
#include "ash/system/unified/unified_system_tray.h"
#include "ash/wm/window_util.h"
#include "base/check.h"
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/histogram.h"
#include "base/metrics/histogram_functions.h"
#include "base/notimplemented.h"
#include "base/notreached.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/system/sys_info.h"
#include "base/task/single_thread_task_runner.h"
#include "ui/aura/client/capture_client.h"
#include "ui/aura/client/focus_client.h"
#include "ui/aura/client/screen_position_client.h"
#include "ui/aura/window.h"
#include "ui/aura/window_event_dispatcher.h"
#include "ui/aura/window_tracker.h"
#include "ui/aura/window_tree_host.h"
#include "ui/base/class_property.h"
#include "ui/base/ime/init/input_method_factory.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/compositor/compositor.h"
#include "ui/compositor/layer.h"
#include "ui/display/display.h"
#include "ui/display/display_layout.h"
#include "ui/display/display_transform.h"
#include "ui/display/manager/display_configurator.h"
#include "ui/display/manager/display_layout_store.h"
#include "ui/display/manager/display_manager.h"
#include "ui/display/screen.h"
#include "ui/display/types/display_constants.h"
#include "ui/display/util/display_util.h"
#include "ui/gfx/geometry/rounded_corners_f.h"
#include "ui/wm/core/coordinate_conversion.h"
#include "ui/wm/public/activation_client.h"
namespace ash {
using DM = display::DisplayObserver::DisplayMetric;
namespace {
// Primary display stored in global object as it can be
// accessed after Shell is deleted. A separate display instance is created
// during the shutdown instead of always keeping two display instances
// (one here and another one in display_manager) in sync, which is error prone.
// This is initialized in the constructor, and then in CreatePrimaryHost().
int64_t primary_display_id = -1;
// The compositor memory limit when display size is larger than
// `kUICompositorMemoryLimitDisplaySizeThreshold`.
constexpr int kUICompositorLargeDisplayMemoryLimitMB = 1024;
// The compositor memory limit when the device memory is greater than
// `kUICompositorMemoryLimitRamCapacityThreshold`.
constexpr int kUICompositorLargeRamMemoryLimitMB = 2048;
// The display size threshold, above which the larger memory limits are used.
// Pixel size was chosen to trigger for 4K+ displays. See: crbug.com/1261776
constexpr int kUICompositorMemoryLimitDisplaySizeThreshold = 3500;
// The RAM capacity threshold in MB. When the device has 16GB+ of memory,
// configure the compositor to use a higher memory limit.
constexpr int kUICompositorMemoryLimitRamCapacityThreshold = 16 * 1024;
// An UMA signal for the current effective resolution/dpi is sent at this rate.
// This keeps track of the effective resolution/dpi most used on
// internal/external display by the user.
constexpr base::TimeDelta kEffectiveResolutionRepeatingDelay =
base::Minutes(30);
// The uma name for display effective dpi histogram. This histogram helps
// determine the default settings of display resolution and zoom factor.
constexpr char kInternalDisplayEffectiveDPIHistogram[] =
"Ash.Display.InternalDisplay.ActiveEffectiveDPI";
constexpr char kExternalDisplayEffectiveDPIHistogram[] =
"Ash.Display.ExternalDisplay.ActiveEffectiveDPI";
// Most commonly used Chromebook internal display dpi ranges from 100 to 150. A
// 15" 4K external display has a dpi close to 300. A 21" 8K external display's
// dpi is around 420. Considering the display zoom factor, setting a min dpi 50
// and max dpi 500 should cover most if not all cases.
constexpr int kEffectiveDPIMinVal = 50;
constexpr int kEffectiveDPIMaxVal = 500;
constexpr int kEffectiveDPIBucketCount = 90;
display::DisplayManager* GetDisplayManager() {
return Shell::Get()->display_manager();
}
void SetDisplayPropertiesOnHost(AshWindowTreeHost* ash_host,
const display::Display& display,
bool needs_redraw = true) {
const display::Display::Rotation effective_rotation =
display.panel_rotation();
aura::WindowTreeHost* host = ash_host->AsWindowTreeHost();
ash_host->UpdateCursorConfig();
ash_host->SetRootWindowTransformer(
CreateRootWindowTransformerForDisplay(display));
host->SetDisplayTransformHint(
display::DisplayRotationToOverlayTransform(effective_rotation));
const display::ManagedDisplayInfo& display_info =
GetDisplayManager()->GetDisplayInfo(display.id());
std::optional<base::TimeDelta> max_vsync_interval = std::nullopt;
if (display_info.variable_refresh_rate_state() !=
display::VariableRefreshRateState::kVrrNotCapable &&
display_info.vsync_rate_min().has_value() &&
display_info.vsync_rate_min() > 0) {
max_vsync_interval = base::Hertz(display_info.vsync_rate_min().value());
}
host->compositor()->SetMaxVSyncAndVrr(
max_vsync_interval, display_info.variable_refresh_rate_state());
// Just moving the display requires the full redraw.
// chrome-os-partner:33558.
if (needs_redraw) {
host->compositor()->ScheduleFullRedraw();
}
}
void ClearDisplayPropertiesOnHost(AshWindowTreeHost* ash_host) {
ash_host->ClearCursorConfig();
}
aura::Window* GetWindow(AshWindowTreeHost* ash_host) {
CHECK(ash_host->AsWindowTreeHost());
return ash_host->AsWindowTreeHost()->window();
}
// Returns the index to the enum - |EffectiveResolution|. The enum value
// represents the resolution that exactly matches the primary display's
// effective resolution.
int GetEffectiveResolutionUMAIndex(const display::Display& display) {
const gfx::Size effective_size = display.size();
// The UMA enum index for portrait mode has 1 subtracted from itself. This
// differentiates it from the landscape mode.
return effective_size.width() > effective_size.height()
? effective_size.width() * effective_size.height()
: effective_size.width() * effective_size.height() - 1;
}
// Returns active effective dpi for a given active display. Returns 0 if the
// dpi is not available.
std::optional<float> GetEffectiveDPI(const display::Display& display) {
const display::ManagedDisplayInfo& display_info =
GetDisplayManager()->GetDisplayInfo(display.id());
float dpi = display_info.device_dpi();
if (!dpi) {
return std::nullopt;
}
// Apply device effective scale factor.
return dpi / display_info.GetEffectiveDeviceScaleFactor();
}
void RepeatingEffectiveResolutionUMA(base::RepeatingTimer* timer,
bool is_first_run) {
display::Display internal_display;
const auto* session_controller = Shell::Get()->session_controller();
// Record the UMA only when this is an active user session and the
// internal display is present.
if (display::HasInternalDisplay() &&
display::Screen::GetScreen()->GetDisplayWithDisplayId(
display::Display::InternalDisplayId(), &internal_display) &&
session_controller->IsActiveUserSessionStarted() &&
session_controller->GetSessionState() ==
session_manager::SessionState::ACTIVE) {
base::UmaHistogramSparse(
"Ash.Display.InternalDisplay.ActiveEffectiveResolution",
GetEffectiveResolutionUMAIndex(internal_display));
}
if (session_controller->IsActiveUserSessionStarted() &&
session_controller->GetSessionState() ==
session_manager::SessionState::ACTIVE) {
for (const auto& display : GetDisplayManager()->active_display_list()) {
std::optional<float> effective_dpi = GetEffectiveDPI(display);
// Only emit event when the dpi is valid.
if (effective_dpi.has_value()) {
base::UmaHistogramCustomCounts(
(display::IsInternalDisplayId(display.id())
? kInternalDisplayEffectiveDPIHistogram
: kExternalDisplayEffectiveDPIHistogram),
effective_dpi.value(), kEffectiveDPIMinVal, kEffectiveDPIMaxVal,
kEffectiveDPIBucketCount);
}
}
}
// The first run of the repeating timer is half the actual delay. Reset the
// timer after the first run with the correct delay.
if (is_first_run && timer) {
timer->Start(
FROM_HERE, kEffectiveResolutionRepeatingDelay,
base::BindRepeating(&RepeatingEffectiveResolutionUMA,
nullptr /*timer=*/, false /*is_first_run=*/));
}
}
} // namespace
// A utility class to store/restore focused/active window
// when the display configuration has changed.
class FocusActivationStore {
public:
FocusActivationStore()
: activation_client_(nullptr),
capture_client_(nullptr),
focus_client_(nullptr),
focused_(nullptr),
active_(nullptr) {}
FocusActivationStore(const FocusActivationStore&) = delete;
FocusActivationStore& operator=(const FocusActivationStore&) = delete;
void Store(bool clear_focus) {
if (!activation_client_) {
aura::Window* root = Shell::GetPrimaryRootWindow();
activation_client_ = ::wm::GetActivationClient(root);
capture_client_ = aura::client::GetCaptureClient(root);
focus_client_ = aura::client::GetFocusClient(root);
}
focused_ = focus_client_->GetFocusedWindow();
if (focused_)
tracker_.Add(focused_);
active_ = activation_client_->GetActiveWindow();
if (active_ && focused_ != active_)
tracker_.Add(active_);
// Deactivate the window to close menu / bubble windows. Deactivating by
// setting active window to nullptr to avoid side effects of activating an
// arbitrary window, such as covering |active_| before Restore().
if (clear_focus && active_)
activation_client_->ActivateWindow(nullptr);
// Release capture if any.
capture_client_->SetCapture(nullptr);
// Clear the focused window if any. This is necessary because a
// window may be deleted when losing focus (fullscreen flash for
// example). If the focused window is still alive after move, it'll
// be re-focused below.
if (clear_focus)
focus_client_->FocusWindow(nullptr);
}
void Restore() {
// Restore focused or active window if it's still alive.
if (focused_ && tracker_.Contains(focused_)) {
focus_client_->FocusWindow(focused_);
} else if (active_ && tracker_.Contains(active_)) {
activation_client_->ActivateWindow(active_);
}
if (focused_)
tracker_.Remove(focused_);
if (active_)
tracker_.Remove(active_);
focused_ = nullptr;
active_ = nullptr;
}
private:
raw_ptr<::wm::ActivationClient> activation_client_;
raw_ptr<aura::client::CaptureClient> capture_client_;
raw_ptr<aura::client::FocusClient> focus_client_;
aura::WindowTracker tracker_;
raw_ptr<aura::Window, DanglingUntriaged> focused_;
raw_ptr<aura::Window, DanglingUntriaged> active_;
};
////////////////////////////////////////////////////////////////////////////////
// WindowTreeHostManager
WindowTreeHostManager::WindowTreeHostManager()
: primary_tree_host_for_replace_(nullptr),
focus_activation_store_(new FocusActivationStore()),
cursor_window_controller_(new CursorWindowController()),
mirror_window_controller_(new MirrorWindowController()),
cursor_display_id_for_restore_(display::kInvalidDisplayId) {
// Reset primary display to make sure that tests don't use
// stale display info from previous tests.
primary_display_id = display::kInvalidDisplayId;
}
WindowTreeHostManager::~WindowTreeHostManager() {
DCHECK(rounded_display_providers_map_.empty())
<< "ShutdownRoundedDisplays() must be called before this is destroyed";
}
void WindowTreeHostManager::Start() {
Shell::Get()
->display_configurator()
->content_protection_manager()
->AddObserver(this);
Shell::Get()->display_manager()->set_delegate(this);
// Start a repeating timer to send UMA at fixed intervals. The first run is at
// half the delay time.
effective_resolution_UMA_timer_ = std::make_unique<base::RepeatingTimer>();
effective_resolution_UMA_timer_->Start(
FROM_HERE, kEffectiveResolutionRepeatingDelay / 2,
base::BindRepeating(&RepeatingEffectiveResolutionUMA,
effective_resolution_UMA_timer_.get(),
true /*is_first_run=*/));
}
void WindowTreeHostManager::ShutdownRoundedDisplays() {
rounded_display_providers_map_.clear();
}
void WindowTreeHostManager::Shutdown() {
effective_resolution_UMA_timer_->Reset();
cursor_window_controller_.reset();
mirror_window_controller_.reset();
Shell::Get()
->display_configurator()
->content_protection_manager()
->RemoveObserver(this);
// Unset the display manager's delegate here because
// DisplayManager outlives WindowTreeHostManager.
Shell::Get()->display_manager()->set_delegate(nullptr);
int64_t primary_id = display::Screen::GetScreen()->GetPrimaryDisplay().id();
// Delete non primary root window controllers first, then
// delete the primary root window controller.
aura::Window::Windows root_windows =
WindowTreeHostManager::GetAllRootWindows();
std::vector<RootWindowController*> to_delete;
RootWindowController* primary_rwc = nullptr;
for (aura::Window::Windows::iterator iter = root_windows.begin();
iter != root_windows.end(); ++iter) {
RootWindowController* rwc = RootWindowController::ForWindow(*iter);
if (GetRootWindowSettings(*iter)->display_id == primary_id)
primary_rwc = rwc;
else
to_delete.push_back(rwc);
}
CHECK(primary_rwc);
Shell::SetRootWindowForNewWindows(nullptr);
for (auto* rwc : to_delete)
delete rwc;
delete primary_rwc;
}
void WindowTreeHostManager::CreatePrimaryHost(
const AshWindowTreeHostInitParams& init_params) {
const display::Display& primary_candidate =
GetDisplayManager()->GetPrimaryDisplayCandidate();
primary_display_id = primary_candidate.id();
CHECK_NE(display::kInvalidDisplayId, primary_display_id);
AddWindowTreeHostForDisplay(primary_candidate, init_params);
}
void WindowTreeHostManager::InitHosts() {
RootWindowController::CreateForPrimaryDisplay(
window_tree_hosts_[primary_display_id]);
display::DisplayManager* display_manager = GetDisplayManager();
for (size_t i = 0; i < display_manager->GetNumDisplays(); ++i) {
const display::Display& display = display_manager->GetDisplayAt(i);
if (primary_display_id != display.id()) {
AshWindowTreeHost* ash_host =
AddWindowTreeHostForDisplay(display, AshWindowTreeHostInitParams());
RootWindowController::CreateForSecondaryDisplay(ash_host);
}
}
// We need to initialize rounded display providers after we have initialized
// the root controllers for each display.
for (size_t i = 0; i < display_manager->GetNumDisplays(); ++i) {
const display::Display& display = display_manager->GetDisplayAt(i);
EnableRoundedCorners(display);
}
}
// static
int64_t WindowTreeHostManager::GetPrimaryDisplayId() {
CHECK_NE(display::kInvalidDisplayId, primary_display_id);
return primary_display_id;
}
// static
bool WindowTreeHostManager::HasValidPrimaryDisplayId() {
return primary_display_id != display::kInvalidDisplayId;
}
aura::Window* WindowTreeHostManager::GetPrimaryRootWindow() {
// If |primary_tree_host_for_replace_| is set, it means |primary_display_id|
// is kInvalidDisplayId.
if (primary_tree_host_for_replace_)
return GetWindow(primary_tree_host_for_replace_);
return GetRootWindowForDisplayId(primary_display_id);
}
aura::Window* WindowTreeHostManager::GetRootWindowForDisplayId(int64_t id) {
AshWindowTreeHost* host = GetAshWindowTreeHostForDisplayId(id);
return host ? GetWindow(host) : nullptr;
}
AshWindowTreeHost* WindowTreeHostManager::GetAshWindowTreeHostForDisplayId(
int64_t display_id) {
const auto host = window_tree_hosts_.find(display_id);
if (host != window_tree_hosts_.end())
return host->second;
return mirror_window_controller_->GetAshWindowTreeHostForDisplayId(
display_id);
}
aura::Window::Windows WindowTreeHostManager::GetAllRootWindows() {
aura::Window::Windows windows;
for (WindowTreeHostMap::const_iterator it = window_tree_hosts_.begin();
it != window_tree_hosts_.end(); ++it) {
DCHECK(it->second);
if (RootWindowController::ForWindow(GetWindow(it->second)))
windows.push_back(GetWindow(it->second));
}
return windows;
}
std::vector<RootWindowController*>
WindowTreeHostManager::GetAllRootWindowControllers() {
std::vector<RootWindowController*> controllers;
for (WindowTreeHostMap::const_iterator it = window_tree_hosts_.begin();
it != window_tree_hosts_.end(); ++it) {
RootWindowController* controller =
RootWindowController::ForWindow(GetWindow(it->second));
if (controller)
controllers.push_back(controller);
}
return controllers;
}
void WindowTreeHostManager::UpdateMouseLocationAfterDisplayChange() {
// If the mouse is currently on a display in native location,
// use the same native location. Otherwise find the display closest
// to the current cursor location in screen coordinates.
gfx::Point point_in_screen =
display::Screen::GetScreen()->GetCursorScreenPoint();
gfx::Point target_location_in_native;
int64_t closest_distance_squared = -1;
display::DisplayManager* display_manager = GetDisplayManager();
aura::Window* dst_root_window = nullptr;
for (size_t i = 0; i < display_manager->GetNumDisplays(); ++i) {
const display::Display& display = display_manager->GetDisplayAt(i);
const display::ManagedDisplayInfo display_info =
display_manager->GetDisplayInfo(display.id());
aura::Window* root_window = GetRootWindowForDisplayId(display.id());
if (display_info.bounds_in_native().Contains(
cursor_location_in_native_coords_for_restore_)) {
dst_root_window = root_window;
target_location_in_native = cursor_location_in_native_coords_for_restore_;
break;
}
gfx::Point center = display.bounds().CenterPoint();
// Use the distance squared from the center of the display. This is not
// exactly "closest" display, but good enough to pick one
// appropriate (and there are at most two displays).
// We don't care about actual distance, only relative to other displays, so
// using the LengthSquared() is cheaper than Length().
int64_t distance_squared = (center - point_in_screen).LengthSquared();
if (closest_distance_squared < 0 ||
closest_distance_squared > distance_squared) {
::wm::ConvertPointFromScreen(root_window, ¢er);
root_window->GetHost()->ConvertDIPToScreenInPixels(¢er);
dst_root_window = root_window;
target_location_in_native = center;
closest_distance_squared = distance_squared;
}
}
gfx::Point target_location_in_root = target_location_in_native;
dst_root_window->GetHost()->ConvertScreenInPixelsToDIP(
&target_location_in_root);
gfx::Point target_location_in_screen = target_location_in_root;
::wm::ConvertPointToScreen(dst_root_window, &target_location_in_screen);
const display::Display& target_display =
display_manager->FindDisplayContainingPoint(target_location_in_screen);
// If the original location isn't on any of new display, let ozone move
// the cursor.
if (!target_display.is_valid())
return;
int64_t target_display_id = target_display.id();
// Do not move the cursor if the cursor's location did not change. This avoids
// moving (and showing) the cursor:
// - At startup.
// - When the device is rotated in tablet mode.
// |cursor_display_id_for_restore_| is checked to ensure that the cursor is
// moved when the cursor's native position does not change but the display
// that it is on has changed. This occurs when swapping the primary display.
if (target_location_in_native !=
cursor_location_in_native_coords_for_restore_ ||
target_display_id != cursor_display_id_for_restore_) {
if (Shell::Get()->cursor_manager()) {
if (Shell::Get()->cursor_manager()->IsCursorVisible()) {
dst_root_window->MoveCursorTo(target_location_in_root);
} else if (target_display_id != cursor_display_id_for_restore_) {
Shell::Get()->cursor_manager()->SetDisplay(target_display);
}
}
return;
}
// Convert the screen coords restore location to native, rather than comparing
// screen locations directly. Converting back and forth causes floating point
// values to be floored at each step, so the conversions must be performed
// equally.
gfx::Point restore_location_in_native =
cursor_location_in_screen_coords_for_restore_;
::wm::ConvertPointFromScreen(dst_root_window, &restore_location_in_native);
dst_root_window->GetHost()->ConvertDIPToScreenInPixels(
&restore_location_in_native);
if (target_location_in_native != restore_location_in_native) {
// The cursor's native position did not change but its screen position did
// change. This occurs when the scale factor or the rotation of the display
// that the cursor is on changes.
// TODO: conditional should not be necessary. http://crbug.com/631103.
if (Shell::Get()->cursor_manager())
Shell::Get()->cursor_manager()->SetDisplay(target_display);
// Update the cursor's root location. This ends up dispatching a synthetic
// mouse move. The synthetic mouse move updates the composited cursor's
// location and hover effects. Synthetic mouse moves do not affect the
// cursor's visibility.
dst_root_window->GetHost()->dispatcher()->OnCursorMovedToRootLocation(
target_location_in_root);
}
}
bool WindowTreeHostManager::UpdateWorkAreaOfDisplayNearestWindow(
const aura::Window* window,
const gfx::Insets& insets) {
const aura::Window* root_window = window->GetRootWindow();
int64_t id = GetRootWindowSettings(root_window)->display_id;
// if id is |kInvalidDisplayID|, it's being deleted.
DCHECK(id != display::kInvalidDisplayId);
return GetDisplayManager()->UpdateWorkAreaOfDisplay(id, insets);
}
void WindowTreeHostManager::CreateDisplay(const display::Display& display) {
// If we're switching from/to offscreen WTH, we need to
// create new WTH for primary display instead of reusing.
if (primary_tree_host_for_replace_ &&
(GetRootWindowSettings(GetWindow(primary_tree_host_for_replace_))
->display_id == display::kUnifiedDisplayId ||
display.id() == display::kUnifiedDisplayId)) {
DCHECK_EQ(display::kInvalidDisplayId, primary_display_id);
primary_display_id = display.id();
AshWindowTreeHost* ash_host =
AddWindowTreeHostForDisplay(display, AshWindowTreeHostInitParams());
RootWindowController* new_root_window_controller =
RootWindowController::CreateForSecondaryDisplay(ash_host);
// Magnifier controllers keep pointers to the current root window.
// Update them here to avoid accessing them later.
Shell::Get()->fullscreen_magnifier_controller()->SwitchTargetRootWindow(
ash_host->AsWindowTreeHost()->window(), false);
Shell::Get()
->partial_magnifier_controller()
->SwitchTargetRootWindowIfNeeded(
ash_host->AsWindowTreeHost()->window());
AshWindowTreeHost* to_delete = primary_tree_host_for_replace_;
// Show the shelf if the original WTH had a visible system
// tray. It may or may not be visible depending on OOBE state.
RootWindowController* old_root_window_controller =
RootWindowController::ForWindow(
to_delete->AsWindowTreeHost()->window());
TrayBackgroundView* old_tray =
old_root_window_controller->GetStatusAreaWidget()
->unified_system_tray();
TrayBackgroundView* new_tray =
new_root_window_controller->GetStatusAreaWidget()
->unified_system_tray();
if (old_tray->GetWidget()->IsVisible()) {
new_tray->SetVisiblePreferred(true);
new_tray->GetWidget()->Show();
}
// |to_delete| has already been removed from |window_tree_hosts_|.
DCHECK(!base::Contains(window_tree_hosts_, to_delete,
&WindowTreeHostMap::value_type::second));
DeleteHost(to_delete);
DCHECK(!primary_tree_host_for_replace_);
} else if (primary_tree_host_for_replace_) {
// TODO(oshima): It should be possible to consolidate logic for
// unified and non unified, but I'm keeping them separated to minimize
// the risk in M44. I'll consolidate this in M45.
DCHECK(window_tree_hosts_.empty());
AshWindowTreeHost* ash_host = primary_tree_host_for_replace_;
primary_tree_host_for_replace_ = nullptr;
primary_display_id = display.id();
window_tree_hosts_[display.id()] = ash_host;
GetRootWindowSettings(GetWindow(ash_host))->display_id = display.id();
const display::ManagedDisplayInfo& display_info =
GetDisplayManager()->GetDisplayInfo(display.id());
ash_host->AsWindowTreeHost()->SetBoundsInPixels(
display_info.bounds_in_native());
SetDisplayPropertiesOnHost(ash_host, display);
} else {
if (primary_display_id == display::kInvalidDisplayId)
primary_display_id = display.id();
DCHECK(!window_tree_hosts_.empty());
AshWindowTreeHost* ash_host =
AddWindowTreeHostForDisplay(display, AshWindowTreeHostInitParams());
RootWindowController::CreateForSecondaryDisplay(ash_host);
}
EnableRoundedCorners(display);
}
void WindowTreeHostManager::DeleteHost(AshWindowTreeHost* host_to_delete) {
ClearDisplayPropertiesOnHost(host_to_delete);
aura::Window* root_being_deleted = GetWindow(host_to_delete);
RootWindowController* controller =
RootWindowController::ForWindow(root_being_deleted);
DCHECK(controller);
// Some code relies on this being called before MoveWindowsTo().
Shell::Get()->OnRootWindowWillShutdown(root_being_deleted);
aura::Window* primary_root_after_host_deletion =
GetRootWindowForDisplayId(GetPrimaryDisplayId());
// Delete most of root window related objects, but don't delete
// root window itself yet because the stack may be using it.
controller->Shutdown(primary_root_after_host_deletion);
if (primary_tree_host_for_replace_ == host_to_delete)
primary_tree_host_for_replace_ = nullptr;
DCHECK_EQ(primary_root_after_host_deletion, Shell::GetPrimaryRootWindow());
if (Shell::GetRootWindowForNewWindows() == root_being_deleted) {
Shell::SetRootWindowForNewWindows(primary_root_after_host_deletion);
}
// NOTE: ShelfWidget is gone, but Shelf still exists until this task runs.
base::SingleThreadTaskRunner::GetCurrentDefault()->DeleteSoon(FROM_HERE,
controller);
}
void WindowTreeHostManager::RemoveDisplay(const display::Display& display) {
AshWindowTreeHost* host_to_delete = window_tree_hosts_[display.id()];
CHECK(host_to_delete) << display.ToString();
RemoveRoundedDisplayProvider(display);
// When the primary root window's display is removed, move the primary
// root to the other display.
if (primary_display_id == display.id()) {
// Temporarily store the primary root window in
// |primary_root_window_for_replace_| when replacing the display.
if (window_tree_hosts_.size() == 1) {
primary_display_id = display::kInvalidDisplayId;
primary_tree_host_for_replace_ = host_to_delete;
// Display for root window will be deleted when the Primary RootWindow
// is deleted by the Shell.
window_tree_hosts_.erase(display.id());
return;
}
for (const auto& pair : window_tree_hosts_) {
if (pair.first != display.id()) {
primary_display_id = pair.first;
break;
}
}
CHECK_NE(display::kInvalidDisplayId, primary_display_id);
AshWindowTreeHost* primary_host = host_to_delete;
// Delete the other host instead.
host_to_delete = window_tree_hosts_[primary_display_id];
GetRootWindowSettings(GetWindow(host_to_delete))->display_id = display.id();
// Setup primary root.
window_tree_hosts_[primary_display_id] = primary_host;
GetRootWindowSettings(GetWindow(primary_host))->display_id =
primary_display_id;
// Ensure that color spaces for the root windows reflect those of their new
// displays. If these go out of sync, we can lose the ability to composite
// HDR content.
const display::Display& new_primary_display =
GetDisplayManager()->GetDisplayForId(primary_display_id);
primary_host->AsWindowTreeHost()->compositor()->SetDisplayColorSpaces(
new_primary_display.GetColorSpaces());
// Since window tree hosts have been swapped between displays, we need to
// update the WTH the RoundedDisplayProviders are attached to.
UpdateHostOfDisplayProviders();
UpdateDisplayMetrics(new_primary_display, DM::DISPLAY_METRIC_BOUNDS);
}
DeleteHost(host_to_delete);
// The window tree host should be erased at last because some handlers can
// access to the host through GetRootWindowForDisplayId() during
// MoveWindowsTo(). See http://crbug.com/415222
window_tree_hosts_.erase(display.id());
}
void WindowTreeHostManager::UpdateDisplayMetrics(
const display::Display& display,
uint32_t metrics) {
if (!(metrics &
(DM::DISPLAY_METRIC_BOUNDS | DM::DISPLAY_METRIC_ROTATION |
DM::DISPLAY_METRIC_DEVICE_SCALE_FACTOR | DM::DISPLAY_METRIC_VRR))) {
return;
}
const display::ManagedDisplayInfo& display_info =
GetDisplayManager()->GetDisplayInfo(display.id());
DCHECK(!display_info.bounds_in_native().IsEmpty());
AshWindowTreeHost* ash_host = window_tree_hosts_[display.id()];
ash_host->AsWindowTreeHost()->SetBoundsInPixels(
display_info.bounds_in_native());
// Redraw should trigger on bounds/resolution changes. VRR-only changes should
// not trigger redraws.
bool needs_redraw =
metrics & (DM::DISPLAY_METRIC_BOUNDS | DM::DISPLAY_METRIC_ROTATION |
DM::DISPLAY_METRIC_DEVICE_SCALE_FACTOR);
SetDisplayPropertiesOnHost(ash_host, display, needs_redraw);
// We need to update the surface on which rounded display mask textures are
// rendered when ever the display device scale factor or display rotation
// changes.
MaybeUpdateRoundedDisplaySurface(display);
}
void WindowTreeHostManager::EnableRoundedCorners(
const display::Display& display) {
// This method will create a provider for the display if one already does not
// exists.
AddRoundedDisplayProviderIfNeeded(display);
MaybeUpdateRoundedDisplaySurface(display);
}
void WindowTreeHostManager::MaybeUpdateRoundedDisplaySurface(
const display::Display& display) {
RoundedDisplayProvider* rounded_display_provider =
GetRoundedDisplayProvider(display.id());
if (rounded_display_provider) {
rounded_display_provider->UpdateRoundedDisplaySurface();
}
}
RoundedDisplayProvider* WindowTreeHostManager::GetRoundedDisplayProvider(
int64_t display_id) {
auto iter = rounded_display_providers_map_.find(display_id);
return (iter != rounded_display_providers_map_.end()) ? iter->second.get()
: nullptr;
}
void WindowTreeHostManager::AddRoundedDisplayProviderIfNeeded(
const display::Display& display) {
const display::ManagedDisplayInfo& display_info =
GetDisplayManager()->GetDisplayInfo(display.id());
const gfx::RoundedCornersF panel_radii = display_info.panel_corners_radii();
if (panel_radii.IsEmpty() || GetRoundedDisplayProvider(display.id())) {
return;
}
auto rounded_display_provider = RoundedDisplayProvider::Create(display.id());
rounded_display_provider->Init(panel_radii,
RoundedDisplayProvider::Strategy::kScanout);
rounded_display_providers_map_[display.id()] =
std::move(rounded_display_provider);
}
void WindowTreeHostManager::RemoveRoundedDisplayProvider(
const display::Display& display) {
rounded_display_providers_map_.erase(display.id());
}
void WindowTreeHostManager::UpdateHostOfDisplayProviders() {
for (auto& pair : window_tree_hosts_) {
RoundedDisplayProvider* rounded_display_provider =
GetRoundedDisplayProvider(pair.first);
if (rounded_display_provider) {
rounded_display_provider->UpdateHostParent();
}
}
}
void WindowTreeHostManager::OnHostResized(aura::WindowTreeHost* host) {
display::Display display =
display::Screen::GetScreen()->GetDisplayNearestWindow(host->window());
display::DisplayManager* display_manager = GetDisplayManager();
if (display_manager->UpdateDisplayBounds(display.id(),
host->GetBoundsInPixels())) {
mirror_window_controller_->UpdateWindow();
cursor_window_controller_->UpdateContainer();
}
}
void WindowTreeHostManager::OnLocalSurfaceIdChanged(
aura::WindowTreeHost* host,
const viz::LocalSurfaceId& id) {
mirror_window_controller_->UpdateWindow();
}
void WindowTreeHostManager::OnDisplaySecurityMaybeChanged(int64_t display_id,
bool secure) {
AshWindowTreeHost* host = GetAshWindowTreeHostForDisplayId(display_id);
// No host for internal display in docked mode.
if (!host)
return;
ui::Compositor* compositor = host->AsWindowTreeHost()->compositor();
if (compositor->output_is_secure() == secure) {
return;
}
compositor->SetOutputIsSecure(secure);
compositor->ScheduleFullRedraw();
}
void WindowTreeHostManager::CreateOrUpdateMirroringDisplay(
const display::DisplayInfoList& info_list) {
if (GetDisplayManager()->IsInMirrorMode() ||
GetDisplayManager()->IsInUnifiedMode()) {
mirror_window_controller_->UpdateWindow(info_list);
cursor_window_controller_->UpdateContainer();
} else {
DUMP_WILL_BE_NOTREACHED();
}
}
void WindowTreeHostManager::CloseMirroringDisplayIfNotNecessary() {
mirror_window_controller_->CloseIfNotNecessary();
// If cursor_compositing is enabled for large cursor, the cursor window is
// always on the desktop display (the visible cursor on the non-desktop
// display is drawn through compositor mirroring). Therefore, it's
// unnecessary to handle the cursor_window at all. See:
// http://crbug.com/412910
if (!cursor_window_controller_->is_cursor_compositing_enabled())
cursor_window_controller_->UpdateContainer();
}
void WindowTreeHostManager::PreDisplayConfigurationChange(bool clear_focus) {
// Pause occlusion tracking during display configuration updates.
scoped_pause_ = std::make_unique<aura::WindowOcclusionTracker::ScopedPause>();
focus_activation_store_->Store(clear_focus);
display::Screen* screen = display::Screen::GetScreen();
gfx::Point point_in_screen = screen->GetCursorScreenPoint();
cursor_location_in_screen_coords_for_restore_ = point_in_screen;
display::Display display = screen->GetDisplayNearestPoint(point_in_screen);
cursor_display_id_for_restore_ = display.id();
gfx::Point point_in_native = point_in_screen;
aura::Window* root_window = GetRootWindowForDisplayId(display.id());
::wm::ConvertPointFromScreen(root_window, &point_in_native);
root_window->GetHost()->ConvertDIPToScreenInPixels(&point_in_native);
cursor_location_in_native_coords_for_restore_ = point_in_native;
}
void WindowTreeHostManager::SetPrimaryDisplayId(int64_t id) {
// TODO(oshima): Move primary display management to DisplayManager.
DCHECK_NE(display::kInvalidDisplayId, id);
if (id == display::kInvalidDisplayId || primary_display_id == id ||
window_tree_hosts_.size() < 2) {
return;
}
const display::Display& new_primary_display =
GetDisplayManager()->GetDisplayForId(id);
const int64_t new_primary_id = new_primary_display.id();
if (!new_primary_display.is_valid()) {
LOG(ERROR) << "Invalid or non-existent display is requested:"
<< new_primary_display.ToString();
return;
}
display::DisplayManager* display_manager = GetDisplayManager();
DCHECK(new_primary_display.is_valid());
DCHECK(display_manager->GetDisplayForId(new_primary_id).is_valid());
AshWindowTreeHost* non_primary_host = window_tree_hosts_[new_primary_id];
LOG_IF(ERROR, !non_primary_host)
<< "Unknown display is requested in SetPrimaryDisplay: id="
<< new_primary_id;
if (!non_primary_host)
return;
display::Display old_primary_display =
display::Screen::GetScreen()->GetPrimaryDisplay();
const int64_t old_primary_id = old_primary_display.id();
DCHECK_EQ(old_primary_id, primary_display_id);
// Swap root windows between current and new primary display.
AshWindowTreeHost* primary_host = window_tree_hosts_[primary_display_id];
CHECK(primary_host);
CHECK_NE(primary_host, non_primary_host);
aura::Window* primary_window = GetWindow(primary_host);
aura::Window* non_primary_window = GetWindow(non_primary_host);
window_tree_hosts_[new_primary_id] = primary_host;
GetRootWindowSettings(primary_window)->display_id = new_primary_id;
window_tree_hosts_[old_primary_id] = non_primary_host;
GetRootWindowSettings(non_primary_window)->display_id = old_primary_id;
// Ensure that color spaces for the root windows reflect those of their new
// displays. If these go out of sync, we can lose the ability to composite
// HDR content.
primary_host->AsWindowTreeHost()->compositor()->SetDisplayColorSpaces(
new_primary_display.GetColorSpaces());
non_primary_host->AsWindowTreeHost()->compositor()->SetDisplayColorSpaces(
old_primary_display.GetColorSpaces());
std::u16string old_primary_title = primary_window->GetTitle();
primary_window->SetTitle(non_primary_window->GetTitle());
non_primary_window->SetTitle(old_primary_title);
const display::DisplayLayout& layout =
GetDisplayManager()->GetCurrentDisplayLayout();
// The requested primary id can be same as one in the stored layout
// when the primary id is set after new displays are connected.
// Only update the layout if it is requested to swap primary display.
if (layout.primary_id != new_primary_id) {
std::unique_ptr<display::DisplayLayout> swapped_layout = layout.Copy();
swapped_layout->SwapPrimaryDisplay(new_primary_id);
display::DisplayIdList list = display_manager->GetConnectedDisplayIdList();
GetDisplayManager()->layout_store()->RegisterLayoutForDisplayIdList(
list, std::move(swapped_layout));
}
// Update the global primary_display_id.
primary_display_id = new_primary_id;
UpdateWorkAreaOfDisplayNearestWindow(GetWindow(primary_host),
old_primary_display.GetWorkAreaInsets());
UpdateWorkAreaOfDisplayNearestWindow(GetWindow(non_primary_host),
new_primary_display.GetWorkAreaInsets());
// Since window tree hosts have been swapped, we need to update the WTH
// that RoundedDisplayProviders are attached to.
UpdateHostOfDisplayProviders();
// Update the display manager with new display info.
GetDisplayManager()->set_force_bounds_changed(true);
GetDisplayManager()->UpdateDisplays();
GetDisplayManager()->set_force_bounds_changed(false);
}
void WindowTreeHostManager::PostDisplayConfigurationChange() {
focus_activation_store_->Restore();
UpdateMouseLocationAfterDisplayChange();
// Enable cursor compositing, so that cursor could be mirrored to
// destination displays along with other display content.
Shell::Get()->UpdateCursorCompositingEnabled();
// Unpause occlusion tracking.
scoped_pause_.reset();
}
ui::EventDispatchDetails WindowTreeHostManager::DispatchKeyEventPostIME(
ui::KeyEvent* event) {
aura::Window* root_window = nullptr;
if (event->target()) {
root_window = static_cast<aura::Window*>(event->target())->GetRootWindow();
DCHECK(root_window);
} else {
// Getting the active root window to dispatch the event. This isn't
// significant as the event will be sent to the window resolved by
// aura::client::FocusClient which is FocusController in ash.
aura::Window* active_window = window_util::GetActiveWindow();
root_window = active_window ? active_window->GetRootWindow()
: Shell::GetPrimaryRootWindow();
}
return root_window->GetHost()->DispatchKeyEventPostIME(event);
}
const display::Display* WindowTreeHostManager::GetDisplayById(
int64_t display_id) const {
const display::Display& display =
GetDisplayManager()->GetDisplayForId(display_id);
return display.is_valid() ? &display : nullptr;
}
void WindowTreeHostManager::SetCurrentEventTargeterSourceHost(
aura::WindowTreeHost* targeter_src_host) {
NOTIMPLEMENTED();
}
AshWindowTreeHost* WindowTreeHostManager::AddWindowTreeHostForDisplay(
const display::Display& display,
const AshWindowTreeHostInitParams& init_params) {
static int host_count = 0;
const display::ManagedDisplayInfo& display_info =
GetDisplayManager()->GetDisplayInfo(display.id());
AshWindowTreeHostInitParams params_with_bounds(init_params);
params_with_bounds.initial_bounds = display_info.bounds_in_native();
if (display.id() == display::kUnifiedDisplayId) {
params_with_bounds.offscreen = true;
params_with_bounds.delegate = mirror_window_controller();
} else {
params_with_bounds.delegate = this;
}
params_with_bounds.display_id = display.id();
params_with_bounds.device_scale_factor = display.device_scale_factor();
// TODO(crbug.com/40799092): Temporarily increase compositor memory limit for
// 4K+ displays to avoid rendering corruption.
// Check both width and height in case of rotated display.
if (std::max(display.GetSizeInPixel().width(),
display.GetSizeInPixel().height()) >
kUICompositorMemoryLimitDisplaySizeThreshold) {
params_with_bounds.compositor_memory_limit_mb =
kUICompositorLargeDisplayMemoryLimitMB;
}
if (base::SysInfo::AmountOfPhysicalMemoryMB() >=
kUICompositorMemoryLimitRamCapacityThreshold) {
params_with_bounds.compositor_memory_limit_mb =
kUICompositorLargeRamMemoryLimitMB;
}
// The AshWindowTreeHost ends up owned by the RootWindowControllers created
// by this class.
AshWindowTreeHost* ash_host =
AshWindowTreeHost::Create(params_with_bounds).release();
aura::WindowTreeHost* host = ash_host->AsWindowTreeHost();
Shell::Get()->frame_throttling_controller()->OnWindowTreeHostCreated(host);
Shell::Get()->refresh_rate_controller()->OnWindowTreeHostCreated(host);
DCHECK(!host->has_input_method());
if (!input_method_) { // Singleton input method instance for Ash.
input_method_ = ui::CreateInputMethod(this, host->GetAcceleratedWidget());
// Makes sure the input method is focused by default when created, because
// Ash uses singleton InputMethod and it won't call OnFocus/OnBlur when
// the active window changed.
input_method_->OnFocus();
}
host->SetSharedInputMethod(input_method_.get());
host->window()->SetName(base::StringPrintf(
"%sRootWindow-%d", params_with_bounds.offscreen ? "Offscreen" : "",
host_count++));
host->window()->SetTitle(base::UTF8ToUTF16(display_info.name()));
host->compositor()->SetBackgroundColor(SK_ColorBLACK);
// No need to remove our observer observer because the WindowTreeHostManager
// outlives the host.
host->AddObserver(this);
InitRootWindowSettings(host->window())->display_id = display.id();
host->InitHost();
host->window()->Show();
window_tree_hosts_[display.id()] = ash_host;
SetDisplayPropertiesOnHost(ash_host, display);
ash_host->ConfineCursorToRootWindow();
return ash_host;
}
} // namespace ash
|