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/geometry/rect.h"
10 #include "ui/gfx/geometry/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(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) {
40 TilingData::TilingData(const gfx::Size
& max_texture_size
,
41 const gfx::Size
& tiling_size
,
43 : max_texture_size_(max_texture_size
),
44 tiling_size_(tiling_size
),
45 border_texels_(border_texels
) {
49 void TilingData::SetTilingSize(const gfx::Size
& tiling_size
) {
50 tiling_size_
= tiling_size
;
54 void TilingData::SetMaxTextureSize(const gfx::Size
& max_texture_size
) {
55 max_texture_size_
= max_texture_size
;
59 void TilingData::SetHasBorderTexels(bool has_border_texels
) {
60 border_texels_
= has_border_texels
? 1 : 0;
64 void TilingData::SetBorderTexels(int border_texels
) {
65 border_texels_
= border_texels
;
69 int TilingData::TileXIndexFromSrcCoord(int src_position
) const {
70 if (num_tiles_x_
<= 1)
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)
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)
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)
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)
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)
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())
133 if (rect
.x() > tiling_size_
.width() || rect
.y() > tiling_size_
.height())
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())
149 if (rect
.x() > tiling_size_
.width() || rect
.y() > tiling_size_
.height())
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 {
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
;
169 lo_x
+= border_texels_
;
171 int lo_y
= max_texture_size_y
* j
;
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());
188 int width
= hi_x
- lo_x
;
189 int height
= hi_y
- lo_y
;
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 {
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());
215 int width
= hi_x
- lo_x
;
216 int height
= hi_y
- lo_y
;
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
;
232 pos
+= border_texels_
;
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
;
243 pos
+= border_texels_
;
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
);
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
);
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() {
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) {
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_
);
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_
);
332 // Index functions always return valid indices, so explicitly check
333 // for non-intersecting rects.
334 if (!top_left_tile
.Intersects(rect
))
338 TilingData::Iterator
& TilingData::Iterator::operator++() {
343 if (index_x_
> right_
) {
346 if (index_y_
> bottom_
)
353 TilingData::BaseDifferenceIterator::BaseDifferenceIterator() {
357 TilingData::BaseDifferenceIterator::BaseDifferenceIterator(
358 const TilingData
* tiling_data
,
359 const gfx::Rect
& consider_rect
,
360 const gfx::Rect
& ignore_rect
)
361 : consider_left_(-1),
364 consider_bottom_(-1),
369 if (tiling_data
->num_tiles_x() <= 0 || tiling_data
->num_tiles_y() <= 0) {
374 gfx::Rect
tiling_bounds_rect(tiling_data
->tiling_size());
375 gfx::Rect
consider(consider_rect
);
376 consider
.Intersect(tiling_bounds_rect
);
378 if (consider
.IsEmpty()) {
383 consider_left_
= tiling_data
->TileXIndexFromSrcCoord(consider
.x());
384 consider_top_
= tiling_data
->TileYIndexFromSrcCoord(consider
.y());
385 consider_right_
= tiling_data
->TileXIndexFromSrcCoord(consider
.right() - 1);
386 consider_bottom_
= tiling_data
->TileYIndexFromSrcCoord(consider
.bottom() - 1);
388 gfx::Rect
ignore(ignore_rect
);
389 ignore
.Intersect(tiling_bounds_rect
);
391 if (!ignore
.IsEmpty()) {
392 ignore_left_
= tiling_data
->TileXIndexFromSrcCoord(ignore
.x());
393 ignore_top_
= tiling_data
->TileYIndexFromSrcCoord(ignore
.y());
394 ignore_right_
= tiling_data
->TileXIndexFromSrcCoord(ignore
.right() - 1);
395 ignore_bottom_
= tiling_data
->TileYIndexFromSrcCoord(ignore
.bottom() - 1);
397 // Clamp ignore indices to consider indices.
398 ignore_left_
= std::max(ignore_left_
, consider_left_
);
399 ignore_top_
= std::max(ignore_top_
, consider_top_
);
400 ignore_right_
= std::min(ignore_right_
, consider_right_
);
401 ignore_bottom_
= std::min(ignore_bottom_
, consider_bottom_
);
403 if (ignore_left_
== consider_left_
&& ignore_right_
== consider_right_
&&
404 ignore_top_
== consider_top_
&& ignore_bottom_
== consider_bottom_
) {
405 consider_left_
= consider_top_
= consider_right_
= consider_bottom_
= -1;
412 bool TilingData::BaseDifferenceIterator::HasConsiderRect() const {
413 // Consider indices are either all valid or all equal to -1.
414 DCHECK((0 <= consider_left_
&& consider_left_
<= consider_right_
&&
415 0 <= consider_top_
&& consider_top_
<= consider_bottom_
) ||
416 (consider_left_
== -1 && consider_top_
== -1 &&
417 consider_right_
== -1 && consider_bottom_
== -1));
418 return consider_left_
!= -1;
421 TilingData::DifferenceIterator::DifferenceIterator() {
424 TilingData::DifferenceIterator::DifferenceIterator(
425 const TilingData
* tiling_data
,
426 const gfx::Rect
& consider_rect
,
427 const gfx::Rect
& ignore_rect
)
428 : BaseDifferenceIterator(tiling_data
, consider_rect
, ignore_rect
) {
429 if (!HasConsiderRect()) {
434 index_x_
= consider_left_
;
435 index_y_
= consider_top_
;
437 if (in_ignore_rect())
441 TilingData::DifferenceIterator
& TilingData::DifferenceIterator::operator++() {
446 if (in_ignore_rect())
447 index_x_
= ignore_right_
+ 1;
449 if (index_x_
> consider_right_
) {
450 index_x_
= consider_left_
;
453 if (in_ignore_rect()) {
454 index_x_
= ignore_right_
+ 1;
455 // If the ignore rect spans the whole consider rect horizontally, then
456 // ignore_right + 1 will be out of bounds.
457 if (in_ignore_rect() || index_x_
> consider_right_
) {
458 index_y_
= ignore_bottom_
+ 1;
459 index_x_
= consider_left_
;
463 if (index_y_
> consider_bottom_
)
470 TilingData::SpiralDifferenceIterator::SpiralDifferenceIterator() {
474 TilingData::SpiralDifferenceIterator::SpiralDifferenceIterator(
475 const TilingData
* tiling_data
,
476 const gfx::Rect
& consider_rect
,
477 const gfx::Rect
& ignore_rect
,
478 const gfx::Rect
& center_rect
)
479 : BaseDifferenceIterator(tiling_data
, consider_rect
, ignore_rect
),
484 horizontal_step_count_(0),
485 vertical_step_count_(0) {
486 if (!HasConsiderRect()) {
491 // Determine around left, such that it is between -1 and num_tiles_x.
493 if (center_rect
.x() < 0 || center_rect
.IsEmpty())
495 else if (center_rect
.x() >= tiling_data
->tiling_size().width())
496 around_left
= tiling_data
->num_tiles_x();
498 around_left
= tiling_data
->TileXIndexFromSrcCoord(center_rect
.x());
500 // Determine around top, such that it is between -1 and num_tiles_y.
502 if (center_rect
.y() < 0 || center_rect
.IsEmpty())
504 else if (center_rect
.y() >= tiling_data
->tiling_size().height())
505 around_top
= tiling_data
->num_tiles_y();
507 around_top
= tiling_data
->TileYIndexFromSrcCoord(center_rect
.y());
509 // Determine around right, such that it is between -1 and num_tiles_x.
510 int right_src_coord
= center_rect
.right() - 1;
511 int around_right
= 0;
512 if (right_src_coord
< 0 || center_rect
.IsEmpty()) {
514 } else if (right_src_coord
>= tiling_data
->tiling_size().width()) {
515 around_right
= tiling_data
->num_tiles_x();
517 around_right
= tiling_data
->TileXIndexFromSrcCoord(right_src_coord
);
520 // Determine around bottom, such that it is between -1 and num_tiles_y.
521 int bottom_src_coord
= center_rect
.bottom() - 1;
522 int around_bottom
= 0;
523 if (bottom_src_coord
< 0 || center_rect
.IsEmpty()) {
525 } else if (bottom_src_coord
>= tiling_data
->tiling_size().height()) {
526 around_bottom
= tiling_data
->num_tiles_y();
528 around_bottom
= tiling_data
->TileYIndexFromSrcCoord(bottom_src_coord
);
531 vertical_step_count_
= around_bottom
- around_top
+ 1;
532 horizontal_step_count_
= around_right
- around_left
+ 1;
533 current_step_
= horizontal_step_count_
- 1;
535 index_x_
= around_right
;
536 index_y_
= around_bottom
;
538 // The current index is the bottom right of the around rect, which is also
539 // ignored. So we have to advance.
543 TilingData::SpiralDifferenceIterator
& TilingData::SpiralDifferenceIterator::
545 int cannot_hit_consider_count
= 0;
546 while (cannot_hit_consider_count
< 4) {
547 if (needs_direction_switch())
550 index_x_
+= delta_x_
;
551 index_y_
+= delta_y_
;
554 if (in_consider_rect()) {
555 cannot_hit_consider_count
= 0;
557 if (!in_ignore_rect())
560 // Steps needed to reach the very edge of the ignore rect, while remaining
561 // inside (so that the continue would take us outside).
562 int steps_to_edge
= 0;
563 switch (direction_
) {
565 steps_to_edge
= index_y_
- ignore_top_
;
568 steps_to_edge
= index_x_
- ignore_left_
;
571 steps_to_edge
= ignore_bottom_
- index_y_
;
574 steps_to_edge
= ignore_right_
- index_x_
;
578 // We need to switch directions in |max_steps|.
579 int max_steps
= current_step_count() - current_step_
;
581 int steps_to_take
= std::min(steps_to_edge
, max_steps
);
582 DCHECK_GE(steps_to_take
, 0);
584 index_x_
+= steps_to_take
* delta_x_
;
585 index_y_
+= steps_to_take
* delta_y_
;
586 current_step_
+= steps_to_take
;
588 int max_steps
= current_step_count() - current_step_
;
589 int steps_to_take
= max_steps
;
590 bool can_hit_consider_rect
= false;
591 switch (direction_
) {
593 if (valid_column() && consider_bottom_
< index_y_
)
594 steps_to_take
= index_y_
- consider_bottom_
- 1;
595 can_hit_consider_rect
|= consider_right_
>= index_x_
;
598 if (valid_row() && consider_right_
< index_x_
)
599 steps_to_take
= index_x_
- consider_right_
- 1;
600 can_hit_consider_rect
|= consider_top_
<= index_y_
;
603 if (valid_column() && consider_top_
> index_y_
)
604 steps_to_take
= consider_top_
- index_y_
- 1;
605 can_hit_consider_rect
|= consider_left_
<= index_x_
;
608 if (valid_row() && consider_left_
> index_x_
)
609 steps_to_take
= consider_left_
- index_x_
- 1;
610 can_hit_consider_rect
|= consider_bottom_
>= index_y_
;
613 steps_to_take
= std::min(steps_to_take
, max_steps
);
614 DCHECK_GE(steps_to_take
, 0);
616 index_x_
+= steps_to_take
* delta_x_
;
617 index_y_
+= steps_to_take
* delta_y_
;
618 current_step_
+= steps_to_take
;
620 if (can_hit_consider_rect
)
621 cannot_hit_consider_count
= 0;
623 ++cannot_hit_consider_count
;
627 if (cannot_hit_consider_count
>= 4)
632 bool TilingData::SpiralDifferenceIterator::needs_direction_switch() const {
633 return current_step_
>= current_step_count();
636 void TilingData::SpiralDifferenceIterator::switch_direction() {
637 // Note that delta_x_ and delta_y_ always remain between -1 and 1.
638 int new_delta_x_
= delta_y_
;
639 delta_y_
= -delta_x_
;
640 delta_x_
= new_delta_x_
;
643 direction_
= static_cast<Direction
>((direction_
+ 1) % 4);
645 if (direction_
== RIGHT
|| direction_
== LEFT
) {
646 ++vertical_step_count_
;
647 ++horizontal_step_count_
;
651 TilingData::ReverseSpiralDifferenceIterator::ReverseSpiralDifferenceIterator() {
655 TilingData::ReverseSpiralDifferenceIterator::ReverseSpiralDifferenceIterator(
656 const TilingData
* tiling_data
,
657 const gfx::Rect
& consider_rect
,
658 const gfx::Rect
& ignore_rect
,
659 const gfx::Rect
& center_rect
)
660 : BaseDifferenceIterator(tiling_data
, consider_rect
, ignore_rect
),
669 horizontal_step_count_(0),
670 vertical_step_count_(0) {
671 if (!HasConsiderRect()) {
676 // Determine around left, such that it is between -1 and num_tiles_x.
677 if (center_rect
.x() < 0 || center_rect
.IsEmpty())
679 else if (center_rect
.x() >= tiling_data
->tiling_size().width())
680 around_left_
= tiling_data
->num_tiles_x();
682 around_left_
= tiling_data
->TileXIndexFromSrcCoord(center_rect
.x());
684 // Determine around top, such that it is between -1 and num_tiles_y.
685 if (center_rect
.y() < 0 || center_rect
.IsEmpty())
687 else if (center_rect
.y() >= tiling_data
->tiling_size().height())
688 around_top_
= tiling_data
->num_tiles_y();
690 around_top_
= tiling_data
->TileYIndexFromSrcCoord(center_rect
.y());
692 // Determine around right, such that it is between -1 and num_tiles_x.
693 int right_src_coord
= center_rect
.right() - 1;
694 if (right_src_coord
< 0 || center_rect
.IsEmpty()) {
696 } else if (right_src_coord
>= tiling_data
->tiling_size().width()) {
697 around_right_
= tiling_data
->num_tiles_x();
699 around_right_
= tiling_data
->TileXIndexFromSrcCoord(right_src_coord
);
702 // Determine around bottom, such that it is between -1 and num_tiles_y.
703 int bottom_src_coord
= center_rect
.bottom() - 1;
704 if (bottom_src_coord
< 0 || center_rect
.IsEmpty()) {
706 } else if (bottom_src_coord
>= tiling_data
->tiling_size().height()) {
707 around_bottom_
= tiling_data
->num_tiles_y();
709 around_bottom_
= tiling_data
->TileYIndexFromSrcCoord(bottom_src_coord
);
712 // Figure out the maximum distance from the around edge to consider edge.
713 int max_distance
= 0;
714 max_distance
= std::max(max_distance
, around_top_
- consider_top_
);
715 max_distance
= std::max(max_distance
, around_left_
- consider_left_
);
716 max_distance
= std::max(max_distance
, consider_bottom_
- around_bottom_
);
717 max_distance
= std::max(max_distance
, consider_right_
- around_right_
);
719 // The step count is the length of the edge (around_right_ - around_left_ + 1)
720 // plus twice the max distance to pad (to the right and to the left). This way
721 // the initial rect is the size proportional to the center, but big enough
722 // to cover the consider rect.
726 // . = area of the padded around rect
727 // md = max distance (note in the picture below, there's md written vertically
729 // I = initial starting position
743 vertical_step_count_
= around_bottom_
- around_top_
+ 1 + 2 * max_distance
;
744 horizontal_step_count_
= around_right_
- around_left_
+ 1 + 2 * max_distance
;
746 // Start with one to the right of the padded around rect.
747 index_x_
= around_right_
+ max_distance
+ 1;
748 index_y_
= around_bottom_
+ max_distance
;
750 // The current index is outside a valid tile, so advance immediately.
754 TilingData::ReverseSpiralDifferenceIterator
&
755 TilingData::ReverseSpiralDifferenceIterator::
757 while (!in_around_rect()) {
758 if (needs_direction_switch())
761 index_x_
+= delta_x_
;
762 index_y_
+= delta_y_
;
765 if (in_around_rect()) {
767 } else if (in_consider_rect()) {
768 // If the tile is in the consider rect but not in ignore rect, then it's a
769 // valid tile to visit.
770 if (!in_ignore_rect())
773 // Steps needed to reach the very edge of the ignore rect, while remaining
774 // inside it (so that the continue would take us outside).
775 int steps_to_edge
= 0;
776 switch (direction_
) {
778 steps_to_edge
= index_y_
- ignore_top_
;
781 steps_to_edge
= index_x_
- ignore_left_
;
784 steps_to_edge
= ignore_bottom_
- index_y_
;
787 steps_to_edge
= ignore_right_
- index_x_
;
791 // We need to switch directions in |max_steps|.
792 int max_steps
= current_step_count() - current_step_
;
794 int steps_to_take
= std::min(steps_to_edge
, max_steps
);
795 DCHECK_GE(steps_to_take
, 0);
797 index_x_
+= steps_to_take
* delta_x_
;
798 index_y_
+= steps_to_take
* delta_y_
;
799 current_step_
+= steps_to_take
;
801 // We're not in the consider rect.
803 int max_steps
= current_step_count() - current_step_
;
804 int steps_to_take
= max_steps
;
806 // We might hit the consider rect before needing to switch directions:
807 // update steps to take.
808 switch (direction_
) {
810 if (valid_column() && consider_bottom_
< index_y_
)
811 steps_to_take
= index_y_
- consider_bottom_
- 1;
814 if (valid_row() && consider_right_
< index_x_
)
815 steps_to_take
= index_x_
- consider_right_
- 1;
818 if (valid_column() && consider_top_
> index_y_
)
819 steps_to_take
= consider_top_
- index_y_
- 1;
822 if (valid_row() && consider_left_
> index_x_
)
823 steps_to_take
= consider_left_
- index_x_
- 1;
826 steps_to_take
= std::min(steps_to_take
, max_steps
);
827 DCHECK_GE(steps_to_take
, 0);
829 index_x_
+= steps_to_take
* delta_x_
;
830 index_y_
+= steps_to_take
* delta_y_
;
831 current_step_
+= steps_to_take
;
835 // Once we enter the around rect, we're done.
836 if (in_around_rect())
841 bool TilingData::ReverseSpiralDifferenceIterator::needs_direction_switch()
843 return current_step_
>= current_step_count();
846 void TilingData::ReverseSpiralDifferenceIterator::switch_direction() {
847 // Note that delta_x_ and delta_y_ always remain between -1 and 1.
848 int new_delta_y_
= delta_x_
;
849 delta_x_
= -delta_y_
;
850 delta_y_
= new_delta_y_
;
853 direction_
= static_cast<Direction
>((direction_
+ 1) % 4);
855 if (direction_
== UP
|| direction_
== DOWN
) {
856 --vertical_step_count_
;
857 --horizontal_step_count_
;
859 // We should always end up in an around rect at some point.
860 // Since the direction is now vertical, we have to ensure that we will
862 DCHECK_GE(horizontal_step_count_
, 1);
863 DCHECK_GE(vertical_step_count_
, 1);