Reland "Add base::TimeDelta::Max()"
[chromium-blink-merge.git] / cc / resources / layer_tiling_data.cc
blobccbb6d0148194cc6ea8d8ee2f2b44cadabcf9e99
1 // Copyright 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/resources/layer_tiling_data.h"
7 #include <vector>
9 #include "base/logging.h"
11 namespace cc {
13 scoped_ptr<LayerTilingData> LayerTilingData::Create(const gfx::Size& tile_size,
14 BorderTexelOption border) {
15 return make_scoped_ptr(new LayerTilingData(tile_size, border));
18 LayerTilingData::LayerTilingData(const gfx::Size& tile_size,
19 BorderTexelOption border)
20 : tiling_data_(tile_size, gfx::Size(), border == HAS_BORDER_TEXELS) {
21 SetTileSize(tile_size);
24 LayerTilingData::~LayerTilingData() {}
26 void LayerTilingData::SetTileSize(const gfx::Size& size) {
27 if (tile_size() == size)
28 return;
30 reset();
32 tiling_data_.SetMaxTextureSize(size);
35 gfx::Size LayerTilingData::tile_size() const {
36 return tiling_data_.max_texture_size();
39 void LayerTilingData::SetBorderTexelOption(
40 BorderTexelOption border_texel_option) {
41 bool border_texels = border_texel_option == HAS_BORDER_TEXELS;
42 if (has_border_texels() == border_texels)
43 return;
45 reset();
46 tiling_data_.SetHasBorderTexels(border_texels);
49 const LayerTilingData& LayerTilingData::operator=
50 (const LayerTilingData & tiler) {
51 tiling_data_ = tiler.tiling_data_;
53 return *this;
56 void LayerTilingData::AddTile(scoped_ptr<Tile> tile, int i, int j) {
57 DCHECK(!TileAt(i, j));
58 tile->move_to(i, j);
59 tiles_.add(std::make_pair(i, j), tile.Pass());
62 scoped_ptr<LayerTilingData::Tile> LayerTilingData::TakeTile(int i, int j) {
63 return tiles_.take_and_erase(std::make_pair(i, j));
66 LayerTilingData::Tile* LayerTilingData::TileAt(int i, int j) const {
67 return tiles_.get(std::make_pair(i, j));
70 void LayerTilingData::ContentRectToTileIndices(const gfx::Rect& content_rect,
71 int* left,
72 int* top,
73 int* right,
74 int* bottom) const {
75 // An empty rect doesn't result in an empty set of tiles, so don't pass an
76 // empty rect.
77 // TODO(enne): Possibly we should fill a vector of tiles instead, since the
78 // normal use of this function is to enumerate some tiles.
79 DCHECK(!content_rect.IsEmpty());
81 *left = tiling_data_.TileXIndexFromSrcCoord(content_rect.x());
82 *top = tiling_data_.TileYIndexFromSrcCoord(content_rect.y());
83 *right = tiling_data_.TileXIndexFromSrcCoord(content_rect.right() - 1);
84 *bottom = tiling_data_.TileYIndexFromSrcCoord(content_rect.bottom() - 1);
87 gfx::Rect LayerTilingData::TileRect(const Tile* tile) const {
88 gfx::Rect tile_rect = tiling_data_.TileBoundsWithBorder(tile->i(), tile->j());
89 tile_rect.set_size(tile_size());
90 return tile_rect;
93 Region LayerTilingData::OpaqueRegionInContentRect(
94 const gfx::Rect& content_rect) const {
95 if (content_rect.IsEmpty())
96 return Region();
98 Region opaque_region;
99 int left, top, right, bottom;
100 ContentRectToTileIndices(content_rect, &left, &top, &right, &bottom);
101 for (int j = top; j <= bottom; ++j) {
102 for (int i = left; i <= right; ++i) {
103 Tile* tile = TileAt(i, j);
104 if (!tile)
105 continue;
107 gfx::Rect tile_opaque_rect =
108 gfx::IntersectRects(content_rect, tile->opaque_rect());
109 opaque_region.Union(tile_opaque_rect);
112 return opaque_region;
115 void LayerTilingData::SetBounds(const gfx::Size& size) {
116 tiling_data_.SetTotalSize(size);
117 if (size.IsEmpty()) {
118 tiles_.clear();
119 return;
122 // Any tiles completely outside our new bounds are invalid and should be
123 // dropped.
124 int left, top, right, bottom;
125 ContentRectToTileIndices(
126 gfx::Rect(size), &left, &top, &right, &bottom);
127 std::vector<TileMapKey> invalid_tile_keys;
128 for (TileMap::const_iterator it = tiles_.begin(); it != tiles_.end(); ++it) {
129 if (it->first.first > right || it->first.second > bottom)
130 invalid_tile_keys.push_back(it->first);
132 for (size_t i = 0; i < invalid_tile_keys.size(); ++i)
133 tiles_.erase(invalid_tile_keys[i]);
136 } // namespace cc