OLD | NEW |
1 // Copyright 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/trees/damage_tracker.h" | 5 #include "cc/trees/damage_tracker.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "cc/base/math_util.h" | 9 #include "cc/base/math_util.h" |
10 #include "cc/layers/heads_up_display_layer_impl.h" | 10 #include "cc/layers/heads_up_display_layer_impl.h" |
11 #include "cc/layers/layer_impl.h" | 11 #include "cc/layers/layer_impl.h" |
12 #include "cc/layers/render_surface_impl.h" | 12 #include "cc/layers/render_surface_impl.h" |
13 #include "cc/output/filter_operations.h" | 13 #include "cc/output/filter_operations.h" |
14 #include "cc/trees/layer_tree_host_common.h" | 14 #include "cc/trees/layer_tree_host_common.h" |
15 #include "cc/trees/layer_tree_impl.h" | 15 #include "cc/trees/layer_tree_impl.h" |
16 | 16 |
17 namespace cc { | 17 namespace cc { |
18 | 18 |
19 scoped_ptr<DamageTracker> DamageTracker::Create() { | 19 scoped_ptr<DamageTracker> DamageTracker::Create() { |
20 return make_scoped_ptr(new DamageTracker()); | 20 return make_scoped_ptr(new DamageTracker()); |
21 } | 21 } |
22 | 22 |
23 DamageTracker::DamageTracker() | 23 DamageTracker::DamageTracker() |
24 : current_rect_history_(new RectMap), | 24 : mailboxId_(0) {} |
25 next_rect_history_(new RectMap) {} | |
26 | 25 |
27 DamageTracker::~DamageTracker() {} | 26 DamageTracker::~DamageTracker() {} |
28 | 27 |
29 static inline void ExpandRectWithFilters( | 28 static inline void ExpandRectWithFilters( |
30 gfx::RectF* rect, const FilterOperations& filters) { | 29 gfx::RectF* rect, const FilterOperations& filters) { |
31 int top, right, bottom, left; | 30 int top, right, bottom, left; |
32 filters.GetOutsets(&top, &right, &bottom, &left); | 31 filters.GetOutsets(&top, &right, &bottom, &left); |
33 rect->Inset(-left, -top, -right, -bottom); | 32 rect->Inset(-left, -top, -right, -bottom); |
34 } | 33 } |
35 | 34 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 // - If a layer/surface property changed, the old bounds and new bounds may | 97 // - 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 | 98 // 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 | 99 // 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, | 100 // 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 | 101 // regardless of how much it overlaps with the layer's new bounds, which |
103 // also need to be entirely redrawn. | 102 // also need to be entirely redrawn. |
104 // | 103 // |
105 // - See comments in the rest of the code to see what exactly is considered a | 104 // - See comments in the rest of the code to see what exactly is considered a |
106 // "change" in a layer/surface. | 105 // "change" in a layer/surface. |
107 // | 106 // |
108 // - To correctly manage exposed rects, two RectMaps are maintained: | 107 // - To correctly manage exposed rects, SortedRectMap is maintained: |
109 // | 108 // |
110 // 1. The "current" map contains all the layer bounds that contributed to | 109 // 1. All existing rects from the previous frame are marked as |
| 110 // not updated. |
| 111 // 2. The map contains all the layer bounds that contributed to |
111 // the previous frame (even outside the previous damaged area). If a | 112 // the previous frame (even outside the previous damaged area). If a |
112 // layer changes or does not exist anymore, those regions are then | 113 // layer changes or does not exist anymore, those regions are then |
113 // exposed and damage the target surface. As the algorithm progresses, | 114 // exposed and damage the target surface. As the algorithm progresses, |
114 // entries are removed from the map until it has only leftover layers | 115 // entries are updated in the map until only leftover layers |
115 // that no longer exist. | 116 // that no longer exist stay marked not updated. |
116 // | 117 // |
117 // 2. The "next" map starts out empty, and as the algorithm progresses, | 118 // 3. After the damage rect is computed, the leftover not marked regions |
118 // every layer/surface that contributes to the surface is added to the | 119 // in a map are used to compute are damaged by deleted layers and |
119 // map. | 120 // erased from map. |
120 // | |
121 // 3. After the damage rect is computed, the two maps are swapped, so | |
122 // that the damage tracker is ready for the next frame. | |
123 // | 121 // |
124 | 122 |
| 123 PrepareRectHistoryForUpdate(); |
125 // These functions cannot be bypassed with early-exits, even if we know what | 124 // 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 | 125 // the damage will be for this frame, because we need to update the damage |
127 // tracker state to correctly track the next frame. | 126 // tracker state to correctly track the next frame. |
128 gfx::RectF damage_from_active_layers = | 127 gfx::RectF damage_from_active_layers = |
129 TrackDamageFromActiveLayers(layer_list, target_surface_layer_id); | 128 TrackDamageFromActiveLayers(layer_list, target_surface_layer_id); |
130 gfx::RectF damage_from_surface_mask = | 129 gfx::RectF damage_from_surface_mask = |
131 TrackDamageFromSurfaceMask(target_surface_mask_layer); | 130 TrackDamageFromSurfaceMask(target_surface_mask_layer); |
132 gfx::RectF damage_from_leftover_rects = TrackDamageFromLeftoverRects(); | 131 gfx::RectF damage_from_leftover_rects = TrackDamageFromLeftoverRects(); |
133 | 132 |
134 gfx::RectF damage_rect_for_this_update; | 133 gfx::RectF damage_rect_for_this_update; |
(...skipping 12 matching lines...) Expand all Loading... |
147 // those here to limit damage. | 146 // those here to limit damage. |
148 damage_rect_for_this_update = target_surface_content_rect; | 147 damage_rect_for_this_update = target_surface_content_rect; |
149 } else if (filters.HasFilterThatMovesPixels()) { | 148 } else if (filters.HasFilterThatMovesPixels()) { |
150 ExpandRectWithFilters(&damage_rect_for_this_update, filters); | 149 ExpandRectWithFilters(&damage_rect_for_this_update, filters); |
151 } | 150 } |
152 } | 151 } |
153 | 152 |
154 // Damage accumulates until we are notified that we actually did draw on that | 153 // Damage accumulates until we are notified that we actually did draw on that |
155 // frame. | 154 // frame. |
156 current_damage_rect_.Union(damage_rect_for_this_update); | 155 current_damage_rect_.Union(damage_rect_for_this_update); |
157 | |
158 // The next history map becomes the current map for the next frame. Note this | |
159 // must happen every frame to correctly track changes, even if damage | |
160 // accumulates over multiple frames before actually being drawn. | |
161 swap(current_rect_history_, next_rect_history_); | |
162 } | 156 } |
163 | 157 |
164 gfx::RectF DamageTracker::RemoveRectFromCurrentFrame(int layer_id, | 158 DamageTracker::RectMapData& DamageTracker::RectDataForLayer( |
165 bool* layer_is_new) { | 159 int layer_id, |
166 RectMap::iterator iter = current_rect_history_->find(layer_id); | 160 bool* layer_is_new) { |
167 *layer_is_new = iter == current_rect_history_->end(); | |
168 if (*layer_is_new) | |
169 return gfx::RectF(); | |
170 | 161 |
171 gfx::RectF ret = iter->second; | 162 RectMapData data(layer_id); |
172 current_rect_history_->erase(iter); | |
173 return ret; | |
174 } | |
175 | 163 |
176 void DamageTracker::SaveRectForNextFrame(int layer_id, | 164 SortedRectMap::iterator it = std::lower_bound(rect_history_.begin(), |
177 const gfx::RectF& target_space_rect) { | 165 rect_history_.end(), data); |
178 // This layer should not yet exist in next frame's history. | 166 |
179 DCHECK_GT(layer_id, 0); | 167 if (it == rect_history_.end() || it->layer_id_ != layer_id) { |
180 DCHECK(next_rect_history_->find(layer_id) == next_rect_history_->end()); | 168 *layer_is_new = true; |
181 (*next_rect_history_)[layer_id] = target_space_rect; | 169 it = rect_history_.insert(it, data); |
| 170 } |
| 171 |
| 172 return *it; |
182 } | 173 } |
183 | 174 |
184 gfx::RectF DamageTracker::TrackDamageFromActiveLayers( | 175 gfx::RectF DamageTracker::TrackDamageFromActiveLayers( |
185 const LayerImplList& layer_list, | 176 const LayerImplList& layer_list, |
186 int target_surface_layer_id) { | 177 int target_surface_layer_id) { |
187 gfx::RectF damage_rect = gfx::RectF(); | 178 gfx::RectF damage_rect = gfx::RectF(); |
188 | 179 |
189 for (size_t layer_index = 0; layer_index < layer_list.size(); ++layer_index) { | 180 for (size_t layer_index = 0; layer_index < layer_list.size(); ++layer_index) { |
190 // Visit layers in back-to-front order. | 181 // Visit layers in back-to-front order. |
191 LayerImpl* layer = layer_list[layer_index]; | 182 LayerImpl* layer = layer_list[layer_index]; |
(...skipping 26 matching lines...) Expand all Loading... |
218 // expected to be a common case. | 209 // expected to be a common case. |
219 if (target_surface_mask_layer->LayerPropertyChanged() || | 210 if (target_surface_mask_layer->LayerPropertyChanged() || |
220 !target_surface_mask_layer->update_rect().IsEmpty()) { | 211 !target_surface_mask_layer->update_rect().IsEmpty()) { |
221 damage_rect = gfx::RectF(gfx::PointF(), | 212 damage_rect = gfx::RectF(gfx::PointF(), |
222 target_surface_mask_layer->bounds()); | 213 target_surface_mask_layer->bounds()); |
223 } | 214 } |
224 | 215 |
225 return damage_rect; | 216 return damage_rect; |
226 } | 217 } |
227 | 218 |
| 219 void DamageTracker::PrepareRectHistoryForUpdate() { |
| 220 mailboxId_++; |
| 221 } |
| 222 |
228 gfx::RectF DamageTracker::TrackDamageFromLeftoverRects() { | 223 gfx::RectF DamageTracker::TrackDamageFromLeftoverRects() { |
229 // After computing damage for all active layers, any leftover items in the | 224 // After computing damage for all active layers, any leftover items in the |
230 // current rect history correspond to layers/surfaces that no longer exist. | 225 // current rect history correspond to layers/surfaces that no longer exist. |
231 // So, these regions are now exposed on the target surface. | 226 // So, these regions are now exposed on the target surface. |
232 | 227 |
233 gfx::RectF damage_rect = gfx::RectF(); | 228 gfx::RectF damage_rect = gfx::RectF(); |
| 229 SortedRectMap::iterator cur_pos = rect_history_.begin(); |
| 230 SortedRectMap::iterator copy_pos = cur_pos; |
234 | 231 |
235 for (RectMap::iterator it = current_rect_history_->begin(); | 232 // Loop below basically implements std::remove_if loop with and extra |
236 it != current_rect_history_->end(); | 233 // processing (adding deleted rect to damage_rect) for deleted items. |
237 ++it) | 234 // cur_pos iterator runs through all elements of the vector, but copy_pos |
238 damage_rect.Union(it->second); | 235 // always points to the element after the last not deleted element. If new |
| 236 // not deleted element found then it is copied to the *copy_pos and copy_pos |
| 237 // moved to the next position. |
| 238 // If there are no deleted elements then copy_pos iterator is in sync with |
| 239 // cur_pos and no copy happens. |
| 240 while (cur_pos < rect_history_.end()) { |
| 241 if (cur_pos->mailboxId_ == mailboxId_) { |
| 242 if (cur_pos != copy_pos) |
| 243 *copy_pos = *cur_pos; |
239 | 244 |
240 current_rect_history_->clear(); | 245 ++copy_pos; |
| 246 } else { |
| 247 damage_rect.Union(cur_pos->rect_); |
| 248 } |
| 249 |
| 250 ++cur_pos; |
| 251 } |
| 252 |
| 253 if (copy_pos != rect_history_.end()) |
| 254 rect_history_.erase(copy_pos, rect_history_.end()); |
| 255 |
| 256 // If the vector has excessive storage, shrink it |
| 257 if (rect_history_.capacity() > rect_history_.size() * 4) |
| 258 SortedRectMap(rect_history_).swap(rect_history_); |
241 | 259 |
242 return damage_rect; | 260 return damage_rect; |
243 } | 261 } |
244 | 262 |
245 void DamageTracker::ExtendDamageForLayer(LayerImpl* layer, | 263 void DamageTracker::ExtendDamageForLayer(LayerImpl* layer, |
246 gfx::RectF* target_damage_rect) { | 264 gfx::RectF* target_damage_rect) { |
247 // There are two ways that a layer can damage a region of the target surface: | 265 // There are two ways that a layer can damage a region of the target surface: |
248 // 1. Property change (e.g. opacity, position, transforms): | 266 // 1. Property change (e.g. opacity, position, transforms): |
249 // - the entire region of the layer itself damages the surface. | 267 // - the entire region of the layer itself damages the surface. |
250 // - the old layer region also damages the surface, because this region | 268 // - the old layer region also damages the surface, because this region |
251 // is now exposed. | 269 // is now exposed. |
252 // - note that in many cases the old and new layer rects may overlap, | 270 // - note that in many cases the old and new layer rects may overlap, |
253 // which is fine. | 271 // which is fine. |
254 // | 272 // |
255 // 2. Repaint/update: If a region of the layer that was repainted/updated, | 273 // 2. Repaint/update: If a region of the layer that was repainted/updated, |
256 // that region damages the surface. | 274 // that region damages the surface. |
257 // | 275 // |
258 // Property changes take priority over update rects. | 276 // Property changes take priority over update rects. |
259 // | 277 // |
260 // This method is called when we want to consider how a layer contributes to | 278 // This method is called when we want to consider how a layer contributes to |
261 // its target RenderSurface, even if that layer owns the target RenderSurface | 279 // its target RenderSurface, even if that layer owns the target RenderSurface |
262 // itself. To consider how a layer's target surface contributes to the | 280 // itself. To consider how a layer's target surface contributes to the |
263 // ancestor surface, ExtendDamageForRenderSurface() must be called instead. | 281 // ancestor surface, ExtendDamageForRenderSurface() must be called instead. |
264 | 282 |
265 bool layer_is_new = false; | 283 bool layer_is_new = false; |
266 gfx::RectF old_rect_in_target_space = | 284 RectMapData& data = RectDataForLayer(layer->id(), &layer_is_new); |
267 RemoveRectFromCurrentFrame(layer->id(), &layer_is_new); | 285 gfx::RectF old_rect_in_target_space = data.rect_; |
268 | 286 |
269 gfx::RectF rect_in_target_space = MathUtil::MapClippedRect( | 287 gfx::RectF rect_in_target_space = MathUtil::MapClippedRect( |
270 layer->draw_transform(), | 288 layer->draw_transform(), |
271 gfx::RectF(gfx::PointF(), layer->content_bounds())); | 289 gfx::RectF(gfx::PointF(), layer->content_bounds())); |
272 SaveRectForNextFrame(layer->id(), rect_in_target_space); | 290 data.Update(rect_in_target_space, mailboxId_); |
273 | 291 |
274 if (layer_is_new || layer->LayerPropertyChanged()) { | 292 if (layer_is_new || layer->LayerPropertyChanged()) { |
275 // If a layer is new or has changed, then its entire layer rect affects the | 293 // If a layer is new or has changed, then its entire layer rect affects the |
276 // target surface. | 294 // target surface. |
277 target_damage_rect->Union(rect_in_target_space); | 295 target_damage_rect->Union(rect_in_target_space); |
278 | 296 |
279 // The layer's old region is now exposed on the target surface, too. | 297 // The layer's old region is now exposed on the target surface, too. |
280 // Note old_rect_in_target_space is already in target space. | 298 // Note old_rect_in_target_space is already in target space. |
281 target_damage_rect->Union(old_rect_in_target_space); | 299 target_damage_rect->Union(old_rect_in_target_space); |
282 } else if (!layer->update_rect().IsEmpty()) { | 300 } else if (!layer->update_rect().IsEmpty()) { |
(...skipping 20 matching lines...) Expand all Loading... |
303 // - just like layers, both the old surface rect and new surface rect | 321 // - just like layers, both the old surface rect and new surface rect |
304 // will damage the target surface in this case. | 322 // will damage the target surface in this case. |
305 // | 323 // |
306 // 2. Damage rect: This surface may have been damaged by its own layer_list | 324 // 2. Damage rect: This surface may have been damaged by its own layer_list |
307 // as well, and that damage should propagate to the target surface. | 325 // as well, and that damage should propagate to the target surface. |
308 // | 326 // |
309 | 327 |
310 RenderSurfaceImpl* render_surface = layer->render_surface(); | 328 RenderSurfaceImpl* render_surface = layer->render_surface(); |
311 | 329 |
312 bool surface_is_new = false; | 330 bool surface_is_new = false; |
313 gfx::RectF old_surface_rect = RemoveRectFromCurrentFrame(layer->id(), | 331 RectMapData& data = RectDataForLayer(layer->id(), &surface_is_new); |
314 &surface_is_new); | 332 gfx::RectF old_surface_rect = data.rect_; |
315 | 333 |
316 // The drawableContextRect() already includes the replica if it exists. | 334 // The drawableContextRect() already includes the replica if it exists. |
317 gfx::RectF surface_rect_in_target_space = | 335 gfx::RectF surface_rect_in_target_space = |
318 render_surface->DrawableContentRect(); | 336 render_surface->DrawableContentRect(); |
319 SaveRectForNextFrame(layer->id(), surface_rect_in_target_space); | 337 data.Update(surface_rect_in_target_space, mailboxId_); |
320 | 338 |
321 gfx::RectF damage_rect_in_local_space; | 339 gfx::RectF damage_rect_in_local_space; |
322 if (surface_is_new || render_surface->SurfacePropertyChanged()) { | 340 if (surface_is_new || render_surface->SurfacePropertyChanged()) { |
323 // The entire surface contributes damage. | 341 // The entire surface contributes damage. |
324 damage_rect_in_local_space = render_surface->content_rect(); | 342 damage_rect_in_local_space = render_surface->content_rect(); |
325 | 343 |
326 // The surface's old region is now exposed on the target surface, too. | 344 // The surface's old region is now exposed on the target surface, too. |
327 target_damage_rect->Union(old_surface_rect); | 345 target_damage_rect->Union(old_surface_rect); |
328 } else { | 346 } else { |
329 // Only the surface's damage_rect will damage the target surface. | 347 // Only the surface's damage_rect will damage the target surface. |
(...skipping 16 matching lines...) Expand all Loading... |
346 replica_draw_transform, damage_rect_in_local_space)); | 364 replica_draw_transform, damage_rect_in_local_space)); |
347 } | 365 } |
348 } | 366 } |
349 | 367 |
350 // If there was damage on the replica's mask, then the target surface receives | 368 // If there was damage on the replica's mask, then the target surface receives |
351 // that damage as well. | 369 // that damage as well. |
352 if (layer->replica_layer() && layer->replica_layer()->mask_layer()) { | 370 if (layer->replica_layer() && layer->replica_layer()->mask_layer()) { |
353 LayerImpl* replica_mask_layer = layer->replica_layer()->mask_layer(); | 371 LayerImpl* replica_mask_layer = layer->replica_layer()->mask_layer(); |
354 | 372 |
355 bool replica_is_new = false; | 373 bool replica_is_new = false; |
356 RemoveRectFromCurrentFrame(replica_mask_layer->id(), &replica_is_new); | 374 RectMapData& data = |
| 375 RectDataForLayer(replica_mask_layer->id(), &replica_is_new); |
357 | 376 |
358 const gfx::Transform& replica_draw_transform = | 377 const gfx::Transform& replica_draw_transform = |
359 render_surface->replica_draw_transform(); | 378 render_surface->replica_draw_transform(); |
360 gfx::RectF replica_mask_layer_rect = MathUtil::MapClippedRect( | 379 gfx::RectF replica_mask_layer_rect = MathUtil::MapClippedRect( |
361 replica_draw_transform, | 380 replica_draw_transform, |
362 gfx::RectF(gfx::PointF(), replica_mask_layer->bounds())); | 381 gfx::RectF(gfx::PointF(), replica_mask_layer->bounds())); |
363 SaveRectForNextFrame(replica_mask_layer->id(), replica_mask_layer_rect); | 382 data.Update(replica_mask_layer_rect, mailboxId_); |
364 | 383 |
365 // In the current implementation, a change in the replica mask damages the | 384 // In the current implementation, a change in the replica mask damages the |
366 // entire replica region. | 385 // entire replica region. |
367 if (replica_is_new || | 386 if (replica_is_new || |
368 replica_mask_layer->LayerPropertyChanged() || | 387 replica_mask_layer->LayerPropertyChanged() || |
369 !replica_mask_layer->update_rect().IsEmpty()) | 388 !replica_mask_layer->update_rect().IsEmpty()) |
370 target_damage_rect->Union(replica_mask_layer_rect); | 389 target_damage_rect->Union(replica_mask_layer_rect); |
371 } | 390 } |
372 | 391 |
373 // If the layer has a background filter, this may cause pixels in our surface | 392 // If the layer has a background filter, this may cause pixels in our surface |
374 // to be expanded, so we will need to expand any damage at or below this | 393 // to be expanded, so we will need to expand any damage at or below this |
375 // layer. We expand the damage from this layer too, as we need to readback | 394 // layer. We expand the damage from this layer too, as we need to readback |
376 // those pixels from the surface with only the contents of layers below this | 395 // those pixels from the surface with only the contents of layers below this |
377 // one in them. This means we need to redraw any pixels in the surface being | 396 // one in them. This means we need to redraw any pixels in the surface being |
378 // used for the blur in this layer this frame. | 397 // used for the blur in this layer this frame. |
379 if (layer->background_filters().HasFilterThatMovesPixels()) { | 398 if (layer->background_filters().HasFilterThatMovesPixels()) { |
380 ExpandDamageRectInsideRectWithFilters(target_damage_rect, | 399 ExpandDamageRectInsideRectWithFilters(target_damage_rect, |
381 surface_rect_in_target_space, | 400 surface_rect_in_target_space, |
382 layer->background_filters()); | 401 layer->background_filters()); |
383 } | 402 } |
384 } | 403 } |
385 | 404 |
386 } // namespace cc | 405 } // namespace cc |
OLD | NEW |