Revert 268405 "Make sure that ScratchBuffer::Allocate() always r..."
[chromium-blink-merge.git] / cc / trees / layer_tree_host_common.h
blobde9a713ccb6b0f44488490c4d1f1d30e23104f6d
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_layer_as_raw_ptr(const LayerList& layers, size_t index) {
131 return layers[index].get();
134 static LayerImpl* get_layer_as_raw_ptr(const OwnedLayerImplList& layers,
135 size_t index) {
136 return layers[index];
139 static LayerImpl* get_layer_as_raw_ptr(const LayerImplList& layers,
140 size_t index) {
141 return layers[index];
144 struct ScrollUpdateInfo {
145 int layer_id;
146 gfx::Vector2d scroll_delta;
150 struct CC_EXPORT ScrollAndScaleSet {
151 ScrollAndScaleSet();
152 ~ScrollAndScaleSet();
154 std::vector<LayerTreeHostCommon::ScrollUpdateInfo> scrolls;
155 float page_scale_delta;
158 template <typename LayerType>
159 bool LayerTreeHostCommon::RenderSurfaceContributesToTarget(
160 LayerType* layer,
161 int target_surface_layer_id) {
162 // A layer will either contribute its own content, or its render surface's
163 // content, to the target surface. The layer contributes its surface's content
164 // when both the following are true:
165 // (1) The layer actually has a render surface, and
166 // (2) The layer's render surface is not the same as the target surface.
168 // Otherwise, the layer just contributes itself to the target surface.
170 return layer->render_surface() && layer->id() != target_surface_layer_id;
173 template <typename LayerType>
174 LayerType* LayerTreeHostCommon::FindLayerInSubtree(LayerType* root_layer,
175 int layer_id) {
176 if (!root_layer)
177 return NULL;
179 if (root_layer->id() == layer_id)
180 return root_layer;
182 if (root_layer->mask_layer() && root_layer->mask_layer()->id() == layer_id)
183 return root_layer->mask_layer();
185 if (root_layer->replica_layer() &&
186 root_layer->replica_layer()->id() == layer_id)
187 return root_layer->replica_layer();
189 for (size_t i = 0; i < root_layer->children().size(); ++i) {
190 if (LayerType* found = FindLayerInSubtree(
191 get_layer_as_raw_ptr(root_layer->children(), i), layer_id))
192 return found;
194 return NULL;
197 template <typename LayerType>
198 void LayerTreeHostCommon::CallFunctionForSubtree(
199 LayerType* root_layer,
200 const base::Callback<void(LayerType* layer)>& function) {
201 function.Run(root_layer);
203 if (LayerType* mask_layer = root_layer->mask_layer())
204 function.Run(mask_layer);
205 if (LayerType* replica_layer = root_layer->replica_layer()) {
206 function.Run(replica_layer);
207 if (LayerType* mask_layer = replica_layer->mask_layer())
208 function.Run(mask_layer);
211 for (size_t i = 0; i < root_layer->children().size(); ++i) {
212 CallFunctionForSubtree(get_layer_as_raw_ptr(root_layer->children(), i),
213 function);
217 template <typename LayerType, typename RenderSurfaceLayerListType>
218 LayerTreeHostCommon::CalcDrawPropsInputsForTesting<LayerType,
219 RenderSurfaceLayerListType>::
220 CalcDrawPropsInputsForTesting(
221 LayerType* root_layer,
222 const gfx::Size& device_viewport_size,
223 const gfx::Transform& device_transform,
224 RenderSurfaceLayerListType* render_surface_layer_list)
225 : CalcDrawPropsInputs<LayerType, RenderSurfaceLayerListType>(
226 root_layer,
227 device_viewport_size,
228 device_transform,
229 1.f,
230 1.f,
231 NULL,
232 std::numeric_limits<int>::max() / 2,
233 false,
234 true,
235 false,
236 render_surface_layer_list) {
237 DCHECK(root_layer);
238 DCHECK(render_surface_layer_list);
241 template <typename LayerType, typename RenderSurfaceLayerListType>
242 LayerTreeHostCommon::CalcDrawPropsInputsForTesting<LayerType,
243 RenderSurfaceLayerListType>::
244 CalcDrawPropsInputsForTesting(
245 LayerType* root_layer,
246 const gfx::Size& device_viewport_size,
247 RenderSurfaceLayerListType* render_surface_layer_list)
248 : CalcDrawPropsInputs<LayerType, RenderSurfaceLayerListType>(
249 root_layer,
250 device_viewport_size,
251 identity_transform_,
252 1.f,
253 1.f,
254 NULL,
255 std::numeric_limits<int>::max() / 2,
256 false,
257 true,
258 false,
259 render_surface_layer_list) {
260 DCHECK(root_layer);
261 DCHECK(render_surface_layer_list);
264 } // namespace cc
266 #endif // CC_TREES_LAYER_TREE_HOST_COMMON_H_