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" |
| 9 | #include "base/trace_event/trace_event_argument.h" |
| 10 | #include "components/exo/surface.h" |
| 11 | |
| 12 | namespace exo { |
| 13 | |
| 14 | //////////////////////////////////////////////////////////////////////////////// |
| 15 | // SubSurface, public: |
| 16 | |
| 17 | SubSurface::SubSurface(Surface* surface, Surface* parent) |
| 18 | : surface_(surface), parent_(parent), is_synchronized_(true) { |
| 19 | surface_->SetSurfaceDelegate(this); |
| 20 | surface_->AddSurfaceObserver(this); |
| 21 | parent_->AddSurfaceObserver(this); |
| 22 | parent_->AddSubSurface(surface_); |
| 23 | } |
| 24 | |
| 25 | SubSurface::~SubSurface() { |
| 26 | if (surface_) { |
| 27 | if (parent_) |
| 28 | parent_->RemoveSubSurface(surface_); |
| 29 | surface_->SetSurfaceDelegate(nullptr); |
| 30 | surface_->RemoveSurfaceObserver(this); |
| 31 | } |
| 32 | if (parent_) |
| 33 | parent_->RemoveSurfaceObserver(this); |
| 34 | } |
| 35 | |
| 36 | void SubSurface::SetPosition(const gfx::Point& position) { |
| 37 | TRACE_EVENT1("exo", "SubSurface::SetPosition", "position", |
| 38 | position.ToString()); |
| 39 | |
| 40 | if (!parent_ || !surface_) |
| 41 | return; |
| 42 | |
| 43 | parent_->SetSubSurfacePosition(surface_, position); |
| 44 | } |
| 45 | |
| 46 | void SubSurface::PlaceAbove(Surface* reference) { |
| 47 | TRACE_EVENT1("exo", "SubSurface::PlaceAbove", "reference", |
| 48 | reference->AsTracedValue()); |
| 49 | |
| 50 | if (!parent_ || !surface_) |
| 51 | return; |
| 52 | |
| 53 | if (reference == surface_) { |
| 54 | DLOG(WARNING) << "Client tried to place sub-surface above itself"; |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | parent_->PlaceSubSurfaceAbove(surface_, reference); |
| 59 | } |
| 60 | |
| 61 | void SubSurface::PlaceBelow(Surface* sibling) { |
| 62 | TRACE_EVENT1("exo", "SubSurface::PlaceBelow", "sibling", |
| 63 | sibling->AsTracedValue()); |
| 64 | |
| 65 | if (!parent_ || !surface_) |
| 66 | return; |
| 67 | |
| 68 | if (sibling == surface_) { |
| 69 | DLOG(WARNING) << "Client tried to place sub-surface below itself"; |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | parent_->PlaceSubSurfaceBelow(surface_, sibling); |
| 74 | } |
| 75 | |
| 76 | void SubSurface::SetCommitBehavior(bool synchronized) { |
| 77 | TRACE_EVENT1("exo", "SubSurface::SetCommitBehavior", "synchronized", |
| 78 | synchronized); |
| 79 | |
| 80 | is_synchronized_ = synchronized; |
| 81 | } |
| 82 | |
dcheng | 31759da | 2016-04-21 01:26:31 | [diff] [blame^] | 83 | std::unique_ptr<base::trace_event::TracedValue> SubSurface::AsTracedValue() |
| 84 | const { |
| 85 | std::unique_ptr<base::trace_event::TracedValue> value( |
primiano | cb1afb3 | 2016-02-29 20:46:05 | [diff] [blame] | 86 | new base::trace_event::TracedValue()); |
reveman | 27fe264 | 2015-11-20 06:33:39 | [diff] [blame] | 87 | value->SetBoolean("is_synchronized", is_synchronized_); |
| 88 | return value; |
| 89 | } |
| 90 | |
| 91 | //////////////////////////////////////////////////////////////////////////////// |
| 92 | // SurfaceDelegate overrides: |
| 93 | |
| 94 | void SubSurface::OnSurfaceCommit() { |
| 95 | // Early out if commit should be synchronized with parent. |
| 96 | if (IsSurfaceSynchronized()) |
| 97 | return; |
| 98 | |
| 99 | surface_->CommitSurfaceHierarchy(); |
| 100 | } |
| 101 | |
| 102 | bool SubSurface::IsSurfaceSynchronized() const { |
| 103 | // A sub-surface is effectively synchronized if either its parent is |
| 104 | // synchronized or itself is in synchronized mode. |
| 105 | if (is_synchronized_) |
| 106 | return true; |
| 107 | |
| 108 | return parent_ ? parent_->IsSynchronized() : false; |
| 109 | } |
| 110 | |
| 111 | //////////////////////////////////////////////////////////////////////////////// |
| 112 | // SurfaceObserver overrides: |
| 113 | |
| 114 | void SubSurface::OnSurfaceDestroying(Surface* surface) { |
| 115 | surface->RemoveSurfaceObserver(this); |
| 116 | if (surface == parent_) { |
| 117 | parent_ = nullptr; |
| 118 | return; |
| 119 | } |
| 120 | DCHECK(surface == surface_); |
| 121 | if (parent_) |
| 122 | parent_->RemoveSubSurface(surface_); |
| 123 | surface_ = nullptr; |
| 124 | } |
| 125 | |
| 126 | } // namespace exo |