Switch to QUIC version 24.
[chromium-blink-merge.git] / cc / base / tiling_data.cc
blob95fd68be200bf6d91d324113ad07d33b4f50b6bf
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"
7 #include <algorithm>
9 #include "ui/gfx/geometry/rect.h"
10 #include "ui/gfx/geometry/vector2d.h"
12 namespace cc {
14 static int ComputeNumTiles(int max_texture_size,
15 int total_size,
16 int border_texels) {
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()
27 : border_texels_(0) {
28 RecomputeNumTiles();
31 TilingData::TilingData(const gfx::Size& max_texture_size,
32 const gfx::Size& tiling_size,
33 bool has_border_texels)
34 : max_texture_size_(max_texture_size),
35 tiling_size_(tiling_size),
36 border_texels_(has_border_texels ? 1 : 0) {
37 RecomputeNumTiles();
40 TilingData::TilingData(const gfx::Size& max_texture_size,
41 const gfx::Size& tiling_size,
42 int border_texels)
43 : max_texture_size_(max_texture_size),
44 tiling_size_(tiling_size),
45 border_texels_(border_texels) {
46 RecomputeNumTiles();
49 void TilingData::SetTilingSize(const gfx::Size& tiling_size) {
50 tiling_size_ = tiling_size;
51 RecomputeNumTiles();
54 void TilingData::SetMaxTextureSize(const gfx::Size& max_texture_size) {
55 max_texture_size_ = max_texture_size;
56 RecomputeNumTiles();
59 void TilingData::SetHasBorderTexels(bool has_border_texels) {
60 border_texels_ = has_border_texels ? 1 : 0;
61 RecomputeNumTiles();
64 void TilingData::SetBorderTexels(int border_texels) {
65 border_texels_ = border_texels;
66 RecomputeNumTiles();
69 int TilingData::TileXIndexFromSrcCoord(int src_position) const {
70 if (num_tiles_x_ <= 1)
71 return 0;
73 DCHECK_GT(max_texture_size_.width() - 2 * border_texels_, 0);
74 int x = (src_position - border_texels_) /
75 (max_texture_size_.width() - 2 * border_texels_);
76 return std::min(std::max(x, 0), num_tiles_x_ - 1);
79 int TilingData::TileYIndexFromSrcCoord(int src_position) const {
80 if (num_tiles_y_ <= 1)
81 return 0;
83 DCHECK_GT(max_texture_size_.height() - 2 * border_texels_, 0);
84 int y = (src_position - border_texels_) /
85 (max_texture_size_.height() - 2 * border_texels_);
86 return std::min(std::max(y, 0), num_tiles_y_ - 1);
89 int TilingData::FirstBorderTileXIndexFromSrcCoord(int src_position) const {
90 if (num_tiles_x_ <= 1)
91 return 0;
93 DCHECK_GT(max_texture_size_.width() - 2 * border_texels_, 0);
94 int inner_tile_size = max_texture_size_.width() - 2 * border_texels_;
95 int x = (src_position - 2 * border_texels_) / inner_tile_size;
96 return std::min(std::max(x, 0), num_tiles_x_ - 1);
99 int TilingData::FirstBorderTileYIndexFromSrcCoord(int src_position) const {
100 if (num_tiles_y_ <= 1)
101 return 0;
103 DCHECK_GT(max_texture_size_.height() - 2 * border_texels_, 0);
104 int inner_tile_size = max_texture_size_.height() - 2 * border_texels_;
105 int y = (src_position - 2 * border_texels_) / inner_tile_size;
106 return std::min(std::max(y, 0), num_tiles_y_ - 1);
109 int TilingData::LastBorderTileXIndexFromSrcCoord(int src_position) const {
110 if (num_tiles_x_ <= 1)
111 return 0;
113 DCHECK_GT(max_texture_size_.width() - 2 * border_texels_, 0);
114 int inner_tile_size = max_texture_size_.width() - 2 * border_texels_;
115 int x = src_position / inner_tile_size;
116 return std::min(std::max(x, 0), num_tiles_x_ - 1);
119 int TilingData::LastBorderTileYIndexFromSrcCoord(int src_position) const {
120 if (num_tiles_y_ <= 1)
121 return 0;
123 DCHECK_GT(max_texture_size_.height() - 2 * border_texels_, 0);
124 int inner_tile_size = max_texture_size_.height() - 2 * border_texels_;
125 int y = src_position / inner_tile_size;
126 return std::min(std::max(y, 0), num_tiles_y_ - 1);
129 gfx::Rect TilingData::ExpandRectIgnoringBordersToTileBounds(
130 const gfx::Rect& rect) const {
131 if (rect.IsEmpty() || has_empty_bounds())
132 return gfx::Rect();
133 if (rect.x() > tiling_size_.width() || rect.y() > tiling_size_.height())
134 return gfx::Rect();
135 int index_x = TileXIndexFromSrcCoord(rect.x());
136 int index_y = TileYIndexFromSrcCoord(rect.y());
137 int index_right = TileXIndexFromSrcCoord(rect.right() - 1);
138 int index_bottom = TileYIndexFromSrcCoord(rect.bottom() - 1);
140 gfx::Rect rect_top_left(TileBounds(index_x, index_y));
141 gfx::Rect rect_bottom_right(TileBounds(index_right, index_bottom));
143 return gfx::UnionRects(rect_top_left, rect_bottom_right);
146 gfx::Rect TilingData::ExpandRectToTileBounds(const gfx::Rect& rect) const {
147 if (rect.IsEmpty() || has_empty_bounds())
148 return gfx::Rect();
149 if (rect.x() > tiling_size_.width() || rect.y() > tiling_size_.height())
150 return gfx::Rect();
151 int index_x = FirstBorderTileXIndexFromSrcCoord(rect.x());
152 int index_y = FirstBorderTileYIndexFromSrcCoord(rect.y());
153 int index_right = LastBorderTileXIndexFromSrcCoord(rect.right() - 1);
154 int index_bottom = LastBorderTileYIndexFromSrcCoord(rect.bottom() - 1);
156 gfx::Rect rect_top_left(TileBounds(index_x, index_y));
157 gfx::Rect rect_bottom_right(TileBounds(index_right, index_bottom));
159 return gfx::UnionRects(rect_top_left, rect_bottom_right);
162 gfx::Rect TilingData::TileBounds(int i, int j) const {
163 AssertTile(i, j);
164 int max_texture_size_x = max_texture_size_.width() - 2 * border_texels_;
165 int max_texture_size_y = max_texture_size_.height() - 2 * border_texels_;
167 int lo_x = max_texture_size_x * i;
168 if (i != 0)
169 lo_x += border_texels_;
171 int lo_y = max_texture_size_y * j;
172 if (j != 0)
173 lo_y += border_texels_;
175 int hi_x = max_texture_size_x * (i + 1) + border_texels_;
176 if (i + 1 == num_tiles_x_)
177 hi_x += border_texels_;
179 int hi_y = max_texture_size_y * (j + 1) + border_texels_;
180 if (j + 1 == num_tiles_y_)
181 hi_y += border_texels_;
183 hi_x = std::min(hi_x, tiling_size_.width());
184 hi_y = std::min(hi_y, tiling_size_.height());
186 int x = lo_x;
187 int y = lo_y;
188 int width = hi_x - lo_x;
189 int height = hi_y - lo_y;
190 DCHECK_GE(x, 0);
191 DCHECK_GE(y, 0);
192 DCHECK_GE(width, 0);
193 DCHECK_GE(height, 0);
194 DCHECK_LE(x, tiling_size_.width());
195 DCHECK_LE(y, tiling_size_.height());
196 return gfx::Rect(x, y, width, height);
199 gfx::Rect TilingData::TileBoundsWithBorder(int i, int j) const {
200 AssertTile(i, j);
201 int max_texture_size_x = max_texture_size_.width() - 2 * border_texels_;
202 int max_texture_size_y = max_texture_size_.height() - 2 * border_texels_;
204 int lo_x = max_texture_size_x * i;
205 int lo_y = max_texture_size_y * j;
207 int hi_x = lo_x + max_texture_size_x + 2 * border_texels_;
208 int hi_y = lo_y + max_texture_size_y + 2 * border_texels_;
210 hi_x = std::min(hi_x, tiling_size_.width());
211 hi_y = std::min(hi_y, tiling_size_.height());
213 int x = lo_x;
214 int y = lo_y;
215 int width = hi_x - lo_x;
216 int height = hi_y - lo_y;
217 DCHECK_GE(x, 0);
218 DCHECK_GE(y, 0);
219 DCHECK_GE(width, 0);
220 DCHECK_GE(height, 0);
221 DCHECK_LE(x, tiling_size_.width());
222 DCHECK_LE(y, tiling_size_.height());
223 return gfx::Rect(x, y, width, height);
226 int TilingData::TilePositionX(int x_index) const {
227 DCHECK_GE(x_index, 0);
228 DCHECK_LT(x_index, num_tiles_x_);
230 int pos = (max_texture_size_.width() - 2 * border_texels_) * x_index;
231 if (x_index != 0)
232 pos += border_texels_;
234 return pos;
237 int TilingData::TilePositionY(int y_index) const {
238 DCHECK_GE(y_index, 0);
239 DCHECK_LT(y_index, num_tiles_y_);
241 int pos = (max_texture_size_.height() - 2 * border_texels_) * y_index;
242 if (y_index != 0)
243 pos += border_texels_;
245 return pos;
248 int TilingData::TileSizeX(int x_index) const {
249 DCHECK_GE(x_index, 0);
250 DCHECK_LT(x_index, num_tiles_x_);
252 if (!x_index && num_tiles_x_ == 1)
253 return tiling_size_.width();
254 if (!x_index && num_tiles_x_ > 1)
255 return max_texture_size_.width() - border_texels_;
256 if (x_index < num_tiles_x_ - 1)
257 return max_texture_size_.width() - 2 * border_texels_;
258 if (x_index == num_tiles_x_ - 1)
259 return tiling_size_.width() - TilePositionX(x_index);
261 NOTREACHED();
262 return 0;
265 int TilingData::TileSizeY(int y_index) const {
266 DCHECK_GE(y_index, 0);
267 DCHECK_LT(y_index, num_tiles_y_);
269 if (!y_index && num_tiles_y_ == 1)
270 return tiling_size_.height();
271 if (!y_index && num_tiles_y_ > 1)
272 return max_texture_size_.height() - border_texels_;
273 if (y_index < num_tiles_y_ - 1)
274 return max_texture_size_.height() - 2 * border_texels_;
275 if (y_index == num_tiles_y_ - 1)
276 return tiling_size_.height() - TilePositionY(y_index);
278 NOTREACHED();
279 return 0;
282 gfx::Vector2d TilingData::TextureOffset(int x_index, int y_index) const {
283 int left = (!x_index || num_tiles_x_ == 1) ? 0 : border_texels_;
284 int top = (!y_index || num_tiles_y_ == 1) ? 0 : border_texels_;
286 return gfx::Vector2d(left, top);
289 void TilingData::RecomputeNumTiles() {
290 num_tiles_x_ = ComputeNumTiles(
291 max_texture_size_.width(), tiling_size_.width(), border_texels_);
292 num_tiles_y_ = ComputeNumTiles(
293 max_texture_size_.height(), tiling_size_.height(), border_texels_);
296 TilingData::BaseIterator::BaseIterator() : index_x_(-1), index_y_(-1) {
299 TilingData::Iterator::Iterator() {
300 done();
303 TilingData::Iterator::Iterator(const TilingData* tiling_data,
304 const gfx::Rect& consider_rect,
305 bool include_borders)
306 : left_(-1), right_(-1), bottom_(-1) {
307 if (tiling_data->num_tiles_x() <= 0 || tiling_data->num_tiles_y() <= 0) {
308 done();
309 return;
312 gfx::Rect tiling_bounds_rect(tiling_data->tiling_size());
313 gfx::Rect rect(consider_rect);
314 rect.Intersect(tiling_bounds_rect);
316 gfx::Rect top_left_tile;
317 if (include_borders) {
318 index_x_ = tiling_data->FirstBorderTileXIndexFromSrcCoord(rect.x());
319 index_y_ = tiling_data->FirstBorderTileYIndexFromSrcCoord(rect.y());
320 right_ = tiling_data->LastBorderTileXIndexFromSrcCoord(rect.right() - 1);
321 bottom_ = tiling_data->LastBorderTileYIndexFromSrcCoord(rect.bottom() - 1);
322 top_left_tile = tiling_data->TileBoundsWithBorder(index_x_, index_y_);
323 } else {
324 index_x_ = tiling_data->TileXIndexFromSrcCoord(rect.x());
325 index_y_ = tiling_data->TileYIndexFromSrcCoord(rect.y());
326 right_ = tiling_data->TileXIndexFromSrcCoord(rect.right() - 1);
327 bottom_ = tiling_data->TileYIndexFromSrcCoord(rect.bottom() - 1);
328 top_left_tile = tiling_data->TileBounds(index_x_, index_y_);
330 left_ = index_x_;
332 // Index functions always return valid indices, so explicitly check
333 // for non-intersecting rects.
334 if (!top_left_tile.Intersects(rect))
335 done();
338 TilingData::Iterator& TilingData::Iterator::operator++() {
339 if (!*this)
340 return *this;
342 index_x_++;
343 if (index_x_ > right_) {
344 index_x_ = left_;
345 index_y_++;
346 if (index_y_ > bottom_)
347 done();
350 return *this;
353 TilingData::DifferenceIterator::DifferenceIterator(
354 const TilingData* tiling_data,
355 const gfx::Rect& consider_rect,
356 const gfx::Rect& ignore_rect)
357 : consider_left_(-1),
358 consider_top_(-1),
359 consider_right_(-1),
360 consider_bottom_(-1),
361 ignore_left_(-1),
362 ignore_top_(-1),
363 ignore_right_(-1),
364 ignore_bottom_(-1) {
365 if (tiling_data->num_tiles_x() <= 0 || tiling_data->num_tiles_y() <= 0) {
366 done();
367 return;
370 gfx::Rect tiling_bounds_rect(tiling_data->tiling_size());
371 gfx::Rect consider(consider_rect);
372 gfx::Rect ignore(ignore_rect);
373 consider.Intersect(tiling_bounds_rect);
374 ignore.Intersect(tiling_bounds_rect);
375 if (consider.IsEmpty()) {
376 done();
377 return;
380 consider_left_ = tiling_data->TileXIndexFromSrcCoord(consider.x());
381 consider_top_ = tiling_data->TileYIndexFromSrcCoord(consider.y());
382 consider_right_ = tiling_data->TileXIndexFromSrcCoord(consider.right() - 1);
383 consider_bottom_ = tiling_data->TileYIndexFromSrcCoord(consider.bottom() - 1);
385 if (!ignore.IsEmpty()) {
386 ignore_left_ = tiling_data->TileXIndexFromSrcCoord(ignore.x());
387 ignore_top_ = tiling_data->TileYIndexFromSrcCoord(ignore.y());
388 ignore_right_ = tiling_data->TileXIndexFromSrcCoord(ignore.right() - 1);
389 ignore_bottom_ = tiling_data->TileYIndexFromSrcCoord(ignore.bottom() - 1);
391 // Clamp ignore indices to consider indices.
392 ignore_left_ = std::max(ignore_left_, consider_left_);
393 ignore_top_ = std::max(ignore_top_, consider_top_);
394 ignore_right_ = std::min(ignore_right_, consider_right_);
395 ignore_bottom_ = std::min(ignore_bottom_, consider_bottom_);
398 if (ignore_left_ == consider_left_ && ignore_right_ == consider_right_ &&
399 ignore_top_ == consider_top_ && ignore_bottom_ == consider_bottom_) {
400 done();
401 return;
404 index_x_ = consider_left_;
405 index_y_ = consider_top_;
407 if (in_ignore_rect())
408 ++(*this);
411 TilingData::DifferenceIterator& TilingData::DifferenceIterator::operator++() {
412 if (!*this)
413 return *this;
415 index_x_++;
416 if (in_ignore_rect())
417 index_x_ = ignore_right_ + 1;
419 if (index_x_ > consider_right_) {
420 index_x_ = consider_left_;
421 index_y_++;
423 if (in_ignore_rect()) {
424 index_x_ = ignore_right_ + 1;
425 // If the ignore rect spans the whole consider rect horizontally, then
426 // ignore_right + 1 will be out of bounds.
427 if (in_ignore_rect() || index_x_ > consider_right_) {
428 index_y_ = ignore_bottom_ + 1;
429 index_x_ = consider_left_;
433 if (index_y_ > consider_bottom_)
434 done();
437 return *this;
440 TilingData::SpiralDifferenceIterator::SpiralDifferenceIterator() {
441 done();
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 : consider_left_(-1),
450 consider_top_(-1),
451 consider_right_(-1),
452 consider_bottom_(-1),
453 ignore_left_(-1),
454 ignore_top_(-1),
455 ignore_right_(-1),
456 ignore_bottom_(-1),
457 direction_(RIGHT),
458 delta_x_(1),
459 delta_y_(0),
460 current_step_(0),
461 horizontal_step_count_(0),
462 vertical_step_count_(0) {
463 if (tiling_data->num_tiles_x() <= 0 || tiling_data->num_tiles_y() <= 0) {
464 done();
465 return;
468 gfx::Rect tiling_bounds_rect(tiling_data->tiling_size());
469 gfx::Rect consider(consider_rect);
470 gfx::Rect ignore(ignore_rect);
471 gfx::Rect center(center_rect);
472 consider.Intersect(tiling_bounds_rect);
473 ignore.Intersect(tiling_bounds_rect);
474 if (consider.IsEmpty()) {
475 done();
476 return;
479 consider_left_ = tiling_data->TileXIndexFromSrcCoord(consider.x());
480 consider_top_ = tiling_data->TileYIndexFromSrcCoord(consider.y());
481 consider_right_ = tiling_data->TileXIndexFromSrcCoord(consider.right() - 1);
482 consider_bottom_ = tiling_data->TileYIndexFromSrcCoord(consider.bottom() - 1);
484 if (!ignore.IsEmpty()) {
485 ignore_left_ = tiling_data->TileXIndexFromSrcCoord(ignore.x());
486 ignore_top_ = tiling_data->TileYIndexFromSrcCoord(ignore.y());
487 ignore_right_ = tiling_data->TileXIndexFromSrcCoord(ignore.right() - 1);
488 ignore_bottom_ = tiling_data->TileYIndexFromSrcCoord(ignore.bottom() - 1);
490 // Clamp ignore indices to consider indices.
491 ignore_left_ = std::max(ignore_left_, consider_left_);
492 ignore_top_ = std::max(ignore_top_, consider_top_);
493 ignore_right_ = std::min(ignore_right_, consider_right_);
494 ignore_bottom_ = std::min(ignore_bottom_, consider_bottom_);
497 if (ignore_left_ == consider_left_ && ignore_right_ == consider_right_ &&
498 ignore_top_ == consider_top_ && ignore_bottom_ == consider_bottom_) {
499 done();
500 return;
503 // Determine around left, such that it is between -1 and num_tiles_x.
504 int around_left = 0;
505 if (center.x() < 0 || center.IsEmpty())
506 around_left = -1;
507 else if (center.x() >= tiling_data->tiling_size().width())
508 around_left = tiling_data->num_tiles_x();
509 else
510 around_left = tiling_data->TileXIndexFromSrcCoord(center.x());
512 // Determine around top, such that it is between -1 and num_tiles_y.
513 int around_top = 0;
514 if (center.y() < 0 || center.IsEmpty())
515 around_top = -1;
516 else if (center.y() >= tiling_data->tiling_size().height())
517 around_top = tiling_data->num_tiles_y();
518 else
519 around_top = tiling_data->TileYIndexFromSrcCoord(center.y());
521 // Determine around right, such that it is between -1 and num_tiles_x.
522 int right_src_coord = center.right() - 1;
523 int around_right = 0;
524 if (right_src_coord < 0 || center.IsEmpty()) {
525 around_right = -1;
526 } else if (right_src_coord >= tiling_data->tiling_size().width()) {
527 around_right = tiling_data->num_tiles_x();
528 } else {
529 around_right = tiling_data->TileXIndexFromSrcCoord(right_src_coord);
532 // Determine around bottom, such that it is between -1 and num_tiles_y.
533 int bottom_src_coord = center.bottom() - 1;
534 int around_bottom = 0;
535 if (bottom_src_coord < 0 || center.IsEmpty()) {
536 around_bottom = -1;
537 } else if (bottom_src_coord >= tiling_data->tiling_size().height()) {
538 around_bottom = tiling_data->num_tiles_y();
539 } else {
540 around_bottom = tiling_data->TileYIndexFromSrcCoord(bottom_src_coord);
543 vertical_step_count_ = around_bottom - around_top + 1;
544 horizontal_step_count_ = around_right - around_left + 1;
545 current_step_ = horizontal_step_count_ - 1;
547 index_x_ = around_right;
548 index_y_ = around_bottom;
550 // The current index is the bottom right of the around rect, which is also
551 // ignored. So we have to advance.
552 ++(*this);
555 TilingData::SpiralDifferenceIterator& TilingData::SpiralDifferenceIterator::
556 operator++() {
557 int cannot_hit_consider_count = 0;
558 while (cannot_hit_consider_count < 4) {
559 if (needs_direction_switch())
560 switch_direction();
562 index_x_ += delta_x_;
563 index_y_ += delta_y_;
564 ++current_step_;
566 if (in_consider_rect()) {
567 cannot_hit_consider_count = 0;
569 if (!in_ignore_rect())
570 break;
572 // Steps needed to reach the very edge of the ignore rect, while remaining
573 // inside (so that the continue would take us outside).
574 int steps_to_edge = 0;
575 switch (direction_) {
576 case UP:
577 steps_to_edge = index_y_ - ignore_top_;
578 break;
579 case LEFT:
580 steps_to_edge = index_x_ - ignore_left_;
581 break;
582 case DOWN:
583 steps_to_edge = ignore_bottom_ - index_y_;
584 break;
585 case RIGHT:
586 steps_to_edge = ignore_right_ - index_x_;
587 break;
590 // We need to switch directions in |max_steps|.
591 int max_steps = current_step_count() - current_step_;
593 int steps_to_take = std::min(steps_to_edge, max_steps);
594 DCHECK_GE(steps_to_take, 0);
596 index_x_ += steps_to_take * delta_x_;
597 index_y_ += steps_to_take * delta_y_;
598 current_step_ += steps_to_take;
599 } else {
600 int max_steps = current_step_count() - current_step_;
601 int steps_to_take = max_steps;
602 bool can_hit_consider_rect = false;
603 switch (direction_) {
604 case UP:
605 if (valid_column() && consider_bottom_ < index_y_)
606 steps_to_take = index_y_ - consider_bottom_ - 1;
607 can_hit_consider_rect |= consider_right_ >= index_x_;
608 break;
609 case LEFT:
610 if (valid_row() && consider_right_ < index_x_)
611 steps_to_take = index_x_ - consider_right_ - 1;
612 can_hit_consider_rect |= consider_top_ <= index_y_;
613 break;
614 case DOWN:
615 if (valid_column() && consider_top_ > index_y_)
616 steps_to_take = consider_top_ - index_y_ - 1;
617 can_hit_consider_rect |= consider_left_ <= index_x_;
618 break;
619 case RIGHT:
620 if (valid_row() && consider_left_ > index_x_)
621 steps_to_take = consider_left_ - index_x_ - 1;
622 can_hit_consider_rect |= consider_bottom_ >= index_y_;
623 break;
625 steps_to_take = std::min(steps_to_take, max_steps);
626 DCHECK_GE(steps_to_take, 0);
628 index_x_ += steps_to_take * delta_x_;
629 index_y_ += steps_to_take * delta_y_;
630 current_step_ += steps_to_take;
632 if (can_hit_consider_rect)
633 cannot_hit_consider_count = 0;
634 else
635 ++cannot_hit_consider_count;
639 if (cannot_hit_consider_count >= 4)
640 done();
641 return *this;
644 bool TilingData::SpiralDifferenceIterator::needs_direction_switch() const {
645 return current_step_ >= current_step_count();
648 void TilingData::SpiralDifferenceIterator::switch_direction() {
649 // Note that delta_x_ and delta_y_ always remain between -1 and 1.
650 int new_delta_x_ = delta_y_;
651 delta_y_ = -delta_x_;
652 delta_x_ = new_delta_x_;
654 current_step_ = 0;
655 direction_ = static_cast<Direction>((direction_ + 1) % 4);
657 if (direction_ == RIGHT || direction_ == LEFT) {
658 ++vertical_step_count_;
659 ++horizontal_step_count_;
663 TilingData::ReverseSpiralDifferenceIterator::ReverseSpiralDifferenceIterator() {
664 done();
667 TilingData::ReverseSpiralDifferenceIterator::ReverseSpiralDifferenceIterator(
668 const TilingData* tiling_data,
669 const gfx::Rect& consider_rect,
670 const gfx::Rect& ignore_rect,
671 const gfx::Rect& center_rect)
672 : consider_left_(-1),
673 consider_top_(-1),
674 consider_right_(-1),
675 consider_bottom_(-1),
676 around_left_(-1),
677 around_top_(-1),
678 around_right_(-1),
679 around_bottom_(-1),
680 ignore_left_(-1),
681 ignore_top_(-1),
682 ignore_right_(-1),
683 ignore_bottom_(-1),
684 direction_(LEFT),
685 delta_x_(-1),
686 delta_y_(0),
687 current_step_(0),
688 horizontal_step_count_(0),
689 vertical_step_count_(0) {
690 if (tiling_data->num_tiles_x() <= 0 || tiling_data->num_tiles_y() <= 0) {
691 done();
692 return;
695 gfx::Rect tiling_bounds_rect(tiling_data->tiling_size());
696 gfx::Rect consider(consider_rect);
697 gfx::Rect ignore(ignore_rect);
698 gfx::Rect center(center_rect);
699 consider.Intersect(tiling_bounds_rect);
700 ignore.Intersect(tiling_bounds_rect);
701 if (consider.IsEmpty()) {
702 done();
703 return;
706 consider_left_ = tiling_data->TileXIndexFromSrcCoord(consider.x());
707 consider_top_ = tiling_data->TileYIndexFromSrcCoord(consider.y());
708 consider_right_ = tiling_data->TileXIndexFromSrcCoord(consider.right() - 1);
709 consider_bottom_ = tiling_data->TileYIndexFromSrcCoord(consider.bottom() - 1);
711 if (!ignore.IsEmpty()) {
712 ignore_left_ = tiling_data->TileXIndexFromSrcCoord(ignore.x());
713 ignore_top_ = tiling_data->TileYIndexFromSrcCoord(ignore.y());
714 ignore_right_ = tiling_data->TileXIndexFromSrcCoord(ignore.right() - 1);
715 ignore_bottom_ = tiling_data->TileYIndexFromSrcCoord(ignore.bottom() - 1);
717 // Clamp ignore indices to consider indices.
718 ignore_left_ = std::max(ignore_left_, consider_left_);
719 ignore_top_ = std::max(ignore_top_, consider_top_);
720 ignore_right_ = std::min(ignore_right_, consider_right_);
721 ignore_bottom_ = std::min(ignore_bottom_, consider_bottom_);
724 if (ignore_left_ == consider_left_ && ignore_right_ == consider_right_ &&
725 ignore_top_ == consider_top_ && ignore_bottom_ == consider_bottom_) {
726 done();
727 return;
730 // Determine around left, such that it is between -1 and num_tiles_x.
731 if (center.x() < 0 || center.IsEmpty())
732 around_left_ = -1;
733 else if (center.x() >= tiling_data->tiling_size().width())
734 around_left_ = tiling_data->num_tiles_x();
735 else
736 around_left_ = tiling_data->TileXIndexFromSrcCoord(center.x());
738 // Determine around top, such that it is between -1 and num_tiles_y.
739 if (center.y() < 0 || center.IsEmpty())
740 around_top_ = -1;
741 else if (center.y() >= tiling_data->tiling_size().height())
742 around_top_ = tiling_data->num_tiles_y();
743 else
744 around_top_ = tiling_data->TileYIndexFromSrcCoord(center.y());
746 // Determine around right, such that it is between -1 and num_tiles_x.
747 int right_src_coord = center.right() - 1;
748 if (right_src_coord < 0 || center.IsEmpty()) {
749 around_right_ = -1;
750 } else if (right_src_coord >= tiling_data->tiling_size().width()) {
751 around_right_ = tiling_data->num_tiles_x();
752 } else {
753 around_right_ = tiling_data->TileXIndexFromSrcCoord(right_src_coord);
756 // Determine around bottom, such that it is between -1 and num_tiles_y.
757 int bottom_src_coord = center.bottom() - 1;
758 if (bottom_src_coord < 0 || center.IsEmpty()) {
759 around_bottom_ = -1;
760 } else if (bottom_src_coord >= tiling_data->tiling_size().height()) {
761 around_bottom_ = tiling_data->num_tiles_y();
762 } else {
763 around_bottom_ = tiling_data->TileYIndexFromSrcCoord(bottom_src_coord);
766 // Figure out the maximum distance from the around edge to consider edge.
767 int max_distance = 0;
768 max_distance = std::max(max_distance, around_top_ - consider_top_);
769 max_distance = std::max(max_distance, around_left_ - consider_left_);
770 max_distance = std::max(max_distance, consider_bottom_ - around_bottom_);
771 max_distance = std::max(max_distance, consider_right_ - around_right_);
773 // The step count is the length of the edge (around_right_ - around_left_ + 1)
774 // plus twice the max distance to pad (to the right and to the left). This way
775 // the initial rect is the size proportional to the center, but big enough
776 // to cover the consider rect.
778 // C = consider rect
779 // A = around rect
780 // . = area of the padded around rect
781 // md = max distance (note in the picture below, there's md written vertically
782 // as well).
783 // I = initial starting position
785 // |md| |md|
787 // - ..........
788 // m ..........
789 // d ..........
790 // - CCCCCCC...
791 // CCCCAAC...
792 // CCCCAAC...
793 // - ..........
794 // m ..........
795 // d ..........
796 // - ..........I
797 vertical_step_count_ = around_bottom_ - around_top_ + 1 + 2 * max_distance;
798 horizontal_step_count_ = around_right_ - around_left_ + 1 + 2 * max_distance;
800 // Start with one to the right of the padded around rect.
801 index_x_ = around_right_ + max_distance + 1;
802 index_y_ = around_bottom_ + max_distance;
804 // The current index is outside a valid tile, so advance immediately.
805 ++(*this);
808 TilingData::ReverseSpiralDifferenceIterator&
809 TilingData::ReverseSpiralDifferenceIterator::
810 operator++() {
811 while (!in_around_rect()) {
812 if (needs_direction_switch())
813 switch_direction();
815 index_x_ += delta_x_;
816 index_y_ += delta_y_;
817 ++current_step_;
819 if (in_around_rect()) {
820 break;
821 } else if (in_consider_rect()) {
822 // If the tile is in the consider rect but not in ignore rect, then it's a
823 // valid tile to visit.
824 if (!in_ignore_rect())
825 break;
827 // Steps needed to reach the very edge of the ignore rect, while remaining
828 // inside it (so that the continue would take us outside).
829 int steps_to_edge = 0;
830 switch (direction_) {
831 case UP:
832 steps_to_edge = index_y_ - ignore_top_;
833 break;
834 case LEFT:
835 steps_to_edge = index_x_ - ignore_left_;
836 break;
837 case DOWN:
838 steps_to_edge = ignore_bottom_ - index_y_;
839 break;
840 case RIGHT:
841 steps_to_edge = ignore_right_ - index_x_;
842 break;
845 // We need to switch directions in |max_steps|.
846 int max_steps = current_step_count() - current_step_;
848 int steps_to_take = std::min(steps_to_edge, max_steps);
849 DCHECK_GE(steps_to_take, 0);
851 index_x_ += steps_to_take * delta_x_;
852 index_y_ += steps_to_take * delta_y_;
853 current_step_ += steps_to_take;
854 } else {
855 // We're not in the consider rect.
857 int max_steps = current_step_count() - current_step_;
858 int steps_to_take = max_steps;
860 // We might hit the consider rect before needing to switch directions:
861 // update steps to take.
862 switch (direction_) {
863 case UP:
864 if (valid_column() && consider_bottom_ < index_y_)
865 steps_to_take = index_y_ - consider_bottom_ - 1;
866 break;
867 case LEFT:
868 if (valid_row() && consider_right_ < index_x_)
869 steps_to_take = index_x_ - consider_right_ - 1;
870 break;
871 case DOWN:
872 if (valid_column() && consider_top_ > index_y_)
873 steps_to_take = consider_top_ - index_y_ - 1;
874 break;
875 case RIGHT:
876 if (valid_row() && consider_left_ > index_x_)
877 steps_to_take = consider_left_ - index_x_ - 1;
878 break;
880 steps_to_take = std::min(steps_to_take, max_steps);
881 DCHECK_GE(steps_to_take, 0);
883 index_x_ += steps_to_take * delta_x_;
884 index_y_ += steps_to_take * delta_y_;
885 current_step_ += steps_to_take;
889 // Once we enter the around rect, we're done.
890 if (in_around_rect())
891 done();
892 return *this;
895 bool TilingData::ReverseSpiralDifferenceIterator::needs_direction_switch()
896 const {
897 return current_step_ >= current_step_count();
900 void TilingData::ReverseSpiralDifferenceIterator::switch_direction() {
901 // Note that delta_x_ and delta_y_ always remain between -1 and 1.
902 int new_delta_y_ = delta_x_;
903 delta_x_ = -delta_y_;
904 delta_y_ = new_delta_y_;
906 current_step_ = 0;
907 direction_ = static_cast<Direction>((direction_ + 1) % 4);
909 if (direction_ == UP || direction_ == DOWN) {
910 --vertical_step_count_;
911 --horizontal_step_count_;
913 // We should always end up in an around rect at some point.
914 // Since the direction is now vertical, we have to ensure that we will
915 // advance.
916 DCHECK_GE(horizontal_step_count_, 1);
917 DCHECK_GE(vertical_step_count_, 1);
921 } // namespace cc