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