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/tiling_data.h"
9 #include "ui/gfx/rect.h"
10 #include "ui/gfx/vector2d.h"
14 static int ComputeNumTiles(int max_texture_size
, int total_size
, int border_texels
) {
15 if (max_texture_size
- 2 * border_texels
<= 0)
16 return total_size
> 0 && max_texture_size
>= total_size
? 1 : 0;
18 int num_tiles
= std::max(1, 1 + (total_size
- 1 - 2 * border_texels
) / (max_texture_size
- 2 * border_texels
));
19 return total_size
> 0 ? num_tiles
: 0;
22 TilingData::TilingData(gfx::Size max_texture_size
, gfx::Size total_size
, bool hasBorderTexels
)
23 : max_texture_size_(max_texture_size
),
24 total_size_(total_size
),
25 border_texels_(hasBorderTexels
? 1 : 0) {
29 TilingData::~TilingData() {
32 void TilingData::SetTotalSize(gfx::Size total_size
) {
33 total_size_
= total_size
;
37 void TilingData::SetMaxTextureSize(gfx::Size max_texture_size
) {
38 max_texture_size_
= max_texture_size
;
42 void TilingData::SetHasBorderTexels(bool has_border_texels
) {
43 border_texels_
= has_border_texels
? 1 : 0;
47 int TilingData::TileXIndexFromSrcCoord(int src_position
) const {
48 if (num_tiles_x_
<= 1)
51 DCHECK(max_texture_size_
.width() - 2 * border_texels_
);
52 int x
= (src_position
- border_texels_
) / (max_texture_size_
.width() - 2 * border_texels_
);
53 return std::min(std::max(x
, 0), num_tiles_x_
- 1);
56 int TilingData::TileYIndexFromSrcCoord(int src_position
) const {
57 if (num_tiles_y_
<= 1)
60 DCHECK(max_texture_size_
.height() - 2 * border_texels_
);
61 int y
= (src_position
- border_texels_
) / (max_texture_size_
.height() - 2 * border_texels_
);
62 return std::min(std::max(y
, 0), num_tiles_y_
- 1);
65 gfx::Rect
TilingData::TileBounds(int i
, int j
) const {
67 int x
= TilePositionX(i
);
68 int y
= TilePositionY(j
);
69 int width
= TileSizeX(i
);
70 int height
= TileSizeY(j
);
75 DCHECK_LE(x
, total_size_
.width());
76 DCHECK_LE(y
, total_size_
.height());
77 return gfx::Rect(x
, y
, width
, height
);
80 gfx::Rect
TilingData::TileBoundsWithBorder(int i
, int j
) const {
81 gfx::Rect bounds
= TileBounds(i
, j
);
85 int x2
= bounds
.right();
87 int y2
= bounds
.bottom();
91 if (i
< (num_tiles_x_
- 1))
95 if (j
< (num_tiles_y_
- 1))
98 bounds
= gfx::Rect(x1
, y1
, x2
- x1
, y2
- y1
);
104 int TilingData::TilePositionX(int x_index
) const {
105 DCHECK_GE(x_index
, 0);
106 DCHECK_LT(x_index
, num_tiles_x_
);
109 for (int i
= 0; i
< x_index
; i
++)
115 int TilingData::TilePositionY(int y_index
) const {
116 DCHECK_GE(y_index
, 0);
117 DCHECK_LT(y_index
, num_tiles_y_
);
120 for (int i
= 0; i
< y_index
; i
++)
126 int TilingData::TileSizeX(int x_index
) const {
127 DCHECK_GE(x_index
, 0);
128 DCHECK_LT(x_index
, num_tiles_x_
);
130 if (!x_index
&& num_tiles_x_
== 1)
131 return total_size_
.width();
132 if (!x_index
&& num_tiles_x_
> 1)
133 return max_texture_size_
.width() - border_texels_
;
134 if (x_index
< num_tiles_x_
- 1)
135 return max_texture_size_
.width() - 2 * border_texels_
;
136 if (x_index
== num_tiles_x_
- 1)
137 return total_size_
.width() - TilePositionX(x_index
);
143 int TilingData::TileSizeY(int y_index
) const {
144 DCHECK_GE(y_index
, 0);
145 DCHECK_LT(y_index
, num_tiles_y_
);
147 if (!y_index
&& num_tiles_y_
== 1)
148 return total_size_
.height();
149 if (!y_index
&& num_tiles_y_
> 1)
150 return max_texture_size_
.height() - border_texels_
;
151 if (y_index
< num_tiles_y_
- 1)
152 return max_texture_size_
.height() - 2 * border_texels_
;
153 if (y_index
== num_tiles_y_
- 1)
154 return total_size_
.height() - TilePositionY(y_index
);
160 gfx::Vector2d
TilingData::TextureOffset(int x_index
, int y_index
) const {
161 int left
= (!x_index
|| num_tiles_x_
== 1) ? 0 : border_texels_
;
162 int top
= (!y_index
|| num_tiles_y_
== 1) ? 0 : border_texels_
;
164 return gfx::Vector2d(left
, top
);
167 void TilingData::RecomputeNumTiles() {
168 num_tiles_x_
= ComputeNumTiles(max_texture_size_
.width(), total_size_
.width(), border_texels_
);
169 num_tiles_y_
= ComputeNumTiles(max_texture_size_
.height(), total_size_
.height(), border_texels_
);