OLD | NEW |
1 // Copyright 2010 The Chromium Authors. All rights reserved. | 1 // Copyright 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/tiling_data.h" | 5 #include "cc/tiling_data.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "ui/gfx/rect.h" | 9 #include "ui/gfx/rect.h" |
10 #include "ui/gfx/vector2d.h" | 10 #include "ui/gfx/vector2d.h" |
11 | 11 |
12 namespace cc { | 12 namespace cc { |
13 | 13 |
14 static int ComputeNumTiles(int max_texture_size, int total_size, int border_texe
ls) { | 14 static int ComputeNumTiles(int max_texture_size, int total_size, int border_texe
ls) { |
15 if (max_texture_size - 2 * border_texels <= 0) | 15 if (max_texture_size - 2 * border_texels <= 0) |
16 return total_size > 0 && max_texture_size >= total_size ? 1 : 0; | 16 return total_size > 0 && max_texture_size >= total_size ? 1 : 0; |
17 | 17 |
18 int num_tiles = std::max(1, 1 + (total_size - 1 - 2 * border_texels) / (max_te
xture_size - 2 * border_texels)); | 18 int num_tiles = std::max(1, 1 + (total_size - 1 - 2 * border_texels) / (max_te
xture_size - 2 * border_texels)); |
19 return total_size > 0 ? num_tiles : 0; | 19 return total_size > 0 ? num_tiles : 0; |
20 } | 20 } |
21 | 21 |
22 TilingData::TilingData(gfx::Size max_texture_size, gfx::Size total_size, bool ha
sBorderTexels) | 22 TilingData::TilingData( |
| 23 gfx::Size max_texture_size, |
| 24 gfx::Size total_size, |
| 25 bool hasBorderTexels) |
23 : max_texture_size_(max_texture_size), | 26 : max_texture_size_(max_texture_size), |
24 total_size_(total_size), | 27 total_size_(total_size), |
25 border_texels_(hasBorderTexels ? 1 : 0) { | 28 border_texels_(hasBorderTexels ? 1 : 0) { |
26 RecomputeNumTiles(); | 29 RecomputeNumTiles(); |
27 } | 30 } |
28 | 31 |
29 TilingData::~TilingData() { | 32 TilingData::TilingData( |
| 33 gfx::Size max_texture_size, |
| 34 gfx::Size total_size, |
| 35 int border_texels) |
| 36 : max_texture_size_(max_texture_size), |
| 37 total_size_(total_size), |
| 38 border_texels_(border_texels) { |
| 39 RecomputeNumTiles(); |
30 } | 40 } |
31 | 41 |
32 void TilingData::SetTotalSize(gfx::Size total_size) { | 42 void TilingData::SetTotalSize(gfx::Size total_size) { |
33 total_size_ = total_size; | 43 total_size_ = total_size; |
34 RecomputeNumTiles(); | 44 RecomputeNumTiles(); |
35 } | 45 } |
36 | 46 |
37 void TilingData::SetMaxTextureSize(gfx::Size max_texture_size) { | 47 void TilingData::SetMaxTextureSize(gfx::Size max_texture_size) { |
38 max_texture_size_ = max_texture_size; | 48 max_texture_size_ = max_texture_size; |
39 RecomputeNumTiles(); | 49 RecomputeNumTiles(); |
40 } | 50 } |
41 | 51 |
42 void TilingData::SetHasBorderTexels(bool has_border_texels) { | 52 void TilingData::SetHasBorderTexels(bool has_border_texels) { |
43 border_texels_ = has_border_texels ? 1 : 0; | 53 border_texels_ = has_border_texels ? 1 : 0; |
44 RecomputeNumTiles(); | 54 RecomputeNumTiles(); |
45 } | 55 } |
46 | 56 |
| 57 void TilingData::SetBorderTexels(int border_texels) { |
| 58 border_texels_ = border_texels; |
| 59 RecomputeNumTiles(); |
| 60 } |
| 61 |
47 int TilingData::TileXIndexFromSrcCoord(int src_position) const { | 62 int TilingData::TileXIndexFromSrcCoord(int src_position) const { |
48 if (num_tiles_x_ <= 1) | 63 if (num_tiles_x_ <= 1) |
49 return 0; | 64 return 0; |
50 | 65 |
51 DCHECK(max_texture_size_.width() - 2 * border_texels_); | 66 DCHECK_GT(max_texture_size_.width() - 2 * border_texels_, 0); |
52 int x = (src_position - border_texels_) / (max_texture_size_.width() - 2 * bor
der_texels_); | 67 int x = (src_position - border_texels_) / |
| 68 (max_texture_size_.width() - 2 * border_texels_); |
53 return std::min(std::max(x, 0), num_tiles_x_ - 1); | 69 return std::min(std::max(x, 0), num_tiles_x_ - 1); |
54 } | 70 } |
55 | 71 |
56 int TilingData::TileYIndexFromSrcCoord(int src_position) const { | 72 int TilingData::TileYIndexFromSrcCoord(int src_position) const { |
57 if (num_tiles_y_ <= 1) | 73 if (num_tiles_y_ <= 1) |
58 return 0; | 74 return 0; |
59 | 75 |
60 DCHECK(max_texture_size_.height() - 2 * border_texels_); | 76 DCHECK_GT(max_texture_size_.height() - 2 * border_texels_, 0); |
61 int y = (src_position - border_texels_) / (max_texture_size_.height() - 2 * bo
rder_texels_); | 77 int y = (src_position - border_texels_) / |
| 78 (max_texture_size_.height() - 2 * border_texels_); |
62 return std::min(std::max(y, 0), num_tiles_y_ - 1); | 79 return std::min(std::max(y, 0), num_tiles_y_ - 1); |
63 } | 80 } |
64 | 81 |
| 82 int TilingData::BorderTileXIndexFromSrcCoord(int src_position) const { |
| 83 if (num_tiles_x_ <= 1) |
| 84 return 0; |
| 85 |
| 86 DCHECK_GT(max_texture_size_.width() - 2 * border_texels_, 0); |
| 87 int inner_tile_size = max_texture_size_.width() - 2 * border_texels_; |
| 88 int x = (src_position - 2 * border_texels_) / inner_tile_size; |
| 89 return std::min(std::max(x, 0), num_tiles_x_ - 1); |
| 90 } |
| 91 |
| 92 int TilingData::BorderTileYIndexFromSrcCoord(int src_position) const { |
| 93 if (num_tiles_y_ <= 1) |
| 94 return 0; |
| 95 |
| 96 DCHECK_GT(max_texture_size_.height() - 2 * border_texels_, 0); |
| 97 int inner_tile_size = max_texture_size_.height() - 2 * border_texels_; |
| 98 int y = (src_position - 2 * border_texels_) / inner_tile_size; |
| 99 return std::min(std::max(y, 0), num_tiles_y_ - 1); |
| 100 } |
| 101 |
65 gfx::Rect TilingData::TileBounds(int i, int j) const { | 102 gfx::Rect TilingData::TileBounds(int i, int j) const { |
66 AssertTile(i, j); | 103 AssertTile(i, j); |
67 int x = TilePositionX(i); | 104 int x = TilePositionX(i); |
68 int y = TilePositionY(j); | 105 int y = TilePositionY(j); |
69 int width = TileSizeX(i); | 106 int width = TileSizeX(i); |
70 int height = TileSizeY(j); | 107 int height = TileSizeY(j); |
71 DCHECK_GE(x, 0); | 108 DCHECK_GE(x, 0); |
72 DCHECK_GE(y, 0); | 109 DCHECK_GE(y, 0); |
73 DCHECK_GE(width, 0); | 110 DCHECK_GE(width, 0); |
74 DCHECK_GE(height, 0); | 111 DCHECK_GE(height, 0); |
75 DCHECK_LE(x, total_size_.width()); | 112 DCHECK_LE(x, total_size_.width()); |
76 DCHECK_LE(y, total_size_.height()); | 113 DCHECK_LE(y, total_size_.height()); |
77 return gfx::Rect(x, y, width, height); | 114 return gfx::Rect(x, y, width, height); |
78 } | 115 } |
79 | 116 |
80 gfx::Rect TilingData::TileBoundsWithBorder(int i, int j) const { | 117 gfx::Rect TilingData::TileBoundsWithBorder(int i, int j) const { |
81 gfx::Rect bounds = TileBounds(i, j); | 118 gfx::Rect bounds = TileBounds(i, j); |
82 | 119 |
83 if (border_texels_) { | 120 if (border_texels_) { |
84 int x1 = bounds.x(); | 121 int x1 = bounds.x(); |
85 int x2 = bounds.right(); | 122 int x2 = bounds.right(); |
86 int y1 = bounds.y(); | 123 int y1 = bounds.y(); |
87 int y2 = bounds.bottom(); | 124 int y2 = bounds.bottom(); |
88 | 125 |
89 if (i > 0) | 126 if (i > 0) |
90 x1--; | 127 x1-= border_texels_; |
91 if (i < (num_tiles_x_ - 1)) | 128 if (i < (num_tiles_x_ - 1)) |
92 x2++; | 129 x2+= border_texels_; |
93 if (j > 0) | 130 if (j > 0) |
94 y1--; | 131 y1-= border_texels_; |
95 if (j < (num_tiles_y_ - 1)) | 132 if (j < (num_tiles_y_ - 1)) |
96 y2++; | 133 y2+= border_texels_; |
97 | 134 |
98 bounds = gfx::Rect(x1, y1, x2 - x1, y2 - y1); | 135 bounds = gfx::Rect(x1, y1, x2 - x1, y2 - y1); |
99 } | 136 } |
100 | 137 |
101 return bounds; | 138 return bounds; |
102 } | 139 } |
103 | 140 |
104 int TilingData::TilePositionX(int x_index) const { | 141 int TilingData::TilePositionX(int x_index) const { |
105 DCHECK_GE(x_index, 0); | 142 DCHECK_GE(x_index, 0); |
106 DCHECK_LT(x_index, num_tiles_x_); | 143 DCHECK_LT(x_index, num_tiles_x_); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 int top = (!y_index || num_tiles_y_ == 1) ? 0 : border_texels_; | 199 int top = (!y_index || num_tiles_y_ == 1) ? 0 : border_texels_; |
163 | 200 |
164 return gfx::Vector2d(left, top); | 201 return gfx::Vector2d(left, top); |
165 } | 202 } |
166 | 203 |
167 void TilingData::RecomputeNumTiles() { | 204 void TilingData::RecomputeNumTiles() { |
168 num_tiles_x_ = ComputeNumTiles(max_texture_size_.width(), total_size_.width(),
border_texels_); | 205 num_tiles_x_ = ComputeNumTiles(max_texture_size_.width(), total_size_.width(),
border_texels_); |
169 num_tiles_y_ = ComputeNumTiles(max_texture_size_.height(), total_size_.height(
), border_texels_); | 206 num_tiles_y_ = ComputeNumTiles(max_texture_size_.height(), total_size_.height(
), border_texels_); |
170 } | 207 } |
171 | 208 |
| 209 TilingData::Iterator::Iterator(const TilingData* tiling_data, gfx::Rect rect) |
| 210 : tiling_data_(tiling_data), |
| 211 rect_(gfx::IntersectRects(rect, gfx::Rect(tiling_data_->total_size()))) { |
| 212 if (tiling_data_->num_tiles_x() <= 0 || tiling_data_->num_tiles_y() <= 0) { |
| 213 done(); |
| 214 return; |
| 215 } |
| 216 |
| 217 index_x_ = tiling_data_->BorderTileXIndexFromSrcCoord(rect_.x()); |
| 218 index_y_ = tiling_data_->BorderTileYIndexFromSrcCoord(rect_.y()); |
| 219 |
| 220 // Index functions always return valid indices, so explicitly check |
| 221 // for non-intersecting rects. |
| 222 gfx::Rect new_rect = tiling_data_->TileBoundsWithBorder(index_x_, index_y_); |
| 223 if (!new_rect.Intersects(rect_)) |
| 224 done(); |
| 225 } |
| 226 |
| 227 TilingData::Iterator& TilingData::Iterator::operator++() { |
| 228 if (!*this) |
| 229 return *this; |
| 230 |
| 231 index_x_++; |
| 232 |
| 233 bool new_row = index_x_ >= tiling_data_->num_tiles_x(); |
| 234 if (!new_row) { |
| 235 gfx::Rect new_rect = tiling_data_->TileBoundsWithBorder(index_x_, index_y_); |
| 236 new_row = new_rect.x() >= rect_.right(); |
| 237 } |
| 238 |
| 239 if (new_row) { |
| 240 index_x_ = tiling_data_->BorderTileXIndexFromSrcCoord(rect_.x()); |
| 241 index_y_++; |
| 242 |
| 243 if (index_y_ >= tiling_data_->num_tiles_y()) { |
| 244 done(); |
| 245 } else { |
| 246 gfx::Rect new_rect = |
| 247 tiling_data_->TileBoundsWithBorder(index_x_, index_y_); |
| 248 if (new_rect.y() >= rect_.bottom()) |
| 249 done(); |
| 250 } |
| 251 } |
| 252 |
| 253 return *this; |
| 254 } |
| 255 |
| 256 TilingData::Iterator::operator bool() const { |
| 257 return index_x_ != -1 && index_y_ != -1; |
| 258 } |
| 259 |
| 260 void TilingData::Iterator::done() { |
| 261 index_x_ = -1; |
| 262 index_y_ = -1; |
| 263 } |
| 264 |
172 } // namespace cc | 265 } // namespace cc |
OLD | NEW |