1 // Copyright 2011 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/trees/damage_tracker.h"
9 #include "cc/base/math_util.h"
10 #include "cc/layers/heads_up_display_layer_impl.h"
11 #include "cc/layers/layer_impl.h"
12 #include "cc/layers/render_surface_impl.h"
13 #include "cc/output/filter_operations.h"
14 #include "cc/trees/layer_tree_host_common.h"
15 #include "cc/trees/layer_tree_impl.h"
16 #include "ui/gfx/geometry/rect_conversions.h"
20 scoped_ptr
<DamageTracker
> DamageTracker::Create() {
21 return make_scoped_ptr(new DamageTracker());
24 DamageTracker::DamageTracker()
27 DamageTracker::~DamageTracker() {}
29 static inline void ExpandRectWithFilters(gfx::Rect
* rect
,
30 const FilterOperations
& filters
) {
31 int top
, right
, bottom
, left
;
32 filters
.GetOutsets(&top
, &right
, &bottom
, &left
);
33 rect
->Inset(-left
, -top
, -right
, -bottom
);
36 static inline void ExpandDamageRectInsideRectWithFilters(
37 gfx::Rect
* damage_rect
,
38 const gfx::Rect
& pre_filter_rect
,
39 const FilterOperations
& filters
) {
40 gfx::Rect expanded_damage_rect
= *damage_rect
;
41 ExpandRectWithFilters(&expanded_damage_rect
, filters
);
42 gfx::Rect filter_rect
= pre_filter_rect
;
43 ExpandRectWithFilters(&filter_rect
, filters
);
45 expanded_damage_rect
.Intersect(filter_rect
);
46 damage_rect
->Union(expanded_damage_rect
);
49 void DamageTracker::UpdateDamageTrackingState(
50 const LayerImplList
& layer_list
,
51 int target_surface_layer_id
,
52 bool target_surface_property_changed_only_from_descendant
,
53 const gfx::Rect
& target_surface_content_rect
,
54 LayerImpl
* target_surface_mask_layer
,
55 const FilterOperations
& filters
) {
57 // This function computes the "damage rect" of a target surface, and updates
58 // the state that is used to correctly track damage across frames. The damage
59 // rect is the region of the surface that may have changed and needs to be
60 // redrawn. This can be used to scissor what is actually drawn, to save GPU
61 // computation and bandwidth.
63 // The surface's damage rect is computed as the union of all possible changes
64 // that have happened to the surface since the last frame was drawn. This
66 // - any changes for existing layers/surfaces that contribute to the target
68 // - layers/surfaces that existed in the previous frame, but no longer exist
70 // The basic algorithm for computing the damage region is as follows:
72 // 1. compute damage caused by changes in active/new layers
73 // for each layer in the layer_list:
74 // if the layer is actually a render_surface:
75 // add the surface's damage to our target surface.
77 // add the layer's damage to the target surface.
79 // 2. compute damage caused by the target surface's mask, if it exists.
81 // 3. compute damage caused by old layers/surfaces that no longer exist
82 // for each leftover layer:
83 // add the old layer/surface bounds to the target surface damage.
85 // 4. combine all partial damage rects to get the full damage rect.
87 // Additional important points:
89 // - This algorithm is implicitly recursive; it assumes that descendant
90 // surfaces have already computed their damage.
92 // - Changes to layers/surfaces indicate "damage" to the target surface; If a
93 // layer is not changed, it does NOT mean that the layer can skip drawing.
94 // All layers that overlap the damaged region still need to be drawn. For
95 // example, if a layer changed its opacity, then layers underneath must be
96 // re-drawn as well, even if they did not change.
98 // - If a layer/surface property changed, the old bounds and new bounds may
99 // overlap... i.e. some of the exposed region may not actually be exposing
100 // anything. But this does not artificially inflate the damage rect. If the
101 // layer changed, its entire old bounds would always need to be redrawn,
102 // regardless of how much it overlaps with the layer's new bounds, which
103 // also need to be entirely redrawn.
105 // - See comments in the rest of the code to see what exactly is considered a
106 // "change" in a layer/surface.
108 // - To correctly manage exposed rects, SortedRectMap is maintained:
110 // 1. All existing rects from the previous frame are marked as
112 // 2. The map contains all the layer bounds that contributed to
113 // the previous frame (even outside the previous damaged area). If a
114 // layer changes or does not exist anymore, those regions are then
115 // exposed and damage the target surface. As the algorithm progresses,
116 // entries are updated in the map until only leftover layers
117 // that no longer exist stay marked not updated.
119 // 3. After the damage rect is computed, the leftover not marked regions
120 // in a map are used to compute are damaged by deleted layers and
124 PrepareRectHistoryForUpdate();
125 // These functions cannot be bypassed with early-exits, even if we know what
126 // the damage will be for this frame, because we need to update the damage
127 // tracker state to correctly track the next frame.
128 gfx::Rect damage_from_active_layers
=
129 TrackDamageFromActiveLayers(layer_list
, target_surface_layer_id
);
130 gfx::Rect damage_from_surface_mask
=
131 TrackDamageFromSurfaceMask(target_surface_mask_layer
);
132 gfx::Rect damage_from_leftover_rects
= TrackDamageFromLeftoverRects();
134 gfx::Rect damage_rect_for_this_update
;
136 if (target_surface_property_changed_only_from_descendant
) {
137 damage_rect_for_this_update
= target_surface_content_rect
;
139 // TODO(shawnsingh): can we clamp this damage to the surface's content rect?
140 // (affects performance, but not correctness)
141 damage_rect_for_this_update
= damage_from_active_layers
;
142 damage_rect_for_this_update
.Union(damage_from_surface_mask
);
143 damage_rect_for_this_update
.Union(damage_from_leftover_rects
);
145 ExpandRectWithFilters(&damage_rect_for_this_update
, filters
);
148 // Damage accumulates until we are notified that we actually did draw on that
150 current_damage_rect_
.Union(damage_rect_for_this_update
);
153 DamageTracker::RectMapData
& DamageTracker::RectDataForLayer(
155 bool* layer_is_new
) {
157 RectMapData
data(layer_id
);
159 SortedRectMap::iterator it
= std::lower_bound(rect_history_
.begin(),
160 rect_history_
.end(), data
);
162 if (it
== rect_history_
.end() || it
->layer_id_
!= layer_id
) {
163 *layer_is_new
= true;
164 it
= rect_history_
.insert(it
, data
);
170 gfx::Rect
DamageTracker::TrackDamageFromActiveLayers(
171 const LayerImplList
& layer_list
,
172 int target_surface_layer_id
) {
173 gfx::Rect damage_rect
;
175 for (size_t layer_index
= 0; layer_index
< layer_list
.size(); ++layer_index
) {
176 // Visit layers in back-to-front order.
177 LayerImpl
* layer
= layer_list
[layer_index
];
179 // We skip damage from the HUD layer because (a) the HUD layer damages the
180 // whole frame and (b) we don't want HUD layer damage to be shown by the
181 // HUD damage rect visualization.
182 if (layer
== layer
->layer_tree_impl()->hud_layer())
184 if (LayerTreeHostCommon::RenderSurfaceContributesToTarget
<LayerImpl
>(
185 layer
, target_surface_layer_id
))
186 ExtendDamageForRenderSurface(layer
, &damage_rect
);
188 ExtendDamageForLayer(layer
, &damage_rect
);
194 gfx::Rect
DamageTracker::TrackDamageFromSurfaceMask(
195 LayerImpl
* target_surface_mask_layer
) {
196 gfx::Rect damage_rect
;
198 if (!target_surface_mask_layer
)
201 // Currently, if there is any change to the mask, we choose to damage the
202 // entire surface. This could potentially be optimized later, but it is not
203 // expected to be a common case.
204 if (target_surface_mask_layer
->LayerPropertyChanged() ||
205 !target_surface_mask_layer
->update_rect().IsEmpty()) {
206 damage_rect
= gfx::Rect(target_surface_mask_layer
->bounds());
212 void DamageTracker::PrepareRectHistoryForUpdate() {
216 gfx::Rect
DamageTracker::TrackDamageFromLeftoverRects() {
217 // After computing damage for all active layers, any leftover items in the
218 // current rect history correspond to layers/surfaces that no longer exist.
219 // So, these regions are now exposed on the target surface.
221 gfx::Rect damage_rect
;
222 SortedRectMap::iterator cur_pos
= rect_history_
.begin();
223 SortedRectMap::iterator copy_pos
= cur_pos
;
225 // Loop below basically implements std::remove_if loop with and extra
226 // processing (adding deleted rect to damage_rect) for deleted items.
227 // cur_pos iterator runs through all elements of the vector, but copy_pos
228 // always points to the element after the last not deleted element. If new
229 // not deleted element found then it is copied to the *copy_pos and copy_pos
230 // moved to the next position.
231 // If there are no deleted elements then copy_pos iterator is in sync with
232 // cur_pos and no copy happens.
233 while (cur_pos
< rect_history_
.end()) {
234 if (cur_pos
->mailboxId_
== mailboxId_
) {
235 if (cur_pos
!= copy_pos
)
236 *copy_pos
= *cur_pos
;
240 damage_rect
.Union(cur_pos
->rect_
);
246 if (copy_pos
!= rect_history_
.end())
247 rect_history_
.erase(copy_pos
, rect_history_
.end());
249 // If the vector has excessive storage, shrink it
250 if (rect_history_
.capacity() > rect_history_
.size() * 4)
251 SortedRectMap(rect_history_
).swap(rect_history_
);
256 void DamageTracker::ExtendDamageForLayer(LayerImpl
* layer
,
257 gfx::Rect
* target_damage_rect
) {
258 // There are two ways that a layer can damage a region of the target surface:
259 // 1. Property change (e.g. opacity, position, transforms):
260 // - the entire region of the layer itself damages the surface.
261 // - the old layer region also damages the surface, because this region
263 // - note that in many cases the old and new layer rects may overlap,
266 // 2. Repaint/update: If a region of the layer that was repainted/updated,
267 // that region damages the surface.
269 // Property changes take priority over update rects.
271 // This method is called when we want to consider how a layer contributes to
272 // its target RenderSurface, even if that layer owns the target RenderSurface
273 // itself. To consider how a layer's target surface contributes to the
274 // ancestor surface, ExtendDamageForRenderSurface() must be called instead.
276 bool layer_is_new
= false;
277 RectMapData
& data
= RectDataForLayer(layer
->id(), &layer_is_new
);
278 gfx::Rect old_rect_in_target_space
= data
.rect_
;
280 gfx::Rect rect_in_target_space
= layer
->GetEnclosingRectInTargetSpace();
281 data
.Update(rect_in_target_space
, mailboxId_
);
283 gfx::RectF damage_rect
=
284 gfx::UnionRects(layer
->update_rect(), layer
->damage_rect());
285 damage_rect
.Intersect(gfx::RectF(layer
->bounds()));
287 if (layer_is_new
|| layer
->LayerPropertyChanged()) {
288 // If a layer is new or has changed, then its entire layer rect affects the
290 target_damage_rect
->Union(rect_in_target_space
);
292 // The layer's old region is now exposed on the target surface, too.
293 // Note old_rect_in_target_space is already in target space.
294 target_damage_rect
->Union(old_rect_in_target_space
);
295 } else if (!damage_rect
.IsEmpty()) {
296 // If the layer properties haven't changed, then the the target surface is
297 // only affected by the layer's damaged area, which could be empty.
298 gfx::Rect damage_rect_in_target_space
= gfx::ToEnclosingRect(
299 MathUtil::MapClippedRect(layer
->draw_transform(), damage_rect
));
300 target_damage_rect
->Union(damage_rect_in_target_space
);
304 void DamageTracker::ExtendDamageForRenderSurface(
306 gfx::Rect
* target_damage_rect
) {
307 // There are two ways a "descendant surface" can damage regions of the "target
309 // 1. Property change:
310 // - a surface's geometry can change because of
311 // - changes to descendants (i.e. the subtree) that affect the
312 // surface's content rect
313 // - changes to ancestor layers that propagate their property
314 // changes to their entire subtree.
315 // - just like layers, both the old surface rect and new surface rect
316 // will damage the target surface in this case.
318 // 2. Damage rect: This surface may have been damaged by its own layer_list
319 // as well, and that damage should propagate to the target surface.
322 RenderSurfaceImpl
* render_surface
= layer
->render_surface();
324 bool surface_is_new
= false;
325 RectMapData
& data
= RectDataForLayer(layer
->id(), &surface_is_new
);
326 gfx::Rect old_surface_rect
= data
.rect_
;
328 // The drawableContextRect() already includes the replica if it exists.
329 gfx::Rect surface_rect_in_target_space
=
330 gfx::ToEnclosingRect(render_surface
->DrawableContentRect());
331 data
.Update(surface_rect_in_target_space
, mailboxId_
);
333 gfx::Rect damage_rect_in_local_space
;
334 if (surface_is_new
|| render_surface
->SurfacePropertyChanged()) {
335 // The entire surface contributes damage.
336 damage_rect_in_local_space
= render_surface
->content_rect();
338 // The surface's old region is now exposed on the target surface, too.
339 target_damage_rect
->Union(old_surface_rect
);
341 // Only the surface's damage_rect will damage the target surface.
342 damage_rect_in_local_space
=
343 render_surface
->damage_tracker()->current_damage_rect();
346 // If there was damage, transform it to target space, and possibly contribute
347 // its reflection if needed.
348 if (!damage_rect_in_local_space
.IsEmpty()) {
349 const gfx::Transform
& draw_transform
= render_surface
->draw_transform();
350 gfx::Rect damage_rect_in_target_space
= MathUtil::MapEnclosingClippedRect(
351 draw_transform
, damage_rect_in_local_space
);
352 target_damage_rect
->Union(damage_rect_in_target_space
);
354 if (layer
->replica_layer()) {
355 const gfx::Transform
& replica_draw_transform
=
356 render_surface
->replica_draw_transform();
357 target_damage_rect
->Union(MathUtil::MapEnclosingClippedRect(
358 replica_draw_transform
, damage_rect_in_local_space
));
362 // If there was damage on the replica's mask, then the target surface receives
363 // that damage as well.
364 if (layer
->replica_layer() && layer
->replica_layer()->mask_layer()) {
365 LayerImpl
* replica_mask_layer
= layer
->replica_layer()->mask_layer();
367 bool replica_is_new
= false;
369 RectDataForLayer(replica_mask_layer
->id(), &replica_is_new
);
371 const gfx::Transform
& replica_draw_transform
=
372 render_surface
->replica_draw_transform();
373 gfx::Rect replica_mask_layer_rect
= MathUtil::MapEnclosingClippedRect(
374 replica_draw_transform
, gfx::Rect(replica_mask_layer
->bounds()));
375 data
.Update(replica_mask_layer_rect
, mailboxId_
);
377 // In the current implementation, a change in the replica mask damages the
378 // entire replica region.
379 if (replica_is_new
||
380 replica_mask_layer
->LayerPropertyChanged() ||
381 !replica_mask_layer
->update_rect().IsEmpty())
382 target_damage_rect
->Union(replica_mask_layer_rect
);
385 // If the layer has a background filter, this may cause pixels in our surface
386 // to be expanded, so we will need to expand any damage at or below this
387 // layer. We expand the damage from this layer too, as we need to readback
388 // those pixels from the surface with only the contents of layers below this
389 // one in them. This means we need to redraw any pixels in the surface being
390 // used for the blur in this layer this frame.
391 if (layer
->background_filters().HasFilterThatMovesPixels()) {
392 ExpandDamageRectInsideRectWithFilters(target_damage_rect
,
393 surface_rect_in_target_space
,
394 layer
->background_filters());