1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297
  298
  299
  300
  301
  302
  303
  304
  305
  306
  307
  308
  309
  310
  311
  312
  313
  314
  315
  316
  317
  318
  319
  320
  321
  322
  323
  324
  325
  326
  327
  328
  329
  330
  331
  332
  333
  334
  335
  336
  337
  338
  339
  340
  341
  342
  343
  344
  345
  346
  347
  348
  349
  350
  351
  352
  353
  354
  355
  356
  357
  358
  359
  360
  361
  362
  363
  364
  365
  366
  367
  368
  369
  370
  371
  372
  373
  374
  375
  376
  377
  378
  379
  380
  381
  382
  383
  384
  385
  386
  387
  388
  389
  390
  391
  392
  393
  394
  395
  396
  397
  398
  399
  400
  401
  402
  403
  404
  405
  406
  407
  408
  409
  410
  411
  412
  413
  414
  415
  416
  417
  418
  419
  420
  421
  422
  423
  424
  425
  426
  427
  428
  429
  430
  431
  432
  433
  434
  435
  436
  437
  438
  439
  440
  441
  442
  443
  444
  445
  446
  447
  448
  449
  450
  451
  452
  453
  454
  455
  456
  457
  458
  459
  460
  461
  462
  463
  464
  465
  466
  467
  468
  469
  470
  471
  472
  473
  474
  475
  476
  477
  478
  479
  480
  481
  482
  483
  484
  485
  486
  487
  488
  489
  490
  491
  492
  493
  494
  495
  496
  497
  498
  499
  500
  501
  502
  503
  504
  505
  506
  507
  508
  509
  510
  511
  512
  513
  514
  515
  516
  517
  518
  519
  520
  521
  522
  523
  524
  525
  526
  527
  528
  529
  530
  531
  532
  533
  534
  535
  536
  537
  538
  539
  540
  541
  542
  543
  544
  545
  546
  547
  548
  549
  550
  551
  552
  553
  554
  555
  556
  557
  558
  559
  560
  561
  562
  563
  564
  565
  566
  567
  568
  569
  570
  571
  572
  573
  574
  575
  576
  577
  578
  579
  580
  581
  582
  583
  584
  585
  586
  587
  588
  589
  590
  591
  592
  593
  594
  595
  596
  597
  598
  599
  600
  601
  602
  603
  604
  605
  606
  607
  608
  609
  610
  611
  612
  613
  614
  615
  616
  617
  618
  619
  620
  621
  622
  623
  624
  625
  626
  627
  628
  629
  630
  631
  632
  633
  634
  635
  636
  637
  638
  639
  640
  641
  642
  643
  644
  645
  646
  647
  648
  649
  650
  651
  652
  653
  654
  655
  656
  657
  658
  659
  660
  661
  662
  663
  664
  665
  666
  667
  668
  669
  670
  671
  672
  673
  674
  675
  676
  677
  678
  679
  680
  681
  682
  683
  684
  685
  686
  687
  688
  689
  690
  691
  692
  693
  694
  695
  696
  697
  698
  699
  700
  701
  702
  703
  704
  705
  706
  707
  708
  709
  710
  711
  712
  713
  714
  715
  716
  717
  718
  719
  720
  721
  722
  723
  724
  725
  726
  727
  728
  729
  730
  731
  732
  733
  734
  735
  736
  737
  738
  739
  740
  741
  742
  743
  744
  745
  746
  747
  748
  749
  750
  751
  752
  753
  754
  755
  756
  757
  758
  759
  760
  761
  762
  763
  764
  765
  766
  767
  768
  769
  770
  771
  772
  773
  774
  775
  776
  777
  778
  779
  780
  781
  782
  783
  784
  785
  786
  787
  788
  789
  790
  791
  792
  793
  794
  795
  796
  797
  798
  799
  800
  801
  802
  803
  804
  805
  806
  807
  808
  809
  810
  811
  812
  813
  814
  815
  816
  817
  818
  819
  820
  821
  822
  823
  824
  825
  826
  827
  828
  829
  830
  831
  832
  833
  834
  835
  836
  837
  838
  839
  840
  841
  842
  843
  844
  845
  846
  847
  848
  849
  850
  851
  852
  853
  854
  855
  856
  857
  858
  859
  860
  861
  862
  863
  864
  865
  866
  867
  868
  869
  870
  871
  872
  873
  874
  875
  876
  877
  878
  879
  880
  881
  882
  883
  884
  885
  886
  887
  888
  889
  890
  891
  892
  893
  894
  895
  896
  897
  898
  899
  900
  901
  902
  903
  904
  905
  906
  907
  908
  909
  910
  911
  912
  913
  914
  915
  916
  917
  918
  919
  920
  921
  922
  923
  924
  925
  926
  927
  928
  929
  930
  931
  932
  933
  934
  935
  936
  937
  938
  939
  940
  941
  942

cc / tiles / picture_layer_tiling.cc [blame]

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cc/tiles/picture_layer_tiling.h"

#include <stddef.h>

#include <algorithm>
#include <cmath>
#include <limits>
#include <set>

#include "base/check_op.h"
#include "base/containers/contains.h"
#include "base/containers/flat_map.h"
#include "base/numerics/safe_conversions.h"
#include "base/trace_event/trace_event.h"
#include "base/trace_event/traced_value.h"
#include "cc/base/features.h"
#include "cc/base/math_util.h"
#include "cc/layers/picture_layer_impl.h"
#include "cc/raster/raster_source.h"
#include "cc/tiles/prioritized_tile.h"
#include "cc/tiles/tile.h"
#include "cc/tiles/tile_priority.h"
#include "ui/gfx/geometry/point_conversions.h"
#include "ui/gfx/geometry/rect_conversions.h"
#include "ui/gfx/geometry/rect_f.h"
#include "ui/gfx/geometry/size_conversions.h"

namespace cc {

PictureLayerTiling::PictureLayerTiling(
    WhichTree tree,
    const gfx::AxisTransform2d& raster_transform,
    scoped_refptr<RasterSource> raster_source,
    PictureLayerTilingClient* client,
    float min_preraster_distance,
    float max_preraster_distance,
    bool can_use_lcd_text)
    : raster_transform_(raster_transform),
      client_(client),
      tree_(tree),
      raster_source_(raster_source),
      min_preraster_distance_(min_preraster_distance),
      max_preraster_distance_(max_preraster_distance),
      can_use_lcd_text_(can_use_lcd_text) {
  DCHECK(!raster_source->IsSolidColor());
  DCHECK_GE(raster_transform.translation().x(), 0.f);
  DCHECK_LT(raster_transform.translation().x(), 1.f);
  DCHECK_GE(raster_transform.translation().y(), 0.f);
  DCHECK_LT(raster_transform.translation().y(), 1.f);

#if DCHECK_IS_ON()
  gfx::SizeF scaled_source_size(gfx::ScaleSize(
      gfx::SizeF(raster_source_->recorded_bounds().size()),
      raster_transform.scale().x(), raster_transform.scale().y()));
  gfx::Size floored_size = gfx::ToFlooredSize(scaled_source_size);
  bool is_width_empty =
      !floored_size.width() &&
      !MathUtil::IsWithinEpsilon(scaled_source_size.width(), 1.f);
  bool is_height_empty =
      !floored_size.height() &&
      !MathUtil::IsWithinEpsilon(scaled_source_size.height(), 1.f);
  DCHECK(!is_width_empty && !is_height_empty)
      << "Tiling created with scale too small as contents become empty."
      << " recorded bounds: " << raster_source_->recorded_bounds().ToString()
      << " Raster transform: " << raster_transform_.ToString();
#endif

  gfx::Rect tiling_rect = ComputeTilingRect();
  SetTilingRect(tiling_rect);
  tiling_data_.SetMaxTextureSize(
      client_->CalculateTileSize(tiling_rect.size()));
}

PictureLayerTiling::~PictureLayerTiling() = default;

Tile* PictureLayerTiling::CreateTile(const Tile::CreateInfo& info) {
  const int i = info.tiling_i_index;
  const int j = info.tiling_j_index;
  TileIndex index(i, j);
  DCHECK(!base::Contains(tiles_, index));

  if (!raster_source_->IntersectsRect(info.enclosing_layer_rect)) {
    return nullptr;
  }

  all_tiles_done_ = false;

  std::unique_ptr<Tile> tile = client_->CreateTile(info);
  Tile* tile_ptr = tile.get();
  tiles_[index] = std::move(tile);
  return tile_ptr;
}

void PictureLayerTiling::CreateMissingTilesInLiveTilesRect() {
  const PictureLayerTiling* active_twin =
      tree_ == PENDING_TREE ? client_->GetPendingOrActiveTwinTiling(this)
                            : nullptr;
  const Region* invalidation =
      active_twin ? client_->GetPendingInvalidation() : nullptr;

  bool include_borders = false;
  for (TilingData::Iterator iter(&tiling_data_, live_tiles_rect_,
                                 include_borders);
       iter; ++iter) {
    TileIndex index(iter.index());
    auto find = tiles_.find(index);
    if (find != tiles_.end())
      continue;

    Tile::CreateInfo info = CreateInfoForTile(index.i, index.j);
    if (ShouldCreateTileAt(info)) {
      Tile* tile = CreateTile(info);

      // If this is the pending tree, then the active twin tiling may contain
      // the previous content ID of these tiles. In that case, we need only
      // partially raster the tile content.
      if (tile && invalidation && TilingMatchesTileIndices(active_twin)) {
        if (const Tile* old_tile = active_twin->TileAt(index)) {
          gfx::Rect tile_rect = tile->content_rect();
          gfx::Rect invalidated;
          for (gfx::Rect rect : *invalidation) {
            gfx::Rect invalid_content_rect =
                EnclosingContentsRectFromLayerRect(rect);
            invalid_content_rect.Intersect(tile_rect);
            invalidated.Union(invalid_content_rect);
          }
          tile->SetInvalidated(invalidated, old_tile->id());
        }
      }
    }
  }
  VerifyTiles();
}

void PictureLayerTiling::TakeTilesAndPropertiesFrom(
    PictureLayerTiling* pending_twin,
    const Region& layer_invalidation) {
  SetRasterSourceAndResize(pending_twin->raster_source_);

  RemoveTilesInRegion(layer_invalidation, false /* recreate tiles */);

  resolution_ = pending_twin->resolution_;

  while (!pending_twin->tiles_.empty()) {
    auto pending_iter = pending_twin->tiles_.begin();
    pending_iter->second->set_tiling(this);
    tiles_[pending_iter->first] = std::move(pending_iter->second);
    pending_twin->tiles_.erase(pending_iter);
  }

  if (all_tiles_done_ && !pending_twin->all_tiles_done_) {
    all_tiles_done_ = false;
  }
  ComputeTilePriorityRects(
      pending_twin->current_visible_rect_in_layer_space_,
      pending_twin->current_skewport_rect_in_layer_space_,
      pending_twin->current_soon_border_rect_in_layer_space_,
      pending_twin->current_eventually_rect_in_layer_space_,
      pending_twin->current_content_to_screen_scale_ * contents_scale_key(),
      pending_twin->current_occlusion_in_layer_space_);

  DCHECK(pending_twin->tiles_.empty());
  pending_twin->all_tiles_done_ = true;

  VerifyTiles();
}

bool PictureLayerTiling::SetRasterSourceAndResize(
    scoped_refptr<RasterSource> raster_source) {
  DCHECK(!raster_source->IsSolidColor());
  raster_source_ = std::move(raster_source);
  gfx::Rect tiling_rect = ComputeTilingRect();
  gfx::Size tile_size = client_->CalculateTileSize(tiling_rect.size());

  if (tile_size != tiling_data_.max_texture_size() ||
      tiling_rect.origin() != tiling_data_.tiling_rect().origin()) {
    SetTilingRect(tiling_rect);
    tiling_data_.SetMaxTextureSize(tile_size);
    // When the origin of recorded bounds or tile size changes, the TilingData
    // positions no longer work as valid indices to the TileMap, so just drop
    // all tiles and clear the live tiles rect.
    Reset();
    // When the tile size changes, all tiles and all tile priority rects
    // including the live tiles rect should be updated, therefore return true to
    // notify the caller to call |ComputeTilePriorityRects| to do this.
    return true;
  }

  // When the tiling rect is the same, we need not notify the caller as it
  // will update tiling as needed, so return false.
  if (tiling_rect == tiling_data_.tiling_rect()) {
    return false;
  }

  // We can't use SetPriorityRect(EVENTUALLY_RECT) and SetLiveTilesRect()
  // to drop and create tiles according to the new bounds. This is because
  // resizing the tiling causes the number of tiles in the tiling_data_ to
  // change.
  int before_eventually_left =
      tiling_data_.TileXIndexFromSrcCoord(current_eventually_rect_.x());
  int before_eventually_top =
      tiling_data_.TileYIndexFromSrcCoord(current_eventually_rect_.y());
  int before_eventually_right =
      tiling_data_.TileXIndexFromSrcCoord(current_eventually_rect_.right() - 1);
  int before_eventually_bottom = tiling_data_.TileYIndexFromSrcCoord(
      current_eventually_rect_.bottom() - 1);

  int before_live_left =
      tiling_data_.TileXIndexFromSrcCoord(live_tiles_rect_.x());
  int before_live_top =
      tiling_data_.TileYIndexFromSrcCoord(live_tiles_rect_.y());
  int before_live_right =
      tiling_data_.TileXIndexFromSrcCoord(live_tiles_rect_.right() - 1);
  int before_live_bottom =
      tiling_data_.TileYIndexFromSrcCoord(live_tiles_rect_.bottom() - 1);

  // The live_tiles_rect_ is clamped to stay within the tiling rect as we
  // change it.
  live_tiles_rect_.Intersect(tiling_rect);
  SetTilingRect(tiling_rect);

  // Evict tiles outside the new tiling rect.
  int after_eventually_right = tiling_data_.num_tiles_x() - 1;
  int after_eventually_bottom = tiling_data_.num_tiles_y() - 1;

  int after_live_right = -1;
  int after_live_bottom = -1;
  if (!live_tiles_rect_.IsEmpty()) {
    after_live_right =
        tiling_data_.TileXIndexFromSrcCoord(live_tiles_rect_.right() - 1);
    after_live_bottom =
        tiling_data_.TileYIndexFromSrcCoord(live_tiles_rect_.bottom() - 1);
  }

  // There is no recycled twin since this is run on the pending tiling
  // during commit, and on the active tree during activate.
  // Drop tiles outside the new recorded bounds if the they shrank.
  for (int i = after_eventually_right + 1; i <= before_eventually_right; ++i) {
    for (int j = before_eventually_top; j <= before_eventually_bottom; ++j) {
      TakeTileAt(i, j);
    }
  }
  for (int i = before_eventually_left; i <= after_eventually_right; ++i) {
    for (int j = after_eventually_bottom + 1; j <= before_eventually_bottom;
         ++j) {
      TakeTileAt(i, j);
    }
  }

  if (after_live_right > before_live_right) {
    DCHECK_EQ(after_live_right, before_live_right + 1);
    for (int j = before_live_top; j <= after_live_bottom; ++j) {
      Tile::CreateInfo info = CreateInfoForTile(after_live_right, j);
      if (ShouldCreateTileAt(info))
        CreateTile(info);
    }
  }
  if (after_live_bottom > before_live_bottom) {
    // Using the smallest horizontal bound here makes sure we don't
    // create tiles twice and don't iterate into deleted tiles.
    int boundary_right = std::min(after_live_right, before_live_right);
    DCHECK_EQ(after_live_bottom, before_live_bottom + 1);
    for (int i = before_live_left; i <= boundary_right; ++i) {
      Tile::CreateInfo info = CreateInfoForTile(i, after_live_bottom);
      if (ShouldCreateTileAt(info))
        CreateTile(info);
    }
  }
  VerifyTiles();
  // We need not notify the caller as it will update tiling as needed, so return
  // false to ensure the existing logic remains unchanged.
  return false;
}

void PictureLayerTiling::Invalidate(const Region& layer_invalidation) {
  DCHECK(tree_ != ACTIVE_TREE || !client_->GetPendingOrActiveTwinTiling(this));
  RemoveTilesInRegion(layer_invalidation, true /* recreate tiles */);
}

void PictureLayerTiling::RemoveTilesInRegion(const Region& layer_invalidation,
                                             bool recreate_tiles) {
  // We only invalidate the active tiling when it's orphaned: it has no pending
  // twin, so it's slated for removal in the future.
  if (current_eventually_rect_.IsEmpty()) {
    return;
  }

  base::flat_map<TileIndex, gfx::Rect> remove_tiles;
  gfx::Rect expanded_eventually_rect =
      tiling_data_.ExpandRectToTileBounds(current_eventually_rect_);
  for (gfx::Rect layer_rect : layer_invalidation) {
    // The pixels which are invalid in content space.
    gfx::Rect invalid_content_rect =
        EnclosingContentsRectFromLayerRect(layer_rect);
    gfx::Rect coverage_content_rect = invalid_content_rect;
    // Avoid needless work by not bothering to invalidate where there aren't
    // tiles.
    coverage_content_rect.Intersect(expanded_eventually_rect);
    if (coverage_content_rect.IsEmpty())
      continue;
    // Since the content_rect needs to invalidate things that only touch a
    // border of a tile, we need to include the borders while iterating.
    bool include_borders = true;
    for (TilingData::Iterator iter(&tiling_data_, coverage_content_rect,
                                   include_borders);
         iter; ++iter) {
      // This also adds the TileIndex to the map.
      remove_tiles[TileIndex(iter.index())].Union(invalid_content_rect);
    }
  }

  for (const auto& pair : remove_tiles) {
    const TileIndex& index = pair.first;
    const gfx::Rect& invalid_content_rect = pair.second;
    // TODO(danakj): This old_tile will not exist if we are committing to a
    // pending tree since there is no tile there to remove, which prevents
    // tiles from knowing the invalidation rect and content id. crbug.com/490847
    std::unique_ptr<Tile> old_tile = TakeTileAt(index.i, index.j);
    if (recreate_tiles && old_tile) {
      Tile::CreateInfo info = CreateInfoForTile(index.i, index.j);
      if (Tile* tile = CreateTile(info))
        tile->SetInvalidated(invalid_content_rect, old_tile->id());
    }
  }
}

Tile::CreateInfo PictureLayerTiling::CreateInfoForTile(int i, int j) const {
  gfx::Rect tile_rect = tiling_data_.TileBoundsWithBorder(i, j);
  tile_rect.set_size(tiling_data_.max_texture_size());
  gfx::Rect enclosing_layer_rect =
      EnclosingLayerRectFromContentsRect(tile_rect);
  return Tile::CreateInfo{this,
                          i,
                          j,
                          enclosing_layer_rect,
                          tile_rect,
                          raster_transform_,
                          can_use_lcd_text_};
}

bool PictureLayerTiling::ShouldCreateTileAt(
    const Tile::CreateInfo& info) const {
  const int i = info.tiling_i_index;
  const int j = info.tiling_j_index;
  // Active tree should always create a tile. The reason for this is that active
  // tree represents content that we draw on screen, which means that whenever
  // we check whether a tile should exist somewhere, the answer is yes. This
  // doesn't mean it will actually be created (if raster source doesn't cover
  // the tile for instance). Pending tree, on the other hand, should only be
  // creating tiles that are different from the current active tree, which is
  // represented by the logic in the rest of the function.
  if (tree_ == ACTIVE_TREE)
    return true;

  // If the pending tree has no active twin, then it needs to create all tiles.
  const PictureLayerTiling* active_twin =
      client_->GetPendingOrActiveTwinTiling(this);
  if (!active_twin)
    return true;

  // Pending tree will override the entire active tree if indices don't match.
  if (!TilingMatchesTileIndices(active_twin))
    return true;

  // If our settings don't match the active twin, it means that the active
  // tiles will all be removed when we activate. So we need all the tiles on the
  // pending tree to be created. See
  // PictureLayerTilingSet::CopyTilingsAndPropertiesFromPendingTwin.
  if (can_use_lcd_text() != active_twin->can_use_lcd_text() ||
      raster_transform() != active_twin->raster_transform())
    return true;

  // If the active tree can't create a tile, because of its raster source, then
  // the pending tree should create one.
  if (!active_twin->raster_source()->IntersectsRect(
          info.enclosing_layer_rect)) {
    return true;
  }

  const Region* layer_invalidation = client_->GetPendingInvalidation();

  // If this tile is invalidated, then the pending tree should create one.
  // Do the intersection test in content space to match the corresponding check
  // on the active tree and avoid floating point inconsistencies.
  for (gfx::Rect layer_rect : *layer_invalidation) {
    gfx::Rect invalid_content_rect =
        EnclosingContentsRectFromLayerRect(layer_rect);
    if (invalid_content_rect.Intersects(info.content_rect))
      return true;
  }
  // If the active tree doesn't have a tile here, but it's in the pending tree's
  // visible rect, then the pending tree should create a tile. This can happen
  // if the pending visible rect is outside of the active tree's live tiles
  // rect. In those situations, we need to block activation until we're ready to
  // display content, which will have to come from the pending tree.
  if (!active_twin->TileAt(i, j) &&
      current_visible_rect_.Intersects(info.content_rect))
    return true;

  // In all other cases, the pending tree doesn't need to create a tile.
  return false;
}

bool PictureLayerTiling::TilingMatchesTileIndices(
    const PictureLayerTiling* twin) const {
  return tiling_data_.max_texture_size() ==
             twin->tiling_data_.max_texture_size() &&
         tiling_rect().origin() == twin->tiling_rect().origin();
}

std::unique_ptr<Tile> PictureLayerTiling::TakeTileAt(int i, int j) {
  auto found = tiles_.find(TileIndex(i, j));
  if (found == tiles_.end())
    return nullptr;
  std::unique_ptr<Tile> result = std::move(found->second);
  tiles_.erase(found);
  return result;
}

void PictureLayerTiling::SetTilePriorityRectsForTesting(
    const gfx::Rect& visible_rect,
    const gfx::Rect& skewport_rect,
    const gfx::Rect& soon_border_rect,
    const gfx::Rect& eventually_rect,
    bool evicts_tiles) {
  current_occlusion_in_layer_space_ = Occlusion();
  current_content_to_screen_scale_ = 1.0;

  SetPriorityRect(EnclosingLayerRectFromContentsRect(visible_rect),
                  VISIBLE_RECT);
  SetPriorityRect(EnclosingLayerRectFromContentsRect(skewport_rect),
                  SKEWPORT_RECT);
  SetPriorityRect(EnclosingLayerRectFromContentsRect(soon_border_rect),
                  SOON_BORDER_RECT);
  SetPriorityRect(EnclosingLayerRectFromContentsRect(eventually_rect),
                  EVENTUALLY_RECT, evicts_tiles);

  // Note that we use the largest skewport extent from the viewport as the
  // "skewport extent". Also note that this math can't produce negative numbers,
  // since skewport.Contains(visible_rect) is always true.
  max_skewport_extent_in_screen_space_ = std::max(
      {current_visible_rect_.x() - current_skewport_rect_.x(),
       current_skewport_rect_.right() - current_visible_rect_.right(),
       current_visible_rect_.y() - current_skewport_rect_.y(),
       current_skewport_rect_.bottom() - current_visible_rect_.bottom()});
}

void PictureLayerTiling::Reset() {
  live_tiles_rect_ = gfx::Rect();
  tiles_.clear();
  all_tiles_done_ = true;
}

void PictureLayerTiling::ComputeTilePriorityRects(
    const gfx::Rect& visible_rect_in_layer_space,
    const gfx::Rect& skewport_rect_in_layer_space,
    const gfx::Rect& soon_border_rect_in_layer_space,
    const gfx::Rect& eventually_rect_in_layer_space,
    float ideal_contents_scale,
    const Occlusion& occlusion_in_layer_space) {
  // If we have, or had occlusions, mark the tiles as 'not done' to ensure that
  // we reiterate the tiles for rasterization.
  if (occlusion_in_layer_space.HasOcclusion() ||
      current_occlusion_in_layer_space_.HasOcclusion()) {
    all_tiles_done_ = false;
  }

  TileMemoryLimitPolicy memory_limit_policy =
      client_->global_tile_state().memory_limit_policy;

  current_occlusion_in_layer_space_ = occlusion_in_layer_space;
  current_content_to_screen_scale_ =
      ideal_contents_scale / contents_scale_key();

  SetPriorityRect(visible_rect_in_layer_space, VISIBLE_RECT);
  SetPriorityRect(skewport_rect_in_layer_space, SKEWPORT_RECT);
  SetPriorityRect(soon_border_rect_in_layer_space, SOON_BORDER_RECT);
  SetPriorityRect(eventually_rect_in_layer_space, EVENTUALLY_RECT,
                  /*evicts_tiles=*/true);

  // Note that we use the largest skewport extent from the viewport as the
  // "skewport extent". Also note that this math can't produce negative numbers,
  // since skewport.Contains(visible_rect) is always true.
  max_skewport_extent_in_screen_space_ =
      current_content_to_screen_scale_ *
      std::max(
          {current_visible_rect_.x() - current_skewport_rect_.x(),
           current_skewport_rect_.right() - current_visible_rect_.right(),
           current_visible_rect_.y() - current_skewport_rect_.y(),
           current_skewport_rect_.bottom() - current_visible_rect_.bottom()});

  gfx::Rect live_tiles_rect;
  if (features::IsCCSlimmingEnabled()) {
    live_tiles_rect = current_visible_rect_;
    bool draws_tiles = has_visible_rect_tiles_;

    if (memory_limit_policy >= TileMemoryLimitPolicy::ALLOW_PREPAINT_ONLY) {
      draws_tiles |= has_skewport_rect_tiles_;
      if (has_skewport_rect_tiles_) {
        live_tiles_rect.Union(current_skewport_rect_);
      }

      draws_tiles |= has_soon_border_rect_tiles_;
      if (has_soon_border_rect_tiles_) {
        live_tiles_rect.Union(current_soon_border_rect_);
      }
    }

    if (memory_limit_policy >= TileMemoryLimitPolicy::ALLOW_ANYTHING) {
      draws_tiles |= has_eventually_rect_tiles_;
      if (has_eventually_rect_tiles_) {
        live_tiles_rect.Union(current_eventually_rect_);
      }
    }
    if (!draws_tiles) {
      all_tiles_done_ = true;
    }
  } else {
    live_tiles_rect = current_eventually_rect_;
  }
  live_tiles_rect.Intersect(tiling_rect());
  SetLiveTilesRect(live_tiles_rect);
}

void PictureLayerTiling::SetPriorityRect(const gfx::Rect& rect_in_layer_space,
                                         PriorityRectType rect_type,
                                         bool evicts_tiles) {
  DCHECK(!evicts_tiles || rect_type == EVENTUALLY_RECT);
  switch (rect_type) {
    case VISIBLE_RECT:
      if (current_visible_rect_in_layer_space_ != rect_in_layer_space) {
        current_visible_rect_in_layer_space_ = rect_in_layer_space;
        current_visible_rect_ =
            EnclosingContentsRectFromLayerRect(rect_in_layer_space);
        has_visible_rect_tiles_ =
            tiling_rect().Intersects(current_visible_rect_);
      }
      break;
    case SKEWPORT_RECT:
      if (current_skewport_rect_in_layer_space_ != rect_in_layer_space) {
        current_skewport_rect_in_layer_space_ = rect_in_layer_space;
        current_skewport_rect_ =
            EnclosingContentsRectFromLayerRect(rect_in_layer_space);
        has_skewport_rect_tiles_ =
            tiling_rect().Intersects(current_skewport_rect_);
      }
      break;
    case SOON_BORDER_RECT:
      if (current_soon_border_rect_in_layer_space_ != rect_in_layer_space) {
        current_soon_border_rect_in_layer_space_ = rect_in_layer_space;
        current_soon_border_rect_ =
            EnclosingContentsRectFromLayerRect(rect_in_layer_space);
        has_soon_border_rect_tiles_ =
            tiling_rect().Intersects(current_soon_border_rect_);
      }
      break;
    case EVENTUALLY_RECT:
      if (current_eventually_rect_in_layer_space_ != rect_in_layer_space) {
        current_eventually_rect_in_layer_space_ = rect_in_layer_space;
        gfx::Rect rect =
            EnclosingContentsRectFromLayerRect(rect_in_layer_space);
        if (evicts_tiles) {
          // Iterate to delete all tiles outside of our new live_tiles rect.
          for (TilingData::DifferenceIterator iter(
                   &tiling_data_, current_eventually_rect_, rect);
               iter; ++iter) {
            TakeTileAt(iter.index_x(), iter.index_y());
          }
        }
        current_eventually_rect_ = rect;
        has_eventually_rect_tiles_ =
            tiling_rect().Intersects(current_eventually_rect_);
      }
      break;
    default:
      NOTREACHED();
  }
}

void PictureLayerTiling::SetLiveTilesRect(
    const gfx::Rect& new_live_tiles_rect) {
  DCHECK(new_live_tiles_rect.IsEmpty() ||
         tiling_rect().Contains(new_live_tiles_rect))
      << "tiling_rect: " << tiling_rect().ToString()
      << " new_live_tiles_rect: " << new_live_tiles_rect.ToString();
  if (live_tiles_rect_ == new_live_tiles_rect)
    return;

  // We don't rasterize non ideal resolution tiles, so there is no need to
  // create any new tiles.
  if (resolution_ == NON_IDEAL_RESOLUTION) {
    live_tiles_rect_.Intersect(new_live_tiles_rect);
    VerifyTiles();
    return;
  }

  // Iterate to allocate new tiles for all regions with newly exposed area.
  for (TilingData::DifferenceIterator iter(&tiling_data_, new_live_tiles_rect,
                                           live_tiles_rect_);
       iter; ++iter) {
    Tile::CreateInfo info = CreateInfoForTile(iter.index_x(), iter.index_y());
    if (ShouldCreateTileAt(info)) {
      Tile* tile = TileAt(iter.index_x(), iter.index_y());
      if (tile) {
        if (!tile->IsReadyToDraw()) {
          all_tiles_done_ = false;
        }
      } else {
        CreateTile(info);
      }
    }
  }
  live_tiles_rect_ = new_live_tiles_rect;
  if (tiles_.size() == 0) {
    all_tiles_done_ = true;
  }
  VerifyTiles();
}

void PictureLayerTiling::VerifyTiles() const {
#if DCHECK_IS_ON()
  for (auto it = tiles_.begin(); it != tiles_.end(); ++it) {
    DCHECK(it->second);
    TileIndex index = it->first;
    DCHECK(index.i < tiling_data_.num_tiles_x())
        << this << " " << index.i << "," << index.j << " num_tiles_x "
        << tiling_data_.num_tiles_x() << " eventually_rect "
        << current_eventually_rect_.ToString();
    DCHECK(index.j < tiling_data_.num_tiles_y())
        << this << " " << index.i << "," << index.j << " num_tiles_y "
        << tiling_data_.num_tiles_y() << " eventually_rect "
        << current_eventually_rect_.ToString();
    DCHECK(tiling_data_.TileBounds(index.i, index.j)
               .Intersects(current_eventually_rect_))
        << this << " " << index.i << "," << index.j << " tile bounds "
        << tiling_data_.TileBounds(index.i, index.j).ToString()
        << " eventually_rect " << current_eventually_rect_.ToString();
  }
#endif
}

bool PictureLayerTiling::IsTileOccludedOnCurrentTree(const Tile* tile) const {
  if (!current_occlusion_in_layer_space_.HasOcclusion())
    return false;
  gfx::Rect tile_bounds =
      tiling_data_.TileBounds(tile->tiling_i_index(), tile->tiling_j_index());
  gfx::Rect tile_query_rect =
      gfx::IntersectRects(tile_bounds, current_visible_rect_);
  // Explicitly check if the tile is outside the viewport. If so, we need to
  // return false, since occlusion for this tile is unknown.
  if (tile_query_rect.IsEmpty())
    return false;

  tile_query_rect = EnclosingLayerRectFromContentsRect(tile_query_rect);
  return current_occlusion_in_layer_space_.IsOccluded(tile_query_rect);
}

bool PictureLayerTiling::ShouldDecodeCheckeredImagesForTile(
    const Tile* tile) const {
  // If this is the pending tree and the tile is not occluded, any checkered
  // images on this tile should be decoded.
  if (tree_ == PENDING_TREE)
    return !IsTileOccludedOnCurrentTree(tile);

  DCHECK_EQ(tree_, ACTIVE_TREE);
  const PictureLayerTiling* pending_twin =
      client_->GetPendingOrActiveTwinTiling(this);

  // If we don't have a pending twin, then 2 cases are possible. Either we don't
  // have a pending tree, in which case we should be decoding images for tiles
  // which are unoccluded.
  // If we do have a pending tree, then not having a twin implies that this
  // tiling will be evicted upon activation. TODO(khushalsagar): Plumb this
  // information here and return false for this case.
  if (!pending_twin)
    return !IsTileOccludedOnCurrentTree(tile);

  // If the tile will be replaced upon activation, then we don't need to process
  // it for checkered images. Since once the pending tree is activated, it is
  // the new active tree's content that we will invalidate and replace once the
  // decode finishes.
  if (!TilingMatchesTileIndices(pending_twin) ||
      pending_twin->TileAt(tile->tiling_i_index(), tile->tiling_j_index())) {
    return false;
  }

  // Ask the pending twin if this tile will become occluded upon activation.
  return !pending_twin->IsTileOccludedOnCurrentTree(tile);
}

void PictureLayerTiling::UpdateRequiredStatesOnTile(Tile* tile) const {
  tile->set_required_for_activation(IsTileRequiredForActivation(
      tile, [this](const Tile* tile) { return IsTileVisible(tile); },
      IsTileOccluded(tile)));
  tile->set_required_for_draw(IsTileRequiredForDraw(
      tile, [this](const Tile* tile) { return IsTileVisible(tile); }));
}

PictureLayerTiling::CoverageIterator PictureLayerTiling::Cover(
    const gfx::Rect& rect,
    float scale) const {
  return CoverageIterator(this, scale, rect);
}

PrioritizedTile PictureLayerTiling::MakePrioritizedTile(
    Tile* tile,
    PriorityRectType priority_rect_type,
    bool is_tile_occluded) const {
  DCHECK(tile);
  DCHECK(raster_source()->IntersectsRect(tile->enclosing_layer_rect()))
      << "Recording rect: "
      << EnclosingLayerRectFromContentsRect(tile->content_rect()).ToString();

  tile->set_required_for_activation(IsTileRequiredForActivation(
      tile,
      [priority_rect_type](const Tile*) {
        return priority_rect_type == VISIBLE_RECT;
      },
      is_tile_occluded));
  tile->set_required_for_draw(
      IsTileRequiredForDraw(tile, [priority_rect_type](const Tile*) {
        return priority_rect_type == VISIBLE_RECT;
      }));

  const auto& tile_priority =
      ComputePriorityForTile(tile, priority_rect_type, is_tile_occluded);
  DCHECK((!tile->required_for_activation() && !tile->required_for_draw()) ||
         tile_priority.priority_bin == TilePriority::NOW ||
         !client_->HasValidTilePriorities());

  // Note that TileManager will consider this flag but may rasterize the tile
  // anyway (if tile is required for activation for example). We should process
  // the tile for images only if it's further than half of the skewport extent.
  bool process_for_images_only =
      tile_priority.distance_to_visible > min_preraster_distance_ &&
      (tile_priority.distance_to_visible > max_preraster_distance_ ||
       tile_priority.distance_to_visible >
           0.5f * max_skewport_extent_in_screen_space_);

  return PrioritizedTile(tile, this, tile_priority, is_tile_occluded,
                         process_for_images_only,
                         ShouldDecodeCheckeredImagesForTile(tile));
}

void PictureLayerTiling::CreateAllTilesForTesting(
    const gfx::Rect& rect_to_raster) {
  SetTilePriorityRectsForTesting(  // IN-TEST
      rect_to_raster, rect_to_raster, rect_to_raster, rect_to_raster,
      /*evicts_tiles=*/true);
  SetLiveTilesRect(rect_to_raster);
}

std::map<const Tile*, PrioritizedTile>
PictureLayerTiling::UpdateAndGetAllPrioritizedTilesForTesting() const {
  std::map<const Tile*, PrioritizedTile> result;
  for (const auto& index_tile_pair : tiles_) {
    Tile* tile = index_tile_pair.second.get();
    PrioritizedTile prioritized_tile = MakePrioritizedTile(
        tile, ComputePriorityRectTypeForTile(tile), IsTileOccluded(tile));
    result.insert(std::make_pair(prioritized_tile.tile(), prioritized_tile));
  }
  return result;
}

TilePriority PictureLayerTiling::ComputePriorityForTile(
    const Tile* tile,
    PriorityRectType priority_rect_type,
    bool is_tile_occluded) const {
  // TODO(vmpstr): See if this can be moved to iterators.
  DCHECK_EQ(ComputePriorityRectTypeForTile(tile), priority_rect_type);
  DCHECK_EQ(TileAt(tile->tiling_i_index(), tile->tiling_j_index()), tile);

  TilePriority::PriorityBin priority_bin;
  if (client_->HasValidTilePriorities()) {
    // Occluded tiles are given a lower PriorityBin to ensure they are evicted
    // before non-occluded tiles.
    priority_bin = is_tile_occluded ? TilePriority::SOON : TilePriority::NOW;
  } else {
    priority_bin = TilePriority::EVENTUALLY;
  }

  switch (priority_rect_type) {
    case VISIBLE_RECT:
    case PENDING_VISIBLE_RECT:
      return TilePriority(resolution_, priority_bin, 0);
    case SKEWPORT_RECT:
    case SOON_BORDER_RECT:
      if (priority_bin < TilePriority::SOON)
        priority_bin = TilePriority::SOON;
      break;
    case EVENTUALLY_RECT:
      priority_bin = TilePriority::EVENTUALLY;
      break;
  }

  gfx::Rect tile_bounds =
      tiling_data_.TileBounds(tile->tiling_i_index(), tile->tiling_j_index());
  DCHECK_GT(current_content_to_screen_scale_, 0.f);
  float distance_to_visible =
      current_content_to_screen_scale_ *
      current_visible_rect_.ManhattanInternalDistance(tile_bounds);

  return TilePriority(resolution_, priority_bin, distance_to_visible);
}

PictureLayerTiling::PriorityRectType
PictureLayerTiling::ComputePriorityRectTypeForTile(const Tile* tile) const {
  DCHECK_EQ(TileAt(tile->tiling_i_index(), tile->tiling_j_index()), tile);
  gfx::Rect tile_bounds =
      tiling_data_.TileBounds(tile->tiling_i_index(), tile->tiling_j_index());

  if (current_visible_rect_.Intersects(tile_bounds))
    return VISIBLE_RECT;

  if (pending_visible_rect().Intersects(tile_bounds))
    return PENDING_VISIBLE_RECT;

  if (current_skewport_rect_.Intersects(tile_bounds))
    return SKEWPORT_RECT;

  if (current_soon_border_rect_.Intersects(tile_bounds))
    return SOON_BORDER_RECT;

  DCHECK(current_eventually_rect_.Intersects(tile_bounds));
  return EVENTUALLY_RECT;
}

void PictureLayerTiling::GetAllPrioritizedTilesForTracing(
    std::vector<PrioritizedTile>* prioritized_tiles) const {
  for (const auto& tile_pair : tiles_) {
    Tile* tile = tile_pair.second.get();
    prioritized_tiles->push_back(MakePrioritizedTile(
        tile, ComputePriorityRectTypeForTile(tile), IsTileOccluded(tile)));
  }
}

void PictureLayerTiling::AsValueInto(
    base::trace_event::TracedValue* state) const {
  state->SetInteger("num_tiles", base::saturated_cast<int>(tiles_.size()));
  state->SetDouble("content_scale", contents_scale_key());

  state->BeginDictionary("raster_transform");
  state->BeginArray("scale");
  state->AppendDouble(raster_transform_.scale().x());
  state->AppendDouble(raster_transform_.scale().y());
  state->EndArray();
  state->BeginArray("translation");
  state->AppendDouble(raster_transform_.translation().x());
  state->AppendDouble(raster_transform_.translation().y());
  state->EndArray();
  state->EndDictionary();

  MathUtil::AddToTracedValue("visible_rect", current_visible_rect_, state);
  MathUtil::AddToTracedValue("skewport_rect", current_skewport_rect_, state);
  MathUtil::AddToTracedValue("soon_rect", current_soon_border_rect_, state);
  MathUtil::AddToTracedValue("eventually_rect", current_eventually_rect_,
                             state);
  MathUtil::AddToTracedValue("tiling_rect", tiling_rect(), state);
}

size_t PictureLayerTiling::GPUMemoryUsageInBytes() const {
  size_t amount = 0;
  for (auto it = tiles_.begin(); it != tiles_.end(); ++it) {
    const Tile* tile = it->second.get();
    amount += tile->GPUMemoryUsageInBytes();
  }
  return amount;
}

gfx::Rect PictureLayerTiling::EnclosingContentsRectFromLayerRect(
    const gfx::Rect& layer_rect) const {
  return ToEnclosingRect(raster_transform_.MapRect(gfx::RectF(layer_rect)));
}

gfx::Rect PictureLayerTiling::EnclosingLayerRectFromContentsRect(
    const gfx::Rect& contents_rect) const {
  return ToEnclosingRect(
      raster_transform_.InverseMapRect(gfx::RectF(contents_rect)));
}

gfx::Rect PictureLayerTiling::ComputeTilingRect() const {
  gfx::Rect recorded_bounds = raster_source_->recorded_bounds();
  gfx::Rect tiling_rect = EnclosingContentsRectFromLayerRect(recorded_bounds);
  gfx::Rect layer_bounds(raster_source_->size());
  if (recorded_bounds != layer_bounds) {
    gfx::Rect layer_contents_rect =
        EnclosingContentsRectFromLayerRect(layer_bounds);
    // Snap tiling_rect to avoid full tiling invalidation on small change of
    // tiling rect origin.
    constexpr int kSnapTexels = 128;
    tiling_rect.SetByBounds(
        MathUtil::UncheckedRoundDown(tiling_rect.x(), kSnapTexels),
        MathUtil::UncheckedRoundDown(tiling_rect.y(), kSnapTexels),
        // Snap to the layer edge if the tiling edge is near the layer edge.
        tiling_rect.right() + kSnapTexels > layer_contents_rect.right()
            ? layer_contents_rect.right()
            : tiling_rect.right(),
        tiling_rect.bottom() + kSnapTexels > layer_contents_rect.bottom()
            ? layer_contents_rect.bottom()
            : tiling_rect.bottom());
    DCHECK(layer_contents_rect.Contains(tiling_rect));
  }
  return tiling_rect;
}

void PictureLayerTiling::SetTilingRect(const gfx::Rect& tiling_rect) {
  if (tiling_data_.tiling_rect() == tiling_rect) {
    return;
  }

  has_visible_rect_tiles_ = tiling_rect.Intersects(current_visible_rect_);
  has_skewport_rect_tiles_ = tiling_rect.Intersects(current_skewport_rect_);
  has_soon_border_rect_tiles_ =
      tiling_rect.Intersects(current_soon_border_rect_);
  has_eventually_rect_tiles_ = tiling_rect.Intersects(current_eventually_rect_);
  tiling_data_.SetTilingRect(tiling_rect);
  tiling_rect_in_layer_space_ = EnclosingLayerRectFromContentsRect(tiling_rect);
}

PictureLayerTiling::TileIterator::TileIterator(PictureLayerTiling* tiling)
    : tiling_(tiling), iter_(tiling->tiles_.begin()) {}

PictureLayerTiling::TileIterator::~TileIterator() = default;

Tile* PictureLayerTiling::TileIterator::GetCurrent() {
  return AtEnd() ? nullptr : iter_->second.get();
}

void PictureLayerTiling::TileIterator::Next() {
  if (!AtEnd())
    ++iter_;
}

bool PictureLayerTiling::TileIterator::AtEnd() const {
  return iter_ == tiling_->tiles_.end();
}

}  // namespace cc