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 LayerType
* inner_viewport_scroll_layer
,
44 const LayerType
* outer_viewport_scroll_layer
,
45 const gfx::Vector2dF
& elastic_overscroll
,
46 const LayerType
* elastic_overscroll_application_layer
,
48 bool can_use_lcd_text
,
49 bool layers_always_allowed_lcd_text
,
50 bool can_render_to_separate_surface
,
51 bool can_adjust_raster_scales
,
52 bool verify_property_trees
,
53 RenderSurfaceLayerListType
* render_surface_layer_list
,
54 int current_render_surface_layer_list_id
,
55 PropertyTrees
* property_trees
)
56 : root_layer(root_layer
),
57 device_viewport_size(device_viewport_size
),
58 device_transform(device_transform
),
59 device_scale_factor(device_scale_factor
),
60 page_scale_factor(page_scale_factor
),
61 page_scale_layer(page_scale_layer
),
62 inner_viewport_scroll_layer(inner_viewport_scroll_layer
),
63 outer_viewport_scroll_layer(outer_viewport_scroll_layer
),
64 elastic_overscroll(elastic_overscroll
),
65 elastic_overscroll_application_layer(
66 elastic_overscroll_application_layer
),
67 max_texture_size(max_texture_size
),
68 can_use_lcd_text(can_use_lcd_text
),
69 layers_always_allowed_lcd_text(layers_always_allowed_lcd_text
),
70 can_render_to_separate_surface(can_render_to_separate_surface
),
71 can_adjust_raster_scales(can_adjust_raster_scales
),
72 verify_property_trees(verify_property_trees
),
73 render_surface_layer_list(render_surface_layer_list
),
74 current_render_surface_layer_list_id(
75 current_render_surface_layer_list_id
),
76 property_trees(property_trees
) {}
78 LayerType
* root_layer
;
79 gfx::Size device_viewport_size
;
80 gfx::Transform device_transform
;
81 float device_scale_factor
;
82 float page_scale_factor
;
83 const LayerType
* page_scale_layer
;
84 const LayerType
* inner_viewport_scroll_layer
;
85 const LayerType
* outer_viewport_scroll_layer
;
86 gfx::Vector2dF elastic_overscroll
;
87 const LayerType
* elastic_overscroll_application_layer
;
89 bool can_use_lcd_text
;
90 bool layers_always_allowed_lcd_text
;
91 bool can_render_to_separate_surface
;
92 bool can_adjust_raster_scales
;
93 bool verify_property_trees
;
94 RenderSurfaceLayerListType
* render_surface_layer_list
;
95 int current_render_surface_layer_list_id
;
96 PropertyTrees
* property_trees
;
99 template <typename LayerType
, typename RenderSurfaceLayerListType
>
100 struct CalcDrawPropsInputsForTesting
101 : public CalcDrawPropsInputs
<LayerType
, RenderSurfaceLayerListType
> {
102 CalcDrawPropsInputsForTesting(
103 LayerType
* root_layer
,
104 const gfx::Size
& device_viewport_size
,
105 const gfx::Transform
& device_transform
,
106 RenderSurfaceLayerListType
* render_surface_layer_list
);
107 CalcDrawPropsInputsForTesting(
108 LayerType
* root_layer
,
109 const gfx::Size
& device_viewport_size
,
110 RenderSurfaceLayerListType
* render_surface_layer_list
);
113 typedef CalcDrawPropsInputs
<Layer
, RenderSurfaceLayerList
>
114 CalcDrawPropsMainInputs
;
115 typedef CalcDrawPropsInputsForTesting
<Layer
, RenderSurfaceLayerList
>
116 CalcDrawPropsMainInputsForTesting
;
117 static void UpdateRenderSurfaces(Layer
* root_layer
,
118 bool can_render_to_separate_surface
,
119 const gfx::Transform
& transform
,
120 bool preserves_2d_axis_alignment
);
121 static void UpdateRenderSurface(Layer
* layer
,
122 bool can_render_to_separate_surface
,
123 gfx::Transform
* transform
,
124 bool* animation_preserves_axis_alignment
);
125 static void CalculateDrawProperties(CalcDrawPropsMainInputs
* inputs
);
126 static void PreCalculateMetaInformation(Layer
* root_layer
);
127 static void PreCalculateMetaInformationForTesting(LayerImpl
* root_layer
);
128 static void PreCalculateMetaInformationForTesting(Layer
* root_layer
);
130 typedef CalcDrawPropsInputs
<LayerImpl
, LayerImplList
> CalcDrawPropsImplInputs
;
131 typedef CalcDrawPropsInputsForTesting
<LayerImpl
, LayerImplList
>
132 CalcDrawPropsImplInputsForTesting
;
133 static void CalculateDrawProperties(CalcDrawPropsImplInputs
* inputs
);
134 static void CalculateDrawProperties(
135 CalcDrawPropsImplInputsForTesting
* inputs
);
137 template <typename LayerType
>
138 static bool RenderSurfaceContributesToTarget(LayerType
*,
139 int target_surface_layer_id
);
141 template <typename LayerType
, typename Function
>
142 static void CallFunctionForSubtree(LayerType
* layer
,
143 const Function
& function
);
145 // Returns a layer with the given id if one exists in the subtree starting
146 // from the given root layer (including mask and replica layers).
147 template <typename LayerType
>
148 static LayerType
* FindLayerInSubtree(LayerType
* root_layer
, int layer_id
);
150 static Layer
* get_layer_as_raw_ptr(const LayerList
& layers
, size_t index
) {
151 return layers
[index
].get();
154 static LayerImpl
* get_layer_as_raw_ptr(const OwnedLayerImplList
& layers
,
156 return layers
[index
];
159 static LayerImpl
* get_layer_as_raw_ptr(const LayerImplList
& layers
,
161 return layers
[index
];
164 struct ScrollUpdateInfo
{
166 // TODO(miletus): Use ScrollOffset once LayerTreeHost/Blink fully supports
167 // franctional scroll offset.
168 gfx::Vector2d scroll_delta
;
172 struct CC_EXPORT ScrollAndScaleSet
{
174 ~ScrollAndScaleSet();
176 std::vector
<LayerTreeHostCommon::ScrollUpdateInfo
> scrolls
;
177 float page_scale_delta
;
178 gfx::Vector2dF elastic_overscroll_delta
;
179 float top_controls_delta
;
180 ScopedPtrVector
<SwapPromise
> swap_promises
;
183 template <typename LayerType
>
184 bool LayerTreeHostCommon::RenderSurfaceContributesToTarget(
186 int target_surface_layer_id
) {
187 // A layer will either contribute its own content, or its render surface's
188 // content, to the target surface. The layer contributes its surface's content
189 // when both the following are true:
190 // (1) The layer actually has a render surface and rendering into that
192 // (2) The layer's render surface is not the same as the target surface.
194 // Otherwise, the layer just contributes itself to the target surface.
196 return layer
->render_target() == layer
&&
197 layer
->id() != target_surface_layer_id
;
200 template <typename LayerType
>
201 LayerType
* LayerTreeHostCommon::FindLayerInSubtree(LayerType
* root_layer
,
206 if (root_layer
->id() == layer_id
)
209 if (root_layer
->mask_layer() && root_layer
->mask_layer()->id() == layer_id
)
210 return root_layer
->mask_layer();
212 if (root_layer
->replica_layer() &&
213 root_layer
->replica_layer()->id() == layer_id
)
214 return root_layer
->replica_layer();
216 for (size_t i
= 0; i
< root_layer
->children().size(); ++i
) {
217 if (LayerType
* found
= FindLayerInSubtree(
218 get_layer_as_raw_ptr(root_layer
->children(), i
), layer_id
))
224 template <typename LayerType
, typename Function
>
225 void LayerTreeHostCommon::CallFunctionForSubtree(LayerType
* layer
,
226 const Function
& function
) {
229 if (LayerType
* mask_layer
= layer
->mask_layer())
230 function(mask_layer
);
231 if (LayerType
* replica_layer
= layer
->replica_layer()) {
232 function(replica_layer
);
233 if (LayerType
* mask_layer
= replica_layer
->mask_layer())
234 function(mask_layer
);
237 for (size_t i
= 0; i
< layer
->children().size(); ++i
) {
238 CallFunctionForSubtree(get_layer_as_raw_ptr(layer
->children(), i
),
243 CC_EXPORT PropertyTrees
* GetPropertyTrees(Layer
* layer
);
244 CC_EXPORT PropertyTrees
* GetPropertyTrees(LayerImpl
* layer
);
246 template <typename LayerType
, typename RenderSurfaceLayerListType
>
247 LayerTreeHostCommon::CalcDrawPropsInputsForTesting
<LayerType
,
248 RenderSurfaceLayerListType
>::
249 CalcDrawPropsInputsForTesting(
250 LayerType
* root_layer
,
251 const gfx::Size
& device_viewport_size
,
252 const gfx::Transform
& device_transform
,
253 RenderSurfaceLayerListType
* render_surface_layer_list
)
254 : CalcDrawPropsInputs
<LayerType
, RenderSurfaceLayerListType
>(
256 device_viewport_size
,
265 std::numeric_limits
<int>::max() / 2,
271 render_surface_layer_list
,
273 GetPropertyTrees(root_layer
)) {
275 DCHECK(render_surface_layer_list
);
278 template <typename LayerType
, typename RenderSurfaceLayerListType
>
279 LayerTreeHostCommon::CalcDrawPropsInputsForTesting
<LayerType
,
280 RenderSurfaceLayerListType
>::
281 CalcDrawPropsInputsForTesting(
282 LayerType
* root_layer
,
283 const gfx::Size
& device_viewport_size
,
284 RenderSurfaceLayerListType
* render_surface_layer_list
)
285 : CalcDrawPropsInputs
<LayerType
, RenderSurfaceLayerListType
>(
287 device_viewport_size
,
296 std::numeric_limits
<int>::max() / 2,
302 render_surface_layer_list
,
304 GetPropertyTrees(root_layer
)) {
306 DCHECK(render_surface_layer_list
);
311 #endif // CC_TREES_LAYER_TREE_HOST_COMMON_H_