OLD | NEW |
| (Empty) |
1 // Copyright 2012 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 #include "cc/picture_layer_tiling.h" | |
6 | |
7 #include "cc/test/fake_picture_layer_tiling_client.h" | |
8 #include "testing/gtest/include/gtest/gtest.h" | |
9 #include "ui/gfx/rect_conversions.h" | |
10 #include "ui/gfx/size_conversions.h" | |
11 | |
12 namespace cc { | |
13 namespace { | |
14 | |
15 class PictureLayerTilingIteratorTest : public testing::Test { | |
16 public: | |
17 PictureLayerTilingIteratorTest() {} | |
18 virtual ~PictureLayerTilingIteratorTest() {} | |
19 | |
20 void Initialize(gfx::Size tile_size, | |
21 float contents_scale, | |
22 gfx::Size layer_bounds) { | |
23 client_.SetTileSize(tile_size); | |
24 tiling_ = PictureLayerTiling::Create(contents_scale); | |
25 tiling_->SetClient(&client_); | |
26 tiling_->SetLayerBounds(layer_bounds); | |
27 } | |
28 | |
29 void VerifyTilesExactlyCoverRect( | |
30 float rect_scale, | |
31 gfx::Rect request_rect, | |
32 gfx::Rect expect_rect) { | |
33 EXPECT_TRUE(request_rect.Contains(expect_rect)); | |
34 | |
35 // Iterators are not valid if this ratio is too large (i.e. the | |
36 // tiling is too high-res for a low-res destination rect.) This is an | |
37 // artifact of snapping geometry to integer coordinates and then mapping | |
38 // back to floating point texture coordinates. | |
39 float dest_to_contents_scale = tiling_->contents_scale() / rect_scale; | |
40 ASSERT_LE(dest_to_contents_scale, 2.0); | |
41 | |
42 Region remaining = expect_rect; | |
43 for (PictureLayerTiling::Iterator iter(tiling_.get(), | |
44 rect_scale, | |
45 request_rect, | |
46 PictureLayerTiling::LayerDeviceAlignm
entUnknown); | |
47 iter; | |
48 ++iter) { | |
49 | |
50 // Geometry cannot overlap previous geometry at all | |
51 gfx::Rect geometry = iter.geometry_rect(); | |
52 EXPECT_TRUE(expect_rect.Contains(geometry)); | |
53 EXPECT_TRUE(remaining.Contains(geometry)); | |
54 remaining.Subtract(geometry); | |
55 | |
56 // Sanity check that texture coords are within the texture rect. | |
57 gfx::RectF texture_rect = iter.texture_rect(); | |
58 EXPECT_GE(texture_rect.x(), 0); | |
59 EXPECT_GE(texture_rect.y(), 0); | |
60 EXPECT_LE(texture_rect.right(), client_.TileSize().width()); | |
61 EXPECT_LE(texture_rect.bottom(), client_.TileSize().height()); | |
62 | |
63 EXPECT_EQ(iter.texture_size(), client_.TileSize()); | |
64 } | |
65 | |
66 // The entire rect must be filled by geometry from the tiling. | |
67 EXPECT_TRUE(remaining.IsEmpty()); | |
68 } | |
69 | |
70 void VerifyTilesExactlyCoverRect(float rect_scale, gfx::Rect rect) { | |
71 VerifyTilesExactlyCoverRect(rect_scale, rect, rect); | |
72 } | |
73 | |
74 void VerifyTilesCoverNonContainedRect(float rect_scale, gfx::Rect dest_rect) { | |
75 float dest_to_contents_scale = tiling_->contents_scale() / rect_scale; | |
76 gfx::Rect clamped_rect(gfx::ToEnclosingRect(gfx::ScaleRect( | |
77 tiling_->ContentRect(), 1 / dest_to_contents_scale))); | |
78 clamped_rect.Intersect(dest_rect); | |
79 VerifyTilesExactlyCoverRect(rect_scale, dest_rect, clamped_rect); | |
80 } | |
81 | |
82 protected: | |
83 FakePictureLayerTilingClient client_; | |
84 scoped_ptr<PictureLayerTiling> tiling_; | |
85 | |
86 DISALLOW_COPY_AND_ASSIGN(PictureLayerTilingIteratorTest); | |
87 }; | |
88 | |
89 TEST_F(PictureLayerTilingIteratorTest, IteratorCoversLayerBoundsNoScale) { | |
90 Initialize(gfx::Size(100, 100), 1, gfx::Size(1099, 801)); | |
91 VerifyTilesExactlyCoverRect(1, gfx::Rect()); | |
92 VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1099, 801)); | |
93 VerifyTilesExactlyCoverRect(1, gfx::Rect(52, 83, 789, 412)); | |
94 | |
95 // With borders, a size of 3x3 = 1 pixel of content. | |
96 Initialize(gfx::Size(3, 3), 1, gfx::Size(10, 10)); | |
97 VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1, 1)); | |
98 VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 2, 2)); | |
99 VerifyTilesExactlyCoverRect(1, gfx::Rect(1, 1, 2, 2)); | |
100 VerifyTilesExactlyCoverRect(1, gfx::Rect(3, 2, 5, 2)); | |
101 } | |
102 | |
103 TEST_F(PictureLayerTilingIteratorTest, IteratorCoversLayerBoundsTilingScale) { | |
104 Initialize(gfx::Size(200, 100), 2.0f, gfx::Size(1005, 2010)); | |
105 VerifyTilesExactlyCoverRect(1, gfx::Rect()); | |
106 VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1005, 2010)); | |
107 VerifyTilesExactlyCoverRect(1, gfx::Rect(50, 112, 512, 381)); | |
108 | |
109 Initialize(gfx::Size(3, 3), 2.0f, gfx::Size(10, 10)); | |
110 VerifyTilesExactlyCoverRect(1, gfx::Rect()); | |
111 VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1, 1)); | |
112 VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 2, 2)); | |
113 VerifyTilesExactlyCoverRect(1, gfx::Rect(1, 1, 2, 2)); | |
114 VerifyTilesExactlyCoverRect(1, gfx::Rect(3, 2, 5, 2)); | |
115 | |
116 Initialize(gfx::Size(100, 200), 0.5f, gfx::Size(1005, 2010)); | |
117 VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1005, 2010)); | |
118 VerifyTilesExactlyCoverRect(1, gfx::Rect(50, 112, 512, 381)); | |
119 | |
120 Initialize(gfx::Size(150, 250), 0.37f, gfx::Size(1005, 2010)); | |
121 VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1005, 2010)); | |
122 VerifyTilesExactlyCoverRect(1, gfx::Rect(50, 112, 512, 381)); | |
123 | |
124 Initialize(gfx::Size(312, 123), 0.01f, gfx::Size(1005, 2010)); | |
125 VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1005, 2010)); | |
126 VerifyTilesExactlyCoverRect(1, gfx::Rect(50, 112, 512, 381)); | |
127 } | |
128 | |
129 TEST_F(PictureLayerTilingIteratorTest, IteratorCoversLayerBoundsBothScale) { | |
130 Initialize(gfx::Size(50, 50), 4.0f, gfx::Size(800, 600)); | |
131 VerifyTilesExactlyCoverRect(2.0f, gfx::Rect()); | |
132 VerifyTilesExactlyCoverRect(2.0f, gfx::Rect(0, 0, 1600, 1200)); | |
133 VerifyTilesExactlyCoverRect(2.0f, gfx::Rect(512, 365, 253, 182)); | |
134 | |
135 float scale = 6.7f; | |
136 gfx::Size bounds(800, 600); | |
137 gfx::Rect full_rect(gfx::ToCeiledSize(gfx::ScaleSize(bounds, scale))); | |
138 Initialize(gfx::Size(256, 512), 5.2f, bounds); | |
139 VerifyTilesExactlyCoverRect(scale, full_rect); | |
140 VerifyTilesExactlyCoverRect(scale, gfx::Rect(2014, 1579, 867, 1033)); | |
141 } | |
142 | |
143 TEST_F(PictureLayerTilingIteratorTest, IteratorEmptyRect) { | |
144 Initialize(gfx::Size(100, 100), 1.0f, gfx::Size(800, 600)); | |
145 | |
146 gfx::Rect empty; | |
147 PictureLayerTiling::Iterator iter(tiling_.get(), 1.0f, empty, | |
148 PictureLayerTiling::LayerDeviceAlignmentUnkn
own); | |
149 EXPECT_FALSE(iter); | |
150 } | |
151 | |
152 TEST_F(PictureLayerTilingIteratorTest, NonIntersectingRect) { | |
153 Initialize(gfx::Size(100, 100), 1.0f, gfx::Size(800, 600)); | |
154 gfx::Rect non_intersecting(1000, 1000, 50, 50); | |
155 PictureLayerTiling::Iterator iter(tiling_.get(), 1, non_intersecting, | |
156 PictureLayerTiling::LayerDeviceAlignmentUnkn
own); | |
157 EXPECT_FALSE(iter); | |
158 } | |
159 | |
160 TEST_F(PictureLayerTilingIteratorTest, LayerEdgeTextureCoordinates) { | |
161 Initialize(gfx::Size(300, 300), 1.0f, gfx::Size(256, 256)); | |
162 // All of these sizes are 256x256, scaled and ceiled. | |
163 VerifyTilesExactlyCoverRect(1.0f, gfx::Rect(gfx::Size(256, 256))); | |
164 VerifyTilesExactlyCoverRect(0.8f, gfx::Rect(gfx::Size(205, 205))); | |
165 VerifyTilesExactlyCoverRect(1.2f, gfx::Rect(gfx::Size(308, 308))); | |
166 } | |
167 | |
168 TEST_F(PictureLayerTilingIteratorTest, NonContainedDestRect) { | |
169 Initialize(gfx::Size(100, 100), 1.0f, gfx::Size(400, 400)); | |
170 | |
171 // Too large in all dimensions | |
172 VerifyTilesCoverNonContainedRect(1.0f, gfx::Rect(-1000, -1000, 2000, 2000)); | |
173 VerifyTilesCoverNonContainedRect(1.5f, gfx::Rect(-1000, -1000, 2000, 2000)); | |
174 VerifyTilesCoverNonContainedRect(0.5f, gfx::Rect(-1000, -1000, 2000, 2000)); | |
175 | |
176 // Partially covering content, but too large | |
177 VerifyTilesCoverNonContainedRect(1.0f, gfx::Rect(-1000, 100, 2000, 100)); | |
178 VerifyTilesCoverNonContainedRect(1.5f, gfx::Rect(-1000, 100, 2000, 100)); | |
179 VerifyTilesCoverNonContainedRect(0.5f, gfx::Rect(-1000, 100, 2000, 100)); | |
180 } | |
181 | |
182 TEST(PictureLayerTilingTest, ExpandRectEqual) { | |
183 gfx::Rect in(40, 50, 100, 200); | |
184 gfx::Rect bounds(-1000, -1000, 10000, 10000); | |
185 int64 target_area = 100 * 200; | |
186 gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy( | |
187 in, target_area, bounds); | |
188 EXPECT_EQ(in.ToString(), out.ToString()); | |
189 } | |
190 | |
191 TEST(PictureLayerTilingTest, ExpandRectSmaller) { | |
192 gfx::Rect in(40, 50, 100, 200); | |
193 gfx::Rect bounds(-1000, -1000, 10000, 10000); | |
194 int64 target_area = 100 * 100; | |
195 gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy( | |
196 in, target_area, bounds); | |
197 EXPECT_EQ(out.bottom() - in.bottom(), in.y() - out.y()); | |
198 EXPECT_EQ(out.right() - in.right(), in.x() - out.x()); | |
199 EXPECT_EQ(out.width() - in.width(), out.height() - in.height()); | |
200 EXPECT_NEAR(100 * 100, out.width() * out.height(), 50); | |
201 EXPECT_TRUE(bounds.Contains(out)); | |
202 } | |
203 | |
204 TEST(PictureLayerTilingTest, ExpandRectUnbounded) { | |
205 gfx::Rect in(40, 50, 100, 200); | |
206 gfx::Rect bounds(-1000, -1000, 10000, 10000); | |
207 int64 target_area = 200 * 200; | |
208 gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy( | |
209 in, target_area, bounds); | |
210 EXPECT_EQ(out.bottom() - in.bottom(), in.y() - out.y()); | |
211 EXPECT_EQ(out.right() - in.right(), in.x() - out.x()); | |
212 EXPECT_EQ(out.width() - in.width(), out.height() - in.height()); | |
213 EXPECT_NEAR(200 * 200, out.width() * out.height(), 100); | |
214 EXPECT_TRUE(bounds.Contains(out)); | |
215 } | |
216 | |
217 TEST(PictureLayerTilingTest, ExpandRectBoundedSmaller) { | |
218 gfx::Rect in(40, 50, 100, 200); | |
219 gfx::Rect bounds(50, 60, 40, 30); | |
220 int64 target_area = 200 * 200; | |
221 gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy( | |
222 in, target_area, bounds); | |
223 EXPECT_EQ(bounds.ToString(), out.ToString()); | |
224 } | |
225 | |
226 TEST(PictureLayerTilingTest, ExpandRectBoundedEqual) { | |
227 gfx::Rect in(40, 50, 100, 200); | |
228 gfx::Rect bounds = in; | |
229 int64 target_area = 200 * 200; | |
230 gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy( | |
231 in, target_area, bounds); | |
232 EXPECT_EQ(bounds.ToString(), out.ToString()); | |
233 } | |
234 | |
235 TEST(PictureLayerTilingTest, ExpandRectBoundedSmallerStretchVertical) { | |
236 gfx::Rect in(40, 50, 100, 200); | |
237 gfx::Rect bounds(45, 0, 90, 300); | |
238 int64 target_area = 200 * 200; | |
239 gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy( | |
240 in, target_area, bounds); | |
241 EXPECT_EQ(bounds.ToString(), out.ToString()); | |
242 } | |
243 | |
244 TEST(PictureLayerTilingTest, ExpandRectBoundedEqualStretchVertical) { | |
245 gfx::Rect in(40, 50, 100, 200); | |
246 gfx::Rect bounds(40, 0, 100, 300); | |
247 int64 target_area = 200 * 200; | |
248 gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy( | |
249 in, target_area, bounds); | |
250 EXPECT_EQ(bounds.ToString(), out.ToString()); | |
251 } | |
252 | |
253 TEST(PictureLayerTilingTest, ExpandRectBoundedSmallerStretchHorizontal) { | |
254 gfx::Rect in(40, 50, 100, 200); | |
255 gfx::Rect bounds(0, 55, 180, 190); | |
256 int64 target_area = 200 * 200; | |
257 gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy( | |
258 in, target_area, bounds); | |
259 EXPECT_EQ(bounds.ToString(), out.ToString()); | |
260 } | |
261 | |
262 TEST(PictureLayerTilingTest, ExpandRectBoundedEqualStretchHorizontal) { | |
263 gfx::Rect in(40, 50, 100, 200); | |
264 gfx::Rect bounds(0, 50, 180, 200); | |
265 int64 target_area = 200 * 200; | |
266 gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy( | |
267 in, target_area, bounds); | |
268 EXPECT_EQ(bounds.ToString(), out.ToString()); | |
269 } | |
270 | |
271 TEST(PictureLayerTilingTest, ExpandRectBoundedLeft) { | |
272 gfx::Rect in(40, 50, 100, 200); | |
273 gfx::Rect bounds(20, -1000, 10000, 10000); | |
274 int64 target_area = 200 * 200; | |
275 gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy( | |
276 in, target_area, bounds); | |
277 EXPECT_EQ(out.bottom() - in.bottom(), in.y() - out.y()); | |
278 EXPECT_EQ(out.bottom() - in.bottom(), out.right() - in.right()); | |
279 EXPECT_LE(out.width() * out.height(), target_area); | |
280 EXPECT_GT(out.width() * out.height(), | |
281 target_area - out.width() - out.height() * 2); | |
282 EXPECT_TRUE(bounds.Contains(out)); | |
283 } | |
284 | |
285 TEST(PictureLayerTilingTest, ExpandRectBoundedRight) { | |
286 gfx::Rect in(40, 50, 100, 200); | |
287 gfx::Rect bounds(-1000, -1000, 1000+120, 10000); | |
288 int64 target_area = 200 * 200; | |
289 gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy( | |
290 in, target_area, bounds); | |
291 EXPECT_EQ(out.bottom() - in.bottom(), in.y() - out.y()); | |
292 EXPECT_EQ(out.bottom() - in.bottom(), in.x() - out.x()); | |
293 EXPECT_LE(out.width() * out.height(), target_area); | |
294 EXPECT_GT(out.width() * out.height(), | |
295 target_area - out.width() - out.height() * 2); | |
296 EXPECT_TRUE(bounds.Contains(out)); | |
297 } | |
298 | |
299 TEST(PictureLayerTilingTest, ExpandRectBoundedTop) { | |
300 gfx::Rect in(40, 50, 100, 200); | |
301 gfx::Rect bounds(-1000, 30, 10000, 10000); | |
302 int64 target_area = 200 * 200; | |
303 gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy( | |
304 in, target_area, bounds); | |
305 EXPECT_EQ(out.right() - in.right(), in.x() - out.x()); | |
306 EXPECT_EQ(out.right() - in.right(), out.bottom() - in.bottom()); | |
307 EXPECT_LE(out.width() * out.height(), target_area); | |
308 EXPECT_GT(out.width() * out.height(), | |
309 target_area - out.width() * 2 - out.height()); | |
310 EXPECT_TRUE(bounds.Contains(out)); | |
311 } | |
312 | |
313 TEST(PictureLayerTilingTest, ExpandRectBoundedBottom) { | |
314 gfx::Rect in(40, 50, 100, 200); | |
315 gfx::Rect bounds(-1000, -1000, 10000, 1000 + 220); | |
316 int64 target_area = 200 * 200; | |
317 gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy( | |
318 in, target_area, bounds); | |
319 EXPECT_EQ(out.right() - in.right(), in.x() - out.x()); | |
320 EXPECT_EQ(out.right() - in.right(), in.y() - out.y()); | |
321 EXPECT_LE(out.width() * out.height(), target_area); | |
322 EXPECT_GT(out.width() * out.height(), | |
323 target_area - out.width() * 2 - out.height()); | |
324 EXPECT_TRUE(bounds.Contains(out)); | |
325 } | |
326 | |
327 TEST(PictureLayerTilingTest, ExpandRectSquishedHorizontally) { | |
328 gfx::Rect in(40, 50, 100, 200); | |
329 gfx::Rect bounds(0, -4000, 100+40+20, 100000); | |
330 int64 target_area = 400 * 400; | |
331 gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy( | |
332 in, target_area, bounds); | |
333 EXPECT_EQ(20, out.right() - in.right()); | |
334 EXPECT_EQ(40, in.x() - out.x()); | |
335 EXPECT_EQ(out.bottom() - in.bottom(), in.y() - out.y()); | |
336 EXPECT_LE(out.width() * out.height(), target_area); | |
337 EXPECT_GT(out.width() * out.height(), | |
338 target_area - out.width() * 2); | |
339 EXPECT_TRUE(bounds.Contains(out)); | |
340 } | |
341 | |
342 TEST(PictureLayerTilingTest, ExpandRectSquishedVertically) { | |
343 gfx::Rect in(40, 50, 100, 200); | |
344 gfx::Rect bounds(-4000, 0, 100000, 200+50+30); | |
345 int64 target_area = 400 * 400; | |
346 gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy( | |
347 in, target_area, bounds); | |
348 EXPECT_EQ(30, out.bottom() - in.bottom()); | |
349 EXPECT_EQ(50, in.y() - out.y()); | |
350 EXPECT_EQ(out.right() - in.right(), in.x() - out.x()); | |
351 EXPECT_LE(out.width() * out.height(), target_area); | |
352 EXPECT_GT(out.width() * out.height(), | |
353 target_area - out.height() * 2); | |
354 EXPECT_TRUE(bounds.Contains(out)); | |
355 } | |
356 | |
357 } // namespace | |
358 } // namespace cc | |
OLD | NEW |