Chromium Code Reviews| Index: cc/surfaces/surface_hittest.cc |
| diff --git a/cc/surfaces/surface_hittest.cc b/cc/surfaces/surface_hittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6e2580bf6301e6b75630fc8b0389462ad9c93fee |
| --- /dev/null |
| +++ b/cc/surfaces/surface_hittest.cc |
| @@ -0,0 +1,153 @@ |
| +// Copyright 2015 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 <iostream> |
|
jbauman
2015/08/05 23:33:22
Remove this.
lfg
2015/08/06 18:28:12
Oops. Done.
|
| + |
| +#include "cc/surfaces/surface_hittest.h" |
| + |
| +#include "cc/output/compositor_frame.h" |
| +#include "cc/output/delegated_frame_data.h" |
| +#include "cc/quads/draw_quad.h" |
| +#include "cc/quads/render_pass_draw_quad.h" |
| +#include "cc/quads/surface_draw_quad.h" |
| +#include "cc/surfaces/surface.h" |
| +#include "cc/surfaces/surface_manager.h" |
| +#include "ui/gfx/geometry/point.h" |
| +#include "ui/gfx/transform.h" |
| + |
| +namespace cc { |
| +namespace { |
| +const RenderPass* GetRootRenderPass(SurfaceManager* manager, |
| + SurfaceId surface_id) { |
| + Surface* surface = manager->GetSurfaceForId(surface_id); |
| + |
| + const CompositorFrame* surface_frame = surface->GetEligibleFrame(); |
| + if (!surface_frame) |
| + return nullptr; |
| + |
| + const DelegatedFrameData* frame_data = |
| + surface_frame->delegated_frame_data.get(); |
| + return frame_data->render_pass_list.empty() |
| + ? nullptr |
| + : frame_data->render_pass_list.back(); |
| +} |
| +} |
| + |
| +SurfaceHittest::SurfaceHittest(SurfaceManager* manager) : manager_(manager) {} |
| + |
| +SurfaceHittest::~SurfaceHittest() {} |
| + |
| +SurfaceId SurfaceHittest::Hittest(SurfaceId surface_id, |
| + const gfx::Point& point, |
| + gfx::Point* transformed_point) { |
| + SurfaceId hittest_surface_id = surface_id; |
| + |
| + if (transformed_point) |
| + *transformed_point = point; |
| + |
| + hittest_surface_id = |
| + HittestInternal(surface_id, GetRootRenderPass(manager_, surface_id), |
| + point, transformed_point); |
| + |
| + referenced_surfaces_.clear(); |
| + |
| + return hittest_surface_id; |
| +} |
| + |
| +SurfaceId SurfaceHittest::HittestInternal(SurfaceId surface_id, |
| + const RenderPass* render_pass, |
| + const gfx::Point& point, |
| + gfx::Point* out_transformed_point) { |
| + gfx::Transform transform_from_root_target; |
| + if (!render_pass || |
| + !render_pass->transform_to_root_target.GetInverse( |
| + &transform_from_root_target)) { |
| + return surface_id; |
| + } |
| + |
| + gfx::Point point_in_target_space(point); |
| + transform_from_root_target.TransformPoint(&point_in_target_space); |
| + |
| + for (const auto* quad : render_pass->quad_list) { |
| + // First we test against the clip_rect. The clip_rect is in target space, so |
| + // we can test the point directly. |
| + if (!quad->shared_quad_state->is_clipped || |
| + (point_in_target_space.x() > quad->shared_quad_state->clip_rect.x() && |
|
jbauman
2015/08/05 23:33:22
quad->shared_quad_state->clip_rect.Contains(point_
lfg
2015/08/06 18:28:12
Done. Just a note: in the previous code I was also
|
| + point_in_target_space.y() > quad->shared_quad_state->clip_rect.y() && |
| + point_in_target_space.x() <= |
| + quad->shared_quad_state->clip_rect.x() + |
| + quad->shared_quad_state->clip_rect.width() && |
| + point_in_target_space.y() <= |
| + quad->shared_quad_state->clip_rect.y() + |
| + quad->shared_quad_state->clip_rect.height())) { |
| + // We now transform the point to content space and test if it hits the |
| + // rect. |
| + gfx::Transform target_to_quad_transform; |
| + if (quad->shared_quad_state->quad_to_target_transform.GetInverse( |
| + &target_to_quad_transform)) { |
| + gfx::Point transformed_point(point_in_target_space); |
| + target_to_quad_transform.TransformPoint(&transformed_point); |
| + |
| + if (transformed_point.x() > quad->rect.x() && |
|
jbauman
2015/08/05 23:33:22
Contains here too.
lfg
2015/08/06 18:28:12
Done.
|
| + transformed_point.y() > quad->rect.y() && |
| + transformed_point.x() <= quad->rect.x() + quad->rect.width() && |
| + transformed_point.y() <= quad->rect.y() + quad->rect.height()) { |
| + if (quad->material == DrawQuad::SURFACE_CONTENT) { |
| + // We've hit a SurfaceDrawQuad, we need to recurse into this |
| + // Surface. |
| + const SurfaceDrawQuad* surface_quad = |
| + SurfaceDrawQuad::MaterialCast(quad); |
| + |
| + // To avoid an infinite recursion, we need to skip the surface if |
| + // it's already been referenced. |
| + if (referenced_surfaces_.find(surface_quad->surface_id) != |
| + referenced_surfaces_.end()) |
| + continue; |
| + referenced_surfaces_.insert(surface_quad->surface_id); |
|
jbauman
2015/08/05 23:33:22
The root surface could still have a reference to i
lfg
2015/08/06 18:28:12
I've fixed it when moving to use referenced passes
|
| + |
| + if (out_transformed_point) |
| + *out_transformed_point = transformed_point; |
| + |
| + const RenderPass* last_pass = |
| + GetRootRenderPass(manager_, surface_quad->surface_id); |
| + return HittestInternal(surface_quad->surface_id, last_pass, |
| + transformed_point, out_transformed_point); |
| + } else if (quad->material == DrawQuad::RENDER_PASS) { |
| + // We've hit a RenderPassDrawQuad, we need to recurse into this |
| + // RenderPass. |
| + const RenderPassDrawQuad* render_quad = |
| + RenderPassDrawQuad::MaterialCast(quad); |
| + |
| + Surface* surface = manager_->GetSurfaceForId(surface_id); |
| + const CompositorFrame* surface_frame = |
| + surface->GetEligibleFrame(); |
| + DCHECK(surface_frame); |
| + const DelegatedFrameData* frame_data = |
| + surface_frame->delegated_frame_data.get(); |
| + |
| + const RenderPass* quad_render_pass; |
| + for (const auto* render_pass : frame_data->render_pass_list) { |
| + if (render_pass->id == render_quad->render_pass_id) { |
| + quad_render_pass = render_pass; |
|
jbauman
2015/08/05 23:33:22
It's possible that two passes could have RenderPas
lfg
2015/08/06 18:28:12
Done.
|
| + break; |
| + } |
| + } |
| + |
| + return HittestInternal(surface_id, quad_render_pass, |
|
jbauman
2015/08/05 23:33:22
Just thought of one last possible issue - it's pos
lfg
2015/08/06 18:28:12
Done. This was a bit more complicated, as I have t
|
| + point_in_target_space, |
| + out_transformed_point); |
| + } else { |
| + // We've hit a different type of quad in the current Surface, |
| + // there's no need to iterate anymore, this is the quad that |
| + // receives the event; |
| + return surface_id; |
| + } |
| + } |
| + } |
| + } |
| + } |
| + |
| + return surface_id; |
| +} |
| +} // namespace cc |