blob: 2fa175edb8bdedbf9b428fc59eeef80cc423d99d [file] [log] [blame]
[email protected]94f206c12012-08-25 00:09:141// Copyright 2011 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
[email protected]556fd292013-03-18 08:03:045#include "cc/trees/layer_tree_host_common.h"
[email protected]94f206c12012-08-25 00:09:146
[email protected]95e4e1a02013-03-18 07:09:097#include "cc/animation/layer_animation_controller.h"
[email protected]681ccff2013-03-18 06:13:528#include "cc/base/math_util.h"
9#include "cc/base/thread.h"
[email protected]cc3cfaa2013-03-18 09:05:5210#include "cc/layers/content_layer.h"
11#include "cc/layers/content_layer_client.h"
12#include "cc/layers/heads_up_display_layer_impl.h"
13#include "cc/layers/layer.h"
14#include "cc/layers/layer_impl.h"
[email protected]50761e92013-03-29 20:51:2815#include "cc/layers/render_surface.h"
16#include "cc/layers/render_surface_impl.h"
[email protected]101441ce2012-10-16 01:45:0317#include "cc/test/animation_test_common.h"
[email protected]586d51ed2012-12-07 20:31:4518#include "cc/test/fake_impl_proxy.h"
19#include "cc/test/fake_layer_tree_host_impl.h"
[email protected]101441ce2012-10-16 01:45:0320#include "cc/test/geometry_test_utils.h"
[email protected]556fd292013-03-18 08:03:0421#include "cc/trees/layer_tree_impl.h"
22#include "cc/trees/proxy.h"
23#include "cc/trees/single_thread_proxy.h"
[email protected]7f0c53db2012-10-02 00:23:1824#include "testing/gmock/include/gmock/gmock.h"
25#include "testing/gtest/include/gtest/gtest.h"
[email protected]93698c12012-12-07 00:43:5626#include "ui/gfx/quad_f.h"
27#include "ui/gfx/size_conversions.h"
[email protected]c8686a02012-11-27 08:29:0028#include "ui/gfx/transform.h"
[email protected]94f206c12012-08-25 00:09:1429
[email protected]ba565742012-11-10 09:29:4830namespace cc {
[email protected]94f206c12012-08-25 00:09:1431namespace {
32
[email protected]fb661802013-03-25 01:59:3233template <typename LayerType>
34void SetLayerPropertiesForTestingInternal(
35 LayerType* layer,
36 const gfx::Transform& transform,
37 const gfx::Transform& sublayer_transform,
38 gfx::PointF anchor,
39 gfx::PointF position,
40 gfx::Size bounds,
41 bool preserves3d) {
42 layer->SetTransform(transform);
43 layer->SetSublayerTransform(sublayer_transform);
44 layer->SetAnchorPoint(anchor);
45 layer->SetPosition(position);
46 layer->SetBounds(bounds);
47 layer->SetPreserves3d(preserves3d);
[email protected]94f206c12012-08-25 00:09:1448}
49
[email protected]fb661802013-03-25 01:59:3250void SetLayerPropertiesForTesting(Layer* layer,
51 const gfx::Transform& transform,
52 const gfx::Transform& sublayer_transform,
53 gfx::PointF anchor,
54 gfx::PointF position,
55 gfx::Size bounds,
56 bool preserves3d) {
57 SetLayerPropertiesForTestingInternal<Layer>(layer,
58 transform,
59 sublayer_transform,
60 anchor,
61 position,
62 bounds,
63 preserves3d);
64 layer->SetAutomaticallyComputeRasterScale(true);
[email protected]94f206c12012-08-25 00:09:1465}
66
[email protected]fb661802013-03-25 01:59:3267void SetLayerPropertiesForTesting(LayerImpl* layer,
68 const gfx::Transform& transform,
69 const gfx::Transform& sublayer_transform,
70 gfx::PointF anchor,
71 gfx::PointF position,
72 gfx::Size bounds,
73 bool preserves3d) {
74 SetLayerPropertiesForTestingInternal<LayerImpl>(layer,
75 transform,
76 sublayer_transform,
77 anchor,
78 position,
79 bounds,
80 preserves3d);
81 layer->SetContentBounds(bounds);
[email protected]94f206c12012-08-25 00:09:1482}
83
[email protected]fb661802013-03-25 01:59:3284void ExecuteCalculateDrawProperties(Layer* root_layer,
85 float device_scale_factor,
86 float page_scale_factor,
[email protected]f2136262013-04-26 21:10:1987 Layer* page_scale_application_layer,
[email protected]fb661802013-03-25 01:59:3288 bool can_use_lcd_text) {
[email protected]f2136262013-04-26 21:10:1989 EXPECT_TRUE(page_scale_application_layer || (page_scale_factor == 1.f));
[email protected]fb661802013-03-25 01:59:3290 gfx::Transform identity_matrix;
[email protected]50761e92013-03-29 20:51:2891 LayerList dummy_render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:3292 int dummy_max_texture_size = 512;
93 gfx::Size device_viewport_size =
94 gfx::Size(root_layer->bounds().width() * device_scale_factor,
95 root_layer->bounds().height() * device_scale_factor);
[email protected]94f206c12012-08-25 00:09:1496
[email protected]fb661802013-03-25 01:59:3297 // We are probably not testing what is intended if the root_layer bounds are
98 // empty.
99 DCHECK(!root_layer->bounds().IsEmpty());
100 LayerTreeHostCommon::CalculateDrawProperties(
101 root_layer,
102 device_viewport_size,
103 device_scale_factor,
104 page_scale_factor,
[email protected]f2136262013-04-26 21:10:19105 page_scale_application_layer,
[email protected]fb661802013-03-25 01:59:32106 dummy_max_texture_size,
107 can_use_lcd_text,
108 &dummy_render_surface_layer_list);
[email protected]94f206c12012-08-25 00:09:14109}
110
[email protected]fb661802013-03-25 01:59:32111void ExecuteCalculateDrawProperties(LayerImpl* root_layer,
112 float device_scale_factor,
113 float page_scale_factor,
[email protected]f2136262013-04-26 21:10:19114 LayerImpl* page_scale_application_layer,
[email protected]fb661802013-03-25 01:59:32115 bool can_use_lcd_text) {
116 gfx::Transform identity_matrix;
[email protected]50761e92013-03-29 20:51:28117 LayerImplList dummy_render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:32118 int dummy_max_texture_size = 512;
119 gfx::Size device_viewport_size =
120 gfx::Size(root_layer->bounds().width() * device_scale_factor,
121 root_layer->bounds().height() * device_scale_factor);
[email protected]94f206c12012-08-25 00:09:14122
[email protected]fb661802013-03-25 01:59:32123 // We are probably not testing what is intended if the root_layer bounds are
124 // empty.
125 DCHECK(!root_layer->bounds().IsEmpty());
126 LayerTreeHostCommon::CalculateDrawProperties(root_layer,
127 device_viewport_size,
128 device_scale_factor,
129 page_scale_factor,
[email protected]f2136262013-04-26 21:10:19130 page_scale_application_layer,
[email protected]fb661802013-03-25 01:59:32131 dummy_max_texture_size,
132 can_use_lcd_text,
133 &dummy_render_surface_layer_list,
134 false);
[email protected]94f206c12012-08-25 00:09:14135}
136
[email protected]fb661802013-03-25 01:59:32137template <class LayerType>
138void ExecuteCalculateDrawProperties(LayerType* root_layer) {
[email protected]f2136262013-04-26 21:10:19139 LayerType* page_scale_application_layer = NULL;
140 ExecuteCalculateDrawProperties(
141 root_layer, 1.f, 1.f, page_scale_application_layer, false);
[email protected]fb661802013-03-25 01:59:32142}
[email protected]94f206c12012-08-25 00:09:14143
[email protected]fb661802013-03-25 01:59:32144template <class LayerType>
145void ExecuteCalculateDrawProperties(LayerType* root_layer,
146 float device_scale_factor) {
[email protected]f2136262013-04-26 21:10:19147 LayerType* page_scale_application_layer = NULL;
148 ExecuteCalculateDrawProperties(root_layer,
149 device_scale_factor,
150 1.f,
151 page_scale_application_layer,
152 false);
[email protected]fb661802013-03-25 01:59:32153}
[email protected]94f206c12012-08-25 00:09:14154
[email protected]fb661802013-03-25 01:59:32155template <class LayerType>
156void ExecuteCalculateDrawProperties(LayerType* root_layer,
157 float device_scale_factor,
[email protected]f2136262013-04-26 21:10:19158 float page_scale_factor,
159 LayerType* page_scale_application_layer) {
160 ExecuteCalculateDrawProperties(root_layer,
161 device_scale_factor,
162 page_scale_factor,
163 page_scale_application_layer,
164 false);
[email protected]fb661802013-03-25 01:59:32165}
[email protected]94f206c12012-08-25 00:09:14166
[email protected]96baf3e2012-10-22 23:09:55167class LayerWithForcedDrawsContent : public Layer {
[email protected]fb661802013-03-25 01:59:32168 public:
169 LayerWithForcedDrawsContent() : Layer() {}
[email protected]94f206c12012-08-25 00:09:14170
[email protected]fb661802013-03-25 01:59:32171 virtual bool DrawsContent() const OVERRIDE;
[email protected]d58499a2012-10-09 22:27:47172
[email protected]fb661802013-03-25 01:59:32173 private:
174 virtual ~LayerWithForcedDrawsContent() {}
[email protected]94f206c12012-08-25 00:09:14175};
176
[email protected]aedf4e52013-01-09 23:24:44177class LayerCanClipSelf : public Layer {
[email protected]fb661802013-03-25 01:59:32178 public:
179 LayerCanClipSelf() : Layer() {}
[email protected]aedf4e52013-01-09 23:24:44180
[email protected]fb661802013-03-25 01:59:32181 virtual bool DrawsContent() const OVERRIDE;
182 virtual bool CanClipSelf() const OVERRIDE;
[email protected]aedf4e52013-01-09 23:24:44183
[email protected]fb661802013-03-25 01:59:32184 private:
185 virtual ~LayerCanClipSelf() {}
[email protected]aedf4e52013-01-09 23:24:44186};
187
[email protected]fb661802013-03-25 01:59:32188bool LayerWithForcedDrawsContent::DrawsContent() const { return true; }
[email protected]aedf4e52013-01-09 23:24:44189
[email protected]fb661802013-03-25 01:59:32190bool LayerCanClipSelf::DrawsContent() const { return true; }
[email protected]aedf4e52013-01-09 23:24:44191
[email protected]fb661802013-03-25 01:59:32192bool LayerCanClipSelf::CanClipSelf() const { return true; }
[email protected]aedf4e52013-01-09 23:24:44193
[email protected]96baf3e2012-10-22 23:09:55194class MockContentLayerClient : public ContentLayerClient {
[email protected]fb661802013-03-25 01:59:32195 public:
196 MockContentLayerClient() {}
197 virtual ~MockContentLayerClient() {}
198 virtual void PaintContents(SkCanvas* canvas,
199 gfx::Rect clip,
200 gfx::RectF* opaque) OVERRIDE {}
201 virtual void DidChangeLayerCanUseLCDText() OVERRIDE {}
[email protected]f34a24232012-09-20 22:59:55202};
203
[email protected]fb661802013-03-25 01:59:32204scoped_refptr<ContentLayer> CreateDrawableContentLayer(
205 ContentLayerClient* delegate) {
206 scoped_refptr<ContentLayer> to_return = ContentLayer::Create(delegate);
207 to_return->SetIsDrawable(true);
208 return to_return;
[email protected]f34a24232012-09-20 22:59:55209}
210
[email protected]fb661802013-03-25 01:59:32211#define EXPECT_CONTENTS_SCALE_EQ(expected, layer) \
212 do { \
213 EXPECT_FLOAT_EQ(expected, layer->contents_scale_x()); \
214 EXPECT_FLOAT_EQ(expected, layer->contents_scale_y()); \
[email protected]904e9132012-11-01 00:12:47215 } while (false)
216
[email protected]fb661802013-03-25 01:59:32217TEST(LayerTreeHostCommonTest, TransformsForNoOpLayer) {
218 // Sanity check: For layers positioned at zero, with zero size,
219 // and with identity transforms, then the draw transform,
220 // screen space transform, and the hierarchy passed on to children
221 // layers should also be identity transforms.
[email protected]94f206c12012-08-25 00:09:14222
[email protected]fb661802013-03-25 01:59:32223 scoped_refptr<Layer> parent = Layer::Create();
224 scoped_refptr<Layer> child = Layer::Create();
225 scoped_refptr<Layer> grand_child = Layer::Create();
226 parent->AddChild(child);
227 child->AddChild(grand_child);
[email protected]94f206c12012-08-25 00:09:14228
[email protected]fb661802013-03-25 01:59:32229 gfx::Transform identity_matrix;
230 SetLayerPropertiesForTesting(parent.get(),
231 identity_matrix,
232 identity_matrix,
233 gfx::PointF(),
234 gfx::PointF(),
235 gfx::Size(100, 100),
236 false);
237 SetLayerPropertiesForTesting(child.get(),
238 identity_matrix,
239 identity_matrix,
240 gfx::PointF(),
241 gfx::PointF(),
242 gfx::Size(),
243 false);
244 SetLayerPropertiesForTesting(grand_child.get(),
245 identity_matrix,
246 identity_matrix,
247 gfx::PointF(),
248 gfx::PointF(),
249 gfx::Size(),
250 false);
[email protected]94f206c12012-08-25 00:09:14251
[email protected]fb661802013-03-25 01:59:32252 ExecuteCalculateDrawProperties(parent.get());
[email protected]94f206c12012-08-25 00:09:14253
[email protected]fb661802013-03-25 01:59:32254 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix, child->draw_transform());
255 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
256 child->screen_space_transform());
257 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
258 grand_child->draw_transform());
259 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
260 grand_child->screen_space_transform());
[email protected]94f206c12012-08-25 00:09:14261}
262
[email protected]fb661802013-03-25 01:59:32263TEST(LayerTreeHostCommonTest, TransformsForSingleLayer) {
264 gfx::Transform identity_matrix;
265 scoped_refptr<Layer> layer = Layer::Create();
[email protected]94f206c12012-08-25 00:09:14266
[email protected]fb661802013-03-25 01:59:32267 scoped_refptr<Layer> root = Layer::Create();
268 SetLayerPropertiesForTesting(root.get(),
269 identity_matrix,
270 identity_matrix,
271 gfx::PointF(),
272 gfx::PointF(),
273 gfx::Size(1, 2),
274 false);
275 root->AddChild(layer);
[email protected]ecc12622012-10-30 20:45:42276
[email protected]fb661802013-03-25 01:59:32277 // Case 1: setting the sublayer transform should not affect this layer's draw
278 // transform or screen-space transform.
279 gfx::Transform arbitrary_translation;
280 arbitrary_translation.Translate(10.0, 20.0);
281 SetLayerPropertiesForTesting(layer.get(),
282 identity_matrix,
283 arbitrary_translation,
284 gfx::PointF(),
285 gfx::PointF(),
286 gfx::Size(100, 100),
287 false);
288 ExecuteCalculateDrawProperties(root.get());
289 gfx::Transform expected_draw_transform = identity_matrix;
290 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_draw_transform,
291 layer->draw_transform());
292 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
293 layer->screen_space_transform());
[email protected]94f206c12012-08-25 00:09:14294
[email protected]fb661802013-03-25 01:59:32295 // Case 2: Setting the bounds of the layer should not affect either the draw
296 // transform or the screenspace transform.
297 gfx::Transform translation_to_center;
298 translation_to_center.Translate(5.0, 6.0);
299 SetLayerPropertiesForTesting(layer.get(),
300 identity_matrix,
301 identity_matrix,
302 gfx::PointF(),
303 gfx::PointF(),
304 gfx::Size(10, 12),
305 false);
306 ExecuteCalculateDrawProperties(root.get());
307 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix, layer->draw_transform());
308 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
309 layer->screen_space_transform());
[email protected]94f206c12012-08-25 00:09:14310
[email protected]fb661802013-03-25 01:59:32311 // Case 3: The anchor point by itself (without a layer transform) should have
312 // no effect on the transforms.
313 SetLayerPropertiesForTesting(layer.get(),
314 identity_matrix,
315 identity_matrix,
316 gfx::PointF(0.25f, 0.25f),
317 gfx::PointF(),
318 gfx::Size(10, 12),
319 false);
320 ExecuteCalculateDrawProperties(root.get());
321 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix, layer->draw_transform());
322 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
323 layer->screen_space_transform());
[email protected]94f206c12012-08-25 00:09:14324
[email protected]fb661802013-03-25 01:59:32325 // Case 4: A change in actual position affects both the draw transform and
326 // screen space transform.
327 gfx::Transform position_transform;
328 position_transform.Translate(0.0, 1.2);
329 SetLayerPropertiesForTesting(layer.get(),
330 identity_matrix,
331 identity_matrix,
332 gfx::PointF(0.25f, 0.25f),
333 gfx::PointF(0.f, 1.2f),
334 gfx::Size(10, 12),
335 false);
336 ExecuteCalculateDrawProperties(root.get());
337 EXPECT_TRANSFORMATION_MATRIX_EQ(position_transform, layer->draw_transform());
338 EXPECT_TRANSFORMATION_MATRIX_EQ(position_transform,
339 layer->screen_space_transform());
[email protected]94f206c12012-08-25 00:09:14340
[email protected]fb661802013-03-25 01:59:32341 // Case 5: In the correct sequence of transforms, the layer transform should
342 // pre-multiply the translation_to_center. This is easily tested by using a
343 // scale transform, because scale and translation are not commutative.
344 gfx::Transform layer_transform;
345 layer_transform.Scale3d(2.0, 2.0, 1.0);
346 SetLayerPropertiesForTesting(layer.get(),
347 layer_transform,
348 identity_matrix,
349 gfx::PointF(),
350 gfx::PointF(),
351 gfx::Size(10, 12),
352 false);
353 ExecuteCalculateDrawProperties(root.get());
354 EXPECT_TRANSFORMATION_MATRIX_EQ(layer_transform, layer->draw_transform());
355 EXPECT_TRANSFORMATION_MATRIX_EQ(layer_transform,
356 layer->screen_space_transform());
[email protected]94f206c12012-08-25 00:09:14357
[email protected]fb661802013-03-25 01:59:32358 // Case 6: The layer transform should occur with respect to the anchor point.
359 gfx::Transform translation_to_anchor;
360 translation_to_anchor.Translate(5.0, 0.0);
361 gfx::Transform expected_result =
362 translation_to_anchor * layer_transform * Inverse(translation_to_anchor);
363 SetLayerPropertiesForTesting(layer.get(),
364 layer_transform,
365 identity_matrix,
366 gfx::PointF(0.5f, 0.f),
367 gfx::PointF(),
368 gfx::Size(10, 12),
369 false);
370 ExecuteCalculateDrawProperties(root.get());
371 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_result, layer->draw_transform());
372 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_result,
373 layer->screen_space_transform());
[email protected]94f206c12012-08-25 00:09:14374
[email protected]fb661802013-03-25 01:59:32375 // Case 7: Verify that position pre-multiplies the layer transform. The
376 // current implementation of CalculateDrawProperties does this implicitly, but
377 // it is still worth testing to detect accidental regressions.
378 expected_result = position_transform * translation_to_anchor *
379 layer_transform * Inverse(translation_to_anchor);
380 SetLayerPropertiesForTesting(layer.get(),
381 layer_transform,
382 identity_matrix,
383 gfx::PointF(0.5f, 0.f),
384 gfx::PointF(0.f, 1.2f),
385 gfx::Size(10, 12),
386 false);
387 ExecuteCalculateDrawProperties(root.get());
388 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_result, layer->draw_transform());
389 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_result,
390 layer->screen_space_transform());
[email protected]94f206c12012-08-25 00:09:14391}
392
[email protected]fb661802013-03-25 01:59:32393TEST(LayerTreeHostCommonTest, TransformsAboutScrollOffset) {
394 const gfx::Vector2d kScrollOffset(50, 100);
395 const gfx::Vector2dF kScrollDelta(2.34f, 5.67f);
396 const gfx::PointF kScrollLayerPosition(-kScrollOffset.x(),
397 -kScrollOffset.y());
398 const float kPageScale = 0.888f;
399 const float kDeviceScale = 1.666f;
[email protected]657b24c2013-03-06 09:01:20400
[email protected]fb661802013-03-25 01:59:32401 FakeImplProxy proxy;
402 FakeLayerTreeHostImpl host_impl(&proxy);
[email protected]657b24c2013-03-06 09:01:20403
[email protected]fb661802013-03-25 01:59:32404 gfx::Transform identity_matrix;
405 scoped_ptr<LayerImpl> sublayer_scoped_ptr(
406 LayerImpl::Create(host_impl.active_tree(), 1));
407 LayerImpl* sublayer = sublayer_scoped_ptr.get();
408 sublayer->SetContentsScale(kPageScale * kDeviceScale,
409 kPageScale * kDeviceScale);
410 SetLayerPropertiesForTesting(sublayer,
411 identity_matrix,
412 identity_matrix,
413 gfx::Point(),
414 gfx::PointF(),
415 gfx::Size(500, 500),
416 false);
[email protected]657b24c2013-03-06 09:01:20417
[email protected]fb661802013-03-25 01:59:32418 scoped_ptr<LayerImpl> scroll_layerScopedPtr(
419 LayerImpl::Create(host_impl.active_tree(), 2));
420 LayerImpl* scroll_layer = scroll_layerScopedPtr.get();
421 SetLayerPropertiesForTesting(scroll_layer,
422 identity_matrix,
423 identity_matrix,
424 gfx::PointF(),
425 kScrollLayerPosition,
426 gfx::Size(10, 20),
427 false);
428 scroll_layer->SetScrollable(true);
429 scroll_layer->SetScrollOffset(kScrollOffset);
430 scroll_layer->SetScrollDelta(kScrollDelta);
431 gfx::Transform impl_transform;
[email protected]fb661802013-03-25 01:59:32432 scroll_layer->AddChild(sublayer_scoped_ptr.Pass());
[email protected]657b24c2013-03-06 09:01:20433
[email protected]fb661802013-03-25 01:59:32434 scoped_ptr<LayerImpl> root(LayerImpl::Create(host_impl.active_tree(), 3));
435 SetLayerPropertiesForTesting(root.get(),
436 identity_matrix,
437 identity_matrix,
438 gfx::PointF(),
439 gfx::PointF(),
440 gfx::Size(3, 4),
441 false);
442 root->AddChild(scroll_layerScopedPtr.Pass());
[email protected]657b24c2013-03-06 09:01:20443
[email protected]f2136262013-04-26 21:10:19444 ExecuteCalculateDrawProperties(
445 root.get(), kDeviceScale, kPageScale, scroll_layer);
[email protected]fb661802013-03-25 01:59:32446 gfx::Transform expected_transform = identity_matrix;
447 gfx::PointF sub_layer_screen_position = kScrollLayerPosition - kScrollDelta;
448 sub_layer_screen_position.Scale(kPageScale * kDeviceScale);
449 expected_transform.Translate(MathUtil::Round(sub_layer_screen_position.x()),
450 MathUtil::Round(sub_layer_screen_position.y()));
451 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_transform,
452 sublayer->draw_transform());
453 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_transform,
454 sublayer->screen_space_transform());
[email protected]657b24c2013-03-06 09:01:20455
[email protected]fb661802013-03-25 01:59:32456 gfx::Transform arbitrary_translate;
457 const float kTranslateX = 10.6f;
458 const float kTranslateY = 20.6f;
459 arbitrary_translate.Translate(kTranslateX, kTranslateY);
460 SetLayerPropertiesForTesting(scroll_layer,
461 arbitrary_translate,
462 identity_matrix,
463 gfx::PointF(),
464 kScrollLayerPosition,
465 gfx::Size(10, 20),
466 false);
[email protected]f2136262013-04-26 21:10:19467 ExecuteCalculateDrawProperties(
468 root.get(), kDeviceScale, kPageScale, scroll_layer);
[email protected]fb661802013-03-25 01:59:32469 expected_transform.MakeIdentity();
470 expected_transform.Translate(
471 MathUtil::Round(kTranslateX * kPageScale * kDeviceScale +
472 sub_layer_screen_position.x()),
473 MathUtil::Round(kTranslateY * kPageScale * kDeviceScale +
474 sub_layer_screen_position.y()));
475 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_transform,
476 sublayer->draw_transform());
[email protected]657b24c2013-03-06 09:01:20477}
478
[email protected]fb661802013-03-25 01:59:32479TEST(LayerTreeHostCommonTest, TransformsForSimpleHierarchy) {
480 gfx::Transform identity_matrix;
481 scoped_refptr<Layer> root = Layer::Create();
482 scoped_refptr<Layer> parent = Layer::Create();
483 scoped_refptr<Layer> child = Layer::Create();
484 scoped_refptr<Layer> grand_child = Layer::Create();
485 root->AddChild(parent);
486 parent->AddChild(child);
487 child->AddChild(grand_child);
[email protected]94f206c12012-08-25 00:09:14488
[email protected]fb661802013-03-25 01:59:32489 // One-time setup of root layer
490 SetLayerPropertiesForTesting(root.get(),
491 identity_matrix,
492 identity_matrix,
493 gfx::PointF(),
494 gfx::PointF(),
495 gfx::Size(1, 2),
496 false);
[email protected]ecc12622012-10-30 20:45:42497
[email protected]fb661802013-03-25 01:59:32498 // Case 1: parent's anchor point should not affect child or grand_child.
499 SetLayerPropertiesForTesting(parent.get(),
500 identity_matrix,
501 identity_matrix,
502 gfx::PointF(0.25f, 0.25f),
503 gfx::PointF(),
504 gfx::Size(10, 12),
505 false);
506 SetLayerPropertiesForTesting(child.get(),
507 identity_matrix,
508 identity_matrix,
509 gfx::PointF(),
510 gfx::PointF(),
511 gfx::Size(16, 18),
512 false);
513 SetLayerPropertiesForTesting(grand_child.get(),
514 identity_matrix,
515 identity_matrix,
516 gfx::PointF(),
517 gfx::PointF(),
518 gfx::Size(76, 78),
519 false);
520 ExecuteCalculateDrawProperties(root.get());
521 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix, child->draw_transform());
522 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
523 child->screen_space_transform());
524 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
525 grand_child->draw_transform());
526 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
527 grand_child->screen_space_transform());
[email protected]94f206c12012-08-25 00:09:14528
[email protected]fb661802013-03-25 01:59:32529 // Case 2: parent's position affects child and grand_child.
530 gfx::Transform parent_position_transform;
531 parent_position_transform.Translate(0.0, 1.2);
532 SetLayerPropertiesForTesting(parent.get(),
533 identity_matrix,
534 identity_matrix,
535 gfx::PointF(0.25f, 0.25f),
536 gfx::PointF(0.f, 1.2f),
537 gfx::Size(10, 12),
538 false);
539 SetLayerPropertiesForTesting(child.get(),
540 identity_matrix,
541 identity_matrix,
542 gfx::PointF(),
543 gfx::PointF(),
544 gfx::Size(16, 18),
545 false);
546 SetLayerPropertiesForTesting(grand_child.get(),
547 identity_matrix,
548 identity_matrix,
549 gfx::PointF(),
550 gfx::PointF(),
551 gfx::Size(76, 78),
552 false);
553 ExecuteCalculateDrawProperties(root.get());
554 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_position_transform,
555 child->draw_transform());
556 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_position_transform,
557 child->screen_space_transform());
558 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_position_transform,
559 grand_child->draw_transform());
560 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_position_transform,
561 grand_child->screen_space_transform());
[email protected]94f206c12012-08-25 00:09:14562
[email protected]fb661802013-03-25 01:59:32563 // Case 3: parent's local transform affects child and grandchild
564 gfx::Transform parent_layer_transform;
565 parent_layer_transform.Scale3d(2.0, 2.0, 1.0);
566 gfx::Transform parent_translation_to_anchor;
567 parent_translation_to_anchor.Translate(2.5, 3.0);
568 gfx::Transform parent_composite_transform =
569 parent_translation_to_anchor * parent_layer_transform *
570 Inverse(parent_translation_to_anchor);
571 SetLayerPropertiesForTesting(parent.get(),
572 parent_layer_transform,
573 identity_matrix,
574 gfx::PointF(0.25f, 0.25f),
575 gfx::PointF(),
576 gfx::Size(10, 12),
577 false);
578 SetLayerPropertiesForTesting(child.get(),
579 identity_matrix,
580 identity_matrix,
581 gfx::PointF(),
582 gfx::PointF(),
583 gfx::Size(16, 18),
584 false);
585 SetLayerPropertiesForTesting(grand_child.get(),
586 identity_matrix,
587 identity_matrix,
588 gfx::PointF(),
589 gfx::PointF(),
590 gfx::Size(76, 78),
591 false);
592 ExecuteCalculateDrawProperties(root.get());
593 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_composite_transform,
594 child->draw_transform());
595 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_composite_transform,
596 child->screen_space_transform());
597 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_composite_transform,
598 grand_child->draw_transform());
599 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_composite_transform,
600 grand_child->screen_space_transform());
[email protected]94f206c12012-08-25 00:09:14601
[email protected]fb661802013-03-25 01:59:32602 // Case 4: parent's sublayer matrix affects child and grandchild scaling is
603 // used here again so that the correct sequence of transforms is properly
604 // tested. Note that preserves3d is false, but the sublayer matrix should
605 // retain its 3D properties when given to child. But then, the child also
606 // does not preserve3D. When it gives its hierarchy to the grand_child, it
607 // should be flattened to 2D.
608 gfx::Transform parent_sublayer_matrix;
609 parent_sublayer_matrix.Scale3d(10.0, 10.0, 3.3);
610 // Sublayer matrix is applied to the anchor point of the parent layer.
611 parent_composite_transform =
612 parent_translation_to_anchor * parent_layer_transform *
613 Inverse(parent_translation_to_anchor) * parent_translation_to_anchor *
614 parent_sublayer_matrix * Inverse(parent_translation_to_anchor);
615 gfx::Transform flattened_composite_transform = parent_composite_transform;
616 flattened_composite_transform.FlattenTo2d();
617 SetLayerPropertiesForTesting(parent.get(),
618 parent_layer_transform,
619 parent_sublayer_matrix,
620 gfx::PointF(0.25f, 0.25f),
621 gfx::PointF(),
622 gfx::Size(10, 12),
623 false);
624 SetLayerPropertiesForTesting(child.get(),
625 identity_matrix,
626 identity_matrix,
627 gfx::PointF(),
628 gfx::PointF(),
629 gfx::Size(16, 18),
630 false);
631 SetLayerPropertiesForTesting(grand_child.get(),
632 identity_matrix,
633 identity_matrix,
634 gfx::PointF(),
635 gfx::PointF(),
636 gfx::Size(76, 78),
637 false);
638 ExecuteCalculateDrawProperties(root.get());
639 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_composite_transform,
640 child->draw_transform());
641 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_composite_transform,
642 child->screen_space_transform());
643 EXPECT_TRANSFORMATION_MATRIX_EQ(flattened_composite_transform,
644 grand_child->draw_transform());
645 EXPECT_TRANSFORMATION_MATRIX_EQ(flattened_composite_transform,
646 grand_child->screen_space_transform());
[email protected]94f206c12012-08-25 00:09:14647
[email protected]fb661802013-03-25 01:59:32648 // Case 5: same as Case 4, except that child does preserve 3D, so the
649 // grand_child should receive the non-flattened composite transform.
650 SetLayerPropertiesForTesting(parent.get(),
651 parent_layer_transform,
652 parent_sublayer_matrix,
653 gfx::PointF(0.25f, 0.25f),
654 gfx::PointF(),
655 gfx::Size(10, 12),
656 false);
657 SetLayerPropertiesForTesting(child.get(),
658 identity_matrix,
659 identity_matrix,
660 gfx::PointF(),
661 gfx::PointF(),
662 gfx::Size(16, 18),
663 true);
664 SetLayerPropertiesForTesting(grand_child.get(),
665 identity_matrix,
666 identity_matrix,
667 gfx::PointF(),
668 gfx::PointF(),
669 gfx::Size(76, 78),
670 false);
671 ExecuteCalculateDrawProperties(root.get());
672 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_composite_transform,
673 child->draw_transform());
674 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_composite_transform,
675 child->screen_space_transform());
676 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_composite_transform,
677 grand_child->draw_transform());
678 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_composite_transform,
679 grand_child->screen_space_transform());
[email protected]94f206c12012-08-25 00:09:14680}
681
[email protected]fb661802013-03-25 01:59:32682TEST(LayerTreeHostCommonTest, TransformsForSingleRenderSurface) {
683 scoped_refptr<Layer> root = Layer::Create();
684 scoped_refptr<Layer> parent = Layer::Create();
685 scoped_refptr<Layer> child = Layer::Create();
686 scoped_refptr<LayerWithForcedDrawsContent> grand_child =
687 make_scoped_refptr(new LayerWithForcedDrawsContent());
688 root->AddChild(parent);
689 parent->AddChild(child);
690 child->AddChild(grand_child);
[email protected]94f206c12012-08-25 00:09:14691
[email protected]fb661802013-03-25 01:59:32692 // One-time setup of root layer
693 gfx::Transform identity_matrix;
694 SetLayerPropertiesForTesting(root.get(),
695 identity_matrix,
696 identity_matrix,
697 gfx::PointF(),
698 gfx::PointF(),
699 gfx::Size(1, 2),
700 false);
[email protected]ecc12622012-10-30 20:45:42701
[email protected]fb661802013-03-25 01:59:32702 // Child is set up so that a new render surface should be created.
703 child->SetOpacity(0.5f);
704 child->SetForceRenderSurface(true);
[email protected]94f206c12012-08-25 00:09:14705
[email protected]fb661802013-03-25 01:59:32706 gfx::Transform parent_layer_transform;
707 parent_layer_transform.Scale3d(1.0, 0.9, 1.0);
708 gfx::Transform parent_translation_to_anchor;
709 parent_translation_to_anchor.Translate(25.0, 30.0);
710 gfx::Transform parent_sublayer_matrix;
711 parent_sublayer_matrix.Scale3d(0.9, 1.0, 3.3);
[email protected]aedf4e52013-01-09 23:24:44712
[email protected]fb661802013-03-25 01:59:32713 gfx::Transform parent_composite_transform =
714 parent_translation_to_anchor * parent_layer_transform *
715 Inverse(parent_translation_to_anchor) * parent_translation_to_anchor *
716 parent_sublayer_matrix * Inverse(parent_translation_to_anchor);
717 gfx::Vector2dF parent_composite_scale =
718 MathUtil::ComputeTransform2dScaleComponents(parent_composite_transform,
719 1.f);
720 gfx::Transform surface_sublayer_transform;
721 surface_sublayer_transform.Scale(parent_composite_scale.x(),
722 parent_composite_scale.y());
723 gfx::Transform surface_sublayer_composite_transform =
724 parent_composite_transform * Inverse(surface_sublayer_transform);
[email protected]94f206c12012-08-25 00:09:14725
[email protected]fb661802013-03-25 01:59:32726 // Child's render surface should not exist yet.
727 ASSERT_FALSE(child->render_surface());
[email protected]94f206c12012-08-25 00:09:14728
[email protected]fb661802013-03-25 01:59:32729 SetLayerPropertiesForTesting(parent.get(),
730 parent_layer_transform,
731 parent_sublayer_matrix,
732 gfx::PointF(0.25f, 0.25f),
733 gfx::PointF(),
734 gfx::Size(100, 120),
735 false);
736 SetLayerPropertiesForTesting(child.get(),
737 identity_matrix,
738 identity_matrix,
739 gfx::PointF(),
740 gfx::PointF(),
741 gfx::Size(16, 18),
742 false);
743 SetLayerPropertiesForTesting(grand_child.get(),
744 identity_matrix,
745 identity_matrix,
746 gfx::PointF(),
747 gfx::PointF(),
748 gfx::Size(8, 10),
749 false);
750 ExecuteCalculateDrawProperties(root.get());
[email protected]94f206c12012-08-25 00:09:14751
[email protected]fb661802013-03-25 01:59:32752 // Render surface should have been created now.
753 ASSERT_TRUE(child->render_surface());
754 ASSERT_EQ(child, child->render_target());
[email protected]94f206c12012-08-25 00:09:14755
[email protected]fb661802013-03-25 01:59:32756 // The child layer's draw transform should refer to its new render surface.
757 // The screen-space transform, however, should still refer to the root.
758 EXPECT_TRANSFORMATION_MATRIX_EQ(surface_sublayer_transform,
759 child->draw_transform());
760 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_composite_transform,
761 child->screen_space_transform());
[email protected]94f206c12012-08-25 00:09:14762
[email protected]fb661802013-03-25 01:59:32763 // Because the grand_child is the only drawable content, the child's render
764 // surface will tighten its bounds to the grand_child. The scale at which the
765 // surface's subtree is drawn must be removed from the composite transform.
766 EXPECT_TRANSFORMATION_MATRIX_EQ(
767 surface_sublayer_composite_transform,
768 child->render_target()->render_surface()->draw_transform());
[email protected]94f206c12012-08-25 00:09:14769
[email protected]fb661802013-03-25 01:59:32770 // The screen space is the same as the target since the child surface draws
771 // into the root.
772 EXPECT_TRANSFORMATION_MATRIX_EQ(
773 surface_sublayer_composite_transform,
774 child->render_target()->render_surface()->screen_space_transform());
[email protected]94f206c12012-08-25 00:09:14775}
776
[email protected]fb661802013-03-25 01:59:32777TEST(LayerTreeHostCommonTest, SublayerTransformWithAnchorPoint) {
778 // crbug.com/157961 - we were always applying the sublayer transform about
779 // the center of the layer, rather than the anchor point.
[email protected]9a3e6b72013-02-12 18:31:21780
[email protected]fb661802013-03-25 01:59:32781 scoped_refptr<Layer> root = Layer::Create();
782 scoped_refptr<Layer> parent = Layer::Create();
783 scoped_refptr<LayerWithForcedDrawsContent> child =
784 make_scoped_refptr(new LayerWithForcedDrawsContent());
785 root->AddChild(parent);
786 parent->AddChild(child);
[email protected]9a3e6b72013-02-12 18:31:21787
[email protected]fb661802013-03-25 01:59:32788 gfx::Transform identity_matrix;
789 gfx::Transform parent_sublayer_matrix;
790 parent_sublayer_matrix.ApplyPerspectiveDepth(2.0);
791 gfx::PointF parent_anchor_point(0.2f, 0.8f);
[email protected]9a3e6b72013-02-12 18:31:21792
[email protected]fb661802013-03-25 01:59:32793 SetLayerPropertiesForTesting(root.get(),
794 identity_matrix,
795 identity_matrix,
796 gfx::PointF(),
797 gfx::PointF(),
798 gfx::Size(1, 2),
799 false);
800 SetLayerPropertiesForTesting(parent.get(),
801 identity_matrix,
802 parent_sublayer_matrix,
803 parent_anchor_point,
804 gfx::PointF(),
805 gfx::Size(100, 100),
806 false);
807 SetLayerPropertiesForTesting(child.get(),
808 identity_matrix,
809 identity_matrix,
810 gfx::PointF(),
811 gfx::PointF(),
812 gfx::Size(10, 10),
813 false);
814 ExecuteCalculateDrawProperties(root.get());
[email protected]9a3e6b72013-02-12 18:31:21815
[email protected]fb661802013-03-25 01:59:32816 gfx::Transform expected_child_draw_transform;
817 expected_child_draw_transform.Translate(20.0, 80.0);
818 expected_child_draw_transform.ApplyPerspectiveDepth(2.0);
819 expected_child_draw_transform.Translate(-20.0, -80.0);
820 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_draw_transform,
821 child->draw_transform());
[email protected]9a3e6b72013-02-12 18:31:21822}
823
[email protected]fb661802013-03-25 01:59:32824TEST(LayerTreeHostCommonTest, SeparateRenderTargetRequirementWithClipping) {
825 scoped_refptr<Layer> root = Layer::Create();
826 scoped_refptr<Layer> parent = Layer::Create();
827 scoped_refptr<Layer> child = Layer::Create();
828 scoped_refptr<Layer> grand_child = make_scoped_refptr(new LayerCanClipSelf());
829 root->AddChild(parent);
830 parent->AddChild(child);
831 child->AddChild(grand_child);
832 parent->SetMasksToBounds(true);
833 child->SetMasksToBounds(true);
[email protected]9a3e6b72013-02-12 18:31:21834
[email protected]fb661802013-03-25 01:59:32835 gfx::Transform identity_matrix;
836 gfx::Transform parent_layer_transform;
837 gfx::Transform parent_sublayer_matrix;
838 gfx::Transform child_layer_matrix;
839
840 // No render surface should exist yet.
841 EXPECT_FALSE(root->render_surface());
842 EXPECT_FALSE(parent->render_surface());
843 EXPECT_FALSE(child->render_surface());
844 EXPECT_FALSE(grand_child->render_surface());
845
846 // One-time setup of root layer
847 parent_layer_transform.Scale3d(1.0, 0.9, 1.0);
848 parent_sublayer_matrix.Scale3d(0.9, 1.0, 3.3);
849 child_layer_matrix.Rotate(20.0);
850
851 SetLayerPropertiesForTesting(root.get(),
852 identity_matrix,
853 identity_matrix,
854 gfx::PointF(),
855 gfx::PointF(),
856 gfx::Size(1, 2),
857 false);
858 SetLayerPropertiesForTesting(parent.get(),
859 parent_layer_transform,
860 parent_sublayer_matrix,
861 gfx::PointF(0.25f, 0.25f),
862 gfx::PointF(),
863 gfx::Size(100, 120),
864 false);
865 SetLayerPropertiesForTesting(child.get(),
866 child_layer_matrix,
867 identity_matrix,
868 gfx::PointF(),
869 gfx::PointF(),
870 gfx::Size(16, 18),
871 false);
872 SetLayerPropertiesForTesting(grand_child.get(),
873 identity_matrix,
874 identity_matrix,
875 gfx::PointF(),
876 gfx::PointF(),
877 gfx::Size(8, 10),
878 false);
879
880 ExecuteCalculateDrawProperties(root.get());
881
882 // Render surfaces should have been created according to clipping rules now
883 // (grandchild can clip self).
884 EXPECT_TRUE(root->render_surface());
885 EXPECT_FALSE(parent->render_surface());
886 EXPECT_FALSE(child->render_surface());
887 EXPECT_FALSE(grand_child->render_surface());
888}
889
890TEST(LayerTreeHostCommonTest, SeparateRenderTargetRequirementWithoutClipping) {
891 scoped_refptr<Layer> root = Layer::Create();
892 scoped_refptr<Layer> parent = Layer::Create();
893 scoped_refptr<Layer> child = Layer::Create();
894 // This layer cannot clip itself, a feature we are testing here.
895 scoped_refptr<Layer> grand_child =
896 make_scoped_refptr(new LayerWithForcedDrawsContent());
897 root->AddChild(parent);
898 parent->AddChild(child);
899 child->AddChild(grand_child);
900 parent->SetMasksToBounds(true);
901 child->SetMasksToBounds(true);
902
903 gfx::Transform identity_matrix;
904 gfx::Transform parent_layer_transform;
905 gfx::Transform parent_sublayer_matrix;
906 gfx::Transform child_layer_matrix;
907
908 // No render surface should exist yet.
909 EXPECT_FALSE(root->render_surface());
910 EXPECT_FALSE(parent->render_surface());
911 EXPECT_FALSE(child->render_surface());
912 EXPECT_FALSE(grand_child->render_surface());
913
914 // One-time setup of root layer
915 parent_layer_transform.Scale3d(1.0, 0.9, 1.0);
916 parent_sublayer_matrix.Scale3d(0.9, 1.0, 3.3);
917 child_layer_matrix.Rotate(20.0);
918
919 SetLayerPropertiesForTesting(root.get(),
920 identity_matrix,
921 identity_matrix,
922 gfx::PointF(),
923 gfx::PointF(),
924 gfx::Size(1, 2),
925 false);
926 SetLayerPropertiesForTesting(parent.get(),
927 parent_layer_transform,
928 parent_sublayer_matrix,
929 gfx::PointF(0.25f, 0.25f),
930 gfx::PointF(),
931 gfx::Size(100, 120),
932 false);
933 SetLayerPropertiesForTesting(child.get(),
934 child_layer_matrix,
935 identity_matrix,
936 gfx::PointF(),
937 gfx::PointF(),
938 gfx::Size(16, 18),
939 false);
940 SetLayerPropertiesForTesting(grand_child.get(),
941 identity_matrix,
942 identity_matrix,
943 gfx::PointF(),
944 gfx::PointF(),
945 gfx::Size(8, 10),
946 false);
947
948 ExecuteCalculateDrawProperties(root.get());
949
950 // Render surfaces should have been created according to clipping rules now
951 // (grandchild can't clip self).
952 EXPECT_TRUE(root->render_surface());
953 EXPECT_FALSE(parent->render_surface());
954 EXPECT_TRUE(child->render_surface());
955 EXPECT_FALSE(grand_child->render_surface());
956}
957
958TEST(LayerTreeHostCommonTest, TransformsForReplica) {
959 scoped_refptr<Layer> root = Layer::Create();
960 scoped_refptr<Layer> parent = Layer::Create();
961 scoped_refptr<Layer> child = Layer::Create();
962 scoped_refptr<Layer> child_replica = Layer::Create();
963 scoped_refptr<LayerWithForcedDrawsContent> grand_child =
964 make_scoped_refptr(new LayerWithForcedDrawsContent());
965 root->AddChild(parent);
966 parent->AddChild(child);
967 child->AddChild(grand_child);
968 child->SetReplicaLayer(child_replica.get());
969
970 // One-time setup of root layer
971 gfx::Transform identity_matrix;
972 SetLayerPropertiesForTesting(root.get(),
973 identity_matrix,
974 identity_matrix,
975 gfx::PointF(),
976 gfx::PointF(),
977 gfx::Size(1, 2),
978 false);
979
980 // Child is set up so that a new render surface should be created.
981 child->SetOpacity(0.5f);
982
983 gfx::Transform parent_layer_transform;
984 parent_layer_transform.Scale3d(2.0, 2.0, 1.0);
985 gfx::Transform parent_translation_to_anchor;
986 parent_translation_to_anchor.Translate(2.5, 3.0);
987 gfx::Transform parent_sublayer_matrix;
988 parent_sublayer_matrix.Scale3d(10.0, 10.0, 3.3);
989 gfx::Transform parent_composite_transform =
990 parent_translation_to_anchor * parent_layer_transform *
991 Inverse(parent_translation_to_anchor) * parent_translation_to_anchor *
992 parent_sublayer_matrix * Inverse(parent_translation_to_anchor);
993 gfx::Transform replica_layer_transform;
994 replica_layer_transform.Scale3d(3.0, 3.0, 1.0);
995 gfx::Vector2dF parent_composite_scale =
996 MathUtil::ComputeTransform2dScaleComponents(parent_composite_transform,
997 1.f);
998 gfx::Transform surface_sublayer_transform;
999 surface_sublayer_transform.Scale(parent_composite_scale.x(),
1000 parent_composite_scale.y());
1001 gfx::Transform replica_composite_transform =
1002 parent_composite_transform * replica_layer_transform *
1003 Inverse(surface_sublayer_transform);
1004
1005 // Child's render surface should not exist yet.
1006 ASSERT_FALSE(child->render_surface());
1007
1008 SetLayerPropertiesForTesting(parent.get(),
1009 parent_layer_transform,
1010 parent_sublayer_matrix,
1011 gfx::PointF(0.25f, 0.25f),
1012 gfx::PointF(),
1013 gfx::Size(10, 12),
1014 false);
1015 SetLayerPropertiesForTesting(child.get(),
1016 identity_matrix,
1017 identity_matrix,
1018 gfx::PointF(),
1019 gfx::PointF(),
1020 gfx::Size(16, 18),
1021 false);
1022 SetLayerPropertiesForTesting(grand_child.get(),
1023 identity_matrix,
1024 identity_matrix,
1025 gfx::PointF(),
1026 gfx::PointF(-0.5f, -0.5f),
1027 gfx::Size(1, 1),
1028 false);
1029 SetLayerPropertiesForTesting(child_replica.get(),
1030 replica_layer_transform,
1031 identity_matrix,
1032 gfx::PointF(),
1033 gfx::PointF(),
1034 gfx::Size(),
1035 false);
1036 ExecuteCalculateDrawProperties(root.get());
1037
1038 // Render surface should have been created now.
1039 ASSERT_TRUE(child->render_surface());
1040 ASSERT_EQ(child, child->render_target());
1041
1042 EXPECT_TRANSFORMATION_MATRIX_EQ(
1043 replica_composite_transform,
1044 child->render_target()->render_surface()->replica_draw_transform());
1045 EXPECT_TRANSFORMATION_MATRIX_EQ(replica_composite_transform,
1046 child->render_target()->render_surface()
1047 ->replica_screen_space_transform());
1048}
1049
1050TEST(LayerTreeHostCommonTest, TransformsForRenderSurfaceHierarchy) {
1051 // This test creates a more complex tree and verifies it all at once. This
1052 // covers the following cases:
1053 // - layers that are described w.r.t. a render surface: should have draw
1054 // transforms described w.r.t. that surface
1055 // - A render surface described w.r.t. an ancestor render surface: should
1056 // have a draw transform described w.r.t. that ancestor surface
1057 // - Replicas of a render surface are described w.r.t. the replica's
1058 // transform around its anchor, along with the surface itself.
1059 // - Sanity check on recursion: verify transforms of layers described w.r.t.
1060 // a render surface that is described w.r.t. an ancestor render surface.
1061 // - verifying that each layer has a reference to the correct render surface
1062 // and render target values.
1063
1064 scoped_refptr<Layer> root = Layer::Create();
1065 scoped_refptr<Layer> parent = Layer::Create();
1066 scoped_refptr<Layer> render_surface1 = Layer::Create();
1067 scoped_refptr<Layer> render_surface2 = Layer::Create();
1068 scoped_refptr<Layer> child_of_root = Layer::Create();
1069 scoped_refptr<Layer> child_of_rs1 = Layer::Create();
1070 scoped_refptr<Layer> child_of_rs2 = Layer::Create();
1071 scoped_refptr<Layer> replica_of_rs1 = Layer::Create();
1072 scoped_refptr<Layer> replica_of_rs2 = Layer::Create();
1073 scoped_refptr<Layer> grand_child_of_root = Layer::Create();
1074 scoped_refptr<LayerWithForcedDrawsContent> grand_child_of_rs1 =
1075 make_scoped_refptr(new LayerWithForcedDrawsContent());
1076 scoped_refptr<LayerWithForcedDrawsContent> grand_child_of_rs2 =
1077 make_scoped_refptr(new LayerWithForcedDrawsContent());
1078 root->AddChild(parent);
1079 parent->AddChild(render_surface1);
1080 parent->AddChild(child_of_root);
1081 render_surface1->AddChild(child_of_rs1);
1082 render_surface1->AddChild(render_surface2);
1083 render_surface2->AddChild(child_of_rs2);
1084 child_of_root->AddChild(grand_child_of_root);
1085 child_of_rs1->AddChild(grand_child_of_rs1);
1086 child_of_rs2->AddChild(grand_child_of_rs2);
1087 render_surface1->SetReplicaLayer(replica_of_rs1.get());
1088 render_surface2->SetReplicaLayer(replica_of_rs2.get());
1089
1090 // In combination with descendant draws content, opacity != 1 forces the layer
1091 // to have a new render surface.
1092 render_surface1->SetOpacity(0.5f);
1093 render_surface2->SetOpacity(0.33f);
1094
1095 // One-time setup of root layer
1096 gfx::Transform identity_matrix;
1097 SetLayerPropertiesForTesting(root.get(),
1098 identity_matrix,
1099 identity_matrix,
1100 gfx::PointF(),
1101 gfx::PointF(),
1102 gfx::Size(1, 2),
1103 false);
1104
1105 // All layers in the tree are initialized with an anchor at .25 and a size of
1106 // (10,10). matrix "A" is the composite layer transform used in all layers,
1107 // centered about the anchor point. matrix "B" is the sublayer transform used
1108 // in all layers, centered about the center position of the layer. matrix "R"
1109 // is the composite replica transform used in all replica layers.
1110 //
1111 // x component tests that layer_transform and sublayer_transform are done in
1112 // the right order (translation and scale are noncommutative). y component
1113 // has a translation by 1 for every ancestor, which indicates the "depth" of
1114 // the layer in the hierarchy.
1115 gfx::Transform translation_to_anchor;
1116 translation_to_anchor.Translate(2.5, 0.0);
1117 gfx::Transform layer_transform;
1118 layer_transform.Translate(1.0, 1.0);
1119 gfx::Transform sublayer_transform;
1120 sublayer_transform.Scale3d(10.0, 1.0, 1.0);
1121 gfx::Transform replica_layer_transform;
1122 replica_layer_transform.Scale3d(-2.0, 5.0, 1.0);
1123
1124 gfx::Transform A =
1125 translation_to_anchor * layer_transform * Inverse(translation_to_anchor);
1126 gfx::Transform B = translation_to_anchor * sublayer_transform *
1127 Inverse(translation_to_anchor);
1128 gfx::Transform R = A * translation_to_anchor * replica_layer_transform *
1129 Inverse(translation_to_anchor);
1130
1131 gfx::Vector2dF surface1_parent_transform_scale =
1132 MathUtil::ComputeTransform2dScaleComponents(A * B, 1.f);
1133 gfx::Transform surface1_sublayer_transform;
1134 surface1_sublayer_transform.Scale(surface1_parent_transform_scale.x(),
1135 surface1_parent_transform_scale.y());
1136
1137 // SS1 = transform given to the subtree of render_surface1
1138 gfx::Transform SS1 = surface1_sublayer_transform;
1139 // S1 = transform to move from render_surface1 pixels to the layer space of
1140 // the owning layer
1141 gfx::Transform S1 = Inverse(surface1_sublayer_transform);
1142
1143 gfx::Vector2dF surface2_parent_transform_scale =
1144 MathUtil::ComputeTransform2dScaleComponents(SS1 * A * B, 1.f);
1145 gfx::Transform surface2_sublayer_transform;
1146 surface2_sublayer_transform.Scale(surface2_parent_transform_scale.x(),
1147 surface2_parent_transform_scale.y());
1148
1149 // SS2 = transform given to the subtree of render_surface2
1150 gfx::Transform SS2 = surface2_sublayer_transform;
1151 // S2 = transform to move from render_surface2 pixels to the layer space of
1152 // the owning layer
1153 gfx::Transform S2 = Inverse(surface2_sublayer_transform);
1154
1155 SetLayerPropertiesForTesting(parent.get(),
1156 layer_transform,
1157 sublayer_transform,
1158 gfx::PointF(0.25f, 0.f),
1159 gfx::PointF(),
1160 gfx::Size(10, 10),
1161 false);
1162 SetLayerPropertiesForTesting(render_surface1.get(),
1163 layer_transform,
1164 sublayer_transform,
1165 gfx::PointF(0.25f, 0.f),
1166 gfx::PointF(),
1167 gfx::Size(10, 10),
1168 false);
1169 SetLayerPropertiesForTesting(render_surface2.get(),
1170 layer_transform,
1171 sublayer_transform,
1172 gfx::PointF(0.25f, 0.f),
1173 gfx::PointF(),
1174 gfx::Size(10, 10),
1175 false);
1176 SetLayerPropertiesForTesting(child_of_root.get(),
1177 layer_transform,
1178 sublayer_transform,
1179 gfx::PointF(0.25f, 0.f),
1180 gfx::PointF(),
1181 gfx::Size(10, 10),
1182 false);
1183 SetLayerPropertiesForTesting(child_of_rs1.get(),
1184 layer_transform,
1185 sublayer_transform,
1186 gfx::PointF(0.25f, 0.f),
1187 gfx::PointF(),
1188 gfx::Size(10, 10),
1189 false);
1190 SetLayerPropertiesForTesting(child_of_rs2.get(),
1191 layer_transform,
1192 sublayer_transform,
1193 gfx::PointF(0.25f, 0.f),
1194 gfx::PointF(),
1195 gfx::Size(10, 10),
1196 false);
1197 SetLayerPropertiesForTesting(grand_child_of_root.get(),
1198 layer_transform,
1199 sublayer_transform,
1200 gfx::PointF(0.25f, 0.f),
1201 gfx::PointF(),
1202 gfx::Size(10, 10),
1203 false);
1204 SetLayerPropertiesForTesting(grand_child_of_rs1.get(),
1205 layer_transform,
1206 sublayer_transform,
1207 gfx::PointF(0.25f, 0.f),
1208 gfx::PointF(),
1209 gfx::Size(10, 10),
1210 false);
1211 SetLayerPropertiesForTesting(grand_child_of_rs2.get(),
1212 layer_transform,
1213 sublayer_transform,
1214 gfx::PointF(0.25f, 0.f),
1215 gfx::PointF(),
1216 gfx::Size(10, 10),
1217 false);
1218 SetLayerPropertiesForTesting(replica_of_rs1.get(),
1219 replica_layer_transform,
1220 sublayer_transform,
1221 gfx::PointF(0.25f, 0.f),
1222 gfx::PointF(),
1223 gfx::Size(),
1224 false);
1225 SetLayerPropertiesForTesting(replica_of_rs2.get(),
1226 replica_layer_transform,
1227 sublayer_transform,
1228 gfx::PointF(0.25f, 0.f),
1229 gfx::PointF(),
1230 gfx::Size(),
1231 false);
1232
1233 ExecuteCalculateDrawProperties(root.get());
1234
1235 // Only layers that are associated with render surfaces should have an actual
1236 // RenderSurface() value.
1237 ASSERT_TRUE(root->render_surface());
1238 ASSERT_FALSE(child_of_root->render_surface());
1239 ASSERT_FALSE(grand_child_of_root->render_surface());
1240
1241 ASSERT_TRUE(render_surface1->render_surface());
1242 ASSERT_FALSE(child_of_rs1->render_surface());
1243 ASSERT_FALSE(grand_child_of_rs1->render_surface());
1244
1245 ASSERT_TRUE(render_surface2->render_surface());
1246 ASSERT_FALSE(child_of_rs2->render_surface());
1247 ASSERT_FALSE(grand_child_of_rs2->render_surface());
1248
1249 // Verify all render target accessors
1250 EXPECT_EQ(root, parent->render_target());
1251 EXPECT_EQ(root, child_of_root->render_target());
1252 EXPECT_EQ(root, grand_child_of_root->render_target());
1253
1254 EXPECT_EQ(render_surface1, render_surface1->render_target());
1255 EXPECT_EQ(render_surface1, child_of_rs1->render_target());
1256 EXPECT_EQ(render_surface1, grand_child_of_rs1->render_target());
1257
1258 EXPECT_EQ(render_surface2, render_surface2->render_target());
1259 EXPECT_EQ(render_surface2, child_of_rs2->render_target());
1260 EXPECT_EQ(render_surface2, grand_child_of_rs2->render_target());
1261
1262 // Verify layer draw transforms note that draw transforms are described with
1263 // respect to the nearest ancestor render surface but screen space transforms
1264 // are described with respect to the root.
1265 EXPECT_TRANSFORMATION_MATRIX_EQ(A, parent->draw_transform());
1266 EXPECT_TRANSFORMATION_MATRIX_EQ(A * B * A, child_of_root->draw_transform());
1267 EXPECT_TRANSFORMATION_MATRIX_EQ(A * B * A * B * A,
1268 grand_child_of_root->draw_transform());
1269
1270 EXPECT_TRANSFORMATION_MATRIX_EQ(SS1, render_surface1->draw_transform());
1271 EXPECT_TRANSFORMATION_MATRIX_EQ(SS1 * B * A, child_of_rs1->draw_transform());
1272 EXPECT_TRANSFORMATION_MATRIX_EQ(SS1 * B * A * B * A,
1273 grand_child_of_rs1->draw_transform());
1274
1275 EXPECT_TRANSFORMATION_MATRIX_EQ(SS2, render_surface2->draw_transform());
1276 EXPECT_TRANSFORMATION_MATRIX_EQ(SS2 * B * A, child_of_rs2->draw_transform());
1277 EXPECT_TRANSFORMATION_MATRIX_EQ(SS2 * B * A * B * A,
1278 grand_child_of_rs2->draw_transform());
1279
1280 // Verify layer screen-space transforms
1281 //
1282 EXPECT_TRANSFORMATION_MATRIX_EQ(A, parent->screen_space_transform());
1283 EXPECT_TRANSFORMATION_MATRIX_EQ(A * B * A,
1284 child_of_root->screen_space_transform());
1285 EXPECT_TRANSFORMATION_MATRIX_EQ(
1286 A * B * A * B * A, grand_child_of_root->screen_space_transform());
1287
1288 EXPECT_TRANSFORMATION_MATRIX_EQ(A * B * A,
1289 render_surface1->screen_space_transform());
1290 EXPECT_TRANSFORMATION_MATRIX_EQ(A * B * A * B * A,
1291 child_of_rs1->screen_space_transform());
1292 EXPECT_TRANSFORMATION_MATRIX_EQ(A * B * A * B * A * B * A,
1293 grand_child_of_rs1->screen_space_transform());
1294
1295 EXPECT_TRANSFORMATION_MATRIX_EQ(A * B * A * B * A,
1296 render_surface2->screen_space_transform());
1297 EXPECT_TRANSFORMATION_MATRIX_EQ(A * B * A * B * A * B * A,
1298 child_of_rs2->screen_space_transform());
1299 EXPECT_TRANSFORMATION_MATRIX_EQ(A * B * A * B * A * B * A * B * A,
1300 grand_child_of_rs2->screen_space_transform());
1301
1302 // Verify render surface transforms.
1303 //
1304 // Draw transform of render surface 1 is described with respect to root.
1305 EXPECT_TRANSFORMATION_MATRIX_EQ(
1306 A * B * A * S1, render_surface1->render_surface()->draw_transform());
1307 EXPECT_TRANSFORMATION_MATRIX_EQ(
1308 A * B * R * S1,
1309 render_surface1->render_surface()->replica_draw_transform());
1310 EXPECT_TRANSFORMATION_MATRIX_EQ(
1311 A * B * A * S1,
1312 render_surface1->render_surface()->screen_space_transform());
1313 EXPECT_TRANSFORMATION_MATRIX_EQ(
1314 A * B * R * S1,
1315 render_surface1->render_surface()->replica_screen_space_transform());
1316 // Draw transform of render surface 2 is described with respect to render
1317 // surface 1.
1318 EXPECT_TRANSFORMATION_MATRIX_EQ(
1319 SS1 * B * A * S2, render_surface2->render_surface()->draw_transform());
1320 EXPECT_TRANSFORMATION_MATRIX_EQ(
1321 SS1 * B * R * S2,
1322 render_surface2->render_surface()->replica_draw_transform());
1323 EXPECT_TRANSFORMATION_MATRIX_EQ(
1324 A * B * A * B * A * S2,
1325 render_surface2->render_surface()->screen_space_transform());
1326 EXPECT_TRANSFORMATION_MATRIX_EQ(
1327 A * B * A * B * R * S2,
1328 render_surface2->render_surface()->replica_screen_space_transform());
1329
1330 // Sanity check. If these fail there is probably a bug in the test itself. It
1331 // is expected that we correctly set up transforms so that the y-component of
1332 // the screen-space transform encodes the "depth" of the layer in the tree.
1333 EXPECT_FLOAT_EQ(1.0,
1334 parent->screen_space_transform().matrix().getDouble(1, 3));
1335 EXPECT_FLOAT_EQ(
1336 2.0, child_of_root->screen_space_transform().matrix().getDouble(1, 3));
1337 EXPECT_FLOAT_EQ(
1338 3.0,
1339 grand_child_of_root->screen_space_transform().matrix().getDouble(1, 3));
1340
1341 EXPECT_FLOAT_EQ(
1342 2.0, render_surface1->screen_space_transform().matrix().getDouble(1, 3));
1343 EXPECT_FLOAT_EQ(
1344 3.0, child_of_rs1->screen_space_transform().matrix().getDouble(1, 3));
1345 EXPECT_FLOAT_EQ(
1346 4.0,
1347 grand_child_of_rs1->screen_space_transform().matrix().getDouble(1, 3));
1348
1349 EXPECT_FLOAT_EQ(
1350 3.0, render_surface2->screen_space_transform().matrix().getDouble(1, 3));
1351 EXPECT_FLOAT_EQ(
1352 4.0, child_of_rs2->screen_space_transform().matrix().getDouble(1, 3));
1353 EXPECT_FLOAT_EQ(
1354 5.0,
1355 grand_child_of_rs2->screen_space_transform().matrix().getDouble(1, 3));
1356}
1357
1358TEST(LayerTreeHostCommonTest, TransformsForFlatteningLayer) {
1359 // For layers that flatten their subtree, there should be an orthographic
1360 // projection (for x and y values) in the middle of the transform sequence.
1361 // Note that the way the code is currently implemented, it is not expected to
1362 // use a canonical orthographic projection.
1363
1364 scoped_refptr<Layer> root = Layer::Create();
1365 scoped_refptr<Layer> child = Layer::Create();
1366 scoped_refptr<LayerWithForcedDrawsContent> grand_child =
1367 make_scoped_refptr(new LayerWithForcedDrawsContent());
1368
1369 gfx::Transform rotation_about_y_axis;
1370 rotation_about_y_axis.RotateAboutYAxis(30.0);
1371
1372 const gfx::Transform identity_matrix;
1373 SetLayerPropertiesForTesting(root.get(),
1374 identity_matrix,
1375 identity_matrix,
1376 gfx::PointF(),
1377 gfx::PointF(),
1378 gfx::Size(100, 100),
1379 false);
1380 SetLayerPropertiesForTesting(child.get(),
1381 rotation_about_y_axis,
1382 identity_matrix,
1383 gfx::PointF(),
1384 gfx::PointF(),
1385 gfx::Size(10, 10),
1386 false);
1387 SetLayerPropertiesForTesting(grand_child.get(),
1388 rotation_about_y_axis,
1389 identity_matrix,
1390 gfx::PointF(),
1391 gfx::PointF(),
1392 gfx::Size(10, 10),
1393 false);
1394
1395 root->AddChild(child);
1396 child->AddChild(grand_child);
1397 child->SetForceRenderSurface(true);
1398
1399 // No layers in this test should preserve 3d.
1400 ASSERT_FALSE(root->preserves_3d());
1401 ASSERT_FALSE(child->preserves_3d());
1402 ASSERT_FALSE(grand_child->preserves_3d());
1403
1404 gfx::Transform expected_child_draw_transform = rotation_about_y_axis;
1405 gfx::Transform expected_child_screen_space_transform = rotation_about_y_axis;
1406 gfx::Transform expected_grand_child_draw_transform =
1407 rotation_about_y_axis; // draws onto child's render surface
1408 gfx::Transform flattened_rotation_about_y = rotation_about_y_axis;
1409 flattened_rotation_about_y.FlattenTo2d();
1410 gfx::Transform expected_grand_child_screen_space_transform =
1411 flattened_rotation_about_y * rotation_about_y_axis;
1412
1413 ExecuteCalculateDrawProperties(root.get());
1414
1415 // The child's draw transform should have been taken by its surface.
1416 ASSERT_TRUE(child->render_surface());
1417 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_draw_transform,
1418 child->render_surface()->draw_transform());
1419 EXPECT_TRANSFORMATION_MATRIX_EQ(
1420 expected_child_screen_space_transform,
1421 child->render_surface()->screen_space_transform());
1422 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix, child->draw_transform());
1423 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_screen_space_transform,
1424 child->screen_space_transform());
1425 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_grand_child_draw_transform,
1426 grand_child->draw_transform());
1427 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_grand_child_screen_space_transform,
1428 grand_child->screen_space_transform());
1429}
1430
1431TEST(LayerTreeHostCommonTest, TransformsForDegenerateIntermediateLayer) {
1432 // A layer that is empty in one axis, but not the other, was accidentally
1433 // skipping a necessary translation. Without that translation, the coordinate
1434 // space of the layer's draw transform is incorrect.
1435 //
1436 // Normally this isn't a problem, because the layer wouldn't be drawn anyway,
1437 // but if that layer becomes a render surface, then its draw transform is
1438 // implicitly inherited by the rest of the subtree, which then is positioned
1439 // incorrectly as a result.
1440
1441 scoped_refptr<Layer> root = Layer::Create();
1442 scoped_refptr<Layer> child = Layer::Create();
1443 scoped_refptr<LayerWithForcedDrawsContent> grand_child =
1444 make_scoped_refptr(new LayerWithForcedDrawsContent());
1445
1446 // The child height is zero, but has non-zero width that should be accounted
1447 // for while computing draw transforms.
1448 const gfx::Transform identity_matrix;
1449 SetLayerPropertiesForTesting(root.get(),
1450 identity_matrix,
1451 identity_matrix,
1452 gfx::PointF(),
1453 gfx::PointF(),
1454 gfx::Size(100, 100),
1455 false);
1456 SetLayerPropertiesForTesting(child.get(),
1457 identity_matrix,
1458 identity_matrix,
1459 gfx::PointF(),
1460 gfx::PointF(),
1461 gfx::Size(10, 0),
1462 false);
1463 SetLayerPropertiesForTesting(grand_child.get(),
1464 identity_matrix,
1465 identity_matrix,
1466 gfx::PointF(),
1467 gfx::PointF(),
1468 gfx::Size(10, 10),
1469 false);
1470
1471 root->AddChild(child);
1472 child->AddChild(grand_child);
1473 child->SetForceRenderSurface(true);
1474
1475 ExecuteCalculateDrawProperties(root.get());
1476
1477 ASSERT_TRUE(child->render_surface());
1478 // This is the real test, the rest are sanity checks.
1479 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
1480 child->render_surface()->draw_transform());
1481 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix, child->draw_transform());
1482 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
1483 grand_child->draw_transform());
1484}
1485
1486TEST(LayerTreeHostCommonTest,
1487 RenderSurfaceListForRenderSurfaceWithClippedLayer) {
1488 scoped_refptr<Layer> parent = Layer::Create();
1489 scoped_refptr<Layer> render_surface1 = Layer::Create();
1490 scoped_refptr<LayerWithForcedDrawsContent> child =
1491 make_scoped_refptr(new LayerWithForcedDrawsContent());
1492
1493 const gfx::Transform identity_matrix;
1494 SetLayerPropertiesForTesting(parent.get(),
1495 identity_matrix,
1496 identity_matrix,
1497 gfx::PointF(),
1498 gfx::PointF(),
1499 gfx::Size(10, 10),
1500 false);
1501 SetLayerPropertiesForTesting(render_surface1.get(),
1502 identity_matrix,
1503 identity_matrix,
1504 gfx::PointF(),
1505 gfx::PointF(),
1506 gfx::Size(10, 10),
1507 false);
1508 SetLayerPropertiesForTesting(child.get(),
1509 identity_matrix,
1510 identity_matrix,
1511 gfx::PointF(),
1512 gfx::PointF(30.f, 30.f),
1513 gfx::Size(10, 10),
1514 false);
1515
1516 parent->AddChild(render_surface1);
1517 parent->SetMasksToBounds(true);
1518 render_surface1->AddChild(child);
1519 render_surface1->SetForceRenderSurface(true);
1520
[email protected]50761e92013-03-29 20:51:281521 LayerList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:321522 int dummy_max_texture_size = 512;
1523 LayerTreeHostCommon::CalculateDrawProperties(parent.get(),
1524 parent->bounds(),
1525 1.f,
1526 1.f,
[email protected]f2136262013-04-26 21:10:191527 NULL,
[email protected]fb661802013-03-25 01:59:321528 dummy_max_texture_size,
1529 false,
1530 &render_surface_layer_list);
1531
1532 // The child layer's content is entirely outside the parent's clip rect, so
1533 // the intermediate render surface should not be listed here, even if it was
1534 // forced to be created. Render surfaces without children or visible content
1535 // are unexpected at draw time (e.g. we might try to create a content texture
1536 // of size 0).
1537 ASSERT_TRUE(parent->render_surface());
1538 ASSERT_FALSE(render_surface1->render_surface());
1539 EXPECT_EQ(1U, render_surface_layer_list.size());
1540}
1541
1542TEST(LayerTreeHostCommonTest, RenderSurfaceListForTransparentChild) {
1543 scoped_refptr<Layer> parent = Layer::Create();
1544 scoped_refptr<Layer> render_surface1 = Layer::Create();
1545 scoped_refptr<LayerWithForcedDrawsContent> child =
1546 make_scoped_refptr(new LayerWithForcedDrawsContent());
1547
1548 const gfx::Transform identity_matrix;
1549 SetLayerPropertiesForTesting(render_surface1.get(),
1550 identity_matrix,
1551 identity_matrix,
1552 gfx::PointF(),
1553 gfx::PointF(),
1554 gfx::Size(10, 10),
1555 false);
1556 SetLayerPropertiesForTesting(child.get(),
1557 identity_matrix,
1558 identity_matrix,
1559 gfx::PointF(),
1560 gfx::PointF(),
1561 gfx::Size(10, 10),
1562 false);
1563
1564 parent->AddChild(render_surface1);
1565 render_surface1->AddChild(child);
1566 render_surface1->SetForceRenderSurface(true);
1567 render_surface1->SetOpacity(0.f);
1568
[email protected]50761e92013-03-29 20:51:281569 LayerList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:321570 int dummy_max_texture_size = 512;
1571 LayerTreeHostCommon::CalculateDrawProperties(parent.get(),
1572 parent->bounds(),
1573 1.f,
1574 1.f,
[email protected]f2136262013-04-26 21:10:191575 NULL,
[email protected]fb661802013-03-25 01:59:321576 dummy_max_texture_size,
1577 false,
1578 &render_surface_layer_list);
1579
1580 // Since the layer is transparent, render_surface1->render_surface() should
1581 // not have gotten added anywhere. Also, the drawable content rect should not
1582 // have been extended by the children.
1583 ASSERT_TRUE(parent->render_surface());
1584 EXPECT_EQ(0U, parent->render_surface()->layer_list().size());
1585 EXPECT_EQ(1U, render_surface_layer_list.size());
1586 EXPECT_EQ(parent->id(), render_surface_layer_list[0]->id());
1587 EXPECT_EQ(gfx::Rect(), parent->drawable_content_rect());
1588}
1589
1590TEST(LayerTreeHostCommonTest, ForceRenderSurface) {
1591 scoped_refptr<Layer> parent = Layer::Create();
1592 scoped_refptr<Layer> render_surface1 = Layer::Create();
1593 scoped_refptr<LayerWithForcedDrawsContent> child =
1594 make_scoped_refptr(new LayerWithForcedDrawsContent());
1595 render_surface1->SetForceRenderSurface(true);
1596
1597 const gfx::Transform identity_matrix;
1598 SetLayerPropertiesForTesting(parent.get(),
1599 identity_matrix,
1600 identity_matrix,
1601 gfx::PointF(),
1602 gfx::PointF(),
1603 gfx::Size(10, 10),
1604 false);
1605 SetLayerPropertiesForTesting(render_surface1.get(),
1606 identity_matrix,
1607 identity_matrix,
1608 gfx::PointF(),
1609 gfx::PointF(),
1610 gfx::Size(10, 10),
1611 false);
1612 SetLayerPropertiesForTesting(child.get(),
1613 identity_matrix,
1614 identity_matrix,
1615 gfx::PointF(),
1616 gfx::PointF(),
1617 gfx::Size(10, 10),
1618 false);
1619
1620 parent->AddChild(render_surface1);
1621 render_surface1->AddChild(child);
1622
1623 // Sanity check before the actual test
1624 EXPECT_FALSE(parent->render_surface());
1625 EXPECT_FALSE(render_surface1->render_surface());
1626
[email protected]50761e92013-03-29 20:51:281627 LayerList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:321628 int dummy_max_texture_size = 512;
1629 LayerTreeHostCommon::CalculateDrawProperties(parent.get(),
1630 parent->bounds(),
1631 1.f,
1632 1.f,
[email protected]f2136262013-04-26 21:10:191633 NULL,
[email protected]fb661802013-03-25 01:59:321634 dummy_max_texture_size,
1635 false,
1636 &render_surface_layer_list);
1637
1638 // The root layer always creates a render surface
1639 EXPECT_TRUE(parent->render_surface());
1640 EXPECT_TRUE(render_surface1->render_surface());
1641 EXPECT_EQ(2U, render_surface_layer_list.size());
1642
1643 render_surface_layer_list.clear();
1644 render_surface1->SetForceRenderSurface(false);
1645 LayerTreeHostCommon::CalculateDrawProperties(parent.get(),
1646 parent->bounds(),
1647 1.f,
1648 1.f,
[email protected]f2136262013-04-26 21:10:191649 NULL,
[email protected]fb661802013-03-25 01:59:321650 dummy_max_texture_size,
1651 false,
1652 &render_surface_layer_list);
1653 EXPECT_TRUE(parent->render_surface());
1654 EXPECT_FALSE(render_surface1->render_surface());
1655 EXPECT_EQ(1U, render_surface_layer_list.size());
1656}
1657
[email protected]fb661802013-03-25 01:59:321658TEST(LayerTreeHostCommonTest, ClipRectCullsRenderSurfaces) {
1659 // The entire subtree of layers that are outside the clip rect should be
1660 // culled away, and should not affect the render_surface_layer_list.
1661 //
1662 // The test tree is set up as follows:
1663 // - all layers except the leaf_nodes are forced to be a new render surface
1664 // that have something to draw.
1665 // - parent is a large container layer.
1666 // - child has masksToBounds=true to cause clipping.
1667 // - grand_child is positioned outside of the child's bounds
1668 // - great_grand_child is also kept outside child's bounds.
1669 //
1670 // In this configuration, grand_child and great_grand_child are completely
1671 // outside the clip rect, and they should never get scheduled on the list of
1672 // render surfaces.
1673 //
1674
1675 const gfx::Transform identity_matrix;
1676 scoped_refptr<Layer> parent = Layer::Create();
1677 scoped_refptr<Layer> child = Layer::Create();
1678 scoped_refptr<Layer> grand_child = Layer::Create();
1679 scoped_refptr<Layer> great_grand_child = Layer::Create();
1680 scoped_refptr<LayerWithForcedDrawsContent> leaf_node1 =
1681 make_scoped_refptr(new LayerWithForcedDrawsContent());
1682 scoped_refptr<LayerWithForcedDrawsContent> leaf_node2 =
1683 make_scoped_refptr(new LayerWithForcedDrawsContent());
1684 parent->AddChild(child);
1685 child->AddChild(grand_child);
1686 grand_child->AddChild(great_grand_child);
1687
1688 // leaf_node1 ensures that parent and child are kept on the
1689 // render_surface_layer_list, even though grand_child and great_grand_child
1690 // should be clipped.
1691 child->AddChild(leaf_node1);
1692 great_grand_child->AddChild(leaf_node2);
1693
1694 SetLayerPropertiesForTesting(parent.get(),
1695 identity_matrix,
1696 identity_matrix,
1697 gfx::PointF(),
1698 gfx::PointF(),
1699 gfx::Size(500, 500),
1700 false);
1701 SetLayerPropertiesForTesting(child.get(),
1702 identity_matrix,
1703 identity_matrix,
1704 gfx::PointF(),
1705 gfx::PointF(),
1706 gfx::Size(20, 20),
1707 false);
1708 SetLayerPropertiesForTesting(grand_child.get(),
1709 identity_matrix,
1710 identity_matrix,
1711 gfx::PointF(),
1712 gfx::PointF(45.f, 45.f),
1713 gfx::Size(10, 10),
1714 false);
1715 SetLayerPropertiesForTesting(great_grand_child.get(),
1716 identity_matrix,
1717 identity_matrix,
1718 gfx::PointF(),
1719 gfx::PointF(),
1720 gfx::Size(10, 10),
1721 false);
1722 SetLayerPropertiesForTesting(leaf_node1.get(),
1723 identity_matrix,
1724 identity_matrix,
1725 gfx::PointF(),
1726 gfx::PointF(),
1727 gfx::Size(500, 500),
1728 false);
1729 SetLayerPropertiesForTesting(leaf_node2.get(),
1730 identity_matrix,
1731 identity_matrix,
1732 gfx::PointF(),
1733 gfx::PointF(),
1734 gfx::Size(20, 20),
1735 false);
1736
1737 child->SetMasksToBounds(true);
1738 child->SetOpacity(0.4f);
1739 child->SetForceRenderSurface(true);
1740 grand_child->SetOpacity(0.5f);
1741 great_grand_child->SetOpacity(0.4f);
1742
[email protected]50761e92013-03-29 20:51:281743 LayerList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:321744 int dummy_max_texture_size = 512;
1745 LayerTreeHostCommon::CalculateDrawProperties(parent.get(),
1746 parent->bounds(),
1747 1.f,
1748 1.f,
[email protected]f2136262013-04-26 21:10:191749 NULL,
[email protected]fb661802013-03-25 01:59:321750 dummy_max_texture_size,
1751 false,
1752 &render_surface_layer_list);
1753
1754 ASSERT_EQ(2U, render_surface_layer_list.size());
1755 EXPECT_EQ(parent->id(), render_surface_layer_list[0]->id());
1756 EXPECT_EQ(child->id(), render_surface_layer_list[1]->id());
1757}
1758
1759TEST(LayerTreeHostCommonTest, ClipRectCullsSurfaceWithoutVisibleContent) {
1760 // When a render surface has a clip rect, it is used to clip the content rect
1761 // of the surface. When the render surface is animating its transforms, then
1762 // the content rect's position in the clip rect is not defined on the main
1763 // thread, and its content rect should not be clipped.
1764
1765 // The test tree is set up as follows:
1766 // - parent is a container layer that masksToBounds=true to cause clipping.
1767 // - child is a render surface, which has a clip rect set to the bounds of
1768 // the parent.
1769 // - grand_child is a render surface, and the only visible content in child.
1770 // It is positioned outside of the clip rect from parent.
1771
1772 // In this configuration, grand_child should be outside the clipped
1773 // content rect of the child, making grand_child not appear in the
1774 // render_surface_layer_list. However, when we place an animation on the
1775 // child, this clipping should be avoided and we should keep the grand_child
1776 // in the render_surface_layer_list.
1777
1778 const gfx::Transform identity_matrix;
1779 scoped_refptr<Layer> parent = Layer::Create();
1780 scoped_refptr<Layer> child = Layer::Create();
1781 scoped_refptr<Layer> grand_child = Layer::Create();
1782 scoped_refptr<LayerWithForcedDrawsContent> leaf_node =
1783 make_scoped_refptr(new LayerWithForcedDrawsContent());
1784 parent->AddChild(child);
1785 child->AddChild(grand_child);
1786 grand_child->AddChild(leaf_node);
1787
1788 SetLayerPropertiesForTesting(parent.get(),
1789 identity_matrix,
1790 identity_matrix,
1791 gfx::PointF(),
1792 gfx::PointF(),
1793 gfx::Size(100, 100),
1794 false);
1795 SetLayerPropertiesForTesting(child.get(),
1796 identity_matrix,
1797 identity_matrix,
1798 gfx::PointF(),
1799 gfx::PointF(),
1800 gfx::Size(20, 20),
1801 false);
1802 SetLayerPropertiesForTesting(grand_child.get(),
1803 identity_matrix,
1804 identity_matrix,
1805 gfx::PointF(),
1806 gfx::PointF(200.f, 200.f),
1807 gfx::Size(10, 10),
1808 false);
1809 SetLayerPropertiesForTesting(leaf_node.get(),
1810 identity_matrix,
1811 identity_matrix,
1812 gfx::PointF(),
1813 gfx::PointF(),
1814 gfx::Size(10, 10),
1815 false);
1816
1817 parent->SetMasksToBounds(true);
1818 child->SetOpacity(0.4f);
1819 child->SetForceRenderSurface(true);
1820 grand_child->SetOpacity(0.4f);
1821 grand_child->SetForceRenderSurface(true);
1822
[email protected]50761e92013-03-29 20:51:281823 LayerList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:321824 int dummy_max_texture_size = 512;
1825 LayerTreeHostCommon::CalculateDrawProperties(parent.get(),
1826 parent->bounds(),
1827 1.f,
1828 1.f,
[email protected]f2136262013-04-26 21:10:191829 NULL,
[email protected]fb661802013-03-25 01:59:321830 dummy_max_texture_size,
1831 false,
1832 &render_surface_layer_list);
1833
1834 // Without an animation, we should cull child and grand_child from the
1835 // render_surface_layer_list.
1836 ASSERT_EQ(1U, render_surface_layer_list.size());
1837 EXPECT_EQ(parent->id(), render_surface_layer_list[0]->id());
1838
1839 // Now put an animating transform on child.
1840 AddAnimatedTransformToController(
1841 child->layer_animation_controller(), 10.0, 30, 0);
1842
1843 parent->ClearRenderSurface();
1844 child->ClearRenderSurface();
1845 grand_child->ClearRenderSurface();
1846 render_surface_layer_list.clear();
1847
1848 LayerTreeHostCommon::CalculateDrawProperties(parent.get(),
1849 parent->bounds(),
1850 1.f,
1851 1.f,
[email protected]f2136262013-04-26 21:10:191852 NULL,
[email protected]fb661802013-03-25 01:59:321853 dummy_max_texture_size,
1854 false,
1855 &render_surface_layer_list);
1856
1857 // With an animating transform, we should keep child and grand_child in the
1858 // render_surface_layer_list.
1859 ASSERT_EQ(3U, render_surface_layer_list.size());
1860 EXPECT_EQ(parent->id(), render_surface_layer_list[0]->id());
1861 EXPECT_EQ(child->id(), render_surface_layer_list[1]->id());
1862 EXPECT_EQ(grand_child->id(), render_surface_layer_list[2]->id());
1863}
1864
1865TEST(LayerTreeHostCommonTest, IsClippedIsSetCorrectly) {
1866 // Layer's IsClipped() property is set to true when:
1867 // - the layer clips its subtree, e.g. masks to bounds,
1868 // - the layer is clipped by an ancestor that contributes to the same
1869 // render target,
1870 // - a surface is clipped by an ancestor that contributes to the same
1871 // render target.
1872 //
1873 // In particular, for a layer that owns a render surface:
1874 // - the render surface inherits any clip from ancestors, and does NOT
1875 // pass that clipped status to the layer itself.
1876 // - but if the layer itself masks to bounds, it is considered clipped
1877 // and propagates the clip to the subtree.
1878
1879 const gfx::Transform identity_matrix;
1880 scoped_refptr<Layer> root = Layer::Create();
1881 scoped_refptr<Layer> parent = Layer::Create();
1882 scoped_refptr<Layer> child1 = Layer::Create();
1883 scoped_refptr<Layer> child2 = Layer::Create();
1884 scoped_refptr<Layer> grand_child = Layer::Create();
1885 scoped_refptr<LayerWithForcedDrawsContent> leaf_node1 =
1886 make_scoped_refptr(new LayerWithForcedDrawsContent());
1887 scoped_refptr<LayerWithForcedDrawsContent> leaf_node2 =
1888 make_scoped_refptr(new LayerWithForcedDrawsContent());
1889 root->AddChild(parent);
1890 parent->AddChild(child1);
1891 parent->AddChild(child2);
1892 child1->AddChild(grand_child);
1893 child2->AddChild(leaf_node2);
1894 grand_child->AddChild(leaf_node1);
1895
1896 child2->SetForceRenderSurface(true);
1897
1898 SetLayerPropertiesForTesting(root.get(),
1899 identity_matrix,
1900 identity_matrix,
1901 gfx::PointF(),
1902 gfx::PointF(),
1903 gfx::Size(100, 100),
1904 false);
1905 SetLayerPropertiesForTesting(parent.get(),
1906 identity_matrix,
1907 identity_matrix,
1908 gfx::PointF(),
1909 gfx::PointF(),
1910 gfx::Size(100, 100),
1911 false);
1912 SetLayerPropertiesForTesting(child1.get(),
1913 identity_matrix,
1914 identity_matrix,
1915 gfx::PointF(),
1916 gfx::PointF(),
1917 gfx::Size(100, 100),
1918 false);
1919 SetLayerPropertiesForTesting(child2.get(),
1920 identity_matrix,
1921 identity_matrix,
1922 gfx::PointF(),
1923 gfx::PointF(),
1924 gfx::Size(100, 100),
1925 false);
1926 SetLayerPropertiesForTesting(grand_child.get(),
1927 identity_matrix,
1928 identity_matrix,
1929 gfx::PointF(),
1930 gfx::PointF(),
1931 gfx::Size(100, 100),
1932 false);
1933 SetLayerPropertiesForTesting(leaf_node1.get(),
1934 identity_matrix,
1935 identity_matrix,
1936 gfx::PointF(),
1937 gfx::PointF(),
1938 gfx::Size(100, 100),
1939 false);
1940 SetLayerPropertiesForTesting(leaf_node2.get(),
1941 identity_matrix,
1942 identity_matrix,
1943 gfx::PointF(),
1944 gfx::PointF(),
1945 gfx::Size(100, 100),
1946 false);
1947
1948 // Case 1: nothing is clipped except the root render surface.
[email protected]50761e92013-03-29 20:51:281949 LayerList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:321950 int dummy_max_texture_size = 512;
1951 LayerTreeHostCommon::CalculateDrawProperties(root.get(),
1952 parent->bounds(),
1953 1.f,
1954 1.f,
[email protected]f2136262013-04-26 21:10:191955 NULL,
[email protected]fb661802013-03-25 01:59:321956 dummy_max_texture_size,
1957 false,
1958 &render_surface_layer_list);
1959
1960 ASSERT_TRUE(root->render_surface());
1961 ASSERT_TRUE(child2->render_surface());
1962
1963 EXPECT_FALSE(root->is_clipped());
1964 EXPECT_TRUE(root->render_surface()->is_clipped());
1965 EXPECT_FALSE(parent->is_clipped());
1966 EXPECT_FALSE(child1->is_clipped());
1967 EXPECT_FALSE(child2->is_clipped());
1968 EXPECT_FALSE(child2->render_surface()->is_clipped());
1969 EXPECT_FALSE(grand_child->is_clipped());
1970 EXPECT_FALSE(leaf_node1->is_clipped());
1971 EXPECT_FALSE(leaf_node2->is_clipped());
1972
1973 // Case 2: parent masksToBounds, so the parent, child1, and child2's
1974 // surface are clipped. But layers that contribute to child2's surface are
1975 // not clipped explicitly because child2's surface already accounts for
1976 // that clip.
1977 render_surface_layer_list.clear();
1978 parent->SetMasksToBounds(true);
1979 LayerTreeHostCommon::CalculateDrawProperties(root.get(),
1980 parent->bounds(),
1981 1.f,
1982 1.f,
[email protected]f2136262013-04-26 21:10:191983 NULL,
[email protected]fb661802013-03-25 01:59:321984 dummy_max_texture_size,
1985 false,
1986 &render_surface_layer_list);
1987
1988 ASSERT_TRUE(root->render_surface());
1989 ASSERT_TRUE(child2->render_surface());
1990
1991 EXPECT_FALSE(root->is_clipped());
1992 EXPECT_TRUE(root->render_surface()->is_clipped());
1993 EXPECT_TRUE(parent->is_clipped());
1994 EXPECT_TRUE(child1->is_clipped());
1995 EXPECT_FALSE(child2->is_clipped());
1996 EXPECT_TRUE(child2->render_surface()->is_clipped());
1997 EXPECT_TRUE(grand_child->is_clipped());
1998 EXPECT_TRUE(leaf_node1->is_clipped());
1999 EXPECT_FALSE(leaf_node2->is_clipped());
2000
2001 // Case 3: child2 masksToBounds. The layer and subtree are clipped, and
2002 // child2's render surface is not clipped.
2003 render_surface_layer_list.clear();
2004 parent->SetMasksToBounds(false);
2005 child2->SetMasksToBounds(true);
2006 LayerTreeHostCommon::CalculateDrawProperties(root.get(),
2007 parent->bounds(),
2008 1.f,
2009 1.f,
[email protected]f2136262013-04-26 21:10:192010 NULL,
[email protected]fb661802013-03-25 01:59:322011 dummy_max_texture_size,
2012 false,
2013 &render_surface_layer_list);
2014
2015 ASSERT_TRUE(root->render_surface());
2016 ASSERT_TRUE(child2->render_surface());
2017
2018 EXPECT_FALSE(root->is_clipped());
2019 EXPECT_TRUE(root->render_surface()->is_clipped());
2020 EXPECT_FALSE(parent->is_clipped());
2021 EXPECT_FALSE(child1->is_clipped());
2022 EXPECT_TRUE(child2->is_clipped());
2023 EXPECT_FALSE(child2->render_surface()->is_clipped());
2024 EXPECT_FALSE(grand_child->is_clipped());
2025 EXPECT_FALSE(leaf_node1->is_clipped());
2026 EXPECT_TRUE(leaf_node2->is_clipped());
2027}
2028
2029TEST(LayerTreeHostCommonTest, drawable_content_rectForLayers) {
2030 // Verify that layers get the appropriate DrawableContentRect when their
2031 // parent masksToBounds is true.
2032 //
2033 // grand_child1 - completely inside the region; DrawableContentRect should
2034 // be the layer rect expressed in target space.
2035 // grand_child2 - partially clipped but NOT masksToBounds; the clip rect
2036 // will be the intersection of layer bounds and the mask region.
2037 // grand_child3 - partially clipped and masksToBounds; the
2038 // DrawableContentRect will still be the intersection of layer bounds and
2039 // the mask region.
2040 // grand_child4 - outside parent's clip rect; the DrawableContentRect should
2041 // be empty.
2042 //
2043
2044 const gfx::Transform identity_matrix;
2045 scoped_refptr<Layer> parent = Layer::Create();
2046 scoped_refptr<Layer> child = Layer::Create();
2047 scoped_refptr<Layer> grand_child1 = Layer::Create();
2048 scoped_refptr<Layer> grand_child2 = Layer::Create();
2049 scoped_refptr<Layer> grand_child3 = Layer::Create();
2050 scoped_refptr<Layer> grand_child4 = Layer::Create();
2051
2052 parent->AddChild(child);
2053 child->AddChild(grand_child1);
2054 child->AddChild(grand_child2);
2055 child->AddChild(grand_child3);
2056 child->AddChild(grand_child4);
2057
2058 SetLayerPropertiesForTesting(parent.get(),
2059 identity_matrix,
2060 identity_matrix,
2061 gfx::PointF(),
2062 gfx::PointF(),
2063 gfx::Size(500, 500),
2064 false);
2065 SetLayerPropertiesForTesting(child.get(),
2066 identity_matrix,
2067 identity_matrix,
2068 gfx::PointF(),
2069 gfx::PointF(),
2070 gfx::Size(20, 20),
2071 false);
2072 SetLayerPropertiesForTesting(grand_child1.get(),
2073 identity_matrix,
2074 identity_matrix,
2075 gfx::PointF(),
2076 gfx::PointF(5.f, 5.f),
2077 gfx::Size(10, 10),
2078 false);
2079 SetLayerPropertiesForTesting(grand_child2.get(),
2080 identity_matrix,
2081 identity_matrix,
2082 gfx::PointF(),
2083 gfx::PointF(15.f, 15.f),
2084 gfx::Size(10, 10),
2085 false);
2086 SetLayerPropertiesForTesting(grand_child3.get(),
2087 identity_matrix,
2088 identity_matrix,
2089 gfx::PointF(),
2090 gfx::PointF(15.f, 15.f),
2091 gfx::Size(10, 10),
2092 false);
2093 SetLayerPropertiesForTesting(grand_child4.get(),
2094 identity_matrix,
2095 identity_matrix,
2096 gfx::PointF(),
2097 gfx::PointF(45.f, 45.f),
2098 gfx::Size(10, 10),
2099 false);
2100
2101 child->SetMasksToBounds(true);
2102 grand_child3->SetMasksToBounds(true);
2103
2104 // Force everyone to be a render surface.
2105 child->SetOpacity(0.4f);
2106 grand_child1->SetOpacity(0.5f);
2107 grand_child2->SetOpacity(0.5f);
2108 grand_child3->SetOpacity(0.5f);
2109 grand_child4->SetOpacity(0.5f);
2110
[email protected]50761e92013-03-29 20:51:282111 LayerList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:322112 int dummy_max_texture_size = 512;
2113 LayerTreeHostCommon::CalculateDrawProperties(parent.get(),
2114 parent->bounds(),
2115 1.f,
2116 1.f,
[email protected]f2136262013-04-26 21:10:192117 NULL,
[email protected]fb661802013-03-25 01:59:322118 dummy_max_texture_size,
2119 false,
2120 &render_surface_layer_list);
2121
[email protected]2c7c6702013-03-26 03:14:052122 EXPECT_RECT_EQ(gfx::Rect(5, 5, 10, 10),
[email protected]fb661802013-03-25 01:59:322123 grand_child1->drawable_content_rect());
[email protected]2c7c6702013-03-26 03:14:052124 EXPECT_RECT_EQ(gfx::Rect(15, 15, 5, 5),
[email protected]fb661802013-03-25 01:59:322125 grand_child3->drawable_content_rect());
[email protected]2c7c6702013-03-26 03:14:052126 EXPECT_RECT_EQ(gfx::Rect(15, 15, 5, 5),
[email protected]fb661802013-03-25 01:59:322127 grand_child3->drawable_content_rect());
2128 EXPECT_TRUE(grand_child4->drawable_content_rect().IsEmpty());
2129}
2130
2131TEST(LayerTreeHostCommonTest, ClipRectIsPropagatedCorrectlyToSurfaces) {
2132 // Verify that render surfaces (and their layers) get the appropriate
2133 // clip rects when their parent masksToBounds is true.
2134 //
2135 // Layers that own render surfaces (at least for now) do not inherit any
2136 // clipping; instead the surface will enforce the clip for the entire subtree.
2137 // They may still have a clip rect of their own layer bounds, however, if
2138 // masksToBounds was true.
2139 const gfx::Transform identity_matrix;
2140 scoped_refptr<Layer> parent = Layer::Create();
2141 scoped_refptr<Layer> child = Layer::Create();
2142 scoped_refptr<Layer> grand_child1 = Layer::Create();
2143 scoped_refptr<Layer> grand_child2 = Layer::Create();
2144 scoped_refptr<Layer> grand_child3 = Layer::Create();
2145 scoped_refptr<Layer> grand_child4 = Layer::Create();
2146 scoped_refptr<LayerWithForcedDrawsContent> leaf_node1 =
2147 make_scoped_refptr(new LayerWithForcedDrawsContent());
2148 scoped_refptr<LayerWithForcedDrawsContent> leaf_node2 =
2149 make_scoped_refptr(new LayerWithForcedDrawsContent());
2150 scoped_refptr<LayerWithForcedDrawsContent> leaf_node3 =
2151 make_scoped_refptr(new LayerWithForcedDrawsContent());
2152 scoped_refptr<LayerWithForcedDrawsContent> leaf_node4 =
2153 make_scoped_refptr(new LayerWithForcedDrawsContent());
2154
2155 parent->AddChild(child);
2156 child->AddChild(grand_child1);
2157 child->AddChild(grand_child2);
2158 child->AddChild(grand_child3);
2159 child->AddChild(grand_child4);
2160
2161 // the leaf nodes ensure that these grand_children become render surfaces for
2162 // this test.
2163 grand_child1->AddChild(leaf_node1);
2164 grand_child2->AddChild(leaf_node2);
2165 grand_child3->AddChild(leaf_node3);
2166 grand_child4->AddChild(leaf_node4);
2167
2168 SetLayerPropertiesForTesting(parent.get(),
2169 identity_matrix,
2170 identity_matrix,
2171 gfx::PointF(),
2172 gfx::PointF(),
2173 gfx::Size(500, 500),
2174 false);
2175 SetLayerPropertiesForTesting(child.get(),
2176 identity_matrix,
2177 identity_matrix,
2178 gfx::PointF(),
2179 gfx::PointF(),
2180 gfx::Size(20, 20),
2181 false);
2182 SetLayerPropertiesForTesting(grand_child1.get(),
2183 identity_matrix,
2184 identity_matrix,
2185 gfx::PointF(),
2186 gfx::PointF(5.f, 5.f),
2187 gfx::Size(10, 10),
2188 false);
2189 SetLayerPropertiesForTesting(grand_child2.get(),
2190 identity_matrix,
2191 identity_matrix,
2192 gfx::PointF(),
2193 gfx::PointF(15.f, 15.f),
2194 gfx::Size(10, 10),
2195 false);
2196 SetLayerPropertiesForTesting(grand_child3.get(),
2197 identity_matrix,
2198 identity_matrix,
2199 gfx::PointF(),
2200 gfx::PointF(15.f, 15.f),
2201 gfx::Size(10, 10),
2202 false);
2203 SetLayerPropertiesForTesting(grand_child4.get(),
2204 identity_matrix,
2205 identity_matrix,
2206 gfx::PointF(),
2207 gfx::PointF(45.f, 45.f),
2208 gfx::Size(10, 10),
2209 false);
2210 SetLayerPropertiesForTesting(leaf_node1.get(),
2211 identity_matrix,
2212 identity_matrix,
2213 gfx::PointF(),
2214 gfx::PointF(),
2215 gfx::Size(10, 10),
2216 false);
2217 SetLayerPropertiesForTesting(leaf_node2.get(),
2218 identity_matrix,
2219 identity_matrix,
2220 gfx::PointF(),
2221 gfx::PointF(),
2222 gfx::Size(10, 10),
2223 false);
2224 SetLayerPropertiesForTesting(leaf_node3.get(),
2225 identity_matrix,
2226 identity_matrix,
2227 gfx::PointF(),
2228 gfx::PointF(),
2229 gfx::Size(10, 10),
2230 false);
2231 SetLayerPropertiesForTesting(leaf_node4.get(),
2232 identity_matrix,
2233 identity_matrix,
2234 gfx::PointF(),
2235 gfx::PointF(),
2236 gfx::Size(10, 10),
2237 false);
2238
2239 child->SetMasksToBounds(true);
2240 grand_child3->SetMasksToBounds(true);
2241 grand_child4->SetMasksToBounds(true);
2242
2243 // Force everyone to be a render surface.
2244 child->SetOpacity(0.4f);
2245 child->SetForceRenderSurface(true);
2246 grand_child1->SetOpacity(0.5f);
2247 grand_child1->SetForceRenderSurface(true);
2248 grand_child2->SetOpacity(0.5f);
2249 grand_child2->SetForceRenderSurface(true);
2250 grand_child3->SetOpacity(0.5f);
2251 grand_child3->SetForceRenderSurface(true);
2252 grand_child4->SetOpacity(0.5f);
2253 grand_child4->SetForceRenderSurface(true);
2254
[email protected]50761e92013-03-29 20:51:282255 LayerList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:322256 int dummy_max_texture_size = 512;
2257 LayerTreeHostCommon::CalculateDrawProperties(parent.get(),
2258 parent->bounds(),
2259 1.f,
2260 1.f,
[email protected]f2136262013-04-26 21:10:192261 NULL,
[email protected]fb661802013-03-25 01:59:322262 dummy_max_texture_size,
2263 false,
2264 &render_surface_layer_list);
2265
2266 ASSERT_TRUE(grand_child1->render_surface());
2267 ASSERT_TRUE(grand_child2->render_surface());
2268 ASSERT_TRUE(grand_child3->render_surface());
2269 // Because grand_child4 is entirely clipped, it is expected to not have a
2270 // render surface.
2271 EXPECT_FALSE(grand_child4->render_surface());
2272
2273 // Surfaces are clipped by their parent, but un-affected by the owning layer's
2274 // masksToBounds.
[email protected]2c7c6702013-03-26 03:14:052275 EXPECT_RECT_EQ(gfx::Rect(0, 0, 20, 20),
[email protected]fb661802013-03-25 01:59:322276 grand_child1->render_surface()->clip_rect());
[email protected]2c7c6702013-03-26 03:14:052277 EXPECT_RECT_EQ(gfx::Rect(0, 0, 20, 20),
[email protected]fb661802013-03-25 01:59:322278 grand_child2->render_surface()->clip_rect());
[email protected]2c7c6702013-03-26 03:14:052279 EXPECT_RECT_EQ(gfx::Rect(0, 0, 20, 20),
[email protected]fb661802013-03-25 01:59:322280 grand_child3->render_surface()->clip_rect());
2281}
2282
2283TEST(LayerTreeHostCommonTest, AnimationsForRenderSurfaceHierarchy) {
2284 scoped_refptr<Layer> parent = Layer::Create();
2285 scoped_refptr<Layer> render_surface1 = Layer::Create();
2286 scoped_refptr<Layer> render_surface2 = Layer::Create();
2287 scoped_refptr<Layer> child_of_root = Layer::Create();
2288 scoped_refptr<Layer> child_of_rs1 = Layer::Create();
2289 scoped_refptr<Layer> child_of_rs2 = Layer::Create();
2290 scoped_refptr<Layer> grand_child_of_root = Layer::Create();
2291 scoped_refptr<LayerWithForcedDrawsContent> grand_child_of_rs1 =
2292 make_scoped_refptr(new LayerWithForcedDrawsContent());
2293 scoped_refptr<LayerWithForcedDrawsContent> grand_child_of_rs2 =
2294 make_scoped_refptr(new LayerWithForcedDrawsContent());
2295 parent->AddChild(render_surface1);
2296 parent->AddChild(child_of_root);
2297 render_surface1->AddChild(child_of_rs1);
2298 render_surface1->AddChild(render_surface2);
2299 render_surface2->AddChild(child_of_rs2);
2300 child_of_root->AddChild(grand_child_of_root);
2301 child_of_rs1->AddChild(grand_child_of_rs1);
2302 child_of_rs2->AddChild(grand_child_of_rs2);
2303
2304 // Make our render surfaces.
2305 render_surface1->SetForceRenderSurface(true);
2306 render_surface2->SetForceRenderSurface(true);
2307
2308 gfx::Transform layer_transform;
2309 layer_transform.Translate(1.0, 1.0);
2310 gfx::Transform sublayer_transform;
2311 sublayer_transform.Scale3d(10.0, 1.0, 1.0);
2312
2313 SetLayerPropertiesForTesting(parent.get(),
2314 layer_transform,
2315 sublayer_transform,
2316 gfx::PointF(0.25f, 0.f),
2317 gfx::PointF(2.5f, 0.f),
2318 gfx::Size(10, 10),
2319 false);
2320 SetLayerPropertiesForTesting(render_surface1.get(),
2321 layer_transform,
2322 sublayer_transform,
2323 gfx::PointF(0.25f, 0.f),
2324 gfx::PointF(2.5f, 0.f),
2325 gfx::Size(10, 10),
2326 false);
2327 SetLayerPropertiesForTesting(render_surface2.get(),
2328 layer_transform,
2329 sublayer_transform,
2330 gfx::PointF(0.25f, 0.f),
2331 gfx::PointF(2.5f, 0.f),
2332 gfx::Size(10, 10),
2333 false);
2334 SetLayerPropertiesForTesting(child_of_root.get(),
2335 layer_transform,
2336 sublayer_transform,
2337 gfx::PointF(0.25f, 0.f),
2338 gfx::PointF(2.5f, 0.f),
2339 gfx::Size(10, 10),
2340 false);
2341 SetLayerPropertiesForTesting(child_of_rs1.get(),
2342 layer_transform,
2343 sublayer_transform,
2344 gfx::PointF(0.25f, 0.f),
2345 gfx::PointF(2.5f, 0.f),
2346 gfx::Size(10, 10),
2347 false);
2348 SetLayerPropertiesForTesting(child_of_rs2.get(),
2349 layer_transform,
2350 sublayer_transform,
2351 gfx::PointF(0.25f, 0.f),
2352 gfx::PointF(2.5f, 0.f),
2353 gfx::Size(10, 10),
2354 false);
2355 SetLayerPropertiesForTesting(grand_child_of_root.get(),
2356 layer_transform,
2357 sublayer_transform,
2358 gfx::PointF(0.25f, 0.f),
2359 gfx::PointF(2.5f, 0.f),
2360 gfx::Size(10, 10),
2361 false);
2362 SetLayerPropertiesForTesting(grand_child_of_rs1.get(),
2363 layer_transform,
2364 sublayer_transform,
2365 gfx::PointF(0.25f, 0.f),
2366 gfx::PointF(2.5f, 0.f),
2367 gfx::Size(10, 10),
2368 false);
2369 SetLayerPropertiesForTesting(grand_child_of_rs2.get(),
2370 layer_transform,
2371 sublayer_transform,
2372 gfx::PointF(0.25f, 0.f),
2373 gfx::PointF(2.5f, 0.f),
2374 gfx::Size(10, 10),
2375 false);
2376
2377 // Put an animated opacity on the render surface.
2378 AddOpacityTransitionToController(
2379 render_surface1->layer_animation_controller(), 10.0, 1.f, 0.f, false);
2380
2381 // Also put an animated opacity on a layer without descendants.
2382 AddOpacityTransitionToController(
2383 grand_child_of_root->layer_animation_controller(), 10.0, 1.f, 0.f, false);
2384
2385 // Put a transform animation on the render surface.
2386 AddAnimatedTransformToController(
2387 render_surface2->layer_animation_controller(), 10.0, 30, 0);
2388
2389 // Also put transform animations on grand_child_of_root, and
2390 // grand_child_of_rs2
2391 AddAnimatedTransformToController(
2392 grand_child_of_root->layer_animation_controller(), 10.0, 30, 0);
2393 AddAnimatedTransformToController(
2394 grand_child_of_rs2->layer_animation_controller(), 10.0, 30, 0);
2395
2396 ExecuteCalculateDrawProperties(parent.get());
2397
2398 // Only layers that are associated with render surfaces should have an actual
2399 // RenderSurface() value.
2400 ASSERT_TRUE(parent->render_surface());
2401 ASSERT_FALSE(child_of_root->render_surface());
2402 ASSERT_FALSE(grand_child_of_root->render_surface());
2403
2404 ASSERT_TRUE(render_surface1->render_surface());
2405 ASSERT_FALSE(child_of_rs1->render_surface());
2406 ASSERT_FALSE(grand_child_of_rs1->render_surface());
2407
2408 ASSERT_TRUE(render_surface2->render_surface());
2409 ASSERT_FALSE(child_of_rs2->render_surface());
2410 ASSERT_FALSE(grand_child_of_rs2->render_surface());
2411
2412 // Verify all render target accessors
2413 EXPECT_EQ(parent, parent->render_target());
2414 EXPECT_EQ(parent, child_of_root->render_target());
2415 EXPECT_EQ(parent, grand_child_of_root->render_target());
2416
2417 EXPECT_EQ(render_surface1, render_surface1->render_target());
2418 EXPECT_EQ(render_surface1, child_of_rs1->render_target());
2419 EXPECT_EQ(render_surface1, grand_child_of_rs1->render_target());
2420
2421 EXPECT_EQ(render_surface2, render_surface2->render_target());
2422 EXPECT_EQ(render_surface2, child_of_rs2->render_target());
2423 EXPECT_EQ(render_surface2, grand_child_of_rs2->render_target());
2424
2425 // Verify draw_opacity_is_animating values
2426 EXPECT_FALSE(parent->draw_opacity_is_animating());
2427 EXPECT_FALSE(child_of_root->draw_opacity_is_animating());
2428 EXPECT_TRUE(grand_child_of_root->draw_opacity_is_animating());
2429 EXPECT_FALSE(render_surface1->draw_opacity_is_animating());
2430 EXPECT_TRUE(render_surface1->render_surface()->draw_opacity_is_animating());
2431 EXPECT_FALSE(child_of_rs1->draw_opacity_is_animating());
2432 EXPECT_FALSE(grand_child_of_rs1->draw_opacity_is_animating());
2433 EXPECT_FALSE(render_surface2->draw_opacity_is_animating());
2434 EXPECT_FALSE(render_surface2->render_surface()->draw_opacity_is_animating());
2435 EXPECT_FALSE(child_of_rs2->draw_opacity_is_animating());
2436 EXPECT_FALSE(grand_child_of_rs2->draw_opacity_is_animating());
2437
2438 // Verify draw_transform_is_animating values
2439 EXPECT_FALSE(parent->draw_transform_is_animating());
2440 EXPECT_FALSE(child_of_root->draw_transform_is_animating());
2441 EXPECT_TRUE(grand_child_of_root->draw_transform_is_animating());
2442 EXPECT_FALSE(render_surface1->draw_transform_is_animating());
2443 EXPECT_FALSE(render_surface1->render_surface()
2444 ->target_surface_transforms_are_animating());
2445 EXPECT_FALSE(child_of_rs1->draw_transform_is_animating());
2446 EXPECT_FALSE(grand_child_of_rs1->draw_transform_is_animating());
2447 EXPECT_FALSE(render_surface2->draw_transform_is_animating());
2448 EXPECT_TRUE(render_surface2->render_surface()
2449 ->target_surface_transforms_are_animating());
2450 EXPECT_FALSE(child_of_rs2->draw_transform_is_animating());
2451 EXPECT_TRUE(grand_child_of_rs2->draw_transform_is_animating());
2452
2453 // Verify screen_space_transform_is_animating values
2454 EXPECT_FALSE(parent->screen_space_transform_is_animating());
2455 EXPECT_FALSE(child_of_root->screen_space_transform_is_animating());
2456 EXPECT_TRUE(grand_child_of_root->screen_space_transform_is_animating());
2457 EXPECT_FALSE(render_surface1->screen_space_transform_is_animating());
2458 EXPECT_FALSE(render_surface1->render_surface()
2459 ->screen_space_transforms_are_animating());
2460 EXPECT_FALSE(child_of_rs1->screen_space_transform_is_animating());
2461 EXPECT_FALSE(grand_child_of_rs1->screen_space_transform_is_animating());
2462 EXPECT_TRUE(render_surface2->screen_space_transform_is_animating());
2463 EXPECT_TRUE(render_surface2->render_surface()
2464 ->screen_space_transforms_are_animating());
2465 EXPECT_TRUE(child_of_rs2->screen_space_transform_is_animating());
2466 EXPECT_TRUE(grand_child_of_rs2->screen_space_transform_is_animating());
2467
2468 // Sanity check. If these fail there is probably a bug in the test itself.
2469 // It is expected that we correctly set up transforms so that the y-component
2470 // of the screen-space transform encodes the "depth" of the layer in the tree.
2471 EXPECT_FLOAT_EQ(1.0,
2472 parent->screen_space_transform().matrix().getDouble(1, 3));
2473 EXPECT_FLOAT_EQ(
2474 2.0, child_of_root->screen_space_transform().matrix().getDouble(1, 3));
2475 EXPECT_FLOAT_EQ(
2476 3.0,
2477 grand_child_of_root->screen_space_transform().matrix().getDouble(1, 3));
2478
2479 EXPECT_FLOAT_EQ(
2480 2.0, render_surface1->screen_space_transform().matrix().getDouble(1, 3));
2481 EXPECT_FLOAT_EQ(
2482 3.0, child_of_rs1->screen_space_transform().matrix().getDouble(1, 3));
2483 EXPECT_FLOAT_EQ(
2484 4.0,
2485 grand_child_of_rs1->screen_space_transform().matrix().getDouble(1, 3));
2486
2487 EXPECT_FLOAT_EQ(
2488 3.0, render_surface2->screen_space_transform().matrix().getDouble(1, 3));
2489 EXPECT_FLOAT_EQ(
2490 4.0, child_of_rs2->screen_space_transform().matrix().getDouble(1, 3));
2491 EXPECT_FLOAT_EQ(
2492 5.0,
2493 grand_child_of_rs2->screen_space_transform().matrix().getDouble(1, 3));
2494}
2495
2496TEST(LayerTreeHostCommonTest, VisibleRectForIdentityTransform) {
2497 // Test the calculateVisibleRect() function works correctly for identity
2498 // transforms.
2499
[email protected]2c7c6702013-03-26 03:14:052500 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
[email protected]fb661802013-03-25 01:59:322501 gfx::Transform layer_to_surface_transform;
2502
2503 // Case 1: Layer is contained within the surface.
2504 gfx::Rect layer_content_rect =
[email protected]2c7c6702013-03-26 03:14:052505 gfx::Rect(10, 10, 30, 30);
2506 gfx::Rect expected = gfx::Rect(10, 10, 30, 30);
[email protected]fb661802013-03-25 01:59:322507 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2508 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2509 EXPECT_RECT_EQ(expected, actual);
2510
2511 // Case 2: Layer is outside the surface rect.
[email protected]2c7c6702013-03-26 03:14:052512 layer_content_rect = gfx::Rect(120, 120, 30, 30);
[email protected]fb661802013-03-25 01:59:322513 actual = LayerTreeHostCommon::CalculateVisibleRect(
2514 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2515 EXPECT_TRUE(actual.IsEmpty());
2516
2517 // Case 3: Layer is partially overlapping the surface rect.
[email protected]2c7c6702013-03-26 03:14:052518 layer_content_rect = gfx::Rect(80, 80, 30, 30);
2519 expected = gfx::Rect(80, 80, 20, 20);
[email protected]fb661802013-03-25 01:59:322520 actual = LayerTreeHostCommon::CalculateVisibleRect(
2521 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2522 EXPECT_RECT_EQ(expected, actual);
2523}
2524
2525TEST(LayerTreeHostCommonTest, VisibleRectForTranslations) {
2526 // Test the calculateVisibleRect() function works correctly for scaling
2527 // transforms.
2528
[email protected]2c7c6702013-03-26 03:14:052529 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
2530 gfx::Rect layer_content_rect = gfx::Rect(0, 0, 30, 30);
[email protected]fb661802013-03-25 01:59:322531 gfx::Transform layer_to_surface_transform;
2532
2533 // Case 1: Layer is contained within the surface.
2534 layer_to_surface_transform.MakeIdentity();
2535 layer_to_surface_transform.Translate(10.0, 10.0);
[email protected]2c7c6702013-03-26 03:14:052536 gfx::Rect expected = gfx::Rect(0, 0, 30, 30);
[email protected]fb661802013-03-25 01:59:322537 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2538 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2539 EXPECT_RECT_EQ(expected, actual);
2540
2541 // Case 2: Layer is outside the surface rect.
2542 layer_to_surface_transform.MakeIdentity();
2543 layer_to_surface_transform.Translate(120.0, 120.0);
2544 actual = LayerTreeHostCommon::CalculateVisibleRect(
2545 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2546 EXPECT_TRUE(actual.IsEmpty());
2547
2548 // Case 3: Layer is partially overlapping the surface rect.
2549 layer_to_surface_transform.MakeIdentity();
2550 layer_to_surface_transform.Translate(80.0, 80.0);
[email protected]2c7c6702013-03-26 03:14:052551 expected = gfx::Rect(0, 0, 20, 20);
[email protected]fb661802013-03-25 01:59:322552 actual = LayerTreeHostCommon::CalculateVisibleRect(
2553 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2554 EXPECT_RECT_EQ(expected, actual);
2555}
2556
2557TEST(LayerTreeHostCommonTest, VisibleRectFor2DRotations) {
2558 // Test the calculateVisibleRect() function works correctly for rotations
2559 // about z-axis (i.e. 2D rotations). Remember that calculateVisibleRect()
2560 // should return the g in the layer's space.
2561
[email protected]2c7c6702013-03-26 03:14:052562 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
2563 gfx::Rect layer_content_rect = gfx::Rect(0, 0, 30, 30);
[email protected]fb661802013-03-25 01:59:322564 gfx::Transform layer_to_surface_transform;
2565
2566 // Case 1: Layer is contained within the surface.
2567 layer_to_surface_transform.MakeIdentity();
2568 layer_to_surface_transform.Translate(50.0, 50.0);
2569 layer_to_surface_transform.Rotate(45.0);
[email protected]2c7c6702013-03-26 03:14:052570 gfx::Rect expected = gfx::Rect(0, 0, 30, 30);
[email protected]fb661802013-03-25 01:59:322571 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2572 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2573 EXPECT_RECT_EQ(expected, actual);
2574
2575 // Case 2: Layer is outside the surface rect.
2576 layer_to_surface_transform.MakeIdentity();
2577 layer_to_surface_transform.Translate(-50.0, 0.0);
2578 layer_to_surface_transform.Rotate(45.0);
2579 actual = LayerTreeHostCommon::CalculateVisibleRect(
2580 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2581 EXPECT_TRUE(actual.IsEmpty());
2582
2583 // Case 3: The layer is rotated about its top-left corner. In surface space,
2584 // the layer is oriented diagonally, with the left half outside of the render
2585 // surface. In this case, the g should still be the entire layer
2586 // (remember the g is computed in layer space); both the top-left
2587 // and bottom-right corners of the layer are still visible.
2588 layer_to_surface_transform.MakeIdentity();
2589 layer_to_surface_transform.Rotate(45.0);
[email protected]2c7c6702013-03-26 03:14:052590 expected = gfx::Rect(0, 0, 30, 30);
[email protected]fb661802013-03-25 01:59:322591 actual = LayerTreeHostCommon::CalculateVisibleRect(
2592 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2593 EXPECT_RECT_EQ(expected, actual);
2594
2595 // Case 4: The layer is rotated about its top-left corner, and translated
2596 // upwards. In surface space, the layer is oriented diagonally, with only the
2597 // top corner of the surface overlapping the layer. In layer space, the render
2598 // surface overlaps the right side of the layer. The g should be
2599 // the layer's right half.
2600 layer_to_surface_transform.MakeIdentity();
2601 layer_to_surface_transform.Translate(0.0, -sqrt(2.0) * 15.0);
2602 layer_to_surface_transform.Rotate(45.0);
[email protected]2c7c6702013-03-26 03:14:052603 expected = gfx::Rect(15, 0, 15, 30); // Right half of layer bounds.
[email protected]fb661802013-03-25 01:59:322604 actual = LayerTreeHostCommon::CalculateVisibleRect(
2605 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2606 EXPECT_RECT_EQ(expected, actual);
2607}
2608
2609TEST(LayerTreeHostCommonTest, VisibleRectFor3dOrthographicTransform) {
2610 // Test that the calculateVisibleRect() function works correctly for 3d
2611 // transforms.
2612
[email protected]2c7c6702013-03-26 03:14:052613 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
2614 gfx::Rect layer_content_rect = gfx::Rect(0, 0, 100, 100);
[email protected]fb661802013-03-25 01:59:322615 gfx::Transform layer_to_surface_transform;
2616
2617 // Case 1: Orthographic projection of a layer rotated about y-axis by 45
2618 // degrees, should be fully contained in the render surface.
2619 layer_to_surface_transform.MakeIdentity();
2620 layer_to_surface_transform.RotateAboutYAxis(45.0);
[email protected]2c7c6702013-03-26 03:14:052621 gfx::Rect expected = gfx::Rect(0, 0, 100, 100);
[email protected]fb661802013-03-25 01:59:322622 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2623 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2624 EXPECT_RECT_EQ(expected, actual);
2625
2626 // Case 2: Orthographic projection of a layer rotated about y-axis by 45
2627 // degrees, but shifted to the side so only the right-half the layer would be
2628 // visible on the surface.
2629 // 100 is the un-rotated layer width; divided by sqrt(2) is the rotated width.
2630 double half_width_of_rotated_layer = (100.0 / sqrt(2.0)) * 0.5;
2631 layer_to_surface_transform.MakeIdentity();
2632 layer_to_surface_transform.Translate(-half_width_of_rotated_layer, 0.0);
2633 layer_to_surface_transform.RotateAboutYAxis(
[email protected]2c7c6702013-03-26 03:14:052634 45.0); // Rotates about the left edge of the layer.
2635 expected = gfx::Rect(50, 0, 50, 100); // Tight half of the layer.
[email protected]fb661802013-03-25 01:59:322636 actual = LayerTreeHostCommon::CalculateVisibleRect(
2637 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2638 EXPECT_RECT_EQ(expected, actual);
2639}
2640
2641TEST(LayerTreeHostCommonTest, VisibleRectFor3dPerspectiveTransform) {
2642 // Test the calculateVisibleRect() function works correctly when the layer has
2643 // a perspective projection onto the target surface.
2644
[email protected]2c7c6702013-03-26 03:14:052645 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
2646 gfx::Rect layer_content_rect = gfx::Rect(-50, -50, 200, 200);
[email protected]fb661802013-03-25 01:59:322647 gfx::Transform layer_to_surface_transform;
2648
2649 // Case 1: Even though the layer is twice as large as the surface, due to
2650 // perspective foreshortening, the layer will fit fully in the surface when
2651 // its translated more than the perspective amount.
2652 layer_to_surface_transform.MakeIdentity();
2653
2654 // The following sequence of transforms applies the perspective about the
2655 // center of the surface.
2656 layer_to_surface_transform.Translate(50.0, 50.0);
2657 layer_to_surface_transform.ApplyPerspectiveDepth(9.0);
2658 layer_to_surface_transform.Translate(-50.0, -50.0);
2659
2660 // This translate places the layer in front of the surface's projection plane.
2661 layer_to_surface_transform.Translate3d(0.0, 0.0, -27.0);
2662
[email protected]2c7c6702013-03-26 03:14:052663 gfx::Rect expected = gfx::Rect(-50, -50, 200, 200);
[email protected]fb661802013-03-25 01:59:322664 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2665 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2666 EXPECT_RECT_EQ(expected, actual);
2667
2668 // Case 2: same projection as before, except that the layer is also translated
2669 // to the side, so that only the right half of the layer should be visible.
2670 //
2671 // Explanation of expected result: The perspective ratio is (z distance
2672 // between layer and camera origin) / (z distance between projection plane and
2673 // camera origin) == ((-27 - 9) / 9) Then, by similar triangles, if we want to
2674 // move a layer by translating -50 units in projected surface units (so that
2675 // only half of it is visible), then we would need to translate by (-36 / 9) *
2676 // -50 == -200 in the layer's units.
2677 layer_to_surface_transform.Translate3d(-200.0, 0.0, 0.0);
2678 expected = gfx::Rect(
2679 gfx::Point(50, -50),
2680 gfx::Size(100, 200)); // The right half of the layer's bounding rect.
2681 actual = LayerTreeHostCommon::CalculateVisibleRect(
2682 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2683 EXPECT_RECT_EQ(expected, actual);
2684}
2685
2686TEST(LayerTreeHostCommonTest,
2687 VisibleRectFor3dOrthographicIsNotClippedBehindSurface) {
2688 // There is currently no explicit concept of an orthographic projection plane
2689 // in our code (nor in the CSS spec to my knowledge). Therefore, layers that
2690 // are technically behind the surface in an orthographic world should not be
2691 // clipped when they are flattened to the surface.
2692
[email protected]2c7c6702013-03-26 03:14:052693 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
2694 gfx::Rect layer_content_rect = gfx::Rect(0, 0, 100, 100);
[email protected]fb661802013-03-25 01:59:322695 gfx::Transform layer_to_surface_transform;
2696
2697 // This sequence of transforms effectively rotates the layer about the y-axis
2698 // at the center of the layer.
2699 layer_to_surface_transform.MakeIdentity();
2700 layer_to_surface_transform.Translate(50.0, 0.0);
2701 layer_to_surface_transform.RotateAboutYAxis(45.0);
2702 layer_to_surface_transform.Translate(-50.0, 0.0);
2703
[email protected]2c7c6702013-03-26 03:14:052704 gfx::Rect expected = gfx::Rect(0, 0, 100, 100);
[email protected]fb661802013-03-25 01:59:322705 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2706 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2707 EXPECT_RECT_EQ(expected, actual);
2708}
2709
2710TEST(LayerTreeHostCommonTest, VisibleRectFor3dPerspectiveWhenClippedByW) {
2711 // Test the calculateVisibleRect() function works correctly when projecting a
2712 // surface onto a layer, but the layer is partially behind the camera (not
2713 // just behind the projection plane). In this case, the cartesian coordinates
2714 // may seem to be valid, but actually they are not. The visible rect needs to
2715 // be properly clipped by the w = 0 plane in homogeneous coordinates before
2716 // converting to cartesian coordinates.
2717
[email protected]2c7c6702013-03-26 03:14:052718 gfx::Rect target_surface_rect = gfx::Rect(-50, -50, 100, 100);
[email protected]fb661802013-03-25 01:59:322719 gfx::Rect layer_content_rect =
[email protected]2c7c6702013-03-26 03:14:052720 gfx::Rect(-10, -1, 20, 2);
[email protected]fb661802013-03-25 01:59:322721 gfx::Transform layer_to_surface_transform;
2722
2723 // The layer is positioned so that the right half of the layer should be in
2724 // front of the camera, while the other half is behind the surface's
2725 // projection plane. The following sequence of transforms applies the
2726 // perspective and rotation about the center of the layer.
2727 layer_to_surface_transform.MakeIdentity();
2728 layer_to_surface_transform.ApplyPerspectiveDepth(1.0);
2729 layer_to_surface_transform.Translate3d(-2.0, 0.0, 1.0);
2730 layer_to_surface_transform.RotateAboutYAxis(45.0);
2731
2732 // Sanity check that this transform does indeed cause w < 0 when applying the
2733 // transform, otherwise this code is not testing the intended scenario.
2734 bool clipped;
2735 MathUtil::MapQuad(layer_to_surface_transform,
2736 gfx::QuadF(gfx::RectF(layer_content_rect)),
2737 &clipped);
2738 ASSERT_TRUE(clipped);
2739
2740 int expected_x_position = 0;
2741 int expected_width = 10;
2742 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2743 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2744 EXPECT_EQ(expected_x_position, actual.x());
2745 EXPECT_EQ(expected_width, actual.width());
2746}
2747
2748TEST(LayerTreeHostCommonTest, VisibleRectForPerspectiveUnprojection) {
2749 // To determine visible rect in layer space, there needs to be an
2750 // un-projection from surface space to layer space. When the original
2751 // transform was a perspective projection that was clipped, it returns a rect
2752 // that encloses the clipped bounds. Un-projecting this new rect may require
2753 // clipping again.
2754
2755 // This sequence of transforms causes one corner of the layer to protrude
2756 // across the w = 0 plane, and should be clipped.
2757 gfx::Rect target_surface_rect =
[email protected]2c7c6702013-03-26 03:14:052758 gfx::Rect(-50, -50, 100, 100);
[email protected]fb661802013-03-25 01:59:322759 gfx::Rect layer_content_rect =
[email protected]2c7c6702013-03-26 03:14:052760 gfx::Rect(-10, -10, 20, 20);
[email protected]fb661802013-03-25 01:59:322761 gfx::Transform layer_to_surface_transform;
2762 layer_to_surface_transform.MakeIdentity();
2763 layer_to_surface_transform.ApplyPerspectiveDepth(1.0);
2764 layer_to_surface_transform.Translate3d(0.0, 0.0, -5.0);
2765 layer_to_surface_transform.RotateAboutYAxis(45.0);
2766 layer_to_surface_transform.RotateAboutXAxis(80.0);
2767
2768 // Sanity check that un-projection does indeed cause w < 0, otherwise this
2769 // code is not testing the intended scenario.
2770 bool clipped;
2771 gfx::RectF clipped_rect =
2772 MathUtil::MapClippedRect(layer_to_surface_transform, layer_content_rect);
2773 MathUtil::ProjectQuad(
2774 Inverse(layer_to_surface_transform), gfx::QuadF(clipped_rect), &clipped);
2775 ASSERT_TRUE(clipped);
2776
2777 // Only the corner of the layer is not visible on the surface because of being
2778 // clipped. But, the net result of rounding visible region to an axis-aligned
2779 // rect is that the entire layer should still be considered visible.
[email protected]2c7c6702013-03-26 03:14:052780 gfx::Rect expected = gfx::Rect(-10, -10, 20, 20);
[email protected]fb661802013-03-25 01:59:322781 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2782 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2783 EXPECT_RECT_EQ(expected, actual);
2784}
2785
2786TEST(LayerTreeHostCommonTest, DrawableAndVisibleContentRectsForSimpleLayers) {
2787 scoped_refptr<Layer> root = Layer::Create();
2788 scoped_refptr<LayerWithForcedDrawsContent> child1 =
2789 make_scoped_refptr(new LayerWithForcedDrawsContent());
2790 scoped_refptr<LayerWithForcedDrawsContent> child2 =
2791 make_scoped_refptr(new LayerWithForcedDrawsContent());
2792 scoped_refptr<LayerWithForcedDrawsContent> child3 =
2793 make_scoped_refptr(new LayerWithForcedDrawsContent());
2794 root->AddChild(child1);
2795 root->AddChild(child2);
2796 root->AddChild(child3);
2797
2798 gfx::Transform identity_matrix;
2799 SetLayerPropertiesForTesting(root.get(),
2800 identity_matrix,
2801 identity_matrix,
2802 gfx::PointF(),
2803 gfx::PointF(),
2804 gfx::Size(100, 100),
2805 false);
2806 SetLayerPropertiesForTesting(child1.get(),
2807 identity_matrix,
2808 identity_matrix,
2809 gfx::PointF(),
2810 gfx::PointF(),
2811 gfx::Size(50, 50),
2812 false);
2813 SetLayerPropertiesForTesting(child2.get(),
2814 identity_matrix,
2815 identity_matrix,
2816 gfx::PointF(),
2817 gfx::PointF(75.f, 75.f),
2818 gfx::Size(50, 50),
2819 false);
2820 SetLayerPropertiesForTesting(child3.get(),
2821 identity_matrix,
2822 identity_matrix,
2823 gfx::PointF(),
2824 gfx::PointF(125.f, 125.f),
2825 gfx::Size(50, 50),
2826 false);
2827
2828 ExecuteCalculateDrawProperties(root.get());
2829
2830 EXPECT_RECT_EQ(gfx::Rect(0, 0, 100, 100),
2831 root->render_surface()->DrawableContentRect());
2832 EXPECT_RECT_EQ(gfx::Rect(0, 0, 100, 100), root->drawable_content_rect());
2833
2834 // Layers that do not draw content should have empty visible_content_rects.
2835 EXPECT_RECT_EQ(gfx::Rect(0, 0, 0, 0), root->visible_content_rect());
2836
2837 // layer visible_content_rects are clipped by their target surface.
2838 EXPECT_RECT_EQ(gfx::Rect(0, 0, 50, 50), child1->visible_content_rect());
2839 EXPECT_RECT_EQ(gfx::Rect(0, 0, 25, 25), child2->visible_content_rect());
2840 EXPECT_TRUE(child3->visible_content_rect().IsEmpty());
2841
2842 // layer drawable_content_rects are not clipped.
2843 EXPECT_RECT_EQ(gfx::Rect(0, 0, 50, 50), child1->drawable_content_rect());
2844 EXPECT_RECT_EQ(gfx::Rect(75, 75, 50, 50), child2->drawable_content_rect());
2845 EXPECT_RECT_EQ(gfx::Rect(125, 125, 50, 50), child3->drawable_content_rect());
2846}
2847
2848TEST(LayerTreeHostCommonTest,
2849 DrawableAndVisibleContentRectsForLayersClippedByLayer) {
2850 scoped_refptr<Layer> root = Layer::Create();
2851 scoped_refptr<Layer> child = Layer::Create();
2852 scoped_refptr<LayerWithForcedDrawsContent> grand_child1 =
2853 make_scoped_refptr(new LayerWithForcedDrawsContent());
2854 scoped_refptr<LayerWithForcedDrawsContent> grand_child2 =
2855 make_scoped_refptr(new LayerWithForcedDrawsContent());
2856 scoped_refptr<LayerWithForcedDrawsContent> grand_child3 =
2857 make_scoped_refptr(new LayerWithForcedDrawsContent());
2858 root->AddChild(child);
2859 child->AddChild(grand_child1);
2860 child->AddChild(grand_child2);
2861 child->AddChild(grand_child3);
2862
2863 gfx::Transform identity_matrix;
2864 SetLayerPropertiesForTesting(root.get(),
2865 identity_matrix,
2866 identity_matrix,
2867 gfx::PointF(),
2868 gfx::PointF(),
2869 gfx::Size(100, 100),
2870 false);
2871 SetLayerPropertiesForTesting(child.get(),
2872 identity_matrix,
2873 identity_matrix,
2874 gfx::PointF(),
2875 gfx::PointF(),
2876 gfx::Size(100, 100),
2877 false);
2878 SetLayerPropertiesForTesting(grand_child1.get(),
2879 identity_matrix,
2880 identity_matrix,
2881 gfx::PointF(),
2882 gfx::PointF(5.f, 5.f),
2883 gfx::Size(50, 50),
2884 false);
2885 SetLayerPropertiesForTesting(grand_child2.get(),
2886 identity_matrix,
2887 identity_matrix,
2888 gfx::PointF(),
2889 gfx::PointF(75.f, 75.f),
2890 gfx::Size(50, 50),
2891 false);
2892 SetLayerPropertiesForTesting(grand_child3.get(),
2893 identity_matrix,
2894 identity_matrix,
2895 gfx::PointF(),
2896 gfx::PointF(125.f, 125.f),
2897 gfx::Size(50, 50),
2898 false);
2899
2900 child->SetMasksToBounds(true);
2901 ExecuteCalculateDrawProperties(root.get());
2902
2903 ASSERT_FALSE(child->render_surface());
2904
2905 EXPECT_RECT_EQ(gfx::Rect(0, 0, 100, 100),
2906 root->render_surface()->DrawableContentRect());
2907 EXPECT_RECT_EQ(gfx::Rect(0, 0, 100, 100), root->drawable_content_rect());
2908
2909 // Layers that do not draw content should have empty visible content rects.
2910 EXPECT_RECT_EQ(gfx::Rect(0, 0, 0, 0), root->visible_content_rect());
2911 EXPECT_RECT_EQ(gfx::Rect(0, 0, 0, 0), child->visible_content_rect());
2912
2913 // All grandchild visible content rects should be clipped by child.
2914 EXPECT_RECT_EQ(gfx::Rect(0, 0, 50, 50), grand_child1->visible_content_rect());
2915 EXPECT_RECT_EQ(gfx::Rect(0, 0, 25, 25), grand_child2->visible_content_rect());
2916 EXPECT_TRUE(grand_child3->visible_content_rect().IsEmpty());
2917
2918 // All grandchild DrawableContentRects should also be clipped by child.
2919 EXPECT_RECT_EQ(gfx::Rect(5, 5, 50, 50),
2920 grand_child1->drawable_content_rect());
2921 EXPECT_RECT_EQ(gfx::Rect(75, 75, 25, 25),
2922 grand_child2->drawable_content_rect());
2923 EXPECT_TRUE(grand_child3->drawable_content_rect().IsEmpty());
2924}
2925
2926TEST(LayerTreeHostCommonTest,
2927 DrawableAndVisibleContentRectsForLayersInUnclippedRenderSurface) {
2928 scoped_refptr<Layer> root = Layer::Create();
2929 scoped_refptr<Layer> render_surface1 = Layer::Create();
2930 scoped_refptr<LayerWithForcedDrawsContent> child1 =
2931 make_scoped_refptr(new LayerWithForcedDrawsContent());
2932 scoped_refptr<LayerWithForcedDrawsContent> child2 =
2933 make_scoped_refptr(new LayerWithForcedDrawsContent());
2934 scoped_refptr<LayerWithForcedDrawsContent> child3 =
2935 make_scoped_refptr(new LayerWithForcedDrawsContent());
2936 root->AddChild(render_surface1);
2937 render_surface1->AddChild(child1);
2938 render_surface1->AddChild(child2);
2939 render_surface1->AddChild(child3);
2940
2941 gfx::Transform identity_matrix;
2942 SetLayerPropertiesForTesting(root.get(),
2943 identity_matrix,
2944 identity_matrix,
2945 gfx::PointF(),
2946 gfx::PointF(),
2947 gfx::Size(100, 100),
2948 false);
2949 SetLayerPropertiesForTesting(render_surface1.get(),
2950 identity_matrix,
2951 identity_matrix,
2952 gfx::PointF(),
2953 gfx::PointF(),
2954 gfx::Size(3, 4),
2955 false);
2956 SetLayerPropertiesForTesting(child1.get(),
2957 identity_matrix,
2958 identity_matrix,
2959 gfx::PointF(),
2960 gfx::PointF(5.f, 5.f),
2961 gfx::Size(50, 50),
2962 false);
2963 SetLayerPropertiesForTesting(child2.get(),
2964 identity_matrix,
2965 identity_matrix,
2966 gfx::PointF(),
2967 gfx::PointF(75.f, 75.f),
2968 gfx::Size(50, 50),
2969 false);
2970 SetLayerPropertiesForTesting(child3.get(),
2971 identity_matrix,
2972 identity_matrix,
2973 gfx::PointF(),
2974 gfx::PointF(125.f, 125.f),
2975 gfx::Size(50, 50),
2976 false);
2977
2978 render_surface1->SetForceRenderSurface(true);
2979 ExecuteCalculateDrawProperties(root.get());
2980
2981 ASSERT_TRUE(render_surface1->render_surface());
2982
2983 EXPECT_RECT_EQ(gfx::Rect(0, 0, 100, 100),
2984 root->render_surface()->DrawableContentRect());
2985 EXPECT_RECT_EQ(gfx::Rect(0, 0, 100, 100), root->drawable_content_rect());
2986
2987 // Layers that do not draw content should have empty visible content rects.
2988 EXPECT_RECT_EQ(gfx::Rect(0, 0, 0, 0), root->visible_content_rect());
2989 EXPECT_RECT_EQ(gfx::Rect(0, 0, 0, 0),
2990 render_surface1->visible_content_rect());
2991
2992 // An unclipped surface grows its DrawableContentRect to include all drawable
2993 // regions of the subtree.
2994 EXPECT_RECT_EQ(gfx::Rect(5, 5, 170, 170),
2995 render_surface1->render_surface()->DrawableContentRect());
2996
2997 // All layers that draw content into the unclipped surface are also unclipped.
2998 EXPECT_RECT_EQ(gfx::Rect(0, 0, 50, 50), child1->visible_content_rect());
2999 EXPECT_RECT_EQ(gfx::Rect(0, 0, 50, 50), child2->visible_content_rect());
3000 EXPECT_RECT_EQ(gfx::Rect(0, 0, 50, 50), child3->visible_content_rect());
3001
3002 EXPECT_RECT_EQ(gfx::Rect(5, 5, 50, 50), child1->drawable_content_rect());
3003 EXPECT_RECT_EQ(gfx::Rect(75, 75, 50, 50), child2->drawable_content_rect());
3004 EXPECT_RECT_EQ(gfx::Rect(125, 125, 50, 50), child3->drawable_content_rect());
3005}
3006
3007TEST(LayerTreeHostCommonTest,
[email protected]451107a32013-04-10 05:12:473008 DrawableAndVisibleContentRectsForLayersWithUninvertibleTransform) {
3009 scoped_refptr<Layer> root = Layer::Create();
3010 scoped_refptr<LayerWithForcedDrawsContent> child =
3011 make_scoped_refptr(new LayerWithForcedDrawsContent());
3012 root->AddChild(child);
3013
3014 gfx::Transform identity_matrix;
3015 gfx::Transform uninvertible_matrix(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
3016
3017 SetLayerPropertiesForTesting(root.get(),
3018 identity_matrix,
3019 identity_matrix,
3020 gfx::PointF(),
3021 gfx::PointF(),
3022 gfx::Size(100, 100),
3023 false);
3024 SetLayerPropertiesForTesting(child.get(),
3025 uninvertible_matrix,
3026 identity_matrix,
3027 gfx::PointF(),
3028 gfx::PointF(5.f, 5.f),
3029 gfx::Size(50, 50),
3030 false);
3031
3032 ExecuteCalculateDrawProperties(root.get());
3033
3034 EXPECT_TRUE(child->visible_content_rect().IsEmpty());
3035 EXPECT_TRUE(child->drawable_content_rect().IsEmpty());
3036}
3037
3038TEST(LayerTreeHostCommonTest,
[email protected]fb661802013-03-25 01:59:323039 DrawableAndVisibleContentRectsForLayersInClippedRenderSurface) {
3040 scoped_refptr<Layer> root = Layer::Create();
3041 scoped_refptr<Layer> render_surface1 = Layer::Create();
3042 scoped_refptr<LayerWithForcedDrawsContent> child1 =
3043 make_scoped_refptr(new LayerWithForcedDrawsContent());
3044 scoped_refptr<LayerWithForcedDrawsContent> child2 =
3045 make_scoped_refptr(new LayerWithForcedDrawsContent());
3046 scoped_refptr<LayerWithForcedDrawsContent> child3 =
3047 make_scoped_refptr(new LayerWithForcedDrawsContent());
3048 root->AddChild(render_surface1);
3049 render_surface1->AddChild(child1);
3050 render_surface1->AddChild(child2);
3051 render_surface1->AddChild(child3);
3052
3053 gfx::Transform identity_matrix;
3054 SetLayerPropertiesForTesting(root.get(),
3055 identity_matrix,
3056 identity_matrix,
3057 gfx::PointF(),
3058 gfx::PointF(),
3059 gfx::Size(100, 100),
3060 false);
3061 SetLayerPropertiesForTesting(render_surface1.get(),
3062 identity_matrix,
3063 identity_matrix,
3064 gfx::PointF(),
3065 gfx::PointF(),
3066 gfx::Size(3, 4),
3067 false);
3068 SetLayerPropertiesForTesting(child1.get(),
3069 identity_matrix,
3070 identity_matrix,
3071 gfx::PointF(),
3072 gfx::PointF(5.f, 5.f),
3073 gfx::Size(50, 50),
3074 false);
3075 SetLayerPropertiesForTesting(child2.get(),
3076 identity_matrix,
3077 identity_matrix,
3078 gfx::PointF(),
3079 gfx::PointF(75.f, 75.f),
3080 gfx::Size(50, 50),
3081 false);
3082 SetLayerPropertiesForTesting(child3.get(),
3083 identity_matrix,
3084 identity_matrix,
3085 gfx::PointF(),
3086 gfx::PointF(125.f, 125.f),
3087 gfx::Size(50, 50),
3088 false);
3089
3090 root->SetMasksToBounds(true);
3091 render_surface1->SetForceRenderSurface(true);
3092 ExecuteCalculateDrawProperties(root.get());
3093
3094 ASSERT_TRUE(render_surface1->render_surface());
3095
3096 EXPECT_RECT_EQ(gfx::Rect(0, 0, 100, 100),
3097 root->render_surface()->DrawableContentRect());
3098 EXPECT_RECT_EQ(gfx::Rect(0, 0, 100, 100), root->drawable_content_rect());
3099
3100 // Layers that do not draw content should have empty visible content rects.
3101 EXPECT_RECT_EQ(gfx::Rect(0, 0, 0, 0), root->visible_content_rect());
3102 EXPECT_RECT_EQ(gfx::Rect(0, 0, 0, 0),
3103 render_surface1->visible_content_rect());
3104
3105 // A clipped surface grows its DrawableContentRect to include all drawable
3106 // regions of the subtree, but also gets clamped by the ancestor's clip.
3107 EXPECT_RECT_EQ(gfx::Rect(5, 5, 95, 95),
3108 render_surface1->render_surface()->DrawableContentRect());
3109
3110 // All layers that draw content into the surface have their visible content
3111 // rect clipped by the surface clip rect.
3112 EXPECT_RECT_EQ(gfx::Rect(0, 0, 50, 50), child1->visible_content_rect());
3113 EXPECT_RECT_EQ(gfx::Rect(0, 0, 25, 25), child2->visible_content_rect());
3114 EXPECT_TRUE(child3->visible_content_rect().IsEmpty());
3115
3116 // But the DrawableContentRects are unclipped.
3117 EXPECT_RECT_EQ(gfx::Rect(5, 5, 50, 50), child1->drawable_content_rect());
3118 EXPECT_RECT_EQ(gfx::Rect(75, 75, 50, 50), child2->drawable_content_rect());
3119 EXPECT_RECT_EQ(gfx::Rect(125, 125, 50, 50), child3->drawable_content_rect());
3120}
3121
3122TEST(LayerTreeHostCommonTest,
3123 DrawableAndVisibleContentRectsForSurfaceHierarchy) {
3124 // Check that clipping does not propagate down surfaces.
3125 scoped_refptr<Layer> root = Layer::Create();
3126 scoped_refptr<Layer> render_surface1 = Layer::Create();
3127 scoped_refptr<Layer> render_surface2 = Layer::Create();
3128 scoped_refptr<LayerWithForcedDrawsContent> child1 =
3129 make_scoped_refptr(new LayerWithForcedDrawsContent());
3130 scoped_refptr<LayerWithForcedDrawsContent> child2 =
3131 make_scoped_refptr(new LayerWithForcedDrawsContent());
3132 scoped_refptr<LayerWithForcedDrawsContent> child3 =
3133 make_scoped_refptr(new LayerWithForcedDrawsContent());
3134 root->AddChild(render_surface1);
3135 render_surface1->AddChild(render_surface2);
3136 render_surface2->AddChild(child1);
3137 render_surface2->AddChild(child2);
3138 render_surface2->AddChild(child3);
3139
3140 gfx::Transform identity_matrix;
3141 SetLayerPropertiesForTesting(root.get(),
3142 identity_matrix,
3143 identity_matrix,
3144 gfx::PointF(),
3145 gfx::PointF(),
3146 gfx::Size(100, 100),
3147 false);
3148 SetLayerPropertiesForTesting(render_surface1.get(),
3149 identity_matrix,
3150 identity_matrix,
3151 gfx::PointF(),
3152 gfx::PointF(),
3153 gfx::Size(3, 4),
3154 false);
3155 SetLayerPropertiesForTesting(render_surface2.get(),
3156 identity_matrix,
3157 identity_matrix,
3158 gfx::PointF(),
3159 gfx::PointF(),
3160 gfx::Size(7, 13),
3161 false);
3162 SetLayerPropertiesForTesting(child1.get(),
3163 identity_matrix,
3164 identity_matrix,
3165 gfx::PointF(),
3166 gfx::PointF(5.f, 5.f),
3167 gfx::Size(50, 50),
3168 false);
3169 SetLayerPropertiesForTesting(child2.get(),
3170 identity_matrix,
3171 identity_matrix,
3172 gfx::PointF(),
3173 gfx::PointF(75.f, 75.f),
3174 gfx::Size(50, 50),
3175 false);
3176 SetLayerPropertiesForTesting(child3.get(),
3177 identity_matrix,
3178 identity_matrix,
3179 gfx::PointF(),
3180 gfx::PointF(125.f, 125.f),
3181 gfx::Size(50, 50),
3182 false);
3183
3184 root->SetMasksToBounds(true);
3185 render_surface1->SetForceRenderSurface(true);
3186 render_surface2->SetForceRenderSurface(true);
3187 ExecuteCalculateDrawProperties(root.get());
3188
3189 ASSERT_TRUE(render_surface1->render_surface());
3190 ASSERT_TRUE(render_surface2->render_surface());
3191
3192 EXPECT_RECT_EQ(gfx::Rect(0, 0, 100, 100),
3193 root->render_surface()->DrawableContentRect());
3194 EXPECT_RECT_EQ(gfx::Rect(0, 0, 100, 100), root->drawable_content_rect());
3195
3196 // Layers that do not draw content should have empty visible content rects.
3197 EXPECT_RECT_EQ(gfx::Rect(0, 0, 0, 0), root->visible_content_rect());
3198 EXPECT_RECT_EQ(gfx::Rect(0, 0, 0, 0),
3199 render_surface1->visible_content_rect());
3200 EXPECT_RECT_EQ(gfx::Rect(0, 0, 0, 0),
3201 render_surface2->visible_content_rect());
3202
3203 // A clipped surface grows its DrawableContentRect to include all drawable
3204 // regions of the subtree, but also gets clamped by the ancestor's clip.
3205 EXPECT_RECT_EQ(gfx::Rect(5, 5, 95, 95),
3206 render_surface1->render_surface()->DrawableContentRect());
3207
3208 // render_surface1 lives in the "unclipped universe" of render_surface1, and
3209 // is only implicitly clipped by render_surface1's content rect. So,
3210 // render_surface2 grows to enclose all drawable content of its subtree.
3211 EXPECT_RECT_EQ(gfx::Rect(5, 5, 170, 170),
3212 render_surface2->render_surface()->DrawableContentRect());
3213
3214 // All layers that draw content into render_surface2 think they are unclipped.
3215 EXPECT_RECT_EQ(gfx::Rect(0, 0, 50, 50), child1->visible_content_rect());
3216 EXPECT_RECT_EQ(gfx::Rect(0, 0, 50, 50), child2->visible_content_rect());
3217 EXPECT_RECT_EQ(gfx::Rect(0, 0, 50, 50), child3->visible_content_rect());
3218
3219 // DrawableContentRects are also unclipped.
3220 EXPECT_RECT_EQ(gfx::Rect(5, 5, 50, 50), child1->drawable_content_rect());
3221 EXPECT_RECT_EQ(gfx::Rect(75, 75, 50, 50), child2->drawable_content_rect());
3222 EXPECT_RECT_EQ(gfx::Rect(125, 125, 50, 50), child3->drawable_content_rect());
3223}
3224
3225TEST(LayerTreeHostCommonTest,
3226 DrawableAndVisibleContentRectsWithTransformOnUnclippedSurface) {
3227 // Layers that have non-axis aligned bounds (due to transforms) have an
3228 // expanded, axis-aligned DrawableContentRect and visible content rect.
3229
3230 scoped_refptr<Layer> root = Layer::Create();
3231 scoped_refptr<Layer> render_surface1 = Layer::Create();
3232 scoped_refptr<LayerWithForcedDrawsContent> child1 =
3233 make_scoped_refptr(new LayerWithForcedDrawsContent());
3234 root->AddChild(render_surface1);
3235 render_surface1->AddChild(child1);
3236
3237 gfx::Transform identity_matrix;
3238 gfx::Transform child_rotation;
3239 child_rotation.Rotate(45.0);
3240 SetLayerPropertiesForTesting(root.get(),
3241 identity_matrix,
3242 identity_matrix,
3243 gfx::PointF(),
3244 gfx::PointF(),
3245 gfx::Size(100, 100),
3246 false);
3247 SetLayerPropertiesForTesting(render_surface1.get(),
3248 identity_matrix,
3249 identity_matrix,
3250 gfx::PointF(),
3251 gfx::PointF(),
3252 gfx::Size(3, 4),
3253 false);
3254 SetLayerPropertiesForTesting(child1.get(),
3255 child_rotation,
3256 identity_matrix,
3257 gfx::PointF(0.5f, 0.5f),
3258 gfx::PointF(25.f, 25.f),
3259 gfx::Size(50, 50),
3260 false);
3261
3262 render_surface1->SetForceRenderSurface(true);
3263 ExecuteCalculateDrawProperties(root.get());
3264
3265 ASSERT_TRUE(render_surface1->render_surface());
3266
3267 EXPECT_RECT_EQ(gfx::Rect(0, 0, 100, 100),
3268 root->render_surface()->DrawableContentRect());
3269 EXPECT_RECT_EQ(gfx::Rect(0, 0, 100, 100), root->drawable_content_rect());
3270
3271 // Layers that do not draw content should have empty visible content rects.
3272 EXPECT_RECT_EQ(gfx::Rect(0, 0, 0, 0), root->visible_content_rect());
3273 EXPECT_RECT_EQ(gfx::Rect(0, 0, 0, 0),
3274 render_surface1->visible_content_rect());
3275
3276 // The unclipped surface grows its DrawableContentRect to include all drawable
3277 // regions of the subtree.
3278 int diagonal_radius = ceil(sqrt(2.0) * 25.0);
3279 gfx::Rect expected_surface_drawable_content =
3280 gfx::Rect(50.0 - diagonal_radius,
3281 50.0 - diagonal_radius,
3282 diagonal_radius * 2.0,
3283 diagonal_radius * 2.0);
3284 EXPECT_RECT_EQ(expected_surface_drawable_content,
3285 render_surface1->render_surface()->DrawableContentRect());
3286
3287 // All layers that draw content into the unclipped surface are also unclipped.
3288 EXPECT_RECT_EQ(gfx::Rect(0, 0, 50, 50), child1->visible_content_rect());
3289 EXPECT_RECT_EQ(expected_surface_drawable_content,
3290 child1->drawable_content_rect());
3291}
3292
3293TEST(LayerTreeHostCommonTest,
3294 DrawableAndVisibleContentRectsWithTransformOnClippedSurface) {
3295 // Layers that have non-axis aligned bounds (due to transforms) have an
3296 // expanded, axis-aligned DrawableContentRect and visible content rect.
3297
3298 scoped_refptr<Layer> root = Layer::Create();
3299 scoped_refptr<Layer> render_surface1 = Layer::Create();
3300 scoped_refptr<LayerWithForcedDrawsContent> child1 =
3301 make_scoped_refptr(new LayerWithForcedDrawsContent());
3302 root->AddChild(render_surface1);
3303 render_surface1->AddChild(child1);
3304
3305 gfx::Transform identity_matrix;
3306 gfx::Transform child_rotation;
3307 child_rotation.Rotate(45.0);
3308 SetLayerPropertiesForTesting(root.get(),
3309 identity_matrix,
3310 identity_matrix,
3311 gfx::PointF(),
3312 gfx::PointF(),
3313 gfx::Size(50, 50),
3314 false);
3315 SetLayerPropertiesForTesting(render_surface1.get(),
3316 identity_matrix,
3317 identity_matrix,
3318 gfx::PointF(),
3319 gfx::PointF(),
3320 gfx::Size(3, 4),
3321 false);
3322 SetLayerPropertiesForTesting(child1.get(),
3323 child_rotation,
3324 identity_matrix,
3325 gfx::PointF(0.5f, 0.5f),
3326 gfx::PointF(25.f, 25.f),
3327 gfx::Size(50, 50),
3328 false);
3329
3330 root->SetMasksToBounds(true);
3331 render_surface1->SetForceRenderSurface(true);
3332 ExecuteCalculateDrawProperties(root.get());
3333
3334 ASSERT_TRUE(render_surface1->render_surface());
3335
3336 // The clipped surface clamps the DrawableContentRect that encloses the
3337 // rotated layer.
3338 int diagonal_radius = ceil(sqrt(2.0) * 25.0);
3339 gfx::Rect unclipped_surface_content = gfx::Rect(50.0 - diagonal_radius,
3340 50.0 - diagonal_radius,
3341 diagonal_radius * 2.0,
3342 diagonal_radius * 2.0);
3343 gfx::Rect expected_surface_drawable_content =
3344 gfx::IntersectRects(unclipped_surface_content, gfx::Rect(0, 0, 50, 50));
3345 EXPECT_RECT_EQ(expected_surface_drawable_content,
3346 render_surface1->render_surface()->DrawableContentRect());
3347
3348 // On the clipped surface, only a quarter of the child1 is visible, but when
3349 // rotating it back to child1's content space, the actual enclosing rect ends
3350 // up covering the full left half of child1.
3351 EXPECT_RECT_EQ(gfx::Rect(0, 0, 26, 50), child1->visible_content_rect());
3352
3353 // The child's DrawableContentRect is unclipped.
3354 EXPECT_RECT_EQ(unclipped_surface_content, child1->drawable_content_rect());
3355}
3356
3357TEST(LayerTreeHostCommonTest, DrawableAndVisibleContentRectsInHighDPI) {
3358 MockContentLayerClient client;
3359
3360 scoped_refptr<Layer> root = Layer::Create();
3361 scoped_refptr<ContentLayer> render_surface1 =
3362 CreateDrawableContentLayer(&client);
3363 scoped_refptr<ContentLayer> render_surface2 =
3364 CreateDrawableContentLayer(&client);
3365 scoped_refptr<ContentLayer> child1 = CreateDrawableContentLayer(&client);
3366 scoped_refptr<ContentLayer> child2 = CreateDrawableContentLayer(&client);
3367 scoped_refptr<ContentLayer> child3 = CreateDrawableContentLayer(&client);
3368 root->AddChild(render_surface1);
3369 render_surface1->AddChild(render_surface2);
3370 render_surface2->AddChild(child1);
3371 render_surface2->AddChild(child2);
3372 render_surface2->AddChild(child3);
3373
3374 gfx::Transform identity_matrix;
3375 SetLayerPropertiesForTesting(root.get(),
3376 identity_matrix,
3377 identity_matrix,
3378 gfx::PointF(),
3379 gfx::PointF(),
3380 gfx::Size(100, 100),
3381 false);
3382 SetLayerPropertiesForTesting(render_surface1.get(),
3383 identity_matrix,
3384 identity_matrix,
3385 gfx::PointF(),
3386 gfx::PointF(5.f, 5.f),
3387 gfx::Size(3, 4),
3388 false);
3389 SetLayerPropertiesForTesting(render_surface2.get(),
3390 identity_matrix,
3391 identity_matrix,
3392 gfx::PointF(),
3393 gfx::PointF(5.f, 5.f),
3394 gfx::Size(7, 13),
3395 false);
3396 SetLayerPropertiesForTesting(child1.get(),
3397 identity_matrix,
3398 identity_matrix,
3399 gfx::PointF(),
3400 gfx::PointF(5.f, 5.f),
3401 gfx::Size(50, 50),
3402 false);
3403 SetLayerPropertiesForTesting(child2.get(),
3404 identity_matrix,
3405 identity_matrix,
3406 gfx::PointF(),
3407 gfx::PointF(75.f, 75.f),
3408 gfx::Size(50, 50),
3409 false);
3410 SetLayerPropertiesForTesting(child3.get(),
3411 identity_matrix,
3412 identity_matrix,
3413 gfx::PointF(),
3414 gfx::PointF(125.f, 125.f),
3415 gfx::Size(50, 50),
3416 false);
3417
3418 float device_scale_factor = 2.f;
3419
3420 root->SetMasksToBounds(true);
3421 render_surface1->SetForceRenderSurface(true);
3422 render_surface2->SetForceRenderSurface(true);
3423 ExecuteCalculateDrawProperties(root.get(), device_scale_factor);
3424
3425 ASSERT_TRUE(render_surface1->render_surface());
3426 ASSERT_TRUE(render_surface2->render_surface());
3427
3428 // drawable_content_rects for all layers and surfaces are scaled by
3429 // device_scale_factor.
3430 EXPECT_RECT_EQ(gfx::Rect(0, 0, 200, 200),
3431 root->render_surface()->DrawableContentRect());
3432 EXPECT_RECT_EQ(gfx::Rect(0, 0, 200, 200), root->drawable_content_rect());
3433 EXPECT_RECT_EQ(gfx::Rect(10, 10, 190, 190),
3434 render_surface1->render_surface()->DrawableContentRect());
3435
3436 // render_surface2 lives in the "unclipped universe" of render_surface1, and
3437 // is only implicitly clipped by render_surface1.
3438 EXPECT_RECT_EQ(gfx::Rect(10, 10, 350, 350),
3439 render_surface2->render_surface()->DrawableContentRect());
3440
3441 EXPECT_RECT_EQ(gfx::Rect(10, 10, 100, 100), child1->drawable_content_rect());
3442 EXPECT_RECT_EQ(gfx::Rect(150, 150, 100, 100),
3443 child2->drawable_content_rect());
3444 EXPECT_RECT_EQ(gfx::Rect(250, 250, 100, 100),
3445 child3->drawable_content_rect());
3446
3447 // The root layer does not actually draw content of its own.
3448 EXPECT_RECT_EQ(gfx::Rect(0, 0, 0, 0), root->visible_content_rect());
3449
3450 // All layer visible content rects are expressed in content space of each
3451 // layer, so they are also scaled by the device_scale_factor.
3452 EXPECT_RECT_EQ(gfx::Rect(0, 0, 6, 8),
3453 render_surface1->visible_content_rect());
3454 EXPECT_RECT_EQ(gfx::Rect(0, 0, 14, 26),
3455 render_surface2->visible_content_rect());
3456 EXPECT_RECT_EQ(gfx::Rect(0, 0, 100, 100), child1->visible_content_rect());
3457 EXPECT_RECT_EQ(gfx::Rect(0, 0, 100, 100), child2->visible_content_rect());
3458 EXPECT_RECT_EQ(gfx::Rect(0, 0, 100, 100), child3->visible_content_rect());
3459}
3460
3461TEST(LayerTreeHostCommonTest, BackFaceCullingWithoutPreserves3d) {
3462 // Verify the behavior of back-face culling when there are no preserve-3d
3463 // layers. Note that 3d transforms still apply in this case, but they are
3464 // "flattened" to each parent layer according to current W3C spec.
3465
3466 const gfx::Transform identity_matrix;
3467 scoped_refptr<Layer> parent = Layer::Create();
3468 scoped_refptr<LayerWithForcedDrawsContent> front_facing_child =
3469 make_scoped_refptr(new LayerWithForcedDrawsContent());
3470 scoped_refptr<LayerWithForcedDrawsContent> back_facing_child =
3471 make_scoped_refptr(new LayerWithForcedDrawsContent());
3472 scoped_refptr<LayerWithForcedDrawsContent> front_facing_surface =
3473 make_scoped_refptr(new LayerWithForcedDrawsContent());
3474 scoped_refptr<LayerWithForcedDrawsContent> back_facing_surface =
3475 make_scoped_refptr(new LayerWithForcedDrawsContent());
3476 scoped_refptr<LayerWithForcedDrawsContent>
3477 front_facing_child_of_front_facing_surface =
3478 make_scoped_refptr(new LayerWithForcedDrawsContent());
3479 scoped_refptr<LayerWithForcedDrawsContent>
3480 back_facing_child_of_front_facing_surface =
3481 make_scoped_refptr(new LayerWithForcedDrawsContent());
3482 scoped_refptr<LayerWithForcedDrawsContent>
3483 front_facing_child_of_back_facing_surface =
3484 make_scoped_refptr(new LayerWithForcedDrawsContent());
3485 scoped_refptr<LayerWithForcedDrawsContent>
3486 back_facing_child_of_back_facing_surface =
3487 make_scoped_refptr(new LayerWithForcedDrawsContent());
3488
3489 parent->AddChild(front_facing_child);
3490 parent->AddChild(back_facing_child);
3491 parent->AddChild(front_facing_surface);
3492 parent->AddChild(back_facing_surface);
3493 front_facing_surface->AddChild(front_facing_child_of_front_facing_surface);
3494 front_facing_surface->AddChild(back_facing_child_of_front_facing_surface);
3495 back_facing_surface->AddChild(front_facing_child_of_back_facing_surface);
3496 back_facing_surface->AddChild(back_facing_child_of_back_facing_surface);
3497
3498 // Nothing is double-sided
3499 front_facing_child->SetDoubleSided(false);
3500 back_facing_child->SetDoubleSided(false);
3501 front_facing_surface->SetDoubleSided(false);
3502 back_facing_surface->SetDoubleSided(false);
3503 front_facing_child_of_front_facing_surface->SetDoubleSided(false);
3504 back_facing_child_of_front_facing_surface->SetDoubleSided(false);
3505 front_facing_child_of_back_facing_surface->SetDoubleSided(false);
3506 back_facing_child_of_back_facing_surface->SetDoubleSided(false);
3507
3508 gfx::Transform backface_matrix;
3509 backface_matrix.Translate(50.0, 50.0);
3510 backface_matrix.RotateAboutYAxis(180.0);
3511 backface_matrix.Translate(-50.0, -50.0);
3512
3513 // Having a descendant and opacity will force these to have render surfaces.
3514 front_facing_surface->SetOpacity(0.5f);
3515 back_facing_surface->SetOpacity(0.5f);
3516
3517 // Nothing preserves 3d. According to current W3C CSS gfx::Transforms spec,
3518 // these layers should blindly use their own local transforms to determine
3519 // back-face culling.
3520 SetLayerPropertiesForTesting(parent.get(),
3521 identity_matrix,
3522 identity_matrix,
3523 gfx::PointF(),
3524 gfx::PointF(),
3525 gfx::Size(100, 100),
3526 false);
3527 SetLayerPropertiesForTesting(front_facing_child.get(),
3528 identity_matrix,
3529 identity_matrix,
3530 gfx::PointF(),
3531 gfx::PointF(),
3532 gfx::Size(100, 100),
3533 false);
3534 SetLayerPropertiesForTesting(back_facing_child.get(),
3535 backface_matrix,
3536 identity_matrix,
3537 gfx::PointF(),
3538 gfx::PointF(),
3539 gfx::Size(100, 100),
3540 false);
3541 SetLayerPropertiesForTesting(front_facing_surface.get(),
3542 identity_matrix,
3543 identity_matrix,
3544 gfx::PointF(),
3545 gfx::PointF(),
3546 gfx::Size(100, 100),
3547 false);
3548 SetLayerPropertiesForTesting(back_facing_surface.get(),
3549 backface_matrix,
3550 identity_matrix,
3551 gfx::PointF(),
3552 gfx::PointF(),
3553 gfx::Size(100, 100),
3554 false);
3555 SetLayerPropertiesForTesting(front_facing_child_of_front_facing_surface.get(),
3556 identity_matrix,
3557 identity_matrix,
3558 gfx::PointF(),
3559 gfx::PointF(),
3560 gfx::Size(100, 100),
3561 false);
3562 SetLayerPropertiesForTesting(back_facing_child_of_front_facing_surface.get(),
3563 backface_matrix,
3564 identity_matrix,
3565 gfx::PointF(),
3566 gfx::PointF(),
3567 gfx::Size(100, 100),
3568 false);
3569 SetLayerPropertiesForTesting(front_facing_child_of_back_facing_surface.get(),
3570 identity_matrix,
3571 identity_matrix,
3572 gfx::PointF(),
3573 gfx::PointF(),
3574 gfx::Size(100, 100),
3575 false);
3576 SetLayerPropertiesForTesting(back_facing_child_of_back_facing_surface.get(),
3577 backface_matrix,
3578 identity_matrix,
3579 gfx::PointF(),
3580 gfx::PointF(),
3581 gfx::Size(100, 100),
3582 false);
3583
[email protected]50761e92013-03-29 20:51:283584 LayerList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:323585 int dummy_max_texture_size = 512;
3586 LayerTreeHostCommon::CalculateDrawProperties(parent.get(),
3587 parent->bounds(),
3588 1.f,
3589 1.f,
[email protected]f2136262013-04-26 21:10:193590 NULL,
[email protected]fb661802013-03-25 01:59:323591 dummy_max_texture_size,
3592 false,
3593 &render_surface_layer_list);
3594
3595 // Verify which render surfaces were created.
3596 EXPECT_FALSE(front_facing_child->render_surface());
3597 EXPECT_FALSE(back_facing_child->render_surface());
3598 EXPECT_TRUE(front_facing_surface->render_surface());
3599 EXPECT_TRUE(back_facing_surface->render_surface());
3600 EXPECT_FALSE(front_facing_child_of_front_facing_surface->render_surface());
3601 EXPECT_FALSE(back_facing_child_of_front_facing_surface->render_surface());
3602 EXPECT_FALSE(front_facing_child_of_back_facing_surface->render_surface());
3603 EXPECT_FALSE(back_facing_child_of_back_facing_surface->render_surface());
3604
3605 // Verify the render_surface_layer_list.
3606 ASSERT_EQ(3u, render_surface_layer_list.size());
3607 EXPECT_EQ(parent->id(), render_surface_layer_list[0]->id());
3608 EXPECT_EQ(front_facing_surface->id(), render_surface_layer_list[1]->id());
3609 // Even though the back facing surface LAYER gets culled, the other
3610 // descendants should still be added, so the SURFACE should not be culled.
3611 EXPECT_EQ(back_facing_surface->id(), render_surface_layer_list[2]->id());
3612
3613 // Verify root surface's layer list.
3614 ASSERT_EQ(
3615 3u, render_surface_layer_list[0]->render_surface()->layer_list().size());
3616 EXPECT_EQ(
3617 front_facing_child->id(),
3618 render_surface_layer_list[0]->render_surface()->layer_list()[0]->id());
3619 EXPECT_EQ(
3620 front_facing_surface->id(),
3621 render_surface_layer_list[0]->render_surface()->layer_list()[1]->id());
3622 EXPECT_EQ(
3623 back_facing_surface->id(),
3624 render_surface_layer_list[0]->render_surface()->layer_list()[2]->id());
3625
3626 // Verify front_facing_surface's layer list.
3627 ASSERT_EQ(
3628 2u, render_surface_layer_list[1]->render_surface()->layer_list().size());
3629 EXPECT_EQ(
3630 front_facing_surface->id(),
3631 render_surface_layer_list[1]->render_surface()->layer_list()[0]->id());
3632 EXPECT_EQ(
3633 front_facing_child_of_front_facing_surface->id(),
3634 render_surface_layer_list[1]->render_surface()->layer_list()[1]->id());
3635
3636 // Verify back_facing_surface's layer list; its own layer should be culled
3637 // from the surface list.
3638 ASSERT_EQ(
3639 1u, render_surface_layer_list[2]->render_surface()->layer_list().size());
3640 EXPECT_EQ(
3641 front_facing_child_of_back_facing_surface->id(),
3642 render_surface_layer_list[2]->render_surface()->layer_list()[0]->id());
3643}
3644
3645TEST(LayerTreeHostCommonTest, BackFaceCullingWithPreserves3d) {
3646 // Verify the behavior of back-face culling when preserves-3d transform style
3647 // is used.
3648
3649 const gfx::Transform identity_matrix;
3650 scoped_refptr<Layer> parent = Layer::Create();
3651 scoped_refptr<LayerWithForcedDrawsContent> front_facing_child =
3652 make_scoped_refptr(new LayerWithForcedDrawsContent());
3653 scoped_refptr<LayerWithForcedDrawsContent> back_facing_child =
3654 make_scoped_refptr(new LayerWithForcedDrawsContent());
3655 scoped_refptr<LayerWithForcedDrawsContent> front_facing_surface =
3656 make_scoped_refptr(new LayerWithForcedDrawsContent());
3657 scoped_refptr<LayerWithForcedDrawsContent> back_facing_surface =
3658 make_scoped_refptr(new LayerWithForcedDrawsContent());
3659 scoped_refptr<LayerWithForcedDrawsContent>
3660 front_facing_child_of_front_facing_surface =
3661 make_scoped_refptr(new LayerWithForcedDrawsContent());
3662 scoped_refptr<LayerWithForcedDrawsContent>
3663 back_facing_child_of_front_facing_surface =
3664 make_scoped_refptr(new LayerWithForcedDrawsContent());
3665 scoped_refptr<LayerWithForcedDrawsContent>
3666 front_facing_child_of_back_facing_surface =
3667 make_scoped_refptr(new LayerWithForcedDrawsContent());
3668 scoped_refptr<LayerWithForcedDrawsContent>
3669 back_facing_child_of_back_facing_surface =
3670 make_scoped_refptr(new LayerWithForcedDrawsContent());
3671 scoped_refptr<LayerWithForcedDrawsContent> dummy_replica_layer1 =
3672 make_scoped_refptr(new LayerWithForcedDrawsContent());
3673 scoped_refptr<LayerWithForcedDrawsContent> dummy_replica_layer2 =
3674 make_scoped_refptr(new LayerWithForcedDrawsContent());
3675
3676 parent->AddChild(front_facing_child);
3677 parent->AddChild(back_facing_child);
3678 parent->AddChild(front_facing_surface);
3679 parent->AddChild(back_facing_surface);
3680 front_facing_surface->AddChild(front_facing_child_of_front_facing_surface);
3681 front_facing_surface->AddChild(back_facing_child_of_front_facing_surface);
3682 back_facing_surface->AddChild(front_facing_child_of_back_facing_surface);
3683 back_facing_surface->AddChild(back_facing_child_of_back_facing_surface);
3684
3685 // Nothing is double-sided
3686 front_facing_child->SetDoubleSided(false);
3687 back_facing_child->SetDoubleSided(false);
3688 front_facing_surface->SetDoubleSided(false);
3689 back_facing_surface->SetDoubleSided(false);
3690 front_facing_child_of_front_facing_surface->SetDoubleSided(false);
3691 back_facing_child_of_front_facing_surface->SetDoubleSided(false);
3692 front_facing_child_of_back_facing_surface->SetDoubleSided(false);
3693 back_facing_child_of_back_facing_surface->SetDoubleSided(false);
3694
3695 gfx::Transform backface_matrix;
3696 backface_matrix.Translate(50.0, 50.0);
3697 backface_matrix.RotateAboutYAxis(180.0);
3698 backface_matrix.Translate(-50.0, -50.0);
3699
3700 // Opacity will not force creation of render surfaces in this case because of
3701 // the preserve-3d transform style. Instead, an example of when a surface
3702 // would be created with preserve-3d is when there is a replica layer.
3703 front_facing_surface->SetReplicaLayer(dummy_replica_layer1.get());
3704 back_facing_surface->SetReplicaLayer(dummy_replica_layer2.get());
3705
3706 // Each surface creates its own new 3d rendering context (as defined by W3C
3707 // spec). According to current W3C CSS gfx::Transforms spec, layers in a 3d
3708 // rendering context should use the transform with respect to that context.
3709 // This 3d rendering context occurs when (a) parent's transform style is flat
3710 // and (b) the layer's transform style is preserve-3d.
3711 SetLayerPropertiesForTesting(parent.get(),
3712 identity_matrix,
3713 identity_matrix,
3714 gfx::PointF(),
3715 gfx::PointF(),
3716 gfx::Size(100, 100),
3717 false); // parent transform style is flat.
3718 SetLayerPropertiesForTesting(front_facing_child.get(),
3719 identity_matrix,
3720 identity_matrix,
3721 gfx::PointF(),
3722 gfx::PointF(),
3723 gfx::Size(100, 100),
3724 false);
3725 SetLayerPropertiesForTesting(back_facing_child.get(),
3726 backface_matrix,
3727 identity_matrix,
3728 gfx::PointF(),
3729 gfx::PointF(),
3730 gfx::Size(100, 100),
3731 false);
3732 SetLayerPropertiesForTesting(
3733 front_facing_surface.get(),
3734 identity_matrix,
3735 identity_matrix,
3736 gfx::PointF(),
3737 gfx::PointF(),
3738 gfx::Size(100, 100),
3739 true); // surface transform style is preserve-3d.
3740 SetLayerPropertiesForTesting(
3741 back_facing_surface.get(),
3742 backface_matrix,
3743 identity_matrix,
3744 gfx::PointF(),
3745 gfx::PointF(),
3746 gfx::Size(100, 100),
3747 true); // surface transform style is preserve-3d.
3748 SetLayerPropertiesForTesting(front_facing_child_of_front_facing_surface.get(),
3749 identity_matrix,
3750 identity_matrix,
3751 gfx::PointF(),
3752 gfx::PointF(),
3753 gfx::Size(100, 100),
3754 false);
3755 SetLayerPropertiesForTesting(back_facing_child_of_front_facing_surface.get(),
3756 backface_matrix,
3757 identity_matrix,
3758 gfx::PointF(),
3759 gfx::PointF(),
3760 gfx::Size(100, 100),
3761 false);
3762 SetLayerPropertiesForTesting(front_facing_child_of_back_facing_surface.get(),
3763 identity_matrix,
3764 identity_matrix,
3765 gfx::PointF(),
3766 gfx::PointF(),
3767 gfx::Size(100, 100),
3768 false);
3769 SetLayerPropertiesForTesting(back_facing_child_of_back_facing_surface.get(),
3770 backface_matrix,
3771 identity_matrix,
3772 gfx::PointF(),
3773 gfx::PointF(),
3774 gfx::Size(100, 100),
3775 false);
3776
[email protected]50761e92013-03-29 20:51:283777 LayerList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:323778 int dummy_max_texture_size = 512;
3779 LayerTreeHostCommon::CalculateDrawProperties(parent.get(),
3780 parent->bounds(),
3781 1.f,
3782 1.f,
[email protected]f2136262013-04-26 21:10:193783 NULL,
[email protected]fb661802013-03-25 01:59:323784 dummy_max_texture_size,
3785 false,
3786 &render_surface_layer_list);
3787
3788 // Verify which render surfaces were created.
3789 EXPECT_FALSE(front_facing_child->render_surface());
3790 EXPECT_FALSE(back_facing_child->render_surface());
3791 EXPECT_TRUE(front_facing_surface->render_surface());
3792 EXPECT_FALSE(back_facing_surface->render_surface());
3793 EXPECT_FALSE(front_facing_child_of_front_facing_surface->render_surface());
3794 EXPECT_FALSE(back_facing_child_of_front_facing_surface->render_surface());
3795 EXPECT_FALSE(front_facing_child_of_back_facing_surface->render_surface());
3796 EXPECT_FALSE(back_facing_child_of_back_facing_surface->render_surface());
3797
3798 // Verify the render_surface_layer_list. The back-facing surface should be
3799 // culled.
3800 ASSERT_EQ(2u, render_surface_layer_list.size());
3801 EXPECT_EQ(parent->id(), render_surface_layer_list[0]->id());
3802 EXPECT_EQ(front_facing_surface->id(), render_surface_layer_list[1]->id());
3803
3804 // Verify root surface's layer list.
3805 ASSERT_EQ(
3806 2u, render_surface_layer_list[0]->render_surface()->layer_list().size());
3807 EXPECT_EQ(
3808 front_facing_child->id(),
3809 render_surface_layer_list[0]->render_surface()->layer_list()[0]->id());
3810 EXPECT_EQ(
3811 front_facing_surface->id(),
3812 render_surface_layer_list[0]->render_surface()->layer_list()[1]->id());
3813
3814 // Verify front_facing_surface's layer list.
3815 ASSERT_EQ(
3816 2u, render_surface_layer_list[1]->render_surface()->layer_list().size());
3817 EXPECT_EQ(
3818 front_facing_surface->id(),
3819 render_surface_layer_list[1]->render_surface()->layer_list()[0]->id());
3820 EXPECT_EQ(
3821 front_facing_child_of_front_facing_surface->id(),
3822 render_surface_layer_list[1]->render_surface()->layer_list()[1]->id());
3823}
3824
3825TEST(LayerTreeHostCommonTest, BackFaceCullingWithAnimatingTransforms) {
3826 // Verify that layers are appropriately culled when their back face is showing
3827 // and they are not double sided, while animations are going on.
3828 //
3829 // Layers that are animating do not get culled on the main thread, as their
3830 // transforms should be treated as "unknown" so we can not be sure that their
3831 // back face is really showing.
3832 const gfx::Transform identity_matrix;
3833 scoped_refptr<Layer> parent = Layer::Create();
3834 scoped_refptr<LayerWithForcedDrawsContent> child =
3835 make_scoped_refptr(new LayerWithForcedDrawsContent());
3836 scoped_refptr<LayerWithForcedDrawsContent> animating_surface =
3837 make_scoped_refptr(new LayerWithForcedDrawsContent());
3838 scoped_refptr<LayerWithForcedDrawsContent> child_of_animating_surface =
3839 make_scoped_refptr(new LayerWithForcedDrawsContent());
3840 scoped_refptr<LayerWithForcedDrawsContent> animating_child =
3841 make_scoped_refptr(new LayerWithForcedDrawsContent());
3842 scoped_refptr<LayerWithForcedDrawsContent> child2 =
3843 make_scoped_refptr(new LayerWithForcedDrawsContent());
3844
3845 parent->AddChild(child);
3846 parent->AddChild(animating_surface);
3847 animating_surface->AddChild(child_of_animating_surface);
3848 parent->AddChild(animating_child);
3849 parent->AddChild(child2);
3850
3851 // Nothing is double-sided
3852 child->SetDoubleSided(false);
3853 child2->SetDoubleSided(false);
3854 animating_surface->SetDoubleSided(false);
3855 child_of_animating_surface->SetDoubleSided(false);
3856 animating_child->SetDoubleSided(false);
3857
3858 gfx::Transform backface_matrix;
3859 backface_matrix.Translate(50.0, 50.0);
3860 backface_matrix.RotateAboutYAxis(180.0);
3861 backface_matrix.Translate(-50.0, -50.0);
3862
3863 // Make our render surface.
3864 animating_surface->SetForceRenderSurface(true);
3865
3866 // Animate the transform on the render surface.
3867 AddAnimatedTransformToController(
3868 animating_surface->layer_animation_controller(), 10.0, 30, 0);
3869 // This is just an animating layer, not a surface.
3870 AddAnimatedTransformToController(
3871 animating_child->layer_animation_controller(), 10.0, 30, 0);
3872
3873 SetLayerPropertiesForTesting(parent.get(),
3874 identity_matrix,
3875 identity_matrix,
3876 gfx::PointF(),
3877 gfx::PointF(),
3878 gfx::Size(100, 100),
3879 false);
3880 SetLayerPropertiesForTesting(child.get(),
3881 backface_matrix,
3882 identity_matrix,
3883 gfx::PointF(),
3884 gfx::PointF(),
3885 gfx::Size(100, 100),
3886 false);
3887 SetLayerPropertiesForTesting(animating_surface.get(),
3888 backface_matrix,
3889 identity_matrix,
3890 gfx::PointF(),
3891 gfx::PointF(),
3892 gfx::Size(100, 100),
3893 false);
3894 SetLayerPropertiesForTesting(child_of_animating_surface.get(),
3895 backface_matrix,
3896 identity_matrix,
3897 gfx::PointF(),
3898 gfx::PointF(),
3899 gfx::Size(100, 100),
3900 false);
3901 SetLayerPropertiesForTesting(animating_child.get(),
3902 backface_matrix,
3903 identity_matrix,
3904 gfx::PointF(),
3905 gfx::PointF(),
3906 gfx::Size(100, 100),
3907 false);
3908 SetLayerPropertiesForTesting(child2.get(),
3909 identity_matrix,
3910 identity_matrix,
3911 gfx::PointF(),
3912 gfx::PointF(),
3913 gfx::Size(100, 100),
3914 false);
3915
[email protected]50761e92013-03-29 20:51:283916 LayerList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:323917 int dummy_max_texture_size = 512;
3918 LayerTreeHostCommon::CalculateDrawProperties(parent.get(),
3919 parent->bounds(),
3920 1.f,
3921 1.f,
[email protected]f2136262013-04-26 21:10:193922 NULL,
[email protected]fb661802013-03-25 01:59:323923 dummy_max_texture_size,
3924 false,
3925 &render_surface_layer_list);
3926
3927 EXPECT_FALSE(child->render_surface());
3928 EXPECT_TRUE(animating_surface->render_surface());
3929 EXPECT_FALSE(child_of_animating_surface->render_surface());
3930 EXPECT_FALSE(animating_child->render_surface());
3931 EXPECT_FALSE(child2->render_surface());
3932
3933 // Verify that the animating_child and child_of_animating_surface were not
3934 // culled, but that child was.
3935 ASSERT_EQ(2u, render_surface_layer_list.size());
3936 EXPECT_EQ(parent->id(), render_surface_layer_list[0]->id());
3937 EXPECT_EQ(animating_surface->id(), render_surface_layer_list[1]->id());
3938
3939 // The non-animating child be culled from the layer list for the parent render
3940 // surface.
3941 ASSERT_EQ(
3942 3u, render_surface_layer_list[0]->render_surface()->layer_list().size());
3943 EXPECT_EQ(
3944 animating_surface->id(),
3945 render_surface_layer_list[0]->render_surface()->layer_list()[0]->id());
3946 EXPECT_EQ(
3947 animating_child->id(),
3948 render_surface_layer_list[0]->render_surface()->layer_list()[1]->id());
3949 EXPECT_EQ(
3950 child2->id(),
3951 render_surface_layer_list[0]->render_surface()->layer_list()[2]->id());
3952
3953 ASSERT_EQ(
3954 2u, render_surface_layer_list[1]->render_surface()->layer_list().size());
3955 EXPECT_EQ(
3956 animating_surface->id(),
3957 render_surface_layer_list[1]->render_surface()->layer_list()[0]->id());
3958 EXPECT_EQ(
3959 child_of_animating_surface->id(),
3960 render_surface_layer_list[1]->render_surface()->layer_list()[1]->id());
3961
3962 EXPECT_FALSE(child2->visible_content_rect().IsEmpty());
3963
3964 // The animating layers should have a visible content rect that represents the
3965 // area of the front face that is within the viewport.
3966 EXPECT_EQ(animating_child->visible_content_rect(),
[email protected]2c7c6702013-03-26 03:14:053967 gfx::Rect(animating_child->content_bounds()));
[email protected]fb661802013-03-25 01:59:323968 EXPECT_EQ(animating_surface->visible_content_rect(),
[email protected]2c7c6702013-03-26 03:14:053969 gfx::Rect(animating_surface->content_bounds()));
[email protected]fb661802013-03-25 01:59:323970 // And layers in the subtree of the animating layer should have valid visible
3971 // content rects also.
3972 EXPECT_EQ(
3973 child_of_animating_surface->visible_content_rect(),
[email protected]2c7c6702013-03-26 03:14:053974 gfx::Rect(child_of_animating_surface->content_bounds()));
[email protected]fb661802013-03-25 01:59:323975}
3976
3977TEST(LayerTreeHostCommonTest,
3978 BackFaceCullingWithPreserves3dForFlatteningSurface) {
3979 // Verify the behavior of back-face culling for a render surface that is
3980 // created when it flattens its subtree, and its parent has preserves-3d.
3981
3982 const gfx::Transform identity_matrix;
3983 scoped_refptr<Layer> parent = Layer::Create();
3984 scoped_refptr<LayerWithForcedDrawsContent> front_facing_surface =
3985 make_scoped_refptr(new LayerWithForcedDrawsContent());
3986 scoped_refptr<LayerWithForcedDrawsContent> back_facing_surface =
3987 make_scoped_refptr(new LayerWithForcedDrawsContent());
3988 scoped_refptr<LayerWithForcedDrawsContent> child1 =
3989 make_scoped_refptr(new LayerWithForcedDrawsContent());
3990 scoped_refptr<LayerWithForcedDrawsContent> child2 =
3991 make_scoped_refptr(new LayerWithForcedDrawsContent());
3992
3993 parent->AddChild(front_facing_surface);
3994 parent->AddChild(back_facing_surface);
3995 front_facing_surface->AddChild(child1);
3996 back_facing_surface->AddChild(child2);
3997
3998 // RenderSurfaces are not double-sided
3999 front_facing_surface->SetDoubleSided(false);
4000 back_facing_surface->SetDoubleSided(false);
4001
4002 gfx::Transform backface_matrix;
4003 backface_matrix.Translate(50.0, 50.0);
4004 backface_matrix.RotateAboutYAxis(180.0);
4005 backface_matrix.Translate(-50.0, -50.0);
4006
4007 SetLayerPropertiesForTesting(parent.get(),
4008 identity_matrix,
4009 identity_matrix,
4010 gfx::PointF(),
4011 gfx::PointF(),
4012 gfx::Size(100, 100),
4013 true); // parent transform style is preserve3d.
4014 SetLayerPropertiesForTesting(front_facing_surface.get(),
4015 identity_matrix,
4016 identity_matrix,
4017 gfx::PointF(),
4018 gfx::PointF(),
4019 gfx::Size(100, 100),
4020 false); // surface transform style is flat.
4021 SetLayerPropertiesForTesting(back_facing_surface.get(),
4022 backface_matrix,
4023 identity_matrix,
4024 gfx::PointF(),
4025 gfx::PointF(),
4026 gfx::Size(100, 100),
4027 false); // surface transform style is flat.
4028 SetLayerPropertiesForTesting(child1.get(),
4029 identity_matrix,
4030 identity_matrix,
4031 gfx::PointF(),
4032 gfx::PointF(),
4033 gfx::Size(100, 100),
4034 false);
4035 SetLayerPropertiesForTesting(child2.get(),
4036 identity_matrix,
4037 identity_matrix,
4038 gfx::PointF(),
4039 gfx::PointF(),
4040 gfx::Size(100, 100),
4041 false);
4042
[email protected]50761e92013-03-29 20:51:284043 LayerList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:324044 int dummy_max_texture_size = 512;
4045 LayerTreeHostCommon::CalculateDrawProperties(parent.get(),
4046 parent->bounds(),
4047 1.f,
4048 1.f,
[email protected]f2136262013-04-26 21:10:194049 NULL,
[email protected]fb661802013-03-25 01:59:324050 dummy_max_texture_size,
4051 false,
4052 &render_surface_layer_list);
4053
4054 // Verify which render surfaces were created.
4055 EXPECT_TRUE(front_facing_surface->render_surface());
4056 EXPECT_FALSE(
4057 back_facing_surface->render_surface()); // because it should be culled
4058 EXPECT_FALSE(child1->render_surface());
4059 EXPECT_FALSE(child2->render_surface());
4060
4061 // Verify the render_surface_layer_list. The back-facing surface should be
4062 // culled.
4063 ASSERT_EQ(2u, render_surface_layer_list.size());
4064 EXPECT_EQ(parent->id(), render_surface_layer_list[0]->id());
4065 EXPECT_EQ(front_facing_surface->id(), render_surface_layer_list[1]->id());
4066
4067 // Verify root surface's layer list.
4068 ASSERT_EQ(
4069 1u, render_surface_layer_list[0]->render_surface()->layer_list().size());
4070 EXPECT_EQ(
4071 front_facing_surface->id(),
4072 render_surface_layer_list[0]->render_surface()->layer_list()[0]->id());
4073
4074 // Verify front_facing_surface's layer list.
4075 ASSERT_EQ(
4076 2u, render_surface_layer_list[1]->render_surface()->layer_list().size());
4077 EXPECT_EQ(
4078 front_facing_surface->id(),
4079 render_surface_layer_list[1]->render_surface()->layer_list()[0]->id());
4080 EXPECT_EQ(
4081 child1->id(),
4082 render_surface_layer_list[1]->render_surface()->layer_list()[1]->id());
4083}
4084
4085TEST(LayerTreeHostCommonTest, HitTestingForEmptyLayerList) {
4086 // Hit testing on an empty render_surface_layer_list should return a null
4087 // pointer.
[email protected]50761e92013-03-29 20:51:284088 LayerImplList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:324089
4090 gfx::Point test_point(0, 0);
4091 LayerImpl* result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4092 test_point, render_surface_layer_list);
4093 EXPECT_FALSE(result_layer);
4094
4095 test_point = gfx::Point(10, 20);
4096 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4097 test_point, render_surface_layer_list);
4098 EXPECT_FALSE(result_layer);
4099}
4100
4101TEST(LayerTreeHostCommonTest, HitTestingForSingleLayer) {
4102 FakeImplProxy proxy;
4103 FakeLayerTreeHostImpl host_impl(&proxy);
4104 scoped_ptr<LayerImpl> root =
4105 LayerImpl::Create(host_impl.active_tree(), 12345);
4106
4107 gfx::Transform identity_matrix;
4108 gfx::PointF anchor;
4109 gfx::PointF position;
4110 gfx::Size bounds(100, 100);
4111 SetLayerPropertiesForTesting(root.get(),
4112 identity_matrix,
4113 identity_matrix,
4114 anchor,
4115 position,
4116 bounds,
4117 false);
4118 root->SetDrawsContent(true);
4119
[email protected]50761e92013-03-29 20:51:284120 LayerImplList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:324121 int dummy_max_texture_size = 512;
4122 LayerTreeHostCommon::CalculateDrawProperties(root.get(),
4123 root->bounds(),
4124 1.f,
4125 1.f,
[email protected]f2136262013-04-26 21:10:194126 NULL,
[email protected]fb661802013-03-25 01:59:324127 dummy_max_texture_size,
4128 false,
4129 &render_surface_layer_list,
4130 false);
4131
4132 // Sanity check the scenario we just created.
4133 ASSERT_EQ(1u, render_surface_layer_list.size());
4134 ASSERT_EQ(1u, root->render_surface()->layer_list().size());
4135
4136 // Hit testing for a point outside the layer should return a null pointer.
4137 gfx::Point test_point(101, 101);
4138 LayerImpl* result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4139 test_point, render_surface_layer_list);
4140 EXPECT_FALSE(result_layer);
4141
4142 test_point = gfx::Point(-1, -1);
4143 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4144 test_point, render_surface_layer_list);
4145 EXPECT_FALSE(result_layer);
4146
4147 // Hit testing for a point inside should return the root layer.
4148 test_point = gfx::Point(1, 1);
4149 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4150 test_point, render_surface_layer_list);
4151 ASSERT_TRUE(result_layer);
4152 EXPECT_EQ(12345, result_layer->id());
4153
4154 test_point = gfx::Point(99, 99);
4155 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4156 test_point, render_surface_layer_list);
4157 ASSERT_TRUE(result_layer);
4158 EXPECT_EQ(12345, result_layer->id());
4159}
4160
4161TEST(LayerTreeHostCommonTest, HitTestingForSingleLayerAndHud) {
4162 FakeImplProxy proxy;
4163 FakeLayerTreeHostImpl host_impl(&proxy);
4164 scoped_ptr<LayerImpl> root =
4165 LayerImpl::Create(host_impl.active_tree(), 12345);
4166 scoped_ptr<HeadsUpDisplayLayerImpl> hud =
4167 HeadsUpDisplayLayerImpl::Create(host_impl.active_tree(), 11111);
4168
4169 gfx::Transform identity_matrix;
4170 gfx::PointF anchor;
4171 gfx::PointF position;
4172 gfx::Size bounds(100, 100);
4173 SetLayerPropertiesForTesting(root.get(),
4174 identity_matrix,
4175 identity_matrix,
4176 anchor,
4177 position,
4178 bounds,
4179 false);
4180 root->SetDrawsContent(true);
4181
4182 // Create hud and add it as a child of root.
4183 gfx::Size hud_bounds(200, 200);
4184 SetLayerPropertiesForTesting(hud.get(),
4185 identity_matrix,
4186 identity_matrix,
4187 anchor,
4188 position,
4189 hud_bounds,
4190 false);
4191 hud->SetDrawsContent(true);
4192
4193 host_impl.active_tree()->set_hud_layer(hud.get());
4194 root->AddChild(hud.PassAs<LayerImpl>());
4195
[email protected]50761e92013-03-29 20:51:284196 LayerImplList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:324197 int dummy_max_texture_size = 512;
4198 LayerTreeHostCommon::CalculateDrawProperties(root.get(),
4199 hud_bounds,
4200 1.f,
4201 1.f,
[email protected]f2136262013-04-26 21:10:194202 NULL,
[email protected]fb661802013-03-25 01:59:324203 dummy_max_texture_size,
4204 false,
4205 &render_surface_layer_list,
4206 false);
4207
4208 // Sanity check the scenario we just created.
4209 ASSERT_EQ(1u, render_surface_layer_list.size());
4210 ASSERT_EQ(2u, root->render_surface()->layer_list().size());
4211
4212 // Hit testing for a point inside HUD, but outside root should return null
4213 gfx::Point test_point(101, 101);
4214 LayerImpl* result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4215 test_point, render_surface_layer_list);
4216 EXPECT_FALSE(result_layer);
4217
4218 test_point = gfx::Point(-1, -1);
4219 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4220 test_point, render_surface_layer_list);
4221 EXPECT_FALSE(result_layer);
4222
4223 // Hit testing for a point inside should return the root layer, never the HUD
4224 // layer.
4225 test_point = gfx::Point(1, 1);
4226 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4227 test_point, render_surface_layer_list);
4228 ASSERT_TRUE(result_layer);
4229 EXPECT_EQ(12345, result_layer->id());
4230
4231 test_point = gfx::Point(99, 99);
4232 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4233 test_point, render_surface_layer_list);
4234 ASSERT_TRUE(result_layer);
4235 EXPECT_EQ(12345, result_layer->id());
4236}
4237
4238TEST(LayerTreeHostCommonTest, HitTestingForUninvertibleTransform) {
4239 FakeImplProxy proxy;
4240 FakeLayerTreeHostImpl host_impl(&proxy);
4241 scoped_ptr<LayerImpl> root =
4242 LayerImpl::Create(host_impl.active_tree(), 12345);
4243
4244 gfx::Transform uninvertible_transform;
4245 uninvertible_transform.matrix().setDouble(0, 0, 0.0);
4246 uninvertible_transform.matrix().setDouble(1, 1, 0.0);
4247 uninvertible_transform.matrix().setDouble(2, 2, 0.0);
4248 uninvertible_transform.matrix().setDouble(3, 3, 0.0);
4249 ASSERT_FALSE(uninvertible_transform.IsInvertible());
4250
4251 gfx::Transform identity_matrix;
4252 gfx::PointF anchor;
4253 gfx::PointF position;
4254 gfx::Size bounds(100, 100);
4255 SetLayerPropertiesForTesting(root.get(),
4256 uninvertible_transform,
4257 identity_matrix,
4258 anchor,
4259 position,
4260 bounds,
4261 false);
4262 root->SetDrawsContent(true);
4263
[email protected]50761e92013-03-29 20:51:284264 LayerImplList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:324265 int dummy_max_texture_size = 512;
4266 LayerTreeHostCommon::CalculateDrawProperties(root.get(),
4267 root->bounds(),
4268 1.f,
4269 1.f,
[email protected]f2136262013-04-26 21:10:194270 NULL,
[email protected]fb661802013-03-25 01:59:324271 dummy_max_texture_size,
4272 false,
4273 &render_surface_layer_list,
4274 false);
4275
4276 // Sanity check the scenario we just created.
4277 ASSERT_EQ(1u, render_surface_layer_list.size());
4278 ASSERT_EQ(1u, root->render_surface()->layer_list().size());
4279 ASSERT_FALSE(root->screen_space_transform().IsInvertible());
4280
4281 // Hit testing any point should not hit the layer. If the invertible matrix is
4282 // accidentally ignored and treated like an identity, then the hit testing
4283 // will incorrectly hit the layer when it shouldn't.
4284 gfx::Point test_point(1, 1);
4285 LayerImpl* result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4286 test_point, render_surface_layer_list);
4287 EXPECT_FALSE(result_layer);
4288
4289 test_point = gfx::Point(10, 10);
4290 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4291 test_point, render_surface_layer_list);
4292 EXPECT_FALSE(result_layer);
4293
4294 test_point = gfx::Point(10, 30);
4295 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4296 test_point, render_surface_layer_list);
4297 EXPECT_FALSE(result_layer);
4298
4299 test_point = gfx::Point(50, 50);
4300 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4301 test_point, render_surface_layer_list);
4302 EXPECT_FALSE(result_layer);
4303
4304 test_point = gfx::Point(67, 48);
4305 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4306 test_point, render_surface_layer_list);
4307 EXPECT_FALSE(result_layer);
4308
4309 test_point = gfx::Point(99, 99);
4310 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4311 test_point, render_surface_layer_list);
4312 EXPECT_FALSE(result_layer);
4313
4314 test_point = gfx::Point(-1, -1);
4315 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4316 test_point, render_surface_layer_list);
4317 EXPECT_FALSE(result_layer);
4318}
4319
4320TEST(LayerTreeHostCommonTest, HitTestingForSinglePositionedLayer) {
4321 FakeImplProxy proxy;
4322 FakeLayerTreeHostImpl host_impl(&proxy);
4323 scoped_ptr<LayerImpl> root =
4324 LayerImpl::Create(host_impl.active_tree(), 12345);
4325
4326 gfx::Transform identity_matrix;
4327 gfx::PointF anchor;
4328 // this layer is positioned, and hit testing should correctly know where the
4329 // layer is located.
4330 gfx::PointF position(50.f, 50.f);
4331 gfx::Size bounds(100, 100);
4332 SetLayerPropertiesForTesting(root.get(),
4333 identity_matrix,
4334 identity_matrix,
4335 anchor,
4336 position,
4337 bounds,
4338 false);
4339 root->SetDrawsContent(true);
4340
[email protected]50761e92013-03-29 20:51:284341 LayerImplList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:324342 int dummy_max_texture_size = 512;
4343 LayerTreeHostCommon::CalculateDrawProperties(root.get(),
4344 root->bounds(),
4345 1.f,
4346 1.f,
[email protected]f2136262013-04-26 21:10:194347 NULL,
[email protected]fb661802013-03-25 01:59:324348 dummy_max_texture_size,
4349 false,
4350 &render_surface_layer_list,
4351 false);
4352
4353 // Sanity check the scenario we just created.
4354 ASSERT_EQ(1u, render_surface_layer_list.size());
4355 ASSERT_EQ(1u, root->render_surface()->layer_list().size());
4356
4357 // Hit testing for a point outside the layer should return a null pointer.
4358 gfx::Point test_point(49, 49);
4359 LayerImpl* result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4360 test_point, render_surface_layer_list);
4361 EXPECT_FALSE(result_layer);
4362
4363 // Even though the layer exists at (101, 101), it should not be visible there
4364 // since the root render surface would clamp it.
4365 test_point = gfx::Point(101, 101);
4366 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4367 test_point, render_surface_layer_list);
4368 EXPECT_FALSE(result_layer);
4369
4370 // Hit testing for a point inside should return the root layer.
4371 test_point = gfx::Point(51, 51);
4372 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4373 test_point, render_surface_layer_list);
4374 ASSERT_TRUE(result_layer);
4375 EXPECT_EQ(12345, result_layer->id());
4376
4377 test_point = gfx::Point(99, 99);
4378 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4379 test_point, render_surface_layer_list);
4380 ASSERT_TRUE(result_layer);
4381 EXPECT_EQ(12345, result_layer->id());
4382}
4383
4384TEST(LayerTreeHostCommonTest, HitTestingForSingleRotatedLayer) {
4385 FakeImplProxy proxy;
4386 FakeLayerTreeHostImpl host_impl(&proxy);
4387 scoped_ptr<LayerImpl> root =
4388 LayerImpl::Create(host_impl.active_tree(), 12345);
4389
4390 gfx::Transform identity_matrix;
4391 gfx::Transform rotation45_degrees_about_center;
4392 rotation45_degrees_about_center.Translate(50.0, 50.0);
4393 rotation45_degrees_about_center.RotateAboutZAxis(45.0);
4394 rotation45_degrees_about_center.Translate(-50.0, -50.0);
4395 gfx::PointF anchor;
4396 gfx::PointF position;
4397 gfx::Size bounds(100, 100);
4398 SetLayerPropertiesForTesting(root.get(),
4399 rotation45_degrees_about_center,
4400 identity_matrix,
4401 anchor,
4402 position,
4403 bounds,
4404 false);
4405 root->SetDrawsContent(true);
4406
[email protected]50761e92013-03-29 20:51:284407 LayerImplList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:324408 int dummy_max_texture_size = 512;
4409 LayerTreeHostCommon::CalculateDrawProperties(root.get(),
4410 root->bounds(),
4411 1.f,
4412 1.f,
[email protected]f2136262013-04-26 21:10:194413 NULL,
[email protected]fb661802013-03-25 01:59:324414 dummy_max_texture_size,
4415 false,
4416 &render_surface_layer_list,
4417 false);
4418
4419 // Sanity check the scenario we just created.
4420 ASSERT_EQ(1u, render_surface_layer_list.size());
4421 ASSERT_EQ(1u, root->render_surface()->layer_list().size());
4422
4423 // Hit testing for points outside the layer.
4424 // These corners would have been inside the un-transformed layer, but they
4425 // should not hit the correctly transformed layer.
4426 gfx::Point test_point(99, 99);
4427 LayerImpl* result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4428 test_point, render_surface_layer_list);
4429 EXPECT_FALSE(result_layer);
4430
4431 test_point = gfx::Point(1, 1);
4432 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4433 test_point, render_surface_layer_list);
4434 EXPECT_FALSE(result_layer);
4435
4436 // Hit testing for a point inside should return the root layer.
4437 test_point = gfx::Point(1, 50);
4438 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4439 test_point, render_surface_layer_list);
4440 ASSERT_TRUE(result_layer);
4441 EXPECT_EQ(12345, result_layer->id());
4442
4443 // Hit testing the corners that would overlap the unclipped layer, but are
4444 // outside the clipped region.
4445 test_point = gfx::Point(50, -1);
4446 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4447 test_point, render_surface_layer_list);
4448 ASSERT_FALSE(result_layer);
4449
4450 test_point = gfx::Point(-1, 50);
4451 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4452 test_point, render_surface_layer_list);
4453 ASSERT_FALSE(result_layer);
4454}
4455
4456TEST(LayerTreeHostCommonTest, HitTestingForSinglePerspectiveLayer) {
4457 FakeImplProxy proxy;
4458 FakeLayerTreeHostImpl host_impl(&proxy);
4459 scoped_ptr<LayerImpl> root =
4460 LayerImpl::Create(host_impl.active_tree(), 12345);
4461
4462 gfx::Transform identity_matrix;
4463
4464 // perspective_projection_about_center * translation_by_z is designed so that
4465 // the 100 x 100 layer becomes 50 x 50, and remains centered at (50, 50).
4466 gfx::Transform perspective_projection_about_center;
4467 perspective_projection_about_center.Translate(50.0, 50.0);
4468 perspective_projection_about_center.ApplyPerspectiveDepth(1.0);
4469 perspective_projection_about_center.Translate(-50.0, -50.0);
4470 gfx::Transform translation_by_z;
4471 translation_by_z.Translate3d(0.0, 0.0, -1.0);
4472
4473 gfx::PointF anchor;
4474 gfx::PointF position;
4475 gfx::Size bounds(100, 100);
4476 SetLayerPropertiesForTesting(
4477 root.get(),
4478 perspective_projection_about_center * translation_by_z,
4479 identity_matrix,
4480 anchor,
4481 position,
4482 bounds,
4483 false);
4484 root->SetDrawsContent(true);
4485
[email protected]50761e92013-03-29 20:51:284486 LayerImplList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:324487 int dummy_max_texture_size = 512;
4488 LayerTreeHostCommon::CalculateDrawProperties(root.get(),
4489 root->bounds(),
4490 1.f,
4491 1.f,
[email protected]f2136262013-04-26 21:10:194492 NULL,
[email protected]fb661802013-03-25 01:59:324493 dummy_max_texture_size,
4494 false,
4495 &render_surface_layer_list,
4496 false);
4497
4498 // Sanity check the scenario we just created.
4499 ASSERT_EQ(1u, render_surface_layer_list.size());
4500 ASSERT_EQ(1u, root->render_surface()->layer_list().size());
4501
4502 // Hit testing for points outside the layer.
4503 // These corners would have been inside the un-transformed layer, but they
4504 // should not hit the correctly transformed layer.
4505 gfx::Point test_point(24, 24);
4506 LayerImpl* result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4507 test_point, render_surface_layer_list);
4508 EXPECT_FALSE(result_layer);
4509
4510 test_point = gfx::Point(76, 76);
4511 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4512 test_point, render_surface_layer_list);
4513 EXPECT_FALSE(result_layer);
4514
4515 // Hit testing for a point inside should return the root layer.
4516 test_point = gfx::Point(26, 26);
4517 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4518 test_point, render_surface_layer_list);
4519 ASSERT_TRUE(result_layer);
4520 EXPECT_EQ(12345, result_layer->id());
4521
4522 test_point = gfx::Point(74, 74);
4523 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4524 test_point, render_surface_layer_list);
4525 ASSERT_TRUE(result_layer);
4526 EXPECT_EQ(12345, result_layer->id());
4527}
4528
4529TEST(LayerTreeHostCommonTest, HitTestingForSingleLayerWithScaledContents) {
4530 // A layer's visible content rect is actually in the layer's content space.
4531 // The screen space transform converts from the layer's origin space to screen
4532 // space. This test makes sure that hit testing works correctly accounts for
4533 // the contents scale. A contents scale that is not 1 effectively forces a
4534 // non-identity transform between layer's content space and layer's origin
4535 // space. The hit testing code must take this into account.
4536 //
4537 // To test this, the layer is positioned at (25, 25), and is size (50, 50). If
4538 // contents scale is ignored, then hit testing will mis-interpret the visible
4539 // content rect as being larger than the actual bounds of the layer.
4540 //
4541 FakeImplProxy proxy;
4542 FakeLayerTreeHostImpl host_impl(&proxy);
4543 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1);
4544
4545 gfx::Transform identity_matrix;
4546 gfx::PointF anchor;
4547
4548 SetLayerPropertiesForTesting(root.get(),
4549 identity_matrix,
4550 identity_matrix,
4551 anchor,
4552 gfx::PointF(),
4553 gfx::Size(100, 100),
4554 false);
4555 {
4556 gfx::PointF position(25.f, 25.f);
4557 gfx::Size bounds(50, 50);
4558 scoped_ptr<LayerImpl> test_layer =
4559 LayerImpl::Create(host_impl.active_tree(), 12345);
4560 SetLayerPropertiesForTesting(test_layer.get(),
4561 identity_matrix,
4562 identity_matrix,
4563 anchor,
4564 position,
4565 bounds,
4566 false);
4567
4568 // override content bounds and contents scale
4569 test_layer->SetContentBounds(gfx::Size(100, 100));
4570 test_layer->SetContentsScale(2, 2);
4571
4572 test_layer->SetDrawsContent(true);
4573 root->AddChild(test_layer.Pass());
4574 }
4575
[email protected]50761e92013-03-29 20:51:284576 LayerImplList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:324577 int dummy_max_texture_size = 512;
4578 LayerTreeHostCommon::CalculateDrawProperties(root.get(),
4579 root->bounds(),
4580 1.f,
4581 1.f,
[email protected]f2136262013-04-26 21:10:194582 NULL,
[email protected]fb661802013-03-25 01:59:324583 dummy_max_texture_size,
4584 false,
4585 &render_surface_layer_list,
4586 false);
4587
4588 // Sanity check the scenario we just created.
4589 // The visible content rect for test_layer is actually 100x100, even though
4590 // its layout size is 50x50, positioned at 25x25.
4591 LayerImpl* test_layer = root->children()[0];
[email protected]2c7c6702013-03-26 03:14:054592 EXPECT_RECT_EQ(gfx::Rect(0, 0, 100, 100),
[email protected]fb661802013-03-25 01:59:324593 test_layer->visible_content_rect());
4594 ASSERT_EQ(1u, render_surface_layer_list.size());
4595 ASSERT_EQ(1u, root->render_surface()->layer_list().size());
4596
4597 // Hit testing for a point outside the layer should return a null pointer (the
4598 // root layer does not draw content, so it will not be hit tested either).
4599 gfx::Point test_point(101, 101);
4600 LayerImpl* result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4601 test_point, render_surface_layer_list);
4602 EXPECT_FALSE(result_layer);
4603
4604 test_point = gfx::Point(24, 24);
4605 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4606 test_point, render_surface_layer_list);
4607 EXPECT_FALSE(result_layer);
4608
4609 test_point = gfx::Point(76, 76);
4610 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4611 test_point, render_surface_layer_list);
4612 EXPECT_FALSE(result_layer);
4613
4614 // Hit testing for a point inside should return the test layer.
4615 test_point = gfx::Point(26, 26);
4616 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4617 test_point, render_surface_layer_list);
4618 ASSERT_TRUE(result_layer);
4619 EXPECT_EQ(12345, result_layer->id());
4620
4621 test_point = gfx::Point(74, 74);
4622 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4623 test_point, render_surface_layer_list);
4624 ASSERT_TRUE(result_layer);
4625 EXPECT_EQ(12345, result_layer->id());
4626}
4627
4628TEST(LayerTreeHostCommonTest, HitTestingForSimpleClippedLayer) {
4629 // Test that hit-testing will only work for the visible portion of a layer,
4630 // and not the entire layer bounds. Here we just test the simple axis-aligned
4631 // case.
4632 gfx::Transform identity_matrix;
4633 gfx::PointF anchor;
4634
4635 FakeImplProxy proxy;
4636 FakeLayerTreeHostImpl host_impl(&proxy);
4637 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1);
4638 SetLayerPropertiesForTesting(root.get(),
4639 identity_matrix,
4640 identity_matrix,
4641 anchor,
4642 gfx::PointF(),
4643 gfx::Size(100, 100),
4644 false);
4645 {
4646 scoped_ptr<LayerImpl> clipping_layer =
4647 LayerImpl::Create(host_impl.active_tree(), 123);
4648 // this layer is positioned, and hit testing should correctly know where the
4649 // layer is located.
[email protected]ca2902e92013-03-28 01:45:354650 gfx::PointF position(25.f, 25.f);
[email protected]fb661802013-03-25 01:59:324651 gfx::Size bounds(50, 50);
4652 SetLayerPropertiesForTesting(clipping_layer.get(),
4653 identity_matrix,
4654 identity_matrix,
4655 anchor,
4656 position,
4657 bounds,
4658 false);
4659 clipping_layer->SetMasksToBounds(true);
4660
4661 scoped_ptr<LayerImpl> child =
4662 LayerImpl::Create(host_impl.active_tree(), 456);
4663 position = gfx::PointF(-50.f, -50.f);
4664 bounds = gfx::Size(300, 300);
4665 SetLayerPropertiesForTesting(child.get(),
4666 identity_matrix,
4667 identity_matrix,
4668 anchor,
4669 position,
4670 bounds,
4671 false);
4672 child->SetDrawsContent(true);
4673 clipping_layer->AddChild(child.Pass());
4674 root->AddChild(clipping_layer.Pass());
4675 }
4676
[email protected]50761e92013-03-29 20:51:284677 LayerImplList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:324678 int dummy_max_texture_size = 512;
4679 LayerTreeHostCommon::CalculateDrawProperties(root.get(),
4680 root->bounds(),
4681 1.f,
4682 1.f,
[email protected]f2136262013-04-26 21:10:194683 NULL,
[email protected]fb661802013-03-25 01:59:324684 dummy_max_texture_size,
4685 false,
4686 &render_surface_layer_list,
4687 false);
4688
4689 // Sanity check the scenario we just created.
4690 ASSERT_EQ(1u, render_surface_layer_list.size());
4691 ASSERT_EQ(1u, root->render_surface()->layer_list().size());
4692 ASSERT_EQ(456, root->render_surface()->layer_list()[0]->id());
4693
4694 // Hit testing for a point outside the layer should return a null pointer.
4695 // Despite the child layer being very large, it should be clipped to the root
4696 // layer's bounds.
4697 gfx::Point test_point(24, 24);
4698 LayerImpl* result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4699 test_point, render_surface_layer_list);
4700 EXPECT_FALSE(result_layer);
4701
4702 // Even though the layer exists at (101, 101), it should not be visible there
4703 // since the clipping_layer would clamp it.
4704 test_point = gfx::Point(76, 76);
4705 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4706 test_point, render_surface_layer_list);
4707 EXPECT_FALSE(result_layer);
4708
4709 // Hit testing for a point inside should return the child layer.
4710 test_point = gfx::Point(26, 26);
4711 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4712 test_point, render_surface_layer_list);
4713 ASSERT_TRUE(result_layer);
4714 EXPECT_EQ(456, result_layer->id());
4715
4716 test_point = gfx::Point(74, 74);
4717 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4718 test_point, render_surface_layer_list);
4719 ASSERT_TRUE(result_layer);
4720 EXPECT_EQ(456, result_layer->id());
4721}
4722
4723TEST(LayerTreeHostCommonTest, HitTestingForMultiClippedRotatedLayer) {
4724 // This test checks whether hit testing correctly avoids hit testing with
4725 // multiple ancestors that clip in non axis-aligned ways. To pass this test,
4726 // the hit testing algorithm needs to recognize that multiple parent layers
4727 // may clip the layer, and should not actually hit those clipped areas.
4728 //
4729 // The child and grand_child layers are both initialized to clip the
4730 // rotated_leaf. The child layer is rotated about the top-left corner, so that
4731 // the root + child clips combined create a triangle. The rotated_leaf will
4732 // only be visible where it overlaps this triangle.
4733 //
4734 FakeImplProxy proxy;
4735 FakeLayerTreeHostImpl host_impl(&proxy);
4736 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 123);
4737
4738 gfx::Transform identity_matrix;
4739 gfx::PointF anchor;
4740 gfx::PointF position;
4741 gfx::Size bounds(100, 100);
4742 SetLayerPropertiesForTesting(root.get(),
4743 identity_matrix,
4744 identity_matrix,
4745 anchor,
4746 position,
4747 bounds,
4748 false);
4749 root->SetMasksToBounds(true);
4750 {
4751 scoped_ptr<LayerImpl> child =
4752 LayerImpl::Create(host_impl.active_tree(), 456);
4753 scoped_ptr<LayerImpl> grand_child =
4754 LayerImpl::Create(host_impl.active_tree(), 789);
4755 scoped_ptr<LayerImpl> rotated_leaf =
4756 LayerImpl::Create(host_impl.active_tree(), 2468);
4757
4758 position = gfx::PointF(10.f, 10.f);
4759 bounds = gfx::Size(80, 80);
4760 SetLayerPropertiesForTesting(child.get(),
4761 identity_matrix,
4762 identity_matrix,
4763 anchor,
4764 position,
4765 bounds,
4766 false);
[email protected]7aba6662013-03-12 10:17:344767 child->SetMasksToBounds(true);
[email protected]aedf4e52013-01-09 23:24:444768
[email protected]fb661802013-03-25 01:59:324769 gfx::Transform rotation45_degrees_about_corner;
4770 rotation45_degrees_about_corner.RotateAboutZAxis(45.0);
[email protected]aedf4e52013-01-09 23:24:444771
[email protected]fb661802013-03-25 01:59:324772 // remember, positioned with respect to its parent which is already at 10,
4773 // 10
4774 position = gfx::PointF();
4775 bounds =
4776 gfx::Size(200, 200); // to ensure it covers at least sqrt(2) * 100.
4777 SetLayerPropertiesForTesting(grand_child.get(),
4778 rotation45_degrees_about_corner,
4779 identity_matrix,
4780 anchor,
4781 position,
4782 bounds,
4783 false);
4784 grand_child->SetMasksToBounds(true);
[email protected]aedf4e52013-01-09 23:24:444785
[email protected]fb661802013-03-25 01:59:324786 // Rotates about the center of the layer
4787 gfx::Transform rotated_leaf_transform;
4788 rotated_leaf_transform.Translate(
4789 -10.0, -10.0); // cancel out the grand_parent's position
4790 rotated_leaf_transform.RotateAboutZAxis(
4791 -45.0); // cancel out the corner 45-degree rotation of the parent.
4792 rotated_leaf_transform.Translate(50.0, 50.0);
4793 rotated_leaf_transform.RotateAboutZAxis(45.0);
4794 rotated_leaf_transform.Translate(-50.0, -50.0);
4795 position = gfx::PointF();
4796 bounds = gfx::Size(100, 100);
4797 SetLayerPropertiesForTesting(rotated_leaf.get(),
4798 rotated_leaf_transform,
4799 identity_matrix,
4800 anchor,
4801 position,
4802 bounds,
4803 false);
4804 rotated_leaf->SetDrawsContent(true);
[email protected]aedf4e52013-01-09 23:24:444805
[email protected]fb661802013-03-25 01:59:324806 grand_child->AddChild(rotated_leaf.Pass());
4807 child->AddChild(grand_child.Pass());
4808 root->AddChild(child.Pass());
4809 }
[email protected]aedf4e52013-01-09 23:24:444810
[email protected]50761e92013-03-29 20:51:284811 LayerImplList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:324812 int dummy_max_texture_size = 512;
4813 LayerTreeHostCommon::CalculateDrawProperties(root.get(),
4814 root->bounds(),
4815 1.f,
4816 1.f,
[email protected]f2136262013-04-26 21:10:194817 NULL,
[email protected]fb661802013-03-25 01:59:324818 dummy_max_texture_size,
4819 false,
4820 &render_surface_layer_list,
4821 false);
[email protected]aedf4e52013-01-09 23:24:444822
[email protected]fb661802013-03-25 01:59:324823 // Sanity check the scenario we just created.
4824 // The grand_child is expected to create a render surface because it
4825 // masksToBounds and is not axis aligned.
4826 ASSERT_EQ(2u, render_surface_layer_list.size());
4827 ASSERT_EQ(
4828 1u, render_surface_layer_list[0]->render_surface()->layer_list().size());
4829 ASSERT_EQ(789,
4830 render_surface_layer_list[0]->render_surface()->layer_list()[0]
4831 ->id()); // grand_child's surface.
4832 ASSERT_EQ(
4833 1u, render_surface_layer_list[1]->render_surface()->layer_list().size());
4834 ASSERT_EQ(
4835 2468,
4836 render_surface_layer_list[1]->render_surface()->layer_list()[0]->id());
4837
4838 // (11, 89) is close to the the bottom left corner within the clip, but it is
4839 // not inside the layer.
4840 gfx::Point test_point(11, 89);
4841 LayerImpl* result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4842 test_point, render_surface_layer_list);
4843 EXPECT_FALSE(result_layer);
4844
4845 // Closer inwards from the bottom left will overlap the layer.
4846 test_point = gfx::Point(25, 75);
4847 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4848 test_point, render_surface_layer_list);
4849 ASSERT_TRUE(result_layer);
4850 EXPECT_EQ(2468, result_layer->id());
4851
4852 // (4, 50) is inside the unclipped layer, but that corner of the layer should
4853 // be clipped away by the grandparent and should not get hit. If hit testing
4854 // blindly uses visible content rect without considering how parent may clip
4855 // the layer, then hit testing would accidentally think that the point
4856 // successfully hits the layer.
4857 test_point = gfx::Point(4, 50);
4858 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4859 test_point, render_surface_layer_list);
4860 EXPECT_FALSE(result_layer);
4861
4862 // (11, 50) is inside the layer and within the clipped area.
4863 test_point = gfx::Point(11, 50);
4864 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4865 test_point, render_surface_layer_list);
4866 ASSERT_TRUE(result_layer);
4867 EXPECT_EQ(2468, result_layer->id());
4868
4869 // Around the middle, just to the right and up, would have hit the layer
4870 // except that that area should be clipped away by the parent.
4871 test_point = gfx::Point(51, 51);
4872 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4873 test_point, render_surface_layer_list);
4874 EXPECT_FALSE(result_layer);
4875
4876 // Around the middle, just to the left and down, should successfully hit the
4877 // layer.
4878 test_point = gfx::Point(49, 51);
4879 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4880 test_point, render_surface_layer_list);
4881 ASSERT_TRUE(result_layer);
4882 EXPECT_EQ(2468, result_layer->id());
[email protected]aedf4e52013-01-09 23:24:444883}
4884
[email protected]fb661802013-03-25 01:59:324885TEST(LayerTreeHostCommonTest, HitTestingForNonClippingIntermediateLayer) {
4886 // This test checks that hit testing code does not accidentally clip to layer
4887 // bounds for a layer that actually does not clip.
4888 gfx::Transform identity_matrix;
4889 gfx::PointF anchor;
[email protected]aedf4e52013-01-09 23:24:444890
[email protected]fb661802013-03-25 01:59:324891 FakeImplProxy proxy;
4892 FakeLayerTreeHostImpl host_impl(&proxy);
4893 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1);
4894 SetLayerPropertiesForTesting(root.get(),
4895 identity_matrix,
4896 identity_matrix,
4897 anchor,
4898 gfx::PointF(),
4899 gfx::Size(100, 100),
4900 false);
4901 {
4902 scoped_ptr<LayerImpl> intermediate_layer =
4903 LayerImpl::Create(host_impl.active_tree(), 123);
4904 // this layer is positioned, and hit testing should correctly know where the
4905 // layer is located.
4906 gfx::PointF position(10.f, 10.f);
4907 gfx::Size bounds(50, 50);
4908 SetLayerPropertiesForTesting(intermediate_layer.get(),
4909 identity_matrix,
4910 identity_matrix,
4911 anchor,
4912 position,
4913 bounds,
4914 false);
4915 // Sanity check the intermediate layer should not clip.
4916 ASSERT_FALSE(intermediate_layer->masks_to_bounds());
4917 ASSERT_FALSE(intermediate_layer->mask_layer());
[email protected]aedf4e52013-01-09 23:24:444918
[email protected]fb661802013-03-25 01:59:324919 // The child of the intermediate_layer is translated so that it does not
4920 // overlap intermediate_layer at all. If child is incorrectly clipped, we
4921 // would not be able to hit it successfully.
4922 scoped_ptr<LayerImpl> child =
4923 LayerImpl::Create(host_impl.active_tree(), 456);
4924 position = gfx::PointF(60.f, 60.f); // 70, 70 in screen space
4925 bounds = gfx::Size(20, 20);
4926 SetLayerPropertiesForTesting(child.get(),
4927 identity_matrix,
4928 identity_matrix,
4929 anchor,
4930 position,
4931 bounds,
4932 false);
4933 child->SetDrawsContent(true);
4934 intermediate_layer->AddChild(child.Pass());
4935 root->AddChild(intermediate_layer.Pass());
4936 }
[email protected]aedf4e52013-01-09 23:24:444937
[email protected]50761e92013-03-29 20:51:284938 LayerImplList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:324939 int dummy_max_texture_size = 512;
4940 LayerTreeHostCommon::CalculateDrawProperties(root.get(),
4941 root->bounds(),
4942 1.f,
4943 1.f,
[email protected]f2136262013-04-26 21:10:194944 NULL,
[email protected]fb661802013-03-25 01:59:324945 dummy_max_texture_size,
4946 false,
4947 &render_surface_layer_list,
4948 false);
[email protected]aedf4e52013-01-09 23:24:444949
[email protected]fb661802013-03-25 01:59:324950 // Sanity check the scenario we just created.
4951 ASSERT_EQ(1u, render_surface_layer_list.size());
4952 ASSERT_EQ(1u, root->render_surface()->layer_list().size());
4953 ASSERT_EQ(456, root->render_surface()->layer_list()[0]->id());
[email protected]aedf4e52013-01-09 23:24:444954
[email protected]fb661802013-03-25 01:59:324955 // Hit testing for a point outside the layer should return a null pointer.
4956 gfx::Point test_point(69, 69);
4957 LayerImpl* result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4958 test_point, render_surface_layer_list);
4959 EXPECT_FALSE(result_layer);
[email protected]aedf4e52013-01-09 23:24:444960
[email protected]fb661802013-03-25 01:59:324961 test_point = gfx::Point(91, 91);
4962 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4963 test_point, render_surface_layer_list);
4964 EXPECT_FALSE(result_layer);
4965
4966 // Hit testing for a point inside should return the child layer.
4967 test_point = gfx::Point(71, 71);
4968 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4969 test_point, render_surface_layer_list);
4970 ASSERT_TRUE(result_layer);
4971 EXPECT_EQ(456, result_layer->id());
4972
4973 test_point = gfx::Point(89, 89);
4974 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
4975 test_point, render_surface_layer_list);
4976 ASSERT_TRUE(result_layer);
4977 EXPECT_EQ(456, result_layer->id());
[email protected]aedf4e52013-01-09 23:24:444978}
4979
[email protected]fb661802013-03-25 01:59:324980TEST(LayerTreeHostCommonTest, HitTestingForMultipleLayers) {
4981 FakeImplProxy proxy;
4982 FakeLayerTreeHostImpl host_impl(&proxy);
4983 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1);
[email protected]94f206c12012-08-25 00:09:144984
[email protected]fb661802013-03-25 01:59:324985 gfx::Transform identity_matrix;
4986 gfx::PointF anchor;
4987 gfx::PointF position;
4988 gfx::Size bounds(100, 100);
4989 SetLayerPropertiesForTesting(root.get(),
4990 identity_matrix,
4991 identity_matrix,
4992 anchor,
4993 position,
4994 bounds,
4995 false);
4996 root->SetDrawsContent(true);
4997 {
4998 // child 1 and child2 are initialized to overlap between x=50 and x=60.
4999 // grand_child is set to overlap both child1 and child2 between y=50 and
5000 // y=60. The expected stacking order is: (front) child2, (second)
5001 // grand_child, (third) child1, and (back) the root layer behind all other
5002 // layers.
[email protected]ecc12622012-10-30 20:45:425003
[email protected]fb661802013-03-25 01:59:325004 scoped_ptr<LayerImpl> child1 =
5005 LayerImpl::Create(host_impl.active_tree(), 2);
5006 scoped_ptr<LayerImpl> child2 =
5007 LayerImpl::Create(host_impl.active_tree(), 3);
5008 scoped_ptr<LayerImpl> grand_child1 =
5009 LayerImpl::Create(host_impl.active_tree(), 4);
[email protected]94f206c12012-08-25 00:09:145010
[email protected]fb661802013-03-25 01:59:325011 position = gfx::PointF(10.f, 10.f);
5012 bounds = gfx::Size(50, 50);
5013 SetLayerPropertiesForTesting(child1.get(),
5014 identity_matrix,
5015 identity_matrix,
5016 anchor,
5017 position,
5018 bounds,
5019 false);
5020 child1->SetDrawsContent(true);
[email protected]94f206c12012-08-25 00:09:145021
[email protected]fb661802013-03-25 01:59:325022 position = gfx::PointF(50.f, 10.f);
5023 bounds = gfx::Size(50, 50);
5024 SetLayerPropertiesForTesting(child2.get(),
5025 identity_matrix,
5026 identity_matrix,
5027 anchor,
5028 position,
5029 bounds,
5030 false);
5031 child2->SetDrawsContent(true);
[email protected]94f206c12012-08-25 00:09:145032
[email protected]fb661802013-03-25 01:59:325033 // Remember that grand_child is positioned with respect to its parent (i.e.
5034 // child1). In screen space, the intended position is (10, 50), with size
5035 // 100 x 50.
5036 position = gfx::PointF(0.f, 40.f);
5037 bounds = gfx::Size(100, 50);
5038 SetLayerPropertiesForTesting(grand_child1.get(),
5039 identity_matrix,
5040 identity_matrix,
5041 anchor,
5042 position,
5043 bounds,
5044 false);
5045 grand_child1->SetDrawsContent(true);
[email protected]94f206c12012-08-25 00:09:145046
[email protected]fb661802013-03-25 01:59:325047 child1->AddChild(grand_child1.Pass());
5048 root->AddChild(child1.Pass());
5049 root->AddChild(child2.Pass());
5050 }
[email protected]94f206c12012-08-25 00:09:145051
[email protected]fb661802013-03-25 01:59:325052 LayerImpl* child1 = root->children()[0];
5053 LayerImpl* child2 = root->children()[1];
5054 LayerImpl* grand_child1 = child1->children()[0];
5055
[email protected]50761e92013-03-29 20:51:285056 LayerImplList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:325057 int dummy_max_texture_size = 512;
5058 LayerTreeHostCommon::CalculateDrawProperties(root.get(),
5059 root->bounds(),
5060 1.f,
5061 1.f,
[email protected]f2136262013-04-26 21:10:195062 NULL,
[email protected]fb661802013-03-25 01:59:325063 dummy_max_texture_size,
5064 false,
5065 &render_surface_layer_list,
5066 false);
5067
5068 // Sanity check the scenario we just created.
5069 ASSERT_TRUE(child1);
5070 ASSERT_TRUE(child2);
5071 ASSERT_TRUE(grand_child1);
5072 ASSERT_EQ(1u, render_surface_layer_list.size());
5073 ASSERT_EQ(4u, root->render_surface()->layer_list().size());
5074 ASSERT_EQ(1, root->render_surface()->layer_list()[0]->id()); // root layer
5075 ASSERT_EQ(2, root->render_surface()->layer_list()[1]->id()); // child1
5076 ASSERT_EQ(4, root->render_surface()->layer_list()[2]->id()); // grand_child1
5077 ASSERT_EQ(3, root->render_surface()->layer_list()[3]->id()); // child2
5078
5079 // Nothing overlaps the root_layer at (1, 1), so hit testing there should find
5080 // the root layer.
5081 gfx::Point test_point = gfx::Point(1, 1);
5082 LayerImpl* result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
5083 test_point, render_surface_layer_list);
5084 ASSERT_TRUE(result_layer);
5085 EXPECT_EQ(1, result_layer->id());
5086
5087 // At (15, 15), child1 and root are the only layers. child1 is expected to be
5088 // on top.
5089 test_point = gfx::Point(15, 15);
5090 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
5091 test_point, render_surface_layer_list);
5092 ASSERT_TRUE(result_layer);
5093 EXPECT_EQ(2, result_layer->id());
5094
5095 // At (51, 20), child1 and child2 overlap. child2 is expected to be on top.
5096 test_point = gfx::Point(51, 20);
5097 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
5098 test_point, render_surface_layer_list);
5099 ASSERT_TRUE(result_layer);
5100 EXPECT_EQ(3, result_layer->id());
5101
5102 // At (80, 51), child2 and grand_child1 overlap. child2 is expected to be on
5103 // top.
5104 test_point = gfx::Point(80, 51);
5105 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
5106 test_point, render_surface_layer_list);
5107 ASSERT_TRUE(result_layer);
5108 EXPECT_EQ(3, result_layer->id());
5109
5110 // At (51, 51), all layers overlap each other. child2 is expected to be on top
5111 // of all other layers.
5112 test_point = gfx::Point(51, 51);
5113 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
5114 test_point, render_surface_layer_list);
5115 ASSERT_TRUE(result_layer);
5116 EXPECT_EQ(3, result_layer->id());
5117
5118 // At (20, 51), child1 and grand_child1 overlap. grand_child1 is expected to
5119 // be on top.
5120 test_point = gfx::Point(20, 51);
5121 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
5122 test_point, render_surface_layer_list);
5123 ASSERT_TRUE(result_layer);
5124 EXPECT_EQ(4, result_layer->id());
[email protected]94f206c12012-08-25 00:09:145125}
5126
[email protected]fb661802013-03-25 01:59:325127TEST(LayerTreeHostCommonTest, HitTestingForMultipleLayerLists) {
5128 //
5129 // The geometry is set up similarly to the previous case, but
5130 // all layers are forced to be render surfaces now.
5131 //
5132 FakeImplProxy proxy;
5133 FakeLayerTreeHostImpl host_impl(&proxy);
5134 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1);
[email protected]94f206c12012-08-25 00:09:145135
[email protected]fb661802013-03-25 01:59:325136 gfx::Transform identity_matrix;
5137 gfx::PointF anchor;
5138 gfx::PointF position;
5139 gfx::Size bounds(100, 100);
5140 SetLayerPropertiesForTesting(root.get(),
5141 identity_matrix,
5142 identity_matrix,
5143 anchor,
5144 position,
5145 bounds,
5146 false);
5147 root->SetDrawsContent(true);
5148 {
5149 // child 1 and child2 are initialized to overlap between x=50 and x=60.
5150 // grand_child is set to overlap both child1 and child2 between y=50 and
5151 // y=60. The expected stacking order is: (front) child2, (second)
5152 // grand_child, (third) child1, and (back) the root layer behind all other
5153 // layers.
[email protected]94f206c12012-08-25 00:09:145154
[email protected]fb661802013-03-25 01:59:325155 scoped_ptr<LayerImpl> child1 =
5156 LayerImpl::Create(host_impl.active_tree(), 2);
5157 scoped_ptr<LayerImpl> child2 =
5158 LayerImpl::Create(host_impl.active_tree(), 3);
5159 scoped_ptr<LayerImpl> grand_child1 =
5160 LayerImpl::Create(host_impl.active_tree(), 4);
[email protected]94f206c12012-08-25 00:09:145161
[email protected]fb661802013-03-25 01:59:325162 position = gfx::PointF(10.f, 10.f);
5163 bounds = gfx::Size(50, 50);
5164 SetLayerPropertiesForTesting(child1.get(),
5165 identity_matrix,
5166 identity_matrix,
5167 anchor,
5168 position,
5169 bounds,
5170 false);
5171 child1->SetDrawsContent(true);
5172 child1->SetForceRenderSurface(true);
[email protected]ecc12622012-10-30 20:45:425173
[email protected]fb661802013-03-25 01:59:325174 position = gfx::PointF(50.f, 10.f);
5175 bounds = gfx::Size(50, 50);
5176 SetLayerPropertiesForTesting(child2.get(),
5177 identity_matrix,
5178 identity_matrix,
5179 anchor,
5180 position,
5181 bounds,
5182 false);
5183 child2->SetDrawsContent(true);
[email protected]7aba6662013-03-12 10:17:345184 child2->SetForceRenderSurface(true);
[email protected]dc462d782012-11-21 21:43:015185
[email protected]fb661802013-03-25 01:59:325186 // Remember that grand_child is positioned with respect to its parent (i.e.
5187 // child1). In screen space, the intended position is (10, 50), with size
5188 // 100 x 50.
5189 position = gfx::PointF(0.f, 40.f);
5190 bounds = gfx::Size(100, 50);
5191 SetLayerPropertiesForTesting(grand_child1.get(),
5192 identity_matrix,
5193 identity_matrix,
5194 anchor,
5195 position,
5196 bounds,
5197 false);
5198 grand_child1->SetDrawsContent(true);
5199 grand_child1->SetForceRenderSurface(true);
[email protected]dc462d782012-11-21 21:43:015200
[email protected]fb661802013-03-25 01:59:325201 child1->AddChild(grand_child1.Pass());
5202 root->AddChild(child1.Pass());
5203 root->AddChild(child2.Pass());
5204 }
[email protected]dc462d782012-11-21 21:43:015205
[email protected]fb661802013-03-25 01:59:325206 LayerImpl* child1 = root->children()[0];
5207 LayerImpl* child2 = root->children()[1];
5208 LayerImpl* grand_child1 = child1->children()[0];
[email protected]dc462d782012-11-21 21:43:015209
[email protected]50761e92013-03-29 20:51:285210 LayerImplList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:325211 int dummy_max_texture_size = 512;
5212 LayerTreeHostCommon::CalculateDrawProperties(root.get(),
5213 root->bounds(),
5214 1.f,
5215 1.f,
[email protected]f2136262013-04-26 21:10:195216 NULL,
[email protected]fb661802013-03-25 01:59:325217 dummy_max_texture_size,
5218 false,
5219 &render_surface_layer_list,
5220 false);
[email protected]dc462d782012-11-21 21:43:015221
[email protected]fb661802013-03-25 01:59:325222 // Sanity check the scenario we just created.
5223 ASSERT_TRUE(child1);
5224 ASSERT_TRUE(child2);
5225 ASSERT_TRUE(grand_child1);
5226 ASSERT_TRUE(child1->render_surface());
5227 ASSERT_TRUE(child2->render_surface());
5228 ASSERT_TRUE(grand_child1->render_surface());
5229 ASSERT_EQ(4u, render_surface_layer_list.size());
5230 // The root surface has the root layer, and child1's and child2's render
5231 // surfaces.
5232 ASSERT_EQ(3u, root->render_surface()->layer_list().size());
5233 // The child1 surface has the child1 layer and grand_child1's render surface.
5234 ASSERT_EQ(2u, child1->render_surface()->layer_list().size());
5235 ASSERT_EQ(1u, child2->render_surface()->layer_list().size());
5236 ASSERT_EQ(1u, grand_child1->render_surface()->layer_list().size());
5237 ASSERT_EQ(1, render_surface_layer_list[0]->id()); // root layer
5238 ASSERT_EQ(2, render_surface_layer_list[1]->id()); // child1
5239 ASSERT_EQ(4, render_surface_layer_list[2]->id()); // grand_child1
5240 ASSERT_EQ(3, render_surface_layer_list[3]->id()); // child2
[email protected]dc462d782012-11-21 21:43:015241
[email protected]fb661802013-03-25 01:59:325242 // Nothing overlaps the root_layer at (1, 1), so hit testing there should find
5243 // the root layer.
5244 gfx::Point test_point = gfx::Point(1, 1);
5245 LayerImpl* result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
5246 test_point, render_surface_layer_list);
5247 ASSERT_TRUE(result_layer);
5248 EXPECT_EQ(1, result_layer->id());
[email protected]dc462d782012-11-21 21:43:015249
[email protected]fb661802013-03-25 01:59:325250 // At (15, 15), child1 and root are the only layers. child1 is expected to be
5251 // on top.
5252 test_point = gfx::Point(15, 15);
5253 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
5254 test_point, render_surface_layer_list);
5255 ASSERT_TRUE(result_layer);
5256 EXPECT_EQ(2, result_layer->id());
[email protected]dc462d782012-11-21 21:43:015257
[email protected]fb661802013-03-25 01:59:325258 // At (51, 20), child1 and child2 overlap. child2 is expected to be on top.
5259 test_point = gfx::Point(51, 20);
5260 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
5261 test_point, render_surface_layer_list);
5262 ASSERT_TRUE(result_layer);
5263 EXPECT_EQ(3, result_layer->id());
[email protected]dc462d782012-11-21 21:43:015264
[email protected]fb661802013-03-25 01:59:325265 // At (80, 51), child2 and grand_child1 overlap. child2 is expected to be on
5266 // top.
5267 test_point = gfx::Point(80, 51);
5268 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
5269 test_point, render_surface_layer_list);
5270 ASSERT_TRUE(result_layer);
5271 EXPECT_EQ(3, result_layer->id());
[email protected]dc462d782012-11-21 21:43:015272
[email protected]fb661802013-03-25 01:59:325273 // At (51, 51), all layers overlap each other. child2 is expected to be on top
5274 // of all other layers.
5275 test_point = gfx::Point(51, 51);
5276 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
5277 test_point, render_surface_layer_list);
5278 ASSERT_TRUE(result_layer);
5279 EXPECT_EQ(3, result_layer->id());
5280
5281 // At (20, 51), child1 and grand_child1 overlap. grand_child1 is expected to
5282 // be on top.
5283 test_point = gfx::Point(20, 51);
5284 result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
5285 test_point, render_surface_layer_list);
5286 ASSERT_TRUE(result_layer);
5287 EXPECT_EQ(4, result_layer->id());
[email protected]dc462d782012-11-21 21:43:015288}
5289
[email protected]fb661802013-03-25 01:59:325290TEST(LayerTreeHostCommonTest, HitCheckingTouchHandlerRegionsForEmptyLayerList) {
5291 // Hit checking on an empty render_surface_layer_list should return a null
5292 // pointer.
[email protected]50761e92013-03-29 20:51:285293 LayerImplList render_surface_layer_list;
[email protected]94f206c12012-08-25 00:09:145294
[email protected]fb661802013-03-25 01:59:325295 gfx::Point test_point(0, 0);
5296 LayerImpl* result_layer =
5297 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5298 test_point, render_surface_layer_list);
5299 EXPECT_FALSE(result_layer);
[email protected]94f206c12012-08-25 00:09:145300
[email protected]fb661802013-03-25 01:59:325301 test_point = gfx::Point(10, 20);
5302 result_layer =
5303 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5304 test_point, render_surface_layer_list);
5305 EXPECT_FALSE(result_layer);
[email protected]94f206c12012-08-25 00:09:145306}
5307
[email protected]fb661802013-03-25 01:59:325308TEST(LayerTreeHostCommonTest, HitCheckingTouchHandlerRegionsForSingleLayer) {
5309 FakeImplProxy proxy;
5310 FakeLayerTreeHostImpl host_impl(&proxy);
5311 scoped_ptr<LayerImpl> root =
5312 LayerImpl::Create(host_impl.active_tree(), 12345);
[email protected]94f206c12012-08-25 00:09:145313
[email protected]fb661802013-03-25 01:59:325314 gfx::Transform identity_matrix;
5315 Region touch_handler_region(gfx::Rect(10, 10, 50, 50));
5316 gfx::PointF anchor;
5317 gfx::PointF position;
5318 gfx::Size bounds(100, 100);
5319 SetLayerPropertiesForTesting(root.get(),
5320 identity_matrix,
5321 identity_matrix,
5322 anchor,
5323 position,
5324 bounds,
5325 false);
5326 root->SetDrawsContent(true);
[email protected]94f206c12012-08-25 00:09:145327
[email protected]50761e92013-03-29 20:51:285328 LayerImplList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:325329 int dummy_max_texture_size = 512;
5330 LayerTreeHostCommon::CalculateDrawProperties(root.get(),
5331 root->bounds(),
5332 1.f,
5333 1.f,
[email protected]f2136262013-04-26 21:10:195334 NULL,
[email protected]fb661802013-03-25 01:59:325335 dummy_max_texture_size,
5336 false,
5337 &render_surface_layer_list,
5338 false);
[email protected]94f206c12012-08-25 00:09:145339
[email protected]fb661802013-03-25 01:59:325340 // Sanity check the scenario we just created.
5341 ASSERT_EQ(1u, render_surface_layer_list.size());
5342 ASSERT_EQ(1u, root->render_surface()->layer_list().size());
[email protected]94f206c12012-08-25 00:09:145343
[email protected]fb661802013-03-25 01:59:325344 // Hit checking for any point should return a null pointer for a layer without
5345 // any touch event handler regions.
5346 gfx::Point test_point(11, 11);
5347 LayerImpl* result_layer =
5348 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5349 test_point, render_surface_layer_list);
5350 EXPECT_FALSE(result_layer);
[email protected]94f206c12012-08-25 00:09:145351
[email protected]fb661802013-03-25 01:59:325352 root->SetTouchEventHandlerRegion(touch_handler_region);
5353 // Hit checking for a point outside the layer should return a null pointer.
5354 test_point = gfx::Point(101, 101);
5355 result_layer =
5356 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5357 test_point, render_surface_layer_list);
5358 EXPECT_FALSE(result_layer);
[email protected]94f206c12012-08-25 00:09:145359
[email protected]fb661802013-03-25 01:59:325360 test_point = gfx::Point(-1, -1);
5361 result_layer =
5362 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5363 test_point, render_surface_layer_list);
5364 EXPECT_FALSE(result_layer);
[email protected]94f206c12012-08-25 00:09:145365
[email protected]fb661802013-03-25 01:59:325366 // Hit checking for a point inside the layer, but outside the touch handler
5367 // region should return a null pointer.
5368 test_point = gfx::Point(1, 1);
5369 result_layer =
5370 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5371 test_point, render_surface_layer_list);
5372 EXPECT_FALSE(result_layer);
[email protected]94f206c12012-08-25 00:09:145373
[email protected]fb661802013-03-25 01:59:325374 test_point = gfx::Point(99, 99);
5375 result_layer =
5376 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5377 test_point, render_surface_layer_list);
5378 EXPECT_FALSE(result_layer);
[email protected]94f206c12012-08-25 00:09:145379
[email protected]fb661802013-03-25 01:59:325380 // Hit checking for a point inside the touch event handler region should
5381 // return the root layer.
5382 test_point = gfx::Point(11, 11);
5383 result_layer =
5384 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5385 test_point, render_surface_layer_list);
5386 ASSERT_TRUE(result_layer);
5387 EXPECT_EQ(12345, result_layer->id());
5388
5389 test_point = gfx::Point(59, 59);
5390 result_layer =
5391 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5392 test_point, render_surface_layer_list);
5393 ASSERT_TRUE(result_layer);
5394 EXPECT_EQ(12345, result_layer->id());
[email protected]94f206c12012-08-25 00:09:145395}
5396
[email protected]fb661802013-03-25 01:59:325397TEST(LayerTreeHostCommonTest,
5398 HitCheckingTouchHandlerRegionsForUninvertibleTransform) {
5399 FakeImplProxy proxy;
5400 FakeLayerTreeHostImpl host_impl(&proxy);
5401 scoped_ptr<LayerImpl> root =
5402 LayerImpl::Create(host_impl.active_tree(), 12345);
[email protected]94f206c12012-08-25 00:09:145403
[email protected]fb661802013-03-25 01:59:325404 gfx::Transform uninvertible_transform;
5405 uninvertible_transform.matrix().setDouble(0, 0, 0.0);
5406 uninvertible_transform.matrix().setDouble(1, 1, 0.0);
5407 uninvertible_transform.matrix().setDouble(2, 2, 0.0);
5408 uninvertible_transform.matrix().setDouble(3, 3, 0.0);
5409 ASSERT_FALSE(uninvertible_transform.IsInvertible());
[email protected]94f206c12012-08-25 00:09:145410
[email protected]fb661802013-03-25 01:59:325411 gfx::Transform identity_matrix;
5412 Region touch_handler_region(gfx::Rect(10, 10, 50, 50));
5413 gfx::PointF anchor;
5414 gfx::PointF position;
5415 gfx::Size bounds(100, 100);
5416 SetLayerPropertiesForTesting(root.get(),
5417 uninvertible_transform,
5418 identity_matrix,
5419 anchor,
5420 position,
5421 bounds,
5422 false);
5423 root->SetDrawsContent(true);
5424 root->SetTouchEventHandlerRegion(touch_handler_region);
[email protected]94f206c12012-08-25 00:09:145425
[email protected]50761e92013-03-29 20:51:285426 LayerImplList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:325427 int dummy_max_texture_size = 512;
5428 LayerTreeHostCommon::CalculateDrawProperties(root.get(),
5429 root->bounds(),
5430 1.f,
5431 1.f,
[email protected]f2136262013-04-26 21:10:195432 NULL,
[email protected]fb661802013-03-25 01:59:325433 dummy_max_texture_size,
5434 false,
5435 &render_surface_layer_list,
5436 false);
[email protected]94f206c12012-08-25 00:09:145437
[email protected]fb661802013-03-25 01:59:325438 // Sanity check the scenario we just created.
5439 ASSERT_EQ(1u, render_surface_layer_list.size());
5440 ASSERT_EQ(1u, root->render_surface()->layer_list().size());
5441 ASSERT_FALSE(root->screen_space_transform().IsInvertible());
[email protected]de4afb5e2012-12-20 00:11:345442
[email protected]fb661802013-03-25 01:59:325443 // Hit checking any point should not hit the touch handler region on the
5444 // layer. If the invertible matrix is accidentally ignored and treated like an
5445 // identity, then the hit testing will incorrectly hit the layer when it
5446 // shouldn't.
5447 gfx::Point test_point(1, 1);
5448 LayerImpl* result_layer =
5449 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5450 test_point, render_surface_layer_list);
5451 EXPECT_FALSE(result_layer);
[email protected]de4afb5e2012-12-20 00:11:345452
[email protected]fb661802013-03-25 01:59:325453 test_point = gfx::Point(10, 10);
5454 result_layer =
5455 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5456 test_point, render_surface_layer_list);
5457 EXPECT_FALSE(result_layer);
[email protected]de4afb5e2012-12-20 00:11:345458
[email protected]fb661802013-03-25 01:59:325459 test_point = gfx::Point(10, 30);
5460 result_layer =
5461 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5462 test_point, render_surface_layer_list);
5463 EXPECT_FALSE(result_layer);
[email protected]de4afb5e2012-12-20 00:11:345464
[email protected]fb661802013-03-25 01:59:325465 test_point = gfx::Point(50, 50);
5466 result_layer =
5467 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5468 test_point, render_surface_layer_list);
5469 EXPECT_FALSE(result_layer);
[email protected]94f206c12012-08-25 00:09:145470
[email protected]fb661802013-03-25 01:59:325471 test_point = gfx::Point(67, 48);
5472 result_layer =
5473 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5474 test_point, render_surface_layer_list);
5475 EXPECT_FALSE(result_layer);
[email protected]94f206c12012-08-25 00:09:145476
[email protected]fb661802013-03-25 01:59:325477 test_point = gfx::Point(99, 99);
5478 result_layer =
5479 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5480 test_point, render_surface_layer_list);
5481 EXPECT_FALSE(result_layer);
[email protected]94f206c12012-08-25 00:09:145482
[email protected]fb661802013-03-25 01:59:325483 test_point = gfx::Point(-1, -1);
5484 result_layer =
5485 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5486 test_point, render_surface_layer_list);
5487 EXPECT_FALSE(result_layer);
[email protected]94f206c12012-08-25 00:09:145488}
5489
[email protected]fb661802013-03-25 01:59:325490TEST(LayerTreeHostCommonTest,
5491 HitCheckingTouchHandlerRegionsForSinglePositionedLayer) {
5492 FakeImplProxy proxy;
5493 FakeLayerTreeHostImpl host_impl(&proxy);
5494 scoped_ptr<LayerImpl> root =
5495 LayerImpl::Create(host_impl.active_tree(), 12345);
[email protected]94f206c12012-08-25 00:09:145496
[email protected]fb661802013-03-25 01:59:325497 gfx::Transform identity_matrix;
5498 Region touch_handler_region(gfx::Rect(10, 10, 50, 50));
5499 gfx::PointF anchor;
5500 // this layer is positioned, and hit testing should correctly know where the
5501 // layer is located.
5502 gfx::PointF position(50.f, 50.f);
5503 gfx::Size bounds(100, 100);
5504 SetLayerPropertiesForTesting(root.get(),
5505 identity_matrix,
5506 identity_matrix,
5507 anchor,
5508 position,
5509 bounds,
5510 false);
5511 root->SetDrawsContent(true);
5512 root->SetTouchEventHandlerRegion(touch_handler_region);
[email protected]94f206c12012-08-25 00:09:145513
[email protected]50761e92013-03-29 20:51:285514 LayerImplList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:325515 int dummy_max_texture_size = 512;
5516 LayerTreeHostCommon::CalculateDrawProperties(root.get(),
5517 root->bounds(),
5518 1.f,
5519 1.f,
[email protected]f2136262013-04-26 21:10:195520 NULL,
[email protected]fb661802013-03-25 01:59:325521 dummy_max_texture_size,
5522 false,
5523 &render_surface_layer_list,
5524 false);
[email protected]94f206c12012-08-25 00:09:145525
[email protected]fb661802013-03-25 01:59:325526 // Sanity check the scenario we just created.
5527 ASSERT_EQ(1u, render_surface_layer_list.size());
5528 ASSERT_EQ(1u, root->render_surface()->layer_list().size());
[email protected]94f206c12012-08-25 00:09:145529
[email protected]fb661802013-03-25 01:59:325530 // Hit checking for a point outside the layer should return a null pointer.
5531 gfx::Point test_point(49, 49);
5532 LayerImpl* result_layer =
5533 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5534 test_point, render_surface_layer_list);
5535 EXPECT_FALSE(result_layer);
5536
5537 // Even though the layer has a touch handler region containing (101, 101), it
5538 // should not be visible there since the root render surface would clamp it.
5539 test_point = gfx::Point(101, 101);
5540 result_layer =
5541 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5542 test_point, render_surface_layer_list);
5543 EXPECT_FALSE(result_layer);
5544
5545 // Hit checking for a point inside the layer, but outside the touch handler
5546 // region should return a null pointer.
5547 test_point = gfx::Point(51, 51);
5548 result_layer =
5549 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5550 test_point, render_surface_layer_list);
5551 EXPECT_FALSE(result_layer);
5552
5553 // Hit checking for a point inside the touch event handler region should
5554 // return the root layer.
5555 test_point = gfx::Point(61, 61);
5556 result_layer =
5557 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5558 test_point, render_surface_layer_list);
5559 ASSERT_TRUE(result_layer);
5560 EXPECT_EQ(12345, result_layer->id());
5561
5562 test_point = gfx::Point(99, 99);
5563 result_layer =
5564 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5565 test_point, render_surface_layer_list);
5566 ASSERT_TRUE(result_layer);
5567 EXPECT_EQ(12345, result_layer->id());
[email protected]94f206c12012-08-25 00:09:145568}
5569
[email protected]fb661802013-03-25 01:59:325570TEST(LayerTreeHostCommonTest,
5571 HitCheckingTouchHandlerRegionsForSingleLayerWithScaledContents) {
5572 // A layer's visible content rect is actually in the layer's content space.
5573 // The screen space transform converts from the layer's origin space to screen
5574 // space. This test makes sure that hit testing works correctly accounts for
5575 // the contents scale. A contents scale that is not 1 effectively forces a
5576 // non-identity transform between layer's content space and layer's origin
5577 // space. The hit testing code must take this into account.
5578 //
5579 // To test this, the layer is positioned at (25, 25), and is size (50, 50). If
5580 // contents scale is ignored, then hit checking will mis-interpret the visible
5581 // content rect as being larger than the actual bounds of the layer.
5582 //
5583 FakeImplProxy proxy;
5584 FakeLayerTreeHostImpl host_impl(&proxy);
5585 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1);
[email protected]94f206c12012-08-25 00:09:145586
[email protected]fb661802013-03-25 01:59:325587 gfx::Transform identity_matrix;
5588 gfx::PointF anchor;
[email protected]94f206c12012-08-25 00:09:145589
[email protected]fb661802013-03-25 01:59:325590 SetLayerPropertiesForTesting(root.get(),
5591 identity_matrix,
5592 identity_matrix,
5593 anchor,
5594 gfx::PointF(),
5595 gfx::Size(100, 100),
5596 false);
5597 {
5598 Region touch_handler_region(gfx::Rect(10, 10, 30, 30));
5599 gfx::PointF position(25.f, 25.f);
5600 gfx::Size bounds(50, 50);
5601 scoped_ptr<LayerImpl> test_layer =
5602 LayerImpl::Create(host_impl.active_tree(), 12345);
5603 SetLayerPropertiesForTesting(test_layer.get(),
5604 identity_matrix,
5605 identity_matrix,
5606 anchor,
5607 position,
5608 bounds,
5609 false);
[email protected]94f206c12012-08-25 00:09:145610
[email protected]fb661802013-03-25 01:59:325611 // override content bounds and contents scale
5612 test_layer->SetContentBounds(gfx::Size(100, 100));
5613 test_layer->SetContentsScale(2, 2);
[email protected]94f206c12012-08-25 00:09:145614
[email protected]fb661802013-03-25 01:59:325615 test_layer->SetDrawsContent(true);
5616 test_layer->SetTouchEventHandlerRegion(touch_handler_region);
5617 root->AddChild(test_layer.Pass());
5618 }
5619
[email protected]50761e92013-03-29 20:51:285620 LayerImplList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:325621 int dummy_max_texture_size = 512;
5622 LayerTreeHostCommon::CalculateDrawProperties(root.get(),
5623 root->bounds(),
5624 1.f,
5625 1.f,
[email protected]f2136262013-04-26 21:10:195626 NULL,
[email protected]fb661802013-03-25 01:59:325627 dummy_max_texture_size,
5628 false,
5629 &render_surface_layer_list,
5630 false);
5631
5632 // Sanity check the scenario we just created.
5633 // The visible content rect for test_layer is actually 100x100, even though
5634 // its layout size is 50x50, positioned at 25x25.
5635 LayerImpl* test_layer = root->children()[0];
[email protected]2c7c6702013-03-26 03:14:055636 EXPECT_RECT_EQ(gfx::Rect(0, 0, 100, 100), test_layer->visible_content_rect());
[email protected]fb661802013-03-25 01:59:325637 ASSERT_EQ(1u, render_surface_layer_list.size());
5638 ASSERT_EQ(1u, root->render_surface()->layer_list().size());
5639
5640 // Hit checking for a point outside the layer should return a null pointer
5641 // (the root layer does not draw content, so it will not be tested either).
5642 gfx::Point test_point(76, 76);
5643 LayerImpl* result_layer =
5644 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5645 test_point, render_surface_layer_list);
5646 EXPECT_FALSE(result_layer);
5647
5648 // Hit checking for a point inside the layer, but outside the touch handler
5649 // region should return a null pointer.
5650 test_point = gfx::Point(26, 26);
5651 result_layer =
5652 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5653 test_point, render_surface_layer_list);
5654 EXPECT_FALSE(result_layer);
5655
5656 test_point = gfx::Point(34, 34);
5657 result_layer =
5658 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5659 test_point, render_surface_layer_list);
5660 EXPECT_FALSE(result_layer);
5661
5662 test_point = gfx::Point(65, 65);
5663 result_layer =
5664 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5665 test_point, render_surface_layer_list);
5666 EXPECT_FALSE(result_layer);
5667
5668 test_point = gfx::Point(74, 74);
5669 result_layer =
5670 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5671 test_point, render_surface_layer_list);
5672 EXPECT_FALSE(result_layer);
5673
5674 // Hit checking for a point inside the touch event handler region should
5675 // return the root layer.
5676 test_point = gfx::Point(35, 35);
5677 result_layer =
5678 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5679 test_point, render_surface_layer_list);
5680 ASSERT_TRUE(result_layer);
5681 EXPECT_EQ(12345, result_layer->id());
5682
5683 test_point = gfx::Point(64, 64);
5684 result_layer =
5685 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5686 test_point, render_surface_layer_list);
5687 ASSERT_TRUE(result_layer);
5688 EXPECT_EQ(12345, result_layer->id());
[email protected]94f206c12012-08-25 00:09:145689}
5690
[email protected]fb661802013-03-25 01:59:325691TEST(LayerTreeHostCommonTest,
5692 HitCheckingTouchHandlerRegionsForSingleLayerWithDeviceScale) {
5693 // The layer's device_scale_factor and page_scale_factor should scale the
5694 // content rect and we should be able to hit the touch handler region by
5695 // scaling the points accordingly.
5696 FakeImplProxy proxy;
5697 FakeLayerTreeHostImpl host_impl(&proxy);
5698 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1);
[email protected]94f206c12012-08-25 00:09:145699
[email protected]fb661802013-03-25 01:59:325700 gfx::Transform identity_matrix;
5701 gfx::PointF anchor;
5702 // Set the bounds of the root layer big enough to fit the child when scaled.
5703 SetLayerPropertiesForTesting(root.get(),
5704 identity_matrix,
5705 identity_matrix,
5706 anchor,
5707 gfx::PointF(),
5708 gfx::Size(100, 100),
5709 false);
5710 {
5711 Region touch_handler_region(gfx::Rect(10, 10, 30, 30));
5712 gfx::PointF position(25.f, 25.f);
5713 gfx::Size bounds(50, 50);
5714 scoped_ptr<LayerImpl> test_layer =
5715 LayerImpl::Create(host_impl.active_tree(), 12345);
5716 SetLayerPropertiesForTesting(test_layer.get(),
5717 identity_matrix,
5718 identity_matrix,
5719 anchor,
5720 position,
5721 bounds,
5722 false);
[email protected]94f206c12012-08-25 00:09:145723
[email protected]fb661802013-03-25 01:59:325724 test_layer->SetDrawsContent(true);
5725 test_layer->SetTouchEventHandlerRegion(touch_handler_region);
5726 root->AddChild(test_layer.Pass());
5727 }
[email protected]94f206c12012-08-25 00:09:145728
[email protected]50761e92013-03-29 20:51:285729 LayerImplList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:325730 int dummy_max_texture_size = 512;
5731 float device_scale_factor = 3.f;
5732 float page_scale_factor = 5.f;
[email protected]fb661802013-03-25 01:59:325733 gfx::Size scaled_bounds_for_root = gfx::ToCeiledSize(
5734 gfx::ScaleSize(root->bounds(), device_scale_factor * page_scale_factor));
5735 LayerTreeHostCommon::CalculateDrawProperties(root.get(),
5736 scaled_bounds_for_root,
5737 device_scale_factor,
[email protected]f2136262013-04-26 21:10:195738 page_scale_factor,
5739 root.get(),
[email protected]fb661802013-03-25 01:59:325740 dummy_max_texture_size,
5741 false,
5742 &render_surface_layer_list,
5743 false);
[email protected]94f206c12012-08-25 00:09:145744
[email protected]fb661802013-03-25 01:59:325745 // Sanity check the scenario we just created.
5746 // The visible content rect for test_layer is actually 100x100, even though
5747 // its layout size is 50x50, positioned at 25x25.
5748 LayerImpl* test_layer = root->children()[0];
5749 ASSERT_EQ(1u, render_surface_layer_list.size());
5750 ASSERT_EQ(1u, root->render_surface()->layer_list().size());
[email protected]94f206c12012-08-25 00:09:145751
[email protected]fb661802013-03-25 01:59:325752 // Check whether the child layer fits into the root after scaled.
5753 EXPECT_RECT_EQ(gfx::Rect(test_layer->content_bounds()),
5754 test_layer->visible_content_rect());
5755
5756 // Hit checking for a point outside the layer should return a null pointer
5757 // (the root layer does not draw content, so it will not be tested either).
5758 gfx::PointF test_point(76.f, 76.f);
5759 test_point =
5760 gfx::ScalePoint(test_point, device_scale_factor * page_scale_factor);
5761 LayerImpl* result_layer =
5762 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5763 test_point, render_surface_layer_list);
5764 EXPECT_FALSE(result_layer);
5765
5766 // Hit checking for a point inside the layer, but outside the touch handler
5767 // region should return a null pointer.
5768 test_point = gfx::Point(26, 26);
5769 test_point =
5770 gfx::ScalePoint(test_point, device_scale_factor * page_scale_factor);
5771 result_layer =
5772 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5773 test_point, render_surface_layer_list);
5774 EXPECT_FALSE(result_layer);
5775
5776 test_point = gfx::Point(34, 34);
5777 test_point =
5778 gfx::ScalePoint(test_point, device_scale_factor * page_scale_factor);
5779 result_layer =
5780 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5781 test_point, render_surface_layer_list);
5782 EXPECT_FALSE(result_layer);
5783
5784 test_point = gfx::Point(65, 65);
5785 test_point =
5786 gfx::ScalePoint(test_point, device_scale_factor * page_scale_factor);
5787 result_layer =
5788 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5789 test_point, render_surface_layer_list);
5790 EXPECT_FALSE(result_layer);
5791
5792 test_point = gfx::Point(74, 74);
5793 test_point =
5794 gfx::ScalePoint(test_point, device_scale_factor * page_scale_factor);
5795 result_layer =
5796 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5797 test_point, render_surface_layer_list);
5798 EXPECT_FALSE(result_layer);
5799
5800 // Hit checking for a point inside the touch event handler region should
5801 // return the root layer.
5802 test_point = gfx::Point(35, 35);
5803 test_point =
5804 gfx::ScalePoint(test_point, device_scale_factor * page_scale_factor);
5805 result_layer =
5806 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5807 test_point, render_surface_layer_list);
5808 ASSERT_TRUE(result_layer);
5809 EXPECT_EQ(12345, result_layer->id());
5810
5811 test_point = gfx::Point(64, 64);
5812 test_point =
5813 gfx::ScalePoint(test_point, device_scale_factor * page_scale_factor);
5814 result_layer =
5815 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5816 test_point, render_surface_layer_list);
5817 ASSERT_TRUE(result_layer);
5818 EXPECT_EQ(12345, result_layer->id());
[email protected]94f206c12012-08-25 00:09:145819}
5820
[email protected]fb661802013-03-25 01:59:325821TEST(LayerTreeHostCommonTest,
5822 HitCheckingTouchHandlerRegionsForSimpleClippedLayer) {
5823 // Test that hit-checking will only work for the visible portion of a layer,
5824 // and not the entire layer bounds. Here we just test the simple axis-aligned
5825 // case.
5826 gfx::Transform identity_matrix;
5827 gfx::PointF anchor;
[email protected]94f206c12012-08-25 00:09:145828
[email protected]fb661802013-03-25 01:59:325829 FakeImplProxy proxy;
5830 FakeLayerTreeHostImpl host_impl(&proxy);
5831 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1);
5832 SetLayerPropertiesForTesting(root.get(),
5833 identity_matrix,
5834 identity_matrix,
5835 anchor,
5836 gfx::PointF(),
5837 gfx::Size(100, 100),
5838 false);
5839 {
5840 scoped_ptr<LayerImpl> clipping_layer =
5841 LayerImpl::Create(host_impl.active_tree(), 123);
5842 // this layer is positioned, and hit testing should correctly know where the
5843 // layer is located.
5844 gfx::PointF position(25.f, 25.f);
5845 gfx::Size bounds(50, 50);
5846 SetLayerPropertiesForTesting(clipping_layer.get(),
5847 identity_matrix,
5848 identity_matrix,
5849 anchor,
5850 position,
5851 bounds,
5852 false);
5853 clipping_layer->SetMasksToBounds(true);
[email protected]94f206c12012-08-25 00:09:145854
[email protected]fb661802013-03-25 01:59:325855 scoped_ptr<LayerImpl> child =
5856 LayerImpl::Create(host_impl.active_tree(), 456);
5857 Region touch_handler_region(gfx::Rect(10, 10, 50, 50));
5858 position = gfx::PointF(-50.f, -50.f);
5859 bounds = gfx::Size(300, 300);
5860 SetLayerPropertiesForTesting(child.get(),
5861 identity_matrix,
5862 identity_matrix,
5863 anchor,
5864 position,
5865 bounds,
5866 false);
5867 child->SetDrawsContent(true);
5868 child->SetTouchEventHandlerRegion(touch_handler_region);
5869 clipping_layer->AddChild(child.Pass());
5870 root->AddChild(clipping_layer.Pass());
5871 }
[email protected]94f206c12012-08-25 00:09:145872
[email protected]50761e92013-03-29 20:51:285873 LayerImplList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:325874 int dummy_max_texture_size = 512;
5875 LayerTreeHostCommon::CalculateDrawProperties(root.get(),
5876 root->bounds(),
5877 1.f,
5878 1.f,
[email protected]f2136262013-04-26 21:10:195879 NULL,
[email protected]fb661802013-03-25 01:59:325880 dummy_max_texture_size,
5881 false,
5882 &render_surface_layer_list,
5883 false);
5884
5885 // Sanity check the scenario we just created.
5886 ASSERT_EQ(1u, render_surface_layer_list.size());
5887 ASSERT_EQ(1u, root->render_surface()->layer_list().size());
5888 ASSERT_EQ(456, root->render_surface()->layer_list()[0]->id());
5889
5890 // Hit checking for a point outside the layer should return a null pointer.
5891 // Despite the child layer being very large, it should be clipped to the root
5892 // layer's bounds.
5893 gfx::Point test_point(24, 24);
5894 LayerImpl* result_layer =
5895 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5896 test_point, render_surface_layer_list);
5897 EXPECT_FALSE(result_layer);
5898
5899 // Hit checking for a point inside the layer, but outside the touch handler
5900 // region should return a null pointer.
5901 test_point = gfx::Point(35, 35);
5902 result_layer =
5903 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5904 test_point, render_surface_layer_list);
5905 EXPECT_FALSE(result_layer);
5906
5907 test_point = gfx::Point(74, 74);
5908 result_layer =
5909 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5910 test_point, render_surface_layer_list);
5911 EXPECT_FALSE(result_layer);
5912
5913 // Hit checking for a point inside the touch event handler region should
5914 // return the root layer.
5915 test_point = gfx::Point(25, 25);
5916 result_layer =
5917 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5918 test_point, render_surface_layer_list);
5919 ASSERT_TRUE(result_layer);
5920 EXPECT_EQ(456, result_layer->id());
5921
5922 test_point = gfx::Point(34, 34);
5923 result_layer =
5924 LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
5925 test_point, render_surface_layer_list);
5926 ASSERT_TRUE(result_layer);
5927 EXPECT_EQ(456, result_layer->id());
[email protected]94f206c12012-08-25 00:09:145928}
5929
[email protected]fb661802013-03-25 01:59:325930class NoScaleContentLayer : public ContentLayer {
5931 public:
5932 static scoped_refptr<NoScaleContentLayer> Create(ContentLayerClient* client) {
5933 return make_scoped_refptr(new NoScaleContentLayer(client));
5934 }
[email protected]94f206c12012-08-25 00:09:145935
[email protected]fb661802013-03-25 01:59:325936 virtual void CalculateContentsScale(float ideal_contents_scale,
5937 bool animating_transform_to_screen,
5938 float* contents_scale_x,
5939 float* contents_scale_y,
5940 gfx::Size* content_bounds) OVERRIDE {
5941 // Skip over the ContentLayer to the base Layer class.
5942 Layer::CalculateContentsScale(ideal_contents_scale,
5943 animating_transform_to_screen,
5944 contents_scale_x,
5945 contents_scale_y,
5946 content_bounds);
5947 }
[email protected]94f206c12012-08-25 00:09:145948
[email protected]fb661802013-03-25 01:59:325949 protected:
5950 explicit NoScaleContentLayer(ContentLayerClient* client)
5951 : ContentLayer(client) {}
5952 virtual ~NoScaleContentLayer() {}
[email protected]518ee582012-10-24 18:29:445953};
5954
[email protected]fb661802013-03-25 01:59:325955scoped_refptr<NoScaleContentLayer> CreateNoScaleDrawableContentLayer(
5956 ContentLayerClient* delegate) {
5957 scoped_refptr<NoScaleContentLayer> to_return =
5958 NoScaleContentLayer::Create(delegate);
5959 to_return->SetIsDrawable(true);
5960 return to_return;
[email protected]518ee582012-10-24 18:29:445961}
5962
[email protected]fb661802013-03-25 01:59:325963TEST(LayerTreeHostCommonTest, LayerTransformsInHighDPI) {
5964 // Verify draw and screen space transforms of layers not in a surface.
5965 MockContentLayerClient delegate;
5966 gfx::Transform identity_matrix;
[email protected]94f206c12012-08-25 00:09:145967
[email protected]fb661802013-03-25 01:59:325968 scoped_refptr<ContentLayer> parent = CreateDrawableContentLayer(&delegate);
5969 SetLayerPropertiesForTesting(parent.get(),
5970 identity_matrix,
5971 identity_matrix,
5972 gfx::PointF(),
5973 gfx::PointF(),
5974 gfx::Size(100, 100),
5975 true);
[email protected]94f206c12012-08-25 00:09:145976
[email protected]fb661802013-03-25 01:59:325977 scoped_refptr<ContentLayer> child = CreateDrawableContentLayer(&delegate);
5978 SetLayerPropertiesForTesting(child.get(),
5979 identity_matrix,
5980 identity_matrix,
5981 gfx::PointF(),
5982 gfx::PointF(2.f, 2.f),
5983 gfx::Size(10, 10),
5984 true);
[email protected]94f206c12012-08-25 00:09:145985
[email protected]fb661802013-03-25 01:59:325986 scoped_refptr<ContentLayer> child_empty =
5987 CreateDrawableContentLayer(&delegate);
5988 SetLayerPropertiesForTesting(child_empty.get(),
5989 identity_matrix,
5990 identity_matrix,
5991 gfx::PointF(),
5992 gfx::PointF(2.f, 2.f),
5993 gfx::Size(),
5994 true);
[email protected]f89f5632012-11-14 23:34:455995
[email protected]fb661802013-03-25 01:59:325996 scoped_refptr<NoScaleContentLayer> child_no_scale =
5997 CreateNoScaleDrawableContentLayer(&delegate);
5998 SetLayerPropertiesForTesting(child_no_scale.get(),
5999 identity_matrix,
6000 identity_matrix,
6001 gfx::PointF(),
6002 gfx::PointF(2.f, 2.f),
6003 gfx::Size(10, 10),
6004 true);
[email protected]94f206c12012-08-25 00:09:146005
[email protected]fb661802013-03-25 01:59:326006 parent->AddChild(child);
6007 parent->AddChild(child_empty);
6008 parent->AddChild(child_no_scale);
[email protected]94f206c12012-08-25 00:09:146009
[email protected]50761e92013-03-29 20:51:286010 LayerList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:326011 int dummy_max_texture_size = 512;
[email protected]94f206c12012-08-25 00:09:146012
[email protected]fb661802013-03-25 01:59:326013 float device_scale_factor = 2.5f;
6014 float page_scale_factor = 1.f;
[email protected]94f206c12012-08-25 00:09:146015
[email protected]fb661802013-03-25 01:59:326016 LayerTreeHostCommon::CalculateDrawProperties(parent.get(),
6017 parent->bounds(),
6018 device_scale_factor,
6019 page_scale_factor,
[email protected]f2136262013-04-26 21:10:196020 parent.get(),
[email protected]fb661802013-03-25 01:59:326021 dummy_max_texture_size,
6022 false,
6023 &render_surface_layer_list);
[email protected]518ee582012-10-24 18:29:446024
[email protected]fb661802013-03-25 01:59:326025 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor, parent);
6026 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor, child);
6027 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor,
6028 child_empty);
6029 EXPECT_CONTENTS_SCALE_EQ(1, child_no_scale);
[email protected]94f206c12012-08-25 00:09:146030
[email protected]fb661802013-03-25 01:59:326031 EXPECT_EQ(1u, render_surface_layer_list.size());
[email protected]94f206c12012-08-25 00:09:146032
[email protected]fb661802013-03-25 01:59:326033 // Verify parent transforms
6034 gfx::Transform expected_parent_transform;
6035 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_parent_transform,
6036 parent->screen_space_transform());
6037 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_parent_transform,
6038 parent->draw_transform());
[email protected]94f206c12012-08-25 00:09:146039
[email protected]fb661802013-03-25 01:59:326040 // Verify results of transformed parent rects
[email protected]2c7c6702013-03-26 03:14:056041 gfx::RectF parent_content_bounds(parent->content_bounds());
[email protected]94f206c12012-08-25 00:09:146042
[email protected]fb661802013-03-25 01:59:326043 gfx::RectF parent_draw_rect =
6044 MathUtil::MapClippedRect(parent->draw_transform(), parent_content_bounds);
6045 gfx::RectF parent_screen_space_rect = MathUtil::MapClippedRect(
6046 parent->screen_space_transform(), parent_content_bounds);
[email protected]94f206c12012-08-25 00:09:146047
[email protected]2c7c6702013-03-26 03:14:056048 gfx::RectF expected_parent_draw_rect(parent->bounds());
[email protected]fb661802013-03-25 01:59:326049 expected_parent_draw_rect.Scale(device_scale_factor);
6050 EXPECT_FLOAT_RECT_EQ(expected_parent_draw_rect, parent_draw_rect);
6051 EXPECT_FLOAT_RECT_EQ(expected_parent_draw_rect, parent_screen_space_rect);
[email protected]94f206c12012-08-25 00:09:146052
[email protected]fb661802013-03-25 01:59:326053 // Verify child and child_empty transforms. They should match.
6054 gfx::Transform expected_child_transform;
6055 expected_child_transform.Translate(
6056 device_scale_factor * child->position().x(),
6057 device_scale_factor * child->position().y());
6058 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_transform,
6059 child->draw_transform());
6060 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_transform,
6061 child->screen_space_transform());
6062 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_transform,
6063 child_empty->draw_transform());
6064 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_transform,
6065 child_empty->screen_space_transform());
[email protected]94f206c12012-08-25 00:09:146066
[email protected]fb661802013-03-25 01:59:326067 // Verify results of transformed child and child_empty rects. They should
6068 // match.
[email protected]2c7c6702013-03-26 03:14:056069 gfx::RectF child_content_bounds(child->content_bounds());
[email protected]94f206c12012-08-25 00:09:146070
[email protected]fb661802013-03-25 01:59:326071 gfx::RectF child_draw_rect =
6072 MathUtil::MapClippedRect(child->draw_transform(), child_content_bounds);
6073 gfx::RectF child_screen_space_rect = MathUtil::MapClippedRect(
6074 child->screen_space_transform(), child_content_bounds);
[email protected]94f206c12012-08-25 00:09:146075
[email protected]fb661802013-03-25 01:59:326076 gfx::RectF child_empty_draw_rect = MathUtil::MapClippedRect(
6077 child_empty->draw_transform(), child_content_bounds);
6078 gfx::RectF child_empty_screen_space_rect = MathUtil::MapClippedRect(
6079 child_empty->screen_space_transform(), child_content_bounds);
[email protected]f89f5632012-11-14 23:34:456080
[email protected]fb661802013-03-25 01:59:326081 gfx::RectF expected_child_draw_rect(child->position(), child->bounds());
6082 expected_child_draw_rect.Scale(device_scale_factor);
6083 EXPECT_FLOAT_RECT_EQ(expected_child_draw_rect, child_draw_rect);
6084 EXPECT_FLOAT_RECT_EQ(expected_child_draw_rect, child_screen_space_rect);
6085 EXPECT_FLOAT_RECT_EQ(expected_child_draw_rect, child_empty_draw_rect);
6086 EXPECT_FLOAT_RECT_EQ(expected_child_draw_rect, child_empty_screen_space_rect);
[email protected]94f206c12012-08-25 00:09:146087
[email protected]fb661802013-03-25 01:59:326088 // Verify child_no_scale transforms
6089 gfx::Transform expected_child_no_scale_transform = child->draw_transform();
6090 // All transforms operate on content rects. The child's content rect
6091 // incorporates device scale, but the child_no_scale does not; add it here.
6092 expected_child_no_scale_transform.Scale(device_scale_factor,
6093 device_scale_factor);
6094 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_no_scale_transform,
6095 child_no_scale->draw_transform());
6096 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_no_scale_transform,
6097 child_no_scale->screen_space_transform());
[email protected]94f206c12012-08-25 00:09:146098}
6099
[email protected]fb661802013-03-25 01:59:326100TEST(LayerTreeHostCommonTest, SurfaceLayerTransformsInHighDPI) {
6101 // Verify draw and screen space transforms of layers in a surface.
6102 MockContentLayerClient delegate;
6103 gfx::Transform identity_matrix;
[email protected]1b30e8e2012-12-21 02:59:096104
[email protected]fb661802013-03-25 01:59:326105 gfx::Transform perspective_matrix;
6106 perspective_matrix.ApplyPerspectiveDepth(2.0);
[email protected]1b30e8e2012-12-21 02:59:096107
[email protected]fb661802013-03-25 01:59:326108 gfx::Transform scale_small_matrix;
6109 scale_small_matrix.Scale(1.0 / 10.0, 1.0 / 12.0);
[email protected]1b30e8e2012-12-21 02:59:096110
[email protected]fb661802013-03-25 01:59:326111 scoped_refptr<ContentLayer> parent = CreateDrawableContentLayer(&delegate);
6112 SetLayerPropertiesForTesting(parent.get(),
6113 identity_matrix,
6114 identity_matrix,
6115 gfx::PointF(),
6116 gfx::PointF(),
6117 gfx::Size(100, 100),
6118 true);
[email protected]1b30e8e2012-12-21 02:59:096119
[email protected]fb661802013-03-25 01:59:326120 scoped_refptr<ContentLayer> perspective_surface =
6121 CreateDrawableContentLayer(&delegate);
6122 SetLayerPropertiesForTesting(perspective_surface.get(),
6123 perspective_matrix * scale_small_matrix,
6124 identity_matrix,
6125 gfx::PointF(),
6126 gfx::PointF(2.f, 2.f),
6127 gfx::Size(10, 10),
6128 true);
[email protected]1b30e8e2012-12-21 02:59:096129
[email protected]fb661802013-03-25 01:59:326130 scoped_refptr<ContentLayer> scale_surface =
6131 CreateDrawableContentLayer(&delegate);
6132 SetLayerPropertiesForTesting(scale_surface.get(),
6133 scale_small_matrix,
6134 identity_matrix,
6135 gfx::PointF(),
6136 gfx::PointF(2.f, 2.f),
6137 gfx::Size(10, 10),
6138 true);
[email protected]1b30e8e2012-12-21 02:59:096139
[email protected]fb661802013-03-25 01:59:326140 perspective_surface->SetForceRenderSurface(true);
6141 scale_surface->SetForceRenderSurface(true);
[email protected]1b30e8e2012-12-21 02:59:096142
[email protected]fb661802013-03-25 01:59:326143 parent->AddChild(perspective_surface);
6144 parent->AddChild(scale_surface);
[email protected]1b30e8e2012-12-21 02:59:096145
[email protected]50761e92013-03-29 20:51:286146 LayerList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:326147 int dummy_max_texture_size = 512;
[email protected]1b30e8e2012-12-21 02:59:096148
[email protected]fb661802013-03-25 01:59:326149 float device_scale_factor = 2.5f;
6150 float page_scale_factor = 3.f;
[email protected]1b30e8e2012-12-21 02:59:096151
[email protected]fb661802013-03-25 01:59:326152 LayerTreeHostCommon::CalculateDrawProperties(parent.get(),
6153 parent->bounds(),
6154 device_scale_factor,
6155 page_scale_factor,
[email protected]f2136262013-04-26 21:10:196156 parent.get(),
[email protected]fb661802013-03-25 01:59:326157 dummy_max_texture_size,
6158 false,
6159 &render_surface_layer_list);
[email protected]1b30e8e2012-12-21 02:59:096160
[email protected]fb661802013-03-25 01:59:326161 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor, parent);
6162 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor,
6163 perspective_surface);
6164 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor,
6165 scale_surface);
[email protected]1b30e8e2012-12-21 02:59:096166
[email protected]fb661802013-03-25 01:59:326167 EXPECT_EQ(3u, render_surface_layer_list.size());
[email protected]1b30e8e2012-12-21 02:59:096168
[email protected]fb661802013-03-25 01:59:326169 gfx::Transform expected_parent_draw_transform;
6170 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_parent_draw_transform,
6171 parent->draw_transform());
[email protected]1b30e8e2012-12-21 02:59:096172
[email protected]fb661802013-03-25 01:59:326173 // The scaled surface is rendered at its appropriate scale, and drawn 1:1
6174 // into its target.
6175 gfx::Transform expected_scale_surface_draw_transform;
6176 expected_scale_surface_draw_transform.Translate(
6177 device_scale_factor * page_scale_factor * scale_surface->position().x(),
6178 device_scale_factor * page_scale_factor * scale_surface->position().y());
6179 gfx::Transform expected_scale_surface_layer_draw_transform;
6180 expected_scale_surface_layer_draw_transform.PreconcatTransform(
6181 scale_small_matrix);
6182 EXPECT_TRANSFORMATION_MATRIX_EQ(
6183 expected_scale_surface_draw_transform,
6184 scale_surface->render_surface()->draw_transform());
6185 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_scale_surface_layer_draw_transform,
6186 scale_surface->draw_transform());
[email protected]1b30e8e2012-12-21 02:59:096187
[email protected]fb661802013-03-25 01:59:326188 // The scale for the perspective surface is not known, so it is rendered 1:1
6189 // with the screen, and then scaled during drawing.
6190 gfx::Transform expected_perspective_surface_draw_transform;
6191 expected_perspective_surface_draw_transform.Translate(
6192 device_scale_factor * page_scale_factor *
6193 perspective_surface->position().x(),
6194 device_scale_factor * page_scale_factor *
6195 perspective_surface->position().y());
6196 expected_perspective_surface_draw_transform.PreconcatTransform(
6197 perspective_matrix);
6198 expected_perspective_surface_draw_transform.PreconcatTransform(
6199 scale_small_matrix);
6200 gfx::Transform expected_perspective_surface_layer_draw_transform;
6201 EXPECT_TRANSFORMATION_MATRIX_EQ(
6202 expected_perspective_surface_draw_transform,
6203 perspective_surface->render_surface()->draw_transform());
6204 EXPECT_TRANSFORMATION_MATRIX_EQ(
6205 expected_perspective_surface_layer_draw_transform,
6206 perspective_surface->draw_transform());
[email protected]1b30e8e2012-12-21 02:59:096207}
6208
[email protected]fb661802013-03-25 01:59:326209TEST(LayerTreeHostCommonTest,
6210 LayerTransformsInHighDPIAccurateScaleZeroChildPosition) {
6211 // Verify draw and screen space transforms of layers not in a surface.
6212 MockContentLayerClient delegate;
6213 gfx::Transform identity_matrix;
[email protected]904e9132012-11-01 00:12:476214
[email protected]fb661802013-03-25 01:59:326215 scoped_refptr<ContentLayer> parent = CreateDrawableContentLayer(&delegate);
6216 SetLayerPropertiesForTesting(parent.get(),
6217 identity_matrix,
6218 identity_matrix,
6219 gfx::PointF(),
6220 gfx::PointF(),
6221 gfx::Size(133, 133),
6222 true);
[email protected]904e9132012-11-01 00:12:476223
[email protected]fb661802013-03-25 01:59:326224 scoped_refptr<ContentLayer> child = CreateDrawableContentLayer(&delegate);
6225 SetLayerPropertiesForTesting(child.get(),
6226 identity_matrix,
6227 identity_matrix,
6228 gfx::PointF(),
6229 gfx::PointF(),
6230 gfx::Size(13, 13),
6231 true);
[email protected]904e9132012-11-01 00:12:476232
[email protected]fb661802013-03-25 01:59:326233 scoped_refptr<NoScaleContentLayer> child_no_scale =
6234 CreateNoScaleDrawableContentLayer(&delegate);
6235 SetLayerPropertiesForTesting(child_no_scale.get(),
6236 identity_matrix,
6237 identity_matrix,
6238 gfx::PointF(),
6239 gfx::PointF(),
6240 gfx::Size(13, 13),
6241 true);
[email protected]904e9132012-11-01 00:12:476242
[email protected]fb661802013-03-25 01:59:326243 parent->AddChild(child);
6244 parent->AddChild(child_no_scale);
[email protected]904e9132012-11-01 00:12:476245
[email protected]50761e92013-03-29 20:51:286246 LayerList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:326247 int dummy_max_texture_size = 512;
[email protected]904e9132012-11-01 00:12:476248
[email protected]fb661802013-03-25 01:59:326249 float device_scale_factor = 1.7f;
6250 float page_scale_factor = 1.f;
[email protected]904e9132012-11-01 00:12:476251
[email protected]fb661802013-03-25 01:59:326252 LayerTreeHostCommon::CalculateDrawProperties(parent.get(),
6253 parent->bounds(),
6254 device_scale_factor,
6255 page_scale_factor,
[email protected]f2136262013-04-26 21:10:196256 parent.get(),
[email protected]fb661802013-03-25 01:59:326257 dummy_max_texture_size,
6258 false,
6259 &render_surface_layer_list);
[email protected]904e9132012-11-01 00:12:476260
[email protected]fb661802013-03-25 01:59:326261 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor, parent);
6262 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor, child);
6263 EXPECT_CONTENTS_SCALE_EQ(1, child_no_scale);
[email protected]904e9132012-11-01 00:12:476264
[email protected]fb661802013-03-25 01:59:326265 EXPECT_EQ(1u, render_surface_layer_list.size());
[email protected]904e9132012-11-01 00:12:476266
[email protected]fb661802013-03-25 01:59:326267 // Verify parent transforms
6268 gfx::Transform expected_parent_transform;
6269 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_parent_transform,
6270 parent->screen_space_transform());
6271 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_parent_transform,
6272 parent->draw_transform());
[email protected]904e9132012-11-01 00:12:476273
[email protected]fb661802013-03-25 01:59:326274 // Verify results of transformed parent rects
[email protected]2c7c6702013-03-26 03:14:056275 gfx::RectF parent_content_bounds(parent->content_bounds());
[email protected]904e9132012-11-01 00:12:476276
[email protected]fb661802013-03-25 01:59:326277 gfx::RectF parent_draw_rect =
6278 MathUtil::MapClippedRect(parent->draw_transform(), parent_content_bounds);
6279 gfx::RectF parent_screen_space_rect = MathUtil::MapClippedRect(
6280 parent->screen_space_transform(), parent_content_bounds);
[email protected]904e9132012-11-01 00:12:476281
[email protected]2c7c6702013-03-26 03:14:056282 gfx::RectF expected_parent_draw_rect(parent->bounds());
[email protected]fb661802013-03-25 01:59:326283 expected_parent_draw_rect.Scale(device_scale_factor);
6284 expected_parent_draw_rect.set_width(ceil(expected_parent_draw_rect.width()));
6285 expected_parent_draw_rect.set_height(
6286 ceil(expected_parent_draw_rect.height()));
6287 EXPECT_FLOAT_RECT_EQ(expected_parent_draw_rect, parent_draw_rect);
6288 EXPECT_FLOAT_RECT_EQ(expected_parent_draw_rect, parent_screen_space_rect);
[email protected]904e9132012-11-01 00:12:476289
[email protected]fb661802013-03-25 01:59:326290 // Verify child transforms
6291 gfx::Transform expected_child_transform;
6292 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_transform,
6293 child->draw_transform());
6294 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_transform,
6295 child->screen_space_transform());
[email protected]904e9132012-11-01 00:12:476296
[email protected]fb661802013-03-25 01:59:326297 // Verify results of transformed child rects
[email protected]2c7c6702013-03-26 03:14:056298 gfx::RectF child_content_bounds(child->content_bounds());
[email protected]904e9132012-11-01 00:12:476299
[email protected]fb661802013-03-25 01:59:326300 gfx::RectF child_draw_rect =
6301 MathUtil::MapClippedRect(child->draw_transform(), child_content_bounds);
6302 gfx::RectF child_screen_space_rect = MathUtil::MapClippedRect(
6303 child->screen_space_transform(), child_content_bounds);
[email protected]904e9132012-11-01 00:12:476304
[email protected]2c7c6702013-03-26 03:14:056305 gfx::RectF expected_child_draw_rect(child->bounds());
[email protected]fb661802013-03-25 01:59:326306 expected_child_draw_rect.Scale(device_scale_factor);
6307 expected_child_draw_rect.set_width(ceil(expected_child_draw_rect.width()));
6308 expected_child_draw_rect.set_height(ceil(expected_child_draw_rect.height()));
6309 EXPECT_FLOAT_RECT_EQ(expected_child_draw_rect, child_draw_rect);
6310 EXPECT_FLOAT_RECT_EQ(expected_child_draw_rect, child_screen_space_rect);
[email protected]904e9132012-11-01 00:12:476311
[email protected]fb661802013-03-25 01:59:326312 // Verify child_no_scale transforms
6313 gfx::Transform expected_child_no_scale_transform = child->draw_transform();
6314 // All transforms operate on content rects. The child's content rect
6315 // incorporates device scale, but the child_no_scale does not; add it here.
6316 expected_child_no_scale_transform.Scale(device_scale_factor,
6317 device_scale_factor);
6318 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_no_scale_transform,
6319 child_no_scale->draw_transform());
6320 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_no_scale_transform,
6321 child_no_scale->screen_space_transform());
[email protected]904e9132012-11-01 00:12:476322}
6323
[email protected]fb661802013-03-25 01:59:326324TEST(LayerTreeHostCommonTest, ContentsScale) {
6325 MockContentLayerClient delegate;
6326 gfx::Transform identity_matrix;
[email protected]518ee582012-10-24 18:29:446327
[email protected]fb661802013-03-25 01:59:326328 gfx::Transform parent_scale_matrix;
6329 double initial_parent_scale = 1.75;
6330 parent_scale_matrix.Scale(initial_parent_scale, initial_parent_scale);
[email protected]518ee582012-10-24 18:29:446331
[email protected]fb661802013-03-25 01:59:326332 gfx::Transform child_scale_matrix;
6333 double initial_child_scale = 1.25;
6334 child_scale_matrix.Scale(initial_child_scale, initial_child_scale);
[email protected]518ee582012-10-24 18:29:446335
[email protected]fb661802013-03-25 01:59:326336 float fixed_raster_scale = 2.5f;
[email protected]518ee582012-10-24 18:29:446337
[email protected]fb661802013-03-25 01:59:326338 scoped_refptr<ContentLayer> parent = CreateDrawableContentLayer(&delegate);
6339 SetLayerPropertiesForTesting(parent.get(),
6340 parent_scale_matrix,
6341 identity_matrix,
6342 gfx::PointF(),
6343 gfx::PointF(),
6344 gfx::Size(100, 100),
6345 true);
[email protected]518ee582012-10-24 18:29:446346
[email protected]fb661802013-03-25 01:59:326347 scoped_refptr<ContentLayer> child_scale =
6348 CreateDrawableContentLayer(&delegate);
6349 SetLayerPropertiesForTesting(child_scale.get(),
6350 child_scale_matrix,
6351 identity_matrix,
6352 gfx::PointF(),
6353 gfx::PointF(2.f, 2.f),
6354 gfx::Size(10, 10),
6355 true);
[email protected]518ee582012-10-24 18:29:446356
[email protected]fb661802013-03-25 01:59:326357 scoped_refptr<ContentLayer> child_empty =
6358 CreateDrawableContentLayer(&delegate);
6359 SetLayerPropertiesForTesting(child_empty.get(),
6360 child_scale_matrix,
6361 identity_matrix,
6362 gfx::PointF(),
6363 gfx::PointF(2.f, 2.f),
6364 gfx::Size(),
6365 true);
[email protected]f89f5632012-11-14 23:34:456366
[email protected]fb661802013-03-25 01:59:326367 scoped_refptr<NoScaleContentLayer> child_no_scale =
6368 CreateNoScaleDrawableContentLayer(&delegate);
6369 SetLayerPropertiesForTesting(child_no_scale.get(),
6370 child_scale_matrix,
6371 identity_matrix,
6372 gfx::PointF(),
6373 gfx::PointF(12.f, 12.f),
6374 gfx::Size(10, 10),
6375 true);
[email protected]518ee582012-10-24 18:29:446376
[email protected]fb661802013-03-25 01:59:326377 scoped_refptr<ContentLayer> child_no_auto_scale =
6378 CreateDrawableContentLayer(&delegate);
6379 SetLayerPropertiesForTesting(child_no_auto_scale.get(),
6380 child_scale_matrix,
6381 identity_matrix,
6382 gfx::PointF(),
6383 gfx::PointF(22.f, 22.f),
6384 gfx::Size(10, 10),
6385 true);
6386 child_no_auto_scale->SetAutomaticallyComputeRasterScale(false);
6387 child_no_auto_scale->SetRasterScale(fixed_raster_scale);
[email protected]518ee582012-10-24 18:29:446388
[email protected]fb661802013-03-25 01:59:326389 parent->AddChild(child_scale);
6390 parent->AddChild(child_empty);
6391 parent->AddChild(child_no_scale);
6392 parent->AddChild(child_no_auto_scale);
[email protected]518ee582012-10-24 18:29:446393
[email protected]50761e92013-03-29 20:51:286394 LayerList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:326395 int dummy_max_texture_size = 512;
[email protected]518ee582012-10-24 18:29:446396
[email protected]fb661802013-03-25 01:59:326397 float device_scale_factor = 2.5f;
6398 float page_scale_factor = 1.f;
[email protected]518ee582012-10-24 18:29:446399
[email protected]fb661802013-03-25 01:59:326400 // FIXME: Remove this when page_scale_factor is applied in the compositor.
6401 gfx::Transform page_scale_matrix;
6402 page_scale_matrix.Scale(page_scale_factor, page_scale_factor);
6403 parent->SetSublayerTransform(page_scale_matrix);
[email protected]518ee582012-10-24 18:29:446404
[email protected]fb661802013-03-25 01:59:326405 LayerTreeHostCommon::CalculateDrawProperties(parent.get(),
6406 parent->bounds(),
6407 device_scale_factor,
6408 page_scale_factor,
[email protected]f2136262013-04-26 21:10:196409 parent.get(),
[email protected]fb661802013-03-25 01:59:326410 dummy_max_texture_size,
6411 false,
6412 &render_surface_layer_list);
[email protected]518ee582012-10-24 18:29:446413
[email protected]f2136262013-04-26 21:10:196414 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor *
6415 initial_parent_scale, parent);
[email protected]fb661802013-03-25 01:59:326416 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor *
6417 initial_parent_scale * initial_child_scale,
6418 child_scale);
6419 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor *
6420 initial_parent_scale * initial_child_scale,
6421 child_empty);
6422 EXPECT_CONTENTS_SCALE_EQ(1, child_no_scale);
6423 EXPECT_CONTENTS_SCALE_EQ(
6424 device_scale_factor * page_scale_factor * fixed_raster_scale,
6425 child_no_auto_scale);
[email protected]518ee582012-10-24 18:29:446426
[email protected]fb661802013-03-25 01:59:326427 // The parent is scaled up and shouldn't need to scale during draw. The child
6428 // that can scale its contents should also not need to scale during draw. This
6429 // shouldn't change if the child has empty bounds. The other children should.
6430 EXPECT_FLOAT_EQ(1.0, parent->draw_transform().matrix().getDouble(0, 0));
6431 EXPECT_FLOAT_EQ(1.0, parent->draw_transform().matrix().getDouble(1, 1));
6432 EXPECT_FLOAT_EQ(1.0, child_scale->draw_transform().matrix().getDouble(0, 0));
6433 EXPECT_FLOAT_EQ(1.0, child_scale->draw_transform().matrix().getDouble(1, 1));
6434 EXPECT_FLOAT_EQ(1.0, child_empty->draw_transform().matrix().getDouble(0, 0));
6435 EXPECT_FLOAT_EQ(1.0, child_empty->draw_transform().matrix().getDouble(1, 1));
6436 EXPECT_FLOAT_EQ(device_scale_factor * page_scale_factor *
6437 initial_parent_scale * initial_child_scale,
6438 child_no_scale->draw_transform().matrix().getDouble(0, 0));
6439 EXPECT_FLOAT_EQ(device_scale_factor * page_scale_factor *
6440 initial_parent_scale * initial_child_scale,
6441 child_no_scale->draw_transform().matrix().getDouble(1, 1));
6442 EXPECT_FLOAT_EQ(
6443 initial_parent_scale * initial_child_scale / fixed_raster_scale,
6444 child_no_auto_scale->draw_transform().matrix().getDouble(0, 0));
6445 EXPECT_FLOAT_EQ(
6446 initial_parent_scale * initial_child_scale / fixed_raster_scale,
6447 child_no_auto_scale->draw_transform().matrix().getDouble(1, 1));
[email protected]518ee582012-10-24 18:29:446448
[email protected]fb661802013-03-25 01:59:326449 // If the device_scale_factor or page_scale_factor changes, then it should be
6450 // updated using the initial transform as the raster scale.
6451 device_scale_factor = 2.25f;
6452 page_scale_factor = 1.25f;
[email protected]518ee582012-10-24 18:29:446453
[email protected]fb661802013-03-25 01:59:326454 render_surface_layer_list.clear();
6455 LayerTreeHostCommon::CalculateDrawProperties(parent.get(),
6456 parent->bounds(),
6457 device_scale_factor,
6458 page_scale_factor,
[email protected]f2136262013-04-26 21:10:196459 parent.get(),
[email protected]fb661802013-03-25 01:59:326460 dummy_max_texture_size,
6461 false,
6462 &render_surface_layer_list);
[email protected]f2136262013-04-26 21:10:196463 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor *
6464 initial_parent_scale,
6465 parent);
[email protected]fb661802013-03-25 01:59:326466 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor *
6467 initial_parent_scale * initial_child_scale,
6468 child_scale);
6469 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor *
6470 initial_parent_scale * initial_child_scale,
6471 child_empty);
6472 EXPECT_CONTENTS_SCALE_EQ(1, child_no_scale);
6473 EXPECT_CONTENTS_SCALE_EQ(
6474 device_scale_factor * page_scale_factor * fixed_raster_scale,
6475 child_no_auto_scale);
[email protected]d0518202013-02-08 02:06:496476
[email protected]fb661802013-03-25 01:59:326477 // If the transform changes, we expect the raster scale to be reset to 1.0.
6478 double second_child_scale = 1.75;
6479 child_scale_matrix.Scale(second_child_scale / initial_child_scale,
6480 second_child_scale / initial_child_scale);
6481 child_scale->SetTransform(child_scale_matrix);
6482 child_empty->SetTransform(child_scale_matrix);
[email protected]d0518202013-02-08 02:06:496483
[email protected]fb661802013-03-25 01:59:326484 render_surface_layer_list.clear();
6485 LayerTreeHostCommon::CalculateDrawProperties(parent.get(),
6486 parent->bounds(),
6487 device_scale_factor,
6488 page_scale_factor,
[email protected]f2136262013-04-26 21:10:196489 parent.get(),
[email protected]fb661802013-03-25 01:59:326490 dummy_max_texture_size,
6491 false,
6492 &render_surface_layer_list);
[email protected]d0518202013-02-08 02:06:496493
[email protected]f2136262013-04-26 21:10:196494 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor *
6495 initial_parent_scale,
6496 parent);
[email protected]fb661802013-03-25 01:59:326497 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor,
6498 child_scale);
6499 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor,
6500 child_empty);
6501 EXPECT_CONTENTS_SCALE_EQ(1, child_no_scale);
[email protected]d0518202013-02-08 02:06:496502
[email protected]fb661802013-03-25 01:59:326503 // If the device_scale_factor or page_scale_factor changes, then it should be
6504 // updated, but still using 1.0 as the raster scale.
6505 device_scale_factor = 2.75f;
6506 page_scale_factor = 1.75f;
[email protected]d0518202013-02-08 02:06:496507
[email protected]fb661802013-03-25 01:59:326508 render_surface_layer_list.clear();
6509 LayerTreeHostCommon::CalculateDrawProperties(parent.get(),
6510 parent->bounds(),
6511 device_scale_factor,
6512 page_scale_factor,
[email protected]f2136262013-04-26 21:10:196513 parent.get(),
[email protected]fb661802013-03-25 01:59:326514 dummy_max_texture_size,
6515 false,
6516 &render_surface_layer_list);
[email protected]d0518202013-02-08 02:06:496517
[email protected]f2136262013-04-26 21:10:196518 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor *
6519 initial_parent_scale,
6520 parent);
[email protected]fb661802013-03-25 01:59:326521 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor,
6522 child_scale);
6523 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor,
6524 child_empty);
6525 EXPECT_CONTENTS_SCALE_EQ(1, child_no_scale);
6526 EXPECT_CONTENTS_SCALE_EQ(
6527 device_scale_factor * page_scale_factor * fixed_raster_scale,
6528 child_no_auto_scale);
[email protected]518ee582012-10-24 18:29:446529}
6530
[email protected]fb661802013-03-25 01:59:326531TEST(LayerTreeHostCommonTest, SmallContentsScale) {
6532 MockContentLayerClient delegate;
6533 gfx::Transform identity_matrix;
[email protected]11ec92972012-11-10 03:06:216534
[email protected]fb661802013-03-25 01:59:326535 gfx::Transform parent_scale_matrix;
6536 double initial_parent_scale = 1.75;
6537 parent_scale_matrix.Scale(initial_parent_scale, initial_parent_scale);
[email protected]11ec92972012-11-10 03:06:216538
[email protected]fb661802013-03-25 01:59:326539 gfx::Transform child_scale_matrix;
6540 double initial_child_scale = 0.25;
6541 child_scale_matrix.Scale(initial_child_scale, initial_child_scale);
[email protected]11ec92972012-11-10 03:06:216542
[email protected]fb661802013-03-25 01:59:326543 scoped_refptr<ContentLayer> parent = CreateDrawableContentLayer(&delegate);
6544 SetLayerPropertiesForTesting(parent.get(),
6545 parent_scale_matrix,
6546 identity_matrix,
6547 gfx::PointF(),
6548 gfx::PointF(),
6549 gfx::Size(100, 100),
6550 true);
[email protected]11ec92972012-11-10 03:06:216551
[email protected]fb661802013-03-25 01:59:326552 scoped_refptr<ContentLayer> child_scale =
6553 CreateDrawableContentLayer(&delegate);
6554 SetLayerPropertiesForTesting(child_scale.get(),
6555 child_scale_matrix,
6556 identity_matrix,
6557 gfx::PointF(),
6558 gfx::PointF(2.f, 2.f),
6559 gfx::Size(10, 10),
6560 true);
[email protected]11ec92972012-11-10 03:06:216561
[email protected]fb661802013-03-25 01:59:326562 parent->AddChild(child_scale);
[email protected]11ec92972012-11-10 03:06:216563
[email protected]50761e92013-03-29 20:51:286564 LayerList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:326565 int dummy_max_texture_size = 512;
[email protected]11ec92972012-11-10 03:06:216566
[email protected]fb661802013-03-25 01:59:326567 float device_scale_factor = 2.5f;
6568 float page_scale_factor = 0.01f;
[email protected]11ec92972012-11-10 03:06:216569
[email protected]fb661802013-03-25 01:59:326570 LayerTreeHostCommon::CalculateDrawProperties(parent.get(),
6571 parent->bounds(),
6572 device_scale_factor,
6573 page_scale_factor,
[email protected]f2136262013-04-26 21:10:196574 parent.get(),
[email protected]fb661802013-03-25 01:59:326575 dummy_max_texture_size,
6576 false,
6577 &render_surface_layer_list);
[email protected]11ec92972012-11-10 03:06:216578
[email protected]f2136262013-04-26 21:10:196579 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor *
6580 initial_parent_scale,
6581 parent);
[email protected]fb661802013-03-25 01:59:326582 // The child's scale is < 1, so we should not save and use that scale factor.
6583 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor * 1,
6584 child_scale);
[email protected]11ec92972012-11-10 03:06:216585
[email protected]fb661802013-03-25 01:59:326586 // When chilld's total scale becomes >= 1, we should save and use that scale
6587 // factor.
6588 child_scale_matrix.MakeIdentity();
6589 double final_child_scale = 0.75;
6590 child_scale_matrix.Scale(final_child_scale, final_child_scale);
6591 child_scale->SetTransform(child_scale_matrix);
[email protected]11ec92972012-11-10 03:06:216592
[email protected]fb661802013-03-25 01:59:326593 render_surface_layer_list.clear();
6594 LayerTreeHostCommon::CalculateDrawProperties(parent.get(),
6595 parent->bounds(),
6596 device_scale_factor,
6597 page_scale_factor,
[email protected]f2136262013-04-26 21:10:196598 parent.get(),
[email protected]fb661802013-03-25 01:59:326599 dummy_max_texture_size,
6600 false,
6601 &render_surface_layer_list);
[email protected]11ec92972012-11-10 03:06:216602
[email protected]f2136262013-04-26 21:10:196603 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor *
6604 initial_parent_scale,
6605 parent);
[email protected]fb661802013-03-25 01:59:326606 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor *
6607 initial_parent_scale * final_child_scale,
6608 child_scale);
[email protected]11ec92972012-11-10 03:06:216609}
6610
[email protected]fb661802013-03-25 01:59:326611TEST(LayerTreeHostCommonTest, ContentsScaleForSurfaces) {
6612 MockContentLayerClient delegate;
6613 gfx::Transform identity_matrix;
[email protected]518ee582012-10-24 18:29:446614
[email protected]fb661802013-03-25 01:59:326615 gfx::Transform parent_scale_matrix;
6616 double initial_parent_scale = 2.0;
6617 parent_scale_matrix.Scale(initial_parent_scale, initial_parent_scale);
[email protected]518ee582012-10-24 18:29:446618
[email protected]fb661802013-03-25 01:59:326619 gfx::Transform child_scale_matrix;
6620 double initial_child_scale = 3.0;
6621 child_scale_matrix.Scale(initial_child_scale, initial_child_scale);
[email protected]518ee582012-10-24 18:29:446622
[email protected]fb661802013-03-25 01:59:326623 float fixed_raster_scale = 4.f;
[email protected]518ee582012-10-24 18:29:446624
[email protected]fb661802013-03-25 01:59:326625 scoped_refptr<ContentLayer> parent = CreateDrawableContentLayer(&delegate);
6626 SetLayerPropertiesForTesting(parent.get(),
6627 parent_scale_matrix,
6628 identity_matrix,
6629 gfx::PointF(),
6630 gfx::PointF(),
6631 gfx::Size(100, 100),
6632 true);
[email protected]518ee582012-10-24 18:29:446633
[email protected]fb661802013-03-25 01:59:326634 scoped_refptr<ContentLayer> surface_scale =
6635 CreateDrawableContentLayer(&delegate);
6636 SetLayerPropertiesForTesting(surface_scale.get(),
6637 child_scale_matrix,
6638 identity_matrix,
6639 gfx::PointF(),
6640 gfx::PointF(2.f, 2.f),
6641 gfx::Size(10, 10),
6642 true);
[email protected]518ee582012-10-24 18:29:446643
[email protected]fb661802013-03-25 01:59:326644 scoped_refptr<ContentLayer> surface_scale_child_scale =
6645 CreateDrawableContentLayer(&delegate);
6646 SetLayerPropertiesForTesting(surface_scale_child_scale.get(),
6647 child_scale_matrix,
6648 identity_matrix,
6649 gfx::PointF(),
6650 gfx::PointF(),
6651 gfx::Size(10, 10),
6652 true);
[email protected]518ee582012-10-24 18:29:446653
[email protected]fb661802013-03-25 01:59:326654 scoped_refptr<NoScaleContentLayer> surface_scale_child_no_scale =
6655 CreateNoScaleDrawableContentLayer(&delegate);
6656 SetLayerPropertiesForTesting(surface_scale_child_no_scale.get(),
6657 child_scale_matrix,
6658 identity_matrix,
6659 gfx::PointF(),
6660 gfx::PointF(),
6661 gfx::Size(10, 10),
6662 true);
[email protected]518ee582012-10-24 18:29:446663
[email protected]fb661802013-03-25 01:59:326664 scoped_refptr<NoScaleContentLayer> surface_no_scale =
6665 CreateNoScaleDrawableContentLayer(&delegate);
6666 SetLayerPropertiesForTesting(surface_no_scale.get(),
6667 child_scale_matrix,
6668 identity_matrix,
6669 gfx::PointF(),
6670 gfx::PointF(12.f, 12.f),
6671 gfx::Size(10, 10),
6672 true);
[email protected]518ee582012-10-24 18:29:446673
[email protected]fb661802013-03-25 01:59:326674 scoped_refptr<ContentLayer> surface_no_scale_child_scale =
6675 CreateDrawableContentLayer(&delegate);
6676 SetLayerPropertiesForTesting(surface_no_scale_child_scale.get(),
6677 child_scale_matrix,
6678 identity_matrix,
6679 gfx::PointF(),
6680 gfx::PointF(),
6681 gfx::Size(10, 10),
6682 true);
[email protected]518ee582012-10-24 18:29:446683
[email protected]fb661802013-03-25 01:59:326684 scoped_refptr<NoScaleContentLayer> surface_no_scale_child_no_scale =
6685 CreateNoScaleDrawableContentLayer(&delegate);
6686 SetLayerPropertiesForTesting(surface_no_scale_child_no_scale.get(),
6687 child_scale_matrix,
6688 identity_matrix,
6689 gfx::PointF(),
6690 gfx::PointF(),
6691 gfx::Size(10, 10),
6692 true);
[email protected]518ee582012-10-24 18:29:446693
[email protected]fb661802013-03-25 01:59:326694 scoped_refptr<ContentLayer> surface_no_auto_scale =
6695 CreateDrawableContentLayer(&delegate);
6696 SetLayerPropertiesForTesting(surface_no_auto_scale.get(),
6697 child_scale_matrix,
6698 identity_matrix,
6699 gfx::PointF(),
6700 gfx::PointF(22.f, 22.f),
6701 gfx::Size(10, 10),
6702 true);
6703 surface_no_auto_scale->SetAutomaticallyComputeRasterScale(false);
6704 surface_no_auto_scale->SetRasterScale(fixed_raster_scale);
[email protected]518ee582012-10-24 18:29:446705
[email protected]fb661802013-03-25 01:59:326706 scoped_refptr<ContentLayer> surface_no_auto_scale_child_scale =
6707 CreateDrawableContentLayer(&delegate);
6708 SetLayerPropertiesForTesting(surface_no_auto_scale_child_scale.get(),
6709 child_scale_matrix,
6710 identity_matrix,
6711 gfx::PointF(),
6712 gfx::PointF(),
6713 gfx::Size(10, 10),
6714 true);
[email protected]518ee582012-10-24 18:29:446715
[email protected]fb661802013-03-25 01:59:326716 scoped_refptr<NoScaleContentLayer> surface_no_auto_scale_child_no_scale =
6717 CreateNoScaleDrawableContentLayer(&delegate);
6718 SetLayerPropertiesForTesting(surface_no_auto_scale_child_no_scale.get(),
6719 child_scale_matrix,
6720 identity_matrix,
6721 gfx::PointF(),
6722 gfx::PointF(),
6723 gfx::Size(10, 10),
6724 true);
[email protected]518ee582012-10-24 18:29:446725
[email protected]fb661802013-03-25 01:59:326726 parent->AddChild(surface_scale);
6727 parent->AddChild(surface_no_scale);
6728 parent->AddChild(surface_no_auto_scale);
[email protected]518ee582012-10-24 18:29:446729
[email protected]fb661802013-03-25 01:59:326730 surface_scale->SetForceRenderSurface(true);
6731 surface_scale->AddChild(surface_scale_child_scale);
6732 surface_scale->AddChild(surface_scale_child_no_scale);
[email protected]518ee582012-10-24 18:29:446733
[email protected]fb661802013-03-25 01:59:326734 surface_no_scale->SetForceRenderSurface(true);
6735 surface_no_scale->AddChild(surface_no_scale_child_scale);
6736 surface_no_scale->AddChild(surface_no_scale_child_no_scale);
[email protected]518ee582012-10-24 18:29:446737
[email protected]fb661802013-03-25 01:59:326738 surface_no_auto_scale->SetForceRenderSurface(true);
6739 surface_no_auto_scale->AddChild(surface_no_auto_scale_child_scale);
6740 surface_no_auto_scale->AddChild(surface_no_auto_scale_child_no_scale);
[email protected]518ee582012-10-24 18:29:446741
[email protected]50761e92013-03-29 20:51:286742 LayerList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:326743 int dummy_max_texture_size = 512;
[email protected]518ee582012-10-24 18:29:446744
[email protected]fb661802013-03-25 01:59:326745 double device_scale_factor = 5;
6746 double page_scale_factor = 7;
[email protected]518ee582012-10-24 18:29:446747
[email protected]fb661802013-03-25 01:59:326748 LayerTreeHostCommon::CalculateDrawProperties(parent.get(),
6749 parent->bounds(),
6750 device_scale_factor,
6751 page_scale_factor,
[email protected]f2136262013-04-26 21:10:196752 parent.get(),
[email protected]fb661802013-03-25 01:59:326753 dummy_max_texture_size,
6754 false,
6755 &render_surface_layer_list);
[email protected]f2136262013-04-26 21:10:196756 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor *
6757 initial_parent_scale,
6758 parent);
[email protected]fb661802013-03-25 01:59:326759 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor *
6760 initial_parent_scale * initial_child_scale,
6761 surface_scale);
6762 EXPECT_CONTENTS_SCALE_EQ(1, surface_no_scale);
6763 EXPECT_CONTENTS_SCALE_EQ(
6764 device_scale_factor * page_scale_factor * fixed_raster_scale,
6765 surface_no_auto_scale);
[email protected]fb661802013-03-25 01:59:326766 EXPECT_CONTENTS_SCALE_EQ(
6767 device_scale_factor * page_scale_factor * initial_parent_scale *
6768 initial_child_scale * initial_child_scale,
6769 surface_scale_child_scale);
6770 EXPECT_CONTENTS_SCALE_EQ(1, surface_scale_child_no_scale);
6771 EXPECT_CONTENTS_SCALE_EQ(
6772 device_scale_factor * page_scale_factor * initial_parent_scale *
6773 initial_child_scale * initial_child_scale,
6774 surface_no_scale_child_scale);
6775 EXPECT_CONTENTS_SCALE_EQ(1, surface_no_scale_child_no_scale);
6776 EXPECT_CONTENTS_SCALE_EQ(
6777 device_scale_factor * page_scale_factor * initial_parent_scale *
6778 initial_child_scale * initial_child_scale,
6779 surface_no_auto_scale_child_scale);
6780 EXPECT_CONTENTS_SCALE_EQ(1, surface_no_auto_scale_child_no_scale);
[email protected]518ee582012-10-24 18:29:446781
[email protected]fb661802013-03-25 01:59:326782 // The parent is scaled up and shouldn't need to scale during draw.
6783 EXPECT_FLOAT_EQ(1.0, parent->draw_transform().matrix().getDouble(0, 0));
6784 EXPECT_FLOAT_EQ(1.0, parent->draw_transform().matrix().getDouble(1, 1));
[email protected]518ee582012-10-24 18:29:446785
[email protected]fb661802013-03-25 01:59:326786 // RenderSurfaces should always be 1:1 with their target.
6787 EXPECT_FLOAT_EQ(
6788 1.0,
6789 surface_scale->render_surface()->draw_transform().matrix().getDouble(0,
6790 0));
6791 EXPECT_FLOAT_EQ(
6792 1.0,
6793 surface_scale->render_surface()->draw_transform().matrix().getDouble(1,
6794 1));
[email protected]518ee582012-10-24 18:29:446795
[email protected]fb661802013-03-25 01:59:326796 // The surface_scale can apply contents scale so the layer shouldn't need to
6797 // scale during draw.
6798 EXPECT_FLOAT_EQ(1.0,
6799 surface_scale->draw_transform().matrix().getDouble(0, 0));
6800 EXPECT_FLOAT_EQ(1.0,
6801 surface_scale->draw_transform().matrix().getDouble(1, 1));
[email protected]518ee582012-10-24 18:29:446802
[email protected]fb661802013-03-25 01:59:326803 // The surface_scale_child_scale can apply contents scale so it shouldn't need
6804 // to scale during draw.
6805 EXPECT_FLOAT_EQ(
6806 1.0,
6807 surface_scale_child_scale->draw_transform().matrix().getDouble(0, 0));
6808 EXPECT_FLOAT_EQ(
6809 1.0,
6810 surface_scale_child_scale->draw_transform().matrix().getDouble(1, 1));
[email protected]518ee582012-10-24 18:29:446811
[email protected]fb661802013-03-25 01:59:326812 // The surface_scale_child_no_scale can not apply contents scale, so it needs
6813 // to be scaled during draw.
6814 EXPECT_FLOAT_EQ(
6815 device_scale_factor * page_scale_factor * initial_parent_scale *
6816 initial_child_scale * initial_child_scale,
6817 surface_scale_child_no_scale->draw_transform().matrix().getDouble(0, 0));
6818 EXPECT_FLOAT_EQ(
6819 device_scale_factor * page_scale_factor * initial_parent_scale *
6820 initial_child_scale * initial_child_scale,
6821 surface_scale_child_no_scale->draw_transform().matrix().getDouble(1, 1));
[email protected]518ee582012-10-24 18:29:446822
[email protected]fb661802013-03-25 01:59:326823 // RenderSurfaces should always be 1:1 with their target.
6824 EXPECT_FLOAT_EQ(
6825 1.0,
6826 surface_no_scale->render_surface()->draw_transform().matrix().getDouble(
6827 0, 0));
6828 EXPECT_FLOAT_EQ(
6829 1.0,
6830 surface_no_scale->render_surface()->draw_transform().matrix().getDouble(
6831 1, 1));
[email protected]518ee582012-10-24 18:29:446832
[email protected]fb661802013-03-25 01:59:326833 // The surface_no_scale layer can not apply contents scale, so it needs to be
6834 // scaled during draw.
6835 EXPECT_FLOAT_EQ(device_scale_factor * page_scale_factor *
6836 initial_parent_scale * initial_child_scale,
6837 surface_no_scale->draw_transform().matrix().getDouble(0, 0));
6838 EXPECT_FLOAT_EQ(device_scale_factor * page_scale_factor *
6839 initial_parent_scale * initial_child_scale,
6840 surface_no_scale->draw_transform().matrix().getDouble(1, 1));
[email protected]518ee582012-10-24 18:29:446841
[email protected]fb661802013-03-25 01:59:326842 // The surface_scale_child_scale can apply contents scale so it shouldn't need
6843 // to scale during draw.
6844 EXPECT_FLOAT_EQ(
6845 1.0,
6846 surface_no_scale_child_scale->draw_transform().matrix().getDouble(0, 0));
6847 EXPECT_FLOAT_EQ(
6848 1.0,
6849 surface_no_scale_child_scale->draw_transform().matrix().getDouble(1, 1));
[email protected]518ee582012-10-24 18:29:446850
[email protected]fb661802013-03-25 01:59:326851 // The surface_scale_child_no_scale can not apply contents scale, so it needs
6852 // to be scaled during draw.
6853 EXPECT_FLOAT_EQ(
6854 device_scale_factor * page_scale_factor * initial_parent_scale *
6855 initial_child_scale * initial_child_scale,
6856 surface_no_scale_child_no_scale->draw_transform().matrix().getDouble(0,
6857 0));
6858 EXPECT_FLOAT_EQ(
6859 device_scale_factor * page_scale_factor * initial_parent_scale *
6860 initial_child_scale * initial_child_scale,
6861 surface_no_scale_child_no_scale->draw_transform().matrix().getDouble(1,
6862 1));
[email protected]518ee582012-10-24 18:29:446863
[email protected]fb661802013-03-25 01:59:326864 // RenderSurfaces should always be 1:1 with their target.
6865 EXPECT_FLOAT_EQ(1.0,
6866 surface_no_auto_scale->render_surface()->draw_transform()
6867 .matrix().getDouble(0, 0));
6868 EXPECT_FLOAT_EQ(1.0,
6869 surface_no_auto_scale->render_surface()->draw_transform()
6870 .matrix().getDouble(1, 1));
[email protected]518ee582012-10-24 18:29:446871
[email protected]fb661802013-03-25 01:59:326872 // The surface_no_auto_scale layer has a fixed contents scale, so it needs to
6873 // be scaled during draw.
6874 EXPECT_FLOAT_EQ(
6875 device_scale_factor * page_scale_factor * initial_parent_scale *
6876 initial_child_scale /
6877 (device_scale_factor * page_scale_factor * fixed_raster_scale),
6878 surface_no_auto_scale->draw_transform().matrix().getDouble(0, 0));
6879 EXPECT_FLOAT_EQ(
6880 device_scale_factor * page_scale_factor * initial_parent_scale *
6881 initial_child_scale /
6882 (device_scale_factor * page_scale_factor * fixed_raster_scale),
6883 surface_no_auto_scale->draw_transform().matrix().getDouble(1, 1));
[email protected]518ee582012-10-24 18:29:446884
[email protected]fb661802013-03-25 01:59:326885 // The surface_scale_child_scale can apply contents scale so it shouldn't need
6886 // to scale during draw.
6887 EXPECT_FLOAT_EQ(
6888 1.0,
6889 surface_no_auto_scale_child_scale->draw_transform().matrix().getDouble(
6890 0, 0));
6891 EXPECT_FLOAT_EQ(
6892 1.0,
6893 surface_no_auto_scale_child_scale->draw_transform().matrix().getDouble(
6894 1, 1));
[email protected]518ee582012-10-24 18:29:446895
[email protected]fb661802013-03-25 01:59:326896 // The surface_scale_child_no_scale can not apply contents scale, so it needs
6897 // to be scaled during draw.
6898 EXPECT_FLOAT_EQ(
6899 device_scale_factor * page_scale_factor * initial_parent_scale *
6900 initial_child_scale * initial_child_scale,
6901 surface_no_auto_scale_child_no_scale->draw_transform().matrix().getDouble(
6902 0, 0));
6903 EXPECT_FLOAT_EQ(
6904 device_scale_factor * page_scale_factor * initial_parent_scale *
6905 initial_child_scale * initial_child_scale,
6906 surface_no_auto_scale_child_no_scale->draw_transform().matrix().getDouble(
6907 1, 1));
[email protected]518ee582012-10-24 18:29:446908}
6909
[email protected]fb661802013-03-25 01:59:326910TEST(LayerTreeHostCommonTest, ContentsScaleForAnimatingLayer) {
6911 MockContentLayerClient delegate;
6912 gfx::Transform identity_matrix;
[email protected]6a9cff92012-11-08 11:53:266913
[email protected]fb661802013-03-25 01:59:326914 gfx::Transform parent_scale_matrix;
6915 double initial_parent_scale = 1.75;
6916 parent_scale_matrix.Scale(initial_parent_scale, initial_parent_scale);
[email protected]6a9cff92012-11-08 11:53:266917
[email protected]fb661802013-03-25 01:59:326918 gfx::Transform child_scale_matrix;
6919 double initial_child_scale = 1.25;
6920 child_scale_matrix.Scale(initial_child_scale, initial_child_scale);
[email protected]6a9cff92012-11-08 11:53:266921
[email protected]fb661802013-03-25 01:59:326922 scoped_refptr<ContentLayer> parent = CreateDrawableContentLayer(&delegate);
6923 SetLayerPropertiesForTesting(parent.get(),
6924 parent_scale_matrix,
6925 identity_matrix,
6926 gfx::PointF(),
6927 gfx::PointF(),
6928 gfx::Size(100, 100),
6929 true);
[email protected]6a9cff92012-11-08 11:53:266930
[email protected]fb661802013-03-25 01:59:326931 scoped_refptr<ContentLayer> child_scale =
6932 CreateDrawableContentLayer(&delegate);
6933 SetLayerPropertiesForTesting(child_scale.get(),
6934 child_scale_matrix,
6935 identity_matrix,
6936 gfx::PointF(),
6937 gfx::PointF(2.f, 2.f),
6938 gfx::Size(10, 10),
6939 true);
[email protected]6a9cff92012-11-08 11:53:266940
[email protected]fb661802013-03-25 01:59:326941 parent->AddChild(child_scale);
[email protected]6a9cff92012-11-08 11:53:266942
[email protected]fb661802013-03-25 01:59:326943 // Now put an animating transform on child.
6944 int animation_id = AddAnimatedTransformToController(
6945 child_scale->layer_animation_controller(), 10.0, 30, 0);
[email protected]6a9cff92012-11-08 11:53:266946
[email protected]50761e92013-03-29 20:51:286947 LayerList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:326948 int dummy_max_texture_size = 512;
[email protected]6a9cff92012-11-08 11:53:266949
[email protected]fb661802013-03-25 01:59:326950 LayerTreeHostCommon::CalculateDrawProperties(parent.get(),
6951 parent->bounds(),
6952 1.f,
6953 1.f,
[email protected]f2136262013-04-26 21:10:196954 NULL,
[email protected]fb661802013-03-25 01:59:326955 dummy_max_texture_size,
6956 false,
6957 &render_surface_layer_list);
[email protected]6a9cff92012-11-08 11:53:266958
[email protected]fb661802013-03-25 01:59:326959 EXPECT_CONTENTS_SCALE_EQ(initial_parent_scale, parent);
6960 // The layers with animating transforms should not compute a contents scale
6961 // other than 1 until they finish animating.
6962 EXPECT_CONTENTS_SCALE_EQ(1, child_scale);
[email protected]6a9cff92012-11-08 11:53:266963
[email protected]fb661802013-03-25 01:59:326964 // Remove the animation, now it can save a raster scale.
6965 child_scale->layer_animation_controller()->RemoveAnimation(animation_id);
[email protected]6a9cff92012-11-08 11:53:266966
[email protected]fb661802013-03-25 01:59:326967 LayerTreeHostCommon::CalculateDrawProperties(parent.get(),
6968 parent->bounds(),
6969 1.f,
6970 1.f,
[email protected]f2136262013-04-26 21:10:196971 NULL,
[email protected]fb661802013-03-25 01:59:326972 dummy_max_texture_size,
6973 false,
6974 &render_surface_layer_list);
[email protected]6a9cff92012-11-08 11:53:266975
[email protected]fb661802013-03-25 01:59:326976 EXPECT_CONTENTS_SCALE_EQ(initial_parent_scale, parent);
6977 // The layers with animating transforms should not compute a contents scale
6978 // other than 1 until they finish animating.
6979 EXPECT_CONTENTS_SCALE_EQ(initial_parent_scale * initial_child_scale,
6980 child_scale);
[email protected]6a9cff92012-11-08 11:53:266981}
6982
[email protected]fb661802013-03-25 01:59:326983TEST(LayerTreeHostCommonTest, RenderSurfaceTransformsInHighDPI) {
6984 MockContentLayerClient delegate;
6985 gfx::Transform identity_matrix;
[email protected]6a9cff92012-11-08 11:53:266986
[email protected]fb661802013-03-25 01:59:326987 scoped_refptr<ContentLayer> parent = CreateDrawableContentLayer(&delegate);
6988 SetLayerPropertiesForTesting(parent.get(),
6989 identity_matrix,
6990 identity_matrix,
6991 gfx::PointF(),
6992 gfx::PointF(),
6993 gfx::Size(30, 30),
6994 true);
[email protected]94f206c12012-08-25 00:09:146995
[email protected]fb661802013-03-25 01:59:326996 scoped_refptr<ContentLayer> child = CreateDrawableContentLayer(&delegate);
6997 SetLayerPropertiesForTesting(child.get(),
6998 identity_matrix,
6999 identity_matrix,
7000 gfx::PointF(),
7001 gfx::PointF(2.f, 2.f),
7002 gfx::Size(10, 10),
7003 true);
[email protected]94f206c12012-08-25 00:09:147004
[email protected]fb661802013-03-25 01:59:327005 gfx::Transform replica_transform;
7006 replica_transform.Scale(1.0, -1.0);
7007 scoped_refptr<ContentLayer> replica = CreateDrawableContentLayer(&delegate);
7008 SetLayerPropertiesForTesting(replica.get(),
7009 replica_transform,
7010 identity_matrix,
7011 gfx::PointF(),
7012 gfx::PointF(2.f, 2.f),
7013 gfx::Size(10, 10),
7014 true);
[email protected]94f206c12012-08-25 00:09:147015
[email protected]fb661802013-03-25 01:59:327016 // This layer should end up in the same surface as child, with the same draw
7017 // and screen space transforms.
7018 scoped_refptr<ContentLayer> duplicate_child_non_owner =
7019 CreateDrawableContentLayer(&delegate);
7020 SetLayerPropertiesForTesting(duplicate_child_non_owner.get(),
7021 identity_matrix,
7022 identity_matrix,
7023 gfx::PointF(),
7024 gfx::PointF(),
7025 gfx::Size(10, 10),
7026 true);
[email protected]94f206c12012-08-25 00:09:147027
[email protected]fb661802013-03-25 01:59:327028 parent->AddChild(child);
7029 child->AddChild(duplicate_child_non_owner);
7030 child->SetReplicaLayer(replica.get());
[email protected]94f206c12012-08-25 00:09:147031
[email protected]50761e92013-03-29 20:51:287032 LayerList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:327033 int dummy_max_texture_size = 512;
[email protected]94f206c12012-08-25 00:09:147034
[email protected]fb661802013-03-25 01:59:327035 float device_scale_factor = 1.5f;
7036 LayerTreeHostCommon::CalculateDrawProperties(parent.get(),
7037 parent->bounds(),
7038 device_scale_factor,
[email protected]f2136262013-04-26 21:10:197039 1.f,
7040 NULL,
[email protected]fb661802013-03-25 01:59:327041 dummy_max_texture_size,
7042 false,
7043 &render_surface_layer_list);
[email protected]94f206c12012-08-25 00:09:147044
[email protected]fb661802013-03-25 01:59:327045 // We should have two render surfaces. The root's render surface and child's
7046 // render surface (it needs one because it has a replica layer).
7047 EXPECT_EQ(2u, render_surface_layer_list.size());
[email protected]94f206c12012-08-25 00:09:147048
[email protected]fb661802013-03-25 01:59:327049 gfx::Transform expected_parent_transform;
7050 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_parent_transform,
7051 parent->screen_space_transform());
7052 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_parent_transform,
7053 parent->draw_transform());
[email protected]94f206c12012-08-25 00:09:147054
[email protected]fb661802013-03-25 01:59:327055 gfx::Transform expected_draw_transform;
7056 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_draw_transform,
7057 child->draw_transform());
[email protected]94f206c12012-08-25 00:09:147058
[email protected]fb661802013-03-25 01:59:327059 gfx::Transform expected_screen_space_transform;
7060 expected_screen_space_transform.Translate(
7061 device_scale_factor * child->position().x(),
7062 device_scale_factor * child->position().y());
7063 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_screen_space_transform,
7064 child->screen_space_transform());
[email protected]94f206c12012-08-25 00:09:147065
[email protected]fb661802013-03-25 01:59:327066 gfx::Transform expected_duplicate_child_draw_transform =
7067 child->draw_transform();
7068 EXPECT_TRANSFORMATION_MATRIX_EQ(child->draw_transform(),
7069 duplicate_child_non_owner->draw_transform());
7070 EXPECT_TRANSFORMATION_MATRIX_EQ(
7071 child->screen_space_transform(),
7072 duplicate_child_non_owner->screen_space_transform());
7073 EXPECT_RECT_EQ(child->drawable_content_rect(),
7074 duplicate_child_non_owner->drawable_content_rect());
7075 EXPECT_EQ(child->content_bounds(),
7076 duplicate_child_non_owner->content_bounds());
[email protected]94f206c12012-08-25 00:09:147077
[email protected]fb661802013-03-25 01:59:327078 gfx::Transform expected_render_surface_draw_transform;
7079 expected_render_surface_draw_transform.Translate(
7080 device_scale_factor * child->position().x(),
7081 device_scale_factor * child->position().y());
7082 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_render_surface_draw_transform,
7083 child->render_surface()->draw_transform());
[email protected]94f206c12012-08-25 00:09:147084
[email protected]fb661802013-03-25 01:59:327085 gfx::Transform expected_surface_draw_transform;
7086 expected_surface_draw_transform.Translate(device_scale_factor * 2.f,
7087 device_scale_factor * 2.f);
7088 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_surface_draw_transform,
7089 child->render_surface()->draw_transform());
[email protected]94f206c12012-08-25 00:09:147090
[email protected]fb661802013-03-25 01:59:327091 gfx::Transform expected_surface_screen_space_transform;
7092 expected_surface_screen_space_transform.Translate(device_scale_factor * 2.f,
7093 device_scale_factor * 2.f);
7094 EXPECT_TRANSFORMATION_MATRIX_EQ(
7095 expected_surface_screen_space_transform,
7096 child->render_surface()->screen_space_transform());
[email protected]94f206c12012-08-25 00:09:147097
[email protected]fb661802013-03-25 01:59:327098 gfx::Transform expected_replica_draw_transform;
7099 expected_replica_draw_transform.matrix().setDouble(1, 1, -1.0);
7100 expected_replica_draw_transform.matrix().setDouble(0, 3, 6.0);
7101 expected_replica_draw_transform.matrix().setDouble(1, 3, 6.0);
7102 EXPECT_TRANSFORMATION_MATRIX_EQ(
7103 expected_replica_draw_transform,
7104 child->render_surface()->replica_draw_transform());
[email protected]94f206c12012-08-25 00:09:147105
[email protected]fb661802013-03-25 01:59:327106 gfx::Transform expected_replica_screen_space_transform;
7107 expected_replica_screen_space_transform.matrix().setDouble(1, 1, -1.0);
7108 expected_replica_screen_space_transform.matrix().setDouble(0, 3, 6.0);
7109 expected_replica_screen_space_transform.matrix().setDouble(1, 3, 6.0);
7110 EXPECT_TRANSFORMATION_MATRIX_EQ(
7111 expected_replica_screen_space_transform,
7112 child->render_surface()->replica_screen_space_transform());
7113 EXPECT_TRANSFORMATION_MATRIX_EQ(
7114 expected_replica_screen_space_transform,
7115 child->render_surface()->replica_screen_space_transform());
[email protected]904e9132012-11-01 00:12:477116}
7117
[email protected]fb661802013-03-25 01:59:327118TEST(LayerTreeHostCommonTest,
7119 RenderSurfaceTransformsInHighDPIAccurateScaleZeroPosition) {
7120 MockContentLayerClient delegate;
7121 gfx::Transform identity_matrix;
[email protected]904e9132012-11-01 00:12:477122
[email protected]fb661802013-03-25 01:59:327123 scoped_refptr<ContentLayer> parent = CreateDrawableContentLayer(&delegate);
7124 SetLayerPropertiesForTesting(parent.get(),
7125 identity_matrix,
7126 identity_matrix,
7127 gfx::PointF(),
7128 gfx::PointF(),
7129 gfx::Size(33, 31),
7130 true);
[email protected]904e9132012-11-01 00:12:477131
[email protected]fb661802013-03-25 01:59:327132 scoped_refptr<ContentLayer> child = CreateDrawableContentLayer(&delegate);
7133 SetLayerPropertiesForTesting(child.get(),
7134 identity_matrix,
7135 identity_matrix,
7136 gfx::PointF(),
7137 gfx::PointF(),
7138 gfx::Size(13, 11),
7139 true);
[email protected]904e9132012-11-01 00:12:477140
[email protected]fb661802013-03-25 01:59:327141 gfx::Transform replica_transform;
7142 replica_transform.Scale(1.0, -1.0);
7143 scoped_refptr<ContentLayer> replica = CreateDrawableContentLayer(&delegate);
7144 SetLayerPropertiesForTesting(replica.get(),
7145 replica_transform,
7146 identity_matrix,
7147 gfx::PointF(),
7148 gfx::PointF(),
7149 gfx::Size(13, 11),
7150 true);
[email protected]904e9132012-11-01 00:12:477151
[email protected]fb661802013-03-25 01:59:327152 // This layer should end up in the same surface as child, with the same draw
7153 // and screen space transforms.
7154 scoped_refptr<ContentLayer> duplicate_child_non_owner =
7155 CreateDrawableContentLayer(&delegate);
7156 SetLayerPropertiesForTesting(duplicate_child_non_owner.get(),
7157 identity_matrix,
7158 identity_matrix,
7159 gfx::PointF(),
7160 gfx::PointF(),
7161 gfx::Size(13, 11),
7162 true);
[email protected]904e9132012-11-01 00:12:477163
[email protected]fb661802013-03-25 01:59:327164 parent->AddChild(child);
7165 child->AddChild(duplicate_child_non_owner);
7166 child->SetReplicaLayer(replica.get());
[email protected]904e9132012-11-01 00:12:477167
[email protected]50761e92013-03-29 20:51:287168 LayerList render_surface_layer_list;
[email protected]fb661802013-03-25 01:59:327169 int dummy_max_texture_size = 512;
[email protected]904e9132012-11-01 00:12:477170
[email protected]fb661802013-03-25 01:59:327171 float device_scale_factor = 1.7f;
7172 LayerTreeHostCommon::CalculateDrawProperties(parent.get(),
7173 parent->bounds(),
7174 device_scale_factor,
7175 1.f,
[email protected]f2136262013-04-26 21:10:197176 NULL,
[email protected]fb661802013-03-25 01:59:327177 dummy_max_texture_size,
7178 false,
7179 &render_surface_layer_list);
[email protected]904e9132012-11-01 00:12:477180
[email protected]fb661802013-03-25 01:59:327181 // We should have two render surfaces. The root's render surface and child's
7182 // render surface (it needs one because it has a replica layer).
7183 EXPECT_EQ(2u, render_surface_layer_list.size());
[email protected]904e9132012-11-01 00:12:477184
[email protected]fb661802013-03-25 01:59:327185 gfx::Transform identity_transform;
[email protected]904e9132012-11-01 00:12:477186
[email protected]fb661802013-03-25 01:59:327187 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_transform,
7188 parent->screen_space_transform());
7189 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_transform, parent->draw_transform());
7190 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_transform, child->draw_transform());
7191 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_transform,
7192 child->screen_space_transform());
7193 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_transform,
7194 duplicate_child_non_owner->draw_transform());
7195 EXPECT_TRANSFORMATION_MATRIX_EQ(
7196 identity_transform, duplicate_child_non_owner->screen_space_transform());
7197 EXPECT_RECT_EQ(child->drawable_content_rect(),
7198 duplicate_child_non_owner->drawable_content_rect());
7199 EXPECT_EQ(child->content_bounds(),
7200 duplicate_child_non_owner->content_bounds());
[email protected]904e9132012-11-01 00:12:477201
[email protected]fb661802013-03-25 01:59:327202 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_transform,
7203 child->render_surface()->draw_transform());
7204 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_transform,
7205 child->render_surface()->draw_transform());
7206 EXPECT_TRANSFORMATION_MATRIX_EQ(
7207 identity_transform, child->render_surface()->screen_space_transform());
[email protected]904e9132012-11-01 00:12:477208
[email protected]fb661802013-03-25 01:59:327209 gfx::Transform expected_replica_draw_transform;
7210 expected_replica_draw_transform.matrix().setDouble(1, 1, -1.0);
7211 EXPECT_TRANSFORMATION_MATRIX_EQ(
7212 expected_replica_draw_transform,
7213 child->render_surface()->replica_draw_transform());
[email protected]904e9132012-11-01 00:12:477214
[email protected]fb661802013-03-25 01:59:327215 gfx::Transform expected_replica_screen_space_transform;
7216 expected_replica_screen_space_transform.matrix().setDouble(1, 1, -1.0);
7217 EXPECT_TRANSFORMATION_MATRIX_EQ(
7218 expected_replica_screen_space_transform,
7219 child->render_surface()->replica_screen_space_transform());
[email protected]94f206c12012-08-25 00:09:147220}
7221
[email protected]fb661802013-03-25 01:59:327222TEST(LayerTreeHostCommonTest, SubtreeSearch) {
7223 scoped_refptr<Layer> root = Layer::Create();
7224 scoped_refptr<Layer> child = Layer::Create();
7225 scoped_refptr<Layer> grand_child = Layer::Create();
7226 scoped_refptr<Layer> mask_layer = Layer::Create();
7227 scoped_refptr<Layer> replica_layer = Layer::Create();
[email protected]94f206c12012-08-25 00:09:147228
[email protected]fb661802013-03-25 01:59:327229 grand_child->SetReplicaLayer(replica_layer.get());
7230 child->AddChild(grand_child.get());
7231 child->SetMaskLayer(mask_layer.get());
7232 root->AddChild(child.get());
[email protected]94f206c12012-08-25 00:09:147233
[email protected]fb661802013-03-25 01:59:327234 int nonexistent_id = -1;
7235 EXPECT_EQ(root,
7236 LayerTreeHostCommon::FindLayerInSubtree(root.get(), root->id()));
7237 EXPECT_EQ(child,
7238 LayerTreeHostCommon::FindLayerInSubtree(root.get(), child->id()));
7239 EXPECT_EQ(
7240 grand_child,
7241 LayerTreeHostCommon::FindLayerInSubtree(root.get(), grand_child->id()));
7242 EXPECT_EQ(
7243 mask_layer,
7244 LayerTreeHostCommon::FindLayerInSubtree(root.get(), mask_layer->id()));
7245 EXPECT_EQ(
7246 replica_layer,
7247 LayerTreeHostCommon::FindLayerInSubtree(root.get(), replica_layer->id()));
7248 EXPECT_EQ(
7249 0, LayerTreeHostCommon::FindLayerInSubtree(root.get(), nonexistent_id));
[email protected]94f206c12012-08-25 00:09:147250}
7251
[email protected]fb661802013-03-25 01:59:327252TEST(LayerTreeHostCommonTest, TransparentChildRenderSurfaceCreation) {
7253 scoped_refptr<Layer> root = Layer::Create();
7254 scoped_refptr<Layer> child = Layer::Create();
7255 scoped_refptr<LayerWithForcedDrawsContent> grand_child =
7256 make_scoped_refptr(new LayerWithForcedDrawsContent());
[email protected]498ec6e0e2012-11-30 18:24:577257
[email protected]fb661802013-03-25 01:59:327258 const gfx::Transform identity_matrix;
7259 SetLayerPropertiesForTesting(root.get(),
7260 identity_matrix,
7261 identity_matrix,
7262 gfx::PointF(),
7263 gfx::PointF(),
7264 gfx::Size(100, 100),
7265 false);
7266 SetLayerPropertiesForTesting(child.get(),
7267 identity_matrix,
7268 identity_matrix,
7269 gfx::PointF(),
7270 gfx::PointF(),
7271 gfx::Size(10, 10),
7272 false);
7273 SetLayerPropertiesForTesting(grand_child.get(),
7274 identity_matrix,
7275 identity_matrix,
7276 gfx::PointF(),
7277 gfx::PointF(),
7278 gfx::Size(10, 10),
7279 false);
[email protected]498ec6e0e2012-11-30 18:24:577280
[email protected]fb661802013-03-25 01:59:327281 root->AddChild(child);
7282 child->AddChild(grand_child);
7283 child->SetOpacity(0.5f);
[email protected]498ec6e0e2012-11-30 18:24:577284
[email protected]fb661802013-03-25 01:59:327285 ExecuteCalculateDrawProperties(root.get());
[email protected]498ec6e0e2012-11-30 18:24:577286
[email protected]fb661802013-03-25 01:59:327287 EXPECT_FALSE(child->render_surface());
[email protected]498ec6e0e2012-11-30 18:24:577288}
7289
[email protected]f90fc412013-03-30 20:13:167290TEST(LayerTreeHostCommonTest, OpacityAnimatingOnPendingTree) {
7291 FakeImplProxy proxy;
7292 FakeLayerTreeHostImpl host_impl(&proxy);
7293 host_impl.CreatePendingTree();
7294 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.pending_tree(), 1);
7295
7296 const gfx::Transform identity_matrix;
7297 SetLayerPropertiesForTesting(root.get(),
7298 identity_matrix,
7299 identity_matrix,
7300 gfx::PointF(),
7301 gfx::PointF(),
7302 gfx::Size(100, 100),
7303 false);
7304 root->SetDrawsContent(true);
7305
7306 scoped_ptr<LayerImpl> child = LayerImpl::Create(host_impl.pending_tree(), 2);
7307 SetLayerPropertiesForTesting(child.get(),
7308 identity_matrix,
7309 identity_matrix,
7310 gfx::PointF(),
7311 gfx::PointF(),
7312 gfx::Size(50, 50),
7313 false);
7314 child->SetDrawsContent(true);
7315 child->SetOpacity(0.0f);
7316
7317 // Add opacity animation.
7318 AddOpacityTransitionToController(
7319 child->layer_animation_controller(), 10.0, 0.0f, 1.0f, false);
7320
7321 root->AddChild(child.Pass());
7322
7323 std::vector<LayerImpl*> render_surface_layer_list;
7324 int dummy_max_texture_size = 512;
7325 LayerTreeHostCommon::CalculateDrawProperties(root.get(),
7326 root->bounds(),
7327 1.f,
7328 1.f,
[email protected]f2136262013-04-26 21:10:197329 NULL,
[email protected]f90fc412013-03-30 20:13:167330 dummy_max_texture_size,
7331 false,
7332 &render_surface_layer_list,
7333 false);
7334
7335 // We should have one render surface and two layers. The child
7336 // layer should be included even though it is transparent.
7337 ASSERT_EQ(1u, render_surface_layer_list.size());
7338 ASSERT_EQ(2u, root->render_surface()->layer_list().size());
7339}
7340
[email protected]10aabcc32012-12-13 09:18:597341typedef std::tr1::tuple<bool, bool> LCDTextTestParam;
7342class LCDTextTest : public testing::TestWithParam<LCDTextTestParam> {
[email protected]fb661802013-03-25 01:59:327343 protected:
7344 virtual void SetUp() {
7345 can_use_lcd_text_ = std::tr1::get<0>(GetParam());
[email protected]10aabcc32012-12-13 09:18:597346
[email protected]fb661802013-03-25 01:59:327347 root_ = Layer::Create();
7348 child_ = Layer::Create();
7349 grand_child_ = Layer::Create();
7350 child_->AddChild(grand_child_.get());
7351 root_->AddChild(child_.get());
[email protected]10aabcc32012-12-13 09:18:597352
[email protected]fb661802013-03-25 01:59:327353 gfx::Transform identity_matrix;
7354 SetLayerPropertiesForTesting(root_,
7355 identity_matrix,
7356 identity_matrix,
7357 gfx::PointF(),
7358 gfx::PointF(),
7359 gfx::Size(1, 1),
7360 false);
7361 SetLayerPropertiesForTesting(child_,
7362 identity_matrix,
7363 identity_matrix,
7364 gfx::PointF(),
7365 gfx::PointF(),
7366 gfx::Size(1, 1),
7367 false);
7368 SetLayerPropertiesForTesting(grand_child_,
7369 identity_matrix,
7370 identity_matrix,
7371 gfx::PointF(),
7372 gfx::PointF(),
7373 gfx::Size(1, 1),
7374 false);
[email protected]10aabcc32012-12-13 09:18:597375
[email protected]fb661802013-03-25 01:59:327376 child_->SetForceRenderSurface(std::tr1::get<1>(GetParam()));
7377 }
[email protected]10aabcc32012-12-13 09:18:597378
[email protected]fb661802013-03-25 01:59:327379 bool can_use_lcd_text_;
7380 scoped_refptr<Layer> root_;
7381 scoped_refptr<Layer> child_;
7382 scoped_refptr<Layer> grand_child_;
[email protected]10aabcc32012-12-13 09:18:597383};
7384
[email protected]fb661802013-03-25 01:59:327385TEST_P(LCDTextTest, CanUseLCDText) {
7386 // Case 1: Identity transform.
7387 gfx::Transform identity_matrix;
[email protected]f2136262013-04-26 21:10:197388 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_);
[email protected]fb661802013-03-25 01:59:327389 EXPECT_EQ(can_use_lcd_text_, root_->can_use_lcd_text());
7390 EXPECT_EQ(can_use_lcd_text_, child_->can_use_lcd_text());
7391 EXPECT_EQ(can_use_lcd_text_, grand_child_->can_use_lcd_text());
[email protected]10aabcc32012-12-13 09:18:597392
[email protected]fb661802013-03-25 01:59:327393 // Case 2: Integral translation.
7394 gfx::Transform integral_translation;
7395 integral_translation.Translate(1.0, 2.0);
7396 child_->SetTransform(integral_translation);
[email protected]f2136262013-04-26 21:10:197397 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_);
[email protected]fb661802013-03-25 01:59:327398 EXPECT_EQ(can_use_lcd_text_, root_->can_use_lcd_text());
7399 EXPECT_EQ(can_use_lcd_text_, child_->can_use_lcd_text());
7400 EXPECT_EQ(can_use_lcd_text_, grand_child_->can_use_lcd_text());
[email protected]10aabcc32012-12-13 09:18:597401
[email protected]fb661802013-03-25 01:59:327402 // Case 3: Non-integral translation.
7403 gfx::Transform non_integral_translation;
7404 non_integral_translation.Translate(1.5, 2.5);
7405 child_->SetTransform(non_integral_translation);
[email protected]f2136262013-04-26 21:10:197406 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_);
[email protected]fb661802013-03-25 01:59:327407 EXPECT_EQ(can_use_lcd_text_, root_->can_use_lcd_text());
7408 EXPECT_FALSE(child_->can_use_lcd_text());
7409 EXPECT_FALSE(grand_child_->can_use_lcd_text());
[email protected]10aabcc32012-12-13 09:18:597410
[email protected]fb661802013-03-25 01:59:327411 // Case 4: Rotation.
7412 gfx::Transform rotation;
7413 rotation.Rotate(10.0);
7414 child_->SetTransform(rotation);
[email protected]f2136262013-04-26 21:10:197415 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_);
[email protected]fb661802013-03-25 01:59:327416 EXPECT_EQ(can_use_lcd_text_, root_->can_use_lcd_text());
7417 EXPECT_FALSE(child_->can_use_lcd_text());
7418 EXPECT_FALSE(grand_child_->can_use_lcd_text());
[email protected]10aabcc32012-12-13 09:18:597419
[email protected]fb661802013-03-25 01:59:327420 // Case 5: Scale.
7421 gfx::Transform scale;
7422 scale.Scale(2.0, 2.0);
7423 child_->SetTransform(scale);
[email protected]f2136262013-04-26 21:10:197424 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_);
[email protected]fb661802013-03-25 01:59:327425 EXPECT_EQ(can_use_lcd_text_, root_->can_use_lcd_text());
7426 EXPECT_FALSE(child_->can_use_lcd_text());
7427 EXPECT_FALSE(grand_child_->can_use_lcd_text());
[email protected]10aabcc32012-12-13 09:18:597428
[email protected]fb661802013-03-25 01:59:327429 // Case 6: Skew.
7430 gfx::Transform skew;
7431 skew.SkewX(10.0);
7432 child_->SetTransform(skew);
[email protected]f2136262013-04-26 21:10:197433 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_);
[email protected]fb661802013-03-25 01:59:327434 EXPECT_EQ(can_use_lcd_text_, root_->can_use_lcd_text());
7435 EXPECT_FALSE(child_->can_use_lcd_text());
7436 EXPECT_FALSE(grand_child_->can_use_lcd_text());
[email protected]10aabcc32012-12-13 09:18:597437
[email protected]fb661802013-03-25 01:59:327438 // Case 7: Translucent.
7439 child_->SetTransform(identity_matrix);
7440 child_->SetOpacity(0.5f);
[email protected]f2136262013-04-26 21:10:197441 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_);
[email protected]fb661802013-03-25 01:59:327442 EXPECT_EQ(can_use_lcd_text_, root_->can_use_lcd_text());
7443 EXPECT_FALSE(child_->can_use_lcd_text());
7444 EXPECT_FALSE(grand_child_->can_use_lcd_text());
[email protected]10aabcc32012-12-13 09:18:597445
[email protected]fb661802013-03-25 01:59:327446 // Case 8: Sanity check: restore transform and opacity.
7447 child_->SetTransform(identity_matrix);
7448 child_->SetOpacity(1.f);
[email protected]f2136262013-04-26 21:10:197449 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_);
[email protected]fb661802013-03-25 01:59:327450 EXPECT_EQ(can_use_lcd_text_, root_->can_use_lcd_text());
7451 EXPECT_EQ(can_use_lcd_text_, child_->can_use_lcd_text());
7452 EXPECT_EQ(can_use_lcd_text_, grand_child_->can_use_lcd_text());
[email protected]10aabcc32012-12-13 09:18:597453}
7454
[email protected]fb661802013-03-25 01:59:327455TEST_P(LCDTextTest, verifycan_use_lcd_textWithAnimation) {
7456 // Sanity check: Make sure can_use_lcd_text_ is set on each node.
[email protected]f2136262013-04-26 21:10:197457 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_);
[email protected]fb661802013-03-25 01:59:327458 EXPECT_EQ(can_use_lcd_text_, root_->can_use_lcd_text());
7459 EXPECT_EQ(can_use_lcd_text_, child_->can_use_lcd_text());
7460 EXPECT_EQ(can_use_lcd_text_, grand_child_->can_use_lcd_text());
[email protected]10aabcc32012-12-13 09:18:597461
[email protected]fb661802013-03-25 01:59:327462 // Add opacity animation.
7463 child_->SetOpacity(0.9f);
7464 AddOpacityTransitionToController(
7465 child_->layer_animation_controller(), 10.0, 0.9f, 0.1f, false);
[email protected]10aabcc32012-12-13 09:18:597466
[email protected]f2136262013-04-26 21:10:197467 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_);
[email protected]fb661802013-03-25 01:59:327468 // Text AA should not be adjusted while animation is active.
7469 // Make sure LCD text AA setting remains unchanged.
7470 EXPECT_EQ(can_use_lcd_text_, root_->can_use_lcd_text());
7471 EXPECT_EQ(can_use_lcd_text_, child_->can_use_lcd_text());
7472 EXPECT_EQ(can_use_lcd_text_, grand_child_->can_use_lcd_text());
[email protected]10aabcc32012-12-13 09:18:597473}
7474
7475INSTANTIATE_TEST_CASE_P(LayerTreeHostCommonTest,
7476 LCDTextTest,
[email protected]fb661802013-03-25 01:59:327477 testing::Combine(testing::Bool(), testing::Bool()));
[email protected]10aabcc32012-12-13 09:18:597478
[email protected]ba565742012-11-10 09:29:487479} // namespace
7480} // namespace cc