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_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_layer(page_scale_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_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 typedef CalcDrawPropsInputs
<Layer
, RenderSurfaceLayerList
>
108 CalcDrawPropsMainInputs
;
109 typedef CalcDrawPropsInputsForTesting
<Layer
, RenderSurfaceLayerList
>
110 CalcDrawPropsMainInputsForTesting
;
111 static void UpdateRenderSurfaces(Layer
* root_layer
,
112 bool can_render_to_separate_surface
,
113 const gfx::Transform
& transform
,
114 bool preserves_2d_axis_alignment
);
115 static void UpdateRenderSurface(Layer
* layer
,
116 bool can_render_to_separate_surface
,
117 gfx::Transform
* transform
,
118 bool* animation_preserves_axis_alignment
);
119 static void CalculateDrawProperties(CalcDrawPropsMainInputs
* inputs
);
120 static void PreCalculateMetaInformation(Layer
* root_layer
);
121 static void PreCalculateMetaInformationForTesting(LayerImpl
* root_layer
);
122 static void PreCalculateMetaInformationForTesting(Layer
* root_layer
);
124 typedef CalcDrawPropsInputs
<LayerImpl
, LayerImplList
> CalcDrawPropsImplInputs
;
125 typedef CalcDrawPropsInputsForTesting
<LayerImpl
, LayerImplList
>
126 CalcDrawPropsImplInputsForTesting
;
127 static void CalculateDrawProperties(CalcDrawPropsImplInputs
* inputs
);
128 static void CalculateDrawProperties(
129 CalcDrawPropsImplInputsForTesting
* inputs
);
131 template <typename LayerType
>
132 static bool RenderSurfaceContributesToTarget(LayerType
*,
133 int target_surface_layer_id
);
135 template <typename LayerType
, typename Function
>
136 static void CallFunctionForSubtree(LayerType
* layer
,
137 const Function
& function
);
139 // Returns a layer with the given id if one exists in the subtree starting
140 // from the given root layer (including mask and replica layers).
141 template <typename LayerType
>
142 static LayerType
* FindLayerInSubtree(LayerType
* root_layer
, int layer_id
);
144 static Layer
* get_layer_as_raw_ptr(const LayerList
& layers
, size_t index
) {
145 return layers
[index
].get();
148 static LayerImpl
* get_layer_as_raw_ptr(const OwnedLayerImplList
& layers
,
150 return layers
[index
];
153 static LayerImpl
* get_layer_as_raw_ptr(const LayerImplList
& layers
,
155 return layers
[index
];
158 struct ScrollUpdateInfo
{
160 // TODO(miletus): Use ScrollOffset once LayerTreeHost/Blink fully supports
161 // franctional scroll offset.
162 gfx::Vector2d scroll_delta
;
166 struct CC_EXPORT ScrollAndScaleSet
{
168 ~ScrollAndScaleSet();
170 std::vector
<LayerTreeHostCommon::ScrollUpdateInfo
> scrolls
;
171 float page_scale_delta
;
172 gfx::Vector2dF elastic_overscroll_delta
;
173 float top_controls_delta
;
174 ScopedPtrVector
<SwapPromise
> swap_promises
;
177 template <typename LayerType
>
178 bool LayerTreeHostCommon::RenderSurfaceContributesToTarget(
180 int target_surface_layer_id
) {
181 // A layer will either contribute its own content, or its render surface's
182 // content, to the target surface. The layer contributes its surface's content
183 // when both the following are true:
184 // (1) The layer actually has a render surface and rendering into that
186 // (2) The layer's render surface is not the same as the target surface.
188 // Otherwise, the layer just contributes itself to the target surface.
190 return layer
->render_target() == layer
&&
191 layer
->id() != target_surface_layer_id
;
194 template <typename LayerType
>
195 LayerType
* LayerTreeHostCommon::FindLayerInSubtree(LayerType
* root_layer
,
200 if (root_layer
->id() == layer_id
)
203 if (root_layer
->mask_layer() && root_layer
->mask_layer()->id() == layer_id
)
204 return root_layer
->mask_layer();
206 if (root_layer
->replica_layer() &&
207 root_layer
->replica_layer()->id() == layer_id
)
208 return root_layer
->replica_layer();
210 for (size_t i
= 0; i
< root_layer
->children().size(); ++i
) {
211 if (LayerType
* found
= FindLayerInSubtree(
212 get_layer_as_raw_ptr(root_layer
->children(), i
), layer_id
))
218 template <typename LayerType
, typename Function
>
219 void LayerTreeHostCommon::CallFunctionForSubtree(LayerType
* layer
,
220 const Function
& function
) {
223 if (LayerType
* mask_layer
= layer
->mask_layer())
224 function(mask_layer
);
225 if (LayerType
* replica_layer
= layer
->replica_layer()) {
226 function(replica_layer
);
227 if (LayerType
* mask_layer
= replica_layer
->mask_layer())
228 function(mask_layer
);
231 for (size_t i
= 0; i
< layer
->children().size(); ++i
) {
232 CallFunctionForSubtree(get_layer_as_raw_ptr(layer
->children(), i
),
237 CC_EXPORT PropertyTrees
* GetPropertyTrees(Layer
* layer
);
238 CC_EXPORT PropertyTrees
* GetPropertyTrees(LayerImpl
* layer
);
240 template <typename LayerType
, typename RenderSurfaceLayerListType
>
241 LayerTreeHostCommon::CalcDrawPropsInputsForTesting
<LayerType
,
242 RenderSurfaceLayerListType
>::
243 CalcDrawPropsInputsForTesting(
244 LayerType
* root_layer
,
245 const gfx::Size
& device_viewport_size
,
246 const gfx::Transform
& device_transform
,
247 RenderSurfaceLayerListType
* render_surface_layer_list
)
248 : CalcDrawPropsInputs
<LayerType
, RenderSurfaceLayerListType
>(
250 device_viewport_size
,
257 std::numeric_limits
<int>::max() / 2,
263 render_surface_layer_list
,
265 GetPropertyTrees(root_layer
)) {
267 DCHECK(render_surface_layer_list
);
270 template <typename LayerType
, typename RenderSurfaceLayerListType
>
271 LayerTreeHostCommon::CalcDrawPropsInputsForTesting
<LayerType
,
272 RenderSurfaceLayerListType
>::
273 CalcDrawPropsInputsForTesting(
274 LayerType
* root_layer
,
275 const gfx::Size
& device_viewport_size
,
276 RenderSurfaceLayerListType
* render_surface_layer_list
)
277 : CalcDrawPropsInputs
<LayerType
, RenderSurfaceLayerListType
>(
279 device_viewport_size
,
286 std::numeric_limits
<int>::max() / 2,
292 render_surface_layer_list
,
294 GetPropertyTrees(root_layer
)) {
296 DCHECK(render_surface_layer_list
);
301 #endif // CC_TREES_LAYER_TREE_HOST_COMMON_H_