Task Manager: Remove goat teleporter.
[chromium-blink-merge.git] / cc / resources / picture_pile.cc
blob0fd3b2a0d2f11de9311c9f8048ebc3084dd95cf3
1 // Copyright 2012 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/resources/picture_pile.h"
7 #include <algorithm>
8 #include <limits>
9 #include <vector>
11 #include "cc/base/region.h"
12 #include "cc/debug/rendering_stats_instrumentation.h"
13 #include "cc/resources/picture_pile_impl.h"
14 #include "cc/resources/raster_worker_pool.h"
15 #include "cc/resources/tile_priority.h"
17 namespace {
18 // Layout pixel buffer around the visible layer rect to record. Any base
19 // picture that intersects the visible layer rect expanded by this distance
20 // will be recorded.
21 const int kPixelDistanceToRecord = 8000;
22 // We don't perform solid color analysis on images that have more than 10 skia
23 // operations.
24 const int kOpCountThatIsOkToAnalyze = 10;
26 // TODO(humper): The density threshold here is somewhat arbitrary; need a
27 // way to set // this from the command line so we can write a benchmark
28 // script and find a sweet spot.
29 const float kDensityThreshold = 0.5f;
31 bool rect_sort_y(const gfx::Rect& r1, const gfx::Rect& r2) {
32 return r1.y() < r2.y() || (r1.y() == r2.y() && r1.x() < r2.x());
35 bool rect_sort_x(const gfx::Rect& r1, const gfx::Rect& r2) {
36 return r1.x() < r2.x() || (r1.x() == r2.x() && r1.y() < r2.y());
39 float PerformClustering(const std::vector<gfx::Rect>& tiles,
40 std::vector<gfx::Rect>* clustered_rects) {
41 // These variables track the record area and invalid area
42 // for the entire clustering
43 int total_record_area = 0;
44 int total_invalid_area = 0;
46 // These variables track the record area and invalid area
47 // for the current cluster being constructed.
48 gfx::Rect cur_record_rect;
49 int cluster_record_area = 0, cluster_invalid_area = 0;
51 for (std::vector<gfx::Rect>::const_iterator it = tiles.begin();
52 it != tiles.end();
53 it++) {
54 gfx::Rect invalid_tile = *it;
56 // For each tile, we consider adding the invalid tile to the
57 // current record rectangle. Only add it if the amount of empty
58 // space created is below a density threshold.
59 int tile_area = invalid_tile.width() * invalid_tile.height();
61 gfx::Rect proposed_union = cur_record_rect;
62 proposed_union.Union(invalid_tile);
63 int proposed_area = proposed_union.width() * proposed_union.height();
64 float proposed_density =
65 static_cast<float>(cluster_invalid_area + tile_area) /
66 static_cast<float>(proposed_area);
68 if (proposed_density >= kDensityThreshold) {
69 // It's okay to add this invalid tile to the
70 // current recording rectangle.
71 cur_record_rect = proposed_union;
72 cluster_record_area = proposed_area;
73 cluster_invalid_area += tile_area;
74 total_invalid_area += tile_area;
75 } else {
76 // Adding this invalid tile to the current recording rectangle
77 // would exceed our badness threshold, so put the current rectangle
78 // in the list of recording rects, and start a new one.
79 clustered_rects->push_back(cur_record_rect);
80 total_record_area += cluster_record_area;
81 cur_record_rect = invalid_tile;
82 cluster_invalid_area = tile_area;
83 cluster_record_area = tile_area;
87 DCHECK(!cur_record_rect.IsEmpty());
88 clustered_rects->push_back(cur_record_rect);
89 total_record_area += cluster_record_area;;
91 DCHECK_NE(total_record_area, 0);
93 return static_cast<float>(total_invalid_area) /
94 static_cast<float>(total_record_area);
97 float ClusterTiles(const std::vector<gfx::Rect>& invalid_tiles,
98 std::vector<gfx::Rect>* record_rects) {
99 TRACE_EVENT1("cc", "ClusterTiles",
100 "count",
101 invalid_tiles.size());
103 if (invalid_tiles.size() <= 1) {
104 // Quickly handle the special case for common
105 // single-invalidation update, and also the less common
106 // case of no tiles passed in.
107 *record_rects = invalid_tiles;
108 return 1;
111 // Sort the invalid tiles by y coordinate.
112 std::vector<gfx::Rect> invalid_tiles_vertical = invalid_tiles;
113 std::sort(invalid_tiles_vertical.begin(),
114 invalid_tiles_vertical.end(),
115 rect_sort_y);
117 float vertical_density;
118 std::vector<gfx::Rect> vertical_clustering;
119 vertical_density = PerformClustering(invalid_tiles_vertical,
120 &vertical_clustering);
122 // If vertical density is optimal, then we can return early.
123 if (vertical_density == 1.f) {
124 *record_rects = vertical_clustering;
125 return vertical_density;
128 // Now try again with a horizontal sort, see which one is best
129 std::vector<gfx::Rect> invalid_tiles_horizontal = invalid_tiles;
130 std::sort(invalid_tiles_horizontal.begin(),
131 invalid_tiles_horizontal.end(),
132 rect_sort_x);
134 float horizontal_density;
135 std::vector<gfx::Rect> horizontal_clustering;
136 horizontal_density = PerformClustering(invalid_tiles_horizontal,
137 &horizontal_clustering);
139 if (vertical_density < horizontal_density) {
140 *record_rects = horizontal_clustering;
141 return horizontal_density;
144 *record_rects = vertical_clustering;
145 return vertical_density;
148 } // namespace
150 namespace cc {
152 PicturePile::PicturePile()
153 : is_suitable_for_gpu_rasterization_(true),
154 is_solid_color_(true),
155 solid_color_(SK_ColorTRANSPARENT) {
158 PicturePile::~PicturePile() {
161 bool PicturePile::UpdateAndExpandInvalidation(
162 ContentLayerClient* painter,
163 Region* invalidation,
164 SkColor background_color,
165 bool contents_opaque,
166 bool contents_fill_bounds_completely,
167 const gfx::Size& layer_size,
168 const gfx::Rect& visible_layer_rect,
169 int frame_number,
170 Picture::RecordingMode recording_mode,
171 RenderingStatsInstrumentation* stats_instrumentation) {
172 background_color_ = background_color;
173 contents_opaque_ = contents_opaque;
174 contents_fill_bounds_completely_ = contents_fill_bounds_completely;
176 bool updated = false;
178 Region resize_invalidation;
179 gfx::Size old_tiling_size = tiling_size();
180 if (old_tiling_size != layer_size) {
181 tiling_.SetTilingSize(layer_size);
182 updated = true;
185 gfx::Rect interest_rect = visible_layer_rect;
186 interest_rect.Inset(
187 -kPixelDistanceToRecord,
188 -kPixelDistanceToRecord,
189 -kPixelDistanceToRecord,
190 -kPixelDistanceToRecord);
191 recorded_viewport_ = interest_rect;
192 recorded_viewport_.Intersect(gfx::Rect(tiling_size()));
194 gfx::Rect interest_rect_over_tiles =
195 tiling_.ExpandRectToTileBounds(interest_rect);
197 if (old_tiling_size != layer_size) {
198 has_any_recordings_ = false;
200 // Drop recordings that are outside the new layer bounds or that changed
201 // size.
202 std::vector<PictureMapKey> to_erase;
203 int min_toss_x = tiling_.num_tiles_x();
204 if (tiling_size().width() > old_tiling_size.width()) {
205 min_toss_x =
206 tiling_.FirstBorderTileXIndexFromSrcCoord(old_tiling_size.width());
208 int min_toss_y = tiling_.num_tiles_y();
209 if (tiling_size().height() > old_tiling_size.height()) {
210 min_toss_y =
211 tiling_.FirstBorderTileYIndexFromSrcCoord(old_tiling_size.height());
213 for (PictureMap::const_iterator it = picture_map_.begin();
214 it != picture_map_.end();
215 ++it) {
216 const PictureMapKey& key = it->first;
217 if (key.first < min_toss_x && key.second < min_toss_y) {
218 has_any_recordings_ |= !!it->second.GetPicture();
219 continue;
221 to_erase.push_back(key);
224 for (size_t i = 0; i < to_erase.size(); ++i)
225 picture_map_.erase(to_erase[i]);
227 // If a recording is dropped and not re-recorded below, invalidate that
228 // full recording to cause any raster tiles that would use it to be
229 // dropped.
230 // If the recording will be replaced below, just invalidate newly exposed
231 // areas to force raster tiles that include the old recording to know
232 // there is new recording to display.
233 gfx::Rect old_tiling_rect_over_tiles =
234 tiling_.ExpandRectToTileBounds(gfx::Rect(old_tiling_size));
235 if (min_toss_x < tiling_.num_tiles_x()) {
236 // The bounds which we want to invalidate are the tiles along the old
237 // edge of the pile. We'll call this bounding box the OLD EDGE RECT.
239 // In the picture below, the old edge rect would be the bounding box
240 // of tiles {h,i,j}. |min_toss_x| would be equal to the horizontal index
241 // of the same tiles.
243 // old pile edge-v new pile edge-v
244 // ---------------+ - - - - - - - -+
245 // mmppssvvyybbeeh|h .
246 // mmppssvvyybbeeh|h .
247 // nnqqttwwzzccffi|i .
248 // nnqqttwwzzccffi|i .
249 // oorruuxxaaddggj|j .
250 // oorruuxxaaddggj|j .
251 // ---------------+ - - - - - - - -+ <- old pile edge
252 // .
253 // - - - - - - - - - - - - - - - -+ <- new pile edge
255 // If you were to slide a vertical beam from the left edge of the
256 // old edge rect toward the right, it would either hit the right edge
257 // of the old edge rect, or the interest rect (expanded to the bounds
258 // of the tiles it touches). The same is true for a beam parallel to
259 // any of the four edges, sliding accross the old edge rect. We use
260 // the union of these four rectangles generated by these beams to
261 // determine which part of the old edge rect is outside of the expanded
262 // interest rect.
264 // Case 1: Intersect rect is outside the old edge rect. It can be
265 // either on the left or the right. The |left_rect| and |right_rect|,
266 // cover this case, one will be empty and one will cover the full
267 // old edge rect. In the picture below, |left_rect| would cover the
268 // old edge rect, and |right_rect| would be empty.
269 // +----------------------+ |^^^^^^^^^^^^^^^|
270 // |===> OLD EDGE RECT | | |
271 // |===> | | INTEREST RECT |
272 // |===> | | |
273 // |===> | | |
274 // +----------------------+ |vvvvvvvvvvvvvvv|
276 // Case 2: Interest rect is inside the old edge rect. It will always
277 // fill the entire old edge rect horizontally since the old edge rect
278 // is a single tile wide, and the interest rect has been expanded to the
279 // bounds of the tiles it touches. In this case the |left_rect| and
280 // |right_rect| will be empty, but the case is handled by the |top_rect|
281 // and |bottom_rect|. In the picture below, neither the |top_rect| nor
282 // |bottom_rect| would empty, they would each cover the area of the old
283 // edge rect outside the expanded interest rect.
284 // +-----------------+
285 // |:::::::::::::::::|
286 // |:::::::::::::::::|
287 // |vvvvvvvvvvvvvvvvv|
288 // | |
289 // +-----------------+
290 // | INTEREST RECT |
291 // | |
292 // +-----------------+
293 // | |
294 // | OLD EDGE RECT |
295 // +-----------------+
297 // Lastly, we need to consider tiles inside the expanded interest rect.
298 // For those tiles, we want to invalidate exactly the newly exposed
299 // pixels. In the picture below the tiles in the old edge rect have been
300 // resized and the area covered by periods must be invalidated. The
301 // |exposed_rect| will cover exactly that area.
302 // v-old pile edge
303 // +---------+-------+
304 // | ........|
305 // | ........|
306 // | OLD EDGE.RECT..|
307 // | ........|
308 // | ........|
309 // | ........|
310 // | ........|
311 // | ........|
312 // | ........|
313 // +---------+-------+
315 int left = tiling_.TilePositionX(min_toss_x);
316 int right = left + tiling_.TileSizeX(min_toss_x);
317 int top = old_tiling_rect_over_tiles.y();
318 int bottom = old_tiling_rect_over_tiles.bottom();
320 int left_until = std::min(interest_rect_over_tiles.x(), right);
321 int right_until = std::max(interest_rect_over_tiles.right(), left);
322 int top_until = std::min(interest_rect_over_tiles.y(), bottom);
323 int bottom_until = std::max(interest_rect_over_tiles.bottom(), top);
325 int exposed_left = old_tiling_size.width();
326 int exposed_left_until = right;
327 DCHECK_GE(exposed_left, left);
329 gfx::Rect left_rect(left, top, left_until - left, bottom - top);
330 gfx::Rect right_rect(right_until, top, right - right_until, bottom - top);
331 gfx::Rect top_rect(left, top, right - left, top_until - top);
332 gfx::Rect bottom_rect(
333 left, bottom_until, right - left, bottom - bottom_until);
334 gfx::Rect exposed_rect(
335 exposed_left, top, exposed_left_until - exposed_left, bottom - top);
336 resize_invalidation.Union(left_rect);
337 resize_invalidation.Union(right_rect);
338 resize_invalidation.Union(top_rect);
339 resize_invalidation.Union(bottom_rect);
340 resize_invalidation.Union(exposed_rect);
342 if (min_toss_y < tiling_.num_tiles_y()) {
343 // The same thing occurs here as in the case above, but the invalidation
344 // rect is the bounding box around the bottom row of tiles in the old
345 // pile. This would be tiles {o,r,u,x,a,d,g,j} in the above picture.
347 int top = tiling_.TilePositionY(min_toss_y);
348 int bottom = top + tiling_.TileSizeY(min_toss_y);
349 int left = old_tiling_rect_over_tiles.x();
350 int right = old_tiling_rect_over_tiles.right();
352 int top_until = std::min(interest_rect_over_tiles.y(), bottom);
353 int bottom_until = std::max(interest_rect_over_tiles.bottom(), top);
354 int left_until = std::min(interest_rect_over_tiles.x(), right);
355 int right_until = std::max(interest_rect_over_tiles.right(), left);
357 int exposed_top = old_tiling_size.height();
358 int exposed_top_until = bottom;
359 DCHECK_GE(exposed_top, top);
361 gfx::Rect left_rect(left, top, left_until - left, bottom - top);
362 gfx::Rect right_rect(right_until, top, right - right_until, bottom - top);
363 gfx::Rect top_rect(left, top, right - left, top_until - top);
364 gfx::Rect bottom_rect(
365 left, bottom_until, right - left, bottom - bottom_until);
366 gfx::Rect exposed_rect(
367 left, exposed_top, right - left, exposed_top_until - exposed_top);
368 resize_invalidation.Union(left_rect);
369 resize_invalidation.Union(right_rect);
370 resize_invalidation.Union(top_rect);
371 resize_invalidation.Union(bottom_rect);
372 resize_invalidation.Union(exposed_rect);
376 Region invalidation_expanded_to_full_tiles;
377 for (Region::Iterator i(*invalidation); i.has_rect(); i.next()) {
378 gfx::Rect invalid_rect = i.rect();
380 // Expand invalidation that is outside tiles that intersect the interest
381 // rect. These tiles are no longer valid and should be considerered fully
382 // invalid, so we can know to not keep around raster tiles that intersect
383 // with these recording tiles.
384 gfx::Rect invalid_rect_outside_interest_rect_tiles = invalid_rect;
385 // TODO(danakj): We should have a Rect-subtract-Rect-to-2-rects operator
386 // instead of using Rect::Subtract which gives you the bounding box of the
387 // subtraction.
388 invalid_rect_outside_interest_rect_tiles.Subtract(interest_rect_over_tiles);
389 invalidation_expanded_to_full_tiles.Union(tiling_.ExpandRectToTileBounds(
390 invalid_rect_outside_interest_rect_tiles));
392 // Split this inflated invalidation across tile boundaries and apply it
393 // to all tiles that it touches.
394 bool include_borders = true;
395 for (TilingData::Iterator iter(&tiling_, invalid_rect, include_borders);
396 iter;
397 ++iter) {
398 const PictureMapKey& key = iter.index();
400 PictureMap::iterator picture_it = picture_map_.find(key);
401 if (picture_it == picture_map_.end())
402 continue;
404 // Inform the grid cell that it has been invalidated in this frame.
405 updated = picture_it->second.Invalidate(frame_number) || updated;
406 // Invalidate drops the picture so the whole tile better be invalidated if
407 // it won't be re-recorded below.
408 DCHECK(
409 tiling_.TileBounds(key.first, key.second).Intersects(interest_rect) ||
410 invalidation_expanded_to_full_tiles.Contains(
411 tiling_.TileBounds(key.first, key.second)));
415 invalidation->Union(invalidation_expanded_to_full_tiles);
416 invalidation->Union(resize_invalidation);
418 // Make a list of all invalid tiles; we will attempt to
419 // cluster these into multiple invalidation regions.
420 std::vector<gfx::Rect> invalid_tiles;
421 bool include_borders = true;
422 for (TilingData::Iterator it(&tiling_, interest_rect, include_borders); it;
423 ++it) {
424 const PictureMapKey& key = it.index();
425 PictureInfo& info = picture_map_[key];
427 gfx::Rect rect = PaddedRect(key);
428 int distance_to_visible =
429 rect.ManhattanInternalDistance(visible_layer_rect);
431 if (info.NeedsRecording(frame_number, distance_to_visible)) {
432 gfx::Rect tile = tiling_.TileBounds(key.first, key.second);
433 invalid_tiles.push_back(tile);
434 } else if (!info.GetPicture()) {
435 if (recorded_viewport_.Intersects(rect)) {
436 // Recorded viewport is just an optimization for a fully recorded
437 // interest rect. In this case, a tile in that rect has declined
438 // to be recorded (probably due to frequent invalidations).
439 // TODO(enne): Shrink the recorded_viewport_ rather than clearing.
440 recorded_viewport_ = gfx::Rect();
443 // If a tile in the interest rect is not recorded, the entire tile needs
444 // to be considered invalid, so that we know not to keep around raster
445 // tiles that intersect this recording tile.
446 invalidation->Union(tiling_.TileBounds(it.index_x(), it.index_y()));
450 std::vector<gfx::Rect> record_rects;
451 ClusterTiles(invalid_tiles, &record_rects);
453 if (record_rects.empty())
454 return updated;
456 for (std::vector<gfx::Rect>::iterator it = record_rects.begin();
457 it != record_rects.end();
458 it++) {
459 gfx::Rect record_rect = *it;
460 record_rect = PadRect(record_rect);
462 int repeat_count = std::max(1, slow_down_raster_scale_factor_for_debug_);
463 scoped_refptr<Picture> picture;
465 // Note: Currently, gathering of pixel refs when using a single
466 // raster thread doesn't provide any benefit. This might change
467 // in the future but we avoid it for now to reduce the cost of
468 // Picture::Create.
469 bool gather_pixel_refs = RasterWorkerPool::GetNumRasterThreads() > 1;
472 base::TimeDelta best_duration = base::TimeDelta::Max();
473 for (int i = 0; i < repeat_count; i++) {
474 base::TimeTicks start_time = stats_instrumentation->StartRecording();
475 picture = Picture::Create(record_rect,
476 painter,
477 tile_grid_info_,
478 gather_pixel_refs,
479 recording_mode);
480 // Note the '&&' with previous is-suitable state.
481 // This means that once a picture-pile becomes unsuitable for gpu
482 // rasterization due to some content, it will continue to be unsuitable
483 // even if that content is replaced by gpu-friendly content.
484 // This is an optimization to avoid iterating though all pictures in
485 // the pile after each invalidation.
486 is_suitable_for_gpu_rasterization_ &=
487 picture->IsSuitableForGpuRasterization();
488 has_text_ |= picture->HasText();
489 base::TimeDelta duration =
490 stats_instrumentation->EndRecording(start_time);
491 best_duration = std::min(duration, best_duration);
493 int recorded_pixel_count =
494 picture->LayerRect().width() * picture->LayerRect().height();
495 stats_instrumentation->AddRecord(best_duration, recorded_pixel_count);
498 bool found_tile_for_recorded_picture = false;
500 bool include_borders = true;
501 for (TilingData::Iterator it(&tiling_, record_rect, include_borders); it;
502 ++it) {
503 const PictureMapKey& key = it.index();
504 gfx::Rect tile = PaddedRect(key);
505 if (record_rect.Contains(tile)) {
506 PictureInfo& info = picture_map_[key];
507 info.SetPicture(picture);
508 found_tile_for_recorded_picture = true;
511 DetermineIfSolidColor();
512 DCHECK(found_tile_for_recorded_picture);
515 has_any_recordings_ = true;
516 DCHECK(CanRasterSlowTileCheck(recorded_viewport_));
517 return true;
520 void PicturePile::SetEmptyBounds() {
521 tiling_.SetTilingSize(gfx::Size());
522 picture_map_.clear();
523 has_any_recordings_ = false;
524 recorded_viewport_ = gfx::Rect();
527 void PicturePile::DetermineIfSolidColor() {
528 is_solid_color_ = false;
529 solid_color_ = SK_ColorTRANSPARENT;
531 if (picture_map_.empty()) {
532 return;
535 PictureMap::const_iterator it = picture_map_.begin();
536 const Picture* picture = it->second.GetPicture();
538 // Missing recordings due to frequent invalidations or being too far away
539 // from the interest rect will cause the a null picture to exist.
540 if (!picture)
541 return;
543 // Don't bother doing more work if the first image is too complicated.
544 if (picture->ApproximateOpCount() > kOpCountThatIsOkToAnalyze)
545 return;
547 // Make sure all of the mapped images point to the same picture.
548 for (++it; it != picture_map_.end(); ++it) {
549 if (it->second.GetPicture() != picture)
550 return;
552 skia::AnalysisCanvas canvas(recorded_viewport_.width(),
553 recorded_viewport_.height());
554 picture->Raster(&canvas, NULL, Region(), 1.0f);
555 is_solid_color_ = canvas.GetColorIfSolid(&solid_color_);
558 } // namespace cc