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(const gfx::Size & tile_size,BorderTexelOption border)13 scoped_ptr<LayerTilingData> LayerTilingData::Create(const gfx::Size& tile_size,
14 BorderTexelOption border) {
15 return make_scoped_ptr(new LayerTilingData(tile_size, border));
16 }
17
LayerTilingData(const gfx::Size & tile_size,BorderTexelOption border)18 LayerTilingData::LayerTilingData(const gfx::Size& tile_size,
19 BorderTexelOption border)
20 : tiling_data_(tile_size, gfx::Rect(), border == HAS_BORDER_TEXELS) {
21 SetTileSize(tile_size);
22 }
23
~LayerTilingData()24 LayerTilingData::~LayerTilingData() {}
25
SetTileSize(const gfx::Size & size)26 void LayerTilingData::SetTileSize(const gfx::Size& size) {
27 if (tile_size() == size)
28 return;
29
30 reset();
31
32 tiling_data_.SetMaxTextureSize(size);
33 }
34
tile_size() const35 gfx::Size LayerTilingData::tile_size() const {
36 return tiling_data_.max_texture_size();
37 }
38
SetBorderTexelOption(BorderTexelOption border_texel_option)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;
44
45 reset();
46 tiling_data_.SetHasBorderTexels(border_texels);
47 }
48
operator =(const LayerTilingData & tiler)49 const LayerTilingData& LayerTilingData::operator=
50 (const LayerTilingData & tiler) {
51 tiling_data_ = tiler.tiling_data_;
52
53 return *this;
54 }
55
AddTile(scoped_ptr<Tile> tile,int i,int j)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());
60 }
61
TakeTile(int i,int j)62 scoped_ptr<LayerTilingData::Tile> LayerTilingData::TakeTile(int i, int j) {
63 return tiles_.take_and_erase(std::make_pair(i, j));
64 }
65
TileAt(int i,int j) const66 LayerTilingData::Tile* LayerTilingData::TileAt(int i, int j) const {
67 return tiles_.get(std::make_pair(i, j));
68 }
69
ContentRectToTileIndices(const gfx::Rect & content_rect,int * left,int * top,int * right,int * bottom) const70 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());
80
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);
85 }
86
TileRect(const Tile * tile) const87 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;
91 }
92
OpaqueRegionInContentRect(const gfx::Rect & content_rect) const93 Region LayerTilingData::OpaqueRegionInContentRect(
94 const gfx::Rect& content_rect) const {
95 if (content_rect.IsEmpty())
96 return Region();
97
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;
106
107 gfx::Rect tile_opaque_rect =
108 gfx::IntersectRects(content_rect, tile->opaque_rect());
109 opaque_region.Union(tile_opaque_rect);
110 }
111 }
112 return opaque_region;
113 }
114
SetTilingRect(const gfx::Rect & tiling_rect)115 void LayerTilingData::SetTilingRect(const gfx::Rect& tiling_rect) {
116 tiling_data_.SetTilingRect(tiling_rect);
117 if (tiling_rect.IsEmpty()) {
118 tiles_.clear();
119 return;
120 }
121
122 // Any tiles completely outside our new bounds are invalid and should be
123 // dropped.
124 int left, top, right, bottom;
125 ContentRectToTileIndices(tiling_rect, &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