reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 1 | // 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' Turner | ea07114 | 2018-10-29 15:38:12 | [diff] [blame] | 9 | #include "base/trace_event/traced_value.h" |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 10 | #include "components/exo/surface.h" |
Yuichiro Hanada | 3a6c31d0 | 2020-06-03 08:45:12 | [diff] [blame^] | 11 | #include "ui/aura/client/aura_constants.h" |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 12 | |
| 13 | namespace exo { |
| 14 | |
| 15 | //////////////////////////////////////////////////////////////////////////////// |
| 16 | // SubSurface, public: |
| 17 | |
| 18 | SubSurface::SubSurface(Surface* surface, Surface* parent) |
reveman | 2d3815d | 2016-06-26 20:13:25 | [diff] [blame] | 19 | : surface_(surface), parent_(parent) { |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 20 | surface_->SetSurfaceDelegate(this); |
| 21 | surface_->AddSurfaceObserver(this); |
| 22 | parent_->AddSurfaceObserver(this); |
| 23 | parent_->AddSubSurface(surface_); |
| 24 | } |
| 25 | |
| 26 | SubSurface::~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 | |
| 37 | void 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 | |
| 47 | void 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 | |
| 62 | void 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 | |
| 77 | void SubSurface::SetCommitBehavior(bool synchronized) { |
| 78 | TRACE_EVENT1("exo", "SubSurface::SetCommitBehavior", "synchronized", |
| 79 | synchronized); |
| 80 | |
| 81 | is_synchronized_ = synchronized; |
| 82 | } |
| 83 | |
dcheng | 31759da | 2016-04-21 01:26:31 | [diff] [blame] | 84 | std::unique_ptr<base::trace_event::TracedValue> SubSurface::AsTracedValue() |
| 85 | const { |
| 86 | std::unique_ptr<base::trace_event::TracedValue> value( |
primiano | cb1afb3 | 2016-02-29 20:46:05 | [diff] [blame] | 87 | new base::trace_event::TracedValue()); |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 88 | value->SetBoolean("is_synchronized", is_synchronized_); |
| 89 | return value; |
| 90 | } |
| 91 | |
| 92 | //////////////////////////////////////////////////////////////////////////////// |
| 93 | // SurfaceDelegate overrides: |
| 94 | |
| 95 | void SubSurface::OnSurfaceCommit() { |
| 96 | // Early out if commit should be synchronized with parent. |
| 97 | if (IsSurfaceSynchronized()) |
| 98 | return; |
| 99 | |
David Reveman | 8b43b35 | 2017-11-03 15:24:51 | [diff] [blame] | 100 | if (parent_) |
| 101 | parent_->OnSubSurfaceCommit(); |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 102 | } |
| 103 | |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 104 | bool 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 Laskowski | 3e2f9479 | 2017-12-15 00:27:10 | [diff] [blame] | 110 | return parent_ && parent_->IsSynchronized(); |
| 111 | } |
| 112 | |
Dominik Laskowski | 14a16377 | 2018-02-09 19:25:18 | [diff] [blame] | 113 | bool SubSurface::IsInputEnabled(Surface* surface) const { |
| 114 | return !parent_ || parent_->IsInputEnabled(surface); |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 115 | } |
| 116 | |
Yuichiro Hanada | 3a6c31d0 | 2020-06-03 08:45:12 | [diff] [blame^] | 117 | void 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 | |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 122 | //////////////////////////////////////////////////////////////////////////////// |
| 123 | // SurfaceObserver overrides: |
| 124 | |
| 125 | void 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 |