OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2010 The Chromium Authors. All rights reserved. | |
tfarina
2012/11/02 00:25:45
2010 -> 2012?
danakj
2012/11/02 00:28:03
Keeping the year from the old file, as this is a c
| |
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 "config.h" | |
tfarina
2012/11/02 00:25:45
what is this for?
danakj
2012/11/02 00:28:03
same as all the other .cc in cc/ until we are clea
| |
6 | |
7 #include "cc/tiling_data.h" | |
8 | |
9 #include <algorithm> | |
10 | |
11 #include "ui/gfx/rect.h" | |
12 #include "ui/gfx/vector2d.h" | |
13 | |
14 namespace cc { | |
15 | |
16 static int ComputeNumTiles(int max_texture_size, int total_size, int border_texe ls) { | |
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, 1 + (total_size - 1 - 2 * border_texels) / (max_te xture_size - 2 * border_texels)); | |
21 return total_size > 0 ? num_tiles : 0; | |
22 } | |
23 | |
24 TilingData::TilingData(gfx::Size max_texture_size, gfx::Size total_size, bool ha sBorderTexels) | |
25 : max_texture_size_(max_texture_size), | |
26 total_size_(total_size), | |
27 border_texels_(hasBorderTexels ? 1 : 0) { | |
28 RecomputeNumTiles(); | |
29 } | |
30 | |
31 TilingData::~TilingData() { | |
32 } | |
33 | |
34 void TilingData::SetTotalSize(gfx::Size total_size) { | |
35 total_size_ = total_size; | |
36 RecomputeNumTiles(); | |
37 } | |
38 | |
39 void TilingData::SetMaxTextureSize(gfx::Size max_texture_size) { | |
40 max_texture_size_ = max_texture_size; | |
41 RecomputeNumTiles(); | |
42 } | |
43 | |
44 void TilingData::SetHasBorderTexels(bool has_border_texels) { | |
45 border_texels_ = has_border_texels ? 1 : 0; | |
46 RecomputeNumTiles(); | |
47 } | |
48 | |
49 int TilingData::TileXIndexFromSrcCoord(int src_position) const { | |
50 if (num_tiles_x_ <= 1) | |
51 return 0; | |
52 | |
53 DCHECK(max_texture_size_.width() - 2 * border_texels_); | |
54 int x = (src_position - border_texels_) / (max_texture_size_.width() - 2 * bor der_texels_); | |
55 return std::min(std::max(x, 0), num_tiles_x_ - 1); | |
56 } | |
57 | |
58 int TilingData::TileYIndexFromSrcCoord(int src_position) const { | |
59 if (num_tiles_y_ <= 1) | |
60 return 0; | |
61 | |
62 DCHECK(max_texture_size_.height() - 2 * border_texels_); | |
63 int y = (src_position - border_texels_) / (max_texture_size_.height() - 2 * bo rder_texels_); | |
64 return std::min(std::max(y, 0), num_tiles_y_ - 1); | |
65 } | |
66 | |
67 gfx::Rect TilingData::TileBounds(int i, int j) const { | |
68 AssertTile(i, j); | |
69 int x = TilePositionX(i); | |
70 int y = TilePositionY(j); | |
71 int width = TileSizeX(i); | |
72 int height = TileSizeY(j); | |
73 DCHECK_GE(x, 0); | |
74 DCHECK_GE(y, 0); | |
75 DCHECK_GE(width, 0); | |
76 DCHECK_GE(height, 0); | |
77 DCHECK_LE(x, total_size_.width()); | |
78 DCHECK_LE(y, total_size_.height()); | |
79 return gfx::Rect(x, y, width, height); | |
80 } | |
81 | |
82 gfx::Rect TilingData::TileBoundsWithBorder(int i, int j) const { | |
83 gfx::Rect bounds = TileBounds(i, j); | |
84 | |
85 if (border_texels_) { | |
86 int x1 = bounds.x(); | |
87 int x2 = bounds.right(); | |
88 int y1 = bounds.y(); | |
89 int y2 = bounds.bottom(); | |
90 | |
91 if (i > 0) | |
92 x1--; | |
93 if (i < (num_tiles_x_ - 1)) | |
94 x2++; | |
95 if (j > 0) | |
96 y1--; | |
97 if (j < (num_tiles_y_ - 1)) | |
98 y2++; | |
99 | |
100 bounds = gfx::Rect(x1, y1, x2 - x1, y2 - y1); | |
101 } | |
102 | |
103 return bounds; | |
104 } | |
105 | |
106 int TilingData::TilePositionX(int x_index) const { | |
107 DCHECK_GE(x_index, 0); | |
108 DCHECK_LT(x_index, num_tiles_x_); | |
109 | |
110 int pos = 0; | |
111 for (int i = 0; i < x_index; i++) | |
112 pos += TileSizeX(i); | |
113 | |
114 return pos; | |
115 } | |
116 | |
117 int TilingData::TilePositionY(int y_index) const { | |
118 DCHECK_GE(y_index, 0); | |
119 DCHECK_LT(y_index, num_tiles_y_); | |
120 | |
121 int pos = 0; | |
122 for (int i = 0; i < y_index; i++) | |
123 pos += TileSizeY(i); | |
124 | |
125 return pos; | |
126 } | |
127 | |
128 int TilingData::TileSizeX(int x_index) const { | |
129 DCHECK_GE(x_index, 0); | |
130 DCHECK_LT(x_index, num_tiles_x_); | |
131 | |
132 if (!x_index && num_tiles_x_ == 1) | |
133 return total_size_.width(); | |
134 if (!x_index && num_tiles_x_ > 1) | |
135 return max_texture_size_.width() - border_texels_; | |
136 if (x_index < num_tiles_x_ - 1) | |
137 return max_texture_size_.width() - 2 * border_texels_; | |
138 if (x_index == num_tiles_x_ - 1) | |
139 return total_size_.width() - TilePositionX(x_index); | |
140 | |
141 NOTREACHED(); | |
142 return 0; | |
143 } | |
144 | |
145 int TilingData::TileSizeY(int y_index) const { | |
146 DCHECK_GE(y_index, 0); | |
147 DCHECK_LT(y_index, num_tiles_y_); | |
148 | |
149 if (!y_index && num_tiles_y_ == 1) | |
150 return total_size_.height(); | |
151 if (!y_index && num_tiles_y_ > 1) | |
152 return max_texture_size_.height() - border_texels_; | |
153 if (y_index < num_tiles_y_ - 1) | |
154 return max_texture_size_.height() - 2 * border_texels_; | |
155 if (y_index == num_tiles_y_ - 1) | |
156 return total_size_.height() - TilePositionY(y_index); | |
157 | |
158 NOTREACHED(); | |
159 return 0; | |
160 } | |
161 | |
162 gfx::Vector2d TilingData::TextureOffset(int x_index, int y_index) const { | |
163 int left = (!x_index || num_tiles_x_ == 1) ? 0 : border_texels_; | |
164 int top = (!y_index || num_tiles_y_ == 1) ? 0 : border_texels_; | |
165 | |
166 return gfx::Vector2d(left, top); | |
167 } | |
168 | |
169 void TilingData::RecomputeNumTiles() { | |
170 num_tiles_x_ = ComputeNumTiles(max_texture_size_.width(), total_size_.width(), border_texels_); | |
171 num_tiles_y_ = ComputeNumTiles(max_texture_size_.height(), total_size_.height( ), border_texels_); | |
172 } | |
173 | |
174 } // namespace cc | |
OLD | NEW |