blob: b60a6d0903acf0d9e86a44724091124f92741475 [file] [log] [blame]
Avi Drissman3a215d1e2022-09-07 19:43:091// Copyright 2015 The Chromium Authors
oshima55ef9212015-04-27 23:27:032// 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"
dchengcbf0d9d2015-12-27 22:49:236
Mitsuru Oshima04b54d02017-10-09 14:22:457#include <memory>
Avi Drissman4155d51d2022-01-10 20:40:138#include <tuple>
dchengcbf0d9d2015-12-27 22:49:239#include <utility>
10
Addison Luh9b0f193c2022-02-24 20:56:1411#include "ash/host/ash_window_tree_host_delegate.h"
oshima55ef9212015-04-27 23:27:0312#include "ash/host/root_window_transformer.h"
Hans Wennborg37696452020-04-24 18:28:4913#include "base/check.h"
Jan Wilken Dörrieb5a41c32020-12-09 18:55:4714#include "base/containers/contains.h"
Avi Drissmand74df412024-07-23 17:35:2815#include "base/memory/raw_ptr.h"
Hans Wennborg37696452020-04-24 18:28:4916#include "base/notreached.h"
Peter Kasting8e5bc8b2022-09-08 11:11:3717#include "base/ranges/algorithm.h"
Avi Drissmand74df412024-07-23 17:35:2818#include "base/types/cxx23_to_underlying.h"
oshima55ef9212015-04-27 23:27:0319#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"
oshima55ef9212015-04-27 23:27:0323#include "ui/gfx/geometry/insets.h"
sadrul4f923692016-02-05 05:21:2424#include "ui/platform_window/stub/stub_window.h"
oshima55ef9212015-04-27 23:27:0325
26namespace ash {
27
28class UnifiedEventTargeter : public aura::WindowTargeter {
29 public:
Ahmed Fakhry4f8e3722017-10-31 21:01:5830 UnifiedEventTargeter(aura::Window* src_root,
31 aura::Window* dst_root,
Addison Luh9b0f193c2022-02-24 20:56:1432 AshWindowTreeHostDelegate* delegate)
Ahmed Fakhry4f8e3722017-10-31 21:01:5833 : src_root_(src_root), dst_root_(dst_root), delegate_(delegate) {
34 DCHECK(delegate);
35 }
oshima55ef9212015-04-27 23:27:0336
Peter Boström5fc1d312021-09-24 02:32:1537 UnifiedEventTargeter(const UnifiedEventTargeter&) = delete;
38 UnifiedEventTargeter& operator=(const UnifiedEventTargeter&) = delete;
Mitsuru Oshima03b7d4da2022-09-09 17:40:1839 ~UnifiedEventTargeter() override { delegate_ = nullptr; }
Peter Boström5fc1d312021-09-24 02:32:1540
Mitsuru Oshima3f1cf2aa2023-03-22 00:09:4241 // aura::WindowTargeter:
oshima55ef9212015-04-27 23:27:0342 ui::EventTarget* FindTargetForEvent(ui::EventTarget* root,
43 ui::Event* event) override {
Mitsuru Oshima3f1cf2aa2023-03-22 00:09:4244 delegate_->SetCurrentEventTargeterSourceHost(nullptr);
oshima55ef9212015-04-27 23:27:0345 if (root == src_root_ && !event->target()) {
Mitsuru Oshima3f1cf2aa2023-03-22 00:09:4246 return root;
oshima55ef9212015-04-27 23:27:0347 } else {
Peter Boström7e4318e2024-08-16 02:25:0748 NOTREACHED() << "event type:" << base::to_underlying(event->type());
oshima55ef9212015-04-27 23:27:0349 }
50 }
Mitsuru Oshima3f1cf2aa2023-03-22 00:09:4251 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 Sonzogni834e018f2023-04-22 10:20:0259 static_cast<aura::Window*>(nullptr), dst_root_.get());
Mitsuru Oshima3f1cf2aa2023-03-22 00:09:4260 }
61 return dst_root_->GetHost()->GetEventSink();
62 }
63 return nullptr;
64 }
oshima55ef9212015-04-27 23:27:0365
Ahmed Fakhry4f8e3722017-10-31 21:01:5866 private:
Bartek Nowierskideb75842023-12-27 02:32:4267 raw_ptr<aura::Window> src_root_;
68 raw_ptr<aura::Window> dst_root_;
69 raw_ptr<AshWindowTreeHostDelegate> delegate_; // Not owned.
oshima55ef9212015-04-27 23:27:0370};
71
72AshWindowTreeHostUnified::AshWindowTreeHostUnified(
Ahmed Fakhry4f8e3722017-10-31 21:01:5873 const gfx::Rect& initial_bounds,
Addison Luh592ab5a2022-03-10 22:54:5074 AshWindowTreeHostDelegate* delegate,
75 size_t compositor_memory_limit_mb)
Mitsuru Oshima90cba8d2022-04-01 20:25:0076 : 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 Oshimaf6cc1902024-08-07 21:38:4782 // TODO(b/356098565): Remove the log once the issue is fixed.
83 LOG(ERROR) << "Creating Unified Desktop bounds=" << initial_bounds.ToString();
oshima55ef9212015-04-27 23:27:0384}
85
86AshWindowTreeHostUnified::~AshWindowTreeHostUnified() {
Ali Hijazie63cbaf62023-12-20 19:29:3587 for (ash::AshWindowTreeHost* ash_host : mirroring_hosts_) {
oshimac2f69582015-05-20 22:04:5488 ash_host->AsWindowTreeHost()->window()->RemoveObserver(this);
Ali Hijazie63cbaf62023-12-20 19:29:3589 }
oshima55ef9212015-04-27 23:27:0390}
91
92void AshWindowTreeHostUnified::PrepareForShutdown() {
sadrul4f923692016-02-05 05:21:2493 AshWindowTreeHostPlatform::PrepareForShutdown();
oshimab8fb0b72015-04-28 18:25:1094
Ali Hijazie63cbaf62023-12-20 19:29:3595 for (ash::AshWindowTreeHost* host : mirroring_hosts_) {
oshima55ef9212015-04-27 23:27:0396 host->PrepareForShutdown();
Ali Hijazie63cbaf62023-12-20 19:29:3597 }
oshima55ef9212015-04-27 23:27:0398}
99
100void AshWindowTreeHostUnified::RegisterMirroringHost(
101 AshWindowTreeHost* mirroring_ash_host) {
102 aura::Window* src_root = mirroring_ash_host->AsWindowTreeHost()->window();
103 src_root->SetEventTargeter(
Ahmed Fakhry4f8e3722017-10-31 21:01:58104 std::make_unique<UnifiedEventTargeter>(src_root, window(), delegate_));
Jan Wilken Dörrie58b0c4b2019-06-06 18:44:34105 DCHECK(!base::Contains(mirroring_hosts_, mirroring_ash_host));
oshima55ef9212015-04-27 23:27:03106 mirroring_hosts_.push_back(mirroring_ash_host);
Addison Luh9b0f193c2022-02-24 20:56:14107 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.
113void AshWindowTreeHostUnified::UpdateCursorConfig() {}
114
115void AshWindowTreeHostUnified::ClearCursorConfig() {
Ali Hijazie63cbaf62023-12-20 19:29:35116 for (ash::AshWindowTreeHost* host : mirroring_hosts_) {
Addison Luh9b0f193c2022-02-24 20:56:14117 host->ClearCursorConfig();
Ali Hijazie63cbaf62023-12-20 19:29:35118 }
oshima55ef9212015-04-27 23:27:03119}
120
oshima55ef9212015-04-27 23:27:03121void AshWindowTreeHostUnified::SetCursorNative(gfx::NativeCursor cursor) {
Ali Hijazie63cbaf62023-12-20 19:29:35122 for (ash::AshWindowTreeHost* host : mirroring_hosts_) {
oshima55ef9212015-04-27 23:27:03123 host->AsWindowTreeHost()->SetCursor(cursor);
Ali Hijazie63cbaf62023-12-20 19:29:35124 }
oshima55ef9212015-04-27 23:27:03125}
126
oshima55ef9212015-04-27 23:27:03127void AshWindowTreeHostUnified::OnCursorVisibilityChangedNative(bool show) {
Ali Hijazie63cbaf62023-12-20 19:29:35128 for (ash::AshWindowTreeHost* host : mirroring_hosts_) {
oshima55ef9212015-04-27 23:27:03129 host->AsWindowTreeHost()->OnCursorVisibilityChanged(show);
Ali Hijazie63cbaf62023-12-20 19:29:35130 }
oshima55ef9212015-04-27 23:27:03131}
132
Mitsuru Oshima410e5dc32022-07-26 20:01:43133void AshWindowTreeHostUnified::OnBoundsChanged(const BoundsChange& change) {
sadrul4f923692016-02-05 05:21:24134 if (platform_window())
Mitsuru Oshima410e5dc32022-07-26 20:01:43135 OnHostResizedInPixels(platform_window()->GetBoundsInPixels().size());
sadrul4f923692016-02-05 05:21:24136}
137
oshima55ef9212015-04-27 23:27:03138void AshWindowTreeHostUnified::OnWindowDestroying(aura::Window* window) {
Peter Kasting8e5bc8b2022-09-08 11:11:37139 auto iter = base::ranges::find(
140 mirroring_hosts_, window, [](AshWindowTreeHost* ash_host) {
141 return ash_host->AsWindowTreeHost()->window();
142 });
oshima55ef9212015-04-27 23:27:03143 DCHECK(iter != mirroring_hosts_.end());
144 window->RemoveObserver(this);
145 mirroring_hosts_.erase(iter);
146}
147
oshima55ef9212015-04-27 23:27:03148} // namespace ash