Avi Drissman | 3a215d1e | 2022-09-07 19:43:09 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "ash/host/ash_window_tree_host_unified.h" |
dcheng | cbf0d9d | 2015-12-27 22:49:23 | [diff] [blame] | 6 | |
Mitsuru Oshima | 04b54d0 | 2017-10-09 14:22:45 | [diff] [blame] | 7 | #include <memory> |
Avi Drissman | 4155d51d | 2022-01-10 20:40:13 | [diff] [blame] | 8 | #include <tuple> |
dcheng | cbf0d9d | 2015-12-27 22:49:23 | [diff] [blame] | 9 | #include <utility> |
| 10 | |
Addison Luh | 9b0f193c | 2022-02-24 20:56:14 | [diff] [blame] | 11 | #include "ash/host/ash_window_tree_host_delegate.h" |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 12 | #include "ash/host/root_window_transformer.h" |
Hans Wennborg | 3769645 | 2020-04-24 18:28:49 | [diff] [blame] | 13 | #include "base/check.h" |
Jan Wilken Dörrie | b5a41c3 | 2020-12-09 18:55:47 | [diff] [blame] | 14 | #include "base/containers/contains.h" |
Avi Drissman | d74df41 | 2024-07-23 17:35:28 | [diff] [blame] | 15 | #include "base/memory/raw_ptr.h" |
Hans Wennborg | 3769645 | 2020-04-24 18:28:49 | [diff] [blame] | 16 | #include "base/notreached.h" |
Peter Kasting | 8e5bc8b | 2022-09-08 11:11:37 | [diff] [blame] | 17 | #include "base/ranges/algorithm.h" |
Avi Drissman | d74df41 | 2024-07-23 17:35:28 | [diff] [blame] | 18 | #include "base/types/cxx23_to_underlying.h" |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 19 | #include "ui/aura/window.h" |
| 20 | #include "ui/aura/window_event_dispatcher.h" |
| 21 | #include "ui/aura/window_targeter.h" |
| 22 | #include "ui/compositor/compositor.h" |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 23 | #include "ui/gfx/geometry/insets.h" |
sadrul | 4f92369 | 2016-02-05 05:21:24 | [diff] [blame] | 24 | #include "ui/platform_window/stub/stub_window.h" |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 25 | |
| 26 | namespace ash { |
| 27 | |
| 28 | class UnifiedEventTargeter : public aura::WindowTargeter { |
| 29 | public: |
Ahmed Fakhry | 4f8e372 | 2017-10-31 21:01:58 | [diff] [blame] | 30 | UnifiedEventTargeter(aura::Window* src_root, |
| 31 | aura::Window* dst_root, |
Addison Luh | 9b0f193c | 2022-02-24 20:56:14 | [diff] [blame] | 32 | AshWindowTreeHostDelegate* delegate) |
Ahmed Fakhry | 4f8e372 | 2017-10-31 21:01:58 | [diff] [blame] | 33 | : src_root_(src_root), dst_root_(dst_root), delegate_(delegate) { |
| 34 | DCHECK(delegate); |
| 35 | } |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 36 | |
Peter Boström | 5fc1d31 | 2021-09-24 02:32:15 | [diff] [blame] | 37 | UnifiedEventTargeter(const UnifiedEventTargeter&) = delete; |
| 38 | UnifiedEventTargeter& operator=(const UnifiedEventTargeter&) = delete; |
Mitsuru Oshima | 03b7d4da | 2022-09-09 17:40:18 | [diff] [blame] | 39 | ~UnifiedEventTargeter() override { delegate_ = nullptr; } |
Peter Boström | 5fc1d31 | 2021-09-24 02:32:15 | [diff] [blame] | 40 | |
Mitsuru Oshima | 3f1cf2aa | 2023-03-22 00:09:42 | [diff] [blame] | 41 | // aura::WindowTargeter: |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 42 | ui::EventTarget* FindTargetForEvent(ui::EventTarget* root, |
| 43 | ui::Event* event) override { |
Mitsuru Oshima | 3f1cf2aa | 2023-03-22 00:09:42 | [diff] [blame] | 44 | delegate_->SetCurrentEventTargeterSourceHost(nullptr); |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 45 | if (root == src_root_ && !event->target()) { |
Mitsuru Oshima | 3f1cf2aa | 2023-03-22 00:09:42 | [diff] [blame] | 46 | return root; |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 47 | } else { |
Peter Boström | 7e4318e | 2024-08-16 02:25:07 | [diff] [blame] | 48 | NOTREACHED() << "event type:" << base::to_underlying(event->type()); |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 49 | } |
| 50 | } |
Mitsuru Oshima | 3f1cf2aa | 2023-03-22 00:09:42 | [diff] [blame] | 51 | ui::EventSink* GetNewEventSinkForEvent(const ui::EventTarget* current_root, |
| 52 | ui::EventTarget* target, |
| 53 | ui::Event* in_out_event) override { |
| 54 | if (current_root == src_root_ && !in_out_event->target()) { |
| 55 | delegate_->SetCurrentEventTargeterSourceHost(src_root_->GetHost()); |
| 56 | if (in_out_event->IsLocatedEvent()) { |
| 57 | ui::LocatedEvent* located_event = in_out_event->AsLocatedEvent(); |
| 58 | located_event->ConvertLocationToTarget( |
Arthur Sonzogni | 834e018f | 2023-04-22 10:20:02 | [diff] [blame] | 59 | static_cast<aura::Window*>(nullptr), dst_root_.get()); |
Mitsuru Oshima | 3f1cf2aa | 2023-03-22 00:09:42 | [diff] [blame] | 60 | } |
| 61 | return dst_root_->GetHost()->GetEventSink(); |
| 62 | } |
| 63 | return nullptr; |
| 64 | } |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 65 | |
Ahmed Fakhry | 4f8e372 | 2017-10-31 21:01:58 | [diff] [blame] | 66 | private: |
Bartek Nowierski | deb7584 | 2023-12-27 02:32:42 | [diff] [blame] | 67 | raw_ptr<aura::Window> src_root_; |
| 68 | raw_ptr<aura::Window> dst_root_; |
| 69 | raw_ptr<AshWindowTreeHostDelegate> delegate_; // Not owned. |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | AshWindowTreeHostUnified::AshWindowTreeHostUnified( |
Ahmed Fakhry | 4f8e372 | 2017-10-31 21:01:58 | [diff] [blame] | 73 | const gfx::Rect& initial_bounds, |
Addison Luh | 592ab5a | 2022-03-10 22:54:50 | [diff] [blame] | 74 | AshWindowTreeHostDelegate* delegate, |
| 75 | size_t compositor_memory_limit_mb) |
Mitsuru Oshima | 90cba8d | 2022-04-01 20:25:00 | [diff] [blame] | 76 | : AshWindowTreeHostPlatform( |
| 77 | std::make_unique<ui::StubWindow>(initial_bounds), |
| 78 | delegate, |
| 79 | compositor_memory_limit_mb) { |
| 80 | ui::StubWindow* stub_window = static_cast<ui::StubWindow*>(platform_window()); |
| 81 | stub_window->InitDelegate(this, true); |
Mitsuru Oshima | f6cc190 | 2024-08-07 21:38:47 | [diff] [blame] | 82 | // TODO(b/356098565): Remove the log once the issue is fixed. |
| 83 | LOG(ERROR) << "Creating Unified Desktop bounds=" << initial_bounds.ToString(); |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | AshWindowTreeHostUnified::~AshWindowTreeHostUnified() { |
Ali Hijazi | e63cbaf6 | 2023-12-20 19:29:35 | [diff] [blame] | 87 | for (ash::AshWindowTreeHost* ash_host : mirroring_hosts_) { |
oshima | c2f6958 | 2015-05-20 22:04:54 | [diff] [blame] | 88 | ash_host->AsWindowTreeHost()->window()->RemoveObserver(this); |
Ali Hijazi | e63cbaf6 | 2023-12-20 19:29:35 | [diff] [blame] | 89 | } |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void AshWindowTreeHostUnified::PrepareForShutdown() { |
sadrul | 4f92369 | 2016-02-05 05:21:24 | [diff] [blame] | 93 | AshWindowTreeHostPlatform::PrepareForShutdown(); |
oshima | b8fb0b7 | 2015-04-28 18:25:10 | [diff] [blame] | 94 | |
Ali Hijazi | e63cbaf6 | 2023-12-20 19:29:35 | [diff] [blame] | 95 | for (ash::AshWindowTreeHost* host : mirroring_hosts_) { |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 96 | host->PrepareForShutdown(); |
Ali Hijazi | e63cbaf6 | 2023-12-20 19:29:35 | [diff] [blame] | 97 | } |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void AshWindowTreeHostUnified::RegisterMirroringHost( |
| 101 | AshWindowTreeHost* mirroring_ash_host) { |
| 102 | aura::Window* src_root = mirroring_ash_host->AsWindowTreeHost()->window(); |
| 103 | src_root->SetEventTargeter( |
Ahmed Fakhry | 4f8e372 | 2017-10-31 21:01:58 | [diff] [blame] | 104 | std::make_unique<UnifiedEventTargeter>(src_root, window(), delegate_)); |
Jan Wilken Dörrie | 58b0c4b | 2019-06-06 18:44:34 | [diff] [blame] | 105 | DCHECK(!base::Contains(mirroring_hosts_, mirroring_ash_host)); |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 106 | mirroring_hosts_.push_back(mirroring_ash_host); |
Addison Luh | 9b0f193c | 2022-02-24 20:56:14 | [diff] [blame] | 107 | src_root->AddObserver(this); |
| 108 | mirroring_ash_host->UpdateCursorConfig(); |
| 109 | } |
| 110 | |
| 111 | // Do nothing, since mirroring hosts had their cursor config updated when they |
| 112 | // were registered. |
| 113 | void AshWindowTreeHostUnified::UpdateCursorConfig() {} |
| 114 | |
| 115 | void AshWindowTreeHostUnified::ClearCursorConfig() { |
Ali Hijazi | e63cbaf6 | 2023-12-20 19:29:35 | [diff] [blame] | 116 | for (ash::AshWindowTreeHost* host : mirroring_hosts_) { |
Addison Luh | 9b0f193c | 2022-02-24 20:56:14 | [diff] [blame] | 117 | host->ClearCursorConfig(); |
Ali Hijazi | e63cbaf6 | 2023-12-20 19:29:35 | [diff] [blame] | 118 | } |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 119 | } |
| 120 | |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 121 | void AshWindowTreeHostUnified::SetCursorNative(gfx::NativeCursor cursor) { |
Ali Hijazi | e63cbaf6 | 2023-12-20 19:29:35 | [diff] [blame] | 122 | for (ash::AshWindowTreeHost* host : mirroring_hosts_) { |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 123 | host->AsWindowTreeHost()->SetCursor(cursor); |
Ali Hijazi | e63cbaf6 | 2023-12-20 19:29:35 | [diff] [blame] | 124 | } |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 125 | } |
| 126 | |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 127 | void AshWindowTreeHostUnified::OnCursorVisibilityChangedNative(bool show) { |
Ali Hijazi | e63cbaf6 | 2023-12-20 19:29:35 | [diff] [blame] | 128 | for (ash::AshWindowTreeHost* host : mirroring_hosts_) { |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 129 | host->AsWindowTreeHost()->OnCursorVisibilityChanged(show); |
Ali Hijazi | e63cbaf6 | 2023-12-20 19:29:35 | [diff] [blame] | 130 | } |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 131 | } |
| 132 | |
Mitsuru Oshima | 410e5dc3 | 2022-07-26 20:01:43 | [diff] [blame] | 133 | void AshWindowTreeHostUnified::OnBoundsChanged(const BoundsChange& change) { |
sadrul | 4f92369 | 2016-02-05 05:21:24 | [diff] [blame] | 134 | if (platform_window()) |
Mitsuru Oshima | 410e5dc3 | 2022-07-26 20:01:43 | [diff] [blame] | 135 | OnHostResizedInPixels(platform_window()->GetBoundsInPixels().size()); |
sadrul | 4f92369 | 2016-02-05 05:21:24 | [diff] [blame] | 136 | } |
| 137 | |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 138 | void AshWindowTreeHostUnified::OnWindowDestroying(aura::Window* window) { |
Peter Kasting | 8e5bc8b | 2022-09-08 11:11:37 | [diff] [blame] | 139 | auto iter = base::ranges::find( |
| 140 | mirroring_hosts_, window, [](AshWindowTreeHost* ash_host) { |
| 141 | return ash_host->AsWindowTreeHost()->window(); |
| 142 | }); |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 143 | DCHECK(iter != mirroring_hosts_.end()); |
| 144 | window->RemoveObserver(this); |
| 145 | mirroring_hosts_.erase(iter); |
| 146 | } |
| 147 | |
oshima | 55ef921 | 2015-04-27 23:27:03 | [diff] [blame] | 148 | } // namespace ash |