heads_up_display_layer_impl.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. #ifndef CC_LAYERS_HEADS_UP_DISPLAY_LAYER_IMPL_H_
  5. #define CC_LAYERS_HEADS_UP_DISPLAY_LAYER_IMPL_H_
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "base/memory/ptr_util.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/time/time.h"
  12. #include "cc/cc_export.h"
  13. #include "cc/layers/layer_impl.h"
  14. #include "cc/metrics/web_vital_metrics.h"
  15. #include "cc/resources/memory_history.h"
  16. #include "cc/resources/resource_pool.h"
  17. #include "cc/trees/debug_rect_history.h"
  18. #include "cc/trees/layer_tree_impl.h"
  19. #include "third_party/skia/include/core/SkRefCnt.h"
  20. class SkTypeface;
  21. struct SkRect;
  22. namespace viz {
  23. class ClientResourceProvider;
  24. }
  25. namespace cc {
  26. class DroppedFrameCounter;
  27. class LayerTreeFrameSink;
  28. class PaintCanvas;
  29. class PaintFlags;
  30. enum class TextAlign { kLeft, kCenter, kRight };
  31. class CC_EXPORT HeadsUpDisplayLayerImpl : public LayerImpl {
  32. public:
  33. static std::unique_ptr<HeadsUpDisplayLayerImpl> Create(
  34. LayerTreeImpl* tree_impl,
  35. int id) {
  36. return base::WrapUnique(new HeadsUpDisplayLayerImpl(tree_impl, id));
  37. }
  38. HeadsUpDisplayLayerImpl(const HeadsUpDisplayLayerImpl&) = delete;
  39. ~HeadsUpDisplayLayerImpl() override;
  40. HeadsUpDisplayLayerImpl& operator=(const HeadsUpDisplayLayerImpl&) = delete;
  41. std::unique_ptr<LayerImpl> CreateLayerImpl(
  42. LayerTreeImpl* tree_impl) const override;
  43. bool WillDraw(DrawMode draw_mode,
  44. viz::ClientResourceProvider* resource_provider) override;
  45. void AppendQuads(viz::CompositorRenderPass* render_pass,
  46. AppendQuadsData* append_quads_data) override;
  47. void UpdateHudTexture(DrawMode draw_mode,
  48. LayerTreeFrameSink* frame_sink,
  49. viz::ClientResourceProvider* resource_provider,
  50. bool gpu_raster,
  51. const viz::CompositorRenderPassList& list);
  52. void ReleaseResources() override;
  53. gfx::Rect GetEnclosingVisibleRectInTargetSpace() const override;
  54. bool IsAnimatingHUDContents() const {
  55. return paint_rects_fade_step_ > 0 || layout_shift_rects_fade_step_ > 0;
  56. }
  57. void SetHUDTypeface(sk_sp<SkTypeface> typeface);
  58. void SetLayoutShiftRects(const std::vector<gfx::Rect>& rects);
  59. void ClearLayoutShiftRects();
  60. const std::vector<gfx::Rect>& LayoutShiftRects() const;
  61. void SetWebVitalMetrics(std::unique_ptr<WebVitalMetrics> web_vital_metrics);
  62. // This evicts hud quad appended during render pass preparation.
  63. void EvictHudQuad(const viz::CompositorRenderPassList& list);
  64. // LayerImpl overrides.
  65. void PushPropertiesTo(LayerImpl* layer) override;
  66. private:
  67. HeadsUpDisplayLayerImpl(LayerTreeImpl* tree_impl, int id);
  68. const char* LayerTypeAsString() const override;
  69. void AsValueInto(base::trace_event::TracedValue* dict) const override;
  70. void UpdateHudContents();
  71. void DrawHudContents(PaintCanvas* canvas);
  72. void DrawText(PaintCanvas* canvas,
  73. const PaintFlags& flags,
  74. const std::string& text,
  75. TextAlign align,
  76. int size,
  77. int x,
  78. int y) const;
  79. void DrawText(PaintCanvas* canvas,
  80. const PaintFlags& flags,
  81. const std::string& text,
  82. TextAlign align,
  83. int size,
  84. const SkPoint& pos) const;
  85. void DrawGraphBackground(PaintCanvas* canvas,
  86. PaintFlags* flags,
  87. const SkRect& bounds) const;
  88. void DrawGraphLines(PaintCanvas* canvas,
  89. PaintFlags* flags,
  90. const SkRect& bounds) const;
  91. // Draw a separator line at top of bounds.
  92. void DrawSeparatorLine(PaintCanvas* canvas,
  93. PaintFlags* flags,
  94. const SkRect& bounds) const;
  95. SkRect DrawFrameThroughputDisplay(
  96. PaintCanvas* canvas,
  97. const DroppedFrameCounter* dropped_frame_counter,
  98. int right,
  99. int top) const;
  100. SkRect DrawMemoryDisplay(PaintCanvas* canvas,
  101. int top,
  102. int right,
  103. int width) const;
  104. SkRect DrawGpuRasterizationStatus(PaintCanvas* canvas,
  105. int right,
  106. int top,
  107. int width) const;
  108. void DrawDebugRect(PaintCanvas* canvas,
  109. PaintFlags* flags,
  110. const DebugRect& rect,
  111. SkColor4f stroke_color,
  112. SkColor4f fill_color,
  113. float stroke_width,
  114. const std::string& label_text) const;
  115. void DrawDebugRects(PaintCanvas* canvas,
  116. DebugRectHistory* debug_rect_history);
  117. // This function draws a single web vital metric. If the metrics doesn't have
  118. // a valid value, the value is set to -1. This function returns the height
  119. // of the current draw so it can be used to calculate the top of the next
  120. // draw.
  121. int DrawSingleMetric(PaintCanvas* canvas,
  122. int left,
  123. int right,
  124. int top,
  125. std::string name,
  126. const WebVitalMetrics::MetricsInfo& info,
  127. bool has_value,
  128. double value) const;
  129. SkRect DrawWebVitalMetrics(PaintCanvas* canvas,
  130. int left,
  131. int top,
  132. int width) const;
  133. // This function draws a single smoothness related metric.
  134. int DrawSinglePercentageMetric(PaintCanvas* canvas,
  135. int left,
  136. int right,
  137. int top,
  138. std::string name,
  139. double value) const;
  140. SkRect DrawSmoothnessMetrics(PaintCanvas* canvas,
  141. int left,
  142. int top,
  143. int width) const;
  144. int bounds_width_in_dips() const {
  145. // bounds() is specified in layout coordinates, which is painted dsf away
  146. // from DIPs.
  147. return bounds().width() / layer_tree_impl()->painted_device_scale_factor();
  148. }
  149. ResourcePool::InUsePoolResource in_flight_resource_;
  150. std::unique_ptr<ResourcePool> pool_;
  151. raw_ptr<viz::DrawQuad> current_quad_ = nullptr;
  152. // Used for software raster when it will be uploaded to a texture.
  153. sk_sp<SkSurface> staging_surface_;
  154. sk_sp<SkTypeface> typeface_;
  155. std::vector<gfx::Rect> layout_shift_rects_;
  156. float internal_contents_scale_ = 1.0f;
  157. gfx::Size internal_content_bounds_;
  158. uint32_t throughput_value_ = 0.0f;
  159. // Obtained from the current BeginFrameArgs.
  160. absl::optional<base::TimeDelta> frame_interval_;
  161. MemoryHistory::Entry memory_entry_;
  162. int paint_rects_fade_step_ = 0;
  163. int layout_shift_rects_fade_step_ = 0;
  164. std::vector<DebugRect> paint_rects_;
  165. std::vector<DebugRect> layout_shift_debug_rects_;
  166. std::unique_ptr<WebVitalMetrics> web_vital_metrics_;
  167. base::TimeTicks time_of_last_graph_update_;
  168. };
  169. } // namespace cc
  170. #endif // CC_LAYERS_HEADS_UP_DISPLAY_LAYER_IMPL_H_