blob: 44718e11b2c808f0ff10c640e9e9b85f0d4171e0 [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"
Arthur Sonzogni834e018f2023-04-22 10:20:026#include "base/memory/raw_ptr.h"
dchengcbf0d9d2015-12-27 22:49:237
Mitsuru Oshima04b54d02017-10-09 14:22:458#include <memory>
Avi Drissman4155d51d2022-01-10 20:40:139#include <tuple>
dchengcbf0d9d2015-12-27 22:49:2310#include <utility>
11
Addison Luh9b0f193c2022-02-24 20:56:1412#include "ash/host/ash_window_tree_host_delegate.h"
oshima55ef9212015-04-27 23:27:0313#include "ash/host/root_window_transformer.h"
Hans Wennborg37696452020-04-24 18:28:4914#include "base/check.h"
Jan Wilken Dörrieb5a41c32020-12-09 18:55:4715#include "base/containers/contains.h"
Hans Wennborg37696452020-04-24 18:28:4916#include "base/notreached.h"
Peter Kasting8e5bc8b2022-09-08 11:11:3717#include "base/ranges/algorithm.h"
oshima55ef9212015-04-27 23:27:0318#include "ui/aura/window.h"
19#include "ui/aura/window_event_dispatcher.h"
20#include "ui/aura/window_targeter.h"
21#include "ui/compositor/compositor.h"
oshima55ef9212015-04-27 23:27:0322#include "ui/gfx/geometry/insets.h"
sadrul4f923692016-02-05 05:21:2423#include "ui/platform_window/stub/stub_window.h"
oshima55ef9212015-04-27 23:27:0324
25namespace ash {
26
27class UnifiedEventTargeter : public aura::WindowTargeter {
28 public:
Ahmed Fakhry4f8e3722017-10-31 21:01:5829 UnifiedEventTargeter(aura::Window* src_root,
30 aura::Window* dst_root,
Addison Luh9b0f193c2022-02-24 20:56:1431 AshWindowTreeHostDelegate* delegate)
Ahmed Fakhry4f8e3722017-10-31 21:01:5832 : src_root_(src_root), dst_root_(dst_root), delegate_(delegate) {
33 DCHECK(delegate);
34 }
oshima55ef9212015-04-27 23:27:0335
Peter Boström5fc1d312021-09-24 02:32:1536 UnifiedEventTargeter(const UnifiedEventTargeter&) = delete;
37 UnifiedEventTargeter& operator=(const UnifiedEventTargeter&) = delete;
Mitsuru Oshima03b7d4da2022-09-09 17:40:1838 ~UnifiedEventTargeter() override { delegate_ = nullptr; }
Peter Boström5fc1d312021-09-24 02:32:1539
Mitsuru Oshima3f1cf2aa2023-03-22 00:09:4240 // aura::WindowTargeter:
oshima55ef9212015-04-27 23:27:0341 ui::EventTarget* FindTargetForEvent(ui::EventTarget* root,
42 ui::Event* event) override {
Mitsuru Oshima3f1cf2aa2023-03-22 00:09:4243 delegate_->SetCurrentEventTargeterSourceHost(nullptr);
oshima55ef9212015-04-27 23:27:0344 if (root == src_root_ && !event->target()) {
Mitsuru Oshima3f1cf2aa2023-03-22 00:09:4245 return root;
oshima55ef9212015-04-27 23:27:0346 } else {
oshimaf8b27692015-04-30 07:23:0047 NOTREACHED() << "event type:" << event->type();
oshima55ef9212015-04-27 23:27:0348 return aura::WindowTargeter::FindTargetForEvent(root, event);
49 }
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);
oshima55ef9212015-04-27 23:27:0382}
83
84AshWindowTreeHostUnified::~AshWindowTreeHostUnified() {
Ali Hijazie63cbaf62023-12-20 19:29:3585 for (ash::AshWindowTreeHost* ash_host : mirroring_hosts_) {
oshimac2f69582015-05-20 22:04:5486 ash_host->AsWindowTreeHost()->window()->RemoveObserver(this);
Ali Hijazie63cbaf62023-12-20 19:29:3587 }
oshima55ef9212015-04-27 23:27:0388}
89
90void AshWindowTreeHostUnified::PrepareForShutdown() {
sadrul4f923692016-02-05 05:21:2491 AshWindowTreeHostPlatform::PrepareForShutdown();
oshimab8fb0b72015-04-28 18:25:1092
Ali Hijazie63cbaf62023-12-20 19:29:3593 for (ash::AshWindowTreeHost* host : mirroring_hosts_) {
oshima55ef9212015-04-27 23:27:0394 host->PrepareForShutdown();
Ali Hijazie63cbaf62023-12-20 19:29:3595 }
oshima55ef9212015-04-27 23:27:0396}
97
98void AshWindowTreeHostUnified::RegisterMirroringHost(
99 AshWindowTreeHost* mirroring_ash_host) {
100 aura::Window* src_root = mirroring_ash_host->AsWindowTreeHost()->window();
101 src_root->SetEventTargeter(
Ahmed Fakhry4f8e3722017-10-31 21:01:58102 std::make_unique<UnifiedEventTargeter>(src_root, window(), delegate_));
Jan Wilken Dörrie58b0c4b2019-06-06 18:44:34103 DCHECK(!base::Contains(mirroring_hosts_, mirroring_ash_host));
oshima55ef9212015-04-27 23:27:03104 mirroring_hosts_.push_back(mirroring_ash_host);
Addison Luh9b0f193c2022-02-24 20:56:14105 src_root->AddObserver(this);
106 mirroring_ash_host->UpdateCursorConfig();
107}
108
109// Do nothing, since mirroring hosts had their cursor config updated when they
110// were registered.
111void AshWindowTreeHostUnified::UpdateCursorConfig() {}
112
113void AshWindowTreeHostUnified::ClearCursorConfig() {
Ali Hijazie63cbaf62023-12-20 19:29:35114 for (ash::AshWindowTreeHost* host : mirroring_hosts_) {
Addison Luh9b0f193c2022-02-24 20:56:14115 host->ClearCursorConfig();
Ali Hijazie63cbaf62023-12-20 19:29:35116 }
oshima55ef9212015-04-27 23:27:03117}
118
oshima55ef9212015-04-27 23:27:03119void AshWindowTreeHostUnified::SetCursorNative(gfx::NativeCursor cursor) {
Ali Hijazie63cbaf62023-12-20 19:29:35120 for (ash::AshWindowTreeHost* host : mirroring_hosts_) {
oshima55ef9212015-04-27 23:27:03121 host->AsWindowTreeHost()->SetCursor(cursor);
Ali Hijazie63cbaf62023-12-20 19:29:35122 }
oshima55ef9212015-04-27 23:27:03123}
124
oshima55ef9212015-04-27 23:27:03125void AshWindowTreeHostUnified::OnCursorVisibilityChangedNative(bool show) {
Ali Hijazie63cbaf62023-12-20 19:29:35126 for (ash::AshWindowTreeHost* host : mirroring_hosts_) {
oshima55ef9212015-04-27 23:27:03127 host->AsWindowTreeHost()->OnCursorVisibilityChanged(show);
Ali Hijazie63cbaf62023-12-20 19:29:35128 }
oshima55ef9212015-04-27 23:27:03129}
130
Mitsuru Oshima410e5dc32022-07-26 20:01:43131void AshWindowTreeHostUnified::OnBoundsChanged(const BoundsChange& change) {
sadrul4f923692016-02-05 05:21:24132 if (platform_window())
Mitsuru Oshima410e5dc32022-07-26 20:01:43133 OnHostResizedInPixels(platform_window()->GetBoundsInPixels().size());
sadrul4f923692016-02-05 05:21:24134}
135
oshima55ef9212015-04-27 23:27:03136void AshWindowTreeHostUnified::OnWindowDestroying(aura::Window* window) {
Peter Kasting8e5bc8b2022-09-08 11:11:37137 auto iter = base::ranges::find(
138 mirroring_hosts_, window, [](AshWindowTreeHost* ash_host) {
139 return ash_host->AsWindowTreeHost()->window();
140 });
oshima55ef9212015-04-27 23:27:03141 DCHECK(iter != mirroring_hosts_.end());
142 window->RemoveObserver(this);
143 mirroring_hosts_.erase(iter);
144}
145
oshima55ef9212015-04-27 23:27:03146} // namespace ash