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_
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 "cc/trees/property_tree.h"
17 #include "ui/gfx/geometry/rect.h"
18 #include "ui/gfx/geometry/vector2d.h"
19 #include "ui/gfx/transform.h"
28 class CC_EXPORT LayerTreeHostCommon
{
30 static gfx::Rect
CalculateVisibleRect(const gfx::Rect
& target_surface_rect
,
31 const gfx::Rect
& layer_bound_rect
,
32 const gfx::Transform
& transform
);
34 template <typename LayerType
, typename RenderSurfaceLayerListType
>
35 struct CalcDrawPropsInputs
{
37 CalcDrawPropsInputs(LayerType
* root_layer
,
38 const gfx::Size
& device_viewport_size
,
39 const gfx::Transform
& device_transform
,
40 float device_scale_factor
,
41 float page_scale_factor
,
42 const LayerType
* page_scale_application_layer
,
43 const gfx::Vector2dF
& elastic_overscroll
,
44 const LayerType
* elastic_overscroll_application_layer
,
46 bool can_use_lcd_text
,
47 bool layers_always_allowed_lcd_text
,
48 bool can_render_to_separate_surface
,
49 bool can_adjust_raster_scales
,
50 bool verify_property_trees
,
51 RenderSurfaceLayerListType
* render_surface_layer_list
,
52 int current_render_surface_layer_list_id
,
53 PropertyTrees
* property_trees
)
54 : root_layer(root_layer
),
55 device_viewport_size(device_viewport_size
),
56 device_transform(device_transform
),
57 device_scale_factor(device_scale_factor
),
58 page_scale_factor(page_scale_factor
),
59 page_scale_application_layer(page_scale_application_layer
),
60 elastic_overscroll(elastic_overscroll
),
61 elastic_overscroll_application_layer(
62 elastic_overscroll_application_layer
),
63 max_texture_size(max_texture_size
),
64 can_use_lcd_text(can_use_lcd_text
),
65 layers_always_allowed_lcd_text(layers_always_allowed_lcd_text
),
66 can_render_to_separate_surface(can_render_to_separate_surface
),
67 can_adjust_raster_scales(can_adjust_raster_scales
),
68 verify_property_trees(verify_property_trees
),
69 render_surface_layer_list(render_surface_layer_list
),
70 current_render_surface_layer_list_id(
71 current_render_surface_layer_list_id
),
72 property_trees(property_trees
) {}
74 LayerType
* root_layer
;
75 gfx::Size device_viewport_size
;
76 gfx::Transform device_transform
;
77 float device_scale_factor
;
78 float page_scale_factor
;
79 const LayerType
* page_scale_application_layer
;
80 gfx::Vector2dF elastic_overscroll
;
81 const LayerType
* elastic_overscroll_application_layer
;
83 bool can_use_lcd_text
;
84 bool layers_always_allowed_lcd_text
;
85 bool can_render_to_separate_surface
;
86 bool can_adjust_raster_scales
;
87 bool verify_property_trees
;
88 RenderSurfaceLayerListType
* render_surface_layer_list
;
89 int current_render_surface_layer_list_id
;
90 PropertyTrees
* property_trees
;
93 template <typename LayerType
, typename RenderSurfaceLayerListType
>
94 struct CalcDrawPropsInputsForTesting
95 : public CalcDrawPropsInputs
<LayerType
, RenderSurfaceLayerListType
> {
96 CalcDrawPropsInputsForTesting(
97 LayerType
* root_layer
,
98 const gfx::Size
& device_viewport_size
,
99 const gfx::Transform
& device_transform
,
100 RenderSurfaceLayerListType
* render_surface_layer_list
);
101 CalcDrawPropsInputsForTesting(
102 LayerType
* root_layer
,
103 const gfx::Size
& device_viewport_size
,
104 RenderSurfaceLayerListType
* render_surface_layer_list
);
107 PropertyTrees temporary_property_trees
;
110 typedef CalcDrawPropsInputs
<Layer
, RenderSurfaceLayerList
>
111 CalcDrawPropsMainInputs
;
112 typedef CalcDrawPropsInputsForTesting
<Layer
, RenderSurfaceLayerList
>
113 CalcDrawPropsMainInputsForTesting
;
114 static void UpdateRenderSurfaces(Layer
* root_layer
,
115 bool can_render_to_separate_surface
,
116 const gfx::Transform
& transform
,
117 bool preserves_2d_axis_alignment
);
118 static void UpdateRenderSurface(Layer
* layer
,
119 bool can_render_to_separate_surface
,
120 gfx::Transform
* transform
,
121 bool* animation_preserves_axis_alignment
);
122 static void CalculateDrawProperties(CalcDrawPropsMainInputs
* inputs
);
123 static void PreCalculateMetaInformation(Layer
* root_layer
);
125 typedef CalcDrawPropsInputs
<LayerImpl
, LayerImplList
> CalcDrawPropsImplInputs
;
126 typedef CalcDrawPropsInputsForTesting
<LayerImpl
, LayerImplList
>
127 CalcDrawPropsImplInputsForTesting
;
128 static void CalculateDrawProperties(CalcDrawPropsImplInputs
* inputs
);
129 static void CalculateDrawProperties(
130 CalcDrawPropsImplInputsForTesting
* inputs
);
132 template <typename LayerType
>
133 static bool RenderSurfaceContributesToTarget(LayerType
*,
134 int target_surface_layer_id
);
136 template <typename LayerType
, typename Function
>
137 static void CallFunctionForSubtree(LayerType
* layer
,
138 const Function
& function
);
140 // Returns a layer with the given id if one exists in the subtree starting
141 // from the given root layer (including mask and replica layers).
142 template <typename LayerType
>
143 static LayerType
* FindLayerInSubtree(LayerType
* root_layer
, int layer_id
);
145 static Layer
* get_layer_as_raw_ptr(const LayerList
& layers
, size_t index
) {
146 return layers
[index
].get();
149 static LayerImpl
* get_layer_as_raw_ptr(const OwnedLayerImplList
& layers
,
151 return layers
[index
];
154 static LayerImpl
* get_layer_as_raw_ptr(const LayerImplList
& layers
,
156 return layers
[index
];
159 struct ScrollUpdateInfo
{
161 // TODO(miletus): Use ScrollOffset once LayerTreeHost/Blink fully supports
162 // franctional scroll offset.
163 gfx::Vector2d scroll_delta
;
167 struct CC_EXPORT ScrollAndScaleSet
{
169 ~ScrollAndScaleSet();
171 std::vector
<LayerTreeHostCommon::ScrollUpdateInfo
> scrolls
;
172 float page_scale_delta
;
173 gfx::Vector2dF elastic_overscroll_delta
;
174 float top_controls_delta
;
175 ScopedPtrVector
<SwapPromise
> swap_promises
;
178 template <typename LayerType
>
179 bool LayerTreeHostCommon::RenderSurfaceContributesToTarget(
181 int target_surface_layer_id
) {
182 // A layer will either contribute its own content, or its render surface's
183 // content, to the target surface. The layer contributes its surface's content
184 // when both the following are true:
185 // (1) The layer actually has a render surface and rendering into that
187 // (2) The layer's render surface is not the same as the target surface.
189 // Otherwise, the layer just contributes itself to the target surface.
191 return layer
->render_target() == layer
&&
192 layer
->id() != target_surface_layer_id
;
195 template <typename LayerType
>
196 LayerType
* LayerTreeHostCommon::FindLayerInSubtree(LayerType
* root_layer
,
201 if (root_layer
->id() == layer_id
)
204 if (root_layer
->mask_layer() && root_layer
->mask_layer()->id() == layer_id
)
205 return root_layer
->mask_layer();
207 if (root_layer
->replica_layer() &&
208 root_layer
->replica_layer()->id() == layer_id
)
209 return root_layer
->replica_layer();
211 for (size_t i
= 0; i
< root_layer
->children().size(); ++i
) {
212 if (LayerType
* found
= FindLayerInSubtree(
213 get_layer_as_raw_ptr(root_layer
->children(), i
), layer_id
))
219 template <typename LayerType
, typename Function
>
220 void LayerTreeHostCommon::CallFunctionForSubtree(LayerType
* layer
,
221 const Function
& function
) {
224 if (LayerType
* mask_layer
= layer
->mask_layer())
225 function(mask_layer
);
226 if (LayerType
* replica_layer
= layer
->replica_layer()) {
227 function(replica_layer
);
228 if (LayerType
* mask_layer
= replica_layer
->mask_layer())
229 function(mask_layer
);
232 for (size_t i
= 0; i
< layer
->children().size(); ++i
) {
233 CallFunctionForSubtree(get_layer_as_raw_ptr(layer
->children(), i
),
238 CC_EXPORT PropertyTrees
* GetPropertyTrees(Layer
* layer
,
239 PropertyTrees
* trees_from_inputs
);
240 CC_EXPORT PropertyTrees
* GetPropertyTrees(LayerImpl
* layer
,
241 PropertyTrees
* trees_from_inputs
);
243 template <typename LayerType
, typename RenderSurfaceLayerListType
>
244 LayerTreeHostCommon::CalcDrawPropsInputsForTesting
<LayerType
,
245 RenderSurfaceLayerListType
>::
246 CalcDrawPropsInputsForTesting(
247 LayerType
* root_layer
,
248 const gfx::Size
& device_viewport_size
,
249 const gfx::Transform
& device_transform
,
250 RenderSurfaceLayerListType
* render_surface_layer_list
)
251 : CalcDrawPropsInputs
<LayerType
, RenderSurfaceLayerListType
>(
253 device_viewport_size
,
260 std::numeric_limits
<int>::max() / 2,
266 render_surface_layer_list
,
268 GetPropertyTrees(root_layer
, &temporary_property_trees
)) {
270 DCHECK(render_surface_layer_list
);
273 template <typename LayerType
, typename RenderSurfaceLayerListType
>
274 LayerTreeHostCommon::CalcDrawPropsInputsForTesting
<LayerType
,
275 RenderSurfaceLayerListType
>::
276 CalcDrawPropsInputsForTesting(
277 LayerType
* root_layer
,
278 const gfx::Size
& device_viewport_size
,
279 RenderSurfaceLayerListType
* render_surface_layer_list
)
280 : CalcDrawPropsInputs
<LayerType
, RenderSurfaceLayerListType
>(
282 device_viewport_size
,
289 std::numeric_limits
<int>::max() / 2,
295 render_surface_layer_list
,
297 GetPropertyTrees(root_layer
, &temporary_property_trees
)) {
299 DCHECK(render_surface_layer_list
);
304 #endif // CC_TREES_LAYER_TREE_HOST_COMMON_H_