blob: 9b814d81bd4faf208dd8171bf7ec21e1fe294a68 [file] [log] [blame]
Avi Drissman8ba1bad2022-09-13 19:22:361// Copyright 2015 The Chromium Authors
reveman27fe2642015-11-20 06:33:392// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "components/exo/sub_surface.h"
6
7#include "base/logging.h"
8#include "base/trace_event/trace_event.h"
David 'Digit' Turnerea071142018-10-29 15:38:129#include "base/trace_event/traced_value.h"
Lucas Berthouf18ac602021-11-12 21:35:1010#include "components/exo/sub_surface_observer.h"
reveman27fe2642015-11-20 06:33:3911#include "components/exo/surface.h"
Yuichiro Hanada3a6c31d02020-06-03 08:45:1212#include "ui/aura/client/aura_constants.h"
Lucas Berthouf18ac602021-11-12 21:35:1013#include "ui/gfx/geometry/point_f.h"
reveman27fe2642015-11-20 06:33:3914
15namespace exo {
16
17////////////////////////////////////////////////////////////////////////////////
18// SubSurface, public:
19
20SubSurface::SubSurface(Surface* surface, Surface* parent)
reveman2d3815d2016-06-26 20:13:2521 : surface_(surface), parent_(parent) {
reveman27fe2642015-11-20 06:33:3922 surface_->SetSurfaceDelegate(this);
23 surface_->AddSurfaceObserver(this);
24 parent_->AddSurfaceObserver(this);
25 parent_->AddSubSurface(surface_);
26}
27
28SubSurface::~SubSurface() {
Lucas Berthouf18ac602021-11-12 21:35:1029 for (SubSurfaceObserver& observer : observers_)
30 observer.OnSubSurfaceDestroying(this);
31
reveman27fe2642015-11-20 06:33:3932 if (surface_) {
33 if (parent_)
34 parent_->RemoveSubSurface(surface_);
35 surface_->SetSurfaceDelegate(nullptr);
36 surface_->RemoveSurfaceObserver(this);
37 }
38 if (parent_)
39 parent_->RemoveSurfaceObserver(this);
Kramer Gea5f414a2024-03-27 16:49:4440
41 // Destroying a sub-surface takes effect immediately.
42 if (surface_ && parent_ && !surface_->is_augmented()) {
43 parent_->OnSubSurfaceCommit();
44 }
reveman27fe2642015-11-20 06:33:3945}
46
Lucas Berthouf18ac602021-11-12 21:35:1047void SubSurface::SetPosition(const gfx::PointF& position) {
reveman27fe2642015-11-20 06:33:3948 TRACE_EVENT1("exo", "SubSurface::SetPosition", "position",
49 position.ToString());
50
51 if (!parent_ || !surface_)
52 return;
53
54 parent_->SetSubSurfacePosition(surface_, position);
55}
56
River Gilhuly4b7c5cb2022-12-01 16:27:2557void SubSurface::SetTransform(const gfx::Transform& transform) {
58 TRACE_EVENT1("exo", "SubSurface::SetTransform", "transform",
59 transform.ToString());
60
61 if (!parent_ || !surface_)
62 return;
63
64 surface_->SetSurfaceTransform(transform);
65}
66
reveman27fe2642015-11-20 06:33:3967void SubSurface::PlaceAbove(Surface* reference) {
68 TRACE_EVENT1("exo", "SubSurface::PlaceAbove", "reference",
69 reference->AsTracedValue());
70
71 if (!parent_ || !surface_)
72 return;
73
74 if (reference == surface_) {
75 DLOG(WARNING) << "Client tried to place sub-surface above itself";
76 return;
77 }
78
79 parent_->PlaceSubSurfaceAbove(surface_, reference);
80}
81
82void SubSurface::PlaceBelow(Surface* sibling) {
83 TRACE_EVENT1("exo", "SubSurface::PlaceBelow", "sibling",
84 sibling->AsTracedValue());
85
86 if (!parent_ || !surface_)
87 return;
88
89 if (sibling == surface_) {
90 DLOG(WARNING) << "Client tried to place sub-surface below itself";
91 return;
92 }
93
94 parent_->PlaceSubSurfaceBelow(surface_, sibling);
95}
96
97void SubSurface::SetCommitBehavior(bool synchronized) {
98 TRACE_EVENT1("exo", "SubSurface::SetCommitBehavior", "synchronized",
99 synchronized);
100
Kramer Gea5f414a2024-03-27 16:49:44101 is_synchronized_ = surface_->is_augmented() || synchronized;
reveman27fe2642015-11-20 06:33:39102}
103
dcheng31759da2016-04-21 01:26:31104std::unique_ptr<base::trace_event::TracedValue> SubSurface::AsTracedValue()
105 const {
106 std::unique_ptr<base::trace_event::TracedValue> value(
primianocb1afb32016-02-29 20:46:05107 new base::trace_event::TracedValue());
reveman27fe2642015-11-20 06:33:39108 value->SetBoolean("is_synchronized", is_synchronized_);
109 return value;
110}
111
112////////////////////////////////////////////////////////////////////////////////
113// SurfaceDelegate overrides:
114
115void SubSurface::OnSurfaceCommit() {
116 // Early out if commit should be synchronized with parent.
117 if (IsSurfaceSynchronized())
118 return;
119
David Reveman8b43b352017-11-03 15:24:51120 if (parent_)
121 parent_->OnSubSurfaceCommit();
reveman27fe2642015-11-20 06:33:39122}
123
reveman27fe2642015-11-20 06:33:39124bool SubSurface::IsSurfaceSynchronized() const {
125 // A sub-surface is effectively synchronized if either its parent is
126 // synchronized or itself is in synchronized mode.
127 if (is_synchronized_)
128 return true;
129
Dominik Laskowski3e2f94792017-12-15 00:27:10130 return parent_ && parent_->IsSynchronized();
131}
132
Dominik Laskowski14a163772018-02-09 19:25:18133bool SubSurface::IsInputEnabled(Surface* surface) const {
134 return !parent_ || parent_->IsInputEnabled(surface);
reveman27fe2642015-11-20 06:33:39135}
136
Yuichiro Hanada3a6c31d02020-06-03 08:45:12137void SubSurface::OnSetParent(Surface* parent, const gfx::Point&) {
138 if (parent->window()->GetProperty(aura::client::kSkipImeProcessing))
139 surface_->window()->SetProperty(aura::client::kSkipImeProcessing, true);
140}
141
Nicholas Hollingumda837042022-07-18 23:46:49142SecurityDelegate* SubSurface::GetSecurityDelegate() {
Nicholas Hollingum93d735bb62022-05-25 05:10:35143 if (parent_)
Nicholas Hollingumda837042022-07-18 23:46:49144 return parent_->GetSecurityDelegate();
Nicholas Hollingum93d735bb62022-05-25 05:10:35145 return nullptr;
146}
147
reveman27fe2642015-11-20 06:33:39148////////////////////////////////////////////////////////////////////////////////
149// SurfaceObserver overrides:
150
151void SubSurface::OnSurfaceDestroying(Surface* surface) {
152 surface->RemoveSurfaceObserver(this);
153 if (surface == parent_) {
154 parent_ = nullptr;
155 return;
156 }
157 DCHECK(surface == surface_);
158 if (parent_)
159 parent_->RemoveSubSurface(surface_);
160 surface_ = nullptr;
161}
162
Lucas Berthouf18ac602021-11-12 21:35:10163////////////////////////////////////////////////////////////////////////////////
164// SubSurface Observers
165void SubSurface::AddSubSurfaceObserver(SubSurfaceObserver* observer) {
166 observers_.AddObserver(observer);
167}
168
169void SubSurface::RemoveSubSurfaceObserver(SubSurfaceObserver* observer) {
170 observers_.RemoveObserver(observer);
171}
172
reveman27fe2642015-11-20 06:33:39173} // namespace exo