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), | |
25 next_rect_history_(new RectMap) {} | |
26 | 24 |
27 DamageTracker::~DamageTracker() {} | 25 DamageTracker::~DamageTracker() {} |
28 | 26 |
29 static inline void ExpandRectWithFilters( | 27 static inline void ExpandRectWithFilters( |
30 gfx::RectF* rect, const FilterOperations& filters) { | 28 gfx::RectF* rect, const FilterOperations& filters) { |
31 int top, right, bottom, left; | 29 int top, right, bottom, left; |
32 filters.GetOutsets(&top, &right, &bottom, &left); | 30 filters.GetOutsets(&top, &right, &bottom, &left); |
33 rect->Inset(-left, -top, -right, -bottom); | 31 rect->Inset(-left, -top, -right, -bottom); |
34 } | 32 } |
35 | 33 |
(...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 | 96 // - 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 | 97 // 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 | 98 // 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, | 99 // 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 | 100 // regardless of how much it overlaps with the layer's new bounds, which |
103 // also need to be entirely redrawn. | 101 // also need to be entirely redrawn. |
104 // | 102 // |
105 // - See comments in the rest of the code to see what exactly is considered a | 103 // - See comments in the rest of the code to see what exactly is considered a |
106 // "change" in a layer/surface. | 104 // "change" in a layer/surface. |
107 // | 105 // |
108 // - To correctly manage exposed rects, two RectMaps are maintained: | 106 // - To correctly manage exposed rects, two RectMaps are maintained: |
shawnsingh
2013/11/21 09:56:03
"two RectMaps" -- this line of comment should also
ostap
2013/11/21 17:06:23
Done.
| |
109 // | 107 // |
110 // 1. The "current" map contains all the layer bounds that contributed to | 108 // 1. All existing from the previous fram regions in map history are |
shawnsingh
2013/11/21 09:56:03
Some typos and clarifications - let's say "All exi
ostap
2013/11/21 17:06:23
Done.
| |
109 // marked not updated. | |
110 // 2. The map contains all the layer bounds that contributed to | |
111 // the previous frame (even outside the previous damaged area). If a | 111 // the previous frame (even outside the previous damaged area). If a |
112 // layer changes or does not exist anymore, those regions are then | 112 // layer changes or does not exist anymore, those regions are then |
113 // exposed and damage the target surface. As the algorithm progresses, | 113 // exposed and damage the target surface. As the algorithm progresses, |
114 // entries are removed from the map until it has only leftover layers | 114 // entries are updated in the map until only leftover layers |
115 // that no longer exist. | 115 // that no longer exist stay marked not updated. |
116 // | 116 // |
117 // 2. The "next" map starts out empty, and as the algorithm progresses, | 117 // 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 | 118 // in a map are used to compute are damaged by deleted layers and |
119 // map. | 119 // 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 // | 120 // |
124 | 121 |
122 PrepareRectHistoryForUpdate(); | |
125 // These functions cannot be bypassed with early-exits, even if we know what | 123 // 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 | 124 // the damage will be for this frame, because we need to update the damage |
127 // tracker state to correctly track the next frame. | 125 // tracker state to correctly track the next frame. |
128 gfx::RectF damage_from_active_layers = | 126 gfx::RectF damage_from_active_layers = |
129 TrackDamageFromActiveLayers(layer_list, target_surface_layer_id); | 127 TrackDamageFromActiveLayers(layer_list, target_surface_layer_id); |
130 gfx::RectF damage_from_surface_mask = | 128 gfx::RectF damage_from_surface_mask = |
131 TrackDamageFromSurfaceMask(target_surface_mask_layer); | 129 TrackDamageFromSurfaceMask(target_surface_mask_layer); |
132 gfx::RectF damage_from_leftover_rects = TrackDamageFromLeftoverRects(); | 130 gfx::RectF damage_from_leftover_rects = TrackDamageFromLeftoverRects(); |
133 | 131 |
134 gfx::RectF damage_rect_for_this_update; | 132 gfx::RectF damage_rect_for_this_update; |
(...skipping 12 matching lines...) Expand all Loading... | |
147 // those here to limit damage. | 145 // those here to limit damage. |
148 damage_rect_for_this_update = target_surface_content_rect; | 146 damage_rect_for_this_update = target_surface_content_rect; |
149 } else if (filters.HasFilterThatMovesPixels()) { | 147 } else if (filters.HasFilterThatMovesPixels()) { |
150 ExpandRectWithFilters(&damage_rect_for_this_update, filters); | 148 ExpandRectWithFilters(&damage_rect_for_this_update, filters); |
151 } | 149 } |
152 } | 150 } |
153 | 151 |
154 // Damage accumulates until we are notified that we actually did draw on that | 152 // Damage accumulates until we are notified that we actually did draw on that |
155 // frame. | 153 // frame. |
156 current_damage_rect_.Union(damage_rect_for_this_update); | 154 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 } | 155 } |
163 | 156 |
164 gfx::RectF DamageTracker::RemoveRectFromCurrentFrame(int layer_id, | 157 DamageTracker::RectMapData& DamageTracker::RectDataForLayer( |
165 bool* layer_is_new) { | 158 int layer_id, bool* layer_is_new) { |
166 RectMap::iterator iter = current_rect_history_->find(layer_id); | 159 RectMap::iterator iter = rect_history_.find(layer_id); |
167 *layer_is_new = iter == current_rect_history_->end(); | 160 *layer_is_new = iter == rect_history_.end(); |
shawnsingh
2013/11/21 09:56:03
For clarity here let's use parentheses around the
ostap
2013/11/21 17:06:23
Done.
| |
168 if (*layer_is_new) | 161 if (*layer_is_new) |
169 return gfx::RectF(); | 162 return rect_history_[layer_id]; // force element creation |
170 | 163 |
171 gfx::RectF ret = iter->second; | 164 return iter->second; |
172 current_rect_history_->erase(iter); | |
173 return ret; | |
174 } | |
175 | |
176 void DamageTracker::SaveRectForNextFrame(int layer_id, | |
177 const gfx::RectF& target_space_rect) { | |
178 // This layer should not yet exist in next frame's history. | |
179 DCHECK_GT(layer_id, 0); | |
180 DCHECK(next_rect_history_->find(layer_id) == next_rect_history_->end()); | |
181 (*next_rect_history_)[layer_id] = target_space_rect; | |
182 } | 165 } |
183 | 166 |
184 gfx::RectF DamageTracker::TrackDamageFromActiveLayers( | 167 gfx::RectF DamageTracker::TrackDamageFromActiveLayers( |
185 const LayerImplList& layer_list, | 168 const LayerImplList& layer_list, |
186 int target_surface_layer_id) { | 169 int target_surface_layer_id) { |
187 gfx::RectF damage_rect = gfx::RectF(); | 170 gfx::RectF damage_rect = gfx::RectF(); |
188 | 171 |
189 for (size_t layer_index = 0; layer_index < layer_list.size(); ++layer_index) { | 172 for (size_t layer_index = 0; layer_index < layer_list.size(); ++layer_index) { |
190 // Visit layers in back-to-front order. | 173 // Visit layers in back-to-front order. |
191 LayerImpl* layer = layer_list[layer_index]; | 174 LayerImpl* layer = layer_list[layer_index]; |
(...skipping 26 matching lines...) Expand all Loading... | |
218 // expected to be a common case. | 201 // expected to be a common case. |
219 if (target_surface_mask_layer->LayerPropertyChanged() || | 202 if (target_surface_mask_layer->LayerPropertyChanged() || |
220 !target_surface_mask_layer->update_rect().IsEmpty()) { | 203 !target_surface_mask_layer->update_rect().IsEmpty()) { |
221 damage_rect = gfx::RectF(gfx::PointF(), | 204 damage_rect = gfx::RectF(gfx::PointF(), |
222 target_surface_mask_layer->bounds()); | 205 target_surface_mask_layer->bounds()); |
223 } | 206 } |
224 | 207 |
225 return damage_rect; | 208 return damage_rect; |
226 } | 209 } |
227 | 210 |
211 void DamageTracker::PrepareRectHistoryForUpdate() { | |
shawnsingh
2013/11/21 09:56:03
Let's use an integer RectMapData::mailboxId_ inst
| |
212 for (RectMap::iterator it = rect_history_.begin(); | |
213 it != rect_history_.end(); | |
214 ++it) { | |
215 it->second.updated_ = false; | |
216 } | |
217 } | |
218 | |
228 gfx::RectF DamageTracker::TrackDamageFromLeftoverRects() { | 219 gfx::RectF DamageTracker::TrackDamageFromLeftoverRects() { |
229 // After computing damage for all active layers, any leftover items in the | 220 // After computing damage for all active layers, any leftover items in the |
230 // current rect history correspond to layers/surfaces that no longer exist. | 221 // current rect history correspond to layers/surfaces that no longer exist. |
231 // So, these regions are now exposed on the target surface. | 222 // So, these regions are now exposed on the target surface. |
232 | 223 |
233 gfx::RectF damage_rect = gfx::RectF(); | 224 gfx::RectF damage_rect = gfx::RectF(); |
234 | 225 |
235 for (RectMap::iterator it = current_rect_history_->begin(); | 226 for (RectMap::iterator it = rect_history_.begin(); |
236 it != current_rect_history_->end(); | 227 it != rect_history_.end(); ) { |
237 ++it) | |
238 damage_rect.Union(it->second); | |
239 | 228 |
240 current_rect_history_->clear(); | 229 if (it->second.updated_) |
danakj
2013/11/21 00:13:40
{} here, since else has them
ostap
2013/11/21 17:06:23
Done.
| |
230 ++it; | |
231 else { | |
232 damage_rect.Union(it->second.rect_); | |
233 RectMap::iterator erase_it = it; | |
234 ++it; | |
235 rect_history_.erase(erase_it); | |
shawnsingh
2013/11/21 09:56:03
I don't know about base::HashMap, but for std:: co
ostap
2013/11/21 17:06:23
Done.
| |
236 } | |
237 } | |
241 | 238 |
242 return damage_rect; | 239 return damage_rect; |
243 } | 240 } |
244 | 241 |
245 void DamageTracker::ExtendDamageForLayer(LayerImpl* layer, | 242 void DamageTracker::ExtendDamageForLayer(LayerImpl* layer, |
246 gfx::RectF* target_damage_rect) { | 243 gfx::RectF* target_damage_rect) { |
247 // There are two ways that a layer can damage a region of the target surface: | 244 // There are two ways that a layer can damage a region of the target surface: |
248 // 1. Property change (e.g. opacity, position, transforms): | 245 // 1. Property change (e.g. opacity, position, transforms): |
249 // - the entire region of the layer itself damages the surface. | 246 // - the entire region of the layer itself damages the surface. |
250 // - the old layer region also damages the surface, because this region | 247 // - the old layer region also damages the surface, because this region |
251 // is now exposed. | 248 // is now exposed. |
252 // - note that in many cases the old and new layer rects may overlap, | 249 // - note that in many cases the old and new layer rects may overlap, |
253 // which is fine. | 250 // which is fine. |
254 // | 251 // |
255 // 2. Repaint/update: If a region of the layer that was repainted/updated, | 252 // 2. Repaint/update: If a region of the layer that was repainted/updated, |
256 // that region damages the surface. | 253 // that region damages the surface. |
257 // | 254 // |
258 // Property changes take priority over update rects. | 255 // Property changes take priority over update rects. |
259 // | 256 // |
260 // This method is called when we want to consider how a layer contributes to | 257 // 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 | 258 // its target RenderSurface, even if that layer owns the target RenderSurface |
262 // itself. To consider how a layer's target surface contributes to the | 259 // itself. To consider how a layer's target surface contributes to the |
263 // ancestor surface, ExtendDamageForRenderSurface() must be called instead. | 260 // ancestor surface, ExtendDamageForRenderSurface() must be called instead. |
264 | 261 |
265 bool layer_is_new = false; | 262 bool layer_is_new = false; |
266 gfx::RectF old_rect_in_target_space = | 263 RectMapData& data = RectDataForLayer(layer->id(), &layer_is_new); |
267 RemoveRectFromCurrentFrame(layer->id(), &layer_is_new); | 264 gfx::RectF old_rect_in_target_space = data.rect_; |
268 | 265 |
269 gfx::RectF rect_in_target_space = MathUtil::MapClippedRect( | 266 gfx::RectF rect_in_target_space = MathUtil::MapClippedRect( |
270 layer->draw_transform(), | 267 layer->draw_transform(), |
271 gfx::RectF(gfx::PointF(), layer->content_bounds())); | 268 gfx::RectF(gfx::PointF(), layer->content_bounds())); |
272 SaveRectForNextFrame(layer->id(), rect_in_target_space); | 269 data.Update(rect_in_target_space); |
273 | 270 |
274 if (layer_is_new || layer->LayerPropertyChanged()) { | 271 if (layer_is_new || layer->LayerPropertyChanged()) { |
275 // If a layer is new or has changed, then its entire layer rect affects the | 272 // If a layer is new or has changed, then its entire layer rect affects the |
276 // target surface. | 273 // target surface. |
277 target_damage_rect->Union(rect_in_target_space); | 274 target_damage_rect->Union(rect_in_target_space); |
278 | 275 |
279 // The layer's old region is now exposed on the target surface, too. | 276 // 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. | 277 // Note old_rect_in_target_space is already in target space. |
281 target_damage_rect->Union(old_rect_in_target_space); | 278 target_damage_rect->Union(old_rect_in_target_space); |
282 } else if (!layer->update_rect().IsEmpty()) { | 279 } 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 | 300 // - just like layers, both the old surface rect and new surface rect |
304 // will damage the target surface in this case. | 301 // will damage the target surface in this case. |
305 // | 302 // |
306 // 2. Damage rect: This surface may have been damaged by its own layer_list | 303 // 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. | 304 // as well, and that damage should propagate to the target surface. |
308 // | 305 // |
309 | 306 |
310 RenderSurfaceImpl* render_surface = layer->render_surface(); | 307 RenderSurfaceImpl* render_surface = layer->render_surface(); |
311 | 308 |
312 bool surface_is_new = false; | 309 bool surface_is_new = false; |
313 gfx::RectF old_surface_rect = RemoveRectFromCurrentFrame(layer->id(), | 310 RectMapData& data = RectDataForLayer(layer->id(), &surface_is_new); |
314 &surface_is_new); | 311 gfx::RectF old_surface_rect = data.rect_; |
315 | 312 |
316 // The drawableContextRect() already includes the replica if it exists. | 313 // The drawableContextRect() already includes the replica if it exists. |
317 gfx::RectF surface_rect_in_target_space = | 314 gfx::RectF surface_rect_in_target_space = |
318 render_surface->DrawableContentRect(); | 315 render_surface->DrawableContentRect(); |
319 SaveRectForNextFrame(layer->id(), surface_rect_in_target_space); | 316 data.Update(surface_rect_in_target_space); |
320 | 317 |
321 gfx::RectF damage_rect_in_local_space; | 318 gfx::RectF damage_rect_in_local_space; |
322 if (surface_is_new || render_surface->SurfacePropertyChanged()) { | 319 if (surface_is_new || render_surface->SurfacePropertyChanged()) { |
323 // The entire surface contributes damage. | 320 // The entire surface contributes damage. |
324 damage_rect_in_local_space = render_surface->content_rect(); | 321 damage_rect_in_local_space = render_surface->content_rect(); |
325 | 322 |
326 // The surface's old region is now exposed on the target surface, too. | 323 // The surface's old region is now exposed on the target surface, too. |
327 target_damage_rect->Union(old_surface_rect); | 324 target_damage_rect->Union(old_surface_rect); |
328 } else { | 325 } else { |
329 // Only the surface's damage_rect will damage the target surface. | 326 // 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)); | 343 replica_draw_transform, damage_rect_in_local_space)); |
347 } | 344 } |
348 } | 345 } |
349 | 346 |
350 // If there was damage on the replica's mask, then the target surface receives | 347 // If there was damage on the replica's mask, then the target surface receives |
351 // that damage as well. | 348 // that damage as well. |
352 if (layer->replica_layer() && layer->replica_layer()->mask_layer()) { | 349 if (layer->replica_layer() && layer->replica_layer()->mask_layer()) { |
353 LayerImpl* replica_mask_layer = layer->replica_layer()->mask_layer(); | 350 LayerImpl* replica_mask_layer = layer->replica_layer()->mask_layer(); |
354 | 351 |
355 bool replica_is_new = false; | 352 bool replica_is_new = false; |
356 RemoveRectFromCurrentFrame(replica_mask_layer->id(), &replica_is_new); | 353 RectMapData& data = RectDataForLayer(replica_mask_layer->id(), |
354 &replica_is_new); | |
danakj
2013/11/21 00:13:40
indenting off, git cl format please
ostap
2013/11/21 17:06:23
Done.
| |
357 | 355 |
358 const gfx::Transform& replica_draw_transform = | 356 const gfx::Transform& replica_draw_transform = |
359 render_surface->replica_draw_transform(); | 357 render_surface->replica_draw_transform(); |
360 gfx::RectF replica_mask_layer_rect = MathUtil::MapClippedRect( | 358 gfx::RectF replica_mask_layer_rect = MathUtil::MapClippedRect( |
361 replica_draw_transform, | 359 replica_draw_transform, |
362 gfx::RectF(gfx::PointF(), replica_mask_layer->bounds())); | 360 gfx::RectF(gfx::PointF(), replica_mask_layer->bounds())); |
363 SaveRectForNextFrame(replica_mask_layer->id(), replica_mask_layer_rect); | 361 data.Update(replica_mask_layer_rect); |
364 | 362 |
365 // In the current implementation, a change in the replica mask damages the | 363 // In the current implementation, a change in the replica mask damages the |
366 // entire replica region. | 364 // entire replica region. |
367 if (replica_is_new || | 365 if (replica_is_new || |
368 replica_mask_layer->LayerPropertyChanged() || | 366 replica_mask_layer->LayerPropertyChanged() || |
369 !replica_mask_layer->update_rect().IsEmpty()) | 367 !replica_mask_layer->update_rect().IsEmpty()) |
370 target_damage_rect->Union(replica_mask_layer_rect); | 368 target_damage_rect->Union(replica_mask_layer_rect); |
371 } | 369 } |
372 | 370 |
373 // If the layer has a background filter, this may cause pixels in our surface | 371 // 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 | 372 // 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 | 373 // 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 | 374 // 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 | 375 // 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. | 376 // used for the blur in this layer this frame. |
379 if (layer->background_filters().HasFilterThatMovesPixels()) { | 377 if (layer->background_filters().HasFilterThatMovesPixels()) { |
380 ExpandDamageRectInsideRectWithFilters(target_damage_rect, | 378 ExpandDamageRectInsideRectWithFilters(target_damage_rect, |
381 surface_rect_in_target_space, | 379 surface_rect_in_target_space, |
382 layer->background_filters()); | 380 layer->background_filters()); |
383 } | 381 } |
384 } | 382 } |
385 | 383 |
386 } // namespace cc | 384 } // namespace cc |
OLD | NEW |