surface_layer.cc 6.4 KB

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