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 
7 #include <stdint.h>
8 
9 #include "base/trace_event/trace_event.h"
10 #include "cc/layers/surface_layer_impl.h"
11 #include "cc/trees/layer_tree_host.h"
12 
13 namespace cc {
14 
Create()15 scoped_refptr<SurfaceLayer> SurfaceLayer::Create() {
16   return base::WrapRefCounted(new SurfaceLayer());
17 }
18 
Create(UpdateSubmissionStateCB update_submission_state_callback)19 scoped_refptr<SurfaceLayer> SurfaceLayer::Create(
20     UpdateSubmissionStateCB update_submission_state_callback) {
21   return base::WrapRefCounted(
22       new SurfaceLayer(std::move(update_submission_state_callback)));
23 }
24 
25 SurfaceLayer::SurfaceLayer() = default;
26 
SurfaceLayer(UpdateSubmissionStateCB update_submission_state_callback)27 SurfaceLayer::SurfaceLayer(
28     UpdateSubmissionStateCB update_submission_state_callback)
29     : update_submission_state_callback_(
30           std::move(update_submission_state_callback)) {}
31 
~SurfaceLayer()32 SurfaceLayer::~SurfaceLayer() {
33   DCHECK(!layer_tree_host());
34 }
35 
SetSurfaceId(const viz::SurfaceId & surface_id,const DeadlinePolicy & deadline_policy)36 void SurfaceLayer::SetSurfaceId(const viz::SurfaceId& surface_id,
37                                 const DeadlinePolicy& deadline_policy) {
38   if (surface_range_.end() == surface_id &&
39       deadline_policy.use_existing_deadline()) {
40     return;
41   }
42   if (surface_id.local_surface_id().is_valid()) {
43     TRACE_EVENT_WITH_FLOW2(
44         TRACE_DISABLED_BY_DEFAULT("viz.surface_id_flow"),
45         "LocalSurfaceId.Embed.Flow",
46         TRACE_ID_GLOBAL(surface_id.local_surface_id().embed_trace_id()),
47         TRACE_EVENT_FLAG_FLOW_IN | TRACE_EVENT_FLAG_FLOW_OUT, "step",
48         "SetSurfaceId", "surface_id", surface_id.ToString());
49   }
50 
51   if (layer_tree_host() && surface_range_.IsValid())
52     layer_tree_host()->RemoveSurfaceRange(surface_range_);
53 
54   surface_range_ = viz::SurfaceRange(surface_range_.start(), surface_id);
55 
56   if (layer_tree_host() && surface_range_.IsValid())
57     layer_tree_host()->AddSurfaceRange(surface_range_);
58 
59   // We should never block or set a deadline on an invalid
60   // |surface_range_|.
61   if (!surface_range_.IsValid()) {
62     deadline_in_frames_ = 0u;
63   } else if (!deadline_policy.use_existing_deadline()) {
64     deadline_in_frames_ = deadline_policy.deadline_in_frames();
65   }
66   UpdateDrawsContent(HasDrawableContent());
67   SetNeedsCommit();
68 }
69 
SetOldestAcceptableFallback(const viz::SurfaceId & surface_id)70 void SurfaceLayer::SetOldestAcceptableFallback(
71     const viz::SurfaceId& surface_id) {
72   // The fallback should never move backwards.
73   DCHECK(!surface_range_.start() ||
74          !surface_range_.start()->IsNewerThan(surface_id));
75   if (surface_range_.start() == surface_id)
76     return;
77 
78   if (layer_tree_host() && surface_range_.IsValid())
79     layer_tree_host()->RemoveSurfaceRange(surface_range_);
80 
81   surface_range_ = viz::SurfaceRange(
82       surface_id.is_valid() ? base::Optional<viz::SurfaceId>(surface_id)
83                             : base::nullopt,
84       surface_range_.end());
85 
86   if (layer_tree_host() && surface_range_.IsValid())
87     layer_tree_host()->AddSurfaceRange(surface_range_);
88 
89   SetNeedsCommit();
90 }
91 
SetStretchContentToFillBounds(bool stretch_content_to_fill_bounds)92 void SurfaceLayer::SetStretchContentToFillBounds(
93     bool stretch_content_to_fill_bounds) {
94   if (stretch_content_to_fill_bounds_ == stretch_content_to_fill_bounds)
95     return;
96   stretch_content_to_fill_bounds_ = stretch_content_to_fill_bounds;
97   SetNeedsPushProperties();
98 }
99 
SetSurfaceHitTestable(bool surface_hit_testable)100 void SurfaceLayer::SetSurfaceHitTestable(bool surface_hit_testable) {
101   if (surface_hit_testable_ == surface_hit_testable)
102     return;
103   surface_hit_testable_ = surface_hit_testable;
104 }
105 
SetHasPointerEventsNone(bool has_pointer_events_none)106 void SurfaceLayer::SetHasPointerEventsNone(bool has_pointer_events_none) {
107   if (has_pointer_events_none_ == has_pointer_events_none)
108     return;
109   has_pointer_events_none_ = has_pointer_events_none;
110   SetNeedsPushProperties();
111   // Change of pointer-events property triggers an update of viz hit test data,
112   // we need to commit in order to submit the new data with compositor frame.
113   SetNeedsCommit();
114 }
115 
SetIsReflection(bool is_reflection)116 void SurfaceLayer::SetIsReflection(bool is_reflection) {
117   is_reflection_ = true;
118 }
119 
SetMayContainVideo(bool may_contain_video)120 void SurfaceLayer::SetMayContainVideo(bool may_contain_video) {
121   may_contain_video_ = may_contain_video;
122   SetNeedsCommit();
123 }
124 
CreateLayerImpl(LayerTreeImpl * tree_impl)125 std::unique_ptr<LayerImpl> SurfaceLayer::CreateLayerImpl(
126     LayerTreeImpl* tree_impl) {
127   auto layer_impl = SurfaceLayerImpl::Create(tree_impl, id(),
128                                              update_submission_state_callback_);
129   return layer_impl;
130 }
131 
HasDrawableContent() const132 bool SurfaceLayer::HasDrawableContent() const {
133   return surface_range_.IsValid() && Layer::HasDrawableContent();
134 }
135 
SetLayerTreeHost(LayerTreeHost * host)136 void SurfaceLayer::SetLayerTreeHost(LayerTreeHost* host) {
137   if (layer_tree_host() == host) {
138     return;
139   }
140   if (layer_tree_host() && surface_range_.IsValid())
141     layer_tree_host()->RemoveSurfaceRange(surface_range_);
142 
143   Layer::SetLayerTreeHost(host);
144 
145   if (layer_tree_host() && surface_range_.IsValid())
146     layer_tree_host()->AddSurfaceRange(surface_range_);
147 }
148 
PushPropertiesTo(LayerImpl * layer)149 void SurfaceLayer::PushPropertiesTo(LayerImpl* layer) {
150   Layer::PushPropertiesTo(layer);
151   TRACE_EVENT0("cc", "SurfaceLayer::PushPropertiesTo");
152   SurfaceLayerImpl* layer_impl = static_cast<SurfaceLayerImpl*>(layer);
153   layer_impl->SetRange(surface_range_, std::move(deadline_in_frames_));
154   // Unless the client explicitly calls SetSurfaceId again after this
155   // commit, don't block on |surface_range_| again.
156   deadline_in_frames_ = 0u;
157   layer_impl->SetIsReflection(is_reflection_);
158   layer_impl->SetStretchContentToFillBounds(stretch_content_to_fill_bounds_);
159   layer_impl->SetSurfaceHitTestable(surface_hit_testable_);
160   layer_impl->SetHasPointerEventsNone(has_pointer_events_none_);
161   layer_impl->set_may_contain_video(may_contain_video_);
162 }
163 
164 }  // namespace cc
165