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