Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(530)

Side by Side Diff: cc/trees/damage_tracker.cc

Issue 79173005: Improve DamageTracker performance. (Closed) Base URL: https://git.chromium.org/chromium/src.git@master
Patch Set: hash_map is replaced by sorted vector Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« cc/trees/damage_tracker.h ('K') | « cc/trees/damage_tracker.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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 = lower_bound(rect_history_.begin(),
shawnsingh 2013/11/23 01:32:01 let's explicitly write std::lower_bound here, and
ostap 2013/11/25 16:11:39 Done.
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
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 copy_pos;
shawnsingh 2013/11/23 01:32:01 Something doesn't seem right here, copy_pos could
ostap 2013/11/25 16:11:39 copy_pos is always initialized to rect_history_.be
230 SortedRectMap::iterator cur_pos = copy_pos = rect_history_.begin();
231 for ( ; cur_pos < rect_history_.end(); ++cur_pos) {
232 if (cur_pos->mailboxId_ == mailboxId_) {
233 if (cur_pos != copy_pos)
234 *copy_pos = *cur_pos;
234 235
235 for (RectMap::iterator it = current_rect_history_->begin(); 236 ++copy_pos;
236 it != current_rect_history_->end(); 237 } else {
237 ++it) 238 damage_rect.Union(cur_pos->rect_);
238 damage_rect.Union(it->second); 239 }
240 }
239 241
240 current_rect_history_->clear(); 242 if (copy_pos != rect_history_.end())
243 rect_history_.erase(copy_pos, rect_history_.end());
244
245 // If the vector has excessive storage is too big, shrink it
246 if (rect_history_.capacity() > rect_history_.size() * 4)
247 SortedRectMap(rect_history_).swap(rect_history_);
241 248
242 return damage_rect; 249 return damage_rect;
243 } 250 }
244 251
245 void DamageTracker::ExtendDamageForLayer(LayerImpl* layer, 252 void DamageTracker::ExtendDamageForLayer(LayerImpl* layer,
246 gfx::RectF* target_damage_rect) { 253 gfx::RectF* target_damage_rect) {
247 // There are two ways that a layer can damage a region of the target surface: 254 // There are two ways that a layer can damage a region of the target surface:
248 // 1. Property change (e.g. opacity, position, transforms): 255 // 1. Property change (e.g. opacity, position, transforms):
249 // - the entire region of the layer itself damages the surface. 256 // - the entire region of the layer itself damages the surface.
250 // - the old layer region also damages the surface, because this region 257 // - the old layer region also damages the surface, because this region
251 // is now exposed. 258 // is now exposed.
252 // - note that in many cases the old and new layer rects may overlap, 259 // - note that in many cases the old and new layer rects may overlap,
253 // which is fine. 260 // which is fine.
254 // 261 //
255 // 2. Repaint/update: If a region of the layer that was repainted/updated, 262 // 2. Repaint/update: If a region of the layer that was repainted/updated,
256 // that region damages the surface. 263 // that region damages the surface.
257 // 264 //
258 // Property changes take priority over update rects. 265 // Property changes take priority over update rects.
259 // 266 //
260 // This method is called when we want to consider how a layer contributes to 267 // 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 268 // its target RenderSurface, even if that layer owns the target RenderSurface
262 // itself. To consider how a layer's target surface contributes to the 269 // itself. To consider how a layer's target surface contributes to the
263 // ancestor surface, ExtendDamageForRenderSurface() must be called instead. 270 // ancestor surface, ExtendDamageForRenderSurface() must be called instead.
264 271
265 bool layer_is_new = false; 272 bool layer_is_new = false;
266 gfx::RectF old_rect_in_target_space = 273 RectMapData& data = RectDataForLayer(layer->id(), &layer_is_new);
267 RemoveRectFromCurrentFrame(layer->id(), &layer_is_new); 274 gfx::RectF old_rect_in_target_space = data.rect_;
268 275
269 gfx::RectF rect_in_target_space = MathUtil::MapClippedRect( 276 gfx::RectF rect_in_target_space = MathUtil::MapClippedRect(
270 layer->draw_transform(), 277 layer->draw_transform(),
271 gfx::RectF(gfx::PointF(), layer->content_bounds())); 278 gfx::RectF(gfx::PointF(), layer->content_bounds()));
272 SaveRectForNextFrame(layer->id(), rect_in_target_space); 279 data.Update(rect_in_target_space, mailboxId_);
273 280
274 if (layer_is_new || layer->LayerPropertyChanged()) { 281 if (layer_is_new || layer->LayerPropertyChanged()) {
275 // If a layer is new or has changed, then its entire layer rect affects the 282 // If a layer is new or has changed, then its entire layer rect affects the
276 // target surface. 283 // target surface.
277 target_damage_rect->Union(rect_in_target_space); 284 target_damage_rect->Union(rect_in_target_space);
278 285
279 // The layer's old region is now exposed on the target surface, too. 286 // 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. 287 // Note old_rect_in_target_space is already in target space.
281 target_damage_rect->Union(old_rect_in_target_space); 288 target_damage_rect->Union(old_rect_in_target_space);
282 } else if (!layer->update_rect().IsEmpty()) { 289 } else if (!layer->update_rect().IsEmpty()) {
(...skipping 20 matching lines...) Expand all
303 // - just like layers, both the old surface rect and new surface rect 310 // - just like layers, both the old surface rect and new surface rect
304 // will damage the target surface in this case. 311 // will damage the target surface in this case.
305 // 312 //
306 // 2. Damage rect: This surface may have been damaged by its own layer_list 313 // 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. 314 // as well, and that damage should propagate to the target surface.
308 // 315 //
309 316
310 RenderSurfaceImpl* render_surface = layer->render_surface(); 317 RenderSurfaceImpl* render_surface = layer->render_surface();
311 318
312 bool surface_is_new = false; 319 bool surface_is_new = false;
313 gfx::RectF old_surface_rect = RemoveRectFromCurrentFrame(layer->id(), 320 RectMapData& data = RectDataForLayer(layer->id(), &surface_is_new);
314 &surface_is_new); 321 gfx::RectF old_surface_rect = data.rect_;
315 322
316 // The drawableContextRect() already includes the replica if it exists. 323 // The drawableContextRect() already includes the replica if it exists.
317 gfx::RectF surface_rect_in_target_space = 324 gfx::RectF surface_rect_in_target_space =
318 render_surface->DrawableContentRect(); 325 render_surface->DrawableContentRect();
319 SaveRectForNextFrame(layer->id(), surface_rect_in_target_space); 326 data.Update(surface_rect_in_target_space, mailboxId_);
320 327
321 gfx::RectF damage_rect_in_local_space; 328 gfx::RectF damage_rect_in_local_space;
322 if (surface_is_new || render_surface->SurfacePropertyChanged()) { 329 if (surface_is_new || render_surface->SurfacePropertyChanged()) {
323 // The entire surface contributes damage. 330 // The entire surface contributes damage.
324 damage_rect_in_local_space = render_surface->content_rect(); 331 damage_rect_in_local_space = render_surface->content_rect();
325 332
326 // The surface's old region is now exposed on the target surface, too. 333 // The surface's old region is now exposed on the target surface, too.
327 target_damage_rect->Union(old_surface_rect); 334 target_damage_rect->Union(old_surface_rect);
328 } else { 335 } else {
329 // Only the surface's damage_rect will damage the target surface. 336 // Only the surface's damage_rect will damage the target surface.
(...skipping 16 matching lines...) Expand all
346 replica_draw_transform, damage_rect_in_local_space)); 353 replica_draw_transform, damage_rect_in_local_space));
347 } 354 }
348 } 355 }
349 356
350 // If there was damage on the replica's mask, then the target surface receives 357 // If there was damage on the replica's mask, then the target surface receives
351 // that damage as well. 358 // that damage as well.
352 if (layer->replica_layer() && layer->replica_layer()->mask_layer()) { 359 if (layer->replica_layer() && layer->replica_layer()->mask_layer()) {
353 LayerImpl* replica_mask_layer = layer->replica_layer()->mask_layer(); 360 LayerImpl* replica_mask_layer = layer->replica_layer()->mask_layer();
354 361
355 bool replica_is_new = false; 362 bool replica_is_new = false;
356 RemoveRectFromCurrentFrame(replica_mask_layer->id(), &replica_is_new); 363 RectMapData& data =
364 RectDataForLayer(replica_mask_layer->id(), &replica_is_new);
357 365
358 const gfx::Transform& replica_draw_transform = 366 const gfx::Transform& replica_draw_transform =
359 render_surface->replica_draw_transform(); 367 render_surface->replica_draw_transform();
360 gfx::RectF replica_mask_layer_rect = MathUtil::MapClippedRect( 368 gfx::RectF replica_mask_layer_rect = MathUtil::MapClippedRect(
361 replica_draw_transform, 369 replica_draw_transform,
362 gfx::RectF(gfx::PointF(), replica_mask_layer->bounds())); 370 gfx::RectF(gfx::PointF(), replica_mask_layer->bounds()));
363 SaveRectForNextFrame(replica_mask_layer->id(), replica_mask_layer_rect); 371 data.Update(replica_mask_layer_rect, mailboxId_);
364 372
365 // In the current implementation, a change in the replica mask damages the 373 // In the current implementation, a change in the replica mask damages the
366 // entire replica region. 374 // entire replica region.
367 if (replica_is_new || 375 if (replica_is_new ||
368 replica_mask_layer->LayerPropertyChanged() || 376 replica_mask_layer->LayerPropertyChanged() ||
369 !replica_mask_layer->update_rect().IsEmpty()) 377 !replica_mask_layer->update_rect().IsEmpty())
370 target_damage_rect->Union(replica_mask_layer_rect); 378 target_damage_rect->Union(replica_mask_layer_rect);
371 } 379 }
372 380
373 // If the layer has a background filter, this may cause pixels in our surface 381 // 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 382 // 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 383 // 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 384 // 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 385 // 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. 386 // used for the blur in this layer this frame.
379 if (layer->background_filters().HasFilterThatMovesPixels()) { 387 if (layer->background_filters().HasFilterThatMovesPixels()) {
380 ExpandDamageRectInsideRectWithFilters(target_damage_rect, 388 ExpandDamageRectInsideRectWithFilters(target_damage_rect,
381 surface_rect_in_target_space, 389 surface_rect_in_target_space,
382 layer->background_filters()); 390 layer->background_filters());
383 } 391 }
384 } 392 }
385 393
386 } // namespace cc 394 } // namespace cc
OLDNEW
« cc/trees/damage_tracker.h ('K') | « cc/trees/damage_tracker.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698