cc: Adding DidFinishImplFrame to LTHI.
[chromium-blink-merge.git] / cc / resources / picture_pile.cc
blobd82ead7b2b772aca24621ea1781b0a07cb4da102
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/histograms.h"
12 #include "cc/base/region.h"
13 #include "cc/resources/picture_pile_impl.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;
29 // TODO(humper): The density threshold here is somewhat arbitrary; need a
30 // way to set // this from the command line so we can write a benchmark
31 // script and find a sweet spot.
32 const float kDensityThreshold = 0.5f;
34 bool rect_sort_y(const gfx::Rect& r1, const gfx::Rect& r2) {
35 return r1.y() < r2.y() || (r1.y() == r2.y() && r1.x() < r2.x());
38 bool rect_sort_x(const gfx::Rect& r1, const gfx::Rect& r2) {
39 return r1.x() < r2.x() || (r1.x() == r2.x() && r1.y() < r2.y());
42 float PerformClustering(const std::vector<gfx::Rect>& tiles,
43 std::vector<gfx::Rect>* clustered_rects) {
44 // These variables track the record area and invalid area
45 // for the entire clustering
46 int total_record_area = 0;
47 int total_invalid_area = 0;
49 // These variables track the record area and invalid area
50 // for the current cluster being constructed.
51 gfx::Rect cur_record_rect;
52 int cluster_record_area = 0, cluster_invalid_area = 0;
54 for (std::vector<gfx::Rect>::const_iterator it = tiles.begin();
55 it != tiles.end();
56 it++) {
57 gfx::Rect invalid_tile = *it;
59 // For each tile, we consider adding the invalid tile to the
60 // current record rectangle. Only add it if the amount of empty
61 // space created is below a density threshold.
62 int tile_area = invalid_tile.width() * invalid_tile.height();
64 gfx::Rect proposed_union = cur_record_rect;
65 proposed_union.Union(invalid_tile);
66 int proposed_area = proposed_union.width() * proposed_union.height();
67 float proposed_density =
68 static_cast<float>(cluster_invalid_area + tile_area) /
69 static_cast<float>(proposed_area);
71 if (proposed_density >= kDensityThreshold) {
72 // It's okay to add this invalid tile to the
73 // current recording rectangle.
74 cur_record_rect = proposed_union;
75 cluster_record_area = proposed_area;
76 cluster_invalid_area += tile_area;
77 total_invalid_area += tile_area;
78 } else {
79 // Adding this invalid tile to the current recording rectangle
80 // would exceed our badness threshold, so put the current rectangle
81 // in the list of recording rects, and start a new one.
82 clustered_rects->push_back(cur_record_rect);
83 total_record_area += cluster_record_area;
84 cur_record_rect = invalid_tile;
85 cluster_invalid_area = tile_area;
86 cluster_record_area = tile_area;
90 DCHECK(!cur_record_rect.IsEmpty());
91 clustered_rects->push_back(cur_record_rect);
92 total_record_area += cluster_record_area;;
94 DCHECK_NE(total_record_area, 0);
96 return static_cast<float>(total_invalid_area) /
97 static_cast<float>(total_record_area);
100 void ClusterTiles(const std::vector<gfx::Rect>& invalid_tiles,
101 std::vector<gfx::Rect>* record_rects) {
102 TRACE_EVENT1("cc", "ClusterTiles",
103 "count",
104 invalid_tiles.size());
105 if (invalid_tiles.size() <= 1) {
106 // Quickly handle the special case for common
107 // single-invalidation update, and also the less common
108 // case of no tiles passed in.
109 *record_rects = invalid_tiles;
110 return;
113 // Sort the invalid tiles by y coordinate.
114 std::vector<gfx::Rect> invalid_tiles_vertical = invalid_tiles;
115 std::sort(invalid_tiles_vertical.begin(),
116 invalid_tiles_vertical.end(),
117 rect_sort_y);
119 std::vector<gfx::Rect> vertical_clustering;
120 float vertical_density =
121 PerformClustering(invalid_tiles_vertical, &vertical_clustering);
123 // If vertical density is optimal, then we can return early.
124 if (vertical_density == 1.f) {
125 *record_rects = vertical_clustering;
126 return;
129 // Now try again with a horizontal sort, see which one is best
130 std::vector<gfx::Rect> invalid_tiles_horizontal = invalid_tiles;
131 std::sort(invalid_tiles_horizontal.begin(),
132 invalid_tiles_horizontal.end(),
133 rect_sort_x);
135 std::vector<gfx::Rect> horizontal_clustering;
136 float horizontal_density =
137 PerformClustering(invalid_tiles_horizontal, &horizontal_clustering);
139 if (vertical_density < horizontal_density) {
140 *record_rects = horizontal_clustering;
141 return;
144 *record_rects = vertical_clustering;
147 #ifdef NDEBUG
148 const bool kDefaultClearCanvasSetting = false;
149 #else
150 const bool kDefaultClearCanvasSetting = true;
151 #endif
153 DEFINE_SCOPED_UMA_HISTOGRAM_AREA_TIMER(
154 ScopedPicturePileUpdateTimer,
155 "Compositing.PicturePile.UpdateUs",
156 "Compositing.PicturePile.UpdateInvalidatedAreaPerMs");
158 } // namespace
160 namespace cc {
162 PicturePile::PicturePile(float min_contents_scale,
163 const gfx::Size& tile_grid_size)
164 : min_contents_scale_(0),
165 slow_down_raster_scale_factor_for_debug_(0),
166 gather_pixel_refs_(false),
167 has_any_recordings_(false),
168 clear_canvas_with_debug_color_(kDefaultClearCanvasSetting),
169 requires_clear_(true),
170 is_solid_color_(false),
171 solid_color_(SK_ColorTRANSPARENT),
172 background_color_(SK_ColorTRANSPARENT),
173 pixel_record_distance_(kPixelDistanceToRecord),
174 is_suitable_for_gpu_rasterization_(true) {
175 tiling_.SetMaxTextureSize(gfx::Size(kBasePictureSize, kBasePictureSize));
176 SetMinContentsScale(min_contents_scale);
177 SetTileGridSize(tile_grid_size);
180 PicturePile::~PicturePile() {
183 bool PicturePile::UpdateAndExpandInvalidation(
184 ContentLayerClient* painter,
185 Region* invalidation,
186 const gfx::Size& layer_size,
187 const gfx::Rect& visible_layer_rect,
188 int frame_number,
189 RecordingSource::RecordingMode recording_mode) {
190 ScopedPicturePileUpdateTimer timer;
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);
200 // Count the area that is being invalidated.
201 Region recorded_invalidation(*invalidation);
202 recorded_invalidation.Intersect(recorded_viewport_);
203 for (Region::Iterator it(recorded_invalidation); it.has_rect(); it.next())
204 timer.AddArea(it.rect().size().GetArea());
206 std::vector<gfx::Rect> invalid_tiles;
207 GetInvalidTileRects(interest_rect, &invalid_tiles);
208 std::vector<gfx::Rect> record_rects;
209 ClusterTiles(invalid_tiles, &record_rects);
211 if (record_rects.empty())
212 return updated;
214 CreatePictures(painter, recording_mode, record_rects);
216 DetermineIfSolidColor();
218 has_any_recordings_ = true;
219 DCHECK(CanRasterSlowTileCheck(recorded_viewport_));
220 return true;
223 bool PicturePile::ApplyInvalidationAndResize(const gfx::Rect& interest_rect,
224 Region* invalidation,
225 const gfx::Size& layer_size,
226 int frame_number) {
227 bool updated = false;
229 Region synthetic_invalidation;
230 gfx::Size old_tiling_size = GetSize();
231 if (old_tiling_size != layer_size) {
232 tiling_.SetTilingSize(layer_size);
233 updated = true;
236 gfx::Rect interest_rect_over_tiles =
237 tiling_.ExpandRectToTileBounds(interest_rect);
239 if (old_tiling_size != layer_size) {
240 gfx::Size min_tiling_size(
241 std::min(GetSize().width(), old_tiling_size.width()),
242 std::min(GetSize().height(), old_tiling_size.height()));
243 gfx::Size max_tiling_size(
244 std::max(GetSize().width(), old_tiling_size.width()),
245 std::max(GetSize().height(), old_tiling_size.height()));
247 has_any_recordings_ = false;
249 // Drop recordings that are outside the new or old layer bounds or that
250 // changed size. Newly exposed areas are considered invalidated.
251 // Previously exposed areas that are now outside of bounds also need to
252 // be invalidated, as they may become part of raster when scale < 1.
253 std::vector<PictureMapKey> to_erase;
254 int min_toss_x = tiling_.num_tiles_x();
255 if (max_tiling_size.width() > min_tiling_size.width()) {
256 min_toss_x =
257 tiling_.FirstBorderTileXIndexFromSrcCoord(min_tiling_size.width());
259 int min_toss_y = tiling_.num_tiles_y();
260 if (max_tiling_size.height() > min_tiling_size.height()) {
261 min_toss_y =
262 tiling_.FirstBorderTileYIndexFromSrcCoord(min_tiling_size.height());
264 for (const auto& key_picture_pair : picture_map_) {
265 const PictureMapKey& key = key_picture_pair.first;
266 if (key.first < min_toss_x && key.second < min_toss_y) {
267 has_any_recordings_ = true;
268 continue;
270 to_erase.push_back(key);
273 for (size_t i = 0; i < to_erase.size(); ++i)
274 picture_map_.erase(to_erase[i]);
276 // If a recording is dropped and not re-recorded below, invalidate that
277 // full recording to cause any raster tiles that would use it to be
278 // dropped.
279 // If the recording will be replaced below, invalidate newly exposed
280 // areas and previously exposed areas to force raster tiles that include the
281 // old recording to know there is new recording to display.
282 gfx::Rect min_tiling_rect_over_tiles =
283 tiling_.ExpandRectToTileBounds(gfx::Rect(min_tiling_size));
284 if (min_toss_x < tiling_.num_tiles_x()) {
285 // The bounds which we want to invalidate are the tiles along the old
286 // edge of the pile when expanding, or the new edge of the pile when
287 // shrinking. In either case, it's the difference of the two, so we'll
288 // call this bounding box the DELTA EDGE RECT.
290 // In the picture below, the delta edge rect would be the bounding box of
291 // tiles {h,i,j}. |min_toss_x| would be equal to the horizontal index of
292 // the same tiles.
294 // min pile edge-v max pile edge-v
295 // ---------------+ - - - - - - - -+
296 // mmppssvvyybbeeh|h .
297 // mmppssvvyybbeeh|h .
298 // nnqqttwwzzccffi|i .
299 // nnqqttwwzzccffi|i .
300 // oorruuxxaaddggj|j .
301 // oorruuxxaaddggj|j .
302 // ---------------+ - - - - - - - -+ <- min pile edge
303 // .
304 // - - - - - - - - - - - - - - - -+ <- max pile edge
306 // If you were to slide a vertical beam from the left edge of the
307 // delta edge rect toward the right, it would either hit the right edge
308 // of the delta edge rect, or the interest rect (expanded to the bounds
309 // of the tiles it touches). The same is true for a beam parallel to
310 // any of the four edges, sliding across the delta edge rect. We use
311 // the union of these four rectangles generated by these beams to
312 // determine which part of the delta edge rect is outside of the expanded
313 // interest rect.
315 // Case 1: Intersect rect is outside the delta edge rect. It can be
316 // either on the left or the right. The |left_rect| and |right_rect|,
317 // cover this case, one will be empty and one will cover the full
318 // delta edge rect. In the picture below, |left_rect| would cover the
319 // delta edge rect, and |right_rect| would be empty.
320 // +----------------------+ |^^^^^^^^^^^^^^^|
321 // |===> DELTA EDGE RECT | | |
322 // |===> | | INTEREST RECT |
323 // |===> | | |
324 // |===> | | |
325 // +----------------------+ |vvvvvvvvvvvvvvv|
327 // Case 2: Interest rect is inside the delta edge rect. It will always
328 // fill the entire delta edge rect horizontally since the old edge rect
329 // is a single tile wide, and the interest rect has been expanded to the
330 // bounds of the tiles it touches. In this case the |left_rect| and
331 // |right_rect| will be empty, but the case is handled by the |top_rect|
332 // and |bottom_rect|. In the picture below, neither the |top_rect| nor
333 // |bottom_rect| would empty, they would each cover the area of the old
334 // edge rect outside the expanded interest rect.
335 // +-----------------+
336 // |:::::::::::::::::|
337 // |:::::::::::::::::|
338 // |vvvvvvvvvvvvvvvvv|
339 // | |
340 // +-----------------+
341 // | INTEREST RECT |
342 // | |
343 // +-----------------+
344 // | |
345 // | DELTA EDGE RECT |
346 // +-----------------+
348 // Lastly, we need to consider tiles inside the expanded interest rect.
349 // For those tiles, we want to invalidate exactly the newly exposed
350 // pixels. In the picture below the tiles in the delta edge rect have
351 // been resized and the area covered by periods must be invalidated. The
352 // |exposed_rect| will cover exactly that area.
353 // v-min pile edge
354 // +---------+-------+
355 // | ........|
356 // | ........|
357 // | DELTA EDGE.RECT.|
358 // | ........|
359 // | ........|
360 // | ........|
361 // | ........|
362 // | ........|
363 // | ........|
364 // +---------+-------+
366 int left = tiling_.TilePositionX(min_toss_x);
367 int right = left + tiling_.TileSizeX(min_toss_x);
368 int top = min_tiling_rect_over_tiles.y();
369 int bottom = min_tiling_rect_over_tiles.bottom();
371 int left_until = std::min(interest_rect_over_tiles.x(), right);
372 int right_until = std::max(interest_rect_over_tiles.right(), left);
373 int top_until = std::min(interest_rect_over_tiles.y(), bottom);
374 int bottom_until = std::max(interest_rect_over_tiles.bottom(), top);
376 int exposed_left = min_tiling_size.width();
377 int exposed_left_until = max_tiling_size.width();
378 int exposed_top = top;
379 int exposed_bottom = max_tiling_size.height();
380 DCHECK_GE(exposed_left, left);
382 gfx::Rect left_rect(left, top, left_until - left, bottom - top);
383 gfx::Rect right_rect(right_until, top, right - right_until, bottom - top);
384 gfx::Rect top_rect(left, top, right - left, top_until - top);
385 gfx::Rect bottom_rect(
386 left, bottom_until, right - left, bottom - bottom_until);
387 gfx::Rect exposed_rect(exposed_left,
388 exposed_top,
389 exposed_left_until - exposed_left,
390 exposed_bottom - exposed_top);
391 synthetic_invalidation.Union(left_rect);
392 synthetic_invalidation.Union(right_rect);
393 synthetic_invalidation.Union(top_rect);
394 synthetic_invalidation.Union(bottom_rect);
395 synthetic_invalidation.Union(exposed_rect);
397 if (min_toss_y < tiling_.num_tiles_y()) {
398 // The same thing occurs here as in the case above, but the invalidation
399 // rect is the bounding box around the bottom row of tiles in the min
400 // pile. This would be tiles {o,r,u,x,a,d,g,j} in the above picture.
402 int top = tiling_.TilePositionY(min_toss_y);
403 int bottom = top + tiling_.TileSizeY(min_toss_y);
404 int left = min_tiling_rect_over_tiles.x();
405 int right = min_tiling_rect_over_tiles.right();
407 int top_until = std::min(interest_rect_over_tiles.y(), bottom);
408 int bottom_until = std::max(interest_rect_over_tiles.bottom(), top);
409 int left_until = std::min(interest_rect_over_tiles.x(), right);
410 int right_until = std::max(interest_rect_over_tiles.right(), left);
412 int exposed_top = min_tiling_size.height();
413 int exposed_top_until = max_tiling_size.height();
414 int exposed_left = left;
415 int exposed_right = max_tiling_size.width();
416 DCHECK_GE(exposed_top, top);
418 gfx::Rect left_rect(left, top, left_until - left, bottom - top);
419 gfx::Rect right_rect(right_until, top, right - right_until, bottom - top);
420 gfx::Rect top_rect(left, top, right - left, top_until - top);
421 gfx::Rect bottom_rect(
422 left, bottom_until, right - left, bottom - bottom_until);
423 gfx::Rect exposed_rect(exposed_left,
424 exposed_top,
425 exposed_right - exposed_left,
426 exposed_top_until - exposed_top);
427 synthetic_invalidation.Union(left_rect);
428 synthetic_invalidation.Union(right_rect);
429 synthetic_invalidation.Union(top_rect);
430 synthetic_invalidation.Union(bottom_rect);
431 synthetic_invalidation.Union(exposed_rect);
435 // Detect cases where the full pile is invalidated, in this situation we
436 // can just drop/invalidate everything.
437 if (invalidation->Contains(gfx::Rect(old_tiling_size)) ||
438 invalidation->Contains(gfx::Rect(GetSize()))) {
439 updated = !picture_map_.empty();
440 picture_map_.clear();
441 } else {
442 // Expand invalidation that is on tiles that aren't in the interest rect and
443 // will not be re-recorded below. These tiles are no longer valid and should
444 // be considerered fully invalid, so we can know to not keep around raster
445 // tiles that intersect with these recording tiles.
446 Region invalidation_expanded_to_full_tiles;
448 for (Region::Iterator i(*invalidation); i.has_rect(); i.next()) {
449 gfx::Rect invalid_rect = i.rect();
451 // This rect covers the bounds (excluding borders) of all tiles whose
452 // bounds (including borders) touch the |interest_rect|. This matches
453 // the iteration of the |invalid_rect| below which includes borders when
454 // calling Invalidate() on pictures.
455 gfx::Rect invalid_rect_outside_interest_rect_tiles =
456 tiling_.ExpandRectToTileBounds(invalid_rect);
457 // We subtract the |interest_rect_over_tiles| which represents the bounds
458 // of tiles that will be re-recorded below. This matches the iteration of
459 // |interest_rect| below which includes borders.
460 // TODO(danakj): We should have a Rect-subtract-Rect-to-2-rects operator
461 // instead of using Rect::Subtract which gives you the bounding box of the
462 // subtraction.
463 invalid_rect_outside_interest_rect_tiles.Subtract(
464 interest_rect_over_tiles);
465 invalidation_expanded_to_full_tiles.Union(
466 invalid_rect_outside_interest_rect_tiles);
468 // Split this inflated invalidation across tile boundaries and apply it
469 // to all tiles that it touches.
470 bool include_borders = true;
471 for (TilingData::Iterator iter(&tiling_, invalid_rect, include_borders);
472 iter;
473 ++iter) {
474 const PictureMapKey& key = iter.index();
476 PictureMap::iterator picture_it = picture_map_.find(key);
477 if (picture_it == picture_map_.end())
478 continue;
480 updated = true;
481 picture_map_.erase(key);
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 std::vector<gfx::Rect>* invalid_tiles) {
500 // Make a list of all invalid tiles; we will attempt to
501 // cluster these into multiple invalidation regions.
502 bool include_borders = true;
503 for (TilingData::Iterator it(&tiling_, interest_rect, include_borders); it;
504 ++it) {
505 const PictureMapKey& key = it.index();
506 if (picture_map_.find(key) == picture_map_.end())
507 invalid_tiles->push_back(tiling_.TileBounds(key.first, key.second));
511 void PicturePile::CreatePictures(ContentLayerClient* painter,
512 RecordingSource::RecordingMode recording_mode,
513 const std::vector<gfx::Rect>& record_rects) {
514 for (const auto& record_rect : record_rects) {
515 gfx::Rect padded_record_rect = PadRect(record_rect);
517 int repeat_count = std::max(1, slow_down_raster_scale_factor_for_debug_);
518 scoped_refptr<Picture> picture;
520 for (int i = 0; i < repeat_count; i++) {
521 picture = Picture::Create(padded_record_rect, painter, tile_grid_size_,
522 gather_pixel_refs_, recording_mode);
523 // Note the '&&' with previous is-suitable state.
524 // This means that once a picture-pile becomes unsuitable for gpu
525 // rasterization due to some content, it will continue to be unsuitable
526 // even if that content is replaced by gpu-friendly content.
527 // This is an optimization to avoid iterating though all pictures in
528 // the pile after each invalidation.
529 if (is_suitable_for_gpu_rasterization_) {
530 const char* reason = nullptr;
531 is_suitable_for_gpu_rasterization_ &=
532 picture->IsSuitableForGpuRasterization(&reason);
534 if (!is_suitable_for_gpu_rasterization_) {
535 TRACE_EVENT_INSTANT1("cc", "GPU Rasterization Veto",
536 TRACE_EVENT_SCOPE_THREAD, "reason", reason);
541 bool found_tile_for_recorded_picture = false;
543 bool include_borders = true;
544 for (TilingData::Iterator it(&tiling_, padded_record_rect, include_borders);
545 it; ++it) {
546 const PictureMapKey& key = it.index();
547 gfx::Rect tile = PaddedRect(key);
548 if (padded_record_rect.Contains(tile)) {
549 picture_map_[key] = picture;
550 found_tile_for_recorded_picture = true;
553 DCHECK(found_tile_for_recorded_picture);
557 scoped_refptr<RasterSource> PicturePile::CreateRasterSource(
558 bool can_use_lcd_text) const {
559 return scoped_refptr<RasterSource>(
560 PicturePileImpl::CreateFromPicturePile(this, can_use_lcd_text));
563 gfx::Size PicturePile::GetSize() const {
564 return tiling_.tiling_size();
567 void PicturePile::SetEmptyBounds() {
568 tiling_.SetTilingSize(gfx::Size());
569 Clear();
572 void PicturePile::SetMinContentsScale(float min_contents_scale) {
573 DCHECK(min_contents_scale);
574 if (min_contents_scale_ == min_contents_scale)
575 return;
577 // Picture contents are played back scaled. When the final contents scale is
578 // less than 1 (i.e. low res), then multiple recorded pixels will be used
579 // to raster one final pixel. To avoid splitting a final pixel across
580 // pictures (which would result in incorrect rasterization due to blending), a
581 // buffer margin is added so that any picture can be snapped to integral
582 // final pixels.
584 // For example, if a 1/4 contents scale is used, then that would be 3 buffer
585 // pixels, since that's the minimum number of pixels to add so that resulting
586 // content can be snapped to a four pixel aligned grid.
587 int buffer_pixels = static_cast<int>(ceil(1 / min_contents_scale) - 1);
588 buffer_pixels = std::max(0, buffer_pixels);
589 SetBufferPixels(buffer_pixels);
590 min_contents_scale_ = min_contents_scale;
593 void PicturePile::SetSlowdownRasterScaleFactor(int factor) {
594 slow_down_raster_scale_factor_for_debug_ = factor;
597 void PicturePile::SetGatherPixelRefs(bool gather_pixel_refs) {
598 gather_pixel_refs_ = gather_pixel_refs;
601 void PicturePile::SetBackgroundColor(SkColor background_color) {
602 background_color_ = background_color;
605 void PicturePile::SetRequiresClear(bool requires_clear) {
606 requires_clear_ = requires_clear;
609 bool PicturePile::IsSuitableForGpuRasterization() const {
610 return is_suitable_for_gpu_rasterization_;
613 void PicturePile::SetTileGridSize(const gfx::Size& tile_grid_size) {
614 DCHECK_GT(tile_grid_size.width(), 0);
615 DCHECK_GT(tile_grid_size.height(), 0);
617 tile_grid_size_ = tile_grid_size;
620 void PicturePile::SetUnsuitableForGpuRasterizationForTesting() {
621 is_suitable_for_gpu_rasterization_ = false;
624 gfx::Size PicturePile::GetTileGridSizeForTesting() const {
625 return tile_grid_size_;
628 bool PicturePile::CanRasterSlowTileCheck(const gfx::Rect& layer_rect) const {
629 bool include_borders = false;
630 for (TilingData::Iterator tile_iter(&tiling_, layer_rect, include_borders);
631 tile_iter; ++tile_iter) {
632 PictureMap::const_iterator map_iter = picture_map_.find(tile_iter.index());
633 if (map_iter == picture_map_.end())
634 return false;
636 return true;
639 void PicturePile::DetermineIfSolidColor() {
640 is_solid_color_ = false;
641 solid_color_ = SK_ColorTRANSPARENT;
643 if (picture_map_.empty()) {
644 return;
647 PictureMap::const_iterator it = picture_map_.begin();
648 const Picture* picture = it->second.get();
650 // Missing recordings due to frequent invalidations or being too far away
651 // from the interest rect will cause the a null picture to exist.
652 if (!picture)
653 return;
655 // Don't bother doing more work if the first image is too complicated.
656 if (picture->ApproximateOpCount() > kOpCountThatIsOkToAnalyze)
657 return;
659 // Make sure all of the mapped images point to the same picture.
660 for (++it; it != picture_map_.end(); ++it) {
661 if (it->second.get() != picture)
662 return;
665 gfx::Size layer_size = GetSize();
666 skia::AnalysisCanvas canvas(layer_size.width(), layer_size.height());
668 picture->Raster(&canvas, nullptr, Region(), 1.0f);
669 is_solid_color_ = canvas.GetColorIfSolid(&solid_color_);
672 gfx::Rect PicturePile::PaddedRect(const PictureMapKey& key) const {
673 gfx::Rect tile = tiling_.TileBounds(key.first, key.second);
674 return PadRect(tile);
677 gfx::Rect PicturePile::PadRect(const gfx::Rect& rect) const {
678 gfx::Rect padded_rect = rect;
679 padded_rect.Inset(-buffer_pixels(), -buffer_pixels(), -buffer_pixels(),
680 -buffer_pixels());
681 return padded_rect;
684 void PicturePile::Clear() {
685 picture_map_.clear();
686 recorded_viewport_ = gfx::Rect();
687 has_any_recordings_ = false;
688 is_solid_color_ = false;
691 void PicturePile::SetBufferPixels(int new_buffer_pixels) {
692 if (new_buffer_pixels == buffer_pixels())
693 return;
695 Clear();
696 tiling_.SetBorderTexels(new_buffer_pixels);
699 } // namespace cc