| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
| 11 #include "base/trace_event/trace_event.h" | 11 #include "base/trace_event/trace_event.h" |
| 12 #include "cc/layers/surface_layer_impl.h" | 12 #include "cc/layers/surface_layer_impl.h" |
| 13 #include "cc/output/swap_promise.h" | 13 #include "cc/output/swap_promise.h" |
| 14 #include "cc/surfaces/surface_sequence_generator.h" | 14 #include "cc/surfaces/surface_sequence_generator.h" |
| 15 #include "cc/trees/layer_tree_host.h" | 15 #include "cc/trees/layer_tree_host.h" |
| 16 #include "cc/trees/swap_promise_manager.h" | 16 #include "cc/trees/swap_promise_manager.h" |
| 17 | 17 |
| 18 namespace cc { | 18 namespace cc { |
| 19 | 19 |
| 20 class SatisfySwapPromise : public SwapPromise { | 20 class SatisfySwapPromise : public SwapPromise { |
| 21 public: | 21 public: |
| 22 SatisfySwapPromise( | 22 SatisfySwapPromise( |
| 23 const SurfaceSequence& sequence, | 23 std::unique_ptr<SurfaceReferenceBase> surface_ref, |
| 24 const SurfaceLayer::SatisfyCallback& satisfy_callback, | |
| 25 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner) | 24 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner) |
| 26 : sequence_(sequence), | 25 : surface_ref_(std::move(surface_ref)), |
| 27 satisfy_callback_(satisfy_callback), | |
| 28 main_task_runner_(std::move(main_task_runner)) {} | 26 main_task_runner_(std::move(main_task_runner)) {} |
| 29 | 27 |
| 30 ~SatisfySwapPromise() override {} | 28 ~SatisfySwapPromise() override {} |
| 31 | 29 |
| 32 private: | 30 private: |
| 33 void DidActivate() override {} | 31 void DidActivate() override {} |
| 32 |
| 34 void WillSwap(CompositorFrameMetadata* metadata) override {} | 33 void WillSwap(CompositorFrameMetadata* metadata) override {} |
| 34 |
| 35 void DidSwap() override { | 35 void DidSwap() override { |
| 36 // DidSwap could run on compositor thread but satisfy callback must | 36 main_task_runner_->DeleteSoon(FROM_HERE, surface_ref_.release()); |
| 37 // run on the main thread. | |
| 38 main_task_runner_->PostTask(FROM_HERE, | |
| 39 base::Bind(satisfy_callback_, sequence_)); | |
| 40 } | 37 } |
| 41 | 38 |
| 42 DidNotSwapAction DidNotSwap(DidNotSwapReason reason) override { | 39 DidNotSwapAction DidNotSwap(DidNotSwapReason reason) override { |
| 43 main_task_runner_->PostTask(FROM_HERE, | 40 main_task_runner_->DeleteSoon(FROM_HERE, surface_ref_.release()); |
| 44 base::Bind(satisfy_callback_, sequence_)); | |
| 45 return DidNotSwapAction::BREAK_PROMISE; | 41 return DidNotSwapAction::BREAK_PROMISE; |
| 46 } | 42 } |
| 43 |
| 47 int64_t TraceId() const override { return 0; } | 44 int64_t TraceId() const override { return 0; } |
| 48 | 45 |
| 49 SurfaceSequence sequence_; | 46 std::unique_ptr<SurfaceReferenceBase> surface_ref_; |
| 50 SurfaceLayer::SatisfyCallback satisfy_callback_; | |
| 51 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; | 47 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; |
| 52 | 48 |
| 53 DISALLOW_COPY_AND_ASSIGN(SatisfySwapPromise); | 49 DISALLOW_COPY_AND_ASSIGN(SatisfySwapPromise); |
| 54 }; | 50 }; |
| 55 | 51 |
| 56 scoped_refptr<SurfaceLayer> SurfaceLayer::Create( | 52 scoped_refptr<SurfaceLayer> SurfaceLayer::Create( |
| 57 const SatisfyCallback& satisfy_callback, | 53 scoped_refptr<SurfaceReferenceFactory> ref_factory) { |
| 58 const RequireCallback& require_callback) { | 54 return make_scoped_refptr(new SurfaceLayer(std::move(ref_factory))); |
| 59 return make_scoped_refptr( | |
| 60 new SurfaceLayer(satisfy_callback, require_callback)); | |
| 61 } | 55 } |
| 62 | 56 |
| 63 SurfaceLayer::SurfaceLayer(const SatisfyCallback& satisfy_callback, | 57 SurfaceLayer::SurfaceLayer(scoped_refptr<SurfaceReferenceFactory> ref_factory) |
| 64 const RequireCallback& require_callback) | 58 : ref_factory_(std::move(ref_factory)) {} |
| 65 : satisfy_callback_(satisfy_callback), | |
| 66 require_callback_(require_callback) {} | |
| 67 | 59 |
| 68 SurfaceLayer::~SurfaceLayer() { | 60 SurfaceLayer::~SurfaceLayer() { |
| 69 DCHECK(!layer_tree_host()); | 61 DCHECK(!layer_tree_host()); |
| 70 DCHECK(!destroy_sequence_.is_valid()); | |
| 71 } | 62 } |
| 72 | 63 |
| 73 void SurfaceLayer::SetSurfaceId(const SurfaceId& surface_id, | 64 void SurfaceLayer::SetSurfaceInfo(const SurfaceInfo& surface_info, |
| 74 float scale, | 65 bool stretch_content_to_fill_bounds) { |
| 75 const gfx::Size& size, | 66 RemoveCurrentReference(); |
| 76 bool stretch_content_to_fill_bounds) { | 67 surface_info_ = surface_info; |
| 77 SatisfyDestroySequence(); | 68 if (layer_tree_host()) { |
| 78 surface_id_ = surface_id; | 69 current_ref_ = |
| 79 surface_size_ = size; | 70 ref_factory_->CreateReference(layer_tree_host(), surface_info_.id()); |
| 80 surface_scale_ = scale; | 71 } |
| 81 stretch_content_to_fill_bounds_ = stretch_content_to_fill_bounds; | 72 stretch_content_to_fill_bounds_ = stretch_content_to_fill_bounds; |
| 82 CreateNewDestroySequence(); | |
| 83 | |
| 84 UpdateDrawsContent(HasDrawableContent()); | 73 UpdateDrawsContent(HasDrawableContent()); |
| 85 SetNeedsPushProperties(); | 74 SetNeedsPushProperties(); |
| 86 } | 75 } |
| 87 | 76 |
| 88 std::unique_ptr<LayerImpl> SurfaceLayer::CreateLayerImpl( | 77 std::unique_ptr<LayerImpl> SurfaceLayer::CreateLayerImpl( |
| 89 LayerTreeImpl* tree_impl) { | 78 LayerTreeImpl* tree_impl) { |
| 90 return SurfaceLayerImpl::Create(tree_impl, id()); | 79 return SurfaceLayerImpl::Create(tree_impl, id()); |
| 91 } | 80 } |
| 92 | 81 |
| 93 bool SurfaceLayer::HasDrawableContent() const { | 82 bool SurfaceLayer::HasDrawableContent() const { |
| 94 return surface_id_.is_valid() && Layer::HasDrawableContent(); | 83 return surface_info_.id().is_valid() && Layer::HasDrawableContent(); |
| 95 } | 84 } |
| 96 | 85 |
| 97 void SurfaceLayer::SetLayerTreeHost(LayerTreeHost* host) { | 86 void SurfaceLayer::SetLayerTreeHost(LayerTreeHost* host) { |
| 98 if (layer_tree_host() == host) { | 87 if (layer_tree_host() == host) { |
| 99 Layer::SetLayerTreeHost(host); | 88 Layer::SetLayerTreeHost(host); |
| 100 return; | 89 return; |
| 101 } | 90 } |
| 102 | 91 RemoveCurrentReference(); |
| 103 SatisfyDestroySequence(); | |
| 104 Layer::SetLayerTreeHost(host); | 92 Layer::SetLayerTreeHost(host); |
| 105 CreateNewDestroySequence(); | 93 if (layer_tree_host()) { |
| 94 current_ref_ = |
| 95 ref_factory_->CreateReference(layer_tree_host(), surface_info_.id()); |
| 96 } |
| 106 } | 97 } |
| 107 | 98 |
| 108 void SurfaceLayer::PushPropertiesTo(LayerImpl* layer) { | 99 void SurfaceLayer::PushPropertiesTo(LayerImpl* layer) { |
| 109 Layer::PushPropertiesTo(layer); | 100 Layer::PushPropertiesTo(layer); |
| 110 TRACE_EVENT0("cc", "SurfaceLayer::PushPropertiesTo"); | 101 TRACE_EVENT0("cc", "SurfaceLayer::PushPropertiesTo"); |
| 111 SurfaceLayerImpl* layer_impl = static_cast<SurfaceLayerImpl*>(layer); | 102 SurfaceLayerImpl* layer_impl = static_cast<SurfaceLayerImpl*>(layer); |
| 112 | 103 layer_impl->SetSurfaceInfo(surface_info_); |
| 113 layer_impl->SetSurfaceId(surface_id_); | |
| 114 layer_impl->SetSurfaceSize(surface_size_); | |
| 115 layer_impl->SetSurfaceScale(surface_scale_); | |
| 116 layer_impl->SetStretchContentToFillBounds(stretch_content_to_fill_bounds_); | 104 layer_impl->SetStretchContentToFillBounds(stretch_content_to_fill_bounds_); |
| 117 } | 105 } |
| 118 | 106 |
| 119 void SurfaceLayer::CreateNewDestroySequence() { | 107 void SurfaceLayer::RemoveCurrentReference() { |
| 120 DCHECK(!destroy_sequence_.is_valid()); | 108 if (!current_ref_) |
| 121 if (layer_tree_host()) { | |
| 122 destroy_sequence_ = layer_tree_host() | |
| 123 ->GetSurfaceSequenceGenerator() | |
| 124 ->CreateSurfaceSequence(); | |
| 125 require_callback_.Run(surface_id_, destroy_sequence_); | |
| 126 } | |
| 127 } | |
| 128 | |
| 129 void SurfaceLayer::SatisfyDestroySequence() { | |
| 130 if (!layer_tree_host()) | |
| 131 return; | 109 return; |
| 132 DCHECK(destroy_sequence_.is_valid()); | 110 auto swap_promise = base::MakeUnique<SatisfySwapPromise>( |
| 133 auto satisfy = | 111 std::move(current_ref_), base::ThreadTaskRunnerHandle::Get()); |
| 134 base::MakeUnique<SatisfySwapPromise>(destroy_sequence_, satisfy_callback_, | |
| 135 base::ThreadTaskRunnerHandle::Get()); | |
| 136 layer_tree_host()->GetSwapPromiseManager()->QueueSwapPromise( | 112 layer_tree_host()->GetSwapPromiseManager()->QueueSwapPromise( |
| 137 std::move(satisfy)); | 113 std::move(swap_promise)); |
| 138 destroy_sequence_ = SurfaceSequence(); | |
| 139 } | 114 } |
| 140 | 115 |
| 141 } // namespace cc | 116 } // namespace cc |
| OLD | NEW |