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.
4
5 #include "cc/resources/layer_tiling_data.h"
6
7 #include <vector>
8
9 #include "base/logging.h"
10
11 namespace cc {
12
Create(gfx::Size tile_size,BorderTexelOption border)13 scoped_ptr<LayerTilingData> LayerTilingData::Create(gfx::Size tile_size,
14 BorderTexelOption border) {
15 return make_scoped_ptr(new LayerTilingData(tile_size, border));
16 }
17
LayerTilingData(gfx::Size tile_size,BorderTexelOption border)18 LayerTilingData::LayerTilingData(gfx::Size tile_size, BorderTexelOption border)
19 : tiling_data_(tile_size, gfx::Size(), border == HAS_BORDER_TEXELS) {
20 SetTileSize(tile_size);
21 }
22
~LayerTilingData()23 LayerTilingData::~LayerTilingData() {}
24
SetTileSize(gfx::Size size)25 void LayerTilingData::SetTileSize(gfx::Size size) {
26 if (tile_size() == size)
27 return;
28
29 reset();
30
31 tiling_data_.SetMaxTextureSize(size);
32 }
33
tile_size() const34 gfx::Size LayerTilingData::tile_size() const {
35 return tiling_data_.max_texture_size();
36 }
37
SetBorderTexelOption(BorderTexelOption border_texel_option)38 void LayerTilingData::SetBorderTexelOption(
39 BorderTexelOption border_texel_option) {
40 bool border_texels = border_texel_option == HAS_BORDER_TEXELS;
41 if (has_border_texels() == border_texels)
42 return;
43
44 reset();
45 tiling_data_.SetHasBorderTexels(border_texels);
46 }
47
operator =(const LayerTilingData & tiler)48 const LayerTilingData& LayerTilingData::operator=
49 (const LayerTilingData & tiler) {
50 tiling_data_ = tiler.tiling_data_;
51
52 return *this;
53 }
54
AddTile(scoped_ptr<Tile> tile,int i,int j)55 void LayerTilingData::AddTile(scoped_ptr<Tile> tile, int i, int j) {
56 DCHECK(!TileAt(i, j));
57 tile->move_to(i, j);
58 tiles_.add(std::make_pair(i, j), tile.Pass());
59 }
60
TakeTile(int i,int j)61 scoped_ptr<LayerTilingData::Tile> LayerTilingData::TakeTile(int i, int j) {
62 return tiles_.take_and_erase(std::make_pair(i, j));
63 }
64
TileAt(int i,int j) const65 LayerTilingData::Tile* LayerTilingData::TileAt(int i, int j) const {
66 return tiles_.get(std::make_pair(i, j));
67 }
68
ContentRectToTileIndices(gfx::Rect content_rect,int * left,int * top,int * right,int * bottom) const69 void LayerTilingData::ContentRectToTileIndices(gfx::Rect content_rect,
70 int* left,
71 int* top,
72 int* right,
73 int* bottom) const {
74 // An empty rect doesn't result in an empty set of tiles, so don't pass an
75 // empty rect.
76 // TODO(enne): Possibly we should fill a vector of tiles instead, since the
77 // normal use of this function is to enumerate some tiles.
78 DCHECK(!content_rect.IsEmpty());
79
80 *left = tiling_data_.TileXIndexFromSrcCoord(content_rect.x());
81 *top = tiling_data_.TileYIndexFromSrcCoord(content_rect.y());
82 *right = tiling_data_.TileXIndexFromSrcCoord(content_rect.right() - 1);
83 *bottom = tiling_data_.TileYIndexFromSrcCoord(content_rect.bottom() - 1);
84 }
85
TileRect(const Tile * tile) const86 gfx::Rect LayerTilingData::TileRect(const Tile* tile) const {
87 gfx::Rect tile_rect = tiling_data_.TileBoundsWithBorder(tile->i(), tile->j());
88 tile_rect.set_size(tile_size());
89 return tile_rect;
90 }
91
OpaqueRegionInContentRect(gfx::Rect content_rect) const92 Region LayerTilingData::OpaqueRegionInContentRect(
93 gfx::Rect content_rect) const {
94 if (content_rect.IsEmpty())
95 return Region();
96
97 Region opaque_region;
98 int left, top, right, bottom;
99 ContentRectToTileIndices(content_rect, &left, &top, &right, &bottom);
100 for (int j = top; j <= bottom; ++j) {
101 for (int i = left; i <= right; ++i) {
102 Tile* tile = TileAt(i, j);
103 if (!tile)
104 continue;
105
106 gfx::Rect tile_opaque_rect =
107 gfx::IntersectRects(content_rect, tile->opaque_rect());
108 opaque_region.Union(tile_opaque_rect);
109 }
110 }
111 return opaque_region;
112 }
113
SetBounds(gfx::Size size)114 void LayerTilingData::SetBounds(gfx::Size size) {
115 tiling_data_.SetTotalSize(size);
116 if (size.IsEmpty()) {
117 tiles_.clear();
118 return;
119 }
120
121 // Any tiles completely outside our new bounds are invalid and should be
122 // dropped.
123 int left, top, right, bottom;
124 ContentRectToTileIndices(
125 gfx::Rect(size), &left, &top, &right, &bottom);
126 std::vector<TileMapKey> invalid_tile_keys;
127 for (TileMap::const_iterator it = tiles_.begin(); it != tiles_.end(); ++it) {
128 if (it->first.first > right || it->first.second > bottom)
129 invalid_tile_keys.push_back(it->first);
130 }
131 for (size_t i = 0; i < invalid_tile_keys.size(); ++i)
132 tiles_.erase(invalid_tile_keys[i]);
133 }
134
135 } // namespace cc
136