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
3 // found in the LICENSE file.
5 #include "cc/base/tiling_data.h"
9 #include "ui/gfx/rect.h"
10 #include "ui/gfx/vector2d.h"
14 static int ComputeNumTiles(int max_texture_size,
17 if (max_texture_size - 2 * border_texels <= 0)
18 return total_size > 0 && max_texture_size >= total_size ? 1 : 0;
20 int num_tiles = std::max(1,
21 1 + (total_size - 1 - 2 * border_texels) /
22 (max_texture_size - 2 * border_texels));
23 return total_size > 0 ? num_tiles : 0;
26 TilingData::TilingData()
31 TilingData::TilingData(
32 const gfx::Size& max_texture_size,
33 const gfx::Size& total_size,
34 bool has_border_texels)
35 : max_texture_size_(max_texture_size),
36 total_size_(total_size),
37 border_texels_(has_border_texels ? 1 : 0) {
41 TilingData::TilingData(
42 const gfx::Size& max_texture_size,
43 const gfx::Size& total_size,
45 : max_texture_size_(max_texture_size),
46 total_size_(total_size),
47 border_texels_(border_texels) {
51 void TilingData::SetTotalSize(const gfx::Size& total_size) {
52 total_size_ = total_size;
56 void TilingData::SetMaxTextureSize(const gfx::Size& max_texture_size) {
57 max_texture_size_ = max_texture_size;
61 void TilingData::SetHasBorderTexels(bool has_border_texels) {
62 border_texels_ = has_border_texels ? 1 : 0;
66 void TilingData::SetBorderTexels(int border_texels) {
67 border_texels_ = border_texels;
71 int TilingData::TileXIndexFromSrcCoord(int src_position) const {
72 if (num_tiles_x_ <= 1)
75 DCHECK_GT(max_texture_size_.width() - 2 * border_texels_, 0);
76 int x = (src_position - border_texels_) /
77 (max_texture_size_.width() - 2 * border_texels_);
78 return std::min(std::max(x, 0), num_tiles_x_ - 1);
81 int TilingData::TileYIndexFromSrcCoord(int src_position) const {
82 if (num_tiles_y_ <= 1)
85 DCHECK_GT(max_texture_size_.height() - 2 * border_texels_, 0);
86 int y = (src_position - border_texels_) /
87 (max_texture_size_.height() - 2 * border_texels_);
88 return std::min(std::max(y, 0), num_tiles_y_ - 1);
91 int TilingData::FirstBorderTileXIndexFromSrcCoord(int src_position) const {
92 if (num_tiles_x_ <= 1)
95 DCHECK_GT(max_texture_size_.width() - 2 * border_texels_, 0);
96 int inner_tile_size = max_texture_size_.width() - 2 * border_texels_;
97 int x = (src_position - 2 * border_texels_) / inner_tile_size;
98 return std::min(std::max(x, 0), num_tiles_x_ - 1);
101 int TilingData::FirstBorderTileYIndexFromSrcCoord(int src_position) const {
102 if (num_tiles_y_ <= 1)
105 DCHECK_GT(max_texture_size_.height() - 2 * border_texels_, 0);
106 int inner_tile_size = max_texture_size_.height() - 2 * border_texels_;
107 int y = (src_position - 2 * border_texels_) / inner_tile_size;
108 return std::min(std::max(y, 0), num_tiles_y_ - 1);
111 int TilingData::LastBorderTileXIndexFromSrcCoord(int src_position) const {
112 if (num_tiles_x_ <= 1)
115 DCHECK_GT(max_texture_size_.width() - 2 * border_texels_, 0);
116 int inner_tile_size = max_texture_size_.width() - 2 * border_texels_;
117 int x = src_position / inner_tile_size;
118 return std::min(std::max(x, 0), num_tiles_x_ - 1);
121 int TilingData::LastBorderTileYIndexFromSrcCoord(int src_position) const {
122 if (num_tiles_y_ <= 1)
125 DCHECK_GT(max_texture_size_.height() - 2 * border_texels_, 0);
126 int inner_tile_size = max_texture_size_.height() - 2 * border_texels_;
127 int y = src_position / inner_tile_size;
128 return std::min(std::max(y, 0), num_tiles_y_ - 1);
131 gfx::Rect TilingData::ExpandRectToTileBoundsWithBorders(
132 const gfx::Rect rect) const {
133 if (!rect.Intersects(gfx::Rect(total_size_)) || has_empty_bounds())
135 int index_x = FirstBorderTileXIndexFromSrcCoord(rect.x());
136 int index_y = FirstBorderTileYIndexFromSrcCoord(rect.y());
137 int index_right = LastBorderTileXIndexFromSrcCoord(rect.right());
138 int index_bottom = LastBorderTileYIndexFromSrcCoord(rect.bottom());
140 gfx::Rect rect_top_left(TileBoundsWithBorder(index_x, index_y));
141 gfx::Rect rect_bottom_right(TileBoundsWithBorder(index_right, index_bottom));
143 gfx::Rect expanded(rect_top_left);
144 expanded.Union(rect_bottom_right);
148 gfx::Rect TilingData::TileBounds(int i, int j) const {
150 int max_texture_size_x = max_texture_size_.width() - 2 * border_texels_;
151 int max_texture_size_y = max_texture_size_.height() - 2 * border_texels_;
152 int total_size_x = total_size_.width();
153 int total_size_y = total_size_.height();
155 int lo_x = max_texture_size_x * i;
157 lo_x += border_texels_;
159 int lo_y = max_texture_size_y * j;
161 lo_y += border_texels_;
163 int hi_x = max_texture_size_x * (i + 1) + border_texels_;
164 if (i + 1 == num_tiles_x_)
165 hi_x += border_texels_;
167 int hi_y = max_texture_size_y * (j + 1) + border_texels_;
168 if (j + 1 == num_tiles_y_)
169 hi_y += border_texels_;
171 hi_x = std::min(hi_x, total_size_x);
172 hi_y = std::min(hi_y, total_size_y);
176 int width = hi_x - lo_x;
177 int height = hi_y - lo_y;
181 DCHECK_GE(height, 0);
182 DCHECK_LE(x, total_size_.width());
183 DCHECK_LE(y, total_size_.height());
184 return gfx::Rect(x, y, width, height);
187 gfx::Rect TilingData::TileBoundsWithBorder(int i, int j) const {
189 int max_texture_size_x = max_texture_size_.width() - 2 * border_texels_;
190 int max_texture_size_y = max_texture_size_.height() - 2 * border_texels_;
191 int total_size_x = total_size_.width();
192 int total_size_y = total_size_.height();
194 int lo_x = max_texture_size_x * i;
195 int lo_y = max_texture_size_y * j;
197 int hi_x = lo_x + max_texture_size_x + 2 * border_texels_;
198 int hi_y = lo_y + max_texture_size_y + 2 * border_texels_;
200 hi_x = std::min(hi_x, total_size_x);
201 hi_y = std::min(hi_y, total_size_y);
205 int width = hi_x - lo_x;
206 int height = hi_y - lo_y;
210 DCHECK_GE(height, 0);
211 DCHECK_LE(x, total_size_.width());
212 DCHECK_LE(y, total_size_.height());
213 return gfx::Rect(x, y, width, height);
216 int TilingData::TilePositionX(int x_index) const {
217 DCHECK_GE(x_index, 0);
218 DCHECK_LT(x_index, num_tiles_x_);
220 int pos = (max_texture_size_.width() - 2 * border_texels_) * x_index;
222 pos += border_texels_;
227 int TilingData::TilePositionY(int y_index) const {
228 DCHECK_GE(y_index, 0);
229 DCHECK_LT(y_index, num_tiles_y_);
231 int pos = (max_texture_size_.height() - 2 * border_texels_) * y_index;
233 pos += border_texels_;
238 int TilingData::TileSizeX(int x_index) const {
239 DCHECK_GE(x_index, 0);
240 DCHECK_LT(x_index, num_tiles_x_);
242 if (!x_index && num_tiles_x_ == 1)
243 return total_size_.width();
244 if (!x_index && num_tiles_x_ > 1)
245 return max_texture_size_.width() - border_texels_;
246 if (x_index < num_tiles_x_ - 1)
247 return max_texture_size_.width() - 2 * border_texels_;
248 if (x_index == num_tiles_x_ - 1)
249 return total_size_.width() - TilePositionX(x_index);
255 int TilingData::TileSizeY(int y_index) const {
256 DCHECK_GE(y_index, 0);
257 DCHECK_LT(y_index, num_tiles_y_);
259 if (!y_index && num_tiles_y_ == 1)
260 return total_size_.height();
261 if (!y_index && num_tiles_y_ > 1)
262 return max_texture_size_.height() - border_texels_;
263 if (y_index < num_tiles_y_ - 1)
264 return max_texture_size_.height() - 2 * border_texels_;
265 if (y_index == num_tiles_y_ - 1)
266 return total_size_.height() - TilePositionY(y_index);
272 gfx::Vector2d TilingData::TextureOffset(int x_index, int y_index) const {
273 int left = (!x_index || num_tiles_x_ == 1) ? 0 : border_texels_;
274 int top = (!y_index || num_tiles_y_ == 1) ? 0 : border_texels_;
276 return gfx::Vector2d(left, top);
279 void TilingData::RecomputeNumTiles() {
280 num_tiles_x_ = ComputeNumTiles(
281 max_texture_size_.width(), total_size_.width(), border_texels_);
282 num_tiles_y_ = ComputeNumTiles(
283 max_texture_size_.height(), total_size_.height(), border_texels_);
286 TilingData::BaseIterator::BaseIterator(const TilingData* tiling_data)
287 : tiling_data_(tiling_data),
292 TilingData::Iterator::Iterator() : BaseIterator(NULL) { done(); }
294 TilingData::Iterator::Iterator(const TilingData* tiling_data,
295 const gfx::Rect& tiling_rect,
296 bool include_borders)
297 : BaseIterator(tiling_data), left_(-1), right_(-1), bottom_(-1) {
298 if (tiling_data_->num_tiles_x() <= 0 || tiling_data_->num_tiles_y() <= 0) {
303 gfx::Rect rect(tiling_rect);
304 rect.Intersect(gfx::Rect(tiling_data_->total_size()));
306 gfx::Rect top_left_tile;
307 if (include_borders) {
308 index_x_ = tiling_data_->FirstBorderTileXIndexFromSrcCoord(rect.x());
309 index_y_ = tiling_data_->FirstBorderTileYIndexFromSrcCoord(rect.y());
310 right_ = tiling_data_->LastBorderTileXIndexFromSrcCoord(rect.right() - 1);
311 bottom_ = tiling_data_->LastBorderTileYIndexFromSrcCoord(rect.bottom() - 1);
312 top_left_tile = tiling_data_->TileBoundsWithBorder(index_x_, index_y_);
314 index_x_ = tiling_data_->TileXIndexFromSrcCoord(rect.x());
315 index_y_ = tiling_data_->TileYIndexFromSrcCoord(rect.y());
316 right_ = tiling_data_->TileXIndexFromSrcCoord(rect.right() - 1);
317 bottom_ = tiling_data_->TileYIndexFromSrcCoord(rect.bottom() - 1);
318 top_left_tile = tiling_data_->TileBounds(index_x_, index_y_);
322 // Index functions always return valid indices, so explicitly check
323 // for non-intersecting rects.
324 if (!top_left_tile.Intersects(rect))
328 TilingData::Iterator& TilingData::Iterator::operator++() {
333 if (index_x_ > right_) {
336 if (index_y_ > bottom_)
343 TilingData::DifferenceIterator::DifferenceIterator(
344 const TilingData* tiling_data,
345 const gfx::Rect& consider_rect,
346 const gfx::Rect& ignore_rect)
347 : BaseIterator(tiling_data),
351 consider_bottom_(-1),
356 if (tiling_data_->num_tiles_x() <= 0 || tiling_data_->num_tiles_y() <= 0) {
361 gfx::Rect bounds(tiling_data_->total_size());
362 gfx::Rect consider(consider_rect);
363 gfx::Rect ignore(ignore_rect);
364 consider.Intersect(bounds);
365 ignore.Intersect(bounds);
366 if (consider.IsEmpty()) {
372 tiling_data_->FirstBorderTileXIndexFromSrcCoord(consider.x());
374 tiling_data_->FirstBorderTileYIndexFromSrcCoord(consider.y());
376 tiling_data_->LastBorderTileXIndexFromSrcCoord(consider.right() - 1);
378 tiling_data_->LastBorderTileYIndexFromSrcCoord(consider.bottom() - 1);
380 if (!ignore.IsEmpty()) {
382 tiling_data_->FirstBorderTileXIndexFromSrcCoord(ignore.x());
384 tiling_data_->FirstBorderTileYIndexFromSrcCoord(ignore.y());
386 tiling_data_->LastBorderTileXIndexFromSrcCoord(ignore.right() - 1);
388 tiling_data_->LastBorderTileYIndexFromSrcCoord(ignore.bottom() - 1);
390 // Clamp ignore indices to consider indices.
391 ignore_left_ = std::max(ignore_left_, consider_left_);
392 ignore_top_ = std::max(ignore_top_, consider_top_);
393 ignore_right_ = std::min(ignore_right_, consider_right_);
394 ignore_bottom_ = std::min(ignore_bottom_, consider_bottom_);
397 if (ignore_left_ == consider_left_ && ignore_right_ == consider_right_ &&
398 ignore_top_ == consider_top_ && ignore_bottom_ == consider_bottom_) {
403 index_x_ = consider_left_;
404 index_y_ = consider_top_;
406 if (in_ignore_rect())
410 TilingData::DifferenceIterator& TilingData::DifferenceIterator::operator++() {
415 if (in_ignore_rect())
416 index_x_ = ignore_right_ + 1;
418 if (index_x_ > consider_right_) {
419 index_x_ = consider_left_;
422 if (in_ignore_rect()) {
423 index_x_ = ignore_right_ + 1;
424 // If the ignore rect spans the whole consider rect horizontally, then
425 // ignore_right + 1 will be out of bounds.
426 if (in_ignore_rect() || index_x_ > consider_right_) {
427 index_y_ = ignore_bottom_ + 1;
428 index_x_ = consider_left_;
432 if (index_y_ > consider_bottom_)
439 TilingData::SpiralDifferenceIterator::SpiralDifferenceIterator()
440 : BaseIterator(NULL) {
444 TilingData::SpiralDifferenceIterator::SpiralDifferenceIterator(
445 const TilingData* tiling_data,
446 const gfx::Rect& consider_rect,
447 const gfx::Rect& ignore_rect,
448 const gfx::Rect& center_rect)
449 : BaseIterator(tiling_data),
453 consider_bottom_(-1),
462 horizontal_step_count_(0),
463 vertical_step_count_(0) {
464 if (tiling_data_->num_tiles_x() <= 0 || tiling_data_->num_tiles_y() <= 0) {
469 gfx::Rect bounds(tiling_data_->total_size());
470 gfx::Rect consider(consider_rect);
471 gfx::Rect ignore(ignore_rect);
472 gfx::Rect center(center_rect);
473 consider.Intersect(bounds);
474 ignore.Intersect(bounds);
475 if (consider.IsEmpty()) {
481 tiling_data_->FirstBorderTileXIndexFromSrcCoord(consider.x());
482 consider_top_ = tiling_data_->FirstBorderTileYIndexFromSrcCoord(consider.y());
484 tiling_data_->LastBorderTileXIndexFromSrcCoord(consider.right() - 1);
486 tiling_data_->LastBorderTileYIndexFromSrcCoord(consider.bottom() - 1);
488 if (!ignore.IsEmpty()) {
489 ignore_left_ = tiling_data_->FirstBorderTileXIndexFromSrcCoord(ignore.x());
490 ignore_top_ = tiling_data_->FirstBorderTileYIndexFromSrcCoord(ignore.y());
492 tiling_data_->LastBorderTileXIndexFromSrcCoord(ignore.right() - 1);
494 tiling_data_->LastBorderTileYIndexFromSrcCoord(ignore.bottom() - 1);
496 // Clamp ignore indices to consider indices.
497 ignore_left_ = std::max(ignore_left_, consider_left_);
498 ignore_top_ = std::max(ignore_top_, consider_top_);
499 ignore_right_ = std::min(ignore_right_, consider_right_);
500 ignore_bottom_ = std::min(ignore_bottom_, consider_bottom_);
503 if (ignore_left_ == consider_left_ && ignore_right_ == consider_right_ &&
504 ignore_top_ == consider_top_ && ignore_bottom_ == consider_bottom_) {
509 // Determine around left, such that it is between -1 and num_tiles_x.
511 if (center.x() < 0 || center.IsEmpty())
513 else if (center.x() > tiling_data->total_size().width())
514 around_left = tiling_data->num_tiles_x();
516 around_left = tiling_data->FirstBorderTileXIndexFromSrcCoord(center.x());
518 // Determine around top, such that it is between -1 and num_tiles_y.
520 if (center.y() < 0 || center.IsEmpty())
522 else if (center.y() > tiling_data->total_size().height())
523 around_top = tiling_data->num_tiles_y();
525 around_top = tiling_data->FirstBorderTileYIndexFromSrcCoord(center.y());
527 // Determine around right, such that it is between -1 and num_tiles_x.
528 int right_src_coord = center.right() - 1;
529 int around_right = 0;
530 if (right_src_coord < 0 || center.IsEmpty()) {
532 } else if (right_src_coord > tiling_data->total_size().width()) {
533 around_right = tiling_data->num_tiles_x();
536 tiling_data->LastBorderTileXIndexFromSrcCoord(right_src_coord);
539 // Determine around bottom, such that it is between -1 and num_tiles_y.
540 int bottom_src_coord = center.bottom() - 1;
541 int around_bottom = 0;
542 if (bottom_src_coord < 0 || center.IsEmpty()) {
544 } else if (bottom_src_coord > tiling_data->total_size().height()) {
545 around_bottom = tiling_data->num_tiles_y();
548 tiling_data->LastBorderTileYIndexFromSrcCoord(bottom_src_coord);
551 vertical_step_count_ = around_bottom - around_top + 1;
552 horizontal_step_count_ = around_right - around_left + 1;
553 current_step_ = horizontal_step_count_ - 1;
555 index_x_ = around_right;
556 index_y_ = around_bottom;
558 // The current index is the bottom right of the around rect, which is also
559 // ignored. So we have to advance.
563 TilingData::SpiralDifferenceIterator& TilingData::SpiralDifferenceIterator::
565 int cannot_hit_consider_count = 0;
566 while (cannot_hit_consider_count < 4) {
567 if (needs_direction_switch())
570 index_x_ += delta_x_;
571 index_y_ += delta_y_;
574 if (in_consider_rect()) {
575 cannot_hit_consider_count = 0;
577 if (!in_ignore_rect())
580 // Steps needed to reach the very edge of the ignore rect, while remaining
581 // inside (so that the continue would take us outside).
582 int steps_to_edge = 0;
583 switch (direction_) {
585 steps_to_edge = index_y_ - ignore_top_;
588 steps_to_edge = index_x_ - ignore_left_;
591 steps_to_edge = ignore_bottom_ - index_y_;
594 steps_to_edge = ignore_right_ - index_x_;
598 // We need to switch directions in |max_steps|.
599 int max_steps = current_step_count() - current_step_;
601 int steps_to_take = std::min(steps_to_edge, max_steps);
602 DCHECK_GE(steps_to_take, 0);
604 index_x_ += steps_to_take * delta_x_;
605 index_y_ += steps_to_take * delta_y_;
606 current_step_ += steps_to_take;
608 int max_steps = current_step_count() - current_step_;
609 int steps_to_take = max_steps;
610 bool can_hit_consider_rect = false;
611 switch (direction_) {
613 if (valid_column() && consider_bottom_ < index_y_)
614 steps_to_take = index_y_ - consider_bottom_ - 1;
615 can_hit_consider_rect |= consider_right_ >= index_x_;
618 if (valid_row() && consider_right_ < index_x_)
619 steps_to_take = index_x_ - consider_right_ - 1;
620 can_hit_consider_rect |= consider_top_ <= index_y_;
623 if (valid_column() && consider_top_ > index_y_)
624 steps_to_take = consider_top_ - index_y_ - 1;
625 can_hit_consider_rect |= consider_left_ <= index_x_;
628 if (valid_row() && consider_left_ > index_x_)
629 steps_to_take = consider_left_ - index_x_ - 1;
630 can_hit_consider_rect |= consider_bottom_ >= index_y_;
633 steps_to_take = std::min(steps_to_take, max_steps);
634 DCHECK_GE(steps_to_take, 0);
636 index_x_ += steps_to_take * delta_x_;
637 index_y_ += steps_to_take * delta_y_;
638 current_step_ += steps_to_take;
640 if (can_hit_consider_rect)
641 cannot_hit_consider_count = 0;
643 ++cannot_hit_consider_count;
647 if (cannot_hit_consider_count >= 4)
652 bool TilingData::SpiralDifferenceIterator::needs_direction_switch() const {
653 return current_step_ >= current_step_count();
656 void TilingData::SpiralDifferenceIterator::switch_direction() {
657 int new_delta_x_ = delta_y_;
658 delta_y_ = -delta_x_;
659 delta_x_ = new_delta_x_;
662 direction_ = static_cast<Direction>((direction_ + 1) % 4);
664 if (direction_ == RIGHT || direction_ == LEFT) {
665 ++vertical_step_count_;
666 ++horizontal_step_count_;