Scroll on main if impl-hit testing isn't guaranteed to be correct
[chromium-blink-merge.git] / cc / trees / layer_tree_host_common.h
blob20b2163da11f1845d3f6a0a968a26e918d1d9a0a
1 // Copyright 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CC_TREES_LAYER_TREE_HOST_COMMON_H_
6 #define CC_TREES_LAYER_TREE_HOST_COMMON_H_
8 #include <limits>
9 #include <vector>
11 #include "base/bind.h"
12 #include "base/memory/ref_counted.h"
13 #include "cc/base/cc_export.h"
14 #include "cc/base/scoped_ptr_vector.h"
15 #include "cc/layers/layer_lists.h"
16 #include "ui/gfx/rect.h"
17 #include "ui/gfx/transform.h"
18 #include "ui/gfx/vector2d.h"
20 namespace cc {
22 class LayerImpl;
23 class Layer;
25 class CC_EXPORT LayerTreeHostCommon {
26 public:
27 static gfx::Rect CalculateVisibleRect(const gfx::Rect& target_surface_rect,
28 const gfx::Rect& layer_bound_rect,
29 const gfx::Transform& transform);
31 template <typename LayerType, typename RenderSurfaceLayerListType>
32 struct CalcDrawPropsInputs {
33 public:
34 CalcDrawPropsInputs(LayerType* root_layer,
35 const gfx::Size& device_viewport_size,
36 const gfx::Transform& device_transform,
37 float device_scale_factor,
38 float page_scale_factor,
39 const LayerType* page_scale_application_layer,
40 int max_texture_size,
41 bool can_use_lcd_text,
42 bool can_render_to_separate_surface,
43 bool can_adjust_raster_scales,
44 RenderSurfaceLayerListType* render_surface_layer_list)
45 : root_layer(root_layer),
46 device_viewport_size(device_viewport_size),
47 device_transform(device_transform),
48 device_scale_factor(device_scale_factor),
49 page_scale_factor(page_scale_factor),
50 page_scale_application_layer(page_scale_application_layer),
51 max_texture_size(max_texture_size),
52 can_use_lcd_text(can_use_lcd_text),
53 can_render_to_separate_surface(can_render_to_separate_surface),
54 can_adjust_raster_scales(can_adjust_raster_scales),
55 render_surface_layer_list(render_surface_layer_list) {}
57 LayerType* root_layer;
58 gfx::Size device_viewport_size;
59 const gfx::Transform& device_transform;
60 float device_scale_factor;
61 float page_scale_factor;
62 const LayerType* page_scale_application_layer;
63 int max_texture_size;
64 bool can_use_lcd_text;
65 bool can_render_to_separate_surface;
66 bool can_adjust_raster_scales;
67 RenderSurfaceLayerListType* render_surface_layer_list;
70 template <typename LayerType, typename RenderSurfaceLayerListType>
71 struct CalcDrawPropsInputsForTesting
72 : public CalcDrawPropsInputs<LayerType, RenderSurfaceLayerListType> {
73 CalcDrawPropsInputsForTesting(
74 LayerType* root_layer,
75 const gfx::Size& device_viewport_size,
76 const gfx::Transform& device_transform,
77 RenderSurfaceLayerListType* render_surface_layer_list);
78 CalcDrawPropsInputsForTesting(
79 LayerType* root_layer,
80 const gfx::Size& device_viewport_size,
81 RenderSurfaceLayerListType* render_surface_layer_list);
83 private:
84 const gfx::Transform identity_transform_;
87 typedef CalcDrawPropsInputs<Layer, RenderSurfaceLayerList>
88 CalcDrawPropsMainInputs;
89 typedef CalcDrawPropsInputsForTesting<Layer, RenderSurfaceLayerList>
90 CalcDrawPropsMainInputsForTesting;
91 static void CalculateDrawProperties(CalcDrawPropsMainInputs* inputs);
93 typedef CalcDrawPropsInputs<LayerImpl, LayerImplList> CalcDrawPropsImplInputs;
94 typedef CalcDrawPropsInputsForTesting<LayerImpl, LayerImplList>
95 CalcDrawPropsImplInputsForTesting;
96 static void CalculateDrawProperties(CalcDrawPropsImplInputs* inputs);
98 // Performs hit testing for a given render_surface_layer_list.
99 static LayerImpl* FindFirstScrollingLayerThatIsHitByPoint(
100 const gfx::PointF& screen_space_point,
101 const LayerImplList& render_surface_layer_list);
103 // Performs hit testing for a given render_surface_layer_list.
104 static LayerImpl* FindLayerThatIsHitByPoint(
105 const gfx::PointF& screen_space_point,
106 const LayerImplList& render_surface_layer_list);
108 static LayerImpl* FindLayerThatIsHitByPointInTouchHandlerRegion(
109 const gfx::PointF& screen_space_point,
110 const LayerImplList& render_surface_layer_list);
112 static bool LayerHasTouchEventHandlersAt(
113 const gfx::PointF& screen_space_point,
114 LayerImpl* layer_impl);
116 template <typename LayerType>
117 static bool RenderSurfaceContributesToTarget(LayerType*,
118 int target_surface_layer_id);
120 template <typename LayerType>
121 static void CallFunctionForSubtree(
122 LayerType* root_layer,
123 const base::Callback<void(LayerType* layer)>& function);
125 // Returns a layer with the given id if one exists in the subtree starting
126 // from the given root layer (including mask and replica layers).
127 template <typename LayerType>
128 static LayerType* FindLayerInSubtree(LayerType* root_layer, int layer_id);
130 static Layer* get_child_as_raw_ptr(
131 const LayerList& children,
132 size_t index) {
133 return children[index].get();
136 static LayerImpl* get_child_as_raw_ptr(
137 const OwnedLayerImplList& children,
138 size_t index) {
139 return children[index];
142 struct ScrollUpdateInfo {
143 int layer_id;
144 gfx::Vector2d scroll_delta;
148 struct CC_EXPORT ScrollAndScaleSet {
149 ScrollAndScaleSet();
150 ~ScrollAndScaleSet();
152 std::vector<LayerTreeHostCommon::ScrollUpdateInfo> scrolls;
153 float page_scale_delta;
156 template <typename LayerType>
157 bool LayerTreeHostCommon::RenderSurfaceContributesToTarget(
158 LayerType* layer,
159 int target_surface_layer_id) {
160 // A layer will either contribute its own content, or its render surface's
161 // content, to the target surface. The layer contributes its surface's content
162 // when both the following are true:
163 // (1) The layer actually has a render surface, and
164 // (2) The layer's render surface is not the same as the target surface.
166 // Otherwise, the layer just contributes itself to the target surface.
168 return layer->render_surface() && layer->id() != target_surface_layer_id;
171 template <typename LayerType>
172 LayerType* LayerTreeHostCommon::FindLayerInSubtree(LayerType* root_layer,
173 int layer_id) {
174 if (!root_layer)
175 return NULL;
177 if (root_layer->id() == layer_id)
178 return root_layer;
180 if (root_layer->mask_layer() && root_layer->mask_layer()->id() == layer_id)
181 return root_layer->mask_layer();
183 if (root_layer->replica_layer() &&
184 root_layer->replica_layer()->id() == layer_id)
185 return root_layer->replica_layer();
187 for (size_t i = 0; i < root_layer->children().size(); ++i) {
188 if (LayerType* found = FindLayerInSubtree(
189 get_child_as_raw_ptr(root_layer->children(), i), layer_id))
190 return found;
192 return NULL;
195 template <typename LayerType>
196 void LayerTreeHostCommon::CallFunctionForSubtree(
197 LayerType* root_layer,
198 const base::Callback<void(LayerType* layer)>& function) {
199 function.Run(root_layer);
201 if (LayerType* mask_layer = root_layer->mask_layer())
202 function.Run(mask_layer);
203 if (LayerType* replica_layer = root_layer->replica_layer()) {
204 function.Run(replica_layer);
205 if (LayerType* mask_layer = replica_layer->mask_layer())
206 function.Run(mask_layer);
209 for (size_t i = 0; i < root_layer->children().size(); ++i) {
210 CallFunctionForSubtree(get_child_as_raw_ptr(root_layer->children(), i),
211 function);
215 template <typename LayerType, typename RenderSurfaceLayerListType>
216 LayerTreeHostCommon::CalcDrawPropsInputsForTesting<LayerType,
217 RenderSurfaceLayerListType>::
218 CalcDrawPropsInputsForTesting(
219 LayerType* root_layer,
220 const gfx::Size& device_viewport_size,
221 const gfx::Transform& device_transform,
222 RenderSurfaceLayerListType* render_surface_layer_list)
223 : CalcDrawPropsInputs<LayerType, RenderSurfaceLayerListType>(
224 root_layer,
225 device_viewport_size,
226 device_transform,
227 1.f,
228 1.f,
229 NULL,
230 std::numeric_limits<int>::max() / 2,
231 false,
232 true,
233 false,
234 render_surface_layer_list) {
235 DCHECK(root_layer);
236 DCHECK(render_surface_layer_list);
239 template <typename LayerType, typename RenderSurfaceLayerListType>
240 LayerTreeHostCommon::CalcDrawPropsInputsForTesting<LayerType,
241 RenderSurfaceLayerListType>::
242 CalcDrawPropsInputsForTesting(
243 LayerType* root_layer,
244 const gfx::Size& device_viewport_size,
245 RenderSurfaceLayerListType* render_surface_layer_list)
246 : CalcDrawPropsInputs<LayerType, RenderSurfaceLayerListType>(
247 root_layer,
248 device_viewport_size,
249 identity_transform_,
250 1.f,
251 1.f,
252 NULL,
253 std::numeric_limits<int>::max() / 2,
254 false,
255 true,
256 false,
257 render_surface_layer_list) {
258 DCHECK(root_layer);
259 DCHECK(render_surface_layer_list);
262 } // namespace cc
264 #endif // CC_TREES_LAYER_TREE_HOST_COMMON_H_