diff options
author | vollick@chromium.org <vollick@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-12 19:07:28 +0000 |
---|---|---|
committer | vollick@chromium.org <vollick@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-12 19:07:28 +0000 |
commit | 28336d567bf426294b8d64e3320f42eced553ea8 (patch) | |
tree | 90c52047d657ec1f694ffedefcf000ce979ae833 | |
parent | 6edb767d519b53d2d5d8ce20fc3ca62cdd6d31e5 (diff) | |
download | chromium_src-28336d567bf426294b8d64e3320f42eced553ea8.zip chromium_src-28336d567bf426294b8d64e3320f42eced553ea8.tar.gz chromium_src-28336d567bf426294b8d64e3320f42eced553ea8.tar.bz2 |
Hit test on the layer tree rather than the RSLL
R=danakj@chromium.org,enne@chromium.org
BUG=369757,368935
Review URL: https://codereview.chromium.org/266913021
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@269835 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | cc/base/math_util.cc | 32 | ||||
-rw-r--r-- | cc/base/math_util.h | 5 | ||||
-rw-r--r-- | cc/cc_tests.gyp | 3 | ||||
-rw-r--r-- | cc/test/layer_tree_host_common_test.cc | 113 | ||||
-rw-r--r-- | cc/test/layer_tree_host_common_test.h | 129 | ||||
-rw-r--r-- | cc/trees/layer_tree_host_common.cc | 217 | ||||
-rw-r--r-- | cc/trees/layer_tree_host_common.h | 18 | ||||
-rw-r--r-- | cc/trees/layer_tree_host_common_unittest.cc | 2148 | ||||
-rw-r--r-- | cc/trees/layer_tree_host_impl.cc | 25 | ||||
-rw-r--r-- | cc/trees/layer_tree_host_impl_unittest.cc | 93 | ||||
-rw-r--r-- | cc/trees/layer_tree_impl.cc | 262 | ||||
-rw-r--r-- | cc/trees/layer_tree_impl.h | 8 | ||||
-rw-r--r-- | cc/trees/layer_tree_impl_unittest.cc | 1940 |
13 files changed, 2583 insertions, 2410 deletions
diff --git a/cc/base/math_util.cc b/cc/base/math_util.cc index ca6e571..641d649 100644 --- a/cc/base/math_util.cc +++ b/cc/base/math_util.cc @@ -41,6 +41,15 @@ static HomogeneousCoordinate ProjectHomogeneousPoint( return result; } +static HomogeneousCoordinate ProjectHomogeneousPoint( + const gfx::Transform& transform, + const gfx::PointF& p, + bool* clipped) { + HomogeneousCoordinate h = ProjectHomogeneousPoint(transform, p); + *clipped = h.w() <= 0; + return h; +} + static HomogeneousCoordinate MapHomogeneousPoint( const gfx::Transform& transform, const gfx::Point3F& p) { @@ -444,28 +453,27 @@ gfx::QuadF MathUtil::ProjectQuad(const gfx::Transform& transform, gfx::PointF MathUtil::ProjectPoint(const gfx::Transform& transform, const gfx::PointF& p, bool* clipped) { - HomogeneousCoordinate h = ProjectHomogeneousPoint(transform, p); - - if (h.w() > 0) { - // The cartesian coordinates will be valid in this case. - *clipped = false; - return h.CartesianPoint2d(); - } - - // The cartesian coordinates will be invalid after dividing by w. - *clipped = true; - + HomogeneousCoordinate h = ProjectHomogeneousPoint(transform, p, clipped); // Avoid dividing by w if w == 0. if (!h.w()) return gfx::PointF(); - // This return value will be invalid because clipped == true, but (1) users of + // This return value will be invalid if clipped == true, but (1) users of // this code should be ignoring the return value when clipped == true anyway, // and (2) this behavior is more consistent with existing behavior of WebKit // transforms if the user really does not ignore the return value. return h.CartesianPoint2d(); } +gfx::Point3F MathUtil::ProjectPoint3D(const gfx::Transform& transform, + const gfx::PointF& p, + bool* clipped) { + HomogeneousCoordinate h = ProjectHomogeneousPoint(transform, p, clipped); + if (!h.w()) + return gfx::Point3F(); + return h.CartesianPoint3d(); +} + gfx::RectF MathUtil::ScaleRectProportional(const gfx::RectF& input_outer_rect, const gfx::RectF& scale_outer_rect, const gfx::RectF& scale_inner_rect) { diff --git a/cc/base/math_util.h b/cc/base/math_util.h index 37b71dd..6a13a38 100644 --- a/cc/base/math_util.h +++ b/cc/base/math_util.h @@ -144,6 +144,11 @@ class CC_EXPORT MathUtil { static gfx::PointF ProjectPoint(const gfx::Transform& transform, const gfx::PointF& point, bool* clipped); + // Identical to the above function, but coerces the homogeneous coordinate to + // a 3d rather than a 2d point. + static gfx::Point3F ProjectPoint3D(const gfx::Transform& transform, + const gfx::PointF& point, + bool* clipped); static gfx::Vector2dF ComputeTransform2dScaleComponents(const gfx::Transform&, float fallbackValue); diff --git a/cc/cc_tests.gyp b/cc/cc_tests.gyp index dc29a77..8ef6803 100644 --- a/cc/cc_tests.gyp +++ b/cc/cc_tests.gyp @@ -107,6 +107,7 @@ 'trees/layer_tree_host_unittest_picture.cc', 'trees/layer_tree_host_unittest_scroll.cc', 'trees/layer_tree_host_unittest_video.cc', + 'trees/layer_tree_impl_unittest.cc', 'trees/occlusion_tracker_unittest.cc', 'trees/tree_synchronizer_unittest.cc', ], @@ -179,6 +180,8 @@ 'test/impl_side_painting_settings.h', 'test/layer_test_common.cc', 'test/layer_test_common.h', + 'test/layer_tree_host_common_test.cc', + 'test/layer_tree_host_common_test.h', 'test/layer_tree_json_parser.cc', 'test/layer_tree_json_parser.h', 'test/layer_tree_pixel_test.cc', diff --git a/cc/test/layer_tree_host_common_test.cc b/cc/test/layer_tree_host_common_test.cc new file mode 100644 index 0000000..61dd401 --- /dev/null +++ b/cc/test/layer_tree_host_common_test.cc @@ -0,0 +1,113 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "cc/test/layer_tree_host_common_test.h" + +#include "cc/layers/layer.h" +#include "cc/layers/layer_impl.h" +#include "cc/trees/layer_tree_host_common.h" + +namespace cc { + +LayerTreeHostCommonTestBase::LayerTreeHostCommonTestBase() + : render_surface_layer_list_count_(0) { +} + +LayerTreeHostCommonTestBase::~LayerTreeHostCommonTestBase() { +} + +void LayerTreeHostCommonTestBase::SetLayerPropertiesForTesting( + Layer* layer, + const gfx::Transform& transform, + const gfx::PointF& anchor, + const gfx::PointF& position, + const gfx::Size& bounds, + bool flatten_transform, + bool is_3d_sorted) { + SetLayerPropertiesForTestingInternal<Layer>(layer, + transform, + anchor, + position, + bounds, + flatten_transform, + is_3d_sorted); +} + +void LayerTreeHostCommonTestBase::SetLayerPropertiesForTesting( + LayerImpl* layer, + const gfx::Transform& transform, + const gfx::PointF& anchor, + const gfx::PointF& position, + const gfx::Size& bounds, + bool flatten_transform, + bool is_3d_sorted) { + SetLayerPropertiesForTestingInternal<LayerImpl>(layer, + transform, + anchor, + position, + bounds, + flatten_transform, + is_3d_sorted); + layer->SetContentBounds(bounds); +} + +void LayerTreeHostCommonTestBase::ExecuteCalculateDrawProperties( + Layer* root_layer, + float device_scale_factor, + float page_scale_factor, + Layer* page_scale_application_layer, + bool can_use_lcd_text) { + EXPECT_TRUE(page_scale_application_layer || (page_scale_factor == 1.f)); + gfx::Transform identity_matrix; + gfx::Size device_viewport_size = + gfx::Size(root_layer->bounds().width() * device_scale_factor, + root_layer->bounds().height() * device_scale_factor); + + render_surface_layer_list_.reset(new RenderSurfaceLayerList); + + // We are probably not testing what is intended if the root_layer bounds are + // empty. + DCHECK(!root_layer->bounds().IsEmpty()); + LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs( + root_layer, device_viewport_size, render_surface_layer_list_.get()); + inputs.device_scale_factor = device_scale_factor; + inputs.page_scale_factor = page_scale_factor; + inputs.page_scale_application_layer = page_scale_application_layer; + inputs.can_use_lcd_text = can_use_lcd_text; + inputs.can_adjust_raster_scales = true; + LayerTreeHostCommon::CalculateDrawProperties(&inputs); +} + +void LayerTreeHostCommonTestBase::ExecuteCalculateDrawProperties( + LayerImpl* root_layer, + float device_scale_factor, + float page_scale_factor, + LayerImpl* page_scale_application_layer, + bool can_use_lcd_text) { + gfx::Transform identity_matrix; + gfx::Size device_viewport_size = + gfx::Size(root_layer->bounds().width() * device_scale_factor, + root_layer->bounds().height() * device_scale_factor); + + render_surface_layer_list_impl_.reset(new LayerImplList); + + // We are probably not testing what is intended if the root_layer bounds are + // empty. + DCHECK(!root_layer->bounds().IsEmpty()); + LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( + root_layer, device_viewport_size, render_surface_layer_list_impl_.get()); + inputs.device_scale_factor = device_scale_factor; + inputs.page_scale_factor = page_scale_factor; + inputs.page_scale_application_layer = page_scale_application_layer; + inputs.can_use_lcd_text = can_use_lcd_text; + inputs.can_adjust_raster_scales = true; + + ++render_surface_layer_list_count_; + inputs.current_render_surface_layer_list_id = + render_surface_layer_list_count_; + + LayerTreeHostCommon::CalculateDrawProperties(&inputs); +} + +} // namespace cc diff --git a/cc/test/layer_tree_host_common_test.h b/cc/test/layer_tree_host_common_test.h new file mode 100644 index 0000000..896388e --- /dev/null +++ b/cc/test/layer_tree_host_common_test.h @@ -0,0 +1,129 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CC_TEST_LAYER_TREE_HOST_COMMON_TEST_H_ +#define CC_TEST_LAYER_TREE_HOST_COMMON_TEST_H_ + +#include <vector> + +#include "base/memory/scoped_ptr.h" +#include "cc/layers/layer_lists.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace gfx { +class PointF; +class Size; +class Transform; +} + +namespace cc { + +class Layer; +class LayerImpl; +class RenderSurfaceLayerList; + +class LayerTreeHostCommonTestBase { + protected: + LayerTreeHostCommonTestBase(); + virtual ~LayerTreeHostCommonTestBase(); + + template <typename LayerType> + void SetLayerPropertiesForTestingInternal(LayerType* layer, + const gfx::Transform& transform, + const gfx::PointF& anchor, + const gfx::PointF& position, + const gfx::Size& bounds, + bool flatten_transform, + bool is_3d_sorted) { + layer->SetTransform(transform); + layer->SetAnchorPoint(anchor); + layer->SetPosition(position); + layer->SetBounds(bounds); + layer->SetShouldFlattenTransform(flatten_transform); + layer->SetIs3dSorted(is_3d_sorted); + } + + void SetLayerPropertiesForTesting(Layer* layer, + const gfx::Transform& transform, + const gfx::PointF& anchor, + const gfx::PointF& position, + const gfx::Size& bounds, + bool flatten_transform, + bool is_3d_sorted); + + void SetLayerPropertiesForTesting(LayerImpl* layer, + const gfx::Transform& transform, + const gfx::PointF& anchor, + const gfx::PointF& position, + const gfx::Size& bounds, + bool flatten_transform, + bool is_3d_sorted); + + void ExecuteCalculateDrawProperties(Layer* root_layer, + float device_scale_factor, + float page_scale_factor, + Layer* page_scale_application_layer, + bool can_use_lcd_text); + + void ExecuteCalculateDrawProperties(LayerImpl* root_layer, + float device_scale_factor, + float page_scale_factor, + LayerImpl* page_scale_application_layer, + bool can_use_lcd_text); + + template <class LayerType> + void ExecuteCalculateDrawProperties(LayerType* root_layer) { + LayerType* page_scale_application_layer = NULL; + ExecuteCalculateDrawProperties( + root_layer, 1.f, 1.f, page_scale_application_layer, false); + } + + template <class LayerType> + void ExecuteCalculateDrawProperties(LayerType* root_layer, + float device_scale_factor) { + LayerType* page_scale_application_layer = NULL; + ExecuteCalculateDrawProperties(root_layer, + device_scale_factor, + 1.f, + page_scale_application_layer, + false); + } + + template <class LayerType> + void ExecuteCalculateDrawProperties(LayerType* root_layer, + float device_scale_factor, + float page_scale_factor, + LayerType* page_scale_application_layer) { + ExecuteCalculateDrawProperties(root_layer, + device_scale_factor, + page_scale_factor, + page_scale_application_layer, + false); + } + + RenderSurfaceLayerList* render_surface_layer_list() const { + return render_surface_layer_list_.get(); + } + + LayerImplList* render_surface_layer_list_impl() const { + return render_surface_layer_list_impl_.get(); + } + + int render_surface_layer_list_count() const { + return render_surface_layer_list_count_; + } + + private: + scoped_ptr<RenderSurfaceLayerList> render_surface_layer_list_; + scoped_ptr<std::vector<LayerImpl*> > render_surface_layer_list_impl_; + + int render_surface_layer_list_count_; +}; + +class LayerTreeHostCommonTest : public LayerTreeHostCommonTestBase, + public testing::Test {}; + +} // namespace cc + +#endif // CC_TEST_LAYER_TREE_HOST_COMMON_TEST_H_ diff --git a/cc/trees/layer_tree_host_common.cc b/cc/trees/layer_tree_host_common.cc index 8a5a552..a9af3ef 100644 --- a/cc/trees/layer_tree_host_common.cc +++ b/cc/trees/layer_tree_host_common.cc @@ -16,7 +16,6 @@ #include "cc/layers/render_surface_impl.h" #include "cc/trees/layer_sorter.h" #include "cc/trees/layer_tree_impl.h" -#include "ui/gfx/point_conversions.h" #include "ui/gfx/rect_conversions.h" #include "ui/gfx/transform.h" @@ -2427,220 +2426,4 @@ void LayerTreeHostCommon::CalculateDrawProperties( DCHECK(inputs->root_layer->render_surface()); } -static bool PointHitsRect( - const gfx::PointF& screen_space_point, - const gfx::Transform& local_space_to_screen_space_transform, - const gfx::RectF& local_space_rect) { - // If the transform is not invertible, then assume that this point doesn't hit - // this rect. - gfx::Transform inverse_local_space_to_screen_space( - gfx::Transform::kSkipInitialization); - if (!local_space_to_screen_space_transform.GetInverse( - &inverse_local_space_to_screen_space)) - return false; - - // Transform the hit test point from screen space to the local space of the - // given rect. - bool clipped = false; - gfx::PointF hit_test_point_in_local_space = MathUtil::ProjectPoint( - inverse_local_space_to_screen_space, screen_space_point, &clipped); - - // If ProjectPoint could not project to a valid value, then we assume that - // this point doesn't hit this rect. - if (clipped) - return false; - - return local_space_rect.Contains(hit_test_point_in_local_space); -} - -static bool PointHitsRegion(const gfx::PointF& screen_space_point, - const gfx::Transform& screen_space_transform, - const Region& layer_space_region, - float layer_content_scale_x, - float layer_content_scale_y) { - // If the transform is not invertible, then assume that this point doesn't hit - // this region. - gfx::Transform inverse_screen_space_transform( - gfx::Transform::kSkipInitialization); - if (!screen_space_transform.GetInverse(&inverse_screen_space_transform)) - return false; - - // Transform the hit test point from screen space to the local space of the - // given region. - bool clipped = false; - gfx::PointF hit_test_point_in_content_space = MathUtil::ProjectPoint( - inverse_screen_space_transform, screen_space_point, &clipped); - gfx::PointF hit_test_point_in_layer_space = - gfx::ScalePoint(hit_test_point_in_content_space, - 1.f / layer_content_scale_x, - 1.f / layer_content_scale_y); - - // If ProjectPoint could not project to a valid value, then we assume that - // this point doesn't hit this region. - if (clipped) - return false; - - return layer_space_region.Contains( - gfx::ToRoundedPoint(hit_test_point_in_layer_space)); -} - -static bool PointIsClippedBySurfaceOrClipRect( - const gfx::PointF& screen_space_point, - LayerImpl* layer) { - LayerImpl* current_layer = layer; - - // Walk up the layer tree and hit-test any render_surfaces and any layer - // clip rects that are active. - while (current_layer) { - if (current_layer->render_surface() && - !PointHitsRect( - screen_space_point, - current_layer->render_surface()->screen_space_transform(), - current_layer->render_surface()->content_rect())) - return true; - - // Note that drawable content rects are actually in target surface space, so - // the transform we have to provide is the target surface's - // screen_space_transform. - LayerImpl* render_target = current_layer->render_target(); - if (LayerClipsSubtree(current_layer) && - !PointHitsRect( - screen_space_point, - render_target->render_surface()->screen_space_transform(), - current_layer->drawable_content_rect())) - return true; - - current_layer = current_layer->parent(); - } - - // If we have finished walking all ancestors without having already exited, - // then the point is not clipped by any ancestors. - return false; -} - -static bool PointHitsLayer(LayerImpl* layer, - const gfx::PointF& screen_space_point) { - gfx::RectF content_rect(layer->content_bounds()); - if (!PointHitsRect( - screen_space_point, layer->screen_space_transform(), content_rect)) - return false; - - // At this point, we think the point does hit the layer, but we need to walk - // up the parents to ensure that the layer was not clipped in such a way - // that the hit point actually should not hit the layer. - if (PointIsClippedBySurfaceOrClipRect(screen_space_point, layer)) - return false; - - // Skip the HUD layer. - if (layer == layer->layer_tree_impl()->hud_layer()) - return false; - - return true; -} - -LayerImpl* LayerTreeHostCommon::FindFirstScrollingLayerThatIsHitByPoint( - const gfx::PointF& screen_space_point, - const LayerImplList& render_surface_layer_list) { - typedef LayerIterator<LayerImpl> LayerIteratorType; - LayerIteratorType end = LayerIteratorType::End(&render_surface_layer_list); - for (LayerIteratorType it = - LayerIteratorType::Begin(&render_surface_layer_list); - it != end; - ++it) { - // We don't want to consider render_surfaces for hit testing. - if (!it.represents_itself()) - continue; - - LayerImpl* current_layer = (*it); - if (!PointHitsLayer(current_layer, screen_space_point)) - continue; - - if (current_layer->scrollable()) - return current_layer; - } - - return NULL; -} - -LayerImpl* LayerTreeHostCommon::FindLayerThatIsHitByPoint( - const gfx::PointF& screen_space_point, - const LayerImplList& render_surface_layer_list) { - LayerImpl* found_layer = NULL; - - typedef LayerIterator<LayerImpl> LayerIteratorType; - LayerIteratorType end = LayerIteratorType::End(&render_surface_layer_list); - for (LayerIteratorType - it = LayerIteratorType::Begin(&render_surface_layer_list); - it != end; - ++it) { - // We don't want to consider render_surfaces for hit testing. - if (!it.represents_itself()) - continue; - - LayerImpl* current_layer = (*it); - if (!PointHitsLayer(current_layer, screen_space_point)) - continue; - - found_layer = current_layer; - break; - } - - // This can potentially return NULL, which means the screen_space_point did - // not successfully hit test any layers, not even the root layer. - return found_layer; -} - -LayerImpl* LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - const gfx::PointF& screen_space_point, - const LayerImplList& render_surface_layer_list) { - typedef LayerIterator<LayerImpl> LayerIteratorType; - LayerIteratorType end = LayerIteratorType::End(&render_surface_layer_list); - for (LayerIteratorType it = - LayerIteratorType::Begin(&render_surface_layer_list); - it != end; - ++it) { - // We don't want to consider render_surfaces for hit testing. - if (!it.represents_itself()) - continue; - - LayerImpl* current_layer = (*it); - if (!PointHitsLayer(current_layer, screen_space_point)) - continue; - - if (LayerTreeHostCommon::LayerHasTouchEventHandlersAt(screen_space_point, - current_layer)) - return current_layer; - - // Note that we could stop searching if we hit a layer we know to be - // opaque to hit-testing, but knowing that reliably is tricky (eg. due to - // CSS pointer-events: none). Also blink has an optimization for the - // common case of an entire document having handlers where it doesn't - // report any rects for child layers (since it knows they can't exceed - // the document bounds). - } - return NULL; -} - -bool LayerTreeHostCommon::LayerHasTouchEventHandlersAt( - const gfx::PointF& screen_space_point, - LayerImpl* layer_impl) { - if (layer_impl->touch_event_handler_region().IsEmpty()) - return false; - - if (!PointHitsRegion(screen_space_point, - layer_impl->screen_space_transform(), - layer_impl->touch_event_handler_region(), - layer_impl->contents_scale_x(), - layer_impl->contents_scale_y())) - return false; - - // At this point, we think the point does hit the touch event handler region - // on the layer, but we need to walk up the parents to ensure that the layer - // was not clipped in such a way that the hit point actually should not hit - // the layer. - if (PointIsClippedBySurfaceOrClipRect(screen_space_point, layer_impl)) - return false; - - return true; -} } // namespace cc diff --git a/cc/trees/layer_tree_host_common.h b/cc/trees/layer_tree_host_common.h index 5090c69..0e13947 100644 --- a/cc/trees/layer_tree_host_common.h +++ b/cc/trees/layer_tree_host_common.h @@ -99,24 +99,6 @@ class CC_EXPORT LayerTreeHostCommon { CalcDrawPropsImplInputsForTesting; static void CalculateDrawProperties(CalcDrawPropsImplInputs* inputs); - // Performs hit testing for a given render_surface_layer_list. - static LayerImpl* FindFirstScrollingLayerThatIsHitByPoint( - const gfx::PointF& screen_space_point, - const LayerImplList& render_surface_layer_list); - - // Performs hit testing for a given render_surface_layer_list. - static LayerImpl* FindLayerThatIsHitByPoint( - const gfx::PointF& screen_space_point, - const LayerImplList& render_surface_layer_list); - - static LayerImpl* FindLayerThatIsHitByPointInTouchHandlerRegion( - const gfx::PointF& screen_space_point, - const LayerImplList& render_surface_layer_list); - - static bool LayerHasTouchEventHandlersAt( - const gfx::PointF& screen_space_point, - LayerImpl* layer_impl); - template <typename LayerType> static bool RenderSurfaceContributesToTarget(LayerType*, int target_surface_layer_id); diff --git a/cc/trees/layer_tree_host_common_unittest.cc b/cc/trees/layer_tree_host_common_unittest.cc index 48a665d..43d94da 100644 --- a/cc/trees/layer_tree_host_common_unittest.cc +++ b/cc/trees/layer_tree_host_common_unittest.cc @@ -11,7 +11,6 @@ #include "cc/base/math_util.h" #include "cc/layers/content_layer.h" #include "cc/layers/content_layer_client.h" -#include "cc/layers/heads_up_display_layer_impl.h" #include "cc/layers/layer.h" #include "cc/layers/layer_client.h" #include "cc/layers/layer_impl.h" @@ -25,183 +24,18 @@ #include "cc/test/fake_layer_tree_host.h" #include "cc/test/fake_layer_tree_host_impl.h" #include "cc/test/geometry_test_utils.h" +#include "cc/test/layer_tree_host_common_test.h" #include "cc/trees/layer_tree_impl.h" #include "cc/trees/proxy.h" #include "cc/trees/single_thread_proxy.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" #include "ui/gfx/quad_f.h" -#include "ui/gfx/size_conversions.h" #include "ui/gfx/transform.h" namespace cc { namespace { -class LayerTreeHostCommonTestBase { - protected: - LayerTreeHostCommonTestBase() : render_surface_layer_list_count_(0) {} - - template <typename LayerType> - void SetLayerPropertiesForTestingInternal( - LayerType* layer, - const gfx::Transform& transform, - const gfx::PointF& anchor, - const gfx::PointF& position, - const gfx::Size& bounds, - bool flatten_transform, - bool is_3d_sorted) { - layer->SetTransform(transform); - layer->SetAnchorPoint(anchor); - layer->SetPosition(position); - layer->SetBounds(bounds); - layer->SetShouldFlattenTransform(flatten_transform); - layer->SetIs3dSorted(is_3d_sorted); - } - - void SetLayerPropertiesForTesting(Layer* layer, - const gfx::Transform& transform, - const gfx::PointF& anchor, - const gfx::PointF& position, - const gfx::Size& bounds, - bool flatten_transform, - bool is_3d_sorted) { - SetLayerPropertiesForTestingInternal<Layer>(layer, - transform, - anchor, - position, - bounds, - flatten_transform, - is_3d_sorted); - } - - void SetLayerPropertiesForTesting(LayerImpl* layer, - const gfx::Transform& transform, - const gfx::PointF& anchor, - const gfx::PointF& position, - const gfx::Size& bounds, - bool flatten_transform, - bool is_3d_sorted) { - SetLayerPropertiesForTestingInternal<LayerImpl>(layer, - transform, - anchor, - position, - bounds, - flatten_transform, - is_3d_sorted); - layer->SetContentBounds(bounds); - } - - void ExecuteCalculateDrawProperties(Layer* root_layer, - float device_scale_factor, - float page_scale_factor, - Layer* page_scale_application_layer, - bool can_use_lcd_text) { - EXPECT_TRUE(page_scale_application_layer || (page_scale_factor == 1.f)); - gfx::Transform identity_matrix; - gfx::Size device_viewport_size = - gfx::Size(root_layer->bounds().width() * device_scale_factor, - root_layer->bounds().height() * device_scale_factor); - - render_surface_layer_list_.reset(new RenderSurfaceLayerList); - - // We are probably not testing what is intended if the root_layer bounds are - // empty. - DCHECK(!root_layer->bounds().IsEmpty()); - LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs( - root_layer, device_viewport_size, render_surface_layer_list_.get()); - inputs.device_scale_factor = device_scale_factor; - inputs.page_scale_factor = page_scale_factor; - inputs.page_scale_application_layer = page_scale_application_layer; - inputs.can_use_lcd_text = can_use_lcd_text; - inputs.can_adjust_raster_scales = true; - LayerTreeHostCommon::CalculateDrawProperties(&inputs); - } - - void ExecuteCalculateDrawProperties(LayerImpl* root_layer, - float device_scale_factor, - float page_scale_factor, - LayerImpl* page_scale_application_layer, - bool can_use_lcd_text) { - gfx::Transform identity_matrix; - gfx::Size device_viewport_size = - gfx::Size(root_layer->bounds().width() * device_scale_factor, - root_layer->bounds().height() * device_scale_factor); - - render_surface_layer_list_impl_.reset(new LayerImplList); - - // We are probably not testing what is intended if the root_layer bounds are - // empty. - DCHECK(!root_layer->bounds().IsEmpty()); - LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( - root_layer, - device_viewport_size, - render_surface_layer_list_impl_.get()); - inputs.device_scale_factor = device_scale_factor; - inputs.page_scale_factor = page_scale_factor; - inputs.page_scale_application_layer = page_scale_application_layer; - inputs.can_use_lcd_text = can_use_lcd_text; - inputs.can_adjust_raster_scales = true; - - ++render_surface_layer_list_count_; - inputs.current_render_surface_layer_list_id = - render_surface_layer_list_count_; - - LayerTreeHostCommon::CalculateDrawProperties(&inputs); - } - - template <class LayerType> - void ExecuteCalculateDrawProperties(LayerType* root_layer) { - LayerType* page_scale_application_layer = NULL; - ExecuteCalculateDrawProperties( - root_layer, 1.f, 1.f, page_scale_application_layer, false); - } - - template <class LayerType> - void ExecuteCalculateDrawProperties(LayerType* root_layer, - float device_scale_factor) { - LayerType* page_scale_application_layer = NULL; - ExecuteCalculateDrawProperties(root_layer, - device_scale_factor, - 1.f, - page_scale_application_layer, - false); - } - - template <class LayerType> - void ExecuteCalculateDrawProperties(LayerType* root_layer, - float device_scale_factor, - float page_scale_factor, - LayerType* page_scale_application_layer) { - ExecuteCalculateDrawProperties(root_layer, - device_scale_factor, - page_scale_factor, - page_scale_application_layer, - false); - } - - RenderSurfaceLayerList* render_surface_layer_list() const { - return render_surface_layer_list_.get(); - } - - LayerImplList* render_surface_layer_list_impl() const { - return render_surface_layer_list_impl_.get(); - } - - int render_surface_layer_list_count() const { - return render_surface_layer_list_count_; - } - - private: - scoped_ptr<RenderSurfaceLayerList> render_surface_layer_list_; - scoped_ptr<LayerImplList> render_surface_layer_list_impl_; - - int render_surface_layer_list_count_; -}; - -class LayerTreeHostCommonTest : public LayerTreeHostCommonTestBase, - public testing::Test { -}; - class LayerWithForcedDrawsContent : public Layer { public: LayerWithForcedDrawsContent() : Layer(), last_device_scale_factor_(0.f) {} @@ -4161,1986 +3995,6 @@ TEST_F(LayerTreeHostCommonTest, ->render_surface()->layer_list().at(1)->id()); } - -TEST_F(LayerTreeHostCommonTest, HitTestingForEmptyLayerList) { - // Hit testing on an empty render_surface_layer_list should return a null - // pointer. - LayerImplList render_surface_layer_list; - - gfx::Point test_point(0, 0); - LayerImpl* result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - test_point = gfx::Point(10, 20); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); -} - -TEST_F(LayerTreeHostCommonTest, HitTestingForSingleLayer) { - FakeImplProxy proxy; - TestSharedBitmapManager shared_bitmap_manager; - FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); - scoped_ptr<LayerImpl> root = - LayerImpl::Create(host_impl.active_tree(), 12345); - - gfx::Transform identity_matrix; - gfx::PointF anchor; - gfx::PointF position; - gfx::Size bounds(100, 100); - SetLayerPropertiesForTesting(root.get(), - identity_matrix, - anchor, - position, - bounds, - true, - false); - root->SetDrawsContent(true); - - LayerImplList render_surface_layer_list; - LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( - root.get(), root->bounds(), &render_surface_layer_list); - inputs.can_adjust_raster_scales = true; - LayerTreeHostCommon::CalculateDrawProperties(&inputs); - - // Sanity check the scenario we just created. - ASSERT_EQ(1u, render_surface_layer_list.size()); - ASSERT_EQ(1u, root->render_surface()->layer_list().size()); - - // Hit testing for a point outside the layer should return a null pointer. - gfx::Point test_point(101, 101); - LayerImpl* result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - test_point = gfx::Point(-1, -1); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - // Hit testing for a point inside should return the root layer. - test_point = gfx::Point(1, 1); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(12345, result_layer->id()); - - test_point = gfx::Point(99, 99); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(12345, result_layer->id()); -} - -TEST_F(LayerTreeHostCommonTest, HitTestingForSingleLayerAndHud) { - FakeImplProxy proxy; - TestSharedBitmapManager shared_bitmap_manager; - FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); - scoped_ptr<LayerImpl> root = - LayerImpl::Create(host_impl.active_tree(), 12345); - scoped_ptr<HeadsUpDisplayLayerImpl> hud = - HeadsUpDisplayLayerImpl::Create(host_impl.active_tree(), 11111); - - gfx::Transform identity_matrix; - gfx::PointF anchor; - gfx::PointF position; - gfx::Size bounds(100, 100); - SetLayerPropertiesForTesting(root.get(), - identity_matrix, - anchor, - position, - bounds, - true, - false); - root->SetDrawsContent(true); - - // Create hud and add it as a child of root. - gfx::Size hud_bounds(200, 200); - SetLayerPropertiesForTesting(hud.get(), - identity_matrix, - anchor, - position, - hud_bounds, - true, - false); - hud->SetDrawsContent(true); - - host_impl.active_tree()->set_hud_layer(hud.get()); - root->AddChild(hud.PassAs<LayerImpl>()); - - LayerImplList render_surface_layer_list; - LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( - root.get(), hud_bounds, &render_surface_layer_list); - inputs.can_adjust_raster_scales = true; - LayerTreeHostCommon::CalculateDrawProperties(&inputs); - - // Sanity check the scenario we just created. - ASSERT_EQ(1u, render_surface_layer_list.size()); - ASSERT_EQ(2u, root->render_surface()->layer_list().size()); - - // Hit testing for a point inside HUD, but outside root should return null - gfx::Point test_point(101, 101); - LayerImpl* result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - test_point = gfx::Point(-1, -1); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - // Hit testing for a point inside should return the root layer, never the HUD - // layer. - test_point = gfx::Point(1, 1); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(12345, result_layer->id()); - - test_point = gfx::Point(99, 99); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(12345, result_layer->id()); -} - -TEST_F(LayerTreeHostCommonTest, HitTestingForUninvertibleTransform) { - FakeImplProxy proxy; - TestSharedBitmapManager shared_bitmap_manager; - FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); - scoped_ptr<LayerImpl> root = - LayerImpl::Create(host_impl.active_tree(), 12345); - - gfx::Transform uninvertible_transform; - uninvertible_transform.matrix().set(0, 0, 0.0); - uninvertible_transform.matrix().set(1, 1, 0.0); - uninvertible_transform.matrix().set(2, 2, 0.0); - uninvertible_transform.matrix().set(3, 3, 0.0); - ASSERT_FALSE(uninvertible_transform.IsInvertible()); - - gfx::Transform identity_matrix; - gfx::PointF anchor; - gfx::PointF position; - gfx::Size bounds(100, 100); - SetLayerPropertiesForTesting(root.get(), - uninvertible_transform, - anchor, - position, - bounds, - true, - false); - root->SetDrawsContent(true); - - LayerImplList render_surface_layer_list; - LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( - root.get(), root->bounds(), &render_surface_layer_list); - inputs.can_adjust_raster_scales = true; - LayerTreeHostCommon::CalculateDrawProperties(&inputs); - - // Sanity check the scenario we just created. - ASSERT_EQ(1u, render_surface_layer_list.size()); - ASSERT_EQ(1u, root->render_surface()->layer_list().size()); - ASSERT_FALSE(root->screen_space_transform().IsInvertible()); - - // Hit testing any point should not hit the layer. If the invertible matrix is - // accidentally ignored and treated like an identity, then the hit testing - // will incorrectly hit the layer when it shouldn't. - gfx::Point test_point(1, 1); - LayerImpl* result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - test_point = gfx::Point(10, 10); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - test_point = gfx::Point(10, 30); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - test_point = gfx::Point(50, 50); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - test_point = gfx::Point(67, 48); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - test_point = gfx::Point(99, 99); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - test_point = gfx::Point(-1, -1); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); -} - -TEST_F(LayerTreeHostCommonTest, HitTestingForSinglePositionedLayer) { - FakeImplProxy proxy; - TestSharedBitmapManager shared_bitmap_manager; - FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); - scoped_ptr<LayerImpl> root = - LayerImpl::Create(host_impl.active_tree(), 12345); - - gfx::Transform identity_matrix; - gfx::PointF anchor; - // this layer is positioned, and hit testing should correctly know where the - // layer is located. - gfx::PointF position(50.f, 50.f); - gfx::Size bounds(100, 100); - SetLayerPropertiesForTesting(root.get(), - identity_matrix, - anchor, - position, - bounds, - true, - false); - root->SetDrawsContent(true); - - LayerImplList render_surface_layer_list; - LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( - root.get(), root->bounds(), &render_surface_layer_list); - inputs.can_adjust_raster_scales = true; - LayerTreeHostCommon::CalculateDrawProperties(&inputs); - - // Sanity check the scenario we just created. - ASSERT_EQ(1u, render_surface_layer_list.size()); - ASSERT_EQ(1u, root->render_surface()->layer_list().size()); - - // Hit testing for a point outside the layer should return a null pointer. - gfx::Point test_point(49, 49); - LayerImpl* result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - // Even though the layer exists at (101, 101), it should not be visible there - // since the root render surface would clamp it. - test_point = gfx::Point(101, 101); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - // Hit testing for a point inside should return the root layer. - test_point = gfx::Point(51, 51); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(12345, result_layer->id()); - - test_point = gfx::Point(99, 99); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(12345, result_layer->id()); -} - -TEST_F(LayerTreeHostCommonTest, HitTestingForSingleRotatedLayer) { - FakeImplProxy proxy; - TestSharedBitmapManager shared_bitmap_manager; - FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); - scoped_ptr<LayerImpl> root = - LayerImpl::Create(host_impl.active_tree(), 12345); - - gfx::Transform identity_matrix; - gfx::Transform rotation45_degrees_about_center; - rotation45_degrees_about_center.Translate(50.0, 50.0); - rotation45_degrees_about_center.RotateAboutZAxis(45.0); - rotation45_degrees_about_center.Translate(-50.0, -50.0); - gfx::PointF anchor; - gfx::PointF position; - gfx::Size bounds(100, 100); - SetLayerPropertiesForTesting(root.get(), - rotation45_degrees_about_center, - anchor, - position, - bounds, - true, - false); - root->SetDrawsContent(true); - - LayerImplList render_surface_layer_list; - LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( - root.get(), root->bounds(), &render_surface_layer_list); - inputs.can_adjust_raster_scales = true; - LayerTreeHostCommon::CalculateDrawProperties(&inputs); - - // Sanity check the scenario we just created. - ASSERT_EQ(1u, render_surface_layer_list.size()); - ASSERT_EQ(1u, root->render_surface()->layer_list().size()); - - // Hit testing for points outside the layer. - // These corners would have been inside the un-transformed layer, but they - // should not hit the correctly transformed layer. - gfx::Point test_point(99, 99); - LayerImpl* result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - test_point = gfx::Point(1, 1); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - // Hit testing for a point inside should return the root layer. - test_point = gfx::Point(1, 50); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(12345, result_layer->id()); - - // Hit testing the corners that would overlap the unclipped layer, but are - // outside the clipped region. - test_point = gfx::Point(50, -1); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - ASSERT_FALSE(result_layer); - - test_point = gfx::Point(-1, 50); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - ASSERT_FALSE(result_layer); -} - -TEST_F(LayerTreeHostCommonTest, HitTestingForSinglePerspectiveLayer) { - FakeImplProxy proxy; - TestSharedBitmapManager shared_bitmap_manager; - FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); - scoped_ptr<LayerImpl> root = - LayerImpl::Create(host_impl.active_tree(), 12345); - - gfx::Transform identity_matrix; - - // perspective_projection_about_center * translation_by_z is designed so that - // the 100 x 100 layer becomes 50 x 50, and remains centered at (50, 50). - gfx::Transform perspective_projection_about_center; - perspective_projection_about_center.Translate(50.0, 50.0); - perspective_projection_about_center.ApplyPerspectiveDepth(1.0); - perspective_projection_about_center.Translate(-50.0, -50.0); - gfx::Transform translation_by_z; - translation_by_z.Translate3d(0.0, 0.0, -1.0); - - gfx::PointF anchor; - gfx::PointF position; - gfx::Size bounds(100, 100); - SetLayerPropertiesForTesting( - root.get(), - perspective_projection_about_center * translation_by_z, - anchor, - position, - bounds, - true, - false); - root->SetDrawsContent(true); - - LayerImplList render_surface_layer_list; - LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( - root.get(), root->bounds(), &render_surface_layer_list); - inputs.can_adjust_raster_scales = true; - LayerTreeHostCommon::CalculateDrawProperties(&inputs); - - // Sanity check the scenario we just created. - ASSERT_EQ(1u, render_surface_layer_list.size()); - ASSERT_EQ(1u, root->render_surface()->layer_list().size()); - - // Hit testing for points outside the layer. - // These corners would have been inside the un-transformed layer, but they - // should not hit the correctly transformed layer. - gfx::Point test_point(24, 24); - LayerImpl* result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - test_point = gfx::Point(76, 76); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - // Hit testing for a point inside should return the root layer. - test_point = gfx::Point(26, 26); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(12345, result_layer->id()); - - test_point = gfx::Point(74, 74); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(12345, result_layer->id()); -} - -TEST_F(LayerTreeHostCommonTest, HitTestingForSingleLayerWithScaledContents) { - // A layer's visible content rect is actually in the layer's content space. - // The screen space transform converts from the layer's origin space to screen - // space. This test makes sure that hit testing works correctly accounts for - // the contents scale. A contents scale that is not 1 effectively forces a - // non-identity transform between layer's content space and layer's origin - // space. The hit testing code must take this into account. - // - // To test this, the layer is positioned at (25, 25), and is size (50, 50). If - // contents scale is ignored, then hit testing will mis-interpret the visible - // content rect as being larger than the actual bounds of the layer. - // - FakeImplProxy proxy; - TestSharedBitmapManager shared_bitmap_manager; - FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); - scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1); - - gfx::Transform identity_matrix; - gfx::PointF anchor; - - SetLayerPropertiesForTesting(root.get(), - identity_matrix, - anchor, - gfx::PointF(), - gfx::Size(100, 100), - true, - false); - { - gfx::PointF position(25.f, 25.f); - gfx::Size bounds(50, 50); - scoped_ptr<LayerImpl> test_layer = - LayerImpl::Create(host_impl.active_tree(), 12345); - SetLayerPropertiesForTesting(test_layer.get(), - identity_matrix, - anchor, - position, - bounds, - true, - false); - - // override content bounds and contents scale - test_layer->SetContentBounds(gfx::Size(100, 100)); - test_layer->SetContentsScale(2, 2); - - test_layer->SetDrawsContent(true); - root->AddChild(test_layer.Pass()); - } - - LayerImplList render_surface_layer_list; - LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( - root.get(), root->bounds(), &render_surface_layer_list); - inputs.can_adjust_raster_scales = true; - LayerTreeHostCommon::CalculateDrawProperties(&inputs); - - // Sanity check the scenario we just created. - // The visible content rect for test_layer is actually 100x100, even though - // its layout size is 50x50, positioned at 25x25. - LayerImpl* test_layer = root->children()[0]; - EXPECT_RECT_EQ(gfx::Rect(0, 0, 100, 100), - test_layer->visible_content_rect()); - ASSERT_EQ(1u, render_surface_layer_list.size()); - ASSERT_EQ(1u, root->render_surface()->layer_list().size()); - - // Hit testing for a point outside the layer should return a null pointer (the - // root layer does not draw content, so it will not be hit tested either). - gfx::Point test_point(101, 101); - LayerImpl* result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - test_point = gfx::Point(24, 24); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - test_point = gfx::Point(76, 76); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - // Hit testing for a point inside should return the test layer. - test_point = gfx::Point(26, 26); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(12345, result_layer->id()); - - test_point = gfx::Point(74, 74); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(12345, result_layer->id()); -} - -TEST_F(LayerTreeHostCommonTest, HitTestingForSimpleClippedLayer) { - // Test that hit-testing will only work for the visible portion of a layer, - // and not the entire layer bounds. Here we just test the simple axis-aligned - // case. - gfx::Transform identity_matrix; - gfx::PointF anchor; - - FakeImplProxy proxy; - TestSharedBitmapManager shared_bitmap_manager; - FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); - scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1); - SetLayerPropertiesForTesting(root.get(), - identity_matrix, - anchor, - gfx::PointF(), - gfx::Size(100, 100), - true, - false); - { - scoped_ptr<LayerImpl> clipping_layer = - LayerImpl::Create(host_impl.active_tree(), 123); - // this layer is positioned, and hit testing should correctly know where the - // layer is located. - gfx::PointF position(25.f, 25.f); - gfx::Size bounds(50, 50); - SetLayerPropertiesForTesting(clipping_layer.get(), - identity_matrix, - anchor, - position, - bounds, - true, - false); - clipping_layer->SetMasksToBounds(true); - - scoped_ptr<LayerImpl> child = - LayerImpl::Create(host_impl.active_tree(), 456); - position = gfx::PointF(-50.f, -50.f); - bounds = gfx::Size(300, 300); - SetLayerPropertiesForTesting(child.get(), - identity_matrix, - anchor, - position, - bounds, - true, - false); - child->SetDrawsContent(true); - clipping_layer->AddChild(child.Pass()); - root->AddChild(clipping_layer.Pass()); - } - - LayerImplList render_surface_layer_list; - LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( - root.get(), root->bounds(), &render_surface_layer_list); - inputs.can_adjust_raster_scales = true; - LayerTreeHostCommon::CalculateDrawProperties(&inputs); - - // Sanity check the scenario we just created. - ASSERT_EQ(1u, render_surface_layer_list.size()); - ASSERT_EQ(1u, root->render_surface()->layer_list().size()); - ASSERT_EQ(456, root->render_surface()->layer_list().at(0)->id()); - - // Hit testing for a point outside the layer should return a null pointer. - // Despite the child layer being very large, it should be clipped to the root - // layer's bounds. - gfx::Point test_point(24, 24); - LayerImpl* result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - // Even though the layer exists at (101, 101), it should not be visible there - // since the clipping_layer would clamp it. - test_point = gfx::Point(76, 76); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - // Hit testing for a point inside should return the child layer. - test_point = gfx::Point(26, 26); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(456, result_layer->id()); - - test_point = gfx::Point(74, 74); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(456, result_layer->id()); -} - -TEST_F(LayerTreeHostCommonTest, HitTestingForMultiClippedRotatedLayer) { - // This test checks whether hit testing correctly avoids hit testing with - // multiple ancestors that clip in non axis-aligned ways. To pass this test, - // the hit testing algorithm needs to recognize that multiple parent layers - // may clip the layer, and should not actually hit those clipped areas. - // - // The child and grand_child layers are both initialized to clip the - // rotated_leaf. The child layer is rotated about the top-left corner, so that - // the root + child clips combined create a triangle. The rotated_leaf will - // only be visible where it overlaps this triangle. - // - FakeImplProxy proxy; - TestSharedBitmapManager shared_bitmap_manager; - FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); - scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 123); - - gfx::Transform identity_matrix; - gfx::PointF anchor; - gfx::PointF position; - gfx::Size bounds(100, 100); - SetLayerPropertiesForTesting(root.get(), - identity_matrix, - anchor, - position, - bounds, - true, - false); - root->SetMasksToBounds(true); - { - scoped_ptr<LayerImpl> child = - LayerImpl::Create(host_impl.active_tree(), 456); - scoped_ptr<LayerImpl> grand_child = - LayerImpl::Create(host_impl.active_tree(), 789); - scoped_ptr<LayerImpl> rotated_leaf = - LayerImpl::Create(host_impl.active_tree(), 2468); - - position = gfx::PointF(10.f, 10.f); - bounds = gfx::Size(80, 80); - SetLayerPropertiesForTesting(child.get(), - identity_matrix, - anchor, - position, - bounds, - true, - false); - child->SetMasksToBounds(true); - - gfx::Transform rotation45_degrees_about_corner; - rotation45_degrees_about_corner.RotateAboutZAxis(45.0); - - // remember, positioned with respect to its parent which is already at 10, - // 10 - position = gfx::PointF(); - bounds = - gfx::Size(200, 200); // to ensure it covers at least sqrt(2) * 100. - SetLayerPropertiesForTesting(grand_child.get(), - rotation45_degrees_about_corner, - anchor, - position, - bounds, - true, - false); - grand_child->SetMasksToBounds(true); - - // Rotates about the center of the layer - gfx::Transform rotated_leaf_transform; - rotated_leaf_transform.Translate( - -10.0, -10.0); // cancel out the grand_parent's position - rotated_leaf_transform.RotateAboutZAxis( - -45.0); // cancel out the corner 45-degree rotation of the parent. - rotated_leaf_transform.Translate(50.0, 50.0); - rotated_leaf_transform.RotateAboutZAxis(45.0); - rotated_leaf_transform.Translate(-50.0, -50.0); - position = gfx::PointF(); - bounds = gfx::Size(100, 100); - SetLayerPropertiesForTesting(rotated_leaf.get(), - rotated_leaf_transform, - anchor, - position, - bounds, - true, - false); - rotated_leaf->SetDrawsContent(true); - - grand_child->AddChild(rotated_leaf.Pass()); - child->AddChild(grand_child.Pass()); - root->AddChild(child.Pass()); - } - - LayerImplList render_surface_layer_list; - LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( - root.get(), root->bounds(), &render_surface_layer_list); - inputs.can_adjust_raster_scales = true; - LayerTreeHostCommon::CalculateDrawProperties(&inputs); - - // Sanity check the scenario we just created. - // The grand_child is expected to create a render surface because it - // MasksToBounds and is not axis aligned. - ASSERT_EQ(2u, render_surface_layer_list.size()); - ASSERT_EQ( - 1u, - render_surface_layer_list.at(0)->render_surface()->layer_list().size()); - ASSERT_EQ(789, - render_surface_layer_list.at(0)->render_surface()->layer_list().at( - 0)->id()); // grand_child's surface. - ASSERT_EQ( - 1u, - render_surface_layer_list.at(1)->render_surface()->layer_list().size()); - ASSERT_EQ( - 2468, - render_surface_layer_list[1]->render_surface()->layer_list().at(0)->id()); - - // (11, 89) is close to the the bottom left corner within the clip, but it is - // not inside the layer. - gfx::Point test_point(11, 89); - LayerImpl* result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - // Closer inwards from the bottom left will overlap the layer. - test_point = gfx::Point(25, 75); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(2468, result_layer->id()); - - // (4, 50) is inside the unclipped layer, but that corner of the layer should - // be clipped away by the grandparent and should not get hit. If hit testing - // blindly uses visible content rect without considering how parent may clip - // the layer, then hit testing would accidentally think that the point - // successfully hits the layer. - test_point = gfx::Point(4, 50); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - // (11, 50) is inside the layer and within the clipped area. - test_point = gfx::Point(11, 50); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(2468, result_layer->id()); - - // Around the middle, just to the right and up, would have hit the layer - // except that that area should be clipped away by the parent. - test_point = gfx::Point(51, 49); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - // Around the middle, just to the left and down, should successfully hit the - // layer. - test_point = gfx::Point(49, 51); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(2468, result_layer->id()); -} - -TEST_F(LayerTreeHostCommonTest, HitTestingForNonClippingIntermediateLayer) { - // This test checks that hit testing code does not accidentally clip to layer - // bounds for a layer that actually does not clip. - gfx::Transform identity_matrix; - gfx::PointF anchor; - - FakeImplProxy proxy; - TestSharedBitmapManager shared_bitmap_manager; - FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); - scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1); - SetLayerPropertiesForTesting(root.get(), - identity_matrix, - anchor, - gfx::PointF(), - gfx::Size(100, 100), - true, - false); - { - scoped_ptr<LayerImpl> intermediate_layer = - LayerImpl::Create(host_impl.active_tree(), 123); - // this layer is positioned, and hit testing should correctly know where the - // layer is located. - gfx::PointF position(10.f, 10.f); - gfx::Size bounds(50, 50); - SetLayerPropertiesForTesting(intermediate_layer.get(), - identity_matrix, - anchor, - position, - bounds, - true, - false); - // Sanity check the intermediate layer should not clip. - ASSERT_FALSE(intermediate_layer->masks_to_bounds()); - ASSERT_FALSE(intermediate_layer->mask_layer()); - - // The child of the intermediate_layer is translated so that it does not - // overlap intermediate_layer at all. If child is incorrectly clipped, we - // would not be able to hit it successfully. - scoped_ptr<LayerImpl> child = - LayerImpl::Create(host_impl.active_tree(), 456); - position = gfx::PointF(60.f, 60.f); // 70, 70 in screen space - bounds = gfx::Size(20, 20); - SetLayerPropertiesForTesting(child.get(), - identity_matrix, - anchor, - position, - bounds, - true, - false); - child->SetDrawsContent(true); - intermediate_layer->AddChild(child.Pass()); - root->AddChild(intermediate_layer.Pass()); - } - - LayerImplList render_surface_layer_list; - LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( - root.get(), root->bounds(), &render_surface_layer_list); - inputs.can_adjust_raster_scales = true; - LayerTreeHostCommon::CalculateDrawProperties(&inputs); - - // Sanity check the scenario we just created. - ASSERT_EQ(1u, render_surface_layer_list.size()); - ASSERT_EQ(1u, root->render_surface()->layer_list().size()); - ASSERT_EQ(456, root->render_surface()->layer_list().at(0)->id()); - - // Hit testing for a point outside the layer should return a null pointer. - gfx::Point test_point(69, 69); - LayerImpl* result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - test_point = gfx::Point(91, 91); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - // Hit testing for a point inside should return the child layer. - test_point = gfx::Point(71, 71); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(456, result_layer->id()); - - test_point = gfx::Point(89, 89); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(456, result_layer->id()); -} - -TEST_F(LayerTreeHostCommonTest, HitTestingForMultipleLayers) { - FakeImplProxy proxy; - TestSharedBitmapManager shared_bitmap_manager; - FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); - scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1); - - gfx::Transform identity_matrix; - gfx::PointF anchor; - gfx::PointF position; - gfx::Size bounds(100, 100); - SetLayerPropertiesForTesting(root.get(), - identity_matrix, - anchor, - position, - bounds, - true, - false); - root->SetDrawsContent(true); - { - // child 1 and child2 are initialized to overlap between x=50 and x=60. - // grand_child is set to overlap both child1 and child2 between y=50 and - // y=60. The expected stacking order is: (front) child2, (second) - // grand_child, (third) child1, and (back) the root layer behind all other - // layers. - - scoped_ptr<LayerImpl> child1 = - LayerImpl::Create(host_impl.active_tree(), 2); - scoped_ptr<LayerImpl> child2 = - LayerImpl::Create(host_impl.active_tree(), 3); - scoped_ptr<LayerImpl> grand_child1 = - LayerImpl::Create(host_impl.active_tree(), 4); - - position = gfx::PointF(10.f, 10.f); - bounds = gfx::Size(50, 50); - SetLayerPropertiesForTesting(child1.get(), - identity_matrix, - anchor, - position, - bounds, - true, - false); - child1->SetDrawsContent(true); - - position = gfx::PointF(50.f, 10.f); - bounds = gfx::Size(50, 50); - SetLayerPropertiesForTesting(child2.get(), - identity_matrix, - anchor, - position, - bounds, - true, - false); - child2->SetDrawsContent(true); - - // Remember that grand_child is positioned with respect to its parent (i.e. - // child1). In screen space, the intended position is (10, 50), with size - // 100 x 50. - position = gfx::PointF(0.f, 40.f); - bounds = gfx::Size(100, 50); - SetLayerPropertiesForTesting(grand_child1.get(), - identity_matrix, - anchor, - position, - bounds, - true, - false); - grand_child1->SetDrawsContent(true); - - child1->AddChild(grand_child1.Pass()); - root->AddChild(child1.Pass()); - root->AddChild(child2.Pass()); - } - - LayerImpl* child1 = root->children()[0]; - LayerImpl* child2 = root->children()[1]; - LayerImpl* grand_child1 = child1->children()[0]; - - LayerImplList render_surface_layer_list; - LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( - root.get(), root->bounds(), &render_surface_layer_list); - inputs.can_adjust_raster_scales = true; - LayerTreeHostCommon::CalculateDrawProperties(&inputs); - - // Sanity check the scenario we just created. - ASSERT_TRUE(child1); - ASSERT_TRUE(child2); - ASSERT_TRUE(grand_child1); - ASSERT_EQ(1u, render_surface_layer_list.size()); - - RenderSurfaceImpl* root_render_surface = root->render_surface(); - ASSERT_EQ(4u, root_render_surface->layer_list().size()); - ASSERT_EQ(1, root_render_surface->layer_list().at(0)->id()); // root layer - ASSERT_EQ(2, root_render_surface->layer_list().at(1)->id()); // child1 - ASSERT_EQ(4, root_render_surface->layer_list().at(2)->id()); // grand_child1 - ASSERT_EQ(3, root_render_surface->layer_list().at(3)->id()); // child2 - - // Nothing overlaps the root_layer at (1, 1), so hit testing there should find - // the root layer. - gfx::Point test_point = gfx::Point(1, 1); - LayerImpl* result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(1, result_layer->id()); - - // At (15, 15), child1 and root are the only layers. child1 is expected to be - // on top. - test_point = gfx::Point(15, 15); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(2, result_layer->id()); - - // At (51, 20), child1 and child2 overlap. child2 is expected to be on top. - test_point = gfx::Point(51, 20); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(3, result_layer->id()); - - // At (80, 51), child2 and grand_child1 overlap. child2 is expected to be on - // top. - test_point = gfx::Point(80, 51); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(3, result_layer->id()); - - // At (51, 51), all layers overlap each other. child2 is expected to be on top - // of all other layers. - test_point = gfx::Point(51, 51); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(3, result_layer->id()); - - // At (20, 51), child1 and grand_child1 overlap. grand_child1 is expected to - // be on top. - test_point = gfx::Point(20, 51); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(4, result_layer->id()); -} - -TEST_F(LayerTreeHostCommonTest, HitTestingForMultipleLayerLists) { - // - // The geometry is set up similarly to the previous case, but - // all layers are forced to be render surfaces now. - // - FakeImplProxy proxy; - TestSharedBitmapManager shared_bitmap_manager; - FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); - scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1); - - gfx::Transform identity_matrix; - gfx::PointF anchor; - gfx::PointF position; - gfx::Size bounds(100, 100); - SetLayerPropertiesForTesting(root.get(), - identity_matrix, - anchor, - position, - bounds, - true, - false); - root->SetDrawsContent(true); - { - // child 1 and child2 are initialized to overlap between x=50 and x=60. - // grand_child is set to overlap both child1 and child2 between y=50 and - // y=60. The expected stacking order is: (front) child2, (second) - // grand_child, (third) child1, and (back) the root layer behind all other - // layers. - - scoped_ptr<LayerImpl> child1 = - LayerImpl::Create(host_impl.active_tree(), 2); - scoped_ptr<LayerImpl> child2 = - LayerImpl::Create(host_impl.active_tree(), 3); - scoped_ptr<LayerImpl> grand_child1 = - LayerImpl::Create(host_impl.active_tree(), 4); - - position = gfx::PointF(10.f, 10.f); - bounds = gfx::Size(50, 50); - SetLayerPropertiesForTesting(child1.get(), - identity_matrix, - anchor, - position, - bounds, - true, - false); - child1->SetDrawsContent(true); - child1->SetForceRenderSurface(true); - - position = gfx::PointF(50.f, 10.f); - bounds = gfx::Size(50, 50); - SetLayerPropertiesForTesting(child2.get(), - identity_matrix, - anchor, - position, - bounds, - true, - false); - child2->SetDrawsContent(true); - child2->SetForceRenderSurface(true); - - // Remember that grand_child is positioned with respect to its parent (i.e. - // child1). In screen space, the intended position is (10, 50), with size - // 100 x 50. - position = gfx::PointF(0.f, 40.f); - bounds = gfx::Size(100, 50); - SetLayerPropertiesForTesting(grand_child1.get(), - identity_matrix, - anchor, - position, - bounds, - true, - false); - grand_child1->SetDrawsContent(true); - grand_child1->SetForceRenderSurface(true); - - child1->AddChild(grand_child1.Pass()); - root->AddChild(child1.Pass()); - root->AddChild(child2.Pass()); - } - - LayerImpl* child1 = root->children()[0]; - LayerImpl* child2 = root->children()[1]; - LayerImpl* grand_child1 = child1->children()[0]; - - LayerImplList render_surface_layer_list; - LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( - root.get(), root->bounds(), &render_surface_layer_list); - inputs.can_adjust_raster_scales = true; - LayerTreeHostCommon::CalculateDrawProperties(&inputs); - - // Sanity check the scenario we just created. - ASSERT_TRUE(child1); - ASSERT_TRUE(child2); - ASSERT_TRUE(grand_child1); - ASSERT_TRUE(child1->render_surface()); - ASSERT_TRUE(child2->render_surface()); - ASSERT_TRUE(grand_child1->render_surface()); - ASSERT_EQ(4u, render_surface_layer_list.size()); - // The root surface has the root layer, and child1's and child2's render - // surfaces. - ASSERT_EQ(3u, root->render_surface()->layer_list().size()); - // The child1 surface has the child1 layer and grand_child1's render surface. - ASSERT_EQ(2u, child1->render_surface()->layer_list().size()); - ASSERT_EQ(1u, child2->render_surface()->layer_list().size()); - ASSERT_EQ(1u, grand_child1->render_surface()->layer_list().size()); - ASSERT_EQ(1, render_surface_layer_list.at(0)->id()); // root layer - ASSERT_EQ(2, render_surface_layer_list[1]->id()); // child1 - ASSERT_EQ(4, render_surface_layer_list.at(2)->id()); // grand_child1 - ASSERT_EQ(3, render_surface_layer_list[3]->id()); // child2 - - // Nothing overlaps the root_layer at (1, 1), so hit testing there should find - // the root layer. - gfx::Point test_point = gfx::Point(1, 1); - LayerImpl* result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(1, result_layer->id()); - - // At (15, 15), child1 and root are the only layers. child1 is expected to be - // on top. - test_point = gfx::Point(15, 15); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(2, result_layer->id()); - - // At (51, 20), child1 and child2 overlap. child2 is expected to be on top. - test_point = gfx::Point(51, 20); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(3, result_layer->id()); - - // At (80, 51), child2 and grand_child1 overlap. child2 is expected to be on - // top. - test_point = gfx::Point(80, 51); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(3, result_layer->id()); - - // At (51, 51), all layers overlap each other. child2 is expected to be on top - // of all other layers. - test_point = gfx::Point(51, 51); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(3, result_layer->id()); - - // At (20, 51), child1 and grand_child1 overlap. grand_child1 is expected to - // be on top. - test_point = gfx::Point(20, 51); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(4, result_layer->id()); -} - -TEST_F(LayerTreeHostCommonTest, HitTestingForEmptyLayers) { - FakeImplProxy proxy; - TestSharedBitmapManager shared_bitmap_manager; - FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); - - // Layer 1 - root - scoped_ptr<LayerImpl> root = - LayerImpl::Create(host_impl.active_tree(), 1); - gfx::Transform identity_matrix; - gfx::PointF anchor; - gfx::PointF position; - gfx::Size bounds(100, 100); - SetLayerPropertiesForTesting(root.get(), - identity_matrix, - anchor, - position, - bounds, - true, - false); - root->SetDrawsContent(true); - - { - // Layer 2 - empty: drawsContent=false - gfx::PointF position(10.f, 10.f); - gfx::Size bounds(30, 30); - scoped_ptr<LayerImpl> empty_layer = - LayerImpl::Create(host_impl.active_tree(), 2); - SetLayerPropertiesForTesting(empty_layer.get(), - identity_matrix, - anchor, - position, - bounds, - true, - false); - - empty_layer->SetDrawsContent(false); - root->AddChild(empty_layer.Pass()); - } - - { - // Layer 3 - empty, but has touch handler - gfx::PointF position(10.f, 60.f); - gfx::Size bounds(30, 30); - scoped_ptr<LayerImpl> test_layer = - LayerImpl::Create(host_impl.active_tree(), 3); - SetLayerPropertiesForTesting(test_layer.get(), - identity_matrix, - anchor, - position, - bounds, - true, - false); - - test_layer->SetDrawsContent(false); - Region touch_handler_region(gfx::Rect(10, 10, 10, 10)); - test_layer->SetTouchEventHandlerRegion(touch_handler_region); - root->AddChild(test_layer.Pass()); - } - - { - // Layer 4 - empty, but has mousewheel handler - gfx::PointF position(60.f, 60.f); - gfx::Size bounds(30, 30); - scoped_ptr<LayerImpl> test_layer = - LayerImpl::Create(host_impl.active_tree(), 4); - SetLayerPropertiesForTesting(test_layer.get(), - identity_matrix, - anchor, - position, - bounds, - true, - false); - - test_layer->SetDrawsContent(false); - test_layer->SetHaveWheelEventHandlers(true); - root->AddChild(test_layer.Pass()); - } - - LayerImplList render_surface_layer_list; - LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( - root.get(), root->bounds(), &render_surface_layer_list); - inputs.can_adjust_raster_scales = true; - LayerTreeHostCommon::CalculateDrawProperties(&inputs); - - // Verify that the root layer and empty layers with touch/wheel handlers - // (but not the empty layer without a touch handler) are in the RSSL. - ASSERT_EQ(1u, render_surface_layer_list.size()); - EXPECT_EQ(1, render_surface_layer_list[0]->id()); - ASSERT_EQ(3u, root->render_surface()->layer_list().size()); - EXPECT_EQ(1, root->render_surface()->layer_list().at(0)->id()); - EXPECT_EQ(3, root->render_surface()->layer_list().at(1)->id()); - EXPECT_EQ(4, root->render_surface()->layer_list().at(2)->id()); - - // Hit testing for a point inside the empty no-handlers layer should return - // the root layer. - gfx::Point test_point = gfx::Point(15, 15); - LayerImpl* result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(1, result_layer->id()); - - // Hit testing for a point inside the touch handler layer should return it. - test_point = gfx::Point(15, 75); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(3, result_layer->id()); - - // Hit testing for a point inside the mousewheel layer should return it. - test_point = gfx::Point(75, 75); - result_layer = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(4, result_layer->id()); -} - -TEST_F(LayerTreeHostCommonTest, - HitCheckingTouchHandlerRegionsForEmptyLayerList) { - // Hit checking on an empty render_surface_layer_list should return a null - // pointer. - LayerImplList render_surface_layer_list; - - gfx::Point test_point(0, 0); - LayerImpl* result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - test_point = gfx::Point(10, 20); - result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); -} - -TEST_F(LayerTreeHostCommonTest, HitCheckingTouchHandlerRegionsForSingleLayer) { - FakeImplProxy proxy; - TestSharedBitmapManager shared_bitmap_manager; - FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); - scoped_ptr<LayerImpl> root = - LayerImpl::Create(host_impl.active_tree(), 12345); - - gfx::Transform identity_matrix; - Region touch_handler_region(gfx::Rect(10, 10, 50, 50)); - gfx::PointF anchor; - gfx::PointF position; - gfx::Size bounds(100, 100); - SetLayerPropertiesForTesting(root.get(), - identity_matrix, - anchor, - position, - bounds, - true, - false); - root->SetDrawsContent(true); - - LayerImplList render_surface_layer_list; - LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( - root.get(), root->bounds(), &render_surface_layer_list); - inputs.can_adjust_raster_scales = true; - LayerTreeHostCommon::CalculateDrawProperties(&inputs); - - // Sanity check the scenario we just created. - ASSERT_EQ(1u, render_surface_layer_list.size()); - ASSERT_EQ(1u, root->render_surface()->layer_list().size()); - - // Hit checking for any point should return a null pointer for a layer without - // any touch event handler regions. - gfx::Point test_point(11, 11); - LayerImpl* result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - root->SetTouchEventHandlerRegion(touch_handler_region); - // Hit checking for a point outside the layer should return a null pointer. - test_point = gfx::Point(101, 101); - result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - test_point = gfx::Point(-1, -1); - result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - // Hit checking for a point inside the layer, but outside the touch handler - // region should return a null pointer. - test_point = gfx::Point(1, 1); - result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - test_point = gfx::Point(99, 99); - result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - // Hit checking for a point inside the touch event handler region should - // return the root layer. - test_point = gfx::Point(11, 11); - result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(12345, result_layer->id()); - - test_point = gfx::Point(59, 59); - result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(12345, result_layer->id()); -} - -TEST_F(LayerTreeHostCommonTest, - HitCheckingTouchHandlerRegionsForUninvertibleTransform) { - FakeImplProxy proxy; - TestSharedBitmapManager shared_bitmap_manager; - FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); - scoped_ptr<LayerImpl> root = - LayerImpl::Create(host_impl.active_tree(), 12345); - - gfx::Transform uninvertible_transform; - uninvertible_transform.matrix().set(0, 0, 0.0); - uninvertible_transform.matrix().set(1, 1, 0.0); - uninvertible_transform.matrix().set(2, 2, 0.0); - uninvertible_transform.matrix().set(3, 3, 0.0); - ASSERT_FALSE(uninvertible_transform.IsInvertible()); - - gfx::Transform identity_matrix; - Region touch_handler_region(gfx::Rect(10, 10, 50, 50)); - gfx::PointF anchor; - gfx::PointF position; - gfx::Size bounds(100, 100); - SetLayerPropertiesForTesting(root.get(), - uninvertible_transform, - anchor, - position, - bounds, - true, - false); - root->SetDrawsContent(true); - root->SetTouchEventHandlerRegion(touch_handler_region); - - LayerImplList render_surface_layer_list; - LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( - root.get(), root->bounds(), &render_surface_layer_list); - inputs.can_adjust_raster_scales = true; - LayerTreeHostCommon::CalculateDrawProperties(&inputs); - - // Sanity check the scenario we just created. - ASSERT_EQ(1u, render_surface_layer_list.size()); - ASSERT_EQ(1u, root->render_surface()->layer_list().size()); - ASSERT_FALSE(root->screen_space_transform().IsInvertible()); - - // Hit checking any point should not hit the touch handler region on the - // layer. If the invertible matrix is accidentally ignored and treated like an - // identity, then the hit testing will incorrectly hit the layer when it - // shouldn't. - gfx::Point test_point(1, 1); - LayerImpl* result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - test_point = gfx::Point(10, 10); - result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - test_point = gfx::Point(10, 30); - result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - test_point = gfx::Point(50, 50); - result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - test_point = gfx::Point(67, 48); - result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - test_point = gfx::Point(99, 99); - result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - test_point = gfx::Point(-1, -1); - result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); -} - -TEST_F(LayerTreeHostCommonTest, - HitCheckingTouchHandlerRegionsForSinglePositionedLayer) { - FakeImplProxy proxy; - TestSharedBitmapManager shared_bitmap_manager; - FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); - scoped_ptr<LayerImpl> root = - LayerImpl::Create(host_impl.active_tree(), 12345); - - gfx::Transform identity_matrix; - Region touch_handler_region(gfx::Rect(10, 10, 50, 50)); - gfx::PointF anchor; - // this layer is positioned, and hit testing should correctly know where the - // layer is located. - gfx::PointF position(50.f, 50.f); - gfx::Size bounds(100, 100); - SetLayerPropertiesForTesting(root.get(), - identity_matrix, - anchor, - position, - bounds, - true, - false); - root->SetDrawsContent(true); - root->SetTouchEventHandlerRegion(touch_handler_region); - - LayerImplList render_surface_layer_list; - LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( - root.get(), root->bounds(), &render_surface_layer_list); - inputs.can_adjust_raster_scales = true; - LayerTreeHostCommon::CalculateDrawProperties(&inputs); - - // Sanity check the scenario we just created. - ASSERT_EQ(1u, render_surface_layer_list.size()); - ASSERT_EQ(1u, root->render_surface()->layer_list().size()); - - // Hit checking for a point outside the layer should return a null pointer. - gfx::Point test_point(49, 49); - LayerImpl* result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - // Even though the layer has a touch handler region containing (101, 101), it - // should not be visible there since the root render surface would clamp it. - test_point = gfx::Point(101, 101); - result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - // Hit checking for a point inside the layer, but outside the touch handler - // region should return a null pointer. - test_point = gfx::Point(51, 51); - result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - // Hit checking for a point inside the touch event handler region should - // return the root layer. - test_point = gfx::Point(61, 61); - result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(12345, result_layer->id()); - - test_point = gfx::Point(99, 99); - result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(12345, result_layer->id()); -} - -TEST_F(LayerTreeHostCommonTest, - HitCheckingTouchHandlerRegionsForSingleLayerWithScaledContents) { - // A layer's visible content rect is actually in the layer's content space. - // The screen space transform converts from the layer's origin space to screen - // space. This test makes sure that hit testing works correctly accounts for - // the contents scale. A contents scale that is not 1 effectively forces a - // non-identity transform between layer's content space and layer's origin - // space. The hit testing code must take this into account. - // - // To test this, the layer is positioned at (25, 25), and is size (50, 50). If - // contents scale is ignored, then hit checking will mis-interpret the visible - // content rect as being larger than the actual bounds of the layer. - // - FakeImplProxy proxy; - TestSharedBitmapManager shared_bitmap_manager; - FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); - scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1); - - gfx::Transform identity_matrix; - gfx::PointF anchor; - - SetLayerPropertiesForTesting(root.get(), - identity_matrix, - anchor, - gfx::PointF(), - gfx::Size(100, 100), - true, - false); - { - Region touch_handler_region(gfx::Rect(10, 10, 30, 30)); - gfx::PointF position(25.f, 25.f); - gfx::Size bounds(50, 50); - scoped_ptr<LayerImpl> test_layer = - LayerImpl::Create(host_impl.active_tree(), 12345); - SetLayerPropertiesForTesting(test_layer.get(), - identity_matrix, - anchor, - position, - bounds, - true, - false); - - // override content bounds and contents scale - test_layer->SetContentBounds(gfx::Size(100, 100)); - test_layer->SetContentsScale(2, 2); - - test_layer->SetDrawsContent(true); - test_layer->SetTouchEventHandlerRegion(touch_handler_region); - root->AddChild(test_layer.Pass()); - } - - LayerImplList render_surface_layer_list; - LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( - root.get(), root->bounds(), &render_surface_layer_list); - inputs.can_adjust_raster_scales = true; - LayerTreeHostCommon::CalculateDrawProperties(&inputs); - - // Sanity check the scenario we just created. - // The visible content rect for test_layer is actually 100x100, even though - // its layout size is 50x50, positioned at 25x25. - LayerImpl* test_layer = root->children()[0]; - EXPECT_RECT_EQ(gfx::Rect(0, 0, 100, 100), test_layer->visible_content_rect()); - ASSERT_EQ(1u, render_surface_layer_list.size()); - ASSERT_EQ(1u, root->render_surface()->layer_list().size()); - - // Hit checking for a point outside the layer should return a null pointer - // (the root layer does not draw content, so it will not be tested either). - gfx::Point test_point(76, 76); - LayerImpl* result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - // Hit checking for a point inside the layer, but outside the touch handler - // region should return a null pointer. - test_point = gfx::Point(26, 26); - result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - test_point = gfx::Point(34, 34); - result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - test_point = gfx::Point(65, 65); - result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - test_point = gfx::Point(74, 74); - result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - // Hit checking for a point inside the touch event handler region should - // return the root layer. - test_point = gfx::Point(35, 35); - result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(12345, result_layer->id()); - - test_point = gfx::Point(64, 64); - result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(12345, result_layer->id()); -} - -TEST_F(LayerTreeHostCommonTest, - HitCheckingTouchHandlerRegionsForSingleLayerWithDeviceScale) { - // The layer's device_scale_factor and page_scale_factor should scale the - // content rect and we should be able to hit the touch handler region by - // scaling the points accordingly. - FakeImplProxy proxy; - TestSharedBitmapManager shared_bitmap_manager; - FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); - scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1); - - gfx::Transform identity_matrix; - gfx::PointF anchor; - // Set the bounds of the root layer big enough to fit the child when scaled. - SetLayerPropertiesForTesting(root.get(), - identity_matrix, - anchor, - gfx::PointF(), - gfx::Size(100, 100), - true, - false); - { - Region touch_handler_region(gfx::Rect(10, 10, 30, 30)); - gfx::PointF position(25.f, 25.f); - gfx::Size bounds(50, 50); - scoped_ptr<LayerImpl> test_layer = - LayerImpl::Create(host_impl.active_tree(), 12345); - SetLayerPropertiesForTesting(test_layer.get(), - identity_matrix, - anchor, - position, - bounds, - true, - false); - - test_layer->SetDrawsContent(true); - test_layer->SetTouchEventHandlerRegion(touch_handler_region); - root->AddChild(test_layer.Pass()); - } - - LayerImplList render_surface_layer_list; - float device_scale_factor = 3.f; - float page_scale_factor = 5.f; - gfx::Size scaled_bounds_for_root = gfx::ToCeiledSize( - gfx::ScaleSize(root->bounds(), device_scale_factor * page_scale_factor)); - - LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( - root.get(), scaled_bounds_for_root, &render_surface_layer_list); - inputs.device_scale_factor = device_scale_factor; - inputs.page_scale_factor = page_scale_factor; - inputs.page_scale_application_layer = root.get(); - inputs.can_adjust_raster_scales = true; - LayerTreeHostCommon::CalculateDrawProperties(&inputs); - - // Sanity check the scenario we just created. - // The visible content rect for test_layer is actually 100x100, even though - // its layout size is 50x50, positioned at 25x25. - LayerImpl* test_layer = root->children()[0]; - ASSERT_EQ(1u, render_surface_layer_list.size()); - ASSERT_EQ(1u, root->render_surface()->layer_list().size()); - - // Check whether the child layer fits into the root after scaled. - EXPECT_RECT_EQ(gfx::Rect(test_layer->content_bounds()), - test_layer->visible_content_rect()); - - // Hit checking for a point outside the layer should return a null pointer - // (the root layer does not draw content, so it will not be tested either). - gfx::PointF test_point(76.f, 76.f); - test_point = - gfx::ScalePoint(test_point, device_scale_factor * page_scale_factor); - LayerImpl* result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - // Hit checking for a point inside the layer, but outside the touch handler - // region should return a null pointer. - test_point = gfx::Point(26, 26); - test_point = - gfx::ScalePoint(test_point, device_scale_factor * page_scale_factor); - result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - test_point = gfx::Point(34, 34); - test_point = - gfx::ScalePoint(test_point, device_scale_factor * page_scale_factor); - result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - test_point = gfx::Point(65, 65); - test_point = - gfx::ScalePoint(test_point, device_scale_factor * page_scale_factor); - result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - test_point = gfx::Point(74, 74); - test_point = - gfx::ScalePoint(test_point, device_scale_factor * page_scale_factor); - result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - // Hit checking for a point inside the touch event handler region should - // return the root layer. - test_point = gfx::Point(35, 35); - test_point = - gfx::ScalePoint(test_point, device_scale_factor * page_scale_factor); - result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(12345, result_layer->id()); - - test_point = gfx::Point(64, 64); - test_point = - gfx::ScalePoint(test_point, device_scale_factor * page_scale_factor); - result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(12345, result_layer->id()); -} - -TEST_F(LayerTreeHostCommonTest, - HitCheckingTouchHandlerRegionsForSimpleClippedLayer) { - // Test that hit-checking will only work for the visible portion of a layer, - // and not the entire layer bounds. Here we just test the simple axis-aligned - // case. - gfx::Transform identity_matrix; - gfx::PointF anchor; - - FakeImplProxy proxy; - TestSharedBitmapManager shared_bitmap_manager; - FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); - scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1); - SetLayerPropertiesForTesting(root.get(), - identity_matrix, - anchor, - gfx::PointF(), - gfx::Size(100, 100), - true, - false); - { - scoped_ptr<LayerImpl> clipping_layer = - LayerImpl::Create(host_impl.active_tree(), 123); - // this layer is positioned, and hit testing should correctly know where the - // layer is located. - gfx::PointF position(25.f, 25.f); - gfx::Size bounds(50, 50); - SetLayerPropertiesForTesting(clipping_layer.get(), - identity_matrix, - anchor, - position, - bounds, - true, - false); - clipping_layer->SetMasksToBounds(true); - - scoped_ptr<LayerImpl> child = - LayerImpl::Create(host_impl.active_tree(), 456); - Region touch_handler_region(gfx::Rect(10, 10, 50, 50)); - position = gfx::PointF(-50.f, -50.f); - bounds = gfx::Size(300, 300); - SetLayerPropertiesForTesting(child.get(), - identity_matrix, - anchor, - position, - bounds, - true, - false); - child->SetDrawsContent(true); - child->SetTouchEventHandlerRegion(touch_handler_region); - clipping_layer->AddChild(child.Pass()); - root->AddChild(clipping_layer.Pass()); - } - - LayerImplList render_surface_layer_list; - LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( - root.get(), root->bounds(), &render_surface_layer_list); - inputs.can_adjust_raster_scales = true; - LayerTreeHostCommon::CalculateDrawProperties(&inputs); - - // Sanity check the scenario we just created. - ASSERT_EQ(1u, render_surface_layer_list.size()); - ASSERT_EQ(1u, root->render_surface()->layer_list().size()); - ASSERT_EQ(456, root->render_surface()->layer_list().at(0)->id()); - - // Hit checking for a point outside the layer should return a null pointer. - // Despite the child layer being very large, it should be clipped to the root - // layer's bounds. - gfx::Point test_point(24, 24); - LayerImpl* result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - // Hit checking for a point inside the layer, but outside the touch handler - // region should return a null pointer. - test_point = gfx::Point(35, 35); - result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - test_point = gfx::Point(74, 74); - result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); - - // Hit checking for a point inside the touch event handler region should - // return the root layer. - test_point = gfx::Point(25, 25); - result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(456, result_layer->id()); - - test_point = gfx::Point(34, 34); - result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(456, result_layer->id()); -} - -TEST_F(LayerTreeHostCommonTest, - HitCheckingTouchHandlerOverlappingRegions) { - gfx::Transform identity_matrix; - gfx::PointF anchor; - - FakeImplProxy proxy; - TestSharedBitmapManager shared_bitmap_manager; - FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager); - scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1); - SetLayerPropertiesForTesting(root.get(), - identity_matrix, - anchor, - gfx::PointF(), - gfx::Size(100, 100), - true, - false); - { - scoped_ptr<LayerImpl> touch_layer = - LayerImpl::Create(host_impl.active_tree(), 123); - // this layer is positioned, and hit testing should correctly know where the - // layer is located. - gfx::PointF position; - gfx::Size bounds(50, 50); - SetLayerPropertiesForTesting(touch_layer.get(), - identity_matrix, - anchor, - position, - bounds, - true, - false); - touch_layer->SetDrawsContent(true); - touch_layer->SetTouchEventHandlerRegion(gfx::Rect(0, 0, 50, 50)); - root->AddChild(touch_layer.Pass()); - } - - { - scoped_ptr<LayerImpl> notouch_layer = - LayerImpl::Create(host_impl.active_tree(), 1234); - // this layer is positioned, and hit testing should correctly know where the - // layer is located. - gfx::PointF position(0, 25); - gfx::Size bounds(50, 50); - SetLayerPropertiesForTesting(notouch_layer.get(), - identity_matrix, - anchor, - position, - bounds, - true, - false); - notouch_layer->SetDrawsContent(true); - root->AddChild(notouch_layer.Pass()); - } - - LayerImplList render_surface_layer_list; - LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs( - root.get(), root->bounds(), &render_surface_layer_list); - inputs.can_adjust_raster_scales = true; - LayerTreeHostCommon::CalculateDrawProperties(&inputs); - - // Sanity check the scenario we just created. - ASSERT_EQ(1u, render_surface_layer_list.size()); - ASSERT_EQ(2u, root->render_surface()->layer_list().size()); - ASSERT_EQ(123, root->render_surface()->layer_list().at(0)->id()); - ASSERT_EQ(1234, root->render_surface()->layer_list().at(1)->id()); - - gfx::Point test_point(35, 35); - LayerImpl* result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - - // We should have passed through the no-touch layer and found the layer - // behind it. - EXPECT_TRUE(result_layer); - - host_impl.active_tree()->LayerById(1234)->SetContentsOpaque(true); - result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - - // Even with an opaque layer in the middle, we should still find the layer - // with - // the touch handler behind it (since we can't assume that opaque layers are - // opaque to hit testing). - EXPECT_TRUE(result_layer); - - test_point = gfx::Point(35, 15); - result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - ASSERT_TRUE(result_layer); - EXPECT_EQ(123, result_layer->id()); - - test_point = gfx::Point(35, 65); - result_layer = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - test_point, render_surface_layer_list); - EXPECT_FALSE(result_layer); -} - class NoScaleContentLayer : public ContentLayer { public: static scoped_refptr<NoScaleContentLayer> Create(ContentLayerClient* client) { diff --git a/cc/trees/layer_tree_host_impl.cc b/cc/trees/layer_tree_host_impl.cc index 3683884..accd967 100644 --- a/cc/trees/layer_tree_host_impl.cc +++ b/cc/trees/layer_tree_host_impl.cc @@ -483,8 +483,8 @@ bool LayerTreeHostImpl::IsCurrentlyScrollingLayerAt( gfx::PointF device_viewport_point = gfx::ScalePoint(viewport_point, device_scale_factor_); - LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - device_viewport_point, active_tree_->RenderSurfaceLayerList()); + LayerImpl* layer_impl = + active_tree_->FindLayerThatIsHitByPoint(device_viewport_point); bool scroll_on_main_thread = false; LayerImpl* scrolling_layer_impl = FindScrollLayerForDeviceViewportPoint( @@ -503,9 +503,9 @@ bool LayerTreeHostImpl::HaveTouchEventHandlersAt( gfx::ScalePoint(viewport_point, device_scale_factor_); LayerImpl* layer_impl = - LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion( - device_viewport_point, - active_tree_->RenderSurfaceLayerList()); + active_tree_->FindLayerThatIsHitByPointInTouchHandlerRegion( + device_viewport_point); + return layer_impl != NULL; } @@ -2206,14 +2206,13 @@ InputHandler::ScrollStatus LayerTreeHostImpl::ScrollBegin( gfx::PointF device_viewport_point = gfx::ScalePoint(viewport_point, device_scale_factor_); - LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - device_viewport_point, - active_tree_->RenderSurfaceLayerList()); + LayerImpl* layer_impl = + active_tree_->FindLayerThatIsHitByPoint(device_viewport_point); if (layer_impl) { LayerImpl* scroll_layer_impl = - LayerTreeHostCommon::FindFirstScrollingLayerThatIsHitByPoint( - device_viewport_point, active_tree_->RenderSurfaceLayerList()); + active_tree_->FindFirstScrollingLayerThatIsHitByPoint( + device_viewport_point); if (scroll_layer_impl && !HasScrollAncestor(layer_impl, scroll_layer_impl)) return ScrollUnknown; } @@ -2573,10 +2572,8 @@ void LayerTreeHostImpl::MouseMoveAt(const gfx::Point& viewport_point) { gfx::PointF device_viewport_point = gfx::ScalePoint(viewport_point, device_scale_factor_); - - LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint( - device_viewport_point, - active_tree_->RenderSurfaceLayerList()); + LayerImpl* layer_impl = + active_tree_->FindLayerThatIsHitByPoint(device_viewport_point); if (HandleMouseOverScrollbar(layer_impl, device_viewport_point)) return; diff --git a/cc/trees/layer_tree_host_impl_unittest.cc b/cc/trees/layer_tree_host_impl_unittest.cc index c262802..80f250e 100644 --- a/cc/trees/layer_tree_host_impl_unittest.cc +++ b/cc/trees/layer_tree_host_impl_unittest.cc @@ -6078,7 +6078,7 @@ TEST_F(LayerTreeHostImplTest, ScrollUnknownScrollAncestorMismatch) { occluder_layer->SetDrawsContent(true); occluder_layer->SetBounds(content_size); occluder_layer->SetContentBounds(content_size); - occluder_layer->SetPosition(gfx::PointF()); + occluder_layer->SetPosition(gfx::PointF(-10.f, -10.f)); occluder_layer->SetAnchorPoint(gfx::PointF()); int child_scroll_clip_layer_id = 7; @@ -6089,7 +6089,7 @@ TEST_F(LayerTreeHostImplTest, ScrollUnknownScrollAncestorMismatch) { scoped_ptr<LayerImpl> child_scroll = CreateScrollableLayer( child_scroll_layer_id, content_size, child_scroll_clip.get()); - child_scroll->SetDrawsContent(false); + child_scroll->SetPosition(gfx::PointF(10.f, 10.f)); child_scroll->AddChild(occluder_layer.Pass()); scroll_layer->AddChild(child_scroll.Pass()); @@ -6100,6 +6100,95 @@ TEST_F(LayerTreeHostImplTest, ScrollUnknownScrollAncestorMismatch) { host_impl_->ScrollBegin(gfx::Point(), InputHandler::Wheel)); } +TEST_F(LayerTreeHostImplTest, ScrollInvisibleScroller) { + gfx::Size content_size(100, 100); + SetupScrollAndContentsLayers(content_size); + + LayerImpl* root = host_impl_->active_tree()->LayerById(1); + + int scroll_layer_id = 2; + LayerImpl* scroll_layer = + host_impl_->active_tree()->LayerById(scroll_layer_id); + + int child_scroll_layer_id = 7; + scoped_ptr<LayerImpl> child_scroll = + CreateScrollableLayer(child_scroll_layer_id, content_size, root); + child_scroll->SetDrawsContent(false); + + scroll_layer->AddChild(child_scroll.Pass()); + + DrawFrame(); + + // We should not have scrolled |child_scroll| even though we technically "hit" + // it. The reason for this is that if the scrolling the scroll would not move + // any layer that is a drawn RSLL member, then we can ignore the hit. + // + // Why ScrollStarted? In this case, it's because we've bubbled out and started + // overscrolling the inner viewport. + EXPECT_EQ(InputHandler::ScrollStarted, + host_impl_->ScrollBegin(gfx::Point(), InputHandler::Wheel)); + + EXPECT_EQ(2, host_impl_->CurrentlyScrollingLayer()->id()); +} + +TEST_F(LayerTreeHostImplTest, ScrollInvisibleScrollerWithVisibleScrollChild) { + // This test case is very similar to the one above with one key difference: + // the invisible scroller has a scroll child that is indeed draw contents. + // If we attempt to initiate a gesture scroll off of the visible scroll child + // we should still start the scroll child. + gfx::Size content_size(100, 100); + SetupScrollAndContentsLayers(content_size); + + LayerImpl* root = host_impl_->active_tree()->LayerById(1); + + int scroll_layer_id = 2; + LayerImpl* scroll_layer = + host_impl_->active_tree()->LayerById(scroll_layer_id); + + int scroll_child_id = 6; + scoped_ptr<LayerImpl> scroll_child = + LayerImpl::Create(host_impl_->active_tree(), scroll_child_id); + scroll_child->SetDrawsContent(true); + scroll_child->SetBounds(content_size); + scroll_child->SetContentBounds(content_size); + // Move the scroll child so it's not hit by our test point. + scroll_child->SetPosition(gfx::PointF(10.f, 10.f)); + scroll_child->SetAnchorPoint(gfx::PointF()); + + int invisible_scroll_layer_id = 7; + scoped_ptr<LayerImpl> invisible_scroll = + CreateScrollableLayer(invisible_scroll_layer_id, content_size, root); + invisible_scroll->SetDrawsContent(false); + + int container_id = 8; + scoped_ptr<LayerImpl> container = + LayerImpl::Create(host_impl_->active_tree(), container_id); + + scoped_ptr<std::set<LayerImpl*> > scroll_children(new std::set<LayerImpl*>()); + scroll_children->insert(scroll_child.get()); + invisible_scroll->SetScrollChildren(scroll_children.release()); + + scroll_child->SetScrollParent(invisible_scroll.get()); + + container->AddChild(invisible_scroll.Pass()); + container->AddChild(scroll_child.Pass()); + + scroll_layer->AddChild(container.Pass()); + + DrawFrame(); + + // We should not have scrolled |child_scroll| even though we technically "hit" + // it. The reason for this is that if the scrolling the scroll would not move + // any layer that is a drawn RSLL member, then we can ignore the hit. + // + // Why ScrollStarted? In this case, it's because we've bubbled out and started + // overscrolling the inner viewport. + EXPECT_EQ(InputHandler::ScrollStarted, + host_impl_->ScrollBegin(gfx::Point(), InputHandler::Wheel)); + + EXPECT_EQ(7, host_impl_->CurrentlyScrollingLayer()->id()); +} + // Make sure LatencyInfo carried by LatencyInfoSwapPromise are passed // to CompositorFrameMetadata after SwapBuffers(); TEST_F(LayerTreeHostImplTest, LatencyInfoPassedToCompositorFrameMetadata) { diff --git a/cc/trees/layer_tree_impl.cc b/cc/trees/layer_tree_impl.cc index 97e9c17..a4d5cab 100644 --- a/cc/trees/layer_tree_impl.cc +++ b/cc/trees/layer_tree_impl.cc @@ -4,6 +4,9 @@ #include "cc/trees/layer_tree_impl.h" +#include <limits> +#include <set> + #include "base/debug/trace_event.h" #include "cc/animation/keyframed_animation_curve.h" #include "cc/animation/scrollbar_animation_controller.h" @@ -20,6 +23,7 @@ #include "cc/resources/ui_resource_request.h" #include "cc/trees/layer_tree_host_common.h" #include "cc/trees/layer_tree_host_impl.h" +#include "ui/gfx/point_conversions.h" #include "ui/gfx/size_conversions.h" #include "ui/gfx/vector2d_conversions.h" @@ -1022,4 +1026,262 @@ void LayerTreeImpl::ReleaseResourcesRecursive(LayerImpl* current) { ReleaseResourcesRecursive(current->children()[i]); } +template <typename LayerType> +static inline bool LayerClipsSubtree(LayerType* layer) { + return layer->masks_to_bounds() || layer->mask_layer(); +} + +static bool PointHitsRect( + const gfx::PointF& screen_space_point, + const gfx::Transform& local_space_to_screen_space_transform, + const gfx::RectF& local_space_rect, + float* distance_to_camera) { + // If the transform is not invertible, then assume that this point doesn't hit + // this rect. + gfx::Transform inverse_local_space_to_screen_space( + gfx::Transform::kSkipInitialization); + if (!local_space_to_screen_space_transform.GetInverse( + &inverse_local_space_to_screen_space)) + return false; + + // Transform the hit test point from screen space to the local space of the + // given rect. + bool clipped = false; + gfx::Point3F planar_point = MathUtil::ProjectPoint3D( + inverse_local_space_to_screen_space, screen_space_point, &clipped); + gfx::PointF hit_test_point_in_local_space = + gfx::PointF(planar_point.x(), planar_point.y()); + + // If ProjectPoint could not project to a valid value, then we assume that + // this point doesn't hit this rect. + if (clipped) + return false; + + if (!local_space_rect.Contains(hit_test_point_in_local_space)) + return false; + + if (distance_to_camera) { + // To compute the distance to the camera, we have to take the planar point + // and pull it back to world space and compute the displacement along the + // z-axis. + gfx::Point3F planar_point_in_screen_space(planar_point); + local_space_to_screen_space_transform.TransformPoint( + &planar_point_in_screen_space); + *distance_to_camera = planar_point_in_screen_space.z(); + } + + return true; +} + +static bool PointHitsRegion(const gfx::PointF& screen_space_point, + const gfx::Transform& screen_space_transform, + const Region& layer_space_region, + float layer_content_scale_x, + float layer_content_scale_y) { + // If the transform is not invertible, then assume that this point doesn't hit + // this region. + gfx::Transform inverse_screen_space_transform( + gfx::Transform::kSkipInitialization); + if (!screen_space_transform.GetInverse(&inverse_screen_space_transform)) + return false; + + // Transform the hit test point from screen space to the local space of the + // given region. + bool clipped = false; + gfx::PointF hit_test_point_in_content_space = MathUtil::ProjectPoint( + inverse_screen_space_transform, screen_space_point, &clipped); + gfx::PointF hit_test_point_in_layer_space = + gfx::ScalePoint(hit_test_point_in_content_space, + 1.f / layer_content_scale_x, + 1.f / layer_content_scale_y); + + // If ProjectPoint could not project to a valid value, then we assume that + // this point doesn't hit this region. + if (clipped) + return false; + + return layer_space_region.Contains( + gfx::ToRoundedPoint(hit_test_point_in_layer_space)); +} + +static bool PointIsClippedBySurfaceOrClipRect( + const gfx::PointF& screen_space_point, + LayerImpl* layer) { + LayerImpl* current_layer = layer; + + // Walk up the layer tree and hit-test any render_surfaces and any layer + // clip rects that are active. + while (current_layer) { + if (current_layer->render_surface() && + !PointHitsRect( + screen_space_point, + current_layer->render_surface()->screen_space_transform(), + current_layer->render_surface()->content_rect(), + NULL)) + return true; + + // Note that drawable content rects are actually in target surface space, so + // the transform we have to provide is the target surface's + // screen_space_transform. + LayerImpl* render_target = current_layer->render_target(); + if (LayerClipsSubtree(current_layer) && + !PointHitsRect( + screen_space_point, + render_target->render_surface()->screen_space_transform(), + current_layer->drawable_content_rect(), + NULL)) + return true; + + current_layer = current_layer->parent(); + } + + // If we have finished walking all ancestors without having already exited, + // then the point is not clipped by any ancestors. + return false; +} + +static bool PointHitsLayer(LayerImpl* layer, + const gfx::PointF& screen_space_point, + float* distance_to_intersection) { + gfx::RectF content_rect(layer->content_bounds()); + if (!PointHitsRect(screen_space_point, + layer->screen_space_transform(), + content_rect, + distance_to_intersection)) + return false; + + // At this point, we think the point does hit the layer, but we need to walk + // up the parents to ensure that the layer was not clipped in such a way + // that the hit point actually should not hit the layer. + if (PointIsClippedBySurfaceOrClipRect(screen_space_point, layer)) + return false; + + // Skip the HUD layer. + if (layer == layer->layer_tree_impl()->hud_layer()) + return false; + + return true; +} + +struct FindClosestMatchingLayerDataForRecursion { + FindClosestMatchingLayerDataForRecursion() + : closest_match(NULL), + closest_distance(-std::numeric_limits<float>::infinity()) {} + LayerImpl* closest_match; + // Note that the positive z-axis points towards the camera, so bigger means + // closer in this case, counterintuitively. + float closest_distance; +}; + +template <typename Functor> +static void FindClosestMatchingLayer( + const gfx::PointF& screen_space_point, + LayerImpl* layer, + const Functor& func, + FindClosestMatchingLayerDataForRecursion* data_for_recursion) { + for (int i = layer->children().size() - 1; i >= 0; --i) { + FindClosestMatchingLayer( + screen_space_point, layer->children()[i], func, data_for_recursion); + } + + float distance_to_intersection = 0.f; + if (func(layer) && + PointHitsLayer(layer, screen_space_point, &distance_to_intersection) && + ((!data_for_recursion->closest_match || + distance_to_intersection > data_for_recursion->closest_distance))) { + data_for_recursion->closest_distance = distance_to_intersection; + data_for_recursion->closest_match = layer; + } +} + +static bool ScrollsAnyDrawnRenderSurfaceLayerListMember(LayerImpl* layer) { + if (!layer->scrollable()) + return false; + if (layer->IsDrawnRenderSurfaceLayerListMember()) + return true; + if (!layer->scroll_children()) + return false; + for (std::set<LayerImpl*>::const_iterator it = + layer->scroll_children()->begin(); + it != layer->scroll_children()->end(); + ++it) { + if ((*it)->IsDrawnRenderSurfaceLayerListMember()) + return true; + } + return false; +} + +struct FindScrollingLayerFunctor { + bool operator()(LayerImpl* layer) const { + return ScrollsAnyDrawnRenderSurfaceLayerListMember(layer); + } +}; + +LayerImpl* LayerTreeImpl::FindFirstScrollingLayerThatIsHitByPoint( + const gfx::PointF& screen_space_point) { + FindClosestMatchingLayerDataForRecursion data_for_recursion; + FindClosestMatchingLayer(screen_space_point, + root_layer(), + FindScrollingLayerFunctor(), + &data_for_recursion); + return data_for_recursion.closest_match; +} + +struct HitTestVisibleScrollableOrTouchableFunctor { + bool operator()(LayerImpl* layer) const { + return layer->IsDrawnRenderSurfaceLayerListMember() || + ScrollsAnyDrawnRenderSurfaceLayerListMember(layer) || + !layer->touch_event_handler_region().IsEmpty() || + layer->have_wheel_event_handlers(); + } +}; + +LayerImpl* LayerTreeImpl::FindLayerThatIsHitByPoint( + const gfx::PointF& screen_space_point) { + FindClosestMatchingLayerDataForRecursion data_for_recursion; + FindClosestMatchingLayer(screen_space_point, + root_layer(), + HitTestVisibleScrollableOrTouchableFunctor(), + &data_for_recursion); + return data_for_recursion.closest_match; +} + +static bool LayerHasTouchEventHandlersAt(const gfx::PointF& screen_space_point, + LayerImpl* layer_impl) { + if (layer_impl->touch_event_handler_region().IsEmpty()) + return false; + + if (!PointHitsRegion(screen_space_point, + layer_impl->screen_space_transform(), + layer_impl->touch_event_handler_region(), + layer_impl->contents_scale_x(), + layer_impl->contents_scale_y())) + return false; + + // At this point, we think the point does hit the touch event handler region + // on the layer, but we need to walk up the parents to ensure that the layer + // was not clipped in such a way that the hit point actually should not hit + // the layer. + if (PointIsClippedBySurfaceOrClipRect(screen_space_point, layer_impl)) + return false; + + return true; +} + +struct FindTouchEventLayerFunctor { + bool operator()(LayerImpl* layer) const { + return LayerHasTouchEventHandlersAt(screen_space_point, layer); + } + const gfx::PointF screen_space_point; +}; + +LayerImpl* LayerTreeImpl::FindLayerThatIsHitByPointInTouchHandlerRegion( + const gfx::PointF& screen_space_point) { + FindTouchEventLayerFunctor func = {screen_space_point}; + FindClosestMatchingLayerDataForRecursion data_for_recursion; + FindClosestMatchingLayer( + screen_space_point, root_layer(), func, &data_for_recursion); + return data_for_recursion.closest_match; +} + } // namespace cc diff --git a/cc/trees/layer_tree_impl.h b/cc/trees/layer_tree_impl.h index 4330213..5b740c3 100644 --- a/cc/trees/layer_tree_impl.h +++ b/cc/trees/layer_tree_impl.h @@ -252,6 +252,14 @@ class CC_EXPORT LayerTreeImpl { return render_surface_layer_list_id_; } + LayerImpl* FindFirstScrollingLayerThatIsHitByPoint( + const gfx::PointF& screen_space_point); + + LayerImpl* FindLayerThatIsHitByPoint(const gfx::PointF& screen_space_point); + + LayerImpl* FindLayerThatIsHitByPointInTouchHandlerRegion( + const gfx::PointF& screen_space_point); + protected: explicit LayerTreeImpl(LayerTreeHostImpl* layer_tree_host_impl); void ReleaseResourcesRecursive(LayerImpl* current); diff --git a/cc/trees/layer_tree_impl_unittest.cc b/cc/trees/layer_tree_impl_unittest.cc new file mode 100644 index 0000000..b27a06e --- /dev/null +++ b/cc/trees/layer_tree_impl_unittest.cc @@ -0,0 +1,1940 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "cc/trees/layer_tree_impl.h" + +#include "cc/layers/heads_up_display_layer_impl.h" +#include "cc/layers/layer.h" +#include "cc/test/fake_impl_proxy.h" +#include "cc/test/fake_layer_tree_host_impl.h" +#include "cc/test/fake_output_surface.h" +#include "cc/test/geometry_test_utils.h" +#include "cc/test/layer_tree_host_common_test.h" +#include "cc/test/test_shared_bitmap_manager.h" +#include "cc/trees/layer_tree_host_impl.h" +#include "ui/gfx/size_conversions.h" + +namespace cc { +namespace { + +class LayerTreeImplTest : public LayerTreeHostCommonTest { + public: + LayerTreeImplTest() { + LayerTreeSettings settings; + settings.layer_transforms_should_scale_layer_contents = true; + host_impl_.reset( + new FakeLayerTreeHostImpl(settings, &proxy_, &shared_bitmap_manager_)); + EXPECT_TRUE(host_impl_->InitializeRenderer( + FakeOutputSurface::Create3d().PassAs<OutputSurface>())); + } + + FakeLayerTreeHostImpl& host_impl() { return *host_impl_; } + + LayerImpl* root_layer() { return host_impl_->active_tree()->root_layer(); } + + const LayerImplList& RenderSurfaceLayerList() const { + return host_impl_->active_tree()->RenderSurfaceLayerList(); + } + + private: + TestSharedBitmapManager shared_bitmap_manager_; + FakeImplProxy proxy_; + scoped_ptr<FakeLayerTreeHostImpl> host_impl_; +}; + +TEST_F(LayerTreeImplTest, HitTestingForSingleLayer) { + scoped_ptr<LayerImpl> root = + LayerImpl::Create(host_impl().active_tree(), 12345); + + gfx::Transform identity_matrix; + gfx::PointF anchor; + gfx::PointF position; + gfx::Size bounds(100, 100); + SetLayerPropertiesForTesting( + root.get(), identity_matrix, anchor, position, bounds, true, false); + root->SetDrawsContent(true); + + host_impl().SetViewportSize(root->bounds()); + host_impl().active_tree()->SetRootLayer(root.Pass()); + host_impl().active_tree()->UpdateDrawProperties(); + + // Sanity check the scenario we just created. + ASSERT_EQ(1u, RenderSurfaceLayerList().size()); + ASSERT_EQ(1u, root_layer()->render_surface()->layer_list().size()); + + // Hit testing for a point outside the layer should return a null pointer. + gfx::Point test_point(101, 101); + LayerImpl* result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + EXPECT_FALSE(result_layer); + + test_point = gfx::Point(-1, -1); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + EXPECT_FALSE(result_layer); + + // Hit testing for a point inside should return the root layer. + test_point = gfx::Point(1, 1); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(12345, result_layer->id()); + + test_point = gfx::Point(99, 99); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(12345, result_layer->id()); +} + +TEST_F(LayerTreeImplTest, HitTestingForSingleLayerAndHud) { + scoped_ptr<LayerImpl> root = + LayerImpl::Create(host_impl().active_tree(), 12345); + scoped_ptr<HeadsUpDisplayLayerImpl> hud = + HeadsUpDisplayLayerImpl::Create(host_impl().active_tree(), 11111); + + gfx::Transform identity_matrix; + gfx::PointF anchor; + gfx::PointF position; + gfx::Size bounds(100, 100); + SetLayerPropertiesForTesting( + root.get(), identity_matrix, anchor, position, bounds, true, false); + root->SetDrawsContent(true); + + // Create hud and add it as a child of root. + gfx::Size hud_bounds(200, 200); + SetLayerPropertiesForTesting( + hud.get(), identity_matrix, anchor, position, hud_bounds, true, false); + hud->SetDrawsContent(true); + + host_impl().active_tree()->set_hud_layer(hud.get()); + root->AddChild(hud.PassAs<LayerImpl>()); + + host_impl().SetViewportSize(hud_bounds); + host_impl().active_tree()->SetRootLayer(root.Pass()); + host_impl().active_tree()->UpdateDrawProperties(); + + // Sanity check the scenario we just created. + ASSERT_EQ(1u, RenderSurfaceLayerList().size()); + ASSERT_EQ(2u, root_layer()->render_surface()->layer_list().size()); + + // Hit testing for a point inside HUD, but outside root should return null + gfx::Point test_point(101, 101); + LayerImpl* result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + EXPECT_FALSE(result_layer); + + test_point = gfx::Point(-1, -1); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + EXPECT_FALSE(result_layer); + + // Hit testing for a point inside should return the root layer, never the HUD + // layer. + test_point = gfx::Point(1, 1); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(12345, result_layer->id()); + + test_point = gfx::Point(99, 99); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(12345, result_layer->id()); +} + +TEST_F(LayerTreeImplTest, HitTestingForUninvertibleTransform) { + scoped_ptr<LayerImpl> root = + LayerImpl::Create(host_impl().active_tree(), 12345); + + gfx::Transform uninvertible_transform; + uninvertible_transform.matrix().set(0, 0, 0.0); + uninvertible_transform.matrix().set(1, 1, 0.0); + uninvertible_transform.matrix().set(2, 2, 0.0); + uninvertible_transform.matrix().set(3, 3, 0.0); + ASSERT_FALSE(uninvertible_transform.IsInvertible()); + + gfx::Transform identity_matrix; + gfx::PointF anchor; + gfx::PointF position; + gfx::Size bounds(100, 100); + SetLayerPropertiesForTesting(root.get(), + uninvertible_transform, + anchor, + position, + bounds, + true, + false); + root->SetDrawsContent(true); + + host_impl().SetViewportSize(root->bounds()); + host_impl().active_tree()->SetRootLayer(root.Pass()); + host_impl().active_tree()->UpdateDrawProperties(); + // Sanity check the scenario we just created. + ASSERT_EQ(1u, RenderSurfaceLayerList().size()); + ASSERT_EQ(1u, root_layer()->render_surface()->layer_list().size()); + ASSERT_FALSE(root_layer()->screen_space_transform().IsInvertible()); + + // Hit testing any point should not hit the layer. If the invertible matrix is + // accidentally ignored and treated like an identity, then the hit testing + // will incorrectly hit the layer when it shouldn't. + gfx::Point test_point(1, 1); + LayerImpl* result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + EXPECT_FALSE(result_layer); + + test_point = gfx::Point(10, 10); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + EXPECT_FALSE(result_layer); + + test_point = gfx::Point(10, 30); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + EXPECT_FALSE(result_layer); + + test_point = gfx::Point(50, 50); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + EXPECT_FALSE(result_layer); + + test_point = gfx::Point(67, 48); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + EXPECT_FALSE(result_layer); + + test_point = gfx::Point(99, 99); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + EXPECT_FALSE(result_layer); + + test_point = gfx::Point(-1, -1); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + EXPECT_FALSE(result_layer); +} + +TEST_F(LayerTreeImplTest, HitTestingForSinglePositionedLayer) { + scoped_ptr<LayerImpl> root = + LayerImpl::Create(host_impl().active_tree(), 12345); + + gfx::Transform identity_matrix; + gfx::PointF anchor; + // this layer is positioned, and hit testing should correctly know where the + // layer is located. + gfx::PointF position(50.f, 50.f); + gfx::Size bounds(100, 100); + SetLayerPropertiesForTesting( + root.get(), identity_matrix, anchor, position, bounds, true, false); + root->SetDrawsContent(true); + + host_impl().SetViewportSize(root->bounds()); + host_impl().active_tree()->SetRootLayer(root.Pass()); + host_impl().active_tree()->UpdateDrawProperties(); + + // Sanity check the scenario we just created. + ASSERT_EQ(1u, RenderSurfaceLayerList().size()); + ASSERT_EQ(1u, root_layer()->render_surface()->layer_list().size()); + + // Hit testing for a point outside the layer should return a null pointer. + gfx::Point test_point(49, 49); + LayerImpl* result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + EXPECT_FALSE(result_layer); + + // Even though the layer exists at (101, 101), it should not be visible there + // since the root render surface would clamp it. + test_point = gfx::Point(101, 101); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + EXPECT_FALSE(result_layer); + + // Hit testing for a point inside should return the root layer. + test_point = gfx::Point(51, 51); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(12345, result_layer->id()); + + test_point = gfx::Point(99, 99); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(12345, result_layer->id()); +} + +TEST_F(LayerTreeImplTest, HitTestingForSingleRotatedLayer) { + scoped_ptr<LayerImpl> root = + LayerImpl::Create(host_impl().active_tree(), 12345); + + gfx::Transform identity_matrix; + gfx::Transform rotation45_degrees_about_center; + rotation45_degrees_about_center.Translate(50.0, 50.0); + rotation45_degrees_about_center.RotateAboutZAxis(45.0); + rotation45_degrees_about_center.Translate(-50.0, -50.0); + gfx::PointF anchor; + gfx::PointF position; + gfx::Size bounds(100, 100); + SetLayerPropertiesForTesting(root.get(), + rotation45_degrees_about_center, + anchor, + position, + bounds, + true, + false); + root->SetDrawsContent(true); + + host_impl().SetViewportSize(root->bounds()); + host_impl().active_tree()->SetRootLayer(root.Pass()); + host_impl().active_tree()->UpdateDrawProperties(); + + // Sanity check the scenario we just created. + ASSERT_EQ(1u, RenderSurfaceLayerList().size()); + ASSERT_EQ(1u, root_layer()->render_surface()->layer_list().size()); + + // Hit testing for points outside the layer. + // These corners would have been inside the un-transformed layer, but they + // should not hit the correctly transformed layer. + gfx::Point test_point(99, 99); + LayerImpl* result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + EXPECT_FALSE(result_layer); + + test_point = gfx::Point(1, 1); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + EXPECT_FALSE(result_layer); + + // Hit testing for a point inside should return the root layer. + test_point = gfx::Point(1, 50); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(12345, result_layer->id()); + + // Hit testing the corners that would overlap the unclipped layer, but are + // outside the clipped region. + test_point = gfx::Point(50, -1); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_FALSE(result_layer); + + test_point = gfx::Point(-1, 50); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_FALSE(result_layer); +} + +TEST_F(LayerTreeImplTest, HitTestingForSinglePerspectiveLayer) { + scoped_ptr<LayerImpl> root = + LayerImpl::Create(host_impl().active_tree(), 12345); + + gfx::Transform identity_matrix; + + // perspective_projection_about_center * translation_by_z is designed so that + // the 100 x 100 layer becomes 50 x 50, and remains centered at (50, 50). + gfx::Transform perspective_projection_about_center; + perspective_projection_about_center.Translate(50.0, 50.0); + perspective_projection_about_center.ApplyPerspectiveDepth(1.0); + perspective_projection_about_center.Translate(-50.0, -50.0); + gfx::Transform translation_by_z; + translation_by_z.Translate3d(0.0, 0.0, -1.0); + + gfx::PointF anchor; + gfx::PointF position; + gfx::Size bounds(100, 100); + SetLayerPropertiesForTesting( + root.get(), + perspective_projection_about_center * translation_by_z, + anchor, + position, + bounds, + true, + false); + root->SetDrawsContent(true); + + host_impl().SetViewportSize(root->bounds()); + host_impl().active_tree()->SetRootLayer(root.Pass()); + host_impl().active_tree()->UpdateDrawProperties(); + + // Sanity check the scenario we just created. + ASSERT_EQ(1u, RenderSurfaceLayerList().size()); + ASSERT_EQ(1u, root_layer()->render_surface()->layer_list().size()); + + // Hit testing for points outside the layer. + // These corners would have been inside the un-transformed layer, but they + // should not hit the correctly transformed layer. + gfx::Point test_point(24, 24); + LayerImpl* result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + EXPECT_FALSE(result_layer); + + test_point = gfx::Point(76, 76); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + EXPECT_FALSE(result_layer); + + // Hit testing for a point inside should return the root layer. + test_point = gfx::Point(26, 26); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(12345, result_layer->id()); + + test_point = gfx::Point(74, 74); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(12345, result_layer->id()); +} + +TEST_F(LayerTreeImplTest, HitTestingForSingleLayerWithScaledContents) { + // A layer's visible content rect is actually in the layer's content space. + // The screen space transform converts from the layer's origin space to screen + // space. This test makes sure that hit testing works correctly accounts for + // the contents scale. A contents scale that is not 1 effectively forces a + // non-identity transform between layer's content space and layer's origin + // space. The hit testing code must take this into account. + // + // To test this, the layer is positioned at (25, 25), and is size (50, 50). If + // contents scale is ignored, then hit testing will mis-interpret the visible + // content rect as being larger than the actual bounds of the layer. + // + scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl().active_tree(), 1); + + gfx::Transform identity_matrix; + gfx::PointF anchor; + + SetLayerPropertiesForTesting(root.get(), + identity_matrix, + anchor, + gfx::PointF(), + gfx::Size(100, 100), + true, + false); + { + gfx::PointF position(25.f, 25.f); + gfx::Size bounds(50, 50); + scoped_ptr<LayerImpl> test_layer = + LayerImpl::Create(host_impl().active_tree(), 12345); + SetLayerPropertiesForTesting(test_layer.get(), + identity_matrix, + anchor, + position, + bounds, + true, + false); + + // override content bounds and contents scale + test_layer->SetContentBounds(gfx::Size(100, 100)); + test_layer->SetContentsScale(2, 2); + + test_layer->SetDrawsContent(true); + root->AddChild(test_layer.Pass()); + } + + host_impl().SetViewportSize(root->bounds()); + host_impl().active_tree()->SetRootLayer(root.Pass()); + host_impl().active_tree()->UpdateDrawProperties(); + + // Sanity check the scenario we just created. + // The visible content rect for test_layer is actually 100x100, even though + // its layout size is 50x50, positioned at 25x25. + LayerImpl* test_layer = + host_impl().active_tree()->root_layer()->children()[0]; + EXPECT_RECT_EQ(gfx::Rect(0, 0, 100, 100), test_layer->visible_content_rect()); + ASSERT_EQ(1u, RenderSurfaceLayerList().size()); + ASSERT_EQ(1u, root_layer()->render_surface()->layer_list().size()); + + // Hit testing for a point outside the layer should return a null pointer (the + // root layer does not draw content, so it will not be hit tested either). + gfx::Point test_point(101, 101); + LayerImpl* result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + EXPECT_FALSE(result_layer); + + test_point = gfx::Point(24, 24); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + EXPECT_FALSE(result_layer); + + test_point = gfx::Point(76, 76); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + EXPECT_FALSE(result_layer); + + // Hit testing for a point inside should return the test layer. + test_point = gfx::Point(26, 26); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(12345, result_layer->id()); + + test_point = gfx::Point(74, 74); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(12345, result_layer->id()); +} + +TEST_F(LayerTreeImplTest, HitTestingForSimpleClippedLayer) { + // Test that hit-testing will only work for the visible portion of a layer, + // and not the entire layer bounds. Here we just test the simple axis-aligned + // case. + gfx::Transform identity_matrix; + gfx::PointF anchor; + + scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl().active_tree(), 1); + SetLayerPropertiesForTesting(root.get(), + identity_matrix, + anchor, + gfx::PointF(), + gfx::Size(100, 100), + true, + false); + { + scoped_ptr<LayerImpl> clipping_layer = + LayerImpl::Create(host_impl().active_tree(), 123); + // this layer is positioned, and hit testing should correctly know where the + // layer is located. + gfx::PointF position(25.f, 25.f); + gfx::Size bounds(50, 50); + SetLayerPropertiesForTesting(clipping_layer.get(), + identity_matrix, + anchor, + position, + bounds, + true, + false); + clipping_layer->SetMasksToBounds(true); + + scoped_ptr<LayerImpl> child = + LayerImpl::Create(host_impl().active_tree(), 456); + position = gfx::PointF(-50.f, -50.f); + bounds = gfx::Size(300, 300); + SetLayerPropertiesForTesting( + child.get(), identity_matrix, anchor, position, bounds, true, false); + child->SetDrawsContent(true); + clipping_layer->AddChild(child.Pass()); + root->AddChild(clipping_layer.Pass()); + } + + host_impl().SetViewportSize(root->bounds()); + host_impl().active_tree()->SetRootLayer(root.Pass()); + host_impl().active_tree()->UpdateDrawProperties(); + + // Sanity check the scenario we just created. + ASSERT_EQ(1u, RenderSurfaceLayerList().size()); + ASSERT_EQ(1u, root_layer()->render_surface()->layer_list().size()); + ASSERT_EQ(456, root_layer()->render_surface()->layer_list().at(0)->id()); + + // Hit testing for a point outside the layer should return a null pointer. + // Despite the child layer being very large, it should be clipped to the root + // layer's bounds. + gfx::Point test_point(24, 24); + LayerImpl* result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + EXPECT_FALSE(result_layer); + + // Even though the layer exists at (101, 101), it should not be visible there + // since the clipping_layer would clamp it. + test_point = gfx::Point(76, 76); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + EXPECT_FALSE(result_layer); + + // Hit testing for a point inside should return the child layer. + test_point = gfx::Point(26, 26); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(456, result_layer->id()); + + test_point = gfx::Point(74, 74); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(456, result_layer->id()); +} + +TEST_F(LayerTreeImplTest, HitTestingForMultiClippedRotatedLayer) { + // This test checks whether hit testing correctly avoids hit testing with + // multiple ancestors that clip in non axis-aligned ways. To pass this test, + // the hit testing algorithm needs to recognize that multiple parent layers + // may clip the layer, and should not actually hit those clipped areas. + // + // The child and grand_child layers are both initialized to clip the + // rotated_leaf. The child layer is rotated about the top-left corner, so that + // the root + child clips combined create a triangle. The rotated_leaf will + // only be visible where it overlaps this triangle. + // + scoped_ptr<LayerImpl> root = + LayerImpl::Create(host_impl().active_tree(), 123); + + gfx::Transform identity_matrix; + gfx::PointF anchor; + gfx::PointF position; + gfx::Size bounds(100, 100); + SetLayerPropertiesForTesting( + root.get(), identity_matrix, anchor, position, bounds, true, false); + root->SetMasksToBounds(true); + { + scoped_ptr<LayerImpl> child = + LayerImpl::Create(host_impl().active_tree(), 456); + scoped_ptr<LayerImpl> grand_child = + LayerImpl::Create(host_impl().active_tree(), 789); + scoped_ptr<LayerImpl> rotated_leaf = + LayerImpl::Create(host_impl().active_tree(), 2468); + + position = gfx::PointF(10.f, 10.f); + bounds = gfx::Size(80, 80); + SetLayerPropertiesForTesting( + child.get(), identity_matrix, anchor, position, bounds, true, false); + child->SetMasksToBounds(true); + + gfx::Transform rotation45_degrees_about_corner; + rotation45_degrees_about_corner.RotateAboutZAxis(45.0); + + // remember, positioned with respect to its parent which is already at 10, + // 10 + position = gfx::PointF(); + bounds = + gfx::Size(200, 200); // to ensure it covers at least sqrt(2) * 100. + SetLayerPropertiesForTesting(grand_child.get(), + rotation45_degrees_about_corner, + anchor, + position, + bounds, + true, + false); + grand_child->SetMasksToBounds(true); + + // Rotates about the center of the layer + gfx::Transform rotated_leaf_transform; + rotated_leaf_transform.Translate( + -10.0, -10.0); // cancel out the grand_parent's position + rotated_leaf_transform.RotateAboutZAxis( + -45.0); // cancel out the corner 45-degree rotation of the parent. + rotated_leaf_transform.Translate(50.0, 50.0); + rotated_leaf_transform.RotateAboutZAxis(45.0); + rotated_leaf_transform.Translate(-50.0, -50.0); + position = gfx::PointF(); + bounds = gfx::Size(100, 100); + SetLayerPropertiesForTesting(rotated_leaf.get(), + rotated_leaf_transform, + anchor, + position, + bounds, + true, + false); + rotated_leaf->SetDrawsContent(true); + + grand_child->AddChild(rotated_leaf.Pass()); + child->AddChild(grand_child.Pass()); + root->AddChild(child.Pass()); + } + + host_impl().SetViewportSize(root->bounds()); + host_impl().active_tree()->SetRootLayer(root.Pass()); + host_impl().active_tree()->UpdateDrawProperties(); + + // Sanity check the scenario we just created. + // The grand_child is expected to create a render surface because it + // MasksToBounds and is not axis aligned. + ASSERT_EQ(2u, RenderSurfaceLayerList().size()); + ASSERT_EQ( + 1u, + RenderSurfaceLayerList().at(0)->render_surface()->layer_list().size()); + ASSERT_EQ(789, + RenderSurfaceLayerList() + .at(0) + ->render_surface() + ->layer_list() + .at(0) + ->id()); // grand_child's surface. + ASSERT_EQ( + 1u, + RenderSurfaceLayerList().at(1)->render_surface()->layer_list().size()); + ASSERT_EQ( + 2468, + RenderSurfaceLayerList()[1]->render_surface()->layer_list().at(0)->id()); + + // (11, 89) is close to the the bottom left corner within the clip, but it is + // not inside the layer. + gfx::Point test_point(11, 89); + LayerImpl* result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + EXPECT_FALSE(result_layer); + + // Closer inwards from the bottom left will overlap the layer. + test_point = gfx::Point(25, 75); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(2468, result_layer->id()); + + // (4, 50) is inside the unclipped layer, but that corner of the layer should + // be clipped away by the grandparent and should not get hit. If hit testing + // blindly uses visible content rect without considering how parent may clip + // the layer, then hit testing would accidentally think that the point + // successfully hits the layer. + test_point = gfx::Point(4, 50); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + EXPECT_FALSE(result_layer); + + // (11, 50) is inside the layer and within the clipped area. + test_point = gfx::Point(11, 50); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(2468, result_layer->id()); + + // Around the middle, just to the right and up, would have hit the layer + // except that that area should be clipped away by the parent. + test_point = gfx::Point(51, 49); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + EXPECT_FALSE(result_layer); + + // Around the middle, just to the left and down, should successfully hit the + // layer. + test_point = gfx::Point(49, 51); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(2468, result_layer->id()); +} + +TEST_F(LayerTreeImplTest, HitTestingForNonClippingIntermediateLayer) { + // This test checks that hit testing code does not accidentally clip to layer + // bounds for a layer that actually does not clip. + gfx::Transform identity_matrix; + gfx::PointF anchor; + + scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl().active_tree(), 1); + SetLayerPropertiesForTesting(root.get(), + identity_matrix, + anchor, + gfx::PointF(), + gfx::Size(100, 100), + true, + false); + { + scoped_ptr<LayerImpl> intermediate_layer = + LayerImpl::Create(host_impl().active_tree(), 123); + // this layer is positioned, and hit testing should correctly know where the + // layer is located. + gfx::PointF position(10.f, 10.f); + gfx::Size bounds(50, 50); + SetLayerPropertiesForTesting(intermediate_layer.get(), + identity_matrix, + anchor, + position, + bounds, + true, + false); + // Sanity check the intermediate layer should not clip. + ASSERT_FALSE(intermediate_layer->masks_to_bounds()); + ASSERT_FALSE(intermediate_layer->mask_layer()); + + // The child of the intermediate_layer is translated so that it does not + // overlap intermediate_layer at all. If child is incorrectly clipped, we + // would not be able to hit it successfully. + scoped_ptr<LayerImpl> child = + LayerImpl::Create(host_impl().active_tree(), 456); + position = gfx::PointF(60.f, 60.f); // 70, 70 in screen space + bounds = gfx::Size(20, 20); + SetLayerPropertiesForTesting( + child.get(), identity_matrix, anchor, position, bounds, true, false); + child->SetDrawsContent(true); + intermediate_layer->AddChild(child.Pass()); + root->AddChild(intermediate_layer.Pass()); + } + + host_impl().SetViewportSize(root->bounds()); + host_impl().active_tree()->SetRootLayer(root.Pass()); + host_impl().active_tree()->UpdateDrawProperties(); + + // Sanity check the scenario we just created. + ASSERT_EQ(1u, RenderSurfaceLayerList().size()); + ASSERT_EQ(1u, root_layer()->render_surface()->layer_list().size()); + ASSERT_EQ(456, root_layer()->render_surface()->layer_list().at(0)->id()); + + // Hit testing for a point outside the layer should return a null pointer. + gfx::Point test_point(69, 69); + LayerImpl* result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + EXPECT_FALSE(result_layer); + + test_point = gfx::Point(91, 91); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + EXPECT_FALSE(result_layer); + + // Hit testing for a point inside should return the child layer. + test_point = gfx::Point(71, 71); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(456, result_layer->id()); + + test_point = gfx::Point(89, 89); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(456, result_layer->id()); +} + +TEST_F(LayerTreeImplTest, HitTestingForMultipleLayers) { + scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl().active_tree(), 1); + + gfx::Transform identity_matrix; + gfx::PointF anchor; + gfx::PointF position; + gfx::Size bounds(100, 100); + SetLayerPropertiesForTesting( + root.get(), identity_matrix, anchor, position, bounds, true, false); + root->SetDrawsContent(true); + { + // child 1 and child2 are initialized to overlap between x=50 and x=60. + // grand_child is set to overlap both child1 and child2 between y=50 and + // y=60. The expected stacking order is: (front) child2, (second) + // grand_child, (third) child1, and (back) the root layer behind all other + // layers. + + scoped_ptr<LayerImpl> child1 = + LayerImpl::Create(host_impl().active_tree(), 2); + scoped_ptr<LayerImpl> child2 = + LayerImpl::Create(host_impl().active_tree(), 3); + scoped_ptr<LayerImpl> grand_child1 = + LayerImpl::Create(host_impl().active_tree(), 4); + + position = gfx::PointF(10.f, 10.f); + bounds = gfx::Size(50, 50); + SetLayerPropertiesForTesting( + child1.get(), identity_matrix, anchor, position, bounds, true, false); + child1->SetDrawsContent(true); + + position = gfx::PointF(50.f, 10.f); + bounds = gfx::Size(50, 50); + SetLayerPropertiesForTesting( + child2.get(), identity_matrix, anchor, position, bounds, true, false); + child2->SetDrawsContent(true); + + // Remember that grand_child is positioned with respect to its parent (i.e. + // child1). In screen space, the intended position is (10, 50), with size + // 100 x 50. + position = gfx::PointF(0.f, 40.f); + bounds = gfx::Size(100, 50); + SetLayerPropertiesForTesting(grand_child1.get(), + identity_matrix, + anchor, + position, + bounds, + true, + false); + grand_child1->SetDrawsContent(true); + + child1->AddChild(grand_child1.Pass()); + root->AddChild(child1.Pass()); + root->AddChild(child2.Pass()); + } + + LayerImpl* child1 = root->children()[0]; + LayerImpl* child2 = root->children()[1]; + LayerImpl* grand_child1 = child1->children()[0]; + + host_impl().SetViewportSize(root->bounds()); + host_impl().active_tree()->SetRootLayer(root.Pass()); + host_impl().active_tree()->UpdateDrawProperties(); + + // Sanity check the scenario we just created. + ASSERT_TRUE(child1); + ASSERT_TRUE(child2); + ASSERT_TRUE(grand_child1); + ASSERT_EQ(1u, RenderSurfaceLayerList().size()); + + RenderSurfaceImpl* root_render_surface = root_layer()->render_surface(); + ASSERT_EQ(4u, root_render_surface->layer_list().size()); + ASSERT_EQ(1, root_render_surface->layer_list().at(0)->id()); // root layer + ASSERT_EQ(2, root_render_surface->layer_list().at(1)->id()); // child1 + ASSERT_EQ(4, root_render_surface->layer_list().at(2)->id()); // grand_child1 + ASSERT_EQ(3, root_render_surface->layer_list().at(3)->id()); // child2 + + // Nothing overlaps the root_layer at (1, 1), so hit testing there should find + // the root layer. + gfx::Point test_point = gfx::Point(1, 1); + LayerImpl* result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(1, result_layer->id()); + + // At (15, 15), child1 and root are the only layers. child1 is expected to be + // on top. + test_point = gfx::Point(15, 15); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(2, result_layer->id()); + + // At (51, 20), child1 and child2 overlap. child2 is expected to be on top. + test_point = gfx::Point(51, 20); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(3, result_layer->id()); + + // At (80, 51), child2 and grand_child1 overlap. child2 is expected to be on + // top. + test_point = gfx::Point(80, 51); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(3, result_layer->id()); + + // At (51, 51), all layers overlap each other. child2 is expected to be on top + // of all other layers. + test_point = gfx::Point(51, 51); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(3, result_layer->id()); + + // At (20, 51), child1 and grand_child1 overlap. grand_child1 is expected to + // be on top. + test_point = gfx::Point(20, 51); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(4, result_layer->id()); +} + +TEST_F(LayerTreeImplTest, HitTestingForMultipleLayersAtVaryingDepths) { + scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl().active_tree(), 1); + + gfx::Transform identity_matrix; + gfx::PointF anchor; + gfx::PointF position; + gfx::Size bounds(100, 100); + SetLayerPropertiesForTesting( + root.get(), identity_matrix, anchor, position, bounds, true, false); + root->SetDrawsContent(true); + root->SetShouldFlattenTransform(false); + root->SetIs3dSorted(true); + { + // child 1 and child2 are initialized to overlap between x=50 and x=60. + // grand_child is set to overlap both child1 and child2 between y=50 and + // y=60. The expected stacking order is: (front) child2, (second) + // grand_child, (third) child1, and (back) the root layer behind all other + // layers. + + scoped_ptr<LayerImpl> child1 = + LayerImpl::Create(host_impl().active_tree(), 2); + scoped_ptr<LayerImpl> child2 = + LayerImpl::Create(host_impl().active_tree(), 3); + scoped_ptr<LayerImpl> grand_child1 = + LayerImpl::Create(host_impl().active_tree(), 4); + + position = gfx::PointF(10.f, 10.f); + bounds = gfx::Size(50, 50); + SetLayerPropertiesForTesting( + child1.get(), identity_matrix, anchor, position, bounds, true, false); + child1->SetDrawsContent(true); + child1->SetShouldFlattenTransform(false); + child1->SetIs3dSorted(true); + + position = gfx::PointF(50.f, 10.f); + bounds = gfx::Size(50, 50); + gfx::Transform translate_z; + translate_z.Translate3d(0, 0, -10.f); + SetLayerPropertiesForTesting( + child2.get(), translate_z, anchor, position, bounds, true, false); + child2->SetDrawsContent(true); + child2->SetShouldFlattenTransform(false); + child2->SetIs3dSorted(true); + + // Remember that grand_child is positioned with respect to its parent (i.e. + // child1). In screen space, the intended position is (10, 50), with size + // 100 x 50. + position = gfx::PointF(0.f, 40.f); + bounds = gfx::Size(100, 50); + SetLayerPropertiesForTesting(grand_child1.get(), + identity_matrix, + anchor, + position, + bounds, + true, + false); + grand_child1->SetDrawsContent(true); + grand_child1->SetShouldFlattenTransform(false); + + child1->AddChild(grand_child1.Pass()); + root->AddChild(child1.Pass()); + root->AddChild(child2.Pass()); + } + + LayerImpl* child1 = root->children()[0]; + LayerImpl* child2 = root->children()[1]; + LayerImpl* grand_child1 = child1->children()[0]; + + host_impl().SetViewportSize(root->bounds()); + host_impl().active_tree()->SetRootLayer(root.Pass()); + host_impl().active_tree()->UpdateDrawProperties(); + + // Sanity check the scenario we just created. + ASSERT_TRUE(child1); + ASSERT_TRUE(child2); + ASSERT_TRUE(grand_child1); + ASSERT_EQ(1u, RenderSurfaceLayerList().size()); + + RenderSurfaceImpl* root_render_surface = + host_impl().active_tree()->root_layer()->render_surface(); + ASSERT_EQ(4u, root_render_surface->layer_list().size()); + ASSERT_EQ(3, root_render_surface->layer_list().at(0)->id()); + ASSERT_EQ(1, root_render_surface->layer_list().at(1)->id()); + ASSERT_EQ(2, root_render_surface->layer_list().at(2)->id()); + ASSERT_EQ(4, root_render_surface->layer_list().at(3)->id()); + + // Nothing overlaps the root_layer at (1, 1), so hit testing there should find + // the root layer. + gfx::Point test_point = gfx::Point(1, 1); + LayerImpl* result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(1, result_layer->id()); + + // At (15, 15), child1 and root are the only layers. child1 is expected to be + // on top. + test_point = gfx::Point(15, 15); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(2, result_layer->id()); + + // At (51, 20), child1 and child2 overlap. child2 is expected to be on top. + // (because 3 is transformed to the back). + test_point = gfx::Point(51, 20); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(2, result_layer->id()); + + // 3 Would have been on top if it hadn't been transformed to the background. + // Make sure that it isn't hit. + test_point = gfx::Point(80, 51); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(4, result_layer->id()); + + // 3 Would have been on top if it hadn't been transformed to the background. + // Make sure that it isn't hit. + test_point = gfx::Point(51, 51); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(4, result_layer->id()); + + // At (20, 51), child1 and grand_child1 overlap. grand_child1 is expected to + // be on top. + test_point = gfx::Point(20, 51); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(4, result_layer->id()); +} + +TEST_F(LayerTreeImplTest, HitTestingForMultipleLayerLists) { + // + // The geometry is set up similarly to the previous case, but + // all layers are forced to be render surfaces now. + // + scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl().active_tree(), 1); + + gfx::Transform identity_matrix; + gfx::PointF anchor; + gfx::PointF position; + gfx::Size bounds(100, 100); + SetLayerPropertiesForTesting( + root.get(), identity_matrix, anchor, position, bounds, true, false); + root->SetDrawsContent(true); + { + // child 1 and child2 are initialized to overlap between x=50 and x=60. + // grand_child is set to overlap both child1 and child2 between y=50 and + // y=60. The expected stacking order is: (front) child2, (second) + // grand_child, (third) child1, and (back) the root layer behind all other + // layers. + + scoped_ptr<LayerImpl> child1 = + LayerImpl::Create(host_impl().active_tree(), 2); + scoped_ptr<LayerImpl> child2 = + LayerImpl::Create(host_impl().active_tree(), 3); + scoped_ptr<LayerImpl> grand_child1 = + LayerImpl::Create(host_impl().active_tree(), 4); + + position = gfx::PointF(10.f, 10.f); + bounds = gfx::Size(50, 50); + SetLayerPropertiesForTesting( + child1.get(), identity_matrix, anchor, position, bounds, true, false); + child1->SetDrawsContent(true); + child1->SetForceRenderSurface(true); + + position = gfx::PointF(50.f, 10.f); + bounds = gfx::Size(50, 50); + SetLayerPropertiesForTesting( + child2.get(), identity_matrix, anchor, position, bounds, true, false); + child2->SetDrawsContent(true); + child2->SetForceRenderSurface(true); + + // Remember that grand_child is positioned with respect to its parent (i.e. + // child1). In screen space, the intended position is (10, 50), with size + // 100 x 50. + position = gfx::PointF(0.f, 40.f); + bounds = gfx::Size(100, 50); + SetLayerPropertiesForTesting(grand_child1.get(), + identity_matrix, + anchor, + position, + bounds, + true, + false); + grand_child1->SetDrawsContent(true); + grand_child1->SetForceRenderSurface(true); + + child1->AddChild(grand_child1.Pass()); + root->AddChild(child1.Pass()); + root->AddChild(child2.Pass()); + } + + LayerImpl* child1 = root->children()[0]; + LayerImpl* child2 = root->children()[1]; + LayerImpl* grand_child1 = child1->children()[0]; + + host_impl().SetViewportSize(root->bounds()); + host_impl().active_tree()->SetRootLayer(root.Pass()); + host_impl().active_tree()->UpdateDrawProperties(); + + // Sanity check the scenario we just created. + ASSERT_TRUE(child1); + ASSERT_TRUE(child2); + ASSERT_TRUE(grand_child1); + ASSERT_TRUE(child1->render_surface()); + ASSERT_TRUE(child2->render_surface()); + ASSERT_TRUE(grand_child1->render_surface()); + ASSERT_EQ(4u, RenderSurfaceLayerList().size()); + // The root surface has the root layer, and child1's and child2's render + // surfaces. + ASSERT_EQ(3u, root_layer()->render_surface()->layer_list().size()); + // The child1 surface has the child1 layer and grand_child1's render surface. + ASSERT_EQ(2u, child1->render_surface()->layer_list().size()); + ASSERT_EQ(1u, child2->render_surface()->layer_list().size()); + ASSERT_EQ(1u, grand_child1->render_surface()->layer_list().size()); + ASSERT_EQ(1, RenderSurfaceLayerList().at(0)->id()); // root layer + ASSERT_EQ(2, RenderSurfaceLayerList()[1]->id()); // child1 + ASSERT_EQ(4, RenderSurfaceLayerList().at(2)->id()); // grand_child1 + ASSERT_EQ(3, RenderSurfaceLayerList()[3]->id()); // child2 + + // Nothing overlaps the root_layer at (1, 1), so hit testing there should find + // the root layer. + gfx::Point test_point = gfx::Point(1, 1); + LayerImpl* result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(1, result_layer->id()); + + // At (15, 15), child1 and root are the only layers. child1 is expected to be + // on top. + test_point = gfx::Point(15, 15); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(2, result_layer->id()); + + // At (51, 20), child1 and child2 overlap. child2 is expected to be on top. + test_point = gfx::Point(51, 20); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(3, result_layer->id()); + + // At (80, 51), child2 and grand_child1 overlap. child2 is expected to be on + // top. + test_point = gfx::Point(80, 51); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(3, result_layer->id()); + + // At (51, 51), all layers overlap each other. child2 is expected to be on top + // of all other layers. + test_point = gfx::Point(51, 51); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(3, result_layer->id()); + + // At (20, 51), child1 and grand_child1 overlap. grand_child1 is expected to + // be on top. + test_point = gfx::Point(20, 51); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(4, result_layer->id()); +} + +TEST_F(LayerTreeImplTest, HitTestingForEmptyLayers) { + // Layer 1 - root + scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl().active_tree(), 1); + gfx::Transform identity_matrix; + gfx::PointF anchor; + gfx::PointF position; + gfx::Size bounds(100, 100); + SetLayerPropertiesForTesting( + root.get(), identity_matrix, anchor, position, bounds, true, false); + root->SetDrawsContent(true); + + { + // Layer 2 - empty: drawsContent=false + gfx::PointF position(10.f, 10.f); + gfx::Size bounds(30, 30); + scoped_ptr<LayerImpl> empty_layer = + LayerImpl::Create(host_impl().active_tree(), 2); + SetLayerPropertiesForTesting(empty_layer.get(), + identity_matrix, + anchor, + position, + bounds, + true, + false); + + empty_layer->SetDrawsContent(false); + root->AddChild(empty_layer.Pass()); + } + + { + // Layer 3 - empty, but has touch handler + gfx::PointF position(10.f, 60.f); + gfx::Size bounds(30, 30); + scoped_ptr<LayerImpl> test_layer = + LayerImpl::Create(host_impl().active_tree(), 3); + SetLayerPropertiesForTesting(test_layer.get(), + identity_matrix, + anchor, + position, + bounds, + true, + false); + + test_layer->SetDrawsContent(false); + Region touch_handler_region(gfx::Rect(10, 10, 10, 10)); + test_layer->SetTouchEventHandlerRegion(touch_handler_region); + root->AddChild(test_layer.Pass()); + } + + { + // Layer 4 - empty, but has mousewheel handler + gfx::PointF position(60.f, 60.f); + gfx::Size bounds(30, 30); + scoped_ptr<LayerImpl> test_layer = + LayerImpl::Create(host_impl().active_tree(), 4); + SetLayerPropertiesForTesting(test_layer.get(), + identity_matrix, + anchor, + position, + bounds, + true, + false); + + test_layer->SetDrawsContent(false); + test_layer->SetHaveWheelEventHandlers(true); + root->AddChild(test_layer.Pass()); + } + + host_impl().SetViewportSize(root->bounds()); + host_impl().active_tree()->SetRootLayer(root.Pass()); + host_impl().active_tree()->UpdateDrawProperties(); + + // Verify that the root layer and empty layers with touch/wheel handlers + // (but not the empty layer without a touch handler) are in the RSSL. + ASSERT_EQ(1u, RenderSurfaceLayerList().size()); + EXPECT_EQ(1, RenderSurfaceLayerList()[0]->id()); + ASSERT_EQ(3u, root_layer()->render_surface()->layer_list().size()); + EXPECT_EQ(1, root_layer()->render_surface()->layer_list().at(0)->id()); + EXPECT_EQ(3, root_layer()->render_surface()->layer_list().at(1)->id()); + EXPECT_EQ(4, root_layer()->render_surface()->layer_list().at(2)->id()); + + // Hit testing for a point inside the empty no-handlers layer should return + // the root layer. + gfx::Point test_point = gfx::Point(15, 15); + LayerImpl* result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(1, result_layer->id()); + + // Hit testing for a point inside the touch handler layer should return it. + test_point = gfx::Point(15, 75); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(3, result_layer->id()); + + // Hit testing for a point inside the mousewheel layer should return it. + test_point = gfx::Point(75, 75); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPoint(test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(4, result_layer->id()); +} + +TEST_F(LayerTreeImplTest, HitCheckingTouchHandlerRegionsForSingleLayer) { + scoped_ptr<LayerImpl> root = + LayerImpl::Create(host_impl().active_tree(), 12345); + + gfx::Transform identity_matrix; + Region touch_handler_region(gfx::Rect(10, 10, 50, 50)); + gfx::PointF anchor; + gfx::PointF position; + gfx::Size bounds(100, 100); + SetLayerPropertiesForTesting( + root.get(), identity_matrix, anchor, position, bounds, true, false); + root->SetDrawsContent(true); + + host_impl().SetViewportSize(root->bounds()); + host_impl().active_tree()->SetRootLayer(root.Pass()); + host_impl().active_tree()->UpdateDrawProperties(); + + // Sanity check the scenario we just created. + ASSERT_EQ(1u, RenderSurfaceLayerList().size()); + ASSERT_EQ(1u, root_layer()->render_surface()->layer_list().size()); + + // Hit checking for any point should return a null pointer for a layer without + // any touch event handler regions. + gfx::Point test_point(11, 11); + LayerImpl* result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + EXPECT_FALSE(result_layer); + + host_impl().active_tree()->root_layer()->SetTouchEventHandlerRegion( + touch_handler_region); + // Hit checking for a point outside the layer should return a null pointer. + test_point = gfx::Point(101, 101); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + EXPECT_FALSE(result_layer); + + test_point = gfx::Point(-1, -1); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + EXPECT_FALSE(result_layer); + + // Hit checking for a point inside the layer, but outside the touch handler + // region should return a null pointer. + test_point = gfx::Point(1, 1); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + EXPECT_FALSE(result_layer); + + test_point = gfx::Point(99, 99); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + EXPECT_FALSE(result_layer); + + // Hit checking for a point inside the touch event handler region should + // return the root layer. + test_point = gfx::Point(11, 11); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(12345, result_layer->id()); + + test_point = gfx::Point(59, 59); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(12345, result_layer->id()); +} + +TEST_F(LayerTreeImplTest, + HitCheckingTouchHandlerRegionsForUninvertibleTransform) { + scoped_ptr<LayerImpl> root = + LayerImpl::Create(host_impl().active_tree(), 12345); + + gfx::Transform uninvertible_transform; + uninvertible_transform.matrix().set(0, 0, 0.0); + uninvertible_transform.matrix().set(1, 1, 0.0); + uninvertible_transform.matrix().set(2, 2, 0.0); + uninvertible_transform.matrix().set(3, 3, 0.0); + ASSERT_FALSE(uninvertible_transform.IsInvertible()); + + gfx::Transform identity_matrix; + Region touch_handler_region(gfx::Rect(10, 10, 50, 50)); + gfx::PointF anchor; + gfx::PointF position; + gfx::Size bounds(100, 100); + SetLayerPropertiesForTesting(root.get(), + uninvertible_transform, + anchor, + position, + bounds, + true, + false); + root->SetDrawsContent(true); + root->SetTouchEventHandlerRegion(touch_handler_region); + + host_impl().SetViewportSize(root->bounds()); + host_impl().active_tree()->SetRootLayer(root.Pass()); + host_impl().active_tree()->UpdateDrawProperties(); + + // Sanity check the scenario we just created. + ASSERT_EQ(1u, RenderSurfaceLayerList().size()); + ASSERT_EQ(1u, root_layer()->render_surface()->layer_list().size()); + ASSERT_FALSE(root_layer()->screen_space_transform().IsInvertible()); + + // Hit checking any point should not hit the touch handler region on the + // layer. If the invertible matrix is accidentally ignored and treated like an + // identity, then the hit testing will incorrectly hit the layer when it + // shouldn't. + gfx::Point test_point(1, 1); + LayerImpl* result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + EXPECT_FALSE(result_layer); + + test_point = gfx::Point(10, 10); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + EXPECT_FALSE(result_layer); + + test_point = gfx::Point(10, 30); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + EXPECT_FALSE(result_layer); + + test_point = gfx::Point(50, 50); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + EXPECT_FALSE(result_layer); + + test_point = gfx::Point(67, 48); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + EXPECT_FALSE(result_layer); + + test_point = gfx::Point(99, 99); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + EXPECT_FALSE(result_layer); + + test_point = gfx::Point(-1, -1); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + EXPECT_FALSE(result_layer); +} + +TEST_F(LayerTreeImplTest, + HitCheckingTouchHandlerRegionsForSinglePositionedLayer) { + scoped_ptr<LayerImpl> root = + LayerImpl::Create(host_impl().active_tree(), 12345); + + gfx::Transform identity_matrix; + Region touch_handler_region(gfx::Rect(10, 10, 50, 50)); + gfx::PointF anchor; + // this layer is positioned, and hit testing should correctly know where the + // layer is located. + gfx::PointF position(50.f, 50.f); + gfx::Size bounds(100, 100); + SetLayerPropertiesForTesting( + root.get(), identity_matrix, anchor, position, bounds, true, false); + root->SetDrawsContent(true); + root->SetTouchEventHandlerRegion(touch_handler_region); + + host_impl().SetViewportSize(root->bounds()); + host_impl().active_tree()->SetRootLayer(root.Pass()); + host_impl().active_tree()->UpdateDrawProperties(); + + // Sanity check the scenario we just created. + ASSERT_EQ(1u, RenderSurfaceLayerList().size()); + ASSERT_EQ(1u, root_layer()->render_surface()->layer_list().size()); + + // Hit checking for a point outside the layer should return a null pointer. + gfx::Point test_point(49, 49); + LayerImpl* result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + EXPECT_FALSE(result_layer); + + // Even though the layer has a touch handler region containing (101, 101), it + // should not be visible there since the root render surface would clamp it. + test_point = gfx::Point(101, 101); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + EXPECT_FALSE(result_layer); + + // Hit checking for a point inside the layer, but outside the touch handler + // region should return a null pointer. + test_point = gfx::Point(51, 51); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + EXPECT_FALSE(result_layer); + + // Hit checking for a point inside the touch event handler region should + // return the root layer. + test_point = gfx::Point(61, 61); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(12345, result_layer->id()); + + test_point = gfx::Point(99, 99); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(12345, result_layer->id()); +} + +TEST_F(LayerTreeImplTest, + HitCheckingTouchHandlerRegionsForSingleLayerWithScaledContents) { + // A layer's visible content rect is actually in the layer's content space. + // The screen space transform converts from the layer's origin space to screen + // space. This test makes sure that hit testing works correctly accounts for + // the contents scale. A contents scale that is not 1 effectively forces a + // non-identity transform between layer's content space and layer's origin + // space. The hit testing code must take this into account. + // + // To test this, the layer is positioned at (25, 25), and is size (50, 50). If + // contents scale is ignored, then hit checking will mis-interpret the visible + // content rect as being larger than the actual bounds of the layer. + // + scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl().active_tree(), 1); + + gfx::Transform identity_matrix; + gfx::PointF anchor; + + SetLayerPropertiesForTesting(root.get(), + identity_matrix, + anchor, + gfx::PointF(), + gfx::Size(100, 100), + true, + false); + { + Region touch_handler_region(gfx::Rect(10, 10, 30, 30)); + gfx::PointF position(25.f, 25.f); + gfx::Size bounds(50, 50); + scoped_ptr<LayerImpl> test_layer = + LayerImpl::Create(host_impl().active_tree(), 12345); + SetLayerPropertiesForTesting(test_layer.get(), + identity_matrix, + anchor, + position, + bounds, + true, + false); + + // override content bounds and contents scale + test_layer->SetContentBounds(gfx::Size(100, 100)); + test_layer->SetContentsScale(2, 2); + + test_layer->SetDrawsContent(true); + test_layer->SetTouchEventHandlerRegion(touch_handler_region); + root->AddChild(test_layer.Pass()); + } + + host_impl().SetViewportSize(root->bounds()); + host_impl().active_tree()->SetRootLayer(root.Pass()); + host_impl().active_tree()->UpdateDrawProperties(); + + // Sanity check the scenario we just created. + // The visible content rect for test_layer is actually 100x100, even though + // its layout size is 50x50, positioned at 25x25. + LayerImpl* test_layer = + host_impl().active_tree()->root_layer()->children()[0]; + EXPECT_RECT_EQ(gfx::Rect(0, 0, 100, 100), test_layer->visible_content_rect()); + ASSERT_EQ(1u, RenderSurfaceLayerList().size()); + ASSERT_EQ(1u, root_layer()->render_surface()->layer_list().size()); + + // Hit checking for a point outside the layer should return a null pointer + // (the root layer does not draw content, so it will not be tested either). + gfx::Point test_point(76, 76); + LayerImpl* result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + EXPECT_FALSE(result_layer); + + // Hit checking for a point inside the layer, but outside the touch handler + // region should return a null pointer. + test_point = gfx::Point(26, 26); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + EXPECT_FALSE(result_layer); + + test_point = gfx::Point(34, 34); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + EXPECT_FALSE(result_layer); + + test_point = gfx::Point(65, 65); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + EXPECT_FALSE(result_layer); + + test_point = gfx::Point(74, 74); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + EXPECT_FALSE(result_layer); + + // Hit checking for a point inside the touch event handler region should + // return the root layer. + test_point = gfx::Point(35, 35); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(12345, result_layer->id()); + + test_point = gfx::Point(64, 64); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(12345, result_layer->id()); +} + +TEST_F(LayerTreeImplTest, + HitCheckingTouchHandlerRegionsForSingleLayerWithDeviceScale) { + // The layer's device_scale_factor and page_scale_factor should scale the + // content rect and we should be able to hit the touch handler region by + // scaling the points accordingly. + scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl().active_tree(), 1); + + gfx::Transform identity_matrix; + gfx::PointF anchor; + // Set the bounds of the root layer big enough to fit the child when scaled. + SetLayerPropertiesForTesting(root.get(), + identity_matrix, + anchor, + gfx::PointF(), + gfx::Size(100, 100), + true, + false); + { + Region touch_handler_region(gfx::Rect(10, 10, 30, 30)); + gfx::PointF position(25.f, 25.f); + gfx::Size bounds(50, 50); + scoped_ptr<LayerImpl> test_layer = + LayerImpl::Create(host_impl().active_tree(), 12345); + SetLayerPropertiesForTesting(test_layer.get(), + identity_matrix, + anchor, + position, + bounds, + true, + false); + + test_layer->SetDrawsContent(true); + test_layer->SetTouchEventHandlerRegion(touch_handler_region); + root->AddChild(test_layer.Pass()); + } + + float device_scale_factor = 3.f; + float page_scale_factor = 5.f; + gfx::Size scaled_bounds_for_root = gfx::ToCeiledSize( + gfx::ScaleSize(root->bounds(), device_scale_factor * page_scale_factor)); + host_impl().SetViewportSize(scaled_bounds_for_root); + + host_impl().SetDeviceScaleFactor(device_scale_factor); + host_impl().active_tree()->SetPageScaleFactorAndLimits( + page_scale_factor, page_scale_factor, page_scale_factor); + host_impl().active_tree()->SetRootLayer(root.Pass()); + host_impl().active_tree()->SetViewportLayersFromIds(1, 1, Layer::INVALID_ID); + host_impl().active_tree()->UpdateDrawProperties(); + + // Sanity check the scenario we just created. + // The visible content rect for test_layer is actually 100x100, even though + // its layout size is 50x50, positioned at 25x25. + LayerImpl* test_layer = + host_impl().active_tree()->root_layer()->children()[0]; + ASSERT_EQ(1u, RenderSurfaceLayerList().size()); + ASSERT_EQ(1u, root_layer()->render_surface()->layer_list().size()); + + // Check whether the child layer fits into the root after scaled. + EXPECT_RECT_EQ(gfx::Rect(test_layer->content_bounds()), + test_layer->visible_content_rect()); + + // Hit checking for a point outside the layer should return a null pointer + // (the root layer does not draw content, so it will not be tested either). + gfx::PointF test_point(76.f, 76.f); + test_point = + gfx::ScalePoint(test_point, device_scale_factor * page_scale_factor); + LayerImpl* result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + EXPECT_FALSE(result_layer); + + // Hit checking for a point inside the layer, but outside the touch handler + // region should return a null pointer. + test_point = gfx::Point(26, 26); + test_point = + gfx::ScalePoint(test_point, device_scale_factor * page_scale_factor); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + EXPECT_FALSE(result_layer); + + test_point = gfx::Point(34, 34); + test_point = + gfx::ScalePoint(test_point, device_scale_factor * page_scale_factor); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + EXPECT_FALSE(result_layer); + + test_point = gfx::Point(65, 65); + test_point = + gfx::ScalePoint(test_point, device_scale_factor * page_scale_factor); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + EXPECT_FALSE(result_layer); + + test_point = gfx::Point(74, 74); + test_point = + gfx::ScalePoint(test_point, device_scale_factor * page_scale_factor); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + EXPECT_FALSE(result_layer); + + // Hit checking for a point inside the touch event handler region should + // return the root layer. + test_point = gfx::Point(35, 35); + test_point = + gfx::ScalePoint(test_point, device_scale_factor * page_scale_factor); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(12345, result_layer->id()); + + test_point = gfx::Point(64, 64); + test_point = + gfx::ScalePoint(test_point, device_scale_factor * page_scale_factor); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(12345, result_layer->id()); +} + +TEST_F(LayerTreeImplTest, HitCheckingTouchHandlerRegionsForSimpleClippedLayer) { + // Test that hit-checking will only work for the visible portion of a layer, + // and not the entire layer bounds. Here we just test the simple axis-aligned + // case. + gfx::Transform identity_matrix; + gfx::PointF anchor; + + scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl().active_tree(), 1); + SetLayerPropertiesForTesting(root.get(), + identity_matrix, + anchor, + gfx::PointF(), + gfx::Size(100, 100), + true, + false); + { + scoped_ptr<LayerImpl> clipping_layer = + LayerImpl::Create(host_impl().active_tree(), 123); + // this layer is positioned, and hit testing should correctly know where the + // layer is located. + gfx::PointF position(25.f, 25.f); + gfx::Size bounds(50, 50); + SetLayerPropertiesForTesting(clipping_layer.get(), + identity_matrix, + anchor, + position, + bounds, + true, + false); + clipping_layer->SetMasksToBounds(true); + + scoped_ptr<LayerImpl> child = + LayerImpl::Create(host_impl().active_tree(), 456); + Region touch_handler_region(gfx::Rect(10, 10, 50, 50)); + position = gfx::PointF(-50.f, -50.f); + bounds = gfx::Size(300, 300); + SetLayerPropertiesForTesting( + child.get(), identity_matrix, anchor, position, bounds, true, false); + child->SetDrawsContent(true); + child->SetTouchEventHandlerRegion(touch_handler_region); + clipping_layer->AddChild(child.Pass()); + root->AddChild(clipping_layer.Pass()); + } + + host_impl().SetViewportSize(root->bounds()); + host_impl().active_tree()->SetRootLayer(root.Pass()); + host_impl().active_tree()->UpdateDrawProperties(); + + // Sanity check the scenario we just created. + ASSERT_EQ(1u, RenderSurfaceLayerList().size()); + ASSERT_EQ(1u, root_layer()->render_surface()->layer_list().size()); + ASSERT_EQ(456, root_layer()->render_surface()->layer_list().at(0)->id()); + + // Hit checking for a point outside the layer should return a null pointer. + // Despite the child layer being very large, it should be clipped to the root + // layer's bounds. + gfx::Point test_point(24, 24); + LayerImpl* result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + EXPECT_FALSE(result_layer); + + // Hit checking for a point inside the layer, but outside the touch handler + // region should return a null pointer. + test_point = gfx::Point(35, 35); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + EXPECT_FALSE(result_layer); + + test_point = gfx::Point(74, 74); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + EXPECT_FALSE(result_layer); + + // Hit checking for a point inside the touch event handler region should + // return the root layer. + test_point = gfx::Point(25, 25); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(456, result_layer->id()); + + test_point = gfx::Point(34, 34); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(456, result_layer->id()); +} + +TEST_F(LayerTreeImplTest, HitCheckingTouchHandlerOverlappingRegions) { + gfx::Transform identity_matrix; + gfx::PointF anchor; + + scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl().active_tree(), 1); + SetLayerPropertiesForTesting(root.get(), + identity_matrix, + anchor, + gfx::PointF(), + gfx::Size(100, 100), + true, + false); + { + scoped_ptr<LayerImpl> touch_layer = + LayerImpl::Create(host_impl().active_tree(), 123); + // this layer is positioned, and hit testing should correctly know where the + // layer is located. + gfx::PointF position; + gfx::Size bounds(50, 50); + SetLayerPropertiesForTesting(touch_layer.get(), + identity_matrix, + anchor, + position, + bounds, + true, + false); + touch_layer->SetDrawsContent(true); + touch_layer->SetTouchEventHandlerRegion(gfx::Rect(0, 0, 50, 50)); + root->AddChild(touch_layer.Pass()); + } + + { + scoped_ptr<LayerImpl> notouch_layer = + LayerImpl::Create(host_impl().active_tree(), 1234); + // this layer is positioned, and hit testing should correctly know where the + // layer is located. + gfx::PointF position(0, 25); + gfx::Size bounds(50, 50); + SetLayerPropertiesForTesting(notouch_layer.get(), + identity_matrix, + anchor, + position, + bounds, + true, + false); + notouch_layer->SetDrawsContent(true); + root->AddChild(notouch_layer.Pass()); + } + + host_impl().SetViewportSize(root->bounds()); + host_impl().active_tree()->SetRootLayer(root.Pass()); + host_impl().active_tree()->UpdateDrawProperties(); + + // Sanity check the scenario we just created. + ASSERT_EQ(1u, RenderSurfaceLayerList().size()); + ASSERT_EQ(2u, root_layer()->render_surface()->layer_list().size()); + ASSERT_EQ(123, root_layer()->render_surface()->layer_list().at(0)->id()); + ASSERT_EQ(1234, root_layer()->render_surface()->layer_list().at(1)->id()); + + gfx::Point test_point(35, 35); + LayerImpl* result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + + // We should have passed through the no-touch layer and found the layer + // behind it. + EXPECT_TRUE(result_layer); + + host_impl().active_tree()->LayerById(1234)->SetContentsOpaque(true); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + + // Even with an opaque layer in the middle, we should still find the layer + // with + // the touch handler behind it (since we can't assume that opaque layers are + // opaque to hit testing). + EXPECT_TRUE(result_layer); + + test_point = gfx::Point(35, 15); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + ASSERT_TRUE(result_layer); + EXPECT_EQ(123, result_layer->id()); + + test_point = gfx::Point(35, 65); + result_layer = + host_impl().active_tree()->FindLayerThatIsHitByPointInTouchHandlerRegion( + test_point); + EXPECT_FALSE(result_layer); +} + +} // namespace +} // namespace cc |