7cd9457095fac4c840c76f38ae20b3df021f205d
[platform/framework/web/crosswalk.git] / src / cc / base / tiling_data.h
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 #ifndef CC_BASE_TILING_DATA_H_
6 #define CC_BASE_TILING_DATA_H_
7
8 #include <utility>
9
10 #include "base/basictypes.h"
11 #include "base/logging.h"
12 #include "cc/base/cc_export.h"
13 #include "ui/gfx/rect.h"
14 #include "ui/gfx/size.h"
15
16 namespace gfx {
17 class Vector2d;
18 }
19
20 namespace cc {
21
22 class CC_EXPORT TilingData {
23  public:
24   TilingData();
25   TilingData(const gfx::Size& max_texture_size,
26              const gfx::Size& tiling_size,
27              bool has_border_texels);
28   TilingData(const gfx::Size& max_texture_size,
29              const gfx::Size& tiling_size,
30              int border_texels);
31
32   gfx::Size tiling_size() const { return tiling_size_; }
33   void SetTilingSize(const gfx::Size& tiling_size);
34
35   gfx::Size max_texture_size() const { return max_texture_size_; }
36   void SetMaxTextureSize(const gfx::Size& max_texture_size);
37
38   int border_texels() const { return border_texels_; }
39   void SetHasBorderTexels(bool has_border_texels);
40   void SetBorderTexels(int border_texels);
41
42   bool has_empty_bounds() const { return !num_tiles_x_ || !num_tiles_y_; }
43   int num_tiles_x() const { return num_tiles_x_; }
44   int num_tiles_y() const { return num_tiles_y_; }
45   // Return the tile index whose non-border texels include src_position.
46   int TileXIndexFromSrcCoord(int src_position) const;
47   int TileYIndexFromSrcCoord(int src_position) const;
48   // Return the lowest tile index whose border texels include src_position.
49   int FirstBorderTileXIndexFromSrcCoord(int src_position) const;
50   int FirstBorderTileYIndexFromSrcCoord(int src_position) const;
51   // Return the highest tile index whose border texels include src_position.
52   int LastBorderTileXIndexFromSrcCoord(int src_position) const;
53   int LastBorderTileYIndexFromSrcCoord(int src_position) const;
54
55   gfx::Rect ExpandRectIgnoringBordersToTileBoundsWithBorders(
56       const gfx::Rect& rect) const;
57   gfx::Rect ExpandRectToTileBounds(const gfx::Rect& rect) const;
58
59   gfx::Rect TileBounds(int i, int j) const;
60   gfx::Rect TileBoundsWithBorder(int i, int j) const;
61   int TilePositionX(int x_index) const;
62   int TilePositionY(int y_index) const;
63   int TileSizeX(int x_index) const;
64   int TileSizeY(int y_index) const;
65
66   // Difference between TileBound's and TileBoundWithBorder's origin().
67   gfx::Vector2d TextureOffset(int x_index, int y_index) const;
68
69   class CC_EXPORT BaseIterator {
70    public:
71     operator bool() const { return index_x_ != -1 && index_y_ != -1; }
72
73     int index_x() const { return index_x_; }
74     int index_y() const { return index_y_; }
75     std::pair<int, int> index() const {
76      return std::make_pair(index_x_, index_y_);
77     }
78
79    protected:
80     explicit BaseIterator(const TilingData* tiling_data);
81     void done() {
82       index_x_ = -1;
83       index_y_ = -1;
84     }
85
86     const TilingData* tiling_data_;
87     int index_x_;
88     int index_y_;
89   };
90
91   // Iterate through tiles whose bounds + optional border intersect with |rect|.
92   class CC_EXPORT Iterator : public BaseIterator {
93    public:
94     Iterator();
95     Iterator(const TilingData* tiling_data,
96              const gfx::Rect& consider_rect,
97              bool include_borders);
98     Iterator& operator++();
99
100    private:
101     int left_;
102     int right_;
103     int bottom_;
104   };
105
106   // Iterate through all indices whose bounds + border intersect with
107   // |consider| but which also do not intersect with |ignore|.
108   class CC_EXPORT DifferenceIterator : public BaseIterator {
109    public:
110     DifferenceIterator(
111       const TilingData* tiling_data,
112       const gfx::Rect& consider_rect,
113       const gfx::Rect& ignore_rect);
114     DifferenceIterator& operator++();
115
116    private:
117     bool in_ignore_rect() const {
118      return index_x_ >= ignore_left_ && index_x_ <= ignore_right_ &&
119        index_y_ >= ignore_top_ && index_y_ <= ignore_bottom_;
120     }
121
122     int consider_left_;
123     int consider_top_;
124     int consider_right_;
125     int consider_bottom_;
126     int ignore_left_;
127     int ignore_top_;
128     int ignore_right_;
129     int ignore_bottom_;
130   };
131
132   // Iterate through all indices whose bounds + border intersect with
133   // |consider| but which also do not intersect with |ignore|. The iterator
134   // order is a counterclockwise spiral around the given center.
135   class CC_EXPORT SpiralDifferenceIterator : public BaseIterator {
136    public:
137     SpiralDifferenceIterator();
138     SpiralDifferenceIterator(const TilingData* tiling_data,
139                              const gfx::Rect& consider_rect,
140                              const gfx::Rect& ignore_rect,
141                              const gfx::Rect& center_rect);
142     SpiralDifferenceIterator& operator++();
143
144    private:
145     bool in_consider_rect() const {
146       return index_x_ >= consider_left_ && index_x_ <= consider_right_ &&
147              index_y_ >= consider_top_ && index_y_ <= consider_bottom_;
148     }
149     bool in_ignore_rect() const {
150       return index_x_ >= ignore_left_ && index_x_ <= ignore_right_ &&
151              index_y_ >= ignore_top_ && index_y_ <= ignore_bottom_;
152     }
153     bool valid_column() const {
154       return index_x_ >= consider_left_ && index_x_ <= consider_right_;
155     }
156     bool valid_row() const {
157       return index_y_ >= consider_top_ && index_y_ <= consider_bottom_;
158     }
159
160     int current_step_count() const {
161       return (direction_ == UP || direction_ == DOWN) ? vertical_step_count_
162                                                       : horizontal_step_count_;
163     }
164
165     bool needs_direction_switch() const;
166     void switch_direction();
167
168     int consider_left_;
169     int consider_top_;
170     int consider_right_;
171     int consider_bottom_;
172     int ignore_left_;
173     int ignore_top_;
174     int ignore_right_;
175     int ignore_bottom_;
176
177     enum Direction { UP, LEFT, DOWN, RIGHT };
178
179     Direction direction_;
180     int delta_x_;
181     int delta_y_;
182     int current_step_;
183     int horizontal_step_count_;
184     int vertical_step_count_;
185   };
186
187  private:
188   void AssertTile(int i, int j) const {
189     DCHECK_GE(i,  0);
190     DCHECK_LT(i, num_tiles_x_);
191     DCHECK_GE(j, 0);
192     DCHECK_LT(j, num_tiles_y_);
193   }
194
195   void RecomputeNumTiles();
196
197   gfx::Size max_texture_size_;
198   gfx::Size tiling_size_;
199   int border_texels_;
200
201   // These are computed values.
202   int num_tiles_x_;
203   int num_tiles_y_;
204 };
205
206 }  // namespace cc
207
208 #endif  // CC_BASE_TILING_DATA_H_