OLD | NEW |
(Empty) | |
| 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/surface.h" |
| 6 |
| 7 #include "base/callback_helpers.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/trace_event/trace_event.h" |
| 10 #include "base/trace_event/trace_event_argument.h" |
| 11 #include "cc/resources/single_release_callback.h" |
| 12 #include "components/exo/buffer.h" |
| 13 #include "components/exo/surface_delegate.h" |
| 14 #include "ui/compositor/layer.h" |
| 15 #include "ui/gfx/buffer_format_util.h" |
| 16 #include "ui/gfx/gpu_memory_buffer.h" |
| 17 |
| 18 namespace exo { |
| 19 |
| 20 //////////////////////////////////////////////////////////////////////////////// |
| 21 // Surface, public: |
| 22 |
| 23 Surface::Surface() : compositor_(nullptr), delegate_(nullptr) { |
| 24 SetLayer(new ui::Layer(ui::LAYER_SOLID_COLOR)); |
| 25 set_owned_by_client(); |
| 26 } |
| 27 |
| 28 Surface::~Surface() { |
| 29 if (delegate_) |
| 30 delegate_->OnSurfaceDestroying(); |
| 31 |
| 32 layer()->SetShowSolidColorContent(); |
| 33 |
| 34 if (compositor_) |
| 35 compositor_->RemoveObserver(this); |
| 36 |
| 37 // Call pending frame callbacks with a null frame time to indicate that they |
| 38 // have been cancelled. |
| 39 frame_callbacks_.splice(frame_callbacks_.end(), pending_frame_callbacks_); |
| 40 active_frame_callbacks_.splice(active_frame_callbacks_.end(), |
| 41 frame_callbacks_); |
| 42 for (const auto& frame_callback : active_frame_callbacks_) |
| 43 frame_callback.Run(base::TimeTicks()); |
| 44 } |
| 45 |
| 46 void Surface::Attach(Buffer* buffer) { |
| 47 TRACE_EVENT1("exo", "Surface::Attach", "buffer", buffer->AsTracedValue()); |
| 48 |
| 49 pending_buffer_ = buffer ? buffer->AsWeakPtr() : base::WeakPtr<Buffer>(); |
| 50 PreferredSizeChanged(); |
| 51 } |
| 52 |
| 53 void Surface::Damage(const gfx::Rect& damage) { |
| 54 TRACE_EVENT1("exo", "Surface::Damage", "damage", damage.ToString()); |
| 55 |
| 56 pending_damage_.Union(damage); |
| 57 } |
| 58 |
| 59 void Surface::RequestFrameCallback(const FrameCallback& callback) { |
| 60 TRACE_EVENT0("exo", "Surface::RequestFrameCallback"); |
| 61 |
| 62 pending_frame_callbacks_.push_back(callback); |
| 63 } |
| 64 |
| 65 void Surface::SetOpaqueRegion(const SkRegion& region) { |
| 66 TRACE_EVENT1("exo", "Surface::SetOpaqueRegion", "region", |
| 67 gfx::SkIRectToRect(region.getBounds()).ToString()); |
| 68 |
| 69 pending_opaque_region_ = region; |
| 70 } |
| 71 |
| 72 void Surface::Commit() { |
| 73 TRACE_EVENT0("exo", "Surface::Commit"); |
| 74 |
| 75 if (delegate_) |
| 76 delegate_->OnSurfaceCommit(); |
| 77 |
| 78 cc::TextureMailbox texture_mailbox; |
| 79 scoped_ptr<cc::SingleReleaseCallback> texture_mailbox_release_callback; |
| 80 if (pending_buffer_) { |
| 81 texture_mailbox_release_callback = |
| 82 pending_buffer_->AcquireTextureMailbox(&texture_mailbox); |
| 83 pending_buffer_.reset(); |
| 84 } else { |
| 85 // Show solid color content if there is no pending buffer. |
| 86 layer()->SetShowSolidColorContent(); |
| 87 } |
| 88 |
| 89 if (texture_mailbox_release_callback) { |
| 90 // Update layer with the new contents. |
| 91 layer()->SetTextureMailbox(texture_mailbox, |
| 92 texture_mailbox_release_callback.Pass(), |
| 93 texture_mailbox.size_in_pixels()); |
| 94 layer()->SetTextureFlipped(false); |
| 95 layer()->SetBounds(gfx::Rect(layer()->bounds().origin(), |
| 96 texture_mailbox.size_in_pixels())); |
| 97 layer()->SetFillsBoundsOpaquely(pending_opaque_region_.contains( |
| 98 gfx::RectToSkIRect(gfx::Rect(texture_mailbox.size_in_pixels())))); |
| 99 } |
| 100 |
| 101 // Schedule redraw of the damage region. |
| 102 layer()->SchedulePaint(pending_damage_); |
| 103 pending_damage_ = gfx::Rect(); |
| 104 |
| 105 ui::Compositor* compositor = layer()->GetCompositor(); |
| 106 if (compositor && !pending_frame_callbacks_.empty()) { |
| 107 // Start observing the compositor for frame callbacks. |
| 108 if (!compositor_) { |
| 109 compositor->AddObserver(this); |
| 110 compositor_ = compositor; |
| 111 } |
| 112 |
| 113 // Move pending frame callbacks to the end of |frame_callbacks_|. |
| 114 frame_callbacks_.splice(frame_callbacks_.end(), pending_frame_callbacks_); |
| 115 } |
| 116 } |
| 117 |
| 118 void Surface::SetSurfaceDelegate(SurfaceDelegate* delegate) { |
| 119 DCHECK(!delegate_ || !delegate); |
| 120 delegate_ = delegate; |
| 121 } |
| 122 |
| 123 scoped_refptr<base::trace_event::TracedValue> Surface::AsTracedValue() const { |
| 124 scoped_refptr<base::trace_event::TracedValue> value = |
| 125 new base::trace_event::TracedValue; |
| 126 value->SetString("name", layer()->name()); |
| 127 return value; |
| 128 } |
| 129 |
| 130 //////////////////////////////////////////////////////////////////////////////// |
| 131 // views::Views overrides: |
| 132 |
| 133 gfx::Size Surface::GetPreferredSize() const { |
| 134 return pending_buffer_ ? pending_buffer_->GetSize() : layer()->size(); |
| 135 } |
| 136 |
| 137 //////////////////////////////////////////////////////////////////////////////// |
| 138 // ui::CompositorObserver overrides: |
| 139 |
| 140 void Surface::OnCompositingDidCommit(ui::Compositor* compositor) { |
| 141 // Move frame callbacks to the end of |active_frame_callbacks_|. |
| 142 active_frame_callbacks_.splice(active_frame_callbacks_.end(), |
| 143 frame_callbacks_); |
| 144 } |
| 145 |
| 146 void Surface::OnCompositingStarted(ui::Compositor* compositor, |
| 147 base::TimeTicks start_time) { |
| 148 // Run all frame callbacks associated with the compositor's active tree. |
| 149 while (!active_frame_callbacks_.empty()) { |
| 150 active_frame_callbacks_.front().Run(start_time); |
| 151 active_frame_callbacks_.pop_front(); |
| 152 } |
| 153 } |
| 154 |
| 155 void Surface::OnCompositingShuttingDown(ui::Compositor* compositor) { |
| 156 compositor->RemoveObserver(this); |
| 157 compositor_ = nullptr; |
| 158 } |
| 159 |
| 160 } // namespace exo |
OLD | NEW |