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.
5 #include "cc/debug/debug_rect_history.h"
7 #include "cc/base/math_util.h"
8 #include "cc/layers/layer_impl.h"
9 #include "cc/layers/layer_iterator.h"
10 #include "cc/layers/layer_utils.h"
11 #include "cc/layers/render_surface_impl.h"
12 #include "cc/trees/damage_tracker.h"
13 #include "cc/trees/layer_tree_host.h"
14 #include "cc/trees/layer_tree_host_common.h"
15 #include "ui/gfx/geometry/rect_conversions.h"
20 scoped_ptr
<DebugRectHistory
> DebugRectHistory::Create() {
21 return make_scoped_ptr(new DebugRectHistory());
24 DebugRectHistory::DebugRectHistory() {}
26 DebugRectHistory::~DebugRectHistory() {}
28 void DebugRectHistory::SaveDebugRectsForCurrentFrame(
29 LayerImpl
* root_layer
,
31 const LayerImplList
& render_surface_layer_list
,
32 const LayerTreeDebugState
& debug_state
) {
33 // For now, clear all rects from previous frames. In the future we may want to
34 // store all debug rects for a history of many frames.
37 if (debug_state
.show_touch_event_handler_rects
)
38 SaveTouchEventHandlerRects(root_layer
);
40 if (debug_state
.show_wheel_event_handler_rects
)
41 SaveWheelEventHandlerRects(root_layer
);
43 if (debug_state
.show_scroll_event_handler_rects
)
44 SaveScrollEventHandlerRects(root_layer
);
46 if (debug_state
.show_non_fast_scrollable_rects
)
47 SaveNonFastScrollableRects(root_layer
);
49 if (debug_state
.show_paint_rects
)
50 SavePaintRects(root_layer
);
52 if (debug_state
.show_property_changed_rects
)
53 SavePropertyChangedRects(render_surface_layer_list
, hud_layer
);
55 if (debug_state
.show_surface_damage_rects
)
56 SaveSurfaceDamageRects(render_surface_layer_list
);
58 if (debug_state
.show_screen_space_rects
)
59 SaveScreenSpaceRects(render_surface_layer_list
);
61 if (debug_state
.show_layer_animation_bounds_rects
)
62 SaveLayerAnimationBoundsRects(render_surface_layer_list
);
65 void DebugRectHistory::SavePaintRects(LayerImpl
* layer
) {
66 // We would like to visualize where any layer's paint rect (update rect) has
67 // changed, regardless of whether this layer is skipped for actual drawing or
68 // not. Therefore we traverse recursively over all layers, not just the render
71 Region invalidation_region
= layer
->GetInvalidationRegion();
72 if (!invalidation_region
.IsEmpty() && layer
->DrawsContent()) {
73 float width_scale
= layer
->content_bounds().width() /
74 static_cast<float>(layer
->bounds().width());
75 float height_scale
= layer
->content_bounds().height() /
76 static_cast<float>(layer
->bounds().height());
78 for (Region::Iterator
it(invalidation_region
); it
.has_rect(); it
.next()) {
79 gfx::Rect update_content_rect
=
80 gfx::ScaleToEnclosingRect(it
.rect(), width_scale
, height_scale
);
81 debug_rects_
.push_back(
82 DebugRect(PAINT_RECT_TYPE
,
83 MathUtil::MapEnclosingClippedRect(
84 layer
->screen_space_transform(), update_content_rect
)));
88 for (unsigned i
= 0; i
< layer
->children().size(); ++i
)
89 SavePaintRects(layer
->children()[i
]);
92 void DebugRectHistory::SavePropertyChangedRects(
93 const LayerImplList
& render_surface_layer_list
,
94 LayerImpl
* hud_layer
) {
95 for (size_t i
= 0; i
< render_surface_layer_list
.size(); ++i
) {
96 size_t surface_index
= render_surface_layer_list
.size() - 1 - i
;
97 LayerImpl
* render_surface_layer
= render_surface_layer_list
[surface_index
];
98 RenderSurfaceImpl
* render_surface
= render_surface_layer
->render_surface();
99 DCHECK(render_surface
);
101 const LayerImplList
& layer_list
= render_surface
->layer_list();
102 for (unsigned layer_index
= 0;
103 layer_index
< layer_list
.size();
105 LayerImpl
* layer
= layer_list
[layer_index
];
107 if (LayerTreeHostCommon::RenderSurfaceContributesToTarget
<LayerImpl
>(
108 layer
, render_surface_layer
->id()))
111 if (layer
== hud_layer
)
114 if (!layer
->LayerPropertyChanged())
117 debug_rects_
.push_back(
118 DebugRect(PROPERTY_CHANGED_RECT_TYPE
,
119 MathUtil::MapEnclosingClippedRect(
120 layer
->screen_space_transform(),
121 gfx::Rect(layer
->content_bounds()))));
126 void DebugRectHistory::SaveSurfaceDamageRects(
127 const LayerImplList
& render_surface_layer_list
) {
128 for (size_t i
= 0; i
< render_surface_layer_list
.size(); ++i
) {
129 size_t surface_index
= render_surface_layer_list
.size() - 1 - i
;
130 LayerImpl
* render_surface_layer
= render_surface_layer_list
[surface_index
];
131 RenderSurfaceImpl
* render_surface
= render_surface_layer
->render_surface();
132 DCHECK(render_surface
);
134 debug_rects_
.push_back(DebugRect(
135 SURFACE_DAMAGE_RECT_TYPE
,
136 MathUtil::MapEnclosingClippedRect(
137 render_surface
->screen_space_transform(),
138 render_surface
->damage_tracker()->current_damage_rect())));
142 void DebugRectHistory::SaveScreenSpaceRects(
143 const LayerImplList
& render_surface_layer_list
) {
144 for (size_t i
= 0; i
< render_surface_layer_list
.size(); ++i
) {
145 size_t surface_index
= render_surface_layer_list
.size() - 1 - i
;
146 LayerImpl
* render_surface_layer
= render_surface_layer_list
[surface_index
];
147 RenderSurfaceImpl
* render_surface
= render_surface_layer
->render_surface();
148 DCHECK(render_surface
);
150 debug_rects_
.push_back(
151 DebugRect(SCREEN_SPACE_RECT_TYPE
,
152 MathUtil::MapEnclosingClippedRect(
153 render_surface
->screen_space_transform(),
154 render_surface
->content_rect())));
156 if (render_surface_layer
->replica_layer()) {
157 debug_rects_
.push_back(
158 DebugRect(REPLICA_SCREEN_SPACE_RECT_TYPE
,
159 MathUtil::MapEnclosingClippedRect(
160 render_surface
->replica_screen_space_transform(),
161 render_surface
->content_rect())));
166 void DebugRectHistory::SaveTouchEventHandlerRects(LayerImpl
* layer
) {
167 LayerTreeHostCommon::CallFunctionForSubtree(layer
, [this](LayerImpl
* layer
) {
168 SaveTouchEventHandlerRectsCallback(layer
);
172 void DebugRectHistory::SaveTouchEventHandlerRectsCallback(LayerImpl
* layer
) {
173 for (Region::Iterator
iter(layer
->touch_event_handler_region());
176 gfx::Rect touch_rect
= gfx::ScaleToEnclosingRect(
177 iter
.rect(), layer
->contents_scale_x(), layer
->contents_scale_y());
178 debug_rects_
.push_back(
179 DebugRect(TOUCH_EVENT_HANDLER_RECT_TYPE
,
180 MathUtil::MapEnclosingClippedRect(
181 layer
->screen_space_transform(), touch_rect
)));
185 void DebugRectHistory::SaveWheelEventHandlerRects(LayerImpl
* layer
) {
186 LayerTreeHostCommon::CallFunctionForSubtree(layer
, [this](LayerImpl
* layer
) {
187 SaveWheelEventHandlerRectsCallback(layer
);
191 void DebugRectHistory::SaveWheelEventHandlerRectsCallback(LayerImpl
* layer
) {
192 if (!layer
->have_wheel_event_handlers())
195 gfx::Rect wheel_rect
=
196 gfx::ScaleToEnclosingRect(gfx::Rect(layer
->content_bounds()),
197 layer
->contents_scale_x(),
198 layer
->contents_scale_y());
199 debug_rects_
.push_back(
200 DebugRect(WHEEL_EVENT_HANDLER_RECT_TYPE
,
201 MathUtil::MapEnclosingClippedRect(
202 layer
->screen_space_transform(), wheel_rect
)));
205 void DebugRectHistory::SaveScrollEventHandlerRects(LayerImpl
* layer
) {
206 LayerTreeHostCommon::CallFunctionForSubtree(layer
, [this](LayerImpl
* layer
) {
207 SaveScrollEventHandlerRectsCallback(layer
);
211 void DebugRectHistory::SaveScrollEventHandlerRectsCallback(LayerImpl
* layer
) {
212 if (!layer
->have_scroll_event_handlers())
215 gfx::Rect scroll_rect
=
216 gfx::ScaleToEnclosingRect(gfx::Rect(layer
->content_bounds()),
217 layer
->contents_scale_x(),
218 layer
->contents_scale_y());
219 debug_rects_
.push_back(
220 DebugRect(SCROLL_EVENT_HANDLER_RECT_TYPE
,
221 MathUtil::MapEnclosingClippedRect(
222 layer
->screen_space_transform(), scroll_rect
)));
225 void DebugRectHistory::SaveNonFastScrollableRects(LayerImpl
* layer
) {
226 LayerTreeHostCommon::CallFunctionForSubtree(layer
, [this](LayerImpl
* layer
) {
227 SaveNonFastScrollableRectsCallback(layer
);
231 void DebugRectHistory::SaveNonFastScrollableRectsCallback(LayerImpl
* layer
) {
232 for (Region::Iterator
iter(layer
->non_fast_scrollable_region());
235 gfx::Rect scroll_rect
= gfx::ScaleToEnclosingRect(
236 iter
.rect(), layer
->contents_scale_x(), layer
->contents_scale_y());
237 debug_rects_
.push_back(
238 DebugRect(NON_FAST_SCROLLABLE_RECT_TYPE
,
239 MathUtil::MapEnclosingClippedRect(
240 layer
->screen_space_transform(), scroll_rect
)));
244 void DebugRectHistory::SaveLayerAnimationBoundsRects(
245 const LayerImplList
& render_surface_layer_list
) {
246 typedef LayerIterator
<LayerImpl
> LayerIteratorType
;
247 LayerIteratorType end
= LayerIteratorType::End(&render_surface_layer_list
);
248 for (LayerIteratorType it
=
249 LayerIteratorType::Begin(&render_surface_layer_list
);
251 if (!it
.represents_itself())
254 // TODO(avallee): Figure out if we should show something for a layer who's
255 // animating bounds but that we can't compute them.
256 gfx::BoxF inflated_bounds
;
257 if (!LayerUtils::GetAnimationBounds(**it
, &inflated_bounds
))
260 debug_rects_
.push_back(
261 DebugRect(ANIMATION_BOUNDS_RECT_TYPE
,
262 gfx::ToEnclosingRect(gfx::RectF(inflated_bounds
.x(),
264 inflated_bounds
.width(),
265 inflated_bounds
.height()))));