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 #ifndef CC_BASE_TILING_DATA_H_
6 #define CC_BASE_TILING_DATA_H_
10 #include "base/basictypes.h"
11 #include "base/logging.h"
12 #include "cc/base/cc_export.h"
13 #include "ui/gfx/geometry/rect.h"
14 #include "ui/gfx/geometry/size.h"
22 class CC_EXPORT 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
,
32 gfx::Size
tiling_size() const { return tiling_size_
; }
33 void SetTilingSize(const gfx::Size
& tiling_size
);
35 gfx::Size
max_texture_size() const { return max_texture_size_
; }
36 void SetMaxTextureSize(const gfx::Size
& max_texture_size
);
38 int border_texels() const { return border_texels_
; }
39 void SetHasBorderTexels(bool has_border_texels
);
40 void SetBorderTexels(int border_texels
);
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;
55 gfx::Rect
ExpandRectIgnoringBordersToTileBounds(const gfx::Rect
& rect
) const;
56 gfx::Rect
ExpandRectToTileBounds(const gfx::Rect
& rect
) const;
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;
65 // Difference between TileBound's and TileBoundWithBorder's origin().
66 gfx::Vector2d
TextureOffset(int x_index
, int y_index
) const;
68 class CC_EXPORT BaseIterator
{
70 operator bool() const { return index_x_
!= -1 && index_y_
!= -1; }
72 int index_x() const { return index_x_
; }
73 int index_y() const { return index_y_
; }
74 std::pair
<int, int> index() const {
75 return std::make_pair(index_x_
, index_y_
);
89 // Iterate through tiles whose bounds + optional border intersect with |rect|.
90 class CC_EXPORT Iterator
: public BaseIterator
{
93 Iterator(const TilingData
* tiling_data
,
94 const gfx::Rect
& consider_rect
,
95 bool include_borders
);
96 Iterator
& operator++();
104 class CC_EXPORT BaseDifferenceIterator
: public BaseIterator
{
106 BaseDifferenceIterator();
107 BaseDifferenceIterator(const TilingData
* tiling_data
,
108 const gfx::Rect
& consider_rect
,
109 const gfx::Rect
& ignore_rect
);
111 bool HasConsiderRect() const;
112 bool in_consider_rect() const {
113 return index_x_
>= consider_left_
&& index_x_
<= consider_right_
&&
114 index_y_
>= consider_top_
&& index_y_
<= consider_bottom_
;
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_
;
124 int consider_bottom_
;
131 // Iterate through all indices whose bounds (not including borders) intersect
132 // with |consider| but which also do not intersect with |ignore|.
133 class CC_EXPORT DifferenceIterator
: public BaseDifferenceIterator
{
135 DifferenceIterator();
136 DifferenceIterator(const TilingData
* tiling_data
,
137 const gfx::Rect
& consider_rect
,
138 const gfx::Rect
& ignore_rect
);
139 DifferenceIterator
& operator++();
142 // Iterate through all indices whose bounds + border intersect with
143 // |consider| but which also do not intersect with |ignore|. The iterator
144 // order is a counterclockwise spiral around the given center.
145 class CC_EXPORT SpiralDifferenceIterator
: public BaseDifferenceIterator
{
147 SpiralDifferenceIterator();
148 SpiralDifferenceIterator(const TilingData
* tiling_data
,
149 const gfx::Rect
& consider_rect
,
150 const gfx::Rect
& ignore_rect
,
151 const gfx::Rect
& center_rect
);
152 SpiralDifferenceIterator
& operator++();
155 bool valid_column() const {
156 return index_x_
>= consider_left_
&& index_x_
<= consider_right_
;
158 bool valid_row() const {
159 return index_y_
>= consider_top_
&& index_y_
<= consider_bottom_
;
162 int current_step_count() const {
163 return (direction_
== UP
|| direction_
== DOWN
) ? vertical_step_count_
164 : horizontal_step_count_
;
167 bool needs_direction_switch() const;
168 void switch_direction();
170 enum Direction
{ UP
, LEFT
, DOWN
, RIGHT
};
172 Direction direction_
;
176 int horizontal_step_count_
;
177 int vertical_step_count_
;
180 class CC_EXPORT ReverseSpiralDifferenceIterator
181 : public BaseDifferenceIterator
{
183 ReverseSpiralDifferenceIterator();
184 ReverseSpiralDifferenceIterator(const TilingData
* tiling_data
,
185 const gfx::Rect
& consider_rect
,
186 const gfx::Rect
& ignore_rect
,
187 const gfx::Rect
& center_rect
);
188 ReverseSpiralDifferenceIterator
& operator++();
191 bool in_around_rect() const {
192 return index_x_
>= around_left_
&& index_x_
<= around_right_
&&
193 index_y_
>= around_top_
&& index_y_
<= around_bottom_
;
195 bool valid_column() const {
196 return index_x_
>= consider_left_
&& index_x_
<= consider_right_
;
198 bool valid_row() const {
199 return index_y_
>= consider_top_
&& index_y_
<= consider_bottom_
;
202 int current_step_count() const {
203 return (direction_
== UP
|| direction_
== DOWN
) ? vertical_step_count_
204 : horizontal_step_count_
;
207 bool needs_direction_switch() const;
208 void switch_direction();
215 enum Direction
{ LEFT
, UP
, RIGHT
, DOWN
};
217 Direction direction_
;
221 int horizontal_step_count_
;
222 int vertical_step_count_
;
226 void AssertTile(int i
, int j
) const {
228 DCHECK_LT(i
, num_tiles_x_
);
230 DCHECK_LT(j
, num_tiles_y_
);
233 void RecomputeNumTiles();
235 gfx::Size max_texture_size_
;
236 gfx::Size tiling_size_
;
239 // These are computed values.
246 #endif // CC_BASE_TILING_DATA_H_