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.
5 #include "cc/layers/surface_layer.h"
7 #include "cc/layers/surface_layer_impl.h"
8 #include "cc/output/swap_promise.h"
9 #include "cc/trees/layer_tree_host.h"
13 class SatisfySwapPromise
: public SwapPromise
{
15 SatisfySwapPromise(SurfaceSequence sequence
,
16 const SurfaceLayer::SatisfyCallback
& satisfy_callback
)
17 : sequence_(sequence
), satisfy_callback_(satisfy_callback
) {}
19 ~SatisfySwapPromise() override
{}
22 void DidActivate() override
{}
23 void DidSwap(CompositorFrameMetadata
* metadata
) override
{
24 metadata
->satisfies_sequences
.push_back(sequence_
.sequence
);
27 void DidNotSwap(DidNotSwapReason reason
) override
{
28 satisfy_callback_
.Run(sequence_
);
30 int64
TraceId() const override
{ return 0; }
32 SurfaceSequence sequence_
;
33 SurfaceLayer::SatisfyCallback satisfy_callback_
;
35 DISALLOW_COPY_AND_ASSIGN(SatisfySwapPromise
);
38 scoped_refptr
<SurfaceLayer
> SurfaceLayer::Create(
39 const SatisfyCallback
& satisfy_callback
,
40 const RequireCallback
& require_callback
) {
41 return make_scoped_refptr(
42 new SurfaceLayer(satisfy_callback
, require_callback
));
45 SurfaceLayer::SurfaceLayer(const SatisfyCallback
& satisfy_callback
,
46 const RequireCallback
& require_callback
)
49 satisfy_callback_(satisfy_callback
),
50 require_callback_(require_callback
) {
53 SurfaceLayer::~SurfaceLayer() {
54 DCHECK(!layer_tree_host());
55 DCHECK(destroy_sequence_
.is_null());
58 void SurfaceLayer::SetSurfaceId(SurfaceId surface_id
,
60 const gfx::Size
& size
) {
61 SatisfyDestroySequence();
62 surface_id_
= surface_id
;
64 surface_scale_
= scale
;
65 CreateNewDestroySequence();
67 UpdateDrawsContent(HasDrawableContent());
68 SetNeedsPushProperties();
71 scoped_ptr
<LayerImpl
> SurfaceLayer::CreateLayerImpl(LayerTreeImpl
* tree_impl
) {
72 return SurfaceLayerImpl::Create(tree_impl
, id());
75 bool SurfaceLayer::HasDrawableContent() const {
76 return !surface_id_
.is_null() && Layer::HasDrawableContent();
79 void SurfaceLayer::SetLayerTreeHost(LayerTreeHost
* host
) {
80 if (layer_tree_host() == host
) {
81 Layer::SetLayerTreeHost(host
);
85 SatisfyDestroySequence();
86 Layer::SetLayerTreeHost(host
);
87 CreateNewDestroySequence();
90 void SurfaceLayer::PushPropertiesTo(LayerImpl
* layer
) {
91 Layer::PushPropertiesTo(layer
);
92 SurfaceLayerImpl
* layer_impl
= static_cast<SurfaceLayerImpl
*>(layer
);
94 layer_impl
->SetSurfaceId(surface_id_
);
97 void SurfaceLayer::CalculateContentsScale(float ideal_contents_scale
,
98 float* contents_scale_x
,
99 float* contents_scale_y
,
100 gfx::Size
* content_bounds
) {
101 *content_bounds
= surface_size_
;
102 *contents_scale_x
= surface_scale_
;
103 *contents_scale_y
= surface_scale_
;
106 void SurfaceLayer::CreateNewDestroySequence() {
107 DCHECK(destroy_sequence_
.is_null());
108 if (layer_tree_host()) {
109 destroy_sequence_
= layer_tree_host()->CreateSurfaceSequence();
110 require_callback_
.Run(surface_id_
, destroy_sequence_
);
114 void SurfaceLayer::SatisfyDestroySequence() {
115 if (!layer_tree_host())
117 DCHECK(!destroy_sequence_
.is_null());
118 scoped_ptr
<SatisfySwapPromise
> satisfy(
119 new SatisfySwapPromise(destroy_sequence_
, satisfy_callback_
));
120 layer_tree_host()->QueueSwapPromise(satisfy
.Pass());
121 destroy_sequence_
= SurfaceSequence();