Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / cc / resources / picture_pile.cc
blobd94ce7cf2187b535237031aec82d8c78e2f8081b
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/resources/picture_pile_impl.h"
13 #include "cc/resources/raster_worker_pool.h"
14 #include "skia/ext/analysis_canvas.h"
16 namespace {
17 // Layout pixel buffer around the visible layer rect to record. Any base
18 // picture that intersects the visible layer rect expanded by this distance
19 // will be recorded.
20 const int kPixelDistanceToRecord = 8000;
21 // We don't perform solid color analysis on images that have more than 10 skia
22 // operations.
23 const int kOpCountThatIsOkToAnalyze = 10;
25 // Dimensions of the tiles in this picture pile as well as the dimensions of
26 // the base picture in each tile.
27 const int kBasePictureSize = 512;
28 const int kTileGridBorderPixels = 1;
30 // Invalidation frequency settings. kInvalidationFrequencyThreshold is a value
31 // between 0 and 1 meaning invalidation frequency between 0% and 100% that
32 // indicates when to stop invalidating offscreen regions.
33 // kFrequentInvalidationDistanceThreshold defines what it means to be
34 // "offscreen" in terms of distance to visible in css pixels.
35 const float kInvalidationFrequencyThreshold = 0.75f;
36 const int kFrequentInvalidationDistanceThreshold = 512;
38 // TODO(humper): The density threshold here is somewhat arbitrary; need a
39 // way to set // this from the command line so we can write a benchmark
40 // script and find a sweet spot.
41 const float kDensityThreshold = 0.5f;
43 bool rect_sort_y(const gfx::Rect& r1, const gfx::Rect& r2) {
44 return r1.y() < r2.y() || (r1.y() == r2.y() && r1.x() < r2.x());
47 bool rect_sort_x(const gfx::Rect& r1, const gfx::Rect& r2) {
48 return r1.x() < r2.x() || (r1.x() == r2.x() && r1.y() < r2.y());
51 float PerformClustering(const std::vector<gfx::Rect>& tiles,
52 std::vector<gfx::Rect>* clustered_rects) {
53 // These variables track the record area and invalid area
54 // for the entire clustering
55 int total_record_area = 0;
56 int total_invalid_area = 0;
58 // These variables track the record area and invalid area
59 // for the current cluster being constructed.
60 gfx::Rect cur_record_rect;
61 int cluster_record_area = 0, cluster_invalid_area = 0;
63 for (std::vector<gfx::Rect>::const_iterator it = tiles.begin();
64 it != tiles.end();
65 it++) {
66 gfx::Rect invalid_tile = *it;
68 // For each tile, we consider adding the invalid tile to the
69 // current record rectangle. Only add it if the amount of empty
70 // space created is below a density threshold.
71 int tile_area = invalid_tile.width() * invalid_tile.height();
73 gfx::Rect proposed_union = cur_record_rect;
74 proposed_union.Union(invalid_tile);
75 int proposed_area = proposed_union.width() * proposed_union.height();
76 float proposed_density =
77 static_cast<float>(cluster_invalid_area + tile_area) /
78 static_cast<float>(proposed_area);
80 if (proposed_density >= kDensityThreshold) {
81 // It's okay to add this invalid tile to the
82 // current recording rectangle.
83 cur_record_rect = proposed_union;
84 cluster_record_area = proposed_area;
85 cluster_invalid_area += tile_area;
86 total_invalid_area += tile_area;
87 } else {
88 // Adding this invalid tile to the current recording rectangle
89 // would exceed our badness threshold, so put the current rectangle
90 // in the list of recording rects, and start a new one.
91 clustered_rects->push_back(cur_record_rect);
92 total_record_area += cluster_record_area;
93 cur_record_rect = invalid_tile;
94 cluster_invalid_area = tile_area;
95 cluster_record_area = tile_area;
99 DCHECK(!cur_record_rect.IsEmpty());
100 clustered_rects->push_back(cur_record_rect);
101 total_record_area += cluster_record_area;;
103 DCHECK_NE(total_record_area, 0);
105 return static_cast<float>(total_invalid_area) /
106 static_cast<float>(total_record_area);
109 void ClusterTiles(const std::vector<gfx::Rect>& invalid_tiles,
110 std::vector<gfx::Rect>* record_rects) {
111 TRACE_EVENT1("cc", "ClusterTiles",
112 "count",
113 invalid_tiles.size());
114 if (invalid_tiles.size() <= 1) {
115 // Quickly handle the special case for common
116 // single-invalidation update, and also the less common
117 // case of no tiles passed in.
118 *record_rects = invalid_tiles;
119 return;
122 // Sort the invalid tiles by y coordinate.
123 std::vector<gfx::Rect> invalid_tiles_vertical = invalid_tiles;
124 std::sort(invalid_tiles_vertical.begin(),
125 invalid_tiles_vertical.end(),
126 rect_sort_y);
128 std::vector<gfx::Rect> vertical_clustering;
129 float vertical_density =
130 PerformClustering(invalid_tiles_vertical, &vertical_clustering);
132 // If vertical density is optimal, then we can return early.
133 if (vertical_density == 1.f) {
134 *record_rects = vertical_clustering;
135 return;
138 // Now try again with a horizontal sort, see which one is best
139 std::vector<gfx::Rect> invalid_tiles_horizontal = invalid_tiles;
140 std::sort(invalid_tiles_horizontal.begin(),
141 invalid_tiles_horizontal.end(),
142 rect_sort_x);
144 std::vector<gfx::Rect> horizontal_clustering;
145 float horizontal_density =
146 PerformClustering(invalid_tiles_horizontal, &horizontal_clustering);
148 if (vertical_density < horizontal_density) {
149 *record_rects = horizontal_clustering;
150 return;
153 *record_rects = vertical_clustering;
156 } // namespace
158 namespace cc {
160 PicturePile::PicturePile()
161 : min_contents_scale_(0),
162 slow_down_raster_scale_factor_for_debug_(0),
163 can_use_lcd_text_(true),
164 has_any_recordings_(false),
165 is_solid_color_(false),
166 solid_color_(SK_ColorTRANSPARENT),
167 pixel_record_distance_(kPixelDistanceToRecord),
168 is_suitable_for_gpu_rasterization_(true) {
169 tiling_.SetMaxTextureSize(gfx::Size(kBasePictureSize, kBasePictureSize));
170 tile_grid_info_.fTileInterval.setEmpty();
171 tile_grid_info_.fMargin.setEmpty();
172 tile_grid_info_.fOffset.setZero();
175 PicturePile::~PicturePile() {
178 bool PicturePile::UpdateAndExpandInvalidation(
179 ContentLayerClient* painter,
180 Region* invalidation,
181 bool can_use_lcd_text,
182 const gfx::Size& layer_size,
183 const gfx::Rect& visible_layer_rect,
184 int frame_number,
185 Picture::RecordingMode recording_mode) {
186 bool can_use_lcd_text_changed = can_use_lcd_text_ != can_use_lcd_text;
187 can_use_lcd_text_ = can_use_lcd_text;
189 gfx::Rect interest_rect = visible_layer_rect;
190 interest_rect.Inset(-pixel_record_distance_, -pixel_record_distance_);
191 recorded_viewport_ = interest_rect;
192 recorded_viewport_.Intersect(gfx::Rect(layer_size));
194 bool updated =
195 ApplyInvalidationAndResize(interest_rect, invalidation, layer_size,
196 frame_number, can_use_lcd_text_changed);
197 std::vector<gfx::Rect> invalid_tiles;
198 GetInvalidTileRects(interest_rect, invalidation, visible_layer_rect,
199 frame_number, &invalid_tiles);
200 std::vector<gfx::Rect> record_rects;
201 ClusterTiles(invalid_tiles, &record_rects);
203 if (record_rects.empty())
204 return updated;
206 CreatePictures(painter, recording_mode, record_rects);
208 DetermineIfSolidColor();
210 has_any_recordings_ = true;
211 DCHECK(CanRasterSlowTileCheck(recorded_viewport_));
212 return true;
215 bool PicturePile::ApplyInvalidationAndResize(const gfx::Rect& interest_rect,
216 Region* invalidation,
217 const gfx::Size& layer_size,
218 int frame_number,
219 bool can_use_lcd_text_changed) {
220 bool updated = false;
222 Region synthetic_invalidation;
223 gfx::Size old_tiling_size = GetSize();
224 if (old_tiling_size != layer_size) {
225 tiling_.SetTilingSize(layer_size);
226 updated = true;
228 if (can_use_lcd_text_changed) {
229 // When LCD text is enabled/disabled, we must drop any raster tiles for
230 // the pile, so they can be recreated in a manner consistent with the new
231 // setting. We do this with |synthetic_invalidation| since we don't need to
232 // do a new recording, just invalidate rastered content.
233 synthetic_invalidation.Union(gfx::Rect(GetSize()));
234 updated = true;
237 gfx::Rect interest_rect_over_tiles =
238 tiling_.ExpandRectToTileBounds(interest_rect);
240 if (old_tiling_size != layer_size) {
241 gfx::Size min_tiling_size(
242 std::min(GetSize().width(), old_tiling_size.width()),
243 std::min(GetSize().height(), old_tiling_size.height()));
244 gfx::Size max_tiling_size(
245 std::max(GetSize().width(), old_tiling_size.width()),
246 std::max(GetSize().height(), old_tiling_size.height()));
248 has_any_recordings_ = false;
250 // Drop recordings that are outside the new or old layer bounds or that
251 // changed size. Newly exposed areas are considered invalidated.
252 // Previously exposed areas that are now outside of bounds also need to
253 // be invalidated, as they may become part of raster when scale < 1.
254 std::vector<PictureMapKey> to_erase;
255 int min_toss_x = tiling_.num_tiles_x();
256 if (max_tiling_size.width() > min_tiling_size.width()) {
257 min_toss_x =
258 tiling_.FirstBorderTileXIndexFromSrcCoord(min_tiling_size.width());
260 int min_toss_y = tiling_.num_tiles_y();
261 if (max_tiling_size.height() > min_tiling_size.height()) {
262 min_toss_y =
263 tiling_.FirstBorderTileYIndexFromSrcCoord(min_tiling_size.height());
265 for (const auto& key_picture_pair : picture_map_) {
266 const PictureMapKey& key = key_picture_pair.first;
267 if (key.first < min_toss_x && key.second < min_toss_y) {
268 has_any_recordings_ |= !!key_picture_pair.second.GetPicture();
269 continue;
271 to_erase.push_back(key);
274 for (size_t i = 0; i < to_erase.size(); ++i)
275 picture_map_.erase(to_erase[i]);
277 // If a recording is dropped and not re-recorded below, invalidate that
278 // full recording to cause any raster tiles that would use it to be
279 // dropped.
280 // If the recording will be replaced below, invalidate newly exposed
281 // areas and previously exposed areas to force raster tiles that include the
282 // old recording to know there is new recording to display.
283 gfx::Rect min_tiling_rect_over_tiles =
284 tiling_.ExpandRectToTileBounds(gfx::Rect(min_tiling_size));
285 if (min_toss_x < tiling_.num_tiles_x()) {
286 // The bounds which we want to invalidate are the tiles along the old
287 // edge of the pile when expanding, or the new edge of the pile when
288 // shrinking. In either case, it's the difference of the two, so we'll
289 // call this bounding box the DELTA EDGE RECT.
291 // In the picture below, the delta edge rect would be the bounding box of
292 // tiles {h,i,j}. |min_toss_x| would be equal to the horizontal index of
293 // the same tiles.
295 // min pile edge-v max pile edge-v
296 // ---------------+ - - - - - - - -+
297 // mmppssvvyybbeeh|h .
298 // mmppssvvyybbeeh|h .
299 // nnqqttwwzzccffi|i .
300 // nnqqttwwzzccffi|i .
301 // oorruuxxaaddggj|j .
302 // oorruuxxaaddggj|j .
303 // ---------------+ - - - - - - - -+ <- min pile edge
304 // .
305 // - - - - - - - - - - - - - - - -+ <- max pile edge
307 // If you were to slide a vertical beam from the left edge of the
308 // delta edge rect toward the right, it would either hit the right edge
309 // of the delta edge rect, or the interest rect (expanded to the bounds
310 // of the tiles it touches). The same is true for a beam parallel to
311 // any of the four edges, sliding across the delta edge rect. We use
312 // the union of these four rectangles generated by these beams to
313 // determine which part of the delta edge rect is outside of the expanded
314 // interest rect.
316 // Case 1: Intersect rect is outside the delta edge rect. It can be
317 // either on the left or the right. The |left_rect| and |right_rect|,
318 // cover this case, one will be empty and one will cover the full
319 // delta edge rect. In the picture below, |left_rect| would cover the
320 // delta edge rect, and |right_rect| would be empty.
321 // +----------------------+ |^^^^^^^^^^^^^^^|
322 // |===> DELTA EDGE RECT | | |
323 // |===> | | INTEREST RECT |
324 // |===> | | |
325 // |===> | | |
326 // +----------------------+ |vvvvvvvvvvvvvvv|
328 // Case 2: Interest rect is inside the delta edge rect. It will always
329 // fill the entire delta edge rect horizontally since the old edge rect
330 // is a single tile wide, and the interest rect has been expanded to the
331 // bounds of the tiles it touches. In this case the |left_rect| and
332 // |right_rect| will be empty, but the case is handled by the |top_rect|
333 // and |bottom_rect|. In the picture below, neither the |top_rect| nor
334 // |bottom_rect| would empty, they would each cover the area of the old
335 // edge rect outside the expanded interest rect.
336 // +-----------------+
337 // |:::::::::::::::::|
338 // |:::::::::::::::::|
339 // |vvvvvvvvvvvvvvvvv|
340 // | |
341 // +-----------------+
342 // | INTEREST RECT |
343 // | |
344 // +-----------------+
345 // | |
346 // | DELTA EDGE RECT |
347 // +-----------------+
349 // Lastly, we need to consider tiles inside the expanded interest rect.
350 // For those tiles, we want to invalidate exactly the newly exposed
351 // pixels. In the picture below the tiles in the delta edge rect have
352 // been resized and the area covered by periods must be invalidated. The
353 // |exposed_rect| will cover exactly that area.
354 // v-min pile edge
355 // +---------+-------+
356 // | ........|
357 // | ........|
358 // | DELTA EDGE.RECT.|
359 // | ........|
360 // | ........|
361 // | ........|
362 // | ........|
363 // | ........|
364 // | ........|
365 // +---------+-------+
367 int left = tiling_.TilePositionX(min_toss_x);
368 int right = left + tiling_.TileSizeX(min_toss_x);
369 int top = min_tiling_rect_over_tiles.y();
370 int bottom = min_tiling_rect_over_tiles.bottom();
372 int left_until = std::min(interest_rect_over_tiles.x(), right);
373 int right_until = std::max(interest_rect_over_tiles.right(), left);
374 int top_until = std::min(interest_rect_over_tiles.y(), bottom);
375 int bottom_until = std::max(interest_rect_over_tiles.bottom(), top);
377 int exposed_left = min_tiling_size.width();
378 int exposed_left_until = max_tiling_size.width();
379 int exposed_top = top;
380 int exposed_bottom = max_tiling_size.height();
381 DCHECK_GE(exposed_left, left);
383 gfx::Rect left_rect(left, top, left_until - left, bottom - top);
384 gfx::Rect right_rect(right_until, top, right - right_until, bottom - top);
385 gfx::Rect top_rect(left, top, right - left, top_until - top);
386 gfx::Rect bottom_rect(
387 left, bottom_until, right - left, bottom - bottom_until);
388 gfx::Rect exposed_rect(exposed_left,
389 exposed_top,
390 exposed_left_until - exposed_left,
391 exposed_bottom - exposed_top);
392 synthetic_invalidation.Union(left_rect);
393 synthetic_invalidation.Union(right_rect);
394 synthetic_invalidation.Union(top_rect);
395 synthetic_invalidation.Union(bottom_rect);
396 synthetic_invalidation.Union(exposed_rect);
398 if (min_toss_y < tiling_.num_tiles_y()) {
399 // The same thing occurs here as in the case above, but the invalidation
400 // rect is the bounding box around the bottom row of tiles in the min
401 // pile. This would be tiles {o,r,u,x,a,d,g,j} in the above picture.
403 int top = tiling_.TilePositionY(min_toss_y);
404 int bottom = top + tiling_.TileSizeY(min_toss_y);
405 int left = min_tiling_rect_over_tiles.x();
406 int right = min_tiling_rect_over_tiles.right();
408 int top_until = std::min(interest_rect_over_tiles.y(), bottom);
409 int bottom_until = std::max(interest_rect_over_tiles.bottom(), top);
410 int left_until = std::min(interest_rect_over_tiles.x(), right);
411 int right_until = std::max(interest_rect_over_tiles.right(), left);
413 int exposed_top = min_tiling_size.height();
414 int exposed_top_until = max_tiling_size.height();
415 int exposed_left = left;
416 int exposed_right = max_tiling_size.width();
417 DCHECK_GE(exposed_top, top);
419 gfx::Rect left_rect(left, top, left_until - left, bottom - top);
420 gfx::Rect right_rect(right_until, top, right - right_until, bottom - top);
421 gfx::Rect top_rect(left, top, right - left, top_until - top);
422 gfx::Rect bottom_rect(
423 left, bottom_until, right - left, bottom - bottom_until);
424 gfx::Rect exposed_rect(exposed_left,
425 exposed_top,
426 exposed_right - exposed_left,
427 exposed_top_until - exposed_top);
428 synthetic_invalidation.Union(left_rect);
429 synthetic_invalidation.Union(right_rect);
430 synthetic_invalidation.Union(top_rect);
431 synthetic_invalidation.Union(bottom_rect);
432 synthetic_invalidation.Union(exposed_rect);
436 // Detect cases where the full pile is invalidated, in this situation we
437 // can just drop/invalidate everything.
438 if (invalidation->Contains(gfx::Rect(old_tiling_size)) ||
439 invalidation->Contains(gfx::Rect(GetSize()))) {
440 for (auto& it : picture_map_)
441 updated = it.second.Invalidate(frame_number) || updated;
442 } else {
443 // Expand invalidation that is on tiles that aren't in the interest rect and
444 // will not be re-recorded below. These tiles are no longer valid and should
445 // be considerered fully invalid, so we can know to not keep around raster
446 // tiles that intersect with these recording tiles.
447 Region invalidation_expanded_to_full_tiles;
449 for (Region::Iterator i(*invalidation); i.has_rect(); i.next()) {
450 gfx::Rect invalid_rect = i.rect();
452 // This rect covers the bounds (excluding borders) of all tiles whose
453 // bounds (including borders) touch the |interest_rect|. This matches
454 // the iteration of the |invalid_rect| below which includes borders when
455 // calling Invalidate() on pictures.
456 gfx::Rect invalid_rect_outside_interest_rect_tiles =
457 tiling_.ExpandRectToTileBounds(invalid_rect);
458 // We subtract the |interest_rect_over_tiles| which represents the bounds
459 // of tiles that will be re-recorded below. This matches the iteration of
460 // |interest_rect| below which includes borders.
461 // TODO(danakj): We should have a Rect-subtract-Rect-to-2-rects operator
462 // instead of using Rect::Subtract which gives you the bounding box of the
463 // subtraction.
464 invalid_rect_outside_interest_rect_tiles.Subtract(
465 interest_rect_over_tiles);
466 invalidation_expanded_to_full_tiles.Union(
467 invalid_rect_outside_interest_rect_tiles);
469 // Split this inflated invalidation across tile boundaries and apply it
470 // to all tiles that it touches.
471 bool include_borders = true;
472 for (TilingData::Iterator iter(&tiling_, invalid_rect, include_borders);
473 iter;
474 ++iter) {
475 const PictureMapKey& key = iter.index();
477 PictureMap::iterator picture_it = picture_map_.find(key);
478 if (picture_it == picture_map_.end())
479 continue;
481 // Inform the grid cell that it has been invalidated in this frame.
482 updated = picture_it->second.Invalidate(frame_number) || updated;
483 // Invalidate drops the picture so the whole tile better be invalidated
484 // if it won't be re-recorded below.
485 DCHECK_IMPLIES(!tiling_.TileBounds(key.first, key.second)
486 .Intersects(interest_rect_over_tiles),
487 invalidation_expanded_to_full_tiles.Contains(
488 tiling_.TileBounds(key.first, key.second)));
491 invalidation->Union(invalidation_expanded_to_full_tiles);
494 invalidation->Union(synthetic_invalidation);
495 return updated;
498 void PicturePile::GetInvalidTileRects(const gfx::Rect& interest_rect,
499 Region* invalidation,
500 const gfx::Rect& visible_layer_rect,
501 int frame_number,
502 std::vector<gfx::Rect>* invalid_tiles) {
503 // Make a list of all invalid tiles; we will attempt to
504 // cluster these into multiple invalidation regions.
505 bool include_borders = true;
506 for (TilingData::Iterator it(&tiling_, interest_rect, include_borders); it;
507 ++it) {
508 const PictureMapKey& key = it.index();
509 PictureInfo& info = picture_map_[key];
511 gfx::Rect rect = PaddedRect(key);
512 int distance_to_visible =
513 rect.ManhattanInternalDistance(visible_layer_rect);
515 if (info.NeedsRecording(frame_number, distance_to_visible)) {
516 gfx::Rect tile = tiling_.TileBounds(key.first, key.second);
517 invalid_tiles->push_back(tile);
518 } else if (!info.GetPicture()) {
519 if (recorded_viewport_.Intersects(rect)) {
520 // Recorded viewport is just an optimization for a fully recorded
521 // interest rect. In this case, a tile in that rect has declined
522 // to be recorded (probably due to frequent invalidations).
523 // TODO(enne): Shrink the recorded_viewport_ rather than clearing.
524 recorded_viewport_ = gfx::Rect();
527 // If a tile in the interest rect is not recorded, the entire tile needs
528 // to be considered invalid, so that we know not to keep around raster
529 // tiles that intersect this recording tile.
530 invalidation->Union(tiling_.TileBounds(it.index_x(), it.index_y()));
535 void PicturePile::CreatePictures(ContentLayerClient* painter,
536 Picture::RecordingMode recording_mode,
537 const std::vector<gfx::Rect>& record_rects) {
538 for (const auto& record_rect : record_rects) {
539 gfx::Rect padded_record_rect = PadRect(record_rect);
541 int repeat_count = std::max(1, slow_down_raster_scale_factor_for_debug_);
542 scoped_refptr<Picture> picture;
544 // Note: Currently, gathering of pixel refs when using a single
545 // raster thread doesn't provide any benefit. This might change
546 // in the future but we avoid it for now to reduce the cost of
547 // Picture::Create.
548 bool gather_pixel_refs = RasterWorkerPool::GetNumRasterThreads() > 1;
550 for (int i = 0; i < repeat_count; i++) {
551 picture = Picture::Create(padded_record_rect, painter, tile_grid_info_,
552 gather_pixel_refs, recording_mode);
553 // Note the '&&' with previous is-suitable state.
554 // This means that once a picture-pile becomes unsuitable for gpu
555 // rasterization due to some content, it will continue to be unsuitable
556 // even if that content is replaced by gpu-friendly content.
557 // This is an optimization to avoid iterating though all pictures in
558 // the pile after each invalidation.
559 is_suitable_for_gpu_rasterization_ &=
560 picture->IsSuitableForGpuRasterization();
563 bool found_tile_for_recorded_picture = false;
565 bool include_borders = true;
566 for (TilingData::Iterator it(&tiling_, padded_record_rect, include_borders);
567 it; ++it) {
568 const PictureMapKey& key = it.index();
569 gfx::Rect tile = PaddedRect(key);
570 if (padded_record_rect.Contains(tile)) {
571 PictureInfo& info = picture_map_[key];
572 info.SetPicture(picture);
573 found_tile_for_recorded_picture = true;
576 DCHECK(found_tile_for_recorded_picture);
580 scoped_refptr<RasterSource> PicturePile::CreateRasterSource() const {
581 return scoped_refptr<RasterSource>(
582 PicturePileImpl::CreateFromPicturePile(this));
585 gfx::Size PicturePile::GetSize() const {
586 return tiling_.tiling_size();
589 void PicturePile::SetEmptyBounds() {
590 tiling_.SetTilingSize(gfx::Size());
591 Clear();
594 void PicturePile::SetMinContentsScale(float min_contents_scale) {
595 DCHECK(min_contents_scale);
596 if (min_contents_scale_ == min_contents_scale)
597 return;
599 // Picture contents are played back scaled. When the final contents scale is
600 // less than 1 (i.e. low res), then multiple recorded pixels will be used
601 // to raster one final pixel. To avoid splitting a final pixel across
602 // pictures (which would result in incorrect rasterization due to blending), a
603 // buffer margin is added so that any picture can be snapped to integral
604 // final pixels.
606 // For example, if a 1/4 contents scale is used, then that would be 3 buffer
607 // pixels, since that's the minimum number of pixels to add so that resulting
608 // content can be snapped to a four pixel aligned grid.
609 int buffer_pixels = static_cast<int>(ceil(1 / min_contents_scale) - 1);
610 buffer_pixels = std::max(0, buffer_pixels);
611 SetBufferPixels(buffer_pixels);
612 min_contents_scale_ = min_contents_scale;
615 void PicturePile::SetSlowdownRasterScaleFactor(int factor) {
616 slow_down_raster_scale_factor_for_debug_ = factor;
619 bool PicturePile::IsSuitableForGpuRasterization() const {
620 return is_suitable_for_gpu_rasterization_;
623 // static
624 void PicturePile::ComputeTileGridInfo(const gfx::Size& tile_grid_size,
625 SkTileGridFactory::TileGridInfo* info) {
626 DCHECK(info);
627 info->fTileInterval.set(tile_grid_size.width() - 2 * kTileGridBorderPixels,
628 tile_grid_size.height() - 2 * kTileGridBorderPixels);
629 DCHECK_GT(info->fTileInterval.width(), 0);
630 DCHECK_GT(info->fTileInterval.height(), 0);
631 info->fMargin.set(kTileGridBorderPixels, kTileGridBorderPixels);
632 // Offset the tile grid coordinate space to take into account the fact
633 // that the top-most and left-most tiles do not have top and left borders
634 // respectively.
635 info->fOffset.set(-kTileGridBorderPixels, -kTileGridBorderPixels);
638 void PicturePile::SetTileGridSize(const gfx::Size& tile_grid_size) {
639 ComputeTileGridInfo(tile_grid_size, &tile_grid_info_);
642 void PicturePile::SetUnsuitableForGpuRasterizationForTesting() {
643 is_suitable_for_gpu_rasterization_ = false;
646 SkTileGridFactory::TileGridInfo PicturePile::GetTileGridInfoForTesting() const {
647 return tile_grid_info_;
650 bool PicturePile::CanRasterSlowTileCheck(const gfx::Rect& layer_rect) const {
651 bool include_borders = false;
652 for (TilingData::Iterator tile_iter(&tiling_, layer_rect, include_borders);
653 tile_iter; ++tile_iter) {
654 PictureMap::const_iterator map_iter = picture_map_.find(tile_iter.index());
655 if (map_iter == picture_map_.end())
656 return false;
657 if (!map_iter->second.GetPicture())
658 return false;
660 return true;
663 void PicturePile::DetermineIfSolidColor() {
664 is_solid_color_ = false;
665 solid_color_ = SK_ColorTRANSPARENT;
667 if (picture_map_.empty()) {
668 return;
671 PictureMap::const_iterator it = picture_map_.begin();
672 const Picture* picture = it->second.GetPicture();
674 // Missing recordings due to frequent invalidations or being too far away
675 // from the interest rect will cause the a null picture to exist.
676 if (!picture)
677 return;
679 // Don't bother doing more work if the first image is too complicated.
680 if (picture->ApproximateOpCount() > kOpCountThatIsOkToAnalyze)
681 return;
683 // Make sure all of the mapped images point to the same picture.
684 for (++it; it != picture_map_.end(); ++it) {
685 if (it->second.GetPicture() != picture)
686 return;
688 skia::AnalysisCanvas canvas(recorded_viewport_.width(),
689 recorded_viewport_.height());
690 canvas.translate(-recorded_viewport_.x(), -recorded_viewport_.y());
691 picture->Raster(&canvas, nullptr, Region(), 1.0f);
692 is_solid_color_ = canvas.GetColorIfSolid(&solid_color_);
695 gfx::Rect PicturePile::PaddedRect(const PictureMapKey& key) const {
696 gfx::Rect tile = tiling_.TileBounds(key.first, key.second);
697 return PadRect(tile);
700 gfx::Rect PicturePile::PadRect(const gfx::Rect& rect) const {
701 gfx::Rect padded_rect = rect;
702 padded_rect.Inset(-buffer_pixels(), -buffer_pixels(), -buffer_pixels(),
703 -buffer_pixels());
704 return padded_rect;
707 void PicturePile::Clear() {
708 picture_map_.clear();
709 recorded_viewport_ = gfx::Rect();
710 has_any_recordings_ = false;
711 is_solid_color_ = false;
714 PicturePile::PictureInfo::PictureInfo() : last_frame_number_(0) {
717 PicturePile::PictureInfo::~PictureInfo() {
720 void PicturePile::PictureInfo::AdvanceInvalidationHistory(int frame_number) {
721 DCHECK_GE(frame_number, last_frame_number_);
722 if (frame_number == last_frame_number_)
723 return;
725 invalidation_history_ <<= (frame_number - last_frame_number_);
726 last_frame_number_ = frame_number;
729 bool PicturePile::PictureInfo::Invalidate(int frame_number) {
730 AdvanceInvalidationHistory(frame_number);
731 invalidation_history_.set(0);
733 bool did_invalidate = !!picture_.get();
734 picture_ = NULL;
735 return did_invalidate;
738 bool PicturePile::PictureInfo::NeedsRecording(int frame_number,
739 int distance_to_visible) {
740 AdvanceInvalidationHistory(frame_number);
742 // We only need recording if we don't have a picture. Furthermore, we only
743 // need a recording if we're within frequent invalidation distance threshold
744 // or the invalidation is not frequent enough (below invalidation frequency
745 // threshold).
746 return !picture_.get() &&
747 ((distance_to_visible <= kFrequentInvalidationDistanceThreshold) ||
748 (GetInvalidationFrequency() < kInvalidationFrequencyThreshold));
751 void PicturePile::SetBufferPixels(int new_buffer_pixels) {
752 if (new_buffer_pixels == buffer_pixels())
753 return;
755 Clear();
756 tiling_.SetBorderTexels(new_buffer_pixels);
759 void PicturePile::PictureInfo::SetPicture(scoped_refptr<Picture> picture) {
760 picture_ = picture;
763 const Picture* PicturePile::PictureInfo::GetPicture() const {
764 return picture_.get();
767 float PicturePile::PictureInfo::GetInvalidationFrequency() const {
768 return invalidation_history_.count() /
769 static_cast<float>(INVALIDATION_FRAMES_TRACKED);
772 } // namespace cc