| 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_layer_tiling.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <cmath> | |
| 9 #include <limits> | |
| 10 #include <set> | |
| 11 | |
| 12 #include "base/logging.h" | |
| 13 #include "base/trace_event/trace_event.h" | |
| 14 #include "base/trace_event/trace_event_argument.h" | |
| 15 #include "cc/base/math_util.h" | |
| 16 #include "cc/resources/prioritized_tile.h" | |
| 17 #include "cc/resources/raster_source.h" | |
| 18 #include "cc/resources/tile.h" | |
| 19 #include "cc/resources/tile_priority.h" | |
| 20 #include "ui/gfx/geometry/point_conversions.h" | |
| 21 #include "ui/gfx/geometry/rect_conversions.h" | |
| 22 #include "ui/gfx/geometry/safe_integer_conversions.h" | |
| 23 #include "ui/gfx/geometry/size_conversions.h" | |
| 24 | |
| 25 namespace cc { | |
| 26 namespace { | |
| 27 | |
| 28 const float kSoonBorderDistanceViewportPercentage = 0.15f; | |
| 29 const float kMaxSoonBorderDistanceInScreenPixels = 312.f; | |
| 30 | |
| 31 } // namespace | |
| 32 | |
| 33 scoped_ptr<PictureLayerTiling> PictureLayerTiling::Create( | |
| 34 WhichTree tree, | |
| 35 float contents_scale, | |
| 36 scoped_refptr<RasterSource> raster_source, | |
| 37 PictureLayerTilingClient* client, | |
| 38 float tiling_interest_area_viewport_multiplier, | |
| 39 float skewport_target_time_in_seconds, | |
| 40 int skewport_extrapolation_limit_in_content_pixels) { | |
| 41 return make_scoped_ptr(new PictureLayerTiling( | |
| 42 tree, contents_scale, raster_source, client, | |
| 43 tiling_interest_area_viewport_multiplier, skewport_target_time_in_seconds, | |
| 44 skewport_extrapolation_limit_in_content_pixels)); | |
| 45 } | |
| 46 | |
| 47 PictureLayerTiling::PictureLayerTiling( | |
| 48 WhichTree tree, | |
| 49 float contents_scale, | |
| 50 scoped_refptr<RasterSource> raster_source, | |
| 51 PictureLayerTilingClient* client, | |
| 52 float tiling_interest_area_viewport_multiplier, | |
| 53 float skewport_target_time_in_seconds, | |
| 54 int skewport_extrapolation_limit_in_content_pixels) | |
| 55 : tiling_interest_area_viewport_multiplier_( | |
| 56 tiling_interest_area_viewport_multiplier), | |
| 57 skewport_target_time_in_seconds_(skewport_target_time_in_seconds), | |
| 58 skewport_extrapolation_limit_in_content_pixels_( | |
| 59 skewport_extrapolation_limit_in_content_pixels), | |
| 60 contents_scale_(contents_scale), | |
| 61 client_(client), | |
| 62 tree_(tree), | |
| 63 raster_source_(raster_source), | |
| 64 resolution_(NON_IDEAL_RESOLUTION), | |
| 65 tiling_data_(gfx::Size(), gfx::Size(), kBorderTexels), | |
| 66 can_require_tiles_for_activation_(false), | |
| 67 current_content_to_screen_scale_(0.f), | |
| 68 has_visible_rect_tiles_(false), | |
| 69 has_skewport_rect_tiles_(false), | |
| 70 has_soon_border_rect_tiles_(false), | |
| 71 has_eventually_rect_tiles_(false) { | |
| 72 DCHECK(!raster_source->IsSolidColor()); | |
| 73 gfx::Size content_bounds = gfx::ToCeiledSize( | |
| 74 gfx::ScaleSize(raster_source_->GetSize(), contents_scale)); | |
| 75 gfx::Size tile_size = client_->CalculateTileSize(content_bounds); | |
| 76 | |
| 77 DCHECK(!gfx::ToFlooredSize(gfx::ScaleSize(raster_source_->GetSize(), | |
| 78 contents_scale)).IsEmpty()) | |
| 79 << "Tiling created with scale too small as contents become empty." | |
| 80 << " Layer bounds: " << raster_source_->GetSize().ToString() | |
| 81 << " Contents scale: " << contents_scale; | |
| 82 | |
| 83 tiling_data_.SetTilingSize(content_bounds); | |
| 84 tiling_data_.SetMaxTextureSize(tile_size); | |
| 85 } | |
| 86 | |
| 87 PictureLayerTiling::~PictureLayerTiling() { | |
| 88 } | |
| 89 | |
| 90 // static | |
| 91 float PictureLayerTiling::CalculateSoonBorderDistance( | |
| 92 const gfx::Rect& visible_rect_in_content_space, | |
| 93 float content_to_screen_scale) { | |
| 94 float max_dimension = std::max(visible_rect_in_content_space.width(), | |
| 95 visible_rect_in_content_space.height()); | |
| 96 return std::min( | |
| 97 kMaxSoonBorderDistanceInScreenPixels / content_to_screen_scale, | |
| 98 max_dimension * kSoonBorderDistanceViewportPercentage); | |
| 99 } | |
| 100 | |
| 101 Tile* PictureLayerTiling::CreateTile(int i, int j) { | |
| 102 TileMapKey key(i, j); | |
| 103 DCHECK(tiles_.find(key) == tiles_.end()); | |
| 104 | |
| 105 gfx::Rect paint_rect = tiling_data_.TileBoundsWithBorder(i, j); | |
| 106 gfx::Rect tile_rect = paint_rect; | |
| 107 tile_rect.set_size(tiling_data_.max_texture_size()); | |
| 108 | |
| 109 if (!raster_source_->CoversRect(tile_rect, contents_scale_)) | |
| 110 return nullptr; | |
| 111 | |
| 112 ScopedTilePtr tile = client_->CreateTile(contents_scale_, tile_rect); | |
| 113 Tile* raw_ptr = tile.get(); | |
| 114 tile->set_tiling_index(i, j); | |
| 115 tiles_.add(key, tile.Pass()); | |
| 116 return raw_ptr; | |
| 117 } | |
| 118 | |
| 119 void PictureLayerTiling::CreateMissingTilesInLiveTilesRect() { | |
| 120 bool include_borders = false; | |
| 121 for (TilingData::Iterator iter(&tiling_data_, live_tiles_rect_, | |
| 122 include_borders); | |
| 123 iter; ++iter) { | |
| 124 TileMapKey key = iter.index(); | |
| 125 TileMap::iterator find = tiles_.find(key); | |
| 126 if (find != tiles_.end()) | |
| 127 continue; | |
| 128 | |
| 129 if (ShouldCreateTileAt(key.first, key.second)) | |
| 130 CreateTile(key.first, key.second); | |
| 131 } | |
| 132 VerifyLiveTilesRect(false); | |
| 133 } | |
| 134 | |
| 135 void PictureLayerTiling::TakeTilesAndPropertiesFrom( | |
| 136 PictureLayerTiling* pending_twin, | |
| 137 const Region& layer_invalidation) { | |
| 138 TRACE_EVENT0("cc", "TakeTilesAndPropertiesFrom"); | |
| 139 SetRasterSourceAndResize(pending_twin->raster_source_); | |
| 140 | |
| 141 RemoveTilesInRegion(layer_invalidation, false /* recreate tiles */); | |
| 142 | |
| 143 resolution_ = pending_twin->resolution_; | |
| 144 bool create_missing_tiles = false; | |
| 145 if (live_tiles_rect_.IsEmpty()) { | |
| 146 live_tiles_rect_ = pending_twin->live_tiles_rect(); | |
| 147 create_missing_tiles = true; | |
| 148 } else { | |
| 149 SetLiveTilesRect(pending_twin->live_tiles_rect()); | |
| 150 } | |
| 151 | |
| 152 if (tiles_.empty()) { | |
| 153 tiles_.swap(pending_twin->tiles_); | |
| 154 } else { | |
| 155 while (!pending_twin->tiles_.empty()) { | |
| 156 TileMapKey key = pending_twin->tiles_.begin()->first; | |
| 157 tiles_.set(key, pending_twin->tiles_.take_and_erase(key)); | |
| 158 } | |
| 159 } | |
| 160 DCHECK(pending_twin->tiles_.empty()); | |
| 161 | |
| 162 if (create_missing_tiles) | |
| 163 CreateMissingTilesInLiveTilesRect(); | |
| 164 | |
| 165 VerifyLiveTilesRect(false); | |
| 166 | |
| 167 SetTilePriorityRects(pending_twin->current_content_to_screen_scale_, | |
| 168 pending_twin->current_visible_rect_, | |
| 169 pending_twin->current_skewport_rect_, | |
| 170 pending_twin->current_soon_border_rect_, | |
| 171 pending_twin->current_eventually_rect_, | |
| 172 pending_twin->current_occlusion_in_layer_space_); | |
| 173 } | |
| 174 | |
| 175 void PictureLayerTiling::SetRasterSourceAndResize( | |
| 176 scoped_refptr<RasterSource> raster_source) { | |
| 177 DCHECK(!raster_source->IsSolidColor()); | |
| 178 gfx::Size old_layer_bounds = raster_source_->GetSize(); | |
| 179 raster_source_.swap(raster_source); | |
| 180 gfx::Size new_layer_bounds = raster_source_->GetSize(); | |
| 181 gfx::Size content_bounds = | |
| 182 gfx::ToCeiledSize(gfx::ScaleSize(new_layer_bounds, contents_scale_)); | |
| 183 gfx::Size tile_size = client_->CalculateTileSize(content_bounds); | |
| 184 | |
| 185 if (tile_size != tiling_data_.max_texture_size()) { | |
| 186 tiling_data_.SetTilingSize(content_bounds); | |
| 187 tiling_data_.SetMaxTextureSize(tile_size); | |
| 188 // When the tile size changes, the TilingData positions no longer work | |
| 189 // as valid keys to the TileMap, so just drop all tiles and clear the live | |
| 190 // tiles rect. | |
| 191 Reset(); | |
| 192 return; | |
| 193 } | |
| 194 | |
| 195 if (old_layer_bounds == new_layer_bounds) | |
| 196 return; | |
| 197 | |
| 198 // The SetLiveTilesRect() method would drop tiles outside the new bounds, | |
| 199 // but may do so incorrectly if resizing the tiling causes the number of | |
| 200 // tiles in the tiling_data_ to change. | |
| 201 gfx::Rect content_rect(content_bounds); | |
| 202 int before_left = tiling_data_.TileXIndexFromSrcCoord(live_tiles_rect_.x()); | |
| 203 int before_top = tiling_data_.TileYIndexFromSrcCoord(live_tiles_rect_.y()); | |
| 204 int before_right = | |
| 205 tiling_data_.TileXIndexFromSrcCoord(live_tiles_rect_.right() - 1); | |
| 206 int before_bottom = | |
| 207 tiling_data_.TileYIndexFromSrcCoord(live_tiles_rect_.bottom() - 1); | |
| 208 | |
| 209 // The live_tiles_rect_ is clamped to stay within the tiling size as we | |
| 210 // change it. | |
| 211 live_tiles_rect_.Intersect(content_rect); | |
| 212 tiling_data_.SetTilingSize(content_bounds); | |
| 213 | |
| 214 int after_right = -1; | |
| 215 int after_bottom = -1; | |
| 216 if (!live_tiles_rect_.IsEmpty()) { | |
| 217 after_right = | |
| 218 tiling_data_.TileXIndexFromSrcCoord(live_tiles_rect_.right() - 1); | |
| 219 after_bottom = | |
| 220 tiling_data_.TileYIndexFromSrcCoord(live_tiles_rect_.bottom() - 1); | |
| 221 } | |
| 222 | |
| 223 // There is no recycled twin since this is run on the pending tiling | |
| 224 // during commit, and on the active tree during activate. | |
| 225 // Drop tiles outside the new layer bounds if the layer shrank. | |
| 226 for (int i = after_right + 1; i <= before_right; ++i) { | |
| 227 for (int j = before_top; j <= before_bottom; ++j) | |
| 228 RemoveTileAt(i, j); | |
| 229 } | |
| 230 for (int i = before_left; i <= after_right; ++i) { | |
| 231 for (int j = after_bottom + 1; j <= before_bottom; ++j) | |
| 232 RemoveTileAt(i, j); | |
| 233 } | |
| 234 | |
| 235 if (after_right > before_right) { | |
| 236 DCHECK_EQ(after_right, before_right + 1); | |
| 237 for (int j = before_top; j <= after_bottom; ++j) { | |
| 238 if (ShouldCreateTileAt(after_right, j)) | |
| 239 CreateTile(after_right, j); | |
| 240 } | |
| 241 } | |
| 242 if (after_bottom > before_bottom) { | |
| 243 DCHECK_EQ(after_bottom, before_bottom + 1); | |
| 244 for (int i = before_left; i <= before_right; ++i) { | |
| 245 if (ShouldCreateTileAt(i, after_bottom)) | |
| 246 CreateTile(i, after_bottom); | |
| 247 } | |
| 248 } | |
| 249 } | |
| 250 | |
| 251 void PictureLayerTiling::Invalidate(const Region& layer_invalidation) { | |
| 252 DCHECK_IMPLIES(tree_ == ACTIVE_TREE, | |
| 253 !client_->GetPendingOrActiveTwinTiling(this)); | |
| 254 RemoveTilesInRegion(layer_invalidation, true /* recreate tiles */); | |
| 255 } | |
| 256 | |
| 257 void PictureLayerTiling::RemoveTilesInRegion(const Region& layer_invalidation, | |
| 258 bool recreate_tiles) { | |
| 259 // We only invalidate the active tiling when it's orphaned: it has no pending | |
| 260 // twin, so it's slated for removal in the future. | |
| 261 if (live_tiles_rect_.IsEmpty()) | |
| 262 return; | |
| 263 std::vector<TileMapKey> new_tile_keys; | |
| 264 gfx::Rect expanded_live_tiles_rect = | |
| 265 tiling_data_.ExpandRectIgnoringBordersToTileBounds(live_tiles_rect_); | |
| 266 for (Region::Iterator iter(layer_invalidation); iter.has_rect(); | |
| 267 iter.next()) { | |
| 268 gfx::Rect layer_rect = iter.rect(); | |
| 269 gfx::Rect content_rect = | |
| 270 gfx::ScaleToEnclosingRect(layer_rect, contents_scale_); | |
| 271 // Consider tiles inside the live tiles rect even if only their border | |
| 272 // pixels intersect the invalidation. But don't consider tiles outside | |
| 273 // the live tiles rect with the same conditions, as they won't exist. | |
| 274 int border_pixels = tiling_data_.border_texels(); | |
| 275 content_rect.Inset(-border_pixels, -border_pixels); | |
| 276 // Avoid needless work by not bothering to invalidate where there aren't | |
| 277 // tiles. | |
| 278 content_rect.Intersect(expanded_live_tiles_rect); | |
| 279 if (content_rect.IsEmpty()) | |
| 280 continue; | |
| 281 // Since the content_rect includes border pixels already, don't include | |
| 282 // borders when iterating to avoid double counting them. | |
| 283 bool include_borders = false; | |
| 284 for ( | |
| 285 TilingData::Iterator iter(&tiling_data_, content_rect, include_borders); | |
| 286 iter; ++iter) { | |
| 287 if (RemoveTileAt(iter.index_x(), iter.index_y())) { | |
| 288 if (recreate_tiles) | |
| 289 new_tile_keys.push_back(iter.index()); | |
| 290 } | |
| 291 } | |
| 292 } | |
| 293 | |
| 294 for (const auto& key : new_tile_keys) | |
| 295 CreateTile(key.first, key.second); | |
| 296 } | |
| 297 | |
| 298 bool PictureLayerTiling::ShouldCreateTileAt(int i, int j) const { | |
| 299 // Active tree should always create a tile. The reason for this is that active | |
| 300 // tree represents content that we draw on screen, which means that whenever | |
| 301 // we check whether a tile should exist somewhere, the answer is yes. This | |
| 302 // doesn't mean it will actually be created (if raster source doesn't cover | |
| 303 // the tile for instance). Pending tree, on the other hand, should only be | |
| 304 // creating tiles that are different from the current active tree, which is | |
| 305 // represented by the logic in the rest of the function. | |
| 306 if (tree_ == ACTIVE_TREE) | |
| 307 return true; | |
| 308 | |
| 309 // If the pending tree has no active twin, then it needs to create all tiles. | |
| 310 const PictureLayerTiling* active_twin = | |
| 311 client_->GetPendingOrActiveTwinTiling(this); | |
| 312 if (!active_twin) | |
| 313 return true; | |
| 314 | |
| 315 // Pending tree will override the entire active tree if indices don't match. | |
| 316 if (!TilingMatchesTileIndices(active_twin)) | |
| 317 return true; | |
| 318 | |
| 319 gfx::Rect paint_rect = tiling_data_.TileBoundsWithBorder(i, j); | |
| 320 gfx::Rect tile_rect = paint_rect; | |
| 321 tile_rect.set_size(tiling_data_.max_texture_size()); | |
| 322 | |
| 323 // If the active tree can't create a tile, because of its raster source, then | |
| 324 // the pending tree should create one. | |
| 325 if (!active_twin->raster_source()->CoversRect(tile_rect, contents_scale())) | |
| 326 return true; | |
| 327 | |
| 328 const Region* layer_invalidation = client_->GetPendingInvalidation(); | |
| 329 gfx::Rect layer_rect = | |
| 330 gfx::ScaleToEnclosingRect(tile_rect, 1.f / contents_scale()); | |
| 331 | |
| 332 // If this tile is invalidated, then the pending tree should create one. | |
| 333 if (layer_invalidation && layer_invalidation->Intersects(layer_rect)) | |
| 334 return true; | |
| 335 | |
| 336 // If the active tree doesn't have a tile here, but it's in the pending tree's | |
| 337 // visible rect, then the pending tree should create a tile. This can happen | |
| 338 // if the pending visible rect is outside of the active tree's live tiles | |
| 339 // rect. In those situations, we need to block activation until we're ready to | |
| 340 // display content, which will have to come from the pending tree. | |
| 341 if (!active_twin->TileAt(i, j) && current_visible_rect_.Intersects(tile_rect)) | |
| 342 return true; | |
| 343 | |
| 344 // In all other cases, the pending tree doesn't need to create a tile. | |
| 345 return false; | |
| 346 } | |
| 347 | |
| 348 bool PictureLayerTiling::TilingMatchesTileIndices( | |
| 349 const PictureLayerTiling* twin) const { | |
| 350 return tiling_data_.max_texture_size() == | |
| 351 twin->tiling_data_.max_texture_size(); | |
| 352 } | |
| 353 | |
| 354 PictureLayerTiling::CoverageIterator::CoverageIterator() | |
| 355 : tiling_(NULL), | |
| 356 current_tile_(NULL), | |
| 357 tile_i_(0), | |
| 358 tile_j_(0), | |
| 359 left_(0), | |
| 360 top_(0), | |
| 361 right_(-1), | |
| 362 bottom_(-1) { | |
| 363 } | |
| 364 | |
| 365 PictureLayerTiling::CoverageIterator::CoverageIterator( | |
| 366 const PictureLayerTiling* tiling, | |
| 367 float dest_scale, | |
| 368 const gfx::Rect& dest_rect) | |
| 369 : tiling_(tiling), | |
| 370 dest_rect_(dest_rect), | |
| 371 dest_to_content_scale_(0), | |
| 372 current_tile_(NULL), | |
| 373 tile_i_(0), | |
| 374 tile_j_(0), | |
| 375 left_(0), | |
| 376 top_(0), | |
| 377 right_(-1), | |
| 378 bottom_(-1) { | |
| 379 DCHECK(tiling_); | |
| 380 if (dest_rect_.IsEmpty()) | |
| 381 return; | |
| 382 | |
| 383 dest_to_content_scale_ = tiling_->contents_scale_ / dest_scale; | |
| 384 | |
| 385 gfx::Rect content_rect = | |
| 386 gfx::ScaleToEnclosingRect(dest_rect_, | |
| 387 dest_to_content_scale_, | |
| 388 dest_to_content_scale_); | |
| 389 // IndexFromSrcCoord clamps to valid tile ranges, so it's necessary to | |
| 390 // check for non-intersection first. | |
| 391 content_rect.Intersect(gfx::Rect(tiling_->tiling_size())); | |
| 392 if (content_rect.IsEmpty()) | |
| 393 return; | |
| 394 | |
| 395 left_ = tiling_->tiling_data_.TileXIndexFromSrcCoord(content_rect.x()); | |
| 396 top_ = tiling_->tiling_data_.TileYIndexFromSrcCoord(content_rect.y()); | |
| 397 right_ = tiling_->tiling_data_.TileXIndexFromSrcCoord( | |
| 398 content_rect.right() - 1); | |
| 399 bottom_ = tiling_->tiling_data_.TileYIndexFromSrcCoord( | |
| 400 content_rect.bottom() - 1); | |
| 401 | |
| 402 tile_i_ = left_ - 1; | |
| 403 tile_j_ = top_; | |
| 404 ++(*this); | |
| 405 } | |
| 406 | |
| 407 PictureLayerTiling::CoverageIterator::~CoverageIterator() { | |
| 408 } | |
| 409 | |
| 410 PictureLayerTiling::CoverageIterator& | |
| 411 PictureLayerTiling::CoverageIterator::operator++() { | |
| 412 if (tile_j_ > bottom_) | |
| 413 return *this; | |
| 414 | |
| 415 bool first_time = tile_i_ < left_; | |
| 416 bool new_row = false; | |
| 417 tile_i_++; | |
| 418 if (tile_i_ > right_) { | |
| 419 tile_i_ = left_; | |
| 420 tile_j_++; | |
| 421 new_row = true; | |
| 422 if (tile_j_ > bottom_) { | |
| 423 current_tile_ = NULL; | |
| 424 return *this; | |
| 425 } | |
| 426 } | |
| 427 | |
| 428 current_tile_ = tiling_->TileAt(tile_i_, tile_j_); | |
| 429 | |
| 430 // Calculate the current geometry rect. Due to floating point rounding | |
| 431 // and ToEnclosingRect, tiles might overlap in destination space on the | |
| 432 // edges. | |
| 433 gfx::Rect last_geometry_rect = current_geometry_rect_; | |
| 434 | |
| 435 gfx::Rect content_rect = tiling_->tiling_data_.TileBounds(tile_i_, tile_j_); | |
| 436 | |
| 437 current_geometry_rect_ = | |
| 438 gfx::ScaleToEnclosingRect(content_rect, | |
| 439 1 / dest_to_content_scale_, | |
| 440 1 / dest_to_content_scale_); | |
| 441 | |
| 442 current_geometry_rect_.Intersect(dest_rect_); | |
| 443 | |
| 444 if (first_time) | |
| 445 return *this; | |
| 446 | |
| 447 // Iteration happens left->right, top->bottom. Running off the bottom-right | |
| 448 // edge is handled by the intersection above with dest_rect_. Here we make | |
| 449 // sure that the new current geometry rect doesn't overlap with the last. | |
| 450 int min_left; | |
| 451 int min_top; | |
| 452 if (new_row) { | |
| 453 min_left = dest_rect_.x(); | |
| 454 min_top = last_geometry_rect.bottom(); | |
| 455 } else { | |
| 456 min_left = last_geometry_rect.right(); | |
| 457 min_top = last_geometry_rect.y(); | |
| 458 } | |
| 459 | |
| 460 int inset_left = std::max(0, min_left - current_geometry_rect_.x()); | |
| 461 int inset_top = std::max(0, min_top - current_geometry_rect_.y()); | |
| 462 current_geometry_rect_.Inset(inset_left, inset_top, 0, 0); | |
| 463 | |
| 464 if (!new_row) { | |
| 465 DCHECK_EQ(last_geometry_rect.right(), current_geometry_rect_.x()); | |
| 466 DCHECK_EQ(last_geometry_rect.bottom(), current_geometry_rect_.bottom()); | |
| 467 DCHECK_EQ(last_geometry_rect.y(), current_geometry_rect_.y()); | |
| 468 } | |
| 469 | |
| 470 return *this; | |
| 471 } | |
| 472 | |
| 473 gfx::Rect PictureLayerTiling::CoverageIterator::geometry_rect() const { | |
| 474 return current_geometry_rect_; | |
| 475 } | |
| 476 | |
| 477 gfx::RectF PictureLayerTiling::CoverageIterator::texture_rect() const { | |
| 478 gfx::PointF tex_origin = | |
| 479 tiling_->tiling_data_.TileBoundsWithBorder(tile_i_, tile_j_).origin(); | |
| 480 | |
| 481 // Convert from dest space => content space => texture space. | |
| 482 gfx::RectF texture_rect(current_geometry_rect_); | |
| 483 texture_rect.Scale(dest_to_content_scale_, | |
| 484 dest_to_content_scale_); | |
| 485 texture_rect.Intersect(gfx::Rect(tiling_->tiling_size())); | |
| 486 if (texture_rect.IsEmpty()) | |
| 487 return texture_rect; | |
| 488 texture_rect.Offset(-tex_origin.OffsetFromOrigin()); | |
| 489 | |
| 490 return texture_rect; | |
| 491 } | |
| 492 | |
| 493 bool PictureLayerTiling::RemoveTileAt(int i, int j) { | |
| 494 TileMap::iterator found = tiles_.find(TileMapKey(i, j)); | |
| 495 if (found == tiles_.end()) | |
| 496 return false; | |
| 497 tiles_.erase(found); | |
| 498 return true; | |
| 499 } | |
| 500 | |
| 501 void PictureLayerTiling::Reset() { | |
| 502 live_tiles_rect_ = gfx::Rect(); | |
| 503 tiles_.clear(); | |
| 504 } | |
| 505 | |
| 506 gfx::Rect PictureLayerTiling::ComputeSkewport( | |
| 507 double current_frame_time_in_seconds, | |
| 508 const gfx::Rect& visible_rect_in_content_space) const { | |
| 509 gfx::Rect skewport = visible_rect_in_content_space; | |
| 510 if (skewport.IsEmpty()) | |
| 511 return skewport; | |
| 512 | |
| 513 if (visible_rect_history_[1].frame_time_in_seconds == 0.0) | |
| 514 return skewport; | |
| 515 | |
| 516 double time_delta = current_frame_time_in_seconds - | |
| 517 visible_rect_history_[1].frame_time_in_seconds; | |
| 518 if (time_delta == 0.0) | |
| 519 return skewport; | |
| 520 | |
| 521 double extrapolation_multiplier = | |
| 522 skewport_target_time_in_seconds_ / time_delta; | |
| 523 | |
| 524 int old_x = visible_rect_history_[1].visible_rect_in_content_space.x(); | |
| 525 int old_y = visible_rect_history_[1].visible_rect_in_content_space.y(); | |
| 526 int old_right = | |
| 527 visible_rect_history_[1].visible_rect_in_content_space.right(); | |
| 528 int old_bottom = | |
| 529 visible_rect_history_[1].visible_rect_in_content_space.bottom(); | |
| 530 | |
| 531 int new_x = visible_rect_in_content_space.x(); | |
| 532 int new_y = visible_rect_in_content_space.y(); | |
| 533 int new_right = visible_rect_in_content_space.right(); | |
| 534 int new_bottom = visible_rect_in_content_space.bottom(); | |
| 535 | |
| 536 // Compute the maximum skewport based on | |
| 537 // |skewport_extrapolation_limit_in_content_pixels_|. | |
| 538 gfx::Rect max_skewport = skewport; | |
| 539 max_skewport.Inset(-skewport_extrapolation_limit_in_content_pixels_, | |
| 540 -skewport_extrapolation_limit_in_content_pixels_); | |
| 541 | |
| 542 // Inset the skewport by the needed adjustment. | |
| 543 skewport.Inset(extrapolation_multiplier * (new_x - old_x), | |
| 544 extrapolation_multiplier * (new_y - old_y), | |
| 545 extrapolation_multiplier * (old_right - new_right), | |
| 546 extrapolation_multiplier * (old_bottom - new_bottom)); | |
| 547 | |
| 548 // Ensure that visible rect is contained in the skewport. | |
| 549 skewport.Union(visible_rect_in_content_space); | |
| 550 | |
| 551 // Clip the skewport to |max_skewport|. This needs to happen after the | |
| 552 // union in case intersecting would have left the empty rect. | |
| 553 skewport.Intersect(max_skewport); | |
| 554 | |
| 555 return skewport; | |
| 556 } | |
| 557 | |
| 558 bool PictureLayerTiling::ComputeTilePriorityRects( | |
| 559 const gfx::Rect& viewport_in_layer_space, | |
| 560 float ideal_contents_scale, | |
| 561 double current_frame_time_in_seconds, | |
| 562 const Occlusion& occlusion_in_layer_space) { | |
| 563 if (!NeedsUpdateForFrameAtTimeAndViewport(current_frame_time_in_seconds, | |
| 564 viewport_in_layer_space)) { | |
| 565 // This should never be zero for the purposes of has_ever_been_updated(). | |
| 566 DCHECK_NE(current_frame_time_in_seconds, 0.0); | |
| 567 return false; | |
| 568 } | |
| 569 gfx::Rect visible_rect_in_content_space = | |
| 570 gfx::ScaleToEnclosingRect(viewport_in_layer_space, contents_scale_); | |
| 571 | |
| 572 if (tiling_size().IsEmpty()) { | |
| 573 UpdateVisibleRectHistory(current_frame_time_in_seconds, | |
| 574 visible_rect_in_content_space); | |
| 575 last_viewport_in_layer_space_ = viewport_in_layer_space; | |
| 576 return false; | |
| 577 } | |
| 578 | |
| 579 // Calculate the skewport. | |
| 580 gfx::Rect skewport = ComputeSkewport(current_frame_time_in_seconds, | |
| 581 visible_rect_in_content_space); | |
| 582 DCHECK(skewport.Contains(visible_rect_in_content_space)); | |
| 583 | |
| 584 // Calculate the eventually/live tiles rect. | |
| 585 int64 eventually_rect_area = tiling_interest_area_viewport_multiplier_ * | |
| 586 visible_rect_in_content_space.width() * | |
| 587 visible_rect_in_content_space.height(); | |
| 588 | |
| 589 gfx::Rect eventually_rect = | |
| 590 ExpandRectEquallyToAreaBoundedBy(visible_rect_in_content_space, | |
| 591 eventually_rect_area, | |
| 592 gfx::Rect(tiling_size()), | |
| 593 &expansion_cache_); | |
| 594 | |
| 595 DCHECK(eventually_rect.IsEmpty() || | |
| 596 gfx::Rect(tiling_size()).Contains(eventually_rect)) | |
| 597 << "tiling_size: " << tiling_size().ToString() | |
| 598 << " eventually_rect: " << eventually_rect.ToString(); | |
| 599 | |
| 600 // Calculate the soon border rect. | |
| 601 float content_to_screen_scale = ideal_contents_scale / contents_scale_; | |
| 602 gfx::Rect soon_border_rect = visible_rect_in_content_space; | |
| 603 float border = CalculateSoonBorderDistance(visible_rect_in_content_space, | |
| 604 content_to_screen_scale); | |
| 605 soon_border_rect.Inset(-border, -border, -border, -border); | |
| 606 | |
| 607 UpdateVisibleRectHistory(current_frame_time_in_seconds, | |
| 608 visible_rect_in_content_space); | |
| 609 last_viewport_in_layer_space_ = viewport_in_layer_space; | |
| 610 | |
| 611 SetTilePriorityRects(content_to_screen_scale, visible_rect_in_content_space, | |
| 612 skewport, soon_border_rect, eventually_rect, | |
| 613 occlusion_in_layer_space); | |
| 614 SetLiveTilesRect(eventually_rect); | |
| 615 return true; | |
| 616 } | |
| 617 | |
| 618 void PictureLayerTiling::SetTilePriorityRects( | |
| 619 float content_to_screen_scale, | |
| 620 const gfx::Rect& visible_rect_in_content_space, | |
| 621 const gfx::Rect& skewport, | |
| 622 const gfx::Rect& soon_border_rect, | |
| 623 const gfx::Rect& eventually_rect, | |
| 624 const Occlusion& occlusion_in_layer_space) { | |
| 625 current_visible_rect_ = visible_rect_in_content_space; | |
| 626 current_skewport_rect_ = skewport; | |
| 627 current_soon_border_rect_ = soon_border_rect; | |
| 628 current_eventually_rect_ = eventually_rect; | |
| 629 current_occlusion_in_layer_space_ = occlusion_in_layer_space; | |
| 630 current_content_to_screen_scale_ = content_to_screen_scale; | |
| 631 | |
| 632 gfx::Rect tiling_rect(tiling_size()); | |
| 633 has_visible_rect_tiles_ = tiling_rect.Intersects(current_visible_rect_); | |
| 634 has_skewport_rect_tiles_ = tiling_rect.Intersects(current_skewport_rect_); | |
| 635 has_soon_border_rect_tiles_ = | |
| 636 tiling_rect.Intersects(current_soon_border_rect_); | |
| 637 has_eventually_rect_tiles_ = tiling_rect.Intersects(current_eventually_rect_); | |
| 638 } | |
| 639 | |
| 640 void PictureLayerTiling::SetLiveTilesRect( | |
| 641 const gfx::Rect& new_live_tiles_rect) { | |
| 642 DCHECK(new_live_tiles_rect.IsEmpty() || | |
| 643 gfx::Rect(tiling_size()).Contains(new_live_tiles_rect)) | |
| 644 << "tiling_size: " << tiling_size().ToString() | |
| 645 << " new_live_tiles_rect: " << new_live_tiles_rect.ToString(); | |
| 646 if (live_tiles_rect_ == new_live_tiles_rect) | |
| 647 return; | |
| 648 | |
| 649 // Iterate to delete all tiles outside of our new live_tiles rect. | |
| 650 for (TilingData::DifferenceIterator iter(&tiling_data_, live_tiles_rect_, | |
| 651 new_live_tiles_rect); | |
| 652 iter; ++iter) { | |
| 653 RemoveTileAt(iter.index_x(), iter.index_y()); | |
| 654 } | |
| 655 | |
| 656 // Iterate to allocate new tiles for all regions with newly exposed area. | |
| 657 for (TilingData::DifferenceIterator iter(&tiling_data_, new_live_tiles_rect, | |
| 658 live_tiles_rect_); | |
| 659 iter; ++iter) { | |
| 660 TileMapKey key(iter.index()); | |
| 661 if (ShouldCreateTileAt(key.first, key.second)) | |
| 662 CreateTile(key.first, key.second); | |
| 663 } | |
| 664 | |
| 665 live_tiles_rect_ = new_live_tiles_rect; | |
| 666 VerifyLiveTilesRect(false); | |
| 667 } | |
| 668 | |
| 669 void PictureLayerTiling::VerifyLiveTilesRect(bool is_on_recycle_tree) const { | |
| 670 #if DCHECK_IS_ON() | |
| 671 for (auto it = tiles_.begin(); it != tiles_.end(); ++it) { | |
| 672 if (!it->second) | |
| 673 continue; | |
| 674 DCHECK(it->first.first < tiling_data_.num_tiles_x()) | |
| 675 << this << " " << it->first.first << "," << it->first.second | |
| 676 << " num_tiles_x " << tiling_data_.num_tiles_x() << " live_tiles_rect " | |
| 677 << live_tiles_rect_.ToString(); | |
| 678 DCHECK(it->first.second < tiling_data_.num_tiles_y()) | |
| 679 << this << " " << it->first.first << "," << it->first.second | |
| 680 << " num_tiles_y " << tiling_data_.num_tiles_y() << " live_tiles_rect " | |
| 681 << live_tiles_rect_.ToString(); | |
| 682 DCHECK(tiling_data_.TileBounds(it->first.first, it->first.second) | |
| 683 .Intersects(live_tiles_rect_)) | |
| 684 << this << " " << it->first.first << "," << it->first.second | |
| 685 << " tile bounds " | |
| 686 << tiling_data_.TileBounds(it->first.first, it->first.second).ToString() | |
| 687 << " live_tiles_rect " << live_tiles_rect_.ToString(); | |
| 688 } | |
| 689 #endif | |
| 690 } | |
| 691 | |
| 692 bool PictureLayerTiling::IsTileOccluded(const Tile* tile) const { | |
| 693 // If this tile is not occluded on this tree, then it is not occluded. | |
| 694 if (!IsTileOccludedOnCurrentTree(tile)) | |
| 695 return false; | |
| 696 | |
| 697 // Otherwise, if this is the pending tree, we're done and the tile is | |
| 698 // occluded. | |
| 699 if (tree_ == PENDING_TREE) | |
| 700 return true; | |
| 701 | |
| 702 // On the active tree however, we need to check if this tile will be | |
| 703 // unoccluded upon activation, in which case it has to be considered | |
| 704 // unoccluded. | |
| 705 const PictureLayerTiling* pending_twin = | |
| 706 client_->GetPendingOrActiveTwinTiling(this); | |
| 707 if (pending_twin) { | |
| 708 // If there's a pending tile in the same position. Or if the pending twin | |
| 709 // would have to be creating all tiles, then we don't need to worry about | |
| 710 // occlusion on the twin. | |
| 711 if (!TilingMatchesTileIndices(pending_twin) || | |
| 712 pending_twin->TileAt(tile->tiling_i_index(), tile->tiling_j_index())) { | |
| 713 return true; | |
| 714 } | |
| 715 return pending_twin->IsTileOccludedOnCurrentTree(tile); | |
| 716 } | |
| 717 return true; | |
| 718 } | |
| 719 | |
| 720 bool PictureLayerTiling::IsTileOccludedOnCurrentTree(const Tile* tile) const { | |
| 721 if (!current_occlusion_in_layer_space_.HasOcclusion()) | |
| 722 return false; | |
| 723 gfx::Rect tile_query_rect = | |
| 724 gfx::IntersectRects(tile->content_rect(), current_visible_rect_); | |
| 725 // Explicitly check if the tile is outside the viewport. If so, we need to | |
| 726 // return false, since occlusion for this tile is unknown. | |
| 727 if (tile_query_rect.IsEmpty()) | |
| 728 return false; | |
| 729 | |
| 730 if (contents_scale_ != 1.f) { | |
| 731 tile_query_rect = | |
| 732 gfx::ScaleToEnclosingRect(tile_query_rect, 1.f / contents_scale_); | |
| 733 } | |
| 734 return current_occlusion_in_layer_space_.IsOccluded(tile_query_rect); | |
| 735 } | |
| 736 | |
| 737 bool PictureLayerTiling::IsTileRequiredForActivation(const Tile* tile) const { | |
| 738 if (tree_ == PENDING_TREE) { | |
| 739 if (!can_require_tiles_for_activation_) | |
| 740 return false; | |
| 741 | |
| 742 if (resolution_ != HIGH_RESOLUTION) | |
| 743 return false; | |
| 744 | |
| 745 if (IsTileOccluded(tile)) | |
| 746 return false; | |
| 747 | |
| 748 bool tile_is_visible = | |
| 749 tile->content_rect().Intersects(current_visible_rect_); | |
| 750 if (!tile_is_visible) | |
| 751 return false; | |
| 752 | |
| 753 if (client_->RequiresHighResToDraw()) | |
| 754 return true; | |
| 755 | |
| 756 const PictureLayerTiling* active_twin = | |
| 757 client_->GetPendingOrActiveTwinTiling(this); | |
| 758 if (!active_twin || !TilingMatchesTileIndices(active_twin)) | |
| 759 return true; | |
| 760 | |
| 761 if (active_twin->raster_source()->GetSize() != raster_source()->GetSize()) | |
| 762 return true; | |
| 763 | |
| 764 if (active_twin->current_visible_rect_ != current_visible_rect_) | |
| 765 return true; | |
| 766 | |
| 767 Tile* twin_tile = | |
| 768 active_twin->TileAt(tile->tiling_i_index(), tile->tiling_j_index()); | |
| 769 if (!twin_tile) | |
| 770 return false; | |
| 771 return true; | |
| 772 } | |
| 773 | |
| 774 DCHECK_EQ(tree_, ACTIVE_TREE); | |
| 775 const PictureLayerTiling* pending_twin = | |
| 776 client_->GetPendingOrActiveTwinTiling(this); | |
| 777 // If we don't have a pending tree, or the pending tree will overwrite the | |
| 778 // given tile, then it is not required for activation. | |
| 779 if (!pending_twin || !TilingMatchesTileIndices(pending_twin) || | |
| 780 pending_twin->TileAt(tile->tiling_i_index(), tile->tiling_j_index())) { | |
| 781 return false; | |
| 782 } | |
| 783 // Otherwise, ask the pending twin if this tile is required for activation. | |
| 784 return pending_twin->IsTileRequiredForActivation(tile); | |
| 785 } | |
| 786 | |
| 787 bool PictureLayerTiling::IsTileRequiredForDraw(const Tile* tile) const { | |
| 788 if (tree_ == PENDING_TREE) | |
| 789 return false; | |
| 790 | |
| 791 if (resolution_ != HIGH_RESOLUTION) | |
| 792 return false; | |
| 793 | |
| 794 bool tile_is_visible = current_visible_rect_.Intersects(tile->content_rect()); | |
| 795 if (!tile_is_visible) | |
| 796 return false; | |
| 797 | |
| 798 if (IsTileOccludedOnCurrentTree(tile)) | |
| 799 return false; | |
| 800 return true; | |
| 801 } | |
| 802 | |
| 803 void PictureLayerTiling::UpdateRequiredStatesOnTile(Tile* tile) const { | |
| 804 DCHECK(tile); | |
| 805 tile->set_required_for_activation(IsTileRequiredForActivation(tile)); | |
| 806 tile->set_required_for_draw(IsTileRequiredForDraw(tile)); | |
| 807 } | |
| 808 | |
| 809 PrioritizedTile PictureLayerTiling::MakePrioritizedTile( | |
| 810 Tile* tile, | |
| 811 PriorityRectType priority_rect_type) const { | |
| 812 DCHECK(tile); | |
| 813 DCHECK( | |
| 814 raster_source()->CoversRect(tile->content_rect(), tile->contents_scale())) | |
| 815 << "Recording rect: " | |
| 816 << gfx::ScaleToEnclosingRect(tile->content_rect(), | |
| 817 1.f / tile->contents_scale()).ToString(); | |
| 818 | |
| 819 return PrioritizedTile(tile, raster_source(), | |
| 820 ComputePriorityForTile(tile, priority_rect_type), | |
| 821 IsTileOccluded(tile)); | |
| 822 } | |
| 823 | |
| 824 std::map<const Tile*, PrioritizedTile> | |
| 825 PictureLayerTiling::UpdateAndGetAllPrioritizedTilesForTesting() const { | |
| 826 std::map<const Tile*, PrioritizedTile> result; | |
| 827 for (const auto& key_tile_pair : tiles_) { | |
| 828 Tile* tile = key_tile_pair.second; | |
| 829 UpdateRequiredStatesOnTile(tile); | |
| 830 PrioritizedTile prioritized_tile = | |
| 831 MakePrioritizedTile(tile, ComputePriorityRectTypeForTile(tile)); | |
| 832 result.insert(std::make_pair(prioritized_tile.tile(), prioritized_tile)); | |
| 833 } | |
| 834 return result; | |
| 835 } | |
| 836 | |
| 837 TilePriority PictureLayerTiling::ComputePriorityForTile( | |
| 838 const Tile* tile, | |
| 839 PriorityRectType priority_rect_type) const { | |
| 840 // TODO(vmpstr): See if this can be moved to iterators. | |
| 841 TilePriority::PriorityBin max_tile_priority_bin = | |
| 842 client_->GetMaxTilePriorityBin(); | |
| 843 | |
| 844 DCHECK_EQ(ComputePriorityRectTypeForTile(tile), priority_rect_type); | |
| 845 DCHECK_EQ(TileAt(tile->tiling_i_index(), tile->tiling_j_index()), tile); | |
| 846 | |
| 847 TilePriority::PriorityBin priority_bin = max_tile_priority_bin; | |
| 848 | |
| 849 switch (priority_rect_type) { | |
| 850 case VISIBLE_RECT: | |
| 851 return TilePriority(resolution_, priority_bin, 0); | |
| 852 case PENDING_VISIBLE_RECT: | |
| 853 if (max_tile_priority_bin <= TilePriority::SOON) | |
| 854 return TilePriority(resolution_, TilePriority::SOON, 0); | |
| 855 priority_bin = TilePriority::EVENTUALLY; | |
| 856 break; | |
| 857 case SKEWPORT_RECT: | |
| 858 case SOON_BORDER_RECT: | |
| 859 if (max_tile_priority_bin <= TilePriority::SOON) | |
| 860 priority_bin = TilePriority::SOON; | |
| 861 break; | |
| 862 case EVENTUALLY_RECT: | |
| 863 priority_bin = TilePriority::EVENTUALLY; | |
| 864 break; | |
| 865 } | |
| 866 | |
| 867 gfx::Rect tile_bounds = | |
| 868 tiling_data_.TileBounds(tile->tiling_i_index(), tile->tiling_j_index()); | |
| 869 DCHECK_GT(current_content_to_screen_scale_, 0.f); | |
| 870 float distance_to_visible = | |
| 871 current_visible_rect_.ManhattanInternalDistance(tile_bounds) * | |
| 872 current_content_to_screen_scale_; | |
| 873 | |
| 874 return TilePriority(resolution_, priority_bin, distance_to_visible); | |
| 875 } | |
| 876 | |
| 877 PictureLayerTiling::PriorityRectType | |
| 878 PictureLayerTiling::ComputePriorityRectTypeForTile(const Tile* tile) const { | |
| 879 DCHECK_EQ(TileAt(tile->tiling_i_index(), tile->tiling_j_index()), tile); | |
| 880 gfx::Rect tile_bounds = | |
| 881 tiling_data_.TileBounds(tile->tiling_i_index(), tile->tiling_j_index()); | |
| 882 | |
| 883 if (current_visible_rect_.Intersects(tile_bounds)) | |
| 884 return VISIBLE_RECT; | |
| 885 | |
| 886 if (pending_visible_rect().Intersects(tile_bounds)) | |
| 887 return PENDING_VISIBLE_RECT; | |
| 888 | |
| 889 if (current_skewport_rect_.Intersects(tile_bounds)) | |
| 890 return SKEWPORT_RECT; | |
| 891 | |
| 892 if (current_soon_border_rect_.Intersects(tile_bounds)) | |
| 893 return SOON_BORDER_RECT; | |
| 894 | |
| 895 DCHECK(current_eventually_rect_.Intersects(tile_bounds)); | |
| 896 return EVENTUALLY_RECT; | |
| 897 } | |
| 898 | |
| 899 void PictureLayerTiling::GetAllPrioritizedTilesForTracing( | |
| 900 std::vector<PrioritizedTile>* prioritized_tiles) const { | |
| 901 for (const auto& tile_pair : tiles_) { | |
| 902 Tile* tile = tile_pair.second; | |
| 903 prioritized_tiles->push_back( | |
| 904 MakePrioritizedTile(tile, ComputePriorityRectTypeForTile(tile))); | |
| 905 } | |
| 906 } | |
| 907 | |
| 908 void PictureLayerTiling::AsValueInto( | |
| 909 base::trace_event::TracedValue* state) const { | |
| 910 state->SetInteger("num_tiles", tiles_.size()); | |
| 911 state->SetDouble("content_scale", contents_scale_); | |
| 912 MathUtil::AddToTracedValue("visible_rect", current_visible_rect_, state); | |
| 913 MathUtil::AddToTracedValue("skewport_rect", current_skewport_rect_, state); | |
| 914 MathUtil::AddToTracedValue("soon_rect", current_soon_border_rect_, state); | |
| 915 MathUtil::AddToTracedValue("eventually_rect", current_eventually_rect_, | |
| 916 state); | |
| 917 MathUtil::AddToTracedValue("tiling_size", tiling_size(), state); | |
| 918 } | |
| 919 | |
| 920 size_t PictureLayerTiling::GPUMemoryUsageInBytes() const { | |
| 921 size_t amount = 0; | |
| 922 for (TileMap::const_iterator it = tiles_.begin(); it != tiles_.end(); ++it) { | |
| 923 const Tile* tile = it->second; | |
| 924 amount += tile->GPUMemoryUsageInBytes(); | |
| 925 } | |
| 926 return amount; | |
| 927 } | |
| 928 | |
| 929 PictureLayerTiling::RectExpansionCache::RectExpansionCache() | |
| 930 : previous_target(0) { | |
| 931 } | |
| 932 | |
| 933 namespace { | |
| 934 | |
| 935 // This struct represents an event at which the expending rect intersects | |
| 936 // one of its boundaries. 4 intersection events will occur during expansion. | |
| 937 struct EdgeEvent { | |
| 938 enum { BOTTOM, TOP, LEFT, RIGHT } edge; | |
| 939 int* num_edges; | |
| 940 int distance; | |
| 941 }; | |
| 942 | |
| 943 // Compute the delta to expand from edges to cover target_area. | |
| 944 int ComputeExpansionDelta(int num_x_edges, int num_y_edges, | |
| 945 int width, int height, | |
| 946 int64 target_area) { | |
| 947 // Compute coefficients for the quadratic equation: | |
| 948 // a*x^2 + b*x + c = 0 | |
| 949 int a = num_y_edges * num_x_edges; | |
| 950 int b = num_y_edges * width + num_x_edges * height; | |
| 951 int64 c = static_cast<int64>(width) * height - target_area; | |
| 952 | |
| 953 // Compute the delta for our edges using the quadratic equation. | |
| 954 int delta = | |
| 955 (a == 0) ? -c / b : (-b + static_cast<int>(std::sqrt( | |
| 956 static_cast<int64>(b) * b - 4.0 * a * c))) / | |
| 957 (2 * a); | |
| 958 return std::max(0, delta); | |
| 959 } | |
| 960 | |
| 961 } // namespace | |
| 962 | |
| 963 gfx::Rect PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy( | |
| 964 const gfx::Rect& starting_rect, | |
| 965 int64 target_area, | |
| 966 const gfx::Rect& bounding_rect, | |
| 967 RectExpansionCache* cache) { | |
| 968 if (starting_rect.IsEmpty()) | |
| 969 return starting_rect; | |
| 970 | |
| 971 if (cache && | |
| 972 cache->previous_start == starting_rect && | |
| 973 cache->previous_bounds == bounding_rect && | |
| 974 cache->previous_target == target_area) | |
| 975 return cache->previous_result; | |
| 976 | |
| 977 if (cache) { | |
| 978 cache->previous_start = starting_rect; | |
| 979 cache->previous_bounds = bounding_rect; | |
| 980 cache->previous_target = target_area; | |
| 981 } | |
| 982 | |
| 983 DCHECK(!bounding_rect.IsEmpty()); | |
| 984 DCHECK_GT(target_area, 0); | |
| 985 | |
| 986 // Expand the starting rect to cover target_area, if it is smaller than it. | |
| 987 int delta = ComputeExpansionDelta( | |
| 988 2, 2, starting_rect.width(), starting_rect.height(), target_area); | |
| 989 gfx::Rect expanded_starting_rect = starting_rect; | |
| 990 if (delta > 0) | |
| 991 expanded_starting_rect.Inset(-delta, -delta); | |
| 992 | |
| 993 gfx::Rect rect = IntersectRects(expanded_starting_rect, bounding_rect); | |
| 994 if (rect.IsEmpty()) { | |
| 995 // The starting_rect and bounding_rect are far away. | |
| 996 if (cache) | |
| 997 cache->previous_result = rect; | |
| 998 return rect; | |
| 999 } | |
| 1000 if (delta >= 0 && rect == expanded_starting_rect) { | |
| 1001 // The starting rect already covers the entire bounding_rect and isn't too | |
| 1002 // large for the target_area. | |
| 1003 if (cache) | |
| 1004 cache->previous_result = rect; | |
| 1005 return rect; | |
| 1006 } | |
| 1007 | |
| 1008 // Continue to expand/shrink rect to let it cover target_area. | |
| 1009 | |
| 1010 // These values will be updated by the loop and uses as the output. | |
| 1011 int origin_x = rect.x(); | |
| 1012 int origin_y = rect.y(); | |
| 1013 int width = rect.width(); | |
| 1014 int height = rect.height(); | |
| 1015 | |
| 1016 // In the beginning we will consider 2 edges in each dimension. | |
| 1017 int num_y_edges = 2; | |
| 1018 int num_x_edges = 2; | |
| 1019 | |
| 1020 // Create an event list. | |
| 1021 EdgeEvent events[] = { | |
| 1022 { EdgeEvent::BOTTOM, &num_y_edges, rect.y() - bounding_rect.y() }, | |
| 1023 { EdgeEvent::TOP, &num_y_edges, bounding_rect.bottom() - rect.bottom() }, | |
| 1024 { EdgeEvent::LEFT, &num_x_edges, rect.x() - bounding_rect.x() }, | |
| 1025 { EdgeEvent::RIGHT, &num_x_edges, bounding_rect.right() - rect.right() } | |
| 1026 }; | |
| 1027 | |
| 1028 // Sort the events by distance (closest first). | |
| 1029 if (events[0].distance > events[1].distance) std::swap(events[0], events[1]); | |
| 1030 if (events[2].distance > events[3].distance) std::swap(events[2], events[3]); | |
| 1031 if (events[0].distance > events[2].distance) std::swap(events[0], events[2]); | |
| 1032 if (events[1].distance > events[3].distance) std::swap(events[1], events[3]); | |
| 1033 if (events[1].distance > events[2].distance) std::swap(events[1], events[2]); | |
| 1034 | |
| 1035 for (int event_index = 0; event_index < 4; event_index++) { | |
| 1036 const EdgeEvent& event = events[event_index]; | |
| 1037 | |
| 1038 int delta = ComputeExpansionDelta( | |
| 1039 num_x_edges, num_y_edges, width, height, target_area); | |
| 1040 | |
| 1041 // Clamp delta to our event distance. | |
| 1042 if (delta > event.distance) | |
| 1043 delta = event.distance; | |
| 1044 | |
| 1045 // Adjust the edge count for this kind of edge. | |
| 1046 --*event.num_edges; | |
| 1047 | |
| 1048 // Apply the delta to the edges and edge events. | |
| 1049 for (int i = event_index; i < 4; i++) { | |
| 1050 switch (events[i].edge) { | |
| 1051 case EdgeEvent::BOTTOM: | |
| 1052 origin_y -= delta; | |
| 1053 height += delta; | |
| 1054 break; | |
| 1055 case EdgeEvent::TOP: | |
| 1056 height += delta; | |
| 1057 break; | |
| 1058 case EdgeEvent::LEFT: | |
| 1059 origin_x -= delta; | |
| 1060 width += delta; | |
| 1061 break; | |
| 1062 case EdgeEvent::RIGHT: | |
| 1063 width += delta; | |
| 1064 break; | |
| 1065 } | |
| 1066 events[i].distance -= delta; | |
| 1067 } | |
| 1068 | |
| 1069 // If our delta is less then our event distance, we're done. | |
| 1070 if (delta < event.distance) | |
| 1071 break; | |
| 1072 } | |
| 1073 | |
| 1074 gfx::Rect result(origin_x, origin_y, width, height); | |
| 1075 if (cache) | |
| 1076 cache->previous_result = result; | |
| 1077 return result; | |
| 1078 } | |
| 1079 | |
| 1080 } // namespace cc | |
| OLD | NEW |