blob: 8c81ed507feb15ae907138fe151840bfc6c4954f [file] [log] [blame]
reveman27fe2642015-11-20 06:33:391// Copyright 2015 The Chromium Authors. All rights reserved.
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 "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"
reveman27fe2642015-11-20 06:33:3910#include "components/exo/surface.h"
Yuichiro Hanada3a6c31d02020-06-03 08:45:1211#include "ui/aura/client/aura_constants.h"
reveman27fe2642015-11-20 06:33:3912
13namespace exo {
14
15////////////////////////////////////////////////////////////////////////////////
16// SubSurface, public:
17
18SubSurface::SubSurface(Surface* surface, Surface* parent)
reveman2d3815d2016-06-26 20:13:2519 : surface_(surface), parent_(parent) {
reveman27fe2642015-11-20 06:33:3920 surface_->SetSurfaceDelegate(this);
21 surface_->AddSurfaceObserver(this);
22 parent_->AddSurfaceObserver(this);
23 parent_->AddSubSurface(surface_);
24}
25
26SubSurface::~SubSurface() {
27 if (surface_) {
28 if (parent_)
29 parent_->RemoveSubSurface(surface_);
30 surface_->SetSurfaceDelegate(nullptr);
31 surface_->RemoveSurfaceObserver(this);
32 }
33 if (parent_)
34 parent_->RemoveSurfaceObserver(this);
35}
36
37void SubSurface::SetPosition(const gfx::Point& position) {
38 TRACE_EVENT1("exo", "SubSurface::SetPosition", "position",
39 position.ToString());
40
41 if (!parent_ || !surface_)
42 return;
43
44 parent_->SetSubSurfacePosition(surface_, position);
45}
46
47void SubSurface::PlaceAbove(Surface* reference) {
48 TRACE_EVENT1("exo", "SubSurface::PlaceAbove", "reference",
49 reference->AsTracedValue());
50
51 if (!parent_ || !surface_)
52 return;
53
54 if (reference == surface_) {
55 DLOG(WARNING) << "Client tried to place sub-surface above itself";
56 return;
57 }
58
59 parent_->PlaceSubSurfaceAbove(surface_, reference);
60}
61
62void SubSurface::PlaceBelow(Surface* sibling) {
63 TRACE_EVENT1("exo", "SubSurface::PlaceBelow", "sibling",
64 sibling->AsTracedValue());
65
66 if (!parent_ || !surface_)
67 return;
68
69 if (sibling == surface_) {
70 DLOG(WARNING) << "Client tried to place sub-surface below itself";
71 return;
72 }
73
74 parent_->PlaceSubSurfaceBelow(surface_, sibling);
75}
76
77void SubSurface::SetCommitBehavior(bool synchronized) {
78 TRACE_EVENT1("exo", "SubSurface::SetCommitBehavior", "synchronized",
79 synchronized);
80
81 is_synchronized_ = synchronized;
82}
83
dcheng31759da2016-04-21 01:26:3184std::unique_ptr<base::trace_event::TracedValue> SubSurface::AsTracedValue()
85 const {
86 std::unique_ptr<base::trace_event::TracedValue> value(
primianocb1afb32016-02-29 20:46:0587 new base::trace_event::TracedValue());
reveman27fe2642015-11-20 06:33:3988 value->SetBoolean("is_synchronized", is_synchronized_);
89 return value;
90}
91
92////////////////////////////////////////////////////////////////////////////////
93// SurfaceDelegate overrides:
94
95void SubSurface::OnSurfaceCommit() {
96 // Early out if commit should be synchronized with parent.
97 if (IsSurfaceSynchronized())
98 return;
99
David Reveman8b43b352017-11-03 15:24:51100 if (parent_)
101 parent_->OnSubSurfaceCommit();
reveman27fe2642015-11-20 06:33:39102}
103
reveman27fe2642015-11-20 06:33:39104bool SubSurface::IsSurfaceSynchronized() const {
105 // A sub-surface is effectively synchronized if either its parent is
106 // synchronized or itself is in synchronized mode.
107 if (is_synchronized_)
108 return true;
109
Dominik Laskowski3e2f94792017-12-15 00:27:10110 return parent_ && parent_->IsSynchronized();
111}
112
Dominik Laskowski14a163772018-02-09 19:25:18113bool SubSurface::IsInputEnabled(Surface* surface) const {
114 return !parent_ || parent_->IsInputEnabled(surface);
reveman27fe2642015-11-20 06:33:39115}
116
Yuichiro Hanada3a6c31d02020-06-03 08:45:12117void SubSurface::OnSetParent(Surface* parent, const gfx::Point&) {
118 if (parent->window()->GetProperty(aura::client::kSkipImeProcessing))
119 surface_->window()->SetProperty(aura::client::kSkipImeProcessing, true);
120}
121
reveman27fe2642015-11-20 06:33:39122////////////////////////////////////////////////////////////////////////////////
123// SurfaceObserver overrides:
124
125void SubSurface::OnSurfaceDestroying(Surface* surface) {
126 surface->RemoveSurfaceObserver(this);
127 if (surface == parent_) {
128 parent_ = nullptr;
129 return;
130 }
131 DCHECK(surface == surface_);
132 if (parent_)
133 parent_->RemoveSubSurface(surface_);
134 surface_ = nullptr;
135}
136
137} // namespace exo