| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "cc/layers/surface_layer.h" | 5 #include "cc/layers/surface_layer.h" |
| 6 | 6 |
| 7 #include "cc/base/swap_promise.h" |
| 7 #include "cc/layers/surface_layer_impl.h" | 8 #include "cc/layers/surface_layer_impl.h" |
| 9 #include "cc/surfaces/surface_manager.h" |
| 10 #include "cc/trees/layer_tree_host.h" |
| 8 | 11 |
| 9 namespace cc { | 12 namespace cc { |
| 10 | 13 |
| 14 // This only works with threaded compositing disabled, as the swap promise could |
| 15 // be destroyed on either the impl or main thread. |
| 16 class SatisfySwapPromise : public SwapPromise, |
| 17 public base::SupportsWeakPtr<SatisfySwapPromise> { |
| 18 public: |
| 19 SatisfySwapPromise(SurfaceSequence sequence, |
| 20 LayerTreeHost* host, |
| 21 SurfaceHolder::SatisfyCallback satisfy_callback) |
| 22 : sequence_(sequence), host_(host), satisfy_callback_(satisfy_callback) {} |
| 23 |
| 24 SurfaceSequence RemoveSequence() { |
| 25 SurfaceSequence last_sequence = sequence_; |
| 26 sequence_ = SurfaceSequence(); |
| 27 return last_sequence; |
| 28 } |
| 29 |
| 30 LayerTreeHost* host() { return host_; } |
| 31 |
| 32 private: |
| 33 void DidSwap(CompositorFrameMetadata* metadata) override { |
| 34 if (!sequence_.is_null()) |
| 35 metadata->satisfies_sequences.push_back(sequence_.sequence); |
| 36 } |
| 37 |
| 38 void DidNotSwap(DidNotSwapReason reason) override { |
| 39 if (!sequence_.is_null()) |
| 40 satisfy_callback_.Run(sequence_); |
| 41 } |
| 42 int64 TraceId() const override { return 0; } |
| 43 |
| 44 SurfaceSequence sequence_; |
| 45 LayerTreeHost* host_; |
| 46 SurfaceHolder::SatisfyCallback satisfy_callback_; |
| 47 }; |
| 48 |
| 11 scoped_refptr<SurfaceLayer> SurfaceLayer::Create() { | 49 scoped_refptr<SurfaceLayer> SurfaceLayer::Create() { |
| 12 return make_scoped_refptr(new SurfaceLayer); | 50 return make_scoped_refptr(new SurfaceLayer); |
| 13 } | 51 } |
| 14 | 52 |
| 15 SurfaceLayer::SurfaceLayer() : Layer() { | 53 SurfaceLayer::SurfaceLayer() : Layer() { |
| 16 } | 54 } |
| 17 | 55 |
| 18 SurfaceLayer::~SurfaceLayer() {} | 56 SurfaceLayer::~SurfaceLayer() { |
| 57 DCHECK(!layer_tree_host()); |
| 58 DCHECK(destroy_sequence_.is_null()); |
| 59 } |
| 19 | 60 |
| 20 void SurfaceLayer::SetSurfaceId(SurfaceId surface_id) { | 61 void SurfaceLayer::SetSurfaceId(scoped_refptr<SurfaceHolder> surface_holder) { |
| 21 surface_id_ = surface_id; | 62 SatisfyDestroySequence(); |
| 63 surface_holder_ = surface_holder; |
| 64 last_swap_promise_.reset(); |
| 65 CreateNewDestroySequence(); |
| 66 |
| 22 UpdateDrawsContent(HasDrawableContent()); | 67 UpdateDrawsContent(HasDrawableContent()); |
| 23 SetNeedsPushProperties(); | 68 SetNeedsPushProperties(); |
| 24 } | 69 } |
| 25 | 70 |
| 26 scoped_ptr<LayerImpl> SurfaceLayer::CreateLayerImpl(LayerTreeImpl* tree_impl) { | 71 scoped_ptr<LayerImpl> SurfaceLayer::CreateLayerImpl(LayerTreeImpl* tree_impl) { |
| 27 return SurfaceLayerImpl::Create(tree_impl, id()); | 72 return SurfaceLayerImpl::Create(tree_impl, id()); |
| 28 } | 73 } |
| 29 | 74 |
| 30 bool SurfaceLayer::HasDrawableContent() const { | 75 bool SurfaceLayer::HasDrawableContent() const { |
| 31 return !surface_id_.is_null() && Layer::HasDrawableContent(); | 76 return surface_holder_.get() && !surface_holder_->surface_id().is_null() && |
| 77 Layer::HasDrawableContent(); |
| 78 } |
| 79 |
| 80 void SurfaceLayer::SetLayerTreeHost(LayerTreeHost* host) { |
| 81 if (layer_tree_host() == host) { |
| 82 Layer::SetLayerTreeHost(host); |
| 83 return; |
| 84 } |
| 85 |
| 86 SatisfyDestroySequence(); |
| 87 Layer::SetLayerTreeHost(host); |
| 88 CreateNewDestroySequence(); |
| 32 } | 89 } |
| 33 | 90 |
| 34 void SurfaceLayer::PushPropertiesTo(LayerImpl* layer) { | 91 void SurfaceLayer::PushPropertiesTo(LayerImpl* layer) { |
| 35 Layer::PushPropertiesTo(layer); | 92 Layer::PushPropertiesTo(layer); |
| 36 SurfaceLayerImpl* layer_impl = static_cast<SurfaceLayerImpl*>(layer); | 93 SurfaceLayerImpl* layer_impl = static_cast<SurfaceLayerImpl*>(layer); |
| 37 | 94 |
| 38 layer_impl->SetSurfaceId(surface_id_); | 95 layer_impl->SetSurfaceId(surface_holder_.get() ? surface_holder_->surface_id() |
| 96 : SurfaceId()); |
| 97 } |
| 98 |
| 99 void SurfaceLayer::CreateNewDestroySequence() { |
| 100 DCHECK(destroy_sequence_.is_null()); |
| 101 if (layer_tree_host() && surface_holder_.get()) { |
| 102 if (last_swap_promise_ && last_swap_promise_->host() == layer_tree_host()) { |
| 103 // Last swap promise hasn't been submitted yet, so reuse its |
| 104 // SurfaceSequence instead of creating a new one. This allows the layer |
| 105 // to be removed from and added back to a tree in the course of a commit |
| 106 // and avoid requiring a new sequence. |
| 107 destroy_sequence_ = last_swap_promise_->RemoveSequence(); |
| 108 DCHECK(!destroy_sequence_.is_null()); |
| 109 last_swap_promise_.reset(); |
| 110 } else { |
| 111 destroy_sequence_ = layer_tree_host()->CreateSurfaceSequence(); |
| 112 surface_holder_->AddSurfaceSequence(destroy_sequence_); |
| 113 } |
| 114 } |
| 115 } |
| 116 |
| 117 void SurfaceLayer::SatisfyDestroySequence() { |
| 118 if (!layer_tree_host() || !surface_holder_.get()) |
| 119 return; |
| 120 DCHECK(!destroy_sequence_.is_null()); |
| 121 scoped_ptr<SatisfySwapPromise> satisfy( |
| 122 new SatisfySwapPromise(destroy_sequence_, |
| 123 layer_tree_host(), |
| 124 surface_holder_->satisfy_callback())); |
| 125 last_swap_promise_ = satisfy->AsWeakPtr(); |
| 126 layer_tree_host()->QueueSwapPromise(satisfy.Pass()); |
| 127 destroy_sequence_ = SurfaceSequence(); |
| 39 } | 128 } |
| 40 | 129 |
| 41 } // namespace cc | 130 } // namespace cc |
| OLD | NEW |