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 
5 #ifndef CC_TREES_DEBUG_RECT_HISTORY_H_
6 #define CC_TREES_DEBUG_RECT_HISTORY_H_
7 
8 #include <memory>
9 #include <vector>
10 
11 #include "cc/input/touch_action.h"
12 #include "cc/layers/layer_collections.h"
13 #include "ui/gfx/geometry/rect.h"
14 
15 namespace cc {
16 
17 class LayerImpl;
18 class LayerTreeDebugState;
19 class LayerTreeImpl;
20 class HeadsUpDisplayLayerImpl;
21 
22 // There are various types of debug rects:
23 //
24 // - Paint rects (update rects): regions of a layer that needed to be
25 // re-uploaded to the texture resource; in most cases implying that they had to
26 // be repainted, too.
27 //
28 // - Property-changed rects: enclosing bounds of layers that cause changes to
29 // the screen even if the layer did not change internally. (For example, if the
30 // layer's opacity or position changes.)
31 //
32 // - Surface damage rects: the aggregate damage on a target surface that is
33 // caused by all layers and surfaces that contribute to it. This includes (1)
34 // paint rects, (2) property- changed rects, and (3) newly exposed areas.
35 //
36 // - Screen space rects: this is the region the contents occupy in screen space.
37 //
38 // - Layout shift rects: regions of an animation frame that were shifted while
39 // the page is loading content.
40 enum DebugRectType {
41   PAINT_RECT_TYPE,
42   PROPERTY_CHANGED_RECT_TYPE,
43   SURFACE_DAMAGE_RECT_TYPE,
44   SCREEN_SPACE_RECT_TYPE,
45   TOUCH_EVENT_HANDLER_RECT_TYPE,
46   WHEEL_EVENT_HANDLER_RECT_TYPE,
47   SCROLL_EVENT_HANDLER_RECT_TYPE,
48   NON_FAST_SCROLLABLE_RECT_TYPE,
49   MAIN_THREAD_SCROLLING_REASON_RECT_TYPE,
50   ANIMATION_BOUNDS_RECT_TYPE,
51   LAYOUT_SHIFT_RECT_TYPE,
52 };
53 
54 struct DebugRect {
55   DebugRect(DebugRectType new_type,
56             const gfx::Rect& new_rect,
57             TouchAction new_touch_action = TouchAction::kNone,
58             uint32_t main_thread_scrolling_reasons = 0)
typeDebugRect59       : type(new_type),
60         rect(new_rect),
61         touch_action(new_touch_action),
62         main_thread_scrolling_reasons(main_thread_scrolling_reasons) {
63     if (type != TOUCH_EVENT_HANDLER_RECT_TYPE)
64       DCHECK_EQ(touch_action, TouchAction::kNone);
65     if (type != MAIN_THREAD_SCROLLING_REASON_RECT_TYPE)
66       DCHECK(!main_thread_scrolling_reasons);
67   }
68   DebugRectType type;
69   gfx::Rect rect;
70   // Valid when |type| is |TOUCH_EVENT_HANDLER_RECT_TYPE|, otherwise default to
71   // |TouchAction::kNone|.
72   TouchAction touch_action;
73   // Valid when |type| is |MAIN_THREAD_SCROLLING_REASON_RECT_TYPE|, otherwise 0.
74   uint32_t main_thread_scrolling_reasons;
75 };
76 
77 // This class maintains a history of rects of various types that can be used
78 // for debugging purposes. The overhead of collecting rects is performed only if
79 // the appropriate LayerTreeSettings are enabled.
80 class DebugRectHistory {
81  public:
82   static std::unique_ptr<DebugRectHistory> Create();
83 
84   DebugRectHistory(const DebugRectHistory&) = delete;
85   ~DebugRectHistory();
86 
87   DebugRectHistory& operator=(const DebugRectHistory&) = delete;
88 
89   // Note: Saving debug rects must happen before layers' change tracking is
90   // reset.
91   void SaveDebugRectsForCurrentFrame(
92       LayerTreeImpl* tree_impl,
93       HeadsUpDisplayLayerImpl* hud_layer,
94       const RenderSurfaceList& render_surface_list,
95       const LayerTreeDebugState& debug_state);
96 
debug_rects()97   const std::vector<DebugRect>& debug_rects() { return debug_rects_; }
98 
99  private:
100   DebugRectHistory();
101 
102   void SaveLayoutShiftRects(HeadsUpDisplayLayerImpl* hud);
103   void SavePaintRects(LayerTreeImpl* tree_impl);
104   void SavePropertyChangedRects(LayerTreeImpl* tree_impl, LayerImpl* hud_layer);
105   void SaveSurfaceDamageRects(const RenderSurfaceList& render_surface_list);
106   void SaveScreenSpaceRects(const RenderSurfaceList& render_surface_list);
107   void SaveTouchEventHandlerRects(LayerTreeImpl* layer);
108   void SaveTouchEventHandlerRectsCallback(LayerImpl* layer);
109   void SaveWheelEventHandlerRects(LayerTreeImpl* tree_impl);
110   void SaveScrollEventHandlerRects(LayerTreeImpl* layer);
111   void SaveScrollEventHandlerRectsCallback(LayerImpl* layer);
112   void SaveNonFastScrollableRects(LayerTreeImpl* layer);
113   void SaveNonFastScrollableRectsCallback(LayerImpl* layer);
114   void SaveMainThreadScrollingReasonRects(LayerTreeImpl*);
115 
116   std::vector<DebugRect> debug_rects_;
117 };
118 
119 }  // namespace cc
120 
121 #endif  // CC_TREES_DEBUG_RECT_HISTORY_H_
122