Upstream version 7.35.139.0
[platform/framework/web/crosswalk.git] / src / cc / base / tiling_data.cc
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.
4
5 #include "cc/base/tiling_data.h"
6
7 #include <algorithm>
8
9 #include "ui/gfx/rect.h"
10 #include "ui/gfx/vector2d.h"
11
12 namespace cc {
13
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;
19
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;
24 }
25
26 TilingData::TilingData()
27     : border_texels_(0) {
28   RecomputeNumTiles();
29 }
30
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) {
38   RecomputeNumTiles();
39 }
40
41 TilingData::TilingData(
42     const gfx::Size& max_texture_size,
43     const gfx::Size& total_size,
44     int border_texels)
45     : max_texture_size_(max_texture_size),
46       total_size_(total_size),
47       border_texels_(border_texels) {
48   RecomputeNumTiles();
49 }
50
51 void TilingData::SetTotalSize(const gfx::Size& total_size) {
52   total_size_ = total_size;
53   RecomputeNumTiles();
54 }
55
56 void TilingData::SetMaxTextureSize(const gfx::Size& max_texture_size) {
57   max_texture_size_ = max_texture_size;
58   RecomputeNumTiles();
59 }
60
61 void TilingData::SetHasBorderTexels(bool has_border_texels) {
62   border_texels_ = has_border_texels ? 1 : 0;
63   RecomputeNumTiles();
64 }
65
66 void TilingData::SetBorderTexels(int border_texels) {
67   border_texels_ = border_texels;
68   RecomputeNumTiles();
69 }
70
71 int TilingData::TileXIndexFromSrcCoord(int src_position) const {
72   if (num_tiles_x_ <= 1)
73     return 0;
74
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);
79 }
80
81 int TilingData::TileYIndexFromSrcCoord(int src_position) const {
82   if (num_tiles_y_ <= 1)
83     return 0;
84
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);
89 }
90
91 int TilingData::FirstBorderTileXIndexFromSrcCoord(int src_position) const {
92   if (num_tiles_x_ <= 1)
93     return 0;
94
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);
99 }
100
101 int TilingData::FirstBorderTileYIndexFromSrcCoord(int src_position) const {
102   if (num_tiles_y_ <= 1)
103     return 0;
104
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);
109 }
110
111 int TilingData::LastBorderTileXIndexFromSrcCoord(int src_position) const {
112   if (num_tiles_x_ <= 1)
113     return 0;
114
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);
119 }
120
121 int TilingData::LastBorderTileYIndexFromSrcCoord(int src_position) const {
122   if (num_tiles_y_ <= 1)
123     return 0;
124
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);
129 }
130
131 gfx::Rect TilingData::ExpandRectToTileBoundsWithBorders(
132     const gfx::Rect rect) const {
133   if (!rect.Intersects(gfx::Rect(total_size_)) || has_empty_bounds())
134     return gfx::Rect();
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());
139
140   gfx::Rect rect_top_left(TileBoundsWithBorder(index_x, index_y));
141   gfx::Rect rect_bottom_right(TileBoundsWithBorder(index_right, index_bottom));
142
143   gfx::Rect expanded(rect_top_left);
144   expanded.Union(rect_bottom_right);
145   return expanded;
146 }
147
148 gfx::Rect TilingData::TileBounds(int i, int j) const {
149   AssertTile(i, j);
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();
154
155   int lo_x = max_texture_size_x * i;
156   if (i != 0)
157     lo_x += border_texels_;
158
159   int lo_y = max_texture_size_y * j;
160   if (j != 0)
161     lo_y += border_texels_;
162
163   int hi_x = max_texture_size_x * (i + 1) + border_texels_;
164   if (i + 1 == num_tiles_x_)
165     hi_x += border_texels_;
166
167   int hi_y = max_texture_size_y * (j + 1) + border_texels_;
168   if (j + 1 == num_tiles_y_)
169     hi_y += border_texels_;
170
171   hi_x = std::min(hi_x, total_size_x);
172   hi_y = std::min(hi_y, total_size_y);
173
174   int x = lo_x;
175   int y = lo_y;
176   int width = hi_x - lo_x;
177   int height = hi_y - lo_y;
178   DCHECK_GE(x, 0);
179   DCHECK_GE(y, 0);
180   DCHECK_GE(width, 0);
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);
185 }
186
187 gfx::Rect TilingData::TileBoundsWithBorder(int i, int j) const {
188   AssertTile(i, j);
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();
193
194   int lo_x = max_texture_size_x * i;
195   int lo_y = max_texture_size_y * j;
196
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_;
199
200   hi_x = std::min(hi_x, total_size_x);
201   hi_y = std::min(hi_y, total_size_y);
202
203   int x = lo_x;
204   int y = lo_y;
205   int width = hi_x - lo_x;
206   int height = hi_y - lo_y;
207   DCHECK_GE(x, 0);
208   DCHECK_GE(y, 0);
209   DCHECK_GE(width, 0);
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);
214 }
215
216 int TilingData::TilePositionX(int x_index) const {
217   DCHECK_GE(x_index, 0);
218   DCHECK_LT(x_index, num_tiles_x_);
219
220   int pos = (max_texture_size_.width() - 2 * border_texels_) * x_index;
221   if (x_index != 0)
222     pos += border_texels_;
223
224   return pos;
225 }
226
227 int TilingData::TilePositionY(int y_index) const {
228   DCHECK_GE(y_index, 0);
229   DCHECK_LT(y_index, num_tiles_y_);
230
231   int pos = (max_texture_size_.height() - 2 * border_texels_) * y_index;
232   if (y_index != 0)
233     pos += border_texels_;
234
235   return pos;
236 }
237
238 int TilingData::TileSizeX(int x_index) const {
239   DCHECK_GE(x_index, 0);
240   DCHECK_LT(x_index, num_tiles_x_);
241
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);
250
251   NOTREACHED();
252   return 0;
253 }
254
255 int TilingData::TileSizeY(int y_index) const {
256   DCHECK_GE(y_index, 0);
257   DCHECK_LT(y_index, num_tiles_y_);
258
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);
267
268   NOTREACHED();
269   return 0;
270 }
271
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_;
275
276   return gfx::Vector2d(left, top);
277 }
278
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_);
284 }
285
286 TilingData::BaseIterator::BaseIterator(const TilingData* tiling_data)
287     : tiling_data_(tiling_data),
288       index_x_(-1),
289       index_y_(-1) {
290 }
291
292 TilingData::Iterator::Iterator() : BaseIterator(NULL) { done(); }
293
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) {
299     done();
300     return;
301   }
302
303   gfx::Rect rect(tiling_rect);
304   rect.Intersect(gfx::Rect(tiling_data_->total_size()));
305
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_);
313   } else {
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_);
319   }
320   left_ = index_x_;
321
322   // Index functions always return valid indices, so explicitly check
323   // for non-intersecting rects.
324   if (!top_left_tile.Intersects(rect))
325     done();
326 }
327
328 TilingData::Iterator& TilingData::Iterator::operator++() {
329   if (!*this)
330     return *this;
331
332   index_x_++;
333   if (index_x_ > right_) {
334     index_x_ = left_;
335     index_y_++;
336     if (index_y_ > bottom_)
337       done();
338   }
339
340   return *this;
341 }
342
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),
348       consider_left_(-1),
349       consider_top_(-1),
350       consider_right_(-1),
351       consider_bottom_(-1),
352       ignore_left_(-1),
353       ignore_top_(-1),
354       ignore_right_(-1),
355       ignore_bottom_(-1) {
356   if (tiling_data_->num_tiles_x() <= 0 || tiling_data_->num_tiles_y() <= 0) {
357     done();
358     return;
359   }
360
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()) {
367     done();
368     return;
369   }
370
371   consider_left_ =
372       tiling_data_->FirstBorderTileXIndexFromSrcCoord(consider.x());
373   consider_top_ =
374       tiling_data_->FirstBorderTileYIndexFromSrcCoord(consider.y());
375   consider_right_ =
376       tiling_data_->LastBorderTileXIndexFromSrcCoord(consider.right() - 1);
377   consider_bottom_ =
378       tiling_data_->LastBorderTileYIndexFromSrcCoord(consider.bottom() - 1);
379
380   if (!ignore.IsEmpty()) {
381     ignore_left_ =
382         tiling_data_->FirstBorderTileXIndexFromSrcCoord(ignore.x());
383     ignore_top_ =
384         tiling_data_->FirstBorderTileYIndexFromSrcCoord(ignore.y());
385     ignore_right_ =
386         tiling_data_->LastBorderTileXIndexFromSrcCoord(ignore.right() - 1);
387     ignore_bottom_ =
388         tiling_data_->LastBorderTileYIndexFromSrcCoord(ignore.bottom() - 1);
389
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_);
395   }
396
397   if (ignore_left_ == consider_left_ && ignore_right_ == consider_right_ &&
398       ignore_top_ == consider_top_ && ignore_bottom_ == consider_bottom_) {
399     done();
400     return;
401   }
402
403   index_x_ = consider_left_;
404   index_y_ = consider_top_;
405
406   if (in_ignore_rect())
407     ++(*this);
408 }
409
410 TilingData::DifferenceIterator& TilingData::DifferenceIterator::operator++() {
411   if (!*this)
412     return *this;
413
414   index_x_++;
415   if (in_ignore_rect())
416     index_x_ = ignore_right_ + 1;
417
418   if (index_x_ > consider_right_) {
419     index_x_ = consider_left_;
420     index_y_++;
421
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_;
429       }
430     }
431
432     if (index_y_ > consider_bottom_)
433       done();
434   }
435
436   return *this;
437 }
438
439 TilingData::SpiralDifferenceIterator::SpiralDifferenceIterator()
440     : BaseIterator(NULL) {
441   done();
442 }
443
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),
450       consider_left_(-1),
451       consider_top_(-1),
452       consider_right_(-1),
453       consider_bottom_(-1),
454       ignore_left_(-1),
455       ignore_top_(-1),
456       ignore_right_(-1),
457       ignore_bottom_(-1),
458       direction_(RIGHT),
459       delta_x_(1),
460       delta_y_(0),
461       current_step_(0),
462       horizontal_step_count_(0),
463       vertical_step_count_(0) {
464   if (tiling_data_->num_tiles_x() <= 0 || tiling_data_->num_tiles_y() <= 0) {
465     done();
466     return;
467   }
468
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()) {
476     done();
477     return;
478   }
479
480   consider_left_ =
481       tiling_data_->FirstBorderTileXIndexFromSrcCoord(consider.x());
482   consider_top_ = tiling_data_->FirstBorderTileYIndexFromSrcCoord(consider.y());
483   consider_right_ =
484       tiling_data_->LastBorderTileXIndexFromSrcCoord(consider.right() - 1);
485   consider_bottom_ =
486       tiling_data_->LastBorderTileYIndexFromSrcCoord(consider.bottom() - 1);
487
488   if (!ignore.IsEmpty()) {
489     ignore_left_ = tiling_data_->FirstBorderTileXIndexFromSrcCoord(ignore.x());
490     ignore_top_ = tiling_data_->FirstBorderTileYIndexFromSrcCoord(ignore.y());
491     ignore_right_ =
492         tiling_data_->LastBorderTileXIndexFromSrcCoord(ignore.right() - 1);
493     ignore_bottom_ =
494         tiling_data_->LastBorderTileYIndexFromSrcCoord(ignore.bottom() - 1);
495
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_);
501   }
502
503   if (ignore_left_ == consider_left_ && ignore_right_ == consider_right_ &&
504       ignore_top_ == consider_top_ && ignore_bottom_ == consider_bottom_) {
505     done();
506     return;
507   }
508
509   // Determine around left, such that it is between -1 and num_tiles_x.
510   int around_left = 0;
511   if (center.x() < 0 || center.IsEmpty())
512     around_left = -1;
513   else if (center.x() > tiling_data->total_size().width())
514     around_left = tiling_data->num_tiles_x();
515   else
516     around_left = tiling_data->FirstBorderTileXIndexFromSrcCoord(center.x());
517
518   // Determine around top, such that it is between -1 and num_tiles_y.
519   int around_top = 0;
520   if (center.y() < 0 || center.IsEmpty())
521     around_top = -1;
522   else if (center.y() > tiling_data->total_size().height())
523     around_top = tiling_data->num_tiles_y();
524   else
525     around_top = tiling_data->FirstBorderTileYIndexFromSrcCoord(center.y());
526
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()) {
531     around_right = -1;
532   } else if (right_src_coord > tiling_data->total_size().width()) {
533     around_right = tiling_data->num_tiles_x();
534   } else {
535     around_right =
536         tiling_data->LastBorderTileXIndexFromSrcCoord(right_src_coord);
537   }
538
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()) {
543     around_bottom = -1;
544   } else if (bottom_src_coord > tiling_data->total_size().height()) {
545     around_bottom = tiling_data->num_tiles_y();
546   } else {
547     around_bottom =
548         tiling_data->LastBorderTileYIndexFromSrcCoord(bottom_src_coord);
549   }
550
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;
554
555   index_x_ = around_right;
556   index_y_ = around_bottom;
557
558   // The current index is the bottom right of the around rect, which is also
559   // ignored. So we have to advance.
560   ++(*this);
561 }
562
563 TilingData::SpiralDifferenceIterator& TilingData::SpiralDifferenceIterator::
564 operator++() {
565   int cannot_hit_consider_count = 0;
566   while (cannot_hit_consider_count < 4) {
567     if (needs_direction_switch())
568       switch_direction();
569
570     index_x_ += delta_x_;
571     index_y_ += delta_y_;
572     ++current_step_;
573
574     if (in_consider_rect()) {
575       cannot_hit_consider_count = 0;
576
577       if (!in_ignore_rect())
578         break;
579
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_) {
584         case UP:
585           steps_to_edge = index_y_ - ignore_top_;
586           break;
587         case LEFT:
588           steps_to_edge = index_x_ - ignore_left_;
589           break;
590         case DOWN:
591           steps_to_edge = ignore_bottom_ - index_y_;
592           break;
593         case RIGHT:
594           steps_to_edge = ignore_right_ - index_x_;
595           break;
596       }
597
598       // We need to switch directions in |max_steps|.
599       int max_steps = current_step_count() - current_step_;
600
601       int steps_to_take = std::min(steps_to_edge, max_steps);
602       DCHECK_GE(steps_to_take, 0);
603
604       index_x_ += steps_to_take * delta_x_;
605       index_y_ += steps_to_take * delta_y_;
606       current_step_ += steps_to_take;
607     } else {
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_) {
612         case UP:
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_;
616           break;
617         case LEFT:
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_;
621           break;
622         case DOWN:
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_;
626           break;
627         case RIGHT:
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_;
631           break;
632       }
633       steps_to_take = std::min(steps_to_take, max_steps);
634       DCHECK_GE(steps_to_take, 0);
635
636       index_x_ += steps_to_take * delta_x_;
637       index_y_ += steps_to_take * delta_y_;
638       current_step_ += steps_to_take;
639
640       if (can_hit_consider_rect)
641         cannot_hit_consider_count = 0;
642       else
643         ++cannot_hit_consider_count;
644     }
645   }
646
647   if (cannot_hit_consider_count >= 4)
648     done();
649   return *this;
650 }
651
652 bool TilingData::SpiralDifferenceIterator::needs_direction_switch() const {
653   return current_step_ >= current_step_count();
654 }
655
656 void TilingData::SpiralDifferenceIterator::switch_direction() {
657   int new_delta_x_ = delta_y_;
658   delta_y_ = -delta_x_;
659   delta_x_ = new_delta_x_;
660
661   current_step_ = 0;
662   direction_ = static_cast<Direction>((direction_ + 1) % 4);
663
664   if (direction_ == RIGHT || direction_ == LEFT) {
665     ++vertical_step_count_;
666     ++horizontal_step_count_;
667   }
668 }
669
670 }  // namespace cc