[email protected] | c1ae8294 | 2014-01-17 23:17:04 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "cc/layers/surface_layer.h" |
| 6 | |
jbauman | dbccae1ab | 2014-11-06 23:26:44 | [diff] [blame] | 7 | #include "cc/base/swap_promise.h" |
[email protected] | c1ae8294 | 2014-01-17 23:17:04 | [diff] [blame] | 8 | #include "cc/layers/surface_layer_impl.h" |
jbauman | dbccae1ab | 2014-11-06 23:26:44 | [diff] [blame] | 9 | #include "cc/trees/layer_tree_host.h" |
[email protected] | c1ae8294 | 2014-01-17 23:17:04 | [diff] [blame] | 10 | |
| 11 | namespace cc { |
| 12 | |
jbauman | dbccae1ab | 2014-11-06 23:26:44 | [diff] [blame] | 13 | class SatisfySwapPromise : public SwapPromise { |
| 14 | public: |
| 15 | SatisfySwapPromise(SurfaceSequence sequence, |
| 16 | const SurfaceLayer::SatisfyCallback& satisfy_callback) |
| 17 | : sequence_(sequence), satisfy_callback_(satisfy_callback) {} |
| 18 | |
| 19 | ~SatisfySwapPromise() override {} |
| 20 | |
| 21 | private: |
| 22 | void DidSwap(CompositorFrameMetadata* metadata) override { |
| 23 | metadata->satisfies_sequences.push_back(sequence_.sequence); |
| 24 | } |
| 25 | |
| 26 | void DidNotSwap(DidNotSwapReason reason) override { |
| 27 | satisfy_callback_.Run(sequence_); |
| 28 | } |
| 29 | int64 TraceId() const override { return 0; } |
| 30 | |
| 31 | SurfaceSequence sequence_; |
| 32 | SurfaceLayer::SatisfyCallback satisfy_callback_; |
| 33 | |
| 34 | DISALLOW_COPY_AND_ASSIGN(SatisfySwapPromise); |
| 35 | }; |
| 36 | |
| 37 | scoped_refptr<SurfaceLayer> SurfaceLayer::Create( |
| 38 | const SatisfyCallback& satisfy_callback, |
| 39 | const RequireCallback& require_callback) { |
| 40 | return make_scoped_refptr( |
| 41 | new SurfaceLayer(satisfy_callback, require_callback)); |
[email protected] | c1ae8294 | 2014-01-17 23:17:04 | [diff] [blame] | 42 | } |
| 43 | |
jbauman | dbccae1ab | 2014-11-06 23:26:44 | [diff] [blame] | 44 | SurfaceLayer::SurfaceLayer(const SatisfyCallback& satisfy_callback, |
| 45 | const RequireCallback& require_callback) |
| 46 | : Layer(), |
| 47 | satisfy_callback_(satisfy_callback), |
| 48 | require_callback_(require_callback) { |
[email protected] | 16a6af15 | 2014-06-13 04:20:16 | [diff] [blame] | 49 | } |
[email protected] | c1ae8294 | 2014-01-17 23:17:04 | [diff] [blame] | 50 | |
jbauman | dbccae1ab | 2014-11-06 23:26:44 | [diff] [blame] | 51 | SurfaceLayer::~SurfaceLayer() { |
| 52 | DCHECK(!layer_tree_host()); |
| 53 | DCHECK(destroy_sequence_.is_null()); |
| 54 | } |
[email protected] | c1ae8294 | 2014-01-17 23:17:04 | [diff] [blame] | 55 | |
jbauman | 5a8f0ab | 2014-11-07 01:41:16 | [diff] [blame^] | 56 | void SurfaceLayer::SetSurfaceId(SurfaceId surface_id, const gfx::Size& size) { |
jbauman | dbccae1ab | 2014-11-06 23:26:44 | [diff] [blame] | 57 | SatisfyDestroySequence(); |
[email protected] | c1ae8294 | 2014-01-17 23:17:04 | [diff] [blame] | 58 | surface_id_ = surface_id; |
jbauman | 5a8f0ab | 2014-11-07 01:41:16 | [diff] [blame^] | 59 | surface_size_ = size; |
jbauman | dbccae1ab | 2014-11-06 23:26:44 | [diff] [blame] | 60 | CreateNewDestroySequence(); |
| 61 | |
[email protected] | ad63b2f | 2014-08-11 17:39:54 | [diff] [blame] | 62 | UpdateDrawsContent(HasDrawableContent()); |
[email protected] | c1ae8294 | 2014-01-17 23:17:04 | [diff] [blame] | 63 | SetNeedsPushProperties(); |
| 64 | } |
| 65 | |
| 66 | scoped_ptr<LayerImpl> SurfaceLayer::CreateLayerImpl(LayerTreeImpl* tree_impl) { |
danakj | f446a07 | 2014-09-27 21:55:48 | [diff] [blame] | 67 | return SurfaceLayerImpl::Create(tree_impl, id()); |
[email protected] | c1ae8294 | 2014-01-17 23:17:04 | [diff] [blame] | 68 | } |
| 69 | |
[email protected] | ad63b2f | 2014-08-11 17:39:54 | [diff] [blame] | 70 | bool SurfaceLayer::HasDrawableContent() const { |
| 71 | return !surface_id_.is_null() && Layer::HasDrawableContent(); |
[email protected] | c1ae8294 | 2014-01-17 23:17:04 | [diff] [blame] | 72 | } |
| 73 | |
jbauman | dbccae1ab | 2014-11-06 23:26:44 | [diff] [blame] | 74 | void SurfaceLayer::SetLayerTreeHost(LayerTreeHost* host) { |
| 75 | if (layer_tree_host() == host) { |
| 76 | Layer::SetLayerTreeHost(host); |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | SatisfyDestroySequence(); |
| 81 | Layer::SetLayerTreeHost(host); |
| 82 | CreateNewDestroySequence(); |
| 83 | } |
| 84 | |
[email protected] | c1ae8294 | 2014-01-17 23:17:04 | [diff] [blame] | 85 | void SurfaceLayer::PushPropertiesTo(LayerImpl* layer) { |
| 86 | Layer::PushPropertiesTo(layer); |
| 87 | SurfaceLayerImpl* layer_impl = static_cast<SurfaceLayerImpl*>(layer); |
| 88 | |
| 89 | layer_impl->SetSurfaceId(surface_id_); |
| 90 | } |
| 91 | |
jbauman | 5a8f0ab | 2014-11-07 01:41:16 | [diff] [blame^] | 92 | void SurfaceLayer::CalculateContentsScale(float ideal_contents_scale, |
| 93 | float* contents_scale_x, |
| 94 | float* contents_scale_y, |
| 95 | gfx::Size* content_bounds) { |
| 96 | *content_bounds = surface_size_; |
| 97 | *contents_scale_x = |
| 98 | bounds().IsEmpty() ? 1.f : static_cast<float>(content_bounds->width()) / |
| 99 | bounds().width(); |
| 100 | *contents_scale_y = |
| 101 | bounds().IsEmpty() ? 1.f : static_cast<float>(content_bounds->height()) / |
| 102 | bounds().height(); |
| 103 | } |
| 104 | |
jbauman | dbccae1ab | 2014-11-06 23:26:44 | [diff] [blame] | 105 | void SurfaceLayer::CreateNewDestroySequence() { |
| 106 | DCHECK(destroy_sequence_.is_null()); |
| 107 | if (layer_tree_host()) { |
| 108 | destroy_sequence_ = layer_tree_host()->CreateSurfaceSequence(); |
| 109 | require_callback_.Run(surface_id_, destroy_sequence_); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void SurfaceLayer::SatisfyDestroySequence() { |
| 114 | if (!layer_tree_host()) |
| 115 | return; |
| 116 | DCHECK(!destroy_sequence_.is_null()); |
| 117 | scoped_ptr<SatisfySwapPromise> satisfy( |
| 118 | new SatisfySwapPromise(destroy_sequence_, satisfy_callback_)); |
| 119 | layer_tree_host()->QueueSwapPromise(satisfy.Pass()); |
| 120 | destroy_sequence_ = SurfaceSequence(); |
| 121 | } |
| 122 | |
[email protected] | c1ae8294 | 2014-01-17 23:17:04 | [diff] [blame] | 123 | } // namespace cc |