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"
11 #include "cc/base/region.h"
12 #include "cc/resources/picture_pile_impl.h"
13 #include "cc/resources/tile_task_worker_pool.h"
14 #include "skia/ext/analysis_canvas.h"
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
20 const int kPixelDistanceToRecord
= 8000;
21 // We don't perform solid color analysis on images that have more than 10 skia
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;
29 // Invalidation frequency settings. kInvalidationFrequencyThreshold is a value
30 // between 0 and 1 meaning invalidation frequency between 0% and 100% that
31 // indicates when to stop invalidating offscreen regions.
32 // kFrequentInvalidationDistanceThreshold defines what it means to be
33 // "offscreen" in terms of distance to visible in css pixels.
34 const float kInvalidationFrequencyThreshold
= 0.75f
;
35 const int kFrequentInvalidationDistanceThreshold
= 512;
37 // TODO(humper): The density threshold here is somewhat arbitrary; need a
38 // way to set // this from the command line so we can write a benchmark
39 // script and find a sweet spot.
40 const float kDensityThreshold
= 0.5f
;
42 bool rect_sort_y(const gfx::Rect
& r1
, const gfx::Rect
& r2
) {
43 return r1
.y() < r2
.y() || (r1
.y() == r2
.y() && r1
.x() < r2
.x());
46 bool rect_sort_x(const gfx::Rect
& r1
, const gfx::Rect
& r2
) {
47 return r1
.x() < r2
.x() || (r1
.x() == r2
.x() && r1
.y() < r2
.y());
50 float PerformClustering(const std::vector
<gfx::Rect
>& tiles
,
51 std::vector
<gfx::Rect
>* clustered_rects
) {
52 // These variables track the record area and invalid area
53 // for the entire clustering
54 int total_record_area
= 0;
55 int total_invalid_area
= 0;
57 // These variables track the record area and invalid area
58 // for the current cluster being constructed.
59 gfx::Rect cur_record_rect
;
60 int cluster_record_area
= 0, cluster_invalid_area
= 0;
62 for (std::vector
<gfx::Rect
>::const_iterator it
= tiles
.begin();
65 gfx::Rect invalid_tile
= *it
;
67 // For each tile, we consider adding the invalid tile to the
68 // current record rectangle. Only add it if the amount of empty
69 // space created is below a density threshold.
70 int tile_area
= invalid_tile
.width() * invalid_tile
.height();
72 gfx::Rect proposed_union
= cur_record_rect
;
73 proposed_union
.Union(invalid_tile
);
74 int proposed_area
= proposed_union
.width() * proposed_union
.height();
75 float proposed_density
=
76 static_cast<float>(cluster_invalid_area
+ tile_area
) /
77 static_cast<float>(proposed_area
);
79 if (proposed_density
>= kDensityThreshold
) {
80 // It's okay to add this invalid tile to the
81 // current recording rectangle.
82 cur_record_rect
= proposed_union
;
83 cluster_record_area
= proposed_area
;
84 cluster_invalid_area
+= tile_area
;
85 total_invalid_area
+= tile_area
;
87 // Adding this invalid tile to the current recording rectangle
88 // would exceed our badness threshold, so put the current rectangle
89 // in the list of recording rects, and start a new one.
90 clustered_rects
->push_back(cur_record_rect
);
91 total_record_area
+= cluster_record_area
;
92 cur_record_rect
= invalid_tile
;
93 cluster_invalid_area
= tile_area
;
94 cluster_record_area
= tile_area
;
98 DCHECK(!cur_record_rect
.IsEmpty());
99 clustered_rects
->push_back(cur_record_rect
);
100 total_record_area
+= cluster_record_area
;;
102 DCHECK_NE(total_record_area
, 0);
104 return static_cast<float>(total_invalid_area
) /
105 static_cast<float>(total_record_area
);
108 void ClusterTiles(const std::vector
<gfx::Rect
>& invalid_tiles
,
109 std::vector
<gfx::Rect
>* record_rects
) {
110 TRACE_EVENT1("cc", "ClusterTiles",
112 invalid_tiles
.size());
113 if (invalid_tiles
.size() <= 1) {
114 // Quickly handle the special case for common
115 // single-invalidation update, and also the less common
116 // case of no tiles passed in.
117 *record_rects
= invalid_tiles
;
121 // Sort the invalid tiles by y coordinate.
122 std::vector
<gfx::Rect
> invalid_tiles_vertical
= invalid_tiles
;
123 std::sort(invalid_tiles_vertical
.begin(),
124 invalid_tiles_vertical
.end(),
127 std::vector
<gfx::Rect
> vertical_clustering
;
128 float vertical_density
=
129 PerformClustering(invalid_tiles_vertical
, &vertical_clustering
);
131 // If vertical density is optimal, then we can return early.
132 if (vertical_density
== 1.f
) {
133 *record_rects
= vertical_clustering
;
137 // Now try again with a horizontal sort, see which one is best
138 std::vector
<gfx::Rect
> invalid_tiles_horizontal
= invalid_tiles
;
139 std::sort(invalid_tiles_horizontal
.begin(),
140 invalid_tiles_horizontal
.end(),
143 std::vector
<gfx::Rect
> horizontal_clustering
;
144 float horizontal_density
=
145 PerformClustering(invalid_tiles_horizontal
, &horizontal_clustering
);
147 if (vertical_density
< horizontal_density
) {
148 *record_rects
= horizontal_clustering
;
152 *record_rects
= vertical_clustering
;
156 const bool kDefaultClearCanvasSetting
= false;
158 const bool kDefaultClearCanvasSetting
= true;
165 PicturePile::PicturePile(float min_contents_scale
,
166 const gfx::Size
& tile_grid_size
)
167 : min_contents_scale_(0),
168 slow_down_raster_scale_factor_for_debug_(0),
169 has_any_recordings_(false),
170 clear_canvas_with_debug_color_(kDefaultClearCanvasSetting
),
171 requires_clear_(true),
172 is_solid_color_(false),
173 solid_color_(SK_ColorTRANSPARENT
),
174 background_color_(SK_ColorTRANSPARENT
),
175 pixel_record_distance_(kPixelDistanceToRecord
),
176 is_suitable_for_gpu_rasterization_(true) {
177 tiling_
.SetMaxTextureSize(gfx::Size(kBasePictureSize
, kBasePictureSize
));
178 SetMinContentsScale(min_contents_scale
);
179 SetTileGridSize(tile_grid_size
);
182 PicturePile::~PicturePile() {
185 bool PicturePile::UpdateAndExpandInvalidation(
186 ContentLayerClient
* painter
,
187 Region
* invalidation
,
188 const gfx::Size
& layer_size
,
189 const gfx::Rect
& visible_layer_rect
,
191 RecordingSource::RecordingMode recording_mode
) {
192 gfx::Rect interest_rect
= visible_layer_rect
;
193 interest_rect
.Inset(-pixel_record_distance_
, -pixel_record_distance_
);
194 recorded_viewport_
= interest_rect
;
195 recorded_viewport_
.Intersect(gfx::Rect(layer_size
));
197 bool updated
= ApplyInvalidationAndResize(interest_rect
, invalidation
,
198 layer_size
, frame_number
);
199 std::vector
<gfx::Rect
> invalid_tiles
;
200 GetInvalidTileRects(interest_rect
, invalidation
, visible_layer_rect
,
201 frame_number
, &invalid_tiles
);
202 std::vector
<gfx::Rect
> record_rects
;
203 ClusterTiles(invalid_tiles
, &record_rects
);
205 if (record_rects
.empty())
208 CreatePictures(painter
, recording_mode
, record_rects
);
210 DetermineIfSolidColor();
212 has_any_recordings_
= true;
213 DCHECK(CanRasterSlowTileCheck(recorded_viewport_
));
217 bool PicturePile::ApplyInvalidationAndResize(const gfx::Rect
& interest_rect
,
218 Region
* invalidation
,
219 const gfx::Size
& layer_size
,
221 bool updated
= false;
223 Region synthetic_invalidation
;
224 gfx::Size old_tiling_size
= GetSize();
225 if (old_tiling_size
!= layer_size
) {
226 tiling_
.SetTilingSize(layer_size
);
230 gfx::Rect interest_rect_over_tiles
=
231 tiling_
.ExpandRectToTileBounds(interest_rect
);
233 if (old_tiling_size
!= layer_size
) {
234 gfx::Size
min_tiling_size(
235 std::min(GetSize().width(), old_tiling_size
.width()),
236 std::min(GetSize().height(), old_tiling_size
.height()));
237 gfx::Size
max_tiling_size(
238 std::max(GetSize().width(), old_tiling_size
.width()),
239 std::max(GetSize().height(), old_tiling_size
.height()));
241 has_any_recordings_
= false;
243 // Drop recordings that are outside the new or old layer bounds or that
244 // changed size. Newly exposed areas are considered invalidated.
245 // Previously exposed areas that are now outside of bounds also need to
246 // be invalidated, as they may become part of raster when scale < 1.
247 std::vector
<PictureMapKey
> to_erase
;
248 int min_toss_x
= tiling_
.num_tiles_x();
249 if (max_tiling_size
.width() > min_tiling_size
.width()) {
251 tiling_
.FirstBorderTileXIndexFromSrcCoord(min_tiling_size
.width());
253 int min_toss_y
= tiling_
.num_tiles_y();
254 if (max_tiling_size
.height() > min_tiling_size
.height()) {
256 tiling_
.FirstBorderTileYIndexFromSrcCoord(min_tiling_size
.height());
258 for (const auto& key_picture_pair
: picture_map_
) {
259 const PictureMapKey
& key
= key_picture_pair
.first
;
260 if (key
.first
< min_toss_x
&& key
.second
< min_toss_y
) {
261 has_any_recordings_
|= !!key_picture_pair
.second
.GetPicture();
264 to_erase
.push_back(key
);
267 for (size_t i
= 0; i
< to_erase
.size(); ++i
)
268 picture_map_
.erase(to_erase
[i
]);
270 // If a recording is dropped and not re-recorded below, invalidate that
271 // full recording to cause any raster tiles that would use it to be
273 // If the recording will be replaced below, invalidate newly exposed
274 // areas and previously exposed areas to force raster tiles that include the
275 // old recording to know there is new recording to display.
276 gfx::Rect min_tiling_rect_over_tiles
=
277 tiling_
.ExpandRectToTileBounds(gfx::Rect(min_tiling_size
));
278 if (min_toss_x
< tiling_
.num_tiles_x()) {
279 // The bounds which we want to invalidate are the tiles along the old
280 // edge of the pile when expanding, or the new edge of the pile when
281 // shrinking. In either case, it's the difference of the two, so we'll
282 // call this bounding box the DELTA EDGE RECT.
284 // In the picture below, the delta edge rect would be the bounding box of
285 // tiles {h,i,j}. |min_toss_x| would be equal to the horizontal index of
288 // min pile edge-v max pile edge-v
289 // ---------------+ - - - - - - - -+
290 // mmppssvvyybbeeh|h .
291 // mmppssvvyybbeeh|h .
292 // nnqqttwwzzccffi|i .
293 // nnqqttwwzzccffi|i .
294 // oorruuxxaaddggj|j .
295 // oorruuxxaaddggj|j .
296 // ---------------+ - - - - - - - -+ <- min pile edge
298 // - - - - - - - - - - - - - - - -+ <- max pile edge
300 // If you were to slide a vertical beam from the left edge of the
301 // delta edge rect toward the right, it would either hit the right edge
302 // of the delta edge rect, or the interest rect (expanded to the bounds
303 // of the tiles it touches). The same is true for a beam parallel to
304 // any of the four edges, sliding across the delta edge rect. We use
305 // the union of these four rectangles generated by these beams to
306 // determine which part of the delta edge rect is outside of the expanded
309 // Case 1: Intersect rect is outside the delta edge rect. It can be
310 // either on the left or the right. The |left_rect| and |right_rect|,
311 // cover this case, one will be empty and one will cover the full
312 // delta edge rect. In the picture below, |left_rect| would cover the
313 // delta edge rect, and |right_rect| would be empty.
314 // +----------------------+ |^^^^^^^^^^^^^^^|
315 // |===> DELTA EDGE RECT | | |
316 // |===> | | INTEREST RECT |
319 // +----------------------+ |vvvvvvvvvvvvvvv|
321 // Case 2: Interest rect is inside the delta edge rect. It will always
322 // fill the entire delta edge rect horizontally since the old edge rect
323 // is a single tile wide, and the interest rect has been expanded to the
324 // bounds of the tiles it touches. In this case the |left_rect| and
325 // |right_rect| will be empty, but the case is handled by the |top_rect|
326 // and |bottom_rect|. In the picture below, neither the |top_rect| nor
327 // |bottom_rect| would empty, they would each cover the area of the old
328 // edge rect outside the expanded interest rect.
329 // +-----------------+
330 // |:::::::::::::::::|
331 // |:::::::::::::::::|
332 // |vvvvvvvvvvvvvvvvv|
334 // +-----------------+
337 // +-----------------+
339 // | DELTA EDGE RECT |
340 // +-----------------+
342 // Lastly, we need to consider tiles inside the expanded interest rect.
343 // For those tiles, we want to invalidate exactly the newly exposed
344 // pixels. In the picture below the tiles in the delta edge rect have
345 // been resized and the area covered by periods must be invalidated. The
346 // |exposed_rect| will cover exactly that area.
348 // +---------+-------+
351 // | DELTA EDGE.RECT.|
358 // +---------+-------+
360 int left
= tiling_
.TilePositionX(min_toss_x
);
361 int right
= left
+ tiling_
.TileSizeX(min_toss_x
);
362 int top
= min_tiling_rect_over_tiles
.y();
363 int bottom
= min_tiling_rect_over_tiles
.bottom();
365 int left_until
= std::min(interest_rect_over_tiles
.x(), right
);
366 int right_until
= std::max(interest_rect_over_tiles
.right(), left
);
367 int top_until
= std::min(interest_rect_over_tiles
.y(), bottom
);
368 int bottom_until
= std::max(interest_rect_over_tiles
.bottom(), top
);
370 int exposed_left
= min_tiling_size
.width();
371 int exposed_left_until
= max_tiling_size
.width();
372 int exposed_top
= top
;
373 int exposed_bottom
= max_tiling_size
.height();
374 DCHECK_GE(exposed_left
, left
);
376 gfx::Rect
left_rect(left
, top
, left_until
- left
, bottom
- top
);
377 gfx::Rect
right_rect(right_until
, top
, right
- right_until
, bottom
- top
);
378 gfx::Rect
top_rect(left
, top
, right
- left
, top_until
- top
);
379 gfx::Rect
bottom_rect(
380 left
, bottom_until
, right
- left
, bottom
- bottom_until
);
381 gfx::Rect
exposed_rect(exposed_left
,
383 exposed_left_until
- exposed_left
,
384 exposed_bottom
- exposed_top
);
385 synthetic_invalidation
.Union(left_rect
);
386 synthetic_invalidation
.Union(right_rect
);
387 synthetic_invalidation
.Union(top_rect
);
388 synthetic_invalidation
.Union(bottom_rect
);
389 synthetic_invalidation
.Union(exposed_rect
);
391 if (min_toss_y
< tiling_
.num_tiles_y()) {
392 // The same thing occurs here as in the case above, but the invalidation
393 // rect is the bounding box around the bottom row of tiles in the min
394 // pile. This would be tiles {o,r,u,x,a,d,g,j} in the above picture.
396 int top
= tiling_
.TilePositionY(min_toss_y
);
397 int bottom
= top
+ tiling_
.TileSizeY(min_toss_y
);
398 int left
= min_tiling_rect_over_tiles
.x();
399 int right
= min_tiling_rect_over_tiles
.right();
401 int top_until
= std::min(interest_rect_over_tiles
.y(), bottom
);
402 int bottom_until
= std::max(interest_rect_over_tiles
.bottom(), top
);
403 int left_until
= std::min(interest_rect_over_tiles
.x(), right
);
404 int right_until
= std::max(interest_rect_over_tiles
.right(), left
);
406 int exposed_top
= min_tiling_size
.height();
407 int exposed_top_until
= max_tiling_size
.height();
408 int exposed_left
= left
;
409 int exposed_right
= max_tiling_size
.width();
410 DCHECK_GE(exposed_top
, top
);
412 gfx::Rect
left_rect(left
, top
, left_until
- left
, bottom
- top
);
413 gfx::Rect
right_rect(right_until
, top
, right
- right_until
, bottom
- top
);
414 gfx::Rect
top_rect(left
, top
, right
- left
, top_until
- top
);
415 gfx::Rect
bottom_rect(
416 left
, bottom_until
, right
- left
, bottom
- bottom_until
);
417 gfx::Rect
exposed_rect(exposed_left
,
419 exposed_right
- exposed_left
,
420 exposed_top_until
- exposed_top
);
421 synthetic_invalidation
.Union(left_rect
);
422 synthetic_invalidation
.Union(right_rect
);
423 synthetic_invalidation
.Union(top_rect
);
424 synthetic_invalidation
.Union(bottom_rect
);
425 synthetic_invalidation
.Union(exposed_rect
);
429 // Detect cases where the full pile is invalidated, in this situation we
430 // can just drop/invalidate everything.
431 if (invalidation
->Contains(gfx::Rect(old_tiling_size
)) ||
432 invalidation
->Contains(gfx::Rect(GetSize()))) {
433 for (auto& it
: picture_map_
)
434 updated
= it
.second
.Invalidate(frame_number
) || updated
;
436 // Expand invalidation that is on tiles that aren't in the interest rect and
437 // will not be re-recorded below. These tiles are no longer valid and should
438 // be considerered fully invalid, so we can know to not keep around raster
439 // tiles that intersect with these recording tiles.
440 Region invalidation_expanded_to_full_tiles
;
442 for (Region::Iterator
i(*invalidation
); i
.has_rect(); i
.next()) {
443 gfx::Rect invalid_rect
= i
.rect();
445 // This rect covers the bounds (excluding borders) of all tiles whose
446 // bounds (including borders) touch the |interest_rect|. This matches
447 // the iteration of the |invalid_rect| below which includes borders when
448 // calling Invalidate() on pictures.
449 gfx::Rect invalid_rect_outside_interest_rect_tiles
=
450 tiling_
.ExpandRectToTileBounds(invalid_rect
);
451 // We subtract the |interest_rect_over_tiles| which represents the bounds
452 // of tiles that will be re-recorded below. This matches the iteration of
453 // |interest_rect| below which includes borders.
454 // TODO(danakj): We should have a Rect-subtract-Rect-to-2-rects operator
455 // instead of using Rect::Subtract which gives you the bounding box of the
457 invalid_rect_outside_interest_rect_tiles
.Subtract(
458 interest_rect_over_tiles
);
459 invalidation_expanded_to_full_tiles
.Union(
460 invalid_rect_outside_interest_rect_tiles
);
462 // Split this inflated invalidation across tile boundaries and apply it
463 // to all tiles that it touches.
464 bool include_borders
= true;
465 for (TilingData::Iterator
iter(&tiling_
, invalid_rect
, include_borders
);
468 const PictureMapKey
& key
= iter
.index();
470 PictureMap::iterator picture_it
= picture_map_
.find(key
);
471 if (picture_it
== picture_map_
.end())
474 // Inform the grid cell that it has been invalidated in this frame.
475 updated
= picture_it
->second
.Invalidate(frame_number
) || updated
;
476 // Invalidate drops the picture so the whole tile better be invalidated
477 // if it won't be re-recorded below.
478 DCHECK_IMPLIES(!tiling_
.TileBounds(key
.first
, key
.second
)
479 .Intersects(interest_rect_over_tiles
),
480 invalidation_expanded_to_full_tiles
.Contains(
481 tiling_
.TileBounds(key
.first
, key
.second
)));
484 invalidation
->Union(invalidation_expanded_to_full_tiles
);
487 invalidation
->Union(synthetic_invalidation
);
491 void PicturePile::GetInvalidTileRects(const gfx::Rect
& interest_rect
,
492 Region
* invalidation
,
493 const gfx::Rect
& visible_layer_rect
,
495 std::vector
<gfx::Rect
>* invalid_tiles
) {
496 // Make a list of all invalid tiles; we will attempt to
497 // cluster these into multiple invalidation regions.
498 bool include_borders
= true;
499 for (TilingData::Iterator
it(&tiling_
, interest_rect
, include_borders
); it
;
501 const PictureMapKey
& key
= it
.index();
502 PictureInfo
& info
= picture_map_
[key
];
504 gfx::Rect rect
= PaddedRect(key
);
505 int distance_to_visible
=
506 rect
.ManhattanInternalDistance(visible_layer_rect
);
508 if (info
.NeedsRecording(frame_number
, distance_to_visible
)) {
509 gfx::Rect tile
= tiling_
.TileBounds(key
.first
, key
.second
);
510 invalid_tiles
->push_back(tile
);
511 } else if (!info
.GetPicture()) {
512 if (recorded_viewport_
.Intersects(rect
)) {
513 // Recorded viewport is just an optimization for a fully recorded
514 // interest rect. In this case, a tile in that rect has declined
515 // to be recorded (probably due to frequent invalidations).
516 // TODO(enne): Shrink the recorded_viewport_ rather than clearing.
517 recorded_viewport_
= gfx::Rect();
520 // If a tile in the interest rect is not recorded, the entire tile needs
521 // to be considered invalid, so that we know not to keep around raster
522 // tiles that intersect this recording tile.
523 invalidation
->Union(tiling_
.TileBounds(it
.index_x(), it
.index_y()));
528 void PicturePile::CreatePictures(ContentLayerClient
* painter
,
529 RecordingSource::RecordingMode recording_mode
,
530 const std::vector
<gfx::Rect
>& record_rects
) {
531 for (const auto& record_rect
: record_rects
) {
532 gfx::Rect padded_record_rect
= PadRect(record_rect
);
534 int repeat_count
= std::max(1, slow_down_raster_scale_factor_for_debug_
);
535 scoped_refptr
<Picture
> picture
;
537 // Note: Currently, gathering of pixel refs when using a single
538 // raster thread doesn't provide any benefit. This might change
539 // in the future but we avoid it for now to reduce the cost of
541 bool gather_pixel_refs
= TileTaskWorkerPool::GetNumWorkerThreads() > 1;
543 for (int i
= 0; i
< repeat_count
; i
++) {
544 picture
= Picture::Create(padded_record_rect
, painter
, tile_grid_size_
,
545 gather_pixel_refs
, recording_mode
);
546 // Note the '&&' with previous is-suitable state.
547 // This means that once a picture-pile becomes unsuitable for gpu
548 // rasterization due to some content, it will continue to be unsuitable
549 // even if that content is replaced by gpu-friendly content.
550 // This is an optimization to avoid iterating though all pictures in
551 // the pile after each invalidation.
552 if (is_suitable_for_gpu_rasterization_
) {
553 const char* reason
= nullptr;
554 is_suitable_for_gpu_rasterization_
&=
555 picture
->IsSuitableForGpuRasterization(&reason
);
557 if (!is_suitable_for_gpu_rasterization_
) {
558 TRACE_EVENT_INSTANT1("cc", "GPU Rasterization Veto",
559 TRACE_EVENT_SCOPE_THREAD
, "reason", reason
);
564 bool found_tile_for_recorded_picture
= false;
566 bool include_borders
= true;
567 for (TilingData::Iterator
it(&tiling_
, padded_record_rect
, include_borders
);
569 const PictureMapKey
& key
= it
.index();
570 gfx::Rect tile
= PaddedRect(key
);
571 if (padded_record_rect
.Contains(tile
)) {
572 PictureInfo
& info
= picture_map_
[key
];
573 info
.SetPicture(picture
);
574 found_tile_for_recorded_picture
= true;
577 DCHECK(found_tile_for_recorded_picture
);
581 scoped_refptr
<RasterSource
> PicturePile::CreateRasterSource(
582 bool can_use_lcd_text
) const {
583 return scoped_refptr
<RasterSource
>(
584 PicturePileImpl::CreateFromPicturePile(this, can_use_lcd_text
));
587 gfx::Size
PicturePile::GetSize() const {
588 return tiling_
.tiling_size();
591 void PicturePile::SetEmptyBounds() {
592 tiling_
.SetTilingSize(gfx::Size());
596 void PicturePile::SetMinContentsScale(float min_contents_scale
) {
597 DCHECK(min_contents_scale
);
598 if (min_contents_scale_
== min_contents_scale
)
601 // Picture contents are played back scaled. When the final contents scale is
602 // less than 1 (i.e. low res), then multiple recorded pixels will be used
603 // to raster one final pixel. To avoid splitting a final pixel across
604 // pictures (which would result in incorrect rasterization due to blending), a
605 // buffer margin is added so that any picture can be snapped to integral
608 // For example, if a 1/4 contents scale is used, then that would be 3 buffer
609 // pixels, since that's the minimum number of pixels to add so that resulting
610 // content can be snapped to a four pixel aligned grid.
611 int buffer_pixels
= static_cast<int>(ceil(1 / min_contents_scale
) - 1);
612 buffer_pixels
= std::max(0, buffer_pixels
);
613 SetBufferPixels(buffer_pixels
);
614 min_contents_scale_
= min_contents_scale
;
617 void PicturePile::SetSlowdownRasterScaleFactor(int factor
) {
618 slow_down_raster_scale_factor_for_debug_
= factor
;
621 void PicturePile::SetBackgroundColor(SkColor background_color
) {
622 background_color_
= background_color
;
625 void PicturePile::SetRequiresClear(bool requires_clear
) {
626 requires_clear_
= requires_clear
;
629 bool PicturePile::IsSuitableForGpuRasterization() const {
630 return is_suitable_for_gpu_rasterization_
;
633 void PicturePile::SetTileGridSize(const gfx::Size
& tile_grid_size
) {
634 DCHECK_GT(tile_grid_size
.width(), 0);
635 DCHECK_GT(tile_grid_size
.height(), 0);
637 tile_grid_size_
= tile_grid_size
;
640 void PicturePile::SetUnsuitableForGpuRasterizationForTesting() {
641 is_suitable_for_gpu_rasterization_
= false;
644 gfx::Size
PicturePile::GetTileGridSizeForTesting() const {
645 return tile_grid_size_
;
648 bool PicturePile::CanRasterSlowTileCheck(const gfx::Rect
& layer_rect
) const {
649 bool include_borders
= false;
650 for (TilingData::Iterator
tile_iter(&tiling_
, layer_rect
, include_borders
);
651 tile_iter
; ++tile_iter
) {
652 PictureMap::const_iterator map_iter
= picture_map_
.find(tile_iter
.index());
653 if (map_iter
== picture_map_
.end())
655 if (!map_iter
->second
.GetPicture())
661 void PicturePile::DetermineIfSolidColor() {
662 is_solid_color_
= false;
663 solid_color_
= SK_ColorTRANSPARENT
;
665 if (picture_map_
.empty()) {
669 PictureMap::const_iterator it
= picture_map_
.begin();
670 const Picture
* picture
= it
->second
.GetPicture();
672 // Missing recordings due to frequent invalidations or being too far away
673 // from the interest rect will cause the a null picture to exist.
677 // Don't bother doing more work if the first image is too complicated.
678 if (picture
->ApproximateOpCount() > kOpCountThatIsOkToAnalyze
)
681 // Make sure all of the mapped images point to the same picture.
682 for (++it
; it
!= picture_map_
.end(); ++it
) {
683 if (it
->second
.GetPicture() != picture
)
687 gfx::Size layer_size
= GetSize();
688 skia::AnalysisCanvas
canvas(layer_size
.width(), layer_size
.height());
690 picture
->Raster(&canvas
, nullptr, Region(), 1.0f
);
691 is_solid_color_
= canvas
.GetColorIfSolid(&solid_color_
);
694 gfx::Rect
PicturePile::PaddedRect(const PictureMapKey
& key
) const {
695 gfx::Rect tile
= tiling_
.TileBounds(key
.first
, key
.second
);
696 return PadRect(tile
);
699 gfx::Rect
PicturePile::PadRect(const gfx::Rect
& rect
) const {
700 gfx::Rect padded_rect
= rect
;
701 padded_rect
.Inset(-buffer_pixels(), -buffer_pixels(), -buffer_pixels(),
706 void PicturePile::Clear() {
707 picture_map_
.clear();
708 recorded_viewport_
= gfx::Rect();
709 has_any_recordings_
= false;
710 is_solid_color_
= false;
713 PicturePile::PictureInfo::PictureInfo() : last_frame_number_(0) {
716 PicturePile::PictureInfo::~PictureInfo() {
719 void PicturePile::PictureInfo::AdvanceInvalidationHistory(int frame_number
) {
720 DCHECK_GE(frame_number
, last_frame_number_
);
721 if (frame_number
== last_frame_number_
)
724 invalidation_history_
<<= (frame_number
- last_frame_number_
);
725 last_frame_number_
= frame_number
;
728 bool PicturePile::PictureInfo::Invalidate(int frame_number
) {
729 AdvanceInvalidationHistory(frame_number
);
730 invalidation_history_
.set(0);
732 bool did_invalidate
= !!picture_
.get();
734 return did_invalidate
;
737 bool PicturePile::PictureInfo::NeedsRecording(int frame_number
,
738 int distance_to_visible
) {
739 AdvanceInvalidationHistory(frame_number
);
741 // We only need recording if we don't have a picture. Furthermore, we only
742 // need a recording if we're within frequent invalidation distance threshold
743 // or the invalidation is not frequent enough (below invalidation frequency
745 return !picture_
.get() &&
746 ((distance_to_visible
<= kFrequentInvalidationDistanceThreshold
) ||
747 (GetInvalidationFrequency() < kInvalidationFrequencyThreshold
));
750 void PicturePile::SetBufferPixels(int new_buffer_pixels
) {
751 if (new_buffer_pixels
== buffer_pixels())
755 tiling_
.SetBorderTexels(new_buffer_pixels
);
758 void PicturePile::PictureInfo::SetPicture(scoped_refptr
<Picture
> picture
) {
762 const Picture
* PicturePile::PictureInfo::GetPicture() const {
763 return picture_
.get();
766 float PicturePile::PictureInfo::GetInvalidationFrequency() const {
767 return invalidation_history_
.count() /
768 static_cast<float>(INVALIDATION_FRAMES_TRACKED
);