Infobar material design refresh: bg color
[chromium-blink-merge.git] / cc / trees / damage_tracker.cc
blob65d3fd321727bf62edaa4baa440fb172c59f027b
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"
7 #include <algorithm>
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"
18 namespace cc {
20 scoped_ptr<DamageTracker> DamageTracker::Create() {
21 return make_scoped_ptr(new DamageTracker());
24 DamageTracker::DamageTracker()
25 : mailboxId_(0) {}
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
65 // includes:
66 // - any changes for existing layers/surfaces that contribute to the target
67 // surface
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.
76 // else
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
111 // not updated.
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
121 // erased from map.
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;
138 } else {
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
149 // frame.
150 current_damage_rect_.Union(damage_rect_for_this_update);
153 DamageTracker::RectMapData& DamageTracker::RectDataForLayer(
154 int layer_id,
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);
167 return *it;
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())
183 continue;
184 if (LayerTreeHostCommon::RenderSurfaceContributesToTarget<LayerImpl>(
185 layer, target_surface_layer_id))
186 ExtendDamageForRenderSurface(layer, &damage_rect);
187 else
188 ExtendDamageForLayer(layer, &damage_rect);
191 return damage_rect;
194 gfx::Rect DamageTracker::TrackDamageFromSurfaceMask(
195 LayerImpl* target_surface_mask_layer) {
196 gfx::Rect damage_rect;
198 if (!target_surface_mask_layer)
199 return damage_rect;
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());
209 return damage_rect;
212 void DamageTracker::PrepareRectHistoryForUpdate() {
213 mailboxId_++;
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;
238 ++copy_pos;
239 } else {
240 damage_rect.Union(cur_pos->rect_);
243 ++cur_pos;
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_);
253 return damage_rect;
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
262 // is now exposed.
263 // - note that in many cases the old and new layer rects may overlap,
264 // which is fine.
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 if (layer_is_new || layer->LayerPropertyChanged()) {
284 // If a layer is new or has changed, then its entire layer rect affects the
285 // target surface.
286 target_damage_rect->Union(rect_in_target_space);
288 // The layer's old region is now exposed on the target surface, too.
289 // Note old_rect_in_target_space is already in target space.
290 target_damage_rect->Union(old_rect_in_target_space);
291 return;
294 // If the layer properties haven't changed, then the the target surface is
295 // only affected by the layer's damaged area, which could be empty.
296 gfx::Rect damage_rect =
297 gfx::UnionRects(layer->update_rect(), layer->damage_rect());
298 damage_rect.Intersect(gfx::Rect(layer->bounds()));
299 if (!damage_rect.IsEmpty()) {
300 gfx::Rect damage_rect_in_target_space =
301 MathUtil::MapEnclosingClippedRect(layer->draw_transform(), damage_rect);
302 target_damage_rect->Union(damage_rect_in_target_space);
306 void DamageTracker::ExtendDamageForRenderSurface(
307 LayerImpl* layer,
308 gfx::Rect* target_damage_rect) {
309 // There are two ways a "descendant surface" can damage regions of the "target
310 // surface":
311 // 1. Property change:
312 // - a surface's geometry can change because of
313 // - changes to descendants (i.e. the subtree) that affect the
314 // surface's content rect
315 // - changes to ancestor layers that propagate their property
316 // changes to their entire subtree.
317 // - just like layers, both the old surface rect and new surface rect
318 // will damage the target surface in this case.
320 // 2. Damage rect: This surface may have been damaged by its own layer_list
321 // as well, and that damage should propagate to the target surface.
324 RenderSurfaceImpl* render_surface = layer->render_surface();
326 bool surface_is_new = false;
327 RectMapData& data = RectDataForLayer(layer->id(), &surface_is_new);
328 gfx::Rect old_surface_rect = data.rect_;
330 // The drawableContextRect() already includes the replica if it exists.
331 gfx::Rect surface_rect_in_target_space =
332 gfx::ToEnclosingRect(render_surface->DrawableContentRect());
333 data.Update(surface_rect_in_target_space, mailboxId_);
335 gfx::Rect damage_rect_in_local_space;
336 if (surface_is_new || render_surface->SurfacePropertyChanged()) {
337 // The entire surface contributes damage.
338 damage_rect_in_local_space = render_surface->content_rect();
340 // The surface's old region is now exposed on the target surface, too.
341 target_damage_rect->Union(old_surface_rect);
342 } else {
343 // Only the surface's damage_rect will damage the target surface.
344 damage_rect_in_local_space =
345 render_surface->damage_tracker()->current_damage_rect();
348 // If there was damage, transform it to target space, and possibly contribute
349 // its reflection if needed.
350 if (!damage_rect_in_local_space.IsEmpty()) {
351 const gfx::Transform& draw_transform = render_surface->draw_transform();
352 gfx::Rect damage_rect_in_target_space = MathUtil::MapEnclosingClippedRect(
353 draw_transform, damage_rect_in_local_space);
354 target_damage_rect->Union(damage_rect_in_target_space);
356 if (layer->replica_layer()) {
357 const gfx::Transform& replica_draw_transform =
358 render_surface->replica_draw_transform();
359 target_damage_rect->Union(MathUtil::MapEnclosingClippedRect(
360 replica_draw_transform, damage_rect_in_local_space));
364 // If there was damage on the replica's mask, then the target surface receives
365 // that damage as well.
366 if (layer->replica_layer() && layer->replica_layer()->mask_layer()) {
367 LayerImpl* replica_mask_layer = layer->replica_layer()->mask_layer();
369 bool replica_is_new = false;
370 RectMapData& data =
371 RectDataForLayer(replica_mask_layer->id(), &replica_is_new);
373 const gfx::Transform& replica_draw_transform =
374 render_surface->replica_draw_transform();
375 gfx::Rect replica_mask_layer_rect = MathUtil::MapEnclosingClippedRect(
376 replica_draw_transform, gfx::Rect(replica_mask_layer->bounds()));
377 data.Update(replica_mask_layer_rect, mailboxId_);
379 // In the current implementation, a change in the replica mask damages the
380 // entire replica region.
381 if (replica_is_new ||
382 replica_mask_layer->LayerPropertyChanged() ||
383 !replica_mask_layer->update_rect().IsEmpty())
384 target_damage_rect->Union(replica_mask_layer_rect);
387 // If the layer has a background filter, this may cause pixels in our surface
388 // to be expanded, so we will need to expand any damage at or below this
389 // layer. We expand the damage from this layer too, as we need to readback
390 // those pixels from the surface with only the contents of layers below this
391 // one in them. This means we need to redraw any pixels in the surface being
392 // used for the blur in this layer this frame.
393 if (layer->background_filters().HasFilterThatMovesPixels()) {
394 ExpandDamageRectInsideRectWithFilters(target_damage_rect,
395 surface_rect_in_target_space,
396 layer->background_filters());
400 } // namespace cc