heads_up_display_layer.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2012 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/heads_up_display_layer.h"
  5. #include <algorithm>
  6. #include <utility>
  7. #include <vector>
  8. #include "base/trace_event/trace_event.h"
  9. #include "cc/layers/heads_up_display_layer_impl.h"
  10. #include "cc/trees/layer_tree_host.h"
  11. namespace cc {
  12. scoped_refptr<HeadsUpDisplayLayer> HeadsUpDisplayLayer::Create() {
  13. return base::WrapRefCounted(new HeadsUpDisplayLayer());
  14. }
  15. HeadsUpDisplayLayer::HeadsUpDisplayLayer()
  16. : typeface_(SkTypeface::MakeFromName("Arial", SkFontStyle())) {
  17. if (!typeface_.Read(*this)) {
  18. typeface_.Write(*this) =
  19. SkTypeface::MakeFromName("monospace", SkFontStyle::Bold());
  20. }
  21. DCHECK(typeface_.Read(*this).get());
  22. SetIsDrawable(true);
  23. SetDrawsContent(HasDrawableContent());
  24. }
  25. HeadsUpDisplayLayer::~HeadsUpDisplayLayer() = default;
  26. void HeadsUpDisplayLayer::UpdateLocationAndSize(
  27. const gfx::Size& device_viewport,
  28. float device_scale_factor) {
  29. float multiplier = 1.f / (device_scale_factor *
  30. layer_tree_host()->painted_device_scale_factor());
  31. gfx::Size device_viewport_in_dips =
  32. gfx::ScaleToFlooredSize(device_viewport, multiplier);
  33. gfx::Size bounds_in_dips;
  34. // If the HUD is not displaying full-viewport rects (e.g., it is showing the
  35. // Frame Rendering Stats), use a fixed size.
  36. constexpr int kDefaultHUDSize = 256;
  37. bounds_in_dips.SetSize(kDefaultHUDSize, kDefaultHUDSize);
  38. if (layer_tree_host()->GetDebugState().ShowDebugRects()) {
  39. bounds_in_dips = device_viewport_in_dips;
  40. } else if (layer_tree_host()->GetDebugState().show_web_vital_metrics ||
  41. layer_tree_host()->GetDebugState().show_smoothness_metrics) {
  42. // If the HUD is used to display performance metrics (which is on the right
  43. // hand side_, make sure the bounds has the correct width, with a fixed
  44. // height.
  45. bounds_in_dips.set_width(device_viewport_in_dips.width());
  46. // Increase HUD layer height to make sure all the metrics are showing.
  47. bounds_in_dips.set_height(kDefaultHUDSize * 2);
  48. }
  49. // DIPs are layout coordinates if painted dsf is 1. If it's not 1, then layout
  50. // coordinates are DIPs * painted dsf.
  51. auto bounds_in_layout_space = gfx::ScaleToCeiledSize(
  52. bounds_in_dips, layer_tree_host()->painted_device_scale_factor());
  53. SetBounds(bounds_in_layout_space);
  54. }
  55. bool HeadsUpDisplayLayer::HasDrawableContent() const {
  56. return true;
  57. }
  58. std::unique_ptr<LayerImpl> HeadsUpDisplayLayer::CreateLayerImpl(
  59. LayerTreeImpl* tree_impl) const {
  60. return HeadsUpDisplayLayerImpl::Create(tree_impl, id());
  61. }
  62. const std::vector<gfx::Rect>& HeadsUpDisplayLayer::LayoutShiftRects() const {
  63. return layout_shift_rects_.Read(*this);
  64. }
  65. void HeadsUpDisplayLayer::SetLayoutShiftRects(
  66. const std::vector<gfx::Rect>& rects) {
  67. layout_shift_rects_.Write(*this) = rects;
  68. }
  69. void HeadsUpDisplayLayer::UpdateWebVitalMetrics(
  70. std::unique_ptr<WebVitalMetrics> web_vital_metrics) {
  71. web_vital_metrics_.Write(*this) = std::move(web_vital_metrics);
  72. }
  73. void HeadsUpDisplayLayer::PushPropertiesTo(
  74. LayerImpl* layer,
  75. const CommitState& commit_state,
  76. const ThreadUnsafeCommitState& unsafe_state) {
  77. Layer::PushPropertiesTo(layer, commit_state, unsafe_state);
  78. TRACE_EVENT0("cc", "HeadsUpDisplayLayer::PushPropertiesTo");
  79. HeadsUpDisplayLayerImpl* layer_impl =
  80. static_cast<HeadsUpDisplayLayerImpl*>(layer);
  81. layer_impl->SetHUDTypeface(typeface_.Write(*this));
  82. layer_impl->SetLayoutShiftRects(LayoutShiftRects());
  83. layout_shift_rects_.Write(*this).clear();
  84. auto& metrics = web_vital_metrics_.Write(*this);
  85. if (metrics && metrics->HasValue())
  86. layer_impl->SetWebVitalMetrics(std::move(metrics));
  87. }
  88. } // namespace cc